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 #!/usr/bin/env python 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 import gammalib 21 import gammalib
3 import ctools 22 import ctools
4 import sys 23 import sys
5 import os 24 import os
6 import glob 25 import glob
7 26
  27 +
8 # =============== # 28 # =============== #
9 # cshessobs class # 29 # cshessobs class #
10 # =============== # 30 # =============== #
11 class cshessobs(ctools.cscript): 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 Please see the internal documentation for this purpose: 37 Please see the internal documentation for this purpose:
20 https://hess-confluence.desy.de/confluence/display/HESS/HESS+Open+Source+Tools+-+HESS+data+and+IRFs+in+FITS+format 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,18 +44,19 @@ class cshessobs(ctools.cscript):
26 # Set name 44 # Set name
27 self.name = "cshessobs" 45 self.name = "cshessobs"
28 self.version = "0.1.0" 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 try: 49 try:
34 self.datapath = os.environ["HESSFITS"] 50 self.datapath = os.environ["HESSFITS"]
35 except KeyError: 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 # Make sure that parfile exists 61 # Make sure that parfile exists
43 file = self.parfile() 62 file = self.parfile()
@@ -61,10 +80,6 @@ class cshessobs(ctools.cscript): @@ -61,10 +80,6 @@ class cshessobs(ctools.cscript):
61 """ 80 """
62 Destructor. 81 Destructor.
63 """ 82 """
64 - # Write separator into logger  
65 - if self.logTerse():  
66 - self.log("\n")  
67 -  
68 # Return 83 # Return
69 return 84 return
70 85
@@ -144,7 +159,6 @@ class cshessobs(ctools.cscript): @@ -144,7 +159,6 @@ class cshessobs(ctools.cscript):
144 pars.append(gammalib.GApplicationPar(chain+"_"+dstver+"_config","s","a",checked_folders[chain][dstver][0],avconfigs[:-1],"","Analysis configuration")) 159 pars.append(gammalib.GApplicationPar(chain+"_"+dstver+"_config","s","a",checked_folders[chain][dstver][0],avconfigs[:-1],"","Analysis configuration"))
145 pars.append(gammalib.GApplicationPar(chain+"_dstver","s","a",checked_folders[chain].keys()[0],avdsts[:-1],"","DST version")) 160 pars.append(gammalib.GApplicationPar(chain+"_dstver","s","a",checked_folders[chain].keys()[0],avdsts[:-1],"","DST version"))
146 pars.append(gammalib.GApplicationPar("chain","s","a",checked_folders.keys()[0],avchains[:-1],"","Analysis chain")) 161 pars.append(gammalib.GApplicationPar("chain","s","a",checked_folders.keys()[0],avchains[:-1],"","Analysis chain"))
147 -  
148 pars.append(gammalib.GApplicationPar("runlistfile","f","a","runlist.lis","","","Runlist file")) 162 pars.append(gammalib.GApplicationPar("runlistfile","f","a","runlist.lis","","","Runlist file"))
149 pars.append(gammalib.GApplicationPar("inmodel","f","h","NONE","","","Input model XML file (optional)")) 163 pars.append(gammalib.GApplicationPar("inmodel","f","h","NONE","","","Input model XML file (optional)"))
150 pars.append(gammalib.GApplicationPar("outobs","f","a","obs.xml","","","Observation XML outfile")) 164 pars.append(gammalib.GApplicationPar("outobs","f","a","obs.xml","","","Observation XML outfile"))
@@ -177,12 +191,12 @@ class cshessobs(ctools.cscript): @@ -177,12 +191,12 @@ class cshessobs(ctools.cscript):
177 self.outmodel = self["outmodel"].filename() 191 self.outmodel = self["outmodel"].filename()
178 192
179 # Read hidden parameters 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 self.datapath = self["datapath"].string() 196 self.datapath = self["datapath"].string()
183 197
184 # Read run configuration parameters 198 # Read run configuration parameters
185 - self.chain = self["chain"].string() 199 + self.chain = self["chain"].string()
186 self.dstver = self[self.chain+"_dstver"].string() 200 self.dstver = self[self.chain+"_dstver"].string()
187 self.config = self[self.chain+"_"+self.dstver+"_config"].string() 201 self.config = self[self.chain+"_"+self.dstver+"_config"].string()
188 202
@@ -195,8 +209,8 @@ class cshessobs(ctools.cscript): @@ -195,8 +209,8 @@ class cshessobs(ctools.cscript):
195 if not os.path.isdir(self.directory): 209 if not os.path.isdir(self.directory):
196 raise gammalib.Exception.runtime_error("Folder structure not found. Please check your HESSFITS environment variable") 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 self.m_clobber = self["clobber"].boolean() 214 self.m_clobber = self["clobber"].boolean()
201 215
202 # Return 216 # Return
@@ -259,7 +273,7 @@ class cshessobs(ctools.cscript): @@ -259,7 +273,7 @@ class cshessobs(ctools.cscript):
259 elif self.nodes == 2: 273 elif self.nodes == 2:
260 274
261 # User a power law scaling function 275 # User a power law scaling function
262 - e= gammalib.GEnergy(1.0,"TeV") 276 + e = gammalib.GEnergy(1.0,"TeV")
263 spec = gammalib.GModelSpectralPlaw(1.0,0.0,e) 277 spec = gammalib.GModelSpectralPlaw(1.0,0.0,e)
264 spec[0].free() 278 spec[0].free()
265 spec[0].min(0.01) 279 spec[0].min(0.01)
@@ -395,10 +409,10 @@ class cshessobs(ctools.cscript): @@ -395,10 +409,10 @@ class cshessobs(ctools.cscript):
395 409
396 # construct filenames 410 # construct filenames
397 eventfile = runfolder + "hess_events_"+run+".fits.gz" 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 edispfile = ""#runfolder + "hess_edisp_2d_"+run+".fits.gz" 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 # Check for existence of files 417 # Check for existence of files
404 msg = "" 418 msg = ""
@@ -497,7 +511,8 @@ class cshessobs(ctools.cscript): @@ -497,7 +511,8 @@ class cshessobs(ctools.cscript):
497 511
498 # Save model XML file 512 # Save model XML file
499 self.models.save(self.outmodel) 513 self.models.save(self.outmodel)
500 - 514 +
  515 +
501 # ======================== # 516 # ======================== #
502 # Main routine entry point # 517 # Main routine entry point #
503 # ======================== # 518 # ======================== #