Commit cf531ccafdfdea0dd3a58755d93c263d188376ec
1 parent
25f4cad1
Correct sim function, and add set_obs and set_obs_list functions.
The CTA observation container was not copied in the sim function following yesterday's merge, this was corrected. The set function is now obsolete, and has been renamed to set_obs for clarity. The set function is still there, but a warning is issued if it is still used. A set_obs_list function has been added that allows setting lists of observations.
Showing
1 changed file
with
160 additions
and
24 deletions
scripts/obsutils.py
| ... | ... | @@ -43,7 +43,7 @@ def sim(obs, log=False, debug=False, edisp=False, seed=0, nbins=0, |
| 43 | 43 | """ |
| 44 | 44 | # Allocate ctobssim application and set parameters |
| 45 | 45 | sim = ctools.ctobssim(obs) |
| 46 | - sim['seed'].integer(seed) | |
| 46 | + sim["seed"].integer(seed) | |
| 47 | 47 | |
| 48 | 48 | # Optionally open the log file |
| 49 | 49 | if log: |
| ... | ... | @@ -106,6 +106,13 @@ def sim(obs, log=False, debug=False, edisp=False, seed=0, nbins=0, |
| 106 | 106 | # (the ctbin object will go out of scope one the function is |
| 107 | 107 | # left) |
| 108 | 108 | obs = bin.obs().copy() |
| 109 | + | |
| 110 | + else: | |
| 111 | + | |
| 112 | + # Make a deep copy of the observation that will be returned | |
| 113 | + # (the ctobssim object will go out of scope one the function is | |
| 114 | + # left) | |
| 115 | + obs = sim.obs().copy() | |
| 109 | 116 | |
| 110 | 117 | # Delete the simulation |
| 111 | 118 | del sim |
| ... | ... | @@ -264,18 +271,40 @@ def modmap(obs, eref=0.1, proj="TAN", coord="GAL", xval=0.0, yval=0.0, \ |
| 264 | 271 | return map |
| 265 | 272 | |
| 266 | 273 | |
| 267 | -# ======================= # | |
| 268 | -# Set one CTA observation # | |
| 269 | -# ======================= # | |
| 274 | +# ===================================== # | |
| 275 | +# Set one CTA observation (old version) # | |
| 276 | +# ===================================== # | |
| 270 | 277 | def set(pntdir, tstart=0.0, duration=1800.0, deadc=0.95, \ |
| 271 | 278 | emin=0.1, emax=100.0, rad=5.0, \ |
| 272 | 279 | irf="cta_dummy_irf", caldb="dummy"): |
| 273 | 280 | """ |
| 274 | - Returns a single CTA observation. By looping over this function we can | |
| 275 | - add CTA observations to the observation container. | |
| 281 | + Obsolete function, use set_obs instead. | |
| 282 | + """ | |
| 283 | + # Print warning | |
| 284 | + print("Warning: obsutils.set is obsolete, use obsutils.set_obs instead.") | |
| 285 | + | |
| 286 | + # Call new function | |
| 287 | + obs_cta = set_obs(pntdir, tstart=tstart, duration=duration, deadc=deadc, \ | |
| 288 | + emin=emin, emax=emax, rad=rad, \ | |
| 289 | + irf=irf, caldb=caldb) | |
| 290 | + | |
| 291 | + # Return CTA observation | |
| 292 | + return obs_cta | |
| 293 | + | |
| 294 | + | |
| 295 | +# ======================= # | |
| 296 | +# Set one CTA observation # | |
| 297 | +# ======================= # | |
| 298 | +def set_obs(pntdir, tstart=0.0, duration=1800.0, deadc=0.95, \ | |
| 299 | + emin=0.1, emax=100.0, rad=5.0, \ | |
| 300 | + irf="cta_dummy_irf", caldb="dummy", id="000000"): | |
| 301 | + """ | |
| 302 | + Returns a single CTA observation containing an empty CTA event list. | |
| 303 | + By looping over this function you can add CTA observations to the | |
| 304 | + observation container. | |
| 276 | 305 | |
| 277 | 306 | Parameters: |
| 278 | - pntdir - Pointing direction | |
| 307 | + pntdir - Pointing direction [GSkyDir] | |
| 279 | 308 | Keywords: |
| 280 | 309 | tstart - Start time [seconds] (default: 0.0) |
| 281 | 310 | duration - Duration of observation [seconds] (default: 1800.0) |
| ... | ... | @@ -287,7 +316,7 @@ def set(pntdir, tstart=0.0, duration=1800.0, deadc=0.95, \ |
| 287 | 316 | caldb - Calibration database path (default: "dummy") |
| 288 | 317 | """ |
| 289 | 318 | # Allocate CTA observation |
| 290 | - obs = gammalib.GCTAObservation() | |
| 319 | + obs_cta = gammalib.GCTAObservation() | |
| 291 | 320 | |
| 292 | 321 | # Set calibration database |
| 293 | 322 | db = gammalib.GCaldb() |
| ... | ... | @@ -299,7 +328,7 @@ def set(pntdir, tstart=0.0, duration=1800.0, deadc=0.95, \ |
| 299 | 328 | # Set pointing direction |
| 300 | 329 | pnt = gammalib.GCTAPointing() |
| 301 | 330 | pnt.dir(pntdir) |
| 302 | - obs.pointing(pnt) | |
| 331 | + obs_cta.pointing(pnt) | |
| 303 | 332 | |
| 304 | 333 | # Set ROI |
| 305 | 334 | roi = gammalib.GCTARoi() |
| ... | ... | @@ -309,33 +338,140 @@ def set(pntdir, tstart=0.0, duration=1800.0, deadc=0.95, \ |
| 309 | 338 | roi.radius(rad) |
| 310 | 339 | |
| 311 | 340 | # Set GTI |
| 312 | - gti = gammalib.GGti() | |
| 313 | - start = gammalib.GTime(tstart) | |
| 314 | - stop = gammalib.GTime(tstart+duration) | |
| 315 | - gti.append(start, stop) | |
| 341 | + gti = gammalib.GGti() | |
| 342 | + gti.append(gammalib.GTime(tstart), gammalib.GTime(tstart+duration)) | |
| 316 | 343 | |
| 317 | 344 | # Set energy boundaries |
| 318 | 345 | ebounds = gammalib.GEbounds() |
| 319 | - e_min = gammalib.GEnergy() | |
| 320 | - e_max = gammalib.GEnergy() | |
| 321 | - e_min.TeV(emin) | |
| 322 | - e_max.TeV(emax) | |
| 323 | - ebounds.append(e_min, e_max) | |
| 346 | + ebounds.append(gammalib.GEnergy(emin, "TeV"), gammalib.GEnergy(emax, "TeV")) | |
| 324 | 347 | |
| 325 | 348 | # Allocate event list |
| 326 | 349 | events = gammalib.GCTAEventList() |
| 327 | 350 | events.roi(roi) |
| 328 | 351 | events.gti(gti) |
| 329 | 352 | events.ebounds(ebounds) |
| 330 | - obs.events(events) | |
| 353 | + obs_cta.events(events) | |
| 331 | 354 | |
| 332 | 355 | # Set instrument response |
| 333 | - obs.response(irf, db) | |
| 356 | + obs_cta.response(irf, db) | |
| 334 | 357 | |
| 335 | 358 | # Set ontime, livetime, and deadtime correction factor |
| 336 | - obs.ontime(duration) | |
| 337 | - obs.livetime(duration*deadc) | |
| 338 | - obs.deadc(deadc) | |
| 359 | + obs_cta.ontime(duration) | |
| 360 | + obs_cta.livetime(duration*deadc) | |
| 361 | + obs_cta.deadc(deadc) | |
| 362 | + obs_cta.id(id) | |
| 363 | + | |
| 364 | + # Return CTA observation | |
| 365 | + return obs_cta | |
| 366 | + | |
| 367 | + | |
| 368 | +# ============================ # | |
| 369 | +# Set list of CTA observations # | |
| 370 | +# ============================ # | |
| 371 | +def set_obs_list(obsdeflist, tstart=0.0, duration=1800.0, deadc=0.95, \ | |
| 372 | + emin=0.1, emax=100.0, rad=5.0, \ | |
| 373 | + irf="cta_dummy_irf", caldb="dummy"): | |
| 374 | + """ | |
| 375 | + Returns an observation container filled with a list of CTA observations. | |
| 376 | + The list is defined by the obsdeflist parameter which is a dictionnary | |
| 377 | + containing the mandatory keywords 'ra' and 'dec' that specify the | |
| 378 | + pointing direction for a given observation. Optional keyword give control | |
| 379 | + over other observation proposerties, such as duration, deadtime correction, | |
| 380 | + energy range, etc. If an optional keyword is not specified, the function | |
| 381 | + keyword is used instead. | |
| 339 | 382 | |
| 340 | - # Return observation | |
| 383 | + Parameters: | |
| 384 | + obsdeflist - Observation definition list [{'ra': x.xx, 'dec': x.xx}] | |
| 385 | + The directory can take the following optional keywords: | |
| 386 | + - duration: Duration of CTA observation [seconds] | |
| 387 | + - deadc: Deadtime correction factor | |
| 388 | + - emin: Minimum event energy [TeV] | |
| 389 | + - emax: Maximum event energy [TeV] | |
| 390 | + - rad: ROI radius used for analysis [deg] | |
| 391 | + - irf: Instrument response function | |
| 392 | + - caldb: Calibration database path | |
| 393 | + Optional keywords overwrite keywords specified in the | |
| 394 | + function call. | |
| 395 | + Keywords: | |
| 396 | + tstart - Start time [seconds] (default: 0.0) | |
| 397 | + duration - Duration of one CTA observation [seconds] (default: 1800.0) | |
| 398 | + deadc - Deadtime correction factor (default: 0.95) | |
| 399 | + emin - Minimum event energy [TeV] (default: 0.1) | |
| 400 | + emax - Maximum event energy [TeV] (default: 100.0) | |
| 401 | + rad - ROI radius used for analysis [deg] (default: 5.0) | |
| 402 | + irf - Instrument response function (default: cta_dummy_irf) | |
| 403 | + caldb - Calibration database path (default: "dummy") | |
| 404 | + """ | |
| 405 | + # Initialise empty observation container | |
| 406 | + obs = gammalib.GObservations() | |
| 407 | + | |
| 408 | + # Initialise first time and identifier | |
| 409 | + obs_start = tstart | |
| 410 | + obs_id = 1 | |
| 411 | + | |
| 412 | + # Loop over observation definition list | |
| 413 | + for obsdef in obsdeflist: | |
| 414 | + | |
| 415 | + # Set pointing direction | |
| 416 | + pntdir = gammalib.GSkyDir() | |
| 417 | + pntdir.radec_deg(obsdef['ra'], obsdef['dec']) | |
| 418 | + | |
| 419 | + # Set duration (use default if not found in definition list) | |
| 420 | + if 'duration' in obsdef: | |
| 421 | + obs_duration = obsdef['duration'] | |
| 422 | + else: | |
| 423 | + obs_duration = duration | |
| 424 | + | |
| 425 | + # Set emin (use default if not found in definition list) | |
| 426 | + if 'emin' in obsdef: | |
| 427 | + obs_emin = obsdef['emin'] | |
| 428 | + else: | |
| 429 | + obs_emin = emin | |
| 430 | + | |
| 431 | + # Set emax (use default if not found in definition list) | |
| 432 | + if 'emax' in obsdef: | |
| 433 | + obs_emax = obsdef['emax'] | |
| 434 | + else: | |
| 435 | + obs_emax = emax | |
| 436 | + | |
| 437 | + # Set radius (use default if not found in definition list) | |
| 438 | + if 'rad' in obsdef: | |
| 439 | + obs_rad = obsdef['rad'] | |
| 440 | + else: | |
| 441 | + obs_rad = rad | |
| 442 | + | |
| 443 | + # Set deadc (use default if not found in definition list) | |
| 444 | + if 'deadc' in obsdef: | |
| 445 | + obs_deadc = obsdef['deadc'] | |
| 446 | + else: | |
| 447 | + obs_deadc = deadc | |
| 448 | + | |
| 449 | + # Set caldb (use default if not found in definition list) | |
| 450 | + if 'caldb' in obsdef: | |
| 451 | + obs_caldb = obsdef['caldb'] | |
| 452 | + else: | |
| 453 | + obs_caldb = caldb | |
| 454 | + | |
| 455 | + # Set irf (use default if not found in definition list) | |
| 456 | + if 'irf' in obsdef: | |
| 457 | + obs_irf = obsdef['irf'] | |
| 458 | + else: | |
| 459 | + obs_irf = irf | |
| 460 | + | |
| 461 | + # Generate identifier string | |
| 462 | + id = "%6.6d" % obs_id | |
| 463 | + | |
| 464 | + # Set CTA observation | |
| 465 | + obs_cta = set_obs(pntdir, tstart=obs_start, duration=obs_duration, \ | |
| 466 | + deadc=obs_deadc, emin=obs_emin, emax=obs_emax, \ | |
| 467 | + rad=obs_rad, irf=obs_irf, caldb=obs_caldb, id=id) | |
| 468 | + | |
| 469 | + # Append to container | |
| 470 | + obs.append(obs_cta) | |
| 471 | + | |
| 472 | + # Update start time and identifier | |
| 473 | + obs_start += obs_duration | |
| 474 | + obs_id += 1 | |
| 475 | + | |
| 476 | + # Return observation container | |
| 341 | 477 | return obs | ... | ... |