From a8b33b94b54bf1b0b3f41f13765dae29555147ca Mon Sep 17 00:00:00 2001 From: Hatice Karatay Date: Wed, 4 Oct 2023 16:43:05 -0400 Subject: [PATCH] Address additional style errors --- .../ero_nirspec_ifu_02729_demo.ipynb | 61 ++++---- .../ero_nirspec_ifu_02732_demo.ipynb | 143 +++++++++--------- ...o_nirspec_ifu_02732_demo_pointsource.ipynb | 86 +++++------ 3 files changed, 142 insertions(+), 148 deletions(-) diff --git a/notebooks/NIRSpec_IFU_spectral_extraction/ero_nirspec_ifu_02729_demo.ipynb b/notebooks/NIRSpec_IFU_spectral_extraction/ero_nirspec_ifu_02729_demo.ipynb index 1faf0938c..9cffce196 100644 --- a/notebooks/NIRSpec_IFU_spectral_extraction/ero_nirspec_ifu_02729_demo.ipynb +++ b/notebooks/NIRSpec_IFU_spectral_extraction/ero_nirspec_ifu_02729_demo.ipynb @@ -84,32 +84,31 @@ "\n", "# ----------------------------------------------General Imports-----------------------------------------------------\n", "\n", - "import numpy as np\n", - "import warnings\n", - "warnings.filterwarnings('ignore') # Set to 'default' to turn warnings back on\n", + "import numpy as np # noqa\n", + "import warnings # noqa\n", + "warnings.filterwarnings('ignore') # Set to 'default' to turn warnings back on\n", "\n", "# --------------------------------------------File Operation Imports------------------------------------------------\n", "\n", - "import glob\n", - "import os\n", - "import json\n", - "from shutil import copy\n", + "import glob # noqa\n", + "import os # noqa\n", + "import json # noqa\n", + "from shutil import copy # noqa\n", "\n", "# --------------------------------------------Astropy/Astroquery Imports--------------------------------------------\n", "\n", - "from astropy.io import fits\n", - "from astropy import wcs\n", - "from astropy.wcs import WCS\n", - "from astropy.visualization import ImageNormalize, ManualInterval, LogStretch, LinearStretch, AsinhStretch\n", - "from astroquery.mast import Observations\n", + "from astropy.io import fits # noqa\n", + "from astropy import wcs # noqa\n", + "from astropy.wcs import WCS # noqa\n", + "from astropy.visualization import ImageNormalize, ManualInterval, LogStretch, LinearStretch, AsinhStretch # noqa\n", + "from astroquery.mast import Observations # noqa\n", "\n", "# ------------------------------------------------Plotting Imports--------------------------------------------------\n", "\n", - "import matplotlib.pyplot as plt\n", - "import matplotlib as mpl\n", - "from matplotlib.patches import Circle\n", - "import matplotlib.gridspec as grd\n", - "from matplotlib import cm\n", + "import matplotlib.pyplot as plt # noqa\n", + "import matplotlib as mpl # noqa\n", + "import matplotlib.gridspec as grd # noqa\n", + "from matplotlib import cm # noqa\n", "\n", "# Use this version for non-interactive plots (easier scrolling of the notebook)\n", "%matplotlib inline\n", @@ -274,12 +273,12 @@ "\n", " # SCI Extension: [Type:ImageHDU Cards:92 Dimensions:(57, 61, 973) Format:float32]\n", " cube = s3d[1].data # Science data\n", - " wcs = WCS(s3d[1].header) # World Coordinate System (WCS) Transformation keywords \n", - " wmap = s3d[4].data # 3-D weight image giving the relative weights of the output spaxels.\n", - " cdelt1 = s3d[1].header['CDELT1']*3600. # Axis 1 coordinate increment at reference point \n", - " cdelt2 = s3d[1].header['CDELT2']*3600. # Axis 2 coordinate increment at reference point \n", - " cdelt3 = s3d[1].header['CDELT3'] # Axis 3 coordinate increment at reference point \n", - " crval3 = s3d[1].header['CRVAL3'] # third axis value at the reference pixel \n", + " wcs = WCS(s3d[1].header) # World Coordinate System (WCS) Transformation keywords # noqa\n", + " wmap = s3d[4].data # 3-D weight image giving the relative weights of the output spaxels.\n", + " cdelt1 = s3d[1].header['CDELT1']*3600. # Axis 1 coordinate increment at reference point # noqa\n", + " cdelt2 = s3d[1].header['CDELT2']*3600. # Axis 2 coordinate increment at reference point # noqa\n", + " cdelt3 = s3d[1].header['CDELT3'] # Axis 3 coordinate increment at reference point \n", + " crval3 = s3d[1].header['CRVAL3'] # Third axis value at the reference pixel \n", "\n", " # Wavelength range of the grating/filter combination\n", " wavstart = s3d[1].header['WAVSTART']\n", @@ -297,7 +296,7 @@ " wavelengths = wavelength_slices[0]\n", " spaxel_loc = spaxel_locs[0]\n", " vmin_vmax_vals = vmin_vmax[0]\n", - " \n", + " \n", " if y_scale:\n", " y_scales = y_scale[0]\n", "\n", @@ -313,7 +312,7 @@ " spaxel_loc = spaxel_locs[0]\n", " vmin_vmax_vals = vmin_vmax[0]\n", " if y_scale:\n", - " y_scales = y_scale[0]\n", + " y_scales = y_scale[0]\n", "\n", " # Loop through each wavelength slices\n", " for i, wave_slice in enumerate(wavelengths):\n", @@ -351,7 +350,6 @@ " ax1.tick_params(axis='both', which='major', labelsize=20)\n", " ax1.coords[0].set_ticklabel(rotation=13, ha='right', pad=24)\n", "\n", - " \n", " # ------------------------------------------Spaxel 1-D Spectrum---------------------------------------------\n", " \n", " # Zoom in on a Spaxel: Spectrum\n", @@ -394,7 +392,7 @@ " slice_wmap = ax3.imshow(slice_mean_wmap, norm=slice_norm_wmap, origin='lower', aspect='auto', cmap=cmap) # plot slice\n", "\n", " cb_wmap = fig.colorbar(slice_wmap, fraction=0.046, pad=0.04)\n", - " cb_wmap.set_label('Weight', labelpad=-1, fontsize = 22)\n", + " cb_wmap.set_label('Weight', labelpad=-1, fontsize=22)\n", " cb_wmap.ax.tick_params(labelsize=20)\n", " cb_wmap.ax.yaxis.get_offset_text().set_fontsize(20)\n", " \n", @@ -417,8 +415,7 @@ " fig.tight_layout(rect=[0, 0, 0.98, 0.98])\n", "\n", " if save_figure:\n", - " fig.savefig(root+\".png\", dpi=24, bbox_inches=\"tight\")\n", - "\n" + " fig.savefig(root+\".png\", dpi=24, bbox_inches=\"tight\")" ] }, { @@ -438,7 +435,7 @@ "outputs": [], "source": [ "# To rerun the notebook and all the pipeline steps set runflag=True\n", - "runflag = True \n", + "runflag = True\n", "\n", "# Demo directory -- contains pre-computed products\n", "if not runflag:\n", @@ -586,7 +583,7 @@ " title='Countrate Image \\n Detector: {} \\n 8-Cycle Dither Position Index: {} \\n GRATING/FILTER: {}/{}'.format(ratefile_open.meta.instrument.detector,\n", " ratefile_open.meta.dither.position_number, \n", " ratefile_open.meta.instrument.grating,\n", - " ratefile_open.meta.instrument.filter)) #rate files have units of DN/s\n", + " ratefile_open.meta.instrument.filter)) # Rate files have units of DN/s\n", " \n", " show_image(ratefile_dq, 0, 10, units='Bit Value', scale='linear', zoom_in=[650, 700, 1250, 1300], ysize=20, xsize=20,\n", " title='Data Quality Map \\n Detector: {} \\n 8-Cycle Dither Position Index: {} \\n GRATING/FILTER: {}/{}'.format(ratefile_open.meta.instrument.detector,\n", @@ -857,7 +854,7 @@ " title='Countrate Image \\n Detector: {} \\n 8-Cycle Dither Position Index: {} \\n GRATING/FILTER: {}/{}'.format(ratefile_open.meta.instrument.detector,\n", " ratefile_open.meta.dither.position_number, \n", " ratefile_open.meta.instrument.grating,\n", - " ratefile_open.meta.instrument.filter)) # rate files have units of DN/s\n", + " ratefile_open.meta.instrument.filter)) # Rate files have units of DN/s\n", " \n", " show_image(ratefile_dq, 0, 10, units='Bit Value', scale='linear', zoom_in=[650, 700, 1250, 1300], ysize=20, xsize=20,\n", " title='Data Quality Map \\n Detector: {} \\n 8-Cycle Dither Position Index: {} \\n GRATING/FILTER: {}/{}'.format(ratefile_open.meta.instrument.detector,\n", diff --git a/notebooks/NIRSpec_IFU_spectral_extraction/ero_nirspec_ifu_02732_demo.ipynb b/notebooks/NIRSpec_IFU_spectral_extraction/ero_nirspec_ifu_02732_demo.ipynb index dc264f403..4580caf46 100644 --- a/notebooks/NIRSpec_IFU_spectral_extraction/ero_nirspec_ifu_02732_demo.ipynb +++ b/notebooks/NIRSpec_IFU_spectral_extraction/ero_nirspec_ifu_02732_demo.ipynb @@ -86,36 +86,34 @@ "\n", "# ----------------------------------------------General Imports-----------------------------------------------------\n", "\n", - "import numpy as np\n", - "import warnings\n", - "warnings.filterwarnings('ignore') #Set to 'default' to turn warnings back on\n", + "import numpy as np # noqa\n", + "import warnings # noqa\n", + "warnings.filterwarnings('ignore') # Set to 'default' to turn warnings back on\n", "\n", "# --------------------------------------------File Operation Imports------------------------------------------------\n", "\n", - "import glob\n", - "import os\n", - "import asdf\n", - "import json\n", - "from shutil import copy\n", + "import glob # noqa\n", + "import os # noqa\n", + "import asdf # noqa\n", + "import json # noqa\n", + "from shutil import copy # noqa\n", "\n", "# --------------------------------------------Astropy/Astroquery Imports--------------------------------------------\n", "\n", - "from astropy.io import fits\n", - "from astropy import wcs\n", - "from astropy.wcs import WCS\n", + "from astropy.io import fits # noqa\n", + "from astropy import wcs # noqa\n", + "from astropy.wcs import WCS # noqa\n", "from astropy.visualization import ImageNormalize, ManualInterval, LogStretch, LinearStretch, AsinhStretch, SqrtStretch\n", - "from astropy.stats import sigma_clipped_stats\n", - "import astroquery\n", - "from astroquery.mast import Mast\n", - "from astroquery.mast import Observations\n", + "import astroquery # noqa\n", + "from astroquery.mast import Mast # noqa\n", + "from astroquery.mast import Observations # noqa\n", "\n", "# ------------------------------------------------Plotting Imports--------------------------------------------------\n", "\n", - "import matplotlib.pyplot as plt\n", - "import matplotlib as mpl\n", + "import matplotlib.pyplot as plt # noqa\n", + "import matplotlib as mpl # noqa\n", "import matplotlib.gridspec as grd\n", - "from matplotlib.patches import Circle\n", - "from matplotlib import cm\n", + "from matplotlib import cm # noqa\n", "\n", "# Use this version for non-interactive plots (easier scrolling of the notebook)\n", "%matplotlib inline\n", @@ -216,12 +214,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "id": "3d233556", "metadata": {}, "outputs": [], "source": [ - "def show_ifu_cubeslices(s3d_file_list, wavelength_slices=[], spaxel_locs=[], y_scale=None, cmap='jet', vmin_vmax = [[[0, 15e1]]], save_figure=False, title=None, title_font=30):\n", + "def show_ifu_cubeslices(s3d_file_list, wavelength_slices=[], spaxel_locs=[], y_scale=None, cmap='jet', vmin_vmax=[[[0, 15e1]]], save_figure=False, title=None, title_font=30):\n", " \"\"\"\n", " Function to that takes a 3-D IFU data cube and generates: \n", " \n", @@ -256,9 +254,9 @@ " \n", " # ---------------------------------------------- Set-up Figure -------------------------------------------------\n", "\n", - " #Plot Slices From the Cube\n", - " fig, axs = plt.subplots(3, np.array(wavelength_slices).size, figsize=(8*np.array(wavelength_slices).size,18))\n", - " gs = grd.GridSpec(3, np.array(wavelength_slices).size, height_ratios=[1]*3, width_ratios=[1]*np.array(wavelength_slices).size, hspace=0.4,wspace=0.7)\n", + " # Plot Slices From the Cube\n", + " fig, axs = plt.subplots(3, np.array(wavelength_slices).size, figsize=(8*np.array(wavelength_slices).size, 18))\n", + " gs = grd.GridSpec(3, np.array(wavelength_slices).size, height_ratios=[1]*3, width_ratios=[1]*np.array(wavelength_slices).size, hspace=0.4, wspace=0.7)\n", "\n", " total_num_plots = 3*np.array(wavelength_slices).size\n", " \n", @@ -267,10 +265,10 @@ " \n", " for s3d_file in s3d_file_list:\n", " \n", - " root=s3d_file[:-9] #Root file name \n", + " root=s3d_file[:-9] # Root file name \n", "\n", - " s3d = fits.open(s3d_file) #3-D IFU data cube fits file \n", - " x1d3 = datamodels.open(root+'_x1d.fits') #1-D Extracted Spectrum \n", + " s3d = fits.open(s3d_file) # 3-D IFU data cube fits file\n", + " x1d3 = datamodels.open(root+'_x1d.fits') # 1-D Extracted Spectrum \n", " \n", " # --------------------------------Wavelength & Surface Brightness/Flux Arrays------------------------------\n", " \n", @@ -278,13 +276,12 @@ " \n", " # --------------------------------------Data & Header Information------------------------------------------\n", "\n", - " \n", " # SCI Extension: [Type:ImageHDU Cards:92 Dimensions:(57, 61, 973) Format:float32]\n", " cube = s3d[1].data # Science data\n", - " wcs = WCS(s3d[1].header) # World Coordinate System (WCS) Transformation keywords \n", + " wcs = WCS(s3d[1].header) # World Coordinate System (WCS) Transformation keywords # noqa\n", " wmap = s3d[4].data # 3-D weight image giving the relative weights of the output spaxels.\n", - " cdelt1 = s3d[1].header['CDELT1']*3600. # Axis 1 coordinate increment at reference point \n", - " cdelt2 = s3d[1].header['CDELT2']*3600. # Axis 2 coordinate increment at reference point \n", + " cdelt1 = s3d[1].header['CDELT1']*3600. # Axis 1 coordinate increment at reference point # noqa\n", + " cdelt2 = s3d[1].header['CDELT2']*3600. # Axis 2 coordinate increment at reference point # noqa\n", " cdelt3 = s3d[1].header['CDELT3'] # Axis 3 coordinate increment at reference point \n", " crval3 = s3d[1].header['CRVAL3'] # Third axis value at the reference pixel \n", "\n", @@ -321,13 +318,12 @@ " spaxel_loc = spaxel_locs[0]\n", " vmin_vmax_vals = vmin_vmax[0]\n", " if y_scale:\n", - " y_scales = y_scale[0]\n", + " y_scales = y_scale[0]\n", "\n", - " \n", " # Loop through each wavelength slices\n", " for i, wave_slice in enumerate(wavelengths):\n", "\n", - " if float(wavstart)<=wave_slice*10**-6 <= float(wavend):\n", + " if float(wavstart) <= wave_slice*10**-6 <= float(wavend):\n", " \n", " # --------------------------------------------2-D Cube Slice------------------------------------------------\n", " \n", @@ -340,14 +336,14 @@ " vmin_val = vmin_vmax_vals[0][0]\n", "\n", " slicewave = wave_slice\n", - " nslice = int((slicewave - crval3)/cdelt3) #the slice of the cube we want to plot\n", + " nslice = int((slicewave - crval3)/cdelt3) # The slice of the cube we want to plot\n", "\n", " ax1 = plt.subplot(gs[0+plot_count], projection=wcs, slices=('x', 'y', nslice)) # Set up the subplot space\n", - " #ax1 = plt.subplot(3,len(wavelength_slices), 0+plot_count, projection=wcs, slices=('x', 'y', nslice)) # Set up the subplot space\n", + " # ax1 = plt.subplot(3,len(wavelength_slices), 0+plot_count, projection=wcs, slices=('x', 'y', nslice)) # Set up the subplot space\n", "\n", " slice_mean = np.nanmean(cube[(nslice-2):(nslice+2), :, :], axis=0) # Mean of the slice looking in the range (nslice-2):(nslice+2)\n", - " slice_norm=ImageNormalize(slice_mean, vmin=vmin_val, vmax=vmax_val, stretch=AsinhStretch()) # Normalize &stretch \n", - " slice_image= ax1.imshow(slice_mean, norm=slice_norm, origin='lower', aspect='auto',cmap=cmap) # Plot slice\n", + " slice_norm = ImageNormalize(slice_mean, vmin=vmin_val, vmax=vmax_val, stretch=AsinhStretch()) # Normalize &stretch \n", + " slice_image = ax1.imshow(slice_mean, norm=slice_norm, origin='lower', aspect='auto', cmap=cmap) # Plot slice\n", " \n", " cb_image = fig.colorbar(slice_image, fraction=0.046, pad=0.04)\n", " cb_image.set_label('MJy/sr', labelpad=-1, fontsize=22)\n", @@ -356,28 +352,27 @@ " \n", " ax1.set_xlabel('RA', fontsize=22)\n", " ax1.set_ylabel('DEC', labelpad=-1, fontsize=22)\n", - " #ax1.grid(color='white', ls='solid')\n", + " # ax1.grid(color='white', ls='solid')\n", " ax1.set_title('Detector {} \\n Grating/Filter: {}/{} \\n {} microns'.format(s3d[0].header['DETECTOR'],s3d[0].header['GRATING'], s3d[0].header['FILTER'], str(slicewave)), fontsize=25)\n", " ax1.tick_params(axis='both', which='major', labelsize=20)\n", " ax1.coords[0].set_ticklabel(rotation=13, ha='right', pad=24)\n", "\n", - " \n", " # ------------------------------------------Spaxel 1-D Spectrum---------------------------------------------\n", " \n", " # Zoom in on a Spaxel: Spectrum\n", - " loc = [spaxel_loc[i][0],spaxel_loc[i][1]]\n", + " loc = [spaxel_loc[i][0], spaxel_loc[i][1]]\n", " x1d3flux_loc = cube[:, loc[1], loc[0]]\n", " # ax2 = plt.subplot(3,len(wavelength_slices), int(total_num_plots/3)+plot_count)\n", " ax2 = plt.subplot(gs[int(total_num_plots/3)+plot_count])\n", "\n", " # Spaxel Box Highlight \n", - " spaxel_rect = plt.Rectangle((loc[0]-.5, loc[1]-.5), 1,1, fill=False, color='black', linewidth=2)\n", + " spaxel_rect = plt.Rectangle((loc[0]-.5, loc[1]-.5), 1, 1, fill=False, color='black', linewidth=2)\n", " ax1.add_patch(spaxel_rect)\n", " \n", " ax2.plot(x1d3wave, x1d3flux_loc, linewidth=1, color=colors[i])\n", " ax2.grid(linewidth=2)\n", " ax2.set_xlabel(r'$\\u03BB [\\u03BC$m]',fontsize=22)\n", - " ax2.set_ylabel(\"Surface Brightness \\n (MJy/sr)\",fontsize=22)\n", + " ax2.set_ylabel(\"Surface Brightness \\n (MJy/sr)\", fontsize=22)\n", " ax2.set_title('Spaxel at (x, y)='+repr(loc), fontsize=25)\n", " ax2.tick_params(axis='both', which='major', labelsize=20)\n", " ax2.yaxis.get_offset_text().set_fontsize(15)\n", @@ -400,8 +395,8 @@ " # ax3 = plt.subplot(3, len(wavelength_slices), int(total_num_plots)-len(wavelength_slices)+plot_count, projection=wcs, slices=('x', 'y', nslice)) # Set up the subplot space\n", " \n", " slice_mean_wmap = np.nanmean(wmap[(nslice-2):(nslice+2), :, :], axis=0) # Mean of the wmap slice looking in the range (nslice-2):(nslice+2)\n", - " slice_norm_wmap=ImageNormalize(slice_mean_wmap, stretch=AsinhStretch()) # Normalize &stretch\n", - " slice_wmap = ax3.imshow(slice_mean_wmap, norm=slice_norm_wmap, origin='lower',aspect='auto', cmap=cmap) # Plot slice\n", + " slice_norm_wmap = ImageNormalize(slice_mean_wmap, stretch=AsinhStretch()) # Normalize &stretch\n", + " slice_wmap = ax3.imshow(slice_mean_wmap, norm=slice_norm_wmap, origin='lower', aspect='auto', cmap=cmap) # Plot slice\n", " \n", " cb_wmap = fig.colorbar(slice_wmap, fraction=0.046, pad=0.04)\n", " cb_wmap.set_label('Weight', labelpad=-1, fontsize=22)\n", @@ -427,8 +422,7 @@ " fig.tight_layout(rect=[0, 0, 0.98, 0.98])\n", "\n", " if save_figure:\n", - " fig.savefig(root+\".png\", dpi=24, bbox_inches=\"tight\")\n", - " " + " fig.savefig(root+\".png\", dpi=24, bbox_inches=\"tight\")" ] }, { @@ -443,10 +437,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "id": "f94025d4", "metadata": {}, - "outputs": [], + "outputs": [ + { + "ename": "NameError", + "evalue": "name 'os' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[2], line 13\u001b[0m\n\u001b[1;32m 9\u001b[0m \u001b[38;5;28;01melif\u001b[39;00m runflag:\n\u001b[1;32m 10\u001b[0m \u001b[38;5;66;03m# If you want to actually re-download the data and run everything offline, \u001b[39;00m\n\u001b[1;32m 11\u001b[0m \u001b[38;5;66;03m# then comment out this line, set runflag=True, & specify a desired local directory\u001b[39;00m\n\u001b[1;32m 12\u001b[0m output_dir \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124m./nirspec_ifu_02732_rerun/\u001b[39m\u001b[38;5;124m'\u001b[39m\n\u001b[0;32m---> 13\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[43mos\u001b[49m\u001b[38;5;241m.\u001b[39mpath\u001b[38;5;241m.\u001b[39mexists(output_dir):\n\u001b[1;32m 14\u001b[0m os\u001b[38;5;241m.\u001b[39mmakedirs(output_dir)\n", + "\u001b[0;31mNameError\u001b[0m: name 'os' is not defined" + ] + } + ], "source": [ "# To rerun the notebook and all the pipeline steps set runflag=True\n", "runflag = True \n", @@ -518,9 +524,9 @@ "\n", "# Define the general search criteria\n", "obs = Observations.query_criteria(\n", - " obs_collection = 'JWST',\n", - " instrument_name = ['NIRSPEC/IFU'],\n", - " proposal_id = '02732')\n", + " obs_collection='JWST',\n", + " instrument_name=['NIRSPEC/IFU'],\n", + " proposal_id='02732')\n", "\n", "# Print the Observations returned from the general search criteria\n", "products = Observations.get_product_list(obs)\n", @@ -586,21 +592,20 @@ "for rate_file in sorted(glob.glob(mast_products_dir+'*00004_nrs1_rate.fits')):\n", " \n", " ratefile_open = datamodels.open(rate_file)\n", - " ratefile_sci = ratefile_open.data #get the pixel data (the SCI extension of the fits file)\n", - " ratefile_dq = ratefile_open.dq #data quality map data (DQ extension)\n", + " ratefile_sci = ratefile_open.data # Get the pixel data (the SCI extension of the fits file)\n", + " ratefile_dq = ratefile_open.dq # Data quality map data (DQ extension)\n", " \n", - " # print the version and CRDS pmap used to create these rate.fits files \n", + " # Print the version and CRDS pmap used to create these rate.fits files \n", " # ratefile_open.serach(key='context')\n", " print(\"Products found in MAST used JWST calibration pipeline version: {} and {}\".format(ratefile_open.meta.calibration_software_version,\n", " ratefile_open.meta.ref_file.crds.context_used))\n", " \n", " # Plot the slope image and zoom in on a small section of the countrate image & corresponding section of the DQ map\n", - " show_image(ratefile_sci, 0, 10, units='DN/s', zoom_in=[500, 550, 1250,1300],\n", + " show_image(ratefile_sci, 0, 10, units='DN/s', zoom_in=[500, 550, 1250, 1300],\n", " title='Countrate Image \\n Detector: {} \\n 8-Cycle Dither Position Index: {} \\n GRATING/FILTER: {}/{}'.format(ratefile_open.meta.instrument.detector,\n", " ratefile_open.meta.dither.position_number, \n", " ratefile_open.meta.instrument.grating,\n", " ratefile_open.meta.instrument.filter)) # rate files have units of DN/s\n", - " \n", " show_image(ratefile_dq, 0, 10, units='Bit Value', scale='linear', zoom_in=[500, 550, 1250, 1300],\n", " title='Data Quality Map \\n Detector: {} \\n 8-Cycle Dither Position Index: {} \\n GRATING/FILTER: {}/{}'.format(ratefile_open.meta.instrument.detector,\n", " ratefile_open.meta.dither.position_number, \n", @@ -644,12 +649,10 @@ "\n", "title_stage2_mast = 'NGC 7319 AGN \\n Level 2 IFU Product: 3-D Cube Slices vs. Corresponding 3-D Weighted Map'\n", "\n", - "\n", "# Characteristics of the plot \n", "nrs1_wavelengths = [1.4, 3.3, 4.5] # Wavelength slices (microns) to take from the 3-D data cube\n", "nrs1_spaxel_locs = [[30, 29], [28, 39], [14, 25]] # Spaxel locations for associated 1-D spectrum (one spaxel plotted per slice)\n", "\n", - "\n", "# Plot using the convience function defined above\n", "show_ifu_cubeslices(stage2_s3d_file, wavelength_slices=[nrs1_wavelengths], spaxel_locs=[nrs1_spaxel_locs], title=title_stage2_mast)" ] @@ -678,8 +681,8 @@ "title_stage3_mast = 'NGC 7319 AGN \\n Level 3 IFU Product: 3-D Cube Slices vs. Corresponding 3-D Weighted Map'\n", "\n", "# Characteristics of the plot \n", - "nrs1_wavelengths = [1.4,3.3,4.5] #Wavelength slices (microns) to take from the 3-D data cube\n", - "nrs1_spaxel_locs = [[30,29],[28,39],[14,25]] #Spaxel locations for associated 1-D spectrum (one spaxel plotted per slice)\n", + "nrs1_wavelengths = [1.4, 3.3, 4.5] # Wavelength slices (microns) to take from the 3-D data cube\n", + "nrs1_spaxel_locs = [[30, 29], [28, 39], [14, 25]] # Spaxel locations for associated 1-D spectrum (one spaxel plotted per slice)\n", "\n", "# Plot using the convience function defined above\n", "show_ifu_cubeslices(stage3_s3d_file, wavelength_slices=[nrs1_wavelengths], spaxel_locs=[nrs1_spaxel_locs], title=title_stage3_mast)" @@ -712,9 +715,9 @@ "x1d3flux_mast = x1d3_mast.spec[0].spec_table.SURF_BRIGHT\n", "\n", "# Plot the Extracted 1-D Spectrum\n", - "fig = plt.figure(figsize=(15,9))\n", + "fig = plt.figure(figsize=(15, 9))\n", "\n", - "plt.plot(x1d3wave_mast,x1d3flux_mast, linewidth =2)\n", + "plt.plot(x1d3wave_mast, x1d3flux_mast, linewidth=2)\n", "\n", "# Where wavelength slice was taken above\n", "plt.vlines(1.4, 0., 400., 'black', 'dotted', label='1.4 microns')\n", @@ -949,7 +952,7 @@ }, "outputs": [], "source": [ - "#Copy ASN file from MAST into the stage 1 rerun directory\n", + "# Copy ASN file from MAST into the stage 1 rerun directory\n", "\n", "asnfile_mast = glob.glob(mast_products_dir+'*_spec3_00001_asn.json')[0] # ASN file found in MAST\n", "\n", @@ -998,15 +1001,15 @@ }, "outputs": [], "source": [ - "#Rerun stage 3 with outlier detection off\n", + "# Rerun stage 3 with outlier detection off\n", "if runflag:\n", "\n", " result = Spec3Pipeline.call(asnfile_rerun,\n", " save_results=True,\n", " output_dir=output_dir_rerun,\n", " steps={\"outlier_detection\": {\"skip\": False,\n", - " \"save_results\": True,\n", - " \"kernel_size\": '3 3'}})" + " \"save_results\": True,\n", + " \"kernel_size\": '3 3'}})" ] }, { @@ -1016,11 +1019,11 @@ "metadata": {}, "outputs": [], "source": [ - "#Stage 3 Products -- Combined Calibrated 3-D data cube for PRISM/CLEAR \n", + "# Stage 3 Products -- Combined Calibrated 3-D data cube for PRISM/CLEAR \n", "\n", "stage3_s3d_file = sorted(glob.glob(output_dir_rerun+'*nirspec_prism-clear_s3d.fits')) \n", "\n", - "title_stage3_rerun='NGC 7319 AGN \\n Level 3 IFU Product: 3-D Cube Slices vs. Corresponding 3-D Weighted Map'\n", + "title_stage3_rerun = 'NGC 7319 AGN \\n Level 3 IFU Product: 3-D Cube Slices vs. Corresponding 3-D Weighted Map'\n", "\n", "# Characteristics of the plot \n", "nrs1_wavelengths = [1.4, 3.3, 4.5] # Wavelength slices (microns) to take from the 3-D data cube\n", @@ -1060,7 +1063,7 @@ "# Plot the Extracted 1-D Spectrum\n", "fig = plt.figure(figsize=(15, 9))\n", "\n", - "plt.plot(x1d3wave_rerun,x1d3flux_rerun, linewidth=2)\n", + "plt.plot(x1d3wave_rerun, x1d3flux_rerun, linewidth=2)\n", "\n", "# Where wavelength slice was taken above\n", "plt.vlines(1.4, 0., 400., 'black', 'dotted', label='1.4 microns')\n", diff --git a/notebooks/NIRSpec_IFU_spectral_extraction/ero_nirspec_ifu_02732_demo_pointsource.ipynb b/notebooks/NIRSpec_IFU_spectral_extraction/ero_nirspec_ifu_02732_demo_pointsource.ipynb index 375e788df..d6c473a5e 100644 --- a/notebooks/NIRSpec_IFU_spectral_extraction/ero_nirspec_ifu_02732_demo_pointsource.ipynb +++ b/notebooks/NIRSpec_IFU_spectral_extraction/ero_nirspec_ifu_02732_demo_pointsource.ipynb @@ -81,36 +81,35 @@ "\n", "# ----------------------------------------------General Imports-----------------------------------------------------\n", "\n", - "import numpy as np\n", - "import warnings\n", - "warnings.filterwarnings('ignore') #Set to 'default' to turn warnings back on\n", + "import numpy as np # noqa\n", + "import warnings # noqa\n", + "warnings.filterwarnings('ignore') # Set to 'default' to turn warnings back on\n", "\n", "# --------------------------------------------File Operation Imports------------------------------------------------\n", "\n", - "import glob\n", - "import os\n", - "import asdf\n", - "import json\n", - "from shutil import copy\n", + "import glob # noqa\n", + "import os # noqa\n", + "import asdf # noqa\n", + "import json # noqa\n", + "from shutil import copy # noqa\n", "\n", "# --------------------------------------------Astropy/Astroquery Imports--------------------------------------------\n", "\n", - "from astropy.io import fits\n", - "from astropy import wcs\n", - "from astropy.wcs import WCS\n", - "from astropy.visualization import ImageNormalize, ManualInterval, LogStretch, LinearStretch, AsinhStretch, SqrtStretch\n", - "from astropy.stats import sigma_clipped_stats\n", - "import astroquery\n", - "from astroquery.mast import Mast\n", - "from astroquery.mast import Observations\n", + "from astropy.io import fits # noqa\n", + "from astropy import wcs # noqa\n", + "from astropy.wcs import WCS # noqa\n", + "from astropy.visualization import ImageNormalize, ManualInterval, LogStretch, LinearStretch, AsinhStretch # noqa\n", + "import astroquery # noqa\n", + "from astroquery.mast import Mast # noqa\n", + "from astroquery.mast import Observations # noqa\n", "\n", "# ------------------------------------------------Plotting Imports--------------------------------------------------\n", "\n", - "import matplotlib.pyplot as plt\n", - "import matplotlib as mpl\n", - "import matplotlib.gridspec as grd\n", - "from matplotlib.patches import Circle\n", - "from matplotlib import cm\n", + "import matplotlib.pyplot as plt # noqa\n", + "import matplotlib as mpl # noqa\n", + "import matplotlib.gridspec as grd # noqa\n", + "from matplotlib.patches import Circle # noqa\n", + "from matplotlib import cm # noqa\n", "\n", "# Use this version for non-interactive plots (easier scrolling of the notebook)\n", "%matplotlib inline\n", @@ -216,7 +215,7 @@ "metadata": {}, "outputs": [], "source": [ - "def show_ifu_cubeslices(s3d_file_list, wavelength_slices=[], spaxel_locs=[], y_scale=None, cmap='jet', vmin_vmax = [[[0, 15e1]]], save_figure=False, title=None, title_font=30):\n", + "def show_ifu_cubeslices(s3d_file_list, wavelength_slices=[], spaxel_locs=[], y_scale=None, cmap='jet', vmin_vmax=[[[0, 15e1]]], save_figure=False, title=None, title_font=30):\n", " \"\"\"\n", " Function to that takes a 3-D IFU data cube and generates: \n", " \n", @@ -252,10 +251,10 @@ " # ---------------------------------------------- Set-up Figure -------------------------------------------------\n", "\n", " # Plot Slices From the Cube\n", - " fig, axs = plt.subplots(3, np.array(wavelength_slices).size, figsize=(8*np.array(wavelength_slices).size,18))\n", - " gs = grd.GridSpec(3, np.array(wavelength_slices).size, height_ratios=[1]*3, width_ratios=[1]*np.array(wavelength_slices).size, hspace=0.4,wspace=0.7)\n", + " fig, axs = plt.subplots(3, np.array(wavelength_slices).size, figsize=(8*np.array(wavelength_slices).size, 18))\n", + " gs = grd.GridSpec(3, np.array(wavelength_slices).size, height_ratios=[1]*3, width_ratios=[1]*np.array(wavelength_slices).size, hspace=0.4, wspace=0.7)\n", "\n", - " total_num_plots=3*np.array(wavelength_slices).size\n", + " total_num_plots = 3*np.array(wavelength_slices).size\n", " \n", " plot_count = 0\n", " # ---------------------------------------------Open Files------------------------------------------------------\n", @@ -273,13 +272,12 @@ " \n", " # --------------------------------------Data & Header Information------------------------------------------\n", "\n", - " \n", " # SCI Extension: [Type:ImageHDU Cards:92 Dimensions:(57, 61, 973) Format:float32]\n", " cube = s3d[1].data # Science data\n", - " wcs = WCS(s3d[1].header) # World Coordinate System (WCS) Transformation keywords \n", + " wcs = WCS(s3d[1].header) # World Coordinate System (WCS) Transformation keywords # noqa\n", " wmap = s3d[4].data # 3-D weight image giving the relative weights of the output spaxels.\n", - " cdelt1 = s3d[1].header['CDELT1']*3600. # Axis 1 coordinate increment at reference point \n", - " cdelt2 = s3d[1].header['CDELT2']*3600. # Axis 2 coordinate increment at reference point \n", + " cdelt1 = s3d[1].header['CDELT1']*3600. # Axis 1 coordinate increment at reference point # noqa\n", + " cdelt2 = s3d[1].header['CDELT2']*3600. # Axis 2 coordinate increment at reference point # noqa\n", " cdelt3 = s3d[1].header['CDELT3'] # Axis 3 coordinate increment at reference point \n", " crval3 = s3d[1].header['CRVAL3'] # Third axis value at the reference pixel \n", "\n", @@ -315,13 +313,12 @@ " spaxel_loc = spaxel_locs[0]\n", " vmin_vmax_vals = vmin_vmax[0]\n", " if y_scale:\n", - " y_scales = y_scale[0]\n", + " y_scales = y_scale[0]\n", "\n", - " \n", " # Loop through each wavelength slices\n", " for i, wave_slice in enumerate(wavelengths):\n", "\n", - " if float(wavstart)<=wave_slice*10**-6 <= float(wavend):\n", + " if float(wavstart) <= wave_slice*10**-6 <= float(wavend):\n", " \n", " # --------------------------------------------2-D Cube Slice------------------------------------------------\n", " \n", @@ -339,8 +336,8 @@ " # ax1 = plt.subplot(3,len(wavelength_slices), 0+plot_count, projection=wcs, slices=('x', 'y', nslice)) # Set up the subplot space\n", "\n", " slice_mean = np.nanmean(cube[(nslice-2):(nslice+2), :, :], axis=0) # Mean of the slice looking in the range (nslice-2):(nslice+2)\n", - " slice_norm=ImageNormalize(slice_mean, vmin=vmin_val, vmax=vmax_val, stretch=AsinhStretch()) # Normalize &stretch \n", - " slice_image= ax1.imshow(slice_mean, norm=slice_norm, origin='lower', aspect='auto', cmap=cmap) # Plot slice\n", + " slice_norm = ImageNormalize(slice_mean, vmin=vmin_val, vmax=vmax_val, stretch=AsinhStretch()) # Normalize &stretch \n", + " slice_image = ax1.imshow(slice_mean, norm=slice_norm, origin='lower', aspect='auto', cmap=cmap) # Plot slice\n", "\n", " cb_image = fig.colorbar(slice_image, fraction=0.046, pad=0.04)\n", " cb_image.set_label('MJy/sr', labelpad=-1, fontsize=22)\n", @@ -354,11 +351,10 @@ " ax1.tick_params(axis='both', which='major', labelsize=20)\n", " ax1.coords[0].set_ticklabel(rotation=13, ha='right', pad=24)\n", "\n", - " \n", " # ------------------------------------------Spaxel 1-D Spectrum---------------------------------------------\n", " \n", " # Zoom in on a Spaxel: Spectrum\n", - " loc = [spaxel_loc[i][0],spaxel_loc[i][1]]\n", + " loc = [spaxel_loc[i][0], spaxel_loc[i][1]]\n", " x1d3flux_loc = cube[:, loc[1], loc[0]]\n", " # ax2 = plt.subplot(3,len(wavelength_slices), int(total_num_plots/3)+plot_count)\n", " ax2 = plt.subplot(gs[int(total_num_plots/3)+plot_count])\n", @@ -370,7 +366,7 @@ " ax2.plot(x1d3wave, x1d3flux_loc, linewidth=1, color=colors[i])\n", " ax2.grid(linewidth=2)\n", " ax2.set_xlabel('$\\u03BB [\\u03BC$m]',fontsize=22)\n", - " ax2.set_ylabel(\"Surface Brightness \\n (MJy/sr)\",fontsize=22)\n", + " ax2.set_ylabel(\"Surface Brightness \\n (MJy/sr)\", fontsize=22)\n", " ax2.set_title('Spaxel at (x, y)='+repr(loc), fontsize=25)\n", " ax2.tick_params(axis='both', which='major', labelsize=20)\n", " ax2.yaxis.get_offset_text().set_fontsize(15)\n", @@ -393,8 +389,8 @@ " # ax3 = plt.subplot(3, len(wavelength_slices), int(total_num_plots)-len(wavelength_slices)+plot_count, projection=wcs, slices=('x', 'y', nslice)) # Set up the subplot space\n", " \n", " slice_mean_wmap = np.nanmean(wmap[(nslice-2):(nslice+2), :, :], axis=0) # Mean of the wmap slice looking in the range (nslice-2):(nslice+2)\n", - " slice_norm_wmap=ImageNormalize(slice_mean_wmap, stretch=AsinhStretch()) # Normalize & stretch\n", - " slice_wmap = ax3.imshow(slice_mean_wmap, norm=slice_norm_wmap, origin='lower',aspect='auto', cmap=cmap) # Plot slice\n", + " slice_norm_wmap = ImageNormalize(slice_mean_wmap, stretch=AsinhStretch()) # Normalize & stretch\n", + " slice_wmap = ax3.imshow(slice_mean_wmap, norm=slice_norm_wmap, origin='lower', aspect='auto', cmap=cmap) # Plot slice\n", "\n", " cb_wmap = fig.colorbar(slice_wmap, fraction=0.046, pad=0.04)\n", " cb_wmap.set_label('Weight', labelpad=-1, fontsize=22)\n", @@ -420,8 +416,7 @@ " fig.tight_layout(rect=[0, 0, 0.98, 0.98])\n", "\n", " if save_figure:\n", - " fig.savefig(root+\".png\", dpi=24, bbox_inches=\"tight\")\n", - " " + " fig.savefig(root+\".png\", dpi=24, bbox_inches=\"tight\")" ] }, { @@ -596,7 +591,6 @@ " ratefile_open.meta.dither.position_number, \n", " ratefile_open.meta.instrument.grating,\n", " ratefile_open.meta.instrument.filter)) # rate files have units of DN/s\n", - " \n", " show_image(ratefile_dq, 0, 10, units='Bit Value', scale='linear',zoom_in=[500, 550, 1250, 1300],\n", " title='Data Quality Map \\n Detector: {} \\n 8-Cycle Dither Position Index: {} \\n GRATING/FILTER: {}/{}'.format(ratefile_open.meta.instrument.detector,\n", " ratefile_open.meta.dither.position_number, \n", @@ -776,8 +770,8 @@ " save_results=True,\n", " output_dir=output_dir,\n", " steps={\"outlier_detection\": {\"skip\": False,\n", - " \"save_results\": True,\n", - " \"kernel_size\": '3 3'},\n", + " \"save_results\": True,\n", + " \"kernel_size\": '3 3'},\n", " \"extract_1d\": {\"subtract_background\": False}}) # Do not automatically apply background subtraction until we modify the extraction region\n", " " ] @@ -821,9 +815,9 @@ "x1d3flux_rerun_point = x1d3_rerun_point.spec[0].spec_table.FLUX\n", "\n", "# Plot the Extracted 1-D Spectrum\n", - "fig = plt.figure(figsize=(15,9))\n", + "fig = plt.figure(figsize=(15, 9))\n", "\n", - "plt.plot(x1d3wave_rerun_point,x1d3flux_rerun_point, linewidth =2)\n", + "plt.plot(x1d3wave_rerun_point, x1d3flux_rerun_point, linewidth=2)\n", "\n", "# Where wavelength slice was taken above\n", "plt.vlines(1.4, 0., 400., 'black', 'dotted', label='1.4 microns')\n",