U
    \IdoQ                     @   s~   d 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	d
dgZdddZddd	Zddd
ZdddZdddZdS )zB
This module contains functions for matching coordinate catalogs.
    N)units   )Angle)UnitSphericalRepresentation)SkyCoordmatch_coordinates_3dmatch_coordinates_skysearch_around_3dsearch_around_sky	kdtree_3dc                 C   s  |j st|dk rtdt||}t| tr>| j|dd} n
| |} |jjj	}| jj
|}|dt|jd f}t|j rtd||j|\}}	|dkr|dddf }|	dddf }	||	 | }
|	|jdd |
||jdd | fS )	a?  
    Finds the nearest 3-dimensional matches of a coordinate or coordinates in
    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 either ``matchcoord``
    or ``catalogcoord``.

    Parameters
    ----------
    matchcoord : `~astropy.coordinates.BaseCoordinateFrame` or `~astropy.coordinates.SkyCoord`
        The coordinate(s) to match to the catalog.
    catalogcoord : `~astropy.coordinates.BaseCoordinateFrame` or `~astropy.coordinates.SkyCoord`
        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).
    storekdtree : bool or str, optional
        If a string, will store the KD-Tree used for the computation
        in the ``catalogcoord``, as in ``catalogcoord.cache`` with the
        provided name.  This dramatically speeds up subsequent calls with the
        same catalog. If False, the KD-Tree is discarded after use.

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

    Notes
    -----
    This function requires `SciPy <https://www.scipy.org/>`_ to be installed
    or it will fail.
    r   CThe catalog for coordinate matching cannot be a scalar or length-0.FZmerge_attributes   z0Matching coordinates cannot contain NaN entries.N)isscalarlen
ValueError_get_cartesian_kdtree
isinstancer   transform_to	cartesianxunitxyztoreshapenpprodshapeisnanvalueanyqueryT
separation)
matchcoordcatalogcoordnthneighborstorekdtreekdtZcatunitZmatchxyzZmatchflatxyzdistidxsep2d r-   @/tmp/pip-unpacked-wheel-1sxf5fhv/astropy/coordinates/matching.pyr      s,    0




kdtree_skyc                 C   s   |j st|dk rtdt| tr4| j|dd}n
| |}|jt}|	|}|jt}|	|}|j
||}t||||\}	}
}t|jtst|jts||	 |}t|tr|j
| |j
|< n|dkr|j
d |j
d< |	|
|fS )a  
    Finds the nearest on-sky matches of a coordinate or coordinates in
    a set of catalog coordinates.

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

    Parameters
    ----------
    matchcoord : `~astropy.coordinates.BaseCoordinateFrame` or `~astropy.coordinates.SkyCoord`
        The coordinate(s) to match to the catalog.
    catalogcoord : `~astropy.coordinates.BaseCoordinateFrame` or `~astropy.coordinates.SkyCoord`
        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).
    storekdtree : bool or str, optional
        If a string, will store the KD-Tree used for the computation
        in the ``catalogcoord`` in ``catalogcoord.cache`` with the
        provided name.  This dramatically speeds up subsequent calls with the
        same catalog. If False, the KD-Tree is discarded after use.

    Returns
    -------
    idx : int array
        Indices into ``catalogcoord`` to get the matched points for each
        ``matchcoord``. Shape matches ``matchcoord``.
    sep2d : `~astropy.coordinates.Angle`
        The on-sky separation between the closest match for each
        ``matchcoord`` and the ``matchcoord``. Shape matches ``matchcoord``.
    dist3d : `~astropy.units.Quantity` ['length']
        The 3D distance between the closest match for each ``matchcoord`` and
        the ``matchcoord``. Shape matches ``matchcoord``.  If either
        ``matchcoord`` or ``catalogcoord`` don't have a distance, this is the 3D
        distance on the unit sphere, rather than a true distance.

    Notes
    -----
    This function requires `SciPy <https://www.scipy.org/>`_ to be installed
    or it will fail.
    r   r   Fr   Tkdtree)r   r   r   r   r   r   datarepresent_asr   realize_framecachegetr   separation_3dstr)r%   r&   r'   r(   ZnewmatchZmatch_ureprZ
newmatch_uZ	cat_ureprZnewcat_ur+   r,   Zsep3dr-   r-   r.   r   j   s8    2



   



c                 C   sZ  |j std| j s|j r"tdt| dks:t|dkrntjg tdtjg tdtg tjt	g | j
jfS t||}|jjj}| |} t| ||d}||}g }g }	t|||D ]&\}
}|D ]}||
 |	| qqtj|td}tj|	td}	|jdkr*tg tj}t	g | j
j}n$| | ||	 }| | ||	 }||	||fS )a	  
    Searches for pairs of points that are at least as close as a specified
    distance in 3D space.

    This is intended for use on coordinate objects with arrays of coordinates,
    not scalars.  For scalar coordinates, it is better to use the
    ``separation_3d`` methods.

    Parameters
    ----------
    coords1 : `~astropy.coordinates.BaseCoordinateFrame` or `~astropy.coordinates.SkyCoord`
        The first set of coordinates, which will be searched for matches from
        ``coords2`` within ``seplimit``.  Cannot be a scalar coordinate.
    coords2 : `~astropy.coordinates.BaseCoordinateFrame` or `~astropy.coordinates.SkyCoord`
        The second set of coordinates, which will be searched for matches from
        ``coords1`` within ``seplimit``.  Cannot be a scalar coordinate.
    distlimit : `~astropy.units.Quantity` ['length']
        The physical radius to search within.
    storekdtree : bool or str, optional
        If a string, will store the KD-Tree used in the search with the name
        ``storekdtree`` in ``coords2.cache``. This speeds up subsequent calls
        to this function. If False, the KD-Trees are not saved.

    Returns
    -------
    idx1 : int array
        Indices into ``coords1`` that matches to the corresponding element of
        ``idx2``. Shape matches ``idx2``.
    idx2 : int array
        Indices into ``coords2`` that matches to the corresponding element of
        ``idx1``. Shape matches ``idx1``.
    sep2d : `~astropy.coordinates.Angle`
        The on-sky separation between the coordinates. Shape matches ``idx1``
        and ``idx2``.
    dist3d : `~astropy.units.Quantity` ['length']
        The 3D distance between the coordinates. Shape matches ``idx1`` and
        ``idx2``. The unit is that of ``coords1``.

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

    If you are using this function to search in a catalog for matches around
    specific points, the convention is for ``coords2`` to be the catalog, and
    ``coords1`` are the points to search around.  While these operations are
    mathematically the same if ``coords1`` and ``coords2`` are flipped, some of
    the optimizations may work better if this convention is obeyed.

    In the current implementation, the return values are always sorted in the
    same order as the ``coords1`` (so ``idx1`` is in ascending order).  This is
    considered an implementation detail, though, so it could change in a future
    release.
    z.distlimit must be a scalar in search_around_3dzOne of the inputs to search_around_3d is a scalar. search_around_3d is intended for use with array coordinates, not scalars.  Instead, use ``coord1.separation_3d(coord2) < distlimit`` to find the coordinates near a scalar coordinate.r   Zdtype)	forceunit)r   r   r   r   arrayintr   udegQuantitydistancer   r   r   r   r   Zto_value	enumeratequery_ball_treeappendsizer$   r6   )coords1coords2Z	distlimitr(   kdt2Zcunitkdt1didxs1idxs2imatchesmatchd2dsd3dsr-   r-   r.   r	      s>    7





c                 C   s(  |j std| j s|j r"tdt| dks:t|dkr|jjtjkrPtj}n| jj}tjg t	dtjg t	dt
g tjtg |fS | |} | jt}| |}t||}|r|j|r|j| }n>|jt}	||	}
t|
|}|r||j|dkrdn|< dtt
|d  j}g }g }t|||D ]*\}}|D ]}|| || qHq<tj|t	d}tj|t	d}|jdkr|jjtjkrtj}n| jj}t
g tj}tg |}nR| | || }z| | || }W n( tk
r   dt|d  }Y nX ||||fS )	a  
    Searches for pairs of points that have an angular separation at least as
    close as a specified angle.

    This is intended for use on coordinate objects with arrays of coordinates,
    not scalars.  For scalar coordinates, it is better to use the ``separation``
    methods.

    Parameters
    ----------
    coords1 : coordinate-like
        The first set of coordinates, which will be searched for matches from
        ``coords2`` within ``seplimit``. Cannot be a scalar coordinate.
    coords2 : coordinate-like
        The second set of coordinates, which will be searched for matches from
        ``coords1`` within ``seplimit``. Cannot be a scalar coordinate.
    seplimit : `~astropy.units.Quantity` ['angle']
        The on-sky separation to search within.
    storekdtree : bool or str, optional
        If a string, will store the KD-Tree used in the search with the name
        ``storekdtree`` in ``coords2.cache``. This speeds up subsequent calls
        to this function. If False, the KD-Trees are not saved.

    Returns
    -------
    idx1 : int array
        Indices into ``coords1`` that matches to the corresponding element of
        ``idx2``. Shape matches ``idx2``.
    idx2 : int array
        Indices into ``coords2`` that matches to the corresponding element of
        ``idx1``. Shape matches ``idx1``.
    sep2d : `~astropy.coordinates.Angle`
        The on-sky separation between the coordinates. Shape matches ``idx1``
        and ``idx2``.
    dist3d : `~astropy.units.Quantity` ['length']
        The 3D distance between the coordinates. Shape matches ``idx1``
        and ``idx2``; the unit is that of ``coords1``.
        If either ``coords1`` or ``coords2`` don't have a distance,
        this is the 3D distance on the unit sphere, rather than a
        physical distance.

    Notes
    -----
    This function 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 ``coords1`` (so ``idx1`` is in ascending order).  This is
    considered an implementation detail, though, so it could change in a future
    release.
    z.seplimit must be a scalar in search_around_skyzOne of the inputs to search_around_sky is a scalar. search_around_sky is intended for use with array coordinates, not scalars.  Instead, use ``coord1.separation(coord2) < seplimit`` to find the coordinates near a scalar coordinate.r   r8   Tr0      g       @)r   r   r   r?   r   r<   Zdimensionless_unscaledr   r:   r;   r   r=   r>   r   r1   r2   r   r3   r   r4   r5   sinr    r@   rA   rB   rC   r$   r6   )rD   rE   Zseplimitr(   ZdistunitZurepr1Zucoords1rG   rF   Zurepr2Zucoords2rrI   rJ   rK   rL   rM   rN   rO   r-   r-   r.   r
   3  s^    4








r0   c           	      C   sh  ddl m} ddlm} z
|j}W n" tk
rD   |d |j}Y nX |dkrRd}t|tr| j	
|d}|dk	rt||std| d	n.t||r|}d}n|sd}ntd
t| |dkrT|dkr| jj}n| jj|}|dt|jd f}t|j rtdz||jjddd}W n" tk
rR   ||jj}Y nX |rd|| j	|< |S )a  
    This is a utility function to retrieve (and build/cache, if necessary)
    a 3D cartesian KD-Tree from various sorts of astropy coordinate objects.

    Parameters
    ----------
    coord : `~astropy.coordinates.BaseCoordinateFrame` or `~astropy.coordinates.SkyCoord`
        The coordinates to build the KD-Tree for.
    attrname_or_kdt : bool or str or KDTree
        If a string, will store the KD-Tree used for the computation in the
        ``coord``, in ``coord.cache`` with the provided name. If given as a
        KD-Tree, it will just be used directly.
    forceunit : unit or None
        If a unit, the cartesian coordinates will convert to that unit before
        being put in the KD-Tree.  If None, whatever unit it's already in
        will be used

    Returns
    -------
    kdt : `~scipy.spatial.cKDTree` or `~scipy.spatial.KDTree`
        The KD-Tree representing the 3D cartesian representation of the input
        coordinates.
    r   )warn)spatialzNC-based KD tree not found, falling back on (much slower) python implementationTr0   NzThe `attrname_or_kdt` "z" is not a scipy KD tree!z/Invalid `attrname_or_kdt` argument for KD-Tree:r   z/Catalog coordinates cannot contain NaN entries.F)Zcompact_nodesZbalanced_tree)warningsrS   ZscipyrT   ZcKDTree	ExceptionKDTreer   r7   r4   r5   	TypeErrorr   r   r   r   r   r   r   r   r    r!   r   r#   )	ZcoordZattrname_or_kdtr9   rS   rT   rW   r)   ZcartxyzZflatxyzr-   r-   r.   r     sN    







r   )r   r   )r   r/   )r   )r/   )r0   N)__doc__Znumpyr   Zastropyr   r<    r   Zrepresentationr   Zsky_coordinater   __all__r   r   r	   r
   r   r-   r-   r-   r.   <module>   s(   	   
T   
^
k
 