Java Code Examples for ucar.nc2.Variable#getDimension()

The following examples show how to use ucar.nc2.Variable#getDimension() . 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: AWIPSConvention.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
protected AxisType getAxisType(NetcdfDataset ds, VariableEnhanced ve) {
  Variable v = (Variable) ve;
  String vname = v.getShortName();

  if (vname.equalsIgnoreCase("x"))
    return AxisType.GeoX;

  if (vname.equalsIgnoreCase("lon"))
    return AxisType.Lon;

  if (vname.equalsIgnoreCase("y"))
    return AxisType.GeoY;

  if (vname.equalsIgnoreCase("lat"))
    return AxisType.Lat;

  if (vname.equalsIgnoreCase("record"))
    return AxisType.Time;
  Dimension dim = v.getDimension(0);
  if ((dim != null) && dim.getShortName().equalsIgnoreCase("record"))
    return AxisType.Time;

  String unit = ve.getUnitsString();
  if (unit != null) {
    if (SimpleUnit.isCompatible("millibar", unit))
      return AxisType.Pressure;

    if (SimpleUnit.isCompatible("m", unit))
      return AxisType.Height;
  }


  return AxisType.GeoZ;
}
 
Example 2
Source File: N3iospWriter.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void writeData(Variable v2, Section section, Array values) throws java.io.IOException, InvalidRangeException {
  N3headerNew.Vinfo vinfo = (N3headerNew.Vinfo) v2.getSPobject();
  DataType dataType = v2.getDataType();

  if (v2.isUnlimited()) {
    Range firstRange = section.getRange(0);
    setNumrecs(firstRange.last() + 1);
  }

  if (v2 instanceof Structure) {
    if (!(values instanceof ArrayStructure))
      throw new IllegalArgumentException("writeData for Structure: data must be ArrayStructure");

    if (v2.getRank() == 0)
      throw new IllegalArgumentException("writeData for Structure: must have rank > 0");

    Dimension d = v2.getDimension(0);
    if (!d.isUnlimited())
      throw new IllegalArgumentException("writeData for Structure: must have unlimited dimension");

    writeRecordData((Structure) v2, section, (ArrayStructure) values);

  } else {
    Layout layout = (!v2.isUnlimited()) ? new LayoutRegular(vinfo.begin, v2.getElementSize(), v2.getShape(), section)
        : new LayoutRegularSegmented(vinfo.begin, v2.getElementSize(), header.recsize, v2.getShape(), section);
    writeData(values, layout, dataType);
  }
}
 
Example 3
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 4
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 5
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 6
Source File: TestGribCreationOptions.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testTimeUnitOption() throws Exception {
  String config = TestDir.cdmTestDataDir + "ucar/nc2/grib/collection/hrrrConus3surface.xml";
  GribCdmIndex.main(new String[] {"--featureCollection", config});

  /*
   * <featureCollection xmlns="http://www.unidata.ucar.edu/namespaces/thredds/InvCatalog/v1.0"
   * name="GSD HRRR CONUS 3km surface" featureType="GRIB2" harvest="true" path="grib/HRRR/CONUS_3km/surface">
   * 
   * <collection name="GSD_HRRR_CONUS_3km_surface"
   * spec="${cdmUnitTest}/gribCollections/hrrr/HRRR_CONUS_3km_20141010_0000.grib2"
   * timePartition="file"
   * dateFormatMark="#HRRR_CONUS_3km_surface_#yyyyMMddHHmm"
   * olderThan="5 min"/>
   * 
   * <tdm rewrite="test" rescan="0 0/15 * * * ? *"/>
   * <gribConfig>
   * <option name="timeUnit" value="1 minute" />
   * </gribConfig>
   * </featureCollection>
   */

  String dataset = TestDir.cdmUnitTestDir + "gribCollections/hrrr/DewpointTempFromGsdHrrrrConus3surface.grib2";
  try (NetcdfDataset ds = NetcdfDatasets.openDataset(dataset)) {
    Variable v = ds.findVariable("Dewpoint_temperature_height_above_ground");
    Assert.assertNotNull("Dewpoint_temperature_height_above_ground", v);
    Dimension d = v.getDimension(0);
    Assert.assertEquals(57, d.getLength());
  }

}
 
Example 7
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 8
Source File: IFPSConvention.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private void createTimeCoordinate(NetcdfDataset ds, Variable ncVar) {
  // Time coordinate is stored in the attribute validTimes
  // One caveat is that the times have two bounds an upper and a lower

  // get the times values
  Attribute timesAtt = ncVar.findAttribute("validTimes");
  if (timesAtt == null)
    return;
  Array timesArray = timesAtt.getValues();

  // get every other one LOOK this is awkward
  try {
    int n = (int) timesArray.getSize();
    List<Range> list = new ArrayList<>();
    list.add(new Range(0, n - 1, 2));
    timesArray = timesArray.section(list);
  } catch (InvalidRangeException e) {
    throw new IllegalStateException(e);
  }

  // make sure it matches the dimension
  DataType dtype = DataType.getType(timesArray);
  int nTimesAtt = (int) timesArray.getSize();

  // create a special dimension and coordinate variable
  Dimension dimTime = ncVar.getDimension(0);
  int nTimesDim = dimTime.getLength();
  if (nTimesDim != nTimesAtt) {
    parseInfo.format(" **error ntimes in attribute (%d) doesnt match dimension length (%d) for variable %s%n",
        nTimesAtt, nTimesDim, ncVar.getFullName());
    return;
  }

  // add the dimension
  String dimName = ncVar.getFullName() + "_timeCoord";
  Dimension newDim = new Dimension(dimName, nTimesDim);
  ds.addDimension(null, newDim);

  // add the coordinate variable
  String units = "seconds since 1970-1-1 00:00:00";
  String desc = "time coordinate for " + ncVar.getFullName();

  CoordinateAxis1D timeCoord = new CoordinateAxis1D(ds, null, dimName, dtype, dimName, units, desc);
  timeCoord.setCachedData(timesArray, true);
  timeCoord.addAttribute(new Attribute(_Coordinate.AxisType, AxisType.Time.toString()));
  ds.addCoordinateAxis(timeCoord);

  parseInfo.format(" added coordinate variable %s%n", dimName);

  // now make the original variable use the new dimension
  List<Dimension> dimsList = new ArrayList(ncVar.getDimensions());
  dimsList.set(0, newDim);
  ncVar.setDimensions(dimsList);

  // better to explicitly set the coordinate system
  ncVar.addAttribute(new Attribute(_Coordinate.Axes, dimName + " yCoord xCoord"));

  // fix the attributes
  Attribute att = ncVar.findAttribute("fillValue");
  if (att != null)
    ncVar.addAttribute(new Attribute(CDM.FILL_VALUE, att.getNumericValue()));
  att = ncVar.findAttribute("descriptiveName");
  if (null != att)
    ncVar.addAttribute(new Attribute(CDM.LONG_NAME, att.getStringValue()));

  // ncVar.enhance();
}
 
Example 9
Source File: CFpointObs.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Identify ragged array representations for double nests (timeSeries profile, timeSeries trajectory)
 * <p/>
 * This uses the contiguous ragged array representation for each profile (9.5.43.3), and the indexed ragged array
 * representation to organise the profiles into time series (9.3.54). The canonical use case is when writing real-time
 * data streams that contain profiles from many stations, arriving randomly, with the data for each entire profile
 * written all at once.
 *
 * @param ds in this dataset
 * @param info put info here
 * @param errlog error go here
 * @return EncodingInfo if ragged array representations is found
 */
protected boolean identifyDoubleRaggeds(NetcdfDataset ds, EncodingInfo info, Formatter errlog) {
  // the timeseries are stored as ragged index
  Evaluator.VarAtt varatt = Evaluator.findVariableWithAttribute(ds, CF.INSTANCE_DIMENSION);
  if (varatt == null)
    varatt = Evaluator.findVariableWithAttribute(ds, CF.RAGGED_PARENTINDEX);
  if (varatt == null)
    return false;

  Variable ragged_parentIndex = varatt.var;
  String instanceDimName = varatt.att.getStringValue();
  Dimension stationDim = ds.findDimension(instanceDimName);

  if (stationDim == null) {
    errlog.format(
        "CFpointObs: Indexed ragged array representation: parent_index variable has illegal value for %s = %s%n",
        CF.INSTANCE_DIMENSION, instanceDimName);
    return false;
  }

  if (ragged_parentIndex.getDataType() != DataType.INT) {
    errlog.format("CFpointObs: Indexed ragged array representation: parent_index variable must be of type integer%n");
    return false;
  }

  if (ragged_parentIndex.getRank() != 1 && info.childStruct == null) {
    errlog.format("CFpointObs: Indexed ragged array representation: parent_index variable %s must be 1D %n",
        ragged_parentIndex);
    return false;
  }
  Dimension profileDim = (info.childDim != null) ? info.childDim : ragged_parentIndex.getDimension(0);

  // onto the profiles, stored contiguously
  varatt = Evaluator.findVariableWithAttribute(ds, CF.SAMPLE_DIMENSION);
  if (varatt == null)
    varatt = Evaluator.findVariableWithAttribute(ds, CF.RAGGED_ROWSIZE);
  if (varatt == null)
    return false;

  Variable ragged_rowSize = varatt.var;
  String obsDimName = varatt.att.getStringValue();
  Dimension obsDim = ds.findDimension(obsDimName);

  if (obsDimName == null) {
    errlog.format(
        "CFpointObs: Contiguous ragged array representation: parent_index variable has illegal value for %s = %s%n",
        CF.SAMPLE_DIMENSION, obsDimName);
    return false;
  }

  if (!obsDimName.equals(info.grandChildDim.getShortName())) {
    errlog.format(
        "CFpointObs: Contiguous ragged array representation: row_size variable has obs dimension %s must be %s%n",
        obsDimName, info.childDim);
    return false;
  }

  if (ragged_rowSize.getDataType() != DataType.INT) {
    errlog.format("CFpointObs: Contiguous ragged array representation: row_size variable must be of type integer%n");
    return false;
  }

  if (info.childDim == null) { // nc4 ext
    Dimension profileDim2 = ragged_rowSize.getDimension(0);
    if (profileDim2 != profileDim) {
      errlog.format("CFpointObs: Double ragged array representation dimensions do not agree: %s != %s%n",
          profileDim2.getShortName(), profileDim.getShortName());
      return false;
    }
  }

  info.set(Encoding.raggedIndex, stationDim, profileDim, obsDim);
  info.ragged_parentIndex = ragged_parentIndex;
  info.ragged_rowSize = ragged_rowSize;
  return true;
}
 
Example 10
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 11
Source File: TestGribEnsembles.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Test
public void testWMOgrib2() throws Exception {

  String filename = TestDir.cdmUnitTestDir + "ft/grid/ensemble/jitka/MOEASURGEENS20100709060002.grib";
  System.out.printf("Open %s%n", filename);
  try (NetcdfFile datafile = NetcdfFiles.open(filename)) {
    NetcdfDataset netcdfDataset = new NetcdfDataset(datafile);
    GridDataset gridDataset = new GridDataset(netcdfDataset);

    String variableName = "VAR_10-3-192_L1";

    GridDatatype gridDatatype = gridDataset.findGridDatatypeByAttribute(Grib.VARIABLE_ID_ATTNAME, variableName);
    assertNotNull(gridDatatype);

    Dimension rtDimension = gridDatatype.getRunTimeDimension();
    Dimension ensDimension = gridDatatype.getEnsembleDimension();
    Dimension timeDimension = gridDatatype.getTimeDimension();
    Dimension xDimension = gridDatatype.getXDimension();
    Dimension yDimension = gridDatatype.getYDimension();
    Dimension zDimension = gridDatatype.getZDimension();

    assertNull(rtDimension);
    assertNotNull(ensDimension);
    assertNotNull(timeDimension);
    assertNotNull(xDimension);
    assertNotNull(yDimension);
    assertNull(zDimension);

    // test ordering
    int rtIndex = gridDatatype.getRunTimeDimensionIndex();
    int ensIndex = gridDatatype.getEnsembleDimensionIndex();
    int timeIndex = gridDatatype.getTimeDimensionIndex();
    int yIndex = gridDatatype.getYDimensionIndex();
    int xIndex = gridDatatype.getXDimensionIndex();
    int zIndex = gridDatatype.getZDimensionIndex();

    assertEquals(-1, rtIndex);
    assertEquals(0, ensIndex);
    assertEquals(1, timeIndex);
    assertEquals(2, yIndex);
    assertEquals(3, xIndex);
    assertEquals(-1, zIndex);

    Dimension ensDim = gridDatatype.getDimension(ensIndex); // ensIndex = 0
    assertEquals(1, ensDim.getLength());
    assertEquals("ens", ensDim.getShortName());

    Variable variable = gridDatatype.getVariable().getOriginalVariable();
    ensDim = variable.getDimension(ensIndex); // ensIndex = 0
  }

}