Passed
Pull Request — develop (#57)
by Angeline
01:10
created

TestCoeffPath.test_set_coeff_path()   B

Complexity

Conditions 8

Size

Total Lines 29
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 29
rs 7.3333
c 0
b 0
f 0
cc 8
nop 2
1
# -*- coding: utf-8 -*-
2
from __future__ import division, absolute_import, unicode_literals
3
4
import datetime as dt
5
from io import StringIO
6
import logging
7
import numpy as np
8
import os
9
from sys import version_info
10
import pytest
11
import warnings
12
13
import aacgmv2
14
15
16
class TestConvertArray:
17
    def setup(self):
18
        self.out = None
19
        self.ref = None
20
        self.rtol = 1.0e-4
21
22
    def teardown(self):
23
        del self.out, self.ref, self.rtol
24
25
    def evaluate_output(self, ind=None):
26
        """ Function used to evaluate convert_latlon_arr output"""
27
        if self.out is not None:
28
            if ind is not None:
29
                self.ref = [[rr[ind]] for rr in self.ref]
30
31
            np.testing.assert_equal(len(self.out), len(self.ref))
32
            for i, oo in enumerate(self.out):
33
                if not isinstance(oo, np.ndarray):
34
                    raise TypeError("output value is not a numpy array")
35
36
                np.testing.assert_equal(len(oo), len(self.ref[i]))
37
                np.testing.assert_allclose(oo, self.ref[i], rtol=self.rtol)
38
39
40
class TestConvertLatLon:
41
    def setup(self):
42
        """Runs before every method to create a clean testing setup"""
43
        self.dtime = dt.datetime(2015, 1, 1, 0, 0, 0)
44
        self.ddate = dt.date(2015, 1, 1)
45
        self.in_args = [60, 0]
46
        self.out = None
47
        self.rtol = 1.0e-4
48
49
    def teardown(self):
50
        """Runs after every method to clean up previous testing"""
51
        del self.out, self.in_args, self.rtol, self.dtime, self.ddate
52
53
    @pytest.mark.parametrize('alt,method_code,ref',
54
                             [(300, 'TRACE', [58.2268, 81.1613, 1.0457]),
55
                              (3000.0, "G2A|BADIDEA", [64.3578, 83.2895,
56
                                                       1.4694]),
57
                              (7000.0, "G2A|TRACE|BADIDEA",
58
                               [69.3187, 85.0845, 2.0973])])
59
    def test_convert_latlon(self, alt, method_code, ref):
60
        """Test single value latlon conversion"""
61
        self.in_args.extend([alt, self.dtime, method_code])
62
        self.out = aacgmv2.convert_latlon(*self.in_args)
63
        np.testing.assert_allclose(self.out, ref, rtol=self.rtol)
64
65
    @pytest.mark.parametrize('lat,ref',
66
                             [(90.01, [83.927161, 170.1471396, 1.04481923]),
67
                              (-90.01, [-74.9814852, 17.990332, 1.044819236])])
68
    def test_convert_latlon_high_lat(self, lat, ref):
69
        """Test single latlon conversion with latitude just out of bounds"""
70
        self.in_args[0] = lat
71
        self.in_args.extend([300, self.dtime, 'G2A'])
72
        self.out = aacgmv2.convert_latlon(*self.in_args)
73
        np.testing.assert_allclose(self.out, ref, rtol=self.rtol)
74
75
    def test_convert_latlon_datetime_date(self):
76
        """Test single latlon conversion with date and datetime input"""
77
        self.in_args.extend([300, self.ddate, 'TRACE'])
78
        self.out = aacgmv2.convert_latlon(*self.in_args)
79
        np.testing.assert_allclose(self.out, [58.2268, 81.1613, 1.0457],
80
                                   rtol=self.rtol)
81
82
    @pytest.mark.skipif(version_info.major == 2,
83
                        reason='Not raised in Python 2')
84
    def test_convert_latlon_location_failure(self):
85
        """Test single value latlon conversion with a bad location"""
86
        self.out = aacgmv2.convert_latlon(0, 0, 0, self.dtime, self.in_args[-1])
87
        assert np.all(np.isnan(np.array(self.out)))
88
89
    def test_convert_latlon_maxalt_failure(self):
90
        """test convert_latlon failure for an altitude too high for coeffs"""
91
        self.in_args.extend([2001, self.dtime, ""])
92
        self.out = aacgmv2.convert_latlon(*self.in_args)
93
        assert np.all(np.isnan(np.array(self.out)))
94
95
    @pytest.mark.parametrize('in_rep,in_irep,msg',
96
                             [(None, 3, "must be a datetime object"),
97
                              (91, 0, "unrealistic latitude"),
98
                              (-91, 0, "unrealistic latitude"),
99
                              (None, 4, "unknown method code")])
100
    def test_convert_latlon_failure(self, in_rep, in_irep, msg):
101
        self.in_args.extend([300, self.dtime, "G2A"])
102
        self.in_args[in_irep] = in_rep
103
        with pytest.raises(ValueError, match=msg):
104
            aacgmv2.convert_latlon(*self.in_args)
105
106
107
class TestConvertLatLonArr(TestConvertArray):
108 View Code Duplication
    def setup(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
109
        """Runs before every method to create a clean testing setup"""
110
        self.dtime = dt.datetime(2015, 1, 1, 0, 0, 0)
111
        self.ddate = dt.date(2015, 1, 1)
112
        self.lat_in = [60.0, 61.0]
113
        self.lon_in = [0.0, 0.0]
114
        self.alt_in = [300.0, 300.0]
115
        self.method = 'TRACE'
116
        self.out = None
117
        self.ref = [[58.2268, 59.3184], [81.1613, 81.6080], [1.0457, 1.0456]]
118
        self.rtol = 1.0e-4
119
120
    def teardown(self):
121
        """Runs after every method to clean up previous testing"""
122
        del self.lat_in, self.lon_in, self.alt_in, self.dtime, self.ddate
123
        del self.method, self.out, self.ref, self.rtol
124
125
    def test_convert_latlon_arr_single_val(self):
126
        """Test array latlon conversion for a single value"""
127
        self.out = aacgmv2.convert_latlon_arr(self.lat_in[0], self.lon_in[0],
128
                                              self.alt_in[0], self.dtime,
129
                                              self.method)
130
        self.evaluate_output(ind=0)
131
132
    def test_convert_latlon_arr_arr_single(self):
133
        """Test array latlon conversion for array input of shape (1,)"""
134
        self.out = aacgmv2.convert_latlon_arr(np.array([self.lat_in[0]]),
135
                                              np.array([self.lon_in[0]]),
136
                                              np.array([self.alt_in[0]]),
137
                                              self.dtime, self.method)
138
        self.evaluate_output(ind=0)
139
140
    def test_convert_latlon_arr_list_single(self):
141
        """Test array latlon conversion for list input of single values"""
142
        self.out = aacgmv2.convert_latlon_arr([self.lat_in[0]],
143
                                              [self.lon_in[0]],
144
                                              [self.alt_in[0]], self.dtime,
145
                                              self.method)
146
        self.evaluate_output(ind=0)
147
148
    def test_convert_latlon_arr_list(self):
149
        """Test array latlon conversion for list input"""
150
        self.out = aacgmv2.convert_latlon_arr(self.lat_in, self.lon_in,
151
                                              self.alt_in, self.dtime,
152
                                              self.method)
153
        self.evaluate_output()
154
155
    def test_convert_latlon_arr_arr(self):
156
        """Test array latlon conversion for array input"""
157
        self.out = aacgmv2.convert_latlon_arr(np.array(self.lat_in),
158
                                              np.array(self.lon_in),
159
                                              np.array(self.alt_in),
160
                                              self.dtime, self.method)
161
        self.evaluate_output()
162
163
    def test_convert_latlon_arr_list_mix(self):
164
        """Test array latlon conversion for mixed types with list"""
165
        self.out = aacgmv2.convert_latlon_arr(self.lat_in, self.lon_in[0],
166
                                              self.alt_in[0], self.dtime,
167
                                              self.method)
168
        self.evaluate_output()
169
170
    def test_convert_latlon_arr_arr_mix(self):
171
        """Test array latlon conversion for mixed type with an array"""
172
        self.out = aacgmv2.convert_latlon_arr(np.array(self.lat_in),
173
                                              self.lon_in[0], self.alt_in[0],
174
                                              self.dtime, self.method)
175
        self.evaluate_output()
176
177
    def test_convert_latlon_arr_arr_mult_and_single_element(self):
178
        """Test latlon conversion for arrays with multiple and single vals"""
179
        self.out = aacgmv2.convert_latlon_arr(np.array(self.lat_in),
180
                                              np.array([self.lon_in[0]]),
181
                                              np.array(self.alt_in),
182
                                              self.dtime, self.method)
183
        self.evaluate_output()
184
185
    @pytest.mark.parametrize('method_code,alt,local_ref',
186
                             [("BADIDEA", 3000.0,
187
                               [[64.3580], [83.2895], [1.4694]]),
188
                              ("BADIDEA|TRACE", 7000.0,
189
                               [[69.3187], [85.0845], [2.0973]])])
190
    def test_convert_latlon_arr_badidea(self, method_code, alt, local_ref):
191
        """Test array latlon conversion for BADIDEA"""
192
        self.out = aacgmv2.convert_latlon_arr(self.lat_in[0], self.lon_in[0],
193
                                              [alt], self.dtime, method_code)
194
        self.ref = local_ref
195
        self.evaluate_output()
196
197
    @pytest.mark.skipif(version_info.major == 2,
198
                        reason='Not raised in Python 2')
199
    def test_convert_latlon_arr_location_failure(self):
200
        """Test array latlon conversion with a bad location"""
201
202
        with warnings.catch_warnings():
203
            # Causes all warnings to be surpressed
204
            warnings.simplefilter("ignore")
205
206
            # Trigger a warning
207
            self.out = aacgmv2.convert_latlon_arr([0], [0], [0], self.dtime, "")
208
209
            # Test the output
210
            np.testing.assert_equal(len(self.out), len(self.ref))
211
            assert np.any(~np.isfinite(np.array(self.out)))
212
213
    def test_convert_latlon_arr_datetime_date(self):
214
        """Test array latlon conversion with date and datetime input"""
215
        self.out = aacgmv2.convert_latlon_arr(self.lat_in, self.lon_in,
216
                                              self.alt_in, self.ddate,
217
                                              self.method)
218
        self.evaluate_output()
219
220
    def test_convert_latlon_arr_clip(self):
221
        """Test array latlon conversion with latitude clipping"""
222
        self.lat_in = [90.01, -90.01]
223
        self.ref = [[83.92352053, -74.98110552], [170.1381271, 17.98164313],
224
                    [1.04481924, 1.04481924]]
225
        self.out = aacgmv2.convert_latlon_arr(self.lat_in, self.lon_in,
226
                                              self.alt_in, self.ddate,
227
                                              self.method)
228
        self.evaluate_output()
229
230
    def test_convert_latlon_arr_maxalt_failure(self):
231
        """test convert_latlon_arr failure for altitudes too high for coeffs"""
232
        self.method = ""
233
        self.out = aacgmv2.convert_latlon_arr(self.lat_in[0], self.lon_in[0],
234
                                              [2001], self.dtime, self.method)
235
        assert np.all(np.isnan(np.array(self.out)))
236
237
    @pytest.mark.parametrize('in_rep,in_irep,msg',
238
                             [(None, 3, "must be a datetime object"),
239
                              ([np.full(shape=(3, 2), fill_value=50.0), 0],
240
                               [0, 1], "unable to process multi-dimensional"),
241
                              ([50, 60, 70], 0, "arrays are mismatched"),
242
                              ([[91, 60, -91], 0, 300], [0, 1, 2],
243
                               "unrealistic latitude"),
244
                              (None, 4, "unknown method code")])
245
    def test_convert_latlon_arr_failure(self, in_rep, in_irep, msg):
246
        in_args = np.array([self.lat_in, self.lon_in, self.alt_in, self.dtime,
247
                            "G2A"], dtype=object)
248
        in_args[in_irep] = in_rep
249
        with pytest.raises(ValueError, match=msg):
250
            aacgmv2.convert_latlon_arr(*in_args)
251
252
253
class TestGetAACGMCoord:
254
    def setup(self):
255
        """Runs before every method to create a clean testing setup"""
256
        self.dtime = dt.datetime(2015, 1, 1, 0, 0, 0)
257
        self.ddate = dt.date(2015, 1, 1)
258
        self.in_args = [60, 0]
259
        self.out = None
260
        self.rtol = 1.0e-4
261
262
    def teardown(self):
263
        """Runs after every method to clean up previous testing"""
264
        del self.out, self.in_args, self.rtol, self.dtime, self.ddate
265
266
    @pytest.mark.parametrize('alt,method_code,ref',
267
                             [(300, 'TRACE', [58.2268, 81.1613, 0.1888]),
268
                              (3000.0, "G2A|BADIDEA", [64.3578, 83.2895,
269
                                                       0.3307]),
270
                              (7000.0, "G2A|TRACE|BADIDEA",
271
                               [69.3187, 85.0845, 0.4503])])
272
    def test_get_aacgm_coord(self, alt, method_code, ref):
273
        """Test single value AACGMV2 calculation, defaults to TRACE"""
274
        self.in_args.extend([alt, self.dtime, method_code])
275
        self.out = aacgmv2.get_aacgm_coord(*self.in_args)
276
        np.testing.assert_allclose(self.out, ref, rtol=self.rtol)
277
278
    def test_get_aacgm_coord_datetime_date(self):
279
        """Test single AACGMV2 calculation with date and datetime input"""
280
        self.in_args.extend([300.0, self.ddate, 'TRACE'])
281
        self.out = aacgmv2.get_aacgm_coord(*self.in_args)
282
        np.testing.assert_allclose(self.out, [58.2268, 81.1613, 0.1888],
283
                                   rtol=self.rtol)
284
285
    @pytest.mark.skipif(version_info.major == 2,
286
                        reason='Not raised in Python 2')
287
    def test_get_aacgm_coord_location_failure(self):
288
        """Test single value AACGMV2 calculation with a bad location"""
289
        self.in_args.extend([0.0, self.dtime, 'TRACE'])
290
        self.in_args[0] = 0.0
291
292
        self.out = aacgmv2.get_aacgm_coord(*self.in_args)
293
        np.all(np.isnan(np.array(self.out)))
294
295
    def test_get_aacgm_coord_maxalt_failure(self):
296
        """test get_aacgm_coord failure for an altitude too high for coeffs"""
297
        self.in_args.extend([2001, self.dtime, ""])
298
        self.out = aacgmv2.get_aacgm_coord(*self.in_args)
299
        assert np.all(np.isnan(np.array(self.out)))
300
301
    @pytest.mark.parametrize('in_index,value',
302
                             [(3, None), (0, 91.0), (0, -91.0)])
303
    def test_get_aacgm_coord_raise_value_error(self, in_index, value):
304
        """Test different ways to raise a ValueError"""
305
        self.in_args.extend([300.0, self.dtime])
306
        self.in_args[in_index] = value
307
        with pytest.raises(ValueError):
308
            self.out = aacgmv2.get_aacgm_coord(*self.in_args)
309
310
311
class TestGetAACGMCoordArr(TestConvertArray):
312 View Code Duplication
    def setup(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
313
        """Runs before every method to create a clean testing setup"""
314
        self.dtime = dt.datetime(2015, 1, 1, 0, 0, 0)
315
        self.ddate = dt.date(2015, 1, 1)
316
        self.lat_in = [60.0, 61.0]
317
        self.lon_in = [0.0, 0.0]
318
        self.alt_in = [300.0, 300.0]
319
        self.method = 'TRACE'
320
        self.out = None
321
        self.ref = [[58.22676, 59.31847], [81.16135, 81.60797],
322
                    [0.18880, 0.21857]]
323
        self.rtol = 1.0e-4
324
325
    def teardown(self):
326
        """Runs after every method to clean up previous testing"""
327
        del self.out, self.ref, self.lat_in, self.dtime, self.ddate
328
        del self.lon_in, self.alt_in, self.method, self.rtol
329
330
    def test_get_aacgm_coord_arr_single_val(self):
331
        """Test array AACGMV2 calculation for a single value"""
332
        self.out = aacgmv2.get_aacgm_coord_arr(self.lat_in[0], self.lon_in[0],
333
                                               self.alt_in[0], self.dtime,
334
                                               self.method)
335
        self.evaluate_output(ind=0)
336
337
    def test_get_aacgm_coord_arr_list_single(self):
338
        """Test array AACGMV2 calculation for list input of single values"""
339
        self.out = aacgmv2.get_aacgm_coord_arr([self.lat_in[0]],
340
                                               [self.lon_in[0]],
341
                                               [self.alt_in[0]], self.dtime,
342
                                               self.method)
343
        self.evaluate_output(ind=0)
344
345
    def test_get_aacgm_coord_arr_arr_single(self):
346
        """Test array AACGMV2 calculation for array with a single value"""
347
        self.out = aacgmv2.get_aacgm_coord_arr(np.array([self.lat_in[0]]),
348
                                               np.array([self.lon_in[0]]),
349
                                               np.array([self.alt_in[0]]),
350
                                               self.dtime, self.method)
351
        self.evaluate_output(ind=0)
352
353
    def test_get_aacgm_coord_arr_list(self):
354
        """Test array AACGMV2 calculation for list input"""
355
        self.out = aacgmv2.get_aacgm_coord_arr(self.lat_in, self.lon_in,
356
                                               self.alt_in, self.dtime,
357
                                               self.method)
358
        self.evaluate_output()
359
360
    def test_get_aacgm_coord_arr_arr(self):
361
        """Test array AACGMV2 calculation for an array"""
362
        self.out = aacgmv2.get_aacgm_coord_arr(np.array(self.lat_in),
363
                                               np.array(self.lon_in),
364
                                               np.array(self.alt_in),
365
                                               self.dtime, self.method)
366
        self.evaluate_output()
367
368
    def test_get_aacgm_coord_arr_list_mix(self):
369
        """Test array AACGMV2 calculation for a list and floats"""
370
        self.out = aacgmv2.get_aacgm_coord_arr(self.lat_in, self.lon_in[0],
371
                                               self.alt_in[0], self.dtime,
372
                                               self.method)
373
        self.evaluate_output()
374
375
    def test_get_aacgm_coord_arr_arr_mix(self):
376
        """Test array AACGMV2 calculation for an array and floats"""
377
        self.out = aacgmv2.get_aacgm_coord_arr(np.array(self.lat_in),
378
                                               self.lon_in[0], self.alt_in[0],
379
                                               self.dtime, self.method)
380
        self.evaluate_output()
381
382
    def test_get_aacgm_coord_arr_badidea(self):
383
        """Test array AACGMV2 calculation for BADIDEA"""
384
        self.method = "|".join([self.method, "BADIDEA"])
385
        self.out = aacgmv2.get_aacgm_coord_arr(self.lat_in[0], self.lon_in[0],
386
                                               [3000.0], self.dtime,
387
                                               self.method)
388
        self.ref = [[64.3481], [83.2885], [0.3306]]
389
        self.evaluate_output()
390
391
    @pytest.mark.skipif(version_info.major == 2,
392
                        reason='Not raised in Python 2')
393
    def test_get_aacgm_coord_arr_location_failure(self):
394
        """Test array AACGMV2 calculation with a bad location"""
395
        self.out = aacgmv2.get_aacgm_coord_arr([0], [0], [0], self.dtime,
396
                                               self.method)
397
398
        np.testing.assert_equal(len(self.out), len(self.ref))
399
        assert [isinstance(oo, np.ndarray) and len(oo) == 1 for oo in self.out]
400
        assert np.any([np.isnan(oo) for oo in self.out])
401
402
    def test_get_aacgm_coord_arr_mult_failure(self):
403
        """Test aacgm_coord_arr failure with multi-dim array input"""
404
405
        with pytest.raises(ValueError):
406
            (self.mlat_out, self.mlon_out,
407
             self.mlt_out) = aacgmv2.get_aacgm_coord_arr(
408
                 np.array([[60, 61, 62], [63, 64, 65]]), 0, 300, self.dtime)
409
410
    def test_get_aacgm_coord_arr_time_failure(self):
411
        """Test array AACGMV2 calculation with a bad time"""
412
        with pytest.raises(ValueError):
413
            aacgmv2.get_aacgm_coord_arr(self.lat_in, self.lon_in, self.alt_in,
414
                                        None, self.method)
415
416
    def test_get_aacgm_coord_arr_mlat_failure(self):
417
        """Test error return for co-latitudes above 90 for an array"""
418
419
        self.lat_in = [91, 60, -91]
420
        with pytest.raises(ValueError):
421
            self.out = aacgmv2.get_aacgm_coord_arr(self.lat_in, self.lon_in[0],
422
                                                   self.alt_in[0], self.dtime,
423
                                                   self.method)
424
425
    def test_get_aacgm_coord_arr_datetime_date(self):
426
        """Test array AACGMV2 calculation with date and datetime input"""
427
        self.out = aacgmv2.get_aacgm_coord_arr(self.lat_in, self.lon_in,
428
                                               self.alt_in, self.ddate,
429
                                               self.method)
430
        self.ref = aacgmv2.get_aacgm_coord_arr(self.lat_in, self.lon_in,
431
                                               self.alt_in, self.dtime,
432
                                               self.method)
433
        self.evaluate_output()
434
435
    def test_get_aacgm_coord_arr_maxalt_failure(self):
436
        """test aacgm_coord_arr failure for an altitude too high for coeff"""
437
        self.method = ""
438
        self.alt_in = [2001 for ll in self.lat_in]
439
        self.out = aacgmv2.get_aacgm_coord_arr(self.lat_in, self.lon_in,
440
                                               self.alt_in, self.dtime,
441
                                               self.method)
442
443
        np.testing.assert_equal(len(self.out), len(self.ref))
444
        assert [isinstance(oo, np.ndarray) and len(oo) == len(self.lat_in)
445
                for oo in self.out]
446
        assert np.all(np.isnan(np.array(self.out)))
447
448
449
class TestConvertCode:
450
    def setup(self):
451
        self.c_method_code = None
452
453
    def teardown(self):
454
        del self.c_method_code
455
456
    @pytest.mark.parametrize('method_code',
457
                             [('G2A'), ('A2G'), ('TRACE'), ('ALLOWTRACE'),
458
                              ('BADIDEA'), ('GEOCENTRIC'), ('g2a')])
459
    def test_convert_str_to_bit(self, method_code):
460
        """Test conversion from string code to bit"""
461
        if hasattr(aacgmv2._aacgmv2, method_code.upper()):
462
            self.c_method_code = getattr(aacgmv2._aacgmv2, method_code.upper())
463
        else:
464
            raise ValueError('cannot find method in C code: {:}'.format(
465
                method_code))
466
467
        assert aacgmv2.convert_str_to_bit(method_code) == self.c_method_code
468
469
    def test_convert_str_to_bit_spaces(self):
470
        """Test conversion from string code to bit for a code with spaces"""
471
        if(aacgmv2.convert_str_to_bit("G2A | trace")
472
           != aacgmv2._aacgmv2.G2A + aacgmv2._aacgmv2.TRACE):
473
            raise AssertionError()
474
475
    def test_convert_str_to_bit_invalid(self):
476
        """Test conversion from string code to bit for an invalid code"""
477
        if aacgmv2.convert_str_to_bit("ggoogg|") != aacgmv2._aacgmv2.G2A:
478
            raise AssertionError()
479
480
    @pytest.mark.parametrize('bool_dict,method_code',
481
                             [({}, 'G2A'), ({'a2g': True}, 'A2G'),
482
                              ({'trace': True}, 'TRACE'),
483
                              ({'allowtrace': True}, 'ALLOWTRACE'),
484
                              ({'badidea': True}, 'BADIDEA'),
485
                              ({'geocentric': True}, 'GEOCENTRIC')])
486
    def test_convert_bool_to_bit(self, bool_dict, method_code):
487
        """Test conversion from Boolean code to bit"""
488
        if hasattr(aacgmv2._aacgmv2, method_code.upper()):
489
            self.c_method_code = getattr(aacgmv2._aacgmv2, method_code.upper())
490
        else:
491
            raise ValueError('cannot find method in C code: {:}'.format(
492
                method_code))
493
494
        assert aacgmv2.convert_bool_to_bit(**bool_dict) == self.c_method_code
495
496
497
class TestMLTConvert:
498
    def setup(self):
499
        """Runs before every method to create a clean testing setup"""
500
        self.dtime = dt.datetime(2015, 1, 1, 0, 0, 0)
501
        self.dtime2 = dt.datetime(2015, 1, 1, 10, 0, 0)
502
        self.ddate = dt.date(2015, 1, 1)
503
        self.mlon_out = None
504
        self.mlt_out = None
505
        self.mlt_diff = None
506
        self.mlon_list = [270.0, 80.0, -95.0]
507
        self.mlt_list = [12.0, 25.0, -1.0]
508
        self.mlon_comp = [-101.670617955439, 93.329382044561, 63.329382044561]
509
        self.mlt_comp = [12.7780412, 0.11137453, 12.44470786]
510
        self.diff_comp = np.ones(shape=(3,)) * -10.52411552
511
512
    def teardown(self):
513
        """Runs after every method to clean up previous testing"""
514
        del self.mlon_out, self.mlt_out, self.mlt_list, self.mlon_list
515
        del self.mlon_comp, self.mlt_comp, self.mlt_diff, self.diff_comp
516
517
    def test_date_input(self):
518
        """Test to see that the date input works"""
519
        self.mlt_out = aacgmv2.convert_mlt(self.mlon_list, self.ddate,
520
                                           m2a=False)
521
        np.testing.assert_allclose(self.mlt_out, self.mlt_comp, rtol=1.0e-4)
522
523
    def test_datetime_exception(self):
524
        """Test to see that a value error is raised with bad time input"""
525
        with pytest.raises(ValueError):
526
            self.mlt_out = aacgmv2.wrapper.convert_mlt(self.mlon_list, 1997)
527
528
    def test_inv_convert_mlt_single(self):
529
        """Test MLT inversion for a single value"""
530
        for i, mlt in enumerate(self.mlt_list):
531
            self.mlon_out = aacgmv2.convert_mlt(mlt, self.dtime, m2a=True)
532
            np.testing.assert_almost_equal(self.mlon_out, self.mlon_comp[i],
533
                                           decimal=4)
534
535
    def test_inv_convert_mlt_list(self):
536
        """Test MLT inversion for a list"""
537
        self.mlon_out = aacgmv2.convert_mlt(self.mlt_list, self.dtime, m2a=True)
538
        np.testing.assert_allclose(self.mlon_out, self.mlon_comp, rtol=1.0e-4)
539
540
    def test_inv_convert_mlt_arr(self):
541
        """Test MLT inversion for an array"""
542
        self.mlon_out = aacgmv2.convert_mlt(np.array(self.mlt_list), self.dtime,
543
                                            m2a=True)
544
545
        np.testing.assert_allclose(self.mlon_out, self.mlon_comp, rtol=1.0e-4)
546
547
    def test_inv_convert_mlt_wrapping(self):
548
        """Test MLT wrapping"""
549
        self.mlon_out = aacgmv2.convert_mlt(np.array([1, 25, -1, 23]),
550
                                            self.dtime, m2a=True)
551
552
        np.testing.assert_almost_equal(self.mlon_out[0], self.mlon_out[1],
553
                                       decimal=6)
554
        np.testing.assert_almost_equal(self.mlon_out[2], self.mlon_out[3],
555
                                       decimal=6)
556
557
    def test_mlt_convert_mlon_wrapping(self):
558
        """Test mlon wrapping"""
559
        self.mlt_out = aacgmv2.convert_mlt(np.array([270, -90, 1, 361]),
560
                                           self.dtime, m2a=False)
561
562
        np.testing.assert_almost_equal(self.mlt_out[0], self.mlt_out[1],
563
                                       decimal=6)
564
        np.testing.assert_almost_equal(self.mlt_out[2], self.mlt_out[3],
565
                                       decimal=6)
566
567
    def test_mlt_convert_single(self):
568
        """Test MLT calculation for a single value"""
569
        for i, mlon in enumerate(self.mlon_list):
570
            self.mlt_out = aacgmv2.convert_mlt(mlon, self.dtime, m2a=False)
571
            np.testing.assert_almost_equal(self.mlt_out, self.mlt_comp[i],
572
                                           decimal=4)
573
574
    def test_mlt_convert_list(self):
575
        """Test MLT calculation for a list"""
576
        self.mlt_out = aacgmv2.convert_mlt(self.mlon_list, self.dtime,
577
                                           m2a=False)
578
        np.testing.assert_allclose(self.mlt_out, self.mlt_comp, rtol=1.0e-4)
579
580
    def test_mlt_convert_arr(self):
581
        """Test MLT calculation for an array"""
582
        self.mlt_out = aacgmv2.convert_mlt(np.array(self.mlon_list),
583
                                           self.dtime, m2a=False)
584
        np.testing.assert_allclose(self.mlt_out, self.mlt_comp, rtol=1.0e-4)
585
586
    def test_mlt_convert_list_w_times(self):
587
        """Test MLT calculation for data and time arrays"""
588
        self.dtime = [self.dtime for dd in self.mlon_list]
589
        self.mlt_out = aacgmv2.convert_mlt(self.mlon_list, self.dtime,
590
                                           m2a=False)
591
        np.testing.assert_allclose(self.mlt_out, self.mlt_comp, rtol=1.0e-4)
592
593
    def test_mlt_convert_change(self):
594
        """Test that MLT changes with UT"""
595
        self.mlt_out = aacgmv2.convert_mlt(self.mlon_list, self.dtime)
596
        self.mlt_diff = np.array(self.mlt_out) \
597
            - np.array(aacgmv2.convert_mlt(self.mlon_list, self.dtime2))
598
599
        np.testing.assert_allclose(self.mlt_diff, self.diff_comp, rtol=1.0e-4)
600
601
    def test_mlt_convert_multidim_failure(self):
602
        """Test MLT calculation failure for multi-dimensional arrays"""
603
        self.mlon_list = np.full(shape=(3, 2), fill_value=50.0)
604
        with pytest.raises(ValueError):
605
            aacgmv2.convert_mlt(self.mlon_list, self.dtime, m2a=False)
606
607
    def test_mlt_convert_mismatch_failure(self):
608
        """Test MLT calculation failure for mismatched array input"""
609
        with pytest.raises(ValueError):
610
            aacgmv2.convert_mlt(self.mlon_list, [self.dtime, self.dtime2],
611
                                m2a=False)
612
613
614
class TestCoeffPath:
615
616
    def setup(self):
617
        """Runs before every method to create a clean testing setup"""
618
        os.environ['IGRF_COEFFS'] = "default_igrf"
619
        os.environ['AACGM_v2_DAT_PREFIX'] = "default_coeff"
620
        self.ref = {"igrf_file": os.environ['IGRF_COEFFS'],
621
                    "coeff_prefix": os.environ['AACGM_v2_DAT_PREFIX']}
622
623
    def teardown(self):
624
        """Runs after every method to clean up previous testing"""
625
        del self.ref
626
627
    @pytest.mark.parametrize("in_coeff",
628
                             [({}),
629
                              ({"igrf_file": "hi", "coeff_prefix": "bye"}),
630
                              ({"igrf_file": True, "coeff_prefix": True}),
631
                              ({"coeff_prefix": "hi"}),
632
                              ({"igrf_file": "hi"}),
633
                              ({"igrf_file": None, "coeff_prefix": None})])
634
    def test_set_coeff_path(self, in_coeff):
635
        """Test the coefficient path setting using default values"""
636
        # Update the reference key, if needed
637
        for ref_key in in_coeff.keys():
638
            if in_coeff[ref_key] is True or in_coeff[ref_key] is None:
639
                if ref_key == "igrf_file":
640
                    self.ref[ref_key] = aacgmv2.IGRF_COEFFS
641
                elif ref_key == "coeff_prefix":
642
                    self.ref[ref_key] = aacgmv2.AACGM_v2_DAT_PREFIX
643
            else:
644
                self.ref[ref_key] = in_coeff[ref_key]
645
646
        # Run the routine
647
        aacgmv2.wrapper.set_coeff_path(**in_coeff)
648
649
        # Ensure the environment variables were set correctly
650
        if os.environ['IGRF_COEFFS'] != self.ref['igrf_file']:
651
            raise AssertionError("{:} != {:}".format(os.environ['IGRF_COEFFS'],
652
                                                     self.ref['igrf_file']))
653
        if os.environ['AACGM_v2_DAT_PREFIX'] != self.ref['coeff_prefix']:
654
            raise AssertionError("{:} != {:}".format(
655
                os.environ['AACGM_v2_DAT_PREFIX'], self.ref['coeff_prefix']))
656
657
658
class TestHeightReturns:
659
    def setup(self):
660
        """Runs before every method to create a clean testing setup"""
661
        self.code = aacgmv2._aacgmv2.A2G
662
        self.bad_code = aacgmv2._aacgmv2.BADIDEA
663
        self.trace_code = aacgmv2._aacgmv2.TRACE
664
665
    def teardown(self):
666
        """Runs after every method to clean up previous testing"""
667
        del self.code, self.bad_code
668
669
    def test_low_height_good(self):
670
        """ Test to see that a very low height is still accepted"""
671
672
        assert aacgmv2.wrapper.test_height(-1, self.code)
673
674
    def test_high_coeff_bad(self):
675
        """ Test to see that a high altitude for coefficent use fails"""
676
677
        assert not aacgmv2.wrapper.test_height(aacgmv2.high_alt_coeff + 10.0,
678
                                               self.code)
679
680
    def test_high_coeff_good(self):
681
        """ Test a high altitude for coefficent use with badidea """
682
683
        assert aacgmv2.wrapper.test_height(aacgmv2.high_alt_coeff + 10.0,
684
                                           self.bad_code)
685
686
    def test_low_coeff_good(self):
687
        """ Test that a normal height succeeds"""
688
        assert aacgmv2.wrapper.test_height(aacgmv2.high_alt_coeff * 0.5,
689
                                           self.code)
690
691
    def test_high_trace_bad(self):
692
        """ Test that a high trace height fails"""
693
        assert not aacgmv2.wrapper.test_height(aacgmv2.high_alt_trace + 10.0,
694
                                               self.code)
695
696
    def test_low_trace_good(self):
697
        """ Test that a high coefficient height succeeds with trace"""
698
        assert aacgmv2.wrapper.test_height(aacgmv2.high_alt_coeff + 10.0,
699
                                           self.trace_code)
700
701
    def test_high_trace_good(self):
702
        """ Test that a high trace height succeeds with badidea"""
703
        assert aacgmv2.wrapper.test_height(aacgmv2.high_alt_trace + 10.0,
704
                                           self.bad_code)
705
706
707
class TestPyLogging:
708
    def setup(self):
709
        """Runs before every method to create a clean testing setup"""
710
711
        self.lwarn = u""
712
        self.lout = u""
713
        self.log_capture = StringIO()
714
        aacgmv2.logger.addHandler(logging.StreamHandler(self.log_capture))
715
        aacgmv2.logger.setLevel(logging.INFO)
716
717
    def teardown(self):
718
        """Runs after every method to clean up previous testing"""
719
        self.log_capture.close()
720
        del self.lwarn, self.lout, self.log_capture
721
722
    def test_warning_below_ground(self):
723
        """ Test that a warning is issued if height < 0 for height test """
724
        self.lwarn = u"conversion not intended for altitudes < 0 km"
725
726
        aacgmv2.wrapper.test_height(-1, 0)
727
        self.lout = self.log_capture.getvalue()
728
        if self.lout.find(self.lwarn) < 0:
729
            raise AssertionError()
730
731
    def test_warning_magnetosphere(self):
732
        """ Test that a warning is issued if altitude is very high"""
733
        self.lwarn = u"coordinates are not intended for the magnetosphere"
734
735
        aacgmv2.wrapper.test_height(70000, aacgmv2._aacgmv2.TRACE)
736
        self.lout = self.log_capture.getvalue()
737
        if self.lout.find(self.lwarn) < 0:
738
            raise AssertionError()
739
740
    def test_warning_high_coeff(self):
741
        """ Test that a warning is issued if altitude is very high"""
742
        self.lwarn = u"must either use field-line tracing (trace=True"
743
744
        aacgmv2.wrapper.test_height(3000, 0)
745
        self.lout = self.log_capture.getvalue()
746
        if self.lout.find(self.lwarn) < 0:
747
            raise AssertionError()
748
749
    def test_warning_single_loc_in_arr(self):
750
        """ Test that user is warned they should be using simpler routine"""
751
        self.lwarn = u"for a single location, consider using"
752
753
        aacgmv2.convert_latlon_arr(60, 0, 300, dt.datetime(2015, 1, 1, 0, 0, 0))
754
        self.lout = self.log_capture.getvalue()
755
        if self.lout.find(self.lwarn) < 0:
756
            raise AssertionError()
757
758
759
class TestTimeReturns:
760
    def setup(self):
761
        """Runs before every method to create a clean testing setup"""
762
        self.dtime = dt.datetime(2015, 1, 1, 0, 0, 0)
763
        self.dtime2 = dt.datetime(2015, 1, 1, 10, 10, 10)
764
        self.ddate = dt.date(2015, 1, 1)
765
766
    def teardown(self):
767
        """Runs after every method to clean up previous testing"""
768
        del self.dtime, self.ddate, self.dtime2
769
770
    def test_good_time(self):
771
        """ Test to see that a good datetime is accepted"""
772
773
        assert self.dtime == aacgmv2.wrapper.test_time(self.dtime)
774
775
    def test_good_time_with_nonzero_time(self):
776
        """ Test to see that a good datetime with h/m/s is accepted"""
777
778
        assert self.dtime2 == aacgmv2.wrapper.test_time(self.dtime2)
779
780
    def test_good_date(self):
781
        """ Test to see that a good date has a good datetime output"""
782
783
        assert self.dtime == aacgmv2.wrapper.test_time(self.dtime)
784
785
    def test_bad_time(self):
786
        """ Test to see that a warning is raised with a bad time input"""
787
        with pytest.raises(ValueError):
788
            aacgmv2.wrapper.test_time(2015)
789