GammaLib  1.7.0.dev
 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-2019 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 "GCOMTim.hpp"
37 #include "GCOMSupport.hpp"
38 
39 /* __ Method name definitions ____________________________________________ */
40 
41 /* __ Macros _____________________________________________________________ */
42 
43 /* __ Coding definitions _________________________________________________ */
44 
45 /* __ Debug definitions __________________________________________________ */
46 
47 
48 
49 /*==========================================================================
50  = =
51  = Constructors/destructors =
52  = =
53  ==========================================================================*/
54 
55 /***********************************************************************//**
56  * @brief Void constructor
57  ***************************************************************************/
59 {
60  // Initialise class members
61  init_members();
62 
63  // Return
64  return;
65 }
66 
67 
68 /***********************************************************************//**
69  * @brief Good Time Interval constructor
70  *
71  * @param[in] gti Good Time Intervals.
72  ***************************************************************************/
74 {
75  // Initialise class members
76  init_members();
77 
78  // Set GTIs
79  m_gti = gti;
80 
81  // Return
82  return;
83 }
84 
85 
86 /***********************************************************************//**
87  * @brief Copy constructor
88  *
89  * @param[in] tim COMPTEL Good Time Intervals.
90  ***************************************************************************/
92 {
93  // Initialise class members
94  init_members();
95 
96  // Copy members
97  copy_members(tim);
98 
99  // Return
100  return;
101 }
102 
103 
104 /***********************************************************************//**
105  * @brief File name constructor
106  *
107  * @param[in] filename TIM file name.
108  * @param[in] usage Usage selection (blank: accept all usage strings).
109  * @param[in] mode Mode selection (blank: accept all mode strings).
110  *
111  * Constructs COMPTEL Good Time Intervals from the information in a TIM
112  * FITS file.
113  ***************************************************************************/
114 GCOMTim::GCOMTim(const GFilename& filename,
115  const std::string& usage,
116  const std::string& mode)
117 {
118  // Initialise class members
119  init_members();
120 
121  // Load TIM file
122  load(filename, usage, mode);
123 
124  // Return
125  return;
126 }
127 
128 
129 /***********************************************************************//**
130  * @brief Destructor
131  ***************************************************************************/
133 {
134  // Free members
135  free_members();
136 
137  // Return
138  return;
139 }
140 
141 
142 /*==========================================================================
143  = =
144  = Operators =
145  = =
146  ==========================================================================*/
147 
148 /***********************************************************************//**
149  * @brief Assignment operator
150  *
151  * @param[in] tim COMPTEL Good Time Intervals.
152  * @return COMPTEL Good Time Intervals.
153  ***************************************************************************/
155 {
156  // Execute only if object is not identical
157  if (this != &tim) {
158 
159  // Free members
160  free_members();
161 
162  // Initialise private members
163  init_members();
164 
165  // Copy members
166  copy_members(tim);
167 
168  } // endif: object was not identical
169 
170  // Return this object
171  return *this;
172 }
173 
174 
175 /*==========================================================================
176  = =
177  = Public methods =
178  = =
179  ==========================================================================*/
180 
181 /***********************************************************************//**
182  * @brief Clear COMPTEL good time intervals
183  ***************************************************************************/
184 void GCOMTim::clear(void)
185 {
186  // Free members
187  free_members();
188 
189  // Initialise private members
190  init_members();
191 
192  // Return
193  return;
194 }
195 
196 
197 /***********************************************************************//**
198  * @brief Clone COMPTEL good time intervals
199  *
200  * @return Pointer to deep copy of COMPTEL Good Time Intervals.
201  ***************************************************************************/
203 {
204  return new GCOMTim(*this);
205 }
206 
207 
208 /***********************************************************************//**
209  * @brief Load COMPTEL Good Time Intervals from FITS file
210  *
211  * @param[in] filename TIM file name.
212  * @param[in] usage Usage selection (blank: accept all usage strings).
213  * @param[in] mode Mode selection (blank: accept all mode strings).
214  *
215  * Load COMPTEL Good Time Intervals from the information in a TIM FITS file.
216  ***************************************************************************/
217 void GCOMTim::load(const GFilename& filename,
218  const std::string& usage,
219  const std::string& mode)
220 {
221  // Open FITS file
222  GFits fits(filename);
223 
224  // Get HDU (pointer is always valid)
225  const GFitsTable& hdu = *fits.table(1);
226 
227  // Read TIM file
228  read(hdu, usage, mode);
229 
230  // Close FITS file
231  fits.close();
232 
233  // Return
234  return;
235 }
236 
237 
238 /***********************************************************************//**
239  * @brief Save Good Time Intervals into FITS file.
240  *
241  * @param[in] filename FITS file name.
242  * @param[in] clobber Overwrite existing FITS file.
243  *
244  * Saves the event list into a FITS file. See the write() method for details.
245  ***************************************************************************/
246 void GCOMTim::save(const GFilename& filename,
247  const bool& clobber) const
248 {
249  // Allocate empty FITS file
250  GFits fits;
251 
252  // Allocate empty FITS binary table
253  GFitsBinTable table;
254 
255  // Write GTIs into binary table
256  write(table);
257 
258  // Append TIM to FITS file
259  fits.append(table);
260 
261  // Save FITS file
262  fits.saveto(filename.url(), clobber);
263 
264  // Return
265  return;
266 }
267 
268 
269 /***********************************************************************//**
270  * @brief Read COMPTEL Good Time Intervals from FITS table
271  *
272  * @param[in] table TIM FITS table.
273  * @param[in] usage Usage selection (blank: accept all usage strings).
274  * @param[in] mode Mode selection (blank: accept all mode strings).
275  *
276  * Reads COMPTEL Good Time Intervals from the information in a TIM FITS
277  * table.
278  ***************************************************************************/
279 void GCOMTim::read(const GFitsTable& table,
280  const std::string& usage,
281  const std::string& mode)
282 {
283  // Clear object
284  clear();
285 
286  // Extract number of TIM rows
287  int num = table.nrows();
288 
289  // If there is TIM information then load it
290  if (num > 0) {
291 
292  // Get column pointers
293  const GFitsTableCol* ptr_tjd_start = table["START_TJD"];
294  const GFitsTableCol* ptr_tic_start = table["START_TIC"];
295  const GFitsTableCol* ptr_tjd_end = table["END_TJD"];
296  const GFitsTableCol* ptr_tic_end = table["END_TIC"];
297  const GFitsTableCol* ptr_usage = table["USAGE"];
298  const GFitsTableCol* ptr_mode = table["MODE"];
299 
300  // Convert data into GTI
301  for (int i = 0; i < num; ++i) {
302 
303  // Skip if usage string does not match
304  if (!usage.empty() && ptr_usage->string(i) != usage) {
305  continue;
306  }
307 
308  // Skip if mode string does not match
309  if (!mode.empty() && ptr_mode->string(i) != mode) {
310  continue;
311  }
312 
313  // Convert times
314  GTime tstart = gammalib::com_time(ptr_tjd_start->integer(i),
315  ptr_tic_start->integer(i));
316  GTime tstop = gammalib::com_time(ptr_tjd_end->integer(i),
317  ptr_tic_end->integer(i));
318 
319  // Append GTI
320  m_gti.append(tstart, tstop);
321 
322  } // endfor: Looped over GTIs
323 
324  } // endif: there was TIM information
325 
326  // Return
327  return;
328 }
329 
330 
331 /***********************************************************************//**
332  * @brief Write COMPTEL Good Time Intervals into FITS binary table
333  *
334  * @param[in] table TIM FITS table.
335  *
336  * Writes COMPTEL Good Time Intervals from the information in a TIM FITS
337  * binary table.
338  ***************************************************************************/
339 void GCOMTim::write(GFitsBinTable& table) const
340 {
341  // Extract number of GTIs
342  int size = m_gti.size();
343 
344  // If there are GTIs then write them now
345  if (size > 0) {
346 
347  // Allocate columns
348  GFitsTableULongCol col_start_tjd("START_TJD", size);
349  GFitsTableULongCol col_start_tic("START_TIC", size);
350  GFitsTableULongCol col_end_tjd("END_TJD", size);
351  GFitsTableULongCol col_end_tic("END_TIC", size);
352  GFitsTableStringCol col_usage("USAGE", size, 8);
353  GFitsTableStringCol col_mode("MODE", size, 8);
354  GFitsTableStringCol col_subkey("SUBKEY", size, 8);
355  GFitsTableStringCol col_descrip("DESCRIP", size, 64);
356 
357  // Set units of columns
358  // (see http://fits.gsfc.nasa.gov/standard30/fits_standard30aa.pdf)
359  col_start_tjd.unit("d");
360  col_end_tjd.unit("d");
361 
362  // Fill columns
363  for (int i = 0; i <size; ++i) {
364  col_start_tjd(i) = gammalib::com_tjd(m_gti.tstart(i));
365  col_start_tic(i) = gammalib::com_tics(m_gti.tstart(i));
366  col_end_tjd(i) = gammalib::com_tjd(m_gti.tstop(i));
367  col_end_tic(i) = gammalib::com_tics(m_gti.tstop(i));
368  col_end_tic(i) = gammalib::com_tics(m_gti.tstop(i));
369  col_usage(i) = "YES";
370  col_mode(i) = "NORMAL";
371  col_subkey(i) = "";
372  col_descrip(i) = "";
373  }
374 
375  // Append columns to table
376  table.append(col_start_tjd);
377  table.append(col_start_tic);
378  table.append(col_end_tjd);
379  table.append(col_end_tic);
380  table.append(col_usage);
381  table.append(col_mode);
382  table.append(col_subkey);
383  table.append(col_descrip);
384 
385  } // endif: there were GTIs
386 
387  // Set extension name
388  table.extname("COMPTEL_TIM");
389 
390  // Set some keywords
391  //TODO
392 
393  // Return
394  return;
395 }
396 
397 
398 /***********************************************************************//**
399  * @brief Print COMPTEL Good Time Intervals
400  *
401  * @param[in] chatter Chattiness.
402  * @return String containing COMPTEL Good Time Intervals information.
403  ***************************************************************************/
404 std::string GCOMTim::print(const GChatter& chatter) const
405 {
406  // Initialise result string
407  std::string result;
408 
409  // Continue only if chatter is not silent
410  if (chatter != SILENT) {
411 
412  // Append header
413  result.append("=== GCOMTim ===");
414 
415  // Append GTI information
416  result.append("\n"+gammalib::parformat("Number of intervals"));
417  result.append(gammalib::str(m_gti.size()));
418  result.append("\n"+gammalib::parformat("Ontime"));
419  result.append(gammalib::str(m_gti.ontime())+" sec");
420  result.append("\n"+gammalib::parformat("Elapsed time"));
421  result.append(gammalib::str(m_gti.telapse())+" sec");
422 
423  // Append time range
424  result.append("\n"+gammalib::parformat("TJD range"));
425  result.append(gammalib::str(gammalib::com_tjd(m_gti.tstart())));
426  result.append(":");
427  result.append(gammalib::str(gammalib::com_tics(m_gti.tstart())));
428  result.append(" - ");
429  result.append(gammalib::str(gammalib::com_tjd(m_gti.tstop())));
430  result.append(":");
431  result.append(gammalib::str(gammalib::com_tics(m_gti.tstop())));
432  result.append("\n"+gammalib::parformat("MJD range"));
433  result.append(gammalib::str(m_gti.tstart().mjd()));
434  result.append(" - ");
435  result.append(gammalib::str(m_gti.tstop().mjd()));
436  result.append("\n"+gammalib::parformat("UTC range"));
437  result.append(m_gti.tstart().utc());
438  result.append(" - ");
439  result.append(m_gti.tstop().utc());
440 
441  } // endif: chatter was not silent
442 
443  // Return result
444  return result;
445 }
446 
447 
448 /*==========================================================================
449  = =
450  = Private methods =
451  = =
452  ==========================================================================*/
453 
454 /***********************************************************************//**
455  * @brief Initialise class members
456  ***************************************************************************/
458 {
459  // Initialise members
460  m_gti.clear();
461 
462  // Return
463  return;
464 }
465 
466 
467 /***********************************************************************//**
468  * @brief Copy class members
469  *
470  * @param[in] tim COMPTEL Good Time Intervals.
471  ***************************************************************************/
473 {
474  // Copy members
475  m_gti = tim.m_gti;
476 
477  // Return
478  return;
479 }
480 
481 
482 /***********************************************************************//**
483  * @brief Delete class members
484  ***************************************************************************/
486 {
487  // Return
488  return;
489 }
void unit(const std::string &unit)
Set column unit.
GFitsTable * table(const int &extno)
Get pointer to table HDU.
Definition: GFits.cpp:472
GCOMTim & operator=(const GCOMTim &tim)
Assignment operator.
Definition: GCOMTim.cpp:154
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:404
void init_members(void)
Initialise class members.
Definition: GCOMTim.cpp:457
virtual ~GCOMTim(void)
Destructor.
Definition: GCOMTim.cpp:132
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:279
GFitsTableCol * append(const GFitsTableCol &column)
Append column to the table.
Definition: GFitsTable.hpp:147
Time class.
Definition: GTime.hpp:54
FITS file class.
Definition: GFits.hpp:63
void free_members(void)
Delete class members.
Definition: GCOMTim.cpp:485
FITS file class interface definition.
GTime com_time(const int &tjd, const int &tics)
Convert TJD and COMPTEL ticks in GTime object.
COMPTEL Good Time Intervals class definition.
void append(const GTime &tstart, const GTime &tstop)
Append Good Time Interval.
Definition: GGti.cpp:269
Implementation of support function used by COMPTEL classes.
int size(void) const
Return number of Good Time Intervals.
Definition: GGti.hpp:153
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:339
void saveto(const GFilename &filename, const bool &clobber=false)
Saves to specified FITS file.
Definition: GFits.cpp:1267
void clear(void)
Clear Good Time Intervals.
Definition: GGti.cpp:237
void copy_members(const GCOMTim &tim)
Copy class members.
Definition: GCOMTim.cpp:472
Filename class.
Definition: GFilename.hpp:62
Abstract interface for FITS table column.
int com_tjd(const GTime &time)
Convert GTime in COMPTEL TJD.
GCOMTim(void)
Void constructor.
Definition: GCOMTim.cpp:58
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:202
virtual void clear(void)
Clear COMPTEL good time intervals.
Definition: GCOMTim.cpp:184
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:206
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:193
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.
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:217
GFitsHDU * append(const GFitsHDU &hdu)
Append HDU to FITS file.
Definition: GFits.cpp:665
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:246
double mjd(void) const
Return time in Modified Julian Days (TT)
Definition: GTime.cpp:319
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:1022
const double & telapse(void) const
Returns elapsed time.
Definition: GGti.hpp:223
void close(void)
Close FITS file.
Definition: GFits.cpp:1314
GGti m_gti
Good Time intervals.
Definition: GCOMTim.hpp:88
std::string utc(const int &precision=0) const
Return time as string in UTC time system.
Definition: GTime.cpp:464
const GGti & gti(void) const
Return Good Time Intervals.
Definition: GCOMTim.hpp:127
const double & ontime(void) const
Returns ontime.
Definition: GGti.hpp:239
std::string str(const unsigned short int &value)
Convert unsigned short integer value into string.
Definition: GTools.cpp:413
FITS table abstract base class interface definition.