BornAgain  1.19.79
Open-source research software to simulate and fit neutron and x-ray reflectometry and grazing-incidence small-angle scattering
ReadReflectometry Class Reference

Description

Class for reading reflectometry data from ASCII file.

Definition at line 29 of file ReadReflectometry.h.

Public Member Functions

DatafieldreadDatafield (std::istream &inStream)
 

Member Function Documentation

◆ readDatafield()

Datafield * ReadReflectometry::readDatafield ( std::istream &  inStream)

Definition at line 24 of file ReadReflectometry.cpp.

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 }
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
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.

References DataUtils::Format::parse_doubles(), and BaseUtils::String::trim().

Referenced by IOFactory::readReflectometryData().

Here is the call graph for this function:

The documentation for this class was generated from the following files: