通过下面代码读取gfs数据出错:
import xarray as xr
data = xr.open_dataset('gfs.t18z.pgrb2.0p25.f006', engine='cfgrib')
print(data)
错误提示:
/home/ricky/anaconda3/lib/python3.9/site-packages/xarray/backends/cfgrib_.py:27: UserWarning: Failed to load cfgrib - most likely there is a problem accessing the ecCodes library. Try `import cfgrib` to get the full error message
warnings.warn(
/home/ricky/anaconda3/lib/python3.9/site-packages/xarray/backends/plugins.py:65: RuntimeWarning: Engine 'cfgrib' loading failed:
Cannot find the ecCodes library
warnings.warn(f"Engine {name!r} loading failed:\n{ex}", RuntimeWarning)
Traceback (most recent call last):
File "/home/ricky/PycharmProjects/python3.9/main.py", line 4, in <module>
data = xr.open_dataset(path, engine='cfgrib')
File "/home/ricky/anaconda3/lib/python3.9/site-packages/xarray/backends/api.py", line 481, in open_dataset
backend = plugins.get_backend(engine)
File "/home/ricky/anaconda3/lib/python3.9/site-packages/xarray/backends/plugins.py", line 160, in get_backend
raise ValueError(
ValueError: unrecognized engine cfgrib must be one of: ['netcdf4', 'scipy', 'store']
在代码段中添加
import cfgrib
依然报错。
为工程重新创建conda环境, 并重新安装
conda install xarray
conda install -c http://conda.anaconda.org/conda-forge eccodes
conda install -c http://conda.anaconda.org/conda-forge cfgrib
在pycharm工程中更改解释器为新创建的解释器,运行程序,成功读取。
<xarray.Dataset>
Dimensions: (isobaricInhPa: 5, latitude: 721, longitude: 1440)
Coordinates:
time datetime64[ns] ...
step timedelta64[ns] ...
* isobaricInhPa (isobaricInhPa) float64 70.0 50.0 40.0 30.0 20.0
* latitude (latitude) float64 90.0 89.75 89.5 ... -89.5 -89.75 -90.0
* longitude (longitude) float64 0.0 0.25 0.5 0.75 ... 359.2 359.5 359.8
valid_time datetime64[ns] ...
Data variables:
u (isobaricInhPa, latitude, longitude) float32 ...
v (isobaricInhPa, latitude, longitude) float32 ...
Attributes:
GRIB_edition: 2
GRIB_centre: kwbc
GRIB_centreDescription: US National Weather Service - NCEP
GRIB_subCentre: 0
Conventions: CF-1.7
institution: US National Weather Service - NCEP
history: 2022-10-10T17:18 GRIB to CDM+CF via cfgrib-0.9.1...