gllmtransformation.py 3.84 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# NIFTy
# Copyright (C) 2017  Theo Steininger
#
# Author: Theo Steininger
#
# 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/>.

19
import numpy as np
20
21

from nifty.config import dependency_injector as gdi
Jait Dixit's avatar
Jait Dixit committed
22
from nifty import GLSpace, LMSpace
23
from slicing_transformation import SlicingTransformation
24
import lm_transformation_helper
25

26
pyHealpix = gdi.get('pyHealpix')
27

28

29
30
31
32
class GLLMTransformation(SlicingTransformation):

    # ---Overwritten properties and methods---

Jait Dixit's avatar
Jait Dixit committed
33
    def __init__(self, domain, codomain=None, module=None):
34
35
36
37
38
39
        if module is None:
            module = 'pyHealpix'

        if module != 'pyHealpix':
            raise ValueError("Unsupported SHT module.")

40
        if 'pyHealpix' not in gdi:
41
            raise ImportError(
42
                "The module pyHealpix is needed but not available.")
43

44
        super(GLLMTransformation, self).__init__(domain, codomain, module)
45
46

    # ---Mandatory properties and methods---
47

48
49
50
51
    @property
    def unitary(self):
        return False

52
53
    @classmethod
    def get_codomain(cls, domain):
Jait Dixit's avatar
Jait Dixit committed
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
        """
            Generates a compatible codomain to which transformations are
            reasonable, i.e.\  an instance of the :py:class:`lm_space` class.

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

            Returns
            -------
            codomain : LMSpace
                A compatible codomain.
        """

        if not isinstance(domain, GLSpace):
70
            raise TypeError("domain needs to be a GLSpace")
Jait Dixit's avatar
Jait Dixit committed
71

72
        nlat = domain.nlat
Jait Dixit's avatar
Jait Dixit committed
73
        lmax = nlat - 1
theos's avatar
theos committed
74

75
        result = LMSpace(lmax=lmax)
76
        return result
Jait Dixit's avatar
Jait Dixit committed
77

78
79
    @classmethod
    def check_codomain(cls, domain, codomain):
Jait Dixit's avatar
Jait Dixit committed
80
        if not isinstance(domain, GLSpace):
81
            raise TypeError("domain is not a GLSpace")
Jait Dixit's avatar
Jait Dixit committed
82
83

        if not isinstance(codomain, LMSpace):
84
            raise TypeError("codomain must be a LMSpace.")
Jait Dixit's avatar
Jait Dixit committed
85

86
87
88
89
        nlat = domain.nlat
        nlon = domain.nlon
        lmax = codomain.lmax
        mmax = codomain.mmax
Jait Dixit's avatar
Jait Dixit committed
90

theos's avatar
theos committed
91
        if lmax != mmax:
92
            cls.logger.warn("Unrecommended: codomain has lmax != mmax.")
Jait Dixit's avatar
Jait Dixit committed
93

theos's avatar
theos committed
94
        if lmax != nlat - 1:
95
            cls.logger.warn("Unrecommended: codomain has lmax != nlat - 1.")
Jait Dixit's avatar
Jait Dixit committed
96

97
        if nlon != 2*nlat - 1:
98
            cls.logger.warn("Unrecommended: domain has nlon != 2*nlat - 1.")
theos's avatar
theos committed
99

100
        super(GLLMTransformation, cls).check_codomain(domain, codomain)
101

102
    def _transformation_of_slice(self, inp, **kwargs):
103
104
105
106
        nlat = self.domain.nlat
        nlon = self.domain.nlon
        lmax = self.codomain.lmax
        mmax = self.codomain.mmax
107

108
109
110
        sjob = pyHealpix.sharpjob_d()
        sjob.set_Gauss_geometry(nlat, nlon)
        sjob.set_triangular_alm_info(lmax, mmax)
111
        if issubclass(inp.dtype.type, np.complexfloating):
112
            [resultReal, resultImag] = [sjob.map2alm(x)
113
114
                                        for x in (inp.real, inp.imag)]

115
            [resultReal,
116
             resultImag] = [lm_transformation_helper.buildIdx(x, lmax=lmax)
117
                            for x in [resultReal, resultImag]]
theos's avatar
theos committed
118

119
            result = self._combine_complex_result(resultReal, resultImag)
120
        else:
121
            result = sjob.map2alm(inp)
122
            result = lm_transformation_helper.buildIdx(result, lmax=lmax)
123

124
        return result