Java Code Examples for ucar.ma2.DataType#DOUBLE

The following examples show how to use ucar.ma2.DataType#DOUBLE . 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: Time2DOffsetCoordSys.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private CoverageCoordAxis makeScalarTimeCoord(double val, CoverageCoordAxis1D runAxisSubset) {
  String name = "constantForecastTime";
  String desc = "forecast time";
  AttributeContainerMutable atts = new AttributeContainerMutable(name);
  atts.addAttribute(new Attribute(CDM.UNITS, runAxisSubset.getUnits()));
  atts.addAttribute(new Attribute(CF.STANDARD_NAME, CF.TIME));
  atts.addAttribute(new Attribute(CDM.LONG_NAME, desc));
  atts.addAttribute(new Attribute(CF.CALENDAR, runAxisSubset.getCalendar().toString()));

  CoverageCoordAxisBuilder builder = new CoverageCoordAxisBuilder(name, runAxisSubset.getUnits(), desc,
      DataType.DOUBLE, AxisType.Time, atts, CoverageCoordAxis.DependenceType.scalar, null,
      CoverageCoordAxis.Spacing.regularPoint, 1, val, val, 0.0, null, null);
  builder.setIsSubset(true);

  return new CoverageCoordAxis1D(builder);
}
 
Example 2
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 3
Source File: AWIPSConvention.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private CoordinateAxis makeLatCoordAxis(NetcdfDataset ds, int n, String xname) {
  double min = findAttributeDouble(ds, "yMin");
  double max = findAttributeDouble(ds, "yMax");
  double d = findAttributeDouble(ds, "dy");
  if (Double.isNaN(min) || Double.isNaN(max) || Double.isNaN(d))
    return null;

  CoordinateAxis v = new CoordinateAxis1D(ds, null, xname, DataType.DOUBLE, xname, CDM.LAT_UNITS, "latitude");
  v.setValues(n, min, d);
  v.addAttribute(new Attribute(_Coordinate.AxisType, AxisType.Lat.toString()));

  double maxCalc = min + d * n;
  parseInfo.format("Created Lat Coordinate Axis (max calc= %f should be = %f)%n", maxCalc, max);
  v.getNameAndDimensions(parseInfo, true, false);
  parseInfo.format("%n");

  return v;
}
 
Example 4
Source File: AWIPSConvention.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private CoordinateAxis makeLonCoordAxis(NetcdfDataset ds, int n, String xname) {
  double min = findAttributeDouble(ds, "xMin");
  double max = findAttributeDouble(ds, "xMax");
  double d = findAttributeDouble(ds, "dx");
  if (Double.isNaN(min) || Double.isNaN(max) || Double.isNaN(d))
    return null;

  CoordinateAxis v = new CoordinateAxis1D(ds, null, xname, DataType.DOUBLE, xname, CDM.LON_UNITS, "longitude");
  v.setValues(n, min, d);
  v.addAttribute(new Attribute(_Coordinate.AxisType, AxisType.Lon.toString()));

  double maxCalc = min + d * n;
  parseInfo.format("Created Lon Coordinate Axis (max calc= %f shoule be = %f)%n", maxCalc, max);
  v.getNameAndDimensions(parseInfo, true, false);
  parseInfo.format("%n");

  return v;
}
 
Example 5
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 6
Source File: FmrcDataset.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private VariableDS makeOffsetCoordinate(NetcdfDataset result, Group group, String dimName, CalendarDate base,
    double[] values) {
  DataType dtype = DataType.DOUBLE;
  VariableDS timeVar = new VariableDS(result, group, null, dimName + "_offset", dtype, dimName, null, null); // LOOK
                                                                                                             // could
                                                                                                             // just
                                                                                                             // make a
                                                                                                             // CoordinateAxis1D
  timeVar.addAttribute(new Attribute(CDM.LONG_NAME, "offset hour from start of run for coordinate = " + dimName));
  timeVar.addAttribute(new ucar.nc2.Attribute("standard_name", "forecast_period"));
  timeVar.addAttribute(new ucar.nc2.Attribute(CF.CALENDAR, base.getCalendar().name()));
  timeVar.addAttribute(new ucar.nc2.Attribute(CDM.UNITS, "hours since " + base));
  timeVar.addAttribute(new ucar.nc2.Attribute(CDM.MISSING_VALUE, Double.NaN));

  // construct the values
  int ntimes = values.length;
  ArrayDouble.D1 timeCoordVals = (ArrayDouble.D1) Array.factory(DataType.DOUBLE, new int[] {ntimes}, values);
  timeVar.setCachedData(timeCoordVals);
  group.addVariable(timeVar);

  return timeVar;
}
 
Example 7
Source File: AWIPSsatConvention.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private CoordinateAxis makeYCoordAxis(NetcdfDataset ds, int ny, String yname) {
  CoordinateAxis v = new CoordinateAxis1D(ds, null, yname, DataType.DOUBLE, yname, "km", "y on projection");
  v.setValues(ny, starty, dy);

  parseInfo.format("Created Y Coordinate Axis = ");
  v.getNameAndDimensions(parseInfo, true, false);
  parseInfo.format("%n");

  if (debugProj)
    parseInfo.format("  makeYCoordAxis ending y %f ny= %d dy= %f%n", starty + ny * dy, ny, dy);
  return v;
}
 
Example 8
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 9
Source File: AWIPSsatConvention.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private CoordinateAxis makeXCoordAxis(NetcdfDataset ds, int nx, String xname) {
  CoordinateAxis v = new CoordinateAxis1D(ds, null, xname, DataType.DOUBLE, xname, "km", "x on projection");
  v.setValues(nx, startx, dx);

  parseInfo.format("Created X Coordinate Axis = ");
  v.getNameAndDimensions(parseInfo, true, false);
  parseInfo.format("%n");

  if (debugProj)
    parseInfo.format("  makeXCoordAxis ending x %f nx= %d dx= %f%n", startx + nx * dx, nx, dx);
  return v;
}
 
Example 10
Source File: AWIPSConvention.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private CoordinateAxis makeTimeCoordAxisFromReference(NetcdfDataset ds, Variable timeVar, Array vals) {
  Variable refVar = ds.findVariable("reftime");
  if (refVar == null)
    return null;
  double refValue;
  try {
    Array refArray = refVar.read();
    refValue = refArray.getDouble(refArray.getIndex()); // get the first value
  } catch (IOException ioe) {
    return null;
  }
  if (refValue == N3iosp.NC_FILL_DOUBLE)
    return null;

  // construct the values array - make it a double to be safe
  Array dvals = Array.factory(DataType.DOUBLE, vals.getShape());
  IndexIterator diter = dvals.getIndexIterator();
  IndexIterator iiter = vals.getIndexIterator();
  while (iiter.hasNext())
    diter.setDoubleNext(iiter.getDoubleNext() + refValue); // add reftime to each of the values

  String units = ds.findAttValueIgnoreCase(refVar, CDM.UNITS, "seconds since 1970-1-1 00:00:00");
  units = normalize(units);
  String desc = "synthesized time coordinate from reftime, valtimeMINUSreftime";
  CoordinateAxis1D timeCoord = new CoordinateAxis1D(ds, null, "timeCoord", DataType.DOUBLE, "record", units, desc);

  timeCoord.setCachedData(dvals, true);

  parseInfo.format("Created Time Coordinate Axis From Reference = ");
  timeCoord.getNameAndDimensions(parseInfo, true, false);
  parseInfo.format("%n");

  return timeCoord;
}
 
Example 11
Source File: AWIPSsatConvention.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private CoordinateAxis makeLonCoordAxis(NetcdfDataset ds, int nx, String xname) {
  CoordinateAxis v = new CoordinateAxis1D(ds, null, xname, DataType.DOUBLE, xname, CDM.LON_UNITS, "longitude");
  v.setValues(nx, startx, dx);

  parseInfo.format("Created X Coordinate Axis = ");
  v.getNameAndDimensions(parseInfo, true, false);
  parseInfo.format("%n");
  return v;
}
 
Example 12
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 13
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 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: 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 16
Source File: VariableEnhancer.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public Array convert(Array in, boolean convertUnsigned, boolean applyScaleOffset, boolean convertMissing) {
  if (!in.getDataType().isNumeric() || (!convertUnsigned && !applyScaleOffset && !convertMissing)) {
    return in; // Nothing to do!
  }

  if (getSignedness() == Signedness.SIGNED) {
    convertUnsigned = false;
  }
  if (!hasScaleOffset()) {
    applyScaleOffset = false;
  }

  DataType outType = origDataType;
  if (convertUnsigned) {
    outType = getUnsignedConversionType();
  }
  if (applyScaleOffset) {
    outType = getScaledOffsetType();
  }

  if (outType != DataType.FLOAT && outType != DataType.DOUBLE) {
    convertMissing = false;
  }

  Array out = Array.factory(outType, in.getShape());
  IndexIterator iterIn = in.getIndexIterator();
  IndexIterator iterOut = out.getIndexIterator();

  while (iterIn.hasNext()) {
    Number value = (Number) iterIn.getObjectNext();

    if (convertUnsigned) {
      value = convertUnsigned(value);
    }
    if (applyScaleOffset) {
      value = applyScaleOffset(value);
    }
    if (convertMissing) {
      value = convertMissing(value);
    }

    iterOut.setObjectNext(value);
  }

  return out;
}
 
Example 17
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<AggDataset> nestedDatasets = getDatasets();
  AggDatasetOuter first = (AggDatasetOuter) nestedDatasets.get(0);
  return first.isStringValued ? DataType.STRING : DataType.DOUBLE;
}
 
Example 18
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 19
Source File: Time2DCoordSys.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private CoverageCoordAxis makeCFTimeCoord(CoverageCoordAxis1D runAxisSubset, CoverageCoordAxis1D timeAxisSubset) {
  String name = timeAxisSubset.getName() + "Forecast";
  String desc = "forecast time";
  AttributeContainerMutable atts = new AttributeContainerMutable(name);
  atts.addAttribute(new Attribute(CDM.UNITS, runAxisSubset.getUnits()));
  atts.addAttribute(new Attribute(CF.STANDARD_NAME, CF.TIME));
  atts.addAttribute(new Attribute(CDM.LONG_NAME, desc));
  atts.addAttribute(new Attribute(CF.CALENDAR, runAxisSubset.getCalendar().toString()));

  if (runAxisSubset.getNcoords() == 1) {
    CoverageCoordAxisBuilder builder = new CoverageCoordAxisBuilder();
    builder.name = name;
    builder.units = runAxisSubset.getUnits();
    builder.description = desc;
    builder.dataType = DataType.DOUBLE;
    builder.axisType = AxisType.Time;
    builder.attributes = atts;
    builder.dependenceType = CoverageCoordAxis.DependenceType.dependent;
    builder.setDependsOn(timeAxisSubset.getName());

    builder.spacing = timeAxisSubset.getSpacing();
    builder.ncoords = timeAxisSubset.ncoords;
    builder.isSubset = true;

    // conversion from timeOffset to runtime units
    double offset = timeAxisSubset.getOffsetInTimeUnits(runAxis.getRefDate(), timeAxisSubset.getRefDate());

    switch (timeAxisSubset.getSpacing()) {
      case regularInterval:
      case regularPoint:
        builder.startValue = timeAxisSubset.getStartValue() + offset;
        builder.endValue = timeAxisSubset.getEndValue() + offset;
        break;

      case contiguousInterval:
      case irregularPoint:
      case discontiguousInterval:
        builder.values = timeAxisSubset.getValues(); // this is a copy
        for (int i = 0; i < builder.values.length; i++)
          builder.values[i] += offset;
        break;
    }

    return new CoverageCoordAxis1D(builder);
  }

  return null;
}
 
Example 20
Source File: CoverageDatasetCapabilities.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private Element writeAxis(CoverageCoordAxis axis) {
  Element varElem = new Element("axis");
  varElem.setAttribute("name", axis.getName());
  varElem.setAttribute("shape", Arrays.toString(axis.getShape()));

  DataType dt = axis.getDataType();
  varElem.setAttribute("type", dt.toString());

  AxisType axisType = axis.getAxisType();
  if (null != axisType)
    varElem.setAttribute("axisType", axisType.toString());

  if (axis.getDependsOn() != null && !axis.getDependsOn().trim().isEmpty())
    varElem.setAttribute("dependsOn", axis.getDependsOn().trim());

  // attributes
  for (Attribute att : axis.getAttributes()) {
    varElem.addContent(ncmlWriter.makeAttributeElement(att));
  }

  /*
   * f.format("%s  npts: %d [%f,%f] spacing=%s", indent, ncoords, startValue, endValue, spacing);
   * if (getResolution() != 0.0)
   * f.format(" resolution=%f", resolution);
   * f.format(" %s :", getDependenceType());
   * for (String s : dependsOn)
   */

  Element values = new Element("values");
  if (!axis.isRegular()) {
    Array array = axis.getCoordsAsArray();
    boolean isRealType = (array.getDataType() == DataType.DOUBLE) || (array.getDataType() == DataType.FLOAT);
    IndexIterator iter = array.getIndexIterator();

    StringBuilder buff = new StringBuilder();
    buff.append(isRealType ? iter.getDoubleNext() : iter.getIntNext());

    while (iter.hasNext()) {
      buff.append(" ");
      buff.append(isRealType ? iter.getDoubleNext() : iter.getIntNext());
    }

    values.setText(buff.toString());
  }

  values.setAttribute("spacing", axis.getSpacing().toString());
  values.setAttribute("npts", Long.toString(axis.getNcoords()));
  values.setAttribute("start", Double.toString(axis.getStartValue()));
  values.setAttribute("end", Double.toString(axis.getEndValue()));
  if (axis.getResolution() != 0.0)
    values.setAttribute("resolution", Double.toString(axis.getResolution()));

  varElem.addContent(values);
  return varElem;
}