From 9f680191b9bd0670a1b3ae067abbe1ab3e14d80f Mon Sep 17 00:00:00 2001 From: Carl Philipp Klemm <philipp@uvos.xyz> Date: Tue, 24 Jan 2023 09:21:52 +0100 Subject: [PATCH] eistype: add exceptions, add function to load eis spectrum from disk --- eisgenerator/eistype.h | 27 +++++++++++++++++++++++++++ eistype.cpp | 32 ++++++++++++++++++++++++++++++-- 2 files changed, 57 insertions(+), 2 deletions(-) diff --git a/eisgenerator/eistype.h b/eisgenerator/eistype.h index c1c0466..116ef3e 100644 --- a/eisgenerator/eistype.h +++ b/eisgenerator/eistype.h @@ -8,6 +8,7 @@ typedef double fvalue; namespace eis { + class DataPoint { public: @@ -109,8 +110,34 @@ public: static std::vector<Range> rangesFromParamString(const std::string& paramStr, size_t count); }; +class parse_errror: public std::exception +{ + std::string whatStr; +public: + parse_errror(const std::string& whatIn): whatStr(whatIn) + {} + virtual const char* what() const noexcept override + { + return whatStr.c_str(); + } +}; + +class file_error: public std::exception +{ + std::string whatStr; +public: + file_error(const std::string& whatIn): whatStr(whatIn) + {} + virtual const char* what() const noexcept override + { + return whatStr.c_str(); + } +}; + bool saveToDisk(const std::vector<DataPoint>& data, const std::string& fileName, std::string headStr = ""); +std::pair<std::vector<DataPoint>, std::string> loadFromDisk(const std::string& fileName); + fvalue eisDistance(const std::vector<eis::DataPoint>& a, const std::vector<eis::DataPoint>& b); fvalue eisNyquistDistance(const std::vector<eis::DataPoint>& a, const std::vector<eis::DataPoint>& b); diff --git a/eistype.cpp b/eistype.cpp index 5c43785..3a9ef70 100644 --- a/eistype.cpp +++ b/eistype.cpp @@ -19,8 +19,8 @@ bool eis::saveToDisk(const std::vector<DataPoint>& data, const std::string& file } if(!headStr.empty()) - file<<headStr<<'\n'; - file<<"omega,real,im\n"; + file<<headStr; + file<<"\nomega,real,im\n"; for(const eis::DataPoint& point : data) file<<point.omega<<','<<point.im.real()<<','<<point.im.imag()<<'\n'; @@ -28,6 +28,34 @@ bool eis::saveToDisk(const std::vector<DataPoint>& data, const std::string& file return true; } +std::pair<std::vector<DataPoint>, std::string> eis::loadFromDisk(const std::string& fileName) +{ + std::fstream file; + file.open(fileName, std::ios_base::in); + if(!file.is_open()) + throw file_error("can not open " + fileName + " for reading\n"); + + std::pair<std::vector<DataPoint>, std::string> out; + + std::getline(file, out.second); + + std::string line; + std::getline(file, line); + while(file.good()) + { + std::getline(file, line); + if(line.empty() || line[0] == '#') + continue; + std::vector<std::string> tokens = tokenize(line, ','); + if(tokens.size() != 3) + throw file_error("invalid line in " + fileName + ": " + line); + out.first.push_back(DataPoint({std::stod(tokens[1]), std::stod(tokens[2])}, std::stod(tokens[0]))); + } + + file.close(); + return out; +} + void eis::Range::print(int level) const { Log(static_cast<Log::Level>(level))<<"Range "<<start<<'-'<<end<<' '<<count<<" steps"<<(log ? " Log" : ""); -- GitLab