def prepare_map_field(map, args, crange=None, printer=noprint): if crange is None: with printer.time("ranges", 3): crange = get_color_range(map, args) if map.ndim == 2: map = map[None] if args.autocrop_each: map = enmap.autocrop(map) with printer.time("colorize", 3): color = map_to_color(map, crange, args) return map, color
L.info("Reading %s" % args.map) map = enmap.read_map(args.map) map = map.reshape((-1, ) + map.shape[-2:])[0][None] L.info("Reading %s" % args.noise) inoise = enmap.read_map(args.noise) inoise = inoise.reshape((-1, ) + inoise.shape[-2:])[0][None, None] print map.shape, inoise.shape shape_orig = map.shape[-2:] # Mask out too noisy areas L.info("Masking") mask = inoise[0, 0] < args.noise_max**-2 map[:, mask] = np.nan if not args.nocrop: L.info("Cropping") map, info = enmap.autocrop(map, method="fft", return_info=True) inoise = enmap.padcrop(inoise, info) shape_crop = map.shape[-2:] L.info("Cropped from %s to %s" % (str(shape_orig), str(shape_crop))) map[np.isnan(map)] = 0 ## Truncate to fft-friendly areas #h = fft.fft_len(map.shape[-2],[2,3,5]) #w = fft.fft_len(map.shape[-1],[2,3,5]) #map = map[:,:h,:w] #inoise = inoise[:,:,:h,:w] #map[~np.isfinite(map)] = 0 # Kill extreme values L.info("Removing extreme outliers above 1e6") map = np.minimum(1e6, np.maximum(-1e6, map))
def get_map(ifile, args, return_info=False): """Read the specified map, and massage it according to the options in args. Relevant ones are sub, autocrop, slice, op, downgrade, scale, mask. Retuns with shape [:,ny,nx], where any extra dimensions have been flattened into a single one.""" with warnings.catch_warnings(): warnings.filterwarnings("ignore") toks = ifile.split(":") ifile, slice = toks[0], ":".join(toks[1:]) m0 = enmap.read_map(ifile) if args.fix_wcs: m0.wcs = enwcs.fix_wcs(m0.wcs) # Save the original map, so we can compare its wcs later m = m0 # Submap slicing currently has wrapping issues if args.sub is not None: default = [[-90,-180],[90,180]] sub = np.array([[(default[j][i] if q == '' else float(q))*np.pi/180 for j,q in enumerate(w.split(":"))]for i,w in enumerate(args.sub.split(","))]).T m = m.submap(sub) # Perform a common autocrop across all fields if args.autocrop: m = enmap.autocrop(m) # If necessary, split into stamps. If no stamp splitting occurs, # a list containing only the original map is returned mlist = extract_stamps(m, args) # The stamp stuff is a bit of an ugly hack. This loop and wcslist # are parts of that hack. for i, m in enumerate(mlist): # Downgrade downgrade = [int(w) for w in args.downgrade.split(",")] m = enmap.downgrade(m, downgrade) # Slicing, either at the file name level or though the slice option m = eval("m"+slice) if args.slice is not None: m = eval("m"+args.slice) flip = (m.wcs.wcs.cdelt*m0.wcs.wcs.cdelt)[::-1]<0 assert m.ndim >= 2, "Image must have at least 2 dimensions" # Apply arbitrary map operations m1 = m if args.op is not None: m = eval(args.op, {"m":m},np.__dict__) # Scale if requested scale = [int(w) for w in args.scale.split(",")] if np.any(np.array(scale)>1): m = enmap.upgrade(m, scale) # Flip such that pixels are in PIL or matplotlib convention, # such that RA increases towards the left and dec upwards in # the final image. Unless a slicing operation on the image # overrrode this. if m.wcs.wcs.cdelt[1] > 0: m = m[...,::-1,:] if m.wcs.wcs.cdelt[0] > 0: m = m[...,:,::-1] if flip[0]: m = m[...,::-1,:] if flip[1]: m = m[...,:,::-1] # Update stamp list mlist[i] = m wcslist = [m.wcs for m in mlist] m = enmap.samewcs(np.asarray(mlist),mlist[0]) if args.stamps is None: m, wcslist = m[0], None # Flatten pre-dimensions mf = m.reshape((-1,)+m.shape[-2:]) # Stack if args.tile is not None: toks = [int(i) for i in args.tile.split(",")] nrow = toks[0] if len(toks) > 0 else -1 ncol = toks[1] if len(toks) > 1 else -1 mf = hwstack(hwexpand(mf, nrow, ncol, args.tile_transpose))[None] # Mask bad data if args.mask is not None: if not np.isfinite(args.mask): mf[np.abs(mf)==args.mask] = np.nan else: mf[np.abs(mf-args.mask)<=args.mask_tol] = np.nan # Done if not return_info: return mf else: info = bunch.Bunch(fname=ifile, ishape=m.shape, wcslist=wcslist) return mf, info