GammaLib 2.2.0.dev
Loading...
Searching...
No Matches
GCOMTim.cpp
Go to the documentation of this file.
1/***************************************************************************
2 * GCOMTim.cpp - COMPTEL Good Time Intervals class *
3 * ----------------------------------------------------------------------- *
4 * copyright (C) 2017-2026 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 GCOMTim.cpp
23 * @brief COMPTEL Good Time Intervals class implementation
24 * @author Juergen Knodlseder
25 */
26
27/* __ Includes ___________________________________________________________ */
28#ifdef HAVE_CONFIG_H
29#include <config.h>
30#endif
31#include "GFits.hpp"
32#include "GFitsTable.hpp"
33#include "GFitsBinTable.hpp"
36#include "GCOMTools.hpp"
37#include "GCOMSupport.hpp"
38#include "GCOMTim.hpp"
39
40/* __ Method name definitions ____________________________________________ */
41
42/* __ Macros _____________________________________________________________ */
43
44/* __ Coding definitions _________________________________________________ */
45
46/* __ Debug definitions __________________________________________________ */
47
48
49
50/*==========================================================================
51 = =
52 = Constructors/destructors =
53 = =
54 ==========================================================================*/
55
56/***********************************************************************//**
57 * @brief Void constructor
58 ***************************************************************************/
60{
61 // Initialise class members
63
64 // Return
65 return;
66}
67
68
69/***********************************************************************//**
70 * @brief Good Time Interval constructor
71 *
72 * @param[in] gti Good Time Intervals.
73 ***************************************************************************/
75{
76 // Initialise class members
78
79 // Set GTIs
80 m_gti = gti;
81
82 // Return
83 return;
84}
85
86
87/***********************************************************************//**
88 * @brief Copy constructor
89 *
90 * @param[in] tim COMPTEL Good Time Intervals.
91 ***************************************************************************/
93{
94 // Initialise class members
96
97 // Copy members
98 copy_members(tim);
99
100 // Return
101 return;
102}
103
104
105/***********************************************************************//**
106 * @brief File name constructor
107 *
108 * @param[in] filename TIM file name.
109 * @param[in] usage Usage selection (blank: accept all usage strings).
110 * @param[in] mode Mode selection (blank: accept all mode strings).
111 *
112 * Constructs COMPTEL Good Time Intervals from the information in a TIM
113 * FITS file.
114 ***************************************************************************/
116 const std::string& usage,
117 const std::string& mode)
118{
119 // Initialise class members
120 init_members();
121
122 // Load TIM file
123 load(filename, usage, mode);
124
125 // Return
126 return;
127}
128
129
130/***********************************************************************//**
131 * @brief Destructor
132 ***************************************************************************/
134{
135 // Free members
136 free_members();
137
138 // Return
139 return;
140}
141
142
143/*==========================================================================
144 = =
145 = Operators =
146 = =
147 ==========================================================================*/
148
149/***********************************************************************//**
150 * @brief Assignment operator
151 *
152 * @param[in] tim COMPTEL Good Time Intervals.
153 * @return COMPTEL Good Time Intervals.
154 ***************************************************************************/
156{
157 // Execute only if object is not identical
158 if (this != &tim) {
159
160 // Free members
161 free_members();
162
163 // Initialise private members
164 init_members();
165
166 // Copy members
167 copy_members(tim);
168
169 } // endif: object was not identical
170
171 // Return this object
172 return *this;
173}
174
175
176/*==========================================================================
177 = =
178 = Public methods =
179 = =
180 ==========================================================================*/
181
182/***********************************************************************//**
183 * @brief Clear COMPTEL good time intervals
184 ***************************************************************************/
186{
187 // Free members
188 free_members();
189
190 // Initialise private members
191 init_members();
192
193 // Return
194 return;
195}
196
197
198/***********************************************************************//**
199 * @brief Clone COMPTEL good time intervals
200 *
201 * @return Pointer to deep copy of COMPTEL Good Time Intervals.
202 ***************************************************************************/
204{
205 return new GCOMTim(*this);
206}
207
208
209/***********************************************************************//**
210 * @brief Load COMPTEL Good Time Intervals from FITS file
211 *
212 * @param[in] filename TIM file name.
213 * @param[in] usage Usage selection (blank: accept all usage strings).
214 * @param[in] mode Mode selection (blank: accept all mode strings).
215 *
216 * Load COMPTEL Good Time Intervals from the information in a TIM FITS file.
217 ***************************************************************************/
218void GCOMTim::load(const GFilename& filename,
219 const std::string& usage,
220 const std::string& mode)
221{
222 // Open FITS file
223 GFits fits(filename);
224
225 // Get HDU (pointer is always valid)
226 const GFitsTable& hdu = *fits.table(1);
227
228 // Read TIM file
229 read(hdu, usage, mode);
230
231 // Close FITS file
232 fits.close();
233
234 // Return
235 return;
236}
237
238
239/***********************************************************************//**
240 * @brief Save Good Time Intervals into FITS file.
241 *
242 * @param[in] filename FITS file name.
243 * @param[in] clobber Overwrite existing FITS file.
244 *
245 * Saves the event list into a FITS file. See the write() method for details.
246 ***************************************************************************/
247void GCOMTim::save(const GFilename& filename,
248 const bool& clobber) const
249{
250 // Allocate empty FITS file
251 GFits fits;
252
253 // Allocate empty FITS binary table
254 GFitsBinTable table;
255
256 // Write GTIs into binary table
257 write(table);
258
259 // Append TIM to FITS file
260 fits.append(table);
261
262 // Save FITS file
263 fits.saveto(filename.url(), clobber);
264
265 // Return
266 return;
267}
268
269
270/***********************************************************************//**
271 * @brief Read COMPTEL Good Time Intervals from FITS table
272 *
273 * @param[in] table TIM FITS table.
274 * @param[in] usage Usage selection (blank: accept all usage strings).
275 * @param[in] mode Mode selection (blank: accept all mode strings).
276 *
277 * Reads COMPTEL Good Time Intervals from the information in a TIM FITS
278 * table. Multpltiple modes may be specified in the @p mode string, for
279 * example "NORMAL|SOLAR80" so that both modes are accepted.
280 ***************************************************************************/
281void GCOMTim::read(const GFitsTable& table,
282 const std::string& usage,
283 const std::string& mode)
284{
285 // Clear object
286 clear();
287
288 // Extract number of TIM rows
289 int num = table.nrows();
290
291 // Extract mode strings by splitting user specified mode
292 std::vector<std::string> modes;
293 if (!mode.empty()) {
294 modes = gammalib::split(mode, "|");
295 }
296 int nmodes = modes.size();
297
298 // If there is TIM information then load it
299 if (num > 0) {
300
301 // Get column pointers
302 const GFitsTableCol* ptr_tjd_start = table["START_TJD"];
303 const GFitsTableCol* ptr_tic_start = table["START_TIC"];
304 const GFitsTableCol* ptr_tjd_end = table["END_TJD"];
305 const GFitsTableCol* ptr_tic_end = table["END_TIC"];
306 const GFitsTableCol* ptr_usage = table["USAGE"];
307 const GFitsTableCol* ptr_mode = table["MODE"];
308 const GFitsTableCol* ptr_subkey = table["SUBKEY"];
309 const GFitsTableCol* ptr_descrip = table["DESCRIP"];
310
311 // Convert data into GTI
312 for (int i = 0; i < num; ++i) {
313
314 // Skip if usage string does not match
315 if (!usage.empty() && ptr_usage->string(i) != usage) {
316 continue;
317 }
318
319 // Skip if mode string does not match
320 if (nmodes > 0) {
321
322 // Get mode string
323 std::string mode = ptr_mode->string(i);
324
325 // Kludge: convert "MODIFIED LOW" into "LOW"
326 if ((mode == "MODIFIED") && (ptr_subkey->string(i) == "LOW")) {
327 mode = "LOW";
328 }
329
330 // Check if TIM entry should be kept
331 bool found = false;
332 for (int k = 0; k < nmodes; ++k) {
333 if (mode == modes[k]) {
334 found = true;
335 break;
336 }
337 }
338
339 // If mode is not in requested modes then skip record
340 if (!found) {
341 continue;
342 }
343 }
344
345 // Convert times
346 GTime tstart = gammalib::com_time(ptr_tjd_start->integer(i),
347 ptr_tic_start->integer(i));
348 GTime tstop = gammalib::com_time(ptr_tjd_end->integer(i),
349 ptr_tic_end->integer(i));
350
351 // Append GTI
352 m_gti.append(tstart, tstop);
353
354 // Append attributes
355 m_usage.push_back(ptr_usage->string(i));
356 m_mode.push_back(ptr_mode->string(i));
357 m_subkey.push_back(ptr_subkey->string(i));
358 m_descrip.push_back(ptr_descrip->string(i));
359
360 } // endfor: Looped over GTIs
361
362 } // endif: there was TIM information
363
364 // Return
365 return;
366}
367
368
369/***********************************************************************//**
370 * @brief Write COMPTEL Good Time Intervals into FITS binary table
371 *
372 * @param[in] table TIM FITS table.
373 *
374 * Writes COMPTEL Good Time Intervals from the information in a TIM FITS
375 * binary table.
376 ***************************************************************************/
378{
379 // Extract number of GTIs
380 int size = m_gti.size();
381
382 // If there are GTIs then write them now
383 if (size > 0) {
384
385 // Allocate columns
386 GFitsTableULongCol col_start_tjd("START_TJD", size);
387 GFitsTableULongCol col_start_tic("START_TIC", size);
388 GFitsTableULongCol col_end_tjd("END_TJD", size);
389 GFitsTableULongCol col_end_tic("END_TIC", size);
390 GFitsTableStringCol col_usage("USAGE", size, 8);
391 GFitsTableStringCol col_mode("MODE", size, 8);
392 GFitsTableStringCol col_subkey("SUBKEY", size, 8);
393 GFitsTableStringCol col_descrip("DESCRIP", size, 64);
394
395 // Set units of columns
396 // (see http://fits.gsfc.nasa.gov/standard30/fits_standard30aa.pdf)
397 col_start_tjd.unit("d");
398 col_end_tjd.unit("d");
399
400 // Fill columns
401 for (int i = 0; i <size; ++i) {
402 col_start_tjd(i) = gammalib::com_tjd(m_gti.tstart(i));
403 col_start_tic(i) = gammalib::com_tics(m_gti.tstart(i));
404 col_end_tjd(i) = gammalib::com_tjd(m_gti.tstop(i));
405 col_end_tic(i) = gammalib::com_tics(m_gti.tstop(i));
406 col_end_tic(i) = gammalib::com_tics(m_gti.tstop(i));
407 col_usage(i) = m_usage[i];
408 col_mode(i) = m_mode[i];
409 col_subkey(i) = m_subkey[i];
410 col_descrip(i) = m_descrip[i];
411 }
412
413 // Append columns to table
414 table.append(col_start_tjd);
415 table.append(col_start_tic);
416 table.append(col_end_tjd);
417 table.append(col_end_tic);
418 table.append(col_usage);
419 table.append(col_mode);
420 table.append(col_subkey);
421 table.append(col_descrip);
422
423 } // endif: there were GTIs
424
425 // Set extension name
426 table.extname("COMPTEL_TIM");
427
428 // Set some keywords
429 //TODO
430
431 // Return
432 return;
433}
434
435
436/***********************************************************************//**
437 * @brief Print COMPTEL Good Time Intervals
438 *
439 * @param[in] chatter Chattiness.
440 * @return String containing COMPTEL Good Time Intervals information.
441 ***************************************************************************/
442std::string GCOMTim::print(const GChatter& chatter) const
443{
444 // Initialise result string
445 std::string result;
446
447 // Continue only if chatter is not silent
448 if (chatter != SILENT) {
449
450 // Append header
451 result.append("=== GCOMTim ===");
452
453 // Append GTI information
454 result.append("\n"+gammalib::parformat("Number of intervals"));
455 result.append(gammalib::str(m_gti.size()));
456 result.append("\n"+gammalib::parformat("Ontime"));
457 result.append(gammalib::str(m_gti.ontime())+" sec");
458 result.append("\n"+gammalib::parformat("Elapsed time"));
459 result.append(gammalib::str(m_gti.telapse())+" sec");
460
461 // Append time range
462 result.append("\n"+gammalib::parformat("TJD range"));
463 result.append(gammalib::str(gammalib::com_tjd(m_gti.tstart())));
464 result.append(":");
465 result.append(gammalib::str(gammalib::com_tics(m_gti.tstart())));
466 result.append(" - ");
467 result.append(gammalib::str(gammalib::com_tjd(m_gti.tstop())));
468 result.append(":");
469 result.append(gammalib::str(gammalib::com_tics(m_gti.tstop())));
470 result.append("\n"+gammalib::parformat("MJD range"));
471 result.append(gammalib::str(m_gti.tstart().mjd()));
472 result.append(" - ");
473 result.append(gammalib::str(m_gti.tstop().mjd()));
474 result.append("\n"+gammalib::parformat("UTC range"));
475 result.append(m_gti.tstart().utc());
476 result.append(" - ");
477 result.append(m_gti.tstop().utc());
478
479 // Collect modes
480 std::vector<std::string> modes;
481 for (int i = 0; i < m_mode.size(); ++i) {
482 std::string mode = gammalib::strip_whitespace(m_mode[i]);
483 if (mode.length() > 0) {
484 bool found = false;
485 for (int k = 0; k < modes.size(); ++k) {
486 if (mode == modes[k]) {
487 found = true;
488 break;
489 }
490 }
491 if (!found) {
492 modes.push_back(mode);
493 }
494 }
495 }
496
497 // Append modes
498 if (modes.size() == 0) {
499 result.append("\n"+gammalib::parformat("Mode"));
500 result.append("NONE");
501 }
502 else if (modes.size() == 1) {
503 result.append("\n"+gammalib::parformat("Mode"));
504 result.append(modes[0]);
505 }
506 else {
507 result.append("\n"+gammalib::parformat("Modes"));
508 for (int k = 0; k < modes.size(); ++k) {
509 if (k > 0) {
510 result.append(",");
511 }
512 result.append(modes[k]);
513 }
514 }
515
516 } // endif: chatter was not silent
517
518 // Return result
519 return result;
520}
521
522
523/*==========================================================================
524 = =
525 = Private methods =
526 = =
527 ==========================================================================*/
528
529/***********************************************************************//**
530 * @brief Initialise class members
531 ***************************************************************************/
533{
534 // Initialise members
535 m_gti.clear();
536 m_usage.clear();
537 m_mode.clear();
538 m_subkey.clear();
539 m_descrip.clear();
540
541 // Return
542 return;
543}
544
545
546/***********************************************************************//**
547 * @brief Copy class members
548 *
549 * @param[in] tim COMPTEL Good Time Intervals.
550 ***************************************************************************/
552{
553 // Copy members
554 m_gti = tim.m_gti;
555 m_usage = tim.m_usage;
556 m_mode = tim.m_mode;
557 m_subkey = tim.m_subkey;
558 m_descrip = tim.m_descrip;
559
560 // Return
561 return;
562}
563
564
565/***********************************************************************//**
566 * @brief Delete class members
567 ***************************************************************************/
569{
570 // Return
571 return;
572}
Implementation of support function used by COMPTEL classes.
COMPTEL Good Time Intervals class definition.
Definition of COMPTEL tools.
FITS binary table class definition.
FITS table string column class interface definition.
FITS table unsigned long integer column class interface definition.
FITS table abstract base class interface definition.
FITS file class interface definition.
GChatter
Definition GTypemaps.hpp:33
@ SILENT
Definition GTypemaps.hpp:34
COMPTEL Good Time Intervals class.
Definition GCOMTim.hpp:50
GGti m_gti
Good Time intervals.
Definition GCOMTim.hpp:89
const GGti & gti(void) const
Return Good Time Intervals.
Definition GCOMTim.hpp:148
void write(GFitsBinTable &table) const
Write COMPTEL Good Time Intervals into FITS binary table.
Definition GCOMTim.cpp:377
std::vector< std::string > m_usage
Usage string.
Definition GCOMTim.hpp:90
std::vector< std::string > m_subkey
Subkey string.
Definition GCOMTim.hpp:92
GCOMTim(void)
Void constructor.
Definition GCOMTim.cpp:59
virtual void clear(void)
Clear COMPTEL good time intervals.
Definition GCOMTim.cpp:185
virtual std::string print(const GChatter &chatter=NORMAL) const
Print COMPTEL Good Time Intervals.
Definition GCOMTim.cpp:442
void init_members(void)
Initialise class members.
Definition GCOMTim.cpp:532
virtual ~GCOMTim(void)
Destructor.
Definition GCOMTim.cpp:133
void read(const GFitsTable &table, const std::string &usage="YES", const std::string &mode="NORMAL|SOLAR80|LOW")
Read COMPTEL Good Time Intervals from FITS table.
Definition GCOMTim.cpp:281
GCOMTim & operator=(const GCOMTim &tim)
Assignment operator.
Definition GCOMTim.cpp:155
std::vector< std::string > m_descrip
Description string.
Definition GCOMTim.hpp:93
void free_members(void)
Delete class members.
Definition GCOMTim.cpp:568
void copy_members(const GCOMTim &tim)
Copy class members.
Definition GCOMTim.cpp:551
void save(const GFilename &filename, const bool &clobber=false) const
Save Good Time Intervals into FITS file.
Definition GCOMTim.cpp:247
void load(const GFilename &filename, const std::string &usage="YES", const std::string &mode="NORMAL|SOLAR80|LOW")
Load COMPTEL Good Time Intervals from FITS file.
Definition GCOMTim.cpp:218
virtual GCOMTim * clone(void) const
Clone COMPTEL good time intervals.
Definition GCOMTim.cpp:203
std::vector< std::string > m_mode
Mode string.
Definition GCOMTim.hpp:91
Filename class.
Definition GFilename.hpp:62
std::string url(void) const
Return Uniform Resource Locator (URL)
FITS binary table class.
const std::string & extname(void) const
Return extension name.
Definition GFitsHDU.hpp:162
Abstract interface for FITS table column.
virtual int integer(const int &row, const int &inx=0) const =0
virtual std::string string(const int &row, const int &inx=0) const =0
void unit(const std::string &unit)
Set column unit.
FITS table string column.
FITS table unsigned long integer column.
Abstract interface for FITS table.
GFitsTableCol * append(const GFitsTableCol &column)
Append column to the table.
const int & nrows(void) const
Return number of rows in table.
FITS file class.
Definition GFits.hpp:63
void saveto(const GFilename &filename, const bool &clobber=false)
Saves to specified FITS file.
Definition GFits.cpp:1293
GFitsHDU * append(const GFitsHDU &hdu)
Append HDU to FITS file.
Definition GFits.cpp:678
void close(void)
Close FITS file.
Definition GFits.cpp:1342
GFitsTable * table(const int &extno)
Get pointer to table HDU.
Definition GFits.cpp:482
Good Time Interval class.
Definition GGti.hpp:63
const double & telapse(void) const
Returns elapsed time.
Definition GGti.hpp:227
const GTime & tstop(void) const
Returns latest stop time in Good Time Intervals.
Definition GGti.hpp:210
int size(void) const
Return number of Good Time Intervals.
Definition GGti.hpp:157
const double & ontime(void) const
Returns ontime.
Definition GGti.hpp:243
void append(const GTime &tstart, const GTime &tstop)
Append Good Time Interval.
Definition GGti.cpp:270
const GTime & tstart(void) const
Returns earliest start time in Good Time Intervals.
Definition GGti.hpp:197
void clear(void)
Clear Good Time Intervals.
Definition GGti.cpp:238
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
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
std::string strip_whitespace(const std::string &arg)
Strip leading and trailing whitespace from string.
Definition GTools.cpp:99
int com_tics(const GTime &time)
Convert GTime in COMPTEL tics.
std::vector< std::string > split(const std::string &s, const std::string &sep)
Split string.
Definition GTools.cpp:1002
int com_tjd(const GTime &time)
Convert GTime in COMPTEL TJD.
Definition GCOMTools.cpp:96
GTime com_time(const int &tjd, const int &tics)
Convert TJD and COMPTEL tics in GTime object.
Definition GCOMTools.cpp:60