Skip to content
Snippets Groups Projects
Commit 43b14838 authored by Wuttke, Joachim's avatar Wuttke, Joachim
Browse files

store original axis name instead of axis0,1.

parent d5ec9ac7
No related branches found
No related tags found
No related merge requests found
...@@ -17,7 +17,6 @@ ...@@ -17,7 +17,6 @@
#include <boost/algorithm/string.hpp> #include <boost/algorithm/string.hpp>
#include <cerrno> #include <cerrno>
#include <charconv> #include <charconv>
#include <regex>
#include <string> #include <string>
//! Returns string right-padded with blanks. //! Returns string right-padded with blanks.
......
...@@ -265,9 +265,10 @@ bool Util::RW::dataMatchesFile(const Datafield& data, const std::string& refFile ...@@ -265,9 +265,10 @@ bool Util::RW::dataMatchesFile(const Datafield& data, const std::string& refFile
std::unique_ptr<Datafield> refDat; std::unique_ptr<Datafield> refDat;
try { try {
refDat.reset(IO::readData2D(refFileName)); refDat.reset(IO::readData2D(refFileName));
} catch (...) { } catch (const std::exception& ex) {
std::cerr << "File comparison: Could not read reference data from file " << refFileName std::cerr << "File comparison: Could not read reference data from file " << refFileName
<< std::endl; << std::endl;
std::cerr << "Error message: " << ex.what() << std::endl;
return false; return false;
} }
ASSERT(refDat); ASSERT(refDat);
......
...@@ -21,18 +21,10 @@ ...@@ -21,18 +21,10 @@
#include <functional> #include <functional>
#include <iostream> #include <iostream>
#include <iterator> #include <iterator>
#include <regex>
namespace { namespace {
std::istringstream getAxisStringRepresentation(std::istream& input_stream)
{
std::string line;
std::getline(input_stream, line);
const std::vector<std::string> to_replace = {",", "\"", "(", ")", "[", "]"};
Base::String::replaceItemsFromString(line, to_replace, " ");
return std::istringstream(line);
}
void readLineOfDoubles(std::vector<double>& buffer, std::istringstream& iss) void readLineOfDoubles(std::vector<double>& buffer, std::istringstream& iss)
{ {
iss.imbue(std::locale::classic()); iss.imbue(std::locale::classic());
...@@ -40,42 +32,59 @@ void readLineOfDoubles(std::vector<double>& buffer, std::istringstream& iss) ...@@ -40,42 +32,59 @@ void readLineOfDoubles(std::vector<double>& buffer, std::istringstream& iss)
back_inserter(buffer)); back_inserter(buffer));
} }
std::vector<double> parse_x_list(std::string text, const std::string& type)
{
if (text.substr(0, 1) == ",") {
std::cerr << "Warning: file format from BornAgain <= 20 is obsolete" << std::endl;
size_t i = text.find_first_not_of(" ", 1);
text.erase(0, i);
}
if (text.substr(0, 1) == "[" && text.substr(text.size() - 1, 1) == "]")
text = text.substr(1, text.size() - 2);
std::vector<std::string> arr = Base::String::split(text, ",");
std::vector<double> result;
for (std::string e : arr) {
e = Base::String::trim(e);
if (e.empty())
continue;
double x;
if (!(Base::String::to_double(e, &x)))
throw std::runtime_error("Reading " + type + ": cannot read entry '" + e + "'");
result.push_back(x);
}
if (!result.size())
throw std::runtime_error("Reading " + type + ": found empty list");
return result;
}
} // namespace } // namespace
//! Creates axis of certain type from input stream //! Creates axis of certain type from input stream
Scale* Util::Parse::parseScale(std::istream& input_stream) Scale* Util::Parse::parseScale(std::istream& input_stream)
{ {
auto iss = ::getAxisStringRepresentation(input_stream); std::string line;
std::string type; std::getline(input_stream, line);
if (!(iss >> type)) std::smatch matched;
throw std::runtime_error("Cannot read axis type from input"); std::regex_match(line, matched, std::regex("(\\w+)\\(\"(.*?)\",\\s*(.+?)\\)"));
if (matched.size() != 4)
throw std::runtime_error("Cannot read axis from input");
const std::string type = matched[1];
const std::string name = matched[2];
const std::string body = matched[3];
if (type == "EquiDivision" || type == "FixedBinAxis" /* for compatibility with pre-21 */) { if (type == "EquiDivision" || type == "FixedBinAxis" /* for compatibility with pre-21 */) {
std::string name; std::vector<std::string> arr = Base::String::split(body, ",");
size_t nbins; int nbins;
if (!(iss >> name >> nbins)) double xmi, xma;
throw std::runtime_error("Reading EquiDivision: cannot read name or size"); if (!(Base::String::to_int(arr[0], &nbins) && Base::String::to_double(arr[1], &xmi)
std::vector<double> boundaries; && Base::String::to_double(arr[2], &xma)))
::readLineOfDoubles(boundaries, iss); throw std::runtime_error("Reading EquiDivision: cannot read parameters");
if (boundaries.size() != 2) return newEquiDivision(name, nbins, xmi, xma);
throw std::runtime_error("Reading EquiDivision: cannot read start or stop");
return newEquiDivision(name, nbins, boundaries[0], boundaries[1]);
} else if (type == "ListScan" || type == "DiscreteAxis" /* for compatibility with pre-21 */) { } else if (type == "ListScan" || type == "DiscreteAxis" /* for compatibility with pre-21 */) {
std::string name; return newListScan(name, parse_x_list(body, type));
if (!(iss >> name))
throw std::runtime_error("Reading ListScan: cannot read name");
std::vector<double> coordinates;
::readLineOfDoubles(coordinates, iss);
return newListScan(name, coordinates);
} else if (type == "Scale") { } else if (type == "Scale") {
std::string name; return newGenericScale(name, parse_x_list(body, type));
if (!(iss >> name))
throw std::runtime_error("Reading Scale: cannot read name");
std::vector<double> coordinates;
::readLineOfDoubles(coordinates, iss);
return newGenericScale(name, coordinates);
} else } else
throw std::runtime_error("Unknown axis type '" + type + "'"); throw std::runtime_error("Unknown axis type '" + type + "'");
......
...@@ -60,12 +60,10 @@ void Util::RW::writeBAInt(const Datafield& data, std::ostream& output_stream) ...@@ -60,12 +60,10 @@ void Util::RW::writeBAInt(const Datafield& data, std::ostream& output_stream)
output_stream << "# BornAgain Intensity Data\n\n"; output_stream << "# BornAgain Intensity Data\n\n";
for (size_t i = 0; i < data.rank(); ++i) { for (size_t i = 0; i < data.rank(); ++i) {
std::string axis_name = std::string("axis") + std::to_string(i); const Scale& axis = data.axis(i);
std::unique_ptr<Scale> axis(data.axis(i).clone());
axis->setAxisName(axis_name);
output_stream << std::endl; output_stream << std::endl;
output_stream << "# axis-" << i << "\n"; output_stream << "# axis-" << i << "\n";
output_stream << (*axis) << "\n"; output_stream << axis << "\n";
} }
size_t n_columns = data.axis(data.rank() - 1).size(); size_t n_columns = data.axis(data.rank() - 1).size();
......
No preview for this file type
No preview for this file type
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment