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 | |
DelimitedFileReaderTempl (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 We also reset the column/row names as a second read might have different names. More... | |
const std::vector< std::string > & | getNames () const |
Return the column/row names. More... | |
const std::vector< std::vector< T > > & | getData () const |
Return the rows/columns of data. More... | |
const std::vector< Point > | getDataAsPoints () const |
Get the data in Point format. More... | |
template<> | |
const std::vector< Point > | getDataAsPoints () const |
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< T > & | getData (const std::string &name) const |
Return the row/column of data for a specified header entry. More... | |
const std::vector< T > & | getData (std::size_t index) const |
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< T > > | _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< T > &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< T > &output) |
Read the numeric data as rows or columns into a single vector. More... | |
void | readRowData (std::ifstream &stream_data, std::vector< T > &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 36 of file DelimitedFileReader.h.
|
strong |
Enumerator | |
---|---|
COLUMNS | |
ROWS |
Definition at line 46 of file DelimitedFileReader.h.
|
strong |
Enumerator | |
---|---|
OFF | |
ON | |
AUTO |
Definition at line 39 of file DelimitedFileReader.h.
MooseUtils::DelimitedFileReaderTempl< T >::DelimitedFileReaderTempl | ( | const std::string & | filename, |
const libMesh::Parallel::Communicator * | comm = nullptr |
||
) |
Definition at line 26 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 389 of file DelimitedFileReader.C.
|
inline |
Definition at line 93 of file DelimitedFileReader.h.
const std::vector< std::vector< T > > & MooseUtils::DelimitedFileReaderTempl< T >::getData | ( | ) | const |
Return the rows/columns of data.
The outer vector is column and the inner the rows.
Definition at line 146 of file DelimitedFileReader.C.
Referenced by TimedSubdomainModifier::buildFromFile(), PiecewiseTabularBase::buildFromFile(), CSVFileTimes::CSVFileTimes(), CSVReaderVectorPostprocessor::CSVReaderVectorPostprocessor(), PropertyReadFile::getBlockData(), PropertyReadFile::getElementData(), PropertyReadFile::getFileNames(), PropertyReadFile::getNodeData(), PropertyReadFile::getVoronoiData(), CSVTimeSequenceStepper::init(), PropertyReadFile::initVoronoiCenterPoints(), and PropertyReadFile::readData().
const std::vector< T > & MooseUtils::DelimitedFileReaderTempl< T >::getData | ( | const std::string & | name | ) | const |
Return the row/column of data for a specified header entry.
Definition at line 192 of file DelimitedFileReader.C.
const std::vector< T > & MooseUtils::DelimitedFileReaderTempl< T >::getData | ( | std::size_t | index | ) | const |
Definition at line 202 of file DelimitedFileReader.C.
const std::vector< Point > MooseUtils::DelimitedFileReaderTempl< T >::getDataAsPoints | ( | ) | const |
Get the data in Point format.
This performs checks that the data is of valid dimensions to do so.
Definition at line 185 of file DelimitedFileReader.C.
Referenced by FilePositions::FilePositions(), CombinerGenerator::fillPositions(), MultiApp::fillPositions(), and XYDelaunayGenerator::XYDelaunayGenerator().
const std::vector< Point > MooseUtils::DelimitedFileReaderTempl< double >::getDataAsPoints | ( | ) | const |
Definition at line 153 of file DelimitedFileReader.C.
|
inline |
Definition at line 84 of file DelimitedFileReader.h.
|
inline |
Definition at line 90 of file DelimitedFileReader.h.
|
inline |
Definition at line 81 of file DelimitedFileReader.h.
const std::vector< std::string > & MooseUtils::DelimitedFileReaderTempl< T >::getNames | ( | ) | const |
Return the column/row names.
Definition at line 139 of file DelimitedFileReader.C.
Referenced by TimedSubdomainModifier::buildFromFile(), PiecewiseTabularBase::buildFromFile(), and CSVReaderVectorPostprocessor::CSVReaderVectorPostprocessor().
|
private |
Return the header flag, if it is set to AUTO attempt to determine if a header exists in line.
Definition at line 405 of file DelimitedFileReader.C.
std::size_t MooseUtils::DelimitedFileReaderTempl< T >::numEntries | ( | ) | const |
Get the total number of entries in the file.
Definition at line 128 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 342 of file DelimitedFileReader.C.
|
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 361 of file DelimitedFileReader.C.
void MooseUtils::DelimitedFileReaderTempl< T >::read | ( | ) |
Perform the actual data reading.
This is a separate method to allow for the filename to be read multiple times.
Definition at line 38 of file DelimitedFileReader.C.
Referenced by TimedSubdomainModifier::buildFromFile(), PiecewiseTabularBase::buildFromFile(), CSVFileTimes::CSVFileTimes(), CSVReaderVectorPostprocessor::CSVReaderVectorPostprocessor(), FilePositions::FilePositions(), CombinerGenerator::fillPositions(), MultiApp::fillPositions(), PropertyReadFile::getFileNames(), CSVTimeSequenceStepper::init(), PropertyReadFile::readData(), and XYDelaunayGenerator::XYDelaunayGenerator().
|
private |
Read the numeric data as rows or columns into a single vector.
Definition at line 217 of file DelimitedFileReader.C.
|
private |
Definition at line 288 of file DelimitedFileReader.C.
|
inline |
Definition at line 92 of file DelimitedFileReader.h.
Referenced by TimedSubdomainModifier::buildFromFile(), and PiecewiseTabularBase::buildFromFile().
|
inline |
Definition at line 86 of file DelimitedFileReader.h.
Referenced by TimedSubdomainModifier::buildFromFile(), CSVReaderVectorPostprocessor::CSVReaderVectorPostprocessor(), and CSVTimeSequenceStepper::init().
|
inline |
Definition at line 87 of file DelimitedFileReader.h.
|
inline |
Set the file name, used to change the file to read from We also reset the column/row names as a second read might have different names.
Definition at line 98 of file DelimitedFileReader.h.
Referenced by PropertyReadFile::initialize().
|
inline |
Definition at line 83 of file DelimitedFileReader.h.
Referenced by PiecewiseTabularBase::buildFromFile(), CSVFileTimes::CSVFileTimes(), FilePositions::FilePositions(), CombinerGenerator::fillPositions(), MultiApp::fillPositions(), PropertyReadFile::readData(), and XYDelaunayGenerator::XYDelaunayGenerator().
|
inline |
Definition at line 89 of file DelimitedFileReader.h.
Referenced by TimedSubdomainModifier::buildFromFile(), CSVReaderVectorPostprocessor::CSVReaderVectorPostprocessor(), PropertyReadFile::getFileNames(), and CSVTimeSequenceStepper::init().
|
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 80 of file DelimitedFileReader.h.
Referenced by CSVReaderVectorPostprocessor::CSVReaderVectorPostprocessor().
|
protected |
Communicator.
Definition at line 150 of file DelimitedFileReader.h.
|
protected |
Storage for the read data columns.
Definition at line 147 of file DelimitedFileReader.h.
|
protected |
The delimiter separating the supplied data entires.
Definition at line 138 of file DelimitedFileReader.h.
Referenced by MooseUtils::DelimitedFileReaderTempl< T >::setDelimiter().
|
protected |
The supplied filename.
Definition at line 132 of file DelimitedFileReader.h.
Referenced by MooseUtils::DelimitedFileReaderTempl< T >::setFileName().
|
protected |
Format "rows" vs "columns".
Definition at line 153 of file DelimitedFileReader.h.
Referenced by MooseUtils::DelimitedFileReaderTempl< T >::getFormatFlag(), and MooseUtils::DelimitedFileReaderTempl< T >::setFormatFlag().
|
protected |
Flag indicating if the file contains a header.
Definition at line 135 of file DelimitedFileReader.h.
Referenced by MooseUtils::DelimitedFileReaderTempl< T >::getHeaderFlag(), and MooseUtils::DelimitedFileReaderTempl< T >::setHeaderFlag().
|
protected |
Flag for ignoring empty lines.
Definition at line 141 of file DelimitedFileReader.h.
Referenced by MooseUtils::DelimitedFileReaderTempl< T >::getIgnoreEmptyLines(), and MooseUtils::DelimitedFileReaderTempl< T >::setIgnoreEmptyLines().
|
protected |
Storage for the read or generated column names.
Definition at line 144 of file DelimitedFileReader.h.
Referenced by MooseUtils::DelimitedFileReaderTempl< T >::setFileName().
|
protected |
Hide row comments.
Definition at line 159 of file DelimitedFileReader.h.
Referenced by MooseUtils::DelimitedFileReaderTempl< T >::getComment(), and MooseUtils::DelimitedFileReaderTempl< T >::setComment().
|
protected |
Row offsets (only used with _format == "rows")
Definition at line 156 of file DelimitedFileReader.h.
const std::size_t MooseUtils::DelimitedFileReaderTempl< T >::INVALID_SIZE = std::numeric_limits<std::size_t>::max() |
Definition at line 52 of file DelimitedFileReader.h.