Java Code Examples for ucar.nc2.dataset.NetcdfDataset#getConventionUsed()

The following examples show how to use ucar.nc2.dataset.NetcdfDataset#getConventionUsed() . 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: TestSimpleGeom.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testLine() throws IOException {

  String failMessage, found, expected;
  boolean testCond;

  String tstFile = TestDir.cdmLocalTestDataDir + "dataset/SimpleGeos/hru_soil_moist_vlen_3hru_5timestep.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);

  // check that attributes were filled in correctly
  List<Variable> vars = ncd.getVariables();
  for (Variable v : vars) {
    if (v.findAttribute(CF.GEOMETRY) != null) {
      Assert.assertNotNull(v.findAttribute(CF.NODE_COORDINATES));
      Assert.assertNotNull(v.findAttribute(_Coordinate.Axes));
    }
  }
  ncd.close();
}
 
Example 2
Source File: TestSimpleGeom.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testPolygon() throws IOException {

  String failMessage, found, expected;
  boolean testCond;

  String tstFile = TestDir.cdmLocalTestDataDir + "dataset/SimpleGeos/outflow_3seg_5timesteps_vlen.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);

  // check that attributes were filled in correctly
  List<Variable> vars = ncd.getVariables();
  for (Variable v : vars) {
    if (v.findAttribute(CF.GEOMETRY) != null) {
      Assert.assertNotNull(v.findAttribute(CF.NODE_COORDINATES));
      Assert.assertNotNull(v.findAttribute(_Coordinate.Axes));
    }
  }
  ncd.close();
}
 
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);
}