Ejemplo n.º 1
0
    def __init__(self,clusterCosmology, \
                 fwhms=[1.5],rms_noises =[1.], freqs = [150.],lmax=8000,lknee=0.,alpha=1., \
                 dell=1.,ksz_file='input/ksz_BBPS.txt',ksz_p_file='input/ksz_p_BBPS.txt', \
                 tsz_cib_file='input/sz_x_cib_template.dat',fg=True):

        self.cc = clusterCosmology

        if (len(freqs) > 1):
            fq_mat = np.matlib.repmat(freqs, len(freqs), 1)
            fq_mat_t = np.transpose(np.matlib.repmat(freqs, len(freqs), 1))
        else:
            fq_mat = freqs
            fq_mat_t = freqs

        self.fgs = fgNoises(
            self.cc.c,
            ksz_file=ksz_file,
            ksz_p_file=ksz_p_file,
            tsz_cib_file=tsz_cib_file,
            tsz_battaglia_template_csv="data/sz_template_battaglia.csv")

        self.dell = dell
        self.evalells = np.arange(2, lmax, self.dell)
        self.N_ll_cmb = self.evalells * 0.0

        self.W_ll_cmb = np.zeros([len(self.evalells), len(np.array(freqs))])
        self.freq = freqs

        f_nu_cmb = f_nu_tsz * 0.0 + 1.

        for ii in range(len(self.evalells)):
            cmb_els = fq_mat * 0.0 + self.cc.cleefunc(self.evalells[ii])
            ## MAKE POL NOISE
            inst_noise = (old_div(
                noise_func(self.evalells[ii],
                           np.array(fwhms),
                           np.array(rms_noises),
                           lknee,
                           alpha,
                           dimensionless=False), self.cc.c['TCMBmuK']**2.))

            nells = np.diag(inst_noise)

            totfg = (self.fgs.rad_pol_ps(self.evalells[ii],fq_mat,fq_mat_t) + \
                         self.fgs.gal_dust_pol(self.evalells[ii],fq_mat,fq_mat_t) + \
                         self.fgs.gal_sync_pol(self.evalells[ii],fq_mat,fq_mat_t))

            N_ll_for_cmb = nells + totfg

            N_ll_for_cmb_inv = np.linalg.inv(N_ll_for_cmb)

            self.W_ll_cmb[ii,:] = 1./np.dot(np.transpose(f_nu_cmb),np.dot(N_ll_for_cmb_inv,f_nu_cmb)) \
                                  * np.dot(np.transpose(f_nu_cmb),N_ll_for_cmb_inv)

            self.N_ll_cmb[ii] = np.dot(
                np.transpose(self.W_ll_cmb[ii, :]),
                np.dot(N_ll_for_cmb, self.W_ll_cmb[ii, :]))
Ejemplo n.º 2
0
      def __init__(self,shape,wcs,theory,freqs,beams,noises,lknees,alphas,ellmins,ellmaxes):

          fgn = fg.fgNoises(cosmology.defaultConstants,ksz_file='/home/msyriac/repos/szar/input/ksz_BBPS.txt',
                    ksz_p_file='/home/msyriac/repos/szar/input/ksz_p_BBPS.txt',
                    tsz_cib_file='/home/msyriac/repos/szar/input/sz_x_cib_template.txt',
                    ksz_battaglia_test_csv=None,
                    tsz_battaglia_template_csv="/home/msyriac/repos/szar/input/sz_template_battaglia.csv",
                    rs_template="/home/msyriac/repos/szar/input/fiducial_scalCls_lensed_5_5.txt",
                    rsx_template="/home/msyriac/repos/szar/input/fiducial_scalCls_lensed_1_5.txt",
                    components=['tsz','cibp','cibc','radps'],lmax=20000)
        
          self.modlmap = enmap.modlmap(shape,wcs)
          modlmap = self.modlmap
          self.fgn = fgn
          lmax = self.modlmap.max()
          ells = np.arange(0,lmax,1)
          ps_cmb = theory.lCl('TT',modlmap).reshape((1,1,shape[-2],shape[-1]))
          self.ps_cmb = ps_cmb
          ps_y = fgn.tsz_template(ells).reshape((1,1,ells.size))*self.fgn.c['A_tsz']*2.*np.pi*np.nan_to_num(1./ells/(ells+1.))
          ps_cibp = (fgn.c['A_cibp'] * ((ells/fgn.c['ell0sec'])) ** 2.0 *2.*np.pi*np.nan_to_num(1./ells/(ells+1.))).reshape((1,1,ells.size))
          ps_cibc = (fgn.c['A_cibc'] * ((ells/fgn.c['ell0sec'])) ** (2.-fgn.c['n_cib']) *                   2.*np.pi*np.nan_to_num(1./ells/(ells+1.))).reshape((1,1,ells.size))
          ps_radps = (fgn.c['A_ps'] * ((ells/fgn.c['ell0sec'])) ** 2 *                   2.*np.pi*np.nan_to_num(1./ells/(ells+1.))).reshape((1,1,ells.size))
          self.cgen = maps.MapGen(shape[-2:],wcs,ps_cmb)
          self.tgen = maps.MapGen(shape[-2:],wcs,ps_y)
          self.cibpgen = maps.MapGen(shape[-2:],wcs,ps_cibp)
          self.cibcgen = maps.MapGen(shape[-2:],wcs,ps_cibc)
          self.radpsgen = maps.MapGen(shape[-2:],wcs,ps_radps)
          self.shape = shape ; self.wcs = wcs
          self.freqs = freqs
          self.kbeams = []
          self.ngens = []
          self.n2ds = []
          for ai,nu in enumerate(self.freqs):
              self.kbeams.append(maps.gauss_beam(fwhm=beams[ai],ell=self.modlmap))
              n2d = cosmology.noise_func(self.modlmap,0,noises[ai],lknee=lknees[ai],alpha=alphas[ai],dimensionless=False,TCMB=2.7255e6)
              n2d[modlmap<ellmins[ai]] = 0
              n2d[modlmap>ellmaxes[ai]] = 0
              n2dmod = n2d.copy()
              n2dmod[modlmap>ellmaxes[ai]] =  1e90
              n2dmod[modlmap<ellmins[ai]] =  1e90
              self.n2ds.append(n2dmod.copy())
              ps_noise = n2d.reshape((1,1,shape[-2],shape[-1]))
              self.ngens.append(maps.MapGen(shape[-2:],wcs,ps_noise))
          self.ellmins = ellmins
          self.ellmaxes = ellmaxes
Ejemplo n.º 3
0
    def __init__(self,clusterCosmology, fgs,fwhms=[1.5],rms_noises =[1.], freqs = [150.],lmax=8000,lknee=0.,alpha=1.,dell=1.,v3mode=-1):
        
        #Inputs
        #clusterCosmology is a class that contains cosmological parameters and power spectra.
        #fgs is a class that contains the functional forms for the foregrounds and constants

        #Options

        #initial set up for ILC
        self.cc = clusterCosmology

        #initializing the frequency matrices
        if (len(freqs) > 1):
            fq_mat   = np.matlib.repmat(freqs,len(freqs),1) 
            fq_mat_t = np.transpose(np.matlib.repmat(freqs,len(freqs),1))
        else:
            fq_mat   = freqs
            fq_mat_t = freqs

        self.fgs = fgs

    
        self.dell = dell
        #set-up ells to evaluate up to lmax
        self.evalells = np.arange(2,lmax,self.dell)
        self.N_ll_noILC = self.evalells*0.0
        self.N_ll_tsz = self.evalells*0.0
        self.N_ll_cmb = self.evalells*0.0
        self.N_ll_rsx = self.evalells*0.0
        self.N_ll_cmb_c_tsz = self.evalells*0.0
        self.N_ll_tsz_c_cmb = self.evalells*0.0
        self.N_ll_tsz_c_cib = self.evalells*0.0

        #Only for SO forecasts, including the SO atmosphere modeling
        if v3mode>-1:
            print("V3 flag enabled.")
            import szar.V3_calc_public as v3

            if v3mode <= 2:
                vfreqs = v3.Simons_Observatory_V3_LA_bands()
                freqs = vfreqs
                vbeams = v3.Simons_Observatory_V3_LA_beams()
                fwhms = vbeams

                v3lmax = self.evalells.max()
                v3dell = np.diff(self.evalells)[0]

                v3ell, N_ell_T_LA, N_ell_P_LA, Map_white_noise_levels = v3.Simons_Observatory_V3_LA_noise(sensitivity_mode=v3mode,f_sky=fsky,ell_max=v3lmax+v3dell,delta_ell=v3dell)
            elif v3mode == 3:
                vfreqs = v3.AdvACT_bands()
                freqs = vfreqs
                vbeams = v3.AdvACT_beams()
                fwhms = vbeams

                v3lmax = self.evalells.max()
                v3dell = np.diff(self.evalells)[0]
                v3ell, N_ell_T_LA, N_ell_P_LA, Map_white_noise_levels = v3.AdvACT_noise(f_sky=fsky,ell_max=v3lmax+v3dell,delta_ell=\
v3dell)

        #initializing the weighting functions for the ilc
        #thermal SZ weights
        self.W_ll_tsz = np.zeros([len(self.evalells),len(np.array(freqs))])
        #CMB weights
        self.W_ll_cmb = np.zeros([len(self.evalells),len(np.array(freqs))])
        #rayleigh scattering cross correlation weights
        self.W_ll_rsx = np.zeros([len(self.evalells),len(np.array(freqs))])
        #thermal SZ constraining the CIB weights 
        self.W_ll_tsz_c_cib = np.zeros([len(self.evalells),len(np.array(freqs))])
        #thermal SZ constraining the CMB weights 
        self.W_ll_tsz_c_cmb = np.zeros([len(self.evalells),len(np.array(freqs))])
        #CMB constraining the thermal SZ weights 
        self.W_ll_cmb_c_tsz = np.zeros([len(self.evalells),len(np.array(freqs))])
        self.freq = freqs

        #frequency functions for
        f_nu_tsz = f_nu(self.cc.c,np.array(freqs)) #tSZ
        f_nu_cmb = f_nu_tsz*0.0 + 1. #CMB
        f_nu_cib = self.fgs.f_nu_cib(np.array(freqs)) #CIB
        f_nu_rsx = self.fgs.rs_nu(np.array(freqs)) #Rayleigh Cross

        for ii in range(len(self.evalells)):

            cmb_els = fq_mat*0.0 + self.cc.clttfunc(self.evalells[ii])
            
            if v3mode < 0:
                inst_noise = ( old_div(noise_func(self.evalells[ii],np.array(fwhms),np.array(rms_noises),lknee,alpha,dimensionless=False), self.cc.c['TCMBmuK']**2.))
                nells = np.diag(inst_noise)
            elif v3mode<=2:
                ndiags = []
                for ff in range(len(freqs)):
                    inst_noise = old_div(N_ell_T_LA[ff,ii], self.cc.c['TCMBmuK']**2.)
                    ndiags.append(inst_noise)
                nells = np.diag(np.array(ndiags))
                # Adding in atmo. freq-freq correlations 
                #nells[0,1] = N_ell_T_LA[6,ii]/ self.cc.c['TCMBmuK']**2.
                #nells[1,0] = N_ell_T_LA[6,ii]/ self.cc.c['TCMBmuK']**2.
                #nells[2,3] = N_ell_T_LA[7,ii]/ self.cc.c['TCMBmuK']**2.
                #nells[3,2] = N_ell_T_LA[7,ii]/ self.cc.c['TCMBmuK']**2.
                #nells[4,5] = N_ell_T_LA[8,ii]/ self.cc.c['TCMBmuK']**2.
                #nells[5,4] = N_ell_T_LA[8,ii]/ self.cc.c['TCMBmuK']**2.

            elif v3mode==3:
                ndiags = []
                for ff in range(len(freqs)):
                    inst_noise = old_div(N_ell_T_LA[ff,ii], self.cc.c['TCMBmuK']**2.)
                    ndiags.append(inst_noise)
                nells = np.diag(np.array(ndiags))
                # Adding in atmo. freq-freq correlations
                #nells[0,1] = N_ell_T_LA[5,ii]/ self.cc.c['TCMBmuK']**2.
                #nells[1,0] = N_ell_T_LA[5,ii]/ self.cc.c['TCMBmuK']**2.
                #nells[2,3] = N_ell_T_LA[6,ii]/ self.cc.c['TCMBmuK']**2.
                #nells[3,2] = N_ell_T_LA[6,ii]/ self.cc.c['TCMBmuK']**2.
                #nells[3,4] = N_ell_T_LA[7,ii]/ self.cc.c['TCMBmuK']**2.
                #nells[4,3] = N_ell_T_LA[7,ii]/ self.cc.c['TCMBmuK']**2.

            self.N_ll_noILC[ii] = nells[3,3]

            totfg = (self.fgs.rad_pst(self.evalells[ii],fq_mat,fq_mat_t) + self.fgs.cib_p(self.evalells[ii],fq_mat,fq_mat_t) +
                      self.fgs.cib_c(self.evalells[ii],fq_mat,fq_mat_t) + self.fgs.tSZ_CIB(self.evalells[ii],fq_mat,fq_mat_t)) \
                      / self.cc.c['TCMBmuK']**2. / ((self.evalells[ii]+1.)*self.evalells[ii]) * 2.* np.pi 

            totfgrs = (self.fgs.rad_ps(self.evalells[ii],fq_mat,fq_mat_t) + self.fgs.cib_p(self.evalells[ii],fq_mat,fq_mat_t) +
                       self.fgs.cib_c(self.evalells[ii],fq_mat,fq_mat_t) + self.fgs.rs_auto(self.evalells[ii],fq_mat,fq_mat_t) + \
                       self.fgs.tSZ_CIB(self.evalells[ii],fq_mat,fq_mat_t)) \
                      / self.cc.c['TCMBmuK']**2. / ((self.evalells[ii]+1.)*self.evalells[ii] ) * 2.* np.pi 

            totfg_cib = (self.fgs.rad_ps(self.evalells[ii],fq_mat,fq_mat_t) + self.fgs.tSZ_CIB(self.evalells[ii],fq_mat,fq_mat_t)) \
                      / self.cc.c['TCMBmuK']**2. / ((self.evalells[ii]+1.)*self.evalells[ii]) * 2.* np.pi


            ksz = fq_mat*0.0 + self.fgs.ksz_temp(self.evalells[ii]) / self.cc.c['TCMBmuK']**2. / ((self.evalells[ii]+1.)*self.evalells[ii]) * 2.* np.pi

            tsz = self.fgs.tSZ(self.evalells[ii],fq_mat,fq_mat_t) / self.cc.c['TCMBmuK']**2. / ((self.evalells[ii]+1.)*self.evalells[ii]) * 2.* np.pi            

            cib = (self.fgs.cib_p(self.evalells[ii],fq_mat,fq_mat_t) + self.fgs.cib_c(self.evalells[ii],fq_mat,fq_mat_t)) \
                     / self.cc.c['TCMBmuK']**2. / ((self.evalells[ii]+1.)*self.evalells[ii]) * 2.* np.pi

            N_ll_for_tsz = nells + totfg + cmb_els + ksz 
            N_ll_for_cmb = nells + totfg + tsz + ksz 
            N_ll_for_rsx = nells + totfg + tsz + ksz + cmb_els

            N_ll_for_tsz_c_cmb = nells + totfg 
            N_ll_for_cmb_c_tsz = N_ll_for_tsz_c_cmb + ksz
            N_ll_for_tsz_c_cib = nells + totfg_cib + cmb_els + ksz

            N_ll_for_tsz_inv = np.linalg.inv(N_ll_for_tsz)
            N_ll_for_cmb_inv = np.linalg.inv(N_ll_for_cmb)
            N_ll_for_rsx_inv = np.linalg.inv(N_ll_for_rsx)
            N_ll_for_tsz_c_cmb_inv = np.linalg.inv(N_ll_for_tsz_c_cmb)
            N_ll_for_cmb_c_tsz_inv = N_ll_for_tsz_c_cmb_inv
            N_ll_for_tsz_c_cib_inv = np.linalg.inv(N_ll_for_tsz_c_cib)

            self.W_ll_tsz[ii,:]=weightcalculator(f_nu_tsz,N_ll_for_tsz)
            self.W_ll_rsx[ii,:]=weightcalculator(f_nu_rsx,N_ll_for_rsx)
            self.W_ll_cmb[ii,:]=weightcalculator(f_nu_cmb,N_ll_for_cmb)
            self.N_ll_tsz[ii] = np.dot(np.transpose(self.W_ll_tsz[ii,:]),np.dot(N_ll_for_tsz,self.W_ll_tsz[ii,:]))
            self.N_ll_cmb[ii] = np.dot(np.transpose(self.W_ll_cmb[ii,:]),np.dot(N_ll_for_cmb,self.W_ll_cmb[ii,:]))
            self.N_ll_rsx[ii] = np.dot(np.transpose(self.W_ll_rsx[ii,:]),np.dot(N_ll_for_rsx,self.W_ll_rsx[ii,:]))
            self.W_ll_tsz_c_cmb[ii,:]=constweightcalculator(f_nu_cmb,f_nu_tsz,N_ll_for_tsz_c_cmb_inv)
            self.W_ll_tsz_c_cib[ii,:]=constweightcalculator(f_nu_cib,f_nu_tsz,N_ll_for_tsz_c_cib_inv)
            self.W_ll_cmb_c_tsz[ii,:]=constweightcalculator(f_nu_tsz,f_nu_cmb,N_ll_for_cmb_c_tsz_inv)
            self.N_ll_tsz_c_cmb[ii] = np.dot(np.transpose(self.W_ll_tsz_c_cmb[ii,:]),np.dot(N_ll_for_tsz_c_cmb,self.W_ll_tsz_c_cmb[ii,:]))
            self.N_ll_cmb_c_tsz[ii] = np.dot(np.transpose(self.W_ll_cmb_c_tsz[ii,:]),np.dot(N_ll_for_cmb_c_tsz,self.W_ll_cmb_c_tsz[ii,:]))
            self.N_ll_tsz_c_cib[ii] = np.dot(np.transpose(self.W_ll_tsz_c_cib[ii,:]),np.dot(N_ll_for_tsz_c_cib,self.W_ll_tsz_c_cib[ii,:]))

            '''
Ejemplo n.º 4
0
def noiseFromConfig(Config,
                    expName,
                    TCMB=2.7255e6,
                    beamsOverride=None,
                    noisesOverride=None,
                    lkneeTOverride=None,
                    lkneePOverride=None,
                    alphaTOverride=None,
                    alphaPOverride=None,
                    tellminOverride=None,
                    pellminOverride=None,
                    tellmaxOverride=None,
                    pellmaxOverride=None):

    tellmin, tellmax = list_from_config(Config, expName, 'tellrange')
    if tellminOverride is not None: tellmin = tellminOverride
    if tellmaxOverride is not None: tellmax = tellmaxOverride
    pellmin, pellmax = list_from_config(Config, expName, 'pellrange')
    if pellminOverride is not None: pellmin = pellminOverride
    if pellmaxOverride is not None: pellmax = pellmaxOverride
    if beamsOverride is not None:
        beams = beamsOverride
    else:
        beams = list_from_config(Config, expName, 'beams')
    if noisesOverride is not None:
        noises = noisesOverride
    else:
        noises = list_from_config(Config, expName, 'noises')
    lkneeT, lkneeP = list_from_config(Config, expName, 'lknee')
    alphaT, alphaP = list_from_config(Config, expName, 'alpha')
    if lkneeTOverride is not None: lkneeT = lkneeTOverride
    if lkneePOverride is not None: lkneeP = lkneePOverride
    if alphaTOverride is not None: alphaT = alphaTOverride
    if alphaPOverride is not None: alphaP = alphaPOverride

    invnTTs = 0.
    invnEEs = 0.
    for beam, noise in zip(beams, noises):
        invnTTs += 1. / cosmo.noise_func(np.arange(tellmin, tellmax),
                                         beam,
                                         noise,
                                         lknee=lkneeT,
                                         alpha=alphaT,
                                         TCMB=TCMB,
                                         dimensionless=True)
        invnEEs += 1. / cosmo.noise_func(np.arange(pellmin, pellmax),
                                         beam,
                                         noise * np.sqrt(2.),
                                         lknee=lkneeP,
                                         alpha=alphaP,
                                         TCMB=TCMB,
                                         dimensionless=True)

    fnTT = interp1d(np.arange(tellmin, tellmax),
                    1. / invnTTs,
                    bounds_error=False,
                    fill_value=np.inf)
    fnEE = interp1d(np.arange(pellmin, pellmax),
                    1. / invnEEs,
                    bounds_error=False,
                    fill_value=np.inf)

    return fnTT, fnEE
Ejemplo n.º 5
0
    def __init__(self,
                 clusterCosmology,
                 fgs,
                 fwhms=[1.5],
                 rms_noises=[1.],
                 freqs=[150.],
                 lmax=8000,
                 lknee=0.,
                 alpha=1.,
                 dell=1.,
                 v3mode=-1):

        #ksz_file='input/ksz_BBPS.txt',ksz_p_file='input/ksz_p_BBPS.txt', \
        #tsz_cib_file='input/sz_x_cib_template.dat',fg=True):

        self.cc = clusterCosmology

        if (len(freqs) > 1):
            fq_mat = np.matlib.repmat(freqs, len(freqs), 1)
            fq_mat_t = np.transpose(np.matlib.repmat(freqs, len(freqs), 1))
        else:
            fq_mat = freqs
            fq_mat_t = freqs

        self.fgs = fgs  #fgNoises(self.cc.c,ksz_file=ksz_file,ksz_p_file=ksz_p_file,tsz_cib_file=tsz_cib_file,tsz_battaglia_template_csv="data/sz_template_battaglia.csv")

        self.dell = dell
        self.evalells = np.arange(2, lmax, self.dell)
        self.N_ll_noILC = self.evalells * 0.0
        self.N_ll_tsz = self.evalells * 0.0
        self.N_ll_cmb = self.evalells * 0.0
        self.N_ll_rsx = self.evalells * 0.0
        self.N_ll_cmb_c_tsz = self.evalells * 0.0
        self.N_ll_tsz_c_cmb = self.evalells * 0.0
        self.N_ll_tsz_c_cib = self.evalells * 0.0

        if v3mode > -1:
            print("V3 flag enabled.")
            import szar.V3_calc_public as v3

            if v3mode <= 2:
                vfreqs = v3.Simons_Observatory_V3_LA_bands()
                freqs = vfreqs
                vbeams = v3.Simons_Observatory_V3_LA_beams()
                fwhms = vbeams

                v3lmax = self.evalells.max()
                v3dell = np.diff(self.evalells)[0]

                v3ell, N_ell_T_LA, N_ell_P_LA, Map_white_noise_levels = v3.Simons_Observatory_V3_LA_noise(
                    sensitivity_mode=v3mode,
                    f_sky=fsky,
                    ell_max=v3lmax + v3dell,
                    delta_ell=v3dell)
            elif v3mode == 3:
                vfreqs = v3.AdvACT_bands()
                freqs = vfreqs
                vbeams = v3.AdvACT_beams()
                fwhms = vbeams

                v3lmax = self.evalells.max()
                v3dell = np.diff(self.evalells)[0]
                v3ell, N_ell_T_LA, N_ell_P_LA, Map_white_noise_levels = v3.AdvACT_noise(f_sky=fsky,ell_max=v3lmax+v3dell,delta_ell=\
v3dell)

        self.W_ll_tsz = np.zeros([len(self.evalells), len(np.array(freqs))])
        self.W_ll_cmb = np.zeros([len(self.evalells), len(np.array(freqs))])
        self.W_ll_rsx = np.zeros([len(self.evalells), len(np.array(freqs))])
        self.W_ll_tsz_c_cib = np.zeros(
            [len(self.evalells), len(np.array(freqs))])
        self.W_ll_tsz_c_cmb = np.zeros(
            [len(self.evalells), len(np.array(freqs))])
        self.W_ll_cmb_c_tsz = np.zeros(
            [len(self.evalells), len(np.array(freqs))])
        self.freq = freqs

        f_nu_tsz = f_nu(self.cc.c, np.array(freqs))
        f_nu_cmb = f_nu_tsz * 0.0 + 1.
        f_nu_cib = self.fgs.f_nu_cib(np.array(freqs))
        f_nu_rsx = self.fgs.rs_nu(np.array(freqs))

        for ii in range(len(self.evalells)):

            cmb_els = fq_mat * 0.0 + self.cc.clttfunc(self.evalells[ii])

            if v3mode < 0:
                inst_noise = (old_div(
                    noise_func(self.evalells[ii],
                               np.array(fwhms),
                               np.array(rms_noises),
                               lknee,
                               alpha,
                               dimensionless=False), self.cc.c['TCMBmuK']**2.))
                nells = np.diag(inst_noise)
            elif v3mode <= 2:
                ndiags = []
                for ff in range(len(freqs)):
                    inst_noise = old_div(N_ell_T_LA[ff, ii],
                                         self.cc.c['TCMBmuK']**2.)
                    ndiags.append(inst_noise)
                nells = np.diag(np.array(ndiags))
                # Adding in atmo. freq-freq correlations
                #nells[0,1] = N_ell_T_LA[6,ii]/ self.cc.c['TCMBmuK']**2.
                #nells[1,0] = N_ell_T_LA[6,ii]/ self.cc.c['TCMBmuK']**2.
                #nells[2,3] = N_ell_T_LA[7,ii]/ self.cc.c['TCMBmuK']**2.
                #nells[3,2] = N_ell_T_LA[7,ii]/ self.cc.c['TCMBmuK']**2.
                #nells[4,5] = N_ell_T_LA[8,ii]/ self.cc.c['TCMBmuK']**2.
                #nells[5,4] = N_ell_T_LA[8,ii]/ self.cc.c['TCMBmuK']**2.

            elif v3mode == 3:
                ndiags = []
                for ff in range(len(freqs)):
                    inst_noise = old_div(N_ell_T_LA[ff, ii],
                                         self.cc.c['TCMBmuK']**2.)
                    ndiags.append(inst_noise)
                nells = np.diag(np.array(ndiags))
                # Adding in atmo. freq-freq correlations
                #nells[0,1] = N_ell_T_LA[5,ii]/ self.cc.c['TCMBmuK']**2.
                #nells[1,0] = N_ell_T_LA[5,ii]/ self.cc.c['TCMBmuK']**2.
                #nells[2,3] = N_ell_T_LA[6,ii]/ self.cc.c['TCMBmuK']**2.
                #nells[3,2] = N_ell_T_LA[6,ii]/ self.cc.c['TCMBmuK']**2.
                #nells[3,4] = N_ell_T_LA[7,ii]/ self.cc.c['TCMBmuK']**2.
                #nells[4,3] = N_ell_T_LA[7,ii]/ self.cc.c['TCMBmuK']**2.

            self.N_ll_noILC[ii] = nells[3, 3]

            totfg = (self.fgs.rad_ps(self.evalells[ii],fq_mat,fq_mat_t) + self.fgs.cib_p(self.evalells[ii],fq_mat,fq_mat_t) +
                      self.fgs.cib_c(self.evalells[ii],fq_mat,fq_mat_t) + self.fgs.tSZ_CIB(self.evalells[ii],fq_mat,fq_mat_t)) \
                      / self.cc.c['TCMBmuK']**2. / ((self.evalells[ii]+1.)*self.evalells[ii]) * 2.* np.pi

            totfgrs = (self.fgs.rad_ps(self.evalells[ii],fq_mat,fq_mat_t) + self.fgs.cib_p(self.evalells[ii],fq_mat,fq_mat_t) +
                       self.fgs.cib_c(self.evalells[ii],fq_mat,fq_mat_t) + self.fgs.rs_auto(self.evalells[ii],fq_mat,fq_mat_t) + \
                       self.fgs.tSZ_CIB(self.evalells[ii],fq_mat,fq_mat_t)) \
                      / self.cc.c['TCMBmuK']**2. / ((self.evalells[ii]+1.)*self.evalells[ii] ) * 2.* np.pi

            totfg_cib = (self.fgs.rad_ps(self.evalells[ii],fq_mat,fq_mat_t) + self.fgs.tSZ_CIB(self.evalells[ii],fq_mat,fq_mat_t)) \
                      / self.cc.c['TCMBmuK']**2. / ((self.evalells[ii]+1.)*self.evalells[ii]) * 2.* np.pi

            ksz = fq_mat * 0.0 + self.fgs.ksz_temp(
                self.evalells[ii]) / self.cc.c['TCMBmuK']**2. / (
                    (self.evalells[ii] + 1.) * self.evalells[ii]) * 2. * np.pi

            tsz = self.fgs.tSZ(
                self.evalells[ii], fq_mat,
                fq_mat_t) / self.cc.c['TCMBmuK']**2. / (
                    (self.evalells[ii] + 1.) * self.evalells[ii]) * 2. * np.pi

            cib = (self.fgs.cib_p(self.evalells[ii],fq_mat,fq_mat_t) + self.fgs.cib_c(self.evalells[ii],fq_mat,fq_mat_t)) \
                     / self.cc.c['TCMBmuK']**2. / ((self.evalells[ii]+1.)*self.evalells[ii]) * 2.* np.pi

            N_ll_for_tsz = nells + totfg + cmb_els + ksz
            N_ll_for_cmb = nells + totfg + tsz + ksz
            N_ll_for_rsx = nells + totfg + tsz + ksz + cmb_els

            N_ll_for_tsz_c_cmb = nells + totfg
            N_ll_for_cmb_c_tsz = N_ll_for_tsz_c_cmb + ksz
            N_ll_for_tsz_c_cib = nells + totfg_cib + cmb_els + ksz

            N_ll_for_tsz_inv = np.linalg.inv(N_ll_for_tsz)
            N_ll_for_cmb_inv = np.linalg.inv(N_ll_for_cmb)
            N_ll_for_rsx_inv = np.linalg.inv(N_ll_for_rsx)
            N_ll_for_tsz_c_cmb_inv = np.linalg.inv(N_ll_for_tsz_c_cmb)
            N_ll_for_cmb_c_tsz_inv = N_ll_for_tsz_c_cmb_inv
            N_ll_for_tsz_c_cib_inv = np.linalg.inv(N_ll_for_tsz_c_cib)


            self.W_ll_tsz[ii,:] = 1./np.dot(np.transpose(f_nu_tsz),np.dot(N_ll_for_tsz_inv,f_nu_tsz)) \
                                  * np.dot(np.transpose(f_nu_tsz),N_ll_for_tsz_inv)
            self.W_ll_cmb[ii,:] = 1./np.dot(np.transpose(f_nu_cmb),np.dot(N_ll_for_cmb_inv,f_nu_cmb)) \
                                  * np.dot(np.transpose(f_nu_cmb),N_ll_for_cmb_inv)
            self.W_ll_rsx[ii,:] = 1./np.dot(np.transpose(f_nu_rsx),np.dot(N_ll_for_rsx_inv,f_nu_rsx)) \
                                  * np.dot(np.transpose(f_nu_rsx),N_ll_for_rsx_inv)

            self.N_ll_tsz[ii] = np.dot(
                np.transpose(self.W_ll_tsz[ii, :]),
                np.dot(N_ll_for_tsz, self.W_ll_tsz[ii, :]))
            self.N_ll_cmb[ii] = np.dot(
                np.transpose(self.W_ll_cmb[ii, :]),
                np.dot(N_ll_for_cmb, self.W_ll_cmb[ii, :]))
            self.N_ll_rsx[ii] = np.dot(
                np.transpose(self.W_ll_rsx[ii, :]),
                np.dot(N_ll_for_rsx, self.W_ll_rsx[ii, :]))

            self.W_ll_tsz_c_cmb[ii,:] = old_div((np.dot(np.transpose(f_nu_cmb),np.dot(N_ll_for_tsz_c_cmb_inv,f_nu_cmb)) \
                                             * np.dot(np.transpose(f_nu_tsz),N_ll_for_tsz_c_cmb_inv) \
                                             - np.dot(np.transpose(f_nu_tsz),np.dot(N_ll_for_tsz_c_cmb_inv,f_nu_cmb)) \
                                             * np.dot(np.transpose(f_nu_cmb),N_ll_for_tsz_c_cmb_inv)), \
                                        (np.dot(np.transpose(f_nu_cmb),np.dot(N_ll_for_tsz_c_cmb_inv,f_nu_cmb)) \
                                             * np.dot(np.transpose(f_nu_tsz),np.dot(N_ll_for_tsz_c_cmb_inv,f_nu_tsz)) \
                                             - (np.dot(np.transpose(f_nu_tsz),np.dot(N_ll_for_tsz_c_cmb_inv,f_nu_cmb)))**2))

            self.W_ll_tsz_c_cib[ii,:] = old_div((np.dot(np.transpose(f_nu_cib),np.dot(N_ll_for_tsz_c_cib_inv,f_nu_cib)) \
                                             * np.dot(np.transpose(f_nu_tsz),N_ll_for_tsz_c_cib_inv) \
                                             - np.dot(np.transpose(f_nu_tsz),np.dot(N_ll_for_tsz_c_cib_inv,f_nu_cib)) \
                                             * np.dot(np.transpose(f_nu_cib),N_ll_for_tsz_c_cib_inv)), \
                                        (np.dot(np.transpose(f_nu_cib),np.dot(N_ll_for_tsz_c_cib_inv,f_nu_cib)) \
                                             * np.dot(np.transpose(f_nu_tsz),np.dot(N_ll_for_tsz_c_cib_inv,f_nu_tsz)) \
                                             - (np.dot(np.transpose(f_nu_tsz),np.dot(N_ll_for_tsz_c_cib_inv,f_nu_cib)))**2))

            self.W_ll_cmb_c_tsz[ii,:] = old_div((np.dot(np.transpose(f_nu_tsz),np.dot(N_ll_for_cmb_c_tsz_inv,f_nu_tsz)) \
                                             * np.dot(np.transpose(f_nu_cmb),N_ll_for_cmb_c_tsz_inv) \
                                             - np.dot(np.transpose(f_nu_cmb),np.dot(N_ll_for_cmb_c_tsz_inv,f_nu_tsz)) \
                                             * np.dot(np.transpose(f_nu_tsz),N_ll_for_cmb_c_tsz_inv)), \
                                        (np.dot(np.transpose(f_nu_cmb),np.dot(N_ll_for_cmb_c_tsz_inv,f_nu_cmb)) \
                                             * np.dot(np.transpose(f_nu_tsz),np.dot(N_ll_for_cmb_c_tsz_inv,f_nu_tsz)) \
                                             - (np.dot(np.transpose(f_nu_cmb),np.dot(N_ll_for_cmb_c_tsz_inv,f_nu_tsz)))**2))

            self.N_ll_tsz_c_cmb[ii] = np.dot(
                np.transpose(self.W_ll_tsz_c_cmb[ii, :]),
                np.dot(N_ll_for_tsz_c_cmb, self.W_ll_tsz_c_cmb[ii, :]))
            self.N_ll_cmb_c_tsz[ii] = np.dot(
                np.transpose(self.W_ll_cmb_c_tsz[ii, :]),
                np.dot(N_ll_for_cmb_c_tsz, self.W_ll_cmb_c_tsz[ii, :]))
            self.N_ll_tsz_c_cib[ii] = np.dot(
                np.transpose(self.W_ll_tsz_c_cib[ii, :]),
                np.dot(N_ll_for_tsz_c_cib, self.W_ll_tsz_c_cib[ii, :]))
Ejemplo n.º 6
0
    def __init__(self,clusterCosmology,clusterDict, \
                 fwhms=[1.5],rms_noises =[1.], freqs = [150.],lmax=8000,lknee=0.,alpha=1., \
                 dell=10,pmaxN=5,numps=1000,nMax=1, \
                 ymin=1.e-14,ymax=4.42e-9,dlnY = 0.1, qmin=5., \
                 ksz_file=root_dir+'input/ksz_BBPS.txt',ksz_p_file=root_dir+'input/ksz_p_BBPS.txt', \
                 tsz_cib_file=root_dir+'input/sz_x_cib_template.txt',fg=True,tsz_cib=False,
                 tsz_battaglia_template_csv=root_dir+"input/sz_template_battaglia.csv",v3mode=-1,fsky=None):

        self.cc = clusterCosmology
        self.P0 = clusterDict['P0']
        self.xc = clusterDict['xc']
        self.al = clusterDict['al']
        self.gm = clusterDict['gm']
        self.bt = clusterDict['bt']

        self.scaling = self.cc.paramDict

        self.qmin = qmin

        lnYmin = np.log(ymin)
        lnYmax = np.log(ymax)
        self.lnY = np.arange(lnYmin, lnYmax, dlnY)
        fgs = fgNoises(self.cc.c,
                       ksz_file=ksz_file,
                       ksz_p_file=ksz_p_file,
                       tsz_cib_file=tsz_cib_file,
                       tsz_battaglia_template_csv=tsz_battaglia_template_csv)

        self.dell = dell
        self.nlinv = 0.
        self.nlinv_cmb = 0.
        self.nlinv_nofg = 0.
        self.nlinv_cmb_nofg = 0.
        self.evalells = np.arange(2, lmax, self.dell)

        if v3mode > -1:
            print("V3 flag enabled.")
            import szar.V3_calc_public as v3

            if v3mode <= 2:
                import szar.so_noise_lat_v3_1_CAND as v3_1
                lat = v3_1.SOLatV3point1(v3mode, el=50.)
                vfreqs = lat.get_bands()  # v3.Simons_Observatory_V3_LA_bands()
                print("Simons Obs")
                print("Replacing ", freqs, " with ", vfreqs)
                N_bands = len(vfreqs)
                freqs = vfreqs
                vbeams = lat.get_beams()  #v3.Simons_Observatory_V3_LA_beams()
                print("Replacing ", fwhms, " with ", vbeams)
                fwhms = vbeams

                v3lmax = self.evalells.max()
                v3dell = np.diff(self.evalells)[0]
                print("Using ", fsky, " for fsky")

                v3ell, N_ell_T_LA_full, N_ell_P_LA = lat.get_noise_curves(
                    fsky,
                    v3lmax + v3dell,
                    v3dell,
                    full_covar=True,
                    deconv_beam=True)

                N_ell_T_LA = np.diagonal(N_ell_T_LA_full).T
                Map_white_noise_levels = lat.get_white_noise(fsky)**.5

                #v3ell, N_ell_T_LA, N_ell_P_LA, Map_white_noise_levels = v3.Simons_Observatory_V3_LA_noise(sensitivity_mode=v3mode,f_sky=fsky,ell_max=v3lmax+v3dell,delta_ell=v3dell)
            elif v3mode == 3:
                vfreqs = v3.AdvACT_bands()
                print("AdvACT")
                print("Replacing ", freqs, " with ", vfreqs)
                freqs = vfreqs
                vbeams = v3.AdvACT_beams()
                print("Replacing ", fwhms, " with ", vbeams)
                fwhms = vbeams

                v3lmax = self.evalells.max()
                v3dell = np.diff(self.evalells)[0]
                print("Using ", fsky, " for fsky")

                v3ell, N_ell_T_LA, N_ell_P_LA, Map_white_noise_levels = v3.AdvACT_noise(
                    f_sky=fsky, ell_max=v3lmax + v3dell, delta_ell=v3dell)

            elif v3mode == 4:
                import szar.noise_model_190604d_public as s4
                #mode = 2
                #ncalc = s4.S4LatV1(mode, N_tels=2)
                #vfreqs = ncalc.get_bands()
                print("S4")

                v3lmax = self.evalells.max()
                v3dell = np.diff(self.evalells)[0]

                v3ell = np.arange(2, v3lmax + 1, v3dell)
                #print (v3ell, self.evalells)
                info, vfreqs, Nmatrix = s4.get_model('hires_deepwide',
                                                     'TT',
                                                     v3ell,
                                                     gal_cut=10,
                                                     deconv_beam=True)
                print("Replacing ", freqs, " with ", vfreqs)

                print("Using ", fsky, " for fsky")

                freqs = vfreqs
                #vbeams = ncalc.get_beams()
                #print("Replacing ",fwhms,  " with ", vbeams)
                fwhms = freqs * 1.  #not used

                N_ell_T_LA_full = Nmatrix
                N_ell_T_LA = np.diagonal(N_ell_T_LA_full).T
                print(N_ell_T_LA.shape)

            elif v3mode == 5:
                import szar.lat_noise_190528_w350ds4 as ccatp
                tubes = (0, 0, 0, 2, 2, 1)
                lat = ccatp.CcatLatv2(v3mode,
                                      el=50.,
                                      survey_years=4000 / 24. / 365.24,
                                      survey_efficiency=1.0,
                                      N_tubes=tubes)
                vfreqs = lat.get_bands()  # v3.Simons_Observatory_V3_LA_bands()
                print("CCATP")
                print("Replacing ", freqs, " with ", vfreqs)
                N_bands = len(vfreqs)
                freqs = vfreqs
                vbeams = lat.get_beams()  #v3.Simons_Observatory_V3_LA_beams()
                print("Replacing ", fwhms, " with ", vbeams)
                fwhms = vbeams

                v3lmax = self.evalells.max()
                v3dell = np.diff(self.evalells)[0]
                print("Using ", fsky, " for fsky")

                v3ell, N_ell_T_LA_full, N_ell_P_LA = lat.get_noise_curves(
                    fsky,
                    v3lmax + v3dell,
                    v3dell,
                    full_covar=True,
                    deconv_beam=True)

                N_ell_T_LA = np.diagonal(N_ell_T_LA_full).T
                Map_white_noise_levels = lat.get_white_noise(fsky)**.5

            if v3mode >= 7:

                usemode = v3mode - 6
                print("V3mode ", usemode)

                vfreqs = v3.Simons_Observatory_V3_LA_bands()
                print("Simons Obs")
                print("Replacing ", freqs, " with ", vfreqs)
                freqs = vfreqs
                vbeams = v3.Simons_Observatory_V3_LA_beams()
                print("Replacing ", fwhms, " with ", vbeams)
                fwhms = vbeams

                v3lmax = self.evalells.max()
                v3dell = np.diff(self.evalells)[0]
                print("Using ", fsky, " for fsky")

                v3ell, N_ell_T_LA, N_ell_P_LA, Map_white_noise_levels = v3.Simons_Observatory_V3_LA_noise(
                    sensitivity_mode=usemode,
                    f_sky=fsky,
                    ell_max=v3lmax + v3dell,
                    delta_ell=v3dell)

            assert np.all(v3ell == self.evalells)

        # pl = io.Plotter(yscale='log',xlabel='l',ylabel='D_l')

        for ii, (freq, fwhm, noise) in enumerate(zip(freqs, fwhms,
                                                     rms_noises)):
            freq_fac = (f_nu(self.cc.c, freq))**2

            if v3mode > -1:
                inst_noise = N_ell_T_LA[ii] / self.cc.c['TCMBmuK']**2.
            else:
                inst_noise = (noise_func(self.evalells,
                                         fwhm,
                                         noise,
                                         lknee,
                                         alpha,
                                         dimensionless=False) /
                              self.cc.c['TCMBmuK']**2.)

            # pl.add(self.evalells,inst_noise*self.evalells**2.,color="C"+str(ii))
            # pl.add(self.evalells,N_ell_T_LA[ii]*self.evalells**2./ self.cc.c['TCMBmuK']**2.,color="C"+str(ii),ls="--")

            nells = self.cc.clttfunc(self.evalells) + inst_noise
            self.nlinv_nofg += old_div((freq_fac), nells)
            self.nlinv_cmb_nofg += (old_div(1., inst_noise))

            totfg = (fgs.rad_ps(self.evalells,freq,freq) + fgs.cib_p(self.evalells,freq,freq) + \
                      fgs.cib_c(self.evalells,freq,freq) + fgs.ksz_temp(self.evalells)) \
                      / self.cc.c['TCMBmuK']**2. / ((self.evalells+1.)*self.evalells) * 2.* np.pi
            nells += totfg

            if (tsz_cib):
                tszcib = fgs.tSZ_CIB(self.evalells,freq,freq) \
                         / self.cc.c['TCMBmuK']**2. / ((self.evalells+1.)*self.evalells) * 2.* np.pi
                nells += tszcib

            self.nlinv += old_div((freq_fac), nells)
            self.nlinv_cmb += (old_div(1., (inst_noise + totfg)))

        # pl.add(self.evalells,self.cc.clttfunc(self.evalells)*self.evalells**2.,color='k',lw=3)
        # pl.done(io.dout_dir+"v3comp.png")

        self.nl_old = (1. / self.nlinv)
        self.nl_cmb = (1. / self.nlinv_cmb)
        self.nl_nofg = (1. / self.nlinv_nofg)
        self.nl_cmb_nofg = (1. / self.nlinv_cmb_nofg)

        f_nu_tsz = f_nu(self.cc.c, np.array(freqs))

        if (len(freqs) > 1):
            fq_mat = repmat(freqs, len(freqs), 1)
            fq_mat_t = np.transpose(repmat(freqs, len(freqs), 1))
        else:
            fq_mat = np.array(freqs)
            fq_mat_t = np.array(freqs)

        self.nl = self.evalells * 0.0

        for ii in range(len(self.evalells)):
            cmb_els = fq_mat * 0.0 + self.cc.clttfunc(self.evalells[ii])

            if v3mode < 0:
                inst_noise = (old_div(
                    noise_func(self.evalells[ii],
                               np.array(fwhms),
                               np.array(rms_noises),
                               lknee,
                               alpha,
                               dimensionless=False), self.cc.c['TCMBmuK']**2.))
                nells = np.diag(inst_noise)
            elif v3mode <= 2:
                nells = N_ell_T_LA_full[:, :, ii] / self.cc.c['TCMBmuK']**2.

                #corr_pairs = [(0,1),(2,3),(4,5)]

                #N_ell_LA_T  = N_ell_LA_T_full[range(N_bands),range(N_bands)]
                #N_ell_LA_Tx = [N_ell_LA_T_full[i,j] for i,j in corr_pairs]
                #N_ell_LA_P  = N_ell_LA_P_full[range(N_bands),range(N_bands)]
                #N_ell_LA_Px = [N_ell_LA_P_full[i,j] for i,j in corr_pairs]

                #nells = N_ell_LA_Tx

                #ndiags = []
                #for ff in range(len(freqs)):
                #    inst_noise = old_div(N_ell_T_LA[ff,ii], self.cc.c['TCMBmuK']**2.)
                #    ndiags.append(inst_noise)
                #nells = np.diag(np.array(ndiags))
                ## Adding in atmo. freq-freq correlations
                #nells[0,1] = old_div(N_ell_T_LA[6,ii], self.cc.c['TCMBmuK']**2.)
                #nells[1,0] = old_div(N_ell_T_LA[6,ii], self.cc.c['TCMBmuK']**2.)
                #nells[2,3] = old_div(N_ell_T_LA[7,ii], self.cc.c['TCMBmuK']**2.)
                #nells[3,2] = old_div(N_ell_T_LA[7,ii], self.cc.c['TCMBmuK']**2.)
                #nells[4,5] = old_div(N_ell_T_LA[8,ii], self.cc.c['TCMBmuK']**2.)
                #nells[5,4] = old_div(N_ell_T_LA[8,ii], self.cc.c['TCMBmuK']**2.)
            elif v3mode == 3:
                ndiags = []
                for ff in range(len(freqs)):
                    inst_noise = old_div(N_ell_T_LA[ff, ii],
                                         self.cc.c['TCMBmuK']**2.)
                    ndiags.append(inst_noise)
                nells = np.diag(np.array(ndiags))
                # Adding in atmo. freq-freq correlations
                nells[0, 1] = old_div(N_ell_T_LA[5, ii],
                                      self.cc.c['TCMBmuK']**2.)
                nells[1, 0] = old_div(N_ell_T_LA[5, ii],
                                      self.cc.c['TCMBmuK']**2.)
                nells[2, 3] = old_div(N_ell_T_LA[6, ii],
                                      self.cc.c['TCMBmuK']**2.)
                nells[3, 2] = old_div(N_ell_T_LA[6, ii],
                                      self.cc.c['TCMBmuK']**2.)
                nells[3, 4] = old_div(N_ell_T_LA[7, ii],
                                      self.cc.c['TCMBmuK']**2.)
                nells[4, 3] = old_div(N_ell_T_LA[7, ii],
                                      self.cc.c['TCMBmuK']**2.)
            elif v3mode == 4:
                nells = N_ell_T_LA_full[:, :, ii] / self.cc.c['TCMBmuK']**2.
            elif v3mode == 5:
                nells = N_ell_T_LA_full[:, :, ii] / self.cc.c['TCMBmuK']**2.
            elif v3mode >= 7:
                ndiags = []
                for ff in range(len(freqs)):
                    inst_noise = old_div(N_ell_T_LA[ff, ii],
                                         self.cc.c['TCMBmuK']**2.)
                    ndiags.append(inst_noise)
                nells = np.diag(np.array(ndiags))
                # Adding in atmo. freq-freq correlations
                nells[0, 1] = old_div(N_ell_T_LA[6, ii],
                                      self.cc.c['TCMBmuK']**2.)
                nells[1, 0] = old_div(N_ell_T_LA[6, ii],
                                      self.cc.c['TCMBmuK']**2.)
                nells[2, 3] = old_div(N_ell_T_LA[7, ii],
                                      self.cc.c['TCMBmuK']**2.)
                nells[3, 2] = old_div(N_ell_T_LA[7, ii],
                                      self.cc.c['TCMBmuK']**2.)
                nells[4, 5] = old_div(N_ell_T_LA[8, ii],
                                      self.cc.c['TCMBmuK']**2.)
                nells[5, 4] = old_div(N_ell_T_LA[8, ii],
                                      self.cc.c['TCMBmuK']**2.)

            totfg = (fgs.rad_ps(self.evalells[ii],fq_mat,fq_mat_t) + fgs.cib_p(self.evalells[ii],fq_mat,fq_mat_t)
                     + fgs.cib_c(self.evalells[ii],fq_mat,fq_mat_t)) \
                     / self.cc.c['TCMBmuK']**2. / ((self.evalells[ii]+1.)*self.evalells[ii]) * 2.* np.pi

            if (tsz_cib):
                totfg += fgs.tSZ_CIB(self.evalells[ii], fq_mat,
                                     fq_mat_t) / self.cc.c['TCMBmuK']**2. / (
                                         (self.evalells[ii] + 1.) *
                                         self.evalells[ii]) * 2. * np.pi
                totfg += fgs.tSZ(
                    self.evalells[ii], fq_mat, fq_mat_t
                ) / self.cc.c['TCMBmuK']**2. / (
                    (self.evalells[ii] + 1.) * self.evalells[ii]
                ) * 2. * np.pi / 2.  # factor of two accounts for resolved halos

            ksz = fq_mat * 0.0 + fgs.ksz_temp(
                self.evalells[ii]) / self.cc.c['TCMBmuK']**2. / (
                    (self.evalells[ii] + 1.) * self.evalells[ii]) * 2. * np.pi

            nells += totfg + cmb_els + ksz

            self.nl[ii] = old_div(
                1., (np.dot(np.transpose(f_nu_tsz),
                            np.dot(np.linalg.inv(nells), f_nu_tsz))))

        # from orphics.io import Plotter
        # pl = Plotter(yscale='log',xlabel='l',ylabel='D_l')
        # pl.add(self.evalells,self.nl*self.evalells**2.)
        # ells = np.arange(2,3000,1)
        # pl.add(ells,self.cc.clttfunc(ells)*ells**2.)
        # pl.done("nltt.png")
        # sys.exit()

        self.fg = fg

        c = self.xc
        alpha = self.al
        beta = self.bt
        gamma = self.gm
        p = lambda x: old_div(1., (((c * x)**gamma) * ((1. + (
            (c * x)**alpha))**(old_div((beta - gamma), alpha)))))

        pmaxN = pmaxN
        numps = numps
        pzrange = np.linspace(-pmaxN, pmaxN, numps)
        self.g = lambda x: np.trapz(p(np.sqrt(pzrange**2. + x**2.)), pzrange,
                                    np.diff(pzrange))

        self.gxrange = np.linspace(0., nMax, numps)
        self.gint = np.array([self.g(x) for x in self.gxrange])

        self.gnorm_pre = np.trapz(self.gxrange * self.gint, self.gxrange)
Ejemplo n.º 7
0
Archivo: ilc.py Proyecto: JerreYjz/szar
    def __init__(self,
                 clusterCosmology,
                 fgs,
                 fwhms=[1.5],
                 rms_noises=[1.],
                 freqs=[150.],
                 lmax=8000,
                 lknee=0.,
                 alpha=1.,
                 dell=1.,
                 v3mode=-1,
                 fsky=None):

        #Inputs
        #clusterCosmology is a class that contains cosmological parameters and power spectra.
        #fgs is a class that contains the functional forms for the foregrounds and constants

        #Options

        #initial set up for ILC
        self.cc = clusterCosmology

        #initializing the frequency matrices

        self.fgs = fgs

        self.dell = dell
        #set-up ells to evaluate up to lmax
        self.evalells = np.arange(2, lmax, self.dell)
        self.N_ll_tsz = self.evalells * 0.0
        self.N_ll_cmb = self.evalells * 0.0
        self.N_ll_rsx = self.evalells * 0.0
        self.N_ll_rsxEE = self.evalells * 0.0
        self.N_ll_rsx_NoFG = self.evalells * 0.0
        self.N_ll_cmb_c_tsz = self.evalells * 0.0
        self.N_ll_tsz_c_cmb = self.evalells * 0.0
        self.N_ll_tsz_c_cib = self.evalells * 0.0

        #Only for SO forecasts, including the SO atmosphere modeling
        if v3mode > -1:
            print("V3 flag enabled.")
            import szar.V3_calc_public as v3
            import szar.so_noise_lat_v3_1_CAND as v3_1

            if v3mode <= 2:
                lat = v3_1.SOLatV3point1(v3mode, el=50.)
                vfreqs = lat.get_bands()  # v3.Simons_Observatory_V3_LA_bands()
                print("Simons Obs")
                print("Replacing ", freqs, " with ", vfreqs)
                N_bands = len(vfreqs)
                freqs = vfreqs
                vbeams = lat.get_beams()  #v3.Simons_Observatory_V3_LA_beams()
                print("Replacing ", fwhms, " with ", vbeams)
                fwhms = vbeams

                v3lmax = self.evalells.max()
                v3dell = np.diff(self.evalells)[0]
                print("Using ", fsky, " for fsky")

                v3ell, N_ell_T_LA_full, N_ell_P_LA = lat.get_noise_curves(
                    fsky,
                    v3lmax + v3dell,
                    v3dell,
                    full_covar=True,
                    deconv_beam=True)

                N_ell_T_LA = np.diagonal(N_ell_T_LA_full).T
                Map_white_noise_levels = lat.get_white_noise(fsky)**.5

            #if v3mode <= 2:
            #    vfreqs = v3.Simons_Observatory_V3_LA_bands()
            #    freqs = vfreqs
            #    vbeams = v3.Simons_Observatory_V3_LA_beams()
            #    fwhms = vbeams

            #    v3lmax = self.evalells.max()
            #    v3dell = np.diff(self.evalells)[0]

            #    v3ell, N_ell_T_LA, N_ell_P_LA, Map_white_noise_levels = v3.Simons_Observatory_V3_LA_noise(sensitivity_mode=v3mode,f_sky=fsky,ell_max=v3lmax+v3dell,delta_ell=v3dell)
            elif v3mode == 3:
                vfreqs = v3.AdvACT_bands()
                freqs = vfreqs
                vbeams = v3.AdvACT_beams()
                fwhms = vbeams

                v3lmax = self.evalells.max()
                v3dell = np.diff(self.evalells)[0]
                v3ell, N_ell_T_LA, N_ell_P_LA, Map_white_noise_levels = v3.AdvACT_noise(f_sky=fsky,ell_max=v3lmax+v3dell,delta_ell=\
v3dell)
            elif v3mode == 5:
                import szar.lat_noise_190528_w350ds4 as ccatp
                tubes = (0, 0, 0, 2, 2, 1)
                lat = ccatp.CcatLatv2(v3mode,
                                      el=50.,
                                      survey_years=4000 / 24. / 365.24,
                                      survey_efficiency=1.0,
                                      N_tubes=tubes)
                vfreqs = lat.get_bands()  # v3.Simons_Observatory_V3_LA_bands()
                print("CCATP")
                print("Replacing ", freqs, " with ", vfreqs)
                N_bands = len(vfreqs)
                freqs = vfreqs
                vbeams = lat.get_beams()  #v3.Simons_Observatory_V3_LA_beams()
                print("Replacing ", fwhms, " with ", vbeams)
                fwhms = vbeams

                v3lmax = self.evalells.max()
                v3dell = np.diff(self.evalells)[0]
                print("Using ", fsky, " for fsky")

                v3ell, N_ell_T_LA_full, N_ell_P_LA = lat.get_noise_curves(
                    fsky,
                    v3lmax + v3dell,
                    v3dell,
                    full_covar=True,
                    deconv_beam=True)

                N_ell_T_LA = np.diagonal(N_ell_T_LA_full).T
                Map_white_noise_levels = lat.get_white_noise(fsky)**.5
        #print(freqs)
        if (len(freqs) > 1):
            fq_mat = np.matlib.repmat(freqs, len(freqs), 1)
            fq_mat_t = np.transpose(np.matlib.repmat(freqs, len(freqs), 1))
        else:
            fq_mat = freqs
            fq_mat_t = freqs
        #print(fq_mat)
        #initializing the weighting functions for the ilc
        #thermal SZ weights
        self.W_ll_tsz = np.zeros([len(self.evalells), len(np.array(freqs))])
        #CMB weights
        self.W_ll_cmb = np.zeros([len(self.evalells), len(np.array(freqs))])
        #rayleigh scattering cross correlation weights
        self.W_ll_rsx = np.zeros([len(self.evalells), len(np.array(freqs))])
        #rayleigh scattering cross E-mode of CMB correlation weights
        self.W_ll_rsxEE = np.zeros([len(self.evalells), len(np.array(freqs))])
        #rayleigh scattering cross correlation weights NO foregrounds
        self.W_ll_rsx_NF = np.zeros([len(self.evalells), len(np.array(freqs))])
        #thermal SZ constraining the CIB weights
        self.W_ll_tsz_c_cib = np.zeros(
            [len(self.evalells), len(np.array(freqs))])
        #thermal SZ constraining the CMB weights
        self.W_ll_tsz_c_cmb = np.zeros(
            [len(self.evalells), len(np.array(freqs))])
        #CMB constraining the thermal SZ weights
        self.W_ll_cmb_c_tsz = np.zeros(
            [len(self.evalells), len(np.array(freqs))])
        self.freq = freqs

        #frequency functions for
        f_nu_tsz = f_nu(self.cc.c, np.array(freqs))  #tSZ
        f_nu_cmb = f_nu_tsz * 0.0 + 1.  #CMB
        f_nu_cib = self.fgs.f_nu_cib(np.array(freqs))  #CIB
        f_nu_rsx = self.fgs.rs_nu(np.array(freqs))  #Rayleigh Cross

        for ii in range(len(self.evalells)):

            cmb_els = fq_mat * 0.0 + self.cc.clttfunc(self.evalells[ii])
            cmb_ee = fq_mat * 0.0 + self.cc.cleefunc(self.evalells[ii])
            if v3mode < 0:
                inst_noise = (old_div(
                    noise_func(self.evalells[ii],
                               np.array(fwhms),
                               np.array(rms_noises),
                               lknee,
                               alpha,
                               dimensionless=False), self.cc.c['TCMBmuK']**2.))
                nells = np.diag(inst_noise)
            elif v3mode <= 2:
                nells = N_ell_T_LA_full[:, :, ii] / self.cc.c['TCMBmuK']**2.

            elif v3mode == 3:
                ndiags = []
                for ff in range(len(freqs)):
                    inst_noise = old_div(N_ell_T_LA[ff, ii],
                                         self.cc.c['TCMBmuK']**2.)
                    ndiags.append(inst_noise)
                nells = np.diag(np.array(ndiags))

            elif v3mode == 5:
                nells = N_ell_T_LA_full[:, :, ii] / self.cc.c['TCMBmuK']**2.


            totfg = (self.fgs.rad_ps(self.evalells[ii],fq_mat,fq_mat_t) + self.fgs.cib_p(self.evalells[ii],fq_mat,fq_mat_t) +
                      self.fgs.cib_c(self.evalells[ii],fq_mat,fq_mat_t) + self.fgs.tSZ_CIB(self.evalells[ii],fq_mat,fq_mat_t)) \
                      / self.cc.c['TCMBmuK']**2. / ((self.evalells[ii]+1.)*self.evalells[ii]) * 2.* np.pi

            totfgrs = (self.fgs.rad_ps(self.evalells[ii],fq_mat,fq_mat_t) + self.fgs.cib_p(self.evalells[ii],fq_mat,fq_mat_t) +
                       self.fgs.cib_c(self.evalells[ii],fq_mat,fq_mat_t) + self.fgs.rs_auto(self.evalells[ii],fq_mat,fq_mat_t) + \
                       self.fgs.tSZ_CIB(self.evalells[ii],fq_mat,fq_mat_t)) \
                      / self.cc.c['TCMBmuK']**2. / ((self.evalells[ii]+1.)*self.evalells[ii] ) * 2.* np.pi

            fgrspol = (self.fgs.rad_pol_ps(self.evalells[ii],fq_mat,fq_mat_t)+self.fgs.gal_dust_pol(self.evalells[ii],fq_mat,fq_mat_t)+self.fgs.gal_sync_pol(self.evalells[ii],fq_mat,fq_mat_t)) \
                      / self.cc.c['TCMBmuK']**2. / ((self.evalells[ii]+1.)*self.evalells[ii] ) * 2.* np.pi

            totfg_cib = (self.fgs.rad_ps(self.evalells[ii],fq_mat,fq_mat_t) + self.fgs.tSZ_CIB(self.evalells[ii],fq_mat,fq_mat_t)) \
                      / self.cc.c['TCMBmuK']**2. / ((self.evalells[ii]+1.)*self.evalells[ii]) * 2.* np.pi

            ksz = fq_mat * 0.0 + self.fgs.ksz_temp(
                self.evalells[ii]) / self.cc.c['TCMBmuK']**2. / (
                    (self.evalells[ii] + 1.) * self.evalells[ii]) * 2. * np.pi

            tsz = self.fgs.tSZ(
                self.evalells[ii], fq_mat,
                fq_mat_t) / self.cc.c['TCMBmuK']**2. / (
                    (self.evalells[ii] + 1.) * self.evalells[ii]) * 2. * np.pi

            cib = (self.fgs.cib_p(self.evalells[ii],fq_mat,fq_mat_t) + self.fgs.cib_c(self.evalells[ii],fq_mat,fq_mat_t)) \
                     / self.cc.c['TCMBmuK']**2. / ((self.evalells[ii]+1.)*self.evalells[ii]) * 2.* np.pi

            N_ll_for_tsz = nells + totfg + cmb_els + ksz
            N_ll_for_cmb = nells + totfg + tsz + ksz
            N_ll_for_rsx = nells + totfg + tsz + ksz + cmb_els
            N_ll_for_rsxEE = nells + fgrspol + cmb_ee

            N_ll_for_tsz_c_cmb = nells + totfg
            N_ll_for_cmb_c_tsz = N_ll_for_tsz_c_cmb + ksz
            N_ll_for_tsz_c_cib = nells + totfg_cib + cmb_els + ksz

            N_ll_for_tsz_inv = np.linalg.inv(N_ll_for_tsz)
            N_ll_for_cmb_inv = np.linalg.inv(N_ll_for_cmb)
            N_ll_for_rsx_inv = np.linalg.inv(N_ll_for_rsx)
            N_ll_for_tsz_c_cmb_inv = np.linalg.inv(N_ll_for_tsz_c_cmb)
            N_ll_for_cmb_c_tsz_inv = N_ll_for_tsz_c_cmb_inv
            N_ll_for_tsz_c_cib_inv = np.linalg.inv(N_ll_for_tsz_c_cib)

            N_ll_noFG = nells

            self.W_ll_tsz[ii, :] = weightcalculator(f_nu_tsz, N_ll_for_tsz)
            self.W_ll_rsx[ii, :] = weightcalculator(f_nu_rsx, N_ll_for_rsx)
            self.W_ll_rsx_NF[ii, :] = weightcalculator(f_nu_rsx, N_ll_noFG)
            self.W_ll_rsxEE[ii, :] = weightcalculator(f_nu_rsx, N_ll_for_rsxEE)
            self.W_ll_cmb[ii, :] = weightcalculator(f_nu_cmb, N_ll_for_cmb)
            self.N_ll_tsz[ii] = np.dot(
                np.transpose(self.W_ll_tsz[ii, :]),
                np.dot(N_ll_for_tsz, self.W_ll_tsz[ii, :]))
            self.N_ll_cmb[ii] = np.dot(
                np.transpose(self.W_ll_cmb[ii, :]),
                np.dot(N_ll_for_cmb, self.W_ll_cmb[ii, :]))
            self.N_ll_rsx[ii] = np.dot(
                np.transpose(self.W_ll_rsx[ii, :]),
                np.dot(N_ll_for_rsx, self.W_ll_rsx[ii, :]))
            self.N_ll_rsxEE[ii] = np.dot(
                np.transpose(self.W_ll_rsxEE[ii, :]),
                np.dot(N_ll_for_rsxEE, self.W_ll_rsxEE[ii, :]))
            self.N_ll_rsx_NoFG[ii] = np.dot(
                np.transpose(self.W_ll_rsx_NF[ii, :]),
                np.dot(N_ll_noFG, self.W_ll_rsx_NF[ii, :]))
            self.W_ll_tsz_c_cmb[ii, :] = constweightcalculator(
                f_nu_cmb, f_nu_tsz, N_ll_for_tsz_c_cmb_inv)
            self.W_ll_tsz_c_cib[ii, :] = constweightcalculator(
                f_nu_cib, f_nu_tsz, N_ll_for_tsz_c_cib_inv)
            self.W_ll_cmb_c_tsz[ii, :] = constweightcalculator(
                f_nu_tsz, f_nu_cmb, N_ll_for_cmb_c_tsz_inv)
            self.N_ll_tsz_c_cmb[ii] = np.dot(
                np.transpose(self.W_ll_tsz_c_cmb[ii, :]),
                np.dot(N_ll_for_tsz_c_cmb, self.W_ll_tsz_c_cmb[ii, :]))
            self.N_ll_cmb_c_tsz[ii] = np.dot(
                np.transpose(self.W_ll_cmb_c_tsz[ii, :]),
                np.dot(N_ll_for_cmb_c_tsz, self.W_ll_cmb_c_tsz[ii, :]))
            self.N_ll_tsz_c_cib[ii] = np.dot(
                np.transpose(self.W_ll_tsz_c_cib[ii, :]),
                np.dot(N_ll_for_tsz_c_cib, self.W_ll_tsz_c_cib[ii, :]))
            '''