Java Code Examples for ucar.ma2.Section#Builder

The following examples show how to use ucar.ma2.Section#Builder . 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 6 votes vote down vote up
@Nonnull
public static Section decodeSection(NcStreamProto.Section proto) {
  Section.Builder section = Section.builder();

  for (ucar.nc2.stream.NcStreamProto.Range pr : proto.getRangeList()) {
    try {
      long stride = pr.getStride();
      if (stride == 0)
        stride = 1; // default in protobuf2 was 1, but protobuf3 is 0, luckily 0 is illegal
      if (pr.getSize() == 0)
        section.appendRange(Range.EMPTY); // used for scalars LOOK really used ??
      else {
        // this.last = first + (this.length-1) * stride;
        section.appendRange((int) pr.getStart(), (int) (pr.getStart() + (pr.getSize() - 1) * stride), (int) stride);
      }

    } catch (InvalidRangeException e) {
      throw new RuntimeException("Bad Section in ncstream", e);
    }
  }
  return section.build();
}
 
Example 2
Source File: Structure.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Use this when this is a one dimensional array of Structures, or you are doing the index calculation yourself for
 * a multidimension array. This will read only the ith structure, and return the data as a StructureData object.
 * 
 * @param index index into 1D array
 * @return ith StructureData
 * @throws java.io.IOException on read error
 * @throws ucar.ma2.InvalidRangeException if index out of range
 */
public StructureData readStructure(int index) throws IOException, ucar.ma2.InvalidRangeException {
  Section.Builder sb = Section.builder();

  if (getRank() == 1) {
    sb.appendRange(index, index);

  } else if (getRank() > 1) {
    Index ii = Index.factory(shape); // convert to nD index
    ii.setCurrentCounter(index);
    int[] origin = ii.getCurrentCounter();
    for (int anOrigin : origin)
      sb.appendRange(anOrigin, anOrigin);
  }

  Array dataArray = read(sb.build());
  ArrayStructure data = (ArrayStructure) dataArray;
  return data.getStructureData(0);
}
 
Example 3
Source File: Structure.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private void readNextGeneralRank() throws IOException {

      try {
        Section.Builder sb = Section.builder().appendRanges(shape);
        sb.setRange(0, new Range(outerCount, outerCount));

        as = (ArrayStructure) read(sb.build());

        if (NetcdfFile.debugStructureIterator)
          System.out.println("readNext inner=" + outerCount + " total=" + outerCount);

        outerCount++;

      } catch (InvalidRangeException e) {
        log.error("Structure.Iterator.readNext() ", e);
        throw new IllegalStateException("Structure.Iterator.readNext() ", e);
      } // cant happen

      readStart += as.getSize();
      readCount = 0;
    }
 
Example 4
Source File: N3iospWriter.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private void fillRecordVariables(int recStart, int recEnd) throws IOException, InvalidRangeException {
  // do each record completely, should be a bit more efficient

  for (int i = recStart; i < recEnd; i++) { // do one record at a time
    Range r = new Range(i, i);

    // run through each variable
    for (Variable v : ncfile.getVariables()) {
      if (!v.isUnlimited() || (v instanceof Structure))
        continue;
      Section.Builder recordSection = Section.builder().appendRanges(v.getRanges());
      recordSection.setRange(0, r);
      writeData(v, recordSection.build(), makeConstantArray(v));
    }
  }
}
 
Example 5
Source File: FmrcDataset.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private Array read(TimeInventory.Instance timeInstance, String fullNameEsc, List<Range> innerSection,
    HashMap<String, NetcdfDataset> openFilesRead) throws IOException, InvalidRangeException {
  NetcdfFile ncfile = open(timeInstance.getDatasetLocation(), openFilesRead);
  if (ncfile == null)
    return null; // file might be deleted ??

  Variable v = ncfile.findVariable(fullNameEsc);
  if (v == null)
    return null; // v could be missing, return missing data i think

  // assume time is first dimension LOOK: out of-order; ensemble; section different ??
  Range timeRange = new Range(timeInstance.getDatasetIndex(), timeInstance.getDatasetIndex());
  Section.Builder sb = Section.builder().appendRanges(innerSection);
  sb.insertRange(0, timeRange);
  return v.read(sb.build());
}
 
Example 6
Source File: PointIteratorMultidim.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private StructureData nextStructureData() {
  StructureDataW sdata = new StructureDataW(members);

  for (Variable var : vars) {
    Section.Builder sb = Section.builder();
    try {
      sb.appendRange(outerIndex, outerIndex);
      sb.appendRange(count, count);
      for (int i = 2; i < var.getRank(); i++)
        sb.appendRangeAll();
      Array data = var.read(sb.build());
      sdata.setMemberData(var.getShortName(), data);

    } catch (InvalidRangeException | IOException e) {
      throw new RuntimeException(e);
    }

  }

  return sdata;
}
 
Example 7
Source File: NcStream.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private static Variable.Builder decodeVar(NcStreamProto.Variable var) {
  DataType varType = convertDataType(var.getDataType());
  Variable.Builder ncvar = Variable.builder().setName(var.getName()).setDataType(varType);

  if (varType.isEnum()) {
    ncvar.setEnumTypeName(var.getEnumType());
  }

  // The Dimensions are stored redunantly in the Variable.
  // If shared, they must also exist in a parent Group. However, we dont yet have the Groups wired together,
  // so that has to wait until build().
  List<Dimension> dims = new ArrayList<>(6);
  Section.Builder section = Section.builder();
  for (ucar.nc2.stream.NcStreamProto.Dimension dim : var.getShapeList()) {
    dims.add(decodeDim(dim));
    section.appendRange((int) dim.getLength());
  }
  ncvar.addDimensions(dims);

  for (ucar.nc2.stream.NcStreamProto.Attribute att : var.getAttsList())
    ncvar.addAttribute(decodeAtt(att));

  if (!var.getData().isEmpty()) {
    // LOOK may mess with ability to change var size later.
    ByteBuffer bb = ByteBuffer.wrap(var.getData().toByteArray());
    Array data = Array.factory(varType, section.build().getShape(), bb);
    ncvar.setCachedData(data, true);
  }

  return ncvar;
}
 
Example 8
Source File: Table.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public StructureDataIterator getStructureDataIterator(Cursor cursor) throws IOException {
  int recnum = cursor.getParentRecnum();
  try {
    Section.Builder sb = Section.builder().appendRange(recnum, recnum);
    int count = 1;
    while (count++ < struct.getRank()) // handles multidim case
      sb.appendRangeAll();
    ArrayStructure data = (ArrayStructure) struct.read(sb.build()); // read all the data for a fixed outer index
    return data.getStructureDataIterator();
  } catch (InvalidRangeException e) {
    throw new IllegalStateException(e);
  }
}
 
Example 9
Source File: ReduceReader.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public Array reallyRead(Variable client, Section section, CancelTask cancelTask)
    throws IOException, InvalidRangeException {
  Section.Builder orgSection = Section.builder().appendRanges(section.getRanges());
  for (int dim : dims) {
    orgSection.insertRange(dim, Range.ONE); // lowest first
  }

  Array data = orgClient._read(orgSection.build());
  for (int i = dims.size() - 1; i >= 0; i--)
    data = data.reduce(dims.get(i)); // highest first

  return data;
}