ifw-daq  3.0.0-pre2
IFW Data Acquisition modules
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 
15 #include <daq/dpm/keywordRule.hpp>
16 #include <daq/fits/cfitsio.hpp>
17 #include <daq/fits/keyword.hpp>
18 
19 namespace daq::dpm::merge {
20 
21 namespace base {
22 /**
23  * Base source
24  */
25 class Source {
26 public:
27  explicit Source(std::string name) noexcept;
28  std::string const& GetName() const noexcept {
29  return m_name;
30  }
31 
32 protected:
33  std::string m_name;
34 };
35 
36 class FitsFile {
37 public:
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 
74 private:
75  std::filesystem::path m_path;
76  fits::UniqueFitsFile m_fits;
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  */
86 class KeywordRules {
87 public:
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 
98 private:
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  */
108 struct SourceNotFoundError : std::invalid_argument {
109  SourceNotFoundError(char const* path);
110 };
111 
112 /**
113  * Target FITS file
114  *
115  * @ingroup daq_dpm_merge
116  */
118 public:
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,
130  fits::UniqueFitsFile file);
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  */
145 public:
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 
152  using FitsFile::Close;
153  using FitsFile::GetFilePath;
154  using FitsFile::GetFitsFile;
155  using KeywordRules::GetInitialKeywords;
156  using KeywordRules::GetKeywordRuleProcessor;
157  using Source::GetName;
158 
159 private:
160  /**
161  * Path as provided in data product specification
162  */
163  std::string m_location;
164 };
165 
166 /**
167  * Input FITS keywords.
168  *
169  * @ingroup daq_dpm_merge
170  */
172 public:
173  FitsKeywordsSource(std::string name,
174  fits::KeywordVector keywords,
175  std::optional<KeywordRuleProcessor::DefaultRule> const& initial_keywords,
176  std::unique_ptr<KeywordRuleProcessor> keyword_rules) noexcept;
177  using KeywordRules::GetInitialKeywords;
178  using KeywordRules::GetKeywordRuleProcessor;
179  using Source::GetName;
180 
181  fits::KeywordVector const& GetKeywords() const& noexcept {
182  return m_keywords;
183  }
184 
186  return m_keywords;
187  }
188 
189 private:
190  fits::KeywordVector m_keywords;
191 };
192 
193 /**
194  * Variant of the different supported source types.
195  */
196 using SourceTypes = std::variant<FitsKeywordsSource, FitsFileSource>;
197 
198 } // namespace daq::dpm::merge
199 
200 #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
FitsFileSource(std::string name, std::filesystem::path path, std::string location, std::optional< KeywordRuleProcessor::DefaultRule > const &initial_keywords, std::unique_ptr< KeywordRuleProcessor > keyword_rules)
Definition: sources.cpp:63
FitsKeywordsSource(std::string name, fits::KeywordVector keywords, std::optional< KeywordRuleProcessor::DefaultRule > const &initial_keywords, std::unique_ptr< KeywordRuleProcessor > keyword_rules) noexcept
Definition: sources.cpp:74
fits::KeywordVector & GetKeywords() &noexcept
Definition: sources.hpp:185
fits::KeywordVector const & GetKeywords() const &noexcept
Definition: sources.hpp:181
TargetSource(std::string name, std::filesystem::path path, std::optional< KeywordRuleProcessor::DefaultRule > const &initial_keywords, std::unique_ptr< KeywordRuleProcessor > keyword_rules)
Definition: sources.cpp:44
void Close()
Close fits file.
Definition: sources.hpp:70
fitsfile * GetFitsFile() const noexcept
Get fits file pointer.
Definition: sources.hpp:54
std::filesystem::path const & GetFilePath() const &noexcept
Get file system path associated with this file.
Definition: sources.hpp:61
FitsFile(std::filesystem::path path, fits::OpenMode mode)
Open existing fits file.
Definition: sources.cpp:22
Represents the keyword rules from the Data Product Specification.
Definition: sources.hpp:86
auto GetKeywordRuleProcessor() const -> KeywordRuleProcessor const &
Definition: sources.hpp:94
KeywordRules(std::optional< KeywordRuleProcessor::DefaultRule > const &initial, std::unique_ptr< KeywordRuleProcessor > keyword_rules)
Definition: sources.cpp:14
auto GetInitialKeywords() const -> std::optional< KeywordRuleProcessor::DefaultRule > const &
Definition: sources.hpp:91
Source(std::string name) noexcept
Definition: sources.cpp:19
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:196
std::unique_ptr< fitsfile, void(*)(fitsfile *) noexcept > UniqueFitsFile
Defines unique ownership type to cfitsio fitsfile.
Definition: cfitsio.hpp:33
std::vector< KeywordVariant > KeywordVector
Vector of keywords.
Definition: keyword.hpp:414
Source file not found.
Definition: sources.hpp:108
SourceNotFoundError(char const *path)
Definition: sources.cpp:40