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

Remove custom time unit conversion in favor of UDUNITS #633

Merged
merged 2 commits into from
Sep 12, 2023
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
32 changes: 11 additions & 21 deletions include/realizations/catchment/Bmi_Adapter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "JSONProperty.hpp"
#include "State_Exception.hpp"
#include "StreamHandler.hpp"
#include <UnitsHelper.hpp>

namespace models {
namespace bmi {
Expand Down Expand Up @@ -81,29 +82,18 @@ namespace models {


/**
* Determine backing model's time units and set the reference parameter to an appropriate conversion factor.
* Determine backing model's time units and return an appropriate conversion factor.
*
* A backing BMI model may use arbitrary units for time, but it will expose what those units are via the
* BMI ``GetTimeUnits`` function. This function retrieves (and interprets) its model's units and
* sets the given reference parameter to an appropriate factor for converting its internal time values to
* equivalent representations within the model, and vice versa.
*
* @param time_convert_factor A reference to set to the determined conversion factor.
* BMI ``GetTimeUnits`` function. This function retrieves (and interprets) its model's units and
* return an appropriate factor for converting its internal time values to equivalent representations
* within the model, and vice versa. This function complies with the BMI get_time_units standard
*/
void acquire_time_conversion_factor(double &time_convert_factor) {
std::string time_units = GetTimeUnits();
if (time_units == "s" || time_units == "sec" || time_units == "second" || time_units == "seconds")
time_convert_factor = 1.0;
else if (time_units == "m" || time_units == "min" || time_units == "minute" ||
time_units == "minutes")
time_convert_factor = 60.0;
else if (time_units == "h" || time_units == "hr" || time_units == "hour" || time_units == "hours")
time_convert_factor = 3600.0;
else if (time_units == "d" || time_units == "day" || time_units == "days")
time_convert_factor = 86400.0;
else
throw std::runtime_error(
"Invalid model time step units ('" + time_units + "') in " + model_name + ".");
double get_time_convert_factor() {
double value = 1.0;
std::string input_units = GetTimeUnits();
std::string output_units = "s";
return UnitsHelper::get_converted_value(input_units, value, output_units);
}

/**
Expand Down Expand Up @@ -193,7 +183,7 @@ namespace models {
construct_and_init_backing_model();
// Make sure this is set to 'true' after this function call finishes
model_initialized = true;
acquire_time_conversion_factor(bmi_model_time_convert_factor);
bmi_model_time_convert_factor = get_time_convert_factor();
}
// Record the exception message before re-throwing to handle subsequent function calls properly
catch (std::exception& e) {
Expand Down
2 changes: 1 addition & 1 deletion include/realizations/catchment/Bmi_Fortran_Adapter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ namespace models {
construct_and_init_backing_model_for_fortran();
// Make sure this is set to 'true' after this function call finishes
model_initialized = true;
acquire_time_conversion_factor(bmi_model_time_convert_factor);
bmi_model_time_convert_factor = get_time_convert_factor();
}
// Record the exception message before re-throwing to handle subsequent function calls properly
catch( models::external::State_Exception& e)
Expand Down
2 changes: 1 addition & 1 deletion src/realizations/catchment/Bmi_C_Adapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ Bmi_C_Adapter::Bmi_C_Adapter(const std::string &type_name, std::string library_f
construct_and_init_backing_model_for_type();
// Make sure this is set to 'true' after this function call finishes
model_initialized = true;
acquire_time_conversion_factor(bmi_model_time_convert_factor);
bmi_model_time_convert_factor = get_time_convert_factor();
}
// Record the exception message before re-throwing to handle subsequent function calls properly
catch( models::external::State_Exception& e)
Expand Down
2 changes: 1 addition & 1 deletion src/realizations/catchment/Bmi_Cpp_Adapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Bmi_Cpp_Adapter::Bmi_Cpp_Adapter(const std::string& type_name, std::string libra
construct_and_init_backing_model_for_type();
// Make sure this is set to 'true' after this function call finishes
model_initialized = true;
acquire_time_conversion_factor(bmi_model_time_convert_factor);
bmi_model_time_convert_factor = get_time_convert_factor();
}
// Record the exception message before re-throwing to handle subsequent function calls properly
catch (const std::exception &e) {
Expand Down
2 changes: 1 addition & 1 deletion src/realizations/catchment/Bmi_Py_Adapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Bmi_Py_Adapter::Bmi_Py_Adapter(const std::string &type_name, std::string bmi_ini
construct_and_init_backing_model_for_py_adapter();
// Make sure this is set to 'true' after this function call finishes
model_initialized = true;
acquire_time_conversion_factor(bmi_model_time_convert_factor);
bmi_model_time_convert_factor = get_time_convert_factor();
}
catch (std::runtime_error& e){ //Catch specific exception and re-throw so type/message isn't erased
model_initialized = true;
Expand Down