Passed
Pull Request — master (#60)
by Angeline
01:47
created

TestCAACGMV2.test_convert_geocentric()   A

Complexity

Conditions 1

Size

Total Lines 25
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 24
dl 0
loc 25
rs 9.304
c 0
b 0
f 0
cc 1
nop 5
1
# -*- coding: utf-8 -*-
2
from __future__ import division, absolute_import, unicode_literals
3
4
import datetime as dt
5
import numpy as np
6
import pytest
7
8
import aacgmv2
9
10
11
class TestCAACGMV2:
12
    def setup(self):
13
        """Runs before every method to create a clean testing setup"""
14
        self.date_args = [(2014, 3, 22, 3, 11, 0), (2018, 1, 1, 0, 0, 0)]
15
        self.long_date = [2014, 3, 22, 3, 11, 0]
16
        self.mlat = None
17
        self.mlon = None
18
        self.rshell = None
19
        self.mlt = None
20
        self.lat_in = [45.5, 60]
21
        self.lon_in = [-23.5, 0]
22
        self.alt_in = [1135, 300]
23
        self.code = {'G2A': aacgmv2._aacgmv2.G2A, 'A2G': aacgmv2._aacgmv2.A2G,
24
                     'TG2A': aacgmv2._aacgmv2.G2A + aacgmv2._aacgmv2.TRACE,
25
                     'TA2G': aacgmv2._aacgmv2.A2G + aacgmv2._aacgmv2.TRACE}
26
        self.lat_comp = {'G2A': [48.1902, 58.2194], 'A2G': [30.7550, 50.4371],
27
                         'TG2A': [48.1954, 58.2189], 'TA2G': [30.7661, 50.4410]}
28
        self.lon_comp = {'G2A': [57.7505, 80.7282], 'A2G': [-94.1724, -77.5323],
29
                         'TG2A': [57.7456, 80.7362],
30
                         'TA2G': [-94.1727, -77.5440]}
31
        self.r_comp = {'G2A': [1.1775, 1.0457], 'A2G': [1133.6246, 305.7308],
32
                       'TG2A': [1.1775, 1.0457], 'TA2G': [1133.6282, 305.7322]}
33
34
    def teardown(self):
35
        """Runs after every method to clean up previous testing"""
36
        del self.date_args, self.long_date, self.mlat, self.mlon, self.mlt
37
        del self.lat_in, self.lon_in, self.alt_in, self.lat_comp, self.lon_comp
38
        del self.r_comp, self.code
39
40
    def test_constants(self):
41
        """Test module constants"""
42
        ans1 = aacgmv2._aacgmv2.G2A == 0
43
        ans2 = aacgmv2._aacgmv2.A2G == 1
44
        ans3 = aacgmv2._aacgmv2.TRACE == 2
45
        ans4 = aacgmv2._aacgmv2.ALLOWTRACE == 4
46
        ans5 = aacgmv2._aacgmv2.BADIDEA == 8
47
        ans6 = aacgmv2._aacgmv2.GEOCENTRIC == 16
48
49
        assert ans1 & ans2 & ans3 & ans4 & ans5 & ans6
50
        del ans1, ans2, ans3, ans4, ans5, ans6
51
52
    @pytest.mark.parametrize('idate', [0, 1])
53
    def test_set_datetime(self, idate):
54
        """Test set_datetime"""
55
        self.mlt = aacgmv2._aacgmv2.set_datetime(*self.date_args[idate]) is None
56
        assert self.mlt
57
58
    @classmethod
59
    def test_fail_set_datetime(self):
60
        """Test unsuccessful set_datetime"""
61
        with pytest.raises(RuntimeError):
62
            aacgmv2._aacgmv2.set_datetime(1013, 1, 1, 0, 0, 0)
63
64
    @pytest.mark.parametrize('idate,ckey', [(0, 'G2A'), (1, 'G2A'),
65
                                            (0, 'A2G'), (1, 'A2G'),
66
                                            (0, 'TG2A'), (1, 'TG2A'),
67
                                            (0, 'TA2G'), (1, 'TA2G')])
68
    def test_convert(self, idate, ckey):
69
        """Test convert from geographic to magnetic coordinates"""
70
        aacgmv2._aacgmv2.set_datetime(*self.date_args[idate])
71
        (self.mlat, self.mlon,
72
         self.rshell) = aacgmv2._aacgmv2.convert(self.lat_in[idate],
73
                                                 self.lon_in[idate],
74
                                                 self.alt_in[idate],
75
                                                 self.code[ckey])
76
        np.testing.assert_almost_equal(self.mlat, self.lat_comp[ckey][idate],
77
                                       decimal=4)
78
        np.testing.assert_almost_equal(self.mlon, self.lon_comp[ckey][idate],
79
                                       decimal=4)
80
        np.testing.assert_almost_equal(self.rshell, self.r_comp[ckey][idate],
81
                                       decimal=4)
82
83
    @pytest.mark.parametrize('ckey', ['G2A', 'A2G', 'TG2A', 'TA2G'])
84
    def test_convert_arr(self, ckey):
85
        """Test convert_arr using from magnetic to geodetic coordinates"""
86
        aacgmv2._aacgmv2.set_datetime(*self.date_args[0])
87
        (self.mlat, self.mlon, self.rshell,
88
         bad_ind) = aacgmv2._aacgmv2.convert_arr(self.lat_in, self.lon_in,
89
                                                 self.alt_in,
90
                                                 self.code[ckey])
91
92
        assert len(self.mlat) == len(self.lat_in)
93
        np.testing.assert_almost_equal(self.mlat[0], self.lat_comp[ckey][0],
94
                                       decimal=4)
95
        np.testing.assert_almost_equal(self.mlon[0], self.lon_comp[ckey][0],
96
                                       decimal=4)
97
        np.testing.assert_almost_equal(self.rshell[0], self.r_comp[ckey][0],
98
                                       decimal=4)
99
        assert bad_ind[0] == -1
100
101
    def test_convert_high_denied(self):
102
        """Test for failure when converting to high altitude geodetic to
103
        magnetic coordinates"""
104
        aacgmv2._aacgmv2.set_datetime(*self.date_args[0])
105
        with pytest.raises(RuntimeError):
106
            aacgmv2._aacgmv2.convert(self.lat_in[0], self.lon_in[0], 5500,
107
                                     aacgmv2._aacgmv2.G2A)
108
109
    @pytest.mark.parametrize('code,lat_comp,lon_comp,r_comp',
110
                             [(aacgmv2._aacgmv2.G2A + aacgmv2._aacgmv2.TRACE,
111
                               59.9753, 57.7294, 1.8626),
112
                              (aacgmv2._aacgmv2.G2A
113
                               + aacgmv2._aacgmv2.ALLOWTRACE, 59.9753, 57.7294,
114
                               1.8626),
115
                              (aacgmv2._aacgmv2.G2A + aacgmv2._aacgmv2.BADIDEA,
116
                               58.7286, 56.4296, 1.8626)])
117
    def test_convert_high(self, code, lat_comp, lon_comp, r_comp):
118
        """Test convert from high altitude geodetic to magnetic coordinates"""
119
        aacgmv2._aacgmv2.set_datetime(*self.date_args[0])
120
        (self.mlat, self.mlon,
121
         self.rshell) = aacgmv2._aacgmv2.convert(self.lat_in[0], self.lon_in[0],
122
                                                 5500, code)
123
        np.testing.assert_almost_equal(self.mlat, lat_comp, decimal=4)
124
        np.testing.assert_almost_equal(self.mlon, lon_comp, decimal=4)
125
        np.testing.assert_almost_equal(self.rshell, r_comp, decimal=4)
126
127
    @pytest.mark.parametrize('code,lat_comp,lon_comp,r_comp',
128
                             [(aacgmv2._aacgmv2.G2A
129
                               + aacgmv2._aacgmv2.GEOCENTRIC, 48.3784, 57.7844,
130
                               1.1781),
131
                              (aacgmv2._aacgmv2.G2A
132
                               + aacgmv2._aacgmv2.GEOCENTRIC, 48.3784, 57.7844,
133
                               1.1781),
134
                              (aacgmv2._aacgmv2.A2G
135
                               + aacgmv2._aacgmv2.GEOCENTRIC, 30.6117, -94.1724,
136
                               1135.0000),
137
                              (aacgmv2._aacgmv2.G2A + aacgmv2._aacgmv2.TRACE
138
                               + aacgmv2._aacgmv2.GEOCENTRIC, 48.3836, 57.7793,
139
                               1.1781),
140
                              (aacgmv2._aacgmv2.A2G + aacgmv2._aacgmv2.TRACE
141
                               + aacgmv2._aacgmv2.GEOCENTRIC, 30.6227, -94.1727,
142
                               1135.0000)])
143
    def test_convert_geocentric(self, code, lat_comp, lon_comp, r_comp):
144
        """Test convert for different code inputs with geocentric coords"""
145
        aacgmv2._aacgmv2.set_datetime(*self.date_args[0])
146
        (self.mlat, self.mlon,
147
         self.rshell) = aacgmv2._aacgmv2.convert(self.lat_in[0], self.lon_in[0],
148
                                                 self.alt_in[0], code)
149
        np.testing.assert_almost_equal(self.mlat, lat_comp, decimal=4)
150
        np.testing.assert_almost_equal(self.mlon, lon_comp, decimal=4)
151
        np.testing.assert_almost_equal(self.rshell, r_comp, decimal=4)
152
153
    @classmethod
154
    def test_forbidden(self):
155
        """Test convert failure"""
156
        with pytest.raises(RuntimeError):
157
            aacgmv2._aacgmv2.convert(7, 0, 0, aacgmv2._aacgmv2.G2A)
158
159
    @pytest.mark.parametrize('marg,mlt_comp',
160
                             [(12.0, -153.6033), (25.0, 41.3967),
161
                              (-1.0, 11.3967)])
162
    def test_inv_mlt_convert(self, marg, mlt_comp):
163
        """Test MLT inversion"""
164
        mlt_args = list(self.long_date)
165
        mlt_args.append(marg)
166
        self.mlon = aacgmv2._aacgmv2.inv_mlt_convert(*mlt_args)
167
        np.testing.assert_almost_equal(self.mlon, mlt_comp, decimal=4)
168
169
        del mlt_args
170
171
    @pytest.mark.parametrize('marg,mlt_comp',
172
                             [(12.0, -153.6033), (25.0, 41.3967),
173
                              (-1.0, 11.3967)])
174
    def test_inv_mlt_convert_yrsec(self, marg, mlt_comp):
175
        """Test MLT inversion with year and seconds of year"""
176
        dtime = dt.datetime(*self.long_date)
177
        soy = (int(dtime.strftime("%j")) - 1) * 86400 + dtime.hour * 3600 \
178
            + dtime.minute * 60 + dtime.second
179
180
        self.mlon = aacgmv2._aacgmv2.inv_mlt_convert_yrsec(dtime.year, soy,
181
                                                           marg)
182
183
        np.testing.assert_almost_equal(self.mlon, mlt_comp, decimal=4)
184
185
        del dtime, soy
186
187
    @pytest.mark.parametrize('marg,mlt_comp',
188
                             [(270.0, 16.2402), (80.0, 3.5736),
189
                              (-90.0, 16.2402)])
190
    def test_mlt_convert(self, marg, mlt_comp):
191
        """Test MLT calculation with different longitudes"""
192
        mlt_args = list(self.long_date)
193
        mlt_args.append(marg)
194
        self.mlt = aacgmv2._aacgmv2.mlt_convert(*mlt_args)
195
        np.testing.assert_almost_equal(self.mlt, mlt_comp, decimal=4)
196
197
    @pytest.mark.parametrize('marg,mlt_comp',
198
                             [(270.0, 16.2402), (80.0, 3.5736),
199
                              (-90.0, 16.2402)])
200
    def test_mlt_convert_yrsec(self, marg, mlt_comp):
201
        """Test MLT calculation using year and seconds of year"""
202
        dtime = dt.datetime(*self.long_date)
203
        soy = (int(dtime.strftime("%j")) - 1) * 86400 + dtime.hour * 3600 \
204
            + dtime.minute * 60 + dtime.second
205
206
        self.mlt = aacgmv2._aacgmv2.mlt_convert_yrsec(dtime.year, soy, marg)
207
208
        np.testing.assert_almost_equal(self.mlt, mlt_comp, decimal=4)
209
210
        del dtime, soy
211