Java Code Examples for ucar.ma2.Section#getRange()

The following examples show how to use ucar.ma2.Section#getRange() . 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: GradsBinaryGridServiceProvider.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Read the data for the variable
 *
 * @param v2 Variable to read
 * @param section section infomation
 * @return Array of data
 * @throws IOException problem reading from file
 * @throws InvalidRangeException invalid Range
 */
public Array readData(Variable v2, Section section) throws IOException, InvalidRangeException {

  Array dataArray = Array.factory(DataType.FLOAT, section.getShape());
  GradsVariable gradsVar = findVar(v2);
  if (gradsVar == null)
    throw new IOException();

  // Canonical ordering is ens, time, level, lat, lon
  int rangeIdx = 0;
  Range ensRange = (gradsDDF.getEnsembleDimension() != null) ? section.getRange(rangeIdx++) : new Range(0, 0);
  Range timeRange = (section.getRank() > 2) ? section.getRange(rangeIdx++) : new Range(0, 0);
  Range levRange = (gradsVar.getNumLevels() > 0) ? section.getRange(rangeIdx++) : new Range(0, 0);
  Range yRange = section.getRange(rangeIdx++);
  Range xRange = section.getRange(rangeIdx);

  IndexIterator ii = dataArray.getIndexIterator();

  for (int ensIdx : ensRange)
    for (int timeIdx : timeRange)
      for (int levelIdx : levRange)
        readXY(v2, ensIdx, timeIdx, levelIdx, yRange, xRange, ii);

  return dataArray;
}
 
Example 2
Source File: N3iospWriter.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void writeData(Variable v2, Section section, Array values) throws java.io.IOException, InvalidRangeException {
  N3headerNew.Vinfo vinfo = (N3headerNew.Vinfo) v2.getSPobject();
  DataType dataType = v2.getDataType();

  if (v2.isUnlimited()) {
    Range firstRange = section.getRange(0);
    setNumrecs(firstRange.last() + 1);
  }

  if (v2 instanceof Structure) {
    if (!(values instanceof ArrayStructure))
      throw new IllegalArgumentException("writeData for Structure: data must be ArrayStructure");

    if (v2.getRank() == 0)
      throw new IllegalArgumentException("writeData for Structure: must have rank > 0");

    Dimension d = v2.getDimension(0);
    if (!d.isUnlimited())
      throw new IllegalArgumentException("writeData for Structure: must have unlimited dimension");

    writeRecordData((Structure) v2, section, (ArrayStructure) values);

  } else {
    Layout layout = (!v2.isUnlimited()) ? new LayoutRegular(vinfo.begin, v2.getElementSize(), v2.getShape(), section)
        : new LayoutRegularSegmented(vinfo.begin, v2.getElementSize(), header.recsize, v2.getShape(), section);
    writeData(values, layout, dataType);
  }
}
 
Example 3
Source File: N3iospWriter.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void writeRecordData(ucar.nc2.Structure s, Section section, ArrayStructure structureArray)
    throws java.io.IOException, ucar.ma2.InvalidRangeException {
  int countSrcRecnum = 0;
  Range recordRange = section.getRange(0);
  for (int recnum : recordRange) {
    StructureData sdata = structureArray.getStructureData(countSrcRecnum);
    writeRecordData(s, recnum, sdata);
    countSrcRecnum++;
  }
}
 
Example 4
Source File: N3iospNew.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private long readRecordData(ucar.nc2.Structure s, Section section, WritableByteChannel out)
    throws java.io.IOException {
  long count = 0;

  /*
   * RegularIndexer index = new RegularIndexer( s.getShape(), recsize, recStart, section, recsize);
   * while (index.hasNext()) {
   * Indexer.Chunk chunk = index.next();
   * count += raf.readBytes( out, chunk.getFilePos(), chunk.getNelems() * s.getElementSize());
   * }
   */

  // LOOK this is the OTW layout based on netcdf-3
  // not sure this works but should give an idea of timing
  Range recordRange = section.getRange(0);
  /*
   * int stride = recordRange.stride();
   * if (stride == 1) {
   * int first = recordRange.first();
   * int n = recordRange.length();
   * if (false) System.out.println(" read record " + first+" "+ n * header.recsize+" bytes ");
   * return raf.readToByteChannel(out, header.recStart + first * header.recsize, n * header.recsize);
   *
   * } else {
   */

  for (int recnum : recordRange) {
    if (debugRecord)
      System.out.println(" read record " + recnum);
    raf.seek(header.recStart + recnum * header.recsize); // where the record starts
    count += raf.readToByteChannel(out, header.recStart + recnum * header.recsize, header.recsize);
  }
  // }

  return count;
}
 
Example 5
Source File: N3iospNew.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Read data from record structure. For N3, this is the only possible structure, and there can be no nesting.
 * Read all variables for each record, put in ByteBuffer.
 *
 * @param s the record structure
 * @param section the record range to read
 * @return an ArrayStructure, with all the data read in.
 * @throws IOException on error
 */
private ucar.ma2.Array readRecordData(ucar.nc2.Structure s, Section section) throws java.io.IOException {
  // has to be 1D
  Range recordRange = section.getRange(0);

  // create the ArrayStructure
  StructureMembers members = s.makeStructureMembers();
  for (StructureMembers.Member m : members.getMembers()) {
    Variable v2 = s.findVariable(m.getName());
    Vinfo vinfo = (Vinfo) v2.getSPobject();
    m.setDataParam((int) (vinfo.begin - header.recStart));
  }

  // protect against too large of reads
  if (header.recsize > Integer.MAX_VALUE)
    throw new IllegalArgumentException("Cant read records when recsize > " + Integer.MAX_VALUE);
  long nrecs = section.computeSize();
  if (nrecs * header.recsize > Integer.MAX_VALUE)
    throw new IllegalArgumentException(
        "Too large read: nrecs * recsize= " + (nrecs * header.recsize) + "bytes exceeds " + Integer.MAX_VALUE);

  members.setStructureSize((int) header.recsize);
  ArrayStructureBB structureArray = new ArrayStructureBB(members, new int[] {recordRange.length()});

  // loop over records
  byte[] result = structureArray.getByteBuffer().array();
  int count = 0;
  for (int recnum : recordRange) {
    if (debugRecord)
      System.out.println(" read record " + recnum);
    raf.seek(header.recStart + recnum * header.recsize); // where the record starts

    if (recnum != header.numrecs - 1) {
      raf.readFully(result, (int) (count * header.recsize), (int) header.recsize);
    } else {
      // "wart" allows file to be one byte short. since its always padding, we allow
      raf.read(result, (int) (count * header.recsize), (int) header.recsize);
    }
    count++;
  }

  return structureArray;
}