BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
OutputDataReadReflectometry Class Reference

Class for reading reflectometry data from ASCII file. More...

Public Member Functions

OutputData< double > * readOutputData (std::istream &input_stream)
 

Detailed Description

Class for reading reflectometry data from ASCII file.

Definition at line 30 of file OutputDataReadReflectometry.h.

Member Function Documentation

◆ readOutputData()

OutputData< double > * OutputDataReadReflectometry::readOutputData ( std::istream &  input_stream)

Definition at line 24 of file OutputDataReadReflectometry.cpp.

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

References OutputData< T >::addAxis(), DataFormatUtils::parse_doubles(), OutputData< T >::setRawDataVector(), and StringUtils::trim().

Referenced by AutomaticDataLoader1D::processContents(), and IntensityDataIOFactory::readReflectometryData().

Here is the call graph for this function:

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