Passed
Push — master ( de2059...5c9f2d )
by Stefan
01:18
created

test_read.test_age()   A

Complexity

Conditions 2

Size

Total Lines 8
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
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
18
from spaceweather import (
19
	ap_kp_3h, sw_daily, get_file_age, update_data,
20
	SW_PATH_ALL, SW_PATH_5Y,
21
)
22
23
24
def test_age():
25
	now = pd.Timestamp.utcnow()
26
	for p in [SW_PATH_ALL, SW_PATH_5Y]:
27
		assert os.path.exists(p)
28
		fage0 = get_file_age(p)
29
		fage1 = now - get_file_age(p, relative=False)
30
		assert (fage0 > pd.Timedelta("3h")) == (fage1 > pd.Timedelta("3h"))
31
		assert (fage0 > pd.Timedelta("1d")) == (fage1 > pd.Timedelta("1d"))
32
33
34
def test_update():
35
	update_data(min_age="100d")
36
	for p in [SW_PATH_ALL, SW_PATH_5Y]:
37
		assert os.path.exists(p)
38
		fage = get_file_age(p)
39
		assert fage < pd.Timedelta("100d")
40
41
42
def test_daily():
43
	df = sw_daily()
44
	np.testing.assert_allclose(
45
		df.loc["2000-01-01"].values,
46
		np.array([
47
			2.000e+03, 1.000e+00, 1.000e+00, 2.272e+03, 7.000e+00, 5.300e+00, 4.700e+00,
48
			4.000e+00, 3.300e+00, 4.300e+00, 3.000e+00, 4.300e+00, 3.700e+00, 3.270e+01,
49
			5.600e+01, 3.900e+01, 2.700e+01, 1.800e+01, 3.200e+01, 1.500e+01, 3.200e+01,
50
			2.200e+01, 3.000e+01, 1.300e+00, 6.000e+00, 4.800e+01, 1.256e+02, 0.000e+00,
51
			1.605e+02, 1.750e+02, 1.299e+02, 1.656e+02, 1.790e+02,
52
		]),
53
		rtol=1e-12,
54
	)
55
56
57
def test_3hourly_ap():
58
	df = ap_kp_3h()
59
	np.testing.assert_allclose(
60
		df.loc[pd.date_range("2000-01-01 01:30", "2000-01-01 23:30", freq='3h')].Ap.values,
61
		np.array([56, 39, 27, 18, 32, 15, 32, 22]),
62
		rtol=1e-12,
63
	)
64
65
66
def test_3hourly_kp():
67
	df = ap_kp_3h()
68
	np.testing.assert_allclose(
69
		df.loc[pd.date_range("2000-01-01 01:30", "2000-01-01 23:30", freq='3h')].Kp.values,
70
		np.array([5.3, 4.7, 4.0, 3.3, 4.3, 3.0, 4.3, 3.7]),
71
		rtol=1e-12,
72
	)
73