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

Masked arrays break wind_direction function #1390

Closed
rpmanser opened this issue Jun 8, 2020 · 1 comment · Fixed by #1401
Closed

Masked arrays break wind_direction function #1390

rpmanser opened this issue Jun 8, 2020 · 1 comment · Fixed by #1401
Labels
Area: Calc Pertains to calculations Type: Bug Something is not working like it should
Milestone

Comments

@rpmanser
Copy link
Contributor

rpmanser commented Jun 8, 2020

Masked arrays break the wind_direction function when all calculated directions are greater than 0 degrees.

import numpy as np
import metpy.calc as mpcalc
from metpy.units import units

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'))

dirs = mpcalc.wind_direction(u_masked, v_masked)

Which results in the following error:
ValueError: can only convert an array of size 1 to a Python scalar

at this line of the function:

wdir[wdir <= 0] += 360. * units.deg

which only occurs when all values in wdir are greater than 0.

The traceback goes through the pint and numpy masked array libraries, so this may be an upstream problem. If different units are specified and a similar operation is performed, things work as expected. For example,

temperature = units.Quantity(np.ma.array([250., 260., 270., 280.], mask=mask), units.kelvin)
temperature[temperature <= 0.] += 10. * units.kelvin

works just fine.

A temporary work around could involve just checking if the magnitude of wdir is a masked array.

Linux Mint 18.3 Sylvia
MetPy version 1.0.0
NumPy version 1.18.4

@rpmanser rpmanser added the Type: Bug Something is not working like it should label Jun 8, 2020
@dopplershift dopplershift added the Area: Calc Pertains to calculations label Jul 14, 2020
@dopplershift
Copy link
Member

I'm not sure why the other one works. If a boolean operation like that returns all False, using that to get items from an array returns an empty scalar, which we should not then be adding to. This should probably change to something like:

mask = wdir <= 0
if np.any(mask):
    wdir[mask] += ...

Wanna submit a PR to fix this?

rpmanser added a commit to rpmanser/MetPy that referenced this issue Jul 14, 2020
@dopplershift dopplershift added this to the 0.12.2 milestone Jul 28, 2020
rpmanser added a commit to rpmanser/MetPy that referenced this issue Jul 28, 2020
dopplershift added a commit that referenced this issue Jul 28, 2020
Fix masked arrays breaking wind_direction #1390
dopplershift pushed a commit to dopplershift/MetPy that referenced this issue Aug 5, 2020
dopplershift pushed a commit to dopplershift/MetPy that referenced this issue Aug 5, 2020
dopplershift pushed a commit to dopplershift/MetPy that referenced this issue Aug 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: Calc Pertains to calculations Type: Bug Something is not working like it should
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants