|
1
|
|
|
# Copyright (c) 2016,2017 MetPy Developers. |
|
2
|
|
|
# Distributed under the terms of the BSD 3-Clause License. |
|
3
|
|
|
# SPDX-License-Identifier: BSD-3-Clause |
|
4
|
|
|
"""Tests for `calc.tools` module.""" |
|
5
|
|
|
|
|
6
|
|
|
import numpy as np |
|
7
|
|
|
import numpy.ma as ma |
|
8
|
|
|
import pytest |
|
9
|
|
|
|
|
10
|
|
|
from metpy.calc import (find_intersections, get_layer, get_layer_heights, |
|
11
|
|
|
interp, interpolate_nans, log_interp, |
|
12
|
|
|
nearest_intersection_idx, pressure_to_height_std, |
|
13
|
|
|
reduce_point_density, resample_nn_1d) |
|
14
|
|
|
from metpy.calc.tools import (_get_bound_pressure_height, _greater_or_close, _less_or_close, |
|
15
|
|
|
_next_non_masked_element, delete_masked_points) |
|
16
|
|
|
from metpy.testing import assert_array_almost_equal, assert_array_equal |
|
17
|
|
|
from metpy.units import units |
|
18
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
def test_resample_nn(): |
|
21
|
|
|
"""Test 1d nearest neighbor functionality.""" |
|
22
|
|
|
a = np.arange(5.) |
|
23
|
|
|
b = np.array([2, 3.8]) |
|
24
|
|
|
truth = np.array([2, 4]) |
|
25
|
|
|
|
|
26
|
|
|
assert_array_equal(truth, resample_nn_1d(a, b)) |
|
27
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
def test_nearest_intersection_idx(): |
|
30
|
|
|
"""Test nearest index to intersection functionality.""" |
|
31
|
|
|
x = np.linspace(5, 30, 17) |
|
32
|
|
|
y1 = 3 * x**2 |
|
33
|
|
|
y2 = 100 * x - 650 |
|
34
|
|
|
truth = np.array([2, 12]) |
|
35
|
|
|
|
|
36
|
|
|
assert_array_equal(truth, nearest_intersection_idx(y1, y2)) |
|
37
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
@pytest.mark.parametrize('direction, expected', [ |
|
40
|
|
|
('all', np.array([[8.88, 24.44], [238.84, 1794.53]])), |
|
41
|
|
|
('increasing', np.array([[24.44], [1794.53]])), |
|
42
|
|
|
('decreasing', np.array([[8.88], [238.84]])) |
|
43
|
|
|
]) |
|
44
|
|
|
def test_find_intersections(direction, expected): |
|
45
|
|
|
"""Test finding the intersection of two curves functionality.""" |
|
46
|
|
|
x = np.linspace(5, 30, 17) |
|
47
|
|
|
y1 = 3 * x**2 |
|
48
|
|
|
y2 = 100 * x - 650 |
|
49
|
|
|
# Note: Truth is what we will get with this sampling, not the mathematical intersection |
|
50
|
|
|
assert_array_almost_equal(expected, find_intersections(x, y1, y2, direction=direction), 2) |
|
51
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
def test_find_intersections_no_intersections(): |
|
54
|
|
|
"""Test finding the intersection of two curves with no intersections.""" |
|
55
|
|
|
x = np.linspace(5, 30, 17) |
|
56
|
|
|
y1 = 3 * x + 0 |
|
57
|
|
|
y2 = 5 * x + 5 |
|
58
|
|
|
# Note: Truth is what we will get with this sampling, not the mathematical intersection |
|
59
|
|
|
truth = np.array([[], |
|
60
|
|
|
[]]) |
|
61
|
|
|
assert_array_equal(truth, find_intersections(x, y1, y2)) |
|
62
|
|
|
|
|
63
|
|
|
|
|
64
|
|
|
def test_find_intersections_invalid_direction(): |
|
65
|
|
|
"""Test exception if an invalid direction is given.""" |
|
66
|
|
|
x = np.linspace(5, 30, 17) |
|
67
|
|
|
y1 = 3 * x ** 2 |
|
68
|
|
|
y2 = 100 * x - 650 |
|
69
|
|
|
with pytest.raises(ValueError): |
|
70
|
|
|
find_intersections(x, y1, y2, direction='increaing') |
|
71
|
|
|
|
|
72
|
|
|
|
|
73
|
|
|
@pytest.mark.parametrize('direction, expected', [ |
|
74
|
|
|
('all', np.array([[0., 3.5, 4.33333333, 7., 9., 10., 11.5, 13.], np.zeros(8)])), |
|
75
|
|
|
('increasing', np.array([[0., 4.333, 7., 11.5], np.zeros(4)])), |
|
76
|
|
|
('decreasing', np.array([[3.5, 10.], np.zeros(2)])) |
|
77
|
|
|
]) |
|
78
|
|
|
def test_find_intersections_intersections_in_data_at_ends(direction, expected): |
|
79
|
|
|
"""Test finding intersections when intersections are in the data. |
|
80
|
|
|
|
|
81
|
|
|
Test data includes points of intersection, sequential points of intersection, intersection |
|
82
|
|
|
at the ends of the data, and intersections in increasing/decreasing direction. |
|
83
|
|
|
""" |
|
84
|
|
|
x = np.arange(14) |
|
85
|
|
|
y1 = np.array([0, 3, 2, 1, -1, 2, 2, 0, 1, 0, 0, -2, 2, 0]) |
|
86
|
|
|
y2 = np.zeros_like(y1) |
|
87
|
|
|
assert_array_almost_equal(expected, find_intersections(x, y1, y2, direction=direction), 2) |
|
88
|
|
|
|
|
89
|
|
|
|
|
90
|
|
|
def test_interpolate_nan_linear(): |
|
91
|
|
|
"""Test linear interpolation of arrays with NaNs in the y-coordinate.""" |
|
92
|
|
|
x = np.linspace(0, 20, 15) |
|
93
|
|
|
y = 5 * x + 3 |
|
94
|
|
|
nan_indexes = [1, 5, 11, 12] |
|
95
|
|
|
y_with_nan = y.copy() |
|
96
|
|
|
y_with_nan[nan_indexes] = np.nan |
|
97
|
|
|
assert_array_almost_equal(y, interpolate_nans(x, y_with_nan), 2) |
|
98
|
|
|
|
|
99
|
|
|
|
|
100
|
|
|
def test_interpolate_nan_log(): |
|
101
|
|
|
"""Test log interpolation of arrays with NaNs in the y-coordinate.""" |
|
102
|
|
|
x = np.logspace(1, 5, 15) |
|
103
|
|
|
y = 5 * np.log(x) + 3 |
|
104
|
|
|
nan_indexes = [1, 5, 11, 12] |
|
105
|
|
|
y_with_nan = y.copy() |
|
106
|
|
|
y_with_nan[nan_indexes] = np.nan |
|
107
|
|
|
assert_array_almost_equal(y, interpolate_nans(x, y_with_nan, kind='log'), 2) |
|
108
|
|
|
|
|
109
|
|
|
|
|
110
|
|
|
def test_interpolate_nan_invalid(): |
|
111
|
|
|
"""Test log interpolation with invalid parameter.""" |
|
112
|
|
|
x = np.logspace(1, 5, 15) |
|
113
|
|
|
y = 5 * np.log(x) + 3 |
|
114
|
|
|
with pytest.raises(ValueError): |
|
115
|
|
|
interpolate_nans(x, y, kind='loog') |
|
116
|
|
|
|
|
117
|
|
|
|
|
118
|
|
|
@pytest.mark.parametrize('mask, expected_idx, expected_element', [ |
|
119
|
|
|
([False, False, False, False, False], 1, 1), |
|
120
|
|
|
([False, True, True, False, False], 3, 3), |
|
121
|
|
|
([False, True, True, True, True], None, None) |
|
122
|
|
|
]) |
|
123
|
|
View Code Duplication |
def test_non_masked_elements(mask, expected_idx, expected_element): |
|
|
|
|
|
|
124
|
|
|
"""Test with a valid element.""" |
|
125
|
|
|
a = ma.masked_array(np.arange(5), mask=mask) |
|
126
|
|
|
idx, element = _next_non_masked_element(a, 1) |
|
127
|
|
|
assert idx == expected_idx |
|
128
|
|
|
assert element == expected_element |
|
129
|
|
|
|
|
130
|
|
|
|
|
131
|
|
|
@pytest.fixture |
|
132
|
|
|
def thin_point_data(): |
|
133
|
|
|
r"""Provide scattered points for testing.""" |
|
134
|
|
|
xy = np.array([[0.8793620, 0.9005706], [0.5382446, 0.8766988], [0.6361267, 0.1198620], |
|
135
|
|
|
[0.4127191, 0.0270573], [0.1486231, 0.3121822], [0.2607670, 0.4886657], |
|
136
|
|
|
[0.7132257, 0.2827587], [0.4371954, 0.5660840], [0.1318544, 0.6468250], |
|
137
|
|
|
[0.6230519, 0.0682618], [0.5069460, 0.2326285], [0.1324301, 0.5609478], |
|
138
|
|
View Code Duplication |
[0.7975495, 0.2109974], [0.7513574, 0.9870045], [0.9305814, 0.0685815], |
|
|
|
|
|
|
139
|
|
|
[0.5271641, 0.7276889], [0.8116574, 0.4795037], [0.7017868, 0.5875983], |
|
140
|
|
|
[0.5591604, 0.5579290], [0.1284860, 0.0968003], [0.2857064, 0.3862123]]) |
|
141
|
|
|
return xy |
|
142
|
|
|
|
|
143
|
|
|
|
|
144
|
|
|
@pytest.mark.parametrize('radius, truth', |
|
145
|
|
|
[(2.0, np.array([1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
|
146
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0], dtype=np.bool)), |
|
147
|
|
|
(1.0, np.array([1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
|
148
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 1, 0], dtype=np.bool)), |
|
149
|
|
|
(0.3, np.array([1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, |
|
150
|
|
|
0, 0, 0, 0, 0, 1, 0, 0, 0, 0], dtype=np.bool)), |
|
151
|
|
|
(0.1, np.array([1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, |
|
152
|
|
|
0, 1, 1, 1, 1, 1, 1, 1, 1, 1], dtype=np.bool)) |
|
153
|
|
|
]) |
|
154
|
|
|
def test_reduce_point_density(thin_point_data, radius, truth): |
|
155
|
|
|
r"""Test that reduce_point_density works.""" |
|
156
|
|
|
assert_array_equal(reduce_point_density(thin_point_data, radius=radius), truth) |
|
157
|
|
|
|
|
158
|
|
|
|
|
159
|
|
|
@pytest.mark.parametrize('radius, truth', |
|
160
|
|
|
[(2.0, np.array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
|
161
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 1], dtype=np.bool)), |
|
162
|
|
|
(0.7, np.array([1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
|
163
|
|
|
0, 0, 0, 1, 0, 0, 0, 0, 0, 1], dtype=np.bool)), |
|
164
|
|
|
(0.3, np.array([1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, |
|
165
|
|
|
0, 0, 0, 1, 0, 0, 0, 1, 0, 1], dtype=np.bool)), |
|
166
|
|
|
(0.1, np.array([1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, |
|
167
|
|
|
0, 1, 1, 1, 1, 1, 1, 1, 1, 1], dtype=np.bool)) |
|
168
|
|
|
]) |
|
169
|
|
|
def test_reduce_point_density_priority(thin_point_data, radius, truth): |
|
170
|
|
|
r"""Test that reduce_point_density works properly with priority.""" |
|
171
|
|
|
key = np.array([8, 6, 2, 8, 6, 4, 4, 8, 8, 6, 3, 4, 3, 0, 7, 4, 3, 2, 3, 3, 9]) |
|
172
|
|
|
assert_array_equal(reduce_point_density(thin_point_data, radius, key), truth) |
|
173
|
|
|
|
|
174
|
|
|
|
|
175
|
|
|
def test_reduce_point_density_1d(): |
|
176
|
|
|
r"""Test that reduce_point_density works with 1D points.""" |
|
177
|
|
|
x = np.array([1, 3, 4, 8, 9, 10]) |
|
178
|
|
|
assert_array_equal(reduce_point_density(x, 2.5), |
|
179
|
|
|
np.array([1, 0, 1, 1, 0, 0], dtype=np.bool)) |
|
180
|
|
|
|
|
181
|
|
|
|
|
182
|
|
|
def test_delete_masked_points(): |
|
183
|
|
|
"""Test deleting masked points.""" |
|
184
|
|
|
a = ma.masked_array(np.arange(5), mask=[False, True, False, False, False]) |
|
185
|
|
|
b = ma.masked_array(np.arange(5), mask=[False, False, False, True, False]) |
|
186
|
|
|
expected = np.array([0, 2, 4]) |
|
187
|
|
|
a, b = delete_masked_points(a, b) |
|
188
|
|
|
assert_array_equal(a, expected) |
|
189
|
|
|
assert_array_equal(b, expected) |
|
190
|
|
|
|
|
191
|
|
|
|
|
192
|
|
|
def test_log_interp(): |
|
193
|
|
|
"""Test interpolating with log x-scale.""" |
|
194
|
|
|
x_log = np.array([1e3, 1e4, 1e5, 1e6]) |
|
195
|
|
|
y_log = np.log(x_log) * 2 + 3 |
|
196
|
|
|
x_interp = np.array([5e3, 5e4, 5e5]) |
|
197
|
|
|
y_interp_truth = np.array([20.0343863828, 24.6395565688, 29.2447267548]) |
|
198
|
|
|
y_interp = log_interp(x_interp, x_log, y_log) |
|
199
|
|
|
assert_array_almost_equal(y_interp, y_interp_truth, 7) |
|
200
|
|
|
|
|
201
|
|
|
|
|
202
|
|
|
def test_log_interp_units(): |
|
203
|
|
|
"""Test interpolating with log x-scale with units.""" |
|
204
|
|
|
x_log = np.array([1e3, 1e4, 1e5, 1e6]) * units.hPa |
|
205
|
|
|
y_log = (np.log(x_log.m) * 2 + 3) * units.degC |
|
206
|
|
|
x_interp = np.array([5e5, 5e6, 5e7]) * units.Pa |
|
207
|
|
|
y_interp_truth = np.array([20.0343863828, 24.6395565688, 29.2447267548]) * units.degC |
|
208
|
|
|
y_interp = log_interp(x_interp, x_log, y_log) |
|
209
|
|
|
assert_array_almost_equal(y_interp, y_interp_truth, 7) |
|
210
|
|
|
|
|
211
|
|
|
|
|
212
|
|
|
@pytest.fixture |
|
213
|
|
|
def get_bounds_data(): |
|
214
|
|
|
"""Provide pressure and height data for testing layer bounds calculation.""" |
|
215
|
|
|
pressures = np.linspace(1000, 100, 10) * units.hPa |
|
216
|
|
|
heights = pressure_to_height_std(pressures) |
|
217
|
|
|
return pressures, heights |
|
218
|
|
|
|
|
219
|
|
|
|
|
220
|
|
|
@pytest.mark.parametrize('pressure, bound, hgts, interp, expected', [ |
|
221
|
|
|
(get_bounds_data()[0], 900 * units.hPa, None, True, |
|
222
|
|
|
(900 * units.hPa, 0.9880028 * units.kilometer)), |
|
223
|
|
|
(get_bounds_data()[0], 900 * units.hPa, None, False, |
|
224
|
|
|
(900 * units.hPa, 0.9880028 * units.kilometer)), |
|
225
|
|
|
(get_bounds_data()[0], 870 * units.hPa, None, True, |
|
226
|
|
|
(870 * units.hPa, 1.2665298 * units.kilometer)), |
|
227
|
|
|
(get_bounds_data()[0], 870 * units.hPa, None, False, |
|
228
|
|
|
(900 * units.hPa, 0.9880028 * units.kilometer)), |
|
229
|
|
|
(get_bounds_data()[0], 0.9880028 * units.kilometer, None, True, |
|
230
|
|
|
(900 * units.hPa, 0.9880028 * units.kilometer)), |
|
231
|
|
|
(get_bounds_data()[0], 0.9880028 * units.kilometer, None, False, |
|
232
|
|
|
(900 * units.hPa, 0.9880028 * units.kilometer)), |
|
233
|
|
|
(get_bounds_data()[0], 1.2665298 * units.kilometer, None, True, |
|
234
|
|
|
(870 * units.hPa, 1.2665298 * units.kilometer)), |
|
235
|
|
|
(get_bounds_data()[0], 1.2665298 * units.kilometer, None, False, |
|
236
|
|
|
(900 * units.hPa, 0.9880028 * units.kilometer)), |
|
237
|
|
|
(get_bounds_data()[0], 900 * units.hPa, get_bounds_data()[1], True, |
|
238
|
|
|
(900 * units.hPa, 0.9880028 * units.kilometer)), |
|
239
|
|
|
(get_bounds_data()[0], 900 * units.hPa, get_bounds_data()[1], False, |
|
240
|
|
|
(900 * units.hPa, 0.9880028 * units.kilometer)), |
|
241
|
|
|
(get_bounds_data()[0], 870 * units.hPa, get_bounds_data()[1], True, |
|
242
|
|
|
(870 * units.hPa, 1.2643214 * units.kilometer)), |
|
243
|
|
|
(get_bounds_data()[0], 870 * units.hPa, get_bounds_data()[1], False, |
|
244
|
|
|
(900 * units.hPa, 0.9880028 * units.kilometer)), |
|
245
|
|
|
(get_bounds_data()[0], 0.9880028 * units.kilometer, get_bounds_data()[1], True, |
|
246
|
|
|
(900 * units.hPa, 0.9880028 * units.kilometer)), |
|
247
|
|
|
(get_bounds_data()[0], 0.9880028 * units.kilometer, get_bounds_data()[1], False, |
|
248
|
|
|
(900 * units.hPa, 0.9880028 * units.kilometer)), |
|
249
|
|
|
(get_bounds_data()[0], 1.2665298 * units.kilometer, get_bounds_data()[1], True, |
|
250
|
|
|
(870.9869087 * units.hPa, 1.2665298 * units.kilometer)), |
|
251
|
|
|
(get_bounds_data()[0], 1.2665298 * units.kilometer, get_bounds_data()[1], False, |
|
252
|
|
|
(900 * units.hPa, 0.9880028 * units.kilometer)), |
|
253
|
|
|
(get_bounds_data()[0], 0.98800289 * units.kilometer, get_bounds_data()[1], True, |
|
254
|
|
|
(900 * units.hPa, 0.9880028 * units.kilometer)) |
|
255
|
|
|
]) |
|
256
|
|
|
def test_get_bound_pressure_height(pressure, bound, hgts, interp, expected): |
|
257
|
|
|
"""Test getting bounds in layers with various parameter combinations.""" |
|
258
|
|
|
bounds = _get_bound_pressure_height(pressure, bound, heights=hgts, interpolate=interp) |
|
259
|
|
|
assert_array_almost_equal(bounds[0], expected[0], 5) |
|
260
|
|
|
assert_array_almost_equal(bounds[1], expected[1], 5) |
|
261
|
|
|
|
|
262
|
|
|
|
|
263
|
|
|
def test_get_bound_invalid_bound_units(): |
|
264
|
|
|
"""Test that value error is raised with invalid bound units.""" |
|
265
|
|
|
p = np.arange(900, 300, -100) * units.hPa |
|
266
|
|
|
with pytest.raises(ValueError): |
|
267
|
|
|
_get_bound_pressure_height(p, 100 * units.degC) |
|
268
|
|
|
|
|
269
|
|
|
|
|
270
|
|
|
def test_get_bound_pressure_out_of_range(): |
|
271
|
|
|
"""Test when bound is out of data range in pressure.""" |
|
272
|
|
|
p = np.arange(900, 300, -100) * units.hPa |
|
273
|
|
|
with pytest.raises(ValueError): |
|
274
|
|
|
_get_bound_pressure_height(p, 100 * units.hPa) |
|
275
|
|
|
with pytest.raises(ValueError): |
|
276
|
|
|
_get_bound_pressure_height(p, 1000 * units.hPa) |
|
277
|
|
|
|
|
278
|
|
|
|
|
279
|
|
|
def test_get_bound_height_out_of_range(): |
|
280
|
|
|
"""Test when bound is out of data range in height.""" |
|
281
|
|
|
p = np.arange(900, 300, -100) * units.hPa |
|
282
|
|
|
h = np.arange(1, 7) * units.kilometer |
|
283
|
|
|
with pytest.raises(ValueError): |
|
284
|
|
|
_get_bound_pressure_height(p, 8 * units.kilometer, heights=h) |
|
285
|
|
|
with pytest.raises(ValueError): |
|
286
|
|
|
_get_bound_pressure_height(p, 100 * units.meter, heights=h) |
|
287
|
|
|
|
|
288
|
|
|
|
|
289
|
|
|
def test_get_layer_float32(): |
|
290
|
|
|
"""Test that get_layer works properly with float32 data.""" |
|
291
|
|
|
p = np.asarray([940.85083008, 923.78851318, 911.42022705, 896.07220459, |
|
292
|
|
|
876.89404297, 781.63330078], np.float32) * units('hPa') |
|
293
|
|
|
hgt = np.asarray([563.671875, 700.93817139, 806.88098145, 938.51745605, |
|
294
|
|
|
1105.25854492, 2075.04443359], dtype=np.float32) * units.meter |
|
295
|
|
|
|
|
296
|
|
|
true_p_layer = np.asarray([940.85083008, 923.78851318, 911.42022705, 896.07220459, |
|
297
|
|
|
876.89404297, 831.86472819], np.float32) * units('hPa') |
|
298
|
|
|
true_hgt_layer = np.asarray([563.671875, 700.93817139, 806.88098145, 938.51745605, |
|
299
|
|
|
1105.25854492, 1549.8079], dtype=np.float32) * units.meter |
|
300
|
|
|
|
|
301
|
|
|
p_layer, hgt_layer = get_layer(p, hgt, heights=hgt, depth=1000. * units.meter) |
|
302
|
|
|
assert_array_almost_equal(p_layer, true_p_layer, 4) |
|
303
|
|
|
assert_array_almost_equal(hgt_layer, true_hgt_layer, 4) |
|
304
|
|
|
|
|
305
|
|
|
|
|
306
|
|
|
def test_get_layer_ragged_data(): |
|
307
|
|
|
"""Tests that error is raised for unequal length pressure and data arrays.""" |
|
308
|
|
|
p = np.arange(10) * units.hPa |
|
309
|
|
|
y = np.arange(9) * units.degC |
|
310
|
|
|
with pytest.raises(ValueError): |
|
311
|
|
|
get_layer(p, y) |
|
312
|
|
|
|
|
313
|
|
|
|
|
314
|
|
|
def test_get_layer_invalid_depth_units(): |
|
315
|
|
|
"""Tests that error is raised when depth has invalid units.""" |
|
316
|
|
|
p = np.arange(10) * units.hPa |
|
317
|
|
|
y = np.arange(9) * units.degC |
|
318
|
|
|
with pytest.raises(ValueError): |
|
319
|
|
|
get_layer(p, y, depth=400 * units.degC) |
|
320
|
|
|
|
|
321
|
|
|
|
|
322
|
|
|
@pytest.fixture |
|
323
|
|
View Code Duplication |
def layer_test_data(): |
|
|
|
|
|
|
324
|
|
|
"""Provide test data for testing of layer bounds.""" |
|
325
|
|
|
pressure = np.arange(1000, 10, -100) * units.hPa |
|
326
|
|
|
temperature = np.linspace(25, -50, len(pressure)) * units.degC |
|
327
|
|
|
return pressure, temperature |
|
328
|
|
|
|
|
329
|
|
|
|
|
330
|
|
|
@pytest.mark.parametrize('pressure, variable, heights, bottom, depth, interp, expected', [ |
|
331
|
|
|
(layer_test_data()[0], layer_test_data()[1], None, None, 150 * units.hPa, True, |
|
332
|
|
|
(np.array([1000, 900, 850]) * units.hPa, |
|
333
|
|
View Code Duplication |
np.array([25.0, 16.666666, 12.62262]) * units.degC)), |
|
|
|
|
|
|
334
|
|
|
(layer_test_data()[0], layer_test_data()[1], None, None, 150 * units.hPa, False, |
|
335
|
|
|
(np.array([1000, 900]) * units.hPa, np.array([25.0, 16.666666]) * units.degC)), |
|
336
|
|
|
(layer_test_data()[0], layer_test_data()[1], None, 2 * units.km, 3 * units.km, True, |
|
337
|
|
|
(np.array([794.85264282, 700., 600., 540.01696548]) * units.hPa, |
|
338
|
|
|
np.array([7.93049516, 0., -8.33333333, -13.14758845]) * units.degC)) |
|
339
|
|
|
]) |
|
340
|
|
|
def test_get_layer(pressure, variable, heights, bottom, depth, interp, expected): |
|
341
|
|
|
"""Tests get_layer functionality.""" |
|
342
|
|
|
p_layer, y_layer = get_layer(pressure, variable, heights=heights, bottom=bottom, |
|
343
|
|
View Code Duplication |
depth=depth, interpolate=interp) |
|
|
|
|
|
|
344
|
|
|
assert_array_almost_equal(p_layer, expected[0], 5) |
|
345
|
|
|
assert_array_almost_equal(y_layer, expected[1], 5) |
|
346
|
|
|
|
|
347
|
|
|
|
|
348
|
|
|
def test_log_interp_2d(): |
|
349
|
|
|
"""Test interpolating with log x-scale in 2 dimensions.""" |
|
350
|
|
|
x_log = np.array([[1e3, 1e4, 1e5, 1e6], [1e3, 1e4, 1e5, 1e6]]) |
|
351
|
|
|
y_log = np.log(x_log) * 2 + 3 |
|
352
|
|
|
x_interp = np.array([5e3, 5e4, 5e5]) |
|
353
|
|
|
y_interp_truth = np.array([20.0343863828, 24.6395565688, 29.2447267548]) |
|
354
|
|
|
y_interp = log_interp(x_interp, x_log, y_log, axis=1) |
|
355
|
|
|
assert_array_almost_equal(y_interp[1], y_interp_truth, 7) |
|
356
|
|
|
|
|
357
|
|
|
|
|
358
|
|
|
def test_log_interp_3d(): |
|
359
|
|
|
"""Test interpolating with log x-scale 3 dimensions along second axis.""" |
|
360
|
|
|
x_log = np.ones((3, 4, 3)) * np.array([1e3, 1e4, 1e5, 1e6]).reshape(-1, 1) |
|
361
|
|
|
y_log = np.log(x_log) * 2 + 3 |
|
362
|
|
|
x_interp = np.array([5e3, 5e4, 5e5]) |
|
363
|
|
|
y_interp_truth = np.array([20.0343863828, 24.6395565688, 29.2447267548]) |
|
364
|
|
|
y_interp = log_interp(x_interp, x_log, y_log, axis=1) |
|
365
|
|
|
assert_array_almost_equal(y_interp[0, :, 0], y_interp_truth, 7) |
|
366
|
|
|
|
|
367
|
|
|
|
|
368
|
|
|
def test_log_interp_4d(): |
|
369
|
|
|
"""Test interpolating with log x-scale 4 dimensions.""" |
|
370
|
|
|
x_log = np.ones((2, 2, 3, 4)) * np.array([1e3, 1e4, 1e5, 1e6]) |
|
371
|
|
|
y_log = np.log(x_log) * 2 + 3 |
|
372
|
|
|
x_interp = np.array([5e3, 5e4, 5e5]) |
|
373
|
|
|
y_interp_truth = np.array([20.0343863828, 24.6395565688, 29.2447267548]) |
|
374
|
|
|
y_interp = log_interp(x_interp, x_log, y_log, axis=3) |
|
375
|
|
|
assert_array_almost_equal(y_interp[0, 0, 0, :], y_interp_truth, 7) |
|
376
|
|
|
|
|
377
|
|
|
|
|
378
|
|
|
def test_log_interp_2args(): |
|
379
|
|
|
"""Test interpolating with log x-scale with 2 arguments.""" |
|
380
|
|
|
x_log = np.array([1e3, 1e4, 1e5, 1e6]) |
|
381
|
|
|
y_log = np.log(x_log) * 2 + 3 |
|
382
|
|
|
y_log2 = np.log(x_log) * 2 + 3 |
|
383
|
|
|
x_interp = np.array([5e3, 5e4, 5e5]) |
|
384
|
|
|
y_interp_truth = np.array([20.0343863828, 24.6395565688, 29.2447267548]) |
|
385
|
|
|
y_interp = log_interp(x_interp, x_log, y_log, y_log2) |
|
386
|
|
|
assert_array_almost_equal(y_interp[1], y_interp_truth, 7) |
|
387
|
|
|
assert_array_almost_equal(y_interp[0], y_interp_truth, 7) |
|
388
|
|
|
|
|
389
|
|
|
|
|
390
|
|
|
def test_log_interp_set_nan_above(): |
|
391
|
|
|
"""Test interpolating with log x-scale setting out of bounds above data to nan.""" |
|
392
|
|
|
x_log = np.array([1e3, 1e4, 1e5, 1e6]) |
|
393
|
|
|
y_log = np.log(x_log) * 2 + 3 |
|
394
|
|
|
x_interp = np.array([1e7]) |
|
395
|
|
|
y_interp_truth = np.nan |
|
396
|
|
|
with pytest.warns(Warning): |
|
397
|
|
|
y_interp = log_interp(x_interp, x_log, y_log) |
|
398
|
|
|
assert_array_almost_equal(y_interp, y_interp_truth, 7) |
|
399
|
|
|
|
|
400
|
|
|
|
|
401
|
|
|
def test_log_interp_no_extrap(): |
|
402
|
|
|
"""Test interpolating with log x-scale setting out of bounds value error.""" |
|
403
|
|
|
x_log = np.array([1e3, 1e4, 1e5, 1e6]) |
|
404
|
|
|
y_log = np.log(x_log) * 2 + 3 |
|
405
|
|
|
x_interp = np.array([1e7]) |
|
406
|
|
|
with pytest.raises(ValueError): |
|
407
|
|
|
log_interp(x_interp, x_log, y_log, fill_value=None) |
|
408
|
|
|
|
|
409
|
|
|
|
|
410
|
|
|
def test_log_interp_set_nan_below(): |
|
411
|
|
|
"""Test interpolating with log x-scale setting out of bounds below data to nan.""" |
|
412
|
|
|
x_log = np.array([1e3, 1e4, 1e5, 1e6]) |
|
413
|
|
|
y_log = np.log(x_log) * 2 + 3 |
|
414
|
|
|
x_interp = 1e2 |
|
415
|
|
|
y_interp_truth = np.nan |
|
416
|
|
|
with pytest.warns(Warning): |
|
417
|
|
|
y_interp = log_interp(x_interp, x_log, y_log) |
|
418
|
|
|
assert_array_almost_equal(y_interp, y_interp_truth, 7) |
|
419
|
|
|
|
|
420
|
|
|
|
|
421
|
|
|
def test_interp_2args(): |
|
422
|
|
|
"""Test interpolation with 2 arguments.""" |
|
423
|
|
|
x = np.array([1., 2., 3., 4.]) |
|
424
|
|
|
y = x |
|
425
|
|
|
y2 = x |
|
426
|
|
|
x_interp = np.array([2.5000000, 3.5000000]) |
|
427
|
|
|
y_interp_truth = np.array([2.5000000, 3.5000000]) |
|
428
|
|
|
y_interp = interp(x_interp, x, y, y2) |
|
429
|
|
|
assert_array_almost_equal(y_interp[0], y_interp_truth, 7) |
|
430
|
|
|
assert_array_almost_equal(y_interp[1], y_interp_truth, 7) |
|
431
|
|
|
|
|
432
|
|
|
|
|
433
|
|
|
def test_interp_decrease(): |
|
434
|
|
|
"""Test interpolation with decreasing interpolation points.""" |
|
435
|
|
|
x = np.array([1., 2., 3., 4.]) |
|
436
|
|
|
y = x |
|
437
|
|
|
x_interp = np.array([3.5000000, 2.5000000]) |
|
438
|
|
|
y_interp_truth = np.array([3.5000000, 2.5000000]) |
|
439
|
|
|
y_interp = interp(x_interp, x, y) |
|
440
|
|
|
assert_array_almost_equal(y_interp, y_interp_truth, 7) |
|
441
|
|
|
|
|
442
|
|
|
|
|
443
|
|
|
def test_interp_decrease_xp(): |
|
444
|
|
|
"""Test interpolation with decreasing order.""" |
|
445
|
|
|
x = np.array([4., 3., 2., 1.]) |
|
446
|
|
|
y = x |
|
447
|
|
|
x_interp = np.array([3.5000000, 2.5000000]) |
|
448
|
|
|
y_interp_truth = np.array([3.5000000, 2.5000000]) |
|
449
|
|
|
y_interp = interp(x_interp, x, y) |
|
450
|
|
|
assert_array_almost_equal(y_interp, y_interp_truth, 7) |
|
451
|
|
|
|
|
452
|
|
|
|
|
453
|
|
|
def test_interp_end_point(): |
|
454
|
|
|
"""Test interpolation with point at data endpoints.""" |
|
455
|
|
|
x = np.array([1., 2., 3., 4.]) |
|
456
|
|
|
y = x |
|
457
|
|
|
x_interp = np.array([1.0, 4.0]) |
|
458
|
|
|
y_interp_truth = np.array([1.0, 4.0]) |
|
459
|
|
|
y_interp = interp(x_interp, x, y) |
|
460
|
|
|
assert_array_almost_equal(y_interp, y_interp_truth, 7) |
|
461
|
|
|
|
|
462
|
|
|
|
|
463
|
|
|
def test_greater_or_close(): |
|
464
|
|
|
"""Test floating point greater or close to.""" |
|
465
|
|
|
x = np.array([0.0, 1.0, 1.49999, 1.5, 1.5000, 1.7]) |
|
466
|
|
|
comparison_value = 1.5 |
|
467
|
|
|
truth = np.array([False, False, True, True, True, True]) |
|
468
|
|
|
res = _greater_or_close(x, comparison_value) |
|
469
|
|
|
assert_array_equal(res, truth) |
|
470
|
|
|
|
|
471
|
|
|
|
|
472
|
|
|
def test_less_or_close(): |
|
473
|
|
|
"""Test floating point less or close to.""" |
|
474
|
|
|
x = np.array([0.0, 1.0, 1.49999, 1.5, 1.5000, 1.7]) |
|
475
|
|
|
comparison_value = 1.5 |
|
476
|
|
|
truth = np.array([True, True, True, True, True, False]) |
|
477
|
|
|
res = _less_or_close(x, comparison_value) |
|
478
|
|
|
assert_array_equal(res, truth) |
|
479
|
|
|
|
|
480
|
|
|
|
|
481
|
|
View Code Duplication |
def test_get_layer_heights_interpolation(): |
|
|
|
|
|
|
482
|
|
|
"""Test get_layer_heights with interpolation.""" |
|
483
|
|
|
heights = np.arange(10) * units.km |
|
484
|
|
|
data = heights.m * 2 * units.degC |
|
485
|
|
|
heights, data = get_layer_heights(heights, 5000 * units.m, data, bottom=1500 * units.m) |
|
486
|
|
|
heights_true = np.array([1.5, 2, 3, 4, 5, 6, 6.5]) * units.km |
|
487
|
|
|
data_true = heights_true.m * 2 * units.degC |
|
488
|
|
|
assert_array_almost_equal(heights_true, heights, 6) |
|
489
|
|
|
assert_array_almost_equal(data_true, data, 6) |
|
490
|
|
|
|
|
491
|
|
|
|
|
492
|
|
View Code Duplication |
def test_get_layer_heights_no_interpolation(): |
|
|
|
|
|
|
493
|
|
|
"""Test get_layer_heights without interpolation.""" |
|
494
|
|
|
heights = np.arange(10) * units.km |
|
495
|
|
|
data = heights.m * 2 * units.degC |
|
496
|
|
|
heights, data = get_layer_heights(heights, 5000 * units.m, data, |
|
497
|
|
|
bottom=1500 * units.m, interpolate=False) |
|
498
|
|
|
heights_true = np.array([2, 3, 4, 5, 6]) * units.km |
|
499
|
|
|
data_true = heights_true.m * 2 * units.degC |
|
500
|
|
|
assert_array_almost_equal(heights_true, heights, 6) |
|
501
|
|
|
assert_array_almost_equal(data_true, data, 6) |
|
502
|
|
|
|
|
503
|
|
|
|
|
504
|
|
View Code Duplication |
def test_get_layer_heights_agl(): |
|
|
|
|
|
|
505
|
|
|
"""Test get_layer_heights with interpolation.""" |
|
506
|
|
|
heights = np.arange(300, 1200, 100) * units.m |
|
507
|
|
|
data = heights.m * 0.1 * units.degC |
|
508
|
|
|
heights, data = get_layer_heights(heights, 500 * units.m, data, with_agl=True) |
|
509
|
|
|
heights_true = np.array([0, 0.1, 0.2, 0.3, 0.4, 0.5]) * units.km |
|
510
|
|
|
data_true = np.array([30, 40, 50, 60, 70, 80]) * units.degC |
|
511
|
|
|
assert_array_almost_equal(heights_true, heights, 6) |
|
512
|
|
|
assert_array_almost_equal(data_true, data, 6) |
|
513
|
|
|
|
|
514
|
|
|
|
|
515
|
|
View Code Duplication |
def test_get_layer_heights_agl_bottom_no_interp(): |
|
|
|
|
|
|
516
|
|
|
"""Test get_layer_heights with no interpolation and a bottom.""" |
|
517
|
|
|
heights = np.arange(300, 1200, 100) * units.m |
|
518
|
|
|
data = heights.m * 0.1 * units.degC |
|
519
|
|
|
heights, data = get_layer_heights(heights, 500 * units.m, data, with_agl=True, |
|
520
|
|
|
interpolation=False, bottom=200 * units.m) |
|
521
|
|
|
heights_true = np.array([0.2, 0.3, 0.4, 0.5, 0.6, 0.7]) * units.km |
|
522
|
|
|
data_true = np.array([50, 60, 70, 80, 90, 100]) * units.degC |
|
523
|
|
|
assert_array_almost_equal(heights_true, heights, 6) |
|
524
|
|
|
assert_array_almost_equal(data_true, data, 6) |
|
525
|
|
|
|