def run(gp, pop): import gr_params gpr = gr_params.grParams(gp) xall,yall = np.loadtxt(gp.files.get_com_file(0), skiprows=1, \ usecols=(0,1), unpack=True) # 2*[Rscale0] R = np.sqrt(xall**2+yall**2) # [Rscale0] # set number and size of (linearly spaced) bins Rmin = 0. #[Rscale0] Rmax = max(R) if gp.maxR < 0 else 1.0*gp.maxR # [Rscale0] R = R[(R<Rmax)] # [Rscale0] Binmin, Binmax, Rbin = gh.determine_radius(R, Rmin, Rmax, gp) # [Rscale0] gp.xipol = Rbin minr = min(Rbin) # [pc] maxr = max(Rbin) # [pc] gp.xepol = np.hstack([minr/8., minr/4., minr/2., Rbin, 2*maxr, 4*maxr, 8*maxr]) # [pc] Vol = gh.volume_circular_ring(Binmin, Binmax, gp) # [Rscale0^2] Rscale0 = gf.read_Xscale(gp.files.get_scale_file(0)) # [pc] print('####### working on component ',pop) print('input: ', gp.files.get_com_file(pop)) # start from data centered on COM already: if gf.bufcount(gp.files.get_com_file(pop))<2: return # only read in data if needed: pops = 1: reuse data from pop=0 part x,y = np.loadtxt(gp.files.get_com_file(pop), skiprows=1, usecols=(0,1), unpack = True) # [Rscalei], [Rscalei] # calculate 2D radius on the skyplane R = np.sqrt(x**2+y**2) #[Rscalei] Rscalei = gf.read_Xscale(gp.files.get_scale_file(pop)) # [pc] # set maximum radius (if gp.maxR is set) Rmax = max(R) if gp.maxR<0 else 1.0*gp.maxR # [Rscale0] print('Rmax [Rscale0] = ', Rmax) sel = (R * Rscalei <= Rmax * Rscale0) x = x[sel] # [Rscalei] y = y[sel] # [Rscalei] R = R[sel] # [Rscalei] totmass_tracers = float(len(x)) # [Munit], Munit = 1/star Rs = R # + possible starting offset, [Rscalei] tr = open(gp.files.get_ntracer_file(pop),'w') print(totmass_tracers, file=tr) tr.close() f_Sig, f_nu, f_mass, f_sig, f_kap, f_zeta = gf.write_headers_2D(gp, pop) Sig_phot = np.zeros((gp.nipol, gpr.n)) # particle selections, shared by density, siglos, kappa and zeta calculations tpb = np.zeros((gp.nipol,gpr.n)) for k in range(gpr.n): Rsi = gh.add_errors(Rs, gpr.Rerr) # [Rscalei] for i in range(gp.nipol): ind1 = np.argwhere(np.logical_and(Rsi * Rscalei >= Binmin[i] * Rscale0, \ Rsi * Rscalei < Binmax[i] * Rscale0)).flatten() # [1] tpb[i][k] = float(len(ind1)) #[1] Sig_phot[i][k] = float(len(ind1))*totmass_tracers/Vol[i] # [Munit/rscale^2] # do the following for all populations Sig0 = np.sum(Sig_phot[0])/float(gpr.n) # [Munit/Rscale^2] Sig0pc = Sig0/Rscale0**2 # [munis/pc^2] gf.write_Sig_scale(gp.files.get_scale_file(pop), Sig0pc, totmass_tracers) # calculate density and mass profile, store it # ---------------------------------------------------------------------- P_dens = np.zeros(gp.nipol) P_edens = np.zeros(gp.nipol) for b in range(gp.nipol): Sig = np.sum(Sig_phot[b])/(1.*gpr.n) # [Munit/Rscale^2] tpbb = np.sum(tpb[b])/float(gpr.n) # [1], mean number of tracers in bin Sigerr = Sig/np.sqrt(tpbb) # [Munit/Rscale^2], Poissonian error # compare data and analytic profile <=> get stellar # density or mass ratio from Matt Walker if(np.isnan(Sigerr)): P_dens[b] = P_dens[b-1] # [1] P_edens[b]= P_edens[b-1] # [1] else: P_dens[b] = Sig/Sig0 # [1] P_edens[b]= Sigerr/Sig0 # [1] print(Rbin[b], Binmin[b], Binmax[b], P_dens[b], P_edens[b], file=f_Sig) # 3*[rscale], [dens0], [dens0] indr = (R<Binmax[b]) Menclosed = float(np.sum(indr))/totmass_tracers # for normalization to 1#[totmass_tracers] Merr = Menclosed/np.sqrt(tpbb) # or artificial Menclosed/10 #[totmass_tracers] print(Rbin[b], Binmin[b], Binmax[b], Menclosed, Merr, file=f_mass) # [Rscale0], 2* [totmass_tracers] f_Sig.close() f_mass.close() # deproject Sig to get nu numedi = gip.Sig_INT_rho(Rbin*Rscalei, Sig0pc*P_dens, gp) #numin = gip.Sig_INT_rho(Rbin*Rscalei, Sig0pc*(P_dens-P_edens), gp) numax = gip.Sig_INT_rho(Rbin*Rscalei, Sig0pc*(P_dens+P_edens), gp) nu0pc = numedi[0] gf.write_nu_scale(gp.files.get_scale_file(pop), nu0pc) nuerr = numax-numedi for b in range(gp.nipol): print(Rbin[b], Binmin[b], Binmax[b],\ numedi[b]/nu0pc, nuerr[b]/nu0pc, \ file = f_nu) f_nu.close() # write dummy sig scale, not to be used later on maxsiglos = -1. #[km/s] fpars = open(gp.files.get_scale_file(pop),'a') print(maxsiglos, file=fpars) #[km/s] fpars.close()
def run(gp): import gr_params gpr = gr_params.grParams(gp) xall,yall,zall = np.loadtxt(gp.files.get_com_file(0),skiprows=1,\ usecols=(0,1,2),unpack=True) # 2*[rscale0] rscale0 = gf.read_Xscale(gp.files.get_scale_file(0)+'_3D') xall *= rscale0 yall *= rscale0 zall *= rscale0 # calculate 3D r = np.sqrt(xall**2+yall**2+zall**2) #[pc] # set number and size of (linearly spaced) bins rmin = 0. # [pc] rmax = max(r) if gp.maxR < 0 else 1.0*gp.maxR # [pc] print('rmax [rscale] = ', rmax) r = r[(r<rmax)] # [pc] binmin, binmax, rbin = gh.determine_radius(r, rmin, rmax, gp) # [pc] vol = volume_spherical_shell(binmin, binmax, gp) # [pc^3] for pop in range(gp.pops+1): print('####### working on component ',pop) print('input: ',gp.files.get_com_file(pop)+'_3D') # start from data centered on COM already: if gf.bufcount(gp.files.get_com_file(pop)+'_3D')<2: continue x,y,z,v = np.loadtxt(gp.files.get_com_file(pop)+'_3D',\ skiprows=1,usecols=(0,1,2,3),unpack=True) # 3*[rscale], [km/s] rscalei = gf.read_Xscale(gp.files.get_scale_file(pop)) # [pc] x *= rscalei y *= rscalei z *= rscalei # calculate 2D radius on the skyplane r = np.sqrt(x**2+y**2+z**2) # [pc] # set maximum radius (if gp.maxR is set) rmax = max(r) if gp.maxR<0 else 1.0*gp.maxR # [pc] print('rmax [pc] = ', rmax) sel = (r<=rmax) x = x[sel]; y = y[sel]; z = z[sel]; v = v[sel]; r = r[sel] # [rscale] totmass_tracers = 1.*len(x) # [Munit], Munit = 1/star rs = r # + possible starting offset, [rscale] vlos = v # + possible starting offset, [km/s] gf.write_tracer_file(gp.files.get_ntracer_file(pop)+'_3D', totmass_tracers) de, em = gf.write_headers_3D(gp, pop) # gpr.n=30 iterations for getting random picked radius values density = np.zeros((gp.nipol,gpr.n)) a = np.zeros((gp.nipol,gpr.n)) # shared by density, siglos, kappa calcs for k in range(gpr.n): rsi = gpr.Rerr * np.random.randn(len(rs)) + rs # [pc] vlosi = gpr.vrerr*np.random.randn(len(vlos)) + vlos # [km/s] for i in range(gp.nipol): ind1 = np.argwhere(np.logical_and(rsi>=binmin[i], rsi<binmax[i])).flatten() # [1] density[i][k] = (1.*len(ind1))/vol[i]*totmass_tracers # [Munit/rscale^2] vlos1 = vlosi[ind1] # [km/s] a[i][k] = 1.*len(ind1) # [1] dens0 = np.sum(density[0])/(1.*gpr.n) # [Munit/rscale^3] print('dens0 = ',dens0,' [Munit/rscale^3]') dens0pc = dens0/rscale0**3 gf.write_Sig_scale(gp.files.get_scale_file(pop)+'_3D', dens0pc, totmass_tracers) tpb0 = np.sum(a[0])/float(gpr.n) # [1] tracers per bin denserr0 = dens0/np.sqrt(tpb0) # [Munit/rscale^3] p_dens = np.zeros(gp.nipol) p_edens = np.zeros(gp.nipol) for b in range(gp.nipol): dens = np.sum(density[b])/float(gpr.n) # [Munit/rscale^3] tpb = np.sum(a[b])/float(gpr.n) # [1] denserr = dens/np.sqrt(tpb) # [Munit/rscale^3] if(np.isnan(denserr)): p_dens[b] = p_dens[b-1] # [1] p_edens[b]= p_edens[b-1] # [1] else: p_dens[b] = dens/dens0 # [1] p_edens[b]= denserr/dens0 # [1] #100/rbin would be artificial guess print(rbin[b], binmin[b], binmax[b], p_dens[b], p_edens[b], file=de) # [rscale], 2*[dens0] indr = (r<binmax[b]) menclosed = float(np.sum(indr))/totmass_tracers # for normalization to 1 # [totmass_tracers] merr = menclosed/np.sqrt(tpb) # artificial menclosed/10 # [totmass_tracers] print(rbin[b], binmin[b], binmax[b], menclosed, merr, file=em) # [rscale], 2*[totmass_tracers] de.close() em.close() if gpr.showplots: print('plotting for pop ', pop) #show_plots_dens(rbin, p_dens, p_edens, gp) mf1 = 0.02 #1/totmass_tracers mf2 = 0.02 rho_dm, rho_star1, rho_star2 = ga.rho_walk(rbin*rscale0, gp, mf1, mf2) if pop == 0: loglog(rbin*rscale0, rho_star1+rho_star2, 'k.-', lw=0.5) elif pop == 1: loglog(rbin*rscale0, rho_star1, 'b.-', lw = 0.5) elif pop == 2: loglog(rbin*rscale0, rho_star2, 'g.-', lw = 0.5) loglog(rbin*rscale0, dens0pc*p_dens, 'r.-') pdb.set_trace() clf()
def run(gp): import gr_params gpr = gr_params.grParams(gp) for pop in range(2): # get radius, used for all binning print('input: ', gp.files.get_com_file(pop)) if gf.bufcount(gp.files.get_com_file(pop))<2: return x,y,vlos = np.loadtxt(gp.files.get_com_file(pop), skiprows=1, unpack=True) #2*[rscale], [km/s] # totmass_tracers = 1.*len(x) # [Munit], [Munit], where each star is weighted with the same mass r = np.sqrt(x*x+y*y) # [rscale] #set binning #gp.nipol = (max - min)*N^(1/3)/(2*(Q3-Q1)) #(method of wand) rmin = 0. # [rscale] rmax = max(r) if gp.maxR < 0 else 1.0*gp.maxR # [rscale] binmin, binmax, rbin = gh.determine_radius(r, rmin, rmax, gp) # [rscale0] # offset from the start! rs = gpr.Rerr*np.random.randn(len(r))+r #[rscale] vlos = gpr.vrerr*np.random.randn(len(vlos))+vlos #[km/s] vfil = open(gp.files.sigfiles[pop], 'w') print('r', 'sigma_r(r)', 'error', file=vfil) # 30 iterations for drawing a given radius in bin dispvelocity = np.zeros((gp.nipol,gpr.n)) a = np.zeros((gp.nipol,gpr.n)) p_dvlos = np.zeros(gp.nipol) p_edvlos = np.zeros(gp.nipol) for k in range(gpr.n): rsi = gpr.Rerr*np.random.randn(len(rs))+rs #[rscale] vlosi = gpr.vrerr*np.random.randn(len(vlos))+vlos #[km/s] for i in range(gp.nipol): ind1 = np.argwhere(np.logical_and(rsi>binmin[i],rsi<binmax[i])).flatten() a[i][k] = len(ind1) #[1] vlos1 = vlosi[ind1] #[km/s] if(len(ind1)<=1): dispvelocity[i][k] = dispvelocity[i-1][k] # attention! should be 0, uses last value else: dispvelocity[i][k] = meanbiweight(vlos1,ci_perc=68.4,\ ci_mean=True,ci_std=True)[1] # [km/s], see BiWeight.py for i in range(gp.nipol): dispvel = np.sum(dispvelocity[i])/gpr.n #[km/s] ab = np.sum(a[i])/(1.*gpr.n) #[1] if ab == 0: dispvelerr = p_edvlos[i-1] #[km/s] # attention! uses last error else: dispvelerr = dispvel/np.sqrt(ab) #[km/s] p_dvlos[i] = dispvel #[km/s] p_edvlos[i]= dispvelerr #[km/s] maxsiglos = max(p_dvlos) #[km/s] print('maxsiglos = ',maxsiglos,'[km/s]') fpars = open(gp.files.get_scale_file(pop),'a') print(maxsiglos, file=fpars) #[km/s] fpars.close() #import shutil #shutil.copy2(gp.files.get_scale_file(0), gp.files.get_scale_file(1)) for i in range(gp.nipol): # [rscale] [maxsiglos] [maxsiglos] print(rbin[i], binmin[i], binmax[i], np.abs(p_dvlos[i]/maxsiglos),np.abs(p_edvlos[i]/maxsiglos), file=vfil) #/np.sqrt(n)) vfil.close()
def run(gp): pop = 0 import gr_params gpr = gr_params.grParams(gp) xall, yall = np.loadtxt(gp.files.get_com_file(0), skiprows=1, usecols=(0, 1), unpack=True) # 2*[Rscale0] R = np.sqrt(xall**2 + yall**2) # [Rscale0] # set number and size of (linearly spaced) bins Rmin = 0. # [Rscale0] Rmax = max(R) if gp.maxR < 0 else 1.0 * gp.maxR # [Rscale0] R = R[(R < Rmax)] # [Rscale0] Binmin, Binmax, Rbin = gh.determine_radius(R, Rmin, Rmax, gp) # [Rscale0] gp.xipol = Rbin minr = min(Rbin) # [pc] maxr = max(Rbin) # [pc] gp.xepol = np.hstack( [minr / 8., minr / 4., minr / 2., Rbin, 2 * maxr, 4 * maxr, 8 * maxr]) # [pc] Vol = gh.volume_circular_ring(Binmin, Binmax, gp) # [Rscale0^2] Rscale0 = float(gf.read_Xscale(gp.files.get_scale_file(0))) # [pc] print('####### working on component ', pop) print('input: ', gp.files.get_com_file(pop)) # start from data centered on COM already: if gf.bufcount(gp.files.get_com_file(pop)) < 2: return # only read in data if needed: pops = 1: reuse data from pop=0 part x, y = np.loadtxt(gp.files.get_com_file(pop), skiprows=1, usecols=(0, 1), unpack=True) # [Rscalei], [Rscalei] # calculate 2D radius on the skyplane R = np.sqrt(x**2 + y**2) #[Rscalei] Rscalei = gf.read_Xscale(gp.files.get_scale_file(pop)) # [pc] # set maximum radius (if gp.maxR is set) Rmax = max(R) if gp.maxR < 0 else 1.0 * gp.maxR # [Rscale0] print('Rmax [Rscale0] = ', Rmax) sel = (R * Rscalei <= Rmax * Rscale0) x = x[sel] # [Rscalei] y = y[sel] # [Rscalei] R = R[sel] # [Rscalei] totmass_tracers = float(len(x)) # [Munit], Munit = 1/star Rs = R # + possible starting offset, [Rscalei] tr = open(gp.files.get_ntracer_file(pop), 'w') print(totmass_tracers, file=tr) tr.close() f_Sig, f_nu, f_mass, f_sig, f_kap, f_zeta = gf.write_headers_2D(gp, pop) Sig_phot = np.zeros((gp.nipol, gpr.n)) # particle selections, shared by density, siglos, kappa and zeta calculations tpb = np.zeros((gp.nipol, gpr.n)) for k in range(gpr.n): Rsi = gh.add_errors(Rs, gpr.Rerr) # [Rscalei] for i in range(gp.nipol): ind1 = np.argwhere(np.logical_and(Rsi * Rscalei >= Binmin[i] * Rscale0, \ Rsi * Rscalei < Binmax[i] * Rscale0)).flatten() # [1] tpb[i][k] = float(len(ind1)) #[1] Sig_phot[i][k] = float( len(ind1)) * totmass_tracers / Vol[i] # [Munit/rscale^2] # do the following for all populations Sig0 = np.sum(Sig_phot[0]) / float(gpr.n) # [Munit/Rscale^2] Sig0pc = Sig0 / Rscale0**2 # [munis/pc^2] gf.write_Sig_scale(gp.files.get_scale_file(pop), Sig0pc, totmass_tracers) # calculate density and mass profile, store it # ---------------------------------------------------------------------- P_dens = np.zeros(gp.nipol) P_edens = np.zeros(gp.nipol) for b in range(gp.nipol): Sig = np.sum(Sig_phot[b]) / (1. * gpr.n) # [Munit/Rscale^2] tpbb = np.sum(tpb[b]) / float( gpr.n) # [1], mean number of tracers in bin Sigerr = Sig / np.sqrt(tpbb) # [Munit/Rscale^2], Poissonian error # compare data and analytic profile <=> get stellar # density or mass ratio from Matt Walker if (np.isnan(Sigerr)): P_dens[b] = P_dens[b - 1] # [1] P_edens[b] = P_edens[b - 1] # [1] else: P_dens[b] = Sig / Sig0 # [1] P_edens[b] = Sigerr / Sig0 # [1] print(Rbin[b], Binmin[b], Binmax[b], P_dens[b], P_edens[b], file=f_Sig) # 3*[rscale], [dens0], [dens0] indr = (R < Binmax[b]) Menclosed = float( np.sum(indr) ) / totmass_tracers # for normalization to 1#[totmass_tracers] Merr = Menclosed / np.sqrt( tpbb) # or artificial Menclosed/10 #[totmass_tracers] print(Rbin[b], Binmin[b], Binmax[b], Menclosed, Merr, file=f_mass) # [Rscale0], 2* [totmass_tracers] f_Sig.close() f_mass.close() # deproject Sig to get nu numedi = gip.Sig_INT_rho(Rbin * Rscalei, Sig0pc * P_dens, gp) #numin = gip.Sig_INT_rho(Rbin*Rscalei, Sig0pc*(P_dens-P_edens), gp) numax = gip.Sig_INT_rho(Rbin * Rscalei, Sig0pc * (P_dens + P_edens), gp) nu0pc = numedi[0] gf.write_nu_scale(gp.files.get_scale_file(pop), nu0pc) nuerr = numax - numedi for b in range(gp.nipol): print(Rbin[b], Binmin[b], Binmax[b],\ numedi[b]/nu0pc, nuerr[b]/nu0pc, \ file = f_nu) f_nu.close() # write dummy sig scale, not to be used later on maxsiglos = -1. #[km/s] fpars = open(gp.files.get_scale_file(pop), 'a') print(maxsiglos, file=fpars) #[km/s] fpars.close()
def run(): Rscale = [] Dens0Rscale = [] Dens0pc = [] Totmass_Tracers = [] Maxvlos = [] rscale = [] dens0Rscale = [] dens0pc = [] totmass_tracers = [] maxvlos = [] for comp in range(3): A = np.loadtxt(gp.files.get_scale_file(comp), unpack=False, skiprows=1) Rscale.append(A[0]) Dens0Rscale.append(A[1]) Dens0pc.append(A[2]) Totmass_Tracers.append(A[3]) B = np.loadtxt(gp.files.get_scale_file(comp) + '_3D', unpack=False, skiprows=1) rscale.append(B[0]) dens0Rscale.append(B[1]) dens0pc.append(B[2]) totmass_tracers.append(B[3]) print('####### working on component ', comp) print('input: ', gp.files.get_com_file(comp) + '_3D') # start from data centered on COM already: if gf.bufcount(gp.files.get_com_file(comp) + '_3D') < 2: continue Rbin,Binmin,Binmax,Dens,Denserr = np.loadtxt(gp.files.nufiles[comp],\ skiprows=1,usecols=(0,1,2,3,4),\ unpack=True) # 3*[Rscale], [km/s] Rbin *= Rscale[comp] Binmin *= Rscale[comp] Binmax *= Rscale[comp] Dens *= Dens0pc[comp] Denserr *= Dens0pc[comp] rbin,binmin,binmax,dens,denserr = np.loadtxt(gp.files.nufiles[comp]+'_3D',\ skiprows=1,usecols=(0,1,2,3,4),\ unpack=True) # 3*[Rscale], [km/s] rbin *= rscale[comp] binmin *= rscale[comp] binmax *= rscale[comp] dens *= dens0pc[comp] denserr *= dens0pc[comp] ion() f = figure(figsize=(6, 3)) ax1 = f.add_subplot(121) # Nu ax2 = f.add_subplot(122, sharex=ax1) # nu ax1.plot(Rbin, Dens, 'b', lw=1) lbound = Dens - Denserr lbound[lbound < 1e-6] = 1e-6 ubound = Dens + Denserr ax1.fill_between(Rbin, lbound, ubound, alpha=0.5, color='r') ax1.set_yscale('log') ax1.set_xlim([0, np.max(Binmax)]) ax1.set_ylim([np.min(lbound), np.max(ubound)]) ax1.set_xlabel(r'$R [R_c]$') ax1.set_ylabel(r'$\nu_{2D}(R)/\nu_{2D}(0)$') try: ax1.plot(Rbin, rho_INT_Rho(Rbin, dens, denserr)) ax1.plot( Rbin, rho_INT_Rho(Rbin, Rho_INT_rho(Rbin, Dens, Denserr), denserr)) except Exception as detail: print('rho_INT_Rho giving NaN in plotting') draw() ax2.plot(rbin, dens, 'b', lw=1) lbound = dens - denserr lbound[lbound < 1e-6] = 1e-6 ubound = dens + denserr ax2.fill_between(rbin, lbound, ubound, alpha=0.5, color='r') ax2.set_yscale('log') ax2.set_xlim([0, np.max(binmax)]) ax2.set_ylim([np.min(lbound), np.max(ubound)]) ax2.set_xlabel(r'$r [R_c]$') ax2.set_ylabel(r'$\nu(r)/\nu(0)$') ax2.yaxis.tick_right() ax2.yaxis.set_label_position("right") draw() # projNu = rho_INT_Rho(rbin, dens) # projNu = test(rbin, binmin, binmax, dens) # ax1.plot(rbin, projNu) ax2.plot(rbin, Rho_INT_rho(Rbin, Dens, Denserr), color='green') draw() pdb.set_trace() ioff() show()
def run(gp): import gr_params gpr = gr_params.grParams(gp) xall,yall = np.loadtxt(gp.files.get_com_file(0), skiprows=1, usecols=(0,1), unpack=True) # 2*[Rscale0] R = np.sqrt(xall**2+yall**2) # [Rscale0] # set number and size of (linearly spaced) bins Rmin = 0. #[Rscale0] Rmax = max(R) if gp.maxR < 0 else 1.0*gp.maxR # [Rscale0] R = R[(R<Rmax)] # [Rscale0] Binmin, Binmax, Rbin = gh.determine_radius(R, Rmin, Rmax, gp) # [Rscale0] gp.xipol = Rbin minr = min(Rbin) # [pc] maxr = max(Rbin) # [pc] gp.xepol = np.hstack([minr/8., minr/4., minr/2., Rbin, 2*maxr, 4*maxr, 8*maxr]) # [pc] Vol = gh.volume_circular_ring(Binmin, Binmax, gp) # [Rscale0^2] Rscale0 = gf.read_Xscale(gp.files.get_scale_file(0)) # [pc] for pop in range(gp.pops+1): print('####### working on component ',pop) print('input: ', gp.files.get_com_file(pop)) # exclude second condition if self-consistent approach wished if gp.investigate == "obs" and gp.case==1 and pop==0: # for Fornax, overwrite first Sigma with deBoer data import gr_MCMCbin_for gr_MCMCbin_for.run(gp) continue # start from data centered on COM already: if gf.bufcount(gp.files.get_com_file(pop))<2: continue # only read in data if needed: pops = 1: reuse data from pop=0 part if (gp.pops == 1 and pop < 1 or gp.pops == 2) or gp.investigate == 'obs': x,y,v = np.loadtxt(gp.files.get_com_file(pop), skiprows=1,usecols=(0,1,2),unpack=True) # [Rscalei], [Rscalei], [km/s] # calculate 2D radius on the skyplane R = np.sqrt(x**2+y**2) #[Rscalei] Rscalei = gf.read_Xscale(gp.files.get_scale_file(pop)) # [pc] # set maximum radius (if gp.maxR is set) Rmax = max(R) if gp.maxR<0 else 1.0*gp.maxR # [Rscale0] print('Rmax [Rscale0] = ', Rmax) #pdb.set_trace() #from pylab import clf, hist, axvline, xlim #clf() #hist(np.log10(R*Rscalei), 40) #for i in range(len(Rbin)): # axvline(np.log10(Rbin[i]*Rscale0)) #xlim([np.log10(min(gp.xepol*Rscale0)), np.log10(max(gp.xepol*Rscale0))]) sel = (R * Rscalei <= Rmax * Rscale0) x = x[sel] y = y[sel] v = v[sel] R = R[sel] # [Rscalei] totmass_tracers = float(len(x)) # [Munit], Munit = 1/star Rs = R # + possible starting offset, [Rscalei] vlos = v # + possible starting offset, [km/s] tr = open(gp.files.get_ntracer_file(pop),'w') print(totmass_tracers, file=tr) tr.close() f_Sig, f_nu, f_mass, f_sig, f_kap, f_zeta = gf.write_headers_2D(gp, pop) if (gp.pops == 1 and pop < 1) or gp.pops == 2 or gp.investigate == 'obs': Sig_kin = np.zeros((gp.nipol, gpr.n)) siglos = np.zeros((gp.nipol, gpr.n)) if gp.usekappa: kappa = np.zeros((gp.nipol, gpr.n)) if gp.usezeta: v2 = np.zeros((gp.nipol, gpr.n)) v4 = np.zeros((gp.nipol, gpr.n)) Ntot = np.zeros(gpr.n) zetaa = np.zeros(gpr.n) zetab = np.zeros(gpr.n) # particle selections, shared by density, siglos, kappa and zeta calculations tpb = np.zeros((gp.nipol,gpr.n)) for k in range(gpr.n): Rsi = gh.add_errors(Rs, gpr.Rerr) # [Rscalei] vlosi = gh.add_errors(vlos, gpr.vrerr) # [km/s] for i in range(gp.nipol): ind1 = np.argwhere(np.logical_and(Rsi * Rscalei >= Binmin[i] * Rscale0, Rsi * Rscalei < Binmax[i] * Rscale0)).flatten() # [1] tpb[i][k] = float(len(ind1)) # [1] Sig_kin[i][k] = float(len(ind1))*totmass_tracers/Vol[i] # [Munit/rscale**2] if(len(ind1)<=1): siglos[i][k] = siglos[i-1][k] print('### using last value, missing data') if gp.usekappa: kappa[i][k] = kappa[i-1][k] # attention! should be 0, uses last value if gp.usezeta: v2[i][k] = v2[i-1][k] v4[i][k] = v4[i-1][k] else: siglos[i][k] = meanbiweight(vlosi[ind1], ci_perc=68.4, \ ci_mean=True, ci_std=True)[1] # [km/s], see BiWeight.py if gp.usekappa: kappa[i][k] = kurtosis(vlosi[ind1], axis=0, \ fisher=False, bias=False) # [1] if gp.usezeta: ave, adev, sdev, var, skew, curt = gh.moments(vlosi[ind1]) v2[i][k] = var v4[i][k] = (curt+3)*var**2 Sigma = Sig_kin[:,k] if gp.usezeta: pdb.set_trace() Ntot[k] = gh.Ntot(Rbin, Sigma, gp) zetaa[k] = gh.starred(Rbin, v4[:,k], Sigma, Ntot[k], gp) v2denom = (gh.starred(Rbin, v2[:,k], Sigma, Ntot[k], gp))**2 zetaa[k] /= v2denom zetab[k] = gh.starred(Rbin, v4[:,k]*Rbin**2, Sigma, Ntot[k], gp) zetab[k] /= v2denom zetab[k] /= (gh.starred(Rbin, Rbin, Sigma, Ntot[k], gp))**2 if gp.investigate == 'obs' and gp.case < 5: Sig_phot = obs_Sig_phot(Binmin, Binmax, Rscale0, Sig_kin, gp, gpr) else: Sig_phot = Sig_kin # do the following for all populations Sig0 = np.sum(Sig_phot[0])/float(gpr.n) # [Munit/Rscale^2] Sig0pc = Sig0/Rscale0**2 # [munis/pc^2] gf.write_Sig_scale(gp.files.get_scale_file(pop), Sig0pc, totmass_tracers) # calculate density and mass profile, store it # ---------------------------------------------------------------------- #tpb0 = np.sum(tpb[0])/float(gpr.n) # [1] #Sigerr0 = Sig0/np.sqrt(tpb0) # [Munit/Rscale^2] P_dens = np.zeros(gp.nipol) P_edens = np.zeros(gp.nipol) for b in range(gp.nipol): Sig = np.sum(Sig_kin[b])/(1.*gpr.n) # [Munit/Rscale^2] tpbb = np.sum(tpb[b])/float(gpr.n) # [1], mean number of tracers in bin Sigerr = Sig/np.sqrt(tpbb) # [Munit/Rscale^2], Poissonian error # compare data and analytic profile <=> get stellar # density or mass ratio from Matt Walker if(np.isnan(Sigerr)): P_dens[b] = P_dens[b-1] # [1] P_edens[b]= P_edens[b-1] # [1] else: P_dens[b] = Sig/Sig0 # [1] P_edens[b]= Sigerr/Sig0 # [1] print(Rbin[b], Binmin[b], Binmax[b], P_dens[b], P_edens[b], file=f_Sig) # 3*[rscale], [dens0], [dens0] indr = (R<Binmax[b]) Menclosed = float(np.sum(indr))/totmass_tracers # for normalization to 1#[totmass_tracers] Merr = Menclosed/np.sqrt(tpbb) # or artificial Menclosed/10 #[totmass_tracers] print(Rbin[b], Binmin[b], Binmax[b], Menclosed, Merr, file=f_mass) # [Rscale0], 2* [totmass_tracers] f_Sig.close() f_mass.close() # deproject Sig to get nu numedi = gip.Sig_INT_rho(Rbin*Rscalei, Sig0pc*P_dens, gp) #numin = gip.Sig_INT_rho(Rbin*Rscalei, Sig0pc*(P_dens-P_edens), gp) numax = gip.Sig_INT_rho(Rbin*Rscalei, Sig0pc*(P_dens+P_edens), gp) nu0pc = numedi[0] gf.write_nu_scale(gp.files.get_scale_file(pop), nu0pc) nuerr = numax-numedi for b in range(gp.nipol): print(Rbin[b], Binmin[b], Binmax[b], numedi[b]/nu0pc, nuerr[b]/nu0pc, file = f_nu) f_nu.close() # calculate and output siglos # -------------------------------------------- p_dvlos = np.zeros(gp.nipol) p_edvlos = np.zeros(gp.nipol) for b in range(gp.nipol): sig = np.sum(siglos[b])/gpr.n #[km/s] tpbb = np.sum(tpb[b])/float(gpr.n) #[1] if tpbb == 0: sigerr = p_edvlos[b-1] #[km/s] # attention! uses last error else: # Poisson error with measurement errors #sigerr = sig/np.sqrt(tpbb) #sigerr = np.sqrt(sigerr**2+2**2) # 2km/s # standard deviation #sigerr = stddevbiweight(siglos[b]) # Poisson error, first guess sigerr = sig/np.sqrt(tpbb) #[km/s] p_dvlos[b] = sig #[km/s] p_edvlos[b]= sigerr #[km/s] maxsiglos = max(p_dvlos) #[km/s] print('maxsiglos = ', maxsiglos, '[km/s]') fpars = open(gp.files.get_scale_file(pop),'a') print(maxsiglos, file=fpars) #[km/s] fpars.close() for b in range(gp.nipol): print(Rbin[b], Binmin[b], Binmax[b], np.abs(p_dvlos[b]/maxsiglos),\ np.abs(p_edvlos[b]/maxsiglos), file=f_sig) # 3*[rscale], 2*[maxsiglos] f_sig.close() # calculate and output kurtosis kappa # -------------------------------------------- if gp.usekappa: p_kappa = np.zeros(gp.nipol) # needed for plotting later p_ekappa = np.zeros(gp.nipol) for b in range(gp.nipol): kappavel = np.sum(kappa[b])/gpr.n #[1] tpbb = np.sum(tpb[b])/float(gpr.n) #[1] if tpbb == 0: kappavelerr = p_edvlos[b-1] #[1] # attention! uses last error else: kappavelerr = np.abs(kappavel/np.sqrt(tpbb)) #[1] p_kappa[b] = kappavel p_ekappa[b] = kappavelerr print(Rbin[b], Binmin[b], Binmax[b], \ kappavel, kappavelerr, file=f_kap) # [rscale], 2*[1] f_kap.close() # output zetas # ------------------------------------------------------------- if gp.usezeta: print(np.median(zetaa), np.median(zetab), file=f_zeta) f_zeta.close() if gpr.showplots: gpr.show_plots_dens_2D(Rbin*Rscalei, P_dens, P_edens, Sig0pc) gpr.show_plots_sigma(Rbin*Rscalei, p_dvlos, p_edvlos) if gp.usekappa: gpr.show_plots_kappa(Rbin*Rscalei, p_kappa, p_ekappa) # overwrite Sig profile if photometric data is used if gp.investigate == 'obs' and gp.case==1 and pop==1 and not gp.selfconsistentnu: import os os.system('cp '+gp.files.get_scale_file(0)+' '+gp.files.get_scale_file(1)) # replace last line with actual maxsiglos from tracer particles os.system("sed -i '$s/^.*/"+str(maxsiglos)+"/' "+gp.files.get_scale_file(1)) os.system('cp '+gp.files.Sigfiles[0]+' '+gp.files.Sigfiles[1]) continue
def run(): Rscale = []; Dens0Rscale = []; Dens0pc = []; Totmass_Tracers = []; Maxvlos = [] rscale = []; dens0Rscale = []; dens0pc = []; totmass_tracers = []; maxvlos = [] for comp in range(3): A = np.loadtxt(gp.files.get_scale_file(comp), unpack=False, skiprows=1) Rscale.append(A[0]) Dens0Rscale.append(A[1]) Dens0pc.append(A[2]) Totmass_Tracers.append(A[3]) B = np.loadtxt(gp.files.get_scale_file(comp)+'_3D', unpack=False, skiprows=1) rscale.append(B[0]) dens0Rscale.append(B[1]) dens0pc.append(B[2]) totmass_tracers.append(B[3]) print('####### working on component ',comp) print('input: ',gp.files.get_com_file(comp)+'_3D') # start from data centered on COM already: if gf.bufcount(gp.files.get_com_file(comp)+'_3D')<2: continue Rbin,Binmin,Binmax,Dens,Denserr = np.loadtxt(gp.files.nufiles[comp],\ skiprows=1,usecols=(0,1,2,3,4),\ unpack=True) # 3*[Rscale], [km/s] Rbin*=Rscale[comp]; Binmin*=Rscale[comp]; Binmax*=Rscale[comp]; Dens*=Dens0pc[comp]; Denserr*=Dens0pc[comp] rbin,binmin,binmax,dens,denserr = np.loadtxt(gp.files.nufiles[comp]+'_3D',\ skiprows=1,usecols=(0,1,2,3,4),\ unpack=True) # 3*[Rscale], [km/s] rbin*=rscale[comp]; binmin*=rscale[comp]; binmax*=rscale[comp]; dens*=dens0pc[comp]; denserr*=dens0pc[comp] ion() f=figure(figsize=(6,3)) ax1 = f.add_subplot(121) # Nu ax2 = f.add_subplot(122, sharex=ax1) # nu ax1.plot(Rbin, Dens,'b',lw=1) lbound = Dens-Denserr; lbound[lbound<1e-6] = 1e-6 ubound = Dens+Denserr; ax1.fill_between(Rbin,lbound,ubound,alpha=0.5,color='r') ax1.set_yscale('log') ax1.set_xlim([0,np.max(Binmax)]) ax1.set_ylim([np.min(lbound),np.max(ubound)]) ax1.set_xlabel(r'$R [R_c]$') ax1.set_ylabel(r'$\nu_{2D}(R)/\nu_{2D}(0)$') try: ax1.plot(Rbin,rho_INT_Rho(Rbin,dens,denserr)) ax1.plot(Rbin,rho_INT_Rho(Rbin,Rho_INT_rho(Rbin,Dens,Denserr),denserr)) except Exception as detail: print('rho_INT_Rho giving NaN in plotting') draw() ax2.plot(rbin, dens,'b',lw=1) lbound = dens-denserr; lbound[lbound<1e-6] = 1e-6 ubound = dens+denserr; ax2.fill_between(rbin,lbound,ubound,alpha=0.5,color='r') ax2.set_yscale('log') ax2.set_xlim([0,np.max(binmax)]) ax2.set_ylim([np.min(lbound),np.max(ubound)]) ax2.set_xlabel(r'$r [R_c]$') ax2.set_ylabel(r'$\nu(r)/\nu(0)$') ax2.yaxis.tick_right() ax2.yaxis.set_label_position("right") draw() # projNu = rho_INT_Rho(rbin, dens) # projNu = test(rbin, binmin, binmax, dens) # ax1.plot(rbin, projNu) ax2.plot(rbin,Rho_INT_rho(Rbin,Dens,Denserr),color='green') draw() pdb.set_trace() ioff(); show()
def run(gp): import gr_params gpr = gr_params.grParams(gp) xall,yall,zall = np.loadtxt(gp.files.get_com_file(0),skiprows=1,\ usecols=(0,1,2),unpack=True) # 2*[rscale0] rscale0 = gf.read_Xscale(gp.files.get_scale_file(0) + '_3D') xall *= rscale0 yall *= rscale0 zall *= rscale0 # calculate 3D r = np.sqrt(xall**2 + yall**2 + zall**2) #[pc] # set number and size of (linearly spaced) bins rmin = 0. # [pc] rmax = max(r) if gp.maxR < 0 else 1.0 * gp.maxR # [pc] print('rmax [rscale] = ', rmax) r = r[(r < rmax)] # [pc] binmin, binmax, rbin = gh.determine_radius(r, rmin, rmax, gp) # [pc] vol = volume_spherical_shell(binmin, binmax, gp) # [pc^3] for pop in range(gp.pops + 1): print('####### working on component ', pop) print('input: ', gp.files.get_com_file(pop) + '_3D') # start from data centered on COM already: if gf.bufcount(gp.files.get_com_file(pop) + '_3D') < 2: continue x,y,z,v = np.loadtxt(gp.files.get_com_file(pop)+'_3D',\ skiprows=1,usecols=(0,1,2,3),unpack=True) # 3*[rscale], [km/s] rscalei = gf.read_Xscale(gp.files.get_scale_file(pop)) # [pc] x *= rscalei y *= rscalei z *= rscalei # calculate 2D radius on the skyplane r = np.sqrt(x**2 + y**2 + z**2) # [pc] # set maximum radius (if gp.maxR is set) rmax = max(r) if gp.maxR < 0 else 1.0 * gp.maxR # [pc] print('rmax [pc] = ', rmax) sel = (r <= rmax) x = x[sel] y = y[sel] z = z[sel] v = v[sel] r = r[sel] # [rscale] totmass_tracers = 1. * len(x) # [Munit], Munit = 1/star rs = r # + possible starting offset, [rscale] vlos = v # + possible starting offset, [km/s] gf.write_tracer_file( gp.files.get_ntracer_file(pop) + '_3D', totmass_tracers) de, em = gf.write_headers_3D(gp, pop) # gpr.n=30 iterations for getting random picked radius values density = np.zeros((gp.nipol, gpr.n)) a = np.zeros( (gp.nipol, gpr.n)) # shared by density, siglos, kappa calcs for k in range(gpr.n): rsi = gpr.Rerr * np.random.randn(len(rs)) + rs # [pc] vlosi = gpr.vrerr * np.random.randn(len(vlos)) + vlos # [km/s] for i in range(gp.nipol): ind1 = np.argwhere( np.logical_and(rsi >= binmin[i], rsi < binmax[i])).flatten() # [1] density[i][k] = ( 1. * len(ind1)) / vol[i] * totmass_tracers # [Munit/rscale^2] vlos1 = vlosi[ind1] # [km/s] a[i][k] = 1. * len(ind1) # [1] dens0 = np.sum(density[0]) / (1. * gpr.n) # [Munit/rscale^3] print('dens0 = ', dens0, ' [Munit/rscale^3]') dens0pc = dens0 / rscale0**3 gf.write_Sig_scale( gp.files.get_scale_file(pop) + '_3D', dens0pc, totmass_tracers) tpb0 = np.sum(a[0]) / float(gpr.n) # [1] tracers per bin denserr0 = dens0 / np.sqrt(tpb0) # [Munit/rscale^3] p_dens = np.zeros(gp.nipol) p_edens = np.zeros(gp.nipol) for b in range(gp.nipol): dens = np.sum(density[b]) / float(gpr.n) # [Munit/rscale^3] tpb = np.sum(a[b]) / float(gpr.n) # [1] denserr = dens / np.sqrt(tpb) # [Munit/rscale^3] if (np.isnan(denserr)): p_dens[b] = p_dens[b - 1] # [1] p_edens[b] = p_edens[b - 1] # [1] else: p_dens[b] = dens / dens0 # [1] p_edens[ b] = denserr / dens0 # [1] #100/rbin would be artificial guess print(rbin[b], binmin[b], binmax[b], p_dens[b], p_edens[b], file=de) # [rscale], 2*[dens0] indr = (r < binmax[b]) menclosed = float( np.sum(indr)) / totmass_tracers # for normalization to 1 # [totmass_tracers] merr = menclosed / np.sqrt( tpb) # artificial menclosed/10 # [totmass_tracers] print(rbin[b], binmin[b], binmax[b], menclosed, merr, file=em) # [rscale], 2*[totmass_tracers] de.close() em.close() if gpr.showplots: print('plotting for pop ', pop) #show_plots_dens(rbin, p_dens, p_edens, gp) mf1 = 0.02 #1/totmass_tracers mf2 = 0.02 rho_dm, rho_star1, rho_star2 = ga.rho_walk(rbin * rscale0, gp, mf1, mf2) if pop == 0: loglog(rbin * rscale0, rho_star1 + rho_star2, 'k.-', lw=0.5) elif pop == 1: loglog(rbin * rscale0, rho_star1, 'b.-', lw=0.5) elif pop == 2: loglog(rbin * rscale0, rho_star2, 'g.-', lw=0.5) loglog(rbin * rscale0, dens0pc * p_dens, 'r.-') pdb.set_trace() clf()