2

Anybody here doing meteorological or earth-sciences work? I am trying to work with GRIB2 format data files. There are libraries available, in particular the Unidata GRIB Java Decoder. And I can see exactly how to extract data in a big linear array, but what I want to do is access values by lat/long. Can't find any simple example on how to do that. Suggestions?

TIA!

Drew Noakes
  • 300,895
  • 165
  • 679
  • 742
Rick Wayne
  • 1,503
  • 2
  • 12
  • 11

5 Answers5

2

Using python with the pygrib module usually works like a charm. You can extract lats/lons and data with the following code.

import pygrib

gr=pygrib.open(file)
data=values.(gr[key]) #use the key to the variable of interest to extract its data
lats,lons=(gr.readline()).latlons() #extract coordinates

Now its easy visualize the data with basemap toolkit or export it to a suitable file:)

petny
  • 21
  • 3
1

The wgrib2 program allows you to extract a time series at a chosen lat/lon location using the -lon option, for example:

wgrib2.exe input_file.grb2 -lon 360 90 > output_file.txt

KAE
  • 815
  • 1
  • 13
  • 32
1

You can use the GRIB2Tools, see https://github.com/philippphb/GRIB2Tools. After reading in a GRIB2 file from an InputStream like

RandomAccessGribFile gribFile = new RandomAccessGribFile("", "");
gribFile.importFromStream(inputstream, 0);

you can access the data of the GRIB file based on lat/lon:

double longitude = ...   // in degrees
double latitude = ...    // in degrees
float val = gribFile.getValueAt(0, GribFile.degToUnits(latitude), GribFile.degToUnits(longiude));

You can also get interpolated data for lat/lon positions that are not exactly on the grid:

double longitude = ...   // in degrees
double latitude = ...    // in degrees
float val = gribFile.interpolateValueAt(0, GribFile.degToUnits(latitude), GribFile.degToUnits(longiude));
gisdev_p
  • 146
  • 3
1

You will need to get the projection type along with parameters, then use that to map the coordinates from lat/long to grid x/y (see f.e. the class CoordinateReferenceSystem from the geotools website).

Tassos Bassoukos
  • 16,017
  • 2
  • 36
  • 40
  • Thanks! I guess if I have to build that, I'm going to use hackier solutions (like just using wgrib2 repeatedly!). I strive for elegance, but "working quickly" has an elegance all its own... – Rick Wayne Sep 12 '11 at 15:18
1

You can use the DEGRIB tool to probe data at a specified lat/long. See here http://www.weather.gov/mdl/degrib/txtview.php?file=degrib.txt&dir=base A windows example would be 'degrib.exe myfile.grib -P -pnt 40.0,-10.0 -Interp 2'. If you need DEGRIB.EXE you can find it in the installation directory of a program called VRTOOL http://www.tecepe.com.br/nav/vrtool/