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

Utility functions for data input and output. More...

Functions

std::unique_ptr< IAxiscreateAxis (std::istream &input_stream)
 Creates axis of certain type from input stream. More...
 
void fillOutputData (OutputData< double > *data, std::istream &input_stream)
 Fills output data raw buffer from input stream. More...
 
std::string GetFileMainExtension (const std::string &name)
 Returns file extension after stripping '.gz' if any. 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 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)
 

Detailed Description

Utility functions for data input and output.

Function Documentation

◆ createAxis()

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

Creates axis of certain type from input stream.

Definition at line 89 of file DataFormatUtils.cpp.

90 {
91  auto iss = getAxisStringRepresentation(input_stream);
92  std::string type;
93  if (!(iss >> type))
94  throw std::runtime_error(
95  "Error in DataFormatUtils::createAxis:: couldn't read axis type from input");
96 
97  for (auto iter = type_map.cbegin(); iter != type_map.end(); ++iter)
98  if (iter->first == type)
99  return iter->second(std::move(iss));
100  throw std::runtime_error("Error in DataFormatUtils::createAxis:"
101  "Unknown axis type '"
102  + type + "'");
103 }

Referenced by OutputDataReadWriteINT::readOutputData().

◆ fillOutputData()

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

Fills output data raw buffer from input stream.

Definition at line 106 of file DataFormatUtils.cpp.

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

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

Referenced by OutputDataReadWriteINT::readOutputData().

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 66 of file DataFormatUtils.cpp.

67 {
68  std::string stripped_name(name);
69  if (isGZipped(name)) {
70  stripped_name = name.substr(0, name.size() - GzipExtension.size());
71  } else if (isBZipped(name)) {
72  stripped_name = name.substr(0, name.size() - BzipExtension.size());
73  }
74  return FileSystemUtils::extension(stripped_name);
75 }
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.
std::string extension(const std::string &path)
Returns extension of given filename.
QString const & name(EShape k)
Definition: particles.cpp:21

References FileSystemUtils::extension(), isBZipped(), isGZipped(), and RealSpace::Particles::name().

Referenced by isIntFile(), and isTiffFile().

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 59 of file DataFormatUtils.cpp.

60 {
61  return FileSystemUtils::extension(name) == BzipExtension;
62 }

References FileSystemUtils::extension(), and RealSpace::Particles::name().

Referenced by GetFileMainExtension(), isCompressed(), IntensityDataIOFactory::readOutputData(), and IntensityDataIOFactory::writeOutputData().

Here is the call graph for this function:

◆ isCompressed()

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

Returns true if name contains *.gz extension.

Definition at line 47 of file DataFormatUtils.cpp.

48 {
49  return isGZipped(name) || isBZipped(name);
50 }

References isBZipped(), isGZipped(), and RealSpace::Particles::name().

Referenced by ImportDataUtils::Import1dData(), IntensityDataIOFactory::readOutputData(), and IntensityDataIOFactory::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 54 of file DataFormatUtils.cpp.

55 {
56  return FileSystemUtils::extension(name) == GzipExtension;
57 }

References FileSystemUtils::extension(), and RealSpace::Particles::name().

Referenced by GetFileMainExtension(), isCompressed(), IntensityDataIOFactory::readOutputData(), and IntensityDataIOFactory::writeOutputData().

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 77 of file DataFormatUtils.cpp.

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

References GetFileMainExtension().

Referenced by ImportDataUtils::Import1dData(), IntensityDataIOFactory::readOutputData(), and IntensityDataIOFactory::writeOutputData().

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 82 of file DataFormatUtils.cpp.

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

References GetFileMainExtension().

Referenced by ImportDataUtils::Import1dData(), IntensityDataIOFactory::readOutputData(), and IntensityDataIOFactory::writeOutputData().

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 OutputDataReadReflectometry::readOutputData(), and OutputDataReadWriteNumpyTXT::readOutputData().

Here is the call graph for this function:

◆ readLineOfDoubles()

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

Definition at line 151 of file DataFormatUtils.cpp.

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

Referenced by fillOutputData(), and parse_doubles().