Passed
Pull Request — master (#40)
by Angeline
01:18
created

TestCAACGMV2.test_convert_G2A_coeff()   A

Complexity

Conditions 2

Size

Total Lines 18
Code Lines 15

Duplication

Lines 18
Ratio 100 %

Importance

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