BornAgain  1.19.79
Open-source research software to simulate and fit neutron and x-ray reflectometry and grazing-incidence small-angle scattering
DataUtils::Format Namespace Reference

Description

Utility functions for data input and output.

Functions

IAxiscreateAxis (std::istream &input_stream)
 Creates axis of certain type from input stream. More...
 
void fillDatafield (Datafield *data, std::istream &input_stream)
 Fills output data raw buffer from input stream. More...
 
bool isBZipped (const std::string &name)
 Returns true if name contains *.bz2 extension. More...
 
bool isCompressed (const std::string &name)
 Returns true if name contains *.gz extension. More...
 
bool isGZipped (const std::string &name)
 Returns true if name contains *.gz extension. More...
 
bool isIntFile (const std::string &file_name)
 Returns true if file name corresponds to BornAgain native format (compressed or not) More...
 
bool isNicosFile (const std::string &file_name)
 Returns true if file name corresponds to Nicos format (compressed or not) More...
 
bool isTiffFile (const std::string &file_name)
 Returns true if file name corresponds to tiff file (can be also compressed) More...
 
std::vector< double > parse_doubles (const std::string &str)
 Parse double values from string to vector of double. More...
 
void readLineOfDoubles (std::vector< double > &buffer, std::istringstream &iss)
 

Function Documentation

◆ createAxis()

IAxis * DataUtils::Format::createAxis ( std::istream &  input_stream)

Creates axis of certain type from input stream.

Definition at line 154 of file DataFormatUtils.cpp.

155 {
156  auto iss = getAxisStringRepresentation(input_stream);
157  std::string type;
158  if (!(iss >> type))
159  throw std::runtime_error(
160  "Error in DataUtils::Format::createAxis:: couldn't read axis type from input");
161 
162  for (auto iter = type_map.cbegin(); iter != type_map.end(); ++iter)
163  if (iter->first == type)
164  return iter->second(std::move(iss));
165  throw std::runtime_error("Error in DataUtils::Format::createAxis:"
166  "Unknown axis type '"
167  + type + "'");
168 }

Referenced by ReadWriteINT::readDatafield().

◆ fillDatafield()

void DataUtils::Format::fillDatafield ( Datafield data,
std::istream &  input_stream 
)

Fills output data raw buffer from input stream.

Definition at line 171 of file DataFormatUtils.cpp.

172 {
173  std::string line;
174  size_t iout = 0;
175  while (std::getline(input_stream, line)) {
176  if (line.empty() || line[0] == '#')
177  break;
178  std::istringstream iss(line);
179  std::vector<double> buffer;
180  readLineOfDoubles(buffer, iss);
181  for (auto value : buffer)
182  (*data)[iout++] = value;
183  }
184  if (iout != data->size())
185  throw std::runtime_error("Error while parsing data, did not reach expected end");
186 }
size_t size() const
Returns total size of data buffer (product of bin number in every dimension).
Definition: Datafield.cpp:80
void readLineOfDoubles(std::vector< double > &buffer, std::istringstream &iss)

References readLineOfDoubles(), and Datafield::size().

Referenced by ReadWriteINT::readDatafield().

Here is the call graph for this function:

◆ isBZipped()

bool DataUtils::Format::isBZipped ( const std::string &  name)

Returns true if name contains *.bz2 extension.

Definition at line 132 of file DataFormatUtils.cpp.

133 {
134  return BaseUtils::Filesystem::hasExtension(name, BzipExtension);
135 }
bool hasExtension(const std::string &path, const std::string &ref_extension)
Returns true if extension of path, converted to lower case, matches given reference extension.

References BaseUtils::Filesystem::hasExtension().

Referenced by isCompressed(), IOFactory::readDatafield(), and IOFactory::writeDatafield().

Here is the call graph for this function:

◆ isCompressed()

bool DataUtils::Format::isCompressed ( const std::string &  name)

Returns true if name contains *.gz extension.

Definition at line 120 of file DataFormatUtils.cpp.

121 {
122  return isGZipped(name) || isBZipped(name);
123 }
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.

References isBZipped(), and isGZipped().

Referenced by IOFactory::readDatafield(), and IOFactory::writeDatafield().

Here is the call graph for this function:

◆ isGZipped()

bool DataUtils::Format::isGZipped ( const std::string &  name)

Returns true if name contains *.gz extension.

Does name contain *.gz extension?

Definition at line 127 of file DataFormatUtils.cpp.

128 {
129  return BaseUtils::Filesystem::hasExtension(name, GzipExtension);
130 }

References BaseUtils::Filesystem::hasExtension().

Referenced by isCompressed(), IOFactory::readDatafield(), and IOFactory::writeDatafield().

Here is the call graph for this function:

◆ isIntFile()

bool DataUtils::Format::isIntFile ( const std::string &  file_name)

Returns true if file name corresponds to BornAgain native format (compressed or not)

Definition at line 137 of file DataFormatUtils.cpp.

138 {
139  return BaseUtils::Filesystem::hasExtension(uncompressedFilename(file_name), IntExtension);
140 }

References BaseUtils::Filesystem::hasExtension().

Referenced by IOFactory::fileTypeMatchesLoaderSelector(), and IOFactory::writeDatafield().

Here is the call graph for this function:

◆ isNicosFile()

bool DataUtils::Format::isNicosFile ( const std::string &  file_name)

Returns true if file name corresponds to Nicos format (compressed or not)

Definition at line 142 of file DataFormatUtils.cpp.

143 {
144  return BaseUtils::Filesystem::hasExtension(uncompressedFilename(file_name), NicosExtension);
145 }

References BaseUtils::Filesystem::hasExtension().

Referenced by IOFactory::fileTypeMatchesLoaderSelector().

Here is the call graph for this function:

◆ isTiffFile()

bool DataUtils::Format::isTiffFile ( const std::string &  file_name)

Returns true if file name corresponds to tiff file (can be also compressed)

Definition at line 147 of file DataFormatUtils.cpp.

148 {
149  return BaseUtils::Filesystem::hasExtension(uncompressedFilename(file_name), TiffExtension)
150  || BaseUtils::Filesystem::hasExtension(uncompressedFilename(file_name), TiffExtension2);
151 }

References BaseUtils::Filesystem::hasExtension().

Referenced by IOFactory::fileTypeMatchesLoaderSelector(), IOFactory::readDatafield(), and IOFactory::writeDatafield().

Here is the call graph for this function:

◆ parse_doubles()

std::vector< double > DataUtils::Format::parse_doubles ( const std::string &  str)

Parse double values from string to vector of double.

Definition at line 192 of file DataFormatUtils.cpp.

193 {
194  std::vector<double> result;
195  std::istringstream iss(str);
197  if (result.empty()) {
198  std::string out = str;
199  const size_t max_string_length(10);
200  if (out.size() > max_string_length)
201  out.resize(max_string_length, ' ');
202  out += " ...";
203  throw std::runtime_error("DataUtils::Format::parse_doubles -> Error! Can't parse double "
204  "values from a string '"
205  + out + "'");
206  }
207  return result;
208 }

References readLineOfDoubles().

Referenced by ReadWriteNumpyTXT::readDatafield(), and ReadReflectometry::readDatafield().

Here is the call graph for this function:

◆ readLineOfDoubles()

void DataUtils::Format::readLineOfDoubles ( std::vector< double > &  buffer,
std::istringstream &  iss 
)

Definition at line 212 of file DataFormatUtils.cpp.

213 {
214  iss.imbue(std::locale::classic());
215  std::copy(std::istream_iterator<double>(iss), std::istream_iterator<double>(),
216  back_inserter(buffer));
217 }

Referenced by fillDatafield(), and parse_doubles().