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 #include "MooseTypes.h"
22 
23 namespace MooseUtils
24 {
25 
36 {
37 public:
38  enum class HeaderFlag
39  {
40  OFF = 0,
41  ON = 1,
42  AUTO = 2
43  };
44 
45  enum class FormatFlag
46  {
47  COLUMNS = 0,
48  ROWS = 1
49  };
50 
52 
53  DelimitedFileReader(const std::string & filename,
54  const libMesh::Parallel::Communicator * comm = nullptr);
55 
61  void read();
62 
67  std::size_t numEntries() const;
68 
70 
80  bool getIgnoreEmptyLines() const { return _ignore_empty_lines; }
81 
84 
85  void setDelimiter(const std::string & value) { _delimiter = value; }
86  const std::string & setDelimiter() const { return _delimiter; }
87 
90 
91  void setComment(const std::string & value) { _row_comment = value; }
92  const std::string & getComment() const { return _row_comment; }
94 
96  void setFileName(const std::string & new_file) { _filename = new_file; }
97 
101  const std::vector<std::string> & getNames() const;
102 
108  const std::vector<std::vector<double>> & getData() const;
109 
114  const std::vector<Point> getDataAsPoints() const;
115 
117 
120  const std::vector<double> & getData(const std::string & name) const;
121  const std::vector<double> & getData(std::size_t index) const;
123 
125 
128  void setHeaderFlag(bool value);
129  const std::vector<std::string> & getColumnNames() const;
130  const std::vector<std::vector<double>> & getColumnData() const;
131  const std::vector<double> & getColumnData(const std::string & name) const;
132  DelimitedFileReader(const std::string & filename,
133  const bool header,
134  const std::string delimiter,
135  const libMesh::Parallel::Communicator * comm = nullptr);
137 
138 protected:
140  std::string _filename;
141 
144 
146  std::string _delimiter;
147 
150 
152  std::vector<std::string> _names;
153 
155  std::vector<std::vector<double>> _data;
156 
159 
162 
164  std::vector<std::size_t> _row_offsets;
165 
167  std::string _row_comment;
168 
169 private:
171 
174  void readColumnData(std::ifstream & stream_data, std::vector<double> & output);
175  void readRowData(std::ifstream & stream_data, std::vector<double> & output);
177 
184  void processLine(const std::string & line, std::vector<double> & row, const unsigned int & num);
185 
192  bool preprocessLine(std::string & line, const unsigned int & num);
193 
200  const std::string & delimiter(const std::string & line);
201 
205  bool header(const std::string & line);
206 };
207 }
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)
std::size_t numEntries() const
Get the total number of entries in the file.
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.
auto max(const L &left, const R &right)
const std::vector< Point > getDataAsPoints() const
Get the data in Point format.
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...
void readColumnData(std::ifstream &stream_data, std::vector< double > &output)
Read the numeric data as rows or columns into a single vector.
Real value(unsigned n, unsigned alpha, unsigned beta, Real x)
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)
void setFileName(const std::string &new_file)
Set the file name, used to change the file to read from.
void readRowData(std::ifstream &stream_data, std::vector< double > &output)
std::string _filename
The supplied filename.
const std::vector< std::string > & getNames() const
Return the column/row names.
const libMesh::Parallel::Communicator *const _communicator
Communicator.
const std::string & getComment() const
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.