ifw-core  2.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
fits.hpp
Go to the documentation of this file.
1 
9 #ifndef IFW_DIT_FITS_HPP_
10 #define IFW_DIT_FITS_HPP_
11 
12 #include <string>
13 
14 #include <fitsio.h>
15 #include <CCfits/CCfits.h>
16 #include <CCfits/FITS.h>
17 #include <CCfits/PHDU.h>
18 
19 #include "ctd/defines/defines.hpp"
20 
21 #include "dit/did/Did.hpp"
22 
23 
24 namespace dit {
25  namespace fits {
26 
27  using CfitsioType = int32_t;
28 
29  const std::string FITS_KEY_ESO_HIERARCH = "HIERARCH ESO";
30  const int16_t ALL_HEADERS = SHRT_MAX;
31 
38 
39 
43  void HandleCfitsioError(const std::string& operation,
44  const int cfitsio_status);
45 
46 
56  std::string GenerateKey(const std::string& key,
57  const std::string& hierarch_prefix = FITS_KEY_ESO_HIERARCH);
58 
59 
72  void CreateFile(std::shared_ptr<CCfits::FITS>& fits_handle,
73  const std::string& filename,
74  const dit::did::Did& dictionary,
75  const int32_t bitpix,
76  const std::vector<int32_t>& naxes,
77  std::string& target_filename,
78  const bool remove_if_exists = false,
79  const uint16_t nb_of_hdr_blocks = 1);
80 
88  void OpenFitsFile(std::shared_ptr<CCfits::FITS>& fits_handle,
89  const std::string& filename,
90  std::string& target_filename,
91  CCfits::RWmode mode = CCfits::Read);
92 
100  bool KeyInHdu(std::shared_ptr<CCfits::FITS>& fits_handle,
101  const int16_t hdu_nb,
102  const std::string& key);
103 
109  void MoveToHdu(std::shared_ptr<CCfits::FITS>& fits_handle,
110  const int16_t hdu_nb);
111 
126  void PrepForAddingKey(std::shared_ptr<CCfits::FITS>& fits_handle,
127  const dit::did::Did& dictionary,
128  const std::string& key,
129  const int16_t hdu_nb,
130  dit::did::Record& did_record,
131  std::string& target_key,
132  bool& key_in_hdu,
133  CfitsioType& cfitsio_type,
134  dit::did::FormatSpecifier& format_specifier);
135 
146  // TODO: Move template code to separate header files.
147  template <class TYPE>
148  void AddKey(std::shared_ptr<CCfits::FITS>& fits_handle,
149  const dit::did::Did& dictionary,
150  const std::string& key,
151  const TYPE& value,
152  const int16_t hdu_nb) {
153  RAD_LOG_TRACE();
154 
155  RAD_LOG_DEBUG1() << "Keyword to add: \"" << key << "\". Type: " << typeid(value).name();
156 
157  if ((typeid(TYPE) == typeid(double)) || (typeid(TYPE) == typeid(float))) {
159  throw rad::Exception("Use dit::fits::AddDoubleKey() for floating point "
160  "type keyword cards");
161  }
162 
163  dit::did::Record did_record;
164  std::string target_key;
165  bool key_in_hdu;
166  CfitsioType cfitsio_type;
167  dit::did::FormatSpecifier format_spec;
168  PrepForAddingKey(fits_handle, dictionary, key, hdu_nb, did_record, target_key, key_in_hdu,
169  cfitsio_type, format_spec);
170 
171  try {
172  if (hdu_nb == 1) {
173  fits_handle.get()->pHDU().addKey(target_key, value, did_record.GetComment());
174  } else {
175  fits_handle.get()->extension(hdu_nb).addKey(target_key, value, did_record.GetComment());
176  }
177  } catch (CCfits::FitsException& ex) {
178  throw rad::Exception(ifw::Write("Error adding key: \"%s\". Diagnostics: %s.",
179  key.c_str(), ex.message().c_str()));
180  }
181  }
182 
183 
192  void AddDoubleKey(std::shared_ptr<CCfits::FITS>& fits_handle,
193  const dit::did::Did& dictionary,
194  const std::string& key,
195  const double value,
196  const uint16_t hdu_nb);
197 
198 
199  class FitsReport {
200  public:
203  private:
204  };
205 
206 
217  void Verify(const std::string& filename,
218  std::string& target_filename,
219  dit::fits::FitsReport& report);
220 
228  void Verify(const std::string& filename,
229  std::string& target_filename,
230  const dit::did::Did& dictionary,
231  dit::fits::FitsReport& report);
232 
241  void ExtractHeaders(const std::string& filename,
242  std::string& target_filename,
243  std::string& hdr_buf,
244  const int16_t hdr_ref = ALL_HEADERS);
245 
252  void ExtractHeaders(std::shared_ptr<CCfits::FITS>& fits_handle,
253  std::string& hdr_buf,
254  const int16_t hdr_ref = ALL_HEADERS);
255 
256  }
257 }
258 
259 #endif // IFW_DIT_FITS_HPP_
void OpenFitsFile(std::shared_ptr< CCfits::FITS > &fits_handle, const std::string &filename, std::string &target_filename, CCfits::RWmode mode)
Open an existing FITS file.
Definition: fits.cpp:331
class to handle the format specifier of a DID record.
Definition: Record.hpp:74
const std::string & GetComment() const
Get the comment defined.
Definition: Record.cpp:306
Common definitions.
void MoveToHdu(std::shared_ptr< CCfits::FITS > &fits_handle, const int16_t hdu_nb)
Move to the given HDU.
Definition: fits.cpp:136
DataType
Data types numeric representations.
Definition: types.hpp:34
void AddDoubleKey(std::shared_ptr< CCfits::FITS > &fits_handle, const dit::did::Did &dictionary, const std::string &key, const double value, const uint16_t hdu_nb)
Add a double type key in an existing FITS file.
Definition: fits.cpp:189
void Verify(const std::string &filename, std::string &target_filename, dit::fits::FitsReport &report)
Function to carry out a verification check on a FITS file.
Definition: fits.cpp:72
CfitsioType DidToCfitsioType(ctd::defines::DataType did_type)
Convert an ELT ICS data type to the corresponding cfitsio data type.
Definition: fits.cpp:14
std::string GenerateKey(const std::string &key, const std::string &hierarch_prefix)
Generates an ESO hierarchical keyword.
Definition: fits.cpp:58
const std::string FITS_KEY_ESO_HIERARCH
Definition: fits.hpp:29
~FitsReport()
Definition: fits.hpp:202
FitsReport()
Definition: fits.hpp:201
std::string Write(const char *format,...)
Create formatted string, return formatted string (C formatting convention).
Definition: defines.cpp:54
Class to handle Data Interface Dictionaries.
bool KeyInHdu(std::shared_ptr< CCfits::FITS > &fits_handle, const int16_t hdu_nb, const std::string &key)
Check if a given key is contained in the referenced HDU (primary HDU = 1).
Definition: fits.cpp:150
void AddKey(std::shared_ptr< CCfits::FITS > &fits_handle, const dit::did::Did &dictionary, const std::string &key, const TYPE &value, const int16_t hdu_nb)
Template function to add a keyword card in an existing FITS file.
Definition: fits.hpp:148
void CreateFile(std::shared_ptr< CCfits::FITS > &fits_handle, const std::string &filename, const dit::did::Did &dictionary, const int32_t bitpix, const std::vector< int32_t > &naxes, std::string &target_filename, const bool remove_if_exists, const uint16_t nb_of_hdr_blocks)
Create a new FITS file.
Definition: fits.cpp:91
void HandleCfitsioError(const std::string &operation, const int cfitsio_status)
Throw an exception, extracting the relevant info from cfitsio, if status != 0.
Definition: fits.cpp:33
Data Interface Dictionary keyword record class.
Definition: Record.hpp:158
void PrepForAddingKey(std::shared_ptr< CCfits::FITS > &fits_handle, const dit::did::Did &dictionary, const std::string &key, const int16_t hdu_nb, dit::did::Record &did_record, std::string &target_key, bool &key_in_hdu, CfitsioType &cfitsio_type, dit::did::FormatSpecifier &format_specifier)
Used to prepare for adding a keyword card in a FITS file (mostly internal usage). ...
Definition: fits.cpp:241
const int16_t ALL_HEADERS
Definition: fits.hpp:30
Definition: fits.hpp:199
Data Interface Dictionary class.
Definition: Did.hpp:36
int32_t CfitsioType
Definition: fits.hpp:27
void ExtractHeaders(std::shared_ptr< CCfits::FITS > &fits_handle, std::string &hdr_buf, const int16_t hdr_ref)
Extract the keyword cards in one or more HDU&#39;s in an ASCII format (newline terminated).
Definition: fits.cpp:269