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.
129 {
130  for (int i = 0; i < numArray_.size(); i++)
131  {
132 
133  // converts vector<string> to vector<double>
134  // if std::stod throws an exception of an invalid argument there might be an encoding issue.
135  CSVFirstColumn_.push_back(std::stod(numArray_[i][0].c_str()) / scalingFactor);
136  }
137  return CSVFirstColumn_;
138 }
std::vector< std::vector< std::string > > numArray_
Definition: csvReader.h:89
std::vector< Mdouble > CSVFirstColumn_
Definition: csvReader.h:100
const std::complex< Mdouble > i
Definition: ExtendedMath.h:51

References CSVFirstColumn_, constants::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.
102 {
103  return headerVector_;
104 }
std::vector< std::string > headerVector_
Definition: csvReader.h:95

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.
119 {
120  return numArray_;
121 }

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.
146 {
147  for (int i = 0; i < numArray_.size(); i++)
148  {
149  // converts vector<string> to vector<double>
150  // if std::stod throws an exception of an invalid argument there might be an encoding issue.
151  CSVSecondColumn_.push_back(std::stod(numArray_[i][1].c_str()) / scalingFactor);
152  }
153  return CSVSecondColumn_;
154 }
std::vector< Mdouble > CSVSecondColumn_
Definition: csvReader.h:105

References CSVSecondColumn_, constants::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.
41 {
42  // strings to catch each line of a file and separate it into fields
43  std::string line, field;
44  // array of values for one line only
45  std::vector<std::string> v;
46  // Create ifstream object to open the file
47  std::ifstream file;
48  file.open(filename);
49  if (file.is_open())
50  {
51  // get first three bytes of the ifstream object (of your file)
52  int ch1 = file.get();
53  int ch2 = file.get();
54  int ch3 = file.get();
55  // if TRUE, the file contains UTF-8 BOM (problem usually encountered on Windows OS). A BOM is a mark which
56  // distinguishes different file encodings from each other. They usually start with a combination of different
57  // bytes (for UTF-8-BOM it is \uFEFF, but asking for it does not work, so it is seperated into three different
58  // bytes). Another workaround is changing the encoding to UTF-8 without BOM which some readers are capable of.
59  if (ch1 == 0xEF && ch2 == 0xBB && ch3 == 0xBF)
60  {
61  // skip first three bytes if BOM exists
62  file.seekg(3);
63  // drop a log message to tell the user that he has a byte-order-mark
64  logger(INFO, "Your file contains a byte-order-mark. The first three bytes of this file will be skipped");
65  }
66  else
67  {
68  // ensure that the ifstream object starts at the first byte of the csv file
69  file.seekg(0);
70  }
71  while (getline(file, line)) // get next line in file
72  {
73  v.clear();
74  std::stringstream ss(line);
75  while (getline(ss, field, ','))
76  {
77  // break line into comma delimitted fields
78  v.push_back(field); // add each field to the 1D array
79  }
80  if (hasHeader_)
81  {
82  headerVector_ = v; // add the 1D array to the 2D array
83  hasHeader_ = false;
84  }
85  else
86  {
87  numArray_.push_back(v); // add the 1D array to the 2D array
88  }
89  }
90  }
91  else
92  {
93  logger(ERROR, "File could not be opened");
94  }
95 }
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:110

References ERROR, hasHeader_, headerVector_, INFO, logger, and numArray_.

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.

110 {
111  hasHeader_ = hasHeader;
112 }

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: