Java Code Examples for ucar.nc2.Attribute#getNumericValue()

The following examples show how to use ucar.nc2.Attribute#getNumericValue() . 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: DecoderWrapper.java    From sis with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the value of the attribute of the given name as a number, or {@code null} if none.
 *
 * @param  name  the name of the attribute to search, or {@code null}.
 * @return the attribute value, or {@code null} if none or unparsable or if the given name was null.
 */
@Override
public Number numericValue(final String name) {
    if (name != null) {
        for (final Group group : groups) {
            final Attribute attribute = findAttribute(group, name);
            if (attribute != null) {
                final Number value = attribute.getNumericValue();
                if (value != null) {
                    return Utils.fixSign(value, attribute.isUnsigned());
                }
                String asString = Utils.nonEmpty(attribute.getStringValue());
                if (asString != null) {
                    return parseNumber(name, asString);
                }
            }
        }
    }
    return null;
}
 
Example 2
Source File: MetadataExtractorAcdd.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private ThreddsMetadata.Range makeRange(boolean isLon, String minName, String maxName, String resName,
    String unitsName) {
  Attribute minAtt = ncfile.get(minName);
  Attribute maxAtt = ncfile.get(maxName);
  if (minAtt == null || maxAtt == null)
    return null;

  Number minN = minAtt.getNumericValue();
  Number maxN = maxAtt.getNumericValue();
  if (minN == null || maxN == null)
    return null;

  double min = minN.doubleValue();
  double max = maxN.doubleValue();
  double size = max - min;
  if (isLon && max < min) {
    size += 360;
  }

  double res = Double.NaN;
  Attribute resAtt = ncfile.get(resName);
  if (resAtt != null) {
    Number result = resAtt.getNumericValue();
    if (result != null)
      res = result.doubleValue();
  }

  Attribute unitAtt = ncfile.get(unitsName);
  String units = (unitAtt == null) ? null : unitAtt.getStringValue();

  return new ThreddsMetadata.Range(min, size, res, units);
}
 
Example 3
Source File: ThreddsMetadataAcdd.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private ThreddsMetadata.GeospatialRange makeRange(boolean isLon, String minName, String maxName, String resName,
    String unitsName) {
  Attribute minAtt = ncfile.get(minName);
  Attribute maxAtt = ncfile.get(maxName);
  if (minAtt == null || maxAtt == null)
    return null;

  Number minN = minAtt.getNumericValue();
  Number maxN = maxAtt.getNumericValue();
  if (minN == null || maxN == null)
    return null;

  double min = minN.doubleValue();
  double max = maxN.doubleValue();
  double size = max - min;
  if (isLon && max < min) {
    size += 360;
  }

  double res = Double.NaN;
  Attribute resAtt = ncfile.get(resName);
  if (resAtt != null) {
    Number result = resAtt.getNumericValue();
    if (result != null)
      res = result.doubleValue();
  }

  Attribute unitAtt = ncfile.get(unitsName);
  String units = (unitAtt == null) ? null : unitAtt.getStringValue();

  return new ThreddsMetadata.GeospatialRange(min, size, res, units);
}
 
Example 4
Source File: H4header.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
void setFillValue(Attribute att) {
  // see IospHelper.makePrimitiveArray(int size, DataType dataType, Object fillValue)
  fillValue = (v.dataType == DataType.STRING) ? att.getStringValue() : att.getNumericValue();
}
 
Example 5
Source File: CDLWriter.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private void writeCDL(Attribute att, String parentname) {
  if (strict && (att.isString() || att.getEnumType() != null)) {
    // Force type explicitly for string.
    out.format("string "); // note lower case and trailing blank
  }
  if (strict && parentname != null) {
    out.format(NetcdfFiles.makeValidCDLName(parentname));
  }
  out.format(":");
  out.format("%s", strict ? NetcdfFiles.makeValidCDLName(att.getShortName()) : att.getShortName());
  if (att.isString()) {
    out.format(" = ");
    for (int i = 0; i < att.getLength(); i++) {
      if (i != 0) {
        out.format(", ");
      }
      String val = att.getStringValue(i);
      if (val != null) {
        out.format("\"%s\"", encodeString(val));
      }
    }
  } else if (att.getEnumType() != null) {
    out.format(" = ");
    for (int i = 0; i < att.getLength(); i++) {
      if (i != 0) {
        out.format(", ");
      }
      EnumTypedef en = att.getEnumType();
      String econst = att.getStringValue(i);
      Integer ecint = en.lookupEnumInt(econst);
      if (ecint == null) {
        throw new ForbiddenConversionException("Illegal enum constant: " + econst);
      }
      out.format("\"%s\"", encodeString(econst));
    }
  } else {
    out.format(" = ");
    for (int i = 0; i < att.getLength(); i++) {
      if (i != 0)
        out.format(", ");

      DataType dataType = att.getDataType();
      Number number = att.getNumericValue(i);
      if (dataType.isUnsigned()) {
        // 'number' is unsigned, but will be treated as signed when we print it below, because Java only has signed
        // types. If it is large enough ( >= 2^(BIT_WIDTH-1) ), its most-significant bit will be interpreted as the
        // sign bit, which will result in an invalid (negative) value being printed. To prevent that, we're going
        // to widen the number before printing it.
        number = DataType.widenNumber(number);
      }
      out.format("%s", number);

      if (dataType.isUnsigned()) {
        out.format("U");
      }

      if (dataType == DataType.FLOAT)
        out.format("f");
      else if (dataType == DataType.SHORT || dataType == DataType.USHORT) {
        out.format("S");
      } else if (dataType == DataType.BYTE || dataType == DataType.UBYTE) {
        out.format("B");
      } else if (dataType == DataType.LONG || dataType == DataType.ULONG) {
        out.format("L");
      }
    }
  }
}