如何迭代grib2文件的纬度经度值?

时间:2016-06-03 10:06:04

标签: python numpy weather grib noaa

我试图获取grib2文件中所有lat,lng的uvIndex。 这是我获取文件的link。问题是我无法理解文件的结构,因此我可以获取数据。我使用pygrib来阅读文件。

这是我尝试过的代码:

grbs = pygrib.open('uv.t12z.grbf01.grib2')
grb = grbs.select(name='UV index')[0]
print grb.data(23.5,55.5)

我想要实现的是迭代所有lat long并打印相应的uvIndex值或输入lat long并获得相应的值。阅读pygrib的文档但无法找到符合我目的的任何合适的命令。请帮忙。

1 个答案:

答案 0 :(得分:3)

您必须迭代GRIB文件并找到所需的记录,然后获取数据,如下所示:

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

要使lat和lon数组使用:lt, ln = g.latlons()g - grbs的元素。

阅读https://software.ecmwf.int/wiki/display/GRIB/GRIB+API+examples部分python中的示例(pygrib使用此库读取GRIB)。

从大型GRIB文件获取数据的最快方法是制作索引:

# 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)