| Total Complexity | 41 |
| Total Lines | 343 |
| Duplicated Lines | 47.81 % |
| 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_c_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 numpy as np |
||
| 5 | import pytest |
||
| 6 | import aacgmv2 |
||
| 7 | |||
| 8 | class TestCAACGMV2: |
||
| 9 | def setup(self): |
||
| 10 | """Runs before every method to create a clean testing setup""" |
||
| 11 | self.date_args = [(2014, 3, 22, 3, 11, 0, aacgmv2.AACGM_V2_DAT_PREFIX), |
||
| 12 | (2018, 1, 1, 0, 0, 0, aacgmv2.AACGM_V2_DAT_PREFIX)] |
||
| 13 | self.long_date = [2014, 3, 22, 3, 11, 0] |
||
| 14 | self.mlat = None |
||
| 15 | self.mlon = None |
||
| 16 | self.rshell = None |
||
| 17 | self.mlt = None |
||
| 18 | self.lat_in = [45.5, 60] |
||
| 19 | self.lon_in = [-23.5, 0] |
||
| 20 | self.alt_in = [1135, 300] |
||
| 21 | |||
| 22 | def teardown(self): |
||
| 23 | """Runs after every method to clean up previous testing""" |
||
| 24 | del self.date_args, self.long_date, self.mlat, self.mlon, self.mlt |
||
| 25 | del self.lat_in, self.lon_in, self.alt_in |
||
| 26 | |||
| 27 | def test_module_structure(self): |
||
| 28 | """Test module structure""" |
||
| 29 | assert aacgmv2 |
||
| 30 | assert aacgmv2._aacgmv2 |
||
| 31 | assert aacgmv2._aacgmv2.set_datetime |
||
| 32 | assert aacgmv2._aacgmv2.convert |
||
| 33 | assert aacgmv2._aacgmv2.inv_mlt_convert |
||
| 34 | assert aacgmv2._aacgmv2.inv_mlt_convert_yrsec |
||
| 35 | assert aacgmv2._aacgmv2.mlt_convert |
||
| 36 | assert aacgmv2._aacgmv2.mlt_convert_yrsec |
||
| 37 | |||
| 38 | def test_constants(self): |
||
| 39 | """Test module constants""" |
||
| 40 | ans1 = aacgmv2._aacgmv2.G2A == 0 |
||
| 41 | ans2 = aacgmv2._aacgmv2.A2G == 1 |
||
| 42 | ans3 = aacgmv2._aacgmv2.TRACE == 2 |
||
| 43 | ans4 = aacgmv2._aacgmv2.ALLOWTRACE == 4 |
||
| 44 | ans5 = aacgmv2._aacgmv2.BADIDEA == 8 |
||
| 45 | ans6 = aacgmv2._aacgmv2.GEOCENTRIC == 16 |
||
| 46 | |||
| 47 | assert ans1 & ans2 & ans3 & ans4 & ans5 & ans6 |
||
| 48 | del ans1, ans2, ans3, ans4, ans5, ans6 |
||
| 49 | |||
| 50 | def test_set_datetime(self): |
||
| 51 | """Test set_datetime""" |
||
| 52 | for darg in self.date_args: |
||
| 53 | arg1 = aacgmv2._aacgmv2.set_datetime(*darg) is None |
||
| 54 | assert arg1 |
||
| 55 | |||
| 56 | def test_fail_set_datetime(self): |
||
| 57 | """Test unsuccessful set_datetime""" |
||
| 58 | with pytest.raises(RuntimeError): |
||
| 59 | aacgmv2._aacgmv2.set_datetime(1013, 1, 1, 0, 0, 0, |
||
| 60 | aacgmv2.AACGM_V2_DAT_PREFIX) |
||
| 61 | |||
| 62 | View Code Duplication | def test_convert_G2A_coeff(self): |
|
|
|
|||
| 63 | """Test convert from geographic to magnetic coordinates""" |
||
| 64 | lat_comp = [48.1896, 58.1633] |
||
| 65 | lon_comp = [57.7635, 81.0719] |
||
| 66 | r_comp = [1.1775, 1.0457] |
||
| 67 | |||
| 68 | for i,darg in enumerate(self.date_args): |
||
| 69 | aacgmv2._aacgmv2.set_datetime(*darg) |
||
| 70 | (self.mlat, self.mlon, |
||
| 71 | self.rshell) = aacgmv2._aacgmv2.convert(self.lat_in[i], |
||
| 72 | self.lon_in[i], |
||
| 73 | self.alt_in[i], |
||
| 74 | aacgmv2._aacgmv2.G2A, |
||
| 75 | aacgmv2.IGRF_12_COEFFS) |
||
| 76 | np.testing.assert_almost_equal(self.mlat, lat_comp[i], decimal=4) |
||
| 77 | np.testing.assert_almost_equal(self.mlon, lon_comp[i], decimal=4) |
||
| 78 | np.testing.assert_almost_equal(self.rshell, r_comp[i], decimal=4) |
||
| 79 | |||
| 80 | del lat_comp, lon_comp, r_comp |
||
| 81 | |||
| 82 | View Code Duplication | def test_convert_A2G_coeff(self): |
|
| 83 | """Test convert from magnetic to geodetic coordinates""" |
||
| 84 | lat_comp = [30.7534, 50.3910] |
||
| 85 | lon_comp = [-94.1806, -77.7919] |
||
| 86 | r_comp = [1133.6241, 305.7138] |
||
| 87 | |||
| 88 | for i,darg in enumerate(self.date_args): |
||
| 89 | aacgmv2._aacgmv2.set_datetime(*darg) |
||
| 90 | (self.mlat, self.mlon, |
||
| 91 | self.rshell) = aacgmv2._aacgmv2.convert(self.lat_in[i], |
||
| 92 | self.lon_in[i], |
||
| 93 | self.alt_in[i], |
||
| 94 | aacgmv2._aacgmv2.A2G, |
||
| 95 | aacgmv2.IGRF_12_COEFFS) |
||
| 96 | np.testing.assert_almost_equal(self.mlat, lat_comp[i], decimal=4) |
||
| 97 | np.testing.assert_almost_equal(self.mlon, lon_comp[i], decimal=4) |
||
| 98 | np.testing.assert_almost_equal(self.rshell, r_comp[i], decimal=4) |
||
| 99 | |||
| 100 | del lat_comp, lon_comp, r_comp |
||
| 101 | |||
| 102 | View Code Duplication | def test_convert_G2A_TRACE(self): |
|
| 103 | """Test convert from geodetic to magnetic coordinates using trace""" |
||
| 104 | code = aacgmv2._aacgmv2.G2A + aacgmv2._aacgmv2.TRACE |
||
| 105 | trace_lat = [48.1948, 58.1633] |
||
| 106 | trace_lon = [57.7588, 81.0756] |
||
| 107 | trace_r = [1.1775, 1.0457] |
||
| 108 | |||
| 109 | for i,dargs in enumerate(self.date_args): |
||
| 110 | aacgmv2._aacgmv2.set_datetime(*dargs) |
||
| 111 | (self.mlat, self.mlon, |
||
| 112 | self.rshell) = aacgmv2._aacgmv2.convert(self.lat_in[i], |
||
| 113 | self.lon_in[i], |
||
| 114 | self.alt_in[i], code, |
||
| 115 | aacgmv2.IGRF_12_COEFFS) |
||
| 116 | np.testing.assert_almost_equal(self.mlat, trace_lat[i], decimal=4) |
||
| 117 | np.testing.assert_almost_equal(self.mlon, trace_lon[i], decimal=4) |
||
| 118 | np.testing.assert_almost_equal(self.rshell, trace_r[i], decimal=4) |
||
| 119 | |||
| 120 | del code, trace_lat, trace_lon, trace_r |
||
| 121 | |||
| 122 | View Code Duplication | def test_convert_A2G_TRACE(self): |
|
| 123 | """Test convert from magnetic to geodetic coordinates using trace""" |
||
| 124 | code = aacgmv2._aacgmv2.A2G + aacgmv2._aacgmv2.TRACE |
||
| 125 | trace_lat = [30.7644, 50.3958] |
||
| 126 | trace_lon = [-94.1809, -77.8019] |
||
| 127 | trace_r = [1133.6277, 305.7156] |
||
| 128 | |||
| 129 | for i,dargs in enumerate(self.date_args): |
||
| 130 | aacgmv2._aacgmv2.set_datetime(*dargs) |
||
| 131 | (self.mlat, self.mlon, |
||
| 132 | self.rshell) = aacgmv2._aacgmv2.convert(self.lat_in[i], |
||
| 133 | self.lon_in[i], |
||
| 134 | self.alt_in[i], code, |
||
| 135 | aacgmv2.IGRF_12_COEFFS) |
||
| 136 | np.testing.assert_almost_equal(self.mlat, trace_lat[i], decimal=4) |
||
| 137 | np.testing.assert_almost_equal(self.mlon, trace_lon[i], decimal=4) |
||
| 138 | np.testing.assert_almost_equal(self.rshell, trace_r[i], decimal=4) |
||
| 139 | |||
| 140 | del code, trace_lat, trace_lon, trace_r |
||
| 141 | |||
| 142 | def test_convert_high_denied(self): |
||
| 143 | """Test for failure when converting to high altitude geodetic to |
||
| 144 | magnetic coordinates""" |
||
| 145 | aacgmv2._aacgmv2.set_datetime(*self.date_args[0]) |
||
| 146 | with pytest.raises(RuntimeError): |
||
| 147 | aacgmv2._aacgmv2.convert(self.lat_in[0], self.lon_in[0], 5500, |
||
| 148 | aacgmv2._aacgmv2.G2A, |
||
| 149 | aacgmv2.IGRF_12_COEFFS) |
||
| 150 | |||
| 151 | def test_convert_high_TRACE(self): |
||
| 152 | """Test convert from high altitude geodetic to magnetic coordinates |
||
| 153 | using trace""" |
||
| 154 | code = aacgmv2._aacgmv2.G2A + aacgmv2._aacgmv2.TRACE |
||
| 155 | aacgmv2._aacgmv2.set_datetime(*self.date_args[0]) |
||
| 156 | (self.mlat, self.mlon, |
||
| 157 | self.rshell) = aacgmv2._aacgmv2.convert(self.lat_in[0], self.lon_in[0], |
||
| 158 | 5500, code, |
||
| 159 | aacgmv2.IGRF_12_COEFFS) |
||
| 160 | np.testing.assert_almost_equal(self.mlat, 59.9748, decimal=4) |
||
| 161 | np.testing.assert_almost_equal(self.mlon, 57.7425, decimal=4) |
||
| 162 | np.testing.assert_almost_equal(self.rshell, 1.8626, decimal=4) |
||
| 163 | |||
| 164 | del code |
||
| 165 | |||
| 166 | def test_convert_high_ALLOWTRACE(self): |
||
| 167 | """Test convert from high altitude geodetic to magnetic coordinates |
||
| 168 | by allowing IGRF tracing""" |
||
| 169 | code = aacgmv2._aacgmv2.G2A + aacgmv2._aacgmv2.ALLOWTRACE |
||
| 170 | aacgmv2._aacgmv2.set_datetime(*self.date_args[0]) |
||
| 171 | (self.mlat, self.mlon, |
||
| 172 | self.rshell) = aacgmv2._aacgmv2.convert(self.lat_in[0], self.lon_in[0], |
||
| 173 | 5500, code, |
||
| 174 | aacgmv2.IGRF_12_COEFFS) |
||
| 175 | np.testing.assert_almost_equal(self.mlat, 59.9748, decimal=4) |
||
| 176 | np.testing.assert_almost_equal(self.mlon, 57.7425, decimal=4) |
||
| 177 | np.testing.assert_almost_equal(self.rshell, 1.8626, decimal=4) |
||
| 178 | |||
| 179 | del code |
||
| 180 | |||
| 181 | def test_convert_high_BADIDEA(self): |
||
| 182 | """Test convert from high altitude geodetic to magnetic coordinates |
||
| 183 | using coefficients""" |
||
| 184 | code = aacgmv2._aacgmv2.G2A + aacgmv2._aacgmv2.BADIDEA |
||
| 185 | aacgmv2._aacgmv2.set_datetime(*self.date_args[0]) |
||
| 186 | (self.mlat, self.mlon, |
||
| 187 | self.rshell) = aacgmv2._aacgmv2.convert(self.lat_in[0], self.lon_in[0], |
||
| 188 | 5500, code, |
||
| 189 | aacgmv2.IGRF_12_COEFFS) |
||
| 190 | np.testing.assert_almost_equal(self.mlat, 58.7154, decimal=4) |
||
| 191 | np.testing.assert_almost_equal(self.mlon, 56.5830, decimal=4) |
||
| 192 | np.testing.assert_almost_equal(self.rshell, 1.8626, decimal=4) |
||
| 193 | |||
| 194 | del code |
||
| 195 | |||
| 196 | View Code Duplication | def test_convert_GEOCENTRIC_G2A_coeff(self): |
|
| 197 | """Test convert from geographic to magnetic coordinates""" |
||
| 198 | code = aacgmv2._aacgmv2.G2A + aacgmv2._aacgmv2.GEOCENTRIC |
||
| 199 | aacgmv2._aacgmv2.set_datetime(*self.date_args[0]) |
||
| 200 | (self.mlat, self.mlon, |
||
| 201 | self.rshell) = aacgmv2._aacgmv2.convert(self.lat_in[0], self.lon_in[0], |
||
| 202 | self.alt_in[0], code, |
||
| 203 | aacgmv2.IGRF_12_COEFFS) |
||
| 204 | np.testing.assert_almost_equal(self.mlat, 48.3779, decimal=4) |
||
| 205 | np.testing.assert_almost_equal(self.mlon, 57.7974, decimal=4) |
||
| 206 | np.testing.assert_almost_equal(self.rshell, 1.1781, decimal=4) |
||
| 207 | |||
| 208 | del code |
||
| 209 | |||
| 210 | View Code Duplication | def test_convert_GEOCENTRIC_A2G_coeff(self): |
|
| 211 | """Test convert from magnetic to geocentric coordinates""" |
||
| 212 | code = aacgmv2._aacgmv2.A2G + aacgmv2._aacgmv2.GEOCENTRIC |
||
| 213 | aacgmv2._aacgmv2.set_datetime(*self.date_args[0]) |
||
| 214 | (self.mlat, self.mlon, |
||
| 215 | self.rshell) = aacgmv2._aacgmv2.convert(self.lat_in[0], self.lon_in[0], |
||
| 216 | self.alt_in[0], code, |
||
| 217 | aacgmv2.IGRF_12_COEFFS) |
||
| 218 | np.testing.assert_almost_equal(self.mlat, 30.6101, decimal=4) |
||
| 219 | np.testing.assert_almost_equal(self.mlon, -94.1806, decimal=4) |
||
| 220 | np.testing.assert_almost_equal(self.rshell, 1135.0000, decimal=4) |
||
| 221 | |||
| 222 | del code |
||
| 223 | |||
| 224 | View Code Duplication | def test_convert_GEOCENTRIC_G2A_TRACE(self): |
|
| 225 | """Test convert from geographic to magnetic coordinates using trace""" |
||
| 226 | code = aacgmv2._aacgmv2.G2A + aacgmv2._aacgmv2.TRACE + \ |
||
| 227 | aacgmv2._aacgmv2.GEOCENTRIC |
||
| 228 | aacgmv2._aacgmv2.set_datetime(*self.date_args[0]) |
||
| 229 | (self.mlat, self.mlon, |
||
| 230 | self.rshell) = aacgmv2._aacgmv2.convert(self.lat_in[0], self.lon_in[0], |
||
| 231 | self.alt_in[0], code, |
||
| 232 | aacgmv2.IGRF_12_COEFFS) |
||
| 233 | np.testing.assert_almost_equal(self.mlat, 48.3830, decimal=4) |
||
| 234 | np.testing.assert_almost_equal(self.mlon, 57.7926, decimal=4) |
||
| 235 | np.testing.assert_almost_equal(self.rshell, 1.1781, decimal=4) |
||
| 236 | |||
| 237 | del code |
||
| 238 | |||
| 239 | View Code Duplication | def test_convert_GEOCENTRIC_A2G_TRACE(self): |
|
| 240 | """Test convert from magnetic to geographic coordinates using trace""" |
||
| 241 | code = aacgmv2._aacgmv2.A2G + aacgmv2._aacgmv2.TRACE + \ |
||
| 242 | aacgmv2._aacgmv2.GEOCENTRIC |
||
| 243 | aacgmv2._aacgmv2.set_datetime(*self.date_args[0]) |
||
| 244 | (self.mlat, self.mlon, |
||
| 245 | self.rshell) = aacgmv2._aacgmv2.convert(self.lat_in[0], self.lon_in[0], |
||
| 246 | self.alt_in[0], code, |
||
| 247 | aacgmv2.IGRF_12_COEFFS) |
||
| 248 | np.testing.assert_almost_equal(self.mlat, 30.6211, decimal=4) |
||
| 249 | np.testing.assert_almost_equal(self.mlon, -94.1809, decimal=4) |
||
| 250 | np.testing.assert_almost_equal(self.rshell, 1135.0000, decimal=4) |
||
| 251 | |||
| 252 | del code |
||
| 253 | |||
| 254 | def test_forbidden(self): |
||
| 255 | """Test convert failure""" |
||
| 256 | with pytest.raises(RuntimeError): |
||
| 257 | mloc = aacgmv2._aacgmv2.convert(7, 0, 0, aacgmv2._aacgmv2.G2A, |
||
| 258 | aacgmv2.IGRF_12_COEFFS) |
||
| 259 | |||
| 260 | View Code Duplication | def test_inv_mlt_convert(self): |
|
| 261 | """Test MLT inversion""" |
||
| 262 | mlt_args = list(self.long_date) |
||
| 263 | mlt_args.extend([12.0, aacgmv2.AACGM_V2_DAT_PREFIX, |
||
| 264 | aacgmv2.IGRF_12_COEFFS]) |
||
| 265 | self.mlon = aacgmv2._aacgmv2.inv_mlt_convert(*mlt_args) |
||
| 266 | np.testing.assert_almost_equal(self.mlon, -153.5931, decimal=4) |
||
| 267 | |||
| 268 | mlt_args[-3] = 25.0 |
||
| 269 | self.mlon = aacgmv2._aacgmv2.inv_mlt_convert(*mlt_args) |
||
| 270 | np.testing.assert_almost_equal(self.mlon, 41.4069, decimal=4) |
||
| 271 | |||
| 272 | mlt_args[-3] = -1.0 |
||
| 273 | self.mlon = aacgmv2._aacgmv2.inv_mlt_convert(*mlt_args) |
||
| 274 | np.testing.assert_almost_equal(self.mlon, 11.4069, decimal=4) |
||
| 275 | |||
| 276 | del mlt_args |
||
| 277 | |||
| 278 | def test_inv_mlt_convert_yrsec(self): |
||
| 279 | """Test MLT inversion with year and seconds of year""" |
||
| 280 | import datetime as dt |
||
| 281 | dtime = dt.datetime(*self.long_date) |
||
| 282 | soy = (int(dtime.strftime("%j"))-1) * 86400 + dtime.hour * 3600 + \ |
||
| 283 | dtime.minute * 60 + dtime.second |
||
| 284 | |||
| 285 | mlt_args_1 = [dtime.year, soy, 12.0, aacgmv2.AACGM_V2_DAT_PREFIX, |
||
| 286 | aacgmv2.IGRF_12_COEFFS] |
||
| 287 | mlt_args_2 = [dtime.year, soy, 25.0, aacgmv2.AACGM_V2_DAT_PREFIX, |
||
| 288 | aacgmv2.IGRF_12_COEFFS] |
||
| 289 | mlt_args_3 = [dtime.year, soy, -1.0, aacgmv2.AACGM_V2_DAT_PREFIX, |
||
| 290 | aacgmv2.IGRF_12_COEFFS] |
||
| 291 | |||
| 292 | mlon_1 = aacgmv2._aacgmv2.inv_mlt_convert_yrsec(*mlt_args_1) |
||
| 293 | mlon_2 = aacgmv2._aacgmv2.inv_mlt_convert_yrsec(*mlt_args_2) |
||
| 294 | mlon_3 = aacgmv2._aacgmv2.inv_mlt_convert_yrsec(*mlt_args_3) |
||
| 295 | |||
| 296 | np.testing.assert_almost_equal(mlon_1, -153.5931, decimal=4) |
||
| 297 | np.testing.assert_almost_equal(mlon_2, 41.4069, decimal=4) |
||
| 298 | np.testing.assert_almost_equal(mlon_3, 11.4069, decimal=4) |
||
| 299 | |||
| 300 | del dtime, soy, mlt_args_1, mlt_args_2, mlt_args_3, mlon_1, mlon_2 |
||
| 301 | del mlon_3 |
||
| 302 | |||
| 303 | View Code Duplication | def test_mlt_convert(self): |
|
| 304 | """Test MLT calculation""" |
||
| 305 | mlt_args = list(self.long_date) |
||
| 306 | mlt_args.extend([270.0, aacgmv2.AACGM_V2_DAT_PREFIX, |
||
| 307 | aacgmv2.IGRF_12_COEFFS]) |
||
| 308 | self.mlt = aacgmv2._aacgmv2.mlt_convert(*mlt_args) |
||
| 309 | np.testing.assert_almost_equal(self.mlt, 16.2395, decimal=4) |
||
| 310 | |||
| 311 | mlt_args[-3] = 80.0 |
||
| 312 | self.mlt = aacgmv2._aacgmv2.mlt_convert(*mlt_args) |
||
| 313 | np.testing.assert_almost_equal(self.mlt, 3.5729, decimal=4) |
||
| 314 | |||
| 315 | mlt_args[-3] = -90.0 |
||
| 316 | self.mlt = aacgmv2._aacgmv2.mlt_convert(*mlt_args) |
||
| 317 | np.testing.assert_almost_equal(self.mlt, 16.2395, decimal=4) |
||
| 318 | |||
| 319 | del mlt_args |
||
| 320 | |||
| 321 | def test_mlt_convert_yrsec(self): |
||
| 322 | """Test MLT calculation using year and seconds of year""" |
||
| 323 | import datetime as dt |
||
| 324 | dtime = dt.datetime(*self.long_date) |
||
| 325 | soy = (int(dtime.strftime("%j"))-1) * 86400 + dtime.hour * 3600 + \ |
||
| 326 | dtime.minute * 60 + dtime.second |
||
| 327 | mlt_args_1 = [dtime.year, soy, 270.0, aacgmv2.AACGM_V2_DAT_PREFIX, |
||
| 328 | aacgmv2.IGRF_12_COEFFS] |
||
| 329 | mlt_args_2 = [dtime.year, soy, 80.0, aacgmv2.AACGM_V2_DAT_PREFIX, |
||
| 330 | aacgmv2.IGRF_12_COEFFS] |
||
| 331 | mlt_args_3 = [dtime.year, soy, -90.0, aacgmv2.AACGM_V2_DAT_PREFIX, |
||
| 332 | aacgmv2.IGRF_12_COEFFS] |
||
| 333 | |||
| 334 | mlt_1 = aacgmv2._aacgmv2.mlt_convert_yrsec(*mlt_args_1) |
||
| 335 | mlt_2 = aacgmv2._aacgmv2.mlt_convert_yrsec(*mlt_args_2) |
||
| 336 | mlt_3 = aacgmv2._aacgmv2.mlt_convert_yrsec(*mlt_args_3) |
||
| 337 | |||
| 338 | np.testing.assert_almost_equal(mlt_1, 16.2395, decimal=4) |
||
| 339 | np.testing.assert_almost_equal(mlt_2, 3.5729, decimal=4) |
||
| 340 | np.testing.assert_equal(mlt_1, mlt_3) |
||
| 341 | |||
| 342 | del dtime, soy, mlt_args_1, mlt_args_2, mlt_args_3, mlt_1, mlt_2, mlt_3 |
||
| 343 |