Completed
Pull Request — master (#20)
by
unknown
03:41
created

test_Apex.test_init_date()   A

Complexity

Conditions 1

Size

Total Lines 4
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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