GRIB
数据格式简介
GRIB(General Regularly distributed Information in Binary form),是由世界气象组织(WMO)设计和维护的一种用于存储和传输网格数据的标准数据格式,它是一种自描述的二进制压缩格式,通常具有扩展名.grib,.grb或.gb。
世界气象组织一共发布了3各版本的GRIB标准:
- GRIB 版本 0: 已淘汰,无技术支持,目前几乎不再使用。
- GRIB 版本 1: 版本1是GRIB的历史遗留版本,已停止开发。由于它已在国际民航组织(ICAO)的世界范围预报系统中使用,因此仍得到WMO的认可。
- GRIB 版本 2: 版本2格式是GRIB标准的扩展和强化,它与版本1相比在压缩比等性能上有更优异的表现。一些国家的数值天气预报机构(尤其是美国和欧洲)正在逐步采用此版本,版本2不能与版本1兼容。
想了解更多GRIB1和GRIB2的信息,请参考:Introduction to GRIB Edition1 and GRIB Edition 2
GRIB数据格式是以一个被称为“报文”(Message)的数据结构为基本单元的集合体。每个“报文”中会存储一套经纬度、变量数组以及所有描述性的属性信息,而每个GRIB文件里会按顺序排列存储多个“报文”。
处理工具及方法
ecCodes
ecCodes是一个由ECMWF开发的程序包,它可以提供用于解码和编码GRIB格式的API和工具。我们可以使用conda来安装:$ conda install -c conda-forge eccodes
ecCodes提供了一套处理grib数据的命令行工具,你可以使用 grib_dump
, grib_ls
和 grib_get
来查看文件内容,也可以使用 grib_set
和 grib_filter
去修改内容,还可以用 grib_copy
去把部分内容复制出来,或者使用 grib_get_data
从文件中把经纬度和变量值提取出来。 grib_compare
还可以按照键去对不同GRIB文件进行对比。
grib_ls
grib_ls
命令行主要用于查看GIRB文件的内容信息
- 查看GRIB文件所有报文的所有参数
$ grib_ls ERA5_20191231.grib ERA5_20191231.grib edition centre typeOfLevel level dataDate stepRange dataType shortName packingType gridType 1 ecmf surface 0 20191231 0 an 10u grid_simple regular_ll 1 ecmf surface 0 20191231 0 an 10v grid_simple regular_ll 1 ecmf surface 0 20191231 0 an 2d grid_simple regular_ll ... 384 of 384 messages in ERA5_20191231.grib384 of 384 total messages in 1 files
🔹 每列字段含义
字段名 | 含义 |
---|---|
edition | GRIB 的版本,这里是 1 ,即 GRIB1 格式 |
centre | 数据提供中心,ecmf 表示 ECMWF(欧洲中期天气预报中心) |
typeOfLevel | 层类型,这里是 surface ,表示地表或接近地表 |
level | 层高度/层次值,对 surface 是 0 |
dataDate | 数据日期,这里是 20100601 (即 2010 年 6 月 1 日)或 20100602 |
stepRange | 预报步长,0 表示分析场(analysis,即即时状态,不是预报) |
dataType | 数据类型,an 表示 analysis(分析场) |
shortName | 变量短名称 |
2.仅查看报文信息中的shortName和dataType参数
$ grib_ls -p shortName,dataType ERA5_20191231.grib ERA5_20191231.grib shortName dataType 10u an 10v an 2d an ... 384 of 384 messages in ERA5_20191231.grib384 of 384 total messages in 1 files
3.筛选参数shortName为tp的报文信息
$ grib_ls -w shortName=tp ERA5_20191231.grib ERA5_20191231.grib edition centre typeOfLevel level dataDate stepRange dataType shortName packingType gridType 1 ecmf surface 0 20191230 5-6 fc tp grid_simple regular_ll 1 ecmf surface 0 20191230 6-7 fc tp grid_simple regular_ll 1 ecmf surface 0 20191230 7-8 fc tp grid_simple regular_ll ... 384 of 384 messages in ERA5_20191231.grib384 of 384 total messages in 1 files
4.查看距离(25°N,100°E)最近点的paramId,name和值
$ grib_ls -l 25,100,1 -p paramId,name ERA5_20191231.grib ERA5_20191231.grib paramId name value 165 10 metre U wind component -0.229126 166 10 metre V wind component -0.69986 168 2 metre dewpoint temperature 272.847 ... 384 of 384 total messages in 1 files Input Point: latitude=25.00 longitude=100.00 Grid Point chosen #2 index=39460 latitude=25.00 longitude=100.00 distance=0.00 (Km) Other grid Points - 1 - index=39461 latitude=25.00 longitude=100.25 distance=25.18 (Km) - 2 - index=39460 latitude=25.00 longitude=100.00 distance=0.00 (Km) - 3 - index=39180 latitude=25.25 longitude=100.25 distance=37.48 (Km) - 4 - index=39179 latitude=25.25 longitude=100.00 distance=27.78 (Km)
更多详细的参数说明及使用方法可以执行 grib_ls -h
查看帮助文档或阅读ECMWF官方文档: GRIB tools
CDO
conda install -c conda-forge cdo
Python
cfgrib & xarray
conda install -c conda-forge eccodes cfgrib xarray
pygrib
安装: conda install -c conda-forge pygrib
使用方法
In [1]: import pygribIn [2]: msgs = pygrib.open('./20180930-fc-sfc.grib')In [3]: msg1 = msgs[1]In [4]: msg1 Out[4]: 1:Surface pressure:Pa (instant):regular_ll:surface:level 0:fcst time 0 hrs:from 201809301200In [5]: msg1.keys() # 查看所有关键字,此处列出的关键字都可以作为select的过滤条件 Out[5]: ['globalDomain','GRIBEditionNumber','eps',...'section5Length','analDate','validDate']In [6]: msg1.shortName # 查看shortName Out[6]: 'sp'In [10]: msg1.name # 查看name Out[10]: 'Surface pressure'In [17]: lats, lons = msg1.latlons() # 提取经纬坐标In [21]: array = msg1.values # 提取变量值In [23]: tps = msgs.select(shortName='tp') # 选出所有shortName为tp的报文
警告
在pygrib中,打开的grib文件无法自动回收内存,因此在使用完毕grib数据以后,一定要执行 .close() 操作,否则如果打开的grib文件太多,会造成内存溢出。
iris
conda install -c conda-forge iris
pynio
conda install -c conda-forge pynio
NCL
conda install -c conda-forge ncl