Java Code Examples for ucar.nc2.Variable#Builder

The following examples show how to use ucar.nc2.Variable#Builder . 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: DatasetEnhancer.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private void enhanceGroup(Group.Builder group) {

    for (Variable.Builder vb : group.vbuilders) {
      if (vb instanceof StructureDS.Builder<?>) {
        enhanceStructure((StructureDS.Builder<?>) vb);
      } else if (vb instanceof VariableDS.Builder) {
        enhanceVariable((VariableDS.Builder) vb);
      } else {
        throw new IllegalStateException("Not a VariableDS " + vb.shortName);
      }
    }

    for (Group.Builder gb : group.gbuilders) {
      enhanceGroup(gb);
    }
  }
 
Example 2
Source File: SequenceDS.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/** Copy metadata from orgVar. */
public T copyFrom(Sequence orgVar) {
  super.copyFrom(orgVar);
  for (Variable v : orgVar.getVariables()) {
    Variable.Builder newVar;
    if (v instanceof Sequence) {
      newVar = SequenceDS.builder().copyFrom((Sequence) v);
    } else if (v instanceof Structure) {
      newVar = StructureDS.builder().copyFrom((Structure) v);
    } else {
      newVar = VariableDS.builder().copyFrom(v);
    }
    addMemberVariable(newVar);
  }
  setOriginalVariable(orgVar);
  setOriginalName(orgVar.getShortName());
  return self();
}
 
Example 3
Source File: BufrIospBuilder.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private String findUniqueName(Structure.Builder<?> struct, String want, String def) {
  if (want == null) {
    return def + tempNo++;
  }

  String vwant = NetcdfFiles.makeValidCdmObjectName(want);
  Optional<Variable.Builder<?>> oldV = struct.findMemberVariable(vwant);
  if (!oldV.isPresent()) {
    return vwant;
  }

  int seq = 2;
  while (true) {
    String wantSeq = vwant + "-" + seq;
    oldV = struct.findMemberVariable(wantSeq);
    if (!oldV.isPresent()) {
      return wantSeq;
    }
    seq++;
  }
}
 
Example 4
Source File: BufrIospBuilder.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private void addDpiStructure(Structure.Builder parent, BufrConfig.FieldConverter parentFld,
    BufrConfig.FieldConverter dpiField) {
  DataDescriptor dpiKey = dpiField.dds;
  String uname = findUniqueName(parent, dpiField.getName(), "struct");
  dpiKey.name = uname; // name may need to be changed for uniqueness

  Structure.Builder struct = Structure.builder().setName(uname);
  parent.addMemberVariable(struct);
  int n = parentFld.dds.replication;
  struct.setDimensionsAnonymous(new int[] {n}); // anon vector

  Variable.Builder v = Variable.builder().setName("name");
  v.setDataType(DataType.STRING); // scalar
  struct.addMemberVariable(v);

  v = Variable.builder().setName("data");
  v.setDataType(DataType.FLOAT); // scalar
  struct.addMemberVariable(v);

  struct.setSPobject(dpiField); // ??
}
 
Example 5
Source File: H5headerNew.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void addMembersToStructure(Group.Builder parent, Structure.Builder<?> s, MessageDatatype mdt)
    throws IOException {
  for (StructureMember m : mdt.members) {
    Variable.Builder v = makeVariableMember(parent, m.name, m.offset, m.mdt);
    if (v != null) {
      s.addMemberVariable(v);
      if (debug1) {
        log.debug("  made Member Variable " + v.shortName + "\n" + v);
      }
    }
  }
}
 
Example 6
Source File: DefaultConventions.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * look for aliases.
 *
 * @param axisType look for this axis type
 * @return name of axis of that type
 */
@Nullable
private String findCoordinateName(AxisType axisType) {
  for (Variable.Builder vb : rootGroup.vbuilders) {
    if (vb instanceof VariableDS.Builder) {
      VariableDS.Builder vds = (VariableDS.Builder) vb;
      if (axisType == getAxisType(vds)) {
        return vds.getFullName();
      }
    }
  }
  return null;
}
 
Example 7
Source File: ZebraConvention.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
protected void augmentDataset(CancelTask cancelTask) throws IOException {
  NcMLReaderNew.wrapNcMLresource(datasetBuilder, CoordSystemFactory.resourcesDir + "Zebra.ncml", cancelTask);

  // special time handling
  // the time coord var is created in the NcML
  // set its values = base_time + time_offset(time)
  Dimension timeDim = rootGroup.findDimension("time").orElse(null);
  VariableDS.Builder base_time = (VariableDS.Builder) rootGroup.findVariableLocal("base_time").orElse(null);
  VariableDS.Builder time_offset = (VariableDS.Builder) rootGroup.findVariableLocal("time_offset").orElse(null);
  Variable.Builder time = rootGroup.findVariableLocal("time").orElse(null);
  if ((timeDim == null) || (base_time == null) || (time_offset == null) || (time == null))
    return;

  String units =
      base_time.getAttributeContainer().findAttributeString(CDM.UNITS, "seconds since 1970-01-01 00:00 UTC");
  time.addAttribute(new Attribute(CDM.UNITS, units));

  Array data;
  try {
    double baseValue = base_time.orgVar.readScalarDouble();

    data = time_offset.orgVar.read();
    IndexIterator iter = data.getIndexIterator();
    while (iter.hasNext())
      iter.setDoubleCurrent(iter.getDoubleNext() + baseValue);

  } catch (IOException ioe) {
    parseInfo.format("ZebraConvention failed to create time Coord Axis for file %s err= %s%n",
        datasetBuilder.location, ioe);
    return;
  }

  time.setCachedData(data, true);
}
 
Example 8
Source File: NetcdfFormatWriter.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/** Add a Variable to the root group. */
public Variable.Builder addVariable(String shortName, DataType dataType, List<Dimension> dims) {
  if (!isNewFile && !useJna) {
    throw new UnsupportedOperationException("Cant add variable to existing netcdf-3 files");
  }
  Variable.Builder vb = Variable.builder().setName(shortName).setDataType(dataType).setParentGroupBuilder(rootGroup)
      .setDimensions(dims);
  rootGroup.addVariable(vb);
  return vb;
}
 
Example 9
Source File: CoordSystemBuilder.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void addVariables(Group.Builder group) {
  for (Variable.Builder vb : group.vbuilders) {
    if (vb instanceof VariableDS.Builder) {
      varList.add(new VarProcess(group, (VariableDS.Builder) vb));
    } else if (vb instanceof StructureDS.Builder) {
      addStructure(group, (StructureDS.Builder) vb);
    }
  }

  for (Group.Builder nested : group.gbuilders) {
    addVariables(nested);
  }
}
 
Example 10
Source File: CoordinatesHelper.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private Optional<CoordinateAxis.Builder> findAxisByVerticalSearch(Variable.Builder vb, String shortName) {
  Optional<Variable.Builder<?>> axis = vb.getParentGroupBuilder().findVariableOrInParent(shortName);
  if (axis.isPresent()) {
    if (axis.get() instanceof CoordinateAxis.Builder) {
      return Optional.of((CoordinateAxis.Builder) axis.get());
    }
  }
  return Optional.empty();
}
 
Example 11
Source File: NetcdfFormatWriter.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/** Add a Variable to the root group. */
public Variable.Builder addVariable(String shortName, DataType dataType, String dimString) {
  if (!isNewFile && !useJna) {
    throw new UnsupportedOperationException("Cant add variable to existing netcdf-3 files");
  }
  Variable.Builder vb = Variable.builder().setName(shortName).setDataType(dataType).setParentGroupBuilder(rootGroup)
      .setDimensionsByName(dimString);
  rootGroup.addVariable(vb);
  return vb;
}
 
Example 12
Source File: Aggregation.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void setDatasetAcquireProxy(AggProxyReader proxy, Group.Builder g) throws IOException {

    // all normal (non agg) variables must use a proxy to lock the file
    for (Variable.Builder v : g.vbuilders) {

      if (v.proxyReader != v && v.proxyReader != null) {
        if (debugProxy)
          System.out.println(" debugProxy: hasProxyReader " + v.shortName);
        continue; // dont mess with agg variables
      }
      /*
       * LOOK no caching
       * if (v.isCaching()) { // cache the small ones
       * v.setCachedData(v.read()); // cache the variableDS directly
       * 
       * } else { // put proxy on the rest
       */
      v.setProxyReader(proxy);
      if (debugProxy)
        System.out.println(" debugProxy: set proxy on " + v.shortName);
    }

    // recurse
    for (Group.Builder nested : g.gbuilders) {
      setDatasetAcquireProxy(proxy, nested);
    }
  }
 
Example 13
Source File: NcStream.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private static Variable.Builder decodeVar(NcStreamProto.Variable var) {
  DataType varType = convertDataType(var.getDataType());
  Variable.Builder ncvar = Variable.builder().setName(var.getName()).setDataType(varType);

  if (varType.isEnum()) {
    ncvar.setEnumTypeName(var.getEnumType());
  }

  // The Dimensions are stored redunantly in the Variable.
  // If shared, they must also exist in a parent Group. However, we dont yet have the Groups wired together,
  // so that has to wait until build().
  List<Dimension> dims = new ArrayList<>(6);
  Section.Builder section = Section.builder();
  for (ucar.nc2.stream.NcStreamProto.Dimension dim : var.getShapeList()) {
    dims.add(decodeDim(dim));
    section.appendRange((int) dim.getLength());
  }
  ncvar.addDimensions(dims);

  for (ucar.nc2.stream.NcStreamProto.Attribute att : var.getAttsList())
    ncvar.addAttribute(decodeAtt(att));

  if (!var.getData().isEmpty()) {
    // LOOK may mess with ability to change var size later.
    ByteBuffer bb = ByteBuffer.wrap(var.getData().toByteArray());
    Array data = Array.factory(varType, section.build().getShape(), bb);
    ncvar.setCachedData(data, true);
  }

  return ncvar;
}
 
Example 14
Source File: NUWGConvention.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private boolean makeCoordinateAxis(Variable.Builder ncvar, Dimension dim) {
  if (ncvar.getRank() != 1)
    return false;
  String vdimName = ncvar.getFirstDimensionName();
  if (!vdimName.equals(dim.getShortName()))
    return false;

  if (!dim.getShortName().equals(ncvar.shortName)) {
    ncvar.addAttribute(new Attribute(_Coordinate.AliasForDimension, dim.getShortName()));
  }
  return true;
}
 
Example 15
Source File: CFGridCoverageWriter.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void addCoordTransforms(CoverageCollection subsetDataset, Group.Builder group) {
  for (CoverageTransform ct : subsetDataset.getCoordTransforms()) {
    // scalar coordinate transform variable - container for transform info
    Variable.Builder ctv = Variable.builder().setName(ct.getName()).setDataType(DataType.INT);
    group.addVariable(ctv);
    ctv.addAttributes(ct.attributes());
  }
}
 
Example 16
Source File: NetcdfCopier.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private Variable.Builder copyVariable(Group.Builder parent, Variable oldVar) throws IOException {
  Variable.Builder vb;
  DataType newType = oldVar.getDataType();
  String dimNames = Dimensions.makeDimensionsString(oldVar.getDimensions());

  if (newType == DataType.STRUCTURE) {
    Structure oldStruct = (Structure) oldVar;
    Structure.Builder sb = Structure.builder().setName(oldVar.getShortName());
    for (Variable nested : oldStruct.getVariables()) {
      sb.addMemberVariable(copyVariable(parent, nested));
    }
    vb = sb;
  } else {
    vb = Variable.builder().setName(oldVar.getShortName()).setDataType(newType);
    if (!extended && newType == DataType.STRING) {
      // find maximum length
      Array data = oldVar.read();
      IndexIterator ii = data.getIndexIterator();
      int max_len = 0;
      while (ii.hasNext()) {
        String s = (String) ii.getObjectNext();
        max_len = Math.max(max_len, s.length());
      }

      // add last dimension
      String strlenDimName = oldVar.getShortName() + "_strlen";
      parent.addDimension(Dimension.builder(strlenDimName, max_len).setIsShared(false).build());

      newType = DataType.CHAR;
      vb.setDataType(DataType.CHAR);
      dimNames += " " + strlenDimName;
    }
  }
  vb.setParentGroupBuilder(parent).setDimensionsByName(dimNames);

  if (newType.isEnum()) {
    EnumTypedef en = oldVar.getEnumTypedef();
    vb.setEnumTypeName(en.getShortName());
  }

  // attributes
  for (Attribute att : oldVar.attributes()) {
    vb.addAttribute(convertAttribute(att));
    if (debug) {
      System.out.println("add varAtt= " + att);
    }
  }

  return vb;
}
 
Example 17
Source File: NcMLReaderNew.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Read the NcML variable element, and nested elements.
 *
 * @param groupBuilder put dimension into this group
 * @param refGroup parent Group in referenced dataset, may be null
 * @param varElem ncml variable element
 */
private void readVariable(Group.Builder groupBuilder, @Nullable Group refGroup, Element varElem) {
  String name = varElem.getAttributeValue("name");
  if (name == null) {
    errlog.format("NcML Variable name is required (%s)%n", varElem);
    return;
  }
  String nameInFile = Optional.ofNullable(varElem.getAttributeValue("orgName")).orElse(name);

  DataType dtype = null;
  String typeS = varElem.getAttributeValue("type");
  if (typeS != null) {
    dtype = DataType.getType(typeS);
  }

  // see if it already exists
  Variable refv = (refGroup == null) ? null : refGroup.findVariableLocal(nameInFile);
  Optional<Variable.Builder<?>> addedFromAgg = groupBuilder.findVariableLocal(nameInFile);
  if (refv == null && !addedFromAgg.isPresent()) { // new
    if (dtype == null) {
      errlog.format("NcML Variable dtype is required for new variable (%s)%n", name);
      return;
    }
    if (dtype == DataType.STRUCTURE || dtype == DataType.SEQUENCE) {
      groupBuilder.addVariable(readStructureNew(groupBuilder, varElem));
    } else {
      groupBuilder.addVariable(readVariableNew(groupBuilder, dtype, varElem));
    }
    return;
  }

  // refv exists
  if (refv != null) {
    if (dtype == null) {
      dtype = refv.getDataType();
    }
    if (dtype == DataType.STRUCTURE || dtype == DataType.SEQUENCE) {
      readStructureExisting(groupBuilder, null, dtype, (Structure) refv, varElem)
          .ifPresent(groupBuilder::addVariable);
    } else {
      readVariableExisting(groupBuilder, null, dtype, refv, varElem).ifPresent(groupBuilder::addVariable);
    }
    return;
  }

  // refv does not exist, but addedFromAgg may be present
  DataType finalDtype = dtype;
  addedFromAgg.ifPresent(agg -> {
    if (agg instanceof VariableDS.Builder<?>) {
      VariableDS.Builder<?> aggDs = (VariableDS.Builder<?>) agg;
      aggDs.setOriginalName(nameInFile);
    }
    DataType reallyFinalDtype = finalDtype != null ? finalDtype : agg.dataType;
    augmentVariableNew(agg, reallyFinalDtype, varElem);
  });
}
 
Example 18
Source File: H4header.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private void addVariableToGroup(Group.Builder g, Variable.Builder v, Tag tag) {
  g.findVariableLocal(v.shortName).ifPresent(varExisting -> v.setName(v.shortName + tag.refno)); // disambiguate
  g.addVariable(v);
  tag.vinfo.group = g;
}
 
Example 19
Source File: H4header.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
void setVariable(Variable.Builder v) {
  this.v = v;
  v.setSPobject(this);
}
 
Example 20
Source File: HdfHeaderIF.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License votes vote down vote up
String readStructMetadata(Variable.Builder<?> structMetadataVar) throws IOException;