Java Code Examples for ucar.ma2.DataType

The following examples show how to use ucar.ma2.DataType. These examples are extracted from open source projects. 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
ProtobufPointFeatureMaker(PointStreamProto.PointFeatureCollection pfc) {
  try {
    // LOOK No calendar
    dateUnit = CalendarDateUnit.of(null, pfc.getTimeUnit());
  } catch (Exception e) {
    e.printStackTrace();
    dateUnit = CalendarDateUnit.unixDateUnit;
  }

  StructureMembers.Builder builder = StructureMembers.builder().setName(pfc.getName());
  for (PointStreamProto.PointFeatureMember m : pfc.getMembersList()) {
    String name = m.getName();
    String desc = !m.getDesc().isEmpty() ? m.getDesc() : null;
    String units = !m.getUnits().isEmpty() ? m.getUnits() : null;
    DataType dtype = NcStream.convertDataType(m.getDataType());
    int[] shape = NcStream.decodeSection(m.getSection()).getShape();

    builder.addMember(name, desc, units, dtype, shape);
  }
  sm = builder.build();
  ArrayStructureBB.setOffsets(sm);
}
 
Example 2
Source Project: tds   Source File: NcDDS.java    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private BaseType createVariable(NetcdfFile ncfile, Variable v) {
  BaseType bt;
  if (v.getRank() == 0) // scalar
    bt = createScalarVariable(ncfile, v);

  else if (v.getDataType() == DataType.CHAR) {
    if (v.getRank() > 1)
      bt = new NcSDCharArray(v);
    else
      bt = new NcSDString(v);

  } else if (v.getDataType() == DataType.STRING) {
    if (v.getRank() == 0)
      bt = new NcSDString(v);
    else
      bt = new NcSDArray(v, new NcSDString(v));

  } else // non-char multidim array
    bt = createArray(ncfile, v);

  return bt;

}
 
Example 3
private boolean addTimeCoordinate() {
  // add time coordinate by parsing the filename, of course.
  CalendarDate cd = parseFilenameForDate(datasetBuilder.orgFile.getLocation());
  if (cd == null) {
    return false;
  }

  rootGroup.addAttribute(new Attribute("_MODIS_Date", cd.toString()));

  // add the time dimension
  rootGroup.addDimension(new Dimension(TIME_NAME, 1));

  // add the coordinate variable
  String units = "seconds since " + cd;
  CoordinateAxis.Builder timeCoord = CoordinateAxis1D.builder().setName(TIME_NAME).setDataType(DataType.DOUBLE)
      .setParentGroupBuilder(rootGroup).setDimensionsByName("").setUnits(units).setDesc("time coordinate");
  timeCoord.setAutoGen(0, 0);
  timeCoord.addAttribute(new Attribute(_Coordinate.AxisType, AxisType.Time.toString()));
  datasetBuilder.replaceCoordinateAxis(rootGroup, timeCoord);

  return true;
}
 
Example 4
/**
 * Add coordinate system variable
 *
 * @param ncfile netCDF file
 * @param name name of the variable
 * @param dims dimensions
 */
private void addCoordSystemVariable(NetcdfFile ncfile, String name, String dims) {
  Variable v = new Variable(ncfile, g, null, name);
  v.setDataType(DataType.CHAR);
  v.setDimensions(""); // scalar
  Array dataArray = Array.factory(DataType.CHAR, new int[0], new char[] {'0'});
  v.setCachedData(dataArray, false);
  v.addAttribute(new Attribute(_Coordinate.Axes, dims));
  if (isLatLon())
    v.addAttribute(new Attribute(_Coordinate.Transforms, "")); // to make sure its identified as a Coordinate System
                                                               // Variable
  else
    v.addAttribute(new Attribute(_Coordinate.Transforms, getGridName()));
  addGDSparams(v);
  ncfile.addVariable(g, v);
}
 
Example 5
public static void main(String args[]) throws IOException {

    List<PointObVar> dataVars = new ArrayList<PointObVar>();
    dataVars.add(new PointObVar("test1", "units1", "desc1", DataType.CHAR, 4));
    dataVars.add(new PointObVar("test2", "units2", "desc3", DataType.CHAR, 4));

    // public CFPointObWriter(DataOutputStream stream, List<Attribute> globalAtts, String altUnits, List<PointObVar>
    // dataVars) throws IOException {

    FileOutputStream fos = new FileOutputStream("C:/temp/test.out");
    DataOutputStream out = new DataOutputStream(new BufferedOutputStream(fos, 10000));

    CFPointObWriter writer = new CFPointObWriter(out, new ArrayList<Attribute>(), "meters", dataVars, 1);

    double[] dvals = new double[0];
    String[] svals = new String[] {"valu", "value"};
    writer.addPoint(1.0, 2.0, 3.0, new Date(), dvals, svals);
    writer.finish();

  }
 
Example 6
public VariableSimpleImpl(String name, String desc, String units, DataType dt, List<Dimension> dims) {
  this.name = name;
  this.desc = desc;
  this.units = units;
  this.dt = dt;

  if (dims == null) {
    this.dims = new ArrayList<>();
    this.shape = new int[0];
  } else {
    this.dims = dims;
    this.shape = new int[dims.size()];
    int count = 0;
    for (Dimension d : dims)
      this.shape[count++] = d.getLength();
  }

  if (units != null)
    atts.add(new Attribute(CDM.UNITS, units));
  if (desc != null)
    atts.add(new Attribute(CDM.LONG_NAME, desc));
}
 
Example 7
private void enhanceVariable(VariableDS.Builder vb) {
  Set<Enhance> varEnhance = EnumSet.copyOf(wantEnhance);

  // varEnhance will only contain enhancements not already applied to orgVar.
  if (vb.orgVar instanceof VariableDS) {
    for (Enhance orgVarEnhancement : ((VariableDS) vb.orgVar).getEnhanceMode()) {
      varEnhance.remove(orgVarEnhancement);
    }
  }

  // enhance() may have been called previously, with a different enhancement set.
  // So, we need to reset to default before we process this new set.
  if (vb.orgDataType != null) {
    vb.setDataType(vb.orgDataType);
  }

  if (varEnhance.contains(Enhance.ConvertEnums) && vb.dataType.isEnum()) {
    vb.setDataType(DataType.STRING);
  }

  vb.addEnhanceMode(varEnhance);
}
 
Example 8
Source Project: tds   Source File: OpendapServlet.java    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
long computeArraySize(SDArray da) throws Exception {
  assert (da.getContainerVar() instanceof DPrimitive);
  BaseType base = da.getPrimitiveVector().getTemplate();
  DataType dtype = DODSNetcdfFile.convertToNCType(base, false);
  int elemSize = dtype.getSize();
  int n = da.numDimensions();
  List<Range> ranges = new ArrayList<>(n);
  long size = 0;
  for (int i = 0; i < n; i++) {
    ranges.add(new Range(da.getStart(i), da.getStop(i), da.getStride(i)));
  }
  Section s = new Section(ranges);
  size += s.computeSize() * elemSize;

  return size;
}
 
Example 9
private void writeRecord(String stnName, double timeCoordValue, CalendarDate obsDate, StructureData sdata)
    throws IOException {
  trackBB(null, obsDate);

  Integer parentIndex = stationIndexMap.get(stnName);
  if (parentIndex == null)
    throw new RuntimeException("Cant find station " + stnName);

  StructureMembers.Builder smb = StructureMembers.builder().setName("Coords");
  smb.addMemberScalar(timeName, null, null, DataType.DOUBLE, timeCoordValue);
  smb.addMemberScalar(stationIndexName, null, null, DataType.INT, parentIndex);
  StructureData coords = new StructureDataFromMember(smb.build());

  // coords first so it takes precedence
  StructureDataComposite sdall = StructureDataComposite.create(ImmutableList.of(coords, sdata));
  obsRecno = super.writeStructureData(obsRecno, record, sdall, dataMap);
}
 
Example 10
public CoverageCoordAxisBuilder(String name, String units, String description, DataType dataType, AxisType axisType,
    AttributeContainer atts, CoverageCoordAxis.DependenceType dependenceType, String dependsOnS,
    CoverageCoordAxis.Spacing spacing, int ncoords, double startValue, double endValue, double resolution,
    double[] values, CoordAxisReader reader) {
  this.name = name;
  this.units = units;
  this.description = description;
  this.dataType = dataType;
  this.axisType = axisType;
  this.attributes = AttributeContainerMutable.copyFrom(atts);
  this.dependenceType = dependenceType;
  this.setDependsOn(dependsOnS);
  this.spacing = spacing;
  this.ncoords = ncoords;
  this.startValue = startValue;
  this.endValue = endValue;
  this.resolution = resolution;
  this.values = values;
  this.reader = reader;
}
 
Example 11
@Test
public void testUnsignedWrap() throws IOException {
  String ncml = "<?xml version='1.0' encoding='UTF-8'?>\n"
      + "<netcdf xmlns='http://www.unidata.ucar.edu/namespaces/netcdf/ncml-2.2' location='"
      + TestDir.cdmLocalTestDataDir + "testWrite.nc'>\n" + "  <variable name='bvar' shape='lat' type='byte'>\n"
      + "    <attribute name='_Unsigned' value='true' />\n"
      + "    <attribute name='scale_factor' type='float' value='2.0' />\n" + "   </variable>\n" + "</netcdf>";

  try (NetcdfFile ncfile = NcMLReader.readNcML(new StringReader(ncml), null);
      NetcdfFile ncd = NetcdfDataset.wrap(ncfile, NetcdfDataset.getEnhanceAll())) {

    Variable v = ncd.findVariable("bvar");
    Assert.assertNotNull(v);
    Assert.assertEquals(DataType.FLOAT, v.getDataType());

    boolean hasSigned = false;
    Array data = v.read();
    while (data.hasNext()) {
      float b = data.nextFloat();
      if (b < 0)
        hasSigned = true;
    }
    Assert.assertTrue(!hasSigned);
  }
}
 
Example 12
@Override
protected Array read(AggDatasetOuter dset, NetcdfFile ncfile) {
  Array data = getData(dset.getId());
  if (data != null)
    return data;

  Attribute att = ncfile.findGlobalAttribute(gattName);
  if (att == null)
    throw new IllegalArgumentException("Unknown attribute name= " + gattName);
  data = att.getValues();
  if (dtype == null)
    dtype = DataType.getType(data);

  if (dset.ncoord == 1) // LOOK ??
    putData(dset.getId(), data);
  else {
    // duplicate the value to each of the coordinates
    Array allData = Array.factory(dtype, new int[] {dset.ncoord});
    for (int i = 0; i < dset.ncoord; i++)
      Array.arraycopy(data, 0, allData, i, 1); // LOOK generalize to vectors ??
    putData(dset.getId(), allData);
    data = allData;
  }
  return data;
}
 
Example 13
public GeoReferencedArray decodeGeoReferencedArray(CoverageDataResponse dataResponse,
    CdmrFeatureProto.GeoReferencedArray parray) {
  DataType dataType = NcStream.convertDataType(parray.getDataType());
  ByteOrder byteOrder = parray.getBigend() ? ByteOrder.BIG_ENDIAN : ByteOrder.LITTLE_ENDIAN;
  boolean deflate = parray.getCompress() == NcStreamProto.Compress.DEFLATE;
  long uncompressedSize = parray.getUncompressedSize();

  int[] shape = new int[parray.getShapeCount()];
  for (int i = 0; i < parray.getShapeCount(); i++)
    shape[i] = parray.getShape(i);

  ByteBuffer bb = parray.getPrimdata().asReadOnlyByteBuffer();
  bb.order(byteOrder);
  Array data = Array.factory(dataType, shape, bb);

  CoverageCoordSys csys = dataResponse.findCoordSys(parray.getCoordSysName());
  if (csys == null)
    throw new IllegalStateException("Misformed response - no coordsys");

  return new GeoReferencedArray(parray.getCoverageName(), dataType, data, csys);
}
 
Example 14
@Test
public void testAttWithUnsignedType() throws IOException {
  String ncml = "<?xml version='1.0' encoding='UTF-8'?>\n"
      + "<netcdf xmlns='http://www.unidata.ucar.edu/namespaces/netcdf/ncml-2.2' location='"
      + TestDir.cdmLocalTestDataDir + "testWrite.nc'>\n" + "  <attribute name='gatt' type='ubyte'>1 0 -1</attribute>"
      + "</netcdf>";

  try (NetcdfFile ncfile = NcMLReader.readNcML(new StringReader(ncml), null);
      NetcdfFile ncd = NetcdfDataset.wrap(ncfile, NetcdfDataset.getEnhanceAll())) {

    Attribute att = ncd.findGlobalAttribute("gatt");
    Assert.assertNotNull(att);
    Assert.assertEquals(DataType.UBYTE, att.getDataType());

    Assert.assertEquals(3, att.getLength());
    Array gattValues = att.getValues();

    boolean hasSigned = false;
    while (gattValues.hasNext()) {
      short b = gattValues.nextShort();
      if (b < 0)
        hasSigned = true;
    }
    Assert.assertTrue(!hasSigned);
  }
}
 
Example 15
Source Project: netcdf-java   Source File: H4header.java    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private void addGlobalAttributes(TagVGroup group) throws IOException {
  // look for attributes
  for (int i = 0; i < group.nelems; i++) {
    Tag tag = tagMap.get(tagid(group.elem_ref[i], group.elem_tag[i]));
    if (tag == null)
      throw new IllegalStateException();
    if (tag.code == 1962) {
      TagVH vh = (TagVH) tag;
      if (vh.className.startsWith("Att")) {
        String lowername = vh.name.toLowerCase();
        if ((vh.nfields == 1) && (H4type.setDataType(vh.fld_type[0], null) == DataType.CHAR)
            && ((vh.fld_isize[0] > 4000) || lowername.startsWith("archivemetadata")
                || lowername.startsWith("coremetadata") || lowername.startsWith("productmetadata")
                || lowername.startsWith("structmetadata"))) {
          root.addVariable(makeVariable(vh)); // // large EOS metadata - make into variable in root group
        } else {
          Attribute att = makeAttribute(vh);
          if (null != att)
            root.addAttribute(att); // make into attribute in root group
        }
      }
    }
  }

  group.used = true;
}
 
Example 16
public static Array getArrayFromMember(Variable var, Member m) {

    // DataType m_dt = writer.findVariable(m.getName()).getDataType();
    DataType v_dt = var.getDataType();
    // DataType m_dt = m.getDataType();

    // Writes one single data
    // int[] shape = writer.findVariable(m.getName()).getShape();
    int[] shape = var.getShape();

    // Set the shape we really want
    for (int i = 0; i < shape.length; i++) {
      shape[i] = 1;
    }

    Array arr = Array.factory(v_dt, shape);
    setDataArray(v_dt, arr, m);

    return arr;

  }
 
Example 17
void makeFeatureVariables(StructureData stnData, boolean isExtended) {
  // add the dimensions : extended model can use an unlimited dimension
  Dimension stationDim = writerb.addDimension(stationDimName, stnList.size());

  List<VariableSimpleIF> stnVars = new ArrayList<>();
  stnVars.add(VariableSimpleBuilder.makeScalar(latName, "station latitude", CDM.LAT_UNITS, DataType.DOUBLE).build());
  stnVars.add(VariableSimpleBuilder.makeScalar(lonName, "station longitude", CDM.LON_UNITS, DataType.DOUBLE).build());

  if (useAlt) {
    stnVars.add(VariableSimpleBuilder.makeScalar(stationAltName, "station altitude", altUnits, DataType.DOUBLE)
        .addAttribute(CF.STANDARD_NAME, CF.SURFACE_ALTITUDE)
        .addAttribute(CF.POSITIVE, CF1Convention.getZisPositive(altName, altUnits)).build());
  }

  stnVars.add(VariableSimpleBuilder.makeString(stationIdName, "station identifier", null, id_strlen)
      .addAttribute(CF.CF_ROLE, CF.TIMESERIES_ID).build()); // station_id:cf_role = "timeseries_id";

  if (useDesc)
    stnVars.add(VariableSimpleBuilder.makeString(descName, "station description", null, desc_strlen)
        .addAttribute(CF.STANDARD_NAME, CF.PLATFORM_NAME).build());

  if (useWmoId)
    stnVars.add(VariableSimpleBuilder.makeString(wmoName, "station WMO id", null, wmo_strlen)
        .addAttribute(CF.STANDARD_NAME, CF.PLATFORM_ID).build());

  for (StructureMembers.Member m : stnData.getMembers()) {
    if (findDataVar(m.getName()) != null)
      stnVars.add(VariableSimpleBuilder.fromMember(m).build());
  }

  if (isExtended) {
    Structure.Builder structb = writerb.addStructure(stationStructName, stationDimName);
    addCoordinatesExtended(structb, stnVars);
  } else {
    addCoordinatesClassic(stationDim, stnVars, stationVarMap);
  }

}
 
Example 18
Source Project: netcdf-java   Source File: Misc.java    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static String showBits(byte[] bytes) {
  try (Formatter f = new Formatter()) {
    int count = 0;
    for (byte b : bytes) {
      short s = DataType.unsignedByteToShort(b);
      f.format("%8s", Long.toBinaryString(s));
      if (++count % 10 == 0)
        f.format("%n");
    }
    return f.toString();
  }
}
 
Example 19
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 20
/** Normally this is called by Group.build() */
public SequenceDS build(Group parentGroup) {
  if (built)
    throw new IllegalStateException("already built");
  built = true;
  this.setDataType(DataType.SEQUENCE);
  return new SequenceDS(this, parentGroup);
}
 
Example 21
private CoordinateAxis.Builder makeLatLonCoordAxis(Group.Builder dataG, int n, double start, double end,
    boolean isLon) {
  String name = isLon ? AxisType.Lon.toString() : AxisType.Lat.toString();
  String dimName = isLon ? DIMX_NAME : DIMY_NAME;

  CoordinateAxis.Builder v = CoordinateAxis1D.builder().setName(name).setDataType(DataType.DOUBLE)
      .setParentGroupBuilder(dataG).setDimensionsByName(dimName).setUnits(isLon ? "degrees_east" : "degrees_north");

  double incr = (end - start) / n;
  v.setAutoGen(start, incr);
  v.addAttribute(new Attribute(_Coordinate.AxisType, name));
  return v;
}
 
Example 22
/** Add a Variable to the root group. */
public Variable.Builder addVariable(String shortName, DataType dataType, String dimString) {
  if (!isNewFile && !useJna) {
    throw new UnsupportedOperationException("Cant add variable to existing netcdf-3 files");
  }
  Variable.Builder vb = Variable.builder().setName(shortName).setDataType(dataType).setParentGroupBuilder(rootGroup)
      .setDimensionsByName(dimString);
  rootGroup.addVariable(vb);
  return vb;
}
 
Example 23
public static Array arrayify(DataType datatype, Object o) {
  // 1. o is a constant
  if (!o.getClass().isArray()) {
    Object ovec = createVector(datatype, 1);
    java.lang.reflect.Array.set(ovec, 0, o);
    o = ovec;
  }
  int[] shape = new int[] {java.lang.reflect.Array.getLength(o)};
  return Array.factory(datatype, shape, o);
}
 
Example 24
/** Make an EnumTypedef setting the base type (must be ENUM1, ENUM2, ENUM4). */
public EnumTypedef(String name, Map<Integer, String> map, DataType basetype) {
  super(name);
  Preconditions.checkArgument(validateMap(map, basetype));
  this.map = ImmutableMap.copyOf(map);

  enumStrings = ImmutableList.sortedCopyOf(map.values());

  assert basetype == DataType.ENUM1 || basetype == DataType.ENUM2 || basetype == DataType.ENUM4;
  this.basetype = basetype;
}
 
Example 25
Source Project: netcdf-java   Source File: CDMDSP.java    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Walk this variable, including fields, to construct sequence types
 * for any contained vlen dimensions
 *
 * @param cdmvar variable to walk
 */

protected void buildseqtypes(Variable cdmvar) throws DapException {
  if (CDMUtil.hasVLEN(cdmvar)) {
    buildseqtype(cdmvar);
  }
  if (cdmvar.getDataType() == DataType.STRUCTURE || cdmvar.getDataType() == DataType.SEQUENCE) {
    Structure struct = (Structure) cdmvar;
    List<Variable> fields = struct.getVariables();
    for (int i = 0; i < fields.size(); i++) {
      Variable field = fields.get(i);
      buildseqtypes(field); // recurse for inner vlen dims
    }
  }
}
 
Example 26
private float[] getData(ucar.unidata.io.RandomAccessFile raf, Grib1Record gr, GribData.InterpolationMethod method,
    int lineno) throws IOException {
  float[] data;
  data = gr.readData(raf, method);

  System.out.printf("%s%n", method);
  Grib1Gds gds = gr.getGDS();
  if (method == GribData.InterpolationMethod.none) {
    int[] lines = gds.getNptsInLine();
    int count = 0;
    for (int line = 0; line < lines.length; line++) {
      if (line != lineno)
        continue;
      System.out.printf(" %3d: ", line);
      for (int i = 0; i < lines[line]; i++)
        System.out.printf("%f,", data[count++]);
      System.out.printf("%n");
    }

  } else {
    int[] shape = new int[] {gds.getNy(), gds.getNx()};
    Array dataA = Array.factory(DataType.FLOAT, shape, data);
    Array lineA = dataA.slice(0, lineno);
    logger.debug("{}", Ncdump.printArray(lineA));
  }
  System.out.printf("%s%n", method);

  return data;
}
 
Example 27
private void writeObsData(PointFeature pf) throws IOException {
  StructureMembers.Builder smb = StructureMembers.builder().setName("Coords");
  if (useAlt)
    smb.addMemberScalar(altitudeCoordinateName, null, null, DataType.DOUBLE, pf.getLocation().getAltitude());
  StructureData coords = new StructureDataFromMember(smb.build());

  // coords first so it takes precedence
  StructureDataComposite sdall = StructureDataComposite.create(ImmutableList.of(coords, pf.getFeatureData()));
  obsRecno = super.writeStructureData(obsRecno, record, sdall, dataMap);
}
 
Example 28
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 29
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 30
protected double getTime(Variable timeVar, StructureData sdata) throws ParseException {
  if (timeVar == null)
    return 0.0;

  if ((timeVar.getDataType() == DataType.CHAR) || (timeVar.getDataType() == DataType.STRING)) {
    String time = sdata.getScalarString(timeVar.getShortName());
    if (null == formatter)
      formatter = new DateFormatter();
    Date date = formatter.getISODate(time);
    return date.getTime() / 1000.0;
  } else {
    return sdata.convertScalarFloat(timeVar.getShortName());
  }
}