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

The following examples show how to use ucar.ma2.Section#getRanges() . 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: NcStream.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static NcStreamProto.Section encodeSection(Section section) {
  NcStreamProto.Section.Builder sbuilder = NcStreamProto.Section.newBuilder();
  for (Range r : section.getRanges()) {
    NcStreamProto.Range.Builder rbuilder = NcStreamProto.Range.newBuilder();
    rbuilder.setStart(r.first());
    rbuilder.setSize(r.length());
    rbuilder.setStride(r.stride());
    sbuilder.addRange(rbuilder);
  }
  return sbuilder.build();
}
 
Example 2
Source File: FmrcDataset.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public Array reallyRead(Variable mainv, Section section, CancelTask cancelTask)
    throws IOException, InvalidRangeException {
  FmrcInvLite.Gridset.Grid gridLite = (FmrcInvLite.Gridset.Grid) mainv.getSPobject();

  // read the original type - if its been promoted to a new type, the conversion happens after this read
  DataType dtype = (mainv instanceof VariableDS) ? ((VariableDS) mainv).getOriginalDataType() : mainv.getDataType();

  Array allData = Array.factory(dtype, section.getShape());
  int destPos = 0;

  // assumes the first two dimensions are runtime and time: LOOK: ensemble ??
  List<Range> ranges = section.getRanges();
  Range runRange = ranges.get(0);
  Range timeRange = ranges.get(1);
  List<Range> innerSection = ranges.subList(2, ranges.size());

  // keep track of open file - must be local variable for thread safety
  HashMap<String, NetcdfDataset> openFilesRead = new HashMap<>();
  try {

    // iterate over the desired runs
    for (int runIdx : runRange) {
      // Date runDate = vstate.runTimes.get(runIdx);

      // iterate over the desired forecast times
      for (int timeIdx : timeRange) {
        Array result = null;

        // find the inventory for this grid, runtime, and hour
        TimeInventory.Instance timeInv = gridLite.getInstance(runIdx, timeIdx);
        if (timeInv != null) {
          if (debugRead)
            System.out.printf("HIT %d %d ", runIdx, timeIdx);
          result = read(timeInv, gridLite.name, innerSection, openFilesRead); // may return null
          result = MAMath.convert(result, dtype); // just in case it need to be converted
        }

        // missing data
        if (result == null) {
          int[] shape = new Section(innerSection).getShape();
          result = ((VariableDS) mainv).getMissingDataArray(shape); // fill with missing values
          if (debugRead)
            System.out.printf("MISS %d %d ", runIdx, timeIdx);
        }

        if (debugRead)
          System.out.printf("%d %d reallyRead %s %d bytes start at %d total size is %d%n", runIdx, timeIdx,
              mainv.getFullName(), result.getSize(), destPos, allData.getSize());

        Array.arraycopy(result, 0, allData, destPos, (int) result.getSize());
        destPos += result.getSize();
      }
    }
    return allData;

  } finally {
    // close any files used during this operation
    closeAll(openFilesRead);
  }
}
 
Example 3
Source File: FmrcDataset.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public Array reallyRead(Variable mainv, Section section, CancelTask cancelTask)
    throws IOException, InvalidRangeException {
  Vstate1D vstate = (Vstate1D) mainv.getSPobject();

  // read the original type - if its been promoted to a new type, the conversion happens after this read
  DataType dtype = (mainv instanceof VariableDS) ? ((VariableDS) mainv).getOriginalDataType() : mainv.getDataType();

  Array allData = Array.factory(dtype, section.getShape());
  int destPos = 0;

  // assumes the first dimension is time: LOOK: what about ensemble ??
  List<Range> ranges = section.getRanges();
  Range timeRange = ranges.get(0);
  List<Range> innerSection = ranges.subList(1, ranges.size());

  // keep track of open files - must be local variable for thread safety
  HashMap<String, NetcdfDataset> openFilesRead = new HashMap<>();

  try {

    // iterate over the desired forecast times
    for (int timeIdx : timeRange) {
      Array result = null;

      // find the inventory for this grid, runtime, and hour
      TimeInventory.Instance timeInv = vstate.timeInv.getInstance(vstate.gridLite, timeIdx);
      if (timeInv == null) {
        if (logger.isDebugEnabled())
          logger.debug("Missing Inventory timeInx=" + timeIdx + " for " + mainv.getFullName() + " in "
              + state.lite.collectionName);
        // vstate.timeInv.getInstance(vstate.gridLite, timeIdx); // allow debugger
      } else if (timeInv.getDatasetLocation() != null) {
        if (debugRead)
          System.out.printf("HIT %s%n", timeInv);
        result = read(timeInv, mainv.getFullNameEscaped(), innerSection, openFilesRead); // may return null
        result = MAMath.convert(result, dtype); // just in case it need to be converted
      }

      // may have missing data
      if (result == null) {
        int[] shape = new Section(innerSection).getShape();
        result = ((VariableDS) mainv).getMissingDataArray(shape); // fill with missing values
        if (debugRead)
          System.out.printf("MISS %d ", timeIdx);
      }

      if (debugRead)
        System.out.printf("%d reallyRead %s %d bytes start at %d total size is %d%n", timeIdx, mainv.getFullName(),
            result.getSize(), destPos, allData.getSize());

      Array.arraycopy(result, 0, allData, destPos, (int) result.getSize());
      destPos += result.getSize();
    }
    return allData;

  } finally {
    // close any files used during this operation
    closeAll(openFilesRead);
  }
}