Passed
Pull Request — develop (#77)
by Angeline
01:21
created

TestCAACGMV2.test_convert_high_denied()   A

Complexity

Conditions 2

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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