102.5. Cross-match to uploaded table with TAP#
102.5. Cross-match to uploaded table with TAP¶
For the Rubin Science Platform at data.lsst.cloud.
Data Release: Data Preview 2
Container Size: Large
LSST Science Pipelines version: r30.0.11
Last verified to run: 2026-10-28
Repository: github.com/lsst/tutorial-notebooks
DOI: 10.11578/rubin/dc.20250909.20
Learning objective: How to use the TAP service to cross-match to a user-uploaded table.
LSST data products: Object table, SSObject table
Packages: lsst.rsp.RSPDiscovery
Credit: Originally developed by the Rubin Community Science team. Please consider acknowledging them if this notebook is used for the preparation of journal articles, software releases, or other notebooks.
Get Support: Everyone is encouraged to ask questions or raise issues in the Support Category of the Rubin Community Forum. Rubin staff will respond to all questions posted there.
1. Introduction¶
TAP provides standardized access to catalog data for discovery, search, and retrieval. Full documentation for TAP is provided by the International Virtual Observatory Alliance (IVOA).
This tutorial demonstrates the workflow for ingesting user-provided tables into the TAP service to enable queries against the LSST/DP2 data. The system's ingestion capability automatically handles columns containing any variations of NaN and Inf values in uploaded tables; therefore, no manual pre-processing is needed.
Warning: When uploading plain text formats (such as CSV), explicitly convert all Boolean True/False strings to integers 1/0 to avoid ingestion errors. For metadata-rich formats like VOTable, ensure the column metadata is correctly defined as boolean.
Related tutorials: The other 100-level tutorials in this series demonstrate how to use the TAP service.
1.1. Import packages¶
Import general python packages matplotlib, numpy, time, and astropy.
Import the pyvo module for accessing remote data.
From the lsst package, import the the RSPDiscovery class for accessing the Table Access Protocol (TAP) service.
import matplotlib.pyplot as plt
import numpy as np
import time
from astropy.table import Table, vstack
from astropy.coordinates import SkyCoord
import astropy.units as u
from astropy_healpix import HEALPix
import pyvo
from lsst.rsp import RSPDiscovery
1.2. Define parameters¶
Instantiate RSPDiscovery with the DP2 release, create an instance of the TAP service, and assert that it exists.
discovery = RSPDiscovery("dp2")
service = discovery.get_tap_client()
assert service is not None
2. Cross-match with a table of coordinates¶
Load a user-defined table to upload to the TAP service and spatially cross-match to the Object table, which includes detections in the coadded images. This example uses a table of 1,000 objects from the SDSS SkyServer within 0.5 degrees of the center of RA = 345 degrees and Dec = -5 degrees.
2.1. Load a table with coordinates¶
Read the user table as an astropy table.
path = '/rubin/cst_repos/tutorial-notebooks-data/data/'
ut1 = Table.read(path + 'dp2_102_user_table.csv')
Option to display the table.
# ut1
2.2. Execute the cross-match¶
To cross-match a user-uploaded table against the LSST catalogs and retrieve the results, Qserv allows users to temporarily upload their own catalogs and use them in queries. When a query joins the uploaded table on coordinates, Qserv automatically partitions it with the same algorithm used for the LSST catalogs. Each uploaded source is then compared only against LSST objects in the same or neighboring sky chunks, not the whole catalog, which keeps the query efficient. Two spatial join types are supported: DISTANCE-based join and CONTAINS-based. For a simple radius match, both approaches give the same result and run with similar efficiency.
Query the Object table for objects that lie within 1" (or 0.00027 degrees) of each source in the uploaded table, ut1. The CONTAINS version below is shown for reference only and is not executed in this notebook:
query = """
SELECT ut1.objid AS ut1_objid, ut1.ra AS ut1_ra, ut1.dec AS ut1_dec,
dp2.objectId, dp2.coord_ra, dp2.coord_dec
FROM dp2.Object AS dp2
JOIN TAP_UPLOAD.ut1 AS ut1
WHERE CONTAINS(POINT('ICRS', dp2.coord_ra, dp2.coord_dec),
CIRCLE('ICRS', ut1.ra, ut1.dec, 0.00027)) = 1
"""
The query run in the next cell uses the DISTANCE-based join.
query = """
SELECT ut1.objid AS ut1_objid, ut1.ra AS ut1_ra, ut1.dec AS ut1_dec,
dp2.objectId, dp2.coord_ra, dp2.coord_dec
FROM dp2.Object AS dp2
JOIN TAP_UPLOAD.ut1 AS ut1
ON DISTANCE(POINT('ICRS', dp2.coord_ra, dp2.coord_dec),
POINT('ICRS', ut1.ra, ut1.dec)) < 0.00027
"""
Submit the query to the TAP service.
job = service.submit_job(query, uploads={"ut1": ut1})
job.run()
job.wait(phases=['COMPLETED', 'ERROR'])
print('Job phase is', job.phase)
if job.phase == 'ERROR':
job.raise_if_error()
Job phase is COMPLETED
Retrieve the results.
results = job.fetch_result().to_table()
unique_matches = np.unique(results['ut1_objid'])
print(f"Returned {len(results)} matches with {len(unique_matches)} unique entries.")
print("Always check for duplicates!")
Returned 856 matches with 849 unique entries. Always check for duplicates!
del results, unique_matches
2.2.1. Add column constraints¶
In the query above for coordinate-based cross-matching, constraints can also be placed on other columns in the uploaded tables.
The following query is identical to the one above, except that it:
- returns the column
rfromut1assdss_r(the $r$-band SDSS magnitude) - returns the difference between DP2 and SDSS $r$-band magnitudes as
r_diffs - constrains the results to cross-matches for which the $r$-band magnitudes agree within 0.1 mag
query = """
SELECT ut1.objid AS ut1_objid, ut1.ra AS ut1_ra, ut1.dec AS ut1_dec,
ut1.r AS sdss_r, ABS(ut1.r - r_cModelMag) AS r_diffs,
dp2.objectId, dp2.coord_ra, dp2.coord_dec, dp2.r_cModelMag
FROM dp2.Object AS dp2
JOIN TAP_UPLOAD.ut1 AS ut1
ON DISTANCE(POINT('ICRS', dp2.coord_ra, dp2.coord_dec),
POINT('ICRS', ut1.ra, ut1.dec)) < 0.00027
WHERE ABS(ut1.r - dp2.r_cModelMag) < 0.1"""
Submit the query to the TAP service.
job = service.submit_job(query, uploads={"ut1": ut1})
job.run()
job.wait(phases=['COMPLETED', 'ERROR'])
print('Job phase is', job.phase)
if job.phase == 'ERROR':
job.raise_if_error()
Job phase is COMPLETED
Retrieve the results.
results = job.fetch_result().to_table()
unique_matches = np.unique(results['ut1_objid'])
print(f"Returned {len(results)} matches with {len(unique_matches)} unique entries.")
Returned 384 matches with 384 unique entries.
del results, unique_matches
3. Cross-match with a table of identifiers¶
Query an external database for solar system objects and cross-match it to the SSObject table using the Minor Planet Catalog (MPC) designation.
This example uses a table of moving objects retrieved from the Gaia database using PyVO.
3.1. Query the Gaia database using TAP¶
Instantiate the Gaia TAP service using PyVO.
gaia_tap_url = 'https://gea.esac.esa.int/tap-server/tap'
gaia_tap = pyvo.dal.TAPService(gaia_tap_url)
assert gaia_tap is not None
Query the Gaia sso_orbits table for main-belt asteroids (MBAs). Constrain the query to 1,000 objects, with semimajor axis a and q following the definition of MBA used by the
JPL Horizons small body database query tool (
2.0 < a < 3.25 au and q > 1.666 au).
gaia_query = """
SELECT TOP 1000 denomination, inclination, eccentricity, semi_major_axis
FROM gaiadr3.sso_orbits
WHERE num_observations > 200
AND semi_major_axis > 2.0
AND semi_major_axis < 3.25
AND semi_major_axis*(1-eccentricity) > 1.666"""
Run the query and retrieve the results as an astropy table named ut2.
gaia_job = gaia_tap.submit_job(gaia_query)
gaia_job.run()
gaia_job.wait(phases=['COMPLETED', 'ERROR'])
print('Job phase is', gaia_job.phase)
if gaia_job.phase == 'ERROR':
gaia_job.raise_if_error()
ut2 = gaia_job.fetch_result().to_table()
Job phase is COMPLETED
Display the results of the query. Confirm that the query returned 1,000 rows.
ut2
| denomination | inclination | eccentricity | semi_major_axis |
|---|---|---|---|
| rad | AU | ||
| object | float64 | float64 | float64 |
| mairepercy | 0.036872739657094256 | 0.05338550332562588 | 2.6979047496130035 |
| 1999_cj138 | 0.025365800073234634 | 0.17064383850623568 | 2.150420617612988 |
| 1999_jw19 | 0.08687176993130774 | 0.09537641647614271 | 2.2857468927378832 |
| 2000_jb23 | 0.2008077410471647 | 0.19637433500656612 | 2.696545547950699 |
| 1999_cv14 | 0.2773846230591117 | 0.1742077595699058 | 3.1492334657780474 |
| 1998_fa85 | 0.22916828296800218 | 0.1764982562246874 | 2.6279412520253747 |
| 2000_dv103 | 0.08421208923670576 | 0.0946230410981651 | 2.5873967955326984 |
| 2001_qa140 | 0.23172314870191268 | 0.1765546829639623 | 2.612878613604526 |
| 5082_t-3 | 0.09722857212427716 | 0.14943538161518813 | 2.313023490713334 |
| ... | ... | ... | ... |
| 2001_fp94 | 0.12197107747419245 | 0.12940395453854112 | 3.1680655747075424 |
| 1998_ue8 | 0.031091168023050786 | 0.13544657166839452 | 3.218942208865641 |
| 2001_yk70 | 0.057912279186778104 | 0.1465772059695987 | 2.261959396276623 |
| liadawley | 0.10593130210196589 | 0.16802426766654122 | 2.24012326517617 |
| 1999_nf16 | 0.11432602578019102 | 0.12376548711769061 | 2.302572876608841 |
| markfisher | 0.3319703456609675 | 0.17862629872606048 | 3.2136024574941255 |
| 2001_qd28 | 0.034956531667453605 | 0.1694664191467491 | 2.428862833546699 |
| 1993_oe13 | 0.09678745444708814 | 0.10196182979667723 | 2.298882243575362 |
| 2001_sg279 | 0.09909932554900358 | 0.04587187535917843 | 2.689203591968772 |
3.2. Manipulate the Gaia results table¶
Format the denomination column in the Gaia results table to match the standard format for MPC designation used in the SSObject table. Create a new column from the Gaia denomination column with underscores removed and letters capitalized.
ut2['mpc_designation'] = np.char.upper(np.char.replace(ut2['denomination'].astype('U'), '_', ' '))
Display the updated table.
ut2
| denomination | inclination | eccentricity | semi_major_axis | mpc_designation |
|---|---|---|---|---|
| rad | AU | |||
| object | float64 | float64 | float64 | str16 |
| mairepercy | 0.036872739657094256 | 0.05338550332562588 | 2.6979047496130035 | MAIREPERCY |
| 1999_cj138 | 0.025365800073234634 | 0.17064383850623568 | 2.150420617612988 | 1999 CJ138 |
| 1999_jw19 | 0.08687176993130774 | 0.09537641647614271 | 2.2857468927378832 | 1999 JW19 |
| 2000_jb23 | 0.2008077410471647 | 0.19637433500656612 | 2.696545547950699 | 2000 JB23 |
| 1999_cv14 | 0.2773846230591117 | 0.1742077595699058 | 3.1492334657780474 | 1999 CV14 |
| 1998_fa85 | 0.22916828296800218 | 0.1764982562246874 | 2.6279412520253747 | 1998 FA85 |
| 2000_dv103 | 0.08421208923670576 | 0.0946230410981651 | 2.5873967955326984 | 2000 DV103 |
| 2001_qa140 | 0.23172314870191268 | 0.1765546829639623 | 2.612878613604526 | 2001 QA140 |
| 5082_t-3 | 0.09722857212427716 | 0.14943538161518813 | 2.313023490713334 | 5082 T-3 |
| ... | ... | ... | ... | ... |
| 2001_fp94 | 0.12197107747419245 | 0.12940395453854112 | 3.1680655747075424 | 2001 FP94 |
| 1998_ue8 | 0.031091168023050786 | 0.13544657166839452 | 3.218942208865641 | 1998 UE8 |
| 2001_yk70 | 0.057912279186778104 | 0.1465772059695987 | 2.261959396276623 | 2001 YK70 |
| liadawley | 0.10593130210196589 | 0.16802426766654122 | 2.24012326517617 | LIADAWLEY |
| 1999_nf16 | 0.11432602578019102 | 0.12376548711769061 | 2.302572876608841 | 1999 NF16 |
| markfisher | 0.3319703456609675 | 0.17862629872606048 | 3.2136024574941255 | MARKFISHER |
| 2001_qd28 | 0.034956531667453605 | 0.1694664191467491 | 2.428862833546699 | 2001 QD28 |
| 1993_oe13 | 0.09678745444708814 | 0.10196182979667723 | 2.298882243575362 | 1993 OE13 |
| 2001_sg279 | 0.09909932554900358 | 0.04587187535917843 | 2.689203591968772 | 2001 SG279 |
3.3. Cross-match to the SSObject table¶
Query the SSObject table for objects matching the MPC designation from the Gaia results table, ut2. Retrieve the SSObjectId for the matches.
query = """
SELECT ut2.mpc_designation AS ut2_mpcDesignation, sso.designation, sso.ssObjectId
FROM dp2.SSObject AS sso
JOIN TAP_UPLOAD.ut2 as ut2
ON ut2.mpc_designation = sso.designation"""
Run the query and upload the user-defined table.
job = service.submit_job(query, uploads={"ut2": ut2})
job.run()
job.wait(phases=['COMPLETED', 'ERROR'])
print('Job phase is', job.phase)
if job.phase == 'ERROR':
job.raise_if_error()
Job phase is COMPLETED
Retrieve the results.
results = job.fetch_result().to_table()
Display the results of the query.
results
| ut2_mpcDesignation | designation | ssObjectId |
|---|---|---|
| str16 | str16 | int64 |
| 1967 US | 1967 US | 20888759556911187 |
| 1979 OH1 | 1979 OH1 | 20889867557810504 |
| 1981 SD2 | 1981 SD2 | 20890932776809028 |
| 1985 CA2 | 1985 CA2 | 20890949688242753 |
| 1986 WP2 | 1986 WP2 | 20890954318754384 |
| 1988 RO11 | 1988 RO11 | 20890962824868175 |
| 1989 EJ1 | 1989 EJ1 | 20890966901666122 |
| 1990 UY4 | 1990 UY4 | 20892028027024473 |
| 1991 DG1 | 1991 DG1 | 20892032036778311 |
| ... | ... | ... |
| 2004 XD2 | 2004 XD2 | 21163624629285444 |
| 2005 EB272 | 2005 EB272 | 21163628607713858 |
| 2005 XA64 | 2005 XA64 | 21163628924646465 |
| 2005 YJ165 | 2005 YJ165 | 21163628942538058 |
| 2009 OG5 | 2009 OG5 | 21163645953127751 |
| 2695 P-L | 2695 P-L | 22601918345263413 |
| 4658 P-L | 4658 P-L | 22601918378816824 |
| 1040 T-2 | 1040 T-2 | 23699230932612144 |
| 5340 T-2 | 5340 T-2 | 23699230999917616 |
| 1208 T-3 | 1208 T-3 | 23700330444369976 |
del results, ut2