csvReader Class Reference

Enables reading of .csv files into MercuryDPM. More...

#include <csvReader.h>

Public Member Functions

 csvReader ()
 Constructor. More...
 
void read (const std::string &filename)
 Reads .csv files into Mercury. More...
 
std::vector< std::string > getHeaderVector ()
 Get the Header string vector of a .csv file. More...
 
void setHeader (bool headings)
 Set the boolean hasHeader_. More...
 
std::vector< std::vector< std::string > > getNumArray ()
 Get the 2D array with the .csv file values. More...
 
std::vector< MdoublegetFirstColumn (Mdouble scalingFactor)
 Get first column of a .csv file and return it as a double. More...
 
std::vector< MdoublegetSecondColumn (Mdouble scalingFactor)
 Get second column of a .csv file and return it as a double. More...
 

Private Attributes

std::vector< std::vector< std::string > > numArray_
 
std::vector< std::string > headerVector_
 
std::vector< MdoubleCSVFirstColumn_
 
std::vector< MdoubleCSVSecondColumn_
 
bool hasHeader_ = false
 

Detailed Description

Enables reading of .csv files into MercuryDPM.

the csvReader stores a 2D array of strings from a comma-separated value (.csv) file. In this way any data type from a .csv file can be read into Mercury and converted into other formats.

Constructor & Destructor Documentation

◆ csvReader()

csvReader::csvReader ( )
default

Constructor.

Default constructor; sets every data member to 0 or default.

Member Function Documentation

◆ getFirstColumn()

std::vector< Mdouble > csvReader::getFirstColumn ( Mdouble  scalingFactor)

Get first column of a .csv file and return it as a double.

Gets the first column of the .csv file as a vector of doubles. It extracts the first column from the 2D array and converts the vector of strings into a vector of doubles.

Returns
A vector of doubles containing the values of the .csv file's first column.
108 {
109  for (int i = 0; i < numArray_.size(); i++)
110  {
111 
112  // converts vector<string> to vector<double>
113  // if std::stod throws an exception of an invalid argument there might be an encoding issue.
114  CSVFirstColumn_.push_back(std::stod(numArray_[i][0].c_str()) / scalingFactor);
115  }
116  return CSVFirstColumn_;
117 }
int i
Definition: BiCGSTAB_step_by_step.cpp:9
std::vector< std::vector< std::string > > numArray_
Definition: csvReader.h:68
std::vector< Mdouble > CSVFirstColumn_
Definition: csvReader.h:79

References CSVFirstColumn_, i, and numArray_.

Referenced by PSD::setPSDFromCSV().

◆ getHeaderVector()

std::vector< std::string > csvReader::getHeaderVector ( )

Get the Header string vector of a .csv file.

Gets the headerVector_ containing the values of the .csv file's first row.

Returns
A vector of strings containing the header values.
81 {
82  return headerVector_;
83 }
std::vector< std::string > headerVector_
Definition: csvReader.h:74

References headerVector_.

◆ getNumArray()

std::vector< std::vector< std::string > > csvReader::getNumArray ( )

Get the 2D array with the .csv file values.

Gets the 2D array of strings which contains all the .csv file values from the read() function.

Returns
A 2D array of strings containing all the .csv file values.
98 {
99  return numArray_;
100 }

References numArray_.

◆ getSecondColumn()

std::vector< Mdouble > csvReader::getSecondColumn ( Mdouble  scalingFactor)

Get second column of a .csv file and return it as a double.

Gets the second column of the .csv file as a vector of doubles. It extracts the second column from the 2D array and converts the vector of strings into a vector of doubles.

Returns
A vector of doubles containing the values of the .csv file's second column.
125 {
126  for (int i = 0; i < numArray_.size(); i++)
127  {
128  // converts vector<string> to vector<double>
129  // if std::stod throws an exception of an invalid argument there might be an encoding issue.
130  CSVSecondColumn_.push_back(std::stod(numArray_[i][1].c_str()) / scalingFactor);
131  }
132  return CSVSecondColumn_;
133 }
std::vector< Mdouble > CSVSecondColumn_
Definition: csvReader.h:84

References CSVSecondColumn_, i, and numArray_.

Referenced by PSD::setPSDFromCSV().

◆ read()

void csvReader::read ( const std::string &  filename)

Reads .csv files into Mercury.

Read function that divides each line of the .csv file into comma delimited fields which get stored in the numArray_. Further it checks for a byte order mark (BOM) and skips them if existent to avoid errors. If the hasHeader_ is set to TRUE the first row of the .csv file will be skipped.

Parameters
[in]filenameName of the .csv file.
20 {
21  // strings to catch each line of a file and separate it into fields
22  std::string line, field;
23  // array of values for one line only
24  std::vector<std::string> v;
25  // Create ifstream object to open the file
26  std::ifstream file;
27  file.open(filename);
28  if (file.is_open())
29  {
30  // get first three bytes of the ifstream object (of your file)
31  int ch1 = file.get();
32  int ch2 = file.get();
33  int ch3 = file.get();
34  // if TRUE, the file contains UTF-8 BOM (problem usually encountered on Windows OS). A BOM is a mark which
35  // distinguishes different file encodings from each other. They usually start with a combination of different
36  // bytes (for UTF-8-BOM it is \uFEFF, but asking for it does not work, so it is seperated into three different
37  // bytes). Another workaround is changing the encoding to UTF-8 without BOM which some readers are capable of.
38  if (ch1 == 0xEF && ch2 == 0xBB && ch3 == 0xBF)
39  {
40  // skip first three bytes if BOM exists
41  file.seekg(3);
42  // drop a log message to tell the user that he has a byte-order-mark
43  logger(INFO, "Your file contains a byte-order-mark. The first three bytes of this file will be skipped");
44  }
45  else
46  {
47  // ensure that the ifstream object starts at the first byte of the csv file
48  file.seekg(0);
49  }
50  while (getline(file, line)) // get next line in file
51  {
52  v.clear();
53  std::stringstream ss(line);
54  while (getline(ss, field, ','))
55  {
56  // break line into comma delimitted fields
57  v.push_back(field); // add each field to the 1D array
58  }
59  if (hasHeader_)
60  {
61  headerVector_ = v; // add the 1D array to the 2D array
62  hasHeader_ = false;
63  }
64  else
65  {
66  numArray_.push_back(v); // add the 1D array to the 2D array
67  }
68  }
69  }
70  else
71  {
72  logger(ERROR, "File could not be opened");
73  }
74 }
Array< int, Dynamic, 1 > v
Definition: Array_initializer_list_vector_cxx11.cpp:1
Logger< MERCURYDPM_LOGLEVEL > logger("MercuryKernel")
Definition of different loggers with certain modules. A user can define its own custom logger here.
@ INFO
@ ERROR
bool hasHeader_
Definition: csvReader.h:89
string filename
Definition: MergeRestartFiles.py:39
line
Definition: calibrate.py:103
std::string string(const unsigned &i)
Definition: oomph_definitions.cc:286

References ERROR, MergeRestartFiles::filename, hasHeader_, headerVector_, INFO, calibrate::line, logger, numArray_, oomph::Global_string_for_annotation::string(), and v.

Referenced by PSD::setPSDFromCSV().

◆ setHeader()

void csvReader::setHeader ( bool  hasHeader)

Set the boolean hasHeader_.

Sets the boolean hasHeader_ which determines if the .csv file has headings.

89 {
90  hasHeader_ = hasHeader;
91 }

References hasHeader_.

Referenced by PSD::setPSDFromCSV().

Member Data Documentation

◆ CSVFirstColumn_

std::vector<Mdouble> csvReader::CSVFirstColumn_
private

vector of doubles containing all values of the .csv file's first column.

Referenced by getFirstColumn().

◆ CSVSecondColumn_

std::vector<Mdouble> csvReader::CSVSecondColumn_
private

vector of doubles containing all values of the .csv file's first column.

Referenced by getSecondColumn().

◆ hasHeader_

bool csvReader::hasHeader_ = false
private

if FALSE the file has no headers and if TRUE the file has headers and the first row will be skipped.

Referenced by read(), and setHeader().

◆ headerVector_

std::vector<std::string> csvReader::headerVector_
private

vector of strings containing possible header values of the .csv file's first row. This vector is only filled when headerFlag is set to TRUE.

Referenced by getHeaderVector(), and read().

◆ numArray_

std::vector<std::vector<std::string> > csvReader::numArray_
private

2D array containing all .csv file values

Referenced by getFirstColumn(), getNumArray(), getSecondColumn(), and read().


The documentation for this class was generated from the following files: