Modular Megafauna Model 1.1.5
A physiological, dynamic herbivore simulator in C++.
Loading...
Searching...
No Matches
Fauna::Output::TextTableWriter Class Reference

Writes output data to tabular plaintext files. More...

#include <text_table_writer.h>

Inheritance diagram for Fauna::Output::TextTableWriter:
Fauna::Output::WriterInterface

Public Member Functions

 TextTableWriter (const OutputInterval interval, const TextTableWriterOptions &options, const std::set< std::string > hft_names)
 Constructor. More...
 
virtual void write_datapoint (const Datapoint &datapoint)
 Append spatially & temporally aggregated output data to table files. More...
 
- Public Member Functions inherited from Fauna::Output::WriterInterface
virtual ~WriterInterface ()
 Virtual Destructor. More...
 
virtual void write_datapoint (const Datapoint &datapoint)=0
 Write spatially & temporally aggregated output data. More...
 

Static Public Attributes

static const char * NA_VALUE = "NA"
 String to print for values that are not available (NA). More...
 
static const char FIELD_SEPARATOR = '\t'
 Character to separate columns. More...
 
static const char * FILE_EXTENSION = ".tsv"
 File extension for tabular plaintext files. More...
 

Private Member Functions

const HerbivoreDataget_hft_data (const Datapoint *datapoint, const std::string &hft_name) const
 Retrieve herbivore data from datapoint for given HFT. More...
 
void start_row (const Datapoint &datapoint, std::ofstream &table)
 Create a new row by writing year/month/day and aggregation unit. More...
 
void write_captions (const Datapoint &datapoint)
 Write the first line in the output files: column headers. More...
 

Static Private Member Functions

static void check_file_exists (const std::string &path)
 Throw an exception if output file already exists. More...
 

Private Attributes

bool captions_written = false
 Whether column captions have already been written to file. More...
 
std::vector< std::ofstream * > file_streams
 List of pointers to the user-selected and active file streams. More...
 
const std::set< std::string > hft_names
 List of Hft names (Fauna::Hft::name) in constant order. More...
 
const OutputInterval interval
 User-selected utput interval. More...
 
const TextTableWriterOptions options
 User options from the instruction file. More...
 
File Streams
std::ofstream available_forage
 
std::ofstream body_fat
 
std::ofstream digestibility
 
std::ofstream eaten_forage_per_ind
 
std::ofstream eaten_nitrogen_per_ind
 
std::ofstream individual_density
 
std::ofstream mass_density
 
std::ofstream mass_density_per_hft
 

Detailed Description

Writes output data to tabular plaintext files.

In Parameters there are boolean variables prefixed with "text_tables". They are switches to enable and disable the creation of different data tables in plaintext files. The files have the same name as the corresponding boolean variable in TextTableWriterOptions. All files are created in a directory specified by TextTableWriterOptions::directory.

Constructor & Destructor Documentation

◆ TextTableWriter()

TextTableWriter::TextTableWriter ( const OutputInterval  interval,
const TextTableWriterOptions options,
const std::set< std::string >  hft_names 
)

Constructor.

Create all files that are selected in options as empty files. Herbivore output files are only created if hft_names is not empty.

Parameters
intervalSelector if output is daily/monthly/annual/...
optionsSpecific user-defined options for this class.
hft_namesAll HFTs in the simulation (see Fauna::Hft::name).
Exceptions
std::runtime_errorIf one of the output files already exists.

Member Function Documentation

◆ check_file_exists()

void TextTableWriter::check_file_exists ( const std::string &  path)
staticprivate

Throw an exception if output file already exists.

◆ get_hft_data()

const HerbivoreData * TextTableWriter::get_hft_data ( const Datapoint datapoint,
const std::string &  hft_name 
) const
private

Retrieve herbivore data from datapoint for given HFT.

The return type is a pointer in order to minimize copying memory. It is save as long as datapoint is stable. If there is no data for the given HFT, an empty record is returned.

Parameters
datapointWhere the herbivore data is in Datapoint::data.
hft_nameThe name of the HFT in Datapoint::data.
Returns
Pointer to HerbivoreData in datapoint if it exists, otherwise a pointer to an empty HerbivoreData object.
See also
HerbivoreInterface::get_output_group() is the “HFT”.

◆ start_row()

void TextTableWriter::start_row ( const Datapoint datapoint,
std::ofstream &  table 
)
private

Create a new row by writing year/month/day and aggregation unit.

There is no FIELD_SEPARATOR at the end of the row.

Parameters
datapointContains the date and aggregation unit.
tableOutput stream to write to. This is a member variable of this class.

◆ write_captions()

void TextTableWriter::write_captions ( const Datapoint datapoint)
private

Write the first line in the output files: column headers.

Parameters
datapointAny output data with the same structure (e.g. list of HFTs) as following output data points will have.
Exceptions
std::logic_errorIf the OutputInterval is not implemented.
std::invalid_argumentIf an HFT name contains whitespaces or the FIELD_SEPARATOR.

◆ write_datapoint()

void TextTableWriter::write_datapoint ( const Datapoint datapoint)
virtual

Append spatially & temporally aggregated output data to table files.

Parameters
datapointThe output data to write.
Warning
datapoint must not change its structure between calls. For example, the list of HFTs must not change. This is because the column captions are written in the first call to this function. In subsequent calls there is no check whether the data tuples match the column captions.
Exceptions
std::invalid_argumentIf an HFT name contains whitespaces or the FIELD_SEPARATOR (only checked on first call).
std::invalid_argumentIf datapoint.data.datapoint_count is zero.
std::invalid_argumentIf datapoint.interval does not match the given OutputInterval.
std::invalid_argumentIf datapoint.aggregation_unit contains a whitespace or FIELD_SEPARATOR.
std::logic_errorIf the OutputInterval is not implemented.
std::runtime_errorIf datapoint.data.hft_data contains an unknown HFT (checked by comparing Fauna::Hft::name).

Implements Fauna::Output::WriterInterface.

Member Data Documentation

◆ available_forage

std::ofstream Fauna::Output::TextTableWriter::available_forage
private

◆ body_fat

std::ofstream Fauna::Output::TextTableWriter::body_fat
private

◆ captions_written

bool Fauna::Output::TextTableWriter::captions_written = false
private

Whether column captions have already been written to file.

◆ digestibility

std::ofstream Fauna::Output::TextTableWriter::digestibility
private

◆ eaten_forage_per_ind

std::ofstream Fauna::Output::TextTableWriter::eaten_forage_per_ind
private

◆ eaten_nitrogen_per_ind

std::ofstream Fauna::Output::TextTableWriter::eaten_nitrogen_per_ind
private

◆ FIELD_SEPARATOR

const char Fauna::Output::TextTableWriter::FIELD_SEPARATOR = '\t'
static

Character to separate columns.

◆ FILE_EXTENSION

const char * TextTableWriter::FILE_EXTENSION = ".tsv"
static

File extension for tabular plaintext files.

◆ file_streams

std::vector<std::ofstream*> Fauna::Output::TextTableWriter::file_streams
private

List of pointers to the user-selected and active file streams.

◆ hft_names

const std::set<std::string> Fauna::Output::TextTableWriter::hft_names
private

List of Hft names (Fauna::Hft::name) in constant order.

◆ individual_density

std::ofstream Fauna::Output::TextTableWriter::individual_density
private

◆ interval

const OutputInterval Fauna::Output::TextTableWriter::interval
private

User-selected utput interval.

◆ mass_density

std::ofstream Fauna::Output::TextTableWriter::mass_density
private

◆ mass_density_per_hft

std::ofstream Fauna::Output::TextTableWriter::mass_density_per_hft
private

◆ NA_VALUE

const char * TextTableWriter::NA_VALUE = "NA"
static

String to print for values that are not available (NA).

◆ options

const TextTableWriterOptions Fauna::Output::TextTableWriter::options
private

User options from the instruction file.


The documentation for this class was generated from the following files: