Java Code Examples for ucar.nc2.Dimension#isUnlimited()

The following examples show how to use ucar.nc2.Dimension#isUnlimited() . 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
private String extendDimension(Group.Builder parent, H5Group h5group, String name, int length) {
  int pos = name.lastIndexOf('/');
  String dimName = (pos >= 0) ? name.substring(pos + 1) : name;

  Dimension d = h5group.dimMap.get(dimName); // first look in current group
  if (d == null) {
    d = parent.findDimension(dimName).orElse(null); // then look in parent groups
  }

  if (d != null) {
    if (d.isUnlimited() && (length > d.getLength())) {
      parent.replaceDimension(d.toBuilder().setLength(length).build());
    }

    if (!d.isUnlimited() && (length != d.getLength())) {
      throw new IllegalStateException(
          "extendDimension: DimScale has different length than dimension it references dimScale=" + dimName);
    }
    return d.getShortName();
  }

  return dimName;
}
 
Example 2
Source File: RafNimbus.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public TableConfig getConfig(FeatureType wantFeatureType, NetcdfDataset ds, Formatter errlog) {
  TableConfig topTable = new TableConfig(Table.Type.Top, "singleTrajectory");

  CoordinateAxis coordAxis = CoordSysEvaluator.findCoordByType(ds, AxisType.Time);
  if (coordAxis == null) {
    errlog.format("Cant find a time coordinate");
    return null;
  }
  Dimension innerDim = coordAxis.getDimension(0);
  boolean obsIsStruct = Evaluator.hasNetcdf3RecordStructure(ds) && innerDim.isUnlimited();

  TableConfig obsTable = new TableConfig(Table.Type.Structure, innerDim.getShortName());
  obsTable.dimName = innerDim.getShortName();
  obsTable.time = coordAxis.getFullName();
  obsTable.structName = obsIsStruct ? "record" : innerDim.getShortName();
  obsTable.structureType =
      obsIsStruct ? TableConfig.StructureType.Structure : TableConfig.StructureType.PsuedoStructure;
  CoordSysEvaluator.findCoords(obsTable, ds, axis -> innerDim.equals(axis.getDimension(0)));

  topTable.addChild(obsTable);
  return topTable;
}
 
Example 3
Source File: N3iospWriter.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void writeData(Variable v2, Section section, Array values) throws java.io.IOException, InvalidRangeException {
  N3headerNew.Vinfo vinfo = (N3headerNew.Vinfo) v2.getSPobject();
  DataType dataType = v2.getDataType();

  if (v2.isUnlimited()) {
    Range firstRange = section.getRange(0);
    setNumrecs(firstRange.last() + 1);
  }

  if (v2 instanceof Structure) {
    if (!(values instanceof ArrayStructure))
      throw new IllegalArgumentException("writeData for Structure: data must be ArrayStructure");

    if (v2.getRank() == 0)
      throw new IllegalArgumentException("writeData for Structure: must have rank > 0");

    Dimension d = v2.getDimension(0);
    if (!d.isUnlimited())
      throw new IllegalArgumentException("writeData for Structure: must have unlimited dimension");

    writeRecordData((Structure) v2, section, (ArrayStructure) values);

  } else {
    Layout layout = (!v2.isUnlimited()) ? new LayoutRegular(vinfo.begin, v2.getElementSize(), v2.getShape(), section)
        : new LayoutRegularSegmented(vinfo.begin, v2.getElementSize(), header.recsize, v2.getShape(), section);
    writeData(values, layout, dataType);
  }
}
 
Example 4
Source File: N3iospWriter.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void setNumrecs(int n) throws IOException, InvalidRangeException {
  if (n <= header.numrecs)
    return;
  int startRec = header.numrecs;

  // fileUsed = recStart + recsize * n;
  ((N3headerWriter) header).setNumrecs(n);
  // this.numrecs = n;

  // TODO udim.setLength : need UnlimitedDimension extends Dimension?
  // need to let unlimited dimension know of new shape
  for (Dimension dim : ncfile.getRootGroup().getDimensions()) {
    if (dim.isUnlimited())
      dim.setLength(n);
  }

  // need to let all unlimited variables know of new shape TODO immutable??
  for (Variable v : ncfile.getVariables()) {
    if (v.isUnlimited()) {
      v.resetShape();
      v.setCachedData(null, false);
    }
  }

  // extend file, handle filling
  if (fill)
    fillRecordVariables(startRec, n);
  else
    raf.setMinLength(header.calcFileSize());
}
 
Example 5
Source File: CFpointObs.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void makeStructureInfo(TableConfig tableConfig, NetcdfDataset ds, Structure parent, Dimension dim) {
  tableConfig.dimName = dim.getShortName();
  if (parent != null) {
    tableConfig.structureType = TableConfig.StructureType.Structure;
    tableConfig.structName = parent.getShortName();
  } else {
    boolean hasNetcdf3Struct = Evaluator.hasNetcdf3RecordStructure(ds) && dim.isUnlimited();
    tableConfig.structureType =
        hasNetcdf3Struct ? TableConfig.StructureType.Structure : TableConfig.StructureType.PsuedoStructure;
    tableConfig.structName = hasNetcdf3Struct ? "record" : dim.getShortName();
  }

}
 
Example 6
Source File: WriterCFPointAbstract.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void addDataVariablesExtended(Structure.Builder<?> recordb, StructureData obsData, String coordNames) {
  for (StructureMembers.Member m : obsData.getMembers()) {
    VariableSimpleIF oldVar = findDataVar(m.getName());
    if (oldVar == null)
      continue;

    // make dimension list
    StringBuilder dimNames = new StringBuilder();
    for (Dimension d : oldVar.getDimensions()) {
      if (d.isUnlimited())
        continue;
      if (d.getShortName() == null || !d.getShortName().equals(recordDimName))
        dimNames.append(" ").append(d.getLength()); // anonymous
    }

    Variable.Builder newVar = Variable.builder().setName(oldVar.getShortName()).setDataType(oldVar.getDataType())
        .setParentGroupBuilder(writerb.getRootGroup()).setDimensionsByName(dimNames.toString());
    recordb.addMemberVariable(newVar);

    // TODO
    /*
     * Variable newVar =
     * writer.addStructureMember(record, oldVar.getShortName(), oldVar.getDataType(), dimNames.toString());
     * if (newVar == null) {
     * logger.warn("Variable already exists =" + oldVar.getShortName()); // LOOK barf
     * continue;
     * }
     */

    for (Attribute att : oldVar.attributes()) {
      String attName = att.getShortName();
      if (!reservedVariableAtts.contains(attName) && !attName.startsWith("_Coordinate"))
        newVar.addAttribute(att);
    }
    newVar.addAttribute(new Attribute(CF.COORDINATES, coordNames));
  }

}
 
Example 7
Source File: Nc4ChunkingDefault.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
protected int[] convertUnlimitedShape(List<Dimension> dims) {
  int[] result = new int[dims.size()];
  int count = 0;
  for (Dimension d : dims) {
    result[count++] = (d.isUnlimited()) ? 1 : d.getLength();
  }
  return result;
}
 
Example 8
Source File: NcmlWriter.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public Element makeDimensionElement(Dimension dim) throws IllegalArgumentException {
  if (!dim.isShared()) {
    throw new IllegalArgumentException(
        "Cannot create private dimension: " + "in NcML, <dimension> elements are always shared.");
  }

  Element dimElem = new Element("dimension", namespace);
  dimElem.setAttribute("name", dim.getShortName());
  dimElem.setAttribute("length", Integer.toString(dim.getLength()));

  if (dim.isUnlimited())
    dimElem.setAttribute("isUnlimited", "true");

  return dimElem;
}
 
Example 9
Source File: CDLWriter.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void writeCDL(Dimension dim, Indent indent) {
  String name = strict ? NetcdfFiles.makeValidCDLName(dim.getShortName()) : dim.getShortName();
  out.format("%s%s", indent, name);
  if (dim.isUnlimited())
    out.format(" = UNLIMITED;   // (%d currently)", dim.getLength());
  else if (dim.isVariableLength())
    out.format(" = UNKNOWN;");
  else
    out.format(" = %d;", dim.getLength());
}
 
Example 10
Source File: WriterCFPointDataset.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private boolean isExtraDimension(Dimension d) {
  return (!d.isUnlimited() && !d.getShortName().equalsIgnoreCase("time"));
}
 
Example 11
Source File: N3headerWriter.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
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 12
Source File: FmrcDataset.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
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);
  }
}