Java Code Examples for ucar.ma2.Array#getIndexIterator()

The following examples show how to use ucar.ma2.Array#getIndexIterator() . 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: GeotiffWriter.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Replace missing values with dataMinMax.min - 1.0; return a floating point data array.
 *
 * @param grid GridDatatype
 * @param data input data array
 * @return floating point data array with missing values replaced.
 */
private ArrayFloat replaceMissingValues(IsMissingEvaluator grid, Array data, MAMath.MinMax dataMinMax) {
  float minValue = (float) (dataMinMax.min - 1.0);

  ArrayFloat floatArray = (ArrayFloat) Array.factory(DataType.FLOAT, data.getShape());
  IndexIterator dataIter = data.getIndexIterator();
  IndexIterator floatIter = floatArray.getIndexIterator();
  while (dataIter.hasNext()) {
    float v = dataIter.getFloatNext();
    if (grid.isMissing((double) v)) {
      v = minValue;
    }
    floatIter.setFloatNext(v);
  }

  return floatArray;
}
 
Example 2
Source File: GeotiffWriter.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Replace missing values with 0; scale other values between 1 and 255, return a byte data array.
 *
 * @param grid GridDatatype
 * @param data input data array
 * @return byte data array with missing values replaced and data scaled from 1- 255.
 */
private ArrayByte replaceMissingValuesAndScale(IsMissingEvaluator grid, Array data, MAMath.MinMax dataMinMax) {
  double scale = 254.0 / (dataMinMax.max - dataMinMax.min);

  ArrayByte byteArray = (ArrayByte) Array.factory(DataType.BYTE, data.getShape());
  IndexIterator dataIter = data.getIndexIterator();
  IndexIterator resultIter = byteArray.getIndexIterator();

  byte bv;
  while (dataIter.hasNext()) {
    double v = dataIter.getDoubleNext();
    if (grid.isMissing(v)) {
      bv = 0;
    } else {
      int iv = (int) ((v - dataMinMax.min) * scale + 1);
      bv = (byte) (iv & 0xff);
    }
    resultIter.setByteNext(bv);
  }

  return byteArray;
}
 
Example 3
Source File: GeotiffWriter.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private double geoShiftGetXstart(Array lon, double inc) {
  Index ilon = lon.getIndex();
  int[] lonShape = lon.getShape();
  IndexIterator lonIter = lon.getIndexIterator();
  double xlon;

  LatLonPoint p0 = LatLonPoint.create(0, lon.getFloat(ilon.set(0)));
  LatLonPoint pN = LatLonPoint.create(0, lon.getFloat(ilon.set(lonShape[0] - 1)));

  xlon = p0.getLongitude();
  while (lonIter.hasNext()) {
    float l = lonIter.getFloatNext();
    LatLonPoint pn = LatLonPoint.create(0, l);
    if (pn.getLongitude() < xlon) {
      xlon = pn.getLongitude();
    }
  }

  if (p0.getLongitude() == pN.getLongitude()) {
    xlon = xlon - inc;
  }

  return xlon;
}
 
Example 4
Source File: GradsBinaryGridServiceProvider.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Read the data for the variable
 *
 * @param v2 Variable to read
 * @param section section infomation
 * @return Array of data
 * @throws IOException problem reading from file
 * @throws InvalidRangeException invalid Range
 */
public Array readData(Variable v2, Section section) throws IOException, InvalidRangeException {

  Array dataArray = Array.factory(DataType.FLOAT, section.getShape());
  GradsVariable gradsVar = findVar(v2);
  if (gradsVar == null)
    throw new IOException();

  // Canonical ordering is ens, time, level, lat, lon
  int rangeIdx = 0;
  Range ensRange = (gradsDDF.getEnsembleDimension() != null) ? section.getRange(rangeIdx++) : new Range(0, 0);
  Range timeRange = (section.getRank() > 2) ? section.getRange(rangeIdx++) : new Range(0, 0);
  Range levRange = (gradsVar.getNumLevels() > 0) ? section.getRange(rangeIdx++) : new Range(0, 0);
  Range yRange = section.getRange(rangeIdx++);
  Range xRange = section.getRange(rangeIdx);

  IndexIterator ii = dataArray.getIndexIterator();

  for (int ensIdx : ensRange)
    for (int timeIdx : timeRange)
      for (int levelIdx : levRange)
        readXY(v2, ensIdx, timeIdx, levelIdx, yRange, xRange, ii);

  return dataArray;
}
 
Example 5
Source File: H5headerNew.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private Array findReferenceObjectNames(Array data) throws IOException {
  IndexIterator ii = data.getIndexIterator();

  Array newData = Array.factory(DataType.STRING, data.getShape());
  IndexIterator ii2 = newData.getIndexIterator();
  while (ii.hasNext()) {
    long objId = ii.getLongNext();
    DataObject dobj = getDataObject(objId, null);
    if (dobj == null) {
      log.warn("readReferenceObjectNames cant find obj= {}", objId);
    } else {
      if (debugReference) {
        log.debug(" Referenced object= {}", dobj.who);
      }
      ii2.setObjectNext(dobj.who);
    }
  }
  return newData;
}
 
Example 6
Source File: H5headerNew.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
protected Array convertEnums(Map<Integer, String> map, DataType dataType, Array values) {
  Array result = Array.factory(DataType.STRING, values.getShape());
  IndexIterator ii = result.getIndexIterator();
  values.resetLocalIterator();
  while (values.hasNext()) {
    int ival;
    if (dataType == DataType.ENUM1)
      ival = (int) DataType.unsignedByteToShort(values.nextByte());
    else if (dataType == DataType.ENUM2)
      ival = DataType.unsignedShortToInt(values.nextShort());
    else
      ival = values.nextInt();
    String sval = map.get(ival);
    if (sval == null)
      sval = "Unknown enum value=" + ival;
    ii.setObjectNext(sval);
  }
  return result;
}
 
Example 7
Source File: WRFConvention.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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 8
Source File: TestVariableDSBuilder.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testMissingData() throws IOException {
  Group.Builder parent = Group.builder().addDimension(Dimension.builder("dim1", 7).setIsUnlimited(true).build())
      .addDimension(new Dimension("dim2", 27));

  VariableDS vds = VariableDS.builder().setName("name").setDataType(DataType.FLOAT).setUnits("units").setDesc("desc")
      .setEnhanceMode(NetcdfDataset.getEnhanceAll()).addAttribute(new Attribute("missing_value", 0.0f))
      .setParentGroupBuilder(parent).setDimensionsByName("dim1").build(parent.build());

  Array data = vds.read();
  System.out.printf("data = %s%n", data);
  IndexIterator iter = data.getIndexIterator();
  while (iter.hasNext()) {
    assertThat(iter.getFloatNext()).isEqualTo(Float.NaN);
  }
}
 
Example 9
Source File: AWIPSConvention.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private CoordinateAxis makeTimeCoordAxisFromReference(NetcdfDataset ds, Variable timeVar, Array vals) {
  Variable refVar = ds.findVariable("reftime");
  if (refVar == null)
    return null;
  double refValue;
  try {
    Array refArray = refVar.read();
    refValue = refArray.getDouble(refArray.getIndex()); // get the first value
  } catch (IOException ioe) {
    return null;
  }
  if (refValue == N3iosp.NC_FILL_DOUBLE)
    return null;

  // construct the values array - make it a double to be safe
  Array dvals = Array.factory(DataType.DOUBLE, vals.getShape());
  IndexIterator diter = dvals.getIndexIterator();
  IndexIterator iiter = vals.getIndexIterator();
  while (iiter.hasNext())
    diter.setDoubleNext(iiter.getDoubleNext() + refValue); // add reftime to each of the values

  String units = ds.findAttValueIgnoreCase(refVar, CDM.UNITS, "seconds since 1970-1-1 00:00:00");
  units = normalize(units);
  String desc = "synthesized time coordinate from reftime, valtimeMINUSreftime";
  CoordinateAxis1D timeCoord = new CoordinateAxis1D(ds, null, "timeCoord", DataType.DOUBLE, "record", units, desc);

  timeCoord.setCachedData(dvals, true);

  parseInfo.format("Created Time Coordinate Axis From Reference = ");
  timeCoord.getNameAndDimensions(parseInfo, true, false);
  parseInfo.format("%n");

  return timeCoord;
}
 
Example 10
Source File: NetcdfCopier.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private Array convertDataToChar(Variable newVar, Array oldData) {
  ArrayChar newData = (ArrayChar) Array.factory(DataType.CHAR, newVar.getShape());
  Index ima = newData.getIndex();
  IndexIterator ii = oldData.getIndexIterator();
  while (ii.hasNext()) {
    String s = (String) ii.getObjectNext();
    int[] c = ii.getCurrentCounter();
    for (int i = 0; i < c.length; i++) {
      ima.setDim(i, c[i]);
    }
    newData.setString(ima, s);
  }
  return newData;
}
 
Example 11
Source File: N3headerWriter.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private long findAtt(long start_pos, String want) throws IOException {
  raf.seek(start_pos + 4);

  int natts = raf.readInt();
  for (int i = 0; i < natts; i++) {
    String name = readString();
    if (name.equals(want))
      return raf.getFilePointer();

    int type = raf.readInt();

    if (type == 2) {
      readString();
    } else {
      int nelems = raf.readInt();
      DataType dtype = getDataType(type);
      int[] shape = {nelems};
      Array arr = Array.factory(dtype, shape);
      IndexIterator ii = arr.getIndexIterator();
      int nbytes = 0;
      for (int j = 0; j < nelems; j++)
        nbytes += readAttributeValue(dtype, ii);
      skip(nbytes);
    }
  }

  throw new IllegalArgumentException("no such attribute " + want);
}
 
Example 12
Source File: ZebraConvention.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
protected void augmentDataset(CancelTask cancelTask) throws IOException {
  NcMLReaderNew.wrapNcMLresource(datasetBuilder, CoordSystemFactory.resourcesDir + "Zebra.ncml", cancelTask);

  // special time handling
  // the time coord var is created in the NcML
  // set its values = base_time + time_offset(time)
  Dimension timeDim = rootGroup.findDimension("time").orElse(null);
  VariableDS.Builder base_time = (VariableDS.Builder) rootGroup.findVariableLocal("base_time").orElse(null);
  VariableDS.Builder time_offset = (VariableDS.Builder) rootGroup.findVariableLocal("time_offset").orElse(null);
  Variable.Builder time = rootGroup.findVariableLocal("time").orElse(null);
  if ((timeDim == null) || (base_time == null) || (time_offset == null) || (time == null))
    return;

  String units =
      base_time.getAttributeContainer().findAttributeString(CDM.UNITS, "seconds since 1970-01-01 00:00 UTC");
  time.addAttribute(new Attribute(CDM.UNITS, units));

  Array data;
  try {
    double baseValue = base_time.orgVar.readScalarDouble();

    data = time_offset.orgVar.read();
    IndexIterator iter = data.getIndexIterator();
    while (iter.hasNext())
      iter.setDoubleCurrent(iter.getDoubleNext() + baseValue);

  } catch (IOException ioe) {
    parseInfo.format("ZebraConvention failed to create time Coord Axis for file %s err= %s%n",
        datasetBuilder.location, ioe);
    return;
  }

  time.setCachedData(data, true);
}
 
Example 13
Source File: AWIPSConvention.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Nullable
private CoordinateAxis.Builder makeTimeCoordAxisFromReference(Array vals) {
  if (!rootGroup.findVariableLocal("reftime").isPresent())
    return null;
  VariableDS.Builder refVar = (VariableDS.Builder) rootGroup.findVariableLocal("reftime").get();

  double refValue;
  try {
    Array refArray = refVar.orgVar.read();
    refValue = refArray.getDouble(refArray.getIndex()); // get the first value
  } catch (IOException ioe) {
    return null;
  }
  if (refValue == N3iosp.NC_FILL_DOUBLE) // why?
    return null;

  // construct the values array - make it a double to be safe
  Array dvals = Array.factory(DataType.DOUBLE, vals.getShape());
  IndexIterator diter = dvals.getIndexIterator();
  IndexIterator iiter = vals.getIndexIterator();
  while (iiter.hasNext())
    diter.setDoubleNext(iiter.getDoubleNext() + refValue); // add reftime to each of the values

  String name = "timeCoord";
  String units = refVar.getAttributeContainer().findAttributeString(CDM.UNITS, "seconds since 1970-1-1 00:00:00");
  units = normalize(units);
  String desc = "synthesized time coordinate from reftime, valtimeMINUSreftime";
  CoordinateAxis1D.Builder timeCoord =
      CoordinateAxis1D.builder().setName(name).setDataType(DataType.DOUBLE).setParentGroupBuilder(rootGroup)
          .setDimensionsByName("record").setUnits(units).setDesc(desc).setCachedData(dvals, true);

  parseInfo.format("Created Time Coordinate Axis From reftime Variable%n");
  return timeCoord;
}
 
Example 14
Source File: CDMDSP.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
protected DapAttribute buildattribute(Attribute cdmattr) throws DapException {
  DapType attrtype = CDMTypeFcns.cdmtype2daptype(cdmattr.getDataType());
  EnumTypedef cdmenum = cdmattr.getEnumType();
  boolean enumfillvalue = (cdmattr.getShortName().equals(FILLVALUE) && cdmenum != null);
  DapEnumeration dapenum = null;

  // We need to handle _FillValue specially if the
  // the variable is enum typed.
  if (enumfillvalue) {
    cdmenum = findMatchingEnum(cdmenum);
    // Make sure the cdm attribute has type enumx
    if (!cdmenum.getBaseType().isEnum())
      throw new DapException("CDM _FillValue attribute type is not enumX");
    // Modify the attr
    cdmattr.setEnumType(cdmenum);
    // Now, map to a DapEnumeration
    dapenum = (DapEnumeration) this.nodemap.get(cdmenum);
    if (dapenum == null)
      throw new DapException("Illegal CDM variable attribute type: " + cdmenum);
    attrtype = dapenum;
  }
  if (attrtype == null)
    throw new DapException("DapFile: illegal CDM variable attribute type: " + cdmattr.getDataType());
  DapAttribute dapattr = (DapAttribute) dmrfactory.newAttribute(cdmattr.getShortName(), attrtype);
  recordNode(cdmattr, dapattr);
  // Transfer the values
  Array values = cdmattr.getValues();
  if (!validatecdmtype(cdmattr.getDataType(), values.getElementType()))
    throw new DapException("Attr type versus attribute data mismatch: " + values.getElementType());
  IndexIterator iter = values.getIndexIterator();
  String[] valuelist = null;
  Object vec = CDMTypeFcns.createVector(cdmattr.getDataType(), values.getSize());
  for (int i = 0; iter.hasNext(); i++) {
    java.lang.reflect.Array.set(vec, i, iter.next());
  }
  valuelist = (String[]) Convert.convert(DapType.STRING, attrtype, vec);
  dapattr.setValues(valuelist);
  return dapattr;
}
 
Example 15
Source File: TestDorade.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private float testReadScalar(Variable v) throws IOException {
  if (show)
    System.out.printf(" read %s%n", v.getNameAndDimensions());
  assert (null != v);
  Array a = v.read();
  assert (null != a);
  IndexIterator ii = a.getIndexIterator();
  return ii.getFloatNext();
}
 
Example 16
Source File: SigmetIOServiceProvider.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Read data from a top level Variable and send data to a WritableByteChannel.
 *
 * @param v2 Variable
 * @param section wanted section of data of Variable. The section list is a list
 *        of ucar.ma2.Range which define the requested data subset.
 * @param channel WritableByteChannel object - channel that can write bytes.
 * @return the number of bytes written, possibly zero.
 */
public long readToByteChannel11(ucar.nc2.Variable v2, Section section, WritableByteChannel channel)
    throws java.io.IOException {
  Array data = readData(v2, section);
  float[] ftdata = new float[(int) data.getSize()];
  byte[] bytedata = new byte[(int) data.getSize() * 4];
  IndexIterator iter = data.getIndexIterator();
  int i = 0;
  ByteBuffer buffer = ByteBuffer.allocateDirect(bytedata.length);
  while (iter.hasNext()) {
    ftdata[i] = iter.getFloatNext();
    bytedata[i] = new Float(ftdata[i]).byteValue();
    buffer.put(bytedata[i]);
    i++;
  }
  buffer = ByteBuffer.wrap(bytedata);
  // write the bytes to the channel
  int count = channel.write(buffer);
  // check if all bytes where written
  if (buffer.hasRemaining()) {
    // if not all bytes were written, move the unwritten bytes to the beginning and
    // set position just after the last unwritten byte
    buffer.compact();
  } else {
    buffer.clear();
  }
  return (long) count;
}
 
Example 17
Source File: TestCoverageHorizSubset.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Test
@Category(NeedsCdmUnitTest.class)
public void testLongitudeSubsetWithHorizontalStride() throws IOException, InvalidRangeException {
  String filename = TestDir.cdmUnitTestDir + "tds/ncep/GFS_Global_onedeg_20100913_0000.grib2";
  String gribId = "VAR_0-3-0_L1";

  try (FeatureDatasetCoverage featureDatasetCoverage = CoverageDatasetFactory.open(filename)) {
    CoverageCollection coverageCollection = featureDatasetCoverage.findCoverageDataset(FeatureType.GRID);
    Coverage coverage = coverageCollection.findCoverageByAttribute(Grib.VARIABLE_ID_ATTNAME, gribId);

    final CalendarDate validTime = CalendarDate.parseISOformat(null, "2010-09-21T00:00:00Z");

    HorizCoordSys origHcs = coverage.getCoordSys().getHorizCoordSys();

    // Next, create the subset param and make the request
    SubsetParams params = new SubsetParams();

    // subset Time axis
    params.setTime(validTime);

    // subset across the seam
    final LatLonRect subsetLatLonRequest = new LatLonRect(LatLonPoint.create(-15, -10), 30, 20);
    params.setLatLonBoundingBox(subsetLatLonRequest);

    // set a horizontal stride
    final int stride = 2;
    params.setHorizStride(stride);

    // make subset
    GeoReferencedArray geo = coverage.readData(params);

    // Check that TimeAxis is 1D, has one coordinate, and it's equal to the time we requested
    CoverageCoordAxis timeAxis = geo.getCoordSysForData().getTimeAxis();
    assertThat(timeAxis).isInstanceOf(CoverageCoordAxis1D.class);
    CoverageCoordAxis1D timeAxis1d = (CoverageCoordAxis1D) timeAxis;
    assertThat(timeAxis1d.getNcoords()).isEqualTo(1);
    assertThat(timeAxis1d.makeDate((double) timeAxis1d.getCoordObject(0))).isEqualTo(validTime);

    // make sure the bounding box requested by subset is contained within the
    // horizontal coordinate system of the GeoReferencedArray produced by the
    // subset
    HorizCoordSys subsetHcs = geo.getCoordSysForData().getHorizCoordSys();
    assertThat(subsetLatLonRequest.containedIn(subsetHcs.calcLatLonBoundingBox())).isTrue();

    // make sure resolution of the lat and lon grids of the subset take into account the stride
    // by comparing the resolution
    CoverageCoordAxis1D origLonAxis = origHcs.getXAxis();
    CoverageCoordAxis1D origLatAxis = origHcs.getYAxis();
    CoverageCoordAxis1D subsetLonAxis = subsetHcs.getXAxis();
    CoverageCoordAxis1D subsetLatAxis = subsetHcs.getYAxis();
    final double tol = 0.001;
    assertThat(origLonAxis.getResolution()).isNotWithin(tol).of(subsetLonAxis.getResolution());
    assertThat(origLonAxis.getResolution()).isWithin(tol).of(subsetLonAxis.getResolution() / stride);
    assertThat(origLatAxis.getResolution()).isNotWithin(tol).of(subsetLatAxis.getResolution());
    assertThat(origLatAxis.getResolution()).isWithin(tol).of(subsetLatAxis.getResolution() / stride);

    // check to make sure we get data from both sides of the seam by testing that
    // half of the array isn't empty.
    // slice along longitude in the middle of the array.
    Array geoData = geo.getData();
    int middle = geoData.getShape()[1] / 2;
    Array data = geo.getData().slice(2, middle).reduce();
    // flip the array
    int numValsToSum = 3;
    Array dataFlip = data.flip(0);
    Section sec = Section.builder().appendRange(0, numValsToSum).build();
    IndexIterator dii = data.getIndexIterator();
    IndexIterator diiFlip = dataFlip.getIndexIterator();

    final double initialSumVal = 0;
    double sumData = initialSumVal;
    double sumDataFlip = initialSumVal;
    for (int i = 0; i < numValsToSum - 1; i++) {
      double val = dii.getDoubleNext();
      double valFlip = diiFlip.getDoubleNext();
      // only sum if not missing
      if (!geo.isMissing(val))
        sumData += val;
      if (!geo.isMissing(valFlip))
        sumDataFlip += valFlip;
    }
    assertThat(sumData).isNotEqualTo(initialSumVal);
    assertThat(sumDataFlip).isNotEqualTo(initialSumVal);
  }
}
 
Example 18
Source File: NetcdfCopier.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private Variable.Builder copyVariable(Group.Builder parent, Variable oldVar) throws IOException {
  Variable.Builder vb;
  DataType newType = oldVar.getDataType();
  String dimNames = Dimensions.makeDimensionsString(oldVar.getDimensions());

  if (newType == DataType.STRUCTURE) {
    Structure oldStruct = (Structure) oldVar;
    Structure.Builder sb = Structure.builder().setName(oldVar.getShortName());
    for (Variable nested : oldStruct.getVariables()) {
      sb.addMemberVariable(copyVariable(parent, nested));
    }
    vb = sb;
  } else {
    vb = Variable.builder().setName(oldVar.getShortName()).setDataType(newType);
    if (!extended && newType == DataType.STRING) {
      // find maximum length
      Array data = oldVar.read();
      IndexIterator ii = data.getIndexIterator();
      int max_len = 0;
      while (ii.hasNext()) {
        String s = (String) ii.getObjectNext();
        max_len = Math.max(max_len, s.length());
      }

      // add last dimension
      String strlenDimName = oldVar.getShortName() + "_strlen";
      parent.addDimension(Dimension.builder(strlenDimName, max_len).setIsShared(false).build());

      newType = DataType.CHAR;
      vb.setDataType(DataType.CHAR);
      dimNames += " " + strlenDimName;
    }
  }
  vb.setParentGroupBuilder(parent).setDimensionsByName(dimNames);

  if (newType.isEnum()) {
    EnumTypedef en = oldVar.getEnumTypedef();
    vb.setEnumTypeName(en.getShortName());
  }

  // attributes
  for (Attribute att : oldVar.attributes()) {
    vb.addAttribute(convertAttribute(att));
    if (debug) {
      System.out.println("add varAtt= " + att);
    }
  }

  return vb;
}
 
Example 19
Source File: WRFConvention.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Nullable
private CoordinateAxis.Builder makeZCoordAxis(String axisName, String dimName) {
  Optional<Dimension> dimOpt = rootGroup.findDimension(dimName);
  if (!dimOpt.isPresent()) {
    return null;
  }
  Dimension dim = dimOpt.get();

  String fromWhere = axisName.endsWith("stag") ? "ZNW" : "ZNU";

  CoordinateAxis.Builder v =
      CoordinateAxis1D.builder().setName(axisName).setDataType(DataType.DOUBLE).setParentGroupBuilder(rootGroup)
          .setDimensionsByName(dim.getShortName()).setUnits("").setDesc("eta values from variable " + fromWhere);
  v.addAttribute(new Attribute(CF.POSITIVE, CF.POSITIVE_DOWN)); // eta coordinate is 1.0 at bottom, 0 at top
  v.setAxisType(AxisType.GeoZ);
  v.addAttribute(new Attribute(_Coordinate.AxisType, "GeoZ"));
  if (!axisName.equals(dim.getShortName()))
    v.addAttribute(new Attribute(_Coordinate.AliasForDimension, dim.getShortName()));

  // create eta values from file variables: ZNU, ZNW
  // But they are a function of time though the values are the same in the sample file
  // NOTE: Use first time sample assuming all are the same!!
  Optional<Variable.Builder<?>> etaVarOpt = rootGroup.findVariableLocal(fromWhere);
  if (!etaVarOpt.isPresent()) {
    return makeFakeCoordAxis(axisName, dim);
  } else {
    VariableDS.Builder<?> etaVarDS = (VariableDS.Builder<?>) etaVarOpt.get();
    Variable etaVar = etaVarDS.orgVar;
    int n = etaVar.getShape(1); // number of eta levels
    int[] origin = {0, 0};
    int[] shape = {1, n};
    try {
      Array array = etaVar.read(origin, shape);// read first time slice
      ArrayDouble.D1 newArray = new ArrayDouble.D1(n);
      IndexIterator it = array.getIndexIterator();
      int count = 0;
      while (it.hasNext()) {
        double d = it.getDoubleNext();
        newArray.set(count++, d);
      }
      v.setCachedData(newArray, true);
    } catch (Exception e) {
      e.printStackTrace();
    } // ADD: error?

    return v;
  }
}
 
Example 20
Source File: VariableEnhancer.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public Array convert(Array in, boolean convertUnsigned, boolean applyScaleOffset, boolean convertMissing) {
  if (!in.getDataType().isNumeric() || (!convertUnsigned && !applyScaleOffset && !convertMissing)) {
    return in; // Nothing to do!
  }

  if (getSignedness() == Signedness.SIGNED) {
    convertUnsigned = false;
  }
  if (!hasScaleOffset()) {
    applyScaleOffset = false;
  }

  DataType outType = origDataType;
  if (convertUnsigned) {
    outType = getUnsignedConversionType();
  }
  if (applyScaleOffset) {
    outType = getScaledOffsetType();
  }

  if (outType != DataType.FLOAT && outType != DataType.DOUBLE) {
    convertMissing = false;
  }

  Array out = Array.factory(outType, in.getShape());
  IndexIterator iterIn = in.getIndexIterator();
  IndexIterator iterOut = out.getIndexIterator();

  while (iterIn.hasNext()) {
    Number value = (Number) iterIn.getObjectNext();

    if (convertUnsigned) {
      value = convertUnsigned(value);
    }
    if (applyScaleOffset) {
      value = applyScaleOffset(value);
    }
    if (convertMissing) {
      value = convertMissing(value);
    }

    iterOut.setObjectNext(value);
  }

  return out;
}