Ejemplo 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
def _get_wEBlm(window_scal, lmax=None, maps=False):
    '''
    Calculate the E/B-type alms from the vector and tensor windows.
    Constructs the vector and tensor windows from the scalar wlms and
    then masks the resulting maps. This masking results in non-zero B-mode
    type wlms.
    '''

    mask = np.ones_like(window_scal)
    mask[window_scal == 0] = 0

    window_vect, window_tens = H_ext.window2vecttens(window_scal, mask=mask,
                                                     lmax=lmax)

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

    n = len(wlm)
    wEBlm = np.empty([5, n], dtype=np.complex)
    wEBlm[0, :] = -wlm

    wlm_tmp = H.map2alm_spin(window_vect, 1, lmax=lmax)
    wEBlm[1, :] = wlm_tmp[0]
    wEBlm[-1, :] = wlm_tmp[1]

    wlm_tmp = H.map2alm_spin(window_tens, 2, lmax=lmax)
    wEBlm[2, :] = wlm_tmp[0]
    wEBlm[-2, :] = wlm_tmp[1]

    if maps:
        window_vect = window_vect[0] + 1j*window_vect[1]
        window_tens = window_tens[0] + 1j*window_tens[1]
        return wEBlm, (window_vect, window_tens)
    else:
        return wEBlm
Ejemplo n.º 3
0
def get_ElmBlm(filename='test_elmblm.npy',
               Qmap=None,
               Umap=None,
               mask=None,
               lmax=100,
               add_beam=None,
               div_beam=None,
               healpy_format=False,
               recalc=False,
               filtermap=False,
               l0=None,
               save=False):
    """computes and saves 2d (Elms, Blms) from given
    Q and U maps, corrected for sqrt(fsky)
    """
    if not recalc and os.path.exists(data_path + filename):
        Elm2d, Blm2d = np.load(data_path + filename)
        return Elm2d, Blm2d
    fsky = mask.sum() / len(mask)
    Elm, Blm = hp.map2alm_spin((Qmap * mask, Umap * mask), 2, lmax=lmax)
    Elm /= np.sqrt(fsky)
    Blm /= np.sqrt(fsky)
    if add_beam is not None:
        hp.sphtfunc.almxfl(Elm, add_beam, inplace=True)
        hp.sphtfunc.almxfl(Blm, add_beam, inplace=True)
    if div_beam is not None:
        hp.sphtfunc.almxfl(Elm, 1. / div_beam, inplace=True)
        hp.sphtfunc.almxfl(Blm, 1. / div_beam, inplace=True)
    if not healpy_format:
        ls, ms = hp.sphtfunc.Alm.getlm(lmax, np.arange(len(Elm)))
        Elm = make2d_alm_square(Elm, lmax, ls, ms)
        Blm = make2d_alm_square(Blm, lmax, ls, ms)
    if save:
        np.save(data_path + filename, [Elm, Blm])
    return Elm, Blm
Ejemplo n.º 4
0
 def test_map2alm_spin_spin_precomputed(self):
     """ compare map2alm_spin outputs to some precomputed results for spin=1,2,3 """
     
     for spin in [1,2,3]:
         tglm, tclm = hp.map2alm_spin( [maps[spin].real, maps[spin].imag], spin, lmax_precomputed )
         np.testing.assert_array_almost_equal( glms[spin], tglm, decimal=7)
         np.testing.assert_array_almost_equal( clms[spin], tclm, decimal=7)
Ejemplo n.º 5
0
def get_spin1_alms(map_r, map_trans):
    """Get the vector spherical harmonic coefficients for spin1 harmonics.

    Parameters:
    -----------
    map_r - np.ndarray(ndim=1, dtype=float)
        map containing radial component of vector field
    map_trans - list
        len(map_trans) = 2
        map_trans[0] - map of vector field corresponding to +1 component
        map_trans[1] - map of vector field corresponding to -1 component

    Returns:
    --------
    alm2r - spin0 spherical harmonic coefficients
    alm2v - spin1 spherical harmonic coefficients for s=+1
    alm2w - spin1 spherical harmonic coefficients for s=-1

    """
    assert len(map_r) == len(map_trans[0]) == len(map_trans[1])
    alm_r = hp.map2alm(map_r)
    alm_pm = hp.map2alm_spin(map_trans, 1)
    alm_v = -alm_pm[0]
    alm_w = -1j * alm_pm[1]
    return alm_r, alm_v, alm_w
Ejemplo n.º 6
0
 def map2alm_spin(self,imap,lmax,spin_alm,spin_transform):
     dmap = -irot2d(np.stack((imap,imap.conj())),spin=spin_alm).real
     if self.hpix:
         res = hp.map2alm_spin(dmap,lmax=lmax,spin=spin_transform)
         return res
     else:
         return cs.map2alm(enmap.enmap(dmap,imap.wcs),spin=spin_transform,lmax=lmax)
Ejemplo n.º 7
0
 def test_map2alm_spin_spin_precomputed(self):
     """ compare map2alm_spin outputs to some precomputed results for spin=1,2,3 """
     
     for spin in [1,2,3]:
         tglm, tclm = hp.map2alm_spin( [maps[spin].real, maps[spin].imag], spin, lmax_precomputed )
         np.testing.assert_array_almost_equal( glms[spin], tglm, decimal=7)
         np.testing.assert_array_almost_equal( clms[spin], tclm, decimal=7)
Ejemplo n.º 8
0
    def apply_ivf(self, det, tmap, pmap):
        assert(self.lmax == 1000)

        mask_t = qcinv.util.load_map(self.mask_t)
        mask_p = qcinv.util.load_map(self.mask_p)

        bl  = dmc.get_bl(self.sim_lib.year, det)[0:self.lmax+1]
        pxw = hp.pixwin( self.sim_lib.nside )[0:self.lmax+1]

        # qcinv filtering for temperature
        dcf = self.lib_dir + "/dense_cache_det_" + det + ".pk"
        #                  id         preconditioners                 lmax    nside     im      em            tr                      cache
        chain_descr = [ [  2, ["split(dense("+dcf+"), 64, diag_cl)"],  256,   128,       3,     0.0,  qcinv.cd_solve.tr_cg,  qcinv.cd_solve.cache_mem()],
                        [  1, ["split(stage(2), 256, diag_cl)"],       512,   256,       3,     0.0,  qcinv.cd_solve.tr_cg,  qcinv.cd_solve.cache_mem()],
                        [  0, ["split(stage(1), 512, diag_cl)"],      1000,   512,  np.inf,  1.0e-6,  qcinv.cd_solve.tr_cg,  qcinv.cd_solve.cache_mem()] ]

        ninv = ( hp.read_map( dmc.get_fname_iqumap(self.sim_lib.year, det, self.sim_lib.forered), hdu=1, field=3 ) /
                 dmc.sigma0[(self.sim_lib.year, self.sim_lib.forered, 'T')][det]**2 / 1e6 * mask_t ) 
        n_inv_filt = qcinv.opfilt_tt.alm_filter_ninv( ninv, bl*pxw, marge_monopole=True, marge_dipole=True, marge_maps=[] )
        chain = qcinv.multigrid.multigrid_chain( qcinv.opfilt_tt, chain_descr, self.cl, n_inv_filt )

        tlm = np.zeros( qcinv.util_alm.lmax2nlm(self.lmax), dtype=np.complex )
        chain.solve( tlm, tmap )

        # simple filtering for polarization.
        elm, blm = hp.map2alm_spin( (pmap.real * mask_p, pmap.imag * mask_p), 2, lmax=self.lmax )
        ftl, fel, fbl = self.get_ftebl(det)
        hp.almxfl( elm, fel / bl / pxw, inplace=True )
        hp.almxfl( blm, fbl / bl / pxw, inplace=True )

        return tlm, elm, blm
Ejemplo n.º 9
0
def calc_alm(Imap, Qmap, Umap, mask=None,
               lmax=200,
               add_beam=None,div_beam=None,
               add_beamP=None,div_beamP=None,
               healpy_format=False):
    """computes alms, given
    maps and a mask, corrected for sqrt(fsky), optionally corrected for beams
    """
    fsky = mask.sum() / len(mask)
    Tlm = hp.map2alm(Imap * mask, lmax=lmax) / np.sqrt(fsky)
    Elm, Blm = hp.map2alm_spin( (Qmap*mask,Umap*mask), 2, lmax=lmax )
    Elm /= np.sqrt(fsky)
    Blm /= np.sqrt(fsky)
    if (add_beam is not None) and (add_beamP is not None):
        hp.sphtfunc.almxfl(Tlm, add_beam, inplace=True)
        hp.sphtfunc.almxfl(Elm, add_beam, inplace=True)
        hp.sphtfunc.almxfl(Blm, add_beam, inplace=True)
    if (div_beam is not None) and (div_beamP is not None):
        hp.sphtfunc.almxfl(Tlm, 1./div_beam, inplace=True)
        hp.sphtfunc.almxfl(Elm, 1./div_beam, inplace=True)
        hp.sphtfunc.almxfl(Blm, 1./div_beam, inplace=True)
    if not healpy_format:
        ls, ms = hp.sphtfunc.Alm.getlm(lmax, np.arange(len(Tlm)))
        Tlm = make2d_alm_square(Tlm, lmax, ls, ms)
        Elm = make2d_alm_square(Elm, lmax, ls, ms)
        Blm = make2d_alm_square(Blm, lmax, ls, ms)

    return Tlm,Elm,Blm
Ejemplo n.º 10
0
def get_ElmBlm(filename='test_elmblm.npy',
               Qmap=None, Umap=None, mask=None,
               lmax=100,add_beam=None,div_beam=None,
               healpy_format=False,
               recalc=False,
               filtermap=False, l0=None,
               save=False):
    """computes and saves 2d (Elms, Blms) from given
    Q and U maps, corrected for sqrt(fsky)
    """
    if not recalc and os.path.exists(data_path + filename):
        Elm2d, Blm2d = np.load(data_path + filename)
        return Elm2d, Blm2d
    fsky = mask.sum() / len(mask)
    Elm, Blm = hp.map2alm_spin( (Qmap*mask,Umap*mask), 2, lmax=lmax )
    Elm /= np.sqrt(fsky)
    Blm /= np.sqrt(fsky)
    if add_beam is not None:
        hp.sphtfunc.almxfl(Elm, add_beam, inplace=True)
        hp.sphtfunc.almxfl(Blm, add_beam, inplace=True)
    if div_beam is not None:
        hp.sphtfunc.almxfl(Elm, 1./div_beam, inplace=True)
        hp.sphtfunc.almxfl(Blm, 1./div_beam, inplace=True)
    if not healpy_format:
        ls, ms = hp.sphtfunc.Alm.getlm(lmax, np.arange(len(Elm)))
        Elm = make2d_alm_square(Elm, lmax, ls, ms)
        Blm = make2d_alm_square(Blm, lmax, ls, ms)
    if save:
        np.save(data_path + filename, [Elm,Blm])
    return Elm, Blm
Ejemplo n.º 11
0
    def _cache_eblm(self, idx):
        elm = self.unlcmbs.get_sim_elm(idx)
        blm = None if 'b' not in self.fields else self.unlcmbs.get_sim_blm(idx)
        dlm = self.get_sim_plm(idx)
        assert 'o' not in self.fields, 'not implemented'

        lmaxd = hp.Alm.getlmax(dlm.size)
        hp.almxfl(dlm,
                  np.sqrt(
                      np.arange(lmaxd + 1, dtype=float) *
                      np.arange(1, lmaxd + 2)),
                  inplace=True)
        Qlen, Ulen = self.lens_module.alm2lenmap_spin([elm, blm], [dlm, None],
                                                      self.nside_lens,
                                                      2,
                                                      nband=self.nbands,
                                                      facres=self.facres,
                                                      verbose=self.verbose)
        elm, blm = hp.map2alm_spin([Qlen, Ulen], 2, lmax=self.lmax)
        del Qlen, Ulen
        hp.write_alm(os.path.join(self.lib_dir, 'sim_%04d_elm.fits' % idx),
                     elm)
        del elm
        hp.write_alm(os.path.join(self.lib_dir, 'sim_%04d_blm.fits' % idx),
                     blm)
Ejemplo n.º 12
0
def calc_IP2_equil(Imap, Qmap, Umap, lmax=100):

    Tlm = hp.map2alm(Imap)
    Elm, Blm = hp.map2alm_spin((Qmap, Umap), 2)

    TEE = hp.alm2cl(Tlm, Elm**2)
    TBB = hp.alm2cl(Tlm, Blm**2)
    TEB = hp.alm2cl(Tlm, Elm * Blm)

    ls = np.arange(len(TEE))
    return ls, TEE, TBB, TEB
Ejemplo n.º 13
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)
Ejemplo n.º 14
0
 def _apply_ivf_p(self, pmap, soltn=None):
     assert len(pmap[0]) == hp.nside2npix(self.nside) and len(
         pmap[0]) == len(pmap[1])
     elm, blm = hp.map2alm_spin([m for m in pmap], 2, lmax=self.lmax_fl)
     elm = hp.almxfl(
         elm,
         self.get_fel() * utils.cli(self.transf[:len(self.fel)]))
     blm = hp.almxfl(
         blm,
         self.get_fbl() * utils.cli(self.transf[:len(self.fbl)]))
     return elm, blm
Ejemplo n.º 15
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)
Ejemplo n.º 16
0
def calc_prep(maps, s_cls, n_inv_filt):
    qmap, umap = np.copy(maps[0]), np.copy(maps[1])
    assert len(qmap) == len(umap)
    lmax = len(n_inv_filt.b_transf) - 1
    npix = len(qmap)

    n_inv_filt.apply_map([qmap, umap])
    elm, blm = map2alm_spin([qmap, umap], 2, lmax=lmax)
    hp.almxfl(elm, n_inv_filt.b_transf * npix / (4. * np.pi), inplace=True)
    hp.almxfl(blm, n_inv_filt.b_transf * npix / (4. * np.pi), inplace=True)
    return eblm([elm, blm])
Ejemplo n.º 17
0
def calc_IP2_equil(Imap, Qmap, Umap,
                   lmax=100):

    Tlm = hp.map2alm( Imap )
    Elm,Blm = hp.map2alm_spin( (Qmap,Umap), 2 )
    
    TEE = hp.alm2cl( Tlm, Elm**2 )
    TBB = hp.alm2cl( Tlm, Blm**2 )
    TEB = hp.alm2cl( Tlm, Elm*Blm )

    ls = np.arange( len(TEE) )
    return ls, TEE, TBB, TEB
Ejemplo n.º 18
0
def b_cov_T353_E143_B143(cl_file=pf.PLANCK_DATA_PATH+'bf_base_cmbonly_plikHMv18_TT_lowTEB_lmax4000.minimum.theory_cl',lmax=100):

    Imap = hp.read_map(pf.PLANCK_DATA_PATH + 'HFI_SkyMap_353_2048_R2.02_full.fits')
    Tlm = hp.map2alm(Imap,lmax=lmax)
    cltt = hp.alm2cl(Tlm,lmax=lmax)

    mask = pf.get_planck_mask(psky=70)
    Qmap, Umap = hp.read_map(pf.PLANCK_DATA_PATH + 'HFI_SkyMap_143_2048_R2.02_full.fits',field=(1,2))
    Elm, Blm = hp.map2alm_spin( (Qmap*mask,Umap*mask), 2, lmax=lmax )
    clee = hp.alm2cl(Elm,lmax=lmax)
    clbb = hp.alm2cl(Blm,lmax=lmax)

    cov = calc_b_cov_TEB(cltt, clee, clbb)
    return cov
Ejemplo n.º 19
0
    def apply_ivf(self, det, tmap, pmap):
        mask_t = qcinv.util.load_map(self.mask_t)
        mask_p = qcinv.util.load_map(self.mask_p)

        bl     = self.bl
        pxw    = hp.pixwin(self.nside)[0:self.lmax+1]

        tlm = hp.map2alm( tmap * mask_t, lmax=self.lmax, iter=0, regression=False )
        elm, blm = hp.map2alm_spin( (pmap.real * mask_p, pmap.imag * mask_p), 2, lmax=self.lmax )

        hp.almxfl( tlm, self.ftl / bl / pxw, inplace=True )
        hp.almxfl( elm, self.fel / bl / pxw, inplace=True )
        hp.almxfl( blm, self.fbl / bl / pxw, inplace=True )
    
        return tlm, elm, blm
Ejemplo n.º 20
0
def b_cov_TEB(lmax=100,frequency=353):
    """this one is map-based"""

    Imap,Qmap, Umap = hp.read_map(pf.PLANCK_DATA_PATH + 'HFI_SkyMap_{}_2048_R2.02_full.fits'.format(frequency),field=(0,1,2))
    mask = pf.get_planck_mask()
    Tlm = hp.map2alm(Imap*mask,lmax=lmax)
    cltt = hp.alm2cl(Tlm,lmax=lmax)

    Elm, Blm = hp.map2alm_spin( (Qmap*mask,Umap*mask), 2, lmax=lmax )
    clee = hp.alm2cl(Elm,lmax=lmax)
    clbb = hp.alm2cl(Blm,lmax=lmax)

    #hs = get_hs(lmax=100)
    cov = calc_b_cov_TEB(cltt, clee, clbb)#/hs
    return cov
Ejemplo n.º 21
0
def apodizedqu2pureeb(polxscal, polxvect, polxtens, lmax=None, mmax=None):
    '''
    Constructs the pure E/B alms from polarization maps apodized by the
    scalar, vector, and tensor window
    '''

    alm = H.map2alm((-polxtens[0], -polxtens[1]),
                    lmax=lmax,
                    mmax=mmax,
                    pol=False)

    if lmax is None:
        lmax = H.Alm.getlmax(len(alm[0]))

    ell, m_tmp = H.Alm.getlm(lmax)

    idx = (ell >= 2)

    apurelm = (np.zeros_like(alm[0]), np.zeros_like(alm[0]))

    spinfact = 1.0 / np.sqrt(
        (ell[idx] - 1.0) * ell[idx] * (ell[idx] + 1.0) * (ell[idx] + 2.0))
    apurelm[0][idx] += spinfact * alm[0][idx]
    apurelm[1][idx] += spinfact * alm[1][idx]

    alm = H.map2alm_spin(polxvect, 1, lmax=lmax, mmax=mmax)
    spinfact = 2.0 / np.sqrt((ell[idx] - 1.0) * (ell[idx] + 2.0))
    apurelm[0][idx] += spinfact * alm[0][idx]
    apurelm[1][idx] += spinfact * alm[1][idx]

    alm = H.map2alm_spin(polxscal, 2, lmax=lmax, mmax=mmax)
    spinfact = 1.0
    apurelm[0][idx] += spinfact * alm[0][idx]
    apurelm[1][idx] += spinfact * alm[1][idx]

    return apurelm
Ejemplo n.º 22
0
 def _get_sim_Tgclm(self, idx, k, swapped=False, xfilt1=None, xfilt2=None):
     """ T only lensing potentials estimators """
     f2map1 = self.f2map1 if not swapped else self.f2map2
     f2map2 = self.f2map2 if not swapped else self.f2map1
     xftl1 = xfilt1 if not swapped else xfilt2
     xftl2 = xfilt2 if not swapped else xfilt1
     tmap = f2map1.get_irestmap(idx, xfilt=xftl1)  # healpy map
     G, C = f2map2.get_gtmap(idx, k=k, xfilt=xftl2)   # 2 healpy maps
     G *= tmap
     C *= tmap
     del tmap
     G, C = hp.map2alm_spin([G, C], 1, lmax=self.lmax_qlm['T'])
     fl = - np.sqrt(np.arange(self.lmax_qlm['T'] + 1, dtype=float) * np.arange(1, self.lmax_qlm['T'] + 2))
     hp.almxfl(G, fl, inplace=True)
     hp.almxfl(C, fl, inplace=True)
     return G, C
Ejemplo n.º 23
0
    def apply_ivf(self, det, tmap, pmap):
        mask_t = qcinv.util.load_map(self.mask_t)
        mask_p = qcinv.util.load_map(self.mask_p)

        tlm = hp.map2alm( tmap * mask_t, lmax=self.lmax, iter=0, regression=False )
        elm, blm = hp.map2alm_spin( (pmap.real * mask_p, pmap.imag * mask_p), 2, lmax=self.lmax )

        bl  = dmc.get_bl(self.sim_lib.year, det)[0:self.lmax+1]
        pxw = hp.pixwin( self.sim_lib.nside )[0:self.lmax+1]

        ftl, fel, fbl = self.get_ftebl(det)

        hp.almxfl( tlm, ftl / bl / pxw, inplace=True )
        hp.almxfl( elm, fel / bl / pxw, inplace=True )
        hp.almxfl( blm, fbl / bl / pxw, inplace=True )

        return tlm, elm, blm
Ejemplo n.º 24
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)
Ejemplo n.º 25
0
def lens_eblm(elm,
              dlm,
              blm=None,
              nside=2048,
              lmaxout=None,
              verbose=True,
              nband=16,
              facres=0):
    # FIXME : suppress the stupid intermediate step
    lenmap = eblm2lensmap(nside,
                          elm,
                          dlm,
                          blm=blm,
                          verbose=verbose,
                          nband=nband,
                          facres=facres)
    return hp.map2alm_spin([lenmap.real, lenmap.imag],
                           2,
                           lmax=(lmaxout or hp.Alm.getlmax(elm.size)))
Ejemplo n.º 26
0
def calc_prep(maps, s_cls, n_inv_filt):
    tmap, qmap, umap = np.copy(maps[0]), np.copy(maps[1]), np.copy(maps[2])
    assert(len(tmap) == len(qmap)); assert(len(tmap) == len(umap))
    npix  = len(tmap)
    
    n_inv_filt.apply_map([tmap, qmap, umap])

    lmax  = len(n_inv_filt.b_transf) - 1

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

    elm, blm = hp.map2alm_spin( [qmap, umap], 2, lmax=lmax)
    elm *= npix / (4.*np.pi); blm *= npix / (4.*np.pi)

    hp.almxfl( tlm, n_inv_filt.b_transf, inplace=True )
    hp.almxfl( elm, n_inv_filt.b_transf, inplace=True )
    hp.almxfl( blm, n_inv_filt.b_transf, inplace=True )
    return teblm([tlm, elm, blm])
Ejemplo n.º 27
0
def calc_prep(maps, s_cls, n_inv_filt):
    tmap, qmap, umap = np.copy(maps[0]), np.copy(maps[1]), np.copy(maps[2])
    assert (len(tmap) == len(qmap))
    assert (len(tmap) == len(umap))
    npix = len(tmap)

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

    lmax = len(n_inv_filt.b_transf) - 1

    tlm = hp.map2alm(tmap, lmax=lmax, iter=0)
    tlm *= npix / (4. * np.pi)

    elm, blm = hp.map2alm_spin([qmap, umap], 2, lmax=lmax)
    elm *= npix / (4. * np.pi)
    blm *= npix / (4. * np.pi)

    hp.almxfl(tlm, n_inv_filt.b_transf, inplace=True)
    hp.almxfl(elm, n_inv_filt.b_transf, inplace=True)
    hp.almxfl(blm, n_inv_filt.b_transf, inplace=True)
    return teblm([tlm, elm, blm])
Ejemplo n.º 28
0
 def _get_sim_Pgclm(self, idx, k, swapped=False, xfilt1=None, xfilt2=None):
     """
     Pol. only lensing potentials estimators
     """
     f2map1 = self.f2map1 if not swapped else self.f2map2
     f2map2 = self.f2map2 if not swapped else self.f2map1
     xftl1 = xfilt1 if not swapped else xfilt2
     xftl2 = xfilt2 if not swapped else xfilt1
     repmap, impmap = f2map1.get_irespmap(idx, xfilt=xftl1)
     # complex spin 2 healpy maps
     Gs, Cs = f2map2.get_gpmap(idx, 3, k=k, xfilt=xftl2)  # 2 healpy maps
     GC = (repmap - 1j * impmap) * (Gs + 1j * Cs)  # (-2 , +3)
     Gs, Cs = f2map2.get_gpmap(idx, 1, k=k, xfilt=xftl2)
     GC -= (repmap + 1j * impmap) * (Gs - 1j * Cs)  # (+2 , -1)
     del repmap, impmap, Gs, Cs
     G, C = hp.map2alm_spin([GC.real, GC.imag], 1, lmax=self.lmax_qlm['P'])
     del GC
     fl = - np.sqrt(np.arange(self.lmax_qlm['P'] + 1, dtype=float) * np.arange(1, self.lmax_qlm['P'] + 2))
     hp.almxfl(G, fl, inplace=True)
     hp.almxfl(C, fl, inplace=True)
     return G, C
Ejemplo n.º 29
0
def analworker(i):
    print "This is analysis worker starting for map", i + 1, "/", nmaps
    QU_maps = hp.read_map(fits[i], field=(1, 2))  #(Q,U)
    pixrecip = np.concatenate((np.ones(2),
                               np.reciprocal(
                                   hp.pixwin(hp.get_nside(QU_maps[0]),
                                             pol=True)[1][2:smoothing_lmax])
                               ))  #P pixwin #Not defined for l < 2
    pm_alms = hp.map2alm_spin(QU_maps, spin, lmax=smoothing_lmax - 1)
    del QU_maps
    hp.almxfl(pm_alms[0], pixrecip, inplace=True)  #Correcting for pixwin
    hp.almxfl(pm_alms[1], pixrecip, inplace=True)  #Correcting for pixwin

    #Reorder to S2LET alms
    pm_alms[0] = ps.lm_hp2lm(pm_alms[0], smoothing_lmax)
    pm_alms[1] = ps.lm_hp2lm(pm_alms[1], smoothing_lmax)

    P_alms = -1. * pm_alms[0] - 1.j * pm_alms[1]  #CHECK THIS IS CORRECT!
    del pm_alms

    wav_maps, scal_maps = ps.analysis_lm2wav_manualtiling(
        P_alms, smoothing_lmax, ndir, spin, scal_tiles, wav_tiles.T.ravel(),
        scal_bandlims, wav_bandlims)
    del P_alms
    np.save(scal_outfits[i], scal_maps)
    del scal_maps

    #Splitting up output wavelet maps
    offset = 0
    for j in xrange(jmax + 1):
        for n in xrange(ndir):
            bandlim = wav_bandlims[j]
            nelem = bandlim * (2. * bandlim - 1.)
            wav_outfits = wav_outfits_root[i] + '_j' + str(j) + '_n' + str(
                n + 1) + '.npy'
            np.save(wav_outfits, wav_maps[offset:offset + nelem])
            offset += nelem
    del wav_maps

    return 0
Ejemplo n.º 30
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)
Ejemplo n.º 31
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)
        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)
Ejemplo n.º 32
0
def check_cl_sims(nmaps=1,lmax=1000,nside=2048,
                  read_file=False,
                  filename='testsky.fits',frequency=100,
                  beam=None, beamP=None, smear=True,
                  nonoise=False,
                  cl_file='bf_base_cmbonly_plikHMv18_TT_lowTEB_lmax4000.minimum.theory_cl'):

    if read_file and os.path.exists(data_path + filename):
        Tmap, Qmap, Umap = hp.read_map(data_path + filename, field=(0,1,2))
    else:
        if nonoise:
            Tmap, Qmap, Umap = simulate_cmb(nside=nside, lmax=lmax,save=False,
                                        smear=smear, beam=beam, beamP=beamP,
                                        cl_file=cl_file)
        else:
            Tmap, Qmap, Umap = observe_cmb_sky(save=False, nside=nside, npix=None, lmax=3000,
                                                frequency=frequency, beam=beam, beamP=beamP,
                                                cl_file=cl_file)
            
    Tlm = hp.map2alm(Tmap, lmax=lmax)
    Elm,Blm = hp.map2alm_spin( (Qmap,Umap), 2, 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, 1./beam, inplace=True)
        hp.sphtfunc.almxfl(Elm, 1./beamP, inplace=True)
        hp.sphtfunc.almxfl(Blm, 1./beamP, inplace=True)
    
    ls = np.arange(lmax+1)
    factor = ls * ( ls + 1. ) / (2.*np.pi)
    
    cltt = hp.alm2cl(Tlm) * factor
    clee = hp.alm2cl(Elm) * factor
    clbb = hp.alm2cl(Blm) * factor
    clte = hp.alm2cl(Tlm, Elm) * factor
                                                  
    cl = get_theory_cls()
    ls_theory = cl[0][:lmax+1]
    factor_theory = ls_theory * ( ls_theory + 1. ) / (2.*np.pi)
    
    cltt_theory = cl[1][:lmax+1] * factor_theory
    clte_theory = cl[2][:lmax+1] * factor_theory
    clee_theory = cl[3][:lmax+1] * factor_theory
    clbb_theory = cl[4][:lmax+1] * factor_theory

    plt.figure()
    plt.plot(ls, cltt, label='sims')
    plt.plot(ls_theory, cltt_theory,label='theory TT')
    plt.legend()
    
    plt.figure()
    plt.plot(ls, clte, label='sims')
    plt.plot(ls_theory, clte_theory,label='theory TE')
    plt.legend()

    plt.figure()
    plt.plot(ls, clee, label='sims')
    plt.plot(ls_theory, clee_theory,label='theory EE')
    plt.legend()

    plt.figure()
    plt.plot(ls, clbb, label='sims')
    plt.plot(ls_theory, clbb_theory,label='theory BB')
    plt.legend()
Ejemplo n.º 33
0
def get_alms(
        maps=None,
        mask=None,
        maplabel='353',
        showI=False,
        pol=True,
        intensity=True,
        rewrite=False,
        writemap=False,
        savealms=True,
        masktype='PowerSpectra',  #'GalPlane2',
        lmax=100):
    """Each written map file must contain I,Q,U, and each alms file
            must contain Tlm, Elm, and Blm.
    """

    newname = 'alms_lmax{}_mask_{}__'.format(lmax,
                                             masktype) + maplabel + '.npy'

    if not os.path.exists(data_path + newname) or rewrite:
        print 'alms file {} does not exist; calculating alms...'.format(
            newname)
        if mask is None:
            if masktype == 'PowerSpectra':
                maskname = 'HFI_PowerSpect_Mask_2048_R1.10.fits'
                maskfield = 0
            elif masktype == 'GalPlane60':
                maskname = 'HFI_Mask_GalPlane-apo0_2048_R2.00.fits',
                maskfield = 2
            elif masktype == 'no':
                maskname = 'HFI_PowerSpect_Mask_2048_R1.10.fits'
                maskfield = 0
            mask = hp.read_map(data_path + maskname, field=maskfield)
            if masktype == 'no':
                mask = mask * 0. + 1.
        masknside = hp.get_nside(mask)
        if maps is None:
            Imap, Qmap, Umap = hp.read_map(
                data_path +
                'HFI_SkyMap_{}_2048_R2.02_full.fits'.format(maplabel),
                hdu=1,
                field=(0, 1, 2))
            mapnside = hp.get_nside(Imap)
        else:
            if intensity and pol:
                Imap = maps[0]
                Qmap = maps[1]
                Umap = maps[2]
                mapnside = hp.get_nside(Imap)
            elif intensity and not pol:
                Imap = maps[0]
                mapnside = hp.get_nside(Imap)
            elif pol and not intensity:
                Qmap = maps[0]
                Umap = maps[1]
                mapnside = hp.get_nside(Qmap)

        if masknside != mapnside:
            print 'adjusting mask to match map resolution...'
            mask = hp.pixelfunc.ud_grade(mask, nside_out=mapnside)

        if showI:
            hp.mollview(Imap * mask)

        alms = []
        if intensity:
            Imap = Imap * mask
            Tlm = hp.map2alm(Imap, lmax=lmax)
            alms.append(Tlm)
        if pol:
            Qmap *= mask
            Umap *= mask
            Elm, Blm = hp.map2alm_spin((Qmap, Umap), 2, lmax=lmax)
            alms.append(Elm)
            alms.append(Blm)

        #this will only work if get_intensity and get_pol
        if writemap and intensity and pol:
            hp.fitsfunc.write_map(data_path + newname, [Imap, Qmap, Umap])

        if savealms and intensity and pol:
            np.save(data_path + newname, alms)

        return alms

    else:
        alms = np.load(data_path + newname, 'r')
        if intensity and pol:
            return alms[0], alms[1], alms[2]
        else:
            if intensity:
                return alms[0]
            if pol:
                return alms[1], alms[2]
Ejemplo n.º 34
0
def calc_TEB(Imap_name='HFI_SkyMap_353_2048_R2.02_full.fits',
             Pmap_name='HFI_SkyMap_353_2048_R2.02_full.fits',
             nus=None, fwhm=0.063, nside=16, lmax=100,
             lmaps_only=False, filename=None):
    """Master function for computing the bispectrum TEB
    """

    # read from file if it's there
    if filename is None:
        filename = 'bispectrum_lmax{}'.format(lmax)
        if nus is not None:
            filename += '_{}-{}-{}GHz.npy'.format(nus[0],nus[1],nus[1])
        else:
            filename += '_{}'.format(Imap_name[-5])
            if Imap_name != Pmap_name:
                filename += '_{}.npy'.format(Pmap_name[-5])
            else:
                filename += '.npy'
    print 'looking for {} ...'.format(filename)
    if os.path.exists(filename) and not lmaps_only:
        bispectrum = np.load(filename, 'r')
        return bispectrum

    # compute it, if the file doesn't exist
    if nus is not None:
        Imap_name = 'HFI_SkyMap_{}_2048_R2.02_full.fits'.format(nus[0])
        Pmap_name = 'HFI_SkyMap_{}_2048_R2.02_full.fits'.format(nus[1])
        title = '$I_{%i} P^2_{%i}$  (equilateral)' % (nus[0],nus[1])
        
    Imap = prepare_map( Imap_name, field=0,
                        nside_out=nside, fwhm=fwhm )
    Tlm = hp.map2alm( Imap, lmax=lmax )

    Qmap, Umap = prepare_map( Pmap_name, field=(1,2),
                              nside_out=nside, fwhm=fwhm )
    
    
    Elm,Blm = hp.map2alm_spin( (Qmap,Umap), 2, lmax=lmax )

    if lmax is None:
        lmax = hp.sphtfunc.Alm.getlmax(len(Tlm))
    ls, ms = hp.sphtfunc.Alm.getlm(lmax,np.arange(len(Tlm)))
    lmin = ls.min()
    mapsize = len(Imap)
    pixelsize = hp.pixelfunc.nside2pixarea(nside)
    
    Ylm = calc_Ylm(Imap, ls, ms)


    #return Ylm, Tlm, ls, ms
    print 'calculating Tl,El,Bl ...'
    
    Tl = sum_over_m(Tlm, Ylm, ls,
                        lmax=lmax, lmin=lmin, mapsize=mapsize)
    El = sum_over_m(Elm, Ylm, ls,
                        lmax=lmax, lmin=lmin, mapsize=mapsize)
    Bl = sum_over_m(Blm, Ylm, ls,
                        lmax=lmax, lmin=lmin, mapsize=mapsize)

    if lmaps_only:
        return Tl,El,Bl
    
    hs = get_hs(lmin=lmin, lmax=lmax)

    print 'calculating bispectrum ...'
    bispectrum = calc_bispectrum(Tl, El, Bl, hs,
                                 pixelsize,
                                 lmax=lmax, lmin=lmin,
                                 mapsize=mapsize)
    clean_bispectrum_of_naninf(bispectrum, hs, inplace=True)
    np.save(filename, bispectrum)
    return bispectrum
Ejemplo n.º 35
0
def get_alms(maps=None,
            mask=None,
            maplabel='353',
            showI=False,
            pol=True,
            intensity=True,
            rewrite=False,
            writemap=False,
            savealms=True,
            masktype='PowerSpectra',#'GalPlane2',
            lmax=100):

    """Each written map file must contain I,Q,U, and each alms file
            must contain Tlm, Elm, and Blm.
    """


    newname = 'alms_lmax{}_mask_{}__'.format(lmax, masktype) + maplabel + '.npy'
   

    
    if not os.path.exists(data_path + newname) or rewrite:
        print 'alms file {} does not exist; calculating alms...'.format(newname)
        if mask is None:
            if masktype == 'PowerSpectra':
                maskname = 'HFI_PowerSpect_Mask_2048_R1.10.fits'
                maskfield = 0
            elif masktype == 'GalPlane60':
                maskname = 'HFI_Mask_GalPlane-apo0_2048_R2.00.fits',
                maskfield = 2
            elif masktype == 'no':
                maskname = 'HFI_PowerSpect_Mask_2048_R1.10.fits'
                maskfield = 0
            mask = hp.read_map(data_path + maskname, field=maskfield)
            if masktype == 'no':
                mask = mask*0. + 1.
        masknside = hp.get_nside(mask)
        if maps is None:
            Imap,Qmap,Umap = hp.read_map( data_path + 'HFI_SkyMap_{}_2048_R2.02_full.fits'.format(maplabel),hdu=1, field=(0,1,2) )
            mapnside = hp.get_nside(Imap)
        else:
            if intensity and pol:
                Imap = maps[0]
                Qmap = maps[1]
                Umap = maps[2]
                mapnside = hp.get_nside(Imap)
            elif intensity and not pol:
                Imap = maps[0]
                mapnside = hp.get_nside(Imap)
            elif pol and not intensity:
                Qmap = maps[0]
                Umap = maps[1]
                mapnside = hp.get_nside(Qmap)
                
        if masknside != mapnside:
            print 'adjusting mask to match map resolution...'
            mask = hp.pixelfunc.ud_grade(mask, nside_out=mapnside)

        if showI:
            hp.mollview(Imap*mask)

        alms = []
        if intensity:
            Imap = Imap*mask
            Tlm = hp.map2alm(Imap, lmax=lmax)
            alms.append(Tlm)
        if pol:
            Qmap *= mask
            Umap *= mask
            Elm,Blm = hp.map2alm_spin( (Qmap,Umap), 2, lmax=lmax )
            alms.append(Elm)
            alms.append(Blm)

        #this will only work if get_intensity and get_pol
        if writemap and intensity and pol:
            hp.fitsfunc.write_map( data_path + newname, [Imap, Qmap, Umap])
            
        if savealms and intensity and pol:
            np.save(data_path + newname, alms)

        return alms


    else:
        alms = np.load(data_path + newname, 'r')
        if intensity and pol:
            return alms[0], alms[1], alms[2]
        else:
            if intensity:
                return alms[0]
            if pol:
                return alms[1], alms[2]
Ejemplo n.º 36
0
    lat_HG = np.linspace(-59.8 + 90, 59.8 + 90, 300) * np.pi / 180
    lon_HG = np.linspace(0.2, 359.8, 900) * np.pi / 180

    dtheta_obs, dphi_obs = lat_HG[1] - lat_HG[0], lon_HG[1] - lon_HG[0]
    LON, LAT = np.meshgrid(lon_HG, lat_HG)

    print(f"[{args.year}] [{args.daynum}] Making healPy maps")
    vlat_map, vlat_mask, th_map, ph_map = make_map(LAT[mask], LON[mask],
                                                   vlat[mask], NSIDE)
    vlon_map, vlon_mask, th_map, ph_map = make_map(LAT[mask], LON[mask],
                                                   vlon[mask], NSIDE)
    map2trans = get_spin1_maps((vlat_map, vlon_map), vlat_mask)

    print(f"[{args.year}] [{args.daynum}] SHT of healPy maps")
    alm_pm = hp.map2alm_spin(map2trans, 1)
    alm_v = -alm_pm[0] * sqrt(2)
    alm_w = -1j * alm_pm[1] * sqrt(2)
    ellmax = hp.sphtfunc.Alm.getlmax(len(alm_v))
    ellArr, emmArr = hp.sphtfunc.Alm.getlm(ellmax)

    if args.chris:
        lctdir = "LCT_chris"
    else:
        lctdir = "LCT"

    if args.testrun:
        np.savez_compressed(f"/scratch/g.samarth/HMIDATA/" +
                            f"{lctdir}/alm_test.npz",
                            vlm=alm_v,
                            wlm=alm_w)
Ejemplo n.º 37
0
def check_cl_sims(
        nmaps=1,
        lmax=1000,
        nside=2048,
        read_file=False,
        filename='testsky.fits',
        frequency=100,
        beam=None,
        beamP=None,
        smear=True,
        nonoise=False,
        cl_file='bf_base_cmbonly_plikHMv18_TT_lowTEB_lmax4000.minimum.theory_cl'
):

    if read_file and os.path.exists(data_path + filename):
        Tmap, Qmap, Umap = hp.read_map(data_path + filename, field=(0, 1, 2))
    else:
        if nonoise:
            Tmap, Qmap, Umap = simulate_cmb(nside=nside,
                                            lmax=lmax,
                                            save=False,
                                            smear=smear,
                                            beam=beam,
                                            beamP=beamP,
                                            cl_file=cl_file)
        else:
            Tmap, Qmap, Umap = observe_cmb_sky(save=False,
                                               nside=nside,
                                               npix=None,
                                               lmax=3000,
                                               frequency=frequency,
                                               beam=beam,
                                               beamP=beamP,
                                               cl_file=cl_file)

    Tlm = hp.map2alm(Tmap, lmax=lmax)
    Elm, Blm = hp.map2alm_spin((Qmap, Umap), 2, 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, 1. / beam, inplace=True)
        hp.sphtfunc.almxfl(Elm, 1. / beamP, inplace=True)
        hp.sphtfunc.almxfl(Blm, 1. / beamP, inplace=True)

    ls = np.arange(lmax + 1)
    factor = ls * (ls + 1.) / (2. * np.pi)

    cltt = hp.alm2cl(Tlm) * factor
    clee = hp.alm2cl(Elm) * factor
    clbb = hp.alm2cl(Blm) * factor
    clte = hp.alm2cl(Tlm, Elm) * factor

    cl = get_theory_cmb()
    ls_theory = cl[0][:lmax + 1]
    factor_theory = ls_theory * (ls_theory + 1.) / (2. * np.pi)

    cltt_theory = cl[1][:lmax + 1] * factor_theory
    clee_theory = cl[2][:lmax + 1] * factor_theory
    clbb_theory = cl[3][:lmax + 1] * factor_theory
    clte_theory = cl[4][:lmax + 1] * factor_theory

    plt.figure()
    plt.plot(ls, cltt, label='sims')
    plt.plot(ls_theory, cltt_theory, label='theory TT')
    plt.legend()

    plt.figure()
    plt.plot(ls, clte, label='sims')
    plt.plot(ls_theory, clte_theory, label='theory TE')
    plt.legend()

    plt.figure()
    plt.plot(ls, clee, label='sims')
    plt.plot(ls_theory, clee_theory, label='theory EE')
    plt.legend()

    plt.figure()
    plt.plot(ls, clbb, label='sims')
    plt.plot(ls_theory, clbb_theory, label='theory BB')
    plt.legend()
Ejemplo n.º 38
0
def measure_dlcl(mode='dl',frequency=353,
                 mask=None,
              lmax=600,lmin=40,
              put_mask=True,
              psky=70,
              mask_sources=True,
              apodization=2,
              beam=None,beamP=None,
              Imap=None,Qmap=None,Umap=None,
              Imap2=None,Qmap2=None,Umap2=None,
              fsky_correction=True):

    """
       If mode=='dl', returns D quantity = Cl *ls*(ls+1)/2./np.pi*1e12 [units: uK_CMB^2]
    """

    
    if put_mask:
        if mask is None:
            print 'reading masks...'
            mask = pf.get_planck_mask(psky=psky,
                                mask_sources=mask_sources,
                                apodization=apodization)
    else:
        mask = map.copy()*0. + 1.

    if (beam is None) or (beamP is None):
        print 'reading beams...'
        beam_file = pf.PLANCK_DATA_PATH+'HFI_RIMO_Beams-100pc_R2.00.fits'
        hdulist = pf.fits.open(beam_file)
        beam = hdulist[pf.BEAM_INDEX['{}'.format(frequency)]].data.NOMINAL[0][:lmax+1]
        beamP = hdulist[pf.BEAM_INDEX['{}P'.format(frequency)]].data.NOMINAL[0][:lmax+1]
        beam = beam[lmin:lmax+1]
        beamP = beamP[lmin:lmax+1]

    fsky = mask.sum() / len(mask)
    ls = np.arange(lmin, lmax+1)
    if mode == 'dl':
        factor =  ls * (ls+1) / (2.*np.pi) * 1e12 / fsky
    if mode == 'cl':
        factor =  1. / fsky

    if fsky_correction:
        fcfilename = pf.FGS_RESULTS_PATH + 'fskycorr_fg_psky{}_apo{}_lmax1000_TT_EE_BB.npy'.format(psky,apodization)
        if os.path.exists(fcfilename):
            fcorr = np.load(fcfilename)
            fcorr_TT = fcorr[0][lmin:lmax+1]
            fcorr_EE = fcorr[1][lmin:lmax+1]
            fcorr_BB = fcorr[2][lmin:lmax+1]
        else:
            fcorr = None
    else:
        fcorr = None
    
    
    if (Imap is None) or (Qmap is None) or (Umap is None):
        print 'reading maps...'
        mapname1 = pf.PLANCK_DATA_PATH + 'HFI_SkyMap_{}_2048_R2.02_halfmission-1.fits'.format(frequency)
        mapname2 = pf.PLANCK_DATA_PATH + 'HFI_SkyMap_{}_2048_R2.02_halfmission-2.fits'.format(frequency)
        Imap = hp.read_map(mapname1,field=0)       
        Imap2 = hp.read_map(mapname2,field=0)
        Qmap, Umap = hp.read_map(mapname1,field=(1,2))
        Qmap2, Umap2 = hp.read_map(mapname2,field=(1,2))
   
    Tlm1 = hp.map2alm(Imap*mask, lmax=lmax)
    Elm1, Blm1 = hp.map2alm_spin( (Qmap*mask, Umap*mask), 2, lmax=lmax )
    if (Imap2 is None) or (Qmap2 is None) or (Umap2 is None):
        Tlm2 = Tlm1
        Elm2 = Elm1
        Blm2 = Blm1       
    else:
        Tlm2 = hp.map2alm(Imap2*mask, lmax=lmax)
        Elm2, Blm2 = hp.map2alm_spin( (Qmap2*mask,Umap2*mask), 2, lmax=lmax )


    TT = hp.alm2cl(Tlm1, Tlm2)
    EE = hp.alm2cl(Elm1, Elm2)
    BB = hp.alm2cl(Blm1, Blm2)

    EE = EE[lmin:] * factor / beamP**2
    TT = TT[lmin:] * factor / beam**2
    BB = BB[lmin:] * factor / beamP**2
    
    TE = hp.alm2cl(Tlm1, Elm2)
    EB = hp.alm2cl(Blm1, Elm2)
    TB = hp.alm2cl(Blm1, Tlm2)
    TE = TE[lmin:] * factor / beam / beamP
    TB = TB[lmin:] * factor / beam / beamP
    EB = EB[lmin:] * factor / beamP**2

    if fcorr is not None:
        TT *= fcorr_TT
        EE *= fcorr_EE
        BB *= fcorr_BB


    return ls, TT, EE, BB, TE, TB, EB
Ejemplo n.º 39
0
def map2alm_spin(maps, spin, lmax=None, mmax=None):
    assert spin >= 0, spin
    if spin > 0:
        return hp.map2alm_spin(maps, spin, lmax=lmax, mmax=mmax)
    else:
        return -hp.map2alm(maps[0], lmax=lmax, mmax=mmax, iter=0), 0.
Ejemplo n.º 40
0
def calc_TEB(Imap_name='HFI_SkyMap_353_2048_R2.02_full.fits',
             Pmap_name='HFI_SkyMap_353_2048_R2.02_full.fits',
             nus=None,
             fwhm=0.063,
             nside=16,
             lmax=100,
             lmaps_only=False,
             filename=None):
    """Master function for computing the bispectrum TEB
    """

    # read from file if it's there
    if filename is None:
        filename = 'bispectrum_lmax{}'.format(lmax)
        if nus is not None:
            filename += '_{}-{}-{}GHz.npy'.format(nus[0], nus[1], nus[1])
        else:
            filename += '_{}'.format(Imap_name[-5])
            if Imap_name != Pmap_name:
                filename += '_{}.npy'.format(Pmap_name[-5])
            else:
                filename += '.npy'
    print 'looking for {} ...'.format(filename)
    if os.path.exists(filename) and not lmaps_only:
        bispectrum = np.load(filename, 'r')
        return bispectrum

    # compute it, if the file doesn't exist
    if nus is not None:
        Imap_name = 'HFI_SkyMap_{}_2048_R2.02_full.fits'.format(nus[0])
        Pmap_name = 'HFI_SkyMap_{}_2048_R2.02_full.fits'.format(nus[1])
        title = '$I_{%i} P^2_{%i}$  (equilateral)' % (nus[0], nus[1])

    Imap = prepare_map(Imap_name, field=0, nside_out=nside, fwhm=fwhm)
    Tlm = hp.map2alm(Imap, lmax=lmax)

    Qmap, Umap = prepare_map(Pmap_name,
                             field=(1, 2),
                             nside_out=nside,
                             fwhm=fwhm)

    Elm, Blm = hp.map2alm_spin((Qmap, Umap), 2, lmax=lmax)

    if lmax is None:
        lmax = hp.sphtfunc.Alm.getlmax(len(Tlm))
    ls, ms = hp.sphtfunc.Alm.getlm(lmax, np.arange(len(Tlm)))
    lmin = ls.min()
    mapsize = len(Imap)
    pixelsize = hp.pixelfunc.nside2pixarea(nside)

    Ylm = calc_Ylm(Imap, ls, ms)

    #return Ylm, Tlm, ls, ms
    print 'calculating Tl,El,Bl ...'

    Tl = sum_over_m(Tlm, Ylm, ls, lmax=lmax, lmin=lmin, mapsize=mapsize)
    El = sum_over_m(Elm, Ylm, ls, lmax=lmax, lmin=lmin, mapsize=mapsize)
    Bl = sum_over_m(Blm, Ylm, ls, lmax=lmax, lmin=lmin, mapsize=mapsize)

    if lmaps_only:
        return Tl, El, Bl

    hs = get_hs(lmin=lmin, lmax=lmax)

    print 'calculating bispectrum ...'
    bispectrum = calc_bispectrum(Tl,
                                 El,
                                 Bl,
                                 hs,
                                 pixelsize,
                                 lmax=lmax,
                                 lmin=lmin,
                                 mapsize=mapsize)
    clean_bispectrum_of_naninf(bispectrum, hs, inplace=True)
    np.save(filename, bispectrum)
    return bispectrum
Ejemplo n.º 41
0
def measure_dlcl(mode='dl',
                 frequency=353,
                 mask=None,
                 lmax=600,
                 lmin=40,
                 put_mask=True,
                 psky=70,
                 mask_sources=True,
                 apodization=2,
                 beam=None,
                 beamP=None,
                 Imap=None,
                 Qmap=None,
                 Umap=None,
                 Imap2=None,
                 Qmap2=None,
                 Umap2=None,
                 fsky_correction=True):
    """
       If mode=='dl', returns D quantity = Cl *ls*(ls+1)/2./np.pi*1e12 [units: uK_CMB^2]
    """

    if put_mask:
        if mask is None:
            print 'reading masks...'
            mask = pf.get_planck_mask(psky=psky,
                                      mask_sources=mask_sources,
                                      apodization=apodization)
    else:
        mask = map.copy() * 0. + 1.

    if (beam is None) or (beamP is None):
        print 'reading beams...'
        beam_file = pf.PLANCK_DATA_PATH + 'HFI_RIMO_Beams-100pc_R2.00.fits'
        hdulist = pf.fits.open(beam_file)
        beam = hdulist[pf.BEAM_INDEX['{}'.format(
            frequency)]].data.NOMINAL[0][:lmax + 1]
        beamP = hdulist[pf.BEAM_INDEX['{}P'.format(
            frequency)]].data.NOMINAL[0][:lmax + 1]
        beam = beam[lmin:lmax + 1]
        beamP = beamP[lmin:lmax + 1]

    fsky = mask.sum() / len(mask)
    ls = np.arange(lmin, lmax + 1)
    if mode == 'dl':
        factor = ls * (ls + 1) / (2. * np.pi) * 1e12 / fsky
    if mode == 'cl':
        factor = 1. / fsky

    if fsky_correction:
        fcfilename = pf.FGS_RESULTS_PATH + 'fskycorr_fg_psky{}_apo{}_lmax1000_TT_EE_BB.npy'.format(
            psky, apodization)
        if os.path.exists(fcfilename):
            fcorr = np.load(fcfilename)
            fcorr_TT = fcorr[0][lmin:lmax + 1]
            fcorr_EE = fcorr[1][lmin:lmax + 1]
            fcorr_BB = fcorr[2][lmin:lmax + 1]
        else:
            fcorr = None
    else:
        fcorr = None

    if (Imap is None) or (Qmap is None) or (Umap is None):
        print 'reading maps...'
        mapname1 = pf.PLANCK_DATA_PATH + 'HFI_SkyMap_{}_2048_R2.02_halfmission-1.fits'.format(
            frequency)
        mapname2 = pf.PLANCK_DATA_PATH + 'HFI_SkyMap_{}_2048_R2.02_halfmission-2.fits'.format(
            frequency)
        Imap = hp.read_map(mapname1, field=0)
        Imap2 = hp.read_map(mapname2, field=0)
        Qmap, Umap = hp.read_map(mapname1, field=(1, 2))
        Qmap2, Umap2 = hp.read_map(mapname2, field=(1, 2))

    Tlm1 = hp.map2alm(Imap * mask, lmax=lmax)
    Elm1, Blm1 = hp.map2alm_spin((Qmap * mask, Umap * mask), 2, lmax=lmax)
    if (Imap2 is None) or (Qmap2 is None) or (Umap2 is None):
        Tlm2 = Tlm1
        Elm2 = Elm1
        Blm2 = Blm1
    else:
        Tlm2 = hp.map2alm(Imap2 * mask, lmax=lmax)
        Elm2, Blm2 = hp.map2alm_spin((Qmap2 * mask, Umap2 * mask),
                                     2,
                                     lmax=lmax)

    TT = hp.alm2cl(Tlm1, Tlm2)
    EE = hp.alm2cl(Elm1, Elm2)
    BB = hp.alm2cl(Blm1, Blm2)

    EE = EE[lmin:] * factor / beamP**2
    TT = TT[lmin:] * factor / beam**2
    BB = BB[lmin:] * factor / beamP**2

    TE = hp.alm2cl(Tlm1, Elm2)
    EB = hp.alm2cl(Blm1, Elm2)
    TB = hp.alm2cl(Blm1, Tlm2)
    TE = TE[lmin:] * factor / beam / beamP
    TB = TB[lmin:] * factor / beam / beamP
    EB = EB[lmin:] * factor / beamP**2

    if fcorr is not None:
        TT *= fcorr_TT
        EE *= fcorr_EE
        BB *= fcorr_BB

    return ls, TT, EE, BB, TE, TB, EB
Ejemplo n.º 42
0
def g2eb(g1, g2):
    nside = gnside(g1)
    (ae, ab) = hp.map2alm_spin((g1, g2), 2)
    ke = hp.alm2map(ae, nside, pol=False)
    kb = hp.alm2map(ab, nside, pol=False)
    return ke, kb