BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
OutputDataReadReflectometry.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file Device/InputOutput/OutputDataReadReflectometry.cpp
6 //! @brief Implements class OutputDataReadWriteReflectometry.
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2018
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
17 #include "Base/Utils/StringUtils.h"
18 #include "Device/Data/OutputData.h"
20 #include <map>
21 
22 // #bamigration +++ this works only if separator is space or tab; it does not
23 // work e.g. with comma or semicolon
25 {
27  std::string line;
28  std::vector<std::vector<double>> vecVec;
29  std::map<double, double> QvsR;
30  std::map<double, double> QvsDR;
31  std::map<double, double> QvsDQ;
32 
33  // Read numbers from file:
34  while (std::getline(fin, line)) {
35  line = StringUtils::trim(line);
36  try {
37  // #bamigration +++ this works only if separator is space or tab; it does not
38  // work e.g. with comma or semicolon
39  std::vector<double> rowVec = DataFormatUtils::parse_doubles(line);
40  vecVec.push_back(rowVec);
41  } catch (...) { // #bamigration +++ This eats useful errors away...
42  continue;
43  }
44  }
45 
46  // validate - There is at least one row and at least two columns
47  size_t nrows = vecVec.size();
48  if (nrows < 1)
49  throw std::runtime_error("No numerical values found");
50  size_t ncols = vecVec[0].size();
51  if (ncols < 2)
52  throw std::runtime_error("Minimum 2 columns required");
53 
54  // Assign Q vs R, dR, dQ:
55  for (size_t row = 0; row < nrows; row++) {
56  if (vecVec[row].size() != ncols)
57  throw std::runtime_error("The number of columns varies among the rows");
58  double Q = vecVec[row][0];
59  switch (ncols) {
60  case 1:
61  break;
62  case 2:
63  QvsR[Q] = vecVec[row][1];
64  QvsDR[Q] = 0;
65  QvsDQ[Q] = 0;
66  break;
67  case 3:
68  QvsR[Q] = vecVec[row][1];
69  QvsDR[Q] = vecVec[row][2];
70  QvsDQ[Q] = 0;
71  break;
72  default:
73  QvsR[Q] = vecVec[row][1];
74  QvsDR[Q] = vecVec[row][2];
75  QvsDQ[Q] = vecVec[row][3];
76  break;
77  }
78  }
79 
80  std::vector<double> qVec;
81  std::vector<double> rVec;
82  for (auto it = QvsR.begin(); it != QvsR.end(); ++it) {
83  if (it->second <= 0)
84  continue;
85  qVec.push_back(it->first);
86  rVec.push_back(it->second);
87  }
88 
89  oData->addAxis(PointwiseAxis("qVector", qVec));
90  oData->setRawDataVector(rVec);
91  return oData;
92 }
Defines a few helper functions.
Defines class OutputDataIOFactory.
Defines OutputDataReadReflectometry.
Defines and implements templated class OutputData.
Defines class PointwiseAxis.
OutputData< double > * readOutputData(std::istream &input_stream)
void addAxis(const IAxis &new_axis)
Definition: OutputData.h:295
void setRawDataVector(const std::vector< T > &data_vector)
Sets new values to raw data vector.
Definition: OutputData.h:556
Axis containing arbitrary (non-equidistant) coordinate values.
Definition: PointwiseAxis.h:37
std::vector< double > parse_doubles(const std::string &str)
Parse double values from string to vector of double.
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.
Definition: StringUtils.cpp:98