hpmollweide.py 1.32 KB
Newer Older
1 2 3 4 5 6
# -*- coding: utf-8 -*-

from nifty import dependency_injector as gdi
from heatmap import Heatmap
import numpy as np

7 8
from .mollweide_helper import mollweide_helper

9 10 11 12
pyHealpix = gdi.get('pyHealpix')


class HPMollweide(Heatmap):
Theo Steininger's avatar
Theo Steininger committed
13
    def __init__(self, data, xsize=800, color_map=None, webgl=False,
14
                 smoothing=False):  # smoothing 'best', 'fast', False
15
        if pyHealpix is None:
16 17
            raise ImportError(
                "The module pyHealpix is needed but not available.")
Theo Steininger's avatar
Theo Steininger committed
18 19 20 21
        self.xsize = xsize
        super(HPMollweide, self).__init__(data, color_map, webgl, smoothing)

    def at(self, data):
22 23 24 25
        if isinstance(data, list):
            data = [self._mollview(d) for d in data]
        else:
            data = self._mollview(data)
Theo Steininger's avatar
Theo Steininger committed
26 27 28 29 30
        return HPMollweide(data=data,
                           xsize=self.xsize,
                           color_map=self.color_map,
                           webgl=self.webgl,
                           smoothing=self.smoothing)
31

Theo Steininger's avatar
Theo Steininger committed
32 33
    def _mollview(self, x):
        xsize = self.xsize
34
        res, mask, theta, phi = mollweide_helper(xsize)
Martin Reinecke's avatar
Martin Reinecke committed
35

Martin Reinecke's avatar
Martin Reinecke committed
36 37 38 39
        ptg = np.empty((phi.size, 2), dtype=np.float64)
        ptg[:, 0] = theta
        ptg[:, 1] = phi
        base = pyHealpix.Healpix_Base(int(np.sqrt(x.size/12)), "RING")
Martin Reinecke's avatar
PEP8  
Martin Reinecke committed
40
        res[mask] = x[base.ang2pix(ptg)]
Martin Reinecke's avatar
Martin Reinecke committed
41
        return res