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) |
Utility class for reading delimited data (e.g., CSV data).
filename | A string for the filename to read. |
comm | A 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.
|
strong |
Enumerator | |
---|---|
COLUMNS | |
ROWS |
Definition at line 45 of file DelimitedFileReader.h.
|
strong |
Enumerator | |
---|---|
OFF | |
ON | |
AUTO |
Definition at line 38 of file DelimitedFileReader.h.
MooseUtils::DelimitedFileReader::DelimitedFileReader | ( | const std::string & | filename, |
const libMesh::Parallel::Communicator * | comm = nullptr |
||
) |
Definition at line 24 of file DelimitedFileReader.C.
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.
|
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().
const std::vector< std::vector< double > > & MooseUtils::DelimitedFileReader::getColumnData | ( | ) | const |
Definition at line 420 of file DelimitedFileReader.C.
const std::vector< double > & MooseUtils::DelimitedFileReader::getColumnData | ( | const std::string & | name | ) | const |
Definition at line 427 of file DelimitedFileReader.C.
const std::vector< std::string > & MooseUtils::DelimitedFileReader::getColumnNames | ( | ) | const |
Definition at line 413 of file DelimitedFileReader.C.
|
inline |
Definition at line 92 of file DelimitedFileReader.h.
const std::vector< std::vector< double > > & MooseUtils::DelimitedFileReader::getData | ( | ) | const |
Return the rows/columns of data.
The outer vector is column and the inner the rows.
Definition at line 140 of file DelimitedFileReader.C.
Referenced by PiecewiseTabularBase::buildFromFile(), CSVFileTimes::CSVFileTimes(), CSVReaderVectorPostprocessor::CSVReaderVectorPostprocessor(), PropertyReadFile::getBlockData(), getColumnData(), PropertyReadFile::getElementData(), PropertyReadFile::getNodeData(), PropertyReadFile::getVoronoiData(), CSVTimeSequenceStepper::init(), PropertyReadFile::initVoronoiCenterPoints(), and PropertyReadFile::readData().
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.
const std::vector< double > & MooseUtils::DelimitedFileReader::getData | ( | std::size_t | index | ) | const |
Definition at line 186 of file DelimitedFileReader.C.
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().
|
inline |
Definition at line 83 of file DelimitedFileReader.h.
|
inline |
Definition at line 89 of file DelimitedFileReader.h.
|
inline |
Definition at line 80 of file DelimitedFileReader.h.
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().
|
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().
std::size_t MooseUtils::DelimitedFileReader::numEntries | ( | ) | const |
Get the total number of entries in the file.
Definition at line 124 of file DelimitedFileReader.C.
|
private |
Check the content of the line and if it should be skipped.
line | Complete line being read. |
num | The current line number. |
Definition at line 323 of file DelimitedFileReader.C.
Referenced by readColumnData(), and readRowData().
|
private |
Populate supplied vector with content from line.
line | The line to extract data from. |
row | The vector to populate. |
num | The current line number. |
Definition at line 341 of file DelimitedFileReader.C.
Referenced by readColumnData(), and readRowData().
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().
|
private |
Read the numeric data as rows or columns into a single vector.
Definition at line 200 of file DelimitedFileReader.C.
Referenced by read().
|
private |
Definition at line 270 of file DelimitedFileReader.C.
Referenced by read().
|
inline |
Definition at line 91 of file DelimitedFileReader.h.
Referenced by PiecewiseTabularBase::buildFromFile().
|
inline |
Definition at line 85 of file DelimitedFileReader.h.
Referenced by CSVReaderVectorPostprocessor::CSVReaderVectorPostprocessor(), and CSVTimeSequenceStepper::init().
|
inline |
Definition at line 86 of file DelimitedFileReader.h.
|
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().
|
inline |
Definition at line 82 of file DelimitedFileReader.h.
Referenced by PiecewiseTabularBase::buildFromFile(), CSVFileTimes::CSVFileTimes(), FilePositions::FilePositions(), CombinerGenerator::fillPositions(), MultiApp::fillPositions(), and PropertyReadFile::readData().
|
inline |
Definition at line 88 of file DelimitedFileReader.h.
Referenced by CSVReaderVectorPostprocessor::CSVReaderVectorPostprocessor(), and CSVTimeSequenceStepper::init().
void MooseUtils::DelimitedFileReader::setHeaderFlag | ( | bool | value | ) |
Deprecated.
Definition at line 434 of file DelimitedFileReader.C.
|
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().
|
protected |
|
protected |
Storage for the read data columns.
Definition at line 155 of file DelimitedFileReader.h.
Referenced by getData(), getDataAsPoints(), numEntries(), and read().
|
protected |
The delimiter separating the supplied data entires.
Definition at line 146 of file DelimitedFileReader.h.
Referenced by delimiter(), and setDelimiter().
|
protected |
The supplied filename.
Definition at line 140 of file DelimitedFileReader.h.
Referenced by getData(), getDataAsPoints(), preprocessLine(), processLine(), read(), readColumnData(), and setFileName().
|
protected |
Format "rows" vs "columns".
Definition at line 161 of file DelimitedFileReader.h.
Referenced by getFormatFlag(), read(), and setFormatFlag().
|
protected |
Flag indicating if the file contains a header.
Definition at line 143 of file DelimitedFileReader.h.
Referenced by getHeaderFlag(), header(), and setHeaderFlag().
|
protected |
Flag for ignoring empty lines.
Definition at line 149 of file DelimitedFileReader.h.
Referenced by getIgnoreEmptyLines(), preprocessLine(), and setIgnoreEmptyLines().
|
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().
|
protected |
Hide row comments.
Definition at line 167 of file DelimitedFileReader.h.
Referenced by getComment(), preprocessLine(), and setComment().
|
protected |
Row offsets (only used with _format == "rows")
Definition at line 164 of file DelimitedFileReader.h.
Referenced by read(), and readRowData().
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().