ifw-daq 3.1.0
IFW Data Acquisition modules
Loading...
Searching...
No Matches
sources.hpp
Go to the documentation of this file.
1/**
2 * @file
3 * @ingroup daq_dpm_merge
4 * @copyright ESO - European Southern Observatory
5 */
6#ifndef DAQ_DPM_MERGE_SOURCES_HPP
7#define DAQ_DPM_MERGE_SOURCES_HPP
8#include <filesystem>
9#include <memory>
10#include <string>
11#include <variant>
12
13#include <nlohmann/json.hpp>
14
16#include <daq/fits/cfitsio.hpp>
17#include <daq/fits/keyword.hpp>
18
19namespace daq::dpm::merge {
20
21namespace base {
22/**
23 * Base source
24 */
25class Source {
26public:
27 explicit Source(std::string name) noexcept;
28 std::string const& GetName() const noexcept {
29 return m_name;
30 }
31
32protected:
33 std::string m_name;
34};
35
36class FitsFile {
37public:
38 /**
39 * Open existing fits file
40 *
41 * @param path Path to file to open. If mode is ReadWrite it must be a regular file (not
42 * symlink).
43 * @throw SourceNotFoundError if file cannot be found.
44 * @throw std::invalid_argument for other errors
45 */
46 explicit FitsFile(std::filesystem::path path, fits::OpenMode mode);
47 explicit FitsFile(std::filesystem::path path, fits::UniqueFitsFile file);
48
49 /**
50 * Get fits file pointer.
51 *
52 * @note This may be nullptr if executed with dry-run.
53 */
54 fitsfile* GetFitsFile() const noexcept {
55 return m_fits.get();
56 }
57
58 /**
59 * Get file system path associated with this file.
60 */
61 std::filesystem::path const& GetFilePath() const& noexcept {
62 return m_path;
63 }
64
65 /**
66 * Close fits file.
67 *
68 * @post GetFitsFile() == nullptr
69 */
70 void Close() {
71 m_fits.reset();
72 }
73
74private:
75 std::filesystem::path m_path;
77};
78
79/**
80 * Represents the keyword rules from the Data Product Specification.
81 *
82 * Currently only primary HDU keywords are supported, if this were to change this structure will
83 * have to be generalized to support it (with possibly different implementations for FITS file
84 * sources and JSON keywords).
85 */
87public:
88 KeywordRules(std::optional<KeywordRuleProcessor::DefaultRule> const& initial,
89 std::unique_ptr<KeywordRuleProcessor> keyword_rules);
90
91 auto GetInitialKeywords() const -> std::optional<KeywordRuleProcessor::DefaultRule> const& {
92 return m_initial;
93 }
95 return *m_processor;
96 }
97
98private:
99 std::optional<KeywordRuleProcessor::DefaultRule> m_initial;
100 std::unique_ptr<KeywordRuleProcessor> m_processor;
101};
102
103} // namespace base
104
105/**
106 * Source file not found.
107 */
108struct SourceNotFoundError : std::invalid_argument {
109 SourceNotFoundError(char const* path);
110};
111
112/**
113 * Target FITS file
114 *
115 * @ingroup daq_dpm_merge
116 */
118public:
119 TargetSource(std::string name,
120 std::filesystem::path path,
121 std::optional<KeywordRuleProcessor::DefaultRule> const& initial_keywords,
122 std::unique_ptr<KeywordRuleProcessor> keyword_rules);
123 /**
124 * Used by unit tests and when executing as dry-run. Allows for @a file to be faked.
125 */
126 TargetSource(std::string name,
127 std::filesystem::path path,
128 std::optional<KeywordRuleProcessor::DefaultRule> const& initial_keywords,
129 std::unique_ptr<KeywordRuleProcessor> keyword_rules,
131 using FitsFile::Close;
132 using FitsFile::GetFilePath;
133 using FitsFile::GetFitsFile;
134 using KeywordRules::GetInitialKeywords;
135 using KeywordRules::GetKeywordRuleProcessor;
136 using Source::GetName;
137};
138
139/**
140 * Input FITS source file.
141 *
142 * @ingroup daq_dpm_merge
143 */
145public:
146 FitsFileSource(std::string name,
147 std::filesystem::path path,
148 std::string location,
149 std::optional<KeywordRuleProcessor::DefaultRule> const& initial_keywords,
150 std::unique_ptr<KeywordRuleProcessor> keyword_rules,
151 bool alert_unmergeable);
152 /**
153 * Used by unit tests. Allows for @a file to be faked.
154 */
155 FitsFileSource(std::string name,
156 std::filesystem::path path,
157 std::string location,
158 std::optional<KeywordRuleProcessor::DefaultRule> const& initial_keywords,
159 std::unique_ptr<KeywordRuleProcessor> keyword_rules,
160 bool alert_unmergeable,
162
163 using FitsFile::Close;
164 using FitsFile::GetFilePath;
165 using FitsFile::GetFitsFile;
166 using KeywordRules::GetInitialKeywords;
167 using KeywordRules::GetKeywordRuleProcessor;
168 using Source::GetName;
169
170 std::string const& GetLocation() const noexcept {
171 return m_location;
172 }
173
174 bool AlertUnmergeable() const noexcept {
175 return m_alert_unmergeable;
176 }
177
178private:
179 /**
180 * Path as provided in data product specification
181 */
182 std::string m_location;
183 /**
184 * Determines whether to produce alert for unmerged data.
185 */
186 bool m_alert_unmergeable;
187};
188
189/**
190 * Input FITS keywords.
191 *
192 * @ingroup daq_dpm_merge
193 */
195public:
196 FitsKeywordsSource(std::string name,
197 fits::KeywordVector keywords,
198 std::optional<KeywordRuleProcessor::DefaultRule> const& initial_keywords,
199 std::unique_ptr<KeywordRuleProcessor> keyword_rules) noexcept;
200 using KeywordRules::GetInitialKeywords;
201 using KeywordRules::GetKeywordRuleProcessor;
202 using Source::GetName;
203
204 fits::KeywordVector const& GetKeywords() const& noexcept {
205 return m_keywords;
206 }
207
209 return m_keywords;
210 }
211
212private:
213 fits::KeywordVector m_keywords;
214};
215
216/**
217 * Variant of the different supported source types.
218 */
219using SourceTypes = std::variant<FitsKeywordsSource, FitsFileSource>;
220
221} // namespace daq::dpm::merge
222
223#endif // #ifndef DAQ_DPM_MERGE_SOURCES_HPP
Contains functions and data structures related to cfitsio.
Interface for keyword rule processors.
Definition: keywordRule.hpp:18
Input FITS source file.
Definition: sources.hpp:144
std::string const & GetLocation() const noexcept
Definition: sources.hpp:170
bool AlertUnmergeable() const noexcept
Definition: sources.hpp:174
fits::KeywordVector & GetKeywords() &noexcept
Definition: sources.hpp:208
fits::KeywordVector const & GetKeywords() const &noexcept
Definition: sources.hpp:204
fitsfile * GetFitsFile() const noexcept
Get fits file pointer.
Definition: sources.hpp:54
void Close()
Close fits file.
Definition: sources.hpp:70
std::filesystem::path const & GetFilePath() const &noexcept
Get file system path associated with this file.
Definition: sources.hpp:61
Represents the keyword rules from the Data Product Specification.
Definition: sources.hpp:86
auto GetKeywordRuleProcessor() const -> KeywordRuleProcessor const &
Definition: sources.hpp:94
auto GetInitialKeywords() const -> std::optional< KeywordRuleProcessor::DefaultRule > const &
Definition: sources.hpp:91
std::string const & GetName() const noexcept
Definition: sources.hpp:28
Contains data structure for FITS keywords.
std::variant< FitsKeywordsSource, FitsFileSource > SourceTypes
Variant of the different supported source types.
Definition: sources.hpp:219
std::unique_ptr< fitsfile, void(*)(fitsfile *) noexcept > UniqueFitsFile
Defines unique ownership type to cfitsio fitsfile.
Definition: cfitsio.hpp:39
std::vector< KeywordVariant > KeywordVector
Vector of keywords.
Definition: keyword.hpp:423
Source file not found.
Definition: sources.hpp:108