Java Code Examples for ucar.nc2.Dimension

The following examples show how to use ucar.nc2.Dimension. These examples are extracted from open source projects. 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
void makeChildren() {
  children = new ArrayList<>();

  List dims = group.getDimensions();
  for (Object dim : dims) {
    children.add(new DimensionNode(this, (Dimension) dim));
  }

  for (Variable var : group.getVariables()) {
    children.add(new VariableNode(this, var));
  }

  List groups = group.getGroups();
  for (Object group1 : groups) {
    children.add(new GroupNode(this, (Group) group1));
  }
}
 
Example 2
@Nullable
private CoordinateAxis.Builder makeXCoordAxis(String axisName, String dimName) {
  Optional<Dimension> dimOpt = rootGroup.findDimension(dimName);
  if (!dimOpt.isPresent()) {
    return null;
  }
  Dimension dim = dimOpt.get();
  double dx = findAttributeDouble("DX") / 1000.0; // km ya just gotta know
  int nx = dim.getLength();
  double startx = centerX - dx * (nx - 1) / 2; // ya just gotta know

  CoordinateAxis.Builder v = CoordinateAxis1D.builder().setName(axisName).setDataType(DataType.DOUBLE)
      .setParentGroupBuilder(rootGroup).setDimensionsByName(dim.getShortName()).setUnits("km")
      .setDesc("synthesized GeoX coordinate from DX attribute");
  v.setAutoGen(startx, dx);
  v.setAxisType(AxisType.GeoX);
  v.addAttribute(new Attribute(_Coordinate.AxisType, "GeoX"));
  if (!axisName.equals(dim.getShortName()))
    v.addAttribute(new Attribute(_Coordinate.AliasForDimension, dim.getShortName()));

  if (gridE)
    v.addAttribute(new Attribute(_Coordinate.Stagger, CDM.ARAKAWA_E));
  return v;
}
 
Example 3
/**
 * Parse the file header information about the file (e.g., file ID, dataset ID,
 * record size, number of records) and create netCDF attributes and dimensions
 * where appropriate.
 *
 * @throws IOException if any problems reading the file (or validating the file).
 */
private void handleFileInformation() throws IOException {
  fileIdAtt = new Attribute(this.fileIdAttName, headerInfo.get(HeaderInfoTitle.FILE_ID.toString()));
  datasetIdAtt = new Attribute(this.datasetIdAttName, headerInfo.get(HeaderInfoTitle.DATA_SET_ID.toString()));
  recordSizeInBytes = Integer.parseInt(headerInfo.get(HeaderInfoTitle.RECORD_BYTES.toString()));
  numRecords = Integer.parseInt(headerInfo.get(HeaderInfoTitle.NUM_RECORDS.toString()));
  numHeaderRecords = Integer.parseInt(headerInfo.get(HeaderInfoTitle.NUM_HEADER_RECORDS.toString()));
  numDataRecords = Integer.parseInt(headerInfo.get(HeaderInfoTitle.NUM_DATA_RECORDS.toString()));
  numDataRecordsDim = new Dimension(this.numDataRecordsDimName, numDataRecords, true, true, false);
  numArtificialDataRecords = Integer.parseInt(headerInfo.get(HeaderInfoTitle.NUM_ARTIFICIAL_DATA_RECORDS.toString()));
  this.headerSizeInBytes = this.numHeaderRecords * this.recordSizeInBytes;
  if (numRecords * ((long) this.recordSizeInBytes) != this.actualSize) {
    throw new IOException("Invalid DMSP file: the number of records <" + this.numRecords + "> times the record size <"
        + this.recordSizeInBytes + "> does not equal the size of the file <" + this.actualSize + ">.");
  }
}
 
Example 4
private String addDimension(Group.Builder parent, H5Group h5group, String name, int length, boolean isUnlimited) {
  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) { // create if not found
    d = Dimension.builder().setName(name).setIsUnlimited(isUnlimited).setLength(length).build();
    h5group.dimMap.put(dimName, d);
    h5group.dimList.add(d);
    parent.addDimension(d);
    if (debugDimensionScales) {
      log.debug("addDimension name=" + name + " dim= " + d + " to group " + parent.shortName);
    }

  } else { // check has correct length
    if (d.getLength() != length)
      throw new IllegalStateException(
          "addDimension: DimScale has different length than dimension it references dimScale=" + dimName);
  }

  return d.getShortName();
}
 
Example 5
@Override
protected boolean identifyEncodingProfile(NetcdfDataset ds, EncodingInfo info, Formatter errlog) {
  Evaluator.VarAtt varatt = Evaluator.findVariableWithAttribute(ds, CF.SAMPLE_DIMENSION);
  if (varatt == null)
    return false;
  String dimName = varatt.att.getStringValue();
  Dimension obsDim = ds.findDimension(dimName);

  Structure profile = info.lat.getParentStructure();
  if (profile.getRank() == 0) { // could be scalar
    info.set(Encoding.single, null, obsDim);
  }
  Dimension profileDim = profile.getDimension(0);

  // now find the child structure
  info.childStruct = Evaluator.findStructureWithDimensions(ds, obsDim, null);

  // the raggeds
  if (identifyRaggeds(ds, info, profileDim, obsDim, errlog))
    return true;

  errlog.format("CFpointObsExt: %s only supports ragged array representation%n", CF.FeatureType.profile);
  return false;
}
 
Example 6
private void testOneStrategy(Dimension[] shape, int maxChunkElems, long expectSize) {
  List<Dimension> dims = Arrays.asList(shape);
  show("shape", dims);
  System.out.printf(" max = %d%n", maxChunkElems);
  Nc4ChunkingDefault chunker = new Nc4ChunkingDefault();
  chunker.setDefaultChunkSize(maxChunkElems);
  chunker.setMinChunksize(maxChunkElems);

  int[] result = chunker.computeUnlimitedChunking(dims, 1);
  show("chunk", result);
  long shapeSize = new Section(result).computeSize();
  System.out.printf(" size = %d%n%n", shapeSize);
  assert shapeSize <= maxChunkElems;
  assert shapeSize >= maxChunkElems / 2;
  assert shapeSize == expectSize : shapeSize + " != " + expectSize;
}
 
Example 7
@Override
public String toString() {
  Formatter f2 = new Formatter();
  f2.format("%s", type == null ? "" : type.toString());
  if (type == null) {
    f2.close();
    return "";
  }

  f2.format("}");
  f2.format("%n allAxes=(");
  for (CoordinateAxis axis : allAxes)
    f2.format("%s, ", axis.getShortName());
  f2.format(") {");
  for (Dimension dim : CoordinateSystem.makeDomain(allAxes))
    f2.format("%s, ", dim.getShortName());
  f2.format("}%n");
  String stringRepres = f2.toString();
  f2.close();
  return stringRepres;
}
 
Example 8
Source Project: OpenDA   Source File: NetcdfUtils.java    License: GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Creates metadata and data variables for the given exchangeItems and/or ensembleExchangeItems in the given netcdfFile, if not present yet.
 * Each exchangeItem stores a scalar timeseries for a single location (and a single ensemble member).
 * All exchangeItems must use the same ensemble member indices.
 */
public static void createMetadataAndDataVariablesForScalars(NetcdfFileWriter netcdfFileWriter,
															List<IExchangeItem> exchangeItems, Map<String, Map<Integer, IExchangeItem>> ensembleExchangeItems, Map<ITimeInfo, Dimension> timeInfoTimeDimensionMap, String stationNameVarName,
															String stationDimensionVarName,
															int stationCount, int ensembleMemberCount) {

	//create stations variable.
	Dimension stationDimension = createStationsVariable(netcdfFileWriter, stationNameVarName, stationDimensionVarName, stationCount);

	//create realization variable.
	Dimension realizationDimension = null;
	if (ensembleMemberCount > 0) {
		realizationDimension = createRealizationVariable(netcdfFileWriter, ensembleMemberCount);
	}

	//create data variables.
	NetcdfUtils.createDataVariables(netcdfFileWriter, exchangeItems, ensembleExchangeItems, realizationDimension, stationDimension, timeInfoTimeDimensionMap, null);
}
 
Example 9
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 10
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 11
Source Project: netcdf-java   Source File: NcStream.java    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private static NcStreamProto.Structure.Builder encodeStructure(Structure s) throws IOException {
  NcStreamProto.Structure.Builder builder = NcStreamProto.Structure.newBuilder();
  builder.setName(s.getShortName());
  builder.setDataType(convertDataType(s.getDataType()));

  for (Dimension dim : s.getDimensions())
    builder.addShape(encodeDim(dim));

  for (Attribute att : s.attributes())
    builder.addAtts(encodeAtt(att));

  for (Variable v : s.getVariables()) {
    if (v instanceof Structure)
      builder.addStructs(NcStream.encodeStructure((Structure) v));
    else
      builder.addVars(NcStream.encodeVar(v, -1));
  }

  return builder;
}
 
Example 12
@Test
public void testChunkingStrategy() {
  Dimension d2 = new Dimension("2", 2);
  Dimension d10 = new Dimension("10", 10);
  Dimension d20 = new Dimension("20", 20);
  Dimension dun = new Dimension("u", 0, true, true, false);

  testOneStrategy(new Dimension[] {dun, d10, d20}, 50, 40);
  testOneStrategy(new Dimension[] {dun, d10, d20}, 500, 400);
  testOneStrategy(new Dimension[] {dun}, 500, 500);
  testOneStrategy(new Dimension[] {dun, d2}, 101, 100);
  testOneStrategy(new Dimension[] {dun, d10}, 777, 770);
  testOneStrategy(new Dimension[] {dun, dun}, 100, 100);
  testOneStrategy(new Dimension[] {dun, d10, dun}, 100, 90);
  testOneStrategy(new Dimension[] {dun, dun, d2}, 100, 98);
  testOneStrategy(new Dimension[] {dun, dun, d2}, 400, 392);
  testOneStrategy(new Dimension[] {dun, dun, dun}, 400, 343);
}
 
Example 13
@Override
void makeFeatureVariables(StructureData trajData, boolean isExtended) {
  // add the dimensions : extended model can use an unlimited dimension
  Dimension trajDim = writerb.addDimension(trajDimName, ntraj);

  List<VariableSimpleIF> trajVars = new ArrayList<>();

  trajVars.add(VariableSimpleBuilder.makeString(trajIdName, "trajectory identifier", null, traj_strlen)
      .addAttribute(CF.CF_ROLE, CF.TRAJECTORY_ID).build());

  for (StructureMembers.Member m : trajData.getMembers()) {
    if (findDataVar(m.getName()) != null)
      trajVars.add(VariableSimpleBuilder.fromMember(m).build());
  }

  if (isExtended) {
    Structure.Builder structb = writerb.addStructure(trajStructName, trajDimName);
    addCoordinatesExtended(structb, trajVars);
  } else {
    addCoordinatesClassic(trajDim, trajVars, trajVarMap);
  }

}
 
Example 14
private static void read(GridDatatype gdt, Count count, int rtIndex, int tIndex, Dimension zDim) throws IOException {
  if (zDim != null) {
    for (int z = 0; z < zDim.getLength(); z++)
      read(gdt, count, rtIndex, tIndex, z);
  } else {
    read(gdt, count, rtIndex, tIndex, -1);
  }
}
 
Example 15
private void addExtraVariables() {
  if (extra == null)
    return;

  addDimensionsClassic(extra);

  for (VariableSimpleIF vs : extra) {
    List<Dimension> dims = makeDimensionList(vs.getDimensions());
    writerb.addVariable(vs.getShortName(), vs.getDataType(), dims).addAttributes(vs.attributes());
  }
}
 
Example 16
/**
 * Find structure variable of rank 2 with the 2 given dimensions
 * (or) Find structure variable of rank 1 with the 1 given dimension
 * 
 * @param ds in this dataset
 * @param dim0 first dimension
 * @param dim1 second dimension (ok to be null)
 * @return structure variable or null
 */
public static Structure findStructureWithDimensions(NetcdfDataset ds, Dimension dim0, Dimension dim1) {
  for (Variable v : ds.getVariables()) {
    if (!(v instanceof Structure))
      continue;

    if (dim1 != null && v.getRank() == 2 && v.getDimension(0).equals(dim0) && v.getDimension(1).equals(dim1))
      return (Structure) v;

    if (dim1 == null && v.getRank() == 1 && v.getDimension(0).equals(dim0))
      return (Structure) v;
  }
  return null;
}
 
Example 17
private static void read(GridDatatype gdt, Count count, int rtIndex, Dimension timeDim, Dimension zDim)
    throws IOException {
  if (timeDim != null) {
    for (int t = 0; t < timeDim.getLength(); t++)
      read(gdt, count, rtIndex, t, zDim);
  } else {
    read(gdt, count, rtIndex, -1, zDim);
  }
}
 
Example 18
private static CoverageCoordAxis makeCoordAxisFromDimension(Dimension dim) {
  CoverageCoordAxisBuilder builder = new CoverageCoordAxisBuilder();
  builder.name = dim.getFullName();
  builder.dataType = DataType.INT;
  builder.axisType = AxisType.Dimension;
  builder.dependenceType = CoverageCoordAxis.DependenceType.dimension;
  builder.spacing = CoverageCoordAxis.Spacing.regularPoint;
  builder.ncoords = dim.getLength();
  builder.startValue = 0;
  builder.endValue = dim.getLength() - 1;
  builder.resolution = 1;

  return new CoverageCoordAxis1D(builder);
}
 
Example 19
private int findDimension(Dimension want) {
  java.util.List dims = vs.getDimensions();
  for (int i = 0; i < dims.size(); i++) {
    Dimension d = (Dimension) dims.get(i);
    if (d.equals(want))
      return i;
  }
  return -1;
}
 
Example 20
Source Project: netcdf-java   Source File: FslRaob.java    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void makeMultidimInner(NetcdfDataset ds, TableConfig parentTable, TableConfig childTable, String outerDin,
    String innerDim) {
  Dimension parentDim = ds.findDimension(outerDin);
  Dimension childDim = ds.findDimension(innerDim);

  // divide up the variables between the parent and the child
  List<String> obsVars;
  List<Variable> vars = ds.getVariables();
  List<String> parentVars = new ArrayList<>(vars.size());
  obsVars = new ArrayList<>(vars.size());
  for (Variable orgV : vars) {
    if (orgV instanceof Structure)
      continue;

    Dimension dim0 = orgV.getDimension(0);
    if ((dim0 != null) && dim0.equals(parentDim)) {
      if ((orgV.getRank() == 1) || ((orgV.getRank() == 2) && orgV.getDataType() == DataType.CHAR)) {
        parentVars.add(orgV.getShortName());
      } else {
        Dimension dim1 = orgV.getDimension(1);
        if ((dim1 != null) && dim1.equals(childDim))
          obsVars.add(orgV.getShortName());
      }
    }
  }
  parentTable.vars = parentVars;
  childTable.vars = obsVars;
}
 
Example 21
private void testDomain(String which, List<Dimension> domain, List<CoordinateAxis> axes) {
  for (CoordinateAxis axis : axes) {
    List<Dimension> dims = axis.getDimensions();
    for (Dimension d : dims)
      if (!domain.contains(d)) {
        System.err.printf("    %s: illegal dimension '%s' in axis %s%n", which, d.getFullName(),
            axis.getNameAndDimensions());
        assert false;
      }
  }
}
 
Example 22
Source Project: sis   Source File: GridWrapper.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new grid geometry with the same coordinate system than the given parent.
 */
private GridWrapper(final GridWrapper parent, final List<Dimension> dimensions) {
    netcdfCS  = parent.netcdfCS;
    domain    = dimensions;
    reordered = parent.reordered;
    assert netcdfCS.getDomain().containsAll(dimensions);
}
 
Example 23
Row(GridDatatype gg) {
  this.gg = gg;
  Formatter f = new Formatter();
  for (Dimension dim : gg.getDimensions())
    f.format("%s ", dim.getShortName());
  dims = f.toString();
}
 
Example 24
public int[] computeUnlimitedChunking(List<Dimension> dims, int elemSize) {
  int maxElements = defaultChunkSize / elemSize;
  int[] result = fillRightmost(convertUnlimitedShape(dims), maxElements);
  long resultSize = new Section(result).computeSize();
  if (resultSize < minChunksize) {
    maxElements = minChunksize / elemSize;
    result = incrUnlimitedShape(dims, result, maxElements);
  }

  return result;
}
 
Example 25
public Object isMine(FeatureType wantFeatureType, NetcdfDataset ncd, Formatter errlog) {
  String format = ncd.getRootGroup().findAttributeString("format", null);
  if (format != null) {
    if (format.startsWith("nssl/netcdf"))
      return this;
  }

  Dimension az = ncd.findDimension("Azimuth");
  Dimension gt = ncd.findDimension("Gate");
  if ((null != az) && (null != gt)) {
    return this;
  }
  return null;
}
 
Example 26
/** 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 27
public TableConfig getConfig(FeatureType wantFeatureType, NetcdfDataset ds, Formatter errlog) {
  Dimension obsDim = ds.findDimension("time");
  if (obsDim == null) {
    CoordinateAxis axis = CoordSysEvaluator.findCoordByType(ds, AxisType.Time);
    if ((axis != null) && axis.isScalar())
      obsDim = axis.getDimension(0);
  }

  if (obsDim == null) {
    errlog.format("Must have an Observation dimension: unlimited dimension, or from Time Coordinate");
    return null;
  }
  boolean hasStruct = Evaluator.hasNetcdf3RecordStructure(ds);



  // otherwise, make it a Station
  TableConfig nt = new TableConfig(Table.Type.Top, "station");
  nt.featureType = FeatureType.STATION;

  nt.lat = CoordSysEvaluator.findCoordNameByType(ds, AxisType.Lat);
  nt.lon = CoordSysEvaluator.findCoordNameByType(ds, AxisType.Lon);

  nt.stnId = ds.getRootGroup().findAttributeString("station_name", null);
  nt.stnWmoId = ds.getRootGroup().findAttributeString("wmo_id", null);

  nt.stnDesc = ds.getRootGroup().findAttributeString("description", null);
  if (nt.stnDesc == null)
    nt.stnDesc = ds.getRootGroup().findAttributeString("comment", null);

  TableConfig obs = new TableConfig(Table.Type.Structure, hasStruct ? "record" : obsDim.getShortName());
  obs.structName = "record";
  obs.structureType = hasStruct ? TableConfig.StructureType.Structure : TableConfig.StructureType.PsuedoStructure;
  obs.dimName = obsDim.getShortName();
  obs.time = CoordSysEvaluator.findCoordNameByType(ds, AxisType.Time);
  nt.addChild(obs);

  return nt;
}
 
Example 28
Source Project: sis   Source File: DimensionWrapper.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Unwraps all given dimensions.
 */
static Dimension[] unwrap(final org.apache.sis.internal.netcdf.Dimension[] dimensions) {
    final Dimension[] ncd = new Dimension[dimensions.length];
    for (int i=0; i<ncd.length; i++) {
        ncd[i] = ((DimensionWrapper) dimensions[i]).netcdf;
    }
    return ncd;
}
 
Example 29
@Override
protected void makeMiddleVariables(StructureData profileData, boolean isExtended) {
  Dimension profileDim = writerb.addDimension(profileDimName, nfeatures);

  // add the profile Variables using the profile dimension
  List<VariableSimpleIF> profileVars = new ArrayList<>();
  profileVars.add(VariableSimpleBuilder.makeString(profileIdName, "profile identifier", null, id_strlen)
      .addAttribute(CF.CF_ROLE, CF.PROFILE_ID) // profileId:cf_role = "profile_id";
      .addAttribute(CDM.MISSING_VALUE, String.valueOf(idMissingValue)).build());

  profileVars
      .add(VariableSimpleBuilder.makeScalar(latName, "profile latitude", CDM.LAT_UNITS, DataType.DOUBLE).build());
  profileVars
      .add(VariableSimpleBuilder.makeScalar(lonName, "profile longitude", CDM.LON_UNITS, DataType.DOUBLE).build());
  profileVars.add(VariableSimpleBuilder
      .makeScalar(profileTimeName, "nominal time of profile", timeUnit.getUdUnit(), DataType.DOUBLE)
      .addAttribute(CF.CALENDAR, timeUnit.getCalendar().toString()).build());

  profileVars.add(
      VariableSimpleBuilder.makeScalar(trajectoryIndexName, "trajectory index for this profile", null, DataType.INT)
          .addAttribute(CF.INSTANCE_DIMENSION, trajDimName).build());

  profileVars
      .add(VariableSimpleBuilder.makeScalar(numberOfObsName, "number of obs for this profile", null, DataType.INT)
          .addAttribute(CF.SAMPLE_DIMENSION, recordDimName).build());

  for (StructureMembers.Member m : profileData.getMembers()) {
    VariableSimpleIF dv = findDataVar(m.getName());
    if (dv != null)
      profileVars.add(dv);
  }

  if (isExtended) {
    Structure.Builder structb = writerb.addStructure(profileStructName, profileDimName);
    addCoordinatesExtended(structb, profileVars);
  } else {
    addCoordinatesClassic(profileDim, profileVars, profileVarMap);
  }
}
 
Example 30
Source Project: netcdf-java   Source File: NcStream.java    License: 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;
}