test_dataset.test_lst()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 43
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 34
dl 0
loc 43
rs 9.064
c 0
b 0
f 0
cc 1
nop 3
1
# -*- coding: utf-8 -*-
2
# vim:fileencoding=utf-8
3
import datetime as dt
4
import numpy as np
5
import pytest
6
7
try:
8
	from nrlmsise00.dataset import msise_4d
9
except ImportError:
10
	pytest.skip("`nrlmsise00.dataset` not installed.", allow_module_level=True)
11
12
13
@pytest.mark.parametrize(
14
	"date", [
15
		[dt.datetime(2009, 6, 21, 8, 3, 20), dt.datetime(2009, 12, 21, 16, 3, 20)],
16
		["2009-06-21 08:03:20", "2009-12-21 16:03:20"],
17
		["2009-06-21 08:03:20+01:00", "2009-12-21 16:03:20-01:00"],
18
	]
19
)
20
@pytest.mark.parametrize(
21
	"ap, lon, lst", [
22
		(None, -70., None),
23
		(4., -70., 16.),
24
		([5., 6.], -70., [6., 18.]),
25
		(None, [-70, 0., 70.], None),
26
		(None, [-70, 0., 70.], [5., 7., 12.]),
27
		(None, [-70, 0., 70.], [[5., 17.], [6., 18.], [7., 19.]]),
28
		(None, [-70, 0., 70.], [[5., 6., 7.], [17., 18., 19.]]),
29
	]
30
)
31
def test_dataset(date, ap, lon, lst):
32
	# checks that broadcasting works as intended
33
	ds = msise_4d(
34
		date,
35
		[400, 200, 100],  # alt
36
		[60, 30, 0, -30, -60],  # g_lat
37
		lon,    # g_long
38
		150,    # f107A
39
		ap=ap,  # ap
40
		lst=lst,
41
	)
42
	assert ds
43
44
45
@pytest.mark.parametrize(
46
	"lon, lst, lst_expected", [
47
		(
48
			0,
49
			None,
50
			np.array([8., 16.]).reshape(2, 1),
51
		),
52
		(
53
			[30, 150],
54
			None,
55
			np.array([[10., 18.], [18., 26.]]).reshape(2, 2),
56
		),
57
		(
58
			[30, 120, 150],
59
			None,
60
			np.array([[10., 16., 18.], [18., 24., 26.]]),
61
		),
62
		(
63
			[30, 120, 150],
64
			[10., 16.],  # same shape as "time"
65
			np.array([[10., 10., 10.], [16., 16., 16.]]),
66
		),
67
		(
68
			[30, 120, 150],
69
			[10., 16., 18.],  # same shape as "lon"
70
			np.array([[10., 16., 18.], [10., 16., 18.]]),
71
		),
72
	]
73
)
74
def test_lst(lon, lst, lst_expected):
75
	# checks that local time works as intended
76
	ds = msise_4d(
77
		[dt.datetime(2009, 6, 21, 8), dt.datetime(2009, 12, 21, 16)],
78
		[400, 200, 100],  # alt
79
		[60, 30, 0, -30, -60],  # g_lat
80
		lon,    # g_long
81
		150,    # f107A
82
		150,    # f107
83
		4,      # ap
84
		lst=lst,
85
	)
86
	assert ds
87
	np.testing.assert_allclose(ds.lst.values, lst_expected)
88
89
90
def test_nd_raise():
91
	with pytest.raises(ValueError, match=r"Only scalars and up to 1-D .*"):
92
		msise_4d(
93
			dt.datetime(2009, 6, 21, 8),
94
			# 2-d should fail
95
			[[200]],  # alt
96
			60,   # g_lat
97
			-70,  # g_long
98
			150,  # f107A
99
			150,  # f107
100
			4,    # ap
101
		)
102