1. 程式人生 > 其它 >使用Python讀取GRIB格式資料

使用Python讀取GRIB格式資料

前言

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)

資料下載

下載地址:https://nomads.ncep.noaa.gov/