Skip to content

Commit

Permalink
third pass styling
Browse files Browse the repository at this point in the history
  • Loading branch information
gibsongreen committed Jun 18, 2024
1 parent 0c3b080 commit 5763908
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 81 deletions.
76 changes: 27 additions & 49 deletions notebooks/psf_photometry/MIRI/miri_1028.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,6 @@
"outputs": [],
"source": [
"from astropy.io import fits\n",
"from astropy.table import Table\n",
"from astropy.nddata import extract_array\n",
"from astropy.coordinates import SkyCoord\n",
"from astropy import wcs\n",
Expand All @@ -187,27 +186,21 @@
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"from astroquery.mast import Observations\n",
"from astropy.visualization import (simple_norm, LinearStretch)\n",
"from mpl_toolkits.axes_grid1 import make_axes_locatable\n",
"from astropy.visualization import simple_norm\n",
"import time\n",
"import math\n",
"from importlib.metadata import version\n",
"\n",
"# JWST models\n",
"from jwst import datamodels, associations\n",
"from jwst.datamodels import ImageModel, dqflags\n",
"from jwst.datamodels import ImageModel\n",
"\n",
"# Background and PSF Functions\n",
"#\n",
"from photutils.background import MMMBackground, MADStdBackgroundRMS, Background2D\n",
"from photutils.background import MMMBackground, MADStdBackgroundRMS\n",
"from photutils.detection import DAOStarFinder\n",
"from photutils import EPSFBuilder, GriddedPSFModel\n",
"from photutils.psf import DAOGroup, extract_stars, IterativelySubtractedPSFPhotometry\n",
"\n",
"# Photutils library and tools\n",
"import photutils\n",
"from photutils.aperture import CircularAperture, CircularAnnulus, aperture_photometry\n",
"from photutils import Background2D, MedianBackground, ModeEstimatorBackground, MMMBackground"
"from photutils import MMMBackground"
]
},
{
Expand All @@ -231,20 +224,6 @@
"!pip install space_phot --upgrade"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7bc12d37-4dc6-41e8-a1a4-756e143ea2eb",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"# Space_Phot Version Number for Reference \n",
"from importlib.metadata import version\n",
"version('space_phot')"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down Expand Up @@ -561,7 +540,7 @@
"norm1 = simple_norm(ref_data, stretch='log', min_cut=4.5, max_cut=5)\n",
"\n",
"plt.figure(figsize=(20, 12))\n",
"plt.imshow(ref_data,origin='lower', norm=norm1, cmap='gray')\n",
"plt.imshow(ref_data, origin='lower', norm=norm1, cmap='gray')\n",
"clb = plt.colorbar()\n",
"clb.set_label('MJy/Str', labelpad=-40, y=1.05, rotation=0)\n",
"plt.gca().tick_params(axis='both', color='none')\n",
Expand Down Expand Up @@ -661,8 +640,8 @@
"\n",
"# The function get_jwst_psf is a space_phot wrapper for the WebbPSF calc_psf and CreatePSFLibrary functions. It is intended to provide easy, but limited, access to WebbPSF.\n",
"# The source code for psfs = space_phot.get_jwst_psf(jwst_obs, source_location) is as follows:\n",
"c = webbpsf.gridded_library.CreatePSFLibrary(inst, inst.filter, num_psfs=1, psf_location=(x,y), fov_pixels=psf_width,\n",
" detectors=st_obs.detector, save=False)\n",
"c = webbpsf.gridded_library.CreatePSFLibrary(inst, inst.filter, num_psfs=1, psf_location=(x, y), fov_pixels=psf_width,\n",
" detectors=st_obs.detector, save=False)\n",
"grid = c.create_grid()\n",
"\n",
"# In most cases, we assume the user wishes to simply grab the default WebbPSF. Advanced users should consider using webbpsf.\n",
Expand All @@ -685,7 +664,6 @@
"outputs": [],
"source": [
"# Get the PSF from WebbPSF using defaults.\n",
"\n",
"jwst_obs = space_phot.observation2(lvl2)\n",
"psfs = space_phot.get_jwst_psf(jwst_obs, source_location)"
]
Expand All @@ -699,7 +677,7 @@
"source": [
"# The scale should highlight the background noise so it is possible to see all faint sources.\n",
"\n",
"ref_cutout = extract_array(psfs[0].data, (41, 41) (122, 122))\n",
"ref_cutout = extract_array(psfs[0].data, (41, 41)(122, 122))\n",
"# ref_cutout = psfs[0].data\n",
"norm1 = simple_norm(ref_cutout, stretch='log', min_cut=0.0, max_cut=0.2)\n",
"plt.imshow(ref_cutout, origin='lower', norm=norm1, cmap='gray')\n",
Expand Down Expand Up @@ -756,9 +734,9 @@
"source": [
"# Do PSF Photometry using space_phot (details of fitting are in documentation)\n",
"\n",
"jwst_obs.psf_photometry(psfs, source_location, bounds={'flux' :[-10000, 10000],\n",
" 'centroid' :[-2, 2],\n",
" 'bkg' :[0, 50]},\n",
"jwst_obs.psf_photometry(psfs, source_location, bounds={'flux': [-10000, 10000],\n",
" 'centroid': [-2, 2],\n",
" 'bkg': [0, 50]},\n",
" fit_width=9,\n",
" fit_bkg=True,\n",
" fit_flux='single')\n",
Expand Down Expand Up @@ -879,7 +857,7 @@
"norm1 = simple_norm(ref_data, stretch='log', min_cut=4.5, max_cut=5)\n",
"\n",
"plt.figure(figsize=(20, 12))\n",
"plt.imshow(ref_data,origin='lower', norm=norm1, cmap='gray')\n",
"plt.imshow(ref_data, origin='lower', norm=norm1, cmap='gray')\n",
"clb = plt.colorbar()\n",
"clb.set_label('MJy/Str', labelpad=-40, y=1.05, rotation=0)\n",
"plt.gca().tick_params(axis='both', color='none')\n",
Expand Down Expand Up @@ -1006,9 +984,9 @@
"source": [
"# Do PSF Photometry using space_phot (details of fitting are in documentation)\n",
"# See detailed notes in Section 3.1 above about the fitting process and diagnostics\n",
"jwst3_obs.psf_photometry(psf3, source_location, bounds={'flux' :[-10000, 10000],\n",
" 'centroid' :[-2, 2],\n",
" 'bkg' :[0, 50]},\n",
"jwst3_obs.psf_photometry(psf3, source_location, bounds={'flux' : [-10000, 10000],\n",
" 'centroid': [-2, 2],\n",
" 'bkg': [0, 50]},\n",
" fit_width=9,\n",
" fit_bkg=True,\n",
" fit_flux=True)\n",
Expand Down Expand Up @@ -1272,9 +1250,9 @@
"# Do PSF Photometry using space_phot (details of fitting are in documentation)\n",
"# https://st-phot.readthedocs.io/en/latest/examples/plot_a_psf.html#jwst-images\n",
"\n",
"jwst_obs.psf_photometry(psfs, source_location, bounds={'flux' :[-10, 1000],\n",
" # 'centroid' :[-2, 2],\n",
" 'bkg' :[0, 50]},\n",
"jwst_obs.psf_photometry(psfs, source_location, bounds={'flux': [-10, 1000],\n",
" # 'centroid': [-2, 2],\n",
" 'bkg': [0, 50]},\n",
" fit_width=5,\n",
" fit_bkg=True,\n",
" fit_centroid='fixed',\n",
Expand Down Expand Up @@ -1343,7 +1321,7 @@
"norm1 = simple_norm(ref_data, stretch='log', min_cut=4.5, max_cut=5)\n",
"\n",
"plt.figure(figsize=(20, 12))\n",
"plt.imshow(ref_data,origin='lower', norm=norm1, cmap='gray')\n",
"plt.imshow(ref_data, origin='lower', norm=norm1, cmap='gray')\n",
"clb = plt.colorbar()\n",
"clb.set_label('MJy/Str', labelpad=-40, y=1.05, rotation=0)\n",
"plt.gca().tick_params(axis='both', color='none')\n",
Expand Down Expand Up @@ -1470,9 +1448,9 @@
},
"outputs": [],
"source": [
"jwst3_obs.psf_photometry(psf3, source_location, bounds={'flux' :[-1000, 1000],\n",
" # 'centroid' :[-2, 2],\n",
" 'bkg' :[0, 50]},\n",
"jwst3_obs.psf_photometry(psf3, source_location, bounds={'flux': [-1000, 1000],\n",
" # 'centroid': [-2, 2],\n",
" 'bkg': [0, 50]},\n",
" fit_width=9,\n",
" fit_bkg=True, \n",
" fit_centroid=False,\n",
Expand Down Expand Up @@ -1874,9 +1852,9 @@
" print('Starting', counter+1., ' of', len(skycoords), ':', source_location)\n",
" # psfs = space_phot.get_jwst_psf(jwst_obs, source_location) # , num_psfs=4)\n",
" psfs = space_phot.util.get_jwst_psf_from_grid(jwst_obs, source_location, grid)\n",
" jwst_obs.psf_photometry(psfs, source_location, bounds={'flux' :[-100000, 100000],\n",
" 'centroid' :[-2., 2.],\n",
" 'bkg' :[0, 50]},\n",
" jwst_obs.psf_photometry(psfs, source_location, bounds={'flux': [-100000, 100000],\n",
" 'centroid': [-2., 2.],\n",
" 'bkg': [0, 50]},\n",
" fit_width=9,\n",
" fit_bkg=True,\n",
" fit_flux='single',\n",
Expand Down Expand Up @@ -1956,7 +1934,7 @@
"norm1 = simple_norm(ref_data, stretch='linear', min_cut=0.5, max_cut=5)\n",
"\n",
"plt.figure(figsize=(20, 12))\n",
"plt.imshow(ref_data,origin='lower', norm=norm1, cmap='gray')\n",
"plt.imshow(ref_data, origin='lower', norm=norm1, cmap='gray')\n",
"clb = plt.colorbar()\n",
"clb.set_label('MJy/Str', labelpad=-40, y=1.05, rotation=0)\n",
"plt.gca().tick_params(axis='both', color='none')\n",
Expand Down Expand Up @@ -2040,7 +2018,7 @@
" print('Starting', counter+1., ' of', len(skycoords), ':', source_location)\n",
" # psf3 = space_phot.util.get_jwst_psf_from_grid(jwst3_obs, source_location,grid)\n",
" psf3 = space_phot.get_jwst3_psf(jwst_obs, jwst3_obs, source_location, num_psfs=4)\n",
" jwst3_obs.psf_photometry(psf3, source_location, bounds={'flux' :[-10000, 10000],\n",
" jwst3_obs.psf_photometry(psf3, source_location, bounds={'flux': [-10000, 10000],\n",
" 'centroid': [-2, 2],\n",
" 'bkg': [0, 50]},\n",
" fit_width=9,\n",
Expand Down
58 changes: 26 additions & 32 deletions notebooks/psf_photometry/NIRCam/nircam_spacephot.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,7 @@
"\n",
"# Background and PSF Functions\n",
"from photutils.background import MMMBackground, MADStdBackgroundRMS\n",
"from photutils.detection import DAOStarFinder\n",
"\n",
"# Photutils library and tools\n",
"from photutils import MMMBackground"
"from photutils.detection import DAOStarFinder"
]
},
{
Expand Down Expand Up @@ -358,7 +355,7 @@
"source": [
"# Get PSF from WebbPSF\n",
"jwst_obs = space_phot.observation2(lvl2)\n",
"psfs = space_phot.get_jwst_psf(jwst_obs, source_location) #,num_psfs=4)\n",
"psfs = space_phot.get_jwst_psf(jwst_obs, source_location) # ,num_psfs=4)\n",
"plt.imshow(psfs[0].data)\n",
"plt.show()"
]
Expand All @@ -376,9 +373,9 @@
"# Do PSF Photometry using space_phot (details of fitting are in documentation)\n",
"# https://st-phot.readthedocs.io/en/latest/examples/plot_a_psf.html#jwst-images\n",
"\n",
"jwst_obs.psf_photometry(psfs, source_location, bounds={'flux' :[-10, 10000],\n",
" 'centroid' :[-2, 2],\n",
" 'bkg' :[0, 50]},\n",
"jwst_obs.psf_photometry(psfs, source_location, bounds={'flux': [-10, 10000],\n",
" 'centroid': [-2, 2],\n",
" 'bkg': [0, 50]},\n",
" fit_width=5,\n",
" fit_bkg=True,\n",
" fit_flux='single')\n",
Expand Down Expand Up @@ -462,11 +459,11 @@
"source": [
"source_location = SkyCoord('5:05:30.6593', '+52:49:49.862', unit=(u.hourangle, u.deg))\n",
"\n",
"ref_y,ref_x = skycoord_to_pixel(source_location, wcs.WCS(ref_fits['SCI', 1], ref_fits))\n",
"ref_y, ref_x = skycoord_to_pixel(source_location, wcs.WCS(ref_fits['SCI', 1], ref_fits))\n",
"ref_cutout = extract_array(ref_data, (11, 11), (ref_x, ref_y))\n",
"norm1 = simple_norm(ref_cutout, stretch='linear', min_cut=-1, max_cut=10)\n",
"plt.imshow(ref_cutout, origin='lower',\n",
" norm=norm1, cmap='gray')\n",
" norm=norm1, cmap='gray')\n",
"plt.title('PID1028,Obs006 (level 3)')\n",
"plt.gca().tick_params(labelcolor='none', axis='both', color='none')\n",
"plt.show()"
Expand Down Expand Up @@ -498,12 +495,12 @@
},
"outputs": [],
"source": [
"jwst3_obs.psf_photometry(psf3, source_location, bounds={'flux' :[-1000, 100000],\n",
" 'centroid' :[-2, 2],\n",
" 'bkg' :[0, 100]},\n",
" fit_width=5,\n",
" fit_bkg=False,\n",
" fit_flux=True)\n",
"jwst3_obs.psf_photometry(psf3, source_location, bounds={'flux': [-1000, 100000],\n",
" 'centroid': [-2, 2],\n",
" 'bkg': [0, 100]},\n",
" fit_width=5,\n",
" fit_bkg=False,\n",
" fit_flux=True)\n",
"\n",
"jwst_obs.plot_psf_fit()\n",
"plt.show()\n",
Expand Down Expand Up @@ -698,7 +695,7 @@
"source": [
"# Get PSF from WebbPSF\n",
"jwst_obs = space_phot.observation2(lvl2)\n",
"psfs = space_phot.get_jwst_psf(jwst_obs, source_location) #,num_psfs=4)\n",
"psfs = space_phot.get_jwst_psf(jwst_obs, source_location) # ,num_psfs=4)\n",
"plt.imshow(psfs[0].data)\n",
"plt.show()"
]
Expand All @@ -715,10 +712,9 @@
"source": [
"# Do PSF Photometry using space_phot (details of fitting are in documentation)\n",
"# https://st-phot.readthedocs.io/en/latest/examples/plot_a_psf.html#jwst-images\n",
"\n",
"jwst_obs.psf_photometry(psfs,source_location, bounds={'flux' :[-10, 1000],\n",
" #'centroid' :[-2,2],\n",
" 'bkg' :[0, 50]},\n",
"jwst_obs.psf_photometry(psfs, source_location, bounds={'flux': [-10, 1000],\n",
" #'centroid': [-2,2],\n",
" 'bkg': [0, 50]},\n",
" fit_width=5,\n",
" fit_bkg=True,\n",
" fit_centroid='fixed',\n",
Expand All @@ -740,7 +736,6 @@
"outputs": [],
"source": [
"# Print Upper Limits\n",
"\n",
"magupper_lvl2psf = jwst_obs.upper_limit(nsigma=5)\n",
"magupper_lvl2psf"
]
Expand Down Expand Up @@ -835,9 +830,9 @@
},
"outputs": [],
"source": [
"jwst3_obs.psf_photometry(psf3, source_location, bounds={'flux' :[-10, 1000],\n",
" #'centroid' :[-2,2],\n",
" 'bkg' :[0, 50]},\n",
"jwst3_obs.psf_photometry(psf3, source_location, bounds={'flux': [-10, 1000],\n",
" #'centroid': [-2,2],\n",
" 'bkg': [0, 50]},\n",
" fit_width=5,\n",
" fit_bkg=False,\n",
" fit_centroid=False,\n",
Expand Down Expand Up @@ -1171,7 +1166,6 @@
"outputs": [],
"source": [
"# Now Loop Through All Stars and Build Photometry Table\n",
"\n",
"import pandas as pd\n",
"counter = 0.\n",
"badindex = []\n",
Expand All @@ -1183,9 +1177,9 @@
" print('Starting', counter+1., ' of', len(skycoords), ':', source_location)\n",
" # psfs = space_phot.get_jwst_psf(jwst_obs,source_location) # ,num_psfs=4)\n",
" psfs = space_phot.util.get_jwst_psf_from_grid(jwst_obs, source_location, grid)\n",
" jwst_obs.psf_photometry(psfs, source_location, bounds={'flux' :[-100, 1000],\n",
" 'centroid' :[-2., 2.],\n",
" 'bkg' :[0, 50]},\n",
" jwst_obs.psf_photometry(psfs, source_location, bounds={'flux': [-100, 1000],\n",
" 'centroid': [-2., 2.],\n",
" 'bkg': [0, 50]},\n",
" fit_width=3,\n",
" fit_bkg=False,\n",
" fit_flux='single',\n",
Expand Down Expand Up @@ -1292,9 +1286,9 @@
" print('Starting', counter+1., ' of', len(skycoords), ':', source_location)\n",
" # psf3 = space_phot.util.get_jwst_psf_from_grid(jwst3_obs,source_location,grid)\n",
" psf3 = space_phot.get_jwst3_psf(jwst_obs, jwst3_obs, source_location, num_psfs=4)\n",
" jwst3_obs.psf_photometry(psf3, source_location, bounds={'flux' :[-1000, 10000],\n",
" 'centroid' :[-2, 2],\n",
" 'bkg' :[0, 50]},\n",
" jwst3_obs.psf_photometry(psf3, source_location, bounds={'flux': [-1000, 10000],\n",
" 'centroid': [-2, 2],\n",
" 'bkg': [0, 50]},\n",
" fit_width=5,\n",
" fit_bkg=True,\n",
" fit_flux=True)\n",
Expand Down

0 comments on commit 5763908

Please sign in to comment.