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