gribfile='gfs20191010.0p25' #file containing 384 hours of forecast data for variable Geopotential Height
grbs=pygrib.open(gribfile)
grb1=grbs.select(name='Geopotential Height')[0:24]
day1 = grb1.values

When I run the above code I get the following error:

AttributeError: 'list' object has no attribute 'values'

The .values key will work when only selecting the first item in the list as shown below:

grb1 = grbs.select(name='Geopotential Height')[0]

But not when I do

grb1 = grbs.select(name='Geopotential Height')[0:24]

I understand that for the .values key the format is dict.values, but I cannot figure out how to retrieve the data within the first 24 list items. My goal is to take in the first 24 files for the selected variable (Geopotential Height) as a numpy array, but in order to do this I need to be able to extract the information contained in each file using .values.

I have been following documentation for pygrib found at: https://jswhit.github.io/pygrib/docs/

1

There are 1 best solutions below

0
On

According to the documentation link you provided:

select returns a list of gribmessage instances.

So, when you do this:

grb1=grbs.select(name='Geopotential Height')[0:24]

You are taking the result of select (which is a list), and then slicing that list (which yields another list). The sliced list is then bound to grb1. Therefore, grb1 is a list of gribmessage instances.

As the Pyhton exception states, a list does not have a values attribute. However, a single gribmessage, presumably, does.

Not sure if this gets you closer to the result you're expecting, but you can use a list comprehension to get the values from the gribmessage instances in your list:

values = [grb.values for grb in grb1]