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

gl = gdi.get('libsharp_wrapper_gl')

10

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

Jait Dixit's avatar
Jait Dixit committed
16
17
18
19
        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
20
21
22
23
            self.domain = domain
            self.codomain = codomain
        else:
            raise ValueError("ERROR: Incompatible codomain!")
24

Jait Dixit's avatar
Jait Dixit committed
25
26
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
    @staticmethod
    def get_codomain(domain):
        """
            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 domain is None:
            raise ValueError('ERROR: cannot generate codomain for None')

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

        nlat = domain.paradict['nlat']
        lmax = nlat - 1
        mmax = nlat - 1
        if domain.dtype == np.dtype('float32'):
            return LMSpace(lmax=lmax, mmax=mmax, dtype=np.complex64)
        else:
            return LMSpace(lmax=lmax, mmax=mmax, dtype=np.complex128)

Jait Dixit's avatar
Jait Dixit committed
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
    @staticmethod
    def check_codomain(domain, codomain):
        if not isinstance(domain, GLSpace):
            raise TypeError('ERROR: domain is not a GLSpace')

        if codomain is None:
            return False

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

        nlat = domain.paradict['nlat']
        nlon = domain.paradict['nlon']
        lmax = codomain.paradict['lmax']
        mmax = codomain.paradict['mmax']

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

        return True

    def transform(self, val, axes=None, **kwargs):
        """
        GL -> LM 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
87

Jait Dixit's avatar
Jait Dixit committed
88
        """
89
        if self.domain.discrete:
Jait Dixit's avatar
Jait Dixit committed
90
            val = self.domain.weight(val, power=-0.5, axes=axes)
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132

        # shorthands for transform parameters
        nlat = self.domain.paradict['nlat']
        nlon = self.domain.paradict['nlon']
        lmax = self.codomain.paradict['lmax']
        mmax = self.codomain.paradict['mmax']

        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]

            if self.domain.dtype == np.dtype('float32'):
                inp = gl.map2alm_f(inp,
                                   nlat=nlat, nlon=nlon,
                                   lmax=lmax, mmax=mmax)
            else:
                inp = gl.map2alm(inp,
                                 nlat=nlat, nlon=nlon,
                                 lmax=lmax, mmax=mmax)

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

        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)

133
        return return_val