Esempio n. 1
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)
Esempio n. 2
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. 3
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. 4
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. 5
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. 6
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. 7
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. 8
0
def keptrial(infile,outfile,datacol,errcol,fmin,fmax,nfreq,method,
             ntrials,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 = 'KEPTRIAL -- '
    call += 'infile='+infile+' '
    call += 'outfile='+outfile+' '
    call += 'datacol='+datacol+' '
    call += 'errcol='+errcol+' '
    call += 'fmin='+str(fmin)+' '
    call += 'fmax='+str(fmax)+' '
    call += 'nfreq='+str(nfreq)+' '
    call += 'method='+method+' '
    call += 'ntrials='+str(ntrials)+' '
    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('KEPTRIAL 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 -- KEPTRIAL: ' + outfile + ' exists. Use clobber=yes'
	    kepmsg.err(logfile,message,verbose)
	    status = 1

# open input file

    if status == 0:
        instr, status = kepio.openfits(infile,'readonly',logfile,verbose)

# fudge non-compliant FITS keywords with no values

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

# input data

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

# remove infinite data from time series

    if status == 0:
        try:
            nanclean = instr[1].header['NANCLEAN']
        except:
	    incols = [barytime, signal, err]
	    [barytime, signal, err] = kepstat.removeinfinlc(signal, incols)

# set up plot

    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:
            print('WARNING: install latex for scientific plotting')
            plotLatex = False

# frequency steps and Monte Carlo iterations

    if status == 0:
        deltaf = (fmax - fmin) / nfreq
        freq = []; pmax = []; trial = []
        for i in range(ntrials):
            trial.append(i+1)

# adjust data within the error bars

            work1 = kepstat.randarray(signal,err)

# determine FT power
            fr, power = kepfourier.ft(barytime,work1,fmin,fmax,deltaf,False)

# determine peak in FT

            pmax.append(-1.0e30)
            for j in range(len(fr)):
                if (power[j] > pmax[-1]):
                    pmax[-1] = power[j]
                    f1 = fr[j]
            freq.append(f1)

# plot stop-motion histogram

            pylab.ion()
	    pylab.figure(1,figsize=[7,10])
            clf()
	    pylab.axes([0.08,0.08,0.88,0.89])
            pylab.gca().xaxis.set_major_formatter(pylab.ScalarFormatter(useOffset=False))
            pylab.gca().yaxis.set_major_formatter(pylab.ScalarFormatter(useOffset=False))
            n,bins,patches = pylab.hist(freq,bins=nfreq,range=[fmin,fmax],
                                        align='mid',rwidth=1,ec='#0000ff',
                                        fc='#ffff00',lw=2)

# fit normal distribution to histogram

            x = zeros(len(bins))
            for j in range(1,len(bins)):
                x[j] = (bins[j] + bins[j-1]) / 2
            pinit = numpy.array([float(i),freq[-1],deltaf])
            if i > 3:
                n = array(n,dtype='float32')
                coeffs, errors, covar, sigma, chi2, dof, fit, plotx, ploty, status = \
                    kepfit.leastsquare('gauss',pinit,x[1:],n,None,logfile,verbose)
                fitfunc = kepfunc.gauss()
                f = arange(fmin,fmax,(fmax-fmin)/100)
                fit = fitfunc(coeffs,f)
                pylab.plot(f,fit,'r-',linewidth=2)
            if plotLatex:
                xlabel(r'Frequency (d$^{-1}$)', {'color' : 'k'})
            else:
                xlabel(r'Frequency (1/d)', {'color' : 'k'})
            ylabel('N', {'color' : 'k'})
            xlim(fmin,fmax)
	    grid()

# render plot

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

# period results

    if status == 0:
        p = 1.0 / coeffs[1]
        perr = p * coeffs[2] / coeffs[1]
        f1 = fmin; f2 = fmax
        gotbin = False
        for i in range(len(n)):
            if n[i] > 0 and not gotbin:
                f1 = bins[i]
                gotbin = True
        gotbin = False
        for i in range(len(n)-1,0,-1):
            if n[i] > 0 and not gotbin:
                f2 = bins[i+1]
                gotbin = True
        powave, powstdev = kepstat.stdev(pmax)

# print result

    if status == 0:
        print('              best period: %.10f days (%.7f min)' % (p, p * 1440.0))
        print('     1-sigma period error: %.10f days (%.7f min)' % (perr, perr * 1440.0))
        print('             search range: %.10f - %.10f days  ' % (1.0 / fmax, 1.0 / fmin))
        print('    100%% confidence range: %.10f - %.10f days  ' % (1.0 / f2, 1.0 / f1))
#        print '     detection confidence: %.2f sigma' % (powave / powstdev)
        print('         number of trials: %d' % ntrials)
        print(' number of frequency bins: %d' % nfreq)

# history keyword in output file

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

## write output file

    if status == 0:
        col1 = Column(name='TRIAL',format='J',array=trial)
        col2 = Column(name='FREQUENCY',format='E',unit='1/day',array=freq)
        col3 = Column(name='POWER',format='E',array=pmax)
        cols = ColDefs([col1,col2,col3])
        instr.append(new_table(cols))
        try:
            instr[-1].header.update('EXTNAME','TRIALS','Extension name')
        except:
            status = 1
        try:
            instr[-1].header.update('SEARCHR1',1.0 / fmax,'Search range lower bound (days)')
        except:
            status = 1
        try:
            instr[-1].header.update('SEARCHR2',1.0 / fmin,'Search range upper bound (days)')
        except:
            status = 1
        try:
            instr[-1].header.update('NFREQ',nfreq,'Number of frequency bins')
        except:
            status = 1
        try:
            instr[-1].header.update('PERIOD',p,'Best period (days)')
        except:
            status = 1
        try:
            instr[-1].header.update('PERIODE',perr,'1-sigma period error (days)')
        except:
            status = 1
#        instr[-1].header.update('DETNCONF',powave/powstdev,'Detection significance (sigma)')
        try:
            instr[-1].header.update('CONFIDR1',1.0 / f2,'Trial confidence lower bound (days)')
        except:
            status = 1
        try:
            instr[-1].header.update('CONFIDR2',1.0 / f1,'Trial confidence upper bound (days)')
        except:
            status = 1
        try:
            instr[-1].header.update('NTRIALS',ntrials,'Number of trials')
        except:
            status = 1
        instr.writeto(outfile)
    
# close input file

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

## end time

    if (status == 0):
	    message = 'KEPTRAIL completed at'
    else:
	    message = '\nKEPTRIAL 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)
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. 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 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. 13
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. 14
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)
Esempio n. 15
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. 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)
Esempio n. 17
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. 18
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. 19
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. 20
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. 21
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. 22
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. 23
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. 24
0
def keptrial(infile,
             outfile,
             datacol,
             errcol,
             fmin,
             fmax,
             nfreq,
             method,
             ntrials,
             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 = 'KEPTRIAL -- '
    call += 'infile=' + infile + ' '
    call += 'outfile=' + outfile + ' '
    call += 'datacol=' + datacol + ' '
    call += 'errcol=' + errcol + ' '
    call += 'fmin=' + str(fmin) + ' '
    call += 'fmax=' + str(fmax) + ' '
    call += 'nfreq=' + str(nfreq) + ' '
    call += 'method=' + method + ' '
    call += 'ntrials=' + str(ntrials) + ' '
    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('KEPTRIAL 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 -- KEPTRIAL: ' + outfile + ' exists. Use clobber=yes'
        kepmsg.err(logfile, message, verbose)
        status = 1

# open input file

    if status == 0:
        instr, status = kepio.openfits(infile, 'readonly', logfile, verbose)

# fudge non-compliant FITS keywords with no values

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

# input data

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

# remove infinite data from time series

    if status == 0:
        try:
            nanclean = instr[1].header['NANCLEAN']
        except:
            incols = [barytime, signal, err]
            [barytime, signal, err] = kepstat.removeinfinlc(signal, incols)

# set up plot

    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:
            print('WARNING: install latex for scientific plotting')
            plotLatex = False

# frequency steps and Monte Carlo iterations

    if status == 0:
        deltaf = (fmax - fmin) / nfreq
        freq = []
        pmax = []
        trial = []
        for i in range(ntrials):
            trial.append(i + 1)

            # adjust data within the error bars

            work1 = kepstat.randarray(signal, err)

            # determine FT power
            fr, power = kepfourier.ft(barytime, work1, fmin, fmax, deltaf,
                                      False)

            # determine peak in FT

            pmax.append(-1.0e30)
            for j in range(len(fr)):
                if (power[j] > pmax[-1]):
                    pmax[-1] = power[j]
                    f1 = fr[j]
            freq.append(f1)

            # plot stop-motion histogram

            pylab.ion()
            pylab.figure(1, figsize=[7, 10])
            clf()
            pylab.axes([0.08, 0.08, 0.88, 0.89])
            pylab.gca().xaxis.set_major_formatter(
                pylab.ScalarFormatter(useOffset=False))
            pylab.gca().yaxis.set_major_formatter(
                pylab.ScalarFormatter(useOffset=False))
            n, bins, patches = pylab.hist(freq,
                                          bins=nfreq,
                                          range=[fmin, fmax],
                                          align='mid',
                                          rwidth=1,
                                          ec='#0000ff',
                                          fc='#ffff00',
                                          lw=2)

            # fit normal distribution to histogram

            x = zeros(len(bins))
            for j in range(1, len(bins)):
                x[j] = (bins[j] + bins[j - 1]) / 2
            pinit = numpy.array([float(i), freq[-1], deltaf])
            if i > 3:
                n = array(n, dtype='float32')
                coeffs, errors, covar, sigma, chi2, dof, fit, plotx, ploty, status = \
                    kepfit.leastsquare('gauss',pinit,x[1:],n,None,logfile,verbose)
                fitfunc = kepfunc.gauss()
                f = arange(fmin, fmax, (fmax - fmin) / 100)
                fit = fitfunc(coeffs, f)
                pylab.plot(f, fit, 'r-', linewidth=2)
            if plotLatex:
                xlabel(r'Frequency (d$^{-1}$)', {'color': 'k'})
            else:
                xlabel(r'Frequency (1/d)', {'color': 'k'})
            ylabel('N', {'color': 'k'})
            xlim(fmin, fmax)
            grid()

# render plot

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

# period results

    if status == 0:
        p = 1.0 / coeffs[1]
        perr = p * coeffs[2] / coeffs[1]
        f1 = fmin
        f2 = fmax
        gotbin = False
        for i in range(len(n)):
            if n[i] > 0 and not gotbin:
                f1 = bins[i]
                gotbin = True
        gotbin = False
        for i in range(len(n) - 1, 0, -1):
            if n[i] > 0 and not gotbin:
                f2 = bins[i + 1]
                gotbin = True
        powave, powstdev = kepstat.stdev(pmax)

# print result

    if status == 0:
        print('              best period: %.10f days (%.7f min)' %
              (p, p * 1440.0))
        print('     1-sigma period error: %.10f days (%.7f min)' %
              (perr, perr * 1440.0))
        print('             search range: %.10f - %.10f days  ' %
              (1.0 / fmax, 1.0 / fmin))
        print('    100%% confidence range: %.10f - %.10f days  ' %
              (1.0 / f2, 1.0 / f1))
        #        print '     detection confidence: %.2f sigma' % (powave / powstdev)
        print('         number of trials: %d' % ntrials)
        print(' number of frequency bins: %d' % nfreq)

# history keyword in output file

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

## write output file

    if status == 0:
        col1 = Column(name='TRIAL', format='J', array=trial)
        col2 = Column(name='FREQUENCY', format='E', unit='1/day', array=freq)
        col3 = Column(name='POWER', format='E', array=pmax)
        cols = ColDefs([col1, col2, col3])
        instr.append(new_table(cols))
        try:
            instr[-1].header.update('EXTNAME', 'TRIALS', 'Extension name')
        except:
            status = 1
        try:
            instr[-1].header.update('SEARCHR1', 1.0 / fmax,
                                    'Search range lower bound (days)')
        except:
            status = 1
        try:
            instr[-1].header.update('SEARCHR2', 1.0 / fmin,
                                    'Search range upper bound (days)')
        except:
            status = 1
        try:
            instr[-1].header.update('NFREQ', nfreq, 'Number of frequency bins')
        except:
            status = 1
        try:
            instr[-1].header.update('PERIOD', p, 'Best period (days)')
        except:
            status = 1
        try:
            instr[-1].header.update('PERIODE', perr,
                                    '1-sigma period error (days)')
        except:
            status = 1
#        instr[-1].header.update('DETNCONF',powave/powstdev,'Detection significance (sigma)')
        try:
            instr[-1].header.update('CONFIDR1', 1.0 / f2,
                                    'Trial confidence lower bound (days)')
        except:
            status = 1
        try:
            instr[-1].header.update('CONFIDR2', 1.0 / f1,
                                    'Trial confidence upper bound (days)')
        except:
            status = 1
        try:
            instr[-1].header.update('NTRIALS', ntrials, 'Number of trials')
        except:
            status = 1
        instr.writeto(outfile)

# close input file

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

## end time

    if (status == 0):
        message = 'KEPTRAIL completed at'
    else:
        message = '\nKEPTRIAL aborted at'
    kepmsg.clock(message, logfile, verbose)
Esempio n. 25
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. 26
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. 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)