| Total Complexity | 176 |
| Total Lines | 1408 |
| Duplicated Lines | 15.98 % |
| 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_Apex 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 | |||
| 3 | from __future__ import division, absolute_import, unicode_literals |
||
| 4 | |||
| 5 | import datetime as dt |
||
| 6 | import itertools |
||
| 7 | import numpy as np |
||
| 8 | from numpy.testing import assert_allclose |
||
| 9 | import os |
||
| 10 | import pytest |
||
| 11 | |||
| 12 | from apexpy import fortranapex as fa |
||
| 13 | from apexpy import Apex, ApexHeightError, helpers |
||
| 14 | |||
| 15 | |||
| 16 | # ---------------------------------------------------------------------------- |
||
| 17 | # NOTE: whenever function outputs are tested against hard-coded numbers, the |
||
| 18 | # test results (numbers) were obtained by running the code that is tested. |
||
| 19 | # Therefore these tests below only check that nothing changes when refactoring, |
||
| 20 | # etc., and not if the results are actually correct. |
||
| 21 | # ----------------------------------------------------------------------------- |
||
| 22 | |||
| 23 | |||
| 24 | # ============================================================================ |
||
| 25 | # Test initiating the Apex class |
||
| 26 | # ============================================================================ |
||
| 27 | |||
| 28 | |||
| 29 | def test_init_defaults(): |
||
| 30 | Apex() |
||
| 31 | |||
| 32 | |||
| 33 | def test_init_date_int(): |
||
| 34 | apex_out = Apex(date=2015) |
||
| 35 | assert apex_out.year == 2015 |
||
| 36 | |||
| 37 | |||
| 38 | def test_init_date_float(): |
||
| 39 | apex_out = Apex(date=2015.5) |
||
| 40 | assert apex_out.year == 2015.5 |
||
| 41 | |||
| 42 | |||
| 43 | def test_init_date(): |
||
| 44 | date = dt.date(2015, 1, 1) |
||
| 45 | apex_out = Apex(date=date) |
||
| 46 | assert apex_out.year == helpers.toYearFraction(date) |
||
| 47 | |||
| 48 | |||
| 49 | def test_init_datetime(): |
||
| 50 | datetime = dt.datetime(2015, 6, 1, 18, 23, 45) |
||
| 51 | apex_out = Apex(date=datetime) |
||
| 52 | assert apex_out.year == helpers.toYearFraction(datetime) |
||
| 53 | |||
| 54 | |||
| 55 | def test_init_datafile_IOError(): |
||
| 56 | with pytest.raises(IOError): |
||
| 57 | Apex(date=2015, datafile='foo/path/to/datafile.blah') |
||
| 58 | |||
| 59 | |||
| 60 | # ============================================================================ |
||
| 61 | # Test the low-level interfaces to the fortran wrappers |
||
| 62 | # ============================================================================ |
||
| 63 | |||
| 64 | def test__geo2qd_scalar(): |
||
| 65 | apex_out = Apex(date=2000, refh=300) |
||
| 66 | for lat in [0, 30, 60, 89]: |
||
| 67 | for lon in [-179, -90, 0, 90, 180]: |
||
| 68 | assert_allclose(apex_out._geo2qd(lat, lon, 100), |
||
| 69 | fa.apxg2q(lat, lon, 100, 0)[:2]) |
||
| 70 | |||
| 71 | |||
| 72 | def test__geo2qd_array(): |
||
| 73 | apex_out = Apex(date=2000, refh=300) |
||
| 74 | lats, lons = apex_out._geo2qd([[0, 30], [60, 90]], 15, |
||
| 75 | [[100, 200], [300, 400]]) |
||
| 76 | lat1, lon1 = fa.apxg2q(0, 15, 100, 0)[:2] |
||
| 77 | lat2, lon2 = fa.apxg2q(30, 15, 200, 0)[:2] |
||
| 78 | lat3, lon3 = fa.apxg2q(60, 15, 300, 0)[:2] |
||
| 79 | lat4, lon4 = fa.apxg2q(90, 15, 400, 0)[:2] |
||
| 80 | assert_allclose(lats.astype(float), np.array([[lat1, lat2], [lat3, lat4]], |
||
| 81 | dtype=float)) |
||
| 82 | assert_allclose(lons.astype(float), np.array([[lon1, lon2], [lon3, lon4]], |
||
| 83 | dtype=float)) |
||
| 84 | |||
| 85 | |||
| 86 | def test__geo2qd_longitude(): |
||
| 87 | apex_out = Apex(date=2000, refh=300) |
||
| 88 | assert_allclose(apex_out._geo2qd(60, 180, 100), |
||
| 89 | fa.apxg2q(60, 180, 100, 0)[:2]) |
||
| 90 | assert_allclose(apex_out._geo2qd(60, -180, 100), |
||
| 91 | fa.apxg2q(60, -180, 100, 0)[:2]) |
||
| 92 | assert_allclose(apex_out._geo2qd(60, -180, 100), |
||
| 93 | apex_out._geo2qd(60, 180, 100)) |
||
| 94 | for i in range(-5, 5): |
||
| 95 | for lat in [0, 30, 60, 90]: |
||
| 96 | assert_allclose(apex_out._geo2qd(lat, 15 + i * 360, 100), |
||
| 97 | fa.apxg2q(lat, 15, 100, 0)[:2]) |
||
| 98 | |||
| 99 | |||
| 100 | def test__geo2apex_scalar(): |
||
| 101 | apex_out = Apex(date=2000, refh=300) |
||
| 102 | for lat in [0, 30, 60, 89]: |
||
| 103 | for lon in [-179, -90, 0, 90, 180]: |
||
| 104 | assert_allclose(apex_out._geo2apex(lat, lon, 100), |
||
| 105 | fa.apxg2all(lat, lon, 100, 300, 0)[2:4]) |
||
| 106 | |||
| 107 | |||
| 108 | def test__geo2apex_array(): |
||
| 109 | apex_out = Apex(date=2000, refh=300) |
||
| 110 | lats, lons = apex_out._geo2apex([[0, 30], [60, 90]], 15, |
||
| 111 | [[100, 200], [300, 400]]) |
||
| 112 | lat1, lon1 = fa.apxg2all(0, 15, 100, 300, 0)[2:4] |
||
| 113 | lat2, lon2 = fa.apxg2all(30, 15, 200, 300, 0)[2:4] |
||
| 114 | lat3, lon3 = fa.apxg2all(60, 15, 300, 300, 0)[2:4] |
||
| 115 | lat4, lon4 = fa.apxg2all(90, 15, 400, 300, 0)[2:4] |
||
| 116 | assert_allclose(lats.astype(float), np.array([[lat1, lat2], [lat3, lat4]], |
||
| 117 | dtype=float)) |
||
| 118 | assert_allclose(lons.astype(float), np.array([[lon1, lon2], [lon3, lon4]], |
||
| 119 | dtype=float)) |
||
| 120 | |||
| 121 | |||
| 122 | def test__geo2apex_longitude(): |
||
| 123 | apex_out = Apex(date=2000, refh=300) |
||
| 124 | assert_allclose(apex_out._geo2apex(60, 180, 100), |
||
| 125 | fa.apxg2all(60, 180, 100, 300, 0)[2:4]) |
||
| 126 | assert_allclose(apex_out._geo2apex(60, -180, 100), |
||
| 127 | fa.apxg2all(60, -180, 100, 300, 0)[2:4]) |
||
| 128 | assert_allclose(apex_out._geo2apex(60, -180, 100), |
||
| 129 | apex_out._geo2apex(60, 180, 100)) |
||
| 130 | for i in range(-5, 5): |
||
| 131 | for lat in [0, 30, 60, 90]: |
||
| 132 | assert_allclose(apex_out._geo2apex(lat, 15 + i * 360, 100), |
||
| 133 | fa.apxg2all(lat, 15, 100, 300, 0)[2:4]) |
||
| 134 | |||
| 135 | |||
| 136 | def test__geo2apexall_scalar(): |
||
| 137 | apex_out = Apex(date=2000, refh=300) |
||
| 138 | for lat in [0, 30, 60, 89]: |
||
| 139 | for lon in [-179, -90, 0, 90, 180]: |
||
| 140 | ret1 = apex_out._geo2apexall(lat, lon, 100) |
||
| 141 | ret2 = fa.apxg2all(lat, lon, 100, 300, 1) |
||
| 142 | for r1, r2 in zip(ret1, ret2): |
||
| 143 | assert_allclose(r1, r2) |
||
| 144 | |||
| 145 | |||
| 146 | def test__geo2apexall_array(): |
||
| 147 | apex_out = Apex(date=2000, refh=300) |
||
| 148 | ret = apex_out._geo2apexall([[0, 30], [60, 90]], 15, |
||
| 149 | [[100, 200], [300, 400]]) |
||
| 150 | ret1 = fa.apxg2all(0, 15, 100, 300, 1) |
||
| 151 | ret2 = fa.apxg2all(30, 15, 200, 300, 1) |
||
| 152 | ret3 = fa.apxg2all(60, 15, 300, 300, 1) |
||
| 153 | ret4 = fa.apxg2all(90, 15, 400, 300, 1) |
||
| 154 | for i in range(len(ret)): |
||
| 155 | try: |
||
| 156 | # ret[i] is array of floats |
||
| 157 | assert_allclose(ret[i].astype(float), |
||
| 158 | np.array([[ret1[i], ret2[i]], [ret3[i], ret4[i]]], |
||
| 159 | dtype=float)) |
||
| 160 | except ValueError: |
||
| 161 | # ret[i] is array of arrays |
||
| 162 | assert_allclose(ret[i][0, 0], ret1[i]) |
||
| 163 | assert_allclose(ret[i][0, 1], ret2[i]) |
||
| 164 | assert_allclose(ret[i][1, 0], ret3[i]) |
||
| 165 | assert_allclose(ret[i][1, 1], ret4[i]) |
||
| 166 | |||
| 167 | |||
| 168 | def test__qd2geo_scalar(): |
||
| 169 | apex_out = Apex(date=2000, refh=300) |
||
| 170 | for lat in [0, 30, 60, 89]: |
||
| 171 | for lon in [-179, -90, 0, 90, 180]: |
||
| 172 | for prec in [-1, 1e-2, 1e-10]: |
||
| 173 | assert_allclose(apex_out._qd2geo(lat, lon, 100, prec), |
||
| 174 | fa.apxq2g(lat, lon, 100, prec)) |
||
| 175 | |||
| 176 | |||
| 177 | def test__qd2geo_array(): |
||
| 178 | apex_out = Apex(date=2000, refh=300) |
||
| 179 | lats, lons, errs = apex_out._qd2geo([[0, 30], [60, 90]], 15, |
||
| 180 | [[100, 200], [300, 400]], 1e-2) |
||
| 181 | lat1, lon1, err1 = fa.apxq2g(0, 15, 100, 1e-2) |
||
| 182 | lat2, lon2, err2 = fa.apxq2g(30, 15, 200, 1e-2) |
||
| 183 | lat3, lon3, err3 = fa.apxq2g(60, 15, 300, 1e-2) |
||
| 184 | lat4, lon4, err4 = fa.apxq2g(90, 15, 400, 1e-2) |
||
| 185 | assert_allclose(lats.astype(float), np.array([[lat1, lat2], [lat3, lat4]], |
||
| 186 | dtype=float)) |
||
| 187 | assert_allclose(lons.astype(float), np.array([[lon1, lon2], [lon3, lon4]], |
||
| 188 | dtype=float)) |
||
| 189 | assert_allclose(errs.astype(float), np.array([[err1, err2], [err3, err4]], |
||
| 190 | dtype=float)) |
||
| 191 | |||
| 192 | |||
| 193 | def test__qd2geo_longitude(): |
||
| 194 | apex_out = Apex(date=2000, refh=300) |
||
| 195 | assert_allclose(apex_out._qd2geo(60, 180, 100, 1e-2), |
||
| 196 | fa.apxq2g(60, 180, 100, 1e-2)) |
||
| 197 | assert_allclose(apex_out._qd2geo(60, -180, 100, 1e-2), |
||
| 198 | fa.apxq2g(60, -180, 100, 1e-2)) |
||
| 199 | assert_allclose(apex_out._qd2geo(60, -180, 100, 1e-2), |
||
| 200 | apex_out._qd2geo(60, 180, 100, 1e-2)) |
||
| 201 | for i in range(-5, 5): |
||
| 202 | for lat in [0, 30, 60, 90]: |
||
| 203 | assert_allclose(apex_out._qd2geo(lat, 15 + i * 360, 100, 1e-2), |
||
| 204 | fa.apxq2g(lat, 15, 100, 1e-2)) |
||
| 205 | |||
| 206 | |||
| 207 | def test__basevec_scalar(): |
||
| 208 | apex_out = Apex(date=2000, refh=300) |
||
| 209 | for lat in [0, 30, 60, 89]: |
||
| 210 | for lon in [-179, -90, 0, 90, 180]: |
||
| 211 | assert_allclose(apex_out._basevec(lat, lon, 100), |
||
| 212 | fa.apxg2q(lat, lon, 100, 1)[2:4]) |
||
| 213 | |||
| 214 | |||
| 215 | def test__basevec_array(): |
||
| 216 | apex_out = Apex(date=2000, refh=300) |
||
| 217 | f1s, f2s = apex_out._basevec([[0, 30], [60, 90]], 15, |
||
| 218 | [[100, 200], [300, 400]]) |
||
| 219 | f11, f21 = fa.apxg2q(0, 15, 100, 1)[2:4] |
||
| 220 | f12, f22 = fa.apxg2q(30, 15, 200, 1)[2:4] |
||
| 221 | f13, f23 = fa.apxg2q(60, 15, 300, 1)[2:4] |
||
| 222 | f14, f24 = fa.apxg2q(90, 15, 400, 1)[2:4] |
||
| 223 | assert_allclose(f1s[0, 0], f11) |
||
| 224 | assert_allclose(f1s[0, 1], f12) |
||
| 225 | assert_allclose(f1s[1, 0], f13) |
||
| 226 | assert_allclose(f1s[1, 1], f14) |
||
| 227 | assert_allclose(f2s[0, 0], f21) |
||
| 228 | assert_allclose(f2s[0, 1], f22) |
||
| 229 | assert_allclose(f2s[1, 0], f23) |
||
| 230 | assert_allclose(f2s[1, 1], f24) |
||
| 231 | |||
| 232 | |||
| 233 | def test__basevec_longitude(): |
||
| 234 | apex_out = Apex(date=2000, refh=300) |
||
| 235 | assert_allclose(apex_out._basevec(60, 180, 100), |
||
| 236 | fa.apxg2q(60, 180, 100, 1)[2:4]) |
||
| 237 | assert_allclose(apex_out._basevec(60, -180, 100), |
||
| 238 | fa.apxg2q(60, -180, 100, 1)[2:4]) |
||
| 239 | assert_allclose(apex_out._basevec(60, -180, 100), |
||
| 240 | apex_out._basevec(60, 180, 100)) |
||
| 241 | for i in range(-5, 5): |
||
| 242 | for lat in [0, 30, 60, 90]: |
||
| 243 | assert_allclose(apex_out._basevec(lat, 15 + i * 360, 100), |
||
| 244 | fa.apxg2q(lat, 15, 100, 1)[2:4]) |
||
| 245 | |||
| 246 | |||
| 247 | # ============================================================================ |
||
| 248 | # Test the convert() method |
||
| 249 | # ============================================================================ |
||
| 250 | |||
| 251 | |||
| 252 | def test_convert_geo2apex(): |
||
| 253 | apex_out = Apex(date=2000, refh=300) |
||
| 254 | assert_allclose(apex_out.convert(60, 15, 'geo', 'apex', height=100), |
||
| 255 | apex_out.geo2apex(60, 15, 100)) |
||
| 256 | |||
| 257 | |||
| 258 | def test_convert_geo2qd(): |
||
| 259 | apex_out = Apex(date=2000, refh=300) |
||
| 260 | assert_allclose(apex_out.convert(60, 15, 'geo', 'qd', height=100), |
||
| 261 | apex_out.geo2qd(60, 15, 100)) |
||
| 262 | |||
| 263 | |||
| 264 | def test_convert_geo2mlt_nodate(): |
||
| 265 | apex_out = Apex(date=2000, refh=300) |
||
| 266 | with pytest.raises(ValueError): |
||
| 267 | apex_out.convert(60, 15, 'geo', 'mlt') |
||
| 268 | |||
| 269 | |||
| 270 | def test_convert_geo2mlt(): |
||
| 271 | datetime = dt.datetime(2000, 3, 9, 14, 25, 58) |
||
| 272 | apex_out = Apex(date=2000, refh=300) |
||
| 273 | assert_allclose(apex_out.convert(60, 15, 'geo', 'mlt', height=100, |
||
| 274 | ssheight=2e5, datetime=datetime)[1], |
||
| 275 | apex_out.mlon2mlt(apex_out.geo2apex(60, 15, 100)[1], |
||
| 276 | datetime, ssheight=2e5)) |
||
| 277 | |||
| 278 | |||
| 279 | def test_convert_apex2geo(): |
||
| 280 | apex_out = Apex(date=2000, refh=300) |
||
| 281 | assert_allclose(apex_out.convert(60, 15, 'apex', 'geo', height=100, |
||
| 282 | precision=1e-2), |
||
| 283 | apex_out.apex2geo(60, 15, 100, precision=1e-2)[:-1]) |
||
| 284 | |||
| 285 | |||
| 286 | def test_convert_apex2qd(): |
||
| 287 | apex_out = Apex(date=2000, refh=300) |
||
| 288 | assert_allclose(apex_out.convert(60, 15, 'apex', 'qd', height=100), |
||
| 289 | apex_out.apex2qd(60, 15, height=100)) |
||
| 290 | |||
| 291 | |||
| 292 | def test_convert_apex2mlt(): |
||
| 293 | datetime = dt.datetime(2000, 3, 9, 14, 25, 58) |
||
| 294 | apex_out = Apex(date=2000, refh=300) |
||
| 295 | assert_allclose(apex_out.convert(60, 15, 'apex', 'mlt', height=100, |
||
| 296 | datetime=datetime, ssheight=2e5)[1], |
||
| 297 | apex_out.mlon2mlt(15, datetime, ssheight=2e5)) |
||
| 298 | |||
| 299 | |||
| 300 | def test_convert_qd2geo(): |
||
| 301 | apex_out = Apex(date=2000, refh=300) |
||
| 302 | assert_allclose(apex_out.convert(60, 15, 'qd', 'geo', height=100, |
||
| 303 | precision=1e-2), |
||
| 304 | apex_out.qd2geo(60, 15, 100, precision=1e-2)[:-1]) |
||
| 305 | |||
| 306 | |||
| 307 | def test_convert_qd2apex(): |
||
| 308 | apex_out = Apex(date=2000, refh=300) |
||
| 309 | assert_allclose(apex_out.convert(60, 15, 'qd', 'apex', height=100), |
||
| 310 | apex_out.qd2apex(60, 15, height=100)) |
||
| 311 | |||
| 312 | |||
| 313 | def test_convert_qd2apex_at_equator(): |
||
| 314 | """Test the quasi-dipole to apex conversion at the magnetic equator.""" |
||
| 315 | apex_out = Apex(date=2000, refh=80) |
||
| 316 | elat, elon = apex_out.convert(lat=0.0, lon=0, source='qd', dest='apex', |
||
| 317 | height=120.0) |
||
| 318 | clat, clon = apex_out.convert(lat=0.001, lon=0, source='qd', dest='apex', |
||
| 319 | height=120.0) |
||
| 320 | assert_allclose([elat, elon], [clat, clon], atol=1e-4) |
||
| 321 | |||
| 322 | |||
| 323 | def test_convert_qd2mlt(): |
||
| 324 | datetime = dt.datetime(2000, 3, 9, 14, 25, 58) |
||
| 325 | apex_out = Apex(date=2000, refh=300) |
||
| 326 | assert_allclose(apex_out.convert(60, 15, 'qd', 'mlt', height=100, |
||
| 327 | datetime=datetime, ssheight=2e5)[1], |
||
| 328 | apex_out.mlon2mlt(15, datetime, ssheight=2e5)) |
||
| 329 | |||
| 330 | |||
| 331 | def test_convert_mlt2geo(): |
||
| 332 | datetime = dt.datetime(2000, 3, 9, 14, 25, 58) |
||
| 333 | apex_out = Apex(date=2000, refh=300) |
||
| 334 | assert_allclose(apex_out.convert(60, 15, 'mlt', 'geo', height=100, |
||
| 335 | datetime=datetime, precision=1e-2, |
||
| 336 | ssheight=2e5), |
||
| 337 | apex_out.apex2geo(60, apex_out.mlt2mlon(15, datetime, |
||
| 338 | ssheight=2e5), 100, |
||
| 339 | precision=1e-2)[:-1]) |
||
| 340 | |||
| 341 | |||
| 342 | def test_convert_mlt2apex(): |
||
| 343 | datetime = dt.datetime(2000, 3, 9, 14, 25, 58) |
||
| 344 | apex_out = Apex(date=2000, refh=300) |
||
| 345 | assert_allclose(apex_out.convert(60, 15, 'mlt', 'apex', height=100, |
||
| 346 | datetime=datetime, ssheight=2e5), |
||
| 347 | (60, apex_out.mlt2mlon(15, datetime, ssheight=2e5))) |
||
| 348 | |||
| 349 | |||
| 350 | def test_convert_mlt2qd(): |
||
| 351 | datetime = dt.datetime(2000, 3, 9, 14, 25, 58) |
||
| 352 | apex_out = Apex(date=2000, refh=300) |
||
| 353 | assert_allclose(apex_out.convert(60, 15, 'mlt', 'qd', height=100, |
||
| 354 | datetime=datetime, ssheight=2e5), |
||
| 355 | apex_out.apex2qd(60, apex_out.mlt2mlon(15, datetime, |
||
| 356 | ssheight=2e5), |
||
| 357 | height=100)) |
||
| 358 | |||
| 359 | |||
| 360 | def test_convert_invalid_lat(): |
||
| 361 | apex_out = Apex(date=2000, refh=300) |
||
| 362 | with pytest.raises(ValueError): |
||
| 363 | apex_out.convert(91, 0, 'geo', 'geo') |
||
| 364 | with pytest.raises(ValueError): |
||
| 365 | apex_out.convert(-91, 0, 'geo', 'geo') |
||
| 366 | apex_out.convert(90, 0, 'geo', 'geo') |
||
| 367 | apex_out.convert(-90, 0, 'geo', 'geo') |
||
| 368 | |||
| 369 | assert_allclose(apex_out.convert(90 + 1e-5, 0, 'geo', 'apex'), |
||
| 370 | apex_out.convert(90, 0, 'geo', 'apex'), rtol=0, atol=1e-8) |
||
| 371 | |||
| 372 | |||
| 373 | def test_convert_invalid_transformation(): |
||
| 374 | apex_out = Apex(date=2000, refh=300) |
||
| 375 | with pytest.raises(NotImplementedError): |
||
| 376 | apex_out.convert(0, 0, 'foobar', 'geo') |
||
| 377 | with pytest.raises(NotImplementedError): |
||
| 378 | apex_out.convert(0, 0, 'geo', 'foobar') |
||
| 379 | |||
| 380 | |||
| 381 | coord_names = ['geo', 'apex', 'qd'] |
||
| 382 | |||
| 383 | |||
| 384 | @pytest.mark.parametrize('transform', itertools.product(coord_names, |
||
| 385 | coord_names)) |
||
| 386 | def test_convert_withnan(transform): |
||
| 387 | """Test Apex.convert success with NaN input.""" |
||
| 388 | num_nans = 5 |
||
| 389 | in_lat = np.arange(0, 10, dtype=float) |
||
| 390 | in_lat[:num_nans] = np.nan |
||
| 391 | in_lon = np.arange(0, 10, dtype=float) |
||
| 392 | in_lon[:num_nans] = np.nan |
||
| 393 | src, dest = transform |
||
| 394 | apex_out = Apex(date=2000, refh=80) |
||
| 395 | out_lat, out_lon = apex_out.convert(in_lat, in_lon, src, dest, height=120) |
||
| 396 | assert np.all(np.isnan(out_lat[:num_nans])) |
||
| 397 | assert np.all(np.isnan(out_lon[:num_nans])) |
||
| 398 | assert np.all(np.isfinite(out_lat[num_nans:])) |
||
| 399 | assert np.all(np.isfinite(out_lat[num_nans:])) |
||
| 400 | |||
| 401 | |||
| 402 | # ============================================================================ |
||
| 403 | # Test the geo2apex() method |
||
| 404 | # ============================================================================ |
||
| 405 | |||
| 406 | |||
| 407 | def test_geo2apex(): |
||
| 408 | apex_out = Apex(date=2000, refh=300) |
||
| 409 | lat, lon = apex_out.geo2apex(60, 15, 100) |
||
| 410 | assert_allclose((lat, lon), apex_out._geo2apex(60, 15, 100)) |
||
| 411 | assert type(lat) != np.ndarray |
||
| 412 | assert type(lon) != np.ndarray |
||
| 413 | |||
| 414 | |||
| 415 | def test_geo2apex_vectorization(): |
||
| 416 | apex_out = Apex(date=2000, refh=300) |
||
| 417 | assert apex_out.geo2apex([60, 60], 15, 100)[0].shape == (2,) |
||
| 418 | assert apex_out.geo2apex(60, [15, 15], 100)[0].shape == (2,) |
||
| 419 | assert apex_out.geo2apex(60, 15, [100, 100])[0].shape == (2,) |
||
| 420 | |||
| 421 | |||
| 422 | def test_geo2apex_invalid_lat(): |
||
| 423 | apex_out = Apex(date=2000, refh=300) |
||
| 424 | with pytest.raises(ValueError): |
||
| 425 | apex_out.geo2apex(91, 0, 0) |
||
| 426 | with pytest.raises(ValueError): |
||
| 427 | apex_out.geo2apex(-91, 0, 0) |
||
| 428 | apex_out.geo2apex(90, 0, 0) |
||
| 429 | apex_out.geo2apex(-90, 0, 0) |
||
| 430 | |||
| 431 | assert_allclose(apex_out.geo2apex(90 + 1e-5, 0, 0), |
||
| 432 | apex_out.geo2apex(90, 0, 0), rtol=0, atol=1e-8) |
||
| 433 | |||
| 434 | |||
| 435 | def test_geo2apex_undefined_warning(recwarn): |
||
| 436 | """Test warning and fill values for an undefined location.""" |
||
| 437 | apex_out = Apex(date=2000, refh=10000) |
||
| 438 | ret = apex_out.geo2apex(0, 0, 0) |
||
| 439 | |||
| 440 | assert np.isnan(ret[0]) |
||
| 441 | assert len(recwarn) == 1 |
||
| 442 | assert issubclass(recwarn[-1].category, UserWarning) |
||
| 443 | assert 'set to NaN where' in str(recwarn[-1].message) |
||
| 444 | |||
| 445 | |||
| 446 | # ============================================================================ |
||
| 447 | # Test the apex2geo() method |
||
| 448 | # ============================================================================ |
||
| 449 | |||
| 450 | |||
| 451 | def test_apex2geo(): |
||
| 452 | apex_out = Apex(date=2000, refh=300) |
||
| 453 | lat, lon, error = apex_out.apex2geo(60, 15, 100, precision=1e-2) |
||
| 454 | assert_allclose((lat, lon, error), |
||
| 455 | apex_out.qd2geo(*apex_out.apex2qd(60, 15, 100), height=100, |
||
| 456 | precision=1e-2)) |
||
| 457 | |||
| 458 | assert type(lat) != np.ndarray |
||
| 459 | assert type(lon) != np.ndarray |
||
| 460 | assert type(error) != np.ndarray |
||
| 461 | |||
| 462 | |||
| 463 | def test_apex2geo_vectorization(): |
||
| 464 | apex_out = Apex(date=2000, refh=300) |
||
| 465 | assert apex_out.apex2geo([60, 60], 15, 100)[0].shape == (2,) |
||
| 466 | assert apex_out.apex2geo(60, [15, 15], 100)[0].shape == (2,) |
||
| 467 | assert apex_out.apex2geo(60, 15, [100, 100])[0].shape == (2,) |
||
| 468 | |||
| 469 | |||
| 470 | def test_apex2geo_invalid_lat(): |
||
| 471 | apex_out = Apex(date=2000, refh=300) |
||
| 472 | with pytest.raises(ValueError): |
||
| 473 | apex_out.apex2geo(91, 0, 0, 1e-2) |
||
| 474 | with pytest.raises(ValueError): |
||
| 475 | apex_out.apex2geo(-91, 0, 0, 1e-2) |
||
| 476 | apex_out.apex2geo(90, 0, 0, 1e-2) |
||
| 477 | apex_out.apex2geo(-90, 0, 0, 1e-2) |
||
| 478 | |||
| 479 | assert_allclose(apex_out.apex2geo(90 + 1e-5, 0, 0, 1e-2), |
||
| 480 | apex_out.apex2geo(90, 0, 0, 1e-2), rtol=0, atol=1e-8) |
||
| 481 | |||
| 482 | |||
| 483 | # ============================================================================ |
||
| 484 | # Test the geo2qd() method |
||
| 485 | # ============================================================================ |
||
| 486 | |||
| 487 | |||
| 488 | def test_geo2qd(): |
||
| 489 | apex_out = Apex(date=2000, refh=300) |
||
| 490 | lat, lon = apex_out.geo2qd(60, 15, 100) |
||
| 491 | assert_allclose((lat, lon), apex_out._geo2qd(60, 15, 100)) |
||
| 492 | assert type(lat) != np.ndarray |
||
| 493 | assert type(lon) != np.ndarray |
||
| 494 | |||
| 495 | |||
| 496 | def test_geo2qd_vectorization(): |
||
| 497 | apex_out = Apex(date=2000, refh=300) |
||
| 498 | assert apex_out.geo2qd([60, 60], 15, 100)[0].shape == (2,) |
||
| 499 | assert apex_out.geo2qd(60, [15, 15], 100)[0].shape == (2,) |
||
| 500 | assert apex_out.geo2qd(60, 15, [100, 100])[0].shape == (2,) |
||
| 501 | |||
| 502 | |||
| 503 | def test_geo2qd_invalid_lat(): |
||
| 504 | apex_out = Apex(date=2000, refh=300) |
||
| 505 | with pytest.raises(ValueError): |
||
| 506 | apex_out.geo2qd(91, 0, 0) |
||
| 507 | with pytest.raises(ValueError): |
||
| 508 | apex_out.geo2qd(-91, 0, 0) |
||
| 509 | apex_out.geo2qd(90, 0, 0) |
||
| 510 | apex_out.geo2qd(-90, 0, 0) |
||
| 511 | |||
| 512 | assert_allclose(apex_out.geo2qd(90 + 1e-5, 0, 0), apex_out.geo2qd(90, 0, 0), |
||
| 513 | rtol=0, atol=1e-8) |
||
| 514 | |||
| 515 | |||
| 516 | # ============================================================================ |
||
| 517 | # Test the qd2geo() method |
||
| 518 | # ============================================================================ |
||
| 519 | |||
| 520 | |||
| 521 | def test_qd2geo(): |
||
| 522 | apex_out = Apex(date=2000, refh=300) |
||
| 523 | lat, lon, error = apex_out.qd2geo(60, 15, 100, precision=1e-2) |
||
| 524 | assert_allclose((lat, lon, error), apex_out._qd2geo(60, 15, 100, 1e-2)) |
||
| 525 | assert type(lat) != np.ndarray |
||
| 526 | assert type(lon) != np.ndarray |
||
| 527 | assert type(error) != np.ndarray |
||
| 528 | |||
| 529 | |||
| 530 | def test_qd2geo_vectorization(): |
||
| 531 | apex_out = Apex(date=2000, refh=300) |
||
| 532 | assert apex_out.qd2geo([60, 60], 15, 100)[0].shape == (2,) |
||
| 533 | assert apex_out.qd2geo(60, [15, 15], 100)[0].shape == (2,) |
||
| 534 | assert apex_out.qd2geo(60, 15, [100, 100])[0].shape == (2,) |
||
| 535 | |||
| 536 | |||
| 537 | def test_qd2geo_invalid_lat(): |
||
| 538 | apex_out = Apex(date=2000, refh=300) |
||
| 539 | with pytest.raises(ValueError): |
||
| 540 | apex_out.qd2geo(91, 0, 0, precision=1e-2) |
||
| 541 | with pytest.raises(ValueError): |
||
| 542 | apex_out.qd2geo(-91, 0, 0, precision=1e-2) |
||
| 543 | apex_out.qd2geo(90, 0, 0, precision=1e-2) |
||
| 544 | apex_out.qd2geo(-90, 0, 0, precision=1e-2) |
||
| 545 | |||
| 546 | assert_allclose(apex_out.qd2geo(90 + 1e-5, 0, 0, 1e-2), |
||
| 547 | apex_out.qd2geo(90, 0, 0, 1e-2), rtol=0, atol=1e-8) |
||
| 548 | |||
| 549 | |||
| 550 | # ============================================================================ |
||
| 551 | # Test the apex2qd() method |
||
| 552 | # ============================================================================ |
||
| 553 | |||
| 554 | |||
| 555 | def test_apex2qd(): |
||
| 556 | apex_out = Apex(date=2000, refh=300) |
||
| 557 | lat, lon = apex_out.apex2qd(60, 15, 100) |
||
| 558 | assert_allclose((lat, lon), |
||
| 559 | [60.498401, 15]) |
||
| 560 | assert type(lat) != np.ndarray |
||
| 561 | assert type(lon) != np.ndarray |
||
| 562 | |||
| 563 | |||
| 564 | def test_apex2qd_vectorization(): |
||
| 565 | apex_out = Apex(date=2000, refh=300) |
||
| 566 | assert apex_out.apex2qd([60, 60], 15, 100)[0].shape == (2,) |
||
| 567 | assert apex_out.apex2qd(60, [15, 15], 100)[0].shape == (2,) |
||
| 568 | assert apex_out.apex2qd(60, 15, [100, 100])[0].shape == (2,) |
||
| 569 | |||
| 570 | |||
| 571 | def test_apex2qd_invalid_lat(): |
||
| 572 | apex_out = Apex(date=2000, refh=300) |
||
| 573 | with pytest.raises(ValueError): |
||
| 574 | apex_out.apex2qd(91, 0, 0) |
||
| 575 | with pytest.raises(ValueError): |
||
| 576 | apex_out.apex2qd(-91, 0, 0) |
||
| 577 | apex_out.apex2qd(90, 0, 0) |
||
| 578 | apex_out.apex2qd(-90, 0, 0) |
||
| 579 | |||
| 580 | assert_allclose(apex_out.apex2qd(90 + 1e-5, 0, 0), |
||
| 581 | apex_out.apex2qd(90, 0, 0), rtol=0, atol=1e-8) |
||
| 582 | |||
| 583 | |||
| 584 | def test_apex2qd_apexheight_close(): |
||
| 585 | apex_out = Apex(date=2000, refh=300) |
||
| 586 | apex_out.apex2qd(0, 15, 300 + 1e-6) |
||
| 587 | |||
| 588 | |||
| 589 | def test_apex2qd_apexheight_over(): |
||
| 590 | apex_out = Apex(date=2000, refh=300) |
||
| 591 | with pytest.raises(ApexHeightError): |
||
| 592 | apex_out.apex2qd(0, 15, 301) |
||
| 593 | |||
| 594 | |||
| 595 | # ============================================================================ |
||
| 596 | # Test the qd2apex() method |
||
| 597 | # ============================================================================ |
||
| 598 | |||
| 599 | |||
| 600 | def test_qd2apex(): |
||
| 601 | apex_out = Apex(date=2000, refh=300) |
||
| 602 | lat, lon = apex_out.qd2apex(60, 15, 100) |
||
| 603 | assert_allclose((lat, lon), |
||
| 604 | [59.491381, 15]) |
||
| 605 | assert type(lat) != np.ndarray |
||
| 606 | assert type(lon) != np.ndarray |
||
| 607 | |||
| 608 | |||
| 609 | def test_qd2apex_vectorization(): |
||
| 610 | apex_out = Apex(date=2000, refh=300) |
||
| 611 | assert apex_out.qd2apex([60, 60], 15, 100)[0].shape == (2,) |
||
| 612 | assert apex_out.qd2apex(60, [15, 15], 100)[0].shape == (2,) |
||
| 613 | assert apex_out.qd2apex(60, 15, [100, 100])[0].shape == (2,) |
||
| 614 | |||
| 615 | |||
| 616 | def test_qd2apex_invalid_lat(): |
||
| 617 | apex_out = Apex(date=2000, refh=300) |
||
| 618 | with pytest.raises(ValueError): |
||
| 619 | apex_out.qd2apex(91, 0, 0) |
||
| 620 | with pytest.raises(ValueError): |
||
| 621 | apex_out.qd2apex(-91, 0, 0) |
||
| 622 | apex_out.qd2apex(90, 0, 0) |
||
| 623 | apex_out.qd2apex(-90, 0, 0) |
||
| 624 | |||
| 625 | assert_allclose(apex_out.qd2apex(90 + 1e-5, 0, 0), |
||
| 626 | apex_out.qd2apex(90, 0, 0), rtol=0, atol=1e-8) |
||
| 627 | |||
| 628 | |||
| 629 | def test_qd2apex_apexheight_close(): |
||
| 630 | apex_out = Apex(date=2000, refh=300) |
||
| 631 | assert_allclose(apex_out.qd2apex(0, 15, 300 - 1e-5), |
||
| 632 | apex_out.qd2apex(0, 15, 300)) |
||
| 633 | |||
| 634 | |||
| 635 | def test_qd2apex_apexheight_over(): |
||
| 636 | apex_out = Apex(date=2000, refh=300) |
||
| 637 | with pytest.raises(ApexHeightError): |
||
| 638 | apex_out.qd2apex(0, 15, 299) |
||
| 639 | |||
| 640 | |||
| 641 | # ============================================================================ |
||
| 642 | # Test mlon2mlt() |
||
| 643 | # ============================================================================ |
||
| 644 | |||
| 645 | |||
| 646 | def test_mlon2mlt_scalar(): |
||
| 647 | apex_out = Apex(date=2000, refh=300) |
||
| 648 | mlon = apex_out.mlon2mlt(0, dt.datetime(2000, 2, 3, 4, 5, 6)) |
||
| 649 | assert_allclose(mlon, 23.019629923502603) |
||
| 650 | assert type(mlon) != np.ndarray |
||
| 651 | |||
| 652 | |||
| 653 | def test_mlon2mlt_ssheight(): |
||
| 654 | apex_out = Apex(date=2000, refh=300) |
||
| 655 | mlt = apex_out.mlon2mlt(0, dt.datetime(2000, 2, 3, 4, 5, 6), |
||
| 656 | ssheight=50 * 2000) |
||
| 657 | assert_allclose(mlt, 23.026712036132814) |
||
| 658 | |||
| 659 | |||
| 660 | def test_mlon2mlt_1Darray(): |
||
| 661 | apex_out = Apex(date=2000, refh=300) |
||
| 662 | assert_allclose(apex_out.mlon2mlt([0, 180], |
||
| 663 | dt.datetime(2000, 2, 3, 4, 5, 6)), |
||
| 664 | [23.019261, 11.019261], rtol=1e-4) |
||
| 665 | |||
| 666 | |||
| 667 | def test_mlon2mlt_2Darray(): |
||
| 668 | apex_out = Apex(date=2000, refh=300) |
||
| 669 | assert_allclose(apex_out.mlon2mlt([[0, 180], [0, 180]], |
||
| 670 | dt.datetime(2000, 2, 3, 4, 5, 6)), |
||
| 671 | [[23.019261, 11.019261], [23.019261, 11.019261]], rtol=1e-4) |
||
| 672 | |||
| 673 | |||
| 674 | def test_mlon2mlt_diffdates(): |
||
| 675 | apex_out = Apex(date=2000, refh=300) |
||
| 676 | dtime1 = dt.datetime(2000, 2, 3, 4, 5, 6) |
||
| 677 | dtime2 = dt.datetime(2000, 2, 3, 5, 5, 6) |
||
| 678 | assert apex_out.mlon2mlt(0, dtime1) != apex_out.mlon2mlt(0, dtime2) |
||
| 679 | |||
| 680 | |||
| 681 | def test_mlon2mlt_offset(): |
||
| 682 | apex_out = Apex(date=2000, refh=300) |
||
| 683 | date = dt.datetime(2000, 2, 3, 4, 5, 6) |
||
| 684 | assert_allclose(apex_out.mlon2mlt(0, date), |
||
| 685 | apex_out.mlon2mlt(-15, date) + 1) |
||
| 686 | assert_allclose(apex_out.mlon2mlt(0, date), |
||
| 687 | apex_out.mlon2mlt(-10 * 15, date) + 10) |
||
| 688 | |||
| 689 | |||
| 690 | def test_mlon2mlt_range(): |
||
| 691 | apex_out = Apex(date=2000, refh=300) |
||
| 692 | assert_allclose(apex_out.mlon2mlt(range(0, 361, 30), |
||
| 693 | dt.datetime(2000, 2, 3, 4, 5, 6)), |
||
| 694 | [23.01963, 1.01963, 3.01963, 5.01963, 7.01963, |
||
| 695 | 9.01963, 11.01963, 13.01963, 15.01963, 17.01963, |
||
| 696 | 19.01963, 21.01963, 23.01963], |
||
| 697 | rtol=1e-4) |
||
| 698 | |||
| 699 | |||
| 700 | # ============================================================================ |
||
| 701 | # Test mlt2mlon() |
||
| 702 | # ============================================================================ |
||
| 703 | |||
| 704 | |||
| 705 | def test_mlt2mlon_scalar(): |
||
| 706 | apex_out = Apex(date=2000, refh=300) |
||
| 707 | mlt = apex_out.mlt2mlon(0, dt.datetime(2000, 2, 3, 4, 5, 6)) |
||
| 708 | assert_allclose(mlt, 14.705535888671875) |
||
| 709 | assert type(mlt) != np.ndarray |
||
| 710 | |||
| 711 | |||
| 712 | def test_mlt2mlon_ssheight(): |
||
| 713 | apex_out = Apex(date=2000, refh=300) |
||
| 714 | mlt = apex_out.mlt2mlon(0, dt.datetime(2000, 2, 3, 4, 5, 6), |
||
| 715 | ssheight=50 * 2000) |
||
| 716 | assert_allclose(mlt, 14.599319458007812) |
||
| 717 | |||
| 718 | |||
| 719 | def test_mlt2mlon_1Darray(): |
||
| 720 | apex_out = Apex(date=2000, refh=300) |
||
| 721 | assert_allclose(apex_out.mlt2mlon([0, 12], |
||
| 722 | dt.datetime(2000, 2, 3, 4, 5, 6)), |
||
| 723 | [14.705551, 194.705551], rtol=1e-4) |
||
| 724 | |||
| 725 | |||
| 726 | def test_mlt2mlon_2Darray(): |
||
| 727 | apex_out = Apex(date=2000, refh=300) |
||
| 728 | assert_allclose(apex_out.mlt2mlon([[0, 12], [0, 12]], |
||
| 729 | dt.datetime(2000, 2, 3, 4, 5, 6)), |
||
| 730 | [[14.705551, 194.705551], [14.705551, 194.705551]], |
||
| 731 | rtol=1e-4) |
||
| 732 | |||
| 733 | |||
| 734 | def test_mlt2mlon_diffdates(): |
||
| 735 | apex_out = Apex(date=2000, refh=300) |
||
| 736 | dtime1 = dt.datetime(2000, 2, 3, 4, 5, 6) |
||
| 737 | dtime2 = dt.datetime(2000, 2, 3, 5, 5, 6) |
||
| 738 | assert apex_out.mlt2mlon(0, dtime1) != apex_out.mlt2mlon(0, dtime2) |
||
| 739 | |||
| 740 | |||
| 741 | def test_mlt2mlon_offset(): |
||
| 742 | apex_out = Apex(date=2000, refh=300) |
||
| 743 | date = dt.datetime(2000, 2, 3, 4, 5, 6) |
||
| 744 | assert_allclose(apex_out.mlt2mlon(0, date), apex_out.mlt2mlon(1, date) - 15) |
||
| 745 | assert_allclose(apex_out.mlt2mlon(0, date), |
||
| 746 | apex_out.mlt2mlon(10, date) - 150) |
||
| 747 | |||
| 748 | |||
| 749 | def test_mlt2mlon_range(): |
||
| 750 | apex_out = Apex(date=2000, refh=300) |
||
| 751 | assert_allclose(apex_out.mlt2mlon(range(0, 25, 2), |
||
| 752 | dt.datetime(2000, 2, 3, 4, 5, 6)), |
||
| 753 | [14.705551, 44.705551, 74.705551, 104.705551, 134.705551, |
||
| 754 | 164.705551, 194.705551, 224.705551, 254.705551, 284.705551, |
||
| 755 | 314.705551, 344.705551, 14.705551], |
||
| 756 | rtol=1e-4) |
||
| 757 | |||
| 758 | |||
| 759 | # ============================================================================ |
||
| 760 | # Test mlt/mlon back and forth |
||
| 761 | # ============================================================================ |
||
| 762 | |||
| 763 | |||
| 764 | def test_mlon2mlt2mlon(): |
||
| 765 | apex_out = Apex(date=2000, refh=300) |
||
| 766 | date = dt.datetime(2000, 2, 3, 4, 5, 6) |
||
| 767 | assert_allclose(apex_out.mlon2mlt(apex_out.mlt2mlon(0, date), date), 0) |
||
| 768 | assert_allclose(apex_out.mlon2mlt(apex_out.mlt2mlon(6, date), date), 6) |
||
| 769 | assert_allclose(apex_out.mlon2mlt(apex_out.mlt2mlon(12, date), date), 12) |
||
| 770 | assert_allclose(apex_out.mlon2mlt(apex_out.mlt2mlon(18, date), date), 18) |
||
| 771 | assert_allclose(apex_out.mlon2mlt(apex_out.mlt2mlon(24, date), date), 0) |
||
| 772 | |||
| 773 | |||
| 774 | def test_mlt2mlon2mlt(): |
||
| 775 | apex_out = Apex(date=2000, refh=300) |
||
| 776 | date = dt.datetime(2000, 2, 3, 4, 5, 6) |
||
| 777 | assert_allclose(apex_out.mlt2mlon(apex_out.mlon2mlt(0, date), date), 0) |
||
| 778 | assert_allclose(apex_out.mlt2mlon(apex_out.mlon2mlt(90, date), date), 90) |
||
| 779 | assert_allclose(apex_out.mlt2mlon(apex_out.mlon2mlt(180, date), date), 180) |
||
| 780 | assert_allclose(apex_out.mlt2mlon(apex_out.mlon2mlt(270, date), date), 270) |
||
| 781 | assert_allclose(apex_out.mlt2mlon(apex_out.mlon2mlt(360, date), date), 0) |
||
| 782 | |||
| 783 | |||
| 784 | # ============================================================================ |
||
| 785 | # Test the map_to_height() method |
||
| 786 | # ============================================================================ |
||
| 787 | |||
| 788 | |||
| 789 | def test_map_to_height(): |
||
| 790 | apex_out = Apex(date=2000, refh=300) |
||
| 791 | assert_allclose(apex_out.map_to_height(60, 15, 100, 10000, conjugate=False, |
||
| 792 | precision=1e-10), |
||
| 793 | (31.841466903686523, 17.916635513305664, |
||
| 794 | 1.7075473124350538e-6)) |
||
| 795 | assert_allclose(apex_out.map_to_height(30, 170, 100, 500, conjugate=False, |
||
| 796 | precision=1e-2), |
||
| 797 | (25.727270126342773, 169.60546875, 0.00017573432705830783)) |
||
| 798 | |||
| 799 | |||
| 800 | def test_map_to_height_same_height(): |
||
| 801 | apex_out = Apex(date=2000, refh=300) |
||
| 802 | assert_allclose(apex_out.map_to_height(60, 15, 100, 100, conjugate=False, |
||
| 803 | precision=1e-10), |
||
| 804 | (60.0, 15.000003814697266, 0.0), rtol=1e-5) |
||
| 805 | |||
| 806 | |||
| 807 | def test_map_to_height_conjugate(): |
||
| 808 | """Test results of map_to_height using conjugacy.""" |
||
| 809 | apex_out = Apex(date=2000, refh=300) |
||
| 810 | assert_allclose(apex_out.map_to_height(60, 15, 100, 10000, conjugate=True, |
||
| 811 | precision=1e-10), |
||
| 812 | (-25.424888610839844, 27.310426712036133, |
||
| 813 | 1.2074182222931995e-6), atol=1e-6) |
||
| 814 | assert_allclose(apex_out.map_to_height(30, 170, 100, 500, conjugate=True, |
||
| 815 | precision=1e-2), |
||
| 816 | (-13.76642894744873, 164.24259948730469, |
||
| 817 | 0.00056820799363777041), atol=1e-6) |
||
| 818 | |||
| 819 | |||
| 820 | def test_map_to_height_vectorization(): |
||
| 821 | apex_out = Apex(date=2000, refh=300) |
||
| 822 | assert_allclose(apex_out.map_to_height([60, 60], 15, 100, 100), |
||
| 823 | ([60] * 2, [15.00000381] * 2, [0] * 2), rtol=1e-5) |
||
| 824 | assert_allclose(apex_out.map_to_height(60, [15, 15], 100, 100), |
||
| 825 | ([60] * 2, [15.00000381] * 2, [0] * 2), rtol=1e-5) |
||
| 826 | assert_allclose(apex_out.map_to_height(60, 15, [100, 100], 100), |
||
| 827 | ([60] * 2, [15.00000381] * 2, [0] * 2), rtol=1e-5) |
||
| 828 | assert_allclose(apex_out.map_to_height(60, 15, 100, [100, 100]), |
||
| 829 | ([60] * 2, [15.00000381] * 2, [0] * 2), rtol=1e-5) |
||
| 830 | |||
| 831 | |||
| 832 | def test_map_to_height_ApexHeightError(): |
||
| 833 | apex_out = Apex(date=2000, refh=300) |
||
| 834 | with pytest.raises(ApexHeightError): |
||
| 835 | apex_out.map_to_height(0, 15, 100, 10000) |
||
| 836 | |||
| 837 | |||
| 838 | # ============================================================================ |
||
| 839 | # Test the map_E_to_height() method |
||
| 840 | # ============================================================================ |
||
| 841 | |||
| 842 | |||
| 843 | View Code Duplication | def test_map_E_to_height(): |
|
|
|
|||
| 844 | apex_out = Apex(date=2000, refh=300) |
||
| 845 | out_60_15_100_500 = [0.71152183, 2.35624876, 0.57260784] |
||
| 846 | out_60_15_100_500_234 = [1.56028502, 3.43916636, 0.78235384] |
||
| 847 | out_60_15_100_1000 = [0.67796492, 2.08982134, 0.55860785] |
||
| 848 | out_60_15_200_500 = [0.72377397, 2.42737471, 0.59083726] |
||
| 849 | out_60_30_100_500 = [0.68626344, 2.37530133, 0.60060124] |
||
| 850 | out_70_15_100_500 = [0.72760378, 2.18082305, 0.29141979] |
||
| 851 | |||
| 852 | # scalar |
||
| 853 | assert_allclose(apex_out.map_E_to_height(60, 15, 100, 500, [1, 2, 3]), |
||
| 854 | out_60_15_100_500, rtol=1e-5) |
||
| 855 | assert_allclose(apex_out.map_E_to_height(60, 15, 100, 500, [2, 3, 4]), |
||
| 856 | out_60_15_100_500_234, rtol=1e-5) |
||
| 857 | assert_allclose(apex_out.map_E_to_height(60, 15, 100, 1000, [1, 2, 3]), |
||
| 858 | out_60_15_100_1000, rtol=1e-5) |
||
| 859 | assert_allclose(apex_out.map_E_to_height(60, 15, 200, 500, [1, 2, 3]), |
||
| 860 | out_60_15_200_500, rtol=1e-5) |
||
| 861 | assert_allclose(apex_out.map_E_to_height(60, 30, 100, 500, [1, 2, 3]), |
||
| 862 | out_60_30_100_500, rtol=1e-5) |
||
| 863 | assert_allclose(apex_out.map_E_to_height(70, 15, 100, 500, [1, 2, 3]), |
||
| 864 | out_70_15_100_500, rtol=1e-5) |
||
| 865 | |||
| 866 | # vectorize lat |
||
| 867 | assert_allclose(apex_out.map_E_to_height([60, 70], 15, 100, 500, |
||
| 868 | np.array([[1, 2, 3]] * 2).T), |
||
| 869 | np.array([out_60_15_100_500, out_70_15_100_500]).T, |
||
| 870 | rtol=1e-5) |
||
| 871 | |||
| 872 | # vectorize lon |
||
| 873 | assert_allclose(apex_out.map_E_to_height(60, [15, 30], 100, 500, |
||
| 874 | np.array([[1, 2, 3]] * 2).T), |
||
| 875 | np.array([out_60_15_100_500, out_60_30_100_500]).T, |
||
| 876 | rtol=1e-5) |
||
| 877 | |||
| 878 | # vectorize height |
||
| 879 | assert_allclose(apex_out.map_E_to_height(60, 15, [100, 200], 500, |
||
| 880 | np.array([[1, 2, 3]] * 2).T), |
||
| 881 | np.array([out_60_15_100_500, out_60_15_200_500]).T, |
||
| 882 | rtol=1e-5) |
||
| 883 | |||
| 884 | # vectorize newheight |
||
| 885 | assert_allclose(apex_out.map_E_to_height(60, 15, 100, [500, 1000], |
||
| 886 | np.array([[1, 2, 3]] * 2).T), |
||
| 887 | np.array([out_60_15_100_500, out_60_15_100_1000]).T, |
||
| 888 | rtol=1e-5) |
||
| 889 | |||
| 890 | # vectorize E |
||
| 891 | assert_allclose(apex_out.map_E_to_height(60, 15, 100, 500, |
||
| 892 | np.array([[1, 2, 3], |
||
| 893 | [2, 3, 4]]).T), |
||
| 894 | np.array([out_60_15_100_500, out_60_15_100_500_234]).T, |
||
| 895 | rtol=1e-5) |
||
| 896 | |||
| 897 | |||
| 898 | # ============================================================================ |
||
| 899 | # Test the map_V_to_height() method |
||
| 900 | # ============================================================================ |
||
| 901 | |||
| 902 | |||
| 903 | View Code Duplication | def test_map_V_to_height(): |
|
| 904 | apex_out = Apex(date=2000, refh=300) |
||
| 905 | out_60_15_100_500 = [0.81971957, 2.84512495, 0.69545001] |
||
| 906 | out_60_15_100_500_234 = [1.83027746, 4.14346436, 0.94764179] |
||
| 907 | out_60_15_100_1000 = [0.92457698, 3.14997661, 0.85135187] |
||
| 908 | out_60_15_200_500 = [0.80388262, 2.79321504, 0.68285158] |
||
| 909 | out_60_30_100_500 = [0.76141245, 2.87884673, 0.73655941] |
||
| 910 | out_70_15_100_500 = [0.84681866, 2.5925821, 0.34792655] |
||
| 911 | |||
| 912 | # scalar |
||
| 913 | assert_allclose(apex_out.map_V_to_height(60, 15, 100, 500, [1, 2, 3]), |
||
| 914 | out_60_15_100_500, rtol=1e-5) |
||
| 915 | assert_allclose(apex_out.map_V_to_height(60, 15, 100, 500, [2, 3, 4]), |
||
| 916 | out_60_15_100_500_234, rtol=1e-5) |
||
| 917 | assert_allclose(apex_out.map_V_to_height(60, 15, 100, 1000, [1, 2, 3]), |
||
| 918 | out_60_15_100_1000, rtol=1e-5) |
||
| 919 | assert_allclose(apex_out.map_V_to_height(60, 15, 200, 500, [1, 2, 3]), |
||
| 920 | out_60_15_200_500, rtol=1e-5) |
||
| 921 | assert_allclose(apex_out.map_V_to_height(60, 30, 100, 500, [1, 2, 3]), |
||
| 922 | out_60_30_100_500, rtol=1e-5) |
||
| 923 | assert_allclose(apex_out.map_V_to_height(70, 15, 100, 500, [1, 2, 3]), |
||
| 924 | out_70_15_100_500, rtol=1e-5) |
||
| 925 | |||
| 926 | # vectorize lat |
||
| 927 | assert_allclose(apex_out.map_V_to_height([60, 70], 15, 100, 500, |
||
| 928 | np.array([[1, 2, 3]] * 2).T), |
||
| 929 | np.array([out_60_15_100_500, out_70_15_100_500]).T, |
||
| 930 | rtol=1e-5) |
||
| 931 | |||
| 932 | # vectorize lon |
||
| 933 | assert_allclose(apex_out.map_V_to_height(60, [15, 30], 100, 500, |
||
| 934 | np.array([[1, 2, 3]] * 2).T), |
||
| 935 | np.array([out_60_15_100_500, out_60_30_100_500]).T, |
||
| 936 | rtol=1e-5) |
||
| 937 | |||
| 938 | # vectorize height |
||
| 939 | assert_allclose(apex_out.map_V_to_height(60, 15, [100, 200], 500, |
||
| 940 | np.array([[1, 2, 3]] * 2).T), |
||
| 941 | np.array([out_60_15_100_500, out_60_15_200_500]).T, |
||
| 942 | rtol=1e-5) |
||
| 943 | |||
| 944 | # vectorize newheight |
||
| 945 | assert_allclose(apex_out.map_V_to_height(60, 15, 100, [500, 1000], |
||
| 946 | np.array([[1, 2, 3]] * 2).T), |
||
| 947 | np.array([out_60_15_100_500, out_60_15_100_1000]).T, |
||
| 948 | rtol=1e-5) |
||
| 949 | |||
| 950 | # vectorize E |
||
| 951 | assert_allclose(apex_out.map_V_to_height(60, 15, 100, 500, |
||
| 952 | np.array([[1, 2, 3], |
||
| 953 | [2, 3, 4]]).T), |
||
| 954 | np.array([out_60_15_100_500, out_60_15_100_500_234]).T, |
||
| 955 | rtol=1e-5) |
||
| 956 | |||
| 957 | |||
| 958 | # ============================================================================ |
||
| 959 | # Test basevectors_qd() |
||
| 960 | # ============================================================================ |
||
| 961 | |||
| 962 | |||
| 963 | # test coords |
||
| 964 | |||
| 965 | def test_basevectors_qd_scalar_geo(): |
||
| 966 | apex_out = Apex(date=2000, refh=300) |
||
| 967 | assert_allclose(apex_out.basevectors_qd(60, 15, 100, coords='geo'), |
||
| 968 | apex_out._basevec(60, 15, 100)) |
||
| 969 | |||
| 970 | |||
| 971 | def test_basevectors_qd_scalar_apex(): |
||
| 972 | apex_out = Apex(date=2000, refh=300) |
||
| 973 | glat, glon, _ = apex_out.apex2geo(60, 15, 100, precision=1e-2) |
||
| 974 | assert_allclose(apex_out.basevectors_qd(60, 15, 100, coords='apex', |
||
| 975 | precision=1e-2), |
||
| 976 | apex_out._basevec(glat, glon, 100)) |
||
| 977 | |||
| 978 | |||
| 979 | def test_basevectors_qd_scalar_qd(): |
||
| 980 | apex_out = Apex(date=2000, refh=300) |
||
| 981 | glat, glon, _ = apex_out.qd2geo(60, 15, 100, precision=1e-2) |
||
| 982 | assert_allclose(apex_out.basevectors_qd(60, 15, 100, coords='qd', |
||
| 983 | precision=1e-2), |
||
| 984 | apex_out._basevec(glat, glon, 100)) |
||
| 985 | |||
| 986 | |||
| 987 | # test shapes and vectorization of arguments |
||
| 988 | def test_basevectors_qd_scalar_shape(): |
||
| 989 | apex_out = Apex(date=2000, refh=300) |
||
| 990 | ret = apex_out.basevectors_qd(60, 15, 100) |
||
| 991 | for r in ret: |
||
| 992 | assert r.shape == (2,) |
||
| 993 | |||
| 994 | |||
| 995 | def test_basevectors_qd_vectorization(): |
||
| 996 | apex_out = Apex(date=2000, refh=300) |
||
| 997 | ret = apex_out.basevectors_qd([60, 60, 60, 60], 15, 100, coords='geo') |
||
| 998 | for r in ret: |
||
| 999 | assert r.shape == (2, 4) |
||
| 1000 | ret = apex_out.basevectors_qd(60, [15, 15, 15, 15], 100, coords='geo') |
||
| 1001 | for r in ret: |
||
| 1002 | assert r.shape == (2, 4) |
||
| 1003 | ret = apex_out.basevectors_qd(60, 15, [100, 100, 100, 100], coords='geo') |
||
| 1004 | for r in ret: |
||
| 1005 | assert r.shape == (2, 4) |
||
| 1006 | |||
| 1007 | |||
| 1008 | # test array return values |
||
| 1009 | |||
| 1010 | def test_basevectors_qd_array(): |
||
| 1011 | apex_out = Apex(date=2000, refh=300) |
||
| 1012 | f1, f2 = apex_out.basevectors_qd([0, 30], 15, 100, coords='geo') |
||
| 1013 | f1_lat0, f2_lat0 = apex_out._basevec(0, 15, 100) |
||
| 1014 | f1_lat30, f2_lat30 = apex_out._basevec(30, 15, 100) |
||
| 1015 | assert_allclose(f1[:, 0], f1_lat0) |
||
| 1016 | assert_allclose(f2[:, 0], f2_lat0) |
||
| 1017 | assert_allclose(f1[:, 1], f1_lat30) |
||
| 1018 | assert_allclose(f2[:, 1], f2_lat30) |
||
| 1019 | |||
| 1020 | |||
| 1021 | # ============================================================================ |
||
| 1022 | # Test basevectors_apex() |
||
| 1023 | # ============================================================================ |
||
| 1024 | |||
| 1025 | |||
| 1026 | # test against return from _geo2apexall for different coords |
||
| 1027 | |||
| 1028 | def test_basevectors_apex_scalar_geo(): |
||
| 1029 | apex_out = Apex(date=2000, refh=300) |
||
| 1030 | |||
| 1031 | (f1, f2, f3, g1, g2, g3, d1, d2, d3, e1, e2, |
||
| 1032 | e3) = apex_out.basevectors_apex(60, 15, 100, coords='geo') |
||
| 1033 | |||
| 1034 | (_, _, _, _, f1_, f2_, _, d1_, d2_, d3_, _, e1_, e2_, |
||
| 1035 | e3_) = apex_out._geo2apexall(60, 15, 100) |
||
| 1036 | |||
| 1037 | assert_allclose(f1, f1_) |
||
| 1038 | assert_allclose(f2, f2_) |
||
| 1039 | assert_allclose(d1, d1_) |
||
| 1040 | assert_allclose(d2, d2_) |
||
| 1041 | assert_allclose(d3, d3_) |
||
| 1042 | assert_allclose(e1, e1_) |
||
| 1043 | assert_allclose(e2, e2_) |
||
| 1044 | assert_allclose(e3, e3_) |
||
| 1045 | |||
| 1046 | |||
| 1047 | View Code Duplication | def test_basevectors_apex_scalar_apex(): |
|
| 1048 | apex_out = Apex(date=2000, refh=300) |
||
| 1049 | |||
| 1050 | (f1, f2, f3, g1, g2, g3, d1, d2, d3, e1, e2, |
||
| 1051 | e3) = apex_out.basevectors_apex(60, 15, 100, coords='apex', precision=1e-2) |
||
| 1052 | |||
| 1053 | glat, glon, _ = apex_out.apex2geo(60, 15, 100, precision=1e-2) |
||
| 1054 | (_, _, _, _, f1_, f2_, _, d1_, d2_, d3_, _, e1_, e2_, |
||
| 1055 | e3_) = apex_out._geo2apexall(glat, glon, 100) |
||
| 1056 | |||
| 1057 | assert_allclose(f1, f1_) |
||
| 1058 | assert_allclose(f2, f2_) |
||
| 1059 | assert_allclose(d1, d1_) |
||
| 1060 | assert_allclose(d2, d2_) |
||
| 1061 | assert_allclose(d3, d3_) |
||
| 1062 | assert_allclose(e1, e1_) |
||
| 1063 | assert_allclose(e2, e2_) |
||
| 1064 | assert_allclose(e3, e3_) |
||
| 1065 | |||
| 1066 | |||
| 1067 | View Code Duplication | def test_basevectors_apex_scalar_qd(): |
|
| 1068 | apex_out = Apex(date=2000, refh=300) |
||
| 1069 | |||
| 1070 | (f1, f2, f3, g1, g2, g3, d1, d2, d3, e1, e2, |
||
| 1071 | e3) = apex_out.basevectors_apex(60, 15, 100, coords='qd', precision=1e-2) |
||
| 1072 | |||
| 1073 | glat, glon, _ = apex_out.qd2geo(60, 15, 100, precision=1e-2) |
||
| 1074 | (_, _, _, _, f1_, f2_, _, d1_, d2_, d3_, _, e1_, e2_, |
||
| 1075 | e3_) = apex_out._geo2apexall(glat, glon, 100) |
||
| 1076 | |||
| 1077 | assert_allclose(f1, f1_) |
||
| 1078 | assert_allclose(f2, f2_) |
||
| 1079 | assert_allclose(d1, d1_) |
||
| 1080 | assert_allclose(d2, d2_) |
||
| 1081 | assert_allclose(d3, d3_) |
||
| 1082 | assert_allclose(e1, e1_) |
||
| 1083 | assert_allclose(e2, e2_) |
||
| 1084 | assert_allclose(e3, e3_) |
||
| 1085 | |||
| 1086 | |||
| 1087 | # test shapes and vectorization of arguments |
||
| 1088 | |||
| 1089 | def test_basevectors_apex_scalar_shape(): |
||
| 1090 | apex_out = Apex(date=2000, refh=300) |
||
| 1091 | ret = apex_out.basevectors_apex(60, 15, 100, precision=1e-2) |
||
| 1092 | for r in ret[:2]: |
||
| 1093 | assert r.shape == (2,) |
||
| 1094 | for r in ret[2:]: |
||
| 1095 | assert r.shape == (3,) |
||
| 1096 | |||
| 1097 | |||
| 1098 | def test_basevectors_apex_vectorization(): |
||
| 1099 | apex_out = Apex(date=2000, refh=300) |
||
| 1100 | ret = apex_out.basevectors_apex([60, 60, 60, 60], 15, 100) |
||
| 1101 | for r in ret[:2]: |
||
| 1102 | assert r.shape == (2, 4) |
||
| 1103 | for r in ret[2:]: |
||
| 1104 | assert r.shape == (3, 4) |
||
| 1105 | ret = apex_out.basevectors_apex(60, [15, 15, 15, 15], 100) |
||
| 1106 | for r in ret[:2]: |
||
| 1107 | assert r.shape == (2, 4) |
||
| 1108 | for r in ret[2:]: |
||
| 1109 | assert r.shape == (3, 4) |
||
| 1110 | ret = apex_out.basevectors_apex(60, 15, [100, 100, 100, 100]) |
||
| 1111 | for r in ret[:2]: |
||
| 1112 | assert r.shape == (2, 4) |
||
| 1113 | for r in ret[2:]: |
||
| 1114 | assert r.shape == (3, 4) |
||
| 1115 | |||
| 1116 | |||
| 1117 | # test correct vectorization of height |
||
| 1118 | View Code Duplication | def test_basevectors_apex_vectorization_height(): |
|
| 1119 | apex_out = Apex(date=2000, refh=0) |
||
| 1120 | (f1, f2, f3, g1, g2, g3, d1, d2, d3, e1, e2, |
||
| 1121 | e3) = apex_out.basevectors_apex(60, 15, [200, 400], coords='geo') |
||
| 1122 | (_, _, _, _, f1_1, f2_1, _, d1_1, d2_1, d3_1, _, e1_1, e2_1, |
||
| 1123 | e3_1) = apex_out._geo2apexall(60, 15, 200) |
||
| 1124 | (_, _, _, _, f1_2, f2_2, _, d1_2, d2_2, d3_2, _, e1_2, e2_2, |
||
| 1125 | e3_2) = apex_out._geo2apexall(60, 15, 400) |
||
| 1126 | |||
| 1127 | assert_allclose(f1[:, 0], f1_1) |
||
| 1128 | assert_allclose(f2[:, 0], f2_1) |
||
| 1129 | assert_allclose(d1[:, 0], d1_1) |
||
| 1130 | assert_allclose(d2[:, 0], d2_1) |
||
| 1131 | assert_allclose(d3[:, 0], d3_1) |
||
| 1132 | assert_allclose(e1[:, 0], e1_1) |
||
| 1133 | assert_allclose(e2[:, 0], e2_1) |
||
| 1134 | assert_allclose(e3[:, 0], e3_1) |
||
| 1135 | |||
| 1136 | assert_allclose(f3[:, 0], np.array([-0.088671, -0.018272, 0.993576]), |
||
| 1137 | rtol=1e-4) |
||
| 1138 | assert_allclose(g1[:, 0], np.array([0.903098, 0.245273, 0.085107]), |
||
| 1139 | rtol=1e-4) |
||
| 1140 | assert_allclose(g2[:, 0], np.array([-0.103495, 1.072078, 0.01048]), |
||
| 1141 | rtol=1e-4) |
||
| 1142 | assert_allclose(g3[:, 0], np.array([0, 0, 1.006465]), rtol=1e-4) |
||
| 1143 | |||
| 1144 | assert_allclose(f1[:, 1], f1_2) |
||
| 1145 | assert_allclose(f2[:, 1], f2_2) |
||
| 1146 | assert_allclose(d1[:, 1], d1_2) |
||
| 1147 | assert_allclose(d2[:, 1], d2_2) |
||
| 1148 | assert_allclose(d3[:, 1], d3_2) |
||
| 1149 | assert_allclose(e1[:, 1], e1_2) |
||
| 1150 | assert_allclose(e2[:, 1], e2_2) |
||
| 1151 | assert_allclose(e3[:, 1], e3_2) |
||
| 1152 | |||
| 1153 | assert_allclose(f3[:, 1], np.array([-0.085415, -0.021176, 0.989645]), |
||
| 1154 | rtol=1e-4) |
||
| 1155 | assert_allclose(g1[:, 1], np.array([0.902695, 0.246919, 0.083194]), |
||
| 1156 | rtol=1e-4) |
||
| 1157 | assert_allclose(g2[:, 1], np.array([-0.11051, 1.066094, 0.013274]), |
||
| 1158 | rtol=1e-4) |
||
| 1159 | assert_allclose(g3[:, 1], np.array([0, 0, 1.010463]), rtol=1e-4) |
||
| 1160 | |||
| 1161 | |||
| 1162 | # test scalar return values |
||
| 1163 | |||
| 1164 | def test_basevectors_apex_scalar(): |
||
| 1165 | apex_out = Apex(date=2000, refh=300) |
||
| 1166 | |||
| 1167 | (f1, f2, f3, g1, g2, g3, d1, d2, d3, e1, e2, |
||
| 1168 | e3) = apex_out.basevectors_apex(0, 15, 100, coords='geo') |
||
| 1169 | (_, _, _, _, f1_1, f2_1, _, d1_1, d2_1, d3_1, _, e1_1, e2_1, |
||
| 1170 | e3_1) = apex_out._geo2apexall(0, 15, 100) |
||
| 1171 | |||
| 1172 | assert_allclose(f1, f1_1) |
||
| 1173 | assert_allclose(f2, f2_1) |
||
| 1174 | assert_allclose(d1, d1_1) |
||
| 1175 | assert_allclose(d2, d2_1) |
||
| 1176 | assert_allclose(d3, d3_1) |
||
| 1177 | assert_allclose(e1, e1_1) |
||
| 1178 | assert_allclose(e2, e2_1) |
||
| 1179 | assert_allclose(e3, e3_1) |
||
| 1180 | |||
| 1181 | assert_allclose(f3, np.array([0.092637, -0.245951, 0.938848]), rtol=1e-4) |
||
| 1182 | assert_allclose(g1, np.array([0.939012, 0.073416, -0.07342]), rtol=1e-4) |
||
| 1183 | assert_allclose(g2, np.array([0.055389, 1.004155, 0.257594]), rtol=1e-4) |
||
| 1184 | assert_allclose(g3, np.array([0, 0, 1.065135]), rtol=1e-4) |
||
| 1185 | |||
| 1186 | |||
| 1187 | # test 1D array return values |
||
| 1188 | |||
| 1189 | View Code Duplication | def test_basevectors_apex_array(): |
|
| 1190 | apex_out = Apex(date=2000, refh=300) |
||
| 1191 | (f1, f2, f3, g1, g2, g3, d1, d2, d3, e1, e2, |
||
| 1192 | e3) = apex_out.basevectors_apex([0, 30], 15, 100, coords='geo') |
||
| 1193 | (_, _, _, _, f1_1, f2_1, _, d1_1, d2_1, d3_1, _, e1_1, e2_1, |
||
| 1194 | e3_1) = apex_out._geo2apexall(0, 15, 100) |
||
| 1195 | (_, _, _, _, f1_2, f2_2, _, d1_2, d2_2, d3_2, _, e1_2, e2_2, |
||
| 1196 | e3_2) = apex_out._geo2apexall(30, 15, 100) |
||
| 1197 | |||
| 1198 | assert_allclose(f1[:, 0], f1_1) |
||
| 1199 | assert_allclose(f2[:, 0], f2_1) |
||
| 1200 | assert_allclose(d1[:, 0], d1_1) |
||
| 1201 | assert_allclose(d2[:, 0], d2_1) |
||
| 1202 | assert_allclose(d3[:, 0], d3_1) |
||
| 1203 | assert_allclose(e1[:, 0], e1_1) |
||
| 1204 | assert_allclose(e2[:, 0], e2_1) |
||
| 1205 | assert_allclose(e3[:, 0], e3_1) |
||
| 1206 | |||
| 1207 | assert_allclose(f3[:, 0], np.array([0.092637, -0.245951, 0.938848]), |
||
| 1208 | rtol=1e-4) |
||
| 1209 | assert_allclose(g1[:, 0], np.array([0.939012, 0.073416, -0.07342]), |
||
| 1210 | rtol=1e-4) |
||
| 1211 | assert_allclose(g2[:, 0], np.array([0.055389, 1.004155, 0.257594]), |
||
| 1212 | rtol=1e-4) |
||
| 1213 | assert_allclose(g3[:, 0], np.array([0, 0, 1.065135]), rtol=1e-4) |
||
| 1214 | |||
| 1215 | assert_allclose(f1[:, 1], f1_2) |
||
| 1216 | assert_allclose(f2[:, 1], f2_2) |
||
| 1217 | assert_allclose(d1[:, 1], d1_2) |
||
| 1218 | assert_allclose(d2[:, 1], d2_2) |
||
| 1219 | assert_allclose(d3[:, 1], d3_2) |
||
| 1220 | assert_allclose(e1[:, 1], e1_2) |
||
| 1221 | assert_allclose(e2[:, 1], e2_2) |
||
| 1222 | assert_allclose(e3[:, 1], e3_2) |
||
| 1223 | |||
| 1224 | assert_allclose(f3[:, 1], np.array([-0.036618, -0.071019, 0.861604]), |
||
| 1225 | rtol=1e-4) |
||
| 1226 | assert_allclose(g1[:, 1], np.array([0.844391, 0.015353, 0.037152]), |
||
| 1227 | rtol=1e-4) |
||
| 1228 | assert_allclose(g2[:, 1], np.array([0.050808, 1.02131, 0.086342]), |
||
| 1229 | rtol=1e-4) |
||
| 1230 | assert_allclose(g3[:, 1], np.array([0, 0, 1.160625]), rtol=1e-4) |
||
| 1231 | |||
| 1232 | |||
| 1233 | # test that vectors are calculated correctly |
||
| 1234 | |||
| 1235 | def test_basevectors_apex_delta(): |
||
| 1236 | apex_out = Apex(date=2000, refh=300) |
||
| 1237 | for lat in range(0, 90, 10): |
||
| 1238 | for lon in range(0, 360, 15): |
||
| 1239 | (f1, f2, f3, g1, g2, g3, d1, d2, d3, e1, e2, |
||
| 1240 | e3) = apex_out.basevectors_apex(lat, lon, 500) |
||
| 1241 | f = [np.append(f1, 0), np.append(f2, 0), f3] |
||
| 1242 | g = [g1, g2, g3] |
||
| 1243 | d = [d1, d2, d3] |
||
| 1244 | e = [e1, e2, e3] |
||
| 1245 | for i, j in [(i, j) for i in range(3) for j in range(3)]: |
||
| 1246 | delta = 1 if i == j else 0 |
||
| 1247 | assert_allclose(np.sum(f[i] * g[j]), delta, rtol=0, atol=1e-5) |
||
| 1248 | assert_allclose(np.sum(d[i] * e[j]), delta, rtol=0, atol=1e-5) |
||
| 1249 | |||
| 1250 | |||
| 1251 | def test_basevectors_apex_invalid_scalar(recwarn): |
||
| 1252 | """Test warning and fill values for calculating base vectors with bad value. |
||
| 1253 | """ |
||
| 1254 | apex_out = Apex(date=2000, refh=10000) |
||
| 1255 | base_vecs = apex_out.basevectors_apex(0, 0, 0) |
||
| 1256 | |||
| 1257 | assert issubclass(recwarn[-1].category, UserWarning) |
||
| 1258 | assert 'set to NaN where' in str(recwarn[-1].message) |
||
| 1259 | |||
| 1260 | invalid = np.ones(3) * np.nan |
||
| 1261 | for i, bvec in enumerate(base_vecs): |
||
| 1262 | if i < 2: |
||
| 1263 | assert not np.allclose(bvec, invalid[:2]) |
||
| 1264 | else: |
||
| 1265 | assert_allclose(bvec, invalid) |
||
| 1266 | |||
| 1267 | |||
| 1268 | # ============================================================================ |
||
| 1269 | # Test the get_apex() method |
||
| 1270 | # ============================================================================ |
||
| 1271 | |||
| 1272 | |||
| 1273 | def test_get_apex(): |
||
| 1274 | apex_out = Apex(date=2000, refh=300) |
||
| 1275 | assert_allclose(apex_out.get_apex(10), 507.409702543805) |
||
| 1276 | assert_allclose(apex_out.get_apex(60), 20313.026999999987) |
||
| 1277 | |||
| 1278 | |||
| 1279 | def test_get_apex_invalid_lat(): |
||
| 1280 | apex_out = Apex(date=2000, refh=300) |
||
| 1281 | with pytest.raises(ValueError): |
||
| 1282 | apex_out.get_apex(91) |
||
| 1283 | with pytest.raises(ValueError): |
||
| 1284 | apex_out.get_apex(-91) |
||
| 1285 | apex_out.get_apex(90) |
||
| 1286 | apex_out.get_apex(-90) |
||
| 1287 | |||
| 1288 | assert_allclose(apex_out.get_apex(90 + 1e-5), apex_out.get_apex(90), |
||
| 1289 | rtol=0, atol=1e-8) |
||
| 1290 | |||
| 1291 | |||
| 1292 | # ============================================================================ |
||
| 1293 | # Test the set_epoch() method |
||
| 1294 | # ============================================================================ |
||
| 1295 | |||
| 1296 | |||
| 1297 | def test_set_epoch(): |
||
| 1298 | """Test successful setting of Apex epoch.""" |
||
| 1299 | apex_out = Apex(date=2000.2, refh=300) |
||
| 1300 | assert_allclose(apex_out.year, 2000.2) |
||
| 1301 | ret_2000_2_py = apex_out._geo2apex(60, 15, 100) |
||
| 1302 | apex_out.set_epoch(2000.8) |
||
| 1303 | assert_allclose(apex_out.year, 2000.8) |
||
| 1304 | ret_2000_8_py = apex_out._geo2apex(60, 15, 100) |
||
| 1305 | |||
| 1306 | assert ret_2000_2_py != ret_2000_8_py |
||
| 1307 | |||
| 1308 | fa.loadapxsh(apex_out.datafile, 2000.2) |
||
| 1309 | ret_2000_2_apex = fa.apxg2all(60, 15, 100, 300, 0)[2:4] |
||
| 1310 | fa.loadapxsh(apex_out.datafile, 2000.8) |
||
| 1311 | ret_2000_8_apex = fa.apxg2all(60, 15, 100, 300, 0)[2:4] |
||
| 1312 | |||
| 1313 | assert ret_2000_2_apex != ret_2000_8_apex |
||
| 1314 | |||
| 1315 | assert_allclose(ret_2000_2_py, ret_2000_2_apex) |
||
| 1316 | assert_allclose(ret_2000_8_py, ret_2000_8_apex) |
||
| 1317 | |||
| 1318 | |||
| 1319 | @pytest.fixture() |
||
| 1320 | def igrf_file(): |
||
| 1321 | # Ensure the coefficient file exists |
||
| 1322 | original_file = os.path.join(os.path.dirname(helpers.__file__), |
||
| 1323 | 'igrf13coeffs.txt') |
||
| 1324 | tmp_file = "temp_coeff.txt" |
||
| 1325 | assert os.path.isfile(original_file) |
||
| 1326 | # Move the coefficient file |
||
| 1327 | os.rename(original_file, tmp_file) |
||
| 1328 | yield original_file |
||
| 1329 | # Move the coefficient file back |
||
| 1330 | os.rename(tmp_file, original_file) |
||
| 1331 | |||
| 1332 | |||
| 1333 | def test_set_epoch_file_error(igrf_file): |
||
| 1334 | """Test raises OSError when IGRF coefficient file is missing.""" |
||
| 1335 | # Test missing coefficient file failure |
||
| 1336 | with pytest.raises(OSError) as oerr: |
||
| 1337 | Apex(date=2000.2, refh=300) |
||
| 1338 | error_string = "File {:} does not exist".format(igrf_file) |
||
| 1339 | assert str(oerr.value).startswith(error_string) |
||
| 1340 | |||
| 1341 | |||
| 1342 | # ============================================================================ |
||
| 1343 | # Test the set_refh() method |
||
| 1344 | # ============================================================================ |
||
| 1345 | |||
| 1346 | |||
| 1347 | def test_set_refh(): |
||
| 1348 | apex_out = Apex(date=2000, refh=300) |
||
| 1349 | assert apex_out.refh, 300 |
||
| 1350 | ret_300 = apex_out._geo2apex(60, 15, 100) |
||
| 1351 | apex_out.set_refh(500) |
||
| 1352 | assert apex_out.refh == 500 |
||
| 1353 | ret_500 = apex_out._geo2apex(60, 15, 100) |
||
| 1354 | |||
| 1355 | assert_allclose(ret_300, fa.apxg2all(60, 15, 100, 300, 0)[2:4]) |
||
| 1356 | assert_allclose(ret_500, fa.apxg2all(60, 15, 100, 500, 0)[2:4]) |
||
| 1357 | |||
| 1358 | |||
| 1359 | # ============================================================================ |
||
| 1360 | # Test the get_babs() method |
||
| 1361 | # ============================================================================ |
||
| 1362 | |||
| 1363 | |||
| 1364 | def test_get_babs(): |
||
| 1365 | inputs = [[[80], [100], [300]], [range(50, 90, 8), range(0, 360, 80), |
||
| 1366 | [300] * 5], [90.0, 0, 1000]] |
||
| 1367 | temp1 = np.array([4.22045410e-05, 5.15672743e-05, 4.98150200e-05, |
||
| 1368 | 5.06769359e-05, 4.91028428e-05]) |
||
| 1369 | expected = [[5.1303124427795412e-05], temp1, [3.793962299823761e-05]] |
||
| 1370 | |||
| 1371 | apex_out = Apex(date=2018.1, refh=0) |
||
| 1372 | for i in range(len(inputs)): |
||
| 1373 | outputs = apex_out.get_babs(*inputs[i]) |
||
| 1374 | if isinstance(outputs, np.float64): |
||
| 1375 | outputs = [outputs] |
||
| 1376 | for j, output in enumerate(outputs): |
||
| 1377 | assert_allclose(output, expected[i][j], rtol=0, atol=1e-5) |
||
| 1378 | |||
| 1379 | |||
| 1380 | # ============================================================================ |
||
| 1381 | # Test the bvectors_apex() method |
||
| 1382 | # ============================================================================ |
||
| 1383 | |||
| 1384 | |||
| 1385 | def test_bvectors_apex(): |
||
| 1386 | inputs = [[80, 81], [100, 120], [100, 200]] |
||
| 1387 | |||
| 1388 | expected = (np.array([5.95166171e-05, 5.95958974e-05]), |
||
| 1389 | np.array([[0.0191583, 0.0020023], |
||
| 1390 | [0.03547136, 0.03392595], |
||
| 1391 | [-0.9412518, -0.8991005]]), |
||
| 1392 | np.array([5.28257734e-05, 4.82450628e-05]), |
||
| 1393 | np.array([[0.02158486, 0.00247339], |
||
| 1394 | [0.03996412, 0.04190787], |
||
| 1395 | [-1.0604696, -1.110636]])) |
||
| 1396 | |||
| 1397 | apex_out = Apex(date=2018.1, refh=0) |
||
| 1398 | |||
| 1399 | outputs = apex_out.bvectors_apex(*inputs, coords='geo', precision=1e-10) |
||
| 1400 | for i, output in enumerate(outputs): |
||
| 1401 | for j in range(output.size): |
||
| 1402 | assert_allclose(output.ravel()[j], expected[i].ravel()[j], rtol=0, |
||
| 1403 | atol=1e-5) |
||
| 1404 | |||
| 1405 | |||
| 1406 | if __name__ == '__main__': |
||
| 1407 | pytest.main() |
||
| 1408 |