Beispiel #1
0
    def to_odd_npix(self, max_radius=None):
        """Create a new geom object with an odd number of pixel and a maximum size.

        This is useful for PSF kernel creation.

        Parameters
        ----------
        max_radius : `~astropy.units.Quantity`
            Max. radius of the geometry (half the width)

        Returns
        -------
        geom : `WcsGeom`
            Geom with odd number of pixels
        """
        if max_radius is None:
            width = self.width.max()
        else:
            width = 2 * u.Quantity(max_radius)

        binsz = self.pixel_scales.max()

        width_npix = (width / binsz).to_value("")
        npix = round_up_to_odd(width_npix)
        return WcsGeom.create(
            skydir=self.center_skydir,
            binsz=binsz,
            npix=npix,
            proj=self.projection,
            frame=self.frame,
            axes=self.axes,
        )
Beispiel #2
0
    def cutout(self, position, width, mode="trim", odd_npix=False):
        """
        Create a cutout around a given position.

        Parameters
        ----------
        position : `~astropy.coordinates.SkyCoord`
            Center position of the cutout region.
        width : tuple of `~astropy.coordinates.Angle`
            Angular sizes of the region in (lon, lat) in that specific order.
            If only one value is passed, a square region is extracted.
        mode : {'trim', 'partial', 'strict'}
            Mode option for Cutout2D, for details see `~astropy.nddata.utils.Cutout2D`.
        odd_npix : bool
            Force width to odd number of pixels.

        Returns
        -------
        cutout : `~gammapy.maps.WcsNDMap`
            Cutout map
        """
        width = _check_width(width) * u.deg

        binsz = self.pixel_scales
        width_npix = np.clip((width / binsz).to_value(""), 1, None)
        width = width_npix*binsz

        if odd_npix:
            width = round_up_to_odd(width_npix)

        dummy_data = np.empty(self.to_image().data_shape)
        c2d = Cutout2D(
            data=dummy_data,
            wcs=self.wcs,
            position=position,
            # Cutout2D takes size with order (lat, lon)
            size=width[::-1],
            mode=mode,
        )
        return self._init_copy(wcs=c2d.wcs, npix=c2d.shape[::-1])
Beispiel #3
0
    def binary_structure(self, width, kernel="disk"):
        """Get binary structure

        Parameters
        ----------
        width : `~astropy.units.Quantity`, str or float
            If a float is given it interpreted as width in pixels. If an (angular)
            quantity is given it converted to pixels using ``geom.wcs.wcs.cdelt``.
            The width corresponds to radius in case of a disk kernel, and
            the side length in case of a box kernel.
        kernel : {'disk', 'box'}
            Kernel shape

        Returns
        -------
        structure : `~numoy.ndarray`
            Binary structure
        """
        width = u.Quantity(width)

        if width.unit.is_equivalent("deg"):
            width = width / self.pixel_scales

        width = round_up_to_odd(width.to_value(""))

        if kernel == "disk":
            disk = Tophat2DKernel(width[0])
            disk.normalize("peak")
            structure = disk.array
        elif kernel == "box":
            structure = np.ones(width)
        else:
            raise ValueError(f"Invalid kernel: {kernel!r}")

        shape = (1, ) * len(self.axes) + structure.shape
        return structure.reshape(shape)