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

The following examples show how to use ucar.nc2.dataset.CoordinateAxis1D#getCoordValue() . 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: ThreddsMetadata.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void setVertical(CoordinateAxis1D vaxis) {
  int n = (int) vaxis.getSize();
  double size = vaxis.getCoordValue(n - 1) - vaxis.getCoordValue(0);
  double resolution = vaxis.getIncrement();
  String units = vaxis.getUnitsString();
  this.updown = new Range(vaxis.getCoordValue(0), size, resolution, units);
  if (units != null) {
    setZPositiveUp(SimpleUnit.isCompatible("m", units));
  }
}
 
Example 3
Source File: GeotiffWriter.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Write GridDatatype data to the geotiff file.
 *
 * @param dataset grid in contained in this dataset
 * @param grid data is in this grid
 * @param data 2D array in YX order
 * @param greyScale if true, write greyScale image, else dataSample.
 * @throws IOException on i/o error
 */
public void writeGrid(GridDataset dataset, GridDatatype grid, Array data, boolean greyScale) throws IOException {
  GridCoordSystem gcs = grid.getCoordinateSystem();

  if (!gcs.isRegularSpatial()) {
    throw new IllegalArgumentException("Must have 1D x and y axes for " + grid.getFullName());
  }

  CoordinateAxis1D xaxis = (CoordinateAxis1D) gcs.getXHorizAxis();
  CoordinateAxis1D yaxis = (CoordinateAxis1D) gcs.getYHorizAxis();

  // units may need to be scaled to meters
  double scaler = (xaxis.getUnitsString().equalsIgnoreCase("km")) ? 1000.0 : 1.0;

  // data must go from top to bottom
  double xStart = xaxis.getCoordEdge(0) * scaler;
  double yStart = yaxis.getCoordEdge(0) * scaler;
  double xInc = xaxis.getIncrement() * scaler;
  double yInc = Math.abs(yaxis.getIncrement()) * scaler;

  if (yaxis.getCoordValue(0) < yaxis.getCoordValue(1)) {
    data = data.flip(0);
    yStart = yaxis.getCoordEdge((int) yaxis.getSize()) * scaler;
  }

  if (!xaxis.isRegular() || !yaxis.isRegular()) {
    throw new IllegalArgumentException("Must be evenly spaced grid = " + grid.getFullName());
  }

  if (pageNumber > 1) {
    geotiff.initTags();
  }

  // write it out
  writeGrid(grid, data, greyScale, xStart, yStart, xInc, yInc, pageNumber);
  pageNumber++;
}
 
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: VertCoord.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
VertCoord(CoordinateAxis1D axis) {
  // this.axis = axis;
  this.name = axis.getFullName();
  this.units = axis.getUnitsString();

  int n = (int) axis.getSize();
  if (axis.isInterval()) {
    values1 = axis.getBound1();
    values2 = axis.getBound2();
  } else {
    values1 = new double[n];
    for (int i = 0; i < axis.getSize(); i++)
      values1[i] = axis.getCoordValue(i);
  }
}
 
Example 6
Source File: ThreddsMetadata.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public GeospatialCoverage(LatLonRect bb, CoordinateAxis1D vaxis, double dX, double dY) {
  if (bb == null) {
    this.eastwest = null;
    this.northsouth = null;
    this.isGlobal = false;
    this.names = null;

  } else {
    LatLonPoint llpt = bb.getLowerLeftPoint();
    LatLonPoint urpt = bb.getUpperRightPoint();
    double height = urpt.getLatitude() - llpt.getLatitude();

    this.eastwest = new GeospatialRange(llpt.getLongitude(), bb.getWidth(), dX, CDM.LON_UNITS);
    this.northsouth = new GeospatialRange(llpt.getLatitude(), height, dY, CDM.LAT_UNITS);

    if ((bb.getWidth() >= (360 - dX)) && (height >= (180 - dY))) {
      this.isGlobal = true;
      // serialize isGlobal
      this.names = new ArrayList<>();
      this.names.add(new Vocab("global", null));
    } else {
      this.isGlobal = false;
      this.names = null;
    }
  }

  if (vaxis == null) {
    this.updown = null;
    this.zpositive = null;

  } else {
    int n = (int) vaxis.getSize();
    double size = vaxis.getCoordValue(n - 1) - vaxis.getCoordValue(0);
    double resolution = vaxis.getIncrement();
    String units = vaxis.getUnitsString();
    this.updown = new GeospatialRange(vaxis.getCoordValue(0), size, resolution, units);
    if (units != null) {
      boolean isPositive = SimpleUnit.isCompatible("m", units);
      this.zpositive = isPositive ? CF.POSITIVE_UP : CF.POSITIVE_DOWN;
    } else {
      this.zpositive = CF.POSITIVE_UP;
    }
  }

}
 
Example 7
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;
}