-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcondition_table.py
142 lines (111 loc) · 4.02 KB
/
condition_table.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
# Matt Bonnyman 18 July 2018
import numpy as np
from astropy.table import Table
from convert_conditions import convertcond
def checkdist(conddist):
"""
Check user input type if distribution type selected for generating conditions.
Parameters
----------
conddist : str or NoneType
condition distribution name
Returns
-------
str or None
"""
if conddist is None:
return None
elif conddist.lower() == 'none' or conddist == '':
return None
elif conddist.lower() == 'r' or conddist.lower() == 'random':
return 'random'
elif conddist.lower() == 'v' or conddist.lower() == 'variant':
return 'variant'
else:
print(' ValueError: Condition distribution type \'{}\' not recognized.'.format(conddist))
print(' Conditions distributions must be "r", "random", "v", or "variant".')
raise ValueError
def condition_table(size, iq, cc, wv, conddist=None):
"""
Generates an '~astropy.table.Table' of sky conditions.
Columns: iq (image quality), cc (cloud condition), wv (water vapour).
If conddist is NoneType, then the conditions will use iq, cc, wv.
If conddist is 'gaussian', conditions will be randomly generated for each night.
If conddist is 'variant', a random variant will be selected for each night.
Examples
--------
Parameters
----------
size : int
length of time grid for observing window (eg. a night or portion of a night)
iq : array of floats
image quality (if conddist is None)
cc : array of floats
cloud condition (if conddist is None)
wv : array of floats
water vapor (if conddist is None)
conddist : str or None
condition distribution type to generate conditions from (or None to use iq, cc, and wv values)
Returns
-------
'~astropy.table.Table' of iq, cc, and wv condition values.
"""
if conddist is None: # Use conditions input
pass
elif conddist.lower() == 'variant' or conddist.lower() == 'v': # Randomly select a variant
variantnum = np.random.randint(4) + 1
if variantnum == 1:
iq = '20%'
cc = '50%'
wv = '50%'
elif variantnum == 2:
iq = '70%'
cc = '70%'
wv = '80%'
elif variantnum == 3:
iq = '85%'
cc = '70%'
wv = 'Any'
else:
iq = 'Any'
cc = '70%'
wv = 'Any'
elif conddist.lower() == 'random' or conddist.lower() == 'r': # Generate random conditions
iq = str(np.random.uniform(0, 100))
cc = str(np.random.uniform(0, 100))
wv = str(np.random.uniform(0, 100))
else:
raise ValueError(' Condition type \'{}\' not recognized.'.format(conddist))
# Convert conditions to decimals and construct table columns.
bg = 'Any'
iq, cc, bg, wv = convertcond(iq, cc, bg, wv) # convert string or float to decimal
iq = np.full(size, iq)
cc = np.full(size, cc)
wv = np.full(size, wv)
return Table([iq, cc, wv], names=('iq', 'cc', 'wv'))
def test_condtable():
size = 3
conds = condition_table(size, '20%', '50%', '85%', conddist=None)
assert conds['iq'][0] == 0.2
assert conds['cc'][0] == 0.5
assert conds['wv'][0] == 0.85
print('\nInput iq=20%, cc=50%, wv=85%\n', conds)
# -- Reset random number seed --
np.random.seed(1000) # Should produce variant 4 each time.
conds = condition_table(size, '20%', '50%', '85%', conddist='variant')
assert conds['iq'][0] == 1.
assert conds['cc'][0] == 0.7
assert conds['wv'][0] == 1.
print('\nInput \'variant\'\n', conds)
# -- Reset random number seed --
np.random.seed(1000)
conds = condition_table(size, '20%', '50%', '85%', conddist='random')
assert conds['iq'][0] == 0.7
assert conds['cc'][0] == 0.2
assert conds['wv'][0] == 1.
print('\nInput \'gaussian\'\n', conds)
print('Test successful!')
return
if __name__ == '__main__':
test_condtable()
exit()