forked from Unidata/MetPy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conftest.py
185 lines (158 loc) · 6.47 KB
/
conftest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# Copyright (c) 2016,2019 MetPy Developers.
# Distributed under the terms of the BSD 3-Clause License.
# SPDX-License-Identifier: BSD-3-Clause
"""Configure pytest for metpy."""
import contextlib
import importlib
import textwrap
import matplotlib.pyplot
import numpy
import pyproj
import pytest
import xarray
import metpy.calc
import metpy.units
def pytest_report_header():
"""Add dependency information to pytest output."""
lines = []
for modname in ('cartopy', 'dask', 'matplotlib', 'numpy', 'pandas', 'pint', 'pooch',
'pyproj', 'scipy', 'shapely', 'traitlets', 'xarray'):
with contextlib.suppress(ImportError):
mod = importlib.import_module(modname)
lines.append(f'{modname.title()}:{mod.__version__}')
# textwrap.wrap will split on the space in 'mod: version', so add space afterwards
lines = textwrap.wrap('Dep Versions:' + ', '.join(lines), width=80, subsequent_indent='\t')
return [line.replace(':', ': ') for line in lines]
@pytest.fixture(autouse=True)
def doctest_available_modules(doctest_namespace):
"""Make modules available automatically to doctests."""
doctest_namespace['metpy'] = metpy
doctest_namespace['metpy.calc'] = metpy.calc
doctest_namespace['np'] = numpy
doctest_namespace['plt'] = matplotlib.pyplot
doctest_namespace['units'] = metpy.units.units
@pytest.fixture()
def ccrs():
"""Provide access to the ``cartopy.crs`` module through a global fixture.
Any testing function/fixture that needs access to ``cartopy.crs`` can simply add this to
their parameter list.
"""
return pytest.importorskip('cartopy.crs')
@pytest.fixture
def cfeature():
"""Provide access to the ``cartopy.feature`` module through a global fixture.
Any testing function/fixture that needs access to ``cartopy.feature`` can simply add this
to their parameter list.
"""
return pytest.importorskip('cartopy.feature')
@pytest.fixture()
def test_da_lonlat():
"""Return a DataArray with a lon/lat grid and no time coordinate for use in tests."""
data = numpy.linspace(300, 250, 3 * 4 * 4).reshape((3, 4, 4))
ds = xarray.Dataset(
{'temperature': (['isobaric', 'lat', 'lon'], data)},
coords={
'isobaric': xarray.DataArray(
numpy.array([850., 700., 500.]),
name='isobaric',
dims=['isobaric'],
attrs={'units': 'hPa'}
),
'lat': xarray.DataArray(
numpy.linspace(30, 40, 4),
name='lat',
dims=['lat'],
attrs={'units': 'degrees_north'}
),
'lon': xarray.DataArray(
numpy.linspace(260, 270, 4),
name='lon',
dims=['lon'],
attrs={'units': 'degrees_east'}
)
}
)
ds['temperature'].attrs['units'] = 'kelvin'
return ds.metpy.parse_cf('temperature')
@pytest.fixture()
def test_da_xy():
"""Return a DataArray with a x/y grid and a time coordinate for use in tests."""
data = numpy.linspace(300, 250, 3 * 3 * 4 * 4).reshape((3, 3, 4, 4))
ds = xarray.Dataset(
{'temperature': (['time', 'isobaric', 'y', 'x'], data),
'lambert_conformal': ([], '')},
coords={
'time': xarray.DataArray(
numpy.array(['2018-07-01T00:00', '2018-07-01T06:00', '2018-07-01T12:00'],
dtype='datetime64[ns]'),
name='time',
dims=['time']
),
'isobaric': xarray.DataArray(
numpy.array([850., 700., 500.]),
name='isobaric',
dims=['isobaric'],
attrs={'units': 'hPa'}
),
'y': xarray.DataArray(
numpy.linspace(-1000, 500, 4),
name='y',
dims=['y'],
attrs={'units': 'km'}
),
'x': xarray.DataArray(
numpy.linspace(0, 1500, 4),
name='x',
dims=['x'],
attrs={'units': 'km'}
)
}
)
ds['temperature'].attrs = {
'units': 'kelvin',
'grid_mapping': 'lambert_conformal'
}
ds['lambert_conformal'].attrs = {
'grid_mapping_name': 'lambert_conformal_conic',
'standard_parallel': 50.0,
'longitude_of_central_meridian': -107.0,
'latitude_of_projection_origin': 50.0,
'earth_shape': 'spherical',
'earth_radius': 6367470.21484375
}
return ds.metpy.parse_cf('temperature')
@pytest.fixture(params=['dask', 'xarray', 'masked', 'numpy'])
def array_type(request):
"""Return an array type for testing calc functions."""
quantity = metpy.units.units.Quantity
if request.param == 'dask':
dask_array = pytest.importorskip('dask.array', reason='dask.array is not available')
marker = request.node.get_closest_marker('xfail_dask')
if marker is not None:
request.applymarker(pytest.mark.xfail(reason=marker.args[0]))
return lambda d, u, *, mask=None: quantity(dask_array.array(d), u)
elif request.param == 'xarray':
return lambda d, u, *, mask=None: xarray.DataArray(d, attrs={'units': u})
elif request.param == 'masked':
return lambda d, u, *, mask=None: quantity(numpy.ma.array(d, mask=mask), u)
elif request.param == 'numpy':
return lambda d, u, *, mask=None: quantity(numpy.array(d), u)
else:
raise ValueError(f'Unsupported array_type option {request.param}')
@pytest.fixture
def geog_data(request):
"""Create data to use for testing calculations on geographic coordinates."""
# Generate a field of u and v on a lat/lon grid
crs = pyproj.CRS(request.param)
proj = pyproj.Proj(crs)
a = numpy.arange(4)[None, :]
arr = numpy.r_[a, a, a] * metpy.units.units('m/s')
lons = numpy.array([-100, -90, -80, -70]) * metpy.units.units.degree
lats = numpy.array([45, 55, 65]) * metpy.units.units.degree
lon_arr, lat_arr = numpy.meshgrid(lons.m_as('degree'), lats.m_as('degree'))
factors = proj.get_factors(lon_arr, lat_arr)
return (crs, lons, lats, arr, arr, factors.parallel_scale, factors.meridional_scale,
metpy.calc.lat_lon_grid_deltas(lons.m, numpy.zeros_like(lons.m),
geod=crs.get_geod())[0][0],
metpy.calc.lat_lon_grid_deltas(numpy.zeros_like(lats.m), lats.m,
geod=crs.get_geod())[1][:, 0])