Passed
Pull Request — master (#37)
by
unknown
05:03
created

test_Apex.test_get_babs()   A

Complexity

Conditions 4

Size

Total Lines 14
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 14
rs 9.75
c 0
b 0
f 0
cc 4
nop 0
1
# -*- coding: utf-8 -*-
2
3
from __future__ import division, absolute_import, unicode_literals
4
5
import datetime as dt
6
import warnings
7
8
import numpy as np
9
import pytest
10
from numpy.testing import assert_allclose
11
12
from apexpy import fortranapex as fa
13
from apexpy import Apex, ApexHeightError, helpers
14
15
16
##############################################################################
17
# NOTE: whenever function outputs are tested against hard-coded numbers,     #
18
# the test results (numbers) were obtained by running the code that is       #
19
# tested. Therefore these tests below only check that nothing changes when   #
20
# refactoring etc., and not if the results are actually correct              #
21
##############################################################################
22
23
24
###============================================================================
25
### Test initiating the Apex class
26
###============================================================================
27
28
29
def test_init_defaults():
30
    Apex()
31
32
33
def test_init_date_int():
34
    apex_out = Apex(date=2015)
35
    assert apex_out.year == 2015
36
37
38
def test_init_date_float():
39
    apex_out = Apex(date=2015.5)
40
    assert apex_out.year == 2015.5
41
42
43
def test_init_date():
44
    date = dt.date(2015, 1, 1)
45
    apex_out = Apex(date=date)
46
    assert apex_out.year == helpers.toYearFraction(date)
47
48
49
def test_init_datetime():
50
    datetime = dt.datetime(2015, 6, 1, 18, 23, 45)
51
    apex_out = Apex(date=datetime)
52
    assert apex_out.year == helpers.toYearFraction(datetime)
53
54
55
def test_init_datafile_IOError():
56
    with pytest.raises(IOError):
57
        Apex(date=2015, datafile='foo/path/to/datafile.blah')
58
59
60
###============================================================================
61
### Test the low-level interfaces to the fortran wrappers
62
###============================================================================
63
64
def test__geo2qd_scalar():
65
    apex_out = Apex(date=2000, refh=300)
66
    for lat in [0, 30, 60, 89]:
67
        for lon in [-179, -90, 0, 90, 180]:
68
            assert_allclose(apex_out._geo2qd(lat, lon, 100),
69
                            fa.apxg2q(lat, lon, 100, 0)[:2])
70
71
72
def test__geo2qd_array():
73
    apex_out = Apex(date=2000, refh=300)
74
    lats, lons = apex_out._geo2qd([[0, 30], [60, 90]], 15,
75
                                  [[100, 200], [300, 400]])
76
    lat1, lon1 = fa.apxg2q(0, 15, 100, 0)[:2]
77
    lat2, lon2 = fa.apxg2q(30, 15, 200, 0)[:2]
78
    lat3, lon3 = fa.apxg2q(60, 15, 300, 0)[:2]
79
    lat4, lon4 = fa.apxg2q(90, 15, 400, 0)[:2]
80
    assert_allclose(lats.astype(float), np.array([[lat1, lat2], [lat3, lat4]],
81
                                                 dtype=float))
82
    assert_allclose(lons.astype(float), np.array([[lon1, lon2], [lon3, lon4]],
83
                                                 dtype=float))
84
85
86
def test__geo2qd_longitude():
87
    apex_out = Apex(date=2000, refh=300)
88
    assert_allclose(apex_out._geo2qd(60, 180, 100),
89
                    fa.apxg2q(60, 180, 100, 0)[:2])
90
    assert_allclose(apex_out._geo2qd(60, -180, 100),
91
                    fa.apxg2q(60, -180, 100, 0)[:2])
92
    assert_allclose(apex_out._geo2qd(60, -180, 100),
93
                    apex_out._geo2qd(60, 180, 100))
94
    for i in range(-5, 5):
95
        for lat in [0, 30, 60, 90]:
96
            assert_allclose(apex_out._geo2qd(lat, 15+i*360, 100),
97
                            fa.apxg2q(lat, 15, 100, 0)[:2])
98
99
100
def test__geo2apex_scalar():
101
    apex_out = Apex(date=2000, refh=300)
102
    for lat in [0, 30, 60, 89]:
103
        for lon in [-179, -90, 0, 90, 180]:
104
            assert_allclose(apex_out._geo2apex(lat, lon, 100),
105
                            fa.apxg2all(lat, lon, 100, 300, 0)[2:4])
106
107
108
def test__geo2apex_array():
109
    apex_out = Apex(date=2000, refh=300)
110
    lats, lons = apex_out._geo2apex([[0, 30], [60, 90]], 15,
111
                                    [[100, 200], [300, 400]])
112
    lat1, lon1 = fa.apxg2all(0, 15, 100, 300, 0)[2:4]
113
    lat2, lon2 = fa.apxg2all(30, 15, 200, 300, 0)[2:4]
114
    lat3, lon3 = fa.apxg2all(60, 15, 300, 300, 0)[2:4]
115
    lat4, lon4 = fa.apxg2all(90, 15, 400, 300, 0)[2:4]
116
    assert_allclose(lats.astype(float), np.array([[lat1, lat2], [lat3, lat4]],
117
                                                 dtype=float))
118
    assert_allclose(lons.astype(float), np.array([[lon1, lon2], [lon3, lon4]],
119
                                                 dtype=float))
120
121
122
def test__geo2apex_longitude():
123
    apex_out = Apex(date=2000, refh=300)
124
    assert_allclose(apex_out._geo2apex(60, 180, 100),
125
                    fa.apxg2all(60, 180, 100, 300, 0)[2:4])
126
    assert_allclose(apex_out._geo2apex(60, -180, 100),
127
                    fa.apxg2all(60, -180, 100, 300, 0)[2:4])
128
    assert_allclose(apex_out._geo2apex(60, -180, 100),
129
                    apex_out._geo2apex(60, 180, 100))
130
    for i in range(-5, 5):
131
        for lat in [0, 30, 60, 90]:
132
            assert_allclose(apex_out._geo2apex(lat, 15+i*360, 100),
133
                            fa.apxg2all(lat, 15, 100, 300, 0)[2:4])
134
135
136
def test__geo2apexall_scalar():
137
    apex_out = Apex(date=2000, refh=300)
138
    for lat in [0, 30, 60, 89]:
139
        for lon in [-179, -90, 0, 90, 180]:
140
            ret1 = apex_out._geo2apexall(lat, lon, 100)
141
            ret2 = fa.apxg2all(lat, lon, 100, 300, 1)
142
            for r1, r2 in zip(ret1, ret2):
143
                assert_allclose(r1, r2)
144
145
146
def test__geo2apexall_array():
147
    apex_out = Apex(date=2000, refh=300)
148
    ret = apex_out._geo2apexall([[0, 30], [60, 90]], 15,
149
                                [[100, 200], [300, 400]])
150
    ret1 = fa.apxg2all(0, 15, 100, 300, 1)
151
    ret2 = fa.apxg2all(30, 15, 200, 300, 1)
152
    ret3 = fa.apxg2all(60, 15, 300, 300, 1)
153
    ret4 = fa.apxg2all(90, 15, 400, 300, 1)
154
    for i in range(len(ret)):
155
        try:
156
            # ret[i] is array of floats
157
            assert_allclose(ret[i].astype(float),
158
                            np.array([[ret1[i], ret2[i]], [ret3[i], ret4[i]]],
159
                                     dtype=float))
160
        except:
161
            # ret[i] is array of arrays
162
            assert_allclose(ret[i][0, 0], ret1[i])
163
            assert_allclose(ret[i][0, 1], ret2[i])
164
            assert_allclose(ret[i][1, 0], ret3[i])
165
            assert_allclose(ret[i][1, 1], ret4[i])
166
167
168
def test__qd2geo_scalar():
169
    apex_out = Apex(date=2000, refh=300)
170
    for lat in [0, 30, 60, 89]:
171
        for lon in [-179, -90, 0, 90, 180]:
172
            for prec in [-1, 1e-2, 1e-10]:
173
                assert_allclose(apex_out._qd2geo(lat, lon, 100, prec),
174
                                fa.apxq2g(lat, lon, 100, prec))
175
176
177
def test__qd2geo_array():
178
    apex_out = Apex(date=2000, refh=300)
179
    lats, lons, errs = apex_out._qd2geo([[0, 30], [60, 90]], 15,
180
                                 [[100, 200], [300, 400]], 1e-2)
181
    lat1, lon1, err1 = fa.apxq2g(0, 15, 100, 1e-2)
182
    lat2, lon2, err2 = fa.apxq2g(30, 15, 200, 1e-2)
183
    lat3, lon3, err3 = fa.apxq2g(60, 15, 300, 1e-2)
184
    lat4, lon4, err4 = fa.apxq2g(90, 15, 400, 1e-2)
185
    assert_allclose(lats.astype(float), np.array([[lat1, lat2], [lat3, lat4]],
186
                                                 dtype=float))
187
    assert_allclose(lons.astype(float), np.array([[lon1, lon2], [lon3, lon4]],
188
                                                 dtype=float))
189
    assert_allclose(errs.astype(float), np.array([[err1, err2], [err3, err4]],
190
                                                 dtype=float))
191
192
193
def test__qd2geo_longitude():
194
    apex_out = Apex(date=2000, refh=300)
195
    assert_allclose(apex_out._qd2geo(60, 180, 100, 1e-2),
196
                    fa.apxq2g(60, 180, 100, 1e-2))
197
    assert_allclose(apex_out._qd2geo(60, -180, 100, 1e-2),
198
                    fa.apxq2g(60, -180, 100, 1e-2))
199
    assert_allclose(apex_out._qd2geo(60, -180, 100, 1e-2),
200
                    apex_out._qd2geo(60, 180, 100, 1e-2))
201
    for i in range(-5, 5):
202
        for lat in [0, 30, 60, 90]:
203
            assert_allclose(apex_out._qd2geo(lat, 15+i*360, 100, 1e-2),
204
                            fa.apxq2g(lat, 15, 100, 1e-2))
205
206
207
def test__basevec_scalar():
208
    apex_out = Apex(date=2000, refh=300)
209
    for lat in [0, 30, 60, 89]:
210
        for lon in [-179, -90, 0, 90, 180]:
211
            assert_allclose(apex_out._basevec(lat, lon, 100),
212
                            fa.apxg2q(lat, lon, 100, 1)[2:4])
213
214
215
def test__basevec_array():
216
    apex_out = Apex(date=2000, refh=300)
217
    f1s, f2s = apex_out._basevec([[0, 30], [60, 90]], 15,
218
                                 [[100, 200], [300, 400]])
219
    f11, f21 = fa.apxg2q(0, 15, 100, 1)[2:4]
220
    f12, f22 = fa.apxg2q(30, 15, 200, 1)[2:4]
221
    f13, f23 = fa.apxg2q(60, 15, 300, 1)[2:4]
222
    f14, f24 = fa.apxg2q(90, 15, 400, 1)[2:4]
223
    assert_allclose(f1s[0, 0], f11)
224
    assert_allclose(f1s[0, 1], f12)
225
    assert_allclose(f1s[1, 0], f13)
226
    assert_allclose(f1s[1, 1], f14)
227
    assert_allclose(f2s[0, 0], f21)
228
    assert_allclose(f2s[0, 1], f22)
229
    assert_allclose(f2s[1, 0], f23)
230
    assert_allclose(f2s[1, 1], f24)
231
232
233
def test__basevec_longitude():
234
    apex_out = Apex(date=2000, refh=300)
235
    assert_allclose(apex_out._basevec(60, 180, 100),
236
                    fa.apxg2q(60, 180, 100, 1)[2:4])
237
    assert_allclose(apex_out._basevec(60, -180, 100),
238
                    fa.apxg2q(60, -180, 100, 1)[2:4])
239
    assert_allclose(apex_out._basevec(60, -180, 100),
240
                    apex_out._basevec(60, 180, 100))
241
    for i in range(-5, 5):
242
        for lat in [0, 30, 60, 90]:
243
            assert_allclose(apex_out._basevec(lat, 15+i*360, 100),
244
                            fa.apxg2q(lat, 15, 100, 1)[2:4])
245
246
247
###============================================================================
248
### Test the convert() method
249
###============================================================================
250
251
252
def test_convert_geo2apex():
253
    apex_out = Apex(date=2000, refh=300)
254
    assert_allclose(apex_out.convert(60, 15, 'geo', 'apex', height=100),
255
                    apex_out.geo2apex(60, 15, 100))
256
257
258
def test_convert_geo2qd():
259
    apex_out = Apex(date=2000, refh=300)
260
    assert_allclose(apex_out.convert(60, 15, 'geo', 'qd', height=100),
261
                    apex_out.geo2qd(60, 15, 100))
262
263
264
def test_convert_geo2mlt_nodate():
265
    apex_out = Apex(date=2000, refh=300)
266
    with pytest.raises(ValueError):
267
        apex_out.convert(60, 15, 'geo', 'mlt')
268
269
270
def test_convert_geo2mlt():
271
    datetime = dt.datetime(2000, 3, 9, 14, 25, 58)
272
    apex_out = Apex(date=2000, refh=300)
273
    assert_allclose(apex_out.convert(60, 15, 'geo', 'mlt', height=100,
274
                                     ssheight=2e5, datetime=datetime)[1],
275
                    apex_out.mlon2mlt(apex_out.geo2apex(60, 15, 100)[1],
276
                                      datetime, ssheight=2e5))
277
278
279
def test_convert_apex2geo():
280
    apex_out = Apex(date=2000, refh=300)
281
    assert_allclose(apex_out.convert(60, 15, 'apex', 'geo', height=100,
282
                                     precision=1e-2),
283
                    apex_out.apex2geo(60, 15, 100, precision=1e-2)[:-1])
284
285
286
def test_convert_apex2qd():
287
    apex_out = Apex(date=2000, refh=300)
288
    assert_allclose(apex_out.convert(60, 15, 'apex', 'qd', height=100),
289
                    apex_out.apex2qd(60, 15, height=100))
290
291
292
def test_convert_apex2mlt():
293
    datetime = dt.datetime(2000, 3, 9, 14, 25, 58)
294
    apex_out = Apex(date=2000, refh=300)
295
    assert_allclose(apex_out.convert(60, 15, 'apex', 'mlt', height=100,
296
                                     datetime=datetime, ssheight=2e5)[1],
297
                    apex_out.mlon2mlt(15, datetime, ssheight=2e5))
298
299
300
def test_convert_qd2geo():
301
    apex_out = Apex(date=2000, refh=300)
302
    assert_allclose(apex_out.convert(60, 15, 'qd', 'geo', height=100,
303
                                     precision=1e-2),
304
                    apex_out.qd2geo(60, 15, 100, precision=1e-2)[:-1])
305
306
307
def test_convert_qd2apex():
308
    apex_out = Apex(date=2000, refh=300)
309
    assert_allclose(apex_out.convert(60, 15, 'qd', 'apex', height=100),
310
                    apex_out.qd2apex(60, 15, height=100))
311
312
313
def test_convert_qd2mlt():
314
    datetime = dt.datetime(2000, 3, 9, 14, 25, 58)
315
    apex_out = Apex(date=2000, refh=300)
316
    assert_allclose(apex_out.convert(60, 15, 'qd', 'mlt', height=100,
317
                              datetime=datetime, ssheight=2e5)[1],
318
                    apex_out.mlon2mlt(15, datetime, ssheight=2e5))
319
320
321
def test_convert_mlt2geo():
322
    datetime = dt.datetime(2000, 3, 9, 14, 25, 58)
323
    apex_out = Apex(date=2000, refh=300)
324
    assert_allclose(apex_out.convert(60, 15, 'mlt', 'geo', height=100,
325
                                     datetime=datetime, precision=1e-2,
326
                                     ssheight=2e5),
327
                    apex_out.apex2geo(60, apex_out.mlt2mlon(15, datetime,
328
                                                            ssheight=2e5), 100,
329
                                      precision=1e-2)[:-1])
330
331
332
def test_convert_mlt2apex():
333
    datetime = dt.datetime(2000, 3, 9, 14, 25, 58)
334
    apex_out = Apex(date=2000, refh=300)
335
    assert_allclose(apex_out.convert(60, 15, 'mlt', 'apex', height=100,
336
                              datetime=datetime, ssheight=2e5),
337
                    (60, apex_out.mlt2mlon(15, datetime, ssheight=2e5)))
338
339
340
def test_convert_mlt2qd():
341
    datetime = dt.datetime(2000, 3, 9, 14, 25, 58)
342
    apex_out = Apex(date=2000, refh=300)
343
    assert_allclose(apex_out.convert(60, 15, 'mlt', 'qd', height=100,
344
                                     datetime=datetime, ssheight=2e5),
345
                    apex_out.apex2qd(60, apex_out.mlt2mlon(15, datetime,
346
                                                           ssheight=2e5),
347
                                     height=100))
348
349
350
def test_convert_invalid_lat():
351
    apex_out = Apex(date=2000, refh=300)
352
    with pytest.raises(ValueError):
353
        apex_out.convert(91, 0, 'geo', 'geo')
354
    with pytest.raises(ValueError):
355
        apex_out.convert(-91, 0, 'geo', 'geo')
356
    apex_out.convert(90, 0, 'geo', 'geo')
357
    apex_out.convert(-90, 0, 'geo', 'geo')
358
359
    assert_allclose(apex_out.convert(90+1e-5, 0, 'geo', 'apex'),
360
                    apex_out.convert(90, 0, 'geo', 'apex'), rtol=0, atol=1e-8)
361
362
363
def test_convert_invalid_transformation():
364
    apex_out = Apex(date=2000, refh=300)
365
    with pytest.raises(NotImplementedError):
366
        apex_out.convert(0, 0, 'foobar', 'geo')
367
    with pytest.raises(NotImplementedError):
368
        apex_out.convert(0, 0, 'geo', 'foobar')
369
370
371
###============================================================================
372
### Test the geo2apex() method
373
###============================================================================
374
375
376
def test_geo2apex():
377
    apex2000 = Apex(date=2000, refh=300)
378
    lat, lon = apex2000.geo2apex(60, 15, 100)
379
    assert_allclose((lat, lon), apex2000._geo2apex(60, 15, 100))
380
    assert type(lat) != np.ndarray
381
    assert type(lon) != np.ndarray
382
383
384
def test_geo2apex_vectorization():
385
    apex_out = Apex(date=2000, refh=300)
386
    assert apex_out.geo2apex([60, 60], 15, 100)[0].shape == (2,)
387
    assert apex_out.geo2apex(60, [15, 15], 100)[0].shape == (2,)
388
    assert apex_out.geo2apex(60, 15, [100, 100])[0].shape == (2,)
389
390
391
def test_geo2apex_invalid_lat():
392
    apex_out = Apex(date=2000, refh=300)
393
    with pytest.raises(ValueError):
394
        apex_out.geo2apex(91, 0, 0)
395
    with pytest.raises(ValueError):
396
        apex_out.geo2apex(-91, 0, 0)
397
    apex_out.geo2apex(90, 0, 0)
398
    apex_out.geo2apex(-90, 0, 0)
399
400
    assert_allclose(apex_out.geo2apex(90+1e-5, 0, 0),
401
                    apex_out.geo2apex(90, 0, 0), rtol=0, atol=1e-8)
402
403
404
def test_geo2apex_undefined_warning():
405
    """Test warning and fill values for an undefined location
406
    """
407
    with warnings.catch_warnings(record=True) as wmsg:
408
        apex2000 = Apex(date=2000, refh=10000)
409
        ret = apex2000.geo2apex(0, 0, 0)
410
411
    assert ret[0] == -9999
412
    assert len(wmsg) == 1
413
    assert issubclass(wmsg[-1].category, UserWarning)
414
    assert 'set to -9999 where' in str(wmsg[-1].message)
415
416
417
###============================================================================
418
### Test the apex2geo() method
419
###============================================================================
420
421
422
def test_apex2geo():
423
    apex_out = Apex(date=2000, refh=300)
424
    lat, lon, error = apex_out.apex2geo(60, 15, 100, precision=1e-2)
425
    assert_allclose((lat, lon, error),
426
                    apex_out.qd2geo(*apex_out.apex2qd(60, 15, 100), height=100,
427
                             precision=1e-2))
428
    assert type(lat) != np.ndarray
429
    assert type(lon) != np.ndarray
430
    assert type(error) != np.ndarray
431
432
433
def test_apex2geo_vectorization():
434
    apex_out = Apex(date=2000, refh=300)
435
    assert apex_out.apex2geo([60, 60], 15, 100)[0].shape == (2,)
436
    assert apex_out.apex2geo(60, [15, 15], 100)[0].shape == (2,)
437
    assert apex_out.apex2geo(60, 15, [100, 100])[0].shape == (2,)
438
439
440
def test_apex2geo_invalid_lat():
441
    apex_out = Apex(date=2000, refh=300)
442
    with pytest.raises(ValueError):
443
        apex_out.apex2geo(91, 0, 0, 1e-2)
444
    with pytest.raises(ValueError):
445
        apex_out.apex2geo(-91, 0, 0, 1e-2)
446
    apex_out.apex2geo(90, 0, 0, 1e-2)
447
    apex_out.apex2geo(-90, 0, 0, 1e-2)
448
449
    assert_allclose(apex_out.apex2geo(90+1e-5, 0, 0, 1e-2),
450
                    apex_out.apex2geo(90, 0, 0, 1e-2), rtol=0, atol=1e-8)
451
452
453
###============================================================================
454
### Test the geo2qd() method
455
###============================================================================
456
457
458
def test_geo2qd():
459
    apex_out = Apex(date=2000, refh=300)
460
    lat, lon = apex_out.geo2qd(60, 15, 100)
461
    assert_allclose((lat, lon), apex_out._geo2qd(60, 15, 100))
462
    assert type(lat) != np.ndarray
463
    assert type(lon) != np.ndarray
464
465
466
def test_geo2qd_vectorization():
467
    apex_out = Apex(date=2000, refh=300)
468
    assert apex_out.geo2qd([60, 60], 15, 100)[0].shape == (2,)
469
    assert apex_out.geo2qd(60, [15, 15], 100)[0].shape == (2,)
470
    assert apex_out.geo2qd(60, 15, [100, 100])[0].shape == (2,)
471
472
473
def test_geo2qd_invalid_lat():
474
    apex_out = Apex(date=2000, refh=300)
475
    with pytest.raises(ValueError):
476
        apex_out.geo2qd(91, 0, 0)
477
    with pytest.raises(ValueError):
478
        apex_out.geo2qd(-91, 0, 0)
479
    apex_out.geo2qd(90, 0, 0)
480
    apex_out.geo2qd(-90, 0, 0)
481
482
    assert_allclose(apex_out.geo2qd(90+1e-5, 0, 0), apex_out.geo2qd(90, 0, 0),
483
                    rtol=0, atol=1e-8)
484
485
486
###============================================================================
487
### Test the qd2geo() method
488
###============================================================================
489
490
491
def test_qd2geo():
492
    apex_out = Apex(date=2000, refh=300)
493
    lat, lon, error = apex_out.qd2geo(60, 15, 100, precision=1e-2)
494
    assert_allclose((lat, lon, error), apex_out._qd2geo(60, 15, 100, 1e-2))
495
    assert type(lat) != np.ndarray
496
    assert type(lon) != np.ndarray
497
    assert type(error) != np.ndarray
498
499
500
def test_qd2geo_vectorization():
501
    apex_out = Apex(date=2000, refh=300)
502
    assert apex_out.qd2geo([60, 60], 15, 100)[0].shape == (2,)
503
    assert apex_out.qd2geo(60, [15, 15], 100)[0].shape == (2,)
504
    assert apex_out.qd2geo(60, 15, [100, 100])[0].shape == (2,)
505
506
507
def test_qd2geo_invalid_lat():
508
    apex_out = Apex(date=2000, refh=300)
509
    with pytest.raises(ValueError):
510
        apex_out.qd2geo(91, 0, 0, precision=1e-2)
511
    with pytest.raises(ValueError):
512
        apex_out.qd2geo(-91, 0, 0, precision=1e-2)
513
    apex_out.qd2geo(90, 0, 0, precision=1e-2)
514
    apex_out.qd2geo(-90, 0, 0, precision=1e-2)
515
516
    assert_allclose(apex_out.qd2geo(90+1e-5, 0, 0, 1e-2),
517
                    apex_out.qd2geo(90, 0, 0, 1e-2), rtol=0, atol=1e-8)
518
519
520
###============================================================================
521
### Test the apex2qd() method
522
###============================================================================
523
524
525
def test_apex2qd():
526
    apex_out = Apex(date=2000, refh=300)
527
    lat, lon = apex_out.apex2qd(60, 15, 100)
528
    assert_allclose((lat, lon),
529
                    [60.498401, 15])
530
    assert type(lat) != np.ndarray
531
    assert type(lon) != np.ndarray
532
533
534
def test_apex2qd_vectorization():
535
    apex_out = Apex(date=2000, refh=300)
536
    assert apex_out.apex2qd([60, 60], 15, 100)[0].shape == (2,)
537
    assert apex_out.apex2qd(60, [15, 15], 100)[0].shape == (2,)
538
    assert apex_out.apex2qd(60, 15, [100, 100])[0].shape == (2,)
539
540
541
def test_apex2qd_invalid_lat():
542
    apex_out = Apex(date=2000, refh=300)
543
    with pytest.raises(ValueError):
544
        apex_out.apex2qd(91, 0, 0)
545
    with pytest.raises(ValueError):
546
        apex_out.apex2qd(-91, 0, 0)
547
    apex_out.apex2qd(90, 0, 0)
548
    apex_out.apex2qd(-90, 0, 0)
549
550
    assert_allclose(apex_out.apex2qd(90+1e-5, 0, 0), apex_out.apex2qd(90, 0, 0),
551
                    rtol=0, atol=1e-8)
552
553
554
def test_apex2qd_apexheight_close():
555
    apex_out = Apex(date=2000, refh=300)
556
    apex_out.apex2qd(0, 15, 300+1e-6)
557
558
559
def test_apex2qd_apexheight_over():
560
    apex_out = Apex(date=2000, refh=300)
561
    with pytest.raises(ApexHeightError):
562
        apex_out.apex2qd(0, 15, 301)
563
564
565
###============================================================================
566
### Test the qd2apex() method
567
###============================================================================
568
569
570
def test_qd2apex():
571
    apex_out = Apex(date=2000, refh=300)
572
    lat, lon = apex_out.qd2apex(60, 15, 100)
573
    assert_allclose((lat, lon),
574
                    [59.491381, 15])
575
    assert type(lat) != np.ndarray
576
    assert type(lon) != np.ndarray
577
578
579
def test_qd2apex_vectorization():
580
    apex_out = Apex(date=2000, refh=300)
581
    assert apex_out.qd2apex([60, 60], 15, 100)[0].shape == (2,)
582
    assert apex_out.qd2apex(60, [15, 15], 100)[0].shape == (2,)
583
    assert apex_out.qd2apex(60, 15, [100, 100])[0].shape == (2,)
584
585
586
def test_qd2apex_invalid_lat():
587
    apex_out = Apex(date=2000, refh=300)
588
    with pytest.raises(ValueError):
589
        apex_out.qd2apex(91, 0, 0)
590
    with pytest.raises(ValueError):
591
        apex_out.qd2apex(-91, 0, 0)
592
    apex_out.qd2apex(90, 0, 0)
593
    apex_out.qd2apex(-90, 0, 0)
594
595
    assert_allclose(apex_out.qd2apex(90+1e-5, 0, 0), apex_out.qd2apex(90, 0, 0),
596
                    rtol=0, atol=1e-8)
597
598
599
def test_qd2apex_apexheight_close():
600
    apex_out = Apex(date=2000, refh=300)
601
    assert_allclose(apex_out.qd2apex(0, 15, 300-1e-5),
602
                    apex_out.qd2apex(0, 15, 300))
603
604
605
def test_qd2apex_apexheight_over():
606
    apex_out = Apex(date=2000, refh=300)
607
    with pytest.raises(ApexHeightError):
608
        apex_out.qd2apex(0, 15, 299)
609
610
611
###============================================================================
612
### Test mlon2mlt()
613
###============================================================================
614
615
616
def test_mlon2mlt_scalar():
617
    apex_out = Apex(date=2000, refh=300)
618
    mlon = apex_out.mlon2mlt(0, dt.datetime(2000, 2, 3, 4, 5, 6))
619
    assert_allclose(mlon, 23.019629923502603)
620
    assert type(mlon) != np.ndarray
621
622
623
def test_mlon2mlt_ssheight():
624
    apex_out = Apex(date=2000, refh=300)
625
    mlt = apex_out.mlon2mlt(0, dt.datetime(2000, 2, 3, 4, 5, 6),
626
                            ssheight=50*2000)
627
    assert_allclose(mlt, 23.026712036132814)
628
629
630
def test_mlon2mlt_1Darray():
631
    apex_out = Apex(date=2000, refh=300)
632
    assert_allclose(apex_out.mlon2mlt([0, 180],
633
                                      dt.datetime(2000, 2, 3, 4, 5, 6)),
634
                    [23.019261, 11.019261], rtol=1e-4)
635
636
637
def test_mlon2mlt_2Darray():
638
    apex_out = Apex(date=2000, refh=300)
639
    assert_allclose(apex_out.mlon2mlt([[0, 180], [0, 180]],
640
                               dt.datetime(2000, 2, 3, 4, 5, 6)),
641
                    [[23.019261, 11.019261], [23.019261, 11.019261]], rtol=1e-4)
642
643
644
def test_mlon2mlt_diffdates():
645
    apex_out = Apex(date=2000, refh=300)
646
    dtime1 = dt.datetime(2000, 2, 3, 4, 5, 6)
647
    dtime2 = dt.datetime(2000, 2, 3, 5, 5, 6)
648
    assert apex_out.mlon2mlt(0, dtime1) != apex_out.mlon2mlt(0, dtime2)
649
650
651
def test_mlon2mlt_offset():
652
    apex_out = Apex(date=2000, refh=300)
653
    date = dt.datetime(2000, 2, 3, 4, 5, 6)
654
    assert_allclose(apex_out.mlon2mlt(0, date),
655
                    apex_out.mlon2mlt(-15, date) + 1)
656
    assert_allclose(apex_out.mlon2mlt(0, date),
657
                    apex_out.mlon2mlt(-10*15, date) + 10)
658
659
660
def test_mlon2mlt_range():
661
    apex_out = Apex(date=2000, refh=300)
662
    assert_allclose(apex_out.mlon2mlt(range(0, 361, 30),
663
                               dt.datetime(2000, 2, 3, 4, 5, 6)),
664
                    [23.01963, 1.01963, 3.01963, 5.01963, 7.01963,
665
                     9.01963, 11.01963, 13.01963, 15.01963, 17.01963,
666
                     19.01963, 21.01963, 23.01963],
667
                    rtol=1e-4)
668
669
670
###============================================================================
671
### Test mlt2mlon()
672
###============================================================================
673
674
675
def test_mlt2mlon_scalar():
676
    apex_out = Apex(date=2000, refh=300)
677
    mlt = apex_out.mlt2mlon(0, dt.datetime(2000, 2, 3, 4, 5, 6))
678
    assert_allclose(mlt, 14.705551147460938)
679
    assert type(mlt) != np.ndarray
680
681
682
def test_mlt2mlon_ssheight():
683
    apex_out = Apex(date=2000, refh=300)
684
    mlt = apex_out.mlt2mlon(0, dt.datetime(2000, 2, 3, 4, 5, 6),
685
                            ssheight=50*2000)
686
    assert_allclose(mlt, 14.599319458007812)
687
688
689
def test_mlt2mlon_1Darray():
690
    apex_out = Apex(date=2000, refh=300)
691
    assert_allclose(apex_out.mlt2mlon([0, 12],
692
                                      dt.datetime(2000, 2, 3, 4, 5, 6)),
693
                    [14.705551, 194.705551], rtol=1e-4)
694
695
696
def test_mlt2mlon_2Darray():
697
    apex_out = Apex(date=2000, refh=300)
698
    assert_allclose(apex_out.mlt2mlon([[0, 12], [0, 12]],
699
                               dt.datetime(2000, 2, 3, 4, 5, 6)),
700
                    [[14.705551, 194.705551], [14.705551, 194.705551]],
701
                    rtol=1e-4)
702
703
704
def test_mlt2mlon_diffdates():
705
    apex_out = Apex(date=2000, refh=300)
706
    dtime1 = dt.datetime(2000, 2, 3, 4, 5, 6)
707
    dtime2 = dt.datetime(2000, 2, 3, 5, 5, 6)
708
    assert apex_out.mlt2mlon(0, dtime1) != apex_out.mlt2mlon(0, dtime2)
709
710
711
def test_mlt2mlon_offset():
712
    apex_out = Apex(date=2000, refh=300)
713
    date = dt.datetime(2000, 2, 3, 4, 5, 6)
714
    assert_allclose(apex_out.mlt2mlon(0, date), apex_out.mlt2mlon(1, date) - 15)
715
    assert_allclose(apex_out.mlt2mlon(0, date),
716
                    apex_out.mlt2mlon(10, date) - 150)
717
718
719
def test_mlt2mlon_range():
720
    apex_out = Apex(date=2000, refh=300)
721
    assert_allclose(apex_out.mlt2mlon(range(0, 25, 2),
722
                               dt.datetime(2000, 2, 3, 4, 5, 6)),
723
                    [14.705551, 44.705551, 74.705551, 104.705551, 134.705551,
724
                     164.705551, 194.705551, 224.705551, 254.705551, 284.705551,
725
                     314.705551, 344.705551, 14.705551],
726
                    rtol=1e-4)
727
728
729
###============================================================================
730
### Test mlt/mlon back and forth
731
###============================================================================
732
733
734
def test_mlon2mlt2mlon():
735
    apex_out = Apex(date=2000, refh=300)
736
    date = dt.datetime(2000, 2, 3, 4, 5, 6)
737
    assert_allclose(apex_out.mlon2mlt(apex_out.mlt2mlon(0, date), date), 0)
738
    assert_allclose(apex_out.mlon2mlt(apex_out.mlt2mlon(6, date), date), 6)
739
    assert_allclose(apex_out.mlon2mlt(apex_out.mlt2mlon(12, date), date), 12)
740
    assert_allclose(apex_out.mlon2mlt(apex_out.mlt2mlon(18, date), date), 18)
741
    assert_allclose(apex_out.mlon2mlt(apex_out.mlt2mlon(24, date), date), 0)
742
743
744
def test_mlt2mlon2mlt():
745
    apex_out = Apex(date=2000, refh=300)
746
    date = dt.datetime(2000, 2, 3, 4, 5, 6)
747
    assert_allclose(apex_out.mlt2mlon(apex_out.mlon2mlt(0, date), date), 0)
748
    assert_allclose(apex_out.mlt2mlon(apex_out.mlon2mlt(90, date), date), 90)
749
    assert_allclose(apex_out.mlt2mlon(apex_out.mlon2mlt(180, date), date), 180)
750
    assert_allclose(apex_out.mlt2mlon(apex_out.mlon2mlt(270, date), date), 270)
751
    assert_allclose(apex_out.mlt2mlon(apex_out.mlon2mlt(360, date), date), 0)
752
753
754
###============================================================================
755
### Test the map_to_height() method
756
###============================================================================
757
758
759
def test_map_to_height():
760
    apex_out = Apex(date=2000, refh=300)
761
    assert_allclose(apex_out.map_to_height(60, 15, 100, 10000, conjugate=False,
762
                                    precision=1e-10),
763
                    (31.841459274291992, 17.916629791259766, 0))
764
    assert_allclose(apex_out.map_to_height(30, 170, 100, 500, conjugate=False,
765
                                    precision=1e-2),
766
                    (25.727252960205078, 169.60546875, 0.00017655163537710905))
767
768
769
def test_map_to_height_same_height():
770
    apex_out = Apex(date=2000, refh=300)
771
    assert_allclose(apex_out.map_to_height(60, 15, 100, 100, conjugate=False,
772
                                    precision=1e-10),
773
                    (60, 15, 3.4150946248701075e-6), rtol=1e-5)
774
775
776
def test_map_to_height_conjugate():
777
    """Test results of map_to_height using conjugacy
778
    """
779
    apex2000 = Apex(date=2000, refh=300)
780
    assert_allclose(apex2000.map_to_height(60, 15, 100, 10000, conjugate=True,
781
                                           precision=1e-10),
782
                    (-25.424892425537109, 27.310417175292969,
783
                     1.2074182222931995e-6), atol=1e-6)
784
    assert_allclose(apex2000.map_to_height(30, 170, 100, 500, conjugate=True,
785
                                           precision=1e-2),
786
                    (-13.76642894744873, 164.24259948730469,
787
                     0.00056820799363777041), atol=1e-6)
788
789
790
def test_map_to_height_vectorization():
791
    apex_out = Apex(date=2000, refh=300)
792
    assert_allclose(apex_out.map_to_height([60, 60], 15, 100, 100),
793
                    ([60]*2, [15]*2, [3.4150946248701075e-6]*2), rtol=1e-5)
794
    assert_allclose(apex_out.map_to_height(60, [15, 15], 100, 100),
795
                    ([60]*2, [15]*2, [3.4150946248701075e-6]*2), rtol=1e-5)
796
    assert_allclose(apex_out.map_to_height(60, 15, [100, 100], 100),
797
                    ([60]*2, [15]*2, [3.4150946248701075e-6]*2), rtol=1e-5)
798
    assert_allclose(apex_out.map_to_height(60, 15, 100, [100, 100]),
799
                    ([60]*2, [15]*2, [3.4150946248701075e-6]*2), rtol=1e-5)
800
801
802
def test_map_to_height_ApexHeightError():
803
    apex_out = Apex(date=2000, refh=300)
804
    with pytest.raises(ApexHeightError):
805
        apex_out.map_to_height(0, 15, 100, 10000)
806
807
808
###============================================================================
809
### Test the map_E_to_height() method
810
###============================================================================
811
812
813 View Code Duplication
def test_map_E_to_height():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
814
    apex_out = Apex(date=2000, refh=300)
815
    out_60_15_100_500 = [0.7115211, 2.3562392, 0.57259707]
816
    out_60_15_100_500_234 = [1.560284, 3.439154, 0.782339]
817
    out_60_15_100_1000 = [0.677964, 2.089811, 0.558601]
818
    out_60_15_200_500 = [0.723773, 2.427366, 0.590826]
819
    out_60_30_100_500 = [0.686265, 2.375296, 0.600594]
820
    out_70_15_100_500 = [0.727605, 2.180817, 0.291414]
821
822
    # scalar
823
    assert_allclose(apex_out.map_E_to_height(60, 15, 100, 500, [1, 2, 3]),
824
                    out_60_15_100_500, rtol=1e-5)
825
    assert_allclose(apex_out.map_E_to_height(60, 15, 100, 500, [2, 3, 4]),
826
                    out_60_15_100_500_234, rtol=1e-5)
827
    assert_allclose(apex_out.map_E_to_height(60, 15, 100, 1000, [1, 2, 3]),
828
                    out_60_15_100_1000, rtol=1e-5)
829
    assert_allclose(apex_out.map_E_to_height(60, 15, 200, 500, [1, 2, 3]),
830
                    out_60_15_200_500, rtol=1e-5)
831
    assert_allclose(apex_out.map_E_to_height(60, 30, 100, 500, [1, 2, 3]),
832
                    out_60_30_100_500, rtol=1e-5)
833
    assert_allclose(apex_out.map_E_to_height(70, 15, 100, 500, [1, 2, 3]),
834
                    out_70_15_100_500, rtol=1e-5)
835
836
    # vectorize lat
837
    assert_allclose(apex_out.map_E_to_height([60, 70], 15, 100, 500,
838
                                      np.array([[1, 2, 3]]*2).T),
839
                    np.array([out_60_15_100_500, out_70_15_100_500]).T,
840
                    rtol=1e-5)
841
842
    # vectorize lon
843
    assert_allclose(apex_out.map_E_to_height(60, [15, 30], 100, 500,
844
                                      np.array([[1, 2, 3]]*2).T),
845
                    np.array([out_60_15_100_500, out_60_30_100_500]).T,
846
                    rtol=1e-5)
847
848
    # vectorize height
849
    assert_allclose(apex_out.map_E_to_height(60, 15, [100, 200], 500,
850
                                      np.array([[1, 2, 3]]*2).T),
851
                    np.array([out_60_15_100_500, out_60_15_200_500]).T,
852
                    rtol=1e-5)
853
854
    # vectorize newheight
855
    assert_allclose(apex_out.map_E_to_height(60, 15, 100, [500, 1000],
856
                                      np.array([[1, 2, 3]]*2).T),
857
                    np.array([out_60_15_100_500, out_60_15_100_1000]).T,
858
                    rtol=1e-5)
859
860
    # vectorize E
861
    assert_allclose(apex_out.map_E_to_height(60, 15, 100, 500,
862
                                      np.array([[1, 2, 3], [2, 3, 4]]).T),
863
                    np.array([out_60_15_100_500, out_60_15_100_500_234]).T,
864
                    rtol=1e-5)
865
866
867
###============================================================================
868
### Test the map_V_to_height() method
869
###============================================================================
870
871
872 View Code Duplication
def test_map_V_to_height():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
873
    apex_out = Apex(date=2000, refh=300)
874
    out_60_15_100_500 = [0.819719, 2.845114, 0.695437]
875
    out_60_15_100_500_234 = [1.830277, 4.14345, 0.947624]
876
    out_60_15_100_1000 = [0.924577, 3.149964, 0.851343]
877
    out_60_15_200_500 = [0.803882, 2.793206, 0.682839]
878
    out_60_30_100_500 = [0.761412, 2.878837, 0.736549]
879
    out_70_15_100_500 = [0.846819, 2.592572, 0.347919]
880
881
    # scalar
882
    assert_allclose(apex_out.map_V_to_height(60, 15, 100, 500, [1, 2, 3]),
883
                    out_60_15_100_500, rtol=1e-5)
884
    assert_allclose(apex_out.map_V_to_height(60, 15, 100, 500, [2, 3, 4]),
885
                    out_60_15_100_500_234, rtol=1e-5)
886
    assert_allclose(apex_out.map_V_to_height(60, 15, 100, 1000, [1, 2, 3]),
887
                    out_60_15_100_1000, rtol=1e-5)
888
    assert_allclose(apex_out.map_V_to_height(60, 15, 200, 500, [1, 2, 3]),
889
                    out_60_15_200_500, rtol=1e-5)
890
    assert_allclose(apex_out.map_V_to_height(60, 30, 100, 500, [1, 2, 3]),
891
                    out_60_30_100_500, rtol=1e-5)
892
    assert_allclose(apex_out.map_V_to_height(70, 15, 100, 500, [1, 2, 3]),
893
                    out_70_15_100_500, rtol=1e-5)
894
895
    # vectorize lat
896
    assert_allclose(apex_out.map_V_to_height([60, 70], 15, 100, 500,
897
                                             np.array([[1, 2, 3]]*2).T),
898
                    np.array([out_60_15_100_500, out_70_15_100_500]).T,
899
                    rtol=1e-5)
900
901
    # vectorize lon
902
    assert_allclose(apex_out.map_V_to_height(60, [15, 30], 100, 500,
903
                                             np.array([[1, 2, 3]]*2).T),
904
                    np.array([out_60_15_100_500, out_60_30_100_500]).T,
905
                    rtol=1e-5)
906
907
    # vectorize height
908
    assert_allclose(apex_out.map_V_to_height(60, 15, [100, 200], 500,
909
                                             np.array([[1, 2, 3]]*2).T),
910
                    np.array([out_60_15_100_500, out_60_15_200_500]).T,
911
                    rtol=1e-5)
912
913
    # vectorize newheight
914
    assert_allclose(apex_out.map_V_to_height(60, 15, 100, [500, 1000],
915
                                             np.array([[1, 2, 3]]*2).T),
916
                    np.array([out_60_15_100_500, out_60_15_100_1000]).T,
917
                    rtol=1e-5)
918
919
    # vectorize E
920
    assert_allclose(apex_out.map_V_to_height(60, 15, 100, 500,
921
                                             np.array([[1, 2, 3],
922
                                                       [2, 3, 4]]).T),
923
                    np.array([out_60_15_100_500, out_60_15_100_500_234]).T,
924
                    rtol=1e-5)
925
926
927
###============================================================================
928
### Test basevectors_qd()
929
###============================================================================
930
931
932
# test coords
933
934
def test_basevectors_qd_scalar_geo():
935
    apex_out = Apex(date=2000, refh=300)
936
    assert_allclose(apex_out.basevectors_qd(60, 15, 100, coords='geo'),
937
                    apex_out._basevec(60, 15, 100))
938
939
940
def test_basevectors_qd_scalar_apex():
941
    apex_out = Apex(date=2000, refh=300)
942
    glat, glon, _ = apex_out.apex2geo(60, 15, 100, precision=1e-2)
943
    assert_allclose(apex_out.basevectors_qd(60, 15, 100, coords='apex',
944
                                            precision=1e-2),
945
                    apex_out._basevec(glat, glon, 100))
946
947
948
def test_basevectors_qd_scalar_qd():
949
    apex_out = Apex(date=2000, refh=300)
950
    glat, glon, _ = apex_out.qd2geo(60, 15, 100, precision=1e-2)
951
    assert_allclose(apex_out.basevectors_qd(60, 15, 100, coords='qd',
952
                                            precision=1e-2),
953
                    apex_out._basevec(glat, glon, 100))
954
955
# test shapes and vectorization of arguments
956
957
def test_basevectors_qd_scalar_shape():
958
    apex_out = Apex(date=2000, refh=300)
959
    ret = apex_out.basevectors_qd(60, 15, 100)
960
    for r in ret:
961
        assert r.shape == (2,)
962
963
964
def test_basevectors_qd_vectorization():
965
    apex_out = Apex(date=2000, refh=300)
966
    ret = apex_out.basevectors_qd([60, 60, 60, 60], 15, 100, coords='geo')
967
    for r in ret:
968
        assert r.shape == (2, 4)
969
    ret = apex_out.basevectors_qd(60, [15, 15, 15, 15], 100, coords='geo')
970
    for r in ret:
971
        assert r.shape == (2, 4)
972
    ret = apex_out.basevectors_qd(60, 15, [100, 100, 100, 100], coords='geo')
973
    for r in ret:
974
        assert r.shape == (2, 4)
975
976
977
# test array return values
978
979
def test_basevectors_qd_array():
980
    apex_out = Apex(date=2000, refh=300)
981
    f1, f2 = apex_out.basevectors_qd([0, 30], 15, 100, coords='geo')
982
    f1_lat0, f2_lat0 = apex_out._basevec(0, 15, 100)
983
    f1_lat30, f2_lat30 = apex_out._basevec(30, 15, 100)
984
    assert_allclose(f1[:, 0], f1_lat0)
985
    assert_allclose(f2[:, 0], f2_lat0)
986
    assert_allclose(f1[:, 1], f1_lat30)
987
    assert_allclose(f2[:, 1], f2_lat30)
988
989
990
###============================================================================
991
### Test basevectors_apex()
992
###============================================================================
993
994
995
# test against return from _geo2apexall for different coords
996
997
def test_basevectors_apex_scalar_geo():
998
    apex_out = Apex(date=2000, refh=300)
999
1000
    (f1, f2, f3, g1, g2, g3, d1, d2, d3, e1, e2,
1001
     e3) = apex_out.basevectors_apex(60, 15, 100, coords='geo')
1002
1003
    (_, _, _, _, f1_, f2_, _, d1_, d2_, d3_, _, e1_, e2_,
1004
     e3_) = apex_out._geo2apexall(60, 15, 100)
1005
1006
    assert_allclose(f1, f1_)
1007
    assert_allclose(f2, f2_)
1008
    assert_allclose(d1, d1_)
1009
    assert_allclose(d2, d2_)
1010
    assert_allclose(d3, d3_)
1011
    assert_allclose(e1, e1_)
1012
    assert_allclose(e2, e2_)
1013
    assert_allclose(e3, e3_)
1014
1015
1016 View Code Duplication
def test_basevectors_apex_scalar_apex():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
1017
    apex_out = Apex(date=2000, refh=300)
1018
1019
    (f1, f2, f3, g1, g2, g3, d1, d2, d3, e1, e2,
1020
     e3) = apex_out.basevectors_apex(60, 15, 100, coords='apex', precision=1e-2)
1021
1022
    glat, glon, _ = apex_out.apex2geo(60, 15, 100, precision=1e-2)
1023
    (_, _, _, _, f1_, f2_, _, d1_, d2_, d3_, _, e1_, e2_,
1024
     e3_) = apex_out._geo2apexall(glat, glon, 100)
1025
1026
    assert_allclose(f1, f1_)
1027
    assert_allclose(f2, f2_)
1028
    assert_allclose(d1, d1_)
1029
    assert_allclose(d2, d2_)
1030
    assert_allclose(d3, d3_)
1031
    assert_allclose(e1, e1_)
1032
    assert_allclose(e2, e2_)
1033
    assert_allclose(e3, e3_)
1034
1035
1036 View Code Duplication
def test_basevectors_apex_scalar_qd():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
1037
    apex_out = Apex(date=2000, refh=300)
1038
1039
    (f1, f2, f3, g1, g2, g3, d1, d2, d3, e1, e2,
1040
     e3) = apex_out.basevectors_apex(60, 15, 100, coords='qd', precision=1e-2)
1041
1042
    glat, glon, _ = apex_out.qd2geo(60, 15, 100, precision=1e-2)
1043
    (_, _, _, _, f1_, f2_, _, d1_, d2_, d3_, _, e1_, e2_,
1044
     e3_) = apex_out._geo2apexall(glat, glon, 100)
1045
1046
    assert_allclose(f1, f1_)
1047
    assert_allclose(f2, f2_)
1048
    assert_allclose(d1, d1_)
1049
    assert_allclose(d2, d2_)
1050
    assert_allclose(d3, d3_)
1051
    assert_allclose(e1, e1_)
1052
    assert_allclose(e2, e2_)
1053
    assert_allclose(e3, e3_)
1054
1055
1056
# test shapes and vectorization of arguments
1057
1058
def test_basevectors_apex_scalar_shape():
1059
    apex_out = Apex(date=2000, refh=300)
1060
    ret = apex_out.basevectors_apex(60, 15, 100, precision=1e-2)
1061
    for r in ret[:2]:
1062
        assert r.shape == (2,)
1063
    for r in ret[2:]:
1064
        assert r.shape == (3,)
1065
1066
1067
def test_basevectors_apex_vectorization():
1068
    apex_out = Apex(date=2000, refh=300)
1069
    ret = apex_out.basevectors_apex([60, 60, 60, 60], 15, 100)
1070
    for r in ret[:2]:
1071
        assert r.shape == (2, 4)
1072
    for r in ret[2:]:
1073
        assert r.shape == (3, 4)
1074
    ret = apex_out.basevectors_apex(60, [15, 15, 15, 15], 100)
1075
    for r in ret[:2]:
1076
        assert r.shape == (2, 4)
1077
    for r in ret[2:]:
1078
        assert r.shape == (3, 4)
1079
    ret = apex_out.basevectors_apex(60, 15, [100, 100, 100, 100])
1080
    for r in ret[:2]:
1081
        assert r.shape == (2, 4)
1082
    for r in ret[2:]:
1083
        assert r.shape == (3, 4)
1084
1085
1086
# test correct vectorization of height
1087 View Code Duplication
def test_basevectors_apex_vectorization_height():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
1088
    apex_out = Apex(date=2000, refh=0)
1089
    (f1, f2, f3, g1, g2, g3, d1, d2, d3, e1, e2,
1090
     e3) = apex_out.basevectors_apex(60, 15, [200, 400], coords='geo')
1091
    (_, _, _, _, f1_1, f2_1, _, d1_1, d2_1, d3_1, _, e1_1, e2_1,
1092
     e3_1) = apex_out._geo2apexall(60, 15, 200)
1093
    (_, _, _, _, f1_2, f2_2, _, d1_2, d2_2, d3_2, _, e1_2, e2_2,
1094
     e3_2) = apex_out._geo2apexall(60, 15, 400)
1095
1096
    assert_allclose(f1[:, 0], f1_1)
1097
    assert_allclose(f2[:, 0], f2_1)
1098
    assert_allclose(d1[:, 0], d1_1)
1099
    assert_allclose(d2[:, 0], d2_1)
1100
    assert_allclose(d3[:, 0], d3_1)
1101
    assert_allclose(e1[:, 0], e1_1)
1102
    assert_allclose(e2[:, 0], e2_1)
1103
    assert_allclose(e3[:, 0], e3_1)
1104
1105
    assert_allclose(f3[:, 0], np.array([-0.088671, -0.018272, 0.993576]),
1106
                    rtol=1e-4)
1107
    assert_allclose(g1[:, 0], np.array([0.903098, 0.245273, 0.085107]),
1108
                    rtol=1e-4)
1109
    assert_allclose(g2[:, 0], np.array([-0.103495, 1.072078, 0.01048]),
1110
                    rtol=1e-4)
1111
    assert_allclose(g3[:, 0], np.array([0, 0, 1.006465]), rtol=1e-4)
1112
1113
    assert_allclose(f1[:, 1], f1_2)
1114
    assert_allclose(f2[:, 1], f2_2)
1115
    assert_allclose(d1[:, 1], d1_2)
1116
    assert_allclose(d2[:, 1], d2_2)
1117
    assert_allclose(d3[:, 1], d3_2)
1118
    assert_allclose(e1[:, 1], e1_2)
1119
    assert_allclose(e2[:, 1], e2_2)
1120
    assert_allclose(e3[:, 1], e3_2)
1121
1122
    assert_allclose(f3[:, 1], np.array([-0.085415, -0.021176, 0.989645]),
1123
                    rtol=1e-4)
1124
    assert_allclose(g1[:, 1], np.array([0.902695, 0.246919, 0.083194]),
1125
                    rtol=1e-4)
1126
    assert_allclose(g2[:, 1], np.array([-0.11051, 1.066094, 0.013274]),
1127
                    rtol=1e-4)
1128
    assert_allclose(g3[:, 1], np.array([0, 0, 1.010463]), rtol=1e-4)
1129
1130
1131
# test scalar return values
1132
1133
def test_basevectors_apex_scalar():
1134
    apex_out = Apex(date=2000, refh=300)
1135
1136
    (f1, f2, f3, g1, g2, g3, d1, d2, d3, e1, e2,
1137
     e3) = apex_out.basevectors_apex(0, 15, 100, coords='geo')
1138
    (_, _, _, _, f1_1, f2_1, _, d1_1, d2_1, d3_1, _, e1_1, e2_1,
1139
     e3_1) = apex_out._geo2apexall(0, 15, 100)
1140
1141
    assert_allclose(f1, f1_1)
1142
    assert_allclose(f2, f2_1)
1143
    assert_allclose(d1, d1_1)
1144
    assert_allclose(d2, d2_1)
1145
    assert_allclose(d3, d3_1)
1146
    assert_allclose(e1, e1_1)
1147
    assert_allclose(e2, e2_1)
1148
    assert_allclose(e3, e3_1)
1149
1150
    assert_allclose(f3, np.array([0.092637, -0.245951, 0.938848]), rtol=1e-4)
1151
    assert_allclose(g1, np.array([0.939012, 0.073416, -0.07342]), rtol=1e-4)
1152
    assert_allclose(g2, np.array([0.055389, 1.004155, 0.257594]), rtol=1e-4)
1153
    assert_allclose(g3, np.array([0, 0, 1.065135]), rtol=1e-4)
1154
1155
1156
# test 1D array return values
1157
1158 View Code Duplication
def test_basevectors_apex_array():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
1159
    apex_out = Apex(date=2000, refh=300)
1160
    (f1, f2, f3, g1, g2, g3, d1, d2, d3, e1, e2,
1161
     e3) = apex_out.basevectors_apex([0, 30], 15, 100, coords='geo')
1162
    (_, _, _, _, f1_1, f2_1, _, d1_1, d2_1, d3_1, _, e1_1, e2_1,
1163
     e3_1) = apex_out._geo2apexall(0, 15, 100)
1164
    (_, _, _, _, f1_2, f2_2, _, d1_2, d2_2, d3_2, _, e1_2, e2_2,
1165
     e3_2) = apex_out._geo2apexall(30, 15, 100)
1166
1167
    assert_allclose(f1[:, 0], f1_1)
1168
    assert_allclose(f2[:, 0], f2_1)
1169
    assert_allclose(d1[:, 0], d1_1)
1170
    assert_allclose(d2[:, 0], d2_1)
1171
    assert_allclose(d3[:, 0], d3_1)
1172
    assert_allclose(e1[:, 0], e1_1)
1173
    assert_allclose(e2[:, 0], e2_1)
1174
    assert_allclose(e3[:, 0], e3_1)
1175
1176
    assert_allclose(f3[:, 0], np.array([0.092637, -0.245951, 0.938848]),
1177
                    rtol=1e-4)
1178
    assert_allclose(g1[:, 0], np.array([0.939012, 0.073416, -0.07342]),
1179
                    rtol=1e-4)
1180
    assert_allclose(g2[:, 0], np.array([0.055389, 1.004155, 0.257594]),
1181
                    rtol=1e-4)
1182
    assert_allclose(g3[:, 0], np.array([0, 0, 1.065135]), rtol=1e-4)
1183
1184
    assert_allclose(f1[:, 1], f1_2)
1185
    assert_allclose(f2[:, 1], f2_2)
1186
    assert_allclose(d1[:, 1], d1_2)
1187
    assert_allclose(d2[:, 1], d2_2)
1188
    assert_allclose(d3[:, 1], d3_2)
1189
    assert_allclose(e1[:, 1], e1_2)
1190
    assert_allclose(e2[:, 1], e2_2)
1191
    assert_allclose(e3[:, 1], e3_2)
1192
1193
    assert_allclose(f3[:, 1], np.array([-0.036618, -0.071019, 0.861604]),
1194
                    rtol=1e-4)
1195
    assert_allclose(g1[:, 1], np.array([0.844391, 0.015353, 0.037152]),
1196
                    rtol=1e-4)
1197
    assert_allclose(g2[:, 1], np.array([0.050808, 1.02131, 0.086342]),
1198
                    rtol=1e-4)
1199
    assert_allclose(g3[:, 1], np.array([0, 0, 1.160625]), rtol=1e-4)
1200
1201
1202
# test that vectors are calculated correctly
1203
1204
def test_basevectors_apex_delta():
1205
    apex_out = Apex(date=2000, refh=300)
1206
    for lat in range(0, 90, 10):
1207
        for lon in range(0, 360, 15):
1208
            (f1, f2, f3, g1, g2, g3, d1, d2, d3, e1, e2,
1209
             e3) = apex_out.basevectors_apex(lat, lon, 500)
1210
            f = [np.append(f1, 0), np.append(f2, 0), f3]
1211
            g = [g1, g2, g3]
1212
            d = [d1, d2, d3]
1213
            e = [e1, e2, e3]
1214
            for i, j in [(i, j) for i in range(3) for j in range(3)]:
1215
                delta = 1 if i == j else 0
1216
                assert_allclose(np.sum(f[i]*g[j]), delta, rtol=0, atol=1e-5)
1217
                assert_allclose(np.sum(d[i]*e[j]), delta, rtol=0, atol=1e-5)
1218
1219
1220
def test_basevectors_apex_invalid_scalar():
1221
    """ Test warning and fill values for calculating base vectors with bad value
1222
    """
1223
    apex2000 = Apex(date=2000, refh=10000)
1224
    with warnings.catch_warnings(record=True) as wmsg:
1225
        base_vecs = apex2000.basevectors_apex(0, 0, 0)
1226
1227
    assert issubclass(wmsg[-1].category, UserWarning)
1228
    assert 'set to -9999 where' in str(wmsg[-1].message)
1229
1230
    invalid = [-9999, -9999, -9999]
1231
    for i, bvec in enumerate(base_vecs):
1232
        if i < 2:
1233
            assert not np.allclose(bvec, invalid[:2])
1234
        else:
1235
            assert_allclose(bvec, invalid)
1236
1237
1238
###============================================================================
1239
### Test the get_apex() method
1240
###============================================================================
1241
1242
1243
def test_get_apex():
1244
    apex_out = Apex(date=2000, refh=300)
1245
    assert_allclose(apex_out.get_apex(10), 507.409702543805)
1246
    assert_allclose(apex_out.get_apex(60), 20313.026999999987)
1247
1248
1249
def test_get_apex_invalid_lat():
1250
    apex_out = Apex(date=2000, refh=300)
1251
    with pytest.raises(ValueError):
1252
        apex_out.get_apex(91)
1253
    with pytest.raises(ValueError):
1254
        apex_out.get_apex(-91)
1255
    apex_out.get_apex(90)
1256
    apex_out.get_apex(-90)
1257
1258
    assert_allclose(apex_out.get_apex(90+1e-5), apex_out.get_apex(90),
1259
                    rtol=0, atol=1e-8)
1260
1261
1262
###============================================================================
1263
### Test the set_epoch() method
1264
###============================================================================
1265
1266
1267
def test_set_epoch():
1268
    apex_out = Apex(date=2000.2, refh=300)
1269
    assert_allclose(apex_out.year, 2000.2)
1270
    ret_2000_2_py = apex_out._geo2apex(60, 15, 100)
1271
    apex_out.set_epoch(2000.8)
1272
    assert_allclose(apex_out.year, 2000.8)
1273
    ret_2000_8_py = apex_out._geo2apex(60, 15, 100)
1274
1275
    assert ret_2000_2_py != ret_2000_8_py
1276
1277
    fa.loadapxsh(apex_out.datafile, 2000.2)
1278
    ret_2000_2_apex = fa.apxg2all(60, 15, 100, 300, 0)[2:4]
1279
    fa.loadapxsh(apex_out.datafile, 2000.8)
1280
    ret_2000_8_apex = fa.apxg2all(60, 15, 100, 300, 0)[2:4]
1281
1282
    assert ret_2000_2_apex != ret_2000_8_apex
1283
1284
    assert_allclose(ret_2000_2_py, ret_2000_2_apex)
1285
    assert_allclose(ret_2000_8_py, ret_2000_8_apex)
1286
1287
1288
###============================================================================
1289
### Test the set_refh() method
1290
###============================================================================
1291
1292
1293
def test_set_refh():
1294
    apex_out = Apex(date=2000, refh=300)
1295
    assert apex_out.refh, 300
1296
    ret_300 = apex_out._geo2apex(60, 15, 100)
1297
    apex_out.set_refh(500)
1298
    assert apex_out.refh == 500
1299
    ret_500 = apex_out._geo2apex(60, 15, 100)
1300
1301
    assert_allclose(ret_300, fa.apxg2all(60, 15, 100, 300, 0)[2:4])
1302
    assert_allclose(ret_500, fa.apxg2all(60, 15, 100, 500, 0)[2:4])
1303
1304
1305
###============================================================================
1306
### Test the get_babs() method
1307
###============================================================================
1308
1309
1310
def test_get_babs():
1311
    inputs = [[[80],[100],[300]],[range(50,90,8),range(0,360,80),[300]*5],
1312
              [90.0,0,1000]]
1313
    temp1 = np.array([4.22045410e-05, 5.15672743e-05, 4.98150200e-05,
1314
                     5.06769359e-05, 4.91028428e-05])
1315
    expected = [[5.1303124427795412e-05], temp1, [3.793962299823761e-05]]
1316
1317
    A = Apex(date=2018.1, refh=0)
1318
    for i in range(len(inputs)):
1319
        outputs = A.get_babs(*inputs[i])
1320
        if isinstance(outputs,np.float64):
1321
            outputs = [outputs]
1322
        for j,output in enumerate(outputs):
1323
            assert_allclose(output, expected[i][j], rtol=0, atol=1e-5)
1324
1325
1326
###============================================================================
1327
### Test the bvectors_apex() method
1328
###============================================================================
1329
1330
1331
def test_bvectors_apex():
1332
    inputs = [[80,81],[100,120],[100,200]]
1333
1334
    expected = (np.array([5.94623305e-05,  5.95450722e-05]),
1335
                np.array([[ 0.02008877,  0.00303204],
1336
                          [ 0.03571109,  0.03377986],
1337
                          [-0.94045794, -0.89848483]]),
1338
                np.array([  5.26919505e-05,   4.81377429e-05]),
1339
                np.array([[ 0.02266997,  0.00375055],
1340
                          [ 0.04029961,  0.04178477],
1341
                          [-1.0612973 , -1.1114012 ]])
1342
               )
1343
1344
    A = Apex(date=2018.1, refh=0)
1345
1346
    outputs = A.bvectors_apex(*inputs,coords='geo', precision=1e-10)
1347
    for i,output in enumerate(outputs):
1348
        for j in range(output.size):
1349
            assert_allclose(output.ravel()[j], expected[i].ravel()[j], rtol=0,
1350
                            atol=1e-5)
1351
1352
1353
if __name__ == '__main__':
1354
    pytest.main()
1355