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

The following examples show how to use ucar.nc2.Variable#findDimensionIndex() . 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: WRFEta.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * 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 2
Source File: CFRadialAdapter.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private CFRadial2Variable(NetcdfDataset nds, Variable v0) {
  super(v0.getShortName(), v0);

  sweeps = new ArrayList<>();

  int[] shape = v0.getShape();
  int ngates = shape[v0.getRank() - 1];
  flattened = v0.findDimensionIndex("n_points") == 0;

  for (int i = 0; i < nsweeps; i++) {
    // For flattened (1D stored data) find max number of gates
    if (flattened) {
      ngates = ray_n_gates[rayStartIdx[i]];
      for (int ray = rayStartIdx[i]; ray <= rayEndIdx[i]; ++ray)
        ngates = ray_n_gates[ray] > ngates ? ray_n_gates[ray] : ngates;
    }
    sweeps.add(new CFRadial2Sweep(v0, i, ngates, rayStartIdx[i], rayEndIdx[i]));
  }
}
 
Example 3
Source File: CFRadialAdapter.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
protected void addRadialVariable(NetcdfDataset nds, Variable var) {

    RadialVariable rsvar = null;
    int tIdx = var.findDimensionIndex("time");
    int rIdx = var.findDimensionIndex("range");
    int ptsIdx = var.findDimensionIndex("n_points");

    if (((tIdx == 0) && (rIdx == 1)) || (ptsIdx == 0)) {
      rsvar = makeRadialVariable(nds, var);
    }

    if (rsvar != null) {
      dataVariables.add(rsvar);
    }
  }
 
Example 4
Source File: NetcdfUtils.java    From OpenDA with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Creates and returns an IGeometryInfo object for the given variable in the given netcdfFile.
 * If no geometry info available, then returns null.
 *
 * @param variable
 * @param netcdfFile
 * @return geometryInfo or null.
 */
public static IArrayGeometryInfo createGeometryInfo(Variable variable, NetcdfFile netcdfFile) {

	Variable latitudeVariable = findLatitudeVariableForVariable(variable, netcdfFile);
	Variable longitudeVariable = findLongitudeVariableForVariable(variable, netcdfFile);
	//currently only 2D grids are supported.
	if (latitudeVariable == null || longitudeVariable == null) {//if data does not depend on space.
		return null;
	}

	//if 2D grid.
	IQuantityInfo latitudeQuantityInfo = new QuantityInfo(LATITUDE_STANDARD_NAME, latitudeVariable.getUnitsString());
	IQuantityInfo longitudeQuantityInfo = new QuantityInfo(LONGITUDE_STANDARD_NAME, longitudeVariable.getUnitsString());
	List<Dimension> latitudeVariableDimensions = latitudeVariable.getDimensions();
	int[] latitudeValueIndices = new int[latitudeVariableDimensions.size()];
	for (int n = 0; n < latitudeValueIndices.length; n++) {
		latitudeValueIndices[n] = variable.findDimensionIndex(latitudeVariableDimensions.get(n).getShortName());
	}
	List<Dimension> longitudeVariableDimensions = longitudeVariable.getDimensions();
	int[] longitudeValueIndices = new int[longitudeVariableDimensions.size()];
	for (int n = 0; n < longitudeValueIndices.length; n++) {
		longitudeValueIndices[n] = variable.findDimensionIndex(longitudeVariableDimensions.get(n).getShortName());
	}
	IArray latitudeArray = (IArray) readData(latitudeVariable);
	IArray longitudeArray = (IArray) readData(longitudeVariable);
       //the latitude and longitude coordinates are stored in the same order as in the netcdf file.
	ArrayGeometryInfo geometryInfo = new ArrayGeometryInfo(latitudeArray, latitudeValueIndices,
			latitudeQuantityInfo, longitudeArray, longitudeValueIndices, longitudeQuantityInfo,null,null,null);
	return geometryInfo;
}
 
Example 5
Source File: NetcdfUtils.java    From OpenDA with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static int getLatitudeDimensionIndexForVariable(Variable variable, NetcdfFile netcdfFile) {
	Variable latitudeVariable = NetcdfUtils.findLatitudeVariableForVariable(variable, netcdfFile);
	if (latitudeVariable == null) {
		return -1;
	}

	List<Dimension> latitudeVariableDimensions = latitudeVariable.getDimensions();
	return variable.findDimensionIndex(latitudeVariableDimensions.get(0).getShortName());
}
 
Example 6
Source File: NetcdfD3dMapDataObject.java    From OpenDA with GNU Lesser General Public License v3.0 4 votes vote down vote up
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);

			}
		}
	}