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

The following examples show how to use ucar.nc2.dataset.NetcdfDataset#close() . 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: DataFactory.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Try to open Access as a NetcdfDataset.
 *
 * @param access open this Access
 * @param acquire if true, aquire the dataset, else open it
 * @param task may be null
 * @param log error message, may be null
 * @return NetcdfDataset or null if failure
 * @throws IOException on read error
 */
public NetcdfDataset openDataset(Access access, boolean acquire, ucar.nc2.util.CancelTask task, Formatter log)
    throws IOException {
  try (Result result = new Result()) {
    NetcdfDataset ncd = openDataset(access, acquire, task, result);
    if (log != null)
      log.format("%s", result.errLog);
    if (result.fatalError && ncd != null)
      ncd.close();
    return (result.fatalError) ? null : ncd;

    // result.close() will be called at the end of this block, which will close the Result's FeatureDataset, if it
    // exists. That data member won't have been set by any of the code above, so the close() will essentially be a
    // no-op.
  }
}
 
Example 2
Source File: TestAggExistingNew.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public void testNoCoords() throws IOException {
  String filename = "file:./" + TestNcMLRead.topDir + "exclude/aggExistingNoCoords.xml";
  logger.debug("{}", filename);
  NetcdfDataset ncd = null;

  try {
    ncd = NetcdfDatasets.openDataset(filename, true, null);
    Variable time = ncd.getRootGroup().findVariableLocal("time");
    Array data = time.read();
    // all missing
    // assert data.getInt(0) ==
  } finally {
    if (ncd != null)
      ncd.close();
  }
  // logger.debug("{}", ncd);
  // assert false;
}
 
Example 3
Source File: TestAggExisting.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public void testNoCoords() throws IOException {
  String filename = "file:./" + TestNcMLRead.topDir + "exclude/aggExistingNoCoords.xml";
  logger.debug("{}", filename);
  NetcdfDataset ncd = null;

  try {
    ncd = NetcdfDatasets.openDataset(filename, true, null);
    Variable time = ncd.getRootGroup().findVariableLocal("time");
    Array data = time.read();
    // all missing
    // assert data.getInt(0) ==
  } finally {
    if (ncd != null)
      ncd.close();
  }
  // logger.debug("{}", ncd);
  // assert false;
}
 
Example 4
Source File: TestAggExistingNew.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public void testNoCoords() throws IOException {
  String filename = "file:./" + TestNcMLRead.topDir + "exclude/aggExistingNoCoords.xml";
  logger.debug("{}", filename);
  NetcdfDataset ncd = null;

  try {
    ncd = NetcdfDatasets.openDataset(filename, true, null);
    Variable time = ncd.getRootGroup().findVariableLocal("time");
    Array data = time.read();
    // all missing
    // assert data.getInt(0) ==
  } finally {
    if (ncd != null)
      ncd.close();
  }
  // logger.debug("{}", ncd);
  // assert false;
}
 
Example 5
Source File: DataFactory.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Try to open as a NetcdfDataset.
 *
 * @param Dataset open this
 * @param acquire if true, aquire the dataset, else open it
 * @param task may be null
 * @param log error message, may be null
 * @return NetcdfDataset or null if failure
 * @throws IOException on read error
 */
public NetcdfDataset openDataset(Dataset Dataset, boolean acquire, ucar.nc2.util.CancelTask task, Formatter log)
    throws IOException {
  Result result = new Result();
  NetcdfDataset ncd = openDataset(Dataset, acquire, task, result);
  if (log != null)
    log.format("%s", result.errLog);
  if (result.fatalError) {
    result.close();
    if (ncd != null)
      ncd.close();
  }
  return (result.fatalError) ? null : ncd;
}
 
Example 6
Source File: TestAggExisting.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testCoordsAdded() throws IOException {
  String filename = "file:./" + TestNcMLRead.topDir + "aggExistingAddCoord.ncml";
  logger.debug("{}", filename);
  NetcdfDataset ncd = null;

  try {
    ncd = NetcdfDataset.openDataset(filename, true, null);
    logger.debug("{}", ncd);
  } finally {
    if (ncd != null)
      ncd.close();
  }
}
 
Example 7
Source File: TestAggExistingNew.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testCoordsAdded() throws IOException {
  String filename = "file:./" + TestNcMLRead.topDir + "aggExistingAddCoord.ncml";
  logger.debug("{}", filename);
  NetcdfDataset ncd = null;

  try {
    ncd = NetcdfDatasets.openDataset(filename, true, null);
    logger.debug("{}", ncd);
  } finally {
    if (ncd != null)
      ncd.close();
  }
}
 
Example 8
Source File: MemoryCounterAgentTest.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
static void testNcd() throws IOException {
  String filename = "C:/data/test2.nc";
  NetcdfDataset ncfile = NetcdfDataset.openDataset(filename);
  measureSize("C:/data/test2.nc", ncfile, null, true);

  NetcdfDatasetInfo info = new NetcdfDatasetInfo(filename);
  measureSize("info", info, null, true);
  String pifo = info.getParseInfo();
  System.out.println("info= " + pifo);
  ncfile.close();
}
 
Example 9
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 10
Source File: TestMiscPointFeature.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testTryWithWrap() throws IOException {
  String location = TestDir.cdmLocalFromTestDataDir + "testWrite.nc";
  NetcdfDataset ncd = NetcdfDataset.openDataset(location);
  Formatter errlog = new Formatter();
  try (FeatureDataset fdataset = FeatureDatasetFactoryManager.wrap(null, ncd, null, errlog)) {
    assert (fdataset == null);
  }
  ncd.close();
}
 
Example 11
Source File: TestAggExistingNew.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testCoordsAdded() throws IOException {
  String filename = "file:./" + TestNcMLRead.topDir + "aggExistingAddCoord.ncml";
  logger.debug("{}", filename);
  NetcdfDataset ncd = null;

  try {
    ncd = NetcdfDatasets.openDataset(filename, true, null);
    logger.debug("{}", ncd);
  } finally {
    if (ncd != null)
      ncd.close();
  }
}
 
Example 12
Source File: TestAggExisting.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testCoordsAdded() throws IOException {
  String filename = "file:./" + TestNcMLRead.topDir + "aggExistingAddCoord.ncml";
  logger.debug("{}", filename);
  NetcdfDataset ncd = null;

  try {
    ncd = NetcdfDatasets.openDataset(filename, true, null);
    logger.debug("{}", ncd);
  } finally {
    if (ncd != null)
      ncd.close();
  }
}
 
Example 13
Source File: DatasetManager.java    From tds with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Open a file as a GridDataset, using getNetcdfFile(), so that it gets wrapped in NcML if needed.
 */
// return null means request has been handled, and calling routine should exit without further processing
public GridDataset openGridDataset(HttpServletRequest req, HttpServletResponse res, String reqPath)
    throws IOException {
  // first look for a feature collection
  DataRootManager.DataRootMatch match = dataRootManager.findDataRootMatch(reqPath);
  if ((match != null) && (match.dataRoot.getFeatureCollection() != null)) {
    // see if its under resource control
    if (!resourceAuthorized(req, res, match.dataRoot.getRestrict()))
      return null;

    FeatureCollectionRef featCollection = match.dataRoot.getFeatureCollection();
    if (log.isDebugEnabled())
      log.debug("  -- DatasetHandler found FeatureCollection= " + featCollection);

    InvDatasetFeatureCollection fc = featureCollectionCache.get(featCollection);
    GridDataset gds = fc.getGridDataset(match.remaining);
    if (gds == null)
      throw new FileNotFoundException(reqPath);
    return gds;
  }

  // fetch it as a NetcdfFile; this deals with possible NcML
  NetcdfFile ncfile = openNetcdfFile(req, res, reqPath);
  if (ncfile == null)
    return null;

  NetcdfDataset ncd = null;
  try {
    // Convert to NetcdfDataset
    if (useNetcdfJavaBuilders || isLocationObjectStore(ncfile.getLocation())) {
      ncd = NetcdfDatasets.enhance(ncfile, NetcdfDataset.getDefaultEnhanceMode(), null);
    } else {
      ncd = NetcdfDataset.wrap(ncfile, NetcdfDataset.getDefaultEnhanceMode());
    }
    return new ucar.nc2.dt.grid.GridDataset(ncd);


  } catch (Throwable t) {
    if (ncd == null)
      ncfile.close();
    else
      ncd.close();

    if (t instanceof IOException)
      throw (IOException) t;

    String msg = ncd == null ? "Problem wrapping NetcdfFile in NetcdfDataset"
        : "Problem creating GridDataset from NetcdfDataset";
    log.error("openGridDataset(): " + msg, t);
    throw new IOException(msg + t.getMessage());
  }
}
 
Example 14
Source File: PointObsDatasetFactory.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public static PointObsDataset open(String location, ucar.nc2.util.CancelTask task, StringBuffer log)
    throws java.io.IOException {

  // otherwise open as netcdf and have a look. use NetcdfDataset in order to deal with scale/enhance, etc.
  DatasetUrl durl = DatasetUrl.findDatasetUrl(location);
  NetcdfDataset ncfile = NetcdfDataset.acquireDataset(durl, true, task);

  // add record variable if there is one.
  ncfile.sendIospMessage(NetcdfFile.IOSP_MESSAGE_ADD_RECORD_STRUCTURE);

  if (UnidataStationObsDataset.isValidFile(ncfile))
    return new UnidataStationObsDataset(ncfile);

  if (UnidataPointObsDataset.isValidFile(ncfile))
    return new UnidataPointObsDataset(ncfile);

  /*
   * if (DapperDataset.isValidFile( ncfile))
   * return DapperDataset.factory( ncfile);
   * 
   * if (SequenceObsDataset.isValidFile( ncfile))
   * return new SequenceObsDataset( ncfile, task);
   */

  if (UnidataStationObsDataset2.isValidFile(ncfile))
    return new UnidataStationObsDataset2(ncfile);

  if (NdbcDataset.isValidFile(ncfile))
    return new NdbcDataset(ncfile);

  if (MadisStationObsDataset.isValidFile(ncfile))
    return new MadisStationObsDataset(ncfile);

  if (OldUnidataStationObsDataset.isValidFile(ncfile))
    return new OldUnidataStationObsDataset(ncfile);

  // put at end to minimize false positive
  if (OldUnidataPointObsDataset.isValidFile(ncfile))
    return new OldUnidataPointObsDataset(ncfile);

  if (null != log)
    log.append("Cant find a Point/Station adapter for ").append(location);
  ncfile.close();
  return null;
}
 
Example 15
Source File: MemoryCounterAgentTest.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
static void testNcml() throws IOException {
  String filename = "C:/dev/tds/thredds/cdm/src/test/data/ncml/aggUnionSimple.xml";
  NetcdfDataset ncfile = NetcdfDataset.openDataset(filename, false, null);
  measureSize("aggUnionSimple", ncfile, null, true);
  ncfile.close();
}
 
Example 16
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 17
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 18
Source File: DatasetManager.java    From tds with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public FeatureDatasetPoint openPointDataset(HttpServletRequest req, HttpServletResponse res, String reqPath)
    throws IOException {
  // first look for a feature collection
  DataRootManager.DataRootMatch match = dataRootManager.findDataRootMatch(reqPath);
  if ((match != null) && (match.dataRoot.getFeatureCollection() != null)) {
    // see if its under resource control
    if (!resourceAuthorized(req, res, match.dataRoot.getRestrict()))
      return null;

    FeatureCollectionRef featCollection = match.dataRoot.getFeatureCollection();
    if (log.isDebugEnabled())
      log.debug("  -- DatasetHandler found FeatureCollection= " + featCollection);

    InvDatasetFeatureCollection fc = featureCollectionCache.get(featCollection);
    FeatureDatasetPoint fd = fc.getPointDataset(match.remaining);
    if (fd == null)
      throw new IllegalArgumentException("Not a Point Dataset " + fc.getName());
    return fd;
  }

  // fetch it as a NetcdfFile; this deals with possible NcML
  NetcdfFile ncfile = openNetcdfFile(req, res, reqPath);
  if (ncfile == null)
    return null;

  Formatter errlog = new Formatter();
  NetcdfDataset ncd = null;
  try {
    if (useNetcdfJavaBuilders || isLocationObjectStore(ncfile.getLocation())) {
      ncd = NetcdfDatasets.enhance(ncfile, NetcdfDataset.getDefaultEnhanceMode(), null);
    } else {
      ncd = NetcdfDataset.wrap(ncfile, NetcdfDataset.getDefaultEnhanceMode());
    }
    return (FeatureDatasetPoint) FeatureDatasetFactoryManager.wrap(FeatureType.ANY_POINT, ncd, null, errlog);

  } catch (Throwable t) {
    if (ncd == null)
      ncfile.close();
    else
      ncd.close();

    if (t instanceof IOException)
      throw (IOException) t;

    String msg = ncd == null ? "Problem wrapping NetcdfFile in NetcdfDataset; "
        : "Problem calling FeatureDatasetFactoryManager; ";
    msg += errlog.toString();
    log.error("openGridDataset(): " + msg, t);
    throw new IOException(msg + t.getMessage());
  }
}
 
Example 19
Source File: DatasetManager.java    From tds with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public SimpleGeometryFeatureDataset openSimpleGeometryDataset(HttpServletRequest req, HttpServletResponse res,
    String reqPath) throws IOException {
  // first look for a feature collection
  DataRootManager.DataRootMatch match = dataRootManager.findDataRootMatch(reqPath);
  if ((match != null) && (match.dataRoot.getFeatureCollection() != null)) {
    // see if its under resource control
    if (!resourceAuthorized(req, res, match.dataRoot.getRestrict()))
      return null;

    FeatureCollectionRef featCollection = match.dataRoot.getFeatureCollection();
    if (log.isDebugEnabled())
      log.debug("  -- DatasetHandler found FeatureCollection= " + featCollection);

    InvDatasetFeatureCollection fc = featureCollectionCache.get(featCollection);
    SimpleGeometryFeatureDataset fd = fc.getSimpleGeometryDataset(match.remaining);
    if (fd == null)
      throw new IllegalArgumentException("Not a Simple Geometry Dataset " + fc.getName());
    return fd;
  }

  // fetch it as a NetcdfFile; this deals with possible NcML
  NetcdfFile ncfile = openNetcdfFile(req, res, reqPath);
  if (ncfile == null)
    return null;

  Formatter errlog = new Formatter();
  NetcdfDataset ncd = null;
  try {
    if (useNetcdfJavaBuilders || isLocationObjectStore(ncfile.getLocation())) {
      ncd = NetcdfDatasets.enhance(ncfile, NetcdfDataset.getDefaultEnhanceMode(), null);
    } else {
      ncd = NetcdfDataset.wrap(ncfile, NetcdfDataset.getDefaultEnhanceMode());
    }
    return (SimpleGeometryFeatureDataset) FeatureDatasetFactoryManager.wrap(FeatureType.SIMPLE_GEOMETRY, ncd, null,
        errlog);

  } catch (Throwable t) {
    if (ncd == null)
      ncfile.close();
    else
      ncd.close();

    if (t instanceof IOException)
      throw (IOException) t;

    String msg = ncd == null ? "Problem wrapping NetcdfFile in NetcdfDataset; "
        : "Problem calling FeatureDatasetFactoryManager; ";
    msg += errlog.toString();
    log.error("openSimpleGeometryDataset(): " + msg, t);
    throw new IOException(msg + t.getMessage());
  }
}
 
Example 20
Source File: FmrcDataset.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private void closeAll(Map<String, NetcdfDataset> openFiles) throws IOException {
  for (NetcdfDataset ncfile : openFiles.values())
    ncfile.close();
  openFiles.clear();
}