def mock_vpf(mock_cart_coords, spheresfile, simul_cosmo, rad):

    gals = h5_arr(mock_cart_coords, "coords")

    print gals

    name = mock_cart_coords.split("/")[-1].split(".")[0]

    if simul_cosmo == "Planck":
        # First make h free
        Planck13.__init__(100.0, Planck13.Om0)
        cosmo = Planck13
    elif simul_cosmo == "WMAP":
        WMAP5.__init__(100.0, WMAP5.Om0)
        cosmo = WMAP5
    comv = cosmo.comoving_distance

    gal_baum = cKDTree(gals)

    spheres = h5_arr(spheresfile, "radecz_{0}".format(str(int(rad))))

    print spheres

    for i, sphere in enumerate(spheres):

        rang = Angle(sphere[0], u.deg)
        decang = Angle(sphere[1], u.deg)

        dis = Distance(comv(sphere[2]), u.Mpc)

        coord = ICRSCoordinates(rang, decang, distance=dis)

        sph_cen = np.array([coord.x, coord.y, coord.z])

        nn = gal_baum.query(sph_cen)

        print "rad: ", rad, ", sphere: ", i

        f = open(
            "{0}/vpf_out/ascii/{1}_{2}.dat".format(
                os.path.dirname(spheresfile), name, str(int(rad))), 'a')

        if not nn[0] < rad:
            f.write("1\n")
        else:
            f.write("0\n")

        f.close()
def mk_mock_coords(radeczfile, outfile, simul_cosmo):

    if simul_cosmo == "Planck":
        Planck13.__init__(100.0, Planck13.Om0)
        cosmo = Planck13
    elif simul_cosmo == "WMAP":
        WMAP5.__init__(100.0, WMAP5.Om0)
        cosmo = WMAP5

    rad = np.arange(1.0, 67.0, 5.0)

    radecz = h5_arr(radeczfile, "radecz")

    cart = np.zeros(radecz.shape)

    for i, rdz in enumerate(radecz):

        ra = Angle(rdz[0], u.deg)
        dec = Angle(rdz[1], u.deg)

        losd = cosmo.comoving_distance(rdz[2])
        dis = Distance(losd)

        coord = ICRSCoordinates(ra, dec, distance=dis)

        cart[i, :] = np.array([coord.x.value, coord.y.value, coord.z.value])

    np.savetxt(outfile, cart)
Example #3
0
def mk_mock_coords(radeczfile, outfile, simul_cosmo):

    if simul_cosmo == "Planck":
        Planck13.__init__(100.0, Planck13.Om0)
        cosmo = Planck13
    elif simul_cosmo == "WMAP":
        WMAP5.__init__(100.0, WMAP5.Om0)
        cosmo = WMAP5

    rad = np.arange(1.0, 67.0, 5.0)

    radecz = h5_arr(radeczfile, "radecz")

    cart = np.zeros(radecz.shape)

    for i, rdz in enumerate(radecz):

        ra = Angle(rdz[0], u.deg)
        dec = Angle(rdz[1], u.deg)

        losd = cosmo.comoving_distance(rdz[2])
        dis = Distance(losd, u.Mpc)

        coord = ICRSCoordinates(ra, dec, distance=dis)

        cart[i, :] = np.array([coord.x, coord.y, coord.z])

    arr2h5(cart, outfile, "coords", mode='w')
Example #4
0
def mock_vpf(mock_cart_coords, spheresfile, simul_cosmo, rad):

    gals = h5_arr(mock_cart_coords, "coords")

    print gals

    name = mock_cart_coords.split("/")[-1].split(".")[0]

    if simul_cosmo == "Planck":
        # First make h free
        Planck13.__init__(100.0, Planck13.Om0)
        cosmo = Planck13
    elif simul_cosmo == "WMAP":
        WMAP5.__init__(100.0, WMAP5.Om0)
        cosmo = WMAP5
    comv = cosmo.comoving_distance

    gal_baum = cKDTree(gals)

    spheres = h5_arr(spheresfile, "radecz_{0}".format(str(int(rad))))

    print spheres

    for i, sphere in enumerate(spheres):

        rang = Angle(sphere[0], u.deg)
        decang = Angle(sphere[1], u.deg)

        dis = Distance(comv(sphere[2]), u.Mpc)

        coord = ICRSCoordinates(rang, decang, distance=dis)

        sph_cen = np.array([coord.x, coord.y, coord.z])

        nn = gal_baum.query(sph_cen)

        print "rad: ", rad, ", sphere: ", i

        f = open("{0}/vpf_out/ascii/{1}_{2}.dat".format(os.path.dirname(spheresfile), name, str(int(rad))), 'a')

        if not nn[0] < rad:
            f.write("1\n")
        else:
            f.write("0\n")

        f.close()
Example #5
0
def mock_vpf(mock_cart_coords, spheresfile, simul_cosmo):

    gals = h5_arr(mock_cart_coords, "coords")
    name = mock_cart_coords.split("/")[-1].split(".")[0]

    if simul_cosmo == "Planck":
        # First make h free
        Planck13.__init__(100.0, Planck13.Om0)
        cosmo = Planck13
    elif simul_cosmo == "WMAP":
        WMAP5.__init__(100.0, WMAP5.Om0)
        cosmo = WMAP5
    comv = cosmo.comoving_distance

    gal_baum = cKDTree(gals)

    rad = np.arange(1.0, 67.0, 5.0)

    for r_i, r in enumerate(rad):

        spheres = h5_arr(spheresfile, "radecz_{0}".format(str(r_i * 5 + 1)))
        voids = np.zeros(spheres.shape[0])

        for i, sphere in enumerate(spheres):

            rang = Angle(sphere[0], u.deg)
            decang = Angle(sphere[1], u.deg)

            dis = Distance(comv(sphere[2]), u.Mpc)

            coord = ICRSCoordinates(rang, decang, distance=dis)

            sph_cen = np.array([coord.x.value, coord.y.value, coord.z.value])

            nn = gal_baum.query(sph_cen)

            print "rad: ", r, ", sphere: ", i

            if not nn[0] < r:

                voids[i] = 1

        arr2h5(voids,
                "{0}/vpf_out/{1}.hdf5".format(os.path.dirname(spheresfile), name),
                "voids_{0}".format(str(r_i * 5 + 1)))
def mk_mock_srch(radecfile, nzdictfile, Nsph, simul_cosmo):

    if simul_cosmo == "Planck":
        # First make h free
        Planck13.__init__(100.0, Planck13.Om0)
        cosmo = Planck13
    elif simul_cosmo == "WMAP":
        WMAP5.__init__(100.0, WMAP5.Om0)
        cosmo = WMAP5
    comv = cosmo.comoving_distance

    radecarr = h5_arr(radecfile, "good_pts")
    nzdict = json.load(open(nzdictfile))

    Nrands = radecarr.shape[0]
    Narrs = Nsph / Nrands
    remain = Nsph % Nrands

    radecz = np.zeros((Nsph, 3))

    for i in range(Narrs):

        start = Nrands * i
        stop = Nrands * (i + 1)
        radecz[start:stop, :2] = radecarr[:, :]

    endchunk = Nrands * (Narrs)
    radecz[endchunk:, :2] = radecarr[:remain, :]

    rad = np.arange(1.0, 67.0, 5.0)
    zlo = nzdict["zlo"]
    zhi = nzdict["zhi"]

    radeczlist = len(rad) * [radecz]

    for r_i, r in enumerate(rad):

        dis_near = Distance(comv(zlo).value + r, u.Mpc)
        dis_far = Distance(comv(zhi).value - r, u.Mpc)

        z_a = dis_near.compute_z(cosmology=cosmo)

        z_b = dis_far.compute_z(cosmology=cosmo)

        randz = (z_a ** 3 + \
                 (z_b ** 3 - z_a ** 3) * np.random.rand(Nsph)) ** (1. / 3.)

        radeczlist[r_i][:, 2] = randz[:]

        arr2h5(
            radeczlist[r_i], "{0}/{1}/mocks/mock_srch_pts.hdf5".format(
                os.path.dirname(radecfile), simul_cosmo),
            "radecz_{0}".format(str(r_i * 5 + 1)))
Example #7
0
def mk_mock_srch(radecfile, nzdictfile, Nsph, simul_cosmo):

    if simul_cosmo == "Planck":
        # First make h free
        Planck13.__init__(100.0, Planck13.Om0)
        cosmo = Planck13
    elif simul_cosmo == "WMAP":
        WMAP5.__init__(100.0, WMAP5.Om0)
        cosmo = WMAP5
    comv = cosmo.comoving_distance

    radecarr = h5_arr(radecfile, "good_pts")
    nzdict = json.load(open(nzdictfile))

    Nrands = radecarr.shape[0]
    Narrs = Nsph / Nrands
    remain = Nsph % Nrands

    radecz = np.zeros((Nsph, 3))

    for i in range(Narrs):

        start = Nrands * i
        stop = Nrands * (i + 1)
        radecz[start:stop, :2] = radecarr[:, :]

    endchunk = Nrands * (Narrs)
    radecz[endchunk:, :2] = radecarr[:remain, :]

    rad = np.arange(1.0, 67.0, 5.0)
    zlo = nzdict["zlo"]
    zhi = nzdict["zhi"]

    radeczlist = len(rad) * [radecz]

    for r_i, r in enumerate(rad):

        dis_near = Distance(comv(zlo) + r, u.Mpc)
        dis_far = Distance(comv(zhi) - r, u.Mpc)

        z_a = dis_near.compute_z(cosmology=cosmo)

        z_b = dis_far.compute_z(cosmology=cosmo)

        randz = (z_a ** 3 + \
                 (z_b ** 3 - z_a ** 3) * np.random.rand(Nsph)) ** (1. / 3.)

        radeczlist[r_i][:, 2] = randz[:]

        arr2h5(radeczlist[r_i], "{0}/{1}/mocks/mock_srch_pts.hdf5".format(os.path.dirname(radecfile), simul_cosmo), "radecz_{0}".format(str(r_i * 5 + 1)))
Example #8
0
import sys
from glob import glob
import numpy as np
from dat.h5_funcs import h5_arr

survey_cap = "CMASS/NGC"
cosmo = "WMAP"
spheresfile = "dat/out/{0}/{1}/mocks/mock_srch_pts.hdf5".format(survey_cap, cosmo)

if len(sys.argv) != 2:
    print "usage: rdz2ascii.py <mock no.: int>"

digits = len(sys.argv[1])

i = 0
name = ""
while i < (4 - digits):
    name += "0"
    i += 1

name += sys.argv[1]

mock_rdzs = glob("dat/out/{0}/{1}/mocks/rdz_down/{2}.hdf5".format(survey_cap, cosmo, name))

for i in range(len(mock_rdzs)):

    mock_rdz = h5_arr(mock_rdzs[i], "radecz")

    np.savetxt("dat/out/{0}/{1}/mocks/rdz_down/ascii/{2}.dat".format(survey_cap, cosmo, name), mock_rdz)
Example #9
0
def vpf(dat_dir, Nsph, simul_cosmo, rad):

    # Grab the data coordinates
    gals = h5_arr("./dat/out/{0}/{1}/gals_cart_coords.hdf5".
                      format(dat_dir, simul_cosmo), "cart_pts")

    # Get details about the redshift interval being considered
    nbar_dict = json.load(open("./dat/out/{0}/{1}/nbar_zrange.json".
                                   format(dat_dir, simul_cosmo)))
    zlo = nbar_dict["zlo"]
    zhi = nbar_dict["zhi"]

    # Get the search points
    good_pts = h5_arr("./dat/out/{0}/srch_radec.hdf5".format(dat_dir), "good_pts")
    bad_pts = h5_arr("./dat/out/{0}/veto.hdf5".format(dat_dir),
                     "bad_pts")

    # Set angular radius of effective area around bad points
    bad_r = np.arccos(1.0 - (np.pi * 9.8544099e-05) / (2 * 180 ** 2))
    bad_r_deg = np.rad2deg(bad_r)

    # Set the cosmology with h free
    # Here the cosmology is based on WMAP (for first MultiDark simulation)
    if simul_cosmo == "Planck":
        # First make h free
        Planck13.__init__(100.0, Planck13.Om0)
        cosmo = Planck13
    elif simul_cosmo == "WMAP":
        WMAP5.__init__(100.0, WMAP5.Om0)
        cosmo = WMAP5
    comv = cosmo.comoving_distance

    # Build the trees

    # galaxy tree
    gal_baum = cKDTree(gals)

    # tree of bad points (angular coordinates on unit sphere)
    bad_xyz = radec2xyz(bad_pts)
    veto_baum = cKDTree(bad_xyz)

    # Initialise final output arrays
#    rad = np.arange(1.0, 67.0, 5.0)  doing it one radius at a time
#    P_0 = np.zeros(rad.shape)

    # No. of spheres and norm
#     Nsph_arr = Nsph * np.array(4 * [0.01] + 4 * [0.1] + 4 * [1.0])
#     norm = 1. / Nsph_arr
#    norm = 1. / Nsph

    rand_i = 0

    for r_i, r in enumerate(rad):

        # start the count of successful voids
        count = 0

        # Custom zrange for sphere size
        dis_near = Distance(comv(zlo).value + r, u.Mpc)
        dis_far = Distance(comv(zhi).value - r, u.Mpc)

        z_a = dis_near.compute_z(cosmology=cosmo)

        z_b = dis_far.compute_z(cosmology=cosmo)

        for i in range(Nsph):  # _arr[r_i]):

            # compensate for finite length of mask file
            rand_i = rand_i % 999999

            radec = good_pts[rand_i, :]

            rang = Angle(radec[0], u.deg)
            decang = Angle(radec[1], u.deg)

            randz = (z_a ** 3 + \
                     (z_b ** 3 - z_a ** 3) * np.random.rand(1)[0]) ** (1. / 3.)
            dis = Distance(comv(randz), u.Mpc)

            coord = ICRSCoordinates(rang, decang, distance=dis)

            sph_cen = np.array([coord.x.value, coord.y.value, coord.z.value])

            nn = gal_baum.query(sph_cen)

            print "rad: ", r, ", sphere: ", i

            if not nn[0] < r:

                # add instance to probability count
                count += 1

                # record quality of sphere using spline values for intersection
                # with bad points

                # Get radius of circular projection of sphere
                R = np.arcsin(r / np.sqrt(np.sum(sph_cen[:] ** 2)))

                # Get coordinates of circle centre on unit sphere
                crc_cen = radec2xyz(radec)[0]

                # Compute tree search radius from Cosine rule
                # (include points extending beyond sphere edge to account for
                # finite area around bad points)
                l_srch = np.sqrt(2. - 2. * np.cos(R))

                # Run search
                pierce_l = veto_baum.query_ball_point(crc_cen, l_srch)

                bad_vol = 0.

                R = np.degrees(R)  # need in degrees for bad_vol computation

                for pt in pierce_l:

                    pt_ang = bad_pts[pt]
                    dis = np.degrees(central_angle(pt_ang, radec))
                    l = dis / R

                    bad_vol += 1.5 * (bad_r_deg / R) ** 2 \
                                   * np.sqrt(1.0 - l ** 2)

                f_r = open("./dat/out/{0}/{1}/vpf_out/volfrac_{2}.dat".
                               format(dat_dir, simul_cosmo, r),
                           'a')
                f_r.write("{0}\n".format(bad_vol))
                f_r.close()

            rand_i += 1
z_far = np.arange((0.5 * (z_near[0] + z_near[1])), z_far[-1] + 0.5 * dzf, 0.5 * dzf)
zcen = 0.5 * (z_far + z_near)
zbinedges = np.append(z_near[0], z_far[:])

print "\nthen\n"
print "zcen: ", zcen
print "z_near: ", z_near
print "z_far: ", z_far
print "zbinedges: ", zbinedges

shell_vols = []
for i in range(len(zcen)):
    shell_vols.append(Nfrac * calc_shell_vol(comv, z_near[i], z_far[i], zcen[i]))
shell_vols = np.array(shell_vols)

dat_rdz = h5_arr("dat/out/CMASS/NGC/WMAP/radecz_down.hdf5", "radecz")

dat_hist = np.histogram(dat_rdz[:, 2], bins=zbinedges)[0]

dat_nbar = dat_hist / shell_vols

mock_nbars = []
mock_fns = glob("dat/out/CMASS/NGC/WMAP/mocks/rdz_down/*")

for mock in mock_fns:

    radecz = h5_arr(mock, "radecz")

    H = np.histogram(radecz[:, 2], bins=zbinedges)

    mock_nbars.append(H[0] / shell_vols)
zcen = 0.5 * (z_far + z_near)
zbinedges = np.append(z_near[0], z_far[:])

print "\nthen\n"
print "zcen: ", zcen
print "z_near: ", z_near
print "z_far: ", z_far
print "zbinedges: ", zbinedges

shell_vols = []
for i in range(len(zcen)):
    shell_vols.append(Nfrac *
                      calc_shell_vol(comv, z_near[i], z_far[i], zcen[i]))
shell_vols = np.array(shell_vols)

dat_rdz = h5_arr("dat/out/CMASS/NGC/WMAP/radecz_down.hdf5", "radecz")

dat_hist = np.histogram(dat_rdz[:, 2], bins=zbinedges)[0]

dat_nbar = dat_hist / shell_vols

mock_nbars = []
mock_fns = glob("dat/out/CMASS/NGC/WMAP/mocks/rdz_down/*")

for mock in mock_fns:

    radecz = h5_arr(mock, "radecz")

    H = np.histogram(radecz[:, 2], bins=zbinedges)

    mock_nbars.append(H[0] / shell_vols)
Example #12
0
def vpf(dat_dir, Nsph, simul_cosmo, rad):

    # Grab the data coordinates
    gals = h5_arr(
        "./dat/out/{0}/{1}/gals_cart_coords.hdf5".format(dat_dir, simul_cosmo),
        "cart_pts")

    # Get details about the redshift interval being considered
    nbar_dict = json.load(
        open("./dat/out/{0}/{1}/nbar_zrange.json".format(dat_dir,
                                                         simul_cosmo)))
    zlo = nbar_dict["zlo"]
    zhi = nbar_dict["zhi"]

    # Get the search points
    good_pts = h5_arr("./dat/out/{0}/srch_radec.hdf5".format(dat_dir),
                      "good_pts")
    bad_pts = h5_arr("./dat/out/{0}/veto.hdf5".format(dat_dir), "bad_pts")

    # Set angular radius of effective area around bad points
    bad_r = np.arccos(1.0 - (np.pi * 9.8544099e-05) / (2 * 180**2))
    bad_r_deg = np.rad2deg(bad_r)

    # Set the cosmology with h free
    # Here the cosmology is based on WMAP (for first MultiDark simulation)
    if simul_cosmo == "Planck":
        # First make h free
        Planck13.__init__(100.0, Planck13.Om0)
        cosmo = Planck13
    elif simul_cosmo == "WMAP":
        WMAP5.__init__(100.0, WMAP5.Om0)
        cosmo = WMAP5
    comv = cosmo.comoving_distance

    # Build the trees

    # galaxy tree
    gal_baum = cKDTree(gals)

    # tree of bad points (angular coordinates on unit sphere)
    bad_xyz = radec2xyz(bad_pts)
    veto_baum = cKDTree(bad_xyz)

    # Initialise final output arrays
    #    rad = np.arange(1.0, 67.0, 5.0)  doing it one radius at a time
    #    P_0 = np.zeros(rad.shape)

    # No. of spheres and norm
    #     Nsph_arr = Nsph * np.array(4 * [0.01] + 4 * [0.1] + 4 * [1.0])
    #     norm = 1. / Nsph_arr
    #    norm = 1. / Nsph

    rand_i = 0

    for r_i, r in enumerate(rad):

        # start the count of successful voids
        count = 0

        # Custom zrange for sphere size
        dis_near = Distance(comv(zlo).value + r, u.Mpc)
        dis_far = Distance(comv(zhi).value - r, u.Mpc)

        z_a = dis_near.compute_z(cosmology=cosmo)

        z_b = dis_far.compute_z(cosmology=cosmo)

        for i in range(Nsph):  # _arr[r_i]):

            # compensate for finite length of mask file
            rand_i = rand_i % 999999

            radec = good_pts[rand_i, :]

            rang = Angle(radec[0], u.deg)
            decang = Angle(radec[1], u.deg)

            randz = (z_a ** 3 + \
                     (z_b ** 3 - z_a ** 3) * np.random.rand(1)[0]) ** (1. / 3.)
            dis = Distance(comv(randz), u.Mpc)

            coord = ICRSCoordinates(rang, decang, distance=dis)

            sph_cen = np.array([coord.x.value, coord.y.value, coord.z.value])

            nn = gal_baum.query(sph_cen)

            print "rad: ", r, ", sphere: ", i

            if not nn[0] < r:

                # add instance to probability count
                count += 1

                # record quality of sphere using spline values for intersection
                # with bad points

                # Get radius of circular projection of sphere
                R = np.arcsin(r / np.sqrt(np.sum(sph_cen[:]**2)))

                # Get coordinates of circle centre on unit sphere
                crc_cen = radec2xyz(radec)[0]

                # Compute tree search radius from Cosine rule
                # (include points extending beyond sphere edge to account for
                # finite area around bad points)
                l_srch = np.sqrt(2. - 2. * np.cos(R))

                # Run search
                pierce_l = veto_baum.query_ball_point(crc_cen, l_srch)

                bad_vol = 0.

                R = np.degrees(R)  # need in degrees for bad_vol computation

                for pt in pierce_l:

                    pt_ang = bad_pts[pt]
                    dis = np.degrees(central_angle(pt_ang, radec))
                    l = dis / R

                    bad_vol += 1.5 * (bad_r_deg / R) ** 2 \
                                   * np.sqrt(1.0 - l ** 2)

                f_r = open(
                    "./dat/out/{0}/{1}/vpf_out/volfrac_{2}.dat".format(
                        dat_dir, simul_cosmo, r), 'a')
                f_r.write("{0}\n".format(bad_vol))
                f_r.close()

            rand_i += 1