Skip to content

Commit

Permalink
Disable the cfgrib backend if eccodes is not installed (#5083)
Browse files Browse the repository at this point in the history
* Disable the cfgrib backend if eccodes is not installed

If cfgrib is installed, but libeccodes.so is not installed on the system, Xarray does not load as cfgrib will throw a RuntimeError(), which is not caught.

* Update xarray/backends/cfgrib_.py

Co-authored-by: Mathias Hauser <mathause@users.noreply.github.com>

* Update xarray/backends/cfgrib_.py

Co-authored-by: Mathias Hauser <mathause@users.noreply.github.com>

* Update cfgrib_.py

Missing import

* Run black

* Update whats-new.rst

* Update whats-new.rst

* Update xarray/backends/cfgrib_.py

Co-authored-by: Mathias Hauser <mathause@users.noreply.github.com>
  • Loading branch information
b8raoult and mathause authored Mar 30, 2021
1 parent 45b4436 commit 483f751
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
1 change: 1 addition & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ New Features
:py:class:`~core.groupby.DataArrayGroupBy`, inspired by pandas'
:py:meth:`~pandas.core.groupby.GroupBy.get_group`.
By `Deepak Cherian <https://github.com/dcherian>`_.
- Disable the `cfgrib` backend if the `eccodes` library is not installed (:pull:`5083`). By `Baudouin Raoult <https://github.com/b8raoult>`_.

Breaking changes
~~~~~~~~~~~~~~~~
Expand Down
8 changes: 7 additions & 1 deletion xarray/backends/cfgrib_.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import warnings

import numpy as np

Expand All @@ -20,7 +21,12 @@
has_cfgrib = True
except ModuleNotFoundError:
has_cfgrib = False

# cfgrib throws a RuntimeError if eccodes is not installed
except RuntimeError:
warnings.warn(
"Failed to load cfgrib - most likely eccodes is missing. Try `import cfgrib` to get the error message"
)
has_cfgrib = False

# FIXME: Add a dedicated lock, even if ecCodes is supposed to be thread-safe
# in most circumstances. See:
Expand Down

0 comments on commit 483f751

Please sign in to comment.