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