csobs2caldb.py
13.5 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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
#!/usr/bin/env python
# ==========================================================================
# Creates a calibration database entry for an IACT observation
#
# Copyright (C) 2015-2017 Michael Mayer
#
# 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 sys
import os
import datetime
import gammalib
import ctools
from cscripts import calutils
# ================= #
# csobs2caldb class #
# ================= #
class csobs2caldb(ctools.cscript):
"""
Creates a calibration database entry for an IACT observation.
The creation of a calibration database entry is useful for performing
simulations for current Imaging Air Cherenkov Telescopes (IACTs).
The class takes an observation definition XML file as input and uses
one observation to create a calibration database entry. By default
the first observation will be used, but the user can specify the
index of any observation using the hidden "index" parameter.
"""
# Constructor
def __init__(self, *argv):
"""
Constructor
"""
# Set name and version
self._name = 'csobs2caldb'
self._version = ctools.__version__
# Initialise members
self._observation = gammalib.GCTAObservation()
self._mission = 'cta'
self._caldb = 'cta'
self._outfile = gammalib.GFilename('irf_file.fits')
self._base_dir = ''
self._cal_dir = ''
self._rsp_dir = ''
self._caldb_inx = gammalib.GFits()
self._irf_fits = gammalib.GFits()
# Initialise observation container from constructor arguments.
self._obs, argv = self._set_input_obs(argv)
# Initialise script by calling the appropriate class constructor.
self._init_cscript(argv)
# Return
return
# Private methods
def _get_parameters(self):
"""
Get parameters from parfile
Raises:
ValueError, IndexError, & RuntimeError.
"""
# Load observation container from "inobs" parameter in case that
# the observation container is still empty.
if self._obs.is_empty():
self._obs = gammalib.GObservations(self['inobs'].filename())
# Raise an exception if the observation container is empty
if self._obs.is_empty():
raise ValueError('No or empty observation provided for '
'parameter "inobs".')
# Get index of observation to be used
index = self['index'].integer()
# Raise an exception if the index is not valid
if index < 0 or index >= self._obs.size():
raise IndexError('Parameter "index=%d" outside the validity '
'range [0,%d].' % (index, self._obs.size()))
# Get observation
self._observation = self._obs[index]
# Make sure we have a CTA observation
if not self._observation.classname() == 'GCTAObservation':
raise RuntimeError('Input observation not of type '
'"GCTAObservation".')
# Make sure we have an IRF response associated with the CTA
# observation
if not self._observation.response().classname() == 'GCTAResponseIrf':
raise RuntimeError('Response of input observation not of '
'type "GCTAResponseIrf".')
# Get calibration database name. If the "caldb" parameter is "NONE"
# or empty then use the instrument name from the observation as
# calibration database name.
self._caldb = self['caldb'].string()
if (self._caldb == 'NONE' or len(self._caldb) == 0):
self._caldb = self._observation.instrument().lower()
# Get instrument response function file name
self._outfile = self['outfile'].filename()
# Make sure that remaining user parameters are queried now. We
# do not store the actual parameter values as we do not want
# too many instance attributes with enhances the maintenance
# costs.
self['irf'].string()
self['rootdir'].string()
# Write input parameters into logger
self._log_parameters(gammalib.TERSE)
# Return
return
def _make_irf_file(self):
"""
Creates an IRF FITS file
"""
# Write header into logger
if self._logTerse():
self._log("\n")
self._log.header2("Creating IRF file")
# Get response for the observation
rsp = self._observation.response()
# Extract response file names
fname_aeff = rsp.aeff().filename()
fname_psf = rsp.psf().filename()
fname_edisp = rsp.edisp().filename()
fname_bkg = rsp.background().filename()
# Log filenames
self._log_header3(gammalib.NORMAL, 'IRF input files')
self._log_value(gammalib.NORMAL, 'Effective area', fname_aeff.url())
self._log_value(gammalib.NORMAL, 'Point spread function', fname_psf.url())
self._log_value(gammalib.NORMAL, 'Energy dispersion', fname_edisp.url())
self._log_value(gammalib.NORMAL, 'Background rate', fname_bkg.url())
# Open FITS files of response components
fits_aeff = gammalib.GFits(fname_aeff)
fits_psf = gammalib.GFits(fname_psf)
fits_edisp = gammalib.GFits(fname_edisp)
fits_bkg = gammalib.GFits(fname_bkg)
# Get extension names
ext_aeff = fname_aeff.extname("EFFECTIVE AREA")
ext_psf = fname_psf.extname("POINT SPREAD FUNCTION")
ext_edisp = fname_edisp.extname("ENERGY DISPERSION")
ext_bkg = fname_bkg.extname("BACKGROUND")
# Create empty FITS file
fits = gammalib.GFits()
# Append IRF component to FITS file
fits.append(fits_aeff[ext_aeff])
fits.append(fits_psf[ext_psf])
fits.append(fits_edisp[ext_edisp])
fits.append(fits_bkg[ext_bkg])
# Log resulting FITS file
if self._logNormal():
self._log(str(fits))
self._log("\n")
if self._logExplicit():
self._log(str(fits[ext_aeff].header()))
self._log("\n")
self._log(str(fits[ext_psf].header()))
self._log("\n")
self._log(str(fits[ext_edisp].header()))
self._log("\n")
self._log(str(fits[ext_bkg].header()))
self._log("\n")
# Return fits file
return fits
def _make_caldb_index(self):
"""
Creates an IRF index FITS file
"""
# Write header into logger
if self._logTerse():
self._log('\n')
self._log.header2('Creating database index file')
# Open calibration database index
indx_file = self._base_dir + '/caldb.indx'
# Open index file (or create one if it does not exist)
cif = gammalib.GFits(indx_file, True)
# Retrieve "CIF" table
if cif.contains('CIF'):
table = cif.table('CIF')
# ... or create binary table if no "CIF" table exists yet, append
# binary table to CIF file and return a reference to the appended
# table to work with
else:
table = cif.append(calutils.create_cif_table())
# Check if output config already exist
has_config = False
row_index = -1
for caldir in table['CAL_DIR']:
row_index += 1
if caldir == self._cal_dir:
has_config = True
break
# Create columns if not available
if not has_config:
# Append 4 rows to CIF extension
table.append_rows(4)
row_index = table.nrows()
else:
row_index += 4
# Add generic information for these 4 rows
for i in range(4):
# Set row number
row = i + row_index-4
# Set date
now = str(datetime.datetime.now())
today, time = now.split()
# Set element
table['TELESCOP'][row] = self._mission
table['INSTRUME'][row] = self._caldb
table['DETNAM'][row] = 'NONE'
table['FILTER'][row] = 'NONE'
table['CAL_DEV'][row] = 'ONLINE'
table['CAL_CLAS'][row] = 'BCF'
table['CAL_DTYP'][row] = 'DATA'
table['CAL_VSD'][row] = today
table['CAL_VST'][row] = time.split('.')[0]
table['REF_TIME'][row] = 51544.0
table['CAL_QUAL'][row] = 0
table['CAL_CBD'][row] = 'NAME('+self['irf'].string()+')'
table['CAL_DATE'][row] = today.replace('-','/')[2:]
table['CAL_DIR'][row] = self._cal_dir
table['CAL_FILE'][row] = self._outfile
# Add effective area information
row = row_index-4
table['CAL_CNAM'][row] = 'EFF_AREA'
table['CAL_DESC'][row] = self._caldb+' effective area'
# Add point spread function information
row = row_index-3
table['CAL_CNAM'][row] = 'RPSF'
table['CAL_DESC'][row] = self._caldb+' point spread function'
# Add energy dispersion information
row = row_index-2
table['CAL_CNAM'][row] = 'EDISP'
table['CAL_DESC'][row] = self._caldb+' energy dispersion'
# Add background information
row = row_index-1
table['CAL_CNAM'][row] = 'BGD'
table['CAL_DESC'][row] = self._caldb+' background'
# Log resulting FITS table and header
if self._logNormal():
self._log(str(table))
self._log('\n')
if self._logExplicit():
self._log(str(table.header()))
self._log('\n')
# Return CIF FITS file
return cif
def _make_dirs(self):
"""
Make CALDB directories
"""
# Write header into logger
if self._logTerse():
self._log("\n")
self._log.header2("Creating directory structure")
# Create calibration database
caldb = gammalib.GCaldb(self["rootdir"].string())
# Set calibration directory
self._cal_dir = "data"
self._cal_dir += "/"+self._mission.lower()
self._cal_dir += "/"+self._caldb.lower()
self._cal_dir += "/bcf/"+self["irf"].string()
# Set absolute path
self._base_dir = caldb.rootdir() +"/data"
self._base_dir += "/"+self._mission.lower()
self._base_dir += "/"+self._caldb.lower()
# Set directory for irf file
self._rsp_dir = caldb.rootdir() + "/" + self._cal_dir
# Log resulting FITS table
self._log_value(gammalib.NORMAL, 'Calibration directory', self._cal_dir)
self._log_value(gammalib.NORMAL, 'Base directory', self._base_dir)
if not os.path.isdir(self._rsp_dir):
name = 'IRF directory'
else:
name = 'IRF directory (existing)'
self._log_value(gammalib.NORMAL, name, self._rsp_dir)
# Create IRF directory is it does not yet exist
if not os.path.isdir(self._rsp_dir):
os.makedirs(self._rsp_dir)
# Return
return
# Public methods
def run(self):
"""
Run the script
"""
# Switch screen logging on in debug mode
if self._logDebug():
self._log.cout(True)
# Get parameters
self._get_parameters()
# Write input parameters and header into logger
if self._logTerse():
self._log('\n')
self._log.header1('Creating CALDB entry')
# Create directory structure
self._make_dirs()
# Create/update calibration database
self._caldb_inx = self._make_caldb_index()
# Create response file
self._irf_fits = self._make_irf_file()
# Return
return
def save(self):
"""
Save calibration database FITS file
"""
# Write header into logger
self._log_header1(gammalib.TERSE, 'Save calibration database')
# Set response filename
filename = self._rsp_dir + '/' + self._outfile
# Write filenames into logger
self._log_value(gammalib.NORMAL, 'CALDB index file',
self._caldb_inx.filename().url())
self._log_value(gammalib.NORMAL, 'Response file', filename)
# Save caldb index file
self._caldb_inx.save(self._clobber())
# Save response file
self._irf_fits.saveto(filename, self._clobber())
# Return
return
# ======================== #
# Main routine entry point #
# ======================== #
if __name__ == '__main__':
# Create instance of application
app = csobs2caldb(sys.argv)
# Execute application
app.execute()