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