Skip to content

Commit

Permalink
Add "body_fat_by_hft" as new output table
Browse files Browse the repository at this point in the history
Compare gh-43
  • Loading branch information
Wolfgang Traylor committed Jun 14, 2021
1 parent 7022632 commit bd8e704
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 7 deletions.
1 change: 1 addition & 0 deletions examples/megafauna.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ directory = "."
precision = 4
tables = [
"available_forage",
"body_fat_by_hft",
"digestibility",
"eaten_forage_per_ind",
"eaten_nitrogen_per_ind",
Expand Down
21 changes: 17 additions & 4 deletions src/Fauna/Output/text_table_writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,19 @@ TextTableWriter::TextTableWriter(const OutputInterval interval,
create_directories(dir);

// Add all selected output files to list of file streams.
// -> Add new output files here in alphabetical order.
if (options.available_forage) {
const std::string path = dir + "/available_forage" + FILE_EXTENSION;
check_file_exists(path);
file_streams.push_back(&available_forage);
available_forage.open(path);
}
if (options.body_fat_by_hft) {
const std::string path = dir + "/body_fat_by_hft" + FILE_EXTENSION;
check_file_exists(path);
file_streams.push_back(&body_fat_by_hft);
body_fat_by_hft.open(path);
}
if (options.digestibility) {
const std::string path = dir + "/digestibility" + FILE_EXTENSION;
check_file_exists(path);
Expand All @@ -58,7 +65,6 @@ TextTableWriter::TextTableWriter(const OutputInterval interval,
file_streams.push_back(&mass_density_per_hft);
mass_density_per_hft.open(path);
}
// -> Add new output files here.

for (auto& s : file_streams) {
// Set precision for all output streams.
Expand Down Expand Up @@ -184,13 +190,14 @@ void TextTableWriter::write_datapoint(const Datapoint& datapoint) {
else
digestibility << FIELD_SEPARATOR << NA_VALUE;
}
// Add more tables here.
// -> Add more tables here in alphabetical order.
}
if (available_forage.is_open()) available_forage << std::endl;
if (digestibility.is_open()) digestibility << std::endl;
// Add more tables here.
// -> Add more tables here in alphabetical order.

// Per-HFT Tables
if (body_fat_by_hft.is_open()) start_row(datapoint, body_fat_by_hft);
if (eaten_nitrogen_per_ind.is_open())
start_row(datapoint, eaten_nitrogen_per_ind);
if (mass_density_per_hft.is_open())
Expand All @@ -200,15 +207,18 @@ void TextTableWriter::write_datapoint(const Datapoint& datapoint) {
const HerbivoreData* d = get_hft_data(&datapoint, hft_name);
assert(d);
// Add datum for this HFT.
if (body_fat_by_hft.is_open())
body_fat_by_hft << FIELD_SEPARATOR << d->bodyfat;
if (eaten_nitrogen_per_ind.is_open())
eaten_nitrogen_per_ind << FIELD_SEPARATOR << d->eaten_nitrogen_per_ind;
if (mass_density_per_hft.is_open())
mass_density_per_hft << FIELD_SEPARATOR << d->massdens;
// -> Add more per-HFT tables here in alphabetical order.
}
if (body_fat_by_hft.is_open()) body_fat_by_hft << std::endl;
if (eaten_nitrogen_per_ind.is_open()) eaten_nitrogen_per_ind << std::endl;
if (mass_density_per_hft.is_open()) mass_density_per_hft << std::endl;
// Add more tables here.
// -> Add more tables here in alphabetical order.

// Per-HFT/Per-Forage Tables
// There is one new row for each forage type.
Expand Down Expand Up @@ -283,6 +293,9 @@ void TextTableWriter::write_captions(const Datapoint& datapoint) {
hft_name + "' contains the field delimiter '" + FIELD_SEPARATOR +
"'");

// -> Add new output files here in alphabetical order.
if (body_fat_by_hft.is_open())
body_fat_by_hft << FIELD_SEPARATOR << hft_name;
if (eaten_forage_per_ind.is_open())
eaten_forage_per_ind << FIELD_SEPARATOR << hft_name;
if (eaten_nitrogen_per_ind.is_open())
Expand Down
1 change: 1 addition & 0 deletions src/Fauna/Output/text_table_writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ class TextTableWriter : public WriterInterface {

/** @{ \name File Streams */
std::ofstream available_forage;
std::ofstream body_fat_by_hft;
std::ofstream digestibility;
std::ofstream eaten_forage_per_ind;
std::ofstream eaten_nitrogen_per_ind;
Expand Down
6 changes: 5 additions & 1 deletion src/Fauna/Output/text_table_writer_options.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ struct TextTableWriterOptions {

/** @} */

/** @{ \name Per-HFT tables: one column per HFT. */
/** @{ \name By-HFT tables: one column per HFT. */

/// Proportional body fat, i.e. fat mass per total body mass [kg/kg].
/** \see \ref Fauna::HerbivoreBase::get_bodyfat() */
bool body_fat_by_hft = false;

/// Daily consumption of nitrogen by herbivore individuals [mgDM/day/ind].
/**
Expand Down
7 changes: 5 additions & 2 deletions src/Fauna/insfile_reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,8 @@ void InsfileReader::read_table_output_text_tables() {
for (const auto& s : *value)
if (lowercase(s) == "available_forage")
params.output_text_tables.available_forage = true;
else if (lowercase(s) == "body_fat_by_hft")
params.output_text_tables.body_fat_by_hft = true;
else if (lowercase(s) == "digestibility")
params.output_text_tables.digestibility = true;
else if (lowercase(s) == "eaten_forage_per_ind")
Expand All @@ -847,8 +849,9 @@ void InsfileReader::read_table_output_text_tables() {
else
throw invalid_option(
key, s,
{"available_forage", "digestibility", "eaten_forage_per_ind",
"eaten_nitrogen_per_ind", "mass_density_per_hft"});
{"available_forage", "body_fat_by_hft", "digestibility",
"eaten_forage_per_ind", "eaten_nitrogen_per_ind",
"mass_density_per_hft"});
}
}
// Remove the table "output" in order to indicate that it’s been parsed.
Expand Down

0 comments on commit bd8e704

Please sign in to comment.