コード例 #1
0
ファイル: maps.py プロジェクト: FairSky/pycmb
    def to_pixel(self, Nside=None, Npix=None, dtype=None, out=None):
        """
        ::
        
            >>> insh = harmonic_sphere_map(0, 0, 4)
            >>> insh.to_pixel(8)
            Pixel sphere map (ring-ordered, Nside=8, Npix=768)

        Convert a uniform map::        
            >>> shmap = harmonic_sphere_map([[23], [0, 0], [0, 0, 0]])
            >>> pixmap = shmap.to_pixel(8); pixmap
            Pixel sphere map (ring-ordered, Nside=8, Npix=768)
            >>> np.allclose(pixmap, 23.0 / np.sqrt(4*np.pi))
            True

        One can convert multiple maps at once::

            >>> M = shmap
            >>> M = np.hstack((M[:,None], M[:,None], M[:,None], M[:,None]))
            >>> M = np.dstack((M, M))
            >>> M = harmonic_sphere_map(M, 0, 2); M
            (4, 2)-stack of brief complex harmonic sphere maps with l=0..2 (6 coefficients)
            >>> x = M.to_pixel(4); x
            (4, 2)-stack of pixel sphere maps (ring-ordered, Nside=4, Npix=192)
            >>> np.allclose(x, 23.0 / np.sqrt(4*np.pi))
            True

        """
        self._check_not_pretending()
        if self.format is not COMPLEX_BRIEF:
            return self.to_complex().to_pixel(Nside, Npix, dtype, out)
        if Nside is None:
            if Npix is None: raise ValueError("Provide Nside or Npix")
            Nside = npix2nside(Npix)
        if Npix is None:
            Npix = nside2npix(Nside)
        if dtype is None:
            dtype = self.dtype
        dtype = to_real_dtype(dtype)
        # for now, only double...
        assert dtype == np.float64
        
        size = self.shape[1:]
        if out is None:
            out = np.empty((Npix,) + size, dtype, order='F')

        # Todo: Optimize -- have lmin in complexpacked2complexmatrix
        if self.lmin == 0:
            source = self
        else:
            source = np.empty((l_to_lm(self.lmax + 1),) + size, complex_dtype, order='F')
            source[:l_to_lm(self.lmin)] = 0
            source[l_to_lm(self.lmin):] = self

        for mapidx in np.ndindex(*size):
            idx = (slice(None),) + mapidx
            alm_matrix = mapdatautils.alm_complexpacked2complexmatrix(source[idx])
            mapdatautils.alm2map(Nside, alm_matrix, out=out[idx])

        return _PixelSphereMap(out, pixel_order='ring')
コード例 #2
0
ファイル: maps.py プロジェクト: FairSky/pycmb
    def rotate(self, psi, theta, phi):
        """
        Returns a rotated copy of self. O(lmax^3).

        ::
        
            >>> M = harmonic_sphere_map([[1, 0], [1, 0, 0]], lmin=1)
            >>> R = M.rotate(0, np.pi, 0); R
            Brief complex harmonic sphere map with l=1..2 (5 coefficients)
            >>> np.round(R.to_array(), 4)
            array([-1.+0.j, -0.+0.j,  1.+0.j,  0.+0.j,  0.+0.j])

        One can convert multiple maps at once::

        TODO write tests

        """
        from healpix import rotate_alm_d
        self._check_not_pretending()

        if psi == 0 and theta == 0 and phi == 0:
            return self.copy()
        
        if self.format is not COMPLEX_BRIEF:
            x = self.to_complex().rotate(psi, theta, phi)
            if self.format is REAL_FULL:
                return x.to_real()
            else:
                assert False

        size = self.shape[1:]

        # Todo: Optimize -- have lmin in complexpacked2complexmatrix
        out = np.empty((l_to_lm(self.lmax + 1),) + size, complex_dtype, order='F')
        out[:l_to_lm(self.lmin), ...] = 0
        out[l_to_lm(self.lmin):, ...] = self

        alm_matrix = np.empty((1, self.lmax + 1, self.lmax + 1), complex_dtype, order='F')

        for mapidx in np.ndindex(*size):
            idx = (slice(None),) + mapidx
            mapdatautils.alm_complexpacked2complexmatrix(out[idx], alm_matrix[0,:,:])
            rotate_alm_d(self.lmax, alm_matrix, psi, theta, phi)
            mapdatautils.alm_complexmatrix2complexpacked(alm_matrix[0,:,:], out[idx])
        out = out[l_to_lm(self.lmin):, ...]
        return _HarmonicSphereMap(out, self.lmin, self.lmax, COMPLEX_BRIEF)