Test Failed
Pull Request — master (#9)
by Angeline
03:32
created

TestCAACGMV2.test_module_structure()   C

Complexity

Conditions 9

Size

Total Lines 10
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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