BornAgain  1.19.79
Open-source research software to simulate and fit neutron and x-ray reflectometry and grazing-incidence small-angle scattering
IOFactory Class Reference

Description

Provides users with possibility to read and write IntensityData from/to files in different format. Type of the file will be deduced from file name. *.txt - ASCII file with 2D array [nrow][ncol], layout as in numpy. *.int - BornAgain internal ASCII format. *.tif - 32-bits tiff file. If file name ends with "*.gz" or "*.bz2" the file will be zipped on the fly using appropriate algorithm.

Usage:

# reading from ASCII file or g-zipped ASCII file
histogram = IOFactory.readDatafield("filename.txt")
histogram = IOFactory.readDatafield("filename.txt.gz")
# writing to 32-bits tiff file or b-zipped tiff file
IOFactory.writeIntensityData(histogram, "filename.tif")
IOFactory.writeIntensityData(histogram, "filename.tif.bz2")
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

Definition at line 46 of file IOFactory.h.

Public Types

enum  LoaderSelector { automatic , bornagain , tiff , nicos }
 

Static Public Member Functions

static DatafieldreadDatafield (const std::string &file_name, LoaderSelector selector=automatic)
 Reads file and returns newly created Datafield object. If selector is automatic, then the file extension will be used to determine which file type to load. If selector is not automatic, then this selector will define which type of file to load (no matter which file extension or content). May throw, but will never return nullptr. More...
 
static DatafieldreadReflectometryData (const std::string &file_name)
 
static void writeDatafield (const Datafield &data, const std::string &file_name)
 Writes Datafield in file. More...
 
static void writeSimulationResult (const SimulationResult &result, const std::string &file_name)
 Writes Datafield contained in the given SimulationResult object. More...
 

Static Private Member Functions

static bool fileTypeMatchesLoaderSelector (const std::string &fileName, LoaderSelector selector)
 
static DatafieldreadDatafield (const std::string &file_name, std::function< Datafield *(std::istream &)> readData)
 
static void writeDatafield (const std::string &file_name, std::function< void(std::ostream &)> writeData)
 

Member Enumeration Documentation

◆ LoaderSelector

Enumerator
automatic 
bornagain 
tiff 
nicos 

Definition at line 48 of file IOFactory.h.

@ bornagain
Definition: IOFactory.h:48
@ automatic
Definition: IOFactory.h:48

Member Function Documentation

◆ fileTypeMatchesLoaderSelector()

bool IOFactory::fileTypeMatchesLoaderSelector ( const std::string &  fileName,
LoaderSelector  selector 
)
staticprivate

Definition at line 130 of file IOFactory.cpp.

131 {
132  switch (selector) {
133  case bornagain:
134  return DataUtils::Format::isIntFile(fileName);
135  case nicos:
136  return DataUtils::Format::isNicosFile(fileName);
137  case tiff:
138  return DataUtils::Format::isTiffFile(fileName);
139  case automatic:
140  return false;
141  }
142 
143  return false;
144 }
bool isIntFile(const std::string &file_name)
Returns true if file name corresponds to BornAgain native format (compressed or not)
bool isTiffFile(const std::string &file_name)
Returns true if file name corresponds to tiff file (can be also compressed)
bool isNicosFile(const std::string &file_name)
Returns true if file name corresponds to Nicos format (compressed or not)

References automatic, bornagain, DataUtils::Format::isIntFile(), DataUtils::Format::isNicosFile(), DataUtils::Format::isTiffFile(), nicos, and tiff.

Referenced by readDatafield().

Here is the call graph for this function:

◆ readDatafield() [1/2]

Datafield * IOFactory::readDatafield ( const std::string &  file_name,
LoaderSelector  selector = automatic 
)
static

Reads file and returns newly created Datafield object. If selector is automatic, then the file extension will be used to determine which file type to load. If selector is not automatic, then this selector will define which type of file to load (no matter which file extension or content). May throw, but will never return nullptr.

Definition at line 41 of file IOFactory.cpp.

42 {
43  const auto readAs = [=](LoaderSelector testForSelector) {
44  return (selector == testForSelector)
45  || (selector == automatic
46  && fileTypeMatchesLoaderSelector(file_name, testForSelector));
47  };
48 
49  Datafield* result = nullptr;
50 
51  if (readAs(bornagain))
52  result = readDatafield(file_name,
53  [](std::istream& s) { return ReadWriteINT().readDatafield(s); });
54 
55  else if (readAs(nicos))
56  result = readDatafield(file_name, [](std::istream& s) { return IO::readNicosData(s); });
57 
58 #ifdef BA_TIFF_SUPPORT
59  else if (readAs(tiff))
60  result = readDatafield(file_name,
61  [](std::istream& s) { return ReadWriteTiff().readDatafield(s); });
62 #endif
63 
64  else
65  // Try to read ASCII by default. Binary maps to ASCII.
66  // If the file is not actually a matrix of numbers,
67  // the error will be thrown during the reading.
68  result = readDatafield(
69  file_name, [](std::istream& s) { return ReadWriteNumpyTXT().readDatafield(s); });
70 
71  ASSERT(result);
72  return result;
73 }
#define ASSERT(condition)
Definition: Assert.h:45
Stores radiation power per bin.
Definition: Datafield.h:30
static bool fileTypeMatchesLoaderSelector(const std::string &fileName, LoaderSelector selector)
Definition: IOFactory.cpp:130
LoaderSelector
Definition: IOFactory.h:48
Class for reading and writing BornAgain native IntensityData from ASCII file.
Definition: ReadWriteINT.h:29
Datafield * readDatafield(std::istream &input_stream)
Class for reading and writing Datafield from simple ASCII file with the layout as in numpy....
Datafield * readDatafield(std::istream &input_stream)
Datafield * readNicosData(std::istream &input_stream)

References ASSERT, automatic, bornagain, fileTypeMatchesLoaderSelector(), nicos, ReadWriteINT::readDatafield(), ReadWriteNumpyTXT::readDatafield(), IO::readNicosData(), and tiff.

Referenced by IOUtil::filesAgree(), and readReflectometryData().

Here is the call graph for this function:

◆ readDatafield() [2/2]

Datafield * IOFactory::readDatafield ( const std::string &  file_name,
std::function< Datafield *(std::istream &)>  readData 
)
staticprivate

Definition at line 152 of file IOFactory.cpp.

154 {
155  if (!BaseUtils::Filesystem::IsFileExists(file_name))
156  throw std::runtime_error("File does not exist: " + file_name);
157 
158  using namespace DataUtils::Format;
159  std::ifstream input_stream;
160  std::ios_base::openmode openmode = std::ios::in;
161  if (isTiffFile(file_name) || isCompressed(file_name))
162  openmode = std::ios::in | std::ios_base::binary;
163 
164 #ifdef _WIN32
165  input_stream.open(BaseUtils::Filesystem::convert_utf8_to_utf16(file_name), openmode);
166 #else
167  input_stream.open(file_name, openmode);
168 #endif
169 
170  if (!input_stream.is_open())
171  throw std::runtime_error("Cannot open file for reading: " + file_name);
172  if (!input_stream.good())
173  throw std::runtime_error("File is not good, probably it is a directory:" + file_name);
174 
175  boost::iostreams::filtering_streambuf<boost::iostreams::input> input_filtered;
176  if (DataUtils::Format::isGZipped(file_name))
177  input_filtered.push(boost::iostreams::gzip_decompressor());
178  else if (DataUtils::Format::isBZipped(file_name))
179  input_filtered.push(boost::iostreams::bzip2_decompressor());
180  input_filtered.push(input_stream);
181  // we use stringstream since it provides random access which is important for tiff files
182  std::stringstream str;
183  boost::iostreams::copy(input_filtered, str);
184 
185  return readData(str);
186 }
std::wstring convert_utf8_to_utf16(const std::string &str)
Converts utf8 string represented by std::string to utf16 string represented by std::wstring.
bool IsFileExists(const std::string &path)
Returns true if file with given name exists on disk.
Utility functions for data input and output.
bool isBZipped(const std::string &name)
Returns true if name contains *.bz2 extension.
bool isGZipped(const std::string &name)
Returns true if name contains *.gz extension.
bool isCompressed(const std::string &name)
Returns true if name contains *.gz extension.

References BaseUtils::Filesystem::convert_utf8_to_utf16(), DataUtils::Format::isBZipped(), DataUtils::Format::isCompressed(), BaseUtils::Filesystem::IsFileExists(), DataUtils::Format::isGZipped(), and DataUtils::Format::isTiffFile().

Here is the call graph for this function:

◆ readReflectometryData()

Datafield * IOFactory::readReflectometryData ( const std::string &  file_name)
static

Definition at line 75 of file IOFactory.cpp.

76 {
77  return readDatafield(file_name,
78  [](std::istream& s) { return ReadReflectometry().readDatafield(s); });
79 }
Class for reading reflectometry data from ASCII file.
Datafield * readDatafield(std::istream &inStream)

References readDatafield(), and ReadReflectometry::readDatafield().

Here is the call graph for this function:

◆ writeDatafield() [1/2]

void IOFactory::writeDatafield ( const Datafield data,
const std::string &  file_name 
)
static

Writes Datafield in file.

Definition at line 81 of file IOFactory.cpp.

82 {
83  if (DataUtils::Format::isIntFile(file_name))
84  writeDatafield(file_name, [&](std::ostream& s) { ReadWriteINT().writeDatafield(data, s); });
85 #ifdef BA_TIFF_SUPPORT
86  else if (DataUtils::Format::isTiffFile(file_name))
87  writeDatafield(file_name,
88  [&](std::ostream& s) { ReadWriteTiff().writeDatafield(data, s); });
89 #endif
90  else
91  writeDatafield(file_name,
92  [&](std::ostream& s) { ReadWriteNumpyTXT().writeDatafield(data, s); });
93 }
static void writeDatafield(const Datafield &data, const std::string &file_name)
Writes Datafield in file.
Definition: IOFactory.cpp:81
void writeDatafield(const Datafield &data, std::ostream &output_stream)
void writeDatafield(const Datafield &data, std::ostream &output_stream)

References DataUtils::Format::isIntFile(), DataUtils::Format::isTiffFile(), ReadWriteINT::writeDatafield(), and ReadWriteNumpyTXT::writeDatafield().

Referenced by writeSimulationResult().

Here is the call graph for this function:

◆ writeDatafield() [2/2]

void IOFactory::writeDatafield ( const std::string &  file_name,
std::function< void(std::ostream &)>  writeData 
)
staticprivate

Definition at line 95 of file IOFactory.cpp.

97 {
98  using namespace DataUtils::Format;
99 
100  std::ofstream fout;
101  std::ios_base::openmode openmode = std::ios::out;
102  if (isTiffFile(file_name) || isCompressed(file_name))
103  openmode = std::ios::out | std::ios_base::binary;
104 
105 #ifdef _WIN32
106  fout.open(BaseUtils::Filesystem::convert_utf8_to_utf16(file_name), openmode);
107 #else
108  fout.open(file_name, openmode);
109 #endif
110 
111  if (!fout.is_open())
112  throw std::runtime_error("Cannot open file for writing: " + file_name);
113  if (!fout.good())
114  throw std::runtime_error("File is not good, probably it is a directory: " + file_name);
115  std::stringstream ss;
116  writeData(ss);
117 
118  boost::iostreams::filtering_streambuf<boost::iostreams::input> input_filtered;
119  if (DataUtils::Format::isGZipped(file_name))
120  input_filtered.push(boost::iostreams::gzip_compressor());
121  else if (DataUtils::Format::isBZipped(file_name))
122  input_filtered.push(boost::iostreams::bzip2_compressor());
123  input_filtered.push(ss);
124 
125  boost::iostreams::copy(input_filtered, fout);
126 
127  fout.close();
128 }

References BaseUtils::Filesystem::convert_utf8_to_utf16(), DataUtils::Format::isBZipped(), DataUtils::Format::isCompressed(), DataUtils::Format::isGZipped(), and DataUtils::Format::isTiffFile().

Here is the call graph for this function:

◆ writeSimulationResult()

void IOFactory::writeSimulationResult ( const SimulationResult result,
const std::string &  file_name 
)
static

Writes Datafield contained in the given SimulationResult object.

Definition at line 146 of file IOFactory.cpp.

147 {
148  auto data = result.datafield();
149  writeDatafield(*data, file_name); // TODO delete ptr
150 }
Datafield * datafield(Coords units=Coords::UNDEFINED) const

References SimulationResult::datafield(), and writeDatafield().

Here is the call graph for this function:

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