Esempio n. 1
0
print(B.kepid)
import numpy as np
kepid = np.random.choice(kepio.get_id(cata_path))

filedir = kepio.pathfinder(kepid, data_path, '*')
filenames = glob.glob(filedir)
all_time = []
all_flux = []
for i in range(len(filenames)):
    #read into cadence
    instr = fits.open(filenames[i])
    tstart, tstop, bjdref, cadence = kepio.timekeys(instr, filenames[i])
    #reduce lc
    intime, nordata = kepreduce.reduce_lc(instr, filenames[i])
    #calculte runing stddev
    stddev = kepstat.running_frac_std(intime, nordata, 6.5 / 24) * 1.0e6
    #cdpp
    cdpp = stddev / math.sqrt(6.5 * 3600.0 / cadence)
    # filter cdpp
    for i in range(len(cdpp)):
        if cdpp[i] > np.median(cdpp) * 10.0:
            cdpp[i] = cdpp[i - 1]

    #calculte RMS cdpp
    rms = kepstat.rms(cdpp, np.zeros(len(stddev)))
    rs = np.random.RandomState(seed=13)
    flux1 = np.zeros(nordata.size)+ np.median(nordata)
    errors1 = rms*1e-06*np.ones_like(nordata)
    #add gaussian noise
    flux1 += errors1*rs.randn(len(nordata))
    all_time.append(intime)
Esempio n. 2
0
def kepstddev(infile,outfile,datacol,timescale,clobber,verbose,logfile,status,cmdLine=False): 

# startup parameters

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

# log the call

    hashline = '----------------------------------------------------------------------------'
    kepmsg.log(logfile,hashline,verbose)
    call = 'KEPSTDDEV -- '
    call += 'infile='+infile+' '
    call += 'outfile='+outfile+' '
    call += 'datacol='+str(datacol)+' '
    call += 'timescale='+str(timescale)+' '
    overwrite = 'n'
    if (clobber): overwrite = 'y'
    call += 'clobber='+overwrite+ ' '
    chatter = 'n'
    if (verbose): chatter = 'y'
    call += 'verbose='+chatter+' '
    call += 'logfile='+logfile
    kepmsg.log(logfile,call+'\n',verbose)

# start time

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

# test log file

    logfile = kepmsg.test(logfile)

# clobber output file

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

# open input file

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

# fudge non-compliant FITS keywords with no values

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

# read table structure

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

# filter input data table

    if status == 0:
        work1 = numpy.array([table.field('time'), table.field(datacol)])
        work1 = numpy.rot90(work1,3)
        work1 = work1[~numpy.isnan(work1).any(1)]            
 
# read table columns

    if status == 0:
        intime = work1[:,1] + bjdref
        indata = work1[:,0]

# calculate STDDEV in units of ppm

    if status == 0:
        stddev = running_frac_std(intime,indata,timescale/24) * 1.0e6
        astddev = numpy.std(indata) * 1.0e6
        cdpp = stddev / sqrt(timescale * 3600.0 / cadence)

# filter cdpp

    if status == 0:
        for i in range(len(cdpp)):
            if cdpp[i] > median(cdpp) * 10.0: cdpp[i] = cdpp[i-1]

# calculate median STDDEV

    if status == 0:
        medcdpp = ones((len(cdpp)),dtype='float32') * median(cdpp[:])
#        print '\nMedian %.1fhr standard deviation = %d ppm' % (timescale, median(stddev[:]))
        print('\nStandard deviation = %d ppm' % astddev)

# calculate median STDDEV

    if status == 0:
        medcdpp = ones((len(cdpp)),dtype='float32') * median(cdpp[:])
        print('Median %.1fhr CDPP = %d ppm' % (timescale, median(cdpp[:])))

# calculate RMS STDDEV

    if status == 0:
        rms, status = kepstat.rms(cdpp,zeros(len(stddev)),logfile,verbose)
        rmscdpp = ones((len(cdpp)),dtype='float32') * rms
        print('   RMS %.1fhr CDPP = %d ppm\n' % (timescale, rms))

# clean up x-axis unit

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

# clean up y-axis units

    if status == 0:
        pout = copy(cdpp)
        nrm = math.ceil(math.log10(median(cdpp))) - 1.0
#	pout = pout / 10**nrm
#	ylab = '%.1fhr $\sigma$ (10$^%d$ ppm)' % (timescale,nrm)
	ylab = '%.1fhr $\sigma$ (ppm)' % timescale

# data limits

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

# plot style

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

# define size of plot on monitor screen

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

# delete any fossil plots in the matplotlib window

        pylab.clf()

# position first axes inside the plotting window

        ax = pylab.axes([0.07,0.15,0.92,0.83])

# force tick labels to be absolute rather than relative

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

# rotate y labels by 90 deg

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

# plot flux vs time

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

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

	pylab.fill(ptime,pout,fc='#ffff00',linewidth=0.0,alpha=0.2)

# plot median CDPP

#        pylab.plot(intime - intime0,medcdpp / 10**nrm,color='r',linestyle='-',linewidth=2.0)
#        pylab.plot(intime - intime0,medcdpp,color='r',linestyle='-',linewidth=2.0)

# plot RMS CDPP

#        pylab.plot(intime - intime0,rmscdpp / 10**nrm,color='r',linestyle='--',linewidth=2.0)

# define plot x and y limits

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

	pylab.xlabel(xlab, {'color' : 'k'})
        pylab.ylabel(ylab, {'color' : 'k'})

# make grid on plot

	pylab.grid()

# render plot

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

# add NaNs back into data

    if status == 0:
        n = 0
        work1 = array([],dtype='float32')
        instr, status = kepio.openfits(infile,'readonly',logfile,verbose)
	table, status = kepio.readfitstab(infile,instr[1],logfile,verbose)
        for i in range(len(table.field(0))):
            if isfinite(table.field('time')[i]) and isfinite(table.field(datacol)[i]):
                work1 = append(work1,cdpp[n])
                n += 1
            else:
                work1 = append(work1,nan)

# write output file
                
    if status == 0:
        status = kepkey.new('MCDPP%d' % (timescale * 10.0),medcdpp[0],
                            'Median %.1fhr CDPP (ppm)' % timescale,
                            instr[1],outfile,logfile,verbose)
        status = kepkey.new('RCDPP%d' % (timescale * 10.0),rmscdpp[0],
                            'RMS %.1fhr CDPP (ppm)' % timescale,
                            instr[1],outfile,logfile,verbose)
        colname = 'CDPP_%d' % (timescale * 10)
	col1 = pyfits.Column(name=colname,format='E13.7',array=work1)
	cols = instr[1].data.columns + col1
	instr[1] = pyfits.new_table(cols,header=instr[1].header)
	instr.writeto(outfile)
	
# comment keyword in output file

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

# close FITS

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

# end time

    if (status == 0):
	    message = 'KEPSTDDEV completed at'
    else:
	    message = '\nKEPSTDDEV aborted at'
    kepmsg.clock(message,logfile,verbose)
Esempio n. 3
0
def GetCDPP(time, trial_lc, npoly, nsig, niter, winsize, stepsize, timescale,
            logfile, verbose, status):

    # detrend data: find limits of each time step

    if status == 0:
        npts = len(time)
        tstep1 = []
        tstep2 = []
        work = time[0]
        while work <= time[-1]:
            tstep1.append(work)
            tstep2.append(
                array([work + winsize, time[-1]], dtype='float64').min())
            work += stepsize

# detrend data: find cadence limits of each time step

    if status == 0:
        cstep1 = []
        cstep2 = []
        for n in range(len(tstep1)):
            for i in range(len(time) - 1):
                if time[i] <= tstep1[n] and time[i + 1] > tstep1[n]:
                    for j in range(i, len(time) - 1):
                        if time[j] < tstep2[n] and time[j + 1] >= tstep2[n]:
                            cstep1.append(i)
                            cstep2.append(j + 1)

# detrend data: loop over each time step, fit data, determine rms

    if status == 0:
        fitarray = zeros((npts, len(cstep1)), dtype='float32')
        fitarray[:, :] = numpy.nan
        masterfit = trial_lc * 0.0
        functype = 'poly' + str(npoly)
        for i in range(len(cstep1)):
            timeSeries = time[cstep1[i]:cstep2[i] + 1] - time[cstep1[i]]
            dataSeries = trial_lc[cstep1[i]:cstep2[i] + 1]
            pinit = [dataSeries.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,timeSeries,dataSeries,None,nsig,nsig,niter,
                                   logfile,verbose)
                fitarray[cstep1[i]:cstep2[i] + 1, i] = 0.0
                for j in range(len(coeffs)):
                    fitarray[cstep1[i]:cstep2[i] + 1,
                             i] += coeffs[j] * timeSeries**j
            except:
                for j in range(cstep1[i], cstep2[i] + 1):
                    fitarray[cstep1[i]:cstep2[i] + 1, i] = 0.0
#                message  = 'WARNING -- KEPFLATTEN: could not fit range '
#                message += str(time[cstep1[i]]) + '-' + str(time[cstep2[i]])
#                kepmsg.warn(None,message)

# detrend data: find mean fit for each timestamp

    if status == 0:
        for i in range(npts):
            masterfit[i] = nanmean(fitarray[i, :])
        masterfit[-1] = masterfit[-4]  #fudge
        masterfit[-2] = masterfit[-4]  #fudge
        masterfit[-3] = masterfit[-4]  #fudge

# detrend data: normalize light curve

    if status == 0:
        trial_lc = trial_lc / masterfit

# calculate STDDEV in units of ppm

    if status == 0:
        stddev = kepstat.running_frac_std(time, trial_lc,
                                          timescale / 24) * 1.0e6

# calculate median STDDEV

    if status == 0:
        medstddev = ones((len(stddev)), dtype='float32') * median(stddev)
#        print '\nMedian %.1fhr STDDEV = %d ppm' % (timescale, median(stddev))

    return median(stddev), stddev, status
Esempio n. 4
0
def kepstddev(infile,outfile,datacol,timescale,clobber,verbose,logfile,status,cmdLine=False): 

# startup parameters

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

# log the call

    hashline = '----------------------------------------------------------------------------'
    kepmsg.log(logfile,hashline,verbose)
    call = 'KEPSTDDEV -- '
    call += 'infile='+infile+' '
    call += 'outfile='+outfile+' '
    call += 'datacol='+str(datacol)+' '
    call += 'timescale='+str(timescale)+' '
    overwrite = 'n'
    if (clobber): overwrite = 'y'
    call += 'clobber='+overwrite+ ' '
    chatter = 'n'
    if (verbose): chatter = 'y'
    call += 'verbose='+chatter+' '
    call += 'logfile='+logfile
    kepmsg.log(logfile,call+'\n',verbose)

# start time

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

# test log file

    logfile = kepmsg.test(logfile)

# clobber output file

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

# open input file

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

# fudge non-compliant FITS keywords with no values

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

# read table structure

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

# filter input data table

    if status == 0:
        work1 = numpy.array([table.field('time'), table.field(datacol)])
        work1 = numpy.rot90(work1,3)
        work1 = work1[~numpy.isnan(work1).any(1)]            
 
# read table columns

    if status == 0:
        intime = work1[:,1] + bjdref
        indata = work1[:,0]

# calculate STDDEV in units of ppm

    if status == 0:
        stddev = running_frac_std(intime,indata,timescale/24) * 1.0e6
        astddev = numpy.std(indata) * 1.0e6
        cdpp = stddev / sqrt(timescale * 3600.0 / cadence)

# filter cdpp

    if status == 0:
        for i in range(len(cdpp)):
            if cdpp[i] > median(cdpp) * 10.0: cdpp[i] = cdpp[i-1]

# calculate median STDDEV

    if status == 0:
        medcdpp = ones((len(cdpp)),dtype='float32') * median(cdpp[:])
#        print '\nMedian %.1fhr standard deviation = %d ppm' % (timescale, median(stddev[:]))
        print '\nStandard deviation = %d ppm' % astddev

# calculate median STDDEV

    if status == 0:
        medcdpp = ones((len(cdpp)),dtype='float32') * median(cdpp[:])
        print 'Median %.1fhr CDPP = %d ppm' % (timescale, median(cdpp[:]))

# calculate RMS STDDEV

    if status == 0:
        rms, status = kepstat.rms(cdpp,zeros(len(stddev)),logfile,verbose)
        rmscdpp = ones((len(cdpp)),dtype='float32') * rms
        print '   RMS %.1fhr CDPP = %d ppm\n' % (timescale, rms)

# clean up x-axis unit

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

# clean up y-axis units

    if status == 0:
        pout = copy(cdpp)
        nrm = math.ceil(math.log10(median(cdpp))) - 1.0
#	pout = pout / 10**nrm
#	ylab = '%.1fhr $\sigma$ (10$^%d$ ppm)' % (timescale,nrm)
	ylab = '%.1fhr $\sigma$ (ppm)' % timescale

# data limits

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

# plot style

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

# define size of plot on monitor screen

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

# delete any fossil plots in the matplotlib window

        pylab.clf()

# position first axes inside the plotting window

        ax = pylab.axes([0.07,0.15,0.92,0.83])

# force tick labels to be absolute rather than relative

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

# rotate y labels by 90 deg

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

# plot flux vs time

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

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

	pylab.fill(ptime,pout,fc='#ffff00',linewidth=0.0,alpha=0.2)

# plot median CDPP

#        pylab.plot(intime - intime0,medcdpp / 10**nrm,color='r',linestyle='-',linewidth=2.0)
#        pylab.plot(intime - intime0,medcdpp,color='r',linestyle='-',linewidth=2.0)

# plot RMS CDPP

#        pylab.plot(intime - intime0,rmscdpp / 10**nrm,color='r',linestyle='--',linewidth=2.0)

# define plot x and y limits

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

	pylab.xlabel(xlab, {'color' : 'k'})
        pylab.ylabel(ylab, {'color' : 'k'})

# make grid on plot

	pylab.grid()

# render plot

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

# add NaNs back into data

    if status == 0:
        n = 0
        work1 = array([],dtype='float32')
        instr, status = kepio.openfits(infile,'readonly',logfile,verbose)
	table, status = kepio.readfitstab(infile,instr[1],logfile,verbose)
        for i in range(len(table.field(0))):
            if isfinite(table.field('time')[i]) and isfinite(table.field(datacol)[i]):
                work1 = append(work1,cdpp[n])
                n += 1
            else:
                work1 = append(work1,nan)

# write output file
                
    if status == 0:
        status = kepkey.new('MCDPP%d' % (timescale * 10.0),medcdpp[0],
                            'Median %.1fhr CDPP (ppm)' % timescale,
                            instr[1],outfile,logfile,verbose)
        status = kepkey.new('RCDPP%d' % (timescale * 10.0),rmscdpp[0],
                            'RMS %.1fhr CDPP (ppm)' % timescale,
                            instr[1],outfile,logfile,verbose)
        colname = 'CDPP_%d' % (timescale * 10)
	col1 = pyfits.Column(name=colname,format='E13.7',array=work1)
	cols = instr[1].data.columns + col1
	instr[1] = pyfits.new_table(cols,header=instr[1].header)
	instr.writeto(outfile)
	
# comment keyword in output file

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

# close FITS

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

# end time

    if (status == 0):
	    message = 'KEPSTDDEV completed at'
    else:
	    message = '\nKEPSTDDEV aborted at'
    kepmsg.clock(message,logfile,verbose)
Esempio n. 5
0
def rmsestimation(quarter, timescale):
    """
    do stastitical analysis of rms cdpp(expected snr) of certain quarter
    """
    #read data from keplerstellar catalog and match lightcurve in lc database
    cata_path = '../catalog/cumulative.csv'
    data_path = '/scratch/kepler_data/'
    kepid = kepio.get_id(cata_path)
    all_rms = []
    own_kid = []

    for i, k_id in enumerate(kepid[:100]):
        #print('This is '+str(kepid[i]))
        #lc path here
        file_dir = kepio.pathfinder(k_id, data_path, quarter)
        try:
            filename = glob.glob(file_dir)
            name = filename[0]
            #open file and read time keys
            instr = fits.open(name)
            tstart, tstop, bjdref, cadence = kepio.timekeys(instr, filename)

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

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

            #flatten the data array
            intime = np.concatenate(intime).ravel()
            indata = np.concatenate(indata).ravel()
            spline = np.concatenate(spline).ravel()
            #normalized flux using spline
            nordata = indata / spline
            #calculte runing stddev
            stddev = kepstat.running_frac_std(intime, nordata,
                                              timescale / 24) * 1.0e6
            #cdpp
            cdpp = stddev / math.sqrt(timescale * 3600.0 / cadence)
            # filter cdpp
            for i in range(len(cdpp)):
                if cdpp[i] > np.median(cdpp) * 10.0:
                    cdpp[i] = cdpp[i - 1]

            #calculte RMS cdpp
            rms = kepstat.rms(cdpp, np.zeros(len(stddev)))
            rmscdpp = np.ones((len(cdpp)), dtype='float32') * rms
            if rms > 200:
                plt.figure(figsize=(10, 8))
                #plt.hist(cdpp, bins =25, color = 'gold', fill = True, edgecolor = 'black', linewidth = 2.0)
                #plt.ylabel('Count')
                #plt.xlabel('CDPP/6.5hrs(ppm)')
                #plt.savefig("./test/KOIQ"+str(quarter)+str(k_id)+"cdpp.png")
                plt.scatter(intime, nordata, marker='.')
                plt.savefig("./test/KOIQ" + str(quarter) + str(k_id) +
                            "display_lc.png")
            # print('%d has RMS %.1fhr CDPP = %d ppm\n' % (k_id,timescale, rms))
            all_rms.append(rms)
            own_kid.append(k_id)

        except IndexError:
            pass

    c, low, upp = sigmaclip(all_rms, 3, 3)
    plt.figure(figsize=(10, 8))
    plt.hist(c,
             bins=25,
             range=(low, upp),
             fill=True,
             edgecolor='black',
             linewidth=2.0)
    plt.ylabel('Count')
    plt.xlabel('RMSCDPP(ppm)')
    #plt.savefig("./result/KOIQ"+str(quarter)+"rms.png")
    result = np.transpose([own_kid, all_rms])
Esempio n. 6
0
def GetCDPP(time,trial_lc,npoly,nsig,niter,winsize,stepsize,timescale,logfile,verbose,status):

# detrend data: find limits of each time step

    if status == 0:
        npts = len(time)
        tstep1 = []; tstep2 = []
        work = time[0]
        while work <= time[-1]:
            tstep1.append(work)
            tstep2.append(array([work+winsize,time[-1]],dtype='float64').min())
            work += stepsize

# detrend data: find cadence limits of each time step

    if status == 0:
        cstep1 = []; cstep2 = []
        for n in range(len(tstep1)):
            for i in range(len(time)-1):
                if time[i] <= tstep1[n] and time[i+1] > tstep1[n]:
                    for j in range(i,len(time)-1):
                        if time[j] < tstep2[n] and time[j+1] >= tstep2[n]:
                            cstep1.append(i)
                            cstep2.append(j+1)

# detrend data: loop over each time step, fit data, determine rms

    if status == 0:
        fitarray = zeros((npts,len(cstep1)),dtype='float32')
        fitarray[:,:] = numpy.nan
        masterfit = trial_lc * 0.0
        functype = 'poly' + str(npoly)
        for i in range(len(cstep1)):
            timeSeries = time[cstep1[i]:cstep2[i]+1]-time[cstep1[i]]
            dataSeries = trial_lc[cstep1[i]:cstep2[i]+1]
            pinit = [dataSeries.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,timeSeries,dataSeries,None,nsig,nsig,niter,
                                   logfile,verbose)
                fitarray[cstep1[i]:cstep2[i]+1,i] = 0.0
                for j in range(len(coeffs)):
                    fitarray[cstep1[i]:cstep2[i]+1,i] += coeffs[j] * timeSeries**j
            except:
                for j in range(cstep1[i],cstep2[i]+1):
                    fitarray[cstep1[i]:cstep2[i]+1,i] = 0.0
#                message  = 'WARNING -- KEPFLATTEN: could not fit range '
#                message += str(time[cstep1[i]]) + '-' + str(time[cstep2[i]])
#                kepmsg.warn(None,message)

# detrend data: find mean fit for each timestamp

    if status == 0:
        for i in range(npts):
            masterfit[i] = nanmean(fitarray[i,:])
        masterfit[-1] = masterfit[-4] #fudge
        masterfit[-2] = masterfit[-4] #fudge
        masterfit[-3] = masterfit[-4] #fudge

# detrend data: normalize light curve

    if status == 0:
        trial_lc = trial_lc / masterfit

# calculate STDDEV in units of ppm

    if status == 0:
        stddev = kepstat.running_frac_std(time,trial_lc,timescale/24) * 1.0e6

# calculate median STDDEV

    if status == 0:
        medstddev = ones((len(stddev)),dtype='float32') * median(stddev)
#        print '\nMedian %.1fhr STDDEV = %d ppm' % (timescale, median(stddev))

    return median(stddev), stddev, status