Commit 23bd532eb1bc8c2275e4998c1f3245754119e7f1

Authored by Jürgen Knödlseder
1 parent 6a937bc4

Use pointing in get_stacked_response() if xref and yref arguments are None

Showing 1 changed file with 96 additions and 91 deletions
cscripts/obsutils.py
... ... @@ -25,7 +25,7 @@ import ctools
25 25 # Simulate observations #
26 26 # ===================== #
27 27 def sim(obs, log=False, debug=False, chatter=2, edisp=False, seed=0,
28   - emin=None, emax=None, nbins=0,
  28 + emin=None, emax=None, nbins=0, addbounds=False,
29 29 binsz=0.05, npix=200, proj='TAN', coord='GAL'):
30 30 """
31 31 Simulate events for all observations in the container
... ... @@ -58,6 +58,8 @@ def sim(obs, log=False, debug=False, chatter=2, edisp=False, seed=0,
58 58 Maximum energy of counts cube for binned (TeV)
59 59 nbins : int, optional
60 60 Number of energy bins (0=unbinned)
  61 + addbounds : bool, optional
  62 + Add boundaries at observation energies
61 63 binsz : float, optional
62 64 Pixel size for binned simulation (deg/pixel)
63 65 npix : int, optional
... ... @@ -92,22 +94,15 @@ def sim(obs, log=False, debug=False, chatter=2, edisp=False, seed=0,
92 94 # Binned option?
93 95 if nbins > 0:
94 96  
95   - # If energy boundaries are not given then determine common energy
96   - # boundaries for all observations
  97 + # If energy boundaries are not given then determine the minimum and
  98 + # the maximum energies from all observations and use these values
  99 + # as energy boundaries. The energy boundaries are given in TeV.
97 100 if emin == None or emax == None:
98   - emin = None
99   - emax = None
  101 + emin = 1.0e30
  102 + emax = 0.0
100 103 for run in sim.obs():
101   - run_emin = run.events().ebounds().emin().TeV()
102   - run_emax = run.events().ebounds().emax().TeV()
103   - if emin == None:
104   - emin = run_emin
105   - elif run_emin > emin:
106   - emin = run_emin
107   - if emax == None:
108   - emax = run_emax
109   - elif run_emax > emax:
110   - emax = run_emax
  104 + emin = min(run.events().ebounds().emin().TeV(), emin)
  105 + emax = max(run.events().ebounds().emax().TeV(), emax)
111 106  
112 107 # Allocate ctbin application and set parameters
113 108 bin = ctools.ctbin(sim.obs())
... ... @@ -136,26 +131,16 @@ def sim(obs, log=False, debug=False, chatter=2, edisp=False, seed=0,
136 131 # cubes and append them to the observation
137 132 if len(sim.obs()) > 1:
138 133  
139   - # Get xref, yref, coordsys and proj from counts cube
140   - _xref = bin.cube().counts().projection().crval(0)
141   - _yref = bin.cube().counts().projection().crval(1)
142   - _coordsys = bin.cube().counts().projection().coordsys()
143   - _proj = bin.cube().counts().projection().code()
144   -
145   - # Kluge: map "EQU" to "CEL" coordinate system (not sure why the
146   - # GSkyProjection::coordsys returns "EQU" instead of "CEL"
147   - if _coordsys == 'EQU':
148   - _coordsys = 'CEL'
149   -
150   - # Get stacked response
151   - response = get_stacked_response(sim.obs(), _xref, _yref,
  134 + # Get stacked response (use pointing for map centre)
  135 + response = get_stacked_response(sim.obs(), None, None,
152 136 binsz=binsz, nxpix=npix, nypix=npix,
153 137 emin=emin, emax=emax, enumbins=nbins,
154 138 edisp=edisp,
155   - coordsys=_coordsys, proj=_proj,
  139 + coordsys=coord, proj=proj,
  140 + addbounds=addbounds,
156 141 log=log, debug=debug,
157 142 chatter=chatter)
158   -
  143 +
159 144 # Set stacked response
160 145 if edisp:
161 146 bin.obs()[0].response(response['expcube'],
... ... @@ -478,11 +463,14 @@ def set_observations(ra, dec, rad, tstart, duration, emin, emax, irf, caldb,
478 463 # ==================== #
479 464 def get_stacked_response(obs, xref, yref, binsz=0.05, nxpix=200, nypix=200,
480 465 emin=0.1, emax=100.0, enumbins=20, edisp=False,
481   - coordsys='GAL', proj='TAN',
  466 + coordsys='GAL', proj='TAN', addbounds=False,
482 467 log=False, debug=False, chatter=2):
483 468 """
484 469 Get stacked response cubes
485 470  
  471 + If no xref and yref arguments have been specified then use the pointing
  472 + information to determine the map centre
  473 +
486 474 Parameters
487 475 ----------
488 476 obs : `~gammalib.GObservations`
... ... @@ -509,6 +497,8 @@ def get_stacked_response(obs, xref, yref, binsz=0.05, nxpix=200, nypix=200,
509 497 Coordinate system
510 498 proj : str, optional
511 499 Projection
  500 + addbounds : bool, optional
  501 + Add boundaries at observation energies
512 502 log : bool, optional
513 503 Create log file(s)
514 504 debug : bool, optional
... ... @@ -521,6 +511,13 @@ def get_stacked_response(obs, xref, yref, binsz=0.05, nxpix=200, nypix=200,
521 511 result : dict
522 512 Dictionary of response cubes
523 513 """
  514 + # If no xref and yref arguments have been specified then use the pointing
  515 + # information
  516 + if xref == None or yref == None:
  517 + usepnt = True
  518 + else:
  519 + usepnt = False
  520 +
524 521 # Compute spatial binning for point spread function and energy dispersion
525 522 # cubes
526 523 psf_binsz = 10.0 * binsz
... ... @@ -529,63 +526,69 @@ def get_stacked_response(obs, xref, yref, binsz=0.05, nxpix=200, nypix=200,
529 526  
530 527 # Create exposure cube
531 528 expcube = ctools.ctexpcube(obs)
532   - expcube['incube'] = 'NONE'
533   - expcube['usepnt'] = False
534   - expcube['ebinalg'] = 'LOG'
535   - expcube['xref'] = xref
536   - expcube['yref'] = yref
537   - expcube['binsz'] = binsz
538   - expcube['nxpix'] = nxpix
539   - expcube['nypix'] = nypix
540   - expcube['enumbins'] = enumbins
541   - expcube['emin'] = emin
542   - expcube['emax'] = emax
543   - expcube['coordsys'] = coordsys
544   - expcube['proj'] = proj
545   - expcube['debug'] = debug
546   - expcube['chatter'] = chatter
  529 + expcube['incube'] = 'NONE'
  530 + expcube['usepnt'] = usepnt
  531 + expcube['ebinalg'] = 'LOG'
  532 + expcube['binsz'] = binsz
  533 + expcube['nxpix'] = nxpix
  534 + expcube['nypix'] = nypix
  535 + expcube['enumbins'] = enumbins
  536 + expcube['emin'] = emin
  537 + expcube['emax'] = emax
  538 + expcube['coordsys'] = coordsys
  539 + expcube['proj'] = proj
  540 + expcube['addbounds'] = addbounds
  541 + expcube['debug'] = debug
  542 + expcube['chatter'] = chatter
  543 + if not usepnt:
  544 + expcube['xref'] = xref
  545 + expcube['yref'] = yref
547 546 if log:
548 547 expcube.logFileOpen()
549 548 expcube.run()
550 549  
551 550 # Create point spread function cube
552 551 psfcube = ctools.ctpsfcube(obs)
553   - psfcube['incube'] = 'NONE'
554   - psfcube['usepnt'] = False
555   - psfcube['ebinalg'] = 'LOG'
556   - psfcube['xref'] = xref
557   - psfcube['yref'] = yref
558   - psfcube['binsz'] = psf_binsz
559   - psfcube['nxpix'] = psf_nxpix
560   - psfcube['nypix'] = psf_nypix
561   - psfcube['enumbins'] = enumbins
562   - psfcube['emin'] = emin
563   - psfcube['emax'] = emax
564   - psfcube['coordsys'] = coordsys
565   - psfcube['proj'] = proj
566   - psfcube['debug'] = debug
567   - psfcube['chatter'] = chatter
  552 + psfcube['incube'] = 'NONE'
  553 + psfcube['usepnt'] = usepnt
  554 + psfcube['ebinalg'] = 'LOG'
  555 + psfcube['binsz'] = psf_binsz
  556 + psfcube['nxpix'] = psf_nxpix
  557 + psfcube['nypix'] = psf_nypix
  558 + psfcube['enumbins'] = enumbins
  559 + psfcube['emin'] = emin
  560 + psfcube['emax'] = emax
  561 + psfcube['coordsys'] = coordsys
  562 + psfcube['proj'] = proj
  563 + psfcube['addbounds'] = addbounds
  564 + psfcube['debug'] = debug
  565 + psfcube['chatter'] = chatter
  566 + if not usepnt:
  567 + psfcube['xref'] = xref
  568 + psfcube['yref'] = yref
568 569 if log:
569 570 psfcube.logFileOpen()
570 571 psfcube.run()
571 572  
572 573 # Create background cube
573 574 bkgcube = ctools.ctbkgcube(obs)
574   - bkgcube['incube'] = 'NONE'
575   - bkgcube['usepnt'] = False
576   - bkgcube['ebinalg'] = 'LOG'
577   - bkgcube['xref'] = xref
578   - bkgcube['yref'] = yref
579   - bkgcube['binsz'] = binsz
580   - bkgcube['nxpix'] = nxpix
581   - bkgcube['nypix'] = nypix
582   - bkgcube['enumbins'] = enumbins
583   - bkgcube['emin'] = emin
584   - bkgcube['emax'] = emax
585   - bkgcube['coordsys'] = coordsys
586   - bkgcube['proj'] = proj
587   - bkgcube['debug'] = debug
588   - bkgcube['chatter'] = chatter
  575 + bkgcube['incube'] = 'NONE'
  576 + bkgcube['usepnt'] = usepnt
  577 + bkgcube['ebinalg'] = 'LOG'
  578 + bkgcube['binsz'] = binsz
  579 + bkgcube['nxpix'] = nxpix
  580 + bkgcube['nypix'] = nypix
  581 + bkgcube['enumbins'] = enumbins
  582 + bkgcube['emin'] = emin
  583 + bkgcube['emax'] = emax
  584 + bkgcube['coordsys'] = coordsys
  585 + bkgcube['proj'] = proj
  586 + bkgcube['addbounds'] = addbounds
  587 + bkgcube['debug'] = debug
  588 + bkgcube['chatter'] = chatter
  589 + if not usepnt:
  590 + bkgcube['xref'] = xref
  591 + bkgcube['yref'] = yref
589 592 if log:
590 593 bkgcube.logFileOpen()
591 594 bkgcube.run()
... ... @@ -593,21 +596,23 @@ def get_stacked_response(obs, xref, yref, binsz=0.05, nxpix=200, nypix=200,
593 596 # If energy dispersion is requested then create energy dispersion cube
594 597 if edisp:
595 598 edispcube = ctools.ctedispcube(obs)
596   - edispcube['incube'] = 'NONE'
597   - edispcube['usepnt'] = False
598   - edispcube['ebinalg'] = 'LOG'
599   - edispcube['xref'] = xref
600   - edispcube['yref'] = yref
601   - edispcube['binsz'] = psf_binsz
602   - edispcube['nxpix'] = psf_nxpix
603   - edispcube['nypix'] = psf_nypix
604   - edispcube['enumbins'] = enumbins
605   - edispcube['emin'] = emin
606   - edispcube['emax'] = emax
607   - edispcube['coordsys'] = coordsys
608   - edispcube['proj'] = proj
609   - edispcube['debug'] = debug
610   - edispcube['chatter'] = chatter
  599 + edispcube['incube'] = 'NONE'
  600 + edispcube['usepnt'] = usepnt
  601 + edispcube['ebinalg'] = 'LOG'
  602 + edispcube['binsz'] = psf_binsz
  603 + edispcube['nxpix'] = psf_nxpix
  604 + edispcube['nypix'] = psf_nypix
  605 + edispcube['enumbins'] = enumbins
  606 + edispcube['emin'] = emin
  607 + edispcube['emax'] = emax
  608 + edispcube['coordsys'] = coordsys
  609 + edispcube['proj'] = proj
  610 + edispcube['addbounds'] = addbounds
  611 + edispcube['debug'] = debug
  612 + edispcube['chatter'] = chatter
  613 + if not usepnt:
  614 + edispcube['xref'] = xref
  615 + edispcube['yref'] = yref
611 616 if log:
612 617 edispcube.logFileOpen()
613 618 edispcube.run()
... ...