Java Code Examples for ucar.nc2.Variable#getSize()

The following examples show how to use ucar.nc2.Variable#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: H5diagNew.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public void showCompress(Variable v, H5headerNew.Vinfo vinfo, Size total, Formatter f) throws IOException {
  H5objects.MessageDataspace mdt = vinfo.mds;

  long total_elems = 1;
  f.format("%8s %-40s(", v.getDataType(), v.getShortName());
  for (int len : mdt.dimLength) {
    f.format("%d ", len);
    total_elems *= len;
  }
  boolean sizeOk = total_elems == v.getSize();
  total_elems = v.getSize();

  long nominalSize = total_elems * v.getElementSize();

  Size size = new Size(nominalSize, 1);
  countStorageSize(vinfo, size);
  total.storage += size.storage;
  total.nominal += nominalSize;
  total.count += size.count;
  float ratio = (size.storage == 0) ? 0 : (float) nominalSize / size.storage;

  f.format(") == %d nelems %s == %d bytes storage = %d (%f) nchunks = %d%n", total_elems, sizeOk ? "" : "*",
      nominalSize, size.storage, ratio, size.count);
}
 
Example 2
Source File: H5diag.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public void showCompress(Variable v, H5header.Vinfo vinfo, Size total, Formatter f) throws IOException {
  H5header.MessageDataspace mdt = vinfo.mds;

  long total_elems = 1;
  f.format("%8s %-40s(", v.getDataType(), v.getShortName());
  for (int len : mdt.dimLength) {
    f.format("%d ", len);
    total_elems *= len;
  }
  boolean sizeOk = total_elems == v.getSize();
  total_elems = v.getSize();

  long nominalSize = total_elems * v.getElementSize();

  Size size = new Size(nominalSize, 1);
  countStorageSize(vinfo, size);
  total.storage += size.storage;
  total.nominal += nominalSize;
  total.count += size.count;
  float ratio = (size.storage == 0) ? 0 : (float) nominalSize / size.storage;

  f.format(") == %d nelems %s == %d bytes storage = %d (%f) nchunks = %d%n", total_elems, sizeOk ? "" : "*",
      nominalSize, size.storage, ratio, size.count);
}
 
Example 3
Source File: TestDir.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public static int readAllData(NetcdfFile ncfile) throws IOException {
  logger.debug("------Reading ncfile {}", ncfile.getLocation());
  try {
    for (Variable v : ncfile.getVariables()) {
      if (v.getSize() > max_size) {
        Section s = makeSubset(v);
        logger.debug("  Try to read variable {} size={} section={}", v.getNameAndDimensions(), v.getSize(), s);
        v.read(s);
      } else {
        logger.debug("  Try to read variable {} size={}", v.getNameAndDimensions(), v.getSize());
        v.read();
      }
    }

    return 1;
  } catch (InvalidRangeException e) {
    throw new RuntimeException(e);
  }
}
 
Example 4
Source File: TestDir.java    From tds with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
static public int readAllData(NetcdfFile ncfile) throws IOException {
  logger.debug("------Reading ncfile {}", ncfile.getLocation());
  try {
    for (Variable v : ncfile.getVariables()) {
      if (v.getSize() > max_size) {
        Section s = makeSubset(v);
        logger.debug("  Try to read variable {} size={} section={}", v.getNameAndDimensions(), v.getSize(), s);
        v.read(s);
      } else {
        logger.debug("  Try to read variable {} size={}", v.getNameAndDimensions(), v.getSize());
        v.read();
      }
    }

    return 1;
  } catch (InvalidRangeException e) {
    throw new RuntimeException(e);
  }
}
 
Example 5
Source File: ConvertD2N.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private Array convertStringToChar(Array data, Variable ncVar) {
  String s = (String) data.getObject(Index.scalarIndexImmutable);

  int total = (int) ncVar.getSize();
  char[] storage = new char[total];

  int len = Math.min(s.length(), total);
  for (int k = 0; k < len; k++)
    storage[k] = s.charAt(k);

  return Array.factory(DataType.CHAR, ncVar.getShape(), storage);
}
 
Example 6
Source File: CFRadialAdapter.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public boolean isStationary() {
  // only check once
  if (!isStationaryChecked) {
    Variable lat = ds.findVariable("latitude");
    if (lat != null) {
      if (lat.isScalar())
        isStationary = lat.getSize() == 1;
      else {
        // if array, check to see if all of the values are
        // approximately the same
        Array gar;
        try {
          gar = lat.read();
          Object firstVal = gar.getObject(0);
          Array gar2 = gar.copy();
          for (int i = 1; i < gar.getSize(); i++) {
            gar2.setObject(i, firstVal);
          }
          isStationary = nearlyEquals(gar, gar2);
        } catch (IOException e) {
          log.error("Error reading latitude variable {}. Cannot determine if "
              + "platform is stationary. Setting to default (false).", lat.getFullName());
        }
      }
    }
    isStationaryChecked = true;
  }

  return isStationary;
}
 
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 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;
}
 
Example 8
Source File: ADASConvention.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void calcCenterPoints(Projection proj) throws IOException {
  double lat_check = rootGroup.getAttributeContainer().findAttributeDouble("CTRLAT", Double.NaN);
  double lon_check = rootGroup.getAttributeContainer().findAttributeDouble("CTRLON", Double.NaN);

  LatLonPoint lpt0 = LatLonPoint.create(lat_check, lon_check);
  ProjectionPoint ppt0 = proj.latLonToProj(lpt0);

  VariableDS.Builder xstag = (VariableDS.Builder) rootGroup.findVariableLocal("x_stag")
      .orElseThrow(() -> new IllegalStateException("Must have x_stag Variable"));
  Variable xstagOrg = xstag.orgVar;
  int nxpts = (int) xstagOrg.getSize();
  ArrayFloat.D1 xstagData = (ArrayFloat.D1) xstagOrg.read();
  float center_x = xstagData.get(nxpts - 1);
  double false_easting = center_x / 2000 - ppt0.getX() * 1000.0;

  VariableDS.Builder ystag = (VariableDS.Builder) rootGroup.findVariableLocal("y_stag")
      .orElseThrow(() -> new IllegalStateException("Must have y_stag Variable"));
  Variable ystagOrg = ystag.orgVar;
  int nypts = (int) ystagOrg.getSize();
  ArrayFloat.D1 ystagData = (ArrayFloat.D1) ystagOrg.read();
  float center_y = ystagData.get(nypts - 1);
  double false_northing = center_y / 2000 - ppt0.getY() * 1000.0;
  log.debug("false easting/northing= {} {} ", false_easting, false_northing);

  double dx = rootGroup.getAttributeContainer().findAttributeDouble("DX", Double.NaN);
  double dy = rootGroup.getAttributeContainer().findAttributeDouble("DY", Double.NaN);

  double w = dx * (nxpts - 1);
  double h = dy * (nypts - 1);
  double startx = ppt0.getX() * 1000.0 - w / 2;
  double starty = ppt0.getY() * 1000.0 - h / 2;

  xstag.setAutoGen(startx, dx);
  ystag.setAutoGen(starty, dy);
}
 
Example 9
Source File: Nc4ChunkingStrategyGrib.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public boolean isChunked(Variable v) {
  if (v.isUnlimited())
    return true;
  // if (getChunkAttribute(v) != null) return true;

  int n = v.getRank();
  return n >= 2 && v.getSize() * v.getElementSize() > getMinVariableSize();
}
 
Example 10
Source File: Nc4ChunkingDefault.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public boolean isChunked(Variable v) {
  if (v.isUnlimited())
    return true;
  // if (getChunkAttribute(v) != null) return true;

  long size = v.getSize() * v.getElementSize();
  return (size > minVariableSize);
}
 
Example 11
Source File: NetcdfCopier.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void copyVariableData(NetcdfFormatWriter ncwriter, Group groupIn, Group groupOut, Count counter,
    CancelTask cancel) throws IOException {
  for (Variable oldVar : groupIn.getVariables()) {
    if (cancel.isCancel()) {
      break;
    }
    Variable newVar = groupOut.findVariableLocal(oldVar.getShortName());
    if (debug) {
      System.out.format("write var= %s size = %d type = %s%n", oldVar.getFullName(), oldVar.getSize(),
          oldVar.getDataType());
    }
    cancel.setProgress("writing " + oldVar.getFullName(), counter.countVars++);

    long size = oldVar.getSize() * oldVar.getElementSize();
    counter.bytes += size;

    if (size <= maxSize) {
      copyAll(ncwriter, oldVar, newVar);
    } else {
      copySome(ncwriter, oldVar, newVar, maxSize, cancel);
    }
  }

  for (Group nestedIn : groupIn.getGroups()) {
    if (cancel.isCancel()) {
      break;
    }
    Group nestedOut = groupOut.findGroupLocal(nestedIn.getShortName());
    copyVariableData(ncwriter, nestedIn, nestedOut, counter, cancel);
  }
}
 
Example 12
Source File: TestHttpOpen.java    From tds with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private long readAllData(NetcdfFile ncfile) throws IOException {
  System.out.println("------Open " + ncfile.getLocation());

  long total = 0;
  for (Variable v : ncfile.getVariables()) {
    long nbytes = v.getSize() * v.getElementSize();
    System.out.println("  Try to read variable " + v.getFullName() + " " + nbytes);
    v.read();
    total += v.getSize() * v.getElementSize();
  }
  ncfile.close();
  return total;
}
 
Example 13
Source File: CFGridWriter.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private long processTransformationVars(ArrayList<Variable> varList, ArrayList<String> varNameList, NetcdfDataset ncd,
    ucar.nc2.dt.GridDataset gds, GridDatatype grid, Range timeRange, Range zRangeUse, Range yRange, Range xRange,
    int z_stride, int y_stride, int x_stride) throws InvalidRangeException {

  long varsSize = 0L;
  List<CoordinateTransform> cctt = grid.getCoordinateSystem().getCoordinateTransforms();
  for (CoordinateTransform ct : cctt) {
    Parameter param = ct.findParameterIgnoreCase(CF.FORMULA_TERMS);

    if (param != null) {
      String[] varStrings = param.getStringValue().split(" ");
      for (int i = 1; i < varStrings.length; i += 2) {
        Variable paramVar = ncd.findVariable(varStrings[i].trim());

        if (!varNameList.contains(varStrings[i]) && (null != paramVar)) {

          if (gds.findGridDatatype(paramVar.getFullName()) != null) {
            // Subset if needed
            if ((null != timeRange) || (zRangeUse != null) || (x_stride > 1 && y_stride > 1)
                || (yRange != null || xRange != null)) {
              GridDatatype complementaryGrid = gds.findGridDatatype(paramVar.getFullName());
              complementaryGrid = complementaryGrid.makeSubset(null, null, timeRange, zRangeUse, yRange, xRange);
              paramVar = complementaryGrid.getVariable();
            }
          } else {
            // Also have to subset the var if it is not a grid but has vertical dimension (the dimensionless vars in
            // the formula) and zRangeUse != null
            if (zRangeUse != null && paramVar.getRank() == 1) {
              List<Range> ranges = new ArrayList<Range>();
              ranges.add(zRangeUse);
              paramVar = paramVar.section(ranges);
            }
          }
          varNameList.add(paramVar.getFullName());
          varsSize += paramVar.getSize() * paramVar.getElementSize();
          varList.add(paramVar);
        }

      }
    }
  }

  return varsSize;

}