Commit afaa6670033310d3c26e2e6d45f499b27b1c23c8
1 parent
866897a5
Add cscaldb script.
The cscaldb script extracts all available CTA calibrations from the database.
Showing
2 changed files
with
211 additions
and
2 deletions
scripts/Makefile.am
| ... | ... | @@ -4,12 +4,12 @@ |
| 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 \ | |
| 7 | +pythonscripts = cspull.py cssens.py cstsdist.py cscaldb.py \ | |
| 8 | 8 | obsutils.py \ |
| 9 | 9 | lsmodel.py |
| 10 | 10 | |
| 11 | 11 | # Scripts for which symbolic links will be created in bindir |
| 12 | -linkscripts = cspull cssens cstsdist \ | |
| 12 | +linkscripts = cspull cssens cstsdist cscaldb \ | |
| 13 | 13 | lsmodel |
| 14 | 14 | |
| 15 | 15 | # Distribute scripts | ... | ... |
scripts/cscaldb.py
0 → 100755
| 1 | +#! /usr/bin/env python | |
| 2 | +# ========================================================================== | |
| 3 | +# This script dumps all available calibrations into the console. | |
| 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 | +import glob | |
| 24 | +import os | |
| 25 | + | |
| 26 | + | |
| 27 | +# ============ # | |
| 28 | +# cspull class # | |
| 29 | +# ============ # | |
| 30 | +class cscaldb(gammalib.GApplication): | |
| 31 | + """ | |
| 32 | + This class implements the pull distribution generation script. It derives | |
| 33 | + from the GammaLib::GApplication class which provides support for parameter | |
| 34 | + files, command line arguments, and logging. In that way the Python | |
| 35 | + script behaves just as a regular ctool. | |
| 36 | + """ | |
| 37 | + def __init__(self, *argv): | |
| 38 | + """ | |
| 39 | + Constructor. | |
| 40 | + """ | |
| 41 | + # Set name | |
| 42 | + self.name = "cscaldb" | |
| 43 | + self.version = "0.1.0" | |
| 44 | + | |
| 45 | + # Make sure that parfile exists | |
| 46 | + file = self.parfile() | |
| 47 | + | |
| 48 | + # Initialise application | |
| 49 | + if len(argv) == 0: | |
| 50 | + gammalib.GApplication.__init__(self, self.name, self.version) | |
| 51 | + elif len(argv) ==1: | |
| 52 | + gammalib.GApplication.__init__(self, self.name, self.version, *argv) | |
| 53 | + else: | |
| 54 | + raise TypeError("Invalid number of arguments given.") | |
| 55 | + | |
| 56 | + # Set logger properties | |
| 57 | + self.log_header() | |
| 58 | + self.log.date(True) | |
| 59 | + | |
| 60 | + # Return | |
| 61 | + return | |
| 62 | + | |
| 63 | + def __del__(self): | |
| 64 | + """ | |
| 65 | + Destructor. | |
| 66 | + """ | |
| 67 | + # Return | |
| 68 | + return | |
| 69 | + | |
| 70 | + def parfile(self): | |
| 71 | + """ | |
| 72 | + Check if parfile exists. If parfile does not exist then create a | |
| 73 | + default parfile. This kluge avoids shipping the cscript with a parfile. | |
| 74 | + """ | |
| 75 | + # Set parfile name | |
| 76 | + parfile = self.name+".par" | |
| 77 | + | |
| 78 | + try: | |
| 79 | + pars = gammalib.GApplicationPars(parfile) | |
| 80 | + except: | |
| 81 | + # Signal if parfile was not found | |
| 82 | + sys.stdout.write("Parfile "+parfile+" not found. Create default parfile.\n") | |
| 83 | + | |
| 84 | + # Create default parfile | |
| 85 | + pars = gammalib.GApplicationPars() | |
| 86 | + pars.append_standard() | |
| 87 | + pars.save(parfile) | |
| 88 | + | |
| 89 | + # Return | |
| 90 | + return | |
| 91 | + | |
| 92 | + def get_parameters(self): | |
| 93 | + """ | |
| 94 | + Get parameters from parfile. | |
| 95 | + """ | |
| 96 | + # Return | |
| 97 | + return | |
| 98 | + | |
| 99 | + def execute(self): | |
| 100 | + """ | |
| 101 | + Execute the script. | |
| 102 | + """ | |
| 103 | + # Run the script | |
| 104 | + self.run() | |
| 105 | + | |
| 106 | + # Return | |
| 107 | + return | |
| 108 | + | |
| 109 | + def run(self): | |
| 110 | + """ | |
| 111 | + Run the script. | |
| 112 | + """ | |
| 113 | + # Switch screen logging on in debug mode | |
| 114 | + if self.logDebug(): | |
| 115 | + self.log.cout(True) | |
| 116 | + | |
| 117 | + # Get parameters | |
| 118 | + self.get_parameters() | |
| 119 | + | |
| 120 | + # Write input parameters into logger | |
| 121 | + if self.logTerse(): | |
| 122 | + self.log_parameters() | |
| 123 | + self.log("\n") | |
| 124 | + | |
| 125 | + # Get the calibration database path | |
| 126 | + caldb = gammalib.GCaldb() | |
| 127 | + | |
| 128 | + # Gather missions | |
| 129 | + missions = [] | |
| 130 | + paths = glob.glob(caldb.rootdir()+"/data/*") | |
| 131 | + paths.sort() | |
| 132 | + for path in paths: | |
| 133 | + head, tail = os.path.split(path) | |
| 134 | + missions.append(tail) | |
| 135 | + | |
| 136 | + # Loop over missions | |
| 137 | + for mission in missions: | |
| 138 | + | |
| 139 | + # Skip all non-CTA instruments | |
| 140 | + if mission != "cta": | |
| 141 | + continue | |
| 142 | + | |
| 143 | + # Write mission into logger | |
| 144 | + if self.logTerse(): | |
| 145 | + self.log("\n") | |
| 146 | + self.log.header1("Mission: "+mission) | |
| 147 | + | |
| 148 | + # Gather instruments | |
| 149 | + instruments = [] | |
| 150 | + paths = glob.glob(caldb.rootdir()+"/data/"+mission+"/*") | |
| 151 | + paths.sort() | |
| 152 | + for path in paths: | |
| 153 | + head, tail = os.path.split(path) | |
| 154 | + instruments.append(tail) | |
| 155 | + | |
| 156 | + # Loop over instruments | |
| 157 | + for instrument in instruments: | |
| 158 | + | |
| 159 | + # Write mission into logger | |
| 160 | + if self.logTerse(): | |
| 161 | + self.log.header3("Calibration: "+instrument) | |
| 162 | + | |
| 163 | + # Open calibration index file | |
| 164 | + cifname = caldb.rootdir()+"/data/"+mission+"/"+instrument+"/caldb.indx" | |
| 165 | + fits = gammalib.GFits(cifname) | |
| 166 | + cif = fits["CIF"] | |
| 167 | + cals = cif["CAL_CBD"] | |
| 168 | + | |
| 169 | + # Extract response names | |
| 170 | + names = [] | |
| 171 | + nrows = cals.length() | |
| 172 | + ncols = cals.number() | |
| 173 | + for row in range(nrows): | |
| 174 | + for col in range(ncols): | |
| 175 | + cal = cals.string(row, col) | |
| 176 | + istart = cal.find("NAME(") | |
| 177 | + if istart != -1: | |
| 178 | + istop = cal.find(")") | |
| 179 | + name = cal[5:istop] | |
| 180 | + if names.count(name) == 0: | |
| 181 | + names.append(name) | |
| 182 | + names.sort() | |
| 183 | + | |
| 184 | + # Print response name | |
| 185 | + if self.logTerse(): | |
| 186 | + for name in names: | |
| 187 | + self.log(name+"\n") | |
| 188 | + self.log("\n") | |
| 189 | + | |
| 190 | + # Return | |
| 191 | + return | |
| 192 | + | |
| 193 | + | |
| 194 | +# ======================== # | |
| 195 | +# Main routine entry point # | |
| 196 | +# ======================== # | |
| 197 | +if __name__ == '__main__': | |
| 198 | + """ | |
| 199 | + Dumps calibration database into log file. | |
| 200 | + """ | |
| 201 | + # Create instance of application | |
| 202 | + app = cscaldb(sys.argv) | |
| 203 | + | |
| 204 | + # Open logfile | |
| 205 | + app.logFileOpen() | |
| 206 | + | |
| 207 | + # Execute application | |
| 208 | + app.execute() | |
| 209 | + | |
| 0 | 210 | \ No newline at end of file | ... | ... |