BornAgain  1.18.0
Simulate and fit neutron and x-ray scattering at grazing incidence
IntensityDataIOFactory.cpp
Go to the documentation of this file.
1 // ************************************************************************** //
2 //
3 // BornAgain: simulate and fit scattering at grazing incidence
4 //
5 //! @file Device/Histo/IntensityDataIOFactory.cpp
6 //! @brief Implements class OutputDataIOFactory.
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 
21 #include <exception>
22 #include <fstream>
23 #include <memory>
24 
26 {
27  if (!FileSystemUtils::IsFileExists(file_name))
28  return nullptr;
29  std::unique_ptr<OutputDataReader> reader(OutputDataReadFactory::getReader(file_name));
30  if (reader)
31  return reader->getOutputData();
32  return nullptr;
33 }
34 
35 OutputData<double>* IntensityDataIOFactory::readReflectometryData(const std::string& file_name)
36 {
37  if (!FileSystemUtils::IsFileExists(file_name))
38  return nullptr;
39  std::unique_ptr<OutputDataReader> reader(
40  OutputDataReadFactory::getReflectometryReader(file_name));
41  if (reader)
42  return reader->getOutputData();
43  return nullptr;
44 }
45 
47 {
48  std::unique_ptr<OutputData<double>> data(readOutputData(file_name));
49  if (!data)
50  throw std::runtime_error("Could not read " + file_name);
51  return IHistogram::createHistogram(*data);
52 }
53 
55  const std::string& file_name)
56 {
57  auto* writer = OutputDataWriteFactory::getWriter(file_name);
58  writer->writeOutputData(data);
59  delete writer;
60 }
61 
63  const std::string& file_name)
64 {
65  std::unique_ptr<OutputData<double>> data(histogram.createOutputData());
66  writeOutputData(*data, file_name);
67 }
68 
70  const std::string& file_name)
71 {
72  auto data = result.data();
73  writeOutputData(*data, file_name);
74 }
Defines namespace FileSystemUtils.
Defines class IHistogram.
Defines class IntensityDataIOFactory.
Defines class OutputDataReadFactory.
Defines class OutputDataWriteFactory.
Defines class SimulationResult.
Base class for 1D and 2D histograms holding values of double type.
Definition: IHistogram.h:27
OutputData< double > * createOutputData(DataType dataType=DataType::INTEGRAL) const
creates new OutputData with histogram's shape and values corresponding to DataType
Definition: IHistogram.cpp:334
static OutputData< double > * readOutputData(const std::string &file_name)
Reads file and returns newly created OutputData object.
static void writeOutputData(const OutputData< double > &data, const std::string &file_name)
Writes OutputData in file.
static IHistogram * readIntensityData(const std::string &file_name)
Reads file and returns newly created Histogram object.
static void writeIntensityData(const IHistogram &histogram, const std::string &file_name)
Writes histogram in file.
static void writeSimulationResult(const SimulationResult &result, const std::string &file_name)
Writes OutputData contained in the given SimulationResult object.
Wrapper around OutputData<double> that also provides unit conversions.
bool IsFileExists(const std::string &str)
Returns true if file with given name exists on disk.