pygrib学习

pygrib-2.0.3/docs/index.html

导入pygrib模块

  >>> import pygrib

 

打开grib文件,获取grib消息迭代器

  >>> grbs = pygrib.open('sampledata/flux.grb')


和打开正常的python文件对象一样,pygrib使用seek, tell, read, readline, 和close方法打开grib文件。唯一的不同的是,偏移量用grib消息衡量,而不是用字节。

  >>> grbs.seek(2)
  >>> grbs.tell()
  2
  >>> grb = grbs.read(1)[0] # read returns a list with the next N (N=1 in this case) messages.
  >>> grb # printing a grib message object displays summary info
  3:Maximum temperature:K (instant):regular_gg:heightAboveGround:level 2 m:fcst time 108-120 hrs:from 200402291200
  >>> grbs.tell()
  3

 

输出文件清单:

  >>> grbs.seek(0)
  >>> for grb in grbs:
  >>>     grb 
  1:Precipitation rate:kg m**-2 s**-1 (avg):regular_gg:surface:level 0:fcst time 108-120 hrs (avg):from 200402291200
  2:Surface pressure:Pa (instant):regular_gg:surface:level 0:fcst time 120 hrs:from 200402291200
  3:Maximum temperature:K (instant):regular_gg:heightAboveGround:level 2 m:fcst time 108-120 hrs:from 200402291200
  4:Minimum temperature:K (instant):regular_gg:heightAboveGround:level 2 m:fcst time 108-120 hrs:from 200402291200

  

寻找匹配名字的第一个grib消息

  >>> grb = grbs.select(name='Maximum temperature')[0]

  

使用'values'键提取数据值(grb.keys()将会返回一个可用键的序列)

  # The data is returned as a numpy array, or if missing values or a bitmap
  # are present, a numpy masked array.  Reduced lat/lon or gaussian grid
  # data is automatically expanded to a regular grid. Details of the internal
  # representation of the grib data (such as the scanning mode) are handled
  # automatically.
  >>> maxt = grb.values # same as grb['values']
  >>> maxt.shape, maxt.min(), maxt.max()
  (94, 192) 223.7 319.9

  

获取网格经纬度值

  >>> lats, lons = grb.latlons()
  >>> lats.shape, lats.min(), lats.max(), lons.shape, lons.min(), lons.max()
  (94, 192) -88.5419501373 88.5419501373  0.0 358.125

  

获取第二个消息

  >>> grb = grbs.message(2) # same as grbs.seek(1); grb=grbs.readline()
  >>> grb
  2:Surface pressure:Pa (instant):regular_gg:surface:level 0:fcst time 120 hrs:from 200402291200

  

从北美洲子集中提取数据,得到经纬值

  >>> data, lats, lons = grb.data(lat1=20,lat2=70,lon1=220,lon2=320)
  >>> data.shape, lats.min(), lats.max(), lons.min(), lons.max()
  (26, 53) 21.904439458 69.5216630593 221.25 318.75

  

使用存在的键(或者通过属性或者访问字典)修改值

  >>> grb['forecastTime'] = 240
  >>> grb.dataDate = 20100101

  

得到与编码信息相关的二进制字符串

  >>> msg = grb.tostring()
  >>> grbs.close() # close the grib file

  

将修改的信息写到新GRIB文件中

  >>> grbout = open('test.grb','wb')
  >>> grbout.write(msg)
  >>> grbout.close()
  >>> pygrib.open('test.grb').readline() 
  1:Surface pressure:Pa (instant):regular_gg:surface:level 0:fcst time 240 hrs:from 201001011200
Documentation

  

============================================

注意!

select方法为从文件中,提取gribmessage对象的集合到一个列表的方法。得到的结果为一个列表(List对象),不能之前对其求keys属性。只能对单个gribmessage对象求属性!

可行的方法是打开这个List,对其中每一个元素(gribmessage对象)进行循环,分别求keys

gribmessage可以求values

 

index对象的slect方法比pygrib.open 对象的slect方法快,但只是适用于单变量场

 findex = pygrib.index(fname,'level','month','day') # 获取messags索引

selected_messages = findex.select(level=lev,month=month,day=day) # “天选之消息”

注意:findex.select()括号中的关键字必须要和包含前面定义用到的所有关键字,否则会出现错误

 

posted @ 2018-09-26 12:04  chinagod  阅读(6840)  评论(1编辑  收藏  举报