From 483f751c37b4de4cffe10a575577d617c9eed8f4 Mon Sep 17 00:00:00 2001 From: b8raoult <53792887+b8raoult@users.noreply.github.com> Date: Tue, 30 Mar 2021 17:00:11 +0100 Subject: [PATCH] Disable the cfgrib backend if eccodes is not installed (#5083) * 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 * Update xarray/backends/cfgrib_.py Co-authored-by: Mathias Hauser * 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 --- doc/whats-new.rst | 1 + xarray/backends/cfgrib_.py | 8 +++++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 77d6296acac..2d4cf6771ab 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -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 `_. +- Disable the `cfgrib` backend if the `eccodes` library is not installed (:pull:`5083`). By `Baudouin Raoult `_. Breaking changes ~~~~~~~~~~~~~~~~ diff --git a/xarray/backends/cfgrib_.py b/xarray/backends/cfgrib_.py index c63e1543746..92c3fb0b286 100644 --- a/xarray/backends/cfgrib_.py +++ b/xarray/backends/cfgrib_.py @@ -1,4 +1,5 @@ import os +import warnings import numpy as np @@ -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: