| 1 |
# Copyright (c) 2018 MetPy Developers. |
| 2 |
# Distributed under the terms of the BSD 3-Clause License. |
| 3 |
# SPDX-License-Identifier: BSD-3-Clause |
| 4 |
"""Test the `cross_sections` module.""" |
| 5 |
|
| 6 |
import numpy as np |
| 7 |
import pytest |
| 8 |
import xarray as xr |
| 9 |
|
| 10 |
from metpy.calc import (absolute_momentum, cross_section_components, normal_component, |
| 11 |
tangential_component, unit_vectors_from_cross_section) |
| 12 |
from metpy.calc.cross_sections import distances_from_cross_section, latitude_from_cross_section |
| 13 |
from metpy.cbook import get_test_data |
| 14 |
from metpy.interpolate import cross_section |
| 15 |
from metpy.testing import assert_array_almost_equal, assert_xarray_allclose, needs_cartopy |
| 16 |
from metpy.units import units |
| 17 |
|
| 18 |
|
| 19 |
@pytest.fixture() |
| 20 |
@needs_cartopy |
| 21 |
def test_cross_lonlat(): |
| 22 |
"""Return cross section on a lon/lat grid with no time coordinate for use in tests.""" |
| 23 |
data_u = np.linspace(-40, 40, 5 * 6 * 7).reshape((5, 6, 7)) * units.knots |
| 24 |
data_v = np.linspace(40, -40, 5 * 6 * 7).reshape((5, 6, 7)) * units.knots |
| 25 |
ds = xr.Dataset( |
| 26 |
{ |
| 27 |
'u_wind': (['isobaric', 'lat', 'lon'], data_u), |
| 28 |
'v_wind': (['isobaric', 'lat', 'lon'], data_v) |
| 29 |
}, |
| 30 |
coords={ |
| 31 |
'isobaric': xr.DataArray( |
| 32 |
np.linspace(1000, 500, 5), |
| 33 |
name='isobaric', |
| 34 |
dims=['isobaric'], |
| 35 |
attrs={'units': 'hPa'} |
| 36 |
), |
| 37 |
'lat': xr.DataArray( |
| 38 |
np.linspace(30, 45, 6), |
| 39 |
name='lat', |
| 40 |
dims=['lat'], |
| 41 |
attrs={'units': 'degrees_north'} |
| 42 |
), |
| 43 |
'lon': xr.DataArray( |
| 44 |
np.linspace(255, 275, 7), |
| 45 |
name='lon', |
| 46 |
dims=['lon'], |
| 47 |
attrs={'units': 'degrees_east'} |
| 48 |
) |
| 49 |
} |
| 50 |
) |
| 51 |
|
| 52 |
start, end = (30.5, 255.5), (44.5, 274.5) |
| 53 |
return cross_section(ds.metpy.parse_cf(), start, end, steps=7, interp_type='nearest') |
| 54 |
|
| 55 |
|
| 56 |
@pytest.fixture() |
| 57 |
@needs_cartopy |
| 58 |
def test_cross_xy(): |
| 59 |
"""Return cross section on a x/y grid with a time coordinate for use in tests.""" |
| 60 |
data_u = np.linspace(-25, 25, 5 * 6 * 7).reshape((1, 5, 6, 7)) * units('m/s') |
| 61 |
data_v = np.linspace(25, -25, 5 * 6 * 7).reshape((1, 5, 6, 7)) * units('m/s') |
| 62 |
ds = xr.Dataset( |
| 63 |
{ |
| 64 |
'u_wind': (['time', 'isobaric', 'y', 'x'], data_u), |
| 65 |
'v_wind': (['time', 'isobaric', 'y', 'x'], data_v), |
| 66 |
'lambert_conformal': ([], '') |
| 67 |
}, |
| 68 |
coords={ |
| 69 |
'time': xr.DataArray( |
| 70 |
np.array(['2018-07-01T00:00'], dtype='datetime64[ns]'), |
| 71 |
name='time', |
| 72 |
dims=['time'] |
| 73 |
), |
| 74 |
'isobaric': xr.DataArray( |
| 75 |
np.linspace(1000, 500, 5), |
| 76 |
name='isobaric', |
| 77 |
dims=['isobaric'], |
| 78 |
attrs={'units': 'hPa'} |
| 79 |
), |
| 80 |
'y': xr.DataArray( |
| 81 |
np.linspace(-1500, 0, 6), |
| 82 |
name='y', |
| 83 |
dims=['y'], |
| 84 |
attrs={'units': 'km'} |
| 85 |
), |
| 86 |
'x': xr.DataArray( |
| 87 |
np.linspace(-500, 3000, 7), |
| 88 |
name='x', |
| 89 |
dims=['x'], |
| 90 |
attrs={'units': 'km'} |
| 91 |
) |
| 92 |
} |
| 93 |
) |
| 94 |
ds['u_wind'].attrs = ds['v_wind'].attrs = { |
| 95 |
'grid_mapping': 'lambert_conformal' |
| 96 |
} |
| 97 |
ds['lambert_conformal'].attrs = { |
| 98 |
'grid_mapping_name': 'lambert_conformal_conic', |
| 99 |
'standard_parallel': 50.0, |
| 100 |
'longitude_of_central_meridian': -107.0, |
| 101 |
'latitude_of_projection_origin': 50.0, |
| 102 |
'earth_shape': 'spherical', |
| 103 |
'earth_radius': 6367470.21484375 |
| 104 |
} |
| 105 |
|
| 106 |
start, end = ((36.46, -112.45), (42.95, -68.74)) |
| 107 |
return cross_section(ds.metpy.parse_cf(), start, end, steps=7) |
| 108 |
|
| 109 |
|
| 110 |
def test_distances_from_cross_section_given_lonlat(test_cross_lonlat): |
| 111 |
"""Test distances from cross section with lat/lon grid.""" |
| 112 |
x, y = distances_from_cross_section(test_cross_lonlat['u_wind']) |
| 113 |
|
| 114 |
true_x_values = np.array([-0., 252585.3108187, 505170.6216374, 757755.93245611, |
| 115 |
1010341.24327481, 1262926.55409352, 1515511.86491222]) |
| 116 |
true_y_values = np.array([-0., 283412.80349716, 566825.60699432, 850238.41049148, |
| 117 |
1133651.21398864, 1417064.0174858, 1700476.82098296]) |
| 118 |
index = xr.DataArray(range(7), name='index', dims=['index']) |
| 119 |
true_x = xr.DataArray( |
| 120 |
true_x_values * units.meters, |
| 121 |
coords={ |
| 122 |
'metpy_crs': test_cross_lonlat['metpy_crs'], |
| 123 |
'lat': test_cross_lonlat['lat'], |
| 124 |
'lon': test_cross_lonlat['lon'], |
| 125 |
'index': index, |
| 126 |
}, |
| 127 |
dims=['index'] |
| 128 |
) |
| 129 |
true_y = xr.DataArray( |
| 130 |
true_y_values * units.meters, |
| 131 |
coords={ |
| 132 |
'metpy_crs': test_cross_lonlat['metpy_crs'], |
| 133 |
'lat': test_cross_lonlat['lat'], |
| 134 |
'lon': test_cross_lonlat['lon'], |
| 135 |
'index': index, |
| 136 |
}, |
| 137 |
dims=['index'] |
| 138 |
) |
| 139 |
assert_xarray_allclose(x, true_x) |
| 140 |
assert_xarray_allclose(y, true_y) |
| 141 |
|
| 142 |
|
| 143 |
def test_distances_from_cross_section_given_xy(test_cross_xy): |
| 144 |
"""Test distances from cross section with x/y grid.""" |
| 145 |
x, y = distances_from_cross_section(test_cross_xy['u_wind']) |
| 146 |
xr.testing.assert_identical(test_cross_xy['x'], x) |
| 147 |
xr.testing.assert_identical(test_cross_xy['y'], y) |
| 148 |
|
| 149 |
|
| 150 |
def test_distances_from_cross_section_given_bad_coords(test_cross_xy): |
| 151 |
"""Ensure an AttributeError is raised when the cross section lacks need coordinates.""" |
| 152 |
with pytest.raises(AttributeError): |
| 153 |
distances_from_cross_section(test_cross_xy['u_wind'].drop_vars('x')) |
| 154 |
|
| 155 |
|
| 156 |
def test_latitude_from_cross_section_given_lat(test_cross_lonlat): |
| 157 |
"""Test latitude from cross section with latitude given.""" |
| 158 |
latitude = latitude_from_cross_section(test_cross_lonlat['v_wind']) |
| 159 |
xr.testing.assert_identical(test_cross_lonlat['lat'], latitude) |
| 160 |
|
| 161 |
|
| 162 |
def test_latitude_from_cross_section_given_y(test_cross_xy): |
| 163 |
"""Test latitude from cross section with y given.""" |
| 164 |
latitude = latitude_from_cross_section(test_cross_xy['v_wind']) |
| 165 |
true_latitude_values = np.array([36.46, 38.64829115, 40.44833152, 41.81277354, 42.7011178, |
| 166 |
43.0845549, 42.95]) |
| 167 |
index = xr.DataArray(range(7), name='index', dims=['index']) |
| 168 |
true_latitude = xr.DataArray( |
| 169 |
true_latitude_values * units.degrees_north, |
| 170 |
coords={ |
| 171 |
'metpy_crs': test_cross_xy['metpy_crs'], |
| 172 |
'y': test_cross_xy['y'], |
| 173 |
'x': test_cross_xy['x'], |
| 174 |
'index': index, |
| 175 |
}, |
| 176 |
dims=['index'] |
| 177 |
) |
| 178 |
assert_xarray_allclose(latitude, true_latitude) |
| 179 |
|
| 180 |
|
| 181 |
def test_unit_vectors_from_cross_section_given_lonlat(test_cross_lonlat): |
| 182 |
"""Test unit vector calculation from cross section with lat/lon grid.""" |
| 183 |
unit_tangent, unit_normal = unit_vectors_from_cross_section(test_cross_lonlat['u_wind']) |
| 184 |
true_unit_tangent = np.array([[0.66533859, 0.66533859, 0.66533859, 0.66533859, 0.66533859, |
| 185 |
0.66533859, 0.66533859], |
| 186 |
[0.74654173, 0.74654173, 0.74654173, 0.74654173, 0.74654173, |
| 187 |
0.74654173, 0.74654173]]) |
| 188 |
true_unit_normal = np.array([[-0.74654173, -0.74654173, -0.74654173, -0.74654173, |
| 189 |
-0.74654173, -0.74654173, -0.74654173], |
| 190 |
[0.66533859, 0.66533859, 0.66533859, 0.66533859, |
| 191 |
0.66533859, 0.66533859, 0.66533859]]) |
| 192 |
assert_array_almost_equal(true_unit_tangent, unit_tangent, 7) |
| 193 |
assert_array_almost_equal(true_unit_normal, unit_normal, 7) |
| 194 |
|
| 195 |
|
| 196 |
def test_unit_vectors_from_cross_section_given_xy(test_cross_xy): |
| 197 |
"""Test unit vector calculation from cross section with x/y grid.""" |
| 198 |
unit_tangent, unit_normal = unit_vectors_from_cross_section(test_cross_xy['u_wind']) |
| 199 |
true_unit_tangent = np.array([[0.93567585, 0.929688, 0.92380315, 0.91844706, 0.91349795, |
| 200 |
0.90875771, 0.90400673], |
| 201 |
[0.35286074, 0.36834796, 0.3828678, 0.39554392, 0.40684333, |
| 202 |
0.41732413, 0.42751822]]) |
| 203 |
true_unit_normal = np.array([[-0.35286074, -0.36834796, -0.3828678, -0.39554392, |
| 204 |
-0.40684333, -0.41732413, -0.42751822], |
| 205 |
[0.93567585, 0.929688, 0.92380315, 0.91844706, 0.91349795, |
| 206 |
0.90875771, 0.90400673]]) |
| 207 |
assert_array_almost_equal(true_unit_tangent, unit_tangent, 7) |
| 208 |
assert_array_almost_equal(true_unit_normal, unit_normal, 7) |
| 209 |
|
| 210 |
|
| 211 |
def test_cross_section_components(test_cross_lonlat): |
| 212 |
"""Test getting cross section components of a 2D vector field.""" |
| 213 |
tang_wind, norm_wind = cross_section_components(test_cross_lonlat['u_wind'], |
| 214 |
test_cross_lonlat['v_wind']) |
| 215 |
true_tang_wind_values = np.array([[3.24812563, 2.9994653, 2.75080496, 2.50214463, |
| 216 |
2.47106208, 2.22240175, 1.97374141], |
| 217 |
[1.94265887, 1.69399854, 1.4453382, 1.19667786, |
| 218 |
1.16559532, 0.91693499, 0.66827465], |
| 219 |
[0.63719211, 0.38853177, 0.13987144, -0.1087889, |
| 220 |
-0.13987144, -0.38853177, -0.63719211], |
| 221 |
[-0.66827465, -0.91693499, -1.16559532, -1.41425566, |
| 222 |
-1.4453382, -1.69399854, -1.94265887], |
| 223 |
[-1.97374141, -2.22240175, -2.47106208, -2.71972242, |
| 224 |
-2.75080496, -2.9994653, -3.24812563]]) |
| 225 |
true_norm_wind_values = np.array([[56.47521297, 52.15175169, 47.82829041, 43.50482913, |
| 226 |
42.96439647, 38.64093519, 34.31747391], |
| 227 |
[33.77704125, 29.45357997, 25.13011869, 20.80665741, |
| 228 |
20.26622475, 15.94276347, 11.61930219], |
| 229 |
[11.07886953, 6.75540825, 2.43194697, -1.89151431, |
| 230 |
-2.43194697, -6.75540825, -11.07886953], |
| 231 |
[-11.61930219, -15.94276347, -20.26622475, -24.58968603, |
| 232 |
-25.13011869, -29.45357997, -33.77704125], |
| 233 |
[-34.31747391, -38.64093519, -42.96439647, -47.28785775, |
| 234 |
-47.82829041, -52.15175169, -56.47521297]]) |
| 235 |
true_tang_wind = xr.DataArray(true_tang_wind_values * units.knots, |
| 236 |
coords=test_cross_lonlat['u_wind'].coords, |
| 237 |
dims=test_cross_lonlat['u_wind'].dims, |
| 238 |
attrs=test_cross_lonlat['u_wind'].attrs) |
| 239 |
true_norm_wind = xr.DataArray(true_norm_wind_values * units.knots, |
| 240 |
coords=test_cross_lonlat['u_wind'].coords, |
| 241 |
dims=test_cross_lonlat['u_wind'].dims, |
| 242 |
attrs=test_cross_lonlat['u_wind'].attrs) |
| 243 |
assert_xarray_allclose(tang_wind, true_tang_wind) |
| 244 |
assert_xarray_allclose(norm_wind, true_norm_wind) |
| 245 |
|
| 246 |
|
| 247 |
def test_tangential_component(test_cross_xy): |
| 248 |
"""Test getting cross section tangential component of a 2D vector field.""" |
| 249 |
tang_wind = tangential_component(test_cross_xy['u_wind'], test_cross_xy['v_wind']) |
| 250 |
true_tang_wind_values = np.array([[[-14.56982141, -13.17102075, -11.83790134, |
| 251 |
-10.59675064, -9.42888813, -8.31533355, -7.2410326], |
| 252 |
[-8.71378435, -7.53076196, -6.40266576, -5.34269988, |
| 253 |
-4.33810002, -3.37748418, -2.45334901], |
| 254 |
[-2.85774728, -1.89050316, -0.96743019, -0.08864912, |
| 255 |
0.7526881, 1.5603652, 2.33433459], |
| 256 |
[2.99828978, 3.74975563, 4.46780539, 5.16540164, |
| 257 |
5.84347621, 6.49821458, 7.12201819], |
| 258 |
[8.85432685, 9.39001443, 9.90304096, 10.41945241, |
| 259 |
10.93426433, 11.43606396, 11.90970179]]]) |
| 260 |
true_tang_wind = xr.DataArray(true_tang_wind_values * units('m/s'), |
| 261 |
coords=test_cross_xy['u_wind'].coords, |
| 262 |
dims=test_cross_xy['u_wind'].dims, |
| 263 |
attrs=test_cross_xy['u_wind'].attrs) |
| 264 |
assert_xarray_allclose(tang_wind, true_tang_wind) |
| 265 |
|
| 266 |
|
| 267 |
def test_normal_component(test_cross_xy): |
| 268 |
"""Test getting cross section normal component of a 2D vector field.""" |
| 269 |
norm_wind = normal_component(test_cross_xy['u_wind'], test_cross_xy['v_wind']) |
| 270 |
true_norm_wind_values = np.array([[[32.21218429, 30.45650997, 28.59536112, 26.62832466, |
| 271 |
24.57166983, 22.43805311, 20.2347284], |
| 272 |
[19.26516594, 17.41404337, 15.46613157, 13.42554447, |
| 273 |
11.30508283, 9.11378585, 6.85576955], |
| 274 |
[6.3181476, 4.37157677, 2.33690202, 0.22276428, |
| 275 |
-1.96150417, -4.2104814, -6.52318931], |
| 276 |
[-6.62887075, -8.67088982, -10.79232752, -12.98001592, |
| 277 |
-15.22809117, -17.53474865, -19.90214816], |
| 278 |
[-19.5758891, -21.71335642, -23.92155707, -26.18279611, |
| 279 |
-28.49467817, -30.8590159, -33.28110701]]]) |
| 280 |
true_norm_wind = xr.DataArray(true_norm_wind_values * units('m/s'), |
| 281 |
coords=test_cross_xy['u_wind'].coords, |
| 282 |
dims=test_cross_xy['u_wind'].dims, |
| 283 |
attrs=test_cross_xy['u_wind'].attrs) |
| 284 |
assert_xarray_allclose(norm_wind, true_norm_wind) |
| 285 |
|
| 286 |
|
| 287 |
def test_absolute_momentum_given_lonlat(test_cross_lonlat): |
| 288 |
"""Test absolute momentum calculation.""" |
| 289 |
momentum = absolute_momentum(test_cross_lonlat['u_wind'], test_cross_lonlat['v_wind']) |
| 290 |
true_momentum_values = np.array([[29.05335956, 57.00676169, 88.89733786, 124.37969813, |
| 291 |
165.02883664, 206.55775948, 250.49678829], |
| 292 |
[17.37641122, 45.32981335, 77.22038952, 112.70274979, |
| 293 |
153.3518883, 194.88081114, 238.81983995], |
| 294 |
[5.69946288, 33.65286501, 65.54344118, 101.02580145, |
| 295 |
141.67493996, 183.2038628, 227.14289161], |
| 296 |
[-5.97748546, 21.97591667, 53.86649284, 89.34885311, |
| 297 |
129.99799162, 171.52691446, 215.46594327], |
| 298 |
[-17.6544338, 10.29896833, 42.1895445, 77.67190477, |
| 299 |
118.32104328, 159.84996612, 203.78899492]]) |
| 300 |
|
| 301 |
true_momentum = xr.DataArray(true_momentum_values * units('m/s'), |
| 302 |
coords=test_cross_lonlat['u_wind'].coords, |
| 303 |
dims=test_cross_lonlat['u_wind'].dims) |
| 304 |
assert_xarray_allclose(momentum, true_momentum) |
| 305 |
|
| 306 |
|
| 307 |
def test_absolute_momentum_given_xy(test_cross_xy): |
| 308 |
"""Test absolute momentum calculation.""" |
| 309 |
momentum = absolute_momentum(test_cross_xy['u_wind'], test_cross_xy['v_wind']) |
| 310 |
true_momentum_values = np.array([[[169.22222693, 146.36354006, 145.75559124, 171.8710635, |
| 311 |
215.04876817, 265.73797007, 318.34138347], |
| 312 |
[156.27520858, 133.32107346, 132.62636169, 158.66828331, |
| 313 |
201.78218117, 252.41370282, 304.96242462], |
| 314 |
[143.32819023, 120.27860686, 119.49713214, 145.46550311, |
| 315 |
188.51559418, 239.08943557, 291.58346576], |
| 316 |
[130.38117188, 107.23614026, 106.36790259, 132.26272292, |
| 317 |
175.24900718, 225.76516831, 278.20450691], |
| 318 |
[117.43415353, 94.19367366, 93.23867305, 119.05994273, |
| 319 |
161.98242018, 212.44090106, 264.82554806]]]) |
| 320 |
true_momentum = xr.DataArray(true_momentum_values * units('m/s'), |
| 321 |
coords=test_cross_xy['u_wind'].coords, |
| 322 |
dims=test_cross_xy['u_wind'].dims) |
| 323 |
assert_xarray_allclose(momentum, true_momentum) |
| 324 |
|
| 325 |
|
| 326 |
def test_absolute_momentum_xarray_units_attr(): |
| 327 |
"""Test absolute momentum when `u` and `v` are DataArrays with a `units` attribute.""" |
| 328 |
data = xr.open_dataset(get_test_data('narr_example.nc', False)) |
| 329 |
data = data.metpy.parse_cf().squeeze() |
| 330 |
|
| 331 |
start = (37.0, -105.0) |
| 332 |
end = (35.5, -65.0) |
| 333 |
cross = cross_section(data, start, end) |
| 334 |
|
| 335 |
u = cross['u_wind'][0].sel(index=slice(0, 2)) |
| 336 |
v = cross['v_wind'][0].sel(index=slice(0, 2)) |
| 337 |
|
| 338 |
momentum = absolute_momentum(u, v) |
| 339 |
true_momentum_values = np.array([137.46164031, 134.11450232, 133.85196023]) |
| 340 |
true_momentum = xr.DataArray(units.Quantity(true_momentum_values, 'm/s'), |
| 341 |
coords=u.coords) |
| 342 |
|
| 343 |
assert_xarray_allclose(momentum, true_momentum) |