8#ifndef GRIDFORMAT_GRID_READER_HPP_
9#define GRIDFORMAT_GRID_READER_HPP_
22#include <gridformat/common/ranges.hpp>
23#include <gridformat/common/exceptions.hpp>
24#include <gridformat/common/concepts.hpp>
25#include <gridformat/common/logging.hpp>
26#include <gridformat/grid/cell_type.hpp>
28namespace GridFormat::Concepts {
34template<
typename T, std::
size_t space_dim = 3>
37 { t.insert_point(std::declval<
const std::array<typename T::ctype, space_dim>&>()) };
38 { t.insert_cell(GridFormat::CellType{}, std::declval<const std::vector<std::size_t>&>()) };
53 using Vector = std::array<double, 3>;
54 using CellVisitor = std::function<void(CellType,
const std::vector<std::size_t>&)>;
58 std::array<std::size_t, 3> lower_left;
59 std::array<std::size_t, 3> upper_right;
98 return _number_of_cells();
103 return _number_of_points();
109 return _number_of_pieces();
116 loc.upper_right.at(0) - loc.lower_left.at(0),
117 loc.upper_right.at(1) - loc.lower_left.at(1),
118 loc.upper_right.at(2) - loc.lower_left.at(2)
128 std::vector<double>
ordinates(
unsigned int direction)
const {
130 throw ValueError(
"direction must be < 3");
131 return _ordinates(direction);
147 throw ValueError(
"direction must be < 3");
148 return _basis_vector(direction);
153 return _is_sequence();
158 return _number_of_steps();
163 return _time_at_step(step_idx);
168 _set_step(step_idx, _field_names);
172 template<std::
size_t space_dim = 3, Concepts::Gr
idFactory<space_dim> Factory>
177 const auto read_space_dim = point_layout.extent(1);
178 const auto copied_space_dim = std::min(space_dim, read_space_dim);
180 std::ostringstream s; s << point_layout;
182 "Point layout " + s.str() +
" does not match number of points: " + std::to_string(
number_of_points())
186 point_field->visit_field_values([&] <
typename T> (std::span<const T> coords) {
187 auto p = Ranges::filled_array<space_dim>(
typename Factory::ctype{0.0});
188 for (std::size_t i = 0; i < point_layout.extent(0); ++i) {
189 for (std::size_t dir = 0; dir < copied_space_dim; ++dir)
190 p[dir] =
static_cast<typename Factory::ctype
>(coords[i*read_space_dim + dir]);
191 factory.insert_point(std::as_const(p));
195 visit_cells([&] (GridFormat::CellType ct,
const std::vector<std::size_t>& corners) {
196 factory.insert_cell(std::move(ct), corners);
202 _visit_cells(visitor);
212 return _cell_field(
name);
217 return _point_field(
name);
222 return _meta_data_field(
name);
227 return reader._field_names.cell_fields;
232 return reader._field_names.point_fields;
237 return reader._field_names.meta_data_fields;
242 return reader._field_names.cell_fields | std::views::transform([&] (
const std::string& n) {
243 return std::make_pair(std::move(n), reader.
cell_field(n));
249 return reader._field_names.point_fields | std::views::transform([&] (
const std::string& n) {
250 return std::make_pair(std::move(n), reader.
point_field(n));
256 return reader._field_names.meta_data_fields | std::views::transform([&] (std::string n) {
261 void set_ignore_warnings(
bool value) {
262 _ignore_warnings = value;
267 std::vector<std::string> cell_fields;
268 std::vector<std::string> point_fields;
269 std::vector<std::string> meta_data_fields;
273 point_fields.clear();
274 meta_data_fields.clear();
278 void _log_warning(std::string_view warning)
const {
279 if (!_ignore_warnings)
282 + (warning.ends_with(
"\n") ?
"" :
"\n")
283 +
"To deactivate this warning, call set_ignore_warnings(true);"
288 std::string _filename =
"";
289 FieldNames _field_names;
290 bool _ignore_warnings =
false;
292 virtual std::string _name()
const = 0;
293 virtual void _open(
const std::string&, FieldNames&) = 0;
294 virtual void _close() = 0;
296 virtual std::size_t _number_of_cells()
const = 0;
297 virtual std::size_t _number_of_points()
const = 0;
298 virtual std::size_t _number_of_pieces()
const = 0;
300 virtual FieldPtr _cell_field(std::string_view)
const = 0;
301 virtual FieldPtr _point_field(std::string_view)
const = 0;
302 virtual FieldPtr _meta_data_field(std::string_view)
const = 0;
304 virtual void _visit_cells(
const CellVisitor&)
const {
305 throw NotImplemented(
"'" + _name() +
"' does not implement cell visiting");
309 throw NotImplemented(
"'" + _name() +
"' does not implement points()");
312 virtual PieceLocation _location()
const {
313 throw NotImplemented(
"Extents/Location are only available with structured grid formats");
316 virtual std::vector<double> _ordinates(
unsigned int)
const {
317 throw NotImplemented(
"Ordinates are only available with rectilinear grid formats.");
320 virtual Vector _spacing()
const {
321 throw NotImplemented(
"Spacing is only available with image grid formats.");
324 virtual Vector _origin()
const {
325 throw NotImplemented(
"Origin is only available with image grid formats.");
328 virtual Vector _basis_vector(
unsigned int i)
const {
329 std::array<double, 3> result{0., 0., 0.};
334 virtual bool _is_sequence()
const = 0;
336 virtual std::size_t _number_of_steps()
const {
337 throw NotImplemented(
"The format read by '" + _name() +
"' is not a sequence");
340 virtual double _time_at_step(std::size_t)
const {
341 throw NotImplemented(
"The format read by '" + _name() +
"' is not a sequence");
344 virtual void _set_step(std::size_t, FieldNames&) {
345 throw NotImplemented(
"The format read by '" + _name() +
"' is not a sequence");
std::shared_ptr< const Field > FieldPtr
Pointer type used by writers/readers for fields.
Definition: field.hpp:186