Exemplo n.º 1
0
def get_synth_maps():
    ulm_t1 = np.sqrt((np.random.rand(len(ellArr)) - 0.5 +
                      1j*(np.random.rand(len(ellArr)) - 0.5)) *
                     (lmax**2 - (ellArr-50)**2))
    vlm_t1 = np.sqrt((np.random.rand(len(ellArr)) - 0.5 +
                      1j*(np.random.rand(len(ellArr)) - 0.5)) *
                     (lmax**2*1.5 - (ellArr-50)**2))
    wlm_t1 = np.sqrt((np.random.rand(len(ellArr)) - 0.5 +
                      1j*(np.random.rand(len(ellArr)) - 0.5)) *
                     (lmax**2*1.7 - (ellArr-50)**2))

    r_t1 = hp.alm2map(ulm_t1, NSIDE)
    ulm_t1_imag = hp.map2alm(r_t1.imag)
    ulm_t2 = ulm_t1 - 1j*ulm_t1_imag

    r1map = hp.alm2map(ulm_t2, NSIDE)
    r_map = hp.alm2map(hp.map2alm(r1map), NSIDE)

    hlmp_t1 = (vlm_t1 + 1j*wlm_t1)/np.sqrt(2)
    hlmm_t1 = (vlm_t1 - 1j*wlm_t1)/np.sqrt(2)
    t1map, p1map = hp.alm2map_spin((hlmp_t1, hlmm_t1), NSIDE, 1, lmax)

    hlmp_t1_imag, hlmm_t1_imag = hp.map2alm_spin((t1map.imag, p1map.imag), 1)
    hlmp = hlmp_t1 - 1j*hlmp_t1_imag
    hlmm = hlmm_t1 - 1j*hlmm_t1_imag

    t1map, p1map = hp.alm2map_spin((hlmm, hlmp), NSIDE, 1, lmax)
    h1map, h2map = hp.alm2map_spin(hp.map2alm_spin((t1map, p1map), 1), NSIDE, 1, lmax)
    return r_map, h1map, h2map
Exemplo n.º 2
0
def alm2map_spin_der1(gclm, nside, spin, zbounds=(-1.0, 1.0), ret_slice=None):
    """Returns spin-s transform '_{s}d' of alm,
    together with d/dtheta _{s}d and 1/sin tht d/dphi _{s}d.

    This crude version has three calls to spin-weight harmonics alm2map_spin.

    Author: Julien Carron ([email protected])
    """
    assert spin > 0, spin
    assert hp.Alm.getlmax(gclm[0].size) == hp.Alm.getlmax(gclm[1].size)
    lmax = hp.Alm.getlmax(gclm[0].size)
    zbounds = np.sort(np.array(zbounds))
    # shape (2, 12 * nside ** 2),
    # first entry = real part, second entry imaginary part.
    _sd = np.array(hp.alm2map_spin(gclm, nside, spin, lmax))
    if spin > 1:
        _gclm = [
            hp.almxfl(gclm[0], get_alpha_lower(spin, lmax)),
            hp.almxfl(gclm[1], get_alpha_lower(spin, lmax)),
        ]
        _sm1d = np.array(hp.alm2map_spin(_gclm, nside, spin - 1, lmax))
    else:
        _sm1d = -np.array(
            [
                hp.alm2map(
                    hp.almxfl(gclm[0], get_alpha_lower(spin, lmax)),
                    nside,
                    verbose=False,
                ),
                hp.alm2map(
                    hp.almxfl(gclm[1], get_alpha_lower(spin, lmax)),
                    nside,
                    verbose=False,
                ),
            ]
        )

    _gclm = [
        hp.almxfl(gclm[0], get_alpha_raise(spin, lmax)),
        hp.almxfl(gclm[1], get_alpha_raise(spin, lmax)),
    ]
    _sp1d = np.array(hp.alm2map_spin(_gclm, nside, spin + 1, lmax))

    d_dth = -0.5 * (_sp1d + _sm1d)

    d_dphi_sin0 = 0.5 * np.array([-_sp1d[1] + _sm1d[1], _sp1d[0] - _sm1d[0]])
    for iring in range(4 * nside - 1):
        startpix, nphi, kphi0, cth, sth = get_healpix_ring_pixel_layout(nside, iring)
        if zbounds[0] <= cth <= zbounds[1]:
            slic = slice(startpix, startpix + nphi)
            d_dphi_sin0[1, slic] -= spin * (cth / sth) * _sd[0, slic]
            d_dphi_sin0[0, slic] += spin * (cth / sth) * _sd[1, slic]
    if ret_slice is not None:
        return _sd[:, ret_slice], d_dth[:, ret_slice], d_dphi_sin0[:, ret_slice]

    return _sd, d_dth, d_dphi_sin0
Exemplo n.º 3
0
    def get_gpmap(self, idx, spin, k=None, xfilt=None):
        """
        \sum_{lm} (Elm +- iBlm) sqrt(l+2 (l-1)) _1 Ylm(n).
                                sqrt(l-2 (l+3)) _3 Ylm(n).
        Output is list with real and imaginary part of the spin 1 or 3 transforms.
        """
        assert k in ['p_p', 'p'], k
        assert spin in [1, 3]
        if xfilt is not None:
            assert isinstance(xfilt, dict) and 'e' in xfilt.keys(
            ) and 'b' in xfilt.keys() and 't' in xfilt.keys()

        need_p = (xfilt is None) or (np.any(xfilt['e']) or np.any(xfilt['b']))
        Glm, Clm = (self.ivfs.get_sim_emliklm(idx),
                    self.ivfs.get_sim_bmliklm(idx)) if need_p else (0., 0.)
        if xfilt is not None and need_p:
            hp.almxfl(Glm, xfilt['e'], inplace=True)
            hp.almxfl(Clm, xfilt['b'], inplace=True)
        if k == 'p':
            need_t = (xfilt is None) or np.any(xfilt['t'])
            G_tlm = hp.almxfl(self.ivfs.get_sim_tlm(idx),
                              self.clte) if need_t else 0.
            if xfilt is not None and need_t:
                hp.almxfl(G_tlm, xfilt['t'], inplace=True)
            Glm = Glm + G_tlm
            del G_tlm
        if np.any(Glm) or np.any(Clm):
            lmax = hp.Alm.getlmax(Glm.size)
            if spin == 1:
                fl = np.arange(2, lmax + 3, dtype=float) * (np.arange(
                    -1, lmax))
            elif spin == 3:
                fl = np.arange(-2, lmax - 1, dtype=float) * (np.arange(
                    3, lmax + 4))
            else:
                assert 0
            fl[:spin] *= 0.
            fl = np.sqrt(fl)
            hp.almxfl(Glm, fl, inplace=True)
            if np.any(Clm):
                hp.almxfl(Clm, fl, inplace=True)
            if np.isscalar(Clm):
                return hp.alm2map_spin([Glm, Glm * 0.], self.nside, spin, lmax)
            else:
                return hp.alm2map_spin([Glm, Clm], self.nside, spin, lmax)
        else:
            return np.zeros(hp.nside2npix(self.nside),
                            dtype=float), np.zeros(hp.nside2npix(self.nside),
                                                   dtype=float)
Exemplo n.º 4
0
def simulate_cmb(nside=2048, lmax=3000,
                 frequency=100,smear=False,
                 nomap = False, beam=None, beamP=None,
                 save=False, filename='testcmb.fits',
                 cl_file='bf_base_cmbonly_plikHMv18_TT_lowTEB_lmax4000.minimum.theory_cl'):
        
    ls, cltt, clte, clee, clbb = get_theory_cls(lmax=lmax, cl_file=cl_file)
 
    Tlm, Elm, Blm = hp.synalm( (cltt, clee, clbb, clte), new=True, lmax=lmax)

    
    if smear:
        if (beam is None) or (beamP is None) :
            hdulist = fits.open(data_path + 'HFI_RIMO_Beams-100pc_R2.00.fits')
            beam = hdulist[beam_index['{}'.format(frequency)]].data.NOMINAL[0][:lmax+1]
            beamP = hdulist[beam_index['{}P'.format(frequency)]].data.NOMINAL[0][:lmax+1]
        hp.sphtfunc.almxfl(Tlm, beam, inplace=True)
        hp.sphtfunc.almxfl(Elm, beamP, inplace=True)
        hp.sphtfunc.almxfl(Blm, beamP, inplace=True)

    if nomap:
        return Tlm,Elm,Blm
    
    Tmap = hp.alm2map( Tlm, nside )
    Qmap, Umap = hp.alm2map_spin( (Elm, Blm), nside, 2, lmax=lmax)

    if save:
        hp.write_map([Tmap,Qmap,Umap],data_path + filename)
    return Tmap, Qmap, Umap
Exemplo n.º 5
0
 def test_alm2map_spin_precomputed(self):
     """ compare alm2map_spin outputs to some precomputed results for spin=1,2,3 """
     
     for spin in [1,2,3]:
         rmap, imap = hp.alm2map_spin( [galm, calm], nside_precomputed, spin, lmax_precomputed )
         np.testing.assert_array_almost_equal( rmap, maps[spin].real, decimal=7)
         np.testing.assert_array_almost_equal( imap, maps[spin].imag, decimal=7)
Exemplo n.º 6
0
    def get_gpmap(self, idx, spin, k=None, xfilt=None):
        """
        \sum_{lm} (Elm +- iBlm) sqrt(l+2 (l-1)) _1 Ylm(n).
                                sqrt(l-2 (l+3)) _3 Ylm(n).
        Output is list with real and imaginary part of the spin 1 or 3 transforms.
        """
        assert spin in [1, 3]
        assert xfilt is None, 'not implemented'
        Glm = self.ivfs.get_sim_emliklm(idx)
        Clm = self.ivfs.get_sim_bmliklm(idx)

        assert Glm.size == Clm.size, (Clm.size, Clm.size)
        lmax = hp.Alm.getlmax(Glm.size)
        if spin == 1:
            fl = np.arange(2, lmax + 3, dtype=float) * (np.arange(-1, lmax))
        elif spin == 3:
            fl = np.arange(-2, lmax - 1, dtype=float) * (np.arange(
                3, lmax + 4))
        else:
            assert 0
        fl[:spin] *= 0.
        fl = np.sqrt(fl)
        hp.almxfl(Glm, fl, inplace=True)
        hp.almxfl(Clm, fl, inplace=True)
        return hp.alm2map_spin([Glm, Clm], self.nside, spin, lmax)
Exemplo n.º 7
0
def eb2g(ke, kb):
    nside = gnside(ke)
    lmax = nside * 3 - 1
    ae = hp.map2alm(ke, 1, pol=False)
    ab = hp.map2alm(kb, 1, pol=False)
    (g1, g2) = hp.alm2map_spin((ae, ab), nside, 2, lmax)
    return g1, g2
Exemplo n.º 8
0
def test_t():
    lmax = 200
    nside = 256
    cls_unl = np.ones(lmax + 1, dtype=float)
    tunl = hp.synalm(cls_unl, new=True)
    dlm = np.zeros_like(tunl)
    hp.almxfl(dlm,
              np.sqrt(
                  np.arange(lmax + 1) * np.arange(1, lmax + 2, dtype=float)),
              inplace=True)
    T = hp.alm2map(tunl, nside)
    T1 = lensing.alm2lenmap(tunl, [dlm, None],
                            nside,
                            verbose=True,
                            nband=8,
                            facres=-2)
    assert np.max(np.abs(T - T1)) / np.std(T) < 1e-5
    d1Re, d1Im = hp.alm2map_spin([dlm, np.zeros_like(dlm)], nside, 1, lmax)
    T2 = lensing.alm2lenmap(tunl, [d1Re, d1Im],
                            nside,
                            verbose=True,
                            nband=8,
                            facres=-2)
    assert np.max(np.abs(T - T2)) / np.std(T) < 1e-5
    T3 = lensing.alm2lenmap(tunl, [dlm, dlm.copy()],
                            nside,
                            verbose=True,
                            nband=8,
                            facres=-2)
    assert np.all(T2 == T3)
Exemplo n.º 9
0
def alm2map_spin(gclm, nside, spin, lmax, mmax=None):
    assert spin >= 0, spin
    assert len(gclm) == 2, len(gclm)
    if spin > 0:
        return hp.alm2map_spin(gclm, nside, spin, lmax, mmax=mmax)
    elif spin == 0:
        return hp.alm2map(-gclm[0], nside, lmax=lmax, mmax=mmax), 0.
Exemplo n.º 10
0
 def get_gtmap(self, idx, k=None, xfilt=None):
     """
     \sum_{lm} MAP_talm sqrt(l (l + 1)) _1 Ylm(n).
     Spin 1 transform with zero curl comp.
     Recall healpy sign convention for which Glm = - Tlm.
     Output is list with real and imaginary part of the spin 1 transform.
     """
     assert k in ['ptt', 'p'], k
     if xfilt is not None:
         assert isinstance(xfilt, dict) and 't' in xfilt.keys()
     mliktlm = self.ivfs.get_sim_tmliklm(idx)
     if xfilt is not None:
         hp.almxfl(mliktlm, xfilt['t'], inplace=True)
     if k == 'p':
         telm = hp.almxfl(self.ivfs.get_sim_elm(idx), self.clte)
         if xfilt is not None:
             assert 'e' in xfilt.keys()
             hp.almxfl(telm, xfilt['e'], inplace=True)
         mliktlm += telm
         del telm
     lmax = hp.Alm.getlmax(mliktlm.size)
     Glm = hp.almxfl(
         mliktlm, -np.sqrt(
             np.arange(lmax + 1, dtype=float) * (np.arange(1, lmax + 2))))
     return hp.alm2map_spin([Glm, np.zeros_like(Glm)], self.nside, 1, lmax)
Exemplo n.º 11
0
    def test_spin0(self):
        m1 = hp.alm2map(self.almg, self.nside, self.lmax)
        m2_r, m2_i = hp.alm2map_spin([self.almg, 0. * self.almg], self.nside,
                                     0, self.lmax)

        np.testing.assert_array_almost_equal(m1, m2_r, decimal=8)
        np.testing.assert_array_almost_equal(m1 * 0., m2_i, decimal=8)
Exemplo n.º 12
0
 def test_alm2map_spin_precomputed(self):
     """ compare alm2map_spin outputs to some precomputed results for spin=1,2,3 """
     
     for spin in [1,2,3]:
         rmap, imap = hp.alm2map_spin( [galm, calm], nside_precomputed, spin, lmax_precomputed )
         np.testing.assert_array_almost_equal( rmap, maps[spin].real, decimal=7)
         np.testing.assert_array_almost_equal( imap, maps[spin].imag, decimal=7)
Exemplo n.º 13
0
    def get_pmap(self, idx):
        """Real-space Wiener filtered polarization.

        """
        Glm = self.ivfs.get_sim_emliklm(idx)
        Clm = self.ivfs.get_sim_bmliklm(idx)
        return hp.alm2map_spin([Glm, Clm], self.nside, 2, hp.Alm.getlmax(Glm.size))
Exemplo n.º 14
0
 def get_gpmap(self, idx, spin, k=None, xfilt=None):
     """
     \sum_{lm} (Elm +- iBlm) sqrt(l+2 (l-1)) _1 Ylm(n).
                             sqrt(l-2 (l+3)) _3 Ylm(n).
     Output is list with real and imaginary part of the spin 1 or 3 transforms.
     """
     assert k in ['p_p', 'p'], k
     assert spin in [1, 3]
     if xfilt is not None:
         assert isinstance(xfilt, dict) and 'e' in xfilt.keys() and 'b' in xfilt.keys() and 't' in xfilt.keys()
     Glm = self.ivfs.get_sim_emliklm(idx)
     Clm = self.ivfs.get_sim_bmliklm(idx)
     if xfilt is not None:
         hp.almxfl(Glm, xfilt['e'], inplace=True)
         hp.almxfl(Clm, xfilt['b'], inplace=True)
     if k == 'p':
         G_tlm = hp.almxfl(self.ivfs.get_sim_tlm(idx), self.clte)
         if xfilt is not None:
             hp.almxfl(G_tlm, xfilt['t'], inplace=True)
         Glm += G_tlm
         del G_tlm
     assert Glm.size == Clm.size, (Clm.size, Clm.size)
     lmax = hp.Alm.getlmax(Glm.size)
     if spin == 1:
         fl = np.arange(2, lmax + 3, dtype=float) * (np.arange(-1, lmax))
     elif spin == 3:
         fl = np.arange(-2, lmax - 1, dtype=float) * (np.arange(3, lmax + 4))
     else:
         assert 0
     fl[:spin] *= 0.
     fl = np.sqrt(fl)
     hp.almxfl(Glm, fl, inplace=True)
     hp.almxfl(Clm, fl, inplace=True)
     return hp.alm2map_spin([Glm, Clm], self.nside, spin, lmax)
Exemplo n.º 15
0
def get_def_hp(nside,dlm,th1,th2):
    # FIXME band only calc. with vtm ?
    pix = hp.query_strip(nside, max(th1, 0.), min(np.pi, th2), inclusive=True)
    Red, Imd = hp.alm2map_spin([dlm, np.zeros_like(dlm)], nside, 1, hp.Alm.getlmax(dlm.size))
    Red = Red[pix]
    Imd = Imd[pix]
    return _buildangles(hp.pix2ang(nside, pix),Red[pix],Imd[pix])
Exemplo n.º 16
0
 def get_sim_qumap(self,idx):
     elm = self.sims_cmb_len.get_sim_elm(idx)
     hp.almxfl(elm,self.cl_ptransf,inplace=True)
     blm = self.sims_cmb_len.get_sim_blm(idx)
     hp.almxfl(blm, self.cl_ptransf, inplace=True)
     Q,U = hp.alm2map_spin([elm,blm], self.nside, 2,hp.Alm.getlmax(elm.size))
     del elm,blm
     return [Q + self.get_sim_qnoise(idx),U + self.get_sim_unoise(idx)]
Exemplo n.º 17
0
    def get_pmap(self, idx, joint=False):
        """Real-space Wiener filtered polarization.

        """
        Glm = self.ivfs.get_sim_emliklm(idx)
        Clm = self.ivfs.get_sim_bmliklm(idx)
        if joint:
            Glm += hp.almxfl(self.ivfs.get_sim_tlm(idx), self.clte)
        return hp.alm2map_spin([Glm, Clm], self.nside, 2, hp.Alm.getlmax(Glm.size))
Exemplo n.º 18
0
 def get_sim_pmap(self, idx):
     elm = hp.almxfl(cmb_len_ffp10.get_sim_elm(idx), self.transf)
     blm = hp.almxfl(cmb_len_ffp10.get_sim_blm(idx), self.transf)
     Q, U = hp.alm2map_spin((elm, blm), self.nside, 2, hp.Alm.getlmax(elm.size))
     del elm, blm
     nlevp_pix = self.nlevp / np.sqrt(hp.nside2pixarea(self.nside, degrees=True)) / 60.
     Q += self.pix_libphas.get_sim(idx, idf=1) * nlevp_pix
     U += self.pix_libphas.get_sim(idx, idf=2) * nlevp_pix
     return Q, U
Exemplo n.º 19
0
    def test_der1(self):
        """ compare output of alm2map_der1 with the equivalent spin-1 transform using alm2map_spin """
        
        m, dt_der1, dp_der1 = hp.alm2map_der1( self.almg, self.nside )

        alm_spin            = hp.almxfl( self.almg, np.array( [np.sqrt(l*(l+1.)) for l in six.moves.xrange(0,self.lmax+1)] ), inplace=False )
        dt_spin, dp_spin    = hp.alm2map_spin( [alm_spin, alm_spin*0.], self.nside, 1, self.lmax )

        np.testing.assert_array_almost_equal( dt_der1, dt_spin, decimal=8)
        np.testing.assert_array_almost_equal( dp_der1, dp_spin, decimal=8)
Exemplo n.º 20
0
 def get_irespmap(self, idx, xfilt=None):
     reselm = self.ivfs.get_sim_elm(idx)
     resblm = self.ivfs.get_sim_blm(idx)
     assert hp.Alm.getlmax(reselm.size) == hp.Alm.getlmax(resblm.size)
     if xfilt is not None:
         assert isinstance(xfilt, dict) and 'e' in xfilt.keys() and 'b' in xfilt.keys()
         hp.almxfl(reselm, xfilt['e'], inplace=True)
         hp.almxfl(resblm, xfilt['b'], inplace=True)
     fac = 0.5
     return hp.alm2map_spin([reselm * fac, resblm * fac], self.nside, 2, hp.Alm.getlmax(reselm.size))
Exemplo n.º 21
0
    def test_der1(self):
        """ compare output of alm2map_der1 with the equivalent spin-1 transform using alm2map_spin """
        
        m, dt_der1, dp_der1 = hp.alm2map_der1( self.almg, self.nside )

        alm_spin            = hp.almxfl( self.almg, np.array( [np.sqrt(l*(l+1.)) for l in xrange(0,self.lmax+1)] ), inplace=False )
        dt_spin, dp_spin    = hp.alm2map_spin( [alm_spin, alm_spin*0.], self.nside, 1, self.lmax )

        np.testing.assert_array_almost_equal( dt_der1, dt_spin, decimal=8)
        np.testing.assert_array_almost_equal( dp_der1, dp_spin, decimal=8)
Exemplo n.º 22
0
def test_pol():
    lmax = 200
    nside = 256
    facres = -1
    cls_unl = np.ones(lmax + 1, dtype=float)
    tunl = hp.synalm(cls_unl, new=True)
    dlm = np.zeros_like(tunl)
    hp.almxfl(dlm,
              np.sqrt(
                  np.arange(lmax + 1) * np.arange(1, lmax + 2, dtype=float)),
              inplace=True)
    Q, U = hp.alm2map_spin([tunl, np.zeros_like(tunl)], nside, 2, lmax)
    Q1, U1 = lensing.alm2lenmap_spin([tunl, None], [dlm, None],
                                     nside,
                                     2,
                                     verbose=True,
                                     nband=8,
                                     facres=facres)

    assert np.max(np.abs(Q - Q1)) / np.std(Q) < 1e-5, np.max(
        np.abs(Q - Q1)) / np.std(Q)
    assert np.max(np.abs(U - U1)) / np.std(U) < 1e-5, np.max(
        np.abs(U - U1)) / np.std(U)

    d1Re, d1Im = hp.alm2map_spin([dlm, np.zeros_like(dlm)], nside, 1, lmax)
    Q2, U2 = lensing.alm2lenmap_spin([tunl, None], [d1Re, d1Im],
                                     nside,
                                     2,
                                     verbose=True,
                                     nband=8,
                                     facres=facres)
    assert np.allclose(Q2, Q1, rtol=1e-10)
    assert np.allclose(U2, U1, rtol=1e-10)

    Q3, U3 = lensing.alm2lenmap_spin([tunl, tunl * 0.], [d1Re, d1Im],
                                     nside,
                                     2,
                                     verbose=True,
                                     nband=8,
                                     facres=facres)
    assert np.all(Q3 == Q2)
    assert np.all(U3 == U2)
Exemplo n.º 23
0
 def test_forward_backward_spin(self):
     """ compare map2alm_spin outputs to alm2map_spin inputs. tolerances are very loose. """
     for spin in [1,2,3]:
         tcl = np.ones(self.lmax+1); tcl[0:spin] = 0.
         almg = hp.almxfl(self.almg, tcl, inplace=False)
         almc = hp.almxfl(self.almc, tcl, inplace=False)
         
         rmap, imap = hp.alm2map_spin( [almg, almc], self.nside, spin, self.lmax )
         tglm, tclm = hp.map2alm_spin( [rmap, imap], spin, self.lmax )
         np.testing.assert_allclose( almg, tglm, rtol=1.e-2, atol=1.e-2)
         np.testing.assert_allclose( almc, tclm, rtol=1.e-2, atol=1.e-2)
Exemplo n.º 24
0
 def alm2map(self,alm,spin,ncomp,mlmax):
     if self.hpix:
         if spin!=0: 
             res = hp.alm2map_spin(alm,nside=self.nside,spin=spin,lmax=mlmax)
             return res
         else: 
             # complex64 not supported here
             return hp.alm2map(alm.astype(np.complex128),nside=self.nside,pol=False)[None]
     else:
         omap = enmap.empty((ncomp,)+self.shape,self.wcs,dtype=self.dtype)
         return cs.alm2map(alm,omap,spin=spin)
Exemplo n.º 25
0
 def test_forward_backward_spin(self):
     """ compare map2alm_spin outputs to alm2map_spin inputs. tolerances are very loose. """
     for spin in [1,2,3]:
         tcl = np.ones(self.lmax+1); tcl[0:spin] = 0.
         almg = hp.almxfl(self.almg, tcl, inplace=False)
         almc = hp.almxfl(self.almc, tcl, inplace=False)
         
         rmap, imap = hp.alm2map_spin( [almg, almc], self.nside, spin, self.lmax )
         tglm, tclm = hp.map2alm_spin( [rmap, imap], spin, self.lmax )
         np.testing.assert_allclose( almg, tglm, rtol=1.e-2, atol=1.e-2)
         np.testing.assert_allclose( almc, tclm, rtol=1.e-2, atol=1.e-2)
Exemplo n.º 26
0
 def get_gtmap(self, idx, k=None, xfilt=None):
     """
     \sum_{lm} MAP_talm sqrt(l (l + 1)) _1 Ylm(n).
     Spin 1 transform with zero curl comp.
     Recall healpy sign convention for which Glm = - Tlm.
     Output is list with real and imaginary part of the spin 1 transform.
     """
     assert xfilt is None, 'not implemented'
     mliktlm = self.ivfs.get_sim_tmliklm(idx)
     lmax = hp.Alm.getlmax(mliktlm.size)
     Glm = hp.almxfl(mliktlm, -np.sqrt(np.arange(lmax + 1, dtype=float) * (np.arange(1, lmax + 2))))
     return hp.alm2map_spin([Glm, np.zeros_like(Glm)], self.nside, 1, lmax)
Exemplo n.º 27
0
def window2vecttens(window_scal, mask=None, lmax=None, mmax=None):
    '''
    Calculates the vector/tensor windows needed in the pure Cl
    calculation

    Notes
    -----
    We calculate both straight from the wlms of the scalar window because
    calculating the spin-2 from the spherical harmonic transform of a
    spin-1 window will result in larger errors.

    If you don't want zero pixels to be masked out input a full sky mask.
    '''

    nside = H.npix2nside(len(window_scal))

    if mask is None:
        mask = np.ones_like(window_scal)
        #mask[window_scal == 0] = 0
    else:
        window_scal = window_scal * mask

    wlm = H.map2alm(window_scal, lmax=lmax, mmax=mmax)

    if lmax is None:
        lmax = H.Alm.getlmax(len(wlm))

    spin = 1
    wlm_tmp = wlm_scalar2spin(wlm, spin)
    window_vect = H.alm2map_spin(wlm_tmp, nside, spin, lmax, mmax=mmax)
    window_vect[0] *= mask
    window_vect[1] *= mask

    spin = 2
    wlm_tmp = wlm_scalar2spin(wlm, spin)
    window_tens = H.alm2map_spin(wlm_tmp, nside, spin, lmax, mmax=mmax)
    window_tens[0] *= mask
    window_tens[1] *= mask

    return window_vect, window_tens
Exemplo n.º 28
0
def make_maps(
    a3darraymap
):  # a function that makes maps from gamma 1 and gamma 2 shear measurements - needs a 3d array of an empty map, a g1 map and a g2map

    NSIDE = hp.npix2nside(len(
        a3darraymap[1]))  #finds nside of a map from just input map

    LMAX = 2 * NSIDE - 1

    ells = hp.sphtfunc.Alm.getsize(lmax=LMAX)

    lmode = np.zeros(ells)
    for jh in range(0, ells):
        lmode[jh] = hp.sphtfunc.Alm.getlm(
            lmax=LMAX,
            i=jh)[0]  #puts ell modes in an array, doesnt need to be a loop
    lfacsphi = -2 / np.sqrt(
        (lmode + 2) * (lmode + 1) * (lmode) * (lmode - 1)
    )  #ell mode coefficients to go from E_alm to gravitational potential
    lfacsphi[np.where(lmode < 2)] = 0
    lfacsphi[np.isnan(lfacsphi)] = 0
    lfacsphi[np.isinf(lfacsphi)] = 0

    lfacskappa = (lmode * (lmode + 1) * ((lmode + 2)**-1) * ((lmode - 1)**-1)
                  )**0.5  ##ell mode coefficients to go from E_alm to kappa

    lfacskappa[np.where(lmode < 2)] = 0
    lfacskappa[np.isinf(lfacskappa)] = 0
    lfacskappa[np.isnan(lfacskappa)] = 0

    ## SY 30/5/18 Added minus sign below
    alphafacs = -2 / np.sqrt(
        (lmode + 2) *
        (lmode -
         1))  #ell mode coefficients to go from E_alm to alpha, bend angle
    alms = hp.sphtfunc.map2alm(a3darraymap, lmax=LMAX, pol=True)
    Ealm = alms[1]
    Kalm = lfacskappa * Ealm
    Palm = lfacsphi * Ealm

    alphaplus = alphafacs * alms[2]
    alphaminus = alphafacs * alms[1]

    alphaplus[np.where(lmode < 2)] = 0
    alphaminus[np.where(lmode < 2)] = 0  #2 different bend angles.
    Kmap = hp.sphtfunc.alm2map(Kalm, nside=NSIDE, lmax=LMAX)
    Pmap = hp.sphtfunc.alm2map(Palm, nside=NSIDE, lmax=LMAX)
    Amap = hp.alm2map_spin(
        [alphaplus, alphaminus], nside=NSIDE, spin=1,
        lmax=LMAX)  # spin1 transform to get the 2 bend angles
    return Kmap, Pmap, Amap
Exemplo n.º 29
0
    def apply_alm(self, alm):
        """B^dagger N^{-1} B"""
        lmax = alm.lmax

        hp.almxfl(alm.elm, self.b_transf, inplace=True)
        hp.almxfl(alm.blm, self.b_transf, inplace=True)
        qmap, umap = alm2map_spin((alm.elm, alm.blm), self.nside, 2, lmax)

        self.apply_map([qmap, umap])  # applies N^{-1}
        npix = len(qmap)

        telm, tblm = map2alm_spin([qmap, umap], 2, lmax=lmax)
        alm.elm[:] = telm
        alm.blm[:] = tblm

        hp.almxfl(alm.elm, self.b_transf * (npix / (4. * np.pi)), inplace=True)
        hp.almxfl(alm.blm, self.b_transf * (npix / (4. * np.pi)), inplace=True)
Exemplo n.º 30
0
    def get_sim_pmap(self,idx):
        """Returns polarization healpy maps for a simulation

            Args:
                idx: simulation index

            Returns:
                Q and U healpy maps

        """
        elm = self.sims_cmb_len.get_sim_elm(idx)
        hp.almxfl(elm,self.cl_transf,inplace=True)
        blm = self.sims_cmb_len.get_sim_blm(idx)
        hp.almxfl(blm, self.cl_transf, inplace=True)
        Q,U = hp.alm2map_spin([elm,blm], self.nside, 2,hp.Alm.getlmax(elm.size))
        del elm,blm
        return Q + self.get_sim_qnoise(idx),U + self.get_sim_unoise(idx)
Exemplo n.º 31
0
def make_maps(a3darraymap):
    '''a function that makes maps from gamma 1 and gamma 2 shear measurements -
        needs a 3d array of an empty map, a g1 map and a g2map
    '''

    #finds nside of a map from just input map
    nside = hp.npix2nside(len(a3darraymap[1]))
    lmax = 2 * nside - 1
    nell = hp.sphtfunc.Alm.getsize(lmax=lmax)

    #puts ell modes in an array, doesnt need to be a loop
    lmode = hp.sphtfunc.Alm.getlm(lmax=lmax, i=np.arange(nell))[0]

    #-- get alm from input map
    alms = hp.sphtfunc.map2alm(a3darraymap, lmax=lmax, pol=True)

    #ell mode coefficients to go from E_alm to gravitational potential
    lfactor_phi = -2 / np.sqrt(
        (lmode + 2) * (lmode + 1) * (lmode) * (lmode - 1))
    lfactor_phi[lmode < 2] = 0

    ##ell mode coefficients to go from E_alm to kappa
    lfactor_kappa = lmode * (lmode + 1) / (lmode + 2) / np.sqrt(lmode - 1)
    lfactor_kappa[lmode < 2] = 0

    ## SY 30/5/18 Added minus sign below
    #ell mode coefficients to go from E_alm to alpha, bend angle
    lfactor_alpha = -2 / np.sqrt((lmode + 2) * (lmode - 1))
    lfactor_alpha[lmode < 2] = 0

    weird_alm = lfactor_kappa * alms[1]
    phi_alm = lfactor_phi * alms[1]

    alphaminus_alm = lfactor_alpha * alms[1]
    alphaplus_alm = lfactor_alpha * alms[2]

    weird_map = hp.sphtfunc.alm2map(kappa_alm, nside=nside, lmax=lmax)
    phi_map = hp.sphtfunc.alm2map(phi_alm, nside=nside, lmax=lmax)
    # -- spin1 transform to get the 2 bend angles
    alpha_map = hp.alm2map_spin([alphaplus_alm, alphaminus_alm],
                                nside=nside,
                                spin=1,
                                lmax=lmax)
    return weird_map, phi_map, alpha_map
Exemplo n.º 32
0
    def simulate(self, det, idx):
        assert( det in (dmc.wmap_das + dmc.wmap_bands) )
        
        tlm = self.sim_teblm_cmb.get_sim_tlm(idx)
        elm = self.sim_teblm_cmb.get_sim_elm(idx)
        blm = self.sim_teblm_cmb.get_sim_blm(idx)
        
        hp.almxfl(tlm, self.get_beam(det), inplace=True)
        hp.almxfl(elm, self.get_beam(det), inplace=True)
        hp.almxfl(blm, self.get_beam(det), inplace=True)
            
        tmap = hp.alm2map(tlm, self.nside)
        qmap, umap = hp.alm2map_spin( (elm, blm), self.nside, 2, lmax=self.lmax )

        tmap_nse, qmap_nse, umap_nse = self.simulate_nse(det)
        tmap += tmap_nse; del tmap_nse
        qmap += qmap_nse; del qmap_nse
        umap += umap_nse; del umap_nse
        
        return tmap, (qmap + 1.j*umap)
Exemplo n.º 33
0
    def simulate(self, det, idx):
        assert(det == '')
        
        tlm = self.sim_teblm_cmb.get_sim_tlm(idx)
        elm = self.sim_teblm_cmb.get_sim_elm(idx)
        blm = self.sim_teblm_cmb.get_sim_blm(idx)

        beam = self.bl[0:self.lmax+1] * hp.pixwin(self.nside)[0:self.lmax+1]
        hp.almxfl(tlm, beam, inplace=True)
        hp.almxfl(elm, beam, inplace=True)
        hp.almxfl(blm, beam, inplace=True)
        
        tmap = hp.alm2map(tlm, self.nside)
        qmap, umap = hp.alm2map_spin( (elm, blm), self.nside, 2, lmax=self.lmax )

        npix = 12*self.nside**2
        tmap += np.random.standard_normal(npix) * (self.noiseT_uK_arcmin * np.sqrt(npix / 4. / np.pi) * np.pi / 180. / 60.)
        qmap += np.random.standard_normal(npix) * (self.noiseP_uK_arcmin * np.sqrt(npix / 4. / np.pi) * np.pi / 180. / 60.)
        umap += np.random.standard_normal(npix) * (self.noiseP_uK_arcmin * np.sqrt(npix / 4. / np.pi) * np.pi / 180. / 60.)

        return tmap, qmap + 1.j*umap
Exemplo n.º 34
0
def simulate_map(alms,
                 nside=2048, lmax=2000,
                 frequency=100,smear=False,
                 beam=None, beamP=None,
                 beam_file=PLANCK_DATA_PATH+'HFI_RIMO_Beams-100pc_R2.00.fits'):

    Tlm = alms[0]
    Elm = alms[1]
    Blm = alms[2]
    if smear:
        if (beam is None) or (beamP is None) :
            hdulist = fits.open(beam_file)
            beam = hdulist[BEAM_INDEX['{}'.format(frequency)]].data.NOMINAL[0][:lmax+1]
            beamP = hdulist[BEAM_INDEX['{}P'.format(frequency)]].data.NOMINAL[0][:lmax+1]
        hp.sphtfunc.almxfl(Tlm, beam, inplace=True)
        hp.sphtfunc.almxfl(Elm, beamP, inplace=True)
        hp.sphtfunc.almxfl(Blm, beamP, inplace=True)

    Tmap = hp.alm2map( Tlm, nside, verbose=False )
    Qmap, Umap = hp.alm2map_spin( (Elm, Blm), nside, 2, lmax=lmax)
    return Tmap, Qmap, Umap
Exemplo n.º 35
0
    def alm2map_spin(self,alm,spin_alm,spin_transform,ncomp,mlmax):
        """
        Returns
        X(n) = sum_lm  alm_s1 s2_Y_lm(n)

        where s1=spin_alm and s2=spin_transform are different spins.

        alm should be (alm_+|s|, alm_-|s|)

        """
        # Transform (alm_+|s|, alm_-|s|) to (alm_+, alm_-)
        ap_am = irot2d(alm,spin=spin_alm)
        if self.hpix:
            res = hp.alm2map_spin(ap_am,nside=self.nside,spin=abs(spin_transform),lmax=mlmax)
        else:
            omap = enmap.empty((ncomp,)+self.shape[-2:],self.wcs,dtype=self.dtype)
            res = cs.alm2map(ap_am,omap,spin=abs(spin_transform))
        # Rotate maps M+ and M- to M_+|s| and M_-|s|
        # M_+|s| is the first component
        # M_-|s| is the second component
        return rot2d(res)
Exemplo n.º 36
0
    def apply_alm(self, alm):
        # applies Y^T N^{-1} Y
        lmax = alm.lmax
        
        hp.almxfl(alm.tlm, self.b_transf, inplace=True)
        hp.almxfl(alm.elm, self.b_transf, inplace=True)
        hp.almxfl(alm.blm, self.b_transf, inplace=True)

        tmap = hp.alm2map(alm.tlm, self.nside)
        qmap, umap = hp.alm2map_spin( (alm.elm, alm.blm), self.nside, 2 )

        self.apply_map( [tmap, qmap, umap] )

        alm.tlm[:]  = hp.map2alm(tmap, lmax=lmax, iter=0, regression=False)
        alm.tlm[:] *= (self.npix / (4.*np.pi))

        telm, tblm = hp.map2alm_spin( [qmap, umap], 2, lmax=lmax )
        alm.elm[:] = telm; alm.blm[:] = tblm
        
        hp.almxfl(alm.tlm, self.b_transf, inplace=True)
        hp.almxfl(alm.elm, self.b_transf, inplace=True)
        hp.almxfl(alm.blm, self.b_transf, inplace=True)
Exemplo n.º 37
0
def lens_glm_sym(spin,dlm,glm,nside,nband = 32,facres = 0,clm = None,rotpol = True):
    """
    Same as lens_alm but lens simultnously a North and South colatitude band,
    to make profit of the symmetries of the spherical harmonics.
    Note that tlm = -glm in the spin 0 case.
    """
    target_nt = 3 ** 1 * 2 ** (11 + facres)
    th1s = np.arange(nband) * (np.pi * 0.5 / nband)
    th2s = np.concatenate((th1s[1:],[np.pi * 0.5]))
    Nt_perband = target_nt / nband
    Redtot, Imdtot = hp.alm2map_spin([dlm, np.zeros_like(dlm)], nside, 1, hp.Alm.getlmax(dlm.size))
    ret = np.zeros(hp.nside2npix(nside),dtype = float if spin == 0 else complex)
    for th1,th2 in zip(th1s,th2s):
        pixN = hp.query_strip(nside, th1, th2, inclusive=True)
        pixS = hp.query_strip(nside, np.pi- th2,np.pi - th1, inclusive=True)
        tnewN,phinewN = _buildangles(hp.pix2ang(nside, pixN),Redtot[pixN],Imdtot[pixN])
        tnewS,phinewS = _buildangles(hp.pix2ang(nside, pixS), Redtot[pixS], Imdtot[pixS])
        matnewN = np.max(tnewN)
        mitnewN = np.min(tnewN)
        matnewS = np.max(tnewS)
        mitnewS = np.min(tnewS)
        buffN = 10 * (matnewN - mitnewN) / (Nt_perband - 1) / (1. - 2. * 10. / (Nt_perband - 1))
        buffS = 10 * (matnewS - mitnewS) / (Nt_perband - 1) / (1. - 2. * 10. / (Nt_perband - 1))
        thup = min(np.pi - (matnewS + buffS),mitnewN - buffN)
        thdown = max(np.pi - (mitnewS - buffS),matnewN + buffN)
        if spin == 0:
            lenN,lenS = lens_band_sym(glm,thup,thdown,Nt_perband,(tnewN,phinewN),(tnewS,phinewS),
                                                Nphi=get_Nphi(thup, thdown, facres=facres))
            ret[pixN] = lenN
            ret[pixS] = lenS
        else :
            lenNR,lenNI,lenSR,lenSI = lens_gcband_sym(spin,glm,thup,thdown, Nt_perband,(tnewN,phinewN),(tnewS,phinewS),
                                                      Nphi=get_Nphi(thup, thdown, facres=facres),clm = clm)
            ret[pixN] = lenNR + 1j * lenNI
            ret[pixS] = lenSR + 1j * lenSI
            if rotpol :
                ret[pixN] *= polrot(spin,ret[pixN], hp.pix2ang(nside, pixN)[0],Redtot[pixN],Imdtot[pixN])
                ret[pixS] *= polrot(spin,ret[pixS], hp.pix2ang(nside, pixS)[0],Redtot[pixS],Imdtot[pixS])
    return ret
Exemplo n.º 38
0
    def test_spin0(self):
        m1 = hp.alm2map( self.almg, self.nside, self.lmax )
        m2_r, m2_i = hp.alm2map_spin( [self.almg, 0.*self.almg], self.nside, 0, self.lmax )

        np.testing.assert_array_almost_equal( m1, m2_r, decimal=8)
        np.testing.assert_array_almost_equal( m1*0., m2_i, decimal=8)