-
Notifications
You must be signed in to change notification settings - Fork 0
/
Observatory.py
308 lines (241 loc) · 10.9 KB
/
Observatory.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
import Constants
import Telescope
from Utilities import UTC_Offset
import ephem
from dateutil.parser import parse
from datetime import tzinfo, timedelta, datetime
import pytz
import numpy as np
import operator
import copy
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
from matplotlib.pyplot import cm
import matplotlib.dates as md
class Observatory():
def __init__(self, name, lon, lat, elevation, horizon,
obs_date_str, obs_filter, timezone, fov, gw_exptime, slewtime_per_deg):
self.name = name
self.fov = fov
self.gw_exptime = gw_exptime
self.obs_filter = obs_filter
self.slewtime_per_deg = slewtime_per_deg
self.ephemeris = ephem.Observer()
self.ephemeris.lon = str(lon)
self.ephemeris.lat = str(lat)
self.ephemeris.elevation = elevation
self.ephemeris.horizon = str(horizon)
self.lon = lon
self.lat = lat
self.elevation = elevation
self.timezone = timezone
#self.telescopes = telescopes
utc_offset = datetime.now(pytz.timezone(timezone)).utcoffset().total_seconds()/60./60.
self.utc_offset = utc_offset
self.obs_date_string = obs_date_str
obs_date = parse("%s 12:00" % obs_date_str) # UTC Noon
self.obs_date = obs_date
self.ephemeris.date = (self.obs_date - timedelta(hours=utc_offset)) # Local Noon n UTC
self.utc_begin_night = self.ephemeris.next_setting(ephem.Sun(), use_center=True).datetime()
self.utc_end_night = self.ephemeris.next_rising(ephem.Sun(), use_center=True).datetime()
self.local_begin_night = pytz.utc.localize(self.utc_begin_night) \
.astimezone(UTC_Offset(utc_offset,name))
self.local_end_night = pytz.utc.localize(self.utc_end_night) \
.astimezone(UTC_Offset(utc_offset,name))
timeDiff = self.local_end_night - self.local_begin_night
self.length_of_night = int(round(timeDiff.total_seconds() / 60))
self.utc_time_array = np.asarray([self.utc_begin_night + timedelta(minutes=minute) \
for minute in range(self.length_of_night)])
self.local_time_array = np.asarray([self.local_begin_night + timedelta(minutes=minute) \
for minute in range(self.length_of_night)])
self.sidereal_string_array = []
self.sidereal_radian_array = []
for utc_time in self.utc_time_array:
self.ephemeris.date = utc_time
st = self.ephemeris.sidereal_time()
tokens = str(st).split(":")
float_tokens = [float(t) for t in tokens]
st_string = "%02d:%02d:%02d" % (float_tokens[0],float_tokens[1],float_tokens[2])
self.sidereal_string_array.append(st_string)
self.sidereal_radian_array.append(st)
print("%s - %s deg Twilight Ends: %s" % (self.name, np.abs(self.ephemeris.horizon), self.local_begin_night))
print("%s - %s deg Dawn Begins: %s" % (self.name, np.abs(self.ephemeris.horizon), self.local_end_night))
def is_contiguous(self, int_array):
i = iter(int_array)
first = next(i)
contiguous = all(a == b for a, b in enumerate(i, first + 1))
return contiguous
def schedule_targets(self, telescope_name, preview_plot=False):
# Update internal Target list with priorities and exposures
telescope = self.telescopes[telescope_name]
telescope.compute_exposures()
telescope.compute_net_priorities()
targets = telescope.get_targets()
# Sorted by priority and closeness to discovery
targets.sort(key = operator.attrgetter('net_priority')) # 'TotalGoodAirMass'
length_of_night = len(self.utc_time_array) # In minutes
for tgt in targets:
print("%s: %s; %s min; Pri: %s" % (tgt.name, tgt.exposures, tgt.total_minutes, tgt.priority))
time_slots = np.zeros(length_of_night)
o = []
bad_o = []
for tgt in targets:
gam1 = copy.deepcopy(tgt.raw_airmass_array)
gam2 = copy.deepcopy(tgt.raw_airmass_array)
found = False
while not found:
if tgt.total_observable_min <= 0:
print("%s is unobservable!" % tgt.name)
break
gam2[np.where(time_slots == 1)] = 8888 # make different than airmass cutoff flag
goodtime = np.where(gam2 <= Constants.airmass_threshold)
n = len(goodtime[0])
current_start = -1 # So that iterator below starts at 0, the first index
best_indices = []
largest_airmass = 1e+6
# We are crawling forward along the array, grabbing segments of length "total_min",
# and incrementing in starting index
for i in range(n):
current_start += 1 # start index
end = (current_start + tgt.total_minutes) # how many
candidate_indices = goodtime[0][current_start:end] # array of selected indices
if len(candidate_indices) != tgt.total_minutes: # If this is at the end of the array, it won't be the size we need
# print("%s: can't fit %s exp time in slot of size %s " % \
# (obj.Name, obj.TotalMinutes, len(candidate_indices)))
# print(candidate_indices)
continue
else:
# Compute the integrated airmass. We're looking for the smallest # => the best conditions
integrated_am = np.sum(gam1[candidate_indices])
# Check if this associated integrated airmass corresponds to a range of time that's contiguous
contiguous = self.is_contiguous(candidate_indices)
# if this is the smallest, and is for a contiguous span of time, it's the new one to beat
if integrated_am < largest_airmass and contiguous:
largest_airmass = integrated_am
best_indices = candidate_indices
if largest_airmass < 1e+6:
found = True
time_slots[best_indices] = 1 # reserve these slots
# grab the corresponding
tgt.scheduled_airmass_array = np.asarray(tgt.raw_airmass_array)[best_indices]
tgt.scheduled_time_array = np.asarray(self.local_time_array)[best_indices]
tgt.starting_index = best_indices[0]
o.append(tgt)
else:
print("Can't fit %s. Skipping!" % tgt.name)
bad_o.append(tgt)
break
self.plot_results(o, telescope_name, preview_plot)
telescope.write_schedule(self.name, self.obs_date ,o)
def schedule_target(self, telescope_name, time_obs, preview_plot=False):
# Update internal Target list with priorities and exposures
telescope = self.telescopes[telescope_name]
telescope.compute_exposures()
telescope.compute_net_priorities()
targets = telescope.get_targets()
# Sorted by priority and closeness to discovery
targets.sort(key = operator.attrgetter('net_priority')) # 'TotalGoodAirMass'
length_of_night = len(self.utc_time_array) # In minutes
for tgt in targets:
print("%s: %s; %s min; Pri: %s" % (tgt.name, tgt.exposures, tgt.total_minutes, tgt.priority))
time_slots = np.zeros(length_of_night)
o = []
bad_o = []
for tgt in targets:
gam1 = copy.deepcopy(tgt.raw_airmass_array)
gam2 = copy.deepcopy(tgt.raw_airmass_array)
found = False
while not found:
if tgt.total_observable_min <= 0:
print("%s is unobservable!" % tgt.name)
break
gam2[np.where(time_slots == 1)] = 8888 # make different than airmass cutoff flag
goodtime = np.where(gam2 <= Constants.airmass_threshold)
n = len(goodtime[0])
current_start = -1 # So that iterator below starts at 0, the first index
best_indices = []
largest_airmass = 1e+6
# We are crawling forward along the array, grabbing segments of length "total_min",
# and incrementing in starting index
for i in range(n):
current_start += 1 # start index
end = (current_start + tgt.total_minutes) # how many
candidate_indices = goodtime[0][current_start:end] # array of selected indices
if len(candidate_indices) != tgt.total_minutes: # If this is at the end of the array, it won't be the size we need
# print("%s: can't fit %s exp time in slot of size %s " % \
# (obj.Name, obj.TotalMinutes, len(candidate_indices)))
# print(candidate_indices)
continue
else:
# Compute the integrated airmass. We're looking for the smallest # => the best conditions
integrated_am = np.sum(gam1[candidate_indices])
# Check if this associated integrated airmass corresponds to a range of time that's contiguous
contiguous = self.is_contiguous(candidate_indices)
# if this is the smallest, and is for a contiguous span of time, it's the new one to beat
if integrated_am < largest_airmass and contiguous:
largest_airmass = integrated_am
best_indices = candidate_indices
if largest_airmass < 1e+6:
found = True
time_slots[best_indices] = 1 # reserve these slots
# grab the corresponding
tgt.scheduled_airmass_array = np.asarray(tgt.raw_airmass_array)[best_indices]
tgt.scheduled_time_array = np.asarray(self.local_time_array)[best_indices]
tgt.starting_index = best_indices[0]
o.append(tgt)
else:
print("Can't fit %s. Skipping!" % tgt.name)
bad_o.append(tgt)
break
self.plot_results(o, telescope_name, preview_plot)
telescope.write_schedule(self.name, self.obs_date ,o)
def plot_results(self, good_targets, telescope_name, preview_plot):
good_targets.sort(key = operator.attrgetter('starting_index'))
length_of_night = len(self.utc_time_array) # in minutes
fig = plt.figure(figsize=(10,4))
ax1 = fig.add_subplot(111)
ax2 = ax1.twiny()
ax3 = ax1.twiny()
ax1.invert_yaxis()
ax1.set_ylim([Constants.airmass_threshold,0.8])
n = len(good_targets)
color=cm.rainbow(np.linspace(0,1,n))
c = iter(color)
ax1.set_ylabel("Relative Air Mass")
ax1.set_xlabel("Local Time")
ax1.grid(True)
ax1.set_axisbelow(True)
ax2.plot(self.utc_time_array, np.zeros(length_of_night))
ax2.set_xlabel("UTC")
ax2.get_xaxis().set_major_formatter(md.DateFormatter('%H:%M'))
ax3.plot(self.utc_time_array, np.zeros(length_of_night))
num_ticks = 7
nn = round(length_of_night/num_ticks)
ax3_ind = [i*nn for i in range(num_ticks)]
ax3_ind.remove(0)
ax3.set_xlabel("LST")
ax3.xaxis.set_ticks_position("bottom")
ax3.xaxis.set_label_position("bottom")
ax3.set_xticks(np.asarray(self.utc_time_array)[ax3_ind])
ax3.set_xticklabels(np.asarray(self.sidereal_string_array)[[ax3_ind]]) #,rotation=0,fontsize='small'
# Offset the twin axis below the host
ax3.spines["bottom"].set_position(("axes", -0.18))
total_tgts = 0
for tgt in good_targets:
lbl = "%s\nNat Pri: %s\nNet Pri: %0.5f\n%s min" % (tgt.name, tgt.priority, tgt.net_priority, tgt.total_minutes)
total_tgts += tgt.total_minutes
col = next(c)
ax1.plot(self.local_time_array,tgt.raw_airmass_array,c=col,linewidth=3.0,alpha=0.1)
ax1.plot(tgt.scheduled_time_array,tgt.scheduled_airmass_array,c=col,label=lbl,linewidth=3.0)
leg = ax1.legend(bbox_to_anchor=(1.01, 1.015), loc='upper left', ncol=2, prop={'size':8})
# set the linewidth of each legend object
for legobj in leg.legendHandles:
legobj.set_linewidth(3.0)
percent1 = 100*float(total_tgts)/float(self.length_of_night)
fig.suptitle("%s %s: %s\nOpen Shutter Time: %0.2f%%" % \
(self.name, telescope_name, self.obs_date.date(),percent1),y=1.10)
fig_to_save = "%s_%s_%s_Plot.png" % (self.name, telescope_name, self.obs_date_string)
fig.savefig(fig_to_save,bbox_inches='tight',dpi=300)
if preview_plot:
plt.show(block=False)