def get_analytic_covariance(param_dict, freqarr, nl_dic=None, bl_dic=None, ignore_fg=[], which_spec='TT', pol_frac_per_cent_dust=0.02, pol_frac_per_cent_radio=0.03, pol_frac_per_cent_tsz=0., pol_frac_per_cent_ksz=0., include_gal=0): #ignore_fg = foreground terms that must be ignored possible_ignore_fg = ['cmb', 'tsz', 'ksz', 'radio', 'dust'] if len(ignore_fg) > 0: if not all([currfg in possible_ignore_fg for currfg in ignore_fg]): print( '\n\t Alert: Elements of ignore_fg should be one of the following: %s\n\n' % (np.array2string(np.asarray(possible_ignore_fg)))) sys.exit() el, cl_cmb = fg.get_foreground_power_spt('CMB', freq1=param_dict['freq0'], freq2=param_dict['freq0']) el, cl_ksz = fg.get_foreground_power_spt('kSZ', freq1=param_dict['freq0'], freq2=param_dict['freq0']) if which_spec == 'EE': cl_ksz = cl_ksz * pol_frac_per_cent_ksz**2. if which_spec == 'TE': cl_ksz = cl_ksz * 0. cl_dic = {} cl_ori = np.zeros(len(el)) for freq1 in freqarr: for freq2 in freqarr: if (freq2, freq1) in cl_dic: cl_dic[(freq1, freq2)] = cl_dic[(freq2, freq1)] continue #get dust el, cl_dg_po, cl_dg_clus = fg.get_cl_dust( freq1, freq2, freq0=param_dict['freq0'], fg_model=param_dict['fg_model'], spec_index_dg_po=param_dict['spec_index_dg_po'], spec_index_dg_clus=param_dict['spec_index_dg_clus'], Tcib=param_dict['Tcib']) if which_spec == 'EE': cl_dg_po = cl_dg_po * pol_frac_per_cent_dust**2. cl_dg_clus = cl_dg_clus * pol_frac_per_cent_dust**2. elif which_spec == 'TE': cl_dg_po = cl_dg_po * 0. cl_dg_clus = cl_dg_clus * 0. #get tsz el, cl_tsz = fg.get_cl_tsz(freq1, freq2, freq0=param_dict['freq0'], fg_model=param_dict['fg_model']) if which_spec == 'EE': cl_tsz = cl_tsz * pol_frac_per_cent_tsz**2. elif which_spec == 'TE': cl_tsz = cl_tsz * 0. #get radio el, cl_radio = fg.get_cl_radio( freq1, freq2, freq0=param_dict['freq0'], fg_model=param_dict['fg_model'], spec_index_rg=param_dict['spec_index_rg']) if which_spec == 'EE': cl_radio = cl_radio * pol_frac_per_cent_radio**2. elif which_spec == 'TE': cl_radio = cl_radio * 0. cl = np.copy(cl_ori) if 'cmb' not in ignore_fg: cl = cl + np.copy(cl_cmb) if 'ksz' not in ignore_fg: #print('ksz') cl = cl + cl_ksz if 'tsz' not in ignore_fg: #print('tsz') cl = cl + cl_tsz if 'radio' not in ignore_fg: #print('radio') cl = cl + cl_radio if 'dust' not in ignore_fg: #print('dust') cl = cl + cl_dg_po + cl_dg_clus if include_gal: # and not pol: #get galactic dust and sync el, cl_gal_dust = fg.get_cl_galactic(param_dict, 'dust', freq1, freq2, which_spec, bl_dic=bl_dic, el=el) el, cl_gal_sync = fg.get_cl_galactic(param_dict, 'sync', freq1, freq2, which_spec, bl_dic=bl_dic, el=el) if (0): #not pol: loglog(cl_dg_po + cl_dg_clus, label=r'EG: %s, %s' % (freq1, freq2)) loglog(cl_gal_dust, label=r'Dust: %s, %s' % (freq1, freq2)) title(r'%s' % (param_dict['which_gal_mask'])) loglog(cl_gal_sync, label=r'Sync: %s, %s' % (freq1, freq2)) title(r'%s' % (param_dict['which_gal_mask'])) #cl = cl + cl_gal_dust + cl_gal_sync #loglog(cl); legend(loc=1) ylim(1e-5, 1e3) #show();sys.exit() xlim(20, param_dict['lmax']) ylim(1e-8, 1e6) show() sys.exit() cl = cl + cl_gal_dust cl = cl + cl_gal_sync #make sure cl start from el=0 rather than el=10 which is the default for SPT G15 results lmin = min(el) cl = np.concatenate((np.zeros(lmin), cl)) #noise auto power spectrum if nl_dic is not None: if (freq1, freq2) in nl_dic: nl = nl_dic[(freq1, freq2)] else: nl = nl_dic[freq1] if freq1 != freq2: nl = np.copy(nl) * 0. if len(cl) > len(nl): cl = cl[:len(nl)] elif len(cl) < len(nl): nl = nl[:len(cl)] el = np.arange(len(cl)) else: nl = np.zeros(len(cl)) #20191116 - fix this: there must be noise correlation in case of atmospheric noise print( '\n\n\t\tfix me: there must be noise correlation in case of atmospheric noise' ) sys.exit() if which_spec != 'TE': cl = cl + nl else: nl_dummy = misc.get_nl(0.1, el, 0.1) cl = cl + nl_dummy cl[np.isnan(cl)] = 0. cl[np.isinf(cl)] = 0. if (0): #which_spec == 'TT': loglog(cl) title('%s - %s' % (freq1, freq2)) show() sys.exit() cl_dic[(freq1, freq2)] = cl return el, cl_dic
def get_analytic_covariance(param_dict, freqarr, el=None, nl_dic=None, bl_dic=None, ignore_fg=[], which_spec='TT', pol_frac_per_cent_dust=0.02, pol_frac_per_cent_radio=0.03, pol_frac_per_cent_tsz=0., pol_frac_per_cent_ksz=0., include_gal=0, null_highfreq_radio=1, reduce_radio_power_150=None, reduce_tsz_power=None, reduce_cib_power=None, return_fg_spectra=True, max_nl_value=5000.): #ignore_fg = foreground terms that must be ignored possible_ignore_fg = [ 'cmb', 'tsz', 'ksz', 'radio', 'dust', 'noise', 'tsz_cib' ] if len(ignore_fg) > 0: if 'cmb' in ignore_fg: ignore_fg.append('ksz') if not all([currfg in possible_ignore_fg for currfg in ignore_fg]): print( '\n\t Alert: Elements of ignore_fg should be one of the following: %s\n\n' % (np.array2string(np.asarray(possible_ignore_fg)))) sys.exit() ###el_, cl_cmb = fg.get_foreground_power_spt('CMB', freq1 = param_dict['freq0'], freq2 = param_dict['freq0']) camb_file = '%s/%s' % (param_dict['data_folder'], param_dict['Dlfile_len']) el_ = np.loadtxt(camb_file, usecols=[0]) dl_camb = np.loadtxt(camb_file, usecols=[1, 2, 3, 4]) Tcmb = 2.73 cl_camb = (Tcmb**2. * dl_camb * 2 * np.pi) / (el_[:, None] * (el_[:, None] + 1)) cl_camb *= 1e12 cl_TT, cl_EE, cl_BB, cl_TE = cl_camb.T cl_cmb_dic = {'TT': cl_TT, 'EE': cl_EE, 'BB': cl_BB, 'TE': cl_TE} cl_cmb = cl_cmb_dic[which_spec] if el is None: el = np.copy(el_) cl_cmb = np.interp(el, el_, cl_cmb) if 'cmb' in ignore_fg: #get CMB spectra cl_cmb = cl_cmb * 0. if 'ksz' not in ignore_fg: el_, cl_ksz = fg.get_foreground_power_spt('kSZ', freq1=param_dict['freq0'], freq2=param_dict['freq0']) if which_spec == 'EE': cl_ksz = cl_ksz * pol_frac_per_cent_ksz**2. if which_spec == 'TE': cl_ksz = cl_ksz * 0. cl_ksz = np.interp(el, el_, cl_ksz) #interpolate to get cl for required els else: cl_ksz = np.zeros(len(el)) cl_dic = {} if return_fg_spectra: fg_cl_dic = {} for freq1 in freqarr: for freq2 in freqarr: if (freq2, freq1) in cl_dic: cl_dic[(freq1, freq2)] = cl_dic[(freq2, freq1)] continue if 'tsz' not in ignore_fg: #get tsz el_, cl_tsz = fg.get_cl_tsz(freq1, freq2, freq0=param_dict['freq0'], fg_model=param_dict['fg_model'], reduce_tsz_power=reduce_tsz_power) if which_spec == 'EE': cl_tsz = cl_tsz * pol_frac_per_cent_tsz**2. elif which_spec == 'TE': cl_tsz = cl_tsz * 0. cl_tsz = np.interp( el, el_, cl_tsz) #interpolate to get cl for required els else: cl_tsz = np.zeros(len(el)) if 'radio' not in ignore_fg: #get radio el_, cl_radio = fg.get_cl_radio( freq1, freq2, freq0=param_dict['freq0'], fg_model=param_dict['fg_model'], spec_index_rg=param_dict['spec_index_rg'], null_highfreq_radio=null_highfreq_radio, reduce_radio_power_150=reduce_radio_power_150) if which_spec == 'EE': cl_radio = cl_radio * pol_frac_per_cent_radio**2. elif which_spec == 'TE': cl_radio = cl_radio * 0. cl_radio = np.interp( el, el_, cl_radio) #interpolate to get cl for required els else: cl_radio = np.zeros(len(el)) if 'dust' not in ignore_fg: #get CIB el_, cl_dg_po, cl_dg_clus = fg.get_cl_dust( freq1, freq2, freq0=param_dict['freq0'], fg_model=param_dict['fg_model'], spec_index_dg_po=param_dict['spec_index_dg_po'], spec_index_dg_clus=param_dict['spec_index_dg_clus'], Tcib=param_dict['Tcib'], reduce_cib_power=reduce_cib_power) cl_dust = cl_dg_po + cl_dg_clus cl_dust[np.isnan(cl_dust)] = 0. if which_spec == 'EE': cl_dust = cl_dust * pol_frac_per_cent_dust**2. elif which_spec == 'TE': cl_dust = cl_dust * 0. cl_dust = np.interp( el, el_, cl_dust) #interpolate to get cl for required els else: cl_dust = np.zeros(len(el)) #galaxy if include_gal: # and not pol: #get galactic dust and sync el_, cl_gal_dust = fg.get_cl_galactic(param_dict, 'dust', freq1, freq2, which_spec, el=el, bl_dic=bl_dic) el_, cl_gal_sync = fg.get_cl_galactic(param_dict, 'sync', freq1, freq2, which_spec, el=el, bl_dic=bl_dic) cl_gal_dust = np.interp( el, el_, cl_gal_dust) #interpolate to get cl for required els cl_gal_sync = np.interp( el, el_, cl_gal_sync) #interpolate to get cl for required els else: cl_gal_dust = np.zeros(len(el)) cl_gal_sync = np.zeros(len(el)) cl = cl_cmb + cl_ksz + cl_tsz + cl_radio + cl_dust + cl_gal_dust + cl_gal_sync #noise auto power spectrum if nl_dic is not None: if (freq1, freq2) in nl_dic: nl = nl_dic[(freq1, freq2)] else: nl = nl_dic[freq1] if freq1 != freq2: nl = np.copy(nl) * 0. #interpolate to get cl for required els el_ = np.arange(len(nl)) nl = np.interp(el, el_, nl) if ( 1 ): #20201121: remove very large numbers because of beam deconvolution ini_nl = np.median(nl[:100]) end_nl = np.median(nl[-100:]) if end_nl > ini_nl: #this implies beam deconvolution has made end nl pretty large #having end_nl pretty large introduces covariance inversion issues badinds = np.where(nl >= max_nl_value)[0] nl[badinds] = max_nl_value #print(ini_nl, end_nl) #clf();loglog(nl); title('%s: %s,%s' %(which_spec, freq1,freq2)); show() else: nl = np.zeros(len(el)) if 'noise' not in ignore_fg: if which_spec != 'TE': cl = cl + np.copy(nl) if return_fg_spectra: if 'cmb' not in ignore_fg: if 'cmb' not in fg_cl_dic: fg_cl_dic['cmb'] = {} fg_cl_dic['cmb'][(freq1, freq2)] = fg_cl_dic['cmb'][( freq2, freq1)] = cl_cmb if 'ksz' not in ignore_fg: if 'ksz' not in fg_cl_dic: fg_cl_dic['ksz'] = {} fg_cl_dic['ksz'][(freq1, freq2)] = fg_cl_dic['ksz'][( freq2, freq1)] = cl_ksz if 'tsz' not in ignore_fg: if 'tsz' not in fg_cl_dic: fg_cl_dic['tsz'] = {} fg_cl_dic['tsz'][(freq1, freq2)] = fg_cl_dic['tsz'][( freq2, freq1)] = cl_tsz if 'radio' not in ignore_fg: if 'radio' not in fg_cl_dic: fg_cl_dic['radio'] = {} fg_cl_dic['radio'][(freq1, freq2)] = fg_cl_dic['radio'][( freq2, freq1)] = cl_radio if 'dust' not in ignore_fg: if 'cib' not in fg_cl_dic: fg_cl_dic['cib'] = {} fg_cl_dic['cib'][(freq1, freq2)] = fg_cl_dic['cib'][( freq2, freq1)] = cl_dust if 'noise' not in ignore_fg and which_spec != 'TE': if 'noise' not in fg_cl_dic: fg_cl_dic['noise'] = {} fg_cl_dic['noise'][(freq1, freq2)] = fg_cl_dic['noise'][( freq2, freq1)] = nl if include_gal: if 'galdust' not in fg_cl_dic: fg_cl_dic['galdust'] = {} fg_cl_dic['galsync'] = {} fg_cl_dic['galdust'][(freq1, freq2)] = fg_cl_dic['galdust'][( freq2, freq1)] = cl_gal_dust fg_cl_dic['galsync'][(freq1, freq2)] = fg_cl_dic['galsync'][( freq2, freq1)] = cl_gal_sync cl[np.isnan(cl)] = 0. cl[np.isinf(cl)] = 0. cl_dic[(freq1, freq2)] = cl if return_fg_spectra: return el, cl_dic, fg_cl_dic else: return el, cl_dic
30: [27, 27], 40: [39, 39], 95: [93, 93], 145: [145, 145], 220: [225, 225], 270: [278, 278], } ''' searchstr_gal_dust = 'sims_from_others/CUmilta/ampmod_maps/Ampmod_map_dust_lmax_5200_freq_xxx_rel0000.fits' searchstr_gal_sync = 'sims_from_others/CUmilta/ampmod_maps/Ampmod_map_sync_lmax_5200_freq_xxx_rel0000.fits' searchstr_extragal_cmb = 'sims_from_others/YOmori/cmbs4/mdpl2_sky_cmbs4_lcmbNG_tszNGmasked_kszNG_cibNG_xxxghz.fits' ############################################################################## if produce_radio_skies:#foreground only to create Gaussian realisations of radio sky cl_radio_dic = {} for freq1 in sorted(freq_dic): el, cl_radio = fg.get_cl_radio(freq1, freq1, freq0 = freq0, fg_model = fg_model, spec_index_rg = spec_index_rg) cl_radio = np.concatenate( (np.zeros(lmin), cl_radio) ) el = np.arange(len(cl_radio)) cl_radio = cl_radio[:lmax] el = el[:lmax] cl_radio_dic[freq1] = cl_radio ############################################################################## npix = H.nside2npix(nside_out) if produce_radio_skies: random_seed = 100 for freq in sorted(freq_dic): if freq != reqd_freq: continue