Skip to content
1 change: 1 addition & 0 deletions examples/remote/basic.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# sphinx_gallery_skip = True
# Copyright (c) 2023 MetPy Developers.
# Distributed under the terms of the BSD 3-Clause License.
# SPDX-License-Identifier: BSD-3-Clause
Expand Down
4 changes: 2 additions & 2 deletions tests/calc/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,7 @@ def test_smooth_window_1d_dataarray():
dims=('time',),
coords={'time': pd.date_range('2020-01-01', periods=10, freq='h')}
)
xr.testing.assert_allclose(smoothed, truth)
xr.testing.assert_allclose(smoothed, truth, rtol=1e-6)


def test_smooth_rectangular(array_type):
Expand Down Expand Up @@ -840,4 +840,4 @@ def test_zoom_xarray():
'metpy_crs': hght.metpy_crs},
attrs=hght.attrs
)
xr.testing.assert_allclose(zoomed, truth)
xr.testing.assert_allclose(zoomed, truth, rtol=1e-6)
10 changes: 7 additions & 3 deletions tests/interpolate/test_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ def test_inverse_distance_to_grid(method, test_data, test_grid):
None]


@pytest.mark.filterwarnings('ignore::pint.errors.UnitStrippedWarning')
def test_interpolate_to_isosurface():
r"""Test interpolation to level function."""
pv = np.array([[[4.29013406, 4.61736108, 4.97453387, 5.36730237, 5.75500645],
Expand Down Expand Up @@ -240,12 +241,13 @@ def test_interpolate_to_isosurface():
[336.7088576, 336.4165698, 335.6255217, 334.0758288, 331.9684081],
[335.6583567, 336.3500714, 336.6844744, 336.3286052, 335.3874244]])

assert_array_almost_equal(truth, dt_theta)
np.testing.assert_allclose(truth, dt_theta, rtol=1e-6, atol=1e-6)


@pytest.mark.parametrize('assume_units', [None, 'mbar'])
@pytest.mark.parametrize('method', interp_methods)
@pytest.mark.parametrize('boundary_coords', boundary_types)
@pytest.mark.filterwarnings('ignore::pint.errors.UnitStrippedWarning')
def test_interpolate_to_grid(method, assume_units, test_coords, boundary_coords):
r"""Test main grid interpolation function."""
xp, yp = test_coords
Expand Down Expand Up @@ -282,9 +284,11 @@ def test_interpolate_to_grid(method, assume_units, test_coords, boundary_coords)

assert np.all(np.diff(xg, axis=-1) <= hres)
assert np.all(np.diff(yg, axis=0) <= hres)
assert_array_almost_equal(truth, img)

np.testing.assert_allclose(truth, img, rtol=1e-6, atol=1e-6)


@pytest.mark.filterwarnings('ignore::pint.errors.UnitStrippedWarning')
def test_interpolate_to_isosurface_from_below():
r"""Test interpolation to level function."""
pv = np.array([[[1.75, 1.875, 2., 2.125, 2.25],
Expand Down Expand Up @@ -327,4 +331,4 @@ def test_interpolate_to_isosurface_from_below():
[352., 367., 382., 400., 420.],
[356., 371., 390., 410., 430.]])

assert_array_almost_equal(truth, dt_theta)
np.testing.assert_allclose(truth, dt_theta, rtol=1e-6, atol=1e-6)
2 changes: 1 addition & 1 deletion tests/interpolate/test_interpolate_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def test_cressman_weights():
0.470588235294117,
0.219512195121951]

assert_array_almost_equal(truth, weights)
np.testing.assert_allclose(truth, weights, rtol=1e-6)


def test_interpolate_to_grid_pandas():
Expand Down
46 changes: 28 additions & 18 deletions tests/interpolate/test_one_dimension.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,27 +47,30 @@ def test_log_interpolate_1d():
x_interp = np.array([5e3, 5e4, 5e5])
y_interp_truth = np.array([20.0343863828, 24.6395565688, 29.2447267548])
y_interp = log_interpolate_1d(x_interp, x_log, y_log)
assert_array_almost_equal(y_interp, y_interp_truth, 7)
np.testing.assert_allclose(y_interp, y_interp_truth, rtol=1e-7)


@pytest.mark.filterwarnings('ignore::pint.errors.UnitStrippedWarning')
def test_log_interpolate_1d_mixed():
"""Test log interpolation with a mix of compatible input types."""
x_log = xr.DataArray([1e3, 1e4, 1e5, 1e6])
y_log = np.log(x_log) * 2 + 3
x_interp = np.array([5e3, 5e4, 5e5])
y_interp_truth = np.array([20.0343863828, 24.6395565688, 29.2447267548])
y_interp = log_interpolate_1d(x_interp, x_log, y_log)
assert_array_almost_equal(y_interp, y_interp_truth, 7)
np.testing.assert_allclose(y_interp, y_interp_truth, rtol=1e-7)


@pytest.mark.filterwarnings('ignore::pint.errors.UnitStrippedWarning')
def test_log_interpolate_1d_units():
"""Test interpolating with log x-scale with units."""
x_log = np.array([1e3, 1e4, 1e5, 1e6]) * units.hPa
y_log = (np.log(x_log.m) * 2 + 3) * units.degC
x_interp = np.array([5e5, 5e6, 5e7]) * units.Pa
y_interp_truth = np.array([20.0343863828, 24.6395565688, 29.2447267548]) * units.degC
y_interp = log_interpolate_1d(x_interp, x_log, y_log)
assert_array_almost_equal(y_interp, y_interp_truth, 7)
assert y_interp.units == units.degC
np.testing.assert_allclose(y_interp, y_interp_truth, rtol=1e-7)


def test_log_interpolate_2d():
Expand All @@ -77,7 +80,7 @@ def test_log_interpolate_2d():
x_interp = np.array([5e3, 5e4, 5e5])
y_interp_truth = np.array([20.0343863828, 24.6395565688, 29.2447267548])
y_interp = log_interpolate_1d(x_interp, x_log, y_log, axis=1)
assert_array_almost_equal(y_interp[1], y_interp_truth, 7)
np.testing.assert_allclose(y_interp[1], y_interp_truth, rtol=1e-7)


def test_log_interpolate_3d():
Expand All @@ -87,7 +90,7 @@ def test_log_interpolate_3d():
x_interp = np.array([5e3, 5e4, 5e5])
y_interp_truth = np.array([20.0343863828, 24.6395565688, 29.2447267548])
y_interp = log_interpolate_1d(x_interp, x_log, y_log, axis=1)
assert_array_almost_equal(y_interp[0, :, 0], y_interp_truth, 7)
np.testing.assert_allclose(y_interp[0, :, 0], y_interp_truth, rtol=1e-7)


def test_log_interpolate_4d():
Expand All @@ -97,7 +100,7 @@ def test_log_interpolate_4d():
x_interp = np.array([5e3, 5e4, 5e5])
y_interp_truth = np.array([20.0343863828, 24.6395565688, 29.2447267548])
y_interp = log_interpolate_1d(x_interp, x_log, y_log, axis=3)
assert_array_almost_equal(y_interp[0, 0, 0, :], y_interp_truth, 7)
np.testing.assert_allclose(y_interp[0, 0, 0, :], y_interp_truth, rtol=1e-7)


def test_log_interpolate_2args():
Expand All @@ -108,8 +111,8 @@ def test_log_interpolate_2args():
x_interp = np.array([5e3, 5e4, 5e5])
y_interp_truth = np.array([20.0343863828, 24.6395565688, 29.2447267548])
y_interp = log_interpolate_1d(x_interp, x_log, y_log, y_log2)
assert_array_almost_equal(y_interp[1], y_interp_truth, 7)
assert_array_almost_equal(y_interp[0], y_interp_truth, 7)
np.testing.assert_allclose(y_interp[1], y_interp_truth, rtol=1e-7)
np.testing.assert_allclose(y_interp[0], y_interp_truth, rtol=1e-7)


def test_log_interpolate_set_nan_above():
Expand All @@ -120,7 +123,7 @@ def test_log_interpolate_set_nan_above():
y_interp_truth = np.nan
with pytest.warns(Warning):
y_interp = log_interpolate_1d(x_interp, x_log, y_log)
assert_array_almost_equal(y_interp, y_interp_truth, 7)
np.testing.assert_allclose(y_interp, y_interp_truth, equal_nan=True)


def test_log_interpolate_no_extrap():
Expand All @@ -140,7 +143,7 @@ def test_log_interpolate_set_nan_below():
y_interp_truth = np.nan
with pytest.warns(Warning):
y_interp = log_interpolate_1d(x_interp, x_log, y_log)
assert_array_almost_equal(y_interp, y_interp_truth, 7)
np.testing.assert_allclose(y_interp, y_interp_truth, equal_nan=True)


def test_interpolate_2args():
Expand All @@ -151,8 +154,8 @@ def test_interpolate_2args():
x_interp = np.array([2.5000000, 3.5000000])
y_interp_truth = np.array([2.5000000, 3.5000000])
y_interp = interpolate_1d(x_interp, x, y, y2)
assert_array_almost_equal(y_interp[0], y_interp_truth, 7)
assert_array_almost_equal(y_interp[1], y_interp_truth, 7)
np.testing.assert_allclose(y_interp[0], y_interp_truth, rtol=1e-7)
np.testing.assert_allclose(y_interp[1], y_interp_truth, rtol=1e-7)


def test_interpolate_decrease():
Expand All @@ -162,7 +165,7 @@ def test_interpolate_decrease():
x_interp = np.array([3.5000000, 2.5000000])
y_interp_truth = np.array([3.5000000, 2.5000000])
y_interp = interpolate_1d(x_interp, x, y)
assert_array_almost_equal(y_interp, y_interp_truth, 7)
np.testing.assert_allclose(y_interp, y_interp_truth, rtol=1e-7)


def test_interpolate_decrease_xp():
Expand All @@ -172,7 +175,7 @@ def test_interpolate_decrease_xp():
x_interp = np.array([3.5000000, 2.5000000])
y_interp_truth = np.array([3.5000000, 2.5000000])
y_interp = interpolate_1d(x_interp, x, y)
assert_array_almost_equal(y_interp, y_interp_truth, 7)
np.testing.assert_allclose(y_interp, y_interp_truth, rtol=1e-7)


def test_interpolate_end_point():
Expand All @@ -182,24 +185,31 @@ def test_interpolate_end_point():
x_interp = np.array([1.0, 4.0])
y_interp_truth = np.array([1.0, 4.0])
y_interp = interpolate_1d(x_interp, x, y)
assert_array_almost_equal(y_interp, y_interp_truth, 7)
np.testing.assert_allclose(y_interp, y_interp_truth, rtol=1e-7)


@pytest.mark.filterwarnings('ignore::pint.errors.UnitStrippedWarning')
def test_interpolate_masked_units():
"""Test interpolating with masked arrays with units."""
x = units.Quantity(np.ma.array([1., 2., 3., 4.]), units.m)
y = units.Quantity(np.ma.array([50., 60., 70., 80.]), units.degC)
x_interp = np.array([250., 350.]) * units.cm
y_interp_truth = np.array([65., 75.]) * units.degC
y_interp = interpolate_1d(x_interp, x, y)
assert_array_almost_equal(y_interp, y_interp_truth, 7)
assert y_interp.units == units.degC
np.testing.assert_allclose(y_interp, y_interp_truth, rtol=1e-7)


@pytest.mark.filterwarnings('ignore::pint.errors.UnitStrippedWarning')
def test_interpolate_broadcast():
"""Test interpolate_1d with input levels needing broadcasting."""
p = units.Quantity([850, 700, 500], 'hPa')
t = units.Quantity(np.arange(60).reshape(3, 4, 5), 'degC')

t_level = interpolate_1d(units.Quantity(700, 'hPa'), p[:, None, None], t)
assert_array_almost_equal(t_level,
units.Quantity(np.arange(20., 40.).reshape(1, 4, 5), 'degC'), 7)
assert t_level.units == units.degC
np.testing.assert_allclose(
t_level,
units.Quantity(np.arange(20., 40.).reshape(1, 4, 5), 'degC'),
rtol=1e-7
)
36 changes: 28 additions & 8 deletions tests/test_xarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -1406,6 +1406,7 @@ def func(a, b):
assert_array_equal(result_21, expected_21)


@pytest.mark.filterwarnings('ignore::pint.errors.UnitStrippedWarning')
def test_grid_deltas_from_dataarray_lonlat(test_da_lonlat):
"""Test grid_deltas_from_dataarray with a lonlat grid."""
dx, dy = grid_deltas_from_dataarray(test_da_lonlat)
Expand All @@ -1417,8 +1418,12 @@ def test_grid_deltas_from_dataarray_lonlat(test_da_lonlat):
[369802.28173967, 369802.28173967, 369802.28173967, 369802.28173967],
[370009.56291098, 370009.56291098, 370009.56291098,
370009.56291098]]]) * units.m
assert_array_almost_equal(dx, true_dx, 5)
assert_array_almost_equal(dy, true_dy, 5)

assert dx.units == units.m
assert dy.units == units.m

np.testing.assert_allclose(dx, true_dx, rtol=1e-6)
np.testing.assert_allclose(dy, true_dy, rtol=1e-6)


def test_grid_deltas_from_dataarray_xy(test_da_xy):
Expand All @@ -1430,6 +1435,7 @@ def test_grid_deltas_from_dataarray_xy(test_da_xy):
assert_array_almost_equal(dy, true_dy, 5)


@pytest.mark.filterwarnings('ignore::pint.errors.UnitStrippedWarning')
def test_grid_deltas_from_dataarray_actual_xy(test_da_xy, ccrs):
"""Test grid_deltas_from_dataarray with a xy grid and kind='actual'."""
# Construct lon/lat coordinates
Expand All @@ -1453,19 +1459,29 @@ def test_grid_deltas_from_dataarray_actual_xy(test_da_xy, ccrs):
true_dy = [[[[496587.363, 496410.523, 495857.430, 494863.795],
[499498.308, 499429.714, 499191.065, 498689.047],
[499474.250, 499549.538, 499727.711, 499874.122]]]] * units.m
assert_array_almost_equal(dx, true_dx, 2)
assert_array_almost_equal(dy, true_dy, 2)

assert dx.units == units.m
assert dy.units == units.m

np.testing.assert_allclose(dx, true_dx, rtol=1e-6)
np.testing.assert_allclose(dy, true_dy, rtol=1e-6)


@pytest.mark.filterwarnings('ignore::pint.errors.UnitStrippedWarning')
def test_grid_deltas_from_dataarray_nominal_lonlat(test_da_lonlat):
"""Test grid_deltas_from_dataarray with a lonlat grid and kind='nominal'."""
dx, dy = grid_deltas_from_dataarray(test_da_lonlat, kind='nominal')
true_dx = [[[3.333333] * 3]] * units.degrees
true_dy = [[[3.333333]] * 3] * units.degrees
assert_array_almost_equal(dx, true_dx, 5)
assert_array_almost_equal(dy, true_dy, 5)

assert dx.units.is_compatible_with(units.degrees)
assert dy.units.is_compatible_with(units.degrees)

np.testing.assert_allclose(dx, true_dx, rtol=1e-7, atol=1e-5)
np.testing.assert_allclose(dy, true_dy, rtol=1e-7, atol=1e-5)


@pytest.mark.filterwarnings('ignore::pint.errors.UnitStrippedWarning')
def test_grid_deltas_from_dataarray_lonlat_assumed_order():
"""Test grid_deltas_from_dataarray when dim order must be assumed."""
# Create test dataarray
Expand All @@ -1491,8 +1507,12 @@ def test_grid_deltas_from_dataarray_lonlat_assumed_order():
[222031.0111961, 222107.8492205]] * units.m
true_dy = [[175661.5413976, 170784.1311091, 165697.7563223],
[175661.5413976, 170784.1311091, 165697.7563223]] * units.m
assert_array_almost_equal(dx, true_dx, 5)
assert_array_almost_equal(dy, true_dy, 5)

assert dx.units == units.m
assert dy.units == units.m

np.testing.assert_allclose(dx, true_dx, rtol=1e-6)
np.testing.assert_allclose(dy, true_dy, rtol=1e-6)


def test_grid_deltas_from_dataarray_invalid_kind(test_da_xy):
Expand Down