lmgltransformation.py 4.12 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/>.

Jait Dixit's avatar
Jait Dixit committed
19
import numpy as np
20
from nifty.config import dependency_injector as gdi
Jait Dixit's avatar
Jait Dixit committed
21
22
from nifty import GLSpace, LMSpace

theos's avatar
theos committed
23
from slicing_transformation import SlicingTransformation
24
import lm_transformation_helper
Jait Dixit's avatar
Jait Dixit committed
25

26
pyHealpix = gdi.get('pyHealpix')
theos's avatar
theos committed
27
28
29
30
31


class LMGLTransformation(SlicingTransformation):

    # ---Overwritten properties and methods---
Jait Dixit's avatar
Jait Dixit committed
32

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.")
theos's avatar
theos committed
43

44
        super(LMGLTransformation, self).__init__(domain, codomain, module)
theos's avatar
theos committed
45
46

    # ---Mandatory properties and methods---
Jait Dixit's avatar
Jait Dixit committed
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
70
71
72
73
74
75
        """
            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
            ----------
            .. [#] M. Reinecke and D. Sverre Seljebotn, 2013,
                   "Libsharp - spherical
                   harmonic transforms revisited";
                   `arXiv:1303.4945 <http://www.arxiv.org/abs/1303.4945>`_
        """
        if not isinstance(domain, LMSpace):
76
            raise TypeError("domain needs to be a LMSpace")
Jait Dixit's avatar
Jait Dixit committed
77

78
        nlat = domain.lmax + 1
79
        nlon = domain.lmax*2 + 1
theos's avatar
theos committed
80

Martin Reinecke's avatar
Martin Reinecke committed
81
        result = GLSpace(nlat=nlat, nlon=nlon)
82
        return result
Jait Dixit's avatar
Jait Dixit committed
83

84
85
    @classmethod
    def check_codomain(cls, domain, codomain):
Jait Dixit's avatar
Jait Dixit committed
86
        if not isinstance(domain, LMSpace):
87
            raise TypeError("domain is not a LMSpace")
Jait Dixit's avatar
Jait Dixit committed
88
89

        if not isinstance(codomain, GLSpace):
90
            raise TypeError("codomain must be a GLSpace.")
Jait Dixit's avatar
Jait Dixit committed
91

92
93
94
95
        nlat = codomain.nlat
        nlon = codomain.nlon
        lmax = domain.lmax
        mmax = domain.mmax
Jait Dixit's avatar
Jait Dixit committed
96

theos's avatar
theos committed
97
        if lmax != mmax:
98
            cls.logger.warn("Unrecommended: codomain has lmax != mmax.")
Jait Dixit's avatar
Jait Dixit committed
99

theos's avatar
theos committed
100
        if nlat != lmax + 1:
101
            cls.logger.warn("Unrecommended: codomain has nlat != lmax + 1.")
Jait Dixit's avatar
Jait Dixit committed
102

103
        if nlon != 2*lmax + 1:
104
            cls.logger.warn("Unrecommended: domain has nlon != 2*lmax + 1.")
Jait Dixit's avatar
Jait Dixit committed
105

106
        super(LMGLTransformation, cls).check_codomain(domain, codomain)
Jait Dixit's avatar
Jait Dixit committed
107

108
    def _transformation_of_slice(self, inp, **kwargs):
theos's avatar
theos committed
109
110
111
112
        nlat = self.codomain.nlat
        nlon = self.codomain.nlon
        lmax = self.domain.lmax
        mmax = self.domain.mmax
Jait Dixit's avatar
Jait Dixit committed
113

114
115
116
        sjob = pyHealpix.sharpjob_d()
        sjob.set_Gauss_geometry(nlat, nlon)
        sjob.set_triangular_alm_info(lmax, mmax)
theos's avatar
theos committed
117
        if issubclass(inp.dtype.type, np.complexfloating):
118
            [resultReal,
119
             resultImag] = [lm_transformation_helper.buildLm(x, lmax=lmax)
120
                            for x in (inp.real, inp.imag)]
theos's avatar
theos committed
121

122
            [resultReal, resultImag] = [sjob.alm2map(x)
theos's avatar
theos committed
123
124
                                        for x in [resultReal, resultImag]]

125
            result = self._combine_complex_result(resultReal, resultImag)
theos's avatar
theos committed
126

Jait Dixit's avatar
Jait Dixit committed
127
        else:
128
            result = lm_transformation_helper.buildLm(inp, lmax=lmax)
129
            result = sjob.alm2map(result)
theos's avatar
theos committed
130
131

        return result