def vel_kms_to_masyr(vel=1000.0, z=1.0): vel = vel * u.kilometer / u.second vel_mas_per_year = (vel.to(u.kpc / u.yr) * cosmo.arcsec_per_kpc_proper(z).to(u.mas / u.kpc)) if z < 0.1: d = cosmo.angular_diameter_distance(z).to(u.kpc) loc_vel_mas_per_year = ((vel.to(u.kpc / u.yr) / d) * (u.rad)).to( u.mas / u.yr) #table 6 says 0.7 mas is max resolution, for 93GHz band print("D_A (z=%g) = " % z, cosmo.angular_diameter_distance(z)) print("D_L (z=%g) = " % z, cosmo.luminosity_distance(z)) print("arcsec/kpc (z=%g) =" % z, cosmo.arcsec_per_kpc_proper(z)) print("mas/kpc (z=%g) =" % z, cosmo.arcsec_per_kpc_proper(z).to(u.mas / u.kpc)) print("physical res for 10 mas (z=%g) = " % z, 10 * u.mas / (cosmo.arcsec_per_kpc_proper(z).to(u.mas / u.pc))) print("vel [km/s] = %g" % vel.to_value()) print("vel [km/s] =", vel) print("vel [pc/Myr] =", vel.to(u.pc / u.Myr)) print(vel) print("vel [kpc/yr] =", vel.to(u.kpc / u.yr)) print("vel [mas/yr] = ", vel_mas_per_year) if z < 0.1: print("loc vel [mas/yr] = ", loc_vel_mas_per_year) print("motion in 10 yr lifetime = ", vel_mas_per_year * 10 * u.yr) return vel_mas_per_year
def update(val): zL,zS = slzL.val,slzS.val xL,yL = slxL.val, slyL.val ML,eL,PAL = slML.val,sleL.val,slPAL.val sh,sha = slss.val,slsa.val xs,ys = slxs.val, slys.val Fs,ws = slFs.val, slws.val ns,ars,pas = slns.val,slars.val,slPAs.val newDd = cosmo.angular_diameter_distance(zL).value newDs = cosmo.angular_diameter_distance(zS).value newDds= cosmo.angular_diameter_distance_z1z2(zL,zS).value newLens = vl.SIELens(zLens,xL,yL,10**ML,eL,PAL) newShear = vl.ExternalShear(sh,sha) newSource = vl.SersicSource(zS,True,xs,ys,Fs,ws,ns,ars,pas) xs,ys = vl.LensRayTrace(xim,yim,[newLens,newShear],newDd,newDs,newDds) imbg = vl.SourceProfile(xim,yim,newSource,[newLens,newShear]) imlensed = vl.SourceProfile(xs,ys,newSource,[newLens,newShear]) caustics = vl.CausticsSIE(newLens,newDd,newDs,newDds,newShear) ax.cla() ax.imshow(imbg,cmap=cmbg,extent=[xim.min(),xim.max(),yim.min(),yim.max()],origin='lower') ax.imshow(imlensed,cmap=cmlens,extent=[xim.min(),xim.max(),yim.min(),yim.max()],origin='lower') mu = imlensed.sum()*(xim[0,1]-xim[0,0])**2 / newSource.flux['value'] ax.text(0.9,1.05,'$\\mu$ = {0:.2f}'.format(mu),transform=ax.transAxes) for i in range(caustics.shape[0]): ax.plot(caustics[i,0,:],caustics[i,1,:],'k-') f.canvas.draw_idle()
def create_modelimage(lens,source,xmap,ymap,xemit,yemit,indices, Dd=None,Ds=None,Dds=None,sourcedatamap=None): """ Creates a model lensed image given the objects and map coordinates specified. Supposed to be common for both image fitting and visibility fitting, so we don't need any data here. Returns: immap A 2D array representing the field evaluated at xmap,ymap with all sources included. mus: A numpy array of length N_sources containing the magnifications of each source (1 if unlensed). """ lens = list(np.array([lens]).flatten()) # Ensure lens(es) are a list source = list(np.array([source]).flatten()) # Ensure source(s) are a list mus = np.zeros(len(source)) immap, imsrc = np.zeros(xmap.shape), np.zeros(xemit.shape) # If we didn't get pre-calculated distances, figure them here assuming WMAP9 if np.any((Dd is None,Ds is None, Dds is None)): from astropy.cosmology import WMAP9 as cosmo Dd = cosmo.angular_diameter_distance(lens[0].z).value Ds = cosmo.angular_diameter_distance(source[0].z).value Dds= cosmo.angular_diameter_distance_z1z2(lens[0].z,source[0].z).value # Do the raytracing for this set of lens & shear params xsrc,ysrc = LensRayTrace(xemit,yemit,lens,Dd,Ds,Dds) if sourcedatamap is not None: # ... then particular source(s) are specified for this map for jsrc in sourcedatamap: if source[jsrc].lensed: ims = SourceProfile(xsrc,ysrc,source[jsrc],lens) imsrc += ims mus[jsrc] = ims.sum()*(xemit[0,1]-xemit[0,0])**2./source[jsrc].flux['value'] else: immap += SourceProfile(xmap,ymap,source[jsrc],lens); mus[jsrc] = 1. else: # Assume we put all sources in this map/field for j,src in enumerate(source): if src.lensed: ims = SourceProfile(xsrc,ysrc,src,lens) imsrc += ims mus[j] = ims.sum()*(xemit[0,1]-xemit[0,0])**2./src.flux['value'] else: immap += SourceProfile(xmap,ymap,src,lens); mus[j] = 1. # Try to reproduce matlab's antialiasing thing; this uses a 3lobe lanczos low-pass filter imsrc = Image.fromarray(imsrc) resize = np.array(imsrc.resize((int(indices[1]-indices[0]),int(indices[3]-indices[2])),Image.ANTIALIAS)) immap[indices[2]:indices[3],indices[0]:indices[1]] += resize # Flip image to match sky coords (so coordinate convention is +y = N, +x = W, angle is deg E of N) immap = immap[::-1,:] # last, correct for pixel size. immap *= (xmap[0,1]-xmap[0,0])**2. return immap,mus
def get_size_flag(self): self.DA=np.zeros(len(self.s.SERSIC_TH50)) #self.DA[self.membflag] = cosmo.angular_diameter_distance(self.s.CLUSTER_REDSHIFT[self.membflag]).value*Mpcrad_kpcarcsec) for i in range(len(self.DA)): if self.membflag[i]: self.DA[i] = cosmo.angular_diameter_distance(self.s.CLUSTER_REDSHIFT[i]).value*Mpcrad_kpcarcsec else: self.DA[i] = cosmo.angular_diameter_distance(self.s.ZDIST[i]).value*Mpcrad_kpcarcsec self.sizeflag=(self.s.SERSIC_TH50*self.DA > minsize_kpc) #& (self.s.SERSIC_TH50 < 20.)
def get_size_flag(self): self.DA = np.zeros(len(self.s.SERSIC_TH50)) #self.DA[self.membflag] = cosmo.angular_diameter_distance(self.s.CLUSTER_REDSHIFT[self.membflag]).value*Mpcrad_kpcarcsec) for i in range(len(self.DA)): if self.membflag[i]: self.DA[i] = cosmo.angular_diameter_distance( self.s.CLUSTER_REDSHIFT[i]).value * Mpcrad_kpcarcsec else: self.DA[i] = cosmo.angular_diameter_distance( self.s.ZDIST[i]).value * Mpcrad_kpcarcsec self.sizeflag = (self.s.SERSIC_TH50 * self.DA > minsize_kpc ) #& (self.s.SERSIC_TH50 < 20.)
def cal_beta(z_los): beta = np.zeros(len(z_los)) for i in range(len(z_los)): zi = np.array([zl,z_los[i]]) z1,z2 = np.min(zi),np.max(zi) D1,D2 = cosmo.angular_diameter_distance(z1),cosmo.angular_diameter_distance(z2) beta[i] = (D2-D1)*Ds/D2/(Ds-D1) return beta
def calculate_Ue_to_UB_kino2014(nu_ssa_ghz, theta_obs_mas, flux_nu_ssa_obs_jy, delta, z, p, gamma_e_min): """ Calculate B using (11) from Kino et al. 2014 (doi:10.1088/0004-637X/786/1/5). :param nu_ssa_ghz: Frequency of observation (it is SSA frequency for radio core). :param theta_obs_mas: Width of the VLBI core (see paper for discussion). :param flux_nu_ssa_obs_jy: Flux of the core. :param delta: Doppler factor. :param z: Redshift. :param p: Exponent of particles energy distribution. Must be 2.5, 3 or 3.5 here. :param gamma_e_min: :return: Value of the particles energy to B energy ratio. :note: U_B = B_{tot}^2/(8*pi), where B_{tot} = sqrt(3)*B => assumes isotropic tangled magnetic field here. """ if p not in (2.5, 3.0, 3.5): raise Exception("p must be 2.5, 3.0 or 3.5") k_p = {2.5: 1.4 * 10**(-2), 3.0: 2.3 * 10**(-3), 3.5: 3.6 * 10**(-4)} b_p = {2.5: 3.3 * 10**(-5), 3.0: 1.9 * 10**(-5), 3.5: 1.2 * 10**(-5)} E_e_min_erg = (gamma_e_min * const.m_e * const.c**2).to(u.erg).value D_A_Gpc = WMAP9.angular_diameter_distance(z).to(u.Gpc).value return (8*np.pi/(3*b_p[p]**2)) * (k_p[p]*E_e_min_erg**(-p+2)/(p-2)) *\ (D_A_Gpc)**(-1) * nu_ssa_ghz**(-2*p-13) * theta_obs_mas**(-2*p-13) *\ flux_nu_ssa_obs_jy**(p+6) * (delta / (1+z))**(-p-5)
def calculate_K_kino2014(nu_ssa_ghz, theta_obs_mas, flux_nu_ssa_obs_jy, delta, z, p): """ Calculate B using (11) from Kino et al. 2014 (doi:10.1088/0004-637X/786/1/5). :param nu_ssa_ghz: Frequency of observation (it is SSA frequency for radio core). :param theta_obs_mas: Width of the VLBI core (see paper for discussion). :param flux_nu_ssa_obs_jy: Flux of the core. :param delta: Doppler factor. :param z: Redshift. :param p: Exponent of particles energy distribution. Must be 2.5, 3 or 3.5 here. :return: Value of the K_e - normalization factor of the electron energy density distribution [erg**(p-1) * cm**(-3]. """ if p not in (2.5, 3.0, 3.5): raise Exception("p must be 2.5, 3.0 or 3.5") k_p = {2.5: 1.4 * 10**(-2), 3.0: 2.3 * 10**(-3), 3.5: 3.6 * 10**(-4)} D_A_Gpc = WMAP9.angular_diameter_distance(z).to(u.Gpc).value return k_p[p] * (D_A_Gpc)**(-1) * nu_ssa_ghz**(-2*p-3) * theta_obs_mas**(-2*p-5) *\ flux_nu_ssa_obs_jy**(p+2) * (delta / (1+z))**(-p-3)
def cosmoDA(redshift, WMAP9=False, H0=70.0, Om0=0.30, Planck15=False, kpc=False): """ Get the Angular Diameter Distance 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) da = cosmo.angular_diameter_distance(redshift) if not kpc: return da.value else: return da.to(u.kpc).value
def conversion_factor(cc): '''cc is a ClusterConfig object, defined in cluster_config.py''' alt_cosmo = FlatLambdaCDM(70, cc.wl_omega_m) dd1 = cosmo.angular_diameter_distance(cc.redshift) ds1 = cosmo.angular_diameter_distance(z_src_infinity) dds1 = cosmo.angular_diameter_distance_z1z2(cc.redshift, z_src_infinity) dd2 = alt_cosmo.angular_diameter_distance(cc.redshift) ds2 = alt_cosmo.angular_diameter_distance(cc.src_redshift) dds2 = alt_cosmo.angular_diameter_distance_z1z2(cc.redshift, cc.src_redshift) crit_surf_density_1 = lensing_crit_surf_density(ds1, dd1, dds1) crit_surf_density_2 = lensing_crit_surf_density(ds2, dd2, dds2) return crit_surf_density_2 / crit_surf_density_1
def cosmoDA(redshift, WMAP9=False, H0=70.0, Om0=0.30, Planck15=False, kpc=False): """ Get the Angular Diameter Distance 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) da = cosmo.angular_diameter_distance(redshift) if not kpc: return da.value else: return da.to(u.kpc).value
def lens_astropy(): import pylab as pl from astropy.cosmology import WMAP9 zl = np.linspace(0.01, 1.9, 100) zs = 2. wa = [] wc = [] angs = WMAP9.angular_diameter_distance(zs).value chis = WMAP9.comoving_distance(zs).value for zi in zl: angl = WMAP9.angular_diameter_distance(zi).value angls = WMAP9.angular_diameter_distance_z1z2(zi,zs).value chil = WMAP9.comoving_distance(zi).value chils = WMAP9.comoving_distance(zs).value-WMAP9.comoving_distance(zi).value wa.append(angl * angls / angs) wc.append(chil * chils / chis / (1+zi)) pl.plot(zl, wa) pl.plot(zl, wc, ls='--') pl.show()
def lens_astropy(): import pylab as pl from astropy.cosmology import WMAP9 zl = np.linspace(0.01, 1.9, 100) zs = 2. wa = [] wc = [] angs = WMAP9.angular_diameter_distance(zs).value chis = WMAP9.comoving_distance(zs).value for zi in zl: angl = WMAP9.angular_diameter_distance(zi).value angls = WMAP9.angular_diameter_distance_z1z2(zi, zs).value chil = WMAP9.comoving_distance(zi).value chils = WMAP9.comoving_distance(zs).value - WMAP9.comoving_distance( zi).value wa.append(angl * angls / angs) wc.append(chil * chils / chis / (1 + zi)) pl.plot(zl, wa) pl.plot(zl, wc, ls='--') pl.show()
def __init__(self,cname): self.ra = lcs.clusterRA[cname] self.dec = lcs.clusterDec[cname] self.vr = lcs.clusterbiweightcenter[cname] self.sigma = lcs.clusterbiweightscale[cname] self.z = lcs.clusterz[cname] # from Finn+2008 # R200 in Mpc self.r200_Mpc=2.02*(self.sigma)/1000./np.sqrt(lcs.OmegaL+lcs.OmegaM*(1.+self.z)**3)*lcs.H0/70. # in Mpc # get R200 in degrees self.r200_deg=self.r200_Mpc*1000./(cosmo.angular_diameter_distance(self.vr/3.e5).value*lcs.Mpcrad_kpcarcsec)/3600.
def update(val): zL,zS = slzL.val,slzS.val xL,yL = slxL.val, slyL.val ML,eL,PAL = slML.val,sleL.val,slPAL.val sh,sha = slss.val,slsa.val newDd = cosmo.angular_diameter_distance(zL).value newDs = cosmo.angular_diameter_distance(zS).value newDds= cosmo.angular_diameter_distance_z1z2(zL,zS).value newLens = vl.SIELens(zLens,xL,yL,10**ML,eL,PAL) newShear = vl.ExternalShear(sh,sha) xsource,ysource = vl.LensRayTrace(xim,yim,[newLens,newShear],newDd,newDs,newDds) caustics = vl.CausticsSIE(newLens,newDd,newDs,newDds,newShear) ax.cla() ax.plot(xsource,ysource,'b-') for i in range(caustics.shape[0]): ax.plot(caustics[i,0,:],caustics[i,1,:],'k-') #ax.set_xlim(-0.5,0.5) #ax.set_ylim(-0.5,0.5) #for i in range(len(xs)): # p[i].set_xdata(xs[i]) # p[i].set_ydata(ys[i]) f.canvas.draw_idle()
def dist_ratio(z_los): Dr = np.zeros(len(z_los)) D_los = cosmo.angular_diameter_distance(z_los) for i in range(len(z_los)): if z_los[i]<zl: Dr[i] = D_los[i]/Dl else: Dr[i] = (Ds-D_los[i])/(Ds-Dl) return Dr
def runbiweight(self): zflag=abs(self.n.Z - self.cz) < dz # calculate angular separation corresponding to a physical radius of 1.7 Mpc # 1.71 Mpc corresponds to a projected radius of 1 degree at Coma Mpc_per_degree = (cosmo.angular_diameter_distance(z=self.cz).value)*np.pi/180. dtheta = 1.71/Mpc_per_degree print self.prefix, dtheta thetaflag=sqrt((self.n.RA-self.cra)**2 + (self.n.DEC - self.cdec)**2) < dtheta self.bootflag=zflag & thetaflag #& (self.n.ISDSS > 0) v=3.e5*(self.n.Z)#/(1+self.cz) self.biweight=self.getbiweight(v[self.bootflag])#,clipiters=None) self.clipflag=abs(v-self.biweight[0])/self.biweight[3] < scale_cut
def data_query_id(data, clust_id, name_column_id): """The function allows us to obtain the query of our data for particular cluster ID""" C = const.c.to('km/s') data_query = data[data[name_column_id] == clust_id][[ 'RAJ2000_gal', 'DEJ2000_gal', 'z_gal', 'RAJ2000_group', 'DEJ2000_group', 'z_group', 'iGrID', 'DL' ]] xyz_0 = to_xyz_coordinates(data_query, 'DEJ2000_group', 'RAJ2000_group') xyz_gal = to_xyz_coordinates(data_query, 'DEJ2000_gal', 'RAJ2000_gal') a = linear_angle(xyz_0, xyz_gal) data_query['r_pr'] = cosmo.angular_diameter_distance( data_query.z_group) * a data_query['v'] = C * (data_query['z_gal'] - data_query['z_group']) return data_query
real_list_select = real_list_all[mask] real_list_bad = np.random.choice(real_list_select,len(real_list_good),replace=False) real_list_good = real_list_good.astype(str) real_list_bad = real_list_bad.astype(str) #real_list = np.array([138]).astype(str) print len(real_list_good) re = 0.75 dist_cut_los = 0.3 dist_cut_sub = 0.12 area_los,area_sub = np.pi*dist_cut_los**2,np.pi*dist_cut_sub**2 zl,zs = 0.34,3.62 Ds, Dl = cosmo.angular_diameter_distance(zs),cosmo.angular_diameter_distance(zl) avg_kappa_good = [] avg_kappa_bad = [] kappa0_good_sub,kappa1_good_sub,kappa2_good_sub = [],[],[] kappa0_good_los,kappa1_good_los,kappa2_good_los = [],[],[] kappa0_bad_sub,kappa1_bad_sub,kappa2_bad_sub = [],[],[] kappa0_bad_los,kappa1_bad_los,kappa2_bad_los = [],[],[] def cal_beta(z_los): beta = np.zeros(len(z_los)) for i in range(len(z_los)): zi = np.array([zl,z_los[i]])
from astropy.table import Table import matplotlib.pyplot as plt from astropy.cosmology import WMAP9 import numpy as np cat = Table.read('CUT2_CLAUDS_HSC_VISTA_Ks23.3_PHYSPARAM_TM.fits') cat_gal = cat[cat['CLASS'] == 0] cat_massive_gal = cat_gal[cat_gal['MASS_BEST'] > 11.0] for z in np.arange(0.3, 2.5, 0.1): print('============='+str(z)+'================') dis = WMAP9.angular_diameter_distance(z).value # angular diameter distance at redshift z dis_l = WMAP9.comoving_distance(z - 0.1).value # comoving distance at redshift z-0.1 dis_h = WMAP9.comoving_distance(z + 0.1).value # comoving distance at redshift z+0.1 total_v = 4. / 3 * np.pi * (dis_h ** 3 - dis_l ** 3) # Mpc^3 survey_v = total_v * (4 / 41253.05) # Mpc^3 density = 0.00003 # desired constant (cumulative) volume number density (Mpc^-3) num = int(density * survey_v) cat_massive_z_slice = cat_massive_gal[abs(cat_massive_gal['ZPHOT'] - z) < 0.1] # massive galaxies in this z slice cat_massive_z_slice.sort('MASS_BEST') cat_massive_z_slice.reverse() cat_massive_z_slice = cat_massive_z_slice[:num] print(num) cat_sf = cat_massive_z_slice[cat_massive_z_slice['SSFR_BEST'] > -11] cat_qs = cat_massive_z_slice[cat_massive_z_slice['SSFR_BEST'] < -11] fig = plt.figure(figsize=(9, 9)) plt.rc('font', family='serif'), plt.rc('xtick', labelsize=15), plt.rc('ytick', labelsize=15) ax = fig.add_subplot(111)
def microcaustic_field_to_curve(field, time, zl, zs, velocity=(10**4)*(u.kilometer/u.s), M=(1*u.solMass).to(u.kg), width_in_einstein_radii=10, loc='Random', plot=False, ax=None, showCurve=True, rescale=True): """ Convolves an expanding photosphere (achromatic disc) with a microcaustic to generate a magnification curve. Parameters ---------- field: :class:`numpy.ndarray` An opened fits file of a microcaustic, can be generated by realizeMicro time: :class:`numpy.array` Time array you want for microlensing magnification curve, explosion time is 0 zl: float redshift of the lens zs: float redshift of the source velocity: float* :class:`astropy.units.Unit` The average velocity of the expanding photosphere M: float* :class:`~astropy.units.Unit` The mass of the deflector width_in_einstein_radii: float The width of your map in units of Einstein radii loc: str or tuple Random is defualt for location of the supernova, or pixel (x,y) coordiante can be specified plot: bool If true, plots the expanding photosphere on the microcaustic ax: `~matplotlib.pyplot.axis` An optional axis object to plot on. If you want to show the curve, this should be a list like this: [main_ax,lower_ax] showCurve: bool If true, the microlensing curve is plotted below the microcaustic rescale: bool If true, assumes image needs to be rescaled: (x-1024)/256 Returns ------- time: :class:`numpy.array` The time array for the magnification curve dmag: :class:`numpy.array` The magnification curve. """ D = cosmo.angular_diameter_distance_z1z2( zl, zs)*cosmo.angular_diameter_distance(zs)/cosmo.angular_diameter_distance(zl) D = D.to(u.m) try: M.to(u.kg) except: print('Assuming mass is in kg.') einsteinRadius = np.sqrt(4*const.G*M*D/const.c**2) einsteinRadius = einsteinRadius.to(u.kilometer) try: velocity.to(u.kilometer/u.s) except: print('Assuming velocity is in km/s.') velocity *= (u.kilometer/u.s) h, w = field.shape height = width_in_einstein_radii*einsteinRadius.value width = width_in_einstein_radii*einsteinRadius.value pixwidth = width/w pixheight = height/h if pixwidth != pixheight: print('Hmm, you are not using squares...') sys.exit() maxRadius = ((np.max(time)*u.d).to(u.s))*velocity maxRadius = maxRadius.value maxx = int(math.floor(maxRadius/pixwidth)) maxy = int(math.floor(maxRadius/pixheight)) mlimage = field[maxx:-maxx][maxy:-maxy] if loc == 'Random' or not isinstance(loc, (list, tuple)): loc = (int(np.random.uniform(maxx, w-maxx)), int(np.random.uniform(maxy, h-maxy))) tempTime = np.array([((x*u.d).to(u.s)).value for x in time]) snSize = velocity.value*tempTime/pixwidth dmag = mu_from_image(field, loc, snSize, 'disk', plot, time, ax, showCurve, rescale, width_in_einstein_radii) return(time, dmag)
z) < z_bin_size] coord_massive_gal = SkyCoord(cat_massive_z_slice['RA'] * u.deg, cat_massive_z_slice['DEC'] * u.deg) cat_all_z_slice = cat_gal[abs(cat_gal[z_keyname] - z) < 0.5] smf_dist = np.zeros(bin_number) smf_dist_bkg = np.zeros(bin_number) isolated_counts = len(cat_massive_z_slice) massive_count = 0 count_bkg = 0 for gal in cat_massive_z_slice: massive_count += 1 print('Progress:' + str(massive_count) + '/' + str(len(cat_massive_z_slice)), end='\r') dis = WMAP9.angular_diameter_distance(gal[z_keyname]).value coord_gal = SkyCoord(gal['RA'] * u.deg, gal['DEC'] * u.deg) # prepare neighbors catalog cat_neighbors_z_slice = cat_all_z_slice[ abs(cat_all_z_slice[z_keyname] - gal[z_keyname]) < 1.5 * 0.044 * (1 + z)] cat_neighbors = cat_neighbors_z_slice[abs(cat_neighbors_z_slice['RA'] - gal['RA']) < 0.7 / dis / np.pi * 180] cat_neighbors = cat_neighbors[ abs(cat_neighbors['DEC'] - gal['DEC']) < 0.7 / dis / np.pi * 180] if len(cat_neighbors) == 0: # central gals which has no companion continue else:
from astropy.table import Table from astropy.cosmology import WMAP9 import time import numpy as np import astropy.units as u from random import random from astropy.coordinates import SkyCoord, match_coordinates_sky from sklearn.neighbors import KDTree cat = Table.read('CUT2_CLAUDS_HSC_VISTA_Ks23.3_PHYSPARAM_TM.fits') cat_gal = cat[cat['CLASS'] == 0] z = 0.5 dis = WMAP9.angular_diameter_distance(z).value cat_all_z_slice = cat_gal[abs(cat_gal['ZPHOT'] - z) < 0.5] ra = 150 + random() * 2.0 / dis / np.pi * 180 dec = 2 + random() * 2.0 / dis / np.pi * 180 start_1 = time.time() coord_gal = SkyCoord(ra * u.deg, dec * u.deg) cat_neighbors = cat_all_z_slice[abs(cat_all_z_slice['RA'] - ra) < 1.0 / dis / np.pi * 180] cat_neighbors = cat_neighbors[abs(cat_neighbors['DEC'] - dec) < 1.0 / dis / np.pi * 180] coord_neighbors = SkyCoord(cat_neighbors['RA'] * u.deg, cat_neighbors['DEC'] * u.deg) cat_neighbors = cat_neighbors[ coord_neighbors.separation(coord_gal).degree < 1.0 / dis / np.pi * 180] print(len(cat_neighbors)) print("--- %s seconds ---" % (time.time() - start_1))
from matplotlib.animation import FuncAnimation from mpl_toolkits.axes_grid1 import make_axes_locatable from matplotlib.transforms import Bbox from matplotlib.colors import LogNorm from mpl_toolkits.mplot3d import Axes3D from IPython.display import display import math import emcee import corner from multiprocessing import Pool import utils import source_detection z = 0.014313 arc_to_kpc = (cosmo.angular_diameter_distance(z=z) / 206265).to( u.kpc) / u.arcsec # cubefolder=home+'/astro/ARC/data/cubes/NGC6328/' # cube21_file=cubefolder+'CO21/NGC6328_CO_selfcal_20km_natural_pbcor.fits' # ucube21_file=cubefolder+'CO21/NGC6328_CO_selfcal_20km_natural.fits' # CO21,CO21_total=utils.load_fits_cube(cube_file=cube21_file,ucube_file=ucube21_file,pbfile=None, # drop={'x':2.6,'y':4.9,'v':460},z=z,sigma=5, # beam=[0.296,0.220,38.624],zaxis='freq',restFreq=230.538e9, # debug=False,nosignalregions = [{'x0':300,'y0':600,'dx':200,'dy':500},{'x0':1200,'y0':600,'dx':200,'dy':500}]) # IX=CO21.attrs['ixx'] # IY=CO21.attrs['iyy'] # X=CO21.x.data[IX] # Y=CO21.y.data[IY]
z_range = np.linspace(0.02, 1.50, 75) small_angles = [] large_angles = [] for z in z_range: scale = cosmo.arcsec_per_kpc_proper(z) small_angles.append((small_radius * scale).value) large_angles.append((large_radius * scale).value) plt.figure() plt.plot(z_range, small_angles, label="Typical Small Galaxy (Radius 7 Kpc)") plt.plot(z_range, large_angles, label="Typical Large Galaxy (Radius 50 Kpc)") plt.axhline(y=10, color='r', linestyle='-', label="ASASSN cut") plt.legend() path = "plots/galaxy_radius.pdf" print "Saving to", path plt.savefig(path) plt.close() z = 0.014350 d1 = cosmo.angular_diameter_distance(z) d2 = cosmo.luminosity_distance(z) print z, d1, d2 print d1.to("au"), d1.to("lightyear") # d = astropy.coordinates.Distance(z=z)
def __init__(self,infile,usecoma=True,useherc=True,onlycoma=False,prefix='all'): self.prefix=prefix print '%%%%%%%%%%%%%%%%%%%%%%%%%%' print '%%%%%%%%%%%%%%%%%%%%%%%%%%' print '%%%%%%%%%%%%%%%%%%%%%%%%%%' print '\n prefix = \n',self.prefix print '%%%%%%%%%%%%%%%%%%%%%%%%%%' print '%%%%%%%%%%%%%%%%%%%%%%%%%%' print '%%%%%%%%%%%%%%%%%%%%%%%%%%' hdulist=fits.open(homedir+'research/LocalClusters/NSAmastertables/LCS_Spirals_all_fsps_v2.4_miles_chab_charlot_sfhgrid01.fits') self.jmass=hdulist[1].data hdulist.close() # use jmass.mstar_50 and jmass.mstar_err hdulist=fits.open(homedir+'research/LocalClusters/NSAmastertables/LCS_Spirals_isorad.fits') self.isorad=hdulist[1].data hdulist.close() hdulist=fits.open(homedir+'research/LocalClusters/NSAmastertables/LCS_Spirals_AGC.fits') self.agc=hdulist[1].data hdulist.close() hdulist=fits.open(infile) self.s=hdulist[1].data #self.membflag = (self.s.DR_R200 < 1.) & (abs(self.dv) < 3.) if usecoma == False: self.s=self.s[(self.s.CLUSTER != 'Coma') | (abs(self.s.DELTA_V) > 3.)] try: self.jmass=self.jmass[self.s.CLUSTER != 'Coma'] self.isorad=self.isorad[self.s.CLUSTER != 'Coma'] self.agc=self.agc[self.s.CLUSTER != 'Coma'] except: print 'WARNING: problem matching to moustakas MSTAR_50 - probably ok' #self.agnflag=self.agnflag[self.s.CLUSTER != 'Coma'] self.comaflag=False if onlycoma == True: self.s=self.s[self.s.CLUSTER == 'Coma'] #self.jmass=self.jmass[self.s.CLUSTER == 'Coma'] #self.isorad=self.isorad[self.s.CLUSTER == 'Coma'] #self.agc=self.agc[self.s.CLUSTER == 'Coma'] if useherc == False: self.s=self.s[self.s.CLUSTER != 'Hercules'] #self.jmass=self.jmass[self.s.CLUSTER != 'Hercules'] #self.isorad=self.isorad[self.s.CLUSTER != 'Hercules'] #self.agc=self.agc[self.s.CLUSTER != 'Hercules'] #self.logstellarmassTaylor=self.logstellarmassTaylor[self.s.CLUSTER != 'Hercules'] cols=self.s.columns cnames=cols.names hdulist.close() self.logstellarmassTaylor=1.15+0.70*(self.s.ABSMAG[:,3]-self.s.ABSMAG[:,5]) -0.4*(self.s.ABSMAG[:,5]+ 5.*log10(h)) bad_imag=self.logstellarmassTaylor < 5. newi=(self.s.ABSMAG[:,4]+self.s.ABSMAG[:,6])/2. #print len(newi) self.logstellarmassTaylor[bad_imag]=1.15+0.70*(self.s.ABSMAG[:,3][bad_imag]-newi[bad_imag]) -0.4*(newi[bad_imag]+ 5.*log10(h)) self.AGNKAUFF=self.s['AGNKAUFF'] self.AGNKEWLEY=self.s['AGNKEWLEY'] self.AGNSTASIN=self.s['AGNSTASIN'] self.AGNKAUFF=self.s['AGNKAUFF'] & (self.s.HAEW > 0.) self.AGNKEWLEY=self.s['AGNKEWLEY']& (self.s.HAEW > 0.) self.AGNSTASIN=self.s['AGNSTASIN']& (self.s.HAEW > 0.) self.cnumerical_error_flag24=self.s['fnumerical_error_flag24'] self.fcnumerical_error_flag24=self.s['fcnumerical_error_flag24'] self.AGNKAUFF= ((log10(self.s.O3FLUX/self.s.HBFLUX) > (.61/(log10(self.s.N2FLUX/self.s.HAFLUX)-.05)+1.3)) | (log10(self.s.N2FLUX/self.s.HAFLUX) > 0.)) #y=(.61/(x-.47)+1.19) self.AGNKEWLEY= ((log10(self.s.O3FLUX/self.s.HBFLUX) > (.61/(log10(self.s.N2FLUX/self.s.HAFLUX)-.47)+1.19)) | (log10(self.s.N2FLUX/self.s.HAFLUX) > 0.3)) self.upperlimit=self.s['RE_UPPERLIMIT'] # converts this to proper boolean array self.pointsource=self.s['POINTSOURCE'] # converts this to proper boolean array self.gim2dflag=self.s['matchflag'] & (self.s.Rd == self.s.Rd) # get rid of nan's in Rd self.zooflag=self.s['match_flag'] self.nerrorflag=self.s['fcnumerical_error_flag24'] # convert flags to boolean arrays for col in cnames: if (col.find('flag') > -1) | (col.find('AGN') > -1): #print col self.s.field(col)[:]=np.array(self.s[col],'bool') self.nsadict=dict((a,b) for a,b in zip(self.s.NSAID,arange(len(self.s.NSAID)))) self.logstellarmass = self.s.MSTAR_50 # self.logstellarmassTaylor # or #self.logstellarmass = self.logstellarmassTaylor # or #self.define_supersize() # calculating magnitudes from fluxes provided from NSA # # m = 22.5 - 2.5 log_10 (flux_nanomaggies) # from http://www.sdss3.org/dr8/algorithms/magnitudes.php#nmgy self.nsamag=22.5-2.5*log10(self.s.NMGY) self.badfits=zeros(len(self.s.RA),'bool') #badfits=array([166134, 166185, 103789, 104181],'i')' nearbystar=[142655, 143485, 99840, 80878] # bad NSA fit; 24um is ok #nearbygalaxy=[103927,143485,146607, 166638,99877,103933,99056]#,140197] # either NSA or 24um fit is unreliable # checked after reworking galfit nearbygalaxy=[143485,146607, 166638,99877,103933,99056]#,140197] # either NSA or 24um fit is unreliable #badNSA=[166185,142655,99644,103825,145998] #badNSA = [ badfits= nearbygalaxy#+nearbystar+nearbygalaxy badfits=array(badfits,'i') for gal in badfits: self.badfits[where(self.s.NSAID == gal)] = 1 self.sdssspecflag=(self.s.ISDSS > -1) self.emissionflag=((self.s.HAFLUX != 0.) & (self.s.HAFLUX != -9999.) & (self.s.N2FLUX != 0.)) | self.sdssspecflag self.alfalfaflag=(self.s.IALFALFA > -1) self.mipsflag=(self.s.LIR_ZDIST > 0.) self.mipsflag=(self.s.FLUX_RADIUS1 > 0.) self.wiseflag = (self.s.W1FLG_3 < 2) & (self.s.W2FLG_3 < 2) & (self.s.W3FLG_3 < 2) & (self.s.W4FLG_3 < 2) # this allows for source confusion and the presence of some bad pixels within the aperture. self.wiseagn=(self.s.W1MAG_3 - self.s.W2MAG_3) > 0.8 self.agnflag = self.AGNKAUFF | self.wiseagn #self.agnkauff=self.s.AGNKAUFF > .1 #self.agnkewley=self.s.AGNKEWLEY > .1 #self.agnstasin=self.s.AGNSTASIN > .1 self.dv = (self.s.ZDIST - self.s.CLUSTER_REDSHIFT)*3.e5/self.s.CLUSTER_SIGMA self.dvflag = abs(self.dv) < 3. #self.agnflag = self.agnkauff #self.galfitflag=(self.s.galfitflag > .1) #| (self.s.fcmag1 < .1) #self.galfitflag[(self.s.fcmag1 < .1)]=zeros(sum(self.s.fcmag1<.1)) #self.agnflag = self.s.agnflag > .1 #self.zooflag = self.s.match_flag > .1 # self.gim2dflag = self.s.matchflag > .1 self.membflag = (self.s.DR_R200 < 1.) & (abs(self.dv) < 3.) #self.membflag = abs(self.dv) < (-1.25*self.s.DR_R200 + 1.5) # selection of infalling galaxies from Oman+13 self.membflag = abs(self.dv) < (-4./3.*self.s.DR_R200 + 2) # sharper cut determined by eye #self.membflag = abs(self.dv) < (-3./1.2*self.s.DR_R200 + 3) #self.nearexteriorflag = (self.s.DR_R200 > 1.) & (abs(self.dv) < 3.) self.nearexteriorflag = ~self.membflag & (abs(self.dv) < 3.) self.exteriorflag = (abs(self.dv) > 3.) self.groupflag = ((self.s.CLUSTER == 'MKW11') | (self.s.CLUSTER == 'MKW8') | (self.s.CLUSTER == 'AWM4') | (self.s.CLUSTER == 'NGC6107')) self.clusterflag = ((self.s.CLUSTER == 'A1367') | (self.s.CLUSTER == 'Coma') | (self.s.CLUSTER == 'Hercules') | (self.s.CLUSTER == 'A2052') | (self.s.CLUSTER == 'A2063')) #environmental zones self.zone1=(self.s.DR_R200 < .5) & (abs(self.dv) < 3.) self.zone2=(self.s.DR_R200 > .5) & (self.s.DR_R200 < 1) & (abs(self.dv) < 3.) self.zone3=(self.s.DR_R200 > 1) & (abs(self.dv) < 3.) self.zone4= (abs(self.dv) > 3.) self.HIflag = self.s.HIMASS > 0. self.sumagnflag=self.s.AGNKAUFF + self.s.AGNKEWLEY + self.s.AGNSTASIN self.da=zeros(len(self.s.ZDIST),'f') q=.2 baflag=self.s.SERSIC_BA < q self.incl=arccos(sqrt((self.s.SERSIC_BA**2-q**2)/(1.-q**2)))*(~baflag)+baflag*pi/2. # in radians # correct for inclination #self.isorad.NSA=self.isorad.NSA*cos(self.incl) #self.isorad.MIPS=self.isorad.MIPS*cos(self.incl) self.mag24=23.9-2.5*log10(self.s.FLUX24) self.NUV24=(self.nsamag[:,2])-self.mag24 self.mag24se=18.526-2.5*log10(self.s.SE_FLUX_AUTO) #self.gi_corr=(self.nsamag[:,3]-self.nsamag[:,5])-(.17*(1-cos(self.incl))*((self.jmass.MSTAR_50)-8.19)) self.gi_corr=(self.nsamag[:,3]-self.nsamag[:,5])-(.17*(1-cos(self.incl))*((self.logstellarmass)-8.19)) for i in range(len(self.s.ZDIST)): self.da[i] = cosmo.angular_diameter_distance(self.s.ZDIST[i]).value self.da=cosmo.angular_diameter_distance(self.s.ZDIST).value*Mpcrad_kpcarcsec # kpc/arcsec for c in clusternames: if (c == 'Coma') & (usecoma == False): continue else: for id in spiral_100_nozoo[c]: try: self.spiralflag[self.nsadict[id]]=1 except: print 'did not find ',id self.dist3d=sqrt((self.dv-3.)**2 + (self.s.DR_R200)**2) self.sb_obs=zeros(len(self.s.RA)) flag= (~self.s['fcnumerical_error_flag24']) self.sb_obs[flag]=self.s.fcmag1[flag] + 2.5*log10(pi*((self.s.fcre1[flag]*mipspixelscale)**2)*self.s.fcaxisratio1[flag]) self.DA=zeros(len(self.s.SERSIC_TH50)) for i in range(len(self.DA)): if self.membflag[i]: self.DA[i] = cosmo.angular_diameter_distance(self.s.CLUSTER_REDSHIFT[i]).value*Mpcrad_kpcarcsec else: self.DA[i] = cosmo.angular_diameter_distance(self.s.ZDIST[i]).value*Mpcrad_kpcarcsec self.sizeflag=(self.s.SERSIC_TH50*self.DA > minsize_kpc) #& (self.s.SERSIC_TH50 < 20.) self.SIZE_RATIO_DISK = np.zeros(len(self.gim2dflag)) self.SIZE_RATIO_DISK[self.gim2dflag] = self.s.fcre1[self.gim2dflag]*mipspixelscale*self.DA[self.gim2dflag]/self.s.Rd[self.gim2dflag] self.SIZE_RATIO_DISK_ERR = np.zeros(len(self.gim2dflag)) self.SIZE_RATIO_DISK_ERR[self.gim2dflag] = self.s.fcre1err[self.gim2dflag]*mipspixelscale*self.DA[self.gim2dflag]/self.s.Rd[self.gim2dflag] self.SIZE_RATIO_gim2d = np.zeros(len(self.gim2dflag)) self.SIZE_RATIO_gim2d[self.gim2dflag] = self.s.fcre1[self.gim2dflag]*mipspixelscale*self.DA[self.gim2dflag]/self.s.Rhlr_2[self.gim2dflag] self.SIZE_RATIO_gim2d_ERR = np.zeros(len(self.gim2dflag)) self.SIZE_RATIO_gim2d_ERR[self.gim2dflag] = self.s.fcre1err[self.gim2dflag]*mipspixelscale*self.DA[self.gim2dflag]/self.s.Rhlr_2[self.gim2dflag] self.SIZE_RATIO_NSA = self.s.fcre1*mipspixelscale/self.s.SERSIC_TH50 self.SIZE_RATIO_NSA_ERR=self.s.fcre1err*mipspixelscale/self.s.SERSIC_TH50 if USE_DISK_ONLY: self.sizeratio = self.SIZE_RATIO_DISK self.sizeratioERR=self.SIZE_RATIO_DISK_ERR else: #self.sizeratio = self.SIZE_RATIO_gim2d #self.sizeratioERR=self.SIZE_RATIO_gim2d_ERR self.sizeratio = self.s.fcre1*mipspixelscale/self.s.SERSIC_TH50 self.sizeratioERR=self.s.fcre1err*mipspixelscale/self.s.SERSIC_TH50 self.massflag=self.logstellarmass > minmass self.Re24_kpc = self.s.fcre1*mipspixelscale*self.DA self.lirflag=(self.s.LIR_ZDIST > 5.2e8) self.galfitflag = (self.s.fcmag1 > .1) & ~self.nerrorflag & (self.sb_obs < 20.) & (self.s.fcre1/self.s.fcre1err > .5)#20.) #override the galfit flag for the following galaxies self.galfit_override = [70588,70696,43791,69673,146875,82170, 82182, 82188, 82198, 99058, 99660, 99675, 146636, 146638, 146659, 113092, 113095, 72623,72631,72659, 72749, 72778, 79779, 146121, 146130, 166167, 79417, 79591, 79608, 79706, 80769, 80873, 146003, 166044,166083, 89101, 89108,103613,162792,162838, 89063] for id in self.galfit_override: try: self.galfitflag[self.nsadict[int(id)]] = True except KeyError: if self.prefix == 'no_coma': print 'ids not relevant for nc' else: sys.exit() #self.galfitflag = self.galfitflag self.galfitflag[self.nsadict[79378]] = False self.galfitflag = self.galfitflag & ~self.badfits self.sbflag=self.sb_obs < 20. self.sfsampleflag = self.sizeflag & self.massflag & self.lirflag & ~self.badfits self.ur=self.s.ABSMAG[:,2]-self.s.ABSMAG[:,4] self.redflag=(self.ur > 2.3) self.greenflag=(self.ur > 1.8) & (self.ur < 2.3) self.blueflag=(self.ur<1.8) self.NUVr=self.s.ABSMAG[:,1] - self.s.ABSMAG[:,4] self.blueflag2=self.NUVr < 4 self.greenflag = (self.NUVr < 5) & ~self.blueflag2 # add galaxies with blue u-r colors but no galex data self.blue_nogalex = (self.s.NMGY[:,1] == 0.) & (self.blueflag) self.blueflag2[self.blue_nogalex] = np.ones(sum(self.blue_nogalex)) self.basesampleflag = self.galfitflag & self.sizeflag & self.massflag & self.lirflag & ~self.badfits & self.gim2dflag self.sampleflag = self.galfitflag & self.lirflag & self.sizeflag & ~self.agnflag & self.sbflag#& self.massflag#& self.gim2dflag#& self.blueflag2 self.allbutgalfitflag = ~self.galfitflag & self.lirflag & self.sizeflag & ~self.agnflag #& self.massflag#& self.gim2dflag#& self.blueflag2 if USE_DISK_ONLY: self.sampleflag = self.sampleflag & self.gim2dflag self.greensampleflag = self.galfitflag & self.sizeflag & self.massflag & self.lirflag & ~self.badfits & self.greenflag & self.gim2dflag self.bluesampleflag = self.sampleflag & self.blueflag2 self.unknownagn= self.sizeflag & self.massflag & self.lirflag & ~self.emissionflag & ~self.wiseflag self.virialflag = self.dv < (1.5-1.25*self.s.DR_R200) self.limitedsample=self.sampleflag & (self.logstellarmass > 9.5) & (self.logstellarmass < 10.2) & self.gim2dflag & (self.s.B_T_r < 0.2) & self.dvflag self.c90=self.s.FLUX_RADIUS2/self.s.fcre1 self.size_ratio_corr=self.sizeratio*(self.s.faxisratio1/self.s.SERSIC_BA) self.truncflag=(self.sizeratio < 0.7) & self.sampleflag & ~self.agnflag self.dL = self.s.ZDIST*3.e5/H0 self.distmod=25.+5.*log10(self.dL) #best_distance=self.membflag * self.cdMpc + ~self.membflag*(self.n.ZDIST*3.e5/H0) self.LIR_BEST = self.s.LIR_ZCLUST * self.membflag + ~self.membflag*(self.s.LIR_ZDIST) self.SFR_BEST = self.s.SFR_ZCLUST * np.array(self.dvflag,'i') + np.array(~self.dvflag,'i')*(self.s.SFR_ZDIST) self.ssfr=self.SFR_BEST/(10.**(self.logstellarmass)) self.ssfrerr=self.SFR_BEST/(10.**(self.logstellarmass))*(self.s.FLUX24ERR/self.s.FLUX24) self.ssfrms=np.log10(self.ssfr*1.e9/.08) self.sigma_ir=np.zeros(len(self.LIR_BEST),'d') self.sigma_irerr=np.zeros(len(self.LIR_BEST),'d') self.sigma_irerr[self.galfitflag]= np.sqrt(((self.LIR_BEST[self.galfitflag]*self.s.FLUX24ERR[self.galfitflag]/self.s.FLUX24ERR[self.galfitflag])/2/(np.pi*(self.s.fcre1[self.galfitflag]*self.DA[self.galfitflag])**2))**2+(2.*self.LIR_BEST[self.galfitflag]/2/(np.pi*(self.s.fcre1[self.galfitflag]*self.DA[self.galfitflag])**3)*self.s.fcre1err[self.galfitflag])**2) self.sigma_ir[self.galfitflag]= self.LIR_BEST[self.galfitflag]/2/(np.pi*(self.s.fcre1[self.galfitflag]*self.DA[self.galfitflag])**2) self.starburst = (self.ssfr*1.e9 > .16) self.compact_starburst = (self.ssfr*1.e9 > .16) & (self.sigma_ir > 5.e9) self.agcdict=dict((a,b) for a,b in zip(self.s.AGCNUMBER,arange(len(self.s.AGCNUMBER)))) self.nsadict=dict((a,b) for a,b in zip(self.s.NSAID,arange(len(self.s.NSAID)))) self.massdensity=self.logstellarmass-log10(2*pi*(self.s.SERSIC_TH50*self.DA)**2)
def microcaustic_field_to_curve(field, time, zl, zs, velocity=(10**4) * (u.kilometer / u.s), M=(1 * u.solMass).to(u.kg), loc='Random', plot=False): """Convolves an expanding photosphere (achromatic disc) with a microcaustic to generate a magnification curve. Parameters ---------- field: :class:`numpy.ndarray` An opened fits file of a microcaustic, can be generated by realizeMicro time: :class:`numpy.array` Time array you want for microlensing magnification curve, explosion time is 0 zl: float redshift of the lens zs: float redshift of the source velocity: float* :class:`astropy.units.Unit` The average velocity of the expanding photosphere M: float* :class:`astropy.units.Unit` The mass of the deflector loc: str or tuple Random is defualt for location of the supernova, or pixel (x,y) coordiante can be specified plot: Boolean If true, plots the expanding photosphere on the microcaustic Returns ------- time: :class:`numpy.array` The time array for the magnification curve dmag: :class:`numpy.array` The magnification curve. """ D = cosmo.angular_diameter_distance_z1z2( zl, zs) * cosmo.angular_diameter_distance( zs) / cosmo.angular_diameter_distance(zl) D = D.to(u.m) try: M.to(u.kg) except: print('Assuming mass is in kg.') einsteinRadius = np.sqrt(4 * const.G * M * D / const.c**2) einsteinRadius = einsteinRadius.to(u.kilometer) try: velocity.to(u.kilometer / u.s) except: print('Assuming velocity is in km/s.') velocity *= (u.kilometer / u.s) #mlimage=fits.getdata(field) h, w = field.shape height = 10 * einsteinRadius.value width = 10 * einsteinRadius.value #print(10*einsteinRadius) #center=(width/2,height/2) pixwidth = width / w pixheight = height / h if pixwidth != pixheight: print('Hmm, you are not using squares...') sys.exit() maxRadius = ((np.max(time) * u.d).to(u.s)) * velocity maxRadius = maxRadius.value maxx = int(math.floor(maxRadius / pixwidth)) maxy = int(math.floor(maxRadius / pixheight)) mlimage = field[maxx:-maxx][maxy:-maxy] if loc == 'Random' or not isinstance(loc, (list, tuple)): loc = (int(np.random.uniform(maxx, w - maxx)), int(np.random.uniform(maxy, h - maxy))) tempTime = np.array([((x * u.d).to(u.s)).value for x in time]) snSize = velocity.value * tempTime / pixwidth dmag = mu_from_image(field, loc, snSize, 'disk', plot, time) return (time, dmag)
def load_fits_cube(cube_file=None,ucube_file=None,pbfile=None,z=0,sigma=1,area_mask=None,center='center',voffset=0.,zaxis='vel',n=2,restFreq=230.5380e+9,nosignalregions = [{'x0':0,'y0':0,'dx':100,'dy':100}],drop=None,debug=False,beam=None): """Load a fits cube file and stores it internally in class. The user must provide 2/3 cubes (pb corrected and uncorrected, pb corrected and pb correction, uncorrected and pb correction). Args: name ([str]): Internal name for the cube (ie co21) cube_file ([str], optional): Filename of the pb-corrected cube. Defaults to None. ucube_file ([str], optional): Filename of the pb-uncorrected cube. Defaults to None. pbfile ([str], optional): Filename of the pb-correction cube. Defaults to None. sigma (float, optional): Sigma masking value. The mask is (2d-area) means all channels in area are preserved. Defaults to 1. area_mask ([dic], optional): Mask a 3d area with a 3dellipsoid using a dic of dic. Defaults to None. the dictionaries are in form: {'tag for the area(unique)':{'x0':center in x, 'Rx': radius in x,'y0':center in y, 'Ry': radius in y, 'v0':center in v, 'Rv': radius in v, 's':'del' to remove this data, 'add' to add this data}} center ([str or dic], optional): if 'center' cube is centered on Y/2, X/2, else moves the cube by padding voffset ([float], optional): Voffset NOT IMPLEMENTED Defaults to 0.. zaxis (str, optional): Z axis, velocities ('vel') or frequencies ('freq'). Defaults to 'vel'. n (int, optional): Molecular level, (for CO). Defaults to 2. restFreq ([float], optional): Rest frequency of the line in case zaxis='freq'. Defaults to 230.5380e+9. nosignalregions (list, optional): list of dics, with the areas for calculating the noise. Defaults to [{'x0':0,'y0':0,'dx':100,'dy':100}]. drop ([dic], optional): Drop data for memory savings, ie drop={'x':2,'y':5,'v':1000} to keep [[-2,2],[-5,5],[-1000,1000]]]. Defaults to None. debug (bool, optional): Show debugging maps. Defaults to False. beam ([list], optional): In case header does not provide beam, ie drop={'x':2.8,'y':5,'v':1000}. Defaults to None. fit (bool, optional): If the cube will be used on 3d fitting. Defaults to False. Raises: Exception: [description] Exception: [description] Exception: [description] """ if (pbfile is None) and (ucube_file is not None) and (cube_file is not None): hduCO = fits.open(cube_file)[0] hdr_cube, cube = hduCO.header, hduCO.data hduUCO = fits.open(ucube_file)[0] hdrUCO, ucube = hduUCO.header, hduUCO.data if cube.shape[0]==1: cube=cube[0] if ucube.shape[0]==1: ucube=ucube[0] print(f'Original Data Corrected Cube shape {cube.shape}') print(f'Original Data UnCorrected Cube shape {ucube.shape}') if center != 'center': #pivot=[center['y'],center['x']] padX = [cube.shape[2] - center['x'], center['x']] padY = [cube.shape[1] - center['y'], center['y']] padz = [0,0] cube = np.pad(cube, [padz,padY, padX], 'constant') ucube = np.pad(ucube, [padz,padY, padX], 'constant') print(f'Padded Data Corrected Cube shape {cube.shape}') #pbcorr=cube[int(cube.shape[0]/2),:,:]/ucube[int(cube.shape[0]/2),:,:] #TODO: load the pbcorr image, it changes with the frequency pbcorr=cube/ucube madmap3d = np.ones(shape=(ucube.shape)) rms=np.array([]) for nosireg in nosignalregions: rmsi=mad_std(ucube[:,nosireg['y0']:nosireg['y0']+nosireg['dy'],nosireg['x0']:nosireg['x0']+nosireg['dx']]) rms=np.append(rms,rmsi) print(f"RMS Noise in area {nosireg} {rmsi}") rms=np.nanmean(rms) print('Mean RMS: ',rms) madmap3d=pbcorr*rms elif (pbfile is not None) and (ucube_file is None) and (cube_file is not None): hduUCO = fits.open(pbfile)[0] hdrUCO, pbcorr = hduUCO.header, hduUCO.data if pbcorr.shape[0]==1: pbcorr=pbcorr[0] print(f'Original pbema shape {pbcorr.shape}') hduCO = fits.open(cube_file)[0] hdr_cube, cube = hduCO.header, hduCO.data if cube.shape[0]==1: cube=cube[0] print(f'Original Data Corrected Cube shape {cube.shape}') ucube=cube/pbcorr print(f'Original Data UnCorrected Cube shape {ucube.shape}') if center != 'center': padX = [cube.shape[2] - center['x'], center['x']] padY = [cube.shape[1] - center['y'], center['y']] padz = [0,0] cube = np.pad(cube, [padz,padY, padX], 'constant') ucube = np.pad(ucube, [padz,padY, padX], 'constant') print(f'Padded Data Corrected Cube shape {cube.shape}') rms=np.array([]) for nosireg in nosignalregions: rmsi=mad_std(ucube[:,nosireg['y0']:nosireg['y0']+nosireg['dy'],nosireg['x0']:nosireg['x0']+nosireg['dx']]) rms=np.append(rms,rmsi) print(f"RMS Noise in area {nosireg} {rmsi}") rms=np.nanmean(rms) print('Mean RMS: ',rms) madmap3d=pbcorr*rms elif (pbfile is not None) and (ucube_file is not None) and (cube_file is None): hduUCO = fits.open(pbfile)[0] hdrUCO, pbcorr = hduUCO.header, hduUCO.data if pbcorr.shape[0]==1: pbcorr=pbcorr[0] print(f'Original pbema shape {pbcorr.shape}') hduUCO = fits.open(ucube_file)[0] hdr_cube, ucube = hduUCO.header, hduUCO.data if ucube.shape[0]==1: ucube=ucube[0] print(f'Original Data UnCorrected Cube shape {ucube.shape}') cube=pbcorr*ucube print(f'Original Data Corrected Cube shape {cube.shape}') print(f'Original Data UnCorrected Cube shape {ucube.shape}') if center != 'center': #pivot=[center['y'],center['x']] padX = [cube.shape[2] - center['x'], center['x']] padY = [cube.shape[1] - center['y'], center['y']] padz = [0,0] cube = np.pad(cube, [padz,padY, padX], 'constant') ucube = np.pad(ucube, [padz,padY, padX], 'constant') print(f'Padded Data Corrected Cube shape {cube.shape}') rms=np.array([]) for nosireg in nosignalregions: rmsi=mad_std(ucube[:,nosireg['y0']:nosireg['y0']+nosireg['dy'],nosireg['x0']:nosireg['x0']+nosireg['dx']]) rms=np.append(rms,rmsi) print(f"RMS Noise in area {nosireg} {rmsi}") rms=np.nanmean(rms) print('Mean RMS: ',rms) madmap3d=pbcorr*rms else: raise Exception('We need at least two cubes (pb corrected and uncorrected, pb corrected and pb correction, uncorrected and pb correction)') wmap = WCS(hdr_cube) if debug: fig1 = plt.figure(figsize=(11,50)) gs=gridspec.GridSpec(6, 2, height_ratios=[1,1,1,1,1,1], width_ratios=[1,0.025]) axc = fig1.add_subplot(gs[0,0]) axu = fig1.add_subplot(gs[1,0]) caximc=fig1.add_subplot(gs[0,1]) caximu=fig1.add_subplot(gs[1,1]) axc.set_title('Primary Beam Corrected integrated Image') imc = axc.imshow(np.nansum(cube,axis=0),origin='lower') plt.colorbar(imc, cax=caximc) axu.set_title('Non Corrected integrated Image') imu = axu.imshow(np.nansum(ucube,axis=0),origin='lower') if center!='center': axu.axvline(center['x']);axu.axhline(center['y']) for nosireg in nosignalregions: rect = patches.Rectangle((nosireg['x0'],nosireg['y0']),nosireg['dx'],nosireg['dy'],linewidth=1,edgecolor='r',facecolor='none') axu.add_patch(rect) plt.colorbar(imu, cax=caximu)#,orientation = 'horizontal', ticklocation = 'top') plt.show() dx = np.abs(hdr_cube['CDELT1'])*3600 if zaxis=='vel': dv=hdr_cube['CDELT3']/1e3 b=hdr_cube['CRVAL3']/1e3-hdr_cube['CRPIX3']*dv vel=np.linspace(b+dv,b+dv*hdr_cube['NAXIS3'],hdr_cube['NAXIS3'])#np.arange(b+dv,b+dv*hdrCO['NAXIS3']+dv,dv)#np.linspace(b,b+dv*hdrCO['NAXIS3'],hdrCO['NAXIS3']) elif zaxis=='freq': nu = (hdr_cube['CRVAL3'] + np.arange(hdr_cube['NAXIS3']) * hdr_cube['CDELT3']) # in Hz #nu0 = 230.5380e+9/(1. + z) # Restframe CO(2-1) frequency in GHz, from splatalogue nu0 = restFreq/(1.+z) vel = ((nu0**2 - nu**2) / (nu0**2 + nu**2) * const.c.value * 1.e-3) else: print('zaxis must be vel or freq') dv = np.mean(vel[1:] - vel[:-1]) # average channel width in km/s dx = np.abs(hdr_cube['CDELT1'])*3600 dy = np.abs(hdr_cube['CDELT2'])*3600 # if center == 'center': x1=np.linspace(-cube.shape[2]*dx/2.,cube.shape[2]*dx/2.,int(cube.shape[2])) y1=np.linspace(-cube.shape[1]*dy/2.,cube.shape[1]*dy/2.,int(cube.shape[1])) # else: # x1=np.linspace(-center['x']*dx,cube.shape[2]*dx/2.,int(cube.shape[2])) # y1=np.linspace(-center['y']*dy/2.,cube.shape[1]*dy/2.,int(cube.shape[1])) if 'BMAJ' in hdr_cube.keys(): Bmaj=hdr_cube['BMAJ']*3600 Bmin=hdr_cube['BMIN']*3600 Bpa=hdr_cube['BPA']#*3600 elif beam is not None: print('BEAM not in HEADER, using user values') Bmaj,Bmin,Bpa=beam else: raise Exception('BEAM not in HEADER, use the beam argument') arctokpc=((cosmo.angular_diameter_distance(z=z)/206265).to(u.kpc)/u.arcsec).value data=xr.Dataset() #print(arctokpc,x1,dx,cube.shape) data['cube']=xr.DataArray(cube, coords=[vel, y1*arctokpc,x1*arctokpc], dims=['v', 'y' ,'x']) data['madcube']=xr.DataArray(madmap3d, coords=[vel, y1*arctokpc,x1*arctokpc], dims=['v', 'y' ,'x']) if drop is not None: # data=data.where(((data.v>-drop['v'])&(data.v<drop['v']))&((data.x>-drop['x'])&(data.x<drop['x']))&((data.y>-drop['y'])&(data.y<drop['y'])),drop=True) wx=(-drop['x']<data.x)&(drop['x']>data.x) wy=(-drop['y']<data.y)&(drop['y']>data.y) wv=(-drop['v']<data.v)&(drop['v']>data.v) vs=slice(np.ix_(wv)[0][0],np.ix_(wv)[0][-1]) xs=slice(np.ix_(wx)[0][0],np.ix_(wx)[0][-1]) ys=slice(np.ix_(wy)[0][0],np.ix_(wy)[0][-1]) data=data.where(wx&wy&wv,drop=True) # ws=slice(np.ix_(wv,wy,wx)) # wmap2=wmap.slice((ws)) wmap2=wmap.celestial[ys,xs,]#.slice((ws)) else: wmap2=wmap.celestial#[ivlim[0]:ivlim[1],:,:] data.attrs['z']=z data.attrs['header_original']=hdr_cube #data.attrs['wcs']=wmap2 data.attrs['n']=n data.attrs['x_units']=u.kpc data.attrs['y_units']=u.kpc data.attrs['v_units']=u.km/u.s data.attrs['cube_units']=u.Jy/u.beam data.attrs['arctokpc']=arctokpc data.attrs['xsize']=data.dims['x']*dx*arctokpc data.attrs['ysize']=data.dims['y']*dy*arctokpc data.attrs['vsize']=data.dims['v']*dv data.attrs['dx']=dx*arctokpc data.attrs['dy']=dy*arctokpc data.attrs['dv']=dv data.attrs['rms']=rms data.attrs['beam']=np.array([Bmaj*arctokpc,Bmin*arctokpc,Bpa]) data.attrs['beam_area']=data.attrs['beam'][0]*data.attrs['beam'][1]*np.pi/(4*np.log(2))*u.kpc**2/u.beam#kpc^2 data.attrs['pixel_area']=data.attrs['dx']*data.attrs['dy']/u.pixel**2 display(data.attrs['beam_area']) display(data.attrs['pixel_area']) mask=(data['cube']>sigma*data['madcube']) mom0=np.nansum(mask,axis=0) mask=mom0>0 if area_mask is not None: amask=~(data['cube'].x>-100)&(data['cube'].y>-100)&(data['cube'].v>-4000) for area in area_mask: x0=area_mask[area]['x0'] Rx=area_mask[area]['Rx'] y0=area_mask[area]['y0'] Ry=area_mask[area]['Ry'] v0=area_mask[area]['v0'] Rv=area_mask[area]['Rv'] if area_mask[area]['s'] == 'del': amask=amask | (~(((data['cube'].x-x0)/Rx)**2+((data['cube'].y-y0)/Ry)**2+((data['cube'].v-v0)/Rv)**2<1)) elif area_mask[area]['s'] == 'add': amask=amask | (((data['cube'].x-x0)/Rx)**2+((data['cube'].y-y0)/Ry)**2+((data['cube'].v-v0)/Rv)**2<1) else: raise Exception('Not an option (del|add)') cubedata=data.where(mask).where(amask,drop=True) cubedata_total=data.where(amask,drop=False) #data.update({name:{'cube':data.where(mask).where(amask,drop=True),'cube_total':data.where(amask,drop=False)}}) else: cubedata=data.where(mask).where(mask) cubedata_total=data #data.update({name:{'cube':data.where(mask),'cube_total':data}}) mom0=np.nansum(cubedata['cube'].data,axis=0) iyy,ixx=np.where(np.where(mom0>0,True,False)) cubedata.attrs['ixx']=ixx cubedata.attrs['iyy']=iyy if debug: fig1 = plt.figure(figsize=(11,20)) axu = fig1.add_subplot(1,1,1) axu.set_title('Masked image') imu = axu.pcolormesh(cubedata.x,cubedata.y,np.nansum(cubedata['cube'],axis=0)) axu.axvline(0);axu.axhline(0) if area_mask is not None: for area in area_mask: x0=area_mask[area]['x0'] Rx=area_mask[area]['Rx'] y0=area_mask[area]['y0'] Ry=area_mask[area]['Ry'] el = patches.Ellipse((x0,y0),2*Rx,2*Ry,linewidth=1,edgecolor='r',facecolor='none') axu.add_patch(el) axu.set(aspect=1) plt.show() return cubedata,cubedata_total
gal_ids = open('gal_ims.txt').readlines() pbar = tqdm.tqdm(total=len(gal_ids)) for i in range(len(gal_ids)): gal_ids[i] = gal_ids[i].rstrip() gal_ids[i] = gal_ids[i].replace('cutout_', '') gal_ids[i] = gal_ids[i].replace('.fits', '') for gal in cat_massive_gal: if gal['ID'] in gal_ids: pbar.update(1) pbar.set_description(gal['ID']) massive_count += 1 dis = WMAP9.angular_diameter_distance(float(gal['z'])).value mock_cat_zslice = mock_cat[abs(mock_cat['zKDEPeak'] - float(gal['z'])) < 5 * 0.044 * (1 + z)] cat_neighbors = mock_cat_zslice[abs(mock_cat_zslice['X_WORLD'] - gal['ra']) < 0.7 / dis / np.pi * 180] cat_neighbors = cat_neighbors[abs(cat_neighbors['Y_WORLD'] - gal['dec']) < 0.7 / dis / np.pi * 180] coord_gal = SkyCoord(gal['ra'] * u.deg, gal['dec'] * u.deg) coord_neighbors = SkyCoord(cat_neighbors['X_WORLD'] * u.deg, cat_neighbors['Y_WORLD'] * u.deg) radius_list = coord_neighbors.separation( coord_gal).degree / 180. * np.pi * dis * 1000 # kpc
xim = np.arange(-3.,3.,.02) yim = np.arange(-3.,3.,.02) xim,yim = np.meshgrid(xim,yim) zLens,zSource = 0.8,5.656 xLens,yLens = 0.,0. MLens,eLens,PALens = 2.87e11,0.5,70. xSource,ySource,FSource,sSource,nSource,arSource,PAsource = 0.216,0.24,0.02,0.1,0.8,0.7,120.-90 shear,shearangle = 0.12, 120. Lens = vl.SIELens(zLens,xLens,yLens,MLens,eLens,PALens) Shear = vl.ExternalShear(shear,shearangle) Source = vl.SersicSource(zSource,True,xSource,ySource,FSource,sSource,nSource,arSource,PAsource) Dd = cosmo.angular_diameter_distance(zLens).value Ds = cosmo.angular_diameter_distance(zSource).value Dds = cosmo.angular_diameter_distance_z1z2(zLens,zSource).value xsource,ysource = vl.LensRayTrace(xim,yim,[Lens,Shear],Dd,Ds,Dds) imbg = vl.SourceProfile(xim,yim,Source,[Lens,Shear]) imlensed = vl.SourceProfile(xsource,ysource,Source,[Lens,Shear]) caustics = vl.CausticsSIE(Lens,Dd,Ds,Dds,Shear) f = pl.figure(figsize=(12,6)) ax = f.add_subplot(111,aspect='equal') pl.subplots_adjust(right=0.48,top=0.97,bottom=0.03,left=0.05) cmbg = cm.cool cmbg._init()
print('============='+str(z)+'================') cat_random_copy = np.copy(cat_random) cat_massive_z_slice = cat_massive_gal[abs(cat_massive_gal['ZPHOT'] - z) < 0.09] # cat_massive_z_slice = cat_massive_z_slice[np.random.rand(len(cat_massive_z_slice)) < 0.5] cat_all_z_slice = cat_gal[abs(cat_gal['ZPHOT'] - z) < 0.5] # Fetch coordinates for massive gals cat_massive_z_slice['RA'].unit = u.deg cat_massive_z_slice['DEC'].unit = u.deg coord_massive_gal = SkyCoord.guess_from_table(cat_massive_z_slice) radial_dist = 0 radial_dist_bkg = 0 massive_counts = len(cat_massive_z_slice) for gal in cat_massive_z_slice: dis = WMAP9.angular_diameter_distance(gal['ZPHOT']).value coord_gal = SkyCoord(gal['RA'] * u.deg, gal['DEC'] * u.deg) # prepare neighbors catalog cat_neighbors_z_slice = cat_all_z_slice[abs(cat_all_z_slice['ZPHOT'] - gal['ZPHOT']) < 1.5 * 0.03 * (1 + z)] cat_neighbors = cat_neighbors_z_slice[abs(cat_neighbors_z_slice['RA'] - gal['RA']) < 0.7 / dis / np.pi * 180] cat_neighbors = cat_neighbors[abs(cat_neighbors['DEC'] - gal['DEC']) < 0.7 / dis / np.pi * 180] if len(cat_neighbors) == 0: # exclude central gals which has no companion radial_bkg, coord_bkg_list = bkg(gal['RA'], cat_neighbors_z_slice, coord_massive_gal, mode=mode) radial_dist_bkg += radial_bkg cat_random_copy = cut_random_cat(cat_random_copy, coord_bkg_list) continue else: ind = KDTree(np.array(cat_neighbors['RA', 'DEC']).tolist()).query_radius([(gal['RA'], gal['DEC'])], 0.7 / dis / np.pi * 180) cat_neighbors = cat_neighbors[ind[0]]
def comov_vol(z, delta_z, omega_pix): return (omega_pix * ((cosmo.angular_diameter_distance(z).value)**2) * (sc.c / cosmo.H(z).value) * delta_z ) ###how to deal with delta z...
# Remove NANs, INFs, z <= 0 and r <= 0 print "Original data shape: ", hdu_in.data.shape data_aux = np.asarray(hdu_in.data.tolist()) rm_crit = np.isnan (data_aux).any(axis=1) | np.isinf (data_aux).any(axis=1) | (hdu_in.data.field(field_z) <= 0) | (hdu_in.data.field(field_r) <= 0) del data_aux hdu_in.data = hdu_in.data[~rm_crit] #tbdata = tbdata[~np.isnan(np.asarray(tbdata.tolist())).any(axis=1)] tbdata = hdu_in.data print "Removed NANs and INFs: ", tbdata.shape r = tbdata.field(field_r) m = tbdata.field(field_m) z = tbdata.field(field_z) psf = tbdata.field(field_psf) d = cosmo.angular_diameter_distance(z).to(ap.units.kpc) # distance in kpc scale = d*ap.units.arcsec.to(ap.units.radian) # scale [kpc/arcsec] R = r * scale M = m - 5*np.log10(4.28E8*z) r_psf = 2.*np.sqrt(2*np.log(2)) * r/psf print hdu_in.data.field(field_z).shape cols = [pf.Column (name = field_r, array = r, format = "D"), pf.Column (name = field_m, array = m, format = "D"), pf.Column (name = field_z, array = z, format = "D"), pf.Column (name = field_psf, array = psf, format = "D"), pf.Column (name = "absPetroMag_r", array = M, format = "D"), pf.Column (name = "petroRad_r_psf", array = r_psf, format = "D"), pf.Column (name = "petroRad_r_kpc", array = R, format = "D")] #t = hdu_in.columns + pf.ColDefs(cols)
def distToKpc(z, cosmo=None): # returns Kpc/arcsec dist = cos.angular_diameter_distance(z) return dist.value * 1000
def plotpositionson24(plotsingle=0,plotcolorbar=1,plotnofit=0,useirsb=0): plt.figure(figsize=(10,8)) plt.subplots_adjust(hspace=.02,wspace=.02,left=.12,bottom=.12,right=.85) i=1 allax=[] for cl in clusternamesbylx: plt.subplot(3,3,i) infile=homedir+'/github/LCS/tables/clustertables/'+cl+'_NSAmastertable.fits' d=fits.getdata(infile) #print cl, i ra=g.s.RA-clusterRA[cl] dec=g.s.DEC-clusterDec[cl] r200=2.02*(clusterbiweightscale[cl])/1000./sqrt(OmegaL+OmegaM*(1.+clusterz[cl])**3)*H0/70. # in Mpc r200deg=r200*1000./(cosmo.angular_diameter_distance(clusterbiweightcenter[cl]/3.e5).value*Mpcrad_kpcarcsec)/3600. cir=Circle((0,0),radius=r200deg,color='None',ec='k') gca().add_patch(cir) flag=(g.s.CLUSTER == cl)& g.sampleflag #& g.dvflag plt.hexbin(d.RA-clusterRA[cl],d.DEC-clusterDec[cl],cmap=cm.Greys,gridsize=40,vmin=0,vmax=10) if plotnofit: flag=g.sfsampleflag & ~g.sampleflag & g.dvflag & (g.s.CLUSTER == cl) plot(ra[flag],dec[flag],'rv',mec='r',mfc='None') flag=g.sampleflag & (g.s.CLUSTER == cl) #& g.membflag #print cl, len(ra[flag]),len(dec[flag]),len(s.s.SIZE_RATIO[flag]) if useirsb: color=log10(g.sigma_ir) v1=7.6 v2=10.5 colormap=cm.jet else: color=g.s.SIZE_RATIO v1=.1 v2=1 colormap='jet_r' try: plt.scatter(ra[flag],dec[flag],s=30,c=color[flag],cmap=colormap,vmin=v1,vmax=v2,edgecolors='k') except ValueError: plt.scatter(ra[flag],dec[flag],s=30,c='k',cmap=cm.jet_r,vmin=.1,vmax=1,edgecolors='k') ax=plt.gca() fsize=14 t=cluster24Box[cl] drawbox([t[0]-clusterRA[cl],t[1]-clusterDec[cl],t[2],t[3],t[4]],'g-') ax=gca() ax.invert_xaxis() if plotsingle: xlabel('$ \Delta RA \ (deg) $',fontsize=22) ylabel('$ \Delta DEC \ (deg) $',fontsize=22) legend(numpoints=1,scatterpoints=1) cname='$'+cl+'$' text(.1,.8,cname,fontsize=18,transform=ax.transAxes,horizontalalignment='left') plt.axis([1.8,-1.8,-1.8,1.8]) plt.xticks(np.arange(-1,2,1)) plt.yticks(np.arange(-1,2,1)) allax.append(ax) multiplotaxes(i) i+=1 if plotcolorbar: c=colorbar(ax=allax,fraction=0.05) c.ax.text(2.2,.5,'$R_{24}/R_d$',rotation=-90,verticalalignment='center',fontsize=20) plt.text(-.5,-.28,'$\Delta RA \ (deg) $',fontsize=26,horizontalalignment='center',transform=ax.transAxes) plt.text(-2.4,1.5,'$\Delta Dec \ $',fontsize=26,verticalalignment='center',rotation=90,transform=ax.transAxes,family='serif') #plt.savefig(homedir+'/research/LocalClusters/SamplePlots/positionson24.eps') #plt.savefig(homedir+'/research/LocalClusters/SamplePlots/positionson24.png') plt.savefig(figuredir+'fig3.pdf')
def processSelection (hdu, i_ini, i_end, field_r, field_m, field_z, field_psf, p_class1, biases, out_dir, class_fields = False, no_zeros = False): out_fn = out_dir + "sim_bias_" + str(i_ini) + "_" + str(i_end) + ".fits" if os.path.exists (out_fn): print out_fn, "exists" return print "selecting data" tbdata = hdu.data[i_ini:i_end] print "selected" data_aux = tbdata.tolist() print "tbdata.tolist() done" data_aux = np.asarray(tbdata.tolist()) print "creating rm_crit" print np.isnan (data_aux).shape rm_crit = np.isnan (data_aux).any(axis=1) | np.isinf (data_aux).any(axis=1) | (tbdata.field(field_z) <= 0) | (tbdata.field(field_r) <= 0) print "is_nan = ", np.isnan (data_aux).any(axis=1).sum() print "is_inf = ", np.isinf (data_aux).any(axis=1).sum() print field_z, " <= 0 = ", (tbdata.field(field_z) <= 0).sum() print field_r, " <= 0 = ", (tbdata.field(field_r) <= 0).sum(), tbdata.field(field_r).max() print "rm_crit.sum = ", rm_crit.sum() del data_aux print tbdata.shape tbdata = tbdata[~rm_crit] print tbdata.shape if np.isscalar (class_fields): y = (np.random.random(tbdata.shape[0]) < p_class1) else: y = createLabels (tbdata, class_fields, np.zeros (len (class_fields)) + p_class1) data_aux = tbdata.tolist() data_aux = np.asarray(tbdata.tolist()) if no_zeros: print "NULL = ", pd.isnull(data_aux).any(axis = 1).sum() #crit_zeros = (y != 0) & ~np.isnan(data_aux).any(axis = 1) crit_zeros = (y != 0) & ~pd.isnull(data_aux).any(axis = 1) y = y - 1 else: #crit_zeros = ~np.isnan(data_aux).any(axis = 1) crit_zeros = ~pd.isnull(data_aux).any(axis = 1) del data_aux y = y[crit_zeros] tbdata = tbdata[crit_zeros] r = tbdata.field(field_r) m = tbdata.field(field_m) # Uniform distributions to test # r = np.random.random (tbdata.shape[0]) # m = np.random.random (tbdata.shape[0])*30 z = tbdata.field(field_z) psf = tbdata.field(field_psf) d = cosmo.angular_diameter_distance(z).to(ap.units.kpc) # distance in kpc scale = d*ap.units.arcsec.to(ap.units.radian) # scale [kpc/arcsec] R = r * scale M = m - 5*np.log10(4.28E8*z) r_psf = 2.*np.sqrt(2*np.log(2)) * r/psf cols = [pf.Column (name = field_r, array = r, format = "D"), pf.Column (name = field_m, array = m, format = "D"), pf.Column (name = field_z, array = z, format = "D"), pf.Column (name = field_psf, array = psf, format = "D"), pf.Column (name = "absPetroMag_r", array = M, format = "D"), pf.Column (name = "petroRad_r_psf", array = r_psf, format = "D"), pf.Column (name = "petroRad_r_kpc", array = R, format = "D"), pf.Column (name = "class", array = y, format = "B")] # Create biased labels r_m = np.median(r_psf) m_m = np.median(m)-17.8 crit1 = (y == 1) r1 = r_psf[crit1] m1 = m[crit1]-17.8 factor = np.exp(-(r1*r1/(2*r_m*r_m) + m1*m1/(2*m_m*m_m))) factor [m1 >= 0] = np.exp(-(r1[m1 >= 0]**2/(2*r_m*r_m))) uniform = np.random.random(crit1.sum()) for bias in biases: print "bias = ", bias p_b = factor**(1./bias**2) y_b = y.copy() y_b[crit1] = y_b[crit1] * (uniform >= p_b) cols = cols + [pf.Column (name = "class_bias" + str(bias), array = y_b, format = "B")] hdu = pf.BinTableHDU.from_columns(cols) hdu.writeto (out_fn, clobber = True)
def ztod(z): 'return the distance from redshift z' return cosmo.angular_diameter_distance(z)
def comov_vol(z, delta_z, omega_pix): return ( omega_pix * ((cosmo.angular_diameter_distance(z).value)**2) * (sc.c / cosmo.H(0).value) * (1 / np.sqrt((0.31 * ((1 + z)**3)) + (0.69))) * ((1 + z)**2) * delta_z) ###how to deal with delta z...
cat_massive_z_slice = cat_massive_gal[abs(cat_massive_gal['zKDEPeak'] - z) < bin_size] coord_massive_gal = SkyCoord(cat_massive_z_slice['RA'] * u.deg, cat_massive_z_slice['DEC'] * u.deg) cat_all_z_slice = cat_gal[abs(cat_gal['zKDEPeak'] - z) < 0.5] massive_counts = len(cat_massive_z_slice) massive_count = 0 massive_counts_cq = 0 massive_counts_csf = 0 for gal in cat_massive_z_slice: massive_count += 1 print('Progress:' + str(massive_count) + '/' + str(len(cat_massive_z_slice)), end='\r') dis = WMAP9.angular_diameter_distance(gal['zKDEPeak']).value coord_gal = SkyCoord(gal['RA'] * u.deg, gal['DEC'] * u.deg) cat_neighbors_z_slice = cat_all_z_slice[ abs(cat_all_z_slice['zKDEPeak'] - gal['zKDEPeak']) < 1.5 * 0.03 * (1 + z)] cat_neighbors = cat_neighbors_z_slice[abs(cat_neighbors_z_slice['RA'] - gal['RA']) < 0.7 / dis / np.pi * 180] cat_neighbors = cat_neighbors[ abs(cat_neighbors['DEC'] - gal['DEC']) < 0.7 / dis / np.pi * 180] if len(cat_neighbors) == 0: # central gals which has no companion continue else: ind = KDTree(np.array(cat_neighbors['RA', 'DEC']).tolist()).query_radius( [(gal['RA'], gal['DEC'])],
import matplotlib.backends.backend_pdf import matplotlib.gridspec as gridspec import matplotlib.patches as patches import matplotlib.colors as colors from matplotlib.animation import FuncAnimation from mpl_toolkits.axes_grid1 import make_axes_locatable from matplotlib.transforms import Bbox from matplotlib.colors import LogNorm from mpl_toolkits.mplot3d import Axes3D from IPython.display import display #mcmc import emcee z = 0.014313 arc_to_kpc = (cosmo.angular_diameter_distance(z=z) / 206265).to( u.kpc) / u.arcsec import utils cubefolder = '../data/NGC6328/cubes/' cloudsfolder = '../data/NGC6328/clouds/' datafolder = '../data/NGC6328/other/' import argparse parser = argparse.ArgumentParser() parser.add_argument("--file", help="file to mine for data points (.out)") parser.add_argument("--walkers", help="number of walkers", type=int, default=2) parser.add_argument("--steps", help="number of steps", type=int, default=300) parser.add_argument("--save", help="ssave file of the chains") args = parser.parse_args()