GCTASupport.cpp
16.3 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
/***************************************************************************
* GCTASupport.cpp - CTA support functions *
* ----------------------------------------------------------------------- *
* copyright (C) 2011-2017 by 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/>. *
* *
***************************************************************************/
/**
* @file GCTASupport.cpp
* @brief Implementation of support function used by CTA classes
* @author Juergen Knoedlseder
*/
/* __ Includes ___________________________________________________________ */
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <cmath>
#include <iostream>
#include <string>
#include "GCTASupport.hpp"
#include "GTools.hpp"
#include "GMath.hpp"
#include "GFitsHDU.hpp"
#include "GEbounds.hpp"
#include "GPhases.hpp"
#include "GCTARoi.hpp"
#include "GCTAInstDir.hpp"
/* __ Method name definitions ____________________________________________ */
#define G_READ_DS_ROI "gammalib::read_ds_roi(GFitsHDU&)"
#define G_READ_DS_EBOUNDS "gammalib::read_ds_ebounds(GFitsHDU&)"
#define G_READ_DS_PHASE "gammalib::read_ds_phase(GFitsHDU&)"
#define G_READ_DS_GTI "gammalib::read_ds_gti_extname(GFitsHDU&)"
/* __ Coding definitions _________________________________________________ */
/* __ Debug definitions __________________________________________________ */
#define G_CHECK_FOR_NAN 0
/***********************************************************************//**
* @brief Returns length of circular arc within circular ROI
*
* @param[in] rad Circle radius in radians (<pi).
* @param[in] dist Circle centre distance to ROI centre (<pi).
* @param[in] cosdist Cosine of circle centre distance to ROI centre.
* @param[in] sindist Sinus of circle centre distance to ROI centre.
* @param[in] roi Radius of ROI in radians.
* @param[in] cosroi Cosine of ROI radius.
*
* This method returns the arclength in radians of a circle of radius 'rad'
* with a centre that is offset by 'dist' from the ROI centre, where the ROI
* radius is given by 'roi'. To speed-up computations, the cosines and sinus
* of 'roi' and 'psf' should be calculated by the client and be passed to
* the method.
***************************************************************************/
double gammalib::cta_roi_arclength(const double& rad, const double& dist,
const double& cosdist, const double& sindist,
const double& roi, const double& cosroi)
{
// Declare arclength
double arclength;
// Handle special case that circle centre matches ROI centre
if (dist == 0.0) {
if (rad > roi) arclength = 0.0; // Circle outside ROI
else arclength = gammalib::twopi; // Circle inside ROI
}
// ... otherwise circle and ROI centres are not identical
else {
// Handle special case that we evaluate exactly at the circle
// centre. In this case we have in fact a point, and if this point
// falls within the ROI it has a formal arclength of 2pi.
//
if (rad == 0.0) {
if (dist > roi) arclength = 0.0; // Circle centre outside ROI
else arclength = gammalib::twopi; // Circle centre inside ROI
}
// ... otherwise we have to handle the general case
else {
double d = roi - dist;
if (-rad >= d) {
arclength = 0.0;
}
else if (rad <= d) {
arclength = gammalib::twopi;
}
else {
double cosrad = std::cos(rad);
double sinrad = std::sin(rad);
double cosang = (cosroi - cosdist*cosrad) / (sindist*sinrad);
arclength = 2.0 * gammalib::acos(cosang);
#if G_CHECK_FOR_NAN
if (gammalib::is_infinite(arclength) ||
gammalib::is_notanumber(arclength)) {
std::cout << "cta_roi_arclength: NaN occured";
std::cout << " rad=" << rad;
std::cout << " sinrad=" << sinrad;
std::cout << " cosrad=" << cosrad;
std::cout << " sindist=" << sindist;
std::cout << " cosdist=" << cosdist;
std::cout << " cosang=" << cosang;
std::cout << std::endl;
}
#endif
}
}
} // endelse: Circle and ROI centres were not identical
// Return arclength
return arclength;
}
/***********************************************************************//**
* @brief Extract ROI from data sub-space keywords
*
* @param[in] hdu FITS HDU
*
* @exception GException::invalid_value
* Invalid ROI data sub-space encountered
*
* Reads the ROI data sub-space keywords by searching for a DSTYPx keyword
* named "POS(RA,DEC)". The data sub-space information is expected to be in
* the format "CIRCLE(267.0208,-24.78,4.5)", where the 3 arguments are Right
* Ascension, Declination and radius in units of degrees. No detailed syntax
* checking is performed.
*
* If no ROI information has been found, an GCTARoi object with initial
* values will be returned.
***************************************************************************/
GCTARoi gammalib::read_ds_roi(const GFitsHDU& hdu)
{
// Initialise ROI
GCTARoi roi;
// Get number of data sub-space keywords (default to 0 if keyword is
// not found)
int ndskeys = (hdu.has_card("NDSKEYS")) ? hdu.integer("NDSKEYS") : 0;
// Loop over all data selection keys
for (int i = 1; i <= ndskeys; ++i) {
// Set data sub-space key strings
std::string type_key = "DSTYP"+gammalib::str(i);
//std::string unit_key = "DSUNI"+gammalib::str(i);
std::string value_key = "DSVAL"+gammalib::str(i);
// Continue only if type_key is found and if this key is POS(RA,DEC)
if (hdu.has_card(type_key) && hdu.string(type_key) == "POS(RA,DEC)") {
// ...
//std::string unit = gammalib::toupper(hdu.string(unit_key));
std::string value = hdu.string(value_key);
std::string value_proc = gammalib::strip_chars(value, "CIRCLE(");
value_proc = gammalib::strip_chars(value_proc, ")");
std::vector<std::string> args = gammalib::split(value_proc, ",");
if (args.size() == 3) {
double ra = gammalib::todouble(args[0]);
double dec = gammalib::todouble(args[1]);
double rad = gammalib::todouble(args[2]);
GCTAInstDir dir;
dir.dir().radec_deg(ra, dec);
roi.centre(dir);
roi.radius(rad);
}
else {
std::string msg = "Invalid acceptance cone value \""+value+
"\" encountered in data sub-space "
"key \""+value_key+"\".";
throw GException::invalid_value(G_READ_DS_ROI, msg);
}
} // endif: POS(RA,DEC) type found
} // endfor: looped over data sub-space keys
// Return roi
return roi;
}
/***********************************************************************//**
* @brief Read energy boundary data sub-space keywords
*
* @param[in] hdu FITS HDU
*
* @exception GException::invalid_value
* Invalid energy data sub-space encountered
*
* Reads the energy boundary data sub-space keywords by searching for a
* DSTYPx keyword named "ENERGY". The data sub-space information is expected
* to be in the format "200:50000", where the 2 arguments are the minimum
* and maximum energy. The energy unit is given by the keyword DSUNIx, which
* supports keV, MeV, GeV and TeV (case independent). No detailed syntax
* checking is performed.
***************************************************************************/
GEbounds gammalib::read_ds_ebounds(const GFitsHDU& hdu)
{
// Initialise energy boundaries
GEbounds ebounds;
// Get number of data sub-space keywords (default to 0 if keyword is
// not found)
int ndskeys = (hdu.has_card("NDSKEYS")) ? hdu.integer("NDSKEYS") : 0;
// Loop over all data sub-space keys
for (int i = 1; i <= ndskeys; ++i) {
// Set data sub-space key strings
std::string type_key = "DSTYP"+gammalib::str(i);
std::string unit_key = "DSUNI"+gammalib::str(i);
std::string value_key = "DSVAL"+gammalib::str(i);
// Continue only if type_key is found and if this key is ENERGY
if (hdu.has_card(type_key) && hdu.string(type_key) == "ENERGY") {
// Extract energy boundaries
std::string value = hdu.string(value_key);
std::string unit = hdu.string(unit_key);
std::vector<std::string> values = gammalib::split(value, ":");
if (values.size() == 2) {
double emin = gammalib::todouble(values[0]);
double emax = gammalib::todouble(values[1]);
GEnergy e_min(emin, unit);
GEnergy e_max(emax, unit);
ebounds.append(e_min, e_max);
}
else {
std::string msg = "Invalid energy value \""+value+
"\" encountered in data selection key \""+
value_key+"\"";
throw GException::invalid_value(G_READ_DS_EBOUNDS, msg);
}
} // endif: ENERGY type_key found
} // endfor: looped over data selection keys
// Return
return ebounds;
}
/***********************************************************************//**
* @brief Read phase boundary data sub-space keywords
*
* @param[in] hdu FITS HDU
* @return Phase intervals
*
* @exception GException::invalid_value
* Invalid phase data sub-space encountered
*
* Reads the phase boundary data sub-space keywords by searching for a
* DSTYPx keyword named "PHASE". The data sub-space information is expected
* to be in the format "0.1:0.3,0.5:0.7", where each subset of numbers
* represents the minimum and maximum phase. No detailed syntax
* checking is performed.
***************************************************************************/
GPhases gammalib::read_ds_phase(const GFitsHDU& hdu)
{
// Initialise phase intervals
GPhases phases;
// Get number of data sub-space keywords (default to 0 if keyword is
// not found)
int ndskeys = (hdu.has_card("NDSKEYS")) ? hdu.integer("NDSKEYS") : 0;
// Loop over all data sub-space keys
for (int i = 1; i <= ndskeys; ++i) {
// Set data sub-space key strings
std::string type_key = "DSTYP"+gammalib::str(i);
std::string unit_key = "DSUNI"+gammalib::str(i);
std::string value_key = "DSVAL"+gammalib::str(i);
// Continue only if type_key is found and if this key is PHASE
if (hdu.has_card(type_key) && hdu.string(type_key) == "PHASE") {
// Extract phase boundaries
std::string value = hdu.string(value_key);
std::vector<std::string> intervals = gammalib::split(value, ",");
for (int j = 0; j < intervals.size(); ++j) {
std::vector<std::string> values = gammalib::split(intervals[j], ":");
if (values.size() == 2) {
double phmin = gammalib::todouble(values[0]);
double phmax = gammalib::todouble(values[1]);
phases.append(phmin, phmax);
}
else {
std::string msg = "Invalid phase value \""+value+
"\" encountered in data selection key \""+
value_key+"\"";
throw GException::invalid_value(G_READ_DS_PHASE, msg);
}
} //endfor: looped over phase intervals
} // endif: PHASE type_key found
} // endfor: looped over data selection keys
// Return
return phases;
}
/***********************************************************************//**
* @brief Return Good Time Intervals extension name from data sub-space keywords
*
* @param[in] hdu FITS HDU
* @return Good Time Interval extension name
*
* @exception GException::invalid_value
* Invalid Good Time Intervals data sub-space encountered
*
* Returns the name of the FITS extension that contains the Good Time
* Intervals by screening the data sub-space keywords that are present in
* the FITS header. The method searches for a DSTYPx keyword named "TIME"
* and a corresponding DSVALx keyword named "TABLE", and the extension name
* is extracted from the corresponding DSREFx keyword. Note that by
* convention the extension name is preceeded by a colon, which is stripped
* by this method.
***************************************************************************/
std::string gammalib::read_ds_gti_extname(const GFitsHDU& hdu)
{
// Initialise extension name
std::string extname;
// Get number of data sub-space keys (default to 0 if "NDSKEYS" keyword
// is not found)
int ndskeys = (hdu.has_card("NDSKEYS")) ? hdu.integer("NDSKEYS") : 0;
// Loop over all data sub-space keys
for (int i = 1; i <= ndskeys; ++i) {
// Set data sub-space key strings
std::string type_key = "DSTYP"+gammalib::str(i);
std::string unit_key = "DSUNI"+gammalib::str(i);
std::string value_key = "DSVAL"+gammalib::str(i);
std::string ref_key = "DSREF"+gammalib::str(i);
// Skip if DSTYPi keyword does not exist, or if it is not "TIME"
if (!hdu.has_card(type_key) || hdu.string(type_key) != "TIME") {
continue;
}
// If DSVALi keyword does not exist or if it's value is not
// "TABLE" then throw an exception
if (!hdu.has_card(value_key)) {
std::string msg = "Keyword \""+value_key+"\" missing for data "
"sub-space selection of type "+type_key+
"=\"TIME\". Please correct FITS header.";
throw GException::invalid_value(G_READ_DS_GTI, msg);
}
if (hdu.string(value_key) != "TABLE") {
std::string msg = "Cannot interpret keyword \""+value_key+"\" "
"value \""+hdu.string(value_key)+"\". Only "
"the value \"TABLE\" is supported.";
throw GException::invalid_value(G_READ_DS_GTI, msg);
}
// If DSREFi keyword does not exist then throw an exception
if (!hdu.has_card(ref_key)) {
std::string msg = "Keyword \""+ref_key+"\" missing for data "
"sub-space selection of type "+type_key+
"=\"TIME\". Please correct FITS header.";
throw GException::invalid_value(G_READ_DS_GTI, msg);
}
// Get extension name (strip any leading and trailing colons)
extname = gammalib::strip_whitespace(gammalib::strip_chars(hdu.string(ref_key), ":"));
} // endfor: looped over data sub-space keywords
// Return
return extname;
}