-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathopsimObs.py
264 lines (231 loc) · 11.9 KB
/
opsimObs.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
"""
opsimObs.py :
A python script intended to compute 'as would be (approximately) seen in opSim' observation conditions
for a given list of RA (degrees)/Dec (degrees)/Time (UTC MJD)/ExpTime values.
The script will read weather data (cloud and seeing histories from Cerro Tololo),
compute seeing appropriate for the bandpass & airmass of each observation,
compute sky brightness (including telescope dome scattered light contributions) for each observation,
compute the 5-sigma limiting magnitude for each observation,
and report these values to the user.
Requirements:
python
numpy
pyslalib
LSST 'throughputs' package
"""
import sys
import warnings
import numpy
from Weather import Weather
from Downtime import Downtime
from SkyPos import SkyPos
from ObsFields import ObsFields
from Sun import Sun
from Moon import Moon
from SkyBright import SkyBright
from m5calculations import m5calculations
_deg2rad = numpy.pi/180.0
_rad2deg = 180.0/numpy.pi
_sec2day = 1/60.0/60.0/24.0
_day2sec = 24.0*60.0*60.0
import time
def dtime(time_prev):
return (time.time() - time_prev), time.time()
def setDefaultConfigs(override_file = None):
""" Set some default configuration parameters for the LSST. The override_file can be used to override
these parameters - file format should be 'keyword' 'value' (keyword should match one of the dictionary
keys below and value should be in quotes or have no spaces).
Returns dictionaries 'config', 'obsreq', 'lsstsite' containing needed parameters. """
# These next parameters are used to define configuration parameters for opsimObs.
config = {}
config_keys = ['sim_start', 'seeing_start', 'cloud_start', 'seeing_datafile', 'cloud_datafile',
'filter_names', 'seeing_wavelen', 'filter_wavelen',
'schedDowntime_datafile', 'unschedDowntime_datafile']
config_keytypes = [float, float, float, str, str, tuple, float, dict, str, str]
# Simulation 'start' day (UTC MJD) for placing location of Earth / visibility of fields.
# OpSim starts this on 49353 (to match with start of the weather data). (sim_start == seeing_start ..)
config['sim_start'] = 49353.0
# Seeing data 'start' day (can offset into the seeing data by this many days - will loop if run past end).
config['seeing_start'] = 0.0
# Cloud data 'start' day (can offset into the cloud data by this many days - will loop if run past end).
config['cloud_start'] = 0.0
# Seeing data file. Default file contains 2 years of seeing data from Pachon, recorded every 300 seconds.
config['seeing_datafile'] = 'SeeingPachon.txt'
# Cloud data file. Default file contains 10 years of cloud data from CTIO, sampled 4 times/night.
config['cloud_datafile'] = 'CloudTololo.txt'
# List of the LSST filters. Default 'y' = 'y4' (as values for 'y' == 'y4').
config['filter_names'] = ('u', 'g', 'r', 'i', 'z', 'y', 'y3', 'y4')
# Effective wavelengths of each filter (for seeing corrections).
config['seeing_wavelen'] = 500.0
config['filter_wavelen'] = {'u':359, 'g':479, 'r':620, 'i':753, 'z':869,
'y':967, 'y3':996, 'y4':967}
# Downtime files.
config['schedDowntime_datafile'] = 'schedDown.conf'
config['unschedDowntime_datafile'] = 'unschedDown.conf'
# These next parameters are used to do a rudimentary check on if requested observations are possible
# based on some simple time requirements (filter changes, number of filters within a night..).
config_keys += ['check_observations', 'n_simultaneous_filters', 'readout_time', 'shutter_time', 'filter_time', 'slew_time',
'nexp_visit', 'add_shutter', 'add_slew']
config_keytypes += [int, int, float, float, float, float, int, int, int]
# Note that this check does NOT include any instrument model or slew time needed.
# Do consistency checks for incoming observations (to check minimum time spacing, etc.)
config['check_observations'] = 1
# Number of filters that can be used in the telescope in one night.
config['n_simultaneous_filters'] = 5
# Readout time (seconds)
config['readout_time'] = 2.0
# Shutter time (seconds)
config['shutter_time'] = 1.0
# Add shutter time to given expTime (=1) or implicitly include (=0) [opsim implicitly includes in visit expTime]
config['add_shutter'] = 0.0
# Slew/settle time (seconds)
config['slew_time'] = 5.0
# Add slew time after each visit (for dither, for example?) [=0 or 1].
config['add_slew'] = 0
# Filter change time (seconds)
config['filter_time'] = 120.0
# Number of exposures per visit (1==one exp per visit) .. telescope slew/settle happens after visit, if 'add_slew'.
# NOTE : the input 'observation' file should only record VISITS
config['nexp_visit'] = 1.0
# These next parameters are used to determine some parameters for the LSST telescope.
config_keys += ['latitude', 'longitude', 'height', 'pressure', 'temperature', 'relativeHumidity', 'lapseRate']
config_keytypes += [float, float, float, float, float, float, float]
config['longitude'] = -1.2320792*_rad2deg #degrees
config['latitude'] = -0.517781017*_rad2deg #degrees
config['height'] = 2650.0 #km
config['pressure'] = 749.3
config['temperature'] = 285.655 #C
config['relativeHumidity'] = 0.40
config['lapseRate'] = 0.0065
config['midnight'] = 0.16 # Midnight at LSST is this much past the start of a day (UTC)
# Contribution toward the final seeing that comes from the telescope and camera (arcseconds)
# (the seeing data file is raw atmospheric seeing)
config['seeing_Telescope'] = 0.38
# And limit on 'good end' of seeing.
config['good_seeing_limit'] = 0.25
# Minimum distance to moon (radians)
config['min_moonDist'] = 15.0 * _deg2rad
# Read override file, if provided.
if override_file != None:
file = open(override_file, 'r')
for line in file:
# Skip comments or incomplete keyword / value pairs.
if (line.startswith('#') or line.startswith('!') or (len(line.split())<3)):
continue
# Get the keyword / "=" / value pairs from this line.
keyword = line.split()[0]
value = line.lstrip(keyword).lstrip().lstrip('=').lstrip()
value = value.rstrip()
# Evaluate and override default dictionary settings.
if keyword in config_keys:
ix = config_keys.index(keyword)
print '# Overriding ', keyword,' from ', config[keyword], 'to ', value, ' (a %s value)' %(config_keytypes[ix])
config[keyword] = config_keytypes[ix](value)
else:
raise warnings.warn('Unrecognized keyword %s: ignoring this value.' %(keyword))
file.close()
return config
def run(inputobs_file, config):
# Set up a skypos object to hold site information and provide ra/dec -> alt/az/airmass translations.
skypos = SkyPos()
skypos.setSite(lat=config['latitude'], lon=config['longitude'], height=config['height'],
pressure=config['pressure'], temperature=config['temperature'],
relativeHumidity=config['relativeHumidity'], lapseRate=config['lapseRate'])
# Set up a Weather object to read the site weather data.
t = time.time()
weather = Weather()
weather.readWeather(config)
dt, t = dtime(t)
print '# Reading weather required %.2f seconds' %(dt)
# Set up a Downtime object to read the downtime data.
downtime = Downtime()
downtime.readDowntime(config)
dt, t = dtime(t)
print '# Reading downtime required %.2f seconds' %(dt)
# Read observations.
obs = ObsFields()
obs.readInputObs(inputobs_file, config)
nobs = len(obs.ra)
dt, t = dtime(t)
print '# Reading %d input observations required %.2f seconds' %(nobs, dt)
# Check timing of input observations.
if config['check_observations']:
obs.checkInputObs(config)
dt, t = dtime(t)
print '# Checking %d input observations required %.2f seconds' %(nobs, dt)
else:
print '# Did not check input observations for minimum required timing separation!'
# Calculate alt/az/airmass for all fields.
obs.getAltAzAirmass(skypos)
dt, t = dtime(t)
print '# Calculating alt/az/airmass for %d input observations required %.2f seconds' %(nobs, dt)
# Calculate weather (cloud/seeing) for all fields.
dt, t = dtime(t)
obs.getObsWeather(weather, config)
dt, t = dtime(t)
print '# Getting weather information for %d observations required %.2f seconds' %(nobs, dt)
# Check downtime status for these observations
obs.getDowntime(downtime, config)
# Calculate position of sun at the times of these observations.
sun = Sun()
dt, t = dtime(t)
sun.calcPos(obs.mjd)
sun.getAltAz(skypos)
dt, t = dtime(t)
print '# Calculating sun position at %d times required %.2f seconds' %(nobs, dt)
# Calculate the position, phase and altitude of the Moon.
moon = Moon()
moon.calcPos(obs.mjd, config)
moon.getAltAz(skypos)
dt, t = dtime(t)
print '# Calculating moon position at %d times required %.2f seconds' %(nobs, dt)
# Will clean this up and put into classes as time is available.
# Calculate the sky brightness.
skybright = SkyBright(model='Perry', solar_phase='ave')
sky = numpy.zeros(len(obs.mjd), 'float')
filterlist = numpy.unique(obs.filter)
for f in filterlist:
condition = (obs.filter == f)
skybright.setSkyBright(obs.alt[condition], obs.az[condition], moon.alt[condition], moon.az[condition],
moon.phase[condition], bandpass = f)
sky[condition] = skybright.getSkyBright()
# Add modification to match 'skybrightness_modified' (which is brighter in twilight)
sky = numpy.where(sun.alt > -18, 17, sky)
dt, t = dtime(t)
print '# Calculating the sky brightness for %d observations required %.2f seconds' %(nobs, dt)
# Calculate the 5-sigma limiting magnitudes.
maglimit = numpy.zeros(len(obs.mjd), 'float')
m5 = m5calculations()
# Read the throughput curves for LSST (needed to determine zeropoints).
m5.setup_Throughputs(verbose=False)
# Swap 'y4' for 'y' (or other default y value).
tmp_filters = m5.check_filter(obs.filter)
# Determine the unique exposure times, as have to set up dark current, etc. based on this for telescope ZP's.
exptimes = numpy.unique(obs.exptime)
for expT in exptimes:
condition = [obs.exptime == expT]
# Calculate telescope zeropoints.
# Calculate actual open-sky time, after accounting for readout time, number of exposures per visit, shutter time, etc.
opentime = ((expT - config['readout_time']*config['nexp_visit'] - config['nexp_visit']* config['add_shutter']*config['shutter_time'])
/ float(config['nexp_visit']))
print "# Calculating depth for %d exposures of %.2f open shutter time" %(config['nexp_visit'], opentime)
m5.setup_values(expTime=opentime, nexp=config['nexp_visit'])
# Calculate 5sigma limiting magnitudes.
maglimit[condition] = m5.calc_maglimit(obs.seeing[condition], sky[condition], tmp_filters[condition], obs.airmass[condition], snr=5.0)
dt, t = dtime(t)
print '# Calculating the m5 limit for %d observations required %.2f seconds' %(nobs, dt)
# Print out interesting values.
obs.printObs(sun, moon, sky, maglimit, config)
if __name__ == '__main__':
inputobs_file = sys.argv[1]
if ((inputobs_file == 'help') | (inputobs_file == '-h')):
print ' Usage: <python> opsimObs.py inputobs_filename [override_config_filename]'
exit()
# Deal with input configuration information.
if len(sys.argv)> 2:
override_config_file = sys.argv[2]
else:
override_config_file = None
# Read configuration parameters.
config = setDefaultConfigs(override_config_file)
run(inputobs_file, config)