gl_space.py 4.44 KB
Newer Older
csongor's avatar
csongor committed
1
2
from __future__ import division

Jait Dixit's avatar
Jait Dixit committed
3
import itertools
csongor's avatar
csongor committed
4
5
6
7
import numpy as np

from d2o import STRATEGIES as DISTRIBUTION_STRATEGIES

8
from nifty.spaces.space import Space
9
10
from nifty.config import about, nifty_configuration as gc,\
                         dependency_injector as gdi
theos's avatar
theos committed
11
from gl_space_paradict import GLSpaceParadict
12
import nifty.nifty_utilities as utilities
csongor's avatar
csongor committed
13
14
15
16
17

gl = gdi.get('libsharp_wrapper_gl')

GL_DISTRIBUTION_STRATEGIES = DISTRIBUTION_STRATEGIES['global']

18
19

class GLSpace(Space):
csongor's avatar
csongor committed
20
21
22
23
24
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
    """
        ..                 __
        ..               /  /
        ..     ____ __  /  /
        ..   /   _   / /  /
        ..  /  /_/  / /  /_
        ..  \___   /  \___/  space class
        .. /______/

        NIFTY subclass for Gauss-Legendre pixelizations [#]_ of the two-sphere.

        Parameters
        ----------
        nlat : int
            Number of latitudinal bins, or rings.
        nlon : int, *optional*
            Number of longitudinal bins (default: ``2*nlat - 1``).
        dtype : numpy.dtype, *optional*
            Data type of the field values (default: numpy.float64).

        See Also
        --------
        hp_space : A class for the HEALPix discretization of the sphere [#]_.
        lm_space : A class for spherical harmonic components.

        Notes
        -----
        Only real-valued fields on the two-sphere are supported, i.e.
        `dtype` has to be either numpy.float64 or numpy.float32.

        References
        ----------
        .. [#] M. Reinecke and D. Sverre Seljebotn, 2013, "Libsharp - spherical
               harmonic transforms revisited";
               `arXiv:1303.4945 <http://www.arxiv.org/abs/1303.4945>`_
        .. [#] K.M. Gorski et al., 2005, "HEALPix: A Framework for
               High-Resolution Discretization and Fast Analysis of Data
               Distributed on the Sphere", *ApJ* 622..759G.

        Attributes
        ----------
        para : numpy.ndarray
            One-dimensional array containing the two numbers `nlat` and `nlon`.
        dtype : numpy.dtype
            Data type of the field values.
        discrete : bool
            Whether or not the underlying space is discrete, always ``False``
            for spherical spaces.
        vol : numpy.ndarray
            An array containing the pixel sizes.
    """

72
    def __init__(self, nlat, nlon=None, dtype=np.dtype('float')):
csongor's avatar
csongor committed
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
        """
            Sets the attributes for a gl_space class instance.

            Parameters
            ----------
            nlat : int
                Number of latitudinal bins, or rings.
            nlon : int, *optional*
                Number of longitudinal bins (default: ``2*nlat - 1``).
            dtype : numpy.dtype, *optional*
                Data type of the field values (default: numpy.float64).

            Returns
            -------
            None

            Raises
            ------
            ImportError
                If the libsharp_wrapper_gl module is not available.
            ValueError
                If input `nlat` is invaild.

        """
        # check imports
        if not gc['use_libsharp']:
            raise ImportError(about._errors.cstring(
                "ERROR: libsharp_wrapper_gl not loaded."))

Jait Dixit's avatar
Jait Dixit committed
102
        # setup paradict
theos's avatar
theos committed
103
        self.paradict = GLSpaceParadict(nlat=nlat, nlon=nlon)
csongor's avatar
csongor committed
104

105
106
        # setup dtype
        self.dtype = np.dtype(dtype)
csongor's avatar
csongor committed
107

Jait Dixit's avatar
Jait Dixit committed
108
109
        # GLSpace is not harmonic
        self._harmonic = False
csongor's avatar
csongor committed
110
111
112
113
114

    @property
    def shape(self):
        return (np.int((self.paradict['nlat'] * self.paradict['nlon'])),)

115
    @property
116
117
118
119
120
121
    def dim(self):
        return np.int((self.paradict['nlat'] * self.paradict['nlon']))

    @property
    def total_volume(self):
        return 4 * np.pi
122

Jait Dixit's avatar
Jait Dixit committed
123
    def weight(self, x, power=1, axes=None, inplace=False):
124
        axes = utilities.cast_axis_to_tuple(axes, length=1)
125

126
127
128
129
130
131
132
        nlon = self.paradict['nlon']
        nlat = self.paradict['nlat']

        weight = np.array(list(itertools.chain.from_iterable(
                    itertools.repeat(x ** power, nlon)
                    for x in gl.vol(nlat))
                 ))
Jait Dixit's avatar
Jait Dixit committed
133
134
135

        if axes is not None:
            # reshape the weight array to match the input shape
136
            new_shape = np.ones(len(x.shape), dtype=np.int)
Jait Dixit's avatar
Jait Dixit committed
137
138
139
140
141
142
143
            for index in range(len(axes)):
                new_shape[index] = len(weight)
            weight = weight.reshape(new_shape)

        if inplace:
            x *= weight
            result_x = x
csongor's avatar
csongor committed
144
        else:
Jait Dixit's avatar
Jait Dixit committed
145
            result_x = x * weight
csongor's avatar
csongor committed
146

Jait Dixit's avatar
Jait Dixit committed
147
        return result_x