Completed
Pull Request — master (#518)
by
unknown
01:00
created

test_cross_section()   B

Complexity

Conditions 1

Size

Total Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
dl 0
loc 44
rs 8.8571
1
# Copyright (c) 2008-2015 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 (extract_cross_section, find_intersections, get_layer, interp,
11
                        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
def test_interpolate_nan_linear():
74
    """Test linear interpolation of arrays with NaNs in the y-coordinate."""
75
    x = np.linspace(0, 20, 15)
76
    y = 5 * x + 3
77
    nan_indexes = [1, 5, 11, 12]
78
    y_with_nan = y.copy()
79
    y_with_nan[nan_indexes] = np.nan
80
    assert_array_almost_equal(y, interpolate_nans(x, y_with_nan), 2)
81
82
83
def test_interpolate_nan_log():
84
    """Test log interpolation of arrays with NaNs in the y-coordinate."""
85
    x = np.logspace(1, 5, 15)
86
    y = 5 * np.log(x) + 3
87
    nan_indexes = [1, 5, 11, 12]
88
    y_with_nan = y.copy()
89
    y_with_nan[nan_indexes] = np.nan
90
    assert_array_almost_equal(y, interpolate_nans(x, y_with_nan, kind='log'), 2)
91
92
93
def test_interpolate_nan_invalid():
94
    """Test log interpolation with invalid parameter."""
95
    x = np.logspace(1, 5, 15)
96
    y = 5 * np.log(x) + 3
97
    with pytest.raises(ValueError):
98
        interpolate_nans(x, y, kind='loog')
99
100
101
@pytest.mark.parametrize('mask, expected_idx, expected_element', [
102
    ([False, False, False, False, False], 1, 1),
103
    ([False, True, True, False, False], 3, 3),
104
    ([False, True, True, True, True], None, None)
105
])
106
def test_non_masked_elements(mask, expected_idx, expected_element):
107
    """Test with a valid element."""
108
    a = ma.masked_array(np.arange(5), mask=mask)
109
    idx, element = _next_non_masked_element(a, 1)
110
    assert idx == expected_idx
111
    assert element == expected_element
112
113
114
@pytest.fixture
115
def thin_point_data():
116
    r"""Provide scattered points for testing."""
117
    xy = np.array([[0.8793620, 0.9005706], [0.5382446, 0.8766988], [0.6361267, 0.1198620],
118
                   [0.4127191, 0.0270573], [0.1486231, 0.3121822], [0.2607670, 0.4886657],
119
                   [0.7132257, 0.2827587], [0.4371954, 0.5660840], [0.1318544, 0.6468250],
120
                   [0.6230519, 0.0682618], [0.5069460, 0.2326285], [0.1324301, 0.5609478],
121
                   [0.7975495, 0.2109974], [0.7513574, 0.9870045], [0.9305814, 0.0685815],
122
                   [0.5271641, 0.7276889], [0.8116574, 0.4795037], [0.7017868, 0.5875983],
123 View Code Duplication
                   [0.5591604, 0.5579290], [0.1284860, 0.0968003], [0.2857064, 0.3862123]])
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
124
    return xy
125
126
127
@pytest.mark.parametrize('radius, truth',
128
                         [(2.0, np.array([1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
129
                                          0, 0, 0, 0, 0, 0, 0, 0, 0, 0], dtype=np.bool)),
130
                          (1.0, np.array([1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
131
                                          0, 0, 0, 0, 0, 0, 0, 0, 1, 0], dtype=np.bool)),
132
                          (0.3, np.array([1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0,
133
                                          0, 0, 0, 0, 0, 1, 0, 0, 0, 0], dtype=np.bool)),
134
                          (0.1, np.array([1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1,
135
                                          0, 1, 1, 1, 1, 1, 1, 1, 1, 1], dtype=np.bool))
136
                          ])
137
def test_reduce_point_density(thin_point_data, radius, truth):
138 View Code Duplication
    r"""Test that reduce_point_density works."""
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
139
    assert_array_equal(reduce_point_density(thin_point_data, radius=radius), truth)
140
141
142
@pytest.mark.parametrize('radius, truth',
143
                         [(2.0, np.array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
144
                                          0, 0, 0, 0, 0, 0, 0, 0, 0, 1], dtype=np.bool)),
145
                          (0.7, np.array([1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
146
                                          0, 0, 0, 1, 0, 0, 0, 0, 0, 1], dtype=np.bool)),
147
                          (0.3, np.array([1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0,
148
                                          0, 0, 0, 1, 0, 0, 0, 1, 0, 1], dtype=np.bool)),
149
                          (0.1, np.array([1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1,
150
                                          0, 1, 1, 1, 1, 1, 1, 1, 1, 1], dtype=np.bool))
151
                          ])
152
def test_reduce_point_density_priority(thin_point_data, radius, truth):
153
    r"""Test that reduce_point_density works properly with priority."""
154
    key = np.array([8, 6, 2, 8, 6, 4, 4, 8, 8, 6, 3, 4, 3, 0, 7, 4, 3, 2, 3, 3, 9])
155
    assert_array_equal(reduce_point_density(thin_point_data, radius, key), truth)
156
157
158
def test_reduce_point_density_1d():
159
    r"""Test that reduce_point_density works with 1D points."""
160
    x = np.array([1, 3, 4, 8, 9, 10])
161
    assert_array_equal(reduce_point_density(x, 2.5),
162
                       np.array([1, 0, 1, 1, 0, 0], dtype=np.bool))
163
164
165
def test_delete_masked_points():
166
    """Test deleting masked points."""
167
    a = ma.masked_array(np.arange(5), mask=[False, True, False, False, False])
168
    b = ma.masked_array(np.arange(5), mask=[False, False, False, True, False])
169
    expected = np.array([0, 2, 4])
170
    a, b = delete_masked_points(a, b)
171
    assert_array_equal(a, expected)
172
    assert_array_equal(b, expected)
173
174
175
def test_log_interp():
176
    """Test interpolating with log x-scale."""
177
    x_log = np.array([1e3, 1e4, 1e5, 1e6])
178
    y_log = np.log(x_log) * 2 + 3
179
    x_interp = np.array([5e3, 5e4, 5e5])
180
    y_interp_truth = np.array([20.0343863828, 24.6395565688, 29.2447267548])
181
    y_interp = log_interp(x_interp, x_log, y_log)
182
    assert_array_almost_equal(y_interp, y_interp_truth, 7)
183
184
185
def test_log_interp_units():
186
    """Test interpolating with log x-scale with units."""
187
    x_log = np.array([1e3, 1e4, 1e5, 1e6]) * units.hPa
188
    y_log = (np.log(x_log.m) * 2 + 3) * units.degC
189
    x_interp = np.array([5e5, 5e6, 5e7]) * units.Pa
190
    y_interp_truth = np.array([20.0343863828, 24.6395565688, 29.2447267548]) * units.degC
191
    y_interp = log_interp(x_interp, x_log, y_log)
192
    assert_array_almost_equal(y_interp, y_interp_truth, 7)
193
194
195
@pytest.fixture
196
def get_bounds_data():
197
    """Provide pressure and height data for testing layer bounds calculation."""
198
    pressures = np.linspace(1000, 100, 10) * units.hPa
199
    heights = pressure_to_height_std(pressures)
200
    return pressures, heights
201
202
203
@pytest.mark.parametrize('pressure, bound, hgts, interp, expected', [
204
    (get_bounds_data()[0], 900 * units.hPa, None, True,
205
     (900 * units.hPa, 0.9880028 * units.kilometer)),
206
    (get_bounds_data()[0], 900 * units.hPa, None, False,
207
     (900 * units.hPa, 0.9880028 * units.kilometer)),
208
    (get_bounds_data()[0], 870 * units.hPa, None, True,
209
     (870 * units.hPa, 1.2665298 * units.kilometer)),
210
    (get_bounds_data()[0], 870 * units.hPa, None, False,
211
     (900 * units.hPa, 0.9880028 * units.kilometer)),
212
    (get_bounds_data()[0], 0.9880028 * units.kilometer, None, True,
213
     (900 * units.hPa, 0.9880028 * units.kilometer)),
214
    (get_bounds_data()[0], 0.9880028 * units.kilometer, None, False,
215
     (900 * units.hPa, 0.9880028 * units.kilometer)),
216
    (get_bounds_data()[0], 1.2665298 * units.kilometer, None, True,
217
     (870 * units.hPa, 1.2665298 * units.kilometer)),
218
    (get_bounds_data()[0], 1.2665298 * units.kilometer, None, False,
219
     (900 * units.hPa, 0.9880028 * units.kilometer)),
220
    (get_bounds_data()[0], 900 * units.hPa, get_bounds_data()[1], True,
221
     (900 * units.hPa, 0.9880028 * units.kilometer)),
222
    (get_bounds_data()[0], 900 * units.hPa, get_bounds_data()[1], False,
223
     (900 * units.hPa, 0.9880028 * units.kilometer)),
224
    (get_bounds_data()[0], 870 * units.hPa, get_bounds_data()[1], True,
225
     (870 * units.hPa, 1.2643214 * units.kilometer)),
226
    (get_bounds_data()[0], 870 * units.hPa, get_bounds_data()[1], False,
227
     (900 * units.hPa, 0.9880028 * units.kilometer)),
228
    (get_bounds_data()[0], 0.9880028 * units.kilometer, get_bounds_data()[1], True,
229
     (900 * units.hPa, 0.9880028 * units.kilometer)),
230
    (get_bounds_data()[0], 0.9880028 * units.kilometer, get_bounds_data()[1], False,
231
     (900 * units.hPa, 0.9880028 * units.kilometer)),
232
    (get_bounds_data()[0], 1.2665298 * units.kilometer, get_bounds_data()[1], True,
233
     (870.9869087 * units.hPa, 1.2665298 * units.kilometer)),
234
    (get_bounds_data()[0], 1.2665298 * units.kilometer, get_bounds_data()[1], False,
235
     (900 * units.hPa, 0.9880028 * units.kilometer)),
236
    (get_bounds_data()[0], 0.98800289 * units.kilometer, get_bounds_data()[1], True,
237
     (900 * units.hPa, 0.9880028 * units.kilometer))
238
])
239
def test_get_bound_pressure_height(pressure, bound, hgts, interp, expected):
240
    """Test getting bounds in layers with various parameter combinations."""
241
    bounds = _get_bound_pressure_height(pressure, bound, heights=hgts, interpolate=interp)
242
    assert_array_almost_equal(bounds[0], expected[0], 5)
243
    assert_array_almost_equal(bounds[1], expected[1], 5)
244
245
246
def test_get_bound_invalid_bound_units():
247
    """Test that value error is raised with invalid bound units."""
248
    p = np.arange(900, 300, -100) * units.hPa
249
    with pytest.raises(ValueError):
250
        _get_bound_pressure_height(p, 100 * units.degC)
251
252
253
def test_get_bound_pressure_out_of_range():
254
    """Test when bound is out of data range in pressure."""
255
    p = np.arange(900, 300, -100) * units.hPa
256
    with pytest.raises(ValueError):
257
        _get_bound_pressure_height(p, 100 * units.hPa)
258
    with pytest.raises(ValueError):
259
        _get_bound_pressure_height(p, 1000 * units.hPa)
260
261
262
def test_get_bound_height_out_of_range():
263
    """Test when bound is out of data range in height."""
264
    p = np.arange(900, 300, -100) * units.hPa
265
    h = np.arange(1, 7) * units.kilometer
266
    with pytest.raises(ValueError):
267
        _get_bound_pressure_height(p, 8 * units.kilometer, heights=h)
268
    with pytest.raises(ValueError):
269
        _get_bound_pressure_height(p, 100 * units.meter, heights=h)
270
271
272
def test_get_layer_float32():
273
    """Test that get_layer works properly with float32 data."""
274
    p = np.asarray([940.85083008, 923.78851318, 911.42022705, 896.07220459,
275
                    876.89404297, 781.63330078], np.float32) * units('hPa')
276
    hgt = np.asarray([563.671875, 700.93817139, 806.88098145, 938.51745605,
277
                      1105.25854492, 2075.04443359], dtype=np.float32) * units.meter
278
279
    true_p_layer = np.asarray([940.85083008, 923.78851318, 911.42022705, 896.07220459,
280
                               876.89404297, 831.86472819], np.float32) * units('hPa')
281
    true_hgt_layer = np.asarray([563.671875, 700.93817139, 806.88098145, 938.51745605,
282
                                 1105.25854492, 1549.8079], dtype=np.float32) * units.meter
283
284
    p_layer, hgt_layer = get_layer(p, hgt, heights=hgt, depth=1000. * units.meter)
285
    assert_array_almost_equal(p_layer, true_p_layer, 4)
286
    assert_array_almost_equal(hgt_layer, true_hgt_layer, 4)
287
288
289
def test_get_layer_ragged_data():
290
    """Tests that error is raised for unequal length pressure and data arrays."""
291
    p = np.arange(10) * units.hPa
292
    y = np.arange(9) * units.degC
293
    with pytest.raises(ValueError):
294
        get_layer(p, y)
295
296
297
def test_get_layer_invalid_depth_units():
298
    """Tests that error is raised when depth has invalid units."""
299
    p = np.arange(10) * units.hPa
300
    y = np.arange(9) * units.degC
301
    with pytest.raises(ValueError):
302
        get_layer(p, y, depth=400 * units.degC)
303
304
305
@pytest.fixture
306
def layer_test_data():
307
    """Provide test data for testing of layer bounds."""
308
    pressure = np.arange(1000, 10, -100) * units.hPa
309
    temperature = np.linspace(25, -50, len(pressure)) * units.degC
310
    return pressure, temperature
311
312
313
@pytest.mark.parametrize('pressure, variable, heights, bottom, depth, interp, expected', [
314
    (layer_test_data()[0], layer_test_data()[1], None, None, 150 * units.hPa, True,
315
     (np.array([1000, 900, 850]) * units.hPa,
316
      np.array([25.0, 16.666666, 12.62262]) * units.degC)),
317
    (layer_test_data()[0], layer_test_data()[1], None, None, 150 * units.hPa, False,
318
     (np.array([1000, 900]) * units.hPa, np.array([25.0, 16.666666]) * units.degC)),
319
    (layer_test_data()[0], layer_test_data()[1], None, 2 * units.km, 3 * units.km, True,
320
     (np.array([794.85264282, 700., 600., 540.01696548]) * units.hPa,
321
      np.array([7.93049516, 0., -8.33333333, -13.14758845]) * units.degC))
322
])
323 View Code Duplication
def test_get_layer(pressure, variable, heights, bottom, depth, interp, expected):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
324
    """Tests get_layer functionality."""
325
    p_layer, y_layer = get_layer(pressure, variable, heights=heights, bottom=bottom,
326
                                 depth=depth, interpolate=interp)
327
    assert_array_almost_equal(p_layer, expected[0], 5)
328
    assert_array_almost_equal(y_layer, expected[1], 5)
329
330
331
def test_log_interp_2d():
332
    """Test interpolating with log x-scale in 2 dimensions."""
333 View Code Duplication
    x_log = np.array([[1e3, 1e4, 1e5, 1e6], [1e3, 1e4, 1e5, 1e6]])
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
334
    y_log = np.log(x_log) * 2 + 3
335
    x_interp = np.array([5e3, 5e4, 5e5])
336
    y_interp_truth = np.array([20.0343863828, 24.6395565688, 29.2447267548])
337
    y_interp = log_interp(x_interp, x_log, y_log, axis=1)
338
    assert_array_almost_equal(y_interp[1], y_interp_truth, 7)
339
340
341
def test_log_interp_3d():
342
    """Test interpolating with log x-scale 3 dimensions along second axis."""
343 View Code Duplication
    x_log = np.ones((3, 4, 3)) * np.array([1e3, 1e4, 1e5, 1e6]).reshape(-1, 1)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
344
    y_log = np.log(x_log) * 2 + 3
345
    x_interp = np.array([5e3, 5e4, 5e5])
346
    y_interp_truth = np.array([20.0343863828, 24.6395565688, 29.2447267548])
347
    y_interp = log_interp(x_interp, x_log, y_log, axis=1)
348
    assert_array_almost_equal(y_interp[0, :, 0], y_interp_truth, 7)
349
350
351
def test_log_interp_4d():
352
    """Test interpolating with log x-scale 4 dimensions."""
353
    x_log = np.ones((2, 2, 3, 4)) * np.array([1e3, 1e4, 1e5, 1e6])
354
    y_log = np.log(x_log) * 2 + 3
355
    x_interp = np.array([5e3, 5e4, 5e5])
356
    y_interp_truth = np.array([20.0343863828, 24.6395565688, 29.2447267548])
357
    y_interp = log_interp(x_interp, x_log, y_log, axis=3)
358
    assert_array_almost_equal(y_interp[0, 0, 0, :], y_interp_truth, 7)
359
360
361
def test_log_interp_2args():
362
    """Test interpolating with log x-scale with 2 arguments."""
363
    x_log = np.array([1e3, 1e4, 1e5, 1e6])
364
    y_log = np.log(x_log) * 2 + 3
365
    y_log2 = np.log(x_log) * 2 + 3
366
    x_interp = np.array([5e3, 5e4, 5e5])
367
    y_interp_truth = np.array([20.0343863828, 24.6395565688, 29.2447267548])
368
    y_interp = log_interp(x_interp, x_log, y_log, y_log2)
369
    assert_array_almost_equal(y_interp[1], y_interp_truth, 7)
370
    assert_array_almost_equal(y_interp[0], y_interp_truth, 7)
371
372
373
def test_log_interp_set_nan_above():
374
    """Test interpolating with log x-scale setting out of bounds above data to nan."""
375
    x_log = np.array([1e3, 1e4, 1e5, 1e6])
376
    y_log = np.log(x_log) * 2 + 3
377
    x_interp = np.array([1e7])
378
    y_interp_truth = np.nan
379
    with pytest.warns(Warning):
380
        y_interp = log_interp(x_interp, x_log, y_log)
381
    assert_array_almost_equal(y_interp, y_interp_truth, 7)
382
383
384
def test_log_interp_no_extrap():
385
    """Test interpolating with log x-scale setting out of bounds value error."""
386
    x_log = np.array([1e3, 1e4, 1e5, 1e6])
387
    y_log = np.log(x_log) * 2 + 3
388
    x_interp = np.array([1e7])
389
    with pytest.raises(ValueError):
390
        log_interp(x_interp, x_log, y_log, fill_value=None)
391
392
393
def test_log_interp_set_nan_below():
394
    """Test interpolating with log x-scale setting out of bounds below data to nan."""
395
    x_log = np.array([1e3, 1e4, 1e5, 1e6])
396
    y_log = np.log(x_log) * 2 + 3
397
    x_interp = 1e2
398
    y_interp_truth = np.nan
399
    with pytest.warns(Warning):
400
        y_interp = log_interp(x_interp, x_log, y_log)
401
    assert_array_almost_equal(y_interp, y_interp_truth, 7)
402
403
404
def test_interp_2args():
405
    """Test interpolation with 2 arguments."""
406
    x = np.array([1., 2., 3., 4.])
407
    y = x
408
    y2 = x
409
    x_interp = np.array([2.5000000, 3.5000000])
410
    y_interp_truth = np.array([2.5000000, 3.5000000])
411
    y_interp = interp(x_interp, x, y, y2)
412
    assert_array_almost_equal(y_interp[0], y_interp_truth, 7)
413
    assert_array_almost_equal(y_interp[1], y_interp_truth, 7)
414
415
416
def test_interp_decrease():
417
    """Test interpolation with decreasing interpolation points."""
418
    x = np.array([1., 2., 3., 4.])
419
    y = x
420
    x_interp = np.array([3.5000000, 2.5000000])
421
    y_interp_truth = np.array([3.5000000, 2.5000000])
422
    y_interp = interp(x_interp, x, y)
423
    assert_array_almost_equal(y_interp, y_interp_truth, 7)
424
425
426
def test_interp_decrease_xp():
427
    """Test interpolation with decreasing order."""
428
    x = np.array([4., 3., 2., 1.])
429
    y = x
430
    x_interp = np.array([3.5000000, 2.5000000])
431
    y_interp_truth = np.array([3.5000000, 2.5000000])
432
    y_interp = interp(x_interp, x, y)
433
    assert_array_almost_equal(y_interp, y_interp_truth, 7)
434
435
436
def test_interp_end_point():
437
    """Test interpolation with point at data endpoints."""
438
    x = np.array([1., 2., 3., 4.])
439
    y = x
440
    x_interp = np.array([1.0, 4.0])
441
    y_interp_truth = np.array([1.0, 4.0])
442
    y_interp = interp(x_interp, x, y)
443
    assert_array_almost_equal(y_interp, y_interp_truth, 7)
444
445
446
def test_greater_or_close():
447
    """Test floating point greater or close to."""
448
    x = np.array([0.0, 1.0, 1.49999, 1.5, 1.5000, 1.7])
449
    comparison_value = 1.5
450
    truth = np.array([False, False, True, True, True, True])
451
    res = _greater_or_close(x, comparison_value)
452
    assert_array_equal(res, truth)
453
454
455
def test_less_or_close():
456
    """Test floating point less or close to."""
457
    x = np.array([0.0, 1.0, 1.49999, 1.5, 1.5000, 1.7])
458
    comparison_value = 1.5
459
    truth = np.array([True, True, True, True, True, False])
460
    res = _less_or_close(x, comparison_value)
461
    assert_array_equal(res, truth)
462
463
464
def test_cross_section():
465
    """Test extracting cross section from model data."""
466
    lat = np.array([85., 84.5, 84., 83.5, 83.])
467
    lon = np.array([215., 215.5, 216., 216.5, 217.])
468
    tmp = np.array([[[241.5, 241.6000061, 241.6000061, 241.6000061, 241.6000061],
469
                     [240.80000305, 240.80000305, 240.80000305, 240.80000305, 240.8999939],
470
                     [240.1000061, 240.1000061, 240.19999695, 240.19999695, 240.19999695],
471
                     [239.3999939, 239.5, 239.5, 239.5, 239.6000061],
472
                     [238.8999939, 238.8999939, 239., 239., 239.]],
473
                    [[244., 244.1000061, 244.1000061, 244.1000061, 244.19999695],
474
                     [243.5, 243.5, 243.5, 243.6000061, 243.6000061],
475
                     [242.8999939, 243., 243., 243.1000061, 243.1000061],
476
                     [242.5, 242.6000061, 242.6000061, 242.6000061, 242.69999695],
477
                     [242.30000305, 242.3999939, 242.3999939, 242.3999939, 242.5]],
478
                    [[246.19999695, 246.19999695, 246.19999695, 246.19999695, 246.30000305],
479
                     [245.69999695, 245.80000305, 245.80000305, 245.80000305, 245.8999939],
480
                     [245.30000305, 245.30000305, 245.3999939, 245.3999939, 245.5],
481
                     [244.80000305, 244.8999939, 244.8999939, 245., 245.],
482
                     [244.6000061, 244.6000061, 244.69999695, 244.80000305, 244.80000305]],
483
                    [[247.80000305, 247.80000305, 247.80000305, 247.8999939, 247.8999939],
484
                     [247.3999939, 247.3999939, 247.5, 247.5, 247.6000061],
485
                     [246.8999939, 246.8999939, 246.8999939, 247., 247.],
486
                     [246.30000305, 246.30000305, 246.3999939, 246.3999939, 246.5],
487
                     [245.80000305, 245.80000305, 245.8999939, 245.8999939, 246.]],
488
                    [[248.80000305, 248.8999939, 248.8999939, 248.8999939, 249.],
489
                     [248.30000305, 248.3999939, 248.3999939, 248.5, 248.5],
490
                     [247.80000305, 247.80000305, 247.80000305, 247.8999939, 247.8999939],
491
                     [247., 247.1000061, 247.1000061, 247.1000061, 247.19999695],
492
                     [246.5, 246.5, 246.5, 246.6000061, 246.6000061]]])
493
    cross_section = extract_cross_section([1, 1], [3, 3], lat, lon, tmp, num=5)
494
    cross_section_truth = np.array([[240.80000305, 240.19999695, 240.19999695, 240.19999695,
495
                                     239.5],
496
                                    [243.5, 243., 243., 243, 242.6000061],
497
                                    [245.80000305, 245.3999939, 245.3999939, 245.3999939,
498
                                     245.],
499
                                    [247.3999939, 246.8999939, 246.8999939, 246.8999939,
500
                                     246.3999939],
501
                                    [248.3999939, 247.80000305, 247.80000305, 247.80000305,
502
                                     247.1000061]])
503
    assert_array_almost_equal(cross_section[2], cross_section_truth, 4)
504
    lat_truth = np.array([84.5, 84., 84., 84., 83.5])
505
    assert_array_almost_equal(cross_section[0], lat_truth, 4)
506
    lon_truth = np.array([215.5, 216., 216., 216., 216.5])
507
    assert_array_almost_equal(cross_section[1], lon_truth, 4)
508
509
510
def test_cross_section_non_3d():
511
    """Tests that error is given if x and y are not 1 or 2 dimensions."""
512
    lat = np.array([85., 84.5, 84., 83.5, 83.])
513
    lon = np.ones((3, 2, 4))
514
    tmp = np.ones(5)
515
    with pytest.raises(ValueError):
516
        extract_cross_section([1, 1], [3, 3], lat, lon, tmp)
517