def getNpartAll(self): """Get number of particles to be read from nbody. Returns ------- Npart : int Number of particles to read in this task's domain """ Npart = 0 if self.nbody.domain.fmt == 'BCCLightcone': partpath = self.nbody.partpath[self.nbody.boxnum] rpmin = int(self.nbody.domain.rmin // 25.) rpmax = int(self.nbody.domain.rmax // 25.) for r in range(rpmin, rpmax + 1): pix_file, peano_idx = self.getFilePixels(r) for p in pix_file: f = '{}/snapshot_Lightcone_{}_{}'.format(partpath, r, p) hdr, idx = read_radial_bin(f) Npart += np.sum(idx[peano_idx]) else: raise(ValueError("Only BCCLightcone reading is currently implemented")) return Npart
def getFilePixels(self, r): """Given a healpix cell and radius for a given nside, figure out which lightcone pixels we need to read Parameters ---------- r : int radial bin to read Returns ------- pix_file : list List of the lightcone file pixels that need to be read peano_idx : list List of the peano indices of particles that need to be read """ partpath = self.nbody.partpath[self.nbody.boxnum] nside = self.nbody.domain.nside pix = self.nbody.domain.pix header_fmt = ['Np', 'nside_index', 'nside_file', 'box_rmin', 'box_rmax', 'void', 'Lbox', 'Mpart', 'Omega_m', 'Omega_l', 'h'] f = '{}/snapshot_Lightcone_{}_0'.format(partpath, r) hdr, idx = read_radial_bin(f) hdr = dict(zip(header_fmt, hdr)) self.part_mass = hdr['Mpart'] * 1e10 if not self.nbody.domain.nest: pix = hp.ring2nest(nside, pix) # this assumes that nside < nside_index which should always be true idxmap = hp.ud_grade(np.arange(12 * nside**2), hdr['nside_index'], order_in='NESTED', order_out='NESTED') # get peano cells corresponding to pix # first get nside=nside_index, nest ordered cells corresponding to pix peano_idx = nest2peano(np.where(idxmap == pix)[0], int(np.log2(hdr['nside_index']))) if nside < hdr['nside_file']: udmap = hp.ud_grade(np.arange(12 * nside**2), hdr['nside_file'], order_in='NESTED', order_out='NESTED') pix_file, = np.where(udmap == pix) elif nside > hdr['nside_file']: udmap = hp.ud_grade(np.arange(12 * hdr['nside_file']**2), nside, order_in='NESTED', order_out='NESTED') pix_file = [udmap[pix]] else: pix_file = [pix] return pix_file, peano_idx
def read_file_downsample(filename, nd=1e-2, read_vel=False): """ Read in particles and downsample. inputs ------ filename - str Name of the file to read f_down - float Factor to downsample the particles by read_vel - bool True if you would also like velocities (for RSD) outputs ------- pos - array (N,3) array of particle positions in comoving Mpc/h vel - array (N,3) array of velocities in km/s """ if read_vel: hdr, idx, pos, vel = read_radial_bin(filename, read_pos=True, read_vel=True) pos = pos.reshape((len(pos) // 3, 3)) vel = vel.reshape((len(vel) // 3, 3)) file_nside = hdr[2] else: hdr, idx, pos = read_radial_bin(filename, read_pos=True) pos = pos.reshape((len(pos) // 3, 3)) file_nside = hdr[2] rbin = int(filename.split('_')[-2]) r = np.sqrt(np.sum(pos**2, axis=1)) idx_down = select_matter(r, 25 * rbin, 25 * (rbin + 1), file_nside, nd) if read_vel: return pos[idx_down], vel[idx_down] else: return pos[idx_down]
def get_lightcone_files(nside, pix, radii, filebase): """ Get the lightcone files corresponding to the current jackknife region """ files = [] for r in radii: r = int(r) # read in default file to get nside of this radial bin hdr, idx = read_radial_bin('{}_{}_{}'.format(filebase, r, 0)) file_nside = hdr[2] if nside == file_nside: file_pix = [pix] elif nside < file_nside: umap = hp.ud_grade(np.arange(12 * nside**2), file_nside, order_in='NESTED', order_out='NESTED') if not hasattr(pix, '__iter__'): pix = [pix] for i, p in enumerate(pix): if i == 0: file_pix, = np.where(umap == p) else: fp = np.where(umap == p) file_pix = np.hstack([file_pix, fp]) else: umap = hp.ud_grade(np.arange(12 * file_nside**2), nside, order_in='NESTED', order_out='NESTED') file_pix = [umap[pix]] files.extend(['{}_{}_{}'.format(filebase, r, p) for p in file_pix]) return files