-
Notifications
You must be signed in to change notification settings - Fork 1
/
semester.py
executable file
·315 lines (265 loc) · 8.61 KB
/
semester.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
309
310
311
312
313
314
#!/opt/local/bin/python2.7
#
import MySQLdb
import re,glob,os,sys
from SubBlock import *
from Queue import *
#from cStringIO import *
import numpy as np
import random
#import webbrowser
from matplotlib import pyplot as plt
from matplotlib.patches import Rectangle
#import matplotlib.animation as animation
#from matplotlib.collections import PatchCollection
#from matplotlib.gridspec import gridspec
from matplotlib import dates
import matplotlib.dates as md
from datetime import datetime, timedelta
#http://rhodesmill.org/pyephem/
import ephem
#import re,glob,os,sys
#setup some global things
dtor = np.pi/180.0
fsize=[20,40]
fsize=[8,8]
ymin=-0.3
ymax=5.3
XLAB_ROT=40
XLAB_FSIZE=14
SHOW_FULL=0
def IsOverlapping(Astart,Aend,Bstart,Bend):
return ( max(Astart,Bstart) <= min(Aend,Bend))
#SETUP SOME DEFAULT PARAMS
cgi_p0 = 1
cgi_p1 = 1
cgi_p2 = 1
cgi_p3 = 1
cgi_p4 = 1 #leaving this out for speed...
cgi_rss = 1
cgi_hrs = 1
cgi_bvit= 0 #bvit not really used
cgi_scam= 1
cgi_smin = 0
cgi_smax = 10
now = datetime.utcnow()+timedelta(hours=2)
if(now.hour >= 0 and now.hour <= 10):
now = now-timedelta(days=1)
#cgi_date = now.strftime("%Y/%m/%d")
#if(len(sys.argv[1])>0):
# cgi_date = sys.argv[1]
cgi_alttime = now.strftime("%H:%M")
cgi_usealttime = 0
cgi_altendtime = now.strftime("%H:%M")
cgi_usealtendtime = 0
cgi_tran = "any"
cgi_notcmoon= 0
cgi_propcode = '2014-2'
cgi_piname = ''
cgi_blockid= ''
cgi_moondist = 30.0
priority_list = []
if(cgi_p0):
priority_list.append(0)
if(cgi_p1):
priority_list.append(1)
if(cgi_p2):
priority_list.append(2)
if(cgi_p3):
priority_list.append(3)
if(cgi_p4):
priority_list.append(4)
instrument_list = []
if(cgi_rss):
instrument_list.append("RSS")
if(cgi_scam):
instrument_list.append("SCAM")
if(cgi_hrs):
instrument_list.append("HRS")
if(cgi_bvit):
instrument_list.append("BVIT")
#Need to set the semester to a default - no observations done...
#Needed a table to keep track of the observed blocks:
#err == index to keep track of whether the block was rejected for some reason (0 = accepted)
#CREATE TABLE BlockTracker (
#Block_Id int(10) unsigned,
#bstart datetime,
#bend datetime,
#err int(10) unsigned,
#Priority int(10) unsigned,
#ObsTime int(10) unsigned
#);
#drop table BlockTracker;
#NightTracker
#CREATE TABLE NightTracker(
#Night datetime,
#Seeing float unsigned,
#dstart datetime,
#dend datetime,
#P0 float unsigned,
#P1 float unsigned,
#P2 float unsigned,
#P3 float unsigned,
#P4 float unsigned,
#Weather float unsigned,
#WeatherSeeing float unsigned,
#WeatherCloud float unsigned,
#WeatherHumidity float unsigned,
#Eng float unsigned,
#Tech float unsigned
#);
#update Block set NDone=0;
sqluser = 'brent'
sqldb = 'sdb_brent'
sqlhost = 'devsdb'
sqlpasswd='yourpasswordhere'
#Clear out the old observations
#May need to do reset other quantities here (to check with Encarni)
con = MySQLdb.connect(user=sqluser,db=sqldb,host=sqlhost,passwd=sqlpasswd)
cur = con.cursor()
qtxt = "update Block set NDone=0"
cur.execute(qtxt)
qtxt = "update Block set OnHold=0"
cur.execute(qtxt)
#remove all rows in the BlockTracker table
qtxt = "delete from BlockTracker"
cur.execute(qtxt)
qtxt = "delete from NightTracker"
cur.execute(qtxt)
cur.close()
con.commit()
con.close()
#allow print out of debug info from Queue class and elsewhere...
debug = 1
startdate = "2014/11/01"
enddate = "2015/04/30"
#startdate.replace('/','-')
d1 = ephem.localtime(ephem.date(startdate+' 15:00:00'))
Ndays = 7
#d2 = d1+timedelta(days=Ndays)
d2 = ephem.localtime(ephem.date(enddate+' 15:00:00'))
#thorough
cgi_niter = 20
#fast but poor results
#cgi_niter = 5
day = d1
while (day <= d2):
#pick a random seeing
cgi_smin = np.random.uniform(1.5,3.0)
cgi_date = day.strftime("%Y/%m/%d")
#a fixed seeing seems a bit too restrictive for now
cgi_smin = 0
print "Day: %s Seeing: %.2f" % (cgi_date,cgi_smin)
seeing_range = [cgi_smin,cgi_smax]
q = Queue(cgi_date,priority_list,seeing_range,cgi_usealttime,cgi_alttime,cgi_usealtendtime,cgi_altendtime,instrument_list,cgi_tran,cgi_notcmoon,cgi_propcode,cgi_piname,cgi_blockid,cgi_moondist,sqlhost,sqldb,debug)
(dstart,dend) = q.GetTwilightTimes()
total_duration = q.GetDuration()
duration_str="%.2f h" % (total_duration)
x=[dstart-timedelta(minutes=30),dend+timedelta(minutes=30)]
dt=timedelta(hours=total_duration*1.0/3.0)
#twilight buffer
dtwi=timedelta(minutes=30)
#buffer between plots
db=timedelta(minutes=5)
t1=dstart-dtwi
t2=t1+dtwi+dt
x1=[t1,t2]
t1=t2
t2=t1+dt
x2=[t1-db,t2+db]
t1=t2
t3=dend+dtwi
x3=[t1,t3]
yrange=[ymin,ymax]
yoffset=0.05*ymax
f, (ax1, ax2, ax3) = plt.subplots(nrows=3,ncols=1, sharey=True,subplot_kw=dict(ylim=yrange,yticks=[]),figsize=fsize)
f.subplots_adjust(hspace=0.3,left=0.05,right=0.95,top=0.925)
xfmt=md.DateFormatter('%H:%M')
ax1.xaxis.set_major_formatter(xfmt)
ax2.xaxis.set_major_formatter(xfmt)
ax3.xaxis.set_major_formatter(xfmt)
ax1.set_xlim(x1)
ax2.set_xlim(x2)
ax3.set_xlim(x3)
ax1.xaxis.set_major_locator(md.HourLocator(interval=1))
ax2.xaxis.set_major_locator(md.HourLocator(interval=1))
ax3.xaxis.set_major_locator(md.HourLocator(interval=1))
ax1.xaxis.set_minor_locator(md.MinuteLocator(byminute=range(0,60,10)))
ax2.xaxis.set_minor_locator(md.MinuteLocator(byminute=range(0,60,10)))
ax3.xaxis.set_minor_locator(md.MinuteLocator(byminute=range(0,60,10)))
ax1.tick_params(which='major',length=7,width=2)
ax1.tick_params(which='minor',length=5)
ax2.tick_params(which='major',length=7,width=2)
ax2.tick_params(which='minor',length=5)
ax3.tick_params(which='major',length=7,width=2)
ax3.tick_params(which='minor',length=5)
#ax1.major_ticks.set_ticksize(10)
#f.figsize=fsize
moonstr = ' [%d per cent]' % (q.GetMoonIllum())
ax1.set_title(dstart.strftime("%d %b %Y")+"\n"+duration_str+moonstr)#,weight='bold',va='top')
#format the axes properly
plt.setp(ax1.get_yticklabels(), visible=False)
plt.setp(ax2.get_yticklabels(), visible=False)
plt.setp(ax3.get_yticklabels(), visible=False)
for label in ax1.xaxis.get_ticklabels():
label.set_rotation(XLAB_ROT)
label.set_fontsize(XLAB_FSIZE)
for label in ax2.xaxis.get_ticklabels():
label.set_rotation(XLAB_ROT)
label.set_fontsize(XLAB_FSIZE)
for label in ax3.xaxis.get_ticklabels():
label.set_rotation(XLAB_ROT)
label.set_fontsize(XLAB_FSIZE)
#xstart,xend =ax1.get_xlim()
#ax1.xaxis.set_ticks(np.arange(xstart,xend,timedelta(hours=1.0)))
ax1.plot([dstart,dstart],yrange,'r--',lw=2.0)
ax3.plot([dend,dend],yrange,'r--',lw=2.0)
ti5=dstart-timedelta(minutes=15)
ti10=dstart-timedelta(minutes=20)
ti15=dstart-timedelta(minutes=25)
tf5=dend+timedelta(minutes=5)
tf10=dend+timedelta(minutes=10)
tf15=dend+timedelta(minutes=15)
ax3.plot([tf5,tf5],yrange,'y-',lw=2.0)
ax3.plot([tf10,tf10],yrange,'y-',lw=2.0)
ax3.plot([tf15,tf15],yrange,'y-',lw=2.0)
if(cgi_usealttime == 0):
ax1.plot([ti5,ti5],yrange,'y-',lw=2.0)
ax1.plot([ti10,ti10],yrange,'y-',lw=2.0)
ax1.plot([ti15,ti15],yrange,'y-',lw=2.0)
moon_alpha = 0.3
#moon_alpha = 0.5
(mstart,mend,mduration) = q.GetMoonRange()
if(mstart != 'NULL' and mend != 'NULL'):
ax1.add_patch(Rectangle((mstart,yrange[0]),mduration,yrange[1]*1.5,color='yellow',picker=False,alpha=moon_alpha))
ax2.add_patch(Rectangle((mstart,yrange[0]),mduration,yrange[1]*1.5,color='yellow',picker=False,alpha=moon_alpha))
ax3.add_patch(Rectangle((mstart,yrange[0]),mduration,yrange[1]*1.5,color='yellow',picker=False,alpha=moon_alpha))
#The following would do the schedule for each night
#by optimising the blocks via randomisation and progressively
#allocating the highest priorities first
# cgi_niter = 20
# if(cgi_p0 and cgi_p1):
# q.RandomiseBlocks(cgi_niter,False,0,1)
# if(cgi_p1 and cgi_p2):
# q.RandomiseBlocks(cgi_niter,False,1,2)
# if(cgi_p2 and cgi_p3):
# q.RandomiseBlocks(cgi_niter,False,2,3)
# if(cgi_p3 and cgi_p4):
# q.RandomiseBlocks(cgi_niter,False,3,4)
#This does the allocation based on essentially what the SA does at the telescope
#Results are not as good as RandomiseBlocks approach
q.MimicSA()
#display the queue
q.DisplayActiveBlocks(ax1,ax2,ax3,yrange)
#Record the queue as observed in the database
q.MarkActiveAsObserved()
#Write out stats to each of the tables
q.UpdateBlockTracker()
q.UpdateNightTracker()
#Write out a plot of the queue for the night
basename = day.strftime("%Y%m%d")
basename = basename+'.png'
plt.savefig(basename,orientation='portrait',papertype='A4',pad_inches=0.0)
#plt.show()
#Go to the next day
day = day+timedelta(days=1)