{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# How to connect observations to specific models?" ] }, { "cell_type": "raw", "metadata": {}, "source": [ "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In the previous examples there was always a single background model component to describe the residual particle background in the various dataset. This implies that the spatial and spectral shape of the background distribution is assumed to be identical for all observations. This is fine in a simulation, but for a real life situation this assumption will probably not hold.\n", "\n", "Start by importing the gammalib, ctools, and cscripts Python modules." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import gammalib\n", "import ctools\n", "import cscripts" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Simulating the dataset\n", "\n", "Let’s start with creating a pointing definition ASCII file for two 30 min pointings near the Crab." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "f = open('pnt.def', 'wb')\n", "f.write('name,ra,dec,duration\\n')\n", "f.write('Crab,83.63,21.51,1800.0\\n')\n", "f.write('Crab,83.63,22.51,1800.0\\n')\n", "f.close()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Inspect the file that you just created. For that purpose let's create a `peek()` function that will also be used later to display XML files." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "name,ra,dec,duration\n", "Crab,83.63,21.51,1800.0\n", "Crab,83.63,22.51,1800.0\n" ] } ], "source": [ "def peek(filename):\n", " f = open(gammalib.expand_env(filename), 'r')\n", " for line in f:\n", " print(line.rstrip())\n", " f.close()\n", "peek('pnt.def')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A pointing definition file is an ASCII file in Comma Separated Values (CSV) format that specifies one pointing per row. The file provides the `name` of the observation, the Right Ascension `ra` and Declination `dec` of the pointing, and its `duration`. Additional optional columns are possible (defining for example the energy range or the Instrument Response Function), but for this simulation the provided information is sufficient.\n", "\n", "Now transform the pointing definition file into an observation definition XML file using the csobsdef script." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "obsdef = cscripts.csobsdef()\n", "obsdef['inpnt'] = 'pnt.def'\n", "obsdef['outobs'] = 'obs.xml'\n", "obsdef['caldb'] = 'prod2'\n", "obsdef['irf'] = 'South_0.5h'\n", "obsdef.execute()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's peek the resulting observation definition XML file" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "