Java Code Examples for ucar.nc2.dataset.CoordinateAxis1D#Builder

The following examples show how to use ucar.nc2.dataset.CoordinateAxis1D#Builder . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AWIPSConvention.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Nullable
private CoordinateAxis.Builder makeLonCoordAxis(int n, String xname) {
  double min = findAttributeDouble("xMin");
  double max = findAttributeDouble("xMax");
  double d = findAttributeDouble("dx");
  if (Double.isNaN(min) || Double.isNaN(max) || Double.isNaN(d))
    return null;

  CoordinateAxis1D.Builder v = CoordinateAxis1D.builder().setName(xname).setDataType(DataType.DOUBLE)
      .setParentGroupBuilder(rootGroup).setDimensionsByName(xname).setUnits(CDM.LON_UNITS).setDesc("longitude");
  v.addAttribute(new Attribute(_Coordinate.AxisType, AxisType.Lon.toString()));
  v.setAutoGen(min, d);

  double maxCalc = min + d * n;
  parseInfo.format("Created Lon Coordinate Axis (max calc= %f should be = %f)%n", maxCalc, max);
  return v;
}
 
Example 2
Source File: AWIPSConvention.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private CoordinateAxis.Builder makeLatCoordAxis(int n, String name) {
  double min = findAttributeDouble("yMin");
  double max = findAttributeDouble("yMax");
  double d = findAttributeDouble("dy");
  if (Double.isNaN(min) || Double.isNaN(max) || Double.isNaN(d))
    return null;

  CoordinateAxis1D.Builder v = CoordinateAxis1D.builder().setName(name).setDataType(DataType.DOUBLE)
      .setParentGroupBuilder(rootGroup).setDimensionsByName(name).setUnits(CDM.LAT_UNITS).setDesc("latitude");
  v.addAttribute(new Attribute(_Coordinate.AxisType, AxisType.Lat.toString()));
  v.setAutoGen(min, d);

  double maxCalc = min + d * n;
  parseInfo.format("Created Lat Coordinate Axis (max calc= %f should be = %f)%n", maxCalc, max);
  return v;
}
 
Example 3
Source File: AWIPSSatConvention.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private CoordinateAxis.Builder makeLonCoordAxis(String xname) {
  CoordinateAxis1D.Builder v = CoordinateAxis1D.builder().setName(xname).setDataType(DataType.DOUBLE)
      .setParentGroupBuilder(rootGroup).setDimensionsByName(xname).setUnits(CDM.LON_UNITS).setDesc("longitude");
  v.addAttribute(new Attribute(_Coordinate.AxisType, AxisType.Lon.toString()));
  v.setAutoGen(startx, dx);

  parseInfo.format("Created Lon Coordinate Axis = %s%n", xname);
  return v;
}
 
Example 4
Source File: AWIPSSatConvention.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private CoordinateAxis.Builder makeLatCoordAxis(String yname) {
  CoordinateAxis1D.Builder v = CoordinateAxis1D.builder().setName(yname).setDataType(DataType.DOUBLE)
      .setParentGroupBuilder(rootGroup).setDimensionsByName(yname).setUnits(CDM.LAT_UNITS).setDesc("latitude");
  v.addAttribute(new Attribute(_Coordinate.AxisType, AxisType.Lat.toString()));
  v.setAutoGen(starty, dy);
  parseInfo.format("Created Lat Coordinate Axis = %s%n", yname);
  return v;
}
 
Example 5
Source File: AWIPSConvention.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
CoordinateAxis.Builder makeXCoordAxis(String xname) {
  CoordinateAxis1D.Builder v = CoordinateAxis1D.builder().setName(xname).setDataType(DataType.DOUBLE)
      .setParentGroupBuilder(rootGroup).setDimensionsByName(xname).setUnits("km").setDesc("x on projection");
  v.setAutoGen(startx, dx);

  parseInfo.format("Created X Coordinate Axis = %s%n", xname);
  return v;
}
 
Example 6
Source File: AWIPSConvention.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
CoordinateAxis.Builder makeYCoordAxis(String yname) {
  CoordinateAxis1D.Builder v = CoordinateAxis1D.builder().setName(yname).setDataType(DataType.DOUBLE)
      .setParentGroupBuilder(rootGroup).setDimensionsByName(yname).setUnits("km").setDesc("y on projection");
  v.setAutoGen(starty, dy);

  parseInfo.format("Created Y Coordinate Axis = %s%n", yname);
  return v;
}
 
Example 7
Source File: AWIPSConvention.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Nullable
private CoordinateAxis.Builder makeTimeCoordAxisFromReference(Array vals) {
  if (!rootGroup.findVariableLocal("reftime").isPresent())
    return null;
  VariableDS.Builder refVar = (VariableDS.Builder) rootGroup.findVariableLocal("reftime").get();

  double refValue;
  try {
    Array refArray = refVar.orgVar.read();
    refValue = refArray.getDouble(refArray.getIndex()); // get the first value
  } catch (IOException ioe) {
    return null;
  }
  if (refValue == N3iosp.NC_FILL_DOUBLE) // why?
    return null;

  // construct the values array - make it a double to be safe
  Array dvals = Array.factory(DataType.DOUBLE, vals.getShape());
  IndexIterator diter = dvals.getIndexIterator();
  IndexIterator iiter = vals.getIndexIterator();
  while (iiter.hasNext())
    diter.setDoubleNext(iiter.getDoubleNext() + refValue); // add reftime to each of the values

  String name = "timeCoord";
  String units = refVar.getAttributeContainer().findAttributeString(CDM.UNITS, "seconds since 1970-1-1 00:00:00");
  units = normalize(units);
  String desc = "synthesized time coordinate from reftime, valtimeMINUSreftime";
  CoordinateAxis1D.Builder timeCoord =
      CoordinateAxis1D.builder().setName(name).setDataType(DataType.DOUBLE).setParentGroupBuilder(rootGroup)
          .setDimensionsByName("record").setUnits(units).setDesc(desc).setCachedData(dvals, true);

  parseInfo.format("Created Time Coordinate Axis From reftime Variable%n");
  return timeCoord;
}
 
Example 8
Source File: AWIPSConvention.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private CoordinateAxis.Builder makeTimeCoordAxis() {
  VariableDS.Builder timeVar = (VariableDS.Builder) rootGroup.findVariableLocal("valtimeMINUSreftime")
      .orElseThrow(() -> new RuntimeException("must have varible 'valtimeMINUSreftime'"));

  Dimension recordDim =
      rootGroup.findDimension("record").orElseThrow(() -> new RuntimeException("must have dimension 'record'"));

  Array vals;
  try {
    vals = timeVar.orgVar.read();
  } catch (IOException ioe) {
    return null;
  }

  // it seems that the record dimension does not always match valtimeMINUSreftime dimension!!
  // HAHAHAHAHAHAHAHA !
  int recLen = recordDim.getLength();
  int valLen = (int) vals.getSize();
  if (recLen != valLen) {
    try {
      vals = vals.sectionNoReduce(new int[] {0}, new int[] {recordDim.getLength()}, null);
      parseInfo.format(" corrected the TimeCoordAxis length%n");
    } catch (InvalidRangeException e) {
      parseInfo.format("makeTimeCoordAxis InvalidRangeException%n");
    }
  }

  // create the units out of the filename if possible
  String units = makeTimeUnitFromFilename(datasetBuilder.location);
  if (units == null) // ok that didnt work, try something else
    return makeTimeCoordAxisFromReference(vals);

  // create the coord axis
  String name = "timeCoord";
  String desc = "synthesized time coordinate from valtimeMINUSreftime and filename YYYYMMDD_HHMM";
  CoordinateAxis1D.Builder timeCoord =
      CoordinateAxis1D.builder().setName(name).setDataType(DataType.INT).setParentGroupBuilder(rootGroup)
          .setDimensionsByName("record").setUnits(units).setDesc(desc).setCachedData(vals, true);

  parseInfo.format("Created Time Coordinate Axis = %s%n", name);
  return timeCoord;
}
 
Example 9
Source File: M3IOConvention.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private void makeTimeCoordAxis(String timeName) {
  int start_date = findAttributeInt("SDATE");
  int start_time = findAttributeInt("STIME");
  int time_step = findAttributeInt("TSTEP");

  int year = start_date / 1000;
  int doy = start_date % 1000;
  int hour = start_time / 10000;
  start_time = start_time % 10000;
  int min = start_time / 100;
  int sec = start_time % 100;

  Calendar cal = new GregorianCalendar(new SimpleTimeZone(0, "GMT"));
  cal.clear();
  cal.set(Calendar.YEAR, year);
  cal.set(Calendar.DAY_OF_YEAR, doy);
  cal.set(Calendar.HOUR_OF_DAY, hour);
  cal.set(Calendar.MINUTE, min);
  cal.set(Calendar.SECOND, sec);

  java.text.SimpleDateFormat dateFormatOut = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  dateFormatOut.setTimeZone(TimeZone.getTimeZone("GMT"));

  String units = "seconds since " + dateFormatOut.format(cal.getTime()) + " UTC";

  // parse the time step
  hour = time_step / 10000;
  time_step = time_step % 10000;
  min = time_step / 100;
  sec = time_step % 100;
  time_step = hour * 3600 + min * 60 + sec;

  // create the coord axis
  CoordinateAxis1D.Builder timeCoord = CoordinateAxis1D.builder().setName("time").setDataType(DataType.INT)
      .setParentGroupBuilder(rootGroup).setDimensionsByName(timeName).setUnits(units)
      .setDesc("synthesized time coordinate from SDATE, STIME, STEP global attributes");
  timeCoord.setAutoGen(0, time_step);
  timeCoord.addAttribute(new Attribute(_Coordinate.AxisType, AxisType.Time.toString()));

  datasetBuilder.replaceCoordinateAxis(rootGroup, timeCoord);
}
 
Example 10
Source File: IFPSConvention.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private void createTimeCoordinate(VariableDS.Builder<?> ncVar) {
  // Time coordinate is stored in the attribute validTimes
  // One caveat is that the times have two bounds an upper and a lower

  // get the times values
  Attribute timesAtt = ncVar.getAttributeContainer().findAttribute("validTimes");
  if (timesAtt == null)
    return;
  Array timesArray = timesAtt.getValues();

  // get every other one LOOK this is awkward
  try {
    int n = (int) timesArray.getSize();
    List<Range> list = new ArrayList<>();
    list.add(new Range(0, n - 1, 2));
    timesArray = timesArray.section(list);
  } catch (InvalidRangeException e) {
    throw new IllegalStateException(e);
  }

  // make sure it matches the dimension
  DataType dtype = DataType.getType(timesArray);
  int nTimesAtt = (int) timesArray.getSize();

  // create a special dimension and coordinate variable
  Dimension dimTime = ncVar.orgVar.getDimension(0);
  int nTimesDim = dimTime.getLength();
  if (nTimesDim != nTimesAtt) {
    parseInfo.format(" **error ntimes in attribute (%d) doesnt match dimension length (%d) for variable %s%n",
        nTimesAtt, nTimesDim, ncVar.getFullName());
    return;
  }

  // add the dimension
  String dimName = ncVar.shortName + "_timeCoord";
  Dimension newDim = new Dimension(dimName, nTimesDim);
  rootGroup.addDimension(newDim);

  // add the coordinate variable
  String units = "seconds since 1970-1-1 00:00:00";
  String desc = "time coordinate for " + ncVar.shortName;

  CoordinateAxis1D.Builder timeCoord = CoordinateAxis1D.builder().setName(dimName).setDataType(dtype)
      .setParentGroupBuilder(rootGroup).setDimensionsByName(dimName).setUnits(units).setDesc(desc);
  timeCoord.setCachedData(timesArray, true);
  timeCoord.addAttribute(new Attribute(_Coordinate.AxisType, AxisType.Time.toString()));
  datasetBuilder.replaceCoordinateAxis(rootGroup, timeCoord);

  parseInfo.format(" added coordinate variable %s%n", dimName);

  // now make the original variable use the new dimension
  ArrayList<Dimension> newDims = new ArrayList<>(ncVar.getDimensions());
  newDims.set(0, newDim);
  ncVar.setDimensions(newDims);

  // better to explicitly set the coordinate system
  ncVar.addAttribute(new Attribute(_Coordinate.Axes, dimName + " yCoord xCoord"));

  // fix the attributes
  Attribute att = ncVar.getAttributeContainer().findAttribute("fillValue");
  if (att != null)
    ncVar.addAttribute(new Attribute(CDM.FILL_VALUE, att.getNumericValue()));
  att = ncVar.getAttributeContainer().findAttribute("descriptiveName");
  if (null != att)
    ncVar.addAttribute(new Attribute(CDM.LONG_NAME, att.getStringValue()));
}