-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdo_manage.py
1175 lines (936 loc) · 37.3 KB
/
do_manage.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
import time
import numpy as np
import pandas as pd
import healpy as hp
from astropy.table import Table
from astropy.io import fits
from astropy.wcs import WCS
from scipy import interpolate
import os, socket, subprocess, shlex
import argparse
import logging, traceback
import paramiko
from helper_funcs import send_email, send_error_email, send_email_attach, send_email_wHTML
from sqlite_funcs import get_conn
from dbread_funcs import get_files_tab, get_info_tab, guess_dbfname
from coord_conv_funcs import convert_radec2imxy, convert_imxy2radec
def cli():
parser = argparse.ArgumentParser()
parser.add_argument('--evfname', type=str,\
help="Event data file",
default=None)
parser.add_argument('--fp_dir', type=str,\
help="Directory where the detector footprints are",
default='/storage/work/jjd330/local/bat_data/rtfp_dir_npy/')
parser.add_argument('--Nrate_jobs', type=int,\
help="Total number of jobs",
default=16)
parser.add_argument('--TSscan', type=float,\
help="Min TS needed to do a full FoV scan",
default=6.25)
parser.add_argument('--pix_fname', type=str,\
help="Name of the file with good imx/y coordinates",\
default='good_pix2scan.npy')
parser.add_argument('--bkg_fname', type=str,\
help="Name of the file with the bkg fits",\
default='bkg_estimation.csv')
parser.add_argument('--dbfname', type=str,\
help="Name to save the database to",\
default=None)
parser.add_argument('--GWname', type=str,\
help="Name of the event to submit jobs as",\
default='')
parser.add_argument('--queue', type=str,\
help="Name of the queue to submit jobs to",\
default='cyberlamp')
parser.add_argument('--qos', type=str,\
help="Name of the qos to submit jobs to",\
default=None)
parser.add_argument('--pcfname', type=str,\
help="Name of the partial coding image",\
default='pc_2.img')
parser.add_argument('--BKGpyscript', type=str,\
help="Name of python script for Bkg Estimation",\
default='do_bkg_estimation_wPSs_mp.py')
parser.add_argument('--RATEpyscript', type=str,\
help="Name of python script for Rates analysis",\
default='do_rates_mle_wPSs.py')
parser.add_argument('--LLHpyscript', type=str,\
help="Name of python script for LLH analysis",\
default='do_llh_wPSs_uncoded_realtime.py')
parser.add_argument('--SCANpyscript', type=str,\
help="Name of python script for FoV scan",\
default='do_llh_scan_uncoded.py')
parser.add_argument('--PEAKpyscript', type=str,\
help="Name of python script for FoV scan",\
default='do_intLLH_forPeaks.py')
parser.add_argument('--do_bkg',\
help="Submit the BKG estimation script",\
action='store_true')
parser.add_argument('--do_rates',\
help="Submit the Rate jobs",\
action='store_true')
parser.add_argument('--do_llh',\
help="Submit the llh jobs",\
action='store_true')
parser.add_argument('--do_scan',\
help="Submit the scan jobs",\
action='store_true')
parser.add_argument('--skip_waiting',\
help="Skip waiting for the stuff to finish and use what's there now",\
action='store_true')
parser.add_argument('--archive',\
help="Run in archive mode, not realtime mode",\
action='store_true')
parser.add_argument('--rhel7',\
help="Submit to a rhel7 node",\
action='store_true')
parser.add_argument('--pbs_fname', type=str,\
help="Name of pbs script",\
default='/storage/work/jjd330/local/bat_data/BatML/submission_scripts/pyscript_template.pbs')
parser.add_argument('--min_pc', type=float,\
help="Min partical coding fraction to use",\
default=0.1)
args = parser.parse_args()
return args
def im_dist(imx0, imy0, imx1, imy1):
return np.hypot(imx0 - imx1, imy0 - imy1)
def get_rate_res_fnames(direc='.'):
rate_fnames = [fname for fname in os.listdir(direc) if ('rates' in fname) and (fname[-4:]=='.csv')]
return rate_fnames
def get_res_fnames(direc='.'):
res_fnames = [fname for fname in os.listdir(direc) if (fname[-4:]=='.csv') and (fname[:4]=='res_')]
return res_fnames
def get_scan_res_fnames(direc='.'):
res_fnames = [fname for fname in os.listdir(direc) if (fname[-4:]=='.csv') and ('scan_res_' in fname)]
return res_fnames
def get_peak_res_fnames(direc='.'):
res_fnames = [fname for fname in os.listdir(direc) if (fname[-4:]=='.csv') and ('peak_scan_' in fname)]
return res_fnames
def get_merged_csv_df(csv_fnames):
dfs = []
for csv_fname in csv_fnames:
try:
dfs.append(pd.read_csv(csv_fname, dtype={'timeID':np.int}))
except Exception as E:
logging.error(E)
continue
df = pd.concat(dfs)
return df
def probm2perc(pmap):
bl = (pmap>0)
p_map = np.copy(pmap)
inds_sort = np.argsort(p_map)[::-1]
perc_map = np.zeros_like(p_map)
perc_map[inds_sort] = np.cumsum(p_map[inds_sort])#*\
perc_map[~bl] = 1.
return perc_map
def get_merged_csv_df_wpos(csv_fnames, attfile, perc_map=None, direc=None):
dfs = []
for csv_fname in csv_fnames:
try:
if direc is None:
tab = pd.read_csv(csv_fname, dtype={'timeID':np.int})
else:
tab = pd.read_csv(os.path.join(direc,csv_fname), dtype={'timeID':np.int})
if len(tab) > 0:
# att_ind = np.argmin(np.abs(attfile['TIME'] - trigger_time))
# att_quat = attfile['QPARAM'][att_ind]
# ras = np.zeros(len(tab))
# decs = np.zeros(len(tab))
# for i in xrange(len(ras)):
# # print np.shape(res_tab['time'][i]), np.shape(attfile['TIME'])
# att_ind0 = np.argmin(np.abs(tab['time'][i] + tab['duration'][i]/2. - attfile['TIME']))
# att_quat0 = attfile['QPARAM'][att_ind0]
# ras[i], decs[i] = convert_imxy2radec(tab['imx'][i],\
# tab['imy'][i],\
# att_quat0)
t0_ = np.nanmean(tab['time'] + tab['duration']/2.)
att_ind0 = np.argmin(np.abs(t0_ - attfile['TIME']))
att_quat0 = attfile['QPARAM'][att_ind0]
ras, decs = convert_imxy2radec(tab['imx'], tab['imy'], att_quat0)
tab['ra'] = ras
tab['dec'] = decs
if not perc_map is None:
Nside = hp.npix2nside(len(perc_map))
hp_inds = hp.ang2pix(Nside, ras, decs, lonlat=True, nest=True)
cred_lvl = perc_map[hp_inds]
tab['cls'] = cred_lvl
dfs.append(tab)
except Exception as E:
logging.warning(E)
continue
df = pd.concat(dfs)
return df
def mk_seed_tab4scans(res_tab, pc_fname, rate_seed_tab, TS_min=6.5, im_steps=20, pc_min=0.1):
PC = fits.open(pc_fname)[0]
pc = PC.data
w_t = WCS(PC.header, key='T')
pcbl = (pc>=(pc_min*.99))
pc_inds = np.where(pcbl)
pc_imxs, pc_imys = w_t.all_pix2world(pc_inds[1], pc_inds[0], 0)
imxax = np.linspace(-2,2,im_steps*4+1)
imyax = np.linspace(-1,1,im_steps*2+1)
im_step = imxax[1] - imxax[0]
bins = [imxax, imyax]
h = np.histogram2d(pc_imxs, pc_imys, bins=bins)[0]
inds = np.where(h>=10)
squareIDs_all = np.ravel_multi_index(inds, h.shape)
df_twinds = res_tab.groupby('timeID')
seed_tabs = []
for twind, dft in df_twinds:
if np.max(dft['TS']) >= TS_min:
seed_dict = {}
seed_dict['timeID'] = twind
seed_dict['dur'] = dft['duration'].values[0]
seed_dict['time'] = dft['time'].values[0]
bl_rate_seed = (rate_seed_tab['timeID']==twind)
squareIDs_done = rate_seed_tab['squareID'][bl_rate_seed]
squareIDs = squareIDs_all[~np.isin(squareIDs_all, squareIDs_done)]
seed_dict['squareID'] = squareIDs
seed_tabs.append(pd.DataFrame(seed_dict))
seed_tab = pd.concat(seed_tabs)
return seed_tab
def mk_seed_tab(rates_res, TS_min=3.75, im_steps=20):
imxax = np.linspace(-2,2,im_steps*4+1)
imyax = np.linspace(-1,1,im_steps*2+1)
# imyg, imxg = np.meshgrid((imyax[1:]+imyax[:-1])/2., (imxax[1:]+imxax[:-1])/2.)
im_step = imxax[1] - imxax[0]
bins = [imxax, imyax]
df_twinds = rates_res.groupby('timeID')
seed_tabs = []
for twind, dft in df_twinds:
maxTS = np.nanmax(dft['TS'])
if maxTS >= TS_min:
TS_min2_ = min(maxTS-2.5, .8*maxTS)
TS_min2 = max(TS_min2_, np.nanmedian(dft['TS']), TS_min-.5)
seed_dict = {}
seed_dict['timeID'] = twind
seed_dict['dur'] = dft['dur'].values[0]
seed_dict['time'] = dft['time'].values[0]
pnts = np.vstack([dft['imx'],dft['imy']]).T
TSintp = interpolate.LinearNDInterpolator(pnts, dft['TS'])
imxax = np.linspace(-1.8, 1.8, 8*36+1)
imyax = np.linspace(-1.0, 1.0, 8*20+1)
xgrid, ygrid = np.meshgrid(imxax, imyax)
pnts = np.vstack([xgrid.ravel(),ygrid.ravel()]).T
TSgrid = TSintp(pnts)
bl = (TSgrid>=(TS_min2))
xs = xgrid.ravel()[bl]
ys = ygrid.ravel()[bl]
h = np.histogram2d(xs, ys, bins=bins)[0]
inds = np.where(h>0)
squareIDs = np.ravel_multi_index(inds, h.shape)
seed_dict['squareID'] = squareIDs
seed_tabs.append(pd.DataFrame(seed_dict))
seed_tab = pd.concat(seed_tabs)
return seed_tab
# def mk_seed_tab(rates_res, TS_min=3.5, im_steps=20):
#
# imxax = np.linspace(-2,2,im_steps*4+1)
# imyax = np.linspace(-1,1,im_steps*2+1)
# im_step = imxax[1] - imxax[0]
# bins = [imxax, imyax]
#
# df_twinds = rates_res.groupby('timeID')
# seed_tabs = []
# for twind, dft in df_twinds:
# if np.max(dft['TS']) >= TS_min:
# seed_dict = {}
# seed_dict['timeID'] = twind
# seed_dict['dur'] = dft['dur'].values[0]
# seed_dict['time'] = dft['time'].values[0]
#
# pnts = np.vstack([dft['imx'],dft['imy']]).T
# TSintp = interpolate.LinearNDInterpolator(pnts, dft['TS'])
# imxax = np.linspace(-1.5, 1.5, 8*30+1)
# imyax = np.linspace(-.85, .85, 8*17+1)
# xgrid, ygrid = np.meshgrid(imxax, imyax)
# pnts = np.vstack([xgrid.ravel(),ygrid.ravel()]).T
# TSgrid = TSintp(pnts)
# bl = (TSgrid>=(TS_min-.1))
# xs = xgrid.ravel()[bl]
# ys = ygrid.ravel()[bl]
#
# h = np.histogram2d(xs, ys, bins=bins)[0]
# inds = np.where(h>0)
# squareIDs = np.ravel_multi_index(inds, h.shape)
# seed_dict['squareID'] = squareIDs
# seed_tabs.append(pd.DataFrame(seed_dict))
#
# seed_tab = pd.concat(seed_tabs)
#
# return seed_tab
def mk_job_tab(seed_tab, Njobs, im_steps=20):
imxax = np.linspace(-2,2,im_steps*4+1)
imyax = np.linspace(-1,1,im_steps*2+1)
im_step = imxax[1] - imxax[0]
bins = [imxax, imyax]
squareIDs = np.unique(seed_tab['squareID'])
shp = (len(imxax)-1,len(imyax)-1)
data_dicts = []
for i, squareID in enumerate(squareIDs):
data_dict = {}
data_dict['proc_group'] = i%Njobs
indx, indy = np.unravel_index(squareID, shp)
data_dict['imx0'] = bins[0][indx]
data_dict['imx1'] = bins[0][indx+1]
data_dict['imy0'] = bins[1][indy]
data_dict['imy1'] = bins[1][indy+1]
data_dict['squareID'] = squareID
data_dicts.append(data_dict)
job_tab = pd.DataFrame(data_dicts)
return job_tab
def execute_ssh_cmd(client, cmd, server, retries=5):
tries = 0
while tries < retries:
try:
stdin, stdout, stderr = client.exec_command(cmd)
logging.info("stdout: ")
sto = stdout.read()
logging.info(sto)
return sto
except Exception as E:
logging.error(E)
logging.error(traceback.format_exc())
logging.error("Messed up with ")
logging.error(cmd)
client.close()
client = get_ssh_client(server)
tries += 1
logging.debug("retry %d of %d"%(tries,retries))
return
def get_ssh_client(server, retries=5):
tries = 0
try:
client = paramiko.SSHClient()
client.load_system_host_keys()
except Exception as E:
logging.error(E)
logging.error(traceback.format_exc())
return
while tries < retries:
try:
client.connect(server)
return client
except Exception as E:
logging.error(E)
logging.error(traceback.format_exc())
tries += 1
return
def sub_jobs(njobs, name, pyscript, pbs_fname, queue='open',\
workdir=None, qos=None, ssh=True, extra_args=None,\
ppn=1):
hostname = socket.gethostname()
if len(name) > 15:
name = name[:15]
if 'aci.ics' in hostname and 'amon' not in hostname:
ssh=False
if ssh:
ssh_cmd = 'ssh aci-b.aci.ics.psu.edu "'
server = 'aci-b.aci.ics.psu.edu'
# client = paramiko.SSHClient()
# client.load_system_host_keys()
# client.connect(server)
client = get_ssh_client(server)
# base_sub_cmd = 'qsub %s -A %s -N %s -v '\
# %(args.pbs_fname, args.queue, args.name)
if qos is not None:
base_sub_cmd = 'qsub %s -A %s -N %s -l nodes=1:ppn=%d -l qos=%s -v '\
%(pbs_fname, queue, name, ppn, qos)
else:
base_sub_cmd = 'qsub %s -A %s -N %s -l nodes=1:ppn=%d -v '\
%(pbs_fname, queue, name, ppn)
else:
if qos is not None:
base_sub_cmd = 'qsub %s -A %s -N %s -l nodes=1:ppn=%d -l qos=%s -v '\
%(pbs_fname, queue, name, ppn, qos)
else:
base_sub_cmd = 'qsub %s -A %s -N %s -l nodes=1:ppn=%d -v '\
%(pbs_fname, queue, name, ppn)
if workdir is None:
workdir = os.getcwd()
if extra_args is None:
extra_args = ""
cmd = ''
jobids = []
for i in xrange(njobs):
# cmd_ = 'jobid=%d,workdir=%s,njobs=%d,pyscript=%s' %(i,workdir,njobs,pyscript)
cmd_ = 'jobid=%d,workdir=%s,njobs=%d,pyscript=%s,extra_args="%s"' %(i,workdir,njobs,pyscript,extra_args)
if ssh:
cmd += base_sub_cmd + cmd_
if i < (njobs-1):
cmd += ' | '
# cmd = base_sub_cmd + cmd_
# jbid = execute_ssh_cmd(client, cmd, server)
# jobids.append(jbid)
# try:
# stdin, stdout, stderr = client.exec_command(cmd)
# logging.info("stdout: ")
# sto = stdout.read()
# logging.info(sto)
# jobids.append(sto)
# except Exception as E:
# logging.error(E)
# logging.error(traceback.format_exc())
# logging.error("Messed up with ")
# logging.error(cmd)
else:
cmd = base_sub_cmd + cmd_
logging.info("Trying to submit: ")
logging.info(cmd)
try:
os.system(cmd)
# subprocess.check_call(cmd, shell=True)
except Exception as E:
logging.error(E)
logging.error("Messed up with ")
logging.error(cmd)
time.sleep(0.1)
if ssh:
# ssh_cmd = 'ssh aci-b.aci.ics.psu.edu "'
# cmd = ssh_cmd + cmd + '"'
logging.info("Full cmd to run:")
logging.info(cmd)
try:
jobids = execute_ssh_cmd(client, cmd, server)
# os.system(cmd)
# subprocess.check_call(cmd, shell=True)
# cmd_list = ['ssh', 'aci-b.aci.ics.psu.edu', '"'+cmd+'"']
# cmd_list = shlex.split(cmd)
# logging.info(cmd_list)
# subprocess.check_call(cmd_list)
except Exception as E:
logging.error(E)
logging.error("Messed up with ")
logging.error(cmd)
if ssh:
client.close()
return jobids
def find_peaks2scan(res_df, max_dv=10.0, min_sep=8e-3, max_Npeaks=48, min_Npeaks=2, minTS=6.0):
tgrps = res_df.groupby('timeID')
peak_dfs = []
for timeID, df_ in tgrps:
if np.nanmax(df_['TS']) < minTS:
continue
df = df_.sort_values('sig_nllh')
vals = df['sig_nllh']
# ind_sort = np.argsort(vals)
min_val = np.nanmin(df['sig_nllh'])
peak_dict = {'timeID': int(timeID), 'time':np.nanmean(df['time']),
'duration':np.nanmean(df['duration'])}
imxs_ = np.empty(0)
imys_ = np.empty_like(imxs_)
As_ = np.empty_like(imxs_)
Gs_ = np.empty_like(imxs_)
for row_ind, row in df.iterrows():
if row['sig_nllh'] > (min_val + max_dv) and len(imxs_) >= min_Npeaks:
break
if len(imxs_) >= max_Npeaks:
break
if len(imxs_) > 0:
imdist = np.min(im_dist(row['imx'], row['imy'], imxs_, imys_))
if imdist <= min_sep:
continue
imxs_ = np.append(imxs_, [row['imx']])
imys_ = np.append(imys_, [row['imy']])
As_ = np.append(As_, [row['A']])
Gs_ = np.append(Gs_, [row['ind']])
peak_dict['imx'] = imxs_
peak_dict['imy'] = imys_
peak_dict['Signal_A'] = As_
peak_dict['Signal_gamma'] = Gs_
peak_dfs.append(pd.DataFrame(peak_dict))
peaks_df = pd.concat(peak_dfs, ignore_index=True)
return peaks_df
def main(args):
fname = 'manager'
logging.basicConfig(filename=fname+'.log', level=logging.DEBUG,\
format='%(asctime)s-' '%(levelname)s- %(message)s')
f = open(fname+'.pid', 'w')
f.write(str(os.getpid()))
f.close()
logging.info("Wrote pid: %d" %(os.getpid()))
to = ['delauj2@gmail.com', 'aaron.tohu@gmail.com']
subject = 'BATML ' + args.GWname
body = "Got data and starting analysis"
try:
send_email(subject, body, to)
except Exception as E:
logging.error(E)
logging.error("Trouble sending email")
t_0 = time.time()
has_sky_map = False
try:
sky_map_fnames = [fname for fname in os.listdir('.') if\
'cWB.fits.gz' in fname or 'bayestar' in fname\
or 'skymap' in fname]
sky_map = hp.read_map(sky_map_fnames[0], field=(0,), nest=True)
logging.info('Opened sky map')
perc_map = probm2perc(sky_map)
logging.info('Made perc map')
has_sky_map = True
except Exception as E:
logging.warning("problem reading skymap")
logging.error(E)
logging.error(traceback.format_exc())
try:
logging.info('Connecting to DB')
if args.dbfname is None:
db_fname = guess_dbfname()
if isinstance(db_fname, list):
db_fname = db_fname[0]
else:
db_fname = args.dbfname
conn = get_conn(db_fname)
info_tab = get_info_tab(conn)
logging.info('Got info table')
trigtime = info_tab['trigtimeMET'][0]
files_tab = get_files_tab(conn)
logging.info('Got files table')
attfname = files_tab['attfname'][0]
evfname = files_tab['evfname'][0]
except Exception as E:
logging.warning("problem getting files tab from DB")
logging.error(E)
logging.error(traceback.format_exc())
attfname = 'attitude.fits'
evfname = 'filter_evdata.fits'
try:
attfile = Table.read(attfname)
logging.info('Opened att file')
except Exception as E:
logging.warning("Trouble openning attitude file")
logging.error(E)
logging.error(traceback.format_exc())
try:
GTI_pnt = Table.read(evfname, hdu='GTI_POINTING')
logging.info('Opened GTI_pnt')
logging.info(GTI_pnt)
tot_exp = 0.0
for row in GTI_pnt:
tot_exp += row['STOP'] - row['START']
logging.info("Total Exposure Time is %.3f seconds"%(tot_exp))
if tot_exp < 1.0:
logging.info("Total Pointing time is <1s")
logging.info("Exiting now")
body = "Total Pointing time is < 1s, only %.3f seconds. Exiting analysis." %(tot_exp)
try:
send_email(subject, body, to)
except Exception as E:
logging.error(E)
logging.error("Trouble sending email")
return
except Exception as E:
logging.warning("Trouble openning GTI file")
logging.error(E)
logging.error(traceback.format_exc())
try:
good_pix = np.load(args.pix_fname)
Ngood_pix = len(good_pix)
if Ngood_pix < 1:
# stop here
logging.info("Completely out of FoV")
logging.info("Exiting now")
body = "Completely out of FoV. Exiting analysis."
try:
send_email(subject, body, to)
except Exception as E:
logging.error(E)
logging.error("Trouble sending email")
return
Nratejobs = 4
if Ngood_pix > 5e4:
Nratejobs = 16
if Ngood_pix > 1e5:
Nratejobs = 32
if Ngood_pix > 2.5e5:
Nratejobs = 48
if Ngood_pix > 5e5:
Nratejobs = 64
except Exception as E:
logging.warn("Trouble reading good pix file")
Nratejobs = 64
if args.archive:
Nratejobs = 108
if args.do_bkg:
logging.info("Submitting bkg estimation job now")
# try:
if args.archive:
sub_jobs(1, 'BKG_'+args.GWname, args.BKGpyscript,\
args.pbs_fname, queue=args.queue, ppn=4,\
extra_args="--archive", qos=None)
else:
sub_jobs(1, 'BKG_'+args.GWname, args.BKGpyscript,\
args.pbs_fname,\
queue='open',#args.queue,\
ppn=4, qos=None)
logging.info("Job submitted")
# except Exception as E:
# logging.warn(E)
# logging.warn("Might have been a problem submitting")
# Wait for bkg job to finish before submitting rates jobs
dt = 0.0
t_0 = time.time()
bkg_fname = 'bkg_estimation.csv'
while dt < 16*3600.0:
if os.path.exists(bkg_fname):
break
else:
time.sleep(10.0)
dt = time.time() - t_0
if not os.path.exists(bkg_fname):
logging.info("Didn't do BKG for some reason")
logging.info("Exiting now")
body = "Didn't do BKG for some reason. Exiting analysis."
try:
send_email(subject, body, to)
except Exception as E:
logging.error(E)
logging.error("Trouble sending email")
return
extra_args = "--min_pc %.4f"%(args.min_pc)
if args.do_rates:
logging.info("Submitting %d rates jobs now"%(Nratejobs))
# try:
sub_jobs(Nratejobs, 'RATES_'+args.GWname, args.RATEpyscript,\
args.pbs_fname, queue=args.queue, qos=args.qos,\
extra_args=extra_args)
logging.info("Jobs submitted")
# except Exception as E:
# logging.warn(E)
# logging.warn("Might have been a problem submitting")
dt = 0.0
t_0 = time.time()
while (dt < 3600.0*36.0):
rate_res_fnames = get_rate_res_fnames()
logging.info("%d of %d rate jobs done" %(len(rate_res_fnames),Nratejobs))
if args.skip_waiting:
rate_res = get_merged_csv_df(rate_res_fnames)
try:
rate_res['dt'] = rate_res['time'] - trigtime
except Exception:
pass
break
elif len(rate_res_fnames) < Nratejobs:
time.sleep(30.0)
dt = time.time() - t_0
else:
rate_res = get_merged_csv_df(rate_res_fnames)
try:
rate_res['dt'] = rate_res['time'] - trigtime
except Exception:
pass
break
try:
body = "Done with rates analysis\n"
body += "Max TS is %.3f" %(np.max(rate_res['TS']))
logging.info(body)
send_email(subject, body, to)
rate_res_tab_top = rate_res.sort_values("TS").tail(16)
body = rate_res_tab_top.to_html()
logging.info(body)
# send_email(subject, body, to)
send_email_wHTML(subject, body, to)
except Exception as E:
logging.error(E)
logging.error("Trouble sending email")
if args.archive:
seed_tab = mk_seed_tab(rate_res, TS_min=4.15)
else:
seed_tab = mk_seed_tab(rate_res)
seed_tab.to_csv('rate_seeds.csv', index=False)
Nsquares = len(np.unique(seed_tab['squareID']))
Nseeds = len(seed_tab)
if Nseeds < 1:
body = "No seeds. Exiting analysis."
try:
send_email(subject, body, to)
except Exception as E:
logging.error(E)
logging.error("Trouble sending email")
return
if Nsquares > 512:
Njobs = 96
elif Nsquares > 128:
Njobs = 64
else:
Njobs = Nsquares/2
if (1.*Nseeds)/Nsquares >= 8:
Njobs += Njobs/2
if (1.*Nseeds)/Nsquares >= 16:
Njobs += Njobs/4
if (1.*Nseeds)/Nsquares >= 32:
Njobs += Njobs/4
Njobs = max(Njobs,1)
job_tab = mk_job_tab(seed_tab, Njobs)
job_tab.to_csv('job_table.csv', index=False)
# Now need to launch those jobs
# then start the next while loop, curogating the results
# get the best TSs or TSs > 6 and do whatever with them
# maybe mk imgs and do batcelldetect on the good time windows
# or if there's a decent TS from the full analysis
# also maybe add some emails in here for progress and info and errors
if args.do_llh:
logging.info("Submitting %d Jobs now"%(Njobs))
sub_jobs(Njobs, 'LLH_'+args.GWname, args.LLHpyscript,\
args.pbs_fname, queue=args.queue, qos=args.qos,\
extra_args=extra_args)
logging.info("Jobs submitted, now going to monitor progress")
t_0 = time.time()
dt = 0.0
Ndone = 0
while (dt < 3600.0*40.0):
res_fnames = get_res_fnames()
if args.skip_waiting:
try:
if has_sky_map:
res_tab = get_merged_csv_df_wpos(res_fnames, attfile, perc_map)
else:
res_tab = get_merged_csv_df_wpos(res_fnames, attfile)
logging.info("Got merged results with RA Decs")
except Exception as E:
logging.error(E)
res_tab = get_merged_csv_df(res_fnames)
logging.info("Got merged results without RA Decs")
logging.info("Max TS: %.3f" %(np.max(res_tab['TS'])))
break
if len(res_fnames) == Ndone:
time.sleep(30.0)
dt = time.time() - t_0
else:
Ndone = len(res_fnames)
logging.info("%d of %d squares scanned" %(Ndone,Nsquares))
if Ndone < Nsquares:
res_tab = get_merged_csv_df(res_fnames)
time.sleep(30.0)
dt = time.time() - t_0
else:
logging.info("Got all of the results now")
try:
if has_sky_map:
res_tab = get_merged_csv_df_wpos(res_fnames, attfile, perc_map)
else:
res_tab = get_merged_csv_df_wpos(res_fnames, attfile)
logging.info("Got merged results with RA Decs")
except Exception as E:
logging.error(E)
res_tab = get_merged_csv_df(res_fnames)
logging.info("Got merged results without RA Decs")
logging.info("Max TS: %.3f" %(np.max(res_tab['TS'])))
break
try:
res_tab['dt'] = res_tab['time'] - trigtime
except Exception:
pass
# logging.info("Saving full result table to: ")
# save_fname = 'full_res_tab.csv'
# logging.info(save_fname)
# res_tab.to_csv(save_fname)
try:
# body = "Done with LLH analysis\n"
# body += "Max TS is %.3f\n\n" %(np.max(res_tab['TS']))
res_tab_top = res_tab.sort_values("TS").tail(16)
body = "LLH analysis results\n"
body += res_tab_top.to_html()
logging.info(body)
# send_email(subject, body, to)
send_email_wHTML(subject, body, to)
except Exception as E:
logging.error(E)
logging.error("Trouble sending email")
# Now need to find anything interesting and investigate it further
# probably find each time bin with a TS>6 and scan around each
# blip with a nllh that's within 5-10 or so
# Should also probably do submit jobs for a full FoV scan
# if a TS is found above something border line alert, like
# TS ~>7-8
if np.nanmax(res_tab['TS']) < args.TSscan:
return
scan_seed_tab = mk_seed_tab4scans(res_tab, args.pcfname, seed_tab,\
TS_min=args.TSscan, pc_min=args.min_pc)
Nscan_seeds = len(scan_seed_tab)
logging.info("%d scan seeds"%(Nscan_seeds))
Nscan_squares = len(np.unique(scan_seed_tab['squareID']))
Njobs = 64
if Nscan_squares < 64:
Njobs = Nscan_squares/2
if Nscan_seeds > 1e3:
Njobs = 72
if Nscan_seeds > 2.5e3:
Njobs = 96
if Nscan_seeds > 5e3:
Njobs = 128
if Nscan_seeds > 1e4:
Njobs = 160
scan_job_tab = mk_job_tab(scan_seed_tab, Njobs)
scan_seed_tab.to_csv('scan_seeds.csv', index=False)
scan_job_tab.to_csv('scan_job_table.csv', index=False)
if args.do_scan:
logging.info("Submitting %d Scan Jobs now"%(Njobs))
sub_jobs(Njobs, 'SCAN_'+args.GWname, args.SCANpyscript,\
args.pbs_fname, queue=args.queue, qos=args.qos,\
extra_args=extra_args)
logging.info("Jobs submitted, now going to monitor progress")
t_0 = time.time()
dt = 0.0
Ndone = 0
while (dt < 3600.0*40.0):
res_fnames = get_scan_res_fnames()
if args.skip_waiting:
if len(res_fnames) < 1:
scan_res_tab = pd.DataFrame()
break
try:
if has_sky_map:
scan_res_tab = get_merged_csv_df_wpos(res_fnames, attfile, perc_map)
else:
scan_res_tab = get_merged_csv_df_wpos(res_fnames, attfile)
logging.info("Got merged scan results with RA Decs")
except Exception as E:
logging.error(E)
scan_res_tab = get_merged_csv_df(res_fnames)
logging.info("Got merged scan results without RA Decs")
logging.info("Max TS: %.3f" %(np.max(scan_res_tab['TS'])))
break
if len(res_fnames) == Ndone:
time.sleep(30.0)
dt = time.time() - t_0
else:
Ndone = len(res_fnames)
logging.info("%d of %d squares scanned" %(Ndone,Nscan_squares))
if Ndone < Nscan_squares: