Completed
Pull Request — master (#597)
by
unknown
01:04
created

test_get_layer_float32_reversed()   A

Complexity

Conditions 1

Size

Total Lines 19

Duplication

Lines 2
Ratio 10.53 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
dl 2
loc 19
rs 9.4285
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):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
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],
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
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_float32_reversed():
307
    """
308
    Test that get_layer works properly with float32 data in increasing pressure order.
309
    Issue #593
310
    """
311
    p = np.asarray([940.85083008, 923.78851318, 911.42022705, 896.07220459,
312
                    876.89404297, 781.63330078], np.float32) * units('hPa')
313
    hgt = np.asarray([563.671875, 700.93817139, 806.88098145, 938.51745605,
314
                      1105.25854492, 2075.04443359], dtype=np.float32) * units.meter
315
316
    true_p_layer = np.asarray([940.85083008, 923.78851318, 911.42022705, 896.07220459,
317
                               876.89404297, 831.86472819], np.float32) * units('hPa')
318
    true_hgt_layer = np.asarray([563.671875, 700.93817139, 806.88098145, 938.51745605,
319
                                 1105.25854492, 1549.8079], dtype=np.float32) * units.meter
320
321
    p_layer, hgt_layer = get_layer(p[::-1], hgt[::-1], heights=hgt[::-1],
322
                                   depth=1000. * units.meter)
323 View Code Duplication
    assert_array_almost_equal(p_layer, true_p_layer, 4)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
324
    assert_array_almost_equal(hgt_layer, true_hgt_layer, 4)
325
326
327
def test_get_layer_ragged_data():
328
    """Tests that error is raised for unequal length pressure and data arrays."""
329
    p = np.arange(10) * units.hPa
330
    y = np.arange(9) * units.degC
331
    with pytest.raises(ValueError):
332
        get_layer(p, y)
333 View Code Duplication
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
334
335
def test_get_layer_invalid_depth_units():
336
    """Tests that error is raised when depth has invalid units."""
337
    p = np.arange(10) * units.hPa
338
    y = np.arange(9) * units.degC
339
    with pytest.raises(ValueError):
340
        get_layer(p, y, depth=400 * units.degC)
341
342
343 View Code Duplication
@pytest.fixture
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
344
def layer_test_data():
345
    """Provide test data for testing of layer bounds."""
346
    pressure = np.arange(1000, 10, -100) * units.hPa
347
    temperature = np.linspace(25, -50, len(pressure)) * units.degC
348
    return pressure, temperature
349
350
351
@pytest.mark.parametrize('pressure, variable, heights, bottom, depth, interp, expected', [
352
    (layer_test_data()[0], layer_test_data()[1], None, None, 150 * units.hPa, True,
353
     (np.array([1000, 900, 850]) * units.hPa,
354
      np.array([25.0, 16.666666, 12.62262]) * units.degC)),
355
    (layer_test_data()[0], layer_test_data()[1], None, None, 150 * units.hPa, False,
356
     (np.array([1000, 900]) * units.hPa, np.array([25.0, 16.666666]) * units.degC)),
357
    (layer_test_data()[0], layer_test_data()[1], None, 2 * units.km, 3 * units.km, True,
358
     (np.array([794.85264282, 700., 600., 540.01696548]) * units.hPa,
359
      np.array([7.93049516, 0., -8.33333333, -13.14758845]) * units.degC))
360
])
361
def test_get_layer(pressure, variable, heights, bottom, depth, interp, expected):
362
    """Tests get_layer functionality."""
363
    p_layer, y_layer = get_layer(pressure, variable, heights=heights, bottom=bottom,
364
                                 depth=depth, interpolate=interp)
365
    assert_array_almost_equal(p_layer, expected[0], 5)
366
    assert_array_almost_equal(y_layer, expected[1], 5)
367
368
369
def test_log_interp_2d():
370
    """Test interpolating with log x-scale in 2 dimensions."""
371
    x_log = np.array([[1e3, 1e4, 1e5, 1e6], [1e3, 1e4, 1e5, 1e6]])
372
    y_log = np.log(x_log) * 2 + 3
373
    x_interp = np.array([5e3, 5e4, 5e5])
374
    y_interp_truth = np.array([20.0343863828, 24.6395565688, 29.2447267548])
375
    y_interp = log_interp(x_interp, x_log, y_log, axis=1)
376
    assert_array_almost_equal(y_interp[1], y_interp_truth, 7)
377
378
379
def test_log_interp_3d():
380
    """Test interpolating with log x-scale 3 dimensions along second axis."""
381
    x_log = np.ones((3, 4, 3)) * np.array([1e3, 1e4, 1e5, 1e6]).reshape(-1, 1)
382
    y_log = 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, axis=1)
386
    assert_array_almost_equal(y_interp[0, :, 0], y_interp_truth, 7)
387
388
389
def test_log_interp_4d():
390
    """Test interpolating with log x-scale 4 dimensions."""
391
    x_log = np.ones((2, 2, 3, 4)) * np.array([1e3, 1e4, 1e5, 1e6])
392
    y_log = np.log(x_log) * 2 + 3
393
    x_interp = np.array([5e3, 5e4, 5e5])
394
    y_interp_truth = np.array([20.0343863828, 24.6395565688, 29.2447267548])
395
    y_interp = log_interp(x_interp, x_log, y_log, axis=3)
396
    assert_array_almost_equal(y_interp[0, 0, 0, :], y_interp_truth, 7)
397
398
399
def test_log_interp_2args():
400
    """Test interpolating with log x-scale with 2 arguments."""
401
    x_log = np.array([1e3, 1e4, 1e5, 1e6])
402
    y_log = np.log(x_log) * 2 + 3
403
    y_log2 = np.log(x_log) * 2 + 3
404
    x_interp = np.array([5e3, 5e4, 5e5])
405
    y_interp_truth = np.array([20.0343863828, 24.6395565688, 29.2447267548])
406
    y_interp = log_interp(x_interp, x_log, y_log, y_log2)
407
    assert_array_almost_equal(y_interp[1], y_interp_truth, 7)
408
    assert_array_almost_equal(y_interp[0], y_interp_truth, 7)
409
410
411
def test_log_interp_set_nan_above():
412
    """Test interpolating with log x-scale setting out of bounds above data to nan."""
413
    x_log = np.array([1e3, 1e4, 1e5, 1e6])
414
    y_log = np.log(x_log) * 2 + 3
415
    x_interp = np.array([1e7])
416
    y_interp_truth = np.nan
417
    with pytest.warns(Warning):
418
        y_interp = log_interp(x_interp, x_log, y_log)
419
    assert_array_almost_equal(y_interp, y_interp_truth, 7)
420
421
422
def test_log_interp_no_extrap():
423
    """Test interpolating with log x-scale setting out of bounds value error."""
424
    x_log = np.array([1e3, 1e4, 1e5, 1e6])
425
    y_log = np.log(x_log) * 2 + 3
426
    x_interp = np.array([1e7])
427
    with pytest.raises(ValueError):
428
        log_interp(x_interp, x_log, y_log, fill_value=None)
429
430
431
def test_log_interp_set_nan_below():
432
    """Test interpolating with log x-scale setting out of bounds below data to nan."""
433
    x_log = np.array([1e3, 1e4, 1e5, 1e6])
434
    y_log = np.log(x_log) * 2 + 3
435
    x_interp = 1e2
436
    y_interp_truth = np.nan
437
    with pytest.warns(Warning):
438
        y_interp = log_interp(x_interp, x_log, y_log)
439
    assert_array_almost_equal(y_interp, y_interp_truth, 7)
440
441
442
def test_interp_2args():
443
    """Test interpolation with 2 arguments."""
444
    x = np.array([1., 2., 3., 4.])
445
    y = x
446
    y2 = x
447
    x_interp = np.array([2.5000000, 3.5000000])
448
    y_interp_truth = np.array([2.5000000, 3.5000000])
449
    y_interp = interp(x_interp, x, y, y2)
450
    assert_array_almost_equal(y_interp[0], y_interp_truth, 7)
451
    assert_array_almost_equal(y_interp[1], y_interp_truth, 7)
452
453
454
def test_interp_decrease():
455
    """Test interpolation with decreasing interpolation points."""
456
    x = np.array([1., 2., 3., 4.])
457
    y = x
458
    x_interp = np.array([3.5000000, 2.5000000])
459
    y_interp_truth = np.array([3.5000000, 2.5000000])
460
    y_interp = interp(x_interp, x, y)
461
    assert_array_almost_equal(y_interp, y_interp_truth, 7)
462
463
464
def test_interp_decrease_xp():
465
    """Test interpolation with decreasing order."""
466
    x = np.array([4., 3., 2., 1.])
467
    y = x
468
    x_interp = np.array([3.5000000, 2.5000000])
469
    y_interp_truth = np.array([3.5000000, 2.5000000])
470
    y_interp = interp(x_interp, x, y)
471
    assert_array_almost_equal(y_interp, y_interp_truth, 7)
472
473
474
def test_interp_end_point():
475
    """Test interpolation with point at data endpoints."""
476
    x = np.array([1., 2., 3., 4.])
477
    y = x
478
    x_interp = np.array([1.0, 4.0])
479
    y_interp_truth = np.array([1.0, 4.0])
480
    y_interp = interp(x_interp, x, y)
481 View Code Duplication
    assert_array_almost_equal(y_interp, y_interp_truth, 7)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
482
483
484
def test_greater_or_close():
485
    """Test floating point greater or close to."""
486
    x = np.array([0.0, 1.0, 1.49999, 1.5, 1.5000, 1.7])
487
    comparison_value = 1.5
488
    truth = np.array([False, False, True, True, True, True])
489
    res = _greater_or_close(x, comparison_value)
490
    assert_array_equal(res, truth)
491
492 View Code Duplication
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
493
def test_less_or_close():
494
    """Test floating point less or close to."""
495
    x = np.array([0.0, 1.0, 1.49999, 1.5, 1.5000, 1.7])
496
    comparison_value = 1.5
497
    truth = np.array([True, True, True, True, True, False])
498
    res = _less_or_close(x, comparison_value)
499
    assert_array_equal(res, truth)
500
501
502
def test_get_layer_heights_interpolation():
503
    """Test get_layer_heights with interpolation."""
504 View Code Duplication
    heights = np.arange(10) * units.km
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
505
    data = heights.m * 2 * units.degC
506
    heights, data = get_layer_heights(heights, 5000 * units.m, data, bottom=1500 * units.m)
507
    heights_true = np.array([1.5, 2, 3, 4, 5, 6, 6.5]) * units.km
508
    data_true = heights_true.m * 2 * units.degC
509
    assert_array_almost_equal(heights_true, heights, 6)
510
    assert_array_almost_equal(data_true, data, 6)
511
512
513
def test_get_layer_heights_no_interpolation():
514
    """Test get_layer_heights without interpolation."""
515 View Code Duplication
    heights = np.arange(10) * units.km
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
516
    data = heights.m * 2 * units.degC
517
    heights, data = get_layer_heights(heights, 5000 * units.m, data,
518
                                      bottom=1500 * units.m, interpolate=False)
519
    heights_true = np.array([2, 3, 4, 5, 6]) * units.km
520
    data_true = heights_true.m * 2 * units.degC
521
    assert_array_almost_equal(heights_true, heights, 6)
522
    assert_array_almost_equal(data_true, data, 6)
523
524
525
def test_get_layer_heights_agl():
526
    """Test get_layer_heights with interpolation."""
527
    heights = np.arange(300, 1200, 100) * units.m
528
    data = heights.m * 0.1 * units.degC
529
    heights, data = get_layer_heights(heights, 500 * units.m, data, with_agl=True)
530
    heights_true = np.array([0, 0.1, 0.2, 0.3, 0.4, 0.5]) * units.km
531
    data_true = np.array([30, 40, 50, 60, 70, 80]) * units.degC
532
    assert_array_almost_equal(heights_true, heights, 6)
533
    assert_array_almost_equal(data_true, data, 6)
534
535
536
def test_get_layer_heights_agl_bottom_no_interp():
537
    """Test get_layer_heights with no interpolation and a bottom."""
538
    heights = np.arange(300, 1200, 100) * units.m
539
    data = heights.m * 0.1 * units.degC
540
    heights, data = get_layer_heights(heights, 500 * units.m, data, with_agl=True,
541
                                      interpolation=False, bottom=200 * units.m)
542
    heights_true = np.array([0.2, 0.3, 0.4, 0.5, 0.6, 0.7]) * units.km
543
    data_true = np.array([50, 60, 70, 80, 90, 100]) * units.degC
544
    assert_array_almost_equal(heights_true, heights, 6)
545
    assert_array_almost_equal(data_true, data, 6)
546