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

The following examples show how to use ucar.ma2.Array#getElementType() . 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: NetcdfFormatWriter.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Write String data to a CHAR variable.
 *
 * @param v variable to write to
 * @param origin offset to start writing, ignore the strlen dimension.
 * @param values write this array; must be ArrayObject of String
 * @throws IOException if I/O error
 * @throws InvalidRangeException if values Array has illegal shape
 */
public void writeStringDataToChar(Variable v, int[] origin, Array values) throws IOException, InvalidRangeException {
  if (values.getElementType() != String.class)
    throw new IllegalArgumentException("values must be an ArrayObject of String ");

  if (v.getDataType() != DataType.CHAR)
    throw new IllegalArgumentException("variable " + v.getFullName() + " is not type CHAR");

  int rank = v.getRank();
  int strlen = v.getShape(rank - 1);

  // turn it into an ArrayChar
  ArrayChar cvalues = ArrayChar.makeFromStringArray((ArrayObject) values, strlen);

  int[] corigin = new int[rank];
  System.arraycopy(origin, 0, corigin, 0, rank - 1);

  write(v, corigin, cvalues);
}
 
Example 2
Source File: CDMDSP.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
protected DapAttribute buildattribute(Attribute cdmattr) throws DapException {
  DapType attrtype = CDMTypeFcns.cdmtype2daptype(cdmattr.getDataType());
  EnumTypedef cdmenum = cdmattr.getEnumType();
  boolean enumfillvalue = (cdmattr.getShortName().equals(FILLVALUE) && cdmenum != null);
  DapEnumeration dapenum = null;

  // We need to handle _FillValue specially if the
  // the variable is enum typed.
  if (enumfillvalue) {
    cdmenum = findMatchingEnum(cdmenum);
    // Make sure the cdm attribute has type enumx
    if (!cdmenum.getBaseType().isEnum())
      throw new DapException("CDM _FillValue attribute type is not enumX");
    // Modify the attr
    cdmattr.setEnumType(cdmenum);
    // Now, map to a DapEnumeration
    dapenum = (DapEnumeration) this.nodemap.get(cdmenum);
    if (dapenum == null)
      throw new DapException("Illegal CDM variable attribute type: " + cdmenum);
    attrtype = dapenum;
  }
  if (attrtype == null)
    throw new DapException("DapFile: illegal CDM variable attribute type: " + cdmattr.getDataType());
  DapAttribute dapattr = (DapAttribute) dmrfactory.newAttribute(cdmattr.getShortName(), attrtype);
  recordNode(cdmattr, dapattr);
  // Transfer the values
  Array values = cdmattr.getValues();
  if (!validatecdmtype(cdmattr.getDataType(), values.getElementType()))
    throw new DapException("Attr type versus attribute data mismatch: " + values.getElementType());
  IndexIterator iter = values.getIndexIterator();
  String[] valuelist = null;
  Object vec = CDMTypeFcns.createVector(cdmattr.getDataType(), values.getSize());
  for (int i = 0; iter.hasNext(); i++) {
    java.lang.reflect.Array.set(vec, i, iter.next());
  }
  valuelist = (String[]) Convert.convert(DapType.STRING, attrtype, vec);
  dapattr.setValues(valuelist);
  return dapattr;
}
 
Example 3
Source File: StationSubsetWriterXML.java    From tds with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
protected void writeStationPointFeature(StationPointFeature stationPointFeat) throws XMLStreamException, IOException {
  Station station = stationPointFeat.getStation();

  staxWriter.writeCharacters("\n    ");
  staxWriter.writeStartElement("stationFeature");
  staxWriter.writeAttribute("date",
      CalendarDateFormatter.toDateTimeStringISO(stationPointFeat.getObservationTimeAsCalendarDate()));

  staxWriter.writeCharacters("\n        ");
  staxWriter.writeStartElement("station");
  staxWriter.writeAttribute("name", station.getName());
  staxWriter.writeAttribute("latitude", Format.dfrac(station.getLatitude(), 3));
  staxWriter.writeAttribute("longitude", Format.dfrac(station.getLongitude(), 3));
  if (!Double.isNaN(station.getAltitude()))
    staxWriter.writeAttribute("altitude", Format.dfrac(station.getAltitude(), 0));
  if (station.getDescription() != null)
    staxWriter.writeCharacters(station.getDescription());
  staxWriter.writeEndElement();

  for (VariableSimpleIF wantedVar : wantedVariables) {
    staxWriter.writeCharacters("\n        ");
    staxWriter.writeStartElement("data");
    staxWriter.writeAttribute("name", wantedVar.getShortName());
    if (wantedVar.getUnitsString() != null)
      staxWriter.writeAttribute(CDM.UNITS, wantedVar.getUnitsString());

    Array dataArray = stationPointFeat.getDataAll().getArray(wantedVar.getShortName());
    String ss = dataArray.toString();
    Class elemType = dataArray.getElementType();
    if ((elemType == String.class) || (elemType == char.class) || (elemType == StructureData.class))
      ss = ucar.nc2.util.xml.Parse.cleanCharacterData(ss); // make sure no bad chars
    staxWriter.writeCharacters(ss.trim());
    staxWriter.writeEndElement();
  }

  staxWriter.writeCharacters("\n    ");
  staxWriter.writeEndElement();
}
 
Example 4
Source File: PointSubsetWriterXML.java    From tds with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void writePoint(PointFeature pointFeat) throws XMLStreamException, IOException {
  EarthLocation loc = pointFeat.getLocation();

  staxWriter.writeCharacters("\n    ");
  staxWriter.writeStartElement("pointFeature");
  staxWriter.writeAttribute("date",
      CalendarDateFormatter.toDateTimeStringISO(pointFeat.getObservationTimeAsCalendarDate()));

  staxWriter.writeCharacters("\n        ");
  staxWriter.writeEmptyElement("location");
  staxWriter.writeAttribute("latitude", Format.dfrac(loc.getLatitude(), 3));
  staxWriter.writeAttribute("longitude", Format.dfrac(loc.getLongitude(), 3));
  if (!Double.isNaN(loc.getAltitude()))
    staxWriter.writeAttribute("altitude", Format.dfrac(loc.getAltitude(), 0));

  StructureData structureData = pointFeat.getDataAll();
  for (VariableSimpleIF wantedVar : wantedVariables) {
    staxWriter.writeCharacters("\n        ");
    staxWriter.writeStartElement("data");
    staxWriter.writeAttribute("name", wantedVar.getShortName());
    if (wantedVar.getUnitsString() != null)
      staxWriter.writeAttribute(CDM.UNITS, wantedVar.getUnitsString());

    Array dataArray = structureData.getArray(wantedVar.getShortName());
    String ss = dataArray.toString();
    Class elemType = dataArray.getElementType();
    if ((elemType == String.class) || (elemType == char.class) || (elemType == StructureData.class))
      ss = ucar.nc2.util.xml.Parse.cleanCharacterData(ss); // make sure no bad chars
    staxWriter.writeCharacters(ss.trim());
    staxWriter.writeEndElement();
  }

  staxWriter.writeCharacters("\n    ");
  staxWriter.writeEndElement();
}
 
Example 5
Source File: Ncdump.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private static void printArray(Formatter out, Array array, String name, String units, Indent ilev, CancelTask ct,
    boolean printSeq) {
  if (ct != null && ct.isCancel())
    return;

  if (name != null)
    out.format("%s%s = ", ilev, name);
  ilev.incr();

  if (array == null) {
    out.format("null array for %s", name);
    ilev.decr();
    return;
  }

  if ((array instanceof ArrayChar) && (array.getRank() > 0)) {
    printStringArray(out, (ArrayChar) array, ilev, ct);

  } else if (array.getElementType() == String.class) {
    printStringArray(out, array, ilev, ct);

  } else if (array instanceof ArraySequence) {
    if (printSeq)
      printSequence(out, (ArraySequence) array, ilev, ct);

  } else if (array instanceof ArrayStructure) {
    printStructureDataArray(out, (ArrayStructure) array, ilev, ct);

  } else if (array.getElementType() == ByteBuffer.class) { // opaque type
    array.resetLocalIterator();
    while (array.hasNext()) {
      printByteBuffer(out, (ByteBuffer) array.next(), ilev);
      out.format("%s%n", array.hasNext() ? "," : ";"); // peek ahead
      if (ct != null && ct.isCancel())
        return;
    }
  } else if (array instanceof ArrayObject) {
    printVariableArray(out, (ArrayObject) array, ilev, ct);
  } else {
    printArray(out, array, ilev, ct);
  }

  if (units != null)
    out.format(" %s", units);
  out.format("%n");
  ilev.decr();
  out.flush();
}