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

SPHEREx Source Discovery Tool IRSA Demo

The SPHEREx Source Discovery Tool is the Python package spherex_source_discovery_tool (included in this directory), which is used to discover and extract sources from SPHEREx Spectral Images and visualize their spectra. This notebook demonstrates how to use it.

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. Requirements

3.1 On local machines

All necessary packages and tools (e.g., SExtractor) are listed in the conda-spherex_sdt.yml file.

To create a conda environment with the dependencies on your local machine use:

conda env create --file conda-spherex_sdt.yml

Then, make the environment available in the list of kernels for your JupyterLab:

conda activate spherex_sdt
python -m ipykernel install --user --name=spherex_sdt

To use the environment in your Jupyter notebooks, either start JupyterLab in that environment by typing

conda activate spherex_sdt
jupyter-lab

or select the environment spherex_sdt in your Jupyter Notebook using the dropdown on the upper left.

3.2 On Fornax

Installing the conda environment on the NASA Fornax Science Console needs slightly different steps. These can be reviewed in the documentation create a new environment.

In order to install this specific conda environment on Fornax, the file name of the yml file specifically needs to be in the format conda-*.yml. The yml file distributed here is already provided in that format (conda-spherex_sdt.yml). Once this is set, open a new terminal (click on the large “+” button right under the “File” menu tab) and type to following command inside the same directory where the yml file is located:

setup-conda-env --user

Note that we use the --user option here, which will keep the environment available for subsequent Fornax sessions.

To use the environment in your Jupyter notebooks on Fornax, directly select the environment spherex_sdt in your Jupyter Notebook using the dropdown on the upper left.

4. Imports

5. Initialize Ephemeral Query Parameters

6. Search for SPHEREx Spectral Images

We search first search for all the available SPHEREx images around a given position on the sky.

Here, we choose the random position at R.A. = 150.7817877 degrees and Decl. = 3.3502204 degrees, which is close to the COSMOS field. We also choose a search radius of 50 arc-seconds.

We then search for the SPHEREx spectral images that overlap with the position in the search radius defined above. For this we use the IRSA module in astroquery and the Simple Image Access (SIA) API.

Note that we have used the collection spherex_qr2 in this case. All the available collections are documented at SPHEREx Data Access: Application Program Interfaces (APIs).

Next, we set up the Firefly client and use it to visualize one image with the search coordinates (and search radius) indicated.

There are two ways to run Firefly from the JupyterLab environment:

The second option requires setting some environment variables and installing the Firefly JupyterLab extension. See here for more information on how to do that. More information can be found in the Firefly documentation.

Below, we give the two options to open Firefly.

Now, we select two images, one each in Detector 2 (D2) and Detector (D4). For simplicity, we just select the first image in Detector 2 and the first in Detector 4. Detector 2 covers 1.10 - 1.62 μ\mum and Detector 4 covers 2.42 - 3.82 μ\mum.

We can use two different methods to obtain the images:

We show both methods below, the first one for the detector 2 spectral image and the second one for the detector 4 spectral image.

7. Load the Data

Next, we download the two SPHEREx spectral images. We download them directly from the cloud.

For this, we first define a handy function to obtain the S3 cloud path.

Next we download the images.

8. Remove Local Background, Create Masks, and Reproject

Before we can subtract the images, we have to do some preprocessing. This includes:

First, we convert from surface brightness to flux units using the solid angle pixel map for each detector. We download these calibrations from the cloud using the S3 paths hardcoded below. Then, we use astropy.units to convert the image data from MJy/sr to μ\muJy/arcsec2^2. This quantity is multiplied by the solid angle pixel map in arcsec2^2 to get image data in units of μ\muJy.

After downloading the solid angle pixel map calibration products for each detector, we convert the images from brightness (MJy/sr) to spectral flux density (μ\muJy) and update the corresponding units in the headers of the imates.

Next, we remove the background from both images. We estimate the background using the sep Python package, which allows fitting a non-parametric background to an image in user-defined zones.

Next we create masks (which also need to be reprojected). The merged masks will be applied later to the difference image. Note that flags 2097152 (which corresponds to 221, so bit 21), is assigned to pixel which are not contaminated by image errors, transients, cosmic rays, etc. Note that we ignore the OVERFLOW flag, which is coincident with the SUR_ERROR flag. See the SPHEREx bitmask flag description for more information about the different flag bits. We therefore choose these as good pixels and flag the other ones. For simplicity, we set the bad pixels to np.nan.

Finally, we reproject the images and masks to the same pixel grid. We reproject D2 on D4. For this we use reproject_exact() (for flux conservation) and reproject_interp() (for mask) from the reproject Python package.

We use bokeh to visualize the two detector images as well as the reprojected D4 image. See the file spherex_source_discovery_tool/bokeh_viz.py for more information on the used function plot_overlap_trio() to learn how to use bokeh.

9. Generate Cutouts and Subtracted Color Image

For this example, we only use parts of the image. For this, we first generate cutouts of the D4 image as well as the reprojected D2 image.

Next, we subtract the images. For keeping track of the wavelength and file names, we create two handy dictionaries.

Finally, we use bokeh to show interactive representation of the two images as well as the subtracted image inside this Jupyter Notebook.

10. Run SExtractor on the Subtracted Color Image

A use case could be to search for red sources such as galaxies with an Active Galactic Nucleus (AGN), which are manifested by red mid-infrared colors. Such red sources can be identified on the subtracted image.

In the following, we run SExtractor on all the images (reprojected D2, D4, and subtracted image) to identify sources. Before that, however, we have to save the subtracted image to disk (note that the D2 and D4 cutouts are already saved to disk).

Now, we run SExtractor. This software needs a few parameter files such as a configuration and parameter files as well as some others (for example a convolution filter). These files are provided in the spherex_source_discovery_tool directory. For SExtractor to find them, we have to give it absolute path names to these files. We use os.path.realpath() in the following to translate relative paths to absolute paths. Finally, we run SExtractor on the last lines.

11. Select Sources Interactively

In the following, we show two ways to visualize the extracted sources on the images and how to interactively select interesting sources. (Note that you can always select the source automatically in Python using, for example, astropy tables instead of doing it interactively.)

First, we read in the SExtractor catalog. Note that we want to change ALPHA_J2000 and DELTA_J2000 to the more universal ra and dec column names. This is especially important for Firefly to be able to read the table out of the box and mark the source in the table on the images.

11.1 Using bokeh visualizations

First, we demonstrate how we can use bokeh to create an interactive dashboard to select sources.

Alternatively, the sources can be easily selected in Python itself by defining a selection function.

11.2 Using Firefly visualization

Alternatively to bokeh, we can use the Firefly application to show the image including the data table. First we reinitiate Firefly, which will again open a new tab called “Firefly Viewer”. As mentioned above, we give again two options for initiating Firefly; in a new browser tab or a new JupterLab tab.

Then, we load the images and finally the catalog to the application. Firefly takes care of the rest!

12. Perform Aperture Photometry on Selected Sources

Finally, we compute the SPHEREx spectra for selected sources. This includes downloading all the available SPHEREx spectral image cutouts for the sources and then measuring their photometry to generate a spectrum. For simplicity, we here perform aperture photometry but include background subtraction.

12.1 Download SPHEREx spectral image cutouts

Here we select one source to proceed (multiple sources can be measured by looping over the code below). Note that we here directly give it the unique source ID that we defined in the catalog column NUMBER.

We then set up the TAP service as well as the cutout size to run a query for all SPHEREx spectral images that contain the sources.

We then define a handy function that downloads the images.

We add some placeholders and other information to the table so we can keep track of things.

Finally, we can run the download of the cutouts in parallel via the function created above. The cutouts are saved directly in the table.

12.2 Obtain spectrum

Having obtained the cutouts, we can now perform aperture photometry on the cutouts and measure the fluxes as a function of wavelength. See spherex_source_discovery_tool/aperture_photometry.py for more information on the functions used below.

Load the photometry file we just created.

Apply flagging to the extracted 1D-Spectrum.

Finally visualize the spectrum using bokeh in flux and magnitudes!

Acknowledgements

This work has made use of the NASA/IPAC Infrared Science Archive, which is funded by the National Aeronautics and Space Administration and operated by the California Institute of Technology. We acknowledge support from the SPHEREx project under a contract from the NASA/Goddard Space Flight Center to the California Institute of Technology. This research was partly carried out at the California Institute of Technology under a contract with the National Aeronautics and Space Administration (80GSFC18C0011).  The DOI for SPHEREx QR2 data is 10.26131/IRSA629 (SPHEREx Team (2025)).

About this Notebook

Contact: IRSA Helpdesk with questions or problems.

Updated: 18 February 2026

Runtime: approximately 5 minutes

References
  1. SPHEREx Team. (2025). SPHEREx Quick Release Spectral Images. IPAC. 10.26131/IRSA629