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

The following examples show how to use ucar.nc2.Variable#getShape() . 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 Array readData(Variable v, int stationIndex, int obsIndex) throws IOException {
  int[] shape = v.getShape();
  int[] origin = new int[v.getRank()];
  origin[0] = stationIndex;
  origin[1] = obsIndex;
  shape[0] = 1;
  shape[1] = 1;

  Array data = null;
  try {
    data = v.read(origin, shape);
  } catch (InvalidRangeException e) {
    throw new IllegalStateException(e.getMessage());
  }
  return data;
}
 
Example 2
/**
 * Extract an Array (with rank reduced by one) from the Variable
 * for the given time index.
 *
 * @param v variable to extract from
 * @param timeIndex time index
 * @return Array of data
 * @throws IOException problem getting Array
 */
private Array getTimeSlice(Variable v, int timeIndex) throws IOException {
  // ADD: this would make a good utility method
  // ADD: use Array.slice?
  int[] shape = v.getShape();
  int[] origin = new int[v.getRank()];

  if (getTimeDimension() != null) {
    int dimIndex = v.findDimensionIndex(getTimeDimension().getShortName());
    if (dimIndex >= 0) {
      shape[dimIndex] = 1;
      origin[dimIndex] = timeIndex;
    }
  }

  try {
    return v.read(origin, shape).reduce();
  } catch (InvalidRangeException e) {
    throw new IOException(e);
  }
}
 
Example 3
public static void writeDataForVariableForSingleTimeSingleLocationSingleRealization(NetcdfFileWriter netcdfFileWriter, Variable variable,
		int timeIndex, int realizationDimensionIndex, int realizationIndex, int stationDimensionIndex, int stationIndex, double[] values) {
	int[] origin = createOrigin(variable);
	int[] sizeArray = variable.getShape();

	//here assume that timeDimensionIndex is 0.
	int timeDimensionIndex = 0;
	//select only the given time, station and realization.
	origin[timeDimensionIndex] = timeIndex;
	sizeArray[timeDimensionIndex] = 1;
	origin[realizationDimensionIndex] = realizationIndex;
	sizeArray[realizationDimensionIndex] = 1;
	origin[stationDimensionIndex] = stationIndex;
	sizeArray[stationDimensionIndex] = 1;

	writeSelectedData(netcdfFileWriter, variable, origin, sizeArray, values);
}
 
Example 4
private Netcdf2Variable(NetcdfDataset nds, Variable v0) {
  super(v0.getShortName(), v0);

  sweeps = new ArrayList<>();
  nsweeps = 0;
  int[] shape = v0.getShape();
  int count = v0.getRank() - 1;

  int ngates = shape[count];
  count--;
  int nrays = shape[count];
  count--;

  if (shape.length == 3)
    nsweeps = shape[count];
  else
    nsweeps = 1;

  for (int i = 0; i < nsweeps; i++)
    sweeps.add(new Netcdf2Sweep(v0, i, nrays, ngates));

}
 
Example 5
public static Array getArrayFromMember(Variable var, Member m) {

    // DataType m_dt = writer.findVariable(m.getName()).getDataType();
    DataType v_dt = var.getDataType();
    // DataType m_dt = m.getDataType();

    // Writes one single data
    // int[] shape = writer.findVariable(m.getName()).getShape();
    int[] shape = var.getShape();

    // Set the shape we really want
    for (int i = 0; i < shape.length; i++) {
      shape[i] = 1;
    }

    Array arr = Array.factory(v_dt, shape);
    setDataArray(v_dt, arr, m);

    return arr;

  }
 
Example 6
private Array readStationVariable(Variable svar) throws IOException {
  if (svar.getRank() == 1)
    return svar.read();
  if (svar.getRank() == 2) {
    int[] shape = svar.getShape();
    shape[1] = 1;
    try {
      return svar.read(new int[2], shape).reduce(1);
    } catch (InvalidRangeException e) {
      throw new IllegalStateException(e.getMessage());
    }
  }
  throw new IllegalStateException("Station variables must have rank 1 or 2");
}
 
Example 7
static Section makeSubset(Variable v) throws InvalidRangeException {
  int[] shape = v.getShape();
  shape[0] = 1;
  Section s = new Section(shape);
  long size = s.computeSize();
  shape[0] = (int) Math.max(1, max_size / size);
  return new Section(shape);
}
 
Example 8
public <T extends NetcdfFile> void testG16RadVar(T nc) throws IOException {
  // find variable "Rad"
  Variable radiance = nc.findVariable("Rad");
  Assert.assertNotNull(radiance);

  // read full array
  Array array = radiance.read();
  assertThat(array.getRank()).isEqualTo(2);

  // check shape of array is the same as the shape of the variable
  int[] variableShape = radiance.getShape();
  int[] arrayShape = array.getShape();
  assertThat(variableShape).isEqualTo(arrayShape);
}
 
Example 9
public static double[] readDataForVariableFor2DGridForSingleTimeAndRealization(Variable variable,
	   int realizationDimensionIndex, int realizationIndex, int timeDimensionIndex, int timeIndex, int dimensionIndexToFlip) {
	int[] origin = createOrigin(variable);
	int[] sizeArray = variable.getShape();

	//select only the given time.
	origin[timeDimensionIndex] = timeIndex;
	sizeArray[timeDimensionIndex] = 1;
	//select only the given realization
	origin[realizationDimensionIndex] = realizationIndex;
	sizeArray[realizationDimensionIndex] = 1;
	return readSelectedData(variable, origin, sizeArray, dimensionIndexToFlip);
}
 
Example 10
private void createNewVariables(NetcdfDataset ds, Variable ncVar, List<Dimension> newDims, Dimension levelDim)
    throws InvalidRangeException {

  List<Dimension> dims = ncVar.getDimensions();
  int newDimIndex = dims.indexOf(levelDim);
  // String shapeS = ncVar.getShapeS();

  int[] origin = new int[ncVar.getRank()];
  int[] shape = ncVar.getShape();
  int count = 0;
  for (Dimension dim : newDims) {
    String name = ncVar.getShortName() + "-" + dim.getShortName();

    origin[newDimIndex] = count;
    shape[newDimIndex] = dim.getLength();

    Variable varNew = ncVar.section(new Section(origin, shape));
    varNew.setName(name);
    varNew.setDimension(newDimIndex, dim);

    // synthesize long name
    String long_name = ds.findAttValueIgnoreCase(ncVar, CDM.LONG_NAME, ncVar.getShortName());
    long_name = long_name + "-" + dim.getShortName();
    ds.addVariableAttribute(varNew, new Attribute(CDM.LONG_NAME, long_name));

    ds.addVariable(null, varNew);

    parseInfo.format("Created New Variable as section = ");
    varNew.getNameAndDimensions(parseInfo, true, false);
    parseInfo.format("%n");

    count += dim.getLength();
  }
}
 
Example 11
private void testTimeDelta(Variable timeVar, TimeDelta expectedDiff) throws IOException {
  CalendarDateUnit calendarDateUnit = getCalendarDateUnit(timeVar);
  int[] shape = timeVar.getShape();
  Array vals = timeVar.read();
  for (int val = 1; val < shape[0]; val++) {
    CalendarDate today = calendarDateUnit.makeCalendarDate(vals.getInt(val));
    CalendarDate yesterday = calendarDateUnit.makeCalendarDate(vals.getInt(val - 1));
    long diff = today.getDifference(yesterday, expectedDiff.getUnit());
    assertThat(diff).isEqualTo(expectedDiff.getValue());
  }
}
 
Example 12
public static double[] readDataForVariableForSingleLocationAndRealization(Variable variable, int stationDimensionIndex, int stationIndex, int realizationDimensionIndex, int realizationIndex) {
	int[] origin = createOrigin(variable);
	int[] sizeArray = variable.getShape();

	//select only the given station.
	origin[stationDimensionIndex] = stationIndex;
	sizeArray[stationDimensionIndex] = 1;
	//select only the given realization
	origin[realizationDimensionIndex] = realizationIndex;
	sizeArray[realizationDimensionIndex] = 1;
	return readSelectedData(variable, origin, sizeArray, -1);
}
 
Example 13
@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 14
private void writeRecordData(ucar.nc2.Structure s, int recnum, StructureData sdata)
    throws java.io.IOException, ucar.ma2.InvalidRangeException {

  StructureMembers members = sdata.getStructureMembers();

  // loop over members
  for (Variable vm : s.getVariables()) {
    StructureMembers.Member m = members.findMember(vm.getShortName());
    if (null == m)
      continue; // this means that the data is missing from the ArrayStructure

    // convert String member data into CHAR data
    Array data = sdata.getArray(m);
    if (data instanceof ArrayObject && vm.getDataType() == DataType.CHAR && vm.getRank() > 0) {
      int strlen = vm.getShape(vm.getRank() - 1);
      data = ArrayChar.makeFromStringArray((ArrayObject) data, strlen); // turn it into an ArrayChar
    }

    // layout of the destination
    N3headerNew.Vinfo vinfo = (N3headerNew.Vinfo) vm.getSPobject();
    long begin = vinfo.begin + recnum * header.recsize; // this assumes unlimited dimension
    Section memberSection = vm.getShapeAsSection();
    Layout layout = new LayoutRegular(begin, vm.getElementSize(), vm.getShape(), memberSection);

    try {
      writeData(data, layout, vm.getDataType());
    } catch (Exception e) {
      log.error("Error writing member=" + vm.getShortName() + " in struct=" + s.getFullName(), e);
      throw new IOException(e);
    }
  }
}
 
Example 15
public static void writeDataForVariableForSingleTimeSingleLocation(NetcdfFileWriter netcdfFileWriter, Variable variable, int timeIndex, int stationDimensionIndex, int stationIndex, double[] values) {
	int[] origin = createOrigin(variable);
	int[] sizeArray = variable.getShape();

	//here assume that timeDimensionIndex is 0.
	int timeDimensionIndex = 0;
	//select only the given time and station.
	origin[timeDimensionIndex] = timeIndex;
	sizeArray[timeDimensionIndex] = 1;
	origin[stationDimensionIndex] = stationIndex;
	sizeArray[stationDimensionIndex] = 1;

	writeSelectedData(netcdfFileWriter, variable, origin, sizeArray, values);
}
 
Example 16
public static void writeDataForVariableForSingleTime(NetcdfFileWriter netcdfFileWriter, Variable variable, int timeDimensionIndex, int timeIndex, double[] values) {
	int[] origin = createOrigin(variable);
	int[] sizeArray = variable.getShape();

	//select only the given time.
	origin[timeDimensionIndex] = timeIndex;
	sizeArray[timeDimensionIndex] = 1;

	writeSelectedData(netcdfFileWriter, variable, origin, sizeArray, values);
}
 
Example 17
private void testTimeDelta(Variable timeVar, TimeDelta expectedDiff) throws IOException {
  CalendarDateUnit calendarDateUnit = getCalendarDateUnit(timeVar);
  int[] shape = timeVar.getShape();
  Array vals = timeVar.read();
  for (int val = 1; val < shape[0]; val++) {
    CalendarDate today = calendarDateUnit.makeCalendarDate(vals.getInt(val));
    CalendarDate yesterday = calendarDateUnit.makeCalendarDate(vals.getInt(val - 1));
    long diff = today.getDifference(yesterday, expectedDiff.getUnit());
    assertThat(diff).isEqualTo(expectedDiff.getValue());
  }
}
 
Example 18
public UnidataStationObsMultidimDataset(NetcdfDataset ds) throws IOException {
  super(ds);

  stationDim = UnidataObsDatasetHelper.findDimension(ds, "station");
  obsDim = UnidataObsDatasetHelper.findDimension(ds, "observation");
  if (obsDim == null)
    obsDim = ds.getUnlimitedDimension();
  if (obsDim == null)
    throw new IllegalStateException("must specify the observation dimension or use unlimited dimension");

  // coordinate variables
  latVar = UnidataObsDatasetHelper.getCoordinate(ds, AxisType.Lat);
  lonVar = UnidataObsDatasetHelper.getCoordinate(ds, AxisType.Lon);
  altVar = UnidataObsDatasetHelper.getCoordinate(ds, AxisType.Height);
  timeVar = UnidataObsDatasetHelper.getCoordinate(ds, AxisType.Time);
  timeNominalVar = UnidataObsDatasetHelper.findVariable(ds, "time_nominal");

  if (latVar == null)
    throw new IllegalStateException("Missing latitude variable");
  if (lonVar == null)
    throw new IllegalStateException("Missing longitude coordinate variable");
  if (timeVar == null)
    throw new IllegalStateException("Missing time coordinate variable");

  if (!latVar.getDimension(0).equals(stationDim))
    throw new IllegalStateException("latitude variable must use the station dimension");
  if (!lonVar.getDimension(0).equals(stationDim))
    throw new IllegalStateException("longitude variable must use the station dimension");
  if (!timeVar.getDimension(0).equals(stationDim))
    throw new IllegalStateException("time variable must use the station dimension");
  if ((altVar != null) && !altVar.getDimension(0).equals(stationDim))
    throw new IllegalStateException("altitude variable must use the station dimension");
  if ((timeNominalVar != null) && !timeNominalVar.getDimension(0).equals(stationDim))
    throw new IllegalStateException("timeNominal variable must use the station dimension");

  // station variables
  stationIdVar = UnidataObsDatasetHelper.findVariable(ds, "station_id");
  stationDescVar = UnidataObsDatasetHelper.findVariable(ds, "station_description");
  numStationsVar = UnidataObsDatasetHelper.findVariable(ds, "number_stations");

  if (stationIdVar == null)
    throw new IllegalStateException("Missing station id variable");
  if (!stationIdVar.getDimension(0).equals(stationDim))
    throw new IllegalStateException("stationId variable must use the station dimension");

  if ((stationDescVar != null) && !stationDescVar.getDimension(0).equals(stationDim))
    throw new IllegalStateException("stationDesc variable must use the station dimension");

  // create member variables
  structureMembers = new StructureMembers("UnidataStationObsMultidimDataset_obsStructure");
  for (Variable v : netcdfDataset.getVariables()) {
    if (v.getRank() < 2)
      continue;
    if (v.getDimension(0).equals(this.stationDim) && v.getDimension(1).equals(this.obsDim)) {
      dataVariables.add(v);
      int[] shape = v.getShape();
      shape[0] = 1;
      shape[1] = 1;
      structureMembers.addMember(v.getShortName(), v.getDescription(), v.getUnitsString(), v.getDataType(), shape);
    }
  }

  readStations(); // LOOK try to defer this

  // get min, max date
  startDate = UnidataObsDatasetHelper.getStartDate(ds);
  endDate = UnidataObsDatasetHelper.getEndDate(ds);
  boundingBox = UnidataObsDatasetHelper.getBoundingBox(ds);
  if (null == boundingBox)
    setBoundingBox();

  setTimeUnits();

  title = ds.findAttValueIgnoreCase(null, "title", "");
  desc = ds.findAttValueIgnoreCase(null, "description", "");
}
 
Example 19
private void readNetCdfVariables() {

		for (Variable variable : this.netcdfFile.getVariables()) {

			if (Arrays.asList(keyVariables).contains(variable.getShortName())) {

				Variable timeVariable = NetcdfUtils.findTimeVariableForVariable(variable, this.netcdfFile);
				if (timeVariable == null) {
					throw new RuntimeException("NetcdfD3dMapDataObject: no time axis for " + variable.getShortName() + ", file: " + netcdfFile.getLocation());
				}
				this.timeDimensionIndex = variable.findDimensionIndex(timeVariable.getShortName());
				timeDependentVars.put(variable.getShortName(), variable);

				// if this is the first time dependent variable: read the times
				if (timesInNetcdfFile == null) {
					ucar.ma2.Array timesArray;
					try {
						timesArray = timeVariable.read();
					} catch (IOException e) {
						throw new RuntimeException("NetcdfD3dMapDataObject could not read time variable " + timeVariable.getShortName() +
								"from netcdf file " + netcdfFile.getLocation());
					}
					timesInNetcdfFile = (double[]) timesArray.get1DJavaArray(double.class);
				}

				// get the number of spatial dimensions
				// one of the dimensions was for time
				List<Dimension> dimensions = variable.getDimensions();
				int nonTimeDimensions = dimensions.size() - 1;
				if (nonTimeDimensions != 4 && variable.getShortName().equalsIgnoreCase("R1")) {
					throw new RuntimeException("NetcdfD3dMapDataObject: #dims for R1 should be four, file: " + netcdfFile.getLocation());
				}

				int layerCount = 0;
				for (int i = 0; i < dimensions.size(); i++) {
					Dimension dimension = dimensions.get(i);
					if (variable.getShortName().equalsIgnoreCase("R1")){
						if (dimension.getShortName().equalsIgnoreCase("LSTSCI")) {
							lstsciDimensionIndex = i;
							if (dimension.getLength() != 1) {
								throw new RuntimeException("NetcdfD3dMapDataObject: #R1 != 1 is not supported (temp. or salinity), file: "
									+ netcdfFile.getLocation());
							}
						}
					}
					if (!variable.getShortName().equalsIgnoreCase("S1")) {
						if (dimension.getShortName().equalsIgnoreCase("KMAXOUT_RESTR")) {
							if (variable.getShortName().equalsIgnoreCase("R1")) {
								kmaxOutRestrDimensionIndex = i;
							} else if (variable.getShortName().equalsIgnoreCase("U1") || variable.getShortName().equalsIgnoreCase("V1")) {
								kmaxOutRestrDimensionIndexVelocity = i;
							}
							if (dimension.getLength() < 0) {
								throw new RuntimeException("NetcdfD3dMapDataObject: could not read number of layers, file: "
									+ netcdfFile.getLocation());
							}
							layerCount = dimension.getLength();
						}
					}

				}

				if (lstsciDimensionIndex == 0 || kmaxOutRestrDimensionIndex == 0 || kmaxOutRestrDimensionIndexVelocity == 0) {
					throw new RuntimeException("NetcdfD3dMapDataObject: dims not available, file: "
						+ netcdfFile.getLocation());
				}

				ITimeInfo timeInfo = NetcdfUtils.createTimeInfo(variable, this.netcdfFile, timeInfoCache);
				// for memory reasons, for the time being we only keep the last time step in the exchange time
				// so adjust the time info
				double[] times = timeInfo.getTimes();
				timeInfo = new TimeInfo(new double[]{times[times.length-1]});

				// Extracting geometry information only once
				if (this.xCoords == null | this.yCoords == null | this.zCoords == null) {
					Variable variableX = this.netcdfFile.findVariable("XZ");
					Variable variableY = this.netcdfFile.findVariable("YZ");
					Variable variableZ = this.netcdfFile.findVariable("ZK_LYR");

					int[] originXY = createOrigin(variableX);
					int[] sizeArrayXY = variableX.getShape();
					int[] originZ = createOrigin(variableZ);
					int[] sizeArrayZ = variableZ.getShape();

					this.xCoords = NetcdfUtils.readSelectedData(variableX, originXY, sizeArrayXY, -1);
					this.yCoords = NetcdfUtils.readSelectedData(variableY, originXY, sizeArrayXY, -1);
					this.zCoords = NetcdfUtils.readSelectedData(variableZ, originZ, sizeArrayZ, -1);
				}

				IGeometryInfo geometryInfo;
				if (variable.getShortName().equalsIgnoreCase("S1")) {
					geometryInfo = new NetcdfD3dMapExchangeItemGeometryInfo(this.xCoords, this.yCoords, null);
				} else {
					geometryInfo = new NetcdfD3dMapExchangeItemGeometryInfo(this.xCoords, this.yCoords, this.zCoords);
				}
				IExchangeItem exchangeItem = new NetcdfD3dMapExchangeItem(variable.getShortName(), this, timeInfo, geometryInfo);
				this.exchangeItems.put(exchangeItem.getId(), exchangeItem);

			}
		}
	}
 
Example 20
public double[] getExchangeItemValues(String varName, int stationIndex, int layerIndex) {

		Variable variable;
		if (varName.equalsIgnoreCase("kEN") || varName.equalsIgnoreCase("vMAG")) {
			variable = this.netcdfFile.findVariable("ZCURU");
		} else {
			variable = this.netcdfFile.findVariable(varName);
		}

		int[] origin = createOrigin(variable);
		int[] sizeArray = variable.getShape();

		origin[timeDimensionIndex] = 0;
		if (varName.equalsIgnoreCase("GRO")) {
			//select only the given station and layer
			//origin[timeDimensionIndex] = 0;
			origin[lstsciDimensionIndex] = 0; //Only a single constituent possible for now, if several, arguments need to be changed
			origin[kmaxOutRestrDimensionIndex] = layerIndex;
			origin[statDimensionIndex] = stationIndex;
			//only one station and layer
			sizeArray[kmaxOutRestrDimensionIndex] = 1;
			sizeArray[statDimensionIndex] = 1;
		}else if (varName.equalsIgnoreCase("ZCURU") || varName.equalsIgnoreCase("ZCURV") || varName.equalsIgnoreCase("ZCURW")|| varName.equalsIgnoreCase("kEN")) {
			//origin[timeDimensionIndexVelocity] = 0;
			origin[kmaxOutRestrDimensionIndexVelocity] = layerIndex;
			origin[statDimensionIndexVelocity] = stationIndex;
			//only one station and layer
			sizeArray[kmaxOutRestrDimensionIndexVelocity] = 1;
			sizeArray[statDimensionIndexVelocity] = 1;
		}else if (varName.equalsIgnoreCase("ZWL")){
			origin[statDimensionIndex] = stationIndex;
			sizeArray[statDimensionIndexVelocity] = 1;
		}

		if (varName.equalsIgnoreCase("kEN")) {
			return getKineticEnergyValues(origin, sizeArray);
		}else if (varName.equalsIgnoreCase("vMAG")){
			return getVelocityMagnitudeValues(origin, sizeArray);
		} else{
			return NetcdfUtils.readSelectedData(variable, origin, sizeArray, -1);
		}

	}