Reputation: 3386
I'm trying to get the uvIndex
of all the lat,lng present in a grib2 file.
This is the link from where I'm getting the file. The problem is I'm not able to understand the structure of the file so that I can get the data. I'm using pygrib
to read the file.
Here's the code I've tried:
grbs = pygrib.open('uv.t12z.grbf01.grib2')
grb = grbs.select(name='UV index')[0]
print grb.data(23.5,55.5)
What I'm trying to achieve is either iterate over all the lat longs and print the corresponding uvIndex value or enter a lat long and get the corresponding value. Read the docs of pygrib
but couldn't find any suitable command that will serve my purpose. Please help.
Upvotes: 2
Views: 3307
Reputation: 36655
You have to iterate though GRIB file and find desirable record, then get data, like here:
for g in grbs:
print g.shortName, g.typeOfLevel, g.level # print info about all GRIB records and check names
if (g.shortName == shortName and g.typeOfLevel == typeOfLevel and g.level == level):
tmp = np.array(g.values)
# now work with tmp as numpy array
To get lat and lon arrays use: lt, ln = g.latlons()
, g
- element of grbs
.
Read the examples in Section python at https://software.ecmwf.int/wiki/display/GRIB/GRIB+API+examples (pygrib use this library to read GRIB).
The fastest way to get data from large GRIB file is to make index:
# use attributes what you want to build index
indx = pygrib.index(gribfile,'typeOfLevel','level','parameterName')
# important: msg is an array and may have more then one record
# get U wind component on 10 m above ground
msg = indx.select(level = 10, typeOfLevel = "heightAboveGround",
parameterName = "U U-component of wind m s**-1")
u10 = np.array(msg[0].values)
# get V wind component on 10 m above ground
msg = indx.select(level = 10, typeOfLevel = "heightAboveGround",
parameterName = "V V-component of wind m s**-1")
v10 = np.array(msg[0].values)
Upvotes: 3