-
Notifications
You must be signed in to change notification settings - Fork 35
/
keprange.py
480 lines (408 loc) · 16.4 KB
/
keprange.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
import sys, time
import numpy as np
from astropy.io import fits as pyfits
from matplotlib import pyplot as plt
import kepio, kepmsg, kepkey
# global variables
labelsize = 24; ticksize = 16; xsize = 17; ysize = 7
lcolor = '#0000ff'; lwidth = 1.0; fcolor = '#ffff00'; falpha = 0.2
instr = ''; cadence = 1800.0; barytime0 = 0
nrm = 1; barytime = []; flux = []; xmin = 0; xmax = 1
ymin = 0; ymax = 1; xr = 1; yr = 1; xlab = ''; ylab = ''
mask = []; aid = None; bid = None; cid = None; did = None; eid = None; fid = None
clobb = True; outf = ''; verb = True; logf = ''; rinf = ''
cmdLine = False
def keprange(infile, rinfile, outfile, column, clobber, verbose, logfile,
status, cLine=False):
# startup parameters
status = 0
global instr, cadence, barytime0, nrm, barytime, flux
global xmin, xmax, ymin, ymax, xr, yr, xlab, ylab
global clobb, outf, verb, logf, rinf, col, bjdref, cade, cmdLine
# log the call
if rinfile.lower() == 'none':
rinfile = ''
hashline = '----------------------------------------------------------------------------'
kepmsg.log(logfile,hashline,verbose)
call = 'KEPRANGE -- '
call += 'infile='+infile+' '
call += 'rinfile='+rinfile+' '
call += 'outfile='+outfile+' '
call += 'column='+column+' '
overwrite = 'n'
if (clobber): overwrite = 'y'
call += 'clobber='+overwrite+ ' '
chatter = 'n'
if (verbose): chatter = 'y'
call += 'verbose='+chatter+' '
call += 'logfile='+logfile
kepmsg.log(logfile,call+'\n',verbose)
clobb = clobber
outf = outfile
verb = verbose
logf = logfile
rinf = rinfile
cmdLine = cLine
# start time
kepmsg.clock('KEPRANGE started at: ',logfile,verbose)
# test log file
logfile = kepmsg.test(logfile)
# clobber output file
if clobber: status = kepio.clobber(outfile,logfile,verbose)
if kepio.fileexists(outfile):
message = 'ERROR -- KEPRANGE: ' + outfile + ' exists. Use --clobber'
status = kepmsg.err(logfile,message,verbose)
## open input file
if status == 0:
instr, status = kepio.openfits(infile,'readonly',logfile,verbose)
if status == 0:
tstart, tstop, bjdref, cadence, status = kepio.timekeys(instr,
infile,
logfile,
verbose,
status)
if status == 0:
try:
work = instr[0].header['FILEVER']
cadenom = 1.0
except:
cadenom = cadence
cade = cadenom
# fudge non-compliant FITS keywords with no values
if status == 0:
instr = kepkey.emptykeys(instr, infile, logfile, verbose)
# input data
if status == 0:
table = instr[1].data
# filter out NaNs
work1 = []; work2 = []
col = column
if status == 0:
barytime, status = kepio.readtimecol(infile, table, logfile, verbose)
if status == 0:
try:
flux = instr[1].data.field(col)
except:
message = 'ERROR -- KEPRANGE: no column named ' + col + ' in table ' + infile + '[1]'
status = kepmsg.err(file,message,verbose)
if status == 0:
for i in range(len(barytime)):
if (np.isfinite(barytime[i]) and np.isfinite(flux[i]) and flux[i] != 0.0):
work1.append(barytime[i] + bjdref)
work2.append(flux[i])
barytime = np.array(work1, dtype=np.float64)
flux = np.array(work2, dtype=np.float32) / cadenom
# clean up x-axis unit
if status == 0:
barytime0 = float(int(tstart / 100) * 100.0)
barytime = barytime - barytime0
xlab = 'BJD $-$ %d' % barytime0
# clean up y-axis units
if status == 0:
nrm = len(str(int(flux.max())))-1
flux = flux / 10**nrm
ylab = '10$^%d$ e$^-$ s$^{-1}$' % nrm
# data limits
xmin = barytime.min()
xmax = barytime.max()
ymin = flux.min()
ymax = flux.max()
xr = xmax - xmin
yr = ymax - ymin
flux[0] = 0.0
flux[-1] = 0.0
# plot new light curve
if status == 0:
plt.rcParams['figure.dpi'] = 80
plt.figure(figsize=[xsize,ysize])
plt.clf()
plotlc(cmdLine)
#if status == 0:
# status = kepio.closefits(instr, logfile, verbose)
# -----------------------------------------------------------
# plot light curve and tool
def plotlc(cmdLine):
global aid, bid, cid, did, eid, fid, mask
# load button
plt.ion()
plt.clf()
plt.axes([0.06,0.02,0.22,0.1])
plt.text(0.5,0.5,'LOAD',fontsize=24,weight='heavy',
horizontalalignment='center', verticalalignment='center')
plt.setp(plt.gca(),xticklabels=[],xticks=[],yticklabels=[],yticks=[])
plt.fill([0.0,1.0,1.0,0.0,0.0],[0.0,0.0,1.0,1.0,0.0],'#ffffee')
plt.xlim(0.0,1.0)
plt.ylim(0.0,1.0)
aid = plt.connect('button_press_event',clicker1)
# save button
plt.axes([0.2933,0.02,0.22,0.1])
plt.text(0.5,0.5,'SAVE',fontsize=24,weight='heavy',
horizontalalignment='center',verticalalignment='center')
plt.setp(plt.gca(),xticklabels=[],xticks=[],yticklabels=[],yticks=[])
plt.fill([0.0,1.0,1.0,0.0,0.0],[0.0,0.0,1.0,1.0,0.0],'#ffffee')
plt.xlim(0.0,1.0)
plt.ylim(0.0,1.0)
bid = plt.connect('button_press_event',clicker2)
# clear button
plt.axes([0.5266,0.02,0.22,0.1])
plt.text(0.5,0.5,'CLEAR',fontsize=24,weight='heavy',
horizontalalignment='center',verticalalignment='center')
plt.setp(plt.gca(),xticklabels=[],xticks=[],yticklabels=[],yticks=[])
plt.fill([0.0,1.0,1.0,0.0,0.0],[0.0,0.0,1.0,1.0,0.0],'#ffffee')
plt.xlim(0.0,1.0)
plt.ylim(0.0,1.0)
cid = plt.connect('button_press_event',clicker3)
# print button
plt.axes([0.76,0.02,0.22,0.1])
plt.text(0.5,0.5,'PRINT',fontsize=24,weight='heavy',
horizontalalignment='center',verticalalignment='center')
plt.setp(plt.gca(),xticklabels=[],xticks=[],yticklabels=[],yticks=[])
plt.fill([0.0,1.0,1.0,0.0,0.0],[0.0,0.0,1.0,1.0,0.0],'#ffffee')
plt.xlim(0.0,1.0)
plt.ylim(0.0,1.0)
did = plt.connect('button_press_event',clicker4)
# light curve
plt.axes([0.06,0.213,0.92,0.77])
plt.gca().xaxis.set_major_formatter(plt.ScalarFormatter(useOffset=False))
plt.gca().yaxis.set_major_formatter(plt.ScalarFormatter(useOffset=False))
ltime = []; ldata = []
work1 = instr[1].data.field(0) + bjdref
work2 = instr[1].data.field(col) / cade
for i in range(len(work1)):
if np.isfinite(work1[i]) or np.isfinite(work2[i]):
ltime.append(work1[i])
ldata.append(work2[i])
else:
ltime = np.array(ltime, dtype=np.float64) - barytime0
ldata = np.array(ldata, dtype=np.float64) / 10**nrm
plt.plot(ltime,ldata,color=lcolor,linestyle='-',linewidth=lwidth)
ltime = []; ldata = []
ltime = np.array(ltime, dtype=np.float64) - barytime0
ldata = np.array(ldata, dtype=np.float64) / 10**nrm
plt.plot(ltime,ldata,color=lcolor,linestyle='-',linewidth=lwidth)
plt.fill(barytime,flux,fc=fcolor,linewidth=0.0,alpha=falpha)
plt.xlabel(xlab, {'color' : 'k'})
plt.ylabel(ylab, {'color' : 'k'})
plt.grid()
# plt.plot masks
for i in range(len(mask)):
t = float(mask[i])
plt.plot([t,t],[ymin,ymax],color='g',linestyle='-',linewidth=0.5)
nt = 0
for i in range(int(len(mask)/2)):
t1 = float(mask[nt])
t2 = float(mask[nt+1])
nt += 2
plt.fill([t1,t1,t2,t2,t1],[ymin,ymax,ymax,ymin,ymin],
fc='g',linewidth=0.0,alpha=0.5)
# plot ranges
plt.xlim(xmin-xr*0.01,xmax+xr*0.01)
if ymin-yr*0.01 <= 0.0:
plt.ylim(1.0e-10,ymax+yr*0.01)
else:
plt.ylim(ymin-yr*0.01,ymax+yr*0.01)
# ranges
eid = plt.connect('key_press_event',clicker6)
# render plot
plt.ion()
plt.show()
# -----------------------------------------------------------
# load mask from ascii file
def clicker1(event):
global mask, aid, bid, cid, did, eid, fid, cmdLine
if event.inaxes:
if event.button == 1:
if (event.x > 83 and event.x < 383 and
event.y > 12 and event.y < 68):
if kepio.fileexists(rinf):
mask = []
lines, status = kepio.openascii(rinf,'r',logf,verb)
for line in lines:
line = line.strip().split(',')
try:
float(line[0])
float(line[1])
if barytime0 > 2.4e6:
mask.append(float(line[0]) - barytime0)
mask.append(float(line[1]) - barytime0)
else:
mask.append(float(line[0]) - barytime0 - 2.4e6)
mask.append(float(line[1]) - barytime0 - 2.4e6)
except:
message = 'ERROR -- KEPRANGE: ascii format of ranges '
message += 'file not recognized.'
status = kepmsg.err(logf,message,False)
plt.disconnect(aid)
plt.disconnect(bid)
plt.disconnect(cid)
plt.disconnect(did)
plt.disconnect(eid)
plt.disconnect(fid)
plotlc(cmdLine)
else:
print('WARNING -- KEPRANGE: input ranges file does not exist or was not provided')
return
# -----------------------------------------------------------
# save mask to ascii file
def clicker2(event):
global mask, aid, bid, cid, did, eid, fid, clobb, cmdLine
if clobb: status = kepio.clobber(outf,logf,verb)
if kepio.fileexists(outf):
message = 'ERROR -- KEPRANGE: ' + outf + ' exists. Use --clobber'
status = kepmsg.err(logf,message,verb)
else:
if event.inaxes:
if event.button == 1:
if (event.x > 402 and event.x < 702 and
event.y > 12 and event.y < 68):
nt = 0; txt = ''
for i in range(int(len(mask)/2)):
t1 = float(mask[nt]) + barytime0
t2 = float(mask[nt+1]) + barytime0
if t1 < 2.4e6: t1 += 2.4e6
if t2 < 2.4e6: t2 += 2.4e6
txt += str(t1) + ',' + str(t2) + '\n'
nt += 2
txt = txt.strip()
print txt
kepmsg.file(outf,txt,True)
print '\nWrote ASCII file ' + outf
plotlc(cmdLine)
return
# -----------------------------------------------------------
# clear time domain mask
def clicker3(event):
global mask, aid, bid, cid, did, eid, fid, cmdLine
if event.inaxes:
if event.button == 1:
if (event.x > 723 and event.x < 1022 and
event.y > 12 and event.y < 68):
mask = []
plt.disconnect(aid)
plt.disconnect(bid)
plt.disconnect(cid)
plt.disconnect(did)
plt.disconnect(eid)
plt.disconnect(fid)
plotlc(cmdLine)
return
# -----------------------------------------------------------
# print time domain mask
def clicker4(event):
global mask, aid, bid, cid, did, eid, fid, cmdLine
if event.inaxes:
if event.button == 1:
if (event.x > 1042 and event.x < 1342 and
event.y > 12 and event.y < 68):
nt = 0; txt = ''
for i in range(int(len(mask)/2)):
t1 = float(mask[nt]) + barytime0
t2 = float(mask[nt+1]) + barytime0
if t1 < 2.4e6: t1 += 2.4e6
if t2 < 2.4e6: t2 += 2.4e6
txt += str(t1) + ',' + str(t2) + '\n'
nt += 2
txt = txt.strip()
print('\n' + txt)
plt.disconnect(aid)
plt.disconnect(bid)
plt.disconnect(cid)
plt.disconnect(did)
plt.disconnect(eid)
plt.disconnect(fid)
plotlc(cmdLine)
return
# -----------------------------------------------------------
# right-click create time ranges
def clicker6(event):
global mask, aid, bid, cid, did, eid, fid, cmdLine
if event.inaxes:
# event.key
if event.key == '' or \
event.key == '1' or \
event.key == '2' or \
event.key == '3' or \
event.key == '4' or \
event.key == '5' or \
event.key == '6' or \
event.key == '7' or \
event.key == '8' or \
event.key == '9' or \
event.key == '0' or \
event.key == '-' or \
event.key == '=' or \
event.key == 'q' or \
event.key == 'w' or \
event.key == 'e' or \
event.key == 'r' or \
event.key == 't' or \
event.key == 'y' or \
event.key == 'u' or \
event.key == 'i' or \
event.key == 'o' or \
event.key == 'p' or \
event.key == '[' or \
event.key == ']' or \
event.key == 'a' or \
event.key == 'd' or \
event.key == 'f' or \
event.key == 'g' or \
event.key == 'h' or \
event.key == 'j' or \
event.key == 'k' or \
event.key == 'l' or \
event.key == ';' or \
event.key == 'x' or \
event.key == 'c' or \
event.key == 'v' or \
event.key == 'b' or \
event.key == 'n' or \
event.key == 'm' or \
event.key == ',' or \
event.key == '.' or \
event.key == '/':
if (event.x > 83 and event.x < 1337 and
event.y > 122 and event.y < 558):
if len(mask) % 2 == 0:
mask.append(event.xdata)
else:
if event.xdata > mask[-1]:
mask.append(event.xdata)
else:
mask.append(mask[-1])
mask[-2] = event.xdata
plotlc(cmdLine)
if event.key == 'z':
kepio.closefits(instr, "keprangelog.txt", True)
plt.close()
return
# main
if '--shell' in sys.argv:
import argparse
parser = argparse.ArgumentParser(description='Interactively define and store time ranges via a GUI')
parser.add_argument('--shell', action='store_true',
help='Are we running from the shell?')
parser.add_argument('infile', help='Name of input file', type=str)
parser.add_argument('--rinfile', default='',
help='Name of input ASCII time ranges file', type=str)
parser.add_argument('--outfile', default='',
help='Name of output ASCII time ranges file',
type=str)
parser.add_argument('--column', default='SAP_FLUX',
help='Name of diagnostic FITS column', type=str)
parser.add_argument('--clobber', action='store_true',
help='Overwrite output file?')
parser.add_argument('--verbose', action='store_true',
help='Write to a log file?')
parser.add_argument('--logfile', '-l', help='Name of ascii log file',
default='keprange.log', dest='logfile', type=str)
parser.add_argument('--status', '-e', help='Exit status (0=good)',
default=0, dest='status', type=int)
args = parser.parse_args()
cmdLine=True
keprange(args.infile, args.rinfile, args.outfile, args.column,
args.clobber, args.verbose, args.logfile, args.status, cmdLine)
else:
from pyraf import iraf
parfile = iraf.osfn("kepler$keprange.par")
t = iraf.IrafTaskFactory(taskname="keprange", value=parfile, function=keprange)