Java Code Examples for ucar.nc2.NetcdfFile#close()

The following examples show how to use ucar.nc2.NetcdfFile#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: TestOffAggUpdating.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testUpdateLastModified() throws IOException, InvalidRangeException, InterruptedException {
  // make sure that the extra file is not in the agg
  move(extraFile);

  // open the agg
  NetcdfFile ncfile = NcMLReader.readNcML(new StringReader(ncml), location, null);
  long start = ncfile.getLastModified();

  // now make sure that the extra file is in the agg
  moveBack(extraFile);

  // reread
  long end = ncfile.getLastModified();
  assert (end > start);

  // again
  long end2 = ncfile.getLastModified();
  assert (end == end2);

  ncfile.close();
}
 
Example 2
Source File: TestAggExistingNew.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test(expected = IOException.class)
public void testNcmlDatasetNoProtocolInNcmlAbsPath() throws IOException, InvalidRangeException {
  // if using an absolute path in the NcML file location attr of the element netcdf, then
  // you must prepend file:
  // this should fail with an IOException
  String filename = "file:./" + TestNcMLRead.topDir + "exclude/aggExisting6.xml";

  NetcdfFile ncfile = NetcdfDatasets.openDataset(filename, true, null);
  logger.debug(" TestNcmlAggExisting.open {}", filename);

  testDimensions(ncfile);
  testCoordVar(ncfile);
  testAggCoordVar(ncfile);
  testReadData(ncfile);
  testReadSlice(ncfile);
  ncfile.close();
}
 
Example 3
Source File: TestOffAggNewSync.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
@Ignore("file in use - testing artifact")
public void testRemove() throws IOException, InterruptedException {
  NetcdfFile ncfile = NcMLReader.readNcML(new StringReader(aggExistingSync), "aggExistingSync", null);
  testAggCoordVar(ncfile, ntimes);
  System.out.println("");
  ncfile.close();

  String fname = dataDir + "WEST-CONUS_4km_3.9_20050912_2130.gini";
  boolean ok = TestOffAggUpdating.move(fname);
  int nfiles = ok ? ntimes - 1 : ntimes; // sometimes fails

  ncfile = NcMLReader.readNcML(new StringReader(aggExistingSync), "aggExistingSync", null);
  testAggCoordVar(ncfile, nfiles);
  ncfile.close();

  TestOffAggUpdating.moveBack(fname);
  System.out.printf("ok testRemove%n");
}
 
Example 4
Source File: TestOffAggFmrcScan2.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Ignore("Cant use Fmrc on GRIB")
@Test
public void testOpen() throws Exception {
  String dataDir = TestDir.cdmUnitTestDir + "ft/fmrc/rtmodels/";
  String ncml = "<?xml version='1.0' encoding='UTF-8'?>\n"
      + "<netcdf xmlns='http://www.unidata.ucar.edu/namespaces/netcdf/ncml-2.2'>\n"
      + "  <aggregation dimName='run' type='forecastModelRunSingleCollection' timeUnitsChange='true' >\n"
      + "    <scanFmrc location='" + dataDir + "' regExp='.*_nmm\\.GrbF[0-9]{5}$'\n"
      + "           runDateMatcher='yyMMddHH#_nmm.GrbF#'\n" + "           forecastOffsetMatcher='#_nmm.GrbF#HHH'/>\n"
      + "  </aggregation>\n" + "</netcdf>";

  String filename = "fake:TestOffAggFmrcScan2/aggFmrcScan2.xml";
  NetcdfFile ncfile = NcMLReader.readNcML(new StringReader(ncml), filename, null);
  System.out.println(" TestAggForecastModel.open " + filename);
  System.out.println("file=" + ncfile);

  TestDir.readAllData(ncfile);

  ncfile.close();
}
 
Example 5
Source File: TestAggSynthetic.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testRename() throws IOException, InvalidRangeException {
  String xml = "<?xml version='1.0' encoding='UTF-8'?>\n" // leavit
      + "<netcdf xmlns='http://www.unidata.ucar.edu/namespaces/netcdf/ncml-2.2'>\n" // leavit
      + "  <variable name='Temperature' orgName='T' />\n" // leavit
      + "  <aggregation  dimName='time' type='joinNew'>\n" // leavit
      + "    <variableAgg name='T'/>\n" // leavit
      + "    <scan location='src/test/data/ncml/nc/' suffix='Dir.nc' subdirs='false'/>\n" // leavit
      + "  </aggregation>\n" // leavit
      + "</netcdf>"; // leavit

  String filename = "file:./" + TestNcMLRead.topDir + "exclude/aggSynRename.xml";
  NetcdfFile ncfile = NcMLReaderNew.readNcML(new StringReader(xml), null, null).build();

  testDimensions(ncfile);
  testCoordVar(ncfile);
  testAggCoordVarNoCoordsDir(ncfile);
  testReadData(ncfile, "Temperature");
  testReadSlice(ncfile, "Temperature");
  ncfile.close();
}
 
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 testNcmlDatasetWcoords() throws IOException, InvalidRangeException {
  String filename = "file:./" + TestNcMLRead.topDir + "aggExistingWcoords.xml";

  NetcdfFile ncfile = NetcdfDataset.openDataset(filename, true, null);
  logger.debug(" testNcmlDatasetWcoords.open {}", filename);

  testDimensions(ncfile);
  testCoordVar(ncfile);
  testAggCoordVar(ncfile);
  testReadData(ncfile);
  testReadSlice(ncfile);
  ncfile.close();
  logger.debug(" testNcmlDatasetWcoords.closed ");
}
 
Example 7
Source File: TestAggSynthetic.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void test2() throws IOException, InvalidRangeException {
  String filename = "file:./" + TestNcMLRead.topDir + "aggSynthetic2.xml";
  NetcdfFile ncfile = NcMLReaderNew.readNcML(filename, null, null).build();

  testDimensions(ncfile);
  testCoordVar(ncfile);
  testAggCoordVar2(ncfile);
  testReadData(ncfile, "T");
  testReadSlice(ncfile, "T");

  ncfile.close();
}
 
Example 8
Source File: TestAggExistingNew.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testNcmlDatasetNoProtocolInNcmlRelPath() throws IOException, InvalidRangeException {
  String filename = "file:./" + TestNcMLRead.topDir + "aggExisting7.xml";

  NetcdfFile ncfile = NetcdfDatasets.openDataset(filename, true, null);
  logger.debug(" TestNcmlAggExisting.open {}", filename);

  testDimensions(ncfile);
  testCoordVar(ncfile);
  testAggCoordVar(ncfile);
  testReadData(ncfile);
  testReadSlice(ncfile);
  ncfile.close();
}
 
Example 9
Source File: TimeOpen.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
static void openFile(String filename, Average avg, boolean enhance) throws IOException, InvalidRangeException {
  try {
    long start = System.nanoTime();
    NetcdfFile ncfile = enhance ? NetcdfDataset.openDataset(filename) : NetcdfDataset.openFile(filename, null);
    long end = System.nanoTime();
    double took = (double) ((end - start)) / 1000 / 1000 / 1000;
    ncfile.close();
    if (avg != null)
      avg.add(took);
  } catch (Exception e) {
    System.out.println("BAD " + filename);
    e.printStackTrace();
  }
}
 
Example 10
Source File: TestOffAggDirectory.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void testNcmlDataset() throws IOException {
  String filename = "file:" + TestDir.cdmUnitTestDir + "ncml/nc/seawifs/aggDirectory.ncml";

  NetcdfFile ncfile = NetcdfDataset.openDataset(filename, true, null);
  logger.debug(" TestNcmlAggExisting.openDataset {}", filename);

  testDimensions(ncfile);
  testCoordVar(ncfile);
  testAggCoordVar(ncfile);
  testReadData2(ncfile);

  ncfile.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 testNcmlDirect() throws IOException, InvalidRangeException {
  String filename = "file:./" + TestNcMLRead.topDir + "aggExisting.xml";

  NetcdfFile ncfile = NcMLReaderNew.readNcML(filename, null, null).build();
  logger.debug(" TestNcmlAggExisting.open {}", filename);

  testDimensions(ncfile);
  testCoordVar(ncfile);
  testAggCoordVar(ncfile);
  testReadData(ncfile);
  testReadSlice(ncfile);
  ncfile.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 testNcmlDatasetNoProtocolInFilenameOrNcmlRelPath() throws IOException, InvalidRangeException {
  String filename = "./" + TestNcMLRead.topDir + "aggExisting7.xml";

  NetcdfFile ncfile = NetcdfDataset.openDataset(filename, true, null);
  logger.debug(" TestNcmlAggExisting.open {}", filename);

  testDimensions(ncfile);
  testCoordVar(ncfile);
  testAggCoordVar(ncfile);
  testReadData(ncfile);
  testReadSlice(ncfile);
  ncfile.close();
}
 
Example 13
Source File: TestAggSynthetic.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void test3() throws IOException, InvalidRangeException {
  String filename = "file:./" + TestNcMLRead.topDir + "aggSynthetic3.xml";
  NetcdfFile ncfile = NcMLReaderNew.readNcML(filename, null, null).build();

  testDimensions(ncfile);
  testCoordVar(ncfile);
  testAggCoordVar3(ncfile);
  testReadData(ncfile, "T");
  testReadSlice(ncfile, "T");

  ncfile.close();
}
 
Example 14
Source File: TestAggExisting.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testNcmlDatasetNoProtocolInFilenameOrNcmlRelPath() throws IOException, InvalidRangeException {
  String filename = "./" + TestNcMLRead.topDir + "aggExisting7.xml";

  NetcdfFile ncfile = NetcdfDatasets.openDataset(filename, true, null);
  logger.debug(" TestNcmlAggExisting.open {}", filename);

  testDimensions(ncfile);
  testCoordVar(ncfile);
  testAggCoordVar(ncfile);
  testReadData(ncfile);
  testReadSlice(ncfile);
  ncfile.close();
}
 
Example 15
Source File: TestAggExistingNew.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testNcmlDatasetNoProtocolInFilename() throws IOException, InvalidRangeException {
  String filename = "./" + TestNcMLRead.topDir + "aggExisting.xml";

  NetcdfFile ncfile = NetcdfDatasets.openDataset(filename, true, null);
  logger.debug(" TestNcmlAggExisting.open {}", filename);

  testDimensions(ncfile);
  testCoordVar(ncfile);
  testAggCoordVar(ncfile);
  testReadData(ncfile);
  testReadSlice(ncfile);
  ncfile.close();
}
 
Example 16
Source File: TestJustReadGrib.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testProblem() throws IOException {
  String filename = TestDir.cdmUnitTestDir + "formats/grib1/testproj2.grb";
  System.out.println("read file= " + filename);
  NetcdfFile ncfile = NetcdfDatasets.openFile(filename, null);
  ncfile.close();
}
 
Example 17
Source File: TestAggExisting.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testNcmlDataset() throws IOException, InvalidRangeException {
  String filename = "file:./" + TestNcMLRead.topDir + "aggExisting.xml";

  NetcdfFile ncfile = NetcdfDatasets.openDataset(filename, true, null);
  logger.debug(" TestNcmlAggExisting.open {}", filename);

  testDimensions(ncfile);
  testCoordVar(ncfile);
  testAggCoordVar(ncfile);
  testReadData(ncfile);
  testReadSlice(ncfile);
  ncfile.close();
}
 
Example 18
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 19
Source File: AggDataset.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
protected void close(NetcdfFile ncfile) throws IOException {
  if (ncfile == null)
    return;
  cacheVariables(ncfile);
  ncfile.close();
}
 
Example 20
Source File: NcMLReader.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 3 votes vote down vote up
/**
 * Read an NcML and write an equivilent NetcdfFile to a physical file, using Netcdf-3 file format.
 * The NcML may have a referenced dataset in the location URL, in which case the underlying data
 * (modified by the NcML) is written to the new file. If the NcML does not have a referenced dataset,
 * then the new file is filled with fill values, like ncgen.
 *
 * @param ncml read NcML from this input stream
 * @param fileOutName write to this local file
 * @param version kind of netcdf file
 * @param chunker optional chunking (netcdf4 only)
 */
public static void writeNcMLToFile(InputStream ncml, String fileOutName, NetcdfFileWriter.Version version,
    Nc4Chunking chunker) throws IOException {
  NetcdfDataset ncd = NcMLReader.readNcML(ncml, null);
  FileWriter2 writer = new FileWriter2(ncd, fileOutName, version, chunker);
  NetcdfFile result = writer.write();
  result.close();
  ncd.close();
}