pythongfspygrib

how to use pygrib expand_reduce functionality?


I am using pygrib to open GFS data, I want the data to be in unstructured format (not the default option). in short, How can I set expand_reduce to False?

grbs = pygrib.open(filename)
print(grbs[1].expand_reduce) # this prints True (default)

In the docs (https://jswhit.github.io/pygrib/api.html) it says:

@ivar expand_reduced: If True (default), reduced lat/lon and gaussian grids will be expanded to regular grids when data is accessed via "values" key. If False, data is kept on unstructured reduced grid, and is returned in a 1-d array.

I took a look at the source code (https://searchcode.com/file/11567389/pygrib.pyx/) and found that it is a private attribute, and that it is hard coded in the _create_gribmessage method.

 625 cdef _create_gribmessage(grib_handle *gh, object messagenumber):
 626    """factory function for creating gribmessage instances"""
 627    cdef gribmessage grb  = gribmessage.__new__(gribmessage)
 628    grb.messagenumber = messagenumber
 629    grb.expand_reduced = True   ## HERE <---
 630    grb._gh = grib_handle_clone(gh)
 631    grb._all_keys = grb.keys()
 632    grb._ro_keys  = grb._read_only_keys()
 633    grb._set_projparams() # set projection parameter dict.
 634    return setdates(grb)

Solution

  • The solution is to call expand_grid, this will modify the expand_reduced:

    grbs[1].expand_grid(False)
    print(grbs[1].expand_reduce) # this now prints False