コード例 #1
0
ファイル: check.py プロジェクト: Li-Yangyang/DLKepler
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)
    all_flux.append(flux1)
    instr.close()
t = np.concatenate(all_time)
simflux = np.concatenate(all_flux)
print(t.shape, simflux.shape)
コード例 #2
0
ファイル: kepstddev.py プロジェクト: rodluger/PyKE
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)
コード例 #3
0
ファイル: kepfit.py プロジェクト: Sleekery/PyKE
def leastsquare(functype,pinit,xdata,ydata,yerr,logfile,verbose):

    status = 0
    coeffs = []

# functional form

    if (functype == 'poly0'): fitfunc = kepfunc.poly0()
    if (functype == 'poly1'): fitfunc = kepfunc.poly1()
    if (functype == 'poly2'): fitfunc = kepfunc.poly2()
    if (functype == 'poly3'): fitfunc = kepfunc.poly3()
    if (functype == 'poly4'): fitfunc = kepfunc.poly4()
    if (functype == 'poly5'): fitfunc = kepfunc.poly5()
    if (functype == 'poly6'): fitfunc = kepfunc.poly6()
    if (functype == 'poly7'): fitfunc = kepfunc.poly7()
    if (functype == 'poly8'): fitfunc = kepfunc.poly8()
    if (functype == 'poly1con'): fitfunc = kepfunc.poly1con()
    if (functype == 'gauss'): fitfunc = kepfunc.gauss()
    if (functype == 'gauss0'): fitfunc = kepfunc.gauss0()
    if (functype == 'congauss'): fitfunc = kepfunc.congauss()
    if (functype == 'sine'): fitfunc = kepfunc.sine()
    if (functype == 'moffat0'): fitfunc = kepfunc.moffat0()
    if (functype == 'conmoffat'): fitfunc = kepfunc.conmoffat()

# define error coefficent calculation

    errfunc = lambda p, x, y, err: (y - fitfunc(p, x)) / err

# if no data errors, substitude rms of fit

    if (yerr == None):
	yerr = []
	rerr = []
	for i in range(len(ydata)):
	    rerr.append(1.e10)
	try:
            out = optimize.leastsq(errfunc,pinit,args=(xdata,ydata,rerr),full_output=1)
	except:
	    message = 'ERROR -- KEPFIT.LEASTSQUARE: failed to fit data'
	    status = kepmsg.err(logfile,message,verbose)
            if functype == 'poly0':
                out = [numpy.mean(ydata),sqrt(numpy.mean(ydata))]
	if (functype == 'poly0' or functype == 'sineCompareBinPSF'):
	    coeffs.append(out[0])
	else:
	    coeffs = out[0]
	if (len(coeffs) > 1):
	    fit = fitfunc(coeffs,xdata)
	else:
	    fit = numpy.zeros(len(xdata))
	    for i in range(len(fit)):
		fit[i] = coeffs[0]
	sigma, status = kepstat.rms(ydata,fit,logfile,verbose)
	for i in range(len(ydata)):
	    yerr.append(sigma)

# fit data 

    try:
        out = optimize.leastsq(errfunc, pinit, args=(xdata, ydata, yerr), full_output=1)
    except:
	message = 'ERROR -- KEPFIT.LEASTSQUARE: failed to fit data'
	status = kepmsg.err(logfile,message,verbose)
        if functype == 'poly0':
            out = [numpy.mean(ydata),sqrt(numpy.mean(ydata))]

# define coefficients

    coeffs = []
    covar = []
    if (functype == 'poly0' or functype == 'poly1con' or 
	functype == 'sineCompareBinPSF'):
	coeffs.append(out[0])
	covar.append(out[1])
    else:
	coeffs = out[0]
	covar = out[1]

# calculate 1-sigma error on coefficients

    errors = []
    if (covar == None): 
	message = 'WARNING -- KEPFIT.leastsquare: NULL covariance matrix'
#	kepmsg.log(logfile,message,verbose)
    for i in range(len(coeffs)):
	if (covar != None and len(coeffs) > 1):
	    errors.append(sqrt(covar[i][i]))
	else:
	    errors.append(coeffs[i])

# generate fit points for rms calculation

    if (len(coeffs) > 1):
	fit = fitfunc(coeffs,xdata)
    else:
	fit = numpy.zeros(len(xdata))
	for i in range(len(fit)):
	    fit[i] = coeffs[0]
    sigma, status = kepstat.rms(ydata,fit,logfile,verbose)

# generate fit points for plotting

    dx = xdata[len(xdata)-1] - xdata[0]
    plotx = linspace(xdata.min(),xdata.max(),10000)
    ploty = fitfunc(coeffs,plotx)
    if (len(coeffs) == 1):
	ploty = []
	for i in range(len(plotx)):
	    ploty.append(coeffs[0])
	ploty = numpy.array(ploty)

# reduced chi^2 calculation

    chi2 = 0
    dof = len(ydata) - len(coeffs)
    for i in range(len(ydata)):
	chi2 += (ydata[i] - fit[i])**2 / yerr[i]
    chi2 /= dof

    return coeffs, errors, covar, sigma, chi2, dof, fit, plotx, ploty, status
コード例 #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)
コード例 #5
0
ファイル: rmsestimation.py プロジェクト: Li-Yangyang/DLKepler
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])
コード例 #6
0
def leastsquare(functype, pinit, xdata, ydata, yerr, logfile, verbose):

    status = 0
    coeffs = []

    # functional form

    if (functype == 'poly0'): fitfunc = kepfunc.poly0()
    if (functype == 'poly1'): fitfunc = kepfunc.poly1()
    if (functype == 'poly2'): fitfunc = kepfunc.poly2()
    if (functype == 'poly3'): fitfunc = kepfunc.poly3()
    if (functype == 'poly4'): fitfunc = kepfunc.poly4()
    if (functype == 'poly5'): fitfunc = kepfunc.poly5()
    if (functype == 'poly6'): fitfunc = kepfunc.poly6()
    if (functype == 'poly7'): fitfunc = kepfunc.poly7()
    if (functype == 'poly8'): fitfunc = kepfunc.poly8()
    if (functype == 'poly1con'): fitfunc = kepfunc.poly1con()
    if (functype == 'gauss'): fitfunc = kepfunc.gauss()
    if (functype == 'gauss0'): fitfunc = kepfunc.gauss0()
    if (functype == 'congauss'): fitfunc = kepfunc.congauss()
    if (functype == 'sine'): fitfunc = kepfunc.sine()
    if (functype == 'moffat0'): fitfunc = kepfunc.moffat0()
    if (functype == 'conmoffat'): fitfunc = kepfunc.conmoffat()

    # define error coefficent calculation

    errfunc = lambda p, x, y, err: (y - fitfunc(p, x)) / err

    # if no data errors, substitude rms of fit

    if (yerr == None):
        yerr = []
        rerr = []
        for i in range(len(ydata)):
            rerr.append(1.e10)
        try:
            out = optimize.leastsq(errfunc,
                                   pinit,
                                   args=(xdata, ydata, rerr),
                                   full_output=1)
        except:
            message = 'ERROR -- KEPFIT.LEASTSQUARE: failed to fit data'
            status = kepmsg.err(logfile, message, verbose)
            if functype == 'poly0':
                out = [numpy.mean(ydata), sqrt(numpy.mean(ydata))]
        if (functype == 'poly0' or functype == 'sineCompareBinPSF'):
            coeffs.append(out[0])
        else:
            coeffs = out[0]
        if (len(coeffs) > 1):
            fit = fitfunc(coeffs, xdata)
        else:
            fit = numpy.zeros(len(xdata))
            for i in range(len(fit)):
                fit[i] = coeffs[0]
        sigma, status = kepstat.rms(ydata, fit, logfile, verbose)
        for i in range(len(ydata)):
            yerr.append(sigma)

# fit data

    try:
        out = optimize.leastsq(errfunc,
                               pinit,
                               args=(xdata, ydata, yerr),
                               full_output=1)
    except:
        message = 'ERROR -- KEPFIT.LEASTSQUARE: failed to fit data'
        status = kepmsg.err(logfile, message, verbose)
        if functype == 'poly0':
            out = [numpy.mean(ydata), sqrt(numpy.mean(ydata))]

# define coefficients

    coeffs = []
    covar = []
    if (functype == 'poly0' or functype == 'poly1con'
            or functype == 'sineCompareBinPSF'):
        coeffs.append(out[0])
        covar.append(out[1])
    else:
        coeffs = out[0]
        covar = out[1]

# calculate 1-sigma error on coefficients

    errors = []
    if (covar == None):
        message = 'WARNING -- KEPFIT.leastsquare: NULL covariance matrix'
#        kepmsg.log(logfile,message,verbose)
    for i in range(len(coeffs)):
        if (covar != None and len(coeffs) > 1):
            errors.append(sqrt(abs(covar[i][i])))
        else:
            errors.append(coeffs[i])

# generate fit points for rms calculation

    if (len(coeffs) > 1):
        fit = fitfunc(coeffs, xdata)
    else:
        fit = numpy.zeros(len(xdata))
        for i in range(len(fit)):
            fit[i] = coeffs[0]
    sigma, status = kepstat.rms(ydata, fit, logfile, verbose)

    # generate fit points for plotting

    dx = xdata[len(xdata) - 1] - xdata[0]
    plotx = linspace(xdata.min(), xdata.max(), 10000)
    ploty = fitfunc(coeffs, plotx)
    if (len(coeffs) == 1):
        ploty = []
        for i in range(len(plotx)):
            ploty.append(coeffs[0])
        ploty = numpy.array(ploty)

# reduced chi^2 calculation

    chi2 = 0
    dof = len(ydata) - len(coeffs)
    for i in range(len(ydata)):
        chi2 += (ydata[i] - fit[i])**2 / yerr[i]
    chi2 /= dof

    return coeffs, errors, covar, sigma, chi2, dof, fit, plotx, ploty, status