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

The following examples show how to use ucar.nc2.dataset.NetcdfDataset#acquireDataset() . 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: TrajectoryObsDatasetFactory.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public static TrajectoryObsDataset open(String netcdfFileURI, ucar.nc2.util.CancelTask cancelTask)
    throws IOException {
  DatasetUrl durl = DatasetUrl.findDatasetUrl(netcdfFileURI);
  NetcdfDataset ds = NetcdfDataset.acquireDataset(durl, true, cancelTask);
  if (RafTrajectoryObsDataset.isValidFile(ds))
    return new RafTrajectoryObsDataset(ds);
  else if (SimpleTrajectoryObsDataset.isValidFile(ds))
    return new SimpleTrajectoryObsDataset(ds);
  else if (Float10TrajectoryObsDataset.isValidFile(ds))
    return new Float10TrajectoryObsDataset(ds);
  else if (ZebraClassTrajectoryObsDataset.isValidFile(ds))
    return new ZebraClassTrajectoryObsDataset(ds);
  else if (ARMTrajectoryObsDataset.isValidFile(ds))
    return new ARMTrajectoryObsDataset(ds);
  else
    return null;

}
 
Example 2
Source File: FmrcDataset.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Open a file, keep track of open files
 *
 * @param location open this location
 * @param openFiles keep track of open files
 * @return file or null if not found
 */
private NetcdfDataset open(String location, Map<String, NetcdfDataset> openFiles) throws IOException {
  NetcdfDataset ncd;

  if (openFiles != null) {
    ncd = openFiles.get(location);
    if (ncd != null)
      return ncd;
  }

  if (config.innerNcml == null) {
    ncd = NetcdfDataset.acquireDataset(DatasetUrl.create(null, location), true, null); // default enhance

  } else {
    NetcdfFile nc = NetcdfDataset.acquireFile(DatasetUrl.create(null, location), null);
    ncd = NcMLReader.mergeNcML(nc, config.innerNcml); // create new dataset
    ncd.enhance(); // now that the ncml is added, enhance "in place", ie modify the NetcdfDataset
  }

  if (openFiles != null && ncd != null) {
    openFiles.put(location, ncd);
  }

  return ncd;
}
 
Example 3
Source File: TestAggNested.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void TestCached() throws IOException {
  try {
    NetcdfDataset.initNetcdfFileCache(10, 20, -1);

    String filename = TestDir.cdmUnitTestDir + "ncml/nestedAgg/test.ncml";
    try (NetcdfDataset ncd = NetcdfDataset.acquireDataset(DatasetUrl.findDatasetUrl(filename), true, null)) {
      Variable time = ncd.findVariable("time");
      assertThat(time.getSize()).isEqualTo(19723);
      // System.out.printf(" time array = %s%n", NCdumpW.toString(time.read()));
    }

    FileCacheIF cache = NetcdfDataset.getNetcdfFileCache();
    cache.showCache();
  } finally {
    NetcdfDataset.shutdown();
  }
}
 
Example 4
Source File: TestOffAggUpdating.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testUpdateCache() throws IOException, InvalidRangeException, InterruptedException {
  // make sure that the extra file is not in the agg
  move(extraFile);

  DatasetUrl durl = DatasetUrl.findDatasetUrl(location);

  // open the agg
  NetcdfFile ncfile = NetcdfDataset.acquireDataset(new NcmlStringFileFactory(), durl, null, -1, null, null);

  check(ncfile, 12);

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

  // reread
  ncfile.syncExtend();
  check(ncfile, 18);

  ncfile.close();
}
 
Example 5
Source File: InvDatasetFeatureCollection.java    From tds with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Get the dataset named by the path.
 * called by DatasetHandler.getNetcdfFile()
 *
 * @param matchPath remaining path from match
 * @return requested dataset
 * @throws IOException if read error
 */
public NetcdfDataset getNetcdfDataset(String matchPath) throws IOException {
  int pos = matchPath.indexOf('/');
  String type = (pos > -1) ? matchPath.substring(0, pos) : matchPath;
  String name = (pos > -1) ? matchPath.substring(pos + 1) : "";

  // this assumes that these are files. also might be remote datasets from a catalog
  if (type.equalsIgnoreCase(FILES)) {
    if (topDirectory == null)
      return null;

    String filename =
        new StringBuilder(topDirectory).append(topDirectory.endsWith("/") ? "" : "/").append(name).toString();
    DatasetUrl durl = DatasetUrl.create(null, filename);
    if (useNetcdfJavaBuilders || DatasetManager.isLocationObjectStore(durl.getTrueurl())) {
      return NetcdfDatasets.acquireDataset(null, durl, null, -1, null, null); // no enhancement
    } else {
      return NetcdfDataset.acquireDataset(null, durl, null, -1, null, null); // no enhancement
    }
  }

  GridDataset gds = getGridDataset(matchPath); // LOOK cant be right
  return (gds == null) ? null : (NetcdfDataset) gds.getNetcdfFile();
}
 
Example 6
Source File: TestAggNested.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void TestNotCached() throws IOException {
  String filename = TestDir.cdmUnitTestDir + "ncml/nestedAgg/test.ncml";

  try (NetcdfDataset ncd = NetcdfDataset.acquireDataset(DatasetUrl.findDatasetUrl(filename), true, null)) {
    Variable time = ncd.findVariable("time");
    assertThat(time.getSize()).isEqualTo(19723);
    // System.out.printf(" time array = %s%n", NCdumpW.toString(time.read()));
  }
}
 
Example 7
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 8
Source File: UnitTestCommon.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public static NetcdfDataset openDataset(String url) throws IOException {
  DatasetUrl durl = DatasetUrl.findDatasetUrl(url);
  return NetcdfDataset.acquireDataset(null, durl, ENHANCEMENT, -1, null, null);
}
 
Example 9
Source File: UnitTestCommon.java    From tds with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
static public NetcdfDataset openDatasetDap4Tests(String url) throws IOException {
  DatasetUrl durl = DatasetUrl.findDatasetUrl(url);
  return NetcdfDataset.acquireDataset(null, durl, ENHANCEMENT, -1, null, null);
}
 
Example 10
Source File: TypedDatasetFactory.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 3 votes vote down vote up
/**
 * Open a dataset as a TypedDataset.
 *
 * @param datatype open this kind of Typed Dataset; may be null, which means search all factories.
 *        If datatype is not null, only return correct TypedDataset (eg PointObsDataset for DataType.POINT).
 * @param location URL or file location of the dataset
 * @param task user may cancel
 * @param errlog place errors here, may not be null
 * @return a subclass of TypedDataset
 * @throws java.io.IOException on io error
 */
public static TypedDataset open(FeatureType datatype, String location, ucar.nc2.util.CancelTask task,
    StringBuilder errlog) throws IOException {
  DatasetUrl durl = DatasetUrl.findDatasetUrl(location);
  NetcdfDataset ncd = NetcdfDataset.acquireDataset(durl, true, task);
  return open(datatype, ncd, task, errlog);
}