Passed
Pull Request — main (#103)
by Angeline
01:40
created

test_fortranapex.TestFortranApex.setup_method()   A

Complexity

Conditions 1

Size

Total Lines 33
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 28
nop 1
dl 0
loc 33
rs 9.208
c 0
b 0
f 0
1
# -*- coding: utf-8 -*-
2
"""Test the apexpy.fortranapex class
3
4
Notes
5
-----
6
Whenever function outputs are tested against hard-coded numbers, the test
7
results (numbers) were obtained by running the code that is tested.  Therefore,
8
these tests below only check that nothing changes when refactoring, etc., and
9
not if the results are actually correct.
10
11
These results are expected to change when IGRF is updated.
12
13
"""
14
15
from numpy.testing import assert_allclose
16
import os
17
import pytest
18
19
import apexpy
20
from apexpy import fortranapex as fa
21
22
23
class TestFortranApex():
24
    def setup_method(self):
25
        """Initialize each test."""
26
        fa.loadapxsh(os.path.join(os.path.dirname(apexpy.__file__),
27
                                  'apexsh.dat'), 2000)
28
29
        # Set the inputs
30
        self.lat = 60
31
        self.lon = 15
32
        self.height = 100
33
        self.refh = 300
34
        self.vecflg = 1
35
        self.precision = 1e-10
36
37
        # Set the output values
38
        self.glat = 50.979549407958984
39
        self.glon = -66.16891479492188
40
        self.error = 2.8316469524725107e-06
41
        self.qlat = 56.531288146972656
42
        self.qlon = 94.1068344116211
43
        self.mlat = 55.94841766357422
44
        self.mlon = 94.1068344116211
45
        self.f1 = [1.079783, 0.10027137]
46
        self.f2 = [-0.24546318, 0.90718889]
47
        self.fmag = 1.0041800737380981
48
        self.d1 = [0.9495701, 0.25693053, 0.09049474]
49
        self.d2 = [0.10011087, -1.0780545, -0.33892432]
50
        self.d3 = [0.00865356, 0.27327004, -0.8666646]
51
        self.dmag = 1.100391149520874
52
        self.e1 = [1.0269295, 0.08382964, 0.03668632]
53
        self.e2 = [0.24740215, -0.82374191, -0.25726584]
54
        self.e3 = [0.01047826, 0.33089194, -1.04941]
55
        self.out = None
56
        self.test_out = None
57
58
    def teardown_method(self):
59
        """Clean environment after each test."""
60
        del self.lat, self.lon, self.height, self.refh, self.vecflg
61
        del self.qlat, self.qlon, self.mlat, self.mlon, self.f1, self.f2
62
        del self.fmag, self.d1, self.d2, self.d3, self.dmag, self.e1
63
        del self.e2, self.e3, self.precision, self.glat, self.glon, self.error
64
        del self.out, self.test_out
65
66
    def run_test_evaluation(self, rtol=1e-5, atol=1e-5):
67
        """Run the evaluation of the test results.
68
69
        Parameters
70
        ----------
71
        rtol : float
72
            Relative tolerance, default value based on old code's precision
73
            (default=1e-5)
74
        atol : float
75
            Absolute tolerance, default value based on old code's precision
76
            (default=1e-5)
77
78
        """
79
80
        assert self.out is not None, "No results to test"
81
        assert self.test_out is not None, "No 'truth' results provided"
82
        assert len(self.out) == len(self.test_out), "Mismatched outputs"
83
84
        for i, out_val in enumerate(self.out):
85
            assert_allclose(out_val, self.test_out[i], rtol=rtol, atol=atol)
86
        return
87
88
    def test_apxg2q(self):
89
        """Test fortran apex geographic to quasi-dipole."""
90
        # Get the output
91
        self.out = fa.apxg2q(self.lat, self.lon, self.height, self.vecflg)
92
93
        # Test the output
94
        self.test_out = (self.qlat, self.qlon, self.f1, self.f2, self.fmag)
95
        self.run_test_evaluation()
96
        return
97
98
    def test_apxg2all(self):
99
        """Test fortran apex geographic to all outputs."""
100
        # Get the output
101
        self.out = fa.apxg2all(self.lat, self.lon, self.height, self.refh,
102
                               self.vecflg)
103
104
        # Test the output
105
        self.test_out = (self.qlat, self.qlon, self.mlat, self.mlon, self.f1,
106
                         self.f2, self.fmag, self.d1, self.d2, self.d3,
107
                         self.dmag, self.e1, self.e2, self.e3)
108
        self.run_test_evaluation()
109
        return
110
111
    def test_apxq2g(self):
112
        """ Test fortran quasi-dipole to geographic."""
113
        # Get the output
114
        self.out = fa.apxq2g(self.lat, self.lon, self.height, self.precision)
115
116
        # Test the output
117
        self.test_out = (self.glat, self.glon, self.error)
118
        self.run_test_evaluation()
119
        return
120
121
    @pytest.mark.parametrize("lat", [0, 30, 60, 89])
122
    @pytest.mark.parametrize("lon", [-179, -90, 0, 90, 179])
123
    def test_g2q2d(self, lat, lon):
124
        """ Test fortran geographic to quasi-dipole and back again."""
125
        self.out = fa.apxg2q(lat, lon, self.height, 0)
126
        self.test_out = fa.apxq2g(self.out[0], self.out[1], self.height,
127
                                  self.precision)
128
129
        # Test the results againt the initial input
130
        assert_allclose(self.test_out[0], lat, atol=0.01)
131
        assert_allclose(self.test_out[1], lon, atol=0.01)
132
133
    def test_apxq2g_lowprecision(self):
134
        """Test low precision error value."""
135
        self.out = fa.apxq2g(self.lat, self.lon, self.height, -1)
136
137
        # Test the output
138
        self.test_out = (51.00891876220703, -66.11973571777344, -9999.0)
139
        self.run_test_evaluation()
140
        return
141