26 bool isDoubleStartChar(
char c)
28 return isdigit(c) || c ==
'-' || c ==
'+';
36 std::vector<std::vector<double>> data;
39 while (std::getline(input_stream, line)) {
41 if (line.empty() || !isDoubleStartChar(line[0]))
46 data.push_back(dataInRow);
53 size_t nrows = data.size();
56 ncols = data[0].size();
59 throw std::runtime_error(
"ReadNumpyTXTStrategy::readDatafield() -> Error. "
62 for (
size_t row = 0; row < nrows; row++) {
63 if (data[row].size() != ncols)
64 throw std::runtime_error(
"ReadNumpyTXTStrategy::readDatafield() -> Error. "
65 "Number of elements is different from row to row.");
72 const size_t size = data.size();
73 std::vector<double> vector1d(size);
74 for (
size_t i = 0; i < size; ++i)
75 vector1d[i] = data[i][0];
84 output_stream <<
"# BornAgain Intensity Data" << std::endl;
85 output_stream <<
"# Simple array suitable for numpy, matlab etc." << std::endl;
87 const size_t dim = data.
rank();
96 throw std::runtime_error(
"Error in DatafieldWriteNumpyTXTStrategy::writeDatafield: data "
97 "of unsupported dimensions");
103 output_stream <<
"# coordinates intensities" << std::endl;
104 output_stream.imbue(std::locale::classic());
107 const std::vector<double> axis_values = data.
axis(0).
binCenters();
110 for (
size_t i = 0, nrows = axis_values.size(); i < nrows; ++i)
116 const size_t nrows = data.
axis(1).
size();
117 const size_t ncols = data.
axis(0).
size();
119 output_stream <<
"# [nrows=" << nrows <<
", ncols=" << ncols <<
"]" << std::endl;
122 output_stream.imbue(std::locale::classic());
125 for (
size_t i = 0; i < nrows; i++) {
126 for (
size_t j = 0; j < ncols; j++) {
127 double z_value = dataArray[i][j];
130 output_stream << std::endl;
136 return (std::fpclassify(value) == FP_SUBNORMAL) ? 0.0 : value;
Defines various functions to interact from numpy on Python side.
Defines a few helper functions.
Defines structs Bin1D, Bin1DCVector.
Defines ReadWriteNumpyTXT.
Stores radiation power per bin.
const IAxis & axis(size_t k) const
virtual std::vector< double > binCenters() const
virtual size_t size() const =0
Returns the number of bins.
static double ignoreDenormalized(double value)
static void write2DRepresentation(const Datafield &data, std::ostream &output_stream)
void writeDatafield(const Datafield &data, std::ostream &output_stream)
Datafield * readDatafield(std::istream &input_stream)
static void write1DRepresentation(const Datafield &data, std::ostream &output_stream)
std::string scientific(T value, int n=10)
Returns scientific string representing given value of any numeric type.
std::string trim(const std::string &str, const std::string &whitespace=" \t")
Cuts any of the chars given in whitespace from start and end of str.
std::vector< std::vector< double > > createVector2D(const Datafield &data)
Creates 2D vector from Datafield.
std::unique_ptr< Datafield > createPField1D(const std::vector< double > &vec)
std::unique_ptr< Datafield > createPField2D(const std::vector< std::vector< double >> &vec)