Java Code Examples for ucar.ma2.DataType#STRING

The following examples show how to use ucar.ma2.DataType#STRING . 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
private static DataType decodeAttributeType(ucar.nc2.stream.NcStreamProto.Attribute.Type dtype) {
  switch (dtype) {
    case STRING:
      return DataType.STRING;
    case BYTE:
      return DataType.BYTE;
    case SHORT:
      return DataType.SHORT;
    case INT:
      return DataType.INT;
    case LONG:
      return DataType.LONG;
    case FLOAT:
      return DataType.FLOAT;
    case DOUBLE:
      return DataType.DOUBLE;
  }
  throw new IllegalStateException("illegal att type " + dtype);
}
 
Example 2
Source File: Attribute.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Set the value as a String, trimming trailing zeros
 * 
 * @param val value of Attribute
 */
private void setStringValue(String val) {
  if (val == null)
    throw new IllegalArgumentException("Attribute value cannot be null");

  // get rid of trailing nul characters
  int len = val.length();
  while ((len > 0) && (val.charAt(len - 1) == 0))
    len--;
  if (len != val.length())
    val = val.substring(0, len);

  this.svalue = val;
  this.nelems = 1;
  this.dataType = DataType.STRING;
}
 
Example 3
Source File: Attribute.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Set the value as a String, trimming trailing zeros
 * 
 * @param svalue value of Attribute
 */
public Builder setStringValue(String svalue) {
  if (svalue == null)
    throw new IllegalArgumentException("Attribute value cannot be null");

  // get rid of trailing nul characters
  int len = svalue.length();
  while ((len > 0) && (svalue.charAt(len - 1) == 0))
    len--;
  if (len != svalue.length())
    svalue = svalue.substring(0, len);

  this.svalue = svalue;
  this.nelems = 1;
  this.dataType = DataType.STRING;
  return this;
}
 
Example 4
Source File: NUWGConvention.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
NavInfo(VariableDS.Builder vb) throws IOException {
  this.vb = vb;
  this.orgVar = vb.orgVar;
  valueType = vb.dataType;
  try {
    if ((valueType == DataType.CHAR) || (valueType == DataType.STRING))
      svalue = orgVar.readScalarString();
    else if (valueType == DataType.BYTE)
      bvalue = orgVar.readScalarByte();
    else if ((valueType == DataType.INT) || (valueType == DataType.SHORT))
      ivalue = orgVar.readScalarInt();
    else
      dvalue = orgVar.readScalarDouble();
  } catch (UnsupportedOperationException e) {
    parseInfo.format("Nav variable %s  not a scalar%n", getName());
  }
}
 
Example 5
Source File: NcDDS.java    From tds with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private BaseType createScalarVariable(NetcdfFile ncfile, Variable v) {
  DataType dt = v.getDataType();
  if (dt == DataType.DOUBLE)
    return new NcSDFloat64(v);
  else if (dt == DataType.FLOAT)
    return new NcSDFloat32(v);
  else if (dt == DataType.INT)
    return new NcSDInt32(v);
  else if (dt == DataType.UINT)
    return new NcSDUInt32(v);
  else if (dt == DataType.SHORT)
    return new NcSDInt16(v);
  else if (dt == DataType.USHORT)
    return new NcSDUInt16(v);
  else if ((dt == DataType.BYTE) || (dt == DataType.UBYTE))
    return new NcSDByte(v);
  else if (dt == DataType.CHAR)
    return new NcSDString(v);
  else if (dt == DataType.STRING)
    return new NcSDString(v);
  else if (dt == DataType.STRUCTURE)
    return createStructure(ncfile, (Structure) v);
  else
    throw new UnsupportedOperationException("NcDDS Variable data type = " + dt);
}
 
Example 6
Source File: UnidataObsDatasetHelper.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static Date getEndDate(NetcdfDataset ds) {
  Attribute att = ds.findGlobalAttributeIgnoreCase("time_coverage_end");
  if (null == att)
    throw new IllegalArgumentException("Must have a time_coverage_end global attribute");

  Date result;
  if (att.getDataType() == DataType.STRING) {
    result = DateUnit.getStandardOrISO(att.getStringValue());
  } else {
    throw new IllegalArgumentException("time_coverage_end must be a ISO or udunit date string");
  }

  return result;
}
 
Example 7
Source File: WriterCFPointAbstract.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void addDataVariablesClassic(Dimension recordDim, StructureData stnData, HashSet<String> varSet,
    String coordVars) {
  addDimensionsClassic(dataVars);

  for (StructureMembers.Member m : stnData.getMembers()) {
    VariableSimpleIF oldVar = findDataVar(m.getName());
    if (oldVar == null)
      continue;

    List<Dimension> dims = makeDimensionList(oldVar.getDimensions());
    dims.add(0, recordDim);

    Variable.Builder newVar;
    if (oldVar.getDataType() == DataType.STRING && !isExtendedModel) {
      // What should the string length be ??
      String name = oldVar.getShortName();
      Dimension strlen = new Dimension(name + "_strlen", defaultStringLength);
      newVar = Variable.builder().setName(name).setDataType(DataType.CHAR).setDimensions(dims).addDimension(strlen);
      writerb.getRootGroup().addDimensionIfNotExists(strlen);
    } else {
      newVar =
          Variable.builder().setName(oldVar.getShortName()).setDataType(oldVar.getDataType()).setDimensions(dims);
    }

    if (writerb.getRootGroup().replaceVariable(newVar)) {
      logger.warn("Variable was already added =" + oldVar.getShortName());
    }

    for (Attribute att : oldVar.attributes()) {
      String attName = att.getShortName();
      if (!reservedVariableAtts.contains(attName) && !attName.startsWith("_Coordinate"))
        newVar.addAttribute(att);
    }
    newVar.addAttribute(new Attribute(CF.COORDINATES, coordVars));
    varSet.add(oldVar.getShortName());
  }

}
 
Example 8
Source File: NetcdfCopier.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void copyAll(NetcdfFormatWriter ncwriter, Variable oldVar, Variable newVar) throws IOException {
  Array data = oldVar.read();
  try {
    if (!extended && oldVar.getDataType() == DataType.STRING) {
      data = convertDataToChar(newVar, data);
    }
    if (data.getSize() > 0) { // zero when record dimension = 0
      ncwriter.write(newVar, data);
    }

  } catch (InvalidRangeException e) {
    e.printStackTrace();
    throw new IOException(e.getMessage() + " for Variable " + oldVar.getFullName());
  }
}
 
Example 9
Source File: UnidataPointDatasetHelper.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private static double getAttAsDouble(NetcdfDataset ds, String attname) {
  Attribute att = ds.findGlobalAttributeIgnoreCase(attname);
  if (null == att)
    throw new IllegalArgumentException("Must have a " + attname + " global attribute");

  if (att.getDataType() == DataType.STRING) {
    return Double.parseDouble(att.getStringValue());
  } else {
    return att.getNumericValue().doubleValue();
  }
}
 
Example 10
Source File: NUWGConvention.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public String getStringValue() {
  if ((valueType == DataType.CHAR) || (valueType == DataType.STRING))
    return svalue;
  else if (valueType == DataType.BYTE)
    return Byte.toString(bvalue);
  else if ((valueType == DataType.INT) || (valueType == DataType.SHORT))
    return Integer.toString(ivalue);
  else
    return Double.toString(dvalue);
}
 
Example 11
Source File: Attribute.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Retrieve String value; only call if isString() is true.
 *
 * @return String if this is a String valued attribute, else null.
 * @see Attribute#isString
 */
@Nullable
public String getStringValue() {
  if (dataType != DataType.STRING)
    return null;
  return (svalue != null) ? svalue : _getStringValue(0);
}
 
Example 12
Source File: H5headerNew.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private Variable.Builder makeVariableMember(Group.Builder parentGroup, String name, long dataPos, MessageDatatype mdt)
    throws IOException {

  Vinfo vinfo = new Vinfo(mdt, null, dataPos); // LOOK need mds
  if (vinfo.getNCDataType() == null) {
    log.debug("SKIPPING DataType= " + vinfo.typeInfo.hdfType + " for variable " + name);
    return null;
  }

  if (mdt.type == 6) {
    Structure.Builder sb = Structure.builder().setName(name).setParentGroupBuilder(parentGroup);
    makeVariableShapeAndType(parentGroup, sb, mdt, null, vinfo, null);
    addMembersToStructure(parentGroup, sb, mdt);
    sb.setElementSize(mdt.byteSize);

    sb.setSPobject(vinfo);
    vinfo.setOwner(sb);
    return sb;

  } else {
    Variable.Builder vb = Variable.builder().setName(name).setParentGroupBuilder(parentGroup);
    makeVariableShapeAndType(parentGroup, vb, mdt, null, vinfo, null);

    // special case of variable length strings
    if (vb.dataType == DataType.STRING)
      vb.setElementSize(16); // because the array has elements that are HeapIdentifier
    else if (vb.dataType == DataType.OPAQUE) // special case of opaque
      vb.setElementSize(mdt.getBaseSize());

    vb.setSPobject(vinfo);
    vinfo.setOwner(vb);
    return vb;
  }
}
 
Example 13
Source File: CFPointObWriter.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public List<Dimension> getDimensions() {
  if (pov.getLen() > 1) {
    List<Dimension> dims = new ArrayList<Dimension>(1);
    String suffix =
        (pov.getDataType() == DataType.STRING) || (pov.getDataType() == DataType.CHAR) ? "_strlen" : "_len";
    dims.add(new Dimension(pov.getName() + suffix, pov.getLen(), false, false, false));
    return dims;
  } else
    return new ArrayList<Dimension>(0);
}
 
Example 14
Source File: NetcdfCopier.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
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 15
Source File: H5headerNew.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private TypeInfo calcNCtype(MessageDatatype mdt) {
  int hdfType = mdt.type;
  int byteSize = mdt.byteSize;
  byte[] flags = mdt.flags;
  // boolean unsigned = mdt.unsigned;

  TypeInfo tinfo = new TypeInfo(hdfType, byteSize);

  if (hdfType == 0) { // int, long, short, byte
    tinfo.dataType = getNCtype(hdfType, byteSize, mdt.unsigned);
    tinfo.endian = ((flags[0] & 1) == 0) ? RandomAccessFile.LITTLE_ENDIAN : RandomAccessFile.BIG_ENDIAN;
    tinfo.unsigned = ((flags[0] & 8) == 0);

  } else if (hdfType == 1) { // floats, doubles
    tinfo.dataType = getNCtype(hdfType, byteSize, mdt.unsigned);
    tinfo.endian = ((flags[0] & 1) == 0) ? RandomAccessFile.LITTLE_ENDIAN : RandomAccessFile.BIG_ENDIAN;

  } else if (hdfType == 2) { // time
    tinfo.dataType = DataType.STRING;
    tinfo.endian = ((flags[0] & 1) == 0) ? RandomAccessFile.LITTLE_ENDIAN : RandomAccessFile.BIG_ENDIAN;

  } else if (hdfType == 3) { // fixed length strings map to CHAR. String is used for Vlen type = 1.
    tinfo.dataType = DataType.CHAR;
    tinfo.vpad = (flags[0] & 0xf);
    // when elem length = 1, there is a problem with dimensionality.
    // eg char cr(2); has a storage_size of [1,1].

  } else if (hdfType == 4) { // bit field
    tinfo.dataType = getNCtype(hdfType, byteSize, mdt.unsigned);

  } else if (hdfType == 5) { // opaque
    tinfo.dataType = DataType.OPAQUE;

  } else if (hdfType == 6) { // structure
    tinfo.dataType = DataType.STRUCTURE;

  } else if (hdfType == 7) { // reference
    tinfo.endian = RandomAccessFile.LITTLE_ENDIAN;
    tinfo.dataType = DataType.LONG; // file offset of the referenced object
    // LOOK - should get the object, and change type to whatever it is (?)

  } else if (hdfType == 8) { // enums
    if (tinfo.byteSize == 1)
      tinfo.dataType = DataType.ENUM1;
    else if (tinfo.byteSize == 2)
      tinfo.dataType = DataType.ENUM2;
    else if (tinfo.byteSize == 4)
      tinfo.dataType = DataType.ENUM4;
    else {
      log.warn("Illegal byte suze for enum type = {}", tinfo.byteSize);
      throw new IllegalStateException("Illegal byte suze for enum type = " + tinfo.byteSize);
    }

    // enumMap = mdt.map;

  } else if (hdfType == 9) { // variable length array
    tinfo.isVString = mdt.isVString;
    tinfo.isVlen = mdt.isVlen;
    if (mdt.isVString) {
      tinfo.vpad = ((flags[0] >> 4) & 0xf);
      tinfo.dataType = DataType.STRING;
    } else {
      tinfo.dataType = getNCtype(mdt.getBaseType(), mdt.getBaseSize(), mdt.base.unsigned);
      tinfo.endian = mdt.base.endian;
      tinfo.unsigned = mdt.base.unsigned;
    }
  } else if (hdfType == 10) { // array : used for structure members
    tinfo.endian = (mdt.getFlags()[0] & 1) == 0 ? RandomAccessFile.LITTLE_ENDIAN : RandomAccessFile.BIG_ENDIAN;
    if (mdt.isVString()) {
      tinfo.dataType = DataType.STRING;
    } else {
      int basetype = mdt.getBaseType();
      tinfo.dataType = getNCtype(basetype, mdt.getBaseSize(), mdt.unsigned);
    }
  } else if (warnings) {
    log.debug("WARNING not handling hdf dataType = " + hdfType + " size= " + byteSize);
  }

  if (mdt.base != null) {
    tinfo.base = calcNCtype(mdt.base);
  }
  return tinfo;
}
 
Example 16
Source File: NcStream.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public static DataType convertDataType(ucar.nc2.stream.NcStreamProto.DataType dtype) {
  switch (dtype) {
    case CHAR:
      return DataType.CHAR;
    case BYTE:
      return DataType.BYTE;
    case SHORT:
      return DataType.SHORT;
    case INT:
      return DataType.INT;
    case LONG:
      return DataType.LONG;
    case FLOAT:
      return DataType.FLOAT;
    case DOUBLE:
      return DataType.DOUBLE;
    case STRING:
      return DataType.STRING;
    case STRUCTURE:
      return DataType.STRUCTURE;
    case SEQUENCE:
      return DataType.SEQUENCE;
    case ENUM1:
      return DataType.ENUM1;
    case ENUM2:
      return DataType.ENUM2;
    case ENUM4:
      return DataType.ENUM4;
    case OPAQUE:
      return DataType.OPAQUE;
    case UBYTE:
      return DataType.UBYTE;
    case USHORT:
      return DataType.USHORT;
    case UINT:
      return DataType.UINT;
    case ULONG:
      return DataType.ULONG;
  }
  throw new IllegalStateException("illegal data type " + dtype);
}
 
Example 17
Source File: H4header.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
void setFillValue(Attribute att) {
  // see IospHelper.makePrimitiveArray(int size, DataType dataType, Object fillValue)
  fillValue = (v.dataType == DataType.STRING) ? att.getStringValue() : att.getNumericValue();
}
 
Example 18
Source File: NcMLReader.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Parse the values element
 *
 * @param s JDOM element to parse
 * @return Array with parsed values
 * @throws IllegalArgumentException if string values not parsable to specified data type
 */
public static ucar.ma2.Array readAttributeValues(Element s) throws IllegalArgumentException {
  String valString = s.getAttributeValue("value");

  // can also be element text
  if (valString == null) {
    valString = s.getTextNormalize();
  }

  // no value specified hmm technically this is not illegal !!
  if (valString == null)
    throw new IllegalArgumentException("No value specified");

  String type = s.getAttributeValue("type");
  DataType dtype = (type == null) ? DataType.STRING : DataType.getType(type);
  if (dtype == DataType.CHAR)
    dtype = DataType.STRING;

  // backwards compatibility with deprecated isUnsigned attribute
  String unS = s.getAttributeValue("isUnsigned");
  boolean isUnsignedSet = "true".equalsIgnoreCase(unS);
  if (isUnsignedSet && dtype.isIntegral() && !dtype.isUnsigned()) {
    dtype = dtype.withSignedness(DataType.Signedness.UNSIGNED);
  }

  String sep = s.getAttributeValue("separator");
  if ((sep == null) && (dtype == DataType.STRING)) {
    List<String> list = new ArrayList<>();
    list.add(valString);
    return Array.makeArray(dtype, list);
  }

  if (sep == null)
    sep = " "; // default whitespace separated

  List<String> stringValues = new ArrayList<>();
  StringTokenizer tokn = new StringTokenizer(valString, sep);
  while (tokn.hasMoreTokens())
    stringValues.add(tokn.nextToken());

  return Array.makeArray(dtype, stringValues);
}
 
Example 19
Source File: AggregationNew.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * What is the data type of the aggregation coordinate ?
 *
 * @return the data type of the aggregation coordinate
 */
private DataType getCoordinateType() {
  List<Dataset> nestedDatasets = getDatasets();
  DatasetOuterDimension first = (DatasetOuterDimension) nestedDatasets.get(0);
  return first.isStringValued ? DataType.STRING : DataType.DOUBLE;
}
 
Example 20
Source File: DefaultConventions.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
@Nullable
protected AxisType getAxisType(VariableDS.Builder vb) {
  AxisType result = getAxisTypeCoards(vb);
  if (result != null) {
    return result;
  }

  String vname = vb.shortName;
  if (vname == null) {
    return null;
  }
  String unit = vb.getUnits();
  if (unit == null) {
    unit = "";
  }
  String desc = vb.getDescription();
  if (desc == null) {
    desc = "";
  }

  if (vname.equalsIgnoreCase("x") || findAlias(vb).equalsIgnoreCase("x")) {
    return AxisType.GeoX;
  }

  if (vname.equalsIgnoreCase("lon") || vname.equalsIgnoreCase("longitude") || findAlias(vb).equalsIgnoreCase("lon")) {
    return AxisType.Lon;
  }

  if (vname.equalsIgnoreCase("y") || findAlias(vb).equalsIgnoreCase("y")) {
    return AxisType.GeoY;
  }

  if (vname.equalsIgnoreCase("lat") || vname.equalsIgnoreCase("latitude") || findAlias(vb).equalsIgnoreCase("lat")) {
    return AxisType.Lat;
  }

  if (vname.equalsIgnoreCase("lev") || findAlias(vb).equalsIgnoreCase("lev")
      || (vname.equalsIgnoreCase("level") || findAlias(vb).equalsIgnoreCase("level"))) {
    return AxisType.GeoZ;
  }

  if (vname.equalsIgnoreCase("z") || findAlias(vb).equalsIgnoreCase("z") || vname.equalsIgnoreCase("altitude")
      || desc.contains("altitude") || vname.equalsIgnoreCase("depth") || vname.equalsIgnoreCase("elev")
      || vname.equalsIgnoreCase("elevation")) {
    if (SimpleUnit.isCompatible("m", unit)) // units of meters
    {
      return AxisType.Height;
    }
  }

  if (vname.equalsIgnoreCase("time") || findAlias(vb).equalsIgnoreCase("time")) {
    if (SimpleUnit.isDateUnit(unit)) {
      return AxisType.Time;
    }
  }

  if (vname.equalsIgnoreCase("time") && vb.dataType == DataType.STRING) {
    if (vb.orgVar != null) {
      try {
        Array firstValue = vb.orgVar.read("0");
        if (firstValue instanceof ArrayObject.D1) {
          ArrayObject.D1 sarry = (ArrayObject.D1) firstValue;
          String firstStringValue = (String) sarry.get(0);
          if (CalendarDate.parseISOformat(null, firstStringValue) != null) { // valid iso date string LOOK
            return AxisType.Time;
          }
        }
      } catch (IOException | InvalidRangeException e) {
        logger.warn("time string error", e);
      }
    }
  }

  return null;
}