Passed
Pull Request — master (#46)
by Angeline
03:47
created

test_c_aacgmv2.TestCAACGMV2.test_convert_arr()   A

Complexity

Conditions 2

Size

Total Lines 20
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 20
rs 9.6
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
    def test_convert_arr(self):
90
        """Test convert_arr using from magnetic to geodetic coordinates"""
91
        lat_comp = [30.7534, 50.0891]
92
        lon_comp = [-94.1806, -77.3773]
93
        r_comp = [1133.6241, 305.602877]
94
95
        aacgmv2._aacgmv2.set_datetime(*self.date_args[0])
96
        (self.mlat, self.mlon, self.rshell,
97
         bad_ind) = aacgmv2._aacgmv2.convert_arr(self.lat_in, self.lon_in,
98
                                                 self.alt_in,
99
                                                 aacgmv2._aacgmv2.A2G)
100
101
        assert len(self.mlat) == len(self.lat_in)
102
        for i, ll in enumerate(self.mlat):
103
            np.testing.assert_almost_equal(ll, lat_comp[i], decimal=4)
104
            np.testing.assert_almost_equal(self.mlon[i], lon_comp[i], decimal=4)
105
            np.testing.assert_almost_equal(self.rshell[i], r_comp[i], decimal=4)
106
            assert bad_ind[i] == -1
107
108
        del lat_comp, lon_comp, r_comp
109
110 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...
111
        """Test convert from geodetic to magnetic coordinates using trace"""
112
        code = aacgmv2._aacgmv2.G2A + aacgmv2._aacgmv2.TRACE
113
        trace_lat = [48.1948, 58.1633]
114
        trace_lon = [57.7588, 81.0756]
115
        trace_r = [1.1775,  1.0457]
116
117
        for i,dargs in enumerate(self.date_args):
118
            aacgmv2._aacgmv2.set_datetime(*dargs)
119
            (self.mlat, self.mlon,
120
             self.rshell) = aacgmv2._aacgmv2.convert(self.lat_in[i],
121
                                                     self.lon_in[i],
122
                                                     self.alt_in[i], code)
123
            np.testing.assert_almost_equal(self.mlat, trace_lat[i], decimal=4)
124
            np.testing.assert_almost_equal(self.mlon, trace_lon[i], decimal=4)
125
            np.testing.assert_almost_equal(self.rshell, trace_r[i], decimal=4)
126
127
        del code, trace_lat, trace_lon, trace_r
128
129 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...
130
        """Test convert from magnetic to geodetic coordinates using trace"""
131
        code = aacgmv2._aacgmv2.A2G + aacgmv2._aacgmv2.TRACE
132
        trace_lat = [30.7644, 50.3958]
133
        trace_lon = [-94.1809, -77.8019]
134
        trace_r = [1133.6277, 305.7156]
135
        
136
        for i,dargs in enumerate(self.date_args):
137
            aacgmv2._aacgmv2.set_datetime(*dargs)
138
            (self.mlat, self.mlon,
139
             self.rshell) = aacgmv2._aacgmv2.convert(self.lat_in[i],
140
                                                     self.lon_in[i],
141
                                                     self.alt_in[i], code)
142
            np.testing.assert_almost_equal(self.mlat, trace_lat[i], decimal=4)
143
            np.testing.assert_almost_equal(self.mlon, trace_lon[i], decimal=4)
144
            np.testing.assert_almost_equal(self.rshell, trace_r[i], decimal=4)
145
146
        del code, trace_lat, trace_lon, trace_r
147
148
    def test_convert_high_denied(self):
149
        """Test for failure when converting to high altitude geodetic to
150
        magnetic coordinates"""
151
        aacgmv2._aacgmv2.set_datetime(*self.date_args[0])
152
        with pytest.raises(RuntimeError):
153
            aacgmv2._aacgmv2.convert(self.lat_in[0], self.lon_in[0], 5500,
154
                                     aacgmv2._aacgmv2.G2A)
155
156
    def test_convert_high_TRACE(self):
157
        """Test convert from high altitude geodetic to magnetic coordinates
158
        using trace"""
159
        code = aacgmv2._aacgmv2.G2A + aacgmv2._aacgmv2.TRACE
160
        aacgmv2._aacgmv2.set_datetime(*self.date_args[0])
161
        (self.mlat, self.mlon,
162
         self.rshell) = aacgmv2._aacgmv2.convert(self.lat_in[0], self.lon_in[0],
163
                                                 5500, code)
164
        np.testing.assert_almost_equal(self.mlat, 59.9748, decimal=4)
165
        np.testing.assert_almost_equal(self.mlon, 57.7425, decimal=4)
166
        np.testing.assert_almost_equal(self.rshell, 1.8626, decimal=4)
167
168
        del code
169
170
    def test_convert_high_ALLOWTRACE(self):
171
        """Test convert from high altitude geodetic to magnetic coordinates
172
        by allowing IGRF tracing"""
173
        code = aacgmv2._aacgmv2.G2A + aacgmv2._aacgmv2.ALLOWTRACE
174
        aacgmv2._aacgmv2.set_datetime(*self.date_args[0])
175
        (self.mlat, self.mlon,
176
         self.rshell) = aacgmv2._aacgmv2.convert(self.lat_in[0], self.lon_in[0],
177
                                                 5500, code)
178
        np.testing.assert_almost_equal(self.mlat, 59.9748, decimal=4)
179
        np.testing.assert_almost_equal(self.mlon, 57.7425, decimal=4)
180
        np.testing.assert_almost_equal(self.rshell, 1.8626, decimal=4)
181
182
        del code
183
184
    def test_convert_high_BADIDEA(self):
185
        """Test convert from high altitude geodetic to magnetic coordinates
186
        using coefficients"""
187
        code = aacgmv2._aacgmv2.G2A + aacgmv2._aacgmv2.BADIDEA
188
        aacgmv2._aacgmv2.set_datetime(*self.date_args[0])
189
        (self.mlat, self.mlon,
190
         self.rshell) = aacgmv2._aacgmv2.convert(self.lat_in[0], self.lon_in[0],
191
                                                 5500, code)
192
        np.testing.assert_almost_equal(self.mlat, 58.7154, decimal=4)
193
        np.testing.assert_almost_equal(self.mlon, 56.5830, decimal=4)
194
        np.testing.assert_almost_equal(self.rshell, 1.8626, decimal=4)
195
196
        del code
197
198 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...
199
        """Test convert from geographic to magnetic coordinates"""
200
        code = aacgmv2._aacgmv2.G2A + aacgmv2._aacgmv2.GEOCENTRIC
201
        aacgmv2._aacgmv2.set_datetime(*self.date_args[0])
202
        (self.mlat, self.mlon,
203
         self.rshell) = aacgmv2._aacgmv2.convert(self.lat_in[0], self.lon_in[0],
204
                                                 self.alt_in[0], code)
205
        np.testing.assert_almost_equal(self.mlat, 48.3779, decimal=4)
206
        np.testing.assert_almost_equal(self.mlon, 57.7974, decimal=4)
207
        np.testing.assert_almost_equal(self.rshell, 1.1781, decimal=4)
208
209
        del code
210
        
211 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...
212
        """Test convert from magnetic to geocentric coordinates"""
213
        code = aacgmv2._aacgmv2.A2G + aacgmv2._aacgmv2.GEOCENTRIC
214
        aacgmv2._aacgmv2.set_datetime(*self.date_args[0])
215
        (self.mlat, self.mlon,
216
         self.rshell) = aacgmv2._aacgmv2.convert(self.lat_in[0], self.lon_in[0],
217
                                                 self.alt_in[0], code)
218
        np.testing.assert_almost_equal(self.mlat, 30.6101, decimal=4)
219
        np.testing.assert_almost_equal(self.mlon, -94.1806, decimal=4)
220
        np.testing.assert_almost_equal(self.rshell, 1135.0000, decimal=4)
221
222
        del code
223
224 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...
225
        """Test convert from geographic to magnetic coordinates using trace"""
226
        code = aacgmv2._aacgmv2.G2A + aacgmv2._aacgmv2.TRACE + \
227
               aacgmv2._aacgmv2.GEOCENTRIC
228
        aacgmv2._aacgmv2.set_datetime(*self.date_args[0])
229
        (self.mlat, self.mlon,
230
         self.rshell) = aacgmv2._aacgmv2.convert(self.lat_in[0], self.lon_in[0],
231
                                                 self.alt_in[0], code)
232
        np.testing.assert_almost_equal(self.mlat, 48.3830, decimal=4)
233
        np.testing.assert_almost_equal(self.mlon, 57.7926, decimal=4)
234
        np.testing.assert_almost_equal(self.rshell, 1.1781, decimal=4)
235
236
        del code
237
238 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...
239
        """Test convert from magnetic to geographic coordinates using trace"""
240
        code = aacgmv2._aacgmv2.A2G + aacgmv2._aacgmv2.TRACE + \
241
               aacgmv2._aacgmv2.GEOCENTRIC
242
        aacgmv2._aacgmv2.set_datetime(*self.date_args[0])
243
        (self.mlat, self.mlon,
244
         self.rshell) = aacgmv2._aacgmv2.convert(self.lat_in[0], self.lon_in[0],
245
                                                 self.alt_in[0], code)
246
        np.testing.assert_almost_equal(self.mlat, 30.6211, decimal=4)
247
        np.testing.assert_almost_equal(self.mlon, -94.1809, decimal=4)
248
        np.testing.assert_almost_equal(self.rshell, 1135.0000, decimal=4)
249
250
        del code
251
252
    @classmethod
253
    def test_forbidden(self):
254
        """Test convert failure"""
255
        with pytest.raises(RuntimeError):
256
            aacgmv2._aacgmv2.convert(7, 0, 0, aacgmv2._aacgmv2.G2A)
257
258 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...
259
        """Test MLT inversion"""
260
        mlt_args = list(self.long_date)
261
        mlt_args.extend([12.0])
262
        self.mlon = aacgmv2._aacgmv2.inv_mlt_convert(*mlt_args)
263
        np.testing.assert_almost_equal(self.mlon, -153.5931, decimal=4)
264
265
        mlt_args[-1] = 25.0
266
        self.mlon = aacgmv2._aacgmv2.inv_mlt_convert(*mlt_args)
267
        np.testing.assert_almost_equal(self.mlon, 41.4069, decimal=4)
268
269
        mlt_args[-1] = -1.0
270
        self.mlon = aacgmv2._aacgmv2.inv_mlt_convert(*mlt_args)
271
        np.testing.assert_almost_equal(self.mlon, 11.4069, decimal=4)
272
273
        del mlt_args
274
275
    def test_inv_mlt_convert_yrsec(self):
276
        """Test MLT inversion with year and seconds of year"""
277
        dtime = dt.datetime(*self.long_date)
278
        soy = (int(dtime.strftime("%j"))-1) * 86400 + dtime.hour * 3600 + \
279
              dtime.minute * 60 + dtime.second
280
        
281
        mlt_args_1 = [dtime.year, soy, 12.0]
282
        mlt_args_2 = [dtime.year, soy, 25.0]
283
        mlt_args_3 = [dtime.year, soy, -1.0]
284
285
        mlon_1 = aacgmv2._aacgmv2.inv_mlt_convert_yrsec(*mlt_args_1)
286
        mlon_2 = aacgmv2._aacgmv2.inv_mlt_convert_yrsec(*mlt_args_2)
287
        mlon_3 = aacgmv2._aacgmv2.inv_mlt_convert_yrsec(*mlt_args_3)
288
289
        np.testing.assert_almost_equal(mlon_1, -153.5931, decimal=4)
290
        np.testing.assert_almost_equal(mlon_2, 41.4069, decimal=4)
291
        np.testing.assert_almost_equal(mlon_3, 11.4069, decimal=4)
292
293
        del dtime, soy, mlt_args_1, mlt_args_2, mlt_args_3, mlon_1, mlon_2
294
        del mlon_3
295
296 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...
297
        """Test MLT calculation with different longitudes"""
298
        mlt_args = list(self.long_date)
299
        mlt_args.extend([270.0])
300
        self.mlt = aacgmv2._aacgmv2.mlt_convert(*mlt_args)
301
        np.testing.assert_almost_equal(self.mlt, 16.2395, decimal=4)
302
303
        mlt_args[-1] = 80.0
304
        self.mlt = aacgmv2._aacgmv2.mlt_convert(*mlt_args)
305
        np.testing.assert_almost_equal(self.mlt, 3.5729, decimal=4)
306
307
        mlt_args[-1] = -90.0
308
        self.mlt = aacgmv2._aacgmv2.mlt_convert(*mlt_args)
309
        np.testing.assert_almost_equal(self.mlt, 16.2395, decimal=4)
310
311
        del mlt_args
312
313
    def test_mlt_convert_yrsec(self):
314
        """Test MLT calculation using year and seconds of year"""
315
        dtime = dt.datetime(*self.long_date)
316
        soy = (int(dtime.strftime("%j"))-1) * 86400 + dtime.hour * 3600 + \
317
            dtime.minute * 60 + dtime.second
318
        mlt_args_1 = [dtime.year, soy, 270.0]
319
        mlt_args_2 = [dtime.year, soy, 80.0]
320
        mlt_args_3 = [dtime.year, soy, -90.0]
321
        
322
        mlt_1 = aacgmv2._aacgmv2.mlt_convert_yrsec(*mlt_args_1)
323
        mlt_2 = aacgmv2._aacgmv2.mlt_convert_yrsec(*mlt_args_2)
324
        mlt_3 = aacgmv2._aacgmv2.mlt_convert_yrsec(*mlt_args_3)
325
326
        np.testing.assert_almost_equal(mlt_1, 16.2395, decimal=4)
327
        np.testing.assert_almost_equal(mlt_2, 3.5729, decimal=4)
328
        np.testing.assert_equal(mlt_1, mlt_3)
329
330
        del dtime, soy, mlt_args_1, mlt_args_2, mlt_args_3, mlt_1, mlt_2, mlt_3
331