Skip to content

Commit

Permalink
fix: update output format
Browse files Browse the repository at this point in the history
  • Loading branch information
program-- committed Aug 22, 2023
1 parent 3808e8d commit 3cbdf52
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 20 deletions.
30 changes: 19 additions & 11 deletions include/utilities/nexus_writer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@

#define NGEN_NETCDF_ERROR throw std::runtime_error("NetCDF is not available")

// Changing these will affect both NetCDF and CSV outputs
#define NGEN_VARIABLE_NEXUS_ID "feature_id"
#define NGEN_VARIABLE_SEGMENT_ID "segment_id"
#define NGEN_VARIABLE_Q_LATERAL "q_lateral"

namespace ngen {
namespace utils {

Expand All @@ -25,7 +30,7 @@ struct nexus_writer

nexus_writer() = default;

virtual ~nexus_writer() {};
virtual ~nexus_writer() = default;

/**
* @brief Initialize this writer
Expand Down Expand Up @@ -68,7 +73,7 @@ struct nexus_csv_writer : public nexus_writer

nexus_csv_writer() = default;

~nexus_csv_writer() override {};
~nexus_csv_writer() override = default;

void init(size_type steps) override
{
Expand All @@ -81,7 +86,7 @@ struct nexus_csv_writer : public nexus_writer
{
it_++;
it_->open(file_name + ".csv");
(*it_) << "nexus_id,segment_id,qSfcLatRunoff\n";
(*it_) << NGEN_VARIABLE_NEXUS_ID "," NGEN_VARIABLE_SEGMENT_ID "," NGEN_VARIABLE_Q_LATERAL "\n";
}

void write(const std::string& segment_id, const std::string& nexus_id, double contribution) override
Expand Down Expand Up @@ -109,7 +114,7 @@ struct nexus_netcdf_writer : public nexus_writer

nexus_netcdf_writer() = default;

~nexus_netcdf_writer() override {};
~nexus_netcdf_writer() override = default;

void init(size_type steps) override
{
Expand All @@ -133,14 +138,14 @@ struct nexus_netcdf_writer : public nexus_writer
out = std::make_unique<netCDF::NcFile>(file_name + ".nc", netCDF::NcFile::replace);
const auto& dim_fid = out->addDim("feature_id", num_nexuses);

const auto& var_nexus_id = out->addVar("nexus_id", netCDF::ncString, dim_fid);
const auto& var_nexus_id = out->addVar(NGEN_VARIABLE_NEXUS_ID, netCDF::ncString, dim_fid);
var_nexus_id.putAtt("description", "Contributing Nexus ID");

const auto& var_segment_id = out->addVar("segment_id", netCDF::ncString, dim_fid);
const auto& var_segment_id = out->addVar(NGEN_VARIABLE_SEGMENT_ID, netCDF::ncString, dim_fid);
var_segment_id.putAtt("description", "Flowpath ID downstream from the corresponding nexus");

const auto& var_qlat = out->addVar("qSfcLatRunoff", netCDF::ncDouble, dim_fid);
var_qlat.putAtt("description", "Runoff from terrain routing");
const auto& var_qlat = out->addVar(NGEN_VARIABLE_Q_LATERAL, netCDF::ncDouble, dim_fid);
var_qlat.putAtt("description", "Runoff into channel reach");
var_qlat.putAtt("units", "m3 s-1");

index_ = 0;
Expand All @@ -152,9 +157,9 @@ struct nexus_netcdf_writer : public nexus_writer
#ifndef NETCDF_ACTIVE
NGEN_NETCDF_ERROR;
#else
it_->get()->getVar("nexus_id").putVar({ index_ }, nexus_id);
it_->get()->getVar("segment_id").putVar({ index_ }, segment_id);
it_->get()->getVar("qSfcLatRunoff").putVar({ index_ }, contribution);
it_->get()->getVar(NGEN_VARIABLE_NEXUS_ID).putVar({ index_ }, nexus_id);
it_->get()->getVar(NGEN_VARIABLE_SEGMENT_ID).putVar({ index_ }, segment_id);
it_->get()->getVar(NGEN_VARIABLE_Q_LATERAL).putVar({ index_ }, contribution);
index_++;
#endif
}
Expand Down Expand Up @@ -182,3 +187,6 @@ struct nexus_netcdf_writer : public nexus_writer
#endif // NGEN_UTILITIES_QLAT_HANDLER_HPP

#undef NGEN_NETCDF_ERROR
#undef NGEN_VARIABLE_NEXUS_ID
#undef NGEN_VARIABLE_SEGMENT_ID
#undef NGEN_VARIABLE_Q_LATERAL
2 changes: 2 additions & 0 deletions src/NGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,8 @@ int main(int argc, char *argv[]) {
boost::algorithm::erase_all(output_timestamp, "-");
boost::algorithm::erase_all(output_timestamp, " ");
boost::algorithm::erase_all(output_timestamp, ":");
// Remove Seconds
output_timestamp = output_timestamp.substr(0, output_timestamp.size() - 2);
nexus_output->next(manager->get_output_root() + output_timestamp + "NEXOUT", num_nexuses);

for(const auto& id : features.nexuses()) {
Expand Down
18 changes: 9 additions & 9 deletions test/utils/nexus_writer_Test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
class nexus_writer_Test : public ::testing::Test
{
protected:
~nexus_writer_Test() override {};
~nexus_writer_Test() override = default;

void TearDown() override
{
unlink((tempfile + ".csv").c_str());
// unlink((tempfile + ".nc").c_str());
unlink((tempfile + ".nc").c_str());
}

template<typename WriterType>
Expand Down Expand Up @@ -49,7 +49,7 @@ TEST_F(nexus_writer_Test, csv_writer)

std::string line;
stream >> line;
ASSERT_EQ(line, "nexus_id,segment_id,qSfcLatRunoff");
ASSERT_EQ(line, "feature_id,segment_id,q_lateral");
stream >> line;
ASSERT_EQ(line, "nex-1,wb-1,5");
stream >> line;
Expand All @@ -73,11 +73,11 @@ TEST_F(nexus_writer_Test, netcdf_writer)
ASSERT_FALSE(dim.isNull());
ASSERT_EQ(dim.getSize(), 2);

const auto& var_nexus_id = stream.getVar("nexus_id");
const auto& var_nexus_id = stream.getVar("feature_id");
ASSERT_FALSE(var_nexus_id.isNull());
char* nex_id_1;
char* nex_id_1 = nullptr;
var_nexus_id.getVar({ 0 }, &nex_id_1);
char* nex_id_2;
char* nex_id_2 = nullptr;
var_nexus_id.getVar({ 1 }, &nex_id_2);
ASSERT_STREQ(nex_id_1, "nex-1");
ASSERT_STREQ(nex_id_2, "nex-2");
Expand All @@ -86,16 +86,16 @@ TEST_F(nexus_writer_Test, netcdf_writer)

const auto& var_segment_id = stream.getVar("segment_id");
ASSERT_FALSE(var_segment_id.isNull());
char* seg_id_1;
char* seg_id_1 = nullptr;
var_segment_id.getVar({ 0 }, &seg_id_1);
char* seg_id_2;
char* seg_id_2 = nullptr;
var_segment_id.getVar({ 1 }, &seg_id_2);
ASSERT_STREQ(seg_id_1, "wb-1");
ASSERT_STREQ(seg_id_2, "wb-2");
delete seg_id_1;
delete seg_id_2;

const auto& var_qlat = stream.getVar("qSfcLatRunoff");
const auto& var_qlat = stream.getVar("q_lateral");
ASSERT_FALSE(var_qlat.isNull());
double qlat_1 = 0;
var_qlat.getVar({ 0 }, &qlat_1);
Expand Down

0 comments on commit 3cbdf52

Please sign in to comment.