https://mooseframework.inl.gov
DelimitedFileReader.h
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://mooseframework.inl.gov
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 
35 template <typename T>
37 {
38 public:
39  enum class HeaderFlag
40  {
41  OFF = 0,
42  ON = 1,
43  AUTO = 2
44  };
45 
46  enum class FormatFlag
47  {
48  COLUMNS = 0,
49  ROWS = 1
50  };
51 
53 
54  DelimitedFileReaderTempl(const std::string & filename,
55  const libMesh::Parallel::Communicator * comm = nullptr);
56 
62  void read();
63 
68  std::size_t numEntries() const;
69 
71 
81  bool getIgnoreEmptyLines() const { return _ignore_empty_lines; }
82 
85 
86  void setDelimiter(const std::string & value) { _delimiter = value; }
87  const std::string & setDelimiter() const { return _delimiter; }
88 
91 
92  void setComment(const std::string & value) { _row_comment = value; }
93  const std::string & getComment() const { return _row_comment; }
95 
98  void setFileName(const std::string & new_file)
99  {
100  _filename = new_file;
101  _names.clear();
102  }
103 
107  const std::vector<std::string> & getNames() const;
108 
114  const std::vector<std::vector<T>> & getData() const;
115 
120  const std::vector<Point> getDataAsPoints() const;
121 
123 
126  const std::vector<T> & getData(const std::string & name) const;
127  const std::vector<T> & getData(std::size_t index) const;
129 
130 protected:
132  std::string _filename;
133 
136 
138  std::string _delimiter;
139 
142 
144  std::vector<std::string> _names;
145 
147  std::vector<std::vector<T>> _data;
148 
151 
154 
156  std::vector<std::size_t> _row_offsets;
157 
159  std::string _row_comment;
160 
161 private:
163 
166  void readColumnData(std::ifstream & stream_data, std::vector<T> & output);
167  void readRowData(std::ifstream & stream_data, std::vector<T> & output);
169 
176  void processLine(const std::string & line, std::vector<T> & row, const unsigned int & num);
177 
184  bool preprocessLine(std::string & line, const unsigned int & num);
185 
192  const std::string & delimiter(const std::string & line);
193 
197  bool header(const std::string & line);
198 };
199 
202 }
DelimitedFileReaderTempl< double > DelimitedFileReader
void setIgnoreEmptyLines(bool value)
Set/Get methods for file format controls.
void setComment(const std::string &value)
const std::vector< Point > getDataAsPoints() const
Get the data in Point format.
DelimitedFileReaderTempl(const std::string &filename, const libMesh::Parallel::Communicator *comm=nullptr)
HeaderFlag _header_flag
Flag indicating if the file contains a header.
void readRowData(std::ifstream &stream_data, std::vector< T > &output)
std::string _filename
The supplied filename.
const libMesh::Parallel::Communicator *const _communicator
Communicator.
void readColumnData(std::ifstream &stream_data, std::vector< T > &output)
Read the numeric data as rows or columns into a single vector.
std::vector< std::string > _names
Storage for the read or generated column names.
std::vector< std::vector< T > > _data
Storage for the read data columns.
auto max(const L &left, const R &right)
void setFileName(const std::string &new_file)
Set the file name, used to change the file to read from We also reset the column/row names as a secon...
Real value(unsigned n, unsigned alpha, unsigned beta, Real x)
std::string _delimiter
The delimiter separating the supplied data entires.
std::size_t numEntries() const
Get the total number of entries in the file.
void read()
Perform the actual data reading.
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 std::vector< std::vector< T > > & getData() const
Return the rows/columns of data.
const std::string & delimiter(const std::string &line)
Determine the delimiter.
const std::string & setDelimiter() const
bool preprocessLine(std::string &line, const unsigned int &num)
Check the content of the line and if it should be skipped.
Utility class for reading delimited data (e.g., CSV data).
bool _ignore_empty_lines
Flag for ignoring empty lines.
void setDelimiter(const std::string &value)
std::string _row_comment
Hide row comments.
const std::string & getComment() const
FormatFlag _format_flag
Format "rows" vs "columns".
void processLine(const std::string &line, std::vector< T > &row, const unsigned int &num)
Populate supplied vector with content from line.
DelimitedFileReaderTempl< std::string > DelimitedFileOfStringReader
const std::vector< std::string > & getNames() const
Return the column/row names.
std::vector< std::size_t > _row_offsets
Row offsets (only used with _format == "rows")