Java Code Examples for ucar.nc2.VariableSimpleIF#getUnitsString()

The following examples show how to use ucar.nc2.VariableSimpleIF#getUnitsString() . 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: ThreddsMetadataExtractor.java    From tds with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public ThreddsMetadata.VariableGroup extractVariables(FeatureDatasetPoint fd) {
  List<ThreddsMetadata.Variable> vars = new ArrayList<>();

  List<VariableSimpleIF> dataVars = fd.getDataVariables();
  if (dataVars == null)
    return null;

  for (VariableSimpleIF v : dataVars) {
    String name = v.getShortName();
    String desc = v.getDescription();
    String units = v.getUnitsString();
    String vname = null;
    String id = null;

    ucar.nc2.Attribute att = v.attributes().findAttributeIgnoreCase("standard_name");
    if (att != null)
      vname = att.getStringValue();
    vars.add(new ThreddsMetadata.Variable(name, desc, vname, units, id));
  }

  Collections.sort(vars);
  // String vocab, String vocabHref, URI vocabUri, URI mapUri, List<Variable> variables
  return new ThreddsMetadata.VariableGroup("CF-1.0", null, null, vars);
}
 
Example 2
Source File: NcUnitReference.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static UnitReference initUom(UnitReference uom, VariableSimpleIF dataVar) {
  // @code
  String udunits = dataVar.getUnitsString();
  if (udunits == null) { // Variable may not have a "units" attribute.
    return null;
  }

  String ucum = ErddapEDUnits.udunitsToUcum(udunits);
  uom.setCode(ucum);

  return uom;
}
 
Example 3
Source File: StationSubsetWriterXML.java    From tds with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
protected void writeStationPointFeature(StationPointFeature stationPointFeat) throws XMLStreamException, IOException {
  Station station = stationPointFeat.getStation();

  staxWriter.writeCharacters("\n    ");
  staxWriter.writeStartElement("stationFeature");
  staxWriter.writeAttribute("date",
      CalendarDateFormatter.toDateTimeStringISO(stationPointFeat.getObservationTimeAsCalendarDate()));

  staxWriter.writeCharacters("\n        ");
  staxWriter.writeStartElement("station");
  staxWriter.writeAttribute("name", station.getName());
  staxWriter.writeAttribute("latitude", Format.dfrac(station.getLatitude(), 3));
  staxWriter.writeAttribute("longitude", Format.dfrac(station.getLongitude(), 3));
  if (!Double.isNaN(station.getAltitude()))
    staxWriter.writeAttribute("altitude", Format.dfrac(station.getAltitude(), 0));
  if (station.getDescription() != null)
    staxWriter.writeCharacters(station.getDescription());
  staxWriter.writeEndElement();

  for (VariableSimpleIF wantedVar : wantedVariables) {
    staxWriter.writeCharacters("\n        ");
    staxWriter.writeStartElement("data");
    staxWriter.writeAttribute("name", wantedVar.getShortName());
    if (wantedVar.getUnitsString() != null)
      staxWriter.writeAttribute(CDM.UNITS, wantedVar.getUnitsString());

    Array dataArray = stationPointFeat.getDataAll().getArray(wantedVar.getShortName());
    String ss = dataArray.toString();
    Class elemType = dataArray.getElementType();
    if ((elemType == String.class) || (elemType == char.class) || (elemType == StructureData.class))
      ss = ucar.nc2.util.xml.Parse.cleanCharacterData(ss); // make sure no bad chars
    staxWriter.writeCharacters(ss.trim());
    staxWriter.writeEndElement();
  }

  staxWriter.writeCharacters("\n    ");
  staxWriter.writeEndElement();
}
 
Example 4
Source File: StationSubsetWriterCSV.java    From tds with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
protected void writeHeader(StationPointFeature stationPointFeat) throws IOException {
  writer.print("time,station,latitude[unit=\"degrees_north\"],longitude[unit=\"degrees_east\"]");
  for (VariableSimpleIF wantedVar : wantedVariables) {
    writer.print(",");
    writer.print(wantedVar.getShortName());
    if (wantedVar.getUnitsString() != null)
      writer.print("[unit=\"" + wantedVar.getUnitsString() + "\"]");
  }
  writer.println();
}
 
Example 5
Source File: PointSubsetWriterCSV.java    From tds with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void writeHeader(PointFeature pf) {
  writer.print("time,latitude[unit=\"degrees_north\"],longitude[unit=\"degrees_east\"]");
  for (VariableSimpleIF wantedVar : wantedVariables) {
    writer.print(",");
    writer.print(wantedVar.getShortName());
    if (wantedVar.getUnitsString() != null)
      writer.print("[unit=\"" + wantedVar.getUnitsString() + "\"]");
  }
  writer.println();
}
 
Example 6
Source File: PointSubsetWriterXML.java    From tds with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void writePoint(PointFeature pointFeat) throws XMLStreamException, IOException {
  EarthLocation loc = pointFeat.getLocation();

  staxWriter.writeCharacters("\n    ");
  staxWriter.writeStartElement("pointFeature");
  staxWriter.writeAttribute("date",
      CalendarDateFormatter.toDateTimeStringISO(pointFeat.getObservationTimeAsCalendarDate()));

  staxWriter.writeCharacters("\n        ");
  staxWriter.writeEmptyElement("location");
  staxWriter.writeAttribute("latitude", Format.dfrac(loc.getLatitude(), 3));
  staxWriter.writeAttribute("longitude", Format.dfrac(loc.getLongitude(), 3));
  if (!Double.isNaN(loc.getAltitude()))
    staxWriter.writeAttribute("altitude", Format.dfrac(loc.getAltitude(), 0));

  StructureData structureData = pointFeat.getDataAll();
  for (VariableSimpleIF wantedVar : wantedVariables) {
    staxWriter.writeCharacters("\n        ");
    staxWriter.writeStartElement("data");
    staxWriter.writeAttribute("name", wantedVar.getShortName());
    if (wantedVar.getUnitsString() != null)
      staxWriter.writeAttribute(CDM.UNITS, wantedVar.getUnitsString());

    Array dataArray = structureData.getArray(wantedVar.getShortName());
    String ss = dataArray.toString();
    Class elemType = dataArray.getElementType();
    if ((elemType == String.class) || (elemType == char.class) || (elemType == StructureData.class))
      ss = ucar.nc2.util.xml.Parse.cleanCharacterData(ss); // make sure no bad chars
    staxWriter.writeCharacters(ss.trim());
    staxWriter.writeEndElement();
  }

  staxWriter.writeCharacters("\n    ");
  staxWriter.writeEndElement();
}