def load_ivar_curvedsky(aobj):

    if aobj.ivar == 'vc':
        if '16' in aobj.wind:
            return hp.fitsfunc.read_map(aobj.fivar16, verbose=False)
        elif '15' in aobj.wind:
            return hp.fitsfunc.read_map(aobj.fivar15, verbose=False)
        else:
            sys.exit('ivar is not well defined')
    elif aobj.ivar == 'vd':
        ivar = enmap.read_map(aobj.fivarvd)
        return enmap.to_healpix(ivar[0], nside=aobj.nside)
    elif aobj.ivar == 'v3':
        ivar = enmap.read_map(local.init_analysis_params(qid='boss_d03').fivar)
        return enmap.to_healpix(ivar[0], nside=aobj.nside)
    elif aobj.ivar == 'V3':
        ivar = enmap.read_map(
            local.init_analysis_params(qid='boss_d03').fivar)**2
        return enmap.to_healpix(ivar[0], nside=aobj.nside)
    elif aobj.ivar == 'noivar':
        return 1.
    elif aobj.ivar == 'base':
        ivar = enmap.read_map(aobj.fivar)
        return enmap.to_healpix(ivar[0], nside=aobj.nside)
    else:
        sys.exit('ivar is not well defined')
def map2alm_core_spin(Qmap, Umap, aobj, amask):

    Qmap_fl = remove_lxly(Qmap, lmin=aobj.clmin, lmax=aobj.lmax)
    Umap_fl = remove_lxly(Umap, lmin=aobj.clmin, lmax=aobj.lmax)
    Qmap_hp = enmap.to_healpix(Qmap_fl, nside=aobj.nside)
    Umap_hp = enmap.to_healpix(Umap_fl, nside=aobj.nside)
    Ealm, Balm = curvedsky.utils.hp_map2alm_spin(aobj.nside, aobj.lmax,
                                                 aobj.lmax, 2, Qmap_hp * amask,
                                                 Umap_hp * amask)

    return Ealm, Balm
Exemple #3
0
def cmbmap2alm(i, mtype, p, f, r):

    fmap = enmap.read_map(
        f.imap[mtype][i])  # load flatsky K-space combined map

    # FT
    print('2D ell filtering in F-space and assign to healpix map')
    alm = enmap.fft(fmap)

    # remove some Fourier modes
    shape, wcs = fmap.shape, fmap.wcs
    kmask = mask_kspace(shape, wcs, lmin=p.lcut, lmax=4000, lxcut=90, lycut=50)
    alm[kmask < 0.5] = 0

    # alm -> map
    fmap = enmap.ifft(alm).real

    # transform cmb map to healpix
    hpmap = enmap.to_healpix(fmap, nside=p.nside)

    # from map to alm
    hpmap = r.w * hpmap  # masking
    #hp.fitsfunc.write_map(f.omap[mtype][i],hpmap,overwrite=True)
    alm = curvedsky.utils.hp_map2alm(p.nside, p.lmax, p.lmax, hpmap)

    print("save to file")
    pickle.dump((alm),
                open(f.alm[mtype][i], "wb"),
                protocol=pickle.HIGHEST_PROTOCOL)
def map2alm_core(fmap, aobj, amask):

    fmap_fl = remove_lxly(fmap, lmin=aobj.clmin, lmax=aobj.lmax)
    hpmap = enmap.to_healpix(fmap_fl, nside=aobj.nside)
    alm = curvedsky.utils.hp_map2alm(aobj.nside, aobj.lmax, aobj.lmax,
                                     hpmap * amask)

    return alm
def load_window_curvedsky(aobj,
                          survey_mask=True,
                          with_ivar=True,
                          ivar_norm=False):
    # load combined window function in curvedsky (survey mask x inverse variance x point source mask)

    if survey_mask:
        # Load the default survey mask from soapack and project it onto healpix grids
        # (note that the mask is already apodized)

        if aobj.fltr == 'cinv' or (aobj.fltr == 'none' and aobj.wind == 'base'
                                   and aobj.ascale == 1.):
            # cinv case only loads survey_mask to make a boundary mask later
            if aobj.qid in local.qid_all:  # for individual array, freqs
                mask = load_survey_mask(aobj.qid)
                mask = enmap.to_healpix(mask,
                                        nside=aobj.nside)  # to healpix map
            else:
                sys.exit(
                    'there are only survey masks for individual array maps')

        # Load other types of mask, including additional apodization (not equals to 1deg) and restricted boundary
        else:
            print('load custum mask', aobj.amask)
            mask = hp.fitsfunc.read_map(aobj.amask, verbose=False)
    else:
        mask = 1.

    # Load the default ivar map
    if with_ivar and aobj.ivar != 'noivar' and aobj.fltr != 'cinv':
        ivar = load_ivar_curvedsky(aobj)
        if ivar_norm:
            ivar = ivar / np.max(ivar)
    else:
        ivar = 1.

    # load ptsr mask for day
    if aobj.ptsr == '':
        ptsr = 1.
    else:
        print('load ptsr mask', aobj.fptsr)
        ptsr = hp.fitsfunc.read_map(aobj.fptsr, verbose=False)

    return mask * ivar * ptsr
# # Read maps and compute power spectra

# ## TileC CMB maps, reconvolved to 1.4'

# In[6]:

# TileC CMB: D56 + BN
pathMap = "./output/cmb_map/tilec_pact_cmbksz/" + "tilec_reconv14_map.fits"
pathCarFullMask = "./output/cmb_map/tilec_pact_cmbksz/" + "mask_foot_planck_ps_car.fits"

# read maps
tMap = enmap.read_map(pathMap)
tMask = enmap.read_map(pathCarFullMask)

# convert maps to healpix
hTMap = enmap.to_healpix(tMap)
hTMask = enmap.to_healpix(tMask)
del tMap, tMask

# plot masked map
hp.mollview(hTMap * hTMask, title='TileC BN + D56')

# compute power spectra
lCen, ClTilec, sClTilec = powerSpectrum(hTMap,
                                        mask=hTMask,
                                        theory=[flensedTT, ftotal],
                                        fsCl=None,
                                        nBins=201,
                                        lRange=None,
                                        plot=False,
                                        path=None,
def wiener_cinv_core(qids,
                     wqid,
                     white=False,
                     kwargs_ov={
                         'overwrite': False,
                         'verbose': True
                     },
                     kwargs_cmb={},
                     kwargs_cinv={}):

    # parameter
    vb = kwargs_ov['verbose']

    # specify output filename
    aobj = local.init_analysis_params(qid=wqid, **kwargs_cmb)

    # input CMB data
    Aobj = {q: local.init_analysis_params(qid=q, **kwargs_cmb) for q in qids}

    mn = len(qids)
    bl = np.zeros((mn, aobj.lmax + 1))
    Nij = np.zeros((1, mn, aobj.npix))
    mask = {}
    inl = np.ones((1, mn, aobj.lmax + 1))

    for i, q in enumerate(qids):

        # beam
        bl[i, :] = beam_func(Aobj[q].lmax, Aobj[q].qid)

        # binary mask
        mask[q] = load_window_curvedsky(Aobj[q], with_ivar=False)
        mask[q][mask[q] != 0] = 1.

        # inv noise covariance
        #os.system('echo "'+q+',constructing noise covariance" >> test.txt')
        if vb: print(q, 'constructing noise covariance')

        Nvar = load_ivar_curvedsky(Aobj[q])

        if white:

            Nij[0,
                i, :] = mask[q] * Nvar / np.max(Nvar) / local.qid_wnoise(q)**2

        else:

            if q in local.boss_d:
                # averaged noise spectrum at S16 region
                bobj = local.init_analysis_params(qid=q,
                                                  fltr='none',
                                                  wind='com16',
                                                  ivar='base',
                                                  ptsr=aobj.ptsr)
            if q in local.boss_n or q in local.s_16_d:
                bobj = local.init_analysis_params(qid=q,
                                                  fltr='none',
                                                  wind='base',
                                                  ivar='base',
                                                  ptsr=aobj.ptsr)

            Nl = np.loadtxt(bobj.fscl['n'], unpack=True)[1]
            inl[0, i, 2:] = 1. / (Nl[2:] * bl[i, 2:]**2)
            Nvar[Nvar <= 0] = 1e-60
            Nij[0, i, :] = mask[q] * np.sqrt(
                Nvar / np.max(Nvar))  #/ local.qid_wnoise(q)

        del Nvar

    # temperature map
    T = np.zeros((1, mn, aobj.npix))

    for rlz in aobj.rlz:

        if misctools.check_path(aobj.falm['c']['T'][rlz], **kwargs_ov):
            continue

        #os.system('echo "'+str(rlz)+',loading temperature obs map" >> test.txt')
        if rlz == 0:
            for i, q in enumerate(qids):
                if vb: print(rlz, q, 'loading temperature obs map')
                Tc = enmap.read_map(Aobj[q].fmap['s'][rlz])[0]
                T[0, i, :] = mask[q] * enmap.to_healpix(remove_lxly(
                    Tc, lmin=aobj.clmin, lmax=aobj.lmax),
                                                        nside=aobj.nside)
        else:
            for i, q in enumerate(qids):
                if vb: print(rlz, q, 'loading temperature sim map')
                Ts = enmap.read_map(Aobj[q].fmap['s'][rlz])[0]
                Tn = enmap.read_map(Aobj[q].fmap['n'][rlz])[0]
                T[0, i, :] = mask[q] * enmap.to_healpix(remove_lxly(
                    Ts + Tn, lmin=aobj.clmin, lmax=aobj.lmax),
                                                        nside=aobj.nside)

        # cinv
        if vb: print('cinv filtering')
        #os.system('echo "cinv filter" >> test.txt')
        if white:
            Tlm = curvedsky.cninv.cnfilter_freq(1,
                                                mn,
                                                aobj.nside,
                                                aobj.lmax,
                                                aobj.lcl[0:1, :],
                                                bl,
                                                Nij,
                                                T,
                                                verbose=kwargs_ov['verbose'],
                                                **kwargs_cinv)
        else:
            Tlm = curvedsky.cninv.cnfilter_freq(1,
                                                mn,
                                                aobj.nside,
                                                aobj.lmax,
                                                aobj.lcl[0:1, :],
                                                bl,
                                                Nij,
                                                T,
                                                verbose=kwargs_ov['verbose'],
                                                inl=inl,
                                                **kwargs_cinv)
        pickle.dump((Tlm[0, :, :]),
                    open(aobj.falm['c']['T'][rlz], "wb"),
                    protocol=pickle.HIGHEST_PROTOCOL)
    aobj = local.init_analysis_params(qid='boss_d03', ascale=3., wtype=wtype)

    if wtype in ['com15', 'com16']:
        amask = hp.fitsfunc.read_map(aobj.amask)
        amask[amask != 0] = 1.
    else:
        amask = 1.

    if wtype == 'com16':
        qids = local.qid_all
        fivar = aobj.fivar16
    if wtype == 'com15':
        qids = local.boss_dn
        fivar = aobj.fivar15
    if wtype == 'comdy':
        qids = local.day_all
        fivar = aobj.fivarvd

    var = 0.
    for q in qids:
        ivars = enmap.read_map(local.init_analysis_params(qid=q).fivar)
        ivars = enmap.to_healpix(ivars[0], nside=aobj.nside)
        ivars[ivars == 0] = np.inf
        var += 1 / ivars
    var[var == 0] = np.inf
    ivar = np.nan_to_num(1. / var) * amask
    print(np.min(ivar), np.max(ivar))

    hp.fitsfunc.write_map(fivar, ivar, overwrite=True)
Exemple #9
0
def interface(qid,run=['norm','qrec','n0','mean','aps'],mean_sub=True,kwargs_ov={},kwargs_cmb={},kwargs_qrec={}):

    aobj = local.init_analysis_params(qid=qid,**kwargs_cmb)
    
    if qid == 'diff_dn':
        aobj_c = local.init_analysis_params(qid='comb_dn',**kwargs_cmb)
    else:
        # same as aobj
        aobj_c = local.init_analysis_params(qid=qid,**kwargs_cmb)

    if aobj.fltr == 'cinv':

        # filter
        ep  = 1e-30
        
        # noise
        if aobj.qid in local.wqids:
            nl = 0.
            qids = local.get_subqids(aobj.qid)
            for q in qids:
                if q in local.boss_d:
                    bobj = local.init_analysis_params(qid=q,fltr='none',wind='com16',ivar='base')
                if q in local.boss_n or q in local.s_16_d:
                    bobj = local.init_analysis_params(qid=q,fltr='none',wind='base',ivar='base')
                nl += 1. / ( np.loadtxt(bobj.fscl['n'],unpack=True)[1] + ep )
            nl = 1./(nl+ep)
            # load wfactors
            print('computing W factor')
            W = 1.
            for q in qids:
                W *= 1. - enmap.to_healpix(tools_cmb.load_survey_mask(q),nside=aobj.nside)
            wn = np.zeros(5)
            wn[0] = np.mean(1.-W)
            wn[:] = wn[0]
            print('W factor:',wn[0])
        else:
            # white
            bl  = tools_cmb.beam_func(aobj.lmax,aobj.qid)
            nl  = ( local.qid_wnoise(qid) / bl )**2
            wn = tools_cmb.get_wfactors([aobj.qid],aobj.ascale,wind=aobj.wind,ivar=aobj.ivar,ptsr=aobj.ptsr,fltr=aobj.fltr)[aobj.qid]
            wn[:] = wn[0]
        
        # corrected factors
        cnl = aobj.lcl[0,:] + nl
        #wcl = (np.loadtxt(aobj.fscl['c'])).T[1] # wiener-fileterd CMB aps
        #ocl, ifl = quad_func.cinv_empirical_fltr(aobj.lcl[0,:],wcl,cnl)
        #ocl = np.reshape( aobj.lcl[0,:]**2/(wcl+ep) ,(1,aobj.lmax+1) )
        ocl = np.reshape( cnl ,(1,aobj.lmax+1) )
        ifl = np.reshape( aobj.lcl[0,:], (1,aobj.lmax+1) )

        #wn = tools_cmb.get_wfactors([aobj.qid],1.,wind='base',ptsr=aobj.ptsr,fltr='cinv')[aobj.qid]

    else:

        # load wfactors
        wn = tools_cmb.get_wfactors([aobj.qid],aobj.ascale,wind=aobj.wind,ivar=aobj.ivar,ptsr=aobj.ptsr,fltr=aobj.fltr)[aobj.qid]

        # filter
        ocl = np.ones((3,aobj_c.lmax+1))
        ocl[0,:] = (np.loadtxt(aobj_c.fscl['c'])).T[1]
        ifl = ocl.copy()
    

    dirs = local.data_directory()
    qobj = quad_func.reconstruction(dirs['local'],aobj.ids,rlz=aobj.rlz,stag=aobj.stag,run=run,wn=wn,lcl=aobj.lcl,ocl=ocl,ifl=ifl,falm=aobj.falm['c'],**kwargs_ov,**kwargs_qrec)

    # Aps of reconstructed phi
    if 'aps' in run:
        aps(qobj,aobj.rlz,aobj.fiklm,wn,mean_sub=mean_sub)
Exemple #10
0
#!/usr/bin/env python
# coding: utf-8

import numpy as np, healpy as hp, curvedsky as cs, sys, local, tools_cmb

sys.path.append("/global/homes/t/toshiyan/Work/Lib/actlib/actsims/")
sys.path.append("/global/homes/t/toshiyan/Work/Lib/actlib/soapack/")
sys.path.append("/global/homes/t/toshiyan/Work/Lib/actlib/")
sys.path.append("/global/homes/t/toshiyan/Work/Lib/actlib/orphics/")
sys.path.append("/global/homes/t/toshiyan/Work/Lib/actlib/tilec/")
from pixell import enmap
from soapack import interfaces

#qids = ['boss_dcom15','boss_com15','boss_dcom16','boss_com']

aobj = {q: local.init_analysis_params(qid=q, ascale=ap) for q in local.qid_all}

mask = {}

for q in qids:
    mask_iv = tools_cmb.load_mask(q, with_ivar=False)
    mask[q] = enmap.to_healpix(mask_iv, nside=2048)

if wqid == 'boss_dcom15':
    mask['boss_d03']
    hp.fitsfunc.write_map(aobj_dcom15.amask, mask, overwrite=True)
    
if 'apod' in run:

    #qids = ['boss_d01']
    qids = ['boss_01','boss_d02','boss_02','boss_d03','boss_03','boss_d04','boss_04','s16_d01','s16_d02','s16_d03']
    
    # for different apodization scales
    for ap in [1.,2.,3.,4.,5.]:
    #or ap in [1.,5.]:

        aobj = {q: local.init_analysis_params(qid=q,ascale=ap) for q in qids}

        for q in qids:
        
            mask_iv = tools_cmb.load_survey_mask(q)
            mask = enmap.to_healpix(mask_iv[0],nside=2048)
    
            if ap != 1.:
                mask[mask!=0] = 1. # binary mask
                amask = cs.utils.apodize(2048,mask,aobj[q].ascale)
            else:
                amask = mask.copy()
        
            print(q,np.average(amask),np.max(amask))        
            hp.fitsfunc.write_map(aobj[q].amask,amask,overwrite=True)


if 'cinv' in run:

    # for different apodization scales
    for wqid in local.wqids:
Exemple #12
0
def transmask(nside, ffmask, frmask):  # transform to fullsky mask
    hpmap = enmap.to_healpix(enmap.read_map(ffmask), nside=nside)
    print(np.max(hpmap))
    hpmap = hpmap / np.max(hpmap)
    hpmap[hpmap < 0] = 0.
    hp.fitsfunc.write_map(frmask, hpmap, overwrite=True)