Java Code Examples for ucar.ma2.DataType#getSize()

The following examples show how to use ucar.ma2.DataType#getSize() . 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: H5headerNew.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
Array readArray() throws IOException {
  int[] shape = mds.dimLength;
  DataType dataType = typeInfo.dataType;
  Layout layout;
  try {
    if (isChunked) {
      layout = new H5tiledLayout(this, dataType, new Section(shape));
    } else {
      layout = new LayoutRegular(dataPos, dataType.getSize(), shape, null);
    }
  } catch (InvalidRangeException e) {
    // cant happen because we use null for wantSection
    throw new IllegalStateException();
  }
  Object data = IospHelper.readDataFill(raf, layout, dataType, getFillValue(), typeInfo.endian, false);
  return Array.factory(dataType, shape, data);
}
 
Example 2
Source File: BlockUtil.java    From tablestore-examples with Apache License 2.0 6 votes vote down vote up
public static Grid2D buildGrid2DFromBlocks(Plane plane, DataType dataType, List<Grid2D> blocks, byte[] buffer, int pos) {
    int size = plane.getxRange().getSize() * plane.getyRange().getSize() * dataType.getSize();
    if (buffer.length - pos < size) {
        throw new IllegalArgumentException("buffer not enough");
    }
    int count = 0;
    for (Grid2D block : blocks) {
        Plane blockPlane = block.getPlane();
        for (int x = Math.max(blockPlane.getxRange().getStart(), plane.getxRange().getStart());
                x < Math.min(blockPlane.getxRange().getEnd(), plane.getxRange().getEnd()); x++) {
            for (int y = Math.max(blockPlane.getyRange().getStart(), plane.getyRange().getStart());
                    y < Math.min(blockPlane.getyRange().getEnd(), plane.getyRange().getEnd()); y++) {
                int posInBlock = dataType.getSize() * ((x - blockPlane.getxRange().getStart()) * (blockPlane.getyRange().getSize()) + (y - blockPlane.getyRange().getStart())) ;
                int posInData = dataType.getSize() * ((x - plane.getxRange().getStart()) * plane.getyRange().getSize() + (y - plane.getyRange().getStart()));
                System.arraycopy(block.getDataAsByteArray(), posInBlock, buffer, pos + posInData, dataType.getSize());
                count += dataType.getSize();
            }
        }
    }
    if (count != size) {
        throw new RuntimeException("the blocks does not contain enough data");
    }
    ByteBuffer byteBuffer = ByteBuffer.wrap(buffer, pos, size);
    return new Grid2D(byteBuffer, dataType, plane.getOrigin(), plane.getShape());
}
 
Example 3
Source File: Grid.java    From tablestore-examples with Apache License 2.0 6 votes vote down vote up
public Grid(ByteBuffer buffer, DataType dataType, int[] origin, int[] shape) {
    this.buffer = buffer;
    this.dataType = dataType;
    this.origin = origin;
    this.shape = shape;
    int size = dataType.getSize();
    for (int i = 0; i < shape.length; i++) {
        size *= shape[i];
    }
    if (buffer.remaining() != size) {
        throw new IllegalArgumentException("data length and shape mismatch");
    }
    if (origin.length != shape.length) {
        throw new IllegalArgumentException("the length of origin and shape mismatch");
    }
}
 
Example 4
Source File: OpendapServlet.java    From tds with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
long computeArraySize(SDArray da) throws Exception {
  assert (da.getContainerVar() instanceof DPrimitive);
  BaseType base = da.getPrimitiveVector().getTemplate();
  DataType dtype = DODSNetcdfFile.convertToNCType(base, false);
  int elemSize = dtype.getSize();
  int n = da.numDimensions();
  List<Range> ranges = new ArrayList<>(n);
  long size = 0;
  for (int i = 0; i < n; i++) {
    ranges.add(new Range(da.getStart(i), da.getStop(i), da.getStride(i)));
  }
  Section s = new Section(ranges);
  size += s.computeSize() * elemSize;

  return size;
}
 
Example 5
Source File: H5headerNew.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
String readString() throws IOException {
  int[] shape = new int[] {mdt.byteSize};
  DataType dataType = typeInfo.dataType;
  Layout layout;
  try {
    if (isChunked) {
      layout = new H5tiledLayout(this, dataType, new Section(shape));
    } else {
      layout = new LayoutRegular(dataPos, dataType.getSize(), shape, null);
    }
  } catch (InvalidRangeException e) {
    // cant happen because we use null for wantSection
    throw new IllegalStateException();
  }
  Object data = IospHelper.readDataFill(raf, layout, dataType, getFillValue(), typeInfo.endian, true);
  Array dataArray = Array.factory(dataType, shape, data);

  // read and parse the ODL
  String result = "";
  if (dataArray instanceof ArrayChar.D1) {
    ArrayChar ca = (ArrayChar) dataArray;
    result = ca.getString(); // common case only StructMetadata.0, avoid extra copy
  } else if (dataArray instanceof ArrayObject.D0) {
    ArrayObject ao = (ArrayObject) dataArray;
    result = (String) ao.getObject(0);
  } else {
    log.error("Unsupported array type {} for StructMetadata", dataArray.getElementType());
  }
  return result;
}
 
Example 6
Source File: OpendapServlet.java    From tds with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
long computeFieldSize(BaseType bt, boolean isAscii) throws Exception {
  long fieldsize = 0;
  // Figure out what this field is (e.g. primitive or not)
  // Somewhat convoluted.
  if (bt instanceof DConstructor) {
    // simple struct, seq, or grid => recurse
    fieldsize = computeSize((DConstructor) bt, isAscii);
  } else if (bt instanceof DArray) {
    SDArray da = (SDArray) bt;
    // Separate structure arrays from primitive arrays
    if (da.getContainerVar() instanceof DPrimitive) {
      fieldsize = computeArraySize(da);
    } else if (da.getContainerVar() instanceof DStructure) {
      fieldsize = computeSize((DStructure) da.getContainerVar(), isAscii); // recurse
    } else { // Some kind of problem
      throw new NoSuchTypeException("Computesize: unexpected type for " + bt.getLongName());
    }
  } else if (bt instanceof DPrimitive) {
    DPrimitive dp = (DPrimitive) bt;
    if (dp instanceof DString) {
      String v = ((DString) dp).getValue();
      fieldsize = (v == null ? 0 : v.length());
    } else {
      DataType dtype = DODSNetcdfFile.convertToNCType(bt, false);
      fieldsize = dtype.getSize();
    }
  } else { // Some kind of problem
    throw new NoSuchTypeException("Computesize: unknown type for " + bt.getLongName());
  }
  return fieldsize;
}