def rand_map_flat(shape, wcs, ps, lmax=None, lens=True, aberrate=True, beta=None, dir=None, seed=None, dtype=None, verbose=False, recenter=False, pad=0): """Simulate a random flat-sky map. The input spectrum should be [{phi,T,E,B},{phi,T,E,b},nl] of lens is True, and just [{T,E,B},{T,E,B},nl] otherwise.""" if dtype is None: dtype = np.float64 if dir is None: dir = aberration.dir_equ if beta is None: beta = aberration.beta ctype = np.result_type(dtype, 0j) if verbose: print "Generating unlensed cmb" # No position calculation necessary if we're not lensing or aberrating. if not lens and not aberrate: return enmap.rand_map(shape, wcs, ps, seed=seed) # Otherwise we must deal with various displacements if aberrate: pad += np.pi * beta * 1.2 pad_pix = int(pad / enmap.pixsize(shape, wcs)**0.5) if pad_pix > 0: if verbose: print "Padding" template = enmap.zeros(shape, wcs, np.int16) template, pslice = enmap.pad(template, pad_pix, return_slice=True) pshape, pwcs = template.shape, template.wcs else: pshape, pwcs = shape, wcs # Simulate (padded) lensing map if lens: maps = enmap.rand_map((ps.shape[0], ) + pshape[-2:], pwcs, ps) phi, unlensed = maps[0], maps[1:] if verbose: print "Lensing" m = lensing.lens_map_flat(unlensed, phi) else: m = enmap.rand_map((ps.shape[0], ) + pshape[-2:], pwcs, ps) # Then handle aberration if necessary if aberrate: if verbose: print "Computing aberration displacement" pos = m.posmap() pos = enmap.samewcs( aberration.remap(pos[1::-1], dir=dir, beta=beta, recenter=recenter), pos) amp = pos[3] pos = pos[1::-1] if verbose: print "Interpolating aberration" m = enmap.samewcs(m.at(pos, mask_nan=False), m) if verbose: print "Applying modulation" m *= amp if pad_pix > 0: if verbose: print "Unpadding" m = m[pslice] return m
ps = powspec.read_camb_full_lens(args.powspec).astype(dtype) if args.beam: raise NotImplementedError("Beam not supported for lensed sims yet") if args.geometry == "curved": m, = lensing.rand_map(shape, wcs, ps, lmax=lmax, maplmax=maplmax, seed=(seed, i), verbose=verbose, dtype=dtype) else: maps = enmap.rand_map((shape[0] + 1, ) + shape[1:], wcs, ps) phi, unlensed = maps[0], maps[1:] m = lensing.lens_map_flat(unlensed, phi) else: ps = powspec.read_spectrum(args.powspec).astype(type) beam = make_beam(ps.shape[-1], args.beam * utils.arcmin * utils.fwhm) ps *= beam if args.geometry == "curved": m = curvedsky.rand_map(shape, wcs, ps, lmax=lmax, seed=(seed, i), method=args.method, direct=args.direct, dtype=dtype, verbose=verbose) else:
enmap.write_map("%s/%02d_%s_tqu.hdf" % (dir,i+1,desc), tqu) enmap.write_map("%s/%02d_%s_teb.hdf" % (dir,i+1,desc), teb) cl_scal, cl_phi = powspec.read_camb_scalar(args.scal_cls, expand=True) cl_tens = powspec.read_spectrum(args.tens_cls, expand="diag") cl_cmb = cl_scal+cl_tens np.random.seed(args.seed) res = bunch.Bunch(tqu=[],teb=[],desc=[]) # 1. Start with last scattering cmb = enmap.rand_map(shape, wcs, cl_cmb) m = cmb; add(res, m, "cmb") # 2. Add lensing phi = enmap.rand_map(shape[-2:], wcs, cl_phi) lcmb = lensing.lens_map_flat(cmb, phi) m = lcmb; add(res, m, "lens") # 3. Add point sources pinfo = parse_points(args.ptsrc) pmap = sim_points(shape, wcs, pinfo) m += pmap; add(res, m, "ptsrc") # 4. Add sz clusters zinfo = parse_points(args.sz) zmap = sim_points(shape, wcs, zinfo) m += zmap; add(res, m, "sz") # 5. Add faraday rotation (probably invisible) m = enmap.rotate_pol(m, args.faraday) add(res, m, "faraday") # 6. Add dust (as a representative galactic foreground) cl_dust = parse_powlaw(args.dust) dust = enmap.rand_map(shape, wcs, cl_dust)
verbose = args.verbose - args.quiet > 0 def make_beam(nl, bsize): l = np.arange(nl) return np.exp(-l*(l+1)*bsize**2) for i in range(comm.rank, args.nsim, comm.size): if args.lensed: ps = powspec.read_camb_full_lens(args.powspec).astype(dtype) if args.beam: raise NotImplementedError("Beam not supported for lensed sims yet") if args.geometry == "curved": m, = lensing.rand_map(shape, wcs, ps, lmax=lmax, maplmax=maplmax, seed=(seed,i), verbose=verbose, dtype=dtype) else: maps = enmap.rand_map((shape[0]+1,)+shape[1:], wcs, ps) phi, unlensed = maps[0], maps[1:] m = lensing.lens_map_flat(unlensed, phi) else: ps = powspec.read_spectrum(args.powspec).astype(type) beam = make_beam(ps.shape[-1], args.beam*utils.arcmin*utils.fwhm) ps *= beam if args.geometry == "curved": m = curvedsky.rand_map(shape, wcs, ps, lmax=lmax, seed=(seed,i), method=args.method, direct=args.direct, dtype=dtype, verbose=verbose) else: m = enmap.rand_map(shape, wcs, ps) if args.nsim == 1: if verbose: print "Writing %s" % args.ofile enmap.write_map(args.ofile, m) else: if verbose: print "Writing %s" % (args.ofile % i) enmap.write_map(args.ofile % i, m)
box = np.array([[-w,-w],[w,w]])/2 wcs = enmap.create_wcs(shape, box) cl_scal, cl_phi = powspec.read_camb_scalar(args.scal_cls, expand=True) cl_tens = powspec.read_spectrum(args.tens_cls, expand="diag") ## Generate scalar-only lensed and unlensed map #m_scal_u, m_scal_l = lensing.rand_map(shape, wcs, cl_scal, cl_phi, seed=args.seed, output="ul", verbose=args.verbose) ## Generate tensor-only lensed and unlensed map #m_tens_u, m_tens_l = lensing.rand_map(shape, wcs, cl_tens, cl_phi, seed=args.seed, output="ul", verbose=args.verbose) np.random.seed(args.seed) phi = enmap.rand_map(shape[-2:], wcs, cl_phi) m_scal_u = enmap.rand_map(shape, wcs, cl_scal) m_tens_u = enmap.rand_map(shape, wcs, cl_tens) m_scal_l = lensing.lens_map_flat(m_scal_u, phi) m_tens_l = lensing.lens_map_flat(m_tens_u, phi) # And the sums m_tot_u = m_scal_u + m_tens_u m_tot_l = m_scal_l + m_tens_l # Convert from TQU to TEB and downgrade def to_eb(m): return enmap.ifft(enmap.map2harm(m)).real m_scal_u, m_scal_l, m_tens_u, m_tens_l, m_tot_u, m_tot_l = [enmap.downgrade(to_eb(i),os) for i in [m_scal_u, m_scal_l, m_tens_u, m_tens_l, m_tot_u, m_tot_l]] # And output utils.mkdir(args.odir) enmap.write_map(args.odir + "/map_scalar.fits", m_scal_u) enmap.write_map(args.odir + "/map_tensor.fits", m_tens_u) enmap.write_map(args.odir + "/map_tot.fits", m_tot_u)