前言
GRIB格式是一種應用於氣象領域的高效存儲格式,由世界氣象組織進行標准化。當前有3個版本的GRIB格式,目前GRIB1和GRIB2在廣泛使用。
Python讀取grib文件可以選擇安裝xarray工具包和cfgrib。
安裝
xarray github地址:https://github.com/pydata/xarray
cfgrib github地址:https://github.com/ecmwf/cfgrib
GRIB 指導文檔: https://www.nco.ncep.noaa.gov/pmb/docs/grib2/
首先要安裝一下ECMWF的cfgrib庫,python模塊依賴於ecmwf eccode 二進制庫 必須安裝在系統上並作為共享庫訪問。
在ubuntu 18.04上使用以下命令:
sudo apt-get install libeccodes0
使用pip工具安裝:
pip install xarray
# 安裝cfgrib需先安裝二進制共享庫
pip install cfgrib
Python讀取示例
# 讀取grib1數據
ds = xr.open_dataset('example.grib', engine='cfgrib')
print(ds)
# 讀取grib2數據
# 目前cfgrib庫無法同時讀取多個typeOfLevel,因此需要根據提示篩選我們需要的數據。
ds = xr.open_dataset('example.grb2', engine="cfgrib", backend_kwargs={'filter_by_keys': {'typeOfLevel': 'isobaricInhPa'}})
print(ds)