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

The following examples show how to use ucar.nc2.ft.FeatureDatasetPoint#getDataVariables() . 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: MetadataExtractor.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public static ThreddsMetadata.Variables extractVariables(FeatureDatasetPoint fd) {
  ThreddsMetadata.Variables vars = new ThreddsMetadata.Variables("CF-1.5");
  List<VariableSimpleIF> dataVars = fd.getDataVariables();
  if (dataVars == null)
    return vars;

  for (VariableSimpleIF v : dataVars) {
    ThreddsMetadata.Variable tv = new ThreddsMetadata.Variable();
    vars.addVariable(tv);

    tv.setName(v.getShortName());
    tv.setDescription(v.getDescription());
    tv.setUnits(v.getUnitsString());

    ucar.nc2.Attribute att = v.findAttributeIgnoreCase("standard_name");
    if (att != null)
      tv.setVocabularyName(att.getStringValue());
  }
  vars.sort();
  return vars;
}
 
Example 2
Source File: CFPointWriter.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private static int writePointFeatureCollection(FeatureDatasetPoint fdpoint, PointFeatureCollection pfc,
    String fileOut, CFPointWriterConfig config) throws IOException {

  try (WriterCFPointCollection pointWriter = new WriterCFPointCollection(fileOut, fdpoint.attributes(),
      fdpoint.getDataVariables(), pfc.getTimeUnit(), pfc.getAltUnits(), config)) {

    pointWriter.setExtraVariables(pfc.getExtraVariables());

    int count = 0;
    for (PointFeature pf : pfc) {
      if (count == 0)
        pointWriter.writeHeader(pf);

      pointWriter.writeRecord(pf, pf.getFeatureData());
      count++;
      if (debug && count % 100 == 0)
        System.out.printf("%d ", count);
      if (debug && count % 1000 == 0)
        System.out.printf("%n ");
    }

    pointWriter.finish();
    return count;
  }
}
 
Example 3
Source File: ThreddsMetadataExtractor.java    From tds with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public ThreddsMetadata.VariableGroup extractVariables(FeatureDatasetPoint fd) {
  List<ThreddsMetadata.Variable> vars = new ArrayList<>();

  List<VariableSimpleIF> dataVars = fd.getDataVariables();
  if (dataVars == null)
    return null;

  for (VariableSimpleIF v : dataVars) {
    String name = v.getShortName();
    String desc = v.getDescription();
    String units = v.getUnitsString();
    String vname = null;
    String id = null;

    ucar.nc2.Attribute att = v.attributes().findAttributeIgnoreCase("standard_name");
    if (att != null)
      vname = att.getStringValue();
    vars.add(new ThreddsMetadata.Variable(name, desc, vname, units, id));
  }

  Collections.sort(vars);
  // String vocab, String vocabHref, URI vocabUri, URI mapUri, List<Variable> variables
  return new ThreddsMetadata.VariableGroup("CF-1.0", null, null, vars);
}
 
Example 4
Source File: CFPointWriter.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private static int writeStationFeatureCollection(FeatureDatasetPoint dataset, StationTimeSeriesFeatureCollection fc,
    String fileOut, CFPointWriterConfig config) throws IOException {

  try (WriterCFStationCollection cfWriter = new WriterCFStationCollection(fileOut, dataset.attributes(),
      dataset.getDataVariables(), fc.getTimeUnit(), fc.getAltUnits(), config)) {

    cfWriter.setExtraVariables(fc.getExtraVariables());

    // write all data, but no need to sort by station
    PointFeatureCollection pfc = fc.flatten(null, null, null);

    int count = 0;
    for (PointFeature pf : pfc) {
      StationPointFeature spf = (StationPointFeature) pf;
      if (count == 0)
        cfWriter.writeHeader(fc.getStationFeatures(), spf);

      cfWriter.writeRecord(spf.getStation(), pf, pf.getFeatureData());
      count++;
      if (debug && count % 100 == 0)
        System.out.printf("%d ", count);
      if (debug && count % 1000 == 0)
        System.out.printf("%n ");
    }

    cfWriter.finish();
    return count;
  }
}
 
Example 5
Source File: DsgSubsetWriter.java    From tds with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static List<VariableSimpleIF> getWantedVariables(FeatureDatasetPoint fdPoint, SubsetParams ncssParams)
    throws VariableNotContainedInDatasetException {
  List<String> vars = ncssParams.getVariables();
  if (vars.size() == 1 && vars.get(0).equals("all")) {
    return fdPoint.getDataVariables(); // Return all variables.
  }

  // restrict to these variables
  Map<String, VariableSimpleIF> dataVarsMap = new HashMap<>();
  for (VariableSimpleIF dataVar : fdPoint.getDataVariables()) {
    dataVarsMap.put(dataVar.getShortName(), dataVar);
  }

  List<String> allVarNames = new ArrayList<>(dataVarsMap.keySet());
  List<VariableSimpleIF> wantedVars = new ArrayList<>();

  for (String varName : vars) {
    if (allVarNames.contains(varName)) {
      VariableSimpleIF var = dataVarsMap.get(varName);
      wantedVars.add(var);
    } else {
      throw new VariableNotContainedInDatasetException(
          "Variable: " + varName + " is not contained in the requested dataset");
    }
  }

  return wantedVars;
}
 
Example 6
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;
}