Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix output_root #855

Merged
merged 15 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -105,5 +105,6 @@
"start_time": "2015-12-01 00:00:00",
"end_time": "2015-12-30 23:00:00",
"output_interval": 3600
}
},
"output_root": "./output_dir/"
}
1 change: 1 addition & 0 deletions data/example_realization_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"end_time": "2015-12-30 23:00:00",
"output_interval": 3600
},
"output_root": "./output_dir/",
"catchments": {
"cat-27": {
"formulations": [
Expand Down
30 changes: 26 additions & 4 deletions include/realizations/catchment/Formulation_Manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@
#include <functional>
#include <dirent.h>
#include <sys/stat.h>
#include <cerrno>
#include <regex>
#include <sys/types.h>
#include <unistd.h>
#include <string>

#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
Expand Down Expand Up @@ -271,7 +275,9 @@ namespace realization {
}

/**
* @brief Get the formatted output root
* @brief Get the formatted output root: check the existence of the output_root directory defined
* in realization. If true, return the directory name. Otherwise, try to create the directory
* or throw an error on failure.
*
* @code{.cpp}
* // Example config:
Expand All @@ -285,16 +291,32 @@ namespace realization {
*
* @return std::string of the output root directory
*/
std::string get_output_root() const noexcept {
std::string get_output_root() const {
const auto output_root = this->tree.get_optional<std::string>("output_root");
if (output_root != boost::none && *output_root != "") {
// Check if the path ends with a trailing slash,
// otherwise add it.
return output_root->back() == '/'
std::string str = output_root->back() == '/'
? *output_root
: *output_root + "/";
}

const char* dir = str.c_str();

//use C++ system function to check if there is a dir match that defined in realization
struct stat sb;
if (stat(dir, &sb) == 0 && S_ISDIR(sb.st_mode)) {
return str;
} else {
errno = 0;
int result = mkdir(dir, 0755);
if (result == 0)
return str;
else
throw std::runtime_error("failed to create directory '" + str + "': " + std::strerror(errno));
}
}

//for case where there is no output_root in the realization file
return "./";
}

Expand Down
6 changes: 3 additions & 3 deletions test/realizations/Formulation_Manager_Test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,6 @@ class Formulation_Manager_Test : public ::testing::Test {
const double EPSILON = 0.0000001;

const std::string EXAMPLE_1 = "{ "
"\"output_root\": \"/tmp/output-dir/\","
"\"global\": { "
"\"formulations\": [ "
"{"
Expand Down Expand Up @@ -264,6 +263,7 @@ const std::string EXAMPLE_1 = "{ "
"} "
"}";
const std::string EXAMPLE_2 = "{ "
"\"output_root\": \"./output_dir/\","
"\"global\": { "
"\"formulations\": [ "
"{"
Expand Down Expand Up @@ -719,7 +719,7 @@ TEST_F(Formulation_Manager_Test, basic_reading_1) {

ASSERT_TRUE(manager.contains("cat-52"));
ASSERT_TRUE(manager.contains("cat-67"));
ASSERT_EQ(manager.get_output_root(), "/tmp/output-dir/");
ASSERT_EQ(manager.get_output_root(), "./");
}

TEST_F(Formulation_Manager_Test, basic_reading_2) {
Expand All @@ -742,7 +742,7 @@ TEST_F(Formulation_Manager_Test, basic_reading_2) {

ASSERT_TRUE(manager.contains("cat-52"));
ASSERT_TRUE(manager.contains("cat-67"));
ASSERT_EQ(manager.get_output_root(), "./");
ASSERT_EQ(manager.get_output_root(), "./output_dir/");
}

TEST_F(Formulation_Manager_Test, basic_run_1) {
Expand Down
Loading