I am using xarray with cfgrib to load grib files in Python. I have custom grib definitions, which I am providing to eccodes (backend for cfgrib) via the environment variable GRIB_DEFINITION_PATH
.
This setup works well, as long as I run the Python script in an environment where the variable was already set.
Now I want to be more flexible with my setup and provide the environment variable from within Python using os.environ
(see the example below).
But somehow when setting up the environment like this, the variable gets ignored and I don't understand why.
Can anyone provide me some insight into this mystery? Thanks in advance!
Here an "MRE" of the setting.
import xarray as xr
import os
grib_definitions_path = "/paths/to/definitions:/split/like/this"
os.environ["GRIB_DEFINITION_PATH"] = grib_definitions_path
grib_file = '/path/to/grib/file'
backend_args = {
"filter_by_keys": {"shortName": "P"}
}
array = xr.open_dataset(grib_file, engine="cfgrib", encode_cf=("geography", "vertical"), backend_kwargs=backend_args)["P"]
print(array.dims)
Executing the above code in a terminal fails for me with KeyError: 'P'
. If I however first run
export GRIB_DEFINITION_PATH="/paths/to/definitions:/split/like/this"
the dimensions of array
are being printed as expected.