def __init__(self, shape, wcs, pattern, offset, site, pad=2.0*utils.degree, order=0, subsample=2.0): """Build an unskew operator that uses spline interpolation along an azimuth sweep to straighten out the scanning motion for one scanning pattern. Relatively slow, and leads to some smoothing due to the interpolation, but does not assume that dec changes with a constant speed during a sweep.""" # Find the unskew transformation for this pattern. # We basically want dec->az and ra->ra0, with az spacing # similar to el spacing. ndec, nra = shape[-2:] info = calc_az_sweep(pattern, offset, site, pad=pad, subsample=subsample) sweep_ra, sweep_dec = info.sweep_cel #(sweep_ra, sweep_dec), naz, daz = calc_az_sweep(pattern, offset, site, pad=pad, subsample=subsample) # We want to be able to go from (y,x) to (ra,dec), with # dec = dec[y] # ra = ra[y]-ra[0]+x # Precompute the pixel mapping. This will have the full witdh in ra, # but will be smaller in dec due to the limited az range. raw_dec, raw_ra = enmap.posmap(shape, wcs) skew_pos = np.zeros((2, info.naz, nra)) skew_pos[0] = sweep_dec[:,None] skew_pos[1] = (sweep_ra-sweep_ra[0])[:,None] + raw_ra[None,ndec/2,:] skew_pix = enmap.sky2pix(shape, wcs, skew_pos).astype(dtype) # Build geometry for the unskewed system ushape, uwcs = enmap.geometry(pos=[0,0], shape=[info.naz, nra], res=[np.abs(info.daz), enmap.pixshape(shape,wcs)[1]], proj="car") # Save self.order = order self.shape = shape self.wcs = wcs self.pattern = pattern self.site = site self.skew_pix = skew_pix # External interface self.ushape = ushape self.uwcs = uwcs
def make_maps(tod, data, pos, ncomp, radius, resolution): tod = tod.copy() pos = np.array(pos) # Handle angle wrapping pos = utils.rewind(pos, data.ref) nsrc= len(pos) dbox= np.array([[-1,-1],[1,1]])*radius shape, wcs = enmap.geometry(pos=dbox, res=resolution) # Set up pixels n = int(np.round(2*radius/resolution)) boxes = np.array([[p-radius,p+radius] for p in pos]) # Set up output maps rhs = enmap.zeros((nsrc,ncomp) +shape, wcs, dtype=dtype) div = enmap.zeros((ncomp,nsrc,ncomp)+shape, wcs, dtype=dtype) # Build rhs ptsrc_data.nmat_basis(tod, data) pmat_thumbs(-1, tod, rhs, boxes, data) # Build div for c in range(ncomp): idiv = div[0].copy(); idiv[:,c] = 1 wtod = data.tod.astype(dtype,copy=True); wtod[...] = 0 pmat_thumbs( 1, wtod, idiv, boxes, data) ptsrc_data.nmat_basis(wtod, data, white=True) pmat_thumbs(-1, wtod, div[c], boxes, data) div = np.rollaxis(div,1) mask = div[:,0] != 0 bin = rhs.copy() bin[mask] = rhs[mask]/div[:,0][mask] # Fixme: only works for ncomp == 1 return bin, rhs, div
def mask_map(imap, iys, ixs, hole_arc, hole_frac=0.6): shape, wcs = imap.shape, imap.wcs Ny, Nx = shape[-2:] px = maps.resolution(shape, wcs) * 60. * 180. / np.pi hole_n = int(round(hole_arc / px)) hole_ny = hole_nx = hole_n oshape, owcs = enmap.geometry(pos=(0., 0.), shape=(2 * hole_n, 2 * hole_n), res=px * np.pi / 180. / 60.) modrmap = enmap.modrmap(oshape, owcs) mask = enmap.ones(shape, wcs) for iy, ix in zip(iys, ixs): if iy <= hole_n or ix <= hole_n or iy >= (Ny - hole_n) or ix >= ( Nx - hole_n): continue vslice = imap[np.int(iy - hole_ny):np.int(iy + hole_ny), np.int(ix - hole_nx):np.int(ix + hole_nx)] vslice[modrmap < (hole_frac * hole_arc) * np.pi / 180. / 60.] = np.nan # !!!! could cause a bias mask[np.int(iy - hole_ny):np.int(iy + hole_ny), np.int(ix - hole_nx):np.int(ix + hole_nx)][modrmap < hole_arc * np.pi / 180. / 60.] = 0 return mask
def make_maps(tod, data, pos, ncomp, radius, resolution): tod = tod.copy() pos = np.array(pos) # Handle angle wrapping pos = utils.rewind(pos, data.ref) nsrc = len(pos) dbox = np.array([[-1, -1], [1, 1]]) * radius shape, wcs = enmap.geometry(pos=dbox, res=resolution) # Set up pixels n = int(np.round(2 * radius / resolution)) boxes = np.array([[p - radius, p + radius] for p in pos]) # Set up output maps rhs = enmap.zeros((nsrc, ncomp) + shape, wcs, dtype=dtype) div = enmap.zeros((ncomp, nsrc, ncomp) + shape, wcs, dtype=dtype) # Build rhs ptsrc_data.nmat_basis(tod, data) pmat_thumbs(-1, tod, rhs, boxes, data) # Build div for c in range(ncomp): idiv = div[0].copy() idiv[:, c] = 1 wtod = data.tod.astype(dtype, copy=True) wtod[...] = 0 pmat_thumbs(1, wtod, idiv, boxes, data) ptsrc_data.nmat_basis(wtod, data, white=True) pmat_thumbs(-1, wtod, div[c], boxes, data) div = np.rollaxis(div, 1) mask = div[:, 0] != 0 bin = rhs.copy() bin[mask] = rhs[mask] / div[:, 0][mask] # Fixme: only works for ncomp == 1 return bin, rhs, div
def map_likelihood(likfun, rmax=5*utils.arcmin, res=0.7*utils.arcmin): shape, wcs = enmap.geometry(np.array([[-1,-1],[1,1]])*rmax, res=res, proj="car") map = enmap.zeros(shape, wcs) pos = map.posmap() for y in range(map.shape[0]): for x in range(map.shape[1]): map[y,x] = likfun(pos[:,y,x]/utils.arcmin) return map
def likgrid(self, R, n, super=1, marg=False, verbose=False): shape, wcs = enmap.geometry(pos=np.array([[-R,-R],[R,R]]), shape=(n,n), proj="car") dchisqs = enmap.zeros(shape, wcs) amps = enmap.zeros((self.nsrc,)+shape, wcs) pos = dchisqs.posmap() for i,p in enumerate(pos.reshape(2,-1).T): if np.sum(p**2)**0.5 > R: continue L = self.lik.eval(p) if verbose: print "%6d %7.3f %7.3f %15.7f" % (i, p[0]/utils.arcmin, p[1]/utils.arcmin, L.chisq0-L.chisq) dchisqs.reshape(-1)[i] = L.chisq0-L.chisq amps.reshape(self.nsrc,-1)[:,i] = L.amps if super > 1: # Use bicubic spline interpolation to upscale shape2, wcs2 = enmap.geometry(pos=np.array([[-R,-R],[R,R]]), shape=(n*super,n*super), proj="car") dchisqs = dchisqs.project(shape2, wcs2, mode="constant") amps = amps.project(shape2, wcs2, mode="constant") return dchisqs, amps
def get_area_str(area): # dec1:dec2,ra1:ra2 -> [[dec1,ra1],[dec2,ra2]] box = np.array([[float(w) for w in tok.split(":")] for tok in area.split(",")]).T * utils.degree box[:, 1] = np.sort(box[:, 1])[::-1] # standard ra ordering return enmap.geometry(box, res=0.5 * utils.arcmin, proj="car", ref=[0, 0])
def getKappaSZ(bSims,snap,massIndex,px,thetaMapshape): b = bSims PIX = 2048 maps, z, kappaSimDat, szMapuKDat, projectedM500, trueM500, trueR500, pxInRad, pxInRad = b.getMaps(snap,massIndex,freqGHz=150.) pxIn = pxInRad * 180.*60./np.pi hwidth = PIX*pxIn/2. print "At redshift ", z , " stamp is ", hwidth ," arcminutes wide." # input pixelization shapeSim, wcsSim = enmap.geometry(pos=[[-hwidth*arcmin,-hwidth*arcmin],[hwidth*arcmin,hwidth*arcmin]], res=pxIn*arcmin, proj="car") kappaSim = enmap.enmap(kappaSimDat,wcsSim) szMapuK = enmap.enmap(szMapuKDat,wcsSim) # downgrade to native shapeOut, wcsOut = enmap.geometry(pos=[[-hwidth*arcmin,-hwidth*arcmin],[hwidth*arcmin,hwidth*arcmin]], res=px*arcmin, proj="car") kappaMap = enmap.project(kappaSim,shapeOut,wcsOut) szMap = enmap.project(szMapuK,shapeOut,wcsOut) print thetaMapshape print szMap.shape if szMap.shape[0]>thetaMapshape[0]: kappaMap = enmap.project(kappaSim,thetaMapshape,wcsOut) szMap = enmap.project(szMapuK,thetaMapshape,wcsOut) else: diffPad = ((np.array(thetaMapshape) - np.array(szMap.shape))/2.+0.5).astype(int) apodWidth = 25 kappaMap = enmap.pad(enmap.apod(kappaMap,apodWidth),diffPad)[:-1,:-1] szMap = enmap.pad(enmap.apod(szMap,apodWidth),diffPad)[:-1,:-1] # print szMap.shape assert szMap.shape==thetaMap.shape # print z, projectedM500 # pl = Plotter() # pl.plot2d(kappaMap) # pl.done("output/kappasim.png") # pl = Plotter() # pl.plot2d(szMap) # pl.done("output/szsim.png") # sys.exit() # print "kappaint ", kappaMap[thetaMap*60.*180./np.pi<10.].mean() return kappaMap,szMap
def get_geometry(self,snap): # MAP DIMENSIONS: THIS NEEDS DEBUGGING z = self.snapToZ(snap) stampWidthMpc = old_div(8., self.h) comovingMpc = self.results.comoving_radial_distance(z) widthRadians = old_div(stampWidthMpc,comovingMpc) pixWidthRadians = old_div(widthRadians,self.PIX) shape,wcs = enmap.geometry(pos=np.array([[old_div(-widthRadians,2.),old_div(-widthRadians,2.)],[old_div(widthRadians,2.),old_div(widthRadians,2.)]]),res=pixWidthRadians,proj="car") return shape,wcs
def __init__(self, shape, wcs, pattern, offset, site, pad=2.0 * utils.degree): """This unskew operation assumes that equal spacing in dec corresponds to equal spacing in time, and that shifts in RA can be done in units of whole pixels. This is an approximation relative to UnskewCurved, but it is several times faster, uses less memory, and causes less smoothing.""" ndec, nra = shape[-2:] info = calc_az_sweep(pattern, offset, site, pad=pad) sweep_ra, sweep_dec = info.sweep_cel # For each pixel in dec (that we hit for this scanning pattern), we # want to know how far we have been displaced in ra. # First get the dec of each pixel center. ysweep, xsweep = enmap.sky2pix(shape, wcs, [sweep_dec, sweep_ra]) y1 = max(int(np.min(ysweep)), 0) y2 = min(int(np.max(ysweep)) + 1, shape[-2]) # Make fft-friendly ny = y2 - y1 ny2 = fft.fft_len(ny, "above", [2, 3, 5, 7]) y1 = max(y1 - (ny2 - ny) / 2, 0) y2 = min(y1 + ny2, shape[-2]) y = np.arange(y1, y2) dec, _ = enmap.pix2sky(shape, wcs, [y, y * 0]) # Then interpolate the ra values corresponding to those decs. # InterpolatedUnivariateSpline broken. Returns nan even when # interpolating. So we will use UnivariateSpline spline = scipy.interpolate.UnivariateSpline(sweep_dec, sweep_ra) ra = spline(dec) dra = ra - ra[len(ra) / 2] y, x = np.round(enmap.sky2pix(shape, wcs, [dec, ra])) dx = x - x[len(x) / 2] # It's also useful to be able to go from normal map index to # position in y and dx inv_y = np.zeros(shape[-2], dtype=int) - 1 inv_y[y.astype(int)] = np.arange(len(y)) # Compute the azimuth step size based on the total azimuth sweep. daz = (pattern[2] - pattern[1] + 2 * pad) / len(y) # Build the geometry of the unskewed system ushape, uwcs = enmap.geometry(pos=[0, 0], shape=[len(y), shape[-1]], res=[daz, enmap.pixshape(shape, wcs)[1]], proj="car") # And store the result self.y = y.astype(int) self.dx = np.round(dx).astype(int) self.dx_raw = dx self.inv_y = inv_y self.ushape = ushape self.uwcs = uwcs
def __init__(self, scans, pids, patterns, array_shape, res, dtype, comm, cuts=None, name="phase", ofmt="{name}_{pid:02}_{az0:.0f}_{az1:.0f}_{el:.0f}", output=True, ext="fits", col_major=True, hysteresis=True): Signal.__init__(self, name, ofmt, output, ext) nrow, ncol = array_shape ndet = nrow * ncol self.pids = pids self.patterns = patterns self.comm = comm self.dtype = dtype self.col_major = col_major self.cuts = cuts self.data = {} self.areas = [] # Set up an area for each scanning pattern. We assume that these are constant # elevation scans, so only azimuth matters. This setup is ugly and would be # nicer if passed in, but then the ugliness would only be moved to the calling # code instead. for pattern in patterns: az0, az1 = utils.widen_box(pattern)[:, 1] naz = int(np.ceil((az1 - az0) / res)) az1 = az0 + naz * res det_unit = nrow if col_major else ncol shape, wcs = enmap.geometry( pos=[[0, az0], [ndet / det_unit * utils.degree, az1]], shape=(ndet, naz), proj="car") if hysteresis: area = enmap.zeros((2, ) + shape, wcs, dtype=dtype) else: area = enmap.zeros(shape, wcs, dtype=dtype) self.areas.append(area) for pid, scan in zip(pids, scans): dets = scan.dets if col_major: dets = utils.transpose_inds(dets, nrow, ncol) mat = pmat.PmatScan(scan, self.areas[pid], dets) self.data[scan] = [pid, mat] self.dof = zipper.MultiZipper( [zipper.ArrayZipper(area, comm=comm) for area in self.areas], comm=comm)
def get_geometry_regions(ncomp, n, res, hole_radius): tshape, twcs = enmap.geometry(pos=(0, 0), shape=(n, n), res=res, proj='car') modrmap = enmap.modrmap(tshape, twcs) # Select the hole (m1) and context(m2) across all components amodrmap = np.repeat(modrmap.reshape((1, n, n)), ncomp, 0) m1 = np.where(amodrmap.reshape(-1) < hole_radius)[0] m2 = np.where(amodrmap.reshape(-1) >= hole_radius)[0] return m1, m2
def likgrid(self, R, n, super=1, marg=False, verbose=False): shape, wcs = enmap.geometry(pos=np.array([[-R, -R], [R, R]]), shape=(n, n), proj="car") dchisqs = enmap.zeros(shape, wcs) amps = enmap.zeros((self.nsrc, ) + shape, wcs) pos = dchisqs.posmap() for i, p in enumerate(pos.reshape(2, -1).T): if np.sum(p**2)**0.5 > R: continue L = self.lik.eval(p) if verbose: print "%6d %7.3f %7.3f %15.7f" % (i, p[0] / utils.arcmin, p[1] / utils.arcmin, L.chisq0 - L.chisq) dchisqs.reshape(-1)[i] = L.chisq0 - L.chisq amps.reshape(self.nsrc, -1)[:, i] = L.amps if super > 1: # Use bicubic spline interpolation to upscale shape2, wcs2 = enmap.geometry(pos=np.array([[-R, -R], [R, R]]), shape=(n * super, n * super), proj="car") dchisqs = dchisqs.project(shape2, wcs2, mode="constant") amps = amps.project(shape2, wcs2, mode="constant") return dchisqs, amps
def grid_pos(d, params, box=np.array([[-1,-1],[1,1]])*pos_rel_max, shape=(10,10)): # Build the pos grid p = params.copy() shape, wcs = enmap.geometry(pos=box, shape=shape, proj="car") probs = enmap.zeros(shape, wcs) pos_rel = probs.posmap() best = -np.inf for iy in range(shape[0]): for ix in range(shape[1]): p.pos_rel = pos_rel[:,iy,ix] P_s, P_w, adist_strong = calc_marginal_amps_strong(d, p) probs[iy,ix] = P_s + P_w best = max(best,probs[iy,ix]) print "%4d %4d %6.2f %6.2f %9.3f %9.3f %9.3f %s" % ((iy,ix)+tuple(pos_rel[:,iy,ix]/m2r)+(probs[iy,ix],P_s,P_w) + ("*" if probs[iy,ix]>=best else "",)) return probs
def __init__(self, shape, wcs, pattern, offset, site, pad=2.0*utils.degree): """This unskew operation assumes that equal spacing in dec corresponds to equal spacing in time, and that shifts in RA can be done in units of whole pixels. This is an approximation relative to UnskewCurved, but it is several times faster, uses less memory, and causes less smoothing.""" ndec, nra = shape[-2:] info = calc_az_sweep(pattern, offset, site, pad=pad) sweep_ra, sweep_dec = info.sweep_cel # For each pixel in dec (that we hit for this scanning pattern), we # want to know how far we have been displaced in ra. # First get the dec of each pixel center. ysweep, xsweep = enmap.sky2pix(shape, wcs, [sweep_dec,sweep_ra]) y1 = max(int(np.min(ysweep)),0) y2 = min(int(np.max(ysweep))+1,shape[-2]) # Make fft-friendly ny = y2-y1 ny2 = fft.fft_len(ny, "above", [2,3,5,7]) y1 = max(y1-(ny2-ny)/2,0) y2 = min(y1+ny2,shape[-2]) y = np.arange(y1,y2) dec, _ = enmap.pix2sky(shape, wcs, [y,y*0]) # Then interpolate the ra values corresponding to those decs. # InterpolatedUnivariateSpline broken. Returns nan even when # interpolating. So we will use UnivariateSpline spline = scipy.interpolate.UnivariateSpline(sweep_dec, sweep_ra) ra = spline(dec) dra = ra - ra[len(ra)/2] y, x = np.round(enmap.sky2pix(shape, wcs, [dec,ra])) dx = x-x[len(x)/2] # It's also useful to be able to go from normal map index to # position in y and dx inv_y = np.zeros(shape[-2],dtype=int)-1 inv_y[y.astype(int)]= np.arange(len(y)) # Compute the azimuth step size based on the total azimuth sweep. daz = (pattern[2]-pattern[1]+2*pad)/len(y) # Build the geometry of the unskewed system ushape, uwcs = enmap.geometry(pos=[0,0], shape=[len(y),shape[-1]], res=[daz,enmap.pixshape(shape,wcs)[1]], proj="car") # And store the result self.y = y.astype(int) self.dx = np.round(dx).astype(int) self.dx_raw = dx self.inv_y = inv_y self.ushape = ushape self.uwcs = uwcs
def __init__(self, scan, srcpos, res=0.25*utils.arcmin, rad=20*utils.arcmin, perdet=False, detoff=10*utils.arcmin): if perdet: # Offset each detector's pointing so that we produce a grid of images, one per detector. gside = int(np.ceil(scan.ndet**0.5)) goffs = np.mgrid[:gside,:gside] - (gside-1)/2.0 goffs = goffs.reshape(2,-1).T[:scan.ndet]*detoff scan.offsets = scan.offsets.copy() scan.offsets[:,1:] += goffs rad = rad + np.max(np.abs(goffs)) # Build geometry for each source shape, wcs = enmap.geometry(pos=[[-rad,-rad],[rad,rad]], res=res, proj="car") area = enmap.zeros((3,)+shape, wcs, dtype=scan.tod.dtype) self.pmats = [] for i, pos in enumerate(srcpos.T): if planet: sys = src_sys else: sys = ["icrs",[np.array([[pos[0]],[pos[1]],[0],[0]]),False]] with config.override("pmat_accuracy", 10): self.pmats.append(pmat.PmatMap(scan, area, sys=sys)) self.shape = (len(srcpos.T),3)+shape self.wcs = wcs
def grid_pos(d, params, box=np.array([[-1, -1], [1, 1]]) * pos_rel_max, shape=(10, 10)): # Build the pos grid p = params.copy() shape, wcs = enmap.geometry(pos=box, shape=shape, proj="car") probs = enmap.zeros(shape, wcs) pos_rel = probs.posmap() best = -np.inf for iy in range(shape[0]): for ix in range(shape[1]): p.pos_rel = pos_rel[:, iy, ix] P_s, P_w, adist_strong = calc_marginal_amps_strong(d, p) probs[iy, ix] = P_s + P_w best = max(best, probs[iy, ix]) print "%4d %4d %6.2f %6.2f %9.3f %9.3f %9.3f %s" % ( (iy, ix) + tuple(pos_rel[:, iy, ix] / m2r) + (probs[iy, ix], P_s, P_w) + ("*" if probs[iy, ix] >= best else "", )) return probs
def __init__(self, data, srcpos, res=0.25*utils.arcmin, rad=20*utils.arcmin, perdet=False, detoff=10*utils.arcmin): scan = actscan.ACTScan(data.entry, d=data) if perdet: # Offset each detector's pointing so that we produce a grid of images, one per detector. gside = int(np.ceil(data.ndet**0.5)) goffs = np.mgrid[:gside,:gside] - (gside-1)/2.0 goffs = goffs.reshape(2,-1).T[:data.ndet]*detoff scan.offsets = scan.offsets.copy() scan.offsets[:,1:] += goffs rad = rad + np.max(np.abs(goffs)) # Build geometry for each source shape, wcs = enmap.geometry(pos=[[-rad,-rad],[rad,rad]], res=res, proj="car") area = enmap.zeros((3,)+shape, wcs, dtype=data.tod.dtype) self.pmats = [] for i, pos in enumerate(srcpos.T): if planet: sys = src_sys else: sys = ["icrs",[np.array([[pos[0]],[pos[1]],[0],[0]]),False]] with config.override("pmat_accuracy", 10): self.pmats.append(pmat.PmatMap(scan, area, sys=sys)) self.shape = (len(srcpos.T),3)+shape self.wcs = wcs
dets.append(det) offs.append(off) boxes.append(box) imaps.append(imap) box = utils.bounding_box(boxes) box = utils.widen_box(box, rad * 5, relative=False) # We assume that the two maps have the same pixelization imaps = enmap.samewcs(np.array(imaps), imaps[0]) # Downsample by averaging imaps = enmap.downgrade(imaps, (1, args.step)) naz = imaps.shape[-1] # Ok, build our output geometry shape, wcs = enmap.geometry(pos=box, res=args.res * utils.arcmin, proj="car", pre=(naz, )) omap = enmap.zeros(shape, wcs, dtype=dtype) # Normalization norm = enmap.zeros(shape[-2:], wcs) norm[0, 0] = 1 norm = enmap.smooth_gauss(norm, rad)[0, 0] # Loop through slices and populate bazs = [] for iaz in range(naz): # Get our boresight az bazs.append(imaps.pix2sky([0, iaz])[1]) vals = [] for i in range(nfile):
from __future__ import print_function from orphics import maps,io,cosmology,catalogs from enlib import enmap,bench import numpy as np shape,wcs = maps.rect_geometry(width_deg=20.,px_res_arcmin=0.5) bigmap = enmap.zeros(shape,wcs) Nobj = 1000 ras,decs = catalogs.random_catalog(shape,wcs,Nobj,edge_avoid_deg=1.) arcmin_width = 30. res = np.min(bigmap.extent()/bigmap.shape[-2:])*180./np.pi*60. Npix = int(arcmin_width/res)*1. if Npix%2==0: Npix += 1 cshape,cwcs = enmap.geometry(pos=(0.,0.),res=res/(180./np.pi*60.),shape=(Npix,Npix)) cmodrmap = enmap.modrmap(cshape,cwcs) sigmas = [] for ra,dec in zip(ras,decs): iy,ix = enmap.sky2pix(shape,wcs,(dec*np.pi/180.,ra*np.pi/180.)) sigma = np.random.normal(3.0,1.0)*np.pi/180./60. paste_in = np.exp(-cmodrmap**2./2./sigma**2.) bigmap[int(iy-Npix/2):int(iy+Npix/2),int(ix-Npix/2):int(ix+Npix/2)] += paste_in sigmas.append(sigma) io.plot_img(bigmap,"cat.png",high_res=True) print("done")
sys.stderr.write("splitting %d:[%s] tods into %d splits via %d blocks%s" % ( ntod, atolist(nper), nsplit, nblock, (" with %d:%d free:fixed" % (nfree,nfixed)) if nfixed > 0 else "") + "\n") # We assume that site and pointing offsets are the same for all tods, # so get them based on the first one entry = filedb.data[ids[0]] site = actdata.read(entry, ["site"]).site # Determine the bounding box of our selected data bounds = db.data["bounds"].reshape(2,-1).copy() bounds[0] = utils.rewind(bounds[0], bounds[0,0], 360) box = utils.widen_box(utils.bounding_box(bounds.T), 4*args.rad, relative=False) waz, wel = box[1]-box[0] # Use fullsky horizontally if we wrap too far if waz <= 180: shape, wcs = enmap.geometry(pos=box[:,::-1]*utils.degree, res=args.res*utils.degree, proj="car", ref=(0,0)) else: shape, wcs = enmap.fullsky_geometry(res=args.res*utils.degree) y1, y2 = np.sort(enmap.sky2pix(shape, wcs, [box[:,1]*utils.degree,[0,0]])[0].astype(int)) shape, wcs = enmap.slice_geometry(shape, wcs, (slice(y1,y2),slice(None))) sys.stderr.write("using %s workspace with resolution %.2f deg" % (str(shape), args.res) + "\n") # Get the hitmap for each block hits = enmap.zeros((nblock,narray)+shape, wcs) ndig = calc_ndig(nblock) sys.stderr.write("estimating hitmap for block %*d/%d" % (ndig,0,nblock)) for bi in range(nblock): for ai in range(narray): block_db = db.select(block_inds[bi,ai]) hits[bi,ai] = fastweight.fastweight(shape, wcs, block_db, array_rad=args.rad*utils.degree, site=site, weight=args.weight)
# Find azimuth scanning speed aspeed = np.median(np.abs(d.boresight[1,1:]-d.boresight[1,:-1])[::10])*d.srate tref = d.boresight[0,d.nsamp/2] # Build a small, high-res map around each source sdata = [] with bench.mark("scan"): scan = actscan.ACTScan(entry, d=d) pcut = pmat.PmatCut(scan) junk = np.zeros(pcut.njunk, dtype) pcut.backward(tod, junk) #wtod = apply_bivar(tod*0+1,bivar,bsize_ivar,inplace=True) wtod = tod*0+ivar[:,None] pcut.backward(wtod, junk) for sid in sids: shape, wcs = enmap.geometry(pos=[srcpos[::-1,sid]-R,srcpos[::-1,sid]+R], res=res, proj="car") area = enmap.zeros(shape, wcs, dtype) with bench.mark("pmap"): pmap = pmat.PmatMap(scan, area) rhs = enmap.zeros((3,)+shape, wcs, dtype) div = rhs*0 with bench.mark("rhs"): pmap.backward(tod, rhs) with bench.mark("div"): pmap.backward(wtod,div) div = div[0] map = rhs.copy() map[:,div>0] /= div[div>0] map = map[0] # Crop the outermost pixel, where outside hits will have accumulated map, div, area = [m[...,1:-1,1:-1] for m in [map,div,area]]
def enmaps_from_config(Config, sim_section, analysis_section, pol=False): """ Algorithm for deciding sim and analysis shapes and wcs: Check if user has specified a *data* template * If yes, use its shape and wcs for the data - Determine ratio simpixel/anpixel - Upsample by that ratio to make sim template * If no, """ pixel_sim = Config.getfloat(sim_section, "pixel_arcmin") buffer_sim = Config.getfloat(sim_section, "buffer") projection = Config.get(analysis_section, "projection") try: pt_file = Config.get(analysis_section, "patch_template") imap = enmap.read_map(pt_file) shape_dat = imap.shape wcs_dat = imap.wcs res = np.min(imap.extent() / imap.shape[-2:]) * 60. * 180. / np.pi if np.isclose(pixel_sim, res, 1.e-2): shape_sim = shape_dat wcs_sim = wcs_dat else: bbox = enmap.box(shape_dat, wcs_dat) shape_sim, wcs_sim = enmap.geometry(bbox, res=pixel_sim * np.pi / 180. / 60., proj=projection) except: pixel_analysis = Config.getfloat(analysis_section, "pixel_arcmin") try: width_analysis_deg = Config.getfloat(analysis_section, "patch_degrees_width") except: width_analysis_deg = Config.getfloat(analysis_section, "patch_arcmin_width") / 60. try: height_analysis_deg = Config.getfloat(analysis_section, "patch_degrees_height") except: height_analysis_deg = Config.getfloat(analysis_section, "patch_arcmin_height") / 60. ra_offset = Config.getfloat(analysis_section, "ra_offset") dec_offset = Config.getfloat(analysis_section, "dec_offset") shape_dat, wcs_dat = maps.rect_geometry( width_analysis_deg * 60., pixel_analysis, proj=projection, pol=pol, height_arcmin=height_analysis_deg * 60., xoffset_degree=ra_offset, yoffset_degree=dec_offset) if np.abs(buffer_sim - 1.) < 1.e-3: shape_sim, wcs_sim = maps.rect_geometry( width_analysis_deg * 60., pixel_sim, proj=projection, pol=pol, height_arcmin=height_analysis_deg * 60., xoffset_degree=ra_offset, yoffset_degree=dec_offset) else: raise NotImplementedError("Buffer !=1 not implemented") return shape_sim, wcs_sim, shape_dat, wcs_dat
# This program computes a simple estimation of the amount of distortion the # flat-sky approximation involves import numpy as np, argparse from enlib import enmap parser = argparse.ArgumentParser() parser.add_argument("omap") parser.add_argument("-D", "--diameter", type=float, default=30) parser.add_argument("-n", "--npix", type=int, default=800) parser.add_argument("--proj", type=str, default="cea") args = parser.parse_args() r = args.diameter*np.pi/180/2 shape, wcs = enmap.geometry(pos=[[-r,-r],[r,r]], shape=(args.npix, args.npix), proj=args.proj) def linpos(n,b): return b[0] + (np.arange(n)+0.5)*(b[1]-b[0])/n alpha = enmap.zeros((2,)+shape, wcs) pos = enmap.posmap(shape, wcs) # I'm not sure how to compute this in general, so here's a specialization # for cylindrical projections if args.proj == "cea" or args.proj =="car": dec = pos[0,:,0] ra = pos[1,0,:] lindec= linpos(shape[0],enmap.box(shape,wcs)[:,0]) scale = 1/np.cos(dec)-1 alpha[0] = (lindec-dec)[:,None] alpha[1] = ra[None,:]*scale[:,None] enmap.write_map(args.omap, alpha*180*60/np.pi)
def __init__(self, shape, wcs=None, bbpix=None, tshape=None, dtype=None, comm=None, bbox=None, pre=None): """DGeometry(shape, wcs, bbpix, tshape) or DGeometry(dgeometry) Construct a DMap geometry. When constructed explicitly from shape, wcs, etc., it is a relatively expensive operation, as MPI communication is necessary. Constructing one based on an existing DGeometry is fast. bbox indicates the bounds [{from,to},{lat,lon}] of the area of the map of interest to each mpi task, and will in general be different for each task. bbpix is the same as bbox, but expressed in units of pixels. It is allowed to pass a list of bounding boxes, in which case each mpi task will have multiple work spaces. tshape specifies the tiling scheme to use. The global geometry will be split into tiles of tshape dimensions (except at the edges, as no tile will extend beyond the edge of the full map), and tiles will be stored in a distributed fashion between mpi tasks based on the degree of overlap with their workspaces.""" try: # We are pretty lightweight, so copy everything properly. This is # less error prone than having changes in one object suddenly affect # another. comm must *not* be deepcopied, as that breaks it. for key in ["shape", "tshape", "comm", "dtype"]: setattr(self, key, getattr(shape, key)) for key in [ "wcs", "tile_pos", "tile_boxes", "tile_geometry", "work_geometry", "tile_ownership", "tile_glob2loc", "tile_loc2glob", "tile_bufinfo", "work_bufinfo" ]: setattr(self, key, copy.deepcopy(getattr(shape, key))) except AttributeError: # 1. Set up basic properties assert shape is not None if wcs is None: _, wcs = enmap.geometry(pos=np.array([[-1, -1], [1, 1]]) * 5 * np.pi / 180, shape=shape[-2:]) if comm is None: comm = mpi.COMM_WORLD if tshape is None: tshape = (720, 720) if dtype is None: dtype = np.float64 if bbpix is None: if bbox is None: bbpix = [[0, 0], shape[-2:]] else: bbpix = box2pix(shape, wcs, bbox) nphi = int(np.round(np.abs(360 / wcs.wcs.cdelt[0]))) # Reorder from/to if necessary, and expand to [:,2,2] bbpix = sanitize_pixbox(bbpix, shape) dtype = np.dtype(dtype) self.shape, self.wcs, self.bbpix = tuple( shape), wcs.deepcopy(), np.array(bbpix) self.tshape, self.dtype, self.comm = tuple(tshape), dtype, comm # 2. Set up workspace descriptions work_geometry = [ enmap.slice_geometry( shape, wcs, (slice(b[0, 0], b[1, 0]), slice(b[0, 1], b[1, 1])), nowrap=True) for b in bbpix ] # 3. Define global workspace ownership nwork = utils.allgather([len(bbpix)], comm) wown = np.concatenate( [np.full(n, i, dtype=int) for i, n in enumerate(nwork)]) # 3. Define tiling. Each tile has shape tshape, starting from the (0,0) corner # of the full map. Tiles at the edge are clipped, as pixels beyond the edge # of the full map may have undefined wcs positions. tbox = build_tiles(shape, tshape) bshape = tbox.shape[:2] tbox = tbox.reshape(-1, 2, 2) ntile = len(tbox) tile_indices = np.array( [np.arange(ntile) / bshape[1], np.arange(ntile) % bshape[1]]).T # 4. Define tile ownership. # a) For each task compute the overlap of each tile with its workspaces, and # concatenate across tasks to form a [nworktot,ntile] array. wslices = select_nonempty( # slices into work utils.allgatherv(utils.box_slice(bbpix, tbox), comm, axis=0), # normal utils.allgatherv(utils.box_slice(bbpix + [0, nphi], tbox), comm, axis=0)) # wrapped tslices = select_nonempty( # slices into tiles utils.allgatherv(utils.box_slice(tbox, bbpix), comm, axis=1), # normal utils.allgatherv(utils.box_slice(tbox, bbpix + [0, nphi]), comm, axis=1)) # wrapped # b) Compute the total overlap each mpi task has with each tile, and use this # to decide who should get which tiles overlaps = utils.box_area(wslices) overlaps = utils.sum_by_id(overlaps, wown, 0) town = assign_cols_round_robin(overlaps) # Map tile indices from local to global and back tgmap = [[] for i in range(comm.size)] tlmap = np.zeros(ntile, dtype=int) for ti, id in enumerate(town): tlmap[ti] = len(tgmap[id]) # glob 2 loc tgmap[id].append(ti) # loc 2 glob # 5. Define tiles tile_geometry = [ enmap.slice_geometry( shape, wcs, (slice(tb[0, 0], tb[1, 0]), slice(tb[0, 1], tb[1, 1]))) for tb in tbox ] # 6. Define mapping between work<->wbuf and tiles<->tbuf wbufinfo = np.zeros([2, comm.size], dtype=int) tbufinfo = np.zeros([2, comm.size], dtype=int) winfo, tinfo = [], [] woff, toff = 0, 0 prelen = np.product(shape[:-2]) for id in xrange(comm.size): ## Buffer info to send to alltoallv wbufinfo[1, id] = woff tbufinfo[1, id] = toff # Slices for transfering to and from w buffer. Loop over all of my # workspaces and determine the slices into them and how much we need # to send. for tloc, tglob in enumerate(np.where(town == id)[0]): for wloc, wglob in enumerate( np.where(wown == comm.rank)[0]): ws = wslices[wglob, tglob] wlen = utils.box_area(ws) * prelen work_slice = (Ellipsis, slice(ws[0, 0], ws[1, 0]), slice(ws[0, 1], ws[1, 1])) wbuf_slice = slice(woff, woff + wlen) winfo.append((wloc, work_slice, wbuf_slice)) woff += wlen # Slices for transferring to and from t buffer. Loop over all # my tiles, and determine how much I have to receive from each # workspace of each task. for tloc, tglob in enumerate(np.where(town == comm.rank)[0]): for wloc, wglob in enumerate(np.where(wown == id)[0]): ts = tslices[tglob, wglob] tlen = utils.box_area(ts) * prelen tile_slice = (Ellipsis, slice(ts[0, 0], ts[1, 0]), slice(ts[0, 1], ts[1, 1])) tbuf_slice = slice(toff, toff + tlen) tinfo.append((tloc, tile_slice, tbuf_slice)) toff += tlen wbufinfo[0, id] = woff - wbufinfo[1, id] tbufinfo[0, id] = toff - tbufinfo[1, id] wbufinfo, tbufinfo = tuple(wbufinfo), tuple(tbufinfo) # TODO: store tbox? loc_geometry vs. tile_geometry, etc. # 7. Store # [ntile,2]: position of each (global) tile in grid self.tile_pos = tile_indices # [ntile,2,2]: pixel box for each (global) tile self.tile_boxes = tbox # [ntile,(shape,wcs)]: geometry of each (global) tile self.tile_geometry = tile_geometry # [nwork,(shape,wcs)]: geometry of each local workspace self.work_geometry = work_geometry # [ntile]: rank of owner of each tile self.tile_ownership = town # [ntile]: local index of each (global) tile self.tile_glob2loc = tlmap # [nloc]: global index of each local tile self.tile_loc2glob = tgmap[comm.rank] # Communication buffers self.tile_bufinfo = Bufmap(tinfo, tbufinfo, toff) self.work_bufinfo = Bufmap(winfo, wbufinfo, woff)
print("Recentering") # Center l=0 def recenter(m, shape): return np.roll(np.roll(m, -shape[-2] // 2, -2), -shape[-1] // 2, -1) ps_cross = recenter(ps_cross, ps_cross.shape) # Replace WCS, so that we can plot it with proper axes print("Updating WCS") ly, lx = enmap.laxes(ps_cross.shape, ps_cross.wcs) lbox = [[np.min(ly), np.min(lx)], [np.max(ly), np.max(lx)]] lshape = (len(ly), len(lx)) _, lwcs = enmap.geometry(pos=lbox, shape=lshape, proj="plain") ps_cross.wcs = lwcs # Project onto an equal-aspect ratio coordinate system print("Projecting") oshape, owcs = enmap.geometry(pos=lbox, shape=(np.max(lshape), np.max(lshape)), proj="plain") ospec = ps_cross.project(oshape, owcs, order=3) print("Downgrading") ospec = enmap.downgrade(ospec, args.downgrade) # Keep only the real part for now ospec = ospec.real
wo.wcs.ctype = ["RA---ZEA", "DEC--ZEA"] wo.wcs.crval = [0, 90] wo.wcs.cdelt = [res / deg2rad, res / deg2rad] wo.wcs.crpix = [1, 1] x, y = wo.wcs_world2pix(0, 90 - R / deg2rad, 1) y = int(np.ceil(y)) n = 2 * y - 1 wo.wcs.crpix = [y, y] omap = enmap.zeros((n, n), wo) # Construct our projection coordinates this is a CAR system in order # to make interpolation easy. with dprint("construct imap"): ires = np.array([1, 1. / np.sin(R)]) * res / args.supersample shape, wi = enmap.geometry(pos=[[np.pi / 2 - R, -np.pi], [np.pi / 2, np.pi]], res=ires, proj="car") imap = enmap.zeros((ncomp, ) + shape, wi) # Define SHT for interpolation pixels with dprint("construct sht"): minfo = curvedsky.map2minfo(imap) lmax_ideal = np.pi / res ps = ps[:, :, :lmax_ideal] lmax = ps.shape[-1] # We do not need all ms when centered on the pole. To reach 1e-10 relative # error up to R, we need mmax approx 9560*R in radians mmax = args.mmax or int(R * 9560) ainfo = sharp.alm_info(lmax, mmax) sht = sharp.sht(minfo, ainfo)
return omaps # Set up existing data if we are continuing cont_data = {} if args.cont_from: with open(args.cont_from, "r") as f: for line in f: toks = line.split() id = toks[0] sid = int(toks[5]) cont_data[(id, sid)] = line # Set up shifted map geometry shape, wcs = enmap.geometry(pos=np.array([[-1, -1], [1, 1]]) * args.orad * utils.arcmin, res=args.ores * utils.arcmin, proj="car") # Set up fit output f = open(args.odir + "/fit_rank_%03d.txt" % comm.rank, "w") for ind in range(comm.rank, len(args.ifiles), comm.size): ifile = args.ifiles[ind] try: sdata = read_sdata(ifile) except Exception as e: sys.stderr.write("Exception for %s: %s\n" % (ifile, str(e))) continue # Eliminate invalid data sdata = [s for s in sdata if np.any(s.div > 0)]
[args.el-args.wel/2., args.el+args.wel/2.], ]).T + det_box/utils.degree # units ibox[:,1:] *= utils.degree wibox = ibox.copy() wibox[:,:] = utils.widen_box(ibox[:,:]) srate = nsamp/wt # output box icorners = utils.box2corners(ibox) ocorners = hor2cel(icorners.T, t0) obox = utils.minmax(ocorners, -1)[:,:2] wobox = utils.widen_box(obox) # define a pixelization shape, wcs = enmap.geometry(pos=wobox[:,::-1], res=args.res*utils.arcmin, proj="cea") nphi = int(2*np.pi/(args.res*utils.arcmin)) map_orig = enmap.rand_gauss((ncomp,)+shape, wcs).astype(dtype) print "map shape %s" % str(map_orig.shape) pbox = np.array([[0,0],shape],dtype=int) # define a test tod bore = np.zeros([nsamp,3],dtype=ptype) bore[:,0] = (wt*np.linspace(0,1,nsamp,endpoint=False)) bore[:,1] = (args.az + args.waz/2*utils.triangle_wave(np.linspace(0,1,nsamp,endpoint=False)*20))*utils.degree bore[:,2] = (args.el + args.wel/2*utils.triangle_wave(np.linspace(0,1,nsamp,endpoint=False)))*utils.degree #bore = (ibox[None,0] + np.random.uniform(0,1,size=(nsamp,3))*(ibox[1]-ibox[0])[None,:]).astype(ptype) tod = np.zeros((ndet,nsamp),dtype=dtype) psi = np.arange(nsamp)*2*np.pi/100 hwp = np.zeros([nsamp,2]) hwp[:,0] = np.cos(psi)
ids = filedb.scans[args.sel] comm = mpi.COMM_WORLD dtype= np.float32 nref = args.nref min_accuracy = 1.0 # 1 pixel utils.mkdir(args.odir) src_cols = [int(w) for w in args.cols.split(":")] # Set up thumbnail geometry res = args.res*utils.arcmin pad = args.pad*utils.arcmin box = np.array([[float(w) for w in dim.split(":")] for dim in args.box.split(",")]).T*utils.arcmin box[0] -= pad box[1] += pad shape, wcs = enmap.geometry(pos=box, res=res, proj="car") area = enmap.zeros(shape, wcs, dtype) def read_srcs(fname, cols=(0,1,2)): if fname.endswith(".fits"): data = fits.open(fname)[1].data return np.array([data.ra*utils.degree,data.dec*utils.degree,data.sn]) else: data = np.loadtxt(fname, usecols=cols).T data[:2] *= utils.degree return data def find_ref_pixs(divs, rcost=1.0, dcost=1.0): """rcost is cost per pixel away from center dcost is cost per dB change in div value avay from median""" # Find median nonzero div per map
]).T + det_box / utils.degree # units ibox[:, 1:] *= utils.degree wibox = ibox.copy() wibox[:, :] = utils.widen_box(ibox[:, :]) srate = nsamp / wt # output box icorners = utils.box2corners(ibox) ocorners = hor2cel(icorners.T, t0) obox = utils.minmax(ocorners, -1)[:, :2] wobox = utils.widen_box(obox) # define a pixelization shape, wcs = enmap.geometry(pos=wobox[:, ::-1], res=args.res * utils.arcmin, proj="cea") nphi = int(2 * np.pi / (args.res * utils.arcmin)) map_orig = enmap.rand_gauss((ncomp, ) + shape, wcs).astype(dtype) print "map shape %s" % str(map_orig.shape) pbox = np.array([[0, 0], shape], dtype=int) # define a test tod bore = np.zeros([nsamp, 3], dtype=ptype) bore[:, 0] = (wt * np.linspace(0, 1, nsamp, endpoint=False)) bore[:, 1] = (args.az + args.waz / 2 * utils.triangle_wave( np.linspace(0, 1, nsamp, endpoint=False) * 20)) * utils.degree bore[:, 2] = (args.el + args.wel / 2 * utils.triangle_wave( np.linspace(0, 1, nsamp, endpoint=False))) * utils.degree #bore = (ibox[None,0] + np.random.uniform(0,1,size=(nsamp,3))*(ibox[1]-ibox[0])[None,:]).astype(ptype) tod = np.zeros((ndet, nsamp), dtype=dtype)
# massOverh = 2.e15 # concentration = 3.2 # zL = 1.0 sourceZ = 1100. overdensity = 180. critical = False atClusterZ = False # === TEMPLATE MAP === px = 0.2 arc = 100 hwidth = arc/2. hwidthTen = 5. deg = utils.degree arcmin = utils.arcmin shape, wcs = enmap.geometry(pos=[[-hwidth*arcmin,-hwidth*arcmin],[hwidth*arcmin,hwidth*arcmin]], res=px*arcmin, proj="car") shapeTen, wcsTen = enmap.geometry(pos=[[-hwidthTen*arcmin,-hwidthTen*arcmin],[hwidthTen*arcmin,hwidthTen*arcmin]], res=px*arcmin, proj="car") thetaMap = enmap.posmap(shape, wcs) thetaMap = np.sum(thetaMap**2,0)**0.5 # === KAPPA MAP === # kappaMap,r500 = NFWkappa(cc,massOverh,concentration,zL,thetaMap*180.*60./np.pi,sourceZ,overdensity=overdensity,critical=critical,atClusterZ=atClusterZ) snap = snapNum b = BattagliaSims(constDict) # === CMB POWER SPECTRUM === ps = powspec.read_spectrum("data/cl_lensinput.dat")
for i, imap in enumerate(imaps): omaps[i] = imaps[i].at(pmap+pos[i,::-1,None,None]) return omaps # Set up existing data if we are continuing cont_data = {} if args.cont_from: with open(args.cont_from,"r") as f: for line in f: toks = line.split() id = toks[0] sid = int(toks[5]) cont_data[(id,sid)] = line # Set up shifted map geometry shape, wcs = enmap.geometry( pos=np.array([[-1,-1],[1,1]])*args.orad*utils.arcmin, res=args.ores*utils.arcmin, proj="car") # Set up fit output f = open(args.odir + "/fit_rank_%03d.txt" % comm.rank, "w") for ind in range(comm.rank, len(args.ifiles), comm.size): ifile = args.ifiles[ind] try: sdata = read_sdata(ifile) except Exception as e: sys.stderr.write("Exception for %s: %s\n" % (ifile, e.message)) continue # Eliminate invalid data sdata = [s for s in sdata if np.any(s.div > 0)] if len(sdata) == 0:
from enlib import enmap, utils from alhazen.halos import NFWkappa from mpi4py import MPI import numpy as np import orphics.analysis.pipeline as pipes wdeg = 100. / 60. hwidth = wdeg * 60. deg = utils.degree arcmin = utils.arcmin px = 0.1 shape_car, wcs_car = enmap.geometry(pos=[[-hwidth * arcmin, -hwidth * arcmin], [hwidth * arcmin, hwidth * arcmin]], res=px * arcmin, proj="car") # === COSMOLOGY === cosmologyName = 'LACosmology' # from ini file iniFile = "../SZ_filter/input/params.ini" Config = SafeConfigParser() Config.optionxform = str Config.read(iniFile) lmax = 8000 cosmoDict = dictFromSection(Config, cosmologyName) constDict = dictFromSection(Config, 'constants') cc = ClusterCosmology(cosmoDict, constDict, lmax) TCMB = 2.7255e6 #=== KAPPA MAP === kappaMap, r500 = NFWkappa(cc,
# critical = False # atClusterZ = False overdensity = 500. critical = True atClusterZ = True # === TEMPLATE MAP === px = 0.1 arc = 50 hwidth = arc/2. pxDown = 0.2 deg = utils.degree arcmin = utils.arcmin shape, wcs = enmap.geometry(pos=[[-hwidth*arcmin,-hwidth*arcmin],[hwidth*arcmin,hwidth*arcmin]], res=px*arcmin, proj="car") shapeDown, wcsDown = enmap.geometry(pos=[[-hwidth*arcmin,-hwidth*arcmin],[hwidth*arcmin,hwidth*arcmin]], res=pxDown*arcmin, proj="car") thetaMap = enmap.posmap(shape, wcs) thetaMap = np.sum(thetaMap**2,0)**0.5 thetaMapDown = enmap.posmap(shapeDown, wcsDown) thetaMapDown = np.sum(thetaMapDown**2,0)**0.5 comL = cc.results.comoving_radial_distance(zL)*cc.h comS = cc.results.comoving_radial_distance(sourceZ)*cc.h winAtLens = (comS-comL)/comS kappaMap,r500 = NFWkappa(cc,massOverh,concentration,zL,thetaMap*180.*60./np.pi,winAtLens,overdensity=overdensity,critical=critical,atClusterZ=atClusterZ) # === CMB POWER SPECTRUM ===
class Axis: def __init__(self, n, scale, range): self.n, self.range, self.scale = n, range, scale self.f = np.log10 if scale == "log" else lambda x:x self.fr = self.f(range) def __call__(self, v): return np.minimum(self.n-1,np.floor((self.f(v)-self.fr[0])/(self.fr[1]-self.fr[0])*self.n).astype(np.int32)) def parse_axis(n, scale, range): return Axis(n, scale, [float(w) for w in range.split(":")]) xaxis = parse_axis(args.nx, args.sx, args.xrange) yaxis = parse_axis(args.ny, args.sy, yrange) xinds = np.arange(xaxis.n) shape, wcs = enmap.geometry(pos=np.array([yaxis.fr,xaxis.fr]).T*np.pi/180, shape=(yaxis.n,xaxis.n), proj="car") filedb.init() ids = filedb.scans[args.sel] for si in range(comm.rank, len(ids), comm.size): id = ids[si] entry = filedb.data[id] ofile = "%s/%s.fits" % (args.odir, id) if args.c and os.path.isfile(ofile): continue print "reading %s" % id try: d = actdata.read(entry) d = actdata.calibrate(d) except (IOError, errors.DataMissing) as e: print "skipping (%s)" % e.message continue
wo = wcsutils.WCS(naxis=2) wo.wcs.ctype = ["RA---ZEA","DEC--ZEA"] wo.wcs.crval = [0,90] wo.wcs.cdelt = [res/deg2rad, res/deg2rad] wo.wcs.crpix = [1,1] x, y = wo.wcs_world2pix(0,90-R/deg2rad,1) y = int(np.ceil(y)) n = 2*y-1 wo.wcs.crpix = [y,y] omap = enmap.zeros((n,n),wo) # Construct our projection coordinates this is a CAR system in order # to make interpolation easy. with dprint("construct imap"): ires = np.array([1,1./np.sin(R)])*res/args.supersample shape, wi = enmap.geometry(pos=[[np.pi/2-R,-np.pi],[np.pi/2,np.pi]], res=ires, proj="car") imap = enmap.zeros((ncomp,)+shape, wi) # Define SHT for interpolation pixels with dprint("construct sht"): minfo = curvedsky.map2minfo(imap) lmax_ideal = np.pi/res ps = ps[:,:,:lmax_ideal] lmax = ps.shape[-1] # We do not need all ms when centered on the pole. To reach 1e-10 relative # error up to R, we need mmax approx 9560*R in radians mmax = args.mmax or int(R*9560) ainfo = sharp.alm_info(lmax, mmax) sht = sharp.sht(minfo, ainfo) with dprint("curvedsky tot"):
return np.minimum( self.n - 1, np.floor((self.f(v) - self.fr[0]) / (self.fr[1] - self.fr[0]) * self.n).astype(np.int32)) def parse_axis(n, scale, range): return Axis(n, scale, [float(w) for w in range.split(":")]) xaxis = parse_axis(args.nx, args.sx, args.xrange) yaxis = parse_axis(args.ny, args.sy, yrange) xinds = np.arange(xaxis.n) shape, wcs = enmap.geometry(pos=np.array([yaxis.fr, xaxis.fr]).T * np.pi / 180, shape=(yaxis.n, xaxis.n), proj="car") filedb.init() ids = filedb.scans[args.sel] for si in range(comm.rank, len(ids), comm.size): id = ids[si] entry = filedb.data[id] ofile = "%s/%s.fits" % (args.odir, id) if args.c and os.path.isfile(ofile): continue print "reading %s" % id try: d = actdata.read(entry) d = actdata.calibrate(d) except (IOError, errors.DataMissing) as e: print "skipping (%s)" % e.message
filename = lambda x,ext="fits",k=task: args.SimRoot+"_"+x+"_"+str(k).zfill(4)+args.save+"."+ext try: if args.flat and args.flat_force: raise if not(args.save_meanfield is None): raise cpatch = enmap.read_fits(filename("lensed"),box=box if not(args.flat) else None,wcs_override=fwcs if not(args.flat) else None) kpatch = enmap.read_fits(filename("kappa"),box=box if not(args.flat) else None,wcs_override=fwcs if not(args.flat) else None) if i==0: shape,wcs = cpatch.shape,cpatch.wcs except: assert args.flat or not(args.save_meanfield is None), "No sims found in directory specified. I can only make lensed sims if they are flat-sky." if not(inited): if not(args.save_meanfield is None) and not(args.flat): template = enmap.read_fits(filename("lensed","fits",0),box=box if not(args.flat) else None,wcs_override=fwcs if not(args.flat) else None) shape,wcs = template.shape,template.wcs else: shape,wcs = enmap.geometry(pos=box,res=args.res*np.pi/180./60., proj="car") if pol: shape = (3,) + shape mgen, kgen = init_flat(shape[-2:],wcs) inited = True unlensed = mgen.get_map(iau=args.iau) if not(args.save_meanfield is None): cpatch = unlensed else: kpatch = kgen.get_map() cpatch = lens(unlensed,kpatch) # enmap.write_fits(filename("lensed"),cpatch) # enmap.write_fits(filename("unlensed"),unlensed) enmap.write_fits(filename("kappa"),kpatch) if i==0:
parser.add_argument("--signal",type=str, default="ptsrc:100:1e3:-3") parser.add_argument("--noise", type=str, default="1/f:20:2:0.5") parser.add_argument("--seed", type=int, default=1) parser.add_argument("--measure", type=float, default=None) parser.add_argument("--real", type=str, default=None) args = parser.parse_args() log_level = log.verbosity2level(config.get("verbosity")) L = log.init(level=log_level) utils.mkdir(args.odir) if args.area: area = enmap.read_map(args.area) if area.ndim == 2: area = area[None] else: shape, wcs = enmap.geometry(pos=np.array([[-1,-1],[1,1]])*np.pi/180, shape=(600,600), pre=(3,), proj="car", ref=[0,0]) area = enmap.zeros(shape, wcs) def get_scans(area, signal, bore, dets, noise, seed=0, real=None, noise_override=None): scans = [] # Get real scan information if necessary L.debug("real") if real: real_scans = [] filedb.init() db = filedb.data ids = fileb.scans[real].ids for id in ids: try: real_scans.append(actscan.ACTScan(db[id])) except errors.DataMissing as e:
# This program computes a simple estimation of the amount of distortion the # flat-sky approximation involves import numpy as np, argparse from enlib import enmap parser = argparse.ArgumentParser() parser.add_argument("omap") parser.add_argument("-D", "--diameter", type=float, default=30) parser.add_argument("-n", "--npix", type=int, default=800) parser.add_argument("--proj", type=str, default="cea") args = parser.parse_args() r = args.diameter * np.pi / 180 / 2 shape, wcs = enmap.geometry(pos=[[-r, -r], [r, r]], shape=(args.npix, args.npix), proj=args.proj) def linpos(n, b): return b[0] + (np.arange(n) + 0.5) * (b[1] - b[0]) / n alpha = enmap.zeros((2, ) + shape, wcs) pos = enmap.posmap(shape, wcs) # I'm not sure how to compute this in general, so here's a specialization # for cylindrical projections if args.proj == "cea" or args.proj == "car": dec = pos[0, :, 0] ra = pos[1, 0, :] lindec = linpos(shape[0], enmap.box(shape, wcs)[:, 0]) scale = 1 / np.cos(dec) - 1 alpha[0] = (lindec - dec)[:, None]
box = utils.minmax(off,0) dets.append(det) offs.append(off) boxes.append(box) imaps.append(imap) box = utils.bounding_box(boxes) box = utils.widen_box(box, rad*5, relative=False) # We assume that the two maps have the same pixelization imaps = enmap.samewcs(np.array(imaps), imaps[0]) # Downsample by averaging imaps = enmap.downgrade(imaps, (1,args.step)) naz = imaps.shape[-1] # Ok, build our output geometry shape, wcs = enmap.geometry(pos=box, res=args.res*utils.arcmin, proj="car", pre=(naz,)) omap = enmap.zeros(shape, wcs, dtype=dtype) # Normalization norm = enmap.zeros(shape[-2:],wcs) norm[0,0] = 1 norm = enmap.smooth_gauss(norm, rad)[0,0] # Loop through slices and populate bazs = [] for iaz in range(naz): # Get our boresight az bazs.append(imaps.pix2sky([0,iaz])[1]) vals = [] for i in range(nfile): # Go from detectors to y-pixel in input maps
np.abs(d.boresight[1, 1:] - d.boresight[1, :-1])[::10]) * d.srate tref = d.boresight[0, d.nsamp / 2] # Build a small, high-res map around each source sdata = [] with bench.mark("scan"): scan = actscan.ACTScan(entry, d=d) pcut = pmat.PmatCut(scan) junk = np.zeros(pcut.njunk, dtype) pcut.backward(tod, junk) #wtod = apply_bivar(tod*0+1,bivar,bsize_ivar,inplace=True) wtod = tod * 0 + ivar[:, None] pcut.backward(wtod, junk) for sid in sids: shape, wcs = enmap.geometry( pos=[srcpos[::-1, sid] - R, srcpos[::-1, sid] + R], res=res, proj="car") area = enmap.zeros(shape, wcs, dtype) with bench.mark("pmap"): pmap = pmat.PmatMap(scan, area) rhs = enmap.zeros((3, ) + shape, wcs, dtype) div = rhs * 0 with bench.mark("rhs"): pmap.backward(tod, rhs) with bench.mark("div"): pmap.backward(wtod, div) div = div[0] map = rhs.copy() map[:, div > 0] /= div[div > 0] map = map[0] # Crop the outermost pixel, where outside hits will have accumulated