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

The following examples show how to use ucar.nc2.Variable#setSPobject() . 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
private void writeVars(List<Variable> vars, boolean largeFile, Formatter fout) throws IOException {
  int n = vars.size();
  if (n == 0) {
    raf.writeInt(0);
    raf.writeInt(0);
  } else {
    raf.writeInt(MAGIC_VAR);
    raf.writeInt(n);
  }

  for (Variable var : vars) {
    writeString(var.getShortName());

    // dimensions
    long vsize = var.getDataType().getSize(); // works for all netcdf-3 data types
    List<Dimension> dims = var.getDimensions();
    raf.writeInt(dims.size());
    for (Dimension dim : dims) {
      int dimIndex = findDimensionIndex(ncfile, dim);
      raf.writeInt(dimIndex);

      if (!dim.isUnlimited())
        vsize *= dim.getLength();
    }
    long unpaddedVsize = vsize;
    vsize += padding(vsize);

    // variable attributes
    long varAttsPos = raf.getFilePointer();
    writeAtts(var.attributes(), fout);

    // data type, variable size, beginning file position
    DataType dtype = var.getDataType();
    int type = getType(dtype);
    raf.writeInt(type);

    int vsizeWrite = (vsize < MAX_UNSIGNED_INT) ? (int) vsize : -1;
    raf.writeInt(vsizeWrite);
    long pos = raf.getFilePointer();
    if (largeFile)
      raf.writeLong(0); // come back to this later
    else
      raf.writeInt(0); // come back to this later

    // From nc3 file format specification
    // (https://www.unidata.ucar.edu/software/netcdf/docs/netcdf.html#NetCDF-Classic-Format):
    // Note on padding: In the special case of only a single record variable of character,
    // byte, or short type, no padding is used between data values.
    // 2/15/2011: we will continue to write the (incorrect) padded vsize into the header, but we will use the unpadded
    // size to read/write
    if (uvars.size() == 1 && uvars.get(0) == var) {
      if ((dtype == DataType.CHAR) || (dtype == DataType.BYTE) || (dtype == DataType.SHORT)) {
        vsize = unpaddedVsize;
      }
    }
    var.setSPobject(new N3headerNew.Vinfo(var.getShortName(), vsize, pos, var.isUnlimited(), varAttsPos));
  }
}
 
Example 2
private Structure makeChunkVariable(NetcdfFile ncfile, TagVH vh) {
  Vinfo vinfo = new Vinfo(vh.refno);
  vinfo.tags.add(vh);
  vh.vinfo = vinfo;
  vh.used = true;

  TagData data = (TagData) tagMap.get(tagid(vh.refno, TagEnum.VS.getCode()));
  if (data == null) {
    log.error("Cant find tag " + vh.refno + "/" + TagEnum.VS.getCode() + " for TagVH=" + vh.detail());
    return null;
  }
  vinfo.tags.add(data);
  data.used = true;
  data.vinfo = vinfo;

  if (vh.nfields < 1)
    throw new IllegalStateException();

  try {
    Structure.Builder sb = Structure.builder().setName(vh.name);
    vinfo.setVariable(sb);
    Structure s = new Structure(ncfile, null, null, vh.name); // LOOK cheating
    s.setSPobject(vinfo);

    if (vh.nvert > 1)
      s.setDimensionsAnonymous(new int[] {vh.nvert});
    else
      s.setIsScalar();

    for (int fld = 0; fld < vh.nfields; fld++) {
      Variable m = new Variable(ncfile, null, s, vh.fld_name[fld]);
      short type = vh.fld_type[fld];
      short nelems = vh.fld_order[fld];
      H4type.setDataType(type, m);
      if (nelems > 1)
        m.setDimensionsAnonymous(new int[] {nelems});
      else
        m.setIsScalar();

      m.setSPobject(new Minfo(vh.fld_offset[fld]));
      s.addMemberVariable(m);
    }

    vinfo.setData(data, vh.ivsize);
    return s;

  } catch (InvalidRangeException e) {
    throw new IllegalStateException(e.getMessage());
  }
}
 
Example 3
private void transferGroup(Group srcGroup, Group targetGroup, NetcdfDataset target) throws IOException {
  // group attributes
  DatasetConstructor.transferGroupAttributes(srcGroup, targetGroup);

  // dimensions
  for (Dimension d : srcGroup.getDimensions()) {
    if (null == targetGroup.findDimensionLocal(d.getShortName())) {
      Dimension newd =
          new Dimension(d.getShortName(), d.getLength(), d.isShared(), d.isUnlimited(), d.isVariableLength());
      targetGroup.addDimension(newd);
    }
  }

  // transfer variables - eliminate any references to component files
  for (Variable v : srcGroup.getVariables()) {
    Variable targetV = targetGroup.findVariableLocal(v.getShortName());

    if (null == targetV) { // add it
      if (v instanceof Structure) {
        targetV = new StructureDS(target, targetGroup, null, v.getShortName(), v.getDimensionsString(),
            v.getUnitsString(), v.getDescription());
        // LOOK - not adding the members here - what to do ??

      } else {
        targetV = new VariableDS(target, targetGroup, null, v.getShortName(), v.getDataType(),
            v.getDimensionsString(), v.getUnitsString(), v.getDescription());
      }

      DatasetConstructor.transferVariableAttributes(v, targetV);
      VariableDS vds = (VariableDS) v;
      targetV.setSPobject(vds); // temporary, for non-agg variables when proto is made
      if (vds.hasCachedDataRecurse()) {
        if (vds.getSize() > 1000 * 1000) {
          boolean wtf = vds.hasCachedDataRecurse();
        }
        targetV.setCachedData(vds.read()); //
      }
      targetGroup.addVariable(targetV);
    }
  }

  // nested groups - check if target already has it
  for (Group srcNested : srcGroup.getGroups()) {
    Group nested = targetGroup.findGroupLocal(srcNested.getShortName());
    if (null == nested) {
      nested = new Group(target, targetGroup, srcNested.getShortName());
      targetGroup.addGroup(nested);
      for (EnumTypedef et : srcNested.getEnumTypedefs()) {
        targetGroup.addEnumeration(et);
      }
    }
    transferGroup(srcNested, nested, target);
  }
}