Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More descriptive errors for precipitable_water #1926

Merged
merged 2 commits into from
Jun 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/metpy/calc/indices.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,22 @@ def precipitable_water(pressure, dewpoint, *, bottom=None, top=None):

pressure, dewpoint = _remove_nans(pressure, dewpoint)

min_pres = np.nanmin(pressure)
max_pres = np.nanmax(pressure)

if top is None:
top = np.nanmin(pressure)
top = min_pres
elif not min_pres <= top <= max_pres:
raise ValueError(f'The pressure and dewpoint profile ranges from {max_pres} to '
f'{min_pres}, after removing missing values. {top} is outside this '
'range.')

if bottom is None:
bottom = np.nanmax(pressure)
bottom = max_pres
elif not min_pres <= bottom <= max_pres:
raise ValueError(f'The pressure and dewpoint profile ranges from {max_pres} to '
f'{min_pres}, after removing missing values. {bottom} is outside '
'this range.')

pres_layer, dewpoint_layer = get_layer(pressure, dewpoint, bottom=bottom,
depth=bottom - top)
Expand Down
22 changes: 22 additions & 0 deletions tests/calc/test_indices.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from datetime import datetime

import numpy as np
import pytest
import xarray as xr

from metpy.calc import (bulk_shear, bunkers_storm_motion, critical_angle,
Expand Down Expand Up @@ -61,6 +62,27 @@ def test_precipitable_water_nans():
assert_almost_equal(pw, truth, 5)


def test_precipitable_water_descriptive_bound_error():
"""Test that error is raised when bound is outside profile after nan have been removed."""
pressure = np.array([1001, 1000, 997, 977.9, 977, 957, 937.8, 925, 906, 899.3, 887, 862.5,
854, 850, 800, 793.9, 785, 777, 771, 762, 731.8, 726, 703, 700, 655,
630, 621.2, 602, 570.7, 548, 546.8, 539, 513, 511, 485, 481, 468,
448, 439, 424, 420, 412]) * units.hPa
dewpoint = np.array([np.nan, np.nan, -26.8, np.nan, -27.3, -28.2, np.nan, -27.2, -26.6,
np.nan, -27.4, np.nan, -23.5, -23.5, -25.1, np.nan, -22.9, -17.8,
-16.6, np.nan, np.nan, -16.4, np.nan, -18.5, -21., -23.7, np.nan,
-28.3, np.nan, -32.6, np.nan, -33.8, -35., -35.1, -38.1, -40.,
-43.3, -44.6, -46.4, np.nan, np.nan, np.nan]) * units.degC

# Top bound is above highest pressure in profile
with pytest.raises(ValueError, match='The pressure and dewpoint profile ranges from'):
precipitable_water(pressure, dewpoint, top=units.Quantity(415, 'hPa'))

# Bottom bound is below lowest pressure in profile
with pytest.raises(ValueError, match='The pressure and dewpoint profile ranges from'):
precipitable_water(pressure, dewpoint, bottom=units.Quantity(999, 'hPa'))


def test_mean_pressure_weighted():
"""Test pressure-weighted mean wind function with vertical interpolation."""
data = get_upper_air_data(datetime(2016, 5, 22, 0), 'DDC')
Expand Down