Commit a8093c0a7ee7957cf16f72b0a868252ce1249cab

Authored by Jürgen Knödlseder
1 parent c8385b97

Adapt cscaldb to Python coding conventions.

Showing 1 changed file with 55 additions and 73 deletions
cscripts/cscaldb.py
1 1 #! /usr/bin/env python
2 2 # ==========================================================================
3   -# This script dumps all available calibrations into the console.
  3 +# Show the content of the ctools calibration database
4 4 #
5 5 # Copyright (C) 2014-2016 Juergen Knoedlseder
6 6 #
... ... @@ -32,19 +32,30 @@ class cscaldb(ctools.cscript):
32 32 """
33 33 Shows the content of the ctools calibration database.
34 34  
35   - If the "debug" parameter is set to "yes" the calibration database
36   - is shown in the console, otherwise the content is only written
37   - into the "cscaldb.log" log file.
  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 38 """
39 39  
40 40 # Constructors and destructors
41 41 def __init__(self, *argv):
42 42 """
43 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.
44 55 """
45 56 # Set name
46   - self._name = "cscaldb"
47   - self._version = "1.1.0"
  57 + self._name = 'cscaldb'
  58 + self._version = '1.1.0'
48 59  
49 60 # Initialise application
50 61 if len(argv) == 0:
... ... @@ -52,7 +63,7 @@ class cscaldb(ctools.cscript):
52 63 elif len(argv) ==1:
53 64 ctools.cscript.__init__(self, self._name, self._version, *argv)
54 65 else:
55   - raise TypeError("Invalid number of arguments given.")
  66 + raise TypeError('Invalid number of arguments provided.')
56 67  
57 68 # Set logger properties
58 69 self._log_header()
... ... @@ -61,37 +72,28 @@ class cscaldb(ctools.cscript):
61 72 # Return
62 73 return
63 74  
64   - def __del__(self):
65   - """
66   - Destructor.
67   - """
68   - # Return
69   - return
70   -
71 75  
72 76 # Private methods
  77 + #
  78 + # Query all user parameters
73 79 def _get_parameters(self):
74   - """
75   - Get parameters from parfile.
76   - """
  80 +
  81 + # Write input parameters into logger
  82 + if self._logTerse():
  83 + self._log_parameters()
  84 + self._log('\n')
  85 +
77 86 # Return
78 87 return
79 88  
  89 + # Extract mission names from a calibration database
80 90 def _get_missions(self, caldb):
81   - """
82   - Extract mission names from a calibration database.
83 91  
84   - Args:
85   - caldb: Calibration database.
86   -
87   - Returns:
88   - A list of mission names.
89   - """
90 92 # Initialise mission list
91 93 missions = []
92 94  
93 95 # Extract missions
94   - paths = glob.glob(caldb.rootdir()+"/data/*")
  96 + paths = glob.glob(caldb.rootdir()+'/data/*')
95 97 paths.sort()
96 98 for path in paths:
97 99 missions.append(os.path.basename(path))
... ... @@ -102,22 +104,14 @@ class cscaldb(ctools.cscript):
102 104 # Return missions
103 105 return missions
104 106  
  107 + # Extract instrument names from a calibration database
105 108 def _get_instruments(self, caldb, mission):
106   - """
107   - Extract instrument names from a calibration database.
108 109  
109   - Args:
110   - caldb: Calibration database.
111   - mission: Mission name.
112   -
113   - Returns:
114   - A list of instrument names.
115   - """
116 110 # Initialise instrument list
117 111 instruments = []
118 112  
119 113 # Extract instruments
120   - paths = glob.glob(caldb.rootdir()+"/data/"+mission+"/*")
  114 + paths = glob.glob(caldb.rootdir()+'/data/'+mission+'/*')
121 115 paths.sort()
122 116 for path in paths:
123 117 instruments.append(os.path.basename(path))
... ... @@ -128,17 +122,10 @@ class cscaldb(ctools.cscript):
128 122 # Return instruments
129 123 return instruments
130 124  
  125 + # Extract response names from a calibrations FITS table and return them
  126 + # in form of a list
131 127 def _get_response_names(self, calibrations):
132   - """
133   - Extract response names from a calibrations FITS table and return
134   - them in form of a list.
135 128  
136   - Args:
137   - calibrations: FITS table containing the calibrations.
138   -
139   - Returns:
140   - A list of response names.
141   - """
142 129 # Initialise response name list
143 130 names = []
144 131  
... ... @@ -149,9 +136,9 @@ class cscaldb(ctools.cscript):
149 136 for row in range(nrows):
150 137 for col in range(ncols):
151 138 cal = calibrations.string(row, col)
152   - istart = cal.find("NAME(")
  139 + istart = cal.find('NAME(')
153 140 if istart != -1:
154   - istop = cal.find(")")
  141 + istop = cal.find(')')
155 142 name = cal[5:istop]
156 143 if names.count(name) == 0:
157 144 names.append(name)
... ... @@ -164,16 +151,6 @@ class cscaldb(ctools.cscript):
164 151  
165 152  
166 153 # Public methods
167   - def execute(self):
168   - """
169   - Execute the script.
170   - """
171   - # Run the script
172   - self.run()
173   -
174   - # Return
175   - return
176   -
177 154 def run(self):
178 155 """
179 156 Run the script.
... ... @@ -185,11 +162,6 @@ class cscaldb(ctools.cscript):
185 162 # Get parameters
186 163 self._get_parameters()
187 164  
188   - # Write input parameters into logger
189   - if self._logTerse():
190   - self._log_parameters()
191   - self._log("\n")
192   -
193 165 # Get the calibration database
194 166 caldb = gammalib.GCaldb()
195 167  
... ... @@ -200,13 +172,13 @@ class cscaldb(ctools.cscript):
200 172 for mission in missions:
201 173  
202 174 # Skip all non-CTA instruments
203   - if mission != "cta":
  175 + if mission != 'cta':
204 176 continue
205 177  
206 178 # Write mission into logger
207 179 if self._logTerse():
208   - self._log("\n")
209   - self._log.header1("Mission: "+mission)
  180 + self._log('\n')
  181 + self._log.header1('Mission: '+mission)
210 182  
211 183 # Extract instruments
212 184 instruments = self._get_instruments(caldb, mission)
... ... @@ -216,15 +188,15 @@ class cscaldb(ctools.cscript):
216 188  
217 189 # Write mission into logger
218 190 if self._logTerse():
219   - self._log.header3("Response functions in database \""+
220   - instrument+"\"")
  191 + self._log.header3('Response functions in database "'+
  192 + instrument+'"')
221 193  
222 194 # Open calibration index file and retrieve calibrations
223   - filename = "/data/"+mission+"/"+instrument+"/caldb.indx"
  195 + filename = '/data/'+mission+'/'+instrument+'/caldb.indx'
224 196 cifname = caldb.rootdir() + filename
225 197 fits = gammalib.GFits(cifname)
226   - cif = fits["CIF"]
227   - caltable = cif["CAL_CBD"]
  198 + cif = fits['CIF']
  199 + caltable = cif['CAL_CBD']
228 200  
229 201 # Extract response names
230 202 names = self._get_response_names(caltable)
... ... @@ -232,8 +204,18 @@ class cscaldb(ctools.cscript):
232 204 # Print response name
233 205 if self._logTerse():
234 206 for name in names:
235   - self._log(name+"\n")
236   - self._log("\n")
  207 + self._log(name+'\n')
  208 + self._log('\n')
  209 +
  210 + # Return
  211 + return
  212 +
  213 + def execute(self):
  214 + """
  215 + Execute the script.
  216 + """
  217 + # Run the script
  218 + self.run()
237 219  
238 220 # Return
239 221 return
... ... @@ -248,7 +230,7 @@ if __name__ == '__main__':
248 230 app = cscaldb(sys.argv)
249 231  
250 232 # Open logfile
251   - app._logFileOpen()
  233 + app.logFileOpen()
252 234  
253 235 # Execute application
254 236 app.execute()
... ...