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

The following examples show how to use ucar.ma2.Array#getDataAsByteBuffer() . 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
public static NcStreamProto.Attribute.Builder encodeAtt(Attribute att) {
  NcStreamProto.Attribute.Builder attBuilder = NcStreamProto.Attribute.newBuilder();
  attBuilder.setName(att.getShortName());
  attBuilder.setDataType(convertDataType(att.getDataType()));
  attBuilder.setLen(att.getLength());

  // values
  if (att.getLength() > 0) {
    if (att.isString()) {
      for (int i = 0; i < att.getLength(); i++)
        attBuilder.addSdata(att.getStringValue(i));

    } else {
      Array data = att.getValues();
      ByteBuffer bb = data.getDataAsByteBuffer();
      attBuilder.setData(ByteString.copyFrom(bb.array()));
    }
  }

  return attBuilder;
}
 
Example 2
Source File: DataImportExample.java    From tablestore-examples with Apache License 2.0 6 votes vote down vote up
/**
 * read data from netcdf file and write data to table store.
 * @param meta
 * @param ncFileName
 * @throws Exception
 */
public void importFromNcFile(GridDataSetMeta meta, String ncFileName) throws Exception {
    GridDataWriter writer = tableStoreGrid.getDataWriter(meta);
    NetcdfFile ncFile = NetcdfFile.open(ncFileName);
    List<Variable> variables = ncFile.getVariables();
    for (Variable variable : variables) {
        if (meta.getVariables().contains(variable.getShortName())) {
            for (int t = 0; t < meta.gettSize(); t++) {
                for (int z = 0; z < meta.getzSize(); z++) {
                    Array array = variable.read(new int[]{t, z, 0, 0}, new int[]{1, 1, meta.getxSize(), meta.getySize()});
                    Grid2D grid2D = new Grid2D(array.getDataAsByteBuffer(), variable.getDataType(),
                            new int[] {0, 0}, new int[] {meta.getxSize(), meta.getySize()});
                    writer.writeGrid2D(variable.getShortName(), t, z, grid2D);
                }
            }
        }
    }
}
 
Example 3
Source File: BlockUtil.java    From tablestore-examples with Apache License 2.0 6 votes vote down vote up
public static List<Grid2D> splitGrid2DToBlocks(Grid2D grid2D, int xSplitCount, int ySplitCount) throws InvalidRangeException {
    Array array = grid2D.toArray();
    int blockXSize = (grid2D.getPlane().getxRange().getSize() - 1) / xSplitCount + 1;
    int blockYSize = (grid2D.getPlane().getyRange().getSize() - 1) / ySplitCount + 1;
    List<Grid2D> result = new ArrayList<Grid2D>();
    for (int i = 0; i < xSplitCount; i++) {
        int startX = i * blockXSize;
        int endX = Math.min(grid2D.getPlane().getxRange().getSize(), startX + blockXSize);
        if (startX >= grid2D.getPlane().getxRange().getSize()) {
            break;
        }
        for (int j = 0; j < ySplitCount; j++) {
            int startY = j * blockYSize;
            int endY = Math.min(grid2D.getPlane().getyRange().getSize(), startY + blockYSize);
            if (startY >= grid2D.getPlane().getyRange().getSize()) {
                break;
            }
            int[] origin = new int[] { startX, startY };
            int[] shape = new int[] { endX - startX, endY - startY };
            Array section = array.section(origin, shape);
            Grid2D block = new Grid2D(section.getDataAsByteBuffer(), grid2D.getDataType(), origin, shape);
            result.add(block);
        }
    }
    return result;
}
 
Example 4
Source File: NcStream.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private static Builder encodeVar(Variable var, int sizeToCache) throws IOException {
  Builder builder = NcStreamProto.Variable.newBuilder();
  builder.setName(var.getShortName());
  builder.setDataType(convertDataType(var.getDataType()));
  if (var.getDataType().isEnum()) {
    EnumTypedef enumType = var.getEnumTypedef();
    if (enumType != null)
      builder.setEnumType(enumType.getShortName());
  }

  for (Dimension dim : var.getDimensions()) {
    builder.addShape(encodeDim(dim));
  }

  for (Attribute att : var.attributes()) {
    builder.addAtts(encodeAtt(att));
  }

  // put small amounts of data in header "immediate mode"
  if (var.isCaching() && var.getDataType().isNumeric()) {
    if (var.isCoordinateVariable() || var.getSize() * var.getElementSize() < sizeToCache) {
      Array data = var.read();
      ByteBuffer bb = data.getDataAsByteBuffer();
      builder.setData(ByteString.copyFrom(bb.array()));
    }
  }

  return builder;
}