Beispiel #1
0
 def save_covsqrt(self,covsqrt,season=None,patch=None,array=None,coadd=True,mask_patch=None):
     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)
     enmap.write_map(fpath ,covsqrt)
     print(fpath,covsqrt.shape)
Beispiel #2
0
def s18dStamp(ra, dec, data, name, width=0.5, write=True):
    #Find tile corresponding to RA, Dec
    path = '/scratch/r/rbond/jorlo/S18d_202006/filteredMaps/'
    tileName = tileFinder(ra, dec, data)
    if tileName == None: return None
    tile = enmap.read_map(path + tileName + '/Arnaud_M2e14_z0p4#' + tileName +
                          '_filteredMap.fits')

    stamp = reproject.postage_stamp(tile, ra, dec, width * 60, 0.5)
    if write:
        #tempdec, tempra = np.deg2rad([dec, ra])
        #tempwid = np.deg2rad(width)
        #box = [[tempdec-tempwid,tempra-tempwid],[tempdec+tempwid,tempra+tempwid]]

        #stampgeo = tile.submap(box)

        box = np.array([[ra - width / 2, dec - width / 2],
                        [ra + width / 2, dec + width / 2]]) * utils.degree
        shape, wcs = enmap.geometry(pos=box,
                                    res=0.5 * utils.arcmin,
                                    proj='car')
        print(shape)
        #print(stampgeo.wcs)
        #print(stamp.wcs)
        stamp.wcs = wcs
        print(stamp.wcs)
        print(stamp[0].shape)
        #plt.imshow(stamp[0])
        #plt.show()
        #Return map
        plot = enplot.plot(stamp, mask=0)
        enplot.show(plot)
        enmap.write_map('./for_tony/{}.fits'.format(name), stamp)
    return stamp
def genGRF(iMock):
    print("- generating mock " + str(iMock))
    # set the random seed
    np.random.seed(iMock)
    # Generate GRF healpix map
    hpGrfMap = hp.synfast(Cl,
                          nSide,
                          lmax=None,
                          mmax=None,
                          alm=False,
                          pol=False,
                          pixwin=False,
                          fwhm=0.0,
                          sigma=None,
                          new=False,
                          verbose=False)
    # save healpix map
    hp.write_map(pathOut + "hp_mock_" + str(iMock) + "_grf_f150_daynight.fits",
                 hpGrfMap,
                 overwrite=True)
    # Convert to pixell CAR map
    grfMap = reproject.enmap_from_healpix(hpGrfMap, hitShape, hitWcs, rot=None)
    # save CAR map
    enmap.write_map(pathOut + "mock_" + str(iMock) + "_grf_f150_daynight.fits",
                    grfMap)
Beispiel #4
0
def freqStamp(ra, dec, fmap, name, width = 0.5, write = True):
    
    stamp = reproject.postage_stamp(fmap, ra, dec, width*60, 0.5)
    if write:
        stamp.wcs.wcs.crval = [ra,dec]
        enmap.write_map('./for_tony/{}.fits'.format(name), stamp)
    return stamp
Beispiel #5
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
Beispiel #6
0
    def get_lensed_cmb(self, seed=None, kappa=None, save_output=True, verbose=True, overwrite=False, dtype=np.float64):
        try:
            assert(seed is not None)
            assert(not overwrite)
            if verbose:
                print(f"trying to load saved lensed cmb. sim idx: {seed}")
            lmaps = enmap.empty((3,)+self.shape, self.wcs, dtype=dtype)
            for i, polidx in enumerate(['T','Q','U']):
                fname = self.get_output_file_name("lensed_cmb", seed, polidx=polidx)
                lmaps[i] = enmap.read_map(fname)
        except: 
            ualm = self._generate_unlensed_cmb_alm(seed=seed)
            if kappa is None:
                kappa = self._get_kappa(seed, dtype=np.float64)
            lmax = 10000
            l = np.arange(lmax+1)
            l_fact = 1/((l*(l+1))/2)
            l_fact[0] = 0

            kalm = curvedsky.map2alm(kappa, lmax=lmax)
            kalm = curvedsky.map2alm(kappa, lmax=lmax); del kappa
            kalm = hp.almxfl(kalm, l_fact)
            tshape, twcs = enmap.fullsky_geometry(res=1*utils.arcmin)
            print("start lensing")
            lmaps = lensing.lens_map_curved((3,)+tshape, twcs, kalm, ualm)[0]
            lalms = curvedsky.map2alm(lmaps, lmax=lmax, spin=0)
            lmaps = curvedsky.alm2map(lalms, enmap.zeros((3,)+self.shape, self.wcs), spin=0)
            print("finish lensing")
            if save_output:
                for i, polidx in enumerate(['T','Q','U']):    
                    fname = self.get_output_file_name("lensed_cmb", seed, polidx=polidx)
                    os.makedirs(os.path.dirname(fname), exist_ok=True)
                    enmap.write_map(fname, lmaps[i].astype(np.float32))
        return lmaps.astype(dtype)
Beispiel #7
0
 def save_filter_noise(self,n2d,season=None,patch=None,array=None,coadd=True,mask_patch=None):
     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_filter_noise.fits" % (cout)
     enmap.write_map(fpath ,n2d)
     print(fpath,n2d.shape,n2d.wcs)
Beispiel #8
0
 def save_sims(self,
               seed,
               sims,
               season,
               patch,
               array,
               mask_patch,
               coadd=True):
     pout, cout, sout = get_save_paths(self._model,
                                       self._version,
                                       coadd=coadd,
                                       season=season,
                                       patch=patch,
                                       array=array,
                                       overwrite=False,
                                       mask_patch=mask_patch)
     insplits = self.dm.get_nsplits(season, patch, array)
     freqs = self.dm.array_freqs[array]
     nfreqs, nsplits, npol, Ny, Nx = sims.shape
     assert nsplits == insplits
     assert len(freqs) == nfreqs
     for i in range(nfreqs):
         iarray = freqs[i]
         for j in range(nsplits):
             fname = sout + os.path.basename(
                 self.dm.get_split_fname(
                     season, patch, iarray, j, srcfree=True)).replace(
                         ".fits", "_seed_%d.fits" % seed)
             enmap.write_map(fname, sims[i, j, :, :, :])
Beispiel #9
0
def mask(imap, ra, dec, name, width=0.5, apod_pix=4):
    ra, dec = np.deg2rad([ra, dec])
    width = np.deg2rad(width)
    box = [[dec - width / 2., ra - width / 2.],
           [dec + width / 2., ra + width / 2.]]
    stamp = imap.submap(box)
    taper = enmap.apod(stamp * 0 + 1, apod_pix)
    name = name.replace(' ', '-').lower()
    enmap.write_map('./padded_v1/adv{}.fits'.format(name), taper)
    #s14_stamp = s14map.submap(box)
    #enmap.write('./padded_v0/s14_{}__pa1_f150_nohwp_night_3pass_4way_set0_ivar.fits'.format(name), s14_stamp)
    return taper
Beispiel #10
0
def get_extraction_test_results(yaml_file):
    print("Starting tests from ", yaml_file)
    with open(yaml_file) as f:
        config = yaml.safe_load(f)
    geos = get_geometries(config['geometries'])
    lmax = config['lmax']
    lmax_pad = config['lmax_pad']
    spectra = get_spectra(config['spectra'], lmax, lmax_pad)
    seed = config['seed']

    results = {}
    for g in geos.keys():
        results[g] = {}
        for s in spectra.keys():
            results[g][s] = {}
            imap = generate_map(geos[g][0][-2:], geos[g][1], spectra[s], lmax,
                                seed)

            # Do write and read test
            filename = "temporary_map.fits"  # NOT THREAD SAFE
            enmap.write_map(filename, imap)
            imap_in = enmap.read_map(filename)
            check_equality(imap, imap_in)
            for e in config['extracts']:
                print("Doing test for extract ", e['name'], " with geometry ",
                      g, " and spectrum ", s, "...")
                if e['type'] == 'slice':
                    box = np.deg2rad(np.array(e['box_deg']))
                    cutout = enmap.read_map(filename, box=box)
                    cutout_internal = imap.submap(box=box)
                elif e['type'] == 'postage':
                    dec_deg, ra_deg = e['center_deg']
                    width_arcmin = e['width_arcmin']
                    res_arcmin = e['res_arcmin']
                    cutout = reproject.postage_stamp(filename,
                                                     ra_deg,
                                                     dec_deg,
                                                     width_arcmin,
                                                     res_arcmin,
                                                     proj='gnomonic')
                    cutout_internal = reproject.postage_stamp(imap,
                                                              ra_deg,
                                                              dec_deg,
                                                              width_arcmin,
                                                              res_arcmin,
                                                              proj='gnomonic')
                check_equality(cutout, cutout_internal)
                pixels = get_reference_pixels(cutout.shape)
                results[g][s]['refpixels'] = get_pixel_values(cutout, pixels)
                results[g][s]['meansquare'] = get_meansquare(cutout)

    os.remove(filename)
    return results, config['result_name']
Beispiel #11
0
def s18dStamp(ra, dec, data, name, width = 0.5, write = True):
    #Find tile corresponding to RA, Dec
    path = '/scratch/r/rbond/jorlo/S18d_202006/filteredMaps/'
    tileName = tileFinder(ra, dec, data)
    if tileName == None: return None
    tile = enmap.read_map(path+tileName+'/Arnaud_M2e14_z0p4#'+tileName+'_filteredMap.fits')
    
    
    stamp = reproject.postage_stamp(tile, ra, dec, width*60, 0.5)
    if write:
        stamp.wcs.wcs.crval = [ra,dec]
        enmap.write_map('./for_tony/{}.fits'.format(name), stamp)
    return stamp
def save_maps(hmap, root_name, telescope):
    for res in resolutions[telescope]:
        shape, wcs = get_geometry(res, bounds_deg=bounds[telescope])
        imap = ivar_hp_to_cyl(hmap, shape, wcs)
        if comm.rank == 0:
            oname = cr_remote_data.get_local_output(
                f"{root_name}_CAR_{res:.2f}_arcmin.fits")
            enmap.write_map(oname, imap)
            os.system(f"gzip -f {oname}")
            plots = enplot.get_plots(enmap.downgrade(imap, 8))
            savename = cr_remote_data.get_local_output(
                f"rmap_{root_name}_{res:.2f}")
            enplot.write(savename, plots)
            print(f"Plot saved to {savename}")
Beispiel #13
0
def s18dStamp(ra, dec, data, name, width=0.5, write=False):
    #Find tile corresponding to RA, Dec
    path = '/project/r/rbond/jorlo/S18d_202006/filteredMaps/'
    tileName = tileFinder(ra, dec, data)
    if tileName == None: return None
    tile = enmap.read_map(path + tileName + '/Arnaud_M2e14_z0p4#' + tileName +
                          '_filteredMap.fits')

    stamp = reproject.postage_stamp(tile, ra, dec, width * 60, 0.5)
    #print(stamp)
    if write:
        temp = np.ndarray((2, ), buffer=np.array([ra, dec]))
        stamp.wcs.wcs.crval = temp
        enmap.write_map(
            './for_tony/mustang2/for_charles/y0_{}.fits'.format(name), stamp)
    return stamp
Beispiel #14
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)
Beispiel #15
0
def coadd_maps(imaps, ihits, omap, ohit, cont=False, ncomp=-1):
    # The first map will be used as a reference. All subsequent maps
    # must fit in its boundaries.
    if cont and os.path.exists(omap): return
    if args.verbose: print("Reading %s" % imaps[0])
    if ncomp < 0:
        shape, wcs = enmap.read_map_geometry(imaps[0])
        ncomp = 0 if len(shape) == 2 else shape[0]
    m = read_map(imaps[0], ncomp=ncomp)
    if args.verbose: print("Reading %s" % ihits[0])
    w = apply_edge(apply_apod(apply_trim(read_div(ihits[0], ncomp=ncomp))))
    if args.warn and np.any(w.preflat[0] < 0):
        print("Negative weight in %s" % ihits[0])
    wm = mul(w, m)

    for i, (mif, wif) in enumerate(zip(imaps[1:], ihits[1:])):
        if args.verbose: print("Reading %s" % mif)
        try:
            mi = read_map(mif, m.shape, m.wcs, ncomp=ncomp)
        except (IOError, OSError):
            if args.allow_missing:
                print("Can't read %s. Skipping" % mif)
                continue
            else:
                raise
        if args.verbose: print("Reading %s" % wif)

        wi = apply_edge(
            apply_apod(apply_trim(read_div(wif, m.shape, m.wcs, ncomp=ncomp))))
        if args.warn and np.any(wi.preflat[0] < 0):
            print("Negative weight in %s" % ihits[i + 1])
        ## We may need to reproject maps
        #if mi.shape != m.shape or str(mi.wcs.to_header()) != str(m.wcs.to_header()):
        #	mi = enmap.extract(mi, m.shape, m.wcs)
        #	wi = enmap.extract(wi, w.shape, w.wcs)
        w = add(w, wi)
        wm = add(wm, mul(wi, mi))

    if args.verbose: print("Solving")
    m = solve(w, wm)
    if args.verbose: print("Writing %s" % omap)
    enmap.write_map(omap, m)
    if args.verbose: print("Writing %s" % ohit)
    enmap.write_map(ohit, w)
Beispiel #16
0
    def get_foreground(self, seed=None, freq=148, verbose=True, input_kappa=None, post_processes=[],
                            save_output=True, flux_cut=7, polfix=True, dtype=np.float64, fgmaps_148=None, overwrite=False):
        assert(freq in self.freqs)
        try:
            assert(seed is not None)
            assert(not overwrite)
            if verbose:
                print(f"trying to load saved foregrounds. sim idx: {seed}, freq: {freq}GHz")
            fgmaps = enmap.empty((5,)+self.shape, self.wcs, dtype=dtype)
            for i, compt_idx in reversed(list(enumerate(self.fg_compts))):
                fname = self.get_output_file_name(compt_idx, seed, freq=freq)
                fgmaps[i] = enmap.read_map(fname)
        except:
            if verbose:
                print(f"generating foregrounds. sim idx: {seed}, freq: {freq}GHz")

            fgmaps = None    
            if fgmaps_148 is not None:
                fgmaps = fgmaps_148.copy()
            if freq == 148 and fgmaps is None: 
                fgmaps = self._generate_foreground_148GHz(seed=seed, verbose=verbose, input_kappa=input_kappa,
                                                  post_processes=post_processes, 
                                                  flux_cut=flux_cut, polfix=polfix)
            else:
                if fgmaps_148 is None:
                    fgmaps = self.get_foreground(seed=seed, freq=148, verbose=verbose, input_kappa=input_kappa,
                        post_processes=post_processes, save_output=save_output, flux_cut=flux_cut, polfix=polfix, dtype=np.float32)  

                fgmaps[2] *= fnu(freq)/fnu(148)
                for i in [3,4]:
                    compt_idx = self.fg_compts[i]
                    spec_index = self._get_spectral_index(seed=seed, compt_idx=self.fg_compts[i], freq=freq) 
                    fgmaps[i] *= thermo2jysr(148)*(freq/148)**spec_index*jysr2thermo(freq); del spec_index
                   
            if save_output:
                for i, compt_idx in enumerate(self.fg_compts):
                    fname = self.get_output_file_name(compt_idx, seed, freq=freq)
                    if os.path.exists(fname) and not overwrite: continue 
                    os.makedirs(os.path.dirname(fname), exist_ok=True)
                    enmap.write_map(fname, fgmaps[i].astype(np.float32))
        return fgmaps.astype(dtype)
Beispiel #17
0
    def get_combined_map(self, seed=None, freq=148, verbose=True, input_kappa=None, post_processes=[],
            save_output=True, flux_cut=7, polfix=True, dtype=np.float64, fgmaps_148=None, tmap=None, fgmaps=None, overwrite=False):
        
        
        fname = self.get_output_file_name('combined', seed, freq=freq, polidx='T')
        try:
            print("Loading combiend map")
            assert(not overwrite)
            cmap = enmap.read_map(fname)
        except:
            if fgmaps is None:
                fgmaps = self.get_foreground(seed, freq, verbose, input_kappa, post_processes, save_output,flux_cut, polfix, dtype, fgmaps_148, overwrite)    
            if tmap is None:
                if input_kappa is None and fgmaps is not None:
                    tmap = self.get_temperature_map(seed, fgmaps[0], save_output,  verbose, overwrite=overwrite, dtype=dtype)

            cmap = tmap+np.sum(fgmaps[1:,...], axis=0)

            if save_output:
                os.makedirs(os.path.dirname(fname), exist_ok=True)
                enmap.write_map(fname, cmap.astype(np.float32))
        return cmap.astype(dtype)
Beispiel #18
0
def merge_results(wdir,
                  odir,
                  geo,
                  tshape=(1000, 1000),
                  margin=100,
                  verbose=False):
    if verbose: print("Reducing")
    shape, wcs = geo
    shape = np.array(shape[-2:])
    ny, nx = (shape + tshape - 1) // tshape
    # Get the tile catalogs and their area of responsibility
    cats = []
    boxes = []
    for ty in range(ny):
        for tx in range(nx):
            tyx = np.array([ty, tx])
            pixbox = np.array(
                [tyx * tshape,
                 np.minimum((tyx + 1) * tshape, shape)])
            boxes.append(np.sort(enmap.pixbox2skybox(*geo, pixbox), 0))
            cats.append(
                pointsrcs.read_sauron(wdir + "/cat_%03d_%03d.fits" % (ty, tx)))
    if verbose: print("Merging %d cats" % len(cats))
    cat = merge_tiled_cats(cats, boxes)
    cat.ra = utils.rewind(cat.ra)
    pointsrcs.write_sauron("%s/cat.fits" % odir, cat)
    pointsrcs.write_sauron("%s/cat.txt" % odir, cat)
    for name in ["map", "model", "resid", "map_snr", "resid_snr"]:
        paths = [[
            "%s/%s_%03d_%03d.fits" % (wdir, name, ty, tx) for tx in range(nx)
        ] for ty in range(ny)]
        if not os.path.isfile(paths[0][0]): continue
        if verbose: print("Merging tiles for %s" % name)
        dtype = enmap.read_map(paths[0][0]).dtype
        map = merge_tiles(*geo, paths, dtype=dtype, margin=margin)
        enmap.write_map("%s/%s.fits" % (odir, name), map)
        del map
Beispiel #19
0
def write_results(odir, res, padding=0, tag=None):
    def unpad(map):
        if padding == 0: return map
        else: return map[..., padding:-padding, padding:-padding]

    def fix(map):
        return unpad(enmap.apply_window(map)) / res.fconvs[:, None, None, None]

    utils.mkdir(odir)
    suffix = "" if tag is None else "_" + tag
    enmap.write_map("%s/map%s.fits" % (odir, suffix), fix(res.maps))
    enmap.write_map("%s/model%s.fits" % (odir, suffix), fix(res.model))
    enmap.write_map("%s/resid%s.fits" % (odir, suffix),
                    fix(res.maps - res.model))
    if res.snr is not None:
        enmap.write_map("%s/map_snr%s.fits" % (odir, suffix), unpad(res.snr))
    if res.resid_snr is not None:
        enmap.write_map("%s/resid_snr%s.fits" % (odir, suffix),
                        unpad(res.resid_snr))
    # If we have indices of the catalog objects into a predefined catalog, then
    # append that as a new field, so we can use it in merge_results later
    cat = recfunctions.append_fields(
        res.cat, "inds", res.inds) if res.inds is not None else res.cat
    pointsrcs.write_sauron("%s/cat%s.fits" % (odir, suffix), cat)
Beispiel #20
0
            entry = filedb.data[scan_id]

            if args.keep_sidelobe is False:
                remove_sidelobe_cut(entry)

            try:
                data = read_metadata(entry)
            except errors.DataMissing as e:
                print('rank {:3d}: {} skipped ({})'.format(
                    comm.Get_rank(), scan_id, e.message))
                continue

            nocommon = None if args.nocommon == -1 else args.nocommon

            map_cuts(entry,
                     data,
                     hitmap,
                     cutmap,
                     keep_buffer=args.keep_buffer,
                     nocommon_frac=nocommon)

        comm.Barrier()
        hitmap = utils.reduce(hitmap, comm)
        cutmap = utils.reduce(cutmap, comm)

        if comm.Get_rank() == 0:
            # Up to now cutmap is hits - cuts. We want cuts only.
            cutmap = hitmap - cutmap
            enmap.write_map(opj(outdir_sub, 'hits.fits'), hitmap[0])
            enmap.write_map(opj(outdir_sub, 'cuts.fits'), cutmap[0])
# In[ ]:


# # Check sky area
# fSky = np.sum(footMask) / np.prod(footMask.shape)
# print "sky area =", fSky * footMask.area() * (180./np.pi)**2, "deg2"

# # look at the footprint
# plot=enplot.plot(footMask, downgrade=16)
# enplot.show(plot)


# In[ ]:


enmap.write_map(pathOut+"mask_foot.fits", footMask)


# # Planck Galactic mask

# In[ ]:


# Read the Planck Galactic mask
pathPlanckMask = "/global/cscratch1/sd/eschaan/project_ksz_act_planck/data/planck_galactic_mask/HFI_Mask_GalPlane-apo0_2048_R2.00.fits"
# field, fsky:
#0, 0.20
#1, 0.40
#2, 0.60
#3, 0.70
#4, 0.80
Beispiel #22
0
###################################################################################3
# Difference between 150GHz reconv to the 90GHz beam and the 90GHz map
# for null test (tSZ and kSZ estimators)

pathMap1 = "/global/cscratch1/sd/eschaan/project_ksz_act_planck/data/planck_act_coadd_2020_02_28_r2/" + "act_planck_s08_s18_cmb_f150_daynight_map_reconvto90.fits"
pathMap2 = "/global/cscratch1/sd/eschaan/project_ksz_act_planck/data/planck_act_coadd_2020_02_28_r2/" + "act_planck_s08_s18_cmb_f090_daynight_map.fits"
pathMask = "./output/cmb_map/pact20200228_r2/" + "mask_full_foot_gal60_ps.fits"
pathDirOut = "./output/cmb_map/planck_act_coadd_2020_02_28_r2/"
if not os.path.exists(pathDirOut):
    os.makedirs(pathDirOut)
pathOut = pathDirOut + "act_planck_s08_s18_cmb_f150reconvto90_minus_f090_daynight_map.fits"

# save the difference map
diffMap = enmap.read_map(pathMap1)
diffMap -= enmap.read_map(pathMap2)
enmap.write_map(pathOut, diffMap)
# copy the mask
copyfile(pathMask, pathDirOut + "mask_full_foot_gal_ps.fits")

###################################################################################3
# Difference between TileC y and TileC y no CIB
# for CIB null test (tSZ estimator)

pathMap1 = "./output/cmb_map/tilec_pact_y_v1.2.0/" + "tilec_reconv2.4_map.fits"
pathMap2 = "./output/cmb_map/tilec_pact_ynocib_v1.2.0/" + "tilec_map.fits"
pathMask = "./output/cmb_map/tilec_pact_y_v1.2.0/" + "mask_full_foot_gal_ps.fits"
pathDirOut = "./output/cmb_map/tilec_pact_yminusynocib_v1.2.0/"
if not os.path.exists(pathDirOut):
    os.makedirs(pathDirOut)
pathOut = pathDirOut + "diff_map.fits"
Beispiel #23
0
                fnoise,fivars = ngen[dmname].generate_sim(season=season,patch=patch,array=farray,seed=noise_seed,apply_ivar=False)
                print(fnoise.shape,fivars.shape)
                if farray=='pa3': 
                    ind150 = dm.array_freqs['pa3'].index('pa3_f150')
                    ind090 = dm.array_freqs['pa3'].index('pa3_f090')
                    pa3_cache['pa3_f150'] = (fnoise[ind150].copy(),fivars[ind150].copy())
                    pa3_cache['pa3_f090'] = (fnoise[ind090].copy(),fivars[ind090].copy())
                    ind = dm.array_freqs['pa3'].index(arrayname)
                else:
                    ind = 0
                noise = fnoise[ind]
                ivars = fivars[ind]

            splits = actnoise.apply_ivar_window(signal[None,None]+noise[None],ivars[None])
            assert splits.shape[0]==1
            enmap.write_map(fname,splits[0])

        sim_splits.append(fname)

    
    """
    k-space coadd
    """



    """
    SAVE COV
    """
    print("Beginning covariance calculation...")
    with bench.show("sim cov"):
Beispiel #24
0
                       cval=0.0,
                       force=False,
                       prefilter=True,
                       mask_nan=True,
                       safe=True)

# Plot the combined maps to check
plot = enplot.plot(tilecMask, grid=True)
enplot.write(pathFig + "foot_mask", plot)

plot = enplot.plot(tilecMap, grid=True)
enplot.write(pathFig + "map", plot)

# Save the map now, to clear memory
print "saving map to " + pathOut + "tilec_maps.fits"
enmap.write_map(pathOut + "tilec_map.fits", tilecMap)

#########################################################################
# Mask the Milky Way with a Planck mask

# Read the Planck Galactic mask
pathPlanckMask = "/global/cscratch1/sd/eschaan/project_ucsc/data/planck_galactic_mask/HFI_Mask_GalPlane-apo0_2048_R2.00.fits"
# field, fsky:
#0, 0.20
#1, 0.40
#2, 0.60
#3, 0.70
#4, 0.80
#5, 0.90
#6, 0.97
#7, 0.99
pathMap = "/global/cscratch1/sd/eschaan/project_ksz_act_planck/data/planck_act_coadd_2020_02_28_r2/" + "act_planck_s08_s18_cmb_f150_daynight_map.fits"

# read maps
iMap = enmap.read_map(pathMap)
# do the reconvolution in Fourier space
mapF = enmap.fft(iMap)
lMap = np.sqrt(np.sum(iMap.lmap()**2,0))


##########################################################################
# Reconvolve the map

print("Reconvolve 150 to 90")
pathOutMap = "/global/cscratch1/sd/eschaan/project_ksz_act_planck/data/planck_act_coadd_2020_02_28_r2/" + "act_planck_s08_s18_cmb_f150_daynight_map_reconvto90.fits"
oMap = enmap.ifft(mapF * fBeam90F(lMap) / fBeam150F(lMap)).real
enmap.write_map(pathOutMap, oMap)
print("check finite sum "+str(np.sum(oMap)))


print("Reconvolve 150 to TileC deproj")
pathOutMap = "/global/cscratch1/sd/eschaan/project_ksz_act_planck/data/planck_act_coadd_2020_02_28_r2/" + "act_planck_s08_s18_cmb_f150_daynight_map_reconvtotilecdeproj.fits"
oMap = enmap.ifft(mapF * fBeamTilecDeprojF(lMap) / fBeam150F(lMap)).real
enmap.write_map(pathOutMap, oMap)
print("check finite sum "+str(np.sum(oMap)))


print("Reconvolve 150 to TileC")
pathOutMap = "/global/cscratch1/sd/eschaan/project_ksz_act_planck/data/planck_act_coadd_2020_02_28_r2/" + "act_planck_s08_s18_cmb_f150_daynight_map_reconvtotilec.fits"
oMap = enmap.ifft(mapF * fBeamTilecF(lMap) / fBeam150F(lMap)).real
enmap.write_map(pathOutMap, oMap)
print("check finite sum "+str(np.sum(oMap)))
Beispiel #26
0
for sim_id in np.arange(core_start, core_end):
    print("Looking at set=%d sim_id=%d" % (set_id, sim_id))

    # check if simulation already exists
    postfix = "set%d_id%d.fits" % (set_id, sim_id)
    if apply_rotation:
        filename = "fullskyalpha_%s" % postfix
        filename = os.path.join(alpha_map_dir, filename)
        if not os.path.isfile(filename):
            # generate full sky rotation map
            print("File doesn't exist!")
            raise Exception
        else:
            alpha_map = enmap.read_map(filename)
    else:  # if rotation is not needed, alpha_map will not be useful
        alpha_map = None

    # generate rotated lensed cmb + noise + foregrounds + beam sims at act patches
    sg = SimGen(version=map_version, cmb_type='LensedUnabberatedCMB',
                apply_rotation=apply_rotation, alpha_map=alpha_map, max_cached=max_cached)
    for psa in psa_list:
        print("Generating sim for %s" % psa)
        map_patch = sg.get_sim(**psa, sim_num=sim_id)
        filename = "{patch}_{season}_{array}_{postfix}".format(patch=psa["patch"], season=psa["season"], array=psa["array"], postfix=postfix)
        filename = os.path.join(output_dir, filename)
        print("Saving data: %s..." % filename)
        enmap.write_map(filename, map_patch)
        del map_patch
    del alpha_map
def generate_map(qids, overwrite=False, verbose=True, dg=2, **kwargs):
    # Here we compute the real coadd map and simulated maps from the noise covariance and pre-computed fullsky signal
    # This function uses actsims "simgen" code

    for qid in qids:

        if qid in ['boss_d04', 's16_d03', 'boss_04']:
            # Here, the simulation is performed for each array, not frequency, to take into account their correlation
            # For pa3, there are two frequency bands
            if verbose:
                print('skip ' + qid +
                      ': the map is generated by running f090 case.')
            continue

        if '_d0' in qid:
            version = 'v6.3.0_calibrated_mask_version_masks_20200723'
        else:
            version = 'v6.3.0_calibrated_mask_version_padded_v1'

        # define qid_array to take into account multi-frequency case
        if qid == 'boss_d03':
            qid_array = ['boss_d03', 'boss_d04']
        elif qid == 's16_d02':
            qid_array = ['s16_d02', 's16_d03']
        elif qid == 'boss_03':
            qid_array = ['boss_03', 'boss_04']
        else:
            # single frequency case
            qid_array = [qid]

        # define filename
        aobj = {
            q: local.init_analysis_params(qid=q, **kwargs)
            for q in qid_array
        }

        # load survey mask
        mask = load_survey_mask(qid, dg=1)

        # Define an object for sim generation
        model, season, array, patch, freq = local.qid_info(qid)
        if model == 'dr5':
            simobj = simgen.SimGen(version=version, qid=qid_array, model=model)
        if model == 'act_mr3':
            simobj = simgen.SimGen(version=version, model=model)

        # save 1/var map
        if not misctools.check_path(aobj[qid_array[0]].fivar,
                                    overwrite=overwrite,
                                    verbose=verbose):
            ivars = get_ivar(qid_array, mask)
            civars = np.average(ivars, axis=1)  # coadd ivar
            for qi, q in enumerate(qid_array):
                enmap.write_map(aobj[q].fivar, civars[qi, :, :, :])

        # loop over realizations
        for i in tqdm.tqdm(aobj[qid].rlz):

            if misctools.check_path(aobj[qid].fmap['s'][i],
                                    overwrite=overwrite,
                                    verbose=verbose):
                continue

            # maps will have signal ('s') and noise ('n') and each has [Nfreq, NTPol, Coord1, Coord2]
            maps = {}

            # real data
            if i == 0:

                # take coadd data
                maps['s'] = coadd_real_data(qid_array, mask)

                # SZ map subtraction
                shape, wcs = maps['s'][0, 0, :, :].shape, maps['s'].wcs
                if q in [
                        'boss_d03', 's16_d02', 'boss_03'
                ]:  # need both freqs for combined data array of two freqs
                    sz_map_090 = enmap.project(
                        enmap.read_map(
                            'data_local/input/S18d_202006_confirmed_model_f090.fits'
                        ), shape, wcs) / local.Tcmb
                    sz_map_150 = enmap.project(
                        enmap.read_map(
                            'data_local/input/S18d_202006_confirmed_model_f150.fits'
                        ), shape, wcs) / local.Tcmb
                    maps['s'][0, 0, :, :] -= sz_map_090
                    maps['s'][1, 0, :, :] -= sz_map_150
                else:
                    sz_map = enmap.project(
                        enmap.read_map(
                            'data_local/input/S18d_202006_confirmed_model_f150.fits'
                        ), shape, wcs) / local.Tcmb
                    maps['s'][0, 0, :, :] -= sz_map

            # simulation
            else:
                maps['s'], maps['n'], ivars = simobj.get_sim(season,
                                                             patch,
                                                             array,
                                                             sim_num=i,
                                                             set_idx=0)

                # coadd with civars weight implicitly multiplied
                civars = np.average(ivars, axis=1)
                civars[civars == 0] = np.inf
                maps['s'] = mask[None, None, :, :] * np.average(
                    maps['s'] * ivars, axis=1) / civars / local.Tcmb
                maps['n'] = mask[None, None, :, :] * np.average(
                    maps['n'] * ivars, axis=1) / civars / local.Tcmb

                # downgrade
                maps['s'] = enmap.downgrade(maps['s'], dg)
                maps['n'] = enmap.downgrade(maps['n'], dg)

            # save signal and noise to files
            for s in ['s', 'n']:
                if s == 'n' and i == 0:
                    continue  # real data is saved to a signal file
                for qi, q in enumerate(qid_array):
                    enmap.write_map(aobj[q].fmap[s][i], maps[s][qi, :, :, :])
Beispiel #28
0
                                                 lmin=300,
                                                 lmax=8000,
                                                 wnoise_annulus=500,
                                                 bin_annulus=20,
                                                 lknee_guess=3000,
                                                 alpha_guess=-4,
                                                 method="fft",
                                                 radial_fit=True)

#io.plot_img(np.fft.fftshift(np.log10(ndown)),"ndown.png",aspect='auto',lim=[-6,3])
#io.hplot(np.fft.fftshift(np.log10(ndown)),"hndown")
io.hplot(np.fft.fftshift((ndown)), "hndown")
io.plot_img(np.fft.fftshift(ndown / nfitted), "nunred.png", aspect='auto')

nmod = ndown / nfitted
enmap.write_map("anisotropy_template.fits", enmap.samewcs(nmod, npower))

shape, wcs = maps.rect_geometry(width_deg=50.,
                                height_deg=30,
                                px_res_arcmin=0.5)
rms = 10.0
lknee = 3000
alpha = -3
n2d = covtools.get_anisotropic_noise(shape, wcs, rms, lknee, alpha)
modlmap = enmap.modlmap(shape, wcs)
bin_edges = np.arange(100, 8000, 100)
binner = stats.bin2D(modlmap, bin_edges)
cents, n1d = binner.bin(n2d)

pl = io.Plotter(yscale='log', xlabel='l', ylabel='C')
pl.add(cents, n1d)
Beispiel #29
0
		nside = healpy.npix2nside(imap.shape[-1])
		lmax  = args.lmax or 3*nside
		progress("%s TQU alm2map" % name)
		alm   = curvedsky.map2alm_healpix(imap, lmax=lmax)
		del imap
		# work around healpix bug
		progress("%s TQU rotate_alm" % name)
		alm   = alm.astype(np.complex128,copy=False)
		healpy.rotate_alm(alm, euler[0], euler[1], euler[2])
		alm   = alm.astype(ctype,copy=False)
		progress("%s TQU map2alm" % name)
		curvedsky.alm2map_cyl(alm, omap)
		del alm
		ofile = ifile[:-5] + "_map.fits"
		progress("%s TQU write %s" % (name, ofile))
		enmap.write_map(ofile, omap)

def get_pixsize_rect(shape, wcs):
	"""Return the exact pixel size in steradians for the rectangular cylindrical
	projection given by shape, wcs. Returns area[ny], where ny = shape[-2] is the
	number of rows in the image. All pixels on the same row have the same area."""
	ymin  = enmap.sky2pix(shape, wcs, [-np.pi/2,0])[0]
	ymax  = enmap.sky2pix(shape, wcs, [ np.pi/2,0])[0]
	y     = np.arange(shape[-2])
	x     = y*0
	dec1  = enmap.pix2sky(shape, wcs, [np.maximum(ymin, y-0.5),x])[0]
	dec2  = enmap.pix2sky(shape, wcs, [np.minimum(ymax, y+0.5),x])[0]
	area  = np.abs((np.sin(dec2)-np.sin(dec1))*wcs.wcs.cdelt[0]*np.pi/180)
	return area

if "ivar" in outputs or "map0" in outputs:
Beispiel #30
0
def lens_map(imap):
    return plensing.displace_map(imap, alpha, order=args.lens_order)


# Map of distances from center
modrmap = enmap.modrmap(shape, wcs)
# The convergence ~ projected mass density
kappa = lensing.nfw_kappa(massOverh,
                          modrmap,
                          cc,
                          zL=z,
                          concentration=c,
                          overdensity=180.,
                          critical=False,
                          atClusterZ=False)
if rank == 0: enmap.write_map(f'{savedir}kappa.fits', kappa)

# Deflection field
alpha = lensing.alpha_from_kappa(kappa)
# Fourier magnitude map
modlmap = enmap.modlmap(shape, wcs)

# Unlensed CMB theory
theory = cosmology.default_theory()
cltt2d = theory.uCl('TT', modlmap)
clte2d = theory.uCl('TE', modlmap)
clee2d = theory.uCl('EE', modlmap)
power = np.zeros((3, 3, shape[0], shape[1]))
power[0, 0] = cltt2d
power[1, 1] = clee2d
power[1, 2] = clte2d