Skip to content

Instantly share code, notes, and snippets.

@bmorris3
Created July 29, 2024 15:31
Show Gist options
  • Save bmorris3/bdc64245a19b8385f162dffba0bb82ba to your computer and use it in GitHub Desktop.
Save bmorris3/bdc64245a19b8385f162dffba0bb82ba to your computer and use it in GitHub Desktop.
Science validation for cubeviz spectral extraction in spacetelescope/jdaviz#3088
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"id": "df738114-895b-431a-9272-966e529e8239",
"metadata": {},
"source": [
"# Spectral extraction demo with flux calibration source delta UMi\n",
"\n",
"Brett Morris\n",
"\n",
"\n",
"\n",
"First, download the flux calibrated model spectrum of delta UMi:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b256eb04-9b33-4c7e-b574-50400a2ad814",
"metadata": {},
"outputs": [],
"source": [
"import warnings\n",
"from jdaviz import Cubeviz\n",
"from glue.core.roi import CircularROI\n",
"\n",
"import astropy.units as u\n",
"from astropy.io import fits\n",
"from astropy.table import QTable\n",
"from astropy.io import fits\n",
"\n",
"from specutils import Spectrum1D\n",
"from specutils.manipulation import FluxConservingResampler\n",
"\n",
"from regions import PixCoord, CirclePixelRegion\n",
"\n",
"start_slice, (x, y, radius), expected_rtol, uri, calspec_url = (\n",
" 4.85, (20.5, 17, 12), 1e-5,\n",
" \"mast:jwst/product/jw01524-o003_t002_miri_ch1-shortmediumlong_s3d.fits\",\n",
" \"https://archive.stsci.edu/hlsps/reference-atlases/cdbs/current_calspec/delumi_mod_005.fits\"\n",
") # delta UMi\n",
"\n",
"calspec_fitsrec = fits.getdata(calspec_url)\n",
"column_units = [u.AA] + 2 * [u.Unit('erg s-1 cm-2 AA-1')]\n",
"spectra_table = QTable(calspec_fitsrec, units=column_units)\n",
"model_spectrum = Spectrum1D(\n",
" flux=spectra_table['FLUX'],\n",
" spectral_axis=spectra_table['WAVELENGTH']\n",
")"
]
},
{
"cell_type": "markdown",
"id": "4bad414b-5a5c-42b9-86a0-2bb65cca2950",
"metadata": {},
"source": [
"Launch Cubeviz:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "bf31b096-0245-44c4-8b58-5b80b9dfe901",
"metadata": {},
"outputs": [],
"source": [
"cubeviz_helper = Cubeviz()\n",
"cubeviz_helper.show()"
]
},
{
"cell_type": "markdown",
"id": "59d35e30-d653-4c26-9751-b79d1569808a",
"metadata": {},
"source": [
"Load the JWST/MIRI observations of delta UMi, extract its spectrum:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5bfd005e-4ea1-41ea-9221-7aabf10e0192",
"metadata": {},
"outputs": [],
"source": [
"# load observations into Cubeviz\n",
"with warnings.catch_warnings():\n",
" warnings.simplefilter('ignore')\n",
" cubeviz_helper.load_data(uri, cache=True)\n",
"\n",
"# add a subset with an aperture centered on each source\n",
"aperture = CirclePixelRegion(center=PixCoord(x, y), radius=radius)\n",
"\n",
"cubeviz_helper.load_regions(aperture)\n",
"\n",
"# set the slice to the blue end of MIRI CH1\n",
"slice_plugin = cubeviz_helper.plugins['Slice']\n",
"slice_plugin.value = 4.85\n",
"\n",
"# run a conical spectral extraction\n",
"spectral_extraction = cubeviz_helper.plugins['Spectral Extraction']\n",
"spectral_extraction.aperture = 'Subset 1'\n",
"spectral_extraction.wavelength_dependent = True\n",
"spectral_extraction._obj.results_label = 'conical-extraction'\n",
"extracted_spectrum = spectral_extraction.extract()\n",
"\n",
"# resample the model spectrum on the same wavelengths as\n",
"# the observed spectrum:\n",
"resampled_spectrum = FluxConservingResampler()(\n",
" model_spectrum, extracted_spectrum.wavelength\n",
")\n",
"\n",
"# load model spectrum:\n",
"cubeviz_helper.specviz.load_data(resampled_spectrum, data_label='calspec model')"
]
},
{
"cell_type": "markdown",
"id": "d68f92c1-0628-4c44-8062-606a98fa9e04",
"metadata": {},
"source": [
"Make other layers in the spectrum viewer invisible:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7c31152e-f597-4f82-821f-6e1d9dfaa312",
"metadata": {},
"outputs": [],
"source": [
"spectrum_viewer = cubeviz_helper.viewers['spectrum-viewer']._obj\n",
"\n",
"for layer in spectrum_viewer.layers:\n",
" if layer.layer.label not in ['calspec model', 'conical-extraction']:\n",
" layer.visible = False"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "28a87e09-1535-482e-8bfa-66bc8e0454d1",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.5"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment