Using IRSA TAP services from Python

We recommend using the astroquery function query_region for simple TAP queries that just involve a spatial constraint (e.g. cone, box, polygon, all-sky). For more complex TAP queries, we recommend using the query_tap function. The astroquery package can be installed with the terminal command "pip install astroquery" or "conda install -c conda-forge astroquery", if using a conda environment. Although we recommend using astroquery for TAP queries in Python, we note that astroquery uses PyVO internally, which can also be used directly to perform equivalent queries.

Example Query

In the example below we demonstrate two equivalent queries of the AllWISE Source Catalog using first query_region and then query_tap (both astroquery functions). The query_region function is a simple means of performing most spatial queries, while query_tap permits more general queries, but as a result relies on a more complex syntax.

Begin by importing the IRSA module from astroquery and using the list_catalogs function to search for all IRSA catalogs with "allwise" in their name.

>>> from astroquery.ipac.irsa import Irsa
>>> Irsa.list_catalogs(filter='allwise')

Scanning through the list you will find that the AllWISE Source Catalog can be accessed with the code "allwise_p3as_psd".

Note: The "filter" keyword is only available for astroquery>=0.4.10. The full catalog list can still be returned without this argument, or you can update your version of astroquery with either the terminal command "conda update -c conda-forge astroquery" or "pip install --upgrade astroquery", depending on how it was originally installed.

The columns available in this catalog can be inspected with the command:

>>> Irsa.list_columns('allwise_p3as_psd')

If you prefer an interactive view then you can also find column names and descriptions by searching for a particular catalog here.

In this example we are only interested in the names, positions, and basic photometry of the sources, so define a string listing the columns for the query to return:

>>> return_cols = 'designation,ra,dec,w1mpro,w1sigmpro,w2mpro,w2sigmpro, w3mpro,w3sigmpro,w4mpro,w4sigmpro,ph_qual'

Next, use astropy SkyCoord to define the position that you wish to search around, then use query_region to search for all sources in the AllWISE Source Catalog that are within 0.4 degrees of that position.

>>> from astropy.coordinates import SkyCoord
>>> import astropy.units as u
>>> pos = SkyCoord(ra=202.48417*u.deg, dec=47.23056*u.deg)
>>> result = Irsa.query_region(pos, catalog='allwise_p3as_psd',
radius=0.4*u.deg, columns=return_cols)
>>> result

The final command displays the output as an Astropy Table.


Now let's repeat this query, but using TAP directly through the query_tap function.

As before, the ID code of the catalog being searched must be identified and the relevant columns chosen. These steps are exactly as those above, so we will not repeat them here.

The command below will run a TAP query on the catalog, searching within a circle of radius 0.4 degrees around a point at RA = 202.48417 and Dec = 47.23056, and returning only the listed columns.

>>> result = Irsa.query_tap('''SELECT designation,ra,dec,w1mpro,w1sigmpro,w2mpro,w2sigmpro,w3mpro,w3sigmpro,w4mpro,w4sigmpro,ph_qual
FROM allwise_p3as_psd
WHERE CONTAINS(POINT('ICRS',ra, dec), CIRCLE('ICRS',202.48417,47.23056,0.4))=1''')
>>> result.to_table()

The first command runs the query and the second converts the output to an Astropy Table and displays it. It should be identical the the previous table generated with query_region.


In this simple example, and most other spatial queries, query_region offers a straightforward interface for running catalog queries. The advantage of query_tap lies in executing complex queries that go beyond simple spatial searches. More details on the syntax of TAP queries can be found here, and further Python examples of using TAP queries and other aspects of the API can be found in the IRSA notebook tutorials.