简体   繁体   English

如何遍历grib2文件的纬度经度值?

[英]How to iterate over latitude longitude values of a grib2 file?

I'm trying to get the uvIndex of all the lat,lng present in a grib2 file. 我试图让uvIndex所有的LAT,LNG存在于GRIB2文件。 This is the link from where I'm getting the file. 这是我获取文件的链接 The problem is I'm not able to understand the structure of the file so that I can get the data. 问题是我无法理解文件的结构,因此我无法获取数据。 I'm using pygrib to read the file. 我正在使用pygrib读取文件。

Here's the code I've tried: 这是我尝试过的代码:

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

What I'm trying to achieve is either iterate over all the lat longs and print the corresponding uvIndex value or enter a lat long and get the corresponding value. 我想要实现的是要么遍历所有经纬度并打印相应的uvIndex值,要么输入经纬度并获得相应的值。 Read the docs of pygrib but couldn't find any suitable command that will serve my purpose. 阅读pygrib的文档,但找不到任何适合我目的的命令。 Please help. 请帮忙。

You have to iterate though GRIB file and find desirable record, then get data, like here: 您必须遍历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

To get lat and lon arrays use: lt, ln = g.latlons() , g - element of grbs . 为了得到lat和经度阵列使用: lt, ln = g.latlons() g -的元件grbs

Read the examples in Section python at https://software.ecmwf.int/wiki/display/GRIB/GRIB+API+examples (pygrib use this library to read GRIB). 阅读https://software.ecmwf.int/wiki/display/GRIB/GRIB+API+examples中的python节中的示例(pygrib使用此库读取GRIB)。

The fastest way to get data from large GRIB file is to make index: 从大型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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM