def mass_esitmate_redmapper(redmapper_cat):
    richnesses = redmapper_cat['lambda']
    zs = redmapper_cat['z']
    m200m_list = []
    m200c_list = []
    for z, richness in zip(zs, richnesses):
        m200m, r200 = background.lambda_to_m200_r200(
            richness, z, richness_mass_author="Simet_mean")
        m200c, r200 = background.lambda_to_m200_r200(
            richness, z, richness_mass_author="Simet_crit")
        m200m_list.append(m200m)
        m200c_list.append(m200c)
    redmapper_cat['m200c'] = np.array(m200c_list)
    redmapper_cat['m200m'] = np.array(m200m_list)
def mass_estimate_spider(spider_cat):
    r200c_deg = spider_cat['r200c_deg']
    rad = r200c_deg * 60
    r200 = background.arcmin_to_r200(rad, spider_cat['z'])  # kpc physical
    mass_old = background.r200c_to_m200c(r200, spider_cat['z'])  #m200c (h?)
    mass_new = mass_so.R_to_M(r200 * 0.7, spider_cat['z'], '200c')  # collosus
    mass = mass_so.R_to_M(r200 * 0.7, spider_cat['z'], '200c')  # collosus
    spider_cat['m200c'] = mass

    # plt.figure()
    # plt.loglog(mass_new, mass_old, '.')

    # plt.figure()
    # plt.semilogx(mass_new, mass_new/mass_old, '.')

    # plt.show()

    m200m_list = []
    m200m_richness_list = []
    m200c_richness_list = []
    for m200c, redshift, richness in zip(mass, spider_cat['z'],
                                         spider_cat['lambda']):
        m200m, r200m, c200m = mass_adv.changeMassDefinitionCModel(
            m200c, redshift, "200c", "200m", c_model='child18')
        m200m_list.append(m200m)
        m200m, r200m = background.lambda_to_m200_r200(
            richness, redshift, richness_mass_author="Simet_mean")
        m200c, r200c = background.lambda_to_m200_r200(
            richness, redshift, richness_mass_author="Simet_crit")
        m200m_richness_list.append(m200m)
        m200c_richness_list.append(m200c)
    mass = np.array(m200m_list)
    r200m_r200c_ratio = r200m / r200
    rad *= r200m_r200c_ratio
    r200 *= r200m_r200c_ratio
    spider_cat['m200m'] = np.array(m200m_list)
    spider_cat['rad'] = rad
    spider_cat['m200m_lambda'] = np.array(m200m_richness_list)
    spider_cat['m200c_lambda'] = np.array(m200c_richness_list)
Exemple #3
0
def plot_richness_mass_relation():
    background.set_cosmology("wmap7")
    richness = np.logspace(np.log10(20), np.log10(300), 100)
    m200m_rykoff, r200m_rykoff = background.lambda_to_m200_r200(
        richness, 0.25, richness_mass_author="Rykoff_mean")
    m200m_simet, r200m_simet = background.lambda_to_m200_r200(
        richness, 0.25, richness_mass_author="Simet_mean")
    m200m_baxter, r200m_baxter = background.lambda_to_m200_r200(
        richness, 0.25, richness_mass_author="Baxter_mean")
    m200c_rykoff, r200c_rykoff = background.lambda_to_m200_r200(
        richness, 0.25, richness_mass_author="Rykoff_crit")
    m200c_simet, r200c_simet = background.lambda_to_m200_r200(
        richness, 0.25, richness_mass_author="Simet_crit")
    m200c_baxter, r200c_baxter = background.lambda_to_m200_r200(
        richness, 0.25, richness_mass_author="Baxter_crit")
    plt.figure()
    plt.plot(richness, m200m_rykoff, '-r')
    plt.plot(richness, m200m_simet, '-b')
    plt.plot(richness, m200m_baxter, '-g')
    plt.plot(richness, m200c_rykoff, '--r')
    plt.plot(richness, m200c_simet, '--b')
    plt.plot(richness, m200c_baxter, '--g')
    plt.plot([], [], 'r', label='rykoff')
    plt.plot([], [], 'b', label='simet')
    plt.plot([], [], 'g', label='baxter')
    plt.plot([], [], '-k', label='M200m')
    plt.plot([], [], '--k', label='M200c')
    plt.legend(loc='best', framealpha=0.3)
    plt.yscale('log')
    plt.xscale('log')
    plt.xlabel('Richness')
    plt.ylabel(r'M$_{200}$')
    plt.grid()

    plt.figure()
    plt.plot(richness, r200m_rykoff, '-r')
    plt.plot(richness, r200m_simet, '-b')
    plt.plot(richness, r200m_baxter, '-g')
    plt.plot(richness, r200c_rykoff, '--r')
    plt.plot(richness, r200c_simet, '--b')
    plt.plot(richness, r200c_baxter, '--g')
    plt.plot([], [], 'r', label='rykoff')
    plt.plot([], [], 'b', label='simet')
    plt.plot([], [], 'g', label='baxter')
    plt.plot([], [], '-k', label='R200m')
    plt.plot([], [], '--k', label='R200c')
    plt.legend(loc='best', framealpha=0.3)
    plt.yscale('log')
    plt.xscale('log')
    plt.xlabel('Richness')
    plt.ylabel(r'R$_{200}$')
    plt.grid()

    plt.figure()
    plt.plot(richness, m200c_rykoff / m200m_rykoff, '-r', label='rykoff')
    plt.plot(richness, m200c_simet / m200m_simet, '-b', label='simet')
    plt.plot(richness, m200c_baxter / m200m_baxter, '-g', label='baxter')
    plt.legend(loc='best', framealpha=0.3)
    plt.xscale('log')
    plt.xlabel('Richness')
    plt.ylabel(r'M200c/M200m')
    plt.grid()

    plt.figure()
    plt.plot(richness, r200c_rykoff / r200m_rykoff, '-r', label='rykoff')
    plt.plot(richness, r200c_simet / r200m_simet, '-b', label='simet')
    plt.plot(richness, r200c_baxter / r200m_baxter, '-g', label='baxter')
    plt.legend(loc='best', framealpha=0.3)
    plt.xscale('log')
    plt.xlabel('Richness')
    plt.ylabel(r'R200c/R200m')
    plt.grid()

    plt.figure()
    a = 1.0 / (1.0 + 0.25)
    # plt.plot(m200m_simet, r200m_simet, '-b',  label='my simet mean')
    # plt.plot(m200c_simet, r200c_simet, '--b', label='my simet crit')
    plt.plot(m200m_simet, r200m_simet, '-b', label='my simet mean')
    plt.plot(m200c_simet, r200c_simet, '--b', label='my simet crit')

    nfw_200m = NFWProfile(mdef='200m', redshift=0.25)
    nfw_200c = NFWProfile(mdef='200c', redshift=0.25)
    plt.plot(m200m_simet,
             nfw_200m.halo_mass_to_halo_radius(m200m_simet) * 1000,
             '-r',
             label='ht simet mean')
    plt.plot(m200c_simet,
             nfw_200c.halo_mass_to_halo_radius(m200c_simet) * 1000,
             '--r',
             label='ht simet crit')

    plt.plot(m200m_simet,
             M_to_R(m200m_simet, 0.25, '200m'),
             '-g',
             label='cls simet mean')
    plt.plot(m200c_simet,
             M_to_R(m200c_simet, 0.25, '200c'),
             '--g',
             label='cls simet crit')

    plt.legend(loc='best', framealpha=0.3)
    plt.xscale('log')
    plt.yscale('log')
    plt.xlabel('M200 [Msun]')
    plt.ylabel('R200 [proper kpc]')
    plt.grid()

    plt.show()

    #header list can be found in http://arxiv.org/pdf/1303.3562v2.pdf
    hdulist = pyfits.open("redmapper_dr8_public_v6.3_catalog.fits")
    tdata = hdulist[1].data

    # red_ra = tdata.field('ra')
    # red_dec= tdata.field('dec')
    red_z = tdata.field('z_lambda')
    red_lambda = tdata.field('lambda')
    # red_pcen=tdata.field('p_cen')

    plt.figure()
    h, xbins = np.histogram(red_lambda, bins=256)
    plt.plot(dtk.bins_avg(xbins), h)
    plt.yscale('log')
    plt.xscale('log')
    plt.xlabel('Richness')
    plt.ylabel('count')

    m200m_rykoff = background.lambda_to_m200m_Rykoff(red_lambda, 0.25)
    m200m_simet = background.lambda_to_m200m_Simet(red_lambda, 0.25)
    m200m_baxter = background.lambda_to_m200m_Baxter(red_lambda, 0.25)
    m200c_rykoff = background.lambda_to_m200c_Rykoff(red_lambda, 0.25)
    m200c_simet = background.lambda_to_m200c_Simet(red_lambda, 0.25)
    m200c_baxter = background.lambda_to_m200c_Baxter(red_lambda, 0.25)

    xbins = np.logspace(13.5, 15.5, 100)
    xbins_avg = dtk.bins_avg(xbins)

    plt.figure()
    h, _ = np.histogram(m200m_rykoff, bins=xbins)
    plt.plot(xbins_avg, h, label='rykoff')
    h, _ = np.histogram(m200m_simet, bins=xbins)
    plt.plot(xbins_avg, h, label='simet')
    h, _ = np.histogram(m200m_baxter, bins=xbins)
    plt.plot(xbins_avg, h, label='baxter')
    plt.yscale('log')
    plt.xscale('log')
    plt.ylabel('Counts')
    plt.xlabel('M$_{200m}$ [h$^{-1}$ M$_\odot$]')
    plt.legend(loc='best', framealpha=1.0)
    plt.grid()

    plt.figure()
    h, _ = np.histogram(m200c_rykoff, bins=xbins)
    plt.plot(xbins_avg, h, label='rykoff')
    h, _ = np.histogram(m200c_simet, bins=xbins)
    plt.plot(xbins_avg, h, label='simet')
    h, _ = np.histogram(m200c_baxter, bins=xbins)
    plt.plot(xbins_avg, h, label='baxter')
    plt.yscale('log')
    plt.xscale('log')
    plt.ylabel('Counts')
    plt.xlabel('M$_{200c}$ [h$^{-1}$ M$_\odot$]')
    plt.legend(loc='best', framealpha=1.0)
    plt.grid()

    plt.show()
def query_sdss_mask(file_loc,
                    cat_ra,
                    cat_dec,
                    cat_z,
                    cat_lambda,
                    name,
                    num,
                    r200_factor=1.0,
                    start=0,
                    plot=False,
                    save_data=True,
                    spider_rad=None,
                    spider_mean=False,
                    richness_mass_author='Rykoff'):
    global num_pass
    global num_fail
    fails = []
    print file_loc + name + "_mask.hdf5"
    if (save_data):
        hfile = h5py.File(file_loc + name + "_mask.hdf5", 'w')

    for i in range(start, num):
        start = time.time()
        #query columns are defined here:
        #http://skyserver.sdss.org/dr8/en/help/browser/browser.asp?n=PhotoObjAll&t=U
        print "%d/%d " % (i, num),

        ra = cat_ra[i]
        dec = cat_dec[i]
        z = cat_z[i]
        richness = cat_lambda[i]
        mass, r200 = background.lambda_to_m200_r200(
            richness, z, richness_mass_author=richness_mass_author)
        if spider_rad is None:
            r200_deg = background.r200_to_arcmin(r200, z) / 60.0
            rad = background.r200_to_arcmin(r200, z) * r200_factor
        else:
            r200c_deg = spider_rad[i]
            rad = r200c_deg * 60
            r200 = background.arcmin_to_r200(rad, z)
            mass = background.r200c_to_m200c(r200, z)
            if spider_mean:
                m200m, r200m, c200m = mass_adv.changeMassDefinitionCModel(
                    mass, z, "200c", "200m", c_model='child18')
                mass = m200m
                r200m_r200c_ratio = r200m / r200
                rad *= r200m_r200c_ratio
                r200 *= r200m_r200c_ratio

        print "ra", ra, "dec", dec
        print "z", z, "l", richness, "mass", mass, "r200", r200,
        ## Query and save objects around target
        rad_deg = rad / 60.0
        rad_mask = rad_deg * 3
        a = np.cos(np.pi * dec / 180.0)
        query_str = "SELECT ra, dec, radius, type,area FROM Mask where ra < %f and ra > %f and dec < %f and dec > %f and type != 4" % (
            ra + rad_mask / a, ra - rad_mask / a, dec + rad_mask,
            dec - rad_mask)
        result = sqlcl.query(query_str)
        result.readline()
        try:
            data_mask = np.genfromtxt(StringIO(result.read()),
                                      names=True,
                                      delimiter=",",
                                      dtype=['f4', 'f4', 'f4', 'i4', 'S500'])
            mask_pass = mask_outside_r200(ra, dec, rad_deg, data_mask['area'],
                                          data_mask['type'])
        except ValueError as e:
            print("failed query, auto fail")
            mask_pass = False
        if (mask_pass):
            num_pass += 1
        else:
            num_fail += 1
        print "\tpass:"******"\tfract total: %.3f " % (
            float(num_pass) / float(num_pass + num_fail)),
        if (save_data):
            hgroup = hfile.create_group(str(i))
            dataset = hgroup.require_dataset("mask_pass", (1, 1), 'u1',
                                             mask_pass)
            if (mask_pass):
                dataset[0, 0] = True
            else:
                dataset[0, 0] = False
            dt_str = h5py.special_dtype(vlen=str)
            dataset = hgroup.require_dataset("mask_points",
                                             (data_mask["area"].size, ),
                                             dtype=dt_str)
            if (data_mask["area"].size == 1):
                dataset[0] = data_mask['area']
            else:
                for j in range(0, data_mask["area"].size):
                    dataset[j] = data_mask["area"][j]

        if (plot):
            plt.figure()
            plt.title("pass: "******"cirlce test: dist: ",dist,"ra,dec: ",ras[j],decs[j]
            # plt.plot(ras,decs,'-k')
            for j in range(0, data_mask['area'].size):
                if (data_mask['area'].size == 1):
                    x, y = area_str_to_lines(str(data_mask['area']))
                    mask_type = data_mask['type']
                else:
                    x, y = area_str_to_lines(data_mask['area'][j])
                    mask_type = data_mask['type'][j]
                    if (mask_type == 0):
                        plot_type = 'r'
                    elif (mask_type == 1):
                        plot_type = 'b'
                    elif (mask_type == 2):
                        plot_type = 'g--'
                    elif (mask_type == 3):
                        plot_type = 'k:'
                    plt.plot(x, y, plot_type)
            plt.plot([], [], 'r', label='bleeding')
            plt.plot([], [], 'b', label='bright star')
            plt.plot([], [], 'g--', label='trail')
            plt.plot([], [], 'k:', label='quality hole')

            plt.legend(loc='best')
            plt.xlabel('ra')
            plt.ylabel('dec')
            plt.xlim([ra - rad_mask, ra + rad_mask])
            plt.ylim([dec - rad_mask, dec + rad_mask])
            plt.show()
        #close the file we have been writing to.
        end = time.time()
        print " time: %.2f" % float(end - start)
    if (save_data):
        hfile.close()
def query_sdss_culster(file_loc, cat_ra, cat_dec, cat_z, cat_lambda,
                       name, num, start=0, plot=False,
                       spider_rad=None, spider_mean=False,
                       query_galaxy_only=True, r200_factor=None,
                       richness_mass_author=None):
    fails = []
    if(query_galaxy_only):
        query_table = "Galaxy"
    else:
        query_table = "PhotoObj"
    print("querying...")
    hfile = h5py.File(file_loc+"query_results.hdf5",mode='a')
    for i in range(start,num):
        #try:
        start = time.time()
        print("%d/%d"%(i+1,num))
        keys = hfile.keys()
        if("%s%d"%(name,i) in keys and "%s_prop%d"%(name,i) in keys):
            continue;

        if "%s%d"%(name,i) in keys:
            del hfile["%s%d"%(name,i)]

        if "%s_prop%d"%(name,i) in keys:
            del hfile["%s_prop%d"%(name,i)]
 
        #query columns are defined here:
        #http://skyserver.sdss.org/dr8/en/help/browser/browser.asp?n=PhotoObjAll&t=U
           
        ra = cat_ra[i]
        dec = cat_dec[i]
        z   = cat_z[i]
        richness = cat_lambda[i]

        # Xray Spiders have their own r200c, so we don't need to compute it. 
        if spider_rad is None: 
            mass, r200 = background.lambda_to_m200_r200(richness,z, richness_mass_author=richness_mass_author)
            rad = background.r200_to_arcmin(r200, z)
        else:
            r200c_deg = spider_rad[i]
            rad = r200c_deg * 60
            r200 = background.arcmin_to_r200(rad, z)
            mass = background.r200c_to_m200c(r200, z)
            if spider_mean:
                m200m, r200m, c200m =mass_adv.changeMassDefinitionCModel(mass, z, 
                                                                         "200c", "200m",
                                                                         c_model='child18')
                mass = m200m
                r200m_r200c_ratio = r200m/r200
                rad *= r200m_r200c_ratio
                r200 *= r200m_r200c_ratio
                

        hgroup = hfile.create_group("%s_prop%d"%(name,i))
        hgroup.create_dataset("ra",data=ra)
        hgroup.create_dataset("dec",data=dec)
        hgroup.create_dataset("z",data=z)
        hgroup.create_dataset("mass",data=mass)
        hgroup.create_dataset("rad",data=rad)
        hgroup.create_dataset("r200",data=r200)
        hgroup.create_dataset("richness", data = richness)
        ## Query and save objects around target

        query_str = "select  p.ra, p.dec, p.type,p.insidemask,p.flags_g,p.flags_i,p.flags_r,p.cModelMag_u, p.cModelMagErr_u,p.cModelMag_g, p.cModelMagErr_g,p.cModelMag_r, p.cModelMagErr_r,p.cModelMag_i, p.cModelMagErr_i,p.cModelMag_z, p.cModelMagErr_z from "+query_table+" p join dbo.fGetNearbyObjEq(%f,%f,%f) r on p.ObjID = r.ObjID"%(ra, dec, r200_factor*rad)

        result = sqlcl.query(query_str).read()
        # datagal = np.genfromtxt(StringIO(result),names=True,delimiter=', ',dtype=['f8','f8','i2','i1','i8','i8','i8', 'f4','f4', 'f4','f4', 'f4','f4', 'f4','f4', 'f4','f4'])
        try:
            datagal = np.genfromtxt(StringIO(result),names=True,skip_header=1,delimiter=',',dtype=['f8','f8','i2','i1','i8','i8','i8', 'f4','f4', 'f4','f4', 'f4','f4', 'f4','f4', 'f4','f4'])
        except ValueError as e:
            print(query_str)
            continue

        hgroup = hfile.create_group("%s%d"%(name,i))
        hgroup.create_dataset("ra",data=datagal['ra'])
        hgroup.create_dataset("dec",data=datagal['dec'])
        hgroup.create_dataset("type",data=datagal['type'])
        hgroup.create_dataset("insidemask",data=datagal['insidemask'])
        hgroup.create_dataset("flags_g",data=datagal['flags_g'])
        hgroup.create_dataset("flags_i",data=datagal['flags_i'])
        hgroup.create_dataset("flags_r",data=datagal['flags_r'])
        hgroup.create_dataset("mag_u",data=datagal['cModelMag_u'])
        hgroup.create_dataset("mag_err_u",data=datagal['cModelMagErr_u'])
        hgroup.create_dataset("mag_g",data=datagal['cModelMag_g'])
        hgroup.create_dataset("mag_err_g",data=datagal['cModelMagErr_g'])
        hgroup.create_dataset("mag_r",data=datagal['cModelMag_r'])
        hgroup.create_dataset("mag_err_r",data=datagal['cModelMagErr_r'])
        hgroup.create_dataset("mag_i",data=datagal['cModelMag_i'])
        hgroup.create_dataset("mag_err_i",data=datagal['cModelMagErr_i'])
        hgroup.create_dataset("mag_z",data=datagal['cModelMag_z'])
        hgroup.create_dataset("mag_err_z",data=datagal['cModelMagErr_z'])

        end = time.time()
        print(" time: %.2f"%float(end-start))
        if(plot):
            plt.figure()
            legends = ["uknown","cosmic_ray","defect","galaxy","ghost","knownobj","star","trail","sky","notatype"]
            slct1 = datagal['insidemask']==0
            for i in range(0,10):
                slct = (datagal["type"] == i) & slct1
                plt.plot(datagal['ra'][slct],datagal['dec'][slct],'x',label=legends[i])
            plt.legend(loc='best')
            plt.xlabel('ra')
            plt.ylabel('dec')
            plt.show()
        # except ValueError as ie:
        #     print ie
        #     fails.append(i)
        #     print "Failure"
        np.save(file_loc+"fail_indexs.npy",fails)
    hfile.close();