lmhptransformation.py 3.34 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
Theo Steininger's avatar
Theo Steininger committed
13 14 15 16 17
#
# Copyright(C) 2013-2017 Max-Planck-Society
#
# NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik
# and financially supported by the Studienstiftung des deutschen Volkes.
18

Jait Dixit's avatar
Jait Dixit committed
19
import numpy as np
Martin Reinecke's avatar
Martin Reinecke committed
20
from .... import HPSpace, LMSpace
Martin Reinecke's avatar
Martin Reinecke committed
21 22
from .slicing_transformation import SlicingTransformation
from . import lm_transformation_helper
23

Martin Reinecke's avatar
Martin Reinecke committed
24
import pyHealpix
Jait Dixit's avatar
Jait Dixit committed
25 26


27 28 29 30
class LMHPTransformation(SlicingTransformation):

    # ---Overwritten properties and methods---

Martin Reinecke's avatar
Martin Reinecke committed
31 32
    def __init__(self, domain, codomain=None):
        super(LMHPTransformation, self).__init__(domain, codomain)
Jait Dixit's avatar
Jait Dixit committed
33

34 35
    # ---Mandatory properties and methods---

36 37 38 39
    @property
    def unitary(self):
        return False

40 41
    @classmethod
    def get_codomain(cls, domain):
Jait Dixit's avatar
Jait Dixit committed
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
        """
            Generates a compatible codomain to which transformations are
            reasonable, i.e.\  a pixelization of the two-sphere.

            Parameters
            ----------
            domain : LMSpace
                Space for which a codomain is to be generated

            Returns
            -------
            codomain : HPSpace
                A compatible codomain.

            References
            ----------
            .. [#] K.M. Gorski et al., 2005, "HEALPix: A Framework for
                   High-Resolution Discretization and Fast Analysis of Data
                   Distributed on the Sphere", *ApJ* 622..759G.
        """
        if not isinstance(domain, LMSpace):
63
            raise TypeError("domain needs to be a LMSpace.")
Jait Dixit's avatar
Jait Dixit committed
64

Theo Steininger's avatar
Theo Steininger committed
65
        nside = max((domain.lmax + 1)//2, 1)
Martin Reinecke's avatar
Martin Reinecke committed
66
        result = HPSpace(nside=nside)
67
        return result
Jait Dixit's avatar
Jait Dixit committed
68

69 70
    @classmethod
    def check_codomain(cls, domain, codomain):
Jait Dixit's avatar
Jait Dixit committed
71
        if not isinstance(domain, LMSpace):
72
            raise TypeError("domain is not a LMSpace.")
Jait Dixit's avatar
Jait Dixit committed
73 74

        if not isinstance(codomain, HPSpace):
75
            raise TypeError("codomain must be a HPSpace.")
76

77 78
        nside = codomain.nside
        lmax = domain.lmax
Jait Dixit's avatar
Jait Dixit committed
79

80
        super(LMHPTransformation, cls).check_codomain(domain, codomain)
Jait Dixit's avatar
Jait Dixit committed
81

82 83 84
    def _transformation_of_slice(self, inp, **kwargs):
        nside = self.codomain.nside
        lmax = self.domain.lmax
85
        mmax = lmax
Jait Dixit's avatar
Jait Dixit committed
86

87
        if issubclass(inp.dtype.type, np.complexfloating):
88
            [resultReal,
89
             resultImag] = [lm_transformation_helper.buildLm(x, lmax=lmax)
90
                            for x in (inp.real, inp.imag)]
91

Theo Steininger's avatar
Theo Steininger committed
92
            [resultReal, resultImag] = [pyHealpix.alm2map(x, lmax, mmax, nside)
93 94 95
                                        for x in [resultReal, resultImag]]

            result = self._combine_complex_result(resultReal, resultImag)
Jait Dixit's avatar
Jait Dixit committed
96 97

        else:
98
            result = lm_transformation_helper.buildLm(inp, lmax=lmax)
Theo Steininger's avatar
Theo Steininger committed
99
            result = pyHealpix.alm2map(result, lmax, mmax, nside)
Jait Dixit's avatar
Jait Dixit committed
100

101
        return result