|
1
|
|
|
# -*- coding: utf-8 -*- |
|
2
|
|
|
import numpy as np |
|
3
|
|
|
import pytest |
|
4
|
|
|
|
|
5
|
|
|
xr = pytest.importorskip( |
|
6
|
|
|
"xarray", |
|
7
|
|
|
reason="`xarray` is needed, it should be installed with the `models` extra." |
|
8
|
|
|
) |
|
9
|
|
|
aurmod = pytest.importorskip( |
|
10
|
|
|
"eppaurora.models", reason="Could not import the `models` submodule." |
|
11
|
|
|
) |
|
12
|
|
|
|
|
13
|
|
|
COEFF_DS = xr.Dataset( |
|
14
|
|
|
data_vars={ |
|
15
|
|
|
"beta": ( |
|
16
|
|
|
["altitude", "latitude", "mlt", "proxy"], |
|
17
|
|
|
np.tile( |
|
18
|
|
|
[[[[1., 2., 3., 4., 5., 6.]]]], |
|
19
|
|
|
(1, 2, 2, 1), |
|
20
|
|
|
), |
|
21
|
|
|
), |
|
22
|
|
|
"beta_std": ( |
|
23
|
|
|
["altitude", "latitude", "mlt", "proxy"], |
|
24
|
|
|
np.tile( |
|
25
|
|
|
[[[[1., 2., 3., 4., 5., 6.]]]], |
|
26
|
|
|
(1, 2, 2, 1), |
|
27
|
|
|
), |
|
28
|
|
|
), |
|
29
|
|
|
}, |
|
30
|
|
|
coords={ |
|
31
|
|
|
"altitude": [100.], |
|
32
|
|
|
"latitude": [70.2, 73.8], |
|
33
|
|
|
"mlt": [3.0, 5.0], |
|
34
|
|
|
"proxy": ["Kp", "PC", "Ap", "log_f107_81ctr_obs", "log_v_plasma", "offset"], |
|
35
|
|
|
}, |
|
36
|
|
|
) |
|
37
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
def test_ssusiq2023(): |
|
40
|
|
|
res = aurmod.ssusiq2023( |
|
41
|
|
|
70.2, 3, 100., [2.333, 1, 20, 2, 2], coeff_ds=COEFF_DS, |
|
42
|
|
|
) |
|
43
|
|
|
np.testing.assert_allclose(res, [2.333 + 2. + 60. + 8. + 10. + 6.]) |
|
44
|
|
|
res = aurmod.ssusiq2023( |
|
45
|
|
|
70.2, 3, 100., [2.333, 1, 20, 2, 2], coeff_ds=COEFF_DS, return_var=True, |
|
46
|
|
|
) |
|
47
|
|
|
np.testing.assert_allclose( |
|
48
|
|
|
res[1], [2.333**2 + 2.**2 + 60.**2 + 8.**2 + 10.**2 + 6.**2] |
|
49
|
|
|
) |
|
50
|
|
|
res = aurmod.ssusiq2023( |
|
51
|
|
|
70.2, 3, 100., |
|
52
|
|
|
[ |
|
53
|
|
|
[2.333, 1, 20, 2, 2], |
|
54
|
|
|
[3.333, 2, 50, 4, 3] |
|
55
|
|
|
], |
|
56
|
|
|
coeff_ds=COEFF_DS, |
|
57
|
|
|
return_var=True, |
|
58
|
|
|
) |
|
59
|
|
|
np.testing.assert_allclose( |
|
60
|
|
|
res[0], |
|
61
|
|
|
np.array([ |
|
62
|
|
|
2.333 + 2. + 60. + 8. + 10. + 6., |
|
63
|
|
|
3.333 + 4. + 150. + 16. + 15. + 6., |
|
64
|
|
|
]) |
|
65
|
|
|
) |
|
66
|
|
|
np.testing.assert_allclose( |
|
67
|
|
|
res[1], |
|
68
|
|
|
np.array([ |
|
69
|
|
|
2.333**2 + 2.**2 + 60.**2 + 8.**2 + 10.**2 + 6.**2, |
|
70
|
|
|
3.333**2 + 4.**2 + 150.**2 + 16.**2 + 15.**2 + 6.**2, |
|
71
|
|
|
]) |
|
72
|
|
|
) |
|
73
|
|
|
res = aurmod.ssusiq2023( |
|
74
|
|
|
70.2, 3, 100., |
|
75
|
|
|
[ |
|
76
|
|
|
[2.333, 3.333], [1, 2], [20, 50], [2, 4], [2, 3] |
|
77
|
|
|
], |
|
78
|
|
|
coeff_ds=COEFF_DS, |
|
79
|
|
|
return_var=True, |
|
80
|
|
|
) |
|
81
|
|
|
np.testing.assert_allclose( |
|
82
|
|
|
res[0], |
|
83
|
|
|
np.array([ |
|
84
|
|
|
2.333 + 2. + 60. + 8. + 10. + 6., |
|
85
|
|
|
3.333 + 4. + 150. + 16. + 15. + 6., |
|
86
|
|
|
]) |
|
87
|
|
|
) |
|
88
|
|
|
|
|
89
|
|
|
|
|
90
|
|
|
def test_ssusiq2023_xrda_2d(): |
|
91
|
|
|
res = aurmod.ssusiq2023( |
|
92
|
|
|
70.2, 3, 100., |
|
93
|
|
|
xr.DataArray( |
|
94
|
|
|
[[2.333, 3.333], [1, 2], [20, 50], [2, 4], [2, 3]], |
|
95
|
|
|
dims=["proxy", "time"], |
|
96
|
|
|
coords={"proxy": ["Kp", "PC", "Ap", "log_f107_81ctr_obs", "log_v_plasma"]}, |
|
97
|
|
|
), |
|
98
|
|
|
coeff_ds=COEFF_DS, |
|
99
|
|
|
return_var=True, |
|
100
|
|
|
) |
|
101
|
|
|
assert res[0].shape == (2,) |
|
102
|
|
|
np.testing.assert_allclose( |
|
103
|
|
|
res[0], |
|
104
|
|
|
np.array([ |
|
105
|
|
|
2.333 + 2. + 60. + 8. + 10. + 6., |
|
106
|
|
|
3.333 + 4. + 150. + 16. + 15. + 6., |
|
107
|
|
|
]) |
|
108
|
|
|
) |
|
109
|
|
|
|
|
110
|
|
|
|
|
111
|
|
View Code Duplication |
@pytest.mark.parametrize( |
|
|
|
|
|
|
112
|
|
|
"interpolate, method", |
|
113
|
|
|
[ |
|
114
|
|
|
(False, None), |
|
115
|
|
|
(True, "linear"), |
|
116
|
|
|
pytest.param( |
|
117
|
|
|
True, "cubic", |
|
118
|
|
|
marks=pytest.mark.skipif( |
|
119
|
|
|
tuple(map(int, xr.__version__.split("."))) < (0, 16), |
|
120
|
|
|
reason="'cubic' not supported for N-D interpolation.", |
|
121
|
|
|
), |
|
122
|
|
|
), |
|
123
|
|
|
], |
|
124
|
|
|
) |
|
125
|
|
|
def test_ssusiq2023_xrda_3d(interpolate, method): |
|
126
|
|
|
res = aurmod.ssusiq2023( |
|
127
|
|
|
70.2, 3, 100., |
|
128
|
|
|
xr.DataArray( |
|
129
|
|
|
[[[2.333, 3.333], [1, 2], [20, 50], [2, 4], [2, 3]]], |
|
130
|
|
|
dims=["model", "proxy", "time"], |
|
131
|
|
|
coords={"proxy": ["Kp", "PC", "Ap", "log_f107_81ctr_obs", "log_v_plasma"]}, |
|
132
|
|
|
), |
|
133
|
|
|
interpolate=interpolate, |
|
134
|
|
|
method=method, |
|
135
|
|
|
return_var=True, |
|
136
|
|
|
) |
|
137
|
|
|
assert res[0].shape == (1, 2) |
|
138
|
|
|
|
|
139
|
|
|
|
|
140
|
|
View Code Duplication |
@pytest.mark.parametrize( |
|
|
|
|
|
|
141
|
|
|
"interpolate, method", |
|
142
|
|
|
[ |
|
143
|
|
|
(False, None), |
|
144
|
|
|
(True, "linear"), |
|
145
|
|
|
pytest.param( |
|
146
|
|
|
True, "cubic", |
|
147
|
|
|
marks=pytest.mark.skipif( |
|
148
|
|
|
tuple(map(int, xr.__version__.split("."))) < (0, 16), |
|
149
|
|
|
reason="'cubic' not supported for N-D interpolation.", |
|
150
|
|
|
), |
|
151
|
|
|
), |
|
152
|
|
|
], |
|
153
|
|
|
) |
|
154
|
|
|
def test_ssusiq2023_vec(interpolate, method): |
|
155
|
|
|
res = aurmod.ssusiq2023( |
|
156
|
|
|
[66.6, 70.2], [3, 5, 7], [100., 105., 110., 115.], |
|
157
|
|
|
[[2.333], [1], [20], [2]], |
|
158
|
|
|
interpolate=interpolate, |
|
159
|
|
|
method=method, |
|
160
|
|
|
return_var=True, |
|
161
|
|
|
) |
|
162
|
|
|
assert res[0].shape == (3, 2, 4, 1) |
|
163
|
|
|
|