ifw-daq  2.1.0-pre1
IFW Data Acquisition modules
cfitsio.hpp
Go to the documentation of this file.
1 /**
2  * @file
3  * @ingroup daq_ocm_libfits
4  * @copyright 2022 ESO - European Southern Observatory
5  *
6  * @brief Contains functions and data structures related to cfitsio.
7  */
8 #ifndef DAQ_OCM_DAQ_FITS_CFITSIO_HPP_
9 #define DAQ_OCM_DAQ_FITS_CFITSIO_HPP_
10 #include <memory>
11 #include <optional>
12 
13 #include <cfitsio/fitsio.h>
14 
15 #include <daq/fits/keyword.hpp>
16 
17 namespace daq::fits {
18 enum class HduType : int {
19  Image = IMAGE_HDU,
20  Ascii = ASCII_TBL,
21  Binary = BINARY_TBL,
22 };
23 
24 
25 enum class OpenMode : uint8_t { ReadOnly, ReadWrite };
26 
27 /**
28  * Defines unique ownership type to cfitsio fitsfile.
29  *
30  * @ingroup daq_ocm_libfits
31  */
32 using UniqueFitsFile = std::unique_ptr<fitsfile, void (*)(fitsfile*) noexcept>;
33 
34 /**
35  * In-memory FITS file.
36  */
38 public:
39  /**
40  * Creates empty file.
41  */
42  MemoryFitsFile() noexcept;
43  /**
44  * Creates empty file.
45  */
46  explicit MemoryFitsFile(size_t initial_buffer_size);
47  explicit MemoryFitsFile(MemoryFitsFile&& other) noexcept = default;
48  MemoryFitsFile& operator=(MemoryFitsFile&& other) noexcept = default;
49 
50  /**
51  * Get fits pointer
52  *
53  * @return In-memory FITS file pointer.
54  */
55  fitsfile* GetFile() const noexcept {
56  return m_file.get();
57  }
58 
59  /**
60  * Move out ptr.
61  *
62  * @warning Object lifetime of this object must exeed the UniqueFits file as it
63  * references memory owned associated by this object.
64  *
65  * @return Owned FITS file.
66  */
68  return std::move(m_file);
69  }
70 
71 private:
72  /** In-memory buffer used by FITS */
73  struct FitsMemory {
74  FitsMemory(size_t initial_size);
75  ~FitsMemory() noexcept;
76  void* buffer;
77  size_t size;
78  };
79 
80  std::unique_ptr<FitsMemory> m_memory;
81  UniqueFitsFile m_file;
82 };
83 
84 /**
85  * Represents errors from cfitsio.
86  *
87  * @ingroup daq_ocm_libfits
88  */
89 class CfitsioError : public std::runtime_error {
90 public:
91  CfitsioError(int status, char const* message);
92  CfitsioError(int status, std::string const& message);
93 
94  /**
95  * @return cfitsio status code causing the exception.
96  */
97  int GetStatus() const noexcept;
98 
99 private:
100  int m_status;
101 };
102 
103 /**
104  * Select current HDU number.
105  * @param hdu_num HDU number starting with primary HDU == 1.
106  */
107 void SelectHduNum(fitsfile* ptr, int hdu_num);
108 
109 /**
110  * Default close function that is used by UniqueFitsFile as a deleter.
111  *
112  * @ingroup daq_ocm_libfits
113  */
114 void DefaultClose(fitsfile* ptr) noexcept;
115 
116 /**
117  * Creates empty FITS file using @c fits_create_file and returns a pointer with a deleter that will
118  * close the file.
119  *
120  * @ingroup daq_ocm_libfits
121  */
122 UniqueFitsFile CreateEmpty(char const* filename);
123 
124 /**
125  * Open file
126  *
127  * @param filename Path to file to open.
128  * @param mode File open mode.
129  * @throw CfitsioError on errors.
130  */
131 UniqueFitsFile Open(char const* filename, OpenMode mode);
132 
133 /**
134  * Initializes an empty FITS file with an empty primary HDU (no keywords)
135  *
136  * @pre ptr != nullptr
137  *
138  * @param ptr Valid FITS file.
139  * @throw CfitsioError on error.
140  */
141 void InitPrimaryHduEmpty(fitsfile* ptr);
142 
143 /**
144  * Initializes an empty FITS file with a primary HDU
145  *
146  * Mandatory keywords are set and primary HDU added if necessary.
147  *
148  * Mandatory keywords are initialized as:
149  * - SIMPLE = T
150  * - BITPIX = 8
151  * - NAXIS = 0
152  *
153  * @pre ptr != nullptr
154  *
155  * @param ptr Valid FITS file.
156  * @throw CfitsioError on error.
157  */
158 void InitPrimaryHduNoImage(fitsfile* ptr);
159 
160 /**
161  * Read keywords from HDU identifed by absolute position @a hdu_num.
162  *
163  * @param ptr FITS file to read from.
164  * @param hdu_num HDU number. HDU numbers are 1-indexed, so primary HDU is 1.
165  * @throw CfitsioError on error.
166  */
167 std::vector<LiteralKeyword> ReadKeywords(fitsfile* ptr, int hdu_num);
168 
169 /**
170  * Delete all keywords from HDU
171  *
172  * @param ptr FITS file to delete keywords from.
173  * @param hdu_num HDU number. HDU numbers are 1-indexed, so primary HDU is 1.
174  * @throw CfitsioError on error.
175  */
176 void DeleteAllKeywords(fitsfile* ptr, int hdu_num);
177 
178 /**
179  * Read keywords from HDU identifed by @a EXTNAME and @c EXTVER keywords.
180  *
181  * @param ptr FITS file to read from.
182  * @param name HDU extension name as identified by @c EXTNAME keyword.
183  * @param version HDU extension version as identified by @c EXTVER keyword. If version is not
184  * provided the @c EXTVER will be ignored when looking up extension.
185  * @throw CfitsioError on error.
186  */
187 std::vector<LiteralKeyword>
188 ReadKeywords(fitsfile* ptr, std::string_view name, std::optional<int> version = std::nullopt);
189 
190 /**
191  * Write keywords to HDU identified by number @a hdu_num.
192  *
193  * @param ptr FITS file to write to.
194  * @param hdu_num HDU number. HDU numbers are 1-indexed, so primary HDU is 1.
195  * @param keywords Keywords to write.
196  * @param [out] remaining_size optional parameter that is updated with remaining size in HDU before
197  * any allocations. If this number is negative it means the HDU required to be allocated with an
198  * associated performance penalty.
199  *
200  * @throw CfitsioError on error.
201  */
202 void WriteKeywords(fitsfile* ptr,
203  int hdu_num,
204  std::vector<LiteralKeyword> const& keywords,
205  std::optional<ssize_t>* remaining_size = nullptr);
206 
207 /**
208  * Write or update checksum keywords DATASUM and CHECKSUM to HDU specified by @a hdu_num.
209  *
210  * @param hdu_num HDU to calculate and update checksum keywords for if necessary.
211  * @throw CfitsioError on error.
212  */
213 void WriteChecksum(fitsfile* ptr, int hdu_num);
214 
215 } // namespace daq::fits
216 
217 #endif // #define DAQ_OCM_DAQ_FITS_CFITSIO_HPP_
daq::fits::MemoryFitsFile
In-memory FITS file.
Definition: cfitsio.hpp:37
daq::fits::ReadKeywords
std::vector< LiteralKeyword > ReadKeywords(fitsfile *ptr, int hdu_num)
Read keywords from HDU identifed by absolute position hdu_num.
Definition: cfitsio.cpp:181
daq::fits::WriteKeywords
void WriteKeywords(fitsfile *ptr, int hdu_num, std::vector< LiteralKeyword > const &keywords, std::optional< ssize_t > *remaining_size)
Write keywords to HDU identified by number hdu_num.
Definition: cfitsio.cpp:247
daq::fits::CreateEmpty
UniqueFitsFile CreateEmpty(char const *filename)
Creates empty FITS file using fits_create_file and returns a pointer with a deleter that will close t...
Definition: cfitsio.cpp:158
daq::fits::CfitsioError
Represents errors from cfitsio.
Definition: cfitsio.hpp:89
keyword.hpp
Contains data structure for FITS keywords.
daq::fits::InitPrimaryHduEmpty
void InitPrimaryHduEmpty(fitsfile *ptr)
Initializes an empty FITS file with an empty primary HDU (no keywords)
Definition: cfitsio.cpp:112
daq::fits::HduType
HduType
Definition: cfitsio.hpp:18
daq::fits
Definition: cfitsio.cpp:14
daq::fits::SelectHduNum
void SelectHduNum(fitsfile *ptr, int hdu_num)
Select current HDU number.
Definition: cfitsio.cpp:89
daq::fits::Open
UniqueFitsFile Open(char const *filename, OpenMode mode)
Open file.
Definition: cfitsio.cpp:168
daq::fits::UniqueFitsFile
std::unique_ptr< fitsfile, void(*)(fitsfile *) noexcept > UniqueFitsFile
Defines unique ownership type to cfitsio fitsfile.
Definition: cfitsio.hpp:32
conf.version
string version
Definition: conf.py:85
daq::fits::MemoryFitsFile::MemoryFitsFile
MemoryFitsFile(MemoryFitsFile &&other) noexcept=default
daq::fits::OpenMode::ReadOnly
@ ReadOnly
daq::fits::OpenMode
OpenMode
Definition: cfitsio.hpp:25
daq::fits::DeleteAllKeywords
void DeleteAllKeywords(fitsfile *ptr, int hdu_num)
Delete all keywords from HDU.
Definition: cfitsio.cpp:209
daq::fits::MemoryFitsFile::GetFile
fitsfile * GetFile() const noexcept
Get fits pointer.
Definition: cfitsio.hpp:55
daq::fits::MemoryFitsFile::operator=
MemoryFitsFile & operator=(MemoryFitsFile &&other) noexcept=default
daq::fits::MemoryFitsFile::GetOwnedFile
UniqueFitsFile GetOwnedFile() &&
Move out ptr.
Definition: cfitsio.hpp:67
daq::fits::HduType::Image
@ Image
daq::fits::InitPrimaryHduNoImage
void InitPrimaryHduNoImage(fitsfile *ptr)
Initializes an empty FITS file with a primary HDU.
Definition: cfitsio.cpp:125
daq::fits::WriteChecksum
void WriteChecksum(fitsfile *ptr, int hdu_num)
Write or update checksum keywords DATASUM and CHECKSUM to HDU specified by hdu_num.
Definition: cfitsio.cpp:283
daq::fits::MemoryFitsFile::MemoryFitsFile
MemoryFitsFile() noexcept
Creates empty file.
Definition: cfitsio.cpp:55
daq::fits::DefaultClose
void DefaultClose(fitsfile *ptr) noexcept
Default close function that is used by UniqueFitsFile as a deleter.
Definition: cfitsio.cpp:100