def plot_cosmology(zs, mu_mcmc, mu_minuit, std_mcmc, std_minuit, n): import matplotlib.pyplot as plt from matplotlib import gridspec from matplotlib.ticker import MaxNLocator fig = plt.figure(figsize=(4.5, 5.5)) gs = gridspec.GridSpec(2, 1, height_ratios=[2, 1], hspace=0.0, wspace=0.0) ax0 = fig.add_subplot(gs[0]) ax1 = fig.add_subplot(gs[1], sharex=ax0) axes = [ax0, ax1] zsort = sorted(zs) distmod = WMAP9.distmod(zsort).value - 19.3 distmod2 = WMAP9.distmod(zs).value - 19.3 ms = 2 alpha = 0.4 axes[0].errorbar(zs, mu_minuit, yerr=np.sqrt(std_minuit*std_minuit + 0.1*0.1), ms=ms, fmt='o', label=r"Max. Likelihood", color="r", alpha=alpha) axes[0].errorbar(zs, mu_mcmc, yerr=np.sqrt(std_mcmc*std_mcmc + 0.1*0.1), fmt='o', ms=ms, label=r"MCMC", color="b", alpha=alpha) axes[1].errorbar(zs, mu_minuit - distmod2, yerr=np.sqrt(std_minuit*std_minuit + 0.1*0.1), ms=ms, fmt='o', label=r"Max. Likelihood", color="r", alpha=alpha) axes[1].errorbar(zs, mu_mcmc - distmod2, yerr=np.sqrt(std_mcmc*std_mcmc + 0.1*0.1), fmt='o', ms=ms, label=r"MCMC", color="b", alpha=alpha) axes[0].plot(zsort, distmod, 'k') axes[1].axhline(0, color='k') axes[1].set_xlabel("$z$") axes[0].set_ylabel(r"$\mu$") axes[1].set_ylabel(r"$\mu_{\rm obs} - \mu(\mathcal{C})$") axes[0].legend(loc=2, frameon=False) plt.setp(ax0.get_xticklabels(), visible=False) ax0.yaxis.set_major_locator(MaxNLocator(7, prune="lower")) ax1.yaxis.set_major_locator(MaxNLocator(3)) fig.savefig("output/obs_cosmology_%s.png" % n, bbox_inches="tight", dpi=300, transparent=True) fig.savefig("output/obs_cosmology_%s.pdf" % n, bbox_inches="tight", dpi=300, transparent=True)
def get_distance_modulos(SN_df, z = 'CMB', cosmo = 'WMAP9'): if cosmo == 'WMAP9': from astropy.cosmology import WMAP9 as cosmo else: from astropy.cosmology import WMAP7 as cosmo if z == 'CMB': z_distmod = cosmo.distmod(SN_df['z_cmb_salt'].values) else: z_distmod = cosmo.distmod(SN_df['zhel_spec'].values) return z_distmod
def get_catalog(params, map_struct): cat, = Vizier.get_catalogs('J/ApJS/199/26/table3') completeness = 0.5 alpha = -1.0 MK_star = -23.55 MK_max = MK_star + 2.5 * np.log10(gammaincinv(alpha + 2, completeness)) z = (u.Quantity(cat['cz']) / c.c).to(u.dimensionless_unscaled) MK = cat['Ktmag'] - cosmo.distmod(z) keep = (z > 0) & (MK < MK_max) cat = cat[keep] z = z[keep] r = cosmo.luminosity_distance(z).to('Mpc').value theta = 0.5 * np.pi - cat['DEJ2000'].to('rad').value phi = cat['RAJ2000'].to('rad').value ipix = hp.ang2pix(map_struct["nside"], theta, phi) if "distnorm" in map_struct: dp_dV = map_struct["prob"][ipix] * map_struct["distnorm"][ipix] * norm( map_struct["distmu"][ipix], map_struct["distsigma"][ipix]).pdf(r) / map_struct["pixarea"] top50 = cat[np.flipud(np.argsort(dp_dV))][:50] else: top50 = cat[np.flipud(np.argsort(map_struct["prob"][ipix]))][:50] catalogfile = os.path.join(params["outputDir"], 'catalog.dat') top50['RAJ2000', 'DEJ2000', 'Ktmag'].write(catalogfile, format='ascii') return top50
def volume_limit(sample,zmax,magcol,appmag): # Volume-limit the sample based on the detection limit and redshift appmag_lim = appmag * u.mag distmod = WMAP9.distmod(zmax) absmag_lim = appmag_lim - distmod absmags = sample[magcol] - WMAP9.distmod(sample['Z_BEST']).value #print sample['imaging'][0] #print 'absmag_lim',absmag_lim #print 'appmag_lim',appmag_lim #print 'distmod',distmod vl_ind = (absmags < absmag_lim.value) & (sample['Z_BEST'] > 0) & (sample['Z_BEST'] < zmax) return sample[vl_ind]
def SNcosmo_template(): # Pick cosmology from astropy.cosmology import WMAP9 as cosmo #from astropy.cosmology import FlatLambdaCDM #cosmo = FlatLambdaCDM(H0=69.6, Om0=0.286) from astropy import units as u import astropy.constants as const # Parameters obsfilter = inst.filter # This is the name of the sample band we use # Other defined things absmag_V = -19.3 # We use this to normalize (needed?) magsystem = 'ab' # The magnitude system (both ab and vega should work) modelphase = 0 # The phase of the SN template = I.Source # The name of the SN template (e.g. salt2 or hsiao) # When using new bands, assuming these are in newband directory # e.g. from WFC3IR from http://www.stsci.edu/~WFC3/UVIS/SystemThroughput/ bandinfo = {} for filterfile in os.listdir(I.Inst): if filterfile == '.DS_Store': pass else: words = re.split('\.',filterfile) filterdata = np.genfromtxt(I.Inst+'/'+filterfile ) # Remove regions with zero throughput #iNonzero = np.where( filterdata[:,1]>0 )[0] band = sncosmo.Bandpass(i_wave,np.ones(len(i_wave)),name=words[0]) #band = sncosmo.Bandpass(filterdata[:,0],filterdata[:,1],name=words[0]) sncosmo.registry.register(band) # Scale template to chosen absolute V (Vega) model = sncosmo.Model(source=template) model.set(z=0) magdiff = model.bandmag('bessellv','vega',[0])-absmag_V templatescale = 10**(0.4*magdiff) print 'Get scale %s to match absolute mag.'%(templatescale) # Get distance modulus DM = cosmo.distmod(z) print 'Get Distance Modulus %s'%(DM) # Create a model assuming both of these scales model = sncosmo.Model(source=template) print model model.set(x0=templatescale*10**(-0.4*DM.value),z=z) #this only works for salt2 model. need amplitude for hsiao model # Derive the observed magnitude and flux in chosen filter obsmag = model.bandmag(obsfilter,magsystem,[modelphase]) bandflux = model.bandflux(obsfilter, modelphase ) # Flux in photons/s/cm^2 print 'We get mag %s (%s) and flux %s photons/s/cm^2'%(obsmag,magsystem,bandflux) return model.flux(modelphase,i_wave) #wave is in observer frame
def vl_plot(): # Plot the volume-limited samples split by survey tasca = fits.getdata('{0}/comparisons/COSMOS/cosmos_tasca_gzh.fits'.format(gzh_path),1) cassata = fits.getdata('{0}/comparisons/COSMOS/cosmos_cassata_gzh.fits'.format(gzh_path),1) zest = fits.getdata('{0}/comparisons/COSMOS/cosmos_zest_gzh.fits'.format(gzh_path),1) aegis = fits.getdata('{0}/comparisons/AEGIS/aegis_gzh.fits'.format(gzh_path),1) gems = fits.getdata('{0}/comparisons/GEMS/gems_gzh.fits'.format(gzh_path),1) goods = fits.getdata('{0}/comparisons/GOODS/goods_gzh.fits'.format(gzh_path),1) dlist = get_dlist(aegis,gems,goods,cassata,tasca,zest) fig,axarr = plt.subplots(3,2,sharex=True,sharey=True,figsize=(12,12)) for s,ax in zip(dlist,axarr.ravel()): d = s['data'] newdata = volume_limit(d,1.0,s['magcol'],s['maglim']) olddata = d[d['Z_BEST'] > 0.] absmag_old = olddata[s['magcol']] - WMAP9.distmod(olddata['Z_BEST']).value absmag_new = newdata[s['magcol']] - WMAP9.distmod(newdata['Z_BEST']).value appmag_old = olddata[s['magcol']] appmag_new = newdata[s['magcol']] s['data'] = newdata ax.scatter(olddata['Z_BEST'],absmag_old,c='gray') ax.scatter(newdata['Z_BEST'],absmag_new,c='red') ax.set_xlim(-0.5,4) ax.set_ylim(-15,-25) ax.set_xlabel(r'$z$',fontsize=20) ax.set_ylabel(r'$M$',fontsize=20) ax.set_title("{0} ".format(s['survey'])+r'$m_\mathrm{I|i|z}<$'+'{0:.1f}'.format(s['maglim']),fontsize=20) fig.tight_layout() plt.show() plt.close() return None
def get_supernova_data(n=1500, nact=1000, ston_thresh=5, shallow=True): zp, name = get_zp_and_name(shallow) temp_dir = os.path.dirname(__file__) + "/output/supernova_data_%s" % name if not os.path.exists(temp_dir): os.makedirs(temp_dir) ress = Parallel(n_jobs=4, max_nbytes="20M", batch_size=5)(delayed(get_result)( temp_dir, zp, i, 0.1, False, False) for i in range(n)) res = np.array([r for r in ress if r is not None]) ston = res[:, 6] res = res[ston > ston_thresh, :] res = res[:nact, :] diff = np.abs(res[:, 7] - (WMAP9.distmod(res[:, 1]).value - 19.3)) res = res[(diff < 4), :] # [seed, z, t0, x0, x1, c, ston] + mus + stds) # zs, mu_mcmc, mu_minuit, std_mcmc, std_minuit print("Supernova data", res.shape) return res[:, 1], res[:, 7], res[:, 8], res[:, 9], res[:, 10]
def cosmoDistMod(redshift, WMAP9=False, H0=70.0, Om0=0.30, Planck15=False): """ Get the Distance Module 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) dm = cosmo.distmod(redshift) return dm.value
def color_mag_plots(mgs, s82, decals, savefig=False): # Make paneled histograms of the color distribution for several magnitude bins of Galaxy Zoo data. """ SDSS main sample (GZ2) Stripe 82 coadded data (GZ2) DECaLS """ redshifts = (0.12, 0.08, 0.05) appmag_lim = 17.0 # Work out the magnitude limit from cosmology fig, axarr = plt.subplots(num=1, nrows=3, ncols=3, figsize=(12, 10)) for z, ax in zip(redshifts, axarr.ravel()): absmag_lim = appmag_lim - WMAP9.distmod(z).value maglim = (mgs['PETROMAG_MR'] < absmag_lim) & (mgs['REDSHIFT'] <= z) spiral = mgs[ 't01_smooth_or_features_a02_features_or_disk_weighted_fraction'] >= 0.8 elliptical = mgs[ 't01_smooth_or_features_a01_smooth_weighted_fraction'] >= 0.8 ax.hist(mgs[maglim & spiral]['PETROMAG_U'] - mgs[maglim & spiral]['PETROMAG_R'], range=(0, 4), bins=25, color='blue', histtype='step', label='spiral') ax.hist(mgs[maglim & elliptical]['PETROMAG_U'] - mgs[maglim & elliptical]['PETROMAG_R'], range=(0, 4), bins=25, color='red', histtype='step', label='elliptical') ax.set_xlabel(r'$(u-r)$', fontsize=16) ax.set_title(r'$M_r<{0:.2f}, z<{1:.2f}$'.format(absmag_lim, z), fontsize=16) ax.text(0.95, 0.95, 'MGS', ha='right', va='top', transform=ax.transAxes) if ax == axarr.ravel()[0]: ax.legend(loc='upper left', fontsize=10) s82_lim = 17.77 for z, ax in zip(redshifts, axarr.ravel()[3:6]): absmag_lim = s82_lim - WMAP9.distmod(z).value maglim = (s82['PETROMAG_MR'] < absmag_lim) & (s82['REDSHIFT'] <= z) spiral = s82[ 't01_smooth_or_features_a02_features_or_disk_weighted_fraction'] >= 0.8 elliptical = s82[ 't01_smooth_or_features_a01_smooth_weighted_fraction'] >= 0.8 ax.hist(s82[maglim & spiral]['PETROMAG_U'] - s82[maglim & spiral]['PETROMAG_R'], range=(0, 4), bins=25, color='blue', histtype='step', label='spiral') ax.hist(s82[maglim & elliptical]['PETROMAG_U'] - s82[maglim & elliptical]['PETROMAG_R'], range=(0, 4), bins=25, color='red', histtype='step', label='elliptical') ax.set_xlabel(r'$(u-r)$', fontsize=16) ax.set_title(r'$M_r<{0:.2f}, z<{1:.2f}$'.format(absmag_lim, z), fontsize=16) ax.text(0.95, 0.95, 'Stripe 82', ha='right', va='top', transform=ax.transAxes) decals_lim = 17.77 for z, ax in zip(redshifts, axarr.ravel()[6:]): absmag_lim = decals_lim - WMAP9.distmod(z).value maglim = (decals['metadata.mag.abs_r'] < absmag_lim) & (decals['metadata.redshift'] <= z) spiral = decals['t00_smooth_or_features_a1_features_frac'] >= 0.8 elliptical = decals['t00_smooth_or_features_a0_smooth_frac'] >= 0.8 ax.hist(decals[maglim & spiral]['metadata.mag.u'] - decals[maglim & spiral]['metadata.mag.r'], range=(0, 4), bins=25, color='blue', histtype='step', label='spiral') ax.hist(decals[maglim & elliptical]['metadata.mag.u'] - decals[maglim & elliptical]['metadata.mag.r'], range=(0, 4), bins=25, color='red', histtype='step', label='elliptical') ax.set_xlabel(r'$(u-r)$', fontsize=16) ax.set_title(r'$M_r<{0:.2f}, z<{1:.2f}$'.format(absmag_lim, z), fontsize=16) ax.text(0.95, 0.95, 'DECaLS', ha='right', va='top', transform=ax.transAxes) fig.tight_layout() if savefig: plt.savefig('{0}/color_hist.pdf'.format(plot_path)) else: plt.show() return None
def color_mag_ratio(mgs, s82, decal, savefig=False): # Plot the spiral to elliptical ratio as a function of optical color. redshifts = (0.12, 0.08, 0.05) linestyles = ('solid', 'dashed', 'dashdot') datasets = ({ 'data': mgs, 'title': 'MGS', 'appmag': 17.0, 'sp': 't01_smooth_or_features_a02_features_or_disk_weighted_fraction', 'el': 't01_smooth_or_features_a01_smooth_weighted_fraction', 'umag': 'PETROMAG_U', 'rmag': 'PETROMAG_R', 'absr': 'PETROMAG_MR', 'redshift': 'REDSHIFT' }, { 'data': s82, 'title': 'Stripe 82', 'appmag': 17.77, 'sp': 't01_smooth_or_features_a02_features_or_disk_weighted_fraction', 'el': 't01_smooth_or_features_a01_smooth_weighted_fraction', 'umag': 'PETROMAG_U', 'rmag': 'PETROMAG_R', 'absr': 'PETROMAG_MR', 'redshift': 'REDSHIFT' }, { 'data': decals, 'title': 'DECaLS', 'appmag': 17.77, 'sp': 't00_smooth_or_features_a1_features_frac', 'el': 't00_smooth_or_features_a0_smooth_frac', 'umag': 'metadata.mag.u', 'rmag': 'metadata.mag.r', 'absr': 'metadata.mag.abs_r', 'redshift': 'metadata.redshift' }) # Work out the magnitude limit from cosmology fig, axarr = plt.subplots(num=2, nrows=1, ncols=3, figsize=(12, 5)) for ax, d in zip(axarr.ravel(), datasets): for z, ls in zip(redshifts, linestyles): absmag_lim = d['appmag'] - WMAP9.distmod(z).value maglim = (d['data'][d['absr']] < absmag_lim) & (d['data'][d['redshift']] <= z) spiral = d['data'][d['sp']] >= 0.8 elliptical = d['data'][d['el']] >= 0.8 n_sp, bins_sp = np.histogram( d['data'][maglim & spiral][d['umag']] - d['data'][maglim & spiral][d['rmag']], range=(0, 4), bins=25) n_el, bins_el = np.histogram( d['data'][maglim & elliptical][d['umag']] - d['data'][maglim & elliptical][d['rmag']], range=(0, 4), bins=25) plotval = np.log10(n_sp * 1. / n_el) ax.plot(bins_sp[1:], plotval, linestyle=ls, label=r'$M_r<{0:.2f}, z<{1:.2f}$'.format(absmag_lim, z)) ax.set_xlabel(r'$(u-r)$', fontsize=16) ax.set_ylabel(r'$\log(n_{sp}/n_{el})$', fontsize=16) ax.set_ylim(-1.5, 1.5) ax.set_title(d['title'], fontsize=16) if ax == axarr.ravel()[0]: ax.legend(loc='upper left', fontsize=8) fig.tight_layout() if savefig: plt.savefig('{0}/feature_ratio.pdf'.format(plot_path)) else: plt.show() return None
mask = np.isfinite(data[zcol]) & (data[zcol] > 0.) & np.logical_not(prevmask) blankz[mask] = data[zcol][mask] prevmask = mask return blankz zarr = choose_redshift(data) # Calculate absolute size in kpc from astropy.cosmology import WMAP9 from astropy import units as u size = [WMAP9.kpc_proper_per_arcmin(z).to(u.kpc/u.arcsec) * (r * u.arcsec) if z > 0 else -99. * u.kpc for z,r in zip(zarr,data['KRON_RADIUS_I'])] #re = 0.162 * data['FLUX_RADIUS1_B_1']**1.87 absmag = [zmag * u.mag - WMAP9.distmod(z) if z > 0 else -99. * u.mag for z,zmag in zip(zarr,data['Z_1'])] data.rename_column('hubble_id_1' , 'hubble_id' ) data.rename_column('coords_ra_1' , 'ra') data.rename_column('coords_dec_1' , 'dec') data.rename_column('KRON_RADIUS_I' , 'kron_radius_I') data.rename_column('FLUX_RADIUS1_I', 'flux_radius1_I') data.rename_column('B_1' , 'B') data.rename_column('V_1' , 'V') data.rename_column('I_1' , 'I') data.rename_column('Z_1' , 'Z') data.rename_column('group_type_1' , 'group_type') data.rename_column('retire_at_1' , 'retire_at') data.rename_column('survey_1' , 'survey') data.rename_column('depth_1' , 'depth') del data['KRON_RADIUS_B_1']
return data # Plot the CMD as a function of GZ1 morphology for the RGZ 75% counterparts data = ascii.read('/Users/willettk/Astronomy/Research/GalaxyZoo/rgz-analysis/rgz_wise_75_sdss_magerr.csv') fname_spec = 'rgz_75_sdss_mags_spec.vot' fname_nospec = 'rgz_75_sdss_mags_nospec.vot' zoospec = votable_read('%s/%s' % (rgzdir,fname_spec)) zoonospec = votable_read('%s/%s' % (rgzdir,fname_nospec)) na10 = na10_read() gr_na10 = na10['g-r'] distmod_na10 = WMAP9.distmod(na10['z']) Mr_na10 = na10['r'] - distmod_na10.value Mr_na10 = na10['M_g'] - gr_na10 # Generate contours of the NA10 sample magbins = np.linspace(-24,-20,20) colorbins = np.linspace(0.3,1.0,20) hc,xc,yc = np.histogram2d(Mr_na10,gr_na10,bins=(magbins,colorbins)) levels=np.linspace(0,200,10) # Make 2x2 figure fig,axarr = plt.subplots(2,2,figsize=(10,10)) from scipy.ndimage.filters import gaussian_filter
#!/usr/bin/env python2.7 import numpy as np import matplotlib.pyplot as plt from astropy.cosmology import WMAP9 as cosmo, get_current import astropy from background import * zs = np.linspace(0, .35, 100) plt.figure() plt.plot(zs, mstar(zs) - cosmo.distmod(zs).value) plt.xlabel('redshift') plt.ylabel('Absolute Mag') plt.grid() plt.figure() plt.plot(zs, mstar(zs)) plt.xlabel('redshift') plt.ylabel('Apparent Mag') plt.grid() plt.figure() plt.plot(zs, -cosmo.distmod(zs).value) plt.xlabel('redshift') plt.ylabel('Distance Modulus') plt.grid() plt.show()
def get_catalog(params, map_struct): if not os.path.isdir(params["catalogDir"]): os.makedirs(params["catalogDir"]) catalogFile = os.path.join(params["catalogDir"], "%s.hdf5" % params["galaxy_catalog"]) """AB Magnitude zero point.""" MAB0 = -2.5 * np.log10(3631.e-23) pc_cm = 3.08568025e18 const = 4. * np.pi * (10. * pc_cm)**2. if params["galaxy_catalog"] == "2MRS": if not os.path.isfile(catalogFile): import astropy.constants as c cat, = Vizier.get_catalogs('J/ApJS/199/26/table3') ra, dec = cat["RAJ2000"], cat["DEJ2000"] cz = cat["cz"] magk = cat["Ktmag"] z = (u.Quantity(cat['cz']) / c.c).to(u.dimensionless_unscaled) completeness = 0.5 alpha = -1.0 MK_star = -23.55 MK_max = MK_star + 2.5 * np.log10(gammaincinv(alpha + 2, completeness)) MK = magk - cosmo.distmod(z) idx = (z > 0) & (MK < MK_max) ra, dec = ra[idx], dec[idx] z = z[idx] magk = magk[idx] distmpc = cosmo.luminosity_distance(z).to('Mpc').value with h5py.File(catalogFile, 'w') as f: f.create_dataset('ra', data=ra) f.create_dataset('dec', data=dec) f.create_dataset('z', data=z) f.create_dataset('magk', data=magk) f.create_dataset('distmpc', data=distmpc) else: with h5py.File(catalogFile, 'r') as f: ra, dec = f['ra'][:], f['dec'][:] z = f['z'][:] magk = f['magk'][:] distmpc = f['distmpc'][:] r = distmpc * 1.0 mag = magk * 1.0 elif params["galaxy_catalog"] == "GLADE": if not os.path.isfile(catalogFile): cat, = Vizier.get_catalogs('VII/281/glade2') ra, dec = cat["RAJ2000"], cat["DEJ2000"] distmpc, z = cat["Dist"], cat["z"] magb, magk = cat["Bmag"], cat["Kmag"] # Keep track of galaxy identifier GWGC, PGC, HyperLEDA = cat["GWGC"], cat["PGC"], cat["HyperLEDA"] _2MASS, SDSS = cat["_2MASS"], cat["SDSS-DR12"] idx = np.where(distmpc >= 0)[0] ra, dec = ra[idx], dec[idx] distmpc, z = distmpc[idx], z[idx] magb, magk = magb[idx], magk[idx] GWGC, PGC, HyperLEDA = GWGC[idx], PGC[idx], HyperLEDA[idx] _2MASS, SDSS = _2MASS[idx], SDSS[idx] with h5py.File(catalogFile, 'w') as f: f.create_dataset('ra', data=ra) f.create_dataset('dec', data=dec) f.create_dataset('distmpc', data=distmpc) f.create_dataset('magb', data=magb) f.create_dataset('magk', data=magk) f.create_dataset('z', data=z) # Add galaxy identifier f.create_dataset('GWGC', data=GWGC) f.create_dataset('PGC', data=PGC) f.create_dataset('HyperLEDA', data=HyperLEDA) f.create_dataset('2MASS', data=_2MASS) f.create_dataset('SDSS', data=SDSS) else: with h5py.File(catalogFile, 'r') as f: ra, dec = f['ra'][:], f['dec'][:] distmpc, z = f['distmpc'][:], f['z'][:] magb, magk = f['magb'][:], f['magk'][:] GWGC, PGC, _2MASS = f['GWGC'][:], f['PGC'][:], f['2MASS'][:] HyperLEDA, SDSS = f['HyperLEDA'][:], f['SDSS'][:] # Convert bytestring to unicode GWGC = GWGC.astype('U') PGC = PGC.astype('U') HyperLEDA = HyperLEDA.astype('U') _2MASS = _2MASS.astype('U') SDSS = SDSS.astype('U') # Keep only galaxies with finite B mag when using it in the grade if params["galaxy_grade"] == "S": idx = np.where(~np.isnan(magb))[0] ra, dec, distmpc = ra[idx], dec[idx], distmpc[idx] magb, magk = magb[idx], magk[idx] GWGC, PGC, HyperLEDA = GWGC[idx], PGC[idx], HyperLEDA[idx] _2MASS, SDSS = _2MASS[idx], SDSS[idx] r = distmpc * 1.0 mag = magb * 1.0 elif params["galaxy_catalog"] == "CLU": if not os.path.isfile(catalogFile): raise ValueError("Please add %s." % catalogFile) with h5py.File(catalogFile, 'r') as f: name = f['name'][:] ra, dec = f['ra'][:], f['dec'][:] sfr_fuv, mstar = f['sfr_fuv'][:], f['mstar'][:] distmpc, magb = f['distmpc'][:], f['magb'][:] a, b2a, pa = f['a'][:], f['b2a'][:], f['pa'][:] btc = f['btc'][:] idx = np.where(distmpc >= 0)[0] ra, dec = ra[idx], dec[idx] sfr_fuv, mstar = sfr_fuv[idx], mstar[idx] distmpc, magb = distmpc[idx], magb[idx] a, b2a, pa = a[idx], b2a[idx], pa[idx] btc = btc[idx] idx = np.where(~np.isnan(magb))[0] ra, dec = ra[idx], dec[idx] sfr_fuv, mstar = sfr_fuv[idx], mstar[idx] distmpc, magb = distmpc[idx], magb[idx] a, b2a, pa = a[idx], b2a[idx], pa[idx] btc = btc[idx] z = -1*np.ones(distmpc.shape) r = distmpc * 1.0 mag = magb * 1.0 n, cl = params["powerlaw_n"], params["powerlaw_cl"] dist_exp = params["powerlaw_dist_exp"] prob_scaled = copy.deepcopy(map_struct["prob"]) prob_sorted = np.sort(prob_scaled)[::-1] prob_indexes = np.argsort(prob_scaled)[::-1] prob_cumsum = np.cumsum(prob_sorted) index = np.argmin(np.abs(prob_cumsum - cl)) + 1 prob_scaled[prob_indexes[index:]] = 0.0 prob_scaled = prob_scaled**n theta = 0.5 * np.pi - dec * 2 * np.pi / 360.0 phi = ra * 2 * np.pi / 360.0 ipix = hp.ang2pix(map_struct["nside"], ra, dec, lonlat=True) if "distnorm" in map_struct: if map_struct["distnorm"] is not None: #creat an mask to cut at 3 sigma in distance mask = np.zeros(len(r)) condition_indexer = np.where( (r < (map_struct["distmu"][ipix] + (3*map_struct["distsigma"][ipix]))) & (r > (map_struct["distmu"][ipix] - (3*map_struct["distsigma"][ipix])) )) mask[condition_indexer] = 1 Sloc = prob_scaled[ipix] * (map_struct["distnorm"][ipix] * norm(map_struct["distmu"][ipix], map_struct["distsigma"][ipix]).pdf(r))**params["powerlaw_dist_exp"] / map_struct["pixarea"] #multiplie the Sloc by 1 or 0 according to the 3 sigma condistion Sloc = Sloc*mask idx = np.where(condition_indexer)[0] else: Sloc = copy.copy(prob_scaled[ipix]) idx = np.arange(len(r)).astype(int) else: Sloc = copy.copy(prob_scaled[ipix]) idx = np.arange(len(r)).astype(int) # this happens when we are using a tiny catalog... if np.all(Sloc == 0.0): Sloc[:] = 1.0 L_nu = const * 10.**((mag + MAB0)/(-2.5)) L_nu = L_nu / np.nanmax(L_nu[idx]) L_nu = L_nu**params["catalog_n"] L_nu[L_nu < 0.001] = 0.001 L_nu[L_nu > 1.0] = 1.0 Slum = L_nu / np.sum(L_nu) mlim, M_KNmin, M_KNmax = 22, -17, -12 L_KNmin = const * 10.**((M_KNmin + MAB0)/(-2.5)) L_KNmax = const * 10.**((M_KNmax + MAB0)/(-2.5)) Llim = 4. * np.pi * (r * 1e6 * pc_cm)**2. * 10.**((mlim + MAB0)/(-2.5)) Sdet = (L_KNmax-Llim)/(L_KNmax-L_KNmin) Sdet[Sdet < 0.01] = 0.01 Sdet[Sdet > 1.0] = 1.0 # Set nan values to zero Sloc[np.isnan(Sloc)] = 0 Slum[np.isnan(Slum)] = 0 S = Sloc*Slum*Sdet prob = np.zeros(map_struct["prob"].shape) if params["galaxy_grade"] == "Sloc": for j in range(len(ipix)): prob[ipix[j]] += Sloc[j] grade = Sloc elif params["galaxy_grade"] == "S": for j in range(len(ipix)): prob[ipix[j]] += S[j] grade = S #prob[prob==0] = 1e-10 prob = prob / np.sum(prob) map_struct['prob_catalog'] = prob if params["doUseCatalog"]: map_struct['prob'] = prob idx = np.where(~np.isnan(grade))[0] grade = grade[idx] ra, dec, Sloc, S = ra[idx], dec[idx], Sloc[idx], S[idx] distmpc, z = distmpc[idx], z[idx] if params["galaxy_catalog"] == "GLADE": GWGC, PGC, HyperLEDA = GWGC[idx], PGC[idx], HyperLEDA[idx] _2MASS, SDSS = _2MASS[idx], SDSS[idx] """ Sthresh = np.max(grade)*0.01 idx = np.where(grade >= Sthresh)[0] grade = grade[idx] ra, dec, Sloc, S = ra[idx], dec[idx], Sloc[idx], S[idx] distmpc, z = distmpc[idx], z[idx] if params["galaxy_catalog"] == "GLADE": GWGC, PGC, HyperLEDA = GWGC[idx], PGC[idx], HyperLEDA[idx] _2MASS, SDSS = _2MASS[idx], SDSS[idx] """ idx = np.argsort(grade)[::-1] grade = grade[idx] ra, dec, Sloc, S = ra[idx], dec[idx], Sloc[idx], S[idx] distmpc, z = distmpc[idx], z[idx] if params["galaxy_catalog"] == "GLADE": GWGC, PGC, HyperLEDA = GWGC[idx], PGC[idx], HyperLEDA[idx] _2MASS, SDSS = _2MASS[idx], SDSS[idx] # Keep only galaxies within 3sigma in distance mask = Sloc > 0 ra, dec, Sloc, S = ra[mask], dec[mask], Sloc[mask], S[mask] distmpc, z = distmpc[mask], z[mask] if params["galaxy_catalog"] == "GLADE": GWGC, PGC, HyperLEDA = GWGC[mask], PGC[mask], HyperLEDA[mask] _2MASS, SDSS = _2MASS[mask], SDSS[mask] if len(ra) > 2000: print('Cutting catalog to top 2000 galaxies...') idx = np.arange(2000).astype(int) ra, dec, Sloc, S = ra[idx], dec[idx], Sloc[idx], S[idx] distmpc, z = distmpc[idx], z[idx] if params["galaxy_catalog"] == "GLADE": GWGC, PGC, HyperLEDA = GWGC[idx], PGC[idx], HyperLEDA[idx] _2MASS, SDSS = _2MASS[idx], SDSS[idx] # now normalize the distributions S = S / np.sum(S) Sloc = Sloc / np.sum(Sloc) catalog_struct = {} catalog_struct["ra"] = ra catalog_struct["dec"] = dec catalog_struct["Sloc"] = Sloc catalog_struct["S"] = S if params["writeCatalog"]: catalogfile = os.path.join(params["outputDir"], 'catalog.csv') fid = open(catalogfile, 'w') cnt = 1 if params["galaxy_catalog"] == "GLADE": fid.write("id, RAJ2000, DEJ2000, Sloc, S, Dist, z, GWGC, PGC, HyperLEDA, 2MASS, SDSS\n") for a, b, c, d, e, f, g, h, i, j, k in zip(ra, dec, Sloc, S, distmpc, z, GWGC, PGC, HyperLEDA, _2MASS, SDSS): fid.write("%d, %.5f, %.5f, %.5e, %.5e, %.4f, %.4f, %s, %s, %s, %s, %s\n" % (cnt, a, b, c, d, e, f, g, h, i, j, k)) cnt = cnt + 1 else: fid.write("id, RAJ2000, DEJ2000, Sloc, S, Dist, z\n") for a, b, c, d, e, f in zip(ra, dec, Sloc, S, distmpc, z): fid.write("%d, %.5f, %.5f, %.5e, %.5e, %.4f, %.4f\n" % (cnt, a, b, c, d, e, f)) cnt = cnt + 1 fid.close() return map_struct, catalog_struct
def color_mag_ratio(mgs,s82,decal,savefig=False): # Plot the spiral to elliptical ratio as a function of optical color. redshifts = (0.12,0.08,0.05) linestyles = ('solid','dashed','dashdot') datasets = ({'data':mgs, 'title':'MGS', 'appmag':17.0, 'sp':'t01_smooth_or_features_a02_features_or_disk_weighted_fraction', 'el':'t01_smooth_or_features_a01_smooth_weighted_fraction', 'umag':'PETROMAG_U', 'rmag':'PETROMAG_R', 'absr':'PETROMAG_MR', 'redshift':'REDSHIFT'}, {'data':s82, 'title':'Stripe 82', 'appmag':17.77, 'sp':'t01_smooth_or_features_a02_features_or_disk_weighted_fraction', 'el':'t01_smooth_or_features_a01_smooth_weighted_fraction', 'umag':'PETROMAG_U', 'rmag':'PETROMAG_R', 'absr':'PETROMAG_MR', 'redshift':'REDSHIFT'}, {'data':decals, 'title':'DECaLS', 'appmag':17.77, 'sp':'t00_smooth_or_features_a1_features_frac', 'el':'t00_smooth_or_features_a0_smooth_frac', 'umag':'metadata.mag.u', 'rmag':'metadata.mag.r', 'absr':'metadata.mag.abs_r', 'redshift':'metadata.redshift'}) # Work out the magnitude limit from cosmology fig,axarr = plt.subplots(num=2,nrows=1,ncols=3,figsize=(12,5)) for ax,d in zip(axarr.ravel(),datasets): for z,ls in zip(redshifts,linestyles): absmag_lim = d['appmag'] - WMAP9.distmod(z).value maglim = (d['data'][d['absr']] < absmag_lim) & (d['data'][d['redshift']] <= z) spiral = d['data'][d['sp']] >= 0.8 elliptical = d['data'][d['el']] >= 0.8 n_sp,bins_sp = np.histogram(d['data'][maglim & spiral][d['umag']] - d['data'][maglim & spiral][d['rmag']],range=(0,4),bins=25) n_el,bins_el = np.histogram(d['data'][maglim & elliptical][d['umag']] - d['data'][maglim & elliptical][d['rmag']],range=(0,4),bins=25) plotval = np.log10(n_sp * 1./n_el) ax.plot(bins_sp[1:],plotval,linestyle=ls,label=r'$M_r<{0:.2f}, z<{1:.2f}$'.format(absmag_lim,z)) ax.set_xlabel(r'$(u-r)$',fontsize=16) ax.set_ylabel(r'$\log(n_{sp}/n_{el})$',fontsize=16) ax.set_ylim(-1.5,1.5) ax.set_title(d['title'],fontsize=16) if ax == axarr.ravel()[0]: ax.legend(loc='upper left',fontsize=8) fig.tight_layout() if savefig: plt.savefig('{0}/feature_ratio.pdf'.format(plot_path)) else: plt.show() return None
# Remove regions with zero throughput iNonzero = np.where( filterdata[:,2]>0 )[0] band = sncosmo.Bandpass(filterdata[iNonzero,1],filterdata[iNonzero,2],name=words[0]) sncosmo.registry.register(band) # Scale template to chosen absolute V (Vega) model = sncosmo.Model(source=template) model.set(z=0) magdiff = model.bandmag('bessellv','vega',[0])-absmag_V templatescale = 10**(0.4*magdiff) print 'Get scale %s to match absolute mag.'%(templatescale) # Get distance modulus DM = cosmo.distmod(z) print 'Get Distance Modulus %s'%(DM) # Create a model assuming both of these scales model = sncosmo.Model(source=template) model.set(x0=templatescale*10**(-0.4*DM.value),z=z) #this only works for salt2 model. need amplitude for hsiao model # Derive the observed magnitude and flux in chosen filter obsmag = model.bandmag(obsfilter,magsystem,[modelphase]) bandflux = model.bandflux(obsfilter, modelphase ) # Flux in photons/s/cm^2 print 'We get mag %s (%s) and flux %s photons/s/cm^2'%(obsmag,magsystem,bandflux) # Get fluxes (in ergs/s/cm^2/A) spectrum = model.flux(modelphase,wave) plt.figure(1) plt.plot(wave,spectrum)
# Histograms of PWV values and airmasses hist_args = dict(plot_width=400, color='white') pwv_hist = lc_sims.snat_sim_bokeh.histogram('pwv', 'Simulated PWV', line_color='#3A5785', bins=np.arange(0, 35), **hist_args) model_hist = lc_sims.snat_sim_bokeh.histogram('pwv_model', 'Modeled PWV', line_color='orange', bins=np.arange(0, 35), **hist_args) airmass_hist = lc_sims.snat_sim_bokeh.histogram('airmass', 'Airmass', line_color='grey', bins=np.arange(1, 1.75, .01), **hist_args) # Plot the apparent mag and distance modulus mag_scatter = pipeline_output.snat_sim_bokeh.scatter('sim_z', 'mb', 'z', 'Apparent B-band', plot_width=600) mu_scatter = pipeline_output.snat_sim_bokeh.scatter('sim_z', 'mu', 'z', 'Fitted Distance Modulus', plot_width=600) z = np.arange(pipeline_output.sim_z.min(), pipeline_output.sim_z.max() + .005, .005) mu_scatter.line(z, betoule_cosmo.distmod(z), color='red', legend_label='Betoule et al. 2014') mu_scatter.line(z, wmap9.distmod(z), color='grey', legend_label='WMAP9') mu_scatter.legend.click_policy = 'hide' # Plot the simulated light-curves and their fits bands = 'ugrizy' sources = [ColumnDataSource(data=dict(time=[], flux=[], fitted_flux=[], lower_err=[], upper_err=[])) for _ in bands] colors = ('blue', 'orange', 'green', 'red', 'purple', 'black') lc_figs = [] for source, color, band in zip(sources, colors, bands): lc_plot = plotting.figure(plot_height=400, plot_width=400, title="", toolbar_location=None) lc_plot.renderers.append(Span(location=0, dimension='width', line_color='grey', line_dash='dotted', line_width=1)) lc_plot.add_layout( Whisker(source=source, base='time', upper='upper_err', lower='lower_err', line_alpha=.5) # Error bars on flux values )
#!/usr/bin/env python import matplotlib.pyplot as plt import numpy from astropy.cosmology import WMAP9 as cosmo xr = (0.007, 0.21) zline = numpy.arange(xr[0], xr[1], 0.001) m_line = -19.5 + cosmo.distmod(zline).value z_cosmo = numpy.arange(numpy.log(0.01), numpy.log(0.2), 0.1) z_cosmo = numpy.exp(z_cosmo) m = -19.5 + cosmo.distmod(z_cosmo).value num = len(z_cosmo) numpy.random.seed(1) pv = numpy.random.normal(0, 1500., num) / 3e5 plt.plot(zline, m_line) plt.scatter(z_cosmo, m, color='b', label=r'Cosmological $z$') z_obs = (1 + z_cosmo) * (1 + pv) - 1 plt.xscale("log", nonposx='clip') plt.xlabel(r"$z$", fontsize=22) plt.xlim(xr) plt.ylabel("$m$", fontsize=22) plt.legend(loc=2) plt.savefig('hubble.pdf') plt.clf()
Friedman_data9 = Table.read('friedman_table9.fit') print(Friedman_data9) #redshift value z_values = Friedman_data9["zCMB"] #Luminosity distance calculation lum_dis = cosmo.luminosity_distance(z_values) lum_dis = lum_dis.value print " Luminosity-distance after calculated:", lum_dis #Luminosity distance module calculation lum_dist_modulus = cosmo.distmod(z_values) print "Luminosity distance modulus values are :", lum_dist_modulus lum_dist_modulus = lum_dist_modulus.value print "Luminosity distance modulus values are :", lum_dist_modulus # Method 1 without eliminating Mpc plt.figure(1) plt.errorbar(z_values, lum_dis, 0 * Friedman_data9["e_zCMB"], Friedman_data9["e_zCMB"], fmt='o') #plt.scatter(z_values,lum_dis) plt.xlabel("Red Shift") plt.ylabel("Luminosity Distance - Mpc ")
def PCA_mag(dapall, filter_obs, plateifu=None, filename=None, vec_file=None, vec_data=None, pca_data_dir=None): """ Return absolute AB Magnitude in filter provided Parameters ---------- dapall: 'Table', 'dict' DAPALL file data filter_obs: 'str', 'speclite.filters.FilterSequence' observational filter to use plateifu: 'str', list, optional, must be keyword plate-ifu of galaxy desired filename: 'str', optional, must be keyword pca data file to read in, ignores plateifu if provided vec_file: 'str', optional, must be keyword pca data file containing eigenvectors vec_data: 'tuple', optional, must be keyword eigenvector data (mean_spec, evec_spec, lam_spec) """ # Check filter status # CHECK PLATEIFU if plateifu is None: plateifu = dapall["plateifu"] if filter_obs.__class__ is not filters.FilterSequence: if filter_obs in ["GALEX-NUV", "GALEX-FUV"]: wav, resp = np.loadtxt( "{}/data/GALEX_GALEX.NUV.dat".format(directory)).T galex_nuv = filters.FilterResponse(wavelength=wav * u.Angstrom, response=resp, meta=dict(group_name='GALEX', band_name='NUV')) wav, resp = np.loadtxt( "{}/data/GALEX_GALEX.FUV.dat".format(directory)).T galex_fuv = filters.FilterResponse(wavelength=wav * u.Angstrom, response=resp, meta=dict(group_name='GALEX', band_name='FUV')) try: filter_obs = filters.load_filters(filter_obs) except ValueError: logging.warnings("Invalid filter, using default of 'sdss2010-i'") filter_obs = filters.load_filters("sdss2010-i") if filename is None: if plateifu is None: raise ValueError("No input file or plateifu provided") else: filename = os.path.join(pca_data_dir, plateifu, "{}_res.fits".format(plateifu)) spectrum, wlen = PCA_Spectrum(plateifu=plateifu, filename=filename, vec_file=vec_file, vec_data=vec_data, pca_data_dir=pca_data_dir) mag = filter_obs.get_ab_magnitudes( spectrum, wlen, axis=0)[filter_obs.names[0]].data * u.ABmag mag_abs = mag - WMAP9.distmod(dapall["nsa_zdist"]) return mag_abs
def color_mag_plots(mgs,s82,decals,savefig=False): # Make paneled histograms of the color distribution for several magnitude bins of Galaxy Zoo data. """ SDSS main sample (GZ2) Stripe 82 coadded data (GZ2) DECaLS """ redshifts = (0.12,0.08,0.05) appmag_lim = 17.0 # Work out the magnitude limit from cosmology fig,axarr = plt.subplots(num=1,nrows=3,ncols=3,figsize=(12,10)) for z,ax in zip(redshifts,axarr.ravel()): absmag_lim = appmag_lim - WMAP9.distmod(z).value maglim = (mgs['PETROMAG_MR'] < absmag_lim) & (mgs['REDSHIFT'] <= z) spiral = mgs['t01_smooth_or_features_a02_features_or_disk_weighted_fraction'] >= 0.8 elliptical = mgs['t01_smooth_or_features_a01_smooth_weighted_fraction'] >= 0.8 ax.hist(mgs[maglim & spiral]['PETROMAG_U'] - mgs[maglim & spiral]['PETROMAG_R'],range=(0,4),bins=25,color='blue',histtype='step',label='spiral') ax.hist(mgs[maglim & elliptical]['PETROMAG_U'] - mgs[maglim & elliptical]['PETROMAG_R'],range=(0,4),bins=25,color='red',histtype='step',label='elliptical') ax.set_xlabel(r'$(u-r)$',fontsize=16) ax.set_title(r'$M_r<{0:.2f}, z<{1:.2f}$'.format(absmag_lim,z),fontsize=16) ax.text(0.95,0.95,'MGS',ha='right',va='top',transform=ax.transAxes) if ax == axarr.ravel()[0]: ax.legend(loc='upper left',fontsize=10) s82_lim = 17.77 for z,ax in zip(redshifts,axarr.ravel()[3:6]): absmag_lim = s82_lim - WMAP9.distmod(z).value maglim = (s82['PETROMAG_MR'] < absmag_lim) & (s82['REDSHIFT'] <= z) spiral = s82['t01_smooth_or_features_a02_features_or_disk_weighted_fraction'] >= 0.8 elliptical = s82['t01_smooth_or_features_a01_smooth_weighted_fraction'] >= 0.8 ax.hist(s82[maglim & spiral]['PETROMAG_U'] - s82[maglim & spiral]['PETROMAG_R'],range=(0,4),bins=25,color='blue',histtype='step',label='spiral') ax.hist(s82[maglim & elliptical]['PETROMAG_U'] - s82[maglim & elliptical]['PETROMAG_R'],range=(0,4),bins=25,color='red',histtype='step',label='elliptical') ax.set_xlabel(r'$(u-r)$',fontsize=16) ax.set_title(r'$M_r<{0:.2f}, z<{1:.2f}$'.format(absmag_lim,z),fontsize=16) ax.text(0.95,0.95,'Stripe 82',ha='right',va='top',transform=ax.transAxes) decals_lim = 17.77 for z,ax in zip(redshifts,axarr.ravel()[6:]): absmag_lim = decals_lim - WMAP9.distmod(z).value maglim = (decals['metadata.mag.abs_r'] < absmag_lim) & (decals['metadata.redshift'] <= z) spiral = decals['t00_smooth_or_features_a1_features_frac'] >= 0.8 elliptical = decals['t00_smooth_or_features_a0_smooth_frac'] >= 0.8 ax.hist(decals[maglim & spiral]['metadata.mag.u'] - decals[maglim & spiral]['metadata.mag.r'],range=(0,4),bins=25,color='blue',histtype='step',label='spiral') ax.hist(decals[maglim & elliptical]['metadata.mag.u'] - decals[maglim & elliptical]['metadata.mag.r'],range=(0,4),bins=25,color='red',histtype='step',label='elliptical') ax.set_xlabel(r'$(u-r)$',fontsize=16) ax.set_title(r'$M_r<{0:.2f}, z<{1:.2f}$'.format(absmag_lim,z),fontsize=16) ax.text(0.95,0.95,'DECaLS',ha='right',va='top',transform=ax.transAxes) fig.tight_layout() if savefig: plt.savefig('{0}/color_hist.pdf'.format(plot_path)) else: plt.show() return None
def correct_for_distance(self, flux, fluxerr): dlmu = cosmo.distmod(self.redshift).value flux, fluxerr = helpers.calc_luminosity(flux, fluxerr, dlmu) return flux, fluxerr
# Plot the CMD as a function of GZ1 morphology for the RGZ 75% counterparts data = ascii.read( '/Users/willettk/Astronomy/Research/GalaxyZoo/rgz-analysis/rgz_wise_75_sdss_magerr.csv' ) fname_spec = 'rgz_75_sdss_mags_spec.vot' fname_nospec = 'rgz_75_sdss_mags_nospec.vot' zoospec = votable_read('%s/%s' % (rgzdir, fname_spec)) zoonospec = votable_read('%s/%s' % (rgzdir, fname_nospec)) na10 = na10_read() gr_na10 = na10['g-r'] distmod_na10 = WMAP9.distmod(na10['z']) Mr_na10 = na10['r'] - distmod_na10.value Mr_na10 = na10['M_g'] - gr_na10 # Generate contours of the NA10 sample magbins = np.linspace(-24, -20, 20) colorbins = np.linspace(0.3, 1.0, 20) hc, xc, yc = np.histogram2d(Mr_na10, gr_na10, bins=(magbins, colorbins)) levels = np.linspace(0, 200, 10) # Make 2x2 figure fig, axarr = plt.subplots(2, 2, figsize=(10, 10)) from scipy.ndimage.filters import gaussian_filter
def get_catalog(params, map_struct): if not os.path.isdir(params["catalogDir"]): os.makedirs(params["catalogDir"]) catalogFile = os.path.join(params["catalogDir"], "%s.hdf5" % params["galaxy_catalog"]) """AB Magnitude zero point.""" MAB0 = -2.5 * np.log10(3631.e-23) pc_cm = 3.08568025e18 const = 4. * np.pi * (10. * pc_cm)**2. if params["galaxy_catalog"] == "2MRS": if not os.path.isfile(catalogFile): import astropy.constants as c cat, = Vizier.get_catalogs('J/ApJS/199/26/table3') ra, dec = cat["RAJ2000"], cat["DEJ2000"] cz = cat["cz"] magk = cat["Ktmag"] z = (u.Quantity(cat['cz']) / c.c).to(u.dimensionless_unscaled) completeness = 0.5 alpha = -1.0 MK_star = -23.55 MK_max = MK_star + 2.5 * np.log10(gammaincinv(alpha + 2, completeness)) MK = magk - cosmo.distmod(z) idx = (z > 0) & (MK < MK_max) ra, dec = ra[idx], dec[idx] z = z[idx] magk = magk[idx] distmpc = cosmo.luminosity_distance(z).to('Mpc').value with h5py.File(catalogFile, 'w') as f: f.create_dataset('ra', data=ra) f.create_dataset('dec', data=dec) f.create_dataset('z', data=z) f.create_dataset('magk', data=magk) f.create_dataset('distmpc', data=distmpc) else: with h5py.File(catalogFile, 'r') as f: ra, dec = f['ra'][:], f['dec'][:] z = f['z'][:] magk = f['magk'][:] distmpc = f['distmpc'][:] r = distmpc * 1.0 mag = magk * 1.0 elif params["galaxy_catalog"] == "GLADE": if not os.path.isfile(catalogFile): cat, = Vizier.get_catalogs('VII/281/glade2') ra, dec = cat["RAJ2000"], cat["DEJ2000"] distmpc, z = cat["Dist"], cat["z"] magb, magk = cat["Bmag"], cat["Kmag"] # Keep track of galaxy identifier GWGC, PGC, HyperLEDA = cat["GWGC"], cat["PGC"], cat["HyperLEDA"] _2MASS, SDSS = cat["_2MASS"], cat["SDSS-DR12"] idx = np.where(distmpc >= 0)[0] ra, dec = ra[idx], dec[idx] distmpc, z = distmpc[idx], z[idx] magb, magk = magb[idx], magk[idx] GWGC, PGC, HyperLEDA = GWGC[idx], PGC[idx], HyperLEDA[idx] _2MASS, SDSS = _2MASS[idx], SDSS[idx] with h5py.File(catalogFile, 'w') as f: f.create_dataset('ra', data=ra) f.create_dataset('dec', data=dec) f.create_dataset('distmpc', data=distmpc) f.create_dataset('magb', data=magb) f.create_dataset('magk', data=magk) f.create_dataset('z', data=z) # Add galaxy identifier f.create_dataset('GWGC', data=GWGC) f.create_dataset('PGC', data=PGC) f.create_dataset('HyperLEDA', data=HyperLEDA) f.create_dataset('2MASS', data=_2MASS) f.create_dataset('SDSS', data=SDSS) else: with h5py.File(catalogFile, 'r') as f: ra, dec = f['ra'][:], f['dec'][:] distmpc, z = f['distmpc'][:], f['z'][:] magb, magk = f['magb'][:], f['magk'][:] GWGC, PGC, _2MASS = f['GWGC'][:], f['PGC'][:], f['2MASS'][:] HyperLEDA, SDSS = f['HyperLEDA'][:], f['SDSS'][:] # Convert bytestring to unicode GWGC = GWGC.astype('U') PGC = PGC.astype('U') HyperLEDA = HyperLEDA.astype('U') _2MASS = _2MASS.astype('U') SDSS = SDSS.astype('U') # Keep only galaxies with finite B mag when using it in the grade if params["galaxy_grade"] == "S": idx = np.where(~np.isnan(magb))[0] ra, dec, distmpc = ra[idx], dec[idx], distmpc[idx] magb, magk = magb[idx], magk[idx] GWGC, PGC, HyperLEDA = GWGC[idx], PGC[idx], HyperLEDA[idx] _2MASS, SDSS = _2MASS[idx], SDSS[idx] r = distmpc * 1.0 mag = magb * 1.0 elif params["galaxy_catalog"] == "CLU": if not os.path.isfile(catalogFile): raise ValueError("Please add %s." % catalogFile) cat = Table.read(catalogFile) name = cat['name'] ra, dec = cat['ra'], cat['dec'] sfr_fuv, mstar = cat['sfr_fuv'], cat['mstar'] distmpc, magb = cat['distmpc'], cat['magb'] a, b2a, pa = cat['a'], cat['b2a'], cat['pa'] btc = cat['btc'] idx = np.where(distmpc >= 0)[0] ra, dec = ra[idx], dec[idx] sfr_fuv, mstar = sfr_fuv[idx], mstar[idx] distmpc, magb = distmpc[idx], magb[idx] a, b2a, pa = a[idx], b2a[idx], pa[idx] btc = btc[idx] idx = np.where(~np.isnan(magb))[0] ra, dec = ra[idx], dec[idx] sfr_fuv, mstar = sfr_fuv[idx], mstar[idx] distmpc, magb = distmpc[idx], magb[idx] a, b2a, pa = a[idx], b2a[idx], pa[idx] btc = btc[idx] z = -1*np.ones(distmpc.shape) r = distmpc * 1.0 mag = magb * 1.0 elif params["galaxy_catalog"] == "mangrove": catalogFile = os.path.join(params["catalogDir"], "%s.hdf5" % params["galaxy_catalog"]) if not os.path.isfile(catalogFile): print("mangrove catalog not found localy, start the automatic download") url = 'https://mangrove.lal.in2p3.fr/data/mangrove.hdf5' os.system("wget -O {}/mangrove.hdf5 {}".format(params["catalogDir"], url)) cat = Table.read(catalogFile) ra, dec = cat["RA"], cat["dec"] distmpc, z = cat["dist"], cat["z"] magb, magk = cat["B_mag"], cat["K_mag"] magW1, magW2, magW3, magW4 = cat["w1mpro"], cat["w2mpro"], cat["w3mpro"], cat["w4mpro"] stellarmass = cat['stellarmass'] # Keep track of galaxy identifier GWGC, PGC, HyperLEDA = cat["GWGC_name"], cat["PGC"], cat["HyperLEDA_name"] _2MASS, SDSS = cat["2MASS_name"], cat["SDSS-DR12_name"] #keep track of the AGN flag AGN_flag = cat["AGN_flag"] idx = np.where(distmpc >= 0)[0] ra, dec = ra[idx], dec[idx] distmpc, z = distmpc[idx], z[idx] magb, magk = magb[idx], magk[idx] magW1, magW2, magW3, magW4 = magW1[idx], magW2[idx], magW3[idx], magW4[idx] stellarmass = stellarmass[idx] GWGC, PGC, HyperLEDA = GWGC[idx], PGC[idx], HyperLEDA[idx] _2MASS, SDSS = _2MASS[idx], SDSS[idx] AGN_flag = AGN_flag[idx] # Convert bytestring to unicode GWGC = GWGC.astype('U') PGC = PGC.astype('U') HyperLEDA = HyperLEDA.astype('U') _2MASS = _2MASS.astype('U') SDSS = SDSS.astype('U') r = distmpc * 1.0 mag = magb * 1.0 n, cl = params["powerlaw_n"], params["powerlaw_cl"] dist_exp = params["powerlaw_dist_exp"] prob_scaled = copy.deepcopy(map_struct["prob"]) prob_sorted = np.sort(prob_scaled)[::-1] prob_indexes = np.argsort(prob_scaled)[::-1] prob_cumsum = np.cumsum(prob_sorted) index = np.argmin(np.abs(prob_cumsum - cl)) + 1 prob_scaled[prob_indexes[index:]] = 0.0 prob_scaled = prob_scaled**n theta = 0.5 * np.pi - dec * 2 * np.pi / 360.0 phi = ra * 2 * np.pi / 360.0 ipix = hp.ang2pix(map_struct["nside"], ra, dec, lonlat=True) if "distnorm" in map_struct: if map_struct["distnorm"] is not None: #creat an mask to cut at 3 sigma in distance mask = np.zeros(len(r)) #calculate the moments from distmu, distsigma and distnorm mom_mean, mom_std, mom_norm = distance.parameters_to_moments(map_struct["distmu"],map_struct["distsigma"]) condition_indexer = np.where( (r < (mom_mean[ipix] + (3*mom_std[ipix]))) & (r > (mom_mean[ipix] - (3*mom_std[ipix])) )) mask[condition_indexer] = 1 Sloc = prob_scaled[ipix] * (map_struct["distnorm"][ipix] * norm(map_struct["distmu"][ipix], map_struct["distsigma"][ipix]).pdf(r))**params["powerlaw_dist_exp"] / map_struct["pixarea"] #multiplie the Sloc by 1 or 0 according to the 3 sigma condistion Sloc = Sloc*mask idx = np.where(condition_indexer)[0] else: Sloc = copy.copy(prob_scaled[ipix]) idx = np.arange(len(r)).astype(int) else: Sloc = copy.copy(prob_scaled[ipix]) idx = np.arange(len(r)).astype(int) # this happens when we are using a tiny catalog... CompatibleGal = True if np.all(Sloc == 0.0): Sloc[:] = 1.0 CompatibleGal = False #new version of the Slum calcul (from HOGWARTs) Lsun = 3.828e26 Msun = 4.83 Mknmin = -19 Mknmax = -12 Lblist = [] for i in range(0, len(r)): Mb = mag[i] - 5 * np.log10((r[i] * 10 ** 6)) + 5 #L = Lsun * 2.512 ** (Msun - Mb) Lb = Lsun * 2.512 ** (Msun - Mb) Lblist.append(Lb) #set 0 when Sloc is 0 (keep compatible galaxies for normalization) Lblist = np.array(Lblist) Lblist[Sloc == 0] = 0 Slum = Lblist / np.nansum(np.array(Lblist)) """ L_nu = const * 10.**((mag + MAB0)/(-2.5)) L_nu = L_nu / np.nanmax(L_nu[idx]) L_nu = L_nu**params["catalog_n"] L_nu[L_nu < 0.001] = 0.001 L_nu[L_nu > 1.0] = 1.0 Slum = L_nu / np.sum(L_nu) """ mlim, M_KNmin, M_KNmax = 22, -17, -12 L_KNmin = const * 10.**((M_KNmin + MAB0)/(-2.5)) L_KNmax = const * 10.**((M_KNmax + MAB0)/(-2.5)) Llim = 4. * np.pi * (r * 1e6 * pc_cm)**2. * 10.**((mlim + MAB0)/(-2.5)) Sdet = (L_KNmax-Llim)/(L_KNmax-L_KNmin) Sdet[Sdet < 0.01] = 0.01 Sdet[Sdet > 1.0] = 1.0 # Set nan values to zero Sloc[np.isnan(Sloc)] = 0 Slum[np.isnan(Slum)] = 0 if params["galaxy_grade"] == "Smass": if params["galaxy_catalog"] != "mangrove": raise ValueError("You are trying to use the stellar mass information (Smass), please select the mangrove catalog for such use.") #set Smass Smass = np.array(stellarmass) #put null values to nan Smass[np.where(Smass == 0)] = np.nan #go back to linear scaling and not log Smass = 10**Smass # Keep only galaxies with finite stellarmass when using it in the grade #set nan values to 0 Smass[~np.isfinite(Smass)] = 0 #set Smass Smass = Smass / np.sum(Smass) #alpha is defined only with non null mass galaxies, we set a mask for that ind_without_mass = np.where(Smass == 0) Sloc_temp = copy.deepcopy(Sloc) Sloc_temp[ind_without_mass] = 0 #alpha_mass parameter is defined in such way that in mean Sloc count in as much as Sloc*alpha*Smass alpha_mass = (np.sum(Sloc_temp) / np.sum( Sloc_temp*Smass ) ) print("You chose to use the grade using stellar mass, the parameters values are:") print("alpha_mass =", alpha_mass) #beta_mass is a parameter allowing to change the importance of Sloc according to Sloc*alpha*Smass #beta_mass should be fitted in the futur on real GW event for which we have the host galaxy #fixed to one at the moment beta_mass = 1 print("beta_mass =", beta_mass) Smass = Sloc*(1+ (alpha_mass*beta_mass*Smass) ) #Smass = Sloc*Smass S = Sloc*Slum*Sdet prob = np.zeros(map_struct["prob"].shape) if params["galaxy_grade"] == "Sloc": for j in range(len(ipix)): prob[ipix[j]] += Sloc[j] grade = Sloc elif params["galaxy_grade"] == "S": for j in range(len(ipix)): prob[ipix[j]] += S[j] grade = S elif params["galaxy_grade"] == "Smass": for j in range(len(ipix)): prob[ipix[j]] += Smass[j] grade = Smass prob = prob / np.sum(prob) map_struct['prob_catalog'] = prob if params["doUseCatalog"]: map_struct['prob'] = prob Lblist = np.array(Lblist) idx = np.where(~np.isnan(grade))[0] grade = grade[idx] ra, dec, Sloc, S = ra[idx], dec[idx], Sloc[idx], S[idx] distmpc, z = distmpc[idx], z[idx] if params["galaxy_catalog"] == "GLADE": GWGC, PGC, HyperLEDA = GWGC[idx], PGC[idx], HyperLEDA[idx] _2MASS, SDSS = _2MASS[idx], SDSS[idx] Lblist = Lblist[idx] magb = magb[idx] if params["galaxy_catalog"] == "mangrove": GWGC, PGC, HyperLEDA = GWGC[idx], PGC[idx], HyperLEDA[idx] _2MASS, SDSS = _2MASS[idx], SDSS[idx] stellarmass, magb = stellarmass[idx], magb[idx] AGN_flag = AGN_flag[idx] if params["galaxy_grade"] == "Smass": Smass = Smass[idx] """ Sthresh = np.max(grade)*0.01 idx = np.where(grade >= Sthresh)[0] grade = grade[idx] ra, dec, Sloc, S = ra[idx], dec[idx], Sloc[idx], S[idx] distmpc, z = distmpc[idx], z[idx] if params["galaxy_catalog"] == "GLADE": GWGC, PGC, HyperLEDA = GWGC[idx], PGC[idx], HyperLEDA[idx] _2MASS, SDSS = _2MASS[idx], SDSS[idx] """ idx = np.argsort(grade)[::-1] grade = grade[idx] ra, dec, Sloc, S = ra[idx], dec[idx], Sloc[idx], S[idx] distmpc, z = distmpc[idx], z[idx] if params["galaxy_catalog"] == "GLADE": GWGC, PGC, HyperLEDA = GWGC[idx], PGC[idx], HyperLEDA[idx] _2MASS, SDSS = _2MASS[idx], SDSS[idx] Lblist = Lblist[idx] magb = magb[idx] if params["galaxy_catalog"] == "mangrove": GWGC, PGC, HyperLEDA = GWGC[idx], PGC[idx], HyperLEDA[idx] _2MASS, SDSS = _2MASS[idx], SDSS[idx] stellarmass, magb = stellarmass[idx], magb[idx] AGN_flag = AGN_flag[idx] if params["galaxy_grade"] == "Smass": Smass = Smass[idx] # Keep only galaxies within 3sigma in distance if params["galaxy_catalog"] != "mangrove": mask = Sloc > 0 ra, dec, Sloc, S = ra[mask], dec[mask], Sloc[mask], S[mask], distmpc, z = distmpc[mask], z[mask] if params["galaxy_catalog"] == "GLADE": GWGC, PGC, HyperLEDA = GWGC[mask], PGC[mask], HyperLEDA[mask] _2MASS, SDSS = _2MASS[mask], SDSS[mask] Lblist = Lblist[mask] if params["galaxy_catalog"] == "mangrove": # Keep only galaxies within 3sigma in distance mask = Sloc > 0 ra, dec, Sloc, S = ra[mask], dec[mask], Sloc[mask], S[mask] distmpc, z = distmpc[mask], z[mask] GWGC, PGC, HyperLEDA = GWGC[mask], PGC[mask], HyperLEDA[mask] _2MASS, SDSS = _2MASS[mask], SDSS[mask] stellarmass, magb = stellarmass[mask], magb[mask] AGN_flag = AGN_flag[mask] if params["galaxy_grade"] == "Smass": Smass = Smass[mask] if len(ra) > 2000: print('Cutting catalog to top 2000 galaxies...') idx = np.arange(2000).astype(int) ra, dec, Sloc, S = ra[idx], dec[idx], Sloc[idx], S[idx] distmpc, z = distmpc[idx], z[idx] if params["galaxy_catalog"] == "GLADE": GWGC, PGC, HyperLEDA = GWGC[idx], PGC[idx], HyperLEDA[idx] _2MASS, SDSS = _2MASS[idx], SDSS[idx] Lblist = Lblist[idx] magb = magb[idx] elif params["galaxy_catalog"] == "mangrove": GWGC, PGC, HyperLEDA = GWGC[idx], PGC[idx], HyperLEDA[idx] _2MASS, SDSS = _2MASS[idx], SDSS[idx] stellarmass, magb = stellarmass[idx], magb[idx] AGN_flag = AGN_flag[idx] if params["galaxy_grade"] == "Smass": Smass = Smass[idx] # now normalize the distributions S = S / np.sum(S) Sloc = Sloc / np.sum(Sloc) if params["galaxy_grade"] == "Smass": Smass = Smass/np.sum(Smass) else: Smass = np.ones(Sloc.shape) Smass = Smass/np.sum(Smass) catalog_struct = {} catalog_struct["ra"] = ra catalog_struct["dec"] = dec catalog_struct["Sloc"] = Sloc catalog_struct["S"] = S catalog_struct["Smass"] = Smass catalog_struct["CompatibleGal"] = CompatibleGal if params["writeCatalog"]: catalogfile = os.path.join(params["outputDir"], 'catalog.csv') fid = open(catalogfile, 'w') cnt = 1 if params["galaxy_catalog"] == "GLADE": if params["galaxy_grade"] == "S": fid.write("id, RAJ2000, DEJ2000, Sloc, S, Dist, z,GWGC, PGC, HyperLEDA, 2MASS, SDSS, BLum\n") for a, b, c, d, e, f, g, h, i, j, k, l in zip(ra, dec, Sloc, S, distmpc, z, GWGC, PGC, HyperLEDA, _2MASS, SDSS, Lblist): fid.write("%d, %.5f, %.5f, %.5e, %.5e, %.4f, %.4f, %s, %s, %s, %s, %s, %.2E\n" % (cnt, a, b, c, d, e, f, g, h, i, j, k, l)) cnt = cnt + 1 else: fid.write("id, RAJ2000, DEJ2000, Sloc, S, Dist, z, GWGC, PGC, HyperLEDA, 2MASS, SDSS\n") for a, b, c, d, e, f, g, h, i, j, k in zip(ra, dec, Sloc, S, distmpc, z, GWGC, PGC, HyperLEDA, _2MASS, SDSS): fid.write("%d, %.5f, %.5f, %.5e, %.5e, %.4f, %.4f, %s, %s, %s, %s, %s\n" % (cnt, a, b, c, d, e, f, g, h, i, j, k)) cnt = cnt + 1 elif params["galaxy_catalog"] == "mangrove": if params["galaxy_grade"] == "Smass": if params["AGN_flag"]: fid.write("id, RAJ2000, DEJ2000, Smass, S, Sloc, Dist, z, GWGC, PGC, HyperLEDA, 2MASS, SDSS, B_mag, AGN_flag, stellarmass\n") for a, b, c, d, e, f, g, h, i, j, k, l, m, n, o in zip(ra, dec, Smass, S, Sloc, distmpc, z, GWGC, PGC, HyperLEDA, _2MASS, SDSS, magb, AGN_flag,stellarmass): fid.write("%d, %.5f, %.5f, %.5e, %.5e, %.5e, %.4f, %.4f, %s, %s, %s, %s, %s, %s, %s, %s\n" % (cnt, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o)) cnt = cnt + 1 else: fid.write("id, RAJ2000, DEJ2000, Smass, S, Sloc, Dist, z, GWGC, PGC, HyperLEDA, 2MASS, SDSS, B_mag, stellarmass\n") for a, b, c, d, e, f, g, h, i, j, k, l, m, n in zip(ra, dec, Smass, S, Sloc, distmpc, z, GWGC, PGC, HyperLEDA, _2MASS, SDSS, magb, stellarmass): fid.write("%d, %.5f, %.5f, %.5e, %.5e, %.5e, %.4f, %.4f, %s, %s, %s, %s, %s, %s, %s\n" % (cnt, a, b, c, d, e, f, g, h, i, j, k, l, m, n)) cnt = cnt + 1 else: if params["AGN_flag"]: fid.write("id, RAJ2000, DEJ2000, Sloc, S, Dist, z, GWGC, PGC, HyperLEDA, 2MASS, SDSS, AGN_flag\n") for a, b, c, d, e, f, g, h, i, j, k, l in zip(ra, dec, Sloc, S, distmpc, z, GWGC, PGC, HyperLEDA, _2MASS, SDSS, AGN_flag): fid.write("%d, %.5f, %.5f, %.5e, %.5e, %.4f, %.4f, %s, %s, %s, %s, %s, %s\n" % (cnt, a, b, c, d, e, f, g, h, i, j, k, l)) cnt = cnt + 1 else: fid.write("id, RAJ2000, DEJ2000, Sloc, S, Dist, z, GWGC, PGC, HyperLEDA, 2MASS, SDSS\n") for a, b, c, d, e, f, g, h, i, j, k in zip(ra, dec, Sloc, S, distmpc, z, GWGC, PGC, HyperLEDA, _2MASS, SDSS): fid.write("%d, %.5f, %.5f, %.5e, %.5e, %.4f, %.4f, %s, %s, %s, %s, %s\n" % (cnt, a, b, c, d, e, f, g, h, i, j, k)) cnt = cnt + 1 else: fid.write("id, RAJ2000, DEJ2000, Sloc, S, Dist, z\n") for a, b, c, d in zip(ra, dec, Sloc, S, distmpc, z): fid.write("%d, %.5f, %.5f, %.5e, %.5e, %.4f, %.4f\n" % (cnt, a, b, c, d, e, f)) cnt = cnt + 1 fid.close() return map_struct, catalog_struct
def plot_rgz_sdss(rgzdata, nadata): # Plot comparison of the (g-r) CMD for both the RGZ 75% consensus sources (matched with WISE) and SDSS galaxies from NA10 fig = plt.figure(1) fig.clf() ax = fig.add_subplot(111) gr_color = rgzdata['absmagG'] - rgzdata['absmagR'] absmagR = rgzdata['absmagR'] na_gr_color = nadata['g-r'] dm = WMAP9.distmod(nadata['z']) na_absmagR = (nadata['r'] * u.mag - dm).value h, xedges, yedges, im = plt.hist2d(absmagR, gr_color, bins=30, range=[[-24, -18], [0, 1.5]], cmap=plt.cm.hot) #h,xedges,yedges = np.histogram2d(absmagR,gr_color,bins=20,range=[[-24,-18],[0,1.5]]) xa, ya = np.array(xedges), np.array(yedges) xcen = xa[:-1] + (xa[1] - xa[0]) / 2. ycen = ya[:-1] + (ya[1] - ya[0]) / 2. #cs = plt.contour(xcen,ycen,h.T,10,vmin=0.,colors='k',linewidths=1) #ax.scatter(absmagR,gr_color,s=1,c='r') hn, xedgesn, yedgesn = np.histogram2d(na_absmagR, na_gr_color, bins=30, range=[[-24, -18], [0, 1.5]]) csn = plt.contour(xcen, ycen, hn.T, 10, vmin=0., colors='g', linewidths=2) #ax.scatter(na_absmagR,na_gr_color,s=1,c='b') #Create custom artists for legend c = csn.collections[0] art1 = plt.Line2D((0, 1), (0, 0), color='g') legend = ax.legend([art1], ['Nair & Abraham (2010)'], loc='upper left', shadow=True) # Set final properties ax.set_xlim(-18, -24) ax.set_ylim(0.2, 1.0) ax.set_xlabel(r'$M_r$', fontsize=20) ax.set_ylabel(r'$(g-r)$', fontsize=20) plt.show() # Try linear fits to the data, see if slope of the red sequence is same ind1 = (gr_color > 0.6) & (gr_color < 1.0) ind2 = (na_gr_color > 0.6) & (na_gr_color < 1.0) x1, y1 = absmagR[ind1], gr_color[ind1] x2, y2 = na_absmagR[ind2], na_gr_color[ind2] slope1, intercept1, r_value1, p_value1, slope_std_error1 = stats.linregress( x1, y1) slope2, intercept2, r_value2, p_value2, slope_std_error2 = stats.linregress( x2, y2) print 'RGZ: slope = %.2f +- %.5f' % (slope1, slope_std_error1) print 'NA10: slope = %.2f +- %.5f' % (slope2, slope_std_error2) return None