Skip to content

Commit

Permalink
Fix non plotting due to index errors (#316)
Browse files Browse the repository at this point in the history
* Fix non plotting due to index errors

* Fix style errors, and simplify env setup logic

* Change referenced imap files
  • Loading branch information
haticekaratay authored Oct 11, 2024
1 parent 1a2cd30 commit 0a4ae95
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 67 deletions.
105 changes: 45 additions & 60 deletions notebooks/COS/SplitTag/SplitTag.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@
"\n",
"# For supressing an unhelpful warning:\n",
"import warnings\n",
"from astropy.units import UnitsWarning"
"from astropy.units import UnitsWarning\n",
"\n",
"from IPython.display import clear_output"
]
},
{
Expand Down Expand Up @@ -524,60 +526,22 @@
"## 3.1 Getting the `lref` reference files\n",
"To run `CalCOS`, your computer will need an `lref` system variable to tell the pipeline where to find your reference files. If you are on the STScI internet network (including via VPN), you may set this to the shared Institute `lref` directory. However, if you're not on the network, you will need to use the `CRDS` command to download the necessary reference files. This process is described in detail in Chapter 3 of our [notebook on setting up an environment for working with COS data](https://github.com/spacetelescope/hst_notebooks/blob/main/notebooks/COS/Setup/Setup.ipynb#crdsS). \n",
"\n",
"The code in this cell ensures that the \"lref\" path is correctly set in the environment variables. If \"lref\" is not already defined, it will be defined and the crds files will be downloaded. If you have already downloaded the files, the CRDS cache on your computer will be used, as shown in the \"else\" block."
"The code in this cell ensures that the \"lref\" path is correctly set in the environment variables and the crds files will be downloaded. "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cleared-tradition",
"id": "7e4d0889-be32-46b1-a0cf-c2bd232132e4",
"metadata": {},
"outputs": [],
"source": [
"# Check if \"lref\" is not set in the environment variables\n",
"if not os.environ.get('lref'):\n",
" # If on STScI Network/VPN\n",
" if os.path.exists('/grp/hst/cdbs/lref/'):\n",
" os.environ['lref'] = '/grp/hst/cdbs/lref/'\n",
" print(\"It looks like you are on the STScI \" +\n",
" \"network; setting lref to '/grp/hst/cdbs/lref/'\")\n",
" # If not on STScI Network/VPN\n",
" else:\n",
" if os.name == \"nt\":\n",
" home_directory = os.environ[\"USERPROFILE\"]\n",
" home_directory = os.environ[\"HOME\"]\n",
" crds_path = os.path.join(home_directory, \"crds_cache\")\n",
" os.environ[\"CRDS_PATH\"] = crds_path\n",
" # URL for the STScI CRDS page\n",
" crds_server_url = \"https://hst-crds.stsci.edu\"\n",
" # Setting env variable to URL\n",
" os.environ[\"CRDS_SERVER_URL\"] = crds_server_url\n",
" # Synchronize CRDS and fetch references\n",
" !crds sync --contexts hst_cos_0347.imap --fetch-references\n",
" lref = os.path.join(crds_path, \"references/hst/cos\")\n",
" os.environ['lref'] = lref\n",
" # Check if that path exists\n",
" if not os.path.exists(os.environ['lref']):\n",
" print(\"It doesn't look like that's a valid path. Deleting it.\")\n",
" # delete this nonexistant path\n",
" del os.environ['lref']\n",
"else:\n",
" # Set home_directory\n",
" # For windows: \n",
" if os.name == \"nt\":\n",
" home_directory = os.environ[\"USERPROFILE\"]\n",
" # Other operating systems\n",
" else:\n",
" home_directory = os.path.expanduser(\"~\")\n",
" # Get the home directory from the lref\n",
" lref = os.environ['lref']\n",
" # Replace the {HOME} placeholder with the home directory path\n",
" expanded_path = lref.replace(\"{HOME}\", home_directory)\n",
" os.environ['lref'] = expanded_path\n",
" print(f\"You already have an lref path in your env variables: {lref}\\n\")\n",
"\n",
"assert os.path.exists(os.environ['lref']), \"Path to lref directory \" + \\\n",
" \"is invalid ({os.environ['lref']})\""
"os.environ[\"CRDS_SERVER_URL\"] = \"https://hst-crds.stsci.edu\"\n",
"os.environ[\"CRDS_PATH\"] = './crds_cache'\n",
"os.environ['lref'] = \"./crds_cache/references/hst/cos\"\n",
"\n",
"!crds sync --contexts hst_cos_0358.imap --fetch-references \n",
"clear_output()"
]
},
{
Expand Down Expand Up @@ -642,6 +606,10 @@
"source": [
"# Find all the `x1d` files:\n",
"processed_files = sorted(glob.glob('output/calcos/epoch*/*x1d.fits'))\n",
"num_files = len(processed_files)\n",
"\n",
"cmap = plt.colormaps.get_cmap('viridis')\n",
"colors = np.linspace(0, 1, len(processed_files))\n",
"\n",
"# Set up figure\n",
"plt.figure(figsize=(18, 12))\n",
Expand All @@ -653,6 +621,9 @@
" # Mark the transit\n",
" if \"2\" in epoch_label:\n",
" epoch_label += \" (transit)\"\n",
" alpha_value = 1\n",
" else:\n",
" alpha_value = 0.5\n",
"\n",
" with warnings.catch_warnings():\n",
" warnings.filterwarnings('ignore',\n",
Expand All @@ -669,8 +640,8 @@
" # Plot each epoch\n",
" plt.plot(w, f,\n",
" # Epoch2 should stand out\n",
" alpha=[0.5, 1, 0.5][i],\n",
" c=['b', 'purple', 'r'][i],\n",
" alpha=alpha_value,\n",
" c=cmap(colors[i]),\n",
" # Label with the epoch name\n",
" label=epoch_label)\n",
"\n",
Expand Down Expand Up @@ -720,6 +691,9 @@
" epoch_label = processed_files[i].split('/')[2]\n",
" if \"2\" in epoch_label:\n",
" epoch_label += \" (transit)\"\n",
" alpha_value = 1\n",
" else:\n",
" alpha_value = 0.5\n",
"\n",
" with warnings.catch_warnings():\n",
" warnings.filterwarnings('ignore',\n",
Expand All @@ -736,20 +710,20 @@
" # Plot each epoch\n",
" ax0.plot(w, f,\n",
" # Epoch2 should stand out\n",
" alpha=[0.5, 1, 0.5][i],\n",
" c=['b', 'purple', 'r'][i],\n",
" alpha=alpha_value,\n",
" c=cmap(colors[i]),\n",
" linestyle='-',\n",
" # Label with the epoch name\n",
" label=epoch_label)\n",
"\n",
" # Plot each epoch\n",
" ax1.errorbar(w, f, yerr=ferr,\n",
" # Epoch2 should stand out\n",
" alpha=[0.5, 1, 0.5][i],\n",
" alpha=alpha_value,\n",
" marker='.',\n",
" markerfacecolor=['b', 'purple', 'r'][i],\n",
" markerfacecolor=cmap(colors[i]),\n",
" linestyle='',\n",
" ecolor=['b', 'purple', 'r'][i],\n",
" ecolor=cmap(colors[i]),\n",
" # Label with the epoch name\n",
" label=epoch_label)\n",
"\n",
Expand Down Expand Up @@ -808,6 +782,9 @@
" epoch_label = processed_files[i].split('/')[2]\n",
" if \"2\" in epoch_label:\n",
" epoch_label += \" (transit)\"\n",
" alpha_value = 1\n",
" else:\n",
" alpha_value = 0.5\n",
"\n",
" with warnings.catch_warnings():\n",
" warnings.filterwarnings('ignore',\n",
Expand All @@ -824,20 +801,20 @@
" # Plot each epoch\n",
" ax0.plot(w, f,\n",
" # Epoch2 should stand out\n",
" alpha=[0.5, 1, 0.5][i],\n",
" c=['b', 'purple', 'r'][i],\n",
" alpha=alpha_value,\n",
" c=cmap(colors[i]),\n",
" linestyle='-',\n",
" # Label with the epoch name\n",
" label=epoch_label)\n",
"\n",
" # Plot each epoch\n",
" ax1.errorbar(w, f, yerr=ferr,\n",
" # Epoch2 should stand out\n",
" alpha=[0.5, 1, 0.5][i],\n",
" alpha=alpha_value,\n",
" marker='.',\n",
" markerfacecolor=['b', 'purple', 'r'][i],\n",
" markerfacecolor=cmap(colors[i]),\n",
" linestyle='',\n",
" ecolor=['b', 'purple', 'r'][i],\n",
" ecolor=cmap(colors[i]),\n",
" # Label with the epoch name\n",
" label=epoch_label)\n",
"\n",
Expand Down Expand Up @@ -1270,6 +1247,14 @@
"\n",
"Note that epoch 1 contains all the counts from epoch 1.5, while epoch 2 does not. Thus this is not a perfect test. However, the vast majority of epoch 1's counts do *not* overlap with epoch 1.5 (see fig 2.1), so this is an acceptable \"first pass\" test. It's also possible that the very low number of counts, especially in epochs 1.5 and 2, does not give us the signal-to-noise we would need to accurately distinguish the epochs."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f9380ce0-9f41-48b8-99f8-5871cf53fc02",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -1291,7 +1276,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.12.7"
},
"nbdime-conflicts": {
"local_diff": [
Expand Down
14 changes: 7 additions & 7 deletions notebooks/COS/SplitTag/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
astropy==5.3.3
astroquery==0.4.6
calcos==3.4.4
costools==1.2.6
matplotlib==3.7.0
numpy==1.23.4
crds==11.17.0
astropy>=5.3.3
astroquery>=0.4.6
calcos>=3.4.4
costools>=1.2.6
matplotlib>=3.7.0
numpy>=1.23.4
crds>=11.17.0

0 comments on commit 0a4ae95

Please sign in to comment.