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

Add unit support for reduce_point_density #1132

Merged
merged 2 commits into from Aug 26, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions metpy/calc/tests/test_calc_tools.py
Expand Up @@ -158,6 +158,22 @@ def test_reduce_point_density(thin_point_data, radius, truth):
assert_array_equal(reduce_point_density(thin_point_data, radius=radius), truth)


@pytest.mark.parametrize('radius, truth',
[(2.0, np.array([1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0], dtype=np.bool)),
(1.0, np.array([1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 1, 0], dtype=np.bool)),
(0.3, np.array([1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0,
0, 0, 0, 0, 0, 1, 0, 0, 0, 0], dtype=np.bool)),
(0.1, np.array([1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1,
0, 1, 1, 1, 1, 1, 1, 1, 1, 1], dtype=np.bool))
])
def test_reduce_point_density_units(thin_point_data, radius, truth):
r"""Test that reduce_point_density works with units."""
assert_array_equal(reduce_point_density(thin_point_data * units.dam,
radius=radius * units.dam), truth)


@pytest.mark.parametrize('radius, truth',
[(2.0, np.array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 1], dtype=np.bool)),
Expand Down
18 changes: 13 additions & 5 deletions metpy/calc/tools.py
Expand Up @@ -259,14 +259,15 @@ def reduce_point_density(points, radius, priority=None):
data), returning a mask that can be used to select the points from one or more arrays
(e.g. arrays of temperature and dew point). The points selected can be controlled by
providing an array of ``priority`` values (e.g. rainfall totals to ensure that
stations with higher precipitation remain in the mask).
stations with higher precipitation remain in the mask). The points and radius can be
specified with units. If none are provided, meters are assumed.

Parameters
----------
points : (N, K) array-like
N locations of the points in K dimensional space
radius : float
minimum radius allowed between points
radius : `pint.Quantity` or float
Minimum radius allowed between points. If units are not provided, meters is assumed.
priority : (N, K) array-like, optional
If given, this should have the same shape as ``points``; these values will
be used to control selection priority for points.
Expand All @@ -279,12 +280,19 @@ def reduce_point_density(points, radius, priority=None):
Examples
--------
>>> metpy.calc.reduce_point_density(np.array([1, 2, 3]), 1.)
array([ True, False, True])
array([ True, False, True])
>>> metpy.calc.reduce_point_density(np.array([1, 2, 3]), 1.,
... priority=np.array([0.1, 0.9, 0.3]))
array([False, True, False])
array([False, True, False])

"""
# Handle input with units. Assume meters if units are not specified
if hasattr(radius, 'units'):
radius = radius.to('m').m

if hasattr(points, 'units'):
points = points.to('m').m

# Handle 1D input
if points.ndim < 2:
points = points.reshape(-1, 1)
Expand Down