{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Introduction\n", "\n", "This notebook demonstrates how to visualize and analyze JWST data cubes, including how to:\n", "\n", "* Launch [CubeViz](https://jdaviz.readthedocs.io/en/latest/cubeviz/index.html)\n", "* [Load data](https://jdaviz.readthedocs.io/en/latest/cubeviz/import_data.html) from a notebook cell\n", "* Adjust [display parameters](https://jdaviz.readthedocs.io/en/latest/cubeviz/displaycubes.html#display-settings)\n", "* Subtract continuum from a datacube\n", "* Select [spectral regions](https://jdaviz.readthedocs.io/en/latest/specviz/displaying.html#spectral-regions) for further analysis \n", "* Use the [Moment Maps](https://jdaviz.readthedocs.io/en/latest/cubeviz/plugins.html#moment-maps) plugin to map a spectral feature\n", "* Use the Moment Maps plugin to extract a velocity map\n", "* [Model fitting](https://jdaviz.readthedocs.io/en/latest/cubeviz/plugins.html#model-fitting) for every spaxel in a cube.\n", "* Use a cube model to create feature maps.\n", "\n", "Updated on June 10, 2022.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Data\n", "\n", "We use a simulated 4-point dithered observation of a point source produced using [MIRISim](https://www.stsci.edu/jwst/science-planning/proposal-planning-toolbox/mirisim), \n", "with the spectrum of active galaxy NGC 5728. It has been processed with the [JWST pipeline](https://jwst-pipeline.readthedocs.io/en/latest/) (Detector1/Spec2/Spec3). The output is constructed only for a \n", "single MRS channel/band (2C).\n", "\n", "For the NIRSpec IFU data, a point source (quasar) was simulated using the NIRSpec Instrument Performance Simulator (IPS), then run through the JWST Spec2 and Spec3 pipelines. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Imports " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* [_jdaviz_](https://jdaviz.readthedocs.io/en/latest/) : Cubeviz data visualization tool\n", "* [_astropy.io_](https://docs.astropy.org/en/stable/io/fits/index.html) for reading and writing FITS cubes and images\n", "* [_astropy.utils_](https://docs.astropy.org/en/stable/utils/index.html) for downloading files from URLs" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from jdaviz import Cubeviz\n", "from astropy.io import fits\n", "from astropy.utils.data import download_file" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Visualize a simulated MIRI MRS data cube\n", "\n", "* Load and display the data\n", "* Select a spectral region\n", "* Create moment maps\n", "\n", "Execute the next cell to launch Cubeviz, then follow the instructions for each task enumerated in the cell directly below the Cubeviz app." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": false }, "outputs": [], "source": [ "cubeviz = Cubeviz()\n", "cubeviz.app" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### UI Instructions:\n", "#### Task 1: Load the cube and view it\n", "* Load the MIRI MRS datacube into Cubeviz using the next code cell below\n", "* Open the Display menu \n", "* In the Layer tab, change the stretch to Logarithmic, 95 percentile to see the target PSF wings.\n", "* Scrub through the cube using the Slice slider.\n", "\n", "#### Task 2: Select a spectral region for further analysis\n", "* Select 'No selection (create new)' in the Subsets menu in the Cubeviz top banner.\n", "* Select a spectral subset region in spectrum viewer, using the region selection tool under the Tools icon, centered on a spectral feature of interest (e.g., the 10.5 micron PAH feature)\n", "\n", "#### Task 3: Extract an image of a spectral feature using the Moment Maps plugin\n", "* Find the Moment Maps data analysis Plugin by clicking the 'Lego' icon at upper right.\n", "* Select a dataset with the Data dropdown (e.g. SCI extension of our cube).\n", "* Select a spectral region in the Spectral Region dropdown (e.g. Subset 1).\n", "* Enter the Moment (0 for flux, 1 for centroid, 2 for dispersion).\n", "* Press 'Calculate' \n", "* Use the 2nd cube/image viewer to display the moment 0 image. Click the \"Data\" button, deselect the 'ERR' extension checkbox, and select the 'Moment' dataset that was generated by the previous step. \n", "* Adjust the Stretch in the viewer and change the colormap to inferno\n", "\n", "#### Task 4: Model the continuum + broad dust features (PAHs) \n", "* Open the Model Fitting Plugin and resize the tray to make the model parameters readable\n", "* Select the dataset (SCI extension of our cube)\n", "* Create 3 model components: Linear Model L, Gaussian Model G1, and Gaussian Model G2, with the following starting parameters: \n", "L: Leave starting parameter guess as is \n", "G1: ampl = 3.5E5 MJy/sr, stddev = 5e-8 m, mean = 1.05E-5 m = fixed (10.5 um)\n", "G2: ampl = 1.5E6 MJy/sr, stddev = 1e-7 m = fixed, mean = 1.126E-5 m = fixed (11.25 um)\n", "* Enter the model to fit in the Model Equation Editor: L + G1 + G2\n", "* Enter \"Model\" as the Model Label\n", "* Press Fit\n", "* Press Apply to Cube\n", "* Deselect all data in cube viewer 2, then select Model [Cube] to display the modeled continuum + PAH cube\n", "* Scrub through the Model cube\n", "\n", "#### Task 5: Subtract the continuum + PAH model cube from the data cube \n", "* Run the continuum subtraction cell below, which reads in the science data, gets the model cube from the middle viewer, subtracts the continuum, then loads the continuum-subtracted cube into Cubeviz\n", "* The continuum-subtracted spectrum appears in the spectrum viewer on loading.\n", "\n", "#### Task 6: Extract a velocity map from the continuum-subtracted data using the Moment Maps plugin\n", "* Open the Moment Maps Plugin\n", "* Select the continuum-subtracted dataset (Unknown HDU Object [Sci])\n", "* Follow the procedure for Task 3; this time for Moment 1\n", "* Use the 3rd cube/image viewer to display the Moment 1 image\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Load Science Datacube into Cubeviz" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "#filename = \"l3_ch2-long_s3d.fits\"\n", "filename = \"https://stsci.box.com/shared/static/ezl49sugxn379wbo4tveuromnde8e6lv.fits\"\n", "miricube_file = download_file(filename)\n", "cubeviz.app.load_data(miricube_file)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Continuum Subtraction" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": false }, "outputs": [], "source": [ "# Open and inspect the science data with astropy.fits.open\n", "with fits.open(miricube_file, memmap=False) as miricube:\n", " miricube.info()\n", " sci = miricube['SCI'].data\n", " #Retrieve the continuum + PAH model cube from middle cube viewer in Cubeviz\n", " modeled_cube = cubeviz.app.get_data_from_viewer(\"uncert-viewer\", \"Model [Cube] 1\")\n", " #Inspect the data dictionary and retrieve the continuum model flux cube\n", " print(modeled_cube)\n", " model_flux = modeled_cube[\"flux\"]\n", " #Subtract the continuum model from the science data\n", " sci_contsub = sci-model_flux\n", " #Load the continuum subtracted data back into Cubeviz\n", " miricube['SCI'].data = sci_contsub\n", " cubeviz.app.load_data(miricube)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Inspect the continuum model parameters" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#c_model = cubeviz.get_models(model_label=\"Model1\")\n", "#print(c_model)\n", "#print(c_model[\"slope_0\"])\n", "cubeviz.get_models()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## NIRSpec IFU" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cubeviz2 = Cubeviz()\n", "cubeviz2.app" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#Download the cube data from Box\n", "filename = \"https://stsci.box.com/shared/static/ff0bj31acot1272x5qq2clbmto3s6v4f.fits\"\n", "ifucube_file = download_file(filename, cache=True)\n", "cubeviz2.app.load_data(ifucube_file)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## MANGA IFU" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": false }, "outputs": [], "source": [ "cubeviz3 = Cubeviz()\n", "cubeviz3.app" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fn = download_file('https://stsci.box.com/shared/static/28a88k1qfipo4yxc4p4d40v4axtlal8y.fits', cache=True)\n", "cubeviz3.load_data(fn)" ] }, { "cell_type": "code", "execution_count": null, "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.9.13" } }, "nbformat": 4, "nbformat_minor": 4 }