Java Code Examples for ucar.nc2.ft.FeatureDatasetPoint#getPointFeatureCollectionList()

The following examples show how to use ucar.nc2.ft.FeatureDatasetPoint#getPointFeatureCollectionList() . 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: FlattenedDatasetPointCollection.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Constructs a FlattenedDatasetPointCollection.
 *
 * @param fdPoint a point dataset.
 * @throws IllegalArgumentException if any of the feature collections in the dataset are not of type
 *         {@code PointFeatureCollection} or {@code NestedPointFeatureCollection}.
 */
public FlattenedDatasetPointCollection(FeatureDatasetPoint fdPoint) throws IllegalArgumentException {
  super(fdPoint.getLocation(), CalendarDateUnit.unixDateUnit, null); // Default dateUnit and altUnits.
  this.fdPoint = fdPoint;

  List<DsgFeatureCollection> featCols = fdPoint.getPointFeatureCollectionList();

  if (!featCols.isEmpty()) {
    DsgFeatureCollection firstFeatCol = featCols.get(0);

    // Replace this.dateUnit, this.altUnits, and this.extras with "typical" values from firstFeatCol.
    // We can't be certain that those values are representative of ALL collections in the dataset, but it's
    // a decent bet because in practice, firstFeatCol is so often the ONLY collection.
    copyFieldsFrom(firstFeatCol);
  }
}
 
Example 2
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 3
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 4
Source File: CFPointWriter.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Write a FeatureDatasetPoint to a netcd3/4 file.
 *
 * @param fdpoint the FeatureDatasetPoint; do first FeatureCollection contained within.
 * @param fileOut write to the is file
 * @param config configuration
 * @return count of number of pointFeatures written.
 */
public static int writeFeatureCollection(FeatureDatasetPoint fdpoint, String fileOut, CFPointWriterConfig config)
    throws IOException {
  for (DsgFeatureCollection fc : fdpoint.getPointFeatureCollectionList()) {
    if (fc instanceof PointFeatureCollection) {
      return writePointFeatureCollection(fdpoint, (PointFeatureCollection) fc, fileOut, config);

    } else if (fc instanceof StationTimeSeriesFeatureCollection) {
      return writeStationFeatureCollection(fdpoint, (StationTimeSeriesFeatureCollection) fc, fileOut, config);

    } else if (fc instanceof ProfileFeatureCollection) {
      return writeProfileFeatureCollection(fdpoint, (ProfileFeatureCollection) fc, fileOut, config);

    } else if (fc instanceof TrajectoryFeatureCollection) {
      return writeTrajectoryFeatureCollection(fdpoint, (TrajectoryFeatureCollection) fc, fileOut, config);

    } else if (fc instanceof StationProfileFeatureCollection) {
      return writeStationProfileFeatureCollection(fdpoint, (StationProfileFeatureCollection) fc, fileOut, config);

    } else if (fc instanceof TrajectoryProfileFeatureCollection) {
      return writeTrajectoryProfileFeatureCollection(fdpoint, (TrajectoryProfileFeatureCollection) fc, fileOut,
          config);
    }
  }
  return 0;
}
 
Example 5
Source File: NcCollectionType.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public static StationTimeSeriesFeatureCollection getStationFeatures(FeatureDatasetPoint fdPoint) {
  String datasetFileName = new File(fdPoint.getNetcdfFile().getLocation()).getName();

  if (fdPoint.getFeatureType() != FeatureType.STATION) {
    throw new IllegalArgumentException(String.format("In %s, expected feature type to be STATION, not %s.",
        datasetFileName, fdPoint.getFeatureType()));
  }

  List<DsgFeatureCollection> featCollList = fdPoint.getPointFeatureCollectionList();

  if (featCollList.size() != 1) {
    throw new IllegalArgumentException(
        String.format("Expected %s to contain 1 FeatureCollection, not %s.", datasetFileName, featCollList.size()));
  } else if (!(featCollList.get(0) instanceof StationTimeSeriesFeatureCollection)) {
    String expectedClassName = StationTimeSeriesFeatureCollection.class.getName();
    String actualClassName = featCollList.get(0).getClass().getName();

    throw new IllegalArgumentException(String.format("Expected %s's FeatureCollection to be a %s, not a %s.",
        datasetFileName, expectedClassName, actualClassName));
  }

  return (StationTimeSeriesFeatureCollection) featCollList.get(0);
}
 
Example 6
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 7
Source File: CFPointObWriter.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Write a ucar.nc2.ft.PointFeatureCollection in CF point format.
 *
 * @param pfDataset find the first PointFeatureCollection, and write all data from it
 * @param fileOut write to this netcdf-3 file
 * @return number of records written
 * @throws IOException on read/write error, or if no PointFeatureCollection in pfDataset
 */
public static int writePointFeatureCollection(FeatureDatasetPoint pfDataset, String fileOut) throws IOException {
  // extract the PointFeatureCollection
  PointFeatureCollection pointFeatureCollection = null;
  List<DsgFeatureCollection> featureCollectionList = pfDataset.getPointFeatureCollectionList();
  for (DsgFeatureCollection featureCollection : featureCollectionList) {
    if (featureCollection instanceof PointFeatureCollection)
      pointFeatureCollection = (PointFeatureCollection) featureCollection;
  }
  if (null == pointFeatureCollection)
    throw new IOException("There is no PointFeatureCollection in  " + pfDataset.getLocation());

  long start = System.currentTimeMillis();

  FileOutputStream fos = new FileOutputStream(fileOut);
  DataOutputStream out = new DataOutputStream(new BufferedOutputStream(fos, 10000));
  WriterCFPointDataset writer = null;

  /*
   * LOOK BAD
   * List<VariableSimpleIF> dataVars = new ArrayList<VariableSimpleIF>();
   * ucar.nc2.NetcdfFile ncfile = pfDataset.getNetcdfFile();
   * if ((ncfile == null) || !(ncfile instanceof NetcdfDataset)) {
   * dataVars.addAll(pfDataset.getDataVariables());
   * } else {
   * NetcdfDataset ncd = (NetcdfDataset) ncfile;
   * for (VariableSimpleIF vs : pfDataset.getDataVariables()) {
   * if (ncd.findCoordinateAxis(vs.getName()) == null)
   * dataVars.add(vs);
   * }
   * }
   */

  int count = 0;
  for (PointFeature pointFeature : pointFeatureCollection) {
    StructureData data = pointFeature.getDataAll();
    if (count == 0) {
      EarthLocation loc = pointFeature.getLocation(); // LOOK we dont know this until we see the obs
      String altUnits = Double.isNaN(loc.getAltitude()) ? null : "meters"; // LOOK units may be wrong
      writer = new WriterCFPointDataset(out, pfDataset.getGlobalAttributes(), altUnits);
      writer.writeHeader(pfDataset.getDataVariables(), -1);
    }
    writer.writeRecord(pointFeature, data);
    count++;
  }

  writer.finish();
  out.flush();
  out.close();

  long took = System.currentTimeMillis() - start;
  System.out.printf("Write %d records from %s to %s took %d msecs %n", count, pfDataset.getLocation(), fileOut, took);
  return count;
}
 
Example 8
Source File: WriterCFPointDataset.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Write a ucar.nc2.ft.PointFeatureCollection in CF point format.
 *
 * @param pfDataset find the first PointFeatureCollection, and write all data from it
 * @param fileOut write to this netcdf-3 file
 * @return number of records written
 * @throws IOException on read/write error, or if no PointFeatureCollection in pfDataset
 */
public static int writePointFeatureCollection(FeatureDatasetPoint pfDataset, String fileOut) throws IOException {
  // extract the PointFeatureCollection
  PointFeatureCollection pointFeatureCollection = null;
  List<DsgFeatureCollection> featureCollectionList = pfDataset.getPointFeatureCollectionList();
  for (DsgFeatureCollection featureCollection : featureCollectionList) {
    if (featureCollection instanceof PointFeatureCollection)
      pointFeatureCollection = (PointFeatureCollection) featureCollection;
  }
  if (null == pointFeatureCollection)
    throw new IOException("There is no PointFeatureCollection in  " + pfDataset.getLocation());

  long start = System.currentTimeMillis();

  FileOutputStream fos = new FileOutputStream(fileOut);
  DataOutputStream out = new DataOutputStream(new BufferedOutputStream(fos, 10000));
  WriterCFPointDataset writer = null;

  // LOOK BAD
  List<VariableSimpleIF> dataVars = new ArrayList<VariableSimpleIF>();
  ucar.nc2.NetcdfFile ncfile = pfDataset.getNetcdfFile();
  if ((ncfile == null) || !(ncfile instanceof NetcdfDataset)) {
    dataVars.addAll(pfDataset.getDataVariables());
  } else {
    NetcdfDataset ncd = (NetcdfDataset) ncfile;
    for (VariableSimpleIF vs : pfDataset.getDataVariables()) {
      if (ncd.findCoordinateAxis(vs.getShortName()) == null)
        dataVars.add(vs);
    }
  }

  int count = 0;
  for (PointFeature pointFeature : pointFeatureCollection) {
    StructureData data = pointFeature.getDataAll();
    if (count == 0) {
      EarthLocation loc = pointFeature.getLocation(); // LOOK we dont know this until we see the obs
      String altUnits = Double.isNaN(loc.getAltitude()) ? null : "meters"; // LOOK units may be wrong
      writer = new WriterCFPointDataset(out, pfDataset.getGlobalAttributes(), altUnits);
      writer.writeHeader(dataVars, -1);
    }
    writer.writeRecord(pointFeature, data);
    count++;
  }

  writer.finish();
  out.flush();
  out.close();

  long took = System.currentTimeMillis() - start;
  System.out.printf("Write %d records from %s to %s took %d msecs %n", count, pfDataset.getLocation(), fileOut, took);
  return count;
}
 
Example 9
Source File: TestPointDatasets.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public static int checkPointFeatureDataset(FeatureDataset fdataset, boolean show) throws IOException {
  long start = System.currentTimeMillis();
  int count = 0;

  CalendarDate d1 = fdataset.getCalendarDateStart();
  CalendarDate d2 = fdataset.getCalendarDateEnd();
  if ((d1 != null) && (d2 != null))
    Assert.assertTrue("calendar date min <= max", d1.isBefore(d2) || d1.equals(d2));

  List<VariableSimpleIF> dataVars = fdataset.getDataVariables();
  Assert.assertNotNull("fdataset.getDataVariables()", dataVars);
  for (VariableSimpleIF v : dataVars) {
    Assert.assertNotNull(v.getShortName(), fdataset.getDataVariable(v.getShortName()));
  }

  // FeatureDatasetPoint
  Assert.assertTrue("fdataset instanceof FeatureDatasetPoint", fdataset instanceof FeatureDatasetPoint);
  FeatureDatasetPoint fdpoint = (FeatureDatasetPoint) fdataset;

  for (DsgFeatureCollection fc : fdpoint.getPointFeatureCollectionList()) {
    checkDsgFeatureCollection(fc);

    if (fc instanceof PointFeatureCollection) {
      PointFeatureCollection pfc = (PointFeatureCollection) fc;
      count = checkPointFeatureCollection(pfc, show);
      Assert.assertEquals("PointFeatureCollection getData count = size", count, pfc.size());

    } else if (fc instanceof StationTimeSeriesFeatureCollection) {
      count = checkStationFeatureCollection((StationTimeSeriesFeatureCollection) fc);
      // testNestedPointFeatureCollection((StationTimeSeriesFeatureCollection) fc, show);

    } else if (fc instanceof StationProfileFeatureCollection) {
      count = checkStationProfileFeatureCollection((StationProfileFeatureCollection) fc, show);
      if (showStructureData)
        showStructureData((StationProfileFeatureCollection) fc);

    } else if (fc instanceof TrajectoryProfileFeatureCollection) {
      count = checkSectionFeatureCollection((TrajectoryProfileFeatureCollection) fc, show);

    } else if (fc instanceof ProfileFeatureCollection) {
      count = checkProfileFeatureCollection((ProfileFeatureCollection) fc, show);

    } else {
      count = checkOther(fc, show);
    }
    checkInfo(fc);
  }

  long took = System.currentTimeMillis() - start;
  System.out.printf(" nobs=%d took= %d msec%n", count, took);
  return count;
}