Java Code Examples for ucar.nc2.time.CalendarDate#toString()

The following examples show how to use ucar.nc2.time.CalendarDate#toString() . 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: CollectionSpecTable.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public String getDate() {
  CalendarDate cd = dcm.extractDate(mfile);
  if (cd != null) {
    return cd.toString();
  } else {
    return "Unknown";
  }
}
 
Example 2
Source File: Grib1SectionProductDefinition.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private String makeTimeCoord(Grib1ParamTime ptime) {
  CalendarPeriod period = GribUtils.getCalendarPeriod(getTimeUnit());
  CalendarDateUnit unit = CalendarDateUnit.of(null, period.getField(), getReferenceDate());
  int timeCoord;
  if (ptime.isInterval()) {
    int[] intv = ptime.getInterval();
    CalendarDate cdate1 = unit.makeCalendarDate(period.getValue() * intv[0]);
    CalendarDate cdate2 = unit.makeCalendarDate(period.getValue() * intv[1]);
    return "(" + cdate1 + "," + cdate2 + ")";
  } else {
    timeCoord = ptime.getForecastTime();
    CalendarDate cdate = unit.makeCalendarDate(period.getValue() * timeCoord);
    return cdate.toString();
  }
}
 
Example 3
Source File: TestDefaultCalendars.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Test
public void testCfDefaultCalendar() throws IOException {
  String failMessage, found, expected;
  boolean testCond;

  String tstFile = TestDir.cdmLocalTestDataDir + "dataset/cfMissingCalendarAttr.nc";

  // open the test file
  NetcdfDataset ncd = NetcdfDataset.openDataset(tstFile);

  // make sure this dataset used the cfConvention
  expected = cfConvention;
  found = ncd.getConventionUsed();
  testCond = found.equals(expected);
  failMessage =
      format("This dataset used the %s convention, but should have used the %s convention.", found, expected);
  Assert.assertTrue(failMessage, testCond);

  // get the Time Coordinate Axis and read the values
  CoordinateAxis tca = ncd.findCoordinateAxis(AxisType.Time);
  Array times = tca.read();
  ncd.close();

  // first date in this file is 90 [hours since 2015-12-18T06:00:00],
  // which is 2015-12-22 00:00:00\
  expected = "90";
  found = Integer.toString(times.getInt(0));
  testCond = found.equals(expected);
  failMessage = format("The first value in the times array should be %s. I got %s instead.", expected, found);
  Assert.assertTrue(failMessage, testCond);

  // look for the calendar attached to the time variable...if there isn't one,
  // then a default was not set and the assert will fail.
  Calendar cal = Calendar.get(tca.findAttributeIgnoreCase(CF.CALENDAR).getStringValue());
  expected = defaultCFCalendar.toString();
  found = cal.toString();
  testCond = found.equals(expected);
  failMessage =
      format("The calendar should equal %s, but got %s instead. Failed to set a default calendar.", expected, found);
  Assert.assertTrue(failMessage, testCond);

  // convert the time value to a CalendarDate
  CoordinateAxisTimeHelper coordAxisTimeHelper =
      new CoordinateAxisTimeHelper(cal, tca.findAttributeIgnoreCase("units").getStringValue());
  CalendarDate date = coordAxisTimeHelper.makeCalendarDateFromOffset(times.getInt(0));

  // create the correct date as requested from NCSS
  String correctIsoDateTimeString = "2015-12-22T00:00:00Z";
  CalendarDate correctDate = CalendarDate.parseISOformat(defaultCFCalendar.toString(), correctIsoDateTimeString);

  // If everything is correct, then the date and correct date should be the same
  expected = correctDate.toString();
  found = date.toString();
  testCond = date.equals(correctDate);
  failMessage = format("The correct date is %s, but I got %s instead.", expected, found);
  Assert.assertTrue(failMessage, testCond);
}
 
Example 4
Source File: TestDefaultCalendars.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Test
public void testCoardsDefaultCalendar() throws IOException {
  String failMessage, found, expected;
  boolean testCond;

  String tstFile = TestDir.cdmLocalTestDataDir + "dataset/coardsMissingCalendarAttr.nc";

  // open the test file
  NetcdfDataset ncd = NetcdfDataset.openDataset(tstFile);

  // make sure this dataset used the coardsConvention
  found = ncd.getConventionUsed();
  expected = coardsConvention;
  testCond = found.equals(expected);
  failMessage =
      format("This dataset used the %s convention, but should have used the %s convention.", found, expected);
  Assert.assertTrue(failMessage, testCond);

  // get the Time Coordinate Axis and read the values
  CoordinateAxis tca = ncd.findCoordinateAxis(AxisType.Time);
  Array times = tca.read();
  ncd.close();

  // first date in this file is 1.766292E7 [hours since 0001-01-01T00:00:00],
  // which is 2008-06-27 00:00:00
  found = Integer.toString(times.getInt(0));
  expected = "17662920";
  testCond = found.equals(expected);
  failMessage = format("The first value in the times array should be %s. I got %s instead.", expected, found);
  Assert.assertTrue(failMessage, testCond);

  // look for the calendar attached to the time variable...if there isn't one,
  // then a default was not set and the assert will fail.
  Calendar cal = Calendar.get(tca.findAttributeIgnoreCase(CF.CALENDAR).getStringValue());
  found = cal.toString();
  expected = defaultCoardsCalendar.toString();
  testCond = found.equals(expected);
  failMessage =
      format("The calendar should equal %s, but got %s instead. Failed to add a default calendar.", expected, found);
  Assert.assertTrue(failMessage, testCond);

  // convert the time value to a CalendarDate
  CoordinateAxisTimeHelper coordAxisTimeHelper =
      new CoordinateAxisTimeHelper(cal, tca.findAttributeIgnoreCase("units").getStringValue());
  CalendarDate date = coordAxisTimeHelper.makeCalendarDateFromOffset(times.getInt(0));

  // read the correct date from the time attribute and turn it into a CalendarDate
  String correctIsoDateTimeString = tca.findAttributeIgnoreCase("correct_iso_time_value_str").getStringValue();
  CalendarDate correctDate = CalendarDate.parseISOformat(defaultCoardsCalendar.toString(), correctIsoDateTimeString);

  // If everything is correct, then the date and correct date should be the same
  found = date.toString();
  expected = correctDate.toString();
  testCond = found.equals(expected);
  failMessage = format("The correct date is %s, but I got %s instead.", expected, found);
  Assert.assertTrue(failMessage, testCond);
}
 
Example 5
Source File: Grib2CollectionPanel.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public final String getForecastDate() {
  CalendarDate cd = cust.getForecastDate(gr);
  return cd == null ? null : cd.toString();
}