hp_space.py 4.99 KB
Newer Older
csongor's avatar
csongor committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
# NIFTY (Numerical Information Field Theory) has been developed at the
# Max-Planck-Institute for Astrophysics.
#
# Copyright (C) 2015 Max-Planck-Society
#
# Author: Marco Selig
# Project homepage: <http://www.mpa-garching.mpg.de/ift/nifty/>
#
# 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/>.

"""
    ..                  __   ____   __
    ..                /__/ /   _/ /  /_
    ..      __ ___    __  /  /_  /   _/  __   __
    ..    /   _   | /  / /   _/ /  /   /  / /  /
    ..   /  / /  / /  / /  /   /  /_  /  /_/  /
    ..  /__/ /__/ /__/ /__/    \___/  \___   /  lm
    ..                               /______/

    NIFTY submodule for grids on the two-sphere.

"""
from __future__ import division

import numpy as np
37

38
from nifty.config import about
39
from nifty.spaces.space import Space
40
from nifty.config import nifty_configuration as gc, \
csongor's avatar
csongor committed
41 42 43 44
                         dependency_injector as gdi

hp = gdi.get('healpy')

45 46

class HPSpace(Space):
csongor's avatar
csongor committed
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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
    """
        ..        __
        ..      /  /
        ..     /  /___    ______
        ..    /   _   | /   _   |
        ..   /  / /  / /  /_/  /
        ..  /__/ /__/ /   ____/  space class
        ..           /__/

        NIFTY subclass for HEALPix discretizations of the two-sphere [#]_.

        Parameters
        ----------
        nside : int
            Resolution parameter for the HEALPix discretization, resulting in
            ``12*nside**2`` pixels.

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

        Notes
        -----
        Only powers of two are allowed for `nside`.

        References
        ----------
        .. [#] 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.
        .. [#] M. Reinecke and D. Sverre Seljebotn, 2013, "Libsharp - spherical
               harmonic transforms revisited";
               `arXiv:1303.4945 <http://www.arxiv.org/abs/1303.4945>`_

        Attributes
        ----------
        para : numpy.ndarray
            Array containing the number `nside`.
        dtype : numpy.dtype
            Data type of the field values, which is always numpy.float64.
        discrete : bool
            Whether or not the underlying space is discrete, always ``False``
            for spherical spaces.
        vol : numpy.ndarray
            An array with one element containing the pixel size.
    """

96 97
    # ---Overwritten properties and methods---

98
    def __init__(self, nside=2, dtype=np.dtype('float')):
csongor's avatar
csongor committed
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
        """
            Sets the attributes for a hp_space class instance.

            Parameters
            ----------
            nside : int
                Resolution parameter for the HEALPix discretization, resulting
                in ``12*nside**2`` pixels.

            Returns
            -------
            None

            Raises
            ------
            ImportError
                If the healpy module is not available.
            ValueError
                If input `nside` is invaild.

        """
        # check imports
        if not gc['use_healpy']:
122
            raise ImportError("ERROR: healpy not available or not loaded.")
csongor's avatar
csongor committed
123

124
        super(HPSpace, self).__init__(dtype)
csongor's avatar
csongor committed
125

126
        self._nside = self._parse_nside(nside)
csongor's avatar
csongor committed
127

128 129 130 131 132
    # ---Mandatory properties and methods---

    @property
    def harmonic(self):
        return False
csongor's avatar
csongor committed
133 134 135

    @property
    def shape(self):
136
        return (np.int(12 * self.nside ** 2),)
csongor's avatar
csongor committed
137 138

    @property
Jait Dixit's avatar
Jait Dixit committed
139
    def dim(self):
140
        return np.int(12 * self.nside ** 2)
csongor's avatar
csongor committed
141

142 143 144
    @property
    def total_volume(self):
        return 4 * np.pi
145

146 147 148 149
    def copy(self):
        return self.__class__(nside=self.nside,
                              dtype=self.dtype)

150
    def weight(self, x, power=1, axes=None, inplace=False):
151
        weight = ((4*np.pi) / (12 * self.nside**2)) ** power
152 153 154 155 156 157 158 159

        if inplace:
            x *= weight
            result_x = x
        else:
            result_x = x * weight

        return result_x
160 161 162 163 164 165 166 167 168 169 170 171 172

    # ---Added properties and methods---

    @property
    def nside(self):
        return self._nside

    def _parse_nside(self, nside):
        nside = int(nside)
        if nside & (nside - 1) != 0 or nside < 2:
            raise ValueError(about._errors.cstring(
                "ERROR: nside must be positive and a multiple of 2."))
        return nside