Skip to content

Commit c7e9208

Browse files
committed
fix styling
1 parent cb72638 commit c7e9208

File tree

1 file changed

+132
-82
lines changed

1 file changed

+132
-82
lines changed

notebooks/MIRI/psf_photometry/miri_spacephot.ipynb

Lines changed: 132 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -193,12 +193,11 @@
193193
"from jwst.datamodels import ImageModel\n",
194194
"\n",
195195
"# Background and PSF Functions\n",
196-
"from photutils.background import MMMBackground, MADStdBackgroundRMS, Background2D, LocalBackground\n",
196+
"from photutils.background import MMMBackground, MADStdBackgroundRMS, LocalBackground\n",
197197
"\n",
198198
"# Photutils library and tools\n",
199199
"from photutils.aperture import CircularAperture\n",
200200
"from photutils.detection import DAOStarFinder\n",
201-
"from photutils import EPSFBuilder\n",
202201
"\n",
203202
"# set up crds if necessary\n",
204203
"from crds import client\n",
@@ -450,7 +449,7 @@
450449
" data = fits.open(file)['SCI', 1].data\n",
451450
" dq = fits.open(file)['DQ', 1].data\n",
452451
" data[dq >= 10] = np.nan\n",
453-
" hdul['SCI',1].data = data\n",
452+
" hdul['SCI', 1].data = data\n",
454453
" hdul.flush()"
455454
]
456455
},
@@ -492,12 +491,12 @@
492491
"# Reference Link: http://simbad.cds.unistra.fr/simbad/sim-basic?Ident=2MASSJ17430448%2B6655015&submit=SIMBAD+search\n",
493492
"source_location = SkyCoord('17:43:04.4879', '+66:55:01.837', unit=(u.hourangle, u.deg))\n",
494493
"ref_wcs = ref_fits.get_fits_wcs()\n",
495-
"ref_y,ref_x = skycoord_to_pixel(source_location, ref_wcs)\n",
494+
"ref_y, ref_x = skycoord_to_pixel(source_location, ref_wcs)\n",
496495
"ref_cutout = extract_array(ref_data, (21, 21), (ref_x, ref_y))\n",
497496
"\n",
498497
"# The scale should highlight the background noise so it is possible to see all faint sources.\n",
499498
"norm1 = simple_norm(ref_cutout, stretch='log', min_cut=4.3, max_cut=15)\n",
500-
"plt.imshow(ref_cutout,origin='lower', norm=norm1, cmap='gray')\n",
499+
"plt.imshow(ref_cutout, origin='lower', norm=norm1, cmap='gray')\n",
501500
"clb = plt.colorbar()\n",
502501
"clb.set_label('MJy/Str', labelpad=-40, y=1.05, rotation=0)\n",
503502
"plt.title('PID1028,Obs006')\n",
@@ -539,9 +538,9 @@
539538
"source": [
540539
"# Zoom in to see the source\n",
541540
"source_location = SkyCoord('17:43:04.4879', '+66:55:01.837', unit=(u.hourangle, u.deg))\n",
542-
"ref_y,ref_x = skycoord_to_pixel(source_location, wcs.WCS(ref_fits['SCI', 1], ref_fits))\n",
543-
"ref_cutout = extract_array(ref_data,(11, 11), (ref_x, ref_y))\n",
544-
"norm1 = simple_norm(ref_cutout,stretch='linear', min_cut=-1, max_cut=10)\n",
541+
"ref_y, ref_x = skycoord_to_pixel(source_location, wcs.WCS(ref_fits['SCI', 1], ref_fits))\n",
542+
"ref_cutout = extract_array(ref_data, (11, 11), (ref_x, ref_y))\n",
543+
"norm1 = simple_norm(ref_cutout, stretch='linear', min_cut=-1, max_cut=10)\n",
545544
"plt.imshow(ref_cutout, origin='lower', norm=norm1, cmap='gray')\n",
546545
"plt.title('PID1028,Obs006')\n",
547546
"plt.gca().tick_params(labelcolor='none', axis='both', color='none')\n",
@@ -569,14 +568,14 @@
569568
"# 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",
570569
"# The source code for psfs = space_phot.get_jwst_psf(jwst_obs, source_location) is as follows:\n",
571570
"\n",
572-
" # c = webbpsf.gridded_library.CreatePSFLibrary(inst, inst.filter, num_psfs = 1, psf_location=(x,y), fov_pixels=psf_width, detectors=st_obs.detector, save=False)\n",
573-
" # grid = c.create_grid()\n",
571+
"# c = webbpsf.gridded_library.CreatePSFLibrary(inst, inst.filter, num_psfs = 1, psf_location=(x, y), fov_pixels=psf_width, detectors=st_obs.detector, save=False)\n",
572+
"# grid = c.create_grid()\n",
574573
"\n",
575574
"# In most cases, we assume the user wishes to simply grab the default WebbPSF. Advanced users should consider using webbpsf.\n",
576575
"# In that case, you would use the code below.<br>\n",
577576
"\n",
578-
" # psf_model = inst.calc_psf(oversample=4,normalize='last') # creates a psf model in webbpsf\n",
579-
" # psfs = photutils.psf.FittableImageModel(psf[0].data*16,normalize=False,oversampling=oversampling) # turns the psf into a fittableimage model to improve processing time\n",
577+
"# psf_model = inst.calc_psf(oversample=4,normalize='last') # creates a psf model in webbpsf\n",
578+
"# psfs = photutils.psf.FittableImageModel(psf[0].data*16,normalize=False,oversampling=oversampling) # turns the psf into a fittableimage model to improve processing time\n",
580579
"\n",
581580
"# There are more advanced methods for generating your WebbPSF, but those are beyond the scope of this notebook.\n",
582581
"# Useful reference: https://webbpsf.readthedocs.io/en/latest/api/webbpsf.JWInstrument.html#webbpsf.JWInstrument.calc_psf"
@@ -605,7 +604,7 @@
605604
"outputs": [],
606605
"source": [
607606
"# The scale should highlight the background noise so it is possible to see all faint sources.\n",
608-
"ref_cutout = extract_array(psfs[0].data,(41, 41), (122, 122))\n",
607+
"ref_cutout = extract_array(psfs[0].data, (41, 41), (122, 122))\n",
609608
"norm1 = simple_norm(ref_cutout, stretch='log', min_cut=0.0, max_cut=0.2)\n",
610609
"plt.imshow(ref_cutout, origin='lower', norm=norm1, cmap='gray')\n",
611610
"clb = plt.colorbar()\n",
@@ -660,13 +659,20 @@
660659
"outputs": [],
661660
"source": [
662661
"# Do PSF Photometry using space_phot (details of fitting are in documentation)\n",
663-
"jwst_obs.psf_photometry(psfs, source_location, bounds={'flux' :[-10000, 10000],\n",
664-
" 'centroid' :[-2, 2],\n",
665-
" 'bkg' :[0, 50]},\n",
666-
" fit_width=7,\n",
667-
" fit_centroid='pixel',\n",
668-
" fit_bkg=True,\n",
669-
" fit_flux='single')\n",
662+
"jwst_obs.psf_photometry(\n",
663+
" psfs,\n",
664+
" source_location,\n",
665+
" bounds={\n",
666+
" 'flux': [-10000, 10000],\n",
667+
" 'centroid': [-2, 2],\n",
668+
" 'bkg': [0, 50],\n",
669+
" },\n",
670+
" fit_width=7,\n",
671+
" fit_centroid='pixel',\n",
672+
" fit_bkg=True,\n",
673+
" fit_flux='single'\n",
674+
")\n",
675+
"\n",
670676
"jwst_obs.plot_psf_fit()\n",
671677
"plt.show()\n",
672678
"\n",
@@ -794,7 +800,7 @@
794800
"source_location = SkyCoord('17:43:04.4879', '+66:55:01.837', unit=(u.hourangle, u.deg))\n",
795801
"\n",
796802
"ref_wcs = ref_fits.get_fits_wcs()\n",
797-
"ref_y,ref_x = skycoord_to_pixel(source_location, ref_wcs)\n",
803+
"ref_y, ref_x = skycoord_to_pixel(source_location, ref_wcs)\n",
798804
"ref_cutout = extract_array(ref_data, (21, 21), (ref_x, ref_y))\n",
799805
"\n",
800806
"# The scale should highlight the background noise so it is possible to see all faint sources.\n",
@@ -842,9 +848,9 @@
842848
"# The function get_jwst_psf is a space_phot wrapper for the WebbPSF calc_psf function and uses a lot of the same keywords.\n",
843849
"# There are more advanced methods for generating your WebbPSF, but those are beyond the scope of this notebook.\n",
844850
"# The defaults used by get_jwst_psf in this notebook are:\n",
845-
" # oversample=4\n",
846-
" # normalize='last'\n",
847-
" # Non-distorted PSF\n",
851+
"# oversample=4\n",
852+
"# normalize='last'\n",
853+
"# Non-distorted PSF\n",
848854
"# Useful reference: https://webbpsf.readthedocs.io/en/latest/api/webbpsf.JWInstrument.html#webbpsf.JWInstrument.calc_psf\n",
849855
"\n",
850856
"# Get PSF from WebbPSF\n",
@@ -900,12 +906,18 @@
900906
"source": [
901907
"# Do PSF Photometry using space_phot (details of fitting are in documentation)\n",
902908
"# See detailed notes in Section 3.1 above about the fitting process and diagnostics\n",
903-
"jwst3_obs.psf_photometry(psf3, source_location, bounds={'flux' :[-10000, 10000],\n",
904-
" 'centroid' :[-2, 2],\n",
905-
" 'bkg' :[0, 50]},\n",
906-
" fit_width=9,\n",
907-
" fit_bkg=True,\n",
908-
" fit_flux=True)\n",
909+
"jwst3_obs.psf_photometry(\n",
910+
" psf3,\n",
911+
" source_location,\n",
912+
" bounds={\n",
913+
" 'flux': [-10000, 10000],\n",
914+
" 'centroid': [-2, 2],\n",
915+
" 'bkg': [0, 50],\n",
916+
" },\n",
917+
" fit_width=9,\n",
918+
" fit_bkg=True,\n",
919+
" fit_flux=True\n",
920+
")\n",
909921
"\n",
910922
"jwst_obs.plot_psf_fit()\n",
911923
"plt.show()\n",
@@ -1057,7 +1069,7 @@
10571069
"# Pick a blank part of the sky to calculate the upper limit\n",
10581070
"source_location = SkyCoord('17:43:00.0332', '+66:54:42.677', unit=(u.hourangle, u.deg))\n",
10591071
"ref_wcs = ref_fits.get_fits_wcs()\n",
1060-
"ref_y,ref_x = skycoord_to_pixel(source_location, ref_wcs)\n",
1072+
"ref_y, ref_x = skycoord_to_pixel(source_location, ref_wcs)\n",
10611073
"ref_cutout = extract_array(ref_data, (21, 21), (ref_x, ref_y))\n",
10621074
"\n",
10631075
"# The scale should highlight the background noise so it is possible to see all faint sources.\n",
@@ -1104,8 +1116,8 @@
11041116
"source": [
11051117
"# Pick a blank part of the sky to calculate the upper limit\n",
11061118
"source_location = SkyCoord('17:43:00.0332', '+66:54:42.677', unit=(u.hourangle, u.deg))\n",
1107-
"ref_y,ref_x = skycoord_to_pixel(source_location, wcs.WCS(ref_fits['SCI', 1], ref_fits))\n",
1108-
"ref_cutout = extract_array(ref_data,(11, 11),( ref_x, ref_y))\n",
1119+
"ref_y, ref_x = skycoord_to_pixel(source_location, wcs.WCS(ref_fits['SCI', 1], ref_fits))\n",
1120+
"ref_cutout = extract_array(ref_data, (11, 11), (ref_x, ref_y))\n",
11091121
"norm1 = simple_norm(ref_cutout, stretch='linear', min_cut=-1, max_cut=10)\n",
11101122
"\n",
11111123
"plt.imshow(ref_cutout, origin='lower', norm=norm1, cmap='gray')\n",
@@ -1126,9 +1138,9 @@
11261138
"# The function get_jwst_psf is a space_phot wrapper for the WebbPSF calc_psf function and uses a lot of the same keywords.\n",
11271139
"# There are more advanced methods for generating your WebbPSF, but those are beyond the scope of this notebook.\n",
11281140
"# The defaults used by get_jwst_psf in this notebook are:\n",
1129-
" # oversample=4\n",
1130-
" # normalize='last'\n",
1131-
" # Non-distorted PSF\n",
1141+
"# oversample=4\n",
1142+
"# normalize='last'\n",
1143+
"# Non-distorted PSF\n",
11321144
"# Useful reference: https://webbpsf.readthedocs.io/en/latest/api/webbpsf.JWInstrument.html#webbpsf.JWInstrument.calc_psf\n",
11331145
"\n",
11341146
"# Get PSF from WebbPSF\n",
@@ -1168,12 +1180,18 @@
11681180
"source": [
11691181
"# Do PSF Photometry using space_phot (details of fitting are in documentation)\n",
11701182
"# https://st-phot.readthedocs.io/en/latest/examples/plot_a_psf.html#jwst-images\n",
1171-
"jwst_obs.psf_photometry(psfs,source_location, bounds={'flux' :[-10, 1000],\n",
1172-
" 'bkg' :[0, 50]},\n",
1173-
" fit_width=5,\n",
1174-
" fit_bkg=True,\n",
1175-
" fit_centroid='fixed',\n",
1176-
" fit_flux='single')\n",
1183+
"jwst_obs.psf_photometry(\n",
1184+
" psfs,\n",
1185+
" source_location,\n",
1186+
" bounds={\n",
1187+
" 'flux': [-10, 1000],\n",
1188+
" 'bkg': [0, 50],\n",
1189+
" },\n",
1190+
" fit_width=5,\n",
1191+
" fit_bkg=True,\n",
1192+
" fit_centroid='fixed',\n",
1193+
" fit_flux='single'\n",
1194+
")\n",
11771195
"jwst_obs.plot_psf_fit()\n",
11781196
"plt.show()\n",
11791197
"\n",
@@ -1256,7 +1274,7 @@
12561274
"# Pick a blank part of the sky to calculate the upper limit\n",
12571275
"source_location = SkyCoord('17:43:00.0332', '+66:54:42.677', unit=(u.hourangle, u.deg))\n",
12581276
"ref_wcs = ref_fits.get_fits_wcs()\n",
1259-
"ref_y,ref_x = skycoord_to_pixel(source_location, ref_wcs)\n",
1277+
"ref_y, ref_x = skycoord_to_pixel(source_location, ref_wcs)\n",
12601278
"ref_cutout = extract_array(ref_data, (21, 21), (ref_x, ref_y))\n",
12611279
"\n",
12621280
"# The scale should highlight the background noise so it is possible to see all faint sources.\n",
@@ -1303,7 +1321,7 @@
13031321
"# Pick a blank part of the sky to calculate the upper limit\n",
13041322
"source_location = SkyCoord('17:43:00.0332', '+66:54:42.677', unit=(u.hourangle, u.deg))\n",
13051323
"\n",
1306-
"ref_y,ref_x = skycoord_to_pixel(source_location, wcs.WCS(ref_fits['SCI', 1], ref_fits))\n",
1324+
"ref_y, ref_x = skycoord_to_pixel(source_location, wcs.WCS(ref_fits['SCI', 1], ref_fits))\n",
13071325
"ref_cutout = extract_array(ref_data, (11, 11), (ref_x, ref_y))\n",
13081326
"norm1 = simple_norm(ref_cutout, stretch='linear', min_cut=-1, max_cut=10)\n",
13091327
"\n",
@@ -1326,9 +1344,9 @@
13261344
"# The function get_jwst_psf is a space_phot wrapper for the WebbPSF calc_psf function and uses a lot of the same keywords.\n",
13271345
"# There are more advanced methods for generating your WebbPSF, but those are beyond the scope of this notebook.\n",
13281346
"# The defaults used by get_jwst_psf in this notebook are:\n",
1329-
" # oversample=4\n",
1330-
" # normalize='last'\n",
1331-
" # Non-distorted PSF\n",
1347+
"# oversample=4\n",
1348+
"# normalize='last'\n",
1349+
"# Non-distorted PSF\n",
13321350
"# Useful reference: https://webbpsf.readthedocs.io/en/latest/api/webbpsf.JWInstrument.html#webbpsf.JWInstrument.calc_psf\n",
13331351
"\n",
13341352
"# Get PSF from WebbPSF\n",
@@ -1366,12 +1384,18 @@
13661384
},
13671385
"outputs": [],
13681386
"source": [
1369-
"jwst3_obs.psf_photometry(psf3,source_location, bounds={'flux' :[-1000, 1000],\n",
1370-
" 'bkg' :[0, 50]},\n",
1371-
" fit_width=9,\n",
1372-
" fit_bkg=True, \n",
1373-
" fit_centroid=False,\n",
1374-
" fit_flux=True)\n",
1387+
"jwst3_obs.psf_photometry(\n",
1388+
" psf3,\n",
1389+
" source_location,\n",
1390+
" bounds={\n",
1391+
" 'flux': [-1000, 1000],\n",
1392+
" 'bkg': [0, 50],\n",
1393+
" },\n",
1394+
" fit_width=9,\n",
1395+
" fit_bkg=True,\n",
1396+
" fit_centroid=False,\n",
1397+
" fit_flux=True\n",
1398+
")\n",
13751399
"\n",
13761400
"jwst3_obs.plot_psf_fit()\n",
13771401
"plt.show()\n",
@@ -1746,14 +1770,20 @@
17461770
" xys = [jwst_obs.wcs_list[i].world_to_pixel(source_location) for i in range(jwst_obs.n_exposures)]\n",
17471771
" bkg = [localbkg_estimator(jwst_obs.data_arr_pam[i], xys[i][0], xys[i][0]) for i in range(jwst_obs.n_exposures)]\n",
17481772
" print(bkg)\n",
1749-
" jwst_obs.psf_photometry(psfs, source_location, bounds={'flux' :[-100000, 100000],\n",
1750-
" 'centroid' :[-2., 2.]},\n",
1751-
" fit_width=5,\n",
1752-
" fit_bkg=False,\n",
1753-
" fit_centroid='wcs',\n",
1754-
" background=bkg,\n",
1755-
" fit_flux='single',\n",
1756-
" maxiter=None)\n",
1773+
" jwst_obs.psf_photometry(\n",
1774+
" psfs,\n",
1775+
" source_location,\n",
1776+
" bounds={\n",
1777+
" 'flux': [-100000, 100000],\n",
1778+
" 'centroid': [-2.0, 2.0],\n",
1779+
" },\n",
1780+
" fit_width=5,\n",
1781+
" fit_bkg=False,\n",
1782+
" fit_centroid='wcs',\n",
1783+
" background=bkg,\n",
1784+
" fit_flux='single',\n",
1785+
" maxiter=None\n",
1786+
" )\n",
17571787
" \n",
17581788
" jwst_obs.plot_psf_fit()\n",
17591789
" plt.show()\n",
@@ -1762,14 +1792,21 @@
17621792
"\n",
17631793
" fit_location = SkyCoord(ra, dec, unit=u.deg)\n",
17641794
" \n",
1765-
" jwst_obs.psf_photometry(psfs, fit_location, bounds={'flux' :[-100000, 100000],\n",
1766-
" 'centroid' :[-2., 2.]},\n",
1767-
" fit_width=5,\n",
1768-
" fit_bkg=False,\n",
1769-
" background=bkg,\n",
1770-
" fit_centroid='fixed',\n",
1771-
" fit_flux='single',\n",
1772-
" maxiter=None)\n",
1795+
" jwst_obs.psf_photometry(\n",
1796+
" psfs,\n",
1797+
" fit_location,\n",
1798+
" bounds={\n",
1799+
" 'flux': [-100000, 100000],\n",
1800+
" 'centroid': [-2.0, 2.0],\n",
1801+
" },\n",
1802+
" fit_width=5,\n",
1803+
" fit_bkg=False,\n",
1804+
" background=bkg,\n",
1805+
" fit_centroid='fixed',\n",
1806+
" fit_flux='single',\n",
1807+
" maxiter=None\n",
1808+
" )\n",
1809+
"\n",
17731810
"\n",
17741811
" jwst_obs.plot_psf_fit()\n",
17751812
" plt.show()\n",
@@ -1925,26 +1962,39 @@
19251962
" xys = [jwst3_obs.wcs.world_to_pixel(source_location)]\n",
19261963
" bkg = [localbkg_estimator(jwst3_obs.data, xys[0][0], xys[0][1])]\n",
19271964
" print(bkg)\n",
1928-
" jwst3_obs.psf_photometry(psf3,source_location, bounds={'flux' :[-100000, 100000],\n",
1929-
" 'centroid' :[-2., 2.]},\n",
1930-
" fit_width=5,\n",
1931-
" fit_bkg=False,\n",
1932-
" background=bkg,\n",
1933-
" fit_flux=True)\n",
1965+
" jwst3_obs.psf_photometry(\n",
1966+
" psf3,\n",
1967+
" source_location,\n",
1968+
" bounds={\n",
1969+
" 'flux': [-100000, 100000],\n",
1970+
" 'centroid': [-2.0, 2.0],\n",
1971+
" },\n",
1972+
" fit_width=5,\n",
1973+
" fit_bkg=False,\n",
1974+
" background=bkg,\n",
1975+
" fit_flux=True\n",
1976+
" )\n",
19341977
"\n",
19351978
" ra = jwst3_obs.psf_result.phot_cal_table['ra'][0]\n",
19361979
" dec = jwst3_obs.psf_result.phot_cal_table['dec'][0]\n",
19371980
" fit_location = SkyCoord(ra, dec, unit=u.deg)\n",
19381981
" jwst3_obs.aperture_photometry(fit_location, encircled_energy=70)\n",
19391982
" print(jwst3_obs.aperture_result.phot_cal_table['mag'])\n",
19401983
" \n",
1941-
" jwst3_obs.psf_photometry(psf3, fit_location, bounds={'flux' :[-100000, 100000],\n",
1942-
" 'centroid' :[-2., 2.]},\n",
1943-
" fit_width=5,\n",
1944-
" fit_bkg=False,\n",
1945-
" fit_centroid=False,\n",
1946-
" background=bkg,\n",
1947-
" fit_flux=True)\n",
1984+
" jwst3_obs.psf_photometry(\n",
1985+
" psf3,\n",
1986+
" fit_location,\n",
1987+
" bounds={\n",
1988+
" 'flux': [-100000, 100000],\n",
1989+
" 'centroid': [-2.0, 2.0],\n",
1990+
" },\n",
1991+
" fit_width=5,\n",
1992+
" fit_bkg=False,\n",
1993+
" fit_centroid=False,\n",
1994+
" background=bkg,\n",
1995+
" fit_flux=True\n",
1996+
" )\n",
1997+
"\n",
19481998
"\n",
19491999
" jwst3_obs.plot_psf_fit()\n",
19502000
" plt.show()\n",

0 commit comments

Comments
 (0)