def filterNaN(instr, datacol, outfile, logfile, verbose): status = 0 try: nanclean = instr[1].header['NANCLEAN'] except: naxis2 = 0 for i in range(len(instr[1].columns.names)): if 'time' in instr[1].columns.names[i].lower(): timecol = instr[1].columns.names[i] try: instr[1].data.field(datacol) except: msg = 'ERROR -- KEPIO.FILTERNAN: cannot find column ' + datacol + ' in the infile' status = kepmsg.err(logfile, msg, verbose) if status == 0: try: for i in range(len(instr[1].data.field(0))): if str(instr[1].data.field(timecol)[i]) != '-inf' and \ str(instr[1].data.field(datacol)[i]) != '-inf': instr[1].data[naxis2] = instr[1].data[i] naxis2 += 1 instr[1].data = instr[1].data[:naxis2] comment = 'NaN cadences removed from data' status = kepkey.new('NANCLEAN', True, comment, instr[1], outfile, logfile, verbose) except: msg = 'ERROR -- KEPIO.FILTERNAN: Failed to filter NaNs from ' + outfile status = kepmsg.err(logfile, msg, verbose) return instr, status
def filterNaN(instr,datacol,outfile,logfile,verbose): status = 0 try: nanclean = instr[1].header['NANCLEAN'] except: naxis2 = 0 for i in range(len(instr[1].columns.names)): if 'time' in instr[1].columns.names[i].lower(): timecol = instr[1].columns.names[i] try: instr[1].data.field(datacol) except: msg = 'ERROR -- KEPIO.FILTERNAN: cannot find column ' + datacol + ' in the infile' status = kepmsg.err(logfile,msg,verbose) if status == 0: try: for i in range(len(instr[1].data.field(0))): if str(instr[1].data.field(timecol)[i]) != '-inf' and \ str(instr[1].data.field(datacol)[i]) != '-inf': instr[1].data[naxis2] = instr[1].data[i] naxis2 += 1 instr[1].data = instr[1].data[:naxis2] comment = 'NaN cadences removed from data' status = kepkey.new('NANCLEAN',True,comment,instr[1],outfile,logfile,verbose) except: msg = 'ERROR -- KEPIO.FILTERNAN: Failed to filter NaNs from '+ outfile status = kepmsg.err(logfile,msg,verbose) return instr, status
def kepstddev(infile,outfile,datacol,timescale,clobber,verbose,logfile,status,cmdLine=False): # startup parameters status = 0 labelsize = 44 ticksize = 36 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 astddev = numpy.std(indata) * 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 standard deviation = %d ppm' % (timescale, median(stddev[:])) print('\nStandard deviation = %d ppm' % astddev) # calculate median STDDEV if status == 0: medcdpp = ones((len(cdpp)),dtype='float32') * median(cdpp[:]) print('Median %.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 = '%.1fhr $\sigma$ (10$^%d$ ppm)' % (timescale,nrm) ylab = '%.1fhr $\sigma$ (ppm)' % timescale # 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': 36, 'axes.font': 'sans-serif', 'axes.fontweight' : 'bold', 'text.fontsize': 12, 'legend.fontsize': 12, 'xtick.labelsize': 32, 'ytick.labelsize': 36} 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.15,0.92,0.83]) # 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)) ax.yaxis.set_major_locator(MaxNLocator(5)) # rotate y labels by 90 deg labels = ax.get_yticklabels() pylab.setp(labels, 'rotation', 90,fontsize=36) # 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) # pylab.plot(intime - intime0,medcdpp,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'}) 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)
def kepregr(infile, outfile, datacol, kmethod, kneighb, plot, plotlab, clobber, verbose, logfile, status): """ Perform a k-nearest neighbor regression analysis. """ ## startup parameters status = 0 labelsize = 24 ticksize = 16 xsize = 16 ysize = 6 lcolor = '#47AE10' lwidth = 1.0 fcolor = '#9AFF9A' falpha = 0.3 ## log the call hashline = '----------------------------------------------------------------------------' kepmsg.log(logfile, hashline, verbose) call = 'KEPREGR -- ' call += 'infile=' + infile + ' ' call += 'outfile=' + outfile + ' ' call += 'datacol=' + str(datacol) + ' ' call += 'kmethod=' + str(kmethod) + ' ' call += 'kneighb=' + str(kneighb) + ' ' plotit = 'n' if (plot): plotit = 'y' call += 'plot=' + plotit + ' ' call += 'plotlab=' + str(plotlab) + ' ' 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('KEPREGR 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 -- KEPREGR: ' + 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) tstart, tstop, bjdref, cadence, status = kepio.timekeys( instr, infile, logfile, verbose, status) if cadence == 0.0: tstart, tstop, ncad, cadence, status = kepio.cadence( 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) # read time and flux columns if status == 0: barytime, status = kepio.readtimecol(infile, table, logfile, verbose) if status == 0: flux, status = kepio.readfitscol(infile, instr[1].data, datacol, logfile, verbose) # filter input data table if status == 0: try: nanclean = instr[1].header['NANCLEAN'] except: naxis2 = 0 for i in range(len(table.field(0))): if (numpy.isfinite(barytime[i]) and numpy.isfinite(flux[i]) and flux[i] != 0.0): table[naxis2] = table[i] naxis2 += 1 instr[1].data = table[:naxis2] comment = 'NaN cadences removed from data' status = kepkey.new('NANCLEAN', True, comment, instr[1], outfile, logfile, verbose) ## read table columns if status == 0: try: intime = instr[1].data.field('barytime') except: intime, status = kepio.readfitscol(infile, instr[1].data, 'time', logfile, verbose) indata, status = kepio.readfitscol(infile, instr[1].data, datacol, logfile, verbose) if status == 0: intime = intime + bjdref indata = indata / cadenom if status == 0: outdata = knn_predict(intime, indata, kmethod, kneighb) ## comment keyword in output file if status == 0: status = kepkey.history(call, instr[0], outfile, logfile, verbose) ## clean up x-axis unit if status == 0: intime0 = float(int(tstart / 100) * 100.0) if intime0 < 2.4e6: intime0 += 2.4e6 ptime = intime - intime0 # print ptime,intime,intime0 xlab = 'BJD $-$ %d' % intime0 ## clean up y-axis units if status == 0: pout = indata * 1.0 pout2 = outdata * 1.0 nrm = len(str(int(numpy.nanmax(pout)))) - 1 pout = pout / 10**nrm pout2 = pout2 / 10**nrm ylab = '10$^%d$ %s' % (nrm, plotlab) ## data limits xmin = numpy.nanmin(ptime) xmax = numpy.nanmax(ptime) ymin = numpy.min(pout) ymax = numpy.nanmax(pout) 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) pout2 = insert(pout2, [0], [0.0]) pout2 = append(pout2, 0.0) ## plot light curve if status == 0 and plot: try: params = { 'backend': 'png', 'axes.linewidth': 2.5, 'axes.labelsize': labelsize, 'axes.font': 'sans-serif', 'axes.fontweight': 'bold', 'text.fontsize': 12, 'legend.fontsize': 12, 'xtick.labelsize': ticksize, 'ytick.labelsize': ticksize } rcParams.update(params) except: print('ERROR -- KEPREGR: install latex for scientific plotting') status = 1 if status == 0 and plot: pylab.figure(1, figsize=[xsize, ysize]) ## plot regression data ax = pylab.axes([0.06, 0.1, 0.93, 0.87]) pylab.gca().xaxis.set_major_formatter( pylab.ScalarFormatter(useOffset=False)) pylab.gca().yaxis.set_major_formatter( pylab.ScalarFormatter(useOffset=False)) # pylab.plot(ptime,pout,color='#ff9900',linestyle='-',linewidth=lwidth) pylab.scatter(ptime, pout, color='#214CAE', s=5) fill(ptime, pout, color=fcolor, linewidth=0.0, alpha=falpha) pylab.plot(ptime[kneighb:-kneighb], pout2[kneighb:-kneighb], color=lcolor, linestyle='-', linewidth=lwidth * 2.0) xlabel(xlab, {'color': 'k'}) ylabel(ylab, {'color': 'k'}) xlim(xmin - xr * 0.01, xmax + xr * 0.01) if ymin >= 0.0: ylim(ymin - yr * 0.01, ymax + yr * 0.01) else: ylim(1.0e-10, ymax + yr * 0.01) pylab.grid() pylab.draw() pylab.savefig(re.sub('\.\S+', '.png', outfile), dpi=100) ## write output file if status == 0: for i in range(len(outdata)): instr[1].data.field(datacol)[i] = outdata[i] instr.writeto(outfile) ## close input file if status == 0: status = kepio.closefits(instr, logfile, verbose) ## end time if (status == 0): message = 'KEPREGR completed at' else: message = '\nKEPREGR aborted at' kepmsg.clock(message, logfile, verbose)
def kepdip(infile,outfile,datacol,dmethod,kneighb,hstd,plot,plotlab, clobber,verbose,logfile,status): """ Perform a k-nearest neighbor regression analysis. """ ## startup parameters status = 0 labelsize = 24 ticksize = 16 xsize = 16 ysize = 6 lcolor = '#0000ff' lwidth = 1.0 fcolor = '#9AFF9A' falpha = 0.3 ## log the call hashline = '----------------------------------------------------------------------------' kepmsg.log(logfile,hashline,verbose) call = 'KEPDIP -- ' call += 'infile='+infile+' ' call += 'outfile='+outfile+' ' call += 'datacol='+str(datacol)+' ' call += 'dmethod='+dmethod+' ' call += 'hstd='+str(hstd)+' ' call += 'kneighb='+str(kneighb)+' ' plotit = 'n' if (plot): plotit = 'y' call += 'plot='+plotit+ ' ' call += 'plotlab='+str(plotlab)+' ' 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('KEPDIP 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 -- KEPDIP: ' + 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) tstart, tstop, bjdref, cadence, status = kepio.timekeys(instr,infile,logfile,verbose,status) if cadence == 0.0: tstart, tstop, ncad, cadence, status = kepio.cadence(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) # read time and flux columns if status == 0: barytime, status = kepio.readtimecol(infile,table,logfile,verbose) if status == 0: flux, status = kepio.readfitscol(infile,instr[1].data,datacol,logfile,verbose) # filter input data table if status == 0: try: nanclean = instr[1].header['NANCLEAN'] except: naxis2 = 0 for i in range(len(table.field(0))): if (numpy.isfinite(barytime[i]) and numpy.isfinite(flux[i]) and flux[i] != 0.0): table[naxis2] = table[i] naxis2 += 1 instr[1].data = table[:naxis2] comment = 'NaN cadences removed from data' status = kepkey.new('NANCLEAN',True,comment,instr[1],outfile,logfile,verbose) ## read table columns if status == 0: try: intime = instr[1].data.field('barytime') except: intime, status = kepio.readfitscol(infile,instr[1].data,'time',logfile,verbose) indata, status = kepio.readfitscol(infile,instr[1].data,datacol,logfile,verbose) if status == 0: intime = intime + bjdref indata = indata / cadenom ## smooth data if status == 0: # outdata = knn_predict(intime, indata, kmethod, kneighb) outdata_t, outdata_l, outdata_fmt = _find_dips(intime, indata, dmethod, kneighb, hstd) ## comment keyword in output file if status == 0: status = kepkey.history(call,instr[0],outfile,logfile,verbose) ## clean up x-axis unit if status == 0: intime0 = float(int(tstart / 100) * 100.0) if intime0 < 2.4e6: intime0 += 2.4e6 ptime = intime - intime0 ptime2 = outdata_t - intime0 # print ptime,intime,intime0 xlab = 'BJD $-$ %d' % intime0 ## clean up y-axis units if status == 0: pout = indata * 1.0 pout2 = outdata_l * 1.0 nrm = len(str(int(numpy.nanmax(pout))))-1 pout = pout / 10**nrm pout2 = pout2 / 10**nrm ylab = '10$^%d$ %s' % (nrm, plotlab) ## data limits xmin = numpy.nanmin(ptime) xmax = numpy.nanmax(ptime) ymin = numpy.min(pout) ymax = numpy.nanmax(pout) 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) if (len(ptime2) > 0): ptime2 = insert(ptime2,[0],[ptime2[0]]) ptime2 = append(ptime2,[ptime2[-1]]) pout2 = insert(pout2,[0],[0.0]) pout2 = append(pout2,0.0) ## plot light curve if status == 0 and plot: try: params = {'backend': 'png', 'axes.linewidth': 2.5, 'axes.labelsize': labelsize, 'axes.font': 'sans-serif', 'axes.fontweight' : 'bold', 'text.fontsize': 12, 'legend.fontsize': 12, 'xtick.labelsize': ticksize, 'ytick.labelsize': ticksize} rcParams.update(params) except: print('ERROR -- KEPDIP: install latex for scientific plotting') status = 1 if status == 0 and plot: pylab.figure(1,figsize=[xsize,ysize]) ## plot regression data ax = pylab.axes([0.06,0.1,0.93,0.87]) pylab.gca().xaxis.set_major_formatter(pylab.ScalarFormatter(useOffset=False)) pylab.gca().yaxis.set_major_formatter(pylab.ScalarFormatter(useOffset=False)) pylab.scatter(ptime, pout, color='#214CAE', s=2) if (len(ptime2) > 0): pylab.scatter(ptime2, pout2, color='#47AE10', s=35, marker='o', linewidths=2, alpha=0.4) xlabel(xlab, {'color' : 'k'}) ylabel(ylab, {'color' : 'k'}) xlim(xmin-xr*0.01,xmax+xr*0.01) if ymin >= 0.0: ylim(ymin-yr*0.01,ymax+yr*0.01) else: ylim(1.0e-10,ymax+yr*0.01) pylab.grid() pylab.draw() pylab.savefig(re.sub('\.\S+','.png',outfile),dpi=100) ## write output file if status == 0: for i in range(len(outdata_fmt)): instr[1].data.field(datacol)[i] = outdata_fmt[i] instr.writeto(outfile) ## close input file if status == 0: status = kepio.closefits(instr,logfile,verbose) ## end time if (status == 0): message = 'KEPDIP completed at' else: message = '\nKEPDIP aborted at' kepmsg.clock(message,logfile,verbose)
def kepfilter(infile,outfile,datacol,function,cutoff,passband,plot,plotlab, clobber,verbose,logfile,status,cmdLine=False): ## startup parameters status = 0 numpy.seterr(all="ignore") labelsize = 24 ticksize = 16 xsize = 16 ysize = 6 lcolor = '#0000ff' lwidth = 1.0 fcolor = '#ffff00' falpha = 0.2 ## log the call hashline = '----------------------------------------------------------------------------' kepmsg.log(logfile,hashline,verbose) call = 'KEPFILTER -- ' call += 'infile='+infile+' ' call += 'outfile='+outfile+' ' call += 'datacol='+str(datacol)+' ' call += 'function='+str(function)+' ' call += 'cutoff='+str(cutoff)+' ' call += 'passband='+str(passband)+' ' plotit = 'n' if (plot): plotit = 'y' call += 'plot='+plotit+ ' ' call += 'plotlab='+str(plotlab)+' ' 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('KEPFILTER 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 -- KEPFILTER: ' + 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) 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) # read time and flux columns if status == 0: barytime, status = kepio.readtimecol(infile,table,logfile,verbose) flux, status = kepio.readsapcol(infile,table,logfile,verbose) # filter input data table if status == 0: try: nanclean = instr[1].header['NANCLEAN'] except: naxis2 = 0 for i in range(len(table.field(0))): if (numpy.isfinite(barytime[i]) and numpy.isfinite(flux[i]) and flux[i] != 0.0): table[naxis2] = table[i] naxis2 += 1 instr[1].data = table[:naxis2] comment = 'NaN cadences removed from data' status = kepkey.new('NANCLEAN',True,comment,instr[1],outfile,logfile,verbose) ## read table columns if status == 0: intime, status = kepio.readtimecol(infile,instr[1].data,logfile,verbose) if status == 0: indata, status = kepio.readfitscol(infile,instr[1].data,datacol,logfile,verbose) if status == 0: intime = intime + bjdref indata = indata / cadenom ## define data sampling if status == 0: tr = 1.0 / (cadence / 86400) timescale = 1.0 / (cutoff / tr) ## define convolution function if status == 0: if function == 'boxcar': filtfunc = numpy.ones(numpy.ceil(timescale)) elif function == 'gauss': timescale /= 2 dx = numpy.ceil(timescale * 10 + 1) filtfunc = kepfunc.gauss() filtfunc = filtfunc([1.0,dx/2-1.0,timescale],linspace(0,dx-1,dx)) elif function == 'sinc': dx = numpy.ceil(timescale * 12 + 1) fx = linspace(0,dx-1,dx) fx = fx - dx / 2 + 0.5 fx /= timescale filtfunc = numpy.sinc(fx) filtfunc /= numpy.sum(filtfunc) ## pad time series at both ends with noise model if status == 0: ave, sigma = kepstat.stdev(indata[:len(filtfunc)]) padded = append(kepstat.randarray(np.ones(len(filtfunc)) * ave, np.ones(len(filtfunc)) * sigma), indata) ave, sigma = kepstat.stdev(indata[-len(filtfunc):]) padded = append(padded, kepstat.randarray(np.ones(len(filtfunc)) * ave, np.ones(len(filtfunc)) * sigma)) ## convolve data if status == 0: convolved = convolve(padded,filtfunc,'same') ## remove padding from the output array if status == 0: if function == 'boxcar': outdata = convolved[len(filtfunc):-len(filtfunc)] else: outdata = convolved[len(filtfunc):-len(filtfunc)] ## subtract low frequencies if status == 0 and passband == 'high': outmedian = median(outdata) outdata = indata - outdata + outmedian ## comment keyword in output file if status == 0: status = kepkey.history(call,instr[0],outfile,logfile,verbose) ## clean up x-axis unit if status == 0: intime0 = float(int(tstart / 100) * 100.0) if intime0 < 2.4e6: intime0 += 2.4e6 ptime = intime - intime0 xlab = 'BJD $-$ %d' % intime0 ## clean up y-axis units if status == 0: pout = indata * 1.0 pout2 = outdata * 1.0 nrm = len(str(int(numpy.nanmax(pout))))-1 pout = pout / 10**nrm pout2 = pout2 / 10**nrm ylab = '10$^%d$ %s' % (nrm, plotlab) ## data limits xmin = ptime.min() xmax = ptime.max() ymin = numpy.nanmin(pout) ymax = numpy.nanmax(pout) 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) pout2 = insert(pout2,[0],[0.0]) pout2 = append(pout2,0.0) ## plot light curve if status == 0 and plot: try: params = {'backend': 'png', 'axes.linewidth': 2.5, 'axes.labelsize': labelsize, 'axes.font': 'sans-serif', 'axes.fontweight' : 'bold', 'text.fontsize': 12, 'legend.fontsize': 12, 'xtick.labelsize': ticksize, 'ytick.labelsize': ticksize} rcParams.update(params) except: print 'ERROR -- KEPFILTER: install latex for scientific plotting' status = 1 if status == 0 and plot: pylab.figure(figsize=[xsize,ysize]) pylab.clf() ## plot filtered data ax = pylab.axes([0.06,0.1,0.93,0.87]) pylab.gca().xaxis.set_major_formatter(pylab.ScalarFormatter(useOffset=False)) pylab.gca().yaxis.set_major_formatter(pylab.ScalarFormatter(useOffset=False)) labels = ax.get_yticklabels() setp(labels, 'rotation', 90, fontsize=12) pylab.plot(ptime,pout,color='#ff9900',linestyle='-',linewidth=lwidth) fill(ptime,pout,color=fcolor,linewidth=0.0,alpha=falpha) if passband == 'low': pylab.plot(ptime[1:-1],pout2[1:-1],color=lcolor,linestyle='-',linewidth=lwidth) else: pylab.plot(ptime,pout2,color=lcolor,linestyle='-',linewidth=lwidth) fill(ptime,pout2,color=lcolor,linewidth=0.0,alpha=falpha) xlabel(xlab, {'color' : 'k'}) ylabel(ylab, {'color' : 'k'}) xlim(xmin-xr*0.01,xmax+xr*0.01) if ymin >= 0.0: ylim(ymin-yr*0.01,ymax+yr*0.01) else: ylim(1.0e-10,ymax+yr*0.01) pylab.grid() # render plot if cmdLine: pylab.show() else: pylab.ion() pylab.plot([]) pylab.ioff() ## write output file if status == 0: for i in range(len(outdata)): instr[1].data.field(datacol)[i] = outdata[i] instr.writeto(outfile) ## close input file if status == 0: status = kepio.closefits(instr,logfile,verbose) ## end time if (status == 0): message = 'KEPFILTER completed at' else: message = '\nKEPFILTER aborted at' kepmsg.clock(message,logfile,verbose)
def kepsmooth( infile, outfile, datacol, function, fscale, plot, plotlab, clobber, verbose, logfile, status, cmdLine=False ): ## startup parameters status = 0 labelsize = 24 ticksize = 16 xsize = 18 ysize = 6 lcolor = "#0000ff" lwidth = 1.0 fcolor = "#ffff00" falpha = 0.2 ## log the call hashline = "----------------------------------------------------------------------------" kepmsg.log(logfile, hashline, verbose) call = "KEPSMOOTH -- " call += "infile=" + infile + " " call += "outfile=" + outfile + " " call += "datacol=" + str(datacol) + " " call += "function=" + str(function) + " " call += "fscale=" + str(fscale) + " " plotit = "n" if plot: plotit = "y" call += "plot=" + plotit + " " call += "plotlab=" + str(plotlab) + " " 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("KEPSMOOTH 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 -- KEPSMOOTH: " + 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) tstart, tstop, bjdref, cadence, status = kepio.timekeys(instr, infile, logfile, verbose, status) if cadence == 0.0: tstart, tstop, ncad, cadence, status = kepio.cadence(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) # read time and flux columns if status == 0: barytime, status = kepio.readtimecol(infile, table, logfile, verbose) if status == 0: flux, status = kepio.readfitscol(infile, instr[1].data, datacol, logfile, verbose) # filter input data table if status == 0: try: nanclean = instr[1].header["NANCLEAN"] except: naxis2 = 0 for i in range(len(table.field(0))): if numpy.isfinite(barytime[i]) and numpy.isfinite(flux[i]) and flux[i] != 0.0: table[naxis2] = table[i] naxis2 += 1 instr[1].data = table[:naxis2] comment = "NaN cadences removed from data" status = kepkey.new("NANCLEAN", True, comment, instr[1], outfile, logfile, verbose) ## read table columns if status == 0: try: intime = instr[1].data.field("barytime") except: intime, status = kepio.readfitscol(infile, instr[1].data, "time", logfile, verbose) indata, status = kepio.readfitscol(infile, instr[1].data, datacol, logfile, verbose) if status == 0: intime = intime + bjdref indata = indata / cadenom ## smooth data if status == 0: outdata = kepfunc.smooth(indata, fscale / (cadence / 86400), function) ## comment keyword in output file if status == 0: status = kepkey.history(call, instr[0], outfile, logfile, verbose) ## clean up x-axis unit if status == 0: intime0 = float(int(tstart / 100) * 100.0) if intime0 < 2.4e6: intime0 += 2.4e6 ptime = intime - intime0 xlab = "BJD $-$ %d" % intime0 ## clean up y-axis units if status == 0: pout = indata * 1.0 pout2 = outdata * 1.0 nrm = len(str(int(numpy.nanmax(pout)))) - 1 pout = pout / 10 ** nrm pout2 = pout2 / 10 ** nrm ylab = "10$^%d$ %s" % (nrm, re.sub("_", "-", plotlab)) ## data limits xmin = numpy.nanmin(ptime) xmax = numpy.nanmax(ptime) ymin = numpy.min(pout) ymax = numpy.nanmax(pout) 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) pout2 = insert(pout2, [0], [0.0]) pout2 = append(pout2, 0.0) ## plot light curve if status == 0 and plot: try: params = { "backend": "png", "axes.linewidth": 2.5, "axes.labelsize": labelsize, "axes.font": "sans-serif", "axes.fontweight": "bold", "text.fontsize": 12, "legend.fontsize": 12, "xtick.labelsize": ticksize, "ytick.labelsize": ticksize, } rcParams.update(params) except: print "ERROR -- KEPSMOOTH: install latex for scientific plotting" status = 1 if status == 0 and plot: pylab.figure(1, figsize=[xsize, ysize]) # delete any fossil plots in the matplotlib window pylab.clf() # position axes inside the plotting window ax = pylab.subplot(111) pylab.subplots_adjust(0.06, 0.1, 0.93, 0.88) # 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() setp(labels, "rotation", 90) pylab.plot(ptime[1:-1], pout[1:-1], color="#ff9900", linestyle="-", linewidth=lwidth) fill(ptime, pout, color=fcolor, linewidth=0.0, alpha=falpha) pylab.plot(ptime, pout2, color=lcolor, linestyle="-", linewidth=lwidth * 4.0) pylab.xlabel(xlab, {"color": "k"}) pylab.ylabel(ylab, {"color": "k"}) xlim(xmin - xr * 0.01, xmax + xr * 0.01) if ymin >= 0.0: ylim(ymin - yr * 0.01, ymax + yr * 0.01) else: ylim(1.0e-10, ymax + yr * 0.01) pylab.grid() # render plot if cmdLine: pylab.show() else: pylab.ion() pylab.plot([]) pylab.ioff() ## write output file if status == 0: for i in range(len(outdata)): instr[1].data.field(datacol)[i] = outdata[i] instr.writeto(outfile) ## close input file if status == 0: status = kepio.closefits(instr, logfile, verbose) ## end time if status == 0: message = "KEPSMOOTH completed at" else: message = "\nKEPSMOOTH aborted at" kepmsg.clock(message, logfile, verbose)
def kepoutlier(infile,outfile,datacol,nsig,stepsize,npoly,niter, operation,ranges,plot,plotfit,clobber,verbose,logfile,status, cmdLine=False): # startup parameters status = 0 labelsize = 24 ticksize = 16 xsize = 16 ysize = 6 lcolor = '#0000ff' lwidth = 1.0 fcolor = '#ffff00' falpha = 0.2 # log the call hashline = '----------------------------------------------------------------------------' kepmsg.log(logfile,hashline,verbose) call = 'KEPOUTLIER -- ' call += 'infile='+infile+' ' call += 'outfile='+outfile+' ' call += 'datacol='+str(datacol)+' ' call += 'nsig='+str(nsig)+' ' call += 'stepsize='+str(stepsize)+' ' call += 'npoly='+str(npoly)+' ' call += 'niter='+str(niter)+' ' call += 'operation='+str(operation)+' ' call += 'ranges='+str(ranges)+' ' plotit = 'n' if (plot): plotit = 'y' call += 'plot='+plotit+ ' ' plotf = 'n' if (plotfit): plotf = 'y' call += 'plotfit='+plotf+ ' ' 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('KEPOUTLIER 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 -- KEPOUTLIER: ' + 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: try: nanclean = instr[1].header['NANCLEAN'] except: naxis2 = 0 try: for i in range(len(table.field(0))): if numpy.isfinite(table.field('barytime')[i]) and \ numpy.isfinite(table.field(datacol)[i]): table[naxis2] = table[i] naxis2 += 1 instr[1].data = table[:naxis2] except: for i in range(len(table.field(0))): if numpy.isfinite(table.field('time')[i]) and \ numpy.isfinite(table.field(datacol)[i]): table[naxis2] = table[i] naxis2 += 1 instr[1].data = table[:naxis2] comment = 'NaN cadences removed from data' status = kepkey.new('NANCLEAN',True,comment,instr[1],outfile,logfile,verbose) # read table columns if status == 0: try: intime = instr[1].data.field('barytime') + 2.4e6 except: intime, status = kepio.readfitscol(infile,instr[1].data,'time',logfile,verbose) indata, status = kepio.readfitscol(infile,instr[1].data,datacol,logfile,verbose) if status == 0: intime = intime + bjdref indata = indata / cadenom # time ranges for region to be corrected if status == 0: t1, t2, status = kepio.timeranges(ranges,logfile,verbose) cadencelis, status = kepstat.filterOnRange(intime,t1,t2) # find limits of each time step if status == 0: tstep1 = []; tstep2 = [] work = intime[0] while work < intime[-1]: tstep1.append(work) tstep2.append(array([work+stepsize,intime[-1]],dtype='float64').min()) work += stepsize # find cadence limits of each time step if status == 0: cstep1 = []; cstep2 = [] work1 = 0; work2 = 0 for i in range(len(intime)): if intime[i] >= intime[work1] and intime[i] < intime[work1] + stepsize: work2 = i else: cstep1.append(work1) cstep2.append(work2) work1 = i; work2 = i cstep1.append(work1) cstep2.append(work2) outdata = indata * 1.0 # comment keyword in output file if status == 0: status = kepkey.history(call,instr[0],outfile,logfile,verbose) # 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 = indata * 1.0 nrm = len(str(int(pout.max())))-1 pout = pout / 10**nrm ylab = '10$^%d$ e$^-$ s$^{-1}$' % 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 light curve if status == 0 and plot: plotLatex = True try: params = {'backend': 'png', 'axes.linewidth': 2.5, 'axes.labelsize': labelsize, 'axes.font': 'sans-serif', 'axes.fontweight' : 'bold', 'text.fontsize': 12, 'legend.fontsize': 12, 'xtick.labelsize': ticksize, 'ytick.labelsize': ticksize} rcParams.update(params) except: plotLatex = False if status == 0 and plot: pylab.figure(figsize=[xsize,ysize]) pylab.clf() # plot data ax = pylab.axes([0.06,0.1,0.93,0.87]) # 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() setp(labels, 'rotation', 90, fontsize=12) pylab.plot(ptime,pout,color=lcolor,linestyle='-',linewidth=lwidth) fill(ptime,pout,color=fcolor,linewidth=0.0,alpha=falpha) xlabel(xlab, {'color' : 'k'}) if not plotLatex: ylab = '10**%d electrons/sec' % nrm ylabel(ylab, {'color' : 'k'}) grid() # loop over each time step, fit data, determine rms if status == 0: masterfit = indata * 0.0 mastersigma = zeros(len(masterfit)) functype = 'poly' + str(npoly) for i in range(len(cstep1)): pinit = [indata[cstep1[i]:cstep2[i]+1].mean()] if npoly > 0: for j in range(npoly): pinit.append(0.0) pinit = array(pinit,dtype='float32') try: coeffs, errors, covar, iiter, sigma, chi2, dof, fit, plotx, ploty, status = \ kepfit.lsqclip(functype,pinit,intime[cstep1[i]:cstep2[i]+1]-intime[cstep1[i]], indata[cstep1[i]:cstep2[i]+1],None,nsig,nsig,niter,logfile, verbose) for j in range(len(coeffs)): masterfit[cstep1[i]:cstep2[i]+1] += coeffs[j] * \ (intime[cstep1[i]:cstep2[i]+1] - intime[cstep1[i]])**j for j in range(cstep1[i],cstep2[i]+1): mastersigma[j] = sigma if plotfit: pylab.plot(plotx+intime[cstep1[i]]-intime0,ploty / 10**nrm, 'g',lw='3') except: for j in range(cstep1[i],cstep2[i]+1): masterfit[j] = indata[j] mastersigma[j] = 1.0e10 message = 'WARNING -- KEPOUTLIER: could not fit range ' message += str(intime[cstep1[i]]) + '-' + str(intime[cstep2[i]]) kepmsg.warn(None,message) # reject outliers if status == 0: rejtime = []; rejdata = []; naxis2 = 0 for i in range(len(masterfit)): if abs(indata[i] - masterfit[i]) > nsig * mastersigma[i] and i in cadencelis: rejtime.append(intime[i]) rejdata.append(indata[i]) if operation == 'replace': [rnd] = kepstat.randarray([masterfit[i]],[mastersigma[i]]) table[naxis2] = table[i] table.field(datacol)[naxis2] = rnd naxis2 += 1 else: table[naxis2] = table[i] naxis2 += 1 instr[1].data = table[:naxis2] rejtime = array(rejtime,dtype='float64') rejdata = array(rejdata,dtype='float32') pylab.plot(rejtime-intime0,rejdata / 10**nrm,'ro') # plot ranges xlim(xmin-xr*0.01,xmax+xr*0.01) if ymin >= 0.0: ylim(ymin-yr*0.01,ymax+yr*0.01) else: ylim(1.0e-10,ymax+yr*0.01) # render plot if cmdLine: pylab.show() else: pylab.ion() pylab.plot([]) pylab.ioff() # write output file if status == 0: instr.writeto(outfile) # close input file if status == 0: status = kepio.closefits(instr,logfile,verbose) ## end time if (status == 0): message = 'KEPOUTLIER completed at' else: message = '\nKEPOUTLIER aborted at' kepmsg.clock(message,logfile,verbose)
def keppixseries(infile,outfile,plotfile,plottype,filter,function,cutoff,clobber,verbose,logfile,status, cmdLine=False): # input arguments status = 0 seterr(all="ignore") # log the call hashline = '----------------------------------------------------------------------------' kepmsg.log(logfile,hashline,verbose) call = 'KEPPIXSERIES -- ' call += 'infile='+infile+' ' call += 'outfile='+outfile+' ' call += 'plotfile='+plotfile+' ' call += 'plottype='+plottype+' ' filt = 'n' if (filter): filt = 'y' call += 'filter='+filt+ ' ' call += 'function='+function+' ' call += 'cutoff='+str(cutoff)+' ' 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('KEPPIXSERIES 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 -- KEPPIXSERIES: ' + outfile + ' exists. Use --clobber' status = kepmsg.err(logfile,message,verbose) # open TPF FITS file if status == 0: kepid, channel, skygroup, module, output, quarter, season, \ ra, dec, column, row, kepmag, xdim, ydim, barytime, status = \ kepio.readTPF(infile,'TIME',logfile,verbose) if status == 0: kepid, channel, skygroup, module, output, quarter, season, \ ra, dec, column, row, kepmag, xdim, ydim, tcorr, status = \ kepio.readTPF(infile,'TIMECORR',logfile,verbose) if status == 0: kepid, channel, skygroup, module, output, quarter, season, \ ra, dec, column, row, kepmag, xdim, ydim, cadno, status = \ kepio.readTPF(infile,'CADENCENO',logfile,verbose) if status == 0: kepid, channel, skygroup, module, output, quarter, season, \ ra, dec, column, row, kepmag, xdim, ydim, fluxpixels, status = \ kepio.readTPF(infile,'FLUX',logfile,verbose) if status == 0: kepid, channel, skygroup, module, output, quarter, season, \ ra, dec, column, row, kepmag, xdim, ydim, errpixels, status = \ kepio.readTPF(infile,'FLUX_ERR',logfile,verbose) if status == 0: kepid, channel, skygroup, module, output, quarter, season, \ ra, dec, column, row, kepmag, xdim, ydim, qual, status = \ kepio.readTPF(infile,'QUALITY',logfile,verbose) # read mask defintion data from TPF file if status == 0: maskimg, pixcoord1, pixcoord2, status = kepio.readMaskDefinition(infile,logfile,verbose) # print target data if status == 0: print '' print ' KepID: %s' % kepid print ' RA (J2000): %s' % ra print 'Dec (J2000): %s' % dec print ' KepMag: %s' % kepmag print ' SkyGroup: %2s' % skygroup print ' Season: %2s' % str(season) print ' Channel: %2s' % channel print ' Module: %2s' % module print ' Output: %1s' % output print '' # how many quality = 0 rows? if status == 0: npts = 0 nrows = len(fluxpixels) for i in range(nrows): if qual[i] == 0 and \ numpy.isfinite(barytime[i]) and \ numpy.isfinite(fluxpixels[i,ydim*xdim/2]): npts += 1 time = empty((npts)) timecorr = empty((npts)) cadenceno = empty((npts)) quality = empty((npts)) pixseries = empty((ydim,xdim,npts)) errseries = empty((ydim,xdim,npts)) # construct output light curves if status == 0: np = 0 for i in range(ydim): for j in range(xdim): npts = 0 for k in range(nrows): if qual[k] == 0 and \ numpy.isfinite(barytime[k]) and \ numpy.isfinite(fluxpixels[k,ydim*xdim/2]): time[npts] = barytime[k] timecorr[npts] = tcorr[k] cadenceno[npts] = cadno[k] quality[npts] = qual[k] pixseries[i,j,npts] = fluxpixels[k,np] errseries[i,j,npts] = errpixels[k,np] npts += 1 np += 1 # define data sampling if status == 0 and filter: tpf, status = kepio.openfits(infile,'readonly',logfile,verbose) if status == 0 and filter: cadence, status = kepkey.cadence(tpf[1],infile,logfile,verbose) tr = 1.0 / (cadence / 86400) timescale = 1.0 / (cutoff / tr) # define convolution function if status == 0 and filter: if function == 'boxcar': filtfunc = numpy.ones(numpy.ceil(timescale)) elif function == 'gauss': timescale /= 2 dx = numpy.ceil(timescale * 10 + 1) filtfunc = kepfunc.gauss() filtfunc = filtfunc([1.0,dx/2-1.0,timescale],linspace(0,dx-1,dx)) elif function == 'sinc': dx = numpy.ceil(timescale * 12 + 1) fx = linspace(0,dx-1,dx) fx = fx - dx / 2 + 0.5 fx /= timescale filtfunc = numpy.sinc(fx) filtfunc /= numpy.sum(filtfunc) # pad time series at both ends with noise model if status == 0 and filter: for i in range(ydim): for j in range(xdim): ave, sigma = kepstat.stdev(pixseries[i,j,:len(filtfunc)]) padded = numpy.append(kepstat.randarray(numpy.ones(len(filtfunc)) * ave, \ numpy.ones(len(filtfunc)) * sigma), pixseries[i,j,:]) ave, sigma = kepstat.stdev(pixseries[i,j,-len(filtfunc):]) padded = numpy.append(padded, kepstat.randarray(numpy.ones(len(filtfunc)) * ave, \ numpy.ones(len(filtfunc)) * sigma)) # convolve data if status == 0: convolved = convolve(padded,filtfunc,'same') # remove padding from the output array if status == 0: outdata = convolved[len(filtfunc):-len(filtfunc)] # subtract low frequencies if status == 0: outmedian = median(outdata) pixseries[i,j,:] = pixseries[i,j,:] - outdata + outmedian # construct output file if status == 0 and ydim*xdim < 1000: instruct, status = kepio.openfits(infile,'readonly',logfile,verbose) status = kepkey.history(call,instruct[0],outfile,logfile,verbose) hdulist = HDUList(instruct[0]) cols = [] cols.append(Column(name='TIME',format='D',unit='BJD - 2454833',disp='D12.7',array=time)) cols.append(Column(name='TIMECORR',format='E',unit='d',disp='E13.6',array=timecorr)) cols.append(Column(name='CADENCENO',format='J',disp='I10',array=cadenceno)) cols.append(Column(name='QUALITY',format='J',array=quality)) for i in range(ydim): for j in range(xdim): colname = 'COL%d_ROW%d' % (i+column,j+row) cols.append(Column(name=colname,format='E',disp='E13.6',array=pixseries[i,j,:])) hdu1 = new_table(ColDefs(cols)) try: hdu1.header.update('INHERIT',True,'inherit the primary header') except: status = 0 try: hdu1.header.update('EXTNAME','PIXELSERIES','name of extension') except: status = 0 try: hdu1.header.update('EXTVER',instruct[1].header['EXTVER'],'extension version number (not format version)') except: status = 0 try: hdu1.header.update('TELESCOP',instruct[1].header['TELESCOP'],'telescope') except: status = 0 try: hdu1.header.update('INSTRUME',instruct[1].header['INSTRUME'],'detector type') except: status = 0 try: hdu1.header.update('OBJECT',instruct[1].header['OBJECT'],'string version of KEPLERID') except: status = 0 try: hdu1.header.update('KEPLERID',instruct[1].header['KEPLERID'],'unique Kepler target identifier') except: status = 0 try: hdu1.header.update('RADESYS',instruct[1].header['RADESYS'],'reference frame of celestial coordinates') except: status = 0 try: hdu1.header.update('RA_OBJ',instruct[1].header['RA_OBJ'],'[deg] right ascension from KIC') except: status = 0 try: hdu1.header.update('DEC_OBJ',instruct[1].header['DEC_OBJ'],'[deg] declination from KIC') except: status = 0 try: hdu1.header.update('EQUINOX',instruct[1].header['EQUINOX'],'equinox of celestial coordinate system') except: status = 0 try: hdu1.header.update('TIMEREF',instruct[1].header['TIMEREF'],'barycentric correction applied to times') except: status = 0 try: hdu1.header.update('TASSIGN',instruct[1].header['TASSIGN'],'where time is assigned') except: status = 0 try: hdu1.header.update('TIMESYS',instruct[1].header['TIMESYS'],'time system is barycentric JD') except: status = 0 try: hdu1.header.update('BJDREFI',instruct[1].header['BJDREFI'],'integer part of BJD reference date') except: status = 0 try: hdu1.header.update('BJDREFF',instruct[1].header['BJDREFF'],'fraction of the day in BJD reference date') except: status = 0 try: hdu1.header.update('TIMEUNIT',instruct[1].header['TIMEUNIT'],'time unit for TIME, TSTART and TSTOP') except: status = 0 try: hdu1.header.update('TSTART',instruct[1].header['TSTART'],'observation start time in BJD-BJDREF') except: status = 0 try: hdu1.header.update('TSTOP',instruct[1].header['TSTOP'],'observation stop time in BJD-BJDREF') except: status = 0 try: hdu1.header.update('LC_START',instruct[1].header['LC_START'],'mid point of first cadence in MJD') except: status = 0 try: hdu1.header.update('LC_END',instruct[1].header['LC_END'],'mid point of last cadence in MJD') except: status = 0 try: hdu1.header.update('TELAPSE',instruct[1].header['TELAPSE'],'[d] TSTOP - TSTART') except: status = 0 try: hdu1.header.update('LIVETIME',instruct[1].header['LIVETIME'],'[d] TELAPSE multiplied by DEADC') except: status = 0 try: hdu1.header.update('EXPOSURE',instruct[1].header['EXPOSURE'],'[d] time on source') except: status = 0 try: hdu1.header.update('DEADC',instruct[1].header['DEADC'],'deadtime correction') except: status = 0 try: hdu1.header.update('TIMEPIXR',instruct[1].header['TIMEPIXR'],'bin time beginning=0 middle=0.5 end=1') except: status = 0 try: hdu1.header.update('TIERRELA',instruct[1].header['TIERRELA'],'[d] relative time error') except: status = 0 try: hdu1.header.update('TIERABSO',instruct[1].header['TIERABSO'],'[d] absolute time error') except: status = 0 try: hdu1.header.update('INT_TIME',instruct[1].header['INT_TIME'],'[s] photon accumulation time per frame') except: status = 0 try: hdu1.header.update('READTIME',instruct[1].header['READTIME'],'[s] readout time per frame') except: status = 0 try: hdu1.header.update('FRAMETIM',instruct[1].header['FRAMETIM'],'[s] frame time (INT_TIME + READTIME)') except: status = 0 try: hdu1.header.update('NUM_FRM',instruct[1].header['NUM_FRM'],'number of frames per time stamp') except: status = 0 try: hdu1.header.update('TIMEDEL',instruct[1].header['TIMEDEL'],'[d] time resolution of data') except: status = 0 try: hdu1.header.update('DATE-OBS',instruct[1].header['DATE-OBS'],'TSTART as UTC calendar date') except: status = 0 try: hdu1.header.update('DATE-END',instruct[1].header['DATE-END'],'TSTOP as UTC calendar date') except: status = 0 try: hdu1.header.update('BACKAPP',instruct[1].header['BACKAPP'],'background is subtracted') except: status = 0 try: hdu1.header.update('DEADAPP',instruct[1].header['DEADAPP'],'deadtime applied') except: status = 0 try: hdu1.header.update('VIGNAPP',instruct[1].header['VIGNAPP'],'vignetting or collimator correction applied') except: status = 0 try: hdu1.header.update('GAIN',instruct[1].header['GAIN'],'[electrons/count] channel gain') except: status = 0 try: hdu1.header.update('READNOIS',instruct[1].header['READNOIS'],'[electrons] read noise') except: status = 0 try: hdu1.header.update('NREADOUT',instruct[1].header['NREADOUT'],'number of read per cadence') except: status = 0 try: hdu1.header.update('TIMSLICE',instruct[1].header['TIMSLICE'],'time-slice readout sequence section') except: status = 0 try: hdu1.header.update('MEANBLCK',instruct[1].header['MEANBLCK'],'[count] FSW mean black level') except: status = 0 hdulist.append(hdu1) hdulist.writeto(outfile) status = kepkey.new('EXTNAME','APERTURE','name of extension',instruct[2],outfile,logfile,verbose) pyfits.append(outfile,instruct[2].data,instruct[2].header) status = kepio.closefits(instruct,logfile,verbose) else: message = 'WARNING -- KEPPIXSERIES: output FITS file requires > 999 columns. Non-compliant with FITS convention.' kepmsg.warn(logfile,message) # plot style if status == 0: try: params = {'backend': 'png', 'axes.linewidth': 2.0, 'axes.labelsize': 32, 'axes.font': 'sans-serif', 'axes.fontweight' : 'bold', 'text.fontsize': 8, 'legend.fontsize': 8, 'xtick.labelsize': 12, 'ytick.labelsize': 12} pylab.rcParams.update(params) except: pass # plot pixel array fmin = 1.0e33 fmax = -1.033 if status == 0: pylab.figure(num=None,figsize=[12,12]) pylab.clf() dx = 0.93 / xdim dy = 0.94 / ydim ax = pylab.axes([0.06,0.05,0.93,0.94]) pylab.gca().xaxis.set_major_formatter(pylab.ScalarFormatter(useOffset=False)) pylab.gca().yaxis.set_major_formatter(pylab.ScalarFormatter(useOffset=False)) pylab.gca().xaxis.set_major_locator(matplotlib.ticker.MaxNLocator(integer=True)) pylab.gca().yaxis.set_major_locator(matplotlib.ticker.MaxNLocator(integer=True)) labels = ax.get_yticklabels() setp(labels, 'rotation', 90, fontsize=12) pylab.xlim(numpy.min(pixcoord1) - 0.5,numpy.max(pixcoord1) + 0.5) pylab.ylim(numpy.min(pixcoord2) - 0.5,numpy.max(pixcoord2) + 0.5) pylab.xlabel('time', {'color' : 'k'}) pylab.ylabel('arbitrary flux', {'color' : 'k'}) for i in range(ydim): for j in range(xdim): tmin = amin(time) tmax = amax(time) try: numpy.isfinite(amin(pixseries[i,j,:])) numpy.isfinite(amin(pixseries[i,j,:])) fmin = amin(pixseries[i,j,:]) fmax = amax(pixseries[i,j,:]) except: ugh = 1 xmin = tmin - (tmax - tmin) / 40 xmax = tmax + (tmax - tmin) / 40 ymin = fmin - (fmax - fmin) / 20 ymax = fmax + (fmax - fmin) / 20 if kepstat.bitInBitmap(maskimg[i,j],2): pylab.axes([0.06+float(j)*dx,0.05+i*dy,dx,dy],axisbg='lightslategray') elif maskimg[i,j] == 0: pylab.axes([0.06+float(j)*dx,0.05+i*dy,dx,dy],axisbg='black') else: pylab.axes([0.06+float(j)*dx,0.05+i*dy,dx,dy]) if j == int(xdim / 2) and i == 0: pylab.setp(pylab.gca(),xticklabels=[],yticklabels=[]) elif j == 0 and i == int(ydim / 2): pylab.setp(pylab.gca(),xticklabels=[],yticklabels=[]) else: pylab.setp(pylab.gca(),xticklabels=[],yticklabels=[]) ptime = time * 1.0 ptime = numpy.insert(ptime,[0],ptime[0]) ptime = numpy.append(ptime,ptime[-1]) pflux = pixseries[i,j,:] * 1.0 pflux = numpy.insert(pflux,[0],-1000.0) pflux = numpy.append(pflux,-1000.0) pylab.plot(time,pixseries[i,j,:],color='#0000ff',linestyle='-',linewidth=0.5) if not kepstat.bitInBitmap(maskimg[i,j],2): pylab.fill(ptime,pflux,fc='lightslategray',linewidth=0.0,alpha=1.0) pylab.fill(ptime,pflux,fc='#FFF380',linewidth=0.0,alpha=1.0) if 'loc' in plottype: pylab.xlim(xmin,xmax) pylab.ylim(ymin,ymax) if 'glob' in plottype: pylab.xlim(xmin,xmax) pylab.ylim(1.0e-10,numpy.nanmax(pixseries) * 1.05) if 'full' in plottype: pylab.xlim(xmin,xmax) pylab.ylim(1.0e-10,ymax * 1.05) # render plot if cmdLine: pylab.show() else: pylab.ion() pylab.plot([]) pylab.ioff() if plotfile.lower() != 'none': pylab.savefig(plotfile) # stop time if status == 0: kepmsg.clock('KEPPIXSERIES ended at',logfile,verbose) return
def kepstddev(infile,outfile,datacol,timescale,clobber,verbose,logfile,status,cmdLine=False): # startup parameters status = 0 labelsize = 44 ticksize = 36 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 astddev = numpy.std(indata) * 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 standard deviation = %d ppm' % (timescale, median(stddev[:])) print '\nStandard deviation = %d ppm' % astddev # calculate median STDDEV if status == 0: medcdpp = ones((len(cdpp)),dtype='float32') * median(cdpp[:]) print 'Median %.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 = '%.1fhr $\sigma$ (10$^%d$ ppm)' % (timescale,nrm) ylab = '%.1fhr $\sigma$ (ppm)' % timescale # 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': 36, 'axes.font': 'sans-serif', 'axes.fontweight' : 'bold', 'text.fontsize': 12, 'legend.fontsize': 12, 'xtick.labelsize': 32, 'ytick.labelsize': 36} 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.15,0.92,0.83]) # 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)) ax.yaxis.set_major_locator(MaxNLocator(5)) # rotate y labels by 90 deg labels = ax.get_yticklabels() pylab.setp(labels, 'rotation', 90,fontsize=36) # 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) # pylab.plot(intime - intime0,medcdpp,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'}) 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)
def kepsmooth(infile,outfile,datacol,function,fscale,plot,plotlab, clobber,verbose,logfile,status, cmdLine=False): ## startup parameters status = 0 labelsize = 24 ticksize = 16 xsize = 18 ysize = 6 lcolor = '#0000ff' lwidth = 1.0 fcolor = '#ffff00' falpha = 0.2 ## log the call hashline = '----------------------------------------------------------------------------' kepmsg.log(logfile,hashline,verbose) call = 'KEPSMOOTH -- ' call += 'infile='+infile+' ' call += 'outfile='+outfile+' ' call += 'datacol='+str(datacol)+' ' call += 'function='+str(function)+' ' call += 'fscale='+str(fscale)+' ' plotit = 'n' if (plot): plotit = 'y' call += 'plot='+plotit+ ' ' call += 'plotlab='+str(plotlab)+' ' 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('KEPSMOOTH 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 -- KEPSMOOTH: ' + 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) tstart, tstop, bjdref, cadence, status = kepio.timekeys(instr,infile,logfile,verbose,status) if cadence == 0.0: tstart, tstop, ncad, cadence, status = kepio.cadence(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) # read time and flux columns if status == 0: barytime, status = kepio.readtimecol(infile,table,logfile,verbose) if status == 0: flux, status = kepio.readfitscol(infile,instr[1].data,datacol,logfile,verbose) # filter input data table if status == 0: try: nanclean = instr[1].header['NANCLEAN'] except: naxis2 = 0 for i in range(len(table.field(0))): if (numpy.isfinite(barytime[i]) and numpy.isfinite(flux[i]) and flux[i] != 0.0): table[naxis2] = table[i] naxis2 += 1 instr[1].data = table[:naxis2] comment = 'NaN cadences removed from data' status = kepkey.new('NANCLEAN',True,comment,instr[1],outfile,logfile,verbose) ## read table columns if status == 0: try: intime = instr[1].data.field('barytime') except: intime, status = kepio.readfitscol(infile,instr[1].data,'time',logfile,verbose) indata, status = kepio.readfitscol(infile,instr[1].data,datacol,logfile,verbose) if status == 0: intime = intime + bjdref indata = indata / cadenom ## smooth data if status == 0: outdata = kepfunc.smooth(indata,fscale/(cadence/86400),function) ## comment keyword in output file if status == 0: status = kepkey.history(call,instr[0],outfile,logfile,verbose) ## clean up x-axis unit if status == 0: intime0 = float(int(tstart / 100) * 100.0) if intime0 < 2.4e6: intime0 += 2.4e6 ptime = intime - intime0 xlab = 'BJD $-$ %d' % intime0 ## clean up y-axis units if status == 0: pout = indata * 1.0 pout2 = outdata * 1.0 nrm = len(str(int(numpy.nanmax(pout))))-1 pout = pout / 10**nrm pout2 = pout2 / 10**nrm ylab = '10$^%d$ %s' % (nrm, re.sub('_','-',plotlab)) ## data limits xmin = numpy.nanmin(ptime) xmax = numpy.nanmax(ptime) ymin = numpy.min(pout) ymax = numpy.nanmax(pout) 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) pout2 = insert(pout2,[0],[0.0]) pout2 = append(pout2,0.0) ## plot light curve if status == 0 and plot: try: params = {'backend': 'png', 'axes.linewidth': 2.5, 'axes.labelsize': labelsize, 'axes.font': 'sans-serif', 'axes.fontweight' : 'bold', 'text.fontsize': 12, 'legend.fontsize': 12, 'xtick.labelsize': ticksize, 'ytick.labelsize': ticksize} rcParams.update(params) except: print('ERROR -- KEPSMOOTH: install latex for scientific plotting') status = 1 if status == 0 and plot: pylab.figure(1,figsize=[xsize,ysize]) # delete any fossil plots in the matplotlib window pylab.clf() # position axes inside the plotting window ax = pylab.subplot(111) pylab.subplots_adjust(0.06,0.1,0.93,0.88) # 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() setp(labels, 'rotation', 90) pylab.plot(ptime[1:-1],pout[1:-1],color='#ff9900',linestyle='-',linewidth=lwidth) fill(ptime,pout,color=fcolor,linewidth=0.0,alpha=falpha) pylab.plot(ptime,pout2,color=lcolor,linestyle='-',linewidth=lwidth*4.0) pylab.xlabel(xlab, {'color' : 'k'}) pylab.ylabel(ylab, {'color' : 'k'}) xlim(xmin-xr*0.01,xmax+xr*0.01) if ymin >= 0.0: ylim(ymin-yr*0.01,ymax+yr*0.01) else: ylim(1.0e-10,ymax+yr*0.01) pylab.grid() # render plot if cmdLine: pylab.show() else: pylab.ion() pylab.plot([]) pylab.ioff() ## write output file if status == 0: for i in range(len(outdata)): instr[1].data.field(datacol)[i] = outdata[i] instr.writeto(outfile) ## close input file if status == 0: status = kepio.closefits(instr,logfile,verbose) ## end time if (status == 0): message = 'KEPSMOOTH completed at' else: message = '\nKEPSMOOTH aborted at' kepmsg.clock(message,logfile,verbose)
def kepoutlier(infile,outfile,datacol,nsig,stepsize,npoly,niter, operation,ranges,plot,plotfit,clobber,verbose,logfile,status, cmdLine=False): # startup parameters status = 0 labelsize = 24 ticksize = 16 xsize = 16 ysize = 6 lcolor = '#0000ff' lwidth = 1.0 fcolor = '#ffff00' falpha = 0.2 # log the call hashline = '----------------------------------------------------------------------------' kepmsg.log(logfile,hashline,verbose) call = 'KEPOUTLIER -- ' call += 'infile='+infile+' ' call += 'outfile='+outfile+' ' call += 'datacol='+str(datacol)+' ' call += 'nsig='+str(nsig)+' ' call += 'stepsize='+str(stepsize)+' ' call += 'npoly='+str(npoly)+' ' call += 'niter='+str(niter)+' ' call += 'operation='+str(operation)+' ' call += 'ranges='+str(ranges)+' ' plotit = 'n' if (plot): plotit = 'y' call += 'plot='+plotit+ ' ' plotf = 'n' if (plotfit): plotf = 'y' call += 'plotfit='+plotf+ ' ' 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('KEPOUTLIER 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 -- KEPOUTLIER: ' + 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: try: nanclean = instr[1].header['NANCLEAN'] except: naxis2 = 0 try: for i in range(len(table.field(0))): if numpy.isfinite(table.field('barytime')[i]) and \ numpy.isfinite(table.field(datacol)[i]): table[naxis2] = table[i] naxis2 += 1 instr[1].data = table[:naxis2] except: for i in range(len(table.field(0))): if numpy.isfinite(table.field('time')[i]) and \ numpy.isfinite(table.field(datacol)[i]): table[naxis2] = table[i] naxis2 += 1 instr[1].data = table[:naxis2] comment = 'NaN cadences removed from data' status = kepkey.new('NANCLEAN',True,comment,instr[1],outfile,logfile,verbose) # read table columns if status == 0: try: intime = instr[1].data.field('barytime') + 2.4e6 except: intime, status = kepio.readfitscol(infile,instr[1].data,'time',logfile,verbose) indata, status = kepio.readfitscol(infile,instr[1].data,datacol,logfile,verbose) if status == 0: intime = intime + bjdref indata = indata / cadenom # time ranges for region to be corrected if status == 0: t1, t2, status = kepio.timeranges(ranges,logfile,verbose) cadencelis, status = kepstat.filterOnRange(intime,t1,t2) # find limits of each time step if status == 0: tstep1 = []; tstep2 = [] work = intime[0] while work < intime[-1]: tstep1.append(work) tstep2.append(array([work+stepsize,intime[-1]],dtype='float64').min()) work += stepsize # find cadence limits of each time step if status == 0: cstep1 = []; cstep2 = [] work1 = 0; work2 = 0 for i in range(len(intime)): if intime[i] >= intime[work1] and intime[i] < intime[work1] + stepsize: work2 = i else: cstep1.append(work1) cstep2.append(work2) work1 = i; work2 = i cstep1.append(work1) cstep2.append(work2) outdata = indata * 1.0 # comment keyword in output file if status == 0: status = kepkey.history(call,instr[0],outfile,logfile,verbose) # 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 = indata * 1.0 nrm = len(str(int(pout.max())))-1 pout = pout / 10**nrm ylab = '10$^%d$ e$^-$ s$^{-1}$' % 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 light curve if status == 0 and plot: plotLatex = True try: params = {'backend': 'png', 'axes.linewidth': 2.5, 'axes.labelsize': labelsize, 'axes.font': 'sans-serif', 'axes.fontweight' : 'bold', 'text.fontsize': 12, 'legend.fontsize': 12, 'xtick.labelsize': ticksize, 'ytick.labelsize': ticksize} rcParams.update(params) except: plotLatex = False if status == 0 and plot: pylab.figure(figsize=[xsize,ysize]) pylab.clf() # plot data ax = pylab.axes([0.06,0.1,0.93,0.87]) # 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() setp(labels, 'rotation', 90, fontsize=12) pylab.plot(ptime,pout,color=lcolor,linestyle='-',linewidth=lwidth) fill(ptime,pout,color=fcolor,linewidth=0.0,alpha=falpha) xlabel(xlab, {'color' : 'k'}) if not plotLatex: ylab = '10**%d electrons/sec' % nrm ylabel(ylab, {'color' : 'k'}) grid() # loop over each time step, fit data, determine rms if status == 0: masterfit = indata * 0.0 mastersigma = zeros(len(masterfit)) functype = 'poly' + str(npoly) for i in range(len(cstep1)): pinit = [indata[cstep1[i]:cstep2[i]+1].mean()] if npoly > 0: for j in range(npoly): pinit.append(0.0) pinit = array(pinit,dtype='float32') try: coeffs, errors, covar, iiter, sigma, chi2, dof, fit, plotx, ploty, status = \ kepfit.lsqclip(functype,pinit,intime[cstep1[i]:cstep2[i]+1]-intime[cstep1[i]], indata[cstep1[i]:cstep2[i]+1],None,nsig,nsig,niter,logfile, verbose) for j in range(len(coeffs)): masterfit[cstep1[i]:cstep2[i]+1] += coeffs[j] * \ (intime[cstep1[i]:cstep2[i]+1] - intime[cstep1[i]])**j for j in range(cstep1[i],cstep2[i]+1): mastersigma[j] = sigma if plotfit: pylab.plot(plotx+intime[cstep1[i]]-intime0,ploty / 10**nrm, 'g',lw='3') except: for j in range(cstep1[i],cstep2[i]+1): masterfit[j] = indata[j] mastersigma[j] = 1.0e10 message = 'WARNING -- KEPOUTLIER: could not fit range ' message += str(intime[cstep1[i]]) + '-' + str(intime[cstep2[i]]) kepmsg.warn(None,message) # reject outliers if status == 0: rejtime = []; rejdata = []; naxis2 = 0 for i in range(len(masterfit)): if abs(indata[i] - masterfit[i]) > nsig * mastersigma[i] and i in cadencelis: rejtime.append(intime[i]) rejdata.append(indata[i]) if operation == 'replace': [rnd] = kepstat.randarray([masterfit[i]],[mastersigma[i]]) table[naxis2] = table[i] table.field(datacol)[naxis2] = rnd naxis2 += 1 else: table[naxis2] = table[i] naxis2 += 1 instr[1].data = table[:naxis2] rejtime = array(rejtime,dtype='float64') rejdata = array(rejdata,dtype='float32') pylab.plot(rejtime-intime0,rejdata / 10**nrm,'ro') # plot ranges xlim(xmin-xr*0.01,xmax+xr*0.01) if ymin >= 0.0: ylim(ymin-yr*0.01,ymax+yr*0.01) else: ylim(1.0e-10,ymax+yr*0.01) # render plot if cmdLine: pylab.show() else: pylab.ion() pylab.plot([]) pylab.ioff() # write output file if status == 0: instr.writeto(outfile) # close input file if status == 0: status = kepio.closefits(instr,logfile,verbose) # end time if (status == 0): message = 'KEPOUTLIER completed at' else: message = '\nKEPOUTLIER aborted at' kepmsg.clock(message,logfile,verbose)
def kepclip(infile,outfile,ranges,plot,plotcol,clobber,verbose,logfile,status,cmdLine=False): # startup parameters status = 0 labelsize = 32 ticksize = 24 xsize = 18 ysize = 10 lcolor = '#0000ff' lwidth = 1.0 fcolor = '#ffff00' falpha = 0.2 # log the call hashline = '----------------------------------------------------------------------------' kepmsg.log(logfile,hashline,verbose) call = 'KEPCLIP -- ' call += 'infile='+infile+' ' call += 'outfile='+outfile+' ' call += 'ranges='+ranges + ' ' plotit = 'n' if (plot): plotit = 'y' call += 'plot='+plotit+ ' ' call += 'plotcol='+plotcol+ ' ' 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) # time ranges for region if status == 0: t1 = []; t2 = [] t1, t2, status = kepio.timeranges(ranges,logfile,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) 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) # input data if status == 0: table = instr[1].data # read time and flux columns if status == 0: barytime, status = kepio.readtimecol(infile,table,logfile,verbose) if status == 0: flux, status = kepio.readfitscol(infile,table,plotcol,logfile,verbose) if status == 0: barytime = barytime + bjdref if 'flux' in plotcol.lower(): flux = flux / cadenom # filter input data table if status == 0: naxis2 = 0 work1 = array([],'float64') work2 = array([],'float32') for i in range(len(barytime)): if (numpy.isfinite(barytime[i]) and numpy.isfinite(flux[i]) and flux[i] != 0.0): reject = False for j in range(len(t1)): if (barytime[i] >= t1[j] and barytime[i] <= t2[j]): reject = True if not reject: table[naxis2] = table[i] work1 = append(work1,barytime[i]) work2 = append(work2,flux[i]) naxis2 += 1 # comment keyword in output file if status == 0: status = kepkey.history(call,instr[0],outfile,logfile,verbose) # write output file if status == 0: instr[1].data = table[:naxis2] comment = 'NaN cadences removed from data' status = kepkey.new('NANCLEAN',True,comment,instr[1],outfile,logfile,verbose) instr.writeto(outfile) # clean up x-axis unit if status == 0: barytime0 = float(int(tstart / 100) * 100.0) barytime = work1 - barytime0 xlab = 'BJD $-$ %d' % barytime0 # clean up y-axis units if status == 0: try: nrm = len(str(int(work2.max())))-1 except: nrm = 0 flux = work2 / 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 # plotting arguments if status == 0 and plot: try: params = {'backend': 'png', 'axes.linewidth': 2.5, 'axes.labelsize': labelsize, 'axes.font': 'sans-serif', 'axes.fontweight' : 'bold', 'text.fontsize': 12, 'legend.fontsize': 12, 'xtick.labelsize': ticksize, 'ytick.labelsize': ticksize} rcParams.update(params) except: print 'ERROR -- KEPCLIP: install latex for scientific plotting' status = 1 # clear window, plot box if status == 0 and plot: pylab.figure(figsize=[xsize,ysize]) pylab.clf() ax = pylab.axes([0.05,0.1,0.94,0.88]) # 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() setp(labels, 'rotation', 90, fontsize=12) # plot line data ltime = [barytime[0]]; ldata = [flux[0]] for i in range(1,len(flux)): if (barytime[i-1] > barytime[i] - 0.025): ltime.append(barytime[i]) ldata.append(flux[i]) else: ltime = array(ltime, dtype=float64) ldata = array(ldata, dtype=float64) pylab.plot(ltime,ldata,color=lcolor,linestyle='-',linewidth=lwidth) ltime = []; ldata = [] ltime = array(ltime, dtype=float64) ldata = array(ldata, dtype=float64) pylab.plot(ltime,ldata,color=lcolor,linestyle='-',linewidth=lwidth) # plot fill data barytime = insert(barytime,[0],[barytime[0]]) barytime = append(barytime,[barytime[-1]]) flux = insert(flux,[0],[0.0]) flux = append(flux,[0.0]) fill(barytime,flux,fc=fcolor,linewidth=0.0,alpha=falpha) xlim(xmin-xr*0.01,xmax+xr*0.01) if ymin-yr*0.01 <= 0.0: ylim(1.0e-10,ymax+yr*0.01) else: ylim(ymin-yr*0.01,ymax+yr*0.01) xlabel(xlab, {'color' : 'k'}) ylabel(ylab, {'color' : 'k'}) grid() # render plot if status == 0 and plot: if cmdLine: pylab.show() else: pylab.ion() pylab.plot([]) pylab.ioff() # close input file if status == 0: status = kepio.closefits(instr,logfile,verbose) # end time if (status == 0): message = 'KEPCLIP completed at' else: message = '\nKEPCLIP aborted at' kepmsg.clock(message,logfile,verbose)
def kepsmooth(infile,outfile,datacol,function,fscale,plot,plotlab, clobber,verbose,logfile,status, cmdLine=False): ## startup parameters status = 0 labelsize = 24 ticksize = 16 xsize = 18 ysize = 6 lcolor = '#0000ff' lwidth = 1.0 fcolor = '#ffff00' falpha = 0.2 ## log the call hashline = '----------------------------------------------------------------------------' kepmsg.log(logfile,hashline,verbose) call = 'KEPSMOOTH -- ' call += 'infile='+infile+' ' call += 'outfile='+outfile+' ' call += 'datacol='+str(datacol)+' ' call += 'function='+str(function)+' ' call += 'fscale='+str(fscale)+' ' plotit = 'n' if (plot): plotit = 'y' call += 'plot='+plotit+ ' ' call += 'plotlab='+str(plotlab)+' ' 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('KEPSMOOTH 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 -- KEPSMOOTH: ' + 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) tstart, tstop, bjdref, cadence, status = kepio.timekeys(instr,infile,logfile,verbose,status) if cadence == 0.0: tstart, tstop, ncad, cadence, status = kepio.cadence(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) # read time and flux columns if status == 0: barytime, status = kepio.readtimecol(infile,table,logfile,verbose) if status == 0: flux, status = kepio.readfitscol(infile,instr[1].data,datacol,logfile,verbose) # filter input data table if status == 0: try: nanclean = instr[1].header['NANCLEAN'] except: naxis2 = 0 for i in range(len(table.field(0))): if (numpy.isfinite(barytime[i]) and numpy.isfinite(flux[i]) and flux[i] != 0.0): table[naxis2] = table[i] naxis2 += 1 instr[1].data = table[:naxis2] comment = 'NaN cadences removed from data' status = kepkey.new('NANCLEAN',True,comment,instr[1],outfile,logfile,verbose) ## read table columns if status == 0: try: intime = instr[1].data.field('barytime') except: intime, status = kepio.readfitscol(infile,instr[1].data,'time',logfile,verbose) indata, status = kepio.readfitscol(infile,instr[1].data,datacol,logfile,verbose) if status == 0: intime = intime + bjdref indata = indata / cadenom ## smooth data if status == 0: outdata = kepfunc.smooth(indata,fscale/(cadence/86400),function) ## comment keyword in output file if status == 0: status = kepkey.history(call,instr[0],outfile,logfile,verbose) ## clean up x-axis unit if status == 0: intime0 = float(int(tstart / 100) * 100.0) if intime0 < 2.4e6: intime0 += 2.4e6 ptime = intime - intime0 xlab = 'BJD $-$ %d' % intime0 ## clean up y-axis units if status == 0: pout = indata * 1.0 pout2 = outdata * 1.0 nrm = len(str(int(numpy.nanmax(pout))))-1 pout = pout / 10**nrm pout2 = pout2 / 10**nrm ylab = '10$^%d$ %s' % (nrm, re.sub('_','-',plotlab)) ## data limits xmin = numpy.nanmin(ptime) xmax = numpy.nanmax(ptime) ymin = numpy.min(pout) ymax = numpy.nanmax(pout) 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) pout2 = insert(pout2,[0],[0.0]) pout2 = append(pout2,0.0) ## plot light curve if status == 0 and plot: try: params = {'backend': 'png', 'axes.linewidth': 2.5, 'axes.labelsize': labelsize, 'axes.font': 'sans-serif', 'axes.fontweight' : 'bold', 'text.fontsize': 12, 'legend.fontsize': 12, 'xtick.labelsize': ticksize, 'ytick.labelsize': ticksize} rcParams.update(params) except: print 'ERROR -- KEPSMOOTH: install latex for scientific plotting' status = 1 if status == 0 and plot: pylab.figure(1,figsize=[xsize,ysize]) # delete any fossil plots in the matplotlib window pylab.clf() # position axes inside the plotting window ax = pylab.subplot(111) pylab.subplots_adjust(0.06,0.1,0.93,0.88) # 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() setp(labels, 'rotation', 90) pylab.plot(ptime[1:-1],pout[1:-1],color='#ff9900',linestyle='-',linewidth=lwidth) fill(ptime,pout,color=fcolor,linewidth=0.0,alpha=falpha) pylab.plot(ptime,pout2,color=lcolor,linestyle='-',linewidth=lwidth*4.0) pylab.xlabel(xlab, {'color' : 'k'}) pylab.ylabel(ylab, {'color' : 'k'}) xlim(xmin-xr*0.01,xmax+xr*0.01) if ymin >= 0.0: ylim(ymin-yr*0.01,ymax+yr*0.01) else: ylim(1.0e-10,ymax+yr*0.01) pylab.grid() # render plot if cmdLine: pylab.show() else: pylab.ion() pylab.plot([]) pylab.ioff() ## write output file if status == 0: for i in range(len(outdata)): instr[1].data.field(datacol)[i] = outdata[i] instr.writeto(outfile) ## close input file if status == 0: status = kepio.closefits(instr,logfile,verbose) ## end time if (status == 0): message = 'KEPSMOOTH completed at' else: message = '\nKEPSMOOTH aborted at' kepmsg.clock(message,logfile,verbose)
def kepbinary(infile, outfile, datacol, m1, m2, r1, r2, period, bjd0, eccn, omega, inclination, c1, c2, c3, c4, albedo, depth, contamination, gamma, fitparams, eclipses, dopboost, tides, job, clobber, verbose, logfile, status): # startup parameters status = 0 labelsize = 24 ticksize = 16 xsize = 17 ysize = 7 lcolor = '#0000ff' lwidth = 1.0 fcolor = '#ffff00' falpha = 0.2 # log the call hashline = '----------------------------------------------------------------------------' kepmsg.log(logfile, hashline, verbose) call = 'KEPBINARY -- ' call += 'infile=' + infile + ' ' call += 'outfile=' + outfile + ' ' call += 'datacol=' + datacol + ' ' call += 'm1=' + str(m1) + ' ' call += 'm2=' + str(m2) + ' ' call += 'r1=' + str(r1) + ' ' call += 'r2=' + str(r2) + ' ' call += 'period=' + str(period) + ' ' call += 'bjd0=' + str(bjd0) + ' ' call += 'eccn=' + str(eccn) + ' ' call += 'omega=' + str(omega) + ' ' call += 'inclination=' + str(inclination) + ' ' call += 'c1=' + str(c1) + ' ' call += 'c2=' + str(c2) + ' ' call += 'c3=' + str(c3) + ' ' call += 'c4=' + str(c4) + ' ' call += 'albedo=' + str(albedo) + ' ' call += 'depth=' + str(depth) + ' ' call += 'contamination=' + str(contamination) + ' ' call += 'gamma=' + str(gamma) + ' ' call += 'fitparams=' + str(fitparams) + ' ' eclp = 'n' if (eclipses): eclp = 'y' call += 'eclipses=' + eclp + ' ' boost = 'n' if (dopboost): boost = 'y' call += 'dopboost=' + boost + ' ' distort = 'n' if (tides): distort = 'y' call += 'tides=' + distort + ' ' call += 'job=' + str(job) + ' ' 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('KEPBINARY started at', logfile, verbose) # test log file logfile = kepmsg.test(logfile) # check and format the list of fit parameters if status == 0 and job == 'fit': allParams = [m1, m2, r1, r2, period, bjd0, eccn, omega, inclination] allNames = [ 'm1', 'm2', 'r1', 'r2', 'period', 'bjd0', 'eccn', 'omega', 'inclination' ] fitparams = re.sub('\|', ',', fitparams.strip()) fitparams = re.sub('\.', ',', fitparams.strip()) fitparams = re.sub(';', ',', fitparams.strip()) fitparams = re.sub(':', ',', fitparams.strip()) fitparams = re.sub('\s+', ',', fitparams.strip()) fitparams, status = kepio.parselist(fitparams, logfile, verbose) for fitparam in fitparams: if fitparam.strip() not in allNames: message = 'ERROR -- KEPBINARY: unknown field in list of fit parameters' status = kepmsg.err(logfile, message, verbose) # clobber output file if status == 0: if clobber: status = kepio.clobber(outfile, logfile, verbose) if kepio.fileexists(outfile): message = 'ERROR -- KEPBINARY: ' + 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 # check the data column exists if status == 0: try: instr[1].data.field(datacol) except: message = 'ERROR -- KEPBINARY: ' + datacol + ' column does not exist in ' + infile + '[1]' status = kepmsg.err(logfile, message, verbose) # 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: try: nanclean = instr[1].header['NANCLEAN'] except: naxis2 = 0 try: for i in range(len(table.field(0))): if numpy.isfinite(table.field('barytime')[i]) and \ numpy.isfinite(table.field(datacol)[i]): table[naxis2] = table[i] naxis2 += 1 instr[1].data = table[:naxis2] except: for i in range(len(table.field(0))): if numpy.isfinite(table.field('time')[i]) and \ numpy.isfinite(table.field(datacol)[i]): table[naxis2] = table[i] naxis2 += 1 instr[1].data = table[:naxis2] comment = 'NaN cadences removed from data' status = kepkey.new('NANCLEAN', True, comment, instr[1], outfile, logfile, verbose) # read table columns if status == 0: try: time = instr[1].data.field('barytime') except: time, status = kepio.readfitscol(infile, instr[1].data, 'time', logfile, verbose) indata, status = kepio.readfitscol(infile, instr[1].data, datacol, logfile, verbose) if status == 0: time = time + bjdref indata = indata / cadenom # limb-darkening cofficients if status == 0: limbdark = numpy.array([c1, c2, c3, c4], dtype='float32') # time details for model if status == 0: npt = len(time) exptime = numpy.zeros((npt), dtype='float64') dtype = numpy.zeros((npt), dtype='int') for i in range(npt): try: exptime[i] = time[i + 1] - time[i] except: exptime[i] = time[i] - time[i - 1] # calculate binary model if status == 0: tmodel = kepsim.transitModel(1.0, m1, m2, r1, r2, period, inclination, bjd0, eccn, omega, depth, albedo, c1, c2, c3, c4, gamma, contamination, npt, time, exptime, dtype, eclipses, dopboost, tides) # re-normalize binary model to data if status == 0 and (job == 'overlay' or job == 'fit'): dmedian = numpy.median(indata) tmodel = tmodel / numpy.median(tmodel) * dmedian # define arrays of floating and frozen parameters if status == 0 and job == 'fit': params = [] paramNames = [] arguments = [] argNames = [] for i in range(len(allNames)): if allNames[i] in fitparams: params.append(allParams[i]) paramNames.append(allNames[i]) else: arguments.append(allParams[i]) argNames.append(allNames[i]) params.append(dmedian) params = numpy.array(params, dtype='float32') # subtract model from data if status == 0 and job == 'fit': deltam = numpy.abs(indata - tmodel) # fit statistics if status == 0 and job == 'fit': aveDelta = numpy.sum(deltam) / npt chi2 = math.sqrt( numpy.sum( (indata - tmodel) * (indata - tmodel) / (npt - len(params)))) # fit model to data using downhill simplex if status == 0 and job == 'fit': print '' print '%4s %11s %11s' % ('iter', 'delta', 'chi^2') print '----------------------------' print '%4d %.5E %.5E' % (0, aveDelta, chi2) bestFit = scipy.optimize.fmin( fitModel, params, args=(paramNames, dmedian, m1, m2, r1, r2, period, bjd0, eccn, omega, inclination, depth, albedo, c1, c2, c3, c4, gamma, contamination, npt, time, exptime, indata, dtype, eclipses, dopboost, tides), maxiter=1e4) # calculate best fit binary model if status == 0 and job == 'fit': print '' for i in range(len(paramNames)): if 'm1' in paramNames[i].lower(): m1 = bestFit[i] print ' M1 = %.3f Msun' % bestFit[i] elif 'm2' in paramNames[i].lower(): m2 = bestFit[i] print ' M2 = %.3f Msun' % bestFit[i] elif 'r1' in paramNames[i].lower(): r1 = bestFit[i] print ' R1 = %.4f Rsun' % bestFit[i] elif 'r2' in paramNames[i].lower(): r2 = bestFit[i] print ' R2 = %.4f Rsun' % bestFit[i] elif 'period' in paramNames[i].lower(): period = bestFit[i] elif 'bjd0' in paramNames[i].lower(): bjd0 = bestFit[i] print 'BJD0 = %.8f' % bestFit[i] elif 'eccn' in paramNames[i].lower(): eccn = bestFit[i] print ' e = %.3f' % bestFit[i] elif 'omega' in paramNames[i].lower(): omega = bestFit[i] print ' w = %.3f deg' % bestFit[i] elif 'inclination' in paramNames[i].lower(): inclination = bestFit[i] print ' i = %.3f deg' % bestFit[i] flux = bestFit[-1] print '' tmodel = kepsim.transitModel(flux, m1, m2, r1, r2, period, inclination, bjd0, eccn, omega, depth, albedo, c1, c2, c3, c4, gamma, contamination, npt, time, exptime, dtype, eclipses, dopboost, tides) # subtract model from data if status == 0: deltaMod = indata - tmodel # standard deviation of model if status == 0: stdDev = math.sqrt( numpy.sum((indata - tmodel) * (indata - tmodel)) / npt) # clean up x-axis unit if status == 0: time0 = float(int(tstart / 100) * 100.0) ptime = time - time0 xlab = 'BJD $-$ %d' % time0 # clean up y-axis units if status == 0: nrm = len(str(int(indata.max()))) - 1 pout = indata / 10**nrm pmod = tmodel / 10**nrm pres = deltaMod / stdDev if job == 'fit' or job == 'overlay': try: ylab1 = 'Flux (10$^%d$ e$^-$ s$^{-1}$)' % nrm ylab2 = 'Residual ($\sigma$)' except: ylab1 = 'Flux (10**%d e-/s)' % nrm ylab2 = 'Residual (sigma)' else: ylab1 = 'Normalized Flux' # dynamic range of model plot if status == 0 and job == 'model': xmin = ptime.min() xmax = ptime.max() ymin = tmodel.min() ymax = tmodel.max() # dynamic range of model/data overlay or fit if status == 0 and (job == 'overlay' or job == 'fit'): xmin = ptime.min() xmax = ptime.max() ymin = pout.min() ymax = pout.max() tmin = pmod.min() tmax = pmod.max() ymin = numpy.array([ymin, tmin]).min() ymax = numpy.array([ymax, tmax]).max() rmin = pres.min() rmax = pres.max() # pad the dynamic range if status == 0: xr = (xmax - xmin) / 80 yr = (ymax - ymin) / 40 if job == 'overlay' or job == 'fit': rr = (rmax - rmin) / 40 # set up plot style if status == 0: labelsize = 24 ticksize = 16 xsize = 17 ysize = 7 lcolor = '#0000ff' lwidth = 1.0 fcolor = '#ffff00' falpha = 0.2 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) pylab.figure(figsize=[14, 10]) pylab.clf() # main plot window ax = pylab.axes([0.05, 0.3, 0.94, 0.68]) pylab.gca().xaxis.set_major_formatter( pylab.ScalarFormatter(useOffset=False)) pylab.gca().yaxis.set_major_formatter( pylab.ScalarFormatter(useOffset=False)) labels = ax.get_yticklabels() setp(labels, 'rotation', 90, fontsize=12) # plot model time series if status == 0 and job == 'model': pylab.plot(ptime, tmodel, color='#0000ff', linestyle='-', linewidth=1.0) ptime = numpy.insert(ptime, [0.0], ptime[0]) ptime = numpy.append(ptime, ptime[-1]) tmodel = numpy.insert(tmodel, [0.0], 0.0) tmodel = numpy.append(tmodel, 0.0) pylab.fill(ptime, tmodel, fc='#ffff00', linewidth=0.0, alpha=0.2) # plot data time series and best fit if status == 0 and (job == 'overlay' or job == 'fit'): pylab.plot(ptime, pout, color='#0000ff', linestyle='-', linewidth=1.0) ptime = numpy.insert(ptime, [0.0], ptime[0]) ptime = numpy.append(ptime, ptime[-1]) pout = numpy.insert(pout, [0], 0.0) pout = numpy.append(pout, 0.0) pylab.fill(ptime, pout, fc='#ffff00', linewidth=0.0, alpha=0.2) pylab.plot(ptime[1:-1], pmod, color='r', linestyle='-', linewidth=2.0) # ranges and labels if status == 0: pylab.xlim(xmin - xr, xmax + xr) pylab.ylim(ymin - yr, ymax + yr) pylab.xlabel(xlab, {'color': 'k'}) pylab.ylabel(ylab1, {'color': 'k'}) # residual plot window if status == 0 and (job == 'overlay' or job == 'fit'): ax = pylab.axes([0.05, 0.07, 0.94, 0.23]) pylab.gca().xaxis.set_major_formatter( pylab.ScalarFormatter(useOffset=False)) pylab.gca().yaxis.set_major_formatter( pylab.ScalarFormatter(useOffset=False)) labels = ax.get_yticklabels() setp(labels, 'rotation', 90, fontsize=12) # plot residual time series if status == 0 and (job == 'overlay' or job == 'fit'): pylab.plot([ptime[0], ptime[-1]], [0.0, 0.0], color='r', linestyle='--', linewidth=1.0) pylab.plot([ptime[0], ptime[-1]], [-1.0, -1.0], color='r', linestyle='--', linewidth=1.0) pylab.plot([ptime[0], ptime[-1]], [1.0, 1.0], color='r', linestyle='--', linewidth=1.0) pylab.plot(ptime[1:-1], pres, color='#0000ff', linestyle='-', linewidth=1.0) pres = numpy.insert(pres, [0], rmin) pres = numpy.append(pres, rmin) pylab.fill(ptime, pres, fc='#ffff00', linewidth=0.0, alpha=0.2) # ranges and labels of residual time series if status == 0 and (job == 'overlay' or job == 'fit'): pylab.xlim(xmin - xr, xmax + xr) pylab.ylim(rmin - rr, rmax + rr) pylab.xlabel(xlab, {'color': 'k'}) pylab.ylabel(ylab2, {'color': 'k'}) # display the plot if status == 0: pylab.draw()
def kepdiffim(infile,outfile,plotfile,imscale,colmap,filter,function,cutoff,clobber,verbose,logfile,status,cmdLine=False): # input arguments status = 0 seterr(all="ignore") # log the call hashline = '----------------------------------------------------------------------------' kepmsg.log(logfile,hashline,verbose) call = 'KEPDIFFIM -- ' call += 'infile='+infile+' ' call += 'outfile='+outfile+' ' call += 'plotfile='+plotfile+' ' call += 'imscale='+imscale+' ' call += 'colmap='+colmap+' ' filt = 'n' if (filter): filt = 'y' call += 'filter='+filt+ ' ' call += 'function='+function+' ' call += 'cutoff='+str(cutoff)+' ' 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('KEPDIFFIM 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 -- KEPDIFFIM: ' + outfile + ' exists. Use --clobber' status = kepmsg.err(logfile,message,verbose) # reference color map if colmap == 'browse': status = cmap_plot() # open TPF FITS file if status == 0: kepid, channel, skygroup, module, output, quarter, season, \ ra, dec, column, row, kepmag, xdim, ydim, barytime, status = \ kepio.readTPF(infile,'TIME',logfile,verbose) if status == 0: kepid, channel, skygroup, module, output, quarter, season, \ ra, dec, column, row, kepmag, xdim, ydim, tcorr, status = \ kepio.readTPF(infile,'TIMECORR',logfile,verbose) if status == 0: kepid, channel, skygroup, module, output, quarter, season, \ ra, dec, column, row, kepmag, xdim, ydim, cadno, status = \ kepio.readTPF(infile,'CADENCENO',logfile,verbose) if status == 0: kepid, channel, skygroup, module, output, quarter, season, \ ra, dec, column, row, kepmag, xdim, ydim, fluxpixels, status = \ kepio.readTPF(infile,'FLUX',logfile,verbose) if status == 0: kepid, channel, skygroup, module, output, quarter, season, \ ra, dec, column, row, kepmag, xdim, ydim, errpixels, status = \ kepio.readTPF(infile,'FLUX_ERR',logfile,verbose) if status == 0: kepid, channel, skygroup, module, output, quarter, season, \ ra, dec, column, row, kepmag, xdim, ydim, qual, status = \ kepio.readTPF(infile,'QUALITY',logfile,verbose) # read mask defintion data from TPF file if status == 0: maskimg, pixcoord1, pixcoord2, status = kepio.readMaskDefinition(infile,logfile,verbose) # print target data if status == 0: print '' print ' KepID: %s' % kepid print ' RA (J2000): %s' % ra print 'Dec (J2000): %s' % dec print ' KepMag: %s' % kepmag print ' SkyGroup: %2s' % skygroup print ' Season: %2s' % str(season) print ' Channel: %2s' % channel print ' Module: %2s' % module print ' Output: %1s' % output print '' # how many quality = 0 rows? if status == 0: npts = 0 nrows = len(fluxpixels) for i in range(nrows): if qual[i] == 0 and \ numpy.isfinite(barytime[i]) and \ numpy.isfinite(fluxpixels[i,ydim*xdim/2]): npts += 1 time = empty((npts)) timecorr = empty((npts)) cadenceno = empty((npts)) quality = empty((npts)) pixseries = empty((ydim*xdim,npts)) errseries = empty((ydim*xdim,npts)) # construct output light curves if status == 0: np = 0 for i in range(ydim*xdim): npts = 0 for k in range(nrows): if qual[k] == 0 and \ numpy.isfinite(barytime[k]) and \ numpy.isfinite(fluxpixels[k,ydim*xdim/2]): time[npts] = barytime[k] timecorr[npts] = tcorr[k] cadenceno[npts] = cadno[k] quality[npts] = qual[k] pixseries[i,npts] = fluxpixels[k,np] errseries[i,npts] = errpixels[k,np] npts += 1 np += 1 # define data sampling if status == 0 and filter: tpf, status = kepio.openfits(infile,'readonly',logfile,verbose) if status == 0 and filter: cadence, status = kepkey.cadence(tpf[1],infile,logfile,verbose) tr = 1.0 / (cadence / 86400) timescale = 1.0 / (cutoff / tr) # define convolution function if status == 0 and filter: if function == 'boxcar': filtfunc = numpy.ones(numpy.ceil(timescale)) elif function == 'gauss': timescale /= 2 dx = numpy.ceil(timescale * 10 + 1) filtfunc = kepfunc.gauss() filtfunc = filtfunc([1.0,dx/2-1.0,timescale],linspace(0,dx-1,dx)) elif function == 'sinc': dx = numpy.ceil(timescale * 12 + 1) fx = linspace(0,dx-1,dx) fx = fx - dx / 2 + 0.5 fx /= timescale filtfunc = numpy.sinc(fx) filtfunc /= numpy.sum(filtfunc) # pad time series at both ends with noise model if status == 0 and filter: for i in range(ydim*xdim): ave, sigma = kepstat.stdev(pixseries[i,:len(filtfunc)]) padded = numpy.append(kepstat.randarray(numpy.ones(len(filtfunc)) * ave, \ numpy.ones(len(filtfunc)) * sigma), pixseries[i,:]) ave, sigma = kepstat.stdev(pixseries[i,-len(filtfunc):]) padded = numpy.append(padded, kepstat.randarray(numpy.ones(len(filtfunc)) * ave, \ numpy.ones(len(filtfunc)) * sigma)) # convolve data if status == 0: convolved = convolve(padded,filtfunc,'same') # remove padding from the output array if status == 0: outdata = convolved[len(filtfunc):-len(filtfunc)] # subtract low frequencies if status == 0: outmedian = median(outdata) pixseries[i,:] = pixseries[i,:] - outdata + outmedian # sum pixels over cadence if status == 0: np = 0 nrows = len(fluxpixels) pixsum = zeros((ydim*xdim)) errsum = zeros((ydim*xdim)) for i in range(npts): if quality[i] == 0: pixsum += pixseries[:,i] errsum += errseries[:,i]**2 np += 1 pixsum /= np errsum = sqrt(errsum) / np # calculate standard deviation pixels if status == 0: pixvar = zeros((ydim*xdim)) for i in range(npts): if quality[i] == 0: pixvar += (pixsum - pixseries[:,i] / errseries[:,i])**2 pixvar = numpy.sqrt(pixvar) # median pixel errors if status == 0: errmed = empty((ydim*xdim)) for i in range(ydim*xdim): errmed[i] = numpy.median(errseries[:,i]) # calculate chi distribution pixels if status == 0: pixdev = zeros((ydim*xdim)) for i in range(npts): if quality[i] == 0: pixdev += ((pixsum - pixseries[:,i]) / pixsum)**2 pixdev = numpy.sqrt(pixdev) # pixdev = numpy.sqrt(pixvar) / errsum #errmed # image scale and intensity limits if status == 0: pixsum_pl, zminsum, zmaxsum = kepplot.intScale1D(pixsum,imscale) pixvar_pl, zminvar, zmaxvar = kepplot.intScale1D(pixvar,imscale) pixdev_pl, zmindev, zmaxdev = kepplot.intScale1D(pixdev,imscale) # construct output summed image if status == 0: imgsum = empty((ydim,xdim)) imgvar = empty((ydim,xdim)) imgdev = empty((ydim,xdim)) imgsum_pl = empty((ydim,xdim)) imgvar_pl = empty((ydim,xdim)) imgdev_pl = empty((ydim,xdim)) n = 0 for i in range(ydim): for j in range(xdim): imgsum[i,j] = pixsum[n] imgvar[i,j] = pixvar[n] imgdev[i,j] = pixdev[n] imgsum_pl[i,j] = pixsum_pl[n] imgvar_pl[i,j] = pixvar_pl[n] imgdev_pl[i,j] = pixdev_pl[n] n += 1 # construct output file if status == 0: instruct, status = kepio.openfits(infile,'readonly',logfile,verbose) status = kepkey.history(call,instruct[0],outfile,logfile,verbose) hdulist = HDUList(instruct[0]) hdulist.writeto(outfile) status = kepkey.new('EXTNAME','FLUX','name of extension',instruct[2],outfile,logfile,verbose) pyfits.append(outfile,imgsum,instruct[2].header) status = kepkey.new('EXTNAME','CHI','name of extension',instruct[2],outfile,logfile,verbose) pyfits.append(outfile,imgvar,instruct[2].header) status = kepkey.new('EXTNAME','STDDEV','name of extension',instruct[2],outfile,logfile,verbose) pyfits.append(outfile,imgdev,instruct[2].header) status = kepkey.new('EXTNAME','APERTURE','name of extension',instruct[2],outfile,logfile,verbose) pyfits.append(outfile,instruct[2].data,instruct[2].header) status = kepio.closefits(instruct,logfile,verbose) # pixel limits of the subimage if status == 0: ymin = row ymax = ymin + ydim xmin = column xmax = xmin + xdim # plot limits for summed image ymin = float(ymin) - 0.5 ymax = float(ymax) - 0.5 xmin = float(xmin) - 0.5 xmax = float(xmax) - 0.5 # plot style 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': 10, 'ytick.labelsize': 10} pylab.rcParams.update(params) except: 'ERROR -- KEPDIFFIM: install latex for scientific plotting' status = 1 if status == 0: plotimage(imgsum_pl,imgvar_pl,imgdev_pl,zminsum,zminvar,zmindev, zmaxsum,zmaxvar,zmaxdev,xmin,xmax,ymin,ymax,colmap,plotfile,cmdLine) # stop time kepmsg.clock('KEPDIFFIM ended at: ',logfile,verbose) return
def keppixseries(infile, outfile, plotfile, plottype, filter, function, cutoff, clobber, verbose, logfile, status, cmdLine=False): # input arguments status = 0 seterr(all="ignore") # log the call hashline = '----------------------------------------------------------------------------' kepmsg.log(logfile, hashline, verbose) call = 'KEPPIXSERIES -- ' call += 'infile=' + infile + ' ' call += 'outfile=' + outfile + ' ' call += 'plotfile=' + plotfile + ' ' call += 'plottype=' + plottype + ' ' filt = 'n' if (filter): filt = 'y' call += 'filter=' + filt + ' ' call += 'function=' + function + ' ' call += 'cutoff=' + str(cutoff) + ' ' 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('KEPPIXSERIES 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 -- KEPPIXSERIES: ' + outfile + ' exists. Use --clobber' status = kepmsg.err(logfile, message, verbose) # open TPF FITS file if status == 0: kepid, channel, skygroup, module, output, quarter, season, \ ra, dec, column, row, kepmag, xdim, ydim, barytime, status = \ kepio.readTPF(infile,'TIME',logfile,verbose) if status == 0: kepid, channel, skygroup, module, output, quarter, season, \ ra, dec, column, row, kepmag, xdim, ydim, tcorr, status = \ kepio.readTPF(infile,'TIMECORR',logfile,verbose) if status == 0: kepid, channel, skygroup, module, output, quarter, season, \ ra, dec, column, row, kepmag, xdim, ydim, cadno, status = \ kepio.readTPF(infile,'CADENCENO',logfile,verbose) if status == 0: kepid, channel, skygroup, module, output, quarter, season, \ ra, dec, column, row, kepmag, xdim, ydim, fluxpixels, status = \ kepio.readTPF(infile,'FLUX',logfile,verbose) if status == 0: kepid, channel, skygroup, module, output, quarter, season, \ ra, dec, column, row, kepmag, xdim, ydim, errpixels, status = \ kepio.readTPF(infile,'FLUX_ERR',logfile,verbose) if status == 0: kepid, channel, skygroup, module, output, quarter, season, \ ra, dec, column, row, kepmag, xdim, ydim, qual, status = \ kepio.readTPF(infile,'QUALITY',logfile,verbose) # read mask defintion data from TPF file if status == 0: maskimg, pixcoord1, pixcoord2, status = kepio.readMaskDefinition( infile, logfile, verbose) # print target data if status == 0: print '' print ' KepID: %s' % kepid print ' RA (J2000): %s' % ra print 'Dec (J2000): %s' % dec print ' KepMag: %s' % kepmag print ' SkyGroup: %2s' % skygroup print ' Season: %2s' % str(season) print ' Channel: %2s' % channel print ' Module: %2s' % module print ' Output: %1s' % output print '' # how many quality = 0 rows? if status == 0: npts = 0 nrows = len(fluxpixels) for i in range(nrows): if qual[i] == 0 and \ numpy.isfinite(barytime[i]) and \ numpy.isfinite(fluxpixels[i,ydim*xdim/2]): npts += 1 time = empty((npts)) timecorr = empty((npts)) cadenceno = empty((npts)) quality = empty((npts)) pixseries = empty((ydim, xdim, npts)) errseries = empty((ydim, xdim, npts)) # construct output light curves if status == 0: np = 0 for i in range(ydim): for j in range(xdim): npts = 0 for k in range(nrows): if qual[k] == 0 and \ numpy.isfinite(barytime[k]) and \ numpy.isfinite(fluxpixels[k,ydim*xdim/2]): time[npts] = barytime[k] timecorr[npts] = tcorr[k] cadenceno[npts] = cadno[k] quality[npts] = qual[k] pixseries[i, j, npts] = fluxpixels[k, np] errseries[i, j, npts] = errpixels[k, np] npts += 1 np += 1 # define data sampling if status == 0 and filter: tpf, status = kepio.openfits(infile, 'readonly', logfile, verbose) if status == 0 and filter: cadence, status = kepkey.cadence(tpf[1], infile, logfile, verbose) tr = 1.0 / (cadence / 86400) timescale = 1.0 / (cutoff / tr) # define convolution function if status == 0 and filter: if function == 'boxcar': filtfunc = numpy.ones(numpy.ceil(timescale)) elif function == 'gauss': timescale /= 2 dx = numpy.ceil(timescale * 10 + 1) filtfunc = kepfunc.gauss() filtfunc = filtfunc([1.0, dx / 2 - 1.0, timescale], linspace(0, dx - 1, dx)) elif function == 'sinc': dx = numpy.ceil(timescale * 12 + 1) fx = linspace(0, dx - 1, dx) fx = fx - dx / 2 + 0.5 fx /= timescale filtfunc = numpy.sinc(fx) filtfunc /= numpy.sum(filtfunc) # pad time series at both ends with noise model if status == 0 and filter: for i in range(ydim): for j in range(xdim): ave, sigma = kepstat.stdev(pixseries[i, j, :len(filtfunc)]) padded = numpy.append(kepstat.randarray(numpy.ones(len(filtfunc)) * ave, \ numpy.ones(len(filtfunc)) * sigma), pixseries[i,j,:]) ave, sigma = kepstat.stdev(pixseries[i, j, -len(filtfunc):]) padded = numpy.append(padded, kepstat.randarray(numpy.ones(len(filtfunc)) * ave, \ numpy.ones(len(filtfunc)) * sigma)) # convolve data if status == 0: convolved = convolve(padded, filtfunc, 'same') # remove padding from the output array if status == 0: outdata = convolved[len(filtfunc):-len(filtfunc)] # subtract low frequencies if status == 0: outmedian = median(outdata) pixseries[i, j, :] = pixseries[i, j, :] - outdata + outmedian # construct output file if status == 0 and ydim * xdim < 1000: instruct, status = kepio.openfits(infile, 'readonly', logfile, verbose) status = kepkey.history(call, instruct[0], outfile, logfile, verbose) hdulist = HDUList(instruct[0]) cols = [] cols.append( Column(name='TIME', format='D', unit='BJD - 2454833', disp='D12.7', array=time)) cols.append( Column(name='TIMECORR', format='E', unit='d', disp='E13.6', array=timecorr)) cols.append( Column(name='CADENCENO', format='J', disp='I10', array=cadenceno)) cols.append(Column(name='QUALITY', format='J', array=quality)) for i in range(ydim): for j in range(xdim): colname = 'COL%d_ROW%d' % (i + column, j + row) cols.append( Column(name=colname, format='E', disp='E13.6', array=pixseries[i, j, :])) hdu1 = new_table(ColDefs(cols)) try: hdu1.header.update('INHERIT', True, 'inherit the primary header') except: status = 0 try: hdu1.header.update('EXTNAME', 'PIXELSERIES', 'name of extension') except: status = 0 try: hdu1.header.update( 'EXTVER', instruct[1].header['EXTVER'], 'extension version number (not format version)') except: status = 0 try: hdu1.header.update('TELESCOP', instruct[1].header['TELESCOP'], 'telescope') except: status = 0 try: hdu1.header.update('INSTRUME', instruct[1].header['INSTRUME'], 'detector type') except: status = 0 try: hdu1.header.update('OBJECT', instruct[1].header['OBJECT'], 'string version of KEPLERID') except: status = 0 try: hdu1.header.update('KEPLERID', instruct[1].header['KEPLERID'], 'unique Kepler target identifier') except: status = 0 try: hdu1.header.update('RADESYS', instruct[1].header['RADESYS'], 'reference frame of celestial coordinates') except: status = 0 try: hdu1.header.update('RA_OBJ', instruct[1].header['RA_OBJ'], '[deg] right ascension from KIC') except: status = 0 try: hdu1.header.update('DEC_OBJ', instruct[1].header['DEC_OBJ'], '[deg] declination from KIC') except: status = 0 try: hdu1.header.update('EQUINOX', instruct[1].header['EQUINOX'], 'equinox of celestial coordinate system') except: status = 0 try: hdu1.header.update('TIMEREF', instruct[1].header['TIMEREF'], 'barycentric correction applied to times') except: status = 0 try: hdu1.header.update('TASSIGN', instruct[1].header['TASSIGN'], 'where time is assigned') except: status = 0 try: hdu1.header.update('TIMESYS', instruct[1].header['TIMESYS'], 'time system is barycentric JD') except: status = 0 try: hdu1.header.update('BJDREFI', instruct[1].header['BJDREFI'], 'integer part of BJD reference date') except: status = 0 try: hdu1.header.update('BJDREFF', instruct[1].header['BJDREFF'], 'fraction of the day in BJD reference date') except: status = 0 try: hdu1.header.update('TIMEUNIT', instruct[1].header['TIMEUNIT'], 'time unit for TIME, TSTART and TSTOP') except: status = 0 try: hdu1.header.update('TSTART', instruct[1].header['TSTART'], 'observation start time in BJD-BJDREF') except: status = 0 try: hdu1.header.update('TSTOP', instruct[1].header['TSTOP'], 'observation stop time in BJD-BJDREF') except: status = 0 try: hdu1.header.update('LC_START', instruct[1].header['LC_START'], 'mid point of first cadence in MJD') except: status = 0 try: hdu1.header.update('LC_END', instruct[1].header['LC_END'], 'mid point of last cadence in MJD') except: status = 0 try: hdu1.header.update('TELAPSE', instruct[1].header['TELAPSE'], '[d] TSTOP - TSTART') except: status = 0 try: hdu1.header.update('LIVETIME', instruct[1].header['LIVETIME'], '[d] TELAPSE multiplied by DEADC') except: status = 0 try: hdu1.header.update('EXPOSURE', instruct[1].header['EXPOSURE'], '[d] time on source') except: status = 0 try: hdu1.header.update('DEADC', instruct[1].header['DEADC'], 'deadtime correction') except: status = 0 try: hdu1.header.update('TIMEPIXR', instruct[1].header['TIMEPIXR'], 'bin time beginning=0 middle=0.5 end=1') except: status = 0 try: hdu1.header.update('TIERRELA', instruct[1].header['TIERRELA'], '[d] relative time error') except: status = 0 try: hdu1.header.update('TIERABSO', instruct[1].header['TIERABSO'], '[d] absolute time error') except: status = 0 try: hdu1.header.update('INT_TIME', instruct[1].header['INT_TIME'], '[s] photon accumulation time per frame') except: status = 0 try: hdu1.header.update('READTIME', instruct[1].header['READTIME'], '[s] readout time per frame') except: status = 0 try: hdu1.header.update('FRAMETIM', instruct[1].header['FRAMETIM'], '[s] frame time (INT_TIME + READTIME)') except: status = 0 try: hdu1.header.update('NUM_FRM', instruct[1].header['NUM_FRM'], 'number of frames per time stamp') except: status = 0 try: hdu1.header.update('TIMEDEL', instruct[1].header['TIMEDEL'], '[d] time resolution of data') except: status = 0 try: hdu1.header.update('DATE-OBS', instruct[1].header['DATE-OBS'], 'TSTART as UTC calendar date') except: status = 0 try: hdu1.header.update('DATE-END', instruct[1].header['DATE-END'], 'TSTOP as UTC calendar date') except: status = 0 try: hdu1.header.update('BACKAPP', instruct[1].header['BACKAPP'], 'background is subtracted') except: status = 0 try: hdu1.header.update('DEADAPP', instruct[1].header['DEADAPP'], 'deadtime applied') except: status = 0 try: hdu1.header.update('VIGNAPP', instruct[1].header['VIGNAPP'], 'vignetting or collimator correction applied') except: status = 0 try: hdu1.header.update('GAIN', instruct[1].header['GAIN'], '[electrons/count] channel gain') except: status = 0 try: hdu1.header.update('READNOIS', instruct[1].header['READNOIS'], '[electrons] read noise') except: status = 0 try: hdu1.header.update('NREADOUT', instruct[1].header['NREADOUT'], 'number of read per cadence') except: status = 0 try: hdu1.header.update('TIMSLICE', instruct[1].header['TIMSLICE'], 'time-slice readout sequence section') except: status = 0 try: hdu1.header.update('MEANBLCK', instruct[1].header['MEANBLCK'], '[count] FSW mean black level') except: status = 0 hdulist.append(hdu1) hdulist.writeto(outfile) status = kepkey.new('EXTNAME', 'APERTURE', 'name of extension', instruct[2], outfile, logfile, verbose) pyfits.append(outfile, instruct[2].data, instruct[2].header) status = kepio.closefits(instruct, logfile, verbose) else: message = 'WARNING -- KEPPIXSERIES: output FITS file requires > 999 columns. Non-compliant with FITS convention.' kepmsg.warn(logfile, message) # plot style if status == 0: try: params = { 'backend': 'png', 'axes.linewidth': 2.0, 'axes.labelsize': 32, 'axes.font': 'sans-serif', 'axes.fontweight': 'bold', 'text.fontsize': 8, 'legend.fontsize': 8, 'xtick.labelsize': 12, 'ytick.labelsize': 12 } pylab.rcParams.update(params) except: pass # plot pixel array fmin = 1.0e33 fmax = -1.033 if status == 0: pylab.figure(num=None, figsize=[12, 12]) pylab.clf() dx = 0.93 / xdim dy = 0.94 / ydim ax = pylab.axes([0.06, 0.05, 0.93, 0.94]) pylab.gca().xaxis.set_major_formatter( pylab.ScalarFormatter(useOffset=False)) pylab.gca().yaxis.set_major_formatter( pylab.ScalarFormatter(useOffset=False)) pylab.gca().xaxis.set_major_locator( matplotlib.ticker.MaxNLocator(integer=True)) pylab.gca().yaxis.set_major_locator( matplotlib.ticker.MaxNLocator(integer=True)) labels = ax.get_yticklabels() setp(labels, 'rotation', 90, fontsize=12) pylab.xlim(numpy.min(pixcoord1) - 0.5, numpy.max(pixcoord1) + 0.5) pylab.ylim(numpy.min(pixcoord2) - 0.5, numpy.max(pixcoord2) + 0.5) pylab.xlabel('time', {'color': 'k'}) pylab.ylabel('arbitrary flux', {'color': 'k'}) for i in range(ydim): for j in range(xdim): tmin = amin(time) tmax = amax(time) try: numpy.isfinite(amin(pixseries[i, j, :])) numpy.isfinite(amin(pixseries[i, j, :])) fmin = amin(pixseries[i, j, :]) fmax = amax(pixseries[i, j, :]) except: ugh = 1 xmin = tmin - (tmax - tmin) / 40 xmax = tmax + (tmax - tmin) / 40 ymin = fmin - (fmax - fmin) / 20 ymax = fmax + (fmax - fmin) / 20 if kepstat.bitInBitmap(maskimg[i, j], 2): pylab.axes([0.06 + float(j) * dx, 0.05 + i * dy, dx, dy], axisbg='lightslategray') elif maskimg[i, j] == 0: pylab.axes([0.06 + float(j) * dx, 0.05 + i * dy, dx, dy], axisbg='black') else: pylab.axes([0.06 + float(j) * dx, 0.05 + i * dy, dx, dy]) if j == int(xdim / 2) and i == 0: pylab.setp(pylab.gca(), xticklabels=[], yticklabels=[]) elif j == 0 and i == int(ydim / 2): pylab.setp(pylab.gca(), xticklabels=[], yticklabels=[]) else: pylab.setp(pylab.gca(), xticklabels=[], yticklabels=[]) ptime = time * 1.0 ptime = numpy.insert(ptime, [0], ptime[0]) ptime = numpy.append(ptime, ptime[-1]) pflux = pixseries[i, j, :] * 1.0 pflux = numpy.insert(pflux, [0], -1000.0) pflux = numpy.append(pflux, -1000.0) pylab.plot(time, pixseries[i, j, :], color='#0000ff', linestyle='-', linewidth=0.5) if not kepstat.bitInBitmap(maskimg[i, j], 2): pylab.fill(ptime, pflux, fc='lightslategray', linewidth=0.0, alpha=1.0) pylab.fill(ptime, pflux, fc='#FFF380', linewidth=0.0, alpha=1.0) if 'loc' in plottype: pylab.xlim(xmin, xmax) pylab.ylim(ymin, ymax) if 'glob' in plottype: pylab.xlim(xmin, xmax) pylab.ylim(1.0e-10, numpy.nanmax(pixseries) * 1.05) if 'full' in plottype: pylab.xlim(xmin, xmax) pylab.ylim(1.0e-10, ymax * 1.05) # render plot if cmdLine: pylab.show() else: pylab.ion() pylab.plot([]) pylab.ioff() if plotfile.lower() != 'none': pylab.savefig(plotfile) # stop time if status == 0: kepmsg.clock('KEPPIXSERIES ended at', logfile, verbose) return
def kephalophot(infile, outfile, plotfile, plottype, filter, function, cutoff, clobber, verbose, logfile, status, cmdLine=False): # input arguments status = 0 seterr(all="ignore") # log the call hashline = '----------------------------------------------------------------------------' kepmsg.log(logfile, hashline, verbose) call = 'KEPHALOPHOT -- ' call += 'infile=' + infile + ' ' call += 'outfile=' + outfile + ' ' call += 'plotfile=' + plotfile + ' ' call += 'plottype=' + plottype + ' ' filt = 'n' if (filter): filt = 'y' call += 'filter=' + filt + ' ' call += 'function=' + function + ' ' call += 'cutoff=' + str(cutoff) + ' ' 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('KEPHALOPHOT 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 -- KEPHALOPHOT: ' + outfile + ' exists. Use --clobber' status = kepmsg.err(logfile, message, verbose) # open TPF FITS file if status == 0: kepid, channel, skygroup, module, output, quarter, season, \ ra, dec, column, row, kepmag, xdim, ydim, barytime, status = \ kepio.readTPF(infile,'TIME',logfile,verbose) if status == 0: kepid, channel, skygroup, module, output, quarter, season, \ ra, dec, column, row, kepmag, xdim, ydim, tcorr, status = \ kepio.readTPF(infile,'TIMECORR',logfile,verbose) if status == 0: kepid, channel, skygroup, module, output, quarter, season, \ ra, dec, column, row, kepmag, xdim, ydim, cadno, status = \ kepio.readTPF(infile,'CADENCENO',logfile,verbose) if status == 0: kepid, channel, skygroup, module, output, quarter, season, \ ra, dec, column, row, kepmag, xdim, ydim, fluxpixels, status = \ kepio.readTPF(infile,'FLUX',logfile,verbose) if status == 0: kepid, channel, skygroup, module, output, quarter, season, \ ra, dec, column, row, kepmag, xdim, ydim, errpixels, status = \ kepio.readTPF(infile,'FLUX_ERR',logfile,verbose) if status == 0: kepid, channel, skygroup, module, output, quarter, season, \ ra, dec, column, row, kepmag, xdim, ydim, qual, status = \ kepio.readTPF(infile,'QUALITY',logfile,verbose) # read mask defintion data from TPF file if status == 0: maskimg, pixcoord1, pixcoord2, status = kepio.readMaskDefinition( infile, logfile, verbose) # print target data if status == 0: print('') print(' KepID: %s' % kepid) print(' RA (J2000): %s' % ra) print('Dec (J2000): %s' % dec) print(' KepMag: %s' % kepmag) print(' SkyGroup: %2s' % skygroup) print(' Season: %2s' % str(season)) print(' Channel: %2s' % channel) print(' Module: %2s' % module) print(' Output: %1s' % output) print('') # how many quality = 0 rows? how many pixels? if status == 0: np = ydim * xdim nrows = len(fluxpixels) npts = 0 for i in range(nrows): if qual[i] < 1e4 and \ numpy.isfinite(barytime[i]) and \ numpy.isfinite(fluxpixels[i,ydim*xdim/2]): npts += 1 time = empty((npts)) timecorr = empty((npts)) cadenceno = empty((npts)) quality = empty((npts)) pixseries = zeros((npts, np)) errseries = zeros((npts, np)) # pixseries = empty((ydim,xdim,npts)) # errseries = empty((ydim,xdim,npts)) # construct output light curves if status == 0: for i in range(np): npts = 0 for j in range(nrows): if qual[j] < 1e4 and \ numpy.isfinite(barytime[j]) and \ numpy.isfinite(fluxpixels[j,i]): time[npts] = barytime[j] timecorr[npts] = tcorr[j] cadenceno[npts] = cadno[j] quality[npts] = qual[j] pixseries[npts, i] = fluxpixels[j, i] errseries[npts, i] = errpixels[j, i] npts += 1 # define data sampling if status == 0 and filter: tpf, status = kepio.openfits(infile, 'readonly', logfile, verbose) if status == 0 and filter: cadence, status = kepkey.cadence(tpf[1], infile, logfile, verbose) tr = 1.0 / (cadence / 86400) timescale = 1.0 / (cutoff / tr) # define convolution function if status == 0 and filter: if function == 'boxcar': filtfunc = numpy.ones(numpy.ceil(timescale)) elif function == 'gauss': timescale /= 2 dx = numpy.ceil(timescale * 10 + 1) filtfunc = kepfunc.gauss() filtfunc = filtfunc([1.0, dx / 2 - 1.0, timescale], linspace(0, dx - 1, dx)) elif function == 'sinc': dx = numpy.ceil(timescale * 12 + 1) fx = linspace(0, dx - 1, dx) fx = fx - dx / 2 + 0.5 fx /= timescale filtfunc = numpy.sinc(fx) filtfunc /= numpy.sum(filtfunc) # pad time series at both ends with noise model if status == 0 and filter: for i in range(ydim): for j in range(xdim): ave, sigma = kepstat.stdev(pixseries[i, j, :len(filtfunc)]) padded = numpy.append(kepstat.randarray(numpy.ones(len(filtfunc)) * ave, \ numpy.ones(len(filtfunc)) * sigma), pixseries[i,j,:]) ave, sigma = kepstat.stdev(pixseries[i, j, -len(filtfunc):]) padded = numpy.append(padded, kepstat.randarray(numpy.ones(len(filtfunc)) * ave, \ numpy.ones(len(filtfunc)) * sigma)) # convolve data if status == 0: convolved = convolve(padded, filtfunc, 'same') # remove padding from the output array if status == 0: outdata = convolved[len(filtfunc):-len(filtfunc)] # subtract low frequencies if status == 0: outmedian = median(outdata) pixseries[i, j, :] = pixseries[i, j, :] - outdata + outmedian # construct weighted time series if status == 0: wgt = numpy.ones((np, 3)) twgt = numpy.ones((np, 3)) wgt /= sum(wgt, axis=0) satlvl = 0.8 * numpy.max(numpy.max(pixseries, axis=1)) brk1 = 9.7257203 brk2 = 45. ind1 = where(time - time[0] < brk1) ind2 = where((time - time[0] >= brk1) & (time - time[0] < brk2)) ind3 = where(time - time[0] >= brk2) z = numpy.array([0.0, 0.0, 0.0]) for i in range(np): if max(pixseries[ind1, i].flatten()) > satlvl or max( pixseries[ind1, i].flatten()) <= 100: wgt[i, 0] = 0 z[0] += 1 if max(pixseries[ind2, i].flatten()) > satlvl or max( pixseries[ind2, i].flatten()) <= 100: wgt[i, 1] = 0 z[1] += 1 if max(pixseries[ind3, i].flatten()) > satlvl or max( pixseries[ind3, i].flatten()) <= 100: wgt[i, 2] = 0 z[2] += 1 print(z) print(np - z) sf1 = numpy.dot(pixseries[ind1, :], wgt[:, 0]).flatten() sf2 = numpy.dot(pixseries[ind2, :], wgt[:, 1]).flatten() sf3 = numpy.dot(pixseries[ind3, :], wgt[:, 2]).flatten() sf1 /= numpy.median(sf1) sf2 /= numpy.median(sf2) sf3 /= numpy.median(sf3) originalflux = numpy.concatenate([sf1, sf2, sf3]) # a=numpy.array([0.0,0.0,0.0]) # t=0 # ca = numpy.array([0.0,0.0,0.0]) # ct = 0 # sig1 = numpy.std(sf1) # sig2 = numpy.std(sf2) # sig3 = numpy.std(sf3) # while 1: # j = int(numpy.floor(numpy.random.random()*np)) # if sum(wgt[j,:]) == 0: continue # if ct == 1000: # print(ca) # if ca[0] < 333 and ca[1] < 333 and ca[2] < 333: break # ca = numpy.array([0.0,0.0,0.0]) # ct = 0 # t += 1 # ct += 1 # wgt /= sum(wgt,axis=0) # twgt=copy(wgt) # twgt[j,:]*=numpy.random.normal(1.0,0.05,3) # twgt /= sum(twgt,axis=0) # tsf1 = numpy.dot(pixseries[ind1,:],twgt[:,0]).flatten() # tsf2 = numpy.dot(pixseries[ind2,:],twgt[:,1]).flatten() # tsf3 = numpy.dot(pixseries[ind3,:],twgt[:,2]).flatten() # tsf1 /= numpy.median(tsf1) # tsf2 /= numpy.median(tsf2) # tsf3 /= numpy.median(tsf3) # tsig1 = numpy.std(tsf1) # tsig2 = numpy.std(tsf2) # tsig3 = numpy.std(tsf3) # if tsig1 < sig1: # wgt[:,0] = twgt[:,0] # sig1 = tsig1 # a[0] += 1 # ca[0] += 1 # if tsig2 < sig2: # wgt[:,1] = twgt[:,1] # sig2 = tsig2 # a[1] += 1 # ca[1] += 1 # if tsig3 < sig3: # wgt[:,2] = twgt[:,2] # sig3 = tsig3 # a[2] += 1 # ca[2] += 1 # print(100*a/t) # sf1 = numpy.dot(pixseries[ind1,:],wgt[:,0]).flatten() # sf2 = numpy.dot(pixseries[ind2,:],wgt[:,1]).flatten() # sf3 = numpy.dot(pixseries[ind3,:],wgt[:,2]).flatten() # sf1 /= numpy.median(sf1) # sf2 /= numpy.median(sf2) # sf3 /= numpy.median(sf3) # # a=numpy.array([0.0,0.0,0.0]) # t=0 # ca = numpy.array([0.0,0.0,0.0]) # ct = 0 # sig1 = sum(numpy.fabs(sf1[1:]-sf1[:-1])) # sig2 = sum(numpy.fabs(sf2[1:]-sf2[:-1])) # sig3 = sum(numpy.fabs(sf3[1:]-sf3[:-1])) # while 1: # j = int(numpy.floor(numpy.random.random()*np)) # if sum(wgt[j,:]) == 0: continue # if ct == 1000: # print(ca) # if ca[0] < 167 and ca[1] < 167 and ca[2] < 167: break# # ca = numpy.array([0.0,0.0,0.0]) # ct = 0 # t += 1 # ct += 1 # wgt /= sum(wgt,axis=0) # twgt=copy(wgt) # twgt[j,:]*=numpy.random.normal(1.0,0.05,3) # twgt /= sum(twgt,axis=0) # tsf1 = numpy.dot(pixseries[ind1,:],twgt[:,0]).flatten() # tsf2 = numpy.dot(pixseries[ind2,:],twgt[:,1]).flatten() # tsf3 = numpy.dot(pixseries[ind3,:],twgt[:,2]).flatten() # tsf1 /= numpy.median(tsf1) # tsf2 /= numpy.median(tsf2) # tsf3 /= numpy.median(tsf3) # tsig1 = sum(numpy.fabs(tsf1[1:]-tsf1[:-1])) # tsig2 = sum(numpy.fabs(tsf2[1:]-tsf2[:-1])) # tsig3 = sum(numpy.fabs(tsf3[1:]-tsf3[:-1])) # if tsig1 < sig1: # wgt[:,0] = twgt[:,0] # sig1 = tsig1 # a[0] += 1 # ca[0] += 1 # if tsig2 < sig2: # wgt[:,1] = twgt[:,1] # sig2 = tsig2 # a[1] += 1 # ca[1] += 1 # if tsig3 < sig3: # wgt[:,2] = twgt[:,2] # sig3 = tsig3 # a[2] += 1 # ca[2] += 1 # print(100*a/t) # sf1 = numpy.dot(pixseries[ind1,:],wgt[:,0]).flatten() # sf2 = numpy.dot(pixseries[ind2,:],wgt[:,1]).flatten() # sf3 = numpy.dot(pixseries[ind3,:],wgt[:,2]).flatten() # sf1 /= numpy.median(sf1) # sf2 /= numpy.median(sf2) # sf3 /= numpy.median(sf3) a = numpy.array([0.0, 0.0, 0.0]) t = 0 ca = numpy.array([0.0, 0.0, 0.0]) ct = 0 sig1 = sum(numpy.fabs(sf1[2:] - 2 * sf1[1:-1] + sf1[:-2])) sig2 = sum(numpy.fabs(sf2[2:] - 2 * sf2[1:-1] + sf2[:-2])) sig3 = sum(numpy.fabs(sf3[2:] - 2 * sf3[1:-1] + sf3[:-2])) while 1: j = int(numpy.floor(numpy.random.random() * np)) if sum(wgt[j, :]) == 0: continue if ct == 1000: print(ca) if ca[0] < 20 and ca[1] < 20 and ca[2] < 20: break if t > 1000000: break ca = numpy.array([0.0, 0.0, 0.0]) ct = 0 t += 1 ct += 1 wgt /= sum(wgt, axis=0) twgt = copy(wgt) twgt[j, :] *= numpy.random.normal(1.0, 0.05, 3) twgt /= sum(twgt, axis=0) tsf1 = numpy.dot(pixseries[ind1, :], twgt[:, 0]).flatten() tsf2 = numpy.dot(pixseries[ind2, :], twgt[:, 1]).flatten() tsf3 = numpy.dot(pixseries[ind3, :], twgt[:, 2]).flatten() tsf1 /= numpy.median(tsf1) tsf2 /= numpy.median(tsf2) tsf3 /= numpy.median(tsf3) tsig1 = sum(numpy.fabs(tsf1[2:] - 2 * tsf1[1:-1] + tsf1[:-2])) tsig2 = sum(numpy.fabs(tsf2[2:] - 2 * tsf2[1:-1] + tsf2[:-2])) tsig3 = sum(numpy.fabs(tsf3[2:] - 2 * tsf3[1:-1] + tsf3[:-2])) if tsig1 < sig1: wgt[:, 0] = twgt[:, 0] sig1 = tsig1 a[0] += 1 ca[0] += 1 if tsig2 < sig2: wgt[:, 1] = twgt[:, 1] sig2 = tsig2 a[1] += 1 ca[1] += 1 if tsig3 < sig3: wgt[:, 2] = twgt[:, 2] sig3 = tsig3 a[2] += 1 ca[2] += 1 print(100 * a / t) sf1 = numpy.dot(pixseries[ind1, :], wgt[:, 0]).flatten() sf2 = numpy.dot(pixseries[ind2, :], wgt[:, 1]).flatten() sf3 = numpy.dot(pixseries[ind3, :], wgt[:, 2]).flatten() sf1 /= numpy.median(sf1) sf2 /= numpy.median(sf2) sf3 /= numpy.median(sf3) finalflux = numpy.concatenate([sf1, sf2, sf3]) # construct output file if status == 0: instruct, status = kepio.openfits(infile, 'readonly', logfile, verbose) status = kepkey.history(call, instruct[0], outfile, logfile, verbose) hdulist = HDUList(instruct[0]) cols = [] cols.append( Column(name='TIME', format='D', unit='BJD - 2454833', disp='D12.7', array=time)) cols.append( Column(name='TIMECORR', format='E', unit='d', disp='E13.6', array=timecorr)) cols.append( Column(name='CADENCENO', format='J', disp='I10', array=cadenceno)) cols.append(Column(name='QUALITY', format='J', array=quality)) cols.append( Column(name='ORGFLUX', format='E', disp='E13.6', array=originalflux)) cols.append( Column(name='FLUX', format='E', disp='E13.6', array=finalflux)) # for i in range(ydim): # for j in range(xdim): # colname = 'COL%d_ROW%d' % (i+column,j+row) # cols.append(Column(name=colname,format='E',disp='E13.6',array=pixseries[i,j,:])) hdu1 = new_table(ColDefs(cols)) try: hdu1.header.update('INHERIT', True, 'inherit the primary header') except: status = 0 try: hdu1.header.update('EXTNAME', 'PIXELSERIES', 'name of extension') except: status = 0 try: hdu1.header.update( 'EXTVER', instruct[1].header['EXTVER'], 'extension version number (not format version)') except: status = 0 try: hdu1.header.update('TELESCOP', instruct[1].header['TELESCOP'], 'telescope') except: status = 0 try: hdu1.header.update('INSTRUME', instruct[1].header['INSTRUME'], 'detector type') except: status = 0 try: hdu1.header.update('OBJECT', instruct[1].header['OBJECT'], 'string version of KEPLERID') except: status = 0 try: hdu1.header.update('KEPLERID', instruct[1].header['KEPLERID'], 'unique Kepler target identifier') except: status = 0 try: hdu1.header.update('RADESYS', instruct[1].header['RADESYS'], 'reference frame of celestial coordinates') except: status = 0 try: hdu1.header.update('RA_OBJ', instruct[1].header['RA_OBJ'], '[deg] right ascension from KIC') except: status = 0 try: hdu1.header.update('DEC_OBJ', instruct[1].header['DEC_OBJ'], '[deg] declination from KIC') except: status = 0 try: hdu1.header.update('EQUINOX', instruct[1].header['EQUINOX'], 'equinox of celestial coordinate system') except: status = 0 try: hdu1.header.update('TIMEREF', instruct[1].header['TIMEREF'], 'barycentric correction applied to times') except: status = 0 try: hdu1.header.update('TASSIGN', instruct[1].header['TASSIGN'], 'where time is assigned') except: status = 0 try: hdu1.header.update('TIMESYS', instruct[1].header['TIMESYS'], 'time system is barycentric JD') except: status = 0 try: hdu1.header.update('BJDREFI', instruct[1].header['BJDREFI'], 'integer part of BJD reference date') except: status = 0 try: hdu1.header.update('BJDREFF', instruct[1].header['BJDREFF'], 'fraction of the day in BJD reference date') except: status = 0 try: hdu1.header.update('TIMEUNIT', instruct[1].header['TIMEUNIT'], 'time unit for TIME, TSTART and TSTOP') except: status = 0 try: hdu1.header.update('TSTART', instruct[1].header['TSTART'], 'observation start time in BJD-BJDREF') except: status = 0 try: hdu1.header.update('TSTOP', instruct[1].header['TSTOP'], 'observation stop time in BJD-BJDREF') except: status = 0 try: hdu1.header.update('LC_START', instruct[1].header['LC_START'], 'mid point of first cadence in MJD') except: status = 0 try: hdu1.header.update('LC_END', instruct[1].header['LC_END'], 'mid point of last cadence in MJD') except: status = 0 try: hdu1.header.update('TELAPSE', instruct[1].header['TELAPSE'], '[d] TSTOP - TSTART') except: status = 0 try: hdu1.header.update('LIVETIME', instruct[1].header['LIVETIME'], '[d] TELAPSE multiplied by DEADC') except: status = 0 try: hdu1.header.update('EXPOSURE', instruct[1].header['EXPOSURE'], '[d] time on source') except: status = 0 try: hdu1.header.update('DEADC', instruct[1].header['DEADC'], 'deadtime correction') except: status = 0 try: hdu1.header.update('TIMEPIXR', instruct[1].header['TIMEPIXR'], 'bin time beginning=0 middle=0.5 end=1') except: status = 0 try: hdu1.header.update('TIERRELA', instruct[1].header['TIERRELA'], '[d] relative time error') except: status = 0 try: hdu1.header.update('TIERABSO', instruct[1].header['TIERABSO'], '[d] absolute time error') except: status = 0 try: hdu1.header.update('INT_TIME', instruct[1].header['INT_TIME'], '[s] photon accumulation time per frame') except: status = 0 try: hdu1.header.update('READTIME', instruct[1].header['READTIME'], '[s] readout time per frame') except: status = 0 try: hdu1.header.update('FRAMETIM', instruct[1].header['FRAMETIM'], '[s] frame time (INT_TIME + READTIME)') except: status = 0 try: hdu1.header.update('NUM_FRM', instruct[1].header['NUM_FRM'], 'number of frames per time stamp') except: status = 0 try: hdu1.header.update('TIMEDEL', instruct[1].header['TIMEDEL'], '[d] time resolution of data') except: status = 0 try: hdu1.header.update('DATE-OBS', instruct[1].header['DATE-OBS'], 'TSTART as UTC calendar date') except: status = 0 try: hdu1.header.update('DATE-END', instruct[1].header['DATE-END'], 'TSTOP as UTC calendar date') except: status = 0 try: hdu1.header.update('BACKAPP', instruct[1].header['BACKAPP'], 'background is subtracted') except: status = 0 try: hdu1.header.update('DEADAPP', instruct[1].header['DEADAPP'], 'deadtime applied') except: status = 0 try: hdu1.header.update('VIGNAPP', instruct[1].header['VIGNAPP'], 'vignetting or collimator correction applied') except: status = 0 try: hdu1.header.update('GAIN', instruct[1].header['GAIN'], '[electrons/count] channel gain') except: status = 0 try: hdu1.header.update('READNOIS', instruct[1].header['READNOIS'], '[electrons] read noise') except: status = 0 try: hdu1.header.update('NREADOUT', instruct[1].header['NREADOUT'], 'number of read per cadence') except: status = 0 try: hdu1.header.update('TIMSLICE', instruct[1].header['TIMSLICE'], 'time-slice readout sequence section') except: status = 0 try: hdu1.header.update('MEANBLCK', instruct[1].header['MEANBLCK'], '[count] FSW mean black level') except: status = 0 hdulist.append(hdu1) hdulist.writeto(outfile) status = kepkey.new('EXTNAME', 'APERTURE', 'name of extension', instruct[2], outfile, logfile, verbose) pyfits.append(outfile, instruct[2].data, instruct[2].header) wgt1 = numpy.reshape(wgt[:, 0], (ydim, xdim)) wgt2 = numpy.reshape(wgt[:, 1], (ydim, xdim)) wgt3 = numpy.reshape(wgt[:, 2], (ydim, xdim)) hdu3 = ImageHDU(data=wgt1, header=instruct[2].header, name='WEIGHTS1') hdu4 = ImageHDU(data=wgt2, header=instruct[2].header, name='WEIGHTS2') hdu5 = ImageHDU(data=wgt3, header=instruct[2].header, name='WEIGHTS3') pyfits.append(outfile, hdu3.data, hdu3.header) pyfits.append(outfile, hdu4.data, hdu4.header) pyfits.append(outfile, hdu5.data, hdu5.header) status = kepio.closefits(instruct, logfile, verbose) else: message = 'WARNING -- KEPHALOPHOT: output FITS file requires > 999 columns. Non-compliant with FITS convention.' kepmsg.warn(logfile, message) # plot style if status == 0: try: params = { 'backend': 'png', 'axes.linewidth': 2.0, 'axes.labelsize': 32, 'axes.font': 'sans-serif', 'axes.fontweight': 'bold', 'text.fontsize': 8, 'legend.fontsize': 8, 'xtick.labelsize': 12, 'ytick.labelsize': 12 } pylab.rcParams.update(params) except: pass # plot pixel array fmin = 1.0e33 fmax = -1.033 if status == 0: pylab.figure(num=None, figsize=[12, 12]) pylab.clf() dx = 0.93 #/ xdim dy = 0.94 #/ ydim ax = pylab.axes([0.06, 0.05, 0.93, 0.94]) pylab.gca().xaxis.set_major_formatter( pylab.ScalarFormatter(useOffset=False)) pylab.gca().yaxis.set_major_formatter( pylab.ScalarFormatter(useOffset=False)) pylab.gca().xaxis.set_major_locator( matplotlib.ticker.MaxNLocator(integer=True)) pylab.gca().yaxis.set_major_locator( matplotlib.ticker.MaxNLocator(integer=True)) labels = ax.get_yticklabels() setp(labels, 'rotation', 90, fontsize=12) pylab.xlim(numpy.min(pixcoord1) - 0.5, numpy.max(pixcoord1) + 0.5) pylab.ylim(numpy.min(pixcoord2) - 0.5, numpy.max(pixcoord2) + 0.5) pylab.xlabel('time', {'color': 'k'}) pylab.ylabel('arbitrary flux', {'color': 'k'}) tmin = amin(time) tmax = amax(time) try: numpy.isfinite(amin(finalflux)) numpy.isfinite(amin(finalflux)) fmin = amin(finalflux) fmax = amax(finalflux) except: ugh = 1 xmin = tmin - (tmax - tmin) / 40 xmax = tmax + (tmax - tmin) / 40 ymin = fmin - (fmax - fmin) / 20 ymax = fmax + (fmax - fmin) / 20 pylab.axes([0.06, 0.05, dx, dy]) pylab.setp(pylab.gca(), xticklabels=[], yticklabels=[]) ptime = time * 1.0 ptime = numpy.insert(ptime, [0], ptime[0]) ptime = numpy.append(ptime, ptime[-1]) pflux = finalflux * 1.0 pflux = numpy.insert(pflux, [0], -1000.0) pflux = numpy.append(pflux, -1000.0) pylab.plot(time, finalflux, color='#0000ff', linestyle='-', linewidth=0.5) pylab.fill(ptime, pflux, fc='#FFF380', linewidth=0.0, alpha=1.0) if 'loc' in plottype: pylab.xlim(xmin, xmax) pylab.ylim(ymin, ymax) if 'glob' in plottype: pylab.xlim(xmin, xmax) pylab.ylim(1.0e-10, numpy.nanmax(pixseries) * 1.05) if 'full' in plottype: pylab.xlim(xmin, xmax) pylab.ylim(1.0e-10, ymax * 1.05) # render plot if cmdLine: pylab.show() else: pylab.ion() pylab.plot([]) pylab.ioff() if plotfile.lower() != 'none': pylab.savefig(plotfile) # stop time if status == 0: kepmsg.clock('KEPHALOPHOT ended at', logfile, verbose) return
def kepdiffim(infile, outfile, plotfile, imscale, colmap, filter, function, cutoff, clobber, verbose, logfile, status, cmdLine=False): # input arguments status = 0 seterr(all="ignore") # log the call hashline = '----------------------------------------------------------------------------' kepmsg.log(logfile, hashline, verbose) call = 'KEPDIFFIM -- ' call += 'infile=' + infile + ' ' call += 'outfile=' + outfile + ' ' call += 'plotfile=' + plotfile + ' ' call += 'imscale=' + imscale + ' ' call += 'colmap=' + colmap + ' ' filt = 'n' if (filter): filt = 'y' call += 'filter=' + filt + ' ' call += 'function=' + function + ' ' call += 'cutoff=' + str(cutoff) + ' ' 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('KEPDIFFIM 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 -- KEPDIFFIM: ' + outfile + ' exists. Use --clobber' status = kepmsg.err(logfile, message, verbose) # reference color map if colmap == 'browse': status = cmap_plot() # open TPF FITS file if status == 0: kepid, channel, skygroup, module, output, quarter, season, \ ra, dec, column, row, kepmag, xdim, ydim, barytime, status = \ kepio.readTPF(infile,'TIME',logfile,verbose) if status == 0: kepid, channel, skygroup, module, output, quarter, season, \ ra, dec, column, row, kepmag, xdim, ydim, tcorr, status = \ kepio.readTPF(infile,'TIMECORR',logfile,verbose) if status == 0: kepid, channel, skygroup, module, output, quarter, season, \ ra, dec, column, row, kepmag, xdim, ydim, cadno, status = \ kepio.readTPF(infile,'CADENCENO',logfile,verbose) if status == 0: kepid, channel, skygroup, module, output, quarter, season, \ ra, dec, column, row, kepmag, xdim, ydim, fluxpixels, status = \ kepio.readTPF(infile,'FLUX',logfile,verbose) if status == 0: kepid, channel, skygroup, module, output, quarter, season, \ ra, dec, column, row, kepmag, xdim, ydim, errpixels, status = \ kepio.readTPF(infile,'FLUX_ERR',logfile,verbose) if status == 0: kepid, channel, skygroup, module, output, quarter, season, \ ra, dec, column, row, kepmag, xdim, ydim, qual, status = \ kepio.readTPF(infile,'QUALITY',logfile,verbose) # read mask defintion data from TPF file if status == 0: maskimg, pixcoord1, pixcoord2, status = kepio.readMaskDefinition( infile, logfile, verbose) # print target data if status == 0: print('') print(' KepID: %s' % kepid) print(' RA (J2000): %s' % ra) print('Dec (J2000): %s' % dec) print(' KepMag: %s' % kepmag) print(' SkyGroup: %2s' % skygroup) print(' Season: %2s' % str(season)) print(' Channel: %2s' % channel) print(' Module: %2s' % module) print(' Output: %1s' % output) print('') # how many quality = 0 rows? if status == 0: npts = 0 nrows = len(fluxpixels) for i in range(nrows): if qual[i] == 0 and \ numpy.isfinite(barytime[i]) and \ numpy.isfinite(fluxpixels[i,ydim*xdim/2]): npts += 1 time = empty((npts)) timecorr = empty((npts)) cadenceno = empty((npts)) quality = empty((npts)) pixseries = empty((ydim * xdim, npts)) errseries = empty((ydim * xdim, npts)) # construct output light curves if status == 0: np = 0 for i in range(ydim * xdim): npts = 0 for k in range(nrows): if qual[k] == 0 and \ numpy.isfinite(barytime[k]) and \ numpy.isfinite(fluxpixels[k,ydim*xdim/2]): time[npts] = barytime[k] timecorr[npts] = tcorr[k] cadenceno[npts] = cadno[k] quality[npts] = qual[k] pixseries[i, npts] = fluxpixels[k, np] errseries[i, npts] = errpixels[k, np] npts += 1 np += 1 # define data sampling if status == 0 and filter: tpf, status = kepio.openfits(infile, 'readonly', logfile, verbose) if status == 0 and filter: cadence, status = kepkey.cadence(tpf[1], infile, logfile, verbose) tr = 1.0 / (cadence / 86400) timescale = 1.0 / (cutoff / tr) # define convolution function if status == 0 and filter: if function == 'boxcar': filtfunc = numpy.ones(numpy.ceil(timescale)) elif function == 'gauss': timescale /= 2 dx = numpy.ceil(timescale * 10 + 1) filtfunc = kepfunc.gauss() filtfunc = filtfunc([1.0, dx / 2 - 1.0, timescale], linspace(0, dx - 1, dx)) elif function == 'sinc': dx = numpy.ceil(timescale * 12 + 1) fx = linspace(0, dx - 1, dx) fx = fx - dx / 2 + 0.5 fx /= timescale filtfunc = numpy.sinc(fx) filtfunc /= numpy.sum(filtfunc) # pad time series at both ends with noise model if status == 0 and filter: for i in range(ydim * xdim): ave, sigma = kepstat.stdev(pixseries[i, :len(filtfunc)]) padded = numpy.append(kepstat.randarray(numpy.ones(len(filtfunc)) * ave, \ numpy.ones(len(filtfunc)) * sigma), pixseries[i,:]) ave, sigma = kepstat.stdev(pixseries[i, -len(filtfunc):]) padded = numpy.append(padded, kepstat.randarray(numpy.ones(len(filtfunc)) * ave, \ numpy.ones(len(filtfunc)) * sigma)) # convolve data if status == 0: convolved = convolve(padded, filtfunc, 'same') # remove padding from the output array if status == 0: outdata = convolved[len(filtfunc):-len(filtfunc)] # subtract low frequencies if status == 0: outmedian = median(outdata) pixseries[i, :] = pixseries[i, :] - outdata + outmedian # sum pixels over cadence if status == 0: np = 0 nrows = len(fluxpixels) pixsum = zeros((ydim * xdim)) errsum = zeros((ydim * xdim)) for i in range(npts): if quality[i] == 0: pixsum += pixseries[:, i] errsum += errseries[:, i]**2 np += 1 pixsum /= np errsum = sqrt(errsum) / np # calculate standard deviation pixels if status == 0: pixvar = zeros((ydim * xdim)) for i in range(npts): if quality[i] == 0: pixvar += (pixsum - pixseries[:, i] / errseries[:, i])**2 pixvar = numpy.sqrt(pixvar) # median pixel errors if status == 0: errmed = empty((ydim * xdim)) for i in range(ydim * xdim): errmed[i] = numpy.median(errseries[:, i]) # calculate chi distribution pixels if status == 0: pixdev = zeros((ydim * xdim)) for i in range(npts): if quality[i] == 0: pixdev += ((pixsum - pixseries[:, i]) / pixsum)**2 pixdev = numpy.sqrt(pixdev) # pixdev = numpy.sqrt(pixvar) / errsum #errmed # image scale and intensity limits if status == 0: pixsum_pl, zminsum, zmaxsum = kepplot.intScale1D(pixsum, imscale) pixvar_pl, zminvar, zmaxvar = kepplot.intScale1D(pixvar, imscale) pixdev_pl, zmindev, zmaxdev = kepplot.intScale1D(pixdev, imscale) # construct output summed image if status == 0: imgsum = empty((ydim, xdim)) imgvar = empty((ydim, xdim)) imgdev = empty((ydim, xdim)) imgsum_pl = empty((ydim, xdim)) imgvar_pl = empty((ydim, xdim)) imgdev_pl = empty((ydim, xdim)) n = 0 for i in range(ydim): for j in range(xdim): imgsum[i, j] = pixsum[n] imgvar[i, j] = pixvar[n] imgdev[i, j] = pixdev[n] imgsum_pl[i, j] = pixsum_pl[n] imgvar_pl[i, j] = pixvar_pl[n] imgdev_pl[i, j] = pixdev_pl[n] n += 1 # construct output file if status == 0: instruct, status = kepio.openfits(infile, 'readonly', logfile, verbose) status = kepkey.history(call, instruct[0], outfile, logfile, verbose) hdulist = HDUList(instruct[0]) hdulist.writeto(outfile) status = kepkey.new('EXTNAME', 'FLUX', 'name of extension', instruct[2], outfile, logfile, verbose) pyfits.append(outfile, imgsum, instruct[2].header) status = kepkey.new('EXTNAME', 'CHI', 'name of extension', instruct[2], outfile, logfile, verbose) pyfits.append(outfile, imgvar, instruct[2].header) status = kepkey.new('EXTNAME', 'STDDEV', 'name of extension', instruct[2], outfile, logfile, verbose) pyfits.append(outfile, imgdev, instruct[2].header) status = kepkey.new('EXTNAME', 'APERTURE', 'name of extension', instruct[2], outfile, logfile, verbose) pyfits.append(outfile, instruct[2].data, instruct[2].header) status = kepio.closefits(instruct, logfile, verbose) # pixel limits of the subimage if status == 0: ymin = row ymax = ymin + ydim xmin = column xmax = xmin + xdim # plot limits for summed image ymin = float(ymin) - 0.5 ymax = float(ymax) - 0.5 xmin = float(xmin) - 0.5 xmax = float(xmax) - 0.5 # plot style 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': 10, 'ytick.labelsize': 10 } pylab.rcParams.update(params) except: 'ERROR -- KEPDIFFIM: install latex for scientific plotting' status = 1 if status == 0: plotimage(imgsum_pl, imgvar_pl, imgdev_pl, zminsum, zminvar, zmindev, zmaxsum, zmaxvar, zmaxdev, xmin, xmax, ymin, ymax, colmap, plotfile, cmdLine) # stop time kepmsg.clock('KEPDIFFIM ended at: ', logfile, verbose) return
def kepbinary(infile,outfile,datacol,m1,m2,r1,r2,period,bjd0,eccn,omega,inclination, c1,c2,c3,c4,albedo,depth,contamination,gamma,fitparams,eclipses,dopboost, tides,job,clobber,verbose,logfile,status): # startup parameters status = 0 labelsize = 24; ticksize = 16; xsize = 17; ysize = 7 lcolor = '#0000ff'; lwidth = 1.0; fcolor = '#ffff00'; falpha = 0.2 # log the call hashline = '----------------------------------------------------------------------------' kepmsg.log(logfile,hashline,verbose) call = 'KEPBINARY -- ' call += 'infile='+infile+' ' call += 'outfile='+outfile+' ' call += 'datacol='+datacol+' ' call += 'm1='+str(m1)+' ' call += 'm2='+str(m2)+' ' call += 'r1='+str(r1)+' ' call += 'r2='+str(r2)+' ' call += 'period='+str(period)+' ' call += 'bjd0='+str(bjd0)+' ' call += 'eccn='+str(eccn)+' ' call += 'omega='+str(omega)+' ' call += 'inclination='+str(inclination)+' ' call += 'c1='+str(c1)+' ' call += 'c2='+str(c2)+' ' call += 'c3='+str(c3)+' ' call += 'c4='+str(c4)+' ' call += 'albedo='+str(albedo)+' ' call += 'depth='+str(depth)+' ' call += 'contamination='+str(contamination)+' ' call += 'gamma='+str(gamma)+' ' call += 'fitparams='+str(fitparams)+' ' eclp = 'n' if (eclipses): eclp = 'y' call += 'eclipses='+eclp+ ' ' boost = 'n' if (dopboost): boost = 'y' call += 'dopboost='+boost+ ' ' distort = 'n' if (tides): distort = 'y' call += 'tides='+distort+ ' ' call += 'job='+str(job)+ ' ' 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('KEPBINARY started at',logfile,verbose) # test log file logfile = kepmsg.test(logfile) # check and format the list of fit parameters if status == 0 and job == 'fit': allParams = [m1,m2,r1,r2,period,bjd0,eccn,omega,inclination] allNames = ['m1','m2','r1','r2','period','bjd0','eccn','omega','inclination'] fitparams = re.sub('\|',',',fitparams.strip()) fitparams = re.sub('\.',',',fitparams.strip()) fitparams = re.sub(';',',',fitparams.strip()) fitparams = re.sub(':',',',fitparams.strip()) fitparams = re.sub('\s+',',',fitparams.strip()) fitparams, status = kepio.parselist(fitparams,logfile,verbose) for fitparam in fitparams: if fitparam.strip() not in allNames: message = 'ERROR -- KEPBINARY: unknown field in list of fit parameters' status = kepmsg.err(logfile,message,verbose) # clobber output file if status == 0: if clobber: status = kepio.clobber(outfile,logfile,verbose) if kepio.fileexists(outfile): message = 'ERROR -- KEPBINARY: ' + 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 # check the data column exists if status == 0: try: instr[1].data.field(datacol) except: message = 'ERROR -- KEPBINARY: ' + datacol + ' column does not exist in ' + infile + '[1]' status = kepmsg.err(logfile,message,verbose) # 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: try: nanclean = instr[1].header['NANCLEAN'] except: naxis2 = 0 try: for i in range(len(table.field(0))): if numpy.isfinite(table.field('barytime')[i]) and \ numpy.isfinite(table.field(datacol)[i]): table[naxis2] = table[i] naxis2 += 1 instr[1].data = table[:naxis2] except: for i in range(len(table.field(0))): if numpy.isfinite(table.field('time')[i]) and \ numpy.isfinite(table.field(datacol)[i]): table[naxis2] = table[i] naxis2 += 1 instr[1].data = table[:naxis2] comment = 'NaN cadences removed from data' status = kepkey.new('NANCLEAN',True,comment,instr[1],outfile,logfile,verbose) # read table columns if status == 0: try: time = instr[1].data.field('barytime') except: time, status = kepio.readfitscol(infile,instr[1].data,'time',logfile,verbose) indata, status = kepio.readfitscol(infile,instr[1].data,datacol,logfile,verbose) if status == 0: time = time + bjdref indata = indata / cadenom # limb-darkening cofficients if status == 0: limbdark = numpy.array([c1,c2,c3,c4],dtype='float32') # time details for model if status == 0: npt = len(time) exptime = numpy.zeros((npt),dtype='float64') dtype = numpy.zeros((npt),dtype='int') for i in range(npt): try: exptime[i] = time[i+1] - time[i] except: exptime[i] = time[i] - time[i-1] # calculate binary model if status == 0: tmodel = kepsim.transitModel(1.0,m1,m2,r1,r2,period,inclination,bjd0,eccn,omega,depth, albedo,c1,c2,c3,c4,gamma,contamination,npt,time,exptime, dtype,eclipses,dopboost,tides) # re-normalize binary model to data if status == 0 and (job == 'overlay' or job == 'fit'): dmedian = numpy.median(indata) tmodel = tmodel / numpy.median(tmodel) * dmedian # define arrays of floating and frozen parameters if status == 0 and job =='fit': params = []; paramNames = []; arguments = []; argNames = [] for i in range(len(allNames)): if allNames[i] in fitparams: params.append(allParams[i]) paramNames.append(allNames[i]) else: arguments.append(allParams[i]) argNames.append(allNames[i]) params.append(dmedian) params = numpy.array(params,dtype='float32') # subtract model from data if status == 0 and job == 'fit': deltam = numpy.abs(indata - tmodel) # fit statistics if status == 0 and job == 'fit': aveDelta = numpy.sum(deltam) / npt chi2 = math.sqrt(numpy.sum((indata - tmodel) * (indata - tmodel) / (npt - len(params)))) # fit model to data using downhill simplex if status == 0 and job == 'fit': print '' print '%4s %11s %11s' % ('iter', 'delta', 'chi^2') print '----------------------------' print '%4d %.5E %.5E' % (0,aveDelta,chi2) bestFit = scipy.optimize.fmin(fitModel,params,args=(paramNames,dmedian,m1,m2,r1,r2,period,bjd0,eccn, omega,inclination,depth,albedo,c1,c2,c3,c4, gamma,contamination,npt,time,exptime,indata, dtype,eclipses,dopboost,tides),maxiter=1e4) # calculate best fit binary model if status == 0 and job == 'fit': print '' for i in range(len(paramNames)): if 'm1' in paramNames[i].lower(): m1 = bestFit[i] print ' M1 = %.3f Msun' % bestFit[i] elif 'm2' in paramNames[i].lower(): m2 = bestFit[i] print ' M2 = %.3f Msun' % bestFit[i] elif 'r1' in paramNames[i].lower(): r1 = bestFit[i] print ' R1 = %.4f Rsun' % bestFit[i] elif 'r2' in paramNames[i].lower(): r2 = bestFit[i] print ' R2 = %.4f Rsun' % bestFit[i] elif 'period' in paramNames[i].lower(): period = bestFit[i] elif 'bjd0' in paramNames[i].lower(): bjd0 = bestFit[i] print 'BJD0 = %.8f' % bestFit[i] elif 'eccn' in paramNames[i].lower(): eccn = bestFit[i] print ' e = %.3f' % bestFit[i] elif 'omega' in paramNames[i].lower(): omega = bestFit[i] print ' w = %.3f deg' % bestFit[i] elif 'inclination' in paramNames[i].lower(): inclination = bestFit[i] print ' i = %.3f deg' % bestFit[i] flux = bestFit[-1] print '' tmodel = kepsim.transitModel(flux,m1,m2,r1,r2,period,inclination,bjd0,eccn,omega,depth, albedo,c1,c2,c3,c4,gamma,contamination,npt,time,exptime, dtype,eclipses,dopboost,tides) # subtract model from data if status == 0: deltaMod = indata - tmodel # standard deviation of model if status == 0: stdDev = math.sqrt(numpy.sum((indata - tmodel) * (indata - tmodel)) / npt) # clean up x-axis unit if status == 0: time0 = float(int(tstart / 100) * 100.0) ptime = time - time0 xlab = 'BJD $-$ %d' % time0 # clean up y-axis units if status == 0: nrm = len(str(int(indata.max())))-1 pout = indata / 10**nrm pmod = tmodel / 10**nrm pres = deltaMod / stdDev if job == 'fit' or job == 'overlay': try: ylab1 = 'Flux (10$^%d$ e$^-$ s$^{-1}$)' % nrm ylab2 = 'Residual ($\sigma$)' except: ylab1 = 'Flux (10**%d e-/s)' % nrm ylab2 = 'Residual (sigma)' else: ylab1 = 'Normalized Flux' # dynamic range of model plot if status == 0 and job == 'model': xmin = ptime.min() xmax = ptime.max() ymin = tmodel.min() ymax = tmodel.max() # dynamic range of model/data overlay or fit if status == 0 and (job == 'overlay' or job == 'fit'): xmin = ptime.min() xmax = ptime.max() ymin = pout.min() ymax = pout.max() tmin = pmod.min() tmax = pmod.max() ymin = numpy.array([ymin,tmin]).min() ymax = numpy.array([ymax,tmax]).max() rmin = pres.min() rmax = pres.max() # pad the dynamic range if status == 0: xr = (xmax - xmin) / 80 yr = (ymax - ymin) / 40 if job == 'overlay' or job == 'fit': rr = (rmax - rmin) / 40 # set up plot style if status == 0: labelsize = 24; ticksize = 16; xsize = 17; ysize = 7 lcolor = '#0000ff'; lwidth = 1.0; fcolor = '#ffff00'; falpha = 0.2 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) pylab.figure(figsize=[14,10]) pylab.clf() # main plot window ax = pylab.axes([0.05,0.3,0.94,0.68]) pylab.gca().xaxis.set_major_formatter(pylab.ScalarFormatter(useOffset=False)) pylab.gca().yaxis.set_major_formatter(pylab.ScalarFormatter(useOffset=False)) labels = ax.get_yticklabels() setp(labels, 'rotation', 90, fontsize=12) # plot model time series if status == 0 and job == 'model': pylab.plot(ptime,tmodel,color='#0000ff',linestyle='-',linewidth=1.0) ptime = numpy.insert(ptime,[0.0],ptime[0]) ptime = numpy.append(ptime,ptime[-1]) tmodel = numpy.insert(tmodel,[0.0],0.0) tmodel = numpy.append(tmodel,0.0) pylab.fill(ptime,tmodel,fc='#ffff00',linewidth=0.0,alpha=0.2) # plot data time series and best fit if status == 0 and (job == 'overlay' or job == 'fit'): pylab.plot(ptime,pout,color='#0000ff',linestyle='-',linewidth=1.0) ptime = numpy.insert(ptime,[0.0],ptime[0]) ptime = numpy.append(ptime,ptime[-1]) pout = numpy.insert(pout,[0],0.0) pout = numpy.append(pout,0.0) pylab.fill(ptime,pout,fc='#ffff00',linewidth=0.0,alpha=0.2) pylab.plot(ptime[1:-1],pmod,color='r',linestyle='-',linewidth=2.0) # ranges and labels if status == 0: pylab.xlim(xmin-xr,xmax+xr) pylab.ylim(ymin-yr,ymax+yr) pylab.xlabel(xlab, {'color' : 'k'}) pylab.ylabel(ylab1, {'color' : 'k'}) # residual plot window if status == 0 and (job == 'overlay' or job == 'fit'): ax = pylab.axes([0.05,0.07,0.94,0.23]) pylab.gca().xaxis.set_major_formatter(pylab.ScalarFormatter(useOffset=False)) pylab.gca().yaxis.set_major_formatter(pylab.ScalarFormatter(useOffset=False)) labels = ax.get_yticklabels() setp(labels, 'rotation', 90, fontsize=12) # plot residual time series if status == 0 and (job == 'overlay' or job == 'fit'): pylab.plot([ptime[0],ptime[-1]],[0.0,0.0],color='r',linestyle='--',linewidth=1.0) pylab.plot([ptime[0],ptime[-1]],[-1.0,-1.0],color='r',linestyle='--',linewidth=1.0) pylab.plot([ptime[0],ptime[-1]],[1.0,1.0],color='r',linestyle='--',linewidth=1.0) pylab.plot(ptime[1:-1],pres,color='#0000ff',linestyle='-',linewidth=1.0) pres = numpy.insert(pres,[0],rmin) pres = numpy.append(pres,rmin) pylab.fill(ptime,pres,fc='#ffff00',linewidth=0.0,alpha=0.2) # ranges and labels of residual time series if status == 0 and (job == 'overlay' or job == 'fit'): pylab.xlim(xmin-xr,xmax+xr) pylab.ylim(rmin-rr,rmax+rr) pylab.xlabel(xlab, {'color' : 'k'}) pylab.ylabel(ylab2, {'color' : 'k'}) # display the plot if status == 0: pylab.draw()
def kepclip(infile, outfile, ranges, plot, plotcol, clobber, verbose, logfile, status, cmdLine=False): # startup parameters status = 0 labelsize = 32 ticksize = 24 xsize = 18 ysize = 10 lcolor = '#0000ff' lwidth = 1.0 fcolor = '#ffff00' falpha = 0.2 # log the call hashline = '----------------------------------------------------------------------------' kepmsg.log(logfile, hashline, verbose) call = 'KEPCLIP -- ' call += 'infile=' + infile + ' ' call += 'outfile=' + outfile + ' ' call += 'ranges=' + ranges + ' ' plotit = 'n' if (plot): plotit = 'y' call += 'plot=' + plotit + ' ' call += 'plotcol=' + plotcol + ' ' 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) # time ranges for region if status == 0: t1 = [] t2 = [] t1, t2, status = kepio.timeranges(ranges, logfile, 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) 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) # input data if status == 0: table = instr[1].data # read time and flux columns if status == 0: barytime, status = kepio.readtimecol(infile, table, logfile, verbose) if status == 0: flux, status = kepio.readfitscol(infile, table, plotcol, logfile, verbose) if status == 0: barytime = barytime + bjdref if 'flux' in plotcol.lower(): flux = flux / cadenom # filter input data table if status == 0: naxis2 = 0 work1 = array([], 'float64') work2 = array([], 'float32') for i in range(len(barytime)): if (numpy.isfinite(barytime[i]) and numpy.isfinite(flux[i]) and flux[i] != 0.0): reject = False for j in range(len(t1)): if (barytime[i] >= t1[j] and barytime[i] <= t2[j]): reject = True if not reject: table[naxis2] = table[i] work1 = append(work1, barytime[i]) work2 = append(work2, flux[i]) naxis2 += 1 # comment keyword in output file if status == 0: status = kepkey.history(call, instr[0], outfile, logfile, verbose) # write output file if status == 0: instr[1].data = table[:naxis2] comment = 'NaN cadences removed from data' status = kepkey.new('NANCLEAN', True, comment, instr[1], outfile, logfile, verbose) instr.writeto(outfile) # clean up x-axis unit if status == 0: barytime0 = float(int(tstart / 100) * 100.0) barytime = work1 - barytime0 xlab = 'BJD $-$ %d' % barytime0 # clean up y-axis units if status == 0: try: nrm = len(str(int(work2.max()))) - 1 except: nrm = 0 flux = work2 / 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 # plotting arguments if status == 0 and plot: try: params = { 'backend': 'png', 'axes.linewidth': 2.5, 'axes.labelsize': labelsize, 'axes.font': 'sans-serif', 'axes.fontweight': 'bold', 'text.fontsize': 12, 'legend.fontsize': 12, 'xtick.labelsize': ticksize, 'ytick.labelsize': ticksize } rcParams.update(params) except: print('ERROR -- KEPCLIP: install latex for scientific plotting') status = 1 # clear window, plot box if status == 0 and plot: pylab.figure(figsize=[xsize, ysize]) pylab.clf() ax = pylab.axes([0.05, 0.1, 0.94, 0.88]) # 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() setp(labels, 'rotation', 90, fontsize=12) # plot line data ltime = [barytime[0]] ldata = [flux[0]] for i in range(1, len(flux)): if (barytime[i - 1] > barytime[i] - 0.025): ltime.append(barytime[i]) ldata.append(flux[i]) else: ltime = array(ltime, dtype=float64) ldata = array(ldata, dtype=float64) pylab.plot(ltime, ldata, color=lcolor, linestyle='-', linewidth=lwidth) ltime = [] ldata = [] ltime = array(ltime, dtype=float64) ldata = array(ldata, dtype=float64) pylab.plot(ltime, ldata, color=lcolor, linestyle='-', linewidth=lwidth) # plot fill data barytime = insert(barytime, [0], [barytime[0]]) barytime = append(barytime, [barytime[-1]]) flux = insert(flux, [0], [0.0]) flux = append(flux, [0.0]) fill(barytime, flux, fc=fcolor, linewidth=0.0, alpha=falpha) xlim(xmin - xr * 0.01, xmax + xr * 0.01) if ymin - yr * 0.01 <= 0.0: ylim(1.0e-10, ymax + yr * 0.01) else: ylim(ymin - yr * 0.01, ymax + yr * 0.01) xlabel(xlab, {'color': 'k'}) ylabel(ylab, {'color': 'k'}) grid() # render plot if status == 0 and plot: if cmdLine: pylab.show() else: pylab.ion() pylab.plot([]) pylab.ioff() # close input file if status == 0: status = kepio.closefits(instr, logfile, verbose) # end time if (status == 0): message = 'KEPCLIP completed at' else: message = '\nKEPCLIP aborted at' kepmsg.clock(message, logfile, verbose)
def kepfilter(infile,outfile,datacol,function,cutoff,passband,plot,plotlab, clobber,verbose,logfile,status,cmdLine=False): ## startup parameters status = 0 numpy.seterr(all="ignore") labelsize = 24 ticksize = 16 xsize = 16 ysize = 6 lcolor = '#0000ff' lwidth = 1.0 fcolor = '#ffff00' falpha = 0.2 ## log the call hashline = '----------------------------------------------------------------------------' kepmsg.log(logfile,hashline,verbose) call = 'KEPFILTER -- ' call += 'infile='+infile+' ' call += 'outfile='+outfile+' ' call += 'datacol='+str(datacol)+' ' call += 'function='+str(function)+' ' call += 'cutoff='+str(cutoff)+' ' call += 'passband='+str(passband)+' ' plotit = 'n' if (plot): plotit = 'y' call += 'plot='+plotit+ ' ' call += 'plotlab='+str(plotlab)+' ' 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('KEPFILTER 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 -- KEPFILTER: ' + 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) 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) # read time and flux columns if status == 0: barytime, status = kepio.readtimecol(infile,table,logfile,verbose) flux, status = kepio.readsapcol(infile,table,logfile,verbose) # filter input data table if status == 0: try: nanclean = instr[1].header['NANCLEAN'] except: naxis2 = 0 for i in range(len(table.field(0))): if (numpy.isfinite(barytime[i]) and numpy.isfinite(flux[i]) and flux[i] != 0.0): table[naxis2] = table[i] naxis2 += 1 instr[1].data = table[:naxis2] comment = 'NaN cadences removed from data' status = kepkey.new('NANCLEAN',True,comment,instr[1],outfile,logfile,verbose) ## read table columns if status == 0: intime, status = kepio.readtimecol(infile,instr[1].data,logfile,verbose) if status == 0: indata, status = kepio.readfitscol(infile,instr[1].data,datacol,logfile,verbose) if status == 0: intime = intime + bjdref indata = indata / cadenom ## define data sampling if status == 0: tr = 1.0 / (cadence / 86400) timescale = 1.0 / (cutoff / tr) ## define convolution function if status == 0: if function == 'boxcar': filtfunc = numpy.ones(numpy.ceil(timescale)) elif function == 'gauss': timescale /= 2 dx = numpy.ceil(timescale * 10 + 1) filtfunc = kepfunc.gauss() filtfunc = filtfunc([1.0,dx/2-1.0,timescale],linspace(0,dx-1,dx)) elif function == 'sinc': dx = numpy.ceil(timescale * 12 + 1) fx = linspace(0,dx-1,dx) fx = fx - dx / 2 + 0.5 fx /= timescale filtfunc = numpy.sinc(fx) filtfunc /= numpy.sum(filtfunc) ## pad time series at both ends with noise model if status == 0: ave, sigma = kepstat.stdev(indata[:len(filtfunc)]) padded = append(kepstat.randarray(np.ones(len(filtfunc)) * ave, np.ones(len(filtfunc)) * sigma), indata) ave, sigma = kepstat.stdev(indata[-len(filtfunc):]) padded = append(padded, kepstat.randarray(np.ones(len(filtfunc)) * ave, np.ones(len(filtfunc)) * sigma)) ## convolve data if status == 0: convolved = convolve(padded,filtfunc,'same') ## remove padding from the output array if status == 0: if function == 'boxcar': outdata = convolved[len(filtfunc):-len(filtfunc)] else: outdata = convolved[len(filtfunc):-len(filtfunc)] ## subtract low frequencies if status == 0 and passband == 'high': outmedian = median(outdata) outdata = indata - outdata + outmedian ## comment keyword in output file if status == 0: status = kepkey.history(call,instr[0],outfile,logfile,verbose) ## clean up x-axis unit if status == 0: intime0 = float(int(tstart / 100) * 100.0) if intime0 < 2.4e6: intime0 += 2.4e6 ptime = intime - intime0 xlab = 'BJD $-$ %d' % intime0 ## clean up y-axis units if status == 0: pout = indata * 1.0 pout2 = outdata * 1.0 nrm = len(str(int(numpy.nanmax(pout))))-1 pout = pout / 10**nrm pout2 = pout2 / 10**nrm ylab = '10$^%d$ %s' % (nrm, plotlab) ## data limits xmin = ptime.min() xmax = ptime.max() ymin = numpy.nanmin(pout) ymax = numpy.nanmax(pout) 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) pout2 = insert(pout2,[0],[0.0]) pout2 = append(pout2,0.0) ## plot light curve if status == 0 and plot: try: params = {'backend': 'png', 'axes.linewidth': 2.5, 'axes.labelsize': labelsize, 'axes.font': 'sans-serif', 'axes.fontweight' : 'bold', 'text.fontsize': 12, 'legend.fontsize': 12, 'xtick.labelsize': ticksize, 'ytick.labelsize': ticksize} rcParams.update(params) except: print('ERROR -- KEPFILTER: install latex for scientific plotting') status = 1 if status == 0 and plot: pylab.figure(figsize=[xsize,ysize]) pylab.clf() ## plot filtered data ax = pylab.axes([0.06,0.1,0.93,0.87]) pylab.gca().xaxis.set_major_formatter(pylab.ScalarFormatter(useOffset=False)) pylab.gca().yaxis.set_major_formatter(pylab.ScalarFormatter(useOffset=False)) labels = ax.get_yticklabels() setp(labels, 'rotation', 90, fontsize=12) pylab.plot(ptime,pout,color='#ff9900',linestyle='-',linewidth=lwidth) fill(ptime,pout,color=fcolor,linewidth=0.0,alpha=falpha) if passband == 'low': pylab.plot(ptime[1:-1],pout2[1:-1],color=lcolor,linestyle='-',linewidth=lwidth) else: pylab.plot(ptime,pout2,color=lcolor,linestyle='-',linewidth=lwidth) fill(ptime,pout2,color=lcolor,linewidth=0.0,alpha=falpha) xlabel(xlab, {'color' : 'k'}) ylabel(ylab, {'color' : 'k'}) xlim(xmin-xr*0.01,xmax+xr*0.01) if ymin >= 0.0: ylim(ymin-yr*0.01,ymax+yr*0.01) else: ylim(1.0e-10,ymax+yr*0.01) pylab.grid() # render plot if cmdLine: pylab.show() else: pylab.ion() pylab.plot([]) pylab.ioff() ## write output file if status == 0: for i in range(len(outdata)): instr[1].data.field(datacol)[i] = outdata[i] instr.writeto(outfile) ## close input file if status == 0: status = kepio.closefits(instr,logfile,verbose) ## end time if (status == 0): message = 'KEPFILTER completed at' else: message = '\nKEPFILTER aborted at' kepmsg.clock(message,logfile,verbose)