obsutils.py
10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
# ==========================================================================
# This script provides a number of functions that are useful for handling
# CTA observations.
#
# Copyright (C) 2011-2014 Juergen Knoedlseder
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# ==========================================================================
import ctools
import gammalib
# ===================== #
# Simulate observations #
# ===================== #
def sim(obs, log=False, debug=False, edisp=False, seed=0, nbins=0,
binsz=0.05, npix=200):
"""
Simulate events for all observations in the container.
Parameters:
obs - Observation container
Keywords:
log - Create log file(s)
debug - Create console dump?
edisp - Apply energy dispersion?
seed - Seed value for simulations (default: 0)
nbins - Number of energy bins (default: 0=unbinned)
binsz - Pixel size for binned simulation (deg/pixel)
npix - Number of pixels in X and Y for binned simulation
"""
# Allocate ctobssim application and set parameters
sim = ctools.ctobssim(obs)
sim['seed'].integer(seed)
# Optionally open the log file
if log:
sim.logFileOpen()
# Optionally switch-on debugging model
if debug:
sim["debug"].boolean(True)
# Optionally apply energy dispersion
sim["edisp"].boolean(edisp)
# Run ctobssim application. This will loop over all observations in the
# container and simulation the events for each observation. Note that
# events are not added together, they still apply to each observation
# separately.
sim.run()
# Binned option?
if nbins > 0:
# Determine common energy boundaries for observations
emin = None
emax = None
for run in sim.obs():
run_emin = run.events().ebounds().emin().TeV()
run_emax = run.events().ebounds().emax().TeV()
if emin == None:
emin = run_emin
elif run_emin > emin:
emin = run_emin
if emax == None:
emax = run_emax
elif run_emax > emax:
emax = run_emax
# Allocate ctbin application and set parameters
bin = ctools.ctbin(sim.obs())
bin["ebinalg"].string("LOG")
bin["emin"].real(emin)
bin["emax"].real(emax)
bin["enumbins"].integer(nbins)
bin["usepnt"].boolean(True) # Use pointing for map centre
bin["nxpix"].integer(npix)
bin["nypix"].integer(npix)
bin["binsz"].real(binsz)
bin["coordsys"].string("GAL")
bin["proj"].string("TAN")
# Optionally open the log file
if log:
bin.logFileOpen()
# Optionally switch-on debugging model
if debug:
bin["debug"].boolean(True)
# Run ctbin application. This will loop over all observations in
# the container and bin the events in counts maps
bin.run()
# Make a deep copy of the observation that will be returned
# (the ctbin object will go out of scope one the function is
# left)
obs = bin.obs().copy()
else:
# Make a deep copy of the observation that will be returned
# (the ctobssim object will go out of scope one the function is
# left)
obs = sim.obs().copy()
# Delete the simulation
del sim
# Return observation container
return obs
# ================ #
# Fit observations #
# ================ #
def fit(obs, log=False, debug=False, edisp=False):
"""
Perform maximum likelihood fitting of observations in the container.
Parameters:
obs - Observation container
Keywords:
log - Create log file(s)
debug - Create screen dump
edisp - Apply energy dispersion?
"""
# Allocate ctlike application
like = ctools.ctlike(obs)
# Optionally open the log file
if log:
like.logFileOpen()
# Optionally switch-on debugging model
if debug:
like["debug"].boolean(True)
# Optionally apply energy dispersion
like["edisp"].boolean(edisp)
# Run ctlike application.
like.run()
# Return observations
return like
# ================= #
# Create counts map #
# ================= #
def cntmap(obs, proj="TAN", coord="GAL", xval=0.0, yval=0.0, \
binsz=0.05, nxpix=200, nypix=200, \
outname="cntmap.fits"):
"""
Creates a counts map by combining the events of all observations.
The counts map will be a summed map over all energies.
Parameters:
obs - Observation container
Keywords:
proj - Projection type (e.g. TAN, CAR, STG, ...) (default: TAN)
coord - Coordinate type (GAL, CEL) (default: GAL)
xval - Reference longitude value [deg] (default: 0.0)
yval - Reference latitude value [deg] (default: 0.0)
binsz - Pixel size [deg/pixel] (default: 0.05)
nxpix - Number of pixels in X direction (default: 200)
nypix - Number of pixels in Y direction (default: 200)
outname - Counts map FITS filename (default: cntmap.fits)
"""
# Allocate counts map
map = gammalib.GSkymap(proj, coord, xval, yval, -binsz, binsz, nxpix, nypix, 1)
# Set maximum pixel number
maxpixel = nxpix * nypix
# Fill all observations
for run in obs:
# Loop over all events
for event in run.events():
# Determine sky pixel
skydir = gammalib.GCTAInstDir(event.dir()).dir()
pixel = map.dir2inx(skydir)
# Set pixel
if pixel < maxpixel:
map[pixel] += 1.0
# Save sky map. The clobber flag is set to True, so any existing FITS
# file will be overwritten.
map.save(outname, True)
# Return counts map
return map
# ================ #
# Create model map #
# ================ #
def modmap(obs, eref=0.1, proj="TAN", coord="GAL", xval=0.0, yval=0.0, \
binsz=0.05, nxpix=200, nypix=200, \
outname="modmap.fits"):
"""
Make model map for a given reference energy by combining all observations.
The model map will be evaluated for a given reference energy 'eref' and will
be given in units of [counts/(sr MeV s)].
Parameters:
obs - Observation container
Keywords:
eref - Reference energy for which model is created [TeV] (default: 0.1)
proj - Projection type (e.g. TAN, CAR, STG, ...) (default: TAN)
coord - Coordinate type (GAL, CEL) (default: GAL)
xval - Reference longitude value [deg] (default: 0.0)
yval - Reference latitude value [deg] (default: 0.0)
binsz - Pixel size [deg/pixel] (default: 0.05)
nxpix - Number of pixels in X direction (default: 200)
nypix - Number of pixels in Y direction (default: 200)
outname - Model map FITS filename (default: modmap.fits)
"""
# Allocate model map
map = gammalib.GSkymap(proj, coord, xval, yval, -binsz, binsz, nxpix, nypix, 1)
# Set reference energy, time and direction. The time is not initialised and is
# in fact not used (as the IRF is assumed to be time independent for now).
# The sky direction is set later using the pixel values.
energy = gammalib.GEnergy()
time = gammalib.GTime()
instdir = gammalib.GCTAInstDir()
energy.TeV(eref)
# Loop over all map pixels
for pixel in range(map.npix()):
# Get sky direction
skydir = map.inx2dir(pixel)
instdir.dir(skydir)
# Create event atom for map pixel
atom = gammalib.GCTAEventAtom()
atom.dir(instdir)
atom.energy(energy)
atom.time(time)
# Initialise model value
value = 0.0
# Loop over all observations
for run in obs:
value += obs.models().eval(atom, run)
# Set map value
map[pixel] = value
# Save sky map
map.save(outname, True)
# Return model map
return map
# ======================= #
# Set one CTA observation #
# ======================= #
def set(pntdir, tstart=0.0, duration=1800.0, deadc=0.95, \
emin=0.1, emax=100.0, rad=5.0, \
irf="cta_dummy_irf", caldb="$CTOOLS/share/caldb/data/cta/dummy"):
"""
Returns a single CTA observation. By looping over this function we can
add CTA observations to the observation container.
Parameters:
pntdir - Pointing direction
Keywords:
tstart - Start time [seconds] (default: 0.0)
duration - Duration of observation [seconds] (default: 1800.0)
deadc - Deadtime correction factor (default: 0.95)
emin - Minimum event energy [TeV] (default: 0.1)
emax - Maximum event energy [TeV] (default: 100.0)
rad - ROI radius used for analysis [deg] (default: 5.0)
irf - Instrument response function (default: cta_dummy_irf)
caldb - Calibration database path (default: "")
"""
# Allocate CTA observation
obs = gammalib.GCTAObservation()
# Set calibration database
db = gammalib.GCaldb()
if (gammalib.dir_exists(caldb)):
db.rootdir(caldb)
else:
db.open("cta", caldb)
# Set pointing direction
pnt = gammalib.GCTAPointing()
pnt.dir(pntdir)
obs.pointing(pnt)
# Set ROI
roi = gammalib.GCTARoi()
instdir = gammalib.GCTAInstDir()
instdir.dir(pntdir)
roi.centre(instdir)
roi.radius(rad)
# Set GTI
gti = gammalib.GGti()
start = gammalib.GTime(tstart)
stop = gammalib.GTime(tstart+duration)
gti.append(start, stop)
# Set energy boundaries
ebounds = gammalib.GEbounds()
e_min = gammalib.GEnergy()
e_max = gammalib.GEnergy()
e_min.TeV(emin)
e_max.TeV(emax)
ebounds.append(e_min, e_max)
# Allocate event list
events = gammalib.GCTAEventList()
events.roi(roi)
events.gti(gti)
events.ebounds(ebounds)
obs.events(events)
# Set instrument response
obs.response(irf, db)
# Set ontime, livetime, and deadtime correction factor
obs.ontime(duration)
obs.livetime(duration*deadc)
obs.deadc(deadc)
# Return observation
return obs