Пример #1
0
def draw_ellipse(ax):
    """
    Draw an ellipse of width=0.1, height=0.15 in data coordinates
    """
    from mpl_toolkits.axes_grid1.anchored_artists import AnchoredEllipse
    ae = AnchoredEllipse(ax.transData, width=0.1, height=0.15, angle=0.,
                         loc='lower left', pad=0.5, borderpad=0.4,
                         frameon=True)

    ax.add_artist(ae)
Пример #2
0
def draw_ellipse(obj,ax, bmin, bmaj, regrid=False):
    from mpl_toolkits.axes_grid1.anchored_artists import AnchoredEllipse
    """
    Draw an ellipse of width=0.1, height=0.15 in data coordinates
    """

    bpa = obj.bpa.value
    if regrid:
        bpa = 0
    try:
        ae = AnchoredEllipse(ax.transData, width=bmaj.value, height=bmin.value,
                                angle=-bpa, loc='lower left', pad=0.3, borderpad=0.3,
                                frameon=True,color='lightskyblue')
    except:
        ae = AnchoredEllipse(ax.transData, width=bmaj.value, height=bmin.value,
                                angle=-bpa, loc='lower left', pad=0.3, borderpad=0.3,
                                frameon=True)

    ax.add_artist(ae)
Пример #3
0
def add_beam(ax, image, loc=3, pad=0.2, borderpad=0.2, frameon=True):
    ''' image is an imgutils.Image object.
        It will check if image has a beam and add it to ax as a parasite ax'''
    beam = image.get_beam()
    if beam is not None and isinstance(beam, imgutils.GaussianBeam):
        ae = AnchoredEllipse(ax.transData,
                             width=beam.bmin,
                             height=beam.bmaj,
                             angle=np.degrees(beam.bpa),
                             loc=loc,
                             pad=pad,
                             borderpad=borderpad,
                             frameon=frameon)
        ae.ellipse.set_facecolor(orange)
        ae.ellipse.set_edgecolor(black)
        ae.ellipse.set_alpha(0.8)
        ax.add_artist(ae)
Пример #4
0
def _show_beam(fitsfile, fig, kwargs):
    beam = kwargs.get('beam', True)
    beam_kwargs = easy_aplpy.settings.beam_kwargs
    if isinstance(beam, str):
        beam = beam.replace('bottom',
                            'lower')  # AnchoredEllipse needs specfic words
        beam = beam.replace('top',
                            'upper')  # AnchoredEllipse needs specfic words
        beam_kwargs['loc'] = beam

    imtype = kwargs.get('imtype')
    if not (beam is None) and not (imtype == 'pv'):

        if not aplpy.version.version == '1.1.1':  # APLpy 1.1.1 fails to plot beams
            fig.add_beam()
            fig.beam.show()
            fig.beam.set_corner(beam_kwargs['loc'])
            fig.beam.set_frame(easy_aplpy.settings.beam_kwargs['frame'])
            fig.beam.set_color(easy_aplpy.settings.beam_kwargs['facecolor'])
        else:  # plot beam manually for APLpy 1.1.1
            # translate location codes
            # keep human-readable strings in settings and translate only here to numbers
            # see https://matplotlib.org/3.1.1/api/offsetbox_api.html#matplotlib.offsetbox.AnchoredOffsetbox
            codes = {
                'center': 10,
                'center left': 6,
                'center right': 7,
                'lower center': 8,
                'lower left': 3,
                'lower right': 4,
                'right': 5,
                'upper center': 9,
                'upper left': 2,
                'upper right': 1
            }

            try:
                if not (np.abs(fits.getheader(fitsfile)['cdelt1']) -
                        np.abs(fits.getheader(fitsfile)['cdelt2']) < 1e-6):
                    raise ValueError(
                        "Pixels are not square (within 1e-6 degrees). Cannot plot beam."
                    )
                bmaj = fits.getheader(fitsfile)[
                    'bmaj']  # in degrees by default
                bmin = fits.getheader(fitsfile)[
                    'bmin']  # in degrees by default
                bpa = fits.getheader(fitsfile)['bpa']  # in degrees by default
                deg_per_pix = np.abs(fits.getheader(fitsfile)['cdelt1'])

                from mpl_toolkits.axes_grid1.anchored_artists import AnchoredEllipse
                ae = AnchoredEllipse(
                    fig._ax1.transData,
                    width=bmin /
                    deg_per_pix,  # in axis coordinates which is pixels!
                    height=bmaj /
                    deg_per_pix,  # in axis coordinates which is pixels!
                    angle=bpa,
                    loc=codes[beam_kwargs['loc']],
                    pad=beam_kwargs[
                        'pad'],  # padding betwen beam object and axis
                    borderpad=beam_kwargs[
                        'borderpad'],  # padding within beam object
                    frameon=beam_kwargs['frame']  # show a frame around beam?
                )
                fig._ax1.add_artist(ae)
                ae.ellipse.set_fill(beam_kwargs['filled'])
                ae.ellipse.set_linewidth(beam_kwargs['linewidth'])
                ae.ellipse.set_facecolor(beam_kwargs['facecolor'])
                ae.ellipse.set_edgecolor(beam_kwargs['edgecolor'])
                ae.ellipse.set_zorder(
                    beam_kwargs['zorder'])  # ensure beam is always on top

            except:
                raise Warning(
                    "Could not determine beam from header. Will not plot beam."
                )
Пример #5
0
    def show(self, major='BMAJ', minor='BMIN', angle='BPA',
             corner='bottom left', frame=False, borderpad=0.4, pad=0.5,
             **kwargs):
        """
        Display the beam shape and size for the primary image.

        By default, this method will search for the BMAJ, BMIN, and BPA
        keywords in the FITS header to set the major and minor axes and the
        position angle on the sky.

        Parameters
        ----------

        major : float, quantity or unit, optional
            Major axis of the beam in degrees or an angular quantity (overrides
            BMAJ if present)

        minor : float, quantity or unit, optional
            Minor axis of the beam in degrees or an angular quantity (overrides
            BMIN if present)

        angle : float, quantity or unit, optional
            Position angle of the beam on the sky in degrees or an angular
            quantity (overrides BPA if present) in the anticlockwise direction.

        corner : int, optional
            The beam location. Acceptable values are 'left', 'right',
            'top', 'bottom', 'top left', 'top right', 'bottom left'
            (default), and 'bottom right'.

        frame : str, optional
            Whether to display a frame behind the beam (default is False)

        kwargs
            Additional arguments are passed to the matplotlib Ellipse class.
            See the matplotlib documentation for more details.
        """

        if isinstance(major, str):
            major = self._header[major]

        if isinstance(minor, str):
            minor = self._header[minor]

        if isinstance(angle, str):
            angle = self._header[angle]

        if isinstance(major, u.Quantity):
            major = major.to(u.degree).value
        elif isinstance(major, u.Unit):
            major = major.to(u.degree)

        if isinstance(minor, u.Quantity):
            minor = minor.to(u.degree).value
        elif isinstance(minor, u.Unit):
            minor = minor.to(u.degree)

        if isinstance(angle, u.Quantity):
            angle = angle.to(u.degree).value
        elif isinstance(angle, u.Unit):
            angle = angle.to(u.degree)

#        if self._wcs.is_celestial:
        if True:
            pix_scale = proj_plane_pixel_scales(self._wcs)
            sx = pix_scale[self._dimensions[0]]
            sy = pix_scale[self._dimensions[1]]
            degrees_per_pixel = np.sqrt(sx * sy)
        else:
            raise ValueError("Cannot show beam when WCS is not celestial")

        self._base_settings['minor'] = minor
        self._base_settings['major'] = major
        self._base_settings['angle'] = angle
        self._base_settings['corner'] = corner
        self._base_settings['frame'] = frame
        self._base_settings['borderpad'] = borderpad
        self._base_settings['pad'] = pad

        minor /= degrees_per_pixel
        major /= degrees_per_pixel

        try:
            self._beam.remove()
        except Exception:
            pass

        if isinstance(corner, str):
            corner = corners[corner]

        self._beam = AnchoredEllipse(self._ax.transData, width=minor,
                                     height=major, angle=angle, loc=corner,
                                     pad=pad, borderpad=borderpad,
                                     frameon=frame)

        self._ax.add_artist(self._beam)

        self.set(**kwargs)
Пример #6
0
class Beam(object):

    def __init__(self, parent):

        # Retrieve info from parent figure
        self._figure = parent._figure
        self._header = parent._header
        self._ax = parent.ax
        self._wcs = parent._wcs
        self._dimensions = [parent.x, parent.y]

        # Initialize settings
        self._base_settings = {}
        self._beam_settings = {}

    # LAYOUT

    @auto_refresh
    def show(self, major='BMAJ', minor='BMIN', angle='BPA',
             corner='bottom left', frame=False, borderpad=0.4, pad=0.5,
             **kwargs):
        """
        Display the beam shape and size for the primary image.

        By default, this method will search for the BMAJ, BMIN, and BPA
        keywords in the FITS header to set the major and minor axes and the
        position angle on the sky.

        Parameters
        ----------

        major : float, quantity or unit, optional
            Major axis of the beam in degrees or an angular quantity (overrides
            BMAJ if present)

        minor : float, quantity or unit, optional
            Minor axis of the beam in degrees or an angular quantity (overrides
            BMIN if present)

        angle : float, quantity or unit, optional
            Position angle of the beam on the sky in degrees or an angular
            quantity (overrides BPA if present) in the anticlockwise direction.

        corner : int, optional
            The beam location. Acceptable values are 'left', 'right',
            'top', 'bottom', 'top left', 'top right', 'bottom left'
            (default), and 'bottom right'.

        frame : str, optional
            Whether to display a frame behind the beam (default is False)

        kwargs
            Additional arguments are passed to the matplotlib Ellipse class.
            See the matplotlib documentation for more details.
        """

        if isinstance(major, str):
            major = self._header[major]

        if isinstance(minor, str):
            minor = self._header[minor]

        if isinstance(angle, str):
            angle = self._header[angle]

        if isinstance(major, u.Quantity):
            major = major.to(u.degree).value
        elif isinstance(major, u.Unit):
            major = major.to(u.degree)

        if isinstance(minor, u.Quantity):
            minor = minor.to(u.degree).value
        elif isinstance(minor, u.Unit):
            minor = minor.to(u.degree)

        if isinstance(angle, u.Quantity):
            angle = angle.to(u.degree).value
        elif isinstance(angle, u.Unit):
            angle = angle.to(u.degree)

#        if self._wcs.is_celestial:
        if True:
            pix_scale = proj_plane_pixel_scales(self._wcs)
            sx = pix_scale[self._dimensions[0]]
            sy = pix_scale[self._dimensions[1]]
            degrees_per_pixel = np.sqrt(sx * sy)
        else:
            raise ValueError("Cannot show beam when WCS is not celestial")

        self._base_settings['minor'] = minor
        self._base_settings['major'] = major
        self._base_settings['angle'] = angle
        self._base_settings['corner'] = corner
        self._base_settings['frame'] = frame
        self._base_settings['borderpad'] = borderpad
        self._base_settings['pad'] = pad

        minor /= degrees_per_pixel
        major /= degrees_per_pixel

        try:
            self._beam.remove()
        except Exception:
            pass

        if isinstance(corner, str):
            corner = corners[corner]

        self._beam = AnchoredEllipse(self._ax.transData, width=minor,
                                     height=major, angle=angle, loc=corner,
                                     pad=pad, borderpad=borderpad,
                                     frameon=frame)

        self._ax.add_artist(self._beam)

        self.set(**kwargs)

    @auto_refresh
    def _remove(self):
        self._beam.remove()

    @auto_refresh
    def hide(self):
        """
        Hide the beam
        """
        try:
            self._beam.remove()
        except Exception:
            pass

    @auto_refresh
    def set_major(self, major):
        """
        Set the major axis of the beam, in degrees.
        """
        self._base_settings['major'] = major
        self.show(**self._base_settings)
        self.set(**self._beam_settings)

    @auto_refresh
    def set_minor(self, minor):
        """
        Set the minor axis of the beam, in degrees.
        """
        self._base_settings['minor'] = minor
        self.show(**self._base_settings)
        self.set(**self._beam_settings)

    @auto_refresh
    def set_angle(self, angle):
        """
        Set the position angle of the beam on the sky, in degrees.
        """
        self._base_settings['angle'] = angle
        self.show(**self._base_settings)
        self.set(**self._beam_settings)

    @auto_refresh
    def set_corner(self, corner):
        """
        Set the beam location.

        Acceptable values are 'left', 'right', 'top', 'bottom', 'top left',
        'top right', 'bottom left' (default), and 'bottom right'.
        """
        self._base_settings['corner'] = corner
        self.show(**self._base_settings)
        self.set(**self._beam_settings)

    @auto_refresh
    def set_frame(self, frame):
        """
        Set whether to display a frame around the beam.
        """
        self._base_settings['frame'] = frame
        self.show(**self._base_settings)
        self.set(**self._beam_settings)

    @auto_refresh
    def set_borderpad(self, borderpad):
        """
        Set the amount of padding within the beam object, relative to the
        canvas size.
        """
        self._base_settings['borderpad'] = borderpad
        self.show(**self._base_settings)
        self.set(**self._beam_settings)

    @auto_refresh
    def set_pad(self, pad):
        """
        Set the amount of padding between the beam object and the image
        corner/edge, relative to the canvas size.
        """
        self._base_settings['pad'] = pad
        self.show(**self._base_settings)
        self.set(**self._beam_settings)

    # APPEARANCE

    @auto_refresh
    def set_alpha(self, alpha):
        """
        Set the alpha value (transparency).

        This should be a floating point value between 0 and 1.
        """
        self.set(alpha=alpha)

    @auto_refresh
    def set_color(self, color):
        """
        Set the beam color.
        """
        self.set(color=color)

    @auto_refresh
    def set_edgecolor(self, edgecolor):
        """
        Set the color for the edge of the beam.
        """
        self.set(edgecolor=edgecolor)

    @auto_refresh
    def set_facecolor(self, facecolor):
        """
        Set the color for the interior of the beam.
        """
        self.set(facecolor=facecolor)

    @auto_refresh
    def set_linestyle(self, linestyle):
        """
        Set the line style for the edge of the beam.

        This should be one of 'solid', 'dashed', 'dashdot', or 'dotted'.
        """
        self.set(linestyle=linestyle)

    @auto_refresh
    def set_linewidth(self, linewidth):
        """
        Set the line width for the edge of the beam, in points.
        """
        self.set(linewidth=linewidth)

    @auto_refresh
    def set_hatch(self, hatch):
        """
        Set the hatch pattern.

        This should be one of '/', '\', '|', '-', '+', 'x', 'o', 'O', '.', or
        '*'.
        """
        self.set(hatch=hatch)

    @auto_refresh
    def set(self, **kwargs):
        """
        Modify the beam properties. All arguments are passed to the matplotlib
        Ellipse class. See the matplotlib documentation for more details.
        """
        for kwarg in kwargs:
            self._beam_settings[kwarg] = kwargs[kwarg]
        self._beam.ellipse.set(**kwargs)
Пример #7
0
    def show(self, major='BMAJ', minor='BMIN', angle='BPA',
             corner='bottom left', frame=False, borderpad=0.4, pad=0.5,
             **kwargs):
        """
        Display the beam shape and size for the primary image.

        By default, this method will search for the BMAJ, BMIN, and BPA
        keywords in the FITS header to set the major and minor axes and the
        position angle on the sky.

        Parameters
        ----------

        major : float, quantity or unit, optional
            Major axis of the beam in degrees or an angular quantity (overrides
            BMAJ if present)

        minor : float, quantity or unit, optional
            Minor axis of the beam in degrees or an angular quantity (overrides
            BMIN if present)

        angle : float, quantity or unit, optional
            Position angle of the beam on the sky in degrees or an angular
            quantity (overrides BPA if present) in the anticlockwise direction.

        corner : int, optional
            The beam location. Acceptable values are 'left', 'right',
            'top', 'bottom', 'top left', 'top right', 'bottom left'
            (default), and 'bottom right'.

        frame : str, optional
            Whether to display a frame behind the beam (default is False)

        kwargs
            Additional arguments are passed to the matplotlib Ellipse class.
            See the matplotlib documentation for more details.
        """

        if isinstance(major, str):
            major = self._header[major]

        if isinstance(minor, str):
            minor = self._header[minor]

        if isinstance(angle, str):
            angle = self._header[angle]

        if isinstance(major, u.Quantity):
            major = major.to(u.degree).value
        elif isinstance(major, u.Unit):
            major = major.to(u.degree)

        if isinstance(minor, u.Quantity):
            minor = minor.to(u.degree).value
        elif isinstance(minor, u.Unit):
            minor = minor.to(u.degree)

        if isinstance(angle, u.Quantity):
            angle = angle.to(u.degree).value
        elif isinstance(angle, u.Unit):
            angle = angle.to(u.degree)

        if self._wcs.is_celestial:
            pix_scale = proj_plane_pixel_scales(self._wcs)
            sx = pix_scale[self._dimensions[0]]
            sy = pix_scale[self._dimensions[1]]
            degrees_per_pixel = np.sqrt(sx * sy)
        else:
            raise ValueError("Cannot show beam when WCS is not celestial")

        self._base_settings['minor'] = minor
        self._base_settings['major'] = major
        self._base_settings['angle'] = angle
        self._base_settings['corner'] = corner
        self._base_settings['frame'] = frame
        self._base_settings['borderpad'] = borderpad
        self._base_settings['pad'] = pad

        minor /= degrees_per_pixel
        major /= degrees_per_pixel

        try:
            self._beam.remove()
        except Exception:
            pass

        if isinstance(corner, str):
            corner = corners[corner]

        self._beam = AnchoredEllipse(self._ax.transData, width=minor,
                                     height=major, angle=angle, loc=corner,
                                     pad=pad, borderpad=borderpad,
                                     frameon=frame)

        self._ax.add_artist(self._beam)

        self.set(**kwargs)
Пример #8
0
class Beam(object):

    def __init__(self, parent):

        # Retrieve info from parent figure
        self._figure = parent._figure
        self._header = parent._header
        self._ax = parent.ax
        self._wcs = parent._wcs
        self._dimensions = [parent.x, parent.y]

        # Initialize settings
        self._base_settings = {}
        self._beam_settings = {}

    # LAYOUT

    @auto_refresh
    def show(self, major='BMAJ', minor='BMIN', angle='BPA',
             corner='bottom left', frame=False, borderpad=0.4, pad=0.5,
             **kwargs):
        """
        Display the beam shape and size for the primary image.

        By default, this method will search for the BMAJ, BMIN, and BPA
        keywords in the FITS header to set the major and minor axes and the
        position angle on the sky.

        Parameters
        ----------

        major : float, quantity or unit, optional
            Major axis of the beam in degrees or an angular quantity (overrides
            BMAJ if present)

        minor : float, quantity or unit, optional
            Minor axis of the beam in degrees or an angular quantity (overrides
            BMIN if present)

        angle : float, quantity or unit, optional
            Position angle of the beam on the sky in degrees or an angular
            quantity (overrides BPA if present) in the anticlockwise direction.

        corner : int, optional
            The beam location. Acceptable values are 'left', 'right',
            'top', 'bottom', 'top left', 'top right', 'bottom left'
            (default), and 'bottom right'.

        frame : str, optional
            Whether to display a frame behind the beam (default is False)

        kwargs
            Additional arguments are passed to the matplotlib Ellipse class.
            See the matplotlib documentation for more details.
        """

        if isinstance(major, str):
            major = self._header[major]

        if isinstance(minor, str):
            minor = self._header[minor]

        if isinstance(angle, str):
            angle = self._header[angle]

        if isinstance(major, u.Quantity):
            major = major.to(u.degree).value
        elif isinstance(major, u.Unit):
            major = major.to(u.degree)

        if isinstance(minor, u.Quantity):
            minor = minor.to(u.degree).value
        elif isinstance(minor, u.Unit):
            minor = minor.to(u.degree)

        if isinstance(angle, u.Quantity):
            angle = angle.to(u.degree).value
        elif isinstance(angle, u.Unit):
            angle = angle.to(u.degree)

        if self._wcs.is_celestial:
            pix_scale = proj_plane_pixel_scales(self._wcs)
            sx = pix_scale[self._dimensions[0]]
            sy = pix_scale[self._dimensions[1]]
            degrees_per_pixel = np.sqrt(sx * sy)
        else:
            raise ValueError("Cannot show beam when WCS is not celestial")

        self._base_settings['minor'] = minor
        self._base_settings['major'] = major
        self._base_settings['angle'] = angle
        self._base_settings['corner'] = corner
        self._base_settings['frame'] = frame
        self._base_settings['borderpad'] = borderpad
        self._base_settings['pad'] = pad

        minor /= degrees_per_pixel
        major /= degrees_per_pixel

        try:
            self._beam.remove()
        except Exception:
            pass

        if isinstance(corner, str):
            corner = corners[corner]

        self._beam = AnchoredEllipse(self._ax.transData, width=minor,
                                     height=major, angle=angle, loc=corner,
                                     pad=pad, borderpad=borderpad,
                                     frameon=frame)

        self._ax.add_artist(self._beam)

        self.set(**kwargs)

    @auto_refresh
    def _remove(self):
        self._beam.remove()

    @auto_refresh
    def hide(self):
        """
        Hide the beam
        """
        try:
            self._beam.remove()
        except Exception:
            pass

    @auto_refresh
    def set_major(self, major):
        """
        Set the major axis of the beam, in degrees.
        """
        self._base_settings['major'] = major
        self.show(**self._base_settings)
        self.set(**self._beam_settings)

    @auto_refresh
    def set_minor(self, minor):
        """
        Set the minor axis of the beam, in degrees.
        """
        self._base_settings['minor'] = minor
        self.show(**self._base_settings)
        self.set(**self._beam_settings)

    @auto_refresh
    def set_angle(self, angle):
        """
        Set the position angle of the beam on the sky, in degrees.
        """
        self._base_settings['angle'] = angle
        self.show(**self._base_settings)
        self.set(**self._beam_settings)

    @auto_refresh
    def set_corner(self, corner):
        """
        Set the beam location.

        Acceptable values are 'left', 'right', 'top', 'bottom', 'top left',
        'top right', 'bottom left' (default), and 'bottom right'.
        """
        self._base_settings['corner'] = corner
        self.show(**self._base_settings)
        self.set(**self._beam_settings)

    @auto_refresh
    def set_frame(self, frame):
        """
        Set whether to display a frame around the beam.
        """
        self._base_settings['frame'] = frame
        self.show(**self._base_settings)
        self.set(**self._beam_settings)

    @auto_refresh
    def set_borderpad(self, borderpad):
        """
        Set the amount of padding within the beam object, relative to the
        canvas size.
        """
        self._base_settings['borderpad'] = borderpad
        self.show(**self._base_settings)
        self.set(**self._beam_settings)

    @auto_refresh
    def set_pad(self, pad):
        """
        Set the amount of padding between the beam object and the image
        corner/edge, relative to the canvas size.
        """
        self._base_settings['pad'] = pad
        self.show(**self._base_settings)
        self.set(**self._beam_settings)

    # APPEARANCE

    @auto_refresh
    def set_alpha(self, alpha):
        """
        Set the alpha value (transparency).

        This should be a floating point value between 0 and 1.
        """
        self.set(alpha=alpha)

    @auto_refresh
    def set_color(self, color):
        """
        Set the beam color.
        """
        self.set(color=color)

    @auto_refresh
    def set_edgecolor(self, edgecolor):
        """
        Set the color for the edge of the beam.
        """
        self.set(edgecolor=edgecolor)

    @auto_refresh
    def set_facecolor(self, facecolor):
        """
        Set the color for the interior of the beam.
        """
        self.set(facecolor=facecolor)

    @auto_refresh
    def set_linestyle(self, linestyle):
        """
        Set the line style for the edge of the beam.

        This should be one of 'solid', 'dashed', 'dashdot', or 'dotted'.
        """
        self.set(linestyle=linestyle)

    @auto_refresh
    def set_linewidth(self, linewidth):
        """
        Set the line width for the edge of the beam, in points.
        """
        self.set(linewidth=linewidth)

    @auto_refresh
    def set_hatch(self, hatch):
        """
        Set the hatch pattern.

        This should be one of '/', '\', '|', '-', '+', 'x', 'o', 'O', '.', or
        '*'.
        """
        self.set(hatch=hatch)

    @auto_refresh
    def set(self, **kwargs):
        """
        Modify the beam properties. All arguments are passed to the matplotlib
        Ellipse class. See the matplotlib documentation for more details.
        """
        for kwarg in kwargs:
            self._beam_settings[kwarg] = kwargs[kwarg]
        self._beam.ellipse.set(**kwargs)