From a63cc2621598ea4dc301d6cfac85e21e144872fd Mon Sep 17 00:00:00 2001 From: program-- Date: Wed, 3 Jul 2024 10:52:42 -0700 Subject: [PATCH] feat(ForcingsEngineDataProvider): add ensure_variable member for aliasing variable names to forcings engine names --- .../forcing/ForcingsEngineDataProvider.hpp | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/include/forcing/ForcingsEngineDataProvider.hpp b/include/forcing/ForcingsEngineDataProvider.hpp index f189db9948..8cee931230 100644 --- a/include/forcing/ForcingsEngineDataProvider.hpp +++ b/include/forcing/ForcingsEngineDataProvider.hpp @@ -194,6 +194,27 @@ struct ForcingsEngineDataProvider var_output_names_ = bmi_->GetOutputVarNames(); } + std::string ensure_variable(std::string name, const std::string& suffix = "_ELEMENT") const + { + // TODO: use get_available_var_names() once const + auto vars = boost::span{var_output_names_}; + + if (std::find(vars.begin(), vars.end(), name) != vars.end()) { + return name; + } + + auto suffixed_name = std::move(name) + suffix; + if (std::find(vars.begin(), vars.end(), suffixed_name) != vars.end()) { + return suffixed_name; + } + + throw std::runtime_error{ + "ForcingsEngineDataProvider: neither variable `" + + suffixed_name.substr(0, suffixed_name.size() - suffix.size()) + + "` nor `" + suffixed_name + "` exist." + }; + } + //! Forcings Engine instance std::shared_ptr bmi_ = nullptr;