Skip to content

Commit

Permalink
fix(ForcingsEngineLumpedDataProvider): handle divide_index and fix ge…
Browse files Browse the repository at this point in the history
…t_value[s] to use beginning time as epoch
  • Loading branch information
program-- committed Aug 14, 2024
1 parent a754cf2 commit eebc24b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
10 changes: 6 additions & 4 deletions src/forcing/ForcingsEngineLumpedDataProvider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ Provider::ForcingsEngineLumpedDataProvider(
if (divide_id_pos == cat_id_span.end()) {
// throw std::runtime_error{"Unable to find divide ID `" + divide_id + "` in given Forcings Engine domain"};
divide_idx_ = static_cast<std::size_t>(-1);
} else {
divide_idx_ = std::distance(cat_id_span.begin(), divide_id_pos);
}
}

Expand Down Expand Up @@ -87,8 +89,8 @@ Provider::data_type Provider::get_value(
auto current = start;
while (current < end) {
current += time_step_;
bmi_->UpdateUntil(std::chrono::duration_cast<std::chrono::seconds>(current - start).count());
acc += *static_cast<double*>(bmi_->GetValuePtr(variable));
bmi_->UpdateUntil(std::chrono::duration_cast<std::chrono::seconds>(current - time_begin_).count());
acc += static_cast<double*>(bmi_->GetValuePtr(variable))[divide_idx_];
}

if (m == ReSampleMethod::MEAN) {
Expand Down Expand Up @@ -122,8 +124,8 @@ std::vector<Provider::data_type> Provider::get_values(
auto current = start;
while (current < end) {
current += time_step_;
bmi_->UpdateUntil(std::chrono::duration_cast<std::chrono::seconds>(current - start).count());
values.push_back(*static_cast<double*>(bmi_->GetValuePtr(variable)));
bmi_->UpdateUntil(std::chrono::duration_cast<std::chrono::seconds>(current - time_begin_).count());
values.push_back(static_cast<double*>(bmi_->GetValuePtr(variable))[divide_idx_]);
}

return values;
Expand Down
25 changes: 14 additions & 11 deletions test/forcing/ForcingsEngineLumpedDataProvider_Test.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#include "DataProviderSelectors.hpp"
#include "ForcingsEngineDataProvider.hpp"
#include <gtest/gtest.h>

#include <NGenConfig.h>
Expand Down Expand Up @@ -41,8 +39,8 @@ struct ForcingsEngineLumpedDataProviderTest
using TestFixture = ForcingsEngineLumpedDataProviderTest;

constexpr const char* TestFixture::config_file;
const std::time_t TestFixture::time_start = data_access::detail::parse_time("2024-01-17 01:00:00");
const std::time_t TestFixture::time_end = TestFixture::time_start + 3600;
const std::time_t TestFixture::time_start = data_access::detail::parse_time("2023-01-17 01:00:00");
const std::time_t TestFixture::time_end = TestFixture::time_start + 3600 + 3600;

std::shared_ptr<utils::ngenPy::InterpreterUtil> TestFixture::gil_ = nullptr;
std::unique_ptr<TestFixture::provider_type> TestFixture::provider_ = nullptr;
Expand Down Expand Up @@ -78,7 +76,8 @@ void TestFixture::TearDownTestSuite()
/**
* Tests for the flyweight-like design of provider storage by getting
* a new instance of the forcings engine and verifying that it points
* to the same address as the static initialized `provider_` member.
* to the same address as the static initialized `provider_` memberm
* based on matching `init`, and shared over distinct `divide_id`.
*/
TEST_F(ForcingsEngineLumpedDataProviderTest, Storage)
{
Expand All @@ -94,6 +93,9 @@ TEST_F(ForcingsEngineLumpedDataProviderTest, Storage)

TEST_F(ForcingsEngineLumpedDataProviderTest, VariableAccess)
{
ASSERT_EQ(provider_->divide(), 11223UL);
ASSERT_EQ(provider_->divide_index(), 0);

constexpr std::array<const char*, 8> expected_variables = {
"U2D_ELEMENT",
"V2D_ELEMENT",
Expand All @@ -115,11 +117,12 @@ TEST_F(ForcingsEngineLumpedDataProviderTest, VariableAccess)
);
}

const auto selector = CatchmentAggrDataSelector{"cat-11223", "LWDOWN", time_start, 3600, "seconds"};
const auto result = provider_->get_values(selector, data_access::ReSampleMethod::SUM);
auto selector = CatchmentAggrDataSelector{"cat-11223", "PSFC", time_start, 3600, "seconds"};
auto result = provider_->get_value(selector, data_access::ReSampleMethod::SUM);
EXPECT_NEAR(result, 99580.52, 1e-2);

ASSERT_GT(result.size(), 0);
for (const auto& r : result) {
EXPECT_NEAR(r, 164.45626831054688, 1e6);
}
selector = CatchmentAggrDataSelector{"cat-11223", "LWDOWN", time_start + 3600, 3600, "seconds"};
auto result2 = provider_->get_values(selector, data_access::ReSampleMethod::SUM);
ASSERT_GT(result2.size(), 0);
EXPECT_NEAR(result2[0], 0, 1e-6);
}

0 comments on commit eebc24b

Please sign in to comment.