BornAgain  1.19.79
Open-source research software to simulate and fit neutron and x-ray reflectometry and grazing-incidence small-angle scattering
IOFactory.h
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file Device/IO/IOFactory.h
6 //! @brief Defines class IOFactory.
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_IO_IOFACTORY_H
16 #define BORNAGAIN_DEVICE_IO_IOFACTORY_H
17 
18 #include <functional>
19 #include <string>
20 
21 class Datafield;
22 class SimulationResult;
23 
24 //! Provides users with possibility to read and write IntensityData from/to files
25 //! in different format. Type of the file will be deduced from file name.
26 //! *.txt - ASCII file with 2D array [nrow][ncol], layout as in numpy.
27 //! *.int - BornAgain internal ASCII format.
28 //! *.tif - 32-bits tiff file.
29 //! If file name ends with "*.gz" or "*.bz2" the file will be zipped on the fly using
30 //! appropriate algorithm.
31 
32 //! @ingroup input_output
33 
34 /*! Usage:
35 \code{.py}
36 # reading from ASCII file or g-zipped ASCII file
37 histogram = IOFactory.readDatafield("filename.txt")
38 histogram = IOFactory.readDatafield("filename.txt.gz")
39 
40 # writing to 32-bits tiff file or b-zipped tiff file
41 IOFactory.writeIntensityData(histogram, "filename.tif")
42 IOFactory.writeIntensityData(histogram, "filename.tif.bz2")
43 \endcode
44 */
45 
46 class IOFactory {
47 public:
49 
50  //! Reads file and returns newly created Datafield object. If selector is automatic, then the
51  //! file extension will be used to determine which file type to load. If selector is not
52  //! automatic, then this selector will define which type of file to load (no matter which file
53  //! extension or content).
54  //! May throw, but will never return nullptr.
55  static Datafield* readDatafield(const std::string& file_name,
56  LoaderSelector selector = automatic);
57 
58  static Datafield* readReflectometryData(const std::string& file_name);
59 
60  //! Writes Datafield in file
61  static void writeDatafield(const Datafield& data, const std::string& file_name);
62 
63  //! Writes Datafield contained in the given SimulationResult object
64  static void writeSimulationResult(const SimulationResult& result, const std::string& file_name);
65 
66 private:
67  static Datafield* readDatafield(const std::string& file_name,
68  std::function<Datafield*(std::istream&)> readData);
69 
70  static void writeDatafield(const std::string& file_name,
71  std::function<void(std::ostream&)> writeData);
72 
73  static bool fileTypeMatchesLoaderSelector(const std::string& fileName, LoaderSelector selector);
74 };
75 
76 namespace IOUtil {
77 
78 //! Returns true if data in both files agree
79 bool filesAgree(const std::string& datFileName, const std::string& refFileName, double tol);
80 
81 } // namespace IOUtil
82 
83 #endif // BORNAGAIN_DEVICE_IO_IOFACTORY_H
Stores radiation power per bin.
Definition: Datafield.h:30
Provides users with possibility to read and write IntensityData from/to files in different format....
Definition: IOFactory.h:46
static bool fileTypeMatchesLoaderSelector(const std::string &fileName, LoaderSelector selector)
Definition: IOFactory.cpp:130
static void writeSimulationResult(const SimulationResult &result, const std::string &file_name)
Writes Datafield contained in the given SimulationResult object.
Definition: IOFactory.cpp:146
static void writeDatafield(const Datafield &data, const std::string &file_name)
Writes Datafield in file.
Definition: IOFactory.cpp:81
static Datafield * readReflectometryData(const std::string &file_name)
Definition: IOFactory.cpp:75
LoaderSelector
Definition: IOFactory.h:48
@ bornagain
Definition: IOFactory.h:48
@ automatic
Definition: IOFactory.h:48
static Datafield * readDatafield(const std::string &file_name, LoaderSelector selector=automatic)
Reads file and returns newly created Datafield object. If selector is automatic, then the file extens...
Definition: IOFactory.cpp:41
Wrapper around Datafield that also provides unit conversions.
bool filesAgree(const std::string &datFileName, const std::string &refFileName, double tol)
Returns true if data in both files agree.
Definition: IOFactory.cpp:189