pygrib icon indicating copy to clipboard operation
pygrib copied to clipboard

How to extract data on a particular lat and long?

Open wxguy opened this issue 9 years ago • 5 comments

I am able to extract the data for a particular region with the help of documentation. How do I extract for a specif location say lat 13.0 and lon 80.0?

wxguy avatar Jan 26 '16 15:01 wxguy

I am also interested in a solution for this. Or at least documentation on the structure of the message value arrays.

jzalger avatar Mar 06 '16 04:03 jzalger

Understand that a grib message is a 2D array of data, and the entire message needs to be decoded at once. You can't just decode one grid point on the grid. The best way to extract one point is to grab the entire 2d grid, then use the lat and lon arrays to determine the grid index of the point you want (or interpolate to that point).

jswhit avatar Mar 07 '16 16:03 jswhit

Is there any built-in implementation for data interpolation in pygrib? e.g. something similar to the wgrib2 command: $wgrib2 originalfile.grb2 -set_grib_type same -new_grid_winds earth -new_grid latlon -127:30:0.5 22:30:0.5 newfile.grb2

which interpolate the data to lat/lon meshgrid? I have tried using scipy interp2d for the task, but (perhaps as I need to cover a large area), this was too heavy (compared to the wgrib command which runs in <sec).

yehelon avatar May 25 '16 12:05 yehelon

grbs_old = pygrib.open('nomads.ncep.noaa.gov/cgi-bin/'+str(grib_filename))
data=grbs_old[6].values

The command above will print a 2d array with dimmensions 720 x 360 ( my grib file is 0.5 deg * 0.5 deg).

[[ 52.87586589 52.87586589 52.87586589 ... 52.87586589 52.87586589 52.87586589] [ 51.75276693 51.78610026 51.81443359 ... 51.61772786 51.64939453 51.72276693] [ 49.90931641 49.96931641 50.02431641 ... 49.70261068 49.75594401 49.81427734] ... [-11.11582031 -11.15415365 -11.18915365 ... -11.05585938 -11.08752604 -11.08082031] [ -8.13052734 -8.10548828 -8.12048828 ... -8.08886068 -8.10219401 -8.11552734] [ -4.54554687 -4.54554687 -4.54554687 ... -4.54554687 -4.54554687 -4.54554687]]

To find specific coordinates within this 720 x 360 array i just extrapolate the coordinates to the array. Ex: If I want to find data for New York New York coordinates ~40.5N 74 W I'll map those coordinates to the array to by doing 2x(90-40.5) and 2x (180-74) to find the following result:

grbs_old = pygrib.open('nomads.ncep.noaa.gov/cgi-bin/'+str(grib_filename))
data=grbs_old[6].values
data[99][212]

This returns the temperature result converted to Fahrenheit of (79 degrees).

mavengences avatar Jul 24 '19 06:07 mavengences

Right now I am performing the steps above, but my extrapolation is not correct.

To fix this problem I need to know how grib files are formatted in relation to latitude and longitude. Are they formatted top to bottom or bottom to top?

Ex if I am looking for coordinates 40.5N and 75 W do I find the corresponding grib file by doing 2x(90-40.5) North coordinate and 2x(180-74) West Coordinate to find data[99][212] Or do I something else like 2x(180-40.5) North and 2x(360-74) west?

mavengences avatar Jul 24 '19 06:07 mavengences