1 | # -*- coding: utf-8 -*- |
||
2 | # vim:fileencoding=utf-8 |
||
3 | # |
||
4 | # Copyright (c) 2020 Stefan Bender |
||
5 | # |
||
6 | # This module is part of pyspaceweather. |
||
7 | # pyspaceweather is free software: you can redistribute it or modify |
||
8 | # it under the terms of the GNU General Public License as published |
||
9 | # by the Free Software Foundation, version 2. |
||
10 | # See accompanying COPYING.GPLv2 file or http://www.gnu.org/licenses/gpl-2.0.html. |
||
11 | """Space weather index read tests |
||
12 | """ |
||
13 | import os |
||
14 | |||
15 | import numpy as np |
||
16 | import pandas as pd |
||
17 | import requests |
||
18 | |||
19 | import pytest |
||
20 | |||
21 | from spaceweather import ( |
||
22 | ap_kp_3h, sw_daily, get_file_age, update_data, |
||
23 | SW_PATH_ALL, SW_PATH_5Y, |
||
24 | ) |
||
25 | from spaceweather.celestrak import DL_URL_5Y |
||
26 | |||
27 | |||
28 | @pytest.fixture(scope="module") |
||
29 | def df_3h(): |
||
30 | return ap_kp_3h() |
||
31 | |||
32 | |||
33 | def test_age(): |
||
34 | now = pd.Timestamp.utcnow() |
||
35 | for p in [SW_PATH_ALL, SW_PATH_5Y]: |
||
36 | assert os.path.exists(p) |
||
37 | fage0 = get_file_age(p) |
||
38 | fage1 = now - get_file_age(p, relative=False) |
||
39 | assert (fage0 > pd.Timedelta("3h")) == (fage1 > pd.Timedelta("3h")) |
||
40 | assert (fage0 > pd.Timedelta("1d")) == (fage1 > pd.Timedelta("1d")) |
||
41 | |||
42 | |||
43 | def _assert_age(p, age): |
||
44 | assert os.path.exists(p) |
||
45 | fage = get_file_age(p) |
||
46 | assert fage < pd.Timedelta(age) |
||
47 | |||
48 | |||
49 | def test_update(): |
||
50 | update_data(min_age="100d") |
||
51 | for (p, age) in zip([SW_PATH_ALL, SW_PATH_5Y], ["1460d", "100d"]): |
||
52 | _assert_age(p, age) |
||
53 | |||
54 | |||
55 | def test_auto_update(mocker, tmpdir): |
||
56 | # test with non-existent file |
||
57 | mocker.patch("requests.get") |
||
58 | tmpdir = str(tmpdir) |
||
59 | update_data(swpath_5y=os.path.join(tmpdir, "foo.dat")) |
||
60 | requests.get.assert_called_with(DL_URL_5Y, stream=True) |
||
61 | # Should update the last-5-year data |
||
62 | sw_daily(update=True, update_interval="1d") |
||
63 | requests.get.assert_called_with(DL_URL_5Y, stream=True) |
||
64 | _assert_age(SW_PATH_5Y, "100d") |
||
65 | with pytest.warns(UserWarning): |
||
66 | sw_daily(update=False, update_interval="0h") |
||
67 | |||
68 | |||
69 | def test_not_avail(mocker, tmpdir): |
||
70 | # test with non-existent file |
||
71 | mocker.patch("requests.get") |
||
72 | tmpdir = str(tmpdir) |
||
73 | tmpfile = os.path.join(tmpdir, "foo.dat") |
||
74 | # daily |
||
75 | with pytest.raises(IOError): |
||
76 | with pytest.warns(UserWarning): |
||
77 | sw_daily(update=False, update_interval="0h", swpath_5y=tmpfile) |
||
78 | # 3h data |
||
79 | with pytest.raises(IOError): |
||
80 | with pytest.warns(UserWarning): |
||
81 | ap_kp_3h(update=False, update_interval="0h", swpath_5y=tmpfile) |
||
82 | |||
83 | |||
84 | def test_daily(): |
||
85 | df = sw_daily() |
||
86 | np.testing.assert_allclose( |
||
87 | df.loc["2000-01-01"].values, |
||
88 | np.array([ |
||
89 | 2.000e+03, 1.000e+00, 1.000e+00, 2.272e+03, 7.000e+00, 5.300e+00, 4.700e+00, |
||
90 | 4.000e+00, 3.300e+00, 4.300e+00, 3.000e+00, 4.300e+00, 3.700e+00, 3.270e+01, |
||
91 | 5.600e+01, 3.900e+01, 2.700e+01, 1.800e+01, 3.200e+01, 1.500e+01, 3.200e+01, |
||
92 | 2.200e+01, 3.000e+01, 1.300e+00, 6.000e+00, 4.800e+01, 1.256e+02, 0.000e+00, |
||
93 | 1.605e+02, 1.750e+02, 1.299e+02, 1.656e+02, 1.790e+02, |
||
94 | ]), |
||
95 | rtol=1e-12, |
||
96 | ) |
||
97 | |||
98 | |||
99 | View Code Duplication | @pytest.mark.parametrize( |
|
0 ignored issues
–
show
Duplication
introduced
by
![]() |
|||
100 | "name, result", |
||
101 | [ |
||
102 | ("Ap", np.array([56, 39, 27, 18, 32, 15, 32, 22])), |
||
103 | ("Kp", np.array([5.3, 4.7, 4.0, 3.3, 4.3, 3.0, 4.3, 3.7])), |
||
104 | ] |
||
105 | ) |
||
106 | def test_3hourly_index(name, result, df_3h): |
||
107 | df = df_3h |
||
108 | np.testing.assert_allclose( |
||
109 | df.loc[ |
||
110 | pd.date_range( |
||
111 | "2000-01-01 01:30", "2000-01-01 23:30", freq="3h" |
||
112 | ) |
||
113 | ][name].values, |
||
114 | result, |
||
115 | rtol=1e-12, |
||
116 | ) |
||
117 |