Completed
Pull Request — master (#48)
by Angeline
02:22 queued 01:15
created

test_c_aacgmv2.TestCAACGMV2.test_convert_high()   A

Complexity

Conditions 1

Size

Total Lines 17
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 17
rs 9.6
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
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
        self.lat_comp = {'G2A': [48.1902, 58.2194], 'A2G': [30.7550, 50.4371],
23
                         'TG2A': [48.1954, 58.2189], 'TA2G': [30.7661, 50.4410]}
24
        self.lon_comp = {'G2A': [57.7505, 80.7282], 'A2G': [-94.1724, -77.5323],
25
                         'TG2A': [57.7456, 80.7362],
26
                         'TA2G': [-94.1727, -77.5440]}
27
        self.r_comp = {'G2A': [1.1775, 1.0457], 'A2G': [1133.6246, 305.7308],
28
                       'TG2A': [1.1775,  1.0457], 'TA2G': [1133.6282, 305.7322]}
29
30
    def teardown(self):
31
        """Runs after every method to clean up previous testing"""
32
        del self.date_args, self.long_date, self.mlat, self.mlon, self.mlt
33
        del self.lat_in, self.lon_in, self.alt_in, self.lat_comp, self.lon_comp
34
        del self.r_comp
35
36
    def test_constants(self):
37
        """Test module constants"""
38
        ans1 = aacgmv2._aacgmv2.G2A == 0
39
        ans2 = aacgmv2._aacgmv2.A2G == 1
40
        ans3 = aacgmv2._aacgmv2.TRACE == 2
41
        ans4 = aacgmv2._aacgmv2.ALLOWTRACE == 4
42
        ans5 = aacgmv2._aacgmv2.BADIDEA == 8
43
        ans6 = aacgmv2._aacgmv2.GEOCENTRIC == 16
44
45
        assert ans1 & ans2 & ans3 & ans4 & ans5 & ans6
46
        del ans1, ans2, ans3, ans4, ans5, ans6
47
48
    def test_set_datetime(self):
49
        """Test set_datetime"""
50
        for darg in self.date_args:
51
            arg1 = aacgmv2._aacgmv2.set_datetime(*darg) is None
52
            assert arg1
53
54
    @classmethod
55
    def test_fail_set_datetime(self):
56
        """Test unsuccessful set_datetime"""
57
        with pytest.raises(RuntimeError):
58
            aacgmv2._aacgmv2.set_datetime(1013, 1, 1, 0, 0, 0)
59
60 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...
61
        """Test convert from geographic to magnetic coordinates"""
62
        for i,darg in enumerate(self.date_args):
63
            aacgmv2._aacgmv2.set_datetime(*darg)
64
            (self.mlat, self.mlon,
65
             self.rshell) = aacgmv2._aacgmv2.convert(self.lat_in[i],
66
                                                     self.lon_in[i],
67
                                                     self.alt_in[i],
68
                                                     aacgmv2._aacgmv2.G2A)
69
            np.testing.assert_almost_equal(self.mlat, self.lat_comp['G2A'][i],
70
                                           decimal=4)
71
            np.testing.assert_almost_equal(self.mlon, self.lon_comp['G2A'][i],
72
                                           decimal=4)
73
            np.testing.assert_almost_equal(self.rshell, self.r_comp['G2A'][i],
74
                                           decimal=4)
75
76 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...
77
        """Test convert from magnetic to geodetic coordinates"""
78
        for i,darg in enumerate(self.date_args):
79
            aacgmv2._aacgmv2.set_datetime(*darg)
80
            (self.mlat, self.mlon,
81
             self.rshell) = aacgmv2._aacgmv2.convert(self.lat_in[i],
82
                                                     self.lon_in[i],
83
                                                     self.alt_in[i],
84
                                                     aacgmv2._aacgmv2.A2G)
85
            np.testing.assert_almost_equal(self.mlat, self.lat_comp['A2G'][i],
86
                                           decimal=4)
87
            np.testing.assert_almost_equal(self.mlon, self.lon_comp['A2G'][i],
88
                                           decimal=4)
89
            np.testing.assert_almost_equal(self.rshell, self.r_comp['A2G'][i],
90
                                           decimal=4)
91
92
    def test_convert_arr(self):
93
        """Test convert_arr using from magnetic to geodetic coordinates"""
94
        aacgmv2._aacgmv2.set_datetime(*self.date_args[0])
95
        (self.mlat, self.mlon, self.rshell,
96
         bad_ind) = aacgmv2._aacgmv2.convert_arr(self.lat_in, self.lon_in,
97
                                                 self.alt_in,
98
                                                 aacgmv2._aacgmv2.A2G)
99
100
        assert len(self.mlat) == len(self.lat_in)
101
        np.testing.assert_almost_equal(self.mlat[0], self.lat_comp['A2G'][0],
102
                                       decimal=4)
103
        np.testing.assert_almost_equal(self.mlon[0], self.lon_comp['A2G'][0],
104
                                       decimal=4)
105
        np.testing.assert_almost_equal(self.rshell[0], self.r_comp['A2G'][0],
106
                                       decimal=4)
107
        assert bad_ind[0] == -1
108
109
    def test_convert_G2A_TRACE(self):
110
        """Test convert from geodetic to magnetic coordinates using trace"""
111
        code = aacgmv2._aacgmv2.G2A + aacgmv2._aacgmv2.TRACE
112
113
        for i,dargs in enumerate(self.date_args):
114
            aacgmv2._aacgmv2.set_datetime(*dargs)
115
            (self.mlat, self.mlon,
116
             self.rshell) = aacgmv2._aacgmv2.convert(self.lat_in[i],
117
                                                     self.lon_in[i],
118
                                                     self.alt_in[i], code)
119
            np.testing.assert_almost_equal(self.mlat, self.lat_comp['TG2A'][i],
120
                                           decimal=4)
121
            np.testing.assert_almost_equal(self.mlon, self.lon_comp['TG2A'][i],
122
                                           decimal=4)
123
            np.testing.assert_almost_equal(self.rshell, self.r_comp['TG2A'][i],
124
                                           decimal=4)
125
126
        del code
127
128
    def test_convert_A2G_TRACE(self):
129
        """Test convert from magnetic to geodetic coordinates using trace"""
130
        code = aacgmv2._aacgmv2.A2G + aacgmv2._aacgmv2.TRACE
131
        
132
        for i,dargs in enumerate(self.date_args):
133
            aacgmv2._aacgmv2.set_datetime(*dargs)
134
            (self.mlat, self.mlon,
135
             self.rshell) = aacgmv2._aacgmv2.convert(self.lat_in[i],
136
                                                     self.lon_in[i],
137
                                                     self.alt_in[i], code)
138
            np.testing.assert_almost_equal(self.mlat, self.lat_comp['TA2G'][i],
139
                                           decimal=4)
140
            np.testing.assert_almost_equal(self.mlon, self.lon_comp['TA2G'][i],
141
                                           decimal=4)
142
            np.testing.assert_almost_equal(self.rshell, self.r_comp['TA2G'][i],
143
                                           decimal=4)
144
145
        del code
146
147
    def test_convert_high_denied(self):
148
        """Test for failure when converting to high altitude geodetic to
149
        magnetic coordinates"""
150
        aacgmv2._aacgmv2.set_datetime(*self.date_args[0])
151
        with pytest.raises(RuntimeError):
152
            aacgmv2._aacgmv2.convert(self.lat_in[0], self.lon_in[0], 5500,
153
                                     aacgmv2._aacgmv2.G2A)
154
155
    @pytest.mark.parametrize('code,lat_comp,lon_comp,r_comp',
156
                             [(aacgmv2._aacgmv2.G2A + aacgmv2._aacgmv2.TRACE,
157
                               59.9753, 57.7294, 1.8626),
158
                              (aacgmv2._aacgmv2.G2A
159
                               + aacgmv2._aacgmv2.ALLOWTRACE, 59.9753, 57.7294,
160
                               1.8626),
161
                             (aacgmv2._aacgmv2.G2A + aacgmv2._aacgmv2.BADIDEA,
162
                              58.7286, 56.4296, 1.8626)])
163
    def test_convert_high(self, code, lat_comp, lon_comp, r_comp):
164
        """Test convert from high altitude geodetic to magnetic coordinates"""
165
        aacgmv2._aacgmv2.set_datetime(*self.date_args[0])
166
        (self.mlat, self.mlon,
167
         self.rshell) = aacgmv2._aacgmv2.convert(self.lat_in[0], self.lon_in[0],
168
                                                 5500, code)
169
        np.testing.assert_almost_equal(self.mlat, lat_comp, decimal=4)
170
        np.testing.assert_almost_equal(self.mlon, lon_comp, decimal=4)
171
        np.testing.assert_almost_equal(self.rshell, r_comp, decimal=4)
172
173
    @pytest.mark.parametrize('code,lat_comp,lon_comp,r_comp',
174
                             [(aacgmv2._aacgmv2.G2A
175
                               + aacgmv2._aacgmv2.GEOCENTRIC, 48.3784, 57.7844,
176
                               1.1781),
177
                              (aacgmv2._aacgmv2.G2A
178
                               + aacgmv2._aacgmv2.GEOCENTRIC, 48.3784, 57.7844,
179
                               1.1781),
180
                              (aacgmv2._aacgmv2.A2G
181
                               + aacgmv2._aacgmv2.GEOCENTRIC, 30.6117, -94.1724,
182
                               1135.0000),
183
                              (aacgmv2._aacgmv2.G2A + aacgmv2._aacgmv2.TRACE +
184
                               aacgmv2._aacgmv2.GEOCENTRIC, 48.3836, 57.7793,
185
                               1.1781),
186
                              (aacgmv2._aacgmv2.A2G + aacgmv2._aacgmv2.TRACE +
187
                               aacgmv2._aacgmv2.GEOCENTRIC, 30.6227, -94.1727,
188
                               1135.0000)])
189
    def test_convert(self, code, lat_comp, lon_comp, r_comp):
190
        """Test convert for different code inputs"""
191
        aacgmv2._aacgmv2.set_datetime(*self.date_args[0])
192
        (self.mlat, self.mlon,
193
         self.rshell) = aacgmv2._aacgmv2.convert(self.lat_in[0], self.lon_in[0],
194
                                                 self.alt_in[0], code)
195
        np.testing.assert_almost_equal(self.mlat, lat_comp, decimal=4)
196
        np.testing.assert_almost_equal(self.mlon, lon_comp, decimal=4)
197
        np.testing.assert_almost_equal(self.rshell, r_comp, decimal=4)
198
199
    @classmethod
200
    def test_forbidden(self):
201
        """Test convert failure"""
202
        with pytest.raises(RuntimeError):
203
            aacgmv2._aacgmv2.convert(7, 0, 0, aacgmv2._aacgmv2.G2A)
204
205
    @pytest.mark.parametrize('marg,mlt_comp',
206
                             [(12.0, -153.6033), (25.0, 41.3967),
207
                              (-1.0, 11.3967)])
208
    def test_inv_mlt_convert(self, marg, mlt_comp):
209
        """Test MLT inversion"""
210
        mlt_args = list(self.long_date)
211
        mlt_args.append(marg)
212
        self.mlon = aacgmv2._aacgmv2.inv_mlt_convert(*mlt_args)
213
        np.testing.assert_almost_equal(self.mlon, mlt_comp, decimal=4)
214
215
        del mlt_args
216
217
    @pytest.mark.parametrize('marg,mlt_comp',
218
                             [(12.0, -153.6033), (25.0, 41.3967),
219
                              (-1.0, 11.3967)])
220
    def test_inv_mlt_convert_yrsec(self, marg, mlt_comp):
221
        """Test MLT inversion with year and seconds of year"""
222
        dtime = dt.datetime(*self.long_date)
223
        soy = (int(dtime.strftime("%j"))-1) * 86400 + dtime.hour * 3600 + \
224
              dtime.minute * 60 + dtime.second
225
        
226
        self.mlon = aacgmv2._aacgmv2.inv_mlt_convert_yrsec(dtime.year, soy,
227
                                                           marg)
228
229
        np.testing.assert_almost_equal(self.mlon, mlt_comp, decimal=4)
230
231
        del dtime, soy
232
233
    @pytest.mark.parametrize('marg,mlt_comp',
234
                             [(270.0, 16.2402), (80.0, 3.5736),
235
                              (-90.0, 16.2402)])
236
    def test_mlt_convert(self, marg, mlt_comp):
237
        """Test MLT calculation with different longitudes"""
238
        mlt_args = list(self.long_date)
239
        mlt_args.append(marg)
240
        self.mlt = aacgmv2._aacgmv2.mlt_convert(*mlt_args)
241
        np.testing.assert_almost_equal(self.mlt, mlt_comp, decimal=4)
242
243
    @pytest.mark.parametrize('marg,mlt_comp',
244
                             [(270.0, 16.2402), (80.0, 3.5736),
245
                              (-90.0, 16.2402)])
246
    def test_mlt_convert_yrsec(self, marg, mlt_comp):
247
        """Test MLT calculation using year and seconds of year"""
248
        dtime = dt.datetime(*self.long_date)
249
        soy = (int(dtime.strftime("%j"))-1) * 86400 + dtime.hour * 3600 + \
250
            dtime.minute * 60 + dtime.second
251
        
252
        self.mlt = aacgmv2._aacgmv2.mlt_convert_yrsec(dtime.year, soy, marg)
253
254
        np.testing.assert_almost_equal(self.mlt, mlt_comp, decimal=4)
255
256
        del dtime, soy
257