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