cfgrib - GRIB to NetCDF/CF via ecCodes
cfgrib is a Python interface developed by ECMWF that maps GRIB files to the NetCDF Common Data Model, adhering to the CF (Climate and Forecast) Conventions. It leverages the underlying C library ecCodes for efficient GRIB decoding and integrates seamlessly with xarray for data representation. The library is actively maintained by ECMWF, with regular releases addressing new features and bug fixes, typically every few months.
Warnings
- gotcha The `cfgrib` library is a Python wrapper around the ECMWF `ecCodes` C library. `ecCodes` must be installed separately and accessible in your system's PATH (or LD_LIBRARY_PATH on Linux/macOS) for `cfgrib` to function. `pip install cfgrib` does NOT install `ecCodes` itself.
- gotcha GRIB files can contain large volumes of data. Loading an entire file into an `xarray.Dataset` can consume significant memory, potentially leading to `MemoryError` or slow performance. `cfgrib` typically loads data lazily, but computations or slicing can trigger full loading into memory.
- gotcha While `cfgrib` aims for CF-compliance, some GRIB files may have non-standard metadata or structures that lead to unexpected coordinate interpretations (e.g., missing time dimensions, non-standard vertical levels, or unexpected grouping of parameters). This can require manual `set_index` or other `xarray` operations.
- breaking Prior to xarray v2022.09.0, `xarray.open_dataset` used `backend_kwargs` to pass arguments to the underlying engine (`cfgrib` in this case). This keyword argument was renamed to `engine_kwargs` in newer xarray versions.
Install
-
conda install -c conda-forge cfgrib eccodes -
pip install cfgrib # IMPORTANT: ecCodes C library must be pre-installed and discoverable for pip installation to work.
Imports
- open_dataset
from cfgrib import open_dataset
Quickstart
import cfgrib
import xarray as xr
import os
# --- IMPORTANT: Obtain a GRIB file for this example ---
# cfgrib requires a GRIB file to operate. For a runnable example:
# 1. Download a sample GRIB file, e.g., from:
# https://github.com/ecmwf/cfgrib/blob/main/tests/grib_files/era5-levels-members.grib
# 2. Save it as 'sample.grib2' in the same directory as this script,
# or specify its full path.
#
# Alternatively, if you have `earthkit-data` installed:
# import earthkit.data
# ds = xr.open_dataset(earthkit.data.res_source('t.grib'), engine='cfgrib')
grib_file_path = "sample.grib2"
if not os.path.exists(grib_file_path):
print(f"Warning: The GRIB file '{grib_file_path}' was not found.")
print("Please download a sample GRIB file and place it in the current directory or update the path.")
print("Skipping `open_dataset` call for this demonstration.")
else:
try:
# Open the GRIB file as an xarray Dataset
# The `engine='cfgrib'` is crucial for xarray.open_dataset to use cfgrib
ds = xr.open_dataset(grib_file_path, engine="cfgrib")
print("\nDataset loaded successfully:")
print(ds)
# Access a data variable, e.g., 't' for temperature
if 't' in ds:
print("\nTemperature data (first 5 values):")
print(ds['t'].isel(time=0, level=0).values.flatten()[:5])
else:
print("\nNo 't' variable found in the dataset. Available variables:", list(ds.data_vars))
except Exception as e:
print(f"An error occurred while opening the GRIB file: {e}")