def plot_heights(inputfile, outputfile, title=""): MMWA, MLWA, SNR = np.loadtxt(inputfile, usecols=(11, 12, 14), unpack=True) pp = PDF(outputfile) ax = GPP.plot_rows(MMWA, weights=SNR, ylabel="Age [Gyr]", label="MMWA", kpc_scale=0.0485) GPP.plot_rows(MLWA, weights=SNR, label="MLWA", ax=ax, kpc_scale=0.0485) ax.legend(loc=0, scatterpoints=1, numpoints=1, frameon=False) ax.set_xlim(-0.5, 3) ax.set_title(title) pp.savefig(ax.figure) pp.close() plt.close(ax.figure) return
def all_heights(output, inputprefix='NGC_891', err=True, binned=True): datname = output.split('.pdf')[0]+'.dat' f = open(datname,'w') f.write('#{:5}{:>13}{:>13}{:>13}{:>13}{:>13}{:>13}{:>13}{:>13}{:>13}{:>13}\n#\n'.\ format('','height [kpc]','age [Gyr]', 'age eom','age stderr', 'AV','AV eom','AV stderr', 'Z/Z_sol','Z eom', 'Z stderr')) pp = PDF(output) symblist = ["o","^","v","s","*","x"] colorlist = ['b','c','g','y','m','r'] kpc_scale = 0.0485 plist = [6,3,4,2,1,5] zcorrlist = [-0.01, -0.113, -0.225, -0.07, -0.142, -0.004] rlist = [-10.0, -6.4, -2.3, 0.8, 4.4, 8.1] ageax = None AVax = None Zax = None for i in range(6): inputfile = glob('{}*P{}*allz2*.dat'.format(inputprefix,plist[i]))[0] print inputfile if binned: fitsname = glob('{}*P{}*.ms*fits'.format(inputprefix,plist[i]))[0] print fitsname binhead = pyfits.open(fitsname)[0].header else: binhead = False MLWA, MLWZ, TAUV, SNR = np.loadtxt(inputfile, usecols=(62,64,66,67), unpack=True) MLWZ = np.log10(MLWZ) ageax, tmpz, tmpage, tmperr, tmpstd = GPP.plot_rows(MLWA, binheader=binhead, weights=SNR, ax=ageax, label='{}'.\ format(rlist[i]), fullout=True, kpc_scale=kpc_scale, zcorr=zcorrlist[i], err=err, marker=symblist[i], linestyle='', color=colorlist[i]) AVax, _, tmpAV, tmpAVerr, tmpAVstd = GPP.plot_rows(TAUV*1.086, binheader = binhead, weights=SNR, ax=AVax, label='{}'.\ format(rlist[i]), fullout=True, kpc_scale=kpc_scale, zcorr=zcorrlist[i], err=err, marker=symblist[i], linestyle='', color=colorlist[i]) Zax, _, tmpZ, tmpZerr, tmpZstd = GPP.plot_rows(MLWZ, binheader = binhead, weights=SNR, ax=Zax, label='{}'.\ format(rlist[i]), fullout=True, kpc_scale=kpc_scale, zcorr=zcorrlist[i], err=err, marker=symblist[i], linestyle='', color=colorlist[i]) f.write('\n# P{} '.format(plist[i])+92*'#'+'\n# r ~ {} kpc\n'.\ format(rlist[i])) for i in range(tmpz.size): f.write('{:6}{:13.3f}{:13.3f}{:13.3f}{:13.3f}{:13.3f}{:13.3f}{:13.3f}{:13.3f}{:13.3f}{:13.3f}\n'.\ format('',tmpz[i],tmpage[i],tmperr[i],tmpstd[i], tmpAV[i],tmpAVerr[i],tmpAVstd[i], tmpZ[i],tmpZerr[i],tmpZstd[i])) try: z = np.vstack((z,tmpz)) age = np.vstack((age,tmpage)) err = np.vstack((err,tmperr)) std = np.vstack((std,tmpstd)) AV = np.vstack((AV,tmpAV)) AVstd = np.vstack((AVstd,tmpAVstd)) AVerr = np.vstack((AVerr,tmpAVerr)) Z = np.vstack((Z,tmpZ)) Zerr = np.vstack((Zerr,tmpZerr)) Zstd = np.vstack((Zstd,tmpZstd)) except UnboundLocalError: z = tmpz age = tmpage err = tmperr std = tmpstd AV = tmpAV AVstd = tmpAVstd AVerr = tmpAVerr Z = tmpZ Zerr = tmpZerr Zstd = tmpZstd bigz = np.nanmean(z,axis=0) bigage = np.nanmean(age,axis=0) bigerr = np.sqrt( np.nansum(err*(age - bigage)**2,axis=0)/ ((err.shape[0] - 1.)/(err.shape[0]) * np.nansum(err,axis=0))) bigAV = np.nanmean(AV,axis=0) bigAVerr = np.sqrt( np.nansum(AVerr*(AV - bigAV)**2,axis=0)/ ((AVerr.shape[0] - 1.)/(AVerr.shape[0]) * np.nansum(AVerr,axis=0))) bigZ = np.nanmean(Z,axis=0) bigZerr = np.sqrt( np.nansum(Zerr*(Z - bigZ)**2,axis=0)/ ((Zerr.shape[0] - 1.)/(Zerr.shape[0]) * np.nansum(Zerr,axis=0))) with open('{}_means.dat'.format(datname.split('.dat')[0]),'w') as fm: fm.write(str('#{:>9}'+'{:>10}'*6+'\n').format('height', 'MLWA', 'MLWA err', 'Av', 'Av err', 'Z', 'Z err')) for i in range(bigz.size): fm.write('{:10.4f}{:10.4f}{:10.4f}{:10.4f}{:10.4f}{:10.4f}{:10.4f}\n'.\ format(bigz[i],bigage[i],bigerr[i],bigAV[i],bigAVerr[i],bigZ[i],bigZerr[i])) ageax.plot(bigz, bigage) ageax.fill_between(bigz,bigage-bigerr,bigage+bigerr,alpha=0.1) ageax.legend(loc=1,title='radius [kpc]',scatterpoints=1,numpoints=1,frameon=False) ageax.set_xlim(-0.6,2.6) ageax.set_ylim(0,13) ageax.set_ylabel('Light-weighted age [Gyr]') ageax.set_title('Generated on {}'.format(time.asctime())) AVax.plot(bigz,bigAV) AVax.fill_between(bigz,bigAV-bigAVerr,bigAV+bigAVerr,alpha=0.1) AVax.legend(loc=1,title='radius [kpc]',scatterpoints=1,numpoints=1,frameon=False) AVax.set_xlim(-0.6,2.6) AVax.set_ylim(0,6) AVax.set_ylabel(r'$A_V$') Zax.plot(bigz,bigZ) Zax.fill_between(bigz, bigZ-bigZerr, bigZ+bigZerr,alpha=0.1) Zax.legend(loc=1,title='radius [kpc]',scatterpoints=1, numpoints=1,frameon=False) Zax.set_xlim(-0.6,2.6) # Zax.set_ylim(-1.5,3.0) Zax.set_ylabel(r'Log( MLWZ [$Z_{\odot}$] )') pp.savefig(ageax.figure) pp.savefig(AVax.figure) pp.savefig(Zax.figure) plt.close(ageax.figure) plt.close(AVax.figure) plt.close(Zax.figure) pp.close() f.close() return
def all_heights(output, inputprefix="NGC_891", err=True, binned=False, reg=True): datname = output.split(".pdf")[0] + ".dat" f = open(datname, "w") f.write( "#{:5}{:>13}{:>13}{:>13}{:>13}{:>13}{:>13}{:>13}{:>13}{:>13}{:>13}\n#\n".format( "", "height [kpc]", "age [Gyr]", "age eom", "age stderr", "AV", "AV eom", "AV stderr", "Z/Z_sol", "Z eom", "Z stderr", ) ) pp = PDF(output) symblist = ["o", "^", "v", "s", "*", "x"] colorlist = ["b", "c", "g", "y", "m", "r"] kpc_scale = 0.0485 plist = [6, 3, 4, 2, 1, 5] rlist = [-10.0, -6.4, -2.3, 0.8, 4.4, 8.1] # rlist = [4.4,0.8,-6.4,-2.3,8.1,-10.0] ax = None AVax = None metalax = None if reg: psi0ax = None tausfax = None invtausfax = None tformax = None massax = None for i in range(6): inputfile = glob("{}*P{}*fit.dat".format(inputprefix, plist[i]))[0] print inputfile if binned: fitsname = glob("{}*P{}*ms*fits".format(inputprefix, plist[i]))[0] print fitsname binhead = pyfits.open(fitsname)[0].header else: binhead = False if reg: MMWA, MLWA, TAUV, SNR, Z, PSI0, TAUSF, TFORM = np.loadtxt( inputfile, usecols=(11, 12, 13, 14, 17, 18, 19, 20), unpack=True ) mdata = np.loadtxt(inputfile) MASS = np.sum(mdata[:, 1:11], axis=1) else: MMWA, MLWA, TAUV, SNR, Z = np.loadtxt(inputfile, usecols=(11, 12, 13, 14, 19), unpack=True) Z = np.log10(Z) ax, tmpz, tmpage, tmperr, tmpstd = GPP.plot_rows( MLWA, binheader=binhead, weights=SNR, ax=ax, label="{}".format(rlist[i]), fullout=True, kpc_scale=kpc_scale, err=err, marker=symblist[i], linestyle="", color=colorlist[i], ) AVax, _, tmpAV, tmpAVerr, tmpAVstd = GPP.plot_rows( TAUV * 1.086, binheader=binhead, weights=SNR, ax=AVax, label="{}".format(rlist[i]), fullout=True, kpc_scale=kpc_scale, err=err, marker=symblist[i], linestyle="", color=colorlist[i], ) metalax, _, tmpmetal, tmpmetalerr, tmpmetalstd = GPP.plot_rows( Z, binheader=binhead, weights=SNR, ax=metalax, label="{}".format(rlist[i]), fullout=True, kpc_scale=kpc_scale, err=err, marker=symblist[i], linestyle="", color=colorlist[i], ) if reg: psi0ax, _, tmppsi0, tmppsi0err, tmppsi0std = GPP.plot_rows( np.log10(PSI0), binheader=binhead, weights=SNR, ax=psi0ax, label="{}".format(rlist[i]), fullout=True, kpc_scale=kpc_scale, err=err, marker=symblist[i], linestyle="", color=colorlist[i], ) tausfax, _, tmptausf, tmptausferr, tmptausfstd = GPP.plot_rows( TAUSF, binheader=binhead, weights=SNR, ax=tausfax, label="{}".format(rlist[i]), fullout=True, kpc_scale=kpc_scale, err=err, marker=symblist[i], linestyle="", color=colorlist[i], ) invtausfax, _, tmpinvtausf, tmpinvtausferr, tmpinvtausfstd = GPP.plot_rows( 1.0 / TAUSF, binheader=binhead, weights=SNR, ax=invtausfax, label="{}".format(rlist[i]), fullout=True, kpc_scale=kpc_scale, err=err, marker=symblist[i], linestyle="", color=colorlist[i], ) tformax, _, tmptform, tmptformerr, tmptformstd = GPP.plot_rows( TFORM, binheader=binhead, weights=SNR, ax=tformax, label="{}".format(rlist[i]), fullout=True, kpc_scale=kpc_scale, err=err, marker=symblist[i], linestyle="", color=colorlist[i], ) massax, _, tmpmass, tmpmasserr, tmpmassstd = GPP.plot_rows( np.log10(MASS), binheader=binhead, weights=SNR, ax=massax, label="{}".format(rlist[i]), fullout=True, kpc_scale=kpc_scale, err=err, marker=symblist[i], linestyle="", color=colorlist[i], ) f.write("\n# P{} ".format(i + 1) + 92 * "#" + "\n# r ~ {} kpc\n".format(rlist[i])) for i in range(tmpz.size): f.write( "{:6}{:13.3f}{:13.3f}{:13.3f}{:13.3f}{:13.3f}{:13.3f}{:13.3f}{:13.3f}{:13.3f}{:13.3f}\n".format( "", tmpz[i], tmpage[i], tmperr[i], tmpstd[i], tmpAV[i], tmpAVerr[i], tmpAVstd[i], tmpmetal[i], tmpmetalerr[i], tmpmetalstd[i], ) ) try: z = np.vstack((z, tmpz)) age = np.vstack((age, tmpage)) err = np.vstack((err, tmperr)) std = np.vstack((std, tmpstd)) AV = np.vstack((AV, tmpAV)) AVstd = np.vstack((AVstd, tmpAVstd)) AVerr = np.vstack((AVerr, tmpAVerr)) metal = np.vstack((metal, tmpmetal)) metalerr = np.vstack((metalerr, tmpmetalerr)) metalstd = np.vstack((metalstd, tmpmetalstd)) if reg: psi0 = np.vstack((psi0, tmppsi0)) psi0err = np.vstack((psi0err, tmppsi0err)) psi0std = np.vstack((psi0std, tmppsi0std)) tausf = np.vstack((tausf, tmptausf)) tausferr = np.vstack((tausferr, tmptausferr)) tausfstd = np.vstack((tausfstd, tmptausfstd)) invtausf = np.vstack((invtausf, tmpinvtausf)) invtausferr = np.vstack((invtausferr, tmpinvtausferr)) invtausfstd = np.vstack((invtausfstd, tmpinvtausfstd)) tform = np.vstack((tform, tmptform)) tformerr = np.vstack((tformerr, tmptformerr)) tformstd = np.vstack((tformstd, tmptformstd)) mass = np.vstack((mass, tmpmass)) masserr = np.vstack((masserr, tmpmasserr)) massstd = np.vstack((massstd, tmpmassstd)) except UnboundLocalError: z = tmpz age = tmpage err = tmperr std = tmpstd AV = tmpAV AVstd = tmpAVstd AVerr = tmpAVerr metal = tmpmetal metalerr = tmpmetalerr metalstd = tmpmetalstd if reg: psi0 = tmppsi0 psi0err = tmppsi0err psi0std = tmppsi0std tausf = tmptausf tausferr = tmptausferr tausfstd = tmptausfstd invtausf = tmpinvtausf invtausferr = tmpinvtausferr invtausfstd = tmpinvtausfstd tform = tmptform tformerr = tmptformerr tformstd = tmptformstd mass = tmpmass masserr = tmpmasserr massstd = tmpmassstd bigz = np.nanmean(z, axis=0) bigage = np.nanmean(age, axis=0) bigerr = np.sqrt( np.nansum(err * (age - bigage) ** 2, axis=0) / ((err.shape[0] - 1.0) / (err.shape[0]) * np.nansum(err, axis=0)) ) bigAV = np.nanmean(AV, axis=0) bigAVerr = np.sqrt( np.nansum(AVerr * (AV - bigAV) ** 2, axis=0) / ((AVerr.shape[0] - 1.0) / (AVerr.shape[0]) * np.nansum(AVerr, axis=0)) ) bigmetal = np.nanmean(metal, axis=0) bigmetalerr = np.sqrt( np.nansum(metalerr * (metal - bigmetal) ** 2, axis=0) / ((metalerr.shape[0] - 1.0) / (metalerr.shape[0]) * np.nansum(metalerr, axis=0)) ) if reg: bigpsi0 = np.nanmean(psi0, axis=0) bigpsi0err = np.sqrt( np.abs( np.nansum(psi0err * (psi0 - bigpsi0) ** 2, axis=0) / ((psi0err.shape[0] - 1.0) / (psi0err.shape[0]) * np.nansum(psi0err, axis=0)) ) ) bigtausf = np.nanmean(tausf, axis=0) bigtausferr = np.sqrt( np.nansum(tausferr * (tausf - bigtausf) ** 2, axis=0) / ((tausferr.shape[0] - 1.0) / (tausferr.shape[0]) * np.nansum(tausferr, axis=0)) ) biginvtausf = np.nanmean(invtausf, axis=0) biginvtausferr = np.sqrt( np.nansum(invtausferr * (invtausf - biginvtausf) ** 2, axis=0) / ((invtausferr.shape[0] - 1.0) / (invtausferr.shape[0]) * np.nansum(invtausferr, axis=0)) ) bigtform = np.nanmean(tform, axis=0) bigtformerr = np.sqrt( np.nansum(tformerr * (tform - bigtform) ** 2, axis=0) / ((tformerr.shape[0] - 1.0) / (tformerr.shape[0]) * np.nansum(tformerr, axis=0)) ) bigmass = np.nanmean(mass, axis=0) bigmasserr = np.sqrt( np.nansum(masserr * (mass - bigmass) ** 2, axis=0) / ((masserr.shape[0] - 1.0) / (masserr.shape[0]) * np.nansum(masserr, axis=0)) ) with open("{}_means.dat".format(datname.split(".dat")[0]), "w") as fm: fm.write(str("#{:>9}" + "{:>10}" * 6 + "\n").format("height", "MLWA", "MLWA err", "Av", "Av err", "Z", "Z err")) for i in range(bigz.size): fm.write( "{:10.4f}{:10.4f}{:10.4f}{:10.4f}{:10.4f}{:10.4f}{:10.4f}\n".format( bigz[i], bigage[i], bigerr[i], bigAV[i], bigAVerr[i], bigmetal[i], bigmetalerr[i] ) ) ax.plot(bigz, bigage) ax.fill_between(bigz, bigage - bigerr, bigage + bigerr, alpha=0.1) ax.legend(loc=1, title="radius [kpc]", scatterpoints=1, numpoints=1, frameon=False) ax.set_xlim(-0.1, 2.6) ax.set_ylim(-2, 10) ax.set_ylabel("Light-weighted age [Gyr]") ax.set_title("Generated on {}".format(time.asctime())) AVax.plot(bigz, bigAV) AVax.fill_between(bigz, bigAV - bigAVerr, bigAV + bigAVerr, alpha=0.1) AVax.legend(loc=1, title="radius [kpc]", scatterpoints=1, numpoints=1, frameon=False) AVax.set_xlim(-0.1, 2.6) AVax.set_ylim(0, 6) AVax.set_ylabel(r"$A_V$") # plotz = np.linspace(0,2.5,20) # for t in range(len(rlist)): # modelAv = mA.A_vec(np.abs(rlist[t]),plotz,zd=1.0,tau0=0.85,hd=7.68) # AVax.plot(plotz,modelAv,color=colorlist[t]) metalax.plot(bigz, bigmetal) metalax.fill_between(bigz, bigmetal - bigmetalerr, bigmetal + bigmetalerr, alpha=0.1) metalax.legend(loc=1, title="radius [kpc]", scatterpoints=1, numpoints=1, frameon=False) metalax.set_xlim(-0.1, 2.6) # metalax.set_ylim(-1.5,3.0) metalax.set_ylabel(r"Log( $Z/Z_{\odot}$ )") if reg: psi0ax.plot(bigz, bigpsi0) psi0ax.fill_between(bigz, bigpsi0 - bigpsi0err, bigpsi0 + bigpsi0err, alpha=0.1) psi0ax.legend(loc=1, title="radius [kpc]", scatterpoints=1, numpoints=1, frameon=False) psi0ax.set_xlim(-0.1, 2.6) psi0ax.set_ylabel(r"Log($\psi_0$)") tausfax.plot(bigz, bigtausf) tausfax.fill_between(bigz, bigtausf - bigtausferr, bigtausf + bigtausferr, alpha=0.1) tausfax.legend(loc=1, title="radius [kpc]", scatterpoints=1, numpoints=1, frameon=False) tausfax.set_ylim(-1, 5) tausfax.set_xlim(-0.1, 2.6) tausfax.set_ylabel(r"$\tau_{\mathrm{sf}}$") invtausfax.plot(bigz, biginvtausf) invtausfax.fill_between(bigz, biginvtausf - biginvtausferr, biginvtausf + biginvtausferr, alpha=0.1) invtausfax.legend(loc=1, title="radius [kpc]", scatterpoints=1, numpoints=1, frameon=False) invtausfax.set_ylim(-1, 5) invtausfax.set_xlim(-0.1, 2.6) invtausfax.set_ylabel(r"$1/\tau_{\mathrm{sf}}$") tformax.plot(bigz, bigtform) tformax.fill_between(bigz, bigtform - bigtformerr, bigtform + bigtformerr, alpha=0.1) tformax.legend(loc=1, title="radius [kpc]", scatterpoints=1, numpoints=1, frameon=False) tformax.set_xlim(-0.1, 2.6) tformax.set_ylabel(r"$t_{\mathrm{form}}$") massax.plot(bigz, bigmass) massax.fill_between(bigz, bigmass - bigmasserr, bigmass + bigmasserr, alpha=0.1) massax.legend(loc=1, title="radius [kpc]", scatterpoints=1, numpoints=1, frameon=False) massax.set_xlim(-0.1, 2.6) massax.set_ylabel(r"Log($M_{\mathrm{total}}$)") pp.savefig(ax.figure) pp.savefig(AVax.figure) pp.savefig(metalax.figure) plt.close(ax.figure) plt.close(AVax.figure) plt.close(metalax.figure) if reg: pp.savefig(psi0ax.figure) pp.savefig(tausfax.figure) pp.savefig(tformax.figure) pp.savefig(invtausfax.figure) pp.savefig(massax.figure) plt.close(psi0ax.figure) plt.close(tausfax.figure) plt.close(invtausfax.figure) plt.close(tformax.figure) plt.close(massax.figure) pp.close() f.close() return