1 | # -*- coding: utf-8 -*- |
||
2 | # vim:fileencoding=utf-8 |
||
3 | # |
||
4 | # Copyright (c) 2022 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 | """OMNI data read tests |
||
12 | """ |
||
13 | import os |
||
14 | import requests |
||
15 | from posixpath import join as urljoin |
||
16 | |||
17 | import numpy as np |
||
18 | import pandas as pd |
||
19 | |||
20 | import pytest |
||
21 | |||
22 | from spaceweather import cache_omnie, omnie_hourly, omnie_mask_missing, sw_daily |
||
23 | from spaceweather.omni import OMNI_URL_BASE, OMNI_PREFIX, OMNI_EXT |
||
24 | |||
25 | _TEST_YEAR = 2012 |
||
26 | _TEST_FILE = "{0}_{1:04d}.{2}".format(OMNI_PREFIX, _TEST_YEAR, OMNI_EXT) |
||
27 | _TEST_URL = urljoin(OMNI_URL_BASE, _TEST_FILE) |
||
28 | _TEST_PATH = os.path.join(".", "tests") |
||
29 | |||
30 | |||
31 | @pytest.fixture(scope="module") |
||
32 | def df_d(): |
||
33 | return sw_daily() |
||
34 | |||
35 | |||
36 | @pytest.fixture(scope="module") |
||
37 | def df_o(): |
||
38 | return omnie_hourly(2000, local_path=_TEST_PATH, prefix="omni2t") |
||
39 | |||
40 | |||
41 | def test_cache(mocker, tmpdir): |
||
42 | mocker.patch("requests.get") |
||
43 | tmpdir = str(tmpdir) |
||
44 | # Check non-existent file |
||
45 | cache_omnie(year=_TEST_YEAR, local_path=tmpdir) |
||
46 | requests.get.assert_called_once_with(_TEST_URL, stream=True) |
||
47 | # Check non-existent (sub)dir |
||
48 | tmppath = os.path.join(tmpdir, "data") |
||
49 | cache_omnie(year=_TEST_YEAR, local_path=tmppath) |
||
50 | requests.get.assert_called_with(_TEST_URL, stream=True) |
||
51 | |||
52 | |||
53 | def test_auto_update(mocker, tmpdir): |
||
54 | # test with non-existent file |
||
55 | mocker.patch("requests.get") |
||
56 | tmpdir = str(tmpdir) |
||
57 | with pytest.raises(IOError): |
||
58 | with pytest.warns(UserWarning): |
||
59 | omnie_hourly(year=_TEST_YEAR, cache=True, local_path=tmpdir) |
||
60 | requests.get.assert_called_once_with(_TEST_URL, stream=True) |
||
61 | |||
62 | |||
63 | def test_not_avail(mocker, tmpdir): |
||
64 | # test with non-existent file |
||
65 | tmpdir = str(tmpdir) |
||
66 | with pytest.raises(IOError): |
||
67 | with pytest.warns(UserWarning): |
||
68 | omnie_hourly(year=_TEST_YEAR, cache=False, local_path=tmpdir) |
||
69 | |||
70 | |||
71 | @pytest.mark.parametrize("hour", range(0, 24, 3)) |
||
72 | @pytest.mark.parametrize("index", ["Ap", "Kp"]) |
||
73 | def test_hourly(hour, index, df_d, df_o): |
||
74 | df1 = df_o |
||
75 | # The last row is for the missing value test. |
||
76 | df1 = df1.iloc[:-1] |
||
77 | df2 = df_d |
||
78 | ind_name = "{0}{1}".format(index, hour) |
||
79 | df1_ind = df1[df1["hour"] == hour][index] |
||
80 | df2_ind = df2[ind_name].loc[df1_ind.index.date].rename(index) |
||
81 | df2_ind.index = df1_ind.index |
||
82 | pd.testing.assert_series_equal(df1_ind, df2_ind) |
||
83 | |||
84 | |||
85 | View Code Duplication | @pytest.mark.parametrize( |
|
0 ignored issues
–
show
Duplication
introduced
by
![]() |
|||
86 | "name, result", |
||
87 | [ |
||
88 | ("Ap", np.array([56, 39, 27, 18, 32, 15, 32, 22])), |
||
89 | ("Kp", np.array([5.3, 4.7, 4.0, 3.3, 4.3, 3.0, 4.3, 3.7])), |
||
90 | ] |
||
91 | ) |
||
92 | def test_3hourly_index(name, result, df_o): |
||
93 | df = df_o |
||
94 | np.testing.assert_allclose( |
||
95 | df.loc[ |
||
96 | pd.date_range( |
||
97 | "2000-01-01 00:00", "2000-01-01 23:00", freq="3h" |
||
98 | ) |
||
99 | ][name].values, |
||
100 | result, |
||
101 | rtol=1e-12, |
||
102 | ) |
||
103 | |||
104 | |||
105 | def test_mask_missing(df_o): |
||
106 | df = df_o |
||
107 | dfp = omnie_mask_missing(df) |
||
108 | # The last row should contain all NaNs. |
||
109 | dfp = dfp.iloc[-1] |
||
110 | for v in filter( |
||
111 | lambda n: n not in ["year", "doy", "hour"], |
||
112 | dfp.index, |
||
113 | ): |
||
114 | assert np.isnan(dfp[v]) |
||
115 |