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

The following examples show how to use ucar.nc2.Variable#getFullName() . 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
private Array convertToDegrees(Variable.Builder<?> vb) {
  VariableDS.Builder<?> vds = (VariableDS.Builder<?>) vb;
  Variable v = vds.orgVar;
  Array data;
  try {
    data = v.read();
    data = data.reduce();
  } catch (IOException ioe) {
    throw new RuntimeException("data read failed on " + v.getFullName() + "=" + ioe.getMessage());
  }
  IndexIterator ii = data.getIndexIterator();
  while (ii.hasNext()) {
    ii.setDoubleCurrent(Math.toDegrees(ii.getDoubleNext()));
  }
  return data;
}
 
Example 2
private TableConfig makeRaggedIndexChildTable(NetcdfDataset ds, Dimension parentDim, Dimension childDim,
    Variable ragged_parentIndex, Formatter errlog) {
  TableConfig childTable = new TableConfig(Table.Type.ParentIndex, childDim.getShortName());
  childTable.dimName = childDim.getShortName();

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

  makeStructureInfo(childTable, ds, ragged_parentIndex.getParentStructure(), childDim);
  childTable.parentIndex = ragged_parentIndex.getFullName();

  return childTable;
}
 
Example 3
protected String matchAxisTypeAndDimension(NetcdfDataset ds, AxisType type, Dimension outer) {
  Variable var = CoordSysEvaluator.findCoordByType(ds, type, axis -> {
    if ((outer == null) && (axis.getRank() == 0))
      return true;
    if ((outer != null) && (axis.getRank() == 1) && (outer.equals(axis.getDimension(0))))
      return true;

    // if axis is structure member, try pulling dimension out of parent structure
    if (axis.getParentStructure() != null) {
      Structure parent = axis.getParentStructure();
      return (outer != null) && (parent.getRank() == 1) && (outer.equals(parent.getDimension(0)));
    }
    return false;
  });
  if (var == null)
    return null;
  return var.getFullName();
}
 
Example 4
private void findNestedStructures(Structure s, TableConfig parent) {
  for (Variable v : s.getVariables()) {
    if (v instanceof Structure) { // handles Sequences too
      TableConfig nestedTable = new TableConfig(Table.Type.NestedStructure, v.getFullName());
      nestedTable.structName = v.getFullName();
      nestedTable.nestedTableName = v.getShortName();

      addTable(nestedTable);
      parent.addChild(nestedTable);

      // LOOK why not add the join(parent,child) here ?
      // nestedTable.join = new TableConfig.JoinConfig(Join.Type.NestedStructure);
      // joins.add(nestedTable.join);

      findNestedStructures((Structure) v, nestedTable); // search for nested structures
    }
  }
}
 
Example 5
/**
 * Write String data to a CHAR variable.
 *
 * @param v variable to write to
 * @param origin offset to start writing, ignore the strlen dimension.
 * @param values write this array; must be ArrayObject of String
 * @throws IOException if I/O error
 * @throws InvalidRangeException if values Array has illegal shape
 */
public void writeStringDataToChar(Variable v, int[] origin, Array values) throws IOException, InvalidRangeException {
  if (values.getElementType() != String.class)
    throw new IllegalArgumentException("values must be an ArrayObject of String ");

  if (v.getDataType() != DataType.CHAR)
    throw new IllegalArgumentException("variable " + v.getFullName() + " is not type CHAR");

  int rank = v.getRank();
  int strlen = v.getShape(rank - 1);

  // turn it into an ArrayChar
  ArrayChar cvalues = ArrayChar.makeFromStringArray((ArrayObject) values, strlen);

  int[] corigin = new int[rank];
  System.arraycopy(origin, 0, corigin, 0, rank - 1);

  write(v, corigin, cvalues);
}
 
Example 6
/**
 * Find the GradsVariable associated with the netCDF variable
 *
 * @param v2 the netCDF variable
 * @return the corresponding GradsVariable
 */
private GradsVariable findVar(Variable v2) {
  List<GradsVariable> vars = gradsDDF.getVariables();
  String varName = v2.getFullName();
  for (GradsVariable var : vars) {
    if (var.getName().equals(varName)) {
      return var;
    }
  }

  return null; // can't happen?
}
 
Example 7
void setExtraVariables(List<Variable> extra) {
  this.extra = extra;
  if (extra != null) {
    for (Variable v : extra) {
      if (v instanceof CoordinateAxis) {
        CoordinateAxis axis = (CoordinateAxis) v;
        if (axis.getAxisType() == AxisType.Height) {
          useAlt = false; // dont need another altitude variable
          altitudeCoordinateName = v.getFullName();
        }
      }
    }
  }
}
 
Example 8
private void copyAll(NetcdfFormatWriter ncwriter, Variable oldVar, Variable newVar) throws IOException {
  Array data = oldVar.read();
  try {
    if (!extended && oldVar.getDataType() == DataType.STRING) {
      data = convertDataToChar(newVar, data);
    }
    if (data.getSize() > 0) { // zero when record dimension = 0
      ncwriter.write(newVar, data);
    }

  } catch (InvalidRangeException e) {
    e.printStackTrace();
    throw new IOException(e.getMessage() + " for Variable " + oldVar.getFullName());
  }
}
 
Example 9
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 10
protected TableConfig getStationConfig(NetcdfDataset ds, EncodingInfo info, Formatter errlog) {
  if (!identifyEncodingStation(ds, info, CF.FeatureType.timeSeries, errlog))
    return null;

  // make station table
  TableConfig stnTable = makeStationTable(ds, FeatureType.STATION, info, errlog);
  if (stnTable == null)
    return null;

  Dimension obsDim = info.childDim;
  TableConfig obsTable = null;
  switch (info.encoding) {
    case single:
      obsTable = makeSingle(ds, obsDim, errlog);
      break;

    case multidim:
      obsTable = makeMultidimInner(ds, stnTable, info.childDim, info, errlog);
      if (info.time.getRank() == 1) { // join time(time)
        obsTable.addJoin(new JoinArray(info.time, JoinArray.Type.raw, 0));
        obsTable.time = info.time.getFullName();
      }
      break;

    case raggedContiguous:
      stnTable.numRecords = info.ragged_rowSize.getFullName();
      obsTable = makeRaggedContiguousChildTable(ds, info.parentDim, info.childDim, info.childStruct, errlog);
      break;

    case raggedIndex:
      obsTable = makeRaggedIndexChildTable(ds, info.parentDim, info.childDim, info.ragged_parentIndex, errlog);
      break;

    case flat:
      info.set(Encoding.flat, obsDim);
      obsTable = makeStructTable(ds, FeatureType.STATION, info, errlog);
      obsTable.parentIndex = (info.instanceId == null) ? null : info.instanceId.getFullName();
      Variable stnIdVar =
          Evaluator.findVariableWithAttributeAndDimension(ds, CF.CF_ROLE, CF.STATION_ID, obsDim, errlog);
      if (stnIdVar == null)
        stnIdVar =
            Evaluator.findVariableWithAttributeAndDimension(ds, CF.STANDARD_NAME, CF.STATION_ID, obsDim, errlog);
      obsTable.stnId = (stnIdVar == null) ? null : stnIdVar.getFullName();
      obsTable.stnDesc = Evaluator.findNameOfVariableWithAttributeValue(ds, CF.STANDARD_NAME, CF.PLATFORM_NAME);
      if (obsTable.stnDesc == null)
        obsTable.stnDesc = Evaluator.findNameOfVariableWithAttributeValue(ds, CF.STANDARD_NAME, CF.STATION_DESC);
      obsTable.stnWmoId =
          Evaluator.findNameVariableWithStandardNameAndDimension(ds, CF.STATION_WMOID, obsDim, errlog);
      obsTable.stnAlt =
          Evaluator.findNameVariableWithStandardNameAndDimension(ds, CF.SURFACE_ALTITUDE, obsDim, errlog);
      if (obsTable.stnAlt == null)
        obsTable.stnAlt =
            Evaluator.findNameVariableWithStandardNameAndDimension(ds, CF.STATION_ALTITUDE, obsDim, errlog);
      break;
  }
  if (obsTable == null)
    return null;

  stnTable.addChild(obsTable);
  return stnTable;
}
 
Example 11
private String identifyIdVariableName(NetcdfDataset ds, CF.FeatureType ftype) {
  Variable v = identifyIdVariable(ds, ftype);
  return (v == null) ? null : v.getFullName();
}
 
Example 12
private TableConfig makeStationTable(NetcdfDataset ds, FeatureType ftype, EncodingInfo info, Formatter errlog) {
  Variable lat = CoordSysEvaluator.findCoordByType(ds, AxisType.Lat);
  Variable lon = CoordSysEvaluator.findCoordByType(ds, AxisType.Lon);
  if (lat == null || lon == null) {
    errlog.format("CFpointObs: must have lat and lon coordinates%n");
    return null;
  }
  // Dimension stationDim = (info.encoding == Encoding.single) ? null : lat.getDimension(0); // assumes outer dim of
  // lat is parent dimension, single = scalar

  Table.Type stationTableType = Table.Type.Structure;
  if (info.encoding == Encoding.single)
    stationTableType = Table.Type.Top;
  if (info.encoding == Encoding.flat)
    stationTableType = Table.Type.Construct;

  Dimension stationDim = (info.encoding == Encoding.flat) ? info.childDim : info.parentDim;
  String name = (stationDim == null) ? " single" : stationDim.getShortName();
  TableConfig stnTable = new TableConfig(stationTableType, name);
  stnTable.featureType = ftype;

  // stnId
  Variable stnIdVar =
      Evaluator.findVariableWithAttributeAndDimension(ds, CF.CF_ROLE, CF.TIMESERIES_ID, stationDim, errlog);
  if (stnIdVar == null)
    stnIdVar =
        Evaluator.findVariableWithAttributeAndDimension(ds, CF.STANDARD_NAME, CF.STATION_ID, stationDim, errlog);
  if (stnIdVar == null) {
    errlog.format("CFpointObs: must have a Station id variable with %s = %s%n", CF.CF_ROLE, CF.TIMESERIES_ID);
    return null;
  }
  stnTable.stnId = stnIdVar.getFullName();
  info.instanceId = stnIdVar;

  stnTable.stnDesc = Evaluator.findNameVariableWithStandardNameAndDimension(ds, CF.PLATFORM_NAME, stationDim, errlog);
  if (stnTable.stnDesc == null)
    stnTable.stnDesc =
        Evaluator.findNameVariableWithStandardNameAndDimension(ds, CF.STATION_DESC, stationDim, errlog);
  stnTable.stnWmoId = Evaluator.findNameVariableWithStandardNameAndDimension(ds, CF.PLATFORM_ID, stationDim, errlog);
  if (stnTable.stnWmoId == null)
    stnTable.stnWmoId =
        Evaluator.findNameVariableWithStandardNameAndDimension(ds, CF.STATION_WMOID, stationDim, errlog);
  stnTable.stnAlt =
      Evaluator.findNameVariableWithStandardNameAndDimension(ds, CF.SURFACE_ALTITUDE, stationDim, errlog);
  if (stnTable.stnAlt == null)
    stnTable.stnAlt =
        Evaluator.findNameVariableWithStandardNameAndDimension(ds, CF.STATION_ALTITUDE, stationDim, errlog);
  stnTable.lat = lat.getFullName();
  stnTable.lon = lon.getFullName();

  if (info.encoding != Encoding.single && stationDim != null) {
    stnTable.dimName = stationDim.getShortName();
    makeStructureInfo(stnTable, ds, stnIdVar.getParentStructure(), stationDim);
  }

  // LOOK probably need a standard name here
  // optional alt coord - detect if its a station height or actually associated with the obs, eg for a profile
  if (stnTable.stnAlt == null) {
    Variable alt = CoordSysEvaluator.findCoordByType(ds, AxisType.Height);
    if (alt != null) {
      if ((info.encoding == Encoding.single) && alt.getRank() == 0)
        stnTable.stnAlt = alt.getFullName();

      if ((info.encoding != Encoding.single) && (lat.getRank() == alt.getRank()) && alt.getRank() > 0
          && alt.getDimension(0).equals(stationDim))
        stnTable.stnAlt = alt.getFullName();
    }
  }

  return stnTable;
}