Commit 809a67bde8769453016d5c3802b914ad16acb5a1

Authored by Jürgen Knödlseder
1 parent 6798916a

Add lsmodel script.

The lsmodel script generates a model cube for Fermi/LAT data.
scripts/Makefile.am
... ... @@ -4,10 +4,13 @@
4 4 binscripts = cta_kb_bkgrates.py cta_root2irf.py cta_make_nodes.py
5 5  
6 6 # Scripts to be distributed in pythondir
7   -pythonscripts = cspull.py cssens.py cstsdist.py obsutils.py
  7 +pythonscripts = cspull.py cssens.py cstsdist.py \
  8 + obsutils.py \
  9 + lsmodel.py
8 10  
9 11 # Scripts for which symbolic links will be created in bindir
10   -linkscripts = cspull cssens cstsdist
  12 +linkscripts = cspull cssens cstsdist \
  13 + lsmodel
11 14  
12 15 # Distribute scripts
13 16 EXTRA_DIST = $(binscripts) $(pythonscripts)
... ...
scripts/lsmodel.py 0 → 100755
  1 +#! /usr/bin/env python
  2 +# ==========================================================================
  3 +# This script generates a model map from a counts map and a source model.
  4 +#
  5 +# Copyright (C) 2014 Juergen Knoedlseder
  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 +# ==========================================================================
  21 +import gammalib
  22 +import sys
  23 +
  24 +
  25 +# ============= #
  26 +# lsmodel class #
  27 +# ============= #
  28 +class lsmodel(gammalib.GApplication):
  29 + """
  30 + This class implements the model map generation script. It derives from
  31 + the GammaLib::GApplication class which provides support for parameter
  32 + files, command line arguments, and logging. In that way the Python
  33 + script behaves just as a regular ctool.
  34 + """
  35 + def __init__(self, *argv):
  36 + """
  37 + Constructor.
  38 + """
  39 + # Set name
  40 + self.name = "lsmodel"
  41 + self.version = "0.1.0"
  42 +
  43 + # Make sure that parfile exists
  44 + file = self.parfile()
  45 +
  46 + # Initialise application
  47 + if len(argv) == 0:
  48 + gammalib.GApplication.__init__(self, self.name, self.version)
  49 + elif len(argv) ==1:
  50 + gammalib.GApplication.__init__(self, self.name, self.version, *argv)
  51 + else:
  52 + raise TypeError("Invalid number of arguments given.")
  53 +
  54 + # Set logger properties
  55 + self.log_header()
  56 + self.log.date(True)
  57 +
  58 + # Return
  59 + return
  60 +
  61 + def __del__(self):
  62 + """
  63 + Destructor.
  64 + """
  65 + # Write separator into logger
  66 + if self.logTerse():
  67 + self.log("\n")
  68 +
  69 + # Return
  70 + return
  71 +
  72 + def parfile(self):
  73 + """
  74 + Check if parfile exists. If parfile does not exist then create a
  75 + default parfile. This kluge avoids shipping the script with a parfile.
  76 + """
  77 + # Set parfile name
  78 + parfile = self.name+".par"
  79 +
  80 + try:
  81 + pars = gammalib.GApplicationPars(parfile)
  82 + except:
  83 + # Signal if parfile was not found
  84 + sys.stdout.write("Parfile "+parfile+" not found. Create default parfile.\n")
  85 +
  86 + # Create default parfile
  87 + pars = gammalib.GApplicationPars()
  88 + pars.append(gammalib.GApplicationPar("cntmap","f","a","cntmap.fits","","","Counts map"))
  89 + pars.append(gammalib.GApplicationPar("expmap","f","a","binned_expmap.fits","","","Exposure map"))
  90 + pars.append(gammalib.GApplicationPar("ltcube","f","a","ltcube.fits","","","Livetime cube"))
  91 + pars.append(gammalib.GApplicationPar("srcmdl","f","a","crab_model.xml","","","Source model"))
  92 + pars.append(gammalib.GApplicationPar("caldb","s","h","","","","Calibration database"))
  93 + pars.append(gammalib.GApplicationPar("irf","s","a","P7REP_SOURCE_V15","","","Instrument response function"))
  94 + pars.append(gammalib.GApplicationPar("outfile","f","a","model_cube.fits","","","Output file name"))
  95 + pars.append_standard()
  96 + pars.save(parfile)
  97 +
  98 + # Return
  99 + return
  100 +
  101 + def get_parameters(self):
  102 + """
  103 + Get parameters from parfile and setup the observation.
  104 + """
  105 + # Get parameters
  106 + self.m_cntmap = self["cntmap"].filename()
  107 + self.m_expmap = self["expmap"].filename()
  108 + self.m_ltcube = self["ltcube"].filename()
  109 + self.m_srcmdl = self["srcmdl"].filename()
  110 + self.m_caldb = self["caldb"].string()
  111 + self.m_irf = self["irf"].string()
  112 + self.m_outfile = self["outfile"].filename()
  113 +
  114 + # Set some fixed parameters
  115 + self.m_log = False # Logging in client tools
  116 + self.m_debug = False # Debugging in client tools
  117 +
  118 + # Return
  119 + return
  120 +
  121 + def execute(self):
  122 + """
  123 + Execute the script.
  124 + """
  125 + # Run the script
  126 + self.run()
  127 +
  128 + # Return
  129 + return
  130 +
  131 + def run(self):
  132 + """
  133 + Run the script.
  134 + """
  135 + # Switch screen logging on in debug mode
  136 + if self.logDebug():
  137 + self.log.cout(True)
  138 +
  139 + # Get parameters
  140 + self.get_parameters()
  141 +
  142 + # Write input parameters into logger
  143 + if self.logTerse():
  144 + self.log_parameters()
  145 + self.log("\n")
  146 +
  147 + # Write header
  148 + if self.logTerse():
  149 + self.log("\n")
  150 + self.log.header1("Generate model cube")
  151 +
  152 + # Set LAT observation
  153 + obs = gammalib.GLATObservation()
  154 + obs.load_binned(self.m_cntmap, self.m_expmap, self.m_ltcube)
  155 + obs.response(self.m_irf, self.m_caldb)
  156 +
  157 + # Set model (use first model for the moment)
  158 + models = gammalib.GModels(self.m_srcmdl)
  159 + model = models[0]
  160 +
  161 + # Get deep copy of GLATEventCube from observation
  162 + cube = gammalib.GLATEventCube(obs.events())
  163 +
  164 + # Fill cube with model values
  165 + for i in range(cube.size()):
  166 + event = cube[i]
  167 + value = model.eval(event, obs)
  168 + event.counts(value)
  169 + cube[i] = event
  170 +
  171 + # Log cube
  172 + if self.logTerse():
  173 + self.log(str(cube))
  174 + self.log("\n")
  175 +
  176 + # Write header
  177 + if self.logTerse():
  178 + self.log("\n")
  179 + self.log.header1("Save model cube")
  180 +
  181 + # Save model cube
  182 + cube.save(self.m_outfile, self.clobber())
  183 +
  184 + # Return
  185 + return
  186 +
  187 +
  188 +# ======================== #
  189 +# Main routine entry point #
  190 +# ======================== #
  191 +if __name__ == '__main__':
  192 + """
  193 + Generates a model map from a counts map and a source model.
  194 + """
  195 + # Create instance of application
  196 + app = lsmodel(sys.argv)
  197 +
  198 + # Open logfile
  199 + app.logFileOpen()
  200 +
  201 + # Execute application
  202 + app.execute()
  203 +
0 204 \ No newline at end of file
... ...