BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
IntensityDataIOFactory Class Reference

Provides users with possibility to read and write IntensityData from/to files in different format. More...

Static Public Member Functions

static IHistogramreadIntensityData (const std::string &file_name)
 Reads file and returns newly created Histogram object. More...
 
static OutputData< double > * readOutputData (const std::string &file_name)
 Reads file and returns newly created OutputData object. More...
 
static OutputData< double > * readReflectometryData (const std::string &file_name)
 
static void writeIntensityData (const IHistogram &histogram, const std::string &file_name)
 Writes histogram in file. More...
 
static void writeOutputData (const OutputData< double > &data, const std::string &file_name)
 Writes OutputData in file. More...
 
static void writeSimulationResult (const SimulationResult &result, const std::string &file_name)
 Writes OutputData contained in the given SimulationResult object. More...
 

Static Private Member Functions

static OutputData< double > * readOutputData (const std::string &file_name, std::function< OutputData< double > *(std::istream &)> readData)
 
static void writeOutputData (const std::string &file_name, std::function< void(std::ostream &)> writeData)
 

Detailed 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 = IntensityDataIOFactory.readIntensityData("filename.txt")
histogram = IntensityDataIOFactory.readIntensityData("filename.txt.gz")
# writing to 32-bits tiff file or b-zipped tiff file
IntensityDataIOFactory.writeIntensityData(histogram, "filename.tif.bz2")
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.

Definition at line 47 of file IntensityDataIOFactory.h.

Member Function Documentation

◆ readIntensityData()

IHistogram * IntensityDataIOFactory::readIntensityData ( const std::string &  file_name)
static

Reads file and returns newly created Histogram object.

Definition at line 61 of file IntensityDataIOFactory.cpp.

62 {
63  std::unique_ptr<OutputData<double>> data(readOutputData(file_name));
64  if (!data)
65  throw std::runtime_error("Could not read " + file_name);
66  return IHistogram::createHistogram(*data);
67 }
static IHistogram * createHistogram(const OutputData< double > &source)
Definition: IHistogram.cpp:243
static OutputData< double > * readOutputData(const std::string &file_name)
Reads file and returns newly created OutputData object.

References IHistogram::createHistogram(), and readOutputData().

Referenced by IHistogram::createFrom(), and IHistogram::load().

Here is the call graph for this function:

◆ readOutputData() [1/2]

OutputData< double > * IntensityDataIOFactory::readOutputData ( const std::string &  file_name)
static

Reads file and returns newly created OutputData object.

Definition at line 38 of file IntensityDataIOFactory.cpp.

39 {
40  if (DataFormatUtils::isIntFile(file_name))
41  return readOutputData(
42  file_name, [](std::istream& s) { return OutputDataReadWriteINT().readOutputData(s); });
43 #ifdef BORNAGAIN_TIFF_SUPPORT
44  if (DataFormatUtils::isTiffFile(file_name))
45  return readOutputData(
46  file_name, [](std::istream& s) { return OutputDataReadWriteTiff().readOutputData(s); });
47 #endif
48  // Try to read ASCII by default. Binary maps to ASCII.
49  // If the file is not actually a matrix of numbers,
50  // the error will be thrown during the reading.
51  return readOutputData(
52  file_name, [](std::istream& s) { return OutputDataReadWriteNumpyTXT().readOutputData(s); });
53 }
Class for reading and writing BornAgain native IntensityData from ASCII file.
OutputData< double > * readOutputData(std::istream &input_stream)
Class for reading and writing OutputData from simple ASCII file with the layout as in numpy....
OutputData< double > * readOutputData(std::istream &input_stream)
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)

References DataFormatUtils::isIntFile(), DataFormatUtils::isTiffFile(), OutputDataReadWriteINT::readOutputData(), and OutputDataReadWriteNumpyTXT::readOutputData().

Referenced by HistoUtils::agreesWithReference(), ImportDataUtils::ImportKnownData(), DataItem::load(), PointwiseAxisItem::load(), readIntensityData(), and readReflectometryData().

Here is the call graph for this function:

◆ readOutputData() [2/2]

OutputData< double > * IntensityDataIOFactory::readOutputData ( const std::string &  file_name,
std::function< OutputData< double > *(std::istream &)>  readData 
)
staticprivate

Definition at line 140 of file IntensityDataIOFactory.cpp.

142 {
143 
144  if (!FileSystemUtils::IsFileExists(file_name))
145  return nullptr;
146 
147  using namespace DataFormatUtils;
148  std::ifstream input_stream;
149  std::ios_base::openmode openmode = std::ios::in;
150  if (isTiffFile(file_name) || isCompressed(file_name))
151  openmode = std::ios::in | std::ios_base::binary;
152 
153 #ifdef _WIN32
154  input_stream.open(FileSystemUtils::convert_utf8_to_utf16(file_name), openmode);
155 #else
156  input_stream.open(file_name, openmode);
157 #endif
158 
159  if (!input_stream.is_open())
160  throw std::runtime_error(
161  "IntensityDataIOFactory::getFromFilteredStream() -> Error. Can't open file '"
162  + file_name + "' for reading.");
163  if (!input_stream.good())
164  throw std::runtime_error("IntensityDataIOFactory::getFromFilteredStream() -> Error! "
165  "File is not good, probably it is a directory.");
166 
167  boost::iostreams::filtering_streambuf<boost::iostreams::input> input_filtered;
168  if (DataFormatUtils::isGZipped(file_name))
169  input_filtered.push(boost::iostreams::gzip_decompressor());
170  else if (DataFormatUtils::isBZipped(file_name))
171  input_filtered.push(boost::iostreams::bzip2_decompressor());
172  input_filtered.push(input_stream);
173  // we use stringstream since it provides random access which is important for tiff files
174  std::stringstream str;
175  boost::iostreams::copy(input_filtered, str);
176 
177  return readData(str);
178 }
Utility functions for data input and output.
bool isBZipped(const std::string &name)
Returns true if name contains *.bz2 extension.
bool isCompressed(const std::string &name)
Returns true if name contains *.gz extension.
bool isGZipped(const std::string &name)
Returns true if name contains *.gz extension.
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.

References FileSystemUtils::convert_utf8_to_utf16(), DataFormatUtils::isBZipped(), DataFormatUtils::isCompressed(), FileSystemUtils::IsFileExists(), DataFormatUtils::isGZipped(), and DataFormatUtils::isTiffFile().

Here is the call graph for this function:

◆ readReflectometryData()

OutputData< double > * IntensityDataIOFactory::readReflectometryData ( const std::string &  file_name)
static

Definition at line 55 of file IntensityDataIOFactory.cpp.

56 {
57  return readOutputData(
58  file_name, [](std::istream& s) { return OutputDataReadReflectometry().readOutputData(s); });
59 }
Class for reading reflectometry data from ASCII file.
OutputData< double > * readOutputData(std::istream &input_stream)

References readOutputData(), and OutputDataReadReflectometry::readOutputData().

Referenced by ImportDataUtils::ImportReflectometryData().

Here is the call graph for this function:

◆ writeIntensityData()

void IntensityDataIOFactory::writeIntensityData ( const IHistogram histogram,
const std::string &  file_name 
)
static

Writes histogram in file.

Definition at line 125 of file IntensityDataIOFactory.cpp.

127 {
128  std::unique_ptr<OutputData<double>> data(histogram.createOutputData());
129  writeOutputData(*data, file_name);
130 }
OutputData< double > * createOutputData(DataType dataType=DataType::INTEGRAL) const
creates new OutputData with histogram's shape and values corresponding to DataType
Definition: IHistogram.cpp:343
static void writeOutputData(const OutputData< double > &data, const std::string &file_name)
Writes OutputData in file.

References IHistogram::createOutputData(), and writeOutputData().

Referenced by IHistogram::save().

Here is the call graph for this function:

◆ writeOutputData() [1/2]

void IntensityDataIOFactory::writeOutputData ( const OutputData< double > &  data,
const std::string &  file_name 
)
static

Writes OutputData in file.

Definition at line 69 of file IntensityDataIOFactory.cpp.

71 {
72  if (DataFormatUtils::isIntFile(file_name))
74  file_name, [&](std::ostream& s) { OutputDataReadWriteINT().writeOutputData(data, s); });
75 #ifdef BORNAGAIN_TIFF_SUPPORT
76  else if (DataFormatUtils::isTiffFile(file_name))
77  writeOutputData(file_name, [&](std::ostream& s) {
78  OutputDataReadWriteTiff().writeOutputData(data, s);
79  });
80 #endif
81  else
82  writeOutputData(file_name, [&](std::ostream& s) {
84  });
85 }
void writeOutputData(const OutputData< double > &data, std::ostream &output_stream)
void writeOutputData(const OutputData< double > &data, std::ostream &output_stream)

References DataFormatUtils::isIntFile(), DataFormatUtils::isTiffFile(), OutputDataReadWriteINT::writeOutputData(), and OutputDataReadWriteNumpyTXT::writeOutputData().

Referenced by DataItem::save(), PointwiseAxisItem::save(), SavePlotAssistant::saveToFile(), writeIntensityData(), and writeSimulationResult().

Here is the call graph for this function:

◆ writeOutputData() [2/2]

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

Definition at line 87 of file IntensityDataIOFactory.cpp.

89 {
90  using namespace DataFormatUtils;
91 
92  std::ofstream fout;
93  std::ios_base::openmode openmode = std::ios::out;
94  if (isTiffFile(file_name) || isCompressed(file_name))
95  openmode = std::ios::out | std::ios_base::binary;
96 
97 #ifdef _WIN32
98  fout.open(FileSystemUtils::convert_utf8_to_utf16(file_name), openmode);
99 #else
100  fout.open(file_name, openmode);
101 #endif
102 
103  if (!fout.is_open())
104  throw std::runtime_error("IntensityDataIOFactory::writeOutputData() -> Error. "
105  "Can't open file '"
106  + file_name + "' for writing.");
107  if (!fout.good())
108  throw std::runtime_error("IntensityDataIOFactory::writeOutputData() -> Error! "
109  "File is not good, probably it is a directory.");
110  std::stringstream ss;
111  writeData(ss);
112 
113  boost::iostreams::filtering_streambuf<boost::iostreams::input> input_filtered;
114  if (DataFormatUtils::isGZipped(file_name))
115  input_filtered.push(boost::iostreams::gzip_compressor());
116  else if (DataFormatUtils::isBZipped(file_name))
117  input_filtered.push(boost::iostreams::bzip2_compressor());
118  input_filtered.push(ss);
119 
120  boost::iostreams::copy(input_filtered, fout);
121 
122  fout.close();
123 }

References FileSystemUtils::convert_utf8_to_utf16(), DataFormatUtils::isBZipped(), DataFormatUtils::isCompressed(), DataFormatUtils::isGZipped(), and DataFormatUtils::isTiffFile().

Here is the call graph for this function:

◆ writeSimulationResult()

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

Writes OutputData contained in the given SimulationResult object.

Definition at line 132 of file IntensityDataIOFactory.cpp.

134 {
135  auto data = result.data();
136  writeOutputData(*data, file_name);
137 }
std::unique_ptr< OutputData< double > > data(Axes::Units units=Axes::Units::DEFAULT) const

References SimulationResult::data(), and writeOutputData().

Here is the call graph for this function:

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