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
9namespace realization {
10 namespace config {
11
13 static const std::string OUTPUT_CONFIG_KEY = "output";
14
16 static const std::string LEGACY_OUTPUT_ROOT_KEY = "output_root";
17 static const std::string LEGACY_DISABLE_CATCHMENT_KEY = "disable_catchment_output";
18 static const std::string LEGACY_PER_FORMULATION_KEY = "per_formulation_nexus_files";
19
21 enum class OutputFormat { csv, netcdf };
22
30
31 inline OutputFormat parse_output_format(const std::string& s)
32 {
33 if (s == "csv") return OutputFormat::csv;
34 if (s == "netcdf") return OutputFormat::netcdf;
35 throw std::runtime_error("Invalid output format '" + s + "'; expected 'csv' or 'netcdf'.");
36 }
37
38 inline OutputGrouping parse_output_grouping(const std::string& s)
39 {
40 if (s == "per_feature") return OutputGrouping::per_feature;
41 if (s == "per_formulation") return OutputGrouping::per_formulation;
42 throw std::runtime_error("Invalid output grouping '" + s + "'; expected 'per_feature' or 'per_formulation'.");
43 }
44
46 struct OutputDomain {
47 bool enable = true;
55 bool rank_subdir = false;
56
57 OutputDomain() = default;
58
59 explicit OutputDomain(const boost::property_tree::ptree& tree) {
60 enable = tree.get<bool>("enable", true);
61 rank_subdir = tree.get<bool>("rank_subdir", false);
62 if (auto f = tree.get_optional<std::string>("format")) format = parse_output_format(*f);
63 if (auto g = tree.get_optional<std::string>("grouping")) grouping = parse_output_grouping(*g);
64 }
65 };
66
74 inline std::string rank_output_root(const std::string& base_root, const OutputDomain& domain,
75 int mpi_rank, int mpi_num_procs)
76 {
77 const bool needs_rank_subdir = domain.rank_subdir
79 if (mpi_num_procs > 1 && needs_rank_subdir) {
80 return base_root + "rank_" + std::to_string(mpi_rank) + "/";
81 }
82 return base_root;
83 }
84
101 struct Output {
103 static constexpr int DEFAULT_PRECISION = 9;
104
110 std::string root;
116 bool from_legacy_keys = false;
117
118 Output() = default;
119
121 static Output parse_block(const boost::property_tree::ptree& output_tree)
122 {
123 Output out;
124 out.root = output_tree.get<std::string>("root", "");
125 out.precision = output_tree.get<int>("precision", DEFAULT_PRECISION);
126 if (auto c = output_tree.get_child_optional("catchment")) out.catchment = OutputDomain(*c);
127 if (auto n = output_tree.get_child_optional("nexus")) out.nexus = OutputDomain(*n);
128 return out;
129 }
130
132 static Output parse_legacy(const boost::property_tree::ptree& realization_tree)
133 {
134 Output out;
135 const auto root = realization_tree.get_optional<std::string>(LEGACY_OUTPUT_ROOT_KEY);
136 const auto disable_cat = realization_tree.get_optional<bool>(LEGACY_DISABLE_CATCHMENT_KEY);
137 const auto per_form = realization_tree.get_child_optional(LEGACY_PER_FORMULATION_KEY);
138
139 if (root) out.root = *root;
140 if (disable_cat) out.catchment.enable = !(*disable_cat);
141 if (per_form && per_form->get_value<bool>(false)) out.nexus.format = OutputFormat::netcdf;
142
143 out.from_legacy_keys = static_cast<bool>(root) || static_cast<bool>(disable_cat) || static_cast<bool>(per_form);
144 return out;
145 }
146
151 static Output from_realization(const boost::property_tree::ptree& realization_tree)
152 {
153 auto block = realization_tree.get_child_optional(OUTPUT_CONFIG_KEY);
154 Output out = block ? parse_block(*block) : parse_legacy(realization_tree);
155 out.root = normalize_root(out.root);
156 return out;
157 }
158
159 private:
162 static std::string normalize_root(const std::string& configured_root)
163 {
164 if (configured_root.empty()) {
165 return "./";
166 }
167 return configured_root.back() == '/' ? configured_root : configured_root + "/";
168 }
169 };
170
171 }//end namespace config
172}//end namespace realization
173#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:29
OutputGrouping parse_output_grouping(const std::string &s)
Definition output.hpp:38
static const std::string LEGACY_DISABLE_CATCHMENT_KEY
Definition output.hpp:17
static const std::string LEGACY_OUTPUT_ROOT_KEY
Deprecated top-level keys this block replaces.
Definition output.hpp:16
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:74
OutputFormat
Serialization format for an output domain.
Definition output.hpp:21
static const std::string LEGACY_PER_FORMULATION_KEY
Definition output.hpp:18
OutputFormat parse_output_format(const std::string &s)
Definition output.hpp:31
static const std::string OUTPUT_CONFIG_KEY
Key for the dedicated output configuration block in a realization config.
Definition output.hpp:13
Definition HY_CatchmentRealization.hpp:13
Settings for one output domain (catchment or nexus).
Definition output.hpp:46
OutputDomain(const boost::property_tree::ptree &tree)
Definition output.hpp:59
OutputGrouping grouping
Definition output.hpp:49
bool enable
Definition output.hpp:47
bool rank_subdir
Place each MPI rank's files under a "rank_<N>/" subdirectory (the filesystem-layout axis,...
Definition output.hpp:55
OutputFormat format
Definition output.hpp:48
Parsed representation of a realization config's output settings.
Definition output.hpp:101
static constexpr int DEFAULT_PRECISION
Significant digits applied uniformly when a text backend (CSV) renders values.
Definition output.hpp:103
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:151
static Output parse_legacy(const boost::property_tree::ptree &realization_tree)
Build from the deprecated top-level keys.
Definition output.hpp:132
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:162
std::string root
Output directory.
Definition output.hpp:110
static Output parse_block(const boost::property_tree::ptree &output_tree)
Parse the modern "output" block sub-tree.
Definition output.hpp:121
OutputDomain catchment
Definition output.hpp:111
bool from_legacy_keys
True when built from the deprecated top-level keys (used to warn).
Definition output.hpp:116
OutputDomain nexus
Definition output.hpp:112
int precision
Global value precision for text output backends.
Definition output.hpp:114