NGen
Loading...
Searching...
No Matches
mdframe.hpp
1#ifndef NGEN_MDFRAME_DEFINITION_HPP
2#define NGEN_MDFRAME_DEFINITION_HPP
3
4#include <unordered_set>
5#include <unordered_map>
6
7#include "dimension.hpp"
8#include "visitors.hpp"
9#include "variable.hpp"
10
11namespace ngen {
12
56class mdframe {
57 public:
59 using dimension_set = std::unordered_set<dimension, dimension::hash>;
61 using variable_map = std::unordered_map<std::string, variable>;
63
71
73
74 // ------------------------------------------------------------------------
75 // Dimension Member Functions
76 // ------------------------------------------------------------------------
77
78 private:
85 dimension_set::const_iterator find_dimension(const std::string& name) const noexcept
86 {
87 return this->m_dimensions.find(dimension(name));
88 }
89
96 variable_map::const_iterator find_variable(const std::string& name) const noexcept
97 {
98 return this->m_variables.find(name);
99 }
100
101 public:
108 boost::optional<detail::dimension> get_dimension(const std::string& name) const noexcept
109 {
110 decltype(auto) pos = this->find_dimension(name);
111
112 boost::optional<detail::dimension> result;
113 if (pos != this->m_dimensions.end()) {
114 result = *pos;
115 }
116
117 return result;
118 }
119
127 bool has_dimension(const std::string& name) const noexcept
128 {
129 return this->find_dimension(name) != this->m_dimensions.end();
130 }
131
138 mdframe& add_dimension(const std::string& name)
139 {
140 this->m_dimensions.emplace(name);
141 return *this;
142 }
143
152 mdframe& add_dimension(const std::string& name, std::size_t size)
153 {
154 auto stat = this->m_dimensions.emplace(name, size);
155
156 if (!stat.second) {
157 stat.first->m_size = size;
158 }
159
160 return *this;
161 }
162
163 // ------------------------------------------------------------------------
164 // Variable Member Functions
165 // ------------------------------------------------------------------------
166
174 variable& get_variable(const std::string& name) noexcept
175 {
176 return this->m_variables[name];
177 }
178
186 variable& operator[](const std::string& name) noexcept
187 {
188 return this->get_variable(name);
189 }
190
198 bool has_variable(const std::string& name) const noexcept
199 {
200 return this->find_variable(name) != this->m_variables.end();
201 }
202
215 template<typename T, types::enable_if_supports<T, bool> = true>
216 mdframe& add_variable(const std::string& name, std::initializer_list<std::string> dimensions)
217 {
218 std::vector<dimension> references;
219 references.reserve(dimensions.size());
220
221 for (const auto& d : dimensions) {
222 auto dopt = this->get_dimension(d);
223 if (dopt == boost::none) {
224 throw std::runtime_error("not a dimension");
225 }
226 references.push_back(dopt.get());
227 }
228
229 this->m_variables[name] = variable::make<T>(name, references);
230
231 return *this;
232 }
233
234
235 // ------------------------------------------------------------------------
236 // Output Member Functions
237 // ------------------------------------------------------------------------
238
255 void to_csv(const std::string& path, bool header = true) const;
256
262 void to_netcdf(const std::string& path) const;
263
264 private:
267};
268
269} // namespace ngen
270
271#endif // NGEN_MDFRAME_DEFINITION_HPP
A multi-dimensional, tagged data frame.
Definition mdframe.hpp:56
variable & operator[](const std::string &name) noexcept
Return reference to a mdarray representing a variable, if it exists.
Definition mdframe.hpp:186
std::unordered_map< std::string, variable > variable_map
Definition mdframe.hpp:61
bool has_variable(const std::string &name) const noexcept
Check if a variable exists.
Definition mdframe.hpp:198
void to_netcdf(const std::string &path) const
Write this mdframe to a NetCDF file.
Definition handler_netcdf.cpp:83
detail::dimension dimension
Definition mdframe.hpp:58
variable_map m_variables
Definition mdframe.hpp:266
std::unordered_set< dimension, dimension::hash > dimension_set
Definition mdframe.hpp:59
mdframe & add_variable(const std::string &name, std::initializer_list< std::string > dimensions)
Add a (vector) variable definition to this mdframe.
Definition mdframe.hpp:216
mdframe & add_dimension(const std::string &name, std::size_t size)
Add a dimension with a specified size to this mdframe, or update an existing dimension.
Definition mdframe.hpp:152
variable & get_variable(const std::string &name) noexcept
Return reference to a mdarray representing a variable, if it exists.
Definition mdframe.hpp:174
variable_map::const_iterator find_variable(const std::string &name) const noexcept
Get an iterator to a variable, if it exists in the set.
Definition mdframe.hpp:96
dimension_set m_dimensions
Definition mdframe.hpp:265
variable::mdarray_variant mdarray_variant
Definition mdframe.hpp:72
void to_csv(const std::string &path, bool header=true) const
Write this mdframe to a CSV file.
Definition handler_csv.cpp:41
mdframe & add_dimension(const std::string &name)
Add a dimension with unlimited size to this mdframe.
Definition mdframe.hpp:138
dimension_set::const_iterator find_dimension(const std::string &name) const noexcept
Get an iterator to a dimension, if it exists in the set.
Definition mdframe.hpp:85
boost::optional< detail::dimension > get_dimension(const std::string &name) const noexcept
Return a dimension if it exists.
Definition mdframe.hpp:108
bool has_dimension(const std::string &name) const noexcept
Check if a dimension exists.
Definition mdframe.hpp:127
variable::size_type size_type
Definition mdframe.hpp:62
Definition DomainLayer.hpp:9
Dimension Key.
Definition dimension.hpp:19
Variable Key.
Definition variable.hpp:24
static variable make(const std::string &name, const std::vector< dimension > &dimensions)
Constructs a named variable spanned over the given dimensions.
Definition variable.hpp:73
typename types::template variant_container< ngen::mdarray > mdarray_variant
A boost::variant type consisting of mdarrays of the support types, i.e.
Definition variable.hpp:40
traits::type_list< SupportedTypes... > types
The variable value types this frame can support.
Definition variable.hpp:32
std::size_t size_type
Definition variable.hpp:25
Definition traits.hpp:62