ifw-core 6.0.0
Loading...
Searching...
No Matches
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 <ciiLogManager.hpp>
17
18#include <ifw/fnd/defs/dataType.hpp>
19#include <ifw/fnd/defs/fits.hpp>
20
22
24
25
26namespace ifw::core::dit::fits {
27
28 log4cplus::Logger& Logger();
29
30 using CfitsioType = int32_t;
31
32 const int CUR_HDU = -1;
33 const int APPEND = -2;
34
35 const std::string FITS_KEY_ESO_HIERARCH = "HIERARCH ESO";
36 const int16_t ALL_HEADERS = SHRT_MAX;
37 const int BITS_PER_PIXEL = 8;
38
45
49 void HandleCfitsioError(const std::string& operation,
50 const int cfitsio_status);
51
61 std::string GenerateKey(const std::string& key,
62 const std::string& hierarch_prefix = FITS_KEY_ESO_HIERARCH);
63
81 void CreateFile(std::shared_ptr<CCfits::FITS>& fits_handle,
82 const std::string& filename,
83 const ifw::core::dit::did::Did& dictionary,
84 const int32_t bitpix,
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);
89
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);
101
109 bool KeyInHdu(std::shared_ptr<CCfits::FITS>& fits_handle,
110 const int16_t hdu_nb,
111 const std::string& key);
112
118 void MoveToHdu(std::shared_ptr<CCfits::FITS>& fits_handle,
119 const int16_t hdu_nb);
120
121 struct FormatSpec {
122 std::string m_prefix;
124 std::string m_format;
125 };
126
139 void PrepForAddingKey(std::shared_ptr<CCfits::FITS>& fits_handle,
140 const ifw::core::dit::did::Did& dictionary,
141 const std::string& key,
142 const int16_t hdu_nb,
143 ifw::core::dit::did::Record& did_record,
144 std::string& target_key,
145 bool& key_in_hdu);
146
157 template <class TYPE>
158 void AddKey(std::shared_ptr<CCfits::FITS>& fits_handle,
159 const ifw::core::dit::did::Did& dictionary,
160 const std::string& key,
161 const TYPE& value,
162 const int16_t hdu_nb = CUR_HDU) {
163 LOG4CPLUS_TRACE_METHOD(Logger(), __PRETTY_FUNCTION__);
164
165 LOG4CPLUS_DEBUG(Logger(), "Keyword to add: \"" << key << "\". Type: "
166 << typeid(value).name() << ". Value: " << value);
167
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 + ")");
171 }
172
174 std::string target_key;
175 bool key_in_hdu;
176 PrepForAddingKey(fits_handle, dictionary, key, hdu_nb, did_record, target_key, key_in_hdu);
177
178 int cur_hdu;
179 fits_get_hdu_num(fits_handle.get()->fitsPointer(), &cur_hdu);
180 try {
181 if (cur_hdu == 1) {
182 fits_handle.get()->pHDU().addKey(target_key, value, did_record.GetComment());
183 } else {
184 fits_handle.get()->extension(cur_hdu-1).addKey(target_key, value, did_record.GetComment());
185 }
186 } catch (CCfits::FitsException& ex) {
187 throw std::runtime_error(fmt::format("Error adding key: \"{}\". Diagnostics: {}.",
188 key, ex.message()));
189 }
190 }
191
200 void AddDoubleKey(std::shared_ptr<CCfits::FITS>& fits_handle,
201 const ifw::core::dit::did::Did& dictionary,
202 const std::string& key,
203 const double value,
204 const uint16_t hdu_nb);
205
207 template <class TYPE>
208 void UpdateKey(std::shared_ptr<CCfits::FITS>& fits_handle,
209 const ifw::core::dit::did::Did& dictionary,
210 const std::string& key,
211 const TYPE& value,
212 const int16_t hdu_nb = CUR_HDU) {
213 LOG4CPLUS_TRACE_METHOD(Logger(), __PRETTY_FUNCTION__);
214
215 LOG4CPLUS_DEBUG(Logger(), "Keyword to update: \"" << key << "\". Type: "
216 << typeid(value).name());
217
218 MoveToHdu(fits_handle, hdu_nb);
219
221 dictionary.LookUp(key, record);
222 int cfitsio_type = DidToCfitsioType(record.GetDataType());
223 int32_t status = 0;
224 if (fits_update_key(fits_handle.get()->fitsPointer(), cfitsio_type, key.c_str(), (void*)&value,
225 record.GetComment().c_str(), &status) != 0) {
226 std::string err = "executing cfitsio::fits_update_key()";
227 HandleCfitsioError(err, status);
228 }
229 }
230
231 int ReadIntKey(std::shared_ptr<CCfits::FITS>& fits_handle,
232 const std::string& key);
233
242 void ExtractHeaders(const std::string& filename,
243 std::string& target_filename,
244 std::string& hdr_buf,
245 const int16_t hdr_ref = ALL_HEADERS);
246
253 void ExtractHeaders(std::shared_ptr<CCfits::FITS>& fits_handle,
254 std::string& hdr_buf,
255 const int16_t hdr_ref = ALL_HEADERS);
256
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,
270 const int16_t hdu_nb = ifw::core::dit::fits::CUR_HDU,
271 const int16_t naxis3 = 0);
272
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);
284}
285
286#endif // IFW_DIT_FITS_HPP_
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
Definition fits.cpp:19
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
Definition fits.hpp:121
std::string m_format
Definition fits.hpp:124
int m_precision
Definition fits.hpp:123
std::string m_prefix
Definition fits.hpp:122