BornAgain  1.18.0
Simulate and fit neutron and x-ray scattering at grazing incidence
DataFormatUtils Namespace Reference

Functions

bool isCompressed (const std::string &name)
 
bool isGZipped (const std::string &name)
 
bool isBZipped (const std::string &name)
 
std::string GetFileMainExtension (const std::string &name)
 
bool isIntFile (const std::string &file_name)
 
bool isTiffFile (const std::string &file_name)
 
std::unique_ptr< IAxiscreateAxis (std::istream &input_stream)
 
void fillOutputData (OutputData< double > *data, std::istream &input_stream)
 
std::vector< double > parse_doubles (const std::string &str)
 
void readLineOfDoubles (std::vector< double > &buffer, std::istringstream &iss)
 

Detailed Description

Utility functions for data input and output.

Function Documentation

◆ isCompressed()

bool DataFormatUtils::isCompressed ( const std::string &  name)

Returns true if name contains *.gz extension.

Definition at line 48 of file DataFormatUtils.cpp.

49 {
50  return isGZipped(name) || isBZipped(name);
51 }
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 OutputDataReader::getOutputData(), and OutputDataWriter::writeOutputData().

Here is the call graph for this function:

◆ isGZipped()

bool DataFormatUtils::isGZipped ( const std::string &  name)

Returns true if name contains *.gz extension.

Does name contain *.gz extension?

Definition at line 55 of file DataFormatUtils.cpp.

56 {
58 }
std::string extension(const std::string &path)
Returns extension of given filename.

References FileSystemUtils::extension(), and anonymous_namespace{DataFormatUtils.cpp}::GzipExtension.

Referenced by GetFileMainExtension(), anonymous_namespace{OutputDataReader.cpp}::getFromFilteredStream(), isCompressed(), and OutputDataWriter::writeOutputData().

Here is the call graph for this function:

◆ isBZipped()

bool DataFormatUtils::isBZipped ( const std::string &  name)

Returns true if name contains *.bz2 extension.

Definition at line 60 of file DataFormatUtils.cpp.

61 {
63 }

References anonymous_namespace{DataFormatUtils.cpp}::BzipExtension, and FileSystemUtils::extension().

Referenced by GetFileMainExtension(), anonymous_namespace{OutputDataReader.cpp}::getFromFilteredStream(), isCompressed(), and OutputDataWriter::writeOutputData().

Here is the call graph for this function:

◆ GetFileMainExtension()

std::string DataFormatUtils::GetFileMainExtension ( const std::string &  name)

Returns file extension after stripping '.gz' if any.

Returns file main extension (without .gz).

Definition at line 67 of file DataFormatUtils.cpp.

68 {
69  std::string stripped_name(name);
70  if (isGZipped(name)) {
71  stripped_name = name.substr(0, name.size() - GzipExtension.size());
72  } else if (isBZipped(name)) {
73  stripped_name = name.substr(0, name.size() - BzipExtension.size());
74  }
75  return FileSystemUtils::extension(stripped_name);
76 }

References anonymous_namespace{DataFormatUtils.cpp}::BzipExtension, FileSystemUtils::extension(), anonymous_namespace{DataFormatUtils.cpp}::GzipExtension, isBZipped(), and isGZipped().

Referenced by isIntFile(), and isTiffFile().

Here is the call graph for this function:

◆ isIntFile()

bool DataFormatUtils::isIntFile ( const std::string &  file_name)

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

Definition at line 78 of file DataFormatUtils.cpp.

79 {
80  return GetFileMainExtension(file_name) == IntExtension;
81 }
std::string GetFileMainExtension(const std::string &name)
Returns file extension after stripping '.gz' if any.

References GetFileMainExtension(), and anonymous_namespace{DataFormatUtils.cpp}::IntExtension.

Referenced by OutputDataReadFactory::getReadStrategy(), and OutputDataWriteFactory::getWriteStrategy().

Here is the call graph for this function:

◆ isTiffFile()

bool DataFormatUtils::isTiffFile ( const std::string &  file_name)

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

Definition at line 83 of file DataFormatUtils.cpp.

84 {
85  return (GetFileMainExtension(file_name) == TiffExtension
86  || GetFileMainExtension(file_name) == TiffExtension2);
87 }

References GetFileMainExtension(), anonymous_namespace{DataFormatUtils.cpp}::TiffExtension, and anonymous_namespace{DataFormatUtils.cpp}::TiffExtension2.

Referenced by OutputDataReader::getOutputData(), OutputDataReadFactory::getReadStrategy(), OutputDataWriteFactory::getWriteStrategy(), and OutputDataWriter::writeOutputData().

Here is the call graph for this function:

◆ createAxis()

std::unique_ptr< IAxis > DataFormatUtils::createAxis ( std::istream &  input_stream)

Creates axis of certain type from input stream.

Definition at line 90 of file DataFormatUtils.cpp.

91 {
92  auto iss = getAxisStringRepresentation(input_stream);
93  std::string type;
94  if (!(iss >> type))
96  "Error in DataFormatUtils::createAxis:: couldn't read axis type from input");
97 
98  for (auto iter = type_map.cbegin(); iter != type_map.end(); ++iter)
99  if (iter->first == type)
100  return iter->second(std::move(iss));
101  throw Exceptions::LogicErrorException("Error in DataFormatUtils::createAxis:"
102  "Unknown axis type '"
103  + type + "'");
104 }
const std::vector< std::pair< std::string, createAxisFun > > type_map
std::istringstream getAxisStringRepresentation(std::istream &input_stream)

References anonymous_namespace{DataFormatUtils.cpp}::getAxisStringRepresentation(), and anonymous_namespace{DataFormatUtils.cpp}::type_map.

Referenced by OutputDataReadINTStrategy::readOutputData().

Here is the call graph for this function:

◆ fillOutputData()

void DataFormatUtils::fillOutputData ( OutputData< double > *  data,
std::istream &  input_stream 
)

Fills output data raw buffer from input stream.

Definition at line 107 of file DataFormatUtils.cpp.

108 {
109  std::string line;
110  data->setAllTo(0.0);
111  OutputData<double>::iterator it = data->begin();
112  while (std::getline(input_stream, line)) {
113  if (line.empty() || line[0] == '#')
114  break;
115 
116  std::istringstream iss(line);
117  std::vector<double> buffer;
118  readLineOfDoubles(buffer, iss);
119  for (auto value : buffer) {
120  *it = value;
121  ++it;
122  }
123  }
124  if (it != data->end())
126  "DataFormatUtils::fillOutputData() -> Error while parsing data.");
127 }
Template class to store data of any type in multi-dimensional space.
Definition: OutputData.h:33
iterator end()
Returns read/write iterator that points to the one past last element.
Definition: OutputData.h:96
void setAllTo(const T &value)
Sets content of output data to specific value.
Definition: OutputData.h:479
iterator begin()
Returns read/write iterator that points to the first element.
Definition: OutputData.h:344
void readLineOfDoubles(std::vector< double > &buffer, std::istringstream &iss)

References OutputData< T >::begin(), OutputData< T >::end(), readLineOfDoubles(), and OutputData< T >::setAllTo().

Referenced by OutputDataReadINTStrategy::readOutputData().

Here is the call graph for this function:

◆ parse_doubles()

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

Parse double values from string to vector of double.

Definition at line 131 of file DataFormatUtils.cpp.

132 {
133  std::vector<double> result;
134  std::istringstream iss(str);
136  if (result.empty()) {
137  std::string out = str;
138  const size_t max_string_length(10);
139  if (out.size() > max_string_length)
140  out.resize(max_string_length, ' ');
141  out += " ...";
142  throw std::runtime_error("DataFormatUtils::parse_doubles -> Error! Can't parse double "
143  "values from a string '"
144  + out + "'");
145  }
146  return result;
147 }

References readLineOfDoubles().

Referenced by OutputDataReadReflectometryStrategy::readOutputData(), and OutputDataReadNumpyTXTStrategy::readOutputData().

Here is the call graph for this function:

◆ readLineOfDoubles()

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

Definition at line 149 of file DataFormatUtils.cpp.

150 {
151  iss.imbue(std::locale::classic());
152  std::copy(std::istream_iterator<double>(iss), std::istream_iterator<double>(),
153  back_inserter(buffer));
154 }

Referenced by anonymous_namespace{DataFormatUtils.cpp}::createFixedBinLikeAxis(), anonymous_namespace{DataFormatUtils.cpp}::createPointwiseAxis(), anonymous_namespace{DataFormatUtils.cpp}::createVariableBinAxis(), fillOutputData(), and parse_doubles().