Commit ea4cc4d4eb5c23f4e779de55ff5dddffaedbad3b
1 parent
db932f08
Add par file for lsmodel script.
Showing
2 changed files
with
132 additions
and
102 deletions
cscripts/lsmodel.par
0 → 100644
| 1 | +############################################################################# | ||
| 2 | +# # | ||
| 3 | +# Fermi/LAT Science Tools # | ||
| 4 | +# # | ||
| 5 | +# ------------------------------------------------------------------------- # | ||
| 6 | +# # | ||
| 7 | +# File: lsmodel.par # | ||
| 8 | +# Version: 1.1.0 # | ||
| 9 | +# # | ||
| 10 | +# Author: Juergen Knoedlseder # | ||
| 11 | +# Jurgen.Knodlseder@irap.omp.eu # | ||
| 12 | +# IRAP # | ||
| 13 | +# # | ||
| 14 | +# Purpose: Parameter file for the lsmodel script # | ||
| 15 | +# # | ||
| 16 | +############################################################################# | ||
| 17 | +# | ||
| 18 | +# Input and output parameters | ||
| 19 | +#============================ | ||
| 20 | +cntmap, f, a, cntmap.fits,,, "Counts map" | ||
| 21 | +expmap, f, a, binned_expmap.fits,,, "Exposure map" | ||
| 22 | +ltcube, f, a, ltcube.fits,,, "Livetime cube" | ||
| 23 | +srcmdl, f, a, crab_model.xml,,, "Source model" | ||
| 24 | +caldb, s, h, ,,, "Calibration database" | ||
| 25 | +irf, s, a, P8R2_SOURCE_V6,,, "Instrument response function" | ||
| 26 | +outfile, f, a, model_cube.fits,,, "Output file name" | ||
| 27 | + | ||
| 28 | +# | ||
| 29 | +# Standard parameters | ||
| 30 | +#==================== | ||
| 31 | +chatter, i, h, 2,0,4, "Chattiness of output" | ||
| 32 | +clobber, b, h, yes,,, "Overwrite existing output files with new output files?" | ||
| 33 | +debug, b, h, no,,, "Debugging mode activated" | ||
| 34 | +mode, s, h, ql,,, "Mode of automatic parameters" | ||
| 35 | +logfile, f, h, lsmodel.log,,, "Log filename" | ||
| 36 | + | ||
| 37 | + | ||
| 38 | + |
cscripts/lsmodel.py
| 1 | #! /usr/bin/env python | 1 | #! /usr/bin/env python |
| 2 | # ========================================================================== | 2 | # ========================================================================== |
| 3 | -# This script generates a model map from a counts map and a source model. | 3 | +# Generates model cube for Fermi/LAT data. |
| 4 | # | 4 | # |
| 5 | -# Copyright (C) 2014 Juergen Knoedlseder | 5 | +# Copyright (C) 2014-2016 Juergen Knoedlseder |
| 6 | # | 6 | # |
| 7 | # This program is free software: you can redistribute it and/or modify | 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 | 8 | # it under the terms of the GNU General Public License as published by |
| @@ -27,135 +27,97 @@ import sys | @@ -27,135 +27,97 @@ import sys | ||
| 27 | # ============= # | 27 | # ============= # |
| 28 | class lsmodel(gammalib.GApplication): | 28 | class lsmodel(gammalib.GApplication): |
| 29 | """ | 29 | """ |
| 30 | + Generates model cube for Fermi/LAT data. | ||
| 31 | + | ||
| 30 | This class implements the model map generation script. It derives from | 32 | This class implements the model map generation script. It derives from |
| 31 | the GammaLib::GApplication class which provides support for parameter | 33 | the GammaLib::GApplication class which provides support for parameter |
| 32 | files, command line arguments, and logging. In that way the Python | 34 | files, command line arguments, and logging. In that way the Python |
| 33 | script behaves just as a regular ctool. | 35 | script behaves just as a regular ctool. |
| 34 | """ | 36 | """ |
| 37 | + | ||
| 38 | + # Constructor | ||
| 35 | def __init__(self, *argv): | 39 | def __init__(self, *argv): |
| 36 | """ | 40 | """ |
| 37 | Constructor. | 41 | Constructor. |
| 38 | """ | 42 | """ |
| 39 | - # Set name | ||
| 40 | - self.name = "lsmodel" | ||
| 41 | - self.version = "0.1.0" | ||
| 42 | - | ||
| 43 | - # Make sure that parfile exists | ||
| 44 | - file = self.parfile() | 43 | + # Set name and version |
| 44 | + self._name = "lsmodel" | ||
| 45 | + self._version = "1.1.0" | ||
| 45 | 46 | ||
| 46 | # Initialise application | 47 | # Initialise application |
| 47 | if len(argv) == 0: | 48 | if len(argv) == 0: |
| 48 | - gammalib.GApplication.__init__(self, self.name, self.version) | 49 | + gammalib.GApplication.__init__(self, self._name, self._version) |
| 49 | elif len(argv) ==1: | 50 | elif len(argv) ==1: |
| 50 | - gammalib.GApplication.__init__(self, self.name, self.version, *argv) | 51 | + gammalib.GApplication.__init__(self, self._name, self._version, *argv) |
| 51 | else: | 52 | else: |
| 52 | raise TypeError("Invalid number of arguments given.") | 53 | raise TypeError("Invalid number of arguments given.") |
| 53 | 54 | ||
| 54 | # Set logger properties | 55 | # Set logger properties |
| 55 | - self.log_header() | ||
| 56 | - self.log.date(True) | 56 | + self._log_header() |
| 57 | + self._log.date(True) | ||
| 57 | 58 | ||
| 58 | # Return | 59 | # Return |
| 59 | return | 60 | return |
| 60 | 61 | ||
| 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 | 62 | ||
| 72 | - def parfile(self): | 63 | + # Private methods |
| 64 | + def _get_parameters(self): | ||
| 73 | """ | 65 | """ |
| 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. | 66 | + Get parameters from parfile. |
| 76 | """ | 67 | """ |
| 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 | 68 | + # Query user parameters |
| 69 | + self["cntmap"].filename() | ||
| 70 | + self["expmap"].filename() | ||
| 71 | + self["ltcube"].filename() | ||
| 72 | + self["srcmdl"].filename() | ||
| 73 | + self["caldb"].string() | ||
| 74 | + self["irf"].string() | ||
| 75 | + | ||
| 76 | + # Read ahead output parameters | ||
| 77 | + if (self._read_ahead()): | ||
| 78 | + self["outfile"].filename() | ||
| 100 | 79 | ||
| 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 | 80 | + # Write input parameters into logger |
| 81 | + if self._logTerse(): | ||
| 82 | + self._log_parameters() | ||
| 83 | + self._log("\n") | ||
| 117 | 84 | ||
| 118 | # Return | 85 | # Return |
| 119 | return | 86 | return |
| 120 | 87 | ||
| 121 | - def execute(self): | ||
| 122 | - """ | ||
| 123 | - Execute the script. | ||
| 124 | - """ | ||
| 125 | - # Run the script | ||
| 126 | - self.run() | ||
| 127 | - | ||
| 128 | - # Return | ||
| 129 | - return | ||
| 130 | 88 | ||
| 89 | + # Public methods | ||
| 131 | def run(self): | 90 | def run(self): |
| 132 | """ | 91 | """ |
| 133 | Run the script. | 92 | Run the script. |
| 134 | """ | 93 | """ |
| 135 | # Switch screen logging on in debug mode | 94 | # Switch screen logging on in debug mode |
| 136 | - if self.logDebug(): | ||
| 137 | - self.log.cout(True) | 95 | + if self._logDebug(): |
| 96 | + self._log.cout(True) | ||
| 138 | 97 | ||
| 139 | # Get parameters | 98 | # 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") | 99 | + self._get_parameters() |
| 146 | 100 | ||
| 147 | # Write header | 101 | # Write header |
| 148 | - if self.logTerse(): | ||
| 149 | - self.log("\n") | ||
| 150 | - self.log.header1("Generate model cube") | 102 | + if self._logTerse(): |
| 103 | + self._log("\n") | ||
| 104 | + self._log.header1("Generate model cube") | ||
| 105 | + | ||
| 106 | + # Get user parameters | ||
| 107 | + cntmap = self["cntmap"].filename() | ||
| 108 | + expmap = self["expmap"].filename() | ||
| 109 | + ltcube = self["ltcube"].filename() | ||
| 110 | + srcmdl = self["srcmdl"].filename() | ||
| 111 | + caldb = self["caldb"].string() | ||
| 112 | + irf = self["irf"].string() | ||
| 151 | 113 | ||
| 152 | # Set LAT observation | 114 | # Set LAT observation |
| 153 | obs = gammalib.GLATObservation() | 115 | 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) | 116 | + obs.load_binned(cntmap, expmap, ltcube) |
| 117 | + obs.response(irf, caldb) | ||
| 156 | 118 | ||
| 157 | # Set model (use first model for the moment) | 119 | # Set model (use first model for the moment) |
| 158 | - models = gammalib.GModels(self.m_srcmdl) | 120 | + models = gammalib.GModels(srcmdl) |
| 159 | model = models[0] | 121 | model = models[0] |
| 160 | 122 | ||
| 161 | # Get deep copy of GLATEventCube from observation | 123 | # Get deep copy of GLATEventCube from observation |
| @@ -169,17 +131,52 @@ class lsmodel(gammalib.GApplication): | @@ -169,17 +131,52 @@ class lsmodel(gammalib.GApplication): | ||
| 169 | cube[i] = event | 131 | cube[i] = event |
| 170 | 132 | ||
| 171 | # Log cube | 133 | # Log cube |
| 172 | - if self.logTerse(): | ||
| 173 | - self.log(str(cube)) | ||
| 174 | - self.log("\n") | 134 | + if self._logTerse(): |
| 135 | + self._log(str(cube)) | ||
| 136 | + self._log("\n") | ||
| 175 | 137 | ||
| 176 | - # Write header | ||
| 177 | - if self.logTerse(): | ||
| 178 | - self.log("\n") | ||
| 179 | - self.log.header1("Save model cube") | 138 | + # Return |
| 139 | + return | ||
| 140 | + | ||
| 141 | + def execute(self): | ||
| 142 | + """ | ||
| 143 | + Execute the script. | ||
| 144 | + """ | ||
| 145 | + # Open logfile | ||
| 146 | + self._logFileOpen() | ||
| 147 | + | ||
| 148 | + # Read ahead output parameters | ||
| 149 | + self._read_ahead(True) | ||
| 150 | + | ||
| 151 | + # Run the script | ||
| 152 | + self.run() | ||
| 153 | + | ||
| 154 | + # Save event cube | ||
| 155 | + self.save() | ||
| 156 | + | ||
| 157 | + # Return | ||
| 158 | + return | ||
| 180 | 159 | ||
| 181 | - # Save model cube | ||
| 182 | - cube.save(self.m_outfile, self.clobber()) | 160 | + def save(self): |
| 161 | + """ | ||
| 162 | + Save model cube. | ||
| 163 | + """ | ||
| 164 | + # Write header | ||
| 165 | + if self._logTerse(): | ||
| 166 | + self._log("\n") | ||
| 167 | + self._log.header1("Save model cube") | ||
| 168 | + | ||
| 169 | + # Get outfile parameter | ||
| 170 | + outfile = self["outfile"].filename() | ||
| 171 | + | ||
| 172 | + # Log file name | ||
| 173 | + if self._logTerse(): | ||
| 174 | + self._log(gammalib.parformat("Model cube")) | ||
| 175 | + self._log(outfile.url()) | ||
| 176 | + self._log("\n") | ||
| 177 | + | ||
| 178 | + # Save event cube | ||
| 179 | + self._cube.save(outfile, self._clobber()) | ||
| 183 | 180 | ||
| 184 | # Return | 181 | # Return |
| 185 | return | 182 | return |
| @@ -189,14 +186,9 @@ class lsmodel(gammalib.GApplication): | @@ -189,14 +186,9 @@ class lsmodel(gammalib.GApplication): | ||
| 189 | # Main routine entry point # | 186 | # Main routine entry point # |
| 190 | # ======================== # | 187 | # ======================== # |
| 191 | if __name__ == '__main__': | 188 | if __name__ == '__main__': |
| 192 | - """ | ||
| 193 | - Generates a model map from a counts map and a source model. | ||
| 194 | - """ | 189 | + |
| 195 | # Create instance of application | 190 | # Create instance of application |
| 196 | app = lsmodel(sys.argv) | 191 | app = lsmodel(sys.argv) |
| 197 | 192 | ||
| 198 | - # Open logfile | ||
| 199 | - app.logFileOpen() | ||
| 200 | - | ||
| 201 | # Execute application | 193 | # Execute application |
| 202 | app.execute() | 194 | app.execute() |