Skip to content

Commit

Permalink
BUG: Fix SRH unexpectedly returning masked values
Browse files Browse the repository at this point in the history
  • Loading branch information
dopplershift committed Aug 26, 2018
1 parent 5d0707c commit eb60959
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
6 changes: 6 additions & 0 deletions metpy/calc/kinematics.py
Original file line number Diff line number Diff line change
Expand Up @@ -571,8 +571,14 @@ def storm_relative_helicity(u, v, heights, depth, bottom=0 * units.m,
int_layers = (storm_relative_u[1:] * storm_relative_v[:-1] -
storm_relative_u[:-1] * storm_relative_v[1:])

# Need to manually check for masked value because sum() on masked array with non-default
# mask will return a masked value rather than 0. See numpy/numpy#11736
positive_srh = int_layers[int_layers.magnitude > 0.].sum()
if np.ma.is_masked(positive_srh):
positive_srh = 0.0 * units('meter**2 / second**2')
negative_srh = int_layers[int_layers.magnitude < 0.].sum()
if np.ma.is_masked(negative_srh):
negative_srh = 0.0 * units('meter**2 / second**2')

return (positive_srh.to('meter ** 2 / second ** 2'),
negative_srh.to('meter ** 2 / second ** 2'),
Expand Down
17 changes: 17 additions & 0 deletions metpy/calc/tests/test_kinematics.py
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,23 @@ def test_storm_relative_helicity_agl():
assert_almost_equal(total_srh, 300. * units('meter ** 2 / second ** 2 '), 6)


def test_storm_relative_helicity_masked():
"""Test that srh does not return masked values."""
h = np.ma.array([20.72, 234.85, 456.69, 683.21])
u = np.ma.array([-2.32, -3.23, 0.736, 9.07])
v = np.ma.array([8.31, 13.57, 25.56, 30.55])
u = np.ma.array(np.zeros((4,)))
v = np.zeros_like(u)
pos, neg, com = storm_relative_helicity(units.knot * u, units.knot * v, units.meter * h,
depth=500 * units.meter,
storm_u=15.77463015050421 * units('m/s'),
storm_v=21.179437759755647 * units('m/s'))

assert not np.ma.is_masked(pos)
assert not np.ma.is_masked(neg)
assert not np.ma.is_masked(com)


def test_absolute_vorticity_asym():
"""Test absolute vorticity calculation with a complicated field."""
u = np.array([[2, 4, 8], [0, 2, 2], [4, 6, 8]]) * units('m/s')
Expand Down

0 comments on commit eb60959

Please sign in to comment.