Completed
Pull Request — master (#597)
by
unknown
58s
created

test_get_layer_float32_reversed()   A

Complexity

Conditions 1

Size

Total Lines 20

Duplication

Lines 3
Ratio 15 %

Importance

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