-
Notifications
You must be signed in to change notification settings - Fork 1
/
kepstddev.py
executable file
·315 lines (238 loc) · 9.47 KB
/
kepstddev.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
import numpy, scipy, sys, time, pyfits, pylab, copy, math
import kepio, kepmsg, kepstat, kepkey
from scipy import stats
from kepstat import savitzky_golay, running_frac_std
from copy import copy
from math import ceil, sqrt
from numpy import insert, append, array, median, ones, isfinite, nan, zeros
def kepstddev(infile,outfile,datacol,timescale,clobber,verbose,logfile,status,cmdLine=False):
# startup parameters
status = 0
labelsize = 32
ticksize = 18
xsize = 16
ysize = 6
lcolor = '#0000ff'
lwidth = 1.0
fcolor = '#ffff00'
falpha = 0.2
# log the call
hashline = '----------------------------------------------------------------------------'
kepmsg.log(logfile,hashline,verbose)
call = 'KEPSTDDEV -- '
call += 'infile='+infile+' '
call += 'outfile='+outfile+' '
call += 'datacol='+str(datacol)+' '
call += 'timescale='+str(timescale)+' '
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)
# start time
kepmsg.clock('KEPSTDDEV 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 -- KEPSTDDEV: ' + outfile + ' exists. Use clobber=yes'
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
# fudge non-compliant FITS keywords with no values
if status == 0:
instr = kepkey.emptykeys(instr,file,logfile,verbose)
# read table structure
if status == 0:
table, status = kepio.readfitstab(infile,instr[1],logfile,verbose)
# filter input data table
if status == 0:
work1 = numpy.array([table.field('time'), table.field(datacol)])
work1 = numpy.rot90(work1,3)
work1 = work1[~numpy.isnan(work1).any(1)]
# read table columns
if status == 0:
intime = work1[:,1] + bjdref
indata = work1[:,0]
# calculate STDDEV in units of ppm
if status == 0:
stddev = running_frac_std(intime,indata,timescale / 24) * 1.0e6
cdpp = stddev / sqrt(timescale * 3600.0 / cadence)
# filter cdpp
if status == 0:
for i in range(len(cdpp)):
if cdpp[i] > median(cdpp) * 10.0: cdpp[i] = cdpp[i-1]
# calculate median STDDEV
if status == 0:
medcdpp = ones((len(cdpp)),dtype='float32') * median(cdpp)
print '\nMedian %.1fhr CDPP = %d ppm' % (timescale, median(cdpp))
# calculate RMS STDDEV
if status == 0:
rms, status = kepstat.rms(cdpp,zeros(len(stddev)),logfile,verbose)
rmscdpp = ones((len(cdpp)),dtype='float32') * rms
print ' RMS %.1fhr CDPP = %d ppm\n' % (timescale, rms)
# clean up x-axis unit
if status == 0:
intime0 = float(int(tstart / 100) * 100.0)
ptime = intime - intime0
xlab = 'BJD $-$ %d' % intime0
# clean up y-axis units
if status == 0:
pout = copy(cdpp)
nrm = math.ceil(math.log10(median(cdpp))) - 1.0
pout = pout / 10**nrm
ylab = '$\sigma$ (10$^%d$ ppm)' % nrm
# data limits
xmin = ptime.min()
xmax = ptime.max()
ymin = pout.min()
ymax = pout.max()
xr = xmax - xmin
yr = ymax - ymin
ptime = insert(ptime,[0],[ptime[0]])
ptime = append(ptime,[ptime[-1]])
pout = insert(pout,[0],[0.0])
pout = append(pout,0.0)
# plot style
if status == 0:
try:
params = {'backend': 'png',
'axes.linewidth': 2.5,
'axes.labelsize': 24,
'axes.font': 'sans-serif',
'axes.fontweight' : 'bold',
'text.fontsize': 12,
'legend.fontsize': 12,
'xtick.labelsize': 16,
'ytick.labelsize': 16}
pylab.rcParams.update(params)
except:
pass
# define size of plot on monitor screen
pylab.figure(figsize=[xsize,ysize])
# delete any fossil plots in the matplotlib window
pylab.clf()
# position first axes inside the plotting window
ax = pylab.axes([0.07,0.12,0.92,0.84])
# force tick labels to be absolute rather than relative
pylab.gca().xaxis.set_major_formatter(pylab.ScalarFormatter(useOffset=False))
pylab.gca().yaxis.set_major_formatter(pylab.ScalarFormatter(useOffset=False))
# rotate y labels by 90 deg
labels = ax.get_yticklabels()
pylab.setp(labels, 'rotation', 90, fontsize=16)
# plot flux vs time
ltime = array([],dtype='float64')
ldata = array([],dtype='float32')
dt = 0
work1 = 2.0 * cadence / 86400
for i in range(1,len(ptime)-1):
dt = ptime[i] - ptime[i-1]
if dt < work1:
ltime = append(ltime,ptime[i])
ldata = append(ldata,pout[i])
else:
pylab.plot(ltime,ldata,color='#0000ff',linestyle='-',linewidth=1.0)
ltime = array([],dtype='float64')
ldata = array([],dtype='float32')
pylab.plot(ltime,ldata,color='#0000ff',linestyle='-',linewidth=1.0)
# plot the fill color below data time series, with no data gaps
pylab.fill(ptime,pout,fc='#ffff00',linewidth=0.0,alpha=0.2)
# plot median CDPP
pylab.plot(intime - intime0,medcdpp / 10**nrm,color='r',linestyle='-',linewidth=2.0)
# plot RMS CDPP
pylab.plot(intime - intime0,rmscdpp / 10**nrm,color='r',linestyle='--',linewidth=2.0)
# define plot x and y limits
pylab.xlim(xmin - xr * 0.01, xmax + xr * 0.01)
if ymin - yr * 0.01 <= 0.0:
pylab.ylim(1.0e-10, ymax + yr * 0.01)
else:
pylab.ylim(ymin - yr * 0.01, ymax + yr * 0.01)
# plot labels
pylab.xlabel(xlab, {'color' : 'k'})
try:
pylab.ylabel(ylab, {'color' : 'k'})
except:
ylab1 = '10**%d e-/s' % nrm
pylab.ylabel(ylab, {'color' : 'k'})
# make grid on plot
pylab.grid()
# render plot
if status == 0:
if cmdLine:
pylab.show(block=True)
else:
pylab.ion()
pylab.plot([])
pylab.ioff()
# add NaNs back into data
if status == 0:
n = 0
work1 = array([],dtype='float32')
instr, status = kepio.openfits(infile,'readonly',logfile,verbose)
table, status = kepio.readfitstab(infile,instr[1],logfile,verbose)
for i in range(len(table.field(0))):
if isfinite(table.field('time')[i]) and isfinite(table.field(datacol)[i]):
work1 = append(work1,cdpp[n])
n += 1
else:
work1 = append(work1,nan)
# write output file
if status == 0:
status = kepkey.new('MCDPP%d' % (timescale * 10.0),medcdpp[0],
'Median %.1fhr CDPP (ppm)' % timescale,
instr[1],outfile,logfile,verbose)
status = kepkey.new('RCDPP%d' % (timescale * 10.0),rmscdpp[0],
'RMS %.1fhr CDPP (ppm)' % timescale,
instr[1],outfile,logfile,verbose)
colname = 'CDPP_%d' % (timescale * 10)
col1 = pyfits.Column(name=colname,format='E13.7',array=work1)
cols = instr[1].data.columns + col1
instr[1] = pyfits.new_table(cols,header=instr[1].header)
instr.writeto(outfile)
# comment keyword in output file
if status == 0:
status = kepkey.history(call,instr[0],outfile,logfile,verbose)
# close FITS
if status == 0:
status = kepio.closefits(instr,logfile,verbose)
# end time
if (status == 0):
message = 'KEPSTDDEV completed at'
else:
message = '\nKEPSTDDEV aborted at'
kepmsg.clock(message,logfile,verbose)
# -----------------------------------------------------------
# main
if '--shell' in sys.argv:
import argparse
parser = argparse.ArgumentParser(description='Calculate CDPP from a time series')
parser.add_argument('--shell', action='store_true', help='Are we running from the shell?')
parser.add_argument('infile', help='Name of input FITS file', type=str)
parser.add_argument('outfile', help='Name of output FITS file', type=str)
parser.add_argument('--datacol', default='PDCSAP_FLUX', help='Name of data column to plot', type=str)
parser.add_argument('--timescale', '-t', default=6.5, help='CDPP timescale', dest='timescale', type=float)
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='kepstddev.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
kepstddev(args.infile,args.outfile,args.datacol,args.timescale,args.clobber,args.verbose,
args.logfile,args.status,cmdLine)
else:
from pyraf import iraf
parfile = iraf.osfn("kepler$kepstddev.par")
t = iraf.IrafTaskFactory(taskname="kepstddev", value=parfile, function=kepstddev)