This page describes how to search and download SDSS data in Python using astroquery.mast
On this page...
Searching for SDSS data with astroquery.mast
The Python package astroquery.mast can be used for programmatically querying and downloading data from MAST. Accessing SDSS observations from within Astroquery can be done by filtering for obs_collection='SDSS', similar to many other datasets. You can follow this API Advanced Search, customizing it for SDSS. The list of available query filters can be found here in the MAST API guide and in the astroquery.mast documentation. A few example queries are shown here.
Querying all SDSS data
For example, to query for all SDSS data, use the Observations.query_criteria() function and the obs_collection keyword:
from astroquery.mast import Observations # Query for all SDSS data obs = Observations.query_criteria(obs_collection='SDSS') # Display results obs
In this example, use len(obs) to print the number of results to see how many observations match the query specifications. This is helpful to check before downloading files to make sure you don't download more than you were expecting, or for catching cases where your query did not return any results.
# Print length of results len(obs)
Using the pagesize and page keywords can help limit the number of search results and speed up large queries:
# Query for first 10 rows of SDSS data obs = Observations.query_criteria(obs_collection='SDSS', pagesize=10, page=1) # Display results obs
Search by survey
To search for data in a specific survey, use the provenance_name keyword:
# Query for data in a specific survey apogee_obs = Observations.query_criteria(provenance_name='APOGEE') eboss_obs = Observations.query_criteria(provenance_name='eBOSS') manga_obs = Observations.query_criteria(provenance_name='MaNGA') # Display results manga_obs
Search by target or field name
Using the obs_id or target_name keywords to further refine your search for different observations:
# Use target_name to query APOGEE observations of a specific star obs = Observations.query_criteria(provenance_name='APOGEE', target_name='2M06015966-6511312') # Use obs_id query for all APOGEE data by field name, for example the LMC obs = Observations.query_criteria(provenance_name='APOGEE', obs_id='*lmc*')
Downloading SDSS data with astroquery.mast
To download SDSS products using astroquery, use the Observations.get_product_list() to search for files associated with a give observation (or list of observations), an download them using the Observations.download_products() function.
# query for MaNGA observation 8485-3701 obs = Observations.query_criteria(obs_collection='SDSS', provenance_name='MaNGA', obs_id='sdss_manga_8485-3701') # get data products associated with the observation products = Observations.get_product_list(obs) # Check the number of files before downloading print(len(products)) # download products Observations.download_products(products)
By default, Observations.get_product_list() returns all products associated with that observation. You can use other keywords to limit products. For example, use the mrp_only=True flag to return only the "Minimum Recommended Product" (MRP) files, or extension='fits' to only download the FITS files. The list of possible fields you can use to filter product results are listed here.
The Observations.download_products() function also has several useful keyword options. The astroquery.mast documentation contains more detail on how to filter and download products, but a few examples are highlighted here:
- The "
download_dir"keyword can be used to specify what directory to download the files to. - Set "
curl_flag = True"to generate a curl script that can be used to download the files at a later time. - Use "
mrp_only=True"to download the Minimum Recommended Products only
Downloading Individual Files
Data URIs
The data Uniform Resource Identifier (URI) is a unique string associated with each file in MAST. If you know the URI of a particular file, you can download the file directly using the Observations.download_file() function which takes the dataURI as an argument. All SDSS URIs start with mast:SDSS/ and end with the file name, but vary by survey. When querying data with the Observations.get_product_list() function, one of the columns that is returned is "dataURI" which lists the URI for each file. You can also find the data URI for a specific product using the MAST Portal from the "File Details" panel of the Download Basket.
The easiest way to download SDSS catalog files using astroquery.mast is by using the dataURI. Table 1 below lists the Data URIs for the SDSS summary catalogs in MAST as a quick reference.
| Survey | Catalog | dataURI | Read More |
|---|---|---|---|
APOGEE | allStar | mast:SDSS/apogee/allStar-dr17-synspec_rev1.fits | |
allVisit | mast:SDSS/apogee/allVisit-dr17-synspec_rev1.fits | ||
| eBOSS | spAll | mast:SDSS/eboss/spAll-v5_13_2.fits | eBOSS Data Products |
| spAllLine | mast:SDSS/eboss/spAllLine-v5_13_2.fits | ||
| MaNGA | drpall | mast:SDSS/manga/drpall-v3_1_1.fits | |
| dapall | mast:SDSS/manga/dapall-v3_1_1-3.1.0.fits | ||
| SDSS Legacy Imaging | photoRunAll | mast:SDSS/sdss/imaging/photoRunAll-dr13.fits | |
| SDSS Legacy Spectra | specObj | mast:SDSS/sdss/spectro/specObj-dr17.fits |
Table 1 - A list of the dataURI for each survey's summary catalogs. Use this table as a quick reference for downloading the SDSS catalog files using astroquery.mast.
Downloading a file from the Data URI
To download a file using the dataURI, use the Observations.download_file() function. For example, this line of code downloads the MaNGA "drpall" catalog.
# download a single file, given a MAST data URI (useful for catalog files)
Observations.download_file("mast:SDSS/manga/drpall-v3_1_1.fits")
The download_file() function also has several keyword argument options for customizing your download destination (local_path) among other parameters. The astroquery.mast documentation contains the full list of options.
Note on astroquery.mast version
In general, we recommend using the latest version of astroquery available for best performance. To download APOGEE files, you must update astroquery to version 0.4.8 or higher. If you are using an older version of astroquery, you may encounter a "File Not Found" error when attempting to download APOGEE observations containing the "+" character in the observation name. Special character URL encoding is only supported in astroquery.mast version 0.4.8 and higher.
To update astroquery to the latest version, run:
pip install --upgrade astroquery