Esempio n. 1
0
def rand_map(shape,
             wcs,
             ps_lensinput,
             lmax=None,
             maplmax=None,
             dtype=np.float64,
             seed=None,
             oversample=2.0,
             spin=2,
             output="l",
             geodesic=True,
             verbose=False):
    ctype = np.result_type(dtype, 0j)
    # First draw a random lensing field, and use it to compute the undeflected positions
    if verbose: print "Computing observed coordinates"
    obs_pos = enmap.posmap(shape, wcs)
    if verbose: print "Generating alms"
    alm = curvedsky.rand_alm(ps_lensinput, lmax=lmax, seed=seed, dtype=ctype)
    phi_alm, cmb_alm = alm[0], alm[1:]
    # Truncate alm if we want a smoother map. In taylens, it was necessary to truncate
    # to a lower lmax for the map than for phi, to avoid aliasing. The appropriate lmax
    # for the cmb was the one that fits the resolution. FIXME: Can't slice alm this way.
    #if maplmax: cmb_alm = cmb_alm[:,:maplmax]
    del alm
    if "p" in output:
        if verbose: print "Computing phi map"
        phi_map = curvedsky.alm2map(phi_alm,
                                    enmap.zeros(shape[-2:], wcs, dtype=dtype))
    if verbose: print "Computing grad map"
    grad = curvedsky.alm2map(phi_alm,
                             enmap.zeros((2, ) + shape[-2:], wcs, dtype=dtype),
                             deriv=True)
    if verbose: print "Computing alpha map"
    raw_pos = enmap.samewcs(
        offset_by_grad(obs_pos, grad, pol=True, geodesic=geodesic), obs_pos)
    del obs_pos, phi_alm
    if "a" not in output: del grad
    if "u" in output:
        if verbose: print "Computing unlensed map"
        cmb_raw = curvedsky.alm2map(cmb_alm,
                                    enmap.zeros(shape, wcs, dtype=dtype),
                                    spin=spin)
    if verbose: print "Computing lensed map"
    cmb_obs = curvedsky.alm2map_pos(cmb_alm,
                                    raw_pos[:2],
                                    oversample=oversample,
                                    spin=spin)
    if raw_pos.shape[0] > 2 and np.any(raw_pos[2]):
        if verbose: print "Rotating polarization"
        cmb_obs = enmap.rotate_pol(cmb_obs, raw_pos[2])
    del cmb_alm, raw_pos
    # Output in same order as specified in output argument
    res = []
    for c in output:
        if c == "l": res.append(cmb_obs)
        elif c == "u": res.append(cmb_raw)
        elif c == "p": res.append(phi_map)
        elif c == "a": res.append(grad)
    return tuple(res)
Esempio n. 2
0
	ires = np.array([1,1./np.sin(R)])*res/args.supersample
	shape, wi = enmap.geometry(pos=[[np.pi/2-R,-np.pi],[np.pi/2,np.pi]], res=ires, proj="car")
	imap = enmap.zeros((ncomp,)+shape, wi)

# Define SHT for interpolation pixels
with dprint("construct sht"):
	minfo = curvedsky.map2minfo(imap)
	lmax_ideal = np.pi/res
	ps = ps[:,:,:lmax_ideal]
	lmax = ps.shape[-1]
	# We do not need all ms when centered on the pole. To reach 1e-10 relative
	# error up to R, we need mmax approx 9560*R in radians
	mmax = args.mmax or int(R*9560)
	ainfo = sharp.alm_info(lmax, mmax)
	sht = sharp.sht(minfo, ainfo)

with dprint("curvedsky tot"):
	with dprint("rand alm"):
		alm = curvedsky.rand_alm(ps, ainfo=ainfo, seed=1, m_major=False)
	with dprint("alm2map"):
		sht.alm2map(alm[:1], imap[:1].reshape(1,-1))
		if ncomp == 3:
			sht.alm2map(alm[1:3], imap[1:3,:].reshape(2,-1), spin=2)
		del alm
	# Make a test map to see if we can project between these
	with dprint("project"):
		omap = enmap.project(imap, omap.shape, omap.wcs, mode="constant", cval=np.nan)
		del imap

enmap.write_map(args.omap, omap)
Esempio n. 3
0
# Define SHT for interpolation pixels
with dprint("construct sht"):
    minfo = curvedsky.map2minfo(imap)
    lmax_ideal = np.pi / res
    ps = ps[:, :, :lmax_ideal]
    lmax = ps.shape[-1]
    # We do not need all ms when centered on the pole. To reach 1e-10 relative
    # error up to R, we need mmax approx 9560*R in radians
    mmax = args.mmax or int(R * 9560)
    ainfo = sharp.alm_info(lmax, mmax)
    sht = sharp.sht(minfo, ainfo)

with dprint("curvedsky tot"):
    with dprint("rand alm"):
        alm = curvedsky.rand_alm(ps, ainfo=ainfo, seed=1, m_major=False)
    with dprint("alm2map"):
        sht.alm2map(alm[:1], imap[:1].reshape(1, -1))
        if ncomp == 3:
            sht.alm2map(alm[1:3], imap[1:3, :].reshape(2, -1), spin=2)
        del alm
    # Make a test map to see if we can project between these
    with dprint("project"):
        omap = enmap.project(imap,
                             omap.shape,
                             omap.wcs,
                             mode="constant",
                             cval=np.nan)
        del imap

enmap.write_map(args.omap, omap)
Esempio n. 4
0
try:
    os.makedirs(specDir)
except:
    pass

try:
    os.makedirs(plotDir)
except:
    pass

minfo = sharp.map_info_healpix(nside)
ainfo = sharp.alm_info(lmax)
sht = sharp.sht(minfo, ainfo)
# Initialize alm but should not be needed ####################
ps = powspec.read_spectrum(p['theoryPS'])[:ncomp, :ncomp]
alm = curvedsky.rand_alm(ps, ainfo)
##########################################################

mask = healpy.read_map(p['maskfile'])

fields = ['TT', 'EE', 'BB', 'TE', 'EB', 'TB']
alm_all = []
for j in range(nd):
    maps = healpy.read_map(patchDir + '/map_%d.fits' % j, field=[0, 1, 2])
    T = maps[0] * mask
    Q = maps[1] * mask
    U = maps[2] * mask

    sht.map2alm(T, alm[0])
    sht.map2alm((Q, U), alm[1:], spin=2)
    del maps
Esempio n. 5
0
def rand_map_curved(shape,
                    wcs,
                    ps,
                    lmax=None,
                    lens=True,
                    aberrate=True,
                    beta=None,
                    dir=None,
                    seed=None,
                    dtype=None,
                    verbose=False,
                    recenter=False):
    """Simulate a random curved-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 alms"
    alm = curvedsky.rand_alm(ps, lmax=lmax, seed=seed, dtype=ctype)
    # Before any corrections are applied, the pixel positions map
    # directly to the raw cmb positions, and there is no induced polarization
    # rotation or amplitude modulation.
    if verbose: print "Computing observed coordinates"
    pos = enmap.posmap(shape, wcs)
    ang = enmap.zeros(shape[-2:], wcs)
    amp = enmap.zeros(shape[-2:], wcs) + 1
    # Lensing remaps positions
    if lens:
        phi_alm, alm = alm[0], alm[1:]
        if verbose: print "Computing lensing gradient"
        grad = curvedsky.alm2map(phi_alm,
                                 enmap.zeros((2, ) + shape[-2:],
                                             wcs,
                                             dtype=dtype),
                                 deriv=True)
        del phi_alm
        if verbose: print "Applying lensing gradient"
        pos = enmap.samewcs(
            lensing.offset_by_grad(pos, grad, pol=True, geodesic=True), pos)
        ang += pos[2]
    # Aberration remaps positions and modulates amplitudes
    if aberrate:
        if verbose: print "Computing aberration"
        pos = enmap.samewcs(
            aberration.remap(pos[1::-1], dir=dir, beta=beta,
                             recenter=recenter), pos)
        ang += pos[2]
        amp *= pos[3]
        pos = pos[1::-1]
    # Simulate the sky at the observed locations
    if verbose: print "Simulating sky signal"
    map = curvedsky.alm2map_pos(alm, pos)
    del alm, pos
    # Apply polarization rotation
    if verbose: print "Applying polarization rotation"
    map = enmap.rotate_pol(map, ang)
    # and modulation
    if verbose: print "Applying mouldation"
    map *= amp
    return map
Esempio n. 6
0
mg = enmap.MapGen(shape,wcs,p2d.reshape(1,1,modlmap.shape[0],modlmap.shape[1]))
imap = mg.get_map()

io.quickPlot2d(imap,io.dout_dir+"cmb.png")
# imap2 = mg.get_map()
# io.quickPlot2d(imap2,io.dout_dir+"cmb2.png")
dtype = np.complex128
rtype = np.zeros([0],dtype=dtype).real.dtype

print("alms...")
alm = curvedsky.map2alm(imap,lmax=4000)
powfac = 0.5
ps_data = enmap.multi_pow(alm*alm.conj()[None,None],powfac).astype(rtype)
print(alm)
print((alm.shape))

pspec = np.ones((8000))
pspec[:200] = 0.
pspec[4000:] = 0.
rand_alm,ainfo = curvedsky.rand_alm(ps=pspec,lmax=4000,return_ainfo=True)
#rand_map = curvedsky.rand_map(shape,wcs,pspec)
#rand_alm = curvedsky.map2alm(rand_map,lmax=4000)
ps_alm = enmap.multi_pow(rand_alm*rand_alm.conj()[None,None],powfac).astype(rtype)

new_sim_alm = rand_alm *np.nan_to_num(ps_data/ps_alm)

new_sim = curvedsky.alm2map(new_sim_alm,imap)#,ainfo=ainfo)
#new_sim = curvedsky.alm2map(alm,imap)#,ainfo=ainfo)
io.quickPlot2d(imap,io.dout_dir+"cmbsim.png")