| Total Complexity | 198 |
| Total Lines | 1025 |
| Duplicated Lines | 17.27 % |
| 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 | View Code Duplication | class TestFutureDepWarning: |
|
|
|
|||
| 16 | def setup(self): |
||
| 17 | # Initialize the routine to be tested |
||
| 18 | self.test_routine = None |
||
| 19 | self.test_args = [] |
||
| 20 | self.test_kwargs = {} |
||
| 21 | |||
| 22 | def teardown(self): |
||
| 23 | del self.test_routine, self.test_args, self.test_kwargs |
||
| 24 | |||
| 25 | def test_future_dep_warning(self): |
||
| 26 | """Test the implementation of FutureWarning for deprecated kwargs""" |
||
| 27 | if self.test_routine is None: |
||
| 28 | assert True |
||
| 29 | else: |
||
| 30 | with warnings.catch_warnings(record=True) as wout: |
||
| 31 | # Cause all warnings to always be triggered. |
||
| 32 | warnings.simplefilter("always") |
||
| 33 | |||
| 34 | # Trigger a warning. |
||
| 35 | self.test_routine(*self.test_args, **self.test_kwargs) |
||
| 36 | |||
| 37 | # Verify some things |
||
| 38 | assert len(wout) == 1 |
||
| 39 | assert issubclass(wout[-1].category, FutureWarning) |
||
| 40 | assert "Deprecated keyword" in str(wout[-1].message) |
||
| 41 | |||
| 42 | class TestDepConvertWarning(TestFutureDepWarning): |
||
| 43 | def setup(self): |
||
| 44 | self.dtime = dt.datetime(2015, 1, 1, 0, 0, 0) |
||
| 45 | self.test_routine = None |
||
| 46 | self.test_args = [] |
||
| 47 | self.test_kwargs = {} |
||
| 48 | |||
| 49 | def teardown(self): |
||
| 50 | del self.dtime, self.test_routine, self.test_args, self.test_kwargs |
||
| 51 | |||
| 52 | def test_convert_latlon_warning(self): |
||
| 53 | """Test future warning for convert_latlon""" |
||
| 54 | |||
| 55 | self.test_routine = aacgmv2.wrapper.convert_latlon |
||
| 56 | self.test_args = [60, 0, 300, self.dtime] |
||
| 57 | self.test_kwargs = {'code': 'TRACE'} |
||
| 58 | self.test_future_dep_warning() |
||
| 59 | |||
| 60 | def test_convert_latlon_arr_warning(self): |
||
| 61 | """Test future warning for convert_latlon_arr""" |
||
| 62 | |||
| 63 | self.test_routine = aacgmv2.wrapper.convert_latlon_arr |
||
| 64 | self.test_args = [[60, 60], [0, 0], [300, 300], self.dtime] |
||
| 65 | self.test_kwargs = {'code': 'TRACE'} |
||
| 66 | self.test_future_dep_warning() |
||
| 67 | |||
| 68 | def test_convert_latlon_time_error(self): |
||
| 69 | """Test single value latlon conversion with a bad datetime""" |
||
| 70 | self.test_routine = aacgmv2.wrapper.convert_latlon |
||
| 71 | self.test_args = [60, 0, 300, self.dtime] |
||
| 72 | self.test_kwargs = {'bad': 'keyword'} |
||
| 73 | with pytest.raises(TypeError): |
||
| 74 | self.test_routine(*self.test_args, **self.test_kwargs) |
||
| 75 | |||
| 76 | def test_convert_latlon_arr_time_error(self): |
||
| 77 | """Test single value latlon conversion with a bad datetime""" |
||
| 78 | self.test_routine = aacgmv2.wrapper.convert_latlon_arr |
||
| 79 | self.test_args = [[60, 60], [0, 0], [300, 300], self.dtime] |
||
| 80 | self.test_kwargs = {'bad': 'keyword'} |
||
| 81 | with pytest.raises(TypeError): |
||
| 82 | self.test_routine(*self.test_args, **self.test_kwargs) |
||
| 83 | |||
| 84 | |||
| 85 | class TestConvertLatLon: |
||
| 86 | def setup(self): |
||
| 87 | """Runs before every method to create a clean testing setup""" |
||
| 88 | self.dtime = dt.datetime(2015, 1, 1, 0, 0, 0) |
||
| 89 | self.ddate = dt.date(2015, 1, 1) |
||
| 90 | self.in_args = [60, 0, 300, self.dtime, 'TRACE'] |
||
| 91 | self.out = None |
||
| 92 | self.ref = [58.2258, 81.1685, 1.0457] |
||
| 93 | self.rtol = 1.0e-4 |
||
| 94 | |||
| 95 | def teardown(self): |
||
| 96 | """Runs after every method to clean up previous testing""" |
||
| 97 | del self.out, self.in_args, self.ref, self.rtol, self.dtime, self.ddate |
||
| 98 | |||
| 99 | def test_convert_latlon(self): |
||
| 100 | """Test single value latlon conversion""" |
||
| 101 | self.out = aacgmv2.convert_latlon(*self.in_args) |
||
| 102 | np.testing.assert_allclose(self.out, self.ref, rtol=self.rtol) |
||
| 103 | |||
| 104 | def test_convert_latlon_badidea(self): |
||
| 105 | """Test single value latlon conversion with a bad flag""" |
||
| 106 | self.in_args[2] = 3000.0 |
||
| 107 | self.in_args[-1] = "G2A|BADIDEA" |
||
| 108 | self.ref = [64.3568, 83.3027, 1.4694] |
||
| 109 | self.out = aacgmv2.convert_latlon(*self.in_args) |
||
| 110 | np.testing.assert_allclose(self.out, self.ref, rtol=self.rtol) |
||
| 111 | |||
| 112 | def test_convert_latlon_trace_badidea(self): |
||
| 113 | """Test single value latlon conversion with a bad flag for trace""" |
||
| 114 | self.in_args[2] = 7000.0 |
||
| 115 | self.in_args[-1] = "G2A|TRACE|BADIDEA" |
||
| 116 | self.ref = [69.3174, 85.0995, 2.0973] |
||
| 117 | self.out = aacgmv2.convert_latlon(*self.in_args) |
||
| 118 | np.testing.assert_allclose(self.out, self.ref, rtol=self.rtol) |
||
| 119 | |||
| 120 | @pytest.mark.skipif(version_info.major == 2, |
||
| 121 | reason='Not raised in Python 2') |
||
| 122 | def test_convert_latlon_location_failure(self): |
||
| 123 | """Test single value latlon conversion with a bad location""" |
||
| 124 | self.out = aacgmv2.convert_latlon(0, 0, 0, self.dtime, self.in_args[-1]) |
||
| 125 | assert np.all(np.isnan(np.array(self.out))) |
||
| 126 | |||
| 127 | def test_convert_latlon_time_failure(self): |
||
| 128 | """Test single value latlon conversion with a bad datetime""" |
||
| 129 | self.in_args[3] = None |
||
| 130 | with pytest.raises(ValueError): |
||
| 131 | self.out = aacgmv2.convert_latlon(*self.in_args) |
||
| 132 | |||
| 133 | def test_convert_latlon_datetime_date(self): |
||
| 134 | """Test single latlon conversion with date and datetime input""" |
||
| 135 | self.in_args[3] = self.ddate |
||
| 136 | self.out = aacgmv2.convert_latlon(*self.in_args) |
||
| 137 | np.testing.assert_allclose(self.out, self.ref, rtol=self.rtol) |
||
| 138 | |||
| 139 | def test_convert_latlon_maxalt_failure(self): |
||
| 140 | """test convert_latlon failure for an altitude too high for coeffs""" |
||
| 141 | self.in_args[2] = 2001 |
||
| 142 | self.in_args[-1] = "" |
||
| 143 | self.out = aacgmv2.convert_latlon(*self.in_args) |
||
| 144 | assert np.all(np.isnan(np.array(self.out))) |
||
| 145 | |||
| 146 | def test_convert_latlon_lat_high_failure(self): |
||
| 147 | """Test error return for co-latitudes above 90 for a single value""" |
||
| 148 | with pytest.raises(ValueError): |
||
| 149 | aacgmv2.convert_latlon(91, 0, 300, self.dtime) |
||
| 150 | |||
| 151 | def test_convert_latlon_lat_low_failure(self): |
||
| 152 | """Test error return for co-latitudes below -90 for a single value""" |
||
| 153 | with pytest.raises(ValueError): |
||
| 154 | aacgmv2.convert_latlon(-91, 0, 300, self.dtime) |
||
| 155 | |||
| 156 | class TestConvertLatLonArr: |
||
| 157 | View Code Duplication | def setup(self): |
|
| 158 | """Runs before every method to create a clean testing setup""" |
||
| 159 | self.dtime = dt.datetime(2015, 1, 1, 0, 0, 0) |
||
| 160 | self.ddate = dt.date(2015, 1, 1) |
||
| 161 | self.lat_in = [60.0, 61.0] |
||
| 162 | self.lon_in = [0.0, 0.0] |
||
| 163 | self.alt_in = [300.0, 300.0] |
||
| 164 | self.method = 'TRACE' |
||
| 165 | self.out = None |
||
| 166 | self.ref = [[58.22474610, 59.31648007], [81.17611033, 81.62281360], |
||
| 167 | [1.04566346, 1.04561304]] |
||
| 168 | self.rtol = 1.0e-4 |
||
| 169 | |||
| 170 | def teardown(self): |
||
| 171 | """Runs after every method to clean up previous testing""" |
||
| 172 | del self.lat_in, self.lon_in, self.alt_in, self.dtime, self.ddate |
||
| 173 | del self.method, self.out, self.ref, self.rtol |
||
| 174 | |||
| 175 | def test_convert_latlon_arr_single_val(self): |
||
| 176 | """Test array latlon conversion for a single value""" |
||
| 177 | self.out = aacgmv2.convert_latlon_arr(self.lat_in[0], self.lon_in[0], |
||
| 178 | self.alt_in[0], self.dtime, |
||
| 179 | self.method) |
||
| 180 | |||
| 181 | assert len(self.out) == len(self.ref) |
||
| 182 | assert [isinstance(oo, np.ndarray) and len(oo) == 1 for oo in self.out] |
||
| 183 | |||
| 184 | for i, oo in enumerate(self.out): |
||
| 185 | np.testing.assert_allclose(oo, [self.ref[i][0]], rtol=self.rtol) |
||
| 186 | |||
| 187 | View Code Duplication | def test_convert_latlon_arr_list_single(self): |
|
| 188 | """Test array latlon conversion for list input of single values""" |
||
| 189 | self.out = aacgmv2.convert_latlon_arr([self.lat_in[0]], |
||
| 190 | [self.lon_in[0]], |
||
| 191 | [self.alt_in[0]], self.dtime, |
||
| 192 | self.method) |
||
| 193 | |||
| 194 | assert len(self.out) == len(self.ref) |
||
| 195 | assert [isinstance(oo, np.ndarray) and len(oo) == 1 for oo in self.out] |
||
| 196 | |||
| 197 | for i, oo in enumerate(self.out): |
||
| 198 | np.testing.assert_allclose(oo, [self.ref[i][0]], rtol=self.rtol) |
||
| 199 | |||
| 200 | def test_convert_latlon_arr_list(self): |
||
| 201 | """Test array latlon conversion for list input""" |
||
| 202 | self.out = aacgmv2.convert_latlon_arr(self.lat_in, self.lon_in, |
||
| 203 | self.alt_in, self.dtime, |
||
| 204 | self.method) |
||
| 205 | |||
| 206 | assert len(self.out) == len(self.ref) |
||
| 207 | assert [isinstance(oo, np.ndarray) and len(oo) == len(self.ref[i]) |
||
| 208 | for i, oo in enumerate(self.out)] |
||
| 209 | |||
| 210 | for i, oo in enumerate(self.out): |
||
| 211 | np.testing.assert_allclose(oo, self.ref[i], rtol=self.rtol) |
||
| 212 | |||
| 213 | View Code Duplication | def test_convert_latlon_arr_arr_single(self): |
|
| 214 | """Test array latlon conversion for array input of shape (1,)""" |
||
| 215 | self.out = aacgmv2.convert_latlon_arr(np.array([self.lat_in[0]]), |
||
| 216 | np.array([self.lon_in[0]]), |
||
| 217 | np.array([self.alt_in[0]]), |
||
| 218 | self.dtime, self.method) |
||
| 219 | |||
| 220 | assert len(self.out) == len(self.ref) |
||
| 221 | assert [isinstance(oo, np.ndarray) and len(oo) == 1 for oo in self.out] |
||
| 222 | |||
| 223 | for i, oo in enumerate(self.out): |
||
| 224 | np.testing.assert_allclose(oo, [self.ref[i][0]], rtol=self.rtol) |
||
| 225 | |||
| 226 | View Code Duplication | def test_convert_latlon_arr_arr(self): |
|
| 227 | """Test array latlon conversion for array input""" |
||
| 228 | self.out = aacgmv2.convert_latlon_arr(np.array(self.lat_in), |
||
| 229 | np.array(self.lon_in), |
||
| 230 | np.array(self.alt_in), |
||
| 231 | self.dtime, self.method) |
||
| 232 | |||
| 233 | assert len(self.out) == len(self.ref) |
||
| 234 | assert [isinstance(oo, np.ndarray) and len(oo) == len(self.ref[i]) |
||
| 235 | for i, oo in enumerate(self.out)] |
||
| 236 | |||
| 237 | for i, oo in enumerate(self.out): |
||
| 238 | np.testing.assert_allclose(oo, self.ref[i], rtol=self.rtol) |
||
| 239 | |||
| 240 | def test_convert_latlon_arr_list_mix(self): |
||
| 241 | """Test array latlon conversion for mixed types with list""" |
||
| 242 | self.out = aacgmv2.convert_latlon_arr(self.lat_in, self.lon_in[0], |
||
| 243 | self.alt_in[0], self.dtime, |
||
| 244 | self.method) |
||
| 245 | |||
| 246 | assert len(self.out) == len(self.ref) |
||
| 247 | assert [isinstance(oo, np.ndarray) and len(oo) == len(self.ref[i]) |
||
| 248 | for i, oo in enumerate(self.out)] |
||
| 249 | |||
| 250 | for i, oo in enumerate(self.out): |
||
| 251 | np.testing.assert_allclose(oo, self.ref[i], rtol=self.rtol) |
||
| 252 | |||
| 253 | def test_convert_latlon_arr_arr_mix(self): |
||
| 254 | """Test array latlon conversion for mixed type with an array""" |
||
| 255 | self.out = aacgmv2.convert_latlon_arr(np.array(self.lat_in), |
||
| 256 | self.lon_in[0], self.alt_in[0], |
||
| 257 | self.dtime, self.method) |
||
| 258 | |||
| 259 | assert len(self.out) == len(self.ref) |
||
| 260 | assert [isinstance(oo, np.ndarray) and len(oo) == len(self.ref[i]) |
||
| 261 | for i, oo in enumerate(self.out)] |
||
| 262 | |||
| 263 | for i, oo in enumerate(self.out): |
||
| 264 | np.testing.assert_allclose(oo, self.ref[i], rtol=self.rtol) |
||
| 265 | |||
| 266 | def test_convert_latlon_arr_mult_failure(self): |
||
| 267 | """Test array latlon conversion for mix type with multi-dim array""" |
||
| 268 | with pytest.raises(ValueError): |
||
| 269 | aacgmv2.convert_latlon_arr(np.full(shape=(3,2), fill_value=50.0), |
||
| 270 | 0, 300, self.dtime) |
||
| 271 | |||
| 272 | |||
| 273 | View Code Duplication | def test_convert_latlon_arr_badidea(self): |
|
| 274 | """Test array latlon conversion for BADIDEA""" |
||
| 275 | self.method = "G2A | BADIDEA" |
||
| 276 | self.ref = [64.35677791, 83.30272053, 1.46944431] |
||
| 277 | self.out = aacgmv2.convert_latlon_arr(self.lat_in[0], self.lon_in[0], |
||
| 278 | [3000], self.dtime, self.method) |
||
| 279 | |||
| 280 | assert len(self.out) == len(self.ref) |
||
| 281 | assert [isinstance(oo, np.ndarray) and len(oo) == 1 for oo in self.out] |
||
| 282 | |||
| 283 | for i, oo in enumerate(self.out): |
||
| 284 | np.testing.assert_allclose(oo, [self.ref[i]], rtol=self.rtol) |
||
| 285 | |||
| 286 | View Code Duplication | def test_convert_latlon_arr_badidea_trace(self): |
|
| 287 | """Test array latlon conversion for BADIDEA with trace""" |
||
| 288 | self.method = "G2A | BADIDEA | TRACE" |
||
| 289 | self.ref = [69.317391, 85.099499, 2.09726] |
||
| 290 | self.out = aacgmv2.convert_latlon_arr(self.lat_in[0], self.lon_in[0], |
||
| 291 | [7000], self.dtime, self.method) |
||
| 292 | |||
| 293 | assert len(self.out) == len(self.ref) |
||
| 294 | assert [isinstance(oo, np.ndarray) and len(oo) == 1 for oo in self.out] |
||
| 295 | |||
| 296 | for i, oo in enumerate(self.out): |
||
| 297 | np.testing.assert_allclose(oo, [self.ref[i]], rtol=self.rtol) |
||
| 298 | |||
| 299 | @pytest.mark.skipif(version_info.major == 2, |
||
| 300 | reason='Not raised in Python 2') |
||
| 301 | def test_convert_latlon_arr_location_failure(self): |
||
| 302 | """Test array latlon conversion with a bad location""" |
||
| 303 | |||
| 304 | with warnings.catch_warnings(): |
||
| 305 | # Causes all warnings to be surpressed |
||
| 306 | warnings.simplefilter("ignore") |
||
| 307 | |||
| 308 | # Trigger a warning |
||
| 309 | self.out = aacgmv2.convert_latlon_arr([0], [0], [0], self.dtime, "") |
||
| 310 | |||
| 311 | # Test the output |
||
| 312 | assert len(self.out) == len(self.ref) |
||
| 313 | assert np.any(~np.isfinite(np.array(self.out))) |
||
| 314 | |||
| 315 | def test_convert_latlon_arr_mult_arr_unequal_failure(self): |
||
| 316 | """Test array latlon conversion for unequal sized arrays""" |
||
| 317 | with pytest.raises(ValueError): |
||
| 318 | aacgmv2.convert_latlon_arr(np.array([[60, 61, 62], [63, 64, 65]]), |
||
| 319 | np.array([0, 1]), 300, self.dtime) |
||
| 320 | |||
| 321 | def test_convert_latlon_arr_time_failure(self): |
||
| 322 | """Test array latlon conversion with a bad time""" |
||
| 323 | with pytest.raises(ValueError): |
||
| 324 | aacgmv2.convert_latlon_arr(self.lat_in, self.lon_in, self.alt_in, |
||
| 325 | None, self.method) |
||
| 326 | |||
| 327 | def test_convert_latlon_arr_datetime_date(self): |
||
| 328 | """Test array latlon conversion with date and datetime input""" |
||
| 329 | self.out = aacgmv2.convert_latlon_arr(self.lat_in, self.lon_in, |
||
| 330 | self.alt_in, self.ddate, |
||
| 331 | self.method) |
||
| 332 | |||
| 333 | assert len(self.out) == len(self.ref) |
||
| 334 | assert [isinstance(oo, np.ndarray) and len(oo) == len(self.ref[i]) |
||
| 335 | for i, oo in enumerate(self.out)] |
||
| 336 | |||
| 337 | for i, oo in enumerate(self.out): |
||
| 338 | np.testing.assert_allclose(oo, self.ref[i], rtol=self.rtol) |
||
| 339 | |||
| 340 | def test_convert_latlon_arr_maxalt_failure(self): |
||
| 341 | """test convert_latlon_arr failure for altitudes too high for coeffs""" |
||
| 342 | self.method = "" |
||
| 343 | self.out = aacgmv2.convert_latlon_arr(self.lat_in[0], self.lon_in[0], |
||
| 344 | [2001], self.dtime, self.method) |
||
| 345 | assert np.all(np.isnan(np.array(self.out))) |
||
| 346 | |||
| 347 | def test_convert_latlon_arr_lat_failure(self): |
||
| 348 | """Test error return for co-latitudes above 90 for an array""" |
||
| 349 | with pytest.raises(ValueError): |
||
| 350 | aacgmv2.convert_latlon_arr([91, 60, -91], 0, 300, self.dtime) |
||
| 351 | |||
| 352 | class TestGetAACGMCoord: |
||
| 353 | def setup(self): |
||
| 354 | """Runs before every method to create a clean testing setup""" |
||
| 355 | self.dtime = dt.datetime(2015, 1, 1, 0, 0, 0) |
||
| 356 | self.ddate = dt.date(2015, 1, 1) |
||
| 357 | self.in_args = [60, 0, 300, self.dtime, 'TRACE'] |
||
| 358 | self.out = None |
||
| 359 | self.ref = [58.22474610, 81.17611033, 0.18892] |
||
| 360 | self.rtol = 1.0e-4 |
||
| 361 | |||
| 362 | def teardown(self): |
||
| 363 | """Runs after every method to clean up previous testing""" |
||
| 364 | del self.out, self.in_args, self.ref, self.rtol, self.dtime, self.ddate |
||
| 365 | |||
| 366 | def test_get_aacgm_coord(self): |
||
| 367 | """Test single value AACGMV2 calculation, defaults to TRACE""" |
||
| 368 | self.out = aacgmv2.get_aacgm_coord(*self.in_args) |
||
| 369 | np.testing.assert_allclose(self.out, self.ref, rtol=self.rtol) |
||
| 370 | |||
| 371 | def test_get_aacgm_coord_badidea(self): |
||
| 372 | """Test single value AACGMV2 calculation with a bad flag""" |
||
| 373 | self.in_args[-1] = "BADIDEA" |
||
| 374 | self.in_args[2] = 3000 |
||
| 375 | self.ref = [64.3568, 83.3027, 0.3307] |
||
| 376 | self.out = aacgmv2.get_aacgm_coord(*self.in_args) |
||
| 377 | np.testing.assert_allclose(self.out, self.ref, rtol=self.rtol) |
||
| 378 | |||
| 379 | @pytest.mark.skipif(version_info.major == 2, |
||
| 380 | reason='Not raised in Python 2') |
||
| 381 | def test_get_aacgm_coord_location_failure(self): |
||
| 382 | """Test single value AACGMV2 calculation with a bad location""" |
||
| 383 | |||
| 384 | self.in_args[0] = 0.0 |
||
| 385 | self.in_args[2] = 0.0 |
||
| 386 | self.out = aacgmv2.get_aacgm_coord(*self.in_args) |
||
| 387 | np.all(np.isnan(np.array(self.out))) |
||
| 388 | |||
| 389 | def test_get_aacgm_coord_time_failure(self): |
||
| 390 | """Test single value AACGMV2 calculation with a bad datetime""" |
||
| 391 | self.in_args[3] = None |
||
| 392 | with pytest.raises(ValueError): |
||
| 393 | self.out = aacgmv2.get_aacgm_coord(*self.in_args) |
||
| 394 | |||
| 395 | def test_get_aacgm_coord_mlat_high_failure(self): |
||
| 396 | """Test error return for co-latitudes above 90 for a single value""" |
||
| 397 | self.in_args[0] = 91.0 |
||
| 398 | with pytest.raises(ValueError): |
||
| 399 | aacgmv2.get_aacgm_coord(*self.in_args) |
||
| 400 | |||
| 401 | def test_get_aacgm_coord_mlat_low_failure(self): |
||
| 402 | """Test error return for co-latitudes below -90 for a single value""" |
||
| 403 | self.in_args[0] = -91.0 |
||
| 404 | with pytest.raises(ValueError): |
||
| 405 | aacgmv2.get_aacgm_coord(*self.in_args) |
||
| 406 | |||
| 407 | def test_get_aacgm_coord_datetime_date(self): |
||
| 408 | """Test single AACGMV2 calculation with date and datetime input""" |
||
| 409 | self.in_args[3] = self.ddate |
||
| 410 | self.out = aacgmv2.get_aacgm_coord(*self.in_args) |
||
| 411 | np.testing.assert_allclose(self.out, self.ref, rtol=self.rtol) |
||
| 412 | |||
| 413 | def test_get_aacgm_coord_maxalt_failure(self): |
||
| 414 | """test get_aacgm_coord failure for an altitude too high for coeffs""" |
||
| 415 | self.in_args[2] = 2001 |
||
| 416 | self.in_args[-1] = "" |
||
| 417 | self.out = aacgmv2.get_aacgm_coord(*self.in_args) |
||
| 418 | assert np.all(np.isnan(np.array(self.out))) |
||
| 419 | |||
| 420 | class TestGetAACGMCoordArr: |
||
| 421 | View Code Duplication | def setup(self): |
|
| 422 | """Runs before every method to create a clean testing setup""" |
||
| 423 | self.dtime = dt.datetime(2015, 1, 1, 0, 0, 0) |
||
| 424 | self.ddate = dt.date(2015, 1, 1) |
||
| 425 | self.lat_in = [60.0, 61.0] |
||
| 426 | self.lon_in = [0.0, 0.0] |
||
| 427 | self.alt_in = [300.0, 300.0] |
||
| 428 | self.method = 'TRACE' |
||
| 429 | self.out = None |
||
| 430 | self.ref = [[58.22474610, 59.31648007], [81.17611033, 81.62281360], |
||
| 431 | [0.18891995, 0.21870017]] |
||
| 432 | self.rtol = 1.0e-4 |
||
| 433 | |||
| 434 | def teardown(self): |
||
| 435 | """Runs after every method to clean up previous testing""" |
||
| 436 | del self.out, self.ref, self.lat_in, self.dtime, self.ddate |
||
| 437 | del self.lon_in, self.alt_in, self.method, self.rtol |
||
| 438 | |||
| 439 | def test_get_aacgm_coord_arr_single_val(self): |
||
| 440 | """Test array AACGMV2 calculation for a single value""" |
||
| 441 | self.out = aacgmv2.get_aacgm_coord_arr(self.lat_in[0], self.lon_in[0], |
||
| 442 | self.alt_in[0], self.dtime, |
||
| 443 | self.method) |
||
| 444 | |||
| 445 | assert len(self.out) == len(self.ref) |
||
| 446 | assert [isinstance(oo, np.ndarray) and len(oo) == 1 for oo in self.out] |
||
| 447 | |||
| 448 | for i, oo in enumerate(self.out): |
||
| 449 | np.testing.assert_allclose(oo, [self.ref[i][0]], rtol=self.rtol) |
||
| 450 | |||
| 451 | View Code Duplication | def test_get_aacgm_coord_arr_list_single(self): |
|
| 452 | """Test array AACGMV2 calculation for list input of single values""" |
||
| 453 | self.out = aacgmv2.get_aacgm_coord_arr([self.lat_in[0]], |
||
| 454 | [self.lon_in[0]], |
||
| 455 | [self.alt_in[0]], self.dtime, |
||
| 456 | self.method) |
||
| 457 | |||
| 458 | assert len(self.out) == len(self.ref) |
||
| 459 | assert [isinstance(oo, np.ndarray) and len(oo) == 1 for oo in self.out] |
||
| 460 | |||
| 461 | for i, oo in enumerate(self.out): |
||
| 462 | np.testing.assert_allclose(oo, [self.ref[i][0]], rtol=self.rtol) |
||
| 463 | |||
| 464 | def test_get_aacgm_coord_arr_list(self): |
||
| 465 | """Test array AACGMV2 calculation for list input""" |
||
| 466 | self.out = aacgmv2.get_aacgm_coord_arr(self.lat_in,self.lon_in, |
||
| 467 | self.alt_in, self.dtime, |
||
| 468 | self.method) |
||
| 469 | |||
| 470 | assert len(self.out) == len(self.ref) |
||
| 471 | assert [isinstance(oo, np.ndarray) and len(oo) == len(self.lat_in) |
||
| 472 | for oo in self.out] |
||
| 473 | |||
| 474 | for i, oo in enumerate(self.out): |
||
| 475 | np.testing.assert_allclose(oo, self.ref[i], rtol=self.rtol) |
||
| 476 | |||
| 477 | View Code Duplication | def test_get_aacgm_coord_arr_arr_single(self): |
|
| 478 | """Test array AACGMV2 calculation for array with a single value""" |
||
| 479 | self.out = aacgmv2.get_aacgm_coord_arr(np.array([self.lat_in[0]]), |
||
| 480 | np.array([self.lon_in[0]]), |
||
| 481 | np.array([self.alt_in[0]]), |
||
| 482 | self.dtime, self.method) |
||
| 483 | |||
| 484 | |||
| 485 | assert len(self.out) == len(self.ref) |
||
| 486 | assert [isinstance(oo, np.ndarray) and len(oo) == 1 for oo in self.out] |
||
| 487 | |||
| 488 | for i, oo in enumerate(self.out): |
||
| 489 | np.testing.assert_allclose(oo, [self.ref[i][0]], rtol=self.rtol) |
||
| 490 | |||
| 491 | View Code Duplication | def test_get_aacgm_coord_arr_arr(self): |
|
| 492 | """Test array AACGMV2 calculation for an array""" |
||
| 493 | self.out = aacgmv2.get_aacgm_coord_arr(np.array(self.lat_in), |
||
| 494 | np.array(self.lon_in), |
||
| 495 | np.array(self.alt_in), |
||
| 496 | self.dtime, self.method) |
||
| 497 | |||
| 498 | assert len(self.out) == len(self.ref) |
||
| 499 | assert [isinstance(oo, np.ndarray) and len(oo) == len(self.lat_in) |
||
| 500 | for oo in self.out] |
||
| 501 | |||
| 502 | for i, oo in enumerate(self.out): |
||
| 503 | np.testing.assert_allclose(oo, self.ref[i], rtol=self.rtol) |
||
| 504 | |||
| 505 | def test_get_aacgm_coord_arr_list_mix(self): |
||
| 506 | """Test array AACGMV2 calculation for a list and floats""" |
||
| 507 | self.out = aacgmv2.get_aacgm_coord_arr(self.lat_in, self.lon_in[0], |
||
| 508 | self.alt_in[0], self.dtime, |
||
| 509 | self.method) |
||
| 510 | |||
| 511 | assert len(self.out) == len(self.ref) |
||
| 512 | assert [isinstance(oo, np.ndarray) and len(oo) == len(self.lat_in) |
||
| 513 | for oo in self.out] |
||
| 514 | |||
| 515 | for i, oo in enumerate(self.out): |
||
| 516 | np.testing.assert_allclose(oo, self.ref[i], rtol=self.rtol) |
||
| 517 | |||
| 518 | def test_get_aacgm_coord_arr_arr_mix(self): |
||
| 519 | """Test array AACGMV2 calculation for an array and floats""" |
||
| 520 | self.out = aacgmv2.get_aacgm_coord_arr(np.array(self.lat_in), |
||
| 521 | self.lon_in[0], self.alt_in[0], |
||
| 522 | self.dtime, self.method) |
||
| 523 | |||
| 524 | assert len(self.out) == len(self.ref) |
||
| 525 | assert [isinstance(oo, np.ndarray) and len(oo) == len(self.lat_in) |
||
| 526 | for oo in self.out] |
||
| 527 | |||
| 528 | for i, oo in enumerate(self.out): |
||
| 529 | np.testing.assert_allclose(oo, self.ref[i], rtol=self.rtol) |
||
| 530 | |||
| 531 | def test_get_aacgm_coord_arr_mult_failure(self): |
||
| 532 | """Test aacgm_coord_arr failure with multi-dim array input""" |
||
| 533 | |||
| 534 | with pytest.raises(ValueError): |
||
| 535 | (self.mlat_out, self.mlon_out, |
||
| 536 | self.mlt_out) = aacgmv2.get_aacgm_coord_arr( |
||
| 537 | np.array([[60, 61, 62], [63, 64, 65]]), 0, 300, self.dtime) |
||
| 538 | |||
| 539 | View Code Duplication | def test_get_aacgm_coord_arr_badidea(self): |
|
| 540 | """Test array AACGMV2 calculation for BADIDEA""" |
||
| 541 | self.method = "|".join([self.method, "BADIDEA"]) |
||
| 542 | self.out = aacgmv2.get_aacgm_coord_arr(self.lat_in[0], self.lon_in[0], |
||
| 543 | [3000.0], self.dtime, |
||
| 544 | self.method) |
||
| 545 | |||
| 546 | assert len(self.out) == len(self.ref) |
||
| 547 | assert [isinstance(oo, np.ndarray) and len(oo) == 1 for oo in self.out] |
||
| 548 | |||
| 549 | self.ref = [64.34650424987989, 83.30339395305012, 0.3307388620896745] |
||
| 550 | for i, oo in enumerate(self.out): |
||
| 551 | np.testing.assert_allclose(oo, self.ref[i], rtol=self.rtol) |
||
| 552 | |||
| 553 | @pytest.mark.skipif(version_info.major == 2, |
||
| 554 | reason='Not raised in Python 2') |
||
| 555 | def test_get_aacgm_coord_arr_location_failure(self): |
||
| 556 | """Test array AACGMV2 calculation with a bad location""" |
||
| 557 | self.out = aacgmv2.get_aacgm_coord_arr([0], [0], [0], self.dtime, |
||
| 558 | self.method) |
||
| 559 | |||
| 560 | |||
| 561 | assert len(self.out) == len(self.ref) |
||
| 562 | assert [isinstance(oo, np.ndarray) and len(oo) == 1 for oo in self.out] |
||
| 563 | assert np.any([np.isnan(oo) for oo in self.out]) |
||
| 564 | |||
| 565 | def test_get_aacgm_coord_arr_time_failure(self): |
||
| 566 | """Test array AACGMV2 calculation with a bad time""" |
||
| 567 | with pytest.raises(ValueError): |
||
| 568 | aacgmv2.get_aacgm_coord_arr(self.lat_in, self.lon_in, self.alt_in, |
||
| 569 | None, self.method) |
||
| 570 | |||
| 571 | def test_get_aacgm_coord_arr_mlat_failure(self): |
||
| 572 | """Test error return for co-latitudes above 90 for an array""" |
||
| 573 | |||
| 574 | self.lat_in = [91, 60, -91] |
||
| 575 | with pytest.raises(ValueError): |
||
| 576 | self.out = aacgmv2.get_aacgm_coord_arr(self.lat_in, self.lon_in[0], |
||
| 577 | self.alt_in[0], self.dtime, |
||
| 578 | self.method) |
||
| 579 | |||
| 580 | View Code Duplication | def test_get_aacgm_coord_arr_datetime_date(self): |
|
| 581 | """Test array AACGMV2 calculation with date and datetime input""" |
||
| 582 | self.out = aacgmv2.get_aacgm_coord_arr(self.lat_in, self.lon_in, |
||
| 583 | self.alt_in, self.ddate, |
||
| 584 | self.method) |
||
| 585 | self.ref = aacgmv2.get_aacgm_coord_arr(self.lat_in, self.lon_in, |
||
| 586 | self.alt_in, self.dtime, |
||
| 587 | self.method) |
||
| 588 | |||
| 589 | assert len(self.out) == len(self.ref) |
||
| 590 | assert [isinstance(oo, np.ndarray) and len(oo) == len(self.lat_in) |
||
| 591 | for oo in self.out] |
||
| 592 | |||
| 593 | for i, oo in enumerate(self.out): |
||
| 594 | np.testing.assert_allclose(oo, self.ref[i], rtol=self.rtol) |
||
| 595 | |||
| 596 | def test_get_aacgm_coord_arr_maxalt_failure(self): |
||
| 597 | """test aacgm_coord_arr failure for an altitude too high for coeff""" |
||
| 598 | self.method = "" |
||
| 599 | self.alt_in = [2001 for ll in self.lat_in] |
||
| 600 | self.out = aacgmv2.get_aacgm_coord_arr(self.lat_in, self.lon_in, |
||
| 601 | self.alt_in, self.dtime, |
||
| 602 | self.method) |
||
| 603 | |||
| 604 | assert len(self.out) == len(self.ref) |
||
| 605 | assert [isinstance(oo, np.ndarray) and len(oo) == len(self.lat_in) |
||
| 606 | for oo in self.out] |
||
| 607 | assert np.all(np.isnan(np.array(self.out))) |
||
| 608 | |||
| 609 | |||
| 610 | class TestConvertCode: |
||
| 611 | @classmethod |
||
| 612 | def test_convert_str_to_bit_g2a(self): |
||
| 613 | """Test conversion from string code to bit G2A""" |
||
| 614 | if aacgmv2.convert_str_to_bit("G2A") != aacgmv2._aacgmv2.G2A: |
||
| 615 | raise AssertionError() |
||
| 616 | |||
| 617 | @classmethod |
||
| 618 | def test_convert_str_to_bit_a2g(self): |
||
| 619 | """Test conversion from string code to bit A2G""" |
||
| 620 | if aacgmv2.convert_str_to_bit("A2G") != aacgmv2._aacgmv2.A2G: |
||
| 621 | raise AssertionError() |
||
| 622 | |||
| 623 | @classmethod |
||
| 624 | def test_convert_str_to_bit_trace(self): |
||
| 625 | """Test conversion from string code to bit TRACE""" |
||
| 626 | if aacgmv2.convert_str_to_bit("TRACE") != aacgmv2._aacgmv2.TRACE: |
||
| 627 | raise AssertionError() |
||
| 628 | |||
| 629 | @classmethod |
||
| 630 | def test_convert_str_to_bit_allowtrace(self): |
||
| 631 | """Test conversion from string code to bit ALLOWTRACE""" |
||
| 632 | if(aacgmv2.convert_str_to_bit("ALLOWTRACE") != |
||
| 633 | aacgmv2._aacgmv2.ALLOWTRACE): |
||
| 634 | raise AssertionError() |
||
| 635 | |||
| 636 | @classmethod |
||
| 637 | def test_convert_str_to_bit_badidea(self): |
||
| 638 | """Test conversion from string code to bit BADIDEA""" |
||
| 639 | if(aacgmv2.convert_str_to_bit("BADIDEA") != |
||
| 640 | aacgmv2._aacgmv2.BADIDEA): |
||
| 641 | raise AssertionError() |
||
| 642 | |||
| 643 | @classmethod |
||
| 644 | def test_convert_str_to_bit_geocentric(self): |
||
| 645 | """Test conversion from string code to bit GEOCENTRIC""" |
||
| 646 | if(aacgmv2.convert_str_to_bit("GEOCENTRIC") != |
||
| 647 | aacgmv2._aacgmv2.GEOCENTRIC): |
||
| 648 | raise AssertionError() |
||
| 649 | |||
| 650 | @classmethod |
||
| 651 | def test_convert_str_to_bit_lowercase(self): |
||
| 652 | """Test conversion from string code to bit for a lowercase code""" |
||
| 653 | if aacgmv2.convert_str_to_bit("g2a") != aacgmv2._aacgmv2.G2A: |
||
| 654 | raise AssertionError() |
||
| 655 | |||
| 656 | @classmethod |
||
| 657 | def test_convert_str_to_bit_spaces(self): |
||
| 658 | """Test conversion from string code to bit for a code with spaces""" |
||
| 659 | if(aacgmv2.convert_str_to_bit("G2A | trace") != |
||
| 660 | aacgmv2._aacgmv2.G2A + aacgmv2._aacgmv2.TRACE): |
||
| 661 | raise AssertionError() |
||
| 662 | |||
| 663 | @classmethod |
||
| 664 | def test_convert_str_to_bit_invalid(self): |
||
| 665 | """Test conversion from string code to bit for an invalid code""" |
||
| 666 | if aacgmv2.convert_str_to_bit("ggoogg|") != aacgmv2._aacgmv2.G2A: |
||
| 667 | raise AssertionError() |
||
| 668 | |||
| 669 | @classmethod |
||
| 670 | def test_convert_bool_to_bit_g2a(self): |
||
| 671 | """Test conversion from string code to bit G2A""" |
||
| 672 | if aacgmv2.convert_bool_to_bit() != aacgmv2._aacgmv2.G2A: |
||
| 673 | raise AssertionError() |
||
| 674 | |||
| 675 | @classmethod |
||
| 676 | def test_convert_bool_to_bit_a2g(self): |
||
| 677 | """Test conversion from string code to bit A2G""" |
||
| 678 | if aacgmv2.convert_bool_to_bit(a2g=True) != aacgmv2._aacgmv2.A2G: |
||
| 679 | raise AssertionError() |
||
| 680 | |||
| 681 | @classmethod |
||
| 682 | def test_convert_bool_to_bit_trace(self): |
||
| 683 | """Test conversion from string code to bit TRACE""" |
||
| 684 | if aacgmv2.convert_bool_to_bit(trace=True) != aacgmv2._aacgmv2.TRACE: |
||
| 685 | raise AssertionError() |
||
| 686 | |||
| 687 | @classmethod |
||
| 688 | def test_convert_bool_to_bit_allowtrace(self): |
||
| 689 | """Test conversion from string code to bit ALLOWTRACE""" |
||
| 690 | if(aacgmv2.convert_bool_to_bit(allowtrace=True) != |
||
| 691 | aacgmv2._aacgmv2.ALLOWTRACE): |
||
| 692 | raise AssertionError() |
||
| 693 | |||
| 694 | @classmethod |
||
| 695 | def test_convert_bool_to_bit_badidea(self): |
||
| 696 | """Test conversion from string code to bit BADIDEA""" |
||
| 697 | if(aacgmv2.convert_bool_to_bit(badidea=True) != |
||
| 698 | aacgmv2._aacgmv2.BADIDEA): |
||
| 699 | raise AssertionError() |
||
| 700 | |||
| 701 | @classmethod |
||
| 702 | def test_convert_bool_to_bit_geocentric(self): |
||
| 703 | """Test conversion from string code to bit GEOCENTRIC""" |
||
| 704 | if(aacgmv2.convert_bool_to_bit(geocentric=True) != |
||
| 705 | aacgmv2._aacgmv2.GEOCENTRIC): |
||
| 706 | raise AssertionError() |
||
| 707 | |||
| 708 | class TestMLTConvert: |
||
| 709 | def setup(self): |
||
| 710 | """Runs before every method to create a clean testing setup""" |
||
| 711 | self.dtime = dt.datetime(2015, 1, 1, 0, 0, 0) |
||
| 712 | self.dtime2 = dt.datetime(2015, 1, 1, 10, 0, 0) |
||
| 713 | self.ddate = dt.date(2015, 1, 1) |
||
| 714 | self.mlon_out = None |
||
| 715 | self.mlt_out = None |
||
| 716 | self.mlt_diff = None |
||
| 717 | self.mlon_list = [270.0, 80.0, -95.0] |
||
| 718 | self.mlt_list = [12.0, 25.0, -1.0] |
||
| 719 | self.mlon_comp = [-101.657689, 93.34231102, 63.34231102] |
||
| 720 | self.mlt_comp = [12.77717927, 0.1105126, 12.44384593] |
||
| 721 | self.diff_comp = np.ones(shape=(3,)) * -10.52411552 |
||
| 722 | |||
| 723 | def teardown(self): |
||
| 724 | """Runs after every method to clean up previous testing""" |
||
| 725 | del self.mlon_out, self.mlt_out, self.mlt_list, self.mlon_list |
||
| 726 | del self.mlon_comp, self.mlt_comp, self.mlt_diff, self.diff_comp |
||
| 727 | |||
| 728 | def test_date_input(self): |
||
| 729 | """Test to see that the date input works""" |
||
| 730 | self.mlt_out = aacgmv2.convert_mlt(self.mlon_list, self.ddate, |
||
| 731 | m2a=False) |
||
| 732 | np.testing.assert_allclose(self.mlt_out, self.mlt_comp, rtol=1.0e-4) |
||
| 733 | |||
| 734 | def test_datetime_exception(self): |
||
| 735 | """Test to see that a value error is raised with bad time input""" |
||
| 736 | with pytest.raises(ValueError): |
||
| 737 | self.mlt_out = aacgmv2.wrapper.convert_mlt(self.mlon_list, 1997) |
||
| 738 | |||
| 739 | def test_inv_convert_mlt_single(self): |
||
| 740 | """Test MLT inversion for a single value""" |
||
| 741 | for i,mlt in enumerate(self.mlt_list): |
||
| 742 | self.mlon_out = aacgmv2.convert_mlt(mlt, self.dtime, m2a=True) |
||
| 743 | np.testing.assert_almost_equal(self.mlon_out, self.mlon_comp[i], |
||
| 744 | decimal=4) |
||
| 745 | |||
| 746 | def test_inv_convert_mlt_list(self): |
||
| 747 | """Test MLT inversion for a list""" |
||
| 748 | self.mlon_out = aacgmv2.convert_mlt(self.mlt_list, self.dtime, m2a=True) |
||
| 749 | np.testing.assert_allclose(self.mlon_out, self.mlon_comp, rtol=1.0e-4) |
||
| 750 | |||
| 751 | def test_inv_convert_mlt_arr(self): |
||
| 752 | """Test MLT inversion for an array""" |
||
| 753 | self.mlon_out = aacgmv2.convert_mlt(np.array(self.mlt_list), self.dtime, |
||
| 754 | m2a=True) |
||
| 755 | |||
| 756 | np.testing.assert_allclose(self.mlon_out, self.mlon_comp, rtol=1.0e-4) |
||
| 757 | |||
| 758 | def test_inv_convert_mlt_wrapping(self): |
||
| 759 | """Test MLT wrapping""" |
||
| 760 | self.mlon_out = aacgmv2.convert_mlt(np.array([1, 25, -1, 23]), |
||
| 761 | self.dtime, m2a=True) |
||
| 762 | |||
| 763 | np.testing.assert_almost_equal(self.mlon_out[0], self.mlon_out[1], |
||
| 764 | decimal=6) |
||
| 765 | np.testing.assert_almost_equal(self.mlon_out[2], self.mlon_out[3], |
||
| 766 | decimal=6) |
||
| 767 | |||
| 768 | def test_mlt_convert_mlon_wrapping(self): |
||
| 769 | """Test mlon wrapping""" |
||
| 770 | self.mlt_out = aacgmv2.convert_mlt(np.array([270, -90, 1, 361]), |
||
| 771 | self.dtime, m2a=False) |
||
| 772 | |||
| 773 | np.testing.assert_almost_equal(self.mlt_out[0], self.mlt_out[1], |
||
| 774 | decimal=6) |
||
| 775 | np.testing.assert_almost_equal(self.mlt_out[2], self.mlt_out[3], |
||
| 776 | decimal=6) |
||
| 777 | |||
| 778 | def test_mlt_convert_single(self): |
||
| 779 | """Test MLT calculation for a single value""" |
||
| 780 | for i,mlon in enumerate(self.mlon_list): |
||
| 781 | self.mlt_out = aacgmv2.convert_mlt(mlon, self.dtime, m2a=False) |
||
| 782 | np.testing.assert_almost_equal(self.mlt_out, self.mlt_comp[i], |
||
| 783 | decimal=4) |
||
| 784 | |||
| 785 | def test_mlt_convert_list(self): |
||
| 786 | """Test MLT calculation for a list""" |
||
| 787 | self.mlt_out = aacgmv2.convert_mlt(self.mlon_list, self.dtime, |
||
| 788 | m2a=False) |
||
| 789 | np.testing.assert_allclose(self.mlt_out, self.mlt_comp, rtol=1.0e-4) |
||
| 790 | |||
| 791 | def test_mlt_convert_arr(self): |
||
| 792 | """Test MLT calculation for an array""" |
||
| 793 | self.mlt_out = aacgmv2.convert_mlt(np.array(self.mlon_list), |
||
| 794 | self.dtime, m2a=False) |
||
| 795 | np.testing.assert_allclose(self.mlt_out, self.mlt_comp, rtol=1.0e-4) |
||
| 796 | |||
| 797 | def test_mlt_convert_list_w_times(self): |
||
| 798 | """Test MLT calculation for data and time arrays""" |
||
| 799 | self.dtime = [self.dtime for dd in self.mlon_list] |
||
| 800 | self.mlt_out = aacgmv2.convert_mlt(self.mlon_list, self.dtime, |
||
| 801 | m2a=False) |
||
| 802 | np.testing.assert_allclose(self.mlt_out, self.mlt_comp, rtol=1.0e-4) |
||
| 803 | |||
| 804 | def test_mlt_convert_change(self): |
||
| 805 | """Test that MLT changes with UT""" |
||
| 806 | self.mlt_out = aacgmv2.convert_mlt(self.mlon_list, self.dtime) |
||
| 807 | self.mlt_diff = np.array(self.mlt_out) \ |
||
| 808 | - np.array(aacgmv2.convert_mlt(self.mlon_list, self.dtime2)) |
||
| 809 | |||
| 810 | np.testing.assert_allclose(self.mlt_diff, self.diff_comp, rtol=1.0e-4) |
||
| 811 | |||
| 812 | def test_mlt_convert_multidim_failure(self): |
||
| 813 | """Test MLT calculation failure for multi-dimensional arrays""" |
||
| 814 | self.mlon_list = np.full(shape=(3,2), fill_value=50.0) |
||
| 815 | with pytest.raises(ValueError): |
||
| 816 | aacgmv2.convert_mlt(self.mlon_list, self.dtime, m2a=False) |
||
| 817 | |||
| 818 | def test_mlt_convert_mismatch_failure(self): |
||
| 819 | """Test MLT calculation failure for mismatched array input""" |
||
| 820 | with pytest.raises(ValueError): |
||
| 821 | aacgmv2.convert_mlt(self.mlon_list, [self.dtime, self.dtime2], |
||
| 822 | m2a=False) |
||
| 823 | |||
| 824 | class TestCoeffPath: |
||
| 825 | |||
| 826 | def setup(self): |
||
| 827 | """Runs before every method to create a clean testing setup""" |
||
| 828 | os.environ['IGRF_COEFFS'] = "default_igrf" |
||
| 829 | os.environ['AACGM_v2_DAT_PREFIX'] = "default_coeff" |
||
| 830 | self.default_igrf = os.environ['IGRF_COEFFS'] |
||
| 831 | self.default_coeff = os.environ['AACGM_v2_DAT_PREFIX'] |
||
| 832 | |||
| 833 | def teardown(self): |
||
| 834 | """Runs after every method to clean up previous testing""" |
||
| 835 | del self.default_igrf, self.default_coeff |
||
| 836 | |||
| 837 | def test_set_coeff_path_default(self): |
||
| 838 | """Test the coefficient path setting using default values""" |
||
| 839 | aacgmv2.wrapper.set_coeff_path() |
||
| 840 | |||
| 841 | if os.environ['IGRF_COEFFS'] != self.default_igrf: |
||
| 842 | raise AssertionError() |
||
| 843 | if os.environ['AACGM_v2_DAT_PREFIX'] != self.default_coeff: |
||
| 844 | raise AssertionError() |
||
| 845 | |||
| 846 | @classmethod |
||
| 847 | def test_set_coeff_path_string(self): |
||
| 848 | """Test the coefficient path setting using two user specified values""" |
||
| 849 | aacgmv2.wrapper.set_coeff_path("hi", "bye") |
||
| 850 | |||
| 851 | if os.environ['IGRF_COEFFS'] != "hi": |
||
| 852 | raise AssertionError() |
||
| 853 | if os.environ['AACGM_v2_DAT_PREFIX'] != "bye": |
||
| 854 | raise AssertionError() |
||
| 855 | |||
| 856 | @classmethod |
||
| 857 | def test_set_coeff_path_true(self): |
||
| 858 | """Test the coefficient path setting using the module values""" |
||
| 859 | aacgmv2.wrapper.set_coeff_path(True, True) |
||
| 860 | |||
| 861 | if os.environ['IGRF_COEFFS'] != aacgmv2.IGRF_COEFFS: |
||
| 862 | raise AssertionError() |
||
| 863 | if os.environ['AACGM_v2_DAT_PREFIX'] != aacgmv2.AACGM_v2_DAT_PREFIX: |
||
| 864 | raise AssertionError() |
||
| 865 | |||
| 866 | def test_set_only_aacgm_coeff_path(self): |
||
| 867 | """Test the coefficient path setting using a mix of input""" |
||
| 868 | aacgmv2.wrapper.set_coeff_path(coeff_prefix="hi") |
||
| 869 | |||
| 870 | if os.environ['IGRF_COEFFS'] != self.default_igrf: |
||
| 871 | raise AssertionError() |
||
| 872 | if os.environ['AACGM_v2_DAT_PREFIX'] != "hi": |
||
| 873 | raise AssertionError() |
||
| 874 | |||
| 875 | def test_set_only_igrf_coeff_path(self): |
||
| 876 | """Test the coefficient path setting using a mix of input""" |
||
| 877 | aacgmv2.wrapper.set_coeff_path(igrf_file="hi") |
||
| 878 | |||
| 879 | if os.environ['IGRF_COEFFS'] != "hi": |
||
| 880 | raise AssertionError() |
||
| 881 | if os.environ['AACGM_v2_DAT_PREFIX'] != self.default_coeff: |
||
| 882 | raise AssertionError() |
||
| 883 | |||
| 884 | @classmethod |
||
| 885 | def test_set_both_mixed(self): |
||
| 886 | """Test the coefficient path setting using a mix of input""" |
||
| 887 | aacgmv2.wrapper.set_coeff_path(igrf_file=True, coeff_prefix="hi") |
||
| 888 | |||
| 889 | if os.environ['IGRF_COEFFS'] != aacgmv2.IGRF_COEFFS: |
||
| 890 | raise AssertionError() |
||
| 891 | if os.environ['AACGM_v2_DAT_PREFIX'] != "hi": |
||
| 892 | raise AssertionError() |
||
| 893 | |||
| 894 | class TestHeightReturns: |
||
| 895 | def setup(self): |
||
| 896 | """Runs before every method to create a clean testing setup""" |
||
| 897 | self.code = aacgmv2._aacgmv2.A2G |
||
| 898 | self.bad_code = aacgmv2._aacgmv2.BADIDEA |
||
| 899 | self.trace_code = aacgmv2._aacgmv2.TRACE |
||
| 900 | |||
| 901 | def teardown(self): |
||
| 902 | """Runs after every method to clean up previous testing""" |
||
| 903 | del self.code, self.bad_code |
||
| 904 | |||
| 905 | def test_low_height_good(self): |
||
| 906 | """ Test to see that a very low height is still accepted""" |
||
| 907 | |||
| 908 | assert aacgmv2.wrapper.test_height(-1, self.code) |
||
| 909 | |||
| 910 | def test_high_coeff_bad(self): |
||
| 911 | """ Test to see that a high altitude for coefficent use fails""" |
||
| 912 | |||
| 913 | assert not aacgmv2.wrapper.test_height(aacgmv2.high_alt_coeff+10.0, |
||
| 914 | self.code) |
||
| 915 | |||
| 916 | def test_high_coeff_good(self): |
||
| 917 | """ Test a high altitude for coefficent use with badidea """ |
||
| 918 | |||
| 919 | assert aacgmv2.wrapper.test_height(aacgmv2.high_alt_coeff+10.0, |
||
| 920 | self.bad_code) |
||
| 921 | |||
| 922 | def test_low_coeff_good(self): |
||
| 923 | """ Test that a normal height succeeds""" |
||
| 924 | assert aacgmv2.wrapper.test_height(aacgmv2.high_alt_coeff*0.5, |
||
| 925 | self.code) |
||
| 926 | |||
| 927 | def test_high_trace_bad(self): |
||
| 928 | """ Test that a high trace height fails""" |
||
| 929 | assert not aacgmv2.wrapper.test_height(aacgmv2.high_alt_trace+10.0, |
||
| 930 | self.code) |
||
| 931 | |||
| 932 | def test_low_trace_good(self): |
||
| 933 | """ Test that a high coefficient height succeeds with trace""" |
||
| 934 | assert aacgmv2.wrapper.test_height(aacgmv2.high_alt_coeff+10.0, |
||
| 935 | self.trace_code) |
||
| 936 | |||
| 937 | def test_high_trace_good(self): |
||
| 938 | """ Test that a high trace height succeeds with badidea""" |
||
| 939 | assert aacgmv2.wrapper.test_height(aacgmv2.high_alt_trace+10.0, |
||
| 940 | self.bad_code) |
||
| 941 | |||
| 942 | |||
| 943 | class TestPyLogging: |
||
| 944 | def setup(self): |
||
| 945 | """Runs before every method to create a clean testing setup""" |
||
| 946 | |||
| 947 | self.lwarn = u"" |
||
| 948 | self.lout = u"" |
||
| 949 | self.log_capture = StringIO() |
||
| 950 | aacgmv2.logger.addHandler(logging.StreamHandler(self.log_capture)) |
||
| 951 | aacgmv2.logger.setLevel(logging.INFO) |
||
| 952 | |||
| 953 | def teardown(self): |
||
| 954 | """Runs after every method to clean up previous testing""" |
||
| 955 | self.log_capture.close() |
||
| 956 | del self.lwarn, self.lout, self.log_capture |
||
| 957 | |||
| 958 | |||
| 959 | def test_warning_below_ground(self): |
||
| 960 | """ Test that a warning is issued if height < 0 for height test """ |
||
| 961 | self.lwarn = u"conversion not intended for altitudes < 0 km" |
||
| 962 | |||
| 963 | aacgmv2.wrapper.test_height(-1, 0) |
||
| 964 | self.lout = self.log_capture.getvalue() |
||
| 965 | if self.lout.find(self.lwarn) < 0: |
||
| 966 | raise AssertionError() |
||
| 967 | |||
| 968 | def test_warning_magnetosphere(self): |
||
| 969 | """ Test that a warning is issued if altitude is very high""" |
||
| 970 | self.lwarn = u"coordinates are not intended for the magnetosphere" |
||
| 971 | |||
| 972 | aacgmv2.wrapper.test_height(70000, aacgmv2._aacgmv2.TRACE) |
||
| 973 | self.lout = self.log_capture.getvalue() |
||
| 974 | if self.lout.find(self.lwarn) < 0: |
||
| 975 | raise AssertionError() |
||
| 976 | |||
| 977 | def test_warning_high_coeff(self): |
||
| 978 | """ Test that a warning is issued if altitude is very high""" |
||
| 979 | self.lwarn = u"must either use field-line tracing (trace=True" |
||
| 980 | |||
| 981 | aacgmv2.wrapper.test_height(3000, 0) |
||
| 982 | self.lout = self.log_capture.getvalue() |
||
| 983 | if self.lout.find(self.lwarn) < 0: |
||
| 984 | raise AssertionError() |
||
| 985 | |||
| 986 | def test_warning_single_loc_in_arr(self): |
||
| 987 | """ Test that user is warned they should be using simpler routine""" |
||
| 988 | self.lwarn = u"for a single location, consider using" |
||
| 989 | |||
| 990 | aacgmv2.convert_latlon_arr(60, 0, 300, dt.datetime(2015,1,1,0,0,0)) |
||
| 991 | self.lout = self.log_capture.getvalue() |
||
| 992 | if self.lout.find(self.lwarn) < 0: |
||
| 993 | raise AssertionError() |
||
| 994 | |||
| 995 | class TestTimeReturns: |
||
| 996 | def setup(self): |
||
| 997 | """Runs before every method to create a clean testing setup""" |
||
| 998 | self.dtime = dt.datetime(2015, 1, 1, 0, 0, 0) |
||
| 999 | self.dtime2 = dt.datetime(2015, 1, 1, 10, 10, 10) |
||
| 1000 | self.ddate = dt.date(2015, 1, 1) |
||
| 1001 | |||
| 1002 | def teardown(self): |
||
| 1003 | """Runs after every method to clean up previous testing""" |
||
| 1004 | del self.dtime, self.ddate, self.dtime2 |
||
| 1005 | |||
| 1006 | def test_good_time(self): |
||
| 1007 | """ Test to see that a good datetime is accepted""" |
||
| 1008 | |||
| 1009 | assert self.dtime == aacgmv2.wrapper.test_time(self.dtime) |
||
| 1010 | |||
| 1011 | def test_good_time_with_nonzero_time(self): |
||
| 1012 | """ Test to see that a good datetime with h/m/s is accepted""" |
||
| 1013 | |||
| 1014 | assert self.dtime2 == aacgmv2.wrapper.test_time(self.dtime2) |
||
| 1015 | |||
| 1016 | def test_good_date(self): |
||
| 1017 | """ Test to see that a good date has a good datetime output""" |
||
| 1018 | |||
| 1019 | assert self.dtime == aacgmv2.wrapper.test_time(self.dtime) |
||
| 1020 | |||
| 1021 | def test_bad_time(self): |
||
| 1022 | """ Test to see that a warning is raised with a bad time input""" |
||
| 1023 | with pytest.raises(ValueError): |
||
| 1024 | aacgmv2.wrapper.test_time(2015) |
||
| 1025 |