www.mooseframework.org
DelimitedFileReader.h
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://www.mooseframework.org
3 //*
4 //* All rights reserved, see COPYRIGHT for full restrictions
5 //* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6 //*
7 //* Licensed under LGPL 2.1, please see LICENSE for details
8 //* https://www.gnu.org/licenses/lgpl-2.1.html
9 
10 #pragma once
11 
12 // STL includes
13 #include <vector>
14 #include <string>
15 #include <fstream>
16 
17 #include "libmesh/parallel.h"
18 
19 // MOOSE includes
20 #include "MooseEnum.h"
21 
22 namespace MooseUtils
23 {
24 
35 {
36 public:
37  enum class HeaderFlag
38  {
39  OFF = 0,
40  ON = 1,
41  AUTO = 2
42  };
43 
44  enum class FormatFlag
45  {
46  COLUMNS = 0,
47  ROWS = 1
48  };
49 
50  const std::size_t INVALID_SIZE = std::numeric_limits<std::size_t>::max();
51 
52  DelimitedFileReader(const std::string & filename,
53  const libMesh::Parallel::Communicator * comm = nullptr);
54 
60  void read();
61 
63 
72  void setIgnoreEmptyLines(bool value) { _ignore_empty_lines = value; }
73  bool getIgnoreEmptyLines() const { return _ignore_empty_lines; }
74 
75  void setFormatFlag(FormatFlag value) { _format_flag = value; }
77 
78  void setDelimiter(const std::string & value) { _delimiter = value; }
79  const std::string & setDelimiter() const { return _delimiter; }
80 
81  void setHeaderFlag(HeaderFlag value) { _header_flag = value; }
83 
84  void setComment(const std::string & value) { _row_comment = value; }
85  const std::string & getComment() const { return _row_comment; }
87 
91  const std::vector<std::string> & getNames() const;
92 
98  const std::vector<std::vector<double>> & getData() const;
99 
101 
104  const std::vector<double> & getData(const std::string & name) const;
105  const std::vector<double> & getData(std::size_t index) const;
107 
109 
112  void setHeaderFlag(bool value);
113  const std::vector<std::string> & getColumnNames() const;
114  const std::vector<std::vector<double>> & getColumnData() const;
115  const std::vector<double> & getColumnData(const std::string & name) const;
116  DelimitedFileReader(const std::string & filename,
117  const bool header,
118  const std::string delimiter,
119  const libMesh::Parallel::Communicator * comm = nullptr);
121 
122 protected:
124  const std::string _filename;
125 
128 
130  std::string _delimiter;
131 
134 
136  std::vector<std::string> _names;
137 
139  std::vector<std::vector<double>> _data;
140 
142  const libMesh::Parallel::Communicator * _communicator;
143 
146 
148  std::vector<std::size_t> _row_offsets;
149 
151  std::string _row_comment;
152 
153 private:
155 
158  void readColumnData(std::ifstream & stream_data, std::vector<double> & output);
159  void readRowData(std::ifstream & stream_data, std::vector<double> & output);
161 
168  void processLine(const std::string & line, std::vector<double> & row, const unsigned int & num);
169 
176  bool preprocessLine(std::string & line, const unsigned int & num);
177 
184  const std::string & delimiter(const std::string & line);
185 
189  bool header(const std::string & line);
190 };
191 }
192 
std::vector< std::vector< double > > _data
Storage for the read data columns.
void setHeaderFlag(HeaderFlag value)
void read()
Perform the actual data reading.
const std::vector< std::vector< double > > & getColumnData() const
std::string _delimiter
The delimiter separating the supplied data entires.
HeaderFlag _header_flag
Flag indicating if the file contains a header.
const std::vector< std::vector< double > > & getData() const
Return the rows/columns of data.
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.
std::vector< std::string > _names
Storage for the read or generated column names.
FormatFlag _format_flag
Format "rows" vs "columns".
void setIgnoreEmptyLines(bool value)
Set/Get methods for file format controls.
const std::string & setDelimiter() const
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.
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)
const std::vector< std::string > & getColumnNames() const
void setFormatFlag(FormatFlag value)
const std::string _filename
The supplied filename.
void readRowData(std::ifstream &stream_data, std::vector< double > &output)
const std::vector< std::string > & getNames() const
Return the column/row names.
const std::string & getComment() const
MPI_Comm comm
std::string _row_comment
Hide row comments.
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.