GammaLib 2.1.0.dev
Loading...
Searching...
No Matches
GCOMHkd.cpp
Go to the documentation of this file.
1/***************************************************************************
2 * GCOMHkd.cpp - COMPTEL Housekeeping Data container class *
3 * ----------------------------------------------------------------------- *
4 * copyright (C) 2023 by Juergen Knodlseder *
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 GCOMHkd.cpp
23 * @brief COMPTEL Housekeeping Data container class implementation
24 * @author Juergen Knodlseder
25 */
26
27/* __ Includes ___________________________________________________________ */
28#ifdef HAVE_CONFIG_H
29#include <config.h>
30#endif
31#include "GException.hpp"
32#include "GTools.hpp"
33#include "GCOMTools.hpp"
34#include "GCOMHkd.hpp"
35
36/* __ Method name definitions ____________________________________________ */
37#define G_REMOVE "GCOMHkd::remove(int&)"
38#define G_EXTEND "GCOMHkd::extend(GCOMHkd&)"
39#define G_TIME_GET "GCOMHkd::time(int&)"
40#define G_VALUE_GET "GCOMHkd::value(int&)"
41#define G_TIME_SET "GCOMHkd::time(int&, GTime&)"
42#define G_VALUE_SET "GCOMHkd::value(int&, double&)"
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
65
66 // Return
67 return;
68}
69
70
71/***********************************************************************//**
72 * @brief Name constructor
73 *
74 * @param[in] name Name of Housekeeping parameter
75 ***************************************************************************/
76GCOMHkd::GCOMHkd(const std::string& name)
77{
78 // Initialise class members
80
81 // Set name
82 this->name(name);
83
84 // Return
85 return;
86}
87
88
89/***********************************************************************//**
90 * @brief Copy constructor
91 *
92 * @param[in] hkd Housekeeping Data container.
93 ***************************************************************************/
95{
96 // Initialise class members
98
99 // Copy members
100 copy_members(hkd);
101
102 // Return
103 return;
104}
105
106
107/***********************************************************************//**
108 * @brief Destructor
109 ***************************************************************************/
111{
112 // Free members
113 free_members();
114
115 // Return
116 return;
117}
118
119
120/*==========================================================================
121 = =
122 = Operators =
123 = =
124 ==========================================================================*/
125
126/***********************************************************************//**
127 * @brief Assignment operator
128 *
129 * @param[in] hkd Housekeeping Data container.
130 * @return Housekeeping Data container.
131 ***************************************************************************/
133{
134 // Execute only if object is not identical
135 if (this != &hkd) {
136
137 // Free members
138 free_members();
139
140 // Initialise private members
141 init_members();
142
143 // Copy members
144 copy_members(hkd);
145
146 } // endif: object was not identical
147
148 // Return this object
149 return *this;
150}
151
152
153/*==========================================================================
154 = =
155 = Public methods =
156 = =
157 ==========================================================================*/
158
159/***********************************************************************//**
160 * @brief Clear Housekeeping Data container
161 ***************************************************************************/
163{
164 // Free members
165 free_members();
166
167 // Initialise private members
168 init_members();
169
170 // Return
171 return;
172}
173
174
175/***********************************************************************//**
176 * @brief Clone Housekeeping Data container
177 *
178 * @return Pointer to deep copy of Housekeeping Data container.
179 ***************************************************************************/
181{
182 return new GCOMHkd(*this);
183}
184
185
186/***********************************************************************//**
187 * @brief Append Housekeeping Data to container
188 *
189 * @param[in] time Time stamp of Housekeeping Data.
190 * @param[in] value Value of Housekeeping Data.
191 *
192 * Appends time stamp and corresponding value to Housekeeping Data container.
193 ***************************************************************************/
194void GCOMHkd::append(const GTime& time, const double& value)
195{
196 // Append time and value
198 m_values.push_back(value);
199
200 // Return
201 return;
202}
203
204
205/***********************************************************************//**
206 * @brief Remove Housekeeping Data
207 *
208 * @param[in] index Housekeeping Data index [0,...,size()[.
209 *
210 * Removes Housekeeping Data at @p index from the container. All data after
211 * the specified @p index are moved forward by one position.
212 ***************************************************************************/
213void GCOMHkd::remove(const int& index)
214{
215 #if defined(G_RANGE_CHECK)
216 // If index is outside boundary then throw an error
217 if (index < 0 || index >= size()) {
218 throw GException::out_of_range(G_REMOVE, "Housekeeping Data index",
219 index, size());
220 }
221 #endif
222
223 // Remove time and erase value
224 m_times.remove(index);
225 m_values.erase(m_values.begin() + index);
226
227 // Return
228 return;
229}
230
231
232/***********************************************************************//**
233 * @brief Extend Housekeeping Data
234 *
235 * @param[in] hkd Housekeeping Data container.
236 *
237 * @exception GException::invalid_argument
238 * Mismatch between housekeeping parameter names.
239 *
240 * Extend existing Housekeeping Data container with data found in another
241 * container by respecting the time ordering of the data.
242 ***************************************************************************/
243void GCOMHkd::extend(const GCOMHkd& hkd)
244{
245 // Do nothing if Housekeeping Data container is empty
246 if (!hkd.is_empty()) {
247
248 // Check that housekeeping parameter name corresponds to expectation
249 if (hkd.name() != name()) {
250 std::string msg = "Housekeeping parameter name \""+hkd.name()+"\" "
251 "does not correspond to expected name \""+
252 name()+"\". Please specify a container with "
253 "the expected parameter name.";
255 }
256
257 // Get current number of housekeeping data
258 int num = hkd.size();
259
260 // Reserve enough space
261 reserve(size() + num);
262
263 // Find index of where to insert Housekeeping Data
264 int index = 0;
265 for (; index < size(); ++index) {
266 if (hkd.m_times[0] < m_times[index]) {
267 break;
268 }
269 }
270
271 // Inserts Housekeeping Data
272 if (index < size()) {
273 for (int i = 0; i < num; ++i) {
274 m_times.insert(index+i, hkd.m_times[i]);
275 m_values.insert(m_values.begin()+index+i, hkd.m_values[i]);
276 }
277 }
278 else {
279 for (int i = 0; i < num; ++i) {
280 m_times.append(hkd.m_times[i]);
281 m_values.push_back(hkd.m_values[i]);
282 }
283 }
284
285 } // endif: Housekeeping Data container was not empty
286
287 // Return
288 return;
289}
290
291
292/***********************************************************************//**
293 * @brief Return reference to Housekeeping Data time
294 *
295 * @param[in] index Housekeeping Data index [0,...,size()-1].
296 * @return Reference to Housekeeping Data time.
297 *
298 * @exception GException::out_of_range
299 * Housekeeping Data @p index is out of range.
300 *
301 * Returns a reference to the Housekeeping Data time with the specified
302 * @p index.
303 ***************************************************************************/
304const GTime& GCOMHkd::time(const int& index) const
305{
306 // Raise exception if index is out of range
307 if (index < 0 || index >= size()) {
308 std::string msg = "Housekeeping Data index";
309 throw GException::out_of_range(G_TIME_GET, msg, index, size());
310 }
311
312 // Return reference to time
313 return m_times[index];
314}
315
316
317/***********************************************************************//**
318 * @brief Set Housekeeping Data time
319 *
320 * @param[in] index Housekeeping Data index [0,...,size()-1].
321 * @param[in] time Housekeeping Data time.
322 *
323 * @exception GException::out_of_range
324 * Housekeeping Data @p index is out of range.
325 *
326 * Sets the Housekeeping Data time with the specified @p index.
327 ***************************************************************************/
328void GCOMHkd::time(const int& index, const GTime& time)
329{
330 // Raise exception if index is out of range
331 if (index < 0 || index >= size()) {
332 std::string msg = "Housekeeping Data index";
333 throw GException::out_of_range(G_TIME_SET, msg, index, size());
334 }
335
336 // Set time
337 m_times[index] = time;
338
339 // Return
340 return;
341}
342
343
344/***********************************************************************//**
345 * @brief Return reference to Housekeeping Data value
346 *
347 * @param[in] index Housekeeping Data index [0,...,size()-1].
348 * @return Reference to Housekeeping Data value.
349 *
350 * @exception GException::out_of_range
351 * Housekeeping Data @p index is out of range.
352 *
353 * Returns a reference to the Housekeeping Data value with the specified
354 * @p index.
355 ***************************************************************************/
356const double& GCOMHkd::value(const int& index) const
357{
358 // Raise exception if index is out of range
359 if (index < 0 || index >= size()) {
360 std::string msg = "Housekeeping Data index";
361 throw GException::out_of_range(G_VALUE_GET, msg, index, size());
362 }
363
364 // Return reference to value
365 return m_values[index];
366}
367
368
369/***********************************************************************//**
370 * @brief Set Housekeeping Data value
371 *
372 * @param[in] index Housekeeping Data index [0,...,size()-1].
373 * @param[in] value Housekeeping Data value.
374 *
375 * @exception GException::out_of_range
376 * Housekeeping Data @p index is out of range.
377 *
378 * Sets the Housekeeping Data value with the specified @p index.
379 ***************************************************************************/
380void GCOMHkd::value(const int& index, const double& value)
381{
382 // Raise exception if index is out of range
383 if (index < 0 || index >= size()) {
384 std::string msg = "Housekeeping Data index";
385 throw GException::out_of_range(G_VALUE_SET, msg, index, size());
386 }
387
388 // Set value
389 m_values[index] = value;
390
391 // Return
392 return;
393}
394
395
396
397/***********************************************************************//**
398 * @brief Print Housekeeping Data container
399 *
400 * @param[in] chatter Chattiness.
401 * @return String containing Housekeeping Data container information.
402 ***************************************************************************/
403std::string GCOMHkd::print(const GChatter& chatter) const
404{
405 // Initialise result string
406 std::string result;
407
408 // Continue only if chatter is not silent
409 if (chatter != SILENT) {
410
411 // Append header
412 result.append("=== GCOMHkd ===");
413
414 // Append information
415 result.append("\n"+gammalib::parformat("Parameter"));
416 result.append(m_name);
417 result.append("\n"+gammalib::parformat("Records"));
418 result.append(gammalib::str(size()));
419
420 // If there are records then print time range
421 if (size() > 0) {
422
423 // Get start and stop time
424 GTime tstart = m_times[0];
425 GTime tstop = m_times[size()-1];
426
427 // Print time intervals
428 result.append("\n"+gammalib::parformat("TJD interval"));
429 result.append(gammalib::str(gammalib::com_tjd(tstart))+":");
430 result.append(gammalib::str(gammalib::com_tics(tstart))+" - ");
431 result.append(gammalib::str(gammalib::com_tjd(tstop))+":");
432 result.append(gammalib::str(gammalib::com_tics(tstop)));
433 result.append("\n"+gammalib::parformat("MJD interval"));
434 result.append(gammalib::str(tstart.mjd())+" - ");
435 result.append(gammalib::str(tstop.mjd())+" days");
436 result.append("\n"+gammalib::parformat("UTC interval"));
437 result.append(tstart.utc()+" - ");
438 result.append(tstop.utc());
439
440 }
441
442 } // endif: chatter was not silent
443
444 // Return result
445 return result;
446}
447
448
449/*==========================================================================
450 = =
451 = Private methods =
452 = =
453 ==========================================================================*/
454
455/***********************************************************************//**
456 * @brief Initialise class members
457 ***************************************************************************/
459{
460 // Initialise members
461 m_name.clear();
462 m_times.clear();
463 m_values.clear();
464
465 // Return
466 return;
467}
468
469
470/***********************************************************************//**
471 * @brief Copy class members
472 *
473 * @param[in] hkd Housekeeping Data container.
474 ***************************************************************************/
476{
477 // Copy members
478 m_name = hkd.m_name;
479 m_times = hkd.m_times;
480 m_values = hkd.m_values;
481
482 // Return
483 return;
484}
485
486
487/***********************************************************************//**
488 * @brief Delete class members
489 ***************************************************************************/
491{
492 // Return
493 return;
494}
#define G_TIME_GET
#define G_TIME_SET
#define G_EXTEND
#define G_VALUE_GET
Definition GCOMHkd.cpp:40
#define G_VALUE_SET
Definition GCOMHkd.cpp:42
COMPTEL Housekeeping Data container class definition.
Definition of COMPTEL tools.
Exception handler interface definition.
#define G_REMOVE
Gammalib tools definition.
GChatter
Definition GTypemaps.hpp:33
@ SILENT
Definition GTypemaps.hpp:34
COMPTEL Housekeeping Data container class.
Definition GCOMHkd.hpp:49
virtual ~GCOMHkd(void)
Destructor.
Definition GCOMHkd.cpp:110
void free_members(void)
Delete class members.
Definition GCOMHkd.cpp:490
void remove(const int &index)
Remove Housekeeping Data.
Definition GCOMHkd.cpp:213
bool is_empty(void) const
Signals if there are no Housekeeping Data in container.
Definition GCOMHkd.hpp:127
GCOMHkd(void)
Void constructor.
Definition GCOMHkd.cpp:61
std::string m_name
Name of housekeeping parameter.
Definition GCOMHkd.hpp:86
GCOMHkd * clone(void) const
Clone Housekeeping Data container.
Definition GCOMHkd.cpp:180
GTimes m_times
Times.
Definition GCOMHkd.hpp:87
std::vector< double > m_values
Values at times.
Definition GCOMHkd.hpp:88
void extend(const GCOMHkd &hkd)
Extend Housekeeping Data.
Definition GCOMHkd.cpp:243
std::string print(const GChatter &chatter=NORMAL) const
Print Housekeeping Data container.
Definition GCOMHkd.cpp:403
const std::string & name(void) const
Return Housekeeping Data name.
Definition GCOMHkd.hpp:157
void init_members(void)
Initialise class members.
Definition GCOMHkd.cpp:458
GCOMHkd & operator=(const GCOMHkd &hkd)
Assignment operator.
Definition GCOMHkd.cpp:132
void append(const GTime &time, const double &value)
Append Housekeeping Data to container.
Definition GCOMHkd.cpp:194
void clear(void)
Clear Housekeeping Data container.
Definition GCOMHkd.cpp:162
void copy_members(const GCOMHkd &hkd)
Copy class members.
Definition GCOMHkd.cpp:475
void reserve(const int &num)
Reserves space for Housekeeping Data in container.
Definition GCOMHkd.hpp:141
const GTime & time(const int &index) const
Return reference to Housekeeping Data time.
Definition GCOMHkd.cpp:304
const double & value(const int &index) const
Return reference to Housekeeping Data value.
Definition GCOMHkd.cpp:356
int size(void) const
Return number of Housekeeping Data in container.
Definition GCOMHkd.hpp:112
Time class.
Definition GTime.hpp:55
double mjd(void) const
Return time in Modified Julian Days (TT)
Definition GTime.cpp:320
std::string utc(const int &precision=0) const
Return time as string in UTC time system.
Definition GTime.cpp:465
void remove(const int &index)
Remove time from container.
Definition GTimes.cpp:274
void append(const GTime &time)
Append time to container.
Definition GTimes.cpp:216
void clear(void)
Clear container.
Definition GTimes.cpp:185
void insert(const int &index, const GTime &time)
Insert time into container.
Definition GTimes.cpp:238
std::string parformat(const std::string &s, const int &indent=0)
Convert string in parameter format.
Definition GTools.cpp:1162
std::string str(const unsigned short int &value)
Convert unsigned short integer value into string.
Definition GTools.cpp:508
int com_tics(const GTime &time)
Convert GTime in COMPTEL tics.
int com_tjd(const GTime &time)
Convert GTime in COMPTEL TJD.
Definition GCOMTools.cpp:91