Java Code Examples for ucar.nc2.units.DateUnit#getStandardDate()

The following examples show how to use ucar.nc2.units.DateUnit#getStandardDate() . 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: GradsTimeDimension.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Make a time struct from the index.
 *
 * @param timeIndex the time value index
 *
 * @return the corresponding TimeStruct
 */
public GradsTimeStruct makeTimeStruct(int timeIndex) {
  double tVal = getValues()[timeIndex];
  Date d = DateUnit.getStandardDate(tVal + " " + getUnit());
  Calendar calendar = Calendar.getInstance();
  calendar.setTimeZone(java.util.TimeZone.getTimeZone("GMT"));
  calendar.setTime(d);
  return makeTimeStruct(calendar);
}
 
Example 2
Source File: TestCalendarDateUnit.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void testDate(String udunits) {

    Date uddate = DateUnit.getStandardDate(udunits);
    CalendarDate cd = CalendarDate.parseUdunits(null, udunits);

    if (!uddate.equals(cd.toDate())) {
      System.out.printf("  BAD %s == %s != %s (diff = %d)%n", udunits, df.toDateTimeString(uddate), cd,
          cd.toDate().getTime() - uddate.getTime());
    }
  }
 
Example 3
Source File: MultiTrajectoryObsDataset.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public Date getTime(int point) throws IOException {
  return (DateUnit.getStandardDate(getTimeValue(point) + " " + timeVarUnitsString));
}
 
Example 4
Source File: MultiTrajectoryObsDataset.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public Date getNominalTimeAsDate() {
  String dateStr = getNominalTime() + " " + timeVarUnitsString;
  return DateUnit.getStandardDate(dateStr);
}
 
Example 5
Source File: MultiTrajectoryObsDataset.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public Date getObservationTimeAsDate() {
  String dateStr = getObservationTime() + " " + timeVarUnitsString;
  return DateUnit.getStandardDate(dateStr);
}
 
Example 6
Source File: Float10TrajectoryObsDataset.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public static boolean isValidFile(NetcdfDataset ds) {
  // Check that has a time dimension and a trajectory dimension.
  List list = ds.getRootGroup().getDimensions();
  if (list.size() != 2)
    return (false);
  Dimension d;
  for (int i = 0; i < 2; i++) {
    d = (Dimension) list.get(i);
    if (!d.getShortName().equals(timeDimNameDefault) && !d.getShortName().equals(trajDimNameDefault))
      return (false);
  }

  // Check that has a trajectory coordinate variable.
  Variable var = ds.getRootGroup().findVariableLocal(trajVarNameDefault);
  if (var == null)
    return (false);
  list = var.getDimensions();
  if (list.size() != 1)
    return (false);
  d = (Dimension) list.get(0);
  if (!d.getShortName().equals(trajDimNameDefault))
    return (false);

  // Check that has a time coordinate variable with units that are udunits time
  var = ds.getRootGroup().findVariableLocal(timeVarNameDefault);
  if (var == null)
    return (false);
  list = var.getDimensions();
  if (list.size() != 1)
    return (false);
  d = (Dimension) list.get(0);
  if (!d.getShortName().equals(timeDimNameDefault))
    return (false);
  String units = var.findAttribute("units").getStringValue();
  Date date = DateUnit.getStandardDate("0 " + units);
  if (date == null)
    return (false);

  // Check for variable latitude(time) with units of "deg".
  var = ds.getRootGroup().findVariableLocal(latVarNameDefault);
  if (var == null)
    return (false);
  list = var.getDimensions();
  if (list.size() != 2)
    return (false);
  for (int i = 0; i < 2; i++) {
    d = (Dimension) list.get(i);
    if (!d.getShortName().equals(timeDimNameDefault) && !d.getShortName().equals(trajDimNameDefault))
      return (false);
  }
  // units = var.findAttribute( "units").getStringValue();
  // if ( ! SimpleUnit.isCompatible( units, "degrees_north")) return( false);

  // Check for variable longitude(time) with units of "deg".
  var = ds.getRootGroup().findVariableLocal(lonVarNameDefault);
  if (var == null)
    return (false);
  list = var.getDimensions();
  if (list.size() != 2)
    return (false);
  for (int i = 0; i < 2; i++) {
    d = (Dimension) list.get(i);
    if (!d.getShortName().equals(timeDimNameDefault) && !d.getShortName().equals(trajDimNameDefault))
      return (false);
  }
  // units = var.findAttribute( "units").getStringValue();
  // if ( ! SimpleUnit.isCompatible( units, "degrees_east")) return( false);

  // Check for variable altitude(time) with units of "m".
  var = ds.getRootGroup().findVariableLocal(elevVarNameDefault);
  if (var == null)
    return (false);
  list = var.getDimensions();
  if (list.size() != 2)
    return (false);
  for (int i = 0; i < 2; i++) {
    d = (Dimension) list.get(i);
    if (!d.getShortName().equals(timeDimNameDefault) && !d.getShortName().equals(trajDimNameDefault))
      return (false);
  }
  units = var.findAttribute("units").getStringValue();
  if (!SimpleUnit.isCompatible(units, "m"))
    return (false);

  return (true);
}
 
Example 7
Source File: SingleTrajectoryObsDataset.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Setup needed for all SingleTrajectoryObsDatatypes. Can only be called once.
 *
 * Units of time varible must be udunits time units.
 * Units of latitude variable must be convertible to "degrees_north" by udunits.
 * Units of longitude variable must be convertible to "degrees_east" by udunits.
 * Units of altitude variable must be convertible to "meters" by udunits.
 *
 * @throws IllegalArgumentException if units of time, latitude, longitude, or altitude variables are not as required.
 * @throws IllegalStateException if this method has already been called.
 */
public void setTrajectoryInfo(Config trajConfig) throws IOException {
  if (timeDim != null)
    throw new IllegalStateException("The setTrajectoryInfo() method can only be called once.");

  this.trajectoryId = trajConfig.getTrajectoryId();
  this.timeDim = trajConfig.getTimeDim();
  this.timeVar = trajConfig.getTimeVar();
  this.latVar = trajConfig.getLatVar();
  this.lonVar = trajConfig.getLonVar();
  this.elevVar = trajConfig.getElevVar();

  trajectoryNumPoint = this.timeDim.getLength();
  timeVarUnitsString = this.timeVar.findAttribute("units").getStringValue();

  // Check that time, lat, lon, elev units are acceptable.
  if (DateUnit.getStandardDate(timeVarUnitsString) == null) {
    throw new IllegalArgumentException("Units of time variable <" + timeVarUnitsString + "> not a date unit.");
  }
  String latVarUnitsString = this.latVar.findAttribute("units").getStringValue();
  if (!SimpleUnit.isCompatible(latVarUnitsString, "degrees_north")) {
    throw new IllegalArgumentException(
        "Units of lat var <" + latVarUnitsString + "> not compatible with \"degrees_north\".");
  }
  String lonVarUnitsString = this.lonVar.findAttribute("units").getStringValue();
  if (!SimpleUnit.isCompatible(lonVarUnitsString, "degrees_east")) {
    throw new IllegalArgumentException(
        "Units of lon var <" + lonVarUnitsString + "> not compatible with \"degrees_east\".");
  }
  String elevVarUnitsString = this.elevVar.findAttribute("units").getStringValue();
  if (!SimpleUnit.isCompatible(elevVarUnitsString, "meters")) {
    throw new IllegalArgumentException(
        "Units of elev var <" + elevVarUnitsString + "> not compatible with \"meters\".");
  }

  try {
    elevVarUnitsConversionFactor = getMetersConversionFactor(elevVarUnitsString);
  } catch (Exception e) {
    throw new IllegalArgumentException(
        "Exception on getMetersConversionFactor() for the units of elev var <" + elevVarUnitsString + ">.");
  }

  if (this.netcdfDataset.hasUnlimitedDimension() && this.netcdfDataset.getUnlimitedDimension().equals(timeDim)) {
    Object result = this.netcdfDataset.sendIospMessage(NetcdfFile.IOSP_MESSAGE_ADD_RECORD_STRUCTURE);
    if ((result != null) && (Boolean) result)
      this.recordVar = (Structure) this.netcdfDataset.getRootGroup().findVariableLocal("record");
    else
      this.recordVar = new StructurePseudo(this.netcdfDataset, null, "record", timeDim);
  } else {
    this.recordVar = new StructurePseudo(this.netcdfDataset, null, "record", timeDim);
  }

  // @todo HACK, HACK, HACK - remove once addRecordStructure() deals with ncd attribute changes.
  Variable elevVarInRecVar = this.recordVar.findVariable(this.elevVar.getFullNameEscaped());
  if (!elevVarUnitsString.equals(elevVarInRecVar.findAttribute("units").getStringValue())) {
    elevVarInRecVar.addAttribute(new Attribute("units", elevVarUnitsString));
  }

  trajectoryVarsMap = new HashMap();
  // for ( Iterator it = this.recordVar.getVariables().iterator(); it.hasNext(); )
  for (Iterator it = this.netcdfDataset.getRootGroup().getVariables().iterator(); it.hasNext();) {
    Variable curVar = (Variable) it.next();
    if (curVar.getRank() > 0 && !curVar.equals(this.timeVar) && !curVar.equals(this.latVar)
        && !curVar.equals(this.lonVar) && !curVar.equals(this.elevVar)
        && (this.recordVar == null ? true : !curVar.equals(this.recordVar))) {
      MyTypedDataVariable typedVar = new MyTypedDataVariable(new VariableDS(null, curVar, true));
      dataVariables.add(typedVar);
      trajectoryVarsMap.put(typedVar.getShortName(), typedVar);
    }
  }

  trajectory = new SingleTrajectory(this.trajectoryId, trajectoryNumPoint, this.timeVar, timeVarUnitsString,
      this.latVar, this.lonVar, this.elevVar, dataVariables, trajectoryVarsMap);

  startDate = trajectory.getTime(0);
  endDate = trajectory.getTime(trajectoryNumPoint - 1);

  ((SingleTrajectory) trajectory).setStartDate(startDate);
  ((SingleTrajectory) trajectory).setEndDate(endDate);
}
 
Example 8
Source File: SingleTrajectoryObsDataset.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public Date getTime(int point) throws IOException {
  return (DateUnit.getStandardDate(getTimeValue(point) + " " + timeVarUnitsString));
}
 
Example 9
Source File: SingleTrajectoryObsDataset.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public Date getNominalTimeAsDate() {
  String dateStr = getNominalTime() + " " + timeVarUnitsString;
  return DateUnit.getStandardDate(dateStr);
}
 
Example 10
Source File: SingleTrajectoryObsDataset.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public Date getObservationTimeAsDate() {
  String dateStr = getObservationTime() + " " + timeVarUnitsString;
  return DateUnit.getStandardDate(dateStr);
}
 
Example 11
Source File: TrajectoryObsDatasetImpl.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 3 votes vote down vote up
/**
 * _more_
 *
 * @param point _more_
 *
 * @return _more_
 *
 * @throws IOException _more_
 */
public Date getTime(int point) throws IOException {
  if (SimpleUnit.isDateUnit(dimVarUnitsString))
    return (DateUnit.getStandardDate(getTimeValue(point) + " " + dimVarUnitsString));
  else
    return startDate;
}
 
Example 12
Source File: TrajectoryObsDatasetImpl.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 2 votes vote down vote up
/**
 * _more_
 *
 * @return _more_
 */
public Date getNominalTimeAsDate() {
  String dateStr = getNominalTime() + " " + dimVarUnitsString;
  return DateUnit.getStandardDate(dateStr);
}
 
Example 13
Source File: TrajectoryObsDatasetImpl.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 2 votes vote down vote up
/**
 * _more_
 *
 * @return _more_
 */
public Date getObservationTimeAsDate() {
  String dateStr = getObservationTime() + " " + dimVarUnitsString;
  return DateUnit.getStandardDate(dateStr);
}