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

The following examples show how to use ucar.nc2.Variable#attributes() . 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 testSimpleGeometryBuildCS() throws IOException, InvalidRangeException {
  NetcdfDataset data =
      NetcdfDataset.openDataset(TestDir.cdmLocalTestDataDir + "dataset/SimpleGeos/hru_soil_moist_3hru_5timestep.nc");
  List<CoordinateSystem> csl = data.getCoordinateSystems();
  SimpleGeometryCSBuilder builder = new SimpleGeometryCSBuilder(data, csl.get(0), null);
  Variable hru_test = data.findVariable("hru_soil_moist");

  SimpleGeometryFeature sgc =
      new SimpleGeometryFeature(hru_test.getFullNameEscaped(), hru_test.getDataType(), hru_test.attributes(),
          csl.get(0).getName(), hru_test.getUnitsString(), hru_test.getDescription(), null, GeometryType.POLYGON);
  sgc.setCoordSys(builder.makeCoordSys());

  // Test retrieval of axis
  Assert.assertEquals("catchments_x", sgc.getXAxis().getFullNameEscaped());
  Assert.assertEquals("catchments_y", sgc.getYAxis().getFullNameEscaped());
  Assert.assertEquals("hruid", sgc.getIDAxis().getFullNameEscaped());
  Assert.assertEquals(null, sgc.getZAxis());

  // Now test reading a geometry.
  SimpleGeometry testGeometry = sgc.readGeometry(0);
  boolean testInstancePolygon = false;

  if (testGeometry instanceof Polygon)
    testInstancePolygon = true;
  Assert.assertEquals(true, testInstancePolygon);

  Polygon polygonTestGeometry = (Polygon) testGeometry;
  Assert.assertEquals(6233, polygonTestGeometry.getPoints().size());
}
 
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;
}
 
Example 5
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;
}