Commit 8308d2fbcf13d5926cf922389dc74928af662b66

Authored by Jürgen Knödlseder
1 parent c81a8bdb

New mode with optional parameters.

Showing 1 changed file with 95 additions and 19 deletions
scripts/csobsdef.py
... ... @@ -43,11 +43,30 @@ class csobsdef(ctools.cscript):
43 43 duration - Duration of pointing (seconds)
44 44 emin - Lower energy limit (TeV)
45 45 emax - Upper energy limit (TeV)
46   - rad - Radius of region of interest (deg)
47   -
  46 + rad - Radius of region of interest (deg)
  47 + deadc - Deadtime correction factor [0-1]
  48 + caldb - Calibration database
  49 + irf - Response function name
  50 +
48 51 Only the pairs (ra,dec) or (lon,lat) are mandatory header keywords.
49   - All other are optional and are queried as user parameters if they
50   - are not specified in the pointing list.
  52 + All other keywords are optional and can be specified when calling
  53 + csobsdef as user parameters. The only exception is the "duration"
  54 + keyword that will automatically queried.
  55 +
  56 + Here some usage examples:
  57 +
  58 + csobsdef
  59 + Creates minimal observation definition file.
  60 +
  61 + csobsdef emin=0.1 emax=100.0
  62 + Creates observation definition file with an energy range 100 GeV - 100 TeV.
  63 +
  64 + csobsdef rad=5
  65 + Creates observation definition file with a ROI radius of 5 deg.
  66 +
  67 + csobsdef caldb=dummy irf=cta_dummy_irf
  68 + Creates observation definition file using the "cta_dummy_irf" IRF in the
  69 + "dummy" calibration database.
51 70 """
52 71 def __init__(self, *argv):
53 72 """
... ... @@ -111,10 +130,12 @@ class csobsdef(ctools.cscript):
111 130 pars = gammalib.GApplicationPars()
112 131 pars.append(gammalib.GApplicationPar("inpnt","f","a","NONE","","","Pointing definition file"))
113 132 pars.append(gammalib.GApplicationPar("outobs","f","a","obs.xml","","","Output observation definition file"))
114   - pars.append(gammalib.GApplicationPar("emin","r","a","0.1","","","Lower energy limit (TeV)"))
115   - pars.append(gammalib.GApplicationPar("emax","r","a","100.0","","","Upper energy limit (TeV)"))
116 133 pars.append(gammalib.GApplicationPar("duration","r","a","1800.0","","","Pointing duration (seconds)"))
117   - pars.append(gammalib.GApplicationPar("rad","r","a","5.0","","","Radius of ROI (degrees)"))
  134 + pars.append(gammalib.GApplicationPar("caldb","s","h","","","","Calibration database"))
  135 + pars.append(gammalib.GApplicationPar("irf","s","h","","","","Instrument response function"))
  136 + pars.append(gammalib.GApplicationPar("emin","r","h","UNDEF","","","Lower energy limit (TeV)"))
  137 + pars.append(gammalib.GApplicationPar("emax","r","h","UNDEF","","","Upper energy limit (TeV)"))
  138 + pars.append(gammalib.GApplicationPar("rad","r","h","UNDEF","","","Radius of ROI (degrees)"))
118 139 pars.append(gammalib.GApplicationPar("deadc","r","h","0.95","","","Deadtime correction factor"))
119 140 pars.append_standard()
120 141 pars.append(gammalib.GApplicationPar("logfile","f","h","csobsdef.log","","","Log filename"))
... ... @@ -198,7 +219,7 @@ class csobsdef(ctools.cscript):
198 219  
199 220 # Set row index
200 221 row = pnt + 1
201   -
  222 +
202 223 # Create CTA observation
203 224 obs = gammalib.GCTAObservation()
204 225  
... ... @@ -234,6 +255,18 @@ class csobsdef(ctools.cscript):
234 255 " definition file.")
235 256 obs.pointing(gammalib.GCTAPointing(pntdir))
236 257  
  258 + # Set response function
  259 + if "caldb" in header:
  260 + caldb = pntdef[row, header.index("caldb")]
  261 + else:
  262 + caldb = self["caldb"].string()
  263 + if "irf" in header:
  264 + irf = pntdef[row, header.index("irf")]
  265 + else:
  266 + irf = self["irf"].string()
  267 + if caldb != "" and irf != "":
  268 + obs = self.__set_response(obs, caldb, irf)
  269 +
237 270 # Set deadtime correction factor
238 271 if "deadc" in header:
239 272 deadc = float(pntdef[row, header.index("deadc")])
... ... @@ -257,29 +290,48 @@ class csobsdef(ctools.cscript):
257 290 obs.livetime(gti.ontime()*deadc)
258 291  
259 292 # Set Energy Boundaries
  293 + has_emin = False
  294 + has_emax = False
260 295 if "emin" in header:
261   - emin = float(pntdef[row, header.index("emin")])
  296 + emin = float(pntdef[row, header.index("emin")])
  297 + has_emin = True
262 298 else:
263   - emin = self["emin"].real()
  299 + if self["emin"].is_valid():
  300 + emin = self["emin"].real()
  301 + has_emin = True
264 302 if "emax" in header:
265   - emax = float(pntdef[row, header.index("emax")])
  303 + emax = float(pntdef[row, header.index("emax")])
  304 + has_emax = True
266 305 else:
267   - emax = self["emax"].real()
268   - ebounds = gammalib.GEbounds(gammalib.GEnergy(emin, "TeV"),
269   - gammalib.GEnergy(emax, "TeV"))
  306 + if self["emax"].is_valid():
  307 + emax = self["emax"].real()
  308 + has_emax = True
  309 + has_ebounds = has_emin and has_emax
  310 + if has_ebounds:
  311 + ebounds = gammalib.GEbounds(gammalib.GEnergy(emin, "TeV"),
  312 + gammalib.GEnergy(emax, "TeV"))
270 313  
271 314 # Set ROI
  315 + has_roi = False
272 316 if "rad" in header:
273   - rad = float(pntdef[row, header.index("rad")])
  317 + rad = float(pntdef[row, header.index("rad")])
  318 + has_roi = True
274 319 else:
275   - rad = self["rad"].real()
276   - roi = gammalib.GCTARoi(gammalib.GCTAInstDir(pntdir), rad)
  320 + if self["rad"].is_valid():
  321 + rad = self["rad"].real()
  322 + has_roi = True
  323 + if has_roi:
  324 + roi = gammalib.GCTARoi(gammalib.GCTAInstDir(pntdir), rad)
277 325  
278 326 # Create an empty event list
279 327 list = gammalib.GCTAEventList()
280   - list.roi(roi)
281 328 list.gti(gti)
282   - list.ebounds(ebounds)
  329 +
  330 + # Set optional information
  331 + if has_ebounds:
  332 + list.ebounds(ebounds)
  333 + if has_roi:
  334 + list.roi(roi)
283 335  
284 336 # Attach event list to CTA observation
285 337 obs.events(list)
... ... @@ -290,6 +342,30 @@ class csobsdef(ctools.cscript):
290 342 # Return
291 343 return
292 344  
  345 + def __set_response(self, obs, caldb, irf):
  346 + """
  347 + Set response for an observation. We create an XML element for that
  348 + so that the response XML writer will write the database and response
  349 + name into the observation definition file.
  350 + """
  351 + # Create XML element
  352 + xml = gammalib.GXmlElement()
  353 +
  354 + # Append parameter
  355 + parameter = "parameter name=\"Calibration\" database=\""+\
  356 + caldb+"\" response=\""+irf+"\""
  357 + xml.append(gammalib.GXmlElement(parameter))
  358 +
  359 + # Create CTA response
  360 + response = gammalib.GCTAResponseIrf()
  361 + response.read(xml)
  362 +
  363 + # Attach response to observation
  364 + obs.response(response)
  365 +
  366 + # Return observation
  367 + return obs
  368 +
293 369  
294 370 # ======================== #
295 371 # Main routine entry point #
... ...