gllmtransformation.py 3.9 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
csongor's avatar
csongor committed
24
import lm_transformation_factory as ltf
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
        if 'pyHealpix' not in gdi:
35
            raise ImportError(
36
                "The module pyHealpix is needed but not available.")
37

38
39
        super(GLLMTransformation, self).__init__(domain, codomain,
                                                 module=module)
40
41

    # ---Mandatory properties and methods---
42

43
44
    @classmethod
    def get_codomain(cls, domain):
Jait Dixit's avatar
Jait Dixit committed
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
        """
            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):
61
62
            raise TypeError(
                "domain needs to be a GLSpace")
Jait Dixit's avatar
Jait Dixit committed
63

64
        nlat = domain.nlat
Jait Dixit's avatar
Jait Dixit committed
65
66
67
        lmax = nlat - 1
        mmax = nlat - 1
        if domain.dtype == np.dtype('float32'):
Theo Steininger's avatar
Theo Steininger committed
68
            return_dtype = np.float32
Jait Dixit's avatar
Jait Dixit committed
69
        else:
Theo Steininger's avatar
Theo Steininger committed
70
71
            return_dtype = np.float64

Martin Reinecke's avatar
Martin Reinecke committed
72
        result = LMSpace(lmax=lmax, dtype=return_dtype)
73
74
        cls.check_codomain(domain, result)
        return result
Jait Dixit's avatar
Jait Dixit committed
75

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

        if not isinstance(codomain, LMSpace):
83
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

Theo Steininger's avatar
Theo Steininger committed
91
        if lmax != mmax:
92
93
            raise ValueError(
                'ERROR: codomain has lmax != mmax.')
Jait Dixit's avatar
Jait Dixit committed
94

Theo Steininger's avatar
Theo Steininger committed
95
        if lmax != nlat - 1:
96
97
            raise ValueError(
                'ERROR: codomain has lmax != nlat - 1.')
Jait Dixit's avatar
Jait Dixit committed
98

Theo Steininger's avatar
Theo Steininger committed
99
        if nlon != 2 * nlat - 1:
100
101
            raise ValueError(
                'ERROR: domain has nlon != 2 * nlat - 1.')
Theo Steininger's avatar
Theo Steininger committed
102
103

        return None
104

105
    def _transformation_of_slice(self, inp, **kwargs):
106
107
108
109
        nlat = self.domain.nlat
        nlon = self.domain.nlon
        lmax = self.codomain.lmax
        mmax = self.codomain.mmax
110

111
        sjob=pyHealpix.sharpjob_d()
Martin Reinecke's avatar
Martin Reinecke committed
112
        sjob.set_Gauss_geometry(nlat,nlon)
113
        sjob.set_triangular_alm_info(lmax,mmax)
114
115
        if issubclass(inp.dtype.type, np.complexfloating):

116
            [resultReal, resultImag] = [sjob.map2alm(x)
117
118
                                        for x in (inp.real, inp.imag)]

Theo Steininger's avatar
Theo Steininger committed
119
120
121
            [resultReal, resultImag] = [ltf.buildIdx(x, lmax=lmax)
                                        for x in [resultReal, resultImag]]

122
            result = self._combine_complex_result(resultReal, resultImag)
123
        else:
124
            result = sjob.map2alm(inp)
125
            result = ltf.buildIdx(result, lmax=lmax)
126

127
        return result