glmollweide.py 1.87 KB
Newer Older
1
2
# -*- coding: utf-8 -*-

Martin Reinecke's avatar
Martin Reinecke committed
3
from .heatmap import Heatmap
4
5
import numpy as np

Martin Reinecke's avatar
Martin Reinecke committed
6
from ...descriptors import Axis
7

8
9
from .mollweide_helper import mollweide_helper

Martin Reinecke's avatar
Martin Reinecke committed
10
import pyHealpix
11
12
13


class GLMollweide(Heatmap):
Theo Steininger's avatar
Theo Steininger committed
14
    def __init__(self, data, xsize=800, color_map=None,
15
                 webgl=False, smoothing=False, zmin=None, zmax=None):
Theo Steininger's avatar
Theo Steininger committed
16
17
18
        # smoothing 'best', 'fast', False
        self.xsize = xsize

19
20
        super(GLMollweide, self).__init__(data, color_map, webgl, smoothing,
                                          zmin, zmax)
Theo Steininger's avatar
Theo Steininger committed
21
22

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

Theo Steininger's avatar
Theo Steininger committed
35
    @staticmethod
36
37
38
39
40
41
42
43
44
    def _find_closest(A, target):
        # A must be sorted
        idx = A.searchsorted(target)
        idx = np.clip(idx, 1, len(A)-1)
        left = A[idx-1]
        right = A[idx]
        idx -= target - left < right - target
        return idx

Theo Steininger's avatar
Theo Steininger committed
45
46
47
48
49
    def _mollview(self, x):
        xsize = self.xsize
        nlat = x.shape[0]
        nlon = x.shape[1]

50
        res, mask, theta, phi = mollweide_helper(xsize)
Martin Reinecke's avatar
Martin Reinecke committed
51
52

        ra = np.linspace(0, 2*np.pi, nlon+1)
Martin Reinecke's avatar
fixes  
Martin Reinecke committed
53
54
        dec = pyHealpix.GL_thetas(nlat)
        ilat = self._find_closest(dec, theta)
Martin Reinecke's avatar
Martin Reinecke committed
55
        ilon = self._find_closest(ra, phi)
Martin Reinecke's avatar
PEP8  
Martin Reinecke committed
56
57
        ilon = np.where(ilon == nlon, 0, ilon)
        res[mask] = x[ilat, ilon]
Martin Reinecke's avatar
Martin Reinecke committed
58
        return res
59
60
61
62
63
64
65
66
67

    def default_width(self):
        return 1400

    def default_height(self):
        return 700

    def default_axes(self):
        return (Axis(visible=False), Axis(visible=False))