ifw-daq  2.1.0-pre1
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::unique_ptr<KeywordRuleProcessor> keyword_rules);
90  return *m_processor;
91  }
92 
93 private:
94  std::unique_ptr<KeywordRuleProcessor> m_processor;
95 };
96 
97 } // namespace base
98 
99 /**
100  * Source file not found.
101  */
102 struct SourceNotFoundError : std::invalid_argument {
103  SourceNotFoundError(char const* path);
104 };
105 
106 /**
107  * Target FITS file
108  *
109  * @ingroup daq_dpm_merge
110  */
112 public:
113  TargetSource(std::string name,
114  std::filesystem::path path,
115  std::unique_ptr<KeywordRuleProcessor> keyword_rules);
116  /**
117  * Used by unit tests and when executing as dry-run. Allows for @a file to be faked.
118  */
119  TargetSource(std::string name,
120  std::filesystem::path path,
121  std::unique_ptr<KeywordRuleProcessor> keyword_rules,
122  fits::UniqueFitsFile file);
123  using FitsFile::Close;
124  using FitsFile::GetFilePath;
125  using FitsFile::GetFitsFile;
126  using KeywordRules::GetKeywordRuleProcessor;
127  using Source::GetName;
128 };
129 
130 /**
131  * Input FITS source file.
132  *
133  * @ingroup daq_dpm_merge
134  */
136 public:
137  FitsFileSource(std::string name,
138  std::filesystem::path path,
139  std::string origin,
140  std::unique_ptr<KeywordRuleProcessor> keyword_rules);
141 
142  using FitsFile::Close;
143  using FitsFile::GetFilePath;
144  using FitsFile::GetFitsFile;
145  using KeywordRules::GetKeywordRuleProcessor;
146  using Source::GetName;
147 
148 private:
149  /**
150  * Path as provided in data product specification
151  */
152  std::string m_origin;
153 };
154 
155 /**
156  * Input FITS keywords.
157  *
158  * @ingroup daq_dpm_merge
159  */
161 public:
162  FitsKeywordsSource(std::string name,
163  fits::KeywordVector keywords,
164  std::unique_ptr<KeywordRuleProcessor> keyword_rules) noexcept;
165  using KeywordRules::GetKeywordRuleProcessor;
166  using Source::GetName;
167 
168  fits::KeywordVector const& GetKeywords() const& noexcept {
169  return m_keywords;
170  }
171 
173  return m_keywords;
174  }
175 
176 private:
177  fits::KeywordVector m_keywords;
178 };
179 
180 /**
181  * Variant of the different supported source types.
182  */
183 using SourceTypes = std::variant<FitsKeywordsSource, FitsFileSource>;
184 
185 } // namespace daq::dpm::merge
186 
187 #endif // #ifndef DAQ_DPM_MERGE_SOURCES_HPP
daq::dpm::merge::SourceTypes
std::variant< FitsKeywordsSource, FitsFileSource > SourceTypes
Variant of the different supported source types.
Definition: sources.hpp:183
daq::dpm::merge::base::FitsFile::GetFilePath
std::filesystem::path const & GetFilePath() const &noexcept
Get file system path associated with this file.
Definition: sources.hpp:61
daq::dpm::merge::base::Source::Source
Source(std::string name) noexcept
Definition: sources.cpp:18
daq::dpm::merge::base::KeywordRules
Represents the keyword rules from the Data Product Specification.
Definition: sources.hpp:86
keywordRule.hpp
keyword.hpp
Contains data structure for FITS keywords.
daq::dpm::merge::base::Source::GetName
std::string const & GetName() const noexcept
Definition: sources.hpp:28
daq::dpm::merge::base::FitsFile::GetFitsFile
fitsfile * GetFitsFile() const noexcept
Get fits file pointer.
Definition: sources.hpp:54
daq::dpm::merge::SourceNotFoundError
Source file not found.
Definition: sources.hpp:102
daq::fits::UniqueFitsFile
std::unique_ptr< fitsfile, void(*)(fitsfile *) noexcept > UniqueFitsFile
Defines unique ownership type to cfitsio fitsfile.
Definition: cfitsio.hpp:32
daq::dpm::merge::base::Source
Base source.
Definition: sources.hpp:25
daq::dpm::merge::FitsFileSource
Input FITS source file.
Definition: sources.hpp:135
daq::fits::OpenMode
OpenMode
Definition: cfitsio.hpp:25
daq::dpm::merge::base::FitsFile::FitsFile
FitsFile(std::filesystem::path path, fits::OpenMode mode)
Open existing fits file.
Definition: sources.cpp:21
daq::dpm::merge::TargetSource
Target FITS file.
Definition: sources.hpp:111
daq::dpm::merge::SourceNotFoundError::SourceNotFoundError
SourceNotFoundError(char const *path)
Definition: sources.cpp:39
daq::dpm::merge::base::FitsFile
Definition: sources.hpp:36
daq::dpm::merge::base::FitsFile::Close
void Close()
Close fits file.
Definition: sources.hpp:70
daq::dpm::merge
Definition: entrypoint.cpp:26
cfitsio.hpp
Contains functions and data structures related to cfitsio.
daq::dpm::merge::FitsKeywordsSource::GetKeywords
fits::KeywordVector const & GetKeywords() const &noexcept
Definition: sources.hpp:168
daq::dpm::merge::TargetSource::TargetSource
TargetSource(std::string name, std::filesystem::path path, std::unique_ptr< KeywordRuleProcessor > keyword_rules)
Definition: sources.cpp:43
daq::fits::KeywordVector
std::vector< KeywordVariant > KeywordVector
Vector of keywords.
Definition: keyword.hpp:414
daq::dpm::merge::FitsKeywordsSource::GetKeywords
fits::KeywordVector & GetKeywords() &noexcept
Definition: sources.hpp:172
daq::dpm::merge::base::KeywordRules::GetKeywordRuleProcessor
KeywordRuleProcessor const & GetKeywordRuleProcessor() const
Definition: sources.hpp:89
daq::dpm::merge::FitsFileSource::FitsFileSource
FitsFileSource(std::string name, std::filesystem::path path, std::string origin, std::unique_ptr< KeywordRuleProcessor > keyword_rules)
Definition: sources.cpp:60
daq::dpm::merge::base::KeywordRules::KeywordRules
KeywordRules(std::unique_ptr< KeywordRuleProcessor > keyword_rules)
Definition: sources.cpp:14
daq::dpm::KeywordRuleProcessor
Interface for keyword rule processors.
Definition: keywordRule.hpp:18
daq::dpm::merge::base::Source::m_name
std::string m_name
Definition: sources.hpp:33
daq::dpm::merge::FitsKeywordsSource
Input FITS keywords.
Definition: sources.hpp:160
daq::dpm::merge::FitsKeywordsSource::FitsKeywordsSource
FitsKeywordsSource(std::string name, fits::KeywordVector keywords, std::unique_ptr< KeywordRuleProcessor > keyword_rules) noexcept
Definition: sources.cpp:70