Skip to content

Commit

Permalink
feat: use AORC S3 forcings engine module for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
program-- committed Aug 14, 2024
1 parent 3615fac commit a754cf2
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 33 deletions.
5 changes: 5 additions & 0 deletions data/forcing/forcings-engine/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
gfs/**/*.grib2

# Temp directory used by the forcings engine instances
scratch/*
!scratch/.keep

# Cropped GRIB files
!gfs/gfs.20240117/00/atmos/gfs.t00z.sfluxgrbf*.grib2
Binary file not shown.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,34 +1,33 @@
time_step_seconds: 3600
initial_time: 0
NWM_VERSION: 4.0
NWM_CONFIG: "medium_range"
InputForcings: [3]
InputForcingDirectories: ["data/forcing/forcings-engine/gfs"]
InputForcingTypes: ["GRIB2"]
NWM_CONFIG: "AORC"
InputForcings: [12]
InputForcingDirectories: ['']
InputForcingTypes: ["NETCDF4"]
InputMandatory: [1]
OutputFrequency: 1
OutDir: "data/forcing/forcings-engine/scratch/"
ScratchDir: "data/forcing/forcings-engine/scratch/"
OutputFrequency: 60
SubOutputHour: 0
SubOutFreq: 0
ScratchDir: "data/forcing/forcings-engine/scratch"
Output: 1
compressOutput: 0
floatOutput: 0
RetroFlag: 0
AnAFlag: 0
BDateProc: -9999
EDateProc: -9999
LookBack : -9999
RefcstBDateProc: "202401170000"
RefcstEDateProc: "202401170100"
ForecastFrequency: 1
RefcstBDateProc: "202301170000"
RefcstEDateProc: "202301170100"
ForecastFrequency: 60
ForecastShift: 0
ForecastInputHorizons: [14400]
ForecastInputOffsets: [0]
GeogridIn: "data/forcing/forcings-engine/hydrofabric_vpu06_esmf_mesh.nc"
GeogridIn: "data/forcing/forcings-engine/Gauge_01073000_ESMF_Mesh.nc"
SpatialMetaIn: ""
GRID_TYPE: "hydrofabric"
NodeCoords: 'nodeCoords'
ElemCoords: 'centerCoords'
ElemID: 'element_id'
ElemConn: 'elementConn'
ElemID: 'element_id'
NumElemConn: 'numElementConn'
HGTVAR: 'Element_Elevation'
SLOPE: 'Element_Slope'
Expand All @@ -48,7 +47,7 @@ ShortwaveDownscaling: [0]
PressureDownscaling: [0]
PrecipDownscaling: [0]
HumidityDownscaling: [0]
DownscalingParamDirs: [""]
DownscalingParamDirs: ["data/forcing/forcings-engine/scratch"]
SuppPcp: []
SuppPcpForcingTypes: ''
SuppPcpDirectories: ''
Expand All @@ -61,4 +60,5 @@ RqiMethod: 0
RqiThreshold: 0.9
cfsEnsNumber: ''
custom_input_fcst_freq: []
includeLQFraq: 0
includeLQFrac: 0

Binary file not shown.
Empty file.
2 changes: 1 addition & 1 deletion include/forcing/ForcingsEngineDataProvider.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ namespace detail {

//! Parse time string from format.
//! Utility function for ForcingsEngineLumpedDataProvider constructor.
time_t parse_time(const std::string& time, const std::string& fmt);
time_t parse_time(const std::string& time, const std::string& fmt = default_time_format);

//! Check that requirements for running the forcings engine
//! are available at runtime. If requirements are not available,
Expand Down
14 changes: 9 additions & 5 deletions src/forcing/ForcingsEngineLumpedDataProvider.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "DataProvider.hpp"
#include <chrono>
#include <forcing/ForcingsEngineLumpedDataProvider.hpp>

namespace data_access {
Expand Down Expand Up @@ -84,7 +85,9 @@ Provider::data_type Provider::get_value(
assert(end <= time_end_);

auto current = start;
for (; current < end; current += time_step_, bmi_->UpdateUntil((current - start).count())) {
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));
}

Expand Down Expand Up @@ -116,10 +119,11 @@ std::vector<Provider::data_type> Provider::get_values(
assert(end <= time_end_);

std::vector<double> values;
for (auto current = start; current < end; current += time_step_, bmi_->UpdateUntil((current - start).count())) {
values.push_back(
*static_cast<double*>(bmi_->GetValuePtr(variable))
);
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)));
}

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

#include <NGenConfig.h>
Expand All @@ -19,12 +21,14 @@ struct ForcingsEngineLumpedDataProviderTest
ForcingsEngineLumpedDataProviderTest()
{
#if NGEN_WITH_MPI
MPI_Comm_size(MPI_COMM_WORLD, &mpi.size());
MPI_Comm_rank(MPI_COMM_WORLD, &mpi.rank);
MPI_Comm_size(MPI_COMM_WORLD, &mpi_.size);
MPI_Comm_rank(MPI_COMM_WORLD, &mpi_.rank);
#endif
}

static constexpr const char* config_file = "data/forcing/forcings-engine/config.yml";
static constexpr const char* config_file = "data/forcing/forcings-engine/config_aorc.yml";
static const std::time_t time_start;
static const std::time_t time_end;
static std::shared_ptr<utils::ngenPy::InterpreterUtil> gil_;
static std::unique_ptr<provider_type> provider_;

Expand All @@ -37,6 +41,9 @@ 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;

std::shared_ptr<utils::ngenPy::InterpreterUtil> TestFixture::gil_ = nullptr;
std::unique_ptr<TestFixture::provider_type> TestFixture::provider_ = nullptr;

Expand All @@ -52,9 +59,9 @@ void TestFixture::SetUpTestSuite()

TestFixture::provider_ = std::make_unique<data_access::ForcingsEngineLumpedDataProvider>(
/*init=*/TestFixture::config_file,
/*time_begin_seconds=*/0,
/*time_end_seconds=*/3600,
/*divide_id=*/"cat-1015786"
/*time_begin_seconds=*/TestFixture::time_start,
/*time_end_seconds=*/TestFixture::time_end,
/*divide_id=*/"cat-11223"
);
}

Expand All @@ -77,9 +84,9 @@ TEST_F(ForcingsEngineLumpedDataProviderTest, Storage)
{
auto new_inst = std::make_unique<data_access::ForcingsEngineLumpedDataProvider>(
/*init=*/TestFixture::config_file,
/*time_begin_seconds=*/0,
/*time_end_seconds=*/3600,
/*divide_id=*/"cat-1"
/*time_begin_seconds=*/TestFixture::time_start,
/*time_end_seconds=*/TestFixture::time_end,
/*divide_id=*/"cat-11371"
);

ASSERT_EQ(new_inst->model(), provider_->model());
Expand Down Expand Up @@ -108,7 +115,7 @@ TEST_F(ForcingsEngineLumpedDataProviderTest, VariableAccess)
);
}

const auto selector = CatchmentAggrDataSelector{"cat-1015786", "LWDOWN", 0, 3600, "seconds"};
const auto selector = CatchmentAggrDataSelector{"cat-11223", "LWDOWN", time_start, 3600, "seconds"};
const auto result = provider_->get_values(selector, data_access::ReSampleMethod::SUM);

ASSERT_GT(result.size(), 0);
Expand Down

0 comments on commit a754cf2

Please sign in to comment.