From c7fcca9538a45204d2a68cb33d169eade294e60b Mon Sep 17 00:00:00 2001 From: moralejo Date: Wed, 30 Oct 2024 12:11:11 +0000 Subject: [PATCH] Search for gammapy data directory automatically --- notebooks/LST1_observation_simulator.ipynb | 33 ++++++++++++++-------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/notebooks/LST1_observation_simulator.ipynb b/notebooks/LST1_observation_simulator.ipynb index 944005013..cd68c5565 100644 --- a/notebooks/LST1_observation_simulator.ipynb +++ b/notebooks/LST1_observation_simulator.ipynb @@ -12,10 +12,13 @@ "import numpy as np\n", "import astropy.units as u\n", "import matplotlib.pyplot as plt\n", + "import gammapy\n", + "from gammapy.modeling.models import EBLAbsorptionNormSpectralModel\n", + "import subprocess\n", "from pyirf.spectral import CRAB_MAGIC_JHEAP2015, PowerLaw, LogParabola\n", "from pyirf.statistics import li_ma_significance\n", "from scipy.stats import moyal, norm, skewnorm\n", - "from gammapy.modeling.models import EBLAbsorptionNormSpectralModel" + "from pathlib import Path" ] }, { @@ -172,7 +175,7 @@ }, { "cell_type": "markdown", - "id": "9798d5c3", + "id": "ba8de510", "metadata": {}, "source": [ "## Pulsar mode\n", @@ -182,7 +185,7 @@ { "cell_type": "code", "execution_count": null, - "id": "8d8cff5b", + "id": "8e3139e8", "metadata": {}, "outputs": [], "source": [ @@ -223,7 +226,7 @@ }, { "cell_type": "markdown", - "id": "f243b138", + "id": "4ccf4294", "metadata": {}, "source": [ "## Source extension\n", @@ -235,7 +238,7 @@ { "cell_type": "code", "execution_count": null, - "id": "2ab033a7", + "id": "cb352dca", "metadata": { "scrolled": false }, @@ -278,12 +281,20 @@ "# We will apply the Dominguez EBL model to simulate the absorption\n", "\n", "\n", + "# Make sure we have the necessary EBL absorption data:\n", "try:\n", " os.environ['GAMMAPY_DATA']\n", "except:\n", - " # SET HERE THE GAMMAPY_DATA ENV VARIABLE IN CASE IT IS NOT SET\n", - " # YOU MUST SET THE PATH TO THE CORRESPONDING DIRECTORY IN THE CONDA ENVIRONMENT YOU ARE USING:\n", - " os.environ['GAMMAPY_DATA'] = '/fefs/aswg/workspace/abelardo.moralejo/miniconda3/envs/lst-dev/lib/python3.11/site-packages/gammapy/gammapy-datasets/1.1'\n", + " # WE SET HERE THE GAMMAPY_DATA ENV VARIABLE IN CASE IT IS NOT SET\n", + " gammapy_dir = Path(gammapy.__file__).parent\n", + " gammapy_dir\n", + "\n", + " ebl_file = subprocess.run(['find', str(gammapy_dir), '-name', 'ebl_dominguez11.fits.gz'], \n", + " stdout=subprocess.PIPE).stdout.decode()\n", + " gammapy_data = ebl_file[:ebl_file.find('/ebl/')]\n", + " os.environ['GAMMAPY_DATA'] = gammapy_data\n", + " print('Set GAMMAPY_DATA to', gammapy_data)\n", + "\n", "\n", "dominguez = EBLAbsorptionNormSpectralModel.read_builtin(\"dominguez\", redshift=redshift)" ] @@ -664,7 +675,7 @@ { "cell_type": "code", "execution_count": null, - "id": "7295189b", + "id": "e91f0070", "metadata": {}, "outputs": [], "source": [ @@ -721,7 +732,7 @@ { "cell_type": "code", "execution_count": null, - "id": "bfdf90f7", + "id": "5113ba34", "metadata": {}, "outputs": [], "source": [ @@ -939,7 +950,7 @@ { "cell_type": "code", "execution_count": null, - "id": "1ca87a15", + "id": "f9c2b653", "metadata": {}, "outputs": [], "source": []