def test_plain_wcs(self): # Test area and box for a small Cartesian geometry shape,wcs = enmap.geometry(res=np.deg2rad(1./60.),shape=(600,600),pos=(0,0),proj='plain') box = np.rad2deg(enmap.box(shape,wcs)) area = np.rad2deg(np.rad2deg(enmap.area(shape,wcs))) assert np.all(np.isclose(box,np.array([[-5,-5],[5,5]]))) assert np.isclose(area,100.) # and for an artifical Cartesian geometry with area>4pi shape,wcs = enmap.geometry(res=np.deg2rad(10),shape=(100,100),pos=(0,0),proj='plain') box = np.rad2deg(enmap.box(shape,wcs)) area = np.rad2deg(np.rad2deg(enmap.area(shape,wcs))) assert np.all(np.isclose(box,np.array([[-500,-500],[500,500]]))) assert np.isclose(area,1000000)
def test_scale(self): # Test (with a plain geometry) that scale_geometry # will result in geometries with the same bounding box # but different area pixel pres = 0.5 ufact = 2 dfact = 0.5 shape,wcs = enmap.geometry(pos=(0,0),shape=(30,30),res=pres*u.arcmin,proj='plain') ushape,uwcs = enmap.scale_geometry(shape,wcs,ufact) dshape,dwcs = enmap.scale_geometry(shape,wcs,dfact) box = enmap.box(shape,wcs) ubox = enmap.box(ushape,uwcs) dbox = enmap.box(dshape,dwcs) parea = enmap.pixsize(shape,wcs) uparea = enmap.pixsize(ushape,uwcs) dparea = enmap.pixsize(dshape,dwcs) assert np.all(np.isclose(box,ubox)) assert np.all(np.isclose(box,dbox)) assert np.isclose(parea/(ufact**2),uparea) assert np.isclose(parea/(dfact**2),dparea)
def random_catalog(shape, wcs, N, edge_avoid_deg=0.): box = enmap.box(shape, wcs) dec0 = min(box[0, 0], box[1, 0]) + edge_avoid_deg * np.pi / 180. dec1 = max(box[0, 0], box[1, 0]) - edge_avoid_deg * np.pi / 180. ra0 = min(box[0, 1], box[1, 1]) + edge_avoid_deg * np.pi / 180. ra1 = max(box[0, 1], box[1, 1]) - edge_avoid_deg * np.pi / 180. ras = np.random.uniform(ra0, ra1, N) * 180. / np.pi decs = np.random.uniform(dec0, dec1, N) * 180. / np.pi return ras, decs
def create_map(width_deg = 20., px_res_arcmin = 0.5, Ngals = 10000000): shape,wcs = omaps.rect_geometry(width_deg = width_deg, px_res_arcmin = px_res_arcmin) bounds = enmap.box(shape,wcs)*180./np.pi Ngals = Ngals ras = np.random.uniform(bounds[0,1], bounds[1,1], Ngals) decs = np.random.uniform(bounds[0,0], bounds[1,0], Ngals) cmapper = cats.CatMapper(ras, decs, shape, wcs) delta = cmapper.counts/cmapper.counts.mean()-1. modlmap = cmapper.counts.modlmap() #result = {'shape': shape, 'wcs': wcs, 'delta': delta} #PIPE result = OrderedDict([('shape', shape), ('wcs', wcs), ('delta', delta), ('modlmap', modlmap)]) return result
def get_slice(shape_small, wcs_small, shape_large, wcs_large): ''' Return slice of bigger map corresponding to smaller map. If bigger map includes I, Q, U, etc. dimensions, only the first one is selected. shape_small : tuple Shape of small map. wcs_small : astropy.wcs.wcs.WCS object, optional WCS of small map. shape_large : tuple Shape of large map. wcs_large : astropy.wcs.wcs.WCS object, optional WCS of large map. returns ------- slice : slice object Slice into bigger map. Raises ------ ValueError If WCSs of maps are incompatible. If small map is not 2d. If large map is smaller than small map. ''' if not wcsutils.is_compatible(wcs_small, wcs_large): raise ValueError('Incompatible WCSs.') if len(shape_small) != 2: raise ValueError('Small map needs to be 2d.') if shape_large[-1] < shape_small[-1] or shape_large[-2] < shape_small[-2]: raise ValueError('Large map is smaller than small map.') if shape_small == shape_large: return Ellipsis elif shape_small == shape_large[-2::]: return (0, ) * (len(shape_large) - 2) + np.s_[:, :] else: skybox = enmap.box(shape_small, wcs_small) pixbox = enmap.skybox2pixbox(shape_large, wcs_large, skybox, corner=True) pixbox = pixbox.astype(int) return (0, ) * (len(shape_large) - 2) + np.s_[pixbox[0, 0]:pixbox[ 1, 0], pixbox[0, 1]:pixbox[1, 1]]
def sim_srcs(shape, wcs, srcs, beam, omap=None, dtype=None, nsigma=5, rmax=None, smul=1, return_padded=False, pixwin=False, op=np.add, wrap="auto", verbose=False, cache=None): """Simulate a point source map in the geometry given by shape, wcs for the given srcs[nsrc,{dec,ra,T...}], using the beam[{r,val},npoint], which must be equispaced. If omap is specified, the sources will be added to it in place. All angles are in radians. The beam is only evaluated up to the point where it reaches exp(-0.5*nsigma**2) unless rmax is specified, in which case this gives the maximum radius. smul gives a factor to multiply the resulting source model by. This is mostly useful in conction with omap. The source simulation is sped up by using a source lookup grid. """ if omap is None: omap = enmap.zeros(shape, wcs, dtype) ishape = omap.shape omap = omap.preflat ncomp = omap.shape[0] # Set up wrapping if wrap is "auto": wrap = [0, utils.nint(360. / wcs.wcs.cdelt[0])] # In keeping with the rest of the functions here, srcs is [nsrc,{dec,ra,T,Q,U}]. # The beam parameters are ignored - the beam argument is used instead amps = srcs[:, 2:2 + ncomp] poss = srcs[:, :2].copy() # Rewind positions to let us use flat-sky approximation for distance calculations ref = np.mean(enmap.box(shape, wcs, corner=False)[:, 1]) poss[:, 1] = utils.rewind(poss[:, 1], ref) beam = expand_beam(beam, nsigma, rmax) rmax = nsigma2rmax(beam, nsigma) # Pad our map by rmax, so we get the contribution from sources # just ourside our area. We will later split our map into cells of size cres. Let's # adjust the padding so we have a whole number of cells minshape = np.min(omap[..., 5:-5:10, 5:-5:10].pixshapemap() / 10, (-2, -1)) cres = np.maximum(1, utils.nint(rmax / minshape)) epix = cres - (omap.shape[-2:] + 2 * cres) % cres padding = [cres, cres + epix] wmap, wslice = enmap.pad(omap, padding, return_slice=True) # Overall we will have this many grid cells cshape = wmap.shape[-2:] / cres # Find out which sources matter for which cells srcpix = wmap.sky2pix(poss.T).T pixbox = np.array([[0, 0], wmap.shape[-2:]], int) nhit, cell_srcs = build_src_cells(pixbox, srcpix, cres, wrap=wrap) # Optionally cache the posmap if cache is None or cache[0] is None: posmap = wmap.posmap() else: posmap = cache[0] if cache is not None: cache[0] = posmap model = eval_srcs_loop(posmap, poss, amps, beam, cres, nhit, cell_srcs, dtype=wmap.dtype, op=op, verbose=verbose) del posmap if pixwin: model = enmap.apply_window(model) # Update our work map, through our view if smul != 1: model *= smul wmap = op(wmap, model, wmap) if not return_padded: # Copy out omap[:] = wmap[wslice] # Restore shape omap = omap.reshape(ishape) return omap else: return wmap.reshape(ishape[:-2] + wmap.shape[-2:]), wslice