Reworked Populations Loggers.
Added Save and Load Option for Population Data.
This commit is contained in:
@@ -19,90 +19,90 @@
|
||||
#include "error_types.h"
|
||||
|
||||
|
||||
inline double getParameterValue(std::string lineFromFile) {
|
||||
inline double get_parameter_value(std::string csv_line) {
|
||||
std::stringstream ss;
|
||||
ss.str(lineFromFile);
|
||||
std::string nameDummy;
|
||||
double parValue = NAN;
|
||||
if ( !(ss>>nameDummy && ss>>parValue) ) {
|
||||
std::stringstream sserr;
|
||||
sserr<<"isParameterLine: Could not read line.\n";
|
||||
throw MyException(sserr.str());
|
||||
ss.str(csv_line);
|
||||
std::string dummy;
|
||||
double parameter_value = NAN;
|
||||
if ( !(ss>>dummy && ss>>parameter_value) ) {
|
||||
std::stringstream ss_err;
|
||||
ss_err<<"get_parameter_value: Could not read line.\n";
|
||||
throw MyException(ss_err.str());
|
||||
}
|
||||
return (parValue);
|
||||
return (parameter_value);
|
||||
}
|
||||
|
||||
inline bool isParameterLine(std::string lineFromFile, const std::string &parName) {
|
||||
inline bool is_parameter_line(std::string csv_line, const std::string ¶meter_name) {
|
||||
std::stringstream ss;
|
||||
ss.str(lineFromFile);
|
||||
std::string parNameTemp;
|
||||
if ( !(ss>>parNameTemp) ) {
|
||||
std::stringstream sserr;
|
||||
sserr<<"isParameterLine: Could not read line.\n";
|
||||
throw MyException(sserr.str());
|
||||
ss.str(csv_line);
|
||||
std::string temp;
|
||||
if ( !(ss>>temp) ) {
|
||||
std::stringstream ss_err;
|
||||
ss_err<<"is_parameter_line: Could not read line.\n";
|
||||
throw MyException(ss_err.str());
|
||||
}
|
||||
if (parNameTemp == parName) {
|
||||
if (temp == parameter_name) {
|
||||
return (true);
|
||||
} else {
|
||||
return (false);
|
||||
}
|
||||
}
|
||||
|
||||
inline bool isFaulty(std::string lineFromFile) {
|
||||
inline bool is_faulty(std::string csv_line) {
|
||||
std::stringstream ss;
|
||||
ss.str(lineFromFile);
|
||||
std::string nameDummy;
|
||||
double valueDummy;
|
||||
if (ss>>nameDummy && ss>>valueDummy) {
|
||||
ss.str(csv_line);
|
||||
std::string name_dummy;
|
||||
double value_dummy;
|
||||
if (ss>>name_dummy && ss>>value_dummy) {
|
||||
return (false);
|
||||
} else {
|
||||
return (true);
|
||||
}
|
||||
}
|
||||
|
||||
inline bool checkIgnoreLine(const std::string &lineFromFile) { // true if line is empty or starts with #
|
||||
if (lineFromFile[0] =='#') {
|
||||
inline bool check_ignore_line(const std::string &csv_line) { // true if line is empty or starts with #
|
||||
if (csv_line[0] =='#') {
|
||||
return true;
|
||||
} else if ( lineFromFile.empty() ) {
|
||||
} else if ( csv_line.empty() ) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
inline void replaceDelimiter(std::string &lineFromFile) { // replaces delimiter (';'',''=') with whitespace
|
||||
replace(lineFromFile.begin(), lineFromFile.end(), ';', ' ');
|
||||
replace(lineFromFile.begin(), lineFromFile.end(), ',', ' ');
|
||||
replace(lineFromFile.begin(), lineFromFile.end(), '=', ' ');
|
||||
inline void replace_delimiter(std::string &csv_line) { // replaces delimiter (';'',''=') with whitespace
|
||||
replace(csv_line.begin(), csv_line.end(), ';', ' ');
|
||||
replace(csv_line.begin(), csv_line.end(), ',', ' ');
|
||||
replace(csv_line.begin(), csv_line.end(), '=', ' ');
|
||||
}
|
||||
|
||||
inline void removeLeadingWhites(std::string &lineFromFile) {
|
||||
static const std::string WHITESPACE = " \n\r\t\f\v";
|
||||
size_t start = lineFromFile.find_first_not_of(WHITESPACE);
|
||||
lineFromFile = (start == std::string::npos) ? "" : lineFromFile.substr(start);
|
||||
inline void remove_leading_whites(std::string &csv_line) {
|
||||
static const std::string whitespaces = " \n\r\t\f\v";
|
||||
size_t start = csv_line.find_first_not_of(whitespaces);
|
||||
csv_line = (start == std::string::npos) ? "" : csv_line.substr(start);
|
||||
}
|
||||
|
||||
inline double findParameter(std::ifstream *fileStream, const std::string &parName) {
|
||||
std::string lineFromFile;
|
||||
double parValue = NAN;
|
||||
while (getline(*fileStream, lineFromFile)) {
|
||||
removeLeadingWhites(lineFromFile);
|
||||
replaceDelimiter(lineFromFile);
|
||||
if ( checkIgnoreLine(lineFromFile) ) {
|
||||
inline double find_parameter(std::ifstream *parameter_file_stream, const std::string ¶meter_name) {
|
||||
std::string csv_line;
|
||||
double parameter_value = NAN;
|
||||
while (getline(*parameter_file_stream, csv_line)) {
|
||||
remove_leading_whites(csv_line);
|
||||
replace_delimiter(csv_line);
|
||||
if ( check_ignore_line(csv_line) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ( isFaulty(lineFromFile) ) {
|
||||
std::cout<<"Line in parameter file faulty.\n\tline:\t"<<lineFromFile<<"\n";
|
||||
} else if ( isParameterLine(lineFromFile, parName) ) {
|
||||
parValue = getParameterValue(lineFromFile);
|
||||
if ( is_faulty(csv_line) ) {
|
||||
std::cout<<"Line in parameter file faulty.\n\tline:\t"<<csv_line<<"\n";
|
||||
} else if ( is_parameter_line(csv_line, parameter_name) ) {
|
||||
parameter_value = get_parameter_value(csv_line);
|
||||
break;
|
||||
}
|
||||
}
|
||||
return (parValue);
|
||||
return (parameter_value);
|
||||
}
|
||||
|
||||
inline std::string makePath (const std::string &path) { // append '/' if necessary to path.
|
||||
inline std::string make_path (const std::string &path) { // append '/' if necessary to path.
|
||||
if (path.back() != '/') {
|
||||
return (path + '/');
|
||||
} else {
|
||||
@@ -110,45 +110,45 @@ inline std::string makePath (const std::string &path) { // append '/' if necessa
|
||||
}
|
||||
}
|
||||
|
||||
inline std::string getEnv (const std::string &envName) { // string version of C function getenv.
|
||||
inline std::string get_enviroment_variable (const std::string &envName) { // string version of C function getenv.
|
||||
char * val = getenv( envName.c_str() );
|
||||
return val == NULL ? std::string("") : std::string(val);
|
||||
}
|
||||
|
||||
inline std::string getParameterLoadFilePath() {
|
||||
std::string parpath = getEnv("parpath");
|
||||
if (parpath == "") {
|
||||
inline std::string get_parameter_file_path() {
|
||||
std::string parameter_file_path = get_enviroment_variable("parpath");
|
||||
if (parameter_file_path == "") {
|
||||
throw MyException("Parameter::getParameter: Environment variable parpath not defined.");
|
||||
}
|
||||
std::string parfile = getEnv("parfile");
|
||||
if (parfile == "") {
|
||||
std::string parameter_file_name = get_enviroment_variable("parfile");
|
||||
if (parameter_file_name == "") {
|
||||
throw MyException("Parameter::getParameter: Environment variable parfile not defined.");
|
||||
}
|
||||
|
||||
parpath = makePath(parpath);
|
||||
return (parpath + parfile);
|
||||
parameter_file_path = make_path(parameter_file_path);
|
||||
return (parameter_file_path + parameter_file_name);
|
||||
}
|
||||
|
||||
inline double getParameter(const std::string &parName) {
|
||||
std::string loadFilePath = getParameterLoadFilePath();
|
||||
std::ifstream fileStream(loadFilePath);
|
||||
if (!fileStream.good()) {
|
||||
inline double load_parameter(const std::string ¶meter_name) {
|
||||
std::string parameter_file_path = get_parameter_file_path();
|
||||
std::ifstream parameter_file_stream(parameter_file_path);
|
||||
if (!parameter_file_stream.good()) {
|
||||
std::stringstream ss;
|
||||
ss<<"Parameter::getParameter: Could not open file.\n loadFilePath:\t"<<loadFilePath<<"\n";
|
||||
ss<<"Parameter::getParameter: Could not open file.\n loadFilePath:\t"<<parameter_file_path<<"\n";
|
||||
throw MyException(ss.str());
|
||||
}
|
||||
double parValue = findParameter(&fileStream, parName);
|
||||
double parameter_value = find_parameter(¶meter_file_stream, parameter_name);
|
||||
|
||||
if ( isnan(parValue) ) {
|
||||
if ( isnan(parameter_value) ) {
|
||||
std::stringstream ss;
|
||||
ss<<"Parameter::getParameter: Parameter not found in file.\n parName:\t"<<parName<<"\n loadFilePath: \t"<<loadFilePath;
|
||||
ss<<"Parameter::getParameter: Parameter not found in file.\n parameter_name:\t"<<parameter_name<<"\n loadFilePath: \t"<<parameter_file_path;
|
||||
throw MyException(ss.str());
|
||||
} else {
|
||||
return (parValue);
|
||||
return (parameter_value);
|
||||
}
|
||||
}
|
||||
|
||||
inline std::string getTimeString() {
|
||||
inline std::string get_time_string() {
|
||||
time_t rawtime;
|
||||
struct tm * timeinfo;
|
||||
char buffer[80];
|
||||
@@ -159,38 +159,38 @@ inline std::string getTimeString() {
|
||||
return(str);
|
||||
}
|
||||
|
||||
inline std::string getHomebase() {
|
||||
std::string Homebase = getEnv("HOMEBASE");
|
||||
Homebase = makePath(Homebase);
|
||||
inline std::string get_homebase() {
|
||||
std::string Homebase = get_enviroment_variable("HOMEBASE");
|
||||
Homebase = make_path(Homebase);
|
||||
return (Homebase);
|
||||
}
|
||||
|
||||
inline std::string getDataPath() {
|
||||
return (getHomebase() + "src/mc_electron/data/");
|
||||
inline std::string get_data_path() {
|
||||
return (get_homebase() + "src/mc_electron/data/");
|
||||
}
|
||||
|
||||
inline std::string remove_catEnding (std::string inString) {
|
||||
if ( inString.substr(inString.size()-4, 4) == ".cat" ){
|
||||
return (inString.substr(0, inString.size()-4) );
|
||||
inline std::string remove_cat (std::string cat_string) {
|
||||
if ( cat_string.substr(cat_string.size()-4, 4) == ".cat" ){
|
||||
return (cat_string.substr(0, cat_string.size()-4) );
|
||||
} else {
|
||||
return (inString);
|
||||
return (cat_string);
|
||||
}
|
||||
}
|
||||
|
||||
inline std::string getparName() {
|
||||
std::string parpath = getenv("parpath");
|
||||
std::string parfile = remove_catEnding(getenv("parfile"));
|
||||
return (parpath + "/" + parfile);
|
||||
inline std::string get_parameter_filename() {
|
||||
std::string parameter_file_path = getenv("parpath");
|
||||
std::string parameter_file_name = remove_cat(getenv("parfile"));
|
||||
return (parameter_file_path + "/" + parameter_file_name);
|
||||
}
|
||||
|
||||
inline bool isFile(std::string fileName) {
|
||||
inline bool is_file(std::string file_path) {
|
||||
struct stat buffer;
|
||||
return (stat (fileName.c_str(), &buffer) == 0);
|
||||
return (stat (file_path.c_str(), &buffer) == 0);
|
||||
}
|
||||
|
||||
inline bool isDir(std::string dirPath) {
|
||||
inline bool is_dir(std::string dir_path) {
|
||||
struct stat info;
|
||||
if( stat( dirPath.c_str(), &info ) != 0 )
|
||||
if( stat( dir_path.c_str(), &info ) != 0 )
|
||||
return (false);
|
||||
else if( info.st_mode & S_IFDIR ) // S_ISDIR() doesn't exist on my windows
|
||||
return (true);
|
||||
|
||||
Reference in New Issue
Block a user