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