1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
import datetime as dt |
3
|
|
|
|
4
|
|
|
import numpy as np |
5
|
|
|
import pytest |
6
|
|
|
from pytest import approx |
7
|
|
|
from numpy.testing import assert_allclose |
8
|
|
|
|
9
|
|
|
from apexpy import helpers |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
############################################################################## |
13
|
|
|
# NOTE: whenever function outputs are tested against hard-coded numbers, # |
14
|
|
|
# the test results (numbers) were obtained by running the code that is # |
15
|
|
|
# tested. Therefore these tests below only check that nothing changes when # |
16
|
|
|
# refactoring etc., and not if the results are actually correct # |
17
|
|
|
############################################################################## |
18
|
|
|
|
19
|
|
|
|
20
|
|
|
# ============================================================================ |
21
|
|
|
# Test checklat |
22
|
|
|
# ============================================================================ |
23
|
|
|
|
24
|
|
|
def test_checklat_scalar(): |
25
|
|
|
assert helpers.checklat(90) == 90 |
26
|
|
|
assert helpers.checklat(0) == 0 |
27
|
|
|
assert helpers.checklat(-90) == -90 |
28
|
|
|
|
29
|
|
|
assert helpers.checklat(90+1e-5) == 90 |
30
|
|
|
assert helpers.checklat(-90-1e-5) == -90 |
31
|
|
|
|
32
|
|
|
assert type(helpers.checklat(0.)) == float |
33
|
|
|
assert type(helpers.checklat(0)) == int |
34
|
|
|
assert type(helpers.checklat(90+1e-5)) == int |
35
|
|
|
|
36
|
|
|
with pytest.raises(ValueError): |
37
|
|
|
helpers.checklat(90+1e-4) |
38
|
|
|
with pytest.raises(ValueError): |
39
|
|
|
helpers.checklat(-90-1e-4) |
40
|
|
|
|
41
|
|
|
|
42
|
|
|
def test_checklat_message(): |
43
|
|
|
with pytest.raises(ValueError) as excinfo: |
44
|
|
|
helpers.checklat(100) |
45
|
|
|
assert str(excinfo.value).startswith('lat must be in') |
46
|
|
|
with pytest.raises(ValueError) as excinfo: |
47
|
|
|
helpers.checklat(100, name='glat') |
48
|
|
|
assert str(excinfo.value).startswith('glat') |
49
|
|
|
|
50
|
|
|
|
51
|
|
|
def test_checklat_array(): |
52
|
|
|
assert_allclose(helpers.checklat([-90-1e-5, -90, 0, 90, 90+1e-5]), |
53
|
|
|
np.array([-90, -90, 0, 90, 90]), rtol=0, atol=1e-8) |
54
|
|
|
|
55
|
|
|
assert type(helpers.checklat([0])) == list |
56
|
|
|
assert type(helpers.checklat(np.array([0]))) == np.ndarray |
57
|
|
|
|
58
|
|
|
with pytest.raises(ValueError): |
59
|
|
|
helpers.checklat([-90-1e-4, -90, 0, 90, 90+1e-5]) |
60
|
|
|
|
61
|
|
|
with pytest.raises(ValueError): |
62
|
|
|
helpers.checklat([-90-1e-5, -90, 0, 90, 90+1e-4]) |
63
|
|
|
|
64
|
|
|
|
65
|
|
|
# ============================================================================ |
66
|
|
|
# Test getsinIm |
67
|
|
|
# ============================================================================ |
68
|
|
|
|
69
|
|
|
def test_getsinIm_scalar(): |
70
|
|
|
assert_allclose(helpers.getsinIm(60), 0.96076892283052284) |
71
|
|
|
assert_allclose(helpers.getsinIm(10), 0.33257924500670238) |
72
|
|
|
assert type(helpers.getsinIm(60)) != np.ndarray |
73
|
|
|
|
74
|
|
|
|
75
|
|
|
def test_getsinIm_1Darray(): |
76
|
|
|
assert_allclose(helpers.getsinIm([60, 10]), |
77
|
|
|
[0.96076892283052284, 0.33257924500670238]) |
78
|
|
|
|
79
|
|
|
|
80
|
|
|
def test_getsinIm_2Darray(): |
81
|
|
|
assert_allclose(helpers.getsinIm([[60, 10], [60, 10]]), |
82
|
|
|
[[0.96076892283052284, 0.33257924500670238], |
83
|
|
|
[0.96076892283052284, 0.33257924500670238]]) |
84
|
|
|
|
85
|
|
|
|
86
|
|
|
# ============================================================================ |
87
|
|
|
# Test getcosIm |
88
|
|
|
# ============================================================================ |
89
|
|
|
|
90
|
|
|
|
91
|
|
|
def test_getcosIm_scalar(): |
92
|
|
|
assert_allclose(helpers.getcosIm(60), 0.27735009811261463) |
93
|
|
|
assert_allclose(helpers.getcosIm(10), 0.94307531289434765) |
94
|
|
|
assert type(helpers.getcosIm(60)) != np.ndarray |
95
|
|
|
|
96
|
|
|
|
97
|
|
|
def test_getcosIm_1Darray(): |
98
|
|
|
assert_allclose(helpers.getcosIm([60, 10]), |
99
|
|
|
[0.27735009811261463, 0.94307531289434765]) |
100
|
|
|
|
101
|
|
|
|
102
|
|
|
def test_getcosIm_2Darray(): |
103
|
|
|
assert_allclose(helpers.getcosIm([[60, 10], [60, 10]]), |
104
|
|
|
[[0.27735009811261463, 0.94307531289434765], |
105
|
|
|
[0.27735009811261463, 0.94307531289434765]]) |
106
|
|
|
|
107
|
|
|
|
108
|
|
|
# ============================================================================ |
109
|
|
|
# Test toYearFraction |
110
|
|
|
# ============================================================================ |
111
|
|
|
|
112
|
|
|
|
113
|
|
|
def test_toYearFraction(): |
114
|
|
|
assert_allclose(helpers.toYearFraction(dt.datetime(2001, 1, 1, 0, 0, 0)), |
115
|
|
|
2001) |
116
|
|
|
assert_allclose(helpers.toYearFraction(dt.date(2001, 1, 1)), 2001) |
117
|
|
|
assert_allclose(helpers.toYearFraction(dt.datetime(2002, 1, 1, 0, 0, 0)), |
118
|
|
|
2002) |
119
|
|
|
assert_allclose(helpers.toYearFraction(dt.datetime(2005, 2, 3, 4, 5, 6)), |
120
|
|
|
2005.090877283105) |
121
|
|
|
assert_allclose(helpers.toYearFraction(dt.datetime(2005, 12, 11, 10, 9, 8)), |
122
|
|
|
2005.943624682902) |
123
|
|
|
|
124
|
|
|
|
125
|
|
|
# ============================================================================ |
126
|
|
|
# Test gc2gdlat |
127
|
|
|
# ============================================================================ |
128
|
|
|
|
129
|
|
|
|
130
|
|
|
def test_gc2gdlat(): |
131
|
|
|
assert_allclose(helpers.gc2gdlat(0), 0) |
132
|
|
|
assert_allclose(helpers.gc2gdlat(90), 90) |
133
|
|
|
assert_allclose(helpers.gc2gdlat(30), 30.166923849507356) |
134
|
|
|
assert_allclose(helpers.gc2gdlat(60), 60.166364190170931) |
135
|
|
|
|
136
|
|
|
|
137
|
|
|
# ============================================================================ |
138
|
|
|
# Test subsol |
139
|
|
|
# ============================================================================ |
140
|
|
|
|
141
|
|
|
@pytest.mark.parametrize('dtime,coord', [(dt.datetime(2005, 2, 3, 4, 5, 6), (-16.505391672592904, 122.17768157084515)), |
142
|
|
|
(dt.datetime(2010, 12, 11, 10, 9, 8), (-23.001554595838947, 26.008999999955023)), |
143
|
|
|
(dt.datetime(1601, 1, 1, 0, 0, 0), (-23.06239721771427, -178.90131731228584)), |
144
|
|
|
(dt.datetime(2100, 12, 31, 23, 59, 59), (-23.021061422069053, -179.23129780639425))]) |
145
|
|
|
def test_subsol(dtime, coord): |
146
|
|
|
assert helpers.subsol(dtime) == approx(coord) |
147
|
|
|
|
148
|
|
|
|
149
|
|
|
@pytest.mark.parametrize('dtime', [dt.datetime(1600, 12, 31, 23, 59, 59), dt.datetime(2101, 1, 1, 0, 0, 0)]) |
150
|
|
|
def test_bad_subsol(dtime): |
151
|
|
|
with pytest.raises(ValueError): |
152
|
|
|
helpers.subsol(dtime) |
153
|
|
|
|
154
|
|
|
|
155
|
|
|
if __name__ == '__main__': |
156
|
|
|
pytest.main([__file__]) |
157
|
|
|
|