Skip to content

Commit

Permalink
Add a fixture that provides numpy, masked, and dask array functions
Browse files Browse the repository at this point in the history
  • Loading branch information
rpmanser committed Jul 22, 2021
1 parent 4c1d51b commit 177cf45
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
12 changes: 12 additions & 0 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,15 @@ def test_da_xy():
}

return ds.metpy.parse_cf('temperature')


@pytest.fixture(params=['dask', 'masked', 'numpy'])
def array_type(request):
"""Return an array type for testing functions."""
if request.param == 'dask':
dask_array = pytest.importorskip('dask.array', reason='dask.array is not available')
return dask_array.array
elif request.param == 'masked':
return numpy.ma.array
else:
return numpy.array
16 changes: 11 additions & 5 deletions tests/calc/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,22 @@
from metpy.units import units


def test_wind_comps_basic():
def test_wind_comps_basic(array_type):
"""Test the basic wind component calculation."""
speed = np.array([4, 4, 4, 4, 25, 25, 25, 25, 10.]) * units.mph
dirs = np.array([0, 45, 90, 135, 180, 225, 270, 315, 360]) * units.deg
speed = units.Quantity(array_type([4, 4, 4, 4, 25, 25, 25, 25, 10.]), 'mph')
dirs = units.Quantity(array_type([0, 45, 90, 135, 180, 225, 270, 315, 360]), 'deg')
s2 = np.sqrt(2.)

u, v = wind_components(speed, dirs)

true_u = np.array([0, -4 / s2, -4, -4 / s2, 0, 25 / s2, 25, 25 / s2, 0]) * units.mph
true_v = np.array([-4, -4 / s2, 0, 4 / s2, 25, 25 / s2, 0, -25 / s2, -10]) * units.mph
true_u = units.Quantity(
array_type([0, -4 / s2, -4, -4 / s2, 0, 25 / s2, 25, 25 / s2, 0]),
'mph'
)
true_v = units.Quantity(
array_type([-4, -4 / s2, 0, 4 / s2, 25, 25 / s2, 0, -25 / s2, -10]),
'mph'
)

assert_array_almost_equal(true_u, u, 4)
assert_array_almost_equal(true_v, v, 4)
Expand Down

0 comments on commit 177cf45

Please sign in to comment.