hpmollweide.py 1.69 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# -*- coding: utf-8 -*-

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

pyHealpix = gdi.get('pyHealpix')


class HPMollweide(Heatmap):
    def __init__(self, data, color_map=None, webgl=False,
                 smoothing=False):  # smoothing 'best', 'fast', False
        if 'pyHealpix' not in gdi:
            raise ImportError(
                "The module pyHealpix is needed but not available.")
        if isinstance(data, list):
            data = [self._mollview(d) for d in data]
        else:
            data = self._mollview(data)
        super(HPMollweide, self).__init__(data, color_map, webgl, smoothing)

Martin Reinecke's avatar
Martin Reinecke committed
22
23
    @staticmethod
    def _mollweidehelper(xsize):
Martin Reinecke's avatar
Martin Reinecke committed
24
25
        xsize = int(xsize)
        ysize = int(xsize/2)
Martin Reinecke's avatar
PEP8    
Martin Reinecke committed
26
27
        res = np.full(shape=(ysize, xsize), fill_value=np.nan,
                      dtype=np.float64)
Martin Reinecke's avatar
Martin Reinecke committed
28
29
        xc = (xsize-1)*0.5
        yc = (ysize-1)*0.5
Martin Reinecke's avatar
Martin Reinecke committed
30
31
32
        u, v = np.meshgrid(np.arange(xsize), np.arange(ysize))
        u = 2*(u-xc)/(xc/1.02)
        v = (v-yc)/(yc/1.02)
Martin Reinecke's avatar
Martin Reinecke committed
33
34
35
36
37
38

        mask = np.where((u*u*0.25 + v*v) <= 1.)
        t1 = v[mask]
        theta = 0.5*np.pi-(
            np.arcsin(2/np.pi*(np.arcsin(t1) + t1*np.sqrt((1.-t1)*(1+t1)))))
        phi = -0.5*np.pi*u[mask]/np.maximum(np.sqrt((1-t1)*(1+t1)), 1e-6)
Martin Reinecke's avatar
PEP8    
Martin Reinecke committed
39
        phi = np.where(phi < 0, phi+2*np.pi, phi)
Martin Reinecke's avatar
Martin Reinecke committed
40
41
42
43
44
        return res, mask, theta, phi

    def _mollview(self, x, xsize=800):
        res, mask, theta, phi = self._mollweidehelper(xsize)

Martin Reinecke's avatar
Martin Reinecke committed
45
46
47
48
        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
49
        res[mask] = x[base.ang2pix(ptg)]
Martin Reinecke's avatar
Martin Reinecke committed
50
        return res