Java Code Examples for ucar.ma2.ArrayDouble#D3

The following examples show how to use ucar.ma2.ArrayDouble#D3 . 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
/**
 * Get the 1D vertical coordinate array for this time step and point
 * 
 * @param timeIndex the time index. Ignored if !isTimeDependent().
 * @param xIndex the x index
 * @param yIndex the y index
 * @return vertical coordinate array
 * @throws java.io.IOException problem reading data
 * @throws ucar.ma2.InvalidRangeException _more_
 */
public D1 getCoordinateArray1D(int timeIndex, int xIndex, int yIndex) throws IOException, InvalidRangeException {
  ArrayDouble.D3 data = getCoordinateArray(timeIndex);

  int[] origin = new int[3];
  int[] shape = new int[3];

  origin[0] = 0;
  origin[1] = yIndex;
  origin[2] = xIndex;

  shape[0] = data.getShape()[0];
  shape[1] = 1;
  shape[2] = 1;

  Array tmp = data.section(origin, shape);
  return (ArrayDouble.D1) tmp.reduce();
}
 
Example 2
Source File: AtmosSigma.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Get the 3D vertical coordinate array for this time step.
 *
 * @param timeIndex the time index. Ignored if !isTimeDependent().
 * @return vertical coordinate array
 * @throws IOException problem reading data
 * @throws InvalidRangeException _more_
 */
public ArrayDouble.D3 getCoordinateArray(int timeIndex) throws IOException, InvalidRangeException {
  Array ps = readArray(psVar, timeIndex);
  Index psIndex = ps.getIndex();

  int nz = sigma.length;
  int[] shape2D = ps.getShape();
  int ny = shape2D[0];
  int nx = shape2D[1];

  ArrayDouble.D3 result = new ArrayDouble.D3(nz, ny, nx);

  for (int y = 0; y < ny; y++) {
    for (int x = 0; x < nx; x++) {
      double psVal = ps.getDouble(psIndex.set(y, x));
      for (int z = 0; z < nz; z++) {
        result.set(z, y, x, ptop + sigma[z] * (psVal - ptop));
      }
    }
  }

  return result;
}
 
Example 3
Source File: VerticalTransformSubset.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public ArrayDouble.D3 getCoordinateArray(int subsetIndex) throws IOException, InvalidRangeException {
  int orgIndex = subsetIndex;
  if (isTimeDependent() && (t_range != null)) {
    orgIndex = t_range.element(subsetIndex);
  }

  ArrayDouble.D3 data = original.getCoordinateArray(orgIndex);

  return (ArrayDouble.D3) data.sectionNoReduce(subsetList);
}
 
Example 4
Source File: TestDtWithCoverageReadingP.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private static void readAllRuntimes(Coverage cover, GridDatatype dt, CoordinateAxis1DTime runtimeAxis,
    CoordinateAxis1D ensAxis, CoordinateAxis1D vertAxis) {
  GridCoordSystem csys = dt.getCoordinateSystem();
  CoordinateAxis1DTime timeAxis1D = csys.getTimeAxis1D();
  CoordinateAxis timeAxis = csys.getTimeAxis();
  CoordinateAxis2D timeAxis2D = (timeAxis instanceof CoordinateAxis2D) ? (CoordinateAxis2D) timeAxis : null;

  if (runtimeAxis == null)
    readAllTimes1D(cover, dt, null, -1, timeAxis1D, ensAxis, vertAxis);

  else if (timeAxis2D == null) { // 1D time or no time
    for (int i = 0; i < runtimeAxis.getSize(); i++)
      readAllTimes1D(cover, dt, runtimeAxis.getCalendarDate(i), i, timeAxis1D, ensAxis, vertAxis);

  } else { // 2D time
    TimeHelper helper = TimeHelper.factory(timeAxis.getUnitsString(), timeAxis.attributes());

    if (timeAxis2D.isInterval()) {
      ArrayDouble.D3 bounds = timeAxis2D.getCoordBoundsArray();
      for (int i = 0; i < runtimeAxis.getSize(); i++)
        readAllTimes2D(cover, dt, runtimeAxis.getCalendarDate(i), i, helper, bounds.slice(0, i), ensAxis, vertAxis);

    } else {
      ArrayDouble.D2 coords = timeAxis2D.getCoordValuesArray();
      for (int i = 0; i < runtimeAxis.getSize(); i++)
        readAllTimes2D(cover, dt, runtimeAxis.getCalendarDate(i), i, helper, coords.slice(0, i), ensAxis, vertAxis);
    }
  }
}
 
Example 5
Source File: WRFEta.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Get the 3D vertical coordinate array for this time step.
 *
 * @param timeIndex the time index. Ignored if !isTimeDependent().
 * @return vertical coordinate array
 * @throws IOException problem reading data
 */
public ArrayDouble.D3 getCoordinateArray(int timeIndex) throws IOException {
  ArrayDouble.D3 array;

  Array pertArray = getTimeSlice(pertVar, timeIndex);
  Array baseArray = getTimeSlice(baseVar, timeIndex);

  // ADD: use MAMath?
  // ADD: use IndexIterator from getIndexIteratorFast?
  int[] shape = pertArray.getShape();
  // ADD: assert that rank = 3
  // ADD: assert that both arrays are same shape
  int ni = shape[0];
  int nj = shape[1];
  int nk = shape[2];

  array = new ArrayDouble.D3(ni, nj, nk);
  Index index = array.getIndex();

  for (int i = 0; i < ni; i++) {
    for (int j = 0; j < nj; j++) {
      for (int k = 0; k < nk; k++) {
        index.set(i, j, k);
        double d = pertArray.getDouble(index) + baseArray.getDouble(index);
        if (isZStag) {
          d = d / 9.81; // convert geopotential to height
        }
        array.setDouble(index, d);
      }
    }
  }

  if (isXStag) {
    array = addStagger(array, 2); // assuming x dim index is 2
  }
  if (isYStag) {
    array = addStagger(array, 1); // assuming y dim index is 1
  }

  return array;
}
 
Example 6
Source File: WRFEta.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Add 1 to the size of the array for the given dimension.
 * Use linear average and interpolation to fill in the values.
 *
 * @param array use this array
 * @param dimIndex use this dimension
 * @return new array with stagger
 */
private ArrayDouble.D3 addStagger(ArrayDouble.D3 array, int dimIndex) {

  // ADD: assert 0<=dimIndex<=2

  int[] shape = array.getShape();
  int[] newShape = new int[3];
  System.arraycopy(shape, 0, newShape, 0, 3);

  newShape[dimIndex]++;
  int ni = newShape[0];
  int nj = newShape[1];
  int nk = newShape[2];
  ArrayDouble.D3 newArray = new ArrayDouble.D3(ni, nj, nk);
  // Index newIndex = newArray.getIndex();

  // extract 1d array to be extended
  int n = shape[dimIndex]; // length of extracted array
  double[] d = new double[n]; // tmp array to hold extracted values
  int[] eshape = new int[3]; // shape of extracted array
  int[] neweshape = new int[3]; // shape of new array slice to write into
  for (int i = 0; i < 3; i++) {
    eshape[i] = (i == dimIndex) ? n : 1;
    neweshape[i] = (i == dimIndex) ? n + 1 : 1;
  }
  int[] origin = new int[3];

  try {
    // loop through the other 2 dimensions and "extrapinterpolate" the other
    for (int i = 0; i < ((dimIndex == 0) ? 1 : ni); i++) {
      for (int j = 0; j < ((dimIndex == 1) ? 1 : nj); j++) {
        for (int k = 0; k < ((dimIndex == 2) ? 1 : nk); k++) {
          origin[0] = i;
          origin[1] = j;
          origin[2] = k;
          IndexIterator it = array.section(origin, eshape).getIndexIterator();
          for (int l = 0; l < n; l++) {
            d[l] = it.getDoubleNext(); // get the original values
          }
          double[] d2 = extrapinterpolate(d); // compute new values
          // define slice of new array to write into
          IndexIterator newit = newArray.section(origin, neweshape).getIndexIterator();
          for (int l = 0; l < n + 1; l++) {
            newit.setDoubleNext(d2[l]);
          }
        }
      }
    }
  } catch (InvalidRangeException e) {
    // ADD: report error?
    return null;
  }

  return newArray;
}
 
Example 7
Source File: VerticalTransformSubset.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 3 votes vote down vote up
/**
 * Get the 1D vertical coordinate array for this time step and point
 * 
 * @param timeIndex the time index. Ignored if !isTimeDependent().
 * @param xIndex the x index
 * @param yIndex the y index
 * @return vertical coordinate array
 * @throws java.io.IOException problem reading data
 * @throws ucar.ma2.InvalidRangeException _more_
 */
public D1 getCoordinateArray1D(int timeIndex, int xIndex, int yIndex) throws IOException, InvalidRangeException {


  ArrayDouble.D3 data = original.getCoordinateArray(timeIndex);

  int[] origin = new int[3];
  int[] shape = new int[3];

  shape[0] = subsetList.get(0).length();
  shape[1] = 1;
  shape[2] = 1;

  origin[0] = timeIndex;
  if (isTimeDependent() && (t_range != null)) {
    origin[0] = t_range.element(timeIndex);
  }

  origin[1] = yIndex;
  origin[2] = xIndex;

  Array section = data.section(origin, shape);

  return (ArrayDouble.D1) section.reduce();


}