def n_merge(z1, z2, r_list, m_sat, m_cen): t_between = WMAP9.lookback_time(z1).value - WMAP9.lookback_time(z2).value # t_merge_list = merge_est_k(r_list, m_cen, z1) t_merge_list = merge_est_j(r_list, m_sat, m_cen) # merge m_sat = m_sat[t_merge_list < t_between] m_merge = sum(10**(m_sat - 8)) n_merge = sum(np.ones(len(r_list)) * t_merge_list / t_between) # alternatively # n_merge = r_list[t_merge_list<t_between] return n_merge, m_merge
def cosmoLookBack(redshift, WMAP9=False, H0=70.0, Om0=0.30, Planck15=False, Myr=False): """ Get the Look-back Time at redshift=z. This is simply a wrapper of astropy.cosmology The input redsfhit can be an array """ if WMAP9: from astropy.cosmology import WMAP9 as cosmo elif Planck15: from astropy.cosmology import Planck15 as cosmo else: from astropy.cosmology import FlatLambdaCDM cosmo = FlatLambdaCDM(H0=H0, Om0=Om0) lbt = cosmo.lookback_time(redshift) if not Myr: return lbt.value else: return lbt.to(u.Myr).value
def make_scattered_galaxy(filename, **kwargs): print "attempting to read in ", filename input_galaxy = ascii.read(filename) galaxy = initialize_galaxy(len(input_galaxy)) dt = 1.e7 print "assuming timestep is 1.e7 years!!!!!" galaxy['dt'] += 1.e7*u.yr galaxy['sfr'] = np.power(10.0,input_galaxy['SFR']) * u.Msun/u.yr galaxy['dMrdt'] = np.power(10.0,input_galaxy['Mrec'])* u.Msun / galaxy['dt'] # galaxy['dMrdt'].unit = u.Msun / u.yr ## Need to get gas masses from Gergo's code fake_radius = 3000. + np.zeros(len(galaxy['sfr'])) gas = ig.gas_masses(galaxy['sfr'], input_galaxy['Ms'], fake_radius) Mgas, Mhi, Mh2 = gas[0], gas[1], gas[2] galaxy['Mg'] = Mgas * HELIUM_CORR * u.Msun galaxy['Mhi'] = Mhi * u.Msun galaxy['Mh2'] = Mh2 * u.Msun galaxy['Ms'] = np.power(10.0,input_galaxy['Ms']) * u.Msun col_dummy = Column(name='z',data=input_galaxy['z']) galaxy.add_column(col_dummy) print "ASSUMING PROBABLY A WRONG COSMOLOGY FYI" age = cosmo.age(galaxy['z']) ### THIS COSMOLOGY IS WRONG galaxy['age'] = age tlb = cosmo.lookback_time(galaxy['z']) galaxy['lookback'] = tlb galaxy.remove_column('Mh') galaxy.remove_column('Mcgm') galaxy.remove_column('tlogoh') ## check to make sure sorted so earlier is earlier galaxy.sort('age') print galaxy return galaxy
def read_in_galaxy(filename, index): print "attempting to read in",filename if ".pkl" in filename: print "assuming that",filename,"is a pickle file with only a dict of galaxies in it" galaxies = pickle.load(open(filename,"r")) print "for now we're only dealing with one galaxy at a time; taking galaxy #",index assert(index in galaxies.keys()), "index %i not in file :-(" % index galaxy = galaxies[index] col_z = Column(name='z',data=galaxies['z']) galaxy.add_column(col_z,0) elif "peter" in filename: print "assuming that",filename,"is an ascii file from peter behroozi and gergo popping with Mh,Ms,dMhdt,sfr,Mg,Mhi,Mh2" print "also assuming that",filename,"only has one iteration" z,Mh,Ms,dMhdt,sfr,Mg,Mhi,Mh2 = np.loadtxt(filename,usecols=(0,7*index+1,7*index+2,7*index+3,7*index+4,7*index+5,7*index+6,7*index+7),unpack=True) galaxy = QTable([z,Mh,Ms,dMhdt,sfr,Mg,Mhi,Mh2],names=('z','Mh','Ms','dMhdt','sfr','Mg','Mhi','Mh2')) ## need to add units galaxy['Mh'].unit = u.Msun galaxy['Ms'].unit = u.Msun galaxy['Mg'].unit = u.Msun galaxy['Mhi'].unit = u.Msun galaxy['Mh2'].unit = u.Msun galaxy['dMhdt'].unit = u.Msun / u.yr if 'dMrdt' in galaxy.colnames: galaxy['dMrdt'].unit = u.Msun / u.yr galaxy['sfr'].unit = u.Msun / u.yr ### Helium is not included in the gas masses!! galaxy['Mg'] = galaxy['Mg'] * HELIUM_CORR ### Let's make a CGM! ### galaxy['Mcgm'] = galaxy['Mh']*cosmo.Ob0/cosmo.Om0 - galaxy['Ms'] - galaxy['Mg'] age = cosmo.age(galaxy['z']) col_age = Column(name='age', data=age) galaxy.add_column(col_age,1) tlb = cosmo.lookback_time(galaxy['z']) col_tlb = Column(name='lookback', data=tlb) galaxy.add_column(col_tlb,2) ## check to make sure sorted so earlier is earlier galaxy.sort('age') return galaxy
chival = np.sum((obj_fluxes/obj_fluxerrs - const*th_fluxes/obj_fluxerrs)**2) print chival raw_input() if chival < output[m,7]: output[m,:] = np.array([m+1, all_obj_specz[m], z, ages[j], float(tauvals[l]), EBV, const, chival]) print "Age: " + str(ages[j]) + ", tau: " + tauvals[l] + ", redshift: " + str(z) np.savetxt("photoz_grid84876.txt", output, header="obj_no spec_z phot_z age tau EBV norm chi") """ #the chi squared array is now 2D over EBV and the different objects for j in range(128): for l in range(3): th_mag_array = np.loadtxt("synmags_T" + tauvals[l] + "/synmags/synmags_age_" + str(ages[j]) + ".txt", usecols=(1,2,3,4,5,6,7,8,9,10,11,12)) for i in range(1, 501): z = 0.01*i th_mags = th_mag_array[i-1,:] if ages[j]*(10**-9) < 14.00 - WMAP9.lookback_time(z).value: for k in range(41): EBV = 0.05*k th_fluxes = 10**((23.9 - EBV*coef - th_mags)/2.5) #microjanskys const = np.sum(fulldet_obj_fluxes*th_fluxes/fulldet_obj_fluxerrs**2, axis=1)/np.sum(th_fluxes**2/fulldet_obj_fluxerrs**2, axis=1) chival = np.sum((fulldet_obj_fluxes/fulldet_obj_fluxerrs - (const*(th_fluxes/fulldet_obj_fluxerrs).T).T)**2, axis=1) for m in range(len(fulldet_obj_fluxes)): if chival[m] < output[m,7]: output[m,:] = np.array([m+1, all_obj_specz[m], z, ages[j], float(tauvals[l]), EBV, const[m], chival[m]]) print "Age: " + str(ages[j]) + ", tau: " + tauvals[l] + ", redshift: " + str(z) np.savetxt("photoz_grid86548.txt", output, header="obj_no spec_z phot_z age tau EBV norm chi")
offset_errs[0, i, 0] = 0. all_obj_fluxes = all_obj_fluxes + offsets all_obj_fluxerrs = np.sqrt(all_obj_fluxerrs**2 + offset_errs**2) ### Build array for photometric redshift and parameter outputs output = np.zeros(len(all_obj_fluxes)*8, dtype="float") output.shape = (len(all_obj_fluxes), 8) output[:,7] = 9999999999. ### Perform model fitting using a 2D chi squared array over object and redshift with max age of stellar pop physically determined tauvals = np.array([0.05, 1, 10], dtype="str") zarr = np.arange(0.01, 5.001, 0.01) lbtarr = WMAP9.lookback_time(zarr).value EBVarr = np.expand_dims(np.expand_dims(np.expand_dims(np.arange(0, 1.50001, 0.025), axis=0), axis=0), axis=0) for j in range(minage, maxage+1): arg = 0 while ages[j]*(10**-9) < 14.0 - lbtarr[arg] and arg < 499: arg = arg+1 for l in range(3): print "Age: " + str(ages[j]) + ", fitting to redshift " + str((arg+1)*0.01) + ", with tau: " + tauvals[l] th_mag_array = np.expand_dims(np.expand_dims(np.loadtxt("models/" + tauvals[l] + "/synmags_age_" + str(ages[j]) + ".txt", usecols=(1,2,3,4,5,6,7,8,9,10,11,12))[0:arg, :].T, axis=0), axis=3) print "th_mag_array computed" print th_mag_array.nbytes th_flux_array = (10**((23.9 - EBVarr*coef - th_mag_array)/2.5)) #microjanskys print "th_flux_array computed" print th_flux_array.nbytes const = np.expand_dims(np.sum(all_obj_fluxes*th_flux_array/all_obj_fluxerrs**2, axis=1)/np.sum(th_flux_array**2/all_obj_fluxerrs**2, axis=1), axis=1)
PROTONMASS = 1.67262178e-27 #Thomson constant THOMPSON = 6.65245873e-29 #1e8 Solar units E8MSUN = 1.989e38 #Seconds per year SECPERYEAR = 3.15569e7 #Solar mass SOLARMASS = 1.989e30 #Standard units unitsstd = {'M': 1.989e40, 'L': 3.08568e19, 'T': 3.15569e16, 'V': 1000.} #Creating time for cosmological setups atmp = np.arange(0.001, 1.02, 0.001) t_tmp = WMAP9.lookback_time(1.0 / atmp[0] - 1) - WMAP9.lookback_time(1.0 / atmp - 1) def physical_time(a): time = np.interp(a, atmp, t_tmp) return time #======================================================================================== # FUNCTIONS #======================================================================================== #RK4 integrator def RK4_step(f, r, v, t, dt): #Creating solutions K0r, K0v = f(r, v, t) K1r, K1v = f(r + 0.5 * dt * K0r, v + 0.5 * dt * K0v, t + 0.5 * dt)
cat_neighbors['MASS_BEST'] ): # exclude central gals which has larger mass companion continue # cat_neighbors = cat_neighbors[cat_neighbors['MASS_BEST']>8] coord_neighbors = SkyCoord(cat_neighbors['RA'] * u.deg, cat_neighbors['DEC'] * u.deg) radius_list = coord_neighbors.separation( coord_gal).degree / 180. * np.pi * dis * 1000 # in kpc mass_list = cat_neighbors['MASS_BEST'] # t_merge_list = merge_est_k(radius_list, mass_list, z) t_merge_list = merge_est_j(radius_list, mass_list, gal['MASS_BEST']) # select companions that merge with central in the next redshift bin t_merge_h = WMAP9.lookback_time( gal['ZPHOT']).value - WMAP9.lookback_time(z - 0.2).value t_merge_l = WMAP9.lookback_time( gal['ZPHOT']).value - WMAP9.lookback_time(z).value mass_grow_list = mass_list[np.absolute(t_merge_list - (t_merge_h + t_merge_l) / 2.) < (t_merge_h - t_merge_l) / 2.] mass_growth_bkg = bkg(gal['RA'], cat_all_z_slice, t_merge_h, t_merge_l, z, gal['MASS_BEST']) mass_growth_gal.append( sum(10**(mass_grow_list - 10)) - mass_growth_bkg) print(len(mass_grow_list), len(mass_list), sum(10**(mass_grow_list - 10)), mass_growth_bkg) mass_growth_z_median = np.median(mass_growth_gal)
filter_bounds, filter_widths = make_bins(filter[:,0], make_rhs="True") top = np.zeros(len(spec_widths)*len(filter_widths)) bottom = np.copy(top) top = (np.expand_dims(filter_bounds[1:], axis=1) - np.expand_dims(spec_bounds[:-1], axis=0))/np.expand_dims(filter_widths, axis=1) bottom = (np.expand_dims(spec_bounds[1:], axis=0) - np.expand_dims(filter_bounds[:-1], axis=1))/np.expand_dims(filter_widths, axis=1) part1 = np.logical_and(top>=1, bottom>=1).astype("float") part2 = np.logical_and(np.logical_and(0<top, top<1), bottom>=1).astype("float") part3 = np.logical_and(np.logical_and(0<bottom, bottom<1), top>=1).astype("float") part4 = np.logical_and(np.logical_and(0<top, top<1), np.logical_and(0<bottom, bottom<1)).astype("float") crossover = part1 + top*part2 + bottom*part3 + (top+bottom-1)*part4 flux_total = np.sum(np.sum(crossover*np.expand_dims(filter[:,1], axis=1)*np.expand_dims(spec[:,1], axis=0)*np.expand_dims(filter_widths, axis=1), axis=1), axis=0) return flux_total zarr = np.arange(0.01, 7.001, 0.01) lbtarr = cosmo.lookback_time(zarr).value D = np.loadtxt("../IGM_Da_Db.txt") ### Load up the data necessary to build the "filter" to rebin the model spectra onto hdulist1 = fits.open("../../VANDELS_data/spectra/sc_206806_UDS_P1M1_MR_Q1_029_1.fits") wavzpt = hdulist1[0].header["CRVAL1"] dwav = hdulist1[0].header["CDELT1"] fluxes1 = hdulist1[4].data maxwav = wavzpt + dwav*(len(fluxes1)) objwavs_nobin = np.arange(wavzpt, maxwav, dwav) objwavs = specbin(objwavs_nobin, 2)
filter_bounds, filter_widths = make_bins(filter[:,0], make_rhs="True") top = np.zeros(len(spec_widths)*len(filter_widths)) bottom = np.copy(top) top = (np.expand_dims(filter_bounds[1:], axis=1) - np.expand_dims(spec_bounds[:-1], axis=0))/np.expand_dims(filter_widths, axis=1) bottom = (np.expand_dims(spec_bounds[1:], axis=0) - np.expand_dims(filter_bounds[:-1], axis=1))/np.expand_dims(filter_widths, axis=1) part1 = np.logical_and(top>=1, bottom>=1).astype("float") part2 = np.logical_and(np.logical_and(0<top, top<1), bottom>=1).astype("float") part3 = np.logical_and(np.logical_and(0<bottom, bottom<1), top>=1).astype("float") part4 = np.logical_and(np.logical_and(0<top, top<1), np.logical_and(0<bottom, bottom<1)).astype("float") crossover = part1 + top*part2 + bottom*part3 + (top+bottom-1)*part4 flux_total = np.sum(np.sum(crossover*np.expand_dims(filter[:,1], axis=1)*np.expand_dims(spec[:,1], axis=0)*np.expand_dims(filter_widths, axis=1), axis=1), axis=0) return flux_total zarr = np.arange(0.01, 5.001, 0.01) lbtarr = cosmo.lookback_time(zarr).value ### Load up the data necessary to build the "filter" to rebin the model spectra onto hdulist1 = fits.open("../VANDELS_data/spectra/sc_206806_UDS_P1M1_MR_Q1_029_1.fits") wavzpt = hdulist1[0].header["CRVAL1"] dwav = hdulist1[0].header["CDELT1"] fluxes1 = hdulist1[4].data#*10**19 print wavzpt print dwav raw_input() maxwav = wavzpt + dwav*(len(fluxes1)) objwavs_nobin = np.arange(wavzpt, maxwav, dwav) tauvals = np.array([0.05, 1, 10], dtype="str")