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