There are two ways to use cfgrib to load a GRIB file as an xarray dataset.
1. Using cfgrib through Metview
Metview's Python interface ensures a correct environment, and provides an easy means to convert a Fieldset object into a dataset, using cfgrib behind the scenes to do it. Assuming that the 'python3' and 'metview' modules are loaded, the following code shows a simple example that does this:
import metview as mv ds = (mv.read("ERA5_levels.grib")).to_dataset() print(ds)
The next example shows Metview doing some processing on the data before converting to a dataset - note that the result does not need to be explictly written to disk before the conversion takes place:
import metview as mv g = mv.read("ERA5_levels.grib") u = mv.read(data=g, parameter='u') v = mv.read(data=g, parameter='v') d = mv.divergence(u, v) all = mv.merge(g, d) # add divergence to the Fieldset ds = all.to_dataset() print(ds)
2. Using cfgrib standalone
cfgrib can also be run as a standalone Python package, but it requires a little more setup. cfgrib is installed with the Metview installation, and requires the location of the ecCodes library. Assuming that the 'python3', 'eccodes' and 'metview' modules are loaded, this, run at ECMWF, will allow cfgrib to be used:
export LD_LIBRARY_PATH=$ECCODES_LIB_DIR:$LD_LIBRARY_PATH export PYTHONPATH=$METVIEW_DIR/lib/python3.6/site-packages/:$PYTHONPATH
Now, the following Python script should run:
import xarray as xr ds = xr.open_dataset('ERA5_levels.grib', engine='cfgrib') print(ds)