|
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
|
|
|
|
|
8
|
|
|
from metpy.calc import (find_intersections, interpolate_nans, nearest_intersection_idx, |
|
9
|
|
|
resample_nn_1d) |
|
10
|
|
|
from metpy.testing import assert_array_almost_equal, assert_array_equal |
|
11
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
def test_resample_nn(): |
|
14
|
|
|
"""Test 1d nearest neighbor functionality.""" |
|
15
|
|
|
a = np.arange(5.) |
|
16
|
|
|
b = np.array([2, 3.8]) |
|
17
|
|
|
truth = np.array([2, 4]) |
|
18
|
|
|
|
|
19
|
|
|
assert_array_equal(truth, resample_nn_1d(a, b)) |
|
20
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
def test_nearest_intersection_idx(): |
|
23
|
|
|
"""Test nearest index to intersection functionality.""" |
|
24
|
|
|
x = np.linspace(5, 30, 17) |
|
25
|
|
|
y1 = 3 * x**2 |
|
26
|
|
|
y2 = 100 * x - 650 |
|
27
|
|
|
truth = np.array([2, 12]) |
|
28
|
|
|
|
|
29
|
|
|
assert_array_equal(truth, nearest_intersection_idx(y1, y2)) |
|
30
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
def test_find_intersections(): |
|
33
|
|
|
"""Test finding the intersection of two curves functionality.""" |
|
34
|
|
|
x = np.linspace(5, 30, 17) |
|
35
|
|
|
y1 = 3 * x**2 |
|
36
|
|
|
y2 = 100 * x - 650 |
|
37
|
|
|
# Truth is what we will get with this sampling, |
|
38
|
|
|
# not the mathematical intersection |
|
39
|
|
|
truth = np.array([[8.88, 24.44], |
|
40
|
|
|
[238.84, 1794.53]]) |
|
41
|
|
|
|
|
42
|
|
|
assert_array_almost_equal(truth, find_intersections(x, y1, y2), 2) |
|
43
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
def test_interpolate_nan_linear(): |
|
46
|
|
|
"""Test linear interpolation of arrays with NaNs in the y-coordinate.""" |
|
47
|
|
|
x = np.linspace(0, 20, 15) |
|
48
|
|
|
y = 5 * x + 3 |
|
49
|
|
|
nan_indexes = [1, 5, 11, 12] |
|
50
|
|
|
y_with_nan = y.copy() |
|
51
|
|
|
y_with_nan[nan_indexes] = np.nan |
|
52
|
|
|
assert_array_almost_equal(y, interpolate_nans(x, y_with_nan), 2) |
|
53
|
|
|
|
|
54
|
|
|
|
|
55
|
|
|
def test_interpolate_nan_log(): |
|
56
|
|
|
"""Test log interpolation of arrays with NaNs in the y-coordinate.""" |
|
57
|
|
|
x = np.logspace(1, 5, 15) |
|
58
|
|
|
y = 5 * np.log(x) + 3 |
|
59
|
|
|
nan_indexes = [1, 5, 11, 12] |
|
60
|
|
|
y_with_nan = y.copy() |
|
61
|
|
|
y_with_nan[nan_indexes] = np.nan |
|
62
|
|
|
assert_array_almost_equal(y, interpolate_nans(x, y_with_nan, kind='log'), 2) |
|
63
|
|
|
|