def scale_einstein_radius(z_lens, z_src, H0=70, Om0=0.3, Ode0=0.7): cosmo = LambdaCDM(H0=H0, Om0=Om0, Ode0=Ode0) d_ls = cosmo.angular_diameter_distance_z1z2(z_lens, z_src) d_s = cosmo.angular_diameter_distance_z1z2(0, z_src) d = d_ls / d_s return d.value
def partial_profile(RA0, DEC0, Z, RIN, ROUT, ndots, h): ndots = int(ndots) cosmo = LambdaCDM(H0=100 * h, Om0=0.25, Ode0=0.75) dl = cosmo.angular_diameter_distance(Z).value KPCSCALE = dl * (((1.0 / 3600.0) * np.pi) / 180.0) * 1000.0 delta = ROUT / (3600 * KPCSCALE) t0 = time.time() mask = (S.ra < (RA0 + delta)) & (S.ra > (RA0 - delta)) & ( S.dec > (DEC0 - delta)) & (S.dec < (DEC0 + delta)) & (S.z_v > (Z + 0.1)) catdata = S[mask] ds = cosmo.angular_diameter_distance(catdata.z_v).value dls = cosmo.angular_diameter_distance_z1z2(Z, catdata.z_v).value BETA_array = dls / ds Dl = dl * 1.e6 * pc sigma_c = (((cvel**2.0) / (4.0 * np.pi * G * Dl)) * (1. / BETA_array)) * (pc**2 / Msun) rads, theta, test1, test2 = eq2p2(np.deg2rad(catdata.ra), np.deg2rad(catdata.dec), np.deg2rad(RA0), np.deg2rad(DEC0)) r = np.rad2deg(rads) * 3600 * KPCSCALE e1 = catdata.gamma1 e2 = -1. * catdata.gamma2 #get tangential ellipticities et = (-e1 * np.cos(2 * theta) - e2 * np.sin(2 * theta)) * sigma_c k = catdata.kappa * sigma_c r = np.rad2deg(rads) * 3600 * KPCSCALE bines = np.logspace(np.log10(RIN), np.log10(ROUT), num=ndots + 1) dig = np.digitize(r, bines) SIGMA = [] DSIGMA = [] for nbin in range(ndots): mbin = dig == nbin + 1 SIGMA = np.append(SIGMA, np.average(k[mbin])) DSIGMA = np.append(DSIGMA, np.average(k[mbin])) return [SIGMA, DSIGMA]
class AstroPyCosmology(CLMMCosmology): def __init__(self, **kwargs): super(AstroPyCosmology, self).__init__(**kwargs) # this tag will be used to check if the cosmology object is accepted by the modeling self.backend = 'ct' assert isinstance(self.be_cosmo, LambdaCDM) def _init_from_cosmo(self, be_cosmo): assert isinstance(be_cosmo, LambdaCDM) self.be_cosmo = be_cosmo def _init_from_params(self, H0, Omega_b0, Omega_dm0, Omega_k0): Om0 = Omega_b0 + Omega_dm0 Ob0 = Omega_b0 self.be_cosmo = FlatLambdaCDM(H0=H0, Om0=Om0, Ob0=Ob0, Tcmb0=2.7255, Neff=3.046, m_nu=([0.06, 0.0, 0.0] * units.eV)) if Omega_k0 != 0.0: Ode0 = self.be_cosmo.Ode0 - Omega_k0 self.be_cosmo = LambdaCDM(H0=H0, Om0=Om0, Ob0=Ob0, Ode0=Ode0, Tcmb0=2.7255, Neff=3.046, m_nu=([0.06, 0.0, 0.0] * units.eV)) def _set_param(self, key, value): raise NotImplementedError("Astropy do not support changing parameters") def _get_param(self, key): if key == "Omega_m0": return self.be_cosmo.Om0 elif key == "Omega_b0": return self.be_cosmo.Ob0 elif key == "Omega_dm0": return self.be_cosmo.Odm0 elif key == "Omega_k0": return self.be_cosmo.Ok0 elif key == 'h': return self.be_cosmo.H0.to_value() / 100.0 elif key == 'H0': return self.be_cosmo.H0.to_value() else: raise ValueError(f"Unsupported parameter {key}") def get_Omega_m(self, z): return self.be_cosmo.Om(z) def get_E2Omega_m(self, z): return self.be_cosmo.Om(z) * (self.be_cosmo.H(z) / self.be_cosmo.H0)**2 def eval_da_z1z2(self, z1, z2): return self.be_cosmo.angular_diameter_distance_z1z2(z1, z2).to_value( units.Mpc) def eval_sigma_crit(self, z_len, z_src): if np.any(np.array(z_src) <= z_len): warnings.warn( f'Some source redshifts are lower than the cluster redshift. Returning Sigma_crit = np.inf for those galaxies.' ) # Constants clight_pc_s = const.CLIGHT_KMS.value * 1000. / const.PC_TO_METER.value gnewt_pc3_msun_s2 = const.GNEWT.value * const.SOLAR_MASS.value / const.PC_TO_METER.value**3 d_l = self.eval_da_z1z2(0, z_len) d_s = self.eval_da_z1z2(0, z_src) d_ls = self.eval_da_z1z2(z_len, z_src) beta_s = np.maximum(0., d_ls / d_s) return clight_pc_s**2 / (4.0 * np.pi * gnewt_pc3_msun_s2) * 1 / d_l * np.divide( 1., beta_s) * 1.0e6
class TestCosmoInterp(object): """ """ def setup(self): self.H0_true = 70 self.omega_m_true = 0.3 self._ok_true = 0.1 self.cosmo = FlatLambdaCDM(H0=self.H0_true, Om0=self.omega_m_true, Ob0=0.05) self.cosmo_interp = CosmoInterp(cosmo=self.cosmo, z_stop=3, num_interp=100) self.cosmo_ok = LambdaCDM(H0=self.H0_true, Om0=self.omega_m_true, Ode0=1.0 - self.omega_m_true - self._ok_true) self.cosmo_interp_ok = CosmoInterp(cosmo=self.cosmo_ok, z_stop=3, num_interp=100) self.cosmo_ok_neg = LambdaCDM(H0=self.H0_true, Om0=self.omega_m_true, Ode0=1.0 - self.omega_m_true + self._ok_true) self.cosmo_interp_ok_neg = CosmoInterp(cosmo=self.cosmo_ok_neg, z_stop=3, num_interp=100) def test_angular_diameter_distance(self): z = 1. da = self.cosmo.angular_diameter_distance(z=[z]) da_interp = self.cosmo_interp.angular_diameter_distance(z=[z]) npt.assert_almost_equal(da_interp / da, 1, decimal=3) assert da.unit == da_interp.unit da = self.cosmo_ok.angular_diameter_distance(z=z) da_interp = self.cosmo_interp_ok.angular_diameter_distance(z=z) npt.assert_almost_equal(da_interp / da, 1, decimal=3) assert da.unit == da_interp.unit da = self.cosmo_ok_neg.angular_diameter_distance(z=z) da_interp = self.cosmo_interp_ok_neg.angular_diameter_distance(z=z) npt.assert_almost_equal(da_interp / da, 1, decimal=3) assert da.unit == da_interp.unit def test_angular_diameter_distance_array(self): # test for array input z1 = 1. z2 = 2. da_z1 = self.cosmo.angular_diameter_distance(z=[z1]) da_z2 = self.cosmo.angular_diameter_distance(z=[z2]) da_interp = self.cosmo_interp.angular_diameter_distance(z=[z1, z2]) npt.assert_almost_equal(da_interp[0] / da_z1, 1, decimal=3) npt.assert_almost_equal(da_interp[1] / da_z2, 1, decimal=3) assert da_z1.unit == da_interp.unit assert len(da_interp) == 2 da_z12 = self.cosmo.angular_diameter_distance(z=[z1, z2]) npt.assert_almost_equal(da_z12[0] / da_z1, 1, decimal=3) npt.assert_almost_equal(da_z12[1] / da_z2, 1, decimal=3) def test_angular_diameter_distance_z1z2(self): z1 = .3 z2 = 2. delta_a = self.cosmo.angular_diameter_distance_z1z2(z1=z1, z2=z2) delta_a_interp = self.cosmo_interp.angular_diameter_distance_z1z2( z1=z1, z2=z2) npt.assert_almost_equal(delta_a_interp / delta_a, 1, decimal=3) assert delta_a.unit == delta_a_interp.unit delta_a = self.cosmo_ok.angular_diameter_distance_z1z2(z1=z1, z2=z2) delta_a_interp = self.cosmo_interp_ok.angular_diameter_distance_z1z2( z1=z1, z2=z2) npt.assert_almost_equal(delta_a_interp / delta_a, 1, decimal=3) assert delta_a.unit == delta_a_interp.unit
Zs = np.repeat(zb[mask1], sum_mask) #DS = cd.angular_diameter_distance(Zs, z0=0, **cosmo) DS = np.array(cosmo.angular_diameter_distance(Zs)) print '#---------- parameters of group cat' #gcat = grupos[ind_g] gcat = grupos[1].data[ind_g] DL = dl[ind_g] Zl = zl[ind_g] del (ind_g) print '#-------------- computing distances' DLS = np.array(cosmo.angular_diameter_distance_z1z2(Zl, Zs)) #DLS=np.zeros(sum_mask.sum()) del (sum_mask) print '-----------------------------------------------------------------' print ' MAKING THE CATALOGUE FOR GALAXIES ' print '=================================================================' tbhdu = fits.BinTableHDU.from_columns([ fits.Column(name='SE_ID', format='E', array=lcat[:, 0]), fits.Column(name='IM_NUM', format='D', array=lcat[:, 1]), fits.Column(name='RAJ2000', format='D', array=lcat[:, 2]), fits.Column(name='DECJ2000', format='D', array=lcat[:, 3]), fits.Column(name='X_IMAGE', format='E', array=lcat[:, 4]), fits.Column(name='Y_IMAGE', format='E', array=lcat[:, 5]),
def partial_map(RA0, DEC0, Z, angles, ROUT, ndots, h): lsize = int(np.sqrt(ndots)) cosmo = LambdaCDM(H0=100 * h, Om0=0.25, Ode0=0.75) dl = cosmo.angular_diameter_distance(Z).value KPCSCALE = dl * (((1.0 / 3600.0) * np.pi) / 180.0) * 1000.0 delta = (ROUT * 1.5) / (3600 * KPCSCALE) rarray = [[-1. * (ROUT * 1.e-3), (ROUT * 1.e-3)], [-1. * (ROUT * 1.e-3), (ROUT * 1.e-3)]] t0 = time.time() mask = (S.ra < (RA0 + delta)) & (S.ra > (RA0 - delta)) & ( S.dec > (DEC0 - delta)) & (S.dec < (DEC0 + delta)) & (S.z_v > (Z + 0.1)) catdata = S[mask] ds = cosmo.angular_diameter_distance(catdata.z_v).value dls = cosmo.angular_diameter_distance_z1z2(Z, catdata.z_v).value BETA_array = dls / ds Dl = dl * 1.e6 * pc sigma_c = (((cvel**2.0) / (4.0 * np.pi * G * Dl)) * (1. / BETA_array)) * (pc**2 / Msun) rads, theta, test1, test2 = eq2p2(np.deg2rad(catdata.ra), np.deg2rad(catdata.dec), np.deg2rad(RA0), np.deg2rad(DEC0)) theta2 = (2. * np.pi - theta) + np.pi / 2. theta_ra = theta2 theta_ra[theta2 > 2. * np.pi] = theta2[theta2 > 2. * np.pi] - 2. * np.pi e1 = catdata.gamma1 e2 = -1. * catdata.gamma2 #get tangential ellipticities et = (-e1 * np.cos(2 * theta) - e2 * np.sin(2 * theta)) * sigma_c #get cross ellipticities ex = (-e1 * np.sin(2 * theta) + e2 * np.cos(2 * theta)) * sigma_c # ''' k = catdata.kappa * sigma_c wcs.wcs.crval = [RA0, DEC0] dx, dy = wcs.wcs_world2pix(catdata.ra, catdata.dec, 0) dx = dx * KPCSCALE * 1.e-3 dy = dy * KPCSCALE * 1.e-3 # del(e1) # del(e2) r = np.rad2deg(rads) * 3600 * KPCSCALE del (rads) Ntot = len(catdata) # del(catdata) t = np.tile(theta_ra, (3, 1)).T an = np.tile(angles, (len(theta_ra), 1)) at = t - an GTsum = np.zeros((ndots, 3)) GXsum = np.zeros((ndots, 3)) Ksum = np.zeros((ndots, 3)) Ninbin = np.zeros((ndots, 3)) for j in range(3): x_t = (dx * np.cos(an[:, j]) + dy * np.sin(an[:, j])) y_t = (-1. * dx * np.sin(an[:, j]) + dy * np.cos(an[:, j])) m = (abs(x_t) < (ROUT * 1.e-3)) * (abs(y_t) < (ROUT * 1.e-3)) out = stats.binned_statistic_2d(x_t[m], y_t[m], et[m], 'count', bins=lsize, range=rarray) Ninbin[:, j] = out.statistic.flatten() out = stats.binned_statistic_2d(x_t[m], y_t[m], et[m], 'sum', bins=lsize, range=rarray) GTsum[:, j] = out.statistic.flatten() out = stats.binned_statistic_2d(x_t[m], y_t[m], ex[m], 'sum', bins=lsize, range=rarray) GXsum[:, j] = out.statistic.flatten() out = stats.binned_statistic_2d(x_t[m], y_t[m], k[m], 'sum', bins=lsize, range=rarray) Ksum[:, j] = out.statistic.flatten() Ntot = m.sum() output = { 'GTsum': GTsum, 'GXsum': GXsum, 'Ksum': Ksum, 'Ninbin': Ninbin, 'Ntot': Ntot } return output
def partial_profile(RA0,DEC0,Z,angles, RIN,ROUT,ndots,h,roff,phi_off): ndots = int(ndots) cosmo = LambdaCDM(H0=100*h, Om0=0.25, Ode0=0.75) dl = cosmo.angular_diameter_distance(Z).value KPCSCALE = dl*(((1.0/3600.0)*np.pi)/180.0)*1000.0 delta = ROUT/(3600*KPCSCALE) t0 = time.time() mask = (S.ra < (RA0+delta))&(S.ra > (RA0-delta))&(S.dec > (DEC0-delta))&(S.dec < (DEC0+delta))&(S.z_v > (Z+0.1)) t1 = time.time() # print(t1-t0) catdata = S[mask] ds = cosmo.angular_diameter_distance(catdata.z_v).value dls = cosmo.angular_diameter_distance_z1z2(Z, catdata.z_v).value BETA_array = dls/ds Dl = dl*1.e6*pc sigma_c = (((cvel**2.0)/(4.0*np.pi*G*Dl))*(1./BETA_array))*(pc**2/Msun) t2 = time.time() # print(RA0,DEC0,t2-t1) rads, theta, test1,test2 = eq2p2(np.deg2rad(catdata.ra), np.deg2rad(catdata.dec), np.deg2rad(RA0), np.deg2rad(DEC0)) theta2 = (2.*np.pi - theta) +np.pi/2. theta_ra = theta2 theta_ra[theta2 > 2.*np.pi] = theta2[theta2 > 2.*np.pi] - 2.*np.pi e1 = catdata.gamma1 e2 = -1.*catdata.gamma2 #get tangential ellipticities et = (-e1*np.cos(2*theta)-e2*np.sin(2*theta))*sigma_c #get cross ellipticities ex = (-e1*np.sin(2*theta)+e2*np.cos(2*theta))*sigma_c # ''' k = catdata.kappa*sigma_c del(e1) del(e2) r = np.rad2deg(rads)*3600*KPCSCALE r = np.sqrt(roff**2 + r**2 + 2.*roff*r*np.cos(phi_off)) del(rads) Ntot = len(catdata) del(catdata) bines = np.round(np.logspace(np.log10(RIN),np.log10(ROUT),num=ndots+1),0) dig = np.digitize(r,bines) t = np.tile(theta_ra,(3,1)).T an = np.tile(angles,(len(theta_ra),1)) at = t - an SIGMAwsum = [] DSIGMAwsum_T = [] DSIGMAwsum_X = [] N_inbin = [] GAMMATcos_wsum = np.zeros((ndots,3)) GAMMAXsin_wsum = np.zeros((ndots,3)) COS2_2theta = np.zeros((ndots,3)) SIN2_2theta = np.zeros((ndots,3)) for nbin in range(ndots): mbin = dig == nbin+1 SIGMAwsum = np.append(SIGMAwsum,k[mbin].sum()) DSIGMAwsum_T = np.append(DSIGMAwsum_T,et[mbin].sum()) DSIGMAwsum_X = np.append(DSIGMAwsum_X,ex[mbin].sum()) GAMMATcos_wsum[nbin,:] = np.sum((np.tile(et[mbin],(3,1))*np.cos(2.*at[mbin]).T),axis=1) GAMMAXsin_wsum[nbin,:] = np.sum((np.tile(ex[mbin],(3,1))*np.sin(2.*at[mbin]).T),axis=1) COS2_2theta[nbin,:] = np.sum((np.cos(2.*at[mbin]).T)**2,axis=1) SIN2_2theta[nbin,:] = np.sum((np.sin(2.*at[mbin]).T)**2,axis=1) N_inbin = np.append(N_inbin,len(et[mbin])) index = np.arange(mbin.sum()) output = {'SIGMAwsum':SIGMAwsum,'DSIGMAwsum_T':DSIGMAwsum_T, 'DSIGMAwsum_X':DSIGMAwsum_X, 'GAMMATcos_wsum': GAMMATcos_wsum, 'GAMMAXsin_wsum': GAMMAXsin_wsum, 'COS2_2theta_wsum':COS2_2theta,'SIN2_2theta_wsum':SIN2_2theta, 'N_inbin':N_inbin,'Ntot':Ntot} return output