Java Code Examples for ucar.nc2.ft.FeatureDatasetFactoryManager#open()

The following examples show how to use ucar.nc2.ft.FeatureDatasetFactoryManager#open() . 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: CompositeStationCollection.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private PointFeatureIterator getNextIterator() throws IOException {
  if (!iter.hasNext())
    return null;
  TimedCollection.Dataset td = iter.next();
  Formatter errlog = new Formatter();
  currentDataset = (FeatureDatasetPoint) FeatureDatasetFactoryManager.open(FeatureType.STATION, td.getLocation(),
      null, errlog);
  if (currentDataset == null)
    throw new IllegalStateException("Cant open FeatureDatasetPoint " + td.getLocation());

  List<DsgFeatureCollection> fcList = currentDataset.getPointFeatureCollectionList();
  StationTimeSeriesFeatureCollection stnCollection = (StationTimeSeriesFeatureCollection) fcList.get(0);
  StationFeature s = stnCollection.findStationFeature(getName());
  if (s == null) {
    log.debug("CompositeStationFeatureIterator dataset: {} missing station {}", td.getLocation(), getName());
    // close (or just release if cache is enabled) current dataset and check for station in
    // next dataset in collection
    currentDataset.close();
    return getNextIterator();
  }

  StationTimeSeriesFeature stnFeature = stnCollection.getStationTimeSeriesFeature(s);
  if (CompositeDatasetFactory.debug)
    System.out.printf("CompositeStationFeatureIterator open dataset: %s for %s%n", td.getLocation(), s.getName());
  return stnFeature.getPointFeatureIterator();
}
 
Example 2
Source File: RadarDataInventory.java    From tds with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
  try (RadialDatasetSweep rds = (RadialDatasetSweep) FeatureDatasetFactoryManager.open(FeatureType.RADIAL,
      file.toString(), null, new Formatter())) {
    if (rds == null) {
      return FileVisitResult.CONTINUE;
    }

    EarthLocation loc = rds.getCommonOrigin();
    if (loc == null) {
      return FileVisitResult.CONTINUE;
    }

    StationList.Station added = stations.addStation(rds.getRadarID(), loc.getLatLon());
    added.setElevation(loc.getAltitude());
    added.setName(rds.getRadarName());
    return FileVisitResult.TERMINATE;
  } catch (IOException e) {
    return FileVisitResult.CONTINUE;
  }
}
 
Example 3
Source File: CompositePointCollection.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private void readMetadata() {
  // must open a prototype in order to get the data variable
  TimedCollection.Dataset td = pointCollections.getPrototype();
  if (td == null)
    throw new RuntimeException("No datasets in the collection");

  Formatter errlog = new Formatter();
  try (FeatureDatasetPoint openDataset =
      (FeatureDatasetPoint) FeatureDatasetFactoryManager.open(FeatureType.POINT, td.getLocation(), null, errlog)) {
    if (openDataset != null) {
      dataVariables = openDataset.getDataVariables();
      globalAttributes = openDataset.attributes();
    }
  } catch (IOException ioe) {
    throw new RuntimeException(ioe);
  }
}
 
Example 4
Source File: CompositePointCollection.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private PointFeatureIterator getNextIterator() throws IOException {
  if (!iter.hasNext())
    return null;
  TimedCollection.Dataset td = iter.next();

  Formatter errlog = new Formatter();
  currentDataset =
      (FeatureDatasetPoint) FeatureDatasetFactoryManager.open(FeatureType.POINT, td.getLocation(), null, errlog);
  if (currentDataset == null)
    throw new IllegalStateException("Cant open FeatureDatasetPoint " + td.getLocation());
  if (CompositeDatasetFactory.debug)
    System.out.printf("CompositePointFeatureIterator open dataset %s%n", td.getLocation());

  List<DsgFeatureCollection> fcList = currentDataset.getPointFeatureCollectionList();
  PointFeatureCollection pc = (PointFeatureCollection) fcList.get(0);
  return pc.getPointFeatureIterator();
}
 
Example 5
Source File: TestRadialDatasetNew.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testDates() throws IOException {
  String fullpath = TestDir.cdmUnitTestDir + filename;
  Formatter errlog = new Formatter();
  RadialDatasetSweep rds =
      (RadialDatasetSweep) FeatureDatasetFactoryManager.open(FeatureType.RADIAL, fullpath, null, errlog);

  Assert.assertEquals(start, rds.getCalendarDateStart());
  Assert.assertEquals(end, rds.getCalendarDateEnd());
}
 
Example 6
Source File: TestGridAsPointP.java    From tds with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void writeGridAsPointNetcdf() throws JDOMException, IOException {
  String endpoint = TestOnLocalServer.withHttpPath(ds + "?var=" + varName + query + "&accept=netcdf");
  File tempFile = tempFolder.newFile();
  logger.debug(" write {} to {}", endpoint, tempFile.getAbsolutePath());

  try (HTTPSession session = HTTPFactory.newSession(endpoint)) {
    HTTPMethod method = HTTPFactory.Get(session);
    int statusCode = method.execute();
    if (statusCode != 200) {
      logger.debug("statusCode = {} '{}'", statusCode, method.getResponseAsString());
      return;
    }

    IO.appendToFile(method.getResponseAsStream(), tempFile.getAbsolutePath());
  }
  logger.debug(" file length {} bytes exists={} {}", tempFile.length(), tempFile.exists(),
      tempFile.getAbsolutePath());

  // Open the result file as Station feature dataset
  Formatter errlog = new Formatter();
  try (FeatureDataset fd =
      FeatureDatasetFactoryManager.open(FeatureType.STATION, tempFile.getAbsolutePath(), null, errlog)) {
    assertNotNull(errlog.toString(), fd);
    VariableSimpleIF v = fd.getDataVariable(varName);
    assertNotNull(varName, v);
  }
}
 
Example 7
Source File: NcCollectionTypeTest.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testCreateCollection() throws Exception {
  File pointFile = new File(getClass().getResource("multiStationMultiVar.ncml").toURI());
  try (FeatureDatasetPoint fdPoint = (FeatureDatasetPoint) FeatureDatasetFactoryManager.open(FeatureType.STATION,
      pointFile.getAbsolutePath(), null, new Formatter())) {
    MarshallingUtil.marshalPointDataset(fdPoint, new ByteArrayOutputStream());
  }
}
 
Example 8
Source File: TestConventionFeatureTypes.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testFeatureDatasets() throws IOException {
  for (File f : getAllFilesInDirectoryStandardFilter(dir)) {
    logger.debug("Open FeatureDataset {}", f.getPath());
    try (FeatureDataset fd = FeatureDatasetFactoryManager.open(type, f.getPath(), null, new Formatter())) {
      Assert.assertNotNull(f.getPath(), fd);
      if (type == FeatureType.GRID)
        Assert.assertTrue(f.getPath(), fd.getFeatureType().isCoverageFeatureType());
      else if (type == FeatureType.POINT)
        Assert.assertTrue(f.getPath(), fd.getFeatureType().isPointFeatureType());
    }
  }
}
 
Example 9
Source File: TestCFRadial.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private RadialDatasetSweep arrayLatLonData() throws IOException {
  String filename = TestDir.cdmUnitTestDir
      + "conventions/cfradial/cfrad.20171127_202111.203_to_20171127_202123.085_DOW7_v275_s04_el7.00_SUR.nc";
  System.out.printf("arrayLatLonData= %s%n", filename);
  Formatter buf = new Formatter();
  return (RadialDatasetSweep) FeatureDatasetFactoryManager.open(FeatureType.RADIAL, filename, null, buf);
}
 
Example 10
Source File: TestFeatureDatasetFactory.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testOpen() throws IOException, InvalidRangeException {
  System.out.printf("FeatureDatasetFactoryManager.open %s%n", ds);
  Formatter errlog = new Formatter();
  try (FeatureDataset fd = FeatureDatasetFactoryManager.open(what, ds, null, errlog)) {
    Assert.assertNotNull(errlog.toString() + " " + ds, fd);
    if (fd.getFeatureType().isCoverageFeatureType())
      testCoverage(ds);
  }
}
 
Example 11
Source File: CFPointWriter.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static void main(String[] args) throws Exception {
  String progName = CFPointWriter.class.getName();

  try {
    CommandLine cmdLine = new CommandLine(progName, args);

    if (cmdLine.help) {
      cmdLine.printUsage();
      return;
    }

    FeatureType wantFeatureType = FeatureType.ANY_POINT;
    String location = cmdLine.inputFile.getAbsolutePath();
    CancelTask cancel = null;
    Formatter errlog = new Formatter();

    try (FeatureDatasetPoint fdPoint =
        (FeatureDatasetPoint) FeatureDatasetFactoryManager.open(wantFeatureType, location, cancel, errlog)) {
      if (fdPoint == null) {
        System.err.println(errlog);
      } else {
        System.out.printf("CFPointWriter: reading from %s, writing to %s%n", cmdLine.inputFile, cmdLine.outputFile);
        writeFeatureCollection(fdPoint, cmdLine.outputFile.getAbsolutePath(), cmdLine.getCFPointWriterConfig());
        System.out.println("Done.");
      }
    }
  } catch (ParameterException e) {
    System.err.println(e.getMessage());
    System.err.printf("Try \"%s --help\" for more information.%n", progName);
  }
}
 
Example 12
Source File: CompositeStationCollectionFlattened.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private PointFeatureIterator getNextIterator() throws IOException {
  if (!iter.hasNext())
    return null;
  TimedCollection.Dataset td = iter.next();
  Formatter errlog = new Formatter();

  // open the next dataset
  currentDataset =
      (FeatureDatasetPoint) FeatureDatasetFactoryManager.open(FeatureType.STATION, td.getLocation(), null, errlog);
  if (currentDataset == null) {
    logger.error("FeatureDatasetFactoryManager failed to open: " + td.getLocation() + " \nerrlog = " + errlog);
    return getNextIterator();
  }

  if (CompositeDatasetFactory.debug)
    System.out.printf("CompositeStationCollectionFlattened.Iterator open new dataset: %s%n", td.getLocation());

  // it will have a StationTimeSeriesFeatureCollection
  List<DsgFeatureCollection> fcList = currentDataset.getPointFeatureCollectionList();
  StationTimeSeriesFeatureCollection stnCollection = (StationTimeSeriesFeatureCollection) fcList.get(0);

  PointFeatureCollection pc;
  if (wantStationsubset) {
    pc = stnCollection.flatten(stationsSubset, dateRange, varList);
  } else if (bbSubset == null) {
    pc = stnCollection.flatten(null, dateRange, null);
  } else {
    List<StationFeature> stations = stnCollection.getStationFeatures(bbSubset);
    List<String> names = new ArrayList<>();
    for (StationFeature s : stations)
      names.add(s.getName());

    pc = stnCollection.flatten(names, dateRange, null);
  }

  return pc.getPointFeatureIterator();
}
 
Example 13
Source File: CompositeStationCollection.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
protected StationHelper createStationHelper() throws IOException {
  TimedCollection.Dataset td = dataCollection.getPrototype();
  if (td == null)
    throw new RuntimeException("No datasets in the collection");

  Formatter errlog = new Formatter();
  try (FeatureDatasetPoint openDataset =
      (FeatureDatasetPoint) FeatureDatasetFactoryManager.open(FeatureType.STATION, td.getLocation(), null, errlog)) {
    if (openDataset == null)
      throw new IllegalStateException("Cant open FeatureDatasetPoint " + td.getLocation());

    StationHelper stationHelper = new StationHelper();

    List<DsgFeatureCollection> fcList = openDataset.getPointFeatureCollectionList();
    StationTimeSeriesCollectionImpl openCollection = (StationTimeSeriesCollectionImpl) fcList.get(0);
    List<StationFeature> stns = openCollection.getStationFeatures();

    for (StationFeature stnFeature : stns) {
      stationHelper.addStation(new CompositeStationFeature(stnFeature, timeUnit, altUnits,
          stnFeature.getFeatureData(), this.dataCollection));
    }

    dataVariables = openDataset.getDataVariables();
    globalAttributes = openDataset.attributes();

    return stationHelper;
  }
}
 
Example 14
Source File: TestFeatureDatasetCapabilitiesXML.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Test
public void doOne() throws IOException, JDOMException {
  FeatureDatasetCapabilitiesWriter capWriter;

  try (Formatter formatter = new Formatter()) {
    FeatureDatasetPoint fdp =
        (FeatureDatasetPoint) FeatureDatasetFactoryManager.open(FeatureType.ANY_POINT, location, null, formatter);
    // Calculates lat/lon bounding box and date range. If we skip this step, FeatureDatasetCapabilitiesWriter
    // will not include "TimeSpan" and "LatLonBox" in the document.
    fdp.calcBounds(formatter);

    logger.debug(formatter.toString());
    capWriter = new FeatureDatasetCapabilitiesWriter(fdp, path);
  }

  File f = tempFolder.newFile();
  try (FileOutputStream fos = new FileOutputStream(f)) {
    capWriter.getCapabilities(fos);
  }
  logger.debug("{} written", f.getPath());

  // round trip
  Document doc = capWriter.readCapabilitiesDocument(new FileInputStream(f));
  XMLOutputter fmt = new XMLOutputter(Format.getPrettyFormat());
  String xml = fmt.outputString(doc);
  logger.debug(xml);

  String altUnits = FeatureDatasetCapabilitiesWriter.getAltUnits(doc);
  if (hasAlt)
    logger.debug("altUnits={}", altUnits);
  Assert.assertEquals(hasAlt, altUnits != null);

  CalendarDateUnit cdu = FeatureDatasetCapabilitiesWriter.getTimeUnit(doc);
  Assert.assertNotNull("cdu", cdu);
  logger.debug("CalendarDateUnit= {}", cdu);
  logger.debug("Calendar= {}", cdu.getCalendar());

  CalendarDateRange cd = FeatureDatasetCapabilitiesWriter.getTimeSpan(doc); // Looks for "TimeSpan" in doc.
  Assert.assertNotNull("CalendarDateRange", cd);
  logger.debug("CalendarDateRange= {}", cd);

  LatLonRect bbox = FeatureDatasetCapabilitiesWriter.getSpatialExtent(doc); // Looks for "LatLonBox" in doc.
  Assert.assertNotNull("bbox", bbox);
  logger.debug("LatLonRect= {}", bbox);
}
 
Example 15
Source File: SortingStationPointFeatureCache.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public void addAll(File datasetFile) throws IOException {
  try (FeatureDatasetPoint fdPoint = (FeatureDatasetPoint) FeatureDatasetFactoryManager.open(FeatureType.STATION,
      datasetFile.getAbsolutePath(), null, new Formatter())) {
    addAll(fdPoint);
  }
}
 
Example 16
Source File: TestCFRadial.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private RadialDatasetSweep testData() throws IOException {
  String filename = TestDir.cdmUnitTestDir + "conventions/cfradial/cfrad.20080604_002217_000_SPOL_v36_SUR.nc";
  Formatter buf = new Formatter();
  return (RadialDatasetSweep) FeatureDatasetFactoryManager.open(FeatureType.RADIAL, filename, null, buf);
}
 
Example 17
Source File: TestCFRadial.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private RadialDatasetSweep oneDData() throws IOException {
  String filename = TestDir.cdmUnitTestDir
      + "conventions/cfradial/cfrad.20140608_220305.809_to_20140608_220710.630_KFTG_v348_Surveillance_SUR.nc";
  Formatter buf = new Formatter();
  return (RadialDatasetSweep) FeatureDatasetFactoryManager.open(FeatureType.RADIAL, filename, null, buf);
}
 
Example 18
Source File: TestCFRadial.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private RadialDatasetSweep rhiData() throws IOException {
  String filename = TestDir.cdmUnitTestDir
      + "conventions/cfradial/cfrad.20140717_003008.286_to_20140717_003049.699_SPOL_v140_rhi_sim_RHI.nc";
  Formatter buf = new Formatter();
  return (RadialDatasetSweep) FeatureDatasetFactoryManager.open(FeatureType.RADIAL, filename, null, buf);
}
 
Example 19
Source File: TestCompositeStationCollectionsWithCaches.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public void testOpenFileHandles() throws IOException {

    if (checkRafCache) {
      if (RandomAccessFile.getGlobalFileCache() != null) {
        RandomAccessFile.getGlobalFileCache().enable();
      } else {
        RandomAccessFile.enableDefaultGlobalFileCache();
      }
    } else {
      RandomAccessFile.getGlobalFileCache().clearCache(true);
      RandomAccessFile.shutdown();
      // RandomAccessFile.getGlobalFileCache().disable();
    }


    if (checkNetcdfFileCache) {
      // FeatureCollections still use NetcdfDataset, so work with that cache.
      NetcdfDataset.getNetcdfFileCache().enable();
    } else {
      NetcdfDataset.getNetcdfFileCache().clearCache(true);
      NetcdfDataset.getNetcdfFileCache().disable();
    }

    // open the collection
    String collection = ucar.nc2.ft.point.collection.CompositeDatasetFactory.SCHEME + TestDir.cdmUnitTestDir
        + "/ft/station/gempak/collection_with_missing_station_features/#yyMMdd#.sf$";
    FeatureDataset fds = FeatureDatasetFactoryManager.open(FeatureType.STATION, collection, null, null);
    assertThat(fds instanceof FeatureDatasetPoint);
    FeatureDatasetPoint fdp = (FeatureDatasetPoint) fds;

    // the collection dataset should have one feature collection, and it should
    // be a CompositeStationCollection
    assertThat(fdp.getPointFeatureCollectionList()).hasSize(1);
    DsgFeatureCollection dfc = fdp.getPointFeatureCollectionList().get(0);
    assertThat(dfc instanceof CompositeStationCollection);
    CompositeStationCollection csc = (CompositeStationCollection) dfc;

    // now it gets tricky. We have to tickle the PointFeatureIterator for each station trigger
    // the bug, but the iterator is hidden within the station collection because the collection
    // is made up of multiple files. Here, we use the StationHelper to get at the PointFeatureIterator
    // at the individual file level of the collection.
    StationHelper helper = csc.createStationHelper();
    List<Station> stations = helper.getStations();

    // Ensure the RandomAccessFile cache does not contain any locked files
    if (checkRafCache) {
      testRafCache();
    }

    if (checkNetcdfFileCache) {
      testNetcdfFileCache();
    }

    // Now, iterate over the stations. Each station cycles through one or more files of the collection,
    // and the bug is that the file isn't closed or released if it does not contain the given station.
    for (int station = 0; station < 2; station++) {
      // Get the PointFeatureIterator for the given station
      PointFeatureIterator pointFeatureIterator =
          ((StationTimeSeriesFeature) stations.get(station)).getPointFeatureIterator();
      // all we have to do is call .hasNext(), and if not, the underlying code will cycle through the
      // datasets of the collection but not close them
      pointFeatureIterator.hasNext();
      pointFeatureIterator.close();
    }

    if (checkRafCache) {
      testRafCache();
    }

    if (checkNetcdfFileCache) {
      testNetcdfFileCache();
    }
  }
 
Example 20
Source File: CompositeDatasetFactory.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public static FeatureDataset factory(String location, FeatureType wantFeatureType, MFileCollectionManager dcm,
    Formatter errlog) throws IOException {

  TimedCollection collection = new TimedCollection(dcm, errlog);
  if (collection.getDatasets().isEmpty()) {
    throw new FileNotFoundException("Collection is empty; spec=" + dcm);
  }

  DsgFeatureCollection first;
  TimedCollection.Dataset d = collection.getPrototype();
  try (FeatureDatasetPoint proto =
      (FeatureDatasetPoint) FeatureDatasetFactoryManager.open(wantFeatureType, d.getLocation(), null, errlog)) {
    if (proto == null) {
      throw new FileNotFoundException("Collection dataset is not a FeatureDatasetPoint; spec=" + dcm);
    }
    if (wantFeatureType == FeatureType.ANY_POINT)
      wantFeatureType = proto.getFeatureType();

    List<DsgFeatureCollection> fcList = proto.getPointFeatureCollectionList();
    if (fcList.isEmpty()) {
      throw new FileNotFoundException("FeatureCollectionList is empty; spec=" + dcm);
    }
    first = fcList.get(0);

    // LatLonRect bb = null;
    DsgFeatureCollection fc;
    switch (wantFeatureType) {
      case POINT:
        PointFeatureCollection firstPc = (PointFeatureCollection) first;
        CompositePointCollection pfc = new CompositePointCollection(dcm.getCollectionName(), firstPc.getTimeUnit(),
            firstPc.getAltUnits(), collection);
        // bb = pfc.getBoundingBox();
        fc = pfc;
        break;
      case STATION:
        PointFeatureCC firstNpc = (PointFeatureCC) first;
        CompositeStationCollection sfc = new CompositeStationCollection(dcm.getCollectionName(),
            firstNpc.getTimeUnit(), firstNpc.getAltUnits(), collection);
        // bb = sfc.getBoundingBox();
        fc = sfc;
        break;
      default:
        return null;
    }

    return new CompositePointDataset(location, wantFeatureType, fc, collection, null);
  }
}