|
1
|
|
|
"""Unit tests for the utility functions.""" |
|
2
|
|
|
import datetime as dt |
|
3
|
|
|
import numpy as np |
|
4
|
|
|
import pytest |
|
5
|
|
|
|
|
6
|
|
|
from aacgmv2 import utils |
|
7
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
class TestUtilsAACGMV2(object): |
|
10
|
|
|
"""Unit tests for the utility functions.""" |
|
11
|
|
|
|
|
12
|
|
|
def setup_method(self): |
|
13
|
|
|
"""Set up every method to create a clean testing setup.""" |
|
14
|
|
|
self.rtol = 1.0e-4 |
|
15
|
|
|
self.out = None |
|
16
|
|
|
|
|
17
|
|
|
def teardown_method(self): |
|
18
|
|
|
"""Run after every method to clean up previous testing.""" |
|
19
|
|
|
del self.rtol, self.out |
|
20
|
|
|
|
|
21
|
|
|
@pytest.mark.parametrize('year,ref', [(1880, [-179.1494, -23.0801]), |
|
22
|
|
|
(2015, [-179.2004, -23.0431])]) |
|
23
|
|
|
def test_subsol(self, year, ref): |
|
24
|
|
|
"""Test the subsolar calculation. |
|
25
|
|
|
|
|
26
|
|
|
Parameters |
|
27
|
|
|
---------- |
|
28
|
|
|
year : int |
|
29
|
|
|
Input year |
|
30
|
|
|
ref : list |
|
31
|
|
|
Expected output |
|
32
|
|
|
|
|
33
|
|
|
""" |
|
34
|
|
|
self.out = utils.subsol(year, 1, 0.0) |
|
35
|
|
|
np.testing.assert_allclose(self.out, ref, rtol=self.rtol) |
|
36
|
|
|
|
|
37
|
|
|
@pytest.mark.parametrize('year', [(1500), (2110)]) |
|
38
|
|
|
def test_subsol_raises_time_range(self, year): |
|
39
|
|
|
"""Test the routine failure for out-of-range dates. |
|
40
|
|
|
|
|
41
|
|
|
Parameters |
|
42
|
|
|
---------- |
|
43
|
|
|
year : int |
|
44
|
|
|
Input year |
|
45
|
|
|
|
|
46
|
|
|
""" |
|
47
|
|
|
with pytest.raises(ValueError, match="subsol valid between 1601-2100"): |
|
48
|
|
|
self.out = utils.subsol(year, 1, 0.0) |
|
49
|
|
|
|
|
50
|
|
|
@pytest.mark.parametrize('year,ref', |
|
51
|
|
|
[(1500, [0.167107, -0.397251, 0.902367]), |
|
52
|
|
|
(2015, [0.050281, -0.16057, 0.98574]), |
|
53
|
|
|
(2110, [0.019718, -0.095652, 0.99522])]) |
|
54
|
|
|
def test_igrf_dipole_axis(self, year, ref): |
|
55
|
|
|
"""Test the IGRF dipole axis calculation. |
|
56
|
|
|
|
|
57
|
|
|
Parameters |
|
58
|
|
|
---------- |
|
59
|
|
|
year : int |
|
60
|
|
|
Input year |
|
61
|
|
|
ref : list |
|
62
|
|
|
Expected output |
|
63
|
|
|
|
|
64
|
|
|
""" |
|
65
|
|
|
self.out = utils.igrf_dipole_axis(dt.datetime(year, 1, 1)) |
|
66
|
|
|
|
|
67
|
|
|
np.testing.assert_allclose(self.out, ref, rtol=self.rtol) |
|
68
|
|
|
|
|
69
|
|
|
@pytest.mark.parametrize('gc_lat,gd_lat,mult', |
|
70
|
|
|
[(45.0, 45.1924, False), |
|
71
|
|
|
([45.0, -45.0], [45.1924, -45.1924], True), |
|
72
|
|
|
(np.array([45.0, -45.0]), |
|
73
|
|
|
np.array([45.1924, -45.1924]), True)]) |
|
74
|
|
|
def test_gc2gd_lat(self, gc_lat, gd_lat, mult): |
|
75
|
|
|
"""Test the geocentric to geodetic conversion. |
|
76
|
|
|
|
|
77
|
|
|
Parameters |
|
78
|
|
|
---------- |
|
79
|
|
|
gc_lat : float, list, array |
|
80
|
|
|
Geocentric latitude |
|
81
|
|
|
gd_lat : float, list, array |
|
82
|
|
|
Geodetic latitude |
|
83
|
|
|
mult : bool |
|
84
|
|
|
Specify whether or not the input/output is a float |
|
85
|
|
|
|
|
86
|
|
|
""" |
|
87
|
|
|
self.out = utils.gc2gd_lat(gc_lat) |
|
88
|
|
|
|
|
89
|
|
|
if mult: |
|
90
|
|
|
np.testing.assert_allclose(self.out, gd_lat, rtol=self.rtol) |
|
91
|
|
|
else: |
|
92
|
|
|
np.testing.assert_almost_equal(self.out, gd_lat, decimal=4) |
|
93
|
|
|
|