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

The following examples show how to use ucar.nc2.Attribute#isString() . 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: NcStream.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public static NcStreamProto.Attribute.Builder encodeAtt(Attribute att) {
  NcStreamProto.Attribute.Builder attBuilder = NcStreamProto.Attribute.newBuilder();
  attBuilder.setName(att.getShortName());
  attBuilder.setDataType(convertDataType(att.getDataType()));
  attBuilder.setLen(att.getLength());

  // values
  if (att.getLength() > 0) {
    if (att.isString()) {
      for (int i = 0; i < att.getLength(); i++)
        attBuilder.addSdata(att.getStringValue(i));

    } else {
      Array data = att.getValues();
      ByteBuffer bb = data.getDataAsByteBuffer();
      attBuilder.setData(ByteString.copyFrom(bb.array()));
    }
  }

  return attBuilder;
}
 
Example 2
Source File: IFPSConvention.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private Projection makeLCProjection(NetcdfDataset ds) {
  Attribute latLonOrigin = projVar.attributes().findAttributeIgnoreCase("latLonOrigin");
  if (latLonOrigin == null || latLonOrigin.isString())
    throw new IllegalStateException();
  double centralLon = latLonOrigin.getNumericValue(0).doubleValue();
  double centralLat = latLonOrigin.getNumericValue(1).doubleValue();

  double par1 = findAttributeDouble("stdParallelOne");
  double par2 = findAttributeDouble("stdParallelTwo");
  LambertConformal lc = new LambertConformal(centralLat, centralLon, par1, par2);

  // make Coordinate Transform Variable
  ProjectionCT ct = new ProjectionCT("lambertConformalProjection", "FGDC", lc);
  VariableDS ctVar = makeCoordinateTransformVariable(ds, ct);
  ctVar.addAttribute(new Attribute(_Coordinate.Axes, "xCoord yCoord"));
  ds.addVariable(null, ctVar);

  return lc;
}
 
Example 3
Source File: IFPSConvention.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private Projection makeLCProjection() {
  Attribute latLonOrigin = projVar.getAttributeContainer().findAttributeIgnoreCase("latLonOrigin");
  if (latLonOrigin == null || latLonOrigin.isString())
    throw new IllegalStateException();
  double centralLon = latLonOrigin.getNumericValue(0).doubleValue();
  double centralLat = latLonOrigin.getNumericValue(1).doubleValue();

  double par1 = findAttributeDouble("stdParallelOne");
  double par2 = findAttributeDouble("stdParallelTwo");
  LambertConformal lc = new LambertConformal(centralLat, centralLon, par1, par2);

  // make Coordinate Transform Variable
  ProjectionCT ct = new ProjectionCT("lambertConformalProjection", "FGDC", lc);
  VariableDS.Builder ctVar = makeCoordinateTransformVariable(ct);
  ctVar.addAttribute(new Attribute(_Coordinate.Axes, "xCoord yCoord"));
  rootGroup.addVariable(ctVar);

  return lc;
}
 
Example 4
Source File: DecoderWrapper.java    From sis with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the value of the attribute of the given name as a date, or {@code null} if none.
 *
 * @param  name  the name of the attribute to search, or {@code null}.
 * @return the attribute value, or {@code null} if none or unparsable or if the given name was null.
 */
@Override
public Date dateValue(final String name) {
    if (name != null) {
        for (final Group group : groups) {
            final Attribute attribute = findAttribute(group, name);
            if (attribute != null && attribute.isString()) {
                String value = Utils.nonEmpty(attribute.getStringValue());
                if (value != null) {
                    final CalendarDate date;
                    try {
                        date = CalendarDateFormatter.isoStringToCalendarDate(Calendar.proleptic_gregorian, value);
                    } catch (IllegalArgumentException e) {
                        listeners.warning(e);
                        continue;
                    }
                    return new Date(date.getMillis());
                }
            }
        }
    }
    return null;
}
 
Example 5
Source File: UnidataTrajectoryObsDataset.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static boolean isValidFile(NetcdfFile ds) {
  Attribute cdmDtAtt = ds.findGlobalAttributeIgnoreCase("cdm_data_type");
  if (cdmDtAtt == null)
    cdmDtAtt = ds.findGlobalAttributeIgnoreCase("cdm_datatype");
  if (cdmDtAtt == null)
    return false;
  if (!cdmDtAtt.isString())
    return false;

  String cdmDtString = cdmDtAtt.getStringValue();
  if (cdmDtString == null)
    return false;
  if (!cdmDtString.equalsIgnoreCase(FeatureType.TRAJECTORY.toString()))
    return false;

  Attribute conventionsAtt = ds.findGlobalAttributeIgnoreCase("Conventions");
  if (conventionsAtt == null)
    return (false);
  if (!conventionsAtt.isString())
    return (false);
  String convString = conventionsAtt.getStringValue();

  StringTokenizer stoke = new StringTokenizer(convString, ",");
  while (stoke.hasMoreTokens()) {
    String toke = stoke.nextToken().trim();
    if (toke.equalsIgnoreCase("Unidata Observation Dataset v1.0"))
      return true;
  }

  return false;
}
 
Example 6
Source File: AWIPSConvention.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
double findAttributeDouble(String attname) {
  Attribute att = rootGroup.getAttributeContainer().findAttributeIgnoreCase(attname);
  if (att == null || att.isString()) {
    parseInfo.format("ERROR cant find numeric attribute= %s%n", attname);
    return Double.NaN;
  }
  return att.getNumericValue().doubleValue();
}
 
Example 7
Source File: StandardFields.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private Field(TypeAndOrder tao, Variable v) {
  this.tao = tao;
  this.memberName = v.getShortName();
  Attribute att = v.attributes().findAttribute("scale_factor");
  if (att != null && !att.isString()) {
    scale = att.getNumericValue().doubleValue();
    hasScale = true;
  }
  att = v.attributes().findAttribute("add_offset");
  if (att != null && !att.isString()) {
    offset = att.getNumericValue().doubleValue();
    hasScale = true;
  }
}
 
Example 8
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 9
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 10
Source File: TestCoverageFileWritingCompare.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 11
Source File: VariableWrapper.java    From sis with Apache License 2.0 5 votes vote down vote up
/**
 * Implementation of {@link #getAttributeValue(String)} shared with {@link GroupWrapper}.
 */
static Object getAttributeValue(final Attribute attribute) {
    if (attribute != null) {
        final int length = attribute.getLength();
        switch (length) {
            case 0: break;
            case 1: {
                final Object value = attribute.getValue(0);
                if (value instanceof String) {
                    return Utils.nonEmpty((String) value);
                } else if (value instanceof Number) {
                    return Utils.fixSign((Number) value, attribute.isUnsigned());
                }
                break;
            }
            default: {
                if (attribute.isString()) {
                    boolean hasValues = false;
                    final String[] values = new String[length];
                    for (int i=0; i<length; i++) {
                        values[i] = Utils.nonEmpty(attribute.getStringValue(i));
                        hasValues |= (values[i] != null);
                    }
                    if (hasValues) {
                        return values;
                    }
                } else {
                    final Array array = attribute.getValues();
                    return createDecimalVector(array.get1DJavaArray(array.getElementType()), attribute.isUnsigned());
                }
            }
        }
    }
    return null;
}
 
Example 12
Source File: DecoderWrapper.java    From sis with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the value for the attribute of the given name, or {@code null} if none.
 * This method searches in the groups specified by the last call to {@link #setSearchPath(String[])}.
 * Null values and empty strings are ignored.
 *
 * @param  name  the name of the attribute to search, or {@code null}.
 * @return the attribute value, or {@code null} if none or empty or if the given name was null.
 */
@Override
public String stringValue(final String name) {
    if (name != null) {                                 // For createResponsibleParty(...) convenience.
        for (final Group group : groups) {
            final Attribute attribute = findAttribute(group, name);
            if (attribute != null && attribute.isString()) {
                return Utils.nonEmpty(attribute.getStringValue());
            }
        }
    }
    return null;
}
 
Example 13
Source File: ThreddsMetadata.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public void addProperties(List<Attribute> atts) {
  for (Attribute att : atts)
    if (att.isString())
      properties.add(new InvProperty(att.getShortName(), att.getStringValue()));
}
 
Example 14
Source File: IFPSConvention.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private double findAttributeDouble(String attname) {
  Attribute att = projVar.attributes().findAttributeIgnoreCase(attname);
  return (att == null || att.isString()) ? Double.NaN : att.getNumericValue().doubleValue();
}
 
Example 15
Source File: IFPSConvention.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private double findAttributeDouble(String attname) {
  Attribute att = projVar.getAttributeContainer().findAttributeIgnoreCase(attname);
  return (att == null || att.isString()) ? Double.NaN : att.getNumericValue().doubleValue();
}
 
Example 16
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");
      }
    }
  }
}