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

The following examples show how to use ucar.nc2.Variable#getEnumTypedef() . 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 static Builder encodeVar(Variable var, int sizeToCache) throws IOException {
  Builder builder = NcStreamProto.Variable.newBuilder();
  builder.setName(var.getShortName());
  builder.setDataType(convertDataType(var.getDataType()));
  if (var.getDataType().isEnum()) {
    EnumTypedef enumType = var.getEnumTypedef();
    if (enumType != null)
      builder.setEnumType(enumType.getShortName());
  }

  for (Dimension dim : var.getDimensions()) {
    builder.addShape(encodeDim(dim));
  }

  for (Attribute att : var.attributes()) {
    builder.addAtts(encodeAtt(att));
  }

  // put small amounts of data in header "immediate mode"
  if (var.isCaching() && var.getDataType().isNumeric()) {
    if (var.isCoordinateVariable() || var.getSize() * var.getElementSize() < sizeToCache) {
      Array data = var.read();
      ByteBuffer bb = data.getDataAsByteBuffer();
      builder.setData(ByteString.copyFrom(bb.array()));
    }
  }

  return builder;
}
 
Example 2
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 3
@Test
public void problem() throws Exception {
  try (NetcdfFile ncfile = NetcdfFiles.open(TestDir.cdmLocalTestDataDir + "hdf5/test_atomic_types.nc")) {
    Variable primaryCloud = ncfile.findVariable("primary_cloud");
    assertThat((Object) primaryCloud).isNotNull();
    assertThat(primaryCloud.getDataType().isEnum());
    assertThat(primaryCloud.getDataType()).isEqualTo(DataType.ENUM1);
    assertThat(primaryCloud.getEnumTypedef()).isNotNull();
    EnumTypedef typedef = primaryCloud.getEnumTypedef();
    assertThat(typedef).isNotNull();
    // TODO disable this until we have a fix see Issue #126
    // assertThat(typedef.getShortName()).isEqualTo("cloud_class_t");
  }
}
 
Example 4
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;
}