lmgltransformation.py 5.15 KB
Newer Older
Jait Dixit's avatar
Jait Dixit committed
1
2
3
4
5
6
7
import numpy as np
from transformation import Transformation
from d2o import distributed_data_object
from nifty.config import dependency_injector as gdi
import nifty.nifty_utilities as utilities
from nifty import GLSpace, LMSpace

8
import lm_transformation_factory as ltf
Jait Dixit's avatar
Jait Dixit committed
9
10
11
12
gl = gdi.get('libsharp_wrapper_gl')


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

Jait Dixit's avatar
Jait Dixit committed
18
19
20
21
        if codomain is None:
            self.domain = domain
            self.codomain = self.get_codomain(domain)
        elif self.check_codomain(domain, codomain):
Jait Dixit's avatar
Jait Dixit committed
22
23
24
25
26
            self.domain = domain
            self.codomain = codomain
        else:
            raise ValueError("ERROR: Incompatible codomain!")

Jait Dixit's avatar
Jait Dixit committed
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
    @staticmethod
    def get_codomain(domain):
        """
            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 domain is None:
            raise ValueError('ERROR: cannot generate codomain for None')

        if not isinstance(domain, LMSpace):
            raise TypeError('ERROR: domain needs to be a LMSpace')

        if domain.dtype == np.dtype('complex64'):
            new_dtype = np.float32
        elif domain.dtype == np.dtype('complex128'):
            new_dtype = np.float64
        else:
            raise ValueError('ERROR: unsupported domain dtype')

63
64
        nlat = domain.lmax + 1
        nlon = domain.lmax * 2 + 1
Jait Dixit's avatar
Jait Dixit committed
65
66
        return GLSpace(nlat=nlat, nlon=nlon, dtype=new_dtype)

Jait Dixit's avatar
Jait Dixit committed
67
68
69
70
71
72
73
74
75
76
77
    @staticmethod
    def check_codomain(domain, codomain):
        if not isinstance(domain, LMSpace):
            raise TypeError('ERROR: domain is not a LMSpace')

        if codomain is None:
            return False

        if not isinstance(codomain, GLSpace):
            raise TypeError('ERROR: codomain must be a GLSpace.')

78
79
80
81
        nlat = codomain.nlat
        nlon = codomain.nlon
        lmax = domain.lmax
        mmax = domain.mmax
Jait Dixit's avatar
Jait Dixit committed
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115

        if (lmax != mmax) or (nlat != lmax + 1) or (nlon != 2 * lmax + 1):
            return False

        return True

    def transform(self, val, axes=None, **kwargs):
        """
        LM -> GL transform method.

        Parameters
        ----------
        val : np.ndarray or distributed_data_object
            The value array which is to be transformed

        axes : None or tuple
            The axes along which the transformation should take place

        """
        if isinstance(val, distributed_data_object):
            temp_val = val.get_full_data()
        else:
            temp_val = val

        return_val = None

        for slice_list in utilities.get_slice_list(temp_val.shape, axes):
            if slice_list == [slice(None, None)]:
                inp = temp_val
            else:
                if return_val is None:
                    return_val = np.empty_like(temp_val)
                inp = temp_val[slice_list]

116
117
118
119
            nlat = self.codomain.nlat
            nlon = self.codomain.nlon
            lmax = self.domain.lmax
            mmax = self.mmax
Jait Dixit's avatar
Jait Dixit committed
120

csongor's avatar
csongor committed
121
            if inp.dtype >= np.dtype('complex64'):
122
123
124
125
                inpReal = np.real(inp)
                inpImag = np.imag(inp)
                inpReal = ltf.buildLm(inpReal,lmax=lmax)
                inpImag = ltf.buildLm(inpImag,lmax=lmax)
csongor's avatar
csongor committed
126
                inpReal = self.GlAlm2Map(inpReal, nlat=nlat, nlon=nlon,
127
                                 lmax=lmax, mmax=mmax, cl=False)
csongor's avatar
csongor committed
128
                inpImag = self.GlAlm2Map(inpImag, nlat=nlat, nlon=nlon,
Jait Dixit's avatar
Jait Dixit committed
129
                                 lmax=lmax, mmax=mmax, cl=False)
130
131
132
                inp = inpReal+inpImag*(1j)
            else:
                inp = ltf.buildLm(inp, lmax=lmax)
csongor's avatar
csongor committed
133
134
                inp = self.GlAlm2Map(inp, nlat=nlat, nlon=nlon,
                                   lmax=lmax, mmax=mmax, cl=False)
Jait Dixit's avatar
Jait Dixit committed
135
136
137
138
139
140
141
142

            if slice_list == [slice(None, None)]:
                return_val = inp
            else:
                return_val[slice_list] = inp

        # re-weight if discrete
        if self.codomain.discrete:
Jait Dixit's avatar
Jait Dixit committed
143
            val = self.codomain.weight(val, power=0.5, axes=axes)
Jait Dixit's avatar
Jait Dixit committed
144
145
146
147
148
149
150
151

        if isinstance(val, distributed_data_object):
            new_val = val.copy_empty(dtype=self.codomain.dtype)
            new_val.set_full_data(return_val, copy=False)
        else:
            return_val = return_val.astype(self.codomain.dtype, copy=False)

        return return_val
csongor's avatar
csongor committed
152
153
154
155
156
157

    def GlAlm2Map(self, inp, **kwargs):
        if inp.dtype == np.dtype('complex64'):
            return gl.alm2map_f(inp, kwargs)
        else:
            return gl.alm2map(inp, kwargs)