Skip to content
Snippets Groups Projects
Commit 9f680191 authored by Carl Philipp Klemm's avatar Carl Philipp Klemm
Browse files

eistype: add exceptions, add function to load eis spectrum from disk

parent 4e2c2fef
No related branches found
No related tags found
No related merge requests found
...@@ -8,6 +8,7 @@ typedef double fvalue; ...@@ -8,6 +8,7 @@ typedef double fvalue;
namespace eis namespace eis
{ {
class DataPoint class DataPoint
{ {
public: public:
...@@ -109,8 +110,34 @@ public: ...@@ -109,8 +110,34 @@ public:
static std::vector<Range> rangesFromParamString(const std::string& paramStr, size_t count); 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 = ""); 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 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); fvalue eisNyquistDistance(const std::vector<eis::DataPoint>& a, const std::vector<eis::DataPoint>& b);
......
...@@ -19,8 +19,8 @@ bool eis::saveToDisk(const std::vector<DataPoint>& data, const std::string& file ...@@ -19,8 +19,8 @@ bool eis::saveToDisk(const std::vector<DataPoint>& data, const std::string& file
} }
if(!headStr.empty()) if(!headStr.empty())
file<<headStr<<'\n'; file<<headStr;
file<<"omega,real,im\n"; file<<"\nomega,real,im\n";
for(const eis::DataPoint& point : data) for(const eis::DataPoint& point : data)
file<<point.omega<<','<<point.im.real()<<','<<point.im.imag()<<'\n'; 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 ...@@ -28,6 +28,34 @@ bool eis::saveToDisk(const std::vector<DataPoint>& data, const std::string& file
return true; 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 void eis::Range::print(int level) const
{ {
Log(static_cast<Log::Level>(level))<<"Range "<<start<<'-'<<end<<' '<<count<<" steps"<<(log ? " Log" : ""); Log(static_cast<Log::Level>(level))<<"Range "<<start<<'-'<<end<<' '<<count<<" steps"<<(log ? " Log" : "");
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment