Skip to content

Commit

Permalink
Fix masked arrays breaking wind_direction Unidata#1390
Browse files Browse the repository at this point in the history
  • Loading branch information
rpmanser authored and dopplershift committed Aug 5, 2020
1 parent 4609796 commit 9ff7d29
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/metpy/calc/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ def wind_direction(u, v, convention='from'):
elif convention not in ('to', 'from'):
raise ValueError('Invalid kwarg for "convention". Valid options are "from" or "to".')

wdir[wdir <= 0] += 360. * units.deg
mask = wdir <= 0
if np.any(mask):
wdir[mask] += 360. * units.deg
# avoid unintended modification of `pint.Quantity` by direct use of magnitude
calm_mask = (np.asarray(u.magnitude) == 0.) & (np.asarray(v.magnitude) == 0.)
# np.any check required for legacy numpy which treats 0-d False boolean index as zero
Expand Down
17 changes: 17 additions & 0 deletions tests/calc/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,23 @@ def test_direction():
assert_array_almost_equal(true_dir, direc, 4)


def test_direction_masked():
"""Test calculating wind direction from masked wind components."""
mask = np.array([True, False, True, False])
u = np.array([4., 2., 0., 0.])
v = np.array([0., 2., 4., 0.])

u_masked = units.Quantity(np.ma.array(u, mask=mask), units('m/s'))
v_masked = units.Quantity(np.ma.array(v, mask=mask), units('m/s'))

direc = wind_direction(u_masked, v_masked)

true_dir = np.array([270., 225., 180., 0.])
true_dir_masked = units.Quantity(np.ma.array(true_dir, mask=mask), units.deg)

assert_array_almost_equal(true_dir_masked, direc, 4)


def test_direction_with_north_and_calm():
"""Test how wind direction handles northerly and calm winds."""
u = np.array([0., -0., 0.]) * units('m/s')
Expand Down

0 comments on commit 9ff7d29

Please sign in to comment.