Java Code Examples for ucar.nc2.dataset.CoordinateAxis1D#findCoordElement()

The following examples show how to use ucar.nc2.dataset.CoordinateAxis1D#findCoordElement() . 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: GridAsPointDataset.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public Point readData(GridDatatype grid, CalendarDate date, double zCoord, double lat, double lon)
    throws java.io.IOException {
  GridCoordSystem gcs = grid.getCoordinateSystem();

  int tidx = -1;
  // Date may be null if the grid does not have time axis
  if (date != null)
    tidx = findTimeIndexForCalendarDate(gcs, date);

  CoordinateAxis1D zAxis = gcs.getVerticalAxis();
  int zidx = zAxis.findCoordElement(zCoord);

  int[] xy = gcs.findXYindexFromLatLon(lat, lon, null);

  Array data = grid.readDataSlice(tidx, zidx, xy[1], xy[0]);

  // use actual grid midpoint
  LatLonPoint latlon = gcs.getLatLon(xy[0], xy[1]);

  Point p = new Point();
  p.lat = latlon.getLatitude();
  p.lon = latlon.getLongitude();
  p.z = zAxis.getCoordValue(zidx);
  p.dataValue = data.getDouble(data.getIndex());
  return p;
}
 
Example 2
Source File: GridRenderer.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * find the data value at this point
 *
 * @param loc : point in display projection coordinates (vertical view)
 * @return String representation of value
 */
public String getYZvalueStr(Point2D loc) {
  if ((stridedGrid == null) || (dataV == null))
    return "";

  // find the grid indexes
  GridCoordSystem geocs = stridedGrid.getCoordinateSystem();
  CoordinateAxis1D zaxis = geocs.getVerticalAxis();
  if (zaxis == null)
    return "";

  valueIndex = geocs.findXYindexFromCoord(loc.getX(), lastSlice, valueIndex);

  int wanty = valueIndex[1];
  int wantz = zaxis.findCoordElement(loc.getY());

  // get value, construct the string
  if ((wanty == -1) || (wantz == -1))
    return "outside grid area";
  else {
    Index imaV = dataV.getIndex();
    double value = dataV.getDouble(imaV.set(wantz, wanty));
    int wantx = (geocs.getXHorizAxis() == null) ? -1 : lastSlice;
    return makeXYZvalueStr(value, wantx, wanty, wantz);
  }
}
 
Example 3
Source File: GridAsPointDataset.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public boolean hasVert(GridDatatype grid, double zCoord) {
  GridCoordSystem gcs = grid.getCoordinateSystem();
  CoordinateAxis1D zAxis = gcs.getVerticalAxis();
  if (zAxis == null)
    return false;
  return (zAxis.findCoordElement(zCoord) >= 0);
}
 
Example 4
Source File: GridAsPointDataset.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Reads data for the given point (earthlocation) and if bounded is true returns data for the closest point within the
 * grid, for points outside of the grid
 * 
 * @param grid read data from here
 * @param date at this time
 * @param location EarthLocation, if altitude is NaN assume that is 2D point
 * @param bounded if bounded, location must be in grid cell; otherwise get nearest grid point to location
 * @return the location and data value
 * @throws java.io.IOException on bad stuff
 */
public Point readData(GridDatatype grid, CalendarDate date, EarthLocation location, boolean bounded)
    throws java.io.IOException {
  if (!bounded) {
    if (Double.isNaN(location.getAltitude())) {
      return readData(grid, date, location.getLatitude(), location.getLongitude());
    } else {
      return readData(grid, date, location.getAltitude(), location.getLatitude(), location.getLongitude());
    }
  }

  // Bounded --> Read closest data
  GridCoordSystem gcs = grid.getCoordinateSystem();
  int tidx = findTimeIndexForCalendarDate(gcs, date);
  int[] xy = gcs.findXYindexFromLatLonBounded(location.getLatitude(), location.getLongitude(), null);

  LatLonPoint latlon = gcs.getLatLon(xy[0], xy[1]);
  Point p = new Point();
  p.lat = latlon.getLatitude();
  p.lon = latlon.getLongitude();

  int zidx = -1;
  if (!Double.isNaN(location.getAltitude())) {
    CoordinateAxis1D zAxis = gcs.getVerticalAxis();
    zidx = zAxis.findCoordElement(location.getAltitude());
    p.z = zAxis.getCoordValue(zidx);
  }

  Array data = grid.readDataSlice(tidx, zidx, xy[1], xy[0]);
  p.dataValue = data.getDouble(data.getIndex());

  return p;
}
 
Example 5
Source File: GridRenderer.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * find the level (z) index that is represented by this point
 *
 * @param pos coord in data z coordinate space.
 */
public int findLevelCoordElement(double pos) {
  if (null == orgGrid)
    return -1;

  // find the grid index
  GridCoordSystem geocs = orgGrid.getCoordinateSystem();
  CoordinateAxis1D zaxis = geocs.getVerticalAxis();
  return (zaxis == null) ? -1 : zaxis.findCoordElement(pos);
}
 
Example 6
Source File: GridAsPointDataset.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * 
 * Reads one single data for one point.
 * Takes the ensemble and elevation coordinates allowing them to be < 0 and in that case they'll be ignored.
 * 
 * @return Point matching lat/lon for this grid
 */
public Point readData(GridDatatype grid, CalendarDate date, double ensCoord, double zCoord, double lat, double lon)
    throws java.io.IOException {
  GridCoordSystem gcs = grid.getCoordinateSystem();

  // CoordinateAxis1DTime timeAxis = gcs.getTimeAxis1D();
  // int tidx = timeAxis.findTimeIndexFromCalendarDate(date);
  int tidx = -1;
  // Date may be null if the grid does not have time axis
  if (date != null)
    tidx = findTimeIndexForCalendarDate(gcs, date);

  Point p = new Point();
  int zidx = -1;
  // if(zCoord != -1){ LOOK!! --> zCoord may be -1 (ocean sigma levels usually go from 0 to -1)!!!
  CoordinateAxis1D zAxis = gcs.getVerticalAxis();
  if (zAxis != null) {
    zidx = zAxis.findCoordElement(zCoord);
    if (zidx != -1)
      p.z = zAxis.getCoordValue(zidx);
  }
  // }

  int eidx = -1;
  if (ensCoord != -1) {
    CoordinateAxis1D ensAxis = gcs.getEnsembleAxis();
    eidx = ensAxis.findCoordElement(ensCoord);
    p.ens = ensAxis.getCoordValue(eidx);
  }

  int[] xy = gcs.findXYindexFromLatLon(lat, lon, null);

  // Array data = grid.readDataSlice(tidx, zidx, xy[1], xy[0]);
  Array data = grid.readDataSlice(0, eidx, tidx, zidx, xy[1], xy[0]);

  // use actual grid midpoint
  LatLonPoint latlon = gcs.getLatLon(xy[0], xy[1]);


  p.lat = latlon.getLatitude();
  p.lon = latlon.getLongitude();

  p.dataValue = data.getDouble(data.getIndex());
  return p;
}
 
Example 7
Source File: TestDtWithCoverageReadingSingleP.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Test
public void testReadGridCoverageSlice() throws IOException, InvalidRangeException { // read single slice
  System.out.printf("Test Dataset %s coverage %s%n", endpoint, covName);

  try (FeatureDatasetCoverage cc = CoverageDatasetFactory.open(endpoint)) {
    Assert.assertNotNull(endpoint, cc);
    CoverageCollection gcs = (type == null) ? cc.getCoverageCollections().get(0) : cc.findCoverageDataset(type);
    Assert.assertNotNull("gcs", gcs);
    Coverage cover = gcs.findCoverage(covName);
    Assert.assertNotNull(covName, cover);

    // check DtCoverageCS
    try (GridDataset ds = GridDataset.open(endpoint)) {
      GridDatatype dt = ds.findGridByName(gridName);
      if (dt == null)
        dt = ds.findGridByName(covName);
      Assert.assertNotNull(gridName, dt);

      GridCoordSystem csys = dt.getCoordinateSystem();
      CoordinateAxis1DTime rtAxis = csys.getRunTimeAxis();
      CoordinateAxis1D ensAxis = csys.getEnsembleAxis();
      CoordinateAxis1DTime timeAxis = csys.getTimeAxis1D();
      CoordinateAxis1D vertAxis = csys.getVerticalAxis();

      int calcTimeIdx = -1;
      int rt_idx = (rtAxis == null || rt_val == null) ? -1 : rtAxis.findTimeIndexFromCalendarDate(rt_val);
      if (time_idx == null) {
        if (time_val != null) {
          if (timeAxis != null)
            calcTimeIdx = timeAxis.findTimeIndexFromCalendarDate(time_val);
          else if (rt_idx >= 0) {
            CoordinateAxis2D timeAxis2D = (CoordinateAxis2D) csys.getTimeAxis();
            calcTimeIdx = timeAxis2D.findTimeIndexFromCalendarDate(rt_idx, time_val);
            // timeAxis = csys.getTimeAxisForRun(rt_idx); // LOOK doesnt work for interval coords
            // if (timeAxis != null)
            // calcTimeIdx = timeAxis.findTimeIndexFromCalendarDate(time_val); // LOOK theres a bug here, set time_idx
            // as workaround
          }
        }
      } else {
        calcTimeIdx = time_idx;
      }

      int ens_idx = (ensAxis == null || ens_val == null) ? -1 : ensAxis.findCoordElement(ens_val);
      int vert_idx = (vertAxis == null || vert_val == null) ? -1 : vertAxis.findCoordElement(vert_val);


      /*
       * static void readAllVertLevels(Coverage cover, GridDatatype dt, CalendarDate rt_val, int rt_idx, CalendarDate
       * time_val, int time_idx,
       * // double ens_val, int ens_idx, CoordinateAxis1D vertAxis)
       * TestDtWithCoverageReadingP.readAllVertLevels(cover, dt, rt_val, rt_idx, time_val, calcTimeIdx,
       * ens_val == null ? 0 : ens_val, ens_idx,
       * vertAxis); //
       */

      TestDtWithCoverageReadingP.readOneSlice(cover, dt, rt_val, rt_idx, time_val, calcTimeIdx,
          ens_val == null ? 0 : ens_val, ens_idx, vert_val == null ? 0 : vert_val, vert_idx); // */

    }
  }
}