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

The following examples show how to use ucar.nc2.Attribute#getStringValue() . 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: FeatureDatasetCapabilitiesWriter.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
VariableSimpleAdapter(Element velem) {
  name = velem.getAttributeValue("name");
  String type = velem.getAttributeValue("type");
  dt = DataType.getType(type);

  atts = new ArrayList<>();
  List<Element> attElems = velem.getChildren("attribute");
  for (Element attElem : attElems) {
    String attName = attElem.getAttributeValue("name");
    ucar.ma2.Array values = NcMLReader.readAttributeValues(attElem);
    atts.add(new Attribute(attName, values));
  }

  for (Attribute att : atts) {
    if (att.getShortName().equals(CDM.UNITS))
      units = att.getStringValue();
    if (att.getShortName().equals(CDM.LONG_NAME))
      desc = att.getStringValue();
    if ((desc == null) && att.getShortName().equals("description"))
      desc = att.getStringValue();
    if ((desc == null) && att.getShortName().equals("standard_name"))
      desc = att.getStringValue();
  }
}
 
Example 2
Source File: EPSG_OGC_CF_Helper.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public String getWcs1_0CrsId(GridDatatype gridDatatype, GridDataset gridDataset) throws IllegalArgumentException {
  gridDataset.getTitle();
  gridDatatype.getFullName();

  StringBuilder buf = new StringBuilder();

  Attribute gridMappingAtt = gridDatatype.findAttributeIgnoreCase(CF.GRID_MAPPING);
  if (gridMappingAtt != null) {
    String gridMapping = gridMappingAtt.getStringValue();
    Variable gridMapVar = gridDataset.getNetcdfFile().getRootGroup().findVariableLocal(gridMapping);
    if (gridMapVar != null) {
      String gridMappingNameAtt = gridMapVar.attributes().findAttributeString(CF.GRID_MAPPING_NAME, null);
      if (gridMappingNameAtt != null)
        buf.append("EPSG:").append(ProjectionStandardsInfo.getProjectionByCfName(gridMappingNameAtt));
    }
  }

  return buf.toString();
}
 
Example 3
Source File: H5headerNew.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private void findDimensionScales(Group.Builder g, H5Group h5group, DataObjectFacade facade) throws IOException {
  Iterator<MessageAttribute> iter = facade.dobj.attributes.iterator();
  while (iter.hasNext()) {
    MessageAttribute matt = iter.next();
    if (matt.name.equals(HDF5_CLASS)) {
      Attribute att = makeAttribute(matt);
      if (att == null)
        throw new IllegalStateException();
      String val = att.getStringValue();
      if (val.equals(HDF5_DIMENSION_SCALE) && facade.dobj.mds.ndims > 0) {

        // create a dimension - always use the first dataspace length
        facade.dimList =
            addDimension(g, h5group, facade.name, facade.dobj.mds.dimLength[0], facade.dobj.mds.maxLength[0] == -1);
        facade.hasNetcdfDimensions = true;
        if (!h5iosp.includeOriginalAttributes)
          iter.remove();

        if (facade.dobj.mds.ndims > 1)
          facade.is2DCoordinate = true;
      }
    }
  }
}
 
Example 4
Source File: FmrcDataset.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void addAttributeInfo(NetcdfDataset result, String attName, String info) {
  Attribute att = result.findGlobalAttribute(attName);
  if (att == null)
    result.addAttribute(null, new Attribute(attName, info));
  else {
    String oldValue = att.getStringValue();
    result.addAttribute(null, new Attribute(attName, oldValue + " ;\n" + info));
  }
}
 
Example 5
Source File: IFPSConvention.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * @param ncfile the NetcdfFile to test
 * @return true if we think this is a IFPSConvention file.
 */
public static boolean isMine(NetcdfFile ncfile) {
  // check that file has a latitude and longitude variable, and that latitude has an attribute called
  // projectionType
  boolean geoVarsCheck;
  Variable v = ncfile.findVariable("latitude");
  if (null != ncfile.findVariable("longitude") && (null != v)) {
    geoVarsCheck = null != ncfile.findAttValueIgnoreCase(v, "projectionType", null);
  } else {
    // bail early
    return false;
  }

  // check that there is a global attribute called fileFormatVersion, and that it has one
  // of two known values
  boolean fileFormatCheck;
  Attribute ff = ncfile.findGlobalAttributeIgnoreCase("fileFormatVersion");
  if (ff != null) {
    String ffValue = ff.getStringValue();
    // two possible values (as of now)
    fileFormatCheck = (ffValue.equalsIgnoreCase("20030117") || ffValue.equalsIgnoreCase("20010816"));
  } else {
    // bail
    return false;
  }

  // both must be true
  return (geoVarsCheck && fileFormatCheck);
}
 
Example 6
Source File: NsslRadialAdapter.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public String getRadarID() {
  Attribute ga = ds.findGlobalAttribute("radarName-value");
  if (ga != null)
    return ga.getStringValue();
  else
    return "XXXX";
}
 
Example 7
Source File: CFRadialAdapter.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public String getRadarName() {
  Attribute ga = ds.findGlobalAttribute("instrument_name");
  if (ga != null) {
    return ga.getStringValue();
  } else {
    return "Unknown Station";
  }
}
 
Example 8
Source File: CFRadialAdapter.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public String getRadarID() {
  Attribute ga = ds.findGlobalAttribute("Station");
  if (ga != null) {
    return ga.getStringValue();
  } else {
    return "XXXX";
  }
}
 
Example 9
Source File: SimpleGeometryReader.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Given a variable name, returns the geometry type which that variable is associated with.
 * If the variable has no simple geometry information, null will be returned.
 * 
 * @param name variable name which will have geometry type be checked
 * @return Geometry Type if holds geometry information, null if not
 */
public GeometryType getGeometryType(String name) {
  Variable geometryVar = ds.findVariable(name);
  if (geometryVar == null)
    return null;

  // CFConvention
  if (ds.findGlobalAttribute(CF.CONVENTIONS) != null)
    if (ucar.nc2.dataset.conv.CF1Convention
        .getVersion(ds.findGlobalAttribute(CF.CONVENTIONS).getStringValue()) >= 8) {
      Attribute geometryTypeAttr;
      String geometry_type;

      geometryTypeAttr = geometryVar.findAttribute(CF.GEOMETRY_TYPE);
      if (geometryTypeAttr == null)
        return null;
      geometry_type = geometryTypeAttr.getStringValue();

      switch (geometry_type) {
        case CF.POLYGON:
          return GeometryType.POLYGON;
        case CF.LINE:
          return GeometryType.LINE;
        case CF.POINT:
          return GeometryType.POINT;
        default:
          return null;
      }
    }

  return null;
}
 
Example 10
Source File: FmrcDataset.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private String makeCoordinateList(VariableDS aggVar, String timeCoordName, boolean is2D) {
  String coords = "";
  Attribute att = aggVar.findAttribute(CF.COORDINATES);
  if (att == null)
    att = aggVar.findAttribute(_Coordinate.Axes);
  if (att != null)
    coords = att.getStringValue();

  if (is2D)
    return getRunDimensionName() + " " + timeCoordName + " " + coords;
  else
    return timeCoordName + "_" + getRunDimensionName() + " " + timeCoordName + " " + coords;
}
 
Example 11
Source File: ThreddsMetadataAcdd.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void addDate(String dateType) {
  Attribute att = ncfile.get(dateType);
  if (att != null) {
    String dateValue = att.getStringValue();

    try {
      tmi.addToList(Dataset.Dates, new DateType(dateValue, null, dateType));
    } catch (Exception e) {
      log.warn("MetadataExtractorAcdd Cant Parse {} date '{}' for {} message= {}", dateType, dateValue, ds.getName(),
          e.getMessage());
    }
  }
}
 
Example 12
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 13
Source File: TestCFPointWriterCompareProblem.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public boolean attCheckOk(Variable v, Attribute att) {
  if (att.getName().equals("_ChunkSizes")) {
    return false;
  }
  if (!att.isString()) {
    return true;
  }
  String val = att.getStringValue();
  if (val == null) {
    return true;
  }
  return !val.contains("Translation Date");
}
 
Example 14
Source File: MetadataExtractorAcdd.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void addDocumentation(String attName, String docType) {
  Attribute att = ncfile.get(attName);
  if (att != null) {
    String docValue = att.getStringValue();
    String dsValue = ds.getDocumentation(docType); // metadata/documentation[@type="docType"]
    if (dsValue == null || !dsValue.equals(docValue))
      tmi.addDocumentation(new InvDocumentation(null, null, null, docType, docValue));
  }
}
 
Example 15
Source File: NetcdfUtils.java    From OpenDA with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Returns the trimmed String value of the attribute with the given attributeName for the given variable.
 *
 * @param variable
 * @param attributeName
 * @return String or null.
 */
private static String getAttributeStringValue(Variable variable, String attributeName) {
	Attribute attribute = variable.findAttribute(attributeName);
	if (attribute == null) {
		return null;
	}

	String value = attribute.getStringValue();
	if (value == null) {
		return null;
	}

	return value.trim();
}
 
Example 16
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 17
Source File: TestCFPointWriterCompare.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public boolean attCheckOk(Variable v, Attribute att) {
  if (att.getName().equals("_ChunkSizes")) {
    return false;
  }
  if (!att.isString()) {
    return true;
  }
  String val = att.getStringValue();
  if (val == null) {
    return true;
  }
  return !val.contains("Translation Date");
}
 
Example 18
Source File: TableConfigurerImpl.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public boolean match(CoordinateAxis axis) {
  Attribute stdName = axis.findAttribute(CF.STANDARD_NAME);
  if (stdName == null)
    return true;
  String val = stdName.getStringValue();
  return !CF.SURFACE_ALTITUDE.equals(val) && !CF.STATION_ALTITUDE.equals(val);
}
 
Example 19
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");
      }
    }
  }
}
 
Example 20
Source File: CoordSysTable.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private String getCalendarAttribute(Variable vds) {
  Attribute cal = vds.findAttribute("calendar");
  return (cal == null) ? null : cal.getStringValue();
}