Passed
Pull Request — develop (#79)
by Angeline
01:19
created

test_c_aacgmv2.TestCAACGMV2.test_forbidden()   A

Complexity

Conditions 3

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 9
rs 10
c 0
b 0
f 0
cc 3
nop 1
1
import datetime as dt
2
import numpy as np
3
import pytest
4
5
import aacgmv2
6
7
8
class TestCAACGMV2(object):
9
    """Unit tests for the AACGMV2 wrapped C code."""
10
    def setup_method(self):
11
        """Run 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.bad_ind = None
18
        self.mlt = None
19
        self.lat_in = [45.5, 60]
20
        self.lon_in = [-23.5, 0]
21
        self.alt_in = [1135, 300]
22
        self.code = {'G2A': aacgmv2._aacgmv2.G2A, 'A2G': aacgmv2._aacgmv2.A2G,
23
                     'TG2A': aacgmv2._aacgmv2.G2A + aacgmv2._aacgmv2.TRACE,
24
                     'TA2G': aacgmv2._aacgmv2.A2G + aacgmv2._aacgmv2.TRACE}
25
        self.lat_comp = {'G2A': [48.1902, 58.2194], 'A2G': [30.7550, 50.4371],
26
                         'TG2A': [48.1954, 58.2189], 'TA2G': [30.7661, 50.4410]}
27
        self.lon_comp = {'G2A': [57.7505, 80.7282], 'A2G': [-94.1724, -77.5323],
28
                         'TG2A': [57.7456, 80.7362],
29
                         'TA2G': [-94.1727, -77.5440]}
30
        self.r_comp = {'G2A': [1.1775, 1.0457], 'A2G': [1133.6246, 305.7308],
31
                       'TG2A': [1.1775, 1.0457], 'TA2G': [1133.6282, 305.7322]}
32
33
    def teardown_method(self):
34
        """Run after every method to clean up previous testing."""
35
        del self.date_args, self.long_date, self.mlat, self.mlon, self.mlt
36
        del self.lat_in, self.lon_in, self.alt_in, self.lat_comp, self.lon_comp
37
        del self.r_comp, self.code, self.bad_ind
38
39
    @pytest.mark.parametrize('mattr,val', [(aacgmv2._aacgmv2.G2A, 0),
40
                                           (aacgmv2._aacgmv2.A2G, 1),
41
                                           (aacgmv2._aacgmv2.TRACE, 2),
42
                                           (aacgmv2._aacgmv2.ALLOWTRACE, 4),
43
                                           (aacgmv2._aacgmv2.BADIDEA, 8),
44
                                           (aacgmv2._aacgmv2.GEOCENTRIC, 16)])
45
    def test_constants(self, mattr, val):
46
        """Test module constants.
47
48
        Parameters
49
        ----------
50
        mattr : int
51
            Attribute holding an integer value
52
        val : int
53
            Expected integer value
54
55
        """
56
        np.testing.assert_equal(mattr, val)
57
58
    @pytest.mark.parametrize('idate', [0, 1])
59
    def test_set_datetime(self, idate):
60
        """Test set_datetime.
61
62
        Parameters
63
        ----------
64
        idate : int
65
            Integer date value
66
67
        """
68
        self.mlt = aacgmv2._aacgmv2.set_datetime(*self.date_args[idate])
69
        assert self.mlt is None
70
71
    def test_fail_set_datetime(self):
72
        """Test unsuccessful set_datetime."""
73
        self.long_date[0] = 1013
74
        with pytest.raises(RuntimeError) as rerr:
75
            aacgmv2._aacgmv2.set_datetime(*self.long_date)
76
77
        if str(rerr).find("AACGM_v2_SetDateTime returned error code -1") < 0:
78
            raise AssertionError('unknown error message: {:}'.format(str(rerr)))
79
80
    @pytest.mark.parametrize('idate,ckey', [(0, 'G2A'), (1, 'G2A'),
81
                                            (0, 'A2G'), (1, 'A2G'),
82
                                            (0, 'TG2A'), (1, 'TG2A'),
83
                                            (0, 'TA2G'), (1, 'TA2G')])
84
    def test_convert(self, idate, ckey):
85
        """Test convert from geographic to magnetic coordinates.
86
87
        Parameters
88
        ----------
89
        idate : int
90
            Integer date value
91
        ckey : str
92
            Transforming string combination
93
94
        """
95
        aacgmv2._aacgmv2.set_datetime(*self.date_args[idate])
96
        (self.mlat, self.mlon,
97
         self.rshell) = aacgmv2._aacgmv2.convert(self.lat_in[idate],
98
                                                 self.lon_in[idate],
99
                                                 self.alt_in[idate],
100
                                                 self.code[ckey])
101
        np.testing.assert_almost_equal(self.mlat, self.lat_comp[ckey][idate],
102
                                       decimal=4)
103
        np.testing.assert_almost_equal(self.mlon, self.lon_comp[ckey][idate],
104
                                       decimal=4)
105
        np.testing.assert_almost_equal(self.rshell, self.r_comp[ckey][idate],
106
                                       decimal=4)
107
108
    @pytest.mark.parametrize('ckey', ['G2A', 'A2G', 'TG2A', 'TA2G'])
109
    def test_convert_arr(self, ckey):
110
        """Test convert_arr using from magnetic to geodetic coordinates.
111
112
        Parameters
113
        ----------
114
        ckey : str
115
            Transforming string combination
116
117
        """
118
        aacgmv2._aacgmv2.set_datetime(*self.date_args[0])
119
        (self.mlat, self.mlon, self.rshell,
120
         self.bad_ind) = aacgmv2._aacgmv2.convert_arr(self.lat_in, self.lon_in,
121
                                                      self.alt_in,
122
                                                      self.code[ckey])
123
124
        np.testing.assert_equal(len(self.mlat), len(self.lat_in))
125
        np.testing.assert_almost_equal(self.mlat[0], self.lat_comp[ckey][0],
126
                                       decimal=4)
127
        np.testing.assert_almost_equal(self.mlon[0], self.lon_comp[ckey][0],
128
                                       decimal=4)
129
        np.testing.assert_almost_equal(self.rshell[0], self.r_comp[ckey][0],
130
                                       decimal=4)
131
        np.testing.assert_equal(self.bad_ind[0], -1)
132
133
    def test_forbidden(self):
134
        """Test convert failure."""
135
        self.lat_in[0] = 7
136
        with pytest.raises(RuntimeError) as rerr:
137
            aacgmv2._aacgmv2.convert(self.lat_in[0], self.lon_in[0], 0,
138
                                     aacgmv2._aacgmv2.G2A)
139
140
        if str(rerr).find("AACGM_v2_Convert returned error code -1") < 0:
141
            raise AssertionError('unknown error message: {:}'.format(str(rerr)))
142
143
    def test_convert_high_denied(self):
144
        """Test for failure when converting to high alt geod to mag coords."""
145
        aacgmv2._aacgmv2.set_datetime(*self.date_args[0])
146
        with pytest.raises(RuntimeError) as rerr:
147
            aacgmv2._aacgmv2.convert(self.lat_in[0], self.lon_in[0], 5500,
148
                                     aacgmv2._aacgmv2.G2A)
149
150
        if str(rerr).find("AACGM_v2_Convert returned error code -4") < 0:
151
            raise AssertionError('unknown error message: {:}'.format(str(rerr)))
152
153
    @pytest.mark.parametrize('code,lat_comp,lon_comp,r_comp',
154
                             [(aacgmv2._aacgmv2.G2A + aacgmv2._aacgmv2.TRACE,
155
                               59.9753, 57.7294, 1.8626),
156
                              (aacgmv2._aacgmv2.G2A
157
                               + aacgmv2._aacgmv2.ALLOWTRACE, 59.9753, 57.7294,
158
                               1.8626),
159
                              (aacgmv2._aacgmv2.G2A + aacgmv2._aacgmv2.BADIDEA,
160
                               58.7286, 56.4296, 1.8626)])
161
    def test_convert_high(self, code, lat_comp, lon_comp, r_comp):
162
        """Test convert from high altitude geodetic to magnetic coordinates.
163
164
        Parameters
165
        ----------
166
        code : int
167
            Integer code value
168
        lat_comp : float
169
            Comparison latitude in degrees N
170
        lon_comp : float
171
            Comparison longitude in degrees E
172
        r_comp : float
173
            Comparison radius in Earth Radii.
174
175
        """
176
        aacgmv2._aacgmv2.set_datetime(*self.date_args[0])
177
        (self.mlat, self.mlon,
178
         self.rshell) = aacgmv2._aacgmv2.convert(self.lat_in[0], self.lon_in[0],
179
                                                 5500, code)
180
        np.testing.assert_almost_equal(self.mlat, lat_comp, decimal=4)
181
        np.testing.assert_almost_equal(self.mlon, lon_comp, decimal=4)
182
        np.testing.assert_almost_equal(self.rshell, r_comp, decimal=4)
183
184
    @pytest.mark.parametrize('code,lat_comp,lon_comp,r_comp',
185
                             [(aacgmv2._aacgmv2.G2A
186
                               + aacgmv2._aacgmv2.GEOCENTRIC, 48.3784, 57.7844,
187
                               1.1781),
188
                              (aacgmv2._aacgmv2.G2A
189
                               + aacgmv2._aacgmv2.GEOCENTRIC, 48.3784, 57.7844,
190
                               1.1781),
191
                              (aacgmv2._aacgmv2.A2G
192
                               + aacgmv2._aacgmv2.GEOCENTRIC, 30.6117, -94.1724,
193
                               1135.0000),
194
                              (aacgmv2._aacgmv2.G2A + aacgmv2._aacgmv2.TRACE
195
                               + aacgmv2._aacgmv2.GEOCENTRIC, 48.3836, 57.7793,
196
                               1.1781),
197
                              (aacgmv2._aacgmv2.A2G + aacgmv2._aacgmv2.TRACE
198
                               + aacgmv2._aacgmv2.GEOCENTRIC, 30.6227, -94.1727,
199
                               1135.0000)])
200
    def test_convert_geocentric(self, code, lat_comp, lon_comp, r_comp):
201
        """Test convert for different code inputs with geocentric coords.
202
203
        Parameters
204
        ----------
205
        code : int
206
            Integer code value
207
        lat_comp : float
208
            Comparison latitude in degrees N
209
        lon_comp : float
210
            Comparison longitude in degrees E
211
        r_comp : float
212
            Comparison radius in Earth Radii.
213
214
        """
215
        aacgmv2._aacgmv2.set_datetime(*self.date_args[0])
216
        (self.mlat, self.mlon,
217
         self.rshell) = aacgmv2._aacgmv2.convert(self.lat_in[0], self.lon_in[0],
218
                                                 self.alt_in[0], code)
219
        np.testing.assert_almost_equal(self.mlat, lat_comp, decimal=4)
220
        np.testing.assert_almost_equal(self.mlon, lon_comp, decimal=4)
221
        np.testing.assert_almost_equal(self.rshell, r_comp, decimal=4)
222
223
    @pytest.mark.parametrize('marg,mlt_comp',
224
                             [(12.0, -153.6033), (25.0, 41.3967),
225
                              (-1.0, 11.3967)])
226
    def test_inv_mlt_convert(self, marg, mlt_comp):
227
        """Test MLT inversion.
228
229
        Parameters
230
        ----------
231
        marg : float
232
            Input argument
233
        mlt_comp : float
234
            Expected output
235
236
        """
237
        self.long_date = list(self.long_date)
238
        self.long_date.append(marg)
239
        self.mlon = aacgmv2._aacgmv2.inv_mlt_convert(*self.long_date)
240
        np.testing.assert_almost_equal(self.mlon, mlt_comp, decimal=4)
241
242
    def test_inv_mlt_convert_arr(self):
243
        """Test array MLT inversion."""
244
        self.date_args = [[ldate for j in range(3)] for ldate in self.long_date]
245
        self.mlt = [12.0, 25.0, -1.0]
246
        self.lon_in = [-153.6033, 41.3967, 11.3967]
247
        self.mlon = aacgmv2._aacgmv2.inv_mlt_convert_arr(*self.date_args,
248
                                                         self.mlt)
249
        np.testing.assert_almost_equal(self.mlon, self.lon_in, decimal=4)
250
251
    @pytest.mark.parametrize('marg,mlt_comp',
252
                             [(12.0, -153.6033), (25.0, 41.3967),
253
                              (-1.0, 11.3967)])
254
    def test_inv_mlt_convert_yrsec(self, marg, mlt_comp):
255
        """Test MLT inversion with year and seconds of year.
256
257
        Parameters
258
        ----------
259
        marg : float
260
            Input argument
261
        mlt_comp : float
262
            Expected output
263
264
        """
265
        dtime = dt.datetime(*self.long_date)
266
        soy = (int(dtime.strftime("%j")) - 1) * 86400 + dtime.hour * 3600 \
267
            + dtime.minute * 60 + dtime.second
268
269
        self.mlon = aacgmv2._aacgmv2.inv_mlt_convert_yrsec(dtime.year, soy,
270
                                                           marg)
271
272
        np.testing.assert_almost_equal(self.mlon, mlt_comp, decimal=4)
273
274
        del dtime, soy
275
276
    @pytest.mark.parametrize('marg,mlt_comp',
277
                             [(270.0, 16.2402), (80.0, 3.5736),
278
                              (-90.0, 16.2402)])
279
    def test_mlt_convert(self, marg, mlt_comp):
280
        """Test MLT calculation with different longitudes.
281
282
        Parameters
283
        ----------
284
        marg : float
285
            Input argument
286
        mlt_comp : float
287
            Expected output
288
289
        """
290
        mlt_args = list(self.long_date)
291
        mlt_args.append(marg)
292
        self.mlt = aacgmv2._aacgmv2.mlt_convert(*mlt_args)
293
        np.testing.assert_almost_equal(self.mlt, mlt_comp, decimal=4)
294
295
    def test_mlt_convert_arr(self):
296
        """Test array MLT conversion."""
297
        self.date_args = [[ldate for j in range(3)] for ldate in self.long_date]
298
        self.mlon = [-153.6033, 41.3967, 11.3967]
299
        self.lon_in = [12.0, 1.0, 23.0]
300
        self.mlt = aacgmv2._aacgmv2.mlt_convert_arr(*self.date_args, self.mlon)
301
        np.testing.assert_almost_equal(self.mlt, self.lon_in, decimal=4)
302
303
    @pytest.mark.parametrize('marg,mlt_comp',
304
                             [(270.0, 16.2402), (80.0, 3.5736),
305
                              (-90.0, 16.2402)])
306
    def test_mlt_convert_yrsec(self, marg, mlt_comp):
307
        """Test MLT calculation using year and seconds of year.
308
309
        Parameters
310
        ----------
311
        marg : float
312
            Input argument
313
        mlt_comp : float
314
            Expected output
315
316
        """
317
        dtime = dt.datetime(*self.long_date)
318
        soy = (int(dtime.strftime("%j")) - 1) * 86400 + dtime.hour * 3600 \
319
            + dtime.minute * 60 + dtime.second
320
321
        self.mlt = aacgmv2._aacgmv2.mlt_convert_yrsec(dtime.year, soy, marg)
322
323
        np.testing.assert_almost_equal(self.mlt, mlt_comp, decimal=4)
324
325
        del dtime, soy
326