6#ifndef IFW_DIT_FITS_HPP_
7#define IFW_DIT_FITS_HPP_
12#include <CCfits/CCfits.h>
13#include <CCfits/FITS.h>
14#include <CCfits/PHDU.h>
16#include <ciiLogManager.hpp>
18#include <ifw/fnd/defs/dataType.hpp>
19#include <ifw/fnd/defs/fits.hpp>
28 log4cplus::Logger&
Logger();
50 const int cfitsio_status);
81 void CreateFile(std::shared_ptr<CCfits::FITS>& fits_handle,
82 const std::string& filename,
85 const std::vector<int32_t>& naxes,
86 std::string& target_filename,
87 const bool remove_if_exists =
false,
88 const uint16_t nb_of_hdr_blocks = 1);
97 void OpenFitsFile(std::shared_ptr<CCfits::FITS>& fits_handle,
98 const std::string& filename,
99 std::string& target_filename,
100 CCfits::RWmode mode = CCfits::Read);
109 bool KeyInHdu(std::shared_ptr<CCfits::FITS>& fits_handle,
110 const int16_t hdu_nb,
111 const std::string& key);
118 void MoveToHdu(std::shared_ptr<CCfits::FITS>& fits_handle,
119 const int16_t hdu_nb);
141 const std::string& key,
142 const int16_t hdu_nb,
144 std::string& target_key,
157 template <
class TYPE>
158 void AddKey(std::shared_ptr<CCfits::FITS>& fits_handle,
160 const std::string& key,
162 const int16_t hdu_nb =
CUR_HDU) {
163 LOG4CPLUS_TRACE_METHOD(
Logger(), __PRETTY_FUNCTION__);
165 LOG4CPLUS_DEBUG(
Logger(),
"Keyword to add: \"" << key <<
"\". Type: "
166 <<
typeid(value).name() <<
". Value: " << value);
168 if ((
typeid(TYPE) ==
typeid(
double)) || (
typeid(TYPE) ==
typeid(
float))) {
169 throw std::runtime_error(
"Use ifw::core::dit::fits::AddDoubleKey() for floating point type keyword "
170 "cards (" + key +
")");
174 std::string target_key;
176 PrepForAddingKey(fits_handle, dictionary, key, hdu_nb, did_record, target_key, key_in_hdu);
179 fits_get_hdu_num(fits_handle.get()->fitsPointer(), &cur_hdu);
182 fits_handle.get()->pHDU().addKey(target_key, value, did_record.
GetComment());
184 fits_handle.get()->extension(cur_hdu-1).addKey(target_key, value, did_record.
GetComment());
186 }
catch (CCfits::FitsException& ex) {
187 throw std::runtime_error(fmt::format(
"Error adding key: \"{}\". Diagnostics: {}.",
200 void AddDoubleKey(std::shared_ptr<CCfits::FITS>& fits_handle,
202 const std::string& key,
204 const uint16_t hdu_nb);
207 template <
class TYPE>
208 void UpdateKey(std::shared_ptr<CCfits::FITS>& fits_handle,
210 const std::string& key,
212 const int16_t hdu_nb =
CUR_HDU) {
213 LOG4CPLUS_TRACE_METHOD(
Logger(), __PRETTY_FUNCTION__);
215 LOG4CPLUS_DEBUG(
Logger(),
"Keyword to update: \"" << key <<
"\". Type: "
216 <<
typeid(value).name());
221 dictionary.
LookUp(key, record);
224 if (fits_update_key(fits_handle.get()->fitsPointer(), cfitsio_type, key.c_str(), (
void*)&value,
226 std::string err =
"executing cfitsio::fits_update_key()";
231 int ReadIntKey(std::shared_ptr<CCfits::FITS>& fits_handle,
232 const std::string& key);
243 std::string& target_filename,
244 std::string& hdr_buf,
254 std::string& hdr_buf,
266 void StoreImage(std::shared_ptr<CCfits::FITS>& fits_handle,
267 const ifw::fnd::fits::BitPix bitpix,
268 const uint32_t image_size,
269 const void* image_buffer,
271 const int16_t naxis3 = 0);
280 void addHdu(std::shared_ptr<CCfits::FITS>& fits_handle,
281 const std::string extension_name,
282 const ifw::fnd::fits::BitPix bitpix,
283 const std::vector<int32_t>& naxes);
Data Interface Dictionary class.
Definition did.hpp:29
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:219
Data Interface Dictionary keyword record class.
Definition record.hpp:46
ifw::core::dit::did::DataType GetDataType() const
Return data type defined for the keyword.
Definition record.cpp:95
const std::string & GetComment() const
Get the comment defined.
Definition record.cpp:110
DataType
Dictionary data types.
Definition defines.hpp:61
const int APPEND
Definition fits.hpp:33
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:381
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:208
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:180
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:84
const int CUR_HDU
Definition fits.hpp:32
const int BITS_PER_PIXEL
Definition fits.hpp:37
std::string GenerateKey(const std::string &key, const std::string &hierarch_prefix)
Generates an ESO hierarchical keyword.
Definition fits.cpp:72
int32_t CfitsioType
Definition fits.hpp:30
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:158
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:31
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:54
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:481
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:225
const int16_t ALL_HEADERS
Definition fits.hpp:36
log4cplus::Logger & Logger()
Definition fits.cpp:21
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:413
const std::string FITS_KEY_ESO_HIERARCH
Definition fits.hpp:35
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:285
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:316
int ReadIntKey(std::shared_ptr< CCfits::FITS > &fits_handle, const std::string &key)
Definition fits.cpp:218
void MoveToHdu(std::shared_ptr< CCfits::FITS > &fits_handle, const int16_t hdu_nb)
Move to the given HDU.
Definition fits.cpp:164