|
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 pytest |
|
9
|
|
|
import warnings |
|
10
|
|
|
|
|
11
|
|
|
import aacgmv2 |
|
12
|
|
|
|
|
13
|
|
|
class TestFutureDepWarning: |
|
14
|
|
|
def setup(self): |
|
15
|
|
|
# Initialize the routine to be tested |
|
16
|
|
|
self.test_routine = None |
|
17
|
|
|
self.test_args = [] |
|
18
|
|
|
self.test_kwargs = {} |
|
19
|
|
|
|
|
20
|
|
|
def teardown(self): |
|
21
|
|
|
del self.test_routine, self.test_args, self.test_kwargs |
|
22
|
|
|
|
|
23
|
|
|
def test_future_dep_warning(self): |
|
24
|
|
|
"""Test the implementation of FutureWarning for deprecated routines""" |
|
25
|
|
|
if self.test_routine is None: |
|
26
|
|
|
assert True |
|
27
|
|
|
else: |
|
28
|
|
|
with warnings.catch_warnings(record=True) as wout: |
|
29
|
|
|
# Cause all warnings to always be triggered. |
|
30
|
|
|
warnings.simplefilter("always") |
|
31
|
|
|
|
|
32
|
|
|
# Trigger a warning. |
|
33
|
|
|
self.test_routine(*self.test_args, **self.test_kwargs) |
|
34
|
|
|
|
|
35
|
|
|
# Verify some things |
|
36
|
|
|
assert len(wout) == 1 |
|
37
|
|
|
assert issubclass(wout[-1].category, FutureWarning) |
|
38
|
|
|
assert "Deprecated routine" in str(wout[-1].message) |
|
39
|
|
|
|
|
40
|
|
|
|
|
41
|
|
|
class TestDepAACGMV2Warning(TestFutureDepWarning): |
|
42
|
|
|
def setup(self): |
|
43
|
|
|
self.dtime = dt.datetime(2015, 1, 1, 0, 0, 0) |
|
44
|
|
|
self.test_routine = None |
|
45
|
|
|
self.test_args = [] |
|
46
|
|
|
self.test_kwargs = {} |
|
47
|
|
|
|
|
48
|
|
|
def teardown(self): |
|
49
|
|
|
del self.dtime, self.test_routine, self.test_args, self.test_kwargs |
|
50
|
|
|
|
|
51
|
|
|
def test_convert_warning(self): |
|
52
|
|
|
"""Test future deprecation warning for convert""" |
|
53
|
|
|
|
|
54
|
|
|
self.test_routine = aacgmv2.deprecated.convert |
|
55
|
|
|
self.test_args = [60, 0, 300, self.dtime] |
|
56
|
|
|
self.test_future_dep_warning() |
|
57
|
|
|
|
|
58
|
|
|
def test_subsol_warning(self): |
|
59
|
|
|
"""Test future deprecation warning for subsol""" |
|
60
|
|
|
|
|
61
|
|
|
self.test_routine = aacgmv2.deprecated.subsol |
|
62
|
|
|
self.test_args = [self.dtime.year, int(self.dtime.strftime("%j")), |
|
63
|
|
|
self.dtime.second + self.dtime.minute * 60 + |
|
64
|
|
|
self.dtime.hour * 3600] |
|
65
|
|
|
self.test_future_dep_warning() |
|
66
|
|
|
|
|
67
|
|
|
def test_gc2gd_lat_warning(self): |
|
68
|
|
|
"""Test future deprecation warning for gc2gd_lat""" |
|
69
|
|
|
|
|
70
|
|
|
self.test_routine = aacgmv2.deprecated.gc2gd_lat |
|
71
|
|
|
self.test_args = [60.0] |
|
72
|
|
|
self.test_future_dep_warning() |
|
73
|
|
|
|
|
74
|
|
|
def test_igrf_dipole_axis_warning(self): |
|
75
|
|
|
"""Test future deprecation warning for igrf_dipole_axis""" |
|
76
|
|
|
|
|
77
|
|
|
self.test_routine = aacgmv2.deprecated.igrf_dipole_axis |
|
78
|
|
|
self.test_args = [self.dtime] |
|
79
|
|
|
self.test_future_dep_warning() |
|
80
|
|
|
|
|
81
|
|
|
|
|
82
|
|
|
class TestDepLogging: |
|
83
|
|
|
def setup(self): |
|
84
|
|
|
"""Runs before every method to create a clean testing setup""" |
|
85
|
|
|
|
|
86
|
|
|
self.log_capture = StringIO() |
|
87
|
|
|
aacgmv2.logger.addHandler(logging.StreamHandler(self.log_capture)) |
|
88
|
|
|
|
|
89
|
|
|
self.in_convert = ([60], [0], [-1], dt.datetime(2015, 1, 1, 0, 0, 0)) |
|
90
|
|
|
self.lwarn = u"conversion not intended for altitudes < 0 km" |
|
91
|
|
|
self.lout = '' |
|
92
|
|
|
|
|
93
|
|
|
def teardown(self): |
|
94
|
|
|
"""Runs after every method to clean up previous testing""" |
|
95
|
|
|
self.log_capture.close() |
|
96
|
|
|
del self.in_convert, self.lwarn, self.lout |
|
97
|
|
|
|
|
98
|
|
|
def test_warning_below_ground_convert(self): |
|
99
|
|
|
""" Test that a warning is issued if altitude is below zero""" |
|
100
|
|
|
|
|
101
|
|
|
with warnings.catch_warnings(): |
|
102
|
|
|
# Cause all warnings to be ignored |
|
103
|
|
|
warnings.simplefilter("ignore") |
|
104
|
|
|
|
|
105
|
|
|
# Trigger the below ground warning |
|
106
|
|
|
aacgmv2.convert(*self.in_convert) |
|
107
|
|
|
|
|
108
|
|
|
# Test the logging output |
|
109
|
|
|
self.lout = self.log_capture.getvalue() |
|
110
|
|
|
assert self.lout.find(self.lwarn) >= 0 |
|
111
|
|
|
|
|
112
|
|
|
|
|
113
|
|
|
class TestDepAACGMV2: |
|
114
|
|
|
def setup(self): |
|
115
|
|
|
"""Runs before every method to create a clean testing setup""" |
|
116
|
|
|
self.dtime = dt.datetime(2015, 1, 1, 0, 0, 0) |
|
117
|
|
|
self.ddate = dt.date(2015, 1, 1) |
|
118
|
|
|
self.lat = None |
|
119
|
|
|
self.lon = None |
|
120
|
|
|
|
|
121
|
|
|
def teardown(self): |
|
122
|
|
|
"""Runs after every method to clean up previous testing""" |
|
123
|
|
|
del self.dtime, self.ddate, self.lat, self.lon |
|
124
|
|
|
|
|
125
|
|
View Code Duplication |
def test_convert_single_val(self): |
|
|
|
|
|
|
126
|
|
|
"""Test conversion for a single value""" |
|
127
|
|
|
with warnings.catch_warnings(): |
|
128
|
|
|
warnings.simplefilter("ignore") |
|
129
|
|
|
self.lat, self.lon = aacgmv2.convert(60, 0, 300, self.dtime) |
|
130
|
|
|
|
|
131
|
|
|
assert isinstance(self.lat, list) |
|
132
|
|
|
assert isinstance(self.lon, list) |
|
133
|
|
|
assert len(self.lat) == len(self.lon) and len(self.lat) == 1 |
|
134
|
|
|
np.testing.assert_allclose(self.lat, [58.2258], rtol=1e-4) |
|
135
|
|
|
np.testing.assert_allclose(self.lon, [81.1685], rtol=1e-4) |
|
136
|
|
|
|
|
137
|
|
|
def test_convert_list(self): |
|
138
|
|
|
"""Test conversion for list input""" |
|
139
|
|
|
with warnings.catch_warnings(): |
|
140
|
|
|
warnings.simplefilter("ignore") |
|
141
|
|
|
self.lat, self.lon = aacgmv2.convert([60], [0], [300], self.dtime) |
|
142
|
|
|
|
|
143
|
|
|
assert isinstance(self.lat, list) |
|
144
|
|
|
assert isinstance(self.lon, list) |
|
145
|
|
|
assert len(self.lat) == len(self.lon) and len(self.lat) == 1 |
|
146
|
|
|
np.testing.assert_allclose(self.lat, [58.2258], rtol=1e-4) |
|
147
|
|
|
np.testing.assert_allclose(self.lon, [81.1685], rtol=1e-4) |
|
148
|
|
|
|
|
149
|
|
|
with warnings.catch_warnings(): |
|
150
|
|
|
warnings.simplefilter("ignore") |
|
151
|
|
|
self.lat, self.lon = aacgmv2.convert([60, 61], [0, 0], [300, 300], |
|
152
|
|
|
self.dtime) |
|
153
|
|
|
|
|
154
|
|
|
assert isinstance(self.lat, list) |
|
155
|
|
|
assert isinstance(self.lon, list) |
|
156
|
|
|
assert len(self.lat) == len(self.lon) and len(self.lat) == 2 |
|
157
|
|
|
np.testing.assert_allclose(self.lat, [58.2258, 59.3186], rtol=1e-4) |
|
158
|
|
|
np.testing.assert_allclose(self.lon, [81.1685, 81.6140], rtol=1e-4) |
|
159
|
|
|
|
|
160
|
|
|
def test_convert_arr_single(self): |
|
161
|
|
|
"""Test conversion for array input with one element""" |
|
162
|
|
|
with warnings.catch_warnings(): |
|
163
|
|
|
warnings.simplefilter("ignore") |
|
164
|
|
|
self.lat, self.lon = aacgmv2.convert(np.array([60]), np.array([0]), |
|
165
|
|
|
np.array([300]), self.dtime) |
|
166
|
|
|
|
|
167
|
|
|
assert isinstance(self.lat, list) |
|
168
|
|
|
assert isinstance(self.lon, list) |
|
169
|
|
|
assert len(self.lat) == len(self.lon) and len(self.lat) == 1 |
|
170
|
|
|
np.testing.assert_allclose(self.lat, [58.2258], rtol=1e-4) |
|
171
|
|
|
np.testing.assert_allclose(self.lon, [81.1685], rtol=1e-4) |
|
172
|
|
|
|
|
173
|
|
|
def test_convert_arr(self): |
|
174
|
|
|
"""Test conversion for array input""" |
|
175
|
|
|
|
|
176
|
|
|
with warnings.catch_warnings(): |
|
177
|
|
|
warnings.simplefilter("ignore") |
|
178
|
|
|
self.lat, self.lon = aacgmv2.convert(np.array([60, 61]), |
|
179
|
|
|
np.array([0, 0]), |
|
180
|
|
|
np.array([300, 300]), |
|
181
|
|
|
self.dtime) |
|
182
|
|
|
|
|
183
|
|
|
assert isinstance(self.lat, list) |
|
184
|
|
|
assert isinstance(self.lon, list) |
|
185
|
|
|
assert len(self.lat) == len(self.lon) and len(self.lat) == 2 |
|
186
|
|
|
np.testing.assert_allclose(self.lat, [58.2258, 59.3186], rtol=1e-4) |
|
187
|
|
|
np.testing.assert_allclose(self.lon, [81.1685, 81.6140], rtol=1e-4) |
|
188
|
|
|
|
|
189
|
|
View Code Duplication |
def test_convert_list_mix(self): |
|
|
|
|
|
|
190
|
|
|
"""Test conversion for a list and floats""" |
|
191
|
|
|
|
|
192
|
|
|
with warnings.catch_warnings(): |
|
193
|
|
|
warnings.simplefilter("ignore") |
|
194
|
|
|
self.lat, self.lon = aacgmv2.convert([60, 61], 0, 300, self.dtime) |
|
195
|
|
|
|
|
196
|
|
|
assert isinstance(self.lat, list) |
|
197
|
|
|
assert isinstance(self.lon, list) |
|
198
|
|
|
assert len(self.lat) == len(self.lon) and len(self.lat) == 2 |
|
199
|
|
|
np.testing.assert_allclose(self.lat, [58.2258, 59.3186], rtol=1e-4) |
|
200
|
|
|
np.testing.assert_allclose(self.lon, [81.1685, 81.6140], rtol=1e-4) |
|
201
|
|
|
|
|
202
|
|
View Code Duplication |
def test_convert_arr_mix(self): |
|
|
|
|
|
|
203
|
|
|
"""Test conversion for an array and floats""" |
|
204
|
|
|
with warnings.catch_warnings(): |
|
205
|
|
|
warnings.simplefilter("ignore") |
|
206
|
|
|
self.lat, self.lon = aacgmv2.convert(np.array([60, 61]), 0, 300, |
|
207
|
|
|
self.dtime) |
|
208
|
|
|
|
|
209
|
|
|
assert isinstance(self.lat, list) |
|
210
|
|
|
assert isinstance(self.lon, list) |
|
211
|
|
|
assert len(self.lat) == len(self.lon) and len(self.lat) == 2 |
|
212
|
|
|
np.testing.assert_allclose(self.lat, [58.2258, 59.3186], rtol=1e-4) |
|
213
|
|
|
np.testing.assert_allclose(self.lon, [81.1685, 81.6140], rtol=1e-4) |
|
214
|
|
|
|
|
215
|
|
|
def test_convert_mult_array_failure(self): |
|
216
|
|
|
"""Test conversion for a multi-dim array and floats""" |
|
217
|
|
|
with warnings.catch_warnings(): |
|
218
|
|
|
warnings.simplefilter("ignore") |
|
219
|
|
|
with pytest.raises(ValueError): |
|
220
|
|
|
self.lat, self.lon = aacgmv2.convert(np.array([[60, 61, 62], |
|
221
|
|
|
[63, 64, 65]]), |
|
222
|
|
|
0, 300, self.dtime) |
|
223
|
|
|
|
|
224
|
|
|
def test_convert_location_failure(self): |
|
225
|
|
|
"""Test conversion with a bad location""" |
|
226
|
|
|
with warnings.catch_warnings(): |
|
227
|
|
|
warnings.simplefilter("ignore") |
|
228
|
|
|
self.lat, self.lon = aacgmv2.convert([0], [0], [0], self.dtime) |
|
229
|
|
|
|
|
230
|
|
|
assert isinstance(self.lat, list) |
|
231
|
|
|
assert isinstance(self.lon, list) |
|
232
|
|
|
assert len(self.lat) == len(self.lon) and len(self.lat) == 1 |
|
233
|
|
|
assert np.all([np.isinf(self.lat), np.isinf(self.lon)]) |
|
234
|
|
|
|
|
235
|
|
|
def test_convert_time_failure(self): |
|
236
|
|
|
"""Test conversion with a bad time""" |
|
237
|
|
|
with pytest.raises(ValueError): |
|
238
|
|
|
with warnings.catch_warnings(): |
|
239
|
|
|
warnings.simplefilter("ignore") |
|
240
|
|
|
self.lat, self.lon = aacgmv2.convert([60], [0], [300], None) |
|
241
|
|
|
|
|
242
|
|
|
def test_convert_datetime_date(self): |
|
243
|
|
|
"""Test conversion with date and datetime input""" |
|
244
|
|
|
with warnings.catch_warnings(): |
|
245
|
|
|
warnings.simplefilter("ignore") |
|
246
|
|
|
self.lat, self.lon = aacgmv2.convert([60], [0], [300], self.ddate) |
|
247
|
|
|
|
|
248
|
|
|
np.testing.assert_allclose(self.lat, [58.2258], rtol=1e-4) |
|
249
|
|
|
np.testing.assert_allclose(self.lon, [81.1685], rtol=1e-4) |
|
250
|
|
|
|
|
251
|
|
|
def test_convert_maxalt_failure(self): |
|
252
|
|
|
"""For an array, test failure for an altitude too high for |
|
253
|
|
|
coefficients""" |
|
254
|
|
|
with pytest.raises(ValueError): |
|
255
|
|
|
with warnings.catch_warnings(): |
|
256
|
|
|
warnings.simplefilter("ignore") |
|
257
|
|
|
aacgmv2.convert([60], [0], [2001], self.dtime) |
|
258
|
|
|
|
|
259
|
|
|
def test_convert_lat_failure(self): |
|
260
|
|
|
"""Test error return for co-latitudes above 90 for an array""" |
|
261
|
|
|
with pytest.raises(ValueError): |
|
262
|
|
|
with warnings.catch_warnings(): |
|
263
|
|
|
warnings.simplefilter("ignore") |
|
264
|
|
|
aacgmv2.convert([91, 60, -91], 0, 300, self.dtime) |
|
265
|
|
|
|
|
266
|
|
|
def test_subsol(self): |
|
267
|
|
|
"""Test the subsolar calculation""" |
|
268
|
|
|
doy = int(self.dtime.strftime("%j")) |
|
269
|
|
|
ut = self.dtime.hour * 3600.0 + self.dtime.minute * 60.0 + \ |
|
270
|
|
|
self.dtime.second |
|
271
|
|
|
with warnings.catch_warnings(): |
|
272
|
|
|
warnings.simplefilter("ignore") |
|
273
|
|
|
self.lon, self.lat = aacgmv2.deprecated.subsol(self.dtime.year, |
|
274
|
|
|
doy, ut) |
|
275
|
|
|
|
|
276
|
|
|
np.testing.assert_almost_equal(self.lon, -179.2004, decimal=4) |
|
277
|
|
|
np.testing.assert_almost_equal(self.lat, -23.0431, decimal=4) |
|
278
|
|
|
|
|
279
|
|
|
def test_gc2gd_lat(self): |
|
280
|
|
|
"""Test the geocentric to geodetic conversion""" |
|
281
|
|
|
with warnings.catch_warnings(): |
|
282
|
|
|
warnings.simplefilter("ignore") |
|
283
|
|
|
self.lat = aacgmv2.deprecated.gc2gd_lat(45.0) |
|
284
|
|
|
|
|
285
|
|
|
np.testing.assert_almost_equal(self.lat, 45.1924, decimal=4) |
|
286
|
|
|
|
|
287
|
|
|
def test_gc2gd_lat_list(self): |
|
288
|
|
|
"""Test the geocentric to geodetic conversion""" |
|
289
|
|
|
self.lat = [45.0, -45.0] |
|
290
|
|
|
with warnings.catch_warnings(): |
|
291
|
|
|
warnings.simplefilter("ignore") |
|
292
|
|
|
self.lat = aacgmv2.deprecated.gc2gd_lat(self.lat) |
|
293
|
|
|
|
|
294
|
|
|
np.testing.assert_allclose(self.lat, [45.1924, -45.1924], rtol=1.0e-4) |
|
295
|
|
|
|
|
296
|
|
|
def test_gc2gd_lat_arr(self): |
|
297
|
|
|
"""Test the geocentric to geodetic conversion""" |
|
298
|
|
|
self.lat = np.array([45.0, -45.0]) |
|
299
|
|
|
with warnings.catch_warnings(): |
|
300
|
|
|
warnings.simplefilter("ignore") |
|
301
|
|
|
self.lat = aacgmv2.deprecated.gc2gd_lat(self.lat) |
|
302
|
|
|
|
|
303
|
|
|
np.testing.assert_allclose(self.lat, [45.1924, -45.1924], rtol=1.0e-4) |
|
304
|
|
|
|
|
305
|
|
|
def test_igrf_dipole_axis(self): |
|
306
|
|
|
"""Test the IGRF dipole axis calculation""" |
|
307
|
|
|
with warnings.catch_warnings(): |
|
308
|
|
|
warnings.simplefilter("ignore") |
|
309
|
|
|
m = aacgmv2.deprecated.igrf_dipole_axis(self.dtime) |
|
310
|
|
|
|
|
311
|
|
|
np.testing.assert_allclose(m, [0.050253, -0.160608, 0.985738], |
|
312
|
|
|
rtol=1.0e-4) |
|
313
|
|
|
|