Skip to content

Commit

Permalink
Check for duplicate levels in isentropic_interpolation_as_dataset
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
sgdecker committed Dec 14, 2023
1 parent 0fe89d5 commit 57204ce
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/metpy/calc/thermo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 57204ce

Please sign in to comment.