From 221c6d4c33a8fe814c3d1d28fa9c21a5c970e567 Mon Sep 17 00:00:00 2001 From: James Varndell Date: Tue, 24 Sep 2024 22:18:00 +0100 Subject: [PATCH 1/2] Raise useful error when scipy is not installed but a user tries to apply interpolation --- src/earthkit/plots/geo/grids.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/earthkit/plots/geo/grids.py b/src/earthkit/plots/geo/grids.py index 8817fb5..2ef1bd1 100644 --- a/src/earthkit/plots/geo/grids.py +++ b/src/earthkit/plots/geo/grids.py @@ -13,7 +13,12 @@ # limitations under the License. import numpy as np -from scipy.interpolate import griddata + +_NO_SCIPY = False +try: + from scipy.interpolate import griddata +except ImportError: + _NO_SCIPY = True def is_structured(lat, lon, tol=1e-5): @@ -70,6 +75,8 @@ def interpolate_unstructured(x, y, z, resolution=1000, method="linear"): - grid_y: 2D grid of y-coordinates. - grid_z: 2D grid of interpolated z-values, with NaNs in large gap regions. """ + if _NO_SCIPY: + raise ImportError("The 'scipy' package is required for interpolating unstructured data.") # Filter out NaN values from z and corresponding x, y mask = ~np.isnan(z) x_filtered = x[mask] From 9b33c73c5cfabbe9bcd689554b81aa02f903fcb6 Mon Sep 17 00:00:00 2001 From: James Varndell Date: Tue, 24 Sep 2024 22:20:37 +0100 Subject: [PATCH 2/2] QA tweaks --- src/earthkit/plots/geo/grids.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/earthkit/plots/geo/grids.py b/src/earthkit/plots/geo/grids.py index 2ef1bd1..1db379d 100644 --- a/src/earthkit/plots/geo/grids.py +++ b/src/earthkit/plots/geo/grids.py @@ -76,7 +76,9 @@ def interpolate_unstructured(x, y, z, resolution=1000, method="linear"): - grid_z: 2D grid of interpolated z-values, with NaNs in large gap regions. """ if _NO_SCIPY: - raise ImportError("The 'scipy' package is required for interpolating unstructured data.") + raise ImportError( + "The 'scipy' package is required for interpolating unstructured data." + ) # Filter out NaN values from z and corresponding x, y mask = ~np.isnan(z) x_filtered = x[mask]