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