def get_lc(time1, fluxarr1, quality1, n_chunks, bg_cut, flatlc_window, smooth_window): time, lc, xbar, ybar, regnum, lab = optimalAperture(time1, fluxarr1, quality1, qual_cut=False, return_qual=False, toss_resat=False, bg_cut=bg_cut, skip=None) m1 = np.isfinite(lc) * np.isfinite(lc) time = time1[m1] lc = lc[m1] xbar = xbar[m1] ybar = ybar[m1] flatlc = extract_lc.medfilt(time, lc, window=flatlc_window) cadstep = np.int(np.floor(len(time) / n_chunks)) #600 zpt = len(time) % cadstep if zpt == cadstep: zpt = 0 outflux, correction, thr_cad = extract_lc.run_C0_detrend(time, flatlc, xbar, ybar, cadstep=cadstep, skip=None) not_thr = ~thr_cad corflux = (lc[zpt:][not_thr] / np.median(lc[zpt:][not_thr]) / correction[not_thr]) corflatflux = (flatlc[zpt:][not_thr] / np.median(flatlc[zpt:][not_thr]) / correction[not_thr]) # The 1.4826 and *4 factors make this similar to a 4-sigma cut. mad_cut = 1.4826 * MAD(corflatflux - 1.) * 4.0 keep = np.abs(corflatflux - 1.) < mad_cut # this might not be used m2 = np.ones_like(corflatflux, dtype=bool) #corflatflux < 1.1 t1 = time[zpt:][not_thr][m2] cfflux = extract_lc.medfilt(t1, corflux, window=smooth_window) - 1.0 return time, lc, xbar, ybar, t1, corflux, cfflux
def K2_DetrendRev2(fn,): #DANCe = fn.split('/')[-1].split('_')[0] time, flux, xbar, ybar = np.genfromtxt(fn, unpack = True) m1 = np.isfinite(flux) time = time[m1] flux = flux[m1] xbar = xbar[m1] ybar = ybar[m1] flatlc = extract_lc.medfilt(time,flux,window=3) zpt = len(time)%300 outflux, correction, thr_cad = extract_lc.run_C0_detrend( time, flatlc, xbar, ybar, cadstep=300, skip=1828) not_thr = ~thr_cad corflux = (flux[zpt:][not_thr]/ np.median(flux[zpt:][not_thr])/ correction[not_thr]) corflatflux = (flatlc[zpt:][not_thr]/ np.median(flatlc[zpt:][not_thr])/ correction[not_thr]) mad_cut = 1.4826*MAD(corflatflux-1.)*4 keep = np.abs(corflatflux-1.) < mad_cut #plt.plot(time[zpt:][not_thr],corflatflux,c='r') #plt.plot(time[zpt:][not_thr][keep],corflatflux[keep],c='b') #CDPP Calculations #thr = twohr_cdpp(flux) #shr = sixhr_cdpp(flux) #plt.plot(time,flatlc) #plt.figure(figsize=[15,6]) #plt.plot(time,flux/np.median(flux),'bo',markersize=1) #plt.plot(time[zpt:][not_thr][keep],corflux[keep],marker='.') #plt.xlabel('Time [d]') #plt.ylabel('Normalized Flux') #plt.title(str(DANCe) + ' 6.5_Hr_CDPP_' + str(shr) + ' | 2.5_Hr_CCPD_' + str(thr)) #plt.savefig(fn.split('.')[-1] + '.png') #plt.show() #plt.close('all') compiled_table = astropy.table.Table() compiled_table['time'] = time[zpt:][not_thr] #('time', time[zpt:][not_thr]) compiled_table['corflatflux'] = corflatflux compiled_table['corflux'] = corflux compiled_table['keep'] = keep compiled_table['flux'] = flux[zpt:][not_thr] compiled_table['xbar'] = xbar[zpt:][not_thr] compiled_table['ybar'] = ybar[zpt:][not_thr] compiled_table['flatflux'] = flatlc[zpt:][not_thr] DANCe = fn.split('/')[-1].split('.')[-1] np.savetxt('/Users/bryanmann/Documents/NASA_Kepler_2.0/All_Light_Curves/Lightcurves_RADec_ap2.0_BJM/Detrended_Data/' + DANCe, compiled_tbl, fmt='ascii', delimiter=',')
def K2_DetrendRev4(fn, ): time, flux, xbar, ybar = np.genfromtxt(fn, unpack=True) m1 = np.isfinite(flux) time = time[m1] flux = flux[m1] xbar = xbar[m1] ybar = ybar[m1] flatlc = extract_lc.medfilt(time, flux, window=3) zpt = len(time) % 300 outflux, correction, thr_cad = extract_lc.run_C0_detrend(time, flatlc, xbar, ybar, cadstep=300, skip=1828) not_thr = ~thr_cad corflux = (flux[zpt:][not_thr] / np.median(flux[zpt:][not_thr]) / correction[not_thr]) corflatflux = (flatlc[zpt:][not_thr] / np.median(flatlc[zpt:][not_thr]) / correction[not_thr]) mad_cut = 1.4826 * MAD(corflatflux - 1.) * 4 keep = np.abs(corflatflux - 1.) < mad_cut # Adds the detrended data to an ascii table. # To use this in conjunction with the ACF included in this package, you must # comment out corflatflux, keep, flux, and flatflux, then run the script on # your data set. compiled_table = astropy.table.Table() compiled_table['time'] = time[zpt:][not_thr] compiled_table['corflatflux'] = corflatflux compiled_table['corflux'] = corflux compiled_table['keep'] = keep compiled_table['flux'] = flux[zpt:][not_thr] compiled_table['flatflux'] = flatlc[zpt:][not_thr] compiled_table['xbar'] = xbar[zpt:][not_thr] compiled_table['ybar'] = ybar[zpt:][not_thr] # Generates the DANCe # for the file title. DANCe = fn.split('/')[-1].split('.')[0] # Saves the detrended data to a file. Need to update the path to the correct folder. np.savetxt( '/Users/bryanmann/Documents/NASA_Kepler_2.0/All_Light_Curves/Lightcurves_RADec_ap2.0_BJM/Detrended_Data/Detrended_ACF_Data/Raw_Detrended_Data_' + DANCe + '.0.dat', compiled_table, delimiter=',')
def K2_DetrendRev4(fn,): time, flux, xbar, ybar = np.genfromtxt(fn, unpack=True) m1 = np.isfinite(flux) time = time[m1] flux = flux[m1] xbar = xbar[m1] ybar = ybar[m1] flatlc = extract_lc.medfilt(time, flux, window=3) zpt = len(time) % 300 outflux, correction, thr_cad = extract_lc.run_C0_detrend(time, flatlc, xbar, ybar, cadstep=300, skip=1828) not_thr = ~thr_cad corflux = flux[zpt:][not_thr] / np.median(flux[zpt:][not_thr]) / correction[not_thr] corflatflux = flatlc[zpt:][not_thr] / np.median(flatlc[zpt:][not_thr]) / correction[not_thr] mad_cut = 1.4826 * MAD(corflatflux - 1.0) * 4 keep = np.abs(corflatflux - 1.0) < mad_cut # Adds the detrended data to an ascii table. # To use this in conjunction with the ACF included in this package, you must # comment out corflatflux, keep, flux, and flatflux, then run the script on # your data set. compiled_table = astropy.table.Table() compiled_table["time"] = time[zpt:][not_thr] compiled_table["corflatflux"] = corflatflux compiled_table["corflux"] = corflux compiled_table["keep"] = keep compiled_table["flux"] = flux[zpt:][not_thr] compiled_table["flatflux"] = flatlc[zpt:][not_thr] compiled_table["xbar"] = xbar[zpt:][not_thr] compiled_table["ybar"] = ybar[zpt:][not_thr] # Generates the DANCe # for the file title. DANCe = fn.split("/")[-1].split(".")[0] # Saves the detrended data to a file. Need to update the path to the correct folder. np.savetxt( "/Users/bryanmann/Documents/NASA_Kepler_2.0/All_Light_Curves/Lightcurves_RADec_ap2.0_BJM/Detrended_Data/Detrended_ACF_Data/Raw_Detrended_Data_" + DANCe + ".0.dat", compiled_table, delimiter=",", )
def K2_Detrend(fn,): DANCe = fn.split('/')[-1].split('_')[0] time, flux, xbar, ybar = np.genfromtxt(fn, unpack = True) m1 = np.isfinite(flux) time = time[m1] flux = flux[m1] xbar = xbar[m1] ybar = ybar[m1] flatlc = extract_lc.medfilt(time,flux,window=3) zpt = len(time)%300 outflux, correction, thr_cad = extract_lc.run_C0_detrend( time, flatlc, xbar, ybar, cadstep=300, skip=1828) not_thr = ~thr_cad corflux = (flux[zpt:][not_thr]/ np.median(flux[zpt:][not_thr])/ correction[not_thr]) corflatflux = (flatlc[zpt:][not_thr]/ np.median(flatlc[zpt:][not_thr])/ correction[not_thr]) mad_cut = 1.4826*MAD(corflatflux-1.)*4 keep = np.abs(corflatflux-1.) < mad_cut plt.plot(time[zpt:][not_thr],corflatflux,c='r') plt.plot(time[zpt:][not_thr][keep],corflatflux[keep],c='b') #CDPP Calculations #thr = twohr_cdpp(flux) #shr = sixhr_cdpp(flux) plt.plot(time,flatlc) plt.figure(figsize=[15,6]) plt.plot(time,flux/np.median(flux),'bo',markersize=1) plt.plot(time[zpt:][not_thr][keep],corflux[keep],marker='.') plt.xlabel('Time [d]') plt.ylabel('Normalized Flux') #plt.title(str(DANCe) + ' 6.5_Hr_CDPP_' + str(shr) + ' | 2.5_Hr_CCPD_' + str(thr)) #plt.savefig(fn.split('.')[-1] + '.png') plt.show() #plt.close('all')
def K2_Detrend(fn, ): DANCe = fn.split('/')[-1].split('_')[0] time, flux, xbar, ybar = np.genfromtxt(fn, unpack=True) m1 = np.isfinite(flux) time = time[m1] flux = flux[m1] xbar = xbar[m1] ybar = ybar[m1] flatlc = extract_lc.medfilt(time, flux, window=3) zpt = len(time) % 300 outflux, correction, thr_cad = extract_lc.run_C0_detrend(time, flatlc, xbar, ybar, cadstep=300, skip=1828) not_thr = ~thr_cad corflux = (flux[zpt:][not_thr] / np.median(flux[zpt:][not_thr]) / correction[not_thr]) corflatflux = (flatlc[zpt:][not_thr] / np.median(flatlc[zpt:][not_thr]) / correction[not_thr]) mad_cut = 1.4826 * MAD(corflatflux - 1.) * 4 keep = np.abs(corflatflux - 1.) < mad_cut plt.plot(time[zpt:][not_thr], corflatflux, c='r') plt.plot(time[zpt:][not_thr][keep], corflatflux[keep], c='b') #CDPP Calculations #thr = twohr_cdpp(flux) #shr = sixhr_cdpp(flux) plt.plot(time, flatlc) plt.figure(figsize=[15, 6]) plt.plot(time, flux / np.median(flux), 'bo', markersize=1) plt.plot(time[zpt:][not_thr][keep], corflux[keep], marker='.') plt.xlabel('Time [d]') plt.ylabel('Normalized Flux') #plt.title(str(DANCe) + ' 6.5_Hr_CDPP_' + str(shr) + ' | 2.5_Hr_CCPD_' + str(thr)) #plt.savefig(fn.split('.')[-1] + '.png') plt.show()
def K2_DetrendRev3(fn,): time, flux, xbar, ybar = np.genfromtxt(fn, unpack = True) m1 = np.isfinite(flux) time = time[m1] flux = flux[m1] xbar = xbar[m1] ybar = ybar[m1] flatlc = extract_lc.medfilt(time,flux,window=3) zpt = len(time)%300 outflux, correction, thr_cad = extract_lc.run_C0_detrend( time, flatlc, xbar, ybar, cadstep=300, skip=1828) not_thr = ~thr_cad corflux = (flux[zpt:][not_thr]/ np.median(flux[zpt:][not_thr])/ correction[not_thr]) corflatflux = (flatlc[zpt:][not_thr]/ np.median(flatlc[zpt:][not_thr])/ correction[not_thr]) mad_cut = 1.4826*MAD(corflatflux-1.)*4 keep = np.abs(corflatflux-1.) < mad_cut # Adds the detrended data to an ascii table. compiled_table = astropy.table.Table() compiled_table['time'] = time[zpt:][not_thr] #compiled_table['corflatflux'] = corflatflux compiled_table['corflux'] = corflux #compiled_table['keep'] = keep #compiled_table['flux'] = flux[zpt:][not_thr] #compiled_table['flatflux'] = flatlc[zpt:][not_thr] compiled_table['xbar'] = xbar[zpt:][not_thr] compiled_table['ybar'] = ybar[zpt:][not_thr] # Generates the DANCe # for the file title. DANCe = fn.split('/')[-1].split('.')[0] # Saves the detrended data to a file. Need to update the path to the correct folder. np.savetxt('/Users/bryanmann/Documents/NASA_Kepler_2.0/All_Light_Curves/Lightcurves_RADec_ap2.0_BJM/Detrended_Data/Detrended_ACF_Data/Raw_Detrended_Data' + DANCe + '.0.dat', compiled_table, delimiter=',')
for i, filename in enumerate(filenames): #print('{} of {}'.format(i,len(filenames))) bar.update(i + 1) time, lc, xbar, ybar, regnum = extract_lc.run_C0_data_extract(filename, bg_cut=4) if regnum == 0: continue #mask = np.ones(len(lc),dtype=bool) #mask[range(11,len(lc),12)] = False #time,lc,xbar,ybar = time[mask], lc[mask], xbar[mask], ybar[mask] #lets flatten the light curve flatlc = extract_lc.medfilt(time, lc, window=1.) zpt = len(time) % 300. try: outflux, correction, thr_cad = extract_lc.run_C0_detrend( time, flatlc, xbar, ybar, cadstep=300) except ValueError: print('{} had an ValueError'.format(filename)) continue not_thr = ~thr_cad corflux = (lc[zpt:][not_thr] / np.median(lc[zpt:][not_thr]) / correction[not_thr]) corflatflux = (flatlc[zpt:][not_thr] / np.median(flatlc[zpt:][not_thr]) / correction[not_thr])
def K2_DetrendRev4(filename, ap=4.0): lcs = ascii.read(filename) outfilename = filename.split("/")[-1][:-4] logging.info(outfilename) time = lcs["t"] x_pos = lcs["x"] y_pos = lcs["y"] qual_flux = np.zeros_like(time) unc_flux = np.ones_like(time) best_col = "flux_{0:.1f}".format(ap) flux = lcs[best_col] m1 = np.isfinite(flux) time = time[m1] flux = flux[m1] xbar = x_pos[m1] ybar = y_pos[m1] flatlc = extract_lc.medfilt(time,flux,window=3) n_chunks = 6 cadstep = np.int(np.floor(len(time) / n_chunks)) #600 zpt = len(time) % cadstep if zpt==cadstep: zpt = 0 logging.info("%d pts %d chunks step=%d zpt=%d ap=%f ", len(time), n_chunks, cadstep, zpt, ap) outflux, correction, thr_cad = extract_lc.run_C0_detrend( time, flatlc, xbar, ybar, cadstep=cadstep, skip=None) not_thr = ~thr_cad corflux = (flux[zpt:][not_thr]/ np.median(flux[zpt:][not_thr])/ correction[not_thr]) corflatflux = (flatlc[zpt:][not_thr]/ np.median(flatlc[zpt:][not_thr])/ correction[not_thr]) # The 1.4826 and *4 factors make this similar to a 4-sigma cut. mad_cut = 1.4826*MAD(corflatflux-1.)*4 keep = np.abs(corflatflux-1.) < mad_cut # Adds the detrended data to an ascii table. newtable = {'Dates': time[zpt:][not_thr], 'Flux': flux[zpt:][not_thr], 'Corrflux': corflux, 'Xpos': xbar[zpt:][not_thr], 'Ypos': ybar[zpt:][not_thr]} ascii.write(newtable, "output_lcs/{0}_detrended.dat".format(outfilename), names=['Dates','Flux', 'Corrflux','Xpos','Ypos']) # Create some plots # Compare to my outputs sd_lc = ascii.read("../k2spin/output_lcs/{0}_lcs.csv".format(outfilename)) # Raw Lightcurve plt.clf() plt.subplot(211) plt.plot(sd_lc["t"], sd_lc["raw"]/np.median(sd_lc["raw"]), 'c.') plt.plot(time[zpt:][not_thr], flux[zpt:][not_thr]/np.median(flux[zpt:][not_thr]), 'bo', markersize=2) plt.xlabel('Time [d]') plt.ylabel('Flux/Median flux') plt.ylim((np.median(flux[zpt:][not_thr]/np.median(flux[zpt:][not_thr]))-4.5*np.std(flux[zpt:][not_thr]/np.median(flux[zpt:][not_thr])),np.median(flux[zpt:][not_thr]/np.median(flux[zpt:][not_thr]))+ 4.5*np.std(flux[zpt:][not_thr]/np.median(flux[zpt:][not_thr])))) # Corrected lightcurve plt.subplot(212) plt.plot(time[zpt:][not_thr], corflux/np.median(corflux), 'bo', markersize=2) plt.xlabel('Time [d]') plt.ylabel('Flux/Median flux') plt.ylim((np.median(corflux/np.median(corflux))-4.5*np.std(corflux/np.median(corflux)),np.median(corflux/np.median(corflux))+4.5*np.std(corflux/np.median(corflux)))) plt.plot(sd_lc["t"], sd_lc["corr"], 'c.') plt.savefig("plot_outputs/{0}_detrended.png".format(outfilename))
def K2_DetrendRev4(fn, ): time, flux, xbar, ybar = np.genfromtxt(fn, unpack=True, skip_header=1) m1 = np.isfinite(flux) time = time[m1] flux = flux[m1] xbar = xbar[m1] ybar = ybar[m1] flatlc = extract_lc.medfilt(time, flux, window=3) # zpt = len(time)%300 zpt = len(time) % 327 outflux, correction, thr_cad = extract_lc.run_C0_detrend( # time, flatlc, xbar, ybar, cadstep=300, skip=1828) time, flatlc, xbar, ybar, cadstep=327, skip=1828) not_thr = ~thr_cad corflux = (flux[zpt:][not_thr] / np.median(flux[zpt:][not_thr]) / correction[not_thr]) corflatflux = (flatlc[zpt:][not_thr] / np.median(flatlc[zpt:][not_thr]) / correction[not_thr]) # The 1.4826 and *4 factors make this similar to a 4-sigma cut. mad_cut = 1.4826 * MAD(corflatflux - 1.) * 4 keep = np.abs(corflatflux - 1.) < mad_cut # Adds the detrended data to an ascii table. # To use this in conjunction with the ACF included in this package, you must # comment out corflatflux, keep, flux, and flatflux, then run the script on # your data set. compiled_table = astropy.table.Table() compiled_table['time'] = time[zpt:][not_thr] compiled_table['corflatflux'] = corflatflux compiled_table['corflux'] = corflux compiled_table['keep'] = keep compiled_table['flux'] = flux[zpt:][not_thr] compiled_table['flatflux'] = flatlc[zpt:][not_thr] compiled_table['xbar'] = xbar[zpt:][not_thr] compiled_table['ybar'] = ybar[zpt:][not_thr] # Generates the DANCe # for the file title. # DANCe = fn.split('/')[-1].split('.')[0] substr = fn.split('/')[-1] end = substr.find('.dat') DANCe = substr[:end] newtable = { 'Dates': time[zpt:][not_thr], 'Flux': flux[zpt:][not_thr], 'Corrflux': corflux, 'Xpos': xbar[zpt:][not_thr], 'Ypos': ybar[zpt:][not_thr] } ascii.write( newtable, '/Users/acody/Data/K2/Field_0/M35/Lightcurves_RADec_ap3.0_v4_AMCdetrend/' + DANCe + '_detrended.dat', names=['Dates', 'Flux', 'Corrflux', 'Xpos', 'Ypos']) # Create some plots plt.clf() plt.subplot(211) plt.plot(time[zpt:][not_thr], flux[zpt:][not_thr] / np.median(flux[zpt:][not_thr]), 'bo', markersize=2) plt.xlabel('Time [d]') plt.ylabel('Flux/Median flux') plt.ylim( (np.median(flux[zpt:][not_thr] / np.median(flux[zpt:][not_thr])) - 4.5 * np.std(flux[zpt:][not_thr] / np.median(flux[zpt:][not_thr])), np.median(flux[zpt:][not_thr] / np.median(flux[zpt:][not_thr])) + 4.5 * np.std(flux[zpt:][not_thr] / np.median(flux[zpt:][not_thr])))) plt.title = DANCe plt.subplot(212) plt.plot(time[zpt:][not_thr], corflux / np.median(corflux), 'bo', markersize=2) plt.xlabel('Time [d]') plt.ylabel('Flux/Median flux') plt.ylim((np.median(corflux / np.median(corflux)) - 4.5 * np.std(corflux / np.median(corflux)), np.median(corflux / np.median(corflux)) + 4.5 * np.std(corflux / np.median(corflux)))) plt.savefig( '/Users/acody/Data/K2/Field_0/M35/Lightcurves_RADec_ap3.0_v4_AMCdetrend/' + DANCe + '_detrended.png')
def K2_DetrendRev4(fn,): time, flux, xbar, ybar = np.genfromtxt(fn, unpack = True, skip_header=1) m1 = np.isfinite(flux) time = time[m1] flux = flux[m1] xbar = xbar[m1] ybar = ybar[m1] flatlc = extract_lc.medfilt(time,flux,window=3) # zpt = len(time)%300 zpt = len(time)%327 outflux, correction, thr_cad = extract_lc.run_C0_detrend( # time, flatlc, xbar, ybar, cadstep=300, skip=1828) time, flatlc, xbar, ybar, cadstep=327, skip=1828) not_thr = ~thr_cad corflux = (flux[zpt:][not_thr]/ np.median(flux[zpt:][not_thr])/ correction[not_thr]) corflatflux = (flatlc[zpt:][not_thr]/ np.median(flatlc[zpt:][not_thr])/ correction[not_thr]) # The 1.4826 and *4 factors make this similar to a 4-sigma cut. mad_cut = 1.4826*MAD(corflatflux-1.)*4 keep = np.abs(corflatflux-1.) < mad_cut # Adds the detrended data to an ascii table. # To use this in conjunction with the ACF included in this package, you must # comment out corflatflux, keep, flux, and flatflux, then run the script on # your data set. compiled_table = astropy.table.Table() compiled_table['time'] = time[zpt:][not_thr] compiled_table['corflatflux'] = corflatflux compiled_table['corflux'] = corflux compiled_table['keep'] = keep compiled_table['flux'] = flux[zpt:][not_thr] compiled_table['flatflux'] = flatlc[zpt:][not_thr] compiled_table['xbar'] = xbar[zpt:][not_thr] compiled_table['ybar'] = ybar[zpt:][not_thr] # Generates the DANCe # for the file title. # DANCe = fn.split('/')[-1].split('.')[0] substr = fn.split('/')[-1] end = substr.find('.dat') DANCe = substr[:end] newtable = {'Dates': time[zpt:][not_thr], 'Flux': flux[zpt:][not_thr], 'Corrflux': corflux, 'Xpos': xbar[zpt:][not_thr], 'Ypos': ybar[zpt:][not_thr]} ascii.write(newtable, '/Users/acody/Data/K2/Field_0/M35/Lightcurves_RADec_ap3.0_v4_AMCdetrend/'+DANCe + '_detrended.dat', names=['Dates','Flux', 'Corrflux','Xpos','Ypos']) # Create some plots plt.clf() plt.subplot(211) plt.plot(time[zpt:][not_thr], flux[zpt:][not_thr]/np.median(flux[zpt:][not_thr]), 'bo', markersize=2) plt.xlabel('Time [d]') plt.ylabel('Flux/Median flux') plt.ylim((np.median(flux[zpt:][not_thr]/np.median(flux[zpt:][not_thr]))-4.5*np.std(flux[zpt:][not_thr]/np.median(flux[zpt:][not_thr])),np.median(flux[zpt:][not_thr]/np.median(flux[zpt:][not_thr]))+ 4.5*np.std(flux[zpt:][not_thr]/np.median(flux[zpt:][not_thr])))) plt.title = DANCe plt.subplot(212) plt.plot(time[zpt:][not_thr], corflux/np.median(corflux), 'bo', markersize=2) plt.xlabel('Time [d]') plt.ylabel('Flux/Median flux') plt.ylim((np.median(corflux/np.median(corflux))-4.5*np.std(corflux/np.median(corflux)),np.median(corflux/np.median(corflux))+4.5*np.std(corflux/np.median(corflux)))) plt.savefig('/Users/acody/Data/K2/Field_0/M35/Lightcurves_RADec_ap3.0_v4_AMCdetrend/'+DANCe + '_detrended.png')
def detrendThat(time, flux, xpos, ypos, ferr=None, qflags=None, inpflag=None, ap=4.0, cadstep=300): """ code to run self flat field on K2 data Keyword arguments: time -- time time array flux -- the array of brightnesses xpos -- the x-pixel position ypos -- the y-pixel position ferr -- flux error, will be assumed to be uniform if None qflags -- data quality flags inpflag -- flags to use to explude data, non-zero values are removed ap -- a random number """ if ferr is None: ferr = np.ones_like(time) if inpflag is None: inpflag = np.ones_like(time) if qflags is None: qflags = np.zeros_like(time) # we are going to remove all the bad values # we should never pass out the time # this should never trigger badmask = np.isfinite(flux) * (inpflag > 0.) time = np.copy(time[badmask]) flux = np.copy(flux[badmask]) ferr = np.copy(ferr[badmask]) xpos = np.copy(xpos[badmask]) ypos = np.copy(ypos[badmask]) flatlc = extract_lc.medfilt(time, flux, window=3) n_chunks = 6 cadstep = np.int(np.floor(len(time) / n_chunks)) # 600 zpt = len(time) % cadstep if zpt == cadstep: zpt = 0 logging.info("%d pts %d chunks step=%d zpt=%d ap=%f ", len(time), n_chunks, cadstep, zpt, ap) outflux, correction, thr_cad = extract_lc.run_C0_detrend(time, flatlc, xpos, ypos, cadstep=cadstep, skip=None) not_thr = ~thr_cad assert len(outflux) == len(correction) assert len(correction[not_thr]) == len(time[zpt:][not_thr]) corflux = (flux[zpt:][not_thr] / np.median(flux[zpt:][not_thr]) / correction[not_thr]) corflatflux = (flatlc[zpt:][not_thr] / np.median(flatlc[zpt:][not_thr]) / correction[not_thr]) # The 1.4826 and *4 factors make this similar to a 4-sigma cut. # this will never trigger mad_cut = 1.4826 * MAD(corflatflux - 1.) * 4 keep = np.abs(corflatflux - 1.) < mad_cut newflags = np.zeros(len(flux), dtype=bool) newflags[zpt:][not_thr] = 1 return corflux, corflatflux, correction[not_thr], newflags
def detrendThat(time, flux, xpos, ypos, ferr=None, qflags=None, inpflag=None, ap=4.0, cadstep=300): """ code to run self flat field on K2 data Keyword arguments: time -- time time array flux -- the array of brightnesses xpos -- the x-pixel position ypos -- the y-pixel position ferr -- flux error, will be assumed to be uniform if None qflags -- data quality flags inpflag -- flags to use to explude data, non-zero values are removed ap -- a random number """ if ferr is None: ferr = np.ones_like(time) if inpflag is None: inpflag = np.ones_like(time) if qflags is None: qflags = np.zeros_like(time) # we are going to remove all the bad values # we should never pass out the time # this should never trigger badmask = np.isfinite(flux) * (inpflag > 0.) time = np.copy(time[badmask]) flux = np.copy(flux[badmask]) ferr = np.copy(ferr[badmask]) xpos = np.copy(xpos[badmask]) ypos = np.copy(ypos[badmask]) flatlc = extract_lc.medfilt(time, flux, window=3) n_chunks = 6 cadstep = np.int(np.floor(len(time) / n_chunks)) # 600 zpt = len(time) % cadstep if zpt == cadstep: zpt = 0 logging.info("%d pts %d chunks step=%d zpt=%d ap=%f ", len(time), n_chunks, cadstep, zpt, ap) outflux, correction, thr_cad = extract_lc.run_C0_detrend( time, flatlc, xpos, ypos, cadstep=cadstep, skip=None) not_thr = ~thr_cad assert len(outflux) == len(correction) assert len(correction[not_thr]) == len(time[zpt:][not_thr]) corflux = (flux[zpt:][not_thr] / np.median(flux[zpt:][not_thr]) / correction[not_thr]) corflatflux = (flatlc[zpt:][not_thr] / np.median(flatlc[zpt:][not_thr]) / correction[not_thr]) # The 1.4826 and *4 factors make this similar to a 4-sigma cut. # this will never trigger mad_cut = 1.4826*MAD(corflatflux-1.)*4 keep = np.abs(corflatflux-1.) < mad_cut newflags = np.zeros(len(flux), dtype=bool) newflags[zpt:][not_thr] = 1 return corflux, corflatflux, correction[not_thr], newflags
def K2_DetrendRev2(fn, ): #DANCe = fn.split('/')[-1].split('_')[0] time, flux, xbar, ybar = np.genfromtxt(fn, unpack=True) m1 = np.isfinite(flux) time = time[m1] flux = flux[m1] xbar = xbar[m1] ybar = ybar[m1] flatlc = extract_lc.medfilt(time, flux, window=3) zpt = len(time) % 300 outflux, correction, thr_cad = extract_lc.run_C0_detrend(time, flatlc, xbar, ybar, cadstep=300, skip=1828) not_thr = ~thr_cad corflux = (flux[zpt:][not_thr] / np.median(flux[zpt:][not_thr]) / correction[not_thr]) corflatflux = (flatlc[zpt:][not_thr] / np.median(flatlc[zpt:][not_thr]) / correction[not_thr]) mad_cut = 1.4826 * MAD(corflatflux - 1.) * 4 keep = np.abs(corflatflux - 1.) < mad_cut #plt.plot(time[zpt:][not_thr],corflatflux,c='r') #plt.plot(time[zpt:][not_thr][keep],corflatflux[keep],c='b') #CDPP Calculations #thr = twohr_cdpp(flux) #shr = sixhr_cdpp(flux) #plt.plot(time,flatlc) #plt.figure(figsize=[15,6]) #plt.plot(time,flux/np.median(flux),'bo',markersize=1) #plt.plot(time[zpt:][not_thr][keep],corflux[keep],marker='.') #plt.xlabel('Time [d]') #plt.ylabel('Normalized Flux') #plt.title(str(DANCe) + ' 6.5_Hr_CDPP_' + str(shr) + ' | 2.5_Hr_CCPD_' + str(thr)) #plt.savefig(fn.split('.')[-1] + '.png') #plt.show() #plt.close('all') compiled_table = astropy.table.Table() compiled_table['time'] = time[zpt:][ not_thr] #('time', time[zpt:][not_thr]) compiled_table['corflatflux'] = corflatflux compiled_table['corflux'] = corflux compiled_table['keep'] = keep compiled_table['flux'] = flux[zpt:][not_thr] compiled_table['xbar'] = xbar[zpt:][not_thr] compiled_table['ybar'] = ybar[zpt:][not_thr] compiled_table['flatflux'] = flatlc[zpt:][not_thr] DANCe = fn.split('/')[-1].split('.')[-1] np.savetxt( '/Users/bryanmann/Documents/NASA_Kepler_2.0/All_Light_Curves/Lightcurves_RADec_ap2.0_BJM/Detrended_Data/' + DANCe, compiled_tbl, fmt='ascii', delimiter=',')
def K2_DetrendRev4(filename, ap=4.0): lcs = ascii.read(filename) outfilename = filename.split("/")[-1][:-4] logging.info(outfilename) time = lcs["t"] x_pos = lcs["x"] y_pos = lcs["y"] qual_flux = np.zeros_like(time) unc_flux = np.ones_like(time) best_col = "flux_{0:.1f}".format(ap) flux = lcs[best_col] m1 = np.isfinite(flux) time = time[m1] flux = flux[m1] xbar = x_pos[m1] ybar = y_pos[m1] flatlc = extract_lc.medfilt(time, flux, window=3) n_chunks = 6 cadstep = np.int(np.floor(len(time) / n_chunks)) #600 zpt = len(time) % cadstep if zpt == cadstep: zpt = 0 logging.info("%d pts %d chunks step=%d zpt=%d ap=%f ", len(time), n_chunks, cadstep, zpt, ap) outflux, correction, thr_cad = extract_lc.run_C0_detrend(time, flatlc, xbar, ybar, cadstep=cadstep, skip=None) not_thr = ~thr_cad corflux = (flux[zpt:][not_thr] / np.median(flux[zpt:][not_thr]) / correction[not_thr]) corflatflux = (flatlc[zpt:][not_thr] / np.median(flatlc[zpt:][not_thr]) / correction[not_thr]) # The 1.4826 and *4 factors make this similar to a 4-sigma cut. mad_cut = 1.4826 * MAD(corflatflux - 1.) * 4 keep = np.abs(corflatflux - 1.) < mad_cut # Adds the detrended data to an ascii table. newtable = { 'Dates': time[zpt:][not_thr], 'Flux': flux[zpt:][not_thr], 'Corrflux': corflux, 'Xpos': xbar[zpt:][not_thr], 'Ypos': ybar[zpt:][not_thr] } ascii.write(newtable, "output_lcs/{0}_detrended.dat".format(outfilename), names=['Dates', 'Flux', 'Corrflux', 'Xpos', 'Ypos']) # Create some plots # Compare to my outputs sd_lc = ascii.read("../k2spin/output_lcs/{0}_lcs.csv".format(outfilename)) # Raw Lightcurve plt.clf() plt.subplot(211) plt.plot(sd_lc["t"], sd_lc["raw"] / np.median(sd_lc["raw"]), 'c.') plt.plot(time[zpt:][not_thr], flux[zpt:][not_thr] / np.median(flux[zpt:][not_thr]), 'bo', markersize=2) plt.xlabel('Time [d]') plt.ylabel('Flux/Median flux') plt.ylim( (np.median(flux[zpt:][not_thr] / np.median(flux[zpt:][not_thr])) - 4.5 * np.std(flux[zpt:][not_thr] / np.median(flux[zpt:][not_thr])), np.median(flux[zpt:][not_thr] / np.median(flux[zpt:][not_thr])) + 4.5 * np.std(flux[zpt:][not_thr] / np.median(flux[zpt:][not_thr])))) # Corrected lightcurve plt.subplot(212) plt.plot(time[zpt:][not_thr], corflux / np.median(corflux), 'bo', markersize=2) plt.xlabel('Time [d]') plt.ylabel('Flux/Median flux') plt.ylim((np.median(corflux / np.median(corflux)) - 4.5 * np.std(corflux / np.median(corflux)), np.median(corflux / np.median(corflux)) + 4.5 * np.std(corflux / np.median(corflux)))) plt.plot(sd_lc["t"], sd_lc["corr"], 'c.') plt.savefig("plot_outputs/{0}_detrended.png".format(outfilename))
#print('{} of {}'.format(i,len(filenames))) bar.update(i+1) time,lc,xbar,ybar,regnum = extract_lc.run_C0_data_extract( filename, bg_cut=4) if regnum == 0: continue #mask = np.ones(len(lc),dtype=bool) #mask[range(11,len(lc),12)] = False #time,lc,xbar,ybar = time[mask], lc[mask], xbar[mask], ybar[mask] #lets flatten the light curve flatlc = extract_lc.medfilt(time,lc,window=1.) zpt = len(time)%300. try: outflux, correction, thr_cad = extract_lc.run_C0_detrend( time,flatlc,xbar,ybar,cadstep=300) except ValueError: print('{} had an ValueError'.format(filename)) continue not_thr = ~thr_cad corflux = (lc[zpt:][not_thr]/ np.median(lc[zpt:][not_thr])/ correction[not_thr]) corflatflux = (flatlc[zpt:][not_thr]/