Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

IRSA Tutorials

Download a collection of SPHEREx Spectral Image cutouts as a multi-extension FITS file

1. Learning Goals

2. SPHEREx Overview

SPHEREx is a NASA Astrophysics Medium Explorer mission that launched in March 2025. During its planned two-year mission, SPHEREx will obtain 0.75-5 micron spectroscopy over the entire sky, with deeper data in the SPHEREx Deep Fields. SPHEREx data will be used to:

The community will also mine SPHEREx data and combine it with synergistic data sets to address a variety of additional topics in astrophysics.

More information is available in the SPHEREx Explanatory Supplement.

3. Imports

The following packages must be installed to run this notebook.

<astropy.visualization.units.quantity_support.<locals>.MplQuantityConverterFormatted at 0x746d8026d400>

4. Specify inputs and outputs

Specify a right ascension, declination, cutout size, and a SPHEREx bandpass (e.g., ‘SPHEREx-D2’).

In this example, we are creating cutouts of the Pinwheel galaxy (M101) for the SPHEREx detector D2.

5. Query IRSA for a list of cutouts that satisfy the criteria specified above.

Here we show how to use astroquery’s IRSA TAP interface to retrieve all images that overlap with the position defined above. This query will retrieve a table of URLs that link to the MEF cutouts. Each row in the table corresponds to a single cutout and includes the data access URL and an observation timestamp. The results are sorted from oldest to newest.

Time to do TAP query: 16.57 seconds.
Number of images found: 84

6. Define a function that processes a list of SPHEREx Spectral Image Cutouts

This function takes in a row of the catalog that we created above and does the following:

Note that the values of the rows are being added in place.

We provide a small convenience wrapper around process_cutout that is used in the rest of this notebook. It catches transient read errors and simply skips those cutouts, which is sufficient for this demonstration. For science use cases, users may instead want to implement their own retry logic or error handling strategy.

7. Download the Cutouts

This process can take a while. If run in series, it can take about 5 minutes for 700 images on a typical laptop machine. Here, we therefore exploit two different methods. First we show the serial approach and next we show how to parallelize the methods. The latter can be run on many CPUs and is therefore significantly faster.

7.1 Serial Approach

First, we implement the serial approach -- a simple for loop. Before that, we turn the results into an astropy table and add some place holders that will be filled in by the process_cutout() function.

Time to create cutouts in serial mode: 1.40 minutes.
Failed to get the following cutouts:
                                                                                       uri                                                                                       
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
https://irsa.ipac.caltech.edu/ibe/data/spherex/qr2/level2/2025W19_2B/l2b-v20-2025-247/2/level2_2025W19_2B_0346_1D2_spx_l2b-v20-2025-247.fits?center=210.80227,54.34895d&size=0.1
https://irsa.ipac.caltech.edu/ibe/data/spherex/qr2/level2/2026W21_2A/l2b-v25-2026-147/2/level2_2026W21_2A_0213_3D2_spx_l2b-v25-2026-147.fits?center=210.80227,54.34895d&size=0.1

7.2 Parallel Approach

Next, we implement parallel processing, which will make the cutout creation faster. The maximal number of workers can be limited by setting the max_workers argument. The choice of this value depends on the number of cores but also on the number of parallel calls that can be digested by the IRSA server.

Again, before running the cutout processing we define some place holders.

Time to create cutouts in parallel mode: 0.10 minutes.
Failed to get the following cutouts:
                                                                                       uri                                                                                       
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
https://irsa.ipac.caltech.edu/ibe/data/spherex/qr2/level2/2025W20_1C/l2b-v20-2025-247/2/level2_2025W20_1C_0373_1D2_spx_l2b-v20-2025-247.fits?center=210.80227,54.34895d&size=0.1
https://irsa.ipac.caltech.edu/ibe/data/spherex/qr2/level2/2025W20_1C/l2b-v20-2025-247/2/level2_2025W20_1C_0373_2D2_spx_l2b-v20-2025-247.fits?center=210.80227,54.34895d&size=0.1
https://irsa.ipac.caltech.edu/ibe/data/spherex/qr2/level2/2025W21_2C/l2b-v20-2025-250/2/level2_2025W21_2C_0401_2D2_spx_l2b-v20-2025-250.fits?center=210.80227,54.34895d&size=0.1
https://irsa.ipac.caltech.edu/ibe/data/spherex/qr2/level2/2025W49_2A/l2b-v20-2025-342/2/level2_2025W49_2A_0426_3D2_spx_l2b-v20-2025-342.fits?center=210.80227,54.34895d&size=0.1
https://irsa.ipac.caltech.edu/ibe/data/spherex/qr2/level2/2025W49_2A/l2b-v20-2025-342/2/level2_2025W49_2A_0650_4D2_spx_l2b-v20-2025-342.fits?center=210.80227,54.34895d&size=0.1
https://irsa.ipac.caltech.edu/ibe/data/spherex/qr2/level2/2025W50_2A/l2b-v20-2025-349/2/level2_2025W50_2A_0072_2D2_spx_l2b-v20-2025-349.fits?center=210.80227,54.34895d&size=0.1
https://irsa.ipac.caltech.edu/ibe/data/spherex/qr2/level2/2025W50_2A/l2b-v20-2025-349/2/level2_2025W50_2A_0072_3D2_spx_l2b-v20-2025-349.fits?center=210.80227,54.34895d&size=0.1
https://irsa.ipac.caltech.edu/ibe/data/spherex/qr2/level2/2025W50_2A/l2b-v20-2025-349/2/level2_2025W50_2A_0129_1D2_spx_l2b-v20-2025-349.fits?center=210.80227,54.34895d&size=0.1
https://irsa.ipac.caltech.edu/ibe/data/spherex/qr2/level2/2026W20_2A/l2b-v25-2026-140/2/level2_2026W20_2A_0464_1D2_spx_l2b-v25-2026-140.fits?center=210.80227,54.34895d&size=0.1
https://irsa.ipac.caltech.edu/ibe/data/spherex/qr2/level2/2026W20_2A/l2b-v25-2026-140/2/level2_2026W20_2A_0464_2D2_spx_l2b-v25-2026-140.fits?center=210.80227,54.34895d&size=0.1
                                                                                                                                                                             ...
https://irsa.ipac.caltech.edu/ibe/data/spherex/qr2/level2/2026W23_2A/l2b-v25-2026-160/2/level2_2026W23_2A_0336_2D2_spx_l2b-v25-2026-160.fits?center=210.80227,54.34895d&size=0.1
https://irsa.ipac.caltech.edu/ibe/data/spherex/qr2/level2/2026W23_2A/l2b-v25-2026-160/2/level2_2026W23_2A_0336_3D2_spx_l2b-v25-2026-160.fits?center=210.80227,54.34895d&size=0.1
https://irsa.ipac.caltech.edu/ibe/data/spherex/qr2/level2/2026W23_2A/l2b-v25-2026-160/2/level2_2026W23_2A_0336_4D2_spx_l2b-v25-2026-160.fits?center=210.80227,54.34895d&size=0.1
https://irsa.ipac.caltech.edu/ibe/data/spherex/qr2/level2/2026W24_2B/l2b-v25-2026-166/2/level2_2026W24_2B_0379_1D2_spx_l2b-v25-2026-166.fits?center=210.80227,54.34895d&size=0.1
https://irsa.ipac.caltech.edu/ibe/data/spherex/qr2/level2/2026W25_2A/l2b-v25-2026-174/2/level2_2026W25_2A_0214_1D2_spx_l2b-v25-2026-174.fits?center=210.80227,54.34895d&size=0.1
https://irsa.ipac.caltech.edu/ibe/data/spherex/qr2/level2/2026W25_2A/l2b-v25-2026-174/2/level2_2026W25_2A_0279_1D2_spx_l2b-v25-2026-174.fits?center=210.80227,54.34895d&size=0.1
https://irsa.ipac.caltech.edu/ibe/data/spherex/qr2/level2/2026W25_2A/l2b-v25-2026-174/2/level2_2026W25_2A_0311_1D2_spx_l2b-v25-2026-174.fits?center=210.80227,54.34895d&size=0.1
https://irsa.ipac.caltech.edu/ibe/data/spherex/qr2/level2/2026W26_1A/l2b-v25-2026-180/2/level2_2026W26_1A_0178_1D2_spx_l2b-v25-2026-180.fits?center=210.80227,54.34895d&size=0.1
https://irsa.ipac.caltech.edu/ibe/data/spherex/qr2/level2/2026W26_1A/l2b-v25-2026-180/2/level2_2026W26_1A_0361_1D2_spx_l2b-v25-2026-180.fits?center=210.80227,54.34895d&size=0.1
https://irsa.ipac.caltech.edu/ibe/data/spherex/qr2/level2/2026W26_1A/l2b-v25-2026-180/2/level2_2026W26_1A_0500_1D2_spx_l2b-v25-2026-180.fits?center=210.80227,54.34895d&size=0.1
Length = 37 rows

8. Create a summary table HDU with renamed columns

In the following, we continue to use the output of the parallel mode. The following cell does the following:

9. Create the final MEF

Now, we create a primary HDU and combine it with the summary table HDU and the cutout image and wavelength HDUs to a final HDU list. The primary header is left blank because the cutouts generally come from different processing versions, so no single VERSION would be correct for all of them; the version column in CUTOUT_INFO above records each cutout’s version.

10. Write the final MEF

Finally, we save the full HDU list to a multi-extension FITS file to disk. We use overwrite=False so that re-running this cell does not silently clobber an existing output file; delete the file first (or change output_filename above) if you want to regenerate it.

11. Test and visualize the final result

We can now open the new MEF FITS file that we have created above.

Loading the summary table (including wavelength information of each cutout) is straight forward:

Let’s also extract the first 10 images (or all of the extracted cutouts if less than 10).

Plot the images with the wavelength corresponding to their central pixel. We read central_wavelength as an astropy Quantity (via the column’s .quantity property, which picks up the um unit written into the FITS header) rather than a bare float, so the unit travels with the value instead of being assumed.

<Figure size 1500x500 with 10 Axes>

Acknowledgements

About this notebook

Updated: 5 March 2026

Contact: IRSA Helpdesk with questions or problems.

Runtime: As of the date above, this notebook takes about 3 minutes to run to completion on a machine with 8GB RAM and 4 CPU.