Commit 8898fed9428ed226cd001efc8a64b56fd6e1e6fd

Authored by Jürgen Knödlseder
1 parent e5a3b78c

Integrate script.

Showing 1 changed file with 45 additions and 30 deletions
scripts/cshessobs.py
1 1 #!/usr/bin/env python
  2 +# ==========================================================================
  3 +# Generation of an H.E.S.S. observation definition file.
  4 +#
  5 +# Copyright (C) 2015 Michael Mayer
  6 +#
  7 +# This program is free software: you can redistribute it and/or modify
  8 +# it under the terms of the GNU General Public License as published by
  9 +# the Free Software Foundation, either version 3 of the License, or
  10 +# (at your option) any later version.
  11 +#
  12 +# This program is distributed in the hope that it will be useful,
  13 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  14 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15 +# GNU General Public License for more details.
  16 +#
  17 +# You should have received a copy of the GNU General Public License
  18 +# along with this program. If not, see <http://www.gnu.org/licenses/>.
  19 +#
  20 +# ==========================================================================
2 21 import gammalib
3 22 import ctools
4 23 import sys
5 24 import os
6 25 import glob
7 26  
  27 +
8 28 # =============== #
9 29 # cshessobs class #
10 30 # =============== #
11 31 class cshessobs(ctools.cscript):
12 32 """
13   - This class implements the creation of a observation xml file. It derives
14   - from the GammaLib::GApplication class which provides support for parameter
15   - files, command line arguments, and logging. In that way the Python
16   - script behaves just as a regular ctool. This script is dedicated for use inside
17   - the HESS Collaboration, i.e. it can only be used if you have access to HESS data
18   - in FITS format. The HESSFITS environment has to be set correctly to use this script
  33 + This class implements the creation of a observation xml file for HESS
  34 + data analysis. This class is dedicated for use inside the HESS Collaboration,
  35 + i.e. it can only be used if you have access to HESS data in FITS format.
  36 + The HESSFITS environment has to be set correctly to use this script
19 37 Please see the internal documentation for this purpose:
20 38 https://hess-confluence.desy.de/confluence/display/HESS/HESS+Open+Source+Tools+-+HESS+data+and+IRFs+in+FITS+format
21 39 """
... ... @@ -26,18 +44,19 @@ class cshessobs(ctools.cscript):
26 44 # Set name
27 45 self.name = "cshessobs"
28 46 self.version = "0.1.0"
29   -
30   - # Initialise some members
31   - self.datapath = ""
32   - self.inmodels = None
  47 +
  48 + # Check on existence of HESSFITS environment variable
33 49 try:
34 50 self.datapath = os.environ["HESSFITS"]
35 51 except KeyError:
36   - raise gammalib.GException.env_not_found(self.name, "HESSFITS",
37   - "Please set the environment variable HESSFITS to use this script")
38   -
39   - self.outobs = "obs.xml"
40   - self.nodes = 1
  52 + raise RuntimeError("Environment variable \"HESSFITS\" not set. " \
  53 + "Please set the environment variable \"HESSFITS\" to use this script")
  54 +
  55 + # Initialise some members
  56 + self.datapath = ""
  57 + self.inmodels = None
  58 + self.outobs = "obs.xml"
  59 + self.nodes = 1
41 60  
42 61 # Make sure that parfile exists
43 62 file = self.parfile()
... ... @@ -61,10 +80,6 @@ class cshessobs(ctools.cscript):
61 80 """
62 81 Destructor.
63 82 """
64   - # Write separator into logger
65   - if self.logTerse():
66   - self.log("\n")
67   -
68 83 # Return
69 84 return
70 85  
... ... @@ -144,7 +159,6 @@ class cshessobs(ctools.cscript):
144 159 pars.append(gammalib.GApplicationPar(chain+"_"+dstver+"_config","s","a",checked_folders[chain][dstver][0],avconfigs[:-1],"","Analysis configuration"))
145 160 pars.append(gammalib.GApplicationPar(chain+"_dstver","s","a",checked_folders[chain].keys()[0],avdsts[:-1],"","DST version"))
146 161 pars.append(gammalib.GApplicationPar("chain","s","a",checked_folders.keys()[0],avchains[:-1],"","Analysis chain"))
147   -
148 162 pars.append(gammalib.GApplicationPar("runlistfile","f","a","runlist.lis","","","Runlist file"))
149 163 pars.append(gammalib.GApplicationPar("inmodel","f","h","NONE","","","Input model XML file (optional)"))
150 164 pars.append(gammalib.GApplicationPar("outobs","f","a","obs.xml","","","Observation XML outfile"))
... ... @@ -177,12 +191,12 @@ class cshessobs(ctools.cscript):
177 191 self.outmodel = self["outmodel"].filename()
178 192  
179 193 # Read hidden parameters
180   - self.nodes = self["nodes"].integer()
181   - self.usetrig = self["usetrig"].boolean()
  194 + self.nodes = self["nodes"].integer()
  195 + self.usetrig = self["usetrig"].boolean()
182 196 self.datapath = self["datapath"].string()
183 197  
184 198 # Read run configuration parameters
185   - self.chain = self["chain"].string()
  199 + self.chain = self["chain"].string()
186 200 self.dstver = self[self.chain+"_dstver"].string()
187 201 self.config = self[self.chain+"_"+self.dstver+"_config"].string()
188 202  
... ... @@ -195,8 +209,8 @@ class cshessobs(ctools.cscript):
195 209 if not os.path.isdir(self.directory):
196 210 raise gammalib.Exception.runtime_error("Folder structure not found. Please check your HESSFITS environment variable")
197 211  
198   - self.m_log = False # Logging in client tools
199   - self.m_debug = False # Debugging in client tools
  212 + self.m_log = False # Logging in client tools
  213 + self.m_debug = False # Debugging in client tools
200 214 self.m_clobber = self["clobber"].boolean()
201 215  
202 216 # Return
... ... @@ -259,7 +273,7 @@ class cshessobs(ctools.cscript):
259 273 elif self.nodes == 2:
260 274  
261 275 # User a power law scaling function
262   - e= gammalib.GEnergy(1.0,"TeV")
  276 + e = gammalib.GEnergy(1.0,"TeV")
263 277 spec = gammalib.GModelSpectralPlaw(1.0,0.0,e)
264 278 spec[0].free()
265 279 spec[0].min(0.01)
... ... @@ -395,10 +409,10 @@ class cshessobs(ctools.cscript):
395 409  
396 410 # construct filenames
397 411 eventfile = runfolder + "hess_events_"+run+".fits.gz"
398   - aefffile = runfolder + "hess_aeff_2d_"+run+".fits.gz"
399   - psffile = runfolder + "hess_psf_king_"+run+".fits.gz"
  412 + aefffile = runfolder + "hess_aeff_2d_"+run+".fits.gz"
  413 + psffile = runfolder + "hess_psf_king_"+run+".fits.gz"
400 414 edispfile = ""#runfolder + "hess_edisp_2d_"+run+".fits.gz"
401   - bgfile = runfolder + "hess_bkg_offruns_"+run+".fits.gz"
  415 + bgfile = runfolder + "hess_bkg_offruns_"+run+".fits.gz"
402 416  
403 417 # Check for existence of files
404 418 msg = ""
... ... @@ -497,7 +511,8 @@ class cshessobs(ctools.cscript):
497 511  
498 512 # Save model XML file
499 513 self.models.save(self.outmodel)
500   -
  514 +
  515 +
501 516 # ======================== #
502 517 # Main routine entry point #
503 518 # ======================== #
... ...