-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkepbin.py
executable file
·351 lines (285 loc) · 10.1 KB
/
kepbin.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
from pyraf import iraf
import numpy as n
import sys
sys.path.append('/usr/stsci/kepler/' )
import kepio, kepmsg, kepkey
import pyfits
import pylab as plt
from scipy.interpolate import interp1d
from scipy import integrate
from pylab import rc, rcParams
def bin_funct(date,flux,nbins=None,binwidth=None,ownbins=None,method='linear',
interpm='quad'):
"""
A function to bin light curve data
Arguements:
date -- the array containing the time the observations were made
flux -- the flux values corresponding to the date array
flux and date must be the same length and should have been cleaned of
nan values
nbin -- if specifying the number of bins then give the number here
binwidth -- the width of each bin
ownbins -- an array which has been read from a user supplied file
only ONE of nbin, binwidth and ownbins should be defined
No bin can be larger than the largest gap in data <- should fix
method -- the method of interpolation, options are:
'linear', 'nearest', 'zero', 'slinear', 'quadratic' or 'cubic'
or
an integer (i), which interpolates using a spline of order (i)
interpm -- method of performing the integration
can either be 'quad' or 'romberg'
"""
# Try to catch any bad values.
if n.shape(date) != n.shape(flux):
raise IndexError('date and flux arrays should be the same length')
i = 0
for binMethod in [nbins,binwidth,ownbins]:
if binMethod is None:
i += 1
if i != 2:
raise TypeError('Supply one and only one of nbin, binwidth, ownbins')
if not method in [i for i in ['linear','nearest', 'zero', 'slinear',
'quadratic' or 'cubic']]:
try:
testcase = "Integer %d" % int(method)
except ValueError:
raise ValueError("method needs to be one of: 'linear', 'nearest', 'zero', 'slinear', 'quadratic' or 'cubic'or an integer (i), which interpolates using a spline of order (i)")
if not interpm in [i for i in ['quad','romberg']]:
raise ValueError("Integration method must either be 'quad' or 'romberg'")
#perform the interpolation on the data
intpl = interp1d(date,flux,kind=method)
#caculate bin bounds using the correct method, should be of length nbins+1
if binwidth is not None:
bounds = n.arange(date[0],date[-1]+1e-10,binwidth)
bdate = n.arange(date[0]+0.5*binwidth,date[-1]-0.5*binwidth,binwidth)
elif nbins is not None:
bounds = n.arange(date[0],date[-1]+1e-10,(date[-1]-date[0])/nbins)
bdate = n.arange(date[0]+0.5*((date[-1]-date[0])/nbins),
date[-1],(date[-1]-date[0])/nbins)
elif ownbins is not None:
bounds = ownbins
bdate = n.zeros(len(bounds)-1)
for i in range(len(bdate)):
bdate[i] = bounds[i]+(0.5*(bounds[i+1] - bounds[i]))
minbin = []
for i in range(1,len(bounds)):
minbin.append(bounds[i] - bounds[i-1])
mincad = []
for i in range(1,len(date)):
mincad.append(date[i] - date[i-1])
# if min(minbin) < max(mincad):
# raise ValueError('Smallest bin must be larger than largest gap between cadences')
bflux =[]
#Iterate over the bins starting with bounds[1]
for i in range(1,len(bounds)):
#Initialise variables that need to be reet after each iteration
bin = []
t = []
extrabitHigh = 0.
extrabitLow = 0.
eb1 = None
eb2 = None
#Iterate over the length of the flux array
for j in range(len(flux)):
#Catch all date bins within the bounds of the bin
if date[j] >= bounds[i-1] and date[j] < bounds[i]:
bin.append(flux[j])
t.append(j)
try:
tmin = min(t)
tmax = max(t)
#We now have to deal with the bits either side of date[tmin]
#and date[tmax]
#First the bit between date[tmax] and bounds[i]
if bounds[i] > date[tmax]+1e-6:
if interpm == 'quad':
extrabitHigh = integrate.quad(intpl,date[tmax],bounds[i])[0]
elif interpm == 'romberg':
extrabitHigh = integrate.romberg(intpl,date[tmax],bounds[i])
#Now the bit between bound[i-1] and t[min]
if bounds[i-1] < date[tmin]-1e-6:
if interpm == 'quad':
extrabitLow = integrate.quad(intpl,bounds[i-1],date[tmin])[0]
elif interpm == 'romberg':
extrabitLow = integrate.romberg(intpl,bounds[i-1],date[tmin])
#Scale the extrabits as if they were the size of a full bin
#Catch error if there is no extrabit
try:
eb1 = (extrabitHigh/(float(bounds[i]-date[tmax])))
bin.append(eb1)
except ZeroDivisionError:
pass
try:
eb2 = (extrabitLow/(float(date[tmin]-bounds[i-1])))
bin.append(eb2)
except ZeroDivisionError:
pass
except ValueError:
if interpm == 'quad':
totbin = integrate.quad(intpl,bounds[i-1],bounds[i])[0]
elif interpm == 'romberg':
totbin = integrate.romberg(intpl,bounds[i-1],bounds[i])
eb = (totbin/(float(bounds[i]-bounds[i-1])))
bin.append(eb)
binflux = n.mean(bin)
bflux.append(binflux)
return bdate,bflux
def cutBadData(date,flux):
date2 = date[n.logical_and(n.logical_and(n.isfinite(date),
n.isfinite(flux)),flux != 0.0)]
flux2 = flux[n.logical_and(n.logical_and(n.isfinite(date),
n.isfinite(flux)),flux != 0.0)]
return date2,flux2
def do_plot(date,flux,status=0):
xmin = min(date)
xmax = max(date)
ymin = min(flux)
ymax = max(flux)
xr = xmax - xmin
yr = ymax - ymin
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}
rcParams.update(params)
except:
print 'ERROR -- KEPCLIP: install latex for scientific plotting'
status = 1
if status == 0:
# plt.figure(figsize=[12,5])
plt.clf()
# plt.axes([0.2,0.2,0.94,0.88])
# ltime = [date[0]]; ldata = [flux[0]]
# for i in range(1,len(flux)):
# if (date[i-1] > date[i] - 0.025):
# ltime.append(date[i])
# ldata.append(flux[i])
# else:
# ltime = n.array(ltime, dtype='float64')
# ldata = n.array(ldata, dtype='float64')
# plt.plot(ltime,ldata,color='#0000ff',linestyle='-'
# ,linewidth=1.0)
# ltime = []; ldata = []
# ltime = n.array(ltime, dtype='float64')
# ldata = n.array(ldata, dtype='float64')
plt.plot(date,flux,color='#0000ff',linestyle='-',linewidth=1.0)
date = n.insert(date,[0],[date[0]])
date = n.append(date,[date[-1]])
flux = n.insert(flux,[0],[0.0])
flux = n.append(flux,[0.0])
plt.fill(date,flux,fc='#ffff00',linewidth=0.0,alpha=0.2)
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)
xlab = 'BJD'
ylab = 'e- / cadence'
plt.xlabel(xlab, {'color' : 'k'})
plt.ylabel(ylab, {'color' : 'k'})
plt.ion()
plt.grid()
plt.ioff()
def kepbin(infile,outfile,fluxcol,do_nbin,nbins,do_binwidth,binwidth,
do_ownbins,binfile,method,interpm,plot,clobber,verbose,logfile,status):
"""
Setup the kepbin environment
"""
# log the call
hashline = '----------------------------------------------------------------------------'
kepmsg.log(logfile,hashline,verbose)
call = 'KEPBIN -- '
call += 'infile='+infile+' '
call += 'outfile='+outfile+' '
call += 'fluxcol='+fluxcol+ ' '
donbin = 'n'
if (do_nbin): donbin = 'y'
call += 'donbin='+donbin+ ' '
dobinwidth = 'n'
if (do_binwidth): dobinwidth = 'y'
call += 'dbinwidth='+dobinwidth+ ' '
doownbin = 'n'
if (do_ownbins): doownbin = 'y'
call += 'doownbin='+doownbin+ ' '
call += 'method='+method+' '
call += 'interpm='+interpm+' '
plotit = 'n'
if (plot): plotit = 'y'
call += 'plot='+plotit+ ' '
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('KEPCLIP 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 -- KEPCLIP: ' + outfile + ' exists. Use --clobber'
status = kepmsg.err(logfile,message,verbose)
# open input file
if status == 0:
instr, status = kepio.openfits(infile,'readonly',logfile,verbose)
tstart, tstop, bjdref, cadence, status = kepio.timekeys(instr,
infile,logfile,verbose,status)
# fudge non-compliant FITS keywords with no values
if status == 0:
instr = kepkey.emptykeys(instr,file,logfile,verbose)
# input data
if status == 0:
table = instr[1].data
# read time and flux columns
date = table.field('barytime')
flux = table.field(fluxcol)
#cut out infinites and zero flux columns
date,flux = cutBadData(date,flux)
if do_nbin:
bdate,bflux = bin_funct(date,flux,nbins=nbins
,method=method,interpm=interpm)
elif do_binwidth:
bdate,bflux = bin_funct(date,flux,binwidth=binwidth
,method=method,interpm=interpm)
elif do_ownbins:
filepointer = open(binfile,'r')
ownbins = []
for line in filepointer:
splitted = line.split()
ownbins.append(float(splitted[0]))
ownbins = n.array(ownbins)
bdate,bflux = bin_funct(date,flux,ownbins=ownbins
,method=method,interpm=interpm)
if plot:
do_plot(bdate,bflux)
if status == 0:
col1 = pyfits.Column(name='bdate',format='E',unit='day',array=bdate)
col2 = pyfits.Column(name='bflux',format='E',unit='e-/cadence',array=bflux)
cols = pyfits.ColDefs([col1,col2])
instr.append(pyfits.new_table(cols))
instr[-1].header.update('EXTNAME','BINNED DATA','extension name')
instr.writeto(outfile)
# close input file
if status == 0:
status = kepio.closefits(instr,logfile,verbose)
# end time
if (status == 0):
message = 'KEPBIN completed at'
else:
message = '\nKEPBIN aborted at'
kepmsg.clock(message,logfile,verbose)
#def main():
parfile = iraf.osfn("kepler$kepbin.par")
t = iraf.IrafTaskFactory(taskname="kepbin", value=parfile, function=kepbin)
#if __name__ == "__main__":
# main()