BornAgain  1.19.79
Open-source research software to simulate and fit neutron and x-ray reflectometry and grazing-incidence small-angle scattering
ReadReflectometry.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file Device/IO/ReadReflectometry.cpp
6 //! @brief Implements class ReadWriteReflectometry.
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/Util/StringUtils.h"
18 #include "Device/Data/Datafield.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 {
26  std::string line;
27  std::vector<std::vector<double>> vecVec;
28  std::map<double, double> QvsR;
29  std::map<double, double> QvsDR;
30  std::map<double, double> QvsDQ;
31 
32  // Read numbers from file:
33  while (std::getline(inStream, line)) {
34  line = BaseUtils::String::trim(line);
35  try {
36  // #bamigration +++ this works only if separator is space or tab; it does not
37  // work e.g. with comma or semicolon
38  std::vector<double> rowVec = DataUtils::Format::parse_doubles(line);
39  vecVec.push_back(rowVec);
40  } catch (...) { // #bamigration +++ This eats useful errors away...
41  continue;
42  }
43  }
44 
45  // validate - There is at least one row and at least two columns
46  size_t nrows = vecVec.size();
47  if (nrows < 1)
48  throw std::runtime_error("No numerical values found");
49  size_t ncols = vecVec[0].size();
50  if (ncols < 2)
51  throw std::runtime_error("Minimum 2 columns required");
52 
53  // Assign Q vs R, dR, dQ:
54  for (size_t row = 0; row < nrows; row++) {
55  if (vecVec[row].size() != ncols)
56  throw std::runtime_error("The number of columns varies among the rows");
57  double Q = vecVec[row][0];
58  switch (ncols) {
59  case 1:
60  break;
61  case 2:
62  QvsR[Q] = vecVec[row][1];
63  QvsDR[Q] = 0;
64  QvsDQ[Q] = 0;
65  break;
66  case 3:
67  QvsR[Q] = vecVec[row][1];
68  QvsDR[Q] = vecVec[row][2];
69  QvsDQ[Q] = 0;
70  break;
71  default:
72  QvsR[Q] = vecVec[row][1];
73  QvsDR[Q] = vecVec[row][2];
74  QvsDQ[Q] = vecVec[row][3];
75  break;
76  }
77  }
78 
79  std::vector<double> qVec;
80  std::vector<double> rVec;
81  for (auto it = QvsR.begin(); it != QvsR.end(); ++it) {
82  if (it->second <= 0)
83  continue;
84  qVec.push_back(it->first);
85  rVec.push_back(it->second);
86  }
87 
88  return new Datafield{{new PointwiseAxis("qVector", qVec)}, rVec};
89 }
Defines a few helper functions.
Defines class DatafieldIOFactory.
Defines and implements templated class Datafield.
Defines class PointwiseAxis.
Defines ReadReflectometry.
Stores radiation power per bin.
Definition: Datafield.h:30
Axis containing arbitrary (non-equidistant) coordinate values. Lower boundary of the first bin and up...
Definition: PointwiseAxis.h:33
Datafield * readDatafield(std::istream &inStream)
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:87
std::vector< double > parse_doubles(const std::string &str)
Parse double values from string to vector of double.