This archive contains answers to questions sent to Unidata support through mid-2025. Note that the archive is no longer being updated. We provide the archive for reference; many of the answers presented here remain technically correct, even if somewhat outdated. For the most up-to-date information on the use of NSF Unidata software and data services, please consult the Software Documentation first.
=============================================================================== Robb Kambic Unidata Program Center Software Engineer III Univ. Corp for Atmospheric Research address@hidden WWW: http://www.unidata.ucar.edu/ =============================================================================== ---------- Forwarded message ---------- Date: Fri, 2 Oct 2009 13:14:41 -0600 (MDT) From: Robb Kambic <address@hidden> To: Esteban Wagner <address@hidden> Subject: 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 thevalues 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/LonOne 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
address@hidden WWW: http://www.unidata.ucar.edu/
===============================================================================