add matlab to eis converter
This commit is contained in:
parent
a84154d92f
commit
04c5aab3e0
94
src/matlabarraytoeis.cpp
Normal file
94
src/matlabarraytoeis.cpp
Normal file
@ -0,0 +1,94 @@
|
||||
#include <kisstype/spectra.h>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <stdexcept>
|
||||
|
||||
#include "tokenize.h"
|
||||
|
||||
bool saveData(const std::string& exportName, const eis::Spectra& spectra, const std::filesystem::path& outDir, const std::filesystem::path& originFile)
|
||||
{
|
||||
std::filesystem::path filename;
|
||||
size_t index = 0;
|
||||
do
|
||||
{
|
||||
filename.assign(exportName);
|
||||
filename.concat("_");
|
||||
filename.concat(std::to_string(index));
|
||||
filename.concat(".csv");
|
||||
++index;
|
||||
} while(std::filesystem::exists(outDir/filename));
|
||||
|
||||
try
|
||||
{
|
||||
eis::Spectra(data.data, data.modelStr, data.id + ", \"" + originFile.filename().string() + "\"").saveToDisk(outDir/filename);
|
||||
return true;
|
||||
}
|
||||
catch(...)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<eis::DataPoint> parseLine(const std::string& line)
|
||||
{
|
||||
std::vector<eis::DataPoint> data;
|
||||
std::vector<std::string> tokens = tokenize(line, '\t');
|
||||
if(tokens.size() % 2 != 0)
|
||||
throw std::invalid_argument("Line must have an even number of tokens");
|
||||
|
||||
for(size_t i = 0; i < tokens.size()/2; ++i)
|
||||
data.push_back({std::stof(tokens[i]), std::stof(tokens[i+tokens.size()/2])});
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
if(argc != 5)
|
||||
{
|
||||
std::cout<<"Usage: "<<argv[0]<<" [rul or cap] [EIS_FILE] [RUL_FILE] [OUT_DIR]\n";
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
std::fstream eisFile(argv[2], std::ios_base::in);
|
||||
if(!eisFile.is_open())
|
||||
{
|
||||
std::cout<<"could not open "<<argv[2]<<'\n';
|
||||
return 2;
|
||||
}
|
||||
|
||||
std::fstream rulFile(argv[3], std::ios_base::in);
|
||||
if(!rulFile.is_open())
|
||||
{
|
||||
std::cout<<"could not open "<<argv[3]<<'\n';
|
||||
return 2;
|
||||
}
|
||||
|
||||
|
||||
for(std::string eisLine; std::getline(eisFile, eisLine).good();)
|
||||
{
|
||||
try
|
||||
{
|
||||
std::string rul;
|
||||
if(!std::getline(rulFile, rul).good())
|
||||
{
|
||||
std::cout<<argv[3]<<" dose not have the same number of lines as "<<argv[2]<<" aborting\n";
|
||||
return 3;
|
||||
}
|
||||
|
||||
std::vector<eis::DataPoint> data = parseLine(eisLine);
|
||||
eis::Spectra spectra (data, "Unkown", argv[2]);
|
||||
spectra.addLabel(argv[1], std::stod(rul));
|
||||
|
||||
|
||||
}
|
||||
catch(const std::invalid_argument& ex)
|
||||
{
|
||||
std::cout<<ex.what()<<std::endl;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user