ctools  2.0.0
 All Classes Namespaces Files Functions Variables Macros Pages
cscaldb.py
Go to the documentation of this file.
1 #! /usr/bin/env python
2 # ==========================================================================
3 # Show the content of the ctools calibration database
4 #
5 # Copyright (C) 2014-2022 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 sys
22 import glob
23 import os
24 import gammalib
25 import ctools
26 
27 
28 # ============= #
29 # cscaldb class #
30 # ============= #
31 class cscaldb(ctools.cscript):
32  """
33  Shows the content of the ctools calibration database.
34 
35  The ``cscaldb`` script writes the content of the calibration database
36  into the ``cscaldb.log`` log file. If the ``debug`` parameter is set
37  to ``yes`` the calibration database is also logged in the console.
38  """
39 
40  # Constructor
41  def __init__(self, *argv):
42  """
43  Constructor.
44 
45  Parameters
46  ----------
47  argv : list of str
48  List of IRAF command line parameter strings of the form
49  ``parameter=3``.
50 
51  Raises
52  ------
53  TypeError
54  An invalid number of command line arguments was provided.
55  """
56  # Initialise application by calling the base class constructor
57  self._init_cscript(self.__class__.__name__, ctools.__version__, argv)
58 
59  # Return
60  return
61 
62 
63  # Private methods
64  #
65  # Query all user parameters
66  def _get_parameters(self):
67 
68  # Write input parameters into logger
69  self._log_parameters(gammalib.TERSE)
70 
71  # Return
72  return
73 
74  # Extract mission names from a calibration database
75  def _get_missions(self, caldb):
76 
77  # Initialise mission list
78  missions = []
79 
80  # Extract missions
81  paths = glob.glob(caldb.rootdir()+'/data/*')
82  paths.sort()
83  for path in paths:
84  missions.append(os.path.basename(path))
85 
86  # Sort missions
87  missions.sort()
88 
89  # Return missions
90  return missions
91 
92  # Extract instrument names from a calibration database
93  def _get_instruments(self, caldb, mission):
94 
95  # Initialise instrument list
96  instruments = []
97 
98  # Extract instruments
99  paths = glob.glob(caldb.rootdir()+'/data/'+mission+'/*')
100  paths.sort()
101  for path in paths:
102  instruments.append(os.path.basename(path))
103 
104  # Sort instruments
105  instruments.sort()
106 
107  # Return instruments
108  return instruments
109 
110  # Extract response names from a calibrations FITS table and return them
111  # in form of a list
112  def _get_response_names(self, calibrations):
113 
114  # Initialise response name list
115  names = []
116 
117  # Extract response names from calibrations and append them to the
118  # response name list
119  nrows = calibrations.nrows()
120  ncols = calibrations.number()
121  for row in range(nrows):
122  for col in range(ncols):
123  cal = calibrations.string(row, col)
124  istart = cal.find('NAME(')
125  if istart != -1:
126  istop = cal.find(')')
127  name = cal[5:istop]
128  if names.count(name) == 0:
129  names.append(name)
130 
131  # Sort response names
132  names.sort()
133 
134  # Return response names
135  return names
136 
137 
138  # Public methods
139  def process(self):
140  """
141  Process the script.
142  """
143  # Get parameters
144  self._get_parameters()
145 
146  # Get the calibration database
147  caldb = gammalib.GCaldb()
148 
149  # Extract mission names from the calibration database
150  missions = self._get_missions(caldb)
151 
152  # Loop over missions
153  for mission in missions:
154 
155  # Skip all non-CTA instruments
156  if mission != 'cta':
157  continue
158 
159  # Write mission into logger
160  self._log_header1(gammalib.TERSE, 'Mission: '+mission)
161 
162  # Extract instruments
163  instruments = self._get_instruments(caldb, mission)
164 
165  # Loop over instruments
166  for instrument in instruments:
167 
168  # Write instrument into logger
169  self._log_header3(gammalib.TERSE, 'Response functions in database "'+
170  instrument+'"')
171 
172  # Open calibration index file and retrieve calibrations
173  filename = '/data/'+mission+'/'+instrument+'/caldb.indx'
174  cifname = caldb.rootdir() + filename
175  fits = gammalib.GFits(cifname)
176  cif = fits['CIF']
177  caltable = cif['CAL_CBD']
178 
179  # Extract response names
180  names = self._get_response_names(caltable)
181 
182  # Print response name
183  if self._logTerse():
184  for name in names:
185  self._log(name+'\n')
186  self._log('\n')
187 
188  # Return
189  return
190 
191 
192 # ======================== #
193 # Main routine entry point #
194 # ======================== #
195 if __name__ == '__main__':
196 
197  # Create instance of application
198  app = cscaldb(sys.argv)
199 
200  # Execute application
201  app.execute()