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

The following examples show how to use ucar.nc2.dataset.NetcdfDataset#getVariables() . 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: FslWindProfiler.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public void augmentDataset(NetcdfDataset ds, CancelTask cancelTask) {
  for (Variable v : ds.getVariables()) {
    switch (v.getShortName()) {
      case "staName":
        v.addAttribute(new Attribute("standard_name", "station_name"));
        break;
      case "staLat":
        v.addAttribute(new Attribute(_Coordinate.AxisType, AxisType.Lat.toString()));
        break;
      case "staLon":
        v.addAttribute(new Attribute(_Coordinate.AxisType, AxisType.Lon.toString()));
        break;
      case "staElev":
      case "levels":
        v.addAttribute(new Attribute(_Coordinate.AxisType, AxisType.Height.toString()));
        break;
      case "timeObs":
        v.addAttribute(new Attribute(_Coordinate.AxisType, AxisType.Time.toString()));
        break;
    }
  }
  ds.finish();
}
 
Example 2
Source File: UnidataObsConvention.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private Variable hasUnits(NetcdfDataset ds, String unitList) {
  List<Variable> varList = ds.getVariables();
  StringTokenizer stoker = new StringTokenizer(unitList, ",");
  while (stoker.hasMoreTokens()) {
    String unit = stoker.nextToken();

    for (Variable ve : varList) {
      String hasUnit = ve.getUnitsString();
      if (hasUnit == null)
        continue;
      if (hasUnit.equalsIgnoreCase(unit))
        return ve;
    }
  }
  return null;
}
 
Example 3
Source File: CFRadialAdapter.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Constructor.
 *
 * @param ds Source NetCDF dataset
 */
public CFRadialAdapter(NetcdfDataset ds) {
  this.ds = ds;
  desc = "CF/Radial radar dataset";
  init();

  for (Variable var : ds.getVariables()) {
    addRadialVariable(ds, var);
  }
}
 
Example 4
Source File: IFPSConvention.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void augmentDataset(NetcdfDataset ds, CancelTask cancelTask) throws IOException {
  if (null != ds.findVariable("xCoord"))
    return; // check if its already been done - aggregating enhanced datasets.

  parseInfo.format("IFPS augmentDataset %n");

  // Figure out projection info. Assume the same for all variables
  VariableDS lonVar = (VariableDS) ds.findVariable("longitude");
  lonVar.setUnitsString(CDM.LON_UNITS);
  lonVar.addAttribute(new Attribute(_Coordinate.AxisType, AxisType.Lon.toString()));
  VariableDS latVar = (VariableDS) ds.findVariable("latitude");
  latVar.addAttribute(new Attribute(_Coordinate.AxisType, AxisType.Lat.toString()));
  latVar.setUnitsString(CDM.LAT_UNITS);

  projVar = latVar;
  String projName = ds.findAttValueIgnoreCase(projVar, "projectionType", null);
  if ("LAMBERT_CONFORMAL".equals(projName)) {
    Projection proj = makeLCProjection(ds);
    makeXYcoords(ds, proj, latVar, lonVar);
  }

  // figure out the time coordinate for each data variable
  // LOOK : always seperate; could try to discover if they are the same
  List<Variable> vars = ds.getVariables();
  for (Variable ncvar : vars) {
    // variables that are used but not displayable or have no data have DIM_0, also don't want history, since those
    // are just how the person edited the grids
    if ((!ncvar.getDimension(0).getShortName().equals("DIM_0")) && !ncvar.getShortName().endsWith("History")
        && (ncvar.getRank() > 2) && !ncvar.getShortName().startsWith("Tool")) {
      createTimeCoordinate(ds, ncvar);
    } else if (ncvar.getShortName().equals("Topo")) {
      // Deal with Topography variable
      ncvar.addAttribute(new Attribute(CDM.LONG_NAME, "Topography"));
      ncvar.addAttribute(new Attribute(CDM.UNITS, "ft"));
    }
  }

  ds.finish();
}
 
Example 5
Source File: UnidataObsConvention.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private boolean hasAxisType(NetcdfDataset ds, AxisType a) {
  List<Variable> varList = ds.getVariables();
  for (Variable v : varList) {
    String axisType = ds.findAttValueIgnoreCase(v, "CoordinateAxisType", null);
    if ((axisType != null) && axisType.equals(a.toString()))
      return true;
  }
  return false;
}
 
Example 6
Source File: Evaluator.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Find structure variable of rank 2 with the 2 given dimensions
 * (or) Find structure variable of rank 1 with the 1 given dimension
 * 
 * @param ds in this dataset
 * @param dim0 first dimension
 * @param dim1 second dimension (ok to be null)
 * @return structure variable or null
 */
public static Structure findStructureWithDimensions(NetcdfDataset ds, Dimension dim0, Dimension dim1) {
  for (Variable v : ds.getVariables()) {
    if (!(v instanceof Structure))
      continue;

    if (dim1 != null && v.getRank() == 2 && v.getDimension(0).equals(dim0) && v.getDimension(1).equals(dim1))
      return (Structure) v;

    if (dim1 == null && v.getRank() == 1 && v.getDimension(0).equals(dim0))
      return (Structure) v;
  }
  return null;
}
 
Example 7
Source File: FslWindProfiler.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void makeMultidimInner(NetcdfDataset ds, TableConfig parentTable, TableConfig childTable, String outerDin,
    String innerDim) {
  Dimension parentDim = ds.findDimension(outerDin);
  Dimension childDim = ds.findDimension(innerDim);

  // divide up the variables between the parent and the child
  List<String> obsVars;
  List<Variable> vars = ds.getVariables();
  List<String> parentVars = new ArrayList<>(vars.size());
  obsVars = new ArrayList<>(vars.size());
  for (Variable orgV : vars) {
    if (orgV instanceof Structure)
      continue;

    Dimension dim0 = orgV.getDimension(0);
    if ((dim0 != null) && dim0.equals(parentDim)) {
      if ((orgV.getRank() == 1) || ((orgV.getRank() == 2) && orgV.getDataType() == DataType.CHAR)) {
        parentVars.add(orgV.getShortName());
      } else {
        Dimension dim1 = orgV.getDimension(1);
        if ((dim1 != null) && dim1.equals(childDim))
          obsVars.add(orgV.getShortName());
      }
    }
  }
  parentTable.vars = parentVars;
  childTable.vars = obsVars;
}
 
Example 8
Source File: FslRaob.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void makeMultidimInner(NetcdfDataset ds, TableConfig parentTable, TableConfig childTable, String outerDin,
    String innerDim) {
  Dimension parentDim = ds.findDimension(outerDin);
  Dimension childDim = ds.findDimension(innerDim);

  // divide up the variables between the parent and the child
  List<String> obsVars;
  List<Variable> vars = ds.getVariables();
  List<String> parentVars = new ArrayList<>(vars.size());
  obsVars = new ArrayList<>(vars.size());
  for (Variable orgV : vars) {
    if (orgV instanceof Structure)
      continue;

    Dimension dim0 = orgV.getDimension(0);
    if ((dim0 != null) && dim0.equals(parentDim)) {
      if ((orgV.getRank() == 1) || ((orgV.getRank() == 2) && orgV.getDataType() == DataType.CHAR)) {
        parentVars.add(orgV.getShortName());
      } else {
        Dimension dim1 = orgV.getDimension(1);
        if ((dim1 != null) && dim1.equals(childDim))
          obsVars.add(orgV.getShortName());
      }
    }
  }
  parentTable.vars = parentVars;
  childTable.vars = obsVars;
}
 
Example 9
Source File: PointConfigXML.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void makeMultidimInner(NetcdfDataset ds, TableConfig parentTable, TableConfig childTable) {
  Dimension parentDim = ds.findDimension(parentTable.dimName);
  Dimension childDim = ds.findDimension(childTable.innerName);

  // divide up the variables between the parent and the child
  List<String> obsVars;
  List<Variable> vars = ds.getVariables();
  List<String> parentVars = new ArrayList<>(vars.size());
  obsVars = new ArrayList<>(vars.size());
  for (Variable orgV : vars) {
    if (orgV instanceof Structure)
      continue;

    Dimension dim0 = orgV.getDimension(0);
    if ((dim0 != null) && dim0.equals(parentDim)) {
      if ((orgV.getRank() == 1) || ((orgV.getRank() == 2) && orgV.getDataType() == DataType.CHAR)) {
        parentVars.add(orgV.getShortName());
      } else {
        Dimension dim1 = orgV.getDimension(1);
        if ((dim1 != null) && dim1.equals(childDim))
          obsVars.add(orgV.getShortName());
      }
    }
  }
  parentTable.vars = parentVars;
  childTable.vars = obsVars;
}
 
Example 10
Source File: TestSimpleGeom.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testLine() throws IOException {

  String failMessage, found, expected;
  boolean testCond;

  String tstFile = TestDir.cdmLocalTestDataDir + "dataset/SimpleGeos/hru_soil_moist_vlen_3hru_5timestep.nc";

  // open the test file
  NetcdfDataset ncd = NetcdfDataset.openDataset(tstFile);

  // make sure this dataset used the cfConvention
  expected = cfConvention;
  found = ncd.getConventionUsed();
  testCond = found.equals(expected);
  failMessage =
      format("This dataset used the %s convention, but should have used the %s convention.", found, expected);
  Assert.assertTrue(failMessage, testCond);

  // check that attributes were filled in correctly
  List<Variable> vars = ncd.getVariables();
  for (Variable v : vars) {
    if (v.findAttribute(CF.GEOMETRY) != null) {
      Assert.assertNotNull(v.findAttribute(CF.NODE_COORDINATES));
      Assert.assertNotNull(v.findAttribute(_Coordinate.Axes));
    }
  }
  ncd.close();
}
 
Example 11
Source File: TestSimpleGeom.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testPolygon() throws IOException {

  String failMessage, found, expected;
  boolean testCond;

  String tstFile = TestDir.cdmLocalTestDataDir + "dataset/SimpleGeos/outflow_3seg_5timesteps_vlen.nc";

  // open the test file
  NetcdfDataset ncd = NetcdfDataset.openDataset(tstFile);

  // make sure this dataset used the cfConvention
  expected = cfConvention;
  found = ncd.getConventionUsed();
  testCond = found.equals(expected);
  failMessage =
      format("This dataset used the %s convention, but should have used the %s convention.", found, expected);
  Assert.assertTrue(failMessage, testCond);

  // check that attributes were filled in correctly
  List<Variable> vars = ncd.getVariables();
  for (Variable v : vars) {
    if (v.findAttribute(CF.GEOMETRY) != null) {
      Assert.assertNotNull(v.findAttribute(CF.NODE_COORDINATES));
      Assert.assertNotNull(v.findAttribute(_Coordinate.Axes));
    }
  }
  ncd.close();
}
 
Example 12
Source File: RecordDatasetHelper.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Constructor.
 *
 * @param ncfile the netccdf file
 * @param typedDataVariables list of data variables; all record variables will be added to this list, except . You
 *        can remove extra
 * @param obsTimeVName observation time variable name (required)
 * @param nomTimeVName nominal time variable name (may be null)
 * @throws IllegalArgumentException if ncfile has no unlimited dimension and recDimName is null.
 */
public RecordDatasetHelper(NetcdfDataset ncfile, String obsTimeVName, String nomTimeVName,
    List<VariableSimpleIF> typedDataVariables, String recDimName, Formatter errBuffer) {
  this.ncfile = ncfile;
  this.obsTimeVName = obsTimeVName;
  this.nomTimeVName = nomTimeVName;
  this.errs = errBuffer;

  // check if we already have a structure vs if we have to add it.

  if (this.ncfile.hasUnlimitedDimension()) {
    this.ncfile.sendIospMessage(NetcdfFile.IOSP_MESSAGE_ADD_RECORD_STRUCTURE);
    this.recordVar = (StructureDS) this.ncfile.getRootGroup().findVariableLocal("record");
    this.obsDim = ncfile.getUnlimitedDimension();

  } else {
    if (recDimName == null)
      throw new IllegalArgumentException("File <" + this.ncfile.getLocation()
          + "> has no unlimited dimension, specify psuedo record dimension with observationDimension global attribute.");
    this.obsDim = this.ncfile.getRootGroup().findDimension(recDimName);
    this.recordVar = new StructurePseudoDS(this.ncfile, null, "record", null, obsDim);
  }

  // create member variables
  List<Variable> recordMembers = ncfile.getVariables();
  for (Variable v : recordMembers) {
    if (v == recordVar)
      continue;
    if (v.isScalar())
      continue;
    if (v.getDimension(0) == this.obsDim)
      typedDataVariables.add(v);
  }

  // need the time units
  Variable timeVar = ncfile.findVariable(obsTimeVName);
  String timeUnitString = ncfile.findAttValueIgnoreCase(timeVar, CDM.UNITS, "seconds since 1970-01-01");
  Calendar cal = TimeHelper.getCalendarFromAttribute(timeVar);

  try {
    timeUnit = CalendarDateUnit.withCalendar(cal, timeUnitString);

  } catch (Exception e) {
    if (null != errs)
      errs.format("Error on string = %s == %s%n", timeUnitString, e.getMessage());
    timeUnit = CalendarDateUnit.unixDateUnit;
  }
}
 
Example 13
Source File: CFpointObs.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private TableConfig makeMultidimInner(NetcdfDataset ds, TableConfig parentTable, Dimension obsDim, EncodingInfo info,
    Formatter errlog) {
  Dimension parentDim = ds.findDimension(parentTable.dimName);

  Table.Type obsTableType =
      (parentTable.structureType == TableConfig.StructureType.PsuedoStructure) ? Table.Type.MultidimInnerPsuedo
          : Table.Type.MultidimInner;
  // if (info.time.isMemberOfStructure()) obsTableType = Table.Type.Structure;

  TableConfig obsTable = new TableConfig(obsTableType, obsDim.getShortName());

  obsTable.lat = matchAxisTypeAndDimension(ds, AxisType.Lat, parentDim, obsDim);
  obsTable.lon = matchAxisTypeAndDimension(ds, AxisType.Lon, parentDim, obsDim);
  obsTable.elev = matchAxisTypeAndDimension(ds, AxisType.Height, parentDim, obsDim);
  if (obsTable.elev == null)
    obsTable.elev = matchAxisTypeAndDimension(ds, AxisType.Pressure, parentDim, obsDim);
  if (obsTable.elev == null)
    obsTable.elev = matchAxisTypeAndDimension(ds, AxisType.GeoZ, parentDim, obsDim);
  obsTable.time = matchAxisTypeAndDimension(ds, AxisType.Time, parentDim, obsDim);

  // divide up the variables between the parent and the obs
  List<String> obsVars;
  List<Variable> vars = ds.getVariables();
  List<String> parentVars = new ArrayList<>(vars.size());
  obsVars = new ArrayList<>(vars.size());
  for (Variable orgV : vars) {
    if (orgV instanceof Structure)
      continue;

    Dimension dim0 = orgV.getDimension(0);
    if ((dim0 != null) && dim0.equals(parentDim)) {
      if ((orgV.getRank() == 1) || ((orgV.getRank() == 2) && orgV.getDataType() == DataType.CHAR)) {
        parentVars.add(orgV.getShortName());
      } else {
        Dimension dim1 = orgV.getDimension(1);
        if (obsDim.equals(dim1))
          obsVars.add(orgV.getShortName());
      }
    }
  }
  parentTable.vars = parentVars;
  // parentTable.vars = parentTable.isPsuedoStructure ? parentVars : null; // restrict to these if psuedoStruct

  obsTable.structureType = parentTable.structureType;
  obsTable.outerName = parentDim.getShortName();
  obsTable.innerName = obsDim.getShortName();
  obsTable.dimName = (parentTable.structureType == TableConfig.StructureType.PsuedoStructure) ? obsTable.outerName
      : obsTable.innerName;
  obsTable.structName = obsDim.getShortName();
  obsTable.vars = obsVars;

  return obsTable;
}
 
Example 14
Source File: CFpointObs.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private TableConfig makeMultidimInner3D(NetcdfDataset ds, TableConfig outerTable, TableConfig middleTable,
    Dimension innerDim, Formatter errlog) {
  Dimension outerDim = ds.findDimension(outerTable.dimName);
  Dimension middleDim = ds.findDimension(middleTable.innerName);

  Table.Type obsTableType =
      (outerTable.structureType == TableConfig.StructureType.PsuedoStructure) ? Table.Type.MultidimInnerPsuedo3D
          : Table.Type.MultidimInner3D;
  TableConfig obsTable = new TableConfig(obsTableType, innerDim.getShortName());
  obsTable.structureType = TableConfig.StructureType.PsuedoStructure2D;
  obsTable.dimName = outerTable.dimName;
  obsTable.outerName = middleTable.innerName;
  obsTable.innerName = innerDim.getShortName();
  obsTable.structName = innerDim.getShortName();

  obsTable.lat = matchAxisTypeAndDimension(ds, AxisType.Lat, outerDim, middleDim, innerDim);
  obsTable.lon = matchAxisTypeAndDimension(ds, AxisType.Lon, outerDim, middleDim, innerDim);
  obsTable.elev = matchAxisTypeAndDimension(ds, AxisType.Height, outerDim, middleDim, innerDim);
  if (obsTable.elev == null)
    obsTable.elev = matchAxisTypeAndDimension(ds, AxisType.Pressure, middleDim, innerDim);
  if (obsTable.elev == null)
    obsTable.elev = matchAxisTypeAndDimension(ds, AxisType.GeoZ, middleDim, innerDim);
  obsTable.time = matchAxisTypeAndDimension(ds, AxisType.Time, outerDim, middleDim, innerDim);

  // divide up the variables between the 3 tables
  List<Variable> vars = ds.getVariables();
  List<String> outerVars = new ArrayList<>(vars.size());
  List<String> middleVars = new ArrayList<>(vars.size());
  List<String> innerVars = new ArrayList<>(vars.size());
  for (Variable orgV : vars) {
    if (orgV instanceof Structure)
      continue;

    if ((orgV.getRank() == 1) || ((orgV.getRank() == 2) && orgV.getDataType() == DataType.CHAR)) {
      if (outerDim.equals(orgV.getDimension(0)))
        outerVars.add(orgV.getShortName());

    } else if (orgV.getRank() == 2) {
      if (outerDim.equals(orgV.getDimension(0)) && middleDim.equals(orgV.getDimension(1)))
        middleVars.add(orgV.getShortName());

    } else if (orgV.getRank() == 3) {
      if (outerDim.equals(orgV.getDimension(0)) && middleDim.equals(orgV.getDimension(1))
          && innerDim.equals(orgV.getDimension(2)))
        innerVars.add(orgV.getShortName());
    }
  }
  outerTable.vars = outerVars;
  middleTable.vars = middleVars;
  obsTable.vars = innerVars;

  return obsTable;
}
 
Example 15
Source File: GempakCdm.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
protected TableConfig getStationAsPointConfig(NetcdfDataset ds, Formatter errlog) {
  boolean needFinish = false;

  // find lat coord
  Variable lat = CoordSysEvaluator.findCoordByType(ds, AxisType.Lat);
  if (lat == null) {
    errlog.format("GempakCdm: Must have a Latitude coordinate");
    return null;
  }

  // find lon coord
  Variable lon = CoordSysEvaluator.findCoordByType(ds, AxisType.Lon);
  if (lon == null) {
    errlog.format("GempakCdm: Must have a Longitude coordinate");
    return null;
  }

  if (lat.getRank() != lon.getRank()) {
    errlog.format("GempakCdm: Lat and Lon coordinate must have same rank");
    return null;
  }

  // check dimensions
  boolean stnIsScalar = (lat.getRank() == 0);
  boolean stnIsSingle = (lat.getRank() == 1) && (lat.getSize() == 1);
  Dimension stationDim = null;

  if (!stnIsScalar) {
    if (lat.getDimension(0) != lon.getDimension(0)) {
      errlog.format("Lat and Lon coordinate must have same size");
      return null;
    }
    stationDim = lat.getDimension(0);
  }

  // optional alt coord
  Variable alt = CoordSysEvaluator.findCoordByType(ds, AxisType.Height);

  // obs table
  VariableDS time = CoordSysEvaluator.findCoordByType(ds, AxisType.Time);
  if (time == null) {
    errlog.format("GempakCdm: Must have a Time coordinate");
    return null;
  }
  Dimension obsDim = time.getDimension(time.getRank() - 1); // may be time(time) or time(stn, obs)

  Table.Type obsTableType = Table.Type.Structure;
  Structure multidimStruct = Evaluator.findStructureWithDimensions(ds, stationDim, obsDim);

  if (multidimStruct == null) {
    errlog.format("GempakCdm: Cannot figure out StationAsPoint table structure");
    return null;
  }

  TableConfig obs = new TableConfig(obsTableType, obsDim.getShortName());
  obs.dimName = obsDim.getShortName();
  obs.structName = multidimStruct.getFullName();
  obs.structureType = TableConfig.StructureType.Structure;
  obs.featureType = FeatureType.POINT;

  obs.lat = lat.getFullName();
  obs.lon = lon.getFullName();
  obs.time = time.getFullName();
  if (alt != null)
    obs.elev = alt.getFullName();

  List<String> vars = new ArrayList<>(30);
  for (Variable v : ds.getVariables()) {
    if ((v.getDimension(0) == stationDim)
        && ((v.getRank() == 1) || ((v.getRank() == 2) && (v.getDataType() == DataType.CHAR))))
      vars.add(v.getShortName());
  }

  StructureDS s = new StructurePseudoDS(ds, null, "stnStruct", vars, stationDim);
  obs.addJoin(new JoinMuiltdimStructure(s, obsDim.getLength()));
  obs.addJoin(new JoinArray(time, JoinArray.Type.modulo, obsDim.getLength()));

  if (needFinish)
    ds.finish();
  return obs;
}