www.mooseframework.org
Public Types | Public Member Functions | Public Attributes | Protected Attributes | Private Member Functions | List of all members
MooseUtils::DelimitedFileReader Class Reference

Utility class for reading delimited data (e.g., CSV data). More...

#include <DelimitedFileReader.h>

Public Types

enum  HeaderFlag { HeaderFlag::OFF = 0, HeaderFlag::ON = 1, HeaderFlag::AUTO = 2 }
 
enum  FormatFlag { FormatFlag::COLUMNS = 0, FormatFlag::ROWS = 1 }
 

Public Member Functions

 DelimitedFileReader (const std::string &filename, const libMesh::Parallel::Communicator *comm=nullptr)
 
void read ()
 Perform the actual data reading. More...
 
std::size_t numEntries () const
 Get the total number of entries in the file. More...
 
void setFileName (const std::string &new_file)
 Set the file name, used to change the file to read from. More...
 
const std::vector< std::string > & getNames () const
 Return the column/row names. More...
 
const std::vector< std::vector< double > > & getData () const
 Return the rows/columns of data. More...
 
const std::vector< Point > getDataAsPoints () const
 Get the data in Point format. More...
 
void setIgnoreEmptyLines (bool value)
 Set/Get methods for file format controls. More...
 
bool getIgnoreEmptyLines () const
 
void setFormatFlag (FormatFlag value)
 
FormatFlag getFormatFlag () const
 
void setDelimiter (const std::string &value)
 
const std::string & setDelimiter () const
 
void setHeaderFlag (HeaderFlag value)
 
HeaderFlag getHeaderFlag () const
 
void setComment (const std::string &value)
 
const std::string & getComment () const
 
const std::vector< double > & getData (const std::string &name) const
 Return the row/column of data for a specified header entry. More...
 
const std::vector< double > & getData (std::size_t index) const
 
void setHeaderFlag (bool value)
 Deprecated. More...
 
const std::vector< std::string > & getColumnNames () const
 
const std::vector< std::vector< double > > & getColumnData () const
 
const std::vector< double > & getColumnData (const std::string &name) const
 
 DelimitedFileReader (const std::string &filename, const bool header, const std::string delimiter, const libMesh::Parallel::Communicator *comm=nullptr)
 

Public Attributes

const std::size_t INVALID_SIZE = std::numeric_limits<std::size_t>::max()
 

Protected Attributes

std::string _filename
 The supplied filename. More...
 
HeaderFlag _header_flag
 Flag indicating if the file contains a header. More...
 
std::string _delimiter
 The delimiter separating the supplied data entires. More...
 
bool _ignore_empty_lines
 Flag for ignoring empty lines. More...
 
std::vector< std::string > _names
 Storage for the read or generated column names. More...
 
std::vector< std::vector< double > > _data
 Storage for the read data columns. More...
 
const libMesh::Parallel::Communicator *const _communicator
 Communicator. More...
 
FormatFlag _format_flag
 Format "rows" vs "columns". More...
 
std::vector< std::size_t > _row_offsets
 Row offsets (only used with _format == "rows") More...
 
std::string _row_comment
 Hide row comments. More...
 

Private Member Functions

void processLine (const std::string &line, std::vector< double > &row, const unsigned int &num)
 Populate supplied vector with content from line. More...
 
bool preprocessLine (std::string &line, const unsigned int &num)
 Check the content of the line and if it should be skipped. More...
 
const std::string & delimiter (const std::string &line)
 Determine the delimiter. More...
 
bool header (const std::string &line)
 Return the header flag, if it is set to AUTO attempt to determine if a header exists in line. More...
 
void readColumnData (std::ifstream &stream_data, std::vector< double > &output)
 Read the numeric data as rows or columns into a single vector. More...
 
void readRowData (std::ifstream &stream_data, std::vector< double > &output)
 

Detailed Description

Utility class for reading delimited data (e.g., CSV data).

Parameters
filenameA string for the filename to read.
commA pointer to a Communicator object (see below).

This class assumes that all data is numeric and can be converted to a C++ double. If a Communicator is provide then it will only read on processor 0 and broadcast the data to all processors. If not provided it will read on all processors.

Definition at line 35 of file DelimitedFileReader.h.

Member Enumeration Documentation

◆ FormatFlag

Enumerator
COLUMNS 
ROWS 

Definition at line 45 of file DelimitedFileReader.h.

46  {
47  COLUMNS = 0,
48  ROWS = 1
49  };

◆ HeaderFlag

Enumerator
OFF 
ON 
AUTO 

Definition at line 38 of file DelimitedFileReader.h.

39  {
40  OFF = 0,
41  ON = 1,
42  AUTO = 2
43  };

Constructor & Destructor Documentation

◆ DelimitedFileReader() [1/2]

MooseUtils::DelimitedFileReader::DelimitedFileReader ( const std::string &  filename,
const libMesh::Parallel::Communicator comm = nullptr 
)

Definition at line 24 of file DelimitedFileReader.C.

26  : _filename(filename),
28  _ignore_empty_lines(true),
29  _communicator(comm),
31 {
32 }
HeaderFlag _header_flag
Flag indicating if the file contains a header.
bool _ignore_empty_lines
Flag for ignoring empty lines.
FormatFlag _format_flag
Format "rows" vs "columns".
std::string _filename
The supplied filename.
const libMesh::Parallel::Communicator *const _communicator
Communicator.

◆ DelimitedFileReader() [2/2]

MooseUtils::DelimitedFileReader::DelimitedFileReader ( const std::string &  filename,
const bool  header,
const std::string  delimiter,
const libMesh::Parallel::Communicator comm = nullptr 
)

Definition at line 398 of file DelimitedFileReader.C.

402  : _filename(filename),
405  _ignore_empty_lines(true),
406  _communicator(comm),
408 {
409  mooseDeprecated("Use setHeader and setDelimiter method rather than specifying in constructor.");
410 }
std::string _delimiter
The delimiter separating the supplied data entires.
HeaderFlag _header_flag
Flag indicating if the file contains a header.
bool _ignore_empty_lines
Flag for ignoring empty lines.
FormatFlag _format_flag
Format "rows" vs "columns".
bool header(const std::string &line)
Return the header flag, if it is set to AUTO attempt to determine if a header exists in line...
void mooseDeprecated(Args &&... args)
Emit a deprecated code/feature message with the given stringified, concatenated args.
Definition: MooseError.h:313
std::string _filename
The supplied filename.
const libMesh::Parallel::Communicator *const _communicator
Communicator.
const std::string & delimiter(const std::string &line)
Determine the delimiter.

Member Function Documentation

◆ delimiter()

const std::string & MooseUtils::DelimitedFileReader::delimiter ( const std::string &  line)
private

Determine the delimiter.

If the setDelimiter method is not called the data is inspected, if a ',' is found it is assumed to be the delimiter as is the case for . Otherwise a space is used.

Definition at line 359 of file DelimitedFileReader.C.

Referenced by header(), processLine(), readColumnData(), and readRowData().

360 {
361  if (_delimiter.empty())
362  {
363  if (line.find(",") != std::string::npos)
364  _delimiter = ",";
365  else if (line.find("\t") != std::string::npos)
366  _delimiter = "\t";
367  else
368  _delimiter = " ";
369  }
370  return _delimiter;
371 }
std::string _delimiter
The delimiter separating the supplied data entires.

◆ getColumnData() [1/2]

const std::vector< std::vector< double > > & MooseUtils::DelimitedFileReader::getColumnData ( ) const

Definition at line 420 of file DelimitedFileReader.C.

421 {
422  mooseDeprecated("Use getData instead.");
423  return getData();
424 }
const std::vector< std::vector< double > > & getData() const
Return the rows/columns of data.
void mooseDeprecated(Args &&... args)
Emit a deprecated code/feature message with the given stringified, concatenated args.
Definition: MooseError.h:313

◆ getColumnData() [2/2]

const std::vector< double > & MooseUtils::DelimitedFileReader::getColumnData ( const std::string &  name) const

Definition at line 427 of file DelimitedFileReader.C.

428 {
429  mooseDeprecated("Use getData instead.");
430  return getData(name);
431 }
const std::vector< std::vector< double > > & getData() const
Return the rows/columns of data.
void mooseDeprecated(Args &&... args)
Emit a deprecated code/feature message with the given stringified, concatenated args.
Definition: MooseError.h:313

◆ getColumnNames()

const std::vector< std::string > & MooseUtils::DelimitedFileReader::getColumnNames ( ) const

Definition at line 413 of file DelimitedFileReader.C.

414 {
415  mooseDeprecated("Use getNames instead.");
416  return getNames();
417 }
void mooseDeprecated(Args &&... args)
Emit a deprecated code/feature message with the given stringified, concatenated args.
Definition: MooseError.h:313
const std::vector< std::string > & getNames() const
Return the column/row names.

◆ getComment()

const std::string& MooseUtils::DelimitedFileReader::getComment ( ) const
inline

Definition at line 92 of file DelimitedFileReader.h.

92 { return _row_comment; }
std::string _row_comment
Hide row comments.

◆ getData() [1/3]

const std::vector< std::vector< double > > & MooseUtils::DelimitedFileReader::getData ( ) const

◆ getData() [2/3]

const std::vector< double > & MooseUtils::DelimitedFileReader::getData ( const std::string &  name) const

Return the row/column of data for a specified header entry.

Definition at line 177 of file DelimitedFileReader.C.

178 {
179  const auto it = find(_names.begin(), _names.end(), name);
180  if (it == _names.end())
181  mooseError("Could not find '", name, "' in header of file ", _filename, ".");
182  return _data[std::distance(_names.begin(), it)];
183 }
std::string name(const ElemQuality q)
std::vector< std::vector< double > > _data
Storage for the read data columns.
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:284
std::vector< std::string > _names
Storage for the read or generated column names.
std::string _filename
The supplied filename.

◆ getData() [3/3]

const std::vector< double > & MooseUtils::DelimitedFileReader::getData ( std::size_t  index) const

Definition at line 186 of file DelimitedFileReader.C.

187 {
188  if (index >= _data.size())
189  mooseError("The supplied index ",
190  index,
191  " is out-of-range for the available data in file '",
192  _filename,
193  "' which contains ",
194  _data.size(),
195  " items.");
196  return _data[index];
197 }
std::vector< std::vector< double > > _data
Storage for the read data columns.
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:284
std::string _filename
The supplied filename.

◆ getDataAsPoints()

const std::vector< Point > MooseUtils::DelimitedFileReader::getDataAsPoints ( ) const

Get the data in Point format.

This performs checks that the data is of valid dimensions to do so.

Definition at line 146 of file DelimitedFileReader.C.

Referenced by FilePositions::FilePositions(), CombinerGenerator::fillPositions(), and MultiApp::fillPositions().

147 {
148  std::vector<Point> point_data;
149 
150  for (std::size_t i = 0; i < _data.size(); ++i)
151  {
152  Point point;
153 
154  // Other checks in this class ensure that each data entry has the same number of values;
155  // here we just need to check that each data set has LIBMESH_DIM entries (which we could do by
156  // equivalently checking that the total number of entries is divisibly by LIBMESH_DIM
157  // _and_ one of these data sets has LIBMESH_DIM entries (consider the fringe case where
158  // LIBMESH_DIM is 3, but you accidentally put a point file like
159  // 0 0
160  // 1 0
161  // 2 0
162  // where each point is the same length _and_ the total points is still divisible by 3.
163  // This check here is more exact.
164  if (_data.at(i).size() != LIBMESH_DIM)
165  mooseError("Each point in file ", _filename, " must have ", LIBMESH_DIM, " entries");
166 
167  for (std::size_t j = 0; j < LIBMESH_DIM; ++j)
168  point(j) = _data.at(i).at(j);
169 
170  point_data.push_back(point);
171  }
172 
173  return point_data;
174 }
std::vector< std::vector< double > > _data
Storage for the read data columns.
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:284
std::string _filename
The supplied filename.

◆ getFormatFlag()

FormatFlag MooseUtils::DelimitedFileReader::getFormatFlag ( ) const
inline

Definition at line 83 of file DelimitedFileReader.h.

83 { return _format_flag; }
FormatFlag _format_flag
Format "rows" vs "columns".

◆ getHeaderFlag()

HeaderFlag MooseUtils::DelimitedFileReader::getHeaderFlag ( ) const
inline

Definition at line 89 of file DelimitedFileReader.h.

89 { return _header_flag; }
HeaderFlag _header_flag
Flag indicating if the file contains a header.

◆ getIgnoreEmptyLines()

bool MooseUtils::DelimitedFileReader::getIgnoreEmptyLines ( ) const
inline

Definition at line 80 of file DelimitedFileReader.h.

80 { return _ignore_empty_lines; }
bool _ignore_empty_lines
Flag for ignoring empty lines.

◆ getNames()

const std::vector< std::string > & MooseUtils::DelimitedFileReader::getNames ( ) const

Return the column/row names.

Definition at line 134 of file DelimitedFileReader.C.

Referenced by PiecewiseTabularBase::buildFromFile(), CSVReaderVectorPostprocessor::CSVReaderVectorPostprocessor(), and getColumnNames().

135 {
136  return _names;
137 }
std::vector< std::string > _names
Storage for the read or generated column names.

◆ header()

bool MooseUtils::DelimitedFileReader::header ( const std::string &  line)
private

Return the header flag, if it is set to AUTO attempt to determine if a header exists in line.

Definition at line 374 of file DelimitedFileReader.C.

Referenced by readColumnData(), and readRowData().

375 {
376  switch (_header_flag)
377  {
378  case HeaderFlag::OFF:
379  return false;
380  case HeaderFlag::ON:
381  return true;
382  default:
383 
384  // Attempt to convert the line, if it fails assume it is a header
385  std::vector<double> row;
386  bool contains_alpha = !MooseUtils::tokenizeAndConvert<double>(line, row, delimiter(line));
387 
388  // Based on auto detect set the flag to TRUE|FALSE to short-circuit this check for each line
389  // in the case of row data.
390  _header_flag = contains_alpha ? HeaderFlag::ON : HeaderFlag::OFF;
391  return contains_alpha;
392  }
393 }
HeaderFlag _header_flag
Flag indicating if the file contains a header.
const std::string & delimiter(const std::string &line)
Determine the delimiter.

◆ numEntries()

std::size_t MooseUtils::DelimitedFileReader::numEntries ( ) const

Get the total number of entries in the file.

Returns
number of entries in file

Definition at line 124 of file DelimitedFileReader.C.

125 {
126  std::size_t n_entries = 0;
127  for (std::size_t i = 0; i < _data.size(); ++i)
128  n_entries += _data[i].size();
129 
130  return n_entries;
131 }
std::vector< std::vector< double > > _data
Storage for the read data columns.

◆ preprocessLine()

bool MooseUtils::DelimitedFileReader::preprocessLine ( std::string &  line,
const unsigned int num 
)
private

Check the content of the line and if it should be skipped.

Parameters
lineComplete line being read.
numThe current line number.
Returns
True if the line should be skipped.

Definition at line 323 of file DelimitedFileReader.C.

Referenced by readColumnData(), and readRowData().

324 {
325  // Handle row comments
326  std::size_t index = _row_comment.empty() ? line.size() : line.find_first_of(_row_comment);
327  line = MooseUtils::trim(line.substr(0, index));
328 
329  // Ignore empty lines
330  if (line.empty())
331  {
333  return true;
334  else
335  mooseError("Failed to read line ", num, " in file ", _filename, ". The line is empty.");
336  }
337  return false;
338 }
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:284
bool _ignore_empty_lines
Flag for ignoring empty lines.
std::string trim(const std::string &str, const std::string &white_space=" \\\)
Standard scripting language trim function.
Definition: MooseUtils.C:214
std::string _filename
The supplied filename.
std::string _row_comment
Hide row comments.

◆ processLine()

void MooseUtils::DelimitedFileReader::processLine ( const std::string &  line,
std::vector< double > &  row,
const unsigned int num 
)
private

Populate supplied vector with content from line.

Parameters
lineThe line to extract data from.
rowThe vector to populate.
numThe current line number.

Definition at line 341 of file DelimitedFileReader.C.

Referenced by readColumnData(), and readRowData().

344 {
345  // Separate the row and error if it fails
346  bool status = MooseUtils::tokenizeAndConvert<double>(line, row, delimiter(line));
347  if (!status)
348  mooseError("Failed to convert a delimited data into double when reading line ",
349  num,
350  " in file ",
351  _filename,
352  ".\n LINE ",
353  num,
354  ": ",
355  line);
356 }
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:284
MPI_Status status
std::string _filename
The supplied filename.
const std::string & delimiter(const std::string &line)
Determine the delimiter.

◆ read()

void MooseUtils::DelimitedFileReader::read ( )

Perform the actual data reading.

This is a separate method to allow for the filename to be read multiple times.

Definition at line 35 of file DelimitedFileReader.C.

Referenced by PiecewiseTabularBase::buildFromFile(), CSVFileTimes::CSVFileTimes(), CSVReaderVectorPostprocessor::CSVReaderVectorPostprocessor(), FilePositions::FilePositions(), CombinerGenerator::fillPositions(), MultiApp::fillPositions(), CSVTimeSequenceStepper::init(), and PropertyReadFile::readData().

36 {
37  // Number of columns
38  std::size_t n_cols;
39 
40  // Storage for the raw data
41  std::vector<double> raw;
42  std::size_t size_raw = 0;
43  std::size_t size_offsets = 0;
44 
45  // Read data
46  if (_communicator == nullptr || _communicator->rank() == 0)
47  {
48  // Check the file
50 
51  // Create the file stream and do nothing if the file is empty
52  std::ifstream stream_data(_filename);
53  if (stream_data.peek() == std::ifstream::traits_type::eof())
54  return;
55 
56  // Read/generate the header
58  readRowData(stream_data, raw);
59  else
60  readColumnData(stream_data, raw);
61 
62  // Set the number of columns
63  n_cols = _names.size();
64 
65  // Close the stream
66  stream_data.close();
67 
68  // Set raw data vector size
69  size_raw = raw.size();
70  size_offsets = _row_offsets.size();
71  }
72 
73  if (_communicator != nullptr)
74  {
75  // Broadcast column names
76  _communicator->broadcast(n_cols);
77  _names.resize(n_cols);
79 
80  // Broadcast raw data
81  _communicator->broadcast(size_raw);
82  raw.resize(size_raw);
84 
85  // Broadcast row offsets
87  {
88  _communicator->broadcast(size_offsets);
89  _row_offsets.resize(size_offsets);
91  }
92  }
93 
94  // Resize the internal storage
95  _data.resize(n_cols);
96 
97  // Process "row" formatted data
99  {
100  std::vector<double>::iterator start = raw.begin();
101  for (std::size_t j = 0; j < n_cols; ++j)
102  {
103  _data[j] = std::vector<double>(start, start + _row_offsets[j]);
104  std::advance(start, _row_offsets[j]);
105  }
106  }
107 
108  // Process "column" formatted data
109  else
110  {
111  mooseAssert(raw.size() % n_cols == 0,
112  "The raw data is not evenly divisible by the number of columns.");
113  const std::size_t n_rows = raw.size() / n_cols;
114  for (std::size_t j = 0; j < n_cols; ++j)
115  {
116  _data[j].resize(n_rows);
117  for (std::size_t i = 0; i < n_rows; ++i)
118  _data[j][i] = raw[i * n_cols + j];
119  }
120  }
121 }
std::vector< std::vector< double > > _data
Storage for the read data columns.
std::vector< std::string > _names
Storage for the read or generated column names.
FormatFlag _format_flag
Format "rows" vs "columns".
processor_id_type rank() const
void readColumnData(std::ifstream &stream_data, std::vector< double > &output)
Read the numeric data as rows or columns into a single vector.
bool checkFileReadable(const std::string &filename, bool check_line_endings=false, bool throw_on_unreadable=true, bool check_for_git_lfs_pointer=true)
Checks to see if a file is readable (exists and permissions)
Definition: MooseUtils.C:256
void readRowData(std::ifstream &stream_data, std::vector< double > &output)
std::string _filename
The supplied filename.
void broadcast(T &data, const unsigned int root_id=0, const bool identical_sizes=false) const
const libMesh::Parallel::Communicator *const _communicator
Communicator.
std::vector< std::size_t > _row_offsets
Row offsets (only used with _format == "rows")

◆ readColumnData()

void MooseUtils::DelimitedFileReader::readColumnData ( std::ifstream &  stream_data,
std::vector< double > &  output 
)
private

Read the numeric data as rows or columns into a single vector.

Definition at line 200 of file DelimitedFileReader.C.

Referenced by read().

201 {
202  // Local storage for the data being read
203  std::string line;
204  std::vector<double> row;
205 
206  // Keep track of the line number for error reporting
207  unsigned int count = 0;
208 
209  // Number of columns expected based on the first row of the data
210  std::size_t n_cols = INVALID_SIZE;
211 
212  // Read the lines
213  while (std::getline(stream_data, line))
214  {
215  // Increment line counter and clear any tokenized data
216  count++;
217  row.clear();
218 
219  // Ignore empty and/or comment lines, if applicable
220  if (preprocessLine(line, count))
221  continue;
222 
223  // Read header, if the header exists and the column names do not exist.
224  if (_names.empty() && header(line))
225  {
226  MooseUtils::tokenize(line, _names, 1, delimiter(line));
227  for (std::string & str : _names)
228  str = MooseUtils::trim(str);
229  continue;
230  }
231 
232  // Separate the row and error if it fails
233  processLine(line, row, count);
234 
235  // Set the number of columns
236  if (n_cols == INVALID_SIZE)
237  n_cols = row.size();
238 
239  // Check number of columns
240  if (row.size() != n_cols)
241  mooseError("The number of columns read (",
242  row.size(),
243  ") does not match the number of columns expected (",
244  n_cols,
245  ") based on the first row of the file when reading row ",
246  count,
247  " in file ",
248  _filename,
249  ".");
250 
251  // Append data
252  output.insert(output.end(), row.begin(), row.end());
253  }
254 
255  // If the names have not been assigned, create the default names
256  if (_names.empty())
257  {
258  _names.resize(n_cols);
259  int padding = MooseUtils::numDigits(n_cols);
260  for (std::size_t i = 0; i < n_cols; ++i)
261  {
262  std::stringstream ss;
263  ss << "column_" << std::setw(padding) << std::setfill('0') << i;
264  _names[i] = ss.str();
265  }
266  }
267 }
void tokenize(const std::string &str, std::vector< T > &elements, unsigned int min_len=1, const std::string &delims="/")
This function will split the passed in string on a set of delimiters appending the substrings to the ...
Definition: MooseUtils.h:779
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:284
bool preprocessLine(std::string &line, const unsigned int &num)
Check the content of the line and if it should be skipped.
std::vector< std::string > _names
Storage for the read or generated column names.
bool header(const std::string &line)
Return the header flag, if it is set to AUTO attempt to determine if a header exists in line...
std::string trim(const std::string &str, const std::string &white_space=" \\\)
Standard scripting language trim function.
Definition: MooseUtils.C:214
std::string _filename
The supplied filename.
int numDigits(const T &num)
Return the number of digits for a number.
Definition: MooseUtils.h:940
const std::string & delimiter(const std::string &line)
Determine the delimiter.
void processLine(const std::string &line, std::vector< double > &row, const unsigned int &num)
Populate supplied vector with content from line.

◆ readRowData()

void MooseUtils::DelimitedFileReader::readRowData ( std::ifstream &  stream_data,
std::vector< double > &  output 
)
private

Definition at line 270 of file DelimitedFileReader.C.

Referenced by read().

271 {
272  // Local storage for the data being read
273  std::string line;
274  std::vector<double> row;
275  unsigned int linenum = 0; // line number in file
276 
277  // Clear existing data
278  _names.clear();
279  _row_offsets.clear();
280 
281  // Read the lines
282  while (std::getline(stream_data, line))
283  {
284  // Increment line counter and clear any tokenized data
285  linenum++;
286  row.clear();
287 
288  // Ignore empty lines
289  if (preprocessLine(line, linenum))
290  continue;
291 
292  if (header(line))
293  {
294  std::size_t index = line.find_first_of(delimiter(line));
295  _names.push_back(line.substr(0, index));
296  line = line.substr(index);
297  }
298 
299  // Separate the row and error if it fails
300  processLine(line, row, linenum);
301 
302  // Store row offsets to allow for un-even rows
303  _row_offsets.push_back(row.size());
304 
305  // Append data
306  output.insert(output.end(), row.begin(), row.end());
307  }
308 
309  // Assign row names if not provided via header
310  if (_names.empty())
311  {
312  int padding = MooseUtils::numDigits(_row_offsets.size());
313  for (std::size_t i = 0; i < _row_offsets.size(); ++i)
314  {
315  std::stringstream ss;
316  ss << "row_" << std::setw(padding) << std::setfill('0') << i;
317  _names.push_back(ss.str());
318  }
319  }
320 }
bool preprocessLine(std::string &line, const unsigned int &num)
Check the content of the line and if it should be skipped.
std::vector< std::string > _names
Storage for the read or generated column names.
bool header(const std::string &line)
Return the header flag, if it is set to AUTO attempt to determine if a header exists in line...
int numDigits(const T &num)
Return the number of digits for a number.
Definition: MooseUtils.h:940
const std::string & delimiter(const std::string &line)
Determine the delimiter.
std::vector< std::size_t > _row_offsets
Row offsets (only used with _format == "rows")
void processLine(const std::string &line, std::vector< double > &row, const unsigned int &num)
Populate supplied vector with content from line.

◆ setComment()

void MooseUtils::DelimitedFileReader::setComment ( const std::string &  value)
inline

Definition at line 91 of file DelimitedFileReader.h.

Referenced by PiecewiseTabularBase::buildFromFile().

91 { _row_comment = value; }
Real value(unsigned n, unsigned alpha, unsigned beta, Real x)
std::string _row_comment
Hide row comments.

◆ setDelimiter() [1/2]

void MooseUtils::DelimitedFileReader::setDelimiter ( const std::string &  value)
inline

Definition at line 85 of file DelimitedFileReader.h.

Referenced by CSVReaderVectorPostprocessor::CSVReaderVectorPostprocessor(), and CSVTimeSequenceStepper::init().

85 { _delimiter = value; }
std::string _delimiter
The delimiter separating the supplied data entires.
Real value(unsigned n, unsigned alpha, unsigned beta, Real x)

◆ setDelimiter() [2/2]

const std::string& MooseUtils::DelimitedFileReader::setDelimiter ( ) const
inline

Definition at line 86 of file DelimitedFileReader.h.

86 { return _delimiter; }
std::string _delimiter
The delimiter separating the supplied data entires.

◆ setFileName()

void MooseUtils::DelimitedFileReader::setFileName ( const std::string &  new_file)
inline

Set the file name, used to change the file to read from.

Definition at line 96 of file DelimitedFileReader.h.

Referenced by PropertyReadFile::initialize().

96 { _filename = new_file; }
std::string _filename
The supplied filename.

◆ setFormatFlag()

void MooseUtils::DelimitedFileReader::setFormatFlag ( FormatFlag  value)
inline

Definition at line 82 of file DelimitedFileReader.h.

Referenced by PiecewiseTabularBase::buildFromFile(), CSVFileTimes::CSVFileTimes(), FilePositions::FilePositions(), CombinerGenerator::fillPositions(), MultiApp::fillPositions(), and PropertyReadFile::readData().

82 { _format_flag = value; }
FormatFlag _format_flag
Format "rows" vs "columns".
Real value(unsigned n, unsigned alpha, unsigned beta, Real x)

◆ setHeaderFlag() [1/2]

void MooseUtils::DelimitedFileReader::setHeaderFlag ( HeaderFlag  value)
inline

Definition at line 88 of file DelimitedFileReader.h.

Referenced by CSVReaderVectorPostprocessor::CSVReaderVectorPostprocessor(), and CSVTimeSequenceStepper::init().

88 { _header_flag = value; }
HeaderFlag _header_flag
Flag indicating if the file contains a header.
Real value(unsigned n, unsigned alpha, unsigned beta, Real x)

◆ setHeaderFlag() [2/2]

void MooseUtils::DelimitedFileReader::setHeaderFlag ( bool  value)

Deprecated.

Definition at line 434 of file DelimitedFileReader.C.

435 {
436  mooseDeprecated("Use header method with HeaderFlag input.");
438 }
HeaderFlag _header_flag
Flag indicating if the file contains a header.
Real value(unsigned n, unsigned alpha, unsigned beta, Real x)
void mooseDeprecated(Args &&... args)
Emit a deprecated code/feature message with the given stringified, concatenated args.
Definition: MooseError.h:313

◆ setIgnoreEmptyLines()

void MooseUtils::DelimitedFileReader::setIgnoreEmptyLines ( bool  value)
inline

Set/Get methods for file format controls.

IgnoreEmptyLines: When true all empty lines are ignored, when false an error is produced. FormatFlag: Set the file format (rows vs. columns). Delimiter: Set the file delimiter (if unset it will be detected). HeaderFlag: Set the header flag (TRUE used the first row has header, FALSE assumes no header, and AUTO will attempt to determine if a header exists). Comment: Set the comment character, by default no comment character is used.

Definition at line 79 of file DelimitedFileReader.h.

Referenced by CSVReaderVectorPostprocessor::CSVReaderVectorPostprocessor().

bool _ignore_empty_lines
Flag for ignoring empty lines.
Real value(unsigned n, unsigned alpha, unsigned beta, Real x)

Member Data Documentation

◆ _communicator

const libMesh::Parallel::Communicator* const MooseUtils::DelimitedFileReader::_communicator
protected

Communicator.

Definition at line 158 of file DelimitedFileReader.h.

Referenced by read().

◆ _data

std::vector<std::vector<double> > MooseUtils::DelimitedFileReader::_data
protected

Storage for the read data columns.

Definition at line 155 of file DelimitedFileReader.h.

Referenced by getData(), getDataAsPoints(), numEntries(), and read().

◆ _delimiter

std::string MooseUtils::DelimitedFileReader::_delimiter
protected

The delimiter separating the supplied data entires.

Definition at line 146 of file DelimitedFileReader.h.

Referenced by delimiter(), and setDelimiter().

◆ _filename

std::string MooseUtils::DelimitedFileReader::_filename
protected

The supplied filename.

Definition at line 140 of file DelimitedFileReader.h.

Referenced by getData(), getDataAsPoints(), preprocessLine(), processLine(), read(), readColumnData(), and setFileName().

◆ _format_flag

FormatFlag MooseUtils::DelimitedFileReader::_format_flag
protected

Format "rows" vs "columns".

Definition at line 161 of file DelimitedFileReader.h.

Referenced by getFormatFlag(), read(), and setFormatFlag().

◆ _header_flag

HeaderFlag MooseUtils::DelimitedFileReader::_header_flag
protected

Flag indicating if the file contains a header.

Definition at line 143 of file DelimitedFileReader.h.

Referenced by getHeaderFlag(), header(), and setHeaderFlag().

◆ _ignore_empty_lines

bool MooseUtils::DelimitedFileReader::_ignore_empty_lines
protected

Flag for ignoring empty lines.

Definition at line 149 of file DelimitedFileReader.h.

Referenced by getIgnoreEmptyLines(), preprocessLine(), and setIgnoreEmptyLines().

◆ _names

std::vector<std::string> MooseUtils::DelimitedFileReader::_names
protected

Storage for the read or generated column names.

Definition at line 152 of file DelimitedFileReader.h.

Referenced by getData(), getNames(), read(), readColumnData(), and readRowData().

◆ _row_comment

std::string MooseUtils::DelimitedFileReader::_row_comment
protected

Hide row comments.

Definition at line 167 of file DelimitedFileReader.h.

Referenced by getComment(), preprocessLine(), and setComment().

◆ _row_offsets

std::vector<std::size_t> MooseUtils::DelimitedFileReader::_row_offsets
protected

Row offsets (only used with _format == "rows")

Definition at line 164 of file DelimitedFileReader.h.

Referenced by read(), and readRowData().

◆ INVALID_SIZE

const std::size_t MooseUtils::DelimitedFileReader::INVALID_SIZE = std::numeric_limits<std::size_t>::max()

Definition at line 51 of file DelimitedFileReader.h.

Referenced by readColumnData().


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