Example #1
0
 def smooth(self, hpxmap, badval=hp.UNSEEN, sigma=None):
     """ Smooth a healpix map """
     healpix.check_hpxmap(hpxmap, None, None)
     hpxmap = healpix.masked_array(hpxmap, badval)
     hpxmap.fill_value = np.ma.median(hpxmap)
     smooth = hp.smoothing(hpxmap, sigma=np.radians(sigma), verbose=False)
     return np.ma.array(smooth, mask=hpxmap.mask)
Example #2
0
    def draw_hpxmap_rgb(self, r, g, b, xsize=800, **kwargs):
        hpxmap = healpix.masked_array(np.array([r, g, b]))

        vmin, vmax = np.percentile(hpxmap.compressed(), [0.1, 99.9])

        defaults = dict(latlon=True, rasterized=True, vmin=vmin, vmax=vmax)
        setdefaults(kwargs, defaults)

        #ax = plt.gca()
        #lonra = [-180.,180.]
        #latra = [-90.,90]

        #lon = np.linspace(lonra[0], lonra[1], xsize)
        #lat = np.linspace(latra[0], latra[1], xsize*self.aspect)
        #lon, lat = np.meshgrid(lon, lat)

        #nside = hp.get_nside(hpxmap.data)
        #try:
        #    pix = hp.ang2pix(nside,lon,lat,lonlat=True)
        #except TypeError:
        #    pix = hp.ang2pix(nside,np.radians(90-lat),np.radians(lon))

        lonra, latra = self.get_map_range(r)

        lon, lat, R = self.hpx2xy(r, lonra=lonra, latra=latra, xsize=xsize)
        _, _, G = self.hpx2xy(g, lonra=lonra, latra=latra, xsize=xsize)
        _, _, B = self.hpx2xy(b, lonra=lonra, latra=latra, xsize=xsize)

        # Colors are all normalized to R... probably not desired...
        #norm = np.nanmax(R)
        #r = self.set_scale(R,norm=norm)
        #g = self.set_scale(G,norm=norm)
        #b = self.set_scale(B,norm=norm)

        # Better?
        norm = np.percentile(R[~np.isnan(R)], 97.5)
        kw = dict(log=False, sigma=0.5, norm=norm)
        r = self.set_scale(R, **kw)
        g = self.set_scale(G, **kw)
        b = self.set_scale(B, **kw)

        #rgb = np.array([r,g,b]).T
        color_tuples = np.array([
            r[:-1, :-1].filled(np.nan).flatten(),
            g[:-1, :-1].filled(np.nan).flatten(),
            b[:-1, :-1].filled(np.nan).flatten()
        ]).T
        color_tuples[np.where(np.isnan(color_tuples))] = 0.0
        setdefaults(kwargs, {'color': color_tuples})

        if self.projection is 'ortho':
            im = self.pcolor(lon, lat, r, **kwargs)
        else:
            im = self.pcolormesh(lon, lat, r, **kwargs)
        plt.gca().set_facecolor((0, 0, 0))
        plt.draw()

        return im, lon, lat, r, color_tuples
Example #3
0
    def draw_hpxmap(self,
                    hpxmap,
                    pixel=None,
                    nside=None,
                    xsize=800,
                    lonra=None,
                    latra=None,
                    badval=hp.UNSEEN,
                    smooth=None,
                    **kwargs):
        """
        Use pcolor/pcolormesh to draw healpix map.

        Parameters:
        -----------
        hpxmap: input healpix map
        pixel:  explicit pixel indices (required for partial maps)
        nside:  explicit nside of the map (required for partial maps)
        xsize:  resolution of the output image
        lonra:  longitude range [-180,180] (deg)
        latra:  latitude range [-90,90] (deg)
        badval: set of values considered "bad"
        smooth: gaussian smoothing kernel (deg)

        Returns:
        --------
        im,lon,lat,values : mpl image with pixel longitude, latitude (deg), and values
        """
        healpix.check_hpxmap(hpxmap, pixel, nside)
        hpxmap = healpix.masked_array(hpxmap, badval)

        if smooth:
            # To smooth we need the full map
            hpxmap = healpix.create_map(hpxmap, pixel, nside, badval)
            pixel, nside = None, None
            hpxmap = healpix.masked_array(hpxmap, badval)
            hpxmap = self.smooth(hpxmap, sigma=smooth)

        #if pixel is None:
        #    nside = hp.get_nside(hpxmap.data)
        #    pixel = np.arange(len(hpxmap),dtype=int)
        #elif nside is None:
        #    msg = "'nside' must be specified for explicit maps"
        #    raise Exception(msg)

        vmin, vmax = np.percentile(hpxmap.compressed(), [2.5, 97.5])

        defaults = dict(latlon=True, rasterized=True, vmin=vmin, vmax=vmax)
        setdefaults(kwargs, defaults)

        lon, lat, values = healpix.hpx2xy(hpxmap,
                                          pixel=pixel,
                                          nside=nside,
                                          xsize=xsize,
                                          lonra=lonra,
                                          latra=latra)

        # pcolormesh doesn't work in Ortho...
        if self.projection == 'ortho':
            im = self.pcolor(lon, lat, values, **kwargs)
        else:
            # Why were we plotting the values.data?
            #im = self.pcolormesh(lon,lat,values.data,**kwargs)

            # pcolormesh recommends that values be larger than x,y
            # but basemap has problems with this (sometimes?)
            # https://github.com/matplotlib/basemap/issues/182
            try:
                im = self.pcolormesh(lon, lat, values, **kwargs)
            except IndexError:
                im = self.pcolormesh(lon[:-1, :-1], lat[:-1, :-1], values,
                                     **kwargs)

        return im, lon, lat, values