a
    ߙfbHf                    @   s,  d dl Z d dlZd dlZd dlZd dlZd dlZd dlmZ d dl	m
Z d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d d	lmZ d
dlmZ d
dlmZ d
dlmZmZm Z  d
dl!m"Z"m#Z# d
dl$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z* d
dl+m,Z,m-Z-m.Z. ddgZ/G dd deZ0G dd deZ1dS )    N)override__dir__)units)c)	MixinInfo)ShapedLikeNDArray)QTable)Time)AstropyUserWarning   )DistanceAngle)BaseCoordinateFrameframe_transform_graphGenericFrame)ICRSSkyOffsetFrame)RadialDifferentialSphericalDifferentialSphericalRepresentationUnitSphericalCosLatDifferentialUnitSphericalDifferentialUnitSphericalRepresentation)_get_frame_class_get_frame_without_data_parse_coordinate_dataSkyCoordSkyCoordInfoc                       s\   e Zd ZdZedgZdZedd Ze	dd Z
e	dd	 Z fd
dZdddZ  ZS )r   z
    Container for meta information like name, description, format.  This is
    required when the object is used as a mixin column within a table, but can
    be used as a general way to store meta information.
    unitFc                 C   s(   | j j}dd |jD }d||S )Nc                 S   s   g | ]}d | d qS )z{0.z.value:} ).0Zcompnamer   r   Alib/python3.9/site-packages/astropy/coordinates/sky_coordinate.py
<listcomp>.       z/SkyCoordInfo.default_format.<locals>.<listcomp>,)info
_repr_data
componentsjoinformat)val	repr_dataZformatsr   r   r!   default_format+   s
    zSkyCoordInfo.default_formatc                    s$   | j  d fdd jD }|S )Nr$   c                 3   s"   | ]}t t |jpd V  qdS )NoneN)strgetattrr   )r    compr+   r   r!   	<genexpr>5   s   z$SkyCoordInfo.unit.<locals>.<genexpr>)r&   r(   r'   )selfr   r   r1   r!   r   2   s
    zSkyCoordInfo.unitc                 C   sT   | j d u rd S | j }t|jtr@t|jtr@|j|jjdd}n|j|jdd}|S )NT)Zin_frame_units)	_parent
issubclassrepresentation_typer   
isinstancedatar   represent_as	__class__)r3   scr+   r   r   r!   r&   9   s    

zSkyCoordInfo._repr_datac                    s   | j }t|j}t|jtr(|d d }|jjd}|d urt|d}t|t	rd|dd  }nt|t
tfr~|d d }|| |tj  t |}|j |d< |jj|d< |S )Ns   r6   frame)r4   listrepresentation_component_namesr7   r8   r   differentialsget"get_representation_component_namesr   r   r   extendr   frame_attributeskeyssuper_represent_as_dictr6   Zget_namer?   name)r3   r;   attrsZdiffZ
diff_attrsoutr:   r   r!   rI   G   s&    


zSkyCoordInfo._represent_as_dictwarnNc                 C   s   |  |||d}|d }tj|tjd}|| }|dd D ]D}	z|	d |d< W q< ty~ }
 ztd|
W Y d}
~
q<d}
~
0 0 q<dD ]}||v rt|j|||  q|S )a  
        Return a new SkyCoord instance which is consistent with the input
        SkyCoord objects ``skycoords`` and has ``length`` rows.  Being
        "consistent" is defined as being able to set an item from one to each of
        the rest without any exception being raised.

        This is intended for creating a new SkyCoord instance whose elements can
        be set in-place for table operations like join or vstack.  This is used
        when a SkyCoord object is used as a mixin column in an astropy Table.

        The data values are not predictable and it is expected that the consumer
        of the object will fill in all values.

        Parameters
        ----------
        skycoords : list
            List of input SkyCoord objects
        length : int
            Length of the output skycoord object
        metadata_conflicts : str ('warn'|'error'|'silent')
            How to handle metadata conflicts
        name : str
            Output name (sets output skycoord.info.name)

        Returns
        -------
        skycoord : SkyCoord (or subclass)
            Instance of this class consistent with ``skycoords``

        )metadescriptionr   )Zdtyper
   Nz!Input skycoords are inconsistent.)rJ   rO   rP   )Zmerge_cols_attributesnpZzerosZint64	Exception
ValueErrorsetattrr%   )r3   Z	skycoordsZlengthZmetadata_conflictsrJ   rK   Z	skycoord0ZindexesrL   Zskycoorderrattrr   r   r!   new_likeg   s     
"zSkyCoordInfo.new_like)rN   N)__name__
__module____qualname____doc__setZattrs_from_parentZ_supports_indexingstaticmethodr,   propertyr   r&   rI   rW   __classcell__r   r   rM   r!   r   "   s   



 c                       s  e Zd ZdZe ZddddZedd Zedd	 Z	e	j
d
d	 Z	edd Zej
dd Zedd Zdd Zdd Z fddZdd Zd_ddZdd Zd`ddZdad d!Zd"d# Zd$d% Z fd&d'Z fd(d)Zed*d+ Zd,d- Zdbd/d0Zd1d2 Zd3d4 Zd5d6 Zd7d8 Z d9d: Z!d;d< Z"d=d> Z#dcd@dAZ$dddBdCZ%dDdE Z&dFdG Z'dHdI Z(dedJdKZ)dfdNdOZ*dgdQdRZ+e,dhdSdTZ-didUdVZ.djdXdYZ/e,dZd[ Z0e,dkd]d^Z1  Z2S )lr   aT  High-level object providing a flexible interface for celestial coordinate
    representation, manipulation, and transformation between systems.

    The `SkyCoord` class accepts a wide variety of inputs for initialization. At
    a minimum these must provide one or more celestial coordinate values with
    unambiguous units.  Inputs may be scalars or lists/tuples/arrays, yielding
    scalar or array coordinates (can be checked via ``SkyCoord.isscalar``).
    Typically one also specifies the coordinate frame, though this is not
    required. The general pattern for spherical representations is::

      SkyCoord(COORD, [FRAME], keyword_args ...)
      SkyCoord(LON, LAT, [FRAME], keyword_args ...)
      SkyCoord(LON, LAT, [DISTANCE], frame=FRAME, unit=UNIT, keyword_args ...)
      SkyCoord([FRAME], <lon_attr>=LON, <lat_attr>=LAT, keyword_args ...)

    It is also possible to input coordinate values in other representations
    such as cartesian or cylindrical.  In this case one includes the keyword
    argument ``representation_type='cartesian'`` (for example) along with data
    in ``x``, ``y``, and ``z``.

    See also: https://docs.astropy.org/en/stable/coordinates/

    Examples
    --------
    The examples below illustrate common ways of initializing a `SkyCoord`
    object.  For a complete description of the allowed syntax see the
    full coordinates documentation.  First some imports::

      >>> from astropy.coordinates import SkyCoord  # High-level coordinates
      >>> from astropy.coordinates import ICRS, Galactic, FK4, FK5  # Low-level frames
      >>> from astropy.coordinates import Angle, Latitude, Longitude  # Angles
      >>> import astropy.units as u

    The coordinate values and frame specification can now be provided using
    positional and keyword arguments::

      >>> c = SkyCoord(10, 20, unit="deg")  # defaults to ICRS frame
      >>> c = SkyCoord([1, 2, 3], [-30, 45, 8], frame="icrs", unit="deg")  # 3 coords

      >>> coords = ["1:12:43.2 +31:12:43", "1 12 43.2 +31 12 43"]
      >>> c = SkyCoord(coords, frame=FK4, unit=(u.hourangle, u.deg), obstime="J1992.21")

      >>> c = SkyCoord("1h12m43.2s +1d12m43s", frame=Galactic)  # Units from string
      >>> c = SkyCoord(frame="galactic", l="1h12m43.2s", b="+1d12m43s")

      >>> ra = Longitude([1, 2, 3], unit=u.deg)  # Could also use Angle
      >>> dec = np.array([4.5, 5.2, 6.3]) * u.deg  # Astropy Quantity
      >>> c = SkyCoord(ra, dec, frame='icrs')
      >>> c = SkyCoord(frame=ICRS, ra=ra, dec=dec, obstime='2001-01-02T12:34:56')

      >>> c = FK4(1 * u.deg, 2 * u.deg)  # Uses defaults for obstime, equinox
      >>> c = SkyCoord(c, obstime='J2010.11', equinox='B1965')  # Override defaults

      >>> c = SkyCoord(w=0, u=1, v=2, unit='kpc', frame='galactic',
      ...              representation_type='cartesian')

      >>> c = SkyCoord([ICRS(ra=1*u.deg, dec=2*u.deg), ICRS(ra=3*u.deg, dec=4*u.deg)])

    Velocity components (proper motions or radial velocities) can also be
    provided in a similar manner::

      >>> c = SkyCoord(ra=1*u.deg, dec=2*u.deg, radial_velocity=10*u.km/u.s)

      >>> c = SkyCoord(ra=1*u.deg, dec=2*u.deg, pm_ra_cosdec=2*u.mas/u.yr, pm_dec=1*u.mas/u.yr)

    As shown, the frame can be a `~astropy.coordinates.BaseCoordinateFrame`
    class or the corresponding string alias.  The frame classes that are built in
    to astropy are `ICRS`, `FK5`, `FK4`, `FK4NoETerms`, and `Galactic`.
    The string aliases are simply lower-case versions of the class name, and
    allow for creating a `SkyCoord` object and transforming frames without
    explicitly importing the frame classes.

    Parameters
    ----------
    frame : `~astropy.coordinates.BaseCoordinateFrame` class or string, optional
        Type of coordinate frame this `SkyCoord` should represent. Defaults to
        to ICRS if not given or given as None.
    unit : `~astropy.units.Unit`, string, or tuple of :class:`~astropy.units.Unit` or str, optional
        Units for supplied coordinate values.
        If only one unit is supplied then it applies to all values.
        Note that passing only one unit might lead to unit conversion errors
        if the coordinate values are expected to have mixed physical meanings
        (e.g., angles and distances).
    obstime : time-like, optional
        Time(s) of observation.
    equinox : time-like, optional
        Coordinate frame equinox time.
    representation_type : str or Representation class
        Specifies the representation, e.g. 'spherical', 'cartesian', or
        'cylindrical'.  This affects the positional args and other keyword args
        which must correspond to the given representation.
    copy : bool, optional
        If `True` (default), a copy of any coordinate data is made.  This
        argument can only be passed in as a keyword argument.
    **keyword_args
        Other keyword arguments as applicable for user-defined coordinate frames.
        Common options include:

        ra, dec : angle-like, optional
            RA and Dec for frames where ``ra`` and ``dec`` are keys in the
            frame's ``representation_component_names``, including `ICRS`,
            `FK5`, `FK4`, and `FK4NoETerms`.
        pm_ra_cosdec, pm_dec  : `~astropy.units.Quantity` ['angular speed'], optional
            Proper motion components, in angle per time units.
        l, b : angle-like, optional
            Galactic ``l`` and ``b`` for for frames where ``l`` and ``b`` are
            keys in the frame's ``representation_component_names``, including
            the `Galactic` frame.
        pm_l_cosb, pm_b : `~astropy.units.Quantity` ['angular speed'], optional
            Proper motion components in the `Galactic` frame, in angle per time
            units.
        x, y, z : float or `~astropy.units.Quantity` ['length'], optional
            Cartesian coordinates values
        u, v, w : float or `~astropy.units.Quantity` ['length'], optional
            Cartesian coordinates values for the Galactic frame.
        radial_velocity : `~astropy.units.Quantity` ['speed'], optional
            The component of the velocity along the line-of-sight (i.e., the
            radial direction), in velocity units.
    Tcopyc                O   s,  t  | _t|dkrt|dkrt|d ttfr|d }t|trx|j| _|j| _| jD ]}t| |t|| qZ|j	}|j
std|r| | _n|| _nt||\}}t|}t|f i |||\}}	}
|D ]}t| |||  q|
d ur|
| _||	 |f d|i|| _| jj
s(tdd S )Nr
   r   zJCannot initialize from a coordinate frame instance without coordinate datara   z%Cannot create a SkyCoord without data)r\   _extra_frameattr_nameslenr7   r   r   r%   rT   r/   r?   has_datarS   ra   _sky_coord_framer   r@   r   update)r3   ra   argskwargsZcoordsZ	attr_name	frame_clsframe_kwargsZskycoord_kwargsr'   r%   rV   r   r   r!   __init__  s:    




zSkyCoord.__init__c                 C   s   | j S N)re   r3   r   r   r!   r?   `  s    zSkyCoord.framec                 C   s   | j jS rl   r?   r6   rm   r   r   r!   r6   d  s    zSkyCoord.representation_typec                 C   s   || j _d S rl   rn   r3   valuer   r   r!   r6   h  s    c                 C   s   | j jS rl   r?   representationrm   r   r   r!   rr   m  s    zSkyCoord.representationc                 C   s   || j _d S rl   rq   ro   r   r   r!   rr   q  s    c                 C   s   | j jS rl   )r?   shaperm   r   r   r!   rs   u  s    zSkyCoord.shapec                 C   sV   t |tstS | j|jB D ].}| jt| |t||std| dq| j|jkS )zEquality operator for SkyCoord

        This implements strict equality and requires that the frames are
        equivalent, extra frame attributes are equivalent, and that the
        representation data are exactly equal.
        z'cannot compare: extra frame attribute 'zQ' is not equivalent (perhaps compare the frames directly to avoid this exception))	r7   r   NotImplementedrb   r?   _frameattr_equivr/   rS   re   )r3   rp   rV   r   r   r!   __eq__y  s    
zSkyCoord.__eq__c                 C   s   t | |kS rl   )rQ   Zlogical_notro   r   r   r!   __ne__  s    zSkyCoord.__ne__c                    s    fdd}t  | j}| jjg R i |_| j |_| jD ]N}t| |}t|ddrp||}ndksdkrt|}t|d| | qLd| j	v r| j
|_
|S )	au  Create a new instance, applying a method to the underlying data.

        In typical usage, the method is any of the shape-changing methods for
        `~numpy.ndarray` (``reshape``, ``swapaxes``, etc.), as well as those
        picking particular elements (``__getitem__``, ``take``, etc.), which
        are all defined in `~astropy.utils.shapes.ShapedLikeNDArray`. It will be
        applied to the underlying arrays in the representation (e.g., ``x``,
        ``y``, and ``z`` for `~astropy.coordinates.CartesianRepresentation`),
        as well as to any frame attributes that have a shape, with the results
        used to create a new instance.

        Internally, it is also used to apply functions to the above parts
        (in particular, `~numpy.broadcast_to`).

        Parameters
        ----------
        method : str or callable
            If str, it is the name of a method that is applied to the internal
            ``components``. If callable, the function is applied.
        *args
            Any positional arguments for ``method``.
        **kwargs : dict
            Any keyword arguments for ``method``.
        c                    sX   t | tr"| jg R i S tr@| g R i S t|  i S d S rl   )r7   r   _applycallabler/   )rp   rg   rh   methodr   r!   apply_method  s
    
z%SkyCoord._apply.<locals>.apply_methodrs   r   ra   Zflatten_r%   )rH   __new__r:   re   rx   rb   ra   r/   rT   __dict__r%   )r3   r{   rg   rh   r|   newrV   rp   rM   rz   r!   rx     s$    






zSkyCoord._applyc                 C   st   | j |j ur(td| j j d|j j | j|jB D ].}| jt| |t||s4td| dq4|j| j|< dS )a$  Implement self[item] = value for SkyCoord

        The right hand ``value`` must be strictly consistent with self:
        - Identical class
        - Equivalent frames
        - Identical representation_types
        - Identical representation differentials keys
        - Identical frame attributes
        - Identical "extra" frame attributes (e.g. obstime for an ICRS coord)

        With these caveats the setitem ends up as effectively a setitem on
        the representation data.

          self.frame.data[item] = value.frame.data
        z(can only set from object of same class: z vs. z
attribute z is not equivalentN)	r:   	TypeErrorrX   rb   r?   ru   r/   rS   re   )r3   itemrp   rV   r   r   r!   __setitem__  s    zSkyCoord.__setitem__r   c                 C   s   zt |}W n ty(   tdY n0 |dkr:td| jsRtd| jjt|t	| krvt
d|t	| |dk rt	| | }|jrt	|nd}| jjj| gt	| | | jjd}| d| |d|< ||||| < | |d ||| d< |S )	a5  
        Insert coordinate values before the given indices in the object and
        return a new Frame object.

        The values to be inserted must conform to the rules for in-place setting
        of ``SkyCoord`` objects.

        The API signature matches the ``np.insert`` API, but is more limited.
        The specification of insert index ``obj`` must be a single integer,
        and the ``axis`` must be ``0`` for simple insertion before the index.

        Parameters
        ----------
        obj : int
            Integer index before which ``values`` is inserted.
        values : array-like
            Value(s) to insert.  If the type of ``values`` is different
            from that of quantity, ``values`` is converted to the matching type.
        axis : int, optional
            Axis along which to insert ``values``.  Default is 0, which is the
            only allowed value and will insert a row.

        Returns
        -------
        out : `~astropy.coordinates.SkyCoord` instance
            New coordinate object with inserted value(s)

        zobj arg must be an integerr   zaxis must be 0z#cannot insert into scalar {} objectz1index {} is out of bounds for axis 0 with size {}r
   )rJ   N)operatorindexr   rS   rs   r)   r:   rX   absrc   
IndexErrorr%   rW   rJ   )r3   objvaluesZaxisZidx0Zn_valuesrL   r   r   r!   insert  s,    "zSkyCoord.insertc                 C   s"   t |trt|n|}| j|S )a  
        Determines if this coordinate frame can be transformed to another
        given frame.

        Parameters
        ----------
        new_frame : frame class, frame object, or str
            The proposed frame to transform into.

        Returns
        -------
        transformable : bool or str
            `True` if this can be transformed to ``new_frame``, `False` if
            not, or the string 'same' if ``new_frame`` is the same system as
            this object but no transformation is defined.

        Notes
        -----
        A return value of 'same' means the transformation will work, but it will
        just give back a copy of this object.  The intended usage is::

            if coord.is_transformable_to(some_unknown_frame):
                coord2 = coord.transform_to(some_unknown_frame)

        This will work even if ``some_unknown_frame``  turns out to be the same
        frame class as ``coord``.  This is intended for cases where the frame
        is the same regardless of the frame attributes (e.g. ICRS), but be
        aware that it *might* also indicate that someone forgot to define the
        transformation between two objects of the same frame class but with
        different attributes.
        )r7   r.   r   r?   is_transformable_to)r3   Z	new_framer   r   r!   r   *  s    !zSkyCoord.is_transformable_toc                 C   sT  ddl m} i }zt| }W n ty0   Y n0 t|trB|j}t|tr|j}t	j
D ]h}t| |d}t||d}|dur|r||s|||< qX|dur| |s|||< qX|durX|||< qXntdt	| jj|}	|	du r|d| jj|t|}
|	| j|
}t| t| @ D ]}|| q$|dd | j|fi |S )a  Transform this coordinate to a new frame.

        The precise frame transformed to depends on ``merge_attributes``.
        If `False`, the destination frame is used exactly as passed in.
        But this is often not quite what one wants.  E.g., suppose one wants to
        transform an ICRS coordinate that has an obstime attribute to FK4; in
        this case, one likely would want to use this information. Thus, the
        default for ``merge_attributes`` is `True`, in which the precedence is
        as follows: (1) explicitly set (i.e., non-default) values in the
        destination frame; (2) explicitly set values in the source; (3) default
        value in the destination frame.

        Note that in either case, any explicitly set attributes on the source
        `SkyCoord` that are not part of the destination frame's definition are
        kept (stored on the resulting `SkyCoord`), and thus one can round-trip
        (e.g., from FK4 to ICRS to FK4 without losing obstime).

        Parameters
        ----------
        frame : str, `BaseCoordinateFrame` class or instance, or `SkyCoord` instance
            The frame to transform this coordinate into.  If a `SkyCoord`, the
            underlying frame is extracted, and all other information ignored.
        merge_attributes : bool, optional
            Whether the default attributes in the destination frame are allowed
            to be overridden by explicitly set attributes in the source
            (see note above; default: `True`).

        Returns
        -------
        coord : `SkyCoord`
            A new object with this coordinate represented in the `frame` frame.

        Raises
        ------
        ValueError
            If there is no possible transformation route.

        r   )ConvertErrorNz:Transform `frame` must be a frame name, class, or instancezCannot transform from {} to {}origin)Zastropy.coordinates.errorsr   r   rR   r7   r   r?   r   r:   r   rF   r/   Zis_frame_attr_defaultrS   Zget_transformr)   r   r\   get_frame_attr_namesrG   pop)r3   r?   merge_attributesr   rj   Znew_frame_clsrV   Zself_valZ	frame_valZtransZgeneric_frameZ	new_coordr   r   r!   transform_toO  sN    '






zSkyCoord.transform_toNc                    sn  |du r|du s |dur(|dur(t dd jjjvr>t dd jjv rRtd|durl jdu rlt d j}|du r|}n*|du rtd}d}|| }n|| }|}|j}|j} j	
tt}|jd }d	}z|jtjt }W n tjy   d
}d}Y n0 z|jtjtj }	W n tjy<   d
}	Y n0 t|jj|jj|jtjtj |jtjtj ||	|j|j|j|j
}
|rd}nt |
d tj!> d}t"tj#|
d tjd	dtj#|
d tjd	dtj#|
d tjtj d	dtj#|
d tjtj d	d|tj#|
d tjtj d	dtd} fdd j$D }||d<  j%|fi |& j} j'|_'|S )a  
        Compute the position of the source represented by this coordinate object
        to a new time using the velocities stored in this object and assuming
        linear space motion (including relativistic corrections). This is
        sometimes referred to as an "epoch transformation."

        The initial time before the evolution is taken from the ``obstime``
        attribute of this coordinate.  Note that this method currently does not
        support evolving coordinates where the *frame* has an ``obstime`` frame
        attribute, so the ``obstime`` is only used for storing the before and
        after times, not actually as an attribute of the frame. Alternatively,
        if ``dt`` is given, an ``obstime`` need not be provided at all.

        Parameters
        ----------
        new_obstime : `~astropy.time.Time`, optional
            The time at which to evolve the position to. Requires that the
            ``obstime`` attribute be present on this frame.
        dt : `~astropy.units.Quantity`, `~astropy.time.TimeDelta`, optional
            An amount of time to evolve the position of the source. Cannot be
            given at the same time as ``new_obstime``.

        Returns
        -------
        new_coord : `SkyCoord`
            A new coordinate object with the evolved location of this coordinate
            at the new time.  ``obstime`` will be set on this object to the new
            time only if ``self`` also has ``obstime``.
        Nz<You must specify one of `new_obstime` or `dt`, but not both.r=   z7SkyCoord requires velocity data to evolve the position.obstimezUpdating the coordinates in a frame with explicit time dependence is currently not supported. If you would like this functionality, please open an issue on github:
https://github.com/astropy/astropyzzThis object has no associated `obstime`. apply_space_motion() must receive a time difference, `dt`, and not a new obstime.ZJ2000Fg        T   )parallaxr   r`   r
   r>         )ZraZdecZpm_raZpm_decdistanceZradial_velocitydifferential_typec                    s   i | ]}|t  |qS r   r/   )r    Zattrnmrm   r   r!   
<dictcomp>+  s   z/SkyCoord.apply_space_motion.<locals>.<dictcomp>)(rS   r?   r8   rB   rF   NotImplementedErrorr   r   Ztdbicrsr9   r   r   r   Zto_valueuZ	arcsecondr   UnitConversionErrorZ
d_distanceZkmr=   erfaZpmsafelonZradianlatd_lonZyrd_latZjd1Zjd2r   Zarcsecr   ZQuantityrb   r:   r   r   )r3   Znew_obstimeZdtt1t2ZicrsrepZicrsvelZparallax_zeroZplxrvZstarpmZnew_distanceZicrs2Zfrattrsresultr   rm   r!   apply_space_motion  st    





zSkyCoord.apply_space_motionc                 C   s&   | j j|kp$t| j jto$|| j jv S )zO
        Returns whether a string is one of the aliases for the frame.
        )r?   rJ   r7   r@   )r3   stringr   r   r!   _is_name6  s    zSkyCoord._is_namec                 C   s   d| j v r| |r| S |tjv rL|| j v r<t| j|S t| d| dS |dsnt| j	|rnt| j	|S t
|}|dur| j|r| |S td| jj|dS )z
        Overrides getattr to return coordinates that this can be transformed
        to, based on the alias attr in the primary transform graph.
        re   r}   Nz!'{}' object has no attribute '{}')r   r   r   rF   r?   r   r/   
startswithhasattrre   lookup_namer   r   AttributeErrorr)   r:   rX   r3   rV   ri   r   r   r!   __getattr__=  s    




zSkyCoord.__getattr__c                    s   d| j v rz| |r$td| d|dsLt| j|rLt| j|| d S t|}|d urz| j	
|rztd| d|tjv rt d| | tj| |  |  j|hO  _nt || d S Nre   'z' is immutabler}   )r   r   r   r   r   re   rT   r   r   r?   r   rF   rH   __setattr____get__rb   )r3   rV   r*   ri   rM   r   r!   r   ]  s    



zSkyCoord.__setattr__c                    s   d| j v rx| |r$td| d|dsJt| j|rJt| j| d S t|}|d urx| j	
|rxtd| d|tjv rt d|  |  j|h8  _nt | d S r   )r   r   r   r   r   re   delattrr   r   r?   r   rF   rH   __delattr__rb   r   rM   r   r!   r   x  s    



zSkyCoord.__delattr__c                 C   sf   t  }t D ]$}t|}| j|r|| q|t dd t| jD  |tj	
  |S )z
        Override the builtin `dir` behavior to include:
        - Transforms available by aliases
        - Attribute / methods of the underlying self.frame object
        c                 s   s   | ]}| d s|V  qdS )r}   N)r   )r    rV   r   r   r!   r2     r#   z#SkyCoord.__dir__.<locals>.<genexpr>)r\   r   Z	get_namesr   r?   r   addrf   dirrF   rG   )r3   Z
dir_valuesrJ   ri   r   r   r!   __dir__  s    	
zSkyCoord.__dir__c                 C   sP   | j j}| jj j}| j }|r(d| }| j }|r>d| }djf i t S )Nz: z%<{clsnm} ({coonm}{frameattrs}){data}>)r:   rX   r?   Z_frame_attrs_reprZ
_data_reprr)   locals)r3   ZclsnmZcoonmZ
frameattrsr8   r   r   r!   __repr__  s    


zSkyCoord.__repr__decimalc           
      K   sd  | j t}tjddtjdddddtjidtjidtjddtjdddd}i }i }||v r||| d  ||| d	  ntd
d| || || t	
|jjr|jjf i |d |jjf i | }nrg }t|j |j D ]4\}}	||jf i |d |	jf i | g7 }qt|jdkr`t	||j}|S )ae  
        A string representation of the coordinates.

        The default styles definitions are::

          'decimal': 'lat': {'decimal': True, 'unit': "deg"}
                     'lon': {'decimal': True, 'unit': "deg"}
          'dms': 'lat': {'unit': "deg"}
                 'lon': {'unit': "deg"}
          'hmsdms': 'lat': {'alwayssign': True, 'pad': True, 'unit': "deg"}
                    'lon': {'pad': True, 'unit': "hour"}

        See :meth:`~astropy.coordinates.Angle.to_string` for details and
        keyword arguments (the two angles forming the coordinates are are
        both :class:`~astropy.coordinates.Angle` instances). Keyword
        arguments have precedence over the style defaults and are passed
        to :meth:`~astropy.coordinates.Angle.to_string`.

        Parameters
        ----------
        style : {'hmsdms', 'dms', 'decimal'}
            The formatting specification to use. These encode the three most
            common ways to represent coordinates. The default is `decimal`.
        **kwargs
            Keyword args passed to :meth:`~astropy.coordinates.Angle.to_string`.
        T)r   pad)r   r   Z
alwayssign)lonargslatargsr   )r   r   )ZhmsdmsZdmsr   r   r   z#Invalid style.  Valid options are: r$    r
   )r?   r9   r   r   Zhourdegreerf   rS   r(   rQ   Zisscalarr   rp   	to_stringr   zipZravelrc   rs   ZarrayZreshape)
r3   Zstylerh   Z	sph_coordZstylesr   r   Zcoord_stringZlonangleZlatangler   r   r!   r     sF    




zSkyCoord.to_stringc                 C   s`   | j  }i }i }| D ]8\}}t|dddd t| fkrJ|||< q|||< qt||dS )ad  
        Convert this |SkyCoord| to a |QTable|.

        Any attributes that have the same length as the |SkyCoord| will be
        converted to columns of the |QTable|. All other attributes will be
        recorded as metadata.

        Returns
        -------
        `~astropy.table.QTable`
            A |QTable| containing the data of this |SkyCoord|.

        Examples
        --------
        >>> sc = SkyCoord(ra=[40, 70]*u.deg, dec=[0, -20]*u.deg,
        ...               obstime=Time([2000, 2010], format='jyear'))
        >>> t =  sc.to_table()
        >>> t
        <QTable length=2>
           ra     dec   obstime
          deg     deg
        float64 float64   Time
        ------- ------- -------
           40.0     0.0  2000.0
           70.0   -20.0  2010.0
        >>> t.meta
        {'representation_type': 'spherical', 'frame': 'icrs'}
        rs   r   Nr
   )rO   )r%   rI   itemsr/   rc   r   )r3   Zself_as_dictZ	tabledatametadatakeyrp   r   r   r!   to_table  s    


zSkyCoord.to_tablec                 C   sn   t |tr| j|S t |trb|jj| jjkr4dS tjD ]"}tt	| |t	||s: dS q:dS t
ddS )a  
        Checks if this object's frame as the same as that of the ``other``
        object.

        To be the same frame, two objects must be the same frame class and have
        the same frame attributes. For two `SkyCoord` objects, *all* of the
        frame attributes have to match, not just those relevant for the object's
        frame.

        Parameters
        ----------
        other : SkyCoord or BaseCoordinateFrame
            The other object to check.

        Returns
        -------
        isequiv : bool
            True if the frames are the same, False if not.

        Raises
        ------
        TypeError
            If ``other`` isn't a `SkyCoord` or a `BaseCoordinateFrame` or subclass.
        FTzBTried to do is_equivalent_frame on something that isn't frame-likeN)r7   r   r?   is_equivalent_framer   rJ   r   rF   ru   r/   r   )r3   otherZfattrnmr   r   r!   r     s    


zSkyCoord.is_equivalent_framec           
      C   s   ddl m} ddlm} | |sjz,t|tr6ddini }|j| fi |}W n tyh   tdY n0 | j	j
}| j	j}|j	j
}|j	j}|||||}	||	tjdS )a  
        Computes on-sky separation between this coordinate and another.

        .. note::

            If the ``other`` coordinate object is in a different frame, it is
            first transformed to the frame of this object. This can lead to
            unintuitive behavior if not accounted for. Particularly of note is
            that ``self.separation(other)`` and ``other.separation(self)`` may
            not give the same answer in this case.

        For more on how to use this (and related) functionality, see the
        examples in :doc:`astropy:/coordinates/matchsep`.

        Parameters
        ----------
        other : `~astropy.coordinates.SkyCoord` or `~astropy.coordinates.BaseCoordinateFrame`
            The coordinate to get the separation to.

        Returns
        -------
        sep : `~astropy.coordinates.Angle`
            The on-sky separation between this and the ``other`` coordinate.

        Notes
        -----
        The separation is calculated using the Vincenty formula, which
        is stable at all locations, including poles and antipodes [1]_.

        .. [1] https://en.wikipedia.org/wiki/Great-circle_distance

        r
   r   )angular_separationr   FKCan only get separation to another SkyCoord or a coordinate frame with data)r   ) r   angle_utilitiesr   r   r7   r   r   r   	sphericalr   r   r   r   )
r3   r   r   r   rh   Zlon1Zlat1Zlon2Zlat2sepr   r   r!   
separationI  s    !
zSkyCoord.separationc                 C   s   |  |sRz,t|trddini }|j| fi |}W n tyP   tdY n0 t| jjtrht	dt|jjtr~t	d| j
 }|j
 }t||  S )a  
        Computes three dimensional separation between this coordinate
        and another.

        For more on how to use this (and related) functionality, see the
        examples in :doc:`astropy:/coordinates/matchsep`.

        Parameters
        ----------
        other : `~astropy.coordinates.SkyCoord` or `~astropy.coordinates.BaseCoordinateFrame`
            The coordinate to get the separation to.

        Returns
        -------
        sep : `~astropy.coordinates.Distance`
            The real-space distance between these two coordinates.

        Raises
        ------
        ValueError
            If this or the other coordinate do not have distances.
        r   Fr   zCThis object does not have a distance; cannot compute 3d separation.zHThe other object does not have a distance; cannot compute 3d separation.)r   r7   r   r   r   r5   r8   r:   r   rS   	cartesianwithout_differentialsr   norm)r3   r   rh   Zc1Zc2r   r   r!   separation_3d~  s    


zSkyCoord.separation_3dc                 C   sH   |  |std|  }||}|jjt}|jjt}||fS )a  
        Computes angular offsets to go *from* this coordinate *to* another.

        Parameters
        ----------
        tocoord : `~astropy.coordinates.BaseCoordinateFrame`
            The coordinate to find the offset to.

        Returns
        -------
        lon_offset : `~astropy.coordinates.Angle`
            The angular offset in the longitude direction. The definition of
            "longitude" depends on this coordinate's frame (e.g., RA for
            equatorial coordinates).
        lat_offset : `~astropy.coordinates.Angle`
            The angular offset in the latitude direction. The definition of
            "latitude" depends on this coordinate's frame (e.g., Dec for
            equatorial coordinates).

        Raises
        ------
        ValueError
            If the ``tocoord`` is not in the same frame as this one. This is
            different from the behavior of the `separation`/`separation_3d`
            methods because the offset components depend critically on the
            specific choice of frame.

        Notes
        -----
        This uses the sky offset frame machinery, and hence will produce a new
        sky offset frame if one does not already exist for this object's frame
        class.

        See Also
        --------
        separation :
            for the *total* angular offset (not broken out into components).
        position_angle :
            for the direction of the offset.

        z?Tried to use spherical_offsets_to with two non-matching frames!)	r   rS   skyoffset_framer   r   r   Zviewr   r   )r3   ZtocoordZaframeZacoordZdlonZdlatr   r   r!   spherical_offsets_to  s    *

zSkyCoord.spherical_offsets_toc                 C   s   |  t||| jd| S )a  
        Computes the coordinate that is a specified pair of angular offsets away
        from this coordinate.

        Parameters
        ----------
        d_lon : angle-like
            The angular offset in the longitude direction. The definition of
            "longitude" depends on this coordinate's frame (e.g., RA for
            equatorial coordinates).
        d_lat : angle-like
            The angular offset in the latitude direction. The definition of
            "latitude" depends on this coordinate's frame (e.g., Dec for
            equatorial coordinates).

        Returns
        -------
        newcoord : `~astropy.coordinates.SkyCoord`
            The coordinates for the location that corresponds to offsetting by
            ``d_lat`` in the latitude direction and ``d_lon`` in the longitude
            direction.

        Notes
        -----
        This internally uses `~astropy.coordinates.SkyOffsetFrame` to do the
        transformation. For a more complete set of transform offsets, use
        `~astropy.coordinates.SkyOffsetFrame` or `~astropy.wcs.WCS` manually.
        This specific method can be reproduced by doing
        ``SkyCoord(SkyOffsetFrame(d_lon, d_lat, origin=self.frame).transform_to(self))``.

        See Also
        --------
        spherical_offsets_to : compute the angular offsets to another coordinate
        directional_offset_by : offset a coordinate by an angle in a direction
        )r   )r:   r   r?   r   )r3   r   r   r   r   r!   spherical_offsets_by  s    $zSkyCoord.spherical_offsets_byc                 C   sJ   ddl m} | tj}| tj}|j||||d\}}t||| jdS )aN  
        Computes coordinates at the given offset from this coordinate.

        Parameters
        ----------
        position_angle : `~astropy.coordinates.Angle`
            position_angle of offset
        separation : `~astropy.coordinates.Angle`
            offset angular separation

        Returns
        -------
        newpoints : `~astropy.coordinates.SkyCoord`
            The coordinates for the location that corresponds to offsetting by
            the given `position_angle` and `separation`.

        Notes
        -----
        Returned SkyCoord frame retains only the frame attributes that are for
        the resulting frame type.  (e.g. if the input frame is
        `~astropy.coordinates.ICRS`, an ``equinox`` value will be retained, but
        an ``obstime`` will not.)

        For a more complete set of transform offsets, use `~astropy.wcs.WCS`.
        `~astropy.coordinates.SkyCoord.skyoffset_frame()` can also be used to
        create a spherical frame with (lat=0, lon=0) at a reference point,
        approximating an xy cartesian system for small offsets. This method
        is distinct in that it is accurate on the sphere.

        See Also
        --------
        position_angle : inverse operation for the ``position_angle`` component
        separation : inverse operation for the ``separation`` component

        r
   r   )r   r   Zposangr   )r?   )	r   r   r9   r   r   r   Z	offset_byr   r?   )r3   position_angler   r   slatslonZnewlonZnewlatr   r   r!   directional_offset_by  s    $
zSkyCoord.directional_offset_byr
   c                 C   s<   ddl m} t|ttfr |js(td|| ||dd}|S )a  
        Finds the nearest on-sky matches of this coordinate in a set of
        catalog coordinates.

        For more on how to use this (and related) functionality, see the
        examples in :doc:`astropy:/coordinates/matchsep`.

        Parameters
        ----------
        catalogcoord : `~astropy.coordinates.SkyCoord` or `~astropy.coordinates.BaseCoordinateFrame`
            The base catalog in which to search for matches. Typically this
            will be a coordinate object that is an array (i.e.,
            ``catalogcoord.isscalar == False``)
        nthneighbor : int, optional
            Which closest neighbor to search for.  Typically ``1`` is
            desired here, as that is correct for matching one set of
            coordinates to another. The next likely use case is ``2``,
            for matching a coordinate catalog against *itself* (``1``
            is inappropriate because each point will find itself as the
            closest match).

        Returns
        -------
        idx : int array
            Indices into ``catalogcoord`` to get the matched points for
            each of this object's coordinates. Shape matches this
            object.
        sep2d : `~astropy.coordinates.Angle`
            The on-sky separation between the closest match for each
            element in this object in ``catalogcoord``. Shape matches
            this object.
        dist3d : `~astropy.units.Quantity` ['length']
            The 3D distance between the closest match for each element
            in this object in ``catalogcoord``. Shape matches this
            object. Unless both this and ``catalogcoord`` have associated
            distances, this quantity assumes that all sources are at a
            distance of 1 (dimensionless).

        Notes
        -----
        This method requires `SciPy <https://www.scipy.org/>`_ to be
        installed or it will fail.

        See Also
        --------
        astropy.coordinates.match_coordinates_sky
        SkyCoord.match_to_catalog_3d
        r
   )match_coordinates_skyr   _kdtree_skynthneighborstorekdtree)matchingr   r7   r   r   rd   r   )r3   catalogcoordr   r   resr   r   r!   match_to_catalog_sky2  s    1zSkyCoord.match_to_catalog_skyc                 C   s<   ddl m} t|ttfr |js(td|| ||dd}|S )a  
        Finds the nearest 3-dimensional matches of this coordinate to a set
        of catalog coordinates.

        This finds the 3-dimensional closest neighbor, which is only different
        from the on-sky distance if ``distance`` is set in this object or the
        ``catalogcoord`` object.

        For more on how to use this (and related) functionality, see the
        examples in :doc:`astropy:/coordinates/matchsep`.

        Parameters
        ----------
        catalogcoord : `~astropy.coordinates.SkyCoord` or `~astropy.coordinates.BaseCoordinateFrame`
            The base catalog in which to search for matches. Typically this
            will be a coordinate object that is an array (i.e.,
            ``catalogcoord.isscalar == False``)
        nthneighbor : int, optional
            Which closest neighbor to search for.  Typically ``1`` is
            desired here, as that is correct for matching one set of
            coordinates to another.  The next likely use case is
            ``2``, for matching a coordinate catalog against *itself*
            (``1`` is inappropriate because each point will find
            itself as the closest match).

        Returns
        -------
        idx : int array
            Indices into ``catalogcoord`` to get the matched points for
            each of this object's coordinates. Shape matches this
            object.
        sep2d : `~astropy.coordinates.Angle`
            The on-sky separation between the closest match for each
            element in this object in ``catalogcoord``. Shape matches
            this object.
        dist3d : `~astropy.units.Quantity` ['length']
            The 3D distance between the closest match for each element
            in this object in ``catalogcoord``. Shape matches this
            object.

        Notes
        -----
        This method requires `SciPy <https://www.scipy.org/>`_ to be
        installed or it will fail.

        See Also
        --------
        astropy.coordinates.match_coordinates_3d
        SkyCoord.match_to_catalog_sky
        r
   )match_coordinates_3dr   
_kdtree_3dr   )r   r   r7   r   r   rd   r   )r3   r   r   r   r   r   r   r!   match_to_catalog_3do  s    3zSkyCoord.match_to_catalog_3dc                 C   s   ddl m} ||| |ddS )a  
        Searches for all coordinates in this object around a supplied set of
        points within a given on-sky separation.

        This is intended for use on `~astropy.coordinates.SkyCoord` objects
        with coordinate arrays, rather than a scalar coordinate.  For a scalar
        coordinate, it is better to use
        `~astropy.coordinates.SkyCoord.separation`.

        For more on how to use this (and related) functionality, see the
        examples in :doc:`astropy:/coordinates/matchsep`.

        Parameters
        ----------
        searcharoundcoords : coordinate-like
            The coordinates to search around to try to find matching points in
            this `SkyCoord`. This should be an object with array coordinates,
            not a scalar coordinate object.
        seplimit : `~astropy.units.Quantity` ['angle']
            The on-sky separation to search within.

        Returns
        -------
        idxsearcharound : int array
            Indices into ``searcharoundcoords`` that match the
            corresponding elements of ``idxself``. Shape matches
            ``idxself``.
        idxself : int array
            Indices into ``self`` that match the
            corresponding elements of ``idxsearcharound``. Shape matches
            ``idxsearcharound``.
        sep2d : `~astropy.coordinates.Angle`
            The on-sky separation between the coordinates. Shape matches
            ``idxsearcharound`` and ``idxself``.
        dist3d : `~astropy.units.Quantity` ['length']
            The 3D distance between the coordinates. Shape matches
            ``idxsearcharound`` and ``idxself``.

        Notes
        -----
        This method requires `SciPy <https://www.scipy.org/>`_ to be
        installed or it will fail.

        In the current implementation, the return values are always sorted in
        the same order as the ``searcharoundcoords`` (so ``idxsearcharound`` is
        in ascending order).  This is considered an implementation detail,
        though, so it could change in a future release.

        See Also
        --------
        astropy.coordinates.search_around_sky
        SkyCoord.search_around_3d
        r
   )search_around_skyr   r   )r   r   )r3   searcharoundcoordsZseplimitr   r   r   r!   r     s    6zSkyCoord.search_around_skyc                 C   s   ddl m} ||| |ddS )a  
        Searches for all coordinates in this object around a supplied set of
        points within a given 3D radius.

        This is intended for use on `~astropy.coordinates.SkyCoord` objects
        with coordinate arrays, rather than a scalar coordinate.  For a scalar
        coordinate, it is better to use
        `~astropy.coordinates.SkyCoord.separation_3d`.

        For more on how to use this (and related) functionality, see the
        examples in :doc:`astropy:/coordinates/matchsep`.

        Parameters
        ----------
        searcharoundcoords : `~astropy.coordinates.SkyCoord` or `~astropy.coordinates.BaseCoordinateFrame`
            The coordinates to search around to try to find matching points in
            this `SkyCoord`. This should be an object with array coordinates,
            not a scalar coordinate object.
        distlimit : `~astropy.units.Quantity` ['length']
            The physical radius to search within.

        Returns
        -------
        idxsearcharound : int array
            Indices into ``searcharoundcoords`` that match the
            corresponding elements of ``idxself``. Shape matches
            ``idxself``.
        idxself : int array
            Indices into ``self`` that match the
            corresponding elements of ``idxsearcharound``. Shape matches
            ``idxsearcharound``.
        sep2d : `~astropy.coordinates.Angle`
            The on-sky separation between the coordinates. Shape matches
            ``idxsearcharound`` and ``idxself``.
        dist3d : `~astropy.units.Quantity` ['length']
            The 3D distance between the coordinates. Shape matches
            ``idxsearcharound`` and ``idxself``.

        Notes
        -----
        This method requires `SciPy <https://www.scipy.org/>`_ to be
        installed or it will fail.

        In the current implementation, the return values are always sorted in
        the same order as the ``searcharoundcoords`` (so ``idxsearcharound`` is
        in ascending order).  This is considered an implementation detail,
        though, so it could change in a future release.

        See Also
        --------
        astropy.coordinates.search_around_3d
        SkyCoord.search_around_sky
        r
   )search_around_3dr   r   )r   r   )r3   r   Z	distlimitr   r   r   r!   r     s    6zSkyCoord.search_around_3dc                 C   s   ddl m} | |sDz|j| dd}W n tyB   tdY n0 | tj}| tj}|tj}|tj}|	||||S )a  
        Computes the on-sky position angle (East of North) between this
        `SkyCoord` and another.

        Parameters
        ----------
        other : `SkyCoord`
            The other coordinate to compute the position angle to.  It is
            treated as the "head" of the vector of the position angle.

        Returns
        -------
        pa : `~astropy.coordinates.Angle`
            The (positive) position angle of the vector pointing from ``self``
            to ``other``.  If either ``self`` or ``other`` contain arrays, this
            will be an array following the appropriate `numpy` broadcasting
            rules.

        Examples
        --------
        >>> c1 = SkyCoord(0*u.deg, 0*u.deg)
        >>> c2 = SkyCoord(1*u.deg, 0*u.deg)
        >>> c1.position_angle(c2).degree
        90.0
        >>> c3 = SkyCoord(1*u.deg, 1*u.deg)
        >>> c1.position_angle(c3).degree  # doctest: +FLOAT_CMP
        44.995636455344844
        r
   r   F)r   zOCan only get position_angle to another SkyCoord or a coordinate frame with data)
r   r   r   r   r   r9   r   r   r   r   )r3   r   r   r   r   ZolatZolonr   r   r!   r   %  s    
zSkyCoord.position_anglec                 C   s   t | |dS )a  
        Returns the sky offset frame with this `SkyCoord` at the origin.

        Returns
        -------
        astrframe : `~astropy.coordinates.SkyOffsetFrame`
            A sky offset frame of the same type as this `SkyCoord` (e.g., if
            this object has an ICRS coordinate, the resulting frame is
            SkyOffsetICRS, with the origin set to this object)
        rotation : angle-like
            The final rotation of the frame about the ``origin``. The sign of
            the rotation is the left-hand rule. That is, an object at a
            particular position angle in the un-rotated system will be sent to
            the positive latitude (z) direction in the final frame.
        )r   rotation)r   )r3   r   r   r   r!   r   R  s    zSkyCoord.skyoffset_frameFiauc                    sH   ddl m}  fdd jD }t  j fi |}||||S )an  
        Determines the constellation(s) of the coordinates this `SkyCoord`
        contains.

        Parameters
        ----------
        short_name : bool
            If True, the returned names are the IAU-sanctioned abbreviated
            names.  Otherwise, full names for the constellations are used.
        constellation_list : str
            The set of constellations to use.  Currently only ``'iau'`` is
            supported, meaning the 88 "modern" constellations endorsed by the IAU.

        Returns
        -------
        constellation : str or string array
            If this is a scalar coordinate, returns the name of the
            constellation.  If it is an array `SkyCoord`, it returns an array of
            names.

        Notes
        -----
        To determine which constellation a point on the sky is in, this first
        precesses to B1875, and then uses the Delporte boundaries of the 88
        modern constellations, as tabulated by
        `Roman 1987 <http://cdsarc.u-strasbg.fr/viz-bin/Cat?VI/42>`_.

        See Also
        --------
        astropy.coordinates.get_constellation
        r
   )get_constellationc                    s   i | ]}|t  |qS r   r   )r    Znmrm   r   r!   r     s   z.SkyCoord.get_constellation.<locals>.<dictcomp>)Zfuncsr   rb   r   Zrealize_framer8   r   )r3   Z
short_nameZconstellation_listr   Zextra_frameattrsZnovelr   rm   r!   r   d  s     
zSkyCoord.get_constellationallc                 C   s   ddl m} || |||dS )a  
        Convert this coordinate to pixel coordinates using a `~astropy.wcs.WCS`
        object.

        Parameters
        ----------
        wcs : `~astropy.wcs.WCS`
            The WCS to use for convert
        origin : int
            Whether to return 0 or 1-based pixel coordinates.
        mode : 'all' or 'wcs'
            Whether to do the transformation including distortions (``'all'``) or
            only including only the core WCS transformation (``'wcs'``).

        Returns
        -------
        xp, yp : `numpy.ndarray`
            The pixel coordinates

        See Also
        --------
        astropy.wcs.utils.skycoord_to_pixel : the implementation of this method
        r   )skycoord_to_pixel)wcsr   mode)astropy.wcs.utilsr   )r3   r   r   r   r   r   r   r!   to_pixel  s    zSkyCoord.to_pixelc                 C   s    ddl m} ||||||| dS )a  
        Create a new `SkyCoord` from pixel coordinates using an
        `~astropy.wcs.WCS` object.

        Parameters
        ----------
        xp, yp : float or ndarray
            The coordinates to convert.
        wcs : `~astropy.wcs.WCS`
            The WCS to use for convert
        origin : int
            Whether to return 0 or 1-based pixel coordinates.
        mode : 'all' or 'wcs'
            Whether to do the transformation including distortions (``'all'``) or
            only including only the core WCS transformation (``'wcs'``).

        Returns
        -------
        coord : `~astropy.coordinates.SkyCoord`
            A new object with sky coordinates corresponding to the input ``xp``
            and ``yp``.

        See Also
        --------
        to_pixel : to do the inverse operation
        astropy.wcs.utils.pixel_to_skycoord : the implementation of this method
        r   )pixel_to_skycoord)r   r   r   cls)r   r   )r   ZxpZypr   r   r   r   r   r   r!   
from_pixel  s    zSkyCoord.from_pixelc           	   	   K   s   |dur|j \}}n
|j\}}ddl}| V |d z| j|fi |\}}W n  tyt   Y W d   dS 0 W d   n1 s0    Y  ||k |dk@ ||k @ |dk@ S )a  
        Determines if the SkyCoord is contained in the given wcs footprint.

        Parameters
        ----------
        wcs : `~astropy.wcs.WCS`
            The coordinate to check if it is within the wcs coordinate.
        image : array
            Optional.  The image associated with the wcs object that the cooordinate
            is being checked against. If not given the naxis keywords will be used
            to determine if the coordinate falls within the wcs footprint.
        **kwargs
            Additional arguments to pass to `~astropy.coordinates.SkyCoord.to_pixel`

        Returns
        -------
        response : bool
            True means the WCS footprint contains the coordinate, False means it does not.
        Nr   ignoreF)rs   Z_naxiswarningscatch_warningssimplefilterr   rR   )	r3   r   Zimagerh   ZymaxZxmaxr   xyr   r   r!   contained_by  s    


2zSkyCoord.contained_bybarycentricc                 C   sl  ddl m} t|dd}|du rZ| jdurB| j}|durXtdqt|durP|}qttdn| jdusl|durttd| }|du r| j}|du rtdnB| jdur| jjj	r| 
|}n"| jdu rd	| jj	v rtd
t |d|\}}|dkr|}	n0|dkr|d|d }
||
 }	ntd|||\}}|jj}| }| jjtu rX|}n|| }|| }||  }|dkrZ|	| t }dtd| d   }||}|d||  d|t   }|j	rJzR| jj}|j	d	  t }| }||  }|d|| d||  9 }W n" tjyH   tdt Y n0 |d }|t S ||	| S dS )a  
        Compute the correction required to convert a radial velocity at a given
        time and place on the Earth's Surface to a barycentric or heliocentric
        velocity.

        Parameters
        ----------
        kind : str
            The kind of velocity correction.  Must be 'barycentric' or
            'heliocentric'.
        obstime : `~astropy.time.Time` or None, optional
            The time at which to compute the correction.  If `None`, the
            ``obstime`` frame attribute on the `SkyCoord` will be used.
        location : `~astropy.coordinates.EarthLocation` or None, optional
            The observer location at which to compute the correction.  If
            `None`, the  ``location`` frame attribute on the passed-in
            ``obstime`` will be used, and if that is None, the ``location``
            frame attribute on the `SkyCoord` will be used.

        Raises
        ------
        ValueError
            If either ``obstime`` or ``location`` are passed in (not ``None``)
            when the frame attribute is already set on this `SkyCoord`.
        TypeError
            If ``obstime`` or ``location`` aren't provided, either as arguments
            or as frame attributes.

        Returns
        -------
        vcorr : `~astropy.units.Quantity` ['speed']
            The  correction with a positive sign.  I.e., *add* this
            to an observed radial velocity to get the barycentric (or
            heliocentric) velocity. If m/s precision or better is needed,
            see the notes below.

        Notes
        -----
        The barycentric correction is calculated to higher precision than the
        heliocentric correction and includes additional physics (e.g time dilation).
        Use barycentric corrections if m/s precision is required.

        The algorithm here is sufficient to perform corrections at the mm/s level, but
        care is needed in application. The barycentric correction returned uses the optical
        approximation v = z * c. Strictly speaking, the barycentric correction is
        multiplicative and should be applied as::

          >>> from astropy.time import Time
          >>> from astropy.coordinates import SkyCoord, EarthLocation
          >>> from astropy.constants import c
          >>> t = Time(56370.5, format='mjd', scale='utc')
          >>> loc = EarthLocation('149d33m00.5s','-30d18m46.385s',236.87*u.m)
          >>> sc = SkyCoord(1*u.deg, 2*u.deg)
          >>> vcorr = sc.radial_velocity_correction(kind='barycentric', obstime=t, location=loc)  # doctest: +REMOTE_DATA
          >>> rv = rv + vcorr + rv * vcorr / c  # doctest: +SKIP

        Also note that this method returns the correction velocity in the so-called
        *optical convention*::

          >>> vcorr = zb * c  # doctest: +SKIP

        where ``zb`` is the barycentric correction redshift as defined in section 3
        of Wright & Eastman (2014). The application formula given above follows from their
        equation (11) under assumption that the radial velocity ``rv`` has also been defined
        using the same optical convention. Note, this can be regarded as a matter of
        velocity definition and does not by itself imply any loss of accuracy, provided
        sufficient care has been taken during interpretation of the results. If you need
        the barycentric correction expressed as the full relativistic velocity (e.g., to provide
        it as the input to another software which performs the application), the
        following recipe can be used::

          >>> zb = vcorr / c  # doctest: +REMOTE_DATA
          >>> zb_plus_one_squared = (zb + 1) ** 2  # doctest: +REMOTE_DATA
          >>> vcorr_rel = c * (zb_plus_one_squared - 1) / (zb_plus_one_squared + 1)  # doctest: +REMOTE_DATA

        or alternatively using just equivalencies::

          >>> vcorr_rel = vcorr.to(u.Hz, u.doppler_optical(1*u.Hz)).to(vcorr.unit, u.doppler_relativistic(1*u.Hz))  # doctest: +REMOTE_DATA

        See also `~astropy.units.equivalencies.doppler_optical`,
        `~astropy.units.equivalencies.doppler_radio`, and
        `~astropy.units.equivalencies.doppler_relativistic` for more information on
        the velocity conventions.

        The default is for this method to use the builtin ephemeris for
        computing the sun and earth location.  Other ephemerides can be chosen
        by setting the `~astropy.coordinates.solar_system_ephemeris` variable,
        either directly or via ``with`` statement.  For example, to use the JPL
        ephemeris, do::

          >>> from astropy.coordinates import solar_system_ephemeris
          >>> sc = SkyCoord(1*u.deg, 2*u.deg)
          >>> with solar_system_ephemeris.set('jpl'):  # doctest: +REMOTE_DATA
          ...     rv += sc.radial_velocity_correction(obstime=t, location=loc)  # doctest: +SKIP

        r
   )get_body_barycentric_posvellocationNz`location` cannot be in both the passed-in `obstime` and this `SkyCoord` because it is ambiguous which is meant for the radial_velocity_correction.zMust provide a `location` to radial_velocity_correction, either as a SkyCoord frame attribute, as an attribute on the passed in `obstime`, or in the method call.zCannot compute radial velocity correction if `location` argument is passed in and there is also a  `location` attribute on this SkyCoord or the passed-in `obstime`.ztMust provide an `obstime` to radial_velocity_correction, either as a SkyCoord frame attribute or in the method call.r=   an  SkyCoord has space motion, and therefore the specified position of the SkyCoord may not be the same as the `obstime` for the radial velocity measurement. This may affect the rv correction at the order of km/sfor very high proper motions sources. If you wish to apply space motion of the SkyCoord to correct for thisthe `obstime` attribute of the SkyCoord must be setZearthr   ZheliocentricZsunzc`kind` argument to radial_velocity_correction must be 'barycentric' or 'heliocentric', but got '{}'r>   a  SkyCoord contains some velocity information, but not enough to calculate the full space motion of the source, and so this has been ignored for the purposes of calculating the radial velocity correction. This can lead to errors on the order of metres/second.)Zsolar_systemr  r/   r  rS   r   r   r?   r8   rB   r   r   rN   r	   r)   Zget_gcrs_posvelr   r   r   r:   r   r   speed_of_lightrQ   ZsqrtZgravitational_redshiftdotZto_cartesianr   r   )r3   kindr   r  r  ZtimelocZcoo_at_rv_obstimeZ	pos_earthZv_earthZv_origin_to_earthZv_sunZgcrs_pZgcrs_vZ	icrs_cartZicrs_cart_novelZtargcartZobs_icrs_cartZbeta_obsZ	gamma_obsZgrZzbZroZ	beta_starr   r   r!   radial_velocity_correction  sz    c








$
z#SkyCoord.radial_velocity_correctionc                    sT  t g |\}}|f i |}|d||d< t| t|d}i }|D ]}|d }	d| d }
td|	 d|
 dtjtjB   fd	d
|j	D }t
|dkrqPnPt
|dkr| }n:|||h 8 }t
|dkr| }ntd| d| d|| ||< qP| D ]0\}}||v r:td|j|n|||< q| f i |S )a  
        A convenience method to create and return a new `SkyCoord` from the data
        in an astropy Table.

        This method matches table columns that start with the case-insensitive
        names of the the components of the requested frames (including
        differentials), if they are also followed by a non-alphanumeric
        character. It will also match columns that *end* with the component name
        if a non-alphanumeric character is *before* it.

        For example, the first rule means columns with names like
        ``'RA[J2000]'`` or ``'ra'`` will be interpreted as ``ra`` attributes for
        `~astropy.coordinates.ICRS` frames, but ``'RAJ2000'`` or ``'radius'``
        are *not*. Similarly, the second rule applied to the
        `~astropy.coordinates.Galactic` frame means that a column named
        ``'gal_l'`` will be used as the the ``l`` component, but ``gall`` or
        ``'fill'`` will not.

        The definition of alphanumeric here is based on Unicode's definition
        of alphanumeric, except without ``_`` (which is normally considered
        alphanumeric).  So for ASCII, this means the non-alphanumeric characters
        are ``<space>_!"#$%&'()*+,-./\:;<=>?@[]^`{|}~``).

        Parameters
        ----------
        table : `~astropy.table.Table` or subclass
            The table to load data from.
        **coord_kwargs
            Any additional keyword arguments are passed directly to this class's
            constructor.

        Returns
        -------
        newsc : `~astropy.coordinates.SkyCoord` or subclass
            The new `SkyCoord` (or subclass) object.

        Raises
        ------
        ValueError
            If more than one match is found in the table for a component,
            unless the additional matches are also valid frame component names.
            If a "coord_kwargs" is provided for a value also found in the table.

        r?   r=   z	(\W|\b|_)z.*(\W|\b|_)z\b(z)|()c                    s   h | ]}  |r|qS r   )match)r    col_nameZrexr   r!   	<setcomp>  s   
z,SkyCoord.guess_from_table.<locals>.<setcomp>r   r
   z*Found at least two matches for component "z": "z=". Cannot guess coordinates from a table with this ambiguity.zeFound column "{}" in table, but it was already provided as "{}" keyword to guess_from_table function.)r   rC   r\   rD   unionrecompile
IGNORECASEUNICODEZcolnamesrc   r   rS   r   r)   rJ   )r   tableZcoord_kwargsZ
_frame_clsZ_frame_kwargsr?   rA   Zcomp_kwargsZ	comp_nameZstarts_with_compZends_with_compZmatchesr
  kvr   r  r!   guess_from_table  sJ    .



zSkyCoord.guess_from_tabler   c                 C   sB   ddl m} ||||d}| |}|d|jfv r4|S ||S dS )a  
        Given a name, query the CDS name resolver to attempt to retrieve
        coordinate information for that object. The search database, sesame
        url, and  query timeout can be set through configuration items in
        ``astropy.coordinates.name_resolve`` -- see docstring for
        `~astropy.coordinates.get_icrs_coordinates` for more
        information.

        Parameters
        ----------
        name : str
            The name of the object to get coordinates for, e.g. ``'M42'``.
        frame : str or `BaseCoordinateFrame` class or instance
            The frame to transform the object to.
        parse : bool
            Whether to attempt extracting the coordinates from the name by
            parsing with a regex. For objects catalog names that have
            J-coordinates embedded in their names, e.g.,
            'CRTS SSS100805 J194428-420209', this may be much faster than a
            Sesame query for the same object name. The coordinates extracted
            in this way may differ from the database coordinates by a few
            deci-arcseconds, so only use this option if you do not need
            sub-arcsecond accuracy for coordinates.
        cache : bool, optional
            Determines whether to cache the results or not. To update or
            overwrite an existing value, pass ``cache='update'``.

        Returns
        -------
        coord : SkyCoord
            Instance of the SkyCoord class.
        r
   )get_icrs_coordinates)cacher   N)Zname_resolver  r:   r   )r   rJ   r?   parser  r  Z
icrs_coordZicrs_sky_coordr   r   r!   	from_name*  s    #zSkyCoord.from_name)r   )T)NN)r   )r
   )r
   )N)Fr   )r   r   )r   r   )N)r   NN)r   FT)3rX   rY   rZ   r[   r   r%   rk   r^   r?   r6   setterrr   rs   rv   rw   rx   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   classmethodr   r   r  r  r  r_   r   r   rM   r!   r      st   zB





:
A%
e
  

?**5*4'/
=
@;;-

/

%  
 Q
f)2r  ra   r   r   ZnumpyrQ   r   Zastropy.utils.compat.miscr   Zastropyr   r   Zastropy.constantsr   r  Zastropy.utils.data_infor   Zastropy.utilsr   Zastropy.tabler   Zastropy.timer   Zastropy.utils.exceptionsr	   Z	distancesr   Zanglesr   Z	baseframer   r   r   Zbuiltin_framesr   r   rr   r   r   r   r   r   r   Zsky_coordinate_parsersr   r   r   __all__r   r   r   r   r   r!   <module>   s,    