Example #1
0
    def load_alm_fg(self, set_idx, sim_idx, fgflux='sehgal09'):
        print("loading fg alm")
        alm_fg90_150 = None
        if fgflux == 'sehgal09':
            alm_file_postfix = '' if self.eulers == (
                0, 0, 0) else '_rot_{}_{}_{}'.format(
                    self.eulers[0], self.eulers[1], self.eulers[2])
            #fg_file_temp   = os.path.join(self.signal_path, 'fullskyCOMBINED_NODUST_f{}_set%02d_%05d%s.fits' %(set_idx, 0, alm_file_postfix))
            fg_file_temp = os.path.join(
                self.signal_path,
                'fullskyCOMBINED_NODUST_f{}_set%02d_%05d%s.fits' %
                (set_idx, set_idx, alm_file_postfix))
            print(fg_file_temp)

            alm_fg090 = np.complex128(
                hp.fitsfunc.read_alm(fg_file_temp.format('%03d' % 90),
                                     hdu=(1)))
            alm_fg150 = np.complex128(
                hp.fitsfunc.read_alm(fg_file_temp.format('%03d' % 148),
                                     hdu=(1)))
            alm_fg90_150 = np.stack([alm_fg090, alm_fg150])
        elif fgflux == 'sehgal09_gauss':
            ## generate GRF FGs matching sehgal09 flux
            fg_file = os.path.join(actsim_root,
                                   '../data/Sehgal09FG_nodust_15mJycut.dat')
            seed = (set_idx, 0, 1, sim_idx, 0)
            fg_power = powspec.read_spectrum(fg_file, ncol=3, expand='row')
            alm_fg90_150 = curvedsky.rand_alm_healpy(fg_power, seed=seed)
        else:
            assert (False)
        return alm_fg90_150
Example #2
0
    def __init__(self,
                 cmb_type='LensedUnabberatedCMB',
                 dobeam=True,
                 add_foregrounds=True,
                 apply_window=True,
                 max_cached=1,
                 model="act_mr3",
                 apply_rotation=False,
                 alpha_map=None,
                 eulers=None,
                 camb_unlensed_file=None,
                 camb_lensed_file=None,
                 lmax=8000):
        """
        model: The name of an implemented soapack datamodel
        eulers            : rotate alm by euler angles (psi, theta, phi) (i.e (0,15,0) ->  maps are rotated by 15 deg in theta) 
        """
        super(GaussGen, self).__init__(cmb_type=cmb_type, dobeam=dobeam, add_foregrounds=add_foregrounds, apply_window=apply_window, max_cached=max_cached, model=model,\
                apply_rotation=apply_rotation, alpha_map=alpha_map)
        self.cmb_types = ['UnlensedCMB', 'LensedUnabberatedCMB']
        self.signal_path = None
        self.lmax = lmax
        assert (cmb_type in self.cmb_types)

        if not (camb_unlensed_file):
            self.camb_unlensed_file = os.path.join(
                actsim_root, '../inputParams/cosmo2017_10K_acc3_scalCls.dat')
        if not (camb_lensed_file):
            self.camb_lensed_file = os.path.join(
                actsim_root, '../inputParams/cosmo2017_10K_acc3_lensedCls.dat')

        self.ps_unlen = powspec.read_camb_scalar(self.camb_unlensed_file)[0]
        self.ps_len = powspec.read_spectrum(self.camb_lensed_file)
Example #3
0
 def test_sim_slice(self):
     ps = powspec.read_spectrum(DATA_PREFIX+"test_scalCls.dat")[:1,:1]
     test_res_arcmin = 10.0
     lmax = 2000
     fact = 2.
     shape,wcs = enmap.fullsky_geometry(res=np.deg2rad(test_res_arcmin/60.),proj='car')
     omap = curvedsky.rand_map(shape, wcs, ps,lmax=lmax)
     ofunc = lambda ishape,iwcs: fact*enmap.extract(omap,ishape,iwcs)
     nmap = reproject.populate(shape,wcs,ofunc,maxpixy = 400,maxpixx = 400)
     assert np.all(np.isclose(nmap/omap,2.))
Example #4
0
def test_sim_slice():
    path = os.path.dirname(enmap.__file__) + "/../tests/"
    ps = powspec.read_spectrum(path + "data/test_scalCls.dat")[:1, :1]
    test_res_arcmin = 10.0
    lmax = 2000
    fact = 2.
    shape, wcs = enmap.fullsky_geometry(res=np.deg2rad(test_res_arcmin / 60.),
                                        proj='car')
    omap = curvedsky.rand_map(shape, wcs, ps, lmax=lmax)
    ofunc = lambda ishape, iwcs: fact * enmap.extract(omap, ishape, iwcs)
    nmap = reproject.populate(shape, wcs, ofunc, maxpixy=400, maxpixx=400)
    assert np.all(np.isclose(nmap / omap, 2.))
Example #5
0
 def load_alm_fg(self, set_idx, sim_idx, fgflux):
     print("loading fg alm")
     seed = seedgen.get_fg_seed(set_idx, sim_idx, fgflux)
     if fgflux == "15mjy":
         print("loading FG with 15mJy fluxcut")
         fg_file = os.path.join(actsim_root, '../data/fg.dat')
     elif fgflux == "100mjy":
         print("loading FG with 100mJy fluxcut")
         fg_file = os.path.join(actsim_root, '../data/highflux_fg.dat')
     elif fgflux == "quick-srcfree":
         print("loading FG with srcfree fg")
         fg_file = os.path.join(actsim_root,
                                '../data/quick_srcfree_combined_d56.dat')
     else:
         assert (False)  ### :o
     fg_power = powspec.read_spectrum(fg_file, ncol=3, expand='row')
     alm_fg90_150 = curvedsky.rand_alm_healpy(fg_power,
                                              seed=seed)  #, lmax=lmax_sg)
     return alm_fg90_150
window_dir = "windows"
mcm_dir = "mcms"
noise_data_dir = "sim_data/noise_ps"
specDir = "spectra"

lmax_simu = lmax

pspy_utils.create_directory(specDir)

spectra = ["TT", "TE", "TB", "ET", "BT", "EE", "EB", "BE", "BB"]
spin_pairs = ["spin0xspin0", "spin0xspin2", "spin2xspin0", "spin2xspin2"]

all_freqs = [freq for exp in experiments for freq in d["freqs_%s" % exp]]
ncomp = 3
ps_cmb = powspec.read_spectrum(d["clfile"])[:ncomp, :ncomp]

if include_fg == True:
    l, ps_fg = maps_to_params_utils.get_foreground_matrix(
        fg_dir, fg_components, all_freqs, lmax_simu + 1)

so_mpi.init(True)
subtasks = so_mpi.taskrange(imin=d["iStart"], imax=d["iStop"])

for iii in subtasks:
    #First we will generate our simulations and take their harmonics transforms
    t0 = time.time()

    alms = curvedsky.rand_alm(ps_cmb, lmax=lmax_simu)

    if include_fg == True:
    arrays = d["arrays_%s" % sv]
    for ar in arrays:
        freq_list += [d["nu_eff_%s_%s" % (sv, ar)]]
freq_list = list(dict.fromkeys(freq_list))  # this bit removes doublons

id_freq = {}
# create a list assigning an integer index to each freq (used later in the code to generate fg simulations)
for count, freq in enumerate(freq_list):
    id_freq[freq] = count

# we read cmb and fg best fit power spectrum
# we put the best fit power spectrum in a matrix [nfreqs, nfreqs, lmax]
# taking into account the correlation of the fg between different frequencies

ncomp = 3
ps_cmb = powspec.read_spectrum("%s/lcdm.dat" % bestfit_dir)[:ncomp, :ncomp]
l, ps_fg = data_analysis_utils.get_foreground_matrix(bestfit_dir, freq_list,
                                                     lmax)

# prepare the filters

template = {}
filter = {}
for sv in surveys:

    template_name = d["maps_%s_%s" % (sv, arrays[0])][0]
    template[sv] = so_map.read_map(template_name)
    ks_f = d["k_filter_%s" % sv]

    assert (template[sv].pixel == "CAR"
            ), "we only compute kspace tf in CAR pixellisation"
Example #8
0
from pixell import enmap, powspec, curvedsky
import numpy as np
import os, sys
from mapsims import cmb
import healpy as hp

seed = 10
lmax = 150
iteration_num = 0
cmb_set = 0
lensed = False
aberrated = False
nside = 32

theory_filename = "mapsims/data/test_scalCls.dat"
save_name = "mapsims/data/test_map.fits"
output_directory = "mapsims/data"
output_file = cmb._get_cmb_map_string(output_directory, iteration_num, cmb_set,
                                      lensed, aberrated)
ps = powspec.read_spectrum(theory_filename)
alms = curvedsky.rand_alm_healpy(ps, lmax=lmax, seed=seed, dtype=np.complex64)
hp.write_alm(output_file, alms, overwrite=True)
imap = cmb.get_cmb_sky(
    iteration_num=0,
    nside=nside,
    cmb_dir=output_directory,
    lensed=False,
    aberrated=False,
)
hp.write_map(save_name, imap, overwrite=True)
    f0, f1 = f[0], f[1]
    fname = '%sx%s' % (f0, f1)
    cl_th_and_fg['ET', fname] = cl_th_and_fg['TE', fname]
    cl_th_and_fg['EB', fname] = cl_th_and_fg['TE', fname] * 0
    cl_th_and_fg['BE', fname] = cl_th_and_fg['TE', fname] * 0
    cl_th_and_fg['TB', fname] = cl_th_and_fg['TE', fname] * 0
    cl_th_and_fg['BT', fname] = cl_th_and_fg['TE', fname] * 0
    cl_th_and_fg['BB', fname] = np.zeros(lth_max)
    cl_th_and_fg['BB', fname][2:lth_max] = clth['BB'][:lth_max -
                                                      2] / fth[:lth_max - 2]
    for spec in spectra:
        fname_revert = '%sx%s' % (f1, f0)
        cl_th_and_fg[spec, fname_revert] = cl_th_and_fg[spec, fname]

mat = np.zeros((9, 9, lth_max))
fields = ['T', 'E', 'B']
for c1, freq1 in enumerate(freqs):
    for c2, freq2 in enumerate(freqs):
        for count1, field1 in enumerate(fields):
            for count2, field2 in enumerate(fields):

                fname = '%sx%s' % (freq1, freq2)
                print(count1 + 3 * c1, count2 + 3 * c2, field1 + field2, fname)
                mat[count1 + 3 * c1,
                    count2 + 3 * c2, :lth_max] = cl_th_and_fg[field1 + field2,
                                                              fname]

ps_th = powspec.read_spectrum(d['theoryfile'])[:3, :3]

np.save('%s/signal_fg_matrix.npy' % theoryFgDir, mat)
Example #10
0
def getActpolCmbFgSim(beamfileDict,
                      shape, wcs,
                      iterationNum, cmbDir, freqs, psa,
                      cmbSet = 0, \
                      doBeam = True, applyWindow = True, verbose = True, cmbMaptype = 'LensedCMB',
                      foregroundSeed = 0, simType = 'cmb', foregroundPowerFile = None, applyModulation = True):

    nTQUs = len('TQU')
    firstTime = True

    output = enmap.empty((
        len(freqs),
        nTQUs,
    ) + shape[-2:], wcs)

    if simType == 'cmb':

        filename = cmbDir + "/fullsky%s_alm_set%02d_%05d.fits" % (
            cmbMaptype, cmbSet, iterationNum)
        if verbose:
            print('getActpolCmbFgSim(): loading CMB a_lms from %s' % filename)
        import healpy
        almTebFullskyOnecopy = np.complex128(
            healpy.fitsfunc.read_alm(filename, hdu=(1, 2, 3)))

        #Now tile the same for all the frequencies requested (i.e. CMB is same at all frequencies).
        #The beam convolution happens below.
        almTebFullsky = np.tile(almTebFullskyOnecopy, (len(freqs), 1, 1))

        if verbose:
            print('getActpolCmbFgSim(): done')

    elif simType == 'foregrounds':
        outputFreqs = ['f090', 'f150']

        foregroundPowers \
            = powspec.read_spectrum(os.path.join(os.path.dirname(os.path.abspath(__file__)),
                                                 '../data/',
                                                 foregroundPowerFile),
                                ncol = 3,
                                expand = 'row')

        if verbose:
            print('getActpolCmbFgSim(): getting foreground a_lms')
        almTFullsky90and150 = curvedsky.rand_alm(foregroundPowers,
                                                 seed=foregroundSeed)

        almTebFullsky = np.zeros((
            len(freqs),
            nTQUs,
        ) + (len(almTFullsky90and150[-1]), ),
                                 dtype=np.complex128)

        for fi, freq in enumerate(freqs):
            if freq in outputFreqs:
                almTebFullsky[fi, 'TQU'.index('T'), :]  \
                    = almTFullsky90and150[outputFreqs.index(freq), :]

    #Convolve with beam on full sky
    for fi, freq in enumerate(freqs):
        if doBeam:
            beamFile = beamfileDict[psa + '_' + freq]
            if verbose:
                print('getActpolCmbFgSim(): applying beam from %s' % beamFile)
            beamData = (np.loadtxt(beamFile))[:, 1]
        else:
            if verbose:
                print('getActpolCmbFgSim(): not convolving with beam')
            beamData = np.repeat(1., almTebFullsky.shape[-1])

        import healpy

        #couldn't quickly figure out how to vectorize this so loop from 0 to 2.
        for tqui in range(nTQUs):
            almTebFullsky[fi, tqui] = healpy.sphtfunc.almxfl(
                almTebFullsky[fi, tqui].copy(), beamData)

        #These lines stolen from curvedsky.rand_map
        #doing all freqs at once gives error:
        #sharp.pyx in sharp.execute_dp (cython/sharp.c:12118)()
        #ValueError: ndarray is not C-contiguous
        #so loop over all freqs once for now.

    curvedsky.alm2map(almTebFullsky, output, spin=[0, 2], verbose=True)

    # outputThisfreq   = enmap.empty(( nTQUs,)+shape[-2:], wcs)
    # curvedsky.alm2map(almTebFullsky[fi,:,:], outputThisfreq, spin = [0,2],  verbose = True)
    # output[fi,...] = outputThisfreq

    # curvedsky.alm2map(almTebFullsky[fi,:,:], output[fi,:,:,:], spin = [0,2],  verbose = True)

    if applyModulation:
        from pixell import aberration
        for fi, freq in enumerate(freqs):

            print('applying modulation for frequency %s' % freq)
            output, A = aberration.boost_map(output,
                                             aberration.dir_equ,
                                             aberration.beta,
                                             return_modulation=True,
                                             aberrate=False,
                                             freq=freqStrToValGhz[freq] * 1e9)

    if applyWindow:
        from pixell import fft

        #The axes along which to FFT
        axes = [-2, -1]
        if verbose:
            print('getActpolCmbFgSim(): applying pixel window function')

        nfreq = len(freqs)
        for idx in range(nfreq):
            fd = fft.fft(output[idx], axes=axes)
            wy, wx = enmap.calc_window(fd.shape)
            twoDWindow = wy[:, None]**1 * wx[None, :]**1

            #Careful, this is quietly multiplying an array with shape [N_freq, N_TQU, N_y, N_x] with one of shape [N_y, N_x]
            fd *= twoDWindow

            output[idx] = (fft.ifft(fd, axes=axes, normalize=True)).real
            del fd
        if verbose:
            print('getActpolCmbFgSim(): done')

    return enmap.ndmap(output, wcs)
evals = cat['EXT_VAL']

arc = 40.
decmax = 70.
npix = int(arc / 0.5)

assert np.all(fluxes > 5.)
assert np.all(sns > 5.)

print(len(sns[sns > 5]))
print(len(sns[sns > 10]))
print(len(sns[sns > 10]))
print(len(sns[sns > 20]))
#sys.exit()

ps = powspec.read_spectrum("input/cosmo2017_10K_acc3_scalCls.dat")  # CHECK

dm = sints.PlanckHybrid()
pfwhm = dm.fwhms[freq]
nsplits = dm.get_nsplits(None, None, None)
if True:
    imap = dm.get_split(freq, i, ncomp=None, srcfree=True, pccs_sub=False)[0]
    fname = dm.get_split_fname(None,
                               None,
                               freq,
                               i,
                               srcfree=True,
                               pccs_sub=False)
    oname = dm.get_split_fname(None,
                               None,
                               freq,
Example #12
0
    else:
        return teb


def phi2kappa(phiMap, LMAX):

    phiAlm = curvedsky.map2alm(phiMap, lmax=LMAX)
    ells = np.arange(LMAX - 1)
    kappaAlm = healpy.sphtfunc.almxfl(phiAlm, ells * (ells + 1) / 2.)
    kappaMap = curvedsky.alm2map(kappaAlm, phiMap.copy())
    return kappaMap


shape, wcs = enmap.fullsky_geometry(p['PIX_SIZE'] * utils.arcmin)
ps = powspec.read_camb_full_lens(p['inputSpecRoot'] + "_lenspotentialCls.dat")
lPs = powspec.read_spectrum(p['inputSpecRoot'] + "_lensedCls.dat")

doAll = True
cmbSet = 0  # still to-do: loop over sets.

#make phi totally uncorrelated with both T and E.  This is necessary due to the way that separate phi and CMB seeds were put forward in an update to the pixell library around mid-Nov 2018
ps[0, 1:, :] = 0.
ps[1:, 0, :] = 0.

start = time.time()

for cmbSet in range(p['START_SET'], p['STOP_SET']):

    for iii in range(iMin, iMax):
        print('rank', rank, 'doing cmbSet', cmbSet, 'iii' , iii, \
            ', iMin', iMin, ', iMax', iMax, 'calling lensing.rand_map', time.time() - start)
Example #13
0
if include_sys == True:
    simSpectraDir = 'sim_spectra_syst'
else:
    simSpectraDir = 'sim_spectra'

pspy_utils.create_directory(simSpectraDir)

nside = 2048
ncomp = 3

template = so_map.healpix_template(ncomp, nside)

if include_foregrounds == True:
    ps_th = np.load('%s/signal_fg_matrix.npy' % theoryFgDir)
else:
    ps_th = powspec.read_spectrum(d['theoryfile'])[:ncomp, :ncomp]

nSplits = len(splits)

l, Nl_T, Nl_P = planck_utils.get_noise_matrix_spin0and2(
    ps_model_dir,
    experiment,
    freqs,
    lmax,
    nSplits,
    lcut=0,
    use_noise_th=use_noise_th)

pixwin = hp.pixwin(nside)

so_mpi.init(True)
Example #14
0
                              overwrite=overwrite,
                              output_dir=None)
binner = pitas_lib.binner
lbin = pitas_lib.bin_center

# load sims (unlensed 15x15 sq deg)
tmap, qmap, umap = enmap.read_fits(resource_path('test_tqu2.fits'))
tmap -= np.mean(tmap)
qmap -= np.mean(qmap)
umap -= np.mean(umap)
tmap *= taper
qmap *= taper
umap *= taper

# load theory
theo = powspec.read_spectrum(resource_path('cosmo2017_10K_acc3_scalCls.dat'))
cltt_th = theo[0, 0, 2:]
clee_th = theo[1, 1, 2:]
clte_th = theo[2, 2, 2:]
cleb_th = clbb_th = np.zeros(cltt_th.shape)
l_th = np.arange(len(cltt_th)) + 2.

# bin theory (cls)
lbin, clttbin_th = pitas_lib.bin_theory_scalarxscalar(l_th, cltt_th)
lbin, cltebin_th = pitas_lib.bin_theory_scalarxvector(l_th, clte_th)
lbin, cleebin_th, clebbin_th, clbbbin_th = pitas_lib.bin_theory_pureeb(
    l_th, clee_th, cleb_th, clbb_th)

# bin theory (dls)
lbin, dlttbin_th = pitas_lib.bin_theory_scalarxscalar(l_th,
                                                      cltt_th,