GammaLib  2.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GCOMTim.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * GCOMTim.cpp - COMPTEL Good Time Intervals class *
3  * ----------------------------------------------------------------------- *
4  * copyright (C) 2017-2022 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"
34 #include "GFitsTableULongCol.hpp"
35 #include "GFitsTableStringCol.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
62  init_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
77  init_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
95  init_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  ***************************************************************************/
115 GCOMTim::GCOMTim(const GFilename& filename,
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  ***************************************************************************/
185 void GCOMTim::clear(void)
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  ***************************************************************************/
218 void 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  ***************************************************************************/
247 void 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.
279  ***************************************************************************/
280 void GCOMTim::read(const GFitsTable& table,
281  const std::string& usage,
282  const std::string& mode)
283 {
284  // Clear object
285  clear();
286 
287  // Extract number of TIM rows
288  int num = table.nrows();
289 
290  // If there is TIM information then load it
291  if (num > 0) {
292 
293  // Get column pointers
294  const GFitsTableCol* ptr_tjd_start = table["START_TJD"];
295  const GFitsTableCol* ptr_tic_start = table["START_TIC"];
296  const GFitsTableCol* ptr_tjd_end = table["END_TJD"];
297  const GFitsTableCol* ptr_tic_end = table["END_TIC"];
298  const GFitsTableCol* ptr_usage = table["USAGE"];
299  const GFitsTableCol* ptr_mode = table["MODE"];
300 
301  // Convert data into GTI
302  for (int i = 0; i < num; ++i) {
303 
304  // Skip if usage string does not match
305  if (!usage.empty() && ptr_usage->string(i) != usage) {
306  continue;
307  }
308 
309  // Skip if mode string does not match
310  if (!mode.empty() && ptr_mode->string(i) != mode) {
311  continue;
312  }
313 
314  // Convert times
315  GTime tstart = gammalib::com_time(ptr_tjd_start->integer(i),
316  ptr_tic_start->integer(i));
317  GTime tstop = gammalib::com_time(ptr_tjd_end->integer(i),
318  ptr_tic_end->integer(i));
319 
320  // Insert GTI to assume that the GTIs are in order
321  m_gti.insert(tstart, tstop);
322 
323  } // endfor: Looped over GTIs
324 
325  // Merge any GTIs to remove doubles or combine adjacent intervals
326  m_gti.merge();
327 
328  } // endif: there was TIM information
329 
330  // Return
331  return;
332 }
333 
334 
335 /***********************************************************************//**
336  * @brief Write COMPTEL Good Time Intervals into FITS binary table
337  *
338  * @param[in] table TIM FITS table.
339  *
340  * Writes COMPTEL Good Time Intervals from the information in a TIM FITS
341  * binary table.
342  ***************************************************************************/
343 void GCOMTim::write(GFitsBinTable& table) const
344 {
345  // Extract number of GTIs
346  int size = m_gti.size();
347 
348  // If there are GTIs then write them now
349  if (size > 0) {
350 
351  // Allocate columns
352  GFitsTableULongCol col_start_tjd("START_TJD", size);
353  GFitsTableULongCol col_start_tic("START_TIC", size);
354  GFitsTableULongCol col_end_tjd("END_TJD", size);
355  GFitsTableULongCol col_end_tic("END_TIC", size);
356  GFitsTableStringCol col_usage("USAGE", size, 8);
357  GFitsTableStringCol col_mode("MODE", size, 8);
358  GFitsTableStringCol col_subkey("SUBKEY", size, 8);
359  GFitsTableStringCol col_descrip("DESCRIP", size, 64);
360 
361  // Set units of columns
362  // (see http://fits.gsfc.nasa.gov/standard30/fits_standard30aa.pdf)
363  col_start_tjd.unit("d");
364  col_end_tjd.unit("d");
365 
366  // Fill columns
367  for (int i = 0; i <size; ++i) {
368  col_start_tjd(i) = gammalib::com_tjd(m_gti.tstart(i));
369  col_start_tic(i) = gammalib::com_tics(m_gti.tstart(i));
370  col_end_tjd(i) = gammalib::com_tjd(m_gti.tstop(i));
371  col_end_tic(i) = gammalib::com_tics(m_gti.tstop(i));
372  col_end_tic(i) = gammalib::com_tics(m_gti.tstop(i));
373  col_usage(i) = "YES";
374  col_mode(i) = "NORMAL";
375  col_subkey(i) = "";
376  col_descrip(i) = "";
377  }
378 
379  // Append columns to table
380  table.append(col_start_tjd);
381  table.append(col_start_tic);
382  table.append(col_end_tjd);
383  table.append(col_end_tic);
384  table.append(col_usage);
385  table.append(col_mode);
386  table.append(col_subkey);
387  table.append(col_descrip);
388 
389  } // endif: there were GTIs
390 
391  // Set extension name
392  table.extname("COMPTEL_TIM");
393 
394  // Set some keywords
395  //TODO
396 
397  // Return
398  return;
399 }
400 
401 
402 /***********************************************************************//**
403  * @brief Print COMPTEL Good Time Intervals
404  *
405  * @param[in] chatter Chattiness.
406  * @return String containing COMPTEL Good Time Intervals information.
407  ***************************************************************************/
408 std::string GCOMTim::print(const GChatter& chatter) const
409 {
410  // Initialise result string
411  std::string result;
412 
413  // Continue only if chatter is not silent
414  if (chatter != SILENT) {
415 
416  // Append header
417  result.append("=== GCOMTim ===");
418 
419  // Append GTI information
420  result.append("\n"+gammalib::parformat("Number of intervals"));
421  result.append(gammalib::str(m_gti.size()));
422  result.append("\n"+gammalib::parformat("Ontime"));
423  result.append(gammalib::str(m_gti.ontime())+" sec");
424  result.append("\n"+gammalib::parformat("Elapsed time"));
425  result.append(gammalib::str(m_gti.telapse())+" sec");
426 
427  // Append time range
428  result.append("\n"+gammalib::parformat("TJD range"));
429  result.append(gammalib::str(gammalib::com_tjd(m_gti.tstart())));
430  result.append(":");
431  result.append(gammalib::str(gammalib::com_tics(m_gti.tstart())));
432  result.append(" - ");
433  result.append(gammalib::str(gammalib::com_tjd(m_gti.tstop())));
434  result.append(":");
435  result.append(gammalib::str(gammalib::com_tics(m_gti.tstop())));
436  result.append("\n"+gammalib::parformat("MJD range"));
437  result.append(gammalib::str(m_gti.tstart().mjd()));
438  result.append(" - ");
439  result.append(gammalib::str(m_gti.tstop().mjd()));
440  result.append("\n"+gammalib::parformat("UTC range"));
441  result.append(m_gti.tstart().utc());
442  result.append(" - ");
443  result.append(m_gti.tstop().utc());
444 
445  } // endif: chatter was not silent
446 
447  // Return result
448  return result;
449 }
450 
451 
452 /*==========================================================================
453  = =
454  = Private methods =
455  = =
456  ==========================================================================*/
457 
458 /***********************************************************************//**
459  * @brief Initialise class members
460  ***************************************************************************/
462 {
463  // Initialise members
464  m_gti.clear();
465 
466  // Return
467  return;
468 }
469 
470 
471 /***********************************************************************//**
472  * @brief Copy class members
473  *
474  * @param[in] tim COMPTEL Good Time Intervals.
475  ***************************************************************************/
477 {
478  // Copy members
479  m_gti = tim.m_gti;
480 
481  // Return
482  return;
483 }
484 
485 
486 /***********************************************************************//**
487  * @brief Delete class members
488  ***************************************************************************/
490 {
491  // Return
492  return;
493 }
void unit(const std::string &unit)
Set column unit.
GFitsTable * table(const int &extno)
Get pointer to table HDU.
Definition: GFits.cpp:482
GCOMTim & operator=(const GCOMTim &tim)
Assignment operator.
Definition: GCOMTim.cpp:155
void insert(const GTime &tstart, const GTime &tstop)
Insert Good Time Interval.
Definition: GGti.cpp:289
FITS table unsigned long integer column class interface definition.
virtual std::string print(const GChatter &chatter=NORMAL) const
Print COMPTEL Good Time Intervals.
Definition: GCOMTim.cpp:408
Definition of COMPTEL tools.
void init_members(void)
Initialise class members.
Definition: GCOMTim.cpp:461
virtual ~GCOMTim(void)
Destructor.
Definition: GCOMTim.cpp:133
void read(const GFitsTable &table, const std::string &usage="YES", const std::string &mode="NORMAL")
Read COMPTEL Good Time Intervals from FITS table.
Definition: GCOMTim.cpp:280
GFitsTableCol * append(const GFitsTableCol &column)
Append column to the table.
Definition: GFitsTable.hpp:147
Time class.
Definition: GTime.hpp:55
FITS file class.
Definition: GFits.hpp:63
void free_members(void)
Delete class members.
Definition: GCOMTim.cpp:489
FITS file class interface definition.
GTime com_time(const int &tjd, const int &tics)
Convert TJD and COMPTEL tics in GTime object.
Definition: GCOMTools.cpp:55
COMPTEL Good Time Intervals class definition.
Implementation of support function used by COMPTEL classes.
int size(void) const
Return number of Good Time Intervals.
Definition: GGti.hpp:154
virtual std::string string(const int &row, const int &inx=0) const =0
FITS table string column.
void write(GFitsBinTable &table) const
Write COMPTEL Good Time Intervals into FITS binary table.
Definition: GCOMTim.cpp:343
void saveto(const GFilename &filename, const bool &clobber=false)
Saves to specified FITS file.
Definition: GFits.cpp:1293
void clear(void)
Clear Good Time Intervals.
Definition: GGti.cpp:237
void copy_members(const GCOMTim &tim)
Copy class members.
Definition: GCOMTim.cpp:476
Filename class.
Definition: GFilename.hpp:62
Abstract interface for FITS table column.
int com_tjd(const GTime &time)
Convert GTime in COMPTEL TJD.
Definition: GCOMTools.cpp:91
GCOMTim(void)
Void constructor.
Definition: GCOMTim.cpp:59
FITS table unsigned long integer column.
FITS table string column class interface definition.
Abstract interface for FITS table.
Definition: GFitsTable.hpp:44
virtual GCOMTim * clone(void) const
Clone COMPTEL good time intervals.
Definition: GCOMTim.cpp:203
virtual void clear(void)
Clear COMPTEL good time intervals.
Definition: GCOMTim.cpp:185
GChatter
Definition: GTypemaps.hpp:33
Good Time Interval class.
Definition: GGti.hpp:62
const GTime & tstop(void) const
Returns latest stop time in Good Time Intervals.
Definition: GGti.hpp:207
const std::string & extname(void) const
Return extension name.
Definition: GFitsHDU.hpp:162
const GTime & tstart(void) const
Returns earliest start time in Good Time Intervals.
Definition: GGti.hpp:194
const int & nrows(void) const
Return number of rows in table.
Definition: GFitsTable.hpp:119
int com_tics(const GTime &time)
Convert GTime in COMPTEL tics.
Definition: GCOMTools.cpp:125
std::string url(void) const
Return Uniform Resource Locator (URL)
Definition: GFilename.hpp:189
virtual int integer(const int &row, const int &inx=0) const =0
FITS binary table class.
void load(const GFilename &filename, const std::string &usage="YES", const std::string &mode="NORMAL")
Load COMPTEL Good Time Intervals from FITS file.
Definition: GCOMTim.cpp:218
GFitsHDU * append(const GFitsHDU &hdu)
Append HDU to FITS file.
Definition: GFits.cpp:678
FITS binary table class definition.
void save(const GFilename &filename, const bool &clobber=false) const
Save Good Time Intervals into FITS file.
Definition: GCOMTim.cpp:247
double mjd(void) const
Return time in Modified Julian Days (TT)
Definition: GTime.cpp:320
COMPTEL Good Time Intervals class.
Definition: GCOMTim.hpp:50
std::string parformat(const std::string &s, const int &indent=0)
Convert string in parameter format.
Definition: GTools.cpp:1143
const double & telapse(void) const
Returns elapsed time.
Definition: GGti.hpp:224
void close(void)
Close FITS file.
Definition: GFits.cpp:1342
GGti m_gti
Good Time intervals.
Definition: GCOMTim.hpp:89
std::string utc(const int &precision=0) const
Return time as string in UTC time system.
Definition: GTime.cpp:465
const GGti & gti(void) const
Return Good Time Intervals.
Definition: GCOMTim.hpp:144
const double & ontime(void) const
Returns ontime.
Definition: GGti.hpp:240
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.
void merge(void)
Merge all overlapping Good Time Intervals.
Definition: GGti.cpp:317