Total Complexity | 127 |
Total Lines | 797 |
Duplicated Lines | 3.01 % |
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 | from io import StringIO |
||
6 | import logging |
||
7 | import numpy as np |
||
8 | import os |
||
9 | from sys import version_info |
||
10 | import pytest |
||
11 | import warnings |
||
12 | |||
13 | import aacgmv2 |
||
14 | |||
15 | |||
16 | class TestConvertArray: |
||
17 | def setup(self): |
||
18 | self.out = None |
||
19 | self.ref = None |
||
20 | self.rtol = 1.0e-4 |
||
21 | |||
22 | def teardown(self): |
||
23 | del self.out, self.ref, self.rtol |
||
24 | |||
25 | def evaluate_output(self, ind=None): |
||
26 | """ Function used to evaluate convert_latlon_arr output""" |
||
27 | if self.out is not None: |
||
28 | if ind is not None: |
||
29 | self.ref = [[rr[ind]] for rr in self.ref] |
||
30 | |||
31 | np.testing.assert_equal(len(self.out), len(self.ref)) |
||
32 | for i, oo in enumerate(self.out): |
||
33 | if not isinstance(oo, np.ndarray): |
||
34 | raise TypeError("output value is not a numpy array") |
||
35 | |||
36 | np.testing.assert_equal(len(oo), len(self.ref[i])) |
||
37 | np.testing.assert_allclose(oo, self.ref[i], rtol=self.rtol) |
||
38 | |||
39 | |||
40 | class TestConvertLatLon: |
||
41 | def setup(self): |
||
42 | """Runs before every method to create a clean testing setup""" |
||
43 | self.dtime = dt.datetime(2015, 1, 1, 0, 0, 0) |
||
44 | self.ddate = dt.date(2015, 1, 1) |
||
45 | self.in_args = [60, 0] |
||
46 | self.out = None |
||
47 | self.rtol = 1.0e-4 |
||
48 | |||
49 | def teardown(self): |
||
50 | """Runs after every method to clean up previous testing""" |
||
51 | del self.out, self.in_args, self.rtol, self.dtime, self.ddate |
||
52 | |||
53 | @pytest.mark.parametrize('alt,method_code,ref', |
||
54 | [(300, 'TRACE', [58.2268, 81.1613, 1.0457]), |
||
55 | (3000.0, "G2A|BADIDEA", [64.3578, 83.2895, |
||
56 | 1.4694]), |
||
57 | (7000.0, "G2A|TRACE|BADIDEA", |
||
58 | [69.3187, 85.0845, 2.0973])]) |
||
59 | def test_convert_latlon(self, alt, method_code, ref): |
||
60 | """Test single value latlon conversion""" |
||
61 | self.in_args.extend([alt, self.dtime, method_code]) |
||
62 | self.out = aacgmv2.convert_latlon(*self.in_args) |
||
63 | np.testing.assert_allclose(self.out, ref, rtol=self.rtol) |
||
64 | |||
65 | @pytest.mark.parametrize('lat,ref', |
||
66 | [(90.01, [83.927161, 170.1471396, 1.04481923]), |
||
67 | (-90.01, [-74.9814852, 17.990332, 1.044819236])]) |
||
68 | def test_convert_latlon_high_lat(self, lat, ref): |
||
69 | """Test single latlon conversion with latitude just out of bounds""" |
||
70 | self.in_args[0] = lat |
||
71 | self.in_args.extend([300, self.dtime, 'G2A']) |
||
72 | self.out = aacgmv2.convert_latlon(*self.in_args) |
||
73 | np.testing.assert_allclose(self.out, ref, rtol=self.rtol) |
||
74 | |||
75 | def test_convert_latlon_datetime_date(self): |
||
76 | """Test single latlon conversion with date and datetime input""" |
||
77 | self.in_args.extend([300, self.ddate, 'TRACE']) |
||
78 | self.out = aacgmv2.convert_latlon(*self.in_args) |
||
79 | np.testing.assert_allclose(self.out, [58.2268, 81.1613, 1.0457], |
||
80 | rtol=self.rtol) |
||
81 | |||
82 | @pytest.mark.skipif(version_info.major == 2, |
||
83 | reason='Not raised in Python 2') |
||
84 | def test_convert_latlon_location_failure(self): |
||
85 | """Test single value latlon conversion with a bad location""" |
||
86 | self.out = aacgmv2.convert_latlon(0, 0, 0, self.dtime, self.in_args[-1]) |
||
87 | assert np.all(np.isnan(np.array(self.out))) |
||
88 | |||
89 | def test_convert_latlon_maxalt_failure(self): |
||
90 | """test convert_latlon failure for an altitude too high for coeffs""" |
||
91 | self.in_args.extend([2001, self.dtime, ""]) |
||
92 | self.out = aacgmv2.convert_latlon(*self.in_args) |
||
93 | assert np.all(np.isnan(np.array(self.out))) |
||
94 | |||
95 | @pytest.mark.parametrize('in_rep,in_irep,msg', |
||
96 | [(None, 3, "must be a datetime object"), |
||
97 | (91, 0, "unrealistic latitude"), |
||
98 | (-91, 0, "unrealistic latitude"), |
||
99 | (None, 4, "unknown method code")]) |
||
100 | def test_convert_latlon_failure(self, in_rep, in_irep, msg): |
||
101 | self.in_args.extend([300, self.dtime, "G2A"]) |
||
102 | self.in_args[in_irep] = in_rep |
||
103 | with pytest.raises(ValueError, match=msg): |
||
104 | aacgmv2.convert_latlon(*self.in_args) |
||
105 | |||
106 | |||
107 | class TestConvertLatLonArr(TestConvertArray): |
||
108 | View Code Duplication | def setup(self): |
|
|
|||
109 | """Runs before every method to create a clean testing setup""" |
||
110 | self.dtime = dt.datetime(2015, 1, 1, 0, 0, 0) |
||
111 | self.ddate = dt.date(2015, 1, 1) |
||
112 | self.lat_in = [60.0, 61.0] |
||
113 | self.lon_in = [0.0, 0.0] |
||
114 | self.alt_in = [300.0, 300.0] |
||
115 | self.method = 'TRACE' |
||
116 | self.out = None |
||
117 | self.ref = [[58.2268, 59.3184], [81.1613, 81.6080], [1.0457, 1.0456]] |
||
118 | self.rtol = 1.0e-4 |
||
119 | |||
120 | def teardown(self): |
||
121 | """Runs after every method to clean up previous testing""" |
||
122 | del self.lat_in, self.lon_in, self.alt_in, self.dtime, self.ddate |
||
123 | del self.method, self.out, self.ref, self.rtol |
||
124 | |||
125 | def test_convert_latlon_arr_single_val(self): |
||
126 | """Test array latlon conversion for a single value""" |
||
127 | self.out = aacgmv2.convert_latlon_arr(self.lat_in[0], self.lon_in[0], |
||
128 | self.alt_in[0], self.dtime, |
||
129 | self.method) |
||
130 | self.evaluate_output(ind=0) |
||
131 | |||
132 | def test_convert_latlon_arr_arr_single(self): |
||
133 | """Test array latlon conversion for array input of shape (1,)""" |
||
134 | self.out = aacgmv2.convert_latlon_arr(np.array([self.lat_in[0]]), |
||
135 | np.array([self.lon_in[0]]), |
||
136 | np.array([self.alt_in[0]]), |
||
137 | self.dtime, self.method) |
||
138 | self.evaluate_output(ind=0) |
||
139 | |||
140 | def test_convert_latlon_arr_list_single(self): |
||
141 | """Test array latlon conversion for list input of single values""" |
||
142 | self.out = aacgmv2.convert_latlon_arr([self.lat_in[0]], |
||
143 | [self.lon_in[0]], |
||
144 | [self.alt_in[0]], self.dtime, |
||
145 | self.method) |
||
146 | self.evaluate_output(ind=0) |
||
147 | |||
148 | def test_convert_latlon_arr_list(self): |
||
149 | """Test array latlon conversion for list input""" |
||
150 | self.out = aacgmv2.convert_latlon_arr(self.lat_in, self.lon_in, |
||
151 | self.alt_in, self.dtime, |
||
152 | self.method) |
||
153 | self.evaluate_output() |
||
154 | |||
155 | def test_convert_latlon_arr_arr(self): |
||
156 | """Test array latlon conversion for array input""" |
||
157 | self.out = aacgmv2.convert_latlon_arr(np.array(self.lat_in), |
||
158 | np.array(self.lon_in), |
||
159 | np.array(self.alt_in), |
||
160 | self.dtime, self.method) |
||
161 | self.evaluate_output() |
||
162 | |||
163 | def test_convert_latlon_arr_list_mix(self): |
||
164 | """Test array latlon conversion for mixed types with list""" |
||
165 | self.out = aacgmv2.convert_latlon_arr(self.lat_in, self.lon_in[0], |
||
166 | self.alt_in[0], self.dtime, |
||
167 | self.method) |
||
168 | self.evaluate_output() |
||
169 | |||
170 | def test_convert_latlon_arr_arr_mix(self): |
||
171 | """Test array latlon conversion for mixed type with an array""" |
||
172 | self.out = aacgmv2.convert_latlon_arr(np.array(self.lat_in), |
||
173 | self.lon_in[0], self.alt_in[0], |
||
174 | self.dtime, self.method) |
||
175 | self.evaluate_output() |
||
176 | |||
177 | def test_convert_latlon_arr_arr_mult_and_single_element(self): |
||
178 | """Test latlon conversion for arrays with multiple and single vals""" |
||
179 | self.out = aacgmv2.convert_latlon_arr(np.array(self.lat_in), |
||
180 | np.array([self.lon_in[0]]), |
||
181 | np.array(self.alt_in), |
||
182 | self.dtime, self.method) |
||
183 | self.evaluate_output() |
||
184 | |||
185 | @pytest.mark.parametrize('method_code,alt,local_ref', |
||
186 | [("BADIDEA", 3000.0, |
||
187 | [[64.3580], [83.2895], [1.4694]]), |
||
188 | ("BADIDEA|TRACE", 7000.0, |
||
189 | [[69.3187], [85.0845], [2.0973]])]) |
||
190 | def test_convert_latlon_arr_badidea(self, method_code, alt, local_ref): |
||
191 | """Test array latlon conversion for BADIDEA""" |
||
192 | self.out = aacgmv2.convert_latlon_arr(self.lat_in[0], self.lon_in[0], |
||
193 | [alt], self.dtime, method_code) |
||
194 | self.ref = local_ref |
||
195 | self.evaluate_output() |
||
196 | |||
197 | @pytest.mark.skipif(version_info.major == 2, |
||
198 | reason='Not raised in Python 2') |
||
199 | def test_convert_latlon_arr_location_failure(self): |
||
200 | """Test array latlon conversion with a bad location""" |
||
201 | |||
202 | with warnings.catch_warnings(): |
||
203 | # Causes all warnings to be surpressed |
||
204 | warnings.simplefilter("ignore") |
||
205 | |||
206 | # Trigger a warning |
||
207 | self.out = aacgmv2.convert_latlon_arr([0], [0], [0], self.dtime, "") |
||
208 | |||
209 | # Test the output |
||
210 | np.testing.assert_equal(len(self.out), len(self.ref)) |
||
211 | assert np.any(~np.isfinite(np.array(self.out))) |
||
212 | |||
213 | def test_convert_latlon_arr_datetime_date(self): |
||
214 | """Test array latlon conversion with date and datetime input""" |
||
215 | self.out = aacgmv2.convert_latlon_arr(self.lat_in, self.lon_in, |
||
216 | self.alt_in, self.ddate, |
||
217 | self.method) |
||
218 | self.evaluate_output() |
||
219 | |||
220 | def test_convert_latlon_arr_clip(self): |
||
221 | """Test array latlon conversion with latitude clipping""" |
||
222 | self.lat_in = [90.01, -90.01] |
||
223 | self.ref = [[83.92352053, -74.98110552], [170.1381271, 17.98164313], |
||
224 | [1.04481924, 1.04481924]] |
||
225 | self.out = aacgmv2.convert_latlon_arr(self.lat_in, self.lon_in, |
||
226 | self.alt_in, self.ddate, |
||
227 | self.method) |
||
228 | self.evaluate_output() |
||
229 | |||
230 | def test_convert_latlon_arr_maxalt_failure(self): |
||
231 | """test convert_latlon_arr failure for altitudes too high for coeffs""" |
||
232 | self.method = "" |
||
233 | self.out = aacgmv2.convert_latlon_arr(self.lat_in[0], self.lon_in[0], |
||
234 | [2001], self.dtime, self.method) |
||
235 | assert np.all(np.isnan(np.array(self.out))) |
||
236 | |||
237 | @pytest.mark.parametrize('in_rep,in_irep,msg', |
||
238 | [(None, 3, "must be a datetime object"), |
||
239 | ([np.full(shape=(3, 2), fill_value=50.0), 0], |
||
240 | [0, 1], "unable to process multi-dimensional"), |
||
241 | ([50, 60, 70], 0, "arrays are mismatched"), |
||
242 | ([[91, 60, -91], 0, 300], [0, 1, 2], |
||
243 | "unrealistic latitude"), |
||
244 | (None, 4, "unknown method code")]) |
||
245 | def test_convert_latlon_arr_failure(self, in_rep, in_irep, msg): |
||
246 | in_args = np.array([self.lat_in, self.lon_in, self.alt_in, self.dtime, |
||
247 | "G2A"], dtype=object) |
||
248 | in_args[in_irep] = in_rep |
||
249 | with pytest.raises(ValueError, match=msg): |
||
250 | aacgmv2.convert_latlon_arr(*in_args) |
||
251 | |||
252 | |||
253 | class TestGetAACGMCoord: |
||
254 | def setup(self): |
||
255 | """Runs before every method to create a clean testing setup""" |
||
256 | self.dtime = dt.datetime(2015, 1, 1, 0, 0, 0) |
||
257 | self.ddate = dt.date(2015, 1, 1) |
||
258 | self.in_args = [60, 0] |
||
259 | self.out = None |
||
260 | self.rtol = 1.0e-4 |
||
261 | |||
262 | def teardown(self): |
||
263 | """Runs after every method to clean up previous testing""" |
||
264 | del self.out, self.in_args, self.rtol, self.dtime, self.ddate |
||
265 | |||
266 | @pytest.mark.parametrize('alt,method_code,ref', |
||
267 | [(300, 'TRACE', [58.2268, 81.1613, 0.1888]), |
||
268 | (3000.0, "G2A|BADIDEA", [64.3578, 83.2895, |
||
269 | 0.3307]), |
||
270 | (7000.0, "G2A|TRACE|BADIDEA", |
||
271 | [69.3187, 85.0845, 0.4503])]) |
||
272 | def test_get_aacgm_coord(self, alt, method_code, ref): |
||
273 | """Test single value AACGMV2 calculation, defaults to TRACE""" |
||
274 | self.in_args.extend([alt, self.dtime, method_code]) |
||
275 | self.out = aacgmv2.get_aacgm_coord(*self.in_args) |
||
276 | np.testing.assert_allclose(self.out, ref, rtol=self.rtol) |
||
277 | |||
278 | def test_get_aacgm_coord_datetime_date(self): |
||
279 | """Test single AACGMV2 calculation with date and datetime input""" |
||
280 | self.in_args.extend([300.0, self.ddate, 'TRACE']) |
||
281 | self.out = aacgmv2.get_aacgm_coord(*self.in_args) |
||
282 | np.testing.assert_allclose(self.out, [58.2268, 81.1613, 0.1888], |
||
283 | rtol=self.rtol) |
||
284 | |||
285 | @pytest.mark.skipif(version_info.major == 2, |
||
286 | reason='Not raised in Python 2') |
||
287 | def test_get_aacgm_coord_location_failure(self): |
||
288 | """Test single value AACGMV2 calculation with a bad location""" |
||
289 | self.in_args.extend([0.0, self.dtime, 'TRACE']) |
||
290 | self.in_args[0] = 0.0 |
||
291 | |||
292 | self.out = aacgmv2.get_aacgm_coord(*self.in_args) |
||
293 | np.all(np.isnan(np.array(self.out))) |
||
294 | |||
295 | def test_get_aacgm_coord_maxalt_failure(self): |
||
296 | """test get_aacgm_coord failure for an altitude too high for coeffs""" |
||
297 | self.in_args.extend([2001, self.dtime, ""]) |
||
298 | self.out = aacgmv2.get_aacgm_coord(*self.in_args) |
||
299 | assert np.all(np.isnan(np.array(self.out))) |
||
300 | |||
301 | @pytest.mark.parametrize('in_index,value', |
||
302 | [(3, None), (0, 91.0), (0, -91.0)]) |
||
303 | def test_get_aacgm_coord_raise_value_error(self, in_index, value): |
||
304 | """Test different ways to raise a ValueError""" |
||
305 | self.in_args.extend([300.0, self.dtime]) |
||
306 | self.in_args[in_index] = value |
||
307 | with pytest.raises(ValueError): |
||
308 | self.out = aacgmv2.get_aacgm_coord(*self.in_args) |
||
309 | |||
310 | |||
311 | class TestGetAACGMCoordArr(TestConvertArray): |
||
312 | View Code Duplication | def setup(self): |
|
313 | """Runs before every method to create a clean testing setup""" |
||
314 | self.dtime = dt.datetime(2015, 1, 1, 0, 0, 0) |
||
315 | self.ddate = dt.date(2015, 1, 1) |
||
316 | self.lat_in = [60.0, 61.0] |
||
317 | self.lon_in = [0.0, 0.0] |
||
318 | self.alt_in = [300.0, 300.0] |
||
319 | self.method = 'TRACE' |
||
320 | self.out = None |
||
321 | self.ref = [[58.22676, 59.31847], [81.16135, 81.60797], |
||
322 | [0.18880, 0.21857]] |
||
323 | self.rtol = 1.0e-4 |
||
324 | |||
325 | def teardown(self): |
||
326 | """Runs after every method to clean up previous testing""" |
||
327 | del self.out, self.ref, self.lat_in, self.dtime, self.ddate |
||
328 | del self.lon_in, self.alt_in, self.method, self.rtol |
||
329 | |||
330 | def test_get_aacgm_coord_arr_single_val(self): |
||
331 | """Test array AACGMV2 calculation for a single value""" |
||
332 | self.out = aacgmv2.get_aacgm_coord_arr(self.lat_in[0], self.lon_in[0], |
||
333 | self.alt_in[0], self.dtime, |
||
334 | self.method) |
||
335 | self.evaluate_output(ind=0) |
||
336 | |||
337 | def test_get_aacgm_coord_arr_list_single(self): |
||
338 | """Test array AACGMV2 calculation for list input of single values""" |
||
339 | self.out = aacgmv2.get_aacgm_coord_arr([self.lat_in[0]], |
||
340 | [self.lon_in[0]], |
||
341 | [self.alt_in[0]], self.dtime, |
||
342 | self.method) |
||
343 | self.evaluate_output(ind=0) |
||
344 | |||
345 | def test_get_aacgm_coord_arr_arr_single(self): |
||
346 | """Test array AACGMV2 calculation for array with a single value""" |
||
347 | self.out = aacgmv2.get_aacgm_coord_arr(np.array([self.lat_in[0]]), |
||
348 | np.array([self.lon_in[0]]), |
||
349 | np.array([self.alt_in[0]]), |
||
350 | self.dtime, self.method) |
||
351 | self.evaluate_output(ind=0) |
||
352 | |||
353 | def test_get_aacgm_coord_arr_list(self): |
||
354 | """Test array AACGMV2 calculation for list input""" |
||
355 | self.out = aacgmv2.get_aacgm_coord_arr(self.lat_in, self.lon_in, |
||
356 | self.alt_in, self.dtime, |
||
357 | self.method) |
||
358 | self.evaluate_output() |
||
359 | |||
360 | def test_get_aacgm_coord_arr_arr(self): |
||
361 | """Test array AACGMV2 calculation for an array""" |
||
362 | self.out = aacgmv2.get_aacgm_coord_arr(np.array(self.lat_in), |
||
363 | np.array(self.lon_in), |
||
364 | np.array(self.alt_in), |
||
365 | self.dtime, self.method) |
||
366 | self.evaluate_output() |
||
367 | |||
368 | def test_get_aacgm_coord_arr_list_mix(self): |
||
369 | """Test array AACGMV2 calculation for a list and floats""" |
||
370 | self.out = aacgmv2.get_aacgm_coord_arr(self.lat_in, self.lon_in[0], |
||
371 | self.alt_in[0], self.dtime, |
||
372 | self.method) |
||
373 | self.evaluate_output() |
||
374 | |||
375 | def test_get_aacgm_coord_arr_arr_mix(self): |
||
376 | """Test array AACGMV2 calculation for an array and floats""" |
||
377 | self.out = aacgmv2.get_aacgm_coord_arr(np.array(self.lat_in), |
||
378 | self.lon_in[0], self.alt_in[0], |
||
379 | self.dtime, self.method) |
||
380 | self.evaluate_output() |
||
381 | |||
382 | def test_get_aacgm_coord_arr_badidea(self): |
||
383 | """Test array AACGMV2 calculation for BADIDEA""" |
||
384 | self.method = "|".join([self.method, "BADIDEA"]) |
||
385 | self.out = aacgmv2.get_aacgm_coord_arr(self.lat_in[0], self.lon_in[0], |
||
386 | [3000.0], self.dtime, |
||
387 | self.method) |
||
388 | self.ref = [[64.3481], [83.2885], [0.3306]] |
||
389 | self.evaluate_output() |
||
390 | |||
391 | @pytest.mark.skipif(version_info.major == 2, |
||
392 | reason='Not raised in Python 2') |
||
393 | def test_get_aacgm_coord_arr_location_failure(self): |
||
394 | """Test array AACGMV2 calculation with a bad location""" |
||
395 | self.out = aacgmv2.get_aacgm_coord_arr([0], [0], [0], self.dtime, |
||
396 | self.method) |
||
397 | |||
398 | np.testing.assert_equal(len(self.out), len(self.ref)) |
||
399 | assert [isinstance(oo, np.ndarray) and len(oo) == 1 for oo in self.out] |
||
400 | assert np.any([np.isnan(oo) for oo in self.out]) |
||
401 | |||
402 | def test_get_aacgm_coord_arr_mult_failure(self): |
||
403 | """Test aacgm_coord_arr failure with multi-dim array input""" |
||
404 | |||
405 | with pytest.raises(ValueError): |
||
406 | (self.mlat_out, self.mlon_out, |
||
407 | self.mlt_out) = aacgmv2.get_aacgm_coord_arr( |
||
408 | np.array([[60, 61, 62], [63, 64, 65]]), 0, 300, self.dtime) |
||
409 | |||
410 | def test_get_aacgm_coord_arr_time_failure(self): |
||
411 | """Test array AACGMV2 calculation with a bad time""" |
||
412 | with pytest.raises(ValueError): |
||
413 | aacgmv2.get_aacgm_coord_arr(self.lat_in, self.lon_in, self.alt_in, |
||
414 | None, self.method) |
||
415 | |||
416 | def test_get_aacgm_coord_arr_mlat_failure(self): |
||
417 | """Test error return for co-latitudes above 90 for an array""" |
||
418 | |||
419 | self.lat_in = [91, 60, -91] |
||
420 | with pytest.raises(ValueError): |
||
421 | self.out = aacgmv2.get_aacgm_coord_arr(self.lat_in, self.lon_in[0], |
||
422 | self.alt_in[0], self.dtime, |
||
423 | self.method) |
||
424 | |||
425 | def test_get_aacgm_coord_arr_datetime_date(self): |
||
426 | """Test array AACGMV2 calculation with date and datetime input""" |
||
427 | self.out = aacgmv2.get_aacgm_coord_arr(self.lat_in, self.lon_in, |
||
428 | self.alt_in, self.ddate, |
||
429 | self.method) |
||
430 | self.ref = aacgmv2.get_aacgm_coord_arr(self.lat_in, self.lon_in, |
||
431 | self.alt_in, self.dtime, |
||
432 | self.method) |
||
433 | self.evaluate_output() |
||
434 | |||
435 | def test_get_aacgm_coord_arr_maxalt_failure(self): |
||
436 | """test aacgm_coord_arr failure for an altitude too high for coeff""" |
||
437 | self.method = "" |
||
438 | self.alt_in = [2001 for ll in self.lat_in] |
||
439 | self.out = aacgmv2.get_aacgm_coord_arr(self.lat_in, self.lon_in, |
||
440 | self.alt_in, self.dtime, |
||
441 | self.method) |
||
442 | |||
443 | np.testing.assert_equal(len(self.out), len(self.ref)) |
||
444 | assert [isinstance(oo, np.ndarray) and len(oo) == len(self.lat_in) |
||
445 | for oo in self.out] |
||
446 | assert np.all(np.isnan(np.array(self.out))) |
||
447 | |||
448 | |||
449 | class TestConvertCode: |
||
450 | def setup(self): |
||
451 | self.c_method_code = None |
||
452 | self.ref_code = None |
||
453 | self.out = None |
||
454 | |||
455 | def teardown(self): |
||
456 | del self.c_method_code, self.ref_code, self.out |
||
457 | |||
458 | def set_c_code(self): |
||
459 | """ Utility test to get desired C method code""" |
||
460 | if self.ref_code is not None: |
||
461 | self.ref_code = self.ref_code.upper() |
||
462 | self.c_method_code = getattr(aacgmv2._aacgmv2, self.ref_code) |
||
463 | |||
464 | def set_bad_c_code(self): |
||
465 | """ Test failure to get bad code name""" |
||
466 | self.ref_code = "not_a_valid_code" |
||
467 | with pytest.raises(AttributeError): |
||
468 | self.set_c_code() |
||
469 | |||
470 | @pytest.mark.parametrize('method_code', |
||
471 | [('G2A'), ('A2G'), ('TRACE'), ('ALLOWTRACE'), |
||
472 | ('BADIDEA'), ('GEOCENTRIC'), ('g2a')]) |
||
473 | def test_standard_convert_str_to_bit(self, method_code): |
||
474 | """Test conversion from string code to bit for standard cases""" |
||
475 | self.ref_code = method_code |
||
476 | self.set_c_code() |
||
477 | self.out = aacgmv2.convert_str_to_bit(method_code) |
||
478 | |||
479 | np.testing.assert_equal(self.out, self.c_method_code) |
||
480 | |||
481 | @pytest.mark.parametrize('str_code,bit_ref', |
||
482 | [("G2A | trace", |
||
483 | aacgmv2._aacgmv2.G2A + aacgmv2._aacgmv2.TRACE), |
||
484 | ("ggoogg|", aacgmv2._aacgmv2.G2A)]) |
||
485 | def test_non_standard_convert_str_to_bit(self, str_code, bit_ref): |
||
486 | """Test conversion from string code to bit for non-standard cases""" |
||
487 | self.out = aacgmv2.convert_str_to_bit(str_code) |
||
488 | np.testing.assert_equal(self.out, bit_ref) |
||
489 | |||
490 | @pytest.mark.parametrize('bool_dict,method_code', |
||
491 | [({}, 'G2A'), ({'a2g': True}, 'A2G'), |
||
492 | ({'trace': True}, 'TRACE'), |
||
493 | ({'allowtrace': True}, 'ALLOWTRACE'), |
||
494 | ({'badidea': True}, 'BADIDEA'), |
||
495 | ({'geocentric': True}, 'GEOCENTRIC')]) |
||
496 | def test_convert_bool_to_bit(self, bool_dict, method_code): |
||
497 | """Test conversion from Boolean code to bit""" |
||
498 | self.ref_code = method_code |
||
499 | self.set_c_code() |
||
500 | self.out = aacgmv2.convert_bool_to_bit(**bool_dict) |
||
501 | |||
502 | np.testing.assert_equal(self.out, self.c_method_code) |
||
503 | |||
504 | |||
505 | class TestMLTConvert: |
||
506 | def setup(self): |
||
507 | """Runs before every method to create a clean testing setup""" |
||
508 | self.dtime = dt.datetime(2015, 1, 1, 0, 0, 0) |
||
509 | self.dtime2 = dt.datetime(2015, 1, 1, 10, 0, 0) |
||
510 | self.ddate = dt.date(2015, 1, 1) |
||
511 | self.mlon_out = None |
||
512 | self.mlt_out = None |
||
513 | self.mlt_diff = None |
||
514 | self.mlon_list = [270.0, 80.0, -95.0] |
||
515 | self.mlt_list = [12.0, 25.0, -1.0] |
||
516 | self.mlon_comp = [-101.670617955439, 93.329382044561, 63.329382044561] |
||
517 | self.mlt_comp = [12.7780412, 0.11137453, 12.44470786] |
||
518 | self.diff_comp = np.ones(shape=(3,)) * -10.52411552 |
||
519 | |||
520 | def teardown(self): |
||
521 | """Runs after every method to clean up previous testing""" |
||
522 | del self.mlon_out, self.mlt_out, self.mlt_list, self.mlon_list |
||
523 | del self.mlon_comp, self.mlt_comp, self.mlt_diff, self.diff_comp |
||
524 | |||
525 | def test_date_input(self): |
||
526 | """Test to see that the date input works""" |
||
527 | self.mlt_out = aacgmv2.convert_mlt(self.mlon_list, self.ddate, |
||
528 | m2a=False) |
||
529 | np.testing.assert_allclose(self.mlt_out, self.mlt_comp, rtol=1.0e-4) |
||
530 | |||
531 | def test_datetime_exception(self): |
||
532 | """Test to see that a value error is raised with bad time input""" |
||
533 | with pytest.raises(ValueError): |
||
534 | self.mlt_out = aacgmv2.wrapper.convert_mlt(self.mlon_list, 1997) |
||
535 | |||
536 | def test_inv_convert_mlt_single(self): |
||
537 | """Test MLT inversion for a single value""" |
||
538 | for i, mlt in enumerate(self.mlt_list): |
||
539 | self.mlon_out = aacgmv2.convert_mlt(mlt, self.dtime, m2a=True) |
||
540 | np.testing.assert_almost_equal(self.mlon_out, self.mlon_comp[i], |
||
541 | decimal=4) |
||
542 | |||
543 | def test_inv_convert_mlt_list(self): |
||
544 | """Test MLT inversion for a list""" |
||
545 | self.mlon_out = aacgmv2.convert_mlt(self.mlt_list, self.dtime, m2a=True) |
||
546 | np.testing.assert_allclose(self.mlon_out, self.mlon_comp, rtol=1.0e-4) |
||
547 | |||
548 | def test_inv_convert_mlt_arr(self): |
||
549 | """Test MLT inversion for an array""" |
||
550 | self.mlon_out = aacgmv2.convert_mlt(np.array(self.mlt_list), self.dtime, |
||
551 | m2a=True) |
||
552 | |||
553 | np.testing.assert_allclose(self.mlon_out, self.mlon_comp, rtol=1.0e-4) |
||
554 | |||
555 | def test_inv_convert_mlt_wrapping(self): |
||
556 | """Test MLT wrapping""" |
||
557 | self.mlon_out = aacgmv2.convert_mlt(np.array([1, 25, -1, 23]), |
||
558 | self.dtime, m2a=True) |
||
559 | |||
560 | np.testing.assert_almost_equal(self.mlon_out[0], self.mlon_out[1], |
||
561 | decimal=6) |
||
562 | np.testing.assert_almost_equal(self.mlon_out[2], self.mlon_out[3], |
||
563 | decimal=6) |
||
564 | |||
565 | def test_mlt_convert_mlon_wrapping(self): |
||
566 | """Test mlon wrapping""" |
||
567 | self.mlt_out = aacgmv2.convert_mlt(np.array([270, -90, 1, 361]), |
||
568 | self.dtime, m2a=False) |
||
569 | |||
570 | np.testing.assert_almost_equal(self.mlt_out[0], self.mlt_out[1], |
||
571 | decimal=6) |
||
572 | np.testing.assert_almost_equal(self.mlt_out[2], self.mlt_out[3], |
||
573 | decimal=6) |
||
574 | |||
575 | def test_mlt_convert_single(self): |
||
576 | """Test MLT calculation for a single value""" |
||
577 | for i, mlon in enumerate(self.mlon_list): |
||
578 | self.mlt_out = aacgmv2.convert_mlt(mlon, self.dtime, m2a=False) |
||
579 | np.testing.assert_almost_equal(self.mlt_out, self.mlt_comp[i], |
||
580 | decimal=4) |
||
581 | |||
582 | def test_mlt_convert_list(self): |
||
583 | """Test MLT calculation for a list""" |
||
584 | self.mlt_out = aacgmv2.convert_mlt(self.mlon_list, self.dtime, |
||
585 | m2a=False) |
||
586 | np.testing.assert_allclose(self.mlt_out, self.mlt_comp, rtol=1.0e-4) |
||
587 | |||
588 | def test_mlt_convert_arr(self): |
||
589 | """Test MLT calculation for an array""" |
||
590 | self.mlt_out = aacgmv2.convert_mlt(np.array(self.mlon_list), |
||
591 | self.dtime, m2a=False) |
||
592 | np.testing.assert_allclose(self.mlt_out, self.mlt_comp, rtol=1.0e-4) |
||
593 | |||
594 | def test_mlt_convert_list_w_times(self): |
||
595 | """Test MLT calculation for data and time arrays""" |
||
596 | self.dtime = [self.dtime for dd in self.mlon_list] |
||
597 | self.mlt_out = aacgmv2.convert_mlt(self.mlon_list, self.dtime, |
||
598 | m2a=False) |
||
599 | np.testing.assert_allclose(self.mlt_out, self.mlt_comp, rtol=1.0e-4) |
||
600 | |||
601 | def test_mlt_convert_change(self): |
||
602 | """Test that MLT changes with UT""" |
||
603 | self.mlt_out = aacgmv2.convert_mlt(self.mlon_list, self.dtime) |
||
604 | self.mlt_diff = np.array(self.mlt_out) \ |
||
605 | - np.array(aacgmv2.convert_mlt(self.mlon_list, self.dtime2)) |
||
606 | |||
607 | np.testing.assert_allclose(self.mlt_diff, self.diff_comp, rtol=1.0e-4) |
||
608 | |||
609 | def test_mlt_convert_multidim_failure(self): |
||
610 | """Test MLT calculation failure for multi-dimensional arrays""" |
||
611 | self.mlon_list = np.full(shape=(3, 2), fill_value=50.0) |
||
612 | with pytest.raises(ValueError): |
||
613 | aacgmv2.convert_mlt(self.mlon_list, self.dtime, m2a=False) |
||
614 | |||
615 | def test_mlt_convert_mismatch_failure(self): |
||
616 | """Test MLT calculation failure for mismatched array input""" |
||
617 | with pytest.raises(ValueError): |
||
618 | aacgmv2.convert_mlt(self.mlon_list, [self.dtime, self.dtime2], |
||
619 | m2a=False) |
||
620 | |||
621 | |||
622 | class TestCoeffPath: |
||
623 | |||
624 | def setup(self): |
||
625 | """Runs before every method to create a clean testing setup""" |
||
626 | os.environ['IGRF_COEFFS'] = "default_igrf" |
||
627 | os.environ['AACGM_v2_DAT_PREFIX'] = "default_coeff" |
||
628 | self.ref = {"igrf_file": os.environ['IGRF_COEFFS'], |
||
629 | "coeff_prefix": os.environ['AACGM_v2_DAT_PREFIX']} |
||
630 | |||
631 | def teardown(self): |
||
632 | """Runs after every method to clean up previous testing""" |
||
633 | del self.ref |
||
634 | |||
635 | @pytest.mark.parametrize("in_coeff", |
||
636 | [({}), |
||
637 | ({"igrf_file": "hi", "coeff_prefix": "bye"}), |
||
638 | ({"igrf_file": True, "coeff_prefix": True}), |
||
639 | ({"coeff_prefix": "hi"}), |
||
640 | ({"igrf_file": "hi"}), |
||
641 | ({"igrf_file": None, "coeff_prefix": None})]) |
||
642 | def test_set_coeff_path(self, in_coeff): |
||
643 | """Test the coefficient path setting using default values""" |
||
644 | # Update the reference key, if needed |
||
645 | for ref_key in in_coeff.keys(): |
||
646 | if in_coeff[ref_key] is True or in_coeff[ref_key] is None: |
||
647 | if ref_key == "igrf_file": |
||
648 | self.ref[ref_key] = aacgmv2.IGRF_COEFFS |
||
649 | elif ref_key == "coeff_prefix": |
||
650 | self.ref[ref_key] = aacgmv2.AACGM_v2_DAT_PREFIX |
||
651 | else: |
||
652 | self.ref[ref_key] = in_coeff[ref_key] |
||
653 | |||
654 | # Run the routine |
||
655 | aacgmv2.wrapper.set_coeff_path(**in_coeff) |
||
656 | |||
657 | # Ensure the environment variables were set correctly |
||
658 | if os.environ['IGRF_COEFFS'] != self.ref['igrf_file']: |
||
659 | raise AssertionError("{:} != {:}".format(os.environ['IGRF_COEFFS'], |
||
660 | self.ref['igrf_file'])) |
||
661 | if os.environ['AACGM_v2_DAT_PREFIX'] != self.ref['coeff_prefix']: |
||
662 | raise AssertionError("{:} != {:}".format( |
||
663 | os.environ['AACGM_v2_DAT_PREFIX'], self.ref['coeff_prefix'])) |
||
664 | |||
665 | |||
666 | class TestHeightReturns: |
||
667 | def setup(self): |
||
668 | """Runs before every method to create a clean testing setup""" |
||
669 | self.code = aacgmv2._aacgmv2.A2G |
||
670 | self.bad_code = aacgmv2._aacgmv2.BADIDEA |
||
671 | self.trace_code = aacgmv2._aacgmv2.TRACE |
||
672 | |||
673 | def teardown(self): |
||
674 | """Runs after every method to clean up previous testing""" |
||
675 | del self.code, self.bad_code |
||
676 | |||
677 | def test_low_height_good(self): |
||
678 | """ Test to see that a very low height is still accepted""" |
||
679 | |||
680 | assert aacgmv2.wrapper.test_height(-1, self.code) |
||
681 | |||
682 | def test_high_coeff_bad(self): |
||
683 | """ Test to see that a high altitude for coefficent use fails""" |
||
684 | |||
685 | assert not aacgmv2.wrapper.test_height(aacgmv2.high_alt_coeff + 10.0, |
||
686 | self.code) |
||
687 | |||
688 | def test_high_coeff_good(self): |
||
689 | """ Test a high altitude for coefficent use with badidea """ |
||
690 | |||
691 | assert aacgmv2.wrapper.test_height(aacgmv2.high_alt_coeff + 10.0, |
||
692 | self.bad_code) |
||
693 | |||
694 | def test_low_coeff_good(self): |
||
695 | """ Test that a normal height succeeds""" |
||
696 | assert aacgmv2.wrapper.test_height(aacgmv2.high_alt_coeff * 0.5, |
||
697 | self.code) |
||
698 | |||
699 | def test_high_trace_bad(self): |
||
700 | """ Test that a high trace height fails""" |
||
701 | assert not aacgmv2.wrapper.test_height(aacgmv2.high_alt_trace + 10.0, |
||
702 | self.code) |
||
703 | |||
704 | def test_low_trace_good(self): |
||
705 | """ Test that a high coefficient height succeeds with trace""" |
||
706 | assert aacgmv2.wrapper.test_height(aacgmv2.high_alt_coeff + 10.0, |
||
707 | self.trace_code) |
||
708 | |||
709 | def test_high_trace_good(self): |
||
710 | """ Test that a high trace height succeeds with badidea""" |
||
711 | assert aacgmv2.wrapper.test_height(aacgmv2.high_alt_trace + 10.0, |
||
712 | self.bad_code) |
||
713 | |||
714 | |||
715 | class TestPyLogging: |
||
716 | def setup(self): |
||
717 | """Runs before every method to create a clean testing setup""" |
||
718 | |||
719 | self.lwarn = u"" |
||
720 | self.lout = u"" |
||
721 | self.log_capture = StringIO() |
||
722 | aacgmv2.logger.addHandler(logging.StreamHandler(self.log_capture)) |
||
723 | aacgmv2.logger.setLevel(logging.INFO) |
||
724 | |||
725 | def teardown(self): |
||
726 | """Runs after every method to clean up previous testing""" |
||
727 | self.log_capture.close() |
||
728 | del self.lwarn, self.lout, self.log_capture |
||
729 | |||
730 | def test_warning_below_ground(self): |
||
731 | """ Test that a warning is issued if height < 0 for height test """ |
||
732 | self.lwarn = u"conversion not intended for altitudes < 0 km" |
||
733 | |||
734 | aacgmv2.wrapper.test_height(-1, 0) |
||
735 | self.lout = self.log_capture.getvalue() |
||
736 | if self.lout.find(self.lwarn) < 0: |
||
737 | raise AssertionError() |
||
738 | |||
739 | def test_warning_magnetosphere(self): |
||
740 | """ Test that a warning is issued if altitude is very high""" |
||
741 | self.lwarn = u"coordinates are not intended for the magnetosphere" |
||
742 | |||
743 | aacgmv2.wrapper.test_height(70000, aacgmv2._aacgmv2.TRACE) |
||
744 | self.lout = self.log_capture.getvalue() |
||
745 | if self.lout.find(self.lwarn) < 0: |
||
746 | raise AssertionError() |
||
747 | |||
748 | def test_warning_high_coeff(self): |
||
749 | """ Test that a warning is issued if altitude is very high""" |
||
750 | self.lwarn = u"must either use field-line tracing (trace=True" |
||
751 | |||
752 | aacgmv2.wrapper.test_height(3000, 0) |
||
753 | self.lout = self.log_capture.getvalue() |
||
754 | if self.lout.find(self.lwarn) < 0: |
||
755 | raise AssertionError() |
||
756 | |||
757 | def test_warning_single_loc_in_arr(self): |
||
758 | """ Test that user is warned they should be using simpler routine""" |
||
759 | self.lwarn = u"for a single location, consider using" |
||
760 | |||
761 | aacgmv2.convert_latlon_arr(60, 0, 300, dt.datetime(2015, 1, 1, 0, 0, 0)) |
||
762 | self.lout = self.log_capture.getvalue() |
||
763 | if self.lout.find(self.lwarn) < 0: |
||
764 | raise AssertionError() |
||
765 | |||
766 | |||
767 | class TestTimeReturns: |
||
768 | def setup(self): |
||
769 | """Runs before every method to create a clean testing setup""" |
||
770 | self.dtime = dt.datetime(2015, 1, 1, 0, 0, 0) |
||
771 | self.dtime2 = dt.datetime(2015, 1, 1, 10, 10, 10) |
||
772 | self.ddate = dt.date(2015, 1, 1) |
||
773 | |||
774 | def teardown(self): |
||
775 | """Runs after every method to clean up previous testing""" |
||
776 | del self.dtime, self.ddate, self.dtime2 |
||
777 | |||
778 | def test_good_time(self): |
||
779 | """ Test to see that a good datetime is accepted""" |
||
780 | |||
781 | assert self.dtime == aacgmv2.wrapper.test_time(self.dtime) |
||
782 | |||
783 | def test_good_time_with_nonzero_time(self): |
||
784 | """ Test to see that a good datetime with h/m/s is accepted""" |
||
785 | |||
786 | assert self.dtime2 == aacgmv2.wrapper.test_time(self.dtime2) |
||
787 | |||
788 | def test_good_date(self): |
||
789 | """ Test to see that a good date has a good datetime output""" |
||
790 | |||
791 | assert self.dtime == aacgmv2.wrapper.test_time(self.dtime) |
||
792 | |||
793 | def test_bad_time(self): |
||
794 | """ Test to see that a warning is raised with a bad time input""" |
||
795 | with pytest.raises(ValueError): |
||
796 | aacgmv2.wrapper.test_time(2015) |
||
797 |