NOTICE: This version of the NSF Unidata web site (archive.unidata.ucar.edu) is no longer being updated.
Current content can be found at unidata.ucar.edu.

To learn about what's going on, see About the Archive Site.

Re: [netcdf-java] Help reading grib2 file.

On Tue, 29 Sep 2009, Esteban Wagner wrote:

Hi, 
I have a grb2 file containing global Primary_wave_direction forecasts. I would 
like to read the
values it contains.

By the moment I was able to read the file and obtain a GridDataset using the 
following code:
------------
(NetCDF:Version 4.0.16; Grib:Version 6.0.16)

final File file = new File("src/test/resources/multi_1.glo_30m.DIRPW.grb2");
final GridDataset gridDataSet = GridDataset.open(file.getAbsolutePath());
----------
the file can be downloaded from: 
ftp://polar.ncep.noaa.gov/pub/waves/develop/multi_1.latest_run/multi_1.glo_30m.DIRPW.grb2
----------
Now I would like if someone can tell me which are the methods from gridDataSet 
I should use in order
to obtain the different forecast values of the Primary_wave_direction for a 
given grid point.

Thanks,

Regards.

Esteban.


Esteban,

I'll reference the JavaDocs first:

http://www.unidata.ucar.edu/software/netcdf-java/v4.1/javadoc/ucar/nc2/dt/GridDataset.html
http://www.unidata.ucar.edu/software/netcdf-java/v4.1/javadoc/ucar/nc2/dt/GridDatatype.html
http://www.unidata.ucar.edu/software/netcdf-java/v4.1/javadoc/ucar/nc2/dt/GridCoordSystem.html

I'm assuming by grid point you mean Lat/Lon
One needs to be at the GridCoordSystem level to be able to convert the Lat/Lon point into indices. Then use the indices information in the GridDatatype readDatSlice routine to get the value. Here's a short sample program.

public class GetDataFromLatLon {
  public static void main(String[] args) {

       try {

        final File file = new File("C:/data/grib/multi_1.glo_30m.DIRPW.grb2");
        final GridDataset gridDataSet = 
GridDataset.open(file.getAbsolutePath());
        GridDatatype pwd = gridDataSet.findGridDatatype( 
"Primary_wave_direction");
        System.out.println( pwd.getInfo());

        GridCoordSystem pwdGcs = pwd.getCoordinateSystem();
        int[] result = null;
        // Get index value for Lat 30.0 and Lon 179
        int[] idx = pwdGcs.findXYindexFromLatLon( 30.0, 179.0, result );
        //int[] idx = pwdGcs.findXYindexFromLatLon( 90.0, 0.0, result );
        // Extract data value for time 0, no Vert index, Lat index, Lon index
        Array data = pwd.readDataSlice( 0, -1, idx[1], idx[0]);
        // Another option: read all data values for timeIndex = 0
        //Array data = pwd.readVolumeData( 0 );
        IndexIterator iter = data.getIndexIterator();
        while(iter.hasNext()) {
                float val = iter.getFloatNext();
                System.out.println( "Primary_wave_direction = "+ val);
        }

        System.out.println( "Success");
       } catch (Exception exc) {
              exc.printStackTrace();
       }
    }
}
===============================================================================
Robb Kambic                                Unidata Program Center
Software Engineer III                      Univ. Corp for Atmospheric Research
rkambic@xxxxxxxxxxxxxxxx                   WWW: http://www.unidata.ucar.edu/
===============================================================================
  • 2009 messages navigation, sorted by:
    1. Thread
    2. Subject
    3. Author
    4. Date
    5. ↑ Table Of Contents
  • Search the netcdf-java archives: