ifw-core  5.0.0-pre2
fits.hpp
Go to the documentation of this file.
1 
6 #ifndef IFW_DIT_FITS_HPP_
7 #define IFW_DIT_FITS_HPP_
8 
9 #include <string>
10 
11 #include <fitsio.h>
12 #include <CCfits/CCfits.h>
13 #include <CCfits/FITS.h>
14 #include <CCfits/PHDU.h>
15 
16 #include <core/utils/base/base.hpp>
17 
18 #include <core/dit/did/did.hpp>
19 
20 
21 namespace core::dit::fits {
22 
23  //static constexpr const char* LOG_PROPERTIES_FILE = "config/core/dit/fitslib/log.properties";
25 
26  using CfitsioType = int32_t;
27 
28  const int CUR_HDU = -1;
29  const int APPEND = -2;
30 
31  const std::string FITS_KEY_ESO_HIERARCH = "HIERARCH ESO";
32  const int16_t ALL_HEADERS = SHRT_MAX;
33  const int BITS_PER_PIXEL = 8;
34 
35 
36 
42  //CfitsioType DidToCfitsioType(const core::dit::did::DataType did_type);
43 
47  void HandleCfitsioError(const std::string& operation,
48  const int cfitsio_status);
49 
59  std::string GenerateKey(const std::string& key,
60  const std::string& hierarch_prefix = FITS_KEY_ESO_HIERARCH);
61 
79  void CreateFile(std::shared_ptr<CCfits::FITS>& fits_handle,
80  const std::string& filename,
81  const dit::did::Did& dictionary,
82  const int32_t bitpix,
83  const std::vector<int32_t>& naxes,
84  std::string& target_filename,
85  const bool remove_if_exists = false,
86  const uint16_t nb_of_hdr_blocks = 1);
87 
95  void OpenFitsFile(std::shared_ptr<CCfits::FITS>& fits_handle,
96  const std::string& filename,
97  std::string& target_filename,
98  CCfits::RWmode mode = CCfits::Read);
99 
107  bool KeyInHdu(std::shared_ptr<CCfits::FITS>& fits_handle,
108  const int16_t hdu_nb,
109  const std::string& key);
110 
116  void MoveToHdu(std::shared_ptr<CCfits::FITS>& fits_handle,
117  const int16_t hdu_nb);
118 
119  struct FormatSpec {
120  std::string m_prefix;
122  std::string m_format;
123  };
124 
137  void PrepForAddingKey(std::shared_ptr<CCfits::FITS>& fits_handle,
138  const dit::did::Did& dictionary,
139  const std::string& key,
140  const int16_t hdu_nb,
141  dit::did::Record& did_record,
142  std::string& target_key,
143  bool& key_in_hdu);
144 
155  // TODO: Move template code to separate header files.
156  template <class TYPE>
157  void AddKey(std::shared_ptr<CCfits::FITS>& fits_handle,
158  const dit::did::Did& dictionary,
159  const std::string& key,
160  const TYPE& value,
161  const int16_t hdu_nb = CUR_HDU) {
162  LOG4CPLUS_TRACE_METHOD(Logger(), __PRETTY_FUNCTION__);
163 
164  LOG4CPLUS_DEBUG(Logger(), "Keyword to add: \"" << key << "\". Type: "
165  << typeid(value).name());
166 
167  if ((typeid(TYPE) == typeid(double)) || (typeid(TYPE) == typeid(float))) {
168  throw std::runtime_error("Use dit::fits::AddDoubleKey() for floating point type keyword "
169  "cards (" + key + ")");
170  }
171 
172  dit::did::Record did_record;
173  std::string target_key;
174  bool key_in_hdu;
175  PrepForAddingKey(fits_handle, dictionary, key, hdu_nb, did_record, target_key, key_in_hdu);
176 
177  int cur_hdu;
178  fits_get_hdu_num(fits_handle.get()->fitsPointer(), &cur_hdu);
179  try {
180  if (cur_hdu == 1) {
181  fits_handle.get()->pHDU().addKey(target_key, value, did_record.GetComment());
182  } else {
183  fits_handle.get()->extension(cur_hdu-1).addKey(target_key, value, did_record.GetComment());
184  }
185  } catch (CCfits::FitsException& ex) {
186  throw std::runtime_error(fmt::format("Error adding key: \"{}\". Diagnostics: {}.",
187  key, ex.message()));
188  }
189  }
190 
191  int ReadIntKey(std::shared_ptr<CCfits::FITS>& fits_handle,
192  const std::string& key);
193 
202  void AddDoubleKey(std::shared_ptr<CCfits::FITS>& fits_handle,
203  const dit::did::Did& dictionary,
204  const std::string& key,
205  const double value,
206  const uint16_t hdu_nb);
207 
216  void ExtractHeaders(const std::string& filename,
217  std::string& target_filename,
218  std::string& hdr_buf,
219  const int16_t hdr_ref = ALL_HEADERS);
220 
227  void ExtractHeaders(std::shared_ptr<CCfits::FITS>& fits_handle,
228  std::string& hdr_buf,
229  const int16_t hdr_ref = ALL_HEADERS);
230 
240  void StoreImage(std::shared_ptr<CCfits::FITS>& fits_handle,
241  int8_t bitpix,
242  uint32_t image_size,
243  const void* image_buffer,
244  int16_t hdu_nb = core::dit::fits::CUR_HDU,
245  int16_t naxis3 = 0);
249  int BitpixToCfitsioDataType(const int8_t bitpix);
250 
258  void addHDU(std::shared_ptr<CCfits::FITS>& fits_handle, std::string extensionName,
259  int8_t bitpix, const std::vector<int32_t>& naxes);
260 }
261 
262 #endif // IFW_DIT_FITS_HPP_
Data Interface Dictionary class.
Definition: did.hpp:32
Data Interface Dictionary keyword record class.
Definition: record.hpp:46
const std::string & GetComment() const
Get the comment defined.
Definition: record.cpp:107
log4cplus::Logger & Logger()
Definition: defines.cpp:13
Definition: fits.cpp:16
const int BITS_PER_PIXEL
Definition: fits.hpp:33
int32_t CfitsioType
Definition: fits.hpp:26
int ReadIntKey(std::shared_ptr< CCfits::FITS > &fits_handle, const std::string &key)
Definition: fits.cpp:178
const int16_t ALL_HEADERS
Definition: fits.hpp:32
const int CUR_HDU
Definition: fits.hpp:28
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=CUR_HDU)
Template function to add a keyword card in an existing FITS file.
Definition: fits.hpp:157
log4cplus::Logger & Logger()
Definition: fits.cpp:18
int BitpixToCfitsioDataType(const int8_t bitpix)
Definition: fits.cpp:446
const std::string FITS_KEY_ESO_HIERARCH
Definition: fits.hpp:31
const int APPEND
Definition: fits.hpp:29
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:65
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's in an ASCII format (newline terminated).
Definition: fits.cpp:275
std::string GenerateKey(const std::string &key, const std::string &hierarch_prefix)
Generates an ESO hierarchical keyword.
Definition: fits.cpp:53
void addHDU(std::shared_ptr< CCfits::FITS > &fits_handle, std::string extensionName, int8_t bitpix, const std::vector< int32_t > &naxes)
Definition: fits.cpp:435
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:340
void HandleCfitsioError(const std::string &operation, const int cfitsio_status)
Convert an ELT ICS data type to the corresponding cfitsio data type.
Definition: fits.cpp:30
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)
Used to prepare for adding a keyword card in a FITS file (mostly internal usage).
Definition: fits.cpp:245
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:140
void StoreImage(std::shared_ptr< CCfits::FITS > &fits_handle, int8_t bitpix, uint32_t image_size, const void *image_buffer, int16_t hdu_nb, int16_t naxis3)
Definition: fits.cpp:372
void MoveToHdu(std::shared_ptr< CCfits::FITS > &fits_handle, const int16_t hdu_nb)
Move to the given HDU.
Definition: fits.cpp:124
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:185
Definition: fits.hpp:119
std::string m_format
Definition: fits.hpp:122
int m_precision
Definition: fits.hpp:121
std::string m_prefix
Definition: fits.hpp:120