GammaLib  1.7.0.dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GModelSpectralRegistry.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * GModelSpectralRegistry.cpp - Spectral model registry class *
3  * ----------------------------------------------------------------------- *
4  * copyright (C) 2011-2016 by Juergen Knoedlseder *
5  * ----------------------------------------------------------------------- *
6  * *
7  * This program is free software: you can redistribute it and/or modify *
8  * it under the terms of the GNU General Public License as published by *
9  * the Free Software Foundation, either version 3 of the License, or *
10  * (at your option) any later version. *
11  * *
12  * This program is distributed in the hope that it will be useful, *
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15  * GNU General Public License for more details. *
16  * *
17  * You should have received a copy of the GNU General Public License *
18  * along with this program. If not, see <http://www.gnu.org/licenses/>. *
19  * *
20  ***************************************************************************/
21 /**
22  * @file GModelSpectralRegistry.cpp
23  * @brief Spectral model registry class implementation
24  * @author Juergen Knoedlseder
25  */
26 
27 /* __ Includes ___________________________________________________________ */
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif
31 #include "GModelPar.hpp"
32 #include "GModelSpectral.hpp"
34 #include "GException.hpp"
35 #include "GTools.hpp"
36 
37 /* __ Method name definitions ____________________________________________ */
38 #define G_ALLOC "GModelSpectralRegistry::alloc(GXmlElement&)"
39 #define G_NAME "GModelSpectralRegistry::name(int&)"
40 
41 /* __ Macros _____________________________________________________________ */
42 
43 /* __ Coding definitions _________________________________________________ */
44 
45 /* __ Debug definitions __________________________________________________ */
46 #define G_DEBUG_REGISTRY 0
47 
48 
49 /*==========================================================================
50  = =
51  = Constructors/destructors =
52  = =
53  ==========================================================================*/
54 
55 /***********************************************************************//**
56  * @brief Void constructor
57  ***************************************************************************/
59 {
60  // Initialise private members for clean destruction
61  init_members();
62 
63  // Debug option: Show actual registry
64  #if G_DEBUG_REGISTRY
65  std::cout << "GModelSpectralRegistry(void): ";
66  for (int i = 0; i < size(); ++i) {
67  std::cout << "\"" << models()[i]->type() << "\" ";
68  }
69  std::cout << std::endl;
70  #endif
71 
72  // Return
73  return;
74 }
75 
76 
77 /***********************************************************************//**
78  * @brief Model constructor
79  *
80  * @param[in] model Model.
81  *
82  * Construct registry by adding a model to the registry. This is the standard
83  * constructor that is used to register a new model to GammaLib.
84  ***************************************************************************/
86 {
87  // Initialise private members for clean destruction
88  init_members();
89 
90  // Debug option: Notify new registry
91  #if G_DEBUG_REGISTRY
92  std::cout << "GModelSpectralRegistry(const GModelSpatial*): ";
93  std::cout << "add \"" << model->type() << "\" to registry." << std::endl;
94  #endif
95 
96  // Allocate new registry
97  const GModelSpectral** new_models = new const GModelSpectral*[size()+1];
98 
99  // Save old registry
100  for (int i = 0; i < size(); ++i) {
101  new_models[i] = models()[i];
102  }
103 
104  // Add new model to registry
105  new_models[size()] = model;
106 
107  // Set pointers on new registry
108  models().assign(new_models);
109 
110  // Increment number of models in registry
111  number()++;
112 
113  // Debug option: Show actual registry
114  #if G_DEBUG_REGISTRY
115  std::cout << "GModelSpectralRegistry(const GModelSpectral*): ";
116  for (int i = 0; i < size(); ++i) {
117  std::cout << "\"" << models()[i]->type() << "\" ";
118  }
119  std::cout << std::endl;
120  #endif
121 
122  // Return
123  return;
124 }
125 
126 
127 /***********************************************************************//**
128  * @brief Copy constructor
129  *
130  * @param[in] registry Registry.
131  ***************************************************************************/
133 {
134  // Initialise private members
135  init_members();
136 
137  // Copy members
138  copy_members(registry);
139 
140  // Return
141  return;
142 }
143 
144 
145 /***********************************************************************//**
146  * @brief Destructor
147  ***************************************************************************/
149 {
150  // Free members
151  free_members();
152 
153  // Return
154  return;
155 }
156 
157 
158 /*==========================================================================
159  = =
160  = Operators =
161  = =
162  ==========================================================================*/
163 
164 /***********************************************************************//**
165  * @brief Assignment operator
166  *
167  * @param[in] registry Registry.
168  * @return Reference to registry.
169  ***************************************************************************/
171 {
172  // Execute only if object is not identical
173  if (this != &registry) {
174 
175  // Free members
176  free_members();
177 
178  // Initialise private members
179  init_members();
180 
181  // Copy members
182  copy_members(registry);
183 
184  } // endif: object was not identical
185 
186  // Return
187  return *this;
188 }
189 
190 
191 /*==========================================================================
192  = =
193  = Public methods =
194  = =
195  ==========================================================================*/
196 
197 /***********************************************************************//**
198  * @brief Allocate spectral model that is found in XML element
199  *
200  * @param[in] xml XML element.
201  * @return Pointer to spectral model.
202  *
203  * @exception GException::invalid_value
204  * No appropriate spectral model found in XML element.
205  *
206  * Returns a pointer to a spectral model instance that corresponds to the
207  * type and parameters found in an XML element. If no appropriate model is
208  * found the method will throw an exception.
209  ***************************************************************************/
211 {
212  // Initialise spectral model
213  GModelSpectral* model = NULL;
214 
215  // Initialise missing parameters string
216  std::string missing = "";
217 
218  // Search for appropriate model in registry
219  for (int i = 0; i < size(); ++i) {
220 
221  // If an appropriate model type was found then check the model
222  // parameters
223  if (models()[i]->type() == xml.attribute("type")) {
224 
225  // Check if all required parameters are present. Put all missing
226  // parameters as a comma separated list in the "missing" string.
227  // If at least one parameter is missing the "found" flag will be
228  // set to "false".
229  bool found = true;
230  for (int k = 0; k < models()[i]->size(); ++k) {
231  if (!gammalib::xml_has_par(xml, (*(models()[i]))[k].name())) {
232  if (missing.length() > 0) {
233  missing += " ,";
234  }
235  missing += "\""+(*(models()[i]))[k].name()+"\"";
236  found = false;
237  }
238  }
239 
240  // If parameters are missing then check the next model
241  if (!found) {
242  continue;
243  }
244 
245  // No parameters are missing. We thus have the appropriate
246  // model and can break now.
247  model = models()[i]->clone();
248  break;
249 
250  } // endif: appropriate type found
251 
252  } // endfor: looped over models
253 
254  // If no model has been found then throw an exception
255  if (model == NULL) {
256 
257  // Initialise exception message
258  std::string msg = "";
259 
260  // If the type has been found then we had missing parameters
261  if (missing.length() > 0) {
262  msg = "Spectral model of type \""+xml.attribute("type")+ "\" found "
263  "but the following parameters are missing: "+missing;
264  }
265 
266  // ... otherwise the appropriate type was not found
267  else {
268  msg = "Spectral model of type \""+xml.attribute("type")+ "\" not "
269  "found in registry. Possible spectral model types are:";
270  for (int i = 0; i < size(); ++i) {
271  msg += " \""+models()[i]->type()+"\"";
272  }
273  msg += ".";
274  }
275 
276  // Throw exception
278 
279  } // endif: model was not found
280 
281  // Read model from XML element
282  model->read(xml);
283 
284  // Return spectral model
285  return model;
286 }
287 
288 
289 /***********************************************************************//**
290  * @brief Returns model name
291  *
292  * @param[in] index Model index [0,...,size()-1].
293  * @return Model name.
294  *
295  * @exception GException::out_of_range
296  * Model index is out of range.
297  ***************************************************************************/
298 std::string GModelSpectralRegistry::name(const int& index) const
299 {
300  // Compile option: raise exception if index is out of range
301  #if defined(G_RANGE_CHECK)
302  if (index < 0 || index >= size()) {
303  throw GException::out_of_range(G_NAME, "Spectral model index", index,
304  size());
305  }
306  #endif
307 
308  // Return name
309  return (models()[index]->type());
310 }
311 
312 
313 /***********************************************************************//**
314  * @brief Print registry information
315  *
316  * @param[in] chatter Chattiness.
317  * @return Registry content.
318  ***************************************************************************/
319 std::string GModelSpectralRegistry::print(const GChatter& chatter) const
320 {
321  // Initialise result string
322  std::string result;
323 
324  // Continue only if chatter is not silent
325  if (chatter != SILENT) {
326 
327  // Append header
328  result.append("=== GModelSpectralRegistry ===");
329  result.append("\n"+gammalib::parformat("Number of models"));
330  result.append(gammalib::str(size()));
331 
332  // NORMAL: Append models
333  if (chatter >= NORMAL) {
334  for (int i = 0; i < size(); ++i) {
335  result.append("\n"+gammalib::parformat(models()[i]->type()));
336  for (int k = 0; k < models()[i]->size(); ++k) {
337  if (k > 0) {
338  result.append(", ");
339  }
340  result.append("\"");
341  result.append((*(models()[i]))[k].name());
342  result.append("\"");
343  }
344  }
345  }
346 
347  } // endif: chatter was not silent
348 
349  // Return result
350  return result;
351 }
352 
353 
354 /*==========================================================================
355  = =
356  = Private methods =
357  = =
358  ==========================================================================*/
359 
360 /***********************************************************************//**
361  * @brief Initialise class members
362  ***************************************************************************/
364 {
365  // Return
366  return;
367 }
368 
369 
370 /***********************************************************************//**
371  * @brief Copy class members
372  *
373  * @param[in] registry Registry.
374  ***************************************************************************/
376 {
377  // Return
378  return;
379 }
380 
381 
382 /***********************************************************************//**
383  * @brief Delete class members
384  ***************************************************************************/
386 {
387  // Return
388  return;
389 }
void free_members(void)
Delete class members.
Abstract spectral model base class.
void init_members(void)
Initialise class members.
XML element node class.
Definition: GXmlElement.hpp:47
Spectral model registry class definition.
Gammalib tools definition.
virtual std::string type(void) const =0
virtual void read(const GXmlElement &xml)=0
static GRegistryPointer< const GModelSpectral * > & models()
Model parameter class interface definition.
GModelSpectralRegistry(void)
Void constructor.
std::string name(const int &index) const
Returns model name.
const GXmlAttribute * attribute(const int &index) const
Return attribute.
std::string print(const GChatter &chatter=NORMAL) const
Print registry information.
GModelSpectral * alloc(const GXmlElement &xml) const
Allocate spectral model that is found in XML element.
GChatter
Definition: GTypemaps.hpp:33
Interface definition for the spectral model registry class.
bool xml_has_par(const GXmlElement &xml, const std::string &name)
Checks if parameter with given name in XML element exists.
Definition: GTools.cpp:1473
virtual ~GModelSpectralRegistry(void)
Destructor.
Abstract spectral model base class interface definition.
#define G_NAME
GModelSpectralRegistry & operator=(const GModelSpectralRegistry &registry)
Assignment operator.
int size(void) const
Return number of registered models.
Exception handler interface definition.
std::string parformat(const std::string &s, const int &indent=0)
Convert string in parameter format.
Definition: GTools.cpp:1020
#define G_ALLOC
void copy_members(const GModelSpectralRegistry &registry)
Copy class members.
std::string str(const unsigned short int &value)
Convert unsigned short integer value into string.
Definition: GTools.cpp:411