Exemplo n.º 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)
Exemplo n.º 2
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