astrobase/coordutils.py
def xmatch_kdtree(kdtree,
                  extra, extdecl,
                  xmatchdistdeg,
                  closestonly=True):
    '''This cross-matches between `kdtree` and (`extra`, `extdecl`) arrays.

    Returns the indices of the kdtree and the indices of extra, extdecl that
    xmatch successfully.

    Parameters
    ----------

    kdtree : scipy.spatial.CKDTree
        This is a kdtree object generated by the `make_kdtree` function.

    extra,extdecl : array-like
        These are np.arrays of 'external' coordinates in decimal degrees that
        will be cross-matched against the objects in `kdtree`.

    xmatchdistdeg : float
        The match radius to use for the cross-match in decimal degrees.

    closestonly : bool
        If closestonly is True, then this function returns only the closest
        matching indices in (extra, extdecl) for each object in kdtree if there
        are any matches. Otherwise, it returns a list of indices in (extra,
        extdecl) for all matches within xmatchdistdeg between kdtree and (extra,
        extdecl).

    Returns
    -------

    tuple of lists
        Returns a tuple of the form::

            (list of `kdtree` indices matching to external objects,
             list of all `extra`/`extdecl` indices that match to each
             element in `kdtree` within the specified cross-match distance)

    '''

    ext_cosdecl = np.cos(np.radians(extdecl))
    ext_sindecl = np.sin(np.radians(extdecl))
    ext_cosra = np.cos(np.radians(extra))
    ext_sinra = np.sin(np.radians(extra))

    ext_xyz = np.column_stack((ext_cosra*ext_cosdecl,
                               ext_sinra*ext_cosdecl,
                               ext_sindecl))
    ext_xyzdist = 2.0 * np.sin(np.radians(xmatchdistdeg)/2.0)

    # get our kdtree
    our_kdt = kdtree

    # get the external kdtree
    ext_kdt = sps.cKDTree(ext_xyz)

    # do a query_ball_tree
    extkd_matchinds = our_kdt.query_ball_tree(ext_kdt, ext_xyzdist)

    ext_matchinds = []
    kdt_matchinds = []

    for extind, mind in enumerate(extkd_matchinds):
        if len(mind) > 0:
            # our object indices
            kdt_matchinds.append(extind)

            # external object indices
            if closestonly:
                ext_matchinds.append(mind[0])
            else:
                ext_matchinds.append(mind)

    return kdt_matchinds, ext_matchinds
def xmatch_kdtree(kdtree,
                  extra, extdecl,
                  xmatchdistdeg,
                  closestonly=True):
    '''This cross-matches between `kdtree` and (`extra`, `extdecl`) arrays.

    Returns the indices of the kdtree and the indices of extra, extdecl that
    xmatch successfully.

    Parameters
    ----------

    kdtree : scipy.spatial.CKDTree
        This is a kdtree object generated by the `make_kdtree` function.

    extra,extdecl : array-like
        These are np.arrays of 'external' coordinates in decimal degrees that
        will be cross-matched against the objects in `kdtree`.

    xmatchdistdeg : float
        The match radius to use for the cross-match in decimal degrees.

    closestonly : bool
        If closestonly is True, then this function returns only the closest
        matching indices in (extra, extdecl) for each object in kdtree if there
        are any matches. Otherwise, it returns a list of indices in (extra,
        extdecl) for all matches within xmatchdistdeg between kdtree and (extra,
        extdecl).

    Returns
    -------

    tuple of lists
        Returns a tuple of the form::

            (list of `kdtree` indices matching to external objects,
             list of all `extra`/`extdecl` indices that match to each
             element in `kdtree` within the specified cross-match distance)

    '''

    ext_cosdecl = np.cos(np.radians(extdecl))
    ext_sindecl = np.sin(np.radians(extdecl))
    ext_cosra = np.cos(np.radians(extra))
    ext_sinra = np.sin(np.radians(extra))

    ext_xyz = np.column_stack((ext_cosra*ext_cosdecl,
                               ext_sinra*ext_cosdecl,
                               ext_sindecl))
    ext_xyzdist = 2.0 * np.sin(np.radians(xmatchdistdeg)/2.0)

    # get our kdtree
    our_kdt = kdtree

    # get the external kdtree
    ext_kdt = sps.cKDTree(ext_xyz)

    # do a query_ball_tree
    extkd_matchinds = our_kdt.query_ball_tree(ext_kdt, ext_xyzdist)

    ext_matchinds = []
    kdt_matchinds = []

    for extind, mind in enumerate(extkd_matchinds):
        if len(mind) > 0:
            # our object indices
            kdt_matchinds.append(extind)

            # external object indices
            if closestonly:
                ext_matchinds.append(mind[0])
            else:
                ext_matchinds.append(mind)

    return kdt_matchinds, ext_matchinds
This cross-matches between `kdtree` and (`extra`, `extdecl`) arrays.

    Returns the indices of the kdtree and the indices of extra, extdecl that
    xmatch successfully.

    Parameters
    ----------

    kdtree : scipy.spatial.CKDTree
        This is a kdtree object generated by the `make_kdtree` function.

    extra,extdecl : array-like
        These are np.arrays of 'external' coordinates in decimal degrees that
        will be cross-matched against the objects in `kdtree`.

    xmatchdistdeg : float
        The match radius to use for the cross-match in decimal degrees.

    closestonly : bool
        If closestonly is True, then this function returns only the closest
        matching indices in (extra, extdecl) for each object in kdtree if there
        are any matches. Otherwise, it returns a list of indices in (extra,
        extdecl) for all matches within xmatchdistdeg between kdtree and (extra,
        extdecl).

    Returns
    -------

    tuple of lists
        Returns a tuple of the form::

            (list of `kdtree` indices matching to external objects,
             list of all `extra`/`extdecl` indices that match to each
             element in `kdtree` within the specified cross-match distance)