Skip to content

Commit

Permalink
Use array_type fixture; fix bugs for masked array inputs to functions
Browse files Browse the repository at this point in the history
* Use `array_type` as needed `test_basic.py`
* Fix bug in `wind_direction()` for masked arrays when calm. Boolean
  indexing of a masked array resulted in modification of the mask
  itself.
* Fix bug in `smooth_gaussian()` where function returned a
  `numpy.ndarray` when given a `numpy.ma.array`
* Remove `test_direction_masked()`
  • Loading branch information
rpmanser committed Aug 14, 2021
1 parent a56c399 commit 77f4813
Show file tree
Hide file tree
Showing 2 changed files with 219 additions and 169 deletions.
16 changes: 13 additions & 3 deletions src/metpy/calc/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,14 @@ def wind_direction(u, v, convention='from'):
wdir[mask] += units.Quantity(360., 'deg')
# avoid unintended modification of `pint.Quantity` by direct use of magnitude
calm_mask = (np.asarray(u.magnitude) == 0.) & (np.asarray(v.magnitude) == 0.)

if hasattr(wdir, "mask"):
array_mask = wdir.mask
else:
array_mask = False

# np.any check required for legacy numpy which treats 0-d False boolean index as zero
if np.any(calm_mask):
if np.any(calm_mask & np.logical_not(array_mask)):
wdir[calm_mask] = units.Quantity(0., 'deg')
return wdir.reshape(origshape).to('degrees')

Expand Down Expand Up @@ -798,8 +804,12 @@ def smooth_gaussian(scalar_grid, n):
# Assume the last two axes represent the horizontal directions
sgma_seq = [sgma if i > num_ax - 3 else 0 for i in range(num_ax)]

# Compute smoothed field
return gaussian_filter(scalar_grid, sgma_seq, truncate=2 * np.sqrt(2))
filter_args = {'sigma': sgma_seq, 'truncate': 2 * np.sqrt(2)}
if hasattr(scalar_grid, 'mask'):
smoothed = gaussian_filter(scalar_grid.data, **filter_args)
return np.ma.array(smoothed, mask=scalar_grid.mask)
else:
return gaussian_filter(scalar_grid, **filter_args)


@exporter.export
Expand Down
Loading

0 comments on commit 77f4813

Please sign in to comment.