BornAgain  1.18.0
Simulate and fit neutron and x-ray scattering at grazing incidence
OutputDataWriteStrategy.cpp
Go to the documentation of this file.
1 // ************************************************************************** //
2 //
3 // BornAgain: simulate and fit scattering at grazing incidence
4 //
5 //! @file Device/InputOutput/OutputDataWriteStrategy.cpp
6 //! @brief Implements class OutputDataWriteStrategy.
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 
18 #include <cmath>
19 #include <iomanip>
20 
21 namespace
22 {
23 const int precision{12};
24 
25 double IgnoreDenormalized(double value)
26 {
27  if (std::fpclassify(value) == FP_SUBNORMAL)
28  return 0.0;
29  return value;
30 }
31 
32 void Write2DRepresentation(const OutputData<double>& data, std::ostream& output_stream)
33 {
34  const size_t nrows = data.getAxis(1).size();
35  const size_t ncols = data.getAxis(0).size();
36 
37  output_stream << "# [nrows=" << nrows << ", ncols=" << ncols << "]" << std::endl;
38 
39  std::vector<std::vector<double>> dataArray = ArrayUtils::createVector2D(data);
40  output_stream.imbue(std::locale::classic());
41  output_stream << std::scientific << std::setprecision(precision);
42 
43  for (size_t i = 0; i < nrows; i++) {
44  for (size_t j = 0; j < ncols; j++) {
45  double z_value = dataArray[i][j];
46  output_stream << IgnoreDenormalized(z_value) << " ";
47  }
48  output_stream << std::endl;
49  }
50 }
51 
52 void WriteOutputDataDoubles(const OutputData<double>& data, std::ostream& output_stream,
53  size_t n_columns)
54 {
55 
57  output_stream.imbue(std::locale::classic());
58  output_stream << std::scientific << std::setprecision(precision);
59  size_t ncol(0);
60  while (it != data.end()) {
61  ncol++;
62  double z_value = *it++;
63  output_stream << IgnoreDenormalized(z_value) << " ";
64  if (ncol == n_columns) {
65  output_stream << std::endl;
66  ncol = 0;
67  }
68  }
69 }
70 
71 void Write1DRepresentation(const OutputData<double>& data, std::ostream& output_stream)
72 {
73  output_stream << "# coordinates intensities" << std::endl;
74  output_stream.imbue(std::locale::classic());
75  output_stream << std::scientific << std::setprecision(precision);
76 
77  const std::vector<double> axis_values = data.getAxis(0).getBinCenters();
78 
79  // printing coordinate and associated intensity
80  for (size_t i = 0, nrows = axis_values.size(); i < nrows; ++i)
81  output_stream << axis_values[i] << " " << IgnoreDenormalized(data[i]) << std::endl;
82 }
83 } // namespace
84 
85 // ----------------------------------------------------------------------------
86 // class OutputDataWriteINTStrategy
87 // ----------------------------------------------------------------------------
88 
90  std::ostream& output_stream)
91 {
92  output_stream << "# BornAgain Intensity Data\n\n";
93 
94  for (size_t i = 0; i < data.getRank(); ++i) {
95  std::string axis_name = std::string("axis") + std::to_string(i);
96  std::unique_ptr<IAxis> P_axis(data.getAxis(i).clone());
97  P_axis->setName(axis_name);
98  output_stream << std::endl;
99  output_stream << "# axis-" << i << "\n";
100  output_stream << (*P_axis) << "\n";
101  }
102  size_t n_columns = data.getAxis(data.getRank() - 1).size();
103 
104  output_stream << "\n# data\n";
105  WriteOutputDataDoubles(data, output_stream, n_columns);
106  output_stream << std::endl;
107 }
108 
109 // ----------------------------------------------------------------------------
110 // class OutputDataWriteNumpyTXTStrategy
111 // ----------------------------------------------------------------------------
112 
114  std::ostream& output_stream)
115 {
116  output_stream << "# BornAgain Intensity Data" << std::endl;
117  output_stream << "# Simple array suitable for numpy, matlab etc." << std::endl;
118 
119  const size_t dim = data.getRank();
120  switch (dim) {
121  case 1:
122  Write1DRepresentation(data, output_stream);
123  break;
124  case 2:
125  Write2DRepresentation(data, output_stream);
126  break;
127  default:
128  throw std::runtime_error("Error in OutputDataWriteNumpyTXTStrategy::writeOutputData: data "
129  "of unsupported dimensions");
130  }
131 }
132 
133 // ----------------------------------------------------------------------------
134 // class OutputDataWriteTiffStrategy
135 // ----------------------------------------------------------------------------
136 
137 #ifdef BORNAGAIN_TIFF_SUPPORT
138 
139 OutputDataWriteTiffStrategy::OutputDataWriteTiffStrategy() : m_d(new TiffHandler) {}
140 
141 OutputDataWriteTiffStrategy::~OutputDataWriteTiffStrategy()
142 {
143  delete m_d;
144 }
145 
146 void OutputDataWriteTiffStrategy::writeOutputData(const OutputData<double>& data,
147  std::ostream& output_stream)
148 {
149  m_d->write(data, output_stream);
150 }
151 
152 #endif
Defines various functions to interact from numpy on Python side.
Defines classes IOutputDataWriteStrategy and OutputDataWriteStreamIMA.
Defines class TiffHandler.
virtual IAxis * clone() const =0
clone function
virtual std::vector< double > getBinCenters() const
Definition: IAxis.cpp:23
virtual size_t size() const =0
retrieve the number of bins
virtual void writeOutputData(const OutputData< double > &data, std::ostream &output_stream)
virtual void writeOutputData(const OutputData< double > &data, std::ostream &output_stream)
iterator end()
Returns read/write iterator that points to the one past last element.
Definition: OutputData.h:96
size_t getRank() const
Returns number of dimensions.
Definition: OutputData.h:59
const IAxis & getAxis(size_t serial_number) const
returns axis with given serial number
Definition: OutputData.h:314
iterator begin()
Returns read/write iterator that points to the first element.
Definition: OutputData.h:344
decltype(auto) createVector2D(const T &data)
Creates 2D vector from OutputData.
std::string scientific(const T value, int n=10)
Returns scientific string representing given value of any numeric type.
Definition: StringUtils.h:54
void Write2DRepresentation(const OutputData< double > &data, std::ostream &output_stream)
void Write1DRepresentation(const OutputData< double > &data, std::ostream &output_stream)
void WriteOutputDataDoubles(const OutputData< double > &data, std::ostream &output_stream, size_t n_columns)