GammaLib  2.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GFitsHDU.hpp
Go to the documentation of this file.
1 /***************************************************************************
2  * GFitsHDU.hpp - Abstract FITS extension base class *
3  * ----------------------------------------------------------------------- *
4  * copyright (C) 2008-2018 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 GFitsHDU.hpp
23  * @brief Abstract FITS extension base class definition.
24  * @author Juergen Knoedlseder
25  */
26 
27 #ifndef GFITSHDU_HPP
28 #define GFITSHDU_HPP
29 
30 /* __ Includes ___________________________________________________________ */
31 #include <string>
32 #include "GBase.hpp"
33 #include "GFitsHeader.hpp"
34 #include "GFitsHeaderCard.hpp"
35 
36 
37 /***********************************************************************//**
38  * @class GFitsHDU
39  *
40  * @brief Abstract FITS extension base class
41  *
42  * This class defines the abstract interface for a FITS extension, also known
43  * as Header Data Unit (HDU). Each HDU consists of a header and a data area.
44  * The header is composed of cards and is implemented by the GFitsHeader
45  * class. The data are is either an image or a table which are implemented
46  * as derived classes from GFitsHDU.
47  *
48  * @todo Implement GFitsHDU* select(const std::string& expr) that applies
49  * to a table HDU for row selection.
50  ***************************************************************************/
51 class GFitsHDU : public GBase {
52 
53  // Friend classes
54  friend class GFits;
55 
56 public:
57  // Constructors and destructors
58  GFitsHDU(void);
59  GFitsHDU(const GFitsHDU& hdu);
60  virtual ~GFitsHDU(void);
61 
62  // Operators
63  GFitsHDU& operator=(const GFitsHDU& hdu);
64 
65  // Public enumerators
66  enum HDUType {
67  HT_IMAGE = 0,
70  };
71 
72  // Pure virtual methods
73  virtual void clear(void) = 0;
74  virtual GFitsHDU* clone(void) const = 0;
75  virtual std::string classname(void) const = 0;
76  virtual HDUType exttype(void) const = 0;
77  virtual std::string print(const GChatter& chatter = NORMAL) const = 0;
78 
79  // Implemented methods
80  int cards(void) const;
81  const std::string& extname(void) const;
82  void extname(const std::string& extname);
83  const int& extno(void) const;
84  void extno(const int& extno);
85  const GFitsHeader& header(void) const;
86  void header(const GFitsHeader& header);
87  bool has_card(const int& cardno) const;
88  bool has_card(const std::string& keyname) const;
89  GFitsHeaderCard& card(const int& cardno);
90  const GFitsHeaderCard& card(const int& cardno) const;
91  GFitsHeaderCard& card(const std::string& keyname);
92  const GFitsHeaderCard& card(const std::string& keyname) const;
93  void card(const GFitsHeaderCard& card);
94  void card(const std::string& keyname,
95  const std::string& value,
96  const std::string& comment);
97  void card(const std::string& keyname,
98  const double& value,
99  const std::string& comment);
100  void card(const std::string& keyname,
101  const int& value,
102  const std::string& comment);
103  void card(const std::string& keyname,
104  const bool& value,
105  const std::string& comment);
106  void card(const std::string& keyname,
107  const char* value,
108  const std::string& comment);
109  std::string string(const std::string& keyname) const;
110  double real(const std::string& keyname) const;
111  int integer(const std::string& keyname) const;
112 
113 protected:
114  // Protected methods
115  void init_members(void);
116  void copy_members(const GFitsHDU& hdu);
117  void free_members(void);
118  void connect(void* fptr);
119  void move_to_hdu(void);
120  HDUType get_hdu_type(void) const;
121  void open(void* vptr, int hdunum);
122  void save(void);
123  std::string print_hdu(const GChatter& chatter = NORMAL) const;
124  std::string typecode(int type) const;
125 
126  // Pure virtual protected methods
127  virtual void data_open(void* vptr) = 0;
128  virtual void data_save(void) = 0;
129  virtual void data_close(void) = 0;
130  virtual void data_connect(void* vptr) = 0;
131 
132  // Protected data area
133  void* m_fitsfile; //!< FITS file pointer pointing on actual HDU
134  int m_hdunum; //!< HDU number (starting from 0)
135  std::string m_name; //!< HDU name (extname)
136  GFitsHeader m_header; //!< HDU header
137 };
138 
139 
140 /***********************************************************************//**
141  * @brief Return number of cards in HDU header
142  *
143  * @return Number of cards in HDU header.
144  *
145  * Returns the number of cards in the header of the FITS extension.
146  ***************************************************************************/
147 inline
148 int GFitsHDU::cards(void) const
149 {
150  return (m_header.size());
151 }
152 
153 
154 /***********************************************************************//**
155  * @brief Return extension name
156  *
157  * @return Extension name.
158  *
159  * Returns the name of the FITS extension.
160  ***************************************************************************/
161 inline
162 const std::string& GFitsHDU::extname(void) const
163 {
164  return (m_name);
165 }
166 
167 
168 /***********************************************************************//**
169  * @brief Return extension number
170  *
171  * @return Extension number (starting from 0 for the primary image).
172  *
173  * Returns the number of the FITS extension.
174  ***************************************************************************/
175 inline
176 const int& GFitsHDU::extno(void) const
177 {
178  return (m_hdunum);
179 }
180 
181 
182 /***********************************************************************//**
183  * @brief Set extension number
184  *
185  * @param[in] extno Extension number (starting from 0 for the primary image).
186  *
187  * Set the number of the FITS extension.
188  ***************************************************************************/
189 inline
190 void GFitsHDU::extno(const int& extno)
191 {
192  m_hdunum = extno;
193  return;
194 }
195 
196 
197 /***********************************************************************//**
198  * @brief Return extension header
199  *
200  * @return Extension header.
201  *
202  * Returns the extension header.
203  ***************************************************************************/
204 inline
205 const GFitsHeader& GFitsHDU::header(void) const
206 {
207  return (m_header);
208 }
209 
210 
211 /***********************************************************************//**
212  * @brief Set extension header
213  *
214  * @param[in] header Extension header.
215  *
216  * Sets the extension header.
217  ***************************************************************************/
218 inline
219 void GFitsHDU::header(const GFitsHeader& header)
220 {
221  m_header = header;
222  return;
223 }
224 
225 
226 /***********************************************************************//**
227  * @brief Check existence of header card
228  *
229  * @param[in] cardno Number of card in header.
230  * @return True if card with specified card number @p cardno exists in header.
231  ***************************************************************************/
232 inline
233 bool GFitsHDU::has_card(const int& cardno) const
234 {
235  return m_header.contains(cardno);
236 }
237 
238 
239 /***********************************************************************//**
240  * @brief Checks for presence of header card
241  *
242  * @param[in] keyname Name of header card.
243  * @return True if card with specified @p keyname exists in header.
244  ***************************************************************************/
245 inline
246 bool GFitsHDU::has_card(const std::string& keyname) const
247 {
248  return (m_header.contains(keyname));
249 }
250 
251 
252 /***********************************************************************//**
253  * @brief Return header card
254  *
255  * @param[in] cardno Number of card in header.
256  * @return Header card.
257  ***************************************************************************/
258 inline
259 GFitsHeaderCard& GFitsHDU::card(const int& cardno)
260 {
261  return (m_header.at(cardno));
262 }
263 
264 
265 /***********************************************************************//**
266  * @brief Return header card (const version)
267  *
268  * @param[in] cardno Number of card in header.
269  * @return Header card.
270  ***************************************************************************/
271 inline
272 const GFitsHeaderCard& GFitsHDU::card(const int& cardno) const
273 {
274  return (m_header.at(cardno));
275 }
276 
277 
278 /***********************************************************************//**
279  * @brief Return header card
280  *
281  * @param[in] keyname Name of header card.
282  * @return Header card.
283  ***************************************************************************/
284 inline
285 GFitsHeaderCard& GFitsHDU::card(const std::string& keyname)
286 {
287  return (m_header.at(keyname));
288 }
289 
290 
291 /***********************************************************************//**
292  * @brief Return header card (const version)
293  *
294  * @param[in] keyname Name of header card.
295  * @return Header card.
296  ***************************************************************************/
297 inline
298 const GFitsHeaderCard& GFitsHDU::card(const std::string& keyname) const
299 {
300  return (m_header.at(keyname));
301 }
302 
303 
304 /***********************************************************************//**
305  * @brief Append or update header card
306  *
307  * @param[in] card Header card.
308  ***************************************************************************/
309 inline
311 {
312  m_header.append(card);
313  return;
314 }
315 
316 
317 /***********************************************************************//**
318  * @brief Append or update string value header card
319  *
320  * @param[in] keyname Name of the header card.
321  * @param[in] value String value of the header card.
322  * @param[in] comment Comment of the header card.
323  ***************************************************************************/
324 inline
325 void GFitsHDU::card(const std::string& keyname, const std::string& value,
326  const std::string& comment)
327 {
328  GFitsHeaderCard card(keyname, value, comment);
329  m_header.append(card);
330  return;
331 }
332 
333 
334 /***********************************************************************//**
335  * @brief Append or update double precision value header card
336  *
337  * @param[in] keyname Name of the header card.
338  * @param[in] value Double precision value of the header card.
339  * @param[in] comment Comment of the header card.
340  ***************************************************************************/
341 inline
342 void GFitsHDU::card(const std::string& keyname, const double& value,
343  const std::string& comment)
344 {
345  GFitsHeaderCard card(keyname, value, comment);
346  m_header.append(card);
347  return;
348 }
349 
350 
351 /***********************************************************************//**
352  * @brief Append or update integer value header card
353  *
354  * @param[in] keyname Name of the header card.
355  * @param[in] value Integer value of the header card.
356  * @param[in] comment Comment of the header card.
357  ***************************************************************************/
358 inline
359 void GFitsHDU::card(const std::string& keyname, const int& value,
360  const std::string& comment)
361 {
362  GFitsHeaderCard card(keyname, value, comment);
363  m_header.append(card);
364  return;
365 }
366 
367 
368 /***********************************************************************//**
369  * @brief Append or update boolean value header card
370  *
371  * @param[in] keyname Name of the header card.
372  * @param[in] value Boolean value of the header card.
373  * @param[in] comment Comment of the header card.
374  ***************************************************************************/
375 inline
376 void GFitsHDU::card(const std::string& keyname, const bool& value,
377  const std::string& comment)
378 {
379  GFitsHeaderCard card(keyname, value, comment);
380  m_header.append(card);
381  return;
382 }
383 
384 
385 /***********************************************************************//**
386  * @brief Append or update character value header card
387  *
388  * @param[in] keyname Name of the header card.
389  * @param[in] value Character value of the header card.
390  * @param[in] comment Comment of the header card.
391  ***************************************************************************/
392 inline
393 void GFitsHDU::card(const std::string& keyname, const char* value,
394  const std::string& comment)
395 {
396  std::string string(value);
397  GFitsHeaderCard card(keyname, string, comment);
398  m_header.append(card);
399  return;
400 }
401 
402 
403 /***********************************************************************//**
404  * @brief Return card value as string
405  *
406  * @param[in] keyname Name of header card.
407  * @return String card value.
408  ***************************************************************************/
409 inline
410 std::string GFitsHDU::string(const std::string& keyname) const
411 {
412  return (m_header.at(keyname).string());
413 }
414 
415 
416 /***********************************************************************//**
417  * @brief Return card value as double precision
418  *
419  * @param[in] keyname Name of header card.
420  * @return Double precision card value.
421  ***************************************************************************/
422 inline
423 double GFitsHDU::real(const std::string& keyname) const
424 {
425  return (m_header.at(keyname).real());
426 }
427 
428 
429 /***********************************************************************//**
430  * @brief Return card value as integer
431  *
432  * @param[in] keyname Name of header card.
433  * @return Integer card value.
434  ***************************************************************************/
435 inline
436 int GFitsHDU::integer(const std::string& keyname) const
437 {
438  return (m_header.at(keyname).integer());
439 }
440 
441 #endif /* GFITSHDU_HPP */
bool has_card(const int &cardno) const
Check existence of header card.
Definition: GFitsHDU.hpp:233
std::string string(void) const
Return header card value as string.
std::string print_hdu(const GChatter &chatter=NORMAL) const
Print basic HDU information.
Definition: GFitsHDU.cpp:373
void open(void *vptr, int hdunum)
Open HDU.
Definition: GFitsHDU.cpp:285
Abstract FITS extension base class.
Definition: GFitsHDU.hpp:51
void connect(void *fptr)
Connect HDU to FITS file.
Definition: GFitsHDU.cpp:192
Definition of interface for all GammaLib classes.
virtual HDUType exttype(void) const =0
void move_to_hdu(void)
Move FITS file pointer to HDU.
Definition: GFitsHDU.cpp:222
void * m_fitsfile
FITS file pointer pointing on actual HDU.
Definition: GFitsHDU.hpp:133
FITS file class.
Definition: GFits.hpp:63
std::string m_name
HDU name (extname)
Definition: GFitsHDU.hpp:135
const GFitsHeader & header(void) const
Return extension header.
Definition: GFitsHDU.hpp:205
FITS header card class definition.
const int & extno(void) const
Return extension number.
Definition: GFitsHDU.hpp:176
GFitsHDU & operator=(const GFitsHDU &hdu)
Assignment operator.
Definition: GFitsHDU.cpp:124
int cards(void) const
Return number of cards in HDU header.
Definition: GFitsHDU.hpp:148
virtual void data_open(void *vptr)=0
Implements FITS header card interface.
double real(const std::string &keyname) const
Return card value as double precision.
Definition: GFitsHDU.hpp:423
Interface for FITS header class.
Definition: GFitsHeader.hpp:49
void save(void)
Saves HDU.
Definition: GFitsHDU.cpp:340
virtual void clear(void)=0
Clear object.
GFitsHeaderCard & append(const GFitsHeaderCard &card)
Append or update header card.
Interface class for all GammaLib classes.
Definition: GBase.hpp:52
virtual std::string classname(void) const =0
Return class name.
virtual void data_connect(void *vptr)=0
HDUType get_hdu_type(void) const
Get HDU type from FITS file.
Definition: GFitsHDU.cpp:245
virtual GFitsHDU * clone(void) const =0
Clones object.
int size(void) const
Return number of cards in header.
GChatter
Definition: GTypemaps.hpp:33
int integer(const std::string &keyname) const
Return card value as integer.
Definition: GFitsHDU.hpp:436
GFitsHeader m_header
HDU header.
Definition: GFitsHDU.hpp:136
virtual void data_save(void)=0
const std::string & extname(void) const
Return extension name.
Definition: GFitsHDU.hpp:162
void init_members(void)
Initialise class members.
Definition: GFitsHDU.cpp:462
FITS header cards container class definition.
std::string typecode(int type) const
Return typecode as string.
Definition: GFitsHDU.cpp:398
virtual void data_close(void)=0
double real(void) const
Return header card value as double precision.
GFitsHDU(void)
Void constructor.
Definition: GFitsHDU.cpp:72
virtual std::string print(const GChatter &chatter=NORMAL) const =0
Print content of object.
std::string string(const std::string &keyname) const
Return card value as string.
Definition: GFitsHDU.hpp:410
bool contains(const int &cardno) const
Check if card is present in header.
void copy_members(const GFitsHDU &hdu)
Copy class members.
Definition: GFitsHDU.cpp:489
void free_members(void)
Delete class members.
Definition: GFitsHDU.cpp:505
int integer(void) const
Return header card value as integer.
GFitsHeaderCard & at(const int &cardno)
Return header card.
virtual ~GFitsHDU(void)
Destructor.
Definition: GFitsHDU.cpp:103
GFitsHeaderCard & card(const int &cardno)
Return header card.
Definition: GFitsHDU.hpp:259
int extno(const std::string &extname) const
Return extension number for specified extension name.
Definition: GFits.cpp:990
int m_hdunum
HDU number (starting from 0)
Definition: GFitsHDU.hpp:134