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