7#ifndef IFW_DIT_FITS_HPP_
8#define IFW_DIT_FITS_HPP_
13#include <CCfits/CCfits.h>
14#include <CCfits/FITS.h>
15#include <CCfits/PHDU.h>
17#include <ciiLogManager.hpp>
19#include <ifw/fnd/defs/dataType.hpp>
20#include <ifw/fnd/defs/fits.hpp>
29 log4cplus::Logger&
Logger();
51 const int cfitsio_status);
82 void CreateFile(std::shared_ptr<CCfits::FITS>& fits_handle,
83 const std::string& filename,
86 const std::vector<int32_t>& naxes,
87 std::string& target_filename,
88 const bool remove_if_exists =
false,
89 const uint16_t nb_of_hdr_blocks = 1);
98 void OpenFitsFile(std::shared_ptr<CCfits::FITS>& fits_handle,
99 const std::string& filename,
100 std::string& target_filename,
101 CCfits::RWmode mode = CCfits::Read);
110 bool KeyInHdu(std::shared_ptr<CCfits::FITS>& fits_handle,
111 const int16_t hdu_nb,
112 const std::string& key);
119 void MoveToHdu(std::shared_ptr<CCfits::FITS>& fits_handle,
120 const int16_t hdu_nb);
142 const std::string& key,
143 const int16_t hdu_nb,
145 std::string& target_key,
158 template <
class TYPE>
159 void AddKey(std::shared_ptr<CCfits::FITS>& fits_handle,
161 const std::string& key,
163 const int16_t hdu_nb =
CUR_HDU) {
164 LOG4CPLUS_TRACE_METHOD(
Logger(), __PRETTY_FUNCTION__);
166 LOG4CPLUS_DEBUG(
Logger(),
"Keyword to add: \"" << key <<
"\". Type: "
167 <<
typeid(value).name() <<
". Value: " << value);
169 if ((
typeid(TYPE) ==
typeid(
double)) || (
typeid(TYPE) ==
typeid(
float))) {
170 throw std::runtime_error(
"Use ifw::core::dit::fits::AddDoubleKey() for floating point type keyword "
171 "cards (" + key +
")");
175 std::string target_key;
177 PrepForAddingKey(fits_handle, dictionary, key, hdu_nb, did_record, target_key, key_in_hdu);
180 fits_get_hdu_num(fits_handle.get()->fitsPointer(), &cur_hdu);
183 fits_handle.get()->pHDU().addKey(target_key, value, did_record.
GetComment());
185 fits_handle.get()->extension(cur_hdu-1).addKey(target_key, value, did_record.
GetComment());
187 }
catch (CCfits::FitsException& ex) {
188 throw std::runtime_error(fmt::format(
"Error adding key: \"{}\". Diagnostics: {}.",
201 void AddDoubleKey(std::shared_ptr<CCfits::FITS>& fits_handle,
203 const std::string& key,
205 const uint16_t hdu_nb);
208 template <
class TYPE>
209 void UpdateKey(std::shared_ptr<CCfits::FITS>& fits_handle,
211 const std::string& key,
213 const int16_t hdu_nb =
CUR_HDU) {
214 LOG4CPLUS_TRACE_METHOD(
Logger(), __PRETTY_FUNCTION__);
216 LOG4CPLUS_DEBUG(
Logger(),
"Keyword to update: \"" << key <<
"\". Type: "
217 <<
typeid(value).name());
222 dictionary.
LookUp(key, record);
225 if (fits_update_key(fits_handle.get()->fitsPointer(), cfitsio_type, key.c_str(), (
void*)&value,
227 std::string err =
"executing cfitsio::fits_update_key()";
232 int ReadIntKey(std::shared_ptr<CCfits::FITS>& fits_handle,
233 const std::string& key);
244 std::string& target_filename,
245 std::string& hdr_buf,
255 std::string& hdr_buf,
267 void StoreImage(std::shared_ptr<CCfits::FITS>& fits_handle,
268 const ifw::fnd::fits::BitPix bitpix,
269 const uint32_t image_size,
270 const void* image_buffer,
272 const int16_t naxis3 = 0);
281 void addHdu(std::shared_ptr<CCfits::FITS>& fits_handle,
282 const std::string extension_name,
283 const ifw::fnd::fits::BitPix bitpix,
284 const std::vector<int32_t>& naxes);
Data Interface Dictionary class.
Definition did.hpp:30
bool LookUp(const std::string &pattern, ifw::core::dit::did::Record &record, const bool allow_idx_subst=true, const bool exception=true) const
Look up a key in the DIDs loaded. First occurrence taken.
Definition did.cpp:221
Data Interface Dictionary keyword record class.
Definition record.hpp:49
ifw::core::dit::did::DataType GetDataType() const
Return data type defined for the keyword.
Definition record.cpp:96
const std::string & GetComment() const
Get the comment defined.
Definition record.cpp:111
DataType
Dictionary data types.
Definition defines.hpp:56
const int APPEND
Definition fits.hpp:34
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:382
void UpdateKey(std::shared_ptr< CCfits::FITS > &fits_handle, const ifw::core::dit::did::Did &dictionary, const std::string &key, const TYPE &value, const int16_t hdu_nb=CUR_HDU)
Definition fits.hpp:209
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:181
void CreateFile(std::shared_ptr< CCfits::FITS > &fits_handle, const std::string &filename, const ifw::core::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:85
const int CUR_HDU
Definition fits.hpp:33
const int BITS_PER_PIXEL
Definition fits.hpp:38
std::string GenerateKey(const std::string &key, const std::string &hierarch_prefix)
Generates an ESO hierarchical keyword.
Definition fits.cpp:73
int32_t CfitsioType
Definition fits.hpp:31
void AddKey(std::shared_ptr< CCfits::FITS > &fits_handle, const ifw::core::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:159
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:32
int DidToCfitsioType(const ifw::core::dit::did::DataType did_type)
Convert an ELT ICS data type to the corresponding cfitsio data type.
Definition fits.cpp:55
void addHdu(std::shared_ptr< CCfits::FITS > &fits_handle, const std::string extension_name, const ifw::fnd::fits::BitPix bitpix, const std::vector< int32_t > &naxes)
Definition fits.cpp:482
void AddDoubleKey(std::shared_ptr< CCfits::FITS > &fits_handle, const ifw::core::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:226
const int16_t ALL_HEADERS
Definition fits.hpp:37
log4cplus::Logger & Logger()
Definition fits.cpp:22
void StoreImage(std::shared_ptr< CCfits::FITS > &fits_handle, const ifw::fnd::fits::BitPix bitpix, const uint32_t image_size, const void *image_buffer, const int16_t hdu_nb, const int16_t naxis3)
Definition fits.cpp:414
const std::string FITS_KEY_ESO_HIERARCH
Definition fits.hpp:36
void PrepForAddingKey(std::shared_ptr< CCfits::FITS > &fits_handle, const ifw::core::dit::did::Did &dictionary, const std::string &key, const int16_t hdu_nb, ifw::core::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:286
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:317
int ReadIntKey(std::shared_ptr< CCfits::FITS > &fits_handle, const std::string &key)
Definition fits.cpp:219
void MoveToHdu(std::shared_ptr< CCfits::FITS > &fits_handle, const int16_t hdu_nb)
Move to the given HDU.
Definition fits.cpp:165