Completed
Push — develop ( 04a2cb...7e7468 )
by Angeline
12s queued 10s
created

TestPyAACGMV2.test_module_structure()   A

Complexity

Conditions 1

Size

Total Lines 13
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 13
rs 9.8
c 0
b 0
f 0
cc 1
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 TestConvertLatLon:
10
    def setup(self):
11
        """Runs before every method to create a clean testing setup"""
12
        self.dtime = dt.datetime(2015, 1, 1, 0, 0, 0)
13
        self.ddate = dt.date(2015, 1, 1)
14
        self.lat_out = None
15
        self.lon_out = None
16
        self.r_out = None
17
18
    def teardown(self):
19
        """Runs after every method to clean up previous testing"""
20
        del self.lat_out, self.lon_out, self.r_out, self.dtime, self.ddate
21
22
    def test_convert_latlon(self):
23
        """Test single value latlon conversion"""
24
        (self.lat_out, self.lon_out,
25
         self.r_out) = aacgmv2.convert_latlon(60, 0, 300, self.dtime)
26
        np.testing.assert_almost_equal(self.lat_out, 58.2258, decimal=4)
27
        np.testing.assert_almost_equal(self.lon_out, 81.1685, decimal=4)
28
        np.testing.assert_almost_equal(self.r_out, 1.0457, decimal=4)
29
30
    def test_convert_latlon_badidea(self):
31
        """Test single value latlon conversion with a bad flag"""
32
        code = "G2A | BADIDEA"
33
        (self.lat_out, self.lon_out,
34
         self.r_out) = aacgmv2.convert_latlon(60, 0, 3000, self.dtime, code)
35
        np.testing.assert_almost_equal(self.lat_out, 64.3568, decimal=4)
36
        np.testing.assert_almost_equal(self.lon_out, 83.3027, decimal=4)
37
        np.testing.assert_almost_equal(self.r_out, 1.4694, decimal=4)
38
39
        del code
40
41
    def test_convert_latlon_location_failure(self):
42
        """Test single value latlon conversion with a bad location"""
43
        (self.lat_out, self.lon_out,
44
         self.r_out) = aacgmv2.convert_latlon(0, 0, 0, self.dtime)
45
        if not (np.isnan(self.lat_out) & np.isnan(self.lon_out) &
46
                np.isnan(self.r_out)):
47
            raise AssertionError()
48
49
    def test_convert_latlon_time_failure(self):
50
        """Test single value latlon conversion with a bad datetime"""
51
        with pytest.raises(AssertionError):
52
            (self.lat_out, self.lon_out,
53
             self.r_out) = aacgmv2.convert_latlon(60, 0, 300, None)
54
55
    def test_convert_latlon_datetime_date(self):
56
        """Test single latlon conversion with date and datetime input"""
57
        (self.lat_out, self.lon_out,
58
         self.r_out) = aacgmv2.convert_latlon(60, 0, 300, self.ddate)
59
        lat_2, lon_2, r_2 = aacgmv2.convert_latlon(60, 0, 300, self.dtime)
60
61
        if self.lat_out != lat_2:
62
            raise AssertionError()
63
        if self.lon_out != lon_2:
64
            raise AssertionError()
65
        if self.r_out != r_2:
66
            raise AssertionError()
67
68
        del lat_2, lon_2, r_2
69
70
    def test_warning_below_ground_convert_latlon(self):
71
        """ Test that a warning is issued if altitude is below zero"""
72
        import logbook
73
        lwarn = u"conversion not intended for altitudes < 0 km"
74
75
        with logbook.TestHandler() as handler:
76
            (self.lat_out, self.lon_out,
77
             self.r_out) = aacgmv2.convert_latlon(60, 0, -1, self.dtime)
78
            if not handler.has_warning(lwarn):
79
                raise AssertionError()
80
81
        handler.close()
82
83
    def test_convert_latlon_maxalt_failure(self):
84
        """For a single value, test failure for an altitude too high for
85
        coefficients"""
86
        (self.lat_out, self.lon_out,
87
         self.r_out) = aacgmv2.convert_latlon(60, 0, 2001, self.dtime)
88
        if not (np.isnan(self.lat_out) & np.isnan(self.lon_out) &
89
                np.isnan(self.r_out)):
90
            raise AssertionError()
91
92
    def test_convert_latlon_lat_failure(self):
93
        """Test error return for co-latitudes above 90 for a single value"""
94
        with pytest.raises(AssertionError):
95
            aacgmv2.convert_latlon(91, 0, 300, self.dtime)
96
97
        with pytest.raises(AssertionError):
98
            aacgmv2.convert_latlon(-91, 0, 300, self.dtime)
99
100
class TestConvertLatLonArr:
101
    def setup(self):
102
        """Runs before every method to create a clean testing setup"""
103
        self.dtime = dt.datetime(2015, 1, 1, 0, 0, 0)
104
        self.ddate = dt.date(2015, 1, 1)
105
        self.lat_out = None
106
        self.lon_out = None
107
        self.r_out = None
108
109
    def teardown(self):
110
        """Runs after every method to clean up previous testing"""
111
        del self.lat_out, self.lon_out, self.r_out, self.dtime, self.ddate
112
113
    def test_convert_latlon_arr_single_val(self):
114
        """Test array latlon conversion for a single value"""
115
        (self.lat_out, self.lon_out,
116
         self.r_out) = aacgmv2.convert_latlon_arr(60, 0, 300, self.dtime)
117
118
        if not isinstance(self.lat_out, np.ndarray):
119
            raise AssertionError()
120
        if not isinstance(self.lon_out, np.ndarray):
121
            raise AssertionError()
122
        if not isinstance(self.r_out, np.ndarray):
123
            raise AssertionError()
124
        if not (self.r_out.shape == self.lon_out.shape and
125
                self.lat_out.shape == self.r_out.shape and
126
                self.r_out.shape == (1,)):
127
            raise AssertionError()
128
129
        np.testing.assert_allclose(self.lat_out, [58.2257709], rtol=1e-4)
130
        np.testing.assert_allclose(self.lon_out, [81.16846959], rtol=1e-4)
131
        np.testing.assert_allclose(self.r_out, [1.04566346], rtol=1e-4)
132
133 View Code Duplication
    def test_convert_latlon_arr_list_single(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
134
        """Test array latlon conversion for list input of single values"""
135
        (self.lat_out, self.lon_out,
136
         self.r_out) = aacgmv2.convert_latlon_arr([60], [0], [300], self.dtime)
137
138
        if not isinstance(self.lat_out, np.ndarray):
139
            raise AssertionError()
140
        if not isinstance(self.lon_out, np.ndarray):
141
            raise AssertionError()
142
        if not isinstance(self.r_out, np.ndarray):
143
            raise AssertionError()
144
        if not (self.r_out.shape == self.lon_out.shape and
145
                self.lat_out.shape == self.r_out.shape and
146
                self.r_out.shape == (1,)):
147
            raise AssertionError()
148
149
        np.testing.assert_allclose(self.lat_out, [58.2257709], rtol=1e-4)
150
        np.testing.assert_allclose(self.lon_out, [81.16846959], rtol=1e-4)
151
        np.testing.assert_allclose(self.r_out, [1.04566346], rtol=1e-4)
152
153 View Code Duplication
    def test_convert_latlon_arr_list(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
154
        """Test array latlon conversion for list input"""
155
        (self.lat_out, self.lon_out,
156
         self.r_out) = aacgmv2.convert_latlon_arr([60, 61], [0, 0], [300, 300],
157
                                                  self.dtime)
158
159
        if not isinstance(self.lat_out, np.ndarray):
160
            raise AssertionError()
161
        if not isinstance(self.lon_out, np.ndarray):
162
            raise AssertionError()
163
        if not isinstance(self.r_out, np.ndarray):
164
            raise AssertionError()
165
        if not (self.r_out.shape == self.lon_out.shape and
166
                self.lat_out.shape == self.r_out.shape and
167
                self.r_out.shape == (2,)):
168
            raise AssertionError()
169
170
        np.testing.assert_allclose(self.lat_out, [58.22577090, 59.31860933],
171
                                   rtol=1e-4)
172
        np.testing.assert_allclose(self.lon_out, [81.16846959, 81.61398933],
173
                                   rtol=1e-4)
174
        np.testing.assert_allclose(self.r_out, [1.04566346, 1.04561304],
175
                                   rtol=1e-4)
176
177 View Code Duplication
    def test_convert_latlon_arr_arr_single(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
178
        """Test array latlon conversion for array input of shape (1,)"""
179
        (self.lat_out, self.lon_out,
180
        self.r_out) = aacgmv2.convert_latlon_arr(np.array([60]), np.array([0]),
181
                                                 np.array([300]), self.dtime)
182
183
        if not isinstance(self.lat_out, np.ndarray):
184
            raise AssertionError()
185
        if not isinstance(self.lon_out, np.ndarray):
186
            raise AssertionError()
187
        if not isinstance(self.r_out, np.ndarray):
188
            raise AssertionError()
189
        if not (self.r_out.shape == self.lon_out.shape and
190
                self.lat_out.shape == self.r_out.shape and
191
                self.r_out.shape == (1,)):
192
            raise AssertionError()
193
194
        np.testing.assert_allclose(self.lat_out, [58.2257709], rtol=1e-4)
195
        np.testing.assert_allclose(self.lon_out, [81.16846959], rtol=1e-4)
196
        np.testing.assert_allclose(self.r_out, [1.04566346], rtol=1e-4)
197
198 View Code Duplication
    def test_convert_latlon_arr_arr(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
199
        """Test array latlon conversion for array input"""
200
        (self.lat_out, self.lon_out,
201
         self.r_out) = aacgmv2.convert_latlon_arr(np.array([60, 61]),
202
                                                  np.array([0, 0]),
203
                                                  np.array([300, 300]),
204
                                                  self.dtime)
205
206
        if not isinstance(self.lat_out, np.ndarray):
207
            raise AssertionError()
208
        if not isinstance(self.lon_out, np.ndarray):
209
            raise AssertionError()
210
        if not isinstance(self.r_out, np.ndarray):
211
            raise AssertionError()
212
        if not (self.r_out.shape == self.lon_out.shape and
213
                self.lat_out.shape == self.r_out.shape and
214
                self.r_out.shape == (2,)):
215
            raise AssertionError()
216
217
        np.testing.assert_allclose(self.lat_out, [58.22577090, 59.31860933],
218
                                   rtol=1e-4)
219
        np.testing.assert_allclose(self.lon_out, [81.16846959, 81.61398933],
220
                                   rtol=1e-4)
221
        np.testing.assert_allclose(self.r_out, [1.04566346, 1.04561304],
222
                                   rtol=1e-4)
223
224
    def test_convert_latlon_arr_list_mix(self):
225
        """Test array latlon conversion for mixed types with list"""
226
        (self.lat_out, self.lon_out,
227
        self.r_out) = aacgmv2.convert_latlon_arr([60, 61], 0, 300, self.dtime)
228
229
        if not isinstance(self.lat_out, np.ndarray):
230
            raise AssertionError()
231
        if not isinstance(self.lon_out, np.ndarray):
232
            raise AssertionError()
233
        if not isinstance(self.r_out, np.ndarray):
234
            raise AssertionError()
235
        if not (self.r_out.shape == self.lon_out.shape and
236
                self.lat_out.shape == self.r_out.shape and
237
                self.r_out.shape == (2,)):
238
            raise AssertionError()
239
240
        np.testing.assert_allclose(self.lat_out, [58.22577090, 59.31860933],
241
                                   rtol=1e-4)
242
        np.testing.assert_allclose(self.lon_out, [81.16846959, 81.61398933],
243
                                   rtol=1e-4)
244
        np.testing.assert_allclose(self.r_out, [1.04566346, 1.04561304],
245
                                   rtol=1e-4)
246
247 View Code Duplication
    def test_convert_latlon_arr_arr_mix(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
248
        """Test array latlon conversion for mixed type with an array"""
249
        (self.lat_out, self.lon_out,
250
        self.r_out) = aacgmv2.convert_latlon_arr(np.array([60, 61]), 0,
251
                                                 300, self.dtime)
252
253
        if not isinstance(self.lat_out, np.ndarray):
254
            raise AssertionError()
255
        if not isinstance(self.lon_out, np.ndarray):
256
            raise AssertionError()
257
        if not isinstance(self.r_out, np.ndarray):
258
            raise AssertionError()
259
        if not (self.r_out.shape == self.lon_out.shape and
260
                self.lat_out.shape == self.r_out.shape and
261
                self.r_out.shape == (2,)):
262
            raise AssertionError()
263
264
        np.testing.assert_allclose(self.lat_out, [58.22577090, 59.31860933],
265
                                   rtol=1e-4)
266
        np.testing.assert_allclose(self.lon_out, [81.16846959, 81.61398933],
267
                                   rtol=1e-4)
268
        np.testing.assert_allclose(self.r_out, [1.04566346, 1.04561304],
269
                                   rtol=1e-4)
270
271 View Code Duplication
    def test_convert_latlon_arr_mult_arr_mix(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
272
        """Test array latlon conversion for mix type with multi-dim array"""
273
        (self.lat_out, self.lon_out,
274
        self.r_out) = aacgmv2.convert_latlon_arr(np.array([[60, 61, 62],
275
                                                           [63, 64, 65]]),
276
                                                 0, 300, self.dtime)
277
278
        if not isinstance(self.lat_out, np.ndarray):
279
            raise AssertionError()
280
        if not isinstance(self.lon_out, np.ndarray):
281
            raise AssertionError()
282
        if not isinstance(self.r_out, np.ndarray):
283
            raise AssertionError()
284
        if not (self.r_out.shape == self.lon_out.shape and
285
                self.lat_out.shape == self.r_out.shape and
286
                self.r_out.shape == (2, 3)):
287
            raise AssertionError()
288
289
        np.testing.assert_allclose(self.lat_out,
290
                                   [[58.2257709, 59.3186093, 60.4039740],
291
                                    [61.4819893, 62.5527635, 63.6163840]],
292
                                   rtol=1e-4)
293
        np.testing.assert_allclose(self.lon_out,
294
                                   [[81.1684696, 81.6139893, 82.0871880],
295
                                    [82.5909499, 83.1285895, 83.7039272]],
296
                                   rtol=1e-4)
297
        np.testing.assert_allclose(self.r_out,
298
                                   [[1.04566346, 1.04561304, 1.04556369],
299
                                    [1.04551548, 1.04546847, 1.04542272]],
300
                                   rtol=1e-4)
301
302 View Code Duplication
    def test_convert_latlon_arr_mult_arr_unequal(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
303
        """Test array latlon conversion for unequal sized multi-dim array"""
304
        (self.lat_out, self.lon_out,
305
         self.r_out) = aacgmv2.convert_latlon_arr(np.array([[60, 61, 62],
306
                                                            [63, 64, 65]]),
307
                                                  np.array([0]),
308
                                                  np.array([300]), self.dtime)
309
310
        if not isinstance(self.lat_out, np.ndarray):
311
            raise AssertionError()
312
        if not isinstance(self.lon_out, np.ndarray):
313
            raise AssertionError()
314
        if not isinstance(self.r_out, np.ndarray):
315
            raise AssertionError()
316
        if not (self.r_out.shape == self.lon_out.shape and
317
                self.lat_out.shape == self.r_out.shape and
318
                self.r_out.shape == (2, 3)):
319
            raise AssertionError()
320
321
        np.testing.assert_allclose(self.lat_out,
322
                                   [[58.2257709, 59.3186093, 60.4039740],
323
                                    [61.4819893, 62.5527635, 63.6163840]],
324
                                   rtol=1e-4)
325
        np.testing.assert_allclose(self.lon_out,
326
                                   [[81.1684696, 81.6139893, 82.0871880],
327
                                    [82.5909499, 83.1285895, 83.7039272]],
328
                                   rtol=1e-4)
329
        np.testing.assert_allclose(self.r_out,
330
                                   [[1.04566346, 1.04561304, 1.04556369],
331
                                    [1.04551548, 1.04546847, 1.04542272]],
332
                                   rtol=1e-4)
333
334
    def test_convert_latlon_arr_badidea(self):
335
        """Test array latlon conversion for BADIDEA"""
336
        code = "G2A | BADIDEA"
337
        (self.lat_out, self.lon_out,
338
         self.r_out) = aacgmv2.convert_latlon_arr([60], [0], [3000],
339
                                                  self.dtime, code)
340
341
        np.testing.assert_allclose(self.lat_out, [64.35677791], rtol=1e-4)
342
        np.testing.assert_allclose(self.lon_out, [83.30272053], rtol=1e-4)
343
        np.testing.assert_allclose(self.r_out, [1.46944431], rtol=1e-4)
344
345
    def test_convert_latlon_arr_location_failure(self):
346
        """Test array latlon conversion with a bad location"""
347
        (self.lat_out, self.lon_out,
348
         self.r_out) = aacgmv2.convert_latlon_arr([0], [0], [0], self.dtime)
349
350
        if not isinstance(self.lat_out, np.ndarray):
351
            raise AssertionError()
352
        if not isinstance(self.lon_out, np.ndarray):
353
            raise AssertionError()
354
        if not isinstance(self.r_out, np.ndarray):
355
            raise AssertionError()
356
        if not (self.r_out.shape == self.lon_out.shape and
357
                self.lat_out.shape == self.r_out.shape and
358
                self.r_out.shape == (1,)):
359
            raise AssertionError()
360
        if not np.all([np.isnan(self.lat_out), np.isnan(self.lon_out),
361
                       np.isnan(self.r_out)]):
362
            raise AssertionError()
363
364
    def test_convert_latlon_arr_time_failure(self):
365
        """Test array latlon conversion with a bad time"""
366
        with pytest.raises(AssertionError):
367
            (self.lat_out, self.lon_out,
368
             self.r_out) = aacgmv2.convert_latlon_arr([60], [0], [300], None)
369
370
    def test_convert_latlon_arr_datetime_date(self):
371
        """Test array latlon conversion with date and datetime input"""
372
        (self.lat_out, self.lon_out,
373
         self.r_out) = aacgmv2.convert_latlon_arr([60], [0], [300], self.ddate)
374
        lat_2, lon_2, r_2 = aacgmv2.convert_latlon_arr([60], [0], [300],
375
                                                       self.dtime)
376
        if self.lat_out != lat_2:
377
            raise AssertionError()
378
        if self.lon_out != lon_2:
379
            raise AssertionError()
380
        if self.r_out != r_2:
381
            raise AssertionError()
382
383
        del lat_2, lon_2, r_2
384
385
    def test_warning_below_ground_convert_latlon_arr(self):
386
        """ Test that a warning is issued if altitude is below zero"""
387
        import logbook
388
        lwarn = u"conversion not intended for altitudes < 0 km"
389
390
        with logbook.TestHandler() as handler:
391
            (self.lat_out, self.lon_out,
392
             self.r_out) = aacgmv2.convert_latlon_arr([60], [0], [-1],
393
                                                      self.dtime)
394
            if not handler.has_warning(lwarn):
395
                raise AssertionError()
396
397
        handler.close()
398
399
    def test_convert_latlon_arr_maxalt_failure(self):
400
        """For an array, test failure for an altitude too high for
401
        coefficients"""
402
        (self.lat_out, self.lon_out,
403
         self.r_out) = aacgmv2.convert_latlon_arr([60], [0], [2001], self.dtime)
404
        if not np.all([np.isnan(self.lat_out), np.isnan(self.lon_out),
405
                       np.isnan(self.r_out)]):
406
            raise AssertionError()
407
408
    def test_convert_latlon_arr_lat_failure(self):
409
        """Test error return for co-latitudes above 90 for an array"""
410
        with pytest.raises(AssertionError):
411
            aacgmv2.convert_latlon_arr([91, 60, -91], 0, 300, self.dtime)
412
413
class TestGetAACGMCoord:
414
    def setup(self):
415
        """Runs before every method to create a clean testing setup"""
416
        self.dtime = dt.datetime(2015, 1, 1, 0, 0, 0)
417
        self.ddate = dt.date(2015, 1, 1)
418
        self.mlat_out = None
419
        self.mlon_out = None
420
        self.mlt_out = None
421
422
    def teardown(self):
423
        """Runs after every method to clean up previous testing"""
424
        del self.mlat_out, self.mlon_out, self.mlt_out, self.dtime, self.ddate
425
426
    def test_get_aacgm_coord(self):
427
        """Test single value AACGMV2 calculation, defaults to TRACE"""
428
        (self.mlat_out, self.mlon_out,
429
         self.mlt_out) = aacgmv2.get_aacgm_coord(60, 0, 300, self.dtime)
430
431
        np.testing.assert_almost_equal(self.mlat_out, 58.2247, decimal=4)
432
        np.testing.assert_almost_equal(self.mlon_out, 81.1761, decimal=4)
433
        np.testing.assert_almost_equal(self.mlt_out, 0.1889, decimal=4)
434
435
    def test_get_aacgm_coord_badidea(self):
436
        """Test single value AACGMV2 calculation with a bad flag"""
437
        method = "BADIDEA"
438
        (self.mlat_out, self.mlon_out,
439
         self.mlt_out) = aacgmv2.get_aacgm_coord(60, 0, 3000, self.dtime,
440
                                                 method=method)
441
442
        np.testing.assert_almost_equal(self.mlat_out, 64.3568, decimal=4)
443
        np.testing.assert_almost_equal(self.mlon_out, 83.3027, decimal=4)
444
        np.testing.assert_almost_equal(self.mlt_out, 0.3307, decimal=4)
445
        del method
446
447
    def test_get_aacgm_coord_location_failure(self):
448
        """Test single value AACGMV2 calculation with a bad location"""
449
        (self.mlat_out, self.mlon_out,
450
         self.mlt_out) = aacgmv2.get_aacgm_coord(0, 0, 0, self.dtime)
451
        if not (np.isnan(self.mlat_out) & np.isnan(self.mlon_out) &
452
                np.isnan(self.mlt_out)):
453
            raise AssertionError()
454
455
    def test_get_aacgm_coord_time_failure(self):
456
        """Test single value AACGMV2 calculation with a bad datetime"""
457
        with pytest.raises(AssertionError):
458
            (self.mlat_out, self.mlon_out,
459
             self.mlt_out) = aacgmv2.get_aacgm_coord(60, 0, 300, None)
460
461
    def test_get_aacgm_coord_datetime_date(self):
462
        """Test single AACGMV2 calculation with date and datetime input"""
463
        (self.mlat_out, self.mlon_out,
464
         self.mlt_out) = aacgmv2.get_aacgm_coord(60, 0, 300, self.ddate)
465
        mlat_2, mlon_2, mlt_2 = aacgmv2.get_aacgm_coord(60, 0, 300, self.dtime)
466
467
        np.testing.assert_almost_equal(self.mlat_out, mlat_2, decimal=6)
468
        np.testing.assert_almost_equal(self.mlon_out, mlon_2, decimal=6)
469
        np.testing.assert_almost_equal(self.mlt_out, mlt_2, decimal=6)
470
471
        del mlat_2, mlon_2, mlt_2
472
473
    def test_warning_below_ground_get_aacgm_coord(self):
474
        """ Test that a warning is issued if altitude is below zero"""
475
        import logbook
476
        lwarn = u"conversion not intended for altitudes < 0 km"
477
478
        with logbook.TestHandler() as handler:
479
            (self.mlat_out, self.mlon_out,
480
             self.mlt_out) = aacgmv2.get_aacgm_coord(60, 0, -1, self.dtime)
481
            if not handler.has_warning(lwarn):
482
                raise AssertionError()
483
484
        handler.close()
485
486
    def test_get_aacgm_coord_maxalt_failure(self):
487
        """For a single value, test failure for an altitude too high for
488
        coefficients"""
489
        method = ""
490
        (self.mlat_out, self.mlon_out,
491
         self.mlt_out) = aacgmv2.get_aacgm_coord(60, 0, 2001, self.dtime,
492
                                                 method=method)
493
        if not (np.isnan(self.mlat_out) & np.isnan(self.mlon_out) &
494
                np.isnan(self.mlt_out)):
495
            raise AssertionError()
496
497
    def test_get_aacgm_coord_mlat_failure(self):
498
        """Test error return for co-latitudes above 90 for a single value"""
499
        import logbook
500
        lerr = u"unrealistic latitude"
501
502
        with logbook.TestHandler() as hhigh:
503
            with pytest.raises(AssertionError):
504
                (self.mlat_out, self.mlon_out,
505
                 self.mlt_out) = aacgmv2.get_aacgm_coord(91, 0, 300, self.dtime)
506
                if not hhigh.has_error(lerr):
507
                    raise AssertionError()
508
509
        with logbook.TestHandler() as hlow:
510
            with pytest.raises(AssertionError):
511
                (self.mlat_out, self.mlon_out,
512
                 self.mlt_out) = aacgmv2.get_aacgm_coord(-91, 0, 300,
513
                                                         self.dtime)
514
                if not hlow.has_error(lerr):
515
                    raise AssertionError()
516
517
        hhigh.close()
518
        hlow.close()
519
520
class TestGetAACGMCoordArr:
521
    def setup(self):
522
        """Runs before every method to create a clean testing setup"""
523
        self.dtime = dt.datetime(2015, 1, 1, 0, 0, 0)
524
        self.ddate = dt.date(2015, 1, 1)
525
        self.mlat_out = None
526
        self.mlon_out = None
527
        self.mlt_out = None
528
529
    def teardown(self):
530
        """Runs after every method to clean up previous testing"""
531
        del self.mlat_out, self.mlon_out, self.mlt_out, self.dtime, self.ddate
532
533
    def test_get_aacgm_coord_arr_single_val(self):
534
        """Test array AACGMV2 calculation for a single value"""
535
        (self.mlat_out, self.mlon_out,
536
         self.mlt_out) = aacgmv2.get_aacgm_coord_arr(60, 0, 300, self.dtime)
537
538
        if not isinstance(self.mlat_out, np.ndarray):
539
            raise AssertionError()
540
        if not isinstance(self.mlon_out, np.ndarray):
541
            raise AssertionError()
542
        if not isinstance(self.mlt_out, np.ndarray):
543
            raise AssertionError()
544
        if not (self.mlt_out.shape == self.mlon_out.shape and
545
                self.mlat_out.shape == self.mlt_out.shape and
546
                self.mlt_out.shape == (1,)):
547
            raise AssertionError()
548
549
        np.testing.assert_allclose(self.mlat_out, [58.22474610], rtol=1e-4)
550
        np.testing.assert_allclose(self.mlon_out, [81.17611033], rtol=1e-4)
551
        np.testing.assert_allclose(self.mlt_out, [0.18891995], rtol=1e-4)
552
553 View Code Duplication
    def test_get_aacgm_coord_arr_list_single(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
554
        """Test array AACGMV2 calculation for list input of single values"""
555
        (self.mlat_out, self.mlon_out,
556
         self.mlt_out) = aacgmv2.get_aacgm_coord_arr([60], [0], [300],
557
                                                     self.dtime)
558
559
        if not isinstance(self.mlat_out, np.ndarray):
560
            raise AssertionError()
561
        if not isinstance(self.mlon_out, np.ndarray):
562
            raise AssertionError()
563
        if not isinstance(self.mlt_out, np.ndarray):
564
            raise AssertionError()
565
        if not (self.mlt_out.shape == self.mlon_out.shape and
566
                self.mlat_out.shape == self.mlt_out.shape and
567
                self.mlt_out.shape == (1,)):
568
            raise AssertionError()
569
570
        np.testing.assert_allclose(self.mlat_out, [58.22474610], rtol=1e-4)
571
        np.testing.assert_allclose(self.mlon_out, [81.17611033], rtol=1e-4)
572
        np.testing.assert_allclose(self.mlt_out, [0.18891995], rtol=1e-4)
573
574 View Code Duplication
    def test_get_aacgm_coord_arr_list(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
575
        """Test array AACGMV2 calculation for list input"""
576
        (self.mlat_out, self.mlon_out,
577
         self.mlt_out) = aacgmv2.get_aacgm_coord_arr([60, 61], [0, 0],
578
                                                     [300, 300], self.dtime)
579
580
        if not isinstance(self.mlat_out, np.ndarray):
581
            raise AssertionError()
582
        if not isinstance(self.mlon_out, np.ndarray):
583
            raise AssertionError()
584
        if not isinstance(self.mlt_out, np.ndarray):
585
            raise AssertionError()
586
        if not (self.mlt_out.shape == self.mlon_out.shape and
587
                self.mlat_out.shape == self.mlt_out.shape and
588
                self.mlt_out.shape == (2,)):
589
            raise AssertionError()
590
591
        np.testing.assert_allclose(self.mlat_out,
592
                                   [58.22474610, 59.31648007], rtol=1e-4)
593
        np.testing.assert_allclose(self.mlon_out,
594
                                   [81.17611033, 81.62281360], rtol=1e-4)
595
        np.testing.assert_allclose(self.mlt_out,
596
                                   [0.18891995, 0.21870017], rtol=1e-4)
597
598
    def test_get_aacgm_coord_arr_arr_single(self):
599
        """Test array AACGMV2 calculation for array with a single value"""
600
        (self.mlat_out, self.mlon_out,
601
         self.mlt_out) = aacgmv2.get_aacgm_coord_arr(np.array([60]),
602
                                                     np.array([0]),
603
                                                     np.array([300]),
604
                                                     self.dtime)
605
606
        if not isinstance(self.mlat_out, np.ndarray):
607
            raise AssertionError()
608
        if not isinstance(self.mlon_out, np.ndarray):
609
            raise AssertionError()
610
        if not isinstance(self.mlt_out, np.ndarray):
611
            raise AssertionError()
612
        if not (self.mlt_out.shape == self.mlon_out.shape and
613
                self.mlat_out.shape == self.mlt_out.shape and
614
                self.mlt_out.shape == (1,)):
615
            raise AssertionError()
616
617
        np.testing.assert_almost_equal(self.mlat_out, 58.2247, decimal=4)
618
        np.testing.assert_almost_equal(self.mlon_out, 81.1761, decimal=4)
619
        np.testing.assert_almost_equal(self.mlt_out, 0.1889, decimal=4)
620
621 View Code Duplication
    def test_get_aacgm_coord_arr_arr(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
622
        """Test array AACGMV2 calculation for an array"""
623
        (self.mlat_out, self.mlon_out,
624
         self.mlt_out) = aacgmv2.get_aacgm_coord_arr(np.array([60, 61]),
625
                                                     np.array([0, 0]),
626
                                                     np.array([300, 300]),
627
                                                     self.dtime)
628
629
        if not isinstance(self.mlat_out, np.ndarray):
630
            raise AssertionError()
631
        if not isinstance(self.mlon_out, np.ndarray):
632
            raise AssertionError()
633
        if not isinstance(self.mlt_out, np.ndarray):
634
            raise AssertionError()
635
        if not (self.mlt_out.shape == self.mlon_out.shape and
636
                self.mlat_out.shape == self.mlt_out.shape and
637
                self.mlt_out.shape == (2,)):
638
            raise AssertionError()
639
640
        np.testing.assert_allclose(self.mlat_out,
641
                                   [58.22474610, 59.31648007], rtol=1e-4)
642
        np.testing.assert_allclose(self.mlon_out,
643
                                   [81.17611033, 81.62281360], rtol=1e-4)
644
        np.testing.assert_allclose(self.mlt_out,
645
                                   [0.18891995, 0.21870017], rtol=1e-4)
646
647
    def test_get_aacgm_coord_arr_list_mix(self):
648
        """Test array AACGMV2 calculation for a list and floats"""
649
        (self.mlat_out, self.mlon_out,
650
         self.mlt_out) = aacgmv2.get_aacgm_coord_arr([60, 61], 0, 300,
651
                                                     self.dtime)
652
653
        if not isinstance(self.mlat_out, np.ndarray):
654
            raise AssertionError()
655
        if not isinstance(self.mlon_out, np.ndarray):
656
            raise AssertionError()
657
        if not isinstance(self.mlt_out, np.ndarray):
658
            raise AssertionError()
659
        if not (self.mlt_out.shape == self.mlon_out.shape and
660
                self.mlat_out.shape == self.mlt_out.shape and
661
                self.mlt_out.shape == (2,)):
662
            raise AssertionError()
663
664
        np.testing.assert_allclose(self.mlat_out,
665
                                   [58.22474610, 59.31648007], rtol=1e-4)
666
        np.testing.assert_allclose(self.mlon_out,
667
                                   [81.17611033, 81.62281360], rtol=1e-4)
668
        np.testing.assert_allclose(self.mlt_out,
669
                                   [0.18891995, 0.21870017], rtol=1e-4)
670
671
    def test_get_aacgm_coord_arr_arr_mix(self):
672
        """Test array AACGMV2 calculation for an array and floats"""
673
        (self.mlat_out, self.mlon_out,
674
         self.mlt_out) = aacgmv2.get_aacgm_coord_arr(np.array([60, 61]), 0,
675
                                                     300, self.dtime)
676
677
        if not isinstance(self.mlat_out, np.ndarray):
678
            raise AssertionError()
679
        if not isinstance(self.mlon_out, np.ndarray):
680
            raise AssertionError()
681
        if not isinstance(self.mlt_out, np.ndarray):
682
            raise AssertionError()
683
        if not (self.mlt_out.shape == self.mlon_out.shape and
684
                self.mlat_out.shape == self.mlt_out.shape and
685
                self.mlt_out.shape == (2,)):
686
            raise AssertionError()
687
688
        np.testing.assert_allclose(self.mlat_out,
689
                                   [58.22474610, 59.31648007], rtol=1e-4)
690
        np.testing.assert_allclose(self.mlon_out,
691
                                   [81.17611033, 81.62281360], rtol=1e-4)
692
        np.testing.assert_allclose(self.mlt_out,
693
                                   [0.18891995, 0.21870017], rtol=1e-4)
694
695 View Code Duplication
    def test_get_aacgm_coord_arr_mult_arr_mix(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
696
        """Test array AACGMV2 calculation for a multi-dim array and
697
        floats"""
698
        mlat_in = np.array([[60, 61, 62], [63, 64, 65]])
699
        (self.mlat_out, self.mlon_out,
700
         self.mlt_out) = aacgmv2.get_aacgm_coord_arr(mlat_in, 0, 300,
701
                                                     self.dtime)
702
703
        if not isinstance(self.mlat_out, np.ndarray):
704
            raise AssertionError()
705
        if not isinstance(self.mlon_out, np.ndarray):
706
            raise AssertionError()
707
        if not isinstance(self.mlt_out, np.ndarray):
708
            raise AssertionError()
709
        if not (self.mlt_out.shape == self.mlon_out.shape and
710
                self.mlat_out.shape == self.mlt_out.shape and
711
                self.mlt_out.shape == (2, 3)):
712
            raise AssertionError()
713
714
        np.testing.assert_allclose(self.mlat_out,
715
                                   [[58.2247461, 59.3164801, 60.4008651],
716
                                    [61.4780560, 62.5481858, 63.6113609]],
717
                                   rtol=1e-4)
718
        np.testing.assert_allclose(self.mlon_out,
719
                                   [[81.1761103, 81.6228136, 82.0969646],
720
                                    [82.6013918, 83.1393547, 83.7146224]],
721
                                   rtol=1e-4)
722
        np.testing.assert_allclose(self.mlt_out,
723
                                   [[0.18891995, 0.21870017, 0.25031024],
724
                                    [0.28393872, 0.31980291, 0.35815409]],
725
                                   rtol=1e-4)
726
        del mlat_in
727
728 View Code Duplication
    def test_get_aacgm_coord_arr_arr_unequal(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
729
        """Test array AACGMV2 calculation for unequal arrays"""
730
        mlat_in = np.array([[60, 61, 62], [63, 64, 65]])
731
        (self.mlat_out, self.mlon_out,
732
         self.mlt_out) = aacgmv2.get_aacgm_coord_arr(mlat_in, np.array([0]),
733
                                                     np.array([300]),
734
                                                     self.dtime)
735
736
        if not isinstance(self.mlat_out, np.ndarray):
737
            raise AssertionError()
738
        if not isinstance(self.mlon_out, np.ndarray):
739
            raise AssertionError()
740
        if not isinstance(self.mlt_out, np.ndarray):
741
            raise AssertionError()
742
        if not (self.mlt_out.shape == self.mlon_out.shape and
743
                self.mlat_out.shape == self.mlt_out.shape and
744
                self.mlt_out.shape == (2, 3)):
745
            raise AssertionError()
746
747
        np.testing.assert_allclose(self.mlat_out,
748
                                   [[58.2247, 59.3165, 60.4009],
749
                                    [61.4781, 62.5482, 63.6114]], rtol=1e-3)
750
        np.testing.assert_allclose(self.mlon_out,
751
                                   [[81.1761, 81.6228, 82.0970],
752
                                    [82.6014, 83.1394, 83.7146]], rtol=1e-3)
753
        np.testing.assert_allclose(self.mlt_out,
754
                                   [[0.1889, 0.2187, 0.2503],
755
                                    [0.2839, 0.3198, 0.3582]], rtol=1e-3)
756
        del mlat_in
757
758
    def test_get_aacgm_coord_arr_badidea(self):
759
        """Test array AACGMV2 calculation for BADIDEA"""
760
        method = "BADIDEA"
761
        (self.mlat_out, self.mlon_out,
762
         self.mlt_out) = aacgmv2.get_aacgm_coord_arr([60], [0], [3000],
763
                                                     self.dtime, method=method)
764
765
        np.testing.assert_allclose(self.mlat_out, [64.35677791], rtol=1e-3)
766
        np.testing.assert_allclose(self.mlon_out, [83.30272053], rtol=1e-3)
767
        np.testing.assert_allclose(self.mlt_out, [0.33069397], rtol=1e-3)
768
769
        del method
770
771
    def test_get_aacgm_coord_arr_location_failure(self):
772
        """Test array AACGMV2 calculation with a bad location"""
773
        (self.mlat_out, self.mlon_out,
774
         self.mlt_out) = aacgmv2.get_aacgm_coord_arr([0], [0], [0], self.dtime)
775
776
        if not isinstance(self.mlat_out, np.ndarray):
777
            raise AssertionError()
778
        if not isinstance(self.mlon_out, np.ndarray):
779
            raise AssertionError()
780
        if not isinstance(self.mlt_out, np.ndarray):
781
            raise AssertionError()
782
        if not (self.mlt_out.shape == self.mlon_out.shape and
783
                self.mlat_out.shape == self.mlt_out.shape and
784
                self.mlt_out.shape == (1,)):
785
            raise AssertionError()
786
        if not np.all([np.isnan(self.mlat_out), np.isnan(self.mlon_out),
787
                       np.isnan(self.mlt_out)]):
788
            raise AssertionError()
789
790
    def test_get_aacgm_coord_arr_time_failure(self):
791
        """Test array AACGMV2 calculation with a bad time"""
792
        with pytest.raises(AssertionError):
793
            (self.mlat_out, self.mlon_out,
794
             self.mlt_out) = aacgmv2.get_aacgm_coord_arr([60], [0], [300],
795
                                                         None)
796
797
    def test_get_aacgm_coord_arr_datetime_date(self):
798
        """Test array AACGMV2 calculation with date and datetime input"""
799
        (self.mlat_out, self.mlon_out,
800
         self.mlt_out) = aacgmv2.get_aacgm_coord_arr([60], [0], [300],
801
                                                     self.ddate)
802
        mlat_2, mlon_2, mlt_2 = aacgmv2.get_aacgm_coord_arr([60], [0], [300],
803
                                                            self.dtime)
804
805
        np.testing.assert_almost_equal(self.mlat_out, mlat_2, decimal=6)
806
        np.testing.assert_almost_equal(self.mlon_out, mlon_2, decimal=6)
807
        np.testing.assert_almost_equal(self.mlt_out, mlt_2, decimal=6)
808
809
        del mlat_2, mlon_2, mlt_2
810
811
    def test_warning_below_ground_get_aacgm_coord_arr(self):
812
        """ Test that a warning is issued if altitude is below zero"""
813
        import logbook
814
        lwarn = u"conversion not intended for altitudes < 0 km"
815
816
        with logbook.TestHandler() as handler:
817
            (self.mlat_out, self.mlon_out,
818
             self.mlt_out) = aacgmv2.get_aacgm_coord_arr([60], [0], [-1],
819
                                                         self.dtime)
820
            if not handler.has_warning(lwarn):
821
                raise AssertionError()
822
823
        handler.close()
824
825
    def test_get_aacgm_coord_arr_maxalt_failure(self):
826
        """For an array, test failure for an altitude too high for
827
        coefficients"""
828
        method = ""
829
        (self.mlat_out, self.mlon_out,
830
         self.mlt_out) = aacgmv2.get_aacgm_coord_arr([60], [0], [2001],
831
                                                     self.dtime, method=method)
832
        if not np.all([np.isnan(self.mlat_out), np.isnan(self.mlon_out),
833
                       np.isnan(self.mlt_out)]):
834
            raise AssertionError()
835
836
        del method
837
838
    def test_get_aacgm_coord_arr_mlat_failure(self):
839
        """Test error return for co-latitudes above 90 for an array"""
840
        import logbook
841
        lerr = u"unrealistic latitude"
842
843
        with logbook.TestHandler() as handler:
844
            with pytest.raises(AssertionError):
845
                aacgmv2.get_aacgm_coord_arr([91, 60, -91], 0, 300,
846
                                                self.dtime)
847
                if not handler.has_error(lerr):
848
                    raise AssertionError()
849
850
        handler.close()
851
852
class TestConvertCode:
853
    @classmethod
854
    def test_convert_str_to_bit_g2a(self):
855
        """Test conversion from string code to bit G2A"""
856
        if aacgmv2.convert_str_to_bit("G2A") != aacgmv2._aacgmv2.G2A:
857
            raise AssertionError()
858
859
    @classmethod
860
    def test_convert_str_to_bit_a2g(self):
861
        """Test conversion from string code to bit A2G"""
862
        if aacgmv2.convert_str_to_bit("A2G") != aacgmv2._aacgmv2.A2G:
863
            raise AssertionError()
864
865
    @classmethod
866
    def test_convert_str_to_bit_trace(self):
867
        """Test conversion from string code to bit TRACE"""
868
        if aacgmv2.convert_str_to_bit("TRACE") != aacgmv2._aacgmv2.TRACE:
869
            raise AssertionError()
870
871
    @classmethod
872
    def test_convert_str_to_bit_allowtrace(self):
873
        """Test conversion from string code to bit ALLOWTRACE"""
874
        if(aacgmv2.convert_str_to_bit("ALLOWTRACE") !=
875
           aacgmv2._aacgmv2.ALLOWTRACE):
876
            raise AssertionError()
877
878
    @classmethod
879
    def test_convert_str_to_bit_badidea(self):
880
        """Test conversion from string code to bit BADIDEA"""
881
        if(aacgmv2.convert_str_to_bit("BADIDEA") !=
882
           aacgmv2._aacgmv2.BADIDEA):
883
            raise AssertionError()
884
885
    @classmethod
886
    def test_convert_str_to_bit_geocentric(self):
887
        """Test conversion from string code to bit GEOCENTRIC"""
888
        if(aacgmv2.convert_str_to_bit("GEOCENTRIC") !=
889
           aacgmv2._aacgmv2.GEOCENTRIC):
890
            raise AssertionError()
891
892
    @classmethod
893
    def test_convert_str_to_bit_lowercase(self):
894
        """Test conversion from string code to bit for a lowercase code"""
895
        if aacgmv2.convert_str_to_bit("g2a") != aacgmv2._aacgmv2.G2A:
896
            raise AssertionError()
897
898
    @classmethod
899
    def test_convert_str_to_bit_spaces(self):
900
        """Test conversion from string code to bit for a code with spaces"""
901
        if(aacgmv2.convert_str_to_bit("G2A | trace") !=
902
           aacgmv2._aacgmv2.G2A + aacgmv2._aacgmv2.TRACE):
903
            raise AssertionError()
904
905
    @classmethod
906
    def test_convert_str_to_bit_invalid(self):
907
        """Test conversion from string code to bit for an invalid code"""
908
        if aacgmv2.convert_str_to_bit("ggoogg|") != aacgmv2._aacgmv2.G2A:
909
            raise AssertionError()
910
911
    @classmethod
912
    def test_convert_bool_to_bit_g2a(self):
913
        """Test conversion from string code to bit G2A"""
914
        if aacgmv2.convert_bool_to_bit() != aacgmv2._aacgmv2.G2A:
915
            raise AssertionError()
916
917
    @classmethod
918
    def test_convert_bool_to_bit_a2g(self):
919
        """Test conversion from string code to bit A2G"""
920
        if aacgmv2.convert_bool_to_bit(a2g=True) != aacgmv2._aacgmv2.A2G:
921
            raise AssertionError()
922
923
    @classmethod
924
    def test_convert_bool_to_bit_trace(self):
925
        """Test conversion from string code to bit TRACE"""
926
        if aacgmv2.convert_bool_to_bit(trace=True) != aacgmv2._aacgmv2.TRACE:
927
            raise AssertionError()
928
929
    @classmethod
930
    def test_convert_bool_to_bit_allowtrace(self):
931
        """Test conversion from string code to bit ALLOWTRACE"""
932
        if(aacgmv2.convert_bool_to_bit(allowtrace=True) !=
933
           aacgmv2._aacgmv2.ALLOWTRACE):
934
            raise AssertionError()
935
936
    @classmethod
937
    def test_convert_bool_to_bit_badidea(self):
938
        """Test conversion from string code to bit BADIDEA"""
939
        if(aacgmv2.convert_bool_to_bit(badidea=True) !=
940
           aacgmv2._aacgmv2.BADIDEA):
941
            raise AssertionError()
942
943
    @classmethod
944
    def test_convert_bool_to_bit_geocentric(self):
945
        """Test conversion from string code to bit GEOCENTRIC"""
946
        if(aacgmv2.convert_bool_to_bit(geocentric=True) !=
947
           aacgmv2._aacgmv2.GEOCENTRIC):
948
            raise AssertionError()
949
950
class TestMLTConvert:
951
    def setup(self):
952
        """Runs before every method to create a clean testing setup"""
953
        self.dtime = dt.datetime(2015, 1, 1, 0, 0, 0)
954
        self.dtime2 = dt.datetime(2015, 1, 1, 10, 0, 0)
955
        self.ddate = dt.date(2015, 1, 1)
956
        self.mlon_out = None
957
        self.mlt_out = None
958
        self.mlt_diff = None
959
        self.mlon_list = [270.0, 80.0, -95.0]
960
        self.mlt_list = [12.0, 25.0, -1.0]
961
        self.mlon_comp = [-101.657689, 93.34231102, 63.34231102]
962
        self.mlt_comp = [12.77717927, 0.1105126, 12.44384593]
963
        self.diff_comp = np.ones(shape=(3,)) * -10.52411552
964
965
    def teardown(self):
966
        """Runs after every method to clean up previous testing"""
967
        del self.mlon_out, self.mlt_out, self.mlt_list, self.mlon_list
968
        del self.mlon_comp, self.mlt_comp, self.mlt_diff, self.diff_comp
969
970
    def test_date_input(self):
971
        """Test to see that the date input works"""
972
        self.mlt_out = aacgmv2.convert_mlt(self.mlon_list, self.ddate,
973
                                           m2a=False)
974
        np.testing.assert_allclose(self.mlt_out, self.mlt_comp, rtol=1.0e-4)
975
976
    def test_datetime_exception(self):
977
        """Test to see that a value error is raised with bad time input"""
978
        with pytest.raises(ValueError):
979
            self.mlt_out = aacgmv2.wrapper.convert_mlt(self.mlon_list, 1997)
980
981
    def test_inv_convert_mlt_single(self):
982
        """Test MLT inversion for a single value"""
983
        for i,mlt in enumerate(self.mlt_list):
984
            self.mlon_out = aacgmv2.convert_mlt(mlt, self.dtime, m2a=True)
985
            np.testing.assert_almost_equal(self.mlon_out, self.mlon_comp[i],
986
                                           decimal=4)
987
988
    def test_inv_convert_mlt_list(self):
989
        """Test MLT inversion for a list"""
990
        self.mlon_out = aacgmv2.convert_mlt(self.mlt_list, self.dtime, m2a=True)
991
        np.testing.assert_allclose(self.mlon_out, self.mlon_comp, rtol=1.0e-4)
992
993
    def test_inv_convert_mlt_arr(self):
994
        """Test MLT inversion for an array"""
995
        self.mlon_out = aacgmv2.convert_mlt(np.array(self.mlt_list), self.dtime,
996
                                            m2a=True)
997
998
        np.testing.assert_allclose(self.mlon_out, self.mlon_comp, rtol=1.0e-4)
999
1000
    def test_inv_convert_mlt_wrapping(self):
1001
        """Test MLT wrapping"""
1002
        self.mlon_out = aacgmv2.convert_mlt(np.array([1, 25, -1, 23]),
1003
                                            self.dtime, m2a=True)
1004
1005
        np.testing.assert_almost_equal(self.mlon_out[0], self.mlon_out[1],
1006
                                       decimal=6)
1007
        np.testing.assert_almost_equal(self.mlon_out[2], self.mlon_out[3],
1008
                                       decimal=6)
1009
1010
    def test_mlt_convert_mlon_wrapping(self):
1011
        """Test mlon wrapping"""
1012
        self.mlt_out = aacgmv2.convert_mlt(np.array([270, -90, 1, 361]),
1013
                                           self.dtime, m2a=False)
1014
1015
        np.testing.assert_almost_equal(self.mlt_out[0], self.mlt_out[1],
1016
                                       decimal=6)
1017
        np.testing.assert_almost_equal(self.mlt_out[2], self.mlt_out[3],
1018
                                       decimal=6)
1019
1020
    def test_mlt_convert_single(self):
1021
        """Test MLT calculation for a single value"""
1022
        for i,mlon in enumerate(self.mlon_list):
1023
            self.mlt_out = aacgmv2.convert_mlt(mlon, self.dtime, m2a=False)
1024
            np.testing.assert_almost_equal(self.mlt_out, self.mlt_comp[i],
1025
                                           decimal=4)
1026
1027
    def test_mlt_convert_list(self):
1028
        """Test MLT calculation for a list"""
1029
        self.mlt_out = aacgmv2.convert_mlt(self.mlon_list, self.dtime,
1030
                                           m2a=False)
1031
        np.testing.assert_allclose(self.mlt_out, self.mlt_comp, rtol=1.0e-4)
1032
1033
    def test_mlt_convert_arr(self):
1034
        """Test MLT calculation for an array"""
1035
        self.mlt_out = aacgmv2.convert_mlt(np.array(self.mlon_list),
1036
                                           self.dtime, m2a=False)
1037
        np.testing.assert_allclose(self.mlt_out, self.mlt_comp, rtol=1.0e-4)
1038
1039
    def test_mlt_convert_change(self):
1040
        """Test that MLT changes with UT"""
1041
        self.mlt_out = aacgmv2.convert_mlt(self.mlon_list, self.dtime)
1042
        self.mlt_diff = self.mlt_out - aacgmv2.convert_mlt(self.mlon_list,
1043
                                                           self.dtime2)
1044
1045
        np.testing.assert_allclose(self.mlt_diff, self.diff_comp, rtol=1.0e-4)
1046
1047
class TestCoeffPath:
1048
1049
    def setup(self):
1050
        """Runs before every method to create a clean testing setup"""
1051
        import os
1052
        os.environ['IGRF_COEFFS'] = "default_igrf"
1053
        os.environ['AACGM_v2_DAT_PREFIX'] = "default_coeff"
1054
        self.default_igrf = os.environ['IGRF_COEFFS']
1055
        self.default_coeff = os.environ['AACGM_v2_DAT_PREFIX']
1056
1057
    def teardown(self):
1058
        """Runs after every method to clean up previous testing"""
1059
        del self.default_igrf, self.default_coeff
1060
1061
    def test_set_coeff_path_default(self):
1062
        """Test the coefficient path setting using default values"""
1063
        import os
1064
        aacgmv2.wrapper.set_coeff_path()
1065
1066
        if os.environ['IGRF_COEFFS'] != self.default_igrf:
1067
            raise AssertionError()
1068
        if os.environ['AACGM_v2_DAT_PREFIX'] != self.default_coeff:
1069
            raise AssertionError()
1070
1071
    @classmethod
1072
    def test_set_coeff_path_string(self):
1073
        """Test the coefficient path setting using two user specified values"""
1074
        import os
1075
        aacgmv2.wrapper.set_coeff_path("hi", "bye")
1076
1077
        if os.environ['IGRF_COEFFS'] != "hi":
1078
            raise AssertionError()
1079
        if os.environ['AACGM_v2_DAT_PREFIX'] != "bye":
1080
            raise AssertionError()
1081
1082
    @classmethod
1083
    def test_set_coeff_path_true(self):
1084
        """Test the coefficient path setting using the module values"""
1085
        import os
1086
        aacgmv2.wrapper.set_coeff_path(True, True)
1087
1088
        if os.environ['IGRF_COEFFS'] != aacgmv2.IGRF_COEFFS:
1089
            raise AssertionError()
1090
        if os.environ['AACGM_v2_DAT_PREFIX'] != aacgmv2.AACGM_v2_DAT_PREFIX:
1091
            raise AssertionError()
1092
1093
    def test_set_only_aacgm_coeff_path(self):
1094
        """Test the coefficient path setting using a mix of input"""
1095
        import os
1096
        aacgmv2.wrapper.set_coeff_path(coeff_prefix="hi")
1097
1098
        if os.environ['IGRF_COEFFS'] != self.default_igrf:
1099
            raise AssertionError()
1100
        if os.environ['AACGM_v2_DAT_PREFIX'] != "hi":
1101
            raise AssertionError()
1102
1103
    def test_set_only_igrf_coeff_path(self):
1104
        """Test the coefficient path setting using a mix of input"""
1105
        import os
1106
        aacgmv2.wrapper.set_coeff_path(igrf_file="hi")
1107
1108
        if os.environ['IGRF_COEFFS'] != "hi":
1109
            raise AssertionError()
1110
        if os.environ['AACGM_v2_DAT_PREFIX'] != self.default_coeff:
1111
            raise AssertionError()
1112
1113
    @classmethod
1114
    def test_set_both_mixed(self):
1115
        """Test the coefficient path setting using a mix of input"""
1116
        import os
1117
        aacgmv2.wrapper.set_coeff_path(igrf_file=True, coeff_prefix="hi")
1118
1119
        if os.environ['IGRF_COEFFS'] != aacgmv2.IGRF_COEFFS:
1120
            raise AssertionError()
1121
        if os.environ['AACGM_v2_DAT_PREFIX'] != "hi":
1122
            raise AssertionError()
1123