-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdo_blip_search.py
161 lines (119 loc) · 4.78 KB
/
do_blip_search.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
import numpy as np
from astropy.io import fits
import os
import argparse
import logging, traceback, time
from funcs2run_bat_tools import std_grb, do_bkg, do_pc, do_search_mp
from sqlite_funcs import get_conn, timeID2time_dur, write_cat2db
from dbread_funcs import get_top_rate_timeIDs, get_files_tab,\
guess_dbfname, get_info_tab
from config import EBINS0, EBINS1
def cli():
parser = argparse.ArgumentParser()
parser.add_argument('--evfname', type=str,\
help="Event data file",
default=None)
parser.add_argument('--dmask', type=str,\
help="Detmask fname",
default=None)
parser.add_argument('--dbfname', type=str,\
help="Name of the database",\
default=None)
parser.add_argument('--Nimgs', type=int,\
help="Number of images to make",\
default=50)
parser.add_argument('--Nproc', type=int,\
help="Number of procs to use",\
default=2)
args = parser.parse_args()
return args
def main(args):
fname = 'blip_search'
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()))
time_starting = time.time()
# ebins0 = np.append([EBINS0[0]], EBINS0[:-1])
# ebins1 = np.append([EBINS1[-1]], EBINS1[1:])
ebins0 = [EBINS0[0]]
ebins1 = [EBINS1[-1]]
Nebins = len(ebins0)
logging.info("ebins0:")
logging.info(ebins0)
logging.info("ebins1:")
logging.info(ebins1)
if args.dbfname is None:
db_fname = guess_dbfname()
if isinstance(db_fname, list):
db_fname = db_fname[0]
else:
db_fname = args.dbfname
logging.info('Connecting to DB: ' + db_fname)
conn = get_conn(db_fname)
info_tab = get_info_tab(conn)
logging.info('Got info table')
trig_time = info_tab['trigtimeMET'][0]
min_ev_time = info_tab['tstartMET'][0]
files_tab = get_files_tab(conn)
logging.info('Got files table')
work_dir = files_tab['workDir'][0]
ev_fname = files_tab['evfname'][0]
dmask = files_tab['detmask'][0]
att_fname = files_tab['attfname'][0]
Nimgs = args.Nimgs
logging.info("Making PC image if it doesn't already exist")
pc_fname = do_pc(dmask, att_fname, work_dir)
bkg_t1 = trig_time - 20.0
bkg_t0 = max(min_ev_time, bkg_t1 - 20.0)
bkg_dt = bkg_t1 - bkg_t0
dpi_bkg_fnames = []
logging.info("Making the bkg DPIs")
for i in xrange(Nebins):
dpi_bkg_fnames.append(do_bkg(bkg_t0, bkg_t1, ev_fname,\
dmask, work_dir, e0=ebins0[i], e1=ebins1[i]))
logging.info("DPI bkg from MET %.3f - %.3f" %(bkg_t0, bkg_t1))
timeIDs = get_top_rate_timeIDs(conn, N=Nimgs)
conn.close()
times = np.zeros(Nimgs)
dts = np.zeros(Nimgs)
logging.info("Getting times and exposures")
for i in xrange(Nimgs):
times[i], dts[i] = timeID2time_dur(timeIDs[i], trig_time)
logging.info("Starting image creation and search for %d time frames" %(Nimgs))
if args.Nproc > 1:
do_search_mp(args.Nproc, times, dts, ev_fname, dpi_bkg_fnames, pc_fname,\
att_fname, dmask, work_dir, e0=ebins0, e1=ebins1,\
db_fname=db_fname, timeIDs=timeIDs)
else:
for i in xrange(Nimgs):
cat_fname = std_grb(times[i], dts[i], ev_fname, dpi_bkg_fname,\
att_fname, dmask, work_dir, pc=pc_fname)
logging.info("Finished search of time frame %d of %d" %((i+1),Nimgs))
conn = get_conn(db_fname)
try:
write_cat2db(conn, cat_fname, timeIDs[i])
logging.info("Wrote results from " + cat_fname + " into DB")
except Exception as E:
logging.error(str(E))
logging.error(traceback.format_exc())
logging.warning("Failed to write results from " + cat_fname + " into DB")
logging.info("Trying again")
conn.close()
time.sleep(1.0)
conn = get_conn(db_fname)
try:
write_cat2db(conn, cat_fname, timeIDs[i])
logging.info("Wrote results from " + cat_fname + " into DB")
except Exception as E:
logging.error(str(E))
logging.error(traceback.format_exc())
logging.error("Failed to write results from " + cat_fname + " into DB")
logging.error("And not trying again")
conn.close()
logging.info("Done with do_blip_search, now exiting")
if __name__ == "__main__":
args = cli()
main(args)