码迷,mamicode.com
首页 > 其他好文 > 详细

pygrib学习

时间:2018-09-26 13:04:36      阅读:1622      评论:0      收藏:0      [点我收藏+]

标签:sel   list   消息   summary   编码   details   不同   经纬度   子集   

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

  

 

 

pygrib学习

标签:sel   list   消息   summary   编码   details   不同   经纬度   子集   

原文地址:https://www.cnblogs.com/jiangleads/p/9705787.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!