Java Code Examples for ucar.ma2.DataType#withSignedness()

The following examples show how to use ucar.ma2.DataType#withSignedness() . 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: VariableEnhancer.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private DataType getAttributeDataType(Attribute attribute) {
  DataType dataType = attribute.getDataType();
  if (signedness == Signedness.UNSIGNED) {
    // If variable is unsigned, make its integral attributes unsigned too.
    dataType = dataType.withSignedness(signedness);
  }
  return dataType;
}
 
Example 2
Source File: NcMLReader.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Parse the values element
 *
 * @param s JDOM element to parse
 * @return Array with parsed values
 * @throws IllegalArgumentException if string values not parsable to specified data type
 */
public static ucar.ma2.Array readAttributeValues(Element s) throws IllegalArgumentException {
  String valString = s.getAttributeValue("value");

  // can also be element text
  if (valString == null) {
    valString = s.getTextNormalize();
  }

  // no value specified hmm technically this is not illegal !!
  if (valString == null)
    throw new IllegalArgumentException("No value specified");

  String type = s.getAttributeValue("type");
  DataType dtype = (type == null) ? DataType.STRING : DataType.getType(type);
  if (dtype == DataType.CHAR)
    dtype = DataType.STRING;

  // backwards compatibility with deprecated isUnsigned attribute
  String unS = s.getAttributeValue("isUnsigned");
  boolean isUnsignedSet = "true".equalsIgnoreCase(unS);
  if (isUnsignedSet && dtype.isIntegral() && !dtype.isUnsigned()) {
    dtype = dtype.withSignedness(DataType.Signedness.UNSIGNED);
  }

  String sep = s.getAttributeValue("separator");
  if ((sep == null) && (dtype == DataType.STRING)) {
    List<String> list = new ArrayList<>();
    list.add(valString);
    return Array.makeArray(dtype, list);
  }

  if (sep == null)
    sep = " "; // default whitespace separated

  List<String> stringValues = new ArrayList<>();
  StringTokenizer tokn = new StringTokenizer(valString, sep);
  while (tokn.hasMoreTokens())
    stringValues.add(tokn.nextToken());

  return Array.makeArray(dtype, stringValues);
}
 
Example 3
Source File: NcMLReaderNew.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Parse the values element
 *
 * @param s JDOM element to parse
 * @return Array with parsed values
 * @throws IllegalArgumentException if string values not parsable to specified data type
 */
private static ucar.ma2.Array readAttributeValues(Element s) throws IllegalArgumentException {
  String valString = s.getAttributeValue("value");

  // can also be element text
  if (valString == null) {
    valString = s.getTextNormalize();
  }

  // no value specified hmm technically this is not illegal !!
  if (valString == null) {
    throw new IllegalArgumentException("No value specified");
  }

  String type = s.getAttributeValue("type");
  DataType dtype = (type == null) ? DataType.STRING : DataType.getType(type);
  if (dtype == DataType.CHAR) {
    dtype = DataType.STRING;
  }

  // backwards compatibility with deprecated isUnsigned attribute
  String unS = s.getAttributeValue("isUnsigned");
  boolean isUnsignedSet = "true".equalsIgnoreCase(unS);
  if (isUnsignedSet && dtype.isIntegral() && !dtype.isUnsigned()) {
    dtype = dtype.withSignedness(DataType.Signedness.UNSIGNED);
  }

  String sep = s.getAttributeValue("separator");
  if ((sep == null) && (dtype == DataType.STRING)) {
    List<String> list = new ArrayList<>();
    list.add(valString);
    return Array.makeArray(dtype, list);
  }

  if (sep == null) {
    sep = " "; // default whitespace separated
  }

  List<String> stringValues = new ArrayList<>();
  StringTokenizer tokn = new StringTokenizer(valString, sep);
  while (tokn.hasMoreTokens()) {
    stringValues.add(tokn.nextToken());
  }

  return Array.makeArray(dtype, stringValues);
}
 
Example 4
Source File: NcMLReaderNew.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private Optional<Builder> readVariableExisting(Group.Builder groupBuilder,
    @Nullable StructureDS.Builder<?> parentStructure, DataType dtype, Variable refv, Element varElem) {
  String name = varElem.getAttributeValue("name");
  String typedefS = dtype.isEnum() ? varElem.getAttributeValue("typedef") : null;
  String nameInFile = Optional.ofNullable(varElem.getAttributeValue("orgName")).orElse(name);

  VariableDS.Builder vb;
  if (this.explicit) { // all metadata is in the ncml, do not copy
    vb = VariableDS.builder().setOriginalVariable(refv);
  } else { // modify existing
    if (parentStructure != null) {
      vb = (VariableDS.Builder<?>) parentStructure.findMemberVariable(nameInFile)
          .orElseThrow(() -> new IllegalStateException("Cant find variable " + nameInFile));
    } else {
      vb = (VariableDS.Builder) groupBuilder.findVariableLocal(nameInFile)
          .orElseThrow(() -> new IllegalStateException("Cant find variable " + nameInFile));
    }
  }
  vb.setName(name).setDataType(dtype);
  if (typedefS != null) {
    vb.setEnumTypeName(typedefS);
  }

  String dimNames = varElem.getAttributeValue("shape"); // list of dimension names
  if (dimNames != null) {
    List<Dimension> varDims = groupBuilder.makeDimensionsList(dimNames);
    vb.setDimensions(varDims); // TODO check conformable
  }

  java.util.List<Element> attList = varElem.getChildren("attribute", ncNS);
  for (Element attElem : attList) {
    readAtt(vb.getAttributeContainer(), refv, attElem);
  }

  // deal with legacy use of attribute with Unsigned = true
  Attribute att = vb.getAttributeContainer().findAttribute(CDM.UNSIGNED);
  boolean isUnsignedSet = att != null && att.getStringValue().equalsIgnoreCase("true");
  if (isUnsignedSet) {
    dtype = dtype.withSignedness(DataType.Signedness.UNSIGNED);
    vb.setDataType(dtype);
  }

  // process remove command
  java.util.List<Element> removeList = varElem.getChildren("remove", ncNS);
  for (Element remElem : removeList) {
    cmdRemove(vb, remElem.getAttributeValue("type"), remElem.getAttributeValue("name"));
  }

  Element valueElem = varElem.getChild("values", ncNS);
  if (valueElem != null) {
    readValues(vb, dtype, varElem, valueElem);
  }
  /*
   * else {
   * // see if we need to munge existing data. use case : aggregation
   * if (v.hasCachedData()) {
   * Array data;
   * try {
   * data = v.read();
   * } catch (IOException e) {
   * throw new IllegalStateException(e.getMessage());
   * }
   * if (data.getClass() != v.getDataType().getPrimitiveClassType()) {
   * Array newData = Array.factory(v.getDataType(), v.getShape());
   * MAMath.copy(newData, data);
   * v.setCachedData(newData, false);
   * }
   * }
   * }
   */

  // look for logical views
  // processLogicalViews(v, refGroup, varElem);
  // only return if it needs to be added
  return (this.explicit) ? Optional.of(vb) : Optional.empty();
}