lmhptransformation.py 3.64 KB
Newer Older
Jait Dixit's avatar
Jait Dixit committed
1
import numpy as np
2
from nifty.config import dependency_injector as gdi
Jait Dixit's avatar
Jait Dixit committed
3
from nifty import HPSpace, LMSpace
4
from slicing_transformation import SlicingTransformation
5
6
import lm_transformation_factory as ltf

Jait Dixit's avatar
Jait Dixit committed
7
8
9
hp = gdi.get('healpy')


10
11
12
13
class LMHPTransformation(SlicingTransformation):

    # ---Overwritten properties and methods---

Jait Dixit's avatar
Jait Dixit committed
14
    def __init__(self, domain, codomain=None, module=None):
Jait Dixit's avatar
Jait Dixit committed
15
        if gdi.get('healpy') is None:
16
17
            raise ImportError(
                "The module libsharp is needed but not available.")
Jait Dixit's avatar
Jait Dixit committed
18

19
20
        super(LMHPTransformation, self).__init__(domain, codomain,
                                                 module=module)
Jait Dixit's avatar
Jait Dixit committed
21

22
23
24
25
    # ---Mandatory properties and methods---

    @classmethod
    def get_codomain(cls, domain):
Jait Dixit's avatar
Jait Dixit committed
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
        """
            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):
47
48
            raise TypeError(
                'ERROR: domain needs to be a LMSpace')
Jait Dixit's avatar
Jait Dixit committed
49

50
        nside = (domain.lmax + 1) // 3
51
52
53
        result = HPSpace(nside=nside)
        cls.check_codomain(domain, result)
        return result
Jait Dixit's avatar
Jait Dixit committed
54

Jait Dixit's avatar
Jait Dixit committed
55
56
57
    @staticmethod
    def check_codomain(domain, codomain):
        if not isinstance(domain, LMSpace):
58
59
            raise TypeError(
                'ERROR: domain is not a LMSpace')
Jait Dixit's avatar
Jait Dixit committed
60
61

        if not isinstance(codomain, HPSpace):
62
63
            raise TypeError(
                'ERROR: codomain must be a HPSpace.')
64

65
66
        nside = codomain.nside
        lmax = domain.lmax
Jait Dixit's avatar
Jait Dixit committed
67

68
        if 3*nside - 1 != lmax:
69
70
            raise ValueError(
                'ERROR: codomain has 3*nside -1 != lmax.')
Jait Dixit's avatar
Jait Dixit committed
71

72
        return None
Jait Dixit's avatar
Jait Dixit committed
73

74
75
76
    def _transformation_of_slice(self, inp, **kwargs):
        nside = self.codomain.nside
        lmax = self.domain.lmax
77
        mmax = lmax
Jait Dixit's avatar
Jait Dixit committed
78

79
80
81
82
        if issubclass(inp.dtype.type, np.complexfloating):
            [resultReal, resultImag] = [ltf.buildLm(x, lmax=lmax)
                                        for x in (inp.real, inp.imag)]

83
            [resultReal, resultImag] = [hp.alm2map(x.astype(np.complex128,
84
85
86
87
88
89
90
91
92
93
94
95
96
                                                            copy=False),
                                                   nside,
                                                   lmax=lmax,
                                                   mmax=mmax,
                                                   pixwin=False,
                                                   fwhm=0.0,
                                                   sigma=None,
                                                   pol=True,
                                                   inplace=False,
                                                   **kwargs)
                                        for x in [resultReal, resultImag]]

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

        else:
99
100
101
102
            result = ltf.buildLm(inp, lmax=lmax)
            result = hp.alm2map(result.astype(np.complex128, copy=False),
                                nside, lmax=lmax, mmax=mmax, pixwin=False,
                                fwhm=0.0, sigma=None, pol=True, inplace=False)
Jait Dixit's avatar
Jait Dixit committed
103

104
        return result