From 57204cecdfc45d587d5440b9d70363a7abd0eda9 Mon Sep 17 00:00:00 2001 From: Steve Decker Date: Thu, 14 Dec 2023 09:51:49 -0500 Subject: [PATCH] Check for duplicate levels in isentropic_interpolation_as_dataset Fixes #3309 by provided the user with a warning when interpolation to a duplicate isentropic surface is requested. This is likely a mistake on the part of the user, but by issuing a warning rather than an error, computations can proceed if the duplication was really intended. The code used here is an adaptation of the duplicate-level check found in _parcel_profile_helper elsewhere in thermo.py. --- src/metpy/calc/thermo.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/metpy/calc/thermo.py b/src/metpy/calc/thermo.py index eeba567cd6a..0f36e33efd9 100644 --- a/src/metpy/calc/thermo.py +++ b/src/metpy/calc/thermo.py @@ -2826,6 +2826,13 @@ def isentropic_interpolation_as_dataset( # Ensure matching coordinates by broadcasting all_args = xr.broadcast(temperature, *args) + # Check for duplicate isentropic levels, which can be problematic (Issue #3309) + unique, counts = np.unique(levels.m, return_counts=True) + if np.any(counts > 1): + _warnings.warn(f'Duplicate level(s) {unique[counts > 1]} provided. ' + 'The output Dataset includes duplicate levels as a result. ' + 'This may cause xarray to crash when working with this Dataset!') + # Obtain result as list of Quantities ret = isentropic_interpolation( levels,