Re: [netcdf-java] Get data through lat lon coordinates

On 1/27/2012 2:14 AM, Pablo Rozas Larraondo wrote:

Good morning,

I've recently moved to NetCDF Java API from the ECMWF Grib API because I have to work with Lambert projection Gribs and those are not fully developed yet.

I've succeeded in opening and reading Grib1 files, but I'm having problems trying to find out how to access data through lat lon coordinates. In the ECMWF API there is a function called grib_find_nearest(gid, lat,lon) in which you give a geocoordinate point and the function returns the value of the closest, or the four closest grid points.

Using the NetCDF API I can access the _CoordinateAxisType Attributes of the grib file which are GeoX and GeoY expressed in Km, but I wonder if there is a function for accessing values or translating points into geographic coordinates.

Thank you very much for your help,

Pablo Rozas

AEMet Analyst Forecaster

Santander, Spain

netcdf-java mailing list
For list information or to unsubscribe, visit:

Hi Pablo:

You can open the file as a GridDataset, then use GridCoordSystem.findXYindexFromLatLon() to figure out
which index is at that lat,lon, eg:

 // open the dataset, find the variable and its coordinate system
  GridDataset gds =;
  GridDatatype grid = gds.findGridDatatype( "myVariableName");
  GridCoordSystem gcs = grid.getCoordinateSystem();

  double lat = 8.0;
  double lon = 21.0;

  // find the x,y index for a specific lat/lon position
int[] xy = gcs.findXYindexFromLatLon(lat, lon, null); // xy[0] = x, xy[1] = y

// read the data at that lat, lon and the first time and z level (if any) Array data = grid.readDataSlice(0, 0, xy[1], xy[0]); // note order is t, z, y, x
  double val = data.getDouble(0); // we know its a scalar
  System.out.printf("Value at %f %f == %f%n", lat, lon, val);

  • 2012 messages navigation, sorted by:
    1. Thread
    2. Subject
    3. Author
    4. Date
    5. ↑ Table Of Contents
  • Search the netcdf-java archives: