Java Code Examples for ucar.ma2.DataType#SHORT

The following examples show how to use ucar.ma2.DataType#SHORT . 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: 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 3
Source File: CFPointWriterUtils.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private static void setDataArray(DataType dt, Array arr, Member m) {

    // Set the value (int, short, float, double...)
    if (dt == DataType.SHORT) {
      arr.setShort(0, m.getDataArray().getShort(0));
    }

    if (dt == DataType.INT) {
      arr.setInt(0, m.getDataArray().getInt(0));
    }

    if (dt == DataType.DOUBLE) {
      arr.setDouble(0, m.getDataArray().getDouble(0));
    }

    if (dt == DataType.FLOAT) {
      arr.setFloat(0, m.getDataArray().getFloat(0));
    }

  }
 
Example 4
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 5
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 6
Source File: NUWGConvention.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public double getDouble(String name) throws NoSuchElementException {
  NavInfo nav = findInfo(name);
  if (nav == null)
    throw new NoSuchElementException("GRIB1 " + name);

  if ((nav.valueType == DataType.DOUBLE) || (nav.valueType == DataType.FLOAT))
    return nav.dvalue;
  else if ((nav.valueType == DataType.INT) || (nav.valueType == DataType.SHORT))
    return (double) nav.ivalue;
  else if (nav.valueType == DataType.BYTE)
    return (double) nav.bvalue;

  throw new IllegalArgumentException("NUWGConvention.GRIB1.getDouble " + name + " type = " + nav.valueType);
}
 
Example 7
Source File: NUWGConvention.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public int getInt(String name) throws NoSuchElementException {
  NavInfo nav = findInfo(name);
  if (nav == null)
    throw new NoSuchElementException("GRIB1 " + name);

  if ((nav.valueType == DataType.INT) || (nav.valueType == DataType.SHORT))
    return nav.ivalue;
  else if ((nav.valueType == DataType.DOUBLE) || (nav.valueType == DataType.FLOAT))
    return (int) nav.dvalue;
  else if (nav.valueType == DataType.BYTE)
    return (int) nav.bvalue;

  throw new IllegalArgumentException("NUWGConvention.GRIB1.getInt " + name + " type = " + nav.valueType);
}
 
Example 8
Source File: H4header.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
Number get(DataType dataType, int index) {
  if (dataType == DataType.BYTE)
    return bb.get(index);
  if (dataType == DataType.SHORT)
    return bb.asShortBuffer().get(index);
  if (dataType == DataType.INT)
    return bb.asIntBuffer().get(index);
  if (dataType == DataType.LONG)
    return bb.asLongBuffer().get(index);
  if (dataType == DataType.FLOAT)
    return bb.asFloatBuffer().get(index);
  if (dataType == DataType.DOUBLE)
    return bb.asDoubleBuffer().get(index);
  return Double.NaN;
}
 
Example 9
Source File: TestPointDatasets.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
static private void checkData(StructureData sdata) {

    for (StructureMembers.Member member : sdata.getMembers()) {
      DataType dt = member.getDataType();
      if (dt == DataType.FLOAT) {
        sdata.getScalarFloat(member);
        sdata.getJavaArrayFloat(member);
      } else if (dt == DataType.DOUBLE) {
        sdata.getScalarDouble(member);
        sdata.getJavaArrayDouble(member);
      } else if (dt == DataType.BYTE) {
        sdata.getScalarByte(member);
        sdata.getJavaArrayByte(member);
      } else if (dt == DataType.SHORT) {
        sdata.getScalarShort(member);
        sdata.getJavaArrayShort(member);
      } else if (dt == DataType.INT) {
        sdata.getScalarInt(member);
        sdata.getJavaArrayInt(member);
      } else if (dt == DataType.LONG) {
        sdata.getScalarLong(member);
        sdata.getJavaArrayLong(member);
      } else if (dt == DataType.CHAR) {
        sdata.getScalarChar(member);
        sdata.getJavaArrayChar(member);
        sdata.getScalarString(member);
      } else if (dt == DataType.STRING) {
        sdata.getScalarString(member);
      }

      if ((dt != DataType.STRING) && (dt != DataType.CHAR) && (dt != DataType.STRUCTURE) && (dt != DataType.SEQUENCE)) {
        sdata.convertScalarFloat(member.getName());
      }

    }
  }
 
Example 10
Source File: CDMTypeFcns.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public static DataType daptype2cdmtype(DapType type) {
  assert (type != null);
  switch (type.getTypeSort()) {
    case Char:
      return DataType.CHAR;
    case UInt8:
      return DataType.UBYTE;
    case Int8:
      return DataType.BYTE;
    case Int16:
      return DataType.SHORT;
    case UInt16:
      return DataType.USHORT;
    case Int32:
      return DataType.INT;
    case UInt32:
      return DataType.UINT;
    case Int64:
      return DataType.LONG;
    case UInt64:
      return DataType.ULONG;
    case Float32:
      return DataType.FLOAT;
    case Float64:
      return DataType.DOUBLE;
    case String:
    case URL:
      return DataType.STRING;
    case Opaque:
      return DataType.OPAQUE;
    case Enum:
      // Coverity[FB.BC_UNCONFIRMED_CAST]
      DapEnumeration dapenum = (DapEnumeration) type;
      switch (dapenum.getBaseType().getTypeSort()) {
        case Char:
        case UInt8:
        case Int8:
          return DataType.ENUM1;
        case Int16:
        case UInt16:
          return DataType.ENUM2;
        case Int32:
        case UInt32:
          return DataType.ENUM4;
        case Int64:
        case UInt64:
          // since there is no ENUM8, use ENUM4
          return DataType.ENUM4;
        default:
          break;
      }
      break;
    case Structure:
      return DataType.STRUCTURE;
    case Sequence:
      return DataType.SEQUENCE;
    default:
      break;
  }
  return null;
}
 
Example 11
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 12
Source File: Attribute.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Write CDL representation into a Formatter.
 *
 * @param f write into this
 * @param strict if true, create strict CDL, escaping names
 * @deprecated use CDLWriter
 */
@Deprecated
protected void writeCDL(Formatter f, boolean strict, String parentname) {
  if (strict && (isString() || this.getEnumType() != null))
    // Force type explicitly for string.
    f.format("string "); // note lower case and trailing blank
  if (strict && parentname != null)
    f.format(NetcdfFiles.makeValidCDLName(parentname));
  f.format(":");
  f.format("%s", strict ? NetcdfFiles.makeValidCDLName(getShortName()) : getShortName());
  if (isString()) {
    f.format(" = ");
    for (int i = 0; i < getLength(); i++) {
      if (i != 0)
        f.format(", ");
      String val = getStringValue(i);
      if (val != null)
        f.format("\"%s\"", encodeString(val));
    }
  } else if (getEnumType() != null) {
    f.format(" = ");
    for (int i = 0; i < getLength(); i++) {
      if (i != 0)
        f.format(", ");
      EnumTypedef en = getEnumType();
      String econst = getStringValue(i);
      Integer ecint = en.lookupEnumInt(econst);
      if (ecint == null)
        throw new ForbiddenConversionException("Illegal enum constant: " + econst);
      f.format("\"%s\"", encodeString(econst));
    }
  } else {
    f.format(" = ");
    for (int i = 0; i < getLength(); i++) {
      if (i != 0)
        f.format(", ");

      Number number = 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);
      }
      f.format("%s", number);

      if (dataType.isUnsigned()) {
        f.format("U");
      }

      if (dataType == DataType.FLOAT)
        f.format("f");
      else if (dataType == DataType.SHORT || dataType == DataType.USHORT) {
        f.format("S");
      } else if (dataType == DataType.BYTE || dataType == DataType.UBYTE) {
        f.format("B");
      } else if (dataType == DataType.LONG || dataType == DataType.ULONG) {
        f.format("L");
      }
    }
  }
}
 
Example 13
Source File: N3headerWriter.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private void writeVars(List<Variable> vars, boolean largeFile, Formatter fout) throws IOException {
  int n = vars.size();
  if (n == 0) {
    raf.writeInt(0);
    raf.writeInt(0);
  } else {
    raf.writeInt(MAGIC_VAR);
    raf.writeInt(n);
  }

  for (Variable var : vars) {
    writeString(var.getShortName());

    // dimensions
    long vsize = var.getDataType().getSize(); // works for all netcdf-3 data types
    List<Dimension> dims = var.getDimensions();
    raf.writeInt(dims.size());
    for (Dimension dim : dims) {
      int dimIndex = findDimensionIndex(ncfile, dim);
      raf.writeInt(dimIndex);

      if (!dim.isUnlimited())
        vsize *= dim.getLength();
    }
    long unpaddedVsize = vsize;
    vsize += padding(vsize);

    // variable attributes
    long varAttsPos = raf.getFilePointer();
    writeAtts(var.attributes(), fout);

    // data type, variable size, beginning file position
    DataType dtype = var.getDataType();
    int type = getType(dtype);
    raf.writeInt(type);

    int vsizeWrite = (vsize < MAX_UNSIGNED_INT) ? (int) vsize : -1;
    raf.writeInt(vsizeWrite);
    long pos = raf.getFilePointer();
    if (largeFile)
      raf.writeLong(0); // come back to this later
    else
      raf.writeInt(0); // come back to this later

    // From nc3 file format specification
    // (https://www.unidata.ucar.edu/software/netcdf/docs/netcdf.html#NetCDF-Classic-Format):
    // Note on padding: In the special case of only a single record variable of character,
    // byte, or short type, no padding is used between data values.
    // 2/15/2011: we will continue to write the (incorrect) padded vsize into the header, but we will use the unpadded
    // size to read/write
    if (uvars.size() == 1 && uvars.get(0) == var) {
      if ((dtype == DataType.CHAR) || (dtype == DataType.BYTE) || (dtype == DataType.SHORT)) {
        vsize = unpaddedVsize;
      }
    }
    var.setSPobject(new N3headerNew.Vinfo(var.getShortName(), vsize, pos, var.isUnlimited(), varAttsPos));
  }
}
 
Example 14
Source File: H4type.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public static DataType setDataType(short type, Variable v) {
  DataType dt;
  switch (type) {
    case 3:
    case 21:
      dt = DataType.UBYTE;
      break;
    case 4:
      dt = DataType.CHAR;
      break;
    case 5:
      dt = DataType.FLOAT;
      break;
    case 6:
      dt = DataType.DOUBLE;
      break;
    case 20:
      dt = DataType.BYTE;
      break;
    case 22:
      dt = DataType.SHORT;
      break;
    case 23:
      dt = DataType.USHORT;
      break;
    case 24:
      dt = DataType.INT;
      break;
    case 25:
      dt = DataType.UINT;
      break;
    case 26:
      dt = DataType.LONG;
      break;
    case 27:
      dt = DataType.ULONG;
      break;
    default:
      throw new IllegalStateException("unknown type= " + type);
  }

  if (v != null) {
    v.setDataType(dt);
  }

  return dt;
}
 
Example 15
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");
      }
    }
  }
}