ctool.i
12.4 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
/***************************************************************************
* ctool - ctool base class *
* ----------------------------------------------------------------------- *
* copyright (C) 2014-2018 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 ctool.hpp
* @brief ctool base class definition
* @author Juergen Knoedlseder
*/
%{
/* Put headers and other declarations here that are needed for compilation */
#define SWIG
#include "ctool.hpp"
/***********************************************************************//**
* @class cscript
*
* @brief cscript base class
***************************************************************************/
class cscript : public ctool {
public:
// Constructors and destructors
cscript(const std::string& name, const std::string& version) : ctool(name, version) {}
cscript(const std::string& name, const std::string& version,
int argc, char* argv[]) : ctool(name, version, argc, argv) {}
cscript(const ctool& app) : ctool(app) {}
virtual ~cscript(void) {}
// Dummy methods (implementation makes this class non-abstract)
virtual void clear(void) {
}
virtual void run(void) {
}
virtual void save(void) {
}
};
%}
// Include (int ARGC, char **ARGV) typemap to allow passing command line
// arguments to GApplication constructor
%include "argcargv.i"
/* __ Constants __________________________________________________________ */
/***********************************************************************//**
* @class ctool
*
* @brief Base class for ctools
***************************************************************************/
class ctool : public GApplication {
public:
// Constructors and destructors
ctool(const std::string& name, const std::string& version);
ctool(const std::string& name, const std::string& version,
int ARGC, char **ARGV);
ctool(const ctool& app);
virtual ~ctool(void);
// Pure virtual methods
virtual void clear(void) = 0;
virtual void run(void) = 0;
virtual void save(void) = 0;
// Public methods
virtual void execute(void);
// Make methods private in Python by prepending an underscore
%rename(_read_ahead) read_ahead;
%rename(_get_observations) get_observations;
%rename(_setup_observations) setup_observations;
%rename(_setup_models) setup_models;
%rename(_create_ebounds) create_ebounds;
%rename(_create_map) create_map;
%rename(_create_cube) create_cube;
%rename(_create_cta_obs) create_cta_obs;
%rename(_require_inobs) require_inobs;
%rename(_require_inobs_nocube) require_inobs_nocube;
%rename(_get_roi) get_roi;
%rename(_get_ebounds) get_ebounds;
%rename(_get_gti) get_gti;
%rename(_get_pointing) get_pointing;
%rename(_get_skydir) get_skydir;
%rename(_set_outfile_name) set_outfile_name;
%rename(_is_stacked) is_stacked;
%rename(_is_onoff) is_onoff;
%rename(_set_response) set_response;
%rename(_set_edisp) set_edisp;
%rename(_restore_edisp) restore_edisp;
%rename(_set_obs_response) set_obs_response;
%rename(_set_obs_bounds) set_obs_bounds;
%rename(_get_mean_pointing) get_mean_pointing;
%rename(_get_current_rss) get_current_rss;
%rename(_get_obs_header) get_obs_header;
%rename(_insert_energy_boundaries) insert_energy_boundaries;
%rename(_cube_layer_usage) cube_layer_usage;
%rename(_get_gtiname) get_gtiname;
%rename(_save_event_list) save_event_list;
%rename(_warn_too_few_energies) warn_too_few_energies;
%rename(_warn_xml_suffix) warn_xml_suffix;
// Protected methods
const bool& read_ahead(void) const;
// Protected high-level setup methods
void setup_observations(GObservations& obs, const bool& response = true,
const bool& list = true,
const bool& cube = true);
void setup_models(GObservations& obs, const std::string& name = "");
// Protected methods that create objects from user parameters
GEbounds create_ebounds(void);
GSkyMap create_map(const GObservations& obs);
GCTAEventCube create_cube(const GObservations& obs);
GCTAObservation create_cta_obs(void);
// Protected methods that check user parameters
void require_inobs(const std::string& method);
void require_inobs_nocube(const std::string& method);
// Protected methods that extract user parameters
GCTARoi get_roi(const GCTAPointing& pnt = GCTAPointing());
GEbounds get_ebounds(void);
GGti get_gti(const GTimeReference& ref);
GCTAPointing get_pointing(void);
GSkyDir get_skydir(void);
std::string set_outfile_name(const std::string& filename);
// Protected methods that query user parameters
bool is_stacked(void);
bool is_onoff(void);
// Protected support methods
void set_response(GObservations& obs);
std::vector<bool> set_edisp(GObservations& obs, const bool& edisp) const;
void restore_edisp(GObservations& obs,
const std::vector<bool>& edisp) const;
void set_obs_response(GCTAObservation* obs);
void set_obs_bounds(GObservations& obs);
GObservations get_observations(const bool& get_response = true);
GSkyDir get_mean_pointing(const GObservations& obs);
size_t get_current_rss(void);
std::string get_obs_header(const GObservation* obs) const;
GEnergies insert_energy_boundaries(const GEnergies& energies,
const GCTAObservation& obs);
std::vector<bool> cube_layer_usage(const GEbounds& cube_ebounds,
const GEbounds& list_ebounds) const;
std::string get_gtiname(const std::string& filename,
const std::string& evtname) const;
void save_event_list(const GCTAObservation* obs,
const std::string& infile,
const std::string& evtname,
const std::string& gtiname,
const std::string& outfile) const;
// Protected warning strings
std::string warn_too_few_energies(const GEnergies& energies) const;
std::string warn_xml_suffix(const GFilename& filename) const;
};
/***********************************************************************//**
* @class cscript
*
* @brief cscript base class
*
* This is the base class from which all cscripts should derive. This
* enables using of ctool base class methods for generic parameter
* handling.
***************************************************************************/
class cscript : public ctool {
public:
// Constructors and destructors
cscript(const std::string& name, const std::string& version);
cscript(const std::string& name, const std::string& version,
int ARGC, char **ARGV);
cscript(const ctool& app);
virtual ~cscript(void);
// Methods
virtual void clear(void);
virtual void run(void);
virtual void save(void);
};
/***********************************************************************//**
* @brief ctool base class Python extensions
***************************************************************************/
%extend ctool {
void _log_observations(const int& chatter,
const GObservations& obs,
const std::string& what = "Observation") {
self->log_observations(GChatter(chatter), obs, what);
}
void _log_models(const int& chatter,
const GModels& models,
const std::string& what = "Model") {
self->log_models(GChatter(chatter), models, what);
}
void _read_ahead(const bool& flag) {
self->m_read_ahead = flag;
return;
}
}
/***********************************************************************//**
* @brief ctool and cscript base class Python extensions
***************************************************************************/
%pythoncode %{
# Initialise observation container from constructor arguments. In case that
# an observation container is provided as argument, this container will be
# used to initialise the class. The function returns a tuple containing the
# observation container and the eventually reduced argument list.
def _set_input_obs(self, argv):
if len(argv) > 0 and isinstance(argv[0],gammalib.GObservations):
obs = argv[0]
argv = argv[1:]
else:
obs = gammalib.GObservations()
return (obs, argv)
cscript._set_input_obs = _set_input_obs
# Initialise application by calling the appropriate class constructor
def _init_cscript(self, name, version, argv):
if len(argv) == 0:
cscript.__init__(self, name, version)
elif len(argv) == 1:
cscript.__init__(self, name, version, *argv)
else:
raise TypeError('Invalid number of arguments given.')
# Set logger properties
self._log_header()
self._log.date(True)
cscript._init_cscript = _init_cscript
# This function either returns or assigns user parameters in form of a
# dictionary. The assignment function takes a dictionary as argument and
# the function then loops over all keys in that dictionary and assigns
# the value to the parameter. For example
#
# >>> tool.pardict({'ra': 83.63, 'dec': 22.01})
#
# is equivalent to
#
# >>> tool['ra'] = 83.63
# >>> tool['dec'] = 22.01
#
# and assigns the 'ra' and 'dec' parameters of the tool. The return function
# takes no argument and returns a dictionary with the parameter names as
# keys and the current values as values. No parameter querying is performed.
# For example
#
# >>> d = tool.pardict()
#
# puts all parameters in the dictionary d.
def _pardict(self, *args):
if len(args) == 0:
d = {}
for par in self._pars():
if par.type() == 'b':
v = gammalib.tolower(par.current_value())
value = (v == "yes" or v == "y" or v == "true" or v == "t")
d[par.name()] = value
elif par.type() == 'i':
d[par.name()] = int(par.current_value())
elif par.type() == 'r':
d[par.name()] = float(par.current_value())
else:
d[par.name()] = str(par.current_value())
return d
elif len(args) == 1:
for key in args[0]:
self[key] = args[0][key]
else:
raise TypeError('pardict() takes 0 or 1 arguments (%d given)' % len(args))
ctool.pardict = _pardict
cscript.pardict = _pardict
# Execute the script
def _execute(self):
self._read_ahead(True)
self.run()
self.save()
cscript.execute = _execute
%}