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