def get_subpixels(idx, nside_superpix, nside_subpix, nest=True): """Compute the indices of subpixels contained within superpixels. This function returns an output array with one additional dimension of size N for subpixel indices where N is the maximum number of subpixels for any pair of ``nside_superpix`` and ``nside_subpix``. If the number of subpixels is less than N the remaining subpixel indices will be set to -1. Parameters ---------- idx : `~numpy.ndarray` Array of HEALPix pixel indices for superpixels of NSIDE ``nside_superpix``. nside_superpix : int or `~numpy.ndarray` NSIDE of superpixel. nside_subpix : int or `~numpy.ndarray` NSIDE of subpixel. nest : bool If True, assume NESTED pixel ordering, otherwise, RING pixel ordering. Returns ------- idx_sub : `~numpy.ndarray` Indices of HEALpix pixels of nside ``nside_subpix`` contained within pixel indices ``idx`` of nside ``nside_superpix``. """ import healpy as hp if not nest: idx = hp.ring2nest(nside_superpix, idx) idx = np.asarray(idx) nside_superpix = np.asarray(nside_superpix) nside_subpix = np.asarray(nside_subpix) if np.any(~is_power2(nside_superpix)) or np.any(~is_power2(nside_subpix)): raise ValueError("NSIDE must be a power of 2.") # number of subpixels in each superpixel npix = np.array((nside_subpix // nside_superpix)**2, ndmin=1) x = np.arange(np.max(npix), dtype=int) idx = idx * npix if not np.all(npix[0] == npix): x = np.broadcast_to(x, idx.shape + x.shape) idx = idx[..., None] + x idx[x >= np.broadcast_to(npix[..., None], x.shape)] = INVALID_INDEX.int else: idx = idx[..., None] + x if not nest: m = idx == INVALID_INDEX.int idx[m] = 0 idx = hp.nest2ring(nside_subpix[..., None], idx) idx[m] = INVALID_INDEX.int return idx
def downsample(self, factor): if not is_power2(factor): raise ValueError("Downsample factor must be a power of 2.") if self.is_allsky: return self.__class__( self.nside // factor, self.nest, frame=self.frame, region=self.region, axes=copy.deepcopy(self.axes), ) idx = list(self.get_idx(flat=True)) nside = self._get_nside(idx) idx_new = get_superpixels(idx[0], nside, nside // factor, nest=self.nest) idx[0] = idx_new return self.__class__( self.nside // factor, self.nest, frame=self.frame, region=tuple(idx), axes=copy.deepcopy(self.axes), )
def upsample(self, factor): if not is_power2(factor): raise ValueError("Upsample factor must be a power of 2.") if self.is_allsky: return self.__class__( self.nside * factor, self.nest, frame=self.frame, region=self.region, axes=copy.deepcopy(self.axes), ) idx = list(self.get_idx(flat=True)) nside = self._get_nside(idx) idx_new = get_subpixels(idx[0], nside, nside * factor, nest=self.nest) for i in range(1, len(idx)): idx[i] = idx[i][..., None] * np.ones(idx_new.shape, dtype=int) idx[0] = idx_new return self.__class__( self.nside * factor, self.nest, frame=self.frame, region=tuple(idx), axes=copy.deepcopy(self.axes), )
def get_superpixels(idx, nside_subpix, nside_superpix, nest=True): """Compute the indices of superpixels that contain a subpixel. Parameters ---------- idx : `~numpy.ndarray` Array of HEALPix pixel indices for subpixels of NSIDE ``nside_subpix``. nside_subpix : int or `~numpy.ndarray` NSIDE of subpixel. nside_superpix : int or `~numpy.ndarray` NSIDE of superpixel. nest : bool If True, assume NESTED pixel ordering, otherwise, RING pixel ordering. Returns ------- idx_super : `~numpy.ndarray` Indices of HEALpix pixels of nside ``nside_superpix`` that contain pixel indices ``idx`` of nside ``nside_subpix``. """ import healpy as hp idx = np.array(idx) nside_superpix = np.asarray(nside_superpix) nside_subpix = np.asarray(nside_subpix) if not nest: idx = hp.ring2nest(nside_subpix, idx) if np.any(~is_power2(nside_superpix)) or np.any(~is_power2(nside_subpix)): raise ValueError("NSIDE must be a power of 2.") ratio = np.array((nside_subpix // nside_superpix)**2, ndmin=1) idx //= ratio if not nest: m = idx == INVALID_INDEX.int idx[m] = 0 idx = hp.nest2ring(nside_superpix, idx) idx[m] = INVALID_INDEX.int return idx
def nside_to_order(nside): """Compute the ORDER given NSIDE. Returns -1 for NSIDE values that are not a power of 2. """ nside = np.array(nside, ndmin=1) order = -1 * np.ones_like(nside) m = is_power2(nside) order[m] = np.log2(nside[m]).astype(int) return order