-
Notifications
You must be signed in to change notification settings - Fork 0
/
lp_predict.py
executable file
·1634 lines (1541 loc) · 80.8 KB
/
lp_predict.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
########################################
#JCMT Large Program Prediction Tool
########################################
'''Python script that simulates JCMT Large Program observing over the upcoming semester(s).
INPUT: (1) sim_start: proposed start date
(2) sim_end: proposed end date
(3) Blocked out dates for each instrument
(optional) Calibrator lists for SCUBA-2, HARP, and Pointing Cals
OUTPUT: (1) File summary of simulation results detailing the predicted number of hours observed/remaining for each project
(2) File summary of available, used, and unused time in each weather band
(3) File summaries of remaining hrs split by weather band, instrument, and program
(4a) Histograms displaying unused RA range per weather band
(4b) Histograms displaying remaining MSB RA range per weather band
(5) Bar plot of totals (observed/remaining hrs) per program
(6a) Bar plot of totals (used/unused/cals hrs) per weather band
(6b) Bar plot of unused hours per month for LAPs, broken down by weather band
(7) Incremental program completion chart (also available in tabular form)
(8) Bar plot of totals (remaining hrs in each weather band) per program
(9) Bar plot of total remaining hrs split by weather band and instrument
(10) Program specific statistics; Transient (record of which months each target was observed),
PITCH-BLACK (record of which semesters contained a campaign)
NOTES: - This script is meant to be run on an EAO computer as follows,
Usage: lp_predict.py [-h] simstart simend scuba2_un harp_un rua_un dir
simstart start of simulation -- str 'yyyy-mm-dd'
simend end of simulation -- str 'yyyy-mm-dd'
scuba2_un SCUBA-2 range of unavailable MJDs -- str 'MJD1,MJD2'
harp_un HARP range of unavailable MJDs -- str 'MJD1,MJD2'
rua_un UU/AWEOWEO range of unavailable MJDs -- str 'MJD1,MJD2'
dir data directory (i.e., where script is stored) -- str '/path/to/dir/'
If you want to run this script on any machine you need to generate the following on an EAO machine first:
(a) wvm file through the python script provided (getwvm.py).
(b) LAP projects file and MSB files through the sql scripts provided (example-project-summary.sql and example-project-info.sql)
Details are provided below in the 'other options' and 'SQL queries' sections of the script.
- Works in both Python 2 and 3.
- If you get an error about "astropy quantities in scheduler to table task", find scheduling.py in the astroplan package directory,
navigate to the `to_table` function in the `Schedule` class and edit line 303;
i.e.,change u.Quantity(ra) and u.Quantity(dec) to ra and dec in the return statement.
Written by: Alex J. Tetarenko
Last Updated: Aug 08, 2021
'''
#packages to import
from __future__ import print_function
import numpy as np
import math as ma
import pandas as pd
import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt
from matplotlib import rc
from matplotlib.ticker import AutoMinorLocator
import matplotlib.cm as cm
import matplotlib.dates as mdates
from matplotlib.dates import MONDAY
import datetime as datetime
from dateutil.relativedelta import relativedelta
import astropy
from astropy import units as u
from astropy.coordinates import SkyCoord
from astropy.io import ascii
from astropy.table import Table
from astropy.time import Time,TimeDelta
import warnings
warnings.filterwarnings('ignore')
import argparse
import astroplan
from astroplan import Observer, FixedTarget, AltitudeConstraint, is_observable, ObservingBlock, observability_table
from astroplan.constraints import TimeConstraint,AltitudeConstraint,SunSeparationConstraint
from astroplan.scheduling import PriorityScheduler, Schedule, Transitioner,Scheduler, Scorer,TransitionBlock
from astroplan.utils import time_grid_from_range,stride_array
from astroplan.plots import plot_schedule_airmass
import os
from collections import defaultdict
from getwvm import get_wvm_fromdisk, get_sampled_values
import time
import sys
from datetime import date, timedelta
from itertools import cycle
#omp-python import
if sys.version_info.major==2:
sys.path.append('/jac_sw/omp/python/lib/')
else:
#Sarah's python 3 version currently lives here, path will be changed at some point
sys.path.append('/net/kapili/export/data/sgraves/software/omp-python/lib')
from omp.db.part.arc import ArcDB
print('##############')
print('Using the following packages:\n')
print('astropy ',astropy.__version__)
print('matplotlib ',mpl.__version__)
print('numpy ',np.__version__)
print('astroplan ',astroplan.__version__)
print('pandas ',pd.__version__)
print('##############\n')
def sort_blocks(blocks):
''' Sort observing blocks in priority order.'''
bs=[]
for b in blocks:
if type(b.priority) is int or type(b.priority) is float:
bs.append(b.priority)
else:
bs.append(b.priority[0])
sorted_indxs=np.argsort(bs)
blocks_sorted=[]
for i in sorted_indxs:
blocks_sorted.append(blocks[i])
return(blocks_sorted)
class JCMTScheduler(Scheduler):
'''A scheduler that does simple sequential scheduling. That is, it starts at the beginning of the night,
looks at all the blocks (sorted in priority order), picks the best one, schedules it, and then moves on.
NOTE: This is just the astroplan SequentialScheduler with the addition of sorted observing blocks as input.'''
def __init__(self, *args, **kwargs):
super(JCMTScheduler, self).__init__(*args, **kwargs)
def _make_schedule(self, blocks):
blocks=sort_blocks(blocks)
pre_filled = np.array([[block.start_time, block.end_time] for
block in self.schedule.scheduled_blocks])
if len(pre_filled) == 0:
a = self.schedule.start_time
filled_times = Time([a - 1*u.hour, a - 1*u.hour,
a - 1*u.minute, a - 1*u.minute])
pre_filled = filled_times.reshape((2, 2))
else:
filled_times = Time(pre_filled.flatten())
pre_filled = filled_times.reshape((int(len(filled_times)/2), 2))
for b in blocks:
if b.constraints is None:
b._all_constraints = self.constraints
else:
b._all_constraints = self.constraints + b.constraints
# to make sure the scheduler has some constraint to work off of
# and to prevent scheduling of targets below the horizon
# TODO : change default constraints to [] and switch to append
if b._all_constraints is None:
b._all_constraints = [AltitudeConstraint(min=0 * u.deg)]
b.constraints = [AltitudeConstraint(min=0 * u.deg)]
elif not any(isinstance(c, AltitudeConstraint) for c in b._all_constraints):
b._all_constraints.append(AltitudeConstraint(min=0 * u.deg))
if b.constraints is None:
b.constraints = [AltitudeConstraint(min=0 * u.deg)]
else:
b.constraints.append(AltitudeConstraint(min=0 * u.deg))
b._duration_offsets = u.Quantity([0*u.second, b.duration/2,
b.duration])
b.observer = self.observer
current_time = self.schedule.start_time
while (len(blocks) > 0) and (current_time < self.schedule.end_time):
print(current_time)# first compute the value of all the constraints for each block
# given the current starting time
block_transitions = []
block_constraint_results = []
for b in blocks:
# first figure out the transition
if len(self.schedule.observing_blocks) > 0:
trans = self.transitioner(
self.schedule.observing_blocks[-1], b, current_time, self.observer)
else:
trans = None
block_transitions.append(trans)
transition_time = 0*u.second if trans is None else trans.duration
times = current_time + transition_time + b._duration_offsets
# make sure it isn't in a pre-filled slot
if (any((current_time < filled_times) & (filled_times < times[2])) or
any(abs(pre_filled.T[0]-current_time) < 1*u.second)):
block_constraint_results.append(0)
else:
constraint_res = []
for constraint in b._all_constraints:
constraint_res.append(constraint(
self.observer, b.target, times))
block_constraint_results.append(np.prod(constraint_res))# take the product over all the constraints *and* times
if block_constraint_results[-1]==1:
break
# now identify the block that's the best
bestblock_idx = np.argmax(block_constraint_results)
if block_constraint_results[bestblock_idx] == 0.:
# if even the best is unobservable, we need a gap
current_time += self.gap_time
else:
# If there's a best one that's observable, first get its transition
trans = block_transitions.pop(bestblock_idx)
if trans is not None:
self.schedule.insert_slot(trans.start_time, trans)
current_time += trans.duration
# now assign the block itself times and add it to the schedule
newb = blocks.pop(bestblock_idx)
newb.start_time = current_time
current_time += newb.duration
newb.end_time = current_time
newb.constraints_value = block_constraint_results[bestblock_idx]
self.schedule.insert_slot(newb.start_time, newb)
return self.schedule
def correct_msbs(LAPprograms,path_dir):
'''Corrects and simplifies MSB files to ensure that the total time of all MSBs matches the
total allocated time remaining, and that there are no duplicate target sources.'''
program_list=np.array(LAPprograms['projectid'])
print('Correcting MSBS...\n')
print('(- too much in MSBs, + too little in MSBs)')
for m in program_list:
if m.lower() == 'm20al008':
os.system('cp -r '+path_dir+'program_details_org/'+m.lower()+'-project-info.list '+path_dir+'program_details_sim')
else:
#print(m)
msbs=ascii.read(path_dir+'program_details_org/'+m.lower()+'-project-info.list',names=('projectid','msbid','remaining','obscount','timeest','msb_total_hrs','instrument','type','pol','target','ra2000','dec2000','taumin','taumax'))
remaining=LAPprograms['remaining_hrs'][np.where(LAPprograms['projectid']==m)[0][0]]
#check no target repeats first
cor=[(float(i),float(j)) for i,j in zip(msbs['ra2000'],msbs['dec2000'])]
unique_srcs=list(set(cor))
projid=[]
msbid=[]
remain=[]
obsc=[]
timeest=[]
inst=[]
ty=[]
pol=[]
targ=[]
tmin=[]
tmax=[]
for src in unique_srcs:
ind0=np.where(np.logical_and(src[0]==msbs['ra2000'],src[1]==msbs['dec2000']))[0][0]
projid.append(msbs['projectid'][ind0])
msbid.append(msbs['msbid'][ind0])
inst.append(msbs['instrument'][ind0])
ty.append(msbs['type'][ind0])
pol.append(msbs['pol'][ind0])
tmin.append(msbs['taumin'][ind0])
tmax.append(msbs['taumax'][ind0])
targ.append(msbs['target'][ind0])
timeest.append(msbs['timeest'][ind0])
re=[]
ob=[]
for i in range(0,len(msbs['ra2000'])):
if msbs['ra2000'][i]==src[0] and msbs['dec2000'][i]==src[1]:
re.append(msbs['remaining'][i])
ob.append(msbs['obscount'][i])
remain.append(np.sum(re))
obsc.append(np.sum(ob))
ascii.write([projid,msbid,remain,obsc,timeest,(np.array(timeest)*np.array(remain))/3600.,inst,\
ty,pol,targ,[src[0] for src in unique_srcs], [src[1] for src in unique_srcs],tmin,tmax],\
path_dir+'program_details_fix/'+m.lower()+'-project-info.list',names=msbs.colnames)
#now start with fixed msb file to check time allocation matches msb times
msbs=ascii.read(path_dir+'program_details_fix/'+m.lower()+'-project-info.list')
msb_remaining=np.sum(msbs['msb_total_hrs'])
diff= remaining - msb_remaining #negative is too much, + is too little
print('Correcting MSBs file for: ',m,' --> Time difference =', round(diff,2))
if abs(diff) >1:
coords=SkyCoord(ra=msbs['ra2000']*u.rad,dec=msbs['dec2000']*u.rad,frame='icrs')
#print(coords)
sep=coords[0].separation(coords)
repeats=int(remaining/(msbs['timeest'][0]/3600.))
if np.all(sep<0.1*u.degree):
ascii.write([[msbs['projectid'][0]],[msbs['msbid'][0]],[repeats],[msbs['obscount'][0]],\
[msbs['timeest'][0]],[(msbs['timeest'][0]*repeats)/3600.],[msbs['instrument'][0]],\
[msbs['type'][0]],[msbs['pol'][0]],[msbs['target'][0]],[msbs['ra2000'][0]],[msbs['dec2000'][0]],\
[msbs['taumin'][0]],[msbs['taumax'][0]]],\
path_dir+'program_details_fix/'+m.lower()+'-project-info.list', names=msbs.colnames)
else:
hrs_add=np.abs(diff)#int(abs(diff)/np.max((msbs['timeest'][0]/3600.)))
remain=np.array(msbs['remaining'])
t_est=np.array(msbs['timeest'])
while hrs_add >0.:
for i in range(0,len(msbs['target'])):
if hrs_add>0. and remain[i]>0:
remain[i]=remain[i]+np.sign(diff)
hrs_add=hrs_add-(t_est[i]/3600.)#-1
else:
remain[i]=remain[i]
hrs_add=hrs_add
ascii.write([msbs['projectid'],msbs['msbid'],remain,msbs['obscount'],\
msbs['timeest'],(msbs['timeest']*remain)/3600.,msbs['instrument'],\
msbs['type'],msbs['pol'],msbs['target'],msbs['ra2000'],msbs['dec2000'],\
msbs['taumin'],msbs['taumax']],\
path_dir+'program_details_fix/'+m.lower()+'-project-info.list', names=msbs.colnames)
#fix case where same name is given to different objects
values,cts=np.unique(msbs['target'],return_counts=True)
if np.any(cts>1):
fulltarlist=msbs['target'].tolist()
newtarlist=[]
for i,v in enumerate(fulltarlist):
totalcount=fulltarlist.count(v)
count=fulltarlist[:i].count(v)
newtarlist.append(v + 'n'+str(count +1) if totalcount >1 else v)
ascii.write([msbs['projectid'],msbs['msbid'],remain,msbs['obscount'],\
msbs['timeest'],(msbs['timeest']*remain)/3600.,msbs['instrument'],\
msbs['type'],msbs['pol'],newtarlist,msbs['ra2000'],msbs['dec2000'],\
msbs['taumin'],msbs['taumax']],path_dir+'program_details_fix/'+m.lower()+'-project-info.list', names=msbs.colnames)
print('\n')
def time_remain_p_weatherband(LAPprograms,path_dir):
'''Calculates remaining time per weather band for each program, per weather band for all programs,
and per instrument for all programs after the simulation has run.'''
program_list=np.array(LAPprograms['projectid'])
remainwb_tally={k:{} for k in program_list}
for i in ['Band 1', 'Band 2', 'Band 3', 'Band 4', 'Band 5']:
for j in list(remainwb_tally.keys()):
remainwb_tally[j][i]=0
tot={k:0 for k in ['Band 1', 'Band 2', 'Band 3', 'Band 4', 'Band 5']}
inst={k:0 for k in ['SCUBA-2', 'HARP','UU','AWEOWEO']}
fileo=open(path_dir+'sim_results/results_wb.txt','w')
fileo.write('Per Program:\n')
for m in program_list:
msbs=ascii.read(path_dir+'program_details_sim/'+m.lower()+'-project-info.list')
uni_wb=list(np.unique(msbs['taumax']))
instruments=list(np.unique(msbs['instrument']))
for i in range(0,len(uni_wb)):
wb=get_wband(uni_wb[i])
ind=np.where(msbs['taumax']==uni_wb[i])[0]
#RXA time / 2.2 is UU time
for dx in ind:
if msbs['instrument'][dx] == 'RXA3M':
msbs['timeest'][dx]=msbs['timeest'][dx]/2.2
rems=remainwb_tally[m.upper()][wb]+round(np.sum(msbs['remaining'][ind]*msbs['timeest'][ind]/3600.),2)
if rems <0.:#for when we over observe by a few minutes
remainwb_tally[m.upper()][wb]=0.0
else:
remainwb_tally[m.upper()][wb]=rems
fileo.write('{0} {1}\n'.format(m+':',remainwb_tally[m.upper()]))
for bandkey in list(remainwb_tally[m.upper()].keys()):
tot[bandkey]=tot[bandkey]+remainwb_tally[m.upper()][bandkey]
for val in range(0,len(instruments)):
ind2=np.where(msbs['instrument']==instruments[val])[0]
#RXA time is now UU time
if instruments[val] == 'RXA3M':
inst['UU']=inst['UU']+round(np.sum(msbs['remaining'][ind2]*msbs['timeest'][ind2]/3600.),2)
else:
adds=round(np.sum(msbs['remaining'][ind2]*msbs['timeest'][ind2]/3600.),2)
#for when we over observe by a few minutes
if adds>0.:
remsi=inst[instruments[val]]+adds
else:
remsi=inst[instruments[val]]
if remsi <0:
inst[instruments[val]]=0.0
else:
inst[instruments[val]]=remsi
fileo.close()
fileo=open(path_dir+'sim_results/results_split.txt','w')
fileo.write('Per Weather Band:\n')
for band in ['Band 1', 'Band 2', 'Band 3', 'Band 4', 'Band 5']:
fileo.write('{0} {1}\n'.format(band+':',np.round(tot[band],2)))
fileo.write('\n')
fileo.write('Per Instrument:\n')
for ins in ['SCUBA-2', 'HARP','UU','AWEOWEO']:
fileo.write('{0} {1}\n'.format(ins+':',np.round(inst[ins],2)))
fileo.close()
return(tot,inst,remainwb_tally)
def transform_blocks(blocks_file):
'''**(Only used for old version of script prior to switch to remote observing)**
Reads in observing blocks data file. We make sure to properly deal with the irregular observing blocks data file,
which has inconsistent columns.'''
newfile=blocks_file.strip('.txt')+'_corr.txt'
f=open(newfile,'w')
with open(blocks_file, 'r') as ins:
array = []
for line in ins:
linecode=line.strip('\n').split(' ')
if (len(linecode)==4) and ('' not in linecode):
f.write('{0} {1} {2} {3}\n'.format(linecode[0],linecode[1],linecode[2],linecode[3]))
elif (len(linecode)==3) and ('' not in linecode):
f.write('{0} {1} {2} {3}\n'.format(linecode[0],linecode[1],linecode[2],'none'))
else:
raise ValueError('The format of your LAP blocks file is incorrect. Please check for errors in the file.')
f.close()
#read observing blocks
Blocks=ascii.read(newfile,delimiter=' ',\
guess=False,data_start=0,names=['date_start','date_end','program','extra'])
return(Blocks)
def calc_blocks(Blocks,sim_start,sim_end):
'''**(Only used for old version of script prior to switch to remote observing)**
Calculates observing blocks in MJD, and keeps track of which program has priority.'''
OurBlocks=[]
firstprog=[]
for kk in range(0,len(Blocks)):
startBMJD=Time(str(Blocks[kk]['date_start'])[0:4]+'-'+str(Blocks[kk]['date_start'])[4:6]+'-'+str(Blocks[kk]['date_start'])[6:8], format='iso', scale='utc').mjd
endBMJD=Time(str(Blocks[kk]['date_end'])[0:4]+'-'+str(Blocks[kk]['date_end'])[4:6]+'-'+str(Blocks[kk]['date_end'])[6:8], format='iso', scale='utc').mjd
if (startBMJD >= Time(sim_start, format='iso', scale='utc').mjd) and (endBMJD <= Time(sim_end, format='iso', scale='utc').mjd):
OurBlocks.append(Blocks[kk])
if Blocks[kk]['extra']=='none':
firstprog.append([Blocks[kk]['program']])
else:
firstprog.append([Blocks[kk]['program'],Blocks[kk]['extra']])
return(OurBlocks,firstprog)
def create_blocks(startdate,enddate):
'''Simulate a JCMT schedule, based on the average nights in each queue per semester, and writes to a file.
We start at the sim_start date, cycle through the sequence PI, LAP, PI, LAP, PI, LAP, PI, LAP, UH, DDT, where
PI and LAP are 5 night blocks, UH is a 4 night block, and DDT is one night, and end at sim_end date.
NOTE: M20AL008 is a ToO and allowed to run on LAP, PI, and DDT nights, but not during the UH queue.
'''
sdate = date(int(startdate.split('-')[0]), int(startdate.split('-')[1]), int(startdate.split('-')[2]))
edate = date(int(enddate.split('-')[0]), int(enddate.split('-')[1]), int(enddate.split('-')[2]))
delta = edate - sdate
blocks1=[]
blocks2=[]
priority=[]
sequence=['PI','LAP','PI','LAP','PI','LAP','PI','LAP','UH','DDT']
#sequence=['LAP','LAP','LAP','LAP','LAP','LAP','LAP','LAP','UH','DDT']
list_cycle = cycle(sequence)
current=sdate
for i in range(delta.days + 1):
nex=next(list_cycle)
if current < (edate):
if nex in ['PI','LAP']:
blo=5
elif nex=='UH':
blo=4
else:
blo=1
if i==0:
day1 = sdate + timedelta(days=i)
else:
day1 = day2
day2 = day1 + timedelta(days=blo)
if day2 > edate:
day2 = edate
blocks1.append(day1.strftime("%Y%m%d"))
blocks2.append(day2.strftime("%Y%m%d"))
priority.append(nex)
current=day2
ascii.write([blocks1,blocks2,priority],path_dir+'model_obs_blocks.txt',names=['date_start','date_end','program'])
def read_cal(table,cal_table):
'''Reads in calibrator data files.
NOTE - this calibrater method is not currently implemented.'''
cal=table['target name'][np.where(table['fraction of time observable']==np.max(table['fraction of time observable']))[0][0]]
ind=np.where(table['target name']==cal)[0][0]
ra=str(cal_table['col2'][ind])+'h'+str(cal_table['col3'][ind])+'m'+str(cal_table['col4'][ind])+'s'
dec=str(cal_table['col5'][ind])+'d'+str(cal_table['col6'][ind])+'m'+str(cal_table['col7'][ind])+'s'
return cal,ra,dec
def pick_cals(day,obsn,path_dir):
'''Finds calibrators that are best observable on a particular night.
NOTE - this calibrater method is not currently implemented.'''
callst1=[]
names1=[]
callst2=[]
names2=[]
time_range1 = Time([Time((day+1), format='mjd', scale='utc').iso.split(' ')[0]+" 03:30",\
Time((day+1), format='mjd', scale='utc').iso.split(' ')[0]+" 10:30"])
if obsn==17.:
time_range2 = Time([Time((day+1), format='mjd', scale='utc').iso.split(' ')[0]+" 10:30",\
Time((day+1), format='mjd', scale='utc').iso.split(' ')[0]+" 20:30"])
elif obsn==13.:
time_range2 = Time([Time((day+1), format='mjd', scale='utc').iso.split(' ')[0]+" 10:30",\
Time((day+1), format='mjd', scale='utc').iso.split(' ')[0]+" 16:30"])
cal_files=[path_dir+'Harp_cal.txt',path_dir+'SCUBA2_cals.txt',path_dir+'pointing.txt']
for ii in range(0,len(cal_files)):
cal_table=ascii.read(cal_files[ii])
jcmt=Observer.at_site("JCMT")
constraints = [AltitudeConstraint(30*u.deg, 80*u.deg,boolean_constraint=True)]
targets=[]
for i in range(0,len(cal_table['col1'])):
targets.append(FixedTarget(coord=SkyCoord(ra=str(cal_table['col2'][i])+'h'+str(cal_table['col3'][i])+'m'+str(cal_table['col4'][i])+'s',\
dec=str(cal_table['col5'][i])+'d'+str(cal_table['col6'][i])+'m'+str(cal_table['col7'][i])+'s'),name=cal_table['col1'][i]))
table1 = observability_table(constraints, jcmt, targets, time_range= time_range1)
cal1,ra1,dec1=read_cal(table1,cal_table)
table2 = observability_table(constraints, jcmt, targets, time_range= time_range2)
cal2,ra2,dec2=read_cal(table2,cal_table)
callst1.append((cal1,ra1,dec1))
names1.append('CAL_'+cal1)
callst2.append((cal2,ra2,dec2))
names2.append('CAL_'+cal2)
return callst1,names1,callst2,names2
def pick_fault_targets(day,obsn,path_dir):
'''Finds calibrators that are best observable on a particular night for use in fault blocks.'''
callst1=[]
names1=[]
callst2=[]
names2=[]
time_range1 = Time([Time((day+1), format='mjd', scale='utc').iso.split(' ')[0]+" 03:30",\
Time((day+1), format='mjd', scale='utc').iso.split(' ')[0]+" 10:30"])
if obsn==17.:
time_range2 = Time([Time((day+1), format='mjd', scale='utc').iso.split(' ')[0]+" 10:30",\
Time((day+1), format='mjd', scale='utc').iso.split(' ')[0]+" 20:30"])
elif obsn==13.:
time_range2 = Time([Time((day+1), format='mjd', scale='utc').iso.split(' ')[0]+" 10:30",\
Time((day+1), format='mjd', scale='utc').iso.split(' ')[0]+" 16:30"])
cal_file=path_dir+'pointing.txt'
cal_table=ascii.read(cal_file)
jcmt=Observer.at_site("JCMT")
constraints = [AltitudeConstraint(30*u.deg, 80*u.deg,boolean_constraint=True)]
targets=[]
for i in range(0,len(cal_table['col1'])):
targets.append(FixedTarget(coord=SkyCoord(ra=str(cal_table['col2'][i])+'h'+str(cal_table['col3'][i])+'m'+str(cal_table['col4'][i])+'s',\
dec=str(cal_table['col5'][i])+'d'+str(cal_table['col6'][i])+'m'+str(cal_table['col7'][i])+'s'),name=cal_table['col1'][i]))
table1 = observability_table(constraints, jcmt, targets, time_range= time_range1)
cal1,ra1,dec1=read_cal(table1,cal_table)
table2 = observability_table(constraints, jcmt, targets, time_range= time_range2)
cal2,ra2,dec2=read_cal(table2,cal_table)
callst1.append((cal1,ra1,dec1))
names1.append('CAL_'+cal1)
callst2.append((cal2,ra2,dec2))
names2.append('CAL_'+cal2)
return callst1,names1,callst2,names2
def get_cal_times(obsn,day,half):
'''Creates 15min time-blocks for calibrator sources each hour of the night,
giving 25% of observing night to calibrators.
NOTE - this calibrater method is not currently implemented.'''
arr=[]
if half==1:
st0=Time(Time((day+1), format='mjd', scale='utc').iso.split(' ')[0]+" 04:15")
st1=Time(Time((day+1), format='mjd', scale='utc').iso.split(' ')[0]+" 04:30")
elif half==2:
st0=Time(Time((day+1), format='mjd', scale='utc').iso.split(' ')[0]+" 11:15")
st1=Time(Time((day+1), format='mjd', scale='utc').iso.split(' ')[0]+" 11:30")
for i in range(0,int(obsn)):
t1=(st0).datetime+(i)*datetime.timedelta(hours=1)
t2=(st1).datetime+(i)*datetime.timedelta(hours=1)
time_range=Time([str(t1),str(t2)])
arr.append(TimeConstraint(time_range[0], time_range[1]))
return(arr)
def get_time_blocks(obsn,day,jcmt):
'''Converts UT time blocks to LST blocks.'''
lst_list=[]
st0=Time(Time((day+1), format='mjd', scale='utc').iso.split(' ')[0]+" 03:30")
st1=Time(Time((day+1), format='mjd', scale='utc').iso.split(' ')[0]+" 04:30")
for i in range(0,int(obsn)):
t1=(st0).datetime+(i)*datetime.timedelta(hours=1)
t2=(st1).datetime+(i)*datetime.timedelta(hours=1)
lst_list.append((Time(str(Time(t1)),format='iso',scale='utc',location=(jcmt.location.lon,jcmt.location.lat)).sidereal_time('apparent').value,\
Time(str(Time(t2)),format='iso',scale='utc',location=(jcmt.location.lon,jcmt.location.lat)).sidereal_time('apparent').value))
return(lst_list)
def get_wvm_data(sim_start,sim_end,flag,path_dir,wvmfile=''):
'''Get WVM weather data.'''
sim_years=int(ma.ceil(abs((Time(sim_start,format='iso').datetime-Time(sim_end,format='iso').datetime).total_seconds())/(3600.*24*365.)))
if flag=='fetch':
hoursstart=4#6pmHST
hoursend=16#6amHST
#sim_years=4
prev_years=Time(sim_start,format='iso').datetime.year-sim_years
prev_yeare=Time(sim_end,format='iso').datetime.year-sim_years
#print(prev_years)
startdatewvm=Time(str(prev_years)+'-'+sim_start.split('-')[1]+'-'+sim_start.split('-')[2],format='iso').datetime
enddatewvm=Time(str(prev_yeare)+'-'+'12-31',format='iso').datetime
wvmvalues=get_wvm_fromdisk(startdatewvm,enddatewvm)
wvmvalues=wvmvalues[['finalTau']]
with open(path_dir+'program_details_sim/writewvm.csv','w') as f:
wvmvalues.to_csv(f)
wvmvalues=pd.read_csv(path_dir+'program_details_sim/writewvm.csv',index_col='isoTime',parse_dates=['isoTime'])
hours=wvmvalues.index.hour+(wvmvalues.index.minute/60.0)
nightlywvm=wvmvalues[(hours>=hoursstart) & (hours<=hoursend)]
data_daily = get_sampled_values(nightlywvm, 'finalTau',samplerate='D')
with open(path_dir+'program_details_sim/writewvm_daily.csv','w') as f:
data_daily.to_csv(f)
data_daily=ascii.read(path_dir+'program_details_sim/writewvm_daily.csv',format='csv')
elif flag=='file':
data_daily=ascii.read(wvmfile,format='csv')
mjd_wvm = (Time(data_daily['isoTime'], format='iso', scale='utc').mjd)+(365.*sim_years)
#tau = data_daily['median']
mjd_predict=np.arange(Time(sim_start, format='iso', scale='utc').mjd,\
Time(sim_end, format='iso', scale='utc').mjd,1)
tau_predict=[]
for mjd in mjd_predict:
if mjd in mjd_wvm:
tau_predict.append(data_daily['median'][np.where(mjd_wvm==mjd)[0][0]])
else:
tau_predict.append(0.2)
tau_predict_array=np.array(tau_predict)
tau_predict_array[np.isnan(tau_predict_array)]=0.2
return(mjd_predict,tau_predict_array)
def good_blocks(Blocks,mjd_predict,tau_predict):
'''Get MJDs and weather for the observing blocks.'''
start=Blocks['date_start']
end=Blocks['date_end']
startmjd=Time(str(start)[0:4]+'-'+str(start)[4:6]+'-'+str(start)[6:8], format='iso', scale='utc').mjd
endmjd=Time(str(end)[0:4]+'-'+str(end)[4:6]+'-'+str(end)[6:8], format='iso', scale='utc').mjd
dates=np.arange(startmjd,endmjd+1,1)
obs_mjd=mjd_predict[[i for i, item in enumerate(mjd_predict) if item in dates]]
tau_mjd=tau_predict[[i for i, item in enumerate(mjd_predict) if item in dates]]
return(obs_mjd,tau_mjd)
def bad_block(instrument,SCUBA_2_unavailable,HARP_unavailable,RUA_unavailable):
'''Returns the proper list of unavailable dates based on instrument.'''
if instrument=='SCUBA-2':
checklst=SCUBA_2_unavailable
elif instrument=='HARP':
checklst=HARP_unavailable
elif instrument in ['UU','RXA3M','AWEOWEO']:
checklst=RUA_unavailable
else:
raise ValueError('Instrument unavailable.')
return checklst
def get_wband(tau):
'''Matches tau values to a weather band. Note Band 1 is set as 0.055 to take into account the PIs who put 0.055 as a Band 1 limit!'''
if tau<=0.055:
wb='Band 1'
elif tau<=0.08 and tau>0.055:
wb='Band 2'
elif tau<=0.12 and tau>0.08:
wb='Band 3'
elif tau<=0.2 and tau>0.12:
wb='Band 4'
elif tau>0.2:
wb='Band 5'
return(wb)
def get_m16al001_time(tau):
'''Some programs (e.g., M16AL001) MSBs can be run in different weather bands, and the MSB files only reflect one Band (e.g., Band 3) time.
Here we select the proper MSB time depending on the weather band for the night.'''
if tau<=0.055:
time=1320.
elif tau<=0.08 and tau>0.055:
time=1920.
elif tau<=0.12 and tau>0.08:
time=2520.
return(time)
def get_lst(sched,unused_ind,jcmt):
'''Converts UT times of unused blocks in a schedule to LST times.'''
lst_list=[]
start=sched['start time (UTC)'][unused_ind]
end=sched['end time (UTC)'][unused_ind]
for i in range(0,len(start)):
lst_list.append((Time(start[i],format='iso',scale='utc',location=(jcmt.location.lon,jcmt.location.lat)).sidereal_time('apparent').value,\
Time(end[i],format='iso',scale='utc',location=(jcmt.location.lon,jcmt.location.lat)).sidereal_time('apparent').value))
return(lst_list)
def priority_choose(tau,m,tau_max,LAPprograms):
'''Calculates scaled priorities of MSBs.'''
#priority goes (1) faults [so they always happen at same rate], (2) M20AL008 ToO program, (3) weather band, (4) overall priority
tab=LAPprograms['projectid','tagpriority']
tab['scaled_p']=[i for i in range(1,len(LAPprograms['projectid'])+1)]
p=tab['scaled_p'][np.where(tab['projectid']==m)[0]]
WBandDay=int(get_wband(tau).split(' ')[1])
WBandTar=int(get_wband(tau_max).split(' ')[1])
if m.lower=='m20al008':
priority=2
else:
if WBandDay==WBandTar:
priority=3+p
else:
priority=(2*len(LAPprograms['projectid'])+WBandTar+3)+p
return priority
def bin_lst(bin_start,bin_end,lst_tally):
'''Bins LST blocks for histogram creation.'''
bin_hrs=np.zeros(len(bin_start))
for i in range(0,len(bin_start)):
for j in range(0,len(lst_tally)):
if bin_start[i] <= lst_tally[j][0] <= bin_end[i] and bin_start[i] <= lst_tally[j][1] <= bin_end[i]:
bin_hrs[i]=bin_hrs[i]+(lst_tally[j][1]-lst_tally[j][0])
elif bin_start[i] <= lst_tally[j][0] <= bin_end[i] and lst_tally[j][1] > bin_end[i]:
bin_hrs[i]=bin_hrs[i]+(bin_end[i]-lst_tally[j][0])
elif bin_start[i] <= lst_tally[j][1] <= bin_end[i] and lst_tally[j][0] < bin_start[i]:
bin_hrs[i]=bin_hrs[i]+(lst_tally[j][1]-bin_start[i])
return(bin_hrs)
def elevationcheck(jcmt,mjd,target):
'''Some programs (e.g., M17BL002) can have low elevation sources, so this checks if they transit below 40 deg,
and adjusts elevation limit for these sources.'''
date=Time(mjd,format='mjd').iso.split(' ')[0]
airmass=jcmt.altaz(Time(date)+np.linspace(-12,12,100)*u.hour,target).secz
amin=np.min(airmass[airmass>1]).value
elev=np.degrees(np.pi/2 -np.arccos(1./amin))
if elev < 40.:
constraint=12.
else:
constraint=30.
return(constraint)
def check_semester(mjd):
'''Returns the appropriate semester string (e.g., 2020A) given an MJD date
'''
year=Time(mjd,format='mjd',scale='utc').iso.split('-')[0]
month=Time(mjd,format='mjd',scale='utc').iso.split('-')[1]
if month == '01':
sem=str(int(year)-1)+'B'
elif month in ['02','03','04','05','06','07']:
sem=year+'A'
else:
sem=year+'B'
return(sem)
def predict_time(sim_start,sim_end,LAPprograms,Block,path_dir,total_observed,FP,m16al001_tally,m20al008_tally,\
SCUBA_2_unavailable,HARP_unavailable,RUA_unavailable,wb_usage_tally,cal_tally,tot_tally,nothing_obs,lst_tally,\
finished_dates,status,mjd_predict,tau_predict):
'''Simulates observations of Large Programs over specified observing block.'''
#fetch wvm data from previous year(s)
#mjd_predict,tau_predict=get_wvm_data(sim_start,sim_end,flag,path_dir,wvmfile)
#get dates for observing block, make arrays of MJD and tau for these days
obs_mjd,tau_mjd=good_blocks(Block,mjd_predict,tau_predict)
#set up observatory site and general elevation constraints
jcmt=Observer.at_site("JCMT",timezone="US/Hawaii")
print('block:',obs_mjd)
#loop over all days in the current observing block
for k in range(0,len(obs_mjd)):
print('day:',obs_mjd[k])
pb_targets=[]
#A standard observing night will run from 5:30pm HST to 6:30am HST (13 hrs; times below are UTC!)
#if tau is at band 3 or better, EO is scheduled, and we observe till 10:30am HST (17 hrs)
if tau_mjd[k] < 0.12:
time_range = Time([Time((obs_mjd[k]+1), format='mjd', scale='utc').iso.split(' ')[0]+" 03:30",\
Time((obs_mjd[k]+1), format='mjd', scale='utc').iso.split(' ')[0]+" 20:30"])
obsn=17.
#set up general elevation constraints and sun avoidance constraints for when we are in EO
constraints = [AltitudeConstraint(min=0*u.deg),SunSeparationConstraint(min=45*u.deg)]
else:
time_range = Time([Time((obs_mjd[k]+1), format='mjd', scale='utc').iso.split(' ')[0]+" 03:30",\
Time((obs_mjd[k]+1), format='mjd', scale='utc').iso.split(' ')[0]+" 16:30"])
obsn=13.
#set up general elevation constraints
constraints = [AltitudeConstraint(min=0*u.deg)]
# we will only count the LAP nights in the tally of availale time for large programs, even though M20AL008 can run in the
# PI and DDT queues as well.
if FP=='LAP':
tot_tally.append(obsn)
WBand=get_wband(tau_mjd[k])
wb_usage_tally['Available'][WBand].append(obsn)
#make a target list for the night (we keep track of target, MSB time, priority, and program),
#looping over each target in each program
targets=[]
priority=[]
msb_time=[]
prog=[]
tc=[]
ta=[]
configu=[]
for m in LAPprograms['projectid']:
if LAPprograms['taumax'][np.where(LAPprograms['projectid']==m.upper())[0]] >= tau_mjd[k]:
msbs=ascii.read(path_dir+'program_details_sim/'+m.lower()+'-project-info.list')
target_table=msbs['target','ra2000', 'dec2000']
obs_time_table=msbs['target','timeest','remaining','taumin','taumax','instrument']
#targets are added if they meet all of the following requirments:
#(a) Target has MSB repeats remaining
#(b) The night is not in the blackout dates for the instrument
#(c) The weather is appropriate
for j in range(0,len(target_table['target'])):
blackout_dates=bad_block(obs_time_table['instrument'][j],SCUBA_2_unavailable,HARP_unavailable,RUA_unavailable)
if (obs_time_table['remaining'][j] >0 and obs_time_table['taumax'][j] >= tau_mjd[k] and obs_mjd[k] not in blackout_dates):
#The m16al001/m20AL007 program is to run on a monthly basis, so if we are dealing with that
#program we must check whether each target has been observed in the current month yet.
#The m20al008 program is a ToO (can run in LAP, PI and DDT queues), 6 targets, 2 of which will do 16x4hour observations and 4 will do 8x4hours observations.
#So, we restrict to one 4 hour obs per night, 1 source campaign to start per 6 month semeseter
if m.upper() not in ['M16AL001','M20AL007','M20AL008'] and FP=='LAP':
for jj in range(0,obs_time_table['remaining'][j]):
targets.append(FixedTarget(coord=SkyCoord(ra=target_table['ra2000'][j]*u.rad,\
dec=target_table['dec2000'][j]*u.rad),name=target_table['target'][j]))
tc.append(TimeConstraint(time_range[0], time_range[1]))
#if in M17BL002/M20AL014, and transits below 40 deg, set the elevation limit to 15 rather than 30 for the target so it is observed
if m in ['M17BL002','M20AL014']:
el=elevationcheck(jcmt,obs_mjd[k],FixedTarget(coord=SkyCoord(ra=target_table['ra2000'][j]*u.rad,dec=target_table['dec2000'][j]*u.rad),name=target_table['target'][j]))
ta.append(AltitudeConstraint(min=el*u.deg))
else:
ta.append(AltitudeConstraint(min=30*u.deg))
#assign priority
priority.append(priority_choose(tau_mjd[k],m,obs_time_table['taumax'][j],LAPprograms))
if obs_time_table['instrument'][j] == 'RXA3M':
msb_time.append((1.25/2.2)*obs_time_table['timeest'][j]*u.second)
else:
msb_time.append(1.25*obs_time_table['timeest'][j]*u.second)
configu.append(m.lower()+'_'+str(msbs['msbid'][j])+'_'+str(j)+'_'+str(jj))
prog.append(m.lower())
elif m.upper() in ['M16AL001','M20AL007'] and FP=='LAP':
#print('tar2',target_table['target'][j])
#we keep track of the dates each target in the m16al001 program is observed through the m16al001_tally dictionary,
#so we need to first check if the target is present in the dictionary yet
dates_obs=[all(getattr(Time(obs_mjd[k], format='mjd', scale='utc').datetime,x)==getattr(mon.datetime,x) for x in ['year','month']) for mon in m16al001_tally[target_table['target'][j]]]
if target_table['target'][j] not in m16al001_tally.keys():
targets.append(FixedTarget(coord=SkyCoord(ra=target_table['ra2000'][j]*u.rad,\
dec=target_table['dec2000'][j]*u.rad),name=target_table['target'][j]))
tc.append(TimeConstraint(time_range[0], time_range[1]))
#assign priority
priority.append(priority_choose(tau_mjd[k],m,obs_time_table['taumax'][j],LAPprograms))
msb_time.append(1.25*get_m16al001_time(tau_mjd[k])*u.second)
configu.append(m.lower()+'_'+str(msbs['msbid'][j])+'_'+str(j))
prog.append(m.lower())
ta.append(AltitudeConstraint(min=30*u.deg))
else:
#then if present, check if the target has been observed in the current nights month/year combo yet
if not any(dates_obs):
targets.append(FixedTarget(coord=SkyCoord(ra=target_table['ra2000'][j]*u.rad,\
dec=target_table['dec2000'][j]*u.rad),name=target_table['target'][j]))
tc.append(TimeConstraint(time_range[0], time_range[1]))
#assign priority
priority.append(priority_choose(tau_mjd[k],m,obs_time_table['taumax'][j],LAPprograms))
msb_time.append(1.25*get_m16al001_time(tau_mjd[k])*u.second)
configu.append(m.lower()+'_'+str(msbs['msbid'][j])+'_'+str(j))
prog.append(m.lower())
ta.append(AltitudeConstraint(min=30*u.deg))
elif m.upper() =='M20AL008' and FP in ['LAP','PI','DDT']:
# get current semester
current_sem=check_semester(obs_mjd[k])
#if no targets have been observed yet, or a target has already started a campaign, add it to target list
if target_table['target'][j] in m20al008_tally.keys():
#pb_targets.append(target_table['target'][j])
targets.append(FixedTarget(coord=SkyCoord(ra=target_table['ra2000'][j]*u.rad,\
dec=target_table['dec2000'][j]*u.rad),name=target_table['target'][j]))
tc.append(TimeConstraint(time_range[0], time_range[1]))
priority.append(priority_choose(tau_mjd[k],m,obs_time_table['taumax'][j],LAPprograms))
msb_time.append(1.25*obs_time_table['timeest'][j]*u.second)
configu.append(m.lower()+'_'+str(msbs['msbid'][j])+'_'+str(j))
prog.append(m.lower())
ta.append(AltitudeConstraint(min=30*u.deg))
#if a target has not started a campaign yet, check if another target is being observed already this current semester
#only add to target list if this is not the case
#During PI/DDT blocks, m20al008 is only program available. To prevent more then one target being observed in one night,
#we ensure that only one observable target is added to the target list.
elif target_table['target'][j] not in m20al008_tally.keys():
if not any(x==current_sem for x in list(m20al008_tally.values())) and len(pb_targets)==0:
targets.append(FixedTarget(coord=SkyCoord(ra=target_table['ra2000'][j]*u.rad,\
dec=target_table['dec2000'][j]*u.rad),name=target_table['target'][j]))
tc.append(TimeConstraint(time_range[0], time_range[1]))
priority.append(priority_choose(tau_mjd[k],m,obs_time_table['taumax'][j],LAPprograms))
msb_time.append(1.25*obs_time_table['timeest'][j]*u.second)
configu.append(m.lower()+'_'+str(msbs['msbid'][j])+'_'+str(j))
prog.append(m.lower())
ta.append(AltitudeConstraint(min=30*u.deg))
tarpb=[FixedTarget(coord=SkyCoord(ra=target_table['ra2000'][j]*u.rad,\
dec=target_table['dec2000'][j]*u.rad),name=target_table['target'][j])]
frac_obspb=np.array(observability_table([AltitudeConstraint(min=30*u.deg)], jcmt, tarpb, time_range=time_range,time_grid_resolution=0.75*u.hour)['fraction of time observable'])
if frac_obspb[0]>=4./obsn:
pb_targets.append(target_table['target'][j])
#check if at least one target has been added to our potential target list
if len(targets)>0:
#check at least one potential target is observable at some point in the night
ever_observable = is_observable([AltitudeConstraint(min=30*u.deg)], jcmt, targets, time_range=time_range,time_grid_resolution=0.75*u.hour)
frac_obs=observability_table([AltitudeConstraint(min=30*u.deg)], jcmt, targets, time_range=time_range,time_grid_resolution=0.75*u.hour)['fraction of time observable']
#add in some fault blocks - pick a pointing cal best observable for each half night and schedule a 1.5% of total night block == 3% total fault rate
callst1,names1,callst2,names2=pick_fault_targets(obs_mjd[k],obsn,path_dir)
mid_time=Time(Time((obs_mjd[k]+1), format='mjd', scale='utc').iso.split(' ')[0]+" 10:00")
targets.append(FixedTarget(coord=SkyCoord(ra=callst1[0][1],\
dec=callst1[0][2]),name='FAULT'))
msb_time.append(0.015*obsn*3600.*u.second)
priority.append(1)
tc.append(TimeConstraint(time_range[0], mid_time))
prog.append('FAULT')
ta.append(AltitudeConstraint(min=0*u.deg))
configu.append('FAULT')
targets.append(FixedTarget(coord=SkyCoord(ra=callst2[0][1],\
dec=callst2[0][2]),name='FAULT'))
msb_time.append(0.015*obsn*3600.*u.second)
priority.append(1)
tc.append(TimeConstraint(mid_time, time_range[1]))
prog.append('FAULT')
ta.append(AltitudeConstraint(min=0*u.deg))
configu.append('FAULT')
#pick a HARP, SCUBA-2, and pointing calibrator for each half-night, and add to target list
'''(optional) schedule calibrators every hour
#calibrators are chosen based on observability each half-night
callst1,names1,callst2,names2=pick_cals(obs_mjd[k],obsn,path_dir)
names=names1+names2
repeats=7
repeats2=int(obsn-repeats)
#15min of every hour is set aside for calibrators (25% of night)
cal_times1=get_cal_times(repeats,obs_mjd[k],1)
cal_times2=get_cal_times(repeats2,obs_mjd[k],2)
for ii in range(0,len(callst1)):
for iii in range(0,repeats):
targets.append(FixedTarget(coord=SkyCoord(ra=callst1[ii][1],\
dec=callst1[ii][2]),name='CAL_'+callst1[ii][0]))
msb_time.append(3*60.*u.second)
#calibrators given highest priority (1) to ensure they are scheduled
priority.append(1)
tc.append(cal_times1[iii])
if ii==0:
prog.append('HARP cal')
elif ii==1:
prog.append('SCUBA-2 cal')
elif ii==2:
prog.append('Pointing/Focus cal')
for ii in range(0,len(callst2)):
for iii in range(0,repeats2):
targets.append(FixedTarget(coord=SkyCoord(ra=callst2[ii][1],\
dec=callst2[ii][2]),name='CAL_'+callst2[ii][0]))
msb_time.append(3*60.*u.second)
priority.append(1)
tc.append(cal_times2[iii])
if ii==0:
prog.append('HARP cal')
elif ii==1:
prog.append('SCUBA-2 cal')
elif ii==2:
prog.append('Pointing/Focus cal')'''
else:
ever_observable = [False]
frac_obs=[0.]
#As long as we have an observable target, we proceed to scheduling for the night.
#In the LAP queue this means a target can be observed for at least 45 min (typical average MSB time)
#In PI or DDT queue, M20AP008 is oly program that can run, and we need 4 hours on source.
if FP in ['PI','DDT']:
ff=4.
elif FP=='LAP':
ff=0.75
if np.any(ever_observable) and any(frac>=ff/obsn for frac in frac_obs):
#set the slew rate of telescope between sources
slew_rate = 1.2*u.deg/u.second
transitioner = Transitioner(slew_rate)#, {'program':{'default':2*u.minute}})
# set up the schedule for the night
scheduler = JCMTScheduler(constraints = constraints,observer = jcmt,\
transitioner = transitioner,time_resolution=30*u.minute,gap_time=30*u.minute)
schedule = Schedule(time_range[0], time_range[1])
#create observing blocks for each target based on target list made above (these act just like normal MSBs at JCMT)
bnew=[]
for i in range(0,len(targets)):
bnew.append(ObservingBlock(targets[i],msb_time[i], priority[i], configuration={'program' : prog[i]},\
constraints=[tc[i],ta[i]]))
#run the astroplan priority scheduling tool
scheduler(bnew, schedule)
sched=schedule.to_table(show_unused=True)
#print out schedule to file for records
namemjd=str(Time(obs_mjd[k],format='mjd').datetime.year)+\
str(Time(obs_mjd[k],format='mjd').datetime.month)+str(Time(obs_mjd[k],format='mjd').datetime.day)
ascii.write([sched['target'],sched['start time (UTC)'],sched['end time (UTC)'],sched['duration (minutes)']],\
path_dir+'sim_results/schedules/'+namemjd+'.txt',names=['target','start time (UTC)','end time (UTC)','duration (min)'])
#keep track of unused time
WBand=get_wband(tau_mjd[k])
unused_ind=np.where(np.logical_or(np.logical_or(sched['target']=='Unused Time',sched['target']=='TransitionBlock'),sched['target']=='FAULT'))[0]
unused=np.sum(np.array(sched['duration (minutes)'][unused_ind]))/60.
lst_list=get_lst(sched,unused_ind,jcmt)
if FP=='LAP':
wb_usage_tally['Unused'][WBand].append(unused)
lst_tally[WBand].extend(lst_list)
#uncomment if selecting calibrators specifically
#caltime=np.sum(np.array(sched['duration (minutes)'])[[p for p,n in enumerate(np.array(sched['target'])) if n in names]])/60.
#wb_usage_tally['Cal'][WBand].append(caltime)
#cal_tally.append(caltime)
#FOR TESTING ONLY--
#only plot schedule when at least one target is observed
#sometimes the plotting tools fails, we catch this with a try/except
fig=plt.figure(figsize = (14,10))
try:
plot_schedule_airmass(schedule,show_night=True)
plt.title(WBand)
lgd=plt.legend(loc = "upper right",ncol=10,fontsize=6,bbox_to_anchor=(1.05,1.15))
plt.savefig(path_dir+'sim_results/schedules/'+namemjd+'.png',bbox_tight='inches',bbox_extra_artists=(lgd,))
plt.close()
except TypeError:
print('no schedule plotted')
#record what targets have been observed, updating the MSB files and recording total time observed for each program
for h in range(0,len(np.unique(sched['target']))):
tar=np.unique(sched['target'])[h]
if (tar not in ['TransitionBlock','Unused Time','FAULT'] and 'CAL' not in tar):
prog=sched['configuration'][np.where(sched['target']==tar)[0][0]]['program']
msbs=ascii.read(path_dir+'program_details_sim/'+prog+'-project-info.list')
num_used=len(np.array(sched['duration (minutes)'][np.where(sched['target']==tar)[0]]))
if prog=='m16al001' or prog=='m20al007':
tim_used=float(msbs['timeest'][np.where(msbs['target']==tar)[0][0]])/3600.
m16al001_tally[tar].append(Time(obs_mjd[k],format='mjd',scale='utc'))
elif prog=='m20al008':
tim_used=float(msbs['timeest'][np.where(msbs['target']==tar)[0][0]])/3600.
if tar not in list(m20al008_tally.keys()):
m20al008_tally[tar]=check_semester(obs_mjd[k])
else:
tim_used=np.sum(np.array(sched['duration (minutes)'][np.where(sched['target']==tar)[0]]))/60./1.25
if tim_used >0:
r0=msbs['remaining'][np.where(msbs['target']==tar)[0]]-num_used
oc0=msbs['obscount'][np.where(msbs['target']==tar)[0]]+num_used
t0=np.round(msbs['msb_total_hrs'][np.where(msbs['target']==tar)[0]]-tim_used,5)
msbs['remaining'][np.where(msbs['target']==tar)[0]]=r0
msbs['obscount'][np.where(msbs['target']==tar)[0]]=oc0
msbs['msb_total_hrs'][np.where(msbs['target']==tar)[0]]=t0
ascii.write([msbs['projectid'],msbs['msbid'],msbs['remaining'],msbs['obscount'],\
msbs['timeest'],msbs['msb_total_hrs'],msbs['instrument'],
msbs['type'],msbs['pol'],msbs['target'],msbs['ra2000'],msbs['dec2000'],\
msbs['taumin'],msbs['taumax']],\
path_dir+'program_details_sim/'+prog+'-project-info.list', names=msbs.colnames)
total_observed[prog.upper()]=total_observed[prog.upper()]+(tim_used)
WBand=get_wband(tau_mjd[k])
wb_usage_tally['Used'][WBand].append(tim_used)
caltime=(tim_used)*0.25#np.sum(np.array(sched['duration (minutes)'])[[p for p,n in enumerate(np.array(sched['target'])) if n in names]])/60.
wb_usage_tally['Cal'][WBand].append(caltime)
cal_tally.append(caltime)