Пример #1
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
Пример #2
0
def read_signal_map(signal_map, freq, shape=None, wcs=None):
    '''
    Return signal map for given frequency. Optionally, 
    crop to match shape of other (smaller) map.
    
    Parameters
    ----------
    signal_map : str or sequence of strings.
        Path to .fits file or list of paths. Filenames
        must contain unique "fXXX" identifier. If single
        path is given, must contain "{freq}" as placeholder.
    freq : str
        Frequency, e.g. "f150".
    shape : tuple, optional
        Desired shape.
    wcs : astropy.wcs.wcs.WCS object, optional
        WCS of desired cropped map.

    Return
    ------
    signal_map : enmap
        Signal map for given frequency.

    Raises
    ------
    ValueError
        If no suitable signal map is found.
    '''

    signal_file = None
    # Input is string or list of strings.
    if isinstance(signal_map, basestring):
        signal_file = signal_map.replace('{freq}', freq)
    else:
        for path in signal_map:
            _, filename = os.path.split(path)
            if freq in filename:
                signal_file = path
                break
    if signal_file is None:
        raise ValueError('No signal map found for freq: {}'.format(freq))

    if shape is not None and wcs is not None:
        shape_signal, wcs_signal = enmap.read_map_geometry(signal_file)
        sel = get_slice(shape[-2::], wcs, shape_signal, wcs_signal)
    else:
        sel = None

    signal = enmap.read_map(signal_file, sel=sel)

    return signal
Пример #3
0
def allocate_output(area):
    '''
    Parameters
    ----------
    area : str
        Absolute path to footprint map.

    Returns
    -------
    hitmap : (1, ny, nx) enmap
    cutmap : (1, ny, nx) enmap
    '''

    shape, wcs = enmap.read_map_geometry(area)
    hitmap = enmap.zeros((1, ) + shape[-2:], wcs, dtype)
    cutmap = hitmap * 0

    return hitmap, cutmap
Пример #4
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)
Пример #5
0
def load_geometries(qids):
    geoms = {}
    for qid in qids:
        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]()
        print(season, region, array, freq)
        shape, wcs = enmap.read_map_geometry(
            dm.get_split_fname(season=season,
                               patch=region,
                               array=array + "_" +
                               freq if not (is_planck(qid)) else freq,
                               splitnum=0,
                               srcfree=True))
        geoms[qid] = shape[-2:], wcs
    return geoms
Пример #6
0
def merge_tiles(shape, wcs, paths, margin=100, dtype=np.float32):
    # Get the pre-dimensions from the first tile
    shape = enmap.read_map_geometry(paths[0][0])[0][:-2] + shape[-2:]
    omap = enmap.zeros(shape, wcs, dtype)
    ny = len(paths)
    nx = len(paths[0])
    for ty in range(ny):
        for tx in range(nx):
            fname = paths[ty][tx]
            imap = enmap.read_map(fname)
            h, w = imap.shape[-2:]
            wy = make_edge_interp(h - 2 * margin,
                                  margin,
                                  ty > 0,
                                  ty < ny - 1,
                                  dtype=imap.dtype)
            wx = make_edge_interp(w - 2 * margin, margin, dtype=imap.dtype)
            imap = imap * wy[:, None] * wx[None, :]
            omap.insert(imap, op=np.ndarray.__iadd__)
    return omap
Пример #7
0
    def tiles(self, from_file=False):
        comm = self.comm
        for i in range(comm.rank, len(self.pboxes), comm.size):
            eshape, ewcs = slice_geometry_by_pixbox(self.ishape, self.iwcs,
                                                    self.pboxes[i])

            if from_file:
                extracter = lambda x, **kwargs: self._prepare(
                    enmap.read_map(x,
                                   pixbox=enmap.pixbox_of(
                                       enmap.read_map_geometry(x)[1], eshape,
                                       ewcs),
                                   **kwargs))
            else:
                extracter = lambda x: self._prepare(
                    enmap.extract_pixbox(x, enmap.pixbox_of(
                        x.wcs, eshape, ewcs)))
            inserter = lambda inp, out: enmap.insert_at(out,
                                                        self.ipboxes[i],
                                                        self._finalize(inp),
                                                        op=np.ndarray.__iadd__)
            yield i, extracter, inserter, eshape, ewcs
Пример #8
0
rstep = 100
nside = 0
verbose = args.verbose
outputs = args.outputs.split(",")
if len(outputs) == 0:
	print("No outputs selected - nothing to do")
	sys.exit(0)
t0 = time.time()

def progress(msg):
	if verbose:
		print("%6.2f %6.2f %6.2f %s" % ((time.time()-t0)/60, memory.current()/1024.**3, memory.max()/1024.**3, msg))

comm = mpi.COMM_WORLD

shape, wcs = enmap.read_map_geometry(args.template)
shape = shape[-2:]
progress("Allocating output map %s %s" % (str((3,)+shape), str(dtype)))
if "map" in outputs:
	for ifile in args.ifiles[comm.rank::comm.size]:
		name = os.path.basename(ifile)
		runit = unit
		if "545" in name: 
			runit *= factor_545
			npol = 1
		elif "857" in name: 
			runit *= factor_857
			npol = 1
		elif ("smica" in name) or ("WPR2" in name):
			npol = 1
		elif ("COM_Mask_Lensing" in name):
Пример #9
0
args = parser.parse_args()

utils.mkdir(args.odir)
comm = mpi.COMM_WORLD
dtype = np.float32 if config.get("map_bits") == 32 else np.float64
ncomp = 3
tsize = 720
root = args.odir + "/" + (args.prefix + "_" if args.prefix else "")
down = config.get("downsample")
# Set up logging
utils.mkdir(root + ".log")
logfile = root + ".log/log%03d.txt" % comm.rank
log_level = log.verbosity2level(config.get("verbosity"))
L = log.init(level=log_level, file=logfile, rank=comm.rank, shared=True)
# Set up our geometry
shape, wcs = enmap.read_map_geometry(args.area)
shape = (ncomp, ) + shape[-2:]
msys = config.get("map_sys")
dist = config.get("map_dist")
# Filter parameters
filter_fknee = 0.2
filter_alpha = -3

# Get our tod list
filedb.init()
ids = todinfo.get_tods(args.sel, filedb.scans)

# Dump our settings
if comm.rank == 0:
    config.save(root + "config.txt")
    with open(root + "args.txt", "w") as f:
Пример #10
0
"""
Loads a catalog
Maps it
Smooths it
Thresholds it
Projects it onto ACT
This gives a mask of 1s and 0s from which a random catalog can be made
"""


paths = cutils.paths
#cat_type = "wise_panstarrs"
#cat_type = "madcows_photz"
cat_type = args.sys[1]
meanfield = False

# cat_type = "sdss_redmapper"
# meanfield = True

shape,wcs = enmap.fullsky_geometry(res=1 * utils.degree)
ras,decs,_ = cutils.catalog_interface(cat_type,is_meanfield=meanfield)
cmapper = catalogs.CatMapper(ras,decs,shape=shape,wcs=wcs)
cmap = maps.binary_mask(enmap.smooth_gauss(cmapper.counts,2 * utils.degree),1e-3)
io.hplot(cmap,'counts')


shape,wcs = enmap.read_map_geometry(paths.coadd_data + f"act_planck_s08_s18_cmb_f150_daynight_srcfree_map.fits")
omap = enmap.project(cmap,shape,wcs,order=0)
io.plot_img(omap,'pcounts')
enmap.write_map(f'{paths.scratch}{cat_type}_mask.fits',omap)
Пример #11
0
def read_helper(fname, shape=None, wcs=None):
    if shape is None: return enmap.read_map(fname)
    mshape, mwcs = enmap.read_map_geometry(fname)
    pixbox = enmap.pixbox_of(mwcs, shape, wcs)
    return enmap.read_map(fname, pixbox=pixbox)
Пример #12
0
from __future__ import print_function
from orphics import maps, io, cosmology, stats
from pixell import enmap
from enlib import bench
import numpy as np
import os, sys
from tilec import utils as tutils, covtools, ilc, fg as tfg
from szar import foregrounds as fg

c = tutils.Config()

shape, wcs = enmap.read_map_geometry("output/datacov.hdf")
shape = shape[-2:]
Ny, Nx = shape
modlmap = enmap.modlmap(shape, wcs)
kbeams = []
freqs = []
kcoadds = []
for i, array in enumerate(c.arrays):
    freqs.append(c.darrays[array]['freq'])
    kbeams.append(c.get_beam(modlmap, array))
    kcoadds.append(c.load(i, skip_splits=True)[1].copy())
kcoadds = enmap.enmap(np.stack(kcoadds), c.wcs)
chunk_size = 1000000

theory = cosmology.default_theory()

cov = enmap.read_map("output/datacov.hdf")
cov = maps.symmat_from_data(cov)

tcmb = 2.726e6