我使用pygrib打开GFS数据,我希望数据是非结构化格式(不是默认选项(。简而言之,如何将expand_reduce设置为False
grbs = pygrib.open(filename)
print(grbs[1].expand_reduce) # this prints True (default)
在文档中(https://jswhit.github.io/pygrib/api.html)上面写着:
@ivar expand_rereduced:如果为True(默认值(,则减少lat/lon和高斯网格当数据通过"网格"访问时,将扩展到规则网格;值";钥匙如果错误,数据保存在非结构化精简网格中,并以一维形式返回大堆
我看了一下源代码(https://searchcode.com/file/11567389/pygrib.pyx/)并发现它是一个私有属性,并且它是在create_gribmessage方法中硬编码的。
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)
解决方案是调用expand_grid,这将修改expand_rereduced:
grbs[1].expand_grid(False)
print(grbs[1].expand_reduce) # this now prints False