{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "import astropy.units as u\n",
    "from astropy.coordinates import SkyCoord\n",
    "from astropy.time import Time\n",
    "\n",
    "import matplotlib.pyplot as plt\n",
    "from IPython.display import display\n",
    "from gammapy.analysis import Analysis, AnalysisConfig\n",
    "from gammapy.estimators import LightCurveEstimator\n",
    "from gammapy.modeling.models import (\n",
    "    Models,\n",
    "    PointSpatialModel,\n",
    "    PowerLawSpectralModel,\n",
    "    SkyModel,\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Build 3D dataset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "conf_3d = AnalysisConfig()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "conf_3d.observations.obs_ids = [23523, 23526, 23559, 23592]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "# We want a 3D analysis\n",
    "conf_3d.datasets.type = \"3d\"\n",
    "\n",
    "# We want to extract the data by observation and therefore to not stack them\n",
    "conf_3d.datasets.stack = False\n",
    "\n",
    "# Here is the WCS geometry of the Maps\n",
    "conf_3d.datasets.geom.wcs.skydir = dict(\n",
    "    frame=\"icrs\", lon=83.63308 * u.deg, lat=22.01450 * u.deg\n",
    ")\n",
    "conf_3d.datasets.geom.wcs.binsize = 0.02 * u.deg\n",
    "conf_3d.datasets.geom.wcs.width = dict(width=1 * u.deg, height=1 * u.deg)\n",
    "\n",
    "# We define a value for the IRF Maps binsize\n",
    "conf_3d.datasets.geom.wcs.binsize_irf = 0.2 * u.deg\n",
    "\n",
    "# Define energy binning for the Maps\n",
    "conf_3d.datasets.geom.axes.energy = dict(min=0.7 * u.TeV, max=10 * u.TeV, nbins=5)\n",
    "conf_3d.datasets.geom.axes.energy_true = dict(min=0.3 * u.TeV, max=20 * u.TeV, nbins=20)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Setting logging config: {'level': 'INFO', 'filename': None, 'filemode': None, 'format': None, 'datefmt': None}\n",
      "Fetching observations.\n",
      "Observations selected: 4 out of 4.\n",
      "Number of selected observations: 4\n",
      "Creating reference dataset and makers.\n",
      "Creating the background Maker.\n",
      "No background maker set. Check configuration.\n",
      "Start the data reduction loop.\n",
      "Computing dataset for observation 23523\n",
      "Running MapDatasetMaker\n",
      "Running SafeMaskMaker\n",
      "Computing dataset for observation 23526\n",
      "Running MapDatasetMaker\n",
      "Running SafeMaskMaker\n",
      "Computing dataset for observation 23559\n",
      "Running MapDatasetMaker\n",
      "Running SafeMaskMaker\n",
      "Computing dataset for observation 23592\n",
      "Running MapDatasetMaker\n",
      "Running SafeMaskMaker\n"
     ]
    }
   ],
   "source": [
    "analysis_3d = Analysis(conf_3d)\n",
    "analysis_3d.get_observations()\n",
    "analysis_3d.get_datasets()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "analysis_3d.datasets.write('datasets/LC_3d_dataset.fits')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Building the 1D dataset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "conf_1d = AnalysisConfig()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "conf_1d.observations.obs_ids = [23523, 23526, 23559, 23592]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "# We want a 1D analysis\n",
    "conf_1d.datasets.type = \"1d\"\n",
    "\n",
    "# We want to extract the data by observation and therefore to not stack them\n",
    "conf_1d.datasets.stack = False\n",
    "\n",
    "# Here we define the ON region and make sure that PSF leakage is corrected\n",
    "conf_1d.datasets.on_region = dict(\n",
    "    frame=\"icrs\",\n",
    "    lon=83.63308 * u.deg,\n",
    "    lat=22.01450 * u.deg,\n",
    "    radius=0.1 * u.deg,\n",
    ")\n",
    "conf_1d.datasets.containment_correction = True\n",
    "\n",
    "# Finally we define the energy binning for the spectra\n",
    "conf_1d.datasets.geom.axes.energy = dict(min=0.7 * u.TeV, max=10 * u.TeV, nbins=5)\n",
    "conf_1d.datasets.geom.axes.energy_true = dict(min=0.3 * u.TeV, max=20 * u.TeV, nbins=20)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Setting logging config: {'level': 'INFO', 'filename': None, 'filemode': None, 'format': None, 'datefmt': None}\n",
      "Fetching observations.\n",
      "Observations selected: 4 out of 4.\n",
      "Number of selected observations: 4\n",
      "Reducing spectrum datasets.\n",
      "Creating the background Maker.\n",
      "No background maker set. Check configuration.\n"
     ]
    }
   ],
   "source": [
    "analysis_1d = Analysis(conf_1d)\n",
    "analysis_1d.get_observations()\n",
    "analysis_1d.get_datasets()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "analysis_1d.datasets.write('datasets/LC_1d_dataset.fits')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
