dsig = np.arccos(np.sin(f1) * np.sin(f2) + \ np.cos(f1) * np.cos(f2) * np.cos(dl)) return dsig bad_xyz = radec2xyz(bad_pts) veto_baum = cKDTree(bad_xyz) rad = np.arange(1.0, 67.0, 5.0) rand_i = 0 for r_i, r in enumerate(rad): spheres = h5_arr(spheresfile, "radecz_{0}".format(str(r_i * 5 + 1))) badvols = 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]) print "rad: ", r, ", sphere: ", i
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()
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()