NGen
Loading...
Searching...
No Matches
output.hpp
1#ifndef NGEN_REALIZATION_CONFIG_OUTPUT_H
2#define NGEN_REALIZATION_CONFIG_OUTPUT_H
3
4#include <string>
5#include <stdexcept>
6
7#include <boost/property_tree/ptree.hpp>
8#include <boost/optional.hpp>
9
10namespace realization {
11 namespace config {
12
14 static const std::string OUTPUT_CONFIG_KEY = "output";
15
17 static const std::string LEGACY_OUTPUT_ROOT_KEY = "output_root";
18 static const std::string LEGACY_DISABLE_CATCHMENT_KEY = "disable_catchment_output";
19 static const std::string LEGACY_PER_FORMULATION_KEY = "per_formulation_nexus_files";
20
22 enum class OutputFormat { csv, netcdf };
23
31
32 inline OutputFormat parse_output_format(const std::string& s)
33 {
34 if (s == "csv") return OutputFormat::csv;
35 if (s == "netcdf") return OutputFormat::netcdf;
36 throw std::runtime_error("Invalid output format '" + s + "'; expected 'csv' or 'netcdf'.");
37 }
38
39 inline OutputGrouping parse_output_grouping(const std::string& s)
40 {
41 if (s == "per_feature") return OutputGrouping::per_feature;
42 if (s == "per_formulation") return OutputGrouping::per_formulation;
43 throw std::runtime_error("Invalid output grouping '" + s + "'; expected 'per_feature' or 'per_formulation'.");
44 }
45
47 struct OutputDomain {
48 bool enable = true;
56 bool rank_subdir = false;
57
58 OutputDomain() = default;
59
60 explicit OutputDomain(const boost::property_tree::ptree& tree) {
61 enable = tree.get<bool>("enable", true);
62 rank_subdir = tree.get<bool>("rank_subdir", false);
63 if (auto f = tree.get_optional<std::string>("format")) format = parse_output_format(*f);
64 if (auto g = tree.get_optional<std::string>("grouping")) grouping = parse_output_grouping(*g);
65 }
66 };
67
75 inline std::string rank_output_root(const std::string& base_root, const OutputDomain& domain,
76 int mpi_rank, int mpi_num_procs)
77 {
78 const bool needs_rank_subdir = domain.rank_subdir
80 if (mpi_num_procs > 1 && needs_rank_subdir) {
81 return base_root + "rank_" + std::to_string(mpi_rank) + "/";
82 }
83 return base_root;
84 }
85
102 struct Output {
104 static constexpr int DEFAULT_PRECISION = 9;
105
111 std::string root;
117 bool from_legacy_keys = false;
118
119 Output() = default;
120
122 static Output parse_block(const boost::property_tree::ptree& output_tree)
123 {
124 Output out;
125 out.root = output_tree.get<std::string>("root", "");
126 out.precision = output_tree.get<int>("precision", DEFAULT_PRECISION);
127 if (auto c = output_tree.get_child_optional("catchment")) out.catchment = OutputDomain(*c);
128 if (auto n = output_tree.get_child_optional("nexus")) out.nexus = OutputDomain(*n);
129 return out;
130 }
131
133 static Output parse_legacy(const boost::property_tree::ptree& realization_tree)
134 {
135 Output out;
136 const auto root = realization_tree.get_optional<std::string>(LEGACY_OUTPUT_ROOT_KEY);
137 const auto disable_cat = realization_tree.get_optional<bool>(LEGACY_DISABLE_CATCHMENT_KEY);
138 const auto per_form = realization_tree.get_child_optional(LEGACY_PER_FORMULATION_KEY);
139
140 if (root) out.root = *root;
141 if (disable_cat) out.catchment.enable = !(*disable_cat);
142 if (per_form && per_form->get_value<bool>(false)) out.nexus.format = OutputFormat::netcdf;
143
144 out.from_legacy_keys = static_cast<bool>(root) || static_cast<bool>(disable_cat) || static_cast<bool>(per_form);
145 return out;
146 }
147
152 static Output from_realization(const boost::property_tree::ptree& realization_tree)
153 {
154 auto block = realization_tree.get_child_optional(OUTPUT_CONFIG_KEY);
155 Output out = block ? parse_block(*block) : parse_legacy(realization_tree);
156 out.root = normalize_root(out.root);
157 return out;
158 }
159
160 private:
163 static std::string normalize_root(const std::string& configured_root)
164 {
165 if (configured_root.empty()) {
166 return "./";
167 }
168 return configured_root.back() == '/' ? configured_root : configured_root + "/";
169 }
170 };
171
172 }//end namespace config
173}//end namespace realization
174#endif //NGEN_REALIZATION_CONFIG_OUTPUT_H
OutputGrouping
How a domain's records are distributed across files (the file-*granularity* axis): per_feature - one ...
Definition output.hpp:30
OutputGrouping parse_output_grouping(const std::string &s)
Definition output.hpp:39
static const std::string LEGACY_DISABLE_CATCHMENT_KEY
Definition output.hpp:18
static const std::string LEGACY_OUTPUT_ROOT_KEY
Deprecated top-level keys this block replaces.
Definition output.hpp:17
std::string rank_output_root(const std::string &base_root, const OutputDomain &domain, int mpi_rank, int mpi_num_procs)
The output root for a rank's files in this domain: appends "rank_<N>/" to base_root when the domain r...
Definition output.hpp:75
OutputFormat
Serialization format for an output domain.
Definition output.hpp:22
static const std::string LEGACY_PER_FORMULATION_KEY
Definition output.hpp:19
OutputFormat parse_output_format(const std::string &s)
Definition output.hpp:32
static const std::string OUTPUT_CONFIG_KEY
Key for the dedicated output configuration block in a realization config.
Definition output.hpp:14
Definition HY_Features.hpp:13
Settings for one output domain (catchment or nexus).
Definition output.hpp:47
OutputDomain(const boost::property_tree::ptree &tree)
Definition output.hpp:60
OutputGrouping grouping
Definition output.hpp:50
bool enable
Definition output.hpp:48
bool rank_subdir
Place each MPI rank's files under a "rank_<N>/" subdirectory (the filesystem-layout axis,...
Definition output.hpp:56
OutputFormat format
Definition output.hpp:49
Parsed representation of a realization config's output settings.
Definition output.hpp:102
static constexpr int DEFAULT_PRECISION
Significant digits applied uniformly when a text backend (CSV) renders values.
Definition output.hpp:104
static Output from_realization(const boost::property_tree::ptree &realization_tree)
Prefer the modern "output" block; otherwise fall back to legacy keys, then normalize the root (see no...
Definition output.hpp:152
static Output parse_legacy(const boost::property_tree::ptree &realization_tree)
Build from the deprecated top-level keys.
Definition output.hpp:133
static std::string normalize_root(const std::string &configured_root)
Normalize a configured directory to a trailing-slash path, falling back to "./" when unset.
Definition output.hpp:163
std::string root
Output directory.
Definition output.hpp:111
static Output parse_block(const boost::property_tree::ptree &output_tree)
Parse the modern "output" block sub-tree.
Definition output.hpp:122
OutputDomain catchment
Definition output.hpp:112
bool from_legacy_keys
True when built from the deprecated top-level keys (used to warn).
Definition output.hpp:117
OutputDomain nexus
Definition output.hpp:113
int precision
Global value precision for text output backends.
Definition output.hpp:115