def make_periodogram(k,sname,outdir="../PlanetFitting"): minPer=1.5 # ignore aliasing on short time scales p = SystPy.periodogram_ls(SystPy.getResidualMatrix(k), 100000, minPer, 10000, 0, SystPy.K_T_TIME, SystPy.K_T_SVAL, SystPy.K_T_ERR) amp = p[:,SystPy.K_PS_Z] peaks = SystPy.getPeakIndx(amp) amp = amp[peaks] per = p[:,SystPy.K_PS_TIME][peaks] fap = p[:,SystPy.K_PS_FAP][peaks] sort = amp.argsort()[::-1] #print (amp[sort]) #print (per[sort]) #print (fap[sort]) t = Table([amp[sort],per[sort],fap[sort]], names=['Amp','Period','FAP']) # print (t[0:5]) plt.plot(p[:,SystPy.K_PS_TIME], p[:,SystPy.K_PS_Z], c='black') plt.xscale('log') plt.xlabel("Period", fontsize=14) plt.ylabel("Power", fontsize=14) plt.xlim([0.5, 10000]) ylims = plt.ylim() plt.title("Periodgram of residuals for %s" % (sname)) ttxt = "Amp Period FAP" plt.text(1000,0.95*ylims[1],ttxt) for i,s in enumerate([0.9,0.85,0.8]): ttxt = "%.2f %.2f %.2g" % (t['Amp'][i],t['Period'][i],t['FAP'][i]) plt.text(1000,s*ylims[1],ttxt) pname = "%s_ResidualPeriodogram.pdf" % (sname) pname = os.path.join(outdir,pname) plt.savefig(pname, bbox_inches='tight') plt.close()
def checknumvels(invels,sname,outdir="../PlanetFitting"): kernelname = "%s" % (sname) kernelname = os.path.join(outdir,kernelname) if os.path.exists(kernelname): kernel = SystPy.loadKernel(kernelname) if kernel.nData() == len(invels['jd']): return False return True
def fit_planets(k,sname,initphases,vmag,rplanets,mplanets,planetids,veloff,outfile,writefit=False,veldir="../VelsFiles",outdir="../PlanetFitting"): fn = sname + ".vels" indata = fit_TESS_APF.readin_velsfile(os.path.join(veldir,fn)) m = SystPy.matrix_to_array(SystPy.getResidualMatrix(k)) txt = fit_TESS_APF.fit_dates_vels(m[:,0],m[:,1],m[:,2]) otxt = "%s %f" % (txt, k.getRMS()) print (otxt) if writefit: outname = "%s.fit" % (sname) outname = os.path.join(outdir,outname) outfp = open(outname,"w") outfp.write(otxt+"\n") kl = SystPy.MCMC(k,nChains = 4, rStop = 1.05) stds = kl.getElementsStats(SystPy.K_STAT_STDDEV) meds = kl.getElementsStats(SystPy.K_STAT_MEDIAN) mads = 1.4826*kl.getElementsStats(SystPy.K_STAT_MAD) for i in range(0,len(initphases)): fperiod = k.getElement(i+1,SystPy.K_PER) phases = getpriority.compute_currentphase(indata['jd'],fperiod,initphases[i]) K = meds[i+1,SystPy.K_SEMIAMP] err_K = mads[i+1,SystPy.K_SEMIAMP] ostr = "%s %f %f %f %.4g %.4g %.4g %d\n" % (sname,fperiod,K,err_K,k.getElement(i+1,SystPy.K_MASS)*317.8281,mads[i+1,SystPy.K_MASS]*317.8281,rplanets[i],len(phases)) # period (days) K (m/s) err_K (m/s) planet mass (M_earth) error (M_earth) R (R_earth) #vs #phase1 #phase2 #phase3 #phase4 #phase5 outfile.write(ostr) if writefit: outfp.write(ostr) if writefit: outfp.close() return
def plot_planets(k,sname,initphases,vmag,rplanets,mplanets,planetids,veloff,writefit=False,veldir="../VelsFiles",outdir="../PlanetFitting"): fn = sname + ".vels" indata = readin_velsfile(os.path.join(veldir,fn)) m = SystPy.matrix_to_array(SystPy.getResidualMatrix(k)) txt = fit_dates_vels(m[:,0],m[:,1],m[:,2]) otxt = "%s %f" % (txt, k.getRMS()) print (otxt) if writefit: outname = "%s.fit" % (sname) outname = os.path.join(outdir,outname) outfp = open(outname,"w") outfp.write(otxt+"\n") kl = SystPy.MCMC(k,nChains = 4, rStop = 1.05) stds = kl.getElementsStats(SystPy.K_STAT_STDDEV) meds = kl.getElementsStats(SystPy.K_STAT_MEDIAN) mads = 1.4826*kl.getElementsStats(SystPy.K_STAT_MAD) for i in range(0,len(initphases)): fperiod = k.getElement(i+1,SystPy.K_PER) phases = getpriority.compute_currentphase(indata['jd'],fperiod,initphases[i]) daterange=np.linspace(np.min(indata['jd']),np.min(indata['jd'])+fperiod,num=200) plotting_phases=getpriority.compute_currentphase(daterange,fperiod,initphases[i]) plotting_phases = plotting_phases[(plotting_phases > 0) & (plotting_phases < 1)] plotting_phases = np.sort(plotting_phases) plt.errorbar(phases,indata['velocity'],yerr=indata['int unc'],fmt='bo') plt.xlabel('Phases',fontsize=14) plt.ylabel('Velocity (m s$^{-1}$)',fontsize=14) plt.title('Phase folded velocities for ' + sname) K = meds[i+1,SystPy.K_SEMIAMP] err_K = mads[i+1,SystPy.K_SEMIAMP] M = meds[i+1,SystPy.K_MASS] err_M = mads[i+1,SystPy.K_MASS] f=plotting_phases*2.*sc.pi pvels=(np.cos(f+OMEGA) + ECC*np.cos(OMEGA)) plt.plot(plotting_phases,K*pvels + meds[i+1,SystPy.K_P_DATA1],'k-') top = K*pvels+err_K + meds[i+1,SystPy.K_P_DATA1] bot = K*pvels-err_K + meds[i+1,SystPy.K_P_DATA1] plt.fill_between(plotting_phases,bot,top,color='blue',alpha=0.2) ylims = plt.ylim() plt.xlim(0,1) xs = np.asarray([getpriority.EDGE1,getpriority.EDGE2]) plt.fill_between(xs,ylims[0],ylims[1],facecolor='grey',alpha=0.2) xs = np.asarray([getpriority.EDGE3,getpriority.EDGE4]) plt.fill_between(xs,ylims[0],ylims[1],facecolor='grey',alpha=0.2) pstr= "Period %.2f days M=%.2f M$_{sun}$ V = %.2f mag" % (fperiod,k.getElement(0,1),vmag[i]) plt.text(0.37,ylims[1]*0.9,pstr,va='bottom') pstr= "$M=%.2g \pm %.2g\ M_J$"% (M,err_M) plt.text(0.37,ylims[1]*0.8,pstr,va='bottom') pstr="$(K = %.2f \pm %.2f \ m\ s^{-1})$" %(K,err_K) plt.text(0.37,ylims[1]*0.7,pstr,va='bottom') pstr= "$\sigma$=%.2f m s$^{-1}$ $\mu$=%.2f deg" % (k.getRms(),k.getElement(i+1,SystPy.K_MA)) plt.text(0.37,ylims[1]*0.6,pstr,va='bottom') binedges = [getpriority.EDGE1,getpriority.EDGE2,getpriority.EDGE3,getpriority.EDGE4,getpriority.EDGE5] phasebins = np.digitize(phases,binedges,right=True) ninbins = [] for nbin in range(0,len(binedges)): cpb = phasebins[phasebins == nbin] ninbins.append(len(cpb)) figname = "%s_planet%d_PhasedVelocities.pdf" % (sname,i+1) figname = os.path.join(outdir,figname) plt.savefig(figname, bbox_inches='tight') plt.close() if writefit: ostr = "%f %f %f %.4g %.4g %.4g %d" % (fperiod,K,err_K,M*317.8281,err_M*317.8281,rplanets[i],len(phases)) # period (days) K (m/s) err_K (m/s) planet mass (M_earth) error (M_earth) R (R_earth) #vs #phase1 #phase2 #phase3 #phase4 #phase5 bstr = "" for i in range(0,len(ninbins)): bstr += " %d" % ninbins[i] bstr += "\n" outfp.write(ostr) outfp.write(bstr) kernelname = "%s" % (sname) kernelname = os.path.join(outdir,kernelname) k.save(kernelname) if writefit: outfp.close() return
for sname in snames: sfn = sname + ".sys" vfn = sname + ".vels" newvels = True invels = readin_velsfile(os.path.join(veldir,vfn)) ddates,dphases, dvels, derrs, di2sums = bin_phase_dates(invels["jd"],invels["phases"],invels['velocity'],invels["int unc"],invels["I2 counts"]) bvfn = sname + "binned" ascii.write([ddates,dvels,derrs,di2sums,dphases], os.path.join(veldir,bvfn+".vels"),format="no_header",overwrite=True) Generate_Velocities.write_sys(TESSAPFdata,sname,velname=bvfn,outdir=veldir) newvels = checknumvels(invels,sname,outdir=outdir) if len(dvels) > 4 and newvels: k=SystPy.Kernel() k.setEpoch(JD0) ddir =veldir k.addDataFile(sfn, directory=ddir) planets, = np.where((TESSAPFdata['star_names'] == sname) & (TESSAPFdata['detected'] == "TRUE")) # planet masses are in earth masses, Systemic likes Jupiters #mearth = 5.9722e24 #mjup = 1.898e27 mratio = 317.8281 TESSAPFdata['est_mass'] /= mratio add_planets(k,TESSAPFdata,planets,gd['vel_offset'][gd['starname'] == sname]) #print (k.getElements()) print ("%s: RMS of fit %f" % (sname,k.getRms()))
def mcmc_planets(k, sname, TESSAPFdata, indices, veloff, writefit=False, veldir="../VelsFiles", outdir="../PlanetFitting"): initphases = TESSAPFdata['phase'][indices] vmag = TESSAPFdata['vmag'][indices] rplanets = TESSAPFdata['rplanet'][indices] mplanets = TESSAPFdata['true_mass'][indices] planetids = TESSAPFdata['Index'][indices] fn = sname + ".vels" indata = readin_velsfile(os.path.join(veldir, fn)) m = SystPy.matrix_to_array(SystPy.getResidualMatrix(k)) txt = fit_dates_vels(m[:, 0], m[:, 1], m[:, 2]) otxt = "%s %f" % (txt, k.getRMS()) if writefit: outname = "%s_mcmc.fit" % (sname) outname = os.path.join(outdir, outname) outfp = open(outname, "w") outfp.write(otxt + "\n") kl = SystPy.MCMC(k, nChains=4, rStop=1.05) bsname = os.path.join(outdir, sname + ".kl") SystPy.KLSave(kl, bsname) stds = kl.getElementsStats(SystPy.K_STAT_STDDEV) meds = kl.getElementsStats(SystPy.K_STAT_MEDIAN) mads = 1.4826 * kl.getElementsStats(SystPy.K_STAT_MAD) for i in range(0, len(initphases)): fperiod = k.getElement(i + 1, SystPy.K_PER) phases = getpriority.compute_currentphase(indata['jd'], fperiod, initphases[i]) K = meds[i + 1, SystPy.K_SEMIAMP] err_K = mads[i + 1, SystPy.K_SEMIAMP] binedges = [ getpriority.EDGE1, getpriority.EDGE2, getpriority.EDGE3, getpriority.EDGE4, getpriority.EDGE5 ] phasebins = np.digitize(phases, binedges, right=True) ninbins = [] for nbin in range(0, len(binedges)): cpb = phasebins[phasebins == nbin] ninbins.append(len(cpb)) if writefit: ostr = "%f %f %f %.4g %.4g %.4g %d" % ( fperiod, K, err_K, k.getElement(i + 1, SystPy.K_MASS) * 317.8281, mads[i + 1, SystPy.K_MASS] * 317.8281, rplanets[i], len(phases)) # period (days) K (m/s) err_K (m/s) planet mass (M_earth) error (M_earth) R (R_earth) #vs #phase1 #phase2 #phase3 #phase4 #phase5 bstr = "" for i in range(0, len(ninbins)): bstr += " %d" % ninbins[i] bstr += "\n" outfp.write(ostr) outfp.write(bstr) # kernelname = "%s" % (sname) # kernelname = os.path.join(outdir,kernelname) # k.save(kernelname) if writefit: outfp.close() return
outfp.close() return snames, gdfn, mfn, veldir, outdir = parse_options() gd = ascii.read(gdfn) TESSAPFdata = ascii.read(mfn, format='csv') for sname in snames: sfn = sname + ".sys" vfn = sname + ".vels" kernelname = "%s" % (sname) kernelname = os.path.join(outdir, kernelname) if os.path.exists(kernelname): k = SystPy.loadKernel(kernelname) else: continue planets, = np.where((TESSAPFdata['star_names'] == sname) & (TESSAPFdata['detected'] == "TRUE")) # planet masses are in earth masses, Systemic likes Jupiters #mearth = 5.9722e24 #mjup = 1.898e27 mratio = 317.8281 TESSAPFdata['est_mass'] /= mratio print "%s: RMS of fit %f" % (sname, k.getRms()) mcmc_planets(k, sname,