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

The following examples show how to use ucar.ma2.DataType#isEnum() . 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: NcMLReaderNew.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private void augmentVariableNew(Variable.Builder addedFromAgg, DataType dtype, Element varElem) {
  String name = varElem.getAttributeValue("name");
  addedFromAgg.setName(name).setDataType(dtype);

  // list of dimension names
  String dimNames = varElem.getAttributeValue("shape");
  if (dimNames != null) {
    addedFromAgg.setDimensionsByName(dimNames);
  }
  Element valueElem = varElem.getChild("values", ncNS);
  if (valueElem != null) {
    readValues(addedFromAgg, dtype, varElem, valueElem);
  }

  // look for attributes
  java.util.List<Element> attList = varElem.getChildren("attribute", ncNS);
  for (Element attElem : attList) {
    readAtt(addedFromAgg.getAttributeContainer(), null, attElem);
  }

  String typedefS = dtype.isEnum() ? varElem.getAttributeValue("typedef") : null;
  if (typedefS != null) {
    addedFromAgg.setEnumTypeName(typedefS);
  }
}
 
Example 2
Source File: NcStream.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private static Variable.Builder decodeVar(NcStreamProto.Variable var) {
  DataType varType = convertDataType(var.getDataType());
  Variable.Builder ncvar = Variable.builder().setName(var.getName()).setDataType(varType);

  if (varType.isEnum()) {
    ncvar.setEnumTypeName(var.getEnumType());
  }

  // The Dimensions are stored redunantly in the Variable.
  // If shared, they must also exist in a parent Group. However, we dont yet have the Groups wired together,
  // so that has to wait until build().
  List<Dimension> dims = new ArrayList<>(6);
  Section.Builder section = Section.builder();
  for (ucar.nc2.stream.NcStreamProto.Dimension dim : var.getShapeList()) {
    dims.add(decodeDim(dim));
    section.appendRange((int) dim.getLength());
  }
  ncvar.addDimensions(dims);

  for (ucar.nc2.stream.NcStreamProto.Attribute att : var.getAttsList())
    ncvar.addAttribute(decodeAtt(att));

  if (!var.getData().isEmpty()) {
    // LOOK may mess with ability to change var size later.
    ByteBuffer bb = ByteBuffer.wrap(var.getData().toByteArray());
    Array data = Array.factory(varType, section.build().getShape(), bb);
    ncvar.setCachedData(data, true);
  }

  return ncvar;
}
 
Example 3
Source File: NcMLReaderNew.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Read a NcML variable element that does not have a reference variable
 *
 * @param groupBuilder group that the variable is part of
 * @param varElem ncml variable element
 * @return return new Variable.Builder
 */
private VariableDS.Builder readVariableNew(Group.Builder groupBuilder, DataType dtype, Element varElem) {
  String name = varElem.getAttributeValue("name");
  VariableDS.Builder v = VariableDS.builder().setName(name).setDataType(dtype).setParentGroupBuilder(groupBuilder);

  // list of dimension names
  String dimNames = varElem.getAttributeValue("shape");
  if (dimNames != null) {
    v.setDimensionsByName(dimNames);
  }
  Element valueElem = varElem.getChild("values", ncNS);
  if (valueElem != null) {
    readValues(v, dtype, varElem, valueElem);
  }

  // look for attributes
  java.util.List<Element> attList = varElem.getChildren("attribute", ncNS);
  for (Element attElem : attList) {
    readAtt(v.getAttributeContainer(), null, attElem);
  }

  String typedefS = dtype.isEnum() ? varElem.getAttributeValue("typedef") : null;
  if (typedefS != null) {
    v.setEnumTypeName(typedefS);
  }

  return v;
}
 
Example 4
Source File: H5headerNew.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private boolean makeVariableShapeAndType(Group.Builder parent, Variable.Builder v, MessageDatatype mdt,
    MessageDataspace msd, Vinfo vinfo, String dimNames) {

  int[] shape = makeVariableShape(mdt, msd, dimNames);

  // set dimensions on the variable
  if (dimNames != null) { // dimensions were passed in
    if ((mdt.type == 9) && !mdt.isVString)
      v.setDimensionsByName(dimNames + " *");
    else
      v.setDimensionsByName(dimNames);
  } else {
    v.setDimensionsAnonymous(shape);
  }

  // set the type
  DataType dt = vinfo.getNCDataType();
  if (dt == null)
    return false;
  v.setDataType(dt);

  // set the enumTypedef
  if (dt.isEnum()) {
    EnumTypedef enumTypedef = parent.findEnumTypedef(mdt.enumTypeName).orElse(null);
    if (enumTypedef == null) { // if shared object, wont have a name, shared version gets added later
      enumTypedef = new EnumTypedef(mdt.enumTypeName, mdt.map);
      parent.addEnumTypedef(enumTypedef);
    }
    v.setEnumTypeName(enumTypedef.getShortName());
  }

  return true;
}
 
Example 5
Source File: CDLWriter.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void writeCDL(Variable v, Indent indent, boolean useFullName) {
  out.format("%s", indent);
  DataType dataType = v.getDataType();
  if (dataType == null)
    out.format("Unknown");
  else if (dataType.isEnum()) {
    if (v.getEnumTypedef() == null)
      out.format("enum UNKNOWN");
    else
      out.format("enum %s", NetcdfFile.makeValidCDLName(v.getEnumTypedef().getShortName()));
  } else
    out.format("%s", dataType.toString());

  // if (isVariableLength) out.append("(*)"); // LOOK
  out.format(" ");
  v.getNameAndDimensions(out, useFullName, strict);
  out.format(";");
  out.format("%n");

  indent.incr();
  for (Attribute att : v.attributes()) {
    if (Attribute.isspecial(att))
      continue;
    out.format("%s", indent);
    writeCDL(att, v.getShortName());
    out.format(";");
    if (!strict && (att.getDataType() != DataType.STRING))
      out.format(" // %s", att.getDataType());
    out.format("%n");
  }
  indent.decr();
}
 
Example 6
Source File: NcMLWriter.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public Element makeVariableElement(Variable var, boolean showValues) {
  boolean isStructure = var instanceof Structure;

  Element varElem = new Element("variable", namespace);
  varElem.setAttribute("name", var.getShortName());

  StringBuilder buff = new StringBuilder();
  List dims = var.getDimensions();
  for (int i = 0; i < dims.size(); i++) {
    Dimension dim = (Dimension) dims.get(i);
    if (i > 0)
      buff.append(" ");
    if (dim.isShared())
      buff.append(dim.getShortName());
    else if (dim.isVariableLength())
      buff.append("*");
    else
      buff.append(dim.getLength());
  }
  // if (buff.length() > 0)
  varElem.setAttribute("shape", buff.toString());

  DataType dt = var.getDataType();
  if (dt != null) {
    varElem.setAttribute("type", dt.toString());
    if (dt.isEnum())
      varElem.setAttribute("typedef", var.getEnumTypedef().getShortName());
  }


  // attributes
  for (Attribute att : var.attributes()) {
    varElem.addContent(makeAttributeElement(att));
  }

  if (isStructure) {
    Structure s = (Structure) var;
    for (Variable variable : s.getVariables()) {
      varElem.addContent(makeVariableElement(variable, showValues));
    }
  } else if (showValues) {
    try {
      varElem.addContent(makeValuesElement(var, true));
    } catch (IOException e) {
      String message = String.format("Couldn't read values for %s. Omitting <values> element.%n\t%s",
          var.getFullName(), e.getMessage());
      log.warn(message);
    }
  }

  return varElem;
}
 
Example 7
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();
}
 
Example 8
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 9
Source File: NcmlWriter.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public Element makeVariableElement(Variable var, boolean showValues) {
  boolean isStructure = var instanceof Structure;

  Element varElem = new Element("variable", namespace);
  varElem.setAttribute("name", var.getShortName());

  StringBuilder buff = new StringBuilder();
  List dims = var.getDimensions();
  for (int i = 0; i < dims.size(); i++) {
    Dimension dim = (Dimension) dims.get(i);
    if (i > 0)
      buff.append(" ");
    if (dim.isShared())
      buff.append(dim.getShortName());
    else if (dim.isVariableLength())
      buff.append("*");
    else
      buff.append(dim.getLength());
  }
  // if (buff.length() > 0)
  varElem.setAttribute("shape", buff.toString());

  DataType dt = var.getDataType();
  if (dt != null) {
    varElem.setAttribute("type", dt.toString());
    if (dt.isEnum())
      varElem.setAttribute("typedef", var.getEnumTypedef().getShortName());
  }


  // attributes
  for (Attribute att : var.attributes()) {
    varElem.addContent(makeAttributeElement(att));
  }

  if (isStructure) {
    Structure s = (Structure) var;
    for (Variable variable : s.getVariables()) {
      varElem.addContent(makeVariableElement(variable, showValues));
    }
  } else if (showValues) {
    try {
      varElem.addContent(makeValuesElement(var, true));
    } catch (IOException e) {
      String message = String.format("Couldn't read values for %s. Omitting <values> element.%n\t%s",
          var.getFullName(), e.getMessage());
      log.warn(message);
    }
  }

  return varElem;
}