Commit 12b82d2a8c3c2c6dd54b76a4da5b51aca98bd1ec

Authored by Jürgen Knödlseder
1 parent adb18c6c

Add csobsdef.pntdef() method to set directly a pointing definition from a gammalib.GCsv object.

ChangeLog
1   -2016-04-17 Juergen Knoedlseder <jurgen.knodlseder@irap.omp.eu>
  1 +2016-04-22 Juergen Knoedlseder <jurgen.knodlseder@irap.omp.eu>
2 2  
3 3 * Version 1.1.0 released
4 4 ========================
5 5  
  6 + Add csobsdef.pntdef() method (#1771)
6 7 Add ctmapcube tool (#1760)
7 8 Add "seed" parameter to cspull (#1755)
8 9 Add ctedispcube tool (#1724)
... ...
1 1 New Features and Important Changes in ctools 1.1.0
2 2  
3   -17 April 2016
  3 +22 April 2016
4 4  
5 5  
6 6 Introduction
... ... @@ -200,6 +200,9 @@ Add csworkflow script that executes an arbitrary analysis workflow (#1508).
200 200  
201 201 Transformed cta_root2caldb into csroot2caldb script.
202 202  
  203 +Add csobsdef.pntdef() method to set directly a pointing definition from a
  204 +gammalib.GCsv object (#1771).
  205 +
203 206  
204 207 Examples
205 208 --------
... ...
cscripts/csobsdef.py
... ... @@ -76,18 +76,21 @@ class csobsdef(ctools.cscript):
76 76 def __init__(self, *argv):
77 77 """
78 78 Constructor.
  79 +
  80 + Parameters
  81 + ----------
  82 + argv : list of str
  83 + List of IRAF command line parameter strings of the form
  84 + ``parameter=3``.
79 85 """
80 86 # Set name and version
81   - self._name = "csobsdef"
82   - self._version = "1.1.0"
  87 + self._name = 'csobsdef'
  88 + self._version = '1.1.0'
83 89  
84 90 # Initialise class members
85   - self._obs = gammalib.GObservations()
86   - self._inpnt = ""
87   - self._tmin = 0.0
88   - self._chatter = 2
89   - self._clobber = True
90   - self._debug = False
  91 + self._obs = gammalib.GObservations()
  92 + self._pntdef = gammalib.GCsv()
  93 + self._tmin = 0.0
91 94  
92 95 # Initialise application by calling the appropriate class
93 96 # constructor.
... ... @@ -102,22 +105,18 @@ class csobsdef(ctools.cscript):
102 105 """
103 106 Get parameters from parfile.
104 107 """
105   - # Read parameters
106   - self._inpnt = self["inpnt"].filename()
  108 + # Query input filename if necessary
  109 + if self._pntdef.size() == 0:
  110 + self['inpnt'].filename()
107 111  
108 112 # Read ahead parameters
109 113 if self._read_ahead():
110   - self["outobs"].filename()
111   -
112   - # Set some fixed parameters
113   - self._chatter = self["chatter"].integer()
114   - self._clobber = self["clobber"].boolean()
115   - self._debug = self["debug"].boolean()
  114 + self['outobs'].filename()
116 115  
117 116 # Write input parameters into logger
118 117 if self._logTerse():
119 118 self._log_parameters()
120   - self._log("\n")
  119 + self._log('\n')
121 120  
122 121 # Return
123 122 return
... ... @@ -134,8 +133,8 @@ class csobsdef(ctools.cscript):
134 133 xml = gammalib.GXmlElement()
135 134  
136 135 # Append parameter
137   - parameter = "parameter name=\"Calibration\" database=\""+\
138   - caldb+"\" response=\""+irf+"\""
  136 + parameter = 'parameter name="Calibration" database="'+caldb+\
  137 + '" response="'+irf+'"'
139 138 xml.append(gammalib.GXmlElement(parameter))
140 139  
141 140 # Create CTA response
... ... @@ -153,6 +152,11 @@ class csobsdef(ctools.cscript):
153 152 def run(self):
154 153 """
155 154 Run the script.
  155 +
  156 + Raises
  157 + ------
  158 + RuntimeError
  159 + Invalid pointing definition file format.
156 160 """
157 161 # Switch screen logging on in debug mode
158 162 if self._logDebug():
... ... @@ -163,17 +167,18 @@ class csobsdef(ctools.cscript):
163 167  
164 168 # Write header into logger
165 169 if self._logTerse():
166   - self._log("\n")
167   - self._log.header1("Creating observation definition XML file")
  170 + self._log('\n')
  171 + self._log.header1('Creating observation definition XML file')
168 172  
169   - # Load pointing definition file
170   - pntdef = gammalib.GCsv(self._inpnt, ",")
171   - ncols = pntdef.ncols()
172   - npnt = pntdef.nrows()-1
  173 + # Load pointing definition file if it is not already set
  174 + if self._pntdef.size() == 0:
  175 + self._pntdef = gammalib.GCsv(self['inpnt'].filename(), ',')
  176 + ncols = self._pntdef.ncols()
  177 + npnt = self._pntdef.nrows()-1
173 178  
174 179 # Throw an exception is there is no header information
175   - if pntdef.nrows() < 1:
176   - raise RuntimeError("No header found in pointing definition file.")
  180 + if self._pntdef.nrows() < 1:
  181 + raise RuntimeError('No header found in pointing definition file.')
177 182  
178 183 # Clear observation container
179 184 self._obs.clear()
... ... @@ -182,7 +187,7 @@ class csobsdef(ctools.cscript):
182 187 # Extract header from pointing definition file
183 188 header = []
184 189 for col in range(ncols):
185   - header.append(pntdef[0,col])
  190 + header.append(self._pntdef[0,col])
186 191  
187 192 # Loop over all pointings
188 193 for pnt in range(npnt):
... ... @@ -194,60 +199,60 @@ class csobsdef(ctools.cscript):
194 199 obs = gammalib.GCTAObservation()
195 200  
196 201 # Set observation name
197   - if "name" in header:
198   - name = pntdef[row, header.index("name")]
  202 + if 'name' in header:
  203 + name = self._pntdef[row, header.index('name')]
199 204 else:
200   - name = "None"
  205 + name = 'None'
201 206 obs.name(name)
202 207  
203 208 # Set identifier
204   - if "id" in header:
205   - id_ = pntdef[row, header.index("id")]
  209 + if 'id' in header:
  210 + id_ = self._pntdef[row, header.index('id')]
206 211 else:
207   - id_ = "%6.6d" % identifier
  212 + id_ = '%6.6d' % identifier
208 213 identifier += 1
209 214 obs.id(id_)
210 215  
211 216 # Set pointing
212   - if "ra" in header and "dec" in header:
213   - ra = float(pntdef[row, header.index("ra")])
214   - dec = float(pntdef[row, header.index("dec")])
  217 + if 'ra' in header and 'dec' in header:
  218 + ra = float(self._pntdef[row, header.index('ra')])
  219 + dec = float(self._pntdef[row, header.index('dec')])
215 220 pntdir = gammalib.GSkyDir()
216 221 pntdir.radec_deg(ra,dec)
217   - elif "lon" in header and "lat" in header:
218   - lon = float(pntdef[row, header.index("lon")])
219   - lat = float(pntdef[row, header.index("lat")])
  222 + elif 'lon' in header and 'lat' in header:
  223 + lon = float(self._pntdef[row, header.index('lon')])
  224 + lat = float(self._pntdef[row, header.index('lat')])
220 225 pntdir = gammalib.GSkyDir()
221 226 pntdir.lb_deg(lon,lat)
222 227 else:
223   - raise RuntimeError("No (ra,dec) or (lon,lat) columns "
224   - "found in pointing definition file.")
  228 + raise RuntimeError('No (ra,dec) or (lon,lat) columns '
  229 + 'found in pointing definition file.')
225 230 obs.pointing(gammalib.GCTAPointing(pntdir))
226 231  
227 232 # Set response function
228   - if "caldb" in header:
229   - caldb = pntdef[row, header.index("caldb")]
  233 + if 'caldb' in header:
  234 + caldb = self._pntdef[row, header.index('caldb')]
230 235 else:
231   - caldb = self["caldb"].string()
232   - if "irf" in header:
233   - irf = pntdef[row, header.index("irf")]
  236 + caldb = self['caldb'].string()
  237 + if 'irf' in header:
  238 + irf = self._pntdef[row, header.index('irf')]
234 239 else:
235   - irf = self["irf"].string()
236   - if caldb != "" and irf != "":
  240 + irf = self['irf'].string()
  241 + if caldb != '' and irf != '':
237 242 obs = self._set_response(obs, caldb, irf)
238 243  
239 244 # Set deadtime correction factor
240   - if "deadc" in header:
241   - deadc = float(pntdef[row, header.index("deadc")])
  245 + if 'deadc' in header:
  246 + deadc = float(self._pntdef[row, header.index('deadc')])
242 247 else:
243   - deadc = self["deadc"].real()
  248 + deadc = self['deadc'].real()
244 249 obs.deadc(deadc)
245 250  
246 251 # Set Good Time Interval
247   - if "duration" in header:
248   - duration = float(pntdef[row, header.index("duration")])
  252 + if 'duration' in header:
  253 + duration = float(self._pntdef[row, header.index('duration')])
249 254 else:
250   - duration = self["duration"].real()
  255 + duration = self['duration'].real()
251 256 tmin = self._tmin
252 257 tmax = self._tmin + duration
253 258 gti = gammalib.GGti(self._time_reference())
... ... @@ -261,33 +266,33 @@ class csobsdef(ctools.cscript):
261 266 # Set Energy Boundaries
262 267 has_emin = False
263 268 has_emax = False
264   - if "emin" in header:
265   - emin = float(pntdef[row, header.index("emin")])
  269 + if 'emin' in header:
  270 + emin = float(self._pntdef[row, header.index('emin')])
266 271 has_emin = True
267 272 else:
268   - if self["emin"].is_valid():
269   - emin = self["emin"].real()
  273 + if self['emin'].is_valid():
  274 + emin = self['emin'].real()
270 275 has_emin = True
271   - if "emax" in header:
272   - emax = float(pntdef[row, header.index("emax")])
  276 + if 'emax' in header:
  277 + emax = float(self._pntdef[row, header.index('emax')])
273 278 has_emax = True
274 279 else:
275   - if self["emax"].is_valid():
276   - emax = self["emax"].real()
  280 + if self['emax'].is_valid():
  281 + emax = self['emax'].real()
277 282 has_emax = True
278 283 has_ebounds = has_emin and has_emax
279 284 if has_ebounds:
280   - ebounds = gammalib.GEbounds(gammalib.GEnergy(emin, "TeV"),
281   - gammalib.GEnergy(emax, "TeV"))
  285 + ebounds = gammalib.GEbounds(gammalib.GEnergy(emin, 'TeV'),
  286 + gammalib.GEnergy(emax, 'TeV'))
282 287  
283 288 # Set ROI
284 289 has_roi = False
285   - if "rad" in header:
286   - rad = float(pntdef[row, header.index("rad")])
  290 + if 'rad' in header:
  291 + rad = float(self._pntdef[row, header.index('rad')])
287 292 has_roi = True
288 293 else:
289   - if self["rad"].is_valid():
290   - rad = self["rad"].real()
  294 + if self['rad'].is_valid():
  295 + rad = self['rad'].real()
291 296 has_roi = True
292 297 if has_roi:
293 298 roi = gammalib.GCTARoi(gammalib.GCTAInstDir(pntdir), rad)
... ... @@ -308,11 +313,11 @@ class csobsdef(ctools.cscript):
308 313 # Write observation into logger
309 314 if self._logExplicit():
310 315 self._log(str(obs))
311   - self._log("\n")
  316 + self._log('\n')
312 317 elif self._logTerse():
313   - self._log(gammalib.parformat(obs.instrument()+" observation"))
314   - self._log("Name=\""+obs.name()+"\" ")
315   - self._log("ID=\""+obs.id()+"\"\n")
  318 + self._log(gammalib.parformat(obs.instrument()+' observation'))
  319 + self._log('Name="'+obs.name()+'" ')
  320 + self._log('ID="'+obs.id()+'"\n')
316 321  
317 322 # Append observation
318 323 self._obs.append(obs)
... ... @@ -326,20 +331,20 @@ class csobsdef(ctools.cscript):
326 331 """
327 332 # Write header and filename into logger
328 333 if self._logTerse():
329   - self._log("\n")
330   - self._log.header1("Save observation definition XML file")
  334 + self._log('\n')
  335 + self._log.header1('Save observation definition XML file')
331 336  
332 337 # Get output filename in case it was not read ahead
333   - outobs = self["outobs"].filename()
  338 + outobs = self['outobs'].filename()
334 339  
335 340 # Check if observation definition XML file is valid
336   - if outobs.url() != "NONE":
  341 + if outobs.url() != 'NONE':
337 342  
338 343 # Log filename
339 344 if self._logTerse():
340   - self._log(gammalib.parformat("Observation XML file"))
  345 + self._log(gammalib.parformat('Observation XML file'))
341 346 self._log(outobs.url())
342   - self._log("\n")
  347 + self._log('\n')
343 348  
344 349 # Save observation definition XML file
345 350 self._obs.save(outobs)
... ... @@ -366,6 +371,21 @@ class csobsdef(ctools.cscript):
366 371 # Return
367 372 return
368 373  
  374 + def pntdef(self, csv):
  375 + """
  376 + Set pointing definition from a CSV table.
  377 +
  378 + Parameters
  379 + ----------
  380 + csv : `~gammalib.GCsv`
  381 + Comma-separated values table
  382 + """
  383 + # Set pointing definition
  384 + self._pntdef = csv
  385 +
  386 + # Return
  387 + return
  388 +
369 389  
370 390 # ======================== #
371 391 # Main routine entry point #
... ...
cscripts/csroot2caldb.py
... ... @@ -27,7 +27,7 @@ from datetime import datetime
27 27  
28 28 # Optional ROOT import
29 29 try:
30   - from ROOTx import TFile, TH2F, TH3F
  30 + from ROOT import TFile, TH2F, TH3F
31 31 _has_root = True
32 32 except:
33 33 _has_root = False
... ...
test/test_csobsdef.py
... ... @@ -51,11 +51,11 @@ class Test(gammalib.GPythonTestSuite):
51 51 Set all test functions.
52 52 """
53 53 # Set test name
54   - self.name("csobsdef")
  54 + self.name('csobsdef')
55 55  
56 56 # Append tests
57   - self.append(self._test_cmd, "Test csobsdef on command line")
58   - self.append(self._test_python, "Test csobsdef from Python")
  57 + self.append(self._test_cmd, 'Test csobsdef on command line')
  58 + self.append(self._test_python, 'Test csobsdef from Python')
59 59  
60 60 # Return
61 61 return
... ... @@ -66,47 +66,47 @@ class Test(gammalib.GPythonTestSuite):
66 66 Test csobsdef on the command line.
67 67 """
68 68 # Kluge to set the command (installed version has no README file)
69   - if os.path.isfile("README"):
70   - csobsdef = "../cscripts/csobsdef.py"
  69 + if os.path.isfile('README'):
  70 + csobsdef = '../cscripts/csobsdef.py'
71 71 else:
72   - csobsdef = "csobsdef"
  72 + csobsdef = 'csobsdef'
73 73  
74 74 # Setup csobsdef command
75 75 cmd = csobsdef+' inpnt="data/pntdef.dat"'+ \
76   - ' outobs="obsdef_cmd1.xml"'+ \
  76 + ' outobs="csobsdef_cmd1.xml"'+ \
77 77 ' caldb="prod2" irf="South_0.5h"'+ \
78 78 ' emin=0.1 emax=100.0 duration=1800.0 rad=5.0'+ \
79 79 ' logfile="csobsdef_cmd1.log" chatter=1'
80 80  
81 81 # Execute csobsdef, make sure we catch any exception
82 82 try:
83   - rc = os.system(cmd+" >/dev/null 2>&1")
  83 + rc = os.system(cmd+' >/dev/null 2>&1')
84 84 except:
85 85 pass
86 86  
87 87 # Check if execution was successful
88 88 self.test_assert(rc == 0,
89   - "Successful csobsdef execution on command line")
  89 + 'Successful csobsdef execution on command line')
90 90  
91 91 # Check observation definition file
92   - self._check_obsdef_file("obsdef_cmd1.xml")
  92 + self._check_obsdef_file('csobsdef_cmd1.xml')
93 93  
94 94 # Setup csobsdef command
95 95 cmd = csobsdef+' inpnt="obs_definition_that_does_not_exist.dat"'+ \
96   - ' outobs="obsdef_cmd1.xml"'+ \
  96 + ' outobs="csobsdef_cmd1.xml"'+ \
97 97 ' caldb="prod2" irf="South_0.5h"'+ \
98 98 ' emin=0.1 emax=100.0 duration=1800.0 rad=5.0'+ \
99 99 ' logfile="csobsdef_cmd2.log"'
100 100  
101 101 # Execute csobsdef, make sure we catch any exception
102 102 try:
103   - rc = os.system(cmd+" >/dev/null 2>&1")
  103 + rc = os.system(cmd+' >/dev/null 2>&1')
104 104 except:
105 105 pass
106 106  
107 107 # Check if execution failed
108 108 self.test_assert(rc != 0,
109   - "Failure of csobsdef execution on command line")
  109 + 'Failure of csobsdef execution on command line')
110 110  
111 111 # Return
112 112 return
... ... @@ -118,16 +118,16 @@ class Test(gammalib.GPythonTestSuite):
118 118 """
119 119 # Set-up csobsdef
120 120 obsdef = cscripts.csobsdef()
121   - obsdef["inpnt"] = "data/pntdef.dat"
122   - obsdef["outobs"] = "obsdef_py1.xml"
123   - obsdef["caldb"] = "prod2"
124   - obsdef["irf"] = "South_0.5h"
125   - obsdef["emin"] = 0.1
126   - obsdef["emax"] = 100.0
127   - obsdef["duration"] = 1800.0
128   - obsdef["rad"] = 5.0
129   - obsdef["logfile"] = "csobsdef_py1.log"
130   - obsdef["chatter"] = 2
  121 + obsdef['inpnt'] = 'data/pntdef.dat'
  122 + obsdef['outobs'] = 'csobsdef_py1.xml'
  123 + obsdef['caldb'] = 'prod2'
  124 + obsdef['irf'] = 'South_0.5h'
  125 + obsdef['emin'] = 0.1
  126 + obsdef['emax'] = 100.0
  127 + obsdef['duration'] = 1800.0
  128 + obsdef['rad'] = 5.0
  129 + obsdef['logfile'] = 'csobsdef_py1.log'
  130 + obsdef['chatter'] = 2
131 131  
132 132 # Run csobsdef script and save XML file
133 133 obsdef.logFileOpen() # Make sure we get a log file
... ... @@ -135,26 +135,48 @@ class Test(gammalib.GPythonTestSuite):
135 135 obsdef.save()
136 136  
137 137 # Check model file
138   - self._check_obsdef_file("obsdef_py1.xml")
  138 + self._check_obsdef_file('csobsdef_py1.xml')
139 139  
140 140 # Set-up csobsdef
141 141 obsdef = cscripts.csobsdef()
142   - obsdef["inpnt"] = "data/pntdef.dat"
143   - obsdef["outobs"] = "obsdef_py2.xml"
144   - obsdef["caldb"] = "prod2"
145   - obsdef["irf"] = "South_0.5h"
146   - obsdef["emin"] = 0.1
147   - obsdef["emax"] = 100.0
148   - obsdef["duration"] = 1800.0
149   - obsdef["rad"] = 5.0
150   - obsdef["logfile"] = "csobsdef_py2.log"
151   - obsdef["chatter"] = 3
  142 + obsdef['inpnt'] = 'data/pntdef.dat'
  143 + obsdef['outobs'] = 'csobsdef_py2.xml'
  144 + obsdef['caldb'] = 'prod2'
  145 + obsdef['irf'] = 'South_0.5h'
  146 + obsdef['emin'] = 0.1
  147 + obsdef['emax'] = 100.0
  148 + obsdef['duration'] = 1800.0
  149 + obsdef['rad'] = 5.0
  150 + obsdef['logfile'] = 'csobsdef_py2.log'
  151 + obsdef['chatter'] = 3
152 152  
153 153 # Execute csobsdef script
154 154 obsdef.execute()
155 155  
156 156 # Check model file
157   - self._check_obsdef_file("obsdef_py2.xml")
  157 + self._check_obsdef_file('csobsdef_py2.xml')
  158 +
  159 + # Load CSV table
  160 + csv = gammalib.GCsv('data/pntdef.dat', ',')
  161 +
  162 + # Set-up csobsdef
  163 + obsdef = cscripts.csobsdef()
  164 + obsdef.pntdef(csv)
  165 + obsdef['outobs'] = 'csobsdef_py3.xml'
  166 + obsdef['caldb'] = 'prod2'
  167 + obsdef['irf'] = 'South_0.5h'
  168 + obsdef['emin'] = 0.1
  169 + obsdef['emax'] = 100.0
  170 + obsdef['duration'] = 1800.0
  171 + obsdef['rad'] = 5.0
  172 + obsdef['logfile'] = 'csobsdef_py3.log'
  173 + obsdef['chatter'] = 4
  174 +
  175 + # Execute csobsdef script
  176 + obsdef.execute()
  177 +
  178 + # Check model file
  179 + self._check_obsdef_file('csobsdef_py3.xml')
158 180  
159 181 # Return
160 182 return
... ... @@ -173,7 +195,7 @@ class Test(gammalib.GPythonTestSuite):
173 195  
174 196 # Loop over all observations
175 197 for o in obs:
176   - self.test_assert(o.instrument() == "CTA",
  198 + self.test_value(o.instrument(), 'CTA',
177 199 'Check for "CTA" instrument')
178 200 self.test_value(o.ontime(), 1800.0, 1.0e-6,
179 201 'Check for ontime of 1800 sec')
... ...