From db6d3510ac6a3344fb88eee1e56001a48bd1842c Mon Sep 17 00:00:00 2001 From: Hatice Karatay Date: Wed, 18 Sep 2024 14:48:14 -0400 Subject: [PATCH] Refactor the shell command exec --- .../align_multiple_visits.ipynb | 64 ++++++++----------- 1 file changed, 26 insertions(+), 38 deletions(-) diff --git a/notebooks/DrizzlePac/align_multiple_visits/align_multiple_visits.ipynb b/notebooks/DrizzlePac/align_multiple_visits/align_multiple_visits.ipynb index b89de636b..4b2f262e0 100644 --- a/notebooks/DrizzlePac/align_multiple_visits/align_multiple_visits.ipynb +++ b/notebooks/DrizzlePac/align_multiple_visits/align_multiple_visits.ipynb @@ -168,8 +168,7 @@ "props = [\"10737\"]\n", "filts = [\"F606W\"]\n", "\n", - "obsTable = Observations.query_criteria(\n", - " obs_id=obs_ids, proposal_id=props, filters=filts)\n", + "obsTable = Observations.query_criteria(obs_id=obs_ids, proposal_id=props, filters=filts)\n", "products = Observations.get_product_list(obsTable)\n", "\n", "data_prod = [\"FLC\"] # ['FLC','FLT','DRC','DRZ']\n", @@ -254,7 +253,7 @@ "]\n", "ext_1_kws = [\"orientat\"]\n", "\n", - "# grab keywords from data\n", + "#grab keywords from data\n", "\n", "for path in paths:\n", " path_data = []\n", @@ -313,8 +312,7 @@ "ext_0_kws = [\"DETECTOR\"]\n", "ext_1_kws = [\"WCSNAME\", \"NMATCHES\", \"RMS_RA\", \"RMS_DEC\"]\n", "\n", - "det_scale = {\"IR\": 0.1283, \"UVIS\": 0.0396,\n", - " \"WFC\": 0.05} # plate scale (arcsec/pixel)\n", + "det_scale = {\"IR\": 0.1283, \"UVIS\": 0.0396, \"WFC\": 0.05} # plate scale (arcsec/pixel)\n", "\n", "# create dictionary to store alignment keywords\n", "format_dict = {}\n", @@ -485,8 +483,7 @@ " updatehdr=False,\n", " reusename=True,\n", ")\n", - "\n", - "clear_output()" + "#clear_output()" ] }, { @@ -499,8 +496,7 @@ "with open(\"shift_rscale.txt\", \"r\") as shift:\n", " for line_number, line in enumerate(shift, start=1):\n", " if \"nan\" in line:\n", - " raise ValueError(\n", - " \"nan found in line {} in shift file\".format(line_number))" + " raise ValueError(\"nan found in line {} in shift file\".format(line_number))" ] }, { @@ -553,15 +549,11 @@ "\n", "# make a subdirectory for storing images\n", "os.makedirs(\"images\", exist_ok=True)\n", - "shutil.copy(f\"residuals_{rootname_A}_flc.png\",\n", - " f\"images/residuals_{rootname_A}_flc_rscale.png\")\n", - "shutil.copy(f\"residuals_{rootname_B}_flc.png\",\n", - " f\"images/residuals_{rootname_B}_flc_rscale.png\")\n", + "shutil.copy(f\"residuals_{rootname_A}_flc.png\", f\"images/residuals_{rootname_A}_flc_rscale.png\")\n", + "shutil.copy(f\"residuals_{rootname_B}_flc.png\", f\"images/residuals_{rootname_B}_flc_rscale.png\")\n", "\n", - "shutil.copy(f\"vector_{rootname_A}_flc.png\",\n", - " f\"images/vector_{rootname_A}_flc_rscale.png\")\n", - "shutil.copy(f\"vector_{rootname_B}_flc.png\",\n", - " f\"images/vector_{rootname_B}_flc_rscale.png\")" + "shutil.copy(f\"vector_{rootname_A}_flc.png\", f\"images/vector_{rootname_A}_flc_rscale.png\")\n", + "shutil.copy(f\"vector_{rootname_B}_flc.png\", f\"images/vector_{rootname_B}_flc_rscale.png\")" ] }, { @@ -723,15 +715,11 @@ "outputs": [], "source": [ "# Give the 'fit residual plots' a unique name for comparison with other tests.\n", - "shutil.copy(f\"residuals_{rootname_A}_flc.png\",\n", - " f\"images/residuals_{rootname_A}_flc_general.png\")\n", - "shutil.copy(f\"residuals_{rootname_B}_flc.png\",\n", - " f\"images/residuals_{rootname_B}_flc_general.png\")\n", + "shutil.copy(f\"residuals_{rootname_A}_flc.png\", f\"images/residuals_{rootname_A}_flc_general.png\")\n", + "shutil.copy(f\"residuals_{rootname_B}_flc.png\", f\"images/residuals_{rootname_B}_flc_general.png\")\n", "\n", - "shutil.copy(f\"vector_{rootname_A}_flc.png\",\n", - " f\"images/vector_{rootname_A}_flc_general.png\")\n", - "shutil.copy(f\"vector_{rootname_B}_flc.png\",\n", - " f\"images/vector_{rootname_B}_flc_general.png\")" + "shutil.copy(f\"vector_{rootname_A}_flc.png\", f\"images/vector_{rootname_A}_flc_general.png\")\n", + "shutil.copy(f\"vector_{rootname_B}_flc.png\", f\"images/vector_{rootname_B}_flc_general.png\")" ] }, { @@ -816,14 +804,14 @@ "\n", "plt.figure(figsize=(7, 7), dpi=140)\n", "\n", - "with fits.open(rootname + \"_flc.fits\") as hdulist:\n", - " chip1_data = hdulist[\"SCI\", 2].data\n", + "with fits.open(rootname + \"_flc.fits\") as hdulist: \n", + " chip1_data = hdulist[\"SCI\", 2].data \n", " chip2_data = hdulist[\"SCI\", 1].data\n", " fullsci = np.concatenate([chip2_data, chip1_data])\n", " zscale = ZScaleInterval()\n", " z1, z2 = zscale.get_limits(fullsci)\n", " plt.imshow(fullsci, cmap=\"Greys\", origin=\"lower\", vmin=z1, vmax=z2)\n", - "\n", + " \n", " match_tab = ascii.read(\n", " rootname + \"_flc_catalog_fit.match\"\n", " ) # load match file in astropy table\n", @@ -835,7 +823,7 @@ " ] # filter table for sources on chip 1 (on ext 4)\n", " x_cord1, y_cord1 = match_tab_chip1[\"col11\"], match_tab_chip1[\"col12\"]\n", " x_cord2, y_cord2 = match_tab_chip2[\"col11\"], match_tab_chip2[\"col12\"]\n", - "\n", + " \n", " plt.scatter(\n", " x_cord1,\n", " y_cord1 + 2051,\n", @@ -875,9 +863,9 @@ " shiftfile=False,\n", " ylimit=0.2,\n", " fitgeometry=\"general\",\n", - " updatehdr=True, # update the header with the new solution\n", + " updatehdr=True, # update the header with the new solution\n", " reusename=True\n", - ")\n", + ") \n", "\n", "clear_output()" ] @@ -926,9 +914,9 @@ "source": [ "# The following lines of code find and download the MDRIZTAB reference file.\n", "mdz = fits.getval(input_flcs[0], 'MDRIZTAB', ext=0).split('$')[1]\n", + "jref = os.environ['jref']\n", "print('Searching for the MDRIZTAB file:', mdz)\n", - "get_mdriztab = os.system('crds sync --hst --files ' +\n", - " mdz + ' --output-dir '+os.environ['jref'])" + "get_mdriztab = !crds sync --hst --files {mdz} --output-dir {jref}" ] }, { @@ -1062,9 +1050,9 @@ "source": [ "plt.figure(figsize=(8, 8))\n", "\n", - "with fits.open(\"f606w_combined_drc.fits\") as hdulist:\n", - " drc_dat = hdulist[\"SCI\", 1].data # final drizzled image in SCI,1 extension\n", - "\n", + "with fits.open(\"f606w_combined_drc.fits\") as hdulist: \n", + " drc_dat = hdulist[\"SCI\", 1].data # final drizzled image in SCI,1 extension\n", + " \n", " z1, z2 = zscale.get_limits(drc_dat)\n", " plt.imshow(drc_dat, origin=\"lower\", vmin=z1, vmax=z2, cmap=\"Greys\")\n", " plt.title(\"F606W drizzled science image\", fontsize=20)\n", @@ -1086,8 +1074,8 @@ "source": [ "plt.figure(figsize=(8, 8))\n", "\n", - "with fits.open(\"f606w_combined_drc.fits\") as hdulist:\n", - " drc_dat = hdulist[\"WHT\", 1].data # final drizzled image in WHT,1 extension\n", + "with fits.open(\"f606w_combined_drc.fits\") as hdulist: \n", + " drc_dat = hdulist[\"WHT\", 1].data # final drizzled image in WHT,1 extension\n", "\n", " z1, z2 = zscale.get_limits(drc_dat)\n", " plt.imshow(drc_dat, origin=\"lower\", vmin=z1, vmax=z2, cmap=\"Greys\")\n",