cpp/utils/io.cpp

165 lines
4.8 KiB
C++
Raw Normal View History

2024-08-16 17:55:27 +00:00
//
// Created by dominik on 8/14/24.
//
#include "io.h"
#include <sstream>
#include <fstream>
#include <iostream>
#include <algorithm>
2024-11-11 13:07:21 +00:00
#include <complex>
2024-08-16 17:55:27 +00:00
#include <vector>
#include <iomanip>
#include <unordered_map>
#include <map>
#include <string>
2024-08-18 11:21:27 +00:00
#include <filesystem>
2024-08-16 17:55:27 +00:00
2024-08-20 15:51:49 +00:00
2024-11-11 13:07:21 +00:00
Arguments parse_args(const int argc, char* argv[]) {
2024-11-10 14:52:54 +00:00
if (argc < 3) {
2024-08-20 15:51:49 +00:00
throw std::runtime_error("Not enough arguments: missing parameter file");
}
Arguments args;
2024-11-11 13:07:21 +00:00
const std::vector<std::string> input_args(argv + 1, argv + argc);
for (auto it = input_args.begin(); it != input_args.end(); ++it) {
std::cout << *it << std::endl;
if (it->at(0) == '-') {
if (*it == "--spectrum") {
2024-08-20 15:51:49 +00:00
args.spectrum = true;
2024-11-11 13:07:21 +00:00
std::cout << "spectrum: " << args.spectrum << std::endl;
continue;
}
if (*it == "--ste") {
args.ste = true;
std::cout << "ste: " << args.ste << std::endl;
continue;
}
std::string stripped_arg;
if (it->size() > 2 && it->at(0) == '-' && it->at(1) == '-') {
stripped_arg = it->substr(2, it->size());
} else if (it->size() > 1 && it->at(0) == '-') {
stripped_arg = it->substr(1, it->size());
2024-08-20 15:51:49 +00:00
}
2024-11-11 13:07:21 +00:00
std::transform(stripped_arg.begin(), stripped_arg.end(), stripped_arg.begin(), [](unsigned char c) { return std::tolower(c); });
const auto stripped_value = std::stod(*(++it), nullptr);
args.optional[stripped_arg] = stripped_value;
continue;
}
if (args.parameter_file.empty()) {
std::cout << *it << " is parameter_file" << std::endl;
args.parameter_file = *it;
continue;
2024-08-20 15:51:49 +00:00
}
2024-11-11 13:07:21 +00:00
if (args.motion_type.empty()) {
std::cout << *it << " is motion model" << std::endl;
args.motion_type = *it;
continue;
}
throw std::invalid_argument("too many positional arguments");
2024-08-20 15:51:49 +00:00
}
if (args.parameter_file.empty()) {
throw std::invalid_argument("Missing parameter file");
}
if (args.motion_type.empty()) {
throw std::invalid_argument("Missing motion model");
2024-08-20 15:51:49 +00:00
}
return args;
}
std::unordered_map<std::string, double> read_parameter(const std::filesystem::path& infile) {
2024-08-18 11:21:27 +00:00
if (!std::filesystem::exists(infile)) {
std::cerr << "File " << infile << " does not exist" << std::endl;
exit(1);
}
2024-08-16 17:55:27 +00:00
std::ifstream instream(infile);
std::unordered_map<std::string, double> parameter;
std::string line;
std::string delim = "=";
std::string key;
std::string value;
size_t delim_pos;
while (std::getline(instream, line)) {
2024-11-10 14:52:54 +00:00
// skip comment lines starting with #, and empty lines
2024-08-20 15:51:49 +00:00
if (line[0] == '#' || line.length() == 1) continue;
2024-11-10 14:52:54 +00:00
// strip spaces from line to have always key=value
2024-08-16 17:55:27 +00:00
line.erase(std::remove(line.begin(), line.end(), ' '), line.end());
2024-11-10 14:52:54 +00:00
// split at '=' character and add to map
2024-08-16 17:55:27 +00:00
delim_pos = line.find('=');
key = line.substr(0, delim_pos);
value = line.substr(delim_pos+1);
parameter[key] = std::stod(value);
}
2024-11-10 14:52:54 +00:00
// print result
2024-08-16 17:55:27 +00:00
std::cout << "Found parameter\n";
for (const auto& [key, value]: parameter) {
std::cout << " " << key << ": " << std::to_string(value) << "\n";
}
std::cout << std::endl;
return parameter;
}
void fid_write_out(const std::string& filename, const std::vector<double>& x, const std::vector<double>& y, const double tau, const double t_evo) {
auto size = x.size();
std::ostringstream sfile;
sfile << filename << "_";
sfile << std::setprecision(6) << std::scientific;
sfile << "tau=" << tau << "_tevo=" << t_evo << ".dat";
{
std::string outfile = sfile.str();
std::ofstream fid_file(outfile, std::ios::out);
for (unsigned int i = 0; i < size; i++) {
fid_file << x[i] << "\t" << y[i] << "\n";
}
}
}
void fid_write_out(const std::string& filename, const std::vector<double>& x, const std::map<double, std::vector<double>>& y, const double tau) {
auto size = x.size();
std::ostringstream sfile;
sfile << filename << "_";
sfile << std::setprecision(6) << std::scientific;
sfile << "tau=" << tau << ".dat";
{
std::string outfile = sfile.str();
std::ofstream fid_file(outfile, std::ios::out);
fid_file << "#";
for (const auto& [t_echo_j, _] : y) {
fid_file << t_echo_j << "\t";
}
fid_file << std::endl;
for (unsigned int i = 0; i < size; i++) {
fid_file << x[i];
for (const auto& [_, fid_j] : y) {
fid_file << "\t" << fid_j[i];
}
fid_file << "\n";
}
}
}