Esempio n. 1
0
def reduce_lc(instr, lc_path):
    tstart, tstop, bjdref, cadence = kepio.timekeys(instr, lc_path)

    #read lc
    hdu = instr[1]
    time = hdu.data.TIME
    time = time + bjdref - 2454900
    flux = hdu.data.PDCSAP_FLUX
    #filter data
    work1 = np.array([time, flux])
    work1 = np.rot90(work1, 3)
    work1 = work1[~np.isnan(work1).any(1)]

    intime = work1[:, 1]
    indata = work1[:, 0]
    #split lc
    intime, indata = keputils.split(intime, indata, gap_width=0.75)
    #calculate breaking points
    bkspaces = np.logspace(np.log10(0.5), np.log10(20), num=20)
    #calculate spline to every data points
    spline = kepspline.choose_kepler_spline(intime,
                                            indata,
                                            bkspaces,
                                            penalty_coeff=1.0,
                                            verbose=False)[0]
    if spline is None:
        raise ValueError("faied to fit spline")

    #flatten the data array
    intime = np.concatenate(intime).ravel()
    indata = np.concatenate(indata).ravel()
    spline = np.concatenate(spline).ravel()
    #do sigma cilp later
    #normalized flux using spline
    nordata = indata / spline
    #sigma clip to remove outliers
    #nordata = sigma_clip(nordata, 3, 5)
    #mask = nordata.mask
    #intime = np.ma.masked_array(intime, mask=mask)
    #compress yo ndarray
    #intime = intime.compressed()
    #nordata = nordata.compressed()

    return intime, nordata
Esempio n. 2
0
def fetchtseries(instr, lc_path):
    tstart, tstop, bjdref, cadence = kepio.timekeys(instr, lc_path)

    #read lc
    hdu = instr[1]
    time = hdu.data.TIME
    time = time + bjdref - 2454900
    flux = hdu.data.PDCSAP_FLUX
    #filter data
    work1 = np.array([time, flux])
    work1 = np.rot90(work1, 3)
    work1 = work1[~np.isnan(work1).any(1)]

    intime = work1[:, 1]
    #indata = work1[:,0]
    #split lc
    #intime, indata = keputils.split(intime, indata, gap_width = 0.75)

    #adopt uniform distribution
    #intime = np.concatenate(intime).ravel()
    #print(intime.min, intime.max, intime.size)
    tseries = np.linspace(np.min(intime), np.max(intime), np.shape(intime)[0])

    return tseries
Esempio n. 3
0
def keppca(infile,maskfile,outfile,components,plotpca,nreps,clobber,verbose,logfile,status,cmdLine=False): 

    try:
        import mdp
    except:
        msg = 'ERROR -- KEPPCA: this task has an external python dependency to MDP, a Modular toolkit for Data Processing (http://mdp-toolkit.sourceforge.net). In order to take advantage of this PCA task, the user must first install MDP with their current python distribution. Note carefully that you may have more than python installation on your machine, and ensure that MDP is installed with the same version of python that the PyKE tools employ. Installation instructions for MDP can be found at the URL provided above.'
        status = kepmsg.err(None,msg,True)
    
# startup parameters

    status = 0
    labelsize = 32
    ticksize = 18
    xsize = 16
    ysize = 10
    lcolor = '#0000ff'
    lwidth = 1.0
    fcolor = '#ffff00'
    falpha = 0.2
    seterr(all="ignore") 

# log the call 

    if status == 0:
        hashline = '----------------------------------------------------------------------------'
        kepmsg.log(logfile,hashline,verbose)
        call = 'KEPPCA -- '
        call += 'infile='+infile+' '
        call += 'maskfile='+maskfile+' '
        call += 'outfile='+outfile+' '
        call += 'components='+components+' '
        ppca = 'n'
        if (plotpca): ppca = 'y'
        call += 'plotpca='+ppca+ ' '
        call += 'nmaps='+str(nreps)+' '
        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

    if status == 0:
        kepmsg.clock('KEPPCA started at',logfile,verbose)

# test log file

    if status == 0:
        logfile = kepmsg.test(logfile)
    
# clobber output file

    if status == 0:
        if clobber: status = kepio.clobber(outfile,logfile,verbose)
        if kepio.fileexists(outfile): 
            message = 'ERROR -- KEPPCA: ' + outfile + ' exists. Use clobber=yes'
            status = kepmsg.err(logfile,message,verbose)

# Set output file names - text file with data and plot

    if status == 0:
        dataout = copy(outfile)
        repname = re.sub('.fits','.png',outfile)

# open input file

    if status == 0:    
        instr = pyfits.open(infile,mode='readonly',memmap=True)
        tstart, tstop, bjdref, cadence, status = kepio.timekeys(instr,infile,logfile,verbose,status)

# 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, flux_bkg, status = \
            kepio.readTPF(infile,'FLUX_BKG',logfile,verbose)
    if status == 0:
        kepid, channel, skygroup, module, output, quarter, season, \
            ra, dec, column, row, kepmag, xdim, ydim, flux_bkg_err, status = \
            kepio.readTPF(infile,'FLUX_BKG_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)
    if status == 0:
        kepid, channel, skygroup, module, output, quarter, season, \
            ra, dec, column, row, kepmag, xdim, ydim, pcorr1, status = \
            kepio.readTPF(infile,'POS_CORR1',logfile,verbose)
    if status == 0:
        kepid, channel, skygroup, module, output, quarter, season, \
            ra, dec, column, row, kepmag, xdim, ydim, pcorr2, status = \
            kepio.readTPF(infile,'POS_CORR2',logfile,verbose)

# Save original data dimensions, in case of using maskfile

    if status == 0:
        xdimorig = xdim
        ydimorig = ydim
    
# read mask definition file if it has been supplied

    if status == 0 and 'aper' not in maskfile.lower() and maskfile.lower() != 'all':
        maskx = array([],'int')
        masky = array([],'int')
        lines, status = kepio.openascii(maskfile,'r',logfile,verbose)
        for line in lines:
            line = line.strip().split('|')
            if len(line) == 6:
                y0 = int(line[3])
                x0 = int(line[4])
                line = line[5].split(';')
                for items in line:
                    try:
                        masky = numpy.append(masky,y0 + int(items.split(',')[0]))
                        maskx = numpy.append(maskx,x0 + int(items.split(',')[1]))
                    except:
                        continue
        status = kepio.closeascii(lines,logfile,verbose)
        if len(maskx) == 0 or len(masky) == 0:
            message = 'ERROR -- KEPPCA: ' + maskfile + ' contains no pixels.'
            status = kepmsg.err(logfile,message,verbose)
        xdim = max(maskx) - min(maskx) + 1   # Find largest x dimension of mask
        ydim = max(masky) - min(masky) + 1   # Find largest y dimension of mask

# pad mask to ensure it is rectangular

        workx = array([],'int')
        worky = array([],'int')
        for ip in arange(min(maskx),max(maskx) + 1):
            for jp in arange(min(masky),max(masky) + 1):
                workx = append(workx,ip)
                worky = append(worky,jp)
        maskx = workx
        masky = worky

# define new subimage bitmap...

    if status == 0 and maskfile.lower() != 'all':
        aperx = numpy.array([],'int')
        apery = numpy.array([],'int')
        aperb = maskx - x0 + xdimorig * (masky - y0)   # aperb is an array that contains the pixel numbers in the mask
        npix = len(aperb)

# ...or use all pixels

    if status == 0 and maskfile.lower() == 'all':
        npix = xdimorig*ydimorig
        aperb = array([],'int')
        aperb = numpy.r_[0:npix]

# legal mask defined?

    if status == 0:
        if len(aperb) == 0:
            message = 'ERROR -- KEPPCA: no legal pixels within the subimage are defined.'
            status = kepmsg.err(logfile,message,verbose)

# Identify principal components desired

    if status == 0:
        pcaout = []
        txt = components.strip().split(',')
        for work1 in txt:
            try:
                pcaout.append(int(work1.strip()))
            except:
                work2 = work1.strip().split('-')
                try:
                    for work3 in range(int(work2[0]),int(work2[1]) + 1):
                        pcaout.append(work3)
                except:
                    message = 'ERROR -- KEPPCA: cannot understand principal component list requested'
                    status = kepmsg.err(logfile,message,verbose)
    if status == 0:
        pcaout = set(sort(pcaout))
    pcarem = array(list(pcaout))-1    # The list of pca component numbers to be removed

# Initialize arrays and variables, and apply pixel mask to the data

    if status == 0:
        ntim = 0
        time = numpy.array([],dtype='float64')
        timecorr = numpy.array([],dtype='float32')
        cadenceno = numpy.array([],dtype='int')
        pixseries = numpy.array([],dtype='float32')
        errseries = numpy.array([],dtype='float32')
        bkgseries = numpy.array([],dtype='float32')
        berseries = numpy.array([],dtype='float32')
        quality = numpy.array([],dtype='float32')
        pos_corr1 = numpy.array([],dtype='float32')
        pos_corr2 = numpy.array([],dtype='float32')
        nrows = numpy.size(fluxpixels,0)
        
# Apply the pixel mask so we are left with only the desired pixels       

    if status == 0:
        pixseriesb = fluxpixels[:,aperb]
        errseriesb = errpixels[:,aperb]
        bkgseriesb = flux_bkg[:,aperb]
        berseriesb = flux_bkg_err[:,aperb]

# Read in the data to various arrays 
   
    if status == 0:
        for i in range(nrows):
            if qual[i] < 10000 and \
                    numpy.isfinite(barytime[i]) and \
                    numpy.isfinite(fluxpixels[i,int(ydim*xdim/2+0.5)]) and \
                    numpy.isfinite(fluxpixels[i,1+int(ydim*xdim/2+0.5)]):
                ntim += 1
                time = numpy.append(time,barytime[i])
                timecorr = numpy.append(timecorr,tcorr[i])
                cadenceno = numpy.append(cadenceno,cadno[i])
                pixseries = numpy.append(pixseries,pixseriesb[i])
                errseries = numpy.append(errseries,errseriesb[i])
                bkgseries = numpy.append(bkgseries,bkgseriesb[i])
                berseries = numpy.append(berseries,berseriesb[i])
                quality = numpy.append(quality,qual[i])
                pos_corr1 = numpy.append(pos_corr1,pcorr1[i])
                pos_corr2 = numpy.append(pos_corr2,pcorr2[i])
        pixseries = numpy.reshape(pixseries,(ntim,npix))
        errseries = numpy.reshape(errseries,(ntim,npix))
        bkgseries = numpy.reshape(bkgseries,(ntim,npix))
        berseries = numpy.reshape(berseries,(ntim,npix))        
        tmp =  numpy.median(pixseries,axis=1)     
        for i in range(len(tmp)):
             pixseries[i] = pixseries[i] - tmp[i]

# Figure out which pixels are undefined/nan and remove them. Keep track for adding back in later

    if status == 0:
        nanpixels = numpy.array([],dtype='int')
        i = 0
        while (i < npix):
            if numpy.isnan(pixseries[0,i]):
                nanpixels = numpy.append(nanpixels,i)
                npix = npix - 1
            i = i + 1
        pixseries = numpy.delete(pixseries,nanpixels,1)
        errseries = numpy.delete(errseries,nanpixels,1)
        pixseries[numpy.isnan(pixseries)] = random.gauss(100,10)
        errseries[numpy.isnan(errseries)] = 10
 
# Compute statistical weights, means, standard deviations

    if status == 0:
        weightseries = (pixseries/errseries)**2
        pixMean = numpy.average(pixseries,axis=0,weights=weightseries)
        pixStd  = numpy.std(pixseries,axis=0)

# Normalize the input by subtracting the mean and divising by the standard deviation. 
# This makes it a correlation-based PCA, which is what we want.

    if status == 0:
        pixseriesnorm = (pixseries - pixMean)/pixStd

# Number of principal components to compute. Setting it equal to the number of pixels

    if status == 0:
        nvecin = npix  

# Run PCA using the MDP Whitening PCA, which produces normalized PCA components (zero mean and unit variance)
    
    if status == 0:
        pcan = mdp.nodes.WhiteningNode(svd=True)
        pcar = pcan.execute(pixseriesnorm)
        eigvec = pcan.get_recmatrix()
        model = pcar
 
# Re-insert nan columns as zeros

    if status == 0:
        for i in range(0,len(nanpixels)):
            nanpixels[i] = nanpixels[i]-i
        eigvec = numpy.insert(eigvec,nanpixels,0,1)
        pixMean = numpy.insert(pixMean,nanpixels,0,0)

#  Make output eigenvectors (correlation images) into xpix by ypix images

    if status == 0:
        eigvec = eigvec.reshape(nvecin,ydim,xdim)

# Calculate sum of all pixels to display as raw lightcurve and other quantities

    if status == 0:
        pixseriessum = sum(pixseries,axis=1)
        nrem=len(pcarem)  # Number of components to remove
        nplot = npix      # Number of pcas to plot - currently set to plot all components, but could set 
                          # nplot = nrem to just plot as many components as is being removed

# Subtract components by fitting them to the summed light curve

    if status == 0:
        x0 = numpy.tile(-1.0,1)
        for k in range(0,nrem):
            def f(x):
                fluxcor = pixseriessum
                for k in range(0,len(x)):
                    fluxcor = fluxcor - x[k]*model[:,pcarem[k]]
                return mad(fluxcor)
            if k==0:
                x0 = array([-1.0])
            else:
                x0 = numpy.append(x0,1.0)
            myfit = scipy.optimize.fmin(f,x0,maxiter=50000,maxfun=50000,disp=False)
            x0 = myfit
    
# Now that coefficients for all components have been found, subtract them to produce a calibrated time-series, 
# and then divide by the robust mean to produce a normalized time series as well

    if status == 0:
        c = myfit
        fluxcor = pixseriessum
        for k in range(0,nrem):
            fluxcor = fluxcor - c[k]*model[:,pcarem[k]]
            normfluxcor = fluxcor/mean(reject_outliers(fluxcor,2))

# input file data

    if status == 0:
        cards0 = instr[0].header.cards
        cards1 = instr[1].header.cards
        cards2 = instr[2].header.cards
        table = instr[1].data[:]
        maskmap = copy(instr[2].data)

# subimage physical WCS data

    if status == 0:
        crpix1p = cards2['CRPIX1P'].value
        crpix2p = cards2['CRPIX2P'].value
        crval1p = cards2['CRVAL1P'].value
        crval2p = cards2['CRVAL2P'].value
        cdelt1p = cards2['CDELT1P'].value
        cdelt2p = cards2['CDELT2P'].value

# dummy columns for output file

    if status == 0:
        sap_flux_err = numpy.empty(len(time)); sap_flux_err[:] = numpy.nan
        sap_bkg = numpy.empty(len(time)); sap_bkg[:] = numpy.nan
        sap_bkg_err = numpy.empty(len(time)); sap_bkg_err[:] = numpy.nan
        pdc_flux = numpy.empty(len(time)); pdc_flux[:] = numpy.nan
        pdc_flux_err = numpy.empty(len(time)); pdc_flux_err[:] = numpy.nan
        psf_centr1 = numpy.empty(len(time)); psf_centr1[:] = numpy.nan
        psf_centr1_err = numpy.empty(len(time)); psf_centr1_err[:] = numpy.nan
        psf_centr2 = numpy.empty(len(time)); psf_centr2[:] = numpy.nan
        psf_centr2_err = numpy.empty(len(time)); psf_centr2_err[:] = numpy.nan
        mom_centr1 = numpy.empty(len(time)); mom_centr1[:] = numpy.nan
        mom_centr1_err = numpy.empty(len(time)); mom_centr1_err[:] = numpy.nan
        mom_centr2 = numpy.empty(len(time)); mom_centr2[:] = numpy.nan
        mom_centr2_err = numpy.empty(len(time)); mom_centr2_err[:] = numpy.nan

# mask bitmap

    if status == 0 and 'aper' not in maskfile.lower() and maskfile.lower() != 'all':
        for i in range(maskmap.shape[0]):
            for j in range(maskmap.shape[1]):
                aperx = append(aperx,crval1p + (j + 1 - crpix1p) * cdelt1p)
                apery = append(apery,crval2p + (i + 1 - crpix2p) * cdelt2p)
                if maskmap[i,j] == 0:
                    pass
                else:
                    maskmap[i,j] = 1
                    for k in range(len(maskx)):
                        if aperx[-1] == maskx[k] and apery[-1] == masky[k]:
                            maskmap[i,j] = 3

# construct output primary extension

    if status == 0:
        hdu0 = pyfits.PrimaryHDU()
        for i in range(len(cards0)):
            if cards0[i].keyword not in hdu0.header.keys():
                hdu0.header[cards0[i].keyword] = (cards0[i].value, cards0[i].comment)
            else:
                hdu0.header.cards[cards0[i].keyword].comment = cards0[i].comment
        status = kepkey.history(call,hdu0,outfile,logfile,verbose)
        outstr = HDUList(hdu0)

# construct output light curve extension

    if status == 0:
        col1 = Column(name='TIME',format='D',unit='BJD - 2454833',array=time)
        col2 = Column(name='TIMECORR',format='E',unit='d',array=timecorr)
        col3 = Column(name='CADENCENO',format='J',array=cadenceno)
        col4 = Column(name='SAP_FLUX',format='E',unit='e-/s',array=pixseriessum)
        col5 = Column(name='SAP_FLUX_ERR',format='E',unit='e-/s',array=sap_flux_err)
        col6 = Column(name='SAP_BKG',format='E',unit='e-/s',array=sap_bkg)
        col7 = Column(name='SAP_BKG_ERR',format='E',unit='e-/s',array=sap_bkg_err)
        col8 = Column(name='PDCSAP_FLUX',format='E',unit='e-/s',array=pdc_flux)
        col9 = Column(name='PDCSAP_FLUX_ERR',format='E',unit='e-/s',array=pdc_flux_err)
        col10 = Column(name='SAP_QUALITY',format='J',array=quality)
        col11 = Column(name='PSF_CENTR1',format='E',unit='pixel',array=psf_centr1)
        col12 = Column(name='PSF_CENTR1_ERR',format='E',unit='pixel',array=psf_centr1_err)
        col13 = Column(name='PSF_CENTR2',format='E',unit='pixel',array=psf_centr2)
        col14 = Column(name='PSF_CENTR2_ERR',format='E',unit='pixel',array=psf_centr2_err)
        col15 = Column(name='MOM_CENTR1',format='E',unit='pixel',array=mom_centr1)
        col16 = Column(name='MOM_CENTR1_ERR',format='E',unit='pixel',array=mom_centr1_err)
        col17 = Column(name='MOM_CENTR2',format='E',unit='pixel',array=mom_centr2)
        col18 = Column(name='MOM_CENTR2_ERR',format='E',unit='pixel',array=mom_centr2_err)
        col19 = Column(name='POS_CORR1',format='E',unit='pixel',array=pos_corr1)
        col20 = Column(name='POS_CORR2',format='E',unit='pixel',array=pos_corr2)
        col21 = Column(name='PCA_FLUX',format='E',unit='e-/s',array=fluxcor)
        col22 = Column(name='PCA_FLUX_NRM',format='E',array=normfluxcor)
        cols = ColDefs([col1,col2,col3,col4,col5,col6,col7,col8,col9,col10,col11, \
                            col12,col13,col14,col15,col16,col17,col18,col19,col20,col21,col22])
        hdu1 = new_table(cols)
        hdu1.header['TTYPE1'] = ('TIME','column title: data time stamps')
        hdu1.header['TFORM1'] = ('D','data type: float64')
        hdu1.header['TUNIT1'] = ('BJD - 2454833','column units: barycenter corrected JD')
        hdu1.header['TDISP1'] = ('D12.7','column display format')
        hdu1.header['TTYPE2'] = ('TIMECORR','column title: barycentric-timeslice correction')
        hdu1.header['TFORM2'] = ('E','data type: float32')
        hdu1.header['TUNIT2'] = ('d','column units: days')
        hdu1.header['TTYPE3'] = ('CADENCENO','column title: unique cadence number')
        hdu1.header['TFORM3'] = ('J','column format: signed integer32')
        hdu1.header['TTYPE4'] = ('SAP_FLUX','column title: aperture photometry flux')
        hdu1.header['TFORM4'] = ('E','column format: float32')
        hdu1.header['TUNIT4'] = ('e-/s','column units: electrons per second')
        hdu1.header['TTYPE5'] = ('SAP_FLUX_ERR','column title: aperture phot. flux error')
        hdu1.header['TFORM5'] = ('E','column format: float32')
        hdu1.header['TUNIT5'] = ('e-/s','column units: electrons per second (1-sigma)')
        hdu1.header['TTYPE6'] = ('SAP_BKG','column title: aperture phot. background flux')
        hdu1.header['TFORM6'] = ('E','column format: float32')
        hdu1.header['TUNIT6'] = ('e-/s','column units: electrons per second')
        hdu1.header['TTYPE7'] = ('SAP_BKG_ERR','column title: ap. phot. background flux error')
        hdu1.header['TFORM7'] = ('E','column format: float32')
        hdu1.header['TUNIT7'] = ('e-/s','column units: electrons per second (1-sigma)')
        hdu1.header['TTYPE8'] = ('PDCSAP_FLUX','column title: PDC photometry flux')
        hdu1.header['TFORM8'] = ('E','column format: float32')
        hdu1.header['TUNIT8'] = ('e-/s','column units: electrons per second')
        hdu1.header['TTYPE9'] = ('PDCSAP_FLUX_ERR','column title: PDC flux error')
        hdu1.header['TFORM9'] = ('E','column format: float32')
        hdu1.header['TUNIT9'] = ('e-/s','column units: electrons per second (1-sigma)')
        hdu1.header['TTYPE10'] = ('SAP_QUALITY','column title: aperture photometry quality flag')
        hdu1.header['TFORM10'] = ('J','column format: signed integer32')
        hdu1.header['TTYPE11'] = ('PSF_CENTR1','column title: PSF fitted column centroid')
        hdu1.header['TFORM11'] = ('E','column format: float32')
        hdu1.header['TUNIT11'] = ('pixel','column units: pixel')
        hdu1.header['TTYPE12'] = ('PSF_CENTR1_ERR','column title: PSF fitted column error')
        hdu1.header['TFORM12'] = ('E','column format: float32')
        hdu1.header['TUNIT12'] = ('pixel','column units: pixel')
        hdu1.header['TTYPE13'] = ('PSF_CENTR2','column title: PSF fitted row centroid')
        hdu1.header['TFORM13'] = ('E','column format: float32')
        hdu1.header['TUNIT13'] = ('pixel','column units: pixel')
        hdu1.header['TTYPE14'] = ('PSF_CENTR2_ERR','column title: PSF fitted row error')
        hdu1.header['TFORM14'] = ('E','column format: float32')
        hdu1.header['TUNIT14'] = ('pixel','column units: pixel')
        hdu1.header['TTYPE15'] = ('MOM_CENTR1','column title: moment-derived column centroid')
        hdu1.header['TFORM15'] = ('E','column format: float32')
        hdu1.header['TUNIT15'] = ('pixel','column units: pixel')
        hdu1.header['TTYPE16'] = ('MOM_CENTR1_ERR','column title: moment-derived column error')
        hdu1.header['TFORM16'] = ('E','column format: float32')
        hdu1.header['TUNIT16'] = ('pixel','column units: pixel')
        hdu1.header['TTYPE17'] = ('MOM_CENTR2','column title: moment-derived row centroid')
        hdu1.header['TFORM17'] = ('E','column format: float32')
        hdu1.header['TUNIT17'] = ('pixel','column units: pixel')
        hdu1.header['TTYPE18'] = ('MOM_CENTR2_ERR','column title: moment-derived row error')
        hdu1.header['TFORM18'] = ('E','column format: float32')
        hdu1.header['TUNIT18'] = ('pixel','column units: pixel')
        hdu1.header['TTYPE19'] = ('POS_CORR1','column title: col correction for vel. abbern')
        hdu1.header['TFORM19'] = ('E','column format: float32')
        hdu1.header['TUNIT19'] = ('pixel','column units: pixel')
        hdu1.header['TTYPE20'] = ('POS_CORR2','column title: row correction for vel. abbern')
        hdu1.header['TFORM20'] = ('E','column format: float32')
        hdu1.header['TUNIT20'] = ('pixel','column units: pixel')
        hdu1.header['TTYPE21'] = ('PCA_FLUX','column title: PCA-corrected flux')
        hdu1.header['TFORM21'] = ('E','column format: float32')
        hdu1.header['TUNIT21'] = ('pixel','column units: e-/s')
        hdu1.header['TTYPE22'] = ('PCA_FLUX_NRM','column title: normalized PCA-corrected flux')
        hdu1.header['TFORM22'] = ('E','column format: float32')
        hdu1.header['EXTNAME'] = ('LIGHTCURVE','name of extension')
        for i in range(len(cards1)):
            if (cards1[i].keyword not in hdu1.header.keys() and
                cards1[i].keyword[: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']):
                hdu1.header[cards1[i].keyword] = (cards1[i].value, cards1[i].comment)
        outstr.append(hdu1)

# construct output mask bitmap extension

    if status == 0:
        hdu2 = ImageHDU(maskmap)
        for i in range(len(cards2)):
            if cards2[i].keyword not in hdu2.header.keys():
                hdu2.header[cards2[i].keyword] = (cards2[i].value, cards2[i].comment)
            else:
                hdu2.header.cards[cards2[i].keyword].comment = cards2[i].comment
        outstr.append(hdu2)

# construct principal component table

    if status == 0:
        cols = [Column(name='TIME',format='E',unit='BJD - 2454833',array=time)]
        for i in range(len(pcar[0,:])):
            colname = 'PC' + str(i + 1)
            col = Column(name=colname,format='E',array=pcar[:,i])
            cols.append(col)
        hdu3 = new_table(ColDefs(cols))
        hdu3.header['EXTNAME'] = ('PRINCIPAL_COMPONENTS','name of extension')
        hdu3.header['TTYPE1'] = ('TIME','column title: data time stamps')
        hdu3.header['TFORM1'] = ('D','data type: float64')
        hdu3.header['TUNIT1'] = ('BJD - 2454833','column units: barycenter corrected JD')
        hdu3.header['TDISP1'] = ('D12.7','column display format')
        for i in range(len(pcar[0,:])):
            hdu3.header['TTYPE' + str(i + 2)] = \
                ('PC' + str(i + 1), 'column title: principal component number' + str(i + 1))
            hdu3.header['TFORM' + str(i + 2)] = ('E','column format: float32')
        outstr.append(hdu3)

# write output file

    if status == 0:
        outstr.writeto(outfile)
    
# close input structure

    if status == 0:
        status = kepio.closefits(instr,logfile,verbose)
        
# Create PCA report 

    if status == 0 and plotpca:
        npp = 7 # Number of plots per page
        l = 1
        repcnt = 1
        for k in range(nreps):

# First plot of every pagewith flux image, flux and calibrated time series 

            status = kepplot.define(16,12,logfile,verbose)
            if (k % (npp - 1) == 0):     
                pylab.figure(figsize=[10,16])
                subplot2grid((npp,6),(0,0), colspan=2)
#                imshow(log10(pixMean.reshape(xdim,ydim).T-min(pixMean)+1),interpolation="nearest",cmap='RdYlBu')
                imshow(log10(flipud(pixMean.reshape(ydim,xdim))-min(pixMean)+1),interpolation="nearest",cmap='RdYlBu')
                xticks([])
                yticks([])
                ax1 = subplot2grid((npp,6),(0,2), colspan=4)
                px = copy(time) + bjdref
                py = copy(pixseriessum)
                px, xlab, status = kepplot.cleanx(px,logfile,verbose) 
                py, ylab, status = kepplot.cleany(py,1.0,logfile,verbose)
                kepplot.RangeOfPlot(px,py,0.01,False)
                kepplot.plot1d(px,py,cadence,lcolor,lwidth,fcolor,falpha,True)
                py = copy(fluxcor)
                py, ylab, status = kepplot.cleany(py,1.0,logfile,verbose)
                plot(px,py,marker='.',color='r',linestyle='',markersize=1.0)
                kepplot.labels('',re.sub('\)','',re.sub('Flux \(','',ylab)),'k',18)
                grid()
                setp(ax1.get_xticklabels(), visible=False)

# plot principal components

            subplot2grid((npp,6),(l,0), colspan=2)
            imshow(eigvec[k],interpolation="nearest",cmap='RdYlBu')
            xlim(-0.5,xdim-0.5)
            ylim(-0.5,ydim-0.5)
            xticks([])
            yticks([])

# The last plot on the page that should have the xlabel

            if ( k% (npp - 1) == npp - 2 or k == nvecin - 1):  
                subplot2grid((npp,6),(l,2), colspan=4)
                py = copy(model[:,k])
                kepplot.RangeOfPlot(px,py,0.01,False)
                kepplot.plot1d(px,py,cadence,'r',lwidth,'g',falpha,True)
                kepplot.labels(xlab,'PC ' + str(k+1),'k',18)
                pylab.grid()
                pylab.tight_layout()
                l = 1
                pylab.savefig(re.sub('.png','_%d.png' % repcnt,repname))
                if not cmdLine: kepplot.render(cmdLine)
                repcnt += 1

# The other plots on the page that should have no xlabel

            else:
                ax2 = subplot2grid((npp,6),(l,2), colspan=4)
                py = copy(model[:,k])
                kepplot.RangeOfPlot(px,py,0.01,False)
                kepplot.plot1d(px,py,cadence,'r',lwidth,'g',falpha,True)
                kepplot.labels('','PC ' + str(k+1),'k',18)
                grid()
                setp(ax2.get_xticklabels(), visible=False)
                pylab.tight_layout()
                l=l+1
        pylab.savefig(re.sub('.png','_%d.png' % repcnt,repname))
        if not cmdLine: kepplot.render(cmdLine)

# plot style and size

    if status == 0 and plotpca:
        status = kepplot.define(labelsize,ticksize,logfile,verbose)
        pylab.figure(figsize=[xsize,ysize])
        pylab.clf()

# plot aperture photometry and PCA corrected data

    if status == 0 and plotpca:
        ax = kepplot.location([0.06,0.54,0.93,0.43])
        px = copy(time) + bjdref
        py = copy(pixseriessum)
        px, xlab, status = kepplot.cleanx(px,logfile,verbose) 
        py, ylab, status = kepplot.cleany(py,1.0,logfile,verbose)
        kepplot.RangeOfPlot(px,py,0.01,False)
        kepplot.plot1d(px,py,cadence,lcolor,lwidth,fcolor,falpha,True)
        py = copy(fluxcor)
        py, ylab, status = kepplot.cleany(py,1.0,logfile,verbose)
        kepplot.plot1d(px,py,cadence,'r',2,fcolor,0.0,True)
        pylab.setp(pylab.gca(),xticklabels=[])
        kepplot.labels('',ylab,'k',24)
        pylab.grid()

# plot aperture photometry and PCA corrected data

    if status == 0 and plotpca:
        ax = kepplot.location([0.06,0.09,0.93,0.43])
        yr = array([],'float32')
        npc = min([6,nrem])
        for i in range(npc-1,-1,-1):
            py = pcar[:,i] * c[i]
            py, ylab, status = kepplot.cleany(py,1.0,logfile,verbose)
            cl = float(i) / (float(npc))
            kepplot.plot1d(px,py,cadence,[1.0-cl,0.0,cl],2,fcolor,0.0,True)
            yr = append(yr,py)
        y1 = max(yr)
        y2 = -min(yr)
        kepplot.RangeOfPlot(px,array([-y1,y1,-y2,y2]),0.01,False)
        kepplot.labels(xlab,'Principal Components','k',24)
        pylab.grid()

# save plot to file

    if status == 0 and plotpca:
        pylab.savefig(repname)

# render plot

    if status == 0 and plotpca:
        kepplot.render(cmdLine)

# stop time

    if status == 0:
        kepmsg.clock('KEPPCA ended at',logfile,verbose)

    return
Esempio n. 4
0
def kepextract(infile,maskfile,outfile,subback,clobber,verbose,logfile,status): 

# startup parameters

    status = 0
    seterr(all="ignore") 

# log the call 

    hashline = '----------------------------------------------------------------------------'
    kepmsg.log(logfile,hashline,verbose)
    call = 'KEPEXTRACT -- '
    call += 'infile='+infile+' '
    call += 'maskfile='+maskfile+' '
    call += 'outfile='+outfile+' '
    backgr = 'n'
    if (subback): backgr = 'y'
    call += 'background='+backgr+ ' '
    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('KEPEXTRACT 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 -- KEPEXTRACT: ' + outfile + ' exists. Use --clobber'
        status = kepmsg.err(logfile,message,verbose)

# open input file

    status = 0
    instr = pyfits.open(infile,mode='readonly',memmap=True)
    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)

# input file data

    if status == 0:
        cards0 = instr[0].header.cards
        cards1 = instr[1].header.cards
        cards2 = instr[2].header.cards
        table = instr[1].data[:]
        maskmap = copy(instr[2].data)

# input table data

    if status == 0:
        kepid, channel, skygroup, module, output, quarter, season, \
            ra, dec, column, row, kepmag, xdim, ydim, time, status = \
            kepio.readTPF(infile,'TIME',logfile,verbose)
        time = numpy.array(time,dtype='float64')
    if status == 0:
        kepid, channel, skygroup, module, output, quarter, season, \
            ra, dec, column, row, kepmag, xdim, ydim, timecorr, status = \
            kepio.readTPF(infile,'TIMECORR',logfile,verbose)
        timecorr = numpy.array(timecorr,dtype='float32')
    if status == 0:
        kepid, channel, skygroup, module, output, quarter, season, \
            ra, dec, column, row, kepmag, xdim, ydim, cadenceno, status = \
            kepio.readTPF(infile,'CADENCENO',logfile,verbose)
        cadenceno = numpy.array(cadenceno,dtype='int')
    if status == 0:
        kepid, channel, skygroup, module, output, quarter, season, \
            ra, dec, column, row, kepmag, xdim, ydim, raw_cnts, status = \
            kepio.readTPF(infile,'RAW_CNTS',logfile,verbose)
    if status == 0:
        kepid, channel, skygroup, module, output, quarter, season, \
            ra, dec, column, row, kepmag, xdim, ydim, flux, status = \
            kepio.readTPF(infile,'FLUX',logfile,verbose)
    if status == 0:
        kepid, channel, skygroup, module, output, quarter, season, \
            ra, dec, column, row, kepmag, xdim, ydim, flux_err, 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, flux_bkg, status = \
            kepio.readTPF(infile,'FLUX_BKG',logfile,verbose)
    if status == 0:
        kepid, channel, skygroup, module, output, quarter, season, \
            ra, dec, column, row, kepmag, xdim, ydim, flux_bkg_err, status = \
            kepio.readTPF(infile,'FLUX_BKG_ERR',logfile,verbose)
    if status == 0:
        kepid, channel, skygroup, module, output, quarter, season, \
            ra, dec, column, row, kepmag, xdim, ydim, cosmic_rays, status = \
            kepio.readTPF(infile,'COSMIC_RAYS',logfile,verbose)
    if status == 0:
        kepid, channel, skygroup, module, output, quarter, season, \
            ra, dec, column, row, kepmag, xdim, ydim, quality, status = \
            kepio.readTPF(infile,'QUALITY',logfile,verbose)
        quality = numpy.array(quality,dtype='int')
    if status == 0:
        try:
            pos_corr1 = numpy.array(table.field('POS_CORR1'),dtype='float64')  #  ---for FITS wave #2
        except:
            pos_corr1 = empty(len(time)); pos_corr1[:] = numpy.nan   # ---temporary before FITS wave #2
        try:
            pos_corr2 = numpy.array(table.field('POS_CORR2'),dtype='float64')  #  ---for FITS wave #2
        except:
            pos_corr2 = empty(len(time)); pos_corr2[:] = numpy.nan   # ---temporary before FITS wave #2

# dummy columns for output file

        psf_centr1 = empty(len(time)); psf_centr1[:] = numpy.nan
        psf_centr1_err = empty(len(time)); psf_centr1_err[:] = numpy.nan
        psf_centr2 = empty(len(time)); psf_centr2[:] = numpy.nan
        psf_centr2_err = empty(len(time)); psf_centr2_err[:] = numpy.nan
#        mom_centr1 = empty(len(time)); mom_centr1[:] = numpy.nan
        mom_centr1_err = empty(len(time)); mom_centr1_err[:] = numpy.nan
#        mom_centr2 = empty(len(time)); mom_centr2[:] = numpy.nan
        mom_centr2_err = empty(len(time)); mom_centr2_err[:] = numpy.nan

# read mask definition file

    if status == 0 and 'aper' not in maskfile.lower() and maskfile.lower() != 'all':
        maskx = array([],'int')
        masky = array([],'int')
        lines, status = kepio.openascii(maskfile,'r',logfile,verbose)
        for line in lines:
            line = line.strip().split('|')
            if len(line) == 6:
                y0 = int(line[3])
                x0 = int(line[4])
                line = line[5].split(';')
                for items in line:
                    try:
                        masky = append(masky,y0 + int(items.split(',')[0]))
                        maskx = append(maskx,x0 + int(items.split(',')[1]))
                    except:
                        continue
        status = kepio.closeascii(lines,logfile,verbose)
        if len(maskx) == 0 or len(masky) == 0:
            message = 'ERROR -- KEPEXTRACT: ' + maskfile + ' contains no pixels.'
            status = kepmsg.err(logfile,message,verbose)

# subimage physical WCS data

    if status == 0:
        crpix1p = cards2['CRPIX1P'].value
        crpix2p = cards2['CRPIX2P'].value
        crval1p = cards2['CRVAL1P'].value
        crval2p = cards2['CRVAL2P'].value
        cdelt1p = cards2['CDELT1P'].value
        cdelt2p = cards2['CDELT2P'].value

# define new subimage bitmap...

    if status == 0 and 'aper' not in maskfile.lower() and maskfile.lower() != 'all':
        aperx = array([],'int')
        apery = array([],'int')
        aperb = array([],'int')
        for i in range(maskmap.shape[0]):
            for j in range(maskmap.shape[1]):
                aperx = append(aperx,crval1p + (j + 1 - crpix1p) * cdelt1p)
                apery = append(apery,crval2p + (i + 1 - crpix2p) * cdelt2p)
                if maskmap[i,j] == 0:
                    aperb = append(aperb,0)
                else:
                    aperb = append(aperb,1)
                    maskmap[i,j] = 1
                    for k in range(len(maskx)):
                        if aperx[-1] == maskx[k] and apery[-1] == masky[k]:
                            aperb[-1] = 3
                            maskmap[i,j] = 3

# trap case where no aperture needs to be defined but pixel positions are still required for centroiding

    if status == 0 and maskfile.lower() == 'all':
        aperx = array([],'int')
        apery = array([],'int')
        for i in range(maskmap.shape[0]):
            for j in range(maskmap.shape[1]):
                aperx = append(aperx,crval1p + (j + 1 - crpix1p) * cdelt1p)
                apery = append(apery,crval2p + (i + 1 - crpix2p) * cdelt2p)

# ...or use old subimage bitmap

    if status == 0 and 'aper' in maskfile.lower():
        aperb = array([],'int')
        for i in range(maskmap.shape[0]):
            for j in range(maskmap.shape[1]):
                aperb = append(aperb,maskmap[i,j])

# ...or use all pixels

    if status == 0 and maskfile.lower() == 'all':
        aperb = array([],'int')
        for i in range(maskmap.shape[0]):
            for j in range(maskmap.shape[1]):
                if maskmap[i,j] == 0:
                    aperb = append(aperb,0)
                else:
                    aperb = append(aperb,3)
                    maskmap[i,j] = 3

# subtract median pixel value for background?

    if status == 0:
        sky = array([],'float32')
        for i in range(len(time)):
            sky = append(sky,median(flux[i,:]))
        if not subback:
            sky[:] = 0.0

# legal mask defined?

    if status == 0:
        if len(aperb) == 0:
            message = 'ERROR -- KEPEXTRACT: no legal pixels within the subimage are defined.'
            status = kepmsg.err(logfile,message,verbose)
        
# construct new table flux data

    if status == 0:
        naper = (aperb == 3).sum()
        ntime = len(time)
        sap_flux = array([],'float32')
        sap_flux_err = array([],'float32')
        sap_bkg = array([],'float32')
        sap_bkg_err = array([],'float32')
        raw_flux = array([],'float32')
        for i in range(len(time)):
            work1 = array([],'float64')
            work2 = array([],'float64')
            work3 = array([],'float64')
            work4 = array([],'float64')
            work5 = array([],'float64')
            for j in range(len(aperb)):
                if (aperb[j] == 3):
                    work1 = append(work1,flux[i,j]-sky[i])
                    work2 = append(work2,flux_err[i,j])
                    work3 = append(work3,flux_bkg[i,j])
                    work4 = append(work4,flux_bkg_err[i,j])
                    work5 = append(work5,raw_cnts[i,j])
            sap_flux = append(sap_flux,kepstat.sum(work1))
            sap_flux_err = append(sap_flux_err,kepstat.sumerr(work2))
            sap_bkg = append(sap_bkg,kepstat.sum(work3))
            sap_bkg_err = append(sap_bkg_err,kepstat.sumerr(work4))
            raw_flux = append(raw_flux,kepstat.sum(work5))

# construct new table moment data

    if status == 0:
        mom_centr1 = zeros(shape=(ntime))
        mom_centr2 = zeros(shape=(ntime))
        mom_centr1_err = zeros(shape=(ntime))
        mom_centr2_err = zeros(shape=(ntime))
        for i in range(ntime):
            xf = zeros(shape=(naper))
            yf = zeros(shape=(naper))
            f = zeros(shape=(naper))
            xfe = zeros(shape=(naper))
            yfe = zeros(shape=(naper))
            fe = zeros(shape=(naper))
            k = -1
            for j in range(len(aperb)):
                if (aperb[j] == 3):
                    k += 1
                    xf[k] = aperx[j] * flux[i,j]
                    xfe[k] = aperx[j] * flux_err[i,j]
                    yf[k] = apery[j] * flux[i,j]
                    yfe[k] = apery[j] * flux_err[i,j]
                    f[k] = flux[i,j]
                    fe[k] = flux_err[i,j]
            xfsum = kepstat.sum(xf)
            yfsum = kepstat.sum(yf)
            fsum = kepstat.sum(f)
            xfsume = sqrt(kepstat.sum(square(xfe)) / naper)
            yfsume = sqrt(kepstat.sum(square(yfe)) / naper)
            fsume = sqrt(kepstat.sum(square(fe)) / naper)
            mom_centr1[i] = xfsum / fsum
            mom_centr2[i] = yfsum / fsum
            mom_centr1_err[i] = sqrt((xfsume / xfsum)**2 + ((fsume / fsum)**2))
            mom_centr2_err[i] = sqrt((yfsume / yfsum)**2 + ((fsume / fsum)**2))
        mom_centr1_err = mom_centr1_err * mom_centr1
        mom_centr2_err = mom_centr2_err * mom_centr2

# construct new table PSF data

    if status == 0:
        psf_centr1 = zeros(shape=(ntime))
        psf_centr2 = zeros(shape=(ntime))
        psf_centr1_err = zeros(shape=(ntime))
        psf_centr2_err = zeros(shape=(ntime))
        modx = zeros(shape=(naper))
        mody = zeros(shape=(naper))
        k = -1
        for j in range(len(aperb)):
            if (aperb[j] == 3):
                k += 1
                modx[k] = aperx[j]
                mody[k] = apery[j]
        for i in range(ntime):
            modf = zeros(shape=(naper))
            k = -1
            guess = [mom_centr1[i], mom_centr2[i], nanmax(flux[i:]), 1.0, 1.0, 0.0, 0.0]
            for j in range(len(aperb)):
                if (aperb[j] == 3):
                    k += 1
                    modf[k] = flux[i,j]
                    args = (modx, mody, modf)
            ans = leastsq(kepfunc.PRFgauss2d,guess,args=args,xtol=1.0e-8,ftol=1.0e-4,full_output=True)
            s_sq = (ans[2]['fvec']**2).sum() / (ntime-len(guess))
            psf_centr1[i] = ans[0][0]
            psf_centr2[i] = ans[0][1]
            try:
                psf_centr1_err[i] = sqrt(diag(ans[1] * s_sq))[0]
            except:
                psf_centr1_err[i] = numpy.nan
            try:
                psf_centr2_err[i] = sqrt(diag(ans[1] * s_sq))[1]
            except:
                psf_centr2_err[i] = numpy.nan

# construct output primary extension

    if status == 0:
        hdu0 = pyfits.PrimaryHDU()
        for i in range(len(cards0)):
            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
        status = kepkey.history(call,hdu0,outfile,logfile,verbose)
        outstr = HDUList(hdu0)

# construct output light curve extension

    if status == 0:
        col1 = Column(name='TIME',format='D',unit='BJD - 2454833',array=time)
        col2 = Column(name='TIMECORR',format='E',unit='d',array=timecorr)
        col3 = Column(name='CADENCENO',format='J',array=cadenceno)
        col4 = Column(name='SAP_FLUX',format='E',array=sap_flux)
        col5 = Column(name='SAP_FLUX_ERR',format='E',array=sap_flux_err)
        col6 = Column(name='SAP_BKG',format='E',array=sap_bkg)
        col7 = Column(name='SAP_BKG_ERR',format='E',array=sap_bkg_err)
        col8 = Column(name='PDCSAP_FLUX',format='E',array=sap_flux)
        col9 = Column(name='PDCSAP_FLUX_ERR',format='E',array=sap_flux_err)
        col10 = Column(name='SAP_QUALITY',format='J',array=quality)
        col11 = Column(name='PSF_CENTR1',format='E',unit='pixel',array=psf_centr1)
        col12 = Column(name='PSF_CENTR1_ERR',format='E',unit='pixel',array=psf_centr1_err)
        col13 = Column(name='PSF_CENTR2',format='E',unit='pixel',array=psf_centr2)
        col14 = Column(name='PSF_CENTR2_ERR',format='E',unit='pixel',array=psf_centr2_err)
        col15 = Column(name='MOM_CENTR1',format='E',unit='pixel',array=mom_centr1)
        col16 = Column(name='MOM_CENTR1_ERR',format='E',unit='pixel',array=mom_centr1_err)
        col17 = Column(name='MOM_CENTR2',format='E',unit='pixel',array=mom_centr2)
        col18 = Column(name='MOM_CENTR2_ERR',format='E',unit='pixel',array=mom_centr2_err)
        col19 = Column(name='POS_CORR1',format='E',unit='pixel',array=pos_corr1)
        col20 = Column(name='POS_CORR2',format='E',unit='pixel',array=pos_corr2)
        col21 = Column(name='RAW_FLUX',format='E',array=raw_flux)
        cols = ColDefs([col1,col2,col3,col4,col5,col6,col7,col8,col9,col10,col11, \
                            col12,col13,col14,col15,col16,col17,col18,col19,col20,col21])
        hdu1 = new_table(cols)
        hdu1.header.update('TTYPE1','TIME','column title: data time stamps')
        hdu1.header.update('TFORM1','D','data type: float64')
        hdu1.header.update('TUNIT1','BJD - 2454833','column units: barycenter corrected JD')
        hdu1.header.update('TDISP1','D12.7','column display format')
        hdu1.header.update('TTYPE2','TIMECORR','column title: barycentric-timeslice correction')
        hdu1.header.update('TFORM2','E','data type: float32')
        hdu1.header.update('TUNIT2','d','column units: days')
        hdu1.header.update('TTYPE3','CADENCENO','column title: unique cadence number')
        hdu1.header.update('TFORM3','J','column format: signed integer32')
        hdu1.header.update('TTYPE4','SAP_FLUX','column title: aperture photometry flux')
        hdu1.header.update('TFORM4','E','column format: float32')
        hdu1.header.update('TUNIT4','e-/s','column units: electrons per second')
        hdu1.header.update('TTYPE5','SAP_FLUX_ERR','column title: aperture phot. flux error')
        hdu1.header.update('TFORM5','E','column format: float32')
        hdu1.header.update('TUNIT5','e-/s','column units: electrons per second (1-sigma)')
        hdu1.header.update('TTYPE6','SAP_BKG','column title: aperture phot. background flux')
        hdu1.header.update('TFORM6','E','column format: float32')
        hdu1.header.update('TUNIT6','e-/s','column units: electrons per second')
        hdu1.header.update('TTYPE7','SAP_BKG_ERR','column title: ap. phot. background flux error')
        hdu1.header.update('TFORM7','E','column format: float32')
        hdu1.header.update('TUNIT7','e-/s','column units: electrons per second (1-sigma)')
        hdu1.header.update('TTYPE8','PDCSAP_FLUX','column title: PDC photometry flux')
        hdu1.header.update('TFORM8','E','column format: float32')
        hdu1.header.update('TUNIT8','e-/s','column units: electrons per second')
        hdu1.header.update('TTYPE9','PDCSAP_FLUX_ERR','column title: PDC flux error')
        hdu1.header.update('TFORM9','E','column format: float32')
        hdu1.header.update('TUNIT9','e-/s','column units: electrons per second (1-sigma)')
        hdu1.header.update('TTYPE10','SAP_QUALITY','column title: aperture photometry quality flag')
        hdu1.header.update('TFORM10','J','column format: signed integer32')
        hdu1.header.update('TTYPE11','PSF_CENTR1','column title: PSF fitted column centroid')
        hdu1.header.update('TFORM11','E','column format: float32')
        hdu1.header.update('TUNIT11','pixel','column units: pixel')
        hdu1.header.update('TTYPE12','PSF_CENTR1_ERR','column title: PSF fitted column error')
        hdu1.header.update('TFORM12','E','column format: float32')
        hdu1.header.update('TUNIT12','pixel','column units: pixel')
        hdu1.header.update('TTYPE13','PSF_CENTR2','column title: PSF fitted row centroid')
        hdu1.header.update('TFORM13','E','column format: float32')
        hdu1.header.update('TUNIT13','pixel','column units: pixel')
        hdu1.header.update('TTYPE14','PSF_CENTR2_ERR','column title: PSF fitted row error')
        hdu1.header.update('TFORM14','E','column format: float32')
        hdu1.header.update('TUNIT14','pixel','column units: pixel')
        hdu1.header.update('TTYPE15','MOM_CENTR1','column title: moment-derived column centroid')
        hdu1.header.update('TFORM15','E','column format: float32')
        hdu1.header.update('TUNIT15','pixel','column units: pixel')
        hdu1.header.update('TTYPE16','MOM_CENTR1_ERR','column title: moment-derived column error')
        hdu1.header.update('TFORM16','E','column format: float32')
        hdu1.header.update('TUNIT16','pixel','column units: pixel')
        hdu1.header.update('TTYPE17','MOM_CENTR2','column title: moment-derived row centroid')
        hdu1.header.update('TFORM17','E','column format: float32')
        hdu1.header.update('TUNIT17','pixel','column units: pixel')
        hdu1.header.update('TTYPE18','MOM_CENTR2_ERR','column title: moment-derived row error')
        hdu1.header.update('TFORM18','E','column format: float32')
        hdu1.header.update('TUNIT18','pixel','column units: pixel')
        hdu1.header.update('TTYPE19','POS_CORR1','column title: col correction for vel. abbern')
        hdu1.header.update('TFORM19','E','column format: float32')
        hdu1.header.update('TUNIT19','pixel','column units: pixel')
        hdu1.header.update('TTYPE20','POS_CORR2','column title: row correction for vel. abbern')
        hdu1.header.update('TFORM20','E','column format: float32')
        hdu1.header.update('TUNIT20','pixel','column units: pixel')
        hdu1.header.update('TTYPE21','RAW_FLUX','column title: raw aperture photometry flux')
        hdu1.header.update('TFORM21','E','column format: float32')
        hdu1.header.update('TUNIT21','e-/s','column units: electrons per second')
        hdu1.header.update('EXTNAME','LIGHTCURVE','name of extension')
        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']):
                hdu1.header.update(cards1[i].key, cards1[i].value, cards1[i].comment)
        outstr.append(hdu1)

# construct output mask bitmap extension

    if status == 0:
        hdu2 = ImageHDU(maskmap)
        for i in range(len(cards2)):
            if cards2[i].key not in hdu2.header.keys():
                hdu2.header.update(cards2[i].key, cards2[i].value, cards2[i].comment)
            else:
                hdu2.header.cards[cards2[i].key].comment = cards2[i].comment
        outstr.append(hdu2)

# write output file

    if status == 0:
        outstr.writeto(outfile,checksum=True)

# close input structure

    if status == 0:
        status = kepio.closefits(instr,logfile,verbose)	    

# end time

    kepmsg.clock('KEPEXTRACT finished at',logfile,verbose)
Esempio n. 5
0
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)
Esempio n. 6
0
def kepsmooth(
    infile, outfile, datacol, function, fscale, plot, plotlab, clobber, verbose, logfile, status, cmdLine=False
):

    ## startup parameters

    status = 0
    labelsize = 24
    ticksize = 16
    xsize = 18
    ysize = 6
    lcolor = "#0000ff"
    lwidth = 1.0
    fcolor = "#ffff00"
    falpha = 0.2

    ## log the call

    hashline = "----------------------------------------------------------------------------"
    kepmsg.log(logfile, hashline, verbose)
    call = "KEPSMOOTH -- "
    call += "infile=" + infile + " "
    call += "outfile=" + outfile + " "
    call += "datacol=" + str(datacol) + " "
    call += "function=" + str(function) + " "
    call += "fscale=" + str(fscale) + " "
    plotit = "n"
    if plot:
        plotit = "y"
    call += "plot=" + plotit + " "
    call += "plotlab=" + str(plotlab) + " "
    overwrite = "n"
    if clobber:
        overwrite = "y"
    call += "clobber=" + overwrite + " "
    chatter = "n"
    if verbose:
        chatter = "y"
    call += "verbose=" + chatter + " "
    call += "logfile=" + logfile
    kepmsg.log(logfile, call + "\n", verbose)

    ## start time

    kepmsg.clock("KEPSMOOTH started at", logfile, verbose)

    ## test log file

    logfile = kepmsg.test(logfile)

    ## clobber output file

    if clobber:
        status = kepio.clobber(outfile, logfile, verbose)
    if kepio.fileexists(outfile):
        message = "ERROR -- KEPSMOOTH: " + outfile + " exists. Use clobber=yes"
        status = kepmsg.err(logfile, message, verbose)

    ## open input file

    if status == 0:
        instr, status = kepio.openfits(infile, "readonly", logfile, verbose)
        tstart, tstop, bjdref, cadence, status = kepio.timekeys(instr, infile, logfile, verbose, status)
        if cadence == 0.0:
            tstart, tstop, ncad, cadence, status = kepio.cadence(instr, infile, logfile, verbose, status)
    if status == 0:
        try:
            work = instr[0].header["FILEVER"]
            cadenom = 1.0
        except:
            cadenom = cadence

    ## fudge non-compliant FITS keywords with no values

    if status == 0:
        instr = kepkey.emptykeys(instr, file, logfile, verbose)

    ## read table structure

    if status == 0:
        table, status = kepio.readfitstab(infile, instr[1], logfile, verbose)

    # read time and flux columns

    if status == 0:
        barytime, status = kepio.readtimecol(infile, table, logfile, verbose)
    if status == 0:
        flux, status = kepio.readfitscol(infile, instr[1].data, datacol, logfile, verbose)

    # filter input data table

    if status == 0:
        try:
            nanclean = instr[1].header["NANCLEAN"]
        except:
            naxis2 = 0
            for i in range(len(table.field(0))):
                if numpy.isfinite(barytime[i]) and numpy.isfinite(flux[i]) and flux[i] != 0.0:
                    table[naxis2] = table[i]
                    naxis2 += 1
            instr[1].data = table[:naxis2]
            comment = "NaN cadences removed from data"
            status = kepkey.new("NANCLEAN", True, comment, instr[1], outfile, logfile, verbose)

    ## read table columns

    if status == 0:
        try:
            intime = instr[1].data.field("barytime")
        except:
            intime, status = kepio.readfitscol(infile, instr[1].data, "time", logfile, verbose)
        indata, status = kepio.readfitscol(infile, instr[1].data, datacol, logfile, verbose)
    if status == 0:
        intime = intime + bjdref
        indata = indata / cadenom

    ## smooth data

    if status == 0:
        outdata = kepfunc.smooth(indata, fscale / (cadence / 86400), function)

    ## comment keyword in output file

    if status == 0:
        status = kepkey.history(call, instr[0], outfile, logfile, verbose)

    ## clean up x-axis unit

    if status == 0:
        intime0 = float(int(tstart / 100) * 100.0)
        if intime0 < 2.4e6:
            intime0 += 2.4e6
        ptime = intime - intime0
        xlab = "BJD $-$ %d" % intime0

    ## clean up y-axis units

    if status == 0:
        pout = indata * 1.0
        pout2 = outdata * 1.0
        nrm = len(str(int(numpy.nanmax(pout)))) - 1
        pout = pout / 10 ** nrm
        pout2 = pout2 / 10 ** nrm
        ylab = "10$^%d$ %s" % (nrm, re.sub("_", "-", plotlab))

        ## data limits

        xmin = numpy.nanmin(ptime)
        xmax = numpy.nanmax(ptime)
        ymin = numpy.min(pout)
        ymax = numpy.nanmax(pout)
        xr = xmax - xmin
        yr = ymax - ymin
        ptime = insert(ptime, [0], [ptime[0]])
        ptime = append(ptime, [ptime[-1]])
        pout = insert(pout, [0], [0.0])
        pout = append(pout, 0.0)
        pout2 = insert(pout2, [0], [0.0])
        pout2 = append(pout2, 0.0)

    ## plot light curve

    if status == 0 and plot:
        try:
            params = {
                "backend": "png",
                "axes.linewidth": 2.5,
                "axes.labelsize": labelsize,
                "axes.font": "sans-serif",
                "axes.fontweight": "bold",
                "text.fontsize": 12,
                "legend.fontsize": 12,
                "xtick.labelsize": ticksize,
                "ytick.labelsize": ticksize,
            }
            rcParams.update(params)
        except:
            print "ERROR -- KEPSMOOTH: install latex for scientific plotting"
            status = 1
    if status == 0 and plot:
        pylab.figure(1, figsize=[xsize, ysize])

        # delete any fossil plots in the matplotlib window

        pylab.clf()

        # position axes inside the plotting window

        ax = pylab.subplot(111)
        pylab.subplots_adjust(0.06, 0.1, 0.93, 0.88)

        # force tick labels to be absolute rather than relative

        pylab.gca().xaxis.set_major_formatter(pylab.ScalarFormatter(useOffset=False))
        pylab.gca().yaxis.set_major_formatter(pylab.ScalarFormatter(useOffset=False))

        # rotate y labels by 90 deg

        labels = ax.get_yticklabels()
        setp(labels, "rotation", 90)

        pylab.plot(ptime[1:-1], pout[1:-1], color="#ff9900", linestyle="-", linewidth=lwidth)
        fill(ptime, pout, color=fcolor, linewidth=0.0, alpha=falpha)
        pylab.plot(ptime, pout2, color=lcolor, linestyle="-", linewidth=lwidth * 4.0)
        pylab.xlabel(xlab, {"color": "k"})
        pylab.ylabel(ylab, {"color": "k"})
        xlim(xmin - xr * 0.01, xmax + xr * 0.01)
        if ymin >= 0.0:
            ylim(ymin - yr * 0.01, ymax + yr * 0.01)
        else:
            ylim(1.0e-10, ymax + yr * 0.01)
        pylab.grid()

    # render plot

    if cmdLine:
        pylab.show()
    else:
        pylab.ion()
        pylab.plot([])
        pylab.ioff()

    ## write output file

    if status == 0:
        for i in range(len(outdata)):
            instr[1].data.field(datacol)[i] = outdata[i]
        instr.writeto(outfile)

    ## close input file

    if status == 0:
        status = kepio.closefits(instr, logfile, verbose)

    ## end time

    if status == 0:
        message = "KEPSMOOTH completed at"
    else:
        message = "\nKEPSMOOTH aborted at"
    kepmsg.clock(message, logfile, verbose)
Esempio n. 7
0
def kepsff(infile,outfile,datacol,cenmethod,stepsize,npoly_cxcy,sigma_cxcy,npoly_ardx,
           npoly_dsdt,sigma_dsdt,npoly_arfl,sigma_arfl,plotres,clobber,verbose,logfile,
           status,cmdLine=False): 

# startup parameters

    status = 0
    labelsize = 16
    ticksize = 14
    xsize = 20
    ysize = 8
    lcolor = '#0000ff'
    lwidth = 1.0
    fcolor = '#ffff00'
    falpha = 0.2
    seterr(all="ignore") 

# log the call

    hashline = '----------------------------------------------------------------------------'
    kepmsg.log(logfile,hashline,verbose)
    call = 'KEPSFF -- '
    call += 'infile='+infile+' '
    call += 'outfile='+outfile+' '
    call += 'datacol='+datacol+' '
    call += 'cenmethod='+cenmethod+' '
    call += 'stepsize='+str(stepsize)+' '
    call += 'npoly_cxcy='+str(npoly_cxcy)+' '
    call += 'sigma_cxcy='+str(sigma_cxcy)+' '
    call += 'npoly_ardx='+str(npoly_ardx)+' '
    call += 'npoly_dsdt='+str(npoly_dsdt)+' '
    call += 'sigma_dsdt='+str(sigma_dsdt)+' '
    call += 'npoly_arfl='+str(npoly_arfl)+' '
    call += 'sigma_arfl='+str(sigma_arfl)+' '
    savep = 'n'
    if (plotres): savep = 'y'
    call += 'plotres='+savep+ ' '
    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('KEPSFF 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 -- KEPSFF: ' + 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)

# determine sequence of windows in time

    if status == 0:
        frametim = instr[1].header['FRAMETIM']
        num_frm = instr[1].header['NUM_FRM']
        exptime = frametim * num_frm / 86400
        tstart = table.field('TIME')[0]
        tstop = table.field('TIME')[-1]
        winedge = arange(tstart,tstop,stepsize) 
        if tstop > winedge[-1] + stepsize / 2:
            winedge = append(winedge,tstop)
        else:
            winedge[-1] = tstop
        winedge = (winedge - tstart) / exptime
        winedge = winedge.astype(int)
        if len(table.field('TIME')) > winedge[-1] + 1:
            winedge = append(winedge,len(table.field('TIME')))
        elif len(table.field('TIME')) < winedge[-1]:
            winedge[-1] = len(table.field('TIME'))

# step through the time windows
        
    if status == 0:
        for iw in range(1,len(winedge)):
            t1 = winedge[iw-1]
            t2 = winedge[iw]

# filter input data table

            work1 = numpy.array([table.field('TIME')[t1:t2], table.field('CADENCENO')[t1:t2], 
                                 table.field(datacol)[t1:t2], 
                                 table.field('MOM_CENTR1')[t1:t2], table.field('MOM_CENTR2')[t1:t2],
                                 table.field('PSF_CENTR1')[t1:t2], table.field('PSF_CENTR2')[t1:t2],
                                 table.field('SAP_QUALITY')[t1:t2]],'float64')
            work1 = numpy.rot90(work1,3)
            work2 = work1[~numpy.isnan(work1).any(1)]            
            work2 = work2[(work2[:,0] == 0.0) | (work2[:,0] > 1e5)]

# assign table columns

            intime = work2[:,7] + bjdref
            cadenceno = work2[:,6].astype(int)
            indata = work2[:,5]
            mom_centr1 = work2[:,4]
            mom_centr2 = work2[:,3]
            psf_centr1 = work2[:,2]
            psf_centr2 = work2[:,1]
            sap_quality = work2[:,0]
            if cenmethod == 'moments':
                centr1 = copy(mom_centr1)
                centr2 = copy(mom_centr2)
            else:
                centr1 = copy(psf_centr1)
                centr2 = copy(psf_centr2)                

# fit centroid data with low-order polynomial

            cfit = zeros((len(centr2)))
            csig = zeros((len(centr2)))
            functype = 'poly' + str(npoly_cxcy)
            pinit = array([nanmean(centr2)])
            if npoly_cxcy > 0:
                for j in range(npoly_cxcy):
                    pinit = append(pinit,0.0)
            try:
                coeffs, errors, covar, iiter, sigma, chi2, dof, fit, plotx, ploty, status = \
                    kepfit.lsqclip(functype,pinit,centr1,centr2,None,sigma_cxcy,sigma_cxcy,10,logfile,verbose)
                for j in range(len(coeffs)):
                    cfit += coeffs[j] * numpy.power(centr1,j)
                    csig[:] = sigma
            except:
                message  = 'ERROR -- KEPSFF: could not fit centroid data with polynomial. There are no data points within the range of input rows %d - %d. Either increase the stepsize (with an appreciation of the effects on light curve quality this will have!), or better yet - cut the timeseries up to remove large gaps in the input light curve using kepclip.' % (t1,t2)
                status = kepmsg.err(logfile,message,verbose)
#                sys.exit('')
                os._exit(1)

# reject outliers

            time_good = array([],'float64')
            centr1_good = array([],'float32')
            centr2_good = array([],'float32')
            flux_good = array([],'float32')
            cad_good = array([],'int')
            for i in range(len(cfit)):
                if abs(centr2[i] - cfit[i]) < sigma_cxcy * csig[i]:
                    time_good = append(time_good,intime[i])
                    centr1_good = append(centr1_good,centr1[i])
                    centr2_good = append(centr2_good,centr2[i])
                    flux_good = append(flux_good,indata[i])
                    cad_good = append(cad_good,cadenceno[i])

# covariance matrix for centroid time series

            centr = concatenate([[centr1_good] - mean(centr1_good), [centr2_good] - mean(centr2_good)])
            covar = cov(centr)

# eigenvector eigenvalues of covariance matrix

            [eval, evec] = numpy.linalg.eigh(covar)
            ex = arange(-10.0,10.0,0.1)
            epar = evec[1,1] / evec[0,1] * ex
            enor = evec[1,0] / evec[0,0] * ex
            ex = ex + mean(centr1)
            epar = epar + mean(centr2_good)
            enor = enor + mean(centr2_good)

# rotate centroid data

            centr_rot = dot(evec.T,centr)

# fit polynomial to rotated centroids

            rfit = zeros((len(centr2)))
            rsig = zeros((len(centr2)))
            functype = 'poly' + str(npoly_ardx)
            pinit = array([nanmean(centr_rot[0,:])])
            pinit = array([1.0])
            if npoly_ardx > 0:
                for j in range(npoly_ardx):
                    pinit = append(pinit,0.0)
            try:
                coeffs, errors, covar, iiter, sigma, chi2, dof, fit, plotx, ploty, status = \
                    kepfit.lsqclip(functype,pinit,centr_rot[1,:],centr_rot[0,:],None,100.0,100.0,1,
                                   logfile,verbose)
            except:
                message  = 'ERROR -- KEPSFF: could not fit rotated centroid data with polynomial'
                status = kepmsg.err(logfile,message,verbose)
            rx = linspace(nanmin(centr_rot[1,:]),nanmax(centr_rot[1,:]),100)
            ry = zeros((len(rx)))
            for i in range(len(coeffs)):
                ry = ry + coeffs[i] * numpy.power(rx,i)

# calculate arclength of centroids

            s = zeros((len(rx)))
            for i in range(1,len(s)):
                work3 = ((ry[i] - ry[i-1]) / (rx[i] - rx[i-1]))**2 
                s[i] = s[i-1] + math.sqrt(1.0 + work3) * (rx[i] - rx[i-1])

# fit arclength as a function of strongest eigenvector

            sfit = zeros((len(centr2)))
            ssig = zeros((len(centr2)))
            functype = 'poly' + str(npoly_ardx)
            pinit = array([nanmean(s)])
            if npoly_ardx > 0:
                for j in range(npoly_ardx):
                    pinit = append(pinit,0.0)
            try:
                acoeffs, errors, covar, iiter, sigma, chi2, dof, fit, plotx, ploty, status = \
                    kepfit.lsqclip(functype,pinit,rx,s,None,100.0,100.0,100,logfile,verbose)
            except:
                message  = 'ERROR -- KEPSFF: could not fit rotated centroid data with polynomial'
                status = kepmsg.err(logfile,message,verbose)

# correlate arclength with detrended flux

            t = copy(time_good)
            c = copy(cad_good)
            y = copy(flux_good)
            z = centr_rot[1,:]
            x = zeros((len(z)))
            for i in range(len(acoeffs)):
                x = x + acoeffs[i] * numpy.power(z,i)

# calculate time derivative of arclength s

            dx = zeros((len(x)))
            for i in range(1,len(x)):
                dx[i] = (x[i] - x[i-1]) / (t[i] - t[i-1])
            dx[0] = dx[1]

# fit polynomial to derivative and flag outliers (thruster firings)

            dfit = zeros((len(dx)))
            dsig = zeros((len(dx)))
            functype = 'poly' + str(npoly_dsdt)
            pinit = array([nanmean(dx)])
            if npoly_dsdt > 0:
                for j in range(npoly_dsdt):
                    pinit = append(pinit,0.0)
            try:
                dcoeffs, errors, covar, iiter, dsigma, chi2, dof, fit, dumx, dumy, status = \
                    kepfit.lsqclip(functype,pinit,t,dx,None,3.0,3.0,10,logfile,verbose)
            except:
                message  = 'ERROR -- KEPSFF: could not fit rotated centroid data with polynomial'
                status = kepmsg.err(logfile,message,verbose)
            for i in range(len(dcoeffs)):
                dfit = dfit + dcoeffs[i] * numpy.power(t,i)
            centr1_pnt = array([],'float32')
            centr2_pnt = array([],'float32')
            time_pnt = array([],'float64')
            flux_pnt = array([],'float32')
            dx_pnt = array([],'float32')
            s_pnt = array([],'float32')
            time_thr = array([],'float64')
            flux_thr = array([],'float32')
            dx_thr = array([],'float32')
            thr_cadence = []
            for i in range(len(t)):
                if dx[i] < dfit[i] + sigma_dsdt * dsigma and dx[i] > dfit[i] - sigma_dsdt * dsigma:
                    time_pnt = append(time_pnt,time_good[i])
                    flux_pnt = append(flux_pnt,flux_good[i])
                    dx_pnt = append(dx_pnt,dx[i])                
                    s_pnt = append(s_pnt,x[i])                
                    centr1_pnt = append(centr1_pnt,centr1_good[i])
                    centr2_pnt = append(centr2_pnt,centr2_good[i])
                else:
                    time_thr = append(time_thr,time_good[i])
                    flux_thr = append(flux_thr,flux_good[i])                
                    dx_thr = append(dx_thr,dx[i]) 
                    thr_cadence.append(cad_good[i])

# fit arclength-flux correlation

            cfit = zeros((len(time_pnt)))
            csig = zeros((len(time_pnt)))
            functype = 'poly' + str(npoly_arfl)
            pinit = array([nanmean(flux_pnt)])
            if npoly_arfl > 0:
                for j in range(npoly_arfl):
                    pinit = append(pinit,0.0)
            try:
                ccoeffs, errors, covar, iiter, sigma, chi2, dof, fit, plx, ply, status = \
                    kepfit.lsqclip(functype,pinit,s_pnt,flux_pnt,None,sigma_arfl,sigma_arfl,100,logfile,verbose)
            except:
                message  = 'ERROR -- KEPSFF: could not fit rotated centroid data with polynomial'
                status = kepmsg.err(logfile,message,verbose)        

# correction factors for unfiltered data

            centr = concatenate([[centr1] - mean(centr1_good), [centr2] - mean(centr2_good)])
            centr_rot = dot(evec.T,centr)
            yy = copy(indata)
            zz = centr_rot[1,:]
            xx = zeros((len(zz)))
            cfac = zeros((len(zz)))
            for i in range(len(acoeffs)):
                xx = xx + acoeffs[i] * numpy.power(zz,i)
            for i in range(len(ccoeffs)):
                cfac = cfac + ccoeffs[i] * numpy.power(xx,i)

# apply correction to flux time-series

            out_detsap = indata / cfac

# split time-series data for plotting

            tim_gd = array([],'float32')
            flx_gd = array([],'float32')
            tim_bd = array([],'float32')
            flx_bd = array([],'float32')
            for i in range(len(indata)):
                if intime[i] in time_pnt:
                    tim_gd = append(tim_gd,intime[i])
                    flx_gd = append(flx_gd,out_detsap[i])
                else:
                    tim_bd = append(tim_bd,intime[i])
                    flx_bd = append(flx_bd,out_detsap[i])

# plot style and size

            status = kepplot.define(labelsize,ticksize,logfile,verbose)
            pylab.figure(figsize=[xsize,ysize])
            pylab.clf()

# plot x-centroid vs y-centroid

            ax = kepplot.location([0.04,0.57,0.16,0.41])                                      # plot location
            px = copy(centr1)                                                             # clean-up x-axis units
            py = copy(centr2)                                                             # clean-up y-axis units
            pxmin = px.min()
            pxmax = px.max()
            pymin = py.min()
            pymax = py.max()
            pxr = pxmax - pxmin
            pyr = pymax - pymin
            pad = 0.05
            if pxr > pyr:
                dely = (pxr - pyr) / 2 
                xlim(pxmin - pxr * pad, pxmax + pxr * pad)
                ylim(pymin - dely - pyr * pad, pymax + dely + pyr * pad)
            else:
                delx = (pyr - pxr) / 2 
                ylim(pymin - pyr * pad, pymax + pyr * pad)
                xlim(pxmin - delx - pxr * pad, pxmax + delx + pxr * pad)
            pylab.plot(px,py,color='#980000',markersize=5,marker='D',ls='')                   # plot data
            pylab.plot(centr1_good,centr2_good,color='#009900',markersize=5,marker='D',ls='') # plot data
            pylab.plot(ex,epar,color='k',ls='-')
            pylab.plot(ex,enor,color='k',ls='-')
            for tick in ax.xaxis.get_major_ticks(): tick.label.set_fontsize(14) 
            for tick in ax.yaxis.get_major_ticks(): tick.label.set_fontsize(14) 
            kepplot.labels('CCD Column','CCD Row','k',16)                                     # labels
            pylab.grid()                                                                      # grid lines
            
# plot arclength fits vs drift along strongest eigenvector

            ax = kepplot.location([0.24,0.57,0.16,0.41])                                      # plot location
            px = rx - rx[0]
            py = s - rx - (s[0] - rx[0])                                                      # clean-up y-axis units
            py, ylab, status = kepplot.cleany(py,1.0,logfile,verbose)                         # clean-up x-axis units
            kepplot.RangeOfPlot(px,py,0.05,False)                                             # data limits
            pylab.plot(px,py,color='#009900',markersize=5,marker='D',ls='')
            px = plotx - rx[0]                                                              # clean-up x-axis units
            py = ploty-plotx - (s[0] - rx[0])                                              # clean-up y-axis units
            py, ylab, status = kepplot.cleany(py,1.0,logfile,verbose)                         # clean-up x-axis units
            pylab.plot(px,py,color='r',ls='-',lw=3)
            for tick in ax.xaxis.get_major_ticks(): tick.label.set_fontsize(14) 
            for tick in ax.yaxis.get_major_ticks(): tick.label.set_fontsize(14) 
            ylab = re.sub(' e\S+',' pixels)',ylab)
            ylab = re.sub(' s\S+','',ylab)
            ylab = re.sub('Flux','s $-$ x\'',ylab)
            kepplot.labels('Linear Drift [x\'] (pixels)',ylab,'k',16)                               # labels
            pylab.grid()                                                                      # grid lines

# plot time derivative of arclength s

            ax = kepplot.location([0.04,0.08,0.16,0.41])                                        # plot location
            px = copy(time_pnt)
            py = copy(dx_pnt)
            px, xlab, status = kepplot.cleanx(px,logfile,verbose)       # clean-up x-axis units
            kepplot.RangeOfPlot(px,dx,0.05,False)                                             # data limits
            pylab.plot(px,py,color='#009900',markersize=5,marker='D',ls='')
            try:
                px = copy(time_thr)
                py = copy(dx_thr)
                px, xlab, status = kepplot.cleanx(px,logfile,verbose)       # clean-up x-axis units
                pylab.plot(px,py,color='#980000',markersize=5,marker='D',ls='')
            except:
                pass
            px = copy(t)
            py = copy(dfit)
            px, xlab, status = kepplot.cleanx(px,logfile,verbose)       # clean-up x-axis units
            pylab.plot(px,py,color='r',ls='-',lw=3)
            py = copy(dfit+sigma_dsdt*dsigma)
            pylab.plot(px,py,color='r',ls='--',lw=3)
            py = copy(dfit-sigma_dsdt*dsigma)
            pylab.plot(px,py,color='r',ls='--',lw=3)
            for tick in ax.xaxis.get_major_ticks(): tick.label.set_fontsize(14) 
            for tick in ax.yaxis.get_major_ticks(): tick.label.set_fontsize(14) 
            kepplot.labels(xlab,'ds/dt (pixels day$^{-1}$)','k',16)                                  # labels
            pylab.grid()                                                                      # grid lines

# plot relation of arclength vs detrended flux

            ax = kepplot.location([0.24,0.08,0.16,0.41])                                       # plot location
            px = copy(s_pnt)
            py = copy(flux_pnt)
            py, ylab, status = kepplot.cleany(py,1.0,logfile,verbose)                         # clean-up x-axis units
            kepplot.RangeOfPlot(px,py,0.05,False)                                             # data limits
            pylab.plot(px,py,color='#009900',markersize=5,marker='D',ls='')
            pylab.plot(plx,ply,color='r',ls='-',lw=3)
            for tick in ax.xaxis.get_major_ticks(): tick.label.set_fontsize(14) 
            for tick in ax.yaxis.get_major_ticks(): tick.label.set_fontsize(14) 
            kepplot.labels('Arclength [s] (pixels)',ylab,'k',16)                                  # labels
            pylab.grid()                                                                      # grid lines
            
# plot aperture photometry

            kepplot.location([0.44,0.53,0.55,0.45])                          # plot location
            px, xlab, status = kepplot.cleanx(intime,logfile,verbose)       # clean-up x-axis units
            py, ylab, status = kepplot.cleany(indata,1.0,logfile,verbose)   # clean-up x-axis units
            kepplot.RangeOfPlot(px,py,0.01,True)                                 # data limits
            kepplot.plot1d(px,py,cadence,lcolor,lwidth,fcolor,falpha,True)  # plot data
            kepplot.labels(' ',ylab,'k',16)                                   # labels
            pylab.setp(pylab.gca(),xticklabels=[])                          # remove x- or y-tick labels
            kepplot.labels(xlab,re.sub('Flux','Aperture Flux',ylab),'k',16)   # labels
            pylab.grid()                                                    # grid lines

# Plot corrected photometry

            kepplot.location([0.44,0.08,0.55,0.45])                          # plot location
            kepplot.RangeOfPlot(px,py,0.01,True)                                 # data limits
            px, xlab, status = kepplot.cleanx(tim_gd,logfile,verbose)       # clean-up x-axis units
            py, ylab, status = kepplot.cleany(flx_gd,1.0,logfile,verbose)   # clean-up x-axis units
            kepplot.plot1d(px,py,cadence,lcolor,lwidth,fcolor,falpha,True)  # plot data
            try:
                px, xlab, status = kepplot.cleanx(tim_bd,logfile,verbose)       # clean-up x-axis units
                py = copy(flx_bd)
                pylab.plot(px,py,color='#980000',markersize=5,marker='D',ls='')
            except:
                pass
            kepplot.labels(xlab,re.sub('Flux','Corrected Flux',ylab),'k',16)   # labels
            pylab.grid()                                                    # grid lines

# render plot

            if plotres:
                kepplot.render(cmdLine)

# save plot to file

            if plotres:
                pylab.savefig(re.sub('.fits','_%d.png' % (iw + 1),outfile))

# correct fluxes within the output file
                
            intime = work1[:,7] + bjdref
            cadenceno = work1[:,6].astype(int)
            indata = work1[:,5]
            mom_centr1 = work1[:,4]
            mom_centr2 = work1[:,3]
            psf_centr1 = work1[:,2]
            psf_centr2 = work1[:,1]
            centr1 = copy(mom_centr1)
            centr2 = copy(mom_centr2)
            centr = concatenate([[centr1] - mean(centr1_good), [centr2] - mean(centr2_good)])
            centr_rot = dot(evec.T,centr)
            yy = copy(indata)
            zz = centr_rot[1,:]
            xx = zeros((len(zz)))
            cfac = zeros((len(zz)))
            for i in range(len(acoeffs)):
                xx = xx + acoeffs[i] * numpy.power(zz,i)
            for i in range(len(ccoeffs)):
                cfac = cfac + ccoeffs[i] * numpy.power(xx,i)
            out_detsap = yy / cfac
            instr[1].data.field('SAP_FLUX')[t1:t2] /= cfac
            instr[1].data.field('PDCSAP_FLUX')[t1:t2] /= cfac
            try:
                instr[1].data.field('DETSAP_FLUX')[t1:t2] /= cfac
            except:
                pass

# add quality flag to output file for thruster firings

            for i in range(len(intime)):
                if cadenceno[i] in thr_cadence:
                    instr[1].data.field('SAP_QUALITY')[t1+i] += 131072

# 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 = 'KEPSFF completed at'
    else:
	    message = '\nKEPSFF aborted at'
    kepmsg.clock(message,logfile,verbose)
def kepdetrend(infile,
               outfile,
               datacol,
               errcol,
               ranges1,
               npoly1,
               nsig1,
               niter1,
               ranges2,
               npoly2,
               nsig2,
               niter2,
               popnans,
               plot,
               clobber,
               verbose,
               logfile,
               status,
               cmdLine=False):

    # startup parameters

    status = 0
    labelsize = 24
    ticksize = 16
    xsize = 16
    ysize = 9
    lcolor = '#0000ff'
    lwidth = 1.0
    fcolor = '#ffff00'
    falpha = 0.2

    # log the call

    hashline = '----------------------------------------------------------------------------'
    kepmsg.log(logfile, hashline, verbose)
    call = 'KEPDETREND -- '
    call += 'infile=' + infile + ' '
    call += 'outfile=' + outfile + ' '
    call += 'datacol=' + str(datacol) + ' '
    call += 'errcol=' + str(errcol) + ' '
    call += 'ranges1=' + str(ranges1) + ' '
    call += 'npoly1=' + str(npoly1) + ' '
    call += 'nsig1=' + str(nsig1) + ' '
    call += 'niter1=' + str(niter1) + ' '
    call += 'ranges2=' + str(ranges2) + ' '
    call += 'npoly2=' + str(npoly2) + ' '
    call += 'nsig2=' + str(nsig2) + ' '
    call += 'niter2=' + str(niter2) + ' '
    popn = 'n'
    if (popnans): popn = 'y'
    call += 'popnans=' + popn + ' '
    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('KEPDETREND 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 -- KEPDETREND: ' + outfile + ' exists. Use clobber=yes'
        status = kepmsg.err(logfile, message, verbose)

# open input file

    if status == 0:
        instr, status = kepio.openfits(infile, 'readonly', logfile, verbose)
        tstart, tstop, bjdref, cadence, status = kepio.timekeys(
            instr, infile, logfile, verbose, status)

# 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]
        print intime

# time ranges for region 1 (region to be corrected)

    if status == 0:
        time1 = []
        data1 = []
        err1 = []
        t1start, t1stop, status = kepio.timeranges(ranges1, logfile, verbose)
    if status == 0:
        cadencelis1, status = kepstat.filterOnRange(intime, t1start, t1stop)
    if status == 0:
        for i in range(len(cadencelis1)):
            time1.append(intime[cadencelis1[i]])
            data1.append(indata[cadencelis1[i]])
            if errcol.lower() != 'none':
                err1.append(inerr[cadencelis1[i]])
        t0 = time1[0]
        time1 = array(time1, dtype='float64') - t0
        data1 = array(data1, dtype='float32')
        if errcol.lower() != 'none':
            err1 = array(err1, dtype='float32')
        else:
            err1 = None

# fit function to range 1

    if status == 0:
        functype = 'poly' + str(npoly1)
        pinit = [data1.mean()]
        if npoly1 > 0:
            for i in range(npoly1):
                pinit.append(0)
        pinit = array(pinit, dtype='float32')
        coeffs, errors, covar, iiter, sigma, chi2, dof, fit, plotx1, ploty1, status = \
            kepfit.lsqclip(functype,pinit,time1,data1,err1,nsig1,nsig1,niter1,
                           logfile,verbose)
        fit1 = indata * 0.0
        for i in range(len(coeffs)):
            fit1 += coeffs[i] * (intime - t0)**i
        for i in range(len(intime)):
            if i not in cadencelis1:
                fit1[i] = 0.0
        plotx1 += t0
        print coeffs

# time ranges for region 2 (region that is correct)

    if status == 0:
        time2 = []
        data2 = []
        err2 = []
        t2start, t2stop, status = kepio.timeranges(ranges2, logfile, verbose)
        cadencelis2, status = kepstat.filterOnRange(intime, t2start, t2stop)
        for i in range(len(cadencelis2)):
            time2.append(intime[cadencelis2[i]])
            data2.append(indata[cadencelis2[i]])
            if errcol.lower() != 'none':
                err2.append(inerr[cadencelis2[i]])
        t0 = time2[0]
        time2 = array(time2, dtype='float64') - t0
        data2 = array(data2, dtype='float32')
        if errcol.lower() != 'none':
            err2 = array(err2, dtype='float32')
        else:
            err2 = None

# fit function to range 2

    if status == 0:
        functype = 'poly' + str(npoly2)
        pinit = [data2.mean()]
        if npoly2 > 0:
            for i in range(npoly2):
                pinit.append(0)
        pinit = array(pinit, dtype='float32')
        coeffs, errors, covar, iiter, sigma, chi2, dof, fit, plotx2, ploty2, status = \
            kepfit.lsqclip(functype,pinit,time2,data2,err2,nsig2,nsig2,niter2,
                           logfile,verbose)
        fit2 = indata * 0.0
        for i in range(len(coeffs)):
            fit2 += coeffs[i] * (intime - t0)**i
        for i in range(len(intime)):
            if i not in cadencelis1:
                fit2[i] = 0.0
        plotx2 += t0

# normalize data

    if status == 0:
        outdata = indata - fit1 + fit2
        if errcol.lower() != 'none':
            outerr = inerr * 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)
        if intime0 < 2.4e6: intime0 += 2.4e6
        ptime = intime - intime0
        plotx1 = plotx1 - intime0
        plotx2 = plotx2 - intime0
        xlab = 'BJD $-$ %d' % intime0

# clean up y-axis units

    if status == 0:
        pout = outdata
        ploty1
        ploty2
        nrm = len(str(int(numpy.nanmax(indata)))) - 1
        indata = indata / 10**nrm
        pout = pout / 10**nrm
        ploty1 = ploty1 / 10**nrm
        ploty2 = ploty2 / 10**nrm
        ylab = '10$^%d$ e$^-$ s$^{-1}$' % nrm

        # data limits

        xmin = ptime.min()
        xmax = ptime.max()
        ymin = indata.min()
        ymax = indata.max()
        omin = pout.min()
        omax = pout.max()
        xr = xmax - xmin
        yr = ymax - ymin
        oo = omax - omin
        ptime = insert(ptime, [0], [ptime[0]])
        ptime = append(ptime, [ptime[-1]])
        indata = insert(indata, [0], [0.0])
        indata = append(indata, [0.0])
        pout = insert(pout, [0], [0.0])
        pout = append(pout, 0.0)

# plot light curve

    if status == 0 and plot:
        try:
            params = {
                'backend': 'png',
                'axes.linewidth': 2.5,
                'axes.labelsize': labelsize,
                'axes.font': 'sans-serif',
                'axes.fontweight': 'bold',
                'text.fontsize': 12,
                'legend.fontsize': 12,
                'xtick.labelsize': ticksize,
                'ytick.labelsize': ticksize
            }
            rcParams.update(params)
        except:
            pass

        pylab.figure(figsize=[xsize, ysize])
        pylab.clf()

        # plot original data

        ax = pylab.axes([0.06, 0.523, 0.93, 0.45])

        # force tick labels to be absolute rather than relative

        pylab.gca().xaxis.set_major_formatter(
            pylab.ScalarFormatter(useOffset=False))
        pylab.gca().yaxis.set_major_formatter(
            pylab.ScalarFormatter(useOffset=False))

        # rotate y labels by 90 deg

        labels = ax.get_yticklabels()
        pylab.setp(labels, 'rotation', 90, fontsize=12)

        pylab.plot(ptime,
                   indata,
                   color=lcolor,
                   linestyle='-',
                   linewidth=lwidth)
        pylab.fill(ptime, indata, color=fcolor, linewidth=0.0, alpha=falpha)
        pylab.plot(plotx1, ploty1, color='r', linestyle='-', linewidth=2.0)
        pylab.plot(plotx2, ploty2, color='g', linestyle='-', linewidth=2.0)
        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)
            pylab.ylabel(ylab, {'color': 'k'})
        pylab.grid()

        # plot detrended data

        ax = pylab.axes([0.06, 0.073, 0.93, 0.45])

        # force tick labels to be absolute rather than relative

        pylab.gca().xaxis.set_major_formatter(
            pylab.ScalarFormatter(useOffset=False))
        pylab.gca().yaxis.set_major_formatter(
            pylab.ScalarFormatter(useOffset=False))

        # rotate y labels by 90 deg

        labels = ax.get_yticklabels()
        pylab.setp(labels, 'rotation', 90, fontsize=12)

        pylab.plot(ptime, pout, color=lcolor, linestyle='-', linewidth=lwidth)
        pylab.fill(ptime, pout, color=fcolor, linewidth=0.0, alpha=falpha)
        pylab.xlim(xmin - xr * 0.01, xmax + xr * 0.01)
        if ymin > 0.0:
            pylab.ylim(omin - oo * 0.01, omax + oo * 0.01)
        else:
            pylab.ylim(1.0e-10, omax + oo * 0.01)
        pylab.xlabel(xlab, {'color': 'k'})
        try:
            pylab.ylabel(ylab, {'color': 'k'})
        except:
            ylab = '10**%d e-/s' % nrm
            pylab.ylabel(ylab, {'color': 'k'})

# render plot

    if status == 0:
        if cmdLine:
            pylab.show()
        else:
            pylab.ion()
            pylab.plot([])
            pylab.ioff()

# write output file
    if status == 0 and popnans:
        instr[1].data.field(datacol)[good_data] = outdata
        instr[1].data.field(errcol)[good_data] = outerr
        instr[1].data.field(datacol)[bad_data] = None
        instr[1].data.field(errcol)[bad_data] = None
        instr.writeto(outfile)
    elif status == 0 and not popnans:
        for i in range(len(outdata)):
            instr[1].data.field(datacol)[i] = outdata[i]
            if errcol.lower() != 'none':
                instr[1].data.field(errcol)[i] = outerr[i]
        instr.writeto(outfile)

# close input file

    if status == 0:
        status = kepio.closefits(instr, logfile, verbose)

## end time

    if (status == 0):
        message = 'KEPDETREND completed at'
    else:
        message = '\nKEPDETREND aborted at'
    kepmsg.clock(message, logfile, verbose)
Esempio n. 9
0
def keptest(infile,outfile,datacol,ploterr,errcol,quality,
	    lcolor,lwidth,fcolor,falpha,labelsize,ticksize,
	    xsize,ysize,fullrange,plotgrid,verbose,logfile,status): 

# log the call 

    hashline = '----------------------------------------------------------------------------'
    kepmsg.log(logfile,hashline,verbose)
    call = 'KEPTEST -- '
    call += 'infile='+infile+' '
    call += 'outfile='+outfile+' '
    call += 'datacol='+datacol+' '
    perr = 'n'
    if (ploterr): perr = 'y'
    call += 'ploterr='+perr+ ' '
    call += 'errcol='+errcol+' '
    qual = 'n'
    if (quality): qual = 'y'
    call += 'quality='+qual+ ' '
    call += 'lcolor='+str(lcolor)+' '
    call += 'lwidth='+str(lwidth)+' '
    call += 'fcolor='+str(fcolor)+' '
    call += 'falpha='+str(falpha)+' '
    call += 'labelsize='+str(labelsize)+' '
    call += 'ticksize='+str(ticksize)+' '
    call += 'xsize='+str(xsize)+' '
    call += 'ysize='+str(ysize)+' '
    frange = 'n'
    if (fullrange): frange = 'y'
    call += 'fullrange='+frange+ ' '
    pgrid = 'n'
    if (plotgrid): pgrid = 'y'
    call += 'plotgrid='+pgrid+ ' '
    chatter = 'n'
    if (verbose): chatter = 'y'
    call += 'verbose='+chatter+' '
    call += 'logfile='+logfile
    kepmsg.log(logfile,call+'\n',verbose)

# start time

    kepmsg.clock('KEPTEST started at',logfile,verbose)

# test log file

    logfile = kepmsg.test(logfile)

# open input file

    if status == 0:
        struct, status = kepio.openfits(infile,'readonly',logfile,verbose)
    if status == 0:
        tstart, tstop, bjdref, cadence, status = kepio.timekeys(struct,infile,logfile,verbose,status)

# read table structure

    if status == 0:
	table, status = kepio.readfitstab(infile,struct[1],logfile,verbose)

# read table columns

    if status == 0:
        intime, status = kepio.readtimecol(infile,table,logfile,verbose)
        #intime += bjdref
	indata, status = kepio.readfitscol(infile,table,datacol,logfile,verbose)
	if (ploterr):
            indataerr, status = kepio.readfitscol(infile,table,errcol,logfile,verbose)
    if status == 0:
        gaps = zeros(len(indata))

# read table quality column

    if status == 0 and quality:
        try:
            qualtest = table.field('SAP_QUALITY')
        except:
            message = 'ERROR -- KEPTEST: no SAP_QUALITY column found in file ' + infile
            message += '. Use keptest quality=n'
            status = kepmsg.err(logfile,message,verbose)
    if status == 0 and quality:
        gaps, status = kepio.readfitscol(infile,table,'SAP_QUALITY',logfile,verbose)       

# close infile

    if status == 0:
	status = kepio.closefits(struct,logfile,verbose)

# remove infinities and bad data

    if status == 0:
	barytime = []; data = []; dataerr = []
        if 'ap_raw' in datacol or 'ap_corr' in datacol:
            cadenom = cadence
        else:
            cadenom = 1.0
	for i in range(len(intime)):
            if numpy.isfinite(indata[i]) and indata[i] != 0.0 and gaps[i] == 0:
                barytime.append(intime[i])
                data.append(indata[i] / cadenom)
                if (ploterr):
                    dataerr.append(indataerr[i])
	barytime = numpy.array(barytime,dtype='float64')
	data = numpy.array(data,dtype='float64')
	if (ploterr):
            dataerr = numpy.array(dataerr,dtype='float64')

# clean up x-axis unit

    if status == 0:
	barytime0 = float(int(tstart / 100) * 100.0)
	barytime -= barytime0
        xlab = 'BJD $-$ %d' % barytime0

# clean up y-axis units

        try:
            nrm = len(str(int(data.max())))-1
        except:
            nrm = 0
	data = data / 10**nrm
	ylab1 = '10$^%d$ e$^-$ s$^{-1}$' % nrm

# data limits

	xmin = barytime.min()
	xmax = barytime.max()
	ymin = data.min()
	ymax = data.max()
	xr = xmax - xmin
	yr = ymax - ymin
	data[0] = ymin - yr * 2.0
	data[-1] = ymin - yr * 2.0
        if fullrange:
            data[0] = 0.0
            data[-1] = 0.0

# define plot formats

        try:
            rc('text', usetex=True)
            rc('font',**{'family':'sans-serif','sans-serif':['sans-serif']})
            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}
            pylab.rcParams.update(params)
        except:
            pass

# define size of plot on monitor screen

	pylab.figure(figsize=[xsize,ysize])

# delete any fossil plots in the matplotlib window

        pylab.clf()

	# position axes inside the plotting window

	ax = pylab.axes([0.06,0.1,0.93,0.88])

# force tick labels to be absolute rather than relative

        pylab.gca().xaxis.set_major_formatter(pylab.ScalarFormatter(useOffset=False))
        pylab.gca().yaxis.set_major_formatter(pylab.ScalarFormatter(useOffset=False))

# rotate y labels by 90 deg

        labels = ax.get_yticklabels()
        setp(labels, 'rotation', 90, fontsize=12)
	
# plot data time series as an unbroken line, retaining data gaps

	ltime = []; ldata = []; ldataerr = []; ldatagaps = []
        dt = 0

	# SVR 
	svr_rbf = SVR(kernel='rbf', C=1, gamma=0.1)
	svr_lin = SVR(kernel='linear', C=1)
	svr_poly = SVR(kernel='poly', C=1, degree=2)
	svr_ltime = []; svr_ldata = []


	for i in range(len(indata)):
            if i > 0:
		if numpy.isfinite(indata[i]) and indata[i] != 0.0 : 
			# print intime[i], " ", indata[i]
			ltime.append(intime[i])
			ldata.append(indata[i])
			svr_ltime.append([intime[i]])

	ltime = array(ltime, dtype=float64)
	ldata = array(ldata, dtype=float64)

	if len(ldata) > 0 and len(ltime) > 0 :
		pylab.scatter (ltime, ldata, s=1, color=lcolor, label='Data:Input lightcurve')

	svr_ltime = array(svr_ltime, dtype='float64')
	svr_ldata = array(ldata, dtype='float64')

	svr_ldata_rbf = svr_rbf.fit(svr_ltime, svr_ldata).predict(svr_ltime)

	## Get the transits!
	# Identify the difference of data min. and the regression line
	# = An approximate initial dip value.
	
	ldata_min = min(ldata)
	ldata_min_i = ldata.tolist().index(ldata_min)
	fluxdip = svr_ldata_rbf[ldata_min_i] - ldata_min
	# fluxthresh = (svr_ldata_rbf[ldata_min_i] + ldata_min ) / 2.0
	print "ldata min = ", ldata_min, "fluxdip =", fluxdip
	thresh_x = []; thresh_y = [];

	# Sequentially scan the inputs, look for y-points below the 
	# initial mean. Group the points
	i = 0
	while i < len(ldata):
		# print intime[i], " ", indata[i]
		fluxmin = fluxthresh = svr_ldata_rbf[i] - fluxdip/2.0
		if ldata[i] < fluxthresh:
			thresh_y.append(fluxthresh); thresh_x.append(ltime[i])
		# Identify the local min, calculate difference with regression line.
			while i < len(ldata) and ldata[i] < fluxthresh :
				if ldata[i] < fluxmin:
					fluxmin = ldata[i]
					fluxmin_i = i
				i += 1
			
		# We got the local min, now plot the line,
		# converge the dip value with the newly calculated one.	
			pylab.plot([ ltime[fluxmin_i], ltime[fluxmin_i] ], 
				[ ldata[fluxmin_i], svr_ldata_rbf[fluxmin_i] ], 
				'r-', linewidth=1)
			fluxdip = (fluxdip + svr_ldata_rbf[fluxmin_i] - fluxmin)/2.0
		i += 1


	pylab.plot(thresh_x, thresh_y, c='c', label='Adapted transit threshold')
	pylab.scatter(thresh_x, thresh_y, c='k', s=1)
	pylab.plot(svr_ltime, svr_ldata_rbf, c='g', label='Cum. RBF model')


	if (ploterr):
            ldataerr = numpy.array(ldataerr,dtype='float32')


# plot labels

	pylab.xlabel(xlab, {'color' : 'k'})
        try:
            pylab.ylabel(ylab1, {'color' : 'k'})
        except:
            ylab1 = '10**%d e-/s' % nrm
            pylab.ylabel(ylab1, {'color' : 'k'})

# make grid on plot

	if plotgrid: pylab.grid()

# paint plot into window
	pylab.legend()

        pylab.draw()

# save plot to file

    if status == 0 and outfile.lower() != 'none':
	pylab.savefig(outfile)
Esempio n. 10
0
def kepdraw(infile,outfile,datacol,ploterr,errcol,quality,
	    lcolor,lwidth,fcolor,falpha,labelsize,ticksize,
	    xsize,ysize,fullrange,chooserange,y1,y2,plotgrid,
            ylabel,plottype,verbose,logfile,status,cmdLine=False): 

# log the call 

    hashline = '----------------------------------------------------------------------------'
    kepmsg.log(logfile,hashline,verbose)
    call = 'KEPDRAW -- '
    call += 'infile='+infile+' '
    call += 'outfile='+outfile+' '
    call += 'datacol='+datacol+' '
    perr = 'n'
    if (ploterr): perr = 'y'
    call += 'ploterr='+perr+ ' '
    call += 'errcol='+errcol+' '
    qual = 'n'
    if (quality): qual = 'y'
    call += 'quality='+qual+ ' '
    call += 'lcolor='+str(lcolor)+' '
    call += 'lwidth='+str(lwidth)+' '
    call += 'fcolor='+str(fcolor)+' '
    call += 'falpha='+str(falpha)+' '
    call += 'labelsize='+str(labelsize)+' '
    call += 'ticksize='+str(ticksize)+' '
    call += 'xsize='+str(xsize)+' '
    call += 'ysize='+str(ysize)+' '
    frange = 'n'
    if (fullrange): frange = 'y'
    call += 'fullrange='+frange+ ' '
    crange = 'n'
    if (chooserange): crange = 'y'
    call += 'chooserange='+crange+ ' '
    call += 'ymin='+str(y1)+' '
    call += 'ymax='+str(y2)+' '
    pgrid = 'n'
    if (plotgrid): pgrid = 'y'
    call += 'plotgrid='+pgrid+ ' '
    call += 'ylabel='+str(ylabel)+' '
    call += 'plottype='+plottype+' '
    chatter = 'n'
    if (verbose): chatter = 'y'
    call += 'verbose='+chatter+' '
    call += 'logfile='+logfile
    kepmsg.log(logfile,call+'\n',verbose)

# start time

    kepmsg.clock('KEPDRAW started at',logfile,verbose)

# test log file

    logfile = kepmsg.test(logfile)

# open input file

    if status == 0:
        struct, status = kepio.openfits(infile,'readonly',logfile,verbose)
    if status == 0:
        tstart, tstop, bjdref, cadence, status = kepio.timekeys(struct,infile,logfile,verbose,status)

# read table structure

    if status == 0:
	table, status = kepio.readfitstab(infile,struct[1],logfile,verbose)

# read table columns

    if status == 0:
        intime, status = kepio.readtimecol(infile,table,logfile,verbose)
        intime += bjdref
	indata, status = kepio.readfitscol(infile,table,datacol,logfile,verbose)
        indataerr, status = kepio.readfitscol(infile,table,errcol,logfile,verbose)
        qualty, status = kepio.readfitscol(infile,table,'SAP_QUALITY',logfile,verbose)

# close infile

    if status == 0:
	status = kepio.closefits(struct,logfile,verbose)

# remove infinities and bad data

    if status == 0:
        if numpy.isnan(numpy.nansum(indataerr)):
            indataerr[:] = 1.0e-5
        work1 = numpy.array([intime, indata, indataerr, qualty],dtype='float64')
        work1 = numpy.rot90(work1,3)
        work1 = work1[~numpy.isnan(work1).any(1)]
        work1 = work1[~numpy.isinf(work1).any(1)]
        if quality:
            work1 = work1[work1[:,0] == 0.0]
        barytime = numpy.array(work1[:,3],dtype='float64')
        data = numpy.array(work1[:,2],dtype='float32')
        dataerr = numpy.array(work1[:,1],dtype='float32')
        if len(barytime) == 0:
            message = 'ERROR -- KEPDRAW: Plotting arrays are full of NaN'
            status = kepmsg.err(logfile,message,verbose)

# clean up x-axis unit

    if status == 0:
	barytime0 = float(int(tstart / 100) * 100.0)
	barytime -= barytime0
        xlab = 'BJD $-$ %d' % barytime0

# clean up y-axis units

        nrm = 0
        try:
            nrm = len(str(int(numpy.nanmax(data))))-1
        except:
            nrm = 0
	data = data / 10**nrm
        if 'e$^-$ s$^{-1}$' in ylabel or 'default' in ylabel:
            if nrm == 0:
                ylab1 = 'e$^-$ s$^{-1}$'
            else:
                ylab1 = '10$^{%d}$ e$^-$ s$^{-1}$' % nrm
        else:
            ylab1 = re.sub('_','-',ylabel)
            

# data limits

	xmin = numpy.nanmin(barytime)
	xmax = numpy.nanmax(barytime)
	ymin = numpy.nanmin(data)
	ymax = numpy.nanmax(data)
	xr = xmax - xmin
	yr = ymax - ymin
        barytime = insert(barytime,[0],[barytime[0]]) 
        barytime = append(barytime,[barytime[-1]])
        data = insert(data,[0],[-10000.0]) 
        data = append(data,-10000.0)


# define plot formats

        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}
            pylab.rcParams.update(params)
        except:
            pass

# define size of plot on monitor screen

	pylab.figure(figsize=[xsize,ysize])

# delete any fossil plots in the matplotlib window

        pylab.clf()

# position axes inside the plotting window

#        ax = pylab.axes([0.1,0.11,0.89,0.87])
	ax = pylab.subplot(111)
	pylab.subplots_adjust(0.06,0.15,0.92,0.83)

# force tick labels to be absolute rather than relative

        pylab.gca().xaxis.set_major_formatter(pylab.ScalarFormatter(useOffset=False))
        pylab.gca().yaxis.set_major_formatter(pylab.ScalarFormatter(useOffset=False))
        ax.yaxis.set_major_locator(MaxNLocator(5))

# rotate y labels by 90 deg

        labels = ax.get_yticklabels()
        setp(labels, 'rotation', 90, fontsize=ticksize)

# if plot type is 'fast' plot data time series as points

        if plottype == 'fast':
            pylab.plot(barytime,data,'o',color=lcolor)            

# if plot type is 'pretty' plot data time series as an unbroken line, retaining data gaps

        else:
            ltime = numpy.array([],dtype='float64')
            ldata = numpy.array([],dtype='float32')
            dt = 0
            work1 = 2.0 * cadence / 86400
            for i in range(1,len(data)-1):
                dt = barytime[i] - barytime[i-1]
                if dt < work1:
                    ltime = numpy.append(ltime,barytime[i])
                    ldata = numpy.append(ldata,data[i])
                else:
                    pylab.plot(ltime,ldata,color=lcolor,linestyle='-',linewidth=lwidth)
                    ltime = numpy.array([],dtype='float64')
                    ldata = numpy.array([],dtype='float32')
            pylab.plot(ltime,ldata,color=lcolor,linestyle='-',linewidth=lwidth)

# plot the fill color below data time series, with no data gaps

	pylab.fill(barytime,data,fc=fcolor,linewidth=0.0,alpha=falpha)

# define plot x and y limits

	pylab.xlim(xmin-xr*0.01,xmax+xr*0.01)
	if ymin-yr*0.01 <= 0.0 or fullrange:
            pylab.ylim(1.0e-10,ymax+yr*0.01)
	else:
            pylab.ylim(ymin-yr*0.01,ymax+yr*0.01)
        if chooserange:
            pylab.ylim(y1,y2)

# plot labels

	pylab.xlabel(xlab, {'color' : 'k'})
        try:
            pylab.ylabel(ylab1, {'color' : 'k'})
        except:
            ylab1 = '10**%d e-/s' % nrm
            pylab.ylabel(ylab1, {'color' : 'k'})

# make grid on plot

#	if plotgrid: pylab.grid()

# TEMPORARY !!!!!!!!!!!!!!!!!!!

#        btime = numpy.arange(barytime[0],barytime[-1],0.25) + 0.125
#        bflux = numpy.zeros((len(btime)))
#        j = 0
#        work = numpy.array([])
#        for i in range(1,len(barytime)-1):
#            if barytime[i] >= btime[j] - 0.125 and barytime[i] < btime[j] + 0.125:
#                work = numpy.append(work,data[i])
#            else:
#                bflux[j] = numpy.mean(work)
#                work = numpy.array([])
#                j += 1
#        bflux[j] = numpy.mean(work)
#                
#        pylab.plot(btime,bflux,color='r',linestyle='',marker='D',markersize=20)
#	print numpy.std(bflux)        
#
#        pylab.plot([0.0,10000.0],[-49.5,-49.5],color='k',linestyle='--',linewidth=2.0)
#        pylab.plot([0.0,10000.0],[49.5,49.5],color='k',linestyle='--',linewidth=2.0)
##        pylab.plot([0.0,10000.0],[15.5,15.5],color='k',linestyle=':',linewidth=4.0)
##        pylab.plot([0.0,10000.0],[-15.5,-15.5],color='k',linestyle=':',linewidth=4.0)
##        pylab.plot([0.0,10000.0],[-202,-202],color='k',linestyle='--',linewidth=2.0)
##        pylab.plot([0.0,10000.0],[202,202],color='k',linestyle='--',linewidth=2.0)
##        pylab.plot([0.0,10000.0],[0,0],color='k',linestyle=':',linewidth=4.0)
##        pylab.plot([0.0,10000.0],[-81.*12.3,-81.*12.3],color='k',linestyle=':',linewidth=4.0)
        ax.minorticks_on()
        ax.tick_params('both', length=20, width=2, which='major')
        ax.tick_params('both', length=10, width=1, which='minor')

# save plot to file

    if status == 0 and outfile.lower() != 'none':
	pylab.savefig(outfile)

# render plot

        if cmdLine: 
            pylab.show(block=True)
        else: 
            pylab.ion()
            pylab.plot([])
            pylab.ioff()
	
# end time

    if (status == 0):
        message = 'KEPDRAW completed at'
    else:
        message = '\nKEPDRAW aborted at'
    kepmsg.clock(message,logfile,verbose)
Esempio n. 11
0
def kepbinary(infile, outfile, datacol, m1, m2, r1, r2, period, bjd0, eccn,
              omega, inclination, c1, c2, c3, c4, albedo, depth, contamination,
              gamma, fitparams, eclipses, dopboost, tides, job, clobber,
              verbose, logfile, status):

    # startup parameters

    status = 0
    labelsize = 24
    ticksize = 16
    xsize = 17
    ysize = 7
    lcolor = '#0000ff'
    lwidth = 1.0
    fcolor = '#ffff00'
    falpha = 0.2

    # log the call

    hashline = '----------------------------------------------------------------------------'
    kepmsg.log(logfile, hashline, verbose)
    call = 'KEPBINARY -- '
    call += 'infile=' + infile + ' '
    call += 'outfile=' + outfile + ' '
    call += 'datacol=' + datacol + ' '
    call += 'm1=' + str(m1) + ' '
    call += 'm2=' + str(m2) + ' '
    call += 'r1=' + str(r1) + ' '
    call += 'r2=' + str(r2) + ' '
    call += 'period=' + str(period) + ' '
    call += 'bjd0=' + str(bjd0) + ' '
    call += 'eccn=' + str(eccn) + ' '
    call += 'omega=' + str(omega) + ' '
    call += 'inclination=' + str(inclination) + ' '
    call += 'c1=' + str(c1) + ' '
    call += 'c2=' + str(c2) + ' '
    call += 'c3=' + str(c3) + ' '
    call += 'c4=' + str(c4) + ' '
    call += 'albedo=' + str(albedo) + ' '
    call += 'depth=' + str(depth) + ' '
    call += 'contamination=' + str(contamination) + ' '
    call += 'gamma=' + str(gamma) + ' '
    call += 'fitparams=' + str(fitparams) + ' '
    eclp = 'n'
    if (eclipses): eclp = 'y'
    call += 'eclipses=' + eclp + ' '
    boost = 'n'
    if (dopboost): boost = 'y'
    call += 'dopboost=' + boost + ' '
    distort = 'n'
    if (tides): distort = 'y'
    call += 'tides=' + distort + ' '
    call += 'job=' + str(job) + ' '
    overwrite = 'n'
    if (clobber): overwrite = 'y'
    call += 'clobber=' + overwrite + ' '
    chatter = 'n'
    if (verbose): chatter = 'y'
    call += 'verbose=' + chatter + ' '
    call += 'logfile=' + logfile
    kepmsg.log(logfile, call + '\n', verbose)

    # start time

    kepmsg.clock('KEPBINARY started at', logfile, verbose)

    # test log file

    logfile = kepmsg.test(logfile)

    # check and format the list of fit parameters

    if status == 0 and job == 'fit':
        allParams = [m1, m2, r1, r2, period, bjd0, eccn, omega, inclination]
        allNames = [
            'm1', 'm2', 'r1', 'r2', 'period', 'bjd0', 'eccn', 'omega',
            'inclination'
        ]
        fitparams = re.sub('\|', ',', fitparams.strip())
        fitparams = re.sub('\.', ',', fitparams.strip())
        fitparams = re.sub(';', ',', fitparams.strip())
        fitparams = re.sub(':', ',', fitparams.strip())
        fitparams = re.sub('\s+', ',', fitparams.strip())
        fitparams, status = kepio.parselist(fitparams, logfile, verbose)
        for fitparam in fitparams:
            if fitparam.strip() not in allNames:
                message = 'ERROR -- KEPBINARY: unknown field in list of fit parameters'
                status = kepmsg.err(logfile, message, verbose)

# clobber output file

    if status == 0:
        if clobber: status = kepio.clobber(outfile, logfile, verbose)
        if kepio.fileexists(outfile):
            message = 'ERROR -- KEPBINARY: ' + outfile + ' exists. Use --clobber'
            status = kepmsg.err(logfile, message, verbose)

# open input file

    if status == 0:
        instr, status = kepio.openfits(infile, 'readonly', logfile, verbose)
    if status == 0:
        tstart, tstop, bjdref, cadence, status = kepio.timekeys(
            instr, infile, logfile, verbose, status)
    if status == 0:
        try:
            work = instr[0].header['FILEVER']
            cadenom = 1.0
        except:
            cadenom = cadence

# check the data column exists

    if status == 0:
        try:
            instr[1].data.field(datacol)
        except:
            message = 'ERROR -- KEPBINARY: ' + datacol + ' column does not exist in ' + infile + '[1]'
            status = kepmsg.err(logfile, message, verbose)

# fudge non-compliant FITS keywords with no values

    if status == 0:
        instr = kepkey.emptykeys(instr, file, logfile, verbose)

# read table structure

    if status == 0:
        table, status = kepio.readfitstab(infile, instr[1], logfile, verbose)

# filter input data table

    if status == 0:
        try:
            nanclean = instr[1].header['NANCLEAN']
        except:
            naxis2 = 0
            try:
                for i in range(len(table.field(0))):
                    if numpy.isfinite(table.field('barytime')[i]) and \
                            numpy.isfinite(table.field(datacol)[i]):
                        table[naxis2] = table[i]
                        naxis2 += 1
                        instr[1].data = table[:naxis2]
            except:
                for i in range(len(table.field(0))):
                    if numpy.isfinite(table.field('time')[i]) and \
                            numpy.isfinite(table.field(datacol)[i]):
                        table[naxis2] = table[i]
                        naxis2 += 1
                        instr[1].data = table[:naxis2]
            comment = 'NaN cadences removed from data'
            status = kepkey.new('NANCLEAN', True, comment, instr[1], outfile,
                                logfile, verbose)

# read table columns

    if status == 0:
        try:
            time = instr[1].data.field('barytime')
        except:
            time, status = kepio.readfitscol(infile, instr[1].data, 'time',
                                             logfile, verbose)
        indata, status = kepio.readfitscol(infile, instr[1].data, datacol,
                                           logfile, verbose)
    if status == 0:
        time = time + bjdref
        indata = indata / cadenom

# limb-darkening cofficients

    if status == 0:
        limbdark = numpy.array([c1, c2, c3, c4], dtype='float32')

# time details for model

    if status == 0:
        npt = len(time)
        exptime = numpy.zeros((npt), dtype='float64')
        dtype = numpy.zeros((npt), dtype='int')
        for i in range(npt):
            try:
                exptime[i] = time[i + 1] - time[i]
            except:
                exptime[i] = time[i] - time[i - 1]

# calculate binary model

    if status == 0:
        tmodel = kepsim.transitModel(1.0, m1, m2, r1, r2, period, inclination,
                                     bjd0, eccn, omega, depth, albedo, c1, c2,
                                     c3, c4, gamma, contamination, npt, time,
                                     exptime, dtype, eclipses, dopboost, tides)

# re-normalize binary model to data

    if status == 0 and (job == 'overlay' or job == 'fit'):
        dmedian = numpy.median(indata)
        tmodel = tmodel / numpy.median(tmodel) * dmedian

# define arrays of floating and frozen parameters

    if status == 0 and job == 'fit':
        params = []
        paramNames = []
        arguments = []
        argNames = []
        for i in range(len(allNames)):
            if allNames[i] in fitparams:
                params.append(allParams[i])
                paramNames.append(allNames[i])
            else:
                arguments.append(allParams[i])
                argNames.append(allNames[i])
        params.append(dmedian)
        params = numpy.array(params, dtype='float32')

# subtract model from data

    if status == 0 and job == 'fit':
        deltam = numpy.abs(indata - tmodel)

# fit statistics

    if status == 0 and job == 'fit':
        aveDelta = numpy.sum(deltam) / npt
        chi2 = math.sqrt(
            numpy.sum(
                (indata - tmodel) * (indata - tmodel) / (npt - len(params))))

# fit model to data using downhill simplex

    if status == 0 and job == 'fit':
        print ''
        print '%4s %11s %11s' % ('iter', 'delta', 'chi^2')
        print '----------------------------'
        print '%4d %.5E %.5E' % (0, aveDelta, chi2)
        bestFit = scipy.optimize.fmin(
            fitModel,
            params,
            args=(paramNames, dmedian, m1, m2, r1, r2, period, bjd0, eccn,
                  omega, inclination, depth, albedo, c1, c2, c3, c4, gamma,
                  contamination, npt, time, exptime, indata, dtype, eclipses,
                  dopboost, tides),
            maxiter=1e4)

# calculate best fit binary model

    if status == 0 and job == 'fit':
        print ''
        for i in range(len(paramNames)):
            if 'm1' in paramNames[i].lower():
                m1 = bestFit[i]
                print '  M1 = %.3f Msun' % bestFit[i]
            elif 'm2' in paramNames[i].lower():
                m2 = bestFit[i]
                print '  M2 = %.3f Msun' % bestFit[i]
            elif 'r1' in paramNames[i].lower():
                r1 = bestFit[i]
                print '  R1 = %.4f Rsun' % bestFit[i]
            elif 'r2' in paramNames[i].lower():
                r2 = bestFit[i]
                print '  R2 = %.4f Rsun' % bestFit[i]
            elif 'period' in paramNames[i].lower():
                period = bestFit[i]
            elif 'bjd0' in paramNames[i].lower():
                bjd0 = bestFit[i]
                print 'BJD0 = %.8f' % bestFit[i]
            elif 'eccn' in paramNames[i].lower():
                eccn = bestFit[i]
                print '   e = %.3f' % bestFit[i]
            elif 'omega' in paramNames[i].lower():
                omega = bestFit[i]
                print '   w = %.3f deg' % bestFit[i]
            elif 'inclination' in paramNames[i].lower():
                inclination = bestFit[i]
                print '   i = %.3f deg' % bestFit[i]
        flux = bestFit[-1]
        print ''
        tmodel = kepsim.transitModel(flux, m1, m2, r1, r2, period, inclination,
                                     bjd0, eccn, omega, depth, albedo, c1, c2,
                                     c3, c4, gamma, contamination, npt, time,
                                     exptime, dtype, eclipses, dopboost, tides)

# subtract model from data

    if status == 0:
        deltaMod = indata - tmodel

# standard deviation of model

    if status == 0:
        stdDev = math.sqrt(
            numpy.sum((indata - tmodel) * (indata - tmodel)) / npt)

# clean up x-axis unit

    if status == 0:
        time0 = float(int(tstart / 100) * 100.0)
        ptime = time - time0
        xlab = 'BJD $-$ %d' % time0

# clean up y-axis units

    if status == 0:
        nrm = len(str(int(indata.max()))) - 1
        pout = indata / 10**nrm
        pmod = tmodel / 10**nrm
        pres = deltaMod / stdDev
        if job == 'fit' or job == 'overlay':
            try:
                ylab1 = 'Flux (10$^%d$ e$^-$ s$^{-1}$)' % nrm
                ylab2 = 'Residual ($\sigma$)'
            except:
                ylab1 = 'Flux (10**%d e-/s)' % nrm
                ylab2 = 'Residual (sigma)'
        else:
            ylab1 = 'Normalized Flux'

# dynamic range of model plot

    if status == 0 and job == 'model':
        xmin = ptime.min()
        xmax = ptime.max()
        ymin = tmodel.min()
        ymax = tmodel.max()

# dynamic range of model/data overlay or fit

    if status == 0 and (job == 'overlay' or job == 'fit'):
        xmin = ptime.min()
        xmax = ptime.max()
        ymin = pout.min()
        ymax = pout.max()
        tmin = pmod.min()
        tmax = pmod.max()
        ymin = numpy.array([ymin, tmin]).min()
        ymax = numpy.array([ymax, tmax]).max()
        rmin = pres.min()
        rmax = pres.max()

# pad the dynamic range

    if status == 0:
        xr = (xmax - xmin) / 80
        yr = (ymax - ymin) / 40
        if job == 'overlay' or job == 'fit':
            rr = (rmax - rmin) / 40

# set up plot style

    if status == 0:
        labelsize = 24
        ticksize = 16
        xsize = 17
        ysize = 7
        lcolor = '#0000ff'
        lwidth = 1.0
        fcolor = '#ffff00'
        falpha = 0.2
        params = {
            'backend': 'png',
            'axes.linewidth': 2.5,
            'axes.labelsize': 24,
            'axes.font': 'sans-serif',
            'axes.fontweight': 'bold',
            'text.fontsize': 12,
            'legend.fontsize': 12,
            'xtick.labelsize': 16,
            'ytick.labelsize': 16
        }
        pylab.rcParams.update(params)
        pylab.figure(figsize=[14, 10])
        pylab.clf()

        # main plot window

        ax = pylab.axes([0.05, 0.3, 0.94, 0.68])
        pylab.gca().xaxis.set_major_formatter(
            pylab.ScalarFormatter(useOffset=False))
        pylab.gca().yaxis.set_major_formatter(
            pylab.ScalarFormatter(useOffset=False))
        labels = ax.get_yticklabels()
        setp(labels, 'rotation', 90, fontsize=12)

# plot model time series

    if status == 0 and job == 'model':
        pylab.plot(ptime,
                   tmodel,
                   color='#0000ff',
                   linestyle='-',
                   linewidth=1.0)
        ptime = numpy.insert(ptime, [0.0], ptime[0])
        ptime = numpy.append(ptime, ptime[-1])
        tmodel = numpy.insert(tmodel, [0.0], 0.0)
        tmodel = numpy.append(tmodel, 0.0)
        pylab.fill(ptime, tmodel, fc='#ffff00', linewidth=0.0, alpha=0.2)

# plot data time series and best fit

    if status == 0 and (job == 'overlay' or job == 'fit'):
        pylab.plot(ptime, pout, color='#0000ff', linestyle='-', linewidth=1.0)
        ptime = numpy.insert(ptime, [0.0], ptime[0])
        ptime = numpy.append(ptime, ptime[-1])
        pout = numpy.insert(pout, [0], 0.0)
        pout = numpy.append(pout, 0.0)
        pylab.fill(ptime, pout, fc='#ffff00', linewidth=0.0, alpha=0.2)
        pylab.plot(ptime[1:-1], pmod, color='r', linestyle='-', linewidth=2.0)

# ranges and labels

    if status == 0:
        pylab.xlim(xmin - xr, xmax + xr)
        pylab.ylim(ymin - yr, ymax + yr)
        pylab.xlabel(xlab, {'color': 'k'})
        pylab.ylabel(ylab1, {'color': 'k'})

# residual plot window

    if status == 0 and (job == 'overlay' or job == 'fit'):
        ax = pylab.axes([0.05, 0.07, 0.94, 0.23])
        pylab.gca().xaxis.set_major_formatter(
            pylab.ScalarFormatter(useOffset=False))
        pylab.gca().yaxis.set_major_formatter(
            pylab.ScalarFormatter(useOffset=False))
        labels = ax.get_yticklabels()
        setp(labels, 'rotation', 90, fontsize=12)

# plot residual time series

    if status == 0 and (job == 'overlay' or job == 'fit'):
        pylab.plot([ptime[0], ptime[-1]], [0.0, 0.0],
                   color='r',
                   linestyle='--',
                   linewidth=1.0)
        pylab.plot([ptime[0], ptime[-1]], [-1.0, -1.0],
                   color='r',
                   linestyle='--',
                   linewidth=1.0)
        pylab.plot([ptime[0], ptime[-1]], [1.0, 1.0],
                   color='r',
                   linestyle='--',
                   linewidth=1.0)
        pylab.plot(ptime[1:-1],
                   pres,
                   color='#0000ff',
                   linestyle='-',
                   linewidth=1.0)
        pres = numpy.insert(pres, [0], rmin)
        pres = numpy.append(pres, rmin)
        pylab.fill(ptime, pres, fc='#ffff00', linewidth=0.0, alpha=0.2)

# ranges and labels of residual time series

    if status == 0 and (job == 'overlay' or job == 'fit'):
        pylab.xlim(xmin - xr, xmax + xr)
        pylab.ylim(rmin - rr, rmax + rr)
        pylab.xlabel(xlab, {'color': 'k'})
        pylab.ylabel(ylab2, {'color': 'k'})

# display the plot

    if status == 0:
        pylab.draw()
Esempio n. 12
0
def kepdetrend(infile,outfile,datacol,errcol,ranges1,npoly1,nsig1,niter1,
               ranges2,npoly2,nsig2,niter2,popnans,plot,clobber,verbose,logfile,
               status,cmdLine=False): 

# startup parameters

    status = 0
    labelsize = 24
    ticksize = 16
    xsize = 16
    ysize = 9
    lcolor = '#0000ff'
    lwidth = 1.0
    fcolor = '#ffff00'
    falpha = 0.2

# log the call 
          

    hashline = '----------------------------------------------------------------------------'
    kepmsg.log(logfile,hashline,verbose)
    call = 'KEPDETREND -- '
    call += 'infile='+infile+' '
    call += 'outfile='+outfile+' '
    call += 'datacol='+str(datacol)+' '
    call += 'errcol='+str(errcol)+' '
    call += 'ranges1='+str(ranges1)+' '
    call += 'npoly1='+str(npoly1)+' '
    call += 'nsig1='+str(nsig1)+' '
    call += 'niter1='+str(niter1)+' '
    call += 'ranges2='+str(ranges2)+' '
    call += 'npoly2='+str(npoly2)+' '
    call += 'nsig2='+str(nsig2)+' '
    call += 'niter2='+str(niter2)+' '
    popn = 'n'
    if (popnans): popn = 'y'
    call += 'popnans='+popn+ ' '
    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('KEPDETREND 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 -- KEPDETREND: ' + outfile + ' exists. Use clobber=yes'
	    status = kepmsg.err(logfile,message,verbose)

# open input file

    if status == 0:
        instr, status = kepio.openfits(infile,'readonly',logfile,verbose)
        tstart, tstop, bjdref, cadence, status = kepio.timekeys(instr,infile,logfile,verbose,status)

# 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]
        print(intime)

# time ranges for region 1 (region to be corrected)

    if status == 0:
        time1 = []; data1 = []; err1 = []
        t1start, t1stop, status = kepio.timeranges(ranges1,logfile,verbose)
    if status == 0:
        cadencelis1, status = kepstat.filterOnRange(intime,t1start,t1stop)
    if status == 0:
        for i in range(len(cadencelis1)):
            time1.append(intime[cadencelis1[i]])
            data1.append(indata[cadencelis1[i]])
            if errcol.lower() != 'none':
                err1.append(inerr[cadencelis1[i]])
        t0 = time1[0]
        time1 = array(time1,dtype='float64') - t0
        data1 = array(data1,dtype='float32')
        if errcol.lower() != 'none':
            err1 = array(err1,dtype='float32')
        else:
            err1 = None

# fit function to range 1

    if status == 0:
        functype = 'poly' + str(npoly1)
        pinit = [data1.mean()]
        if npoly1 > 0:
            for i in range(npoly1):
                pinit.append(0)
        pinit = array(pinit,dtype='float32')
        coeffs, errors, covar, iiter, sigma, chi2, dof, fit, plotx1, ploty1, status = \
            kepfit.lsqclip(functype,pinit,time1,data1,err1,nsig1,nsig1,niter1,
                           logfile,verbose)
        fit1 = indata * 0.0
        for i in range(len(coeffs)):
            fit1 += coeffs[i] * (intime - t0)**i
        for i in range(len(intime)):
            if i not in cadencelis1:
                fit1[i] = 0.0
        plotx1 += t0
        print(coeffs)

# time ranges for region 2 (region that is correct)

    if status == 0:
        time2 = []; data2 = []; err2 = []
        t2start, t2stop, status = kepio.timeranges(ranges2,logfile,verbose)
        cadencelis2, status = kepstat.filterOnRange(intime,t2start,t2stop)
        for i in range(len(cadencelis2)):
            time2.append(intime[cadencelis2[i]])
            data2.append(indata[cadencelis2[i]])
            if errcol.lower() != 'none':
                err2.append(inerr[cadencelis2[i]])
        t0 = time2[0]
        time2 = array(time2,dtype='float64') - t0
        data2 = array(data2,dtype='float32')
        if errcol.lower() != 'none':
            err2 = array(err2,dtype='float32')
        else:
            err2 = None

# fit function to range 2

    if status == 0:
        functype = 'poly' + str(npoly2)
        pinit = [data2.mean()]
        if npoly2 > 0:
            for i in range(npoly2):
                pinit.append(0)
        pinit = array(pinit,dtype='float32')
        coeffs, errors, covar, iiter, sigma, chi2, dof, fit, plotx2, ploty2, status = \
            kepfit.lsqclip(functype,pinit,time2,data2,err2,nsig2,nsig2,niter2,
                           logfile,verbose)
        fit2 = indata * 0.0
        for i in range(len(coeffs)):
            fit2 += coeffs[i] * (intime - t0)**i
        for i in range(len(intime)):
            if i not in cadencelis1:
                fit2[i] = 0.0
        plotx2 += t0

# normalize data

    if status == 0:
        outdata = indata - fit1 + fit2
        if errcol.lower() != 'none':
            outerr = inerr * 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)
        if intime0 < 2.4e6: intime0 += 2.4e6
	ptime = intime - intime0
	plotx1 = plotx1 - intime0
	plotx2 = plotx2 - intime0
	xlab = 'BJD $-$ %d' % intime0

# clean up y-axis units

    if status == 0:
        pout = outdata
        ploty1
        ploty2
	nrm = len(str(int(numpy.nanmax(indata))))-1
	indata = indata / 10**nrm
	pout = pout / 10**nrm
	ploty1 = ploty1 / 10**nrm
	ploty2 = ploty2 / 10**nrm
	ylab = '10$^%d$ e$^-$ s$^{-1}$' % nrm

# data limits

	xmin = ptime.min()
	xmax = ptime.max()
	ymin = indata.min()
	ymax = indata.max()
	omin = pout.min()
	omax = pout.max()
	xr = xmax - xmin
	yr = ymax - ymin
	oo = omax - omin
        ptime = insert(ptime,[0],[ptime[0]]) 
        ptime = append(ptime,[ptime[-1]])
        indata = insert(indata,[0],[0.0]) 
        indata = append(indata,[0.0])
        pout = insert(pout,[0],[0.0]) 
        pout = append(pout,0.0)

# plot light curve

    if status == 0 and plot:
        try:
            params = {'backend': 'png',
                      'axes.linewidth': 2.5,
                      'axes.labelsize': labelsize,
                      'axes.font': 'sans-serif',
                      'axes.fontweight' : 'bold',
                      'text.fontsize': 12,
                      'legend.fontsize': 12,
                      'xtick.labelsize': ticksize,
                      'ytick.labelsize': ticksize}
            rcParams.update(params)
        except:
            pass

        pylab.figure(figsize=[xsize,ysize])
        pylab.clf()

# plot original data

        ax = pylab.axes([0.06,0.523,0.93,0.45])

# force tick labels to be absolute rather than relative

        pylab.gca().xaxis.set_major_formatter(pylab.ScalarFormatter(useOffset=False))
        pylab.gca().yaxis.set_major_formatter(pylab.ScalarFormatter(useOffset=False))

# rotate y labels by 90 deg

        labels = ax.get_yticklabels()
        pylab.setp(labels, 'rotation', 90, fontsize=12)

        pylab.plot(ptime,indata,color=lcolor,linestyle='-',linewidth=lwidth)
        pylab.fill(ptime,indata,color=fcolor,linewidth=0.0,alpha=falpha)
        pylab.plot(plotx1,ploty1,color='r',linestyle='-',linewidth=2.0)
        pylab.plot(plotx2,ploty2,color='g',linestyle='-',linewidth=2.0)
        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)
	    pylab.ylabel(ylab, {'color' : 'k'})
        pylab.grid()

# plot detrended data

        ax = pylab.axes([0.06,0.073,0.93,0.45])

# force tick labels to be absolute rather than relative

        pylab.gca().xaxis.set_major_formatter(pylab.ScalarFormatter(useOffset=False))
        pylab.gca().yaxis.set_major_formatter(pylab.ScalarFormatter(useOffset=False))

# rotate y labels by 90 deg

        labels = ax.get_yticklabels()
        pylab.setp(labels, 'rotation', 90, fontsize=12)

        pylab.plot(ptime,pout,color=lcolor,linestyle='-',linewidth=lwidth)
        pylab.fill(ptime,pout,color=fcolor,linewidth=0.0,alpha=falpha)
        pylab.xlim(xmin-xr*0.01,xmax+xr*0.01)
        if ymin > 0.0: 
            pylab.ylim(omin-oo*0.01,omax+oo*0.01)
        else:
            pylab.ylim(1.0e-10,omax+oo*0.01)
	pylab.xlabel(xlab, {'color' : 'k'})
        try:
            pylab.ylabel(ylab, {'color' : 'k'})
        except:
            ylab = '10**%d e-/s' % nrm
            pylab.ylabel(ylab, {'color' : 'k'})

# render plot

    if status == 0:
        if cmdLine: 
            pylab.show()
        else: 
            pylab.ion()
            pylab.plot([])
            pylab.ioff()
	
# write output file
    if status == 0 and popnans:
	    instr[1].data.field(datacol)[good_data] = outdata
	    instr[1].data.field(errcol)[good_data] = outerr
	    instr[1].data.field(datacol)[bad_data] = None
	    instr[1].data.field(errcol)[bad_data] = None
	    instr.writeto(outfile)
    elif status == 0 and not popnans:
        for i in range(len(outdata)):
            instr[1].data.field(datacol)[i] = outdata[i]
            if errcol.lower() != 'none':
                instr[1].data.field(errcol)[i] = outerr[i]
        instr.writeto(outfile)
    
# close input file

    if status == 0:
        status = kepio.closefits(instr,logfile,verbose)	    

## end time

    if (status == 0):
	    message = 'KEPDETREND completed at'
    else:
	    message = '\nKEPDETREND aborted at'
    kepmsg.clock(message,logfile,verbose)
def kepconvert(infile, outfile, conversion, columns, baddata, clobber, verbose,
               logfile, status):

    # startup parameters

    status = 0

    # log the call

    hashline = '----------------------------------------------------------------------------'
    kepmsg.log(logfile, hashline, verbose)
    call = 'KEPCONVERT -- '
    call += 'infile=' + infile + ' '
    call += 'outfile=' + outfile + ' '
    call += 'conversion=' + conversion + ' '
    call += 'columns=' + columns + ' '
    writebad = 'n'
    if (baddata): writebad = 'y'
    call += 'baddata=' + writebad + ' '
    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('KEPCONVERT started at', logfile, verbose)

    # test log file

    logfile = kepmsg.test(logfile)

    # data columns

    if status == 0:
        colnames = columns.strip().split(',')
        ncol = len(colnames)
        if ncol < 1:
            message = 'ERROR -- KEPCONVERT: no data columns specified'
            status = kepmsg.err(logfile, message, verbose)

# input file exists

    if status == 0 and not kepio.fileexists(infile):
        message = 'ERROR -- KEPCONVERT: input file ' + infile + ' does not exist'
        status = kepmsg.err(logfile, message, verbose)

# clobber output file

    if status == 0:
        if clobber: status = kepio.clobber(outfile, logfile, verbose)
        if kepio.fileexists(outfile):
            message = 'ERROR -- KEPCONVERT: ' + outfile + ' exists. Use clobber=yes'
            status = kepmsg.err(logfile, message, verbose)

# open FITS input file

    if status == 0 and conversion == 'fits2asc':
        instr, status = kepio.openfits(infile, 'readonly', logfile, verbose)
        tstart, tstop, bjdref, cadence, status = kepio.timekeys(
            instr, infile, logfile, verbose, status)

# read FITS table data

    if status == 0 and conversion == 'fits2asc':
        table, status = kepio.readfitstab(infile, instr[1], logfile, verbose)

# check columns exist in FITS file
    if not baddata and status == 0 and conversion == 'fits2asc':
        try:
            qualcol = table.field('SAP_QUALITY') == 0
        except:
            message = 'No SAP_QUALITY column in data, are you using an old FITS file?'
            status = kepmsg.err(logfile, message, verbose)

    if status == 0 and conversion == 'fits2asc':
        work = []
        for colname in colnames:
            try:
                if colname.lower() == 'time':
                    work.append(table.field(colname) + bjdref)
                else:
                    work.append(table.field(colname))
            except:
                message = 'ERROR -- KEPCONVERT: no column ' + colname + ' in ' + infile
                status = kepmsg.err(logfile, message, verbose)
        if not baddata:
            for i in range(len(work)):
                work[i] = work[i][qualcol]
# close input file

    if status == 0 and conversion == 'fits2asc':
        status = kepio.closefits(instr, logfile, verbose)

## write output file

    if status == 0 and conversion == 'fits2asc':
        # table, status = kepio.openascii(outfile,'w',logfile,verbose)
        # for i in range(len(work[0])):
        # txt = ''
        # for j in range(len(work)):
        # if numpy.isfinite(work[j][i]):
        # txt += str(work[j][i]) + ' '
        # txt = txt.strip()
        # if len(re.sub('\s+',',',txt).split(',')) == ncol:
        # table.write(txt + '\n')
        # status = kepio.closeascii(table,logfile,verbose)
        savetxt(outfile, array(work).T)

## open and read ASCII input file

    if status == 0 and conversion == 'asc2fits':
        table, status = kepio.openascii(infile, 'r', logfile, verbose)

## organize ASCII table into arrays

    if status == 0 and conversion == 'asc2fits':
        work = []
        for i in range(ncol):
            work.append([])
        nline = 0
        for line in table:
            line = line.strip()
            line = re.sub('\s+', ',', line)
            line = re.sub('\|', ',', line)
            line = re.sub(';', ',', line)
            if '#' not in line:
                nline + 1
                line = line.split(',')
                if len(line) == ncol:
                    for i in range(len(line)):
                        try:
                            work[i].append(float(line[i]))
                        except:
                            message = 'ERROR --KEPCONVERT: ' + str(
                                line[i]) + ' is not float'
                            status = kepmsg.err(logfile, message, verbose)
                            break
                else:
                    message = 'ERROR --KEPCONVERT: ' + str(
                        ncol) + ' columns required but '
                    message += str(
                        len(line)) + ' columns supplied by ' + infile
                    message += ' at line' + str(nline)
                    status = kepmsg.err(logfile, message, verbose)
                    break
        for i in range(ncol):
            work[i] = numpy.array(work[i], dtype='float64')

## timing keywords for output file

    if status == 0 and conversion == 'asc2fits':
        for i in range(ncol):
            if 'time' in colnames[i].lower():
                if work[i][1] > 54000.0 and work[i][1] < 60000.0:
                    work[i] += 2.4e6
#                work[i] += 2.4553e6
                tstart = work[i].min()
                tstop = work[i].max()
                lc_start = tstart
                lc_end = tstop
                if lc_start > 2.4e6: lc_start -= 2.4e6
                if lc_end > 2.4e6: lc_end -= 2.4e6
                dts = []
                for j in range(1, len(work[i])):
                    dts.append(work[i][j] - work[i][j - 1])
                dts = numpy.array(dts, dtype='float32')
                cadence = numpy.median(dts)
                if cadence * 86400.0 > 58.0 and cadence * 86400.0 < 61.0:
                    obsmode = 'short cadence'
                elif cadence * 86400.0 > 1600.0 and cadence * 86400.0 < 2000.0:
                    obsmode = 'long cadence'
                else:
                    obsmode = 'unknown'

## Create the outfile primary extension

    if status == 0 and conversion == 'asc2fits':
        hdu0 = PrimaryHDU()
        try:
            hdu0.header.update('EXTNAME', 'PRIMARY', 'name of extension')
            hdu0.header.update('EXTVER', 1.0, 'extension version number')
            hdu0.header.update('ORIGIN', 'NASA/Ames',
                               'organization that generated this file')
            hdu0.header.update('DATE', time.asctime(time.localtime()),
                               'file creation date')
            hdu0.header.update('CREATOR', 'kepconvert',
                               'SW version used to create this file')
            hdu0.header.update('PROCVER', 'None', 'processing script version')
            hdu0.header.update('FILEVER', '2.0', 'file format version')
            hdu0.header.update('TIMVERSN', 'OGIP/93-003',
                               'OGIP memo number for file format')
            hdu0.header.update('TELESCOP', 'Kepler', 'telescope')
            hdu0.header.update('INSTRUME', 'Kepler photometer',
                               'detector type')
            hdu0.header.update('OBJECT', 'Unknown', 'string version of kepID')
            hdu0.header.update('KEPLERID', 'Unknown',
                               'unique Kepler target identifier')
            hdu0.header.update('CHANNEL', 'Unknown', 'CCD channel')
            hdu0.header.update('SKYGROUP', 'Unknown',
                               'roll-independent location of channel')
            hdu0.header.update('MODULE', 'Unknown', 'CCD module')
            hdu0.header.update('OUTPUT', 'Unknown', 'CCD output')
            hdu0.header.update(
                'QUARTER', 'Unknown',
                'mission quarter during which data was collected')
            hdu0.header.update(
                'SEASON', 'Unknown',
                'mission season during which data was collected')
            hdu0.header.update(
                'DATA_REL', 'Unknown',
                'version of data release notes describing data')
            hdu0.header.update('OBSMODE', obsmode, 'observing mode')
            hdu0.header.update('RADESYS', 'Unknown',
                               'reference frame of celestial coordinates')
            hdu0.header.update('RA_OBJ', 'Unknown',
                               '[deg] right ascension from KIC')
            hdu0.header.update('DEC_OBJ', 'Unknown',
                               '[deg] declination from KIC')
            hdu0.header.update('EQUINOX', 2000.0,
                               'equinox of celestial coordinate system')
            hdu0.header.update('PMRA', 'Unknown',
                               '[arcsec/yr] RA proper motion')
            hdu0.header.update('PMDEC', 'Unknown',
                               '[arcsec/yr] Dec proper motion')
            hdu0.header.update('PMTOTAL', 'Unknown',
                               '[arcsec/yr] total proper motion')
            hdu0.header.update('PARALLAX', 'Unknown', '[arcsec] parallax')
            hdu0.header.update('GLON', 'Unknown', '[deg] galactic longitude')
            hdu0.header.update('GLAT', 'Unknown', '[deg] galactic latitude')
            hdu0.header.update('GMAG', 'Unknown',
                               '[mag] SDSS g band magnitude from KIC')
            hdu0.header.update('RMAG', 'Unknown',
                               '[mag] SDSS r band magnitude from KIC')
            hdu0.header.update('IMAG', 'Unknown',
                               '[mag] SDSS i band magnitude from KIC')
            hdu0.header.update('ZMAG', 'Unknown',
                               '[mag] SDSS z band magnitude from KIC')
            hdu0.header.update('D51MAG', 'Unknown',
                               '[mag] D51 magnitude, from KIC')
            hdu0.header.update('JMAG', 'Unknown',
                               '[mag] J band magnitude from 2MASS')
            hdu0.header.update('HMAG', 'Unknown',
                               '[mag] H band magnitude from 2MASS')
            hdu0.header.update('KMAG', 'Unknown',
                               '[mag] K band magnitude from 2MASS')
            hdu0.header.update('KEPMAG', 'Unknown',
                               '[mag] Kepler magnitude (Kp) from KIC')
            hdu0.header.update('GRCOLOR', 'Unknown',
                               '[mag] (g-r) color, SDSS bands')
            hdu0.header.update('JKCOLOR', 'Unknown',
                               '[mag] (J-K) color, 2MASS bands')
            hdu0.header.update('GKCOLOR', 'Unknown',
                               '[mag] (g-K) color, SDSS g - 2MASS K')
            hdu0.header.update('TEFF', 'Unknown',
                               '[K] effective temperature from KIC')
            hdu0.header.update('LOGG', 'Unknown',
                               '[cm/s2] log10 surface gravity from KIC')
            hdu0.header.update('FEH', 'Unknown',
                               '[log10([Fe/H])] metallicity from KIC')
            hdu0.header.update('EBMINUSV', 'Unknown',
                               '[mag] E(B-V) redenning from KIC')
            hdu0.header.update('AV', 'Unknown',
                               '[mag] A_v extinction from KIC')
            hdu0.header.update('RADIUS', 'Unknown',
                               '[solar radii] stellar radius from KIC')
            hdu0.header.update('TMINDEX', 'Unknown',
                               'unique 2MASS catalog ID from KIC')
            hdu0.header.update('SCPID', 'Unknown',
                               'unique SCP processing ID from KIC')
            hdulist = HDUList(hdu0)
        except:
            message = 'ERROR -- KEPCONVERT: cannot create primary extension in ' + outfile
            status = kepmsg.err(logfile, message, verbose)

## create the outfile HDU 1 extension

    if status == 0 and conversion == 'asc2fits':
        try:
            fitscol = []
            for i in range(ncol):
                fitscol.append(
                    Column(name=colnames[i], format='D', array=work[i]))
            fitscols = ColDefs(fitscol)
            hdu1 = new_table(fitscols)
            hdulist.append(hdu1)
            hdu1.header.update('INHERIT', True, 'inherit primary keywords')
            hdu1.header.update('EXTNAME', 'LIGHTCURVE', 'name of extension')
            hdu1.header.update('EXTVER', 1, 'extension version number')
            hdu1.header.update('TELESCOP', 'Kepler', 'telescope')
            hdu1.header.update('INSTRUME', 'Kepler photometer',
                               'detector type')
            hdu1.header.update('OBJECT', 'Unknown', 'string version of kepID')
            hdu1.header.update('KEPLERID', 'Unknown',
                               'unique Kepler target identifier')
            hdu1.header.update('RADESYS', 'Unknown',
                               'reference frame of celestial coordinates')
            hdu1.header.update('RA_OBJ', 'Unknown',
                               '[deg] right ascension from KIC')
            hdu1.header.update('DEC_OBJ', 'Unknown',
                               '[deg] declination from KIC')
            hdu1.header.update('EQUINOX', 2000.0,
                               'equinox of celestial coordinate system')
            hdu1.header.update('TIMEREF', 'Unknown',
                               'barycentric correction applied to times')
            hdu1.header.update('TASSIGN', 'Unknown', 'where time is assigned')
            hdu1.header.update('TIMESYS', 'Unknown',
                               'time system is barycentric JD')
            hdu1.header.update('BJDREFI', 0.0,
                               'integer part of BJD reference date')
            hdu1.header.update('BJDREFF', 0.0,
                               'fraction of day in BJD reference date')
            hdu1.header.update('TIMEUNIT', 'Unknown',
                               'time unit for TIME, TSTART and TSTOP')
            hdu1.header.update('TSTART', tstart,
                               'observation start time in JD - BJDREF')
            hdu1.header.update('TSTOP', tstop,
                               'observation stop time in JD - BJDREF')
            hdu1.header.update('LC_START', lc_start,
                               'observation start time in MJD')
            hdu1.header.update('LC_END', lc_end,
                               'observation stop time in MJD')
            hdu1.header.update('TELAPSE', tstop - tstart, '[d] TSTOP - TSTART')
            hdu1.header.update('LIVETIME', 'Unknown',
                               '[d] TELAPSE multiplied by DEADC')
            hdu1.header.update('EXPOSURE', 'Unknown', '[d] time on source')
            hdu1.header.update('DEADC', 'Unknown', 'deadtime correction')
            hdu1.header.update('TIMEPIXR', 'Unknown',
                               'bin time beginning=0 middle=0.5 end=1')
            hdu1.header.update('TIERRELA', 'Unknown',
                               '[d] relative time error')
            hdu1.header.update('TIERABSO', 'Unknown',
                               '[d] absolute time error')
            hdu1.header.update('INT_TIME', 'Unknown',
                               '[s] photon accumulation time per frame')
            hdu1.header.update('READTIME', 'Unknown',
                               '[s] readout time per frame')
            hdu1.header.update('FRAMETIM', 'Unknown',
                               '[s] frame time (INT_TIME + READTIME)')
            hdu1.header.update('NUM_FRM', 'Unknown',
                               'number of frames per time stamp')
            hdu1.header.update('TIMEDEL', 'Unknown',
                               '[d] time resolution of data')
            hdu1.header.update('DATE-OBS', 'Unknown',
                               'TSTART as UT calendar date')
            hdu1.header.update('DATE-END', 'Unknown',
                               'TSTOP as UT calendar date')
            hdu1.header.update('BACKAPP', 'Unknown',
                               'background is subtracted')
            hdu1.header.update('DEADAPP', 'Unknown', 'deadtime applied')
            hdu1.header.update('VIGNAPP', 'Unknown',
                               'vignetting or collimator correction applied')
            hdu1.header.update('GAIN', 'Unknown',
                               'channel gain [electrons/count]')
            hdu1.header.update('READNOIS', 'Unknown', 'read noise [electrons]')
            hdu1.header.update('NREADOUT', 'Unknown',
                               'number of reads per cadence')
            hdu1.header.update('TIMSLICE', 'Unknown',
                               'time-slice readout sequence section')
            hdu1.header.update('MEANBLCK', 'Unknown',
                               'FSW mean black level [count]')
            hdu1.header.update('PDCSAPFL', 'Unknown',
                               'SAP PDC processing flags (bit code)')
            hdu1.header.update('PDCDIAFL', 'Unknown',
                               'DIA PDC processing flags (bit code)')
            hdu1.header.update(
                'MISPXSAP', 'Unknown',
                'no of optimal aperture pixels missing from SAP')
            hdu1.header.update(
                'MISPXDIA', 'Unknown',
                'no of optimal aperture pixels missing from DIA')
            hdu1.header.update('CROWDSAP', 'Unknown',
                               'crowding metric evaluated over SAP opt. ap.')
            hdu1.header.update('CROWDDIA', 'Unknown',
                               'crowding metric evaluated over DIA aperture')
        except:
            message = 'ERROR -- KEPCONVERT: cannot create light curve extension in ' + outfile
            status = kepmsg.err(logfile, message, verbose)

## history keyword in output file

    if status == 0 and conversion == 'asc2fits':
        status = kepkey.history(call, hdu0, outfile, logfile, verbose)

## filter data table

    if status == 0 and conversion == 'asc2fits':
        instr, status = kepio.filterNaN(
            hdulist, colnames[min(array([1, len(colnames) - 1], dtype='int'))],
            outfile, logfile, verbose)

## write output FITS file

    if status == 0 and conversion == 'asc2fits':
        hdulist.writeto(outfile, checksum=True)

## end time

    if (status == 0):
        message = 'KEPCONVERT completed at'
    else:
        message = '\nKEPCONVERT aborted at'
    kepmsg.clock(message, logfile, verbose)
Esempio n. 14
0
def keprange(infile,rinfile,outfile,column,clobber,verbose,logfile,status,cLine=False): 

# startup parameters

    status = 0
    global instr, cadence, barytime0, nrm, barytime, flux
    global xmin, xmax, ymin, ymax, xr, yr, xlab, ylab
    global clobb, outf, verb, logf, rinf, col, bjdref, cade, cmdLine

# log the call 

    if rinfile.lower() == 'none':
        rinfile = ''
    hashline = '----------------------------------------------------------------------------'
    kepmsg.log(logfile,hashline,verbose)
    call = 'KEPRANGE -- '
    call += 'infile='+infile+' '
    call += 'rinfile='+rinfile+' '
    call += 'outfile='+outfile+' '
    call += 'column='+column+' '
    overwrite = 'n'
    if (clobber): overwrite = 'y'
    call += 'clobber='+overwrite+ ' '
    chatter = 'n'
    if (verbose): chatter = 'y'
    call += 'verbose='+chatter+' '
    call += 'logfile='+logfile
    kepmsg.log(logfile,call+'\n',verbose)
    clobb = clobber
    outf = outfile
    verb = verbose
    logf = logfile
    rinf = rinfile
    cmdLine = cLine

# start time

    kepmsg.clock('KEPRANGE started at: ',logfile,verbose)

# test log file

    logfile = kepmsg.test(logfile)

# clobber output file

    if clobber: status = kepio.clobber(outfile,logfile,verbose)
    if kepio.fileexists(outfile): 
        message = 'ERROR -- KEPRANGE: ' + outfile + ' exists. Use --clobber'
        status = kepmsg.err(logfile,message,verbose)

## open input file

    if status == 0:
        instr, status = kepio.openfits(infile,'readonly',logfile,verbose)
    if status == 0:
        tstart, tstop, bjdref, cadence, status = kepio.timekeys(instr,infile,logfile,verbose,status)
    if status == 0:
        try:
            work = instr[0].header['FILEVER']
            cadenom = 1.0
        except:
            cadenom = cadence
    cade = cadenom

# fudge non-compliant FITS keywords with no values

    if status == 0:
        instr = kepkey.emptykeys(instr,infile,logfile,verbose)

# input data

    if status == 0:
        table = instr[1].data

# filter out NaNs

    work1 = []; work2 = []
    col = column
    if status == 0:
        barytime, status = kepio.readtimecol(infile,table,logfile,verbose)
    if status == 0:
        try:
            flux = instr[1].data.field(col)
        except:
            message = 'ERROR -- KEPRANGE: no column named ' + col + ' in table ' +  infile + '[1]'
            status = kepmsg.err(file,message,verbose)
    if status == 0:
        for i in range(len(barytime)):
            if (numpy.isfinite(barytime[i]) and numpy.isfinite(flux[i]) and flux[i] != 0.0):
                work1.append(barytime[i] + bjdref)
                work2.append(flux[i])
        barytime = array(work1,dtype='float64')
        flux = array(work2,dtype='float32') / cadenom

# clean up x-axis unit

    if status == 0:
	barytime0 = float(int(tstart / 100) * 100.0)
	barytime = barytime - barytime0
        xlab = 'BJD $-$ %d' % barytime0

# clean up y-axis units

    if status == 0:
	nrm = len(str(int(flux.max())))-1
	flux = flux / 10**nrm
	ylab = '10$^%d$ e$^-$ s$^{-1}$' % nrm

# data limits

	xmin = barytime.min()
	xmax = barytime.max()
	ymin = flux.min()
	ymax = flux.max()
	xr = xmax - xmin
	yr = ymax - ymin
	flux[0] = 0.0
	flux[-1] = 0.0

# plot new light curve

    if status == 0:
        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}
            pylab.rcParams.update(params)
        except:
            print 'ERROR -- KEPRANGE: install latex for scientific plotting'
            status = 1
    if status == 0:
	pylab.figure(figsize=[xsize,ysize])
        pylab.clf()
        plotlc(cmdLine)

# close input file

    if status == 0:
        status = kepio.closefits(instr,logfile,verbose)	    
Esempio n. 15
0
def kepdynamic(infile,
               outfile,
               fcol,
               pmin,
               pmax,
               nfreq,
               deltat,
               nslice,
               plot,
               plotscale,
               cmap,
               clobber,
               verbose,
               logfile,
               status,
               cmdLine=False):

    # startup parameters

    status = 0
    labelsize = 24
    ticksize = 16
    xsize = 12
    ysize = 6
    lcolor = '#0000ff'
    lwidth = 1.0
    fcolor = '#ffff00'
    falpha = 0.2
    numpy.seterr(all="ignore")

    # log the call

    hashline = '----------------------------------------------------------------------------'
    kepmsg.log(logfile, hashline, verbose)
    call = 'KEPDYNAMIC -- '
    call += 'infile=' + infile + ' '
    call += 'outfile=' + outfile + ' '
    call += 'fcol=' + fcol + ' '
    call += 'pmin=' + str(pmin) + ' '
    call += 'pmax=' + str(pmax) + ' '
    call += 'nfreq=' + str(nfreq) + ' '
    call += 'deltat=' + str(deltat) + ' '
    call += 'nslice=' + str(nslice) + ' '
    plotit = 'n'
    if (plot): plotit = 'y'
    call += 'plot=' + plotit + ' '
    call += 'plotscale=' + plotscale + ' '
    call += 'cmap=' + str(cmap) + ' '
    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('Start time is', logfile, verbose)

    # test log file

    logfile = kepmsg.test(logfile)

    # error checking

    if status == 0 and pmin >= pmax:
        message = 'ERROR -- KEPDYNAMIC: PMIN must be less than PMAX'
        status = kepmsg.err(logfile, message, verbose)

# clobber output file

    if clobber: status = kepio.clobber(outfile, logfile, verbose)
    if kepio.fileexists(outfile):
        message = 'ERROR -- KEPDYNAMIC: ' + outfile + ' exists. Use clobber'
        status = kepmsg.err(logfile, message, verbose)

# plot color map

    if status == 0 and cmap == 'browse':
        status = keplab.cmap_plot()

# 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 columns

    if status == 0:
        barytime, status = kepio.readtimecol(infile, instr[1].data, logfile,
                                             verbose)
    if status == 0:
        signal, status = kepio.readfitscol(infile, instr[1].data, fcol,
                                           logfile, verbose)
    if status == 0:
        barytime = barytime + bjdref
        signal = signal / cadenom

# remove infinite data from time series

    if status == 0:
        incols = [barytime, signal]
        outcols = kepstat.removeinfinlc(signal, incols)
        barytime = outcols[0]
        signal = outcols[1]

# period to frequency conversion

    if status == 0:
        fmin = 1.0 / pmax
        fmax = 1.0 / pmin
        deltaf = (fmax - fmin) / nfreq

# determine bounds of time slices

    if status == 0:
        t1 = []
        t2 = []
        dt = barytime[-1] - barytime[0]
        dt -= deltat
        if dt < 0:
            message = 'ERROR -- KEPDYNAMIC: time slices are larger than data range'
            status = kepmsg.err(logfile, message, verbose)
        ds = dt / (nslice - 1)
        for i in range(nslice):
            t1.append(barytime[0] + ds * float(i))
            t2.append(barytime[0] + deltat + ds * float(i))

# loop through time slices

    if status == 0:
        dynam = []
        for i in range(nslice):
            x = []
            y = []
            for j in range(len(barytime)):
                if (barytime[j] >= t1[i] and barytime[j] <= t2[i]):
                    x.append(barytime[j])
                    y.append(signal[j])
            x = array(x, dtype='float64')
            y = array(y, dtype='float32')
            y = y - median(y)

            # determine FT power

            fr, power = kepfourier.ft(x, y, fmin, fmax, deltaf, False)
            for j in range(len(power)):
                dynam.append(power[j])
            print('Timeslice: %.4f  Pmax: %.2E' %
                  ((t2[i] + t1[i]) / 2, power.max()))

# define shape of results array

        dynam = array(dynam, dtype='float64')
        dynam.shape = len(t1), len(power)

# write output file

    if status == 0:
        instr.append(ImageHDU())
        instr[-1].data = dynam.transpose()
        instr[-1].header.update('EXTNAME', 'DYNAMIC FT', 'extension name')
        instr[-1].header.update('WCSAXES', 2, 'number of WCS axes')
        instr[-1].header.update('CRPIX1', 0.5, 'reference pixel along axis 1')
        instr[-1].header.update('CRPIX2', 0.5, 'reference pixel along axis 2')
        instr[-1].header.update('CRVAL1', t1[0],
                                'time at reference pixel (BJD)')
        instr[-1].header.update('CRVAL2', fmin,
                                'frequency at reference pixel (1/day)')
        instr[-1].header.update('CDELT1',
                                (barytime[-1] - barytime[0]) / nslice,
                                'pixel scale in dimension 1 (days)')
        instr[-1].header.update('CDELT2', deltaf,
                                'pixel scale in dimension 2 (1/day)')
        instr[-1].header.update('CTYPE1', 'BJD', 'data type of dimension 1')
        instr[-1].header.update('CTYPE2', 'FREQUENCY',
                                'data type of dimension 2')
        instr.writeto(outfile)

# history keyword in output file

    if status == 0:
        status = kepkey.history(call, instr[0], outfile, logfile, verbose)

# close input file

    if status == 0:
        status = kepio.closefits(instr, logfile, verbose)

# clean up x-axis unit

    if status == 0:
        time0 = float(int(barytime[0] / 100) * 100.0)
        barytime = barytime - time0
        xlab = 'BJD $-$ %d' % time0

# image intensity min and max

    if status == 0:
        if 'rithmic' in plotscale:
            dynam = numpy.log10(dynam)
        elif 'sq' in plotscale:
            dynam = numpy.sqrt(dynam)
        elif 'logoflog' in plotscale:
            dynam = numpy.log10(numpy.abs(numpy.log10(dynam)))
#        dynam = -dynam
        nstat = 2
        pixels = []
        for i in range(dynam.shape[0]):
            for j in range(dynam.shape[1]):
                pixels.append(dynam[i, j])
        pixels = array(sort(pixels), dtype=float32)
        if int(float(len(pixels)) * 0.1 + 0.5) > nstat:
            nstat = int(float(len(pixels)) * 0.1 + 0.5)
        zmin = median(pixels[:nstat])
        zmax = median(pixels[-1:])
        if isnan(zmax):
            zmax = median(pixels[-nstat / 2:])
        if isnan(zmax):
            zmax = numpy.nanmax(pixels)

# plot power spectrum

    if status == 0 and plot:
        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)
        pylab.figure(1, figsize=[xsize, ysize])
        pylab.clf()
        pylab.axes([0.08, 0.113, 0.91, 0.86])
        dynam = dynam.transpose()
        pylab.imshow(dynam,
                     origin='lower',
                     aspect='auto',
                     cmap=cmap,
                     vmin=zmin,
                     vmax=zmax,
                     extent=[barytime[0], barytime[-1], fmin, fmax],
                     interpolation='bilinear')
        xlabel(xlab, {'color': 'k'})
        ylabel(r'Frequency (d$^{-1}$)', {'color': 'k'})
        grid()
        pylab.savefig(re.sub('\.\S+', '.png', outfile), dpi=100)

        # render plot

        if cmdLine:
            pylab.show()
        else:
            pylab.ion()
            pylab.plot([])
            pylab.ioff()

    return status

    ## end time

    if (status == 0):
        message = 'KEPDYNAMIC completed at'
    else:
        message = '\nKEPDYNAMIC aborted at'
    kepmsg.clock(message, logfile, verbose)
Esempio n. 16
0
def kepfilter(infile,outfile,datacol,function,cutoff,passband,plot,plotlab,
              clobber,verbose,logfile,status,cmdLine=False): 

## startup parameters

    status = 0
    numpy.seterr(all="ignore") 
    labelsize = 24
    ticksize = 16
    xsize = 16
    ysize = 6
    lcolor = '#0000ff'
    lwidth = 1.0
    fcolor = '#ffff00'
    falpha = 0.2

## log the call 

    hashline = '----------------------------------------------------------------------------'
    kepmsg.log(logfile,hashline,verbose)
    call = 'KEPFILTER -- '
    call += 'infile='+infile+' '
    call += 'outfile='+outfile+' '
    call += 'datacol='+str(datacol)+' '
    call += 'function='+str(function)+' '
    call += 'cutoff='+str(cutoff)+' '
    call += 'passband='+str(passband)+' '
    plotit = 'n'
    if (plot): plotit = 'y'
    call += 'plot='+plotit+ ' '
    call += 'plotlab='+str(plotlab)+' '
    overwrite = 'n'
    if (clobber): overwrite = 'y'
    call += 'clobber='+overwrite+ ' '
    chatter = 'n'
    if (verbose): chatter = 'y'
    call += 'verbose='+chatter+' '
    call += 'logfile='+logfile
    kepmsg.log(logfile,call+'\n',verbose)

## start time

    kepmsg.clock('KEPFILTER started at',logfile,verbose)

## test log file

    logfile = kepmsg.test(logfile)

## clobber output file

    if clobber: status = kepio.clobber(outfile,logfile,verbose)
    if kepio.fileexists(outfile): 
	    message = 'ERROR -- KEPFILTER: ' + outfile + ' exists. Use clobber=yes'
	    status = kepmsg.err(logfile,message,verbose)

## open input file

    if status == 0:
        instr, status = kepio.openfits(infile,'readonly',logfile,verbose)
        tstart, tstop, bjdref, cadence, status = kepio.timekeys(instr,infile,logfile,verbose,status)
    if status == 0:
        try:
            work = instr[0].header['FILEVER']
            cadenom = 1.0
        except:
            cadenom = cadence

## fudge non-compliant FITS keywords with no values

    if status == 0:
        instr = kepkey.emptykeys(instr,file,logfile,verbose)

## read table structure

    if status == 0:
	table, status = kepio.readfitstab(infile,instr[1],logfile,verbose)

# read time and flux columns

    if status == 0:
        barytime, status = kepio.readtimecol(infile,table,logfile,verbose)
        flux, status = kepio.readsapcol(infile,table,logfile,verbose)

# filter input data table

    if status == 0:
        try:
            nanclean = instr[1].header['NANCLEAN']
        except:
            naxis2 = 0
            for i in range(len(table.field(0))):
                if (numpy.isfinite(barytime[i]) and numpy.isfinite(flux[i]) and flux[i] != 0.0):
                    table[naxis2] = table[i]
                    naxis2 += 1
            instr[1].data = table[:naxis2]
            comment = 'NaN cadences removed from data'
            status = kepkey.new('NANCLEAN',True,comment,instr[1],outfile,logfile,verbose)

## read table columns

    if status == 0:
        intime, status = kepio.readtimecol(infile,instr[1].data,logfile,verbose)
    if status == 0:
	indata, status = kepio.readfitscol(infile,instr[1].data,datacol,logfile,verbose)
    if status == 0:
        intime = intime + bjdref
        indata = indata / cadenom

## define data sampling

    if status == 0:
        tr = 1.0 / (cadence / 86400)
        timescale = 1.0 / (cutoff / tr)

## define convolution function

    if status == 0:
        if function == 'boxcar':
            filtfunc = numpy.ones(numpy.ceil(timescale))
        elif function == 'gauss':
            timescale /= 2
            dx = numpy.ceil(timescale * 10 + 1)
            filtfunc = kepfunc.gauss()
            filtfunc = filtfunc([1.0,dx/2-1.0,timescale],linspace(0,dx-1,dx))
        elif function == 'sinc':
            dx = numpy.ceil(timescale * 12 + 1)
            fx = linspace(0,dx-1,dx)
            fx = fx - dx / 2 + 0.5
            fx /= timescale
            filtfunc = numpy.sinc(fx)
        filtfunc /= numpy.sum(filtfunc)

## pad time series at both ends with noise model

    if status == 0:
        ave, sigma  = kepstat.stdev(indata[:len(filtfunc)])
        padded = append(kepstat.randarray(np.ones(len(filtfunc)) * ave,
                                          np.ones(len(filtfunc)) * sigma), indata)
        ave, sigma  = kepstat.stdev(indata[-len(filtfunc):])
        padded = append(padded, kepstat.randarray(np.ones(len(filtfunc)) * ave,
                                                  np.ones(len(filtfunc)) * sigma))

## convolve data

    if status == 0:
        convolved = convolve(padded,filtfunc,'same')

## remove padding from the output array

    if status == 0:
        if function == 'boxcar':
            outdata = convolved[len(filtfunc):-len(filtfunc)]
        else:
            outdata = convolved[len(filtfunc):-len(filtfunc)]
            

## subtract low frequencies

    if status == 0 and passband == 'high':
        outmedian = median(outdata)
        outdata = indata - outdata + outmedian

## comment keyword in output file

    if status == 0:
        status = kepkey.history(call,instr[0],outfile,logfile,verbose)

## clean up x-axis unit

    if status == 0:
	intime0 = float(int(tstart / 100) * 100.0)
        if intime0 < 2.4e6: intime0 += 2.4e6
	ptime = intime - intime0
	xlab = 'BJD $-$ %d' % intime0

## clean up y-axis units

    if status == 0:
        pout = indata * 1.0
        pout2 = outdata * 1.0
	nrm = len(str(int(numpy.nanmax(pout))))-1
	pout = pout / 10**nrm
	pout2 = pout2 / 10**nrm
	ylab = '10$^%d$ %s' % (nrm, plotlab)

## data limits

	xmin = ptime.min()
	xmax = ptime.max()
	ymin = numpy.nanmin(pout)
	ymax = numpy.nanmax(pout)
	xr = xmax - xmin
	yr = ymax - ymin
        ptime = insert(ptime,[0],[ptime[0]]) 
        ptime = append(ptime,[ptime[-1]])
        pout = insert(pout,[0],[0.0]) 
        pout = append(pout,0.0)
        pout2 = insert(pout2,[0],[0.0]) 
        pout2 = append(pout2,0.0)

## plot light curve

    if status == 0 and plot:
        try:
            params = {'backend': 'png',
                      'axes.linewidth': 2.5,
                      'axes.labelsize': labelsize,
                      'axes.font': 'sans-serif',
                      'axes.fontweight' : 'bold',
                      'text.fontsize': 12,
                      'legend.fontsize': 12,
                      'xtick.labelsize': ticksize,
                      'ytick.labelsize': ticksize}
            rcParams.update(params)
        except:
            print 'ERROR -- KEPFILTER: install latex for scientific plotting'
            status = 1
    if status == 0 and plot:
        pylab.figure(figsize=[xsize,ysize])
        pylab.clf()

## plot filtered data

        ax = pylab.axes([0.06,0.1,0.93,0.87])
        pylab.gca().xaxis.set_major_formatter(pylab.ScalarFormatter(useOffset=False))
        pylab.gca().yaxis.set_major_formatter(pylab.ScalarFormatter(useOffset=False))
        labels = ax.get_yticklabels()
        setp(labels, 'rotation', 90, fontsize=12)
        pylab.plot(ptime,pout,color='#ff9900',linestyle='-',linewidth=lwidth)
        fill(ptime,pout,color=fcolor,linewidth=0.0,alpha=falpha)
        if passband == 'low':
            pylab.plot(ptime[1:-1],pout2[1:-1],color=lcolor,linestyle='-',linewidth=lwidth)
        else:
            pylab.plot(ptime,pout2,color=lcolor,linestyle='-',linewidth=lwidth)
            fill(ptime,pout2,color=lcolor,linewidth=0.0,alpha=falpha)
	xlabel(xlab, {'color' : 'k'})
	ylabel(ylab, {'color' : 'k'})
	xlim(xmin-xr*0.01,xmax+xr*0.01)
        if ymin >= 0.0: 
            ylim(ymin-yr*0.01,ymax+yr*0.01)
        else:
            ylim(1.0e-10,ymax+yr*0.01)
        pylab.grid()
        
# render plot

        if cmdLine: 
            pylab.show()
        else: 
            pylab.ion()
            pylab.plot([])
            pylab.ioff()
	
## write output file

    if status == 0:
        for i in range(len(outdata)):
            instr[1].data.field(datacol)[i] = outdata[i]
        instr.writeto(outfile)
    
## close input file

    if status == 0:
        status = kepio.closefits(instr,logfile,verbose)	    

## end time

    if (status == 0):
	    message = 'KEPFILTER completed at'
    else:
	    message = '\nKEPFILTER aborted at'
    kepmsg.clock(message,logfile,verbose)
def kepsmooth(infile,outfile,datacol,function,fscale,plot,plotlab,
              clobber,verbose,logfile,status, cmdLine=False): 

## startup parameters

    status = 0
    labelsize = 24
    ticksize = 16
    xsize = 18
    ysize = 6
    lcolor = '#0000ff'
    lwidth = 1.0
    fcolor = '#ffff00'
    falpha = 0.2

## log the call 

    hashline = '----------------------------------------------------------------------------'
    kepmsg.log(logfile,hashline,verbose)
    call = 'KEPSMOOTH -- '
    call += 'infile='+infile+' '
    call += 'outfile='+outfile+' '
    call += 'datacol='+str(datacol)+' '
    call += 'function='+str(function)+' '
    call += 'fscale='+str(fscale)+' '
    plotit = 'n'
    if (plot): plotit = 'y'
    call += 'plot='+plotit+ ' '
    call += 'plotlab='+str(plotlab)+' '
    overwrite = 'n'
    if (clobber): overwrite = 'y'
    call += 'clobber='+overwrite+ ' '
    chatter = 'n'
    if (verbose): chatter = 'y'
    call += 'verbose='+chatter+' '
    call += 'logfile='+logfile
    kepmsg.log(logfile,call+'\n',verbose)

## start time

    kepmsg.clock('KEPSMOOTH started at',logfile,verbose)

## test log file

    logfile = kepmsg.test(logfile)

## clobber output file

    if clobber: status = kepio.clobber(outfile,logfile,verbose)
    if kepio.fileexists(outfile): 
	    message = 'ERROR -- KEPSMOOTH: ' + outfile + ' exists. Use clobber=yes'
	    status = kepmsg.err(logfile,message,verbose)

## open input file

    if status == 0:
        instr, status = kepio.openfits(infile,'readonly',logfile,verbose)
        tstart, tstop, bjdref, cadence, status = kepio.timekeys(instr,infile,logfile,verbose,status)
        if cadence == 0.0: 
            tstart, tstop, ncad, cadence, status = kepio.cadence(instr,infile,logfile,verbose,status) 
    if status == 0:
        try:
            work = instr[0].header['FILEVER']
            cadenom = 1.0
        except:
            cadenom = cadence

## fudge non-compliant FITS keywords with no values

    if status == 0:
        instr = kepkey.emptykeys(instr,file,logfile,verbose)

## read table structure

    if status == 0:
	table, status = kepio.readfitstab(infile,instr[1],logfile,verbose)

# read time and flux columns

    if status == 0:
        barytime, status = kepio.readtimecol(infile,table,logfile,verbose)
    if status == 0:
        flux, status = kepio.readfitscol(infile,instr[1].data,datacol,logfile,verbose)

# filter input data table

    if status == 0:
        try:
            nanclean = instr[1].header['NANCLEAN']
        except:
            naxis2 = 0
            for i in range(len(table.field(0))):
                if (numpy.isfinite(barytime[i]) and numpy.isfinite(flux[i]) and flux[i] != 0.0):
                    table[naxis2] = table[i]
                    naxis2 += 1
            instr[1].data = table[:naxis2]
            comment = 'NaN cadences removed from data'
            status = kepkey.new('NANCLEAN',True,comment,instr[1],outfile,logfile,verbose)

## read table columns

    if status == 0:
	try:
            intime = instr[1].data.field('barytime')
	except:
            intime, status = kepio.readfitscol(infile,instr[1].data,'time',logfile,verbose)
	indata, status = kepio.readfitscol(infile,instr[1].data,datacol,logfile,verbose)
    if status == 0:
        intime = intime + bjdref
        indata = indata / cadenom

## smooth data

    if status == 0:
        outdata = kepfunc.smooth(indata,fscale/(cadence/86400),function)

## comment keyword in output file

    if status == 0:
        status = kepkey.history(call,instr[0],outfile,logfile,verbose)

## clean up x-axis unit

    if status == 0:
	intime0 = float(int(tstart / 100) * 100.0)
        if intime0 < 2.4e6: intime0 += 2.4e6
	ptime = intime - intime0
	xlab = 'BJD $-$ %d' % intime0

## clean up y-axis units

    if status == 0:
        pout = indata * 1.0
        pout2 = outdata * 1.0 
	nrm = len(str(int(numpy.nanmax(pout))))-1
	pout = pout / 10**nrm
	pout2 = pout2 / 10**nrm
	ylab = '10$^%d$ %s' % (nrm, re.sub('_','-',plotlab))

## data limits

	xmin = numpy.nanmin(ptime)
	xmax = numpy.nanmax(ptime)
	ymin = numpy.min(pout)
	ymax = numpy.nanmax(pout)
	xr = xmax - xmin
	yr = ymax - ymin
        ptime = insert(ptime,[0],[ptime[0]]) 
        ptime = append(ptime,[ptime[-1]])
        pout = insert(pout,[0],[0.0]) 
        pout = append(pout,0.0)
        pout2 = insert(pout2,[0],[0.0]) 
        pout2 = append(pout2,0.0)

## plot light curve

    if status == 0 and plot:
        try:
            params = {'backend': 'png',
                      'axes.linewidth': 2.5,
                      'axes.labelsize': labelsize,
                      'axes.font': 'sans-serif',
                      'axes.fontweight' : 'bold',
                      'text.fontsize': 12,
                      'legend.fontsize': 12,
                      'xtick.labelsize': ticksize,
                      'ytick.labelsize': ticksize}
            rcParams.update(params)
        except:
            print 'ERROR -- KEPSMOOTH: install latex for scientific plotting'
            status = 1
    if status == 0 and plot:
        pylab.figure(1,figsize=[xsize,ysize])

# delete any fossil plots in the matplotlib window

        pylab.clf()

# position axes inside the plotting window

	ax = pylab.subplot(111)
	pylab.subplots_adjust(0.06,0.1,0.93,0.88)

# force tick labels to be absolute rather than relative

        pylab.gca().xaxis.set_major_formatter(pylab.ScalarFormatter(useOffset=False))
        pylab.gca().yaxis.set_major_formatter(pylab.ScalarFormatter(useOffset=False))

# rotate y labels by 90 deg

        labels = ax.get_yticklabels()
        setp(labels, 'rotation', 90)

        pylab.plot(ptime[1:-1],pout[1:-1],color='#ff9900',linestyle='-',linewidth=lwidth)
        fill(ptime,pout,color=fcolor,linewidth=0.0,alpha=falpha)
        pylab.plot(ptime,pout2,color=lcolor,linestyle='-',linewidth=lwidth*4.0)
	pylab.xlabel(xlab, {'color' : 'k'})
	pylab.ylabel(ylab, {'color' : 'k'})
	xlim(xmin-xr*0.01,xmax+xr*0.01)
        if ymin >= 0.0: 
            ylim(ymin-yr*0.01,ymax+yr*0.01)
        else:
            ylim(1.0e-10,ymax+yr*0.01)
        pylab.grid()

# render plot

    if cmdLine: 
        pylab.show()
    else: 
        pylab.ion()
        pylab.plot([])
        pylab.ioff()
	
## write output file

    if status == 0:
        for i in range(len(outdata)):
            instr[1].data.field(datacol)[i] = outdata[i]
        instr.writeto(outfile)
    
## close input file

    if status == 0:
        status = kepio.closefits(instr,logfile,verbose)	    

## end time

    if (status == 0):
	    message = 'KEPSMOOTH completed at'
    else:
	    message = '\nKEPSMOOTH aborted at'
    kepmsg.clock(message,logfile,verbose)
Esempio n. 18
0
def kepregr(infile, outfile, datacol, kmethod, kneighb, plot, plotlab, clobber,
            verbose, logfile, status):
    """
    Perform a k-nearest neighbor regression analysis.
    """

    ## startup parameters

    status = 0
    labelsize = 24
    ticksize = 16
    xsize = 16
    ysize = 6
    lcolor = '#47AE10'
    lwidth = 1.0
    fcolor = '#9AFF9A'
    falpha = 0.3

    ## log the call

    hashline = '----------------------------------------------------------------------------'
    kepmsg.log(logfile, hashline, verbose)
    call = 'KEPREGR -- '
    call += 'infile=' + infile + ' '
    call += 'outfile=' + outfile + ' '
    call += 'datacol=' + str(datacol) + ' '
    call += 'kmethod=' + str(kmethod) + ' '
    call += 'kneighb=' + str(kneighb) + ' '
    plotit = 'n'
    if (plot): plotit = 'y'
    call += 'plot=' + plotit + ' '
    call += 'plotlab=' + str(plotlab) + ' '
    overwrite = 'n'
    if (clobber): overwrite = 'y'
    call += 'clobber=' + overwrite + ' '
    chatter = 'n'
    if (verbose): chatter = 'y'
    call += 'verbose=' + chatter + ' '
    call += 'logfile=' + logfile
    kepmsg.log(logfile, call + '\n', verbose)

    ## start time

    kepmsg.clock('KEPREGR started at', logfile, verbose)

    ## test log file

    logfile = kepmsg.test(logfile)

    ## clobber output file

    if clobber: status = kepio.clobber(outfile, logfile, verbose)
    if kepio.fileexists(outfile):
        message = 'ERROR -- KEPREGR: ' + outfile + ' exists. Use clobber=yes'
        status = kepmsg.err(logfile, message, verbose)

## open input file

    if status == 0:
        instr, status = kepio.openfits(infile, 'readonly', logfile, verbose)
        tstart, tstop, bjdref, cadence, status = kepio.timekeys(
            instr, infile, logfile, verbose, status)
        if cadence == 0.0:
            tstart, tstop, ncad, cadence, status = kepio.cadence(
                instr, infile, logfile, verbose, status)
    if status == 0:
        try:
            work = instr[0].header['FILEVER']
            cadenom = 1.0
        except:
            cadenom = cadence

## fudge non-compliant FITS keywords with no values

    if status == 0:
        instr = kepkey.emptykeys(instr, file, logfile, verbose)

## read table structure

    if status == 0:
        table, status = kepio.readfitstab(infile, instr[1], logfile, verbose)

# read time and flux columns

    if status == 0:
        barytime, status = kepio.readtimecol(infile, table, logfile, verbose)
    if status == 0:
        flux, status = kepio.readfitscol(infile, instr[1].data, datacol,
                                         logfile, verbose)

# filter input data table

    if status == 0:
        try:
            nanclean = instr[1].header['NANCLEAN']
        except:
            naxis2 = 0
            for i in range(len(table.field(0))):
                if (numpy.isfinite(barytime[i]) and numpy.isfinite(flux[i])
                        and flux[i] != 0.0):
                    table[naxis2] = table[i]
                    naxis2 += 1
            instr[1].data = table[:naxis2]
            comment = 'NaN cadences removed from data'
            status = kepkey.new('NANCLEAN', True, comment, instr[1], outfile,
                                logfile, verbose)

## read table columns

    if status == 0:
        try:
            intime = instr[1].data.field('barytime')
        except:
            intime, status = kepio.readfitscol(infile, instr[1].data, 'time',
                                               logfile, verbose)
        indata, status = kepio.readfitscol(infile, instr[1].data, datacol,
                                           logfile, verbose)
    if status == 0:
        intime = intime + bjdref
        indata = indata / cadenom

    if status == 0:
        outdata = knn_predict(intime, indata, kmethod, kneighb)

## comment keyword in output file

    if status == 0:
        status = kepkey.history(call, instr[0], outfile, logfile, verbose)

## clean up x-axis unit

    if status == 0:
        intime0 = float(int(tstart / 100) * 100.0)
        if intime0 < 2.4e6: intime0 += 2.4e6
        ptime = intime - intime0
        # print ptime,intime,intime0
        xlab = 'BJD $-$ %d' % intime0

## clean up y-axis units

    if status == 0:
        pout = indata * 1.0
        pout2 = outdata * 1.0
        nrm = len(str(int(numpy.nanmax(pout)))) - 1
        pout = pout / 10**nrm
        pout2 = pout2 / 10**nrm
        ylab = '10$^%d$ %s' % (nrm, plotlab)

        ## data limits

        xmin = numpy.nanmin(ptime)
        xmax = numpy.nanmax(ptime)
        ymin = numpy.min(pout)
        ymax = numpy.nanmax(pout)
        xr = xmax - xmin
        yr = ymax - ymin
        ptime = insert(ptime, [0], [ptime[0]])
        ptime = append(ptime, [ptime[-1]])
        pout = insert(pout, [0], [0.0])
        pout = append(pout, 0.0)
        pout2 = insert(pout2, [0], [0.0])
        pout2 = append(pout2, 0.0)

## plot light curve

    if status == 0 and plot:
        try:
            params = {
                'backend': 'png',
                'axes.linewidth': 2.5,
                'axes.labelsize': labelsize,
                'axes.font': 'sans-serif',
                'axes.fontweight': 'bold',
                'text.fontsize': 12,
                'legend.fontsize': 12,
                'xtick.labelsize': ticksize,
                'ytick.labelsize': ticksize
            }
            rcParams.update(params)
        except:
            print('ERROR -- KEPREGR: install latex for scientific plotting')
            status = 1
    if status == 0 and plot:
        pylab.figure(1, figsize=[xsize, ysize])

        ## plot regression data

        ax = pylab.axes([0.06, 0.1, 0.93, 0.87])
        pylab.gca().xaxis.set_major_formatter(
            pylab.ScalarFormatter(useOffset=False))
        pylab.gca().yaxis.set_major_formatter(
            pylab.ScalarFormatter(useOffset=False))
        # pylab.plot(ptime,pout,color='#ff9900',linestyle='-',linewidth=lwidth)
        pylab.scatter(ptime, pout, color='#214CAE', s=5)
        fill(ptime, pout, color=fcolor, linewidth=0.0, alpha=falpha)
        pylab.plot(ptime[kneighb:-kneighb],
                   pout2[kneighb:-kneighb],
                   color=lcolor,
                   linestyle='-',
                   linewidth=lwidth * 2.0)
        xlabel(xlab, {'color': 'k'})
        ylabel(ylab, {'color': 'k'})
        xlim(xmin - xr * 0.01, xmax + xr * 0.01)
        if ymin >= 0.0:
            ylim(ymin - yr * 0.01, ymax + yr * 0.01)
        else:
            ylim(1.0e-10, ymax + yr * 0.01)
        pylab.grid()
        pylab.draw()
        pylab.savefig(re.sub('\.\S+', '.png', outfile), dpi=100)

## write output file

    if status == 0:
        for i in range(len(outdata)):
            instr[1].data.field(datacol)[i] = outdata[i]
        instr.writeto(outfile)

## close input file

    if status == 0:
        status = kepio.closefits(instr, logfile, verbose)

## end time

    if (status == 0):
        message = 'KEPREGR completed at'
    else:
        message = '\nKEPREGR aborted at'
    kepmsg.clock(message, logfile, verbose)
Esempio n. 19
0
def kepft(infile,outfile,fcol,pmin,pmax,nfreq,plot,clobber,verbose,logfile,status, cmdLine=False): 

## startup parameters

    status = 0
    labelsize = 24
    ticksize = 16
    xsize = 18
    ysize = 6
    lcolor = '#0000ff'
    lwidth = 1.0
    fcolor = '#ffff00'
    falpha = 0.2

## log the call 

    hashline = '----------------------------------------------------------------------------'
    kepmsg.log(logfile,hashline,verbose)
    call = 'KEPFT -- '
    call += 'infile='+infile+' '
    call += 'outfile='+outfile+' '
    call += 'fcol='+fcol+' '
    call += 'pmin='+str(pmin)+' '
    call += 'pmax='+str(pmax)+' '
    call += 'nfreq='+str(nfreq)+' '
    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('Start time is',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 -- KEPFT: ' + 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)

## fudge non-compliant FITS keywords with no values

    if status == 0:
        instr = kepkey.emptykeys(instr,file,logfile,verbose)

## read table columns

    if status == 0:
	try:
            barytime = instr[1].data.field('barytime')
	except:
            barytime, status = kepio.readfitscol(infile,instr[1].data,'time',logfile,verbose)
	signal, status = kepio.readfitscol(infile,instr[1].data,fcol,logfile,verbose)
    if status == 0:
        barytime = barytime + bjdref

## remove infinite data from time series

    if status == 0:
        incols = [barytime, signal]
        outcols = kepstat.removeinfinlc(signal, incols)
        barytime = outcols[0] 
        signal = outcols[1] - median(outcols[1])

## period to frequency conversion

    fmin = 1.0 / pmax
    fmax = 1.0 / pmin
    deltaf = (fmax - fmin) / nfreq

## loop through frequency steps; determine FT power

    if status == 0:
        fr, power = kepfourier.ft(barytime,signal,fmin,fmax,deltaf,True)

## write output file

    if status == 0:
        col1 = Column(name='FREQUENCY',format='E',unit='1/day',array=fr)
        col2 = Column(name='POWER',format='E',array=power)
        cols = ColDefs([col1,col2])
        instr.append(new_table(cols))
        instr[-1].header.update('EXTNAME','POWER SPECTRUM','extension name')
        instr.writeto(outfile)
    
## history keyword in output file

    if status == 0:
        status = kepkey.history(call,instr[0],outfile,logfile,verbose)

## close input file

    if status == 0:
        status = kepio.closefits(instr,logfile,verbose)	    

## data limits

    if status == 0:
        nrm = int(log10(power.max()))
        power = power / 10**nrm
        ylab = 'Power (x10$^{%d}$)' % nrm
	xmin = fr.min()
	xmax = fr.max()
	ymin = power.min()
	ymax = power.max()
	xr = xmax - xmin
	yr = ymax - ymin
        fr = insert(fr,[0],fr[0])
        fr = append(fr,fr[-1])
        power = insert(power,[0],0.0) 
        power = append(power,0.0)

## plot power spectrum

    if status == 0 and plot:
        try:
            params = {'backend': 'png',
                      'axes.linewidth': 2.5,
                      'axes.labelsize': labelsize,
                      'axes.font': 'sans-serif',
                      'axes.fontweight' : 'bold',
                      'text.fontsize': 12,
                      'legend.fontsize': 12,
                      'xtick.labelsize': ticksize,
                      'ytick.labelsize': ticksize}
            rcParams.update(params)
        except:
            print 'ERROR -- KEPFT: install latex for scientific plotting'
            status = 1

    if status == 0 and plot:
        pylab.figure(1,figsize=[xsize,ysize])
        pylab.clf()
        pylab.axes([0.06,0.113,0.93,0.86])
        pylab.plot(fr,power,color=lcolor,linestyle='-',linewidth=lwidth)
        fill(fr,power,color=fcolor,linewidth=0.0,alpha=falpha)
        xlim(xmin-xr*0.01,xmax+xr*0.01)
        if ymin-yr*0.01 <= 0.0:
            ylim(1.0e-10,ymax+yr*0.01)
        else:
            ylim(ymin-yr*0.01,ymax+yr*0.01)
        xlabel(r'Frequency (d$^{-1}$)', {'color' : 'k'})
        ylabel(ylab, {'color' : 'k'})
        
        grid()

# render plot

        if cmdLine: 
            pylab.show()
        else: 
            pylab.ion()
            pylab.plot([])
            pylab.ioff()
	
## end time

    if (status == 0):
	    message = 'KEPFT completed at'
    else:
	    message = '\nKEPFT aborted at'
    kepmsg.clock(message,logfile,verbose)
Esempio n. 20
0
def kepprfphot(infile,outroot,columns,rows,fluxes,border,background,focus,prfdir,ranges,
               tolerance,ftolerance,qualflags,plt,clobber,verbose,logfile,status,cmdLine=False):

# input arguments

    status = 0
    seterr(all="ignore")

# log the call

    hashline = '----------------------------------------------------------------------------'
    kepmsg.log(logfile,hashline,verbose)
    call = 'KEPPRFPHOT -- '
    call += 'infile='+infile+' '
    call += 'outroot='+outroot+' '
    call += 'columns='+columns+' '
    call += 'rows='+rows+' '
    call += 'fluxes='+fluxes+' '
    call += 'border='+str(border)+' '
    bground = 'n'
    if (background): bground = 'y'
    call += 'background='+bground+' '
    focs = 'n'
    if (focus): focs = 'y'
    call += 'focus='+focs+' '
    call += 'prfdir='+prfdir+' '
    call += 'ranges='+ranges+' '
    call += 'xtol='+str(tolerance)+' '
    call += 'ftol='+str(ftolerance)+' '
    quality = 'n'
    if (qualflags): quality = 'y'
    call += 'qualflags='+quality+' '
    plotit = 'n'
    if (plt): 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)

# test log file

    logfile = kepmsg.test(logfile)

# start time

    kepmsg.clock('KEPPRFPHOT started at',logfile,verbose)

# number of sources

    if status == 0:
        work = fluxes.strip()
        work = re.sub(' ',',',work)
        work = re.sub(';',',',work)
        nsrc = len(work.split(','))

# construct inital guess vector for fit

    if status == 0:
        guess = []
        try:
            f = fluxes.strip().split(',')
            x = columns.strip().split(',')
            y = rows.strip().split(',')
            for i in xrange(len(f)):
                f[i] = float(f[i])
        except:
            f = fluxes
            x = columns
            y = rows
        nsrc = len(f)
        for i in xrange(nsrc):
            try:
                guess.append(float(f[i]))
            except:
                message = 'ERROR -- KEPPRF: Fluxes must be floating point numbers'
                status = kepmsg.err(logfile,message,verbose)
        if status == 0:
            if len(x) != nsrc or len(y) != nsrc:
                message = 'ERROR -- KEPFIT:FITMULTIPRF: Guesses for rows, columns and '
                message += 'fluxes must have the same number of sources'
                status = kepmsg.err(logfile,message,verbose)
        if status == 0:
            for i in xrange(nsrc):
                try:
                    guess.append(float(x[i]))
                except:
                    message = 'ERROR -- KEPPRF: Columns must be floating point numbers'
                    status = kepmsg.err(logfile,message,verbose)
        if status == 0:
            for i in xrange(nsrc):
                try:
                    guess.append(float(y[i]))
                except:
                    message = 'ERROR -- KEPPRF: Rows must be floating point numbers'
                    status = kepmsg.err(logfile,message,verbose)
        if status == 0 and background:
            if border == 0:
                guess.append(0.0)
            else:
                for i in range((border+1)*2):
                    guess.append(0.0)
        if status == 0 and focus:
            guess.append(1.0); guess.append(1.0); guess.append(0.0)

# clobber output file

    for i in range(nsrc):
        outfile = '%s_%d.fits' % (outroot, i)
        if clobber: status = kepio.clobber(outfile,logfile,verbose)
        if kepio.fileexists(outfile):
            message = 'ERROR -- KEPPRFPHOT: ' + outfile + ' exists. Use --clobber'
            status = kepmsg.err(logfile,message,verbose)

# open TPF FITS file

    if status == 0:
        try:
            kepid, channel, skygroup, module, output, quarter, season, \
                ra, dec, column, row, kepmag, xdim, ydim, barytime, status = \
                kepio.readTPF(infile,'TIME',logfile,verbose)
        except:
            message = 'ERROR -- KEPPRFPHOT: is %s a Target Pixel File? ' % infile
            status = kepmsg.err(logfile,message,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, poscorr1, status = \
            kepio.readTPF(infile,'POS_CORR1',logfile,verbose)
        if status != 0:
            poscorr1 = numpy.zeros((len(barytime)),dtype='float32')
            poscorr1[:] = numpy.nan
            status = 0
    if status == 0:
        kepid, channel, skygroup, module, output, quarter, season, \
            ra, dec, column, row, kepmag, xdim, ydim, poscorr2, status = \
            kepio.readTPF(infile,'POS_CORR2',logfile,verbose)
        if status != 0:
            poscorr2 = numpy.zeros((len(barytime)),dtype='float32')
            poscorr2[:] = numpy.nan
            status = 0
    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)
    if status == 0:
        struct, status = kepio.openfits(infile,'readonly',logfile,verbose)
    if status == 0:
        tstart, tstop, bjdref, cadence, status = kepio.timekeys(struct,infile,logfile,verbose,status)

# input file keywords and mask map

    if status == 0:
        cards0 = struct[0].header.cards
        cards1 = struct[1].header.cards
        cards2 = struct[2].header.cards
        maskmap = copy(struct[2].data)
        npix = numpy.size(numpy.nonzero(maskmap)[0])

# print target data

    if status == 0 and verbose:
        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 ''

# determine suitable PRF calibration file

    if status == 0:
        if int(module) < 10:
            prefix = 'kplr0'
        else:
            prefix = 'kplr'
        prfglob = prfdir + '/' + prefix + str(module) + '.' + str(output) + '*' + '_prf.fits'
        try:
            prffile = glob.glob(prfglob)[0]
        except:
            message = 'ERROR -- KEPPRFPHOT: No PRF file found in ' + prfdir
            status = kepmsg.err(logfile,message,verbose)

# read PRF images

    if status == 0:
        prfn = [0,0,0,0,0]
        crpix1p = numpy.zeros((5),dtype='float32')
        crpix2p = numpy.zeros((5),dtype='float32')
        crval1p = numpy.zeros((5),dtype='float32')
        crval2p = numpy.zeros((5),dtype='float32')
        cdelt1p = numpy.zeros((5),dtype='float32')
        cdelt2p = numpy.zeros((5),dtype='float32')
        for i in range(5):
            prfn[i], crpix1p[i], crpix2p[i], crval1p[i], crval2p[i], cdelt1p[i], cdelt2p[i], status \
                = kepio.readPRFimage(prffile,i+1,logfile,verbose)
        PRFx = arange(0.5,shape(prfn[0])[1]+0.5)
        PRFy = arange(0.5,shape(prfn[0])[0]+0.5)
        PRFx = (PRFx - size(PRFx) / 2) * cdelt1p[0]
        PRFy = (PRFy - size(PRFy) / 2) * cdelt2p[0]

# interpolate the calibrated PRF shape to the target position

    if status == 0:
        prf = zeros(shape(prfn[0]),dtype='float32')
        prfWeight = zeros((5),dtype='float32')
        for i in xrange(5):
            prfWeight[i] = sqrt((column - crval1p[i])**2 + (row - crval2p[i])**2)
            if prfWeight[i] == 0.0:
                prfWeight[i] = 1.0e6
            prf = prf + prfn[i] / prfWeight[i]
        prf = prf / nansum(prf)
        prf = prf / cdelt1p[0] / cdelt2p[0]

# location of the data image centered on the PRF image (in PRF pixel units)

    if status == 0:
        prfDimY = ydim / cdelt1p[0]
        prfDimX = xdim / cdelt2p[0]
        PRFy0 = (shape(prf)[0] - prfDimY) / 2
        PRFx0 = (shape(prf)[1] - prfDimX) / 2

# construct input pixel image

    if status == 0:
        DATx = arange(column,column+xdim)
        DATy = arange(row,row+ydim)

# interpolation function over the PRF

    if status == 0:
        splineInterpolation = scipy.interpolate.RectBivariateSpline(PRFx,PRFy,prf,kx=3,ky=3)

# construct mesh for background model

    if status == 0:
        bx = numpy.arange(1.,float(xdim+1))
        by = numpy.arange(1.,float(ydim+1))
        xx, yy = numpy.meshgrid(numpy.linspace(bx.min(), bx.max(), xdim),
                                numpy.linspace(by.min(), by.max(), ydim))

# Get time ranges for new photometry, flag good data

    if status == 0:
        barytime += bjdref
        tstart,tstop,status = kepio.timeranges(ranges,logfile,verbose)
        incl = numpy.zeros((len(barytime)),dtype='int')
        for rownum in xrange(len(barytime)):
            for winnum in xrange(len(tstart)):
                if barytime[rownum] >= tstart[winnum] and \
                        barytime[rownum] <= tstop[winnum] and \
                        (qual[rownum] == 0 or qualflags) and \
                        numpy.isfinite(barytime[rownum]) and \
                        numpy.isfinite(numpy.nansum(fluxpixels[rownum,:])):
                    incl[rownum] = 1
        if not numpy.in1d(1,incl):
            message = 'ERROR -- KEPPRFPHOT: No legal data within the range ' + ranges
            status = kepmsg.err(logfile,message,verbose)

# filter out bad data

    if status == 0:
        n = 0
        nincl = (incl == 1).sum()
        tim = zeros((nincl),'float64')
        tco = zeros((nincl),'float32')
        cad = zeros((nincl),'float32')
        flu = zeros((nincl,len(fluxpixels[0])),'float32')
        fer = zeros((nincl,len(fluxpixels[0])),'float32')
        pc1 = zeros((nincl),'float32')
        pc2 = zeros((nincl),'float32')
        qua = zeros((nincl),'float32')
        for rownum in xrange(len(barytime)):
            if incl[rownum] == 1:
                tim[n] = barytime[rownum]
                tco[n] = tcorr[rownum]
                cad[n] = cadno[rownum]
                flu[n,:] = fluxpixels[rownum]
                fer[n,:] = errpixels[rownum]
                pc1[n] = poscorr1[rownum]
                pc2[n] = poscorr2[rownum]
                qua[n] = qual[rownum]
                n += 1
        barytime = tim * 1.0
        tcorr = tco * 1.0
        cadno = cad * 1.0
        fluxpixels = flu * 1.0
        errpixels = fer * 1.0
        poscorr1 = pc1 * 1.0
        poscorr2 = pc2 * 1.0
        qual = qua * 1.0

# initialize plot arrays

    if status == 0:
        t = numpy.array([],dtype='float64')
        fl = []; dx = []; dy = []; bg = []; fx = []; fy = []; fa = []; rs = []; ch = []
        for i in range(nsrc):
            fl.append(numpy.array([],dtype='float32'))
            dx.append(numpy.array([],dtype='float32'))
            dy.append(numpy.array([],dtype='float32'))

# Preparing fit data message

    if status == 0:
        progress = numpy.arange(nincl)
        if verbose:
            txt  = 'Preparing...'
            sys.stdout.write(txt)
            sys.stdout.flush()

# single processor version

    if status == 0:# and not cmdLine:
        oldtime = 0.0
        for rownum in xrange(numpy.min([80,len(barytime)])):
            try:
                if barytime[rownum] - oldtime > 0.5:
                    ftol = 1.0e-10; xtol = 1.0e-10
            except:
                pass
            args = (fluxpixels[rownum,:],errpixels[rownum,:],DATx,DATy,nsrc,border,xx,yy,PRFx,PRFy,splineInterpolation,
                    guess,ftol,xtol,focus,background,rownum,80,float(x[i]),float(y[i]),False)
            guess = PRFfits(args)
            ftol = ftolerance; xtol = tolerance; oldtime = barytime[rownum]

# Fit the time series: multi-processing

    if status == 0 and cmdLine:
        anslist = []
        cad1 = 0; cad2 = 50
        for i in range(int(nincl/50) + 1):
            try:
                fluxp = fluxpixels[cad1:cad2,:]
                errp = errpixels[cad1:cad2,:]
                progress = numpy.arange(cad1,cad2)
            except:
                fluxp = fluxpixels[cad1:nincl,:]
                errp = errpixels[cad1:nincl,:]
                progress = numpy.arange(cad1,nincl)
            try:
                args = itertools.izip(fluxp,errp,itertools.repeat(DATx),itertools.repeat(DATy),
                                      itertools.repeat(nsrc),itertools.repeat(border),itertools.repeat(xx),
                                      itertools.repeat(yy),itertools.repeat(PRFx),itertools.repeat(PRFy),
                                      itertools.repeat(splineInterpolation),itertools.repeat(guess),
                                      itertools.repeat(ftolerance),itertools.repeat(tolerance),
                                      itertools.repeat(focus),itertools.repeat(background),progress,
                                      itertools.repeat(numpy.arange(cad1,nincl)[-1]),
                                      itertools.repeat(float(x[0])),
                                      itertools.repeat(float(y[0])),itertools.repeat(True))
                p = multiprocessing.Pool()
                model = [0.0]
                model = p.imap(PRFfits,args,chunksize=1)
                p.close()
                p.join()
                cad1 += 50; cad2 += 50
                ans = array([array(item) for item in zip(*model)])
                try:
                    anslist = numpy.concatenate((anslist,ans.transpose()),axis=0)
                except:
                    anslist = ans.transpose()
                guess = anslist[-1]
                ans = anslist.transpose()
            except:
                pass

# single processor version

    if status == 0 and not cmdLine:
        oldtime = 0.0; ans = []
#        for rownum in xrange(1,10):
        for rownum in xrange(nincl):
            proctime = time.time()
            try:
                if barytime[rownum] - oldtime > 0.5:
                    ftol = 1.0e-10; xtol = 1.0e-10
            except:
                pass
            args = (fluxpixels[rownum,:],errpixels[rownum,:],DATx,DATy,nsrc,border,xx,yy,PRFx,PRFy,splineInterpolation,
                    guess,ftol,xtol,focus,background,rownum,nincl,float(x[0]),float(y[0]),True)
            guess = PRFfits(args)
            ans.append(guess)
            ftol = ftolerance; xtol = tolerance; oldtime = barytime[rownum]
        ans = array(ans).transpose()

# unpack the best fit parameters

    if status == 0:
        flux = []; OBJx = []; OBJy = []
        na = shape(ans)[1]
        for i in range(nsrc):
            flux.append(ans[i,:])
            OBJx.append(ans[nsrc+i,:])
            OBJy.append(ans[nsrc*2+i,:])
        try:
            bterms = border + 1
            if bterms == 1:
                b = ans[nsrc*3,:]
            else:
                b = array([])
                bkg = []
                for i in range(na):
                    bcoeff = array([ans[nsrc*3:nsrc*3+bterms,i],ans[nsrc*3+bterms:nsrc*3+bterms*2,i]])
                    bkg.append(kepfunc.polyval2d(xx,yy,bcoeff))
                    b = numpy.append(b,nanmean(bkg[-1].reshape(bkg[-1].size)))
        except:
            b = zeros((na))
        if focus:
            wx = ans[-3,:]; wy = ans[-2,:]; angle = ans[-1,:]
        else:
            wx = ones((na)); wy = ones((na)); angle = zeros((na))

# constuct model PRF in detector coordinates

    if status == 0:
        residual = []; chi2 = []
        for i in range(na):
            f = empty((nsrc))
            x = empty((nsrc))
            y = empty((nsrc))
            for j in range(nsrc):
                f[j] = flux[j][i]
                x[j] = OBJx[j][i]
                y[j] = OBJy[j][i]
            PRFfit = kepfunc.PRF2DET(f,x,y,DATx,DATy,wx[i],wy[i],angle[i],splineInterpolation)
            if background and bterms == 1:
                PRFfit = PRFfit + b[i]
            if background and bterms > 1:
                PRFfit = PRFfit + bkg[i]

# calculate residual of DATA - FIT

            xdim = shape(xx)[1]
            ydim = shape(yy)[0]
            DATimg = numpy.empty((ydim,xdim))
            n = 0
            for k in range(ydim):
                for j in range(xdim):
                    DATimg[k,j] = fluxpixels[i,n]
                    n += 1
            PRFres = DATimg - PRFfit
            residual.append(numpy.nansum(PRFres) / npix)

# calculate the sum squared difference between data and model

            chi2.append(abs(numpy.nansum(numpy.square(DATimg - PRFfit) / PRFfit)))

# load the output arrays

    if status == 0:
        otime = barytime - bjdref
        otimecorr = tcorr
        ocadenceno = cadno
        opos_corr1 = poscorr1
        opos_corr2 = poscorr2
        oquality = qual
        opsf_bkg = b
        opsf_focus1 = wx
        opsf_focus2 = wy
        opsf_rotation = angle
        opsf_residual = residual
        opsf_chi2 = chi2
        opsf_flux_err = numpy.empty((na)); opsf_flux_err.fill(numpy.nan)
        opsf_centr1_err = numpy.empty((na)); opsf_centr1_err.fill(numpy.nan)
        opsf_centr2_err = numpy.empty((na)); opsf_centr2_err.fill(numpy.nan)
        opsf_bkg_err = numpy.empty((na)); opsf_bkg_err.fill(numpy.nan)
        opsf_flux = []
        opsf_centr1 = []
        opsf_centr2 = []
        for i in range(nsrc):
            opsf_flux.append(flux[i])
            opsf_centr1.append(OBJx[i])
            opsf_centr2.append(OBJy[i])

# load the plot arrays

    if status == 0:
        t = barytime
        for i in range(nsrc):
            fl[i] = flux[i]
            dx[i] = OBJx[i]
            dy[i] = OBJy[i]
        bg = b
        fx = wx
        fy = wy
        fa = angle
        rs = residual
        ch = chi2

# construct output primary extension

    if status == 0:
        for j in range(nsrc):
            hdu0 = pyfits.PrimaryHDU()
            for i in range(len(cards0)):
                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
            status = kepkey.history(call,hdu0,outfile,logfile,verbose)
            outstr = HDUList(hdu0)

# construct output light curve extension

            col1 = Column(name='TIME',format='D',unit='BJD - 2454833',array=otime)
            col2 = Column(name='TIMECORR',format='E',unit='d',array=otimecorr)
            col3 = Column(name='CADENCENO',format='J',array=ocadenceno)
            col4 = Column(name='PSF_FLUX',format='E',unit='e-/s',array=opsf_flux[j])
            col5 = Column(name='PSF_FLUX_ERR',format='E',unit='e-/s',array=opsf_flux_err)
            col6 = Column(name='PSF_BKG',format='E',unit='e-/s/pix',array=opsf_bkg)
            col7 = Column(name='PSF_BKG_ERR',format='E',unit='e-/s',array=opsf_bkg_err)
            col8 = Column(name='PSF_CENTR1',format='E',unit='pixel',array=opsf_centr1[j])
            col9 = Column(name='PSF_CENTR1_ERR',format='E',unit='pixel',array=opsf_centr1_err)
            col10 = Column(name='PSF_CENTR2',format='E',unit='pixel',array=opsf_centr2[j])
            col11 = Column(name='PSF_CENTR2_ERR',format='E',unit='pixel',array=opsf_centr2_err)
            col12 = Column(name='PSF_FOCUS1',format='E',array=opsf_focus1)
            col13 = Column(name='PSF_FOCUS2',format='E',array=opsf_focus2)
            col14 = Column(name='PSF_ROTATION',format='E',unit='deg',array=opsf_rotation)
            col15 = Column(name='PSF_RESIDUAL',format='E',unit='e-/s',array=opsf_residual)
            col16 = Column(name='PSF_CHI2',format='E',array=opsf_chi2)
            col17 = Column(name='POS_CORR1',format='E',unit='pixel',array=opos_corr1)
            col18 = Column(name='POS_CORR2',format='E',unit='pixel',array=opos_corr2)
            col19 = Column(name='SAP_QUALITY',format='J',array=oquality)
            cols = ColDefs([col1,col2,col3,col4,col5,col6,col7,col8,col9,col10,col11,
                            col12,col13,col14,col15,col16,col17,col18,col19])
            hdu1 = new_table(cols)
            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']):
                    hdu1.header.update(cards1[i].key, cards1[i].value, cards1[i].comment)
            outstr.append(hdu1)

# construct output mask bitmap extension

            hdu2 = ImageHDU(maskmap)
            for i in range(len(cards2)):
                if cards2[i].key not in hdu2.header.keys():
                    hdu2.header.update(cards2[i].key, cards2[i].value, cards2[i].comment)
                else:
                    hdu2.header.cards[cards2[i].key].comment = cards2[i].comment
            outstr.append(hdu2)

# write output file

            outstr.writeto(outroot + '_' + str(j) + '.fits',checksum=True)

# close input structure

            status = kepio.closefits(struct,logfile,verbose)

# clean up x-axis unit

    if status == 0:
	barytime0 = float(int(t[0] / 100) * 100.0)
	t -= barytime0
        t = numpy.insert(t,[0],[t[0]])
        t = numpy.append(t,[t[-1]])
        xlab = 'BJD $-$ %d' % barytime0

# plot the light curves

    if status == 0:
        bg = numpy.insert(bg,[0],[-1.0e10])
        bg = numpy.append(bg,-1.0e10)
        fx = numpy.insert(fx,[0],[fx[0]])
        fx = numpy.append(fx,fx[-1])
        fy = numpy.insert(fy,[0],[fy[0]])
        fy = numpy.append(fy,fy[-1])
        fa = numpy.insert(fa,[0],[fa[0]])
        fa = numpy.append(fa,fa[-1])
        rs = numpy.insert(rs,[0],[-1.0e10])
        rs = numpy.append(rs,-1.0e10)
        ch = numpy.insert(ch,[0],[-1.0e10])
        ch = numpy.append(ch,-1.0e10)
        for i in range(nsrc):

# clean up y-axis units

            nrm = math.ceil(math.log10(numpy.nanmax(fl[i]))) - 1.0
            fl[i] /= 10**nrm
            if nrm == 0:
                ylab1 = 'e$^-$ s$^{-1}$'
            else:
                ylab1 = '10$^{%d}$ e$^-$ s$^{-1}$' % nrm
            xx = copy(dx[i])
            yy = copy(dy[i])
            ylab2 = 'offset (pixels)'

# data limits

            xmin = numpy.nanmin(t)
            xmax = numpy.nanmax(t)
            ymin1 = numpy.nanmin(fl[i])
            ymax1 = numpy.nanmax(fl[i])
            ymin2 = numpy.nanmin(xx)
            ymax2 = numpy.nanmax(xx)
            ymin3 = numpy.nanmin(yy)
            ymax3 = numpy.nanmax(yy)
            ymin4 = numpy.nanmin(bg[1:-1])
            ymax4 = numpy.nanmax(bg[1:-1])
            ymin5 = numpy.nanmin([numpy.nanmin(fx),numpy.nanmin(fy)])
            ymax5 = numpy.nanmax([numpy.nanmax(fx),numpy.nanmax(fy)])
            ymin6 = numpy.nanmin(fa[1:-1])
            ymax6 = numpy.nanmax(fa[1:-1])
            ymin7 = numpy.nanmin(rs[1:-1])
            ymax7 = numpy.nanmax(rs[1:-1])
            ymin8 = numpy.nanmin(ch[1:-1])
            ymax8 = numpy.nanmax(ch[1:-1])
            xr = xmax - xmin
            yr1 = ymax1 - ymin1
            yr2 = ymax2 - ymin2
            yr3 = ymax3 - ymin3
            yr4 = ymax4 - ymin4
            yr5 = ymax5 - ymin5
            yr6 = ymax6 - ymin6
            yr7 = ymax7 - ymin7
            yr8 = ymax8 - ymin8
            fl[i] = numpy.insert(fl[i],[0],[0.0])
            fl[i] = numpy.append(fl[i],0.0)

# plot style

            try:
                params = {'backend': 'png',
                          'axes.linewidth': 2.5,
                          'axes.labelsize': 24,
                          'axes.font': 'sans-serif',
                          'axes.fontweight' : 'bold',
                          'text.fontsize': 12,
                          'legend.fontsize': 12,
                          'xtick.labelsize': 12,
                          'ytick.labelsize': 12}
                pylab.rcParams.update(params)
            except:
                pass

# define size of plot on monitor screen

            pylab.figure(str(i+1) + ' ' + str(time.asctime(time.localtime())),figsize=[12,16])

# delete any fossil plots in the matplotlib window

            pylab.clf()

# position first axes inside the plotting window

            ax = pylab.axes([0.11,0.523,0.78,0.45])

# 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))

# no x-label

            pylab.setp(pylab.gca(),xticklabels=[])

# plot flux vs time

            ltime = numpy.array([],dtype='float64')
            ldata = numpy.array([],dtype='float32')
            dt = 0
            work1 = 2.0 * cadence / 86400
            for j in range(1,len(t)-1):
                dt = t[j] - t[j-1]
                if dt < work1:
                    ltime = numpy.append(ltime,t[j])
                    ldata = numpy.append(ldata,fl[i][j])
                else:
                    pylab.plot(ltime,ldata,color='#0000ff',linestyle='-',linewidth=1.0)
                    ltime = numpy.array([],dtype='float64')
                    ldata = numpy.array([],dtype='float32')
            pylab.plot(ltime,ldata,color='#0000ff',linestyle='-',linewidth=1.0)

# plot the fill color below data time series, with no data gaps

            pylab.fill(t,fl[i],fc='#ffff00',linewidth=0.0,alpha=0.2)

# define plot x and y limits

            pylab.xlim(xmin - xr * 0.01, xmax + xr * 0.01)
            if ymin1 - yr1 * 0.01 <= 0.0:
                pylab.ylim(1.0e-10, ymax1 + yr1 * 0.01)
            else:
                pylab.ylim(ymin1 - yr1 * 0.01, ymax1 + yr1 * 0.01)

# plot labels

#            pylab.xlabel(xlab, {'color' : 'k'})
            try:
                pylab.ylabel('Source (' + ylab1 + ')', {'color' : 'k'})
            except:
                ylab1 = '10**%d e-/s' % nrm
                pylab.ylabel('Source (' + ylab1 + ')', {'color' : 'k'})

# make grid on plot

            pylab.grid()

# plot centroid tracks - position second axes inside the plotting window

            if focus and background:
                axs = [0.11,0.433,0.78,0.09]
            elif background or focus:
                axs = [0.11,0.388,0.78,0.135]
            else:
                axs = [0.11,0.253,0.78,0.27]
            ax1 = pylab.axes(axs)

# 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))
            pylab.setp(pylab.gca(),xticklabels=[])

# plot dx vs time

            ltime = numpy.array([],dtype='float64')
            ldata = numpy.array([],dtype='float32')
            dt = 0
            work1 = 2.0 * cadence / 86400
            for j in range(1,len(t)-1):
                dt = t[j] - t[j-1]
                if dt < work1:
                    ltime = numpy.append(ltime,t[j])
                    ldata = numpy.append(ldata,xx[j-1])
                else:
                    ax1.plot(ltime,ldata,color='r',linestyle='-',linewidth=1.0)
                    ltime = numpy.array([],dtype='float64')
                    ldata = numpy.array([],dtype='float32')
            ax1.plot(ltime,ldata,color='r',linestyle='-',linewidth=1.0)

# define plot x and y limits

            pylab.xlim(xmin - xr * 0.01, xmax + xr * 0.01)
            pylab.ylim(ymin2 - yr2 * 0.03, ymax2 + yr2 * 0.03)

# plot labels

            ax1.set_ylabel('X-' + ylab2, color='k', fontsize=11)

# position second axes inside the plotting window

            ax2 = ax1.twinx()

# 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))
            pylab.setp(pylab.gca(),xticklabels=[])

# plot dy vs time

            ltime = numpy.array([],dtype='float64')
            ldata = numpy.array([],dtype='float32')
            dt = 0
            work1 = 2.0 * cadence / 86400
            for j in range(1,len(t)-1):
                dt = t[j] - t[j-1]
                if dt < work1:
                    ltime = numpy.append(ltime,t[j])
                    ldata = numpy.append(ldata,yy[j-1])
                else:
                    ax2.plot(ltime,ldata,color='g',linestyle='-',linewidth=1.0)
                    ltime = numpy.array([],dtype='float64')
                    ldata = numpy.array([],dtype='float32')
            ax2.plot(ltime,ldata,color='g',linestyle='-',linewidth=1.0)

# define plot y limits

            pylab.xlim(xmin - xr * 0.01, xmax + xr * 0.01)
            pylab.ylim(ymin3 - yr3 * 0.03, ymax3 + yr3 * 0.03)

# plot labels

            ax2.set_ylabel('Y-' + ylab2, color='k',fontsize=11)

# background - position third axes inside the plotting window

            if background and focus:
                axs = [0.11,0.343,0.78,0.09]
            if background and not focus:
                axs = [0.11,0.253,0.78,0.135]
            if background:
                ax1 = pylab.axes(axs)

# 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))
                pylab.setp(pylab.gca(),xticklabels=[])

# plot background vs time

                ltime = numpy.array([],dtype='float64')
                ldata = numpy.array([],dtype='float32')
                dt = 0
                work1 = 2.0 * cadence / 86400
                for j in range(1,len(t)-1):
                    dt = t[j] - t[j-1]
                    if dt < work1:
                        ltime = numpy.append(ltime,t[j])
                        ldata = numpy.append(ldata,bg[j])
                    else:
                        ax1.plot(ltime,ldata,color='#0000ff',linestyle='-',linewidth=1.0)
                        ltime = numpy.array([],dtype='float64')
                        ldata = numpy.array([],dtype='float32')
                ax1.plot(ltime,ldata,color='#0000ff',linestyle='-',linewidth=1.0)

# plot the fill color below data time series, with no data gaps

                pylab.fill(t,bg,fc='#ffff00',linewidth=0.0,alpha=0.2)

# define plot x and y limits

                pylab.xlim(xmin - xr * 0.01, xmax + xr * 0.01)
                pylab.ylim(ymin4 - yr4 * 0.03, ymax4 + yr4 * 0.03)

# plot labels

                ax1.set_ylabel('Background \n(e$^-$ s$^{-1}$ pix$^{-1}$)',
                               multialignment='center', color='k',fontsize=11)

# make grid on plot

                pylab.grid()

# position focus axes inside the plotting window

            if focus and background:
                axs = [0.11,0.253,0.78,0.09]
            if focus and not background:
                axs = [0.11,0.253,0.78,0.135]
            if focus:
                ax1 = pylab.axes(axs)

# 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))
                pylab.setp(pylab.gca(),xticklabels=[])

# plot x-axis PSF width vs time

                ltime = numpy.array([],dtype='float64')
                ldata = numpy.array([],dtype='float32')
                dt = 0
                work1 = 2.0 * cadence / 86400
                for j in range(1,len(t)-1):
                    dt = t[j] - t[j-1]
                    if dt < work1:
                        ltime = numpy.append(ltime,t[j])
                        ldata = numpy.append(ldata,fx[j])
                    else:
                        ax1.plot(ltime,ldata,color='r',linestyle='-',linewidth=1.0)
                        ltime = numpy.array([],dtype='float64')
                        ldata = numpy.array([],dtype='float32')
                ax1.plot(ltime,ldata,color='r',linestyle='-',linewidth=1.0)

# plot y-axis PSF width vs time

                ltime = numpy.array([],dtype='float64')
                ldata = numpy.array([],dtype='float32')
                dt = 0
                work1 = 2.0 * cadence / 86400
                for j in range(1,len(t)-1):
                    dt = t[j] - t[j-1]
                    if dt < work1:
                        ltime = numpy.append(ltime,t[j])
                        ldata = numpy.append(ldata,fy[j])
                    else:
                        ax1.plot(ltime,ldata,color='g',linestyle='-',linewidth=1.0)
                        ltime = numpy.array([],dtype='float64')
                        ldata = numpy.array([],dtype='float32')
                ax1.plot(ltime,ldata,color='g',linestyle='-',linewidth=1.0)

# define plot x and y limits

                pylab.xlim(xmin - xr * 0.01, xmax + xr * 0.01)
                pylab.ylim(ymin5 - yr5 * 0.03, ymax5 + yr5 * 0.03)

# plot labels

                ax1.set_ylabel('Pixel Scale\nFactor',
                               multialignment='center', color='k',fontsize=11)

# Focus rotation - position second axes inside the plotting window

                ax2 = ax1.twinx()

# 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))
                pylab.setp(pylab.gca(),xticklabels=[])

# plot dy vs time

                ltime = numpy.array([],dtype='float64')
                ldata = numpy.array([],dtype='float32')
                dt = 0
                work1 = 2.0 * cadence / 86400
                for j in range(1,len(t)-1):
                    dt = t[j] - t[j-1]
                    if dt < work1:
                        ltime = numpy.append(ltime,t[j])
                        ldata = numpy.append(ldata,fa[j])
                    else:
                        ax2.plot(ltime,ldata,color='#000080',linestyle='-',linewidth=1.0)
                        ltime = numpy.array([],dtype='float64')
                        ldata = numpy.array([],dtype='float32')
                ax2.plot(ltime,ldata,color='#000080',linestyle='-',linewidth=1.0)

# define plot y limits

                pylab.xlim(xmin - xr * 0.01, xmax + xr * 0.01)
                pylab.ylim(ymin6 - yr6 * 0.03, ymax6 + yr6 * 0.03)

# plot labels

                ax2.set_ylabel('Rotation (deg)', color='k',fontsize=11)

# fit residuals - position fifth axes inside the plotting window

            axs = [0.11,0.163,0.78,0.09]
            ax1 = pylab.axes(axs)

# 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))
            pylab.setp(pylab.gca(),xticklabels=[])

# plot residual vs time

            ltime = numpy.array([],dtype='float64')
            ldata = numpy.array([],dtype='float32')
            dt = 0
            work1 = 2.0 * cadence / 86400
            for j in range(1,len(t)-1):
                dt = t[j] - t[j-1]
                if dt < work1:
                    ltime = numpy.append(ltime,t[j])
                    ldata = numpy.append(ldata,rs[j])
                else:
                    ax1.plot(ltime,ldata,color='b',linestyle='-',linewidth=1.0)
                    ltime = numpy.array([],dtype='float64')
                    ldata = numpy.array([],dtype='float32')
            ax1.plot(ltime,ldata,color='b',linestyle='-',linewidth=1.0)

# plot the fill color below data time series, with no data gaps

            pylab.fill(t,rs,fc='#ffff00',linewidth=0.0,alpha=0.2)

# define plot x and y limits

            pylab.xlim(xmin - xr * 0.01, xmax + xr * 0.01)
            pylab.ylim(ymin7 - yr7 * 0.03, ymax7 + yr7 * 0.03)

# plot labels

            ax1.set_ylabel('Residual \n(e$^-$ s$^{-1}$)',
                           multialignment='center', color='k',fontsize=11)

# make grid on plot

            pylab.grid()

# fit chi square - position sixth axes inside the plotting window

            axs = [0.11,0.073,0.78,0.09]
            ax1 = pylab.axes(axs)

# 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))

# plot background vs time

            ltime = numpy.array([],dtype='float64')
            ldata = numpy.array([],dtype='float32')
            dt = 0
            work1 = 2.0 * cadence / 86400
            for j in range(1,len(t)-1):
                dt = t[j] - t[j-1]
                if dt < work1:
                    ltime = numpy.append(ltime,t[j])
                    ldata = numpy.append(ldata,ch[j])
                else:
                    ax1.plot(ltime,ldata,color='b',linestyle='-',linewidth=1.0)
                    ltime = numpy.array([],dtype='float64')
                    ldata = numpy.array([],dtype='float32')
            ax1.plot(ltime,ldata,color='b',linestyle='-',linewidth=1.0)

# plot the fill color below data time series, with no data gaps

            pylab.fill(t,ch,fc='#ffff00',linewidth=0.0,alpha=0.2)

# define plot x and y limits

            pylab.xlim(xmin - xr * 0.01, xmax + xr * 0.01)
            pylab.ylim(ymin8 - yr8 * 0.03, ymax8 + yr8 * 0.03)

# plot labels

            ax1.set_ylabel('$\chi^2$ (%d dof)' % (npix-len(guess)-1),color='k',fontsize=11)
            pylab.xlabel(xlab, {'color' : 'k'})

# make grid on plot

            pylab.grid()

# render plot

            if status == 0:
                pylab.savefig(outroot + '_' + str(i) + '.png')
            if status == 0 and plt:
                if cmdLine:
                    pylab.show(block=True)
                else:
                    pylab.ion()
                    pylab.plot([])
                    pylab.ioff()

# stop time

    kepmsg.clock('\n\nKEPPRFPHOT ended at',logfile,verbose)

    return
Esempio n. 21
0
def kepclip(infile,outfile,ranges,plot,plotcol,clobber,verbose,logfile,status,cmdLine=False): 

# startup parameters

    status = 0
    labelsize = 32
    ticksize = 24
    xsize = 18
    ysize = 10
    lcolor = '#0000ff'
    lwidth = 1.0
    fcolor = '#ffff00'
    falpha = 0.2

# log the call 

    hashline = '----------------------------------------------------------------------------'
    kepmsg.log(logfile,hashline,verbose)
    call = 'KEPCLIP -- '
    call += 'infile='+infile+' '
    call += 'outfile='+outfile+' '
    call += 'ranges='+ranges + ' '
    plotit = 'n'
    if (plot): plotit = 'y'
    call += 'plot='+plotit+ ' '
    call += 'plotcol='+plotcol+ ' '
    overwrite = 'n'
    if (clobber): overwrite = 'y'
    call += 'clobber='+overwrite+ ' '
    chatter = 'n'
    if (verbose): chatter = 'y'
    call += 'verbose='+chatter+' '
    call += 'logfile='+logfile
    kepmsg.log(logfile,call+'\n',verbose)

# start time

    kepmsg.clock('KEPCLIP started at',logfile,verbose)

# test log file

    logfile = kepmsg.test(logfile)

# clobber output file

    if clobber: status = kepio.clobber(outfile,logfile,verbose)
    if kepio.fileexists(outfile): 
	    message = 'ERROR -- KEPCLIP: ' + outfile + ' exists. Use --clobber'
	    status = kepmsg.err(logfile,message,verbose)

# time ranges for region

    if status == 0:
        t1 = []; t2 = []
        t1, t2, status = kepio.timeranges(ranges,logfile,verbose)

# open input file

    if status == 0:
        instr, status = kepio.openfits(infile,'readonly',logfile,verbose)
        tstart, tstop, bjdref, cadence, status = kepio.timekeys(instr,infile,logfile,verbose,status)
    if status == 0:
        try:
            work = instr[0].header['FILEVER']
            cadenom = 1.0
        except:
            cadenom = cadence

# fudge non-compliant FITS keywords with no values

    if status == 0:
        instr = kepkey.emptykeys(instr,file,logfile,verbose)

# input data

    if status == 0:
        table = instr[1].data

# read time and flux columns

    if status == 0:
        barytime, status = kepio.readtimecol(infile,table,logfile,verbose)
    if status == 0:
        flux, status = kepio.readfitscol(infile,table,plotcol,logfile,verbose)
    if status == 0:
        barytime = barytime + bjdref
        if 'flux' in plotcol.lower():
            flux = flux / cadenom

# filter input data table

    if status == 0:
        naxis2 = 0
        work1 = array([],'float64')
        work2 = array([],'float32')
        for i in range(len(barytime)):
            if (numpy.isfinite(barytime[i]) and numpy.isfinite(flux[i]) and flux[i] != 0.0):
                reject = False
                for j in range(len(t1)):
                    if (barytime[i] >= t1[j] and barytime[i] <= t2[j]):
                        reject = True
                if not reject:
                    table[naxis2] = table[i]
                    work1 = append(work1,barytime[i])
                    work2 = append(work2,flux[i])
                    naxis2 += 1

# comment keyword in output file

    if status == 0:
        status = kepkey.history(call,instr[0],outfile,logfile,verbose)

# write output file

    if status == 0:
        instr[1].data = table[:naxis2]
        comment = 'NaN cadences removed from data'
        status = kepkey.new('NANCLEAN',True,comment,instr[1],outfile,logfile,verbose)
        instr.writeto(outfile)
    
# clean up x-axis unit

    if status == 0:
	barytime0 = float(int(tstart / 100) * 100.0)
	barytime = work1 - barytime0
        xlab = 'BJD $-$ %d' % barytime0

# clean up y-axis units

    if status == 0:
        try:
            nrm = len(str(int(work2.max())))-1
        except:
            nrm = 0
	flux = work2 / 10**nrm
	ylab = '10$^%d$ e$^-$ s$^{-1}$' % nrm

# data limits

	xmin = barytime.min()
	xmax = barytime.max()
	ymin = flux.min()
	ymax = flux.max()
	xr = xmax - xmin
	yr = ymax - ymin

# plotting arguments

    if status == 0 and plot:
        try:
            params = {'backend': 'png',
                      'axes.linewidth': 2.5,
                      'axes.labelsize': labelsize,
                      'axes.font': 'sans-serif',
                      'axes.fontweight' : 'bold',
                      'text.fontsize': 12,
                      'legend.fontsize': 12,
                      'xtick.labelsize': ticksize,
                      'ytick.labelsize': ticksize}
            rcParams.update(params)
        except:
            print 'ERROR -- KEPCLIP: install latex for scientific plotting'
            status = 1

# clear window, plot box

    if status == 0 and plot:
        pylab.figure(figsize=[xsize,ysize])
        pylab.clf()
	ax = pylab.axes([0.05,0.1,0.94,0.88])

# force tick labels to be absolute rather than relative

        pylab.gca().xaxis.set_major_formatter(pylab.ScalarFormatter(useOffset=False))
        pylab.gca().yaxis.set_major_formatter(pylab.ScalarFormatter(useOffset=False))

# rotate y labels by 90 deg

        labels = ax.get_yticklabels()
        setp(labels, 'rotation', 90, fontsize=12)

# plot line data

	ltime = [barytime[0]]; ldata = [flux[0]]
	for i in range(1,len(flux)):
            if (barytime[i-1] > barytime[i] - 0.025):
                ltime.append(barytime[i])
                ldata.append(flux[i])
            else:
                ltime = array(ltime, dtype=float64)
                ldata = array(ldata, dtype=float64)
                pylab.plot(ltime,ldata,color=lcolor,linestyle='-',linewidth=lwidth)
                ltime = []; ldata = []
	ltime = array(ltime, dtype=float64)
	ldata = array(ldata, dtype=float64)
	pylab.plot(ltime,ldata,color=lcolor,linestyle='-',linewidth=lwidth)

# plot fill data

        barytime = insert(barytime,[0],[barytime[0]]) 
        barytime = append(barytime,[barytime[-1]])
        flux = insert(flux,[0],[0.0]) 
        flux = append(flux,[0.0])
	fill(barytime,flux,fc=fcolor,linewidth=0.0,alpha=falpha)
	xlim(xmin-xr*0.01,xmax+xr*0.01)
	if ymin-yr*0.01 <= 0.0:
            ylim(1.0e-10,ymax+yr*0.01)
	else:
            ylim(ymin-yr*0.01,ymax+yr*0.01)
	xlabel(xlab, {'color' : 'k'})
	ylabel(ylab, {'color' : 'k'})
	grid()

# render plot

    if status == 0 and plot:
        if cmdLine: 
            pylab.show()
        else: 
            pylab.ion()
            pylab.plot([])
            pylab.ioff()
	
# close input file

    if status == 0:
        status = kepio.closefits(instr,logfile,verbose)	    

# end time

    if (status == 0):
	    message = 'KEPCLIP completed at'
    else:
	    message = '\nKEPCLIP aborted at'
    kepmsg.clock(message,logfile,verbose)
Esempio n. 22
0
def kepprfphot(infile,outroot,columns,rows,fluxes,border,background,focus,prfdir,ranges,
               tolerance,ftolerance,qualflags,plt,clobber,verbose,logfile,status,cmdLine=False): 

# input arguments

    status = 0
    seterr(all="ignore") 

# log the call 

    hashline = '----------------------------------------------------------------------------'
    kepmsg.log(logfile,hashline,verbose)
    call = 'KEPPRFPHOT -- '
    call += 'infile='+infile+' '
    call += 'outroot='+outroot+' '
    call += 'columns='+columns+' '
    call += 'rows='+rows+' '
    call += 'fluxes='+fluxes+' '
    call += 'border='+str(border)+' '
    bground = 'n'
    if (background): bground = 'y'
    call += 'background='+bground+' '
    focs = 'n'
    if (focus): focs = 'y'
    call += 'focus='+focs+' '
    call += 'prfdir='+prfdir+' '
    call += 'ranges='+ranges+' '
    call += 'xtol='+str(tolerance)+' '
    call += 'ftol='+str(ftolerance)+' '
    quality = 'n'
    if (qualflags): quality = 'y'
    call += 'qualflags='+quality+' '
    plotit = 'n'
    if (plt): 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)

# test log file

    logfile = kepmsg.test(logfile)

# start time

    kepmsg.clock('KEPPRFPHOT started at',logfile,verbose)

# number of sources

    if status == 0:
        work = fluxes.strip()
        work = re.sub(' ',',',work)
        work = re.sub(';',',',work)
        nsrc = len(work.split(','))

# construct inital guess vector for fit 

    if status == 0:
        guess = []
        try:
            f = fluxes.strip().split(',')
            x = columns.strip().split(',')
            y = rows.strip().split(',')
            for i in range(len(f)):
                f[i] = float(f[i])
        except:
            f = fluxes
            x = columns
            y = rows
        nsrc = len(f)
        for i in range(nsrc):
            try:
                guess.append(float(f[i]))
            except:
                message = 'ERROR -- KEPPRF: Fluxes must be floating point numbers'
                status = kepmsg.err(logfile,message,verbose)
        if status == 0:
            if len(x) != nsrc or len(y) != nsrc:
                message = 'ERROR -- KEPFIT:FITMULTIPRF: Guesses for rows, columns and '
                message += 'fluxes must have the same number of sources'
                status = kepmsg.err(logfile,message,verbose)
        if status == 0:
            for i in range(nsrc):
                try:
                    guess.append(float(x[i]))
                except:
                    message = 'ERROR -- KEPPRF: Columns must be floating point numbers'
                    status = kepmsg.err(logfile,message,verbose)
        if status == 0:
            for i in range(nsrc):
                try:
                    guess.append(float(y[i]))
                except:
                    message = 'ERROR -- KEPPRF: Rows must be floating point numbers'
                    status = kepmsg.err(logfile,message,verbose)
        if status == 0 and background:
            if border == 0:
                guess.append(0.0)
            else:
                for i in range((border+1)*2):
                    guess.append(0.0)
        if status == 0 and focus:
            guess.append(1.0); guess.append(1.0); guess.append(0.0)

# clobber output file

    for i in range(nsrc):
        outfile = '%s_%d.fits' % (outroot, i)
        if clobber: status = kepio.clobber(outfile,logfile,verbose)
        if kepio.fileexists(outfile): 
            message = 'ERROR -- KEPPRFPHOT: ' + outfile + ' exists. Use --clobber'
            status = kepmsg.err(logfile,message,verbose)

# open TPF FITS file

    if status == 0:
        try:
            kepid, channel, skygroup, module, output, quarter, season, \
                ra, dec, column, row, kepmag, xdim, ydim, barytime, status = \
                kepio.readTPF(infile,'TIME',logfile,verbose)
        except:
            message = 'ERROR -- KEPPRFPHOT: is %s a Target Pixel File? ' % infile
            status = kepmsg.err(logfile,message,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, poscorr1, status = \
            kepio.readTPF(infile,'POS_CORR1',logfile,verbose)
        if status != 0:
            poscorr1 = numpy.zeros((len(barytime)),dtype='float32')
            poscorr1[:] = numpy.nan
            status = 0
    if status == 0:
        kepid, channel, skygroup, module, output, quarter, season, \
            ra, dec, column, row, kepmag, xdim, ydim, poscorr2, status = \
            kepio.readTPF(infile,'POS_CORR2',logfile,verbose)
        if status != 0:
            poscorr2 = numpy.zeros((len(barytime)),dtype='float32')
            poscorr2[:] = numpy.nan
            status = 0
    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)
    if status == 0:
        struct, status = kepio.openfits(infile,'readonly',logfile,verbose)
    if status == 0:
        tstart, tstop, bjdref, cadence, status = kepio.timekeys(struct,infile,logfile,verbose,status)

# input file keywords and mask map

    if status == 0:
        cards0 = struct[0].header.cards
        cards1 = struct[1].header.cards
        cards2 = struct[2].header.cards
        maskmap = copy(struct[2].data)
        npix = numpy.size(numpy.nonzero(maskmap)[0])

# print target data

    if status == 0 and verbose:
        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('')

# determine suitable PRF calibration file

    if status == 0:
        if int(module) < 10:
            prefix = 'kplr0'
        else:
            prefix = 'kplr'
        prfglob = prfdir + '/' + prefix + str(module) + '.' + str(output) + '*' + '_prf.fits'
        try:
            prffile = glob.glob(prfglob)[0]
        except:
            message = 'ERROR -- KEPPRFPHOT: No PRF file found in ' + prfdir
            status = kepmsg.err(logfile,message,verbose)

# read PRF images

    if status == 0:
        prfn = [0,0,0,0,0]
        crpix1p = numpy.zeros((5),dtype='float32')
        crpix2p = numpy.zeros((5),dtype='float32')
        crval1p = numpy.zeros((5),dtype='float32')
        crval2p = numpy.zeros((5),dtype='float32')
        cdelt1p = numpy.zeros((5),dtype='float32')
        cdelt2p = numpy.zeros((5),dtype='float32')
        for i in range(5):
            prfn[i], crpix1p[i], crpix2p[i], crval1p[i], crval2p[i], cdelt1p[i], cdelt2p[i], status \
                = kepio.readPRFimage(prffile,i+1,logfile,verbose)    
        PRFx = arange(0.5,shape(prfn[0])[1]+0.5)
        PRFy = arange(0.5,shape(prfn[0])[0]+0.5)
        PRFx = (PRFx - size(PRFx) / 2) * cdelt1p[0]
        PRFy = (PRFy - size(PRFy) / 2) * cdelt2p[0]

# interpolate the calibrated PRF shape to the target position

    if status == 0:
        prf = zeros(shape(prfn[0]),dtype='float32')
        prfWeight = zeros((5),dtype='float32')
        for i in range(5):
            prfWeight[i] = sqrt((column - crval1p[i])**2 + (row - crval2p[i])**2)
            if prfWeight[i] == 0.0:
                prfWeight[i] = 1.0e6
            prf = prf + prfn[i] / prfWeight[i]
        prf = prf / nansum(prf)
        prf = prf / cdelt1p[0] / cdelt2p[0]

# location of the data image centered on the PRF image (in PRF pixel units)

    if status == 0:
        prfDimY = ydim / cdelt1p[0]
        prfDimX = xdim / cdelt2p[0]
        PRFy0 = (shape(prf)[0] - prfDimY) / 2
        PRFx0 = (shape(prf)[1] - prfDimX) / 2

# construct input pixel image

    if status == 0:
        DATx = arange(column,column+xdim)
        DATy = arange(row,row+ydim)

# interpolation function over the PRF

    if status == 0:
        splineInterpolation = scipy.interpolate.RectBivariateSpline(PRFx,PRFy,prf,kx=3,ky=3)

# construct mesh for background model

    if status == 0:
        bx = numpy.arange(1.,float(xdim+1))
        by = numpy.arange(1.,float(ydim+1))
        xx, yy = numpy.meshgrid(numpy.linspace(bx.min(), bx.max(), xdim),
                                numpy.linspace(by.min(), by.max(), ydim))

# Get time ranges for new photometry, flag good data

    if status == 0:
        barytime += bjdref
        tstart,tstop,status = kepio.timeranges(ranges,logfile,verbose)
        incl = numpy.zeros((len(barytime)),dtype='int')
        for rownum in range(len(barytime)):
            for winnum in range(len(tstart)):
                if barytime[rownum] >= tstart[winnum] and \
                        barytime[rownum] <= tstop[winnum] and \
                        (qual[rownum] == 0 or qualflags) and \
                        numpy.isfinite(barytime[rownum]) and \
                        numpy.isfinite(numpy.nansum(fluxpixels[rownum,:])):
                    incl[rownum] = 1
        if not numpy.in1d(1,incl):
            message = 'ERROR -- KEPPRFPHOT: No legal data within the range ' + ranges
            status = kepmsg.err(logfile,message,verbose)

# filter out bad data

    if status == 0:
        n = 0
        nincl = (incl == 1).sum()
        tim = zeros((nincl),'float64')
        tco = zeros((nincl),'float32')
        cad = zeros((nincl),'float32')
        flu = zeros((nincl,len(fluxpixels[0])),'float32')
        fer = zeros((nincl,len(fluxpixels[0])),'float32')
        pc1 = zeros((nincl),'float32')
        pc2 = zeros((nincl),'float32')
        qua = zeros((nincl),'float32')
        for rownum in range(len(barytime)):
            if incl[rownum] == 1:
                tim[n] = barytime[rownum]
                tco[n] = tcorr[rownum]
                cad[n] = cadno[rownum]
                flu[n,:] = fluxpixels[rownum]
                fer[n,:] = errpixels[rownum]
                pc1[n] = poscorr1[rownum]
                pc2[n] = poscorr2[rownum]
                qua[n] = qual[rownum]
                n += 1
        barytime = tim * 1.0
        tcorr = tco * 1.0
        cadno = cad * 1.0
        fluxpixels = flu * 1.0
        errpixels = fer * 1.0
        poscorr1 = pc1 * 1.0
        poscorr2 = pc2 * 1.0
        qual = qua * 1.0

# initialize plot arrays

    if status == 0:
        t = numpy.array([],dtype='float64')
        fl = []; dx = []; dy = []; bg = []; fx = []; fy = []; fa = []; rs = []; ch = []
        for i in range(nsrc):
            fl.append(numpy.array([],dtype='float32'))
            dx.append(numpy.array([],dtype='float32'))
            dy.append(numpy.array([],dtype='float32'))

# Preparing fit data message

    if status == 0:
        progress = numpy.arange(nincl)
        if verbose:
            txt  = 'Preparing...'
            sys.stdout.write(txt)
            sys.stdout.flush()

# single processor version

    if status == 0:# and not cmdLine:
        oldtime = 0.0
        for rownum in range(numpy.min([80,len(barytime)])):
            try:
                if barytime[rownum] - oldtime > 0.5:
                    ftol = 1.0e-10; xtol = 1.0e-10
            except:
                pass
            args = (fluxpixels[rownum,:],errpixels[rownum,:],DATx,DATy,nsrc,border,xx,yy,PRFx,PRFy,splineInterpolation,
                    guess,ftol,xtol,focus,background,rownum,80,float(x[i]),float(y[i]),False)
            guess = PRFfits(args)
            ftol = ftolerance; xtol = tolerance; oldtime = barytime[rownum]

# Fit the time series: multi-processing

    if status == 0 and cmdLine:
        anslist = []
        cad1 = 0; cad2 = 50
        for i in range(int(nincl/50) + 1):
            try:
                fluxp = fluxpixels[cad1:cad2,:]
                errp = errpixels[cad1:cad2,:]
                progress = numpy.arange(cad1,cad2)
            except:
                fluxp = fluxpixels[cad1:nincl,:]
                errp = errpixels[cad1:nincl,:]
                progress = numpy.arange(cad1,nincl)
            try:
                args = zip(fluxp,errp,itertools.repeat(DATx),itertools.repeat(DATy),
                                      itertools.repeat(nsrc),itertools.repeat(border),itertools.repeat(xx),
                                      itertools.repeat(yy),itertools.repeat(PRFx),itertools.repeat(PRFy),
                                      itertools.repeat(splineInterpolation),itertools.repeat(guess),
                                      itertools.repeat(ftolerance),itertools.repeat(tolerance),
                                      itertools.repeat(focus),itertools.repeat(background),progress,
                                      itertools.repeat(numpy.arange(cad1,nincl)[-1]),
                                      itertools.repeat(float(x[0])),
                                      itertools.repeat(float(y[0])),itertools.repeat(True))
                p = multiprocessing.Pool()
                model = [0.0]
                model = p.imap(PRFfits,args,chunksize=1)
                p.close()
                p.join()
                cad1 += 50; cad2 += 50
                ans = array([array(item) for item in zip(*model)])
                try:
                    anslist = numpy.concatenate((anslist,ans.transpose()),axis=0)
                except:
                    anslist = ans.transpose()
                guess = anslist[-1]
                ans = anslist.transpose()
            except:
                pass
            
# single processor version

    if status == 0 and not cmdLine:
        oldtime = 0.0; ans = []
#        for rownum in xrange(1,10):
        for rownum in range(nincl):
            proctime = time.time()
            try:
                if barytime[rownum] - oldtime > 0.5:
                    ftol = 1.0e-10; xtol = 1.0e-10
            except:
                pass
            args = (fluxpixels[rownum,:],errpixels[rownum,:],DATx,DATy,nsrc,border,xx,yy,PRFx,PRFy,splineInterpolation,
                    guess,ftol,xtol,focus,background,rownum,nincl,float(x[0]),float(y[0]),True)
            guess = PRFfits(args)
            ans.append(guess)
            ftol = ftolerance; xtol = tolerance; oldtime = barytime[rownum]
        ans = array(ans).transpose()

# unpack the best fit parameters

    if status == 0:
        flux = []; OBJx = []; OBJy = []
        na = shape(ans)[1]
        for i in range(nsrc):
            flux.append(ans[i,:])
            OBJx.append(ans[nsrc+i,:])
            OBJy.append(ans[nsrc*2+i,:])
        try:
            bterms = border + 1
            if bterms == 1:
                b = ans[nsrc*3,:]
            else:
                b = array([])
                bkg = []
                for i in range(na):
                    bcoeff = array([ans[nsrc*3:nsrc*3+bterms,i],ans[nsrc*3+bterms:nsrc*3+bterms*2,i]])
                    bkg.append(kepfunc.polyval2d(xx,yy,bcoeff))
                    b = numpy.append(b,nanmean(bkg[-1].reshape(bkg[-1].size)))
        except:
            b = zeros((na))
        if focus:
            wx = ans[-3,:]; wy = ans[-2,:]; angle = ans[-1,:]
        else:
            wx = ones((na)); wy = ones((na)); angle = zeros((na))
        
# constuct model PRF in detector coordinates

    if status == 0:
        residual = []; chi2 = []
        for i in range(na):
            f = empty((nsrc))
            x = empty((nsrc))
            y = empty((nsrc))
            for j in range(nsrc):
                f[j] = flux[j][i]
                x[j] = OBJx[j][i]
                y[j] = OBJy[j][i]
            PRFfit = kepfunc.PRF2DET(f,x,y,DATx,DATy,wx[i],wy[i],angle[i],splineInterpolation)
            if background and bterms == 1:
                PRFfit = PRFfit + b[i]
            if background and bterms > 1:
                PRFfit = PRFfit + bkg[i]

# calculate residual of DATA - FIT

            xdim = shape(xx)[1]
            ydim = shape(yy)[0]
            DATimg = numpy.empty((ydim,xdim))
            n = 0
            for k in range(ydim):
                for j in range(xdim):
                    DATimg[k,j] = fluxpixels[i,n]
                    n += 1
            PRFres = DATimg - PRFfit
            residual.append(numpy.nansum(PRFres) / npix)
    
# calculate the sum squared difference between data and model

            chi2.append(abs(numpy.nansum(numpy.square(DATimg - PRFfit) / PRFfit)))

# load the output arrays

    if status == 0:
        otime = barytime - bjdref
        otimecorr = tcorr
        ocadenceno = cadno
        opos_corr1 = poscorr1
        opos_corr2 = poscorr2
        oquality = qual
        opsf_bkg = b
        opsf_focus1 = wx
        opsf_focus2 = wy
        opsf_rotation = angle
        opsf_residual = residual
        opsf_chi2 = chi2
        opsf_flux_err = numpy.empty((na)); opsf_flux_err.fill(numpy.nan)
        opsf_centr1_err = numpy.empty((na)); opsf_centr1_err.fill(numpy.nan)
        opsf_centr2_err = numpy.empty((na)); opsf_centr2_err.fill(numpy.nan)
        opsf_bkg_err = numpy.empty((na)); opsf_bkg_err.fill(numpy.nan)
        opsf_flux = []
        opsf_centr1 = []
        opsf_centr2 = []
        for i in range(nsrc):
            opsf_flux.append(flux[i])
            opsf_centr1.append(OBJx[i])
            opsf_centr2.append(OBJy[i])

# load the plot arrays

    if status == 0:
        t = barytime
        for i in range(nsrc):
            fl[i] = flux[i]
            dx[i] = OBJx[i]
            dy[i] = OBJy[i]
        bg = b
        fx = wx
        fy = wy
        fa = angle
        rs = residual
        ch = chi2
                
# construct output primary extension

    if status == 0:
        for j in range(nsrc):
            hdu0 = pyfits.PrimaryHDU()
            for i in range(len(cards0)):
                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
            status = kepkey.history(call,hdu0,outfile,logfile,verbose)
            outstr = HDUList(hdu0)

# construct output light curve extension

            col1 = Column(name='TIME',format='D',unit='BJD - 2454833',array=otime)
            col2 = Column(name='TIMECORR',format='E',unit='d',array=otimecorr)
            col3 = Column(name='CADENCENO',format='J',array=ocadenceno)
            col4 = Column(name='PSF_FLUX',format='E',unit='e-/s',array=opsf_flux[j])
            col5 = Column(name='PSF_FLUX_ERR',format='E',unit='e-/s',array=opsf_flux_err)
            col6 = Column(name='PSF_BKG',format='E',unit='e-/s/pix',array=opsf_bkg)
            col7 = Column(name='PSF_BKG_ERR',format='E',unit='e-/s',array=opsf_bkg_err)
            col8 = Column(name='PSF_CENTR1',format='E',unit='pixel',array=opsf_centr1[j])
            col9 = Column(name='PSF_CENTR1_ERR',format='E',unit='pixel',array=opsf_centr1_err)
            col10 = Column(name='PSF_CENTR2',format='E',unit='pixel',array=opsf_centr2[j])
            col11 = Column(name='PSF_CENTR2_ERR',format='E',unit='pixel',array=opsf_centr2_err)
            col12 = Column(name='PSF_FOCUS1',format='E',array=opsf_focus1)
            col13 = Column(name='PSF_FOCUS2',format='E',array=opsf_focus2)
            col14 = Column(name='PSF_ROTATION',format='E',unit='deg',array=opsf_rotation)
            col15 = Column(name='PSF_RESIDUAL',format='E',unit='e-/s',array=opsf_residual)
            col16 = Column(name='PSF_CHI2',format='E',array=opsf_chi2)
            col17 = Column(name='POS_CORR1',format='E',unit='pixel',array=opos_corr1)
            col18 = Column(name='POS_CORR2',format='E',unit='pixel',array=opos_corr2)
            col19 = Column(name='SAP_QUALITY',format='J',array=oquality)
            cols = ColDefs([col1,col2,col3,col4,col5,col6,col7,col8,col9,col10,col11,
                            col12,col13,col14,col15,col16,col17,col18,col19])
            hdu1 = new_table(cols)
            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']):
                    hdu1.header.update(cards1[i].key, cards1[i].value, cards1[i].comment)
            outstr.append(hdu1)

# construct output mask bitmap extension

            hdu2 = ImageHDU(maskmap)
            for i in range(len(cards2)):
                if cards2[i].key not in list(hdu2.header.keys()):
                    hdu2.header.update(cards2[i].key, cards2[i].value, cards2[i].comment)
                else:
                    hdu2.header.cards[cards2[i].key].comment = cards2[i].comment
            outstr.append(hdu2)

# write output file

            outstr.writeto(outroot + '_' + str(j) + '.fits',checksum=True)

# close input structure

            status = kepio.closefits(struct,logfile,verbose)            

# clean up x-axis unit

    if status == 0:
        barytime0 = float(int(t[0] / 100) * 100.0)
        t -= barytime0
        t = numpy.insert(t,[0],[t[0]]) 
        t = numpy.append(t,[t[-1]])
        xlab = 'BJD $-$ %d' % barytime0

# plot the light curves

    if status == 0:
        bg = numpy.insert(bg,[0],[-1.0e10]) 
        bg = numpy.append(bg,-1.0e10)
        fx = numpy.insert(fx,[0],[fx[0]]) 
        fx = numpy.append(fx,fx[-1])
        fy = numpy.insert(fy,[0],[fy[0]]) 
        fy = numpy.append(fy,fy[-1])
        fa = numpy.insert(fa,[0],[fa[0]]) 
        fa = numpy.append(fa,fa[-1])
        rs = numpy.insert(rs,[0],[-1.0e10]) 
        rs = numpy.append(rs,-1.0e10)
        ch = numpy.insert(ch,[0],[-1.0e10]) 
        ch = numpy.append(ch,-1.0e10)
        for i in range(nsrc):

# clean up y-axis units

            nrm = math.ceil(math.log10(numpy.nanmax(fl[i]))) - 1.0
            fl[i] /= 10**nrm
            if nrm == 0:
                ylab1 = 'e$^-$ s$^{-1}$'
            else:
                ylab1 = '10$^{%d}$ e$^-$ s$^{-1}$' % nrm
            xx = copy(dx[i])
            yy = copy(dy[i])
            ylab2 = 'offset (pixels)'
            
# data limits

            xmin = numpy.nanmin(t)
            xmax = numpy.nanmax(t)
            ymin1 = numpy.nanmin(fl[i])
            ymax1 = numpy.nanmax(fl[i])
            ymin2 = numpy.nanmin(xx)
            ymax2 = numpy.nanmax(xx)
            ymin3 = numpy.nanmin(yy)
            ymax3 = numpy.nanmax(yy)
            ymin4 = numpy.nanmin(bg[1:-1])
            ymax4 = numpy.nanmax(bg[1:-1])
            ymin5 = numpy.nanmin([numpy.nanmin(fx),numpy.nanmin(fy)])
            ymax5 = numpy.nanmax([numpy.nanmax(fx),numpy.nanmax(fy)])
            ymin6 = numpy.nanmin(fa[1:-1])
            ymax6 = numpy.nanmax(fa[1:-1])
            ymin7 = numpy.nanmin(rs[1:-1])
            ymax7 = numpy.nanmax(rs[1:-1])
            ymin8 = numpy.nanmin(ch[1:-1])
            ymax8 = numpy.nanmax(ch[1:-1])
            xr = xmax - xmin
            yr1 = ymax1 - ymin1
            yr2 = ymax2 - ymin2
            yr3 = ymax3 - ymin3
            yr4 = ymax4 - ymin4
            yr5 = ymax5 - ymin5
            yr6 = ymax6 - ymin6
            yr7 = ymax7 - ymin7
            yr8 = ymax8 - ymin8
            fl[i] = numpy.insert(fl[i],[0],[0.0]) 
            fl[i] = numpy.append(fl[i],0.0)

# plot style

            try:
                params = {'backend': 'png',
                          'axes.linewidth': 2.5,
                          'axes.labelsize': 24,
                          'axes.font': 'sans-serif',
                          'axes.fontweight' : 'bold',
                          'text.fontsize': 12,
                          'legend.fontsize': 12,
                          'xtick.labelsize': 12,
                          'ytick.labelsize': 12}
                pylab.rcParams.update(params)
            except:
                pass

# define size of plot on monitor screen

            pylab.figure(str(i+1) + ' ' + str(time.asctime(time.localtime())),figsize=[12,16])

# delete any fossil plots in the matplotlib window

            pylab.clf()

# position first axes inside the plotting window

            ax = pylab.axes([0.11,0.523,0.78,0.45])

# 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))

# no x-label

            pylab.setp(pylab.gca(),xticklabels=[])

# plot flux vs time

            ltime = numpy.array([],dtype='float64')
            ldata = numpy.array([],dtype='float32')
            dt = 0
            work1 = 2.0 * cadence / 86400
            for j in range(1,len(t)-1):
                dt = t[j] - t[j-1]
                if dt < work1:
                    ltime = numpy.append(ltime,t[j])
                    ldata = numpy.append(ldata,fl[i][j])
                else:
                    pylab.plot(ltime,ldata,color='#0000ff',linestyle='-',linewidth=1.0)
                    ltime = numpy.array([],dtype='float64')
                    ldata = numpy.array([],dtype='float32')
            pylab.plot(ltime,ldata,color='#0000ff',linestyle='-',linewidth=1.0)

# plot the fill color below data time series, with no data gaps

            pylab.fill(t,fl[i],fc='#ffff00',linewidth=0.0,alpha=0.2)

# define plot x and y limits

            pylab.xlim(xmin - xr * 0.01, xmax + xr * 0.01)
            if ymin1 - yr1 * 0.01 <= 0.0:
                pylab.ylim(1.0e-10, ymax1 + yr1 * 0.01)
            else:
                pylab.ylim(ymin1 - yr1 * 0.01, ymax1 + yr1 * 0.01)
           
# plot labels

#            pylab.xlabel(xlab, {'color' : 'k'})
            try:
                pylab.ylabel('Source (' + ylab1 + ')', {'color' : 'k'})
            except:
                ylab1 = '10**%d e-/s' % nrm
                pylab.ylabel('Source (' + ylab1 + ')', {'color' : 'k'})

# make grid on plot

            pylab.grid()

# plot centroid tracks - position second axes inside the plotting window

            if focus and background:
                axs = [0.11,0.433,0.78,0.09]
            elif background or focus:
                axs = [0.11,0.388,0.78,0.135]
            else:
                axs = [0.11,0.253,0.78,0.27]
            ax1 = pylab.axes(axs)

# 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))
            pylab.setp(pylab.gca(),xticklabels=[])

# plot dx vs time

            ltime = numpy.array([],dtype='float64')
            ldata = numpy.array([],dtype='float32')
            dt = 0
            work1 = 2.0 * cadence / 86400
            for j in range(1,len(t)-1):
                dt = t[j] - t[j-1]
                if dt < work1:
                    ltime = numpy.append(ltime,t[j])
                    ldata = numpy.append(ldata,xx[j-1])
                else:
                    ax1.plot(ltime,ldata,color='r',linestyle='-',linewidth=1.0)
                    ltime = numpy.array([],dtype='float64')
                    ldata = numpy.array([],dtype='float32')
            ax1.plot(ltime,ldata,color='r',linestyle='-',linewidth=1.0)

# define plot x and y limits

            pylab.xlim(xmin - xr * 0.01, xmax + xr * 0.01)
            pylab.ylim(ymin2 - yr2 * 0.03, ymax2 + yr2 * 0.03)
           
# plot labels

            ax1.set_ylabel('X-' + ylab2, color='k', fontsize=11)

# position second axes inside the plotting window

            ax2 = ax1.twinx()

# 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))
            pylab.setp(pylab.gca(),xticklabels=[])

# plot dy vs time

            ltime = numpy.array([],dtype='float64')
            ldata = numpy.array([],dtype='float32')
            dt = 0
            work1 = 2.0 * cadence / 86400
            for j in range(1,len(t)-1):
                dt = t[j] - t[j-1]
                if dt < work1:
                    ltime = numpy.append(ltime,t[j])
                    ldata = numpy.append(ldata,yy[j-1])
                else:
                    ax2.plot(ltime,ldata,color='g',linestyle='-',linewidth=1.0)
                    ltime = numpy.array([],dtype='float64')
                    ldata = numpy.array([],dtype='float32')
            ax2.plot(ltime,ldata,color='g',linestyle='-',linewidth=1.0)

# define plot y limits

            pylab.xlim(xmin - xr * 0.01, xmax + xr * 0.01)
            pylab.ylim(ymin3 - yr3 * 0.03, ymax3 + yr3 * 0.03)
           
# plot labels

            ax2.set_ylabel('Y-' + ylab2, color='k',fontsize=11)

# background - position third axes inside the plotting window

            if background and focus:
                axs = [0.11,0.343,0.78,0.09]
            if background and not focus:
                axs = [0.11,0.253,0.78,0.135]
            if background:
                ax1 = pylab.axes(axs)

# 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))
                pylab.setp(pylab.gca(),xticklabels=[])

# plot background vs time

                ltime = numpy.array([],dtype='float64')
                ldata = numpy.array([],dtype='float32')
                dt = 0
                work1 = 2.0 * cadence / 86400
                for j in range(1,len(t)-1):
                    dt = t[j] - t[j-1]
                    if dt < work1:
                        ltime = numpy.append(ltime,t[j])
                        ldata = numpy.append(ldata,bg[j])
                    else:
                        ax1.plot(ltime,ldata,color='#0000ff',linestyle='-',linewidth=1.0)
                        ltime = numpy.array([],dtype='float64')
                        ldata = numpy.array([],dtype='float32')
                ax1.plot(ltime,ldata,color='#0000ff',linestyle='-',linewidth=1.0)

# plot the fill color below data time series, with no data gaps

                pylab.fill(t,bg,fc='#ffff00',linewidth=0.0,alpha=0.2)

# define plot x and y limits

                pylab.xlim(xmin - xr * 0.01, xmax + xr * 0.01)
                pylab.ylim(ymin4 - yr4 * 0.03, ymax4 + yr4 * 0.03)
           
# plot labels

                ax1.set_ylabel('Background \n(e$^-$ s$^{-1}$ pix$^{-1}$)', 
                               multialignment='center', color='k',fontsize=11)

# make grid on plot

                pylab.grid()

# position focus axes inside the plotting window

            if focus and background:
                axs = [0.11,0.253,0.78,0.09]
            if focus and not background:
                axs = [0.11,0.253,0.78,0.135]
            if focus:
                ax1 = pylab.axes(axs)

# 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))
                pylab.setp(pylab.gca(),xticklabels=[])

# plot x-axis PSF width vs time

                ltime = numpy.array([],dtype='float64')
                ldata = numpy.array([],dtype='float32')
                dt = 0
                work1 = 2.0 * cadence / 86400
                for j in range(1,len(t)-1):
                    dt = t[j] - t[j-1]
                    if dt < work1:
                        ltime = numpy.append(ltime,t[j])
                        ldata = numpy.append(ldata,fx[j])
                    else:
                        ax1.plot(ltime,ldata,color='r',linestyle='-',linewidth=1.0)
                        ltime = numpy.array([],dtype='float64')
                        ldata = numpy.array([],dtype='float32')
                ax1.plot(ltime,ldata,color='r',linestyle='-',linewidth=1.0)

# plot y-axis PSF width vs time

                ltime = numpy.array([],dtype='float64')
                ldata = numpy.array([],dtype='float32')
                dt = 0
                work1 = 2.0 * cadence / 86400
                for j in range(1,len(t)-1):
                    dt = t[j] - t[j-1]
                    if dt < work1:
                        ltime = numpy.append(ltime,t[j])
                        ldata = numpy.append(ldata,fy[j])
                    else:
                        ax1.plot(ltime,ldata,color='g',linestyle='-',linewidth=1.0)
                        ltime = numpy.array([],dtype='float64')
                        ldata = numpy.array([],dtype='float32')
                ax1.plot(ltime,ldata,color='g',linestyle='-',linewidth=1.0)

# define plot x and y limits

                pylab.xlim(xmin - xr * 0.01, xmax + xr * 0.01)
                pylab.ylim(ymin5 - yr5 * 0.03, ymax5 + yr5 * 0.03)
           
# plot labels

                ax1.set_ylabel('Pixel Scale\nFactor', 
                               multialignment='center', color='k',fontsize=11)

# Focus rotation - position second axes inside the plotting window

                ax2 = ax1.twinx()

# 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))
                pylab.setp(pylab.gca(),xticklabels=[])

# plot dy vs time

                ltime = numpy.array([],dtype='float64')
                ldata = numpy.array([],dtype='float32')
                dt = 0
                work1 = 2.0 * cadence / 86400
                for j in range(1,len(t)-1):
                    dt = t[j] - t[j-1]
                    if dt < work1:
                        ltime = numpy.append(ltime,t[j])
                        ldata = numpy.append(ldata,fa[j])
                    else:
                        ax2.plot(ltime,ldata,color='#000080',linestyle='-',linewidth=1.0)
                        ltime = numpy.array([],dtype='float64')
                        ldata = numpy.array([],dtype='float32')
                ax2.plot(ltime,ldata,color='#000080',linestyle='-',linewidth=1.0)

# define plot y limits
                
                pylab.xlim(xmin - xr * 0.01, xmax + xr * 0.01)
                pylab.ylim(ymin6 - yr6 * 0.03, ymax6 + yr6 * 0.03)
           
# plot labels

                ax2.set_ylabel('Rotation (deg)', color='k',fontsize=11)

# fit residuals - position fifth axes inside the plotting window

            axs = [0.11,0.163,0.78,0.09]
            ax1 = pylab.axes(axs)

# 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))
            pylab.setp(pylab.gca(),xticklabels=[])

# plot residual vs time

            ltime = numpy.array([],dtype='float64')
            ldata = numpy.array([],dtype='float32')
            dt = 0
            work1 = 2.0 * cadence / 86400
            for j in range(1,len(t)-1):
                dt = t[j] - t[j-1]
                if dt < work1:
                    ltime = numpy.append(ltime,t[j])
                    ldata = numpy.append(ldata,rs[j])
                else:
                    ax1.plot(ltime,ldata,color='b',linestyle='-',linewidth=1.0)
                    ltime = numpy.array([],dtype='float64')
                    ldata = numpy.array([],dtype='float32')
            ax1.plot(ltime,ldata,color='b',linestyle='-',linewidth=1.0)

# plot the fill color below data time series, with no data gaps

            pylab.fill(t,rs,fc='#ffff00',linewidth=0.0,alpha=0.2)

# define plot x and y limits

            pylab.xlim(xmin - xr * 0.01, xmax + xr * 0.01)
            pylab.ylim(ymin7 - yr7 * 0.03, ymax7 + yr7 * 0.03)
           
# plot labels

            ax1.set_ylabel('Residual \n(e$^-$ s$^{-1}$)', 
                           multialignment='center', color='k',fontsize=11)

# make grid on plot

            pylab.grid()

# fit chi square - position sixth axes inside the plotting window

            axs = [0.11,0.073,0.78,0.09]
            ax1 = pylab.axes(axs)

# 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))

# plot background vs time

            ltime = numpy.array([],dtype='float64')
            ldata = numpy.array([],dtype='float32')
            dt = 0
            work1 = 2.0 * cadence / 86400
            for j in range(1,len(t)-1):
                dt = t[j] - t[j-1]
                if dt < work1:
                    ltime = numpy.append(ltime,t[j])
                    ldata = numpy.append(ldata,ch[j])
                else:
                    ax1.plot(ltime,ldata,color='b',linestyle='-',linewidth=1.0)
                    ltime = numpy.array([],dtype='float64')
                    ldata = numpy.array([],dtype='float32')
            ax1.plot(ltime,ldata,color='b',linestyle='-',linewidth=1.0)

# plot the fill color below data time series, with no data gaps

            pylab.fill(t,ch,fc='#ffff00',linewidth=0.0,alpha=0.2)

# define plot x and y limits

            pylab.xlim(xmin - xr * 0.01, xmax + xr * 0.01)
            pylab.ylim(ymin8 - yr8 * 0.03, ymax8 + yr8 * 0.03)
           
# plot labels

            ax1.set_ylabel('$\chi^2$ (%d dof)' % (npix-len(guess)-1),color='k',fontsize=11)
            pylab.xlabel(xlab, {'color' : 'k'})

# make grid on plot

            pylab.grid()

# render plot

            if status == 0:
                pylab.savefig(outroot + '_' + str(i) + '.png')
            if status == 0 and plt:
                if cmdLine: 
                    pylab.show(block=True)
                else: 
                    pylab.ion()
                    pylab.plot([])
                    pylab.ioff()
        
# stop time

    kepmsg.clock('\n\nKEPPRFPHOT ended at',logfile,verbose)

    return
Esempio n. 23
0
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)
Esempio n. 24
0
def kepcotrendsc(infile, outfile, bvfile, listbv, fitmethod, fitpower, iterate,
                 sigma, maskfile, scinterp, plot, clobber, verbose, logfile,
                 status):
    """
	Setup the kepcotrend environment
	
	infile: 
	the input file in the FITS format obtained from MAST
	
	outfile:
	The output will be a fits file in the same style as the input file but with two additional columns: CBVSAP_MODL and CBVSAP_FLUX. The first of these is the best fitting linear combination of basis vectors. The second is the new flux with the basis vector sum subtracted. This is the new flux value. 
	
	plot:
	either True or False if you want to see a plot of the light curve
	The top plot shows the original light curve in blue and the sum of basis vectors in red
	The bottom plot has had the basis vector sum subracted
	
	bvfile:
	the name of the FITS file containing the basis vectors

	listbv:
	the basis vectors to fit to the data
	
	fitmethod:
	fit using either the 'llsq' or the 'simplex' method. 'llsq' is usually the correct one to use because as the basis vectors are orthogonal. Simplex gives you option of using a different merit function - ie. you can minimise the least absolute residual instead of the least squares which weights outliers less
	
	fitpower:
	if using a simplex you can chose your own power in the metir function - i.e. the merit function minimises abs(Obs - Mod)^P. P=2 is least squares, P = 1 minimises least absolutes
	
	iterate:
	should the program fit the basis vectors to the light curve data then remove data points further than 'sigma' from the fit and then refit
	
	maskfile:
	this is the name of a mask file which can be used to define regions of the flux time series to exclude from the fit. The easiest way to create this is by using keprange from the PyKE set of tools. You can also make this yourself with two BJDs on each line in the file specifying the beginning and ending date of the region to exclude.
	
	scinterp:
	the basis vectors are only calculated for long cadence data, therefore if you want to use short cadence data you have to interpolate the basis vectors. There are several methods to do this, the best of these probably being nearest which picks the value of the nearest long cadence data point.
	The options available are None|linear|nearest|zero|slinear|quadratic|cubic
	If you are using short cadence data don't choose none
	"""
    # log the call
    hashline = '----------------------------------------------------------------------------'
    kepmsg.log(logfile, hashline, verbose)
    call = 'KEPCOTREND -- '
    call += 'infile=' + infile + ' '
    call += 'outfile=' + outfile + ' '
    call += 'bvfile=' + bvfile + ' '
    #	call += 'numpcomp= '+str(numpcomp)+' '
    call += 'listbv= ' + str(listbv) + ' '
    call += 'fitmethod=' + str(fitmethod) + ' '
    call += 'fitpower=' + str(fitpower) + ' '
    iterateit = 'n'
    if (iterate): iterateit = 'y'
    call += 'iterate=' + iterateit + ' '
    call += 'sigma_clip=' + str(sigma) + ' '
    call += 'mask_file=' + maskfile + ' '
    call += 'scinterp=' + str(scinterp) + ' '
    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('KEPCOTREND 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 -- KEPCOTREND: ' + outfile + ' exists. Use --clobber'
        status = kepmsg.err(logfile, message, verbose)

    # open input file
    if status == 0:
        instr, status = kepio.openfits(infile, 'readonly', logfile, verbose)
        tstart, tstop, bjdref, cadence, status = kepio.timekeys(
            instr, infile, logfile, verbose, status)

    # fudge non-compliant FITS keywords with no values
    if status == 0:
        instr = kepkey.emptykeys(instr, file, logfile, verbose)

    if status == 0:
        if not kepio.fileexists(bvfile):
            message = 'ERROR -- KEPCOTREND: ' + bvfile + ' does not exist.'
            status = kepmsg.err(logfile, message, verbose)

    #lsq_sq - nonlinear least squares fitting and simplex_abs have been removed from the option in PyRAF but they are still in the code!
    if status == 0:
        if fitmethod not in [
                'llsq', 'matrix', 'lst_sq', 'simplex_abs', 'simplex'
        ]:
            message = 'Fit method must either: llsq, matrix, lst_sq or simplex'
            status = kepmsg.err(logfile, message, verbose)

    if status == 0:
        if not is_numlike(fitpower) and fitpower is not None:
            message = 'Fit power must be an real number or None'
            status = kepmsg.err(logfile, message, verbose)

    if status == 0:
        if fitpower is None:
            fitpower = 1.

    # input data
    if status == 0:
        short = False
        try:
            test = str(instr[0].header['FILEVER'])
            version = 2
        except KeyError:
            version = 1

        table = instr[1].data
        if version == 1:
            if str(instr[1].header['DATATYPE']) == 'long cadence':
                #print 'Light curve was taken in Lond Cadence mode!'
                quarter = str(instr[1].header['QUARTER'])
                module = str(instr[1].header['MODULE'])
                output = str(instr[1].header['OUTPUT'])
                channel = str(instr[1].header['CHANNEL'])

                lc_cad_o = table.field('cadence_number')
                lc_date_o = table.field('barytime')
                lc_flux_o = table.field(
                    'ap_raw_flux') / 1625.3468  #convert to e-/s
                lc_err_o = table.field(
                    'ap_raw_err') / 1625.3468  #convert to e-/s
            elif str(instr[1].header['DATATYPE']) == 'short cadence':
                short = True
                #print 'Light curve was taken in Short Cadence mode!'
                quarter = str(instr[1].header['QUARTER'])
                module = str(instr[1].header['MODULE'])
                output = str(instr[1].header['OUTPUT'])
                channel = str(instr[1].header['CHANNEL'])

                lc_cad_o = table.field('cadence_number')
                lc_date_o = table.field('barytime')
                lc_flux_o = table.field(
                    'ap_raw_flux') / 54.178  #convert to e-/s
                lc_err_o = table.field('ap_raw_err') / 54.178  #convert to e-/s

        elif version == 2:
            if str(instr[0].header['OBSMODE']) == 'long cadence':
                #print 'Light curve was taken in Long Cadence mode!'

                quarter = str(instr[0].header['QUARTER'])
                module = str(instr[0].header['MODULE'])
                output = str(instr[0].header['OUTPUT'])
                channel = str(instr[0].header['CHANNEL'])

                lc_cad_o = table.field('CADENCENO')
                lc_date_o = table.field('TIME')
                lc_flux_o = table.field('SAP_FLUX')
                lc_err_o = table.field('SAP_FLUX_ERR')
            elif str(instr[0].header['OBSMODE']) == 'short cadence':
                #print 'Light curve was taken in Short Cadence mode!'
                short = True
                quarter = str(instr[0].header['QUARTER'])
                module = str(instr[0].header['MODULE'])
                output = str(instr[0].header['OUTPUT'])
                channel = str(instr[0].header['CHANNEL'])

                lc_cad_o = table.field('CADENCENO')
                lc_date_o = table.field('TIME')
                lc_flux_o = table.field('SAP_FLUX')
                lc_err_o = table.field('SAP_FLUX_ERR')

        if str(quarter) == str(4) and version == 1:
            lc_cad_o = lc_cad_o[lc_cad_o >= 11914]
            lc_date_o = lc_date_o[lc_cad_o >= 11914]
            lc_flux_o = lc_flux_o[lc_cad_o >= 11914]
            lc_err_o = lc_err_o[lc_cad_o >= 11914]

        # bvfilename = '%s/Q%s_%s_%s_map.txt' %(bvfile,quarter,module,output)
        # if str(quarter) == str(5):
        # 	bvdata = genfromtxt(bvfilename)
        # elif str(quarter) == str(3) or str(quarter) == str(4):
        # 	bvdata = genfromtxt(bvfilename,skip_header=22)
        # elif str(quarter) == str(1):
        # 	bvdata = genfromtxt(bvfilename,skip_header=10)
        # else:
        # 	bvdata = genfromtxt(bvfilename,skip_header=13)

        if short and scinterp == 'None':
            message = 'You cannot select None as the interpolation method because you are using short cadence data and therefore must use some form of interpolation. I reccommend nearest if you are unsure.'
            status = kepmsg.err(logfile, message, verbose)

        bvfiledata = pyfits.open(bvfile)
        bvdata = bvfiledata['MODOUT_%s_%s' % (module, output)].data

        if int(bvfiledata[0].header['QUARTER']) != int(quarter):
            message = 'CBV file and light curve file are from different quarters. CBV file is from Q%s and light curve is from Q%s' % (
                int(bvfiledata[0].header['QUARTER']), int(quarter))
            status = kepmsg.err(logfile, message, verbose)

    if status == 0:
        if int(quarter) == 4 and int(module) == 3:
            message = 'Approximately twenty days into Q4 Module 3 failed. As a result, Q4 light curves contain these 20 day of data. However, we do not calculate CBVs for this section of data.'
            status = kepmsg.err(logfile, message, verbose)

    if status == 0:

        #cut out infinites and zero flux columns
        lc_cad, lc_date, lc_flux, lc_err, bad_data = cutBadData(
            lc_cad_o, lc_date_o, lc_flux_o, lc_err_o)

        #get a list of basis vectors to use from the list given
        #accept different seperators
        listbv = listbv.strip()
        if listbv[1] in [' ', ',', ':', ';', '|', ', ']:
            separator = str(listbv)[1]
        else:
            message = 'You must separate your basis vector numbers to use with \' \' \',\' \':\' \';\' or \'|\' and the first basis vector to use must be between 1 and 9'
            status = kepmsg.err(logfile, message, verbose)

    if status == 0:
        bvlist = fromstring(listbv, dtype=int, sep=separator)

        if bvlist[0] == 0:
            message = 'Must use at least one basis vector'
            status = kepmsg.err(logfile, message, verbose)
    if status == 0:
        #pcomps = get_pcomp(pcompdata,n_comps,lc_cad)
        # if str(quarter) == str(5):
        # 	bvectors = get_pcomp_list(bvdata,bvlist,lc_cad)
        # else:
        #	bvectors = get_pcomp_list_newformat(bvdata,bvlist,lc_cad)

        if short:
            bvdata.field('CADENCENO')[:] = (((bvdata.field('CADENCENO')[:] +
                                              (7.5 / 15.)) * 30.) -
                                            11540.).round()

        bvectors, in1derror = get_pcomp_list_newformat(bvdata, bvlist, lc_cad,
                                                       short, scinterp)

        if in1derror:
            message = 'It seems that you have an old version of numpy which does not have the in1d function included. Please update your version of numpy to a version 1.4.0 or later'
            status = kepmsg.err(logfile, message, verbose)
    if status == 0:

        medflux = median(lc_flux)
        n_flux = (lc_flux / medflux) - 1
        n_err = sqrt(pow(lc_err, 2) / pow(medflux, 2))

        #plt.errorbar(lc_cad,n_flux,yerr=n_err)
        #plt.errorbar(lc_cad,lc_flux,yerr=lc_err)

        #n_err = median(lc_err/lc_flux) * n_flux
        #print n_err

        #does an iterative least squares fit
        #t1 = do_leastsq(pcomps,lc_cad,n_flux)
        #

        if maskfile != '':
            domasking = True
            if not kepio.fileexists(maskfile):
                message = 'Maskfile %s does not exist' % maskfile
                status = kepmsg.err(logfile, message, verbose)
        else:
            domasking = False

    if status == 0:
        if domasking:

            lc_date_masked = copy(lc_date)
            n_flux_masked = copy(n_flux)
            lc_cad_masked = copy(lc_cad)
            n_err_masked = copy(n_err)
            maskdata = atleast_2d(genfromtxt(maskfile, delimiter=','))
            #make a mask of True values incase there are not regions in maskfile to exclude.
            mask = zeros(len(lc_date_masked)) == 0.
            for maskrange in maskdata:
                if version == 1:
                    start = maskrange[0] - 2400000.0
                    end = maskrange[1] - 2400000.0
                elif version == 2:
                    start = maskrange[0] - 2454833.
                    end = maskrange[1] - 2454833.
                masknew = logical_xor(lc_date < start, lc_date > end)
                mask = logical_and(mask, masknew)

            lc_date_masked = lc_date_masked[mask]
            n_flux_masked = n_flux_masked[mask]
            lc_cad_masked = lc_cad_masked[mask]
            n_err_masked = n_err_masked[mask]
        else:
            lc_date_masked = copy(lc_date)
            n_flux_masked = copy(n_flux)
            lc_cad_masked = copy(lc_cad)
            n_err_masked = copy(n_err)

        #pcomps = get_pcomp(pcompdata,n_comps,lc_cad)

        bvectors_masked, hasin1d = get_pcomp_list_newformat(
            bvdata, bvlist, lc_cad_masked, short, scinterp)

        if (iterate) and sigma is None:
            message = 'If fitting iteratively you must specify a clipping range'
            status = kepmsg.err(logfile, message, verbose)

    if status == 0:
        #uses Pvals = yhat * U_transpose
        if (iterate):
            coeffs, fittedmask = do_lst_iter(bvectors_masked, lc_cad_masked,
                                             n_flux_masked, sigma, 50.,
                                             fitmethod, fitpower)
        else:
            if fitmethod == 'matrix' and domasking:
                coeffs = do_lsq_uhat(bvectors_masked, lc_cad_masked,
                                     n_flux_masked, False)
            if fitmethod == 'llsq' and domasking:
                coeffs = do_lsq_uhat(bvectors_masked, lc_cad_masked,
                                     n_flux_masked, False)
            elif fitmethod == 'lst_sq':
                coeffs = do_lsq_nlin(bvectors_masked, lc_cad_masked,
                                     n_flux_masked)
            elif fitmethod == 'simplex_abs':
                coeffs = do_lsq_fmin(bvectors_masked, lc_cad_masked,
                                     n_flux_masked)
            elif fitmethod == 'simplex':
                coeffs = do_lsq_fmin_pow(bvectors_masked, lc_cad_masked,
                                         n_flux_masked, fitpower)
            else:
                coeffs = do_lsq_uhat(bvectors_masked, lc_cad_masked,
                                     n_flux_masked)

        flux_after = (get_newflux(n_flux, bvectors, coeffs) + 1) * medflux
        flux_after_masked = (
            get_newflux(n_flux_masked, bvectors_masked, coeffs) + 1) * medflux
        bvsum = get_pcompsum(bvectors, coeffs)

        bvsum_masked = get_pcompsum(bvectors_masked, coeffs)

        #print 'chi2: ' + str(chi2_gtf(n_flux,bvsum,n_err,2.*len(n_flux)-2))
        #print 'rms: ' + str(rms(n_flux,bvsum))

        bvsum_nans = putInNans(bad_data, bvsum)
        flux_after_nans = putInNans(bad_data, flux_after)

    if plot and status == 0:
        bvsum_un_norm = medflux * (1 - bvsum)
        #bvsum_un_norm = 0-bvsum
        #lc_flux = n_flux
        do_plot(lc_date, lc_flux, flux_after, bvsum_un_norm, lc_cad, bad_data,
                lc_cad_o, version)

    if status == 0:
        make_outfile(instr, outfile, flux_after_nans, bvsum_nans, version)

    # close input file
    if status == 0:
        status = kepio.closefits(instr, logfile, verbose)

        #print some results to screen:
        print('      -----      ')
        if iterate:
            flux_fit = n_flux_masked[fittedmask]
            sum_fit = bvsum_masked[fittedmask]
            err_fit = n_err_masked[fittedmask]
        else:
            flux_fit = n_flux_masked
            sum_fit = bvsum_masked
            err_fit = n_err_masked
        print('reduced chi2: ' + str(
            chi2_gtf(flux_fit, sum_fit, err_fit,
                     len(flux_fit) - len(coeffs))))
        print('rms: ' + str(medflux * rms(flux_fit, sum_fit)))
        for i in range(len(coeffs)):
            print('Coefficient of CBV #%s: %s' % (i + 1, coeffs[i]))
        print('      -----      ')

    # end time
    if (status == 0):
        message = 'KEPCOTREND completed at'
    else:
        message = '\nKEPCOTTREND aborted at'
    kepmsg.clock(message, logfile, verbose)

    return
Esempio n. 25
0
def kepsmooth(infile,outfile,datacol,function,fscale,plot,plotlab,
              clobber,verbose,logfile,status, cmdLine=False): 

## startup parameters

    status = 0
    labelsize = 24
    ticksize = 16
    xsize = 18
    ysize = 6
    lcolor = '#0000ff'
    lwidth = 1.0
    fcolor = '#ffff00'
    falpha = 0.2

## log the call 

    hashline = '----------------------------------------------------------------------------'
    kepmsg.log(logfile,hashline,verbose)
    call = 'KEPSMOOTH -- '
    call += 'infile='+infile+' '
    call += 'outfile='+outfile+' '
    call += 'datacol='+str(datacol)+' '
    call += 'function='+str(function)+' '
    call += 'fscale='+str(fscale)+' '
    plotit = 'n'
    if (plot): plotit = 'y'
    call += 'plot='+plotit+ ' '
    call += 'plotlab='+str(plotlab)+' '
    overwrite = 'n'
    if (clobber): overwrite = 'y'
    call += 'clobber='+overwrite+ ' '
    chatter = 'n'
    if (verbose): chatter = 'y'
    call += 'verbose='+chatter+' '
    call += 'logfile='+logfile
    kepmsg.log(logfile,call+'\n',verbose)

## start time

    kepmsg.clock('KEPSMOOTH started at',logfile,verbose)

## test log file

    logfile = kepmsg.test(logfile)

## clobber output file

    if clobber: status = kepio.clobber(outfile,logfile,verbose)
    if kepio.fileexists(outfile): 
	    message = 'ERROR -- KEPSMOOTH: ' + outfile + ' exists. Use clobber=yes'
	    status = kepmsg.err(logfile,message,verbose)

## open input file

    if status == 0:
        instr, status = kepio.openfits(infile,'readonly',logfile,verbose)
        tstart, tstop, bjdref, cadence, status = kepio.timekeys(instr,infile,logfile,verbose,status)
        if cadence == 0.0: 
            tstart, tstop, ncad, cadence, status = kepio.cadence(instr,infile,logfile,verbose,status) 
    if status == 0:
        try:
            work = instr[0].header['FILEVER']
            cadenom = 1.0
        except:
            cadenom = cadence

## fudge non-compliant FITS keywords with no values

    if status == 0:
        instr = kepkey.emptykeys(instr,file,logfile,verbose)

## read table structure

    if status == 0:
	table, status = kepio.readfitstab(infile,instr[1],logfile,verbose)

# read time and flux columns

    if status == 0:
        barytime, status = kepio.readtimecol(infile,table,logfile,verbose)
    if status == 0:
        flux, status = kepio.readfitscol(infile,instr[1].data,datacol,logfile,verbose)

# filter input data table

    if status == 0:
        try:
            nanclean = instr[1].header['NANCLEAN']
        except:
            naxis2 = 0
            for i in range(len(table.field(0))):
                if (numpy.isfinite(barytime[i]) and numpy.isfinite(flux[i]) and flux[i] != 0.0):
                    table[naxis2] = table[i]
                    naxis2 += 1
            instr[1].data = table[:naxis2]
            comment = 'NaN cadences removed from data'
            status = kepkey.new('NANCLEAN',True,comment,instr[1],outfile,logfile,verbose)

## read table columns

    if status == 0:
	try:
            intime = instr[1].data.field('barytime')
	except:
            intime, status = kepio.readfitscol(infile,instr[1].data,'time',logfile,verbose)
	indata, status = kepio.readfitscol(infile,instr[1].data,datacol,logfile,verbose)
    if status == 0:
        intime = intime + bjdref
        indata = indata / cadenom

## smooth data

    if status == 0:
        outdata = kepfunc.smooth(indata,fscale/(cadence/86400),function)

## comment keyword in output file

    if status == 0:
        status = kepkey.history(call,instr[0],outfile,logfile,verbose)

## clean up x-axis unit

    if status == 0:
	intime0 = float(int(tstart / 100) * 100.0)
        if intime0 < 2.4e6: intime0 += 2.4e6
	ptime = intime - intime0
	xlab = 'BJD $-$ %d' % intime0

## clean up y-axis units

    if status == 0:
        pout = indata * 1.0
        pout2 = outdata * 1.0 
	nrm = len(str(int(numpy.nanmax(pout))))-1
	pout = pout / 10**nrm
	pout2 = pout2 / 10**nrm
	ylab = '10$^%d$ %s' % (nrm, re.sub('_','-',plotlab))

## data limits

	xmin = numpy.nanmin(ptime)
	xmax = numpy.nanmax(ptime)
	ymin = numpy.min(pout)
	ymax = numpy.nanmax(pout)
	xr = xmax - xmin
	yr = ymax - ymin
        ptime = insert(ptime,[0],[ptime[0]]) 
        ptime = append(ptime,[ptime[-1]])
        pout = insert(pout,[0],[0.0]) 
        pout = append(pout,0.0)
        pout2 = insert(pout2,[0],[0.0]) 
        pout2 = append(pout2,0.0)

## plot light curve

    if status == 0 and plot:
        try:
            params = {'backend': 'png',
                      'axes.linewidth': 2.5,
                      'axes.labelsize': labelsize,
                      'axes.font': 'sans-serif',
                      'axes.fontweight' : 'bold',
                      'text.fontsize': 12,
                      'legend.fontsize': 12,
                      'xtick.labelsize': ticksize,
                      'ytick.labelsize': ticksize}
            rcParams.update(params)
        except:
            print('ERROR -- KEPSMOOTH: install latex for scientific plotting')
            status = 1
    if status == 0 and plot:
        pylab.figure(1,figsize=[xsize,ysize])

# delete any fossil plots in the matplotlib window

        pylab.clf()

# position axes inside the plotting window

	ax = pylab.subplot(111)
	pylab.subplots_adjust(0.06,0.1,0.93,0.88)

# force tick labels to be absolute rather than relative

        pylab.gca().xaxis.set_major_formatter(pylab.ScalarFormatter(useOffset=False))
        pylab.gca().yaxis.set_major_formatter(pylab.ScalarFormatter(useOffset=False))

# rotate y labels by 90 deg

        labels = ax.get_yticklabels()
        setp(labels, 'rotation', 90)

        pylab.plot(ptime[1:-1],pout[1:-1],color='#ff9900',linestyle='-',linewidth=lwidth)
        fill(ptime,pout,color=fcolor,linewidth=0.0,alpha=falpha)
        pylab.plot(ptime,pout2,color=lcolor,linestyle='-',linewidth=lwidth*4.0)
	pylab.xlabel(xlab, {'color' : 'k'})
	pylab.ylabel(ylab, {'color' : 'k'})
	xlim(xmin-xr*0.01,xmax+xr*0.01)
        if ymin >= 0.0: 
            ylim(ymin-yr*0.01,ymax+yr*0.01)
        else:
            ylim(1.0e-10,ymax+yr*0.01)
        pylab.grid()

# render plot

    if cmdLine: 
        pylab.show()
    else: 
        pylab.ion()
        pylab.plot([])
        pylab.ioff()
	
## write output file

    if status == 0:
        for i in range(len(outdata)):
            instr[1].data.field(datacol)[i] = outdata[i]
        instr.writeto(outfile)
    
## close input file

    if status == 0:
        status = kepio.closefits(instr,logfile,verbose)	    

## end time

    if (status == 0):
	    message = 'KEPSMOOTH completed at'
    else:
	    message = '\nKEPSMOOTH aborted at'
    kepmsg.clock(message,logfile,verbose)
Esempio n. 26
0
import kepio,kepstat
from astropy.io import fits
import kepreduce
from transit_basic import *

instr = fits.open('/scratch/kepler_data/kepler_Q16/kplr008123937-2013098041711_llc.fits')
filename = '/scratch/kepler_data/kepler_Q16/kplr008123937-2013098041711_llc.fits'
print(kepio.timekeys(instr, filename))

cata_path = '../catalog/cumulative.csv'
data_path = '/scratch/kepler_data/'

B = LcNoiseSampler(cata_path, data_path)
print(B.kepid)
import numpy as np
kepid = np.random.choice(kepio.get_id(cata_path))

filedir = kepio.pathfinder(kepid, data_path, '*')
filenames = glob.glob(filedir)
all_time = []
all_flux = []
for i in range(len(filenames)):
    #read into cadence
    instr = fits.open(filenames[i])
    tstart, tstop, bjdref, cadence = kepio.timekeys(instr, filenames[i])
    #reduce lc
    intime, nordata = kepreduce.reduce_lc(instr, filenames[i])
    #calculte runing stddev
    stddev = kepstat.running_frac_std(intime, nordata, 6.5 / 24) * 1.0e6
    #cdpp
    cdpp = stddev / math.sqrt(6.5 * 3600.0 / cadence)
Esempio n. 27
0
def keptransitmodel(inputfile,
                    datacol,
                    errorcol,
                    period_d,
                    rprs,
                    T0,
                    Ecc,
                    ars,
                    inc,
                    omega,
                    LDparams,
                    sec,
                    norm=False,
                    verbose=0,
                    logfile='logfile.dat',
                    status=0,
                    cmdLine=False):

    #write to a logfile
    hashline = '----------------------------------------------------------------------------'
    kepmsg.log(logfile, hashline, verbose)
    call = 'KEPTRANSIT -- '
    call += 'inputfile=' + inputfile + ' '
    call += 'datacol=' + str(datacol) + ' '
    call += 'errorcol=' + str(errorcol) + ' '
    call += 'period_d=' + str(period_d) + ' '
    call += 'rprs=' + str(rprs) + ' '
    call += 'T0=' + str(T0) + ' '
    call += 'Ecc=' + str(Ecc) + ' '
    call += 'ars=' + str(ars) + ' '
    call += 'inc=' + str(inc) + ' '
    call += 'omega=' + str(omega) + ' '
    call += 'LDparams=' + str(LDparams) + ' '
    call += 'sec=' + str(sec) + ' '
    #to finish

    # open input file

    if status == 0:
        instr, status = kepio.openfits(inputfile, 'readonly', logfile, verbose)
    if status == 0:
        tstart, tstop, bjdref, cadence, status = kepio.timekeys(
            instr, inputfile, 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(inputfile, 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 np.isfinite(table.field('barytime')[i]) and \
                            np.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 np.isfinite(table.field('time')[i]) and \
                            np.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(inputfile, instr[1].data,
                                               'time', logfile, verbose)

        indata, status = kepio.readfitscol(inputfile, instr[1].data, datacol,
                                           logfile, verbose)
        inerr, status = kepio.readfitscol(inputfile, instr[1].data, errorcol,
                                          logfile, verbose)
    if status == 0:
        intime = intime + bjdref
        indata = indata / cadenom
        inerr = inerr / cadenom

    if status == 0 and norm:
        #first remove outliers before normalizing
        threesig = 3. * np.std(indata)
        mask = np.logical_and(indata < indata + threesig,
                              indata > indata - threesig)
        #now normalize
        indata = indata / np.median(indata[mask])

    if status == 0:
        #need to check if LD params are sensible and in right format
        LDparams = [float(i) for i in LDparams.split()]

        inc = inc * np.pi / 180.

    if status == 0:
        modelfit = tmod.lightcurve(intime, period_d, rprs, T0, Ecc, ars, inc,
                                   omega, LDparams, sec)

    if status == 0:
        phi, fluxfold, modelfold, errorfold, phiNotFold = fold_data(
            intime, modelfit, indata, inerr, period_d, T0)

    if status == 0:
        do_plot(intime, modelfit, indata, inerr, period_d, T0, cmdLine)
Esempio n. 28
0
def kepbinary(infile,outfile,datacol,m1,m2,r1,r2,period,bjd0,eccn,omega,inclination,
              c1,c2,c3,c4,albedo,depth,contamination,gamma,fitparams,eclipses,dopboost,
              tides,job,clobber,verbose,logfile,status): 

# startup parameters

    status = 0
    labelsize = 24; ticksize = 16; xsize = 17; ysize = 7
    lcolor = '#0000ff'; lwidth = 1.0; fcolor = '#ffff00'; falpha = 0.2

# log the call 

    hashline = '----------------------------------------------------------------------------'
    kepmsg.log(logfile,hashline,verbose)
    call = 'KEPBINARY -- '
    call += 'infile='+infile+' '
    call += 'outfile='+outfile+' '
    call += 'datacol='+datacol+' '
    call += 'm1='+str(m1)+' '
    call += 'm2='+str(m2)+' '
    call += 'r1='+str(r1)+' '
    call += 'r2='+str(r2)+' '
    call += 'period='+str(period)+' '
    call += 'bjd0='+str(bjd0)+' '
    call += 'eccn='+str(eccn)+' '
    call += 'omega='+str(omega)+' '
    call += 'inclination='+str(inclination)+' '
    call += 'c1='+str(c1)+' '
    call += 'c2='+str(c2)+' '
    call += 'c3='+str(c3)+' '
    call += 'c4='+str(c4)+' '
    call += 'albedo='+str(albedo)+' '
    call += 'depth='+str(depth)+' '
    call += 'contamination='+str(contamination)+' '
    call += 'gamma='+str(gamma)+' '
    call += 'fitparams='+str(fitparams)+' '
    eclp = 'n'
    if (eclipses): eclp = 'y'
    call += 'eclipses='+eclp+ ' '
    boost = 'n'
    if (dopboost): boost = 'y'
    call += 'dopboost='+boost+ ' '
    distort = 'n'
    if (tides): distort = 'y'
    call += 'tides='+distort+ ' '
    call += 'job='+str(job)+ ' '
    overwrite = 'n'
    if (clobber): overwrite = 'y'
    call += 'clobber='+overwrite+ ' '
    chatter = 'n'
    if (verbose): chatter = 'y'
    call += 'verbose='+chatter+' '
    call += 'logfile='+logfile
    kepmsg.log(logfile,call+'\n',verbose)

# start time

    kepmsg.clock('KEPBINARY started at',logfile,verbose)

# test log file

    logfile = kepmsg.test(logfile)

# check and format the list of fit parameters

    if status == 0 and job == 'fit':
        allParams = [m1,m2,r1,r2,period,bjd0,eccn,omega,inclination]
        allNames = ['m1','m2','r1','r2','period','bjd0','eccn','omega','inclination']
        fitparams = re.sub('\|',',',fitparams.strip())
        fitparams = re.sub('\.',',',fitparams.strip())
        fitparams = re.sub(';',',',fitparams.strip())
        fitparams = re.sub(':',',',fitparams.strip())
        fitparams = re.sub('\s+',',',fitparams.strip())
        fitparams, status = kepio.parselist(fitparams,logfile,verbose)
        for fitparam in fitparams:
            if fitparam.strip() not in allNames:
                message = 'ERROR -- KEPBINARY: unknown field in list of fit parameters'
                status = kepmsg.err(logfile,message,verbose)

# clobber output file

    if status == 0:
        if clobber: status = kepio.clobber(outfile,logfile,verbose)
        if kepio.fileexists(outfile): 
            message = 'ERROR -- KEPBINARY: ' + outfile + ' exists. Use --clobber'
            status = kepmsg.err(logfile,message,verbose)

# open input file

    if status == 0:
        instr, status = kepio.openfits(infile,'readonly',logfile,verbose)
    if status == 0:
        tstart, tstop, bjdref, cadence, status = kepio.timekeys(instr,infile,logfile,verbose,status)
    if status == 0:
        try:
            work = instr[0].header['FILEVER']
            cadenom = 1.0
        except:
            cadenom = cadence

# check the data column exists

    if status == 0:
        try:
            instr[1].data.field(datacol)
        except:
            message = 'ERROR -- KEPBINARY: ' + datacol + ' column does not exist in ' + infile + '[1]'
            status = kepmsg.err(logfile,message,verbose)

# fudge non-compliant FITS keywords with no values

    if status == 0:
        instr = kepkey.emptykeys(instr,file,logfile,verbose)

# read table structure

    if status == 0:
	table, status = kepio.readfitstab(infile,instr[1],logfile,verbose)

# filter input data table

    if status == 0:
        try:
            nanclean = instr[1].header['NANCLEAN']
        except:
            naxis2 = 0
            try:
                for i in range(len(table.field(0))):
                    if numpy.isfinite(table.field('barytime')[i]) and \
                            numpy.isfinite(table.field(datacol)[i]):
                        table[naxis2] = table[i]
                        naxis2 += 1
                        instr[1].data = table[:naxis2]
            except:
                for i in range(len(table.field(0))):
                    if numpy.isfinite(table.field('time')[i]) and \
                            numpy.isfinite(table.field(datacol)[i]):
                        table[naxis2] = table[i]
                        naxis2 += 1
                        instr[1].data = table[:naxis2]
            comment = 'NaN cadences removed from data'
            status = kepkey.new('NANCLEAN',True,comment,instr[1],outfile,logfile,verbose)
 
# read table columns

    if status == 0:
	try:
            time = instr[1].data.field('barytime')
	except:
            time, status = kepio.readfitscol(infile,instr[1].data,'time',logfile,verbose)
	indata, status = kepio.readfitscol(infile,instr[1].data,datacol,logfile,verbose)
    if status == 0:
        time = time + bjdref
        indata = indata / cadenom

# limb-darkening cofficients

    if status == 0:
        limbdark = numpy.array([c1,c2,c3,c4],dtype='float32')

# time details for model

    if status == 0:
        npt = len(time)
        exptime = numpy.zeros((npt),dtype='float64')
        dtype = numpy.zeros((npt),dtype='int')
        for i in range(npt):
            try:
                exptime[i] = time[i+1] - time[i]
            except:
                exptime[i] = time[i] - time[i-1]

# calculate binary model

    if status == 0:
        tmodel = kepsim.transitModel(1.0,m1,m2,r1,r2,period,inclination,bjd0,eccn,omega,depth,
                                     albedo,c1,c2,c3,c4,gamma,contamination,npt,time,exptime,
                                     dtype,eclipses,dopboost,tides)

# re-normalize binary model to data

    if status == 0 and (job == 'overlay' or job == 'fit'):
        dmedian = numpy.median(indata)
        tmodel = tmodel / numpy.median(tmodel) * dmedian

# define arrays of floating and frozen parameters

    if status == 0 and job =='fit':
        params = []; paramNames = []; arguments = []; argNames = []
        for i in range(len(allNames)):
            if allNames[i] in fitparams:
                params.append(allParams[i])
                paramNames.append(allNames[i])
            else:
                arguments.append(allParams[i])
                argNames.append(allNames[i])
        params.append(dmedian)
        params = numpy.array(params,dtype='float32')

# subtract model from data

    if status == 0 and job == 'fit':
        deltam = numpy.abs(indata - tmodel)

# fit statistics

    if status == 0 and job == 'fit':
        aveDelta = numpy.sum(deltam) / npt
        chi2 = math.sqrt(numpy.sum((indata - tmodel) * (indata - tmodel) / (npt - len(params))))

# fit model to data using downhill simplex

    if status == 0 and job == 'fit':
        print ''
        print '%4s %11s %11s' % ('iter', 'delta', 'chi^2')
        print '----------------------------'
        print '%4d %.5E %.5E' % (0,aveDelta,chi2)
        bestFit = scipy.optimize.fmin(fitModel,params,args=(paramNames,dmedian,m1,m2,r1,r2,period,bjd0,eccn,
                                                            omega,inclination,depth,albedo,c1,c2,c3,c4,
                                                            gamma,contamination,npt,time,exptime,indata,
                                                            dtype,eclipses,dopboost,tides),maxiter=1e4)

# calculate best fit binary model

    if status == 0 and job == 'fit':
        print ''
        for i in range(len(paramNames)):
            if 'm1' in paramNames[i].lower():
                m1 = bestFit[i]
                print '  M1 = %.3f Msun' % bestFit[i]
            elif 'm2' in paramNames[i].lower():
                m2 = bestFit[i]
                print '  M2 = %.3f Msun' % bestFit[i]
            elif 'r1' in paramNames[i].lower():
                r1 = bestFit[i]
                print '  R1 = %.4f Rsun' % bestFit[i]
            elif 'r2' in paramNames[i].lower():
                r2 = bestFit[i]
                print '  R2 = %.4f Rsun' % bestFit[i]
            elif 'period' in paramNames[i].lower():
                period = bestFit[i]
            elif 'bjd0' in paramNames[i].lower():
                bjd0 = bestFit[i]
                print 'BJD0 = %.8f' % bestFit[i]
            elif 'eccn' in paramNames[i].lower():
                eccn = bestFit[i]
                print '   e = %.3f' % bestFit[i]
            elif 'omega' in paramNames[i].lower():
                omega = bestFit[i]
                print '   w = %.3f deg' % bestFit[i]
            elif 'inclination' in paramNames[i].lower():
                inclination = bestFit[i]
                print '   i = %.3f deg' % bestFit[i]
        flux = bestFit[-1]
        print ''
        tmodel = kepsim.transitModel(flux,m1,m2,r1,r2,period,inclination,bjd0,eccn,omega,depth,
                                     albedo,c1,c2,c3,c4,gamma,contamination,npt,time,exptime,
                                     dtype,eclipses,dopboost,tides)

# subtract model from data

    if status == 0:
        deltaMod = indata - tmodel

# standard deviation of model

    if status == 0:
        stdDev = math.sqrt(numpy.sum((indata - tmodel) * (indata - tmodel)) / npt)

# clean up x-axis unit

    if status == 0:
	time0 = float(int(tstart / 100) * 100.0)
	ptime = time - time0
	xlab = 'BJD $-$ %d' % time0

# clean up y-axis units

    if status == 0:
	nrm = len(str(int(indata.max())))-1
	pout = indata / 10**nrm
	pmod = tmodel / 10**nrm
        pres = deltaMod / stdDev
        if job == 'fit' or job == 'overlay':
            try:
                ylab1 = 'Flux (10$^%d$ e$^-$ s$^{-1}$)' % nrm
                ylab2 = 'Residual ($\sigma$)'
            except:
                ylab1 = 'Flux (10**%d e-/s)' % nrm
                ylab2 = 'Residual (sigma)'
        else:
            ylab1 = 'Normalized Flux'

# dynamic range of model plot

    if status == 0 and job == 'model':
        xmin = ptime.min()
        xmax = ptime.max()
        ymin = tmodel.min()
        ymax = tmodel.max()

# dynamic range of model/data overlay or fit

    if status == 0 and (job == 'overlay' or job == 'fit'):
        xmin = ptime.min()
        xmax = ptime.max()
        ymin = pout.min()
        ymax = pout.max()
        tmin = pmod.min()
        tmax = pmod.max()
        ymin = numpy.array([ymin,tmin]).min()
        ymax = numpy.array([ymax,tmax]).max()
        rmin = pres.min()
        rmax = pres.max()
 
# pad the dynamic range

    if status == 0:
        xr = (xmax - xmin) / 80
        yr = (ymax - ymin) / 40
        if job == 'overlay' or job == 'fit':
            rr = (rmax - rmin) / 40

# set up plot style

    if status == 0:
        labelsize = 24; ticksize = 16; xsize = 17; ysize = 7
        lcolor = '#0000ff'; lwidth = 1.0; fcolor = '#ffff00'; falpha = 0.2
        params = {'backend': 'png',
                  'axes.linewidth': 2.5,
                  'axes.labelsize': 24,
                  'axes.font': 'sans-serif',
                  'axes.fontweight' : 'bold',
                  'text.fontsize': 12,
                  'legend.fontsize': 12,
                  'xtick.labelsize': 16,
                  'ytick.labelsize': 16}
        pylab.rcParams.update(params)
        pylab.figure(figsize=[14,10])
        pylab.clf()

# main plot window

        ax = pylab.axes([0.05,0.3,0.94,0.68])
        pylab.gca().xaxis.set_major_formatter(pylab.ScalarFormatter(useOffset=False))
        pylab.gca().yaxis.set_major_formatter(pylab.ScalarFormatter(useOffset=False))
        labels = ax.get_yticklabels()
        setp(labels, 'rotation', 90, fontsize=12)

# plot model time series 

    if status == 0 and job == 'model':
        pylab.plot(ptime,tmodel,color='#0000ff',linestyle='-',linewidth=1.0)
        ptime = numpy.insert(ptime,[0.0],ptime[0])
        ptime = numpy.append(ptime,ptime[-1])
        tmodel = numpy.insert(tmodel,[0.0],0.0)
        tmodel = numpy.append(tmodel,0.0)
        pylab.fill(ptime,tmodel,fc='#ffff00',linewidth=0.0,alpha=0.2)

# plot data time series and best fit

    if status == 0 and (job == 'overlay' or job == 'fit'):
        pylab.plot(ptime,pout,color='#0000ff',linestyle='-',linewidth=1.0)
        ptime = numpy.insert(ptime,[0.0],ptime[0])
        ptime = numpy.append(ptime,ptime[-1])
        pout = numpy.insert(pout,[0],0.0)
        pout = numpy.append(pout,0.0)
        pylab.fill(ptime,pout,fc='#ffff00',linewidth=0.0,alpha=0.2)
        pylab.plot(ptime[1:-1],pmod,color='r',linestyle='-',linewidth=2.0)

# ranges and labels

    if status == 0:
        pylab.xlim(xmin-xr,xmax+xr)
        pylab.ylim(ymin-yr,ymax+yr)
        pylab.xlabel(xlab, {'color' : 'k'})
        pylab.ylabel(ylab1, {'color' : 'k'})

# residual plot window

    if status == 0 and (job == 'overlay' or job == 'fit'):
        ax = pylab.axes([0.05,0.07,0.94,0.23])
        pylab.gca().xaxis.set_major_formatter(pylab.ScalarFormatter(useOffset=False))
        pylab.gca().yaxis.set_major_formatter(pylab.ScalarFormatter(useOffset=False))
        labels = ax.get_yticklabels()
        setp(labels, 'rotation', 90, fontsize=12)

# plot residual time series 

    if status == 0 and (job == 'overlay' or job == 'fit'):
        pylab.plot([ptime[0],ptime[-1]],[0.0,0.0],color='r',linestyle='--',linewidth=1.0)
        pylab.plot([ptime[0],ptime[-1]],[-1.0,-1.0],color='r',linestyle='--',linewidth=1.0)
        pylab.plot([ptime[0],ptime[-1]],[1.0,1.0],color='r',linestyle='--',linewidth=1.0)
        pylab.plot(ptime[1:-1],pres,color='#0000ff',linestyle='-',linewidth=1.0)
        pres = numpy.insert(pres,[0],rmin)
        pres = numpy.append(pres,rmin)
        pylab.fill(ptime,pres,fc='#ffff00',linewidth=0.0,alpha=0.2)

# ranges and labels of residual time series

    if status == 0 and (job == 'overlay' or job == 'fit'):
        pylab.xlim(xmin-xr,xmax+xr)
        pylab.ylim(rmin-rr,rmax+rr)
        pylab.xlabel(xlab, {'color' : 'k'})
        pylab.ylabel(ylab2, {'color' : 'k'})

# display the plot

    if status == 0:
        pylab.draw()
Esempio n. 29
0
def kepbin(infile,outfile,fluxcol,do_nbin,nbins,do_binwidth,binwidth,
	do_ownbins,binfile,method,interpm,plot,clobber,verbose,logfile,status):
	"""
	Setup the kepbin environment
	"""
	# log the call 
	hashline = '----------------------------------------------------------------------------'
	kepmsg.log(logfile,hashline,verbose)
	call = 'KEPBIN -- '
	call += 'infile='+infile+' '
	call += 'outfile='+outfile+' '
	call += 'fluxcol='+fluxcol+ ' '
	donbin = 'n'
	if (do_nbin): donbin = 'y'
	call += 'donbin='+donbin+ ' '
	dobinwidth = 'n'
	if (do_binwidth): dobinwidth = 'y'
	call += 'dbinwidth='+dobinwidth+ ' '
	doownbin = 'n'
	if (do_ownbins): doownbin = 'y'
	call += 'doownbin='+doownbin+ ' '
	call += 'method='+method+' '
	call += 'interpm='+interpm+' '
	plotit = 'n'
	if (plot): plotit = 'y'
	call += 'plot='+plotit+ ' '
	overwrite = 'n'
	if (clobber): overwrite = 'y'
	call += 'clobber='+overwrite+ ' '
	chatter = 'n'
	if (verbose): chatter = 'y'
	call += 'verbose='+chatter+' '
	call += 'logfile='+logfile
	kepmsg.log(logfile,call+'\n',verbose)


	# start time
	kepmsg.clock('KEPCLIP started at',logfile,verbose)

	# test log file
	logfile = kepmsg.test(logfile)
    
	# clobber output file
	if clobber:
		status = kepio.clobber(outfile,logfile,verbose)
	if kepio.fileexists(outfile): 
		message = 'ERROR -- KEPCLIP: ' + outfile + ' exists. Use --clobber'
		status = kepmsg.err(logfile,message,verbose)


	
	# open input file
	if status == 0:
		instr, status = kepio.openfits(infile,'readonly',logfile,verbose)
		tstart, tstop, bjdref, cadence, status = kepio.timekeys(instr,
			infile,logfile,verbose,status)

	# fudge non-compliant FITS keywords with no values
	if status == 0:
		instr = kepkey.emptykeys(instr,file,logfile,verbose)

	# input data
	if status == 0:
		table = instr[1].data

	# read time and flux columns
	date = table.field('barytime')
	flux = table.field(fluxcol)
	
	
	#cut out infinites and zero flux columns
	date,flux = cutBadData(date,flux)
	
	if do_nbin:
		bdate,bflux = bin_funct(date,flux,nbins=nbins
			,method=method,interpm=interpm)
	elif do_binwidth:
		bdate,bflux = bin_funct(date,flux,binwidth=binwidth
			,method=method,interpm=interpm)
	elif do_ownbins:
		filepointer = open(binfile,'r')
		ownbins = []
		for line in filepointer:
			splitted = line.split()
			ownbins.append(float(splitted[0]))
		ownbins = n.array(ownbins)
		bdate,bflux = bin_funct(date,flux,ownbins=ownbins
			,method=method,interpm=interpm)
	
	if plot:
		do_plot(bdate,bflux)
		
	if status == 0:
		col1 = pyfits.Column(name='bdate',format='E',unit='day',array=bdate)
		col2 = pyfits.Column(name='bflux',format='E',unit='e-/cadence',array=bflux)
		cols = pyfits.ColDefs([col1,col2])
		instr.append(pyfits.new_table(cols))
		instr[-1].header.update('EXTNAME','BINNED DATA','extension name')
		instr.writeto(outfile)
    
	
	# close input file
	if status == 0:
		status = kepio.closefits(instr,logfile,verbose)	    

	# end time
	if (status == 0):
		message = 'KEPBIN completed at'
	else:
		message = '\nKEPBIN aborted at'
	kepmsg.clock(message,logfile,verbose)
Esempio n. 30
0
def kepfoldimg(infile,outfile,datacol,period,phasezero,binmethod,threshold,niter,nbins,
            plot,plotlab,clobber,verbose,logfile,status): 

# startup parameters

    status = 0
    labelsize = 24; ticksize = 16; xsize = 17; ysize = 7
    lcolor = '#0000ff'; lwidth = 1.0; fcolor = '#ffff00'; falpha = 0.2

# log the call 

    hashline = '----------------------------------------------------------------------------'
    kepmsg.log(logfile,hashline,verbose)
    call = 'KEPFOLD -- '
    call += 'infile='+infile+' '
    call += 'outfile='+outfile+' '
    call += 'datacol='+datacol+' '
    call += 'period='+str(period)+' '
    call += 'phasezero='+str(phasezero)+' '
    call += 'binmethod='+binmethod+' '
    call += 'threshold='+str(threshold)+' '
    call += 'niter='+str(niter)+' '
    call += 'nbins='+str(nbins)+' '
    plotres = 'n'
    if (plot): plotres = 'y'
    call += 'plot='+plotres+ ' '
    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('KEPFOLDIMG 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 -- KEPFOLDIMG: ' + 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)

# fudge non-compliant FITS keywords with no values

    if status == 0:
        instr = kepkey.emptykeys(instr,infile,logfile,verbose)

# input data

    if status == 0:
        table = instr[1].data
        incards = instr[1].header.cards
        indata, status = kepio.readfitscol(infile,table,datacol,logfile,verbose)
        barytime, status = kepio.readtimecol(infile,table,logfile,verbose)

# filter out NaNs

    work1 = []; work2 = []
    if status == 0:
        for i in range(len(barytime)):
            if (numpy.isfinite(barytime[i]) and
                numpy.isfinite(indata[i]) and indata[i] != 0.0):
                work1.append(barytime[i])
                work2.append(indata[i])
        barytime = array(work1,dtype='float64')
        indata = array(work2,dtype='float32')

# calculate phase

    if status == 0:
        phase2 = []
        phase1 = (barytime - phasezero) / period
        for i in range(len(phase1)):
            phase2.append(phase1[i] - int(phase1[i]))
            if phase2[-1] < 0.0: phase2[-1] += 1.0
        phase2 = array(phase2,'float32')

# sort phases

    if status == 0:
        ptuple = []
        phase3 = []
        data3 = []
        for i in range(len(phase2)):
            ptuple.append([phase2[i], indata[i]])
        phsort = sorted(ptuple,key=lambda ph: ph[0])
        for i in range(len(phsort)):
            phase3.append(phsort[i][0])
            data3.append(phsort[i][1])
        phase3 = array(phase3,'float32')
        data3 = array(data3,'float32')

# bin phases

    if status == 0:
        work1 = array([data3[0]],'float32')
        phase4 = array([],'float32')
        data4 = array([],'float32')
        dt = (phase3[-1] - phase3[0]) / nbins
        nb = 0.0
        for i in range(len(phase3)):
            if phase3[i] < phase3[0] + nb * dt or phase3[i] >= phase3[0] + (nb + 1.0) * dt:
                if len(work1) > 0:
                    phase4 = append(phase4,phase3[0] + (nb + 0.5) * dt)
                    if (binmethod == 'mean'):
                        data4 = append(data4,kepstat.mean(work1))
                    elif (binmethod == 'median'):
                        data4 = append(data4,kepstat.median(work1,logfile))
                    else:
                        coeffs, errors, covar, iiter, sigma, chi2, dof, fit, plotx, ploty, status = \
                            kepfit.lsqclip('poly0',[1.0],arange(0.0,float(len(work1)),1.0),work1,None,
                                           threshold,threshold,niter,logfile,verbose)
                        data4 = append(data4,coeffs[0])
                work1 = array([],'float32')
                nb += 1.0
            else:
                work1 = append(work1,data3[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['TTYPE20'].comment = 'column title: phase'
        instr[1].header.cards['TFORM20'].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:
        col1 = Column(name='PHASE',format='E',array=phase4)
        col2 = Column(name=datacol,format='E',unit='e/s',array=data4/cadence)
        cols = ColDefs([col1,col2])
        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['TFORM1'].comment = 'column type: float32'
        instr[-1].header.cards['TFORM2'].comment = 'column type: float32'
        instr[-1].header.cards['TUNIT2'].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)

# close input file

    if status == 0:
        status = kepio.closefits(instr,logfile,verbose)	    

# clean up x-axis unit

    if status == 0:
        ptime = array([],'float32')
        pout = array([],'float32')
        work = data4
        for i in range(len(phase4)):
            if (phase4[i] > 0.5): 
                ptime = append(ptime,phase4[i] - 1.0)
                pout = append(pout,work[i] / cadence)
        ptime = append(ptime,phase4)
        pout = append(pout,work / cadence)
        for i in range(len(phase4)):
            if (phase4[i] <= 0.5): 
                ptime = append(ptime,phase4[i] + 1.0)
                pout = append(pout,work[i] / cadence)
	xlab = 'Phase ($\phi$)'

# clean up y-axis units

    if status == 0:
	nrm = len(str(int(pout.max())))-1
	pout = pout / 10**nrm
	ylab = '10$^%d$ %s' % (nrm, plotlab)

# 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 new light curve

    if status == 0 and plot:
        try:
           params = {'backend': 'png',
                      'axes.linewidth': 2.5,
                      'axes.labelsize': labelsize,
                      'axes.font': 'sans-serif',
                      'axes.fontweight' : 'bold',
                      'text.fontsize': 12,
                      'legend.fontsize': 12,
                      'xtick.labelsize': ticksize,
                      'ytick.labelsize': ticksize}
            pylab.rcParams.update(params)
        except:
            print 'ERROR -- KEPFOLD: install latex for scientific plotting'
            status = 1
    if status == 0 and plot:
	pylab.figure(1,figsize=[17,7])
        pylab.clf()
        pylab.axes([0.06,0.1,0.93,0.87])
        pylab.gca().xaxis.set_major_formatter(pylab.ScalarFormatter(useOffset=False))
        pylab.gca().yaxis.set_major_formatter(pylab.ScalarFormatter(useOffset=False))
        pylab.plot(ptime,pout,color=lcolor,linestyle='-',linewidth=lwidth)
        fill(ptime,pout,color=fcolor,linewidth=0.0,alpha=falpha)
	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)
        else:
            ylim(1.0e-10,ymax+yr*0.01)
        pylab.grid()
        pylab.draw()

# stop time

    kepmsg.clock('KEPFOLDIMG ended at: ',logfile,verbose)
def kepsff(infile,
           outfile,
           datacol,
           cenmethod,
           stepsize,
           npoly_cxcy,
           sigma_cxcy,
           npoly_ardx,
           npoly_dsdt,
           sigma_dsdt,
           npoly_arfl,
           sigma_arfl,
           plotres,
           clobber,
           verbose,
           logfile,
           status,
           cmdLine=False):

    # startup parameters

    status = 0
    labelsize = 16
    ticksize = 14
    xsize = 20
    ysize = 8
    lcolor = '#0000ff'
    lwidth = 1.0
    fcolor = '#ffff00'
    falpha = 0.2
    seterr(all="ignore")

    # log the call

    hashline = '----------------------------------------------------------------------------'
    kepmsg.log(logfile, hashline, verbose)
    call = 'KEPSFF -- '
    call += 'infile=' + infile + ' '
    call += 'outfile=' + outfile + ' '
    call += 'datacol=' + datacol + ' '
    call += 'cenmethod=' + cenmethod + ' '
    call += 'stepsize=' + str(stepsize) + ' '
    call += 'npoly_cxcy=' + str(npoly_cxcy) + ' '
    call += 'sigma_cxcy=' + str(sigma_cxcy) + ' '
    call += 'npoly_ardx=' + str(npoly_ardx) + ' '
    call += 'npoly_dsdt=' + str(npoly_dsdt) + ' '
    call += 'sigma_dsdt=' + str(sigma_dsdt) + ' '
    call += 'npoly_arfl=' + str(npoly_arfl) + ' '
    call += 'sigma_arfl=' + str(sigma_arfl) + ' '
    savep = 'n'
    if (plotres): savep = 'y'
    call += 'plotres=' + savep + ' '
    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('KEPSFF 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 -- KEPSFF: ' + 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)

# determine sequence of windows in time

    if status == 0:
        frametim = instr[1].header['FRAMETIM']
        num_frm = instr[1].header['NUM_FRM']
        exptime = frametim * num_frm / 86400
        tstart = table.field('TIME')[0]
        tstop = table.field('TIME')[-1]
        winedge = arange(tstart, tstop, stepsize)
        if tstop > winedge[-1] + stepsize / 2:
            winedge = append(winedge, tstop)
        else:
            winedge[-1] = tstop
        winedge = (winedge - tstart) / exptime
        winedge = winedge.astype(int)
        if len(table.field('TIME')) > winedge[-1] + 1:
            winedge = append(winedge, len(table.field('TIME')))
        elif len(table.field('TIME')) < winedge[-1]:
            winedge[-1] = len(table.field('TIME'))

# step through the time windows

    if status == 0:
        for iw in range(1, len(winedge)):
            t1 = winedge[iw - 1]
            t2 = winedge[iw]

            # filter input data table

            work1 = numpy.array([
                table.field('TIME')[t1:t2],
                table.field('CADENCENO')[t1:t2],
                table.field(datacol)[t1:t2],
                table.field('MOM_CENTR1')[t1:t2],
                table.field('MOM_CENTR2')[t1:t2],
                table.field('PSF_CENTR1')[t1:t2],
                table.field('PSF_CENTR2')[t1:t2],
                table.field('SAP_QUALITY')[t1:t2]
            ], 'float64')
            work1 = numpy.rot90(work1, 3)
            work2 = work1[~numpy.isnan(work1).any(1)]
            work2 = work2[(work2[:, 0] == 0.0) | (work2[:, 0] > 1e5)]

            # assign table columns

            intime = work2[:, 7] + bjdref
            cadenceno = work2[:, 6].astype(int)
            indata = work2[:, 5]
            mom_centr1 = work2[:, 4]
            mom_centr2 = work2[:, 3]
            psf_centr1 = work2[:, 2]
            psf_centr2 = work2[:, 1]
            sap_quality = work2[:, 0]
            if cenmethod == 'moments':
                centr1 = copy(mom_centr1)
                centr2 = copy(mom_centr2)
            else:
                centr1 = copy(psf_centr1)
                centr2 = copy(psf_centr2)

# fit centroid data with low-order polynomial

            cfit = zeros((len(centr2)))
            csig = zeros((len(centr2)))
            functype = 'poly' + str(npoly_cxcy)
            pinit = array([nanmean(centr2)])
            if npoly_cxcy > 0:
                for j in range(npoly_cxcy):
                    pinit = append(pinit, 0.0)
            try:
                coeffs, errors, covar, iiter, sigma, chi2, dof, fit, plotx, ploty, status = \
                    kepfit.lsqclip(functype,pinit,centr1,centr2,None,sigma_cxcy,sigma_cxcy,10,logfile,verbose)
                for j in range(len(coeffs)):
                    cfit += coeffs[j] * numpy.power(centr1, j)
                    csig[:] = sigma
            except:
                message = 'ERROR -- KEPSFF: could not fit centroid data with polynomial. There are no data points within the range of input rows %d - %d. Either increase the stepsize (with an appreciation of the effects on light curve quality this will have!), or better yet - cut the timeseries up to remove large gaps in the input light curve using kepclip.' % (
                    t1, t2)
                status = kepmsg.err(logfile, message, verbose)
                #                sys.exit('')
                os._exit(1)

# reject outliers

            time_good = array([], 'float64')
            centr1_good = array([], 'float32')
            centr2_good = array([], 'float32')
            flux_good = array([], 'float32')
            cad_good = array([], 'int')
            for i in range(len(cfit)):
                if abs(centr2[i] - cfit[i]) < sigma_cxcy * csig[i]:
                    time_good = append(time_good, intime[i])
                    centr1_good = append(centr1_good, centr1[i])
                    centr2_good = append(centr2_good, centr2[i])
                    flux_good = append(flux_good, indata[i])
                    cad_good = append(cad_good, cadenceno[i])

# covariance matrix for centroid time series

            centr = concatenate([[centr1_good] - mean(centr1_good),
                                 [centr2_good] - mean(centr2_good)])
            covar = cov(centr)

            # eigenvector eigenvalues of covariance matrix

            [eval, evec] = numpy.linalg.eigh(covar)
            ex = arange(-10.0, 10.0, 0.1)
            epar = evec[1, 1] / evec[0, 1] * ex
            enor = evec[1, 0] / evec[0, 0] * ex
            ex = ex + mean(centr1)
            epar = epar + mean(centr2_good)
            enor = enor + mean(centr2_good)

            # rotate centroid data

            centr_rot = dot(evec.T, centr)

            # fit polynomial to rotated centroids

            rfit = zeros((len(centr2)))
            rsig = zeros((len(centr2)))
            functype = 'poly' + str(npoly_ardx)
            pinit = array([nanmean(centr_rot[0, :])])
            pinit = array([1.0])
            if npoly_ardx > 0:
                for j in range(npoly_ardx):
                    pinit = append(pinit, 0.0)
            try:
                coeffs, errors, covar, iiter, sigma, chi2, dof, fit, plotx, ploty, status = \
                    kepfit.lsqclip(functype,pinit,centr_rot[1,:],centr_rot[0,:],None,100.0,100.0,1,
                                   logfile,verbose)
            except:
                message = 'ERROR -- KEPSFF: could not fit rotated centroid data with polynomial'
                status = kepmsg.err(logfile, message, verbose)
            rx = linspace(nanmin(centr_rot[1, :]), nanmax(centr_rot[1, :]),
                          100)
            ry = zeros((len(rx)))
            for i in range(len(coeffs)):
                ry = ry + coeffs[i] * numpy.power(rx, i)

# calculate arclength of centroids

            s = zeros((len(rx)))
            for i in range(1, len(s)):
                work3 = ((ry[i] - ry[i - 1]) / (rx[i] - rx[i - 1]))**2
                s[i] = s[i - 1] + math.sqrt(1.0 + work3) * (rx[i] - rx[i - 1])

# fit arclength as a function of strongest eigenvector

            sfit = zeros((len(centr2)))
            ssig = zeros((len(centr2)))
            functype = 'poly' + str(npoly_ardx)
            pinit = array([nanmean(s)])
            if npoly_ardx > 0:
                for j in range(npoly_ardx):
                    pinit = append(pinit, 0.0)
            try:
                acoeffs, errors, covar, iiter, sigma, chi2, dof, fit, plotx, ploty, status = \
                    kepfit.lsqclip(functype,pinit,rx,s,None,100.0,100.0,100,logfile,verbose)
            except:
                message = 'ERROR -- KEPSFF: could not fit rotated centroid data with polynomial'
                status = kepmsg.err(logfile, message, verbose)

# correlate arclength with detrended flux

            t = copy(time_good)
            c = copy(cad_good)
            y = copy(flux_good)
            z = centr_rot[1, :]
            x = zeros((len(z)))
            for i in range(len(acoeffs)):
                x = x + acoeffs[i] * numpy.power(z, i)

# calculate time derivative of arclength s

            dx = zeros((len(x)))
            for i in range(1, len(x)):
                dx[i] = (x[i] - x[i - 1]) / (t[i] - t[i - 1])
            dx[0] = dx[1]

            # fit polynomial to derivative and flag outliers (thruster firings)

            dfit = zeros((len(dx)))
            dsig = zeros((len(dx)))
            functype = 'poly' + str(npoly_dsdt)
            pinit = array([nanmean(dx)])
            if npoly_dsdt > 0:
                for j in range(npoly_dsdt):
                    pinit = append(pinit, 0.0)
            try:
                dcoeffs, errors, covar, iiter, dsigma, chi2, dof, fit, dumx, dumy, status = \
                    kepfit.lsqclip(functype,pinit,t,dx,None,3.0,3.0,10,logfile,verbose)
            except:
                message = 'ERROR -- KEPSFF: could not fit rotated centroid data with polynomial'
                status = kepmsg.err(logfile, message, verbose)
            for i in range(len(dcoeffs)):
                dfit = dfit + dcoeffs[i] * numpy.power(t, i)
            centr1_pnt = array([], 'float32')
            centr2_pnt = array([], 'float32')
            time_pnt = array([], 'float64')
            flux_pnt = array([], 'float32')
            dx_pnt = array([], 'float32')
            s_pnt = array([], 'float32')
            time_thr = array([], 'float64')
            flux_thr = array([], 'float32')
            dx_thr = array([], 'float32')
            thr_cadence = []
            for i in range(len(t)):
                if dx[i] < dfit[i] + sigma_dsdt * dsigma and dx[
                        i] > dfit[i] - sigma_dsdt * dsigma:
                    time_pnt = append(time_pnt, time_good[i])
                    flux_pnt = append(flux_pnt, flux_good[i])
                    dx_pnt = append(dx_pnt, dx[i])
                    s_pnt = append(s_pnt, x[i])
                    centr1_pnt = append(centr1_pnt, centr1_good[i])
                    centr2_pnt = append(centr2_pnt, centr2_good[i])
                else:
                    time_thr = append(time_thr, time_good[i])
                    flux_thr = append(flux_thr, flux_good[i])
                    dx_thr = append(dx_thr, dx[i])
                    thr_cadence.append(cad_good[i])

# fit arclength-flux correlation

            cfit = zeros((len(time_pnt)))
            csig = zeros((len(time_pnt)))
            functype = 'poly' + str(npoly_arfl)
            pinit = array([nanmean(flux_pnt)])
            if npoly_arfl > 0:
                for j in range(npoly_arfl):
                    pinit = append(pinit, 0.0)
            try:
                ccoeffs, errors, covar, iiter, sigma, chi2, dof, fit, plx, ply, status = \
                    kepfit.lsqclip(functype,pinit,s_pnt,flux_pnt,None,sigma_arfl,sigma_arfl,100,logfile,verbose)
            except:
                message = 'ERROR -- KEPSFF: could not fit rotated centroid data with polynomial'
                status = kepmsg.err(logfile, message, verbose)

# correction factors for unfiltered data

            centr = concatenate([[centr1] - mean(centr1_good),
                                 [centr2] - mean(centr2_good)])
            centr_rot = dot(evec.T, centr)
            yy = copy(indata)
            zz = centr_rot[1, :]
            xx = zeros((len(zz)))
            cfac = zeros((len(zz)))
            for i in range(len(acoeffs)):
                xx = xx + acoeffs[i] * numpy.power(zz, i)
            for i in range(len(ccoeffs)):
                cfac = cfac + ccoeffs[i] * numpy.power(xx, i)

# apply correction to flux time-series

            out_detsap = indata / cfac

            # split time-series data for plotting

            tim_gd = array([], 'float32')
            flx_gd = array([], 'float32')
            tim_bd = array([], 'float32')
            flx_bd = array([], 'float32')
            for i in range(len(indata)):
                if intime[i] in time_pnt:
                    tim_gd = append(tim_gd, intime[i])
                    flx_gd = append(flx_gd, out_detsap[i])
                else:
                    tim_bd = append(tim_bd, intime[i])
                    flx_bd = append(flx_bd, out_detsap[i])

# plot style and size

            status = kepplot.define(labelsize, ticksize, logfile, verbose)
            pylab.figure(figsize=[xsize, ysize])
            pylab.clf()

            # plot x-centroid vs y-centroid

            ax = kepplot.location([0.04, 0.57, 0.16, 0.41])  # plot location
            px = copy(centr1)  # clean-up x-axis units
            py = copy(centr2)  # clean-up y-axis units
            pxmin = px.min()
            pxmax = px.max()
            pymin = py.min()
            pymax = py.max()
            pxr = pxmax - pxmin
            pyr = pymax - pymin
            pad = 0.05
            if pxr > pyr:
                dely = (pxr - pyr) / 2
                xlim(pxmin - pxr * pad, pxmax + pxr * pad)
                ylim(pymin - dely - pyr * pad, pymax + dely + pyr * pad)
            else:
                delx = (pyr - pxr) / 2
                ylim(pymin - pyr * pad, pymax + pyr * pad)
                xlim(pxmin - delx - pxr * pad, pxmax + delx + pxr * pad)
            pylab.plot(px,
                       py,
                       color='#980000',
                       markersize=5,
                       marker='D',
                       ls='')  # plot data
            pylab.plot(centr1_good,
                       centr2_good,
                       color='#009900',
                       markersize=5,
                       marker='D',
                       ls='')  # plot data
            pylab.plot(ex, epar, color='k', ls='-')
            pylab.plot(ex, enor, color='k', ls='-')
            for tick in ax.xaxis.get_major_ticks():
                tick.label.set_fontsize(14)
            for tick in ax.yaxis.get_major_ticks():
                tick.label.set_fontsize(14)
            kepplot.labels('CCD Column', 'CCD Row', 'k', 16)  # labels
            pylab.grid()  # grid lines

            # plot arclength fits vs drift along strongest eigenvector

            ax = kepplot.location([0.24, 0.57, 0.16, 0.41])  # plot location
            px = rx - rx[0]
            py = s - rx - (s[0] - rx[0])  # clean-up y-axis units
            py, ylab, status = kepplot.cleany(py, 1.0, logfile,
                                              verbose)  # clean-up x-axis units
            kepplot.RangeOfPlot(px, py, 0.05, False)  # data limits
            pylab.plot(px,
                       py,
                       color='#009900',
                       markersize=5,
                       marker='D',
                       ls='')
            px = plotx - rx[0]  # clean-up x-axis units
            py = ploty - plotx - (s[0] - rx[0])  # clean-up y-axis units
            py, ylab, status = kepplot.cleany(py, 1.0, logfile,
                                              verbose)  # clean-up x-axis units
            pylab.plot(px, py, color='r', ls='-', lw=3)
            for tick in ax.xaxis.get_major_ticks():
                tick.label.set_fontsize(14)
            for tick in ax.yaxis.get_major_ticks():
                tick.label.set_fontsize(14)
            ylab = re.sub(' e\S+', ' pixels)', ylab)
            ylab = re.sub(' s\S+', '', ylab)
            ylab = re.sub('Flux', 's $-$ x\'', ylab)
            kepplot.labels('Linear Drift [x\'] (pixels)', ylab, 'k',
                           16)  # labels
            pylab.grid()  # grid lines

            # plot time derivative of arclength s

            ax = kepplot.location([0.04, 0.08, 0.16, 0.41])  # plot location
            px = copy(time_pnt)
            py = copy(dx_pnt)
            px, xlab, status = kepplot.cleanx(px, logfile,
                                              verbose)  # clean-up x-axis units
            kepplot.RangeOfPlot(px, dx, 0.05, False)  # data limits
            pylab.plot(px,
                       py,
                       color='#009900',
                       markersize=5,
                       marker='D',
                       ls='')
            try:
                px = copy(time_thr)
                py = copy(dx_thr)
                px, xlab, status = kepplot.cleanx(
                    px, logfile, verbose)  # clean-up x-axis units
                pylab.plot(px,
                           py,
                           color='#980000',
                           markersize=5,
                           marker='D',
                           ls='')
            except:
                pass
            px = copy(t)
            py = copy(dfit)
            px, xlab, status = kepplot.cleanx(px, logfile,
                                              verbose)  # clean-up x-axis units
            pylab.plot(px, py, color='r', ls='-', lw=3)
            py = copy(dfit + sigma_dsdt * dsigma)
            pylab.plot(px, py, color='r', ls='--', lw=3)
            py = copy(dfit - sigma_dsdt * dsigma)
            pylab.plot(px, py, color='r', ls='--', lw=3)
            for tick in ax.xaxis.get_major_ticks():
                tick.label.set_fontsize(14)
            for tick in ax.yaxis.get_major_ticks():
                tick.label.set_fontsize(14)
            kepplot.labels(xlab, 'ds/dt (pixels day$^{-1}$)', 'k',
                           16)  # labels
            pylab.grid()  # grid lines

            # plot relation of arclength vs detrended flux

            ax = kepplot.location([0.24, 0.08, 0.16, 0.41])  # plot location
            px = copy(s_pnt)
            py = copy(flux_pnt)
            py, ylab, status = kepplot.cleany(py, 1.0, logfile,
                                              verbose)  # clean-up x-axis units
            kepplot.RangeOfPlot(px, py, 0.05, False)  # data limits
            pylab.plot(px,
                       py,
                       color='#009900',
                       markersize=5,
                       marker='D',
                       ls='')
            pylab.plot(plx, ply, color='r', ls='-', lw=3)
            for tick in ax.xaxis.get_major_ticks():
                tick.label.set_fontsize(14)
            for tick in ax.yaxis.get_major_ticks():
                tick.label.set_fontsize(14)
            kepplot.labels('Arclength [s] (pixels)', ylab, 'k', 16)  # labels
            pylab.grid()  # grid lines

            # plot aperture photometry

            kepplot.location([0.44, 0.53, 0.55, 0.45])  # plot location
            px, xlab, status = kepplot.cleanx(intime, logfile,
                                              verbose)  # clean-up x-axis units
            py, ylab, status = kepplot.cleany(indata, 1.0, logfile,
                                              verbose)  # clean-up x-axis units
            kepplot.RangeOfPlot(px, py, 0.01, True)  # data limits
            kepplot.plot1d(px, py, cadence, lcolor, lwidth, fcolor, falpha,
                           True)  # plot data
            kepplot.labels(' ', ylab, 'k', 16)  # labels
            pylab.setp(pylab.gca(),
                       xticklabels=[])  # remove x- or y-tick labels
            kepplot.labels(xlab, re.sub('Flux', 'Aperture Flux', ylab), 'k',
                           16)  # labels
            pylab.grid()  # grid lines

            # Plot corrected photometry

            kepplot.location([0.44, 0.08, 0.55, 0.45])  # plot location
            kepplot.RangeOfPlot(px, py, 0.01, True)  # data limits
            px, xlab, status = kepplot.cleanx(tim_gd, logfile,
                                              verbose)  # clean-up x-axis units
            py, ylab, status = kepplot.cleany(flx_gd, 1.0, logfile,
                                              verbose)  # clean-up x-axis units
            kepplot.plot1d(px, py, cadence, lcolor, lwidth, fcolor, falpha,
                           True)  # plot data
            try:
                px, xlab, status = kepplot.cleanx(
                    tim_bd, logfile, verbose)  # clean-up x-axis units
                py = copy(flx_bd)
                pylab.plot(px,
                           py,
                           color='#980000',
                           markersize=5,
                           marker='D',
                           ls='')
            except:
                pass
            kepplot.labels(xlab, re.sub('Flux', 'Corrected Flux', ylab), 'k',
                           16)  # labels
            pylab.grid()  # grid lines

            # render plot

            if plotres:
                kepplot.render(cmdLine)

# save plot to file

            if plotres:
                pylab.savefig(re.sub('.fits', '_%d.png' % (iw + 1), outfile))

# correct fluxes within the output file

            intime = work1[:, 7] + bjdref
            cadenceno = work1[:, 6].astype(int)
            indata = work1[:, 5]
            mom_centr1 = work1[:, 4]
            mom_centr2 = work1[:, 3]
            psf_centr1 = work1[:, 2]
            psf_centr2 = work1[:, 1]
            centr1 = copy(mom_centr1)
            centr2 = copy(mom_centr2)
            centr = concatenate([[centr1] - mean(centr1_good),
                                 [centr2] - mean(centr2_good)])
            centr_rot = dot(evec.T, centr)
            yy = copy(indata)
            zz = centr_rot[1, :]
            xx = zeros((len(zz)))
            cfac = zeros((len(zz)))
            for i in range(len(acoeffs)):
                xx = xx + acoeffs[i] * numpy.power(zz, i)
            for i in range(len(ccoeffs)):
                cfac = cfac + ccoeffs[i] * numpy.power(xx, i)
            out_detsap = yy / cfac
            instr[1].data.field('SAP_FLUX')[t1:t2] /= cfac
            instr[1].data.field('PDCSAP_FLUX')[t1:t2] /= cfac
            try:
                instr[1].data.field('DETSAP_FLUX')[t1:t2] /= cfac
            except:
                pass

# add quality flag to output file for thruster firings

            for i in range(len(intime)):
                if cadenceno[i] in thr_cadence:
                    instr[1].data.field('SAP_QUALITY')[t1 + i] += 131072

# 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 = 'KEPSFF completed at'
    else:
        message = '\nKEPSFF aborted at'
    kepmsg.clock(message, logfile, verbose)
Esempio n. 32
0
def keppca(infile,maskfile,outfile,components,clobber,verbose,logfile,status): 

# startup parameters

    cmdLine=False
    status = 0
    labelsize = 32
    ticksize = 18
    xsize = 16
    ysize = 10
    lcolor = '#0000ff'
    lwidth = 1.0
    fcolor = '#ffff00'
    falpha = 0.2
    seterr(all="ignore") 

# log the call 

    hashline = '----------------------------------------------------------------------------'
    kepmsg.log(logfile,hashline,verbose)
    call = 'KEPPCA -- '
    call += 'infile='+infile+' '
    call += 'maskfile='+maskfile+' '
    call += 'outfile='+outfile+' '
    call += 'components='+components+' '
    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('KEPPCA 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 -- KEPPCA: ' + outfile + ' exists. Use --clobber'
        status = kepmsg.err(logfile,message,verbose)

# open input file

    status = 0
    instr = pyfits.open(infile,mode='readonly',memmap=True)
    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)

# input file data

    if status == 0:
        cards0 = instr[0].header.ascardlist()
        cards1 = instr[1].header.ascardlist()
        cards2 = instr[2].header.ascardlist()
        table = instr[1].data[:]
        maskmap = copy(instr[2].data)

# 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, flux_bkg, status = \
            kepio.readTPF(infile,'FLUX_BKG',logfile,verbose)
    if status == 0:
        kepid, channel, skygroup, module, output, quarter, season, \
            ra, dec, column, row, kepmag, xdim, ydim, flux_bkg_err, status = \
            kepio.readTPF(infile,'FLUX_BKG_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)
    if status == 0:
        kepid, channel, skygroup, module, output, quarter, season, \
            ra, dec, column, row, kepmag, xdim, ydim, pcorr1, status = \
            kepio.readTPF(infile,'POS_CORR1',logfile,verbose)
    if status == 0:
        kepid, channel, skygroup, module, output, quarter, season, \
            ra, dec, column, row, kepmag, xdim, ydim, pcorr2, status = \
            kepio.readTPF(infile,'POS_CORR2',logfile,verbose)

# read mask definition file

    if status == 0 and 'aper' not in maskfile.lower() and maskfile.lower() != 'all':
        maskx = array([],'int')
        masky = array([],'int')
        lines, status = kepio.openascii(maskfile,'r',logfile,verbose)
        for line in lines:
            line = line.strip().split('|')
            if len(line) == 6:
                y0 = int(line[3])
                x0 = int(line[4])
                line = line[5].split(';')
                for items in line:
                    try:
                        masky = numpy.append(masky,y0 + int(items.split(',')[0]))
                        maskx = numpy.append(maskx,x0 + int(items.split(',')[1]))
                    except:
                        continue
        status = kepio.closeascii(lines,logfile,verbose)
        if len(maskx) == 0 or len(masky) == 0:
            message = 'ERROR -- KEPPCA: ' + maskfile + ' contains no pixels.'
            status = kepmsg.err(logfile,message,verbose)

# subimage physical WCS data

    if status == 0:
        crpix1p = cards2['CRPIX1P'].value
        crpix2p = cards2['CRPIX2P'].value
        crval1p = cards2['CRVAL1P'].value
        crval2p = cards2['CRVAL2P'].value
        cdelt1p = cards2['CDELT1P'].value
        cdelt2p = cards2['CDELT2P'].value

# define new subimage bitmap...

    if status == 0 and 'aper' not in maskfile.lower() and maskfile.lower() != 'all':
        aperx = numpy.array([],'int')
        apery = numpy.array([],'int')
        aperb = numpy.array([],'int')
        for i in range(maskmap.shape[0]):
            for j in range(maskmap.shape[1]):
                aperx = numpy.append(aperx,crval1p + (j + 1 - crpix1p) * cdelt1p)
                apery = numpy.append(apery,crval2p + (i + 1 - crpix2p) * cdelt2p)
                if maskmap[i,j] == 0:
                    aperb = numpy.append(aperb,0)
                else:
                    aperb = numpy.append(aperb,1)
                    maskmap[i,j] = 1
                    for k in range(len(maskx)):
                        if aperx[-1] == maskx[k] and apery[-1] == masky[k]:
                            aperb[-1] = 3
                            maskmap[i,j] = 3

# ...or use old subimage bitmap

    if status == 0 and 'aper' in maskfile.lower():
        aperb = array([],'int')
        for i in range(maskmap.shape[0]):
            for j in range(maskmap.shape[1]):
                aperb = numpy.append(aperb,maskmap[i,j])

# ...or use all pixels

    if status == 0 and maskfile.lower() == 'all':
        aperb = array([],'int')
        for i in range(maskmap.shape[0]):
            for j in range(maskmap.shape[1]):
                if maskmap[i,j] == 0:
                    aperb = numpy.append(aperb,0)
                else:
                    aperb = numpy.append(aperb,3)
                    maskmap[i,j] = 3

# legal mask defined?

    if status == 0:
        if len(aperb) == 0:
            message = 'ERROR -- KEPPCA: no legal pixels within the subimage are defined.'
            status = kepmsg.err(logfile,message,verbose)
        
# identify principal components to be combined

    if status == 0:
        pcaout = []
        txt = components.strip().split(',')
        for work1 in txt:
            try:
                pcaout.append(int(work1.strip()))
            except:
                work2 = work1.strip().split('-')
                try:
                    for work3 in range(int(work2[0]),int(work2[1]) + 1):
                        pcaout.append(work3)
                except:
                    message = 'ERROR -- KEPPCA: cannot understand principal component list requested'
                    status = kepmsg.err(logfile,message,verbose)
    if status == 0:
        pcaout = set(sort(pcaout))

# flux pixel array size

    if status == 0:
        ntim = 0
        time = numpy.array([],dtype='float64')
        timecorr = numpy.array([],dtype='float32')
        cadenceno = numpy.array([],dtype='int')
        pixseries = numpy.array([],dtype='float32')
        errseries = numpy.array([],dtype='float32')
        bkgseries = numpy.array([],dtype='float32')
        berseries = numpy.array([],dtype='float32')
        quality = numpy.array([],dtype='float32')
        pos_corr1 = numpy.array([],dtype='float32')
        pos_corr2 = numpy.array([],dtype='float32')
        nrows = numpy.size(fluxpixels,0)
        npix = numpy.size(fluxpixels,1)

# remove NaN timestamps

        for i in range(nrows):
            if qual[i] == 0 and \
                    numpy.isfinite(barytime[i]) and \
                    numpy.isfinite(fluxpixels[i,ydim*xdim/2]) and \
                    numpy.isfinite(fluxpixels[i,1+ydim*xdim/2]):
                ntim += 1
                time = numpy.append(time,barytime[i])
                timecorr = numpy.append(timecorr,tcorr[i])
                cadenceno = numpy.append(cadenceno,cadno[i])
                pixseries = numpy.append(pixseries,fluxpixels[i])
                errseries = numpy.append(errseries,errpixels[i])
                bkgseries = numpy.append(bkgseries,flux_bkg[i])
                berseries = numpy.append(berseries,flux_bkg_err[i])
                quality = numpy.append(quality,qual[i])
                pos_corr1 = numpy.append(pos_corr1,pcorr1[i])
                pos_corr2 = numpy.append(pos_corr2,pcorr2[i])
        pixseries = numpy.reshape(pixseries,(-1,npix))
        errseries = numpy.reshape(errseries,(-1,npix))
        bkgseries = numpy.reshape(bkgseries,(-1,npix))
        berseries = numpy.reshape(berseries,(-1,npix))

# dummy columns for output file

    if status == 0:
        pdc_flux = numpy.empty(len(time)); pdc_flux[:] = numpy.nan
        pdc_flux_err = numpy.empty(len(time)); pdc_flux_err[:] = numpy.nan
        psf_centr1 = numpy.empty(len(time)); psf_centr1[:] = numpy.nan
        psf_centr1_err = numpy.empty(len(time)); psf_centr1_err[:] = numpy.nan
        psf_centr2 = numpy.empty(len(time)); psf_centr2[:] = numpy.nan
        psf_centr2_err = numpy.empty(len(time)); psf_centr2_err[:] = numpy.nan
        mom_centr1 = numpy.empty(len(time)); mom_centr1[:] = numpy.nan
        mom_centr1_err = numpy.empty(len(time)); mom_centr1_err[:] = numpy.nan
        mom_centr2 = numpy.empty(len(time)); mom_centr2[:] = numpy.nan
        mom_centr2_err = numpy.empty(len(time)); mom_centr2_err[:] = numpy.nan

# subtract mean over time from each pixel in the mask

    if status == 0:
        nmask = 0
        for i in range(npix):
            if aperb[i] == 3:
                nmask += 1
        work1 = numpy.zeros((len(pixseries),nmask))
        nmask = -1
        for i in range(npix):
            if aperb[i] == 3:
                nmask += 1
                maskedFlux = numpy.ma.masked_invalid(pixseries[:,i])
                pixMean = numpy.mean(maskedFlux)
                if numpy.isfinite(pixMean):
                    work1[:,nmask] = maskedFlux - pixMean
                else:
                    work1[:,nmask] = numpy.zeros((ntim))

# calculate covariance matrix

    if status == 0:
        work2 = work1.T
        covariance = numpy.cov(work2)

# determine eigenfunctions and eigenvectors of the covariance matrix
        
    if status == 0:
        [latent,coeff] = numpy.linalg.eig(covariance)

# projection of the data in the new space

    if status == 0:
        score = numpy.dot(coeff.T,work2).T

# construct new table data

    if status == 0:
        sap_flux = numpy.array([],'float32')
        sap_flux_err = numpy.array([],'float32')
        sap_bkg = numpy.array([],'float32')
        sap_bkg_err = numpy.array([],'float32')
        for i in range(len(time)):
            work1 = numpy.array([],'float64')
            work2 = numpy.array([],'float64')
            work3 = numpy.array([],'float64')
            work4 = numpy.array([],'float64')
            work5 = numpy.array([],'float64')
            for j in range(len(aperb)):
                if (aperb[j] == 3):
                    work1 = numpy.append(work1,pixseries[i,j])
                    work2 = numpy.append(work2,errseries[i,j])
                    work3 = numpy.append(work3,bkgseries[i,j])
                    work4 = numpy.append(work4,berseries[i,j])
            sap_flux = numpy.append(sap_flux,kepstat.sum(work1))
            sap_flux_err = numpy.append(sap_flux_err,kepstat.sumerr(work2))
            sap_bkg = numpy.append(sap_bkg,kepstat.sum(work3))
            sap_bkg_err = numpy.append(sap_bkg_err,kepstat.sumerr(work4))
        sap_mean = scipy.stats.stats.nanmean(sap_flux)

# coadd principal components

    if status == 0:
        pca_flux = numpy.zeros((len(sap_flux)))
        for i in range(nmask):
            if (i + 1) in pcaout:
                pca_flux = pca_flux + score[:,i]
        pca_flux += sap_mean

# construct output primary extension

    if status == 0:
        hdu0 = pyfits.PrimaryHDU()
        for i in range(len(cards0)):
            if cards0[i].key not in hdu0.header.ascardlist().keys():
                hdu0.header.update(cards0[i].key, cards0[i].value, cards0[i].comment)
            else:
                hdu0.header.ascardlist()[cards0[i].key].comment = cards0[i].comment
        status = kepkey.history(call,hdu0,outfile,logfile,verbose)
        outstr = HDUList(hdu0)

# construct output light curve extension

    if status == 0:
        col1 = Column(name='TIME',format='D',unit='BJD - 2454833',array=time)
        col2 = Column(name='TIMECORR',format='E',unit='d',array=timecorr)
        col3 = Column(name='CADENCENO',format='J',array=cadenceno)
        col4 = Column(name='SAP_FLUX',format='E',array=sap_flux)
        col5 = Column(name='SAP_FLUX_ERR',format='E',array=sap_flux_err)
        col6 = Column(name='SAP_BKG',format='E',array=sap_bkg)
        col7 = Column(name='SAP_BKG_ERR',format='E',array=sap_bkg_err)
        col8 = Column(name='PDCSAP_FLUX',format='E',array=pdc_flux)
        col9 = Column(name='PDCSAP_FLUX_ERR',format='E',array=pdc_flux_err)
        col10 = Column(name='SAP_QUALITY',format='J',array=quality)
        col11 = Column(name='PSF_CENTR1',format='E',unit='pixel',array=psf_centr1)
        col12 = Column(name='PSF_CENTR1_ERR',format='E',unit='pixel',array=psf_centr1_err)
        col13 = Column(name='PSF_CENTR2',format='E',unit='pixel',array=psf_centr2)
        col14 = Column(name='PSF_CENTR2_ERR',format='E',unit='pixel',array=psf_centr2_err)
        col15 = Column(name='MOM_CENTR1',format='E',unit='pixel',array=mom_centr1)
        col16 = Column(name='MOM_CENTR1_ERR',format='E',unit='pixel',array=mom_centr1_err)
        col17 = Column(name='MOM_CENTR2',format='E',unit='pixel',array=mom_centr2)
        col18 = Column(name='MOM_CENTR2_ERR',format='E',unit='pixel',array=mom_centr2_err)
        col19 = Column(name='POS_CORR1',format='E',unit='pixel',array=pos_corr1)
        col20 = Column(name='POS_CORR2',format='E',unit='pixel',array=pos_corr2)
        cols = ColDefs([col1,col2,col3,col4,col5,col6,col7,col8,col9,col10,col11, \
                            col12,col13,col14,col15,col16,col17,col18,col19,col20])
        hdu1 = new_table(cols)
        hdu1.header.update('TTYPE1','TIME','column title: data time stamps')
        hdu1.header.update('TFORM1','D','data type: float64')
        hdu1.header.update('TUNIT1','BJD - 2454833','column units: barycenter corrected JD')
        hdu1.header.update('TDISP1','D12.7','column display format')
        hdu1.header.update('TTYPE2','TIMECORR','column title: barycentric-timeslice correction')
        hdu1.header.update('TFORM2','E','data type: float32')
        hdu1.header.update('TUNIT2','d','column units: days')
        hdu1.header.update('TTYPE3','CADENCENO','column title: unique cadence number')
        hdu1.header.update('TFORM3','J','column format: signed integer32')
        hdu1.header.update('TTYPE4','SAP_FLUX','column title: aperture photometry flux')
        hdu1.header.update('TFORM4','E','column format: float32')
        hdu1.header.update('TUNIT4','e-/s','column units: electrons per second')
        hdu1.header.update('TTYPE5','SAP_FLUX_ERR','column title: aperture phot. flux error')
        hdu1.header.update('TFORM5','E','column format: float32')
        hdu1.header.update('TUNIT5','e-/s','column units: electrons per second (1-sigma)')
        hdu1.header.update('TTYPE6','SAP_BKG','column title: aperture phot. background flux')
        hdu1.header.update('TFORM6','E','column format: float32')
        hdu1.header.update('TUNIT6','e-/s','column units: electrons per second')
        hdu1.header.update('TTYPE7','SAP_BKG_ERR','column title: ap. phot. background flux error')
        hdu1.header.update('TFORM7','E','column format: float32')
        hdu1.header.update('TUNIT7','e-/s','column units: electrons per second (1-sigma)')
        hdu1.header.update('TTYPE8','PDCSAP_FLUX','column title: PDC photometry flux')
        hdu1.header.update('TFORM8','E','column format: float32')
        hdu1.header.update('TUNIT8','e-/s','column units: electrons per second')
        hdu1.header.update('TTYPE9','PDCSAP_FLUX_ERR','column title: PDC flux error')
        hdu1.header.update('TFORM9','E','column format: float32')
        hdu1.header.update('TUNIT9','e-/s','column units: electrons per second (1-sigma)')
        hdu1.header.update('TTYPE10','SAP_QUALITY','column title: aperture photometry quality flag')
        hdu1.header.update('TFORM10','J','column format: signed integer32')
        hdu1.header.update('TTYPE11','PSF_CENTR1','column title: PSF fitted column centroid')
        hdu1.header.update('TFORM11','E','column format: float32')
        hdu1.header.update('TUNIT11','pixel','column units: pixel')
        hdu1.header.update('TTYPE12','PSF_CENTR1_ERR','column title: PSF fitted column error')
        hdu1.header.update('TFORM12','E','column format: float32')
        hdu1.header.update('TUNIT12','pixel','column units: pixel')
        hdu1.header.update('TTYPE13','PSF_CENTR2','column title: PSF fitted row centroid')
        hdu1.header.update('TFORM13','E','column format: float32')
        hdu1.header.update('TUNIT13','pixel','column units: pixel')
        hdu1.header.update('TTYPE14','PSF_CENTR2_ERR','column title: PSF fitted row error')
        hdu1.header.update('TFORM14','E','column format: float32')
        hdu1.header.update('TUNIT14','pixel','column units: pixel')
        hdu1.header.update('TTYPE15','MOM_CENTR1','column title: moment-derived column centroid')
        hdu1.header.update('TFORM15','E','column format: float32')
        hdu1.header.update('TUNIT15','pixel','column units: pixel')
        hdu1.header.update('TTYPE16','MOM_CENTR1_ERR','column title: moment-derived column error')
        hdu1.header.update('TFORM16','E','column format: float32')
        hdu1.header.update('TUNIT16','pixel','column units: pixel')
        hdu1.header.update('TTYPE17','MOM_CENTR2','column title: moment-derived row centroid')
        hdu1.header.update('TFORM17','E','column format: float32')
        hdu1.header.update('TUNIT17','pixel','column units: pixel')
        hdu1.header.update('TTYPE18','MOM_CENTR2_ERR','column title: moment-derived row error')
        hdu1.header.update('TFORM18','E','column format: float32')
        hdu1.header.update('TUNIT18','pixel','column units: pixel')
        hdu1.header.update('TTYPE19','POS_CORR1','column title: col correction for vel. abbern')
        hdu1.header.update('TFORM19','E','column format: float32')
        hdu1.header.update('TUNIT19','pixel','column units: pixel')
        hdu1.header.update('TTYPE20','POS_CORR2','column title: row correction for vel. abbern')
        hdu1.header.update('TFORM20','E','column format: float32')
        hdu1.header.update('TUNIT20','pixel','column units: pixel')
        hdu1.header.update('EXTNAME','LIGHTCURVE','name of extension')
        for i in range(len(cards1)):
            if (cards1[i].key not in hdu1.header.ascardlist().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']):
                hdu1.header.update(cards1[i].key, cards1[i].value, cards1[i].comment)
        outstr.append(hdu1)

# construct output mask bitmap extension

    if status == 0:
        hdu2 = ImageHDU(maskmap)
        for i in range(len(cards2)):
            if cards2[i].key not in hdu2.header.ascardlist().keys():
                hdu2.header.update(cards2[i].key, cards2[i].value, cards2[i].comment)
            else:
                hdu2.header.ascardlist()[cards2[i].key].comment = cards2[i].comment
        outstr.append(hdu2)

# construct principal component table

    if status == 0:
        cols = []
        for i in range(nmask):
            colname = 'PC' + str(i + 1)
            col = Column(name=colname,format='E',unit='e-/s',array=score[:,i])
            cols.append(col)
        hdu3 = new_table(ColDefs(cols))
        hdu3.header.update('EXTNAME','PRINCIPAL_COMPONENTS','name of extension')
        for i in range(nmask):
            hdu3.header.update('TTYPE' + str(i + 1),'PC' + str(i + 1),'column title: principal component number' + str(i + 1))
            hdu3.header.update('TFORM' + str(i + 1),'E','column format: float32')
            hdu3.header.update('TUNIT' + str(i + 1),'e-/s','column units: electrons per sec')
        outstr.append(hdu3)

# write output file

    if status == 0:
        outstr.writeto(outfile,checksum=True)

# close input structure

    if status == 0:
        status = kepio.closefits(instr,logfile,verbose)	    

# plotting defaults

    if status == 0:
        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:
        pylab.figure(figsize=[xsize,ysize])
        pylab.clf()

# clean up x-axis unit

    if status == 0:
	intime0 = float(int(tstart / 100) * 100.0)
	ptime = time + bjdref - intime0
	xlab = 'BJD $-$ %d' % intime0

# clean up y-axis units

    if status == 0:
        pout = copy(score)
	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

# plot window

        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=[])

# plot principal components

        for i in range(nmask):
            pylab.plot(ptime,pout[:,i],linestyle='-',linewidth=lwidth)
        if not plotLatex:
            ylab = '10**%d electrons/sec' % nrm
        ylabel(ylab, {'color' : 'k'})
        grid()

# plot ranges

        pylab.xlim(xmin-xr*0.01,xmax+xr*0.01)
        pylab.ylim(ymin-yr*0.01,ymax+yr*0.01)

# plot output data

        ax = pylab.axes([0.06,0.09,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()
        setp(labels, 'rotation', 90)

# clean up y-axis units

    if status == 0:
        pout = copy(pca_flux)
	nrm = len(str(int(pout.max())))-1
	pout = pout / 10**nrm
	ylab = '10$^%d$ e$^-$ s$^{-1}$' % nrm

# data limits

	ymin = pout.min()
	ymax = pout.max()
	yr = ymax - ymin
        ptime = numpy.insert(ptime,[0],[ptime[0]]) 
        ptime = numpy.append(ptime,[ptime[-1]])
        pout = numpy.insert(pout,[0],[0.0]) 
        pout = numpy.append(pout,0.0)

# plot time coadded principal component series

        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

        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 cmdLine: 
            pylab.show()
        else: 
            pylab.ion()
            pylab.plot([])
            pylab.ioff()
	
# stop time

    if status == 0:
        kepmsg.clock('KEPPCA ended at',logfile,verbose)

    return
Esempio n. 33
0
def rmsestimation(quarter, timescale):
    """
    do stastitical analysis of rms cdpp(expected snr) of certain quarter
    """
    #read data from keplerstellar catalog and match lightcurve in lc database
    cata_path = '../catalog/cumulative.csv'
    data_path = '/scratch/kepler_data/'
    kepid = kepio.get_id(cata_path)
    all_rms = []
    own_kid = []

    for i, k_id in enumerate(kepid[:100]):
        #print('This is '+str(kepid[i]))
        #lc path here
        file_dir = kepio.pathfinder(k_id, data_path, quarter)
        try:
            filename = glob.glob(file_dir)
            name = filename[0]
            #open file and read time keys
            instr = fits.open(name)
            tstart, tstop, bjdref, cadence = kepio.timekeys(instr, filename)

            #read lc
            hdu = instr[1]
            time = hdu.data.TIME
            time = time + bjdref - 2454900
            flux = hdu.data.PDCSAP_FLUX
            #filter data
            work1 = np.array([time, flux])
            work1 = np.rot90(work1, 3)
            work1 = work1[~np.isnan(work1).any(1)]

            intime = work1[:, 1]
            indata = work1[:, 0]
            #split lc
            intime, indata = keputils.split(intime, indata, gap_width=0.75)
            #calculate breaking points
            bkspaces = np.logspace(np.log10(0.5), np.log10(20), num=20)
            #calculate spline to every data points
            spline = kepspline.choose_kepler_spline(intime,
                                                    indata,
                                                    bkspaces,
                                                    penalty_coeff=1.0,
                                                    verbose=False)[0]
            if spline is None:
                raise ValueError("faied to fit spline")

            #flatten the data array
            intime = np.concatenate(intime).ravel()
            indata = np.concatenate(indata).ravel()
            spline = np.concatenate(spline).ravel()
            #normalized flux using spline
            nordata = indata / spline
            #calculte runing stddev
            stddev = kepstat.running_frac_std(intime, nordata,
                                              timescale / 24) * 1.0e6
            #cdpp
            cdpp = stddev / math.sqrt(timescale * 3600.0 / cadence)
            # filter cdpp
            for i in range(len(cdpp)):
                if cdpp[i] > np.median(cdpp) * 10.0:
                    cdpp[i] = cdpp[i - 1]

            #calculte RMS cdpp
            rms = kepstat.rms(cdpp, np.zeros(len(stddev)))
            rmscdpp = np.ones((len(cdpp)), dtype='float32') * rms
            if rms > 200:
                plt.figure(figsize=(10, 8))
                #plt.hist(cdpp, bins =25, color = 'gold', fill = True, edgecolor = 'black', linewidth = 2.0)
                #plt.ylabel('Count')
                #plt.xlabel('CDPP/6.5hrs(ppm)')
                #plt.savefig("./test/KOIQ"+str(quarter)+str(k_id)+"cdpp.png")
                plt.scatter(intime, nordata, marker='.')
                plt.savefig("./test/KOIQ" + str(quarter) + str(k_id) +
                            "display_lc.png")
            # print('%d has RMS %.1fhr CDPP = %d ppm\n' % (k_id,timescale, rms))
            all_rms.append(rms)
            own_kid.append(k_id)

        except IndexError:
            pass

    c, low, upp = sigmaclip(all_rms, 3, 3)
    plt.figure(figsize=(10, 8))
    plt.hist(c,
             bins=25,
             range=(low, upp),
             fill=True,
             edgecolor='black',
             linewidth=2.0)
    plt.ylabel('Count')
    plt.xlabel('RMSCDPP(ppm)')
    #plt.savefig("./result/KOIQ"+str(quarter)+"rms.png")
    result = np.transpose([own_kid, all_rms])
Esempio n. 34
0
def kepdip(infile,outfile,datacol,dmethod,kneighb,hstd,plot,plotlab,
              clobber,verbose,logfile,status): 
    """
    Perform a k-nearest neighbor regression analysis.
    """

## startup parameters

    status = 0
    labelsize = 24
    ticksize = 16
    xsize = 16
    ysize = 6
    lcolor = '#0000ff'
    lwidth = 1.0
    fcolor = '#9AFF9A'
    falpha = 0.3

## log the call 

    hashline = '----------------------------------------------------------------------------'
    kepmsg.log(logfile,hashline,verbose)
    call = 'KEPDIP -- '
    call += 'infile='+infile+' '
    call += 'outfile='+outfile+' '
    call += 'datacol='+str(datacol)+' '
    call += 'dmethod='+dmethod+' '
    call += 'hstd='+str(hstd)+' '
    call += 'kneighb='+str(kneighb)+' '
    plotit = 'n'
    if (plot): plotit = 'y'
    call += 'plot='+plotit+ ' '
    call += 'plotlab='+str(plotlab)+' '
    overwrite = 'n'
    if (clobber): overwrite = 'y'
    call += 'clobber='+overwrite+ ' '
    chatter = 'n'
    if (verbose): chatter = 'y'
    call += 'verbose='+chatter+' '
    call += 'logfile='+logfile
    kepmsg.log(logfile,call+'\n',verbose)

## start time

    kepmsg.clock('KEPDIP started at',logfile,verbose)

## test log file

    logfile = kepmsg.test(logfile)

## clobber output file

    if clobber: status = kepio.clobber(outfile,logfile,verbose)
    if kepio.fileexists(outfile): 
	    message = 'ERROR -- KEPDIP: ' + outfile + ' exists. Use clobber=yes'
	    status = kepmsg.err(logfile,message,verbose)

## open input file

    if status == 0:
        instr, status = kepio.openfits(infile,'readonly',logfile,verbose)
        tstart, tstop, bjdref, cadence, status = kepio.timekeys(instr,infile,logfile,verbose,status)
        if cadence == 0.0: 
            tstart, tstop, ncad, cadence, status = kepio.cadence(instr,infile,logfile,verbose,status) 
    if status == 0:
        try:
            work = instr[0].header['FILEVER']
            cadenom = 1.0
        except:
            cadenom = cadence

## fudge non-compliant FITS keywords with no values

    if status == 0:
        instr = kepkey.emptykeys(instr,file,logfile,verbose)

## read table structure

    if status == 0:
	table, status = kepio.readfitstab(infile,instr[1],logfile,verbose)

# read time and flux columns

    if status == 0:
        barytime, status = kepio.readtimecol(infile,table,logfile,verbose)
    if status == 0:
        flux, status = kepio.readfitscol(infile,instr[1].data,datacol,logfile,verbose)

# filter input data table

    if status == 0:
        try:
            nanclean = instr[1].header['NANCLEAN']
        except:
            naxis2 = 0
            for i in range(len(table.field(0))):
                if (numpy.isfinite(barytime[i]) and numpy.isfinite(flux[i]) and flux[i] != 0.0):
                    table[naxis2] = table[i]
                    naxis2 += 1
            instr[1].data = table[:naxis2]
            comment = 'NaN cadences removed from data'
            status = kepkey.new('NANCLEAN',True,comment,instr[1],outfile,logfile,verbose)

## read table columns

    if status == 0:
	try:
            intime = instr[1].data.field('barytime')
	except:
            intime, status = kepio.readfitscol(infile,instr[1].data,'time',logfile,verbose)
	indata, status = kepio.readfitscol(infile,instr[1].data,datacol,logfile,verbose)
    if status == 0:
        intime = intime + bjdref
        indata = indata / cadenom

## smooth data

    if status == 0:
        # outdata = knn_predict(intime, indata, kmethod, kneighb)
	outdata_t, outdata_l, outdata_fmt = _find_dips(intime, indata, dmethod, kneighb, hstd)

## comment keyword in output file

    if status == 0:
        status = kepkey.history(call,instr[0],outfile,logfile,verbose)

## clean up x-axis unit

    if status == 0:
	intime0 = float(int(tstart / 100) * 100.0)
        if intime0 < 2.4e6: intime0 += 2.4e6
	ptime = intime - intime0
	ptime2 = outdata_t - intime0
        # print ptime,intime,intime0
	xlab = 'BJD $-$ %d' % intime0

## clean up y-axis units

    if status == 0:
        pout = indata * 1.0
        pout2 = outdata_l * 1.0 
	nrm = len(str(int(numpy.nanmax(pout))))-1
	pout = pout / 10**nrm
	pout2 = pout2 / 10**nrm
	ylab = '10$^%d$ %s' % (nrm, plotlab)

## data limits

	xmin = numpy.nanmin(ptime)
	xmax = numpy.nanmax(ptime)
	ymin = numpy.min(pout)
	ymax = numpy.nanmax(pout)
	xr = xmax - xmin
	yr = ymax - ymin
        ptime = insert(ptime,[0],[ptime[0]]) 
        ptime = append(ptime,[ptime[-1]])
        pout = insert(pout,[0],[0.0]) 
        pout = append(pout,0.0)
	if (len(ptime2) > 0):
	        ptime2 = insert(ptime2,[0],[ptime2[0]]) 
        	ptime2 = append(ptime2,[ptime2[-1]])
	        pout2 = insert(pout2,[0],[0.0]) 
        	pout2 = append(pout2,0.0)

## plot light curve

    if status == 0 and plot:
        try:
            params = {'backend': 'png',
                      'axes.linewidth': 2.5,
                      'axes.labelsize': labelsize,
                      'axes.font': 'sans-serif',
                      'axes.fontweight' : 'bold',
                      'text.fontsize': 12,
                      'legend.fontsize': 12,
                      'xtick.labelsize': ticksize,
                      'ytick.labelsize': ticksize}
            rcParams.update(params)
        except:
            print('ERROR -- KEPDIP: install latex for scientific plotting')
            status = 1
    if status == 0 and plot:
        pylab.figure(1,figsize=[xsize,ysize])

## plot regression data

        ax = pylab.axes([0.06,0.1,0.93,0.87])
        pylab.gca().xaxis.set_major_formatter(pylab.ScalarFormatter(useOffset=False))
        pylab.gca().yaxis.set_major_formatter(pylab.ScalarFormatter(useOffset=False))
	pylab.scatter(ptime, pout, color='#214CAE', s=2)

	if (len(ptime2) > 0):
	        pylab.scatter(ptime2, pout2, color='#47AE10', s=35, marker='o', linewidths=2, alpha=0.4)
        xlabel(xlab, {'color' : 'k'})
        ylabel(ylab, {'color' : 'k'})
        xlim(xmin-xr*0.01,xmax+xr*0.01)
        if ymin >= 0.0: 
            ylim(ymin-yr*0.01,ymax+yr*0.01)
        else:
            ylim(1.0e-10,ymax+yr*0.01)
        pylab.grid()
        pylab.draw()
        pylab.savefig(re.sub('\.\S+','.png',outfile),dpi=100)

## write output file

    if status == 0:
        for i in range(len(outdata_fmt)):
            instr[1].data.field(datacol)[i] = outdata_fmt[i]
        instr.writeto(outfile)
    
## close input file

    if status == 0:
        status = kepio.closefits(instr,logfile,verbose)	    

## end time

    if (status == 0):
	    message = 'KEPDIP completed at'
    else:
	    message = '\nKEPDIP aborted at'
    kepmsg.clock(message,logfile,verbose)
Esempio n. 35
0
def kepconvert(infile,outfile,conversion,columns,baddata,clobber,verbose,logfile,status): 

# startup parameters

    status = 0

# log the call 

    hashline = '----------------------------------------------------------------------------'
    kepmsg.log(logfile,hashline,verbose)
    call = 'KEPCONVERT -- '
    call += 'infile='+infile+' '
    call += 'outfile='+outfile+' '
    call += 'conversion='+conversion+' '
    call += 'columns='+columns+ ' '
    writebad = 'n'
    if (baddata): writebad = 'y'
    call += 'baddata='+writebad+ ' '
    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('KEPCONVERT started at',logfile,verbose)

# test log file

    logfile = kepmsg.test(logfile)

# data columns

    if status == 0:
        colnames = columns.strip().split(',')
        ncol = len(colnames)
        if ncol < 1:
            message = 'ERROR -- KEPCONVERT: no data columns specified'
            status = kepmsg.err(logfile,message,verbose)

# input file exists

    if status == 0 and not kepio.fileexists(infile):
        message = 'ERROR -- KEPCONVERT: input file '+infile+' does not exist'
        status = kepmsg.err(logfile,message,verbose)

# clobber output file

    if status == 0:
        if clobber: status = kepio.clobber(outfile,logfile,verbose)
        if kepio.fileexists(outfile): 
            message = 'ERROR -- KEPCONVERT: ' + outfile + ' exists. Use clobber=yes'
            status = kepmsg.err(logfile,message,verbose)

# open FITS input file


    if status == 0 and conversion == 'fits2asc':
        instr, status = kepio.openfits(infile,'readonly',logfile,verbose)
        tstart, tstop, bjdref, cadence, status = kepio.timekeys(instr,infile,logfile,verbose,status)

# read FITS table data

    if status == 0 and conversion == 'fits2asc':
        table, status = kepio.readfitstab(infile,instr[1],logfile,verbose)

# check columns exist in FITS file
    if not baddata and status == 0 and conversion == 'fits2asc':
	    try:
	    	qualcol = table.field('SAP_QUALITY') == 0
	    except:
	    	message = 'No SAP_QUALITY column in data, are you using an old FITS file?'
	    	status = kepmsg.err(logfile,message,verbose)

    if status == 0 and conversion == 'fits2asc':
        work = []
        for colname in colnames:
            try:
                if colname.lower() == 'time':
                    work.append(table.field(colname) + bjdref)
                else:
                    work.append(table.field(colname))
            except:
                message = 'ERROR -- KEPCONVERT: no column ' + colname + ' in ' + infile
                status = kepmsg.err(logfile,message,verbose)
        if not baddata:
            for i in range(len(work)):
                work[i] = work[i][qualcol]
# close input file

    if status == 0 and conversion == 'fits2asc':
        status = kepio.closefits(instr,logfile,verbose)

## write output file

    if status == 0 and conversion == 'fits2asc':
        # table, status = kepio.openascii(outfile,'w',logfile,verbose)
        # for i in range(len(work[0])):
            # txt = ''
            # for j in range(len(work)):
                # if numpy.isfinite(work[j][i]):
                    # txt += str(work[j][i]) + ' '
            # txt = txt.strip()
            # if len(re.sub('\s+',',',txt).split(',')) == ncol:
                # table.write(txt + '\n')
        # status = kepio.closeascii(table,logfile,verbose)
        savetxt(outfile,array(work).T)
    	
## open and read ASCII input file

    if status == 0 and conversion == 'asc2fits':
        table, status = kepio.openascii(infile,'r',logfile,verbose)

## organize ASCII table into arrays

    if status == 0 and conversion == 'asc2fits':
        work = []
        for i in range(ncol):
            work.append([])
        nline = 0
        for line in table:
            line = line.strip()
            line = re.sub('\s+',',',line)
            line = re.sub('\|',',',line)
            line = re.sub(';',',',line)
            if '#' not in line:
                nline + 1
                line = line.split(',')
                if len(line) == ncol:
                    for i in range(len(line)):
                        try:
                            work[i].append(float(line[i]))
                        except:
                            message = 'ERROR --KEPCONVERT: ' + str(line[i]) + ' is not float'
                            status = kepmsg.err(logfile,message,verbose)
                            break
                else:
                    message  = 'ERROR --KEPCONVERT: ' + str(ncol) + ' columns required but '
                    message += str(len(line)) + ' columns supplied by ' + infile
                    message += ' at line' + str(nline)
                    status = kepmsg.err(logfile,message,verbose)
                    break
        for i in range(ncol):
            work[i] = numpy.array(work[i],dtype='float64')

## timing keywords for output file

    if status == 0 and conversion == 'asc2fits':
        for i in range(ncol):
            if 'time' in colnames[i].lower():
                if work[i][1] > 54000.0 and work[i][1] < 60000.0:
                    work[i] += 2.4e6
#                work[i] += 2.4553e6
                tstart = work[i].min()
                tstop = work[i].max()
                lc_start = tstart
                lc_end = tstop
                if lc_start > 2.4e6: lc_start -= 2.4e6
                if lc_end > 2.4e6: lc_end -= 2.4e6
                dts = []
                for j in range(1,len(work[i])):
                   dts.append(work[i][j] - work[i][j-1])
                dts = numpy.array(dts,dtype='float32')
                cadence = numpy.median(dts)
                if cadence * 86400.0 > 58.0 and cadence * 86400.0 < 61.0:
                    obsmode = 'short cadence'
                elif cadence * 86400.0 > 1600.0 and cadence * 86400.0 < 2000.0:
                    obsmode = 'long cadence'
                else:
                    obsmode = 'unknown'

## Create the outfile primary extension

    if status == 0 and conversion == 'asc2fits':
        hdu0 = PrimaryHDU()
        try:
            hdu0.header.update('EXTNAME','PRIMARY','name of extension')
            hdu0.header.update('EXTVER',1.0,'extension version number')
            hdu0.header.update('ORIGIN','NASA/Ames','organization that generated this file')
            hdu0.header.update('DATE',time.asctime(time.localtime()),'file creation date')
            hdu0.header.update('CREATOR','kepconvert','SW version used to create this file')
            hdu0.header.update('PROCVER','None','processing script version')
            hdu0.header.update('FILEVER','2.0','file format version')
            hdu0.header.update('TIMVERSN','OGIP/93-003','OGIP memo number for file format')
            hdu0.header.update('TELESCOP','Kepler','telescope')
            hdu0.header.update('INSTRUME','Kepler photometer','detector type')
            hdu0.header.update('OBJECT','Unknown','string version of kepID')
            hdu0.header.update('KEPLERID','Unknown','unique Kepler target identifier')
            hdu0.header.update('CHANNEL','Unknown','CCD channel')
            hdu0.header.update('SKYGROUP','Unknown','roll-independent location of channel')
            hdu0.header.update('MODULE','Unknown','CCD module')
            hdu0.header.update('OUTPUT','Unknown','CCD output')
            hdu0.header.update('QUARTER','Unknown','mission quarter during which data was collected')
            hdu0.header.update('SEASON','Unknown','mission season during which data was collected')
            hdu0.header.update('DATA_REL','Unknown','version of data release notes describing data')
            hdu0.header.update('OBSMODE',obsmode,'observing mode')
            hdu0.header.update('RADESYS','Unknown','reference frame of celestial coordinates')
            hdu0.header.update('RA_OBJ','Unknown','[deg] right ascension from KIC')
            hdu0.header.update('DEC_OBJ','Unknown','[deg] declination from KIC')
            hdu0.header.update('EQUINOX',2000.0,'equinox of celestial coordinate system')
            hdu0.header.update('PMRA','Unknown','[arcsec/yr] RA proper motion')
            hdu0.header.update('PMDEC','Unknown','[arcsec/yr] Dec proper motion')
            hdu0.header.update('PMTOTAL','Unknown','[arcsec/yr] total proper motion')
            hdu0.header.update('PARALLAX','Unknown','[arcsec] parallax')
            hdu0.header.update('GLON','Unknown','[deg] galactic longitude')
            hdu0.header.update('GLAT','Unknown','[deg] galactic latitude')
            hdu0.header.update('GMAG','Unknown','[mag] SDSS g band magnitude from KIC')
            hdu0.header.update('RMAG','Unknown','[mag] SDSS r band magnitude from KIC')
            hdu0.header.update('IMAG','Unknown','[mag] SDSS i band magnitude from KIC')
            hdu0.header.update('ZMAG','Unknown','[mag] SDSS z band magnitude from KIC')
            hdu0.header.update('D51MAG','Unknown','[mag] D51 magnitude, from KIC')
            hdu0.header.update('JMAG','Unknown','[mag] J band magnitude from 2MASS')
            hdu0.header.update('HMAG','Unknown','[mag] H band magnitude from 2MASS')
            hdu0.header.update('KMAG','Unknown','[mag] K band magnitude from 2MASS')
            hdu0.header.update('KEPMAG','Unknown','[mag] Kepler magnitude (Kp) from KIC')
            hdu0.header.update('GRCOLOR','Unknown','[mag] (g-r) color, SDSS bands')
            hdu0.header.update('JKCOLOR','Unknown','[mag] (J-K) color, 2MASS bands')
            hdu0.header.update('GKCOLOR','Unknown','[mag] (g-K) color, SDSS g - 2MASS K')
            hdu0.header.update('TEFF','Unknown','[K] effective temperature from KIC')
            hdu0.header.update('LOGG','Unknown','[cm/s2] log10 surface gravity from KIC')
            hdu0.header.update('FEH','Unknown','[log10([Fe/H])] metallicity from KIC')
            hdu0.header.update('EBMINUSV','Unknown','[mag] E(B-V) redenning from KIC')
            hdu0.header.update('AV','Unknown','[mag] A_v extinction from KIC')
            hdu0.header.update('RADIUS','Unknown','[solar radii] stellar radius from KIC')
            hdu0.header.update('TMINDEX','Unknown','unique 2MASS catalog ID from KIC')
            hdu0.header.update('SCPID','Unknown','unique SCP processing ID from KIC') 
            hdulist = HDUList(hdu0)
        except:
            message = 'ERROR -- KEPCONVERT: cannot create primary extension in ' + outfile
            status = kepmsg.err(logfile,message,verbose)
            
## create the outfile HDU 1 extension

    if status == 0 and conversion == 'asc2fits':
        try:
            fitscol = []
            for i in range(ncol):
                fitscol.append(Column(name=colnames[i],format='D',array=work[i]))
            fitscols = ColDefs(fitscol)
            hdu1 = new_table(fitscols)
            hdulist.append(hdu1)
            hdu1.header.update('INHERIT',True,'inherit primary keywords')
            hdu1.header.update('EXTNAME','LIGHTCURVE','name of extension')
            hdu1.header.update('EXTVER',1,'extension version number')
            hdu1.header.update('TELESCOP','Kepler','telescope')
            hdu1.header.update('INSTRUME','Kepler photometer','detector type')
            hdu1.header.update('OBJECT','Unknown','string version of kepID')
            hdu1.header.update('KEPLERID','Unknown','unique Kepler target identifier')
            hdu1.header.update('RADESYS','Unknown','reference frame of celestial coordinates')
            hdu1.header.update('RA_OBJ','Unknown','[deg] right ascension from KIC')
            hdu1.header.update('DEC_OBJ','Unknown','[deg] declination from KIC')
            hdu1.header.update('EQUINOX',2000.0,'equinox of celestial coordinate system')
            hdu1.header.update('TIMEREF','Unknown','barycentric correction applied to times')
            hdu1.header.update('TASSIGN','Unknown','where time is assigned')
            hdu1.header.update('TIMESYS','Unknown','time system is barycentric JD')
            hdu1.header.update('BJDREFI',0.0,'integer part of BJD reference date')
            hdu1.header.update('BJDREFF',0.0,'fraction of day in BJD reference date')
            hdu1.header.update('TIMEUNIT','Unknown','time unit for TIME, TSTART and TSTOP')
            hdu1.header.update('TSTART',tstart,'observation start time in JD - BJDREF')
            hdu1.header.update('TSTOP',tstop,'observation stop time in JD - BJDREF')
            hdu1.header.update('LC_START',lc_start,'observation start time in MJD')
            hdu1.header.update('LC_END',lc_end,'observation stop time in MJD')
            hdu1.header.update('TELAPSE',tstop-tstart,'[d] TSTOP - TSTART')
            hdu1.header.update('LIVETIME','Unknown','[d] TELAPSE multiplied by DEADC')
            hdu1.header.update('EXPOSURE','Unknown','[d] time on source')
            hdu1.header.update('DEADC','Unknown','deadtime correction')
            hdu1.header.update('TIMEPIXR','Unknown','bin time beginning=0 middle=0.5 end=1')
            hdu1.header.update('TIERRELA','Unknown','[d] relative time error')
            hdu1.header.update('TIERABSO','Unknown','[d] absolute time error')
            hdu1.header.update('INT_TIME','Unknown','[s] photon accumulation time per frame')
            hdu1.header.update('READTIME','Unknown','[s] readout time per frame')
            hdu1.header.update('FRAMETIM','Unknown','[s] frame time (INT_TIME + READTIME)')
            hdu1.header.update('NUM_FRM','Unknown','number of frames per time stamp')
            hdu1.header.update('TIMEDEL','Unknown','[d] time resolution of data')
            hdu1.header.update('DATE-OBS','Unknown','TSTART as UT calendar date')
            hdu1.header.update('DATE-END','Unknown','TSTOP as UT calendar date')
            hdu1.header.update('BACKAPP','Unknown','background is subtracted')
            hdu1.header.update('DEADAPP','Unknown','deadtime applied')
            hdu1.header.update('VIGNAPP','Unknown','vignetting or collimator correction applied')
            hdu1.header.update('GAIN','Unknown','channel gain [electrons/count]')
            hdu1.header.update('READNOIS','Unknown','read noise [electrons]')
            hdu1.header.update('NREADOUT','Unknown','number of reads per cadence')
            hdu1.header.update('TIMSLICE','Unknown','time-slice readout sequence section')
            hdu1.header.update('MEANBLCK','Unknown','FSW mean black level [count]')
            hdu1.header.update('PDCSAPFL','Unknown','SAP PDC processing flags (bit code)')
            hdu1.header.update('PDCDIAFL','Unknown','DIA PDC processing flags (bit code)')
            hdu1.header.update('MISPXSAP','Unknown','no of optimal aperture pixels missing from SAP')
            hdu1.header.update('MISPXDIA','Unknown','no of optimal aperture pixels missing from DIA')
            hdu1.header.update('CROWDSAP','Unknown','crowding metric evaluated over SAP opt. ap.')
            hdu1.header.update('CROWDDIA','Unknown','crowding metric evaluated over DIA aperture')
        except:
            message = 'ERROR -- KEPCONVERT: cannot create light curve extension in ' + outfile
            status = kepmsg.err(logfile,message,verbose)

## history keyword in output file

    if status == 0 and conversion == 'asc2fits':
        status = kepkey.history(call,hdu0,outfile,logfile,verbose)

## filter data table

    if status == 0 and conversion == 'asc2fits':
        instr, status = kepio.filterNaN(hdulist,colnames[min(array([1,len(colnames)-1],dtype='int'))],
                                        outfile,logfile,verbose)

## write output FITS file

    if status == 0 and conversion == 'asc2fits':
        hdulist.writeto(outfile,checksum=True)

## end time

    if (status == 0):
	    message = 'KEPCONVERT completed at'
    else:
	    message = '\nKEPCONVERT aborted at'
    kepmsg.clock(message,logfile,verbose)
Esempio n. 36
0
def kepdynamic(infile,outfile,fcol,pmin,pmax,nfreq,deltat,nslice,
          plot,plotscale,cmap,clobber,verbose,logfile,status,cmdLine=False): 

# startup parameters

    status = 0
    labelsize = 24
    ticksize = 16
    xsize = 12
    ysize = 6
    lcolor = '#0000ff'
    lwidth = 1.0
    fcolor = '#ffff00'
    falpha = 0.2
    numpy.seterr(all="ignore") 

# log the call 

    hashline = '----------------------------------------------------------------------------'
    kepmsg.log(logfile,hashline,verbose)
    call = 'KEPDYNAMIC -- '
    call += 'infile='+infile+' '
    call += 'outfile='+outfile+' '
    call += 'fcol='+fcol+' '
    call += 'pmin='+str(pmin)+' '
    call += 'pmax='+str(pmax)+' '
    call += 'nfreq='+str(nfreq)+' '
    call += 'deltat='+str(deltat)+' '
    call += 'nslice='+str(nslice)+' '
    plotit = 'n'
    if (plot): plotit = 'y'
    call += 'plot='+plotit+ ' '
    call += 'plotscale='+plotscale+ ' '
    call += 'cmap='+str(cmap)+' '
    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('Start time is',logfile,verbose)

# test log file

    logfile = kepmsg.test(logfile)

# error checking

    if status == 0 and pmin >= pmax:
        message = 'ERROR -- KEPDYNAMIC: PMIN must be less than PMAX'
        status = kepmsg.err(logfile,message,verbose)


# clobber output file

    if clobber: status = kepio.clobber(outfile,logfile,verbose)
    if kepio.fileexists(outfile): 
        message = 'ERROR -- KEPDYNAMIC: ' + outfile + ' exists. Use clobber'
        status = kepmsg.err(logfile,message,verbose)

# plot color map

    if status == 0 and cmap == 'browse':
        status = keplab.cmap_plot()

# 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 columns

    if status == 0:
        barytime, status = kepio.readtimecol(infile,instr[1].data,logfile,verbose)
    if status == 0:
        signal, status = kepio.readfitscol(infile,instr[1].data,fcol,logfile,verbose)
    if status == 0:
        barytime = barytime + bjdref
        signal = signal / cadenom

# remove infinite data from time series

    if status == 0:
	    incols = [barytime, signal]
	    outcols = kepstat.removeinfinlc(signal, incols)
	    barytime = outcols[0] 
	    signal = outcols[1]

# period to frequency conversion

    if status == 0:
        fmin = 1.0 / pmax
        fmax = 1.0 / pmin
        deltaf = (fmax - fmin) / nfreq

# determine bounds of time slices

    if status == 0:
        t1 = []; t2 = []
        dt = barytime[-1] - barytime[0]
        dt -= deltat
        if dt < 0:
            message = 'ERROR -- KEPDYNAMIC: time slices are larger than data range'
            status = kepmsg.err(logfile,message,verbose)
        ds = dt / (nslice - 1)
        for i in range(nslice):
            t1.append(barytime[0] + ds * float(i))
            t2.append(barytime[0] + deltat + ds * float(i))

# loop through time slices

    if status == 0:
        dynam = []
        for i in range(nslice):
            x = []; y = []
            for j in range(len(barytime)):
                if (barytime[j] >= t1[i] and barytime[j] <= t2[i]):
                    x.append(barytime[j])
                    y.append(signal[j])
            x = array(x,dtype='float64')
            y = array(y,dtype='float32')
            y = y - median(y)

# determine FT power

	    fr, power = kepfourier.ft(x,y,fmin,fmax,deltaf,False)
            for j in range(len(power)):
                dynam.append(power[j])
            print('Timeslice: %.4f  Pmax: %.2E' % ((t2[i] + t1[i]) / 2, power.max()))

# define shape of results array

        dynam = array(dynam,dtype='float64')
        dynam.shape = len(t1),len(power)

# write output file

    if status == 0:
        instr.append(ImageHDU())
        instr[-1].data = dynam.transpose() 
        instr[-1].header.update('EXTNAME','DYNAMIC FT','extension name')
        instr[-1].header.update('WCSAXES',2,'number of WCS axes')
        instr[-1].header.update('CRPIX1',0.5,'reference pixel along axis 1')
        instr[-1].header.update('CRPIX2',0.5,'reference pixel along axis 2')
        instr[-1].header.update('CRVAL1',t1[0],'time at reference pixel (BJD)')
        instr[-1].header.update('CRVAL2',fmin,'frequency at reference pixel (1/day)')
        instr[-1].header.update('CDELT1',(barytime[-1] - barytime[0]) / nslice,
                                'pixel scale in dimension 1 (days)')
        instr[-1].header.update('CDELT2',deltaf,'pixel scale in dimension 2 (1/day)')
        instr[-1].header.update('CTYPE1','BJD','data type of dimension 1')
        instr[-1].header.update('CTYPE2','FREQUENCY','data type of dimension 2')
        instr.writeto(outfile)
    
# history keyword in output file

    if status == 0:
	    status = kepkey.history(call,instr[0],outfile,logfile,verbose)

# close input file

    if status == 0:
	    status = kepio.closefits(instr,logfile,verbose)	    

# clean up x-axis unit

    if status == 0:
	time0 = float(int(barytime[0] / 100) * 100.0)
	barytime = barytime - time0
	xlab = 'BJD $-$ %d' % time0

# image intensity min and max

    if status == 0:
        if 'rithmic' in plotscale:
            dynam = numpy.log10(dynam)
        elif 'sq' in plotscale:
            dynam = numpy.sqrt(dynam)
        elif 'logoflog' in plotscale:
            dynam = numpy.log10(numpy.abs(numpy.log10(dynam)))
#        dynam = -dynam
        nstat = 2; pixels = []
        for i in range(dynam.shape[0]):
            for j in range(dynam.shape[1]):
                pixels.append(dynam[i,j])
        pixels = array(sort(pixels),dtype=float32)
        if int(float(len(pixels)) * 0.1 + 0.5) > nstat:
            nstat = int(float(len(pixels)) * 0.1 + 0.5)
        zmin = median(pixels[:nstat])
        zmax = median(pixels[-1:])
        if isnan(zmax): 
            zmax = median(pixels[-nstat/2:])
        if isnan(zmax): 
            zmax = numpy.nanmax(pixels)        

# plot power spectrum

    if status == 0 and plot:
        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)
        pylab.figure(1,figsize=[xsize,ysize])
        pylab.clf()
        pylab.axes([0.08,0.113,0.91,0.86])
        dynam = dynam.transpose()
        pylab.imshow(dynam,origin='lower',aspect='auto',cmap=cmap,vmin=zmin,vmax=zmax,
                     extent=[barytime[0],barytime[-1],fmin,fmax],interpolation='bilinear')            
        xlabel(xlab, {'color' : 'k'})
        ylabel(r'Frequency (d$^{-1}$)', {'color' : 'k'})
        grid()
        pylab.savefig(re.sub('\.\S+','.png',outfile),dpi=100)

# render plot

        if cmdLine: 
            pylab.show()
        else: 
            pylab.ion()
            pylab.plot([])
            pylab.ioff()
	
   
    return status

## end time

    if (status == 0):
	    message = 'KEPDYNAMIC completed at'
    else:
	    message = '\nKEPDYNAMIC aborted at'
    kepmsg.clock(message,logfile,verbose)
Esempio n. 37
0
def kepstddev(infile,outfile,datacol,timescale,clobber,verbose,logfile,status,cmdLine=False): 

# startup parameters

    status = 0
    labelsize = 44
    ticksize = 36
    xsize = 16
    ysize = 6
    lcolor = '#0000ff'
    lwidth = 1.0
    fcolor = '#ffff00'
    falpha = 0.2

# log the call

    hashline = '----------------------------------------------------------------------------'
    kepmsg.log(logfile,hashline,verbose)
    call = 'KEPSTDDEV -- '
    call += 'infile='+infile+' '
    call += 'outfile='+outfile+' '
    call += 'datacol='+str(datacol)+' '
    call += 'timescale='+str(timescale)+' '
    overwrite = 'n'
    if (clobber): overwrite = 'y'
    call += 'clobber='+overwrite+ ' '
    chatter = 'n'
    if (verbose): chatter = 'y'
    call += 'verbose='+chatter+' '
    call += 'logfile='+logfile
    kepmsg.log(logfile,call+'\n',verbose)

# start time

    kepmsg.clock('KEPSTDDEV started at',logfile,verbose)

# test log file

    logfile = kepmsg.test(logfile)

# clobber output file

    if clobber: status = kepio.clobber(outfile,logfile,verbose)
    if kepio.fileexists(outfile): 
        message = 'ERROR -- KEPSTDDEV: ' + outfile + ' exists. Use clobber=yes'
        status = kepmsg.err(logfile,message,verbose)

# open input file

    if status == 0:
        instr, status = kepio.openfits(infile,'readonly',logfile,verbose)
    if status == 0:
        tstart, tstop, bjdref, cadence, status = kepio.timekeys(instr,infile,logfile,verbose,status)
    if status == 0:
        try:
            work = instr[0].header['FILEVER']
            cadenom = 1.0
        except:
            cadenom = cadence

# fudge non-compliant FITS keywords with no values

    if status == 0:
        instr = kepkey.emptykeys(instr,file,logfile,verbose)

# read table structure

    if status == 0:
	table, status = kepio.readfitstab(infile,instr[1],logfile,verbose)

# filter input data table

    if status == 0:
        work1 = numpy.array([table.field('time'), table.field(datacol)])
        work1 = numpy.rot90(work1,3)
        work1 = work1[~numpy.isnan(work1).any(1)]            
 
# read table columns

    if status == 0:
        intime = work1[:,1] + bjdref
        indata = work1[:,0]

# calculate STDDEV in units of ppm

    if status == 0:
        stddev = running_frac_std(intime,indata,timescale/24) * 1.0e6
        astddev = numpy.std(indata) * 1.0e6
        cdpp = stddev / sqrt(timescale * 3600.0 / cadence)

# filter cdpp

    if status == 0:
        for i in range(len(cdpp)):
            if cdpp[i] > median(cdpp) * 10.0: cdpp[i] = cdpp[i-1]

# calculate median STDDEV

    if status == 0:
        medcdpp = ones((len(cdpp)),dtype='float32') * median(cdpp[:])
#        print '\nMedian %.1fhr standard deviation = %d ppm' % (timescale, median(stddev[:]))
        print '\nStandard deviation = %d ppm' % astddev

# calculate median STDDEV

    if status == 0:
        medcdpp = ones((len(cdpp)),dtype='float32') * median(cdpp[:])
        print 'Median %.1fhr CDPP = %d ppm' % (timescale, median(cdpp[:]))

# calculate RMS STDDEV

    if status == 0:
        rms, status = kepstat.rms(cdpp,zeros(len(stddev)),logfile,verbose)
        rmscdpp = ones((len(cdpp)),dtype='float32') * rms
        print '   RMS %.1fhr CDPP = %d ppm\n' % (timescale, rms)

# clean up x-axis unit

    if status == 0:
	intime0 = float(int(tstart / 100) * 100.0)
	ptime = intime - intime0
	xlab = 'BJD $-$ %d' % intime0

# clean up y-axis units

    if status == 0:
        pout = copy(cdpp)
        nrm = math.ceil(math.log10(median(cdpp))) - 1.0
#	pout = pout / 10**nrm
#	ylab = '%.1fhr $\sigma$ (10$^%d$ ppm)' % (timescale,nrm)
	ylab = '%.1fhr $\sigma$ (ppm)' % timescale

# data limits

	xmin = ptime.min()
	xmax = ptime.max()
	ymin = pout.min()
	ymax = pout.max()
	xr = xmax - xmin
	yr = ymax - ymin
        ptime = insert(ptime,[0],[ptime[0]]) 
        ptime = append(ptime,[ptime[-1]])
        pout = insert(pout,[0],[0.0]) 
        pout = append(pout,0.0)

# plot style

    if status == 0:
        try:
            params = {'backend': 'png',
                      'axes.linewidth': 2.5,
                      'axes.labelsize': 36,
                      'axes.font': 'sans-serif',
                      'axes.fontweight' : 'bold',
                      'text.fontsize': 12,
                      'legend.fontsize': 12,
                      'xtick.labelsize': 32,
                      'ytick.labelsize': 36}
            pylab.rcParams.update(params)
        except:
            pass

# define size of plot on monitor screen

	pylab.figure(figsize=[xsize,ysize])

# delete any fossil plots in the matplotlib window

        pylab.clf()

# position first axes inside the plotting window

        ax = pylab.axes([0.07,0.15,0.92,0.83])

# force tick labels to be absolute rather than relative

        pylab.gca().xaxis.set_major_formatter(pylab.ScalarFormatter(useOffset=False))
        pylab.gca().yaxis.set_major_formatter(pylab.ScalarFormatter(useOffset=False))
        ax.yaxis.set_major_locator(MaxNLocator(5))

# rotate y labels by 90 deg

        labels = ax.get_yticklabels()
        pylab.setp(labels, 'rotation', 90,fontsize=36)

# plot flux vs time

        ltime = array([],dtype='float64')
        ldata = array([],dtype='float32')
        dt = 0
        work1 = 2.0 * cadence / 86400
        for i in range(1,len(ptime)-1):
            dt = ptime[i] - ptime[i-1]
            if dt < work1:
                ltime = append(ltime,ptime[i])
                ldata = append(ldata,pout[i])
            else:
                pylab.plot(ltime,ldata,color='#0000ff',linestyle='-',linewidth=1.0)
                ltime = array([],dtype='float64')
                ldata = array([],dtype='float32')
        pylab.plot(ltime,ldata,color='#0000ff',linestyle='-',linewidth=1.0)

# plot the fill color below data time series, with no data gaps

	pylab.fill(ptime,pout,fc='#ffff00',linewidth=0.0,alpha=0.2)

# plot median CDPP

#        pylab.plot(intime - intime0,medcdpp / 10**nrm,color='r',linestyle='-',linewidth=2.0)
#        pylab.plot(intime - intime0,medcdpp,color='r',linestyle='-',linewidth=2.0)

# plot RMS CDPP

#        pylab.plot(intime - intime0,rmscdpp / 10**nrm,color='r',linestyle='--',linewidth=2.0)

# define plot x and y limits

	pylab.xlim(xmin - xr * 0.01, xmax + xr * 0.01)
	if ymin - yr * 0.01 <= 0.0:
            pylab.ylim(1.0e-10, ymax + yr * 0.01)
	else:
            pylab.ylim(ymin - yr * 0.01, ymax + yr * 0.01)
           
# plot labels

	pylab.xlabel(xlab, {'color' : 'k'})
        pylab.ylabel(ylab, {'color' : 'k'})

# make grid on plot

	pylab.grid()

# render plot

    if status == 0:
        if cmdLine: 
            pylab.show(block=True)
        else: 
            pylab.ion()
            pylab.plot([])
            pylab.ioff()

# add NaNs back into data

    if status == 0:
        n = 0
        work1 = array([],dtype='float32')
        instr, status = kepio.openfits(infile,'readonly',logfile,verbose)
	table, status = kepio.readfitstab(infile,instr[1],logfile,verbose)
        for i in range(len(table.field(0))):
            if isfinite(table.field('time')[i]) and isfinite(table.field(datacol)[i]):
                work1 = append(work1,cdpp[n])
                n += 1
            else:
                work1 = append(work1,nan)

# write output file
                
    if status == 0:
        status = kepkey.new('MCDPP%d' % (timescale * 10.0),medcdpp[0],
                            'Median %.1fhr CDPP (ppm)' % timescale,
                            instr[1],outfile,logfile,verbose)
        status = kepkey.new('RCDPP%d' % (timescale * 10.0),rmscdpp[0],
                            'RMS %.1fhr CDPP (ppm)' % timescale,
                            instr[1],outfile,logfile,verbose)
        colname = 'CDPP_%d' % (timescale * 10)
	col1 = pyfits.Column(name=colname,format='E13.7',array=work1)
	cols = instr[1].data.columns + col1
	instr[1] = pyfits.new_table(cols,header=instr[1].header)
	instr.writeto(outfile)
	
# comment keyword in output file

    if status == 0:
        status = kepkey.history(call,instr[0],outfile,logfile,verbose)

# close FITS

    if status == 0:
        status = kepio.closefits(instr,logfile,verbose)	    

# end time

    if (status == 0):
	    message = 'KEPSTDDEV completed at'
    else:
	    message = '\nKEPSTDDEV aborted at'
    kepmsg.clock(message,logfile,verbose)
Esempio n. 38
0
def kepstddev(infile,outfile,datacol,timescale,clobber,verbose,logfile,status,cmdLine=False): 

# startup parameters

    status = 0
    labelsize = 44
    ticksize = 36
    xsize = 16
    ysize = 6
    lcolor = '#0000ff'
    lwidth = 1.0
    fcolor = '#ffff00'
    falpha = 0.2

# log the call

    hashline = '----------------------------------------------------------------------------'
    kepmsg.log(logfile,hashline,verbose)
    call = 'KEPSTDDEV -- '
    call += 'infile='+infile+' '
    call += 'outfile='+outfile+' '
    call += 'datacol='+str(datacol)+' '
    call += 'timescale='+str(timescale)+' '
    overwrite = 'n'
    if (clobber): overwrite = 'y'
    call += 'clobber='+overwrite+ ' '
    chatter = 'n'
    if (verbose): chatter = 'y'
    call += 'verbose='+chatter+' '
    call += 'logfile='+logfile
    kepmsg.log(logfile,call+'\n',verbose)

# start time

    kepmsg.clock('KEPSTDDEV started at',logfile,verbose)

# test log file

    logfile = kepmsg.test(logfile)

# clobber output file

    if clobber: status = kepio.clobber(outfile,logfile,verbose)
    if kepio.fileexists(outfile): 
        message = 'ERROR -- KEPSTDDEV: ' + outfile + ' exists. Use clobber=yes'
        status = kepmsg.err(logfile,message,verbose)

# open input file

    if status == 0:
        instr, status = kepio.openfits(infile,'readonly',logfile,verbose)
    if status == 0:
        tstart, tstop, bjdref, cadence, status = kepio.timekeys(instr,infile,logfile,verbose,status)
    if status == 0:
        try:
            work = instr[0].header['FILEVER']
            cadenom = 1.0
        except:
            cadenom = cadence

# fudge non-compliant FITS keywords with no values

    if status == 0:
        instr = kepkey.emptykeys(instr,file,logfile,verbose)

# read table structure

    if status == 0:
	table, status = kepio.readfitstab(infile,instr[1],logfile,verbose)

# filter input data table

    if status == 0:
        work1 = numpy.array([table.field('time'), table.field(datacol)])
        work1 = numpy.rot90(work1,3)
        work1 = work1[~numpy.isnan(work1).any(1)]            
 
# read table columns

    if status == 0:
        intime = work1[:,1] + bjdref
        indata = work1[:,0]

# calculate STDDEV in units of ppm

    if status == 0:
        stddev = running_frac_std(intime,indata,timescale/24) * 1.0e6
        astddev = numpy.std(indata) * 1.0e6
        cdpp = stddev / sqrt(timescale * 3600.0 / cadence)

# filter cdpp

    if status == 0:
        for i in range(len(cdpp)):
            if cdpp[i] > median(cdpp) * 10.0: cdpp[i] = cdpp[i-1]

# calculate median STDDEV

    if status == 0:
        medcdpp = ones((len(cdpp)),dtype='float32') * median(cdpp[:])
#        print '\nMedian %.1fhr standard deviation = %d ppm' % (timescale, median(stddev[:]))
        print('\nStandard deviation = %d ppm' % astddev)

# calculate median STDDEV

    if status == 0:
        medcdpp = ones((len(cdpp)),dtype='float32') * median(cdpp[:])
        print('Median %.1fhr CDPP = %d ppm' % (timescale, median(cdpp[:])))

# calculate RMS STDDEV

    if status == 0:
        rms, status = kepstat.rms(cdpp,zeros(len(stddev)),logfile,verbose)
        rmscdpp = ones((len(cdpp)),dtype='float32') * rms
        print('   RMS %.1fhr CDPP = %d ppm\n' % (timescale, rms))

# clean up x-axis unit

    if status == 0:
	intime0 = float(int(tstart / 100) * 100.0)
	ptime = intime - intime0
	xlab = 'BJD $-$ %d' % intime0

# clean up y-axis units

    if status == 0:
        pout = copy(cdpp)
        nrm = math.ceil(math.log10(median(cdpp))) - 1.0
#	pout = pout / 10**nrm
#	ylab = '%.1fhr $\sigma$ (10$^%d$ ppm)' % (timescale,nrm)
	ylab = '%.1fhr $\sigma$ (ppm)' % timescale

# data limits

	xmin = ptime.min()
	xmax = ptime.max()
	ymin = pout.min()
	ymax = pout.max()
	xr = xmax - xmin
	yr = ymax - ymin
        ptime = insert(ptime,[0],[ptime[0]]) 
        ptime = append(ptime,[ptime[-1]])
        pout = insert(pout,[0],[0.0]) 
        pout = append(pout,0.0)

# plot style

    if status == 0:
        try:
            params = {'backend': 'png',
                      'axes.linewidth': 2.5,
                      'axes.labelsize': 36,
                      'axes.font': 'sans-serif',
                      'axes.fontweight' : 'bold',
                      'text.fontsize': 12,
                      'legend.fontsize': 12,
                      'xtick.labelsize': 32,
                      'ytick.labelsize': 36}
            pylab.rcParams.update(params)
        except:
            pass

# define size of plot on monitor screen

	pylab.figure(figsize=[xsize,ysize])

# delete any fossil plots in the matplotlib window

        pylab.clf()

# position first axes inside the plotting window

        ax = pylab.axes([0.07,0.15,0.92,0.83])

# force tick labels to be absolute rather than relative

        pylab.gca().xaxis.set_major_formatter(pylab.ScalarFormatter(useOffset=False))
        pylab.gca().yaxis.set_major_formatter(pylab.ScalarFormatter(useOffset=False))
        ax.yaxis.set_major_locator(MaxNLocator(5))

# rotate y labels by 90 deg

        labels = ax.get_yticklabels()
        pylab.setp(labels, 'rotation', 90,fontsize=36)

# plot flux vs time

        ltime = array([],dtype='float64')
        ldata = array([],dtype='float32')
        dt = 0
        work1 = 2.0 * cadence / 86400
        for i in range(1,len(ptime)-1):
            dt = ptime[i] - ptime[i-1]
            if dt < work1:
                ltime = append(ltime,ptime[i])
                ldata = append(ldata,pout[i])
            else:
                pylab.plot(ltime,ldata,color='#0000ff',linestyle='-',linewidth=1.0)
                ltime = array([],dtype='float64')
                ldata = array([],dtype='float32')
        pylab.plot(ltime,ldata,color='#0000ff',linestyle='-',linewidth=1.0)

# plot the fill color below data time series, with no data gaps

	pylab.fill(ptime,pout,fc='#ffff00',linewidth=0.0,alpha=0.2)

# plot median CDPP

#        pylab.plot(intime - intime0,medcdpp / 10**nrm,color='r',linestyle='-',linewidth=2.0)
#        pylab.plot(intime - intime0,medcdpp,color='r',linestyle='-',linewidth=2.0)

# plot RMS CDPP

#        pylab.plot(intime - intime0,rmscdpp / 10**nrm,color='r',linestyle='--',linewidth=2.0)

# define plot x and y limits

	pylab.xlim(xmin - xr * 0.01, xmax + xr * 0.01)
	if ymin - yr * 0.01 <= 0.0:
            pylab.ylim(1.0e-10, ymax + yr * 0.01)
	else:
            pylab.ylim(ymin - yr * 0.01, ymax + yr * 0.01)
           
# plot labels

	pylab.xlabel(xlab, {'color' : 'k'})
        pylab.ylabel(ylab, {'color' : 'k'})

# make grid on plot

	pylab.grid()

# render plot

    if status == 0:
        if cmdLine: 
            pylab.show(block=True)
        else: 
            pylab.ion()
            pylab.plot([])
            pylab.ioff()

# add NaNs back into data

    if status == 0:
        n = 0
        work1 = array([],dtype='float32')
        instr, status = kepio.openfits(infile,'readonly',logfile,verbose)
	table, status = kepio.readfitstab(infile,instr[1],logfile,verbose)
        for i in range(len(table.field(0))):
            if isfinite(table.field('time')[i]) and isfinite(table.field(datacol)[i]):
                work1 = append(work1,cdpp[n])
                n += 1
            else:
                work1 = append(work1,nan)

# write output file
                
    if status == 0:
        status = kepkey.new('MCDPP%d' % (timescale * 10.0),medcdpp[0],
                            'Median %.1fhr CDPP (ppm)' % timescale,
                            instr[1],outfile,logfile,verbose)
        status = kepkey.new('RCDPP%d' % (timescale * 10.0),rmscdpp[0],
                            'RMS %.1fhr CDPP (ppm)' % timescale,
                            instr[1],outfile,logfile,verbose)
        colname = 'CDPP_%d' % (timescale * 10)
	col1 = pyfits.Column(name=colname,format='E13.7',array=work1)
	cols = instr[1].data.columns + col1
	instr[1] = pyfits.new_table(cols,header=instr[1].header)
	instr.writeto(outfile)
	
# comment keyword in output file

    if status == 0:
        status = kepkey.history(call,instr[0],outfile,logfile,verbose)

# close FITS

    if status == 0:
        status = kepio.closefits(instr,logfile,verbose)	    

# end time

    if (status == 0):
	    message = 'KEPSTDDEV completed at'
    else:
	    message = '\nKEPSTDDEV aborted at'
    kepmsg.clock(message,logfile,verbose)
Esempio n. 39
0
def kepft(infile,
          outfile,
          fcol,
          pmin,
          pmax,
          nfreq,
          plot,
          clobber,
          verbose,
          logfile,
          status,
          cmdLine=False):

    ## startup parameters

    status = 0
    labelsize = 24
    ticksize = 16
    xsize = 18
    ysize = 6
    lcolor = '#0000ff'
    lwidth = 1.0
    fcolor = '#ffff00'
    falpha = 0.2

    ## log the call

    hashline = '----------------------------------------------------------------------------'
    kepmsg.log(logfile, hashline, verbose)
    call = 'KEPFT -- '
    call += 'infile=' + infile + ' '
    call += 'outfile=' + outfile + ' '
    call += 'fcol=' + fcol + ' '
    call += 'pmin=' + str(pmin) + ' '
    call += 'pmax=' + str(pmax) + ' '
    call += 'nfreq=' + str(nfreq) + ' '
    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('Start time is', 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 -- KEPFT: ' + 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)

## fudge non-compliant FITS keywords with no values

    if status == 0:
        instr = kepkey.emptykeys(instr, file, logfile, verbose)

## read table columns

    if status == 0:
        try:
            barytime = instr[1].data.field('barytime')
        except:
            barytime, status = kepio.readfitscol(infile, instr[1].data, 'time',
                                                 logfile, verbose)
        signal, status = kepio.readfitscol(infile, instr[1].data, fcol,
                                           logfile, verbose)
    if status == 0:
        barytime = barytime + bjdref

## remove infinite data from time series

    if status == 0:
        incols = [barytime, signal]
        outcols = kepstat.removeinfinlc(signal, incols)
        barytime = outcols[0]
        signal = outcols[1] - median(outcols[1])

## period to frequency conversion

    fmin = 1.0 / pmax
    fmax = 1.0 / pmin
    deltaf = (fmax - fmin) / nfreq

    ## loop through frequency steps; determine FT power

    if status == 0:
        fr, power = kepfourier.ft(barytime, signal, fmin, fmax, deltaf, True)

## write output file

    if status == 0:
        col1 = Column(name='FREQUENCY', format='E', unit='1/day', array=fr)
        col2 = Column(name='POWER', format='E', array=power)
        cols = ColDefs([col1, col2])
        instr.append(new_table(cols))
        instr[-1].header.update('EXTNAME', 'POWER SPECTRUM', 'extension name')
        instr.writeto(outfile)

## history keyword in output file

    if status == 0:
        status = kepkey.history(call, instr[0], outfile, logfile, verbose)

## close input file

    if status == 0:
        status = kepio.closefits(instr, logfile, verbose)

## data limits

    if status == 0:
        nrm = int(log10(power.max()))
        power = power / 10**nrm
        ylab = 'Power (x10$^{%d}$)' % nrm
        xmin = fr.min()
        xmax = fr.max()
        ymin = power.min()
        ymax = power.max()
        xr = xmax - xmin
        yr = ymax - ymin
        fr = insert(fr, [0], fr[0])
        fr = append(fr, fr[-1])
        power = insert(power, [0], 0.0)
        power = append(power, 0.0)

## plot power spectrum

    if status == 0 and plot:
        try:
            params = {
                'backend': 'png',
                'axes.linewidth': 2.5,
                'axes.labelsize': labelsize,
                'axes.font': 'sans-serif',
                'axes.fontweight': 'bold',
                'text.fontsize': 12,
                'legend.fontsize': 12,
                'xtick.labelsize': ticksize,
                'ytick.labelsize': ticksize
            }
            rcParams.update(params)
        except:
            print 'ERROR -- KEPFT: install latex for scientific plotting'
            status = 1

    if status == 0 and plot:
        pylab.figure(1, figsize=[xsize, ysize])
        pylab.clf()
        pylab.axes([0.06, 0.113, 0.93, 0.86])
        pylab.plot(fr, power, color=lcolor, linestyle='-', linewidth=lwidth)
        fill(fr, power, color=fcolor, linewidth=0.0, alpha=falpha)
        xlim(xmin - xr * 0.01, xmax + xr * 0.01)
        if ymin - yr * 0.01 <= 0.0:
            ylim(1.0e-10, ymax + yr * 0.01)
        else:
            ylim(ymin - yr * 0.01, ymax + yr * 0.01)
        xlabel(r'Frequency (d$^{-1}$)', {'color': 'k'})
        ylabel(ylab, {'color': 'k'})

        grid()

        # render plot

        if cmdLine:
            pylab.show()
        else:
            pylab.ion()
            pylab.plot([])
            pylab.ioff()

## end time

    if (status == 0):
        message = 'KEPFT completed at'
    else:
        message = '\nKEPFT aborted at'
    kepmsg.clock(message, logfile, verbose)
Esempio n. 40
0
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)
Esempio n. 41
0
def keptransit(inputfile,outputfile,datacol,errorcol,periodini_d,rprsini,T0ini,
    Eccini,arsini,incini,omegaini,LDparams,secini,fixperiod,fixrprs,fixT0,
    fixEcc,fixars,fixinc,fixomega,fixsec,fixfluxoffset,removeflaggeddata,ftol=0.0001,fitter='nothing',norm=False,
    clobber=False, plot=True,verbose=0,logfile='logfile.dat',status=0,cmdLine=False):
    """
    tmod.lightcurve(xdata,period,rprs,T0,Ecc,ars, incl, omega, ld, sec)

    input transit parameters are
    Period in days
    T0
    rplanet / rstar
    a / rstar
    inclination

    limb darkening code number:
    0 = uniform
    1 = linear
    2 = quadratic
    3 = square root
    4 = non linear

    LDarr:
    u      -- linear limb-darkening (set NL=1)
    a, b   -- quadratic limb-darkening (set NL=2)
    c,  d  -- root-square limb-darkening (set NL= -2)
    a1, a2, a3, a4 -- nonlinear limb-darkening  (set NL=4)
    Nothing at all -- uniform limb-darkening (set NL=0)
    """

    np.seterr(all="ignore") 

    #write to a logfile
    hashline = '----------------------------------------------------------------------------'
    kepmsg.log(logfile,hashline,verbose)
    call = 'KEPTRANSIT -- '
    call += 'inputfile='+inputfile+' '
    call += 'outputfile='+outputfile+' '
    call += 'datacol='+str(datacol)+' '
    call += 'errorcol='+str(errorcol)+' '
    call += 'periodini_d='+str(periodini_d)+' '
    call += 'rprsini='+str(rprsini)+' '
    call += 'T0ini='+str(T0ini)+' '
    call += 'Eccini='+str(Eccini)+' '
    call += 'arsini='+str(arsini)+' '
    call += 'incini='+str(incini)+' '
    call += 'omegaini='+str(omegaini)+' '
    call += 'LDparams='+str(LDparams)+' '
    call += 'secini='+str(secini)+' '
    call += 'fixperiod='+str(fixperiod)+' '
    call += 'fixrprs='+str(fixrprs)+' '
    call += 'fixT0='+str(fixT0)+' '
    call += 'fixEcc='+str(fixEcc)+' '
    call += 'fixars='+str(fixars)+' '
    call += 'fixinc='+str(fixinc)+' '
    call += 'fixomega='+str(fixomega)+' '
    call += 'fixsec='+str(fixsec)+' '
    call += 'fixfluxoffset='+str(fixfluxoffset)+' '
    call += 'removeflaggeddata='+str(removeflaggeddata)+' '
    call += 'ftol='+str(ftol)+' '
    call += 'fitter='+str(fitter)+' '
    call += 'norm='+str(norm)+' '

    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)


    kepmsg.clock('KEPTRANSIT started at',logfile,verbose)

    # test log file

    logfile = kepmsg.test(logfile)

# clobber output file

    if clobber: 
        status = kepio.clobber(outputfile,logfile,verbose)
    if kepio.fileexists(outputfile): 
        message = 'ERROR -- KEPTRANSIT: ' + outputfile + ' exists. Use clobber=yes'
        status = kepmsg.err(logfile,message,verbose)

# open input file

    if status == 0:
        instr, status = kepio.openfits(inputfile,'readonly',logfile,verbose)
    if status == 0:
        tstart, tstop, bjdref, cadence, status = kepio.timekeys(instr,
            inputfile,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(inputfile,instr[1],logfile,verbose)

    if status == 0:
        intime_o = table.field('time')
        influx_o = table.field(datacol)
        inerr_o = table.field(errorcol)
        try:
            qualflag = table.field('SAP_QUALITY')
        except:
            qualflag = np.zeros(len(intime_o))

    if status == 0:
        intime, indata, inerr, baddata = cutBadData(intime_o, influx_o, inerr_o,removeflaggeddata,qualflag)

    if status == 0 and norm:
        #first remove outliers before normalizing
        threesig = 3.* np.std(indata)
        mask = np.logical_and(indata< indata + threesig,indata > indata - threesig)
        #now normalize
        indata = indata / np.median(indata[mask])

    if status == 0:
        #need to check if LD params are sensible and in right format
        LDparams = [float(i) for i in LDparams.split()]

        incini = incini * np.pi / 180.

        omegaini = omegaini * np.pi / 180.

    if arsini*np.cos(incini) > 1.0 + rprsini:
        message = 'The guess inclination and a/r* values result in a non-transing planet'
        status = kepmsg.err(logfile,message,verbose)

    if status == 0:
        fixed_dict = fix_params(fixperiod,fixrprs,fixT0,
            fixEcc,fixars,fixinc,fixomega,fixsec,fixfluxoffset)

    #force flux offset to be guessed at zero
    fluxoffsetini = 0.0

    if status == 0:
        guess_params = [periodini_d,rprsini,T0ini,Eccini,arsini, incini, omegaini, 
        secini,fluxoffsetini]

        print('cleaning done: about to fit transit')

        if fitter == 'leastsq':
            fit_output = leastsq(fit_tmod,guess_params,
                args=[LDparams,intime,indata,inerr,fixed_dict,guess_params],
                full_output=True,ftol=ftol)
        elif fitter == 'fmin':

            fit_output = fmin(fit_tmod2,guess_params,
                args=[LDparams,intime,indata,inerr,fixed_dict,guess_params],
                full_output=True,ftol=ftol,xtol=ftol)

        elif fitter == 'anneal':
            fit_output = anneal(fit_tmod2,guess_params,
                args=[LDparams,intime,indata,inerr,fixed_dict,guess_params],
                full_output=True)

    if status == 0:
        if fixed_dict['period'] == True:
            newperiod = guess_params[0]
            print('Fixed period (days) = ' + str(newperiod))
        else:
            newperiod = fit_output[0][0]
            print('Fit period (days) = ' + str(newperiod))
        if fixed_dict['rprs'] == True:
            newrprs = guess_params[1]
            print('Fixed R_planet / R_star = ' + str(newrprs))
        else:
            newrprs = fit_output[0][1]
            print('Fit R_planet / R_star = ' + str(newrprs))
        if fixed_dict['T0'] == True:
            newT0 = guess_params[2]
            print('Fixed T0 (BJD) = ' + str(newT0))
        else:
            newT0 = fit_output[0][2]
            print('Fit T0 (BJD) = ' + str(newT0))
        if fixed_dict['Ecc'] == True:
            newEcc = guess_params[3]
            print('Fixed eccentricity = ' + str(newEcc))
        else:
            newEcc = fit_output[0][3]
            print('Fit eccentricity = ' + str(newEcc))
        if fixed_dict['ars'] == True:
            newars = guess_params[4]
            print('Fixed a / R_star = ' + str(newars))
        else:
            newars = fit_output[0][4]
            print('Fit a / R_star = ' + str(newars))
        if fixed_dict['inc'] == True:
            newinc = guess_params[5]
            print('Fixed inclination (deg) = ' + str(newinc* 180. / np.pi))
        else:
            newinc = fit_output[0][5]
            print('Fit inclination (deg) = ' + str(newinc* 180. / np.pi))
        if fixed_dict['omega'] == True:
            newomega = guess_params[6]
            print('Fixed omega = ' + str(newomega))
        else:
            newomega = fit_output[0][6]
            print('Fit omega = ' + str(newomega))
        if fixed_dict['sec'] == True:
            newsec = guess_params[7]
            print('Fixed seconary eclipse depth = ' + str(newsec))
        else:
            newsec = fit_output[0][7]
            print('Fit seconary eclipse depth = ' + str(newsec))
        if fixfluxoffset == False:
            newfluxoffset = fit_output[0][8]
            print('Fit flux offset = ' + str(newfluxoffset))

    

        modelfit = tmod.lightcurve(intime,newperiod,newrprs,newT0,newEcc,
            newars,newinc,newomega,LDparams,newsec)

        if fixfluxoffset == False:
            modelfit += newfluxoffset


        #output to a file
        phi, fluxfold, modelfold, errorfold, phiNotFold = fold_data(intime, 
            modelfit,indata,inerr,newperiod,newT0)


        make_outfile(instr,outputfile,phiNotFold,modelfit, baddata)


    # end time

    if (status == 0):
        message = 'KEPTRANSIT completed at'
    else:
        message = '\nKEPTRANSIT aborted at'
    kepmsg.clock(message,logfile,verbose)

    if plot and status == 0:
        do_plot(intime,modelfit,indata,inerr,newperiod,newT0,cmdLine)
Esempio n. 42
0
def keppca(infile,
           maskfile,
           outfile,
           components,
           plotpca,
           nreps,
           clobber,
           verbose,
           logfile,
           status,
           cmdLine=False):

    try:
        import mdp
    except:
        msg = 'ERROR -- KEPPCA: this task has an external python dependency to MDP, a Modular toolkit for Data Processing (http://mdp-toolkit.sourceforge.net). In order to take advantage of this PCA task, the user must first install MDP with their current python distribution. Note carefully that you may have more than python installation on your machine, and ensure that MDP is installed with the same version of python that the PyKE tools employ. Installation instructions for MDP can be found at the URL provided above.'
        status = kepmsg.err(None, msg, True)

# startup parameters

    status = 0
    labelsize = 32
    ticksize = 18
    xsize = 16
    ysize = 10
    lcolor = '#0000ff'
    lwidth = 1.0
    fcolor = '#ffff00'
    falpha = 0.2
    seterr(all="ignore")

    # log the call

    if status == 0:
        hashline = '----------------------------------------------------------------------------'
        kepmsg.log(logfile, hashline, verbose)
        call = 'KEPPCA -- '
        call += 'infile=' + infile + ' '
        call += 'maskfile=' + maskfile + ' '
        call += 'outfile=' + outfile + ' '
        call += 'components=' + components + ' '
        ppca = 'n'
        if (plotpca): ppca = 'y'
        call += 'plotpca=' + ppca + ' '
        call += 'nmaps=' + str(nreps) + ' '
        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

    if status == 0:
        kepmsg.clock('KEPPCA started at', logfile, verbose)

# test log file

    if status == 0:
        logfile = kepmsg.test(logfile)

# clobber output file

    if status == 0:
        if clobber: status = kepio.clobber(outfile, logfile, verbose)
        if kepio.fileexists(outfile):
            message = 'ERROR -- KEPPCA: ' + outfile + ' exists. Use clobber=yes'
            status = kepmsg.err(logfile, message, verbose)

# Set output file names - text file with data and plot

    if status == 0:
        dataout = copy(outfile)
        repname = re.sub('.fits', '.png', outfile)

# open input file

    if status == 0:
        instr = pyfits.open(infile, mode='readonly', memmap=True)
        tstart, tstop, bjdref, cadence, status = kepio.timekeys(
            instr, infile, logfile, verbose, status)

# 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, flux_bkg, status = \
            kepio.readTPF(infile,'FLUX_BKG',logfile,verbose)
    if status == 0:
        kepid, channel, skygroup, module, output, quarter, season, \
            ra, dec, column, row, kepmag, xdim, ydim, flux_bkg_err, status = \
            kepio.readTPF(infile,'FLUX_BKG_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)
    if status == 0:
        kepid, channel, skygroup, module, output, quarter, season, \
            ra, dec, column, row, kepmag, xdim, ydim, pcorr1, status = \
            kepio.readTPF(infile,'POS_CORR1',logfile,verbose)
    if status == 0:
        kepid, channel, skygroup, module, output, quarter, season, \
            ra, dec, column, row, kepmag, xdim, ydim, pcorr2, status = \
            kepio.readTPF(infile,'POS_CORR2',logfile,verbose)

# Save original data dimensions, in case of using maskfile

    if status == 0:
        xdimorig = xdim
        ydimorig = ydim

# read mask definition file if it has been supplied

    if status == 0 and 'aper' not in maskfile.lower(
    ) and maskfile.lower() != 'all':
        maskx = array([], 'int')
        masky = array([], 'int')
        lines, status = kepio.openascii(maskfile, 'r', logfile, verbose)
        for line in lines:
            line = line.strip().split('|')
            if len(line) == 6:
                y0 = int(line[3])
                x0 = int(line[4])
                line = line[5].split(';')
                for items in line:
                    try:
                        masky = numpy.append(masky,
                                             y0 + int(items.split(',')[0]))
                        maskx = numpy.append(maskx,
                                             x0 + int(items.split(',')[1]))
                    except:
                        continue
        status = kepio.closeascii(lines, logfile, verbose)
        if len(maskx) == 0 or len(masky) == 0:
            message = 'ERROR -- KEPPCA: ' + maskfile + ' contains no pixels.'
            status = kepmsg.err(logfile, message, verbose)
        xdim = max(maskx) - min(maskx) + 1  # Find largest x dimension of mask
        ydim = max(masky) - min(masky) + 1  # Find largest y dimension of mask

        # pad mask to ensure it is rectangular

        workx = array([], 'int')
        worky = array([], 'int')
        for ip in arange(min(maskx), max(maskx) + 1):
            for jp in arange(min(masky), max(masky) + 1):
                workx = append(workx, ip)
                worky = append(worky, jp)
        maskx = workx
        masky = worky

# define new subimage bitmap...

    if status == 0 and maskfile.lower() != 'all':
        aperx = numpy.array([], 'int')
        apery = numpy.array([], 'int')
        aperb = maskx - x0 + xdimorig * (
            masky - y0
        )  # aperb is an array that contains the pixel numbers in the mask
        npix = len(aperb)

# ...or use all pixels

    if status == 0 and maskfile.lower() == 'all':
        npix = xdimorig * ydimorig
        aperb = array([], 'int')
        aperb = numpy.r_[0:npix]

# legal mask defined?

    if status == 0:
        if len(aperb) == 0:
            message = 'ERROR -- KEPPCA: no legal pixels within the subimage are defined.'
            status = kepmsg.err(logfile, message, verbose)

# Identify principal components desired

    if status == 0:
        pcaout = []
        txt = components.strip().split(',')
        for work1 in txt:
            try:
                pcaout.append(int(work1.strip()))
            except:
                work2 = work1.strip().split('-')
                try:
                    for work3 in range(int(work2[0]), int(work2[1]) + 1):
                        pcaout.append(work3)
                except:
                    message = 'ERROR -- KEPPCA: cannot understand principal component list requested'
                    status = kepmsg.err(logfile, message, verbose)
    if status == 0:
        pcaout = set(sort(pcaout))
    pcarem = array(
        list(pcaout)) - 1  # The list of pca component numbers to be removed

    # Initialize arrays and variables, and apply pixel mask to the data

    if status == 0:
        ntim = 0
        time = numpy.array([], dtype='float64')
        timecorr = numpy.array([], dtype='float32')
        cadenceno = numpy.array([], dtype='int')
        pixseries = numpy.array([], dtype='float32')
        errseries = numpy.array([], dtype='float32')
        bkgseries = numpy.array([], dtype='float32')
        berseries = numpy.array([], dtype='float32')
        quality = numpy.array([], dtype='float32')
        pos_corr1 = numpy.array([], dtype='float32')
        pos_corr2 = numpy.array([], dtype='float32')
        nrows = numpy.size(fluxpixels, 0)

# Apply the pixel mask so we are left with only the desired pixels

    if status == 0:
        pixseriesb = fluxpixels[:, aperb]
        errseriesb = errpixels[:, aperb]
        bkgseriesb = flux_bkg[:, aperb]
        berseriesb = flux_bkg_err[:, aperb]

# Read in the data to various arrays

    if status == 0:
        for i in range(nrows):
            if qual[i] < 10000 and \
                    numpy.isfinite(barytime[i]) and \
                    numpy.isfinite(fluxpixels[i,int(ydim*xdim/2+0.5)]) and \
                    numpy.isfinite(fluxpixels[i,1+int(ydim*xdim/2+0.5)]):
                ntim += 1
                time = numpy.append(time, barytime[i])
                timecorr = numpy.append(timecorr, tcorr[i])
                cadenceno = numpy.append(cadenceno, cadno[i])
                pixseries = numpy.append(pixseries, pixseriesb[i])
                errseries = numpy.append(errseries, errseriesb[i])
                bkgseries = numpy.append(bkgseries, bkgseriesb[i])
                berseries = numpy.append(berseries, berseriesb[i])
                quality = numpy.append(quality, qual[i])
                pos_corr1 = numpy.append(pos_corr1, pcorr1[i])
                pos_corr2 = numpy.append(pos_corr2, pcorr2[i])
        pixseries = numpy.reshape(pixseries, (ntim, npix))
        errseries = numpy.reshape(errseries, (ntim, npix))
        bkgseries = numpy.reshape(bkgseries, (ntim, npix))
        berseries = numpy.reshape(berseries, (ntim, npix))
        tmp = numpy.median(pixseries, axis=1)
        for i in range(len(tmp)):
            pixseries[i] = pixseries[i] - tmp[i]

# Figure out which pixels are undefined/nan and remove them. Keep track for adding back in later

    if status == 0:
        nanpixels = numpy.array([], dtype='int')
        i = 0
        while (i < npix):
            if numpy.isnan(pixseries[0, i]):
                nanpixels = numpy.append(nanpixels, i)
                npix = npix - 1
            i = i + 1
        pixseries = numpy.delete(pixseries, nanpixels, 1)
        errseries = numpy.delete(errseries, nanpixels, 1)
        pixseries[numpy.isnan(pixseries)] = random.gauss(100, 10)
        errseries[numpy.isnan(errseries)] = 10

# Compute statistical weights, means, standard deviations

    if status == 0:
        weightseries = (pixseries / errseries)**2
        pixMean = numpy.average(pixseries, axis=0, weights=weightseries)
        pixStd = numpy.std(pixseries, axis=0)

# Normalize the input by subtracting the mean and divising by the standard deviation.
# This makes it a correlation-based PCA, which is what we want.

    if status == 0:
        pixseriesnorm = (pixseries - pixMean) / pixStd

# Number of principal components to compute. Setting it equal to the number of pixels

    if status == 0:
        nvecin = npix

# Run PCA using the MDP Whitening PCA, which produces normalized PCA components (zero mean and unit variance)

    if status == 0:
        pcan = mdp.nodes.WhiteningNode(svd=True)
        pcar = pcan.execute(pixseriesnorm)
        eigvec = pcan.get_recmatrix()
        model = pcar

# Re-insert nan columns as zeros

    if status == 0:
        for i in range(0, len(nanpixels)):
            nanpixels[i] = nanpixels[i] - i
        eigvec = numpy.insert(eigvec, nanpixels, 0, 1)
        pixMean = numpy.insert(pixMean, nanpixels, 0, 0)

#  Make output eigenvectors (correlation images) into xpix by ypix images

    if status == 0:
        eigvec = eigvec.reshape(nvecin, ydim, xdim)

# Calculate sum of all pixels to display as raw lightcurve and other quantities

    if status == 0:
        pixseriessum = sum(pixseries, axis=1)
        nrem = len(pcarem)  # Number of components to remove
        nplot = npix  # Number of pcas to plot - currently set to plot all components, but could set
        # nplot = nrem to just plot as many components as is being removed

# Subtract components by fitting them to the summed light curve

    if status == 0:
        x0 = numpy.tile(-1.0, 1)
        for k in range(0, nrem):

            def f(x):
                fluxcor = pixseriessum
                for k in range(0, len(x)):
                    fluxcor = fluxcor - x[k] * model[:, pcarem[k]]
                return mad(fluxcor)

            if k == 0:
                x0 = array([-1.0])
            else:
                x0 = numpy.append(x0, 1.0)
            myfit = scipy.optimize.fmin(f,
                                        x0,
                                        maxiter=50000,
                                        maxfun=50000,
                                        disp=False)
            x0 = myfit

# Now that coefficients for all components have been found, subtract them to produce a calibrated time-series,
# and then divide by the robust mean to produce a normalized time series as well

    if status == 0:
        c = myfit
        fluxcor = pixseriessum
        for k in range(0, nrem):
            fluxcor = fluxcor - c[k] * model[:, pcarem[k]]
            normfluxcor = fluxcor / mean(reject_outliers(fluxcor, 2))

# input file data

    if status == 0:
        cards0 = instr[0].header.cards
        cards1 = instr[1].header.cards
        cards2 = instr[2].header.cards
        table = instr[1].data[:]
        maskmap = copy(instr[2].data)

# subimage physical WCS data

    if status == 0:
        crpix1p = cards2['CRPIX1P'].value
        crpix2p = cards2['CRPIX2P'].value
        crval1p = cards2['CRVAL1P'].value
        crval2p = cards2['CRVAL2P'].value
        cdelt1p = cards2['CDELT1P'].value
        cdelt2p = cards2['CDELT2P'].value

# dummy columns for output file

    if status == 0:
        sap_flux_err = numpy.empty(len(time))
        sap_flux_err[:] = numpy.nan
        sap_bkg = numpy.empty(len(time))
        sap_bkg[:] = numpy.nan
        sap_bkg_err = numpy.empty(len(time))
        sap_bkg_err[:] = numpy.nan
        pdc_flux = numpy.empty(len(time))
        pdc_flux[:] = numpy.nan
        pdc_flux_err = numpy.empty(len(time))
        pdc_flux_err[:] = numpy.nan
        psf_centr1 = numpy.empty(len(time))
        psf_centr1[:] = numpy.nan
        psf_centr1_err = numpy.empty(len(time))
        psf_centr1_err[:] = numpy.nan
        psf_centr2 = numpy.empty(len(time))
        psf_centr2[:] = numpy.nan
        psf_centr2_err = numpy.empty(len(time))
        psf_centr2_err[:] = numpy.nan
        mom_centr1 = numpy.empty(len(time))
        mom_centr1[:] = numpy.nan
        mom_centr1_err = numpy.empty(len(time))
        mom_centr1_err[:] = numpy.nan
        mom_centr2 = numpy.empty(len(time))
        mom_centr2[:] = numpy.nan
        mom_centr2_err = numpy.empty(len(time))
        mom_centr2_err[:] = numpy.nan

# mask bitmap

    if status == 0 and 'aper' not in maskfile.lower(
    ) and maskfile.lower() != 'all':
        for i in range(maskmap.shape[0]):
            for j in range(maskmap.shape[1]):
                aperx = append(aperx, crval1p + (j + 1 - crpix1p) * cdelt1p)
                apery = append(apery, crval2p + (i + 1 - crpix2p) * cdelt2p)
                if maskmap[i, j] == 0:
                    pass
                else:
                    maskmap[i, j] = 1
                    for k in range(len(maskx)):
                        if aperx[-1] == maskx[k] and apery[-1] == masky[k]:
                            maskmap[i, j] = 3

# construct output primary extension

    if status == 0:
        hdu0 = pyfits.PrimaryHDU()
        for i in range(len(cards0)):
            if cards0[i].keyword not in list(hdu0.header.keys()):
                hdu0.header[cards0[i].keyword] = (cards0[i].value,
                                                  cards0[i].comment)
            else:
                hdu0.header.cards[
                    cards0[i].keyword].comment = cards0[i].comment
        status = kepkey.history(call, hdu0, outfile, logfile, verbose)
        outstr = HDUList(hdu0)

# construct output light curve extension

    if status == 0:
        col1 = Column(name='TIME',
                      format='D',
                      unit='BJD - 2454833',
                      array=time)
        col2 = Column(name='TIMECORR', format='E', unit='d', array=timecorr)
        col3 = Column(name='CADENCENO', format='J', array=cadenceno)
        col4 = Column(name='SAP_FLUX',
                      format='E',
                      unit='e-/s',
                      array=pixseriessum)
        col5 = Column(name='SAP_FLUX_ERR',
                      format='E',
                      unit='e-/s',
                      array=sap_flux_err)
        col6 = Column(name='SAP_BKG', format='E', unit='e-/s', array=sap_bkg)
        col7 = Column(name='SAP_BKG_ERR',
                      format='E',
                      unit='e-/s',
                      array=sap_bkg_err)
        col8 = Column(name='PDCSAP_FLUX',
                      format='E',
                      unit='e-/s',
                      array=pdc_flux)
        col9 = Column(name='PDCSAP_FLUX_ERR',
                      format='E',
                      unit='e-/s',
                      array=pdc_flux_err)
        col10 = Column(name='SAP_QUALITY', format='J', array=quality)
        col11 = Column(name='PSF_CENTR1',
                       format='E',
                       unit='pixel',
                       array=psf_centr1)
        col12 = Column(name='PSF_CENTR1_ERR',
                       format='E',
                       unit='pixel',
                       array=psf_centr1_err)
        col13 = Column(name='PSF_CENTR2',
                       format='E',
                       unit='pixel',
                       array=psf_centr2)
        col14 = Column(name='PSF_CENTR2_ERR',
                       format='E',
                       unit='pixel',
                       array=psf_centr2_err)
        col15 = Column(name='MOM_CENTR1',
                       format='E',
                       unit='pixel',
                       array=mom_centr1)
        col16 = Column(name='MOM_CENTR1_ERR',
                       format='E',
                       unit='pixel',
                       array=mom_centr1_err)
        col17 = Column(name='MOM_CENTR2',
                       format='E',
                       unit='pixel',
                       array=mom_centr2)
        col18 = Column(name='MOM_CENTR2_ERR',
                       format='E',
                       unit='pixel',
                       array=mom_centr2_err)
        col19 = Column(name='POS_CORR1',
                       format='E',
                       unit='pixel',
                       array=pos_corr1)
        col20 = Column(name='POS_CORR2',
                       format='E',
                       unit='pixel',
                       array=pos_corr2)
        col21 = Column(name='PCA_FLUX', format='E', unit='e-/s', array=fluxcor)
        col22 = Column(name='PCA_FLUX_NRM', format='E', array=normfluxcor)
        cols = ColDefs([col1,col2,col3,col4,col5,col6,col7,col8,col9,col10,col11, \
                            col12,col13,col14,col15,col16,col17,col18,col19,col20,col21,col22])
        hdu1 = new_table(cols)
        hdu1.header['TTYPE1'] = ('TIME', 'column title: data time stamps')
        hdu1.header['TFORM1'] = ('D', 'data type: float64')
        hdu1.header['TUNIT1'] = ('BJD - 2454833',
                                 'column units: barycenter corrected JD')
        hdu1.header['TDISP1'] = ('D12.7', 'column display format')
        hdu1.header['TTYPE2'] = (
            'TIMECORR', 'column title: barycentric-timeslice correction')
        hdu1.header['TFORM2'] = ('E', 'data type: float32')
        hdu1.header['TUNIT2'] = ('d', 'column units: days')
        hdu1.header['TTYPE3'] = ('CADENCENO',
                                 'column title: unique cadence number')
        hdu1.header['TFORM3'] = ('J', 'column format: signed integer32')
        hdu1.header['TTYPE4'] = ('SAP_FLUX',
                                 'column title: aperture photometry flux')
        hdu1.header['TFORM4'] = ('E', 'column format: float32')
        hdu1.header['TUNIT4'] = ('e-/s', 'column units: electrons per second')
        hdu1.header['TTYPE5'] = ('SAP_FLUX_ERR',
                                 'column title: aperture phot. flux error')
        hdu1.header['TFORM5'] = ('E', 'column format: float32')
        hdu1.header['TUNIT5'] = (
            'e-/s', 'column units: electrons per second (1-sigma)')
        hdu1.header['TTYPE6'] = (
            'SAP_BKG', 'column title: aperture phot. background flux')
        hdu1.header['TFORM6'] = ('E', 'column format: float32')
        hdu1.header['TUNIT6'] = ('e-/s', 'column units: electrons per second')
        hdu1.header['TTYPE7'] = (
            'SAP_BKG_ERR', 'column title: ap. phot. background flux error')
        hdu1.header['TFORM7'] = ('E', 'column format: float32')
        hdu1.header['TUNIT7'] = (
            'e-/s', 'column units: electrons per second (1-sigma)')
        hdu1.header['TTYPE8'] = ('PDCSAP_FLUX',
                                 'column title: PDC photometry flux')
        hdu1.header['TFORM8'] = ('E', 'column format: float32')
        hdu1.header['TUNIT8'] = ('e-/s', 'column units: electrons per second')
        hdu1.header['TTYPE9'] = ('PDCSAP_FLUX_ERR',
                                 'column title: PDC flux error')
        hdu1.header['TFORM9'] = ('E', 'column format: float32')
        hdu1.header['TUNIT9'] = (
            'e-/s', 'column units: electrons per second (1-sigma)')
        hdu1.header['TTYPE10'] = (
            'SAP_QUALITY', 'column title: aperture photometry quality flag')
        hdu1.header['TFORM10'] = ('J', 'column format: signed integer32')
        hdu1.header['TTYPE11'] = ('PSF_CENTR1',
                                  'column title: PSF fitted column centroid')
        hdu1.header['TFORM11'] = ('E', 'column format: float32')
        hdu1.header['TUNIT11'] = ('pixel', 'column units: pixel')
        hdu1.header['TTYPE12'] = ('PSF_CENTR1_ERR',
                                  'column title: PSF fitted column error')
        hdu1.header['TFORM12'] = ('E', 'column format: float32')
        hdu1.header['TUNIT12'] = ('pixel', 'column units: pixel')
        hdu1.header['TTYPE13'] = ('PSF_CENTR2',
                                  'column title: PSF fitted row centroid')
        hdu1.header['TFORM13'] = ('E', 'column format: float32')
        hdu1.header['TUNIT13'] = ('pixel', 'column units: pixel')
        hdu1.header['TTYPE14'] = ('PSF_CENTR2_ERR',
                                  'column title: PSF fitted row error')
        hdu1.header['TFORM14'] = ('E', 'column format: float32')
        hdu1.header['TUNIT14'] = ('pixel', 'column units: pixel')
        hdu1.header['TTYPE15'] = (
            'MOM_CENTR1', 'column title: moment-derived column centroid')
        hdu1.header['TFORM15'] = ('E', 'column format: float32')
        hdu1.header['TUNIT15'] = ('pixel', 'column units: pixel')
        hdu1.header['TTYPE16'] = ('MOM_CENTR1_ERR',
                                  'column title: moment-derived column error')
        hdu1.header['TFORM16'] = ('E', 'column format: float32')
        hdu1.header['TUNIT16'] = ('pixel', 'column units: pixel')
        hdu1.header['TTYPE17'] = ('MOM_CENTR2',
                                  'column title: moment-derived row centroid')
        hdu1.header['TFORM17'] = ('E', 'column format: float32')
        hdu1.header['TUNIT17'] = ('pixel', 'column units: pixel')
        hdu1.header['TTYPE18'] = ('MOM_CENTR2_ERR',
                                  'column title: moment-derived row error')
        hdu1.header['TFORM18'] = ('E', 'column format: float32')
        hdu1.header['TUNIT18'] = ('pixel', 'column units: pixel')
        hdu1.header['TTYPE19'] = (
            'POS_CORR1', 'column title: col correction for vel. abbern')
        hdu1.header['TFORM19'] = ('E', 'column format: float32')
        hdu1.header['TUNIT19'] = ('pixel', 'column units: pixel')
        hdu1.header['TTYPE20'] = (
            'POS_CORR2', 'column title: row correction for vel. abbern')
        hdu1.header['TFORM20'] = ('E', 'column format: float32')
        hdu1.header['TUNIT20'] = ('pixel', 'column units: pixel')
        hdu1.header['TTYPE21'] = ('PCA_FLUX',
                                  'column title: PCA-corrected flux')
        hdu1.header['TFORM21'] = ('E', 'column format: float32')
        hdu1.header['TUNIT21'] = ('pixel', 'column units: e-/s')
        hdu1.header['TTYPE22'] = (
            'PCA_FLUX_NRM', 'column title: normalized PCA-corrected flux')
        hdu1.header['TFORM22'] = ('E', 'column format: float32')
        hdu1.header['EXTNAME'] = ('LIGHTCURVE', 'name of extension')
        for i in range(len(cards1)):
            if (cards1[i].keyword not in list(hdu1.header.keys())
                    and cards1[i].keyword[: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'
                    ]):
                hdu1.header[cards1[i].keyword] = (cards1[i].value,
                                                  cards1[i].comment)
        outstr.append(hdu1)

# construct output mask bitmap extension

    if status == 0:
        hdu2 = ImageHDU(maskmap)
        for i in range(len(cards2)):
            if cards2[i].keyword not in list(hdu2.header.keys()):
                hdu2.header[cards2[i].keyword] = (cards2[i].value,
                                                  cards2[i].comment)
            else:
                hdu2.header.cards[
                    cards2[i].keyword].comment = cards2[i].comment
        outstr.append(hdu2)

# construct principal component table

    if status == 0:
        cols = [
            Column(name='TIME', format='E', unit='BJD - 2454833', array=time)
        ]
        for i in range(len(pcar[0, :])):
            colname = 'PC' + str(i + 1)
            col = Column(name=colname, format='E', array=pcar[:, i])
            cols.append(col)
        hdu3 = new_table(ColDefs(cols))
        hdu3.header['EXTNAME'] = ('PRINCIPAL_COMPONENTS', 'name of extension')
        hdu3.header['TTYPE1'] = ('TIME', 'column title: data time stamps')
        hdu3.header['TFORM1'] = ('D', 'data type: float64')
        hdu3.header['TUNIT1'] = ('BJD - 2454833',
                                 'column units: barycenter corrected JD')
        hdu3.header['TDISP1'] = ('D12.7', 'column display format')
        for i in range(len(pcar[0, :])):
            hdu3.header['TTYPE' + str(i + 2)] = \
                ('PC' + str(i + 1), 'column title: principal component number' + str(i + 1))
            hdu3.header['TFORM' + str(i + 2)] = ('E', 'column format: float32')
        outstr.append(hdu3)

# write output file

    if status == 0:
        outstr.writeto(outfile)

# close input structure

    if status == 0:
        status = kepio.closefits(instr, logfile, verbose)

# Create PCA report

    if status == 0 and plotpca:
        npp = 7  # Number of plots per page
        l = 1
        repcnt = 1
        for k in range(nreps):

            # First plot of every pagewith flux image, flux and calibrated time series

            status = kepplot.define(16, 12, logfile, verbose)
            if (k % (npp - 1) == 0):
                pylab.figure(figsize=[10, 16])
                subplot2grid((npp, 6), (0, 0), colspan=2)
                #                imshow(log10(pixMean.reshape(xdim,ydim).T-min(pixMean)+1),interpolation="nearest",cmap='RdYlBu')
                imshow(log10(
                    flipud(pixMean.reshape(ydim, xdim)) - min(pixMean) + 1),
                       interpolation="nearest",
                       cmap='RdYlBu')
                xticks([])
                yticks([])
                ax1 = subplot2grid((npp, 6), (0, 2), colspan=4)
                px = copy(time) + bjdref
                py = copy(pixseriessum)
                px, xlab, status = kepplot.cleanx(px, logfile, verbose)
                py, ylab, status = kepplot.cleany(py, 1.0, logfile, verbose)
                kepplot.RangeOfPlot(px, py, 0.01, False)
                kepplot.plot1d(px, py, cadence, lcolor, lwidth, fcolor, falpha,
                               True)
                py = copy(fluxcor)
                py, ylab, status = kepplot.cleany(py, 1.0, logfile, verbose)
                plot(px,
                     py,
                     marker='.',
                     color='r',
                     linestyle='',
                     markersize=1.0)
                kepplot.labels('', re.sub('\)', '',
                                          re.sub('Flux \(', '', ylab)), 'k',
                               18)
                grid()
                setp(ax1.get_xticklabels(), visible=False)

# plot principal components

            subplot2grid((npp, 6), (l, 0), colspan=2)
            imshow(eigvec[k], interpolation="nearest", cmap='RdYlBu')
            xlim(-0.5, xdim - 0.5)
            ylim(-0.5, ydim - 0.5)
            xticks([])
            yticks([])

            # The last plot on the page that should have the xlabel

            if (k % (npp - 1) == npp - 2 or k == nvecin - 1):
                subplot2grid((npp, 6), (l, 2), colspan=4)
                py = copy(model[:, k])
                kepplot.RangeOfPlot(px, py, 0.01, False)
                kepplot.plot1d(px, py, cadence, 'r', lwidth, 'g', falpha, True)
                kepplot.labels(xlab, 'PC ' + str(k + 1), 'k', 18)
                pylab.grid()
                pylab.tight_layout()
                l = 1
                pylab.savefig(re.sub('.png', '_%d.png' % repcnt, repname))
                if not cmdLine: kepplot.render(cmdLine)
                repcnt += 1

# The other plots on the page that should have no xlabel

            else:
                ax2 = subplot2grid((npp, 6), (l, 2), colspan=4)
                py = copy(model[:, k])
                kepplot.RangeOfPlot(px, py, 0.01, False)
                kepplot.plot1d(px, py, cadence, 'r', lwidth, 'g', falpha, True)
                kepplot.labels('', 'PC ' + str(k + 1), 'k', 18)
                grid()
                setp(ax2.get_xticklabels(), visible=False)
                pylab.tight_layout()
                l = l + 1
        pylab.savefig(re.sub('.png', '_%d.png' % repcnt, repname))
        if not cmdLine: kepplot.render(cmdLine)

# plot style and size

    if status == 0 and plotpca:
        status = kepplot.define(labelsize, ticksize, logfile, verbose)
        pylab.figure(figsize=[xsize, ysize])
        pylab.clf()

# plot aperture photometry and PCA corrected data

    if status == 0 and plotpca:
        ax = kepplot.location([0.06, 0.54, 0.93, 0.43])
        px = copy(time) + bjdref
        py = copy(pixseriessum)
        px, xlab, status = kepplot.cleanx(px, logfile, verbose)
        py, ylab, status = kepplot.cleany(py, 1.0, logfile, verbose)
        kepplot.RangeOfPlot(px, py, 0.01, False)
        kepplot.plot1d(px, py, cadence, lcolor, lwidth, fcolor, falpha, True)
        py = copy(fluxcor)
        py, ylab, status = kepplot.cleany(py, 1.0, logfile, verbose)
        kepplot.plot1d(px, py, cadence, 'r', 2, fcolor, 0.0, True)
        pylab.setp(pylab.gca(), xticklabels=[])
        kepplot.labels('', ylab, 'k', 24)
        pylab.grid()

# plot aperture photometry and PCA corrected data

    if status == 0 and plotpca:
        ax = kepplot.location([0.06, 0.09, 0.93, 0.43])
        yr = array([], 'float32')
        npc = min([6, nrem])
        for i in range(npc - 1, -1, -1):
            py = pcar[:, i] * c[i]
            py, ylab, status = kepplot.cleany(py, 1.0, logfile, verbose)
            cl = float(i) / (float(npc))
            kepplot.plot1d(px, py, cadence, [1.0 - cl, 0.0, cl], 2, fcolor,
                           0.0, True)
            yr = append(yr, py)
        y1 = max(yr)
        y2 = -min(yr)
        kepplot.RangeOfPlot(px, array([-y1, y1, -y2, y2]), 0.01, False)
        kepplot.labels(xlab, 'Principal Components', 'k', 24)
        pylab.grid()

# save plot to file

    if status == 0 and plotpca:
        pylab.savefig(repname)

# render plot

    if status == 0 and plotpca:
        kepplot.render(cmdLine)

# stop time

    if status == 0:
        kepmsg.clock('KEPPCA ended at', logfile, verbose)

    return
Esempio n. 43
0
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)
Esempio n. 44
0
    filenames.sort()
    datepat = re.compile(r'Q+\d+')
    index = []
    for j in range(len(filenames)):
        index.append(datepat.findall(filenames[j])[0][1:])
    d = dict(zip(index, filenames))
    #newfilenames = [d[k] for k in sorted(d.keys(), key=int)]
    qua = sorted(d.keys(), key=int)
    noise_dict = dict()
    noise_dict['kepid'] = kepid[i]
    for j in range(18):
        if str(j) not in qua:
            noise_dict['Q' + str(j) + 'rms'] = 'NaN'
        else:
            instr = fits.open(d[str(j)])
            tstart, tstop, bjdref, cadence = kepio.timekeys(instr, d[str(j)])
            #reduce lc
            intime, nordata = kepreduce.reduce_lc(instr, d[str(j)])
            #do sigma_clip
            mean, median, std = sigma_clipped_stats(nordata,
                                                    sigma=3.0,
                                                    iters=5)
            noise_dict['Q' + str(j) + 'rms'] = std
        plt.scatter(j, noise_dict['Q' + str(j) + 'rms'] * 1e06, color='black')
    plt.title('kepid:' + kepid[i] + '_no:' + no[i])
    plt.xlabel('Quarter')
    plt.ylabel('rms(ppm)')
    plt.close()
    #temp_frame = pd.DataFrame([noise_dict])
    #noise_frame = noise_frame.append(temp_frame, ignore_index=True)
Esempio n. 45
0
def kepwindow(infile,outfile,fcol,fmax,nfreq,plot,clobber,verbose,logfile,status, cmdLine=False): 

## startup parameters

    status = 0
    labelsize = 24
    ticksize = 16
    xsize = 18
    ysize = 6
    lcolor = '#0000ff'
    lwidth = 1.0
    fcolor = '#ffff00'
    falpha = 0.2

## log the call 

    hashline = '----------------------------------------------------------------------------'
    kepmsg.log(logfile,hashline,verbose)
    call = 'KEPWINDOW -- '
    call += 'infile='+infile+' '
    call += 'outfile='+outfile+' '
    call += 'fcol='+fcol+' '
    call += 'fmax='+str(fmax)+' '
    call += 'nfreq='+str(nfreq)+' '
    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('KEPWINDOW 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 -- KEPWINDOW: ' + 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 columns

    if status == 0:
	try:
            barytime = instr[1].data.field('barytime')
	except:
            barytime, status = kepio.readfitscol(infile,instr[1].data,'time',logfile,verbose)
	signal, status = kepio.readfitscol(infile,instr[1].data,fcol,logfile,verbose)

## remove infinite data from time series

    if status == 0:
        incols = [barytime, signal]
        outcols = kepstat.removeinfinlc(signal, incols)
        barytime = outcols[0]
        signal = outcols[1]

## reset signal data to zero

    if status == 0:
        signal = ones(len(outcols[1]))

## frequency steps

    if status == 0:
        deltaf = fmax / nfreq

## loop through frequency steps; determine FT power

    if status == 0:
        fr, power = kepfourier.ft(barytime,signal,0.0,fmax,deltaf,True)
        power[0] = 1.0
        
## mirror window function around ordinate

    if status == 0:
        work1 = []; work2 = []
        for i in range(len(fr)-1, 0, -1):
            work1.append(-fr[i])
            work2.append(power[i])
        for i in range(len(fr)):
            work1.append(fr[i])
            work2.append(power[i])
        fr = array(work1,dtype='float32')
        power = array(work2,dtype='float32')

## write output file

    if status == 0:
        col1 = Column(name='FREQUENCY',format='E',unit='days',array=fr)
        col2 = Column(name='POWER',format='E',array=power)
        cols = ColDefs([col1,col2])
        instr.append(new_table(cols))
        instr[-1].header.update('EXTNAME','WINDOW FUNCTION','extension name')
        
## comment keyword in output file

    if status == 0:
        status = kepkey.comment(call,instr[0],outfile,logfile,verbose)
        instr.writeto(outfile)

## close input file

    if status == 0:
        status = kepio.closefits(instr,logfile,verbose)	    

## data limits

    if status == 0:
        nrm = len(str(int(power.max())))-1
        power = power / 10**nrm
        ylab = 'Power (x10$^%d$)' % nrm
	xmin = fr.min()
	xmax = fr.max()
	ymin = power.min()
	ymax = power.max()
	xr = xmax - xmin
	yr = ymax - ymin
        fr = insert(fr,[0],fr[0])
        fr = append(fr,fr[-1])
        power = insert(power,[0],0.0) 
        power = append(power,0.0)

## plot power spectrum

    if status == 0 and plot:
        try:
            params = {'backend': 'png',
                      'axes.linewidth': 2.5,
                      'axes.labelsize': labelsize,
                      'axes.font': 'sans-serif',
                      'axes.fontweight' : 'bold',
                      'text.fontsize': 12,
                      'legend.fontsize': 12,
                      'xtick.labelsize': ticksize,
                      'ytick.labelsize': ticksize}
            rcParams.update(params)
        except:
            print('ERROR -- KEPWINDOW: install latex for scientific plotting')
            status = 1
    if status == 0 and plot:
        pylab.figure(1,figsize=[xsize,ysize])
        pylab.axes([0.06,0.113,0.93,0.86])
        pylab.plot(fr,power,color=lcolor,linestyle='-',linewidth=lwidth)
        fill(fr,power,color=fcolor,linewidth=0.0,alpha=falpha)
        xlim(xmin-xr*0.01,xmax+xr*0.01)
        if ymin-yr*0.01 <= 0.0:
            ylim(1.0e-10,ymax+yr*0.01)
        else:
            ylim(ymin-yr*0.01,ymax+yr*0.01)
        xlabel(r'Frequency (d$^{-1}$)', {'color' : 'k'})
        ylabel('Power', {'color' : 'k'})

# render plot

        if cmdLine: 
            pylab.show()
        else: 
            pylab.ion()
            pylab.plot([])
            pylab.ioff()
		
## end time

    if (status == 0):
	    message = 'KEPWINDOW completed at'
    else:
	    message = '\nKEPWINDOW aborted at'
    kepmsg.clock(message,logfile,verbose)
Esempio n. 46
0
def kepcotrendsc(infile,outfile,bvfile,listbv,fitmethod,fitpower,iterate,sigma,maskfile,scinterp,plot,clobber,verbose,logfile,
	status,cmdLine=False):
	"""
	Setup the kepcotrend environment

	infile:
	the input file in the FITS format obtained from MAST

	outfile:
	The output will be a fits file in the same style as the input file but with two additional columns: CBVSAP_MODL and CBVSAP_FLUX. The first of these is the best fitting linear combination of basis vectors. The second is the new flux with the basis vector sum subtracted. This is the new flux value.

	plot:
	either True or False if you want to see a plot of the light curve
	The top plot shows the original light curve in blue and the sum of basis vectors in red
	The bottom plot has had the basis vector sum subracted

	bvfile:
	the name of the FITS file containing the basis vectors

	listbv:
	the basis vectors to fit to the data

	fitmethod:
	fit using either the 'llsq' or the 'simplex' method. 'llsq' is usually the correct one to use because as the basis vectors are orthogonal. Simplex gives you option of using a different merit function - ie. you can minimise the least absolute residual instead of the least squares which weights outliers less

	fitpower:
	if using a simplex you can chose your own power in the metir function - i.e. the merit function minimises abs(Obs - Mod)^P. P=2 is least squares, P = 1 minimises least absolutes

	iterate:
	should the program fit the basis vectors to the light curve data then remove data points further than 'sigma' from the fit and then refit

	maskfile:
	this is the name of a mask file which can be used to define regions of the flux time series to exclude from the fit. The easiest way to create this is by using keprange from the PyKE set of tools. You can also make this yourself with two BJDs on each line in the file specifying the beginning and ending date of the region to exclude.

	scinterp:
	the basis vectors are only calculated for long cadence data, therefore if you want to use short cadence data you have to interpolate the basis vectors. There are several methods to do this, the best of these probably being nearest which picks the value of the nearest long cadence data point.
	The options available are None|linear|nearest|zero|slinear|quadratic|cubic
	If you are using short cadence data don't choose none
	"""
	# log the call
	hashline = '----------------------------------------------------------------------------'
	kepmsg.log(logfile,hashline,verbose)
	call = 'KEPCOTREND -- '
	call += 'infile='+infile+' '
	call += 'outfile='+outfile+' '
	call += 'bvfile='+bvfile+' '
#	call += 'numpcomp= '+str(numpcomp)+' '
	call += 'listbv= '+str(listbv)+' '
	call += 'fitmethod=' +str(fitmethod)+ ' '
	call += 'fitpower=' + str(fitpower)+ ' '
	iterateit = 'n'
	if (iterate): iterateit = 'y'
	call += 'iterate='+iterateit+ ' '
	call += 'sigma_clip='+str(sigma)+' '
	call += 'mask_file='+maskfile+' '
	call += 'scinterp=' + str(scinterp)+ ' '
	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('KEPCOTREND 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 -- KEPCOTREND: ' + outfile + ' exists. Use --clobber'
		status = kepmsg.err(logfile,message,verbose)

	# open input file
	if status == 0:
		instr, status = kepio.openfits(infile,'readonly',logfile,verbose)
		tstart, tstop, bjdref, cadence, status = kepio.timekeys(instr,
			infile,logfile,verbose,status)

	# fudge non-compliant FITS keywords with no values
	if status == 0:
		instr = kepkey.emptykeys(instr,file,logfile,verbose)

	if status == 0:
		if not kepio.fileexists(bvfile):
			message = 'ERROR -- KEPCOTREND: ' + bvfile + ' does not exist.'
			status = kepmsg.err(logfile,message,verbose)

	#lsq_sq - nonlinear least squares fitting and simplex_abs have been
	#removed from the options in PyRAF but they are still in the code!
	if status == 0:
		if fitmethod not in ['llsq','matrix','lst_sq','simplex_abs','simplex']:
			message = 'Fit method must either: llsq, matrix, lst_sq or simplex'
			status = kepmsg.err(logfile,message,verbose)

	if status == 0:
		if not is_numlike(fitpower) and fitpower is not None:
			message = 'Fit power must be an real number or None'
			status = kepmsg.err(logfile,message,verbose)



	if status == 0:
		if fitpower is None:
			fitpower = 1.

	# input data
	if status == 0:
		short = False
		try:
			test = str(instr[0].header['FILEVER'])
			version = 2
		except KeyError:
			version = 1

		table = instr[1].data
		if version == 1:
			if str(instr[1].header['DATATYPE']) == 'long cadence':
				#print 'Light curve was taken in Lond Cadence mode!'
				quarter = str(instr[1].header['QUARTER'])
				module = str(instr[1].header['MODULE'])
				output = str(instr[1].header['OUTPUT'])
				channel = str(instr[1].header['CHANNEL'])

				lc_cad_o = table.field('cadence_number')
				lc_date_o = table.field('barytime')
				lc_flux_o = table.field('ap_raw_flux') / 1625.3468 #convert to e-/s
				lc_err_o = table.field('ap_raw_err') / 1625.3468 #convert to e-/s
			elif str(instr[1].header['DATATYPE']) == 'short cadence':
				short = True
				#print 'Light curve was taken in Short Cadence mode!'
				quarter = str(instr[1].header['QUARTER'])
				module = str(instr[1].header['MODULE'])
				output = str(instr[1].header['OUTPUT'])
				channel = str(instr[1].header['CHANNEL'])

				lc_cad_o = table.field('cadence_number')
				lc_date_o = table.field('barytime')
				lc_flux_o = table.field('ap_raw_flux') / 54.178 #convert to e-/s
				lc_err_o = table.field('ap_raw_err') / 54.178 #convert to e-/s

		elif version >= 2:
			if str(instr[0].header['OBSMODE']) == 'long cadence':
				#print 'Light curve was taken in Long Cadence mode!'

				quarter = str(instr[0].header['QUARTER'])
				module = str(instr[0].header['MODULE'])
				output = str(instr[0].header['OUTPUT'])
				channel = str(instr[0].header['CHANNEL'])

				lc_cad_o = table.field('CADENCENO')
				lc_date_o = table.field('TIME')
				lc_flux_o = table.field('SAP_FLUX')
				lc_err_o = table.field('SAP_FLUX_ERR')
			elif str(instr[0].header['OBSMODE']) == 'short cadence':
				#print 'Light curve was taken in Short Cadence mode!'
				short = True
				quarter = str(instr[0].header['QUARTER'])
				module = str(instr[0].header['MODULE'])
				output = str(instr[0].header['OUTPUT'])
				channel = str(instr[0].header['CHANNEL'])

				lc_cad_o = table.field('CADENCENO')
				lc_date_o = table.field('TIME')
				lc_flux_o = table.field('SAP_FLUX')
				lc_err_o = table.field('SAP_FLUX_ERR')


		if str(quarter) == str(4) and version == 1:
			lc_cad_o = lc_cad_o[lc_cad_o >= 11914]
			lc_date_o = lc_date_o[lc_cad_o >= 11914]
			lc_flux_o = lc_flux_o[lc_cad_o >= 11914]
			lc_err_o = lc_err_o[lc_cad_o >= 11914]

		# bvfilename = '%s/Q%s_%s_%s_map.txt' %(bvfile,quarter,module,output)
		# if str(quarter) == str(5):
		# 	bvdata = genfromtxt(bvfilename)
		# elif str(quarter) == str(3) or str(quarter) == str(4):
		# 	bvdata = genfromtxt(bvfilename,skip_header=22)
		# elif str(quarter) == str(1):
		# 	bvdata = genfromtxt(bvfilename,skip_header=10)
		# else:
		# 	bvdata = genfromtxt(bvfilename,skip_header=13)

		if short and scinterp == 'None':
			message = 'You cannot select None as the interpolation method because you are using short cadence data and therefore must use some form of interpolation. I reccommend nearest if you are unsure.'
			status = kepmsg.err(logfile,message,verbose)

		bvfiledata = pyfits.open(bvfile)
		bvdata = bvfiledata['MODOUT_%s_%s' %(module,output)].data


		if int(bvfiledata[0].header['QUARTER']) != int(quarter):
			message = 'CBV file and light curve file are from different quarters. CBV file is from Q%s and light curve is from Q%s' %(int(bvfiledata[0].header['QUARTER']),int(quarter))
			status = kepmsg.err(logfile,message,verbose)

	if status == 0:
		if int(quarter) == 4 and int(module) == 3:
			message = 'Approximately twenty days into Q4 Module 3 failed. As a result, Q4 light curves contain these 20 day of data. However, we do not calculate CBVs for this section of data.'
			status = kepmsg.err(logfile,message,verbose)

	if status == 0:


		#cut out infinites and zero flux columns
		lc_cad,lc_date,lc_flux,lc_err,bad_data = cutBadData(lc_cad_o,
			lc_date_o,lc_flux_o,lc_err_o)

		#get a list of basis vectors to use from the list given
		#accept different seperators
		listbv = listbv.strip()
		if listbv[1] in [' ',',',':',';','|',', ']:
			separator = str(listbv)[1]
		else:
			message = 'You must separate your basis vector numbers to use with \' \' \',\' \':\' \';\' or \'|\' and the first basis vector to use must be between 1 and 9'
			status = kepmsg.err(logfile,message,verbose)


	if status == 0:
		bvlist = fromstring(listbv,dtype=int,sep=separator)

		if bvlist[0] == 0:
			message = 'Must use at least one basis vector'
			status = kepmsg.err(logfile,message,verbose)
	if status == 0:
		#pcomps = get_pcomp(pcompdata,n_comps,lc_cad)
		# if str(quarter) == str(5):
		# 	bvectors = get_pcomp_list(bvdata,bvlist,lc_cad)
		# else:
		#	bvectors = get_pcomp_list_newformat(bvdata,bvlist,lc_cad)

		if short:
			bvdata.field('CADENCENO')[:] = (((bvdata.field('CADENCENO')[:] + (7.5/15.) )* 30.) - 11540.).round()

		bvectors,in1derror = get_pcomp_list_newformat(bvdata,bvlist,lc_cad,short,scinterp)

		if in1derror:
			message = 'It seems that you have an old version of numpy which does not have the in1d function included. Please update your version of numpy to a version 1.4.0 or later'
			status = kepmsg.err(logfile,message,verbose)
	if status == 0:

		medflux = median(lc_flux)
		n_flux = (lc_flux /medflux)-1
		n_err = sqrt(pow(lc_err,2)/ pow(medflux,2))

		#plt.errorbar(lc_cad,n_flux,yerr=n_err)
		#plt.errorbar(lc_cad,lc_flux,yerr=lc_err)

		#n_err = median(lc_err/lc_flux) * n_flux
		#print n_err

		#does an iterative least squares fit
		#t1 = do_leastsq(pcomps,lc_cad,n_flux)
		#

		if maskfile != '':
			domasking = True
			if not kepio.fileexists(maskfile):
				message = 'Maskfile %s does not exist' %maskfile
				status = kepmsg.err(logfile,message,verbose)
		else:
			domasking = False



	if status == 0:
		if domasking:

			lc_date_masked = copy(lc_date)
			n_flux_masked = copy(n_flux)
			lc_cad_masked = copy(lc_cad)
			n_err_masked = copy(n_err)
			maskdata = atleast_2d(genfromtxt(maskfile,delimiter=','))
			#make a mask of True values incase there are not regions in maskfile to exclude.
			mask = zeros(len(lc_date_masked)) == 0.
			for maskrange in maskdata:
				if version == 1:
					start = maskrange[0] - 2400000.0
					end = maskrange[1] - 2400000.0
				elif version == 2:
					start = maskrange[0] - 2454833.
					end = maskrange[1] - 2454833.
				masknew = logical_xor(lc_date < start,lc_date > end)
				mask = logical_and(mask,masknew)

			lc_date_masked = lc_date_masked[mask]
			n_flux_masked = n_flux_masked[mask]
			lc_cad_masked = lc_cad_masked[mask]
			n_err_masked = n_err_masked[mask]
		else:
			lc_date_masked = copy(lc_date)
			n_flux_masked = copy(n_flux)
			lc_cad_masked = copy(lc_cad)
			n_err_masked = copy(n_err)


		#pcomps = get_pcomp(pcompdata,n_comps,lc_cad)

		bvectors_masked,hasin1d = get_pcomp_list_newformat(bvdata,bvlist,lc_cad_masked,short,scinterp)


		if (iterate) and sigma is None:
			message = 'If fitting iteratively you must specify a clipping range'
			status = kepmsg.err(logfile,message,verbose)

	if status == 0:
		#uses Pvals = yhat * U_transpose
		if (iterate):
			coeffs,fittedmask = do_lst_iter(bvectors_masked,lc_cad_masked
				,n_flux_masked,sigma,50.,fitmethod,fitpower)
		else:
			if fitmethod == 'matrix' and domasking:
				coeffs = do_lsq_uhat(bvectors_masked,lc_cad_masked,n_flux_masked,False)
			if fitmethod == 'llsq' and domasking:
				coeffs = do_lsq_uhat(bvectors_masked,lc_cad_masked,n_flux_masked,False)
			elif fitmethod == 'lst_sq':
				coeffs = do_lsq_nlin(bvectors_masked,lc_cad_masked,n_flux_masked)
			elif fitmethod == 'simplex_abs':
				coeffs = do_lsq_fmin(bvectors_masked,lc_cad_masked,n_flux_masked)
			elif fitmethod == 'simplex':
				coeffs = do_lsq_fmin_pow(bvectors_masked,lc_cad_masked,n_flux_masked,fitpower)
			else:
				coeffs = do_lsq_uhat(bvectors_masked,lc_cad_masked,n_flux_masked)



		flux_after = (get_newflux(n_flux,bvectors,coeffs) +1) * medflux
		flux_after_masked = (get_newflux(n_flux_masked,bvectors_masked,coeffs) +1) * medflux
		bvsum = get_pcompsum(bvectors,coeffs)

		bvsum_masked =  get_pcompsum(bvectors_masked,coeffs)

		#print 'chi2: ' + str(chi2_gtf(n_flux,bvsum,n_err,2.*len(n_flux)-2))
		#print 'rms: ' + str(rms(n_flux,bvsum))


		bvsum_nans = putInNans(bad_data,bvsum)
		flux_after_nans = putInNans(bad_data,flux_after)


	if plot and status == 0:
         newmedflux = median(flux_after + 1)
         bvsum_un_norm = newmedflux*(1-bvsum)
         #bvsum_un_norm = 0-bvsum
         #lc_flux = n_flux
         do_plot(lc_date,lc_flux,flux_after,
			bvsum_un_norm,lc_cad,bad_data,lc_cad_o,version,cmdLine)

	if status== 0:
		make_outfile(instr,outfile,flux_after_nans,bvsum_nans,version)

	# close input file
	if status == 0:
		status = kepio.closefits(instr,logfile,verbose)

		#print some results to screen:
		print '      -----      '
		if iterate:
			flux_fit = n_flux_masked[fittedmask]
			sum_fit = bvsum_masked[fittedmask]
			err_fit = n_err_masked[fittedmask]
		else:
			flux_fit = n_flux_masked
			sum_fit = bvsum_masked
			err_fit = n_err_masked
		print 'reduced chi2: ' + str(chi2_gtf(flux_fit,sum_fit,err_fit,len(flux_fit)-len(coeffs)))
		print 'rms: ' + str(medflux*rms(flux_fit,sum_fit))
		for i in range(len(coeffs)):
			print 'Coefficient of CBV #%s: %s' %(i+1,coeffs[i])
		print '      -----      '


	# end time
	if (status == 0):
		message = 'KEPCOTREND completed at'
	else:
		message = '\nKEPCOTTREND aborted at'
	kepmsg.clock(message,logfile,verbose)

	return
Esempio n. 47
0
def kepbin(
    infile,
    outfile,
    fluxcol,
    do_nbin,
    nbins,
    do_binwidth,
    binwidth,
    do_ownbins,
    binfile,
    method,
    interpm,
    plot,
    clobber,
    verbose,
    logfile,
    status,
):
    """
	Setup the kepbin environment
	"""
    # log the call
    hashline = "----------------------------------------------------------------------------"
    kepmsg.log(logfile, hashline, verbose)
    call = "KEPBIN -- "
    call += "infile=" + infile + " "
    call += "outfile=" + outfile + " "
    call += "fluxcol=" + fluxcol + " "
    donbin = "n"
    if do_nbin:
        donbin = "y"
    call += "donbin=" + donbin + " "
    dobinwidth = "n"
    if do_binwidth:
        dobinwidth = "y"
    call += "dbinwidth=" + dobinwidth + " "
    doownbin = "n"
    if do_ownbins:
        doownbin = "y"
    call += "doownbin=" + doownbin + " "
    call += "method=" + method + " "
    call += "interpm=" + interpm + " "
    plotit = "n"
    if plot:
        plotit = "y"
    call += "plot=" + plotit + " "
    overwrite = "n"
    if clobber:
        overwrite = "y"
    call += "clobber=" + overwrite + " "
    chatter = "n"
    if verbose:
        chatter = "y"
    call += "verbose=" + chatter + " "
    call += "logfile=" + logfile
    kepmsg.log(logfile, call + "\n", verbose)

    # start time
    kepmsg.clock("KEPCLIP started at", logfile, verbose)

    # test log file
    logfile = kepmsg.test(logfile)

    # clobber output file
    if clobber:
        status = kepio.clobber(outfile, logfile, verbose)
    if kepio.fileexists(outfile):
        message = "ERROR -- KEPCLIP: " + outfile + " exists. Use --clobber"
        status = kepmsg.err(logfile, message, verbose)

        # open input file
    if status == 0:
        instr, status = kepio.openfits(infile, "readonly", logfile, verbose)
        tstart, tstop, bjdref, cadence, status = kepio.timekeys(instr, infile, logfile, verbose, status)

        # fudge non-compliant FITS keywords with no values
    if status == 0:
        instr = kepkey.emptykeys(instr, file, logfile, verbose)

        # input data
    if status == 0:
        table = instr[1].data

        # read time and flux columns
    date = table.field("barytime")
    flux = table.field(fluxcol)

    # cut out infinites and zero flux columns
    date, flux = cutBadData(date, flux)

    if do_nbin:
        bdate, bflux = bin_funct(date, flux, nbins=nbins, method=method, interpm=interpm)
    elif do_binwidth:
        bdate, bflux = bin_funct(date, flux, binwidth=binwidth, method=method, interpm=interpm)
    elif do_ownbins:
        filepointer = open(binfile, "r")
        ownbins = []
        for line in filepointer:
            splitted = line.split()
            ownbins.append(float(splitted[0]))
        ownbins = n.array(ownbins)
        bdate, bflux = bin_funct(date, flux, ownbins=ownbins, method=method, interpm=interpm)

    if plot:
        do_plot(bdate, bflux)

    if status == 0:
        col1 = pyfits.Column(name="bdate", format="E", unit="day", array=bdate)
        col2 = pyfits.Column(name="bflux", format="E", unit="e-/cadence", array=bflux)
        cols = pyfits.ColDefs([col1, col2])
        instr.append(pyfits.new_table(cols))
        instr[-1].header.update("EXTNAME", "BINNED DATA", "extension name")
        instr.writeto(outfile)

        # close input file
    if status == 0:
        status = kepio.closefits(instr, logfile, verbose)

        # end time
    if status == 0:
        message = "KEPBIN completed at"
    else:
        message = "\nKEPBIN aborted at"
    kepmsg.clock(message, logfile, verbose)
Esempio n. 48
0
def kepwindow(infile,
              outfile,
              fcol,
              fmax,
              nfreq,
              plot,
              clobber,
              verbose,
              logfile,
              status,
              cmdLine=False):

    ## startup parameters

    status = 0
    labelsize = 24
    ticksize = 16
    xsize = 18
    ysize = 6
    lcolor = '#0000ff'
    lwidth = 1.0
    fcolor = '#ffff00'
    falpha = 0.2

    ## log the call

    hashline = '----------------------------------------------------------------------------'
    kepmsg.log(logfile, hashline, verbose)
    call = 'KEPWINDOW -- '
    call += 'infile=' + infile + ' '
    call += 'outfile=' + outfile + ' '
    call += 'fcol=' + fcol + ' '
    call += 'fmax=' + str(fmax) + ' '
    call += 'nfreq=' + str(nfreq) + ' '
    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('KEPWINDOW 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 -- KEPWINDOW: ' + 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 columns

    if status == 0:
        try:
            barytime = instr[1].data.field('barytime')
        except:
            barytime, status = kepio.readfitscol(infile, instr[1].data, 'time',
                                                 logfile, verbose)
        signal, status = kepio.readfitscol(infile, instr[1].data, fcol,
                                           logfile, verbose)

## remove infinite data from time series

    if status == 0:
        incols = [barytime, signal]
        outcols = kepstat.removeinfinlc(signal, incols)
        barytime = outcols[0]
        signal = outcols[1]

## reset signal data to zero

    if status == 0:
        signal = ones(len(outcols[1]))

## frequency steps

    if status == 0:
        deltaf = fmax / nfreq

## loop through frequency steps; determine FT power

    if status == 0:
        fr, power = kepfourier.ft(barytime, signal, 0.0, fmax, deltaf, True)
        power[0] = 1.0

## mirror window function around ordinate

    if status == 0:
        work1 = []
        work2 = []
        for i in range(len(fr) - 1, 0, -1):
            work1.append(-fr[i])
            work2.append(power[i])
        for i in range(len(fr)):
            work1.append(fr[i])
            work2.append(power[i])
        fr = array(work1, dtype='float32')
        power = array(work2, dtype='float32')

## write output file

    if status == 0:
        col1 = Column(name='FREQUENCY', format='E', unit='days', array=fr)
        col2 = Column(name='POWER', format='E', array=power)
        cols = ColDefs([col1, col2])
        instr.append(new_table(cols))
        instr[-1].header.update('EXTNAME', 'WINDOW FUNCTION', 'extension name')

## comment keyword in output file

    if status == 0:
        status = kepkey.comment(call, instr[0], outfile, logfile, verbose)
        instr.writeto(outfile)

## close input file

    if status == 0:
        status = kepio.closefits(instr, logfile, verbose)

## data limits

    if status == 0:
        nrm = len(str(int(power.max()))) - 1
        power = power / 10**nrm
        ylab = 'Power (x10$^%d$)' % nrm
        xmin = fr.min()
        xmax = fr.max()
        ymin = power.min()
        ymax = power.max()
        xr = xmax - xmin
        yr = ymax - ymin
        fr = insert(fr, [0], fr[0])
        fr = append(fr, fr[-1])
        power = insert(power, [0], 0.0)
        power = append(power, 0.0)

## plot power spectrum

    if status == 0 and plot:
        try:
            params = {
                'backend': 'png',
                'axes.linewidth': 2.5,
                'axes.labelsize': labelsize,
                'axes.font': 'sans-serif',
                'axes.fontweight': 'bold',
                'text.fontsize': 12,
                'legend.fontsize': 12,
                'xtick.labelsize': ticksize,
                'ytick.labelsize': ticksize
            }
            rcParams.update(params)
        except:
            print('ERROR -- KEPWINDOW: install latex for scientific plotting')
            status = 1
    if status == 0 and plot:
        pylab.figure(1, figsize=[xsize, ysize])
        pylab.axes([0.06, 0.113, 0.93, 0.86])
        pylab.plot(fr, power, color=lcolor, linestyle='-', linewidth=lwidth)
        fill(fr, power, color=fcolor, linewidth=0.0, alpha=falpha)
        xlim(xmin - xr * 0.01, xmax + xr * 0.01)
        if ymin - yr * 0.01 <= 0.0:
            ylim(1.0e-10, ymax + yr * 0.01)
        else:
            ylim(ymin - yr * 0.01, ymax + yr * 0.01)
        xlabel(r'Frequency (d$^{-1}$)', {'color': 'k'})
        ylabel('Power', {'color': 'k'})

        # render plot

        if cmdLine:
            pylab.show()
        else:
            pylab.ion()
            pylab.plot([])
            pylab.ioff()

## end time

    if (status == 0):
        message = 'KEPWINDOW completed at'
    else:
        message = '\nKEPWINDOW aborted at'
    kepmsg.clock(message, logfile, verbose)
Esempio n. 49
0
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)
Esempio n. 50
0
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)
Esempio n. 51
0
def kepdraw(infile,outfile,datacol,ploterr,errcol,quality,
	    lcolor,lwidth,fcolor,falpha,labelsize,ticksize,
	    xsize,ysize,fullrange,chooserange,y1,y2,plotgrid,
            ylabel,plottype,verbose,logfile,status,cmdLine=False): 

# log the call 

    hashline = '----------------------------------------------------------------------------'
    kepmsg.log(logfile,hashline,verbose)
    call = 'KEPDRAW -- '
    call += 'infile='+infile+' '
    call += 'outfile='+outfile+' '
    call += 'datacol='+datacol+' '
    perr = 'n'
    if (ploterr): perr = 'y'
    call += 'ploterr='+perr+ ' '
    call += 'errcol='+errcol+' '
    qual = 'n'
    if (quality): qual = 'y'
    call += 'quality='+qual+ ' '
    call += 'lcolor='+str(lcolor)+' '
    call += 'lwidth='+str(lwidth)+' '
    call += 'fcolor='+str(fcolor)+' '
    call += 'falpha='+str(falpha)+' '
    call += 'labelsize='+str(labelsize)+' '
    call += 'ticksize='+str(ticksize)+' '
    call += 'xsize='+str(xsize)+' '
    call += 'ysize='+str(ysize)+' '
    frange = 'n'
    if (fullrange): frange = 'y'
    call += 'fullrange='+frange+ ' '
    crange = 'n'
    if (chooserange): crange = 'y'
    call += 'chooserange='+crange+ ' '
    call += 'ymin='+str(y1)+' '
    call += 'ymax='+str(y2)+' '
    pgrid = 'n'
    if (plotgrid): pgrid = 'y'
    call += 'plotgrid='+pgrid+ ' '
    call += 'ylabel='+str(ylabel)+' '
    call += 'plottype='+plottype+' '
    chatter = 'n'
    if (verbose): chatter = 'y'
    call += 'verbose='+chatter+' '
    call += 'logfile='+logfile
    kepmsg.log(logfile,call+'\n',verbose)

# start time

    kepmsg.clock('KEPDRAW started at',logfile,verbose)

# test log file

    logfile = kepmsg.test(logfile)

# open input file

    if status == 0:
        struct, status = kepio.openfits(infile,'readonly',logfile,verbose)
    if status == 0:
        tstart, tstop, bjdref, cadence, status = kepio.timekeys(struct,infile,logfile,verbose,status)

# read table structure

    if status == 0:
	table, status = kepio.readfitstab(infile,struct[1],logfile,verbose)

# read table columns

    if status == 0:
        intime, status = kepio.readtimecol(infile,table,logfile,verbose)
        intime += bjdref
	indata, status = kepio.readfitscol(infile,table,datacol,logfile,verbose)
        indataerr, status = kepio.readfitscol(infile,table,errcol,logfile,verbose)

# read table quality column

    if status == 0 and quality:
        try:
            qualtest = table.field('SAP_QUALITY')
        except:
            message = 'ERROR -- KEPDRAW: no SAP_QUALITY column found in file ' + infile
            message += '. Use kepdraw quality=n'
            status = kepmsg.err(logfile,message,verbose)

# close infile

    if status == 0:
	status = kepio.closefits(struct,logfile,verbose)

# remove infinities and bad data

    if status == 0:
        if numpy.isnan(numpy.nansum(indataerr)):
            indataerr[:] = 1.0e-5
        work1 = numpy.array([intime, indata, indataerr],dtype='float64')
        work1 = numpy.rot90(work1,3)
        work1 = work1[~numpy.isnan(work1).any(1)]
        work1 = work1[~numpy.isinf(work1).any(1)]
        barytime = numpy.array(work1[:,2],dtype='float64')
        data = numpy.array(work1[:,1],dtype='float32')
        dataerr = numpy.array(work1[:,0],dtype='float32')
        if len(barytime) == 0:
            message = 'ERROR -- KEPDRAW: Plotting arrays are full of NaN'
            status = kepmsg.err(logfile,message,verbose)

# clean up x-axis unit

    if status == 0:
	barytime0 = float(int(tstart / 100) * 100.0)
	barytime -= barytime0
        xlab = 'BJD $-$ %d' % barytime0

# clean up y-axis units

        try:
            nrm = len(str(int(numpy.nanmax(data))))-1
        except:
            nrm = 0
	data = data / 10**nrm
        if 'e$^-$ s$^{-1}$' in ylabel or 'default' in ylabel:
            if nrm == 0:
                ylab1 = 'e$^-$ s$^{-1}$'
            else:
                ylab1 = '10$^%d$ e$^-$ s$^{-1}$' % nrm
        else:
            ylab1 = re.sub('_','-',ylabel)
            

# data limits

	xmin = numpy.nanmin(barytime)
	xmax = numpy.nanmax(barytime)
	ymin = numpy.nanmin(data)
	ymax = numpy.nanmax(data)
	xr = xmax - xmin
	yr = ymax - ymin
        barytime = insert(barytime,[0],[barytime[0]]) 
        barytime = append(barytime,[barytime[-1]])
        data = insert(data,[0],[0.0]) 
        data = append(data,0.0)

# define plot formats

        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}
            pylab.rcParams.update(params)
        except:
            pass

# define size of plot on monitor screen

	pylab.figure(figsize=[xsize,ysize])

# delete any fossil plots in the matplotlib window

        pylab.clf()

# position axes inside the plotting window

	ax = pylab.subplot(111)
	pylab.subplots_adjust(0.07,0.1,0.92,0.88)

# force tick labels to be absolute rather than relative

        pylab.gca().xaxis.set_major_formatter(pylab.ScalarFormatter(useOffset=False))
        pylab.gca().yaxis.set_major_formatter(pylab.ScalarFormatter(useOffset=False))

# rotate y labels by 90 deg

        labels = ax.get_yticklabels()
        setp(labels, 'rotation', 90, fontsize=ticksize)

# if plot type is 'fast' plot data time series as points

        if plottype == 'fast':
            pylab.plot(barytime,data,'o',color=lcolor)            

# if plot type is 'pretty' plot data time series as an unbroken line, retaining data gaps

        else:
            ltime = numpy.array([],dtype='float64')
            ldata = numpy.array([],dtype='float32')
            dt = 0
            work1 = 2.0 * cadence / 86400
            for i in range(1,len(data)-1):
                dt = barytime[i] - barytime[i-1]
                if dt < work1:
                    ltime = numpy.append(ltime,barytime[i])
                    ldata = numpy.append(ldata,data[i])
                else:
                    pylab.plot(ltime,ldata,color=lcolor,linestyle='-',linewidth=lwidth)
                    ltime = numpy.array([],dtype='float64')
                    ldata = numpy.array([],dtype='float32')
            pylab.plot(ltime,ldata,color=lcolor,linestyle='-',linewidth=lwidth)

# plot the fill color below data time series, with no data gaps

	pylab.fill(barytime,data,fc=fcolor,linewidth=0.0,alpha=falpha)

# define plot x and y limits

	pylab.xlim(xmin-xr*0.01,xmax+xr*0.01)
	if ymin-yr*0.01 <= 0.0 or fullrange:
            pylab.ylim(1.0e-10,ymax+yr*0.01)
	else:
            pylab.ylim(ymin-yr*0.01,ymax+yr*0.01)
        if chooserange:
            pylab.ylim(y1,y2)

# plot labels

	pylab.xlabel(xlab, {'color' : 'k'})
        try:
            pylab.ylabel(ylab1, {'color' : 'k'})
        except:
            ylab1 = '10**%d e-/s' % nrm
            pylab.ylabel(ylab1, {'color' : 'k'})

# make grid on plot

	if plotgrid: pylab.grid()

# save plot to file

    if status == 0 and outfile.lower() != 'none':
	pylab.savefig(outfile)

# render plot

        if cmdLine: 
#            pylab.show()
            pylab.show(block=True)
        else: 
            pylab.ion()
            pylab.plot([])
            pylab.ioff()
	
# end time

    if (status == 0):
        message = 'KEPDRAW completed at'
    else:
        message = '\nKEPDRAW aborted at'
    kepmsg.clock(message,logfile,verbose)
Esempio n. 52
0
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)
Esempio n. 53
0
def kepextract(infile,maskfile,outfile,subback,clobber,verbose,logfile,status): 

# startup parameters

    status = 0
    seterr(all="ignore") 

# log the call 

    hashline = '----------------------------------------------------------------------------'
    kepmsg.log(logfile,hashline,verbose)
    call = 'KEPEXTRACT -- '
    call += 'infile='+infile+' '
    call += 'maskfile='+maskfile+' '
    call += 'outfile='+outfile+' '
    backgr = 'n'
    if (subback): backgr = 'y'
    call += 'background='+backgr+ ' '
    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('KEPEXTRACT 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 -- KEPEXTRACT: ' + outfile + ' exists. Use --clobber'
        status = kepmsg.err(logfile,message,verbose)

# open input file

    status = 0
    instr = pyfits.open(infile,mode='readonly',memmap=True)
    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)

# input file data

    if status == 0:
        cards0 = instr[0].header.cards
        cards1 = instr[1].header.cards
        cards2 = instr[2].header.cards
        table = instr[1].data[:]
        maskmap = copy(instr[2].data)

# input table data

    if status == 0:
        kepid, channel, skygroup, module, output, quarter, season, \
            ra, dec, column, row, kepmag, xdim, ydim, time, status = \
            kepio.readTPF(infile,'TIME',logfile,verbose)
        time = numpy.array(time,dtype='float64')
    if status == 0:
        kepid, channel, skygroup, module, output, quarter, season, \
            ra, dec, column, row, kepmag, xdim, ydim, timecorr, status = \
            kepio.readTPF(infile,'TIMECORR',logfile,verbose)
        timecorr = numpy.array(timecorr,dtype='float32')
    if status == 0:
        kepid, channel, skygroup, module, output, quarter, season, \
            ra, dec, column, row, kepmag, xdim, ydim, cadenceno, status = \
            kepio.readTPF(infile,'CADENCENO',logfile,verbose)
        cadenceno = numpy.array(cadenceno,dtype='int')
    if status == 0:
        kepid, channel, skygroup, module, output, quarter, season, \
            ra, dec, column, row, kepmag, xdim, ydim, raw_cnts, status = \
            kepio.readTPF(infile,'RAW_CNTS',logfile,verbose)
    if status == 0:
        kepid, channel, skygroup, module, output, quarter, season, \
            ra, dec, column, row, kepmag, xdim, ydim, flux, status = \
            kepio.readTPF(infile,'FLUX',logfile,verbose)
    if status == 0:
        kepid, channel, skygroup, module, output, quarter, season, \
            ra, dec, column, row, kepmag, xdim, ydim, flux_err, 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, flux_bkg, status = \
            kepio.readTPF(infile,'FLUX_BKG',logfile,verbose)
    if status == 0:
        kepid, channel, skygroup, module, output, quarter, season, \
            ra, dec, column, row, kepmag, xdim, ydim, flux_bkg_err, status = \
            kepio.readTPF(infile,'FLUX_BKG_ERR',logfile,verbose)
    if status == 0:
        kepid, channel, skygroup, module, output, quarter, season, \
            ra, dec, column, row, kepmag, xdim, ydim, cosmic_rays, status = \
            kepio.readTPF(infile,'COSMIC_RAYS',logfile,verbose)
    if status == 0:
        kepid, channel, skygroup, module, output, quarter, season, \
            ra, dec, column, row, kepmag, xdim, ydim, quality, status = \
            kepio.readTPF(infile,'QUALITY',logfile,verbose)
        quality = numpy.array(quality,dtype='int')
    if status == 0:
        try:
            pos_corr1 = numpy.array(table.field('POS_CORR1'),dtype='float64')  #  ---for FITS wave #2
        except:
            pos_corr1 = empty(len(time)); pos_corr1[:] = numpy.nan   # ---temporary before FITS wave #2
        try:
            pos_corr2 = numpy.array(table.field('POS_CORR2'),dtype='float64')  #  ---for FITS wave #2
        except:
            pos_corr2 = empty(len(time)); pos_corr2[:] = numpy.nan   # ---temporary before FITS wave #2

# dummy columns for output file

        psf_centr1 = empty(len(time)); psf_centr1[:] = numpy.nan
        psf_centr1_err = empty(len(time)); psf_centr1_err[:] = numpy.nan
        psf_centr2 = empty(len(time)); psf_centr2[:] = numpy.nan
        psf_centr2_err = empty(len(time)); psf_centr2_err[:] = numpy.nan
#        mom_centr1 = empty(len(time)); mom_centr1[:] = numpy.nan
        mom_centr1_err = empty(len(time)); mom_centr1_err[:] = numpy.nan
#        mom_centr2 = empty(len(time)); mom_centr2[:] = numpy.nan
        mom_centr2_err = empty(len(time)); mom_centr2_err[:] = numpy.nan

# read mask definition file

    if status == 0 and 'aper' not in maskfile.lower() and maskfile.lower() != 'all':
        maskx = array([],'int')
        masky = array([],'int')
        lines, status = kepio.openascii(maskfile,'r',logfile,verbose)
        for line in lines:
            line = line.strip().split('|')
            if len(line) == 6:
                y0 = int(line[3])
                x0 = int(line[4])
                line = line[5].split(';')
                for items in line:
                    try:
                        masky = append(masky,y0 + int(items.split(',')[0]))
                        maskx = append(maskx,x0 + int(items.split(',')[1]))
                    except:
                        continue
        status = kepio.closeascii(lines,logfile,verbose)
        if len(maskx) == 0 or len(masky) == 0:
            message = 'ERROR -- KEPEXTRACT: ' + maskfile + ' contains no pixels.'
            status = kepmsg.err(logfile,message,verbose)

# subimage physical WCS data

    if status == 0:
        crpix1p = cards2['CRPIX1P'].value
        crpix2p = cards2['CRPIX2P'].value
        crval1p = cards2['CRVAL1P'].value
        crval2p = cards2['CRVAL2P'].value
        cdelt1p = cards2['CDELT1P'].value
        cdelt2p = cards2['CDELT2P'].value

# define new subimage bitmap...

    if status == 0 and 'aper' not in maskfile.lower() and maskfile.lower() != 'all':
        aperx = array([],'int')
        apery = array([],'int')
        aperb = array([],'int')
        for i in range(maskmap.shape[0]):
            for j in range(maskmap.shape[1]):
                aperx = append(aperx,crval1p + (j + 1 - crpix1p) * cdelt1p)
                apery = append(apery,crval2p + (i + 1 - crpix2p) * cdelt2p)
                if maskmap[i,j] == 0:
                    aperb = append(aperb,0)
                else:
                    aperb = append(aperb,1)
                    maskmap[i,j] = 1
                    for k in range(len(maskx)):
                        if aperx[-1] == maskx[k] and apery[-1] == masky[k]:
                            aperb[-1] = 3
                            maskmap[i,j] = 3

# trap case where no aperture needs to be defined but pixel positions are still required for centroiding

    if status == 0 and maskfile.lower() == 'all':
        aperx = array([],'int')
        apery = array([],'int')
        for i in range(maskmap.shape[0]):
            for j in range(maskmap.shape[1]):
                aperx = append(aperx,crval1p + (j + 1 - crpix1p) * cdelt1p)
                apery = append(apery,crval2p + (i + 1 - crpix2p) * cdelt2p)

# ...or use old subimage bitmap

    if status == 0 and 'aper' in maskfile.lower():
        aperx = array([],'int')
        apery = array([],'int')
        aperb = array([],'int')
        for i in range(maskmap.shape[0]):
            for j in range(maskmap.shape[1]):
                aperb = append(aperb,maskmap[i,j])
                aperx = append(aperx,crval1p + (j + 1 - crpix1p) * cdelt1p)
                apery = append(apery,crval2p + (i + 1 - crpix2p) * cdelt2p)

# ...or use all pixels

    if status == 0 and maskfile.lower() == 'all':
        aperb = array([],'int')
        for i in range(maskmap.shape[0]):
            for j in range(maskmap.shape[1]):
                if maskmap[i,j] == 0:
                    aperb = append(aperb,0)
                else:
                    aperb = append(aperb,3)
                    maskmap[i,j] = 3

# subtract median pixel value for background?

    if status == 0:
        sky = array([],'float32')
        for i in range(len(time)):
            sky = append(sky,median(flux[i,:]))
        if not subback:
            sky[:] = 0.0

# legal mask defined?

    if status == 0:
        if len(aperb) == 0:
            message = 'ERROR -- KEPEXTRACT: no legal pixels within the subimage are defined.'
            status = kepmsg.err(logfile,message,verbose)
        
# construct new table flux data

    if status == 0:
        naper = (aperb == 3).sum()
        ntime = len(time)
        sap_flux = array([],'float32')
        sap_flux_err = array([],'float32')
        sap_bkg = array([],'float32')
        sap_bkg_err = array([],'float32')
        raw_flux = array([],'float32')
        for i in range(len(time)):
            work1 = array([],'float64')
            work2 = array([],'float64')
            work3 = array([],'float64')
            work4 = array([],'float64')
            work5 = array([],'float64')
            for j in range(len(aperb)):
                if (aperb[j] == 3):
                    work1 = append(work1,flux[i,j]-sky[i])
                    work2 = append(work2,flux_err[i,j])
                    work3 = append(work3,flux_bkg[i,j])
                    work4 = append(work4,flux_bkg_err[i,j])
                    work5 = append(work5,raw_cnts[i,j])
            sap_flux = append(sap_flux,kepstat.sum(work1))
            sap_flux_err = append(sap_flux_err,kepstat.sumerr(work2))
            sap_bkg = append(sap_bkg,kepstat.sum(work3))
            sap_bkg_err = append(sap_bkg_err,kepstat.sumerr(work4))
            raw_flux = append(raw_flux,kepstat.sum(work5))

# construct new table moment data

    if status == 0:
        mom_centr1 = zeros(shape=(ntime))
        mom_centr2 = zeros(shape=(ntime))
        mom_centr1_err = zeros(shape=(ntime))
        mom_centr2_err = zeros(shape=(ntime))
        for i in range(ntime):
            xf = zeros(shape=(naper))
            yf = zeros(shape=(naper))
            f = zeros(shape=(naper))
            xfe = zeros(shape=(naper))
            yfe = zeros(shape=(naper))
            fe = zeros(shape=(naper))
            k = -1
            for j in range(len(aperb)):
                if (aperb[j] == 3):
                    k += 1
                    xf[k] = aperx[j] * flux[i,j]
                    xfe[k] = aperx[j] * flux_err[i,j]
                    yf[k] = apery[j] * flux[i,j]
                    yfe[k] = apery[j] * flux_err[i,j]
                    f[k] = flux[i,j]
                    fe[k] = flux_err[i,j]
            xfsum = kepstat.sum(xf)
            yfsum = kepstat.sum(yf)
            fsum = kepstat.sum(f)
            xfsume = sqrt(kepstat.sum(square(xfe)) / naper)
            yfsume = sqrt(kepstat.sum(square(yfe)) / naper)
            fsume = sqrt(kepstat.sum(square(fe)) / naper)
            mom_centr1[i] = xfsum / fsum
            mom_centr2[i] = yfsum / fsum
            mom_centr1_err[i] = sqrt((xfsume / xfsum)**2 + ((fsume / fsum)**2))
            mom_centr2_err[i] = sqrt((yfsume / yfsum)**2 + ((fsume / fsum)**2))
        mom_centr1_err = mom_centr1_err * mom_centr1
        mom_centr2_err = mom_centr2_err * mom_centr2

# construct new table PSF data

    if status == 0:
        psf_centr1 = zeros(shape=(ntime))
        psf_centr2 = zeros(shape=(ntime))
        psf_centr1_err = zeros(shape=(ntime))
        psf_centr2_err = zeros(shape=(ntime))
        modx = zeros(shape=(naper))
        mody = zeros(shape=(naper))
        k = -1
        for j in range(len(aperb)):
            if (aperb[j] == 3):
                k += 1
                modx[k] = aperx[j]
                mody[k] = apery[j]
        for i in range(ntime):
            modf = zeros(shape=(naper))
            k = -1
            guess = [mom_centr1[i], mom_centr2[i], nanmax(flux[i:]), 1.0, 1.0, 0.0, 0.0]
            for j in range(len(aperb)):
                if (aperb[j] == 3):
                    k += 1
                    modf[k] = flux[i,j]
                    args = (modx, mody, modf)
            try:
                ans = leastsq(kepfunc.PRFgauss2d,guess,args=args,xtol=1.0e-8,ftol=1.0e-4,full_output=True)
                s_sq = (ans[2]['fvec']**2).sum() / (ntime-len(guess))
                psf_centr1[i] = ans[0][0]
                psf_centr2[i] = ans[0][1]
            except:
                pass
            try:
                psf_centr1_err[i] = sqrt(diag(ans[1] * s_sq))[0]
            except:
                psf_centr1_err[i] = numpy.nan
            try:
                psf_centr2_err[i] = sqrt(diag(ans[1] * s_sq))[1]
            except:
                psf_centr2_err[i] = numpy.nan

# construct output primary extension

    if status == 0:
        hdu0 = pyfits.PrimaryHDU()
        for i in range(len(cards0)):
            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
        status = kepkey.history(call,hdu0,outfile,logfile,verbose)
        outstr = HDUList(hdu0)

# construct output light curve extension

    if status == 0:
        col1 = Column(name='TIME',format='D',unit='BJD - 2454833',array=time)
        col2 = Column(name='TIMECORR',format='E',unit='d',array=timecorr)
        col3 = Column(name='CADENCENO',format='J',array=cadenceno)
        col4 = Column(name='SAP_FLUX',format='E',array=sap_flux)
        col5 = Column(name='SAP_FLUX_ERR',format='E',array=sap_flux_err)
        col6 = Column(name='SAP_BKG',format='E',array=sap_bkg)
        col7 = Column(name='SAP_BKG_ERR',format='E',array=sap_bkg_err)
        col8 = Column(name='PDCSAP_FLUX',format='E',array=sap_flux)
        col9 = Column(name='PDCSAP_FLUX_ERR',format='E',array=sap_flux_err)
        col10 = Column(name='SAP_QUALITY',format='J',array=quality)
        col11 = Column(name='PSF_CENTR1',format='E',unit='pixel',array=psf_centr1)
        col12 = Column(name='PSF_CENTR1_ERR',format='E',unit='pixel',array=psf_centr1_err)
        col13 = Column(name='PSF_CENTR2',format='E',unit='pixel',array=psf_centr2)
        col14 = Column(name='PSF_CENTR2_ERR',format='E',unit='pixel',array=psf_centr2_err)
        col15 = Column(name='MOM_CENTR1',format='E',unit='pixel',array=mom_centr1)
        col16 = Column(name='MOM_CENTR1_ERR',format='E',unit='pixel',array=mom_centr1_err)
        col17 = Column(name='MOM_CENTR2',format='E',unit='pixel',array=mom_centr2)
        col18 = Column(name='MOM_CENTR2_ERR',format='E',unit='pixel',array=mom_centr2_err)
        col19 = Column(name='POS_CORR1',format='E',unit='pixel',array=pos_corr1)
        col20 = Column(name='POS_CORR2',format='E',unit='pixel',array=pos_corr2)
        col21 = Column(name='RAW_FLUX',format='E',array=raw_flux)
        cols = ColDefs([col1,col2,col3,col4,col5,col6,col7,col8,col9,col10,col11, \
                            col12,col13,col14,col15,col16,col17,col18,col19,col20,col21])
        hdu1 = new_table(cols)
        hdu1.header.update('TTYPE1','TIME','column title: data time stamps')
        hdu1.header.update('TFORM1','D','data type: float64')
        hdu1.header.update('TUNIT1','BJD - 2454833','column units: barycenter corrected JD')
        hdu1.header.update('TDISP1','D12.7','column display format')
        hdu1.header.update('TTYPE2','TIMECORR','column title: barycentric-timeslice correction')
        hdu1.header.update('TFORM2','E','data type: float32')
        hdu1.header.update('TUNIT2','d','column units: days')
        hdu1.header.update('TTYPE3','CADENCENO','column title: unique cadence number')
        hdu1.header.update('TFORM3','J','column format: signed integer32')
        hdu1.header.update('TTYPE4','SAP_FLUX','column title: aperture photometry flux')
        hdu1.header.update('TFORM4','E','column format: float32')
        hdu1.header.update('TUNIT4','e-/s','column units: electrons per second')
        hdu1.header.update('TTYPE5','SAP_FLUX_ERR','column title: aperture phot. flux error')
        hdu1.header.update('TFORM5','E','column format: float32')
        hdu1.header.update('TUNIT5','e-/s','column units: electrons per second (1-sigma)')
        hdu1.header.update('TTYPE6','SAP_BKG','column title: aperture phot. background flux')
        hdu1.header.update('TFORM6','E','column format: float32')
        hdu1.header.update('TUNIT6','e-/s','column units: electrons per second')
        hdu1.header.update('TTYPE7','SAP_BKG_ERR','column title: ap. phot. background flux error')
        hdu1.header.update('TFORM7','E','column format: float32')
        hdu1.header.update('TUNIT7','e-/s','column units: electrons per second (1-sigma)')
        hdu1.header.update('TTYPE8','PDCSAP_FLUX','column title: PDC photometry flux')
        hdu1.header.update('TFORM8','E','column format: float32')
        hdu1.header.update('TUNIT8','e-/s','column units: electrons per second')
        hdu1.header.update('TTYPE9','PDCSAP_FLUX_ERR','column title: PDC flux error')
        hdu1.header.update('TFORM9','E','column format: float32')
        hdu1.header.update('TUNIT9','e-/s','column units: electrons per second (1-sigma)')
        hdu1.header.update('TTYPE10','SAP_QUALITY','column title: aperture photometry quality flag')
        hdu1.header.update('TFORM10','J','column format: signed integer32')
        hdu1.header.update('TTYPE11','PSF_CENTR1','column title: PSF fitted column centroid')
        hdu1.header.update('TFORM11','E','column format: float32')
        hdu1.header.update('TUNIT11','pixel','column units: pixel')
        hdu1.header.update('TTYPE12','PSF_CENTR1_ERR','column title: PSF fitted column error')
        hdu1.header.update('TFORM12','E','column format: float32')
        hdu1.header.update('TUNIT12','pixel','column units: pixel')
        hdu1.header.update('TTYPE13','PSF_CENTR2','column title: PSF fitted row centroid')
        hdu1.header.update('TFORM13','E','column format: float32')
        hdu1.header.update('TUNIT13','pixel','column units: pixel')
        hdu1.header.update('TTYPE14','PSF_CENTR2_ERR','column title: PSF fitted row error')
        hdu1.header.update('TFORM14','E','column format: float32')
        hdu1.header.update('TUNIT14','pixel','column units: pixel')
        hdu1.header.update('TTYPE15','MOM_CENTR1','column title: moment-derived column centroid')
        hdu1.header.update('TFORM15','E','column format: float32')
        hdu1.header.update('TUNIT15','pixel','column units: pixel')
        hdu1.header.update('TTYPE16','MOM_CENTR1_ERR','column title: moment-derived column error')
        hdu1.header.update('TFORM16','E','column format: float32')
        hdu1.header.update('TUNIT16','pixel','column units: pixel')
        hdu1.header.update('TTYPE17','MOM_CENTR2','column title: moment-derived row centroid')
        hdu1.header.update('TFORM17','E','column format: float32')
        hdu1.header.update('TUNIT17','pixel','column units: pixel')
        hdu1.header.update('TTYPE18','MOM_CENTR2_ERR','column title: moment-derived row error')
        hdu1.header.update('TFORM18','E','column format: float32')
        hdu1.header.update('TUNIT18','pixel','column units: pixel')
        hdu1.header.update('TTYPE19','POS_CORR1','column title: col correction for vel. abbern')
        hdu1.header.update('TFORM19','E','column format: float32')
        hdu1.header.update('TUNIT19','pixel','column units: pixel')
        hdu1.header.update('TTYPE20','POS_CORR2','column title: row correction for vel. abbern')
        hdu1.header.update('TFORM20','E','column format: float32')
        hdu1.header.update('TUNIT20','pixel','column units: pixel')
        hdu1.header.update('TTYPE21','RAW_FLUX','column title: raw aperture photometry flux')
        hdu1.header.update('TFORM21','E','column format: float32')
        hdu1.header.update('TUNIT21','e-/s','column units: electrons per second')
        hdu1.header.update('EXTNAME','LIGHTCURVE','name of extension')
        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']):
                hdu1.header.update(cards1[i].key, cards1[i].value, cards1[i].comment)
        outstr.append(hdu1)

# construct output mask bitmap extension

    if status == 0:
        hdu2 = ImageHDU(maskmap)
        for i in range(len(cards2)):
            if cards2[i].key not in hdu2.header.keys():
                hdu2.header.update(cards2[i].key, cards2[i].value, cards2[i].comment)
            else:
                hdu2.header.cards[cards2[i].key].comment = cards2[i].comment
        outstr.append(hdu2)

# write output file

    if status == 0:
        outstr.writeto(outfile,checksum=True)

# close input structure

    if status == 0:
        status = kepio.closefits(instr,logfile,verbose)	    

# end time

    kepmsg.clock('KEPEXTRACT finished at',logfile,verbose)
Esempio n. 54
0
def kepclip(infile,
            outfile,
            ranges,
            plot,
            plotcol,
            clobber,
            verbose,
            logfile,
            status,
            cmdLine=False):

    # startup parameters

    status = 0
    labelsize = 32
    ticksize = 24
    xsize = 18
    ysize = 10
    lcolor = '#0000ff'
    lwidth = 1.0
    fcolor = '#ffff00'
    falpha = 0.2

    # log the call

    hashline = '----------------------------------------------------------------------------'
    kepmsg.log(logfile, hashline, verbose)
    call = 'KEPCLIP -- '
    call += 'infile=' + infile + ' '
    call += 'outfile=' + outfile + ' '
    call += 'ranges=' + ranges + ' '
    plotit = 'n'
    if (plot): plotit = 'y'
    call += 'plot=' + plotit + ' '
    call += 'plotcol=' + plotcol + ' '
    overwrite = 'n'
    if (clobber): overwrite = 'y'
    call += 'clobber=' + overwrite + ' '
    chatter = 'n'
    if (verbose): chatter = 'y'
    call += 'verbose=' + chatter + ' '
    call += 'logfile=' + logfile
    kepmsg.log(logfile, call + '\n', verbose)

    # start time

    kepmsg.clock('KEPCLIP started at', logfile, verbose)

    # test log file

    logfile = kepmsg.test(logfile)

    # clobber output file

    if clobber: status = kepio.clobber(outfile, logfile, verbose)
    if kepio.fileexists(outfile):
        message = 'ERROR -- KEPCLIP: ' + outfile + ' exists. Use --clobber'
        status = kepmsg.err(logfile, message, verbose)

# time ranges for region

    if status == 0:
        t1 = []
        t2 = []
        t1, t2, status = kepio.timeranges(ranges, logfile, verbose)

# open input file

    if status == 0:
        instr, status = kepio.openfits(infile, 'readonly', logfile, verbose)
        tstart, tstop, bjdref, cadence, status = kepio.timekeys(
            instr, infile, logfile, verbose, status)
    if status == 0:
        try:
            work = instr[0].header['FILEVER']
            cadenom = 1.0
        except:
            cadenom = cadence

# fudge non-compliant FITS keywords with no values

    if status == 0:
        instr = kepkey.emptykeys(instr, file, logfile, verbose)

# input data

    if status == 0:
        table = instr[1].data

# read time and flux columns

    if status == 0:
        barytime, status = kepio.readtimecol(infile, table, logfile, verbose)
    if status == 0:
        flux, status = kepio.readfitscol(infile, table, plotcol, logfile,
                                         verbose)
    if status == 0:
        barytime = barytime + bjdref
        if 'flux' in plotcol.lower():
            flux = flux / cadenom

# filter input data table

    if status == 0:
        naxis2 = 0
        work1 = array([], 'float64')
        work2 = array([], 'float32')
        for i in range(len(barytime)):
            if (numpy.isfinite(barytime[i]) and numpy.isfinite(flux[i])
                    and flux[i] != 0.0):
                reject = False
                for j in range(len(t1)):
                    if (barytime[i] >= t1[j] and barytime[i] <= t2[j]):
                        reject = True
                if not reject:
                    table[naxis2] = table[i]
                    work1 = append(work1, barytime[i])
                    work2 = append(work2, flux[i])
                    naxis2 += 1

# comment keyword in output file

    if status == 0:
        status = kepkey.history(call, instr[0], outfile, logfile, verbose)

# write output file

    if status == 0:
        instr[1].data = table[:naxis2]
        comment = 'NaN cadences removed from data'
        status = kepkey.new('NANCLEAN', True, comment, instr[1], outfile,
                            logfile, verbose)
        instr.writeto(outfile)

# clean up x-axis unit

    if status == 0:
        barytime0 = float(int(tstart / 100) * 100.0)
        barytime = work1 - barytime0
        xlab = 'BJD $-$ %d' % barytime0

# clean up y-axis units

    if status == 0:
        try:
            nrm = len(str(int(work2.max()))) - 1
        except:
            nrm = 0
        flux = work2 / 10**nrm
        ylab = '10$^%d$ e$^-$ s$^{-1}$' % nrm

        # data limits

        xmin = barytime.min()
        xmax = barytime.max()
        ymin = flux.min()
        ymax = flux.max()
        xr = xmax - xmin
        yr = ymax - ymin

# plotting arguments

    if status == 0 and plot:
        try:
            params = {
                'backend': 'png',
                'axes.linewidth': 2.5,
                'axes.labelsize': labelsize,
                'axes.font': 'sans-serif',
                'axes.fontweight': 'bold',
                'text.fontsize': 12,
                'legend.fontsize': 12,
                'xtick.labelsize': ticksize,
                'ytick.labelsize': ticksize
            }
            rcParams.update(params)
        except:
            print('ERROR -- KEPCLIP: install latex for scientific plotting')
            status = 1

# clear window, plot box

    if status == 0 and plot:
        pylab.figure(figsize=[xsize, ysize])
        pylab.clf()
        ax = pylab.axes([0.05, 0.1, 0.94, 0.88])

        # force tick labels to be absolute rather than relative

        pylab.gca().xaxis.set_major_formatter(
            pylab.ScalarFormatter(useOffset=False))
        pylab.gca().yaxis.set_major_formatter(
            pylab.ScalarFormatter(useOffset=False))

        # rotate y labels by 90 deg

        labels = ax.get_yticklabels()
        setp(labels, 'rotation', 90, fontsize=12)

        # plot line data

        ltime = [barytime[0]]
        ldata = [flux[0]]
        for i in range(1, len(flux)):
            if (barytime[i - 1] > barytime[i] - 0.025):
                ltime.append(barytime[i])
                ldata.append(flux[i])
            else:
                ltime = array(ltime, dtype=float64)
                ldata = array(ldata, dtype=float64)
                pylab.plot(ltime,
                           ldata,
                           color=lcolor,
                           linestyle='-',
                           linewidth=lwidth)
                ltime = []
                ldata = []
        ltime = array(ltime, dtype=float64)
        ldata = array(ldata, dtype=float64)
        pylab.plot(ltime, ldata, color=lcolor, linestyle='-', linewidth=lwidth)

        # plot fill data

        barytime = insert(barytime, [0], [barytime[0]])
        barytime = append(barytime, [barytime[-1]])
        flux = insert(flux, [0], [0.0])
        flux = append(flux, [0.0])
        fill(barytime, flux, fc=fcolor, linewidth=0.0, alpha=falpha)
        xlim(xmin - xr * 0.01, xmax + xr * 0.01)
        if ymin - yr * 0.01 <= 0.0:
            ylim(1.0e-10, ymax + yr * 0.01)
        else:
            ylim(ymin - yr * 0.01, ymax + yr * 0.01)
        xlabel(xlab, {'color': 'k'})
        ylabel(ylab, {'color': 'k'})
        grid()

# render plot

    if status == 0 and plot:
        if cmdLine:
            pylab.show()
        else:
            pylab.ion()
            pylab.plot([])
            pylab.ioff()

# close input file

    if status == 0:
        status = kepio.closefits(instr, logfile, verbose)

# end time

    if (status == 0):
        message = 'KEPCLIP completed at'
    else:
        message = '\nKEPCLIP aborted at'
    kepmsg.clock(message, logfile, verbose)
Esempio n. 55
0
def keptransitmodel(inputfile,datacol,errorcol,period_d,rprs,T0,
    Ecc,ars,inc,omega,LDparams,sec,norm=False,
    verbose=0,logfile='logfile.dat',status=0,cmdLine=False):
    

    #write to a logfile
    hashline = '----------------------------------------------------------------------------'
    kepmsg.log(logfile,hashline,verbose)
    call = 'KEPTRANSIT -- '
    call += 'inputfile='+inputfile+' '
    call += 'datacol='+str(datacol)+' '
    call += 'errorcol='+str(errorcol)+' '
    call += 'period_d='+str(period_d)+' '
    call += 'rprs='+str(rprs)+' '
    call += 'T0='+str(T0)+' '
    call += 'Ecc='+str(Ecc)+' '
    call += 'ars='+str(ars)+' '
    call += 'inc='+str(inc)+' '
    call += 'omega='+str(omega)+' '
    call += 'LDparams='+str(LDparams)+' '
    call += 'sec='+str(sec)+' '
    #to finish


    # open input file

    if status == 0:
        instr, status = kepio.openfits(inputfile,'readonly',logfile,verbose)
    if status == 0:
        tstart, tstop, bjdref, cadence, status = kepio.timekeys(instr,
            inputfile,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(inputfile,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 np.isfinite(table.field('barytime')[i]) and \
                            np.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 np.isfinite(table.field('time')[i]) and \
                            np.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(inputfile,instr[1].data,'time',logfile,verbose)
        
        indata, status = kepio.readfitscol(inputfile,instr[1].data,datacol,logfile,verbose)
        inerr, status = kepio.readfitscol(inputfile,instr[1].data,errorcol,logfile,verbose)
    if status == 0:
        intime = intime + bjdref
        indata = indata / cadenom
        inerr = inerr / cadenom

    if status == 0 and norm:
        #first remove outliers before normalizing
        threesig = 3.* np.std(indata)
        mask = np.logical_and(indata< indata + threesig,indata > indata - threesig)
        #now normalize
        indata = indata / np.median(indata[mask])

    if status == 0:
        #need to check if LD params are sensible and in right format
        LDparams = [float(i) for i in LDparams.split()]

        inc = inc * np.pi / 180.


    if status == 0:
        modelfit = tmod.lightcurve(intime,period_d,rprs,T0,Ecc,
            ars,inc,omega,LDparams,sec)

    if status == 0:
        phi, fluxfold, modelfold, errorfold, phiNotFold = fold_data(intime, 
            modelfit,indata,inerr,period_d,T0)

    if status == 0:
        do_plot(intime,modelfit,indata,inerr,period_d,T0,cmdLine)