Ejemplo n.º 1
0
def qe_shear(px,mlmax,Talm=None,fTalm=None):
    """
    px is a pixelization object, initialized like this:
    px = pixelization(shape=shape,wcs=wcs) # for CAR
    px = pixelization(nside=nside) # for healpix
    output: curved sky shear estimator
    """
    ells = np.arange(mlmax)
    #prepare temperature map
    rmapT=px.alm2map(np.stack((Talm,Talm)),spin=0,ncomp=1,mlmax=mlmax)[0]
    #find tbarf
    t_alm=cs.almxfl(fTalm,np.sqrt((ells-1.)*ells*(ells+1.)*(ells+2.)))
    alms=np.stack((t_alm,t_alm))
    rmap=px.alm2map_spin(alms,0,2,ncomp=2,mlmax=mlmax)   #same as 2 2
    #multiply the two fields together
    prodmap=rmap*rmapT
    if not(px.hpix): prodmap=enmap.enmap(prodmap,px.wcs)
    realsp2=prodmap[0] #spin +2 real space real space field
    realsm2=prodmap[1] #spin -2 real space real space field
    if not(px.hpix): 
        realsp2 = enmap.enmap(realsp2,px.wcs)
        realsm2=enmap.enmap(realsm2,px.wcs)
    #convert the above spin2 fields to spin pm 2 alms
    res1 = px.map2alm_spin(realsp2,mlmax,2,2) #will return pm2 
    res2= px.map2alm_spin(realsm2,mlmax,-2,2) #will return pm2
    #spin 2 ylm 
    ttalmsp2=rot2dalm(res1,2)[0] #pick up the spin 2 alm of the first one
    ttalmsm2=rot2dalm(res1,2)[1] #pick up the spin -2 alm of the second one
    shear_alm=ttalmsp2+ttalmsm2
    return shear_alm
Ejemplo n.º 2
0
def high_res_plot_img(array,
                      filename=None,
                      down=None,
                      verbose=True,
                      overwrite=True,
                      crange=None,
                      cmap="planck"):
    if not (overwrite):
        if os.path.isfile(filename): return
    try:
        from pixell import enmap, enplot
    except:
        traceback.print_exc()
        cprint(
            "Could not produce plot " + filename +
            ". High resolution plotting requires enlib, which couldn't be imported. Continuing without plotting.",
            color='fail')
        return

    if (down is not None) and (down != 1):
        downmap = enmap.downgrade(enmap.enmap(array)[None], down)
    else:
        downmap = enmap.enmap(array)[None]
    img = enplot.draw_map_field(downmap,
                                enplot.parse_args("-c " + cmap + " -vvvg moo"),
                                crange=crange)
    #img = enplot.draw_map_field(downmap,enplot.parse_args("--grid 1"),crange=crange)
    if filename is None:
        img.show()
    else:
        img.save(filename)
        if verbose:
            print(bcolors.OKGREEN + "Saved high-res plot to",
                  filename + bcolors.ENDC)
Ejemplo n.º 3
0
def sim_build(region, version, overwrite):

    save_scratch = True
    savedir = tutils.get_save_path(version, region)
    if save_scratch:
        scratch = tutils.get_scratch_path(version, region)
        covscratch = scratch
    else:
        covscratch = None
    if not (overwrite):
        assert not(os.path.exists(savedir)), \
       "This version already exists on disk. Please use a different version identifier."
    try:
        os.makedirs(savedir)
    except:
        if overwrite: pass
        else: raise
    if save_scratch:
        try:
            os.makedirs(scratch)
        except:
            pass

    #enmap.write_map(scratch+"/temp1.hdf",enmap.enmap(np.zeros((100,100))))
    #enmap.write_map(savedir+"/temp2.hdf",enmap.enmap(np.zeros((100,100))))

    np.save(scratch + "/temp1.npy", enmap.enmap(np.zeros((100, 100))))
    np.save(savedir + "/temp2.npy", enmap.enmap(np.zeros((100, 100))))

    shutil.rmtree(scratch)
Ejemplo n.º 4
0
def process(kouts, name="default", ellmax=None, y=False, y_ellmin=400):
    ellmax = lmax if ellmax is None else ellmax
    ksilc = enmap.zeros((Ny, Nx), wcs, dtype=np.complex128).reshape(-1)
    ksilc[modlmap.reshape(-1) < lmax] = np.nan_to_num(kouts.copy())
    ksilc = enmap.enmap(ksilc.reshape((Ny, Nx)), wcs)
    ksilc[modlmap > ellmax] = 0
    if y: ksilc[modlmap < y_ellmin] = 0
    msilc = np.nan_to_num(
        fft.ifft(ksilc, axes=[-2, -1], normalize=True).real * bmask)
    enmap.write_map(proot + "outmap_%s.fits" % name, enmap.enmap(msilc, wcs))
    p2d = fc.f2power(ksilc, ksilc)

    bin_edges = np.arange(100, 3000, 40)
    binner = stats.bin2D(modlmap, bin_edges)
    cents, p1d = binner.bin(p2d)

    try:
        # io.plot_img(np.log10(np.fft.fftshift(p2d)),proot+"ksilc_%s.png" % name,aspect='auto')
        # io.plot_img(msilc,proot+"msilc_%s.png" % name)
        # io.plot_img(msilc,proot+"lmsilc_%s.png" % name,lim=300)
        tmask = maps.mask_kspace(shape,
                                 wcs,
                                 lmin=300,
                                 lmax=5000 if not (y) else 1500)
        fmap = maps.filter_map(msilc, tmask) * bmask
        io.plot_img(fmap, proot + "hmsilc_%s.png" % name, high_res=True)
    except:
        pass

    return cents, p1d
Ejemplo n.º 5
0
    def get_shear(self, do_m=True, do_c=True):
        rms = self.cat.data['ishape_hsm_regauss_derived_rms_e']
        m = self.cat.data['ishape_hsm_regauss_derived_bias_m']
        e1 = self.cat.data['ishape_hsm_regauss_e1']
        e2 = self.cat.data['ishape_hsm_regauss_e2']
        c1 = self.cat.data['ishape_hsm_regauss_derived_bias_c1']
        c2 = self.cat.data['ishape_hsm_regauss_derived_bias_c2']

        hsc_wts = self.hsc_wts
        wts = self.wts
        hsc_resp = 1. - np.nan_to_num(
            self.get_map(weights=(wts * (rms**2.))) / hsc_wts)
        hsc_m = np.nan_to_num(self.get_map(weights=(wts * (m))) /
                              hsc_wts) if do_m else hsc_wts * 0.

        hsc_e1 = self.get_map(weights=e1 * wts)
        hsc_e2 = self.get_map(weights=e2 * wts)

        hsc_c1 = np.nan_to_num(self.get_map(weights=c1 * wts) /
                               hsc_wts) if do_c else hsc_wts * 0.
        hsc_c2 = np.nan_to_num(self.get_map(weights=c2 * wts) /
                               hsc_wts) if do_c else hsc_wts * 0.

        g1map = np.nan_to_num(hsc_e1 / 2. / hsc_resp / (1. + hsc_m) /
                              hsc_wts) - np.nan_to_num(hsc_c1 / (1. + hsc_m))
        g2map = np.nan_to_num(hsc_e2 / 2. / hsc_resp / (1. + hsc_m) /
                              hsc_wts) - np.nan_to_num(hsc_c2 / (1. + hsc_m))

        if not self.curved:
            g1map = enmap.enmap(g1map, self.wcs)
            g2map = enmap.enmap(g2map, self.wcs)

        return g1map, g2map
Ejemplo n.º 6
0
def qe_m4(px,mlmax,Talm=None,fTalm=None):
    """
    px is a pixelization object, initialized like this:
    px = pixelization(shape=shape,wcs=wcs) # for CAR
    px = pixelization(nside=nside) # for healpix
    output: curved sky multipole=4 estimator
    """
    ells = np.arange(mlmax)
    #prepare temperature map
    rmapT=px.alm2map(np.stack((Talm,Talm)),spin=0,ncomp=1,mlmax=mlmax)[0]
    #find tbarf
    t_alm=cs.almxfl(fTalm,np.sqrt((ells-3.)*(ells-2.)*(ells-1.)*ells*(ells+1.)*(ells+2.)*(ells+3.)*(ells+4.)))

    alms=np.stack((t_alm,t_alm))
    rmap=px.alm2map_spin(alms,0,4,ncomp=2,mlmax=mlmax)

    #multiply the two fields together
    rmap=np.nan_to_num(rmap)
    prodmap=rmap*rmapT
    prodmap=np.nan_to_num(prodmap)
    if not(px.hpix): prodmap=enmap.enmap(prodmap,px.wcs)
    realsp2=prodmap[0] #spin +4 real space real space field
    if not(px.hpix): realsp2 = enmap.enmap(realsp2,px.wcs)
    #convert the above spin4 fields to spin pm 4 alms
    res1 = px.map2alm_spin(realsp2,mlmax,4,4) #will return pm4
    #spin 4 ylm 
    ttalmsp2=rot2dalm(res1,4)[0] #pick up the spin 4 alm of the first one
    ttalmsm2=rot2dalm(res1,4)[1] #pick up the spin -4 alm of the second one
    m4_alm=ttalmsp2+ttalmsm2
    return m4_alm
Ejemplo n.º 7
0
 def get_delta(self):
     delta = (self.counts/self.nmean-1.)
     if not self.curved:
         parea = maps.psizemap(delta.shape, self.wcs)*((180.*60./np.pi)**2.)
         delta = ((delta+1.)*self.nmean/self.ngal_per_arcminsq/parea)-1.
         delta = enmap.enmap(delta,self.wcs)
     return delta
Ejemplo n.º 8
0
 def get_maps(self, seed=None):
     # SKY
     fgs = []
     for i, comp in enumerate(self.components):
         fgs.append(self.fgens[comp].get_map(seed=(1, i, seed)))
     arrays = []
     for k, (beam, inhom,
             nsplit) in enumerate(zip(self.beams, self.inhoms,
                                      self.nsplits)):
         # SKY
         sky = 0.
         for i, comp in enumerate(self.components):
             sky += (fgs[i] * self.rdict[comp][i])
         kbeam = maps.gauss_beam(self.modlmap, beam)
         kbeam[self.modlmap < self.ellmin] = 0
         sky_convolved = maps.filter_map(sky, kbeam)
         # NOISE
         nmaps = []
         for j in range(nsplit):
             nmap = self.ngens[k].get_map(seed=(2, j, seed)) * nsplit
             nmaps.append(nmap.copy())
         nmaps = np.stack(nmaps)
         observed = enmap.enmap(sky_convolved + nmaps, self.wcs)
         arrays.append(observed.copy())
     return arrays
Ejemplo n.º 9
0
def process_splits(splits,
                   wins,
                   mask,
                   skip_splits=False,
                   do_fft_splits=False,
                   pixwin=False):
    assert wins.ndim > 2
    with np.errstate(divide='ignore', invalid='ignore'):
        coadd = (splits * wins).sum(axis=0) / wins.sum(axis=0)
    coadd[~np.isfinite(coadd)] = 0
    Ny, Nx = splits.shape[-2:]
    assert coadd.shape == (Ny, Nx)
    kcoadd = enmap.enmap(enmap.fft(coadd * mask, normalize='phys'), wins.wcs)

    if pixwin: pwin = tutils.get_pixwin(coadd.shape[-2:])
    else: pwin = 1
    kcoadd = kcoadd / pwin
    if not (skip_splits):
        #data = (splits-coadd)*mask # !!!! wins removed, was (splits-coadd)*wins*mask earlier
        data = (splits - coadd) * mask * wins / wins.sum(
            axis=0)  # !!!! wins removed, was (splits-coadd)*wins*mask earlier
        data[~np.isfinite(data)] = 0
        kdiffs = enmap.fft(data, normalize='phys')
        kdiffs = kdiffs / pwin
    else:
        kdiffs = None
    if do_fft_splits:
        ksplits = enmap.fft(splits * wins * mask, normalize='phys')
        ksplits = ksplits / pwin
        return kdiffs, kcoadd, ksplits
    else:
        return kdiffs, kcoadd
Ejemplo n.º 10
0
 def map2alm_spin(self,imap,lmax,spin_alm,spin_transform):
     dmap = -irot2d(np.stack((imap,imap.conj())),spin=spin_alm).real
     if self.hpix:
         res = hp.map2alm_spin(dmap,lmax=lmax,spin=spin_transform)
         return res
     else:
         return cs.map2alm(enmap.enmap(dmap,imap.wcs),spin=spin_transform,lmax=lmax)
Ejemplo n.º 11
0
    def get_sim(self,season,patch,array,sim_num, save_alm=True, save_map=False, set_idx=0,mask_patch=None,fgflux="15mjy"):
        shape,wcs = self.noise_gen.load_covsqrt(season,patch,array,coadd=True,mask_patch=mask_patch,get_geometry=True)
        # (nfreqs,nsplits,npol,Ny,Nx)
        noises,ivars = self.get_noise(season=season,patch=patch,array=array, sim_num=sim_num,mask_patch=mask_patch,set_idx=set_idx,apply_ivar=False)
        #noises,ivars = self.get_noise(season=season,patch=patch,array=array, sim_num=0,mask_patch=mask_patch,set_idx=set_idx,apply_ivar=False)
        afreqs = self.noise_gen.dm.array_freqs[array]
        #afreqs  = ['pa1_f090', 'pa1_f150'] 
        signals = []
        for afreq in afreqs:
            # This could be improved
            if self.model=='act_mr3': pfreq = afreq.split('_')[1] 
            elif self.model=='planck_hybrid': 
                pfreq = afreq
                assert season is None
                season = "planck"
                patch = "planck"
                array = "planck"

            imap = self.get_signal(season, patch, array, pfreq, sim_num, save_alm=save_alm, save_map=save_map, set_idx=set_idx,oshape=shape,owcs=wcs,fgflux=fgflux)
            signals.append(imap)
        owcs = imap.wcs
        # (nfreqs,npol,Ny,Nx)
        signals = enmap.enmap(np.stack(signals),owcs)[:,None,...]
        del imap
        assert wcsutils.equal(wcs,noises.wcs)
        assert wcsutils.equal(owcs,noises.wcs)
        return self._footprint(noise.apply_ivar_window(signals + noises,ivars))
Ejemplo n.º 12
0
def pcov_from_ivar(n,dec,ra,ivar,cmb_theory_fn,beam_fn,iau=False):
    """
    Get a pixel covariance matrix for a stamp around a given location
    from the white noise inverse variance map and theory and beam
    functions.
    """
    assert ivar.ndim==2
    py,px = ivar.sky2pix((dec,ra))
    py = int(py)
    px = int(px)
    sy = py - n//2
    sx = px - n//2
    ey = sy + n
    ex = sx + n
    sliced = 1./ivar[sy:ey,sx:ex]
    sliced[~np.isfinite(sliced)] = 1./ivar[ivar>0].max() # this is wrong! but needed to prevent singular matrices?!
    ncov = np.diag(sliced.reshape(-1))
    ncov_IQU = np.zeros((3,3,n*n,n*n))
    ncov_IQU[0,0] = ncov.copy()
    ncov_IQU[1,1] = ncov.copy() * 2.
    ncov_IQU[2,2] = ncov.copy() * 2.
    modlmap = sliced.modlmap()
    cmb2d_TEB = np.zeros((3,3,n,n))
    theory = cmb_theory_fn
    cmb2d_TEB[0,0] = theory('TT',modlmap)
    cmb2d_TEB[1,1] = theory('EE',modlmap)
    cmb2d_TEB[2,2] = theory('BB',modlmap)
    cmb2d_TEB[0,1] = theory('TE',modlmap)
    cmb2d_TEB[1,0] = theory('TE',modlmap)
    beam2d = beam_fn(modlmap)
    tcov = stamp_pixcov_from_theory(n,enmap.enmap(cmb2d_TEB,sliced.wcs),n2d_IQU=0.,beam2d=beam2d,iau=iau,return_pow=False)    
    return tcov + ncov_IQU
Ejemplo n.º 13
0
 def update_mask(self, mask_threshold):
     mask = np.zeros(self.shape)
     mask[self.mean_wt > mask_threshold] = 1
     self.mask = mask
     if not self.curved:
         self.mask = enmap.enmap(self.mask, self.wcs)
     self._counts()
Ejemplo n.º 14
0
def signal_average(cov,bin_edges=None,bin_width=40,kind=3,lmin=None,dlspace=True,return_bins=False,**kwargs):
    """
    dcov = cov * ellfact
    bin dcov in annuli
    interpolate back on to ell
    cov = dcov / ellfact
    where ellfact = ell**2 if dlspace else 1
    """
    modlmap = cov.modlmap()
    assert np.all(np.isfinite(cov))

    dcov = cov*modlmap**2. if dlspace else cov.copy()
    if lmin is None:
        minell = maps.minimum_ell(dcov.shape,dcov.wcs)
    else:
        minell = modlmap[modlmap<=lmin].max()

    if bin_edges is None: bin_edges = np.append([2],np.arange(minell,modlmap.max(),bin_width))

    binner = stats.bin2D(modlmap,bin_edges)
    cents,c1d = binner.bin(dcov)

    outcov = enmap.enmap(maps.interp(cents,c1d,kind=kind,fill_value=c1d[-1],**kwargs)(modlmap),dcov.wcs)
    with np.errstate(invalid='ignore'): outcov = outcov / modlmap**2. if dlspace else outcov
    outcov[modlmap<2] = 0
    assert np.all(np.isfinite(outcov))

    if return_bins: return cents,c1d,outcov
    else: return outcov 
Ejemplo n.º 15
0
 def load_covsqrt(self,
                  season=None,
                  patch=None,
                  array=None,
                  coadd=True,
                  mask_patch=None,
                  get_geometry=False):
     pout, cout, sout = get_save_paths(self._model,
                                       self._version,
                                       coadd=coadd,
                                       season=season,
                                       patch=patch,
                                       array=array,
                                       overwrite=False,
                                       mask_patch=mask_patch)
     fpath = "%s_covsqrt.fits" % (cout)
     if get_geometry: return enmap.read_map_geometry(fpath)
     ikey = '_'.join([str(x) for x in [season, patch]])
     try:
         covsqrt = self._ccache[fpath]
         ivars = self._icache[ikey]
         if self.verbose: print("Loaded cached covsqrt and ivars.")
     except:
         if self.verbose:
             print(
                 "Couldn't find covsqrt and ivars in cache. Reading from disk..."
             )
         ivars = enmap.enmap([self.dm.get_ivars(q) for q in self._qid])
         covsqrt = enmap.read_map(fpath)
         print(fpath, covsqrt.shape)
         if len(self._ccache.keys()) < self.ncache:
             self._ccache[fpath] = covsqrt
             self._icache[ikey] = ivars
     return covsqrt, ivars
Ejemplo n.º 16
0
def get_splits_ivar(qid, extracter, ivar_unhit=1e-7, ivar_tol=20):
    dmodel = sints.arrays(qid, 'data_model')
    season = sints.arrays(qid, 'season')
    region = sints.arrays(qid, 'region')
    array = sints.arrays(qid, 'array')
    freq = sints.arrays(qid, 'freq')
    dm = sints.models[dmodel]()
    ivars = []
    for i in range(dm.get_nsplits(season=season, patch=region, array=array)):
        omap = extracter(
            dm.get_split_ivar_fname(season=season,
                                    patch=region,
                                    array=array + "_" +
                                    freq if not (is_planck(qid)) else freq,
                                    splitnum=i))
        if omap.ndim > 2: omap = omap[0]
        if not (np.any(omap > 0)):
            print(
                "Skipping %s as it seems to have an all zero ivar in this tile"
                % qid)
            return None
        omap[~np.isfinite(omap)] = 0
        omap[omap < ivar_unhit] = 0
        ref_div = robust_ref(omap)
        omap = np.minimum(omap, ref_div * ivar_tol)
        omap = filter_div(omap)
        eshape, ewcs = omap.shape, omap.wcs
        ivars.append(omap.copy())
    return enmap.enmap(np.stack(ivars), ewcs)
Ejemplo n.º 17
0
def generate_noise_sim(covsqrt, ivars, seed=None, dtype=None):
    """
    Supports only two cases
    1) nfreqs>=1,npol=3
    2) nfreqs=1,npol=1
    """
    if isinstance(seed, int): seed = (seed, )
    assert np.all(np.isfinite(covsqrt))
    shape, wcs = covsqrt.shape, covsqrt.wcs
    Ny, Nx = shape[-2:]
    ncomps = covsqrt.shape[0]
    assert ncomps == covsqrt.shape[1]
    assert ((ncomps % 3) == 0) or (ncomps == 1)
    nfreqs = 1 if ncomps == 1 else ncomps // 3
    if ncomps == 1: npol = 1
    else: npol = 3
    wmaps = enmap.extract(ivars, shape[-2:], wcs)
    nsplits = wmaps.shape[1]

    if dtype is np.float32: ctype = np.complex64
    elif dtype is np.float64: ctype = np.complex128

    # Old way with loop
    kmap = []
    for i in range(nsplits):
        if seed is None:
            np.random.seed(None)
        else:
            np.random.seed(seed + (i, ))
        rmap = enmap.rand_gauss_harm((ncomps, Ny, Nx),
                                     covsqrt.wcs).astype(ctype)
        kmap.append(enmap.map_mul(covsqrt, rmap))
    del covsqrt, rmap
    kmap = enmap.enmap(np.stack(kmap), wcs)
    outmaps = enmap.ifft(kmap, normalize="phys").real
    del kmap

    # Need to test this more ; it's only marginally faster and has different seed behaviour
    # covsqrt = icovsqrt
    # np.random.seed(seed)
    # rmap = enmap.rand_gauss_harm((nsplits,ncomps,Ny, Nx),covsqrt.wcs)
    # kmap = enmap.samewcs(np.einsum("abyx,cbyx->cayx", covsqrt, rmap),rmap)
    # outmaps = enmap.ifft(kmap, normalize="phys").real

    # isivars = 1/np.sqrt(wmaps)
    with np.errstate(divide='ignore', invalid='ignore'):
        isivars = ((1. / wmaps) - (1. / wmaps.sum(axis=1)[:, None, ...]))**0.5
    isivars[~np.isfinite(isivars)] = 0

    assert np.all(np.isfinite(outmaps))
    # Divide by hits
    for ifreq in range(nfreqs):
        outmaps[:, ifreq * npol:(ifreq + 1) * npol,
                ...] = outmaps[:, ifreq * npol:(ifreq + 1) * npol,
                               ...] * isivars[ifreq, ...] * np.sqrt(nsplits)

    retmaps = outmaps.reshape((nsplits, nfreqs, npol, Ny, Nx)).swapaxes(0, 1)
    assert np.all(np.isfinite(retmaps))
    return retmaps, wmaps
Ejemplo n.º 18
0
def get_offset_result(res=1.,dtype=np.float64,seed=1):
    shape,wcs  = enmap.fullsky_geometry(res=np.deg2rad(res))
    shape = (3,) + shape
    obs_pos = enmap.posmap(shape, wcs)
    np.random.seed(seed)
    grad = enmap.enmap(np.random.random(shape),wcs)*1e-3
    raw_pos = enmap.samewcs(lensing.offset_by_grad(obs_pos, grad, pol=shape[-3]>1, geodesic=True), obs_pos)
    return obs_pos,grad,raw_pos
Ejemplo n.º 19
0
def get_covsqrt(ps, method="arrayops"):
    if method == "multipow":
        covsq = enmap.multi_pow(ps.copy(), 0.5)
    elif method == "arrayops":
        from enlib import array_ops
        covsq = array_ops.eigpow(ps.copy(), 0.5, axes=[0, 1])
    covsq[:, :, ps.modlmap() < 2] = 0
    assert np.all(np.isfinite(covsq))
    return enmap.enmap(covsq, ps.wcs)
Ejemplo n.º 20
0
def mask_smoothing(inmap, N, f=0.5):
    import scipy
    smoothed = scipy.ndimage.filters.gaussian_filter(inmap, N)
    new = np.zeros(smoothed.shape)
    new[smoothed > f] = 1.
    smoothedagain = scipy.ndimage.filters.gaussian_filter(new, N)
    new = enmap.enmap(smoothedagain, inmap.wcs)
    assert np.all((inmap - new) >= 0)
    return new
Ejemplo n.º 21
0
    def __init__(self,
                 ras_deg,
                 decs_deg,
                 shape=None,
                 wcs=None,
                 nside=None,
                 verbose=True,
                 hp_coords="equatorial",
                 mask=None):

        self.verbose = verbose
        if nside is not None:

            eq_coords = ['fk5', 'j2000', 'equatorial']
            gal_coords = ['galactic']

            if verbose: print("Calculating pixels...")
            if hp_coords in gal_coords:
                if verbose: print("Transforming coords...")
                from astropy.coordinates import SkyCoord
                import astropy.units as u
                gc = SkyCoord(ra=ras_deg * u.degree,
                              dec=decs_deg * u.degree,
                              frame='fk5')
                gc = gc.transform_to('galactic')
                phOut = gc.l.deg * np.pi / 180.
                thOut = gc.b.deg * np.pi / 180.
                thOut = np.pi / 2. - thOut  #polar angle is 0 at north pole

                self.pixs = hp.ang2pix(nside, thOut, phOut)
            elif hp_coords in eq_coords:
                ras_out = ras_deg
                decs_out = decs_deg
                self.pixs = hp.ang2pix(nside, ras_out, decs_out, lonlat=True)

            else:
                raise ValueError

            if verbose: print("Done with pixels...")
            self.nside = nside
            self.shape = hp.nside2npix(nside)
            self.curved = True
        else:
            coords = np.vstack((decs_deg, ras_deg)) * np.pi / 180.
            self.shape = shape
            self.wcs = wcs
            if verbose: print("Calculating pixels...")
            self.pixs = enmap.sky2pix(shape, wcs, coords,
                                      corner=True)  # should corner=True?!
            if verbose: print("Done with pixels...")
            self.curved = False
        self.counts = self.get_map()
        if not self.curved:
            self.counts = enmap.enmap(self.counts, self.wcs)

        self.mask = np.ones(shape) if mask is None else mask
        self._counts()
Ejemplo n.º 22
0
def get_splits(qid, extracter):
    nsplits = 2
    splits = []
    for i in range(nsplits):
        fname = os.environ['WORK'] + '/sim_tiling/%s_split_%d.fits' % (qid, i)
        omap = extracter(fname)
        assert np.all(np.isfinite(omap))
        eshape, ewcs = omap.shape, omap.wcs
        splits.append(omap.copy())
    return enmap.enmap(np.stack(splits), ewcs)
Ejemplo n.º 23
0
 def test_fft(self):
     # Tests that ifft(ifft(imap))==imap, i.e. default normalizations are consistent
     shape, wcs = enmap.geometry(pos=(0, 0), shape=(3, 100, 100), res=0.01)
     imap = enmap.enmap(np.random.random(shape), wcs)
     assert np.all(
         np.isclose(
             imap,
             enmap.ifft(enmap.fft(imap, normalize='phy'),
                        normalize='phy').real))
     assert np.all(np.isclose(imap, enmap.ifft(enmap.fft(imap)).real))
Ejemplo n.º 24
0
def tqu2teb(tmap, qmap, umap, lmax):
    atol = np.min(np.array(tmap.pixshape() / eutils.arcmin))
    tqu = np.zeros((3, ) + tmap.shape)
    tqu[0], tqu[1], tqu[2] = (tmap, qmap, umap)
    tqu = enmap.enmap(tqu, tmap.wcs)
    alm = curvedsky.map2alm(tqu, lmax=lmax, atol=atol)

    teb = curvedsky.alm2map(alm[:, None], tqu.copy()[:, None], spin=0)[:, 0]
    del tqu

    return (teb[0], teb[1], teb[2])  # tmap, emap, bmap
Ejemplo n.º 25
0
def get_anisotropic_noise_template(shape,wcs,template_file=None,tmin=0,tmax=100):
    """
    This function reads in a 2D PS unredenned template and returns a full 2D noise PS.
    It doesn't use the template in the most sensible way though.
    """
    if template_file is None: template_file = "data/anisotropy_template.fits"
    template = np.nan_to_num(enmap.read_map(template_file))
    template[template<tmin] = tmin
    template[template>tmax] = tmax
    ops = enmap.enmap(enmap.resample(template,shape),wcs) # interpolate to new geometry
    return ops
Ejemplo n.º 26
0
def coadd_real_data(qid, mask, dg=2):
    # Compute coadded map by simply summing up each split with the inverse variance weight

    assert mask.ndim == 2

    # First, read survey parameters from qid
    model, season, array, patch, freq = local.qid_info(qid[0])

    # Day time data
    if model == 'dr5':
        dm = interfaces.models['dr5'](region=mask)
        datas = enmap.enmap([dm.get_splits(q, calibrated=True) for q in qid])
        ivars = enmap.enmap([dm.get_ivars(q, calibrated=True) for q in qid])

    # Night time data
    if model == 'act_mr3':
        dm = interfaces.models['act_mr3'](region=mask, calibrated=True)
        datas = dm.get_splits(season=season,
                              patch=patch,
                              arrays=dm.array_freqs[array],
                              srcfree=True)
        ivars = dm.get_splits_ivar(season=season,
                                   patch=patch,
                                   arrays=dm.array_freqs[array])

    # coadded map
    civars = np.average(ivars, axis=1)
    civars[civars == 0] = np.inf
    map_c = mask[None, None, :, :] * np.average(datas * ivars,
                                                axis=1) / civars / local.Tcmb

    # downgrade
    map_c = enmap.downgrade(map_c, dg)

    # check
    assert map_c.ndim == 4

    return map_c
Ejemplo n.º 27
0
def interp_enmap(emap, nshape, nwcs, method='cubic', fill_value=np.nan):
    # interpolate enmap to the new shape, and override wcs
    oshape, owcs = emap.shape, emap.wcs
    if nshape == oshape: return emap
    oxm, oym = enmap.posmap(oshape, owcs)
    nxm, nym = enmap.posmap(nshape, nwcs)

    oxm, oym = oxm[:, 0], oym[0, :]
    nxm, nym = nxm[:, 0], nym[0, :]

    f2 = RectBivariateSpline(oxm, oym, emap)
    ndata = f2(nxm, nym)

    return enmap.enmap(ndata, nwcs)
Ejemplo n.º 28
0
    def update_mask(self,rand_sigma_arcmin=2.,rand_threshold=1e-3):
        if rand_sigma_arcmin>1.e-3:
            if self.verbose: print( "Smoothing...")
            if self.curved:
                smap = hp.smoothing(self.rand_map,sigma=rand_sigma_arcmin*np.pi/180./60.)
            else:
                smap = enmap.smooth_gauss(self.rand_map,rand_sigma_arcmin*np.pi/180./60.)
            if self.verbose: print( "Done smoothing...")
        else:
            if self.verbose: smap = self.rand_map

        self.mask = np.zeros(self.shape)
        self.mask[smap>rand_threshold] = 1
        if not self.curved:
            self.mask = enmap.enmap(self.mask,self.wcs)
        self._counts()
Ejemplo n.º 29
0
 def test_extract(self):
     # Tests that extraction is sensible
     shape, wcs = enmap.geometry(pos=(0, 0), shape=(500, 500), res=0.01)
     imap = enmap.enmap(np.random.random(shape), wcs)
     smap = imap[200:300, 200:300]
     sshape, swcs = smap.shape, smap.wcs
     smap2 = enmap.extract(imap, sshape, swcs)
     pixbox = enmap.pixbox_of(imap.wcs, sshape, swcs)
     # Do write and read test
     filename = "temporary_extract_map.fits"  # NOT THREAD SAFE
     enmap.write_map(filename, imap)
     smap3 = enmap.read_map(filename, pixbox=pixbox)
     os.remove(filename)
     assert np.all(np.isclose(smap, smap2))
     assert np.all(np.isclose(smap, smap3))
     assert wcsutils.equal(smap.wcs, smap2.wcs)
     assert wcsutils.equal(smap.wcs, smap3.wcs)
Ejemplo n.º 30
0
def get_n2d(ffts, wmaps, plot_fname=None, coadd_estimator=False, dtype=None):
    assert np.all(np.isfinite(ffts))
    assert np.all(np.isfinite(wmaps))
    shape, wcs = ffts.shape[-2:], ffts.wcs
    modlmap = enmap.modlmap(shape, wcs)
    Ny, Nx = shape[-2:]
    nfreqs = ffts.shape[0]
    npol = ffts.shape[2]
    ncomps = nfreqs * npol

    def ncomp_to_freq_pol(index):
        ifreq = index // 3
        ipol = index % 3
        return ifreq, ipol

    n2d = enmap.zeros((ncomps, ncomps, Ny, Nx), wcs,
                      dtype=dtype)  # WARNING: type)
    pols = ['I', 'Q', 'U']
    for i in range(ncomps):
        for j in range(i, ncomps):
            ifreq, ipol = ncomp_to_freq_pol(i)
            jfreq, jpol = ncomp_to_freq_pol(j)

            isplits = ffts[ifreq, :, ipol]
            iwts = wmaps[ifreq, :, 0]
            if i != j:
                jsplits = ffts[jfreq, :, jpol]
                jwts = wmaps[jfreq, :, 0]
            else:
                jsplits = None
                jwts = None

            n2d[i, j] = noise_power(isplits,
                                    iwts,
                                    jsplits,
                                    jwts,
                                    coadd_estimator=coadd_estimator,
                                    pfunc=naive_power)
            if i != j: n2d[j, i] = n2d[i, j]
            if plot_fname is not None:
                plot("%s_%d_%s_%d_%s" \
                     % (plot_fname,ifreq,pols[ipol],
                        jfreq,pols[jpol]),
                     enmap.enmap(np.arcsinh(np.fft.fftshift(n2d[i,j])),wcs),dg=1,quantile=0)

    return n2d