def tabappend(hdu1, hdu2, logfile, verbose): status = 0 nrows1 = hdu1.data.shape[0] nrows2 = hdu2.data.shape[0] nrows = nrows1 + nrows2 out = pyfits.new_table(hdu1.columns, nrows=nrows) for name in hdu1.columns.names: try: out.data.field(name)[nrows1:] = hdu2.data.field(name) except: message = 'WARNING -- KEPIO.TABAPPEND: could not append column ' message += str(name) status = kepmsg.warn(logfile, message, verbose) return out, status
def tabappend(hdu1,hdu2,logfile,verbose): status = 0 nrows1 = hdu1.data.shape[0] nrows2 = hdu2.data.shape[0] nrows = nrows1 + nrows2 out = pyfits.new_table(hdu1.columns,nrows=nrows) for name in hdu1.columns.names: try: out.data.field(name)[nrows1:] = hdu2.data.field(name) except: message = 'WARNING -- KEPIO.TABAPPEND: could not append column ' message += str(name) status = kepmsg.warn(logfile,message,verbose) return out, status
def kepimages(infile,outfix,imtype,ranges,clobber,verbose,logfile,status): # startup parameters status = 0 # log the call hashline = '----------------------------------------------------------------------------' kepmsg.log(logfile,hashline,verbose) call = 'KEPIMAGES -- ' call += 'infile='+infile+' ' call += 'outfix='+outfix+' ' call += 'imtype='+imtype+' ' call += 'ranges='+str(ranges)+' ' 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('KEPIMAGES started at',logfile,verbose) # test log file logfile = kepmsg.test(logfile) # open input file status = 0 print ' ' instr = pyfits.open(infile,mode='readonly',memmap=True) cards0 = instr[0].header.cards cards1 = instr[1].header.cards cards2 = instr[2].header.cards # fudge non-compliant FITS keywords with no values if status == 0: instr = kepkey.emptykeys(instr,file,logfile,verbose) # ingest time series data if status == 0: time = instr[1].data.field('TIME')[:] + 2454833.0 timecorr = instr[1].data.field('TIMECORR')[:] cadenceno = instr[1].data.field('CADENCENO')[:] raw_cnts = instr[1].data.field('RAW_CNTS')[:] flux = instr[1].data.field('FLUX')[:] flux_err = instr[1].data.field('FLUX_ERR')[:] flux_bkg = instr[1].data.field('FLUX_BKG')[:] flux_bkg_err = instr[1].data.field('FLUX_BKG_ERR')[:] cosmic_rays = instr[1].data.field('COSMIC_RAYS')[:] quality = instr[1].data.field('QUALITY')[:] pos_corr1 = instr[1].data.field('POS_CORR1')[:] pos_corr2 = instr[1].data.field('POS_CORR2')[:] # choose output image if status == 0: if imtype.lower() == 'raw_cnts': outim = raw_cnts elif imtype.lower() == 'flux_err': outim = flux_err elif imtype.lower() == 'flux_bkg': outim = flux_bkg elif imtype.lower() == 'flux_bkg_err': outim = flux_bkg_err elif imtype.lower() == 'cosmic_rays': outim = cosmic_rays else: outim = flux # identify images to be exported if status == 0: tim = array([]); dat = array([]); err = array([]) tstart, tstop, status = kepio.timeranges(ranges,logfile,verbose) if status == 0: cadencelis, status = kepstat.filterOnRange(time,tstart,tstop) # provide name for each output file and clobber if file exists if status == 0: for cadence in cadencelis: outfile = outfix + '_BJD%.4f' % time[cadence] + '.fits' if clobber and status == 0: status = kepio.clobber(outfile,logfile,verbose) if kepio.fileexists(outfile) and status == 0: message = 'ERROR -- KEPIMAGES: ' + outfile + ' exists. Use --clobber' status = kepmsg.err(logfile,message,True) # construct output primary extension if status == 0: ncad = 0 for cadence in cadencelis: outfile = outfix + '_BJD%.4f' % time[cadence] + '.fits' hdu0 = pyfits.PrimaryHDU() for i in range(len(cards0)): try: if cards0[i].key not in hdu0.header.keys(): hdu0.header.update(cards0[i].key, cards0[i].value, cards0[i].comment) else: hdu0.header.cards[cards0[i].key].comment = cards0[i].comment except: pass status = kepkey.history(call,hdu0,outfile,logfile,verbose) outstr = HDUList(hdu0) # construct output image extension hdu1 = ImageHDU(flux[cadence]) for i in range(len(cards2)): try: if cards2[i].key not in hdu1.header.keys(): hdu1.header.update(cards2[i].key, cards2[i].value, cards2[i].comment) except: pass for i in range(len(cards1)): if (cards1[i].key not in hdu1.header.keys() and cards1[i].key[:4] not in ['TTYP','TFOR','TUNI','TDIS','TDIM','WCAX','1CTY', '2CTY','1CRP','2CRP','1CRV','2CRV','1CUN','2CUN', '1CDE','2CDE','1CTY','2CTY','1CDL','2CDL','11PC', '12PC','21PC','22PC','WCSN','TFIE']): hdu1.header.update(cards1[i].key, cards1[i].value, cards1[i].comment) try: int_time = cards1['INT_TIME'].value except: kepmsg.warn(logfile,'WARNING -- KEPIMAGES: cannot find INT_TIME keyword') try: frametim = cards1['FRAMETIM'].value except: kepmsg.warn(logfile,'WARNING -- KEPIMAGES: cannot find FRAMETIM keyword') try: num_frm = cards1['NUM_FRM'].value except: kepmsg.warn(logfile,'WARNING -- KEPIMAGES: cannot find NUM_FRM keyword') hdu1.header.update('EXTNAME','IMAGE','name of extension') try: hdu1.header.update('TELAPSE',frametim * num_frm,'[s] elapsed time for exposure') except: hdu1.header.update('TELAPSE',-999,'[s] elapsed time for exposure') try: hdu1.header.update('LIVETIME',int_time * num_frm,'[s] TELASPE multiplied by DEADC') except: hdu1.header.update('LIVETIME',-999,'[s] TELASPE multiplied by DEADC') try: hdu1.header.update('EXPOSURE',int_time * num_frm,'[s] time on source') except: hdu1.header.update('EXPOSURE',-999,'[s] time on source') try: hdu1.header.update('MIDTIME',time[cadence],'[BJD] mid-time of exposure') except: hdu1.header.update('MIDTIME',-999,'[BJD] mid-time of exposure') try: hdu1.header.update('TIMECORR',timecorr[cadence],'[d] barycenter - timeslice correction') except: hdu1.header.update('TIMECORR',-999,'[d] barycenter - timeslice correction') try: hdu1.header.update('CADENCEN',cadenceno[cadence],'unique cadence number') except: hdu1.header.update('CADENCEN',-999,'unique cadence number') try: hdu1.header.update('QUALITY',quality[cadence],'pixel quality flag') except: hdu1.header.update('QUALITY',-999,'pixel quality flag') try: if True in numpy.isfinite(cosmic_rays[cadence]): hdu1.header.update('COSM_RAY',True,'cosmic ray detected?') else: hdu1.header.update('COSM_RAY',False,'cosmic ray detected?') except: hdu1.header.update('COSM_RAY',-999,'cosmic ray detected?') try: pc1 = str(pos_corr1[cadence]) pc2 = str(pos_corr2[cadence]) hdu1.header.update('POSCORR1',pc1,'[pix] column position correction') hdu1.header.update('POSCORR2',pc2,'[pix] row position correction') except: hdu1.header.update('POSCORR1',-999,'[pix] column position correction') hdu1.header.update('POSCORR2',-999,'[pix] row position correction') outstr.append(hdu1) # write output file if status == 0: outstr.writeto(outfile,checksum=True) ncad += 1 txt = '\r%3d%% ' % (float(ncad) / float(len(cadencelis)) * 100.0) txt += '%s ' % outfile sys.stdout.write(txt) sys.stdout.flush() # close input structure if status == 0: status = kepio.closefits(instr,logfile,verbose) print '\n' # end time kepmsg.clock('KEPIMAGES finished at',logfile,verbose)
def MASTRADec(ra,dec,darcsec,srctab): # coordinate limits darcsec /= 3600.0 ra1 = ra - darcsec / cos(dec * pi / 180) ra2 = ra + darcsec / cos(dec * pi / 180) dec1 = dec - darcsec dec2 = dec + darcsec # build mast query url = 'http://archive.stsci.edu/kepler/kepler_fov/search.php?' url += 'action=Search' url += '&masterRA=' + str(ra1) + '..' + str(ra2) url += '&masterDec=' + str(dec1) + '..' + str(dec2) url += '&max_records=10000' url += '&verb=3' url += '&outputformat=CSV' # retrieve results from MAST if srctab: try: lines = urllib.request.urlopen(url) except: message = 'WARNING -- KEPFIELD: Cannot retrieve data from MAST' status = kepmsg.warn(logfile,message) lines = '' else: lines = '' # collate nearby sources kepid = [] kepmag = [] ra = [] dec = [] for line in lines: line = line.strip() # Python 3 hack try: 'Kepler' in line except: line = line.decode('ascii') if (len(line) > 0 and 'Kepler' not in line and 'integer' not in line and 'no rows found' not in line): out = line.split(',') r,d = sex2dec(out[0],out[1]) try: if out[-22] != 'Possible_artifact': kepid.append(int(out[2])) except: if out[-22] != 'Possible_artifact': kepid.append(0) try: if out[-22] != 'Possible_artifact': kepmag.append(float(out[42])) except: if out[-22] != 'Possible_artifact': kepmag.append(0.0) if out[-22] != 'Possible_artifact': ra.append(r) if out[-22] != 'Possible_artifact': dec.append(d) kepid = array(kepid) kepmag = array(kepmag) ra = array(ra) dec = array(dec) return kepid,ra,dec,kepmag
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 kepflatten(infile,outfile,datacol,errcol,nsig,stepsize,winsize,npoly,niter,ranges, plot,clobber,verbose,logfile,status,cmdLine=False): # startup parameters status = 0 labelsize = 32 ticksize = 18 xsize = 16 ysize = 10 lcolor = '#0000ff' lwidth = 1.0 fcolor = '#ffff00' falpha = 0.2 # log the call hashline = '----------------------------------------------------------------------------' kepmsg.log(logfile,hashline,verbose) call = 'KEPFLATTEN -- ' call += 'infile='+infile+' ' call += 'outfile='+outfile+' ' call += 'datacol='+str(datacol)+' ' call += 'errcol='+str(errcol)+' ' call += 'nsig='+str(nsig)+' ' call += 'stepsize='+str(stepsize)+' ' call += 'winsize='+str(winsize)+' ' call += 'npoly='+str(npoly)+' ' call += 'niter='+str(niter)+' ' call += 'ranges='+str(ranges)+' ' plotit = 'n' if (plot): plotit = 'y' call += 'plot='+plotit+ ' ' overwrite = 'n' if (clobber): overwrite = 'y' call += 'clobber='+overwrite+ ' ' chatter = 'n' if (verbose): chatter = 'y' call += 'verbose='+chatter+' ' call += 'logfile='+logfile kepmsg.log(logfile,call+'\n',verbose) # start time kepmsg.clock('KEPFLATTEN started at',logfile,verbose) # test log file logfile = kepmsg.test(logfile) # test winsize > stepsize if winsize < stepsize: message = 'ERROR -- KEPFLATTEN: winsize must be greater than stepsize' status = kepmsg.err(logfile,message,verbose) # clobber output file if clobber: status = kepio.clobber(outfile,logfile,verbose) if kepio.fileexists(outfile): message = 'ERROR -- KEPFLATTEN: ' + 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: datac = table.field(datacol) except: message = 'ERROR -- KEPFLATTEN: cannot find or read data column ' + datacol status = kepmsg.err(logfile,message,verbose) if status == 0: try: err = table.field(errcol) except: message = 'WARNING -- KEPFLATTEN: cannot find or read error column ' + errcol errcol = 'None' if status == 0: if errcol.lower() == 'none' or errcol == 'PSF_FLUX_ERR': err = datac * cadence err = numpy.sqrt(numpy.abs(err)) / cadence work1 = numpy.array([table.field('time'), datac, err]) else: work1 = numpy.array([table.field('time'), datac, err]) work1 = numpy.rot90(work1,3) work1 = work1[~numpy.isnan(work1).any(1)] # read table columns if status == 0: intime = work1[:,2] + bjdref indata = work1[:,1] inerr = work1[:,0] if len(intime) == 0: message = 'ERROR -- KEPFLATTEN: one of the input arrays is all NaN' status = kepmsg.err(logfile,message,verbose) # 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+winsize,intime[-1]],dtype='float64').min()) work += stepsize # find cadence limits of each time step if status == 0: cstep1 = []; cstep2 = [] for n in range(len(tstep1)): for i in range(len(intime)-1): if intime[i] <= tstep1[n] and intime[i+1] > tstep1[n]: for j in range(i,len(intime)-1): if intime[j] < tstep2[n] and intime[j+1] >= tstep2[n]: cstep1.append(i) cstep2.append(j+1) # 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 = copy(indata) 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.54,0.93,0.43]) # force tick labels to be absolute rather than relative pylab.gca().xaxis.set_major_formatter(pylab.ScalarFormatter(useOffset=False)) pylab.gca().yaxis.set_major_formatter(pylab.ScalarFormatter(useOffset=False)) # rotate y labels by 90 deg labels = ax.get_yticklabels() pylab.setp(labels, 'rotation', 90) pylab.setp(pylab.gca(),xticklabels=[]) pylab.plot(ptime[1:-1],pout[1:-1],color=lcolor,linestyle='-',linewidth=lwidth) pylab.fill(ptime,pout,color=fcolor,linewidth=0.0,alpha=falpha) 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: fitarray = numpy.zeros((len(indata),len(cstep1)),dtype='float32') sigarray = numpy.zeros((len(indata),len(cstep1)),dtype='float32') fitarray[:,:] = numpy.nan sigarray[:,:] = numpy.nan masterfit = indata * 0.0 mastersigma = numpy.zeros(len(masterfit)) functype = 'poly' + str(npoly) for i in range(len(cstep1)): timeSeries = intime[cstep1[i]:cstep2[i]+1]-intime[cstep1[i]] dataSeries = indata[cstep1[i]:cstep2[i]+1] fitTimeSeries = numpy.array([],dtype='float32') fitDataSeries = numpy.array([],dtype='float32') pinit = [dataSeries.mean()] if npoly > 0: for j in range(npoly): pinit.append(0.0) pinit = array(pinit,dtype='float32') try: if len(fitarray[cstep1[i]:cstep2[i]+1,i]) > len(pinit): coeffs, errors, covar, iiter, sigma, chi2, dof, fit, plotx, ploty, status = \ kepfit.lsqclip(functype,pinit,timeSeries,dataSeries,None,nsig,nsig,niter, logfile,verbose) fitarray[cstep1[i]:cstep2[i]+1,i] = 0.0 sigarray[cstep1[i]:cstep2[i]+1,i] = sigma for j in range(len(coeffs)): fitarray[cstep1[i]:cstep2[i]+1,i] += coeffs[j] * timeSeries**j except: for j in range(cstep1[i],cstep2[i]+1): fitarray[cstep1[i]:cstep2[i]+1,i] = 0.0 sigarray[cstep1[i]:cstep2[i]+1,i] = 1.0e-10 message = 'WARNING -- KEPFLATTEN: could not fit range ' message += str(intime[cstep1[i]]) + '-' + str(intime[cstep2[i]]) kepmsg.warn(None,message) # find mean fit for each timestamp if status == 0: for i in range(len(indata)): masterfit[i] = scipy.stats.nanmean(fitarray[i,:]) mastersigma[i] = scipy.stats.nanmean(sigarray[i,:]) masterfit[-1] = masterfit[-4] #fudge masterfit[-2] = masterfit[-4] #fudge masterfit[-3] = masterfit[-4] #fudge pylab.plot(intime-intime0, masterfit / 10**nrm,'g',lw='3') # 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]) rejtime = array(rejtime,dtype='float64') rejdata = array(rejdata,dtype='float32') if plot: pylab.plot(rejtime-intime0,rejdata / 10**nrm,'ro') # new data for output file if status == 0: outdata = indata / masterfit outerr = inerr / masterfit # plot ranges if status == 0 and plot: pylab.xlim(xmin-xr*0.01,xmax+xr*0.01) if ymin >= 0.0: pylab.ylim(ymin-yr*0.01,ymax+yr*0.01) else: pylab.ylim(1.0e-10,ymax+yr*0.01) # plot residual data if status == 0 and plot: ax = pylab.axes([0.06,0.09,0.93,0.43]) # force tick labels to be absolute rather than relative if status == 0 and plot: 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) # clean up y-axis units if status == 0: pout = copy(outdata) ylab = 'Normalized Flux' # data limits if status == 0 and plot: ymin = pout.min() ymax = pout.max() yr = ymax - ymin pout = insert(pout,[0],[0.0]) pout = append(pout,0.0) pylab.plot(ptime[1:-1],pout[1:-1],color=lcolor,linestyle='-',linewidth=lwidth) pylab.fill(ptime,pout,color=fcolor,linewidth=0.0,alpha=falpha) pylab.xlabel(xlab, {'color' : 'k'}) pylab.ylabel(ylab, {'color' : 'k'}) pylab.grid() # plot ranges if status == 0 and plot: pylab.xlim(xmin-xr*0.01,xmax+xr*0.01) if ymin >= 0.0: pylab.ylim(ymin-yr*0.01,ymax+yr*0.01) else: pylab.ylim(1.0e-10,ymax+yr*0.01) # render plot if status == 0 and plot: pylab.savefig(re.sub('.fits','.png',outfile)) 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') work2 = array([],dtype='float32') instr, status = kepio.openfits(infile,'readonly',logfile,verbose) table, status = kepio.readfitstab(infile,instr[1],logfile,verbose) tn = table.field('time') dn = table.field(datacol) for i in range(len(table.field(0))): if numpy.isfinite(tn[i]) and numpy.isfinite(dn[i]) and numpy.isfinite(err[i]): try: work1 = numpy.append(work1,outdata[n]) work2 = numpy.append(work2,outerr[n]) n += 1 except: pass else: work1 = numpy.append(work1,numpy.nan) work2 = numpy.append(work2,numpy.nan) # history keyword in output file if status == 0: status = kepkey.history(call,instr[0],outfile,logfile,verbose) # write output file try: col1 = pyfits.Column(name='DETSAP_FLUX',format='E13.7',array=work1) col2 = pyfits.Column(name='DETSAP_FLUX_ERR',format='E13.7',array=work2) cols = instr[1].data.columns + col1 + col2 instr[1] = pyfits.new_table(cols,header=instr[1].header) instr.writeto(outfile) except ValueError: try: instr[1].data.field('DETSAP_FLUX')[:] = work1 instr[1].data.field('DETSAP_FLUX_ERR')[:] = work2 instr.writeto(outfile) except: message = 'ERROR -- KEPFLATTEN: cannot add DETSAP_FLUX data to FITS file' status = kepmsg.err(logfile,message,verbose) # close input file if status == 0: status = kepio.closefits(instr,logfile,verbose) ## end time if (status == 0): message = 'KEPFLATTEN completed at' else: message = '\nKEPFLATTEN aborted at' kepmsg.clock(message,logfile,verbose)
def getWCSs(file,struct,logfile,verbose): status = 0 crpix1 = 0.0 crpix2 = 0.0 crval1 = 0.0 crval2 = 0.0 cdelt1 = 0.0 cdelt2 = 0.0 pc = [0.0,0.0,0.0,0.0] try: crpix1, status = get(file,struct,'CRPIX1',logfile,verbose) except: txt = 'WARNING -- KEPKEY.GETWCSS: Cannot read keyword CRPIX1 in file ' + file kepmsg.warn(logfile,txt) status = 1 try: crpix2, status = get(file,struct,'CRPIX2',logfile,verbose) except: txt = 'WARNING -- KEPKEY.GETWCSS: Cannot read keyword CRPIX2 in file ' + file kepmsg.warn(logfile,txt) status = 1 try: crval1, status = get(file,struct,'CRVAL1',logfile,verbose) except: txt = 'WARNING -- KEPKEY.GETWCSS: Cannot read keyword CRVAL1 in file ' + file kepmsg.warn(logfile,txt) status = 1 try: crval2, status = get(file,struct,'CRVAL2',logfile,verbose) except: txt = 'WARNING -- KEPKEY.GETWCSS: Cannot read keyword CRVAL2 in file ' + file kepmsg.warn(logfile,txt) status = 1 try: cdelt1, status = get(file,struct,'CDELT1',logfile,verbose) except: txt = 'WARNING -- KEPKEY.GETWCSS: Cannot read keyword CDELT1 in file ' + file kepmsg.warn(logfile,txt) status = 1 try: cdelt2, status = get(file,struct,'CDELT2',logfile,verbose) except: txt = 'WARNING -- KEPKEY.GETWCSS: Cannot read keyword CDELT2 in file ' + file kepmsg.warn(logfile,txt) status = 1 try: pc1_1, status = get(file,struct,'PC1_1',logfile,verbose) except: txt = 'WARNING -- KEPKEY.GETWCSS: Cannot read keyword PC1_1 in file ' + file kepmsg.warn(logfile,txt) status = 1 try: pc1_2, status = get(file,struct,'PC1_2',logfile,verbose) except: txt = 'WARNING -- KEPKEY.GETWCSS: Cannot read keyword PC1_2 in file ' + file kepmsg.warn(logfile,txt) status = 1 try: pc2_1, status = get(file,struct,'PC2_1',logfile,verbose) except: txt = 'WARNING -- KEPKEY.GETWCSS: Cannot read keyword PC2_1 in file ' + file kepmsg.warn(logfile,txt) status = 1 try: pc2_2, status = get(file,struct,'PC2_2',logfile,verbose) except: txt = 'WARNING -- KEPKEY.GETWCSS: Cannot read keyword PC2_2 in file ' + file kepmsg.warn(logfile,txt) status = 1 try: pc = numpy.array([[pc1_1,pc1_2],[pc2_1,pc2_2]]) pc = numpy.linalg.inv(pc) except: pass return crpix1, crpix2, crval1, crval2, cdelt1, cdelt2, pc, status
def kepstitch(infiles,outfile,clobber,verbose,logfile,status): # startup parameters status = 0 lct = []; bjd = [] # log the call hashline = '----------------------------------------------------------------------------' kepmsg.log(logfile,hashline,verbose) call = 'KEPSTITCH -- ' call += 'infiles='+infiles+' ' call += 'outfile='+outfile+' ' 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('KEPSTITCH started at',logfile,verbose) # test log file logfile = kepmsg.test(logfile) # parse input file list infiles, status = kepio.parselist(infiles,logfile,verbose) # clobber output file if clobber: status = kepio.clobber(outfile,logfile,verbose) if kepio.fileexists(outfile): message = 'ERROR -- KEPSTITCH: ' + outfile + ' exists. Use clobber=yes' kepmsg.err(logfile,message,verbose) status = 1 # open output file if status == 0: outstr, status = kepio.openfits(infiles[0],'readonly',logfile,verbose) nrows1 = outstr[1].data.shape[0] # fudge non-compliant FITS keywords with no values if status == 0: outstr = kepkey.emptykeys(outstr,file,logfile,verbose) head0 = outstr[0].header head1 = outstr[1].header # open input files nfiles = 0 if status == 0: for infile in infiles: instr, status = kepio.openfits(infile,'readonly',logfile,verbose) # append table data if nfiles > 0: nrows2 = instr[1].data.shape[0] nrows = nrows1 + nrows2 outtab = pyfits.new_table(outstr[1].columns,nrows=nrows) for name in outstr[1].columns.names: try: outtab.data.field(name)[nrows1:]=instr[1].data.field(name) except: message = 'ERROR -- KEPSTITCH: column ' + name + ' missing from some files.' kepmsg.warn(logfile,message) pass outstr[1] = outtab outstr[0].header = head0 outstr[1].header = head1 nrows1 = nrows # start and stop times of data fitsvers = 1.0 lc_start, status = kepkey.get(infile,instr[1],'LC_START',logfile,verbose) lc_end, status = kepkey.get(infile,instr[1],'LC_END',logfile,verbose) try: startbjd = instr[1].header['STARTBJD'] except: startbjd, status = kepkey.get(infile,instr[1],'TSTART',logfile,verbose) fitsvers = 2.0 try: endbjd = instr[1].header['ENDBJD'] except: endbjd, status = kepkey.get(infile,instr[1],'TSTOP',logfile,verbose) fitsvers = 2.0 lct.append(lc_start); lct.append(lc_end) bjd.append(startbjd); bjd.append(endbjd) # close input files status = kepio.closefits(instr,logfile,verbose) nfiles += 1 # maxmimum and minimum times in file sample if status == 0: lc_start = kepstat.min(lct) lc_end = kepstat.max(lct) startbjd = kepstat.min(bjd) endbjd = kepstat.max(bjd) status = kepkey.change('LC_START',lc_start,outstr[1],outfile,logfile,verbose) status = kepkey.change('LC_END',lc_end,outstr[1],outfile,logfile,verbose) if fitsvers == 1.0: status = kepkey.change('STARTBJD',startbjd,outstr[1],outfile,logfile,verbose) status = kepkey.change('ENDBJD',endbjd,outstr[1],outfile,logfile,verbose) else: status = kepkey.change('TSTART',startbjd,outstr[1],outfile,logfile,verbose) status = kepkey.change('TSTOP',endbjd,outstr[1],outfile,logfile,verbose) # comment keyword in output file if status == 0: status = kepkey.comment(call,outstr[0],outfile,logfile,verbose) # close output file if status == 0: outstr.writeto(outfile) status = kepio.closefits(outstr,logfile,verbose) ## end time if (status == 0): message = 'KEPSTITCH completed at' else: message = '\nKEPSTITCH aborted at' kepmsg.clock(message,logfile,verbose)
def kepbls(infile,outfile,datacol,errcol,minper,maxper,mindur,maxdur,nsearch, nbins,plot,clobber,verbose,logfile,status,cmdLine=False): # startup parameters numpy.seterr(all="ignore") status = 0 labelsize = 32 ticksize = 18 xsize = 16 ysize = 8 lcolor = '#0000ff' lwidth = 1.0 fcolor = '#ffff00' falpha = 0.2 # log the call hashline = '----------------------------------------------------------------------------' kepmsg.log(logfile,hashline,verbose) call = 'KEPBLS -- ' call += 'infile='+infile+' ' call += 'outfile='+outfile+' ' call += 'datacol='+str(datacol)+' ' call += 'errcol='+str(errcol)+' ' call += 'minper='+str(minper)+' ' call += 'maxper='+str(maxper)+' ' call += 'mindur='+str(mindur)+' ' call += 'maxdur='+str(maxdur)+' ' call += 'nsearch='+str(nsearch)+' ' call += 'nbins='+str(nbins)+' ' plotit = 'n' if (plot): plotit = 'y' call += 'plot='+plotit+ ' ' overwrite = 'n' if (clobber): overwrite = 'y' call += 'clobber='+overwrite+ ' ' chatter = 'n' if (verbose): chatter = 'y' call += 'verbose='+chatter+' ' call += 'logfile='+logfile kepmsg.log(logfile,call+'\n',verbose) # start time kepmsg.clock('KEPBLS started at',logfile,verbose) # is duration greater than one bin in the phased light curve? if float(nbins) * maxdur / 24.0 / maxper <= 1.0: message = 'WARNING -- KEPBLS: ' + str(maxdur) + ' hours transit duration < 1 phase bin when P = ' message += str(maxper) + ' days' kepmsg.warn(logfile,message) # test log file logfile = kepmsg.test(logfile) # clobber output file if clobber: status = kepio.clobber(outfile,logfile,verbose) if kepio.fileexists(outfile): message = 'ERROR -- KEPBLS: ' + 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) # 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), table.field(errcol)]) work1 = numpy.rot90(work1,3) work1 = work1[~numpy.isnan(work1).any(1)] # read table columns if status == 0: intime = work1[:,2] + bjdref indata = work1[:,1] inerr = work1[:,0] # test whether the period range is sensible if status == 0: tr = intime[-1] - intime[0] if maxper > tr: message = 'ERROR -- KEPBLS: maxper is larger than the time range of the input data' status = kepmsg.err(logfile,message,verbose) # prepare time series if status == 0: work1 = intime - intime[0] work2 = indata - numpy.mean(indata) # start period search if status == 0: srMax = numpy.array([],dtype='float32') transitDuration = numpy.array([],dtype='float32') transitPhase = numpy.array([],dtype='float32') dPeriod = (maxper - minper) / nsearch trialPeriods = numpy.arange(minper,maxper+dPeriod,dPeriod,dtype='float32') complete = 0 print ' ' for trialPeriod in trialPeriods: fracComplete = float(complete) / float(len(trialPeriods) - 1) * 100.0 txt = '\r' txt += 'Trial period = ' txt += str(int(trialPeriod)) txt += ' days [' txt += str(int(fracComplete)) txt += '% complete]' txt += ' ' * 20 sys.stdout.write(txt) sys.stdout.flush() complete += 1 srMax = numpy.append(srMax,0.0) transitDuration = numpy.append(transitDuration,numpy.nan) transitPhase = numpy.append(transitPhase,numpy.nan) trialFrequency = 1.0 / trialPeriod # minimum and maximum transit durations in quantized phase units duration1 = max(int(float(nbins) * mindur / 24.0 / trialPeriod),2) duration2 = max(int(float(nbins) * maxdur / 24.0 / trialPeriod) + 1,duration1 + 1) # 30 minutes in quantized phase units halfHour = int(0.02083333 / trialPeriod * nbins + 1) # compute folded time series with trial period work4 = numpy.zeros((nbins),dtype='float32') work5 = numpy.zeros((nbins),dtype='float32') phase = numpy.array(((work1 * trialFrequency) - numpy.floor(work1 * trialFrequency)) * float(nbins),dtype='int') ptuple = numpy.array([phase, work2, inerr]) ptuple = numpy.rot90(ptuple,3) phsort = numpy.array(sorted(ptuple,key=lambda ph: ph[2])) for i in range(nbins): elements = numpy.nonzero(phsort[:,2] == float(i))[0] work4[i] = numpy.mean(phsort[elements,1]) work5[i] = math.sqrt(numpy.sum(numpy.power(phsort[elements,0], 2)) / len(elements)) # extend the work arrays beyond nbins by wrapping work4 = numpy.append(work4,work4[:duration2]) work5 = numpy.append(work5,work5[:duration2]) # calculate weights of folded light curve points sigmaSum = numpy.nansum(numpy.power(work5,-2)) omega = numpy.power(work5,-2) / sigmaSum # calculate weighted phased light curve s = omega * work4 # iterate through trial period phase for i1 in range(nbins): # iterate through transit durations for duration in range(duration1,duration2+1,int(halfHour)): # calculate maximum signal residue i2 = i1 + duration sr1 = numpy.sum(numpy.power(s[i1:i2],2)) sr2 = numpy.sum(omega[i1:i2]) sr = math.sqrt(sr1 / (sr2 * (1.0 - sr2))) if sr > srMax[-1]: srMax[-1] = sr transitDuration[-1] = float(duration) transitPhase[-1] = float((i1 + i2) / 2) # normalize maximum signal residue curve bestSr = numpy.max(srMax) bestTrial = numpy.nonzero(srMax == bestSr)[0][0] srMax /= bestSr transitDuration *= trialPeriods / 24.0 BJD0 = numpy.array(transitPhase * trialPeriods / nbins,dtype='float64') + intime[0] - 2454833.0 print '\n' # clean up x-axis unit if status == 0: ptime = copy(trialPeriods) xlab = 'Trial Period (days)' # clean up y-axis units if status == 0: pout = copy(srMax) ylab = 'Normalized Signal Residue' # 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.10,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() pylab.setp(labels, 'rotation', 90) # plot curve if status == 0 and plot: pylab.plot(ptime[1:-1],pout[1:-1],color=lcolor,linestyle='-',linewidth=lwidth) pylab.fill(ptime,pout,color=fcolor,linewidth=0.0,alpha=falpha) pylab.xlabel(xlab, {'color' : 'k'}) pylab.ylabel(ylab, {'color' : 'k'}) pylab.grid() # plot ranges if status == 0 and plot: pylab.xlim(xmin-xr*0.01,xmax+xr*0.01) if ymin >= 0.0: pylab.ylim(ymin-yr*0.01,ymax+yr*0.01) else: pylab.ylim(1.0e-10,ymax+yr*0.01) # render plot if status == 0 and plot: if cmdLine: pylab.show() else: pylab.ion() pylab.plot([]) pylab.ioff() # append new BLS data extension to the output file if status == 0: col1 = Column(name='PERIOD',format='E',unit='days',array=trialPeriods) col2 = Column(name='BJD0',format='D',unit='BJD - 2454833',array=BJD0) col3 = Column(name='DURATION',format='E',unit='hours',array=transitDuration) col4 = Column(name='SIG_RES',format='E',array=srMax) cols = ColDefs([col1,col2,col3,col4]) instr.append(new_table(cols)) instr[-1].header.cards['TTYPE1'].comment = 'column title: trial period' instr[-1].header.cards['TTYPE2'].comment = 'column title: trial mid-transit zero-point' instr[-1].header.cards['TTYPE3'].comment = 'column title: trial transit duration' instr[-1].header.cards['TTYPE4'].comment = 'column title: normalized signal residue' instr[-1].header.cards['TFORM1'].comment = 'column type: float32' instr[-1].header.cards['TFORM2'].comment = 'column type: float64' instr[-1].header.cards['TFORM3'].comment = 'column type: float32' instr[-1].header.cards['TFORM4'].comment = 'column type: float32' instr[-1].header.cards['TUNIT1'].comment = 'column units: days' instr[-1].header.cards['TUNIT2'].comment = 'column units: BJD - 2454833' instr[-1].header.cards['TUNIT3'].comment = 'column units: hours' instr[-1].header.update('EXTNAME','BLS','extension name') instr[-1].header.update('PERIOD',trialPeriods[bestTrial],'most significant trial period [d]') instr[-1].header.update('BJD0',BJD0[bestTrial] + 2454833.0,'time of mid-transit [BJD]') instr[-1].header.update('TRANSDUR',transitDuration[bestTrial],'transit duration [hours]') instr[-1].header.update('SIGNRES',srMax[bestTrial] * bestSr,'maximum signal residue') # history keyword in output file if status == 0: status = kepkey.history(call,instr[0],outfile,logfile,verbose) instr.writeto(outfile) # close input file if status == 0: status = kepio.closefits(instr,logfile,verbose) # print best trial period results if status == 0: print ' Best trial period = %.5f days' % trialPeriods[bestTrial] print ' Time of mid-transit = BJD %.5f' % (BJD0[bestTrial] + 2454833.0) print ' Transit duration = %.5f hours' % transitDuration[bestTrial] print ' Maximum signal residue = %.4g \n' % (srMax[bestTrial] * bestSr) # end time if (status == 0): message = 'KEPBLS completed at' else: message = '\nKEPBLS aborted at' kepmsg.clock(message,logfile,verbose)
def lsqclip(functype,pinit,x,y,yerr,rej_lo,rej_hi,niter,logfile,verbose): # functype = unctional form # pinit = initial guess for parameters # x = list of x data # y = list of y data # yerr = list of 1-sigma y errors # order = polynomial order # rej_lo = lower rejection threshold (units=sigma) # rej_hi = upper rejection threshold (units=sugma) # niter = number of sigma-clipping iterations npts = [] iiter = 0 iterstatus = 1 status = 0 # error catching if (len(x) == 0): status = kepmsg.exit('ERROR -- KEPFIT.LSQCLIP: x data array is empty') if (len(y) == 0): status = kepmsg.exit('ERROR -- KEPFIT.LSQCLIP: y data array is empty') if (len(x) < len(pinit)): kepmsg.warn(logfile,'WARNING -- KEPFIT.LSQCLIP: no degrees of freedom') # sigma-clipping iterations while (iiter < niter and len(x) > len(pinit) and iterstatus > 0): iterstatus = 0 tmpx = [] tmpy = [] tmpyerr = [] npts.append(len(x)) coeffs,errors,covar,sigma,chi2,dof,fit,plotx,ploty,status = \ leastsquare(functype,pinit,x,y,yerr,logfile,verbose) pinit = coeffs # point-by-point sigma-clipping test for ix in range(npts[iiter]): if (y[ix] - fit[ix] < rej_hi * sigma and fit[ix] - y[ix] < rej_lo * sigma): tmpx.append(x[ix]) tmpy.append(y[ix]) if (yerr != None): tmpyerr.append(yerr[ix]) else: iterstatus = 1 x = scipy.array(tmpx) y = scipy.array(tmpy) if (yerr != None): yerr = scipy.array(tmpyerr) iiter += 1 # fudge scalar models # for i in range(len(plotx)): # coeffs = best fit coefficients # covar = covariance matrix # iiter = number of sigma clipping iteration before convergence return coeffs, errors, covar, iiter, sigma, chi2, dof, fit, plotx, ploty, status
def kepfold(infile, outfile, period, phasezero, bindata, binmethod, threshold, niter, nbins, rejqual, plottype, plotlab, clobber, verbose, logfile, status, cmdLine=False): # startup parameters status = 0 labelsize = 32 ticksize = 18 xsize = 18 ysize = 10 lcolor = '#0000ff' lwidth = 2.0 fcolor = '#ffff00' falpha = 0.2 # log the call hashline = '----------------------------------------------------------------------------' kepmsg.log(logfile, hashline, verbose) call = 'KEPFOLD -- ' call += 'infile=' + infile + ' ' call += 'outfile=' + outfile + ' ' call += 'period=' + str(period) + ' ' call += 'phasezero=' + str(phasezero) + ' ' binit = 'n' if (bindata): binit = 'y' call += 'bindata=' + binit + ' ' call += 'binmethod=' + binmethod + ' ' call += 'threshold=' + str(threshold) + ' ' call += 'niter=' + str(niter) + ' ' call += 'nbins=' + str(nbins) + ' ' qflag = 'n' if (rejqual): qflag = 'y' call += 'rejqual=' + qflag + ' ' call += 'plottype=' + plottype + ' ' call += 'plotlab=' + 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('KEPFOLD 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 -- KEPFOLD: ' + 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 # 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 incards = instr[1].header.cards try: sap = instr[1].data.field('SAP_FLUX') except: try: sap = instr[1].data.field('ap_raw_flux') except: sap = zeros(len(table.field(0))) try: saperr = instr[1].data.field('SAP_FLUX_ERR') except: try: saperr = instr[1].data.field('ap_raw_err') except: saperr = zeros(len(table.field(0))) try: pdc = instr[1].data.field('PDCSAP_FLUX') except: try: pdc = instr[1].data.field('ap_corr_flux') except: pdc = zeros(len(table.field(0))) try: pdcerr = instr[1].data.field('PDCSAP_FLUX_ERR') except: try: pdcerr = instr[1].data.field('ap_corr_err') except: pdcerr = zeros(len(table.field(0))) try: cbv = instr[1].data.field('CBVSAP_FLUX') except: cbv = zeros(len(table.field(0))) if 'cbv' in plottype: txt = 'ERROR -- KEPFOLD: CBVSAP_FLUX column is not populated. Use kepcotrend' status = kepmsg.err(logfile, txt, verbose) try: det = instr[1].data.field('DETSAP_FLUX') except: det = zeros(len(table.field(0))) if 'det' in plottype: txt = 'ERROR -- KEPFOLD: DETSAP_FLUX column is not populated. Use kepflatten' status = kepmsg.err(logfile, txt, verbose) try: deterr = instr[1].data.field('DETSAP_FLUX_ERR') except: deterr = zeros(len(table.field(0))) if 'det' in plottype: txt = 'ERROR -- KEPFOLD: DETSAP_FLUX_ERR column is not populated. Use kepflatten' status = kepmsg.err(logfile, txt, verbose) try: quality = instr[1].data.field('SAP_QUALITY') except: quality = zeros(len(table.field(0))) if qualflag: txt = 'WARNING -- KEPFOLD: Cannot find a QUALITY data column' kepmsg.warn(logfile, txt) if status == 0: barytime, status = kepio.readtimecol(infile, table, logfile, verbose) barytime1 = copy(barytime) # filter out NaNs and quality > 0 work1 = [] work2 = [] work3 = [] work4 = [] work5 = [] work6 = [] work8 = [] work9 = [] if status == 0: if 'sap' in plottype: datacol = copy(sap) errcol = copy(saperr) if 'pdc' in plottype: datacol = copy(pdc) errcol = copy(pdcerr) if 'cbv' in plottype: datacol = copy(cbv) errcol = copy(saperr) if 'det' in plottype: datacol = copy(det) errcol = copy(deterr) for i in range(len(barytime)): if (numpy.isfinite(barytime[i]) and numpy.isfinite(datacol[i]) and datacol[i] != 0.0 and numpy.isfinite(errcol[i]) and errcol[i] > 0.0): if rejqual and quality[i] == 0: work1.append(barytime[i]) work2.append(sap[i]) work3.append(saperr[i]) work4.append(pdc[i]) work5.append(pdcerr[i]) work6.append(cbv[i]) work8.append(det[i]) work9.append(deterr[i]) elif not rejqual: work1.append(barytime[i]) work2.append(sap[i]) work3.append(saperr[i]) work4.append(pdc[i]) work5.append(pdcerr[i]) work6.append(cbv[i]) work8.append(det[i]) work9.append(deterr[i]) barytime = array(work1, dtype='float64') sap = array(work2, dtype='float32') / cadenom saperr = array(work3, dtype='float32') / cadenom pdc = array(work4, dtype='float32') / cadenom pdcerr = array(work5, dtype='float32') / cadenom cbv = array(work6, dtype='float32') / cadenom det = array(work8, dtype='float32') / cadenom deterr = array(work9, dtype='float32') / cadenom # calculate phase if status == 0: if phasezero < bjdref: phasezero += bjdref date1 = (barytime1 + bjdref - phasezero) phase1 = (date1 / period) - floor(date1 / period) date2 = (barytime + bjdref - phasezero) phase2 = (date2 / period) - floor(date2 / period) phase2 = array(phase2, 'float32') # sort phases if status == 0: ptuple = [] phase3 = [] sap3 = [] saperr3 = [] pdc3 = [] pdcerr3 = [] cbv3 = [] cbverr3 = [] det3 = [] deterr3 = [] for i in range(len(phase2)): ptuple.append([ phase2[i], sap[i], saperr[i], pdc[i], pdcerr[i], cbv[i], saperr[i], det[i], deterr[i] ]) phsort = sorted(ptuple, key=lambda ph: ph[0]) for i in range(len(phsort)): phase3.append(phsort[i][0]) sap3.append(phsort[i][1]) saperr3.append(phsort[i][2]) pdc3.append(phsort[i][3]) pdcerr3.append(phsort[i][4]) cbv3.append(phsort[i][5]) cbverr3.append(phsort[i][6]) det3.append(phsort[i][7]) deterr3.append(phsort[i][8]) phase3 = array(phase3, 'float32') sap3 = array(sap3, 'float32') saperr3 = array(saperr3, 'float32') pdc3 = array(pdc3, 'float32') pdcerr3 = array(pdcerr3, 'float32') cbv3 = array(cbv3, 'float32') cbverr3 = array(cbverr3, 'float32') det3 = array(det3, 'float32') deterr3 = array(deterr3, 'float32') # bin phases if status == 0 and bindata: work1 = array([sap3[0]], 'float32') work2 = array([saperr3[0]], 'float32') work3 = array([pdc3[0]], 'float32') work4 = array([pdcerr3[0]], 'float32') work5 = array([cbv3[0]], 'float32') work6 = array([cbverr3[0]], 'float32') work7 = array([det3[0]], 'float32') work8 = array([deterr3[0]], 'float32') phase4 = array([], 'float32') sap4 = array([], 'float32') saperr4 = array([], 'float32') pdc4 = array([], 'float32') pdcerr4 = array([], 'float32') cbv4 = array([], 'float32') cbverr4 = array([], 'float32') det4 = array([], 'float32') deterr4 = array([], 'float32') dt = 1.0 / nbins nb = 0.0 rng = numpy.append(phase3, phase3[0] + 1.0) for i in range(len(rng)): if rng[i] < nb * dt or rng[i] >= (nb + 1.0) * dt: if len(work1) > 0: phase4 = append(phase4, (nb + 0.5) * dt) if (binmethod == 'mean'): sap4 = append(sap4, kepstat.mean(work1)) saperr4 = append(saperr4, kepstat.mean_err(work2)) pdc4 = append(pdc4, kepstat.mean(work3)) pdcerr4 = append(pdcerr4, kepstat.mean_err(work4)) cbv4 = append(cbv4, kepstat.mean(work5)) cbverr4 = append(cbverr4, kepstat.mean_err(work6)) det4 = append(det4, kepstat.mean(work7)) deterr4 = append(deterr4, kepstat.mean_err(work8)) elif (binmethod == 'median'): sap4 = append(sap4, kepstat.median(work1, logfile)) saperr4 = append(saperr4, kepstat.mean_err(work2)) pdc4 = append(pdc4, kepstat.median(work3, logfile)) pdcerr4 = append(pdcerr4, kepstat.mean_err(work4)) cbv4 = append(cbv4, kepstat.median(work5, logfile)) cbverr4 = append(cbverr4, kepstat.mean_err(work6)) det4 = append(det4, kepstat.median(work7, logfile)) deterr4 = append(deterr4, kepstat.mean_err(work8)) else: coeffs, errors, covar, iiter, sigma, chi2, dof, fit, plotx, ploty, status = \ kepfit.lsqclip('poly0',[scipy.stats.nanmean(work1)],arange(0.0,float(len(work1)),1.0),work1,work2, threshold,threshold,niter,logfile,False) sap4 = append(sap4, coeffs[0]) saperr4 = append(saperr4, kepstat.mean_err(work2)) coeffs, errors, covar, iiter, sigma, chi2, dof, fit, plotx, ploty, status = \ kepfit.lsqclip('poly0',[scipy.stats.nanmean(work3)],arange(0.0,float(len(work3)),1.0),work3,work4, threshold,threshold,niter,logfile,False) pdc4 = append(pdc4, coeffs[0]) pdcerr4 = append(pdcerr4, kepstat.mean_err(work4)) coeffs, errors, covar, iiter, sigma, chi2, dof, fit, plotx, ploty, status = \ kepfit.lsqclip('poly0',[scipy.stats.nanmean(work5)],arange(0.0,float(len(work5)),1.0),work5,work6, threshold,threshold,niter,logfile,False) cbv4 = append(cbv4, coeffs[0]) cbverr4 = append(cbverr4, kepstat.mean_err(work6)) coeffs, errors, covar, iiter, sigma, chi2, dof, fit, plotx, ploty, status = \ kepfit.lsqclip('poly0',[scipy.stats.nanmean(work7)],arange(0.0,float(len(work7)),1.0),work7,work8, threshold,threshold,niter,logfile,False) det4 = append(det4, coeffs[0]) deterr4 = append(deterr4, kepstat.mean_err(work8)) work1 = array([], 'float32') work2 = array([], 'float32') work3 = array([], 'float32') work4 = array([], 'float32') work5 = array([], 'float32') work6 = array([], 'float32') work7 = array([], 'float32') work8 = array([], 'float32') nb += 1.0 else: work1 = append(work1, sap3[i]) work2 = append(work2, saperr3[i]) work3 = append(work3, pdc3[i]) work4 = append(work4, pdcerr3[i]) work5 = append(work5, cbv3[i]) work6 = append(work6, cbverr3[i]) work7 = append(work7, det3[i]) work8 = append(work8, deterr3[i]) # update HDU1 for output file if status == 0: cols = (instr[1].columns + ColDefs([Column(name='PHASE', format='E', array=phase1)])) instr[1] = pyfits.new_table(cols) instr[1].header.cards[ 'TTYPE' + str(len(instr[1].columns))].comment = 'column title: phase' instr[1].header.cards[ 'TFORM' + str(len(instr[1].columns))].comment = 'data type: float32' for i in range(len(incards)): if incards[i].key not in list(instr[1].header.keys()): instr[1].header.update(incards[i].key, incards[i].value, incards[i].comment) else: instr[1].header.cards[ incards[i].key].comment = incards[i].comment instr[1].header.update('PERIOD', period, 'period defining the phase [d]') instr[1].header.update('BJD0', phasezero, 'time of phase zero [BJD]') # write new phased data extension for output file if status == 0 and bindata: col1 = Column(name='PHASE', format='E', array=phase4) col2 = Column(name='SAP_FLUX', format='E', unit='e/s', array=sap4 / cadenom) col3 = Column(name='SAP_FLUX_ERR', format='E', unit='e/s', array=saperr4 / cadenom) col4 = Column(name='PDC_FLUX', format='E', unit='e/s', array=pdc4 / cadenom) col5 = Column(name='PDC_FLUX_ERR', format='E', unit='e/s', array=pdcerr4 / cadenom) col6 = Column(name='CBV_FLUX', format='E', unit='e/s', array=cbv4 / cadenom) col7 = Column(name='DET_FLUX', format='E', array=det4 / cadenom) col8 = Column(name='DET_FLUX_ERR', format='E', array=deterr4 / cadenom) cols = ColDefs([col1, col2, col3, col4, col5, col6, col7, col8]) instr.append(new_table(cols)) instr[-1].header.cards['TTYPE1'].comment = 'column title: phase' instr[-1].header.cards[ 'TTYPE2'].comment = 'column title: simple aperture photometry' instr[-1].header.cards[ 'TTYPE3'].comment = 'column title: SAP 1-sigma error' instr[-1].header.cards[ 'TTYPE4'].comment = 'column title: pipeline conditioned photometry' instr[-1].header.cards[ 'TTYPE5'].comment = 'column title: PDC 1-sigma error' instr[-1].header.cards[ 'TTYPE6'].comment = 'column title: cotrended basis vector photometry' instr[-1].header.cards[ 'TTYPE7'].comment = 'column title: Detrended aperture photometry' instr[-1].header.cards[ 'TTYPE8'].comment = 'column title: DET 1-sigma error' instr[-1].header.cards['TFORM1'].comment = 'column type: float32' instr[-1].header.cards['TFORM2'].comment = 'column type: float32' instr[-1].header.cards['TFORM3'].comment = 'column type: float32' instr[-1].header.cards['TFORM4'].comment = 'column type: float32' instr[-1].header.cards['TFORM5'].comment = 'column type: float32' instr[-1].header.cards['TFORM6'].comment = 'column type: float32' instr[-1].header.cards['TFORM7'].comment = 'column type: float32' instr[-1].header.cards['TFORM8'].comment = 'column type: float32' instr[-1].header.cards[ 'TUNIT2'].comment = 'column units: electrons per second' instr[-1].header.cards[ 'TUNIT3'].comment = 'column units: electrons per second' instr[-1].header.cards[ 'TUNIT4'].comment = 'column units: electrons per second' instr[-1].header.cards[ 'TUNIT5'].comment = 'column units: electrons per second' instr[-1].header.cards[ 'TUNIT6'].comment = 'column units: electrons per second' instr[-1].header.update('EXTNAME', 'FOLDED', 'extension name') instr[-1].header.update('PERIOD', period, 'period defining the phase [d]') instr[-1].header.update('BJD0', phasezero, 'time of phase zero [BJD]') instr[-1].header.update('BINMETHD', binmethod, 'phase binning method') if binmethod == 'sigclip': instr[-1].header.update('THRSHOLD', threshold, 'sigma-clipping threshold [sigma]') instr[-1].header.update('NITER', niter, 'max number of sigma-clipping iterations') # history keyword in output file if status == 0: status = kepkey.history(call, instr[0], outfile, logfile, verbose) instr.writeto(outfile) # clean up x-axis unit if status == 0: ptime1 = array([], 'float32') ptime2 = array([], 'float32') pout1 = array([], 'float32') pout2 = array([], 'float32') if bindata: work = sap4 if plottype == 'pdc': work = pdc4 if plottype == 'cbv': work = cbv4 if plottype == 'det': work = det4 for i in range(len(phase4)): if (phase4[i] > 0.5): ptime2 = append(ptime2, phase4[i] - 1.0) pout2 = append(pout2, work[i]) ptime2 = append(ptime2, phase4) pout2 = append(pout2, work) for i in range(len(phase4)): if (phase4[i] <= 0.5): ptime2 = append(ptime2, phase4[i] + 1.0) pout2 = append(pout2, work[i]) work = sap3 if plottype == 'pdc': work = pdc3 if plottype == 'cbv': work = cbv3 if plottype == 'det': work = det3 for i in range(len(phase3)): if (phase3[i] > 0.5): ptime1 = append(ptime1, phase3[i] - 1.0) pout1 = append(pout1, work[i]) ptime1 = append(ptime1, phase3) pout1 = append(pout1, work) for i in range(len(phase3)): if (phase3[i] <= 0.5): ptime1 = append(ptime1, phase3[i] + 1.0) pout1 = append(pout1, work[i]) xlab = 'Orbital Phase ($\phi$)' # clean up y-axis units if status == 0: nrm = len(str(int(pout1[isfinite(pout1)].max()))) - 1 pout1 = pout1 / 10**nrm pout2 = pout2 / 10**nrm if nrm == 0: ylab = plotlab else: ylab = '10$^%d$ %s' % (nrm, plotlab) # data limits xmin = ptime1.min() xmax = ptime1.max() ymin = pout1[isfinite(pout1)].min() ymax = pout1[isfinite(pout1)].max() xr = xmax - xmin yr = ymax - ymin ptime1 = insert(ptime1, [0], [ptime1[0]]) ptime1 = append(ptime1, [ptime1[-1]]) pout1 = insert(pout1, [0], [0.0]) pout1 = append(pout1, 0.0) if bindata: ptime2 = insert(ptime2, [0], ptime2[0] - 1.0 / nbins) ptime2 = insert(ptime2, [0], ptime2[0]) ptime2 = append( ptime2, [ptime2[-1] + 1.0 / nbins, ptime2[-1] + 1.0 / nbins]) pout2 = insert(pout2, [0], [pout2[-1]]) pout2 = insert(pout2, [0], [0.0]) pout2 = append(pout2, [pout2[2], 0.0]) # plot new light curve if status == 0 and plottype != 'none': try: params = { 'backend': 'png', 'axes.linewidth': 2.5, 'axes.labelsize': labelsize, 'axes.font': 'sans-serif', 'axes.fontweight': 'bold', 'text.fontsize': 18, 'legend.fontsize': 18, 'xtick.labelsize': ticksize, 'ytick.labelsize': ticksize } pylab.rcParams.update(params) except: print('ERROR -- KEPFOLD: install latex for scientific plotting') status = 1 if status == 0 and plottype != 'none': pylab.figure(figsize=[17, 7]) pylab.clf() ax = pylab.axes([0.06, 0.11, 0.93, 0.86]) 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) if bindata: pylab.fill(ptime2, pout2, color=fcolor, linewidth=0.0, alpha=falpha) else: if 'det' in plottype: pylab.fill(ptime1, pout1, color=fcolor, linewidth=0.0, alpha=falpha) pylab.plot(ptime1, pout1, color=lcolor, linestyle='', linewidth=lwidth, marker='.') if bindata: pylab.plot(ptime2[1:-1], pout2[1:-1], color='r', linestyle='-', linewidth=lwidth, marker='') xlabel(xlab, {'color': 'k'}) ylabel(ylab, {'color': 'k'}) xlim(-0.49999, 1.49999) if ymin >= 0.0: ylim(ymin - yr * 0.01, ymax + yr * 0.01) # ylim(0.96001,1.03999) else: ylim(1.0e-10, ymax + yr * 0.01) grid() if cmdLine: pylab.show() else: pylab.ion() pylab.plot([]) pylab.ioff() # close input file if status == 0: status = kepio.closefits(instr, logfile, verbose) # stop time kepmsg.clock('KEPFOLD ended at: ', logfile, verbose)
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 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 lsqclip(functype, pinit, x, y, yerr, rej_lo, rej_hi, niter, logfile, verbose): # functype = unctional form # pinit = initial guess for parameters # x = list of x data # y = list of y data # yerr = list of 1-sigma y errors # order = polynomial order # rej_lo = lower rejection threshold (units=sigma) # rej_hi = upper rejection threshold (units=sugma) # niter = number of sigma-clipping iterations npts = [] iiter = 0 iterstatus = 1 status = 0 # error catching if (len(x) == 0): status = kepmsg.exit('ERROR -- KEPFIT.LSQCLIP: x data array is empty') if (len(y) == 0): status = kepmsg.exit('ERROR -- KEPFIT.LSQCLIP: y data array is empty') if (len(x) < len(pinit)): kepmsg.warn(logfile, 'WARNING -- KEPFIT.LSQCLIP: no degrees of freedom') # sigma-clipping iterations while (iiter < niter and len(x) > len(pinit) and iterstatus > 0): iterstatus = 0 tmpx = [] tmpy = [] tmpyerr = [] npts.append(len(x)) coeffs,errors,covar,sigma,chi2,dof,fit,plotx,ploty,status = \ leastsquare(functype,pinit,x,y,yerr,logfile,verbose) pinit = coeffs # point-by-point sigma-clipping test for ix in range(npts[iiter]): if (y[ix] - fit[ix] < rej_hi * sigma and fit[ix] - y[ix] < rej_lo * sigma): tmpx.append(x[ix]) tmpy.append(y[ix]) if (yerr != None): tmpyerr.append(yerr[ix]) else: iterstatus = 1 x = scipy.array(tmpx) y = scipy.array(tmpy) if (yerr != None): yerr = scipy.array(tmpyerr) iiter += 1 # fudge scalar models # for i in range(len(plotx)): # coeffs = best fit coefficients # covar = covariance matrix # iiter = number of sigma clipping iteration before convergence return coeffs, errors, covar, iiter, sigma, chi2, dof, fit, plotx, ploty, status
def MASTRADec(ra, dec, darcsec, srctab): # coordinate limits darcsec /= 3600.0 ra1 = ra - darcsec / cos(dec * pi / 180) ra2 = ra + darcsec / cos(dec * pi / 180) dec1 = dec - darcsec dec2 = dec + darcsec # build mast query url = 'http://archive.stsci.edu/kepler/kepler_fov/search.php?' url += 'action=Search' url += '&masterRA=' + str(ra1) + '..' + str(ra2) url += '&masterDec=' + str(dec1) + '..' + str(dec2) url += '&max_records=10000' url += '&verb=3' url += '&outputformat=CSV' # retrieve results from MAST if srctab: try: lines = urllib.request.urlopen(url) except: message = 'WARNING -- KEPFIELD: Cannot retrieve data from MAST' status = kepmsg.warn(logfile, message) lines = '' else: lines = '' # collate nearby sources kepid = [] kepmag = [] ra = [] dec = [] for line in lines: line = line.strip() # Python 3 hack try: 'Kepler' in line except: line = line.decode('ascii') if (len(line) > 0 and 'Kepler' not in line and 'integer' not in line and 'no rows found' not in line): out = line.split(',') r, d = sex2dec(out[0], out[1]) try: if out[-22] != 'Possible_artifact': kepid.append(int(out[2])) except: if out[-22] != 'Possible_artifact': kepid.append(0) try: if out[-22] != 'Possible_artifact': kepmag.append(float(out[42])) except: if out[-22] != 'Possible_artifact': kepmag.append(0.0) if out[-22] != 'Possible_artifact': ra.append(r) if out[-22] != 'Possible_artifact': dec.append(d) kepid = array(kepid) kepmag = array(kepmag) ra = array(ra) dec = array(dec) return kepid, ra, dec, kepmag
def getWCSp(file, struct, logfile, verbose): status = 0 crpix1p = 0.0 crpix2p = 0.0 crval1p = 0.0 crval2p = 0.0 cdelt1p = 0.0 cdelt2p = 0.0 try: crpix1p, status = get(file, struct, 'CRPIX1P', logfile, verbose) except: txt = 'WARNING -- KEPKEY.GETWCSP: Cannot read keyword CRPIX1P in file ' + file kepmsg.warn(logfile, txt) status = 1 try: crpix2p, status = get(file, struct, 'CRPIX2P', logfile, verbose) except: txt = 'WARNING -- KEPKEY.GETWCSP: Cannot read keyword CRPIX2P in file ' + file kepmsg.warn(logfile, txt) status = 1 try: crval1p, status = get(file, struct, 'CRVAL1P', logfile, verbose) except: txt = 'WARNING -- KEPKEY.GETWCSP: Cannot read keyword CRVAL1P in file ' + file kepmsg.warn(logfile, txt) status = 1 try: crval2p, status = get(file, struct, 'CRVAL2P', logfile, verbose) except: txt = 'WARNING -- KEPKEY.GETWCSP: Cannot read keyword CRVAL2P in file ' + file kepmsg.warn(logfile, txt) status = 1 try: cdelt1p, status = get(file, struct, 'CDELT1P', logfile, verbose) except: txt = 'WARNING -- KEPKEY.GETWCSP: Cannot read keyword CDELT1P in file ' + file kepmsg.warn(logfile, txt) status = 1 try: cdelt2p, status = get(file, struct, 'CDELT2P', logfile, verbose) except: txt = 'WARNING -- KEPKEY.GETWCSP: Cannot read keyword CDELT2P in file ' + file kepmsg.warn(logfile, txt) status = 1 return crpix1p, crpix2p, crval1p, crval2p, cdelt1p, cdelt2p, status
def getWCSs(file, struct, logfile, verbose): status = 0 crpix1 = 0.0 crpix2 = 0.0 crval1 = 0.0 crval2 = 0.0 cdelt1 = 0.0 cdelt2 = 0.0 pc = [0.0, 0.0, 0.0, 0.0] try: crpix1, status = get(file, struct, 'CRPIX1', logfile, verbose) except: txt = 'WARNING -- KEPKEY.GETWCSS: Cannot read keyword CRPIX1 in file ' + file kepmsg.warn(logfile, txt) status = 1 try: crpix2, status = get(file, struct, 'CRPIX2', logfile, verbose) except: txt = 'WARNING -- KEPKEY.GETWCSS: Cannot read keyword CRPIX2 in file ' + file kepmsg.warn(logfile, txt) status = 1 try: crval1, status = get(file, struct, 'CRVAL1', logfile, verbose) except: txt = 'WARNING -- KEPKEY.GETWCSS: Cannot read keyword CRVAL1 in file ' + file kepmsg.warn(logfile, txt) status = 1 try: crval2, status = get(file, struct, 'CRVAL2', logfile, verbose) except: txt = 'WARNING -- KEPKEY.GETWCSS: Cannot read keyword CRVAL2 in file ' + file kepmsg.warn(logfile, txt) status = 1 try: cdelt1, status = get(file, struct, 'CDELT1', logfile, verbose) except: txt = 'WARNING -- KEPKEY.GETWCSS: Cannot read keyword CDELT1 in file ' + file kepmsg.warn(logfile, txt) status = 1 try: cdelt2, status = get(file, struct, 'CDELT2', logfile, verbose) except: txt = 'WARNING -- KEPKEY.GETWCSS: Cannot read keyword CDELT2 in file ' + file kepmsg.warn(logfile, txt) status = 1 try: pc1_1, status = get(file, struct, 'PC1_1', logfile, verbose) except: txt = 'WARNING -- KEPKEY.GETWCSS: Cannot read keyword PC1_1 in file ' + file kepmsg.warn(logfile, txt) status = 1 try: pc1_2, status = get(file, struct, 'PC1_2', logfile, verbose) except: txt = 'WARNING -- KEPKEY.GETWCSS: Cannot read keyword PC1_2 in file ' + file kepmsg.warn(logfile, txt) status = 1 try: pc2_1, status = get(file, struct, 'PC2_1', logfile, verbose) except: txt = 'WARNING -- KEPKEY.GETWCSS: Cannot read keyword PC2_1 in file ' + file kepmsg.warn(logfile, txt) status = 1 try: pc2_2, status = get(file, struct, 'PC2_2', logfile, verbose) except: txt = 'WARNING -- KEPKEY.GETWCSS: Cannot read keyword PC2_2 in file ' + file kepmsg.warn(logfile, txt) status = 1 try: pc = numpy.array([[pc1_1, pc1_2], [pc2_1, pc2_2]]) pc = numpy.linalg.inv(pc) except: pass return crpix1, crpix2, crval1, crval2, cdelt1, cdelt2, pc, status
def kepbls(infile, outfile, datacol, errcol, minper, maxper, mindur, maxdur, nsearch, nbins, plot, clobber, verbose, logfile, status, cmdLine=False): # startup parameters numpy.seterr(all="ignore") status = 0 labelsize = 32 ticksize = 18 xsize = 16 ysize = 8 lcolor = '#0000ff' lwidth = 1.0 fcolor = '#ffff00' falpha = 0.2 # log the call hashline = '----------------------------------------------------------------------------' kepmsg.log(logfile, hashline, verbose) call = 'KEPBLS -- ' call += 'infile=' + infile + ' ' call += 'outfile=' + outfile + ' ' call += 'datacol=' + str(datacol) + ' ' call += 'errcol=' + str(errcol) + ' ' call += 'minper=' + str(minper) + ' ' call += 'maxper=' + str(maxper) + ' ' call += 'mindur=' + str(mindur) + ' ' call += 'maxdur=' + str(maxdur) + ' ' call += 'nsearch=' + str(nsearch) + ' ' call += 'nbins=' + str(nbins) + ' ' plotit = 'n' if (plot): plotit = 'y' call += 'plot=' + plotit + ' ' overwrite = 'n' if (clobber): overwrite = 'y' call += 'clobber=' + overwrite + ' ' chatter = 'n' if (verbose): chatter = 'y' call += 'verbose=' + chatter + ' ' call += 'logfile=' + logfile kepmsg.log(logfile, call + '\n', verbose) # start time kepmsg.clock('KEPBLS started at', logfile, verbose) # is duration greater than one bin in the phased light curve? if float(nbins) * maxdur / 24.0 / maxper <= 1.0: message = 'WARNING -- KEPBLS: ' + str( maxdur) + ' hours transit duration < 1 phase bin when P = ' message += str(maxper) + ' days' kepmsg.warn(logfile, message) # test log file logfile = kepmsg.test(logfile) # clobber output file if clobber: status = kepio.clobber(outfile, logfile, verbose) if kepio.fileexists(outfile): message = 'ERROR -- KEPBLS: ' + 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) # 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), table.field(errcol)]) work1 = numpy.rot90(work1, 3) work1 = work1[~numpy.isnan(work1).any(1)] # read table columns if status == 0: intime = work1[:, 2] + bjdref indata = work1[:, 1] inerr = work1[:, 0] # test whether the period range is sensible if status == 0: tr = intime[-1] - intime[0] if maxper > tr: message = 'ERROR -- KEPBLS: maxper is larger than the time range of the input data' status = kepmsg.err(logfile, message, verbose) # prepare time series if status == 0: work1 = intime - intime[0] work2 = indata - numpy.mean(indata) # start period search if status == 0: srMax = numpy.array([], dtype='float32') transitDuration = numpy.array([], dtype='float32') transitPhase = numpy.array([], dtype='float32') dPeriod = (maxper - minper) / nsearch trialPeriods = numpy.arange(minper, maxper + dPeriod, dPeriod, dtype='float32') complete = 0 print ' ' for trialPeriod in trialPeriods: fracComplete = float(complete) / float(len(trialPeriods) - 1) * 100.0 txt = '\r' txt += 'Trial period = ' txt += str(int(trialPeriod)) txt += ' days [' txt += str(int(fracComplete)) txt += '% complete]' txt += ' ' * 20 sys.stdout.write(txt) sys.stdout.flush() complete += 1 srMax = numpy.append(srMax, 0.0) transitDuration = numpy.append(transitDuration, numpy.nan) transitPhase = numpy.append(transitPhase, numpy.nan) trialFrequency = 1.0 / trialPeriod # minimum and maximum transit durations in quantized phase units duration1 = max(int(float(nbins) * mindur / 24.0 / trialPeriod), 2) duration2 = max( int(float(nbins) * maxdur / 24.0 / trialPeriod) + 1, duration1 + 1) # 30 minutes in quantized phase units halfHour = int(0.02083333 / trialPeriod * nbins + 1) # compute folded time series with trial period work4 = numpy.zeros((nbins), dtype='float32') work5 = numpy.zeros((nbins), dtype='float32') phase = numpy.array( ((work1 * trialFrequency) - numpy.floor(work1 * trialFrequency)) * float(nbins), dtype='int') ptuple = numpy.array([phase, work2, inerr]) ptuple = numpy.rot90(ptuple, 3) phsort = numpy.array(sorted(ptuple, key=lambda ph: ph[2])) for i in range(nbins): elements = numpy.nonzero(phsort[:, 2] == float(i))[0] work4[i] = numpy.mean(phsort[elements, 1]) work5[i] = math.sqrt( numpy.sum(numpy.power(phsort[elements, 0], 2)) / len(elements)) # extend the work arrays beyond nbins by wrapping work4 = numpy.append(work4, work4[:duration2]) work5 = numpy.append(work5, work5[:duration2]) # calculate weights of folded light curve points sigmaSum = numpy.nansum(numpy.power(work5, -2)) omega = numpy.power(work5, -2) / sigmaSum # calculate weighted phased light curve s = omega * work4 # iterate through trial period phase for i1 in range(nbins): # iterate through transit durations for duration in range(duration1, duration2 + 1, int(halfHour)): # calculate maximum signal residue i2 = i1 + duration sr1 = numpy.sum(numpy.power(s[i1:i2], 2)) sr2 = numpy.sum(omega[i1:i2]) sr = math.sqrt(sr1 / (sr2 * (1.0 - sr2))) if sr > srMax[-1]: srMax[-1] = sr transitDuration[-1] = float(duration) transitPhase[-1] = float((i1 + i2) / 2) # normalize maximum signal residue curve bestSr = numpy.max(srMax) bestTrial = numpy.nonzero(srMax == bestSr)[0][0] srMax /= bestSr transitDuration *= trialPeriods / 24.0 BJD0 = numpy.array(transitPhase * trialPeriods / nbins, dtype='float64') + intime[0] - 2454833.0 print '\n' # clean up x-axis unit if status == 0: ptime = copy(trialPeriods) xlab = 'Trial Period (days)' # clean up y-axis units if status == 0: pout = copy(srMax) ylab = 'Normalized Signal Residue' # 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.10, 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() pylab.setp(labels, 'rotation', 90) # plot curve if status == 0 and plot: pylab.plot(ptime[1:-1], pout[1:-1], color=lcolor, linestyle='-', linewidth=lwidth) pylab.fill(ptime, pout, color=fcolor, linewidth=0.0, alpha=falpha) pylab.xlabel(xlab, {'color': 'k'}) pylab.ylabel(ylab, {'color': 'k'}) pylab.grid() # plot ranges if status == 0 and plot: pylab.xlim(xmin - xr * 0.01, xmax + xr * 0.01) if ymin >= 0.0: pylab.ylim(ymin - yr * 0.01, ymax + yr * 0.01) else: pylab.ylim(1.0e-10, ymax + yr * 0.01) # render plot if status == 0 and plot: if cmdLine: pylab.show() else: pylab.ion() pylab.plot([]) pylab.ioff() # append new BLS data extension to the output file if status == 0: col1 = Column(name='PERIOD', format='E', unit='days', array=trialPeriods) col2 = Column(name='BJD0', format='D', unit='BJD - 2454833', array=BJD0) col3 = Column(name='DURATION', format='E', unit='hours', array=transitDuration) col4 = Column(name='SIG_RES', format='E', array=srMax) cols = ColDefs([col1, col2, col3, col4]) instr.append(new_table(cols)) instr[-1].header.cards['TTYPE1'].comment = 'column title: trial period' instr[-1].header.cards[ 'TTYPE2'].comment = 'column title: trial mid-transit zero-point' instr[-1].header.cards[ 'TTYPE3'].comment = 'column title: trial transit duration' instr[-1].header.cards[ 'TTYPE4'].comment = 'column title: normalized signal residue' instr[-1].header.cards['TFORM1'].comment = 'column type: float32' instr[-1].header.cards['TFORM2'].comment = 'column type: float64' instr[-1].header.cards['TFORM3'].comment = 'column type: float32' instr[-1].header.cards['TFORM4'].comment = 'column type: float32' instr[-1].header.cards['TUNIT1'].comment = 'column units: days' instr[-1].header.cards[ 'TUNIT2'].comment = 'column units: BJD - 2454833' instr[-1].header.cards['TUNIT3'].comment = 'column units: hours' instr[-1].header.update('EXTNAME', 'BLS', 'extension name') instr[-1].header.update('PERIOD', trialPeriods[bestTrial], 'most significant trial period [d]') instr[-1].header.update('BJD0', BJD0[bestTrial] + 2454833.0, 'time of mid-transit [BJD]') instr[-1].header.update('TRANSDUR', transitDuration[bestTrial], 'transit duration [hours]') instr[-1].header.update('SIGNRES', srMax[bestTrial] * bestSr, 'maximum signal residue') # history keyword in output file if status == 0: status = kepkey.history(call, instr[0], outfile, logfile, verbose) instr.writeto(outfile) # close input file if status == 0: status = kepio.closefits(instr, logfile, verbose) # print best trial period results if status == 0: print ' Best trial period = %.5f days' % trialPeriods[bestTrial] print ' Time of mid-transit = BJD %.5f' % (BJD0[bestTrial] + 2454833.0) print ' Transit duration = %.5f hours' % transitDuration[ bestTrial] print ' Maximum signal residue = %.4g \n' % (srMax[bestTrial] * bestSr) # end time if (status == 0): message = 'KEPBLS completed at' else: message = '\nKEPBLS 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 kepimages(infile,outfix,imtype,ranges,clobber,verbose,logfile,status): # startup parameters status = 0 # log the call hashline = '----------------------------------------------------------------------------' kepmsg.log(logfile,hashline,verbose) call = 'KEPIMAGES -- ' call += 'infile='+infile+' ' call += 'outfix='+outfix+' ' call += 'imtype='+imtype+' ' call += 'ranges='+str(ranges)+' ' 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('KEPIMAGES started at',logfile,verbose) # test log file logfile = kepmsg.test(logfile) # open input file status = 0 print(' ') instr = pyfits.open(infile,mode='readonly',memmap=True) cards0 = instr[0].header.cards cards1 = instr[1].header.cards cards2 = instr[2].header.cards # fudge non-compliant FITS keywords with no values if status == 0: instr = kepkey.emptykeys(instr,file,logfile,verbose) # ingest time series data if status == 0: time = instr[1].data.field('TIME')[:] + 2454833.0 timecorr = instr[1].data.field('TIMECORR')[:] cadenceno = instr[1].data.field('CADENCENO')[:] raw_cnts = instr[1].data.field('RAW_CNTS')[:] flux = instr[1].data.field('FLUX')[:] flux_err = instr[1].data.field('FLUX_ERR')[:] flux_bkg = instr[1].data.field('FLUX_BKG')[:] flux_bkg_err = instr[1].data.field('FLUX_BKG_ERR')[:] cosmic_rays = instr[1].data.field('COSMIC_RAYS')[:] quality = instr[1].data.field('QUALITY')[:] pos_corr1 = instr[1].data.field('POS_CORR1')[:] pos_corr2 = instr[1].data.field('POS_CORR2')[:] # choose output image if status == 0: if imtype.lower() == 'raw_cnts': outim = raw_cnts elif imtype.lower() == 'flux_err': outim = flux_err elif imtype.lower() == 'flux_bkg': outim = flux_bkg elif imtype.lower() == 'flux_bkg_err': outim = flux_bkg_err elif imtype.lower() == 'cosmic_rays': outim = cosmic_rays else: outim = flux # identify images to be exported if status == 0: tim = array([]); dat = array([]); err = array([]) tstart, tstop, status = kepio.timeranges(ranges,logfile,verbose) if status == 0: cadencelis, status = kepstat.filterOnRange(time,tstart,tstop) # provide name for each output file and clobber if file exists if status == 0: for cadence in cadencelis: outfile = outfix + '_BJD%.4f' % time[cadence] + '.fits' if clobber and status == 0: status = kepio.clobber(outfile,logfile,verbose) if kepio.fileexists(outfile) and status == 0: message = 'ERROR -- KEPIMAGES: ' + outfile + ' exists. Use --clobber' status = kepmsg.err(logfile,message,True) # construct output primary extension if status == 0: ncad = 0 for cadence in cadencelis: outfile = outfix + '_BJD%.4f' % time[cadence] + '.fits' hdu0 = pyfits.PrimaryHDU() for i in range(len(cards0)): try: if cards0[i].key not in list(hdu0.header.keys()): hdu0.header.update(cards0[i].key, cards0[i].value, cards0[i].comment) else: hdu0.header.cards[cards0[i].key].comment = cards0[i].comment except: pass status = kepkey.history(call,hdu0,outfile,logfile,verbose) outstr = HDUList(hdu0) # construct output image extension hdu1 = ImageHDU(flux[cadence]) for i in range(len(cards2)): try: if cards2[i].key not in list(hdu1.header.keys()): hdu1.header.update(cards2[i].key, cards2[i].value, cards2[i].comment) except: pass for i in range(len(cards1)): if (cards1[i].key not in list(hdu1.header.keys()) and cards1[i].key[:4] not in ['TTYP','TFOR','TUNI','TDIS','TDIM','WCAX','1CTY', '2CTY','1CRP','2CRP','1CRV','2CRV','1CUN','2CUN', '1CDE','2CDE','1CTY','2CTY','1CDL','2CDL','11PC', '12PC','21PC','22PC','WCSN','TFIE']): hdu1.header.update(cards1[i].key, cards1[i].value, cards1[i].comment) try: int_time = cards1['INT_TIME'].value except: kepmsg.warn(logfile,'WARNING -- KEPIMAGES: cannot find INT_TIME keyword') try: frametim = cards1['FRAMETIM'].value except: kepmsg.warn(logfile,'WARNING -- KEPIMAGES: cannot find FRAMETIM keyword') try: num_frm = cards1['NUM_FRM'].value except: kepmsg.warn(logfile,'WARNING -- KEPIMAGES: cannot find NUM_FRM keyword') hdu1.header.update('EXTNAME','IMAGE','name of extension') try: hdu1.header.update('TELAPSE',frametim * num_frm,'[s] elapsed time for exposure') except: hdu1.header.update('TELAPSE',-999,'[s] elapsed time for exposure') try: hdu1.header.update('LIVETIME',int_time * num_frm,'[s] TELASPE multiplied by DEADC') except: hdu1.header.update('LIVETIME',-999,'[s] TELASPE multiplied by DEADC') try: hdu1.header.update('EXPOSURE',int_time * num_frm,'[s] time on source') except: hdu1.header.update('EXPOSURE',-999,'[s] time on source') try: hdu1.header.update('MIDTIME',time[cadence],'[BJD] mid-time of exposure') except: hdu1.header.update('MIDTIME',-999,'[BJD] mid-time of exposure') try: hdu1.header.update('TIMECORR',timecorr[cadence],'[d] barycenter - timeslice correction') except: hdu1.header.update('TIMECORR',-999,'[d] barycenter - timeslice correction') try: hdu1.header.update('CADENCEN',cadenceno[cadence],'unique cadence number') except: hdu1.header.update('CADENCEN',-999,'unique cadence number') try: hdu1.header.update('QUALITY',quality[cadence],'pixel quality flag') except: hdu1.header.update('QUALITY',-999,'pixel quality flag') try: if True in numpy.isfinite(cosmic_rays[cadence]): hdu1.header.update('COSM_RAY',True,'cosmic ray detected?') else: hdu1.header.update('COSM_RAY',False,'cosmic ray detected?') except: hdu1.header.update('COSM_RAY',-999,'cosmic ray detected?') try: pc1 = str(pos_corr1[cadence]) pc2 = str(pos_corr2[cadence]) hdu1.header.update('POSCORR1',pc1,'[pix] column position correction') hdu1.header.update('POSCORR2',pc2,'[pix] row position correction') except: hdu1.header.update('POSCORR1',-999,'[pix] column position correction') hdu1.header.update('POSCORR2',-999,'[pix] row position correction') outstr.append(hdu1) # write output file if status == 0: outstr.writeto(outfile,checksum=True) ncad += 1 txt = '\r%3d%% ' % (float(ncad) / float(len(cadencelis)) * 100.0) txt += '%s ' % outfile sys.stdout.write(txt) sys.stdout.flush() # close input structure if status == 0: status = kepio.closefits(instr,logfile,verbose) print('\n') # end time kepmsg.clock('KEPIMAGES finished at',logfile,verbose)
def getWCSp(file,struct,logfile,verbose): status = 0 crpix1p = 0.0 crpix2p = 0.0 crval1p = 0.0 crval2p = 0.0 cdelt1p = 0.0 cdelt2p = 0.0 try: crpix1p, status = get(file,struct,'CRPIX1P',logfile,verbose) except: txt = 'WARNING -- KEPKEY.GETWCSP: Cannot read keyword CRPIX1P in file ' + file kepmsg.warn(logfile,txt) status = 1 try: crpix2p, status = get(file,struct,'CRPIX2P',logfile,verbose) except: txt = 'WARNING -- KEPKEY.GETWCSP: Cannot read keyword CRPIX2P in file ' + file kepmsg.warn(logfile,txt) status = 1 try: crval1p, status = get(file,struct,'CRVAL1P',logfile,verbose) except: txt = 'WARNING -- KEPKEY.GETWCSP: Cannot read keyword CRVAL1P in file ' + file kepmsg.warn(logfile,txt) status = 1 try: crval2p, status = get(file,struct,'CRVAL2P',logfile,verbose) except: txt = 'WARNING -- KEPKEY.GETWCSP: Cannot read keyword CRVAL2P in file ' + file kepmsg.warn(logfile,txt) status = 1 try: cdelt1p, status = get(file,struct,'CDELT1P',logfile,verbose) except: txt = 'WARNING -- KEPKEY.GETWCSP: Cannot read keyword CDELT1P in file ' + file kepmsg.warn(logfile,txt) status = 1 try: cdelt2p, status = get(file,struct,'CDELT2P',logfile,verbose) except: txt = 'WARNING -- KEPKEY.GETWCSP: Cannot read keyword CDELT2P in file ' + file kepmsg.warn(logfile,txt) status = 1 return crpix1p, crpix2p, crval1p, crval2p, cdelt1p, cdelt2p, status
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 kepfold(infile,outfile,period,phasezero,bindata,binmethod,threshold,niter,nbins, rejqual,plottype,plotlab,clobber,verbose,logfile,status,cmdLine=False): # startup parameters status = 0 labelsize = 32; ticksize = 18; xsize = 18; ysize = 10 lcolor = '#0000ff'; lwidth = 2.0; fcolor = '#ffff00'; falpha = 0.2 # log the call hashline = '----------------------------------------------------------------------------' kepmsg.log(logfile,hashline,verbose) call = 'KEPFOLD -- ' call += 'infile='+infile+' ' call += 'outfile='+outfile+' ' call += 'period='+str(period)+' ' call += 'phasezero='+str(phasezero)+' ' binit = 'n' if (bindata): binit = 'y' call += 'bindata='+binit+' ' call += 'binmethod='+binmethod+' ' call += 'threshold='+str(threshold)+' ' call += 'niter='+str(niter)+' ' call += 'nbins='+str(nbins)+' ' qflag = 'n' if (rejqual): qflag = 'y' call += 'rejqual='+qflag+ ' ' call += 'plottype='+plottype+ ' ' call += 'plotlab='+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('KEPFOLD 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 -- KEPFOLD: ' + 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 # 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 incards = instr[1].header.cards try: sap = instr[1].data.field('SAP_FLUX') except: try: sap = instr[1].data.field('ap_raw_flux') except: sap = zeros(len(table.field(0))) try: saperr = instr[1].data.field('SAP_FLUX_ERR') except: try: saperr = instr[1].data.field('ap_raw_err') except: saperr = zeros(len(table.field(0))) try: pdc = instr[1].data.field('PDCSAP_FLUX') except: try: pdc = instr[1].data.field('ap_corr_flux') except: pdc = zeros(len(table.field(0))) try: pdcerr = instr[1].data.field('PDCSAP_FLUX_ERR') except: try: pdcerr = instr[1].data.field('ap_corr_err') except: pdcerr = zeros(len(table.field(0))) try: cbv = instr[1].data.field('CBVSAP_FLUX') except: cbv = zeros(len(table.field(0))) if 'cbv' in plottype: txt = 'ERROR -- KEPFOLD: CBVSAP_FLUX column is not populated. Use kepcotrend' status = kepmsg.err(logfile,txt,verbose) try: det = instr[1].data.field('DETSAP_FLUX') except: det = zeros(len(table.field(0))) if 'det' in plottype: txt = 'ERROR -- KEPFOLD: DETSAP_FLUX column is not populated. Use kepflatten' status = kepmsg.err(logfile,txt,verbose) try: deterr = instr[1].data.field('DETSAP_FLUX_ERR') except: deterr = zeros(len(table.field(0))) if 'det' in plottype: txt = 'ERROR -- KEPFOLD: DETSAP_FLUX_ERR column is not populated. Use kepflatten' status = kepmsg.err(logfile,txt,verbose) try: quality = instr[1].data.field('SAP_QUALITY') except: quality = zeros(len(table.field(0))) if qualflag: txt = 'WARNING -- KEPFOLD: Cannot find a QUALITY data column' kepmsg.warn(logfile,txt) if status == 0: barytime, status = kepio.readtimecol(infile,table,logfile,verbose) barytime1 = copy(barytime) # filter out NaNs and quality > 0 work1 = []; work2 = []; work3 = []; work4 = []; work5 = []; work6 = []; work8 = []; work9 = [] if status == 0: if 'sap' in plottype: datacol = copy(sap) errcol = copy(saperr) if 'pdc' in plottype: datacol = copy(pdc) errcol = copy(pdcerr) if 'cbv' in plottype: datacol = copy(cbv) errcol = copy(saperr) if 'det' in plottype: datacol = copy(det) errcol = copy(deterr) for i in range(len(barytime)): if (numpy.isfinite(barytime[i]) and numpy.isfinite(datacol[i]) and datacol[i] != 0.0 and numpy.isfinite(errcol[i]) and errcol[i] > 0.0): if rejqual and quality[i] == 0: work1.append(barytime[i]) work2.append(sap[i]) work3.append(saperr[i]) work4.append(pdc[i]) work5.append(pdcerr[i]) work6.append(cbv[i]) work8.append(det[i]) work9.append(deterr[i]) elif not rejqual: work1.append(barytime[i]) work2.append(sap[i]) work3.append(saperr[i]) work4.append(pdc[i]) work5.append(pdcerr[i]) work6.append(cbv[i]) work8.append(det[i]) work9.append(deterr[i]) barytime = array(work1,dtype='float64') sap = array(work2,dtype='float32') / cadenom saperr = array(work3,dtype='float32') / cadenom pdc = array(work4,dtype='float32') / cadenom pdcerr = array(work5,dtype='float32') / cadenom cbv = array(work6,dtype='float32') / cadenom det = array(work8,dtype='float32') / cadenom deterr = array(work9,dtype='float32') / cadenom # calculate phase if status == 0: if phasezero < bjdref: phasezero += bjdref date1 = (barytime1 + bjdref - phasezero) phase1 = (date1 / period) - floor(date1/period) date2 = (barytime + bjdref - phasezero) phase2 = (date2 / period) - floor(date2/period) phase2 = array(phase2,'float32') # sort phases if status == 0: ptuple = [] phase3 = []; sap3 = []; saperr3 = [] pdc3 = []; pdcerr3 = [] cbv3 = []; cbverr3 = [] det3 = []; deterr3 = [] for i in range(len(phase2)): ptuple.append([phase2[i], sap[i], saperr[i], pdc[i], pdcerr[i], cbv[i], saperr[i], det[i], deterr[i]]) phsort = sorted(ptuple,key=lambda ph: ph[0]) for i in range(len(phsort)): phase3.append(phsort[i][0]) sap3.append(phsort[i][1]) saperr3.append(phsort[i][2]) pdc3.append(phsort[i][3]) pdcerr3.append(phsort[i][4]) cbv3.append(phsort[i][5]) cbverr3.append(phsort[i][6]) det3.append(phsort[i][7]) deterr3.append(phsort[i][8]) phase3 = array(phase3,'float32') sap3 = array(sap3,'float32') saperr3 = array(saperr3,'float32') pdc3 = array(pdc3,'float32') pdcerr3 = array(pdcerr3,'float32') cbv3 = array(cbv3,'float32') cbverr3 = array(cbverr3,'float32') det3 = array(det3,'float32') deterr3 = array(deterr3,'float32') # bin phases if status == 0 and bindata: work1 = array([sap3[0]],'float32') work2 = array([saperr3[0]],'float32') work3 = array([pdc3[0]],'float32') work4 = array([pdcerr3[0]],'float32') work5 = array([cbv3[0]],'float32') work6 = array([cbverr3[0]],'float32') work7 = array([det3[0]],'float32') work8 = array([deterr3[0]],'float32') phase4 = array([],'float32') sap4 = array([],'float32') saperr4 = array([],'float32') pdc4 = array([],'float32') pdcerr4 = array([],'float32') cbv4 = array([],'float32') cbverr4 = array([],'float32') det4 = array([],'float32') deterr4 = array([],'float32') dt = 1.0 / nbins nb = 0.0 rng = numpy.append(phase3,phase3[0]+1.0) for i in range(len(rng)): if rng[i] < nb * dt or rng[i] >= (nb + 1.0) * dt: if len(work1) > 0: phase4 = append(phase4,(nb + 0.5) * dt) if (binmethod == 'mean'): sap4 = append(sap4,kepstat.mean(work1)) saperr4 = append(saperr4,kepstat.mean_err(work2)) pdc4 = append(pdc4,kepstat.mean(work3)) pdcerr4 = append(pdcerr4,kepstat.mean_err(work4)) cbv4 = append(cbv4,kepstat.mean(work5)) cbverr4 = append(cbverr4,kepstat.mean_err(work6)) det4 = append(det4,kepstat.mean(work7)) deterr4 = append(deterr4,kepstat.mean_err(work8)) elif (binmethod == 'median'): sap4 = append(sap4,kepstat.median(work1,logfile)) saperr4 = append(saperr4,kepstat.mean_err(work2)) pdc4 = append(pdc4,kepstat.median(work3,logfile)) pdcerr4 = append(pdcerr4,kepstat.mean_err(work4)) cbv4 = append(cbv4,kepstat.median(work5,logfile)) cbverr4 = append(cbverr4,kepstat.mean_err(work6)) det4 = append(det4,kepstat.median(work7,logfile)) deterr4 = append(deterr4,kepstat.mean_err(work8)) else: coeffs, errors, covar, iiter, sigma, chi2, dof, fit, plotx, ploty, status = \ kepfit.lsqclip('poly0',[scipy.stats.nanmean(work1)],arange(0.0,float(len(work1)),1.0),work1,work2, threshold,threshold,niter,logfile,False) sap4 = append(sap4,coeffs[0]) saperr4 = append(saperr4,kepstat.mean_err(work2)) coeffs, errors, covar, iiter, sigma, chi2, dof, fit, plotx, ploty, status = \ kepfit.lsqclip('poly0',[scipy.stats.nanmean(work3)],arange(0.0,float(len(work3)),1.0),work3,work4, threshold,threshold,niter,logfile,False) pdc4 = append(pdc4,coeffs[0]) pdcerr4 = append(pdcerr4,kepstat.mean_err(work4)) coeffs, errors, covar, iiter, sigma, chi2, dof, fit, plotx, ploty, status = \ kepfit.lsqclip('poly0',[scipy.stats.nanmean(work5)],arange(0.0,float(len(work5)),1.0),work5,work6, threshold,threshold,niter,logfile,False) cbv4 = append(cbv4,coeffs[0]) cbverr4 = append(cbverr4,kepstat.mean_err(work6)) coeffs, errors, covar, iiter, sigma, chi2, dof, fit, plotx, ploty, status = \ kepfit.lsqclip('poly0',[scipy.stats.nanmean(work7)],arange(0.0,float(len(work7)),1.0),work7,work8, threshold,threshold,niter,logfile,False) det4 = append(det4,coeffs[0]) deterr4 = append(deterr4,kepstat.mean_err(work8)) work1 = array([],'float32') work2 = array([],'float32') work3 = array([],'float32') work4 = array([],'float32') work5 = array([],'float32') work6 = array([],'float32') work7 = array([],'float32') work8 = array([],'float32') nb += 1.0 else: work1 = append(work1,sap3[i]) work2 = append(work2,saperr3[i]) work3 = append(work3,pdc3[i]) work4 = append(work4,pdcerr3[i]) work5 = append(work5,cbv3[i]) work6 = append(work6,cbverr3[i]) work7 = append(work7,det3[i]) work8 = append(work8,deterr3[i]) # update HDU1 for output file if status == 0: cols = (instr[1].columns + ColDefs([Column(name='PHASE',format='E',array=phase1)])) instr[1] = pyfits.new_table(cols) instr[1].header.cards['TTYPE'+str(len(instr[1].columns))].comment = 'column title: phase' instr[1].header.cards['TFORM'+str(len(instr[1].columns))].comment = 'data type: float32' for i in range(len(incards)): if incards[i].key not in instr[1].header.keys(): instr[1].header.update(incards[i].key, incards[i].value, incards[i].comment) else: instr[1].header.cards[incards[i].key].comment = incards[i].comment instr[1].header.update('PERIOD',period,'period defining the phase [d]') instr[1].header.update('BJD0',phasezero,'time of phase zero [BJD]') # write new phased data extension for output file if status == 0 and bindata: col1 = Column(name='PHASE',format='E',array=phase4) col2 = Column(name='SAP_FLUX',format='E',unit='e/s',array=sap4/cadenom) col3 = Column(name='SAP_FLUX_ERR',format='E',unit='e/s',array=saperr4/cadenom) col4 = Column(name='PDC_FLUX',format='E',unit='e/s',array=pdc4/cadenom) col5 = Column(name='PDC_FLUX_ERR',format='E',unit='e/s',array=pdcerr4/cadenom) col6 = Column(name='CBV_FLUX',format='E',unit='e/s',array=cbv4/cadenom) col7 = Column(name='DET_FLUX',format='E',array=det4/cadenom) col8 = Column(name='DET_FLUX_ERR',format='E',array=deterr4/cadenom) cols = ColDefs([col1,col2,col3,col4,col5,col6,col7,col8]) instr.append(new_table(cols)) instr[-1].header.cards['TTYPE1'].comment = 'column title: phase' instr[-1].header.cards['TTYPE2'].comment = 'column title: simple aperture photometry' instr[-1].header.cards['TTYPE3'].comment = 'column title: SAP 1-sigma error' instr[-1].header.cards['TTYPE4'].comment = 'column title: pipeline conditioned photometry' instr[-1].header.cards['TTYPE5'].comment = 'column title: PDC 1-sigma error' instr[-1].header.cards['TTYPE6'].comment = 'column title: cotrended basis vector photometry' instr[-1].header.cards['TTYPE7'].comment = 'column title: Detrended aperture photometry' instr[-1].header.cards['TTYPE8'].comment = 'column title: DET 1-sigma error' instr[-1].header.cards['TFORM1'].comment = 'column type: float32' instr[-1].header.cards['TFORM2'].comment = 'column type: float32' instr[-1].header.cards['TFORM3'].comment = 'column type: float32' instr[-1].header.cards['TFORM4'].comment = 'column type: float32' instr[-1].header.cards['TFORM5'].comment = 'column type: float32' instr[-1].header.cards['TFORM6'].comment = 'column type: float32' instr[-1].header.cards['TFORM7'].comment = 'column type: float32' instr[-1].header.cards['TFORM8'].comment = 'column type: float32' instr[-1].header.cards['TUNIT2'].comment = 'column units: electrons per second' instr[-1].header.cards['TUNIT3'].comment = 'column units: electrons per second' instr[-1].header.cards['TUNIT4'].comment = 'column units: electrons per second' instr[-1].header.cards['TUNIT5'].comment = 'column units: electrons per second' instr[-1].header.cards['TUNIT6'].comment = 'column units: electrons per second' instr[-1].header.update('EXTNAME','FOLDED','extension name') instr[-1].header.update('PERIOD',period,'period defining the phase [d]') instr[-1].header.update('BJD0',phasezero,'time of phase zero [BJD]') instr[-1].header.update('BINMETHD',binmethod,'phase binning method') if binmethod =='sigclip': instr[-1].header.update('THRSHOLD',threshold,'sigma-clipping threshold [sigma]') instr[-1].header.update('NITER',niter,'max number of sigma-clipping iterations') # history keyword in output file if status == 0: status = kepkey.history(call,instr[0],outfile,logfile,verbose) instr.writeto(outfile) # clean up x-axis unit if status == 0: ptime1 = array([],'float32') ptime2 = array([],'float32') pout1 = array([],'float32') pout2 = array([],'float32') if bindata: work = sap4 if plottype == 'pdc': work = pdc4 if plottype == 'cbv': work = cbv4 if plottype == 'det': work = det4 for i in range(len(phase4)): if (phase4[i] > 0.5): ptime2 = append(ptime2,phase4[i] - 1.0) pout2 = append(pout2,work[i]) ptime2 = append(ptime2,phase4) pout2 = append(pout2,work) for i in range(len(phase4)): if (phase4[i] <= 0.5): ptime2 = append(ptime2,phase4[i] + 1.0) pout2 = append(pout2,work[i]) work = sap3 if plottype == 'pdc': work = pdc3 if plottype == 'cbv': work = cbv3 if plottype == 'det': work = det3 for i in range(len(phase3)): if (phase3[i] > 0.5): ptime1 = append(ptime1,phase3[i] - 1.0) pout1 = append(pout1,work[i]) ptime1 = append(ptime1,phase3) pout1 = append(pout1,work) for i in range(len(phase3)): if (phase3[i] <= 0.5): ptime1 = append(ptime1,phase3[i] + 1.0) pout1 = append(pout1,work[i]) xlab = 'Orbital Phase ($\phi$)' # clean up y-axis units if status == 0: nrm = len(str(int(pout1[isfinite(pout1)].max())))-1 pout1 = pout1 / 10**nrm pout2 = pout2 / 10**nrm if nrm == 0: ylab = plotlab else: ylab = '10$^%d$ %s' % (nrm, plotlab) # data limits xmin = ptime1.min() xmax = ptime1.max() ymin = pout1[isfinite(pout1)].min() ymax = pout1[isfinite(pout1)].max() xr = xmax - xmin yr = ymax - ymin ptime1 = insert(ptime1,[0],[ptime1[0]]) ptime1 = append(ptime1,[ptime1[-1]]) pout1 = insert(pout1,[0],[0.0]) pout1 = append(pout1,0.0) if bindata: ptime2 = insert(ptime2,[0],ptime2[0] - 1.0 / nbins) ptime2 = insert(ptime2,[0],ptime2[0]) ptime2 = append(ptime2,[ptime2[-1] + 1.0 / nbins, ptime2[-1] + 1.0 / nbins]) pout2 = insert(pout2,[0],[pout2[-1]]) pout2 = insert(pout2,[0],[0.0]) pout2 = append(pout2,[pout2[2],0.0]) # plot new light curve if status == 0 and plottype != 'none': try: params = {'backend': 'png', 'axes.linewidth': 2.5, 'axes.labelsize': labelsize, 'axes.font': 'sans-serif', 'axes.fontweight' : 'bold', 'text.fontsize': 18, 'legend.fontsize': 18, 'xtick.labelsize': ticksize, 'ytick.labelsize': ticksize} pylab.rcParams.update(params) except: print 'ERROR -- KEPFOLD: install latex for scientific plotting' status = 1 if status == 0 and plottype != 'none': pylab.figure(figsize=[17,7]) pylab.clf() ax = pylab.axes([0.06,0.11,0.93,0.86]) 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) if bindata: pylab.fill(ptime2,pout2,color=fcolor,linewidth=0.0,alpha=falpha) else: if 'det' in plottype: pylab.fill(ptime1,pout1,color=fcolor,linewidth=0.0,alpha=falpha) pylab.plot(ptime1,pout1,color=lcolor,linestyle='',linewidth=lwidth,marker='.') if bindata: pylab.plot(ptime2[1:-1],pout2[1:-1],color='r',linestyle='-',linewidth=lwidth,marker='') xlabel(xlab, {'color' : 'k'}) ylabel(ylab, {'color' : 'k'}) xlim(-0.49999,1.49999) if ymin >= 0.0: ylim(ymin-yr*0.01,ymax+yr*0.01) # ylim(0.96001,1.03999) else: ylim(1.0e-10,ymax+yr*0.01) grid() if cmdLine: pylab.show() else: pylab.ion() pylab.plot([]) pylab.ioff() # close input file if status == 0: status = kepio.closefits(instr,logfile,verbose) # stop time kepmsg.clock('KEPFOLD ended at: ',logfile,verbose)
def MASTRADec(ra, dec, darcsec, srctab, logfile): # coordinate limits darcsec /= 3600.0 ra1 = ra - darcsec / np.cos(dec * np.pi / 180) ra2 = ra + darcsec / np.cos(dec * np.pi / 180) dec1 = dec - darcsec dec2 = dec + darcsec # build mast query url = "http://archive.stsci.edu/kepler/kepler_fov/search.php?" url += "action=Search" url += "&masterRA=" + str(ra1) + ".." + str(ra2) url += "&masterDec=" + str(dec1) + ".." + str(dec2) url += "&max_records=10000" url += "&verb=3" url += "&outputformat=CSV" # retrieve results from MAST if srctab: try: lines = urllib.request.urlopen(url) except: message = "WARNING -- KEPFIELD: Cannot retrieve data from MAST" status = kepmsg.warn(logfile, message) lines = "" else: lines = "" # collate nearby sources kepid = [] kepmag = [] ra = [] dec = [] for line in lines: line = line.strip().decode("ascii") if len(line) > 0 and "Kepler" not in line and "integer" not in line and "no rows found" not in line: out = line.split(",") r, d = sex2dec(out[0], out[1]) try: if out[-22] != "Possible_artifact": kepid.append(int(out[2])) except: if out[-22] != "Possible_artifact": kepid.append(0) try: if out[-22] != "Possible_artifact": kepmag.append(float(out[42])) except: if out[-22] != "Possible_artifact": kepmag.append(0.0) if out[-22] != "Possible_artifact": ra.append(r) if out[-22] != "Possible_artifact": dec.append(d) kepid = np.array(kepid) kepmag = np.array(kepmag) ra = np.array(ra) dec = np.array(dec) return kepid, ra, dec, kepmag