Passed
Pull Request — develop (#86)
by Angeline
01:28
created

TestFortranApex.test_g2q2d()   A

Complexity

Conditions 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 11
rs 10
c 0
b 0
f 0
cc 1
nop 3
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(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(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-6, atol=1e-6):
67
        """Run the evaluation of the test results."""
68
69
        assert self.out is not None, "No results to test"
70
        assert self.test_out is not None, "No 'truth' results provided"
71
        assert len(self.out) == len(self.test_out), "Mismatched outputs"
72
73
        for i, out_val in enumerate(self.out):
74
            assert_allclose(out_val, self.test_out[i], rtol=rtol, atol=atol)
75
        return
76
77
    def test_apxg2q(self):
78
        """Test fortran apex geographic to quasi-dipole."""
79
        # Get the output
80
        self.out = fa.apxg2q(self.lat, self.lon, self.height, self.vecflg)
81
82
        # Test the output
83
        self.test_out = (self.qlat, self.qlon, self.f1, self.f2, self.fmag)
84
        self.run_test_evaluation()
85
        return
86
87
    def test_apxg2all(self):
88
        """Test fortran apex geographic to all outputs."""
89
        # Get the output
90
        self.out = fa.apxg2all(self.lat, self.lon, self.height, self.refh,
91
                               self.vecflg)
92
93
        # Test the output
94
        self.test_out = (self.qlat, self.qlon, self.mlat, self.mlon, self.f1,
95
                         self.f2, self.fmag, self.d1, self.d2, self.d3,
96
                         self.dmag, self.e1, self.e2, self.e3)
97
        self.run_test_evaluation()
98
        return
99
100
    def test_apxq2g(self):
101
        """ Test fortran quasi-dipole to geographic."""
102
        # Get the output
103
        self.out = fa.apxq2g(self.lat, self.lon, self.height, self.precision)
104
105
        # Test the output
106
        self.test_out = (self.glat, self.glon, self.error)
107
        self.run_test_evaluation()
108
        return
109
110
    @pytest.mark.parametrize("lat", [0, 30, 60, 89])
111
    @pytest.mark.parametrize("lon", [-179, -90, 0, 90, 179])
112
    def test_g2q2d(self, lat, lon):
113
        """ Test fortran geographic to quasi-dipole and back again."""
114
        self.out = fa.apxg2q(lat, lon, self.height, 0)
115
        self.test_out = fa.apxq2g(self.out[0], self.out[1], self.height,
116
                                  self.precision)
117
118
        # Test the results againt the initial input
119
        assert_allclose(self.test_out[0], lat, atol=0.01)
120
        assert_allclose(self.test_out[1], lon, atol=0.01)
121
122
    def test_apxq2g_lowprecision(self):
123
        """Test low precision error value."""
124
        self.out = fa.apxq2g(self.lat, self.lon, self.height, -1)
125
126
        # Test the output
127
        self.test_out = (51.00891876220703, -66.11973571777344, -9999.0)
128
        self.run_test_evaluation()
129
        return
130