Completed
Push — master ( 56a93d...776aa4 )
by Ryan
01:03
created

uvw_and_known_kf_nonzero_mean()   A

Complexity

Conditions 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
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 the `turbulence` module."""
5
6
import numpy as np
7
from numpy.testing import assert_almost_equal, assert_array_equal
8
import pytest
9
10
from metpy.calc.turbulence import friction_velocity, get_perturbation, kinematic_flux, tke
11
12
13
#
14
# TKE Tests
15
#
16
@pytest.fixture()
17
def uvw_and_known_tke():
18
    """Provide a set of u,v,w with a known tke value."""
19
    u = np.array([-2, -1, 0, 1, 2])
20
    v = -u
21
    w = 2 * u
22
    #  0.5 * sqrt(2 + 2 + 8)
23
    e_true = np.sqrt(12) / 2.
24
    return u, v, w, e_true
25
26
27
def test_no_tke_1d():
28
    """Test tke calculation where the expected value is 0."""
29
    observations = 5
30
    # given all the values are the same, there should not be any tke
31
    u = np.ones(observations)
32
    v = np.ones(observations)
33
    w = np.ones(observations)
34
    e_zero = 0
35
    assert_array_equal(e_zero, tke(u, v, w))
36
37
38
def test_no_tke_2d_axis_last():
39
    """Test 0 tke calculation with 2D arrays; calculation axis is last."""
40
    observations = 5
41
    instruments = 2
42
    # given all the values are the same, there should not be any tke
43
    u = np.ones((instruments, observations))
44
    v = np.ones((instruments, observations))
45
    w = np.ones((instruments, observations))
46
    e_zero = np.zeros(instruments)
47
    assert_array_equal(e_zero, tke(u, v, w, axis=-1))
48
49
50
def test_no_tke_2d_axis_first():
51
    """Test 0 tke calculation with 2D arrays; calculation axis is first."""
52
    observations = 5
53
    instruments = 2
54
    # given all the values are the same, there should not be any tke
55
    u = np.ones((observations, instruments))
56
    v = np.ones((observations, instruments))
57
    w = np.ones((observations, instruments))
58
    e_zero = np.zeros(instruments)
59
    assert_array_equal(e_zero, tke(u, v, w, axis=0))
60
61
62
def test_known_tke(uvw_and_known_tke):
63
    """Test basic behavior of tke with known values."""
64
    u, v, w, e_true = uvw_and_known_tke
65
    assert_array_equal(e_true, tke(u, v, w))
66
67
68
def test_known_tke_2d_axis_last(uvw_and_known_tke):
69
    """Test array with shape (3, 5) [pretend time axis is -1]."""
70
    u, v, w, e_true = uvw_and_known_tke
71
    u = np.array([u, u, u])
72
    v = np.array([v, v, v])
73
    w = np.array([w, w, w])
74
    e_true = e_true * np.ones(3)
75
    assert_array_equal(e_true, tke(u, v, w, axis=-1))
76
77
78
def test_known_tke_2d_axis_first(uvw_and_known_tke):
79
    """Test array with shape (5, 3) [pretend time axis is 0]."""
80
    u, v, w, e_true = uvw_and_known_tke
81
    u = np.array([u, u, u]).transpose()
82
    v = np.array([v, v, v]).transpose()
83
    w = np.array([w, w, w]).transpose()
84
    e_true = e_true * np.ones(3).transpose()
85
    assert_array_equal(e_true, tke(u, v, w, axis=0))
86
    assert_array_equal(e_true, tke(u, v, w, axis=0, perturbation=True))
87
88
89
#
90
# Perturbation tests
91
#
92
@pytest.fixture()
93
def pert_zero_mean():
94
    """Return time series with zero-mean and perturbations."""
95
    ts = np.array([-2, -1, 0, 1, 2])
96
    pert_true = ts.copy()
97
    return ts, pert_true
98
99
100
@pytest.fixture()
101
def pert_nonzero_mean():
102
    """Return time seres with non-zero-mean and perturbations."""
103
    ts = np.array([-2, 0, 2, 4, 6])
104
    # ts.mean() = 2
105
    pert_true = np.array([-4, -2, 0, 2, 4])
106
    return ts, pert_true
107
108
109
def test_no_perturbation_1d():
110
    """Test with uniform data in 1D."""
111
    observations = 5
112
    # given all the values are the same, there should not be perturbations
113
    ts = np.ones(observations)
114
    pert_zero = 0
115
    assert_array_equal(pert_zero, get_perturbation(ts))
116
117
118
def test_no_perturbation_2d_axis_last():
119
    """Test with uniform data in 2D along the last axis."""
120
    observations = 5
121
    instruments = 2
122
    # given all the values are the same, there should not be perturbations
123
    ts = np.ones((instruments, observations))
124
    pert_zero = np.zeros((instruments, observations))
125
    assert_array_equal(pert_zero, get_perturbation(ts, axis=-1))
126
127
128
def test_no_perturbation_2d_axis_first():
129
    """Test with uniform data in 2D along the first axis."""
130
    observations = 5
131
    instruments = 2
132
    # given all the values are the same, there should not be perturbations
133
    ts = np.ones((observations, instruments))
134
    pert_zero = np.zeros((observations, instruments))
135
    assert_array_equal(pert_zero, get_perturbation(ts, axis=0))
136
137
138
def test_known_perturbation_zero_mean_1d(pert_zero_mean):
139
    """Test with zero-mean data in 1D."""
140
    ts, pert_known = pert_zero_mean
141
    assert_array_equal(pert_known, get_perturbation(ts))
142
143
144
def test_known_perturbation_zero_mean_2d_axis_last(pert_zero_mean):
145
    """Test with zero-mean data in 2D along the last axis."""
146
    ts, pert_known = pert_zero_mean
147
    ts = np.array([ts, ts, ts])
148
    pert_known = np.array([pert_known, pert_known, pert_known])
149
    assert_array_equal(pert_known, get_perturbation(ts, axis=-1))
150
151
152
def test_known_perturbation_zero_mean_2d_axis_first(pert_zero_mean):
153
    """Test with zero-mean data in 2D along the first axis."""
154
    ts, pert_known = pert_zero_mean
155
    ts = np.array([ts, ts, ts]).transpose()
156
    pert_known = np.array([pert_known, pert_known, pert_known]).transpose()
157
    assert_array_equal(pert_known, get_perturbation(ts, axis=0))
158
159
160
def test_known_perturbation_nonzero_mean_1d(pert_nonzero_mean):
161
    """Test with non-zero-mean data in 1D."""
162
    ts, pert_known = pert_nonzero_mean
163
    assert_array_equal(pert_known, get_perturbation(ts))
164
165
166
def test_known_perturbation_nonzero_mean_2d_axis_last(pert_nonzero_mean):
167
    """Test with non-zero-mean data in 2D along the last axis."""
168
    ts, pert_known = pert_nonzero_mean
169
    ts = np.array([ts, ts, ts])
170
    pert_known = np.array([pert_known, pert_known, pert_known])
171
    assert_array_equal(pert_known, get_perturbation(ts, axis=-1))
172
173
174
def test_known_perturbation_nonzero_mean_2d_axis_first(pert_nonzero_mean):
175
    """Test with non-zero-mean data in 2D along the first axis."""
176
    ts, pert_known = pert_nonzero_mean
177
    ts = np.array([ts, ts, ts]).transpose()
178
    pert_known = np.array([pert_known, pert_known, pert_known]).transpose()
179
    assert_array_equal(pert_known, get_perturbation(ts, axis=0))
180
181
182
#
183
# Kinematic Flux Tests
184
#
185
@pytest.fixture()
186
def uvw_and_known_kf_zero_mean():
187
    """Return components and kinematic flux for zero-mean time series."""
188
    u = np.array([-2, -1, 0, 1, 2])
189
    v = -u
190
    w = 2 * u
191
    kf_true = {'uv': -2, 'uw': 4, 'vw': -4}
192
    return u, v, w, kf_true
193
194
195
@pytest.fixture()
196
def uvw_and_known_kf_nonzero_mean():
197
    """Return components and kinematic flux for non-zero-mean time series."""
198
    u = np.array([-2, -1, 0, 1, 5])
199
    v = -u
200
    w = 2 * u
201
    kf_true = {'uv': -5.84, 'uw': 11.68, 'vw': -11.68}
202
    return u, v, w, kf_true
203
204
205
def test_kf_1d_zero_mean(uvw_and_known_kf_zero_mean):
206
    """Test kinematic flux calculation in 1D with zero-mean time series."""
207
    u, v, w, kf_true = uvw_and_known_kf_zero_mean
208
    assert_array_equal(kinematic_flux(u, v, perturbation=False),
209
                       kf_true['uv'])
210
    assert_array_equal(kinematic_flux(u, w, perturbation=False),
211
                       kf_true['uw'])
212
    assert_array_equal(kinematic_flux(v, w, perturbation=False),
213
                       kf_true['vw'])
214
215
    # given u, v, and w have a zero mean, the kf computed with
216
    # perturbation=True and perturbation=False should be the same
217
    assert_array_equal(kinematic_flux(u, v, perturbation=False),
218
                       kinematic_flux(u, v, perturbation=True))
219
    assert_array_equal(kinematic_flux(u, w, perturbation=False),
220
                       kinematic_flux(u, w, perturbation=True))
221
    assert_array_equal(kinematic_flux(v, w, perturbation=False),
222
                       kinematic_flux(v, w, perturbation=True))
223
224
225
def test_kf_1d_nonzero_mean(uvw_and_known_kf_nonzero_mean):
226
    """Test kinematic flux calculation in 1D with non-zero-mean time series."""
227
    u, v, w, kf_true = uvw_and_known_kf_nonzero_mean
228
    assert_array_equal(kinematic_flux(u, v, perturbation=False),
229
                       kf_true['uv'])
230
    assert_array_equal(kinematic_flux(u, w, perturbation=False),
231
                       kf_true['uw'])
232
    assert_array_equal(kinematic_flux(v, w, perturbation=False),
233
                       kf_true['vw'])
234
235
236 View Code Duplication
def test_kf_2d_axis_last_zero_mean(uvw_and_known_kf_zero_mean):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
237
    """Test kinematic flux calculation in 2D with zero-mean time series along last axis."""
238
    u, v, w, kf_true = uvw_and_known_kf_zero_mean
239
    u = np.array([u, u, u])
240
    v = np.array([v, v, v])
241
    w = np.array([w, w, w])
242
    for key in kf_true.keys():
243
        tmp = kf_true[key]
244
        kf_true[key] = np.array([tmp, tmp, tmp])
245
    assert_array_equal(kinematic_flux(u, v, perturbation=False, axis=-1),
246
                       kf_true['uv'])
247
    assert_array_equal(kinematic_flux(u, w, perturbation=False, axis=-1),
248
                       kf_true['uw'])
249
    assert_array_equal(kinematic_flux(v, w, perturbation=False, axis=-1),
250
                       kf_true['vw'])
251
    # given u, v, and w have a zero mean, the kf computed with
252
    # perturbation=True and perturbation=False should be the same
253
    assert_array_equal(kinematic_flux(u, v, perturbation=False, axis=-1),
254
                       kinematic_flux(u, v, perturbation=True, axis=-1))
255
    assert_array_equal(kinematic_flux(u, w, perturbation=False, axis=-1),
256
                       kinematic_flux(u, w, perturbation=True, axis=-1))
257
    assert_array_equal(kinematic_flux(v, w, perturbation=False, axis=-1),
258
                       kinematic_flux(v, w, perturbation=True, axis=-1))
259
260
261 View Code Duplication
def test_kf_2d_axis_last_nonzero_mean(uvw_and_known_kf_nonzero_mean):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
262
    """Test kinematic flux calculation in 2D with non-zero-mean time series along last axis."""
263
    u, v, w, kf_true = uvw_and_known_kf_nonzero_mean
264
    u = np.array([u, u, u])
265
    v = np.array([v, v, v])
266
    w = np.array([w, w, w])
267
    for key in kf_true.keys():
268
        tmp = kf_true[key]
269
        kf_true[key] = np.array([tmp, tmp, tmp])
270
    assert_array_equal(kinematic_flux(u, v, perturbation=False, axis=-1),
271
                       kf_true['uv'])
272
    assert_array_equal(kinematic_flux(u, w, perturbation=False, axis=-1),
273
                       kf_true['uw'])
274
    assert_array_equal(kinematic_flux(v, w, perturbation=False, axis=-1),
275
                       kf_true['vw'])
276
277
278 View Code Duplication
def test_kf_2d_axis_first_zero_mean(uvw_and_known_kf_zero_mean):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
279
    """Test kinematic flux calculation in 2D with zero-mean time series along first axis."""
280
    u, v, w, kf_true = uvw_and_known_kf_zero_mean
281
    u = np.array([u, u, u]).transpose()
282
    v = np.array([v, v, v]).transpose()
283
    w = np.array([w, w, w]).transpose()
284
    for key in kf_true.keys():
285
        tmp = kf_true[key]
286
        kf_true[key] = np.array([tmp, tmp, tmp]).transpose()
287
    assert_array_equal(kinematic_flux(u, v, perturbation=False, axis=0),
288
                       kf_true['uv'])
289
    assert_array_equal(kinematic_flux(u, w, perturbation=False, axis=0),
290
                       kf_true['uw'])
291
    assert_array_equal(kinematic_flux(v, w, perturbation=False, axis=0),
292
                       kf_true['vw'])
293
    # given u, v, and w have a zero mean, the kf computed with
294
    # perturbation=True and perturbation=False should be the same
295
    assert_array_equal(kinematic_flux(u, v, perturbation=False, axis=0),
296
                       kinematic_flux(u, v, perturbation=True, axis=0))
297
    assert_array_equal(kinematic_flux(u, w, perturbation=False, axis=0),
298
                       kinematic_flux(u, w, perturbation=True, axis=0))
299
    assert_array_equal(kinematic_flux(v, w, perturbation=False, axis=0),
300
                       kinematic_flux(v, w, perturbation=True, axis=0))
301
302
303 View Code Duplication
def test_kf_2d_axis_first_nonzero_mean(uvw_and_known_kf_nonzero_mean):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
304
    """Test kinematic flux in 2D with non-zero-mean time series along first axis."""
305
    u, v, w, kf_true = uvw_and_known_kf_nonzero_mean
306
    u = np.array([u, u, u]).transpose()
307
    v = np.array([v, v, v]).transpose()
308
    w = np.array([w, w, w]).transpose()
309
    for key in kf_true.keys():
310
        tmp = kf_true[key]
311
        kf_true[key] = np.array([tmp, tmp, tmp]).transpose()
312
    assert_array_equal(kinematic_flux(u, v, perturbation=False, axis=0),
313
                       kf_true['uv'])
314
    assert_array_equal(kinematic_flux(u, w, perturbation=False, axis=0),
315
                       kf_true['uw'])
316
    assert_array_equal(kinematic_flux(v, w, perturbation=False, axis=0),
317
                       kf_true['vw'])
318
319
320
#
321
# Friction Velocity Tests
322
#
323
@pytest.fixture()
324
def uvw_and_known_u_star_zero_mean():
325
    """Return components and friction velocity for a zero-mean time series."""
326
    u = np.array([-2, -1, 0, 1, 2])
327
    v = -u
328
    w = 2 * u
329
    u_star_true = {'uw': 2.0, 'uwvw': 2.3784142300054421}
330
    return u, v, w, u_star_true
331
332
333
@pytest.fixture()
334
def uvw_and_known_u_star_nonzero_mean():
335
    """Return components and friction velocity for a non-zero-mean time series."""
336
    u = np.array([-2, -1, 0, 1, 5])
337
    v = -u
338
    w = 2 * u
339
    u_star_true = {'uw': 3.4176014981270124, 'uwvw': 4.0642360178166017}
340
    return u, v, w, u_star_true
341
342
343
def test_u_star_1d_zero_mean(uvw_and_known_u_star_zero_mean):
344
    """Test friction velocity in 1D with a zero-mean time series."""
345
    u, v, w, u_star_true = uvw_and_known_u_star_zero_mean
346
    assert_almost_equal(friction_velocity(u, w, perturbation=False),
347
                        u_star_true['uw'])
348
    assert_almost_equal(friction_velocity(u, w, v=v, perturbation=False),
349
                        u_star_true['uwvw'])
350
351
352
def test_u_star_1d_nonzero_mean(uvw_and_known_u_star_nonzero_mean):
353
    """Test friction velocity in 1D with a non-zero-mean time series."""
354
    u, v, w, u_star_true = uvw_and_known_u_star_nonzero_mean
355
    assert_almost_equal(friction_velocity(u, w, perturbation=False),
356
                        u_star_true['uw'])
357
    assert_almost_equal(friction_velocity(u, w, v=v, perturbation=False),
358
                        u_star_true['uwvw'])
359
360
361 View Code Duplication
def test_u_star_2d_axis_last_zero_mean(uvw_and_known_u_star_zero_mean):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
362
    """Test friction velocity in 2D with a zero-mean time series along the last axis."""
363
    u, v, w, u_star_true = uvw_and_known_u_star_zero_mean
364
    u = np.array([u, u, u])
365
    v = np.array([v, v, v])
366
    w = np.array([w, w, w])
367
    for key in u_star_true.keys():
368
        tmp = u_star_true[key]
369
        u_star_true[key] = np.array([tmp, tmp, tmp])
370
    assert_almost_equal(friction_velocity(u, w, perturbation=False,
371
                        axis=-1), u_star_true['uw'])
372
    assert_almost_equal(friction_velocity(u, w, v=v, perturbation=False,
373
                        axis=-1), u_star_true['uwvw'])
374
375
376 View Code Duplication
def test_u_star_2d_axis_last_nonzero_mean(uvw_and_known_u_star_nonzero_mean):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
377
    """Test friction velocity in 2D with a non-zero-mean time series along the last axis."""
378
    u, v, w, u_star_true = uvw_and_known_u_star_nonzero_mean
379
    u = np.array([u, u, u])
380
    v = np.array([v, v, v])
381
    w = np.array([w, w, w])
382
    for key in u_star_true.keys():
383
        tmp = u_star_true[key]
384
        u_star_true[key] = np.array([tmp, tmp, tmp])
385
    assert_almost_equal(friction_velocity(u, w, perturbation=False,
386
                        axis=-1), u_star_true['uw'])
387
    assert_almost_equal(friction_velocity(u, w, v=v, perturbation=False,
388
                        axis=-1), u_star_true['uwvw'])
389
390
391 View Code Duplication
def test_u_star_2d_axis_first_zero_mean(uvw_and_known_u_star_zero_mean):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
392
    """Test friction velocity in 2D with a zero-mean time series along the first axis."""
393
    u, v, w, u_star_true = uvw_and_known_u_star_zero_mean
394
    u = np.array([u, u, u]).transpose()
395
    v = np.array([v, v, v]).transpose()
396
    w = np.array([w, w, w]).transpose()
397
    for key in u_star_true.keys():
398
        tmp = u_star_true[key]
399
        u_star_true[key] = np.array([tmp, tmp, tmp]).transpose()
400
    assert_almost_equal(friction_velocity(u, w, perturbation=False,
401
                        axis=0), u_star_true['uw'])
402
    assert_almost_equal(friction_velocity(u, w, v=v, perturbation=False,
403
                        axis=0), u_star_true['uwvw'])
404
405
406 View Code Duplication
def test_u_star_2d_axis_first_nonzero_mean(uvw_and_known_u_star_nonzero_mean):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
407
    """Test friction velocity in 2D with a non-zero-mean time series along the first axis."""
408
    u, v, w, u_star_true = uvw_and_known_u_star_nonzero_mean
409
    u = np.array([u, u, u]).transpose()
410
    v = np.array([v, v, v]).transpose()
411
    w = np.array([w, w, w]).transpose()
412
    for key in u_star_true.keys():
413
        tmp = u_star_true[key]
414
        u_star_true[key] = np.array([tmp, tmp, tmp]).transpose()
415
    assert_almost_equal(friction_velocity(u, w, perturbation=False,
416
                        axis=0), u_star_true['uw'])
417
    assert_almost_equal(friction_velocity(u, w, v=v, perturbation=False,
418
                        axis=0), u_star_true['uwvw'])
419