GammaLib  2.1.0.dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GCOMStatus.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * GCOMStatus.cpp - COMPTEL instrument status class *
3  * ----------------------------------------------------------------------- *
4  * copyright (C) 2017 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 GCOMStatus.cpp
23  * @brief COMPTEL instrument status class implementation
24  * @author Juergen Knoedlseder
25  */
26 
27 /* __ Includes ___________________________________________________________ */
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif
31 #include "GTools.hpp"
32 #include "GException.hpp"
33 #include "GCaldb.hpp"
34 #include "GFits.hpp"
35 #include "GFitsTable.hpp"
36 #include "GCOMStatus.hpp"
37 
38 /* __ Method name definitions ____________________________________________ */
39 #define G_D1STATUS "GCOMStatus::d1status(int&, int&)"
40 #define G_D2STATUS "GCOMStatus::d2status(int&, int&)"
41 #define G_UPDATE_CACHE "GCOMStatus::update_cache(int&)"
42 #define G_LOAD_STATUS "GCOMStatus::load_status()"
43 
44 /* __ Macros _____________________________________________________________ */
45 
46 /* __ Coding definitions _________________________________________________ */
47 
48 /* __ Debug definitions __________________________________________________ */
49 
50 
51 
52 /*==========================================================================
53  = =
54  = Constructors/destructors =
55  = =
56  ==========================================================================*/
57 
58 /***********************************************************************//**
59  * @brief Void constructor
60  ***************************************************************************/
62 {
63  // Initialise class members
64  init_members();
65 
66  // Return
67  return;
68 }
69 
70 
71 /***********************************************************************//**
72  * @brief Copy constructor
73  *
74  * @param[in] status COMPTEL instrument status.
75  ***************************************************************************/
77 {
78  // Initialise class members
79  init_members();
80 
81  // Copy members
82  copy_members(status);
83 
84  // Return
85  return;
86 }
87 
88 
89 /***********************************************************************//**
90  * @brief Destructor
91  ***************************************************************************/
93 {
94  // Free members
95  free_members();
96 
97  // Return
98  return;
99 }
100 
101 
102 /*==========================================================================
103  = =
104  = Operators =
105  = =
106  ==========================================================================*/
107 
108 /***********************************************************************//**
109  * @brief Assignment operator
110  *
111  * @param[in] status COMPTEL instrument status.
112  * @return COMPTEL instrument status.
113  ***************************************************************************/
115 {
116  // Execute only if object is not identical
117  if (this != &status) {
118 
119  // Free members
120  free_members();
121 
122  // Initialise private members
123  init_members();
124 
125  // Copy members
126  copy_members(status);
127 
128  } // endif: object was not identical
129 
130  // Return this object
131  return *this;
132 }
133 
134 
135 /*==========================================================================
136  = =
137  = Public methods =
138  = =
139  ==========================================================================*/
140 
141 /***********************************************************************//**
142  * @brief Clear COMPTEL instrument status
143  ***************************************************************************/
145 {
146  // Free members
147  free_members();
148 
149  // Initialise private members
150  init_members();
151 
152  // Return
153  return;
154 }
155 
156 
157 /***********************************************************************//**
158  * @brief Clone COMPTEL instrument status
159  *
160  * @return Pointer to deep copy of COMPTEL instrument status.
161  ***************************************************************************/
163 {
164  return new GCOMStatus(*this);
165 }
166 
167 
168 /***********************************************************************//**
169  * @brief Load COMPTEL instrument status database
170  ***************************************************************************/
171 void GCOMStatus::load(void) const
172 {
173  // Load database
174  load_status();
175 
176  // Return
177  return;
178 }
179 
180 
181 /***********************************************************************//**
182  * @brief Return D1 module status
183  *
184  * @param[in] tjd TJD for status inquiry.
185  * @param[in] module D1 module number (1-7).
186  * @return D1 module status.
187  ***************************************************************************/
188 int GCOMStatus::d1status(const int& tjd, const int& module) const
189 {
190  // Load database if it's not yet available
191  if (m_tjds.empty()) {
192  load_status();
193  }
194 
195  // Check that module number is in range
196  if (module < 1 || module > 7) {
197  std::string msg = "Invalid D1 module number ("+gammalib::str(module)+
198  "). Number needs to be comprised within [1,7].";
200  }
201 
202  // Update cache
203  update_cache(tjd);
204 
205  // Return module status
206  return (m_last_d1status[module-1]);
207 }
208 
209 
210 /***********************************************************************//**
211  * @brief Return D2 module status
212  *
213  * @param[in] tjd TJD for status inquiry.
214  * @param[in] module D2 module number (1-14).
215  * @return D2 module status.
216  ***************************************************************************/
217 int GCOMStatus::d2status(const int& tjd, const int& module) const
218 {
219  // Load database if it's not yet available
220  if (m_tjds.empty()) {
221  load_status();
222  }
223 
224  // Check that module number is in range
225  if (module < 1 || module > 14) {
226  std::string msg = "Invalid D2 module number ("+gammalib::str(module)+
227  "). Number needs to be comprised within [1,14].";
229  }
230 
231  // Update cache
232  update_cache(tjd);
233 
234  // Return module status
235  return (m_last_d2status[module-1]);
236 }
237 
238 
239 /***********************************************************************//**
240  * @brief Print COMPTEL instrument status
241  *
242  * @param[in] chatter Chattiness.
243  * @return String containing COMPTEL instrument status information.
244  ***************************************************************************/
245 std::string GCOMStatus::print(const GChatter& chatter) const
246 {
247  // Initialise result string
248  std::string result;
249 
250  // Continue only if chatter is not silent
251  if (chatter != SILENT) {
252 
253  // Append header
254  result.append("=== GCOMStatus ===");
255 
256  // Append information
257  result.append("\n"+gammalib::parformat("Database"));
258  if (m_tjds.empty()) {
259  result.append("not loaded");
260  }
261  else {
262  result.append("loaded");
263  result.append("\n"+gammalib::parformat("Number of entries"));
264  result.append(gammalib::str(m_tjds.size()));
265  result.append("\n"+gammalib::parformat("TJD range"));
266  result.append(gammalib::str(m_tjds[0]));
267  result.append(" - ");
268  result.append(gammalib::str(m_tjds[m_tjds.size()-1]));
269  result.append(" days");
270  }
271 
272  } // endif: chatter was not silent
273 
274  // Return result
275  return result;
276 }
277 
278 
279 /*==========================================================================
280  = =
281  = Private methods =
282  = =
283  ==========================================================================*/
284 
285 /***********************************************************************//**
286  * @brief Initialise class members
287  ***************************************************************************/
289 {
290  // Initialise members
291  m_tjds.clear();
292  m_d1status.clear();
293  m_d2status.clear();
294 
295  // Initialise cache
296  m_last_tjd = 0;
297  m_last_d1status.clear();
298  m_last_d2status.clear();
299 
300  // Return
301  return;
302 }
303 
304 
305 /***********************************************************************//**
306  * @brief Copy class members
307  *
308  * @param[in] status COMPTEL instrument status.
309  ***************************************************************************/
311 {
312  // Copy members
313  m_tjds = status.m_tjds;
314  m_d1status = status.m_d1status;
315  m_d2status = status.m_d2status;
316 
317  // Copy cache
318  m_last_tjd = status.m_last_tjd;
321 
322  // Return
323  return;
324 }
325 
326 
327 /***********************************************************************//**
328  * @brief Delete class members
329  ***************************************************************************/
331 {
332  // Return
333  return;
334 }
335 
336 
337 /***********************************************************************//**
338  * @brief Update module status cache
339  *
340  * @param[in] tjd TJD for status inquiry.
341  ***************************************************************************/
342 void GCOMStatus::update_cache(const int& tjd) const
343 {
344  // Update cache only if the TJD value has changed
345  if (tjd != m_last_tjd) {
346 
347  // Update TJD value
348  m_last_tjd = tjd;
349 
350  // Signal that cache update was successful
351  bool success = false;
352 
353  // Find index
354  for (int i = 0; i < m_tjds.size(); ++i) {
355  if (m_tjds[i] == tjd) {
358  success = true;
359  break;
360  }
361  }
362 
363  // Throw an exception if update was not successful
364  if (!success) {
365  std::string msg = "No COMPTEL status information found for TJD "+
366  gammalib::str(tjd)+" in module status database. "
367  "Please exclude that day from your analysis.";
369  }
370 
371  } // endif: TJD value has changed
372 
373  // Return
374  return;
375 }
376 
377 
378 /***********************************************************************//**
379  * @brief Load status information from database
380  *
381  * Loads the status information from the database.
382  ***************************************************************************/
383 void GCOMStatus::load_status(void) const
384 {
385  // Initialise status vectors
386  m_tjds.clear();
387  m_d1status.clear();
388  m_d2status.clear();
389 
390  // Locate COMPTEL calibration database that hold the module status
391  // information
392  GCaldb caldb("cgro","comptel");
393 
394  // Get database filename
395  GFilename filename = caldb.filename("","","MODULE_STATUS","","","DEFAULT");
396 
397  // If filename is empty then throw an exception
398  if (filename.is_empty()) {
399  std::string msg = "COMPTEL status information not found in COMPTEL "
400  "calibration database. Make sure that the COMPTEL "
401  "calibration database is in the path of the $CALDB "
402  "environment variable.";
404  }
405 
406  // Open FITS file
407  GFits fits(filename);
408 
409  // Get status table
410  const GFitsTable& table = *fits.table(1);
411 
412  // Extract number of entries in table
413  int num = table.nrows();
414 
415  // If there are entries then read them
416  if (num > 0) {
417 
418  // Get column pointers
419  const GFitsTableCol* ptr_tjd = table["TJD"];
420  const GFitsTableCol* ptr_d1status = table["D1STATUS"];
421  const GFitsTableCol* ptr_d2status = table["D2STATUS"];
422 
423  // Reserve space
424  m_tjds.reserve(num);
425  m_d1status.reserve(num);
426  m_d2status.reserve(num);
427 
428  // Copy data from table into vectors
429  for (int i = 0; i < num; ++i) {
430 
431  // Copy TJD
432  m_tjds.push_back(ptr_tjd->integer(i));
433 
434  // Copy D1 status
435  std::vector<int> d1status;
436  for (int k = 0; k < 7; ++k) {
437  d1status.push_back(ptr_d1status->integer(i,k));
438  }
439  m_d1status.push_back(d1status);
440 
441  // Copy D2 status
442  std::vector<int> d2status;
443  for (int k = 0; k < 14; ++k) {
444  d2status.push_back(ptr_d2status->integer(i,k));
445  }
446  m_d2status.push_back(d2status);
447 
448  } // endfor: looped over entries
449 
450  } // endif: there were entries
451 
452  // Close FITS file
453  fits.close();
454 
455  // Return
456  return;
457 }
COMPTEL instrument status class.
Definition: GCOMStatus.hpp:49
COMPTEL instrument status class definition.
GFitsTable * table(const int &extno)
Get pointer to table HDU.
Definition: GFits.cpp:482
std::vector< int > m_last_d2status
Last D2 module status.
Definition: GCOMStatus.hpp:87
virtual void clear(void)
Clear COMPTEL instrument status.
Definition: GCOMStatus.cpp:144
void free_members(void)
Delete class members.
Definition: GCOMStatus.cpp:330
void load_status(void) const
Load status information from database.
Definition: GCOMStatus.cpp:383
#define G_D1STATUS
Definition: GCOMStatus.cpp:39
bool is_empty(void) const
Signal if filename is empty.
Definition: GFilename.hpp:160
GFilename filename(const std::string &detector, const std::string &filter, const std::string &codename, const std::string &date, const std::string &time, const std::string &expr)
Return calibration file name based on selection parameters.
Definition: GCaldb.cpp:524
virtual ~GCOMStatus(void)
Destructor.
Definition: GCOMStatus.cpp:92
Gammalib tools definition.
FITS file class.
Definition: GFits.hpp:63
std::vector< int > m_tjds
TJD for status.
Definition: GCOMStatus.hpp:80
FITS file class interface definition.
virtual GCOMStatus * clone(void) const
Clone COMPTEL instrument status.
Definition: GCOMStatus.cpp:162
GCOMStatus & operator=(const GCOMStatus &status)
Assignment operator.
Definition: GCOMStatus.cpp:114
Calibration database class.
Definition: GCaldb.hpp:66
void copy_members(const GCOMStatus &status)
Copy class members.
Definition: GCOMStatus.cpp:310
Filename class.
Definition: GFilename.hpp:62
Abstract interface for FITS table column.
Abstract interface for FITS table.
Definition: GFitsTable.hpp:44
GChatter
Definition: GTypemaps.hpp:33
GCOMStatus(void)
Void constructor.
Definition: GCOMStatus.cpp:61
std::vector< std::vector< int > > m_d1status
D1 module status.
Definition: GCOMStatus.hpp:81
int d1status(const int &tjd, const int &module) const
Return D1 module status.
Definition: GCOMStatus.cpp:188
#define G_UPDATE_CACHE
Definition: GCOMStatus.cpp:41
Calibration database class interface definition.
const int & nrows(void) const
Return number of rows in table.
Definition: GFitsTable.hpp:119
int d2status(const int &tjd, const int &module) const
Return D2 module status.
Definition: GCOMStatus.cpp:217
virtual int integer(const int &row, const int &inx=0) const =0
int m_last_tjd
Last TJD.
Definition: GCOMStatus.hpp:85
std::vector< std::vector< int > > m_d2status
D2 module status.
Definition: GCOMStatus.hpp:82
std::vector< int > m_last_d1status
Last D1 module status.
Definition: GCOMStatus.hpp:86
Exception handler interface definition.
void init_members(void)
Initialise class members.
Definition: GCOMStatus.cpp:288
void load(void) const
Load COMPTEL instrument status database.
Definition: GCOMStatus.cpp:171
#define G_LOAD_STATUS
Definition: GCOMStatus.cpp:42
std::string parformat(const std::string &s, const int &indent=0)
Convert string in parameter format.
Definition: GTools.cpp:1143
void update_cache(const int &tjd) const
Update module status cache.
Definition: GCOMStatus.cpp:342
void close(void)
Close FITS file.
Definition: GFits.cpp:1342
virtual std::string print(const GChatter &chatter=NORMAL) const
Print COMPTEL instrument status.
Definition: GCOMStatus.cpp:245
std::string str(const unsigned short int &value)
Convert unsigned short integer value into string.
Definition: GTools.cpp:489
FITS table abstract base class interface definition.
#define G_D2STATUS
Definition: GCOMStatus.cpp:40