Move common stuff in seperate trabanslation unit
Rename main to madap
This commit is contained in:
44
src/common.cpp
Normal file
44
src/common.cpp
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
#include "common.h"
|
||||||
|
|
||||||
|
#include <eisgenerator/eistype.h>
|
||||||
|
#include <eisgenerator/translators.h>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
bool saveData(const std::string& exportName, const ModelData& data, const std::filesystem::path& outDir, const std::filesystem::path& originFile)
|
||||||
|
{
|
||||||
|
std::filesystem::path filename;
|
||||||
|
std::string modelStrWithoutParams = data.modelStr;
|
||||||
|
eis::purgeEisParamBrackets(modelStrWithoutParams);
|
||||||
|
size_t classIndex = 0;
|
||||||
|
|
||||||
|
do
|
||||||
|
{
|
||||||
|
filename.assign(exportName);
|
||||||
|
filename.concat("_");
|
||||||
|
filename.concat(modelStrWithoutParams);
|
||||||
|
filename.concat("_");
|
||||||
|
filename.concat(std::to_string(classIndex));
|
||||||
|
filename.concat(".csv");
|
||||||
|
++classIndex;
|
||||||
|
} while(std::filesystem::exists(outDir/filename));
|
||||||
|
|
||||||
|
if(!saveToDisk(data.data, outDir/filename, data.modelStr + ", " + data.id + ", \"" + std::string(originFile.filename()) + "\""))
|
||||||
|
{
|
||||||
|
std::cerr<<"Unable to save to "<<outDir/filename;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool checkDir(const std::filesystem::path& outDir)
|
||||||
|
{
|
||||||
|
if(!std::filesystem::is_directory(outDir))
|
||||||
|
{
|
||||||
|
if(!std::filesystem::create_directory(outDir))
|
||||||
|
{
|
||||||
|
std::cerr<<outDir<<" dose not exist and can not be created\n";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
13
src/common.h
Normal file
13
src/common.h
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
#include <filesystem>
|
||||||
|
#include <vector>
|
||||||
|
#include <eisgenerator/eistype.h>
|
||||||
|
|
||||||
|
struct ModelData
|
||||||
|
{
|
||||||
|
std::string modelStr;
|
||||||
|
std::string id;
|
||||||
|
std::vector<eis::DataPoint> data;
|
||||||
|
};
|
||||||
|
|
||||||
|
bool saveData(const std::string& exportName, const ModelData& data, const std::filesystem::path& outDir, const std::filesystem::path& originFile);
|
||||||
|
bool checkDir(const std::filesystem::path& outDir);
|
83
src/madap.cpp
Normal file
83
src/madap.cpp
Normal file
@ -0,0 +1,83 @@
|
|||||||
|
#include <eisgenerator/translators.h>
|
||||||
|
#include <eisgenerator/eistype.h>
|
||||||
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
#include <filesystem>
|
||||||
|
|
||||||
|
#include "csvBackend.h"
|
||||||
|
#include "tokenize.h"
|
||||||
|
#include "common.h"
|
||||||
|
|
||||||
|
void trimBrackets(std::string& str)
|
||||||
|
{
|
||||||
|
if(str[0] == '[')
|
||||||
|
str.erase(str.begin());
|
||||||
|
if(str.back() == ']')
|
||||||
|
str.pop_back();
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<eis::DataPoint> parseEisDataFromMadapArrays(std::string& freqArray, std::string& realArray, std::string& imaginaryArray)
|
||||||
|
{
|
||||||
|
assert(realArray.size() > 2);
|
||||||
|
assert(imaginaryArray.size() > 2);
|
||||||
|
trimBrackets(realArray);
|
||||||
|
trimBrackets(imaginaryArray);
|
||||||
|
trimBrackets(freqArray);
|
||||||
|
|
||||||
|
std::vector<std::string> freqTokens = tokenize(freqArray, ',');
|
||||||
|
std::vector<std::string> realTokens = tokenize(realArray, ',');
|
||||||
|
std::vector<std::string> imaginaryTokens = tokenize(imaginaryArray, ',');
|
||||||
|
assert(realTokens.size() == freqTokens.size() && realTokens.size() == imaginaryTokens.size());
|
||||||
|
|
||||||
|
std::vector<eis::DataPoint> data;
|
||||||
|
data.reserve(freqTokens.size());
|
||||||
|
for(size_t i = 0; i < freqTokens.size(); ++i)
|
||||||
|
data.push_back(eis::DataPoint(std::complex<fvalue>(std::stod(realTokens[i]), std::stod(imaginaryTokens[i])), 2*M_PI*std::stod(freqTokens[i])));
|
||||||
|
|
||||||
|
std::sort(data.begin(), data.end(), [](const eis::DataPoint& a, const eis::DataPoint& b){return a.omega < b.omega;});
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char** argv)
|
||||||
|
{
|
||||||
|
if(argc < 2)
|
||||||
|
{
|
||||||
|
std::cerr<<"Usage: "<<(argc > 0 ? argv[0] : "NULL")<<" [FILE] [OUTDIR]\n";
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
io::CSVReader<7, io::trim_chars<' ', '\t'>, io::no_quote_escape<';'>> csvFile(argv[1]);
|
||||||
|
csvFile.read_header(io::ignore_extra_column, "experimentID", "temperature", "frequency",
|
||||||
|
"real_impedance", "imaginary_impedance", "EIS_fittedParameters", "EIS_circuit");
|
||||||
|
|
||||||
|
if(!csvFile.next_line())
|
||||||
|
return 1;
|
||||||
|
if(!csvFile.next_line())
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
std::string frequencyArray;
|
||||||
|
std::string realArray;
|
||||||
|
std::string imaginaryArray;
|
||||||
|
std::string paramArray;
|
||||||
|
std::string circutString;
|
||||||
|
std::string idStr;
|
||||||
|
double temperature;
|
||||||
|
|
||||||
|
std::filesystem::path outDir("./out");
|
||||||
|
if(argc == 3)
|
||||||
|
outDir = argv[2];
|
||||||
|
|
||||||
|
if(!checkDir(outDir))
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
while(csvFile.read_row(idStr, temperature, frequencyArray, realArray, imaginaryArray, paramArray, circutString))
|
||||||
|
{
|
||||||
|
ModelData data;
|
||||||
|
data.modelStr = eis::madapToEis(circutString, paramArray);
|
||||||
|
data.id = idStr;
|
||||||
|
|
||||||
|
data.data = parseEisDataFromMadapArrays(frequencyArray, realArray, imaginaryArray);
|
||||||
|
saveData("madap", data, outDir, argv[1]);
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user