-
Notifications
You must be signed in to change notification settings - Fork 5
/
test_pwv_atm.py
283 lines (199 loc) · 10.1 KB
/
test_pwv_atm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
# This file is part of the pwv_kpno software package.
#
# The pwv_kpno package is free software: you can redistribute it and/or
# modify it under the terms of the GNU General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# The pwv_kpno package is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
# Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with pwv_kpno. If not, see <http://www.gnu.org/licenses/>.
"""This file provides tests for the function "transmission"."""
from datetime import datetime, timedelta
from unittest import TestCase
import numpy as np
from astropy.table import Table
from pytz import utc
from pwv_kpno import pwv_atm
from ._create_mock_data import create_mock_pwv_model
def _check_attrs(iterable, **kwargs):
"""Check the attribute values of objects in an iterable
Iterate through the contents of a given iterable and check that all
attribute values match those specified by **kwargs.
Args:
table (Table): A table with a 'date' column
**kwargs : datetime attributes and values
Returns:
True or False
"""
assert len(kwargs), 'No attributes specified'
for obj in iterable:
for param_name, param_value in kwargs.items():
if getattr(obj, param_name) != param_value:
return False
return True
class SearchArgumentErrors(TestCase):
"""Test that _check_date_time_args raises the appropriate errors
_check_date_time_args is responsible for checking the arguments for both
pwv_kpno.pwv_atm.measured_pwv and pwv_kpno.pwv_atm.modeled_pwv"""
def assert_raises_iter(self, kwarg, iterable, error):
for val in iterable:
self.assertRaises(error,
pwv_atm._check_date_time_args,
**{kwarg: val})
def test_checks_for_valid_year(self):
"""Test for correct errors due to bad year argument"""
next_year = datetime.now().year + 1
self.assert_raises_iter('year', [next_year], ValueError)
def test_checks_for_valid_month(self):
"""Test for correct errors due to bad month argument"""
self.assert_raises_iter('month', [-3, 0, 13], ValueError)
def test_checks_for_valid_day(self):
"""Test for correct errors due to bad day argument"""
self.assert_raises_iter('day', [-3, 0, 32], ValueError)
def test_checks_for_valid_hour(self):
"""Test for correct errors due to bad hour argument"""
self.assert_raises_iter('hour', [-3, 24, 30], ValueError)
class MeasuredPWV(TestCase):
"""Tests for the 'pwv_atm.measured_pwv' function"""
data_table = pwv_atm.measured_pwv()
def test_returned_tz_info(self):
"""Test if datetimes in the returned data are timezone aware"""
tzinfo = self.data_table[0][0].tzinfo
error_msg = 'Datetimes should be UTC aware (found "{}")'
self.assertTrue(tzinfo == utc, error_msg.format(tzinfo))
def test_returned_column_order(self):
"""Test the column order of the table returned by pwv_atm.measured_pwv()
The first two columns should be 'date' and 'KITT'
"""
col_0 = self.data_table.colnames[0]
col_1 = self.data_table.colnames[1]
error_msg = 'column {} should be "{}", found "{}"'
self.assertEqual(col_0, 'date', error_msg.format(0, 'date', col_0))
self.assertEqual(col_1, 'KITT', error_msg.format(1, 'KITT', col_1))
def test_filtering_by_args(self):
"""Test if results are correctly filtered by kwarg arguments"""
test_cases = [{'year': 2010}, {'month': 7}, {'day': 21}, {'hour': 5},
{'year': 2011, 'month': 4, 'day': 30, 'hour': 21}]
error_msg = 'pwv_atm.measured_pwv returned incorrect dates. kwargs: {}'
for kwargs in test_cases:
good_attr = _check_attrs(pwv_atm.measured_pwv(**kwargs)['date'],
**kwargs)
self.assertTrue(good_attr, error_msg.format(kwargs))
def test_units(self):
"""Test columns for appropriate units"""
for column in self.data_table.itercols():
if column.name == 'date':
self.assertEqual(column.unit, 'UTC')
else:
self.assertEqual(column.unit, 'mm')
class ModeledPWV(TestCase):
"""Tests for the 'pwv_atm.modeled_pwv' function"""
data_table = pwv_atm.modeled_pwv()
test_returned_tz_info = MeasuredPWV.__dict__["test_returned_tz_info"]
test_units = MeasuredPWV.__dict__["test_units"]
class PwvDate(TestCase):
"""Tests for the pwv_date function"""
@classmethod
def setUpClass(cls):
cls.pwv_model = create_mock_pwv_model(2010)
def test_known_dates(self):
"""Tests that _pwv_date returns correct value for a tabulated date"""
error_msg = "pwv_date returned incorrect PWV value for tabulated date"
test_date = datetime.utcfromtimestamp(self.pwv_model['date'][0])
test_date = test_date.replace(tzinfo=utc)
test_pwv = self.pwv_model['pwv'][0]
pwv, pwv_err = pwv_atm._pwv_date(test_date, test_model=self.pwv_model)
self.assertEqual(test_pwv, pwv, error_msg)
def test_data_gap_handling(self):
"""Test errors raised from function call for datetime without PWV data
pwv_date should raise an error if it is asked for the PWV at a datetime
that falls within a gap in available data spanning one day or more.
"""
# Start dates for data gaps
one_day_start = datetime(year=2010, month=1, day=11, tzinfo=utc)
three_day_start = datetime(year=2010, month=4, day=11, tzinfo=utc)
gaps = [(one_day_start, 1), (three_day_start, 3)]
mock_model = create_mock_pwv_model(year=2010, gaps=gaps)
self.assertRaises(ValueError,
pwv_atm._pwv_date,
one_day_start,
1,
mock_model)
self.assertRaises(ValueError,
pwv_atm._pwv_date,
three_day_start,
1,
mock_model)
class TransmissionErrors(TestCase):
"""Test pwv_kpno.transmission for raised errors due to bad arguments"""
def test_argument_values(self):
"""Test errors raise from function call with date out of data range
An error should be raised for dates that are not covered by the locally
available data files. For the release version of the package, the
acceptable date range begins with 2010 through the current date.
"""
early_day = datetime(year=2009, month=12, day=31, tzinfo=utc)
self.assertRaises(ValueError, pwv_atm._raise_transmission_args, early_day)
now = datetime.now()
late_day = now + timedelta(days=1)
self.assertRaises(ValueError, pwv_atm._raise_transmission_args, late_day)
late_year = datetime(year=now.year + 1, month=1, day=1, tzinfo=utc)
self.assertRaises(ValueError, pwv_atm._raise_transmission_args, late_year)
class TransmissionResults(TestCase):
"""Test pwv_kpno.transmission for the expected returns"""
mock_model = create_mock_pwv_model(2010)
def test_argument_values(self):
"""Test errors raised from function call with out of range values
PWV concentrations should be between 0 and 30.1 mm (inclusive). This
is due to the range of the atmospheric models.
"""
self.assertRaises(ValueError, pwv_atm.trans_for_pwv, -1)
# Check value that uses interpolation
self.assertIsNotNone(pwv_atm.trans_for_pwv(15.0))
# Check value outside domain that uses extrapolation
self.assertIsNotNone(pwv_atm.trans_for_pwv(30.5))
def test_column_units(self):
"""Test columns of the returned transmission table for correct units
Perform the test for the transmission at 2011/01/01 00:00 and an
airmass of 1.
"""
sample_transm = pwv_atm.trans_for_date(
datetime(2011, 1, 1, tzinfo=utc), format='datetime')
w_units = sample_transm['wavelength'].unit
t_units = sample_transm['transmission'].unit
error_msg = 'Wrong units for column "{}"'
self.assertEqual(w_units, 'angstrom', error_msg.format('wavelength'))
self.assertEqual(t_units, None, error_msg.format('transmission'))
class TransmissionErrorPropagation(TestCase):
"""Tests for the error propagation of pwv_kpno.pwv_atm.trans_for_pwv"""
def test_zero_pwv_error(self):
"""Returned error should be zero for a PWV error of zero"""
transmission = pwv_atm.trans_for_pwv(pwv=1, pwv_err=0)
expected_error = np.zeros(len(transmission))
all_zeros = np.array_equal(transmission['transmission_err'],
expected_error)
self.assertTrue(all_zeros)
def test_increasing_error(self):
"""As the PWV error increases so should the transmission error"""
transm_1 = pwv_atm.trans_for_pwv(pwv=2, pwv_err=1)
transm_5 = pwv_atm.trans_for_pwv(pwv=2, pwv_err=5)
error_is_greater = (e5 > e1 for e1, e5 in zip(transm_1, transm_5))
pass_test = np.all(error_is_greater)
self.assertTrue(pass_test)
def test_not_passed_pwv_error(self):
"""Returned transmission should have no error if not given PWV error"""
transmission = pwv_atm.trans_for_pwv(1)
no_err = np.all(('err' not in col for col in transmission.colnames))
self.assertTrue(no_err)
def test_works_with_binning(self):
"""Error should be returned regardless if binning occurs"""
transmission = pwv_atm.trans_for_pwv(pwv=2, pwv_err=1, bins=5000)
col_names = ['wavelength', 'transmission', 'transmission_err']
self.assertCountEqual(transmission.colnames, col_names)