www.mooseframework.org
DelimitedFileReader.h
Go to the documentation of this file.
1 /****************************************************************/
2 /* DO NOT MODIFY THIS HEADER */
3 /* MOOSE - Multiphysics Object Oriented Simulation Environment */
4 /* */
5 /* (c) 2010 Battelle Energy Alliance, LLC */
6 /* ALL RIGHTS RESERVED */
7 /* */
8 /* Prepared by Battelle Energy Alliance, LLC */
9 /* Under Contract No. DE-AC07-05ID14517 */
10 /* With the U. S. Department of Energy */
11 /* */
12 /* See COPYRIGHT for full restrictions */
13 /****************************************************************/
14 
15 #ifndef DELIMITEDFILEREADER_H
16 #define DELIMITEDFILEREADER_H
17 
18 // STL includes
19 #include <vector>
20 #include <string>
21 #include <fstream>
22 
23 #include "libmesh/parallel.h"
24 
25 // MOOSE includes
26 #include "MooseEnum.h"
27 
28 namespace MooseUtils
29 {
30 
41 {
42 public:
43  enum class HeaderFlag
44  {
45  OFF = 0,
46  ON = 1,
47  AUTO = 2
48  };
49 
50  enum class FormatFlag
51  {
52  COLUMNS = 0,
53  ROWS = 1
54  };
55 
56  const std::size_t INVALID_SIZE = std::numeric_limits<std::size_t>::max();
57 
58  DelimitedFileReader(const std::string & filename,
59  const libMesh::Parallel::Communicator * comm = nullptr);
60 
66  void read();
67 
69 
78  void setIgnoreEmptyLines(bool value) { _ignore_empty_lines = value; }
79  bool getIgnoreEmptyLines() const { return _ignore_empty_lines; }
80 
81  void setFormatFlag(FormatFlag value) { _format_flag = value; }
83 
84  void setDelimiter(const std::string & value) { _delimiter = value; }
85  const std::string & setDelimiter() const { return _delimiter; }
86 
87  void setHeaderFlag(HeaderFlag value) { _header_flag = value; }
89 
90  void setComment(const std::string & value) { _row_comment = value; }
91  const std::string & getComment() const { return _row_comment; }
93 
97  const std::vector<std::string> & getNames() const;
98 
104  const std::vector<std::vector<double>> & getData() const;
105 
107 
110  const std::vector<double> & getData(const std::string & name) const;
111  const std::vector<double> & getData(std::size_t index) const;
113 
115 
118  void setHeaderFlag(bool value);
119  const std::vector<std::string> & getColumnNames() const;
120  const std::vector<std::vector<double>> & getColumnData() const;
121  const std::vector<double> & getColumnData(const std::string & name) const;
122  DelimitedFileReader(const std::string & filename,
123  const bool header,
124  const std::string delimiter,
125  const libMesh::Parallel::Communicator * comm = nullptr);
127 
128 protected:
130  const std::string _filename;
131 
134 
136  std::string _delimiter;
137 
140 
142  std::vector<std::string> _names;
143 
145  std::vector<std::vector<double>> _data;
146 
148  const libMesh::Parallel::Communicator * _communicator;
149 
152 
154  std::vector<std::size_t> _row_offsets;
155 
157  std::string _row_comment;
158 
159 private:
161 
164  void readColumnData(std::ifstream & stream_data, std::vector<double> & output);
165  void readRowData(std::ifstream & stream_data, std::vector<double> & output);
167 
174  void processLine(const std::string & line, std::vector<double> & row, const unsigned int & num);
175 
182  bool preprocessLine(std::string & line, const unsigned int & num);
183 
190  const std::string & delimiter(const std::string & line);
191 
195  bool header(const std::string & line);
196 };
197 }
198 
199 #endif
std::vector< std::vector< double > > _data
Storage for the read data columns.
void setHeaderFlag(HeaderFlag value)
void read()
Perform the actual data reading.
std::string _delimiter
The delimiter separating the supplied data entires.
HeaderFlag _header_flag
Flag indicating if the file contains a header.
Utility class for reading delimited data (e.g., CSV data).
void setComment(const std::string &value)
bool _ignore_empty_lines
Flag for ignoring empty lines.
bool preprocessLine(std::string &line, const unsigned int &num)
Check the content of the line and if it should be skipped.
const std::vector< std::string > & getNames() const
Return the column/row names.
std::vector< std::string > _names
Storage for the read or generated column names.
const std::vector< std::vector< double > > & getData() const
Return the rows/columns of data.
FormatFlag _format_flag
Format "rows" vs "columns".
const std::vector< std::string > & getColumnNames() const
void setIgnoreEmptyLines(bool value)
Set/Get methods for file format controls.
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...
const libMesh::Parallel::Communicator * _communicator
Communicator.
const std::string & getComment() const
void readColumnData(std::ifstream &stream_data, std::vector< double > &output)
Read the numeric data as rows or columns into a single vector.
DelimitedFileReader(const std::string &filename, const libMesh::Parallel::Communicator *comm=nullptr)
void setDelimiter(const std::string &value)
void setFormatFlag(FormatFlag value)
const std::string _filename
The supplied filename.
void readRowData(std::ifstream &stream_data, std::vector< double > &output)
MPI_Comm comm
std::string _row_comment
Hide row comments.
const std::string & delimiter(const std::string &line)
Determine the delimiter.
const std::string & setDelimiter() const
const std::vector< std::vector< double > > & getColumnData() const
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.