BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
IntensityDataIOFactory.h
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file Device/Histo/IntensityDataIOFactory.h
6 //! @brief Defines class IntensityDataIOFactory.
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 
15 #ifndef BORNAGAIN_DEVICE_HISTO_INTENSITYDATAIOFACTORY_H
16 #define BORNAGAIN_DEVICE_HISTO_INTENSITYDATAIOFACTORY_H
17 
18 #include <functional>
19 #include <string>
20 
21 template <class T> class OutputData;
22 class IHistogram;
23 class SimulationResult;
24 
25 //! Provides users with possibility to read and write IntensityData from/to files
26 //! in different format. Type of the file will be deduced from file name.
27 //! *.txt - ASCII file with 2D array [nrow][ncol], layout as in numpy.
28 //! *.int - BornAgain internal ASCII format.
29 //! *.tif - 32-bits tiff file.
30 //! If file name ends with "*.gz" or "*.bz2" the file will be zipped on the fly using
31 //! appropriate algorithm.
32 
33 //! @ingroup input_output
34 
35 /*! Usage:
36 \code{.py}
37 # reading from ASCII file or g-zipped ASCII file
38 histogram = IntensityDataIOFactory.readIntensityData("filename.txt")
39 histogram = IntensityDataIOFactory.readIntensityData("filename.txt.gz")
40 
41 # writing to 32-bits tiff file or b-zipped tiff file
42 IntensityDataIOFactory.writeIntensityData(histogram, "filename.tif")
43 IntensityDataIOFactory.writeIntensityData(histogram, "filename.tif.bz2")
44 \endcode
45 */
46 
48 public:
49  //! Reads file and returns newly created OutputData object
50  static OutputData<double>* readOutputData(const std::string& file_name);
51  static OutputData<double>* readReflectometryData(const std::string& file_name);
52 
53  //! Reads file and returns newly created Histogram object
54  static IHistogram* readIntensityData(const std::string& file_name);
55 
56  //! Writes OutputData in file
57  static void writeOutputData(const OutputData<double>& data, const std::string& file_name);
58 
59  //! Writes histogram in file
60  static void writeIntensityData(const IHistogram& histogram, const std::string& file_name);
61 
62  //! Writes OutputData contained in the given SimulationResult object
63  static void writeSimulationResult(const SimulationResult& result, const std::string& file_name);
64 
65 private:
66  static OutputData<double>*
67  readOutputData(const std::string& file_name,
68  std::function<OutputData<double>*(std::istream&)> readData);
69 
70  static void writeOutputData(const std::string& file_name,
71  std::function<void(std::ostream&)> writeData);
72 };
73 
74 #endif // BORNAGAIN_DEVICE_HISTO_INTENSITYDATAIOFACTORY_H
Base class for 1D and 2D histograms holding values of double type.
Definition: IHistogram.h:27
Provides users with possibility to read and write IntensityData from/to files in different format.
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 OutputData< double > * readReflectometryData(const std::string &file_name)
static void writeSimulationResult(const SimulationResult &result, const std::string &file_name)
Writes OutputData contained in the given SimulationResult object.
Templated class to store data of type double or CumulativeValue in multi-dimensional space.
Definition: OutputData.h:32
Wrapper around OutputData<double> that also provides unit conversions.