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