Beispiel #1
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 #2
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)
Beispiel #3
0
def read_geometry(fname):
    if os.path.isdir(fname):
        geo = retile.read_tileset_geometry(fname + "/tile%(y)03d_%(x)03d.fits")
        return geo.shape, geo.wcs
    else:
        return enmap.read_map_geometry(fname)
Beispiel #4
0
L = log.init(level=log_level)
ncomp = args.ncomp
assert ncomp == 1 or ncomp == 3, "Only 1 or 3 components supported"

# Read the input maps
L.info("Reading " + args.ihealmap)
imap    = np.atleast_2d(healpy.read_map(args.ihealmap, field=tuple(range(args.first,args.first+ncomp))))
nside   = healpy.npix2nside(imap.shape[-1])
mask    = imap < -1e20
dtype   = imap.dtype
bsize   = 100

if args.unit != 1: imap[~mask]/= args.unit

# Read the template
shape, wcs = enmap.read_map_geometry(args.template)
shape = (args.ncomp,)+shape[-2:]

# Allocate our output map
omap   = enmap.zeros(shape, wcs, dtype)
nblock = (omap.shape[-2]+bsize-1)//bsize

for bi in range(nblock):
	r1 = bi*bsize
	r2 = (bi+1)*bsize
	print "Processing row %5d/%d" % (r1, omap.shape[-2])
	
	# Output map coordinates
	osub = omap[...,r1:r2,:]
	pmap = osub.posmap()
	# Coordinate transformation
Beispiel #5
0
assert ncomp == 1 or ncomp == 3, "Only 1 or 3 components supported"

# Read the input maps
L.info("Reading " + args.ihealmap)
imap = np.atleast_2d(
    healpy.read_map(args.ihealmap,
                    field=tuple(range(args.first, args.first + ncomp))))
nside = healpy.npix2nside(imap.shape[-1])
mask = imap < -1e20
dtype = imap.dtype
bsize = 100

if args.unit != 1: imap[~mask] /= args.unit

# Read the template
shape, wcs = enmap.read_map_geometry(args.template)
shape = (args.ncomp, ) + shape[-2:]

# Allocate our output map
omap = enmap.zeros(shape, wcs, dtype)
nblock = (omap.shape[-2] + bsize - 1) // bsize

for bi in range(nblock):
    r1 = bi * bsize
    r2 = (bi + 1) * bsize
    print "Processing row %5d/%d" % (r1, omap.shape[-2])

    # Output map coordinates
    osub = omap[..., r1:r2, :]
    pmap = osub.posmap()
    # Coordinate transformation
Beispiel #6
0
parser.add_argument("-a", "--apod",    type=int,   default=30, help="The width of the apodization region, in pixels.")
parser.add_argument("--apod-margin",   type=int,   default=10, help="How far away from the apod region a source should be to be valid.")
parser.add_argument("-s", "--nsigma",  type=float, default=3.5, help="The number a sigma a source must be to be included in the catalog when finding sources.")
parser.add_argument("-p", "--pad",     type=int,   default=60, help="The number of pixels to extend each region by in each direciton, to avoid losing sources at region boundaries. Should be larger than apod+apod_margin")
parser.add_argument("-P", "--prior",   type=float, default=1.0, help="The strength of the input prior in fit mode. Actually the inverse of the source variability assumed, so 0 means the source will be assumed to be infinitely variable, and hence the input database amplitudes don't add anything to the new fit. infinity means that the source is completley stable, and the input statistics add in inverse variance to the measurements. The default is 1, which means that the input database contributes only at the 1 sigma level")
parser.add_argument("-v", "--verbose", action="store_true")
parser.add_argument("-o", "--output",  type=str,   default="full,reg", help="What types of output to write to the output directory. Comma-separated list. 'full': Output the final, merged quantities. 'reg': Output per-region results. 'debug': Output lots of debug maps. 'maps': Output maps and not just catalogs at each level.")
parser.add_argument(      "--ncomp",   type=int,   default=1, help="The number of stokes components to fit for in fit mode.")
parser.add_argument("--hack",          type=float, default=0)
args = parser.parse_args()
import numpy as np, os
from enlib import dory
from enlib import enmap, utils, bunch, mpi, fft, pointsrcs

comm       = mpi.COMM_WORLD
shape, wcs = enmap.read_map_geometry(args.imap)
beam       = dory.get_beam(args.beam)
regions    = dory.get_regions(args.regions, shape, wcs)

def divdiag(div):
	if   div.ndim == 2: return div.preflat
	elif div.ndim == 3: return div
	elif div.ndim == 4: return enmap.samewcs(np.einsum("aayx->ayx",div),div)
	else: raise ValueError("Invalid div shape: %s" % div.shape)

def get_div(div, ci):
	if len(div) > ci: return div[ci]
	else: return div[0]*2

def get_beam_profile(beam, nsamp=10001, rmax=0, tol=1e-7):
	# First do a low-res run to find rmax
Beispiel #7
0
parser.add_argument(      "--ncomp",   type=int,   default=1, help="The number of stokes components to fit for in fit mode.")
parser.add_argument(      "--prune",   type=int,   default=0)
parser.add_argument(      "--rlim",    type=float, default=0.25)
parser.add_argument(      "--hack",    type=float, default=0)
parser.add_argument(      "--split",   action="store_true")
parser.add_argument(      "--split-nimage", type=int,   default=16)
parser.add_argument(      "--split-dist",   type=float, default=1)
parser.add_argument(      "--split-minflux",type=float, default=300)

args = parser.parse_args()
import numpy as np, os
from enlib import dory
from enlib import enmap, utils, bunch, mpi, fft, pointsrcs

comm       = mpi.COMM_WORLD
shape, wcs = enmap.read_map_geometry(args.imap)
beam       = dory.get_beam(args.beam)
regions    = dory.get_regions(args.regions, shape, wcs)
if args.rsplit:
	regions = dory.split_regions(regions, args.rsplit)

def divdiag(div):
	if   div.ndim == 2: return div.preflat
	elif div.ndim == 3: return div
	elif div.ndim == 4: return enmap.samewcs(np.einsum("aayx->ayx",div),div)
	else: raise ValueError("Invalid div shape: %s" % div.shape)

def get_div(div, ci):
	if len(div) > ci: return div[ci]
	else: return div[0]*2
Beispiel #8
0
			res = float(param["res"])*utils.arcmin
			tol = float(param["tol"])*utils.degree
			col_major = True
			patterns, mypids = scanutils.classify_scanning_patterns(active_scans, comm=comm, tol=tol)
			L.info("Found %d scanning patterns" % len(patterns))
			if comm.rank == 0:
				print patterns/utils.degree
			# Define our phase maps
			nrow, ncol = active_scans[0].dgrid
			array_dets = np.arange(nrow*ncol)
			if col_major: array_dets = array_dets.reshape(nrow,ncol).T.reshape(-1)
			det_unit   = nrow if col_major else ncol
			areas      = mapmaking.PhaseMap.zeros(patterns, array_dets, res=res, det_unit=det_unit, dtype=dtype)
			signal     = mapmaking.SignalPhase(active_scans, areas, mypids, comm, name=effname, ofmt=param["ofmt"], output=param["output"]=="yes")
		elif param["type"] == "noiserect":
			ashape, awcs = enmap.read_map_geometry(param["value"])
			leftright = int(param["leftright"]) > 0
			# Drift is in degrees per hour, but we want it per second
			drift = float(param["drift"])/3600
			area = enmap.zeros((args.ncomp*(1+leftright),)+ashape[-2:], awcs, dtype)
			# Find the duration of each tod. We need this for the y offsets
			nactive = utils.allgather(np.array(len(active_scans)), comm)
			offs    = utils.cumsum(nactive, endpoint=True)
			durs    = np.zeros(np.sum(nactive))
			for i, scan in enumerate(active_scans): durs[offs[comm.rank]+i] = scan.nsamp/scan.srate
			durs    = utils.allreduce(durs, comm)
			ys      = utils.cumsum(durs)*drift
			my_ys   = ys[offs[comm.rank]:offs[comm.rank+1]]
			# That was surprisingly cumbersome
			signal  = mapmaking.SignalNoiseRect(active_scans, area, drift, my_ys, comm, name=effname, mode=param["mode"], ofmt=param["ofmt"], output=param["output"]=="yes")
		elif param["type"] == "srcsamp":
Beispiel #9
0
 def get_area_file(area):
     return enmap.read_map_geometry(area)
Beispiel #10
0
    parser.add_argument("--srcsub", type=int, default=1)
    parser.add_argument("-M", "--mapsub", type=str, default=None)
    parser.add_argument("-I", "--inject", type=str, default=None)
    parser.add_argument("--only", type=str)
    parser.add_argument("--static", action="store_true")
    parser.add_argument("-c", "--cont", action="store_true")
    parser.add_argument("-D", "--dayerr", type=str, default="-1:1,-2:4")
    parser.add_argument("--srclim-day", type=float, default=50)
    # These should ideally be moved into the general tod autocuts
    parser.add_argument("-a", "--asteroid-file", type=str, default=None)
    parser.add_argument("--asteroid-list", type=str, default=None)
    args = parser.parse_args()

    comm = mpi.COMM_WORLD
    utils.mkdir(args.odir)
    shape, wcs = enmap.read_map_geometry(args.area)
    wshape = (3, ) + shape[-2:]
    dtype = np.float32 if config.get("map_bits") == 32 else np.float64
    root = args.odir + "/" + (args.prefix + "_" if args.prefix else "")
    sys = config.get("map_sys")
    ym = utils.arcmin / utils.yr2days
    # Bias source amplitudes 0.1% towards their fiducial value
    amp_prior = 1e-3

    dayerr = np.array([[float(w) for w in tok.split(":")]
                       for tok in args.dayerr.split(",")
                       ]).T  # [[x1,y1],[x2,y2]]

    only = [int(word) for word in args.only.split(",")] if args.only else []

    # Should we use distributed maps?
Beispiel #11
0
			res = float(param["res"])*utils.arcmin
			tol = float(param["tol"])*utils.degree
			col_major = True
			patterns, mypids = scanutils.classify_scanning_patterns(active_scans, comm=comm, tol=tol)
			L.info("Found %d scanning patterns" % len(patterns))
			if comm.rank == 0:
				print(patterns/utils.degree)
			# Define our phase maps
			nrow, ncol = active_scans[0].dgrid
			array_dets = np.arange(nrow*ncol)
			if col_major: array_dets = array_dets.reshape(nrow,ncol).T.reshape(-1)
			det_unit   = nrow if col_major else ncol
			areas      = mapmaking.PhaseMap.zeros(patterns, array_dets, res=res, det_unit=det_unit, dtype=dtype)
			signal     = mapmaking.SignalPhase(active_scans, areas, mypids, comm, name=effname, ofmt=param["ofmt"], output=param["output"]=="yes")
		elif param["type"] == "noiserect":
			ashape, awcs = enmap.read_map_geometry(get_map_path(param["value"]))
			leftright = int(param["leftright"]) > 0
			# Drift is in degrees per hour, but we want it per second
			drift = float(param["drift"])/3600
			area = enmap.zeros((args.ncomp*(1+leftright),)+ashape[-2:], awcs, dtype)
			# Find the duration of each tod. We need this for the y offsets
			nactive = utils.allgather(np.array(len(active_scans)), comm)
			offs    = utils.cumsum(nactive, endpoint=True)
			durs    = np.zeros(np.sum(nactive))
			for i, scan in enumerate(active_scans): durs[offs[comm.rank]+i] = scan.nsamp/scan.srate
			durs    = utils.allreduce(durs, comm)
			ys      = utils.cumsum(durs)*drift
			my_ys   = ys[offs[comm.rank]:offs[comm.rank+1]]
			# That was surprisingly cumbersome
			signal  = mapmaking.SignalNoiseRect(active_scans, area, drift, my_ys, comm, name=effname, mode=param["mode"], ofmt=param["ofmt"], output=param["output"]=="yes")
		elif param["type"] == "srcsamp":
Beispiel #12
0
from __future__ import division, print_function
import numpy as np, argparse, os
from enlib import retile, mpi, utils, enmap
parser = argparse.ArgumentParser()
parser.add_argument("imap")
parser.add_argument("template", nargs="?")
parser.add_argument("odir")
parser.add_argument("-t", "--tsize", type=int, default=675)
parser.add_argument("-s", "--slice", type=str, default=None)
args = parser.parse_args()
tfile = args.template or args.imap

# Find the bounds of our tiling
shape, wcs = enmap.read_map_geometry(tfile)
box = enmap.box(shape, wcs, corner=False)
tshape = (args.tsize, args.tsize)
ntile = tuple([(s + t - 1) // t for s, t in zip(shape[-2:], tshape)])

utils.mkdir(args.odir)
opathfmt = args.odir + "/tile%(y)03d_%(x)03d.fits"

retile.retile(args.imap,
              opathfmt,
              otilenum=ntile,
              ocorner=box[0],
              otilesize=tshape,
              verbose=True,
              slice=args.slice)