Astroquery
Astroquery provides a uniform interface to query astronomical web services and archives, making it easier to access online astronomical data resources. It is part of the Astropy Project and integrates seamlessly with Astropy's data structures. The current version is 0.4.11, and it maintains an active development cycle with frequent releases to add new services and fix issues.
Common errors
-
ModuleNotFoundError: No module named 'astroquery.<service>'
cause The astroquery library or a specific submodule was not installed, or the import path is incorrect.fixEnsure `astroquery` is installed (`pip install astroquery`). Verify the exact module name for the service you are trying to use (e.g., `astroquery.simbad`, `astroquery.mast`). -
requests.exceptions.HTTPError: 401 Client Error: Unauthorized for url: ...
cause The query to a remote service failed due to a lack of proper authentication (missing API key/token or invalid credentials).fixAuthenticate with the respective service using its `login()` method (e.g., `Mast.login(token='YOUR_API_TOKEN')`). Check documentation for required authentication methods for the specific service. -
astroquery.exceptions.RemoteServiceError: No results found for query: ...
cause The query parameters provided did not match any data in the remote service, or the service returned an empty set of results.fixReview your query parameters (object name, coordinates, radius, filters, etc.) for typos or logical errors. Try a broader query first, then narrow it down. Check the service's own web interface to confirm data existence. -
TypeError: 'Table' object is not callable
cause Attempted to access rows or columns of an `astropy.table.Table` object using function-call syntax (e.g., `table(0)` or `table('column')`) instead of indexing syntax.fixAccess rows and columns using square bracket indexing: `table[0]` for the first row, `table['column_name']` for a column. For specific cells, use `table['column_name'][0]`.
Warnings
- gotcha Many Astroquery services (e.g., MAST, Gaia, ESO) require authentication (login with API tokens or credentials) for full functionality or to bypass rate limits. Failing to authenticate will result in `HTTPError` (e.g., 401 Unauthorized or 403 Forbidden) or limited access.
- gotcha Queries can return extremely large datasets, especially for broad searches. This can lead to long download times, high memory consumption, and potential system instability. Always be mindful of the scope of your query.
- breaking When using the `TapPlus` class, the `upload_table` method no longer allows table names that contain a dot ('.'). This was introduced as a security and consistency measure.
- gotcha Astroquery results are typically `astropy.table.Table` objects, with columns often carrying `astropy.units`. Mixing or ignoring these units can lead to `astropy.units.UnitsError` or incorrect calculations.
Install
-
pip install astroquery
Imports
- Simbad
from astroquery.simbad import Simbad
- Mast
from astroquery.mast import Mast
- Gaia
from astroquery.gaia import Gaia
- Ned
from astroquery.ned import Ned
Quickstart
from astroquery.simbad import Simbad
import astropy.units as u
# Query Simbad for a celestial object
result_table = Simbad.query_object("M1")
if result_table:
print(f"Object Name: {result_table['MAIN_ID'][0]}")
print(f"RA: {result_table['RA'][0]}, Dec: {result_table['DEC'][0]}")
# Example of a cone search around a coordinate
from astropy.coordinates import SkyCoord
coord = SkyCoord('10h00m00s +10d00m00s', frame='icrs')
radius = 5 * u.arcmin
cone_search_results = Simbad.query_region(coord, radius=radius)
if cone_search_results:
print(f"\nObjects found near {coord.ra.deg:.2f}, {coord.dec.deg:.2f} (radius {radius.value:.0f} arcmin):")
for row in cone_search_results[:3]: # Print first 3 results
print(f" - {row['MAIN_ID']} (RA: {row['RA']}, Dec: {row['DEC']})")
else:
print("\nNo objects found for the cone search.")