Java Code Examples for ucar.ma2.Array#setObject()

The following examples show how to use ucar.ma2.Array#setObject() . 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: AggregationOuterDimension.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
protected Array read(DatasetOuterDimension dset, NetcdfFile ncfile) {
  Array data = getData(dset.getId());
  if (data != null)
    return data;

  List<Object> vals = new ArrayList<>();
  for (String gattName : gattNames) {
    Attribute att = ncfile.findGlobalAttribute(gattName);
    if (att == null)
      throw new IllegalArgumentException("Unknown attribute name= " + gattName);
    vals.add(att.getValue(0));
  }

  Formatter f = new Formatter();
  f.format(format, vals.toArray());
  String result = f.toString();

  Array allData = Array.factory(dtype, new int[] {dset.ncoord});
  for (int i = 0; i < dset.ncoord; i++)
    allData.setObject(i, result);
  putData(dset.getId(), allData);
  return allData;
}
 
Example 2
Source File: Attribute.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Create a scalar numeric-valued Attribute, possibly unsigned.
 *
 * @param name name of Attribute
 * @param val value of Attribute
 * @param isUnsigned if value is unsigned, used only for integer types.
 */
public Attribute(String name, Number val, boolean isUnsigned) {
  super(name);
  if (name == null)
    throw new IllegalArgumentException("Trying to set name to null on " + this);

  int[] shape = new int[1];
  shape[0] = 1;
  DataType dt = DataType.getType(val.getClass(), isUnsigned);
  this.dataType = dt;
  Array vala = Array.factory(dt, shape);
  Index ima = vala.getIndex();
  vala.setObject(ima.set0(0), val);
  setValues(vala); // make private
  setImmutable();
}
 
Example 3
Source File: AggregationOuter.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
protected Array read(AggDatasetOuter dset, NetcdfFile ncfile) {
  Array data = getData(dset.getId());
  if (data != null)
    return data;

  List<Object> vals = new ArrayList<>();
  for (String gattName : gattNames) {
    Attribute att = ncfile.findGlobalAttribute(gattName);
    if (att == null)
      throw new IllegalArgumentException("Unknown attribute name= " + gattName);
    vals.add(att.getValue(0));
  }

  Formatter f = new Formatter();
  f.format(format, vals.toArray());
  String result = f.toString();

  Array allData = Array.factory(dtype, new int[] {dset.ncoord});
  for (int i = 0; i < dset.ncoord; i++)
    allData.setObject(i, result);
  putData(dset.getId(), allData);
  return allData;
}
 
Example 4
Source File: NcStream.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static Attribute decodeAtt(NcStreamProto.Attribute attp) {
  // BARF LOOK
  DataType dtOld = decodeAttributeType(attp.getType());
  DataType dtNew = convertDataType(attp.getDataType());
  DataType dtUse;
  if (dtNew != DataType.CHAR)
    dtUse = dtNew;
  else if (dtOld != DataType.STRING)
    dtUse = dtOld;
  else if (attp.getSdataCount() > 0)
    dtUse = DataType.STRING;
  else
    dtUse = DataType.CHAR;

  int len = attp.getLen();
  if (len == 0) // deal with empty attribute
    return Attribute.builder(attp.getName()).setDataType(dtUse).build();

  if (dtUse == DataType.STRING) {
    int lenp = attp.getSdataCount();
    if (lenp != len)
      System.out.println("HEY lenp != len");
    if (lenp == 1)
      return new Attribute(attp.getName(), attp.getSdata(0));
    else {
      Array data = Array.factory(dtUse, new int[] {lenp});
      for (int i = 0; i < lenp; i++)
        data.setObject(i, attp.getSdata(i));
      return new Attribute(attp.getName(), data);
    }
  } else {
    ByteString bs = attp.getData();
    ByteBuffer bb = ByteBuffer.wrap(bs.toByteArray());
    // if null, then use int[]{bb.limit()}
    return new Attribute(attp.getName(), Array.factory(dtUse, (int[]) null, bb));
  }
}
 
Example 5
Source File: Attribute.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public Builder setNumericValue(Number val, boolean isUnsigned) {
  int[] shape = {1};
  DataType dt = DataType.getType(val.getClass(), isUnsigned);
  setDataType(dt);
  Array vala = Array.factory(dt, shape);
  Index ima = vala.getIndex();
  vala.setObject(ima.set0(0), val);
  setValues(vala);
  return this;
}