Beispiel #1
0
    def shift_mask(self, dx, dy, method='lanczos', order=5, cval=0.0):
        '''Shift the mask of Stack object.

        Parameters:
            dx, dy (float): shift distance (in pixel) along x (horizontal) and y (vertical). 
                Note that elements in one row has the same y but different x. 
                Example: dx = 2 is to shift the image "RIGHT", dy = 3 is to shift the image "UP".
            method (str): interpolation method. Use 'lanczos' or 'spline'.
            order (int): the order of spline interpolation (within 0-5) or Lanczos interpolation (>0).
            cval (scalar): value to fill the edges. Default is NaN.

        Returns:
            shift_mask: ndarray.
        '''
        if not hasattr(self, 'mask'):
            raise AttributeError("This `Stack` object doesn't have `mask`!")
        ny, nx = self.image.shape
        if abs(dx) > nx or abs(ny) > ny:
            raise ValueError('# Shift distance is beyond the image size.')

        if method == 'lanczos':
            try:  # try to import galsim
                from galsim import degrees, Angle
                from galsim.interpolant import Lanczos
                from galsim import Image, InterpolatedImage
                from galsim.fitswcs import AstropyWCS
            except:
                raise ImportError(
                    '# Import `galsim` failed! Please check if `galsim` is installed!'
                )
            # Begin shift
            assert (order > 0) and isinstance(
                order,
                int), 'order of ' + method + ' must be positive interger.'
            galimg = InterpolatedImage(Image(self.mask, dtype=float),
                                       scale=0.168,
                                       x_interpolant=Lanczos(order))
            galimg = galimg.shift(dx=dx * 0.168, dy=dy * 0.168)
            result = galimg.drawImage(scale=0.168, nx=nx,
                                      ny=ny)  #, wcs=AstropyWCS(self.wcs))
            self._mask = (result.array > 0.5).astype(float)
            # Change the WCS of image
            hdr = copy.deepcopy(self.header)
            hdr['CRPIX1'] += dx
            hdr['CRPIX2'] += dy
            self.header = hdr
            self.wcs = wcs.WCS(hdr)
            return result.array
        elif method == 'spline':
            from scipy.ndimage.interpolation import shift
            assert 0 < order <= 5 and isinstance(
                order, int), 'order of ' + method + ' must be within 0-5.'
            result = shift(self.mask, [dy, dx],
                           order=order,
                           mode='constant',
                           cval=cval)
            self._mask = (result > 0.5).astype(float)
            # Change the WCS of image
            hdr = copy.deepcopy(self.header)
            hdr['CRPIX1'] += dx
            hdr['CRPIX2'] += dy
            self.header = hdr
            self.wcs = wcs.WCS(hdr)
            return result
        else:
            raise ValueError(
                "# Not supported interpolation method. Use 'lanczos' or 'spline'."
            )
Beispiel #2
0
    def shift_mask(self, dx, dy, method='iraf', order=5, cval=0.0):
        '''Shift the mask of Celestial object.

        Parameters:
            dx, dy (float): shift distance (in pixel) along x (horizontal) and y (vertical). 
                Note that elements in one row has the same y but different x. 
                Example: dx = 2 is to shift the image "RIGHT", dy = 3 is to shift the image "UP".
            method (str): interpolation method. Use 'lanczos' or 'spline' or 'iraf'
            order (int): the order of spline interpolation (within 0-5) or Lanczos interpolation (>0).
            cval (scalar): value to fill the edges. Default is NaN.

        Returns:
            shift_mask: ndarray.
        '''
        ny, nx = self.mask.shape
        if abs(dx) > nx or abs(ny) > ny:
            raise ValueError('# Shift distance is beyond the image size.')
        if method == 'lanczos':
            try:  # try to import galsim
                from galsim import degrees, Angle
                from galsim.interpolant import Lanczos
                from galsim import Image, InterpolatedImage
                from galsim.fitswcs import AstropyWCS
            except:
                raise ImportError(
                    '# Import `galsim` failed! Please check if `galsim` is installed!'
                )
            # Begin shift
            assert (order > 0) and isinstance(
                order,
                int), 'order of ' + method + ' must be positive interger.'
            galimg = InterpolatedImage(Image(self.mask, dtype=float),
                                       scale=self.pixel_scale,
                                       x_interpolant=Lanczos(order))
            galimg = galimg.shift(dx=dx * self.pixel_scale,
                                  dy=dy * self.pixel_scale)
            result = galimg.drawImage(scale=self.pixel_scale, nx=nx,
                                      ny=ny)  #, wcs=AstropyWCS(self.wcs))
            self._mask = result.array
            # Change the WCS of image
            hdr = copy.deepcopy(self.header)
            hdr['CRPIX1'] += dx
            hdr['CRPIX2'] += dy
            self.header = hdr
            self.wcs = wcs.WCS(hdr)
            self._wcs_header_merge()
            return result.array
        elif method == 'iraf':
            self.save_to_fits('./_temp.fits', 'mask')
            imshift('./_temp.fits',
                    './_shift_temp.fits',
                    dx,
                    dy,
                    interp_type='poly3',
                    boundary_type='constant')
            hdu = fits.open('./_shift_temp.fits')
            self.mask = hdu[0].data
            self.shape = hdu[0].data.shape
            self.header = hdu[0].header
            self.wcs = wcs.WCS(self.header)
            hdu.close()
            imdelete('./*temp.fits')
        else:
            raise ValueError(
                "# Not supported interpolation method. Use 'lanczos' or 'iraf'."
            )
Beispiel #3
0
    def shift_mask(self, dx, dy, method='spline', order=5, cval=0.0):
        '''Shift the mask of Celestial object.

        Parameters:
            dx (float): shift distance (in pixel) along x (horizontal). 
                Note that elements in one row has the same y but different x. 
                Example: dx = 2 is to shift the mask "RIGHT" (as seen in DS9), dy = 3 is to shift the image "UP".
            dy (float): shift distance (in pixel) along y (vertical). 
                Note that elements in one row has the same y but different x. 
                Example: dx = 2 is to shift the mask "RIGHT" (as seen in DS9), dy = 3 is to shift the image "UP".
            method (str): interpolation method. Use 'spline', lanczos' or 'iraf'. 
                If using 'iraf', default interpolation is 'poly3. 'Lanczos' requires ``GalSim`` installed.
            order (int): the order of Spline or Lanczos interpolation (>0).
            cval (float): value to fill the edges. Default is 0.

        Returns:
            shift_mask (ndarray): shifted mask. The "mask" attribute of ``Celestial`` class will also be changed accordingly.
        '''

        ny, nx = self.mask.shape
        if abs(dx) > nx or abs(ny) > ny:
            raise ValueError('# Shift distance is beyond the image size.')
        if method == 'lanczos' or method == 'cubic' or method == 'quintic':
            try:  # try to import galsim
                from galsim import degrees, Angle
                from galsim.interpolant import Lanczos
                from galsim import Image, InterpolatedImage
                from galsim.fitswcs import AstropyWCS
            except:
                raise ImportError(
                    '# Import ``galsim`` failed! Please check if ``galsim`` is installed!'
                )
            # Begin shift
            assert (order > 0) and isinstance(
                order,
                int), 'order of ' + method + ' must be positive interger.'
            if method == 'lanczos':
                galimg = InterpolatedImage(Image(self.image, dtype=float),
                                           scale=self.pixel_scale,
                                           x_interpolant=Lanczos(order))
            else:
                galimg = InterpolatedImage(Image(self.image, dtype=float),
                                           scale=self.pixel_scale,
                                           x_interpolant=method)
            galimg = galimg.shift(dx=dx * self.pixel_scale,
                                  dy=dy * self.pixel_scale)
            result = galimg.drawImage(scale=self.pixel_scale, nx=nx,
                                      ny=ny)  #, wcs=AstropyWCS(self.wcs))
            self._mask = result.array
            # Change the WCS of image
            hdr = copy.deepcopy(self.header)
            hdr['CRPIX1'] += dx
            hdr['CRPIX2'] += dy
            self.header = hdr
            self.wcs = wcs.WCS(self.header)
            return result.array

        elif method == 'iraf':
            self.save_to_fits('./_temp.fits', 'mask')
            imshift('./_temp.fits',
                    './_shift_temp.fits',
                    dx,
                    dy,
                    interp_type='poly3',
                    boundary_type='constant')
            hdu = fits.open('./_shift_temp.fits')
            self.mask = hdu[0].data
            self.shape = hdu[0].data.shape
            self.header = hdu[0].header
            self.wcs = wcs.WCS(self.header)
            hdu.close()
            imdelete('./*temp.fits')
            return self.mask

        elif method == 'spline':
            from scipy.ndimage.interpolation import shift
            assert 0 < order <= 5 and isinstance(
                order, int), 'order of ' + method + ' must be within 0-5.'
            result = shift(self.mask, [dy, dx],
                           order=order,
                           mode='constant',
                           cval=cval)
            self._mask = result
            # Change the WCS of image
            hdr = copy.deepcopy(self.header)
            hdr['CRPIX1'] += dx
            hdr['CRPIX2'] += dy
            self.header = hdr
            self.wcs = wcs.WCS(self.header)
            return result

        else:
            raise ValueError(
                "# Not supported interpolation method. Use 'lanczos' or 'iraf'."
            )