R = np.arcsin(r / np.sqrt(np.sum(sph_cen[:] ** 2)))

        # Get coordinates of circle centre on unit sphere
        crc_cen = radec2xyz(sphere[:2])[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, sphere[:2]))
            l = dis / R

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

        badvols[i] = bad_vol

    arr2h5(badvols,
            "{0}/mock_badvols.hdf5".format(os.path.dirname(spheresfile)),
            "badvols_{0}".format(str(r_i * 5 + 1)))
def process_nbar(nbarfile, nz_dict_file, cosmology, radeczfile=None):
    """
    Parameters
    ---------

    nbarfile : str
        the path to and name of the corrected nbar file
    nz_dict_file : str
        path to and name of the json file with the nbar dict
    cosmology : str, "WMAP" or "Planck"
        the cosmology to compute shell volumes with
    radeczfile : str, "data" or "mock"
        the data or mock file to process
    """

    # magic number for width around maximum
    Q = 0.65
    # magic number for shell vol computation
    Nfrac = (6769.0358 * np.pi) / 129600

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

    nbar_corr = np.loadtxt(nbarfile)
    nz_dict = {"tophat height for zrange": Q}

    # Cut out the first bit of crap (works for CMASS, dunno about LOWZ)
    ind03 = np.abs(nbar_corr[:, 0] - 0.3).argmin()

    nbar_corr = nbar_corr[ind03:, :]

    zcen = nbar_corr[:, 0]
    z_near = nbar_corr[:, 1]
    z_far = nbar_corr[:, 2]
    corr_gal_counts = nbar_corr[:, 6]

    nbar = []
    shell_vols = []

    for i in range(len(zcen)):

        shell_vols.append(Nfrac * calc_shell_vol(comv, z_near[i], z_far[i], zcen[i]))
        nbar.append(corr_gal_counts[i] / shell_vols[i])

    nbar = np.array(nbar)

    # Find nbar peak and index
    max_nbar = np.max(nbar)
    max_i = int(np.where(nbar == max_nbar)[0])

    nz_dict["max_nbar_corr"] = max_nbar
    nz_dict["nbar_corr_tophat"] = Q * max_nbar
    nz_dict["z_nbar_max"] = zcen[max_i]

    # get the interval edge indices
    L = np.abs(nbar[:max_i] - max_nbar * Q).argmin()
    R = max_i + np.abs(nbar[max_i:] - max_nbar * Q).argmin()

    nbar = nbar[L:R + 1]
    shell_vols = shell_vols[L:R + 1]

    nz_dict["zlo"] = zcen[L]
    nz_dict["zhi"] = zcen[R]

    nz_dict["avg_nbar_corr"] = np.average(nbar)
    nz_dict["total_shell_vol"] = np.sum(shell_vols)

    if radeczfile:

        radecz = h5_arr(radeczfile, "radecz")

        # Make the redshift cut in the nbar array with right cosmology
        nbar_corr = nbar_corr[(nz_dict["zlo"] <= nbar_corr[:, 0]) * \
                            (nbar_corr[:, 0] <= nz_dict["zhi"])]

        # Get binning those observed galaxies
        zbinedges = np.append(nbar_corr[0, 1], nbar_corr[:, 2])

        # Find the counts per bin
        H = np.histogram(radecz[:, 2], bins=zbinedges)

        # The number to downsample to in each bin
        # (multiply bin number by the relative fraction determined from
        #  corrected distribution of nbar)
        num_down = np.rint((nz_dict["nbar_corr_tophat"] / nbar[:]) * H[0])
        num_down = num_down.astype(int)

        # make a mask for the final array for analysis within the redshift limits
        finmask = np.array(radecz.shape[0] * [False])

        for i, nd in enumerate(num_down):
            """Turn on the right amount of galaxies in each bin."""
            zbin_ids = np.where(((zbinedges[i] < radecz[:, 2]) * (radecz[:, 2] <= zbinedges[i + 1])) == True)

            if zbin_ids[0].shape[0] == 0:
                continue

            keep = np.random.choice(zbin_ids[0], size=nd, replace=False)

            finmask[keep] = True

        radecz = radecz[finmask]

        if not radeczfile.split('/')[-2] == "mocks_hierarchical":
            # now get nbar for the downsampled data for use in mock processing and simulation
            gal_counts = np.histogram(radecz[:, 2], bins=zbinedges)[0]

            nbar_down = []

            for i in range(len(gal_counts)):

                nbar_down.append(gal_counts[i] / shell_vols[i])

            nbar_down = np.array(nbar_down)

            # save the average downsampled value
            nz_dict["avg_nbar_down"] = np.average(nbar_down)

            # and save downsampled array to a hdf5 file
            arr2h5(radecz, "{0}/radecz_down.hdf5".format(os.path.dirname(nz_dict_file)), "radecz")

        # if we are dealing with a mockfile, then there is an extra factor to make
        # the average equal that of the data
        if radeczfile.split('/')[-2] == "mocks_hierarchical":

            # have to open the existing json file
            jf = open(nz_dict_file)
            nz_dict = json.load(jf)

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

            nbar_mock = []

            for i in range(len(gal_counts)):

                nbar_mock.append(gal_counts[i] / shell_vols[i])

            nbar_mock = np.array(nbar_mock)

            num_down = np.rint((nz_dict["avg_nbar_down"] / np.average(nbar_mock)) * H[0])
            num_down = num_down.astype(int)

            finmask = np.array(radecz.shape[0] * [False])

            for i, nd in enumerate(num_down):
                """Turn on the right amount of galaxies in each bin."""
                zbin_ids = np.where(((zbinedges[i] < radecz[:, 2]) * \
                                     (radecz[:, 2] <= zbinedges[i + 1])) == True)

                keep = np.random.choice(zbin_ids[0], size=nd, replace=False)

                finmask[keep] = True

            radecz = radecz[finmask]

            # and save to a hdf5 file
            mock_no = radeczfile.split('/')[-1].split('.')[0]
            arr2h5(radecz, "{0}/mocks/rdz_down/{1}.hdf5".format(os.path.dirname(nz_dict_file), mock_no), "radecz")

            jf.close()

    if not radeczfile.split('/')[-2] == "mocks_hierarchical":
        # don't save the json if we're working on a mock
        nf = open(nz_dict_file, 'w')
    
        json.dump(nz_dict, nf, sort_keys=True, indent=4, separators=(',', ':\t'))
    
        nf.close()
Exemple #3
0
        R = np.arcsin(r / np.sqrt(np.sum(sph_cen[:]**2)))

        # Get coordinates of circle centre on unit sphere
        crc_cen = radec2xyz(sphere[:2])[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, sphere[:2]))
            l = dis / R

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

        badvols[i] = bad_vol

    arr2h5(badvols,
           "{0}/mock_badvols.hdf5".format(os.path.dirname(spheresfile)),
           "badvols_{0}".format(str(r_i * 5 + 1)))
def process_nbar(nbarfile, nz_dict_file, cosmology, radeczfile=None):
    """
    Parameters
    ---------

    nbarfile : str
        the path to and name of the corrected nbar file
    nz_dict_file : str
        path to and name of the json file with the nbar dict
    cosmology : str, "WMAP" or "Planck"
        the cosmology to compute shell volumes with
    radeczfile : str, "data" or "mock"
        the data or mock file to process
    """

    # magic number for width around maximum
    Q = 0.65
    # magic number for shell vol computation
    Nfrac = (6769.0358 * np.pi) / 129600

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

    nbar_corr = np.loadtxt(nbarfile)
    nz_dict = {"tophat height for zrange": Q}

    # Cut out the first bit of crap (works for CMASS, dunno about LOWZ)
    ind03 = np.abs(nbar_corr[:, 0] - 0.3).argmin()

    nbar_corr = nbar_corr[ind03:, :]

    zcen = nbar_corr[:, 0]
    z_near = nbar_corr[:, 1]
    z_far = nbar_corr[:, 2]
    corr_gal_counts = nbar_corr[:, 6]

    nbar = []
    shell_vols = []

    for i in range(len(zcen)):

        shell_vols.append(Nfrac * calc_shell_vol(comv, z_near[i], z_far[i], zcen[i]))
        nbar.append(corr_gal_counts[i] / shell_vols[i])

    nbar = np.array(nbar)
    shell_vols = np.array(shell_vols)

    # Find nbar peak and index
    max_nbar = np.max(nbar)
    max_i = int(np.where(nbar == max_nbar)[0])

    nz_dict["max_nbar_corr"] = max_nbar
    nz_dict["nbar_corr_tophat"] = Q * max_nbar
    nz_dict["z_nbar_max"] = zcen[max_i]

    # get the interval edge indices
    L = np.abs(nbar[:max_i] - max_nbar * Q).argmin()
    R = max_i + np.abs(nbar[max_i:] - max_nbar * Q).argmin()

    nbar = nbar[L:R + 1]
    shell_vols = shell_vols[L:R + 1]

    nz_dict["zlo"] = zcen[L]
    nz_dict["zhi"] = zcen[R]

    nz_dict["avg_nbar_corr"] = np.average(nbar)
    nz_dict["total_shell_vol"] = np.sum(shell_vols)

    if radeczfile:

        radecz = h5_arr(radeczfile, "radecz")

        # Make the redshift cut in the nbar array with right cosmology
        nbar_corr = nbar_corr[(nz_dict["zlo"] <= nbar_corr[:, 0]) * \
                            (nbar_corr[:, 0] <= nz_dict["zhi"])]

        # Get binning those observed galaxies
        zbinedges = np.append(nbar_corr[0, 1], nbar_corr[:, 2])

        # Find the counts per bin and convert to nbar
        H = np.histogram(radecz[:, 2], bins=zbinedges)
        hist_nbar = H[0] / shell_vols

        if not radeczfile.split('/')[-2] == "mocks_hierarchical":
            # save the average downsampled value if it's the data file
            nz_dict["avg_nbar_down"] = np.average(hist_nbar)

            # The number to downsample to in each bin
            # (multiply bin number by the relative fraction determined from
            #  corrected distribution of nbar)
            nz_dict["nbar_data_tophat"] = 0.95 * nz_dict["nbar_corr_tophat"] * (nz_dict["avg_nbar_down"] / nz_dict["avg_nbar_corr"])
            factor_arr = nz_dict["nbar_data_tophat"] / hist_nbar

        # if we are dealing with a mockfile, then there is an extra factor to make
        # the average equal that of the data
        elif radeczfile.split('/')[-2] == "mocks_hierarchical":

            # have to open the existing json file
            jf = open(nz_dict_file)
            nz_dict = json.load(jf)

            factor_arr = nz_dict["nbar_data_tophat"] / hist_nbar

            jf.close()

        num_down = np.rint(factor_arr * H[0])
        num_down = num_down.astype(int)

        # make a mask for the final array for analysis within the redshift limits
        finmask = np.array(radecz.shape[0] * [False])

        for i, nd in enumerate(num_down):
            """Turn on the right amount of galaxies in each bin."""
            zbin_ids = np.where(((zbinedges[i] < radecz[:, 2]) * (radecz[:, 2] <= zbinedges[i + 1])) == True)

            if zbin_ids[0].shape[0] == 0:
                continue

            keep = np.random.choice(zbin_ids[0], size=nd, replace=False)

            finmask[keep] = True

        radecz = radecz[finmask]

        if not radeczfile.split('/')[-2] == "mocks_hierarchical":
            # and save downsampled array to a hdf5 file
            arr2h5(radecz, "{0}/radecz_down.hdf5".format(os.path.dirname(nz_dict_file)), "radecz", mode='w')

        elif radeczfile.split('/')[-2] == "mocks_hierarchical":
            # save mocks to a hdf5 file
            mock_no = radeczfile.split('/')[-1].split('.')[0]
            arr2h5(radecz, "{0}/mocks/rdz_down/{1}.hdf5".format(os.path.dirname(nz_dict_file), mock_no), "radecz", mode='w')


    if not radeczfile.split('/')[-2] == "mocks_hierarchical":
        # don't save the json if we're working on a mock
        nf = open(nz_dict_file, 'w')
    
        json.dump(nz_dict, nf, sort_keys=True, indent=4, separators=(',', ':\t'))
    
        nf.close()