pygrib
pygrib copied to clipboard
huge memory consumption when selecting grib messages
Hi!
I have a piece of code that opens a grib file and gets the messages that correspond to a certain variable name ('Temperature', for instance). The problem is that when selecting all these messages (my grib files contain several thousands of messages) the memory gets full in a few seconds (although the original grib file is a 6.5 Mb file!).
There is my code, so you can see it:
# I wrote a function to get all different variable names, called "get_gribfile_variables". It retrieves the variable name (vn) and the shortname (vsn).
vn, vsn = get_gribfile_variables(file_name)
#Then I index the file:
grb = pg.index(file_name, 'shortName', 'typeOfLevel')
data = []
for sn in vsn:
# Finally I select the data corresponding to each variable
var = grb.select(shortName=sn, typeOfLevel='isobaricInhPa')
data.append(var)
each time the loop gets to var, it fills like half Gb of memory or more. As I said, the total size of the original grib file is 6.5Mb.
Is there a better way to do this?
Would it be possible, for instance, to do the select command only saving some information from the grib message (I just need the values, dataDate, year, month, day, hour, level, latitude and longitude; all the other information stored in the message is not used).
If this is not possible, can you suggest another method, that is also as fast as this? (With this method I save 50% of time w.r.t the open and select method).
Thank you a lot!
Cheers,
Pere