Passed
Pull Request — develop (#32)
by Angeline
01:53
created

TestCAACGMV2.test_module_structure()   A

Complexity

Conditions 1

Size

Total Lines 11
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 1
nop 1
1
# -*- coding: utf-8 -*-
2
from __future__ import division, absolute_import, unicode_literals
3
4
import numpy as np
5
import pytest
6
import aacgmv2
7
8
class TestCAACGMV2:
9
    def setup(self):
10
        """Runs before every method to create a clean testing setup"""
11
        self.date_args = [(2014, 3, 22, 3, 11, 0), (2018, 1, 1, 0, 0, 0)]
12
        self.long_date = [2014, 3, 22, 3, 11, 0]
13
        self.mlat = None
14
        self.mlon = None
15
        self.rshell = None
16
        self.mlt = None
17
        self.lat_in = [45.5, 60]
18
        self.lon_in = [-23.5, 0]
19
        self.alt_in = [1135, 300]
20
21
    def teardown(self):
22
        """Runs after every method to clean up previous testing"""
23
        del self.date_args, self.long_date, self.mlat, self.mlon, self.mlt
24
        del self.lat_in, self.lon_in, self.alt_in
25
26
    def test_constants(self):
27
        """Test module constants"""
28
        ans1 = aacgmv2._aacgmv2.G2A == 0
29
        ans2 = aacgmv2._aacgmv2.A2G == 1
30
        ans3 = aacgmv2._aacgmv2.TRACE == 2
31
        ans4 = aacgmv2._aacgmv2.ALLOWTRACE == 4
32
        ans5 = aacgmv2._aacgmv2.BADIDEA == 8
33
        ans6 = aacgmv2._aacgmv2.GEOCENTRIC == 16
34
35
        assert ans1 & ans2 & ans3 & ans4 & ans5 & ans6
36
        del ans1, ans2, ans3, ans4, ans5, ans6
37
38
    def test_set_datetime(self):
39
        """Test set_datetime"""
40
        for darg in self.date_args:
41
            arg1 = aacgmv2._aacgmv2.set_datetime(*darg) is None
42
            assert arg1
43
44
    @classmethod
45
    def test_fail_set_datetime(self):
46
        """Test unsuccessful set_datetime"""
47
        with pytest.raises(RuntimeError):
48
            aacgmv2._aacgmv2.set_datetime(1013, 1, 1, 0, 0, 0)
49
50 View Code Duplication
    def test_convert_G2A_coeff(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
51
        """Test convert from geographic to magnetic coordinates"""
52
        lat_comp = [48.1896, 58.1633]
53
        lon_comp = [57.7635, 81.0719]
54
        r_comp = [1.1775, 1.0457]
55
56
        for i,darg in enumerate(self.date_args):
57
            aacgmv2._aacgmv2.set_datetime(*darg)
58
            (self.mlat, self.mlon,
59
             self.rshell) = aacgmv2._aacgmv2.convert(self.lat_in[i],
60
                                                     self.lon_in[i],
61
                                                     self.alt_in[i],
62
                                                     aacgmv2._aacgmv2.G2A)
63
            np.testing.assert_almost_equal(self.mlat, lat_comp[i], decimal=4)
64
            np.testing.assert_almost_equal(self.mlon, lon_comp[i], decimal=4)
65
            np.testing.assert_almost_equal(self.rshell, r_comp[i], decimal=4)
66
67
        del lat_comp, lon_comp, r_comp
68
69 View Code Duplication
    def test_convert_A2G_coeff(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
70
        """Test convert from magnetic to geodetic coordinates"""
71
        lat_comp = [30.7534, 50.3910]
72
        lon_comp = [-94.1806, -77.7919]
73
        r_comp = [1133.6241, 305.7138]
74
75
        for i,darg in enumerate(self.date_args):
76
            aacgmv2._aacgmv2.set_datetime(*darg)
77
            (self.mlat, self.mlon,
78
             self.rshell) = aacgmv2._aacgmv2.convert(self.lat_in[i],
79
                                                     self.lon_in[i],
80
                                                     self.alt_in[i],
81
                                                     aacgmv2._aacgmv2.A2G)
82
            np.testing.assert_almost_equal(self.mlat, lat_comp[i], decimal=4)
83
            np.testing.assert_almost_equal(self.mlon, lon_comp[i], decimal=4)
84
            np.testing.assert_almost_equal(self.rshell, r_comp[i], decimal=4)
85
86
        del lat_comp, lon_comp, r_comp
87
88 View Code Duplication
    def test_convert_G2A_TRACE(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
89
        """Test convert from geodetic to magnetic coordinates using trace"""
90
        code = aacgmv2._aacgmv2.G2A + aacgmv2._aacgmv2.TRACE
91
        trace_lat = [48.1948, 58.1633]
92
        trace_lon = [57.7588, 81.0756]
93
        trace_r = [1.1775,  1.0457]
94
95
        for i,dargs in enumerate(self.date_args):
96
            aacgmv2._aacgmv2.set_datetime(*dargs)
97
            (self.mlat, self.mlon,
98
             self.rshell) = aacgmv2._aacgmv2.convert(self.lat_in[i],
99
                                                     self.lon_in[i],
100
                                                     self.alt_in[i], code)
101
            np.testing.assert_almost_equal(self.mlat, trace_lat[i], decimal=4)
102
            np.testing.assert_almost_equal(self.mlon, trace_lon[i], decimal=4)
103
            np.testing.assert_almost_equal(self.rshell, trace_r[i], decimal=4)
104
105
        del code, trace_lat, trace_lon, trace_r
106
107 View Code Duplication
    def test_convert_A2G_TRACE(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
108
        """Test convert from magnetic to geodetic coordinates using trace"""
109
        code = aacgmv2._aacgmv2.A2G + aacgmv2._aacgmv2.TRACE
110
        trace_lat = [30.7644, 50.3958]
111
        trace_lon = [-94.1809, -77.8019]
112
        trace_r = [1133.6277, 305.7156]
113
        
114
        for i,dargs in enumerate(self.date_args):
115
            aacgmv2._aacgmv2.set_datetime(*dargs)
116
            (self.mlat, self.mlon,
117
             self.rshell) = aacgmv2._aacgmv2.convert(self.lat_in[i],
118
                                                     self.lon_in[i],
119
                                                     self.alt_in[i], code)
120
            np.testing.assert_almost_equal(self.mlat, trace_lat[i], decimal=4)
121
            np.testing.assert_almost_equal(self.mlon, trace_lon[i], decimal=4)
122
            np.testing.assert_almost_equal(self.rshell, trace_r[i], decimal=4)
123
124
        del code, trace_lat, trace_lon, trace_r
125
126
    def test_convert_high_denied(self):
127
        """Test for failure when converting to high altitude geodetic to
128
        magnetic coordinates"""
129
        aacgmv2._aacgmv2.set_datetime(*self.date_args[0])
130
        with pytest.raises(RuntimeError):
131
            aacgmv2._aacgmv2.convert(self.lat_in[0], self.lon_in[0], 5500,
132
                                     aacgmv2._aacgmv2.G2A)
133
134
    def test_convert_high_TRACE(self):
135
        """Test convert from high altitude geodetic to magnetic coordinates
136
        using trace"""
137
        code = aacgmv2._aacgmv2.G2A + aacgmv2._aacgmv2.TRACE
138
        aacgmv2._aacgmv2.set_datetime(*self.date_args[0])
139
        (self.mlat, self.mlon,
140
         self.rshell) = aacgmv2._aacgmv2.convert(self.lat_in[0], self.lon_in[0],
141
                                                 5500, code)
142
        np.testing.assert_almost_equal(self.mlat, 59.9748, decimal=4)
143
        np.testing.assert_almost_equal(self.mlon, 57.7425, decimal=4)
144
        np.testing.assert_almost_equal(self.rshell, 1.8626, decimal=4)
145
146
        del code
147
148
    def test_convert_high_ALLOWTRACE(self):
149
        """Test convert from high altitude geodetic to magnetic coordinates
150
        by allowing IGRF tracing"""
151
        code = aacgmv2._aacgmv2.G2A + aacgmv2._aacgmv2.ALLOWTRACE
152
        aacgmv2._aacgmv2.set_datetime(*self.date_args[0])
153
        (self.mlat, self.mlon,
154
         self.rshell) = aacgmv2._aacgmv2.convert(self.lat_in[0], self.lon_in[0],
155
                                                 5500, code)
156
        np.testing.assert_almost_equal(self.mlat, 59.9748, decimal=4)
157
        np.testing.assert_almost_equal(self.mlon, 57.7425, decimal=4)
158
        np.testing.assert_almost_equal(self.rshell, 1.8626, decimal=4)
159
160
        del code
161
162
    def test_convert_high_BADIDEA(self):
163
        """Test convert from high altitude geodetic to magnetic coordinates
164
        using coefficients"""
165
        code = aacgmv2._aacgmv2.G2A + aacgmv2._aacgmv2.BADIDEA
166
        aacgmv2._aacgmv2.set_datetime(*self.date_args[0])
167
        (self.mlat, self.mlon,
168
         self.rshell) = aacgmv2._aacgmv2.convert(self.lat_in[0], self.lon_in[0],
169
                                                 5500, code)
170
        np.testing.assert_almost_equal(self.mlat, 58.7154, decimal=4)
171
        np.testing.assert_almost_equal(self.mlon, 56.5830, decimal=4)
172
        np.testing.assert_almost_equal(self.rshell, 1.8626, decimal=4)
173
174
        del code
175
176 View Code Duplication
    def test_convert_GEOCENTRIC_G2A_coeff(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
177
        """Test convert from geographic to magnetic coordinates"""
178
        code = aacgmv2._aacgmv2.G2A + aacgmv2._aacgmv2.GEOCENTRIC
179
        aacgmv2._aacgmv2.set_datetime(*self.date_args[0])
180
        (self.mlat, self.mlon,
181
         self.rshell) = aacgmv2._aacgmv2.convert(self.lat_in[0], self.lon_in[0],
182
                                                 self.alt_in[0], code)
183
        np.testing.assert_almost_equal(self.mlat, 48.3779, decimal=4)
184
        np.testing.assert_almost_equal(self.mlon, 57.7974, decimal=4)
185
        np.testing.assert_almost_equal(self.rshell, 1.1781, decimal=4)
186
187
        del code
188
        
189 View Code Duplication
    def test_convert_GEOCENTRIC_A2G_coeff(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
190
        """Test convert from magnetic to geocentric coordinates"""
191
        code = aacgmv2._aacgmv2.A2G + aacgmv2._aacgmv2.GEOCENTRIC
192
        aacgmv2._aacgmv2.set_datetime(*self.date_args[0])
193
        (self.mlat, self.mlon,
194
         self.rshell) = aacgmv2._aacgmv2.convert(self.lat_in[0], self.lon_in[0],
195
                                                 self.alt_in[0], code)
196
        np.testing.assert_almost_equal(self.mlat, 30.6101, decimal=4)
197
        np.testing.assert_almost_equal(self.mlon, -94.1806, decimal=4)
198
        np.testing.assert_almost_equal(self.rshell, 1135.0000, decimal=4)
199
200
        del code
201
202 View Code Duplication
    def test_convert_GEOCENTRIC_G2A_TRACE(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
203
        """Test convert from geographic to magnetic coordinates using trace"""
204
        code = aacgmv2._aacgmv2.G2A + aacgmv2._aacgmv2.TRACE + \
205
               aacgmv2._aacgmv2.GEOCENTRIC
206
        aacgmv2._aacgmv2.set_datetime(*self.date_args[0])
207
        (self.mlat, self.mlon,
208
         self.rshell) = aacgmv2._aacgmv2.convert(self.lat_in[0], self.lon_in[0],
209
                                                 self.alt_in[0], code)
210
        np.testing.assert_almost_equal(self.mlat, 48.3830, decimal=4)
211
        np.testing.assert_almost_equal(self.mlon, 57.7926, decimal=4)
212
        np.testing.assert_almost_equal(self.rshell, 1.1781, decimal=4)
213
214
        del code
215
216 View Code Duplication
    def test_convert_GEOCENTRIC_A2G_TRACE(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
217
        """Test convert from magnetic to geographic coordinates using trace"""
218
        code = aacgmv2._aacgmv2.A2G + aacgmv2._aacgmv2.TRACE + \
219
               aacgmv2._aacgmv2.GEOCENTRIC
220
        aacgmv2._aacgmv2.set_datetime(*self.date_args[0])
221
        (self.mlat, self.mlon,
222
         self.rshell) = aacgmv2._aacgmv2.convert(self.lat_in[0], self.lon_in[0],
223
                                                 self.alt_in[0], code)
224
        np.testing.assert_almost_equal(self.mlat, 30.6211, decimal=4)
225
        np.testing.assert_almost_equal(self.mlon, -94.1809, decimal=4)
226
        np.testing.assert_almost_equal(self.rshell, 1135.0000, decimal=4)
227
228
        del code
229
230
    @classmethod
231
    def test_forbidden(self):
232
        """Test convert failure"""
233
        with pytest.raises(RuntimeError):
234
            aacgmv2._aacgmv2.convert(7, 0, 0, aacgmv2._aacgmv2.G2A)
235
236 View Code Duplication
    def test_inv_mlt_convert(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
237
        """Test MLT inversion"""
238
        mlt_args = list(self.long_date)
239
        mlt_args.extend([12.0])
240
        self.mlon = aacgmv2._aacgmv2.inv_mlt_convert(*mlt_args)
241
        np.testing.assert_almost_equal(self.mlon, -153.5931, decimal=4)
242
243
        mlt_args[-1] = 25.0
244
        self.mlon = aacgmv2._aacgmv2.inv_mlt_convert(*mlt_args)
245
        np.testing.assert_almost_equal(self.mlon, 41.4069, decimal=4)
246
247
        mlt_args[-1] = -1.0
248
        self.mlon = aacgmv2._aacgmv2.inv_mlt_convert(*mlt_args)
249
        np.testing.assert_almost_equal(self.mlon, 11.4069, decimal=4)
250
251
        del mlt_args
252
253
    def test_inv_mlt_convert_yrsec(self):
254
        """Test MLT inversion with year and seconds of year"""
255
        import datetime as dt
256
        dtime = dt.datetime(*self.long_date)
257
        soy = (int(dtime.strftime("%j"))-1) * 86400 + dtime.hour * 3600 + \
258
              dtime.minute * 60 + dtime.second
259
        
260
        mlt_args_1 = [dtime.year, soy, 12.0]
261
        mlt_args_2 = [dtime.year, soy, 25.0]
262
        mlt_args_3 = [dtime.year, soy, -1.0]
263
264
        mlon_1 = aacgmv2._aacgmv2.inv_mlt_convert_yrsec(*mlt_args_1)
265
        mlon_2 = aacgmv2._aacgmv2.inv_mlt_convert_yrsec(*mlt_args_2)
266
        mlon_3 = aacgmv2._aacgmv2.inv_mlt_convert_yrsec(*mlt_args_3)
267
268
        np.testing.assert_almost_equal(mlon_1, -153.5931, decimal=4)
269
        np.testing.assert_almost_equal(mlon_2, 41.4069, decimal=4)
270
        np.testing.assert_almost_equal(mlon_3, 11.4069, decimal=4)
271
272
        del dtime, soy, mlt_args_1, mlt_args_2, mlt_args_3, mlon_1, mlon_2
273
        del mlon_3
274
275 View Code Duplication
    def test_mlt_convert(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
276
        """Test MLT calculation with different longitudes"""
277
        mlt_args = list(self.long_date)
278
        mlt_args.extend([270.0])
279
        self.mlt = aacgmv2._aacgmv2.mlt_convert(*mlt_args)
280
        np.testing.assert_almost_equal(self.mlt, 16.2395, decimal=4)
281
282
        mlt_args[-1] = 80.0
283
        self.mlt = aacgmv2._aacgmv2.mlt_convert(*mlt_args)
284
        np.testing.assert_almost_equal(self.mlt, 3.5729, decimal=4)
285
286
        mlt_args[-1] = -90.0
287
        self.mlt = aacgmv2._aacgmv2.mlt_convert(*mlt_args)
288
        np.testing.assert_almost_equal(self.mlt, 16.2395, decimal=4)
289
290
        del mlt_args
291
292
    def test_mlt_convert_yrsec(self):
293
        """Test MLT calculation using year and seconds of year"""
294
        import datetime as dt
295
        dtime = dt.datetime(*self.long_date)
296
        soy = (int(dtime.strftime("%j"))-1) * 86400 + dtime.hour * 3600 + \
297
            dtime.minute * 60 + dtime.second
298
        mlt_args_1 = [dtime.year, soy, 270.0]
299
        mlt_args_2 = [dtime.year, soy, 80.0]
300
        mlt_args_3 = [dtime.year, soy, -90.0]
301
        
302
        mlt_1 = aacgmv2._aacgmv2.mlt_convert_yrsec(*mlt_args_1)
303
        mlt_2 = aacgmv2._aacgmv2.mlt_convert_yrsec(*mlt_args_2)
304
        mlt_3 = aacgmv2._aacgmv2.mlt_convert_yrsec(*mlt_args_3)
305
306
        np.testing.assert_almost_equal(mlt_1, 16.2395, decimal=4)
307
        np.testing.assert_almost_equal(mlt_2, 3.5729, decimal=4)
308
        np.testing.assert_equal(mlt_1, mlt_3)
309
310
        del dtime, soy, mlt_args_1, mlt_args_2, mlt_args_3, mlt_1, mlt_2, mlt_3
311