queso-0.53.0
Private Member Functions | Private Attributes | List of all members
QUESO::AsciiTable< V, M > Class Template Reference

Class for reading ASCII values from a table in a file. More...

#include <AsciiTable.h>

Collaboration diagram for QUESO::AsciiTable< V, M >:
Collaboration graph
[legend]

Public Member Functions

Constructor/Destructor methods
 AsciiTable (const BaseEnvironment &env, unsigned int numRows, unsigned int numExtraCols, const std::vector< bool > *extraColIsString, const std::string &fileName)
 Default constructor. More...
 
 ~AsciiTable ()
 Destructor. More...
 
Property methods
unsigned int numRows () const
 Returns the number of rows in the table. More...
 
unsigned int numCols () const
 Returns the number of columns in the table. More...
 
const DistArray< std::string > & stringColumn (unsigned int j) const
 Returns the string stored in column j. More...
 
const V & doubleColumn (unsigned int j) const
 Returns the value (double) stored in column j. More...
 
I/O methods
void print (std::ostream &os) const
 Prints the table. More...
 

Private Member Functions

MapnewMap ()
 
void readColumnsFromFile ()
 
template<>
MapnewMap ()
 

Private Attributes

const BaseEnvironmentm_env
 
unsigned int m_numRows
 
unsigned int m_numCols
 
std::vector< bool > m_colIsString
 
std::string m_fileName
 
const Mapm_map
 
std::vector< DistArray
< std::string > * > 
m_stringColumns
 
std::vector< V * > m_doubleColumns
 

Detailed Description

template<class V = GslVector, class M = GslMatrix>
class QUESO::AsciiTable< V, M >

Class for reading ASCII values from a table in a file.

Definition at line 45 of file AsciiTable.h.

Constructor & Destructor Documentation

template<class V , class M >
QUESO::AsciiTable< V, M >::AsciiTable ( const BaseEnvironment env,
unsigned int  numRows,
unsigned int  numExtraCols,
const std::vector< bool > *  extraColIsString,
const std::string &  fileName 
)

Default constructor.

This constructor reads the data from file fileName, checking whether the data in each column of the file is or not a string, and whether the data in each row is or not valid.

Definition at line 32 of file AsciiTable.C.

References QUESO::BaseEnvironment::displayVerbosity(), QUESO::AsciiTable< V, M >::m_colIsString, QUESO::AsciiTable< V, M >::m_doubleColumns, QUESO::AsciiTable< V, M >::m_env, QUESO::AsciiTable< V, M >::m_numCols, QUESO::AsciiTable< V, M >::m_stringColumns, QUESO::AsciiTable< V, M >::readColumnsFromFile(), and QUESO::BaseEnvironment::subDisplayFile().

38  :
39  m_env (env),
41  m_numCols (1+numExtraCols),
42  m_colIsString (1,true),
43  m_fileName (fileName),
44  m_map (newMap()),
45  m_stringColumns(0),
47 {
48  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 5)) {
49  *m_env.subDisplayFile() << "Entering AsciiTable<V,M>::constructor()..."
50  << std::endl;
51  }
52 
53  if (m_numCols > 1) {
54  m_colIsString.resize(m_numCols,false);
55  if (extraColIsString == NULL) {
56  // Nothing extra needs to be done
57  }
58  else {
59  unsigned int maxJ = std::min(numExtraCols,(unsigned int) extraColIsString->size());
60  for (unsigned int j = 0; j < maxJ; ++j) {
61  m_colIsString[1+j] = (*extraColIsString)[j];
62  }
63  }
64  }
65  m_stringColumns.resize(m_numCols,NULL);
66  m_doubleColumns.resize(m_numCols,NULL);
68 
69  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 5)) {
70  *m_env.subDisplayFile() << "Leaving AsciiTable<V,M>::constructor()"
71  << std::endl;
72  }
73 }
unsigned int displayVerbosity() const
Definition: Environment.C:396
unsigned int numRows() const
Returns the number of rows in the table.
Definition: AsciiTable.C:236
std::vector< DistArray< std::string > * > m_stringColumns
Definition: AsciiTable.h:92
const Map * m_map
Definition: AsciiTable.h:91
void readColumnsFromFile()
Definition: AsciiTable.C:90
std::vector< bool > m_colIsString
Definition: AsciiTable.h:88
const BaseEnvironment & m_env
Definition: AsciiTable.h:85
unsigned int m_numRows
Definition: AsciiTable.h:86
std::ofstream * subDisplayFile() const
Access function for m_subDisplayFile (displays file on stream).
Definition: Environment.C:274
std::vector< V * > m_doubleColumns
Definition: AsciiTable.h:93
unsigned int m_numCols
Definition: AsciiTable.h:87
std::string m_fileName
Definition: AsciiTable.h:89
template<class V , class M >
QUESO::AsciiTable< V, M >::~AsciiTable ( )

Destructor.

Definition at line 77 of file AsciiTable.C.

78 {
79  for (unsigned int j = 0; j < m_doubleColumns.size(); ++j) {
80  if (m_doubleColumns[j]) delete m_doubleColumns[j];
81  }
82  for (unsigned int j = 0; j < m_stringColumns.size(); ++j) {
83  if (m_stringColumns[j]) delete m_stringColumns[j];
84  }
85 }
std::vector< DistArray< std::string > * > m_stringColumns
Definition: AsciiTable.h:92
std::vector< V * > m_doubleColumns
Definition: AsciiTable.h:93

Member Function Documentation

template<class V , class M >
const V & QUESO::AsciiTable< V, M >::doubleColumn ( unsigned int  j) const

Returns the value (double) stored in column j.

Definition at line 261 of file AsciiTable.C.

References queso_require_less_msg, and queso_require_msg.

262 {
263  queso_require_less_msg(j, m_numCols, "invalid j");
264 
265  queso_require_msg(m_doubleColumns[j], "double column is not ready");
266 
267  return *m_doubleColumns[j];
268 }
#define queso_require_msg(asserted, msg)
Definition: asserts.h:69
#define queso_require_less_msg(expr1, expr2, msg)
Definition: asserts.h:87
std::vector< V * > m_doubleColumns
Definition: AsciiTable.h:93
unsigned int m_numCols
Definition: AsciiTable.h:87
template<>
Map * QUESO::AsciiTable< class GslVector, class GslMatrix >::newMap ( )
private

Definition at line 31 of file GslAsciiTable.C.

32 {
33  return new Map(m_numRows,0,m_env.selfComm());
34 }
const BaseEnvironment & m_env
Definition: AsciiTable.h:85
const MpiComm & selfComm() const
Access function for MpiComm self-communicator.
Definition: Environment.C:254
unsigned int m_numRows
Definition: AsciiTable.h:86
template<class V = GslVector, class M = GslMatrix>
Map* QUESO::AsciiTable< V, M >::newMap ( )
private
template<class V , class M >
unsigned int QUESO::AsciiTable< V, M >::numCols ( ) const

Returns the number of columns in the table.

Definition at line 243 of file AsciiTable.C.

244 {
245  return m_numCols;
246 }
unsigned int m_numCols
Definition: AsciiTable.h:87
template<class V , class M >
unsigned int QUESO::AsciiTable< V, M >::numRows ( ) const

Returns the number of rows in the table.

Definition at line 236 of file AsciiTable.C.

237 {
238  return m_numRows;
239 }
unsigned int m_numRows
Definition: AsciiTable.h:86
template<class V , class M >
void QUESO::AsciiTable< V, M >::print ( std::ostream &  os) const

Prints the table.

Definition at line 273 of file AsciiTable.C.

References queso_require_msg.

274 {
275  for (unsigned int j = 0; j < m_numCols; ++j) {
276  if ((m_stringColumns[j] != NULL)) queso_require_msg(!(m_doubleColumns[j]), "column is not null on both possible ways");
277  if ((m_stringColumns[j] == NULL)) queso_require_msg(m_doubleColumns[j], "column is null on both possible ways");
278 
279  os << "\nContents of table '" << m_fileName
280  << "', column " << j
281  << ":"
282  << std::endl;
283  if (m_stringColumns[j] != NULL) {
284  os << *m_stringColumns[j];
285  //DistArray<std::string>& arrayOfStrings = *m_stringColumns[j];
286  //for (unsigned int i = 0; i < m_numRows; ++i) {
287  // os << arrayOfStrings(i,0)
288  // << std::endl;
289  //}
290  }
291  else {
292  os << *m_doubleColumns[j];
293  }
294  os << std::endl;
295  }
296 
297  return;
298 }
std::vector< DistArray< std::string > * > m_stringColumns
Definition: AsciiTable.h:92
#define queso_require_msg(asserted, msg)
Definition: asserts.h:69
std::vector< V * > m_doubleColumns
Definition: AsciiTable.h:93
unsigned int m_numCols
Definition: AsciiTable.h:87
std::string m_fileName
Definition: AsciiTable.h:89
template<class V , class M >
void QUESO::AsciiTable< V, M >::readColumnsFromFile ( )
private

Definition at line 90 of file AsciiTable.C.

References QUESO::MiscReadCharsAndDoubleFromFile(), QUESO::MiscReadStringAndDoubleFromFile(), queso_error_msg, queso_require_equal_to_msg, and queso_require_msg.

Referenced by QUESO::AsciiTable< V, M >::AsciiTable().

91 {
92  unsigned int maxCharsPerLine = 512;
93 
94  std::ifstream ifs(m_fileName.c_str());
95  queso_require_msg(ifs.is_open(), "file was not found");
96 
97  // Determine number of lines
98  unsigned int numLines = std::count(std::istreambuf_iterator<char>(ifs),
99  std::istreambuf_iterator<char>(),
100  '\n');
101 
102  // Determine number of valid lines
103  int iRC;
104  ifs.seekg(0,std::ios_base::beg);
105  unsigned int lineId = 0;
106  unsigned int numValidLines = 0;
107  std::string tempString;
108  while ((lineId < numLines) && (ifs.eof() == false)) {
109  iRC = MiscReadStringAndDoubleFromFile(ifs,tempString,NULL);
110  queso_require_msg(!(iRC), "failed reading during the determination of the number of valid lines");
111  //*m_env.subDisplayFile() << "lineId = " << lineId
112  // << ", numValidLines = " << numValidLines
113  // << ", tempString = " << tempString
114  // << std::endl;
115  if (tempString[0] != '#') numValidLines++;
116  lineId++;
117  ifs.ignore(maxCharsPerLine,'\n');
118  }
119  queso_require_equal_to_msg(lineId, numLines, "the first number of lines read is nonconsistent");
120  if (m_numRows != numValidLines) {
121  char errorExplanation[512];
122  sprintf(errorExplanation,"number of valid lines (%u) in ASCII table file does not match number of rows (%u)",numValidLines,m_numRows);
123  queso_error_msg(errorExplanation);
124  }
125 
126  if (m_env.subDisplayFile()) {
127  *m_env.subDisplayFile() << "ASCII table file '" << m_fileName
128  << "' has " << numLines
129  << " lines and specifies " << numValidLines
130  << " valid lines."
131  << std::endl;
132  }
133 
134  for (unsigned int j=0; j < m_numCols; ++j) {
135  if (m_colIsString[j]) {
136  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 5)) {
137  *m_env.subDisplayFile() << "Column j = " << j
138  << " is a columns of strings"
139  << std::endl;
140  }
141  m_stringColumns[j] = new DistArray<std::string>(*m_map,1);
142  }
143  else {
144  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 5)) {
145  *m_env.subDisplayFile() << "Column j = " << j
146  << " is a columns of doubles"
147  << std::endl;
148  }
149  m_doubleColumns[j] = new V(m_env,*m_map);
150  }
151  }
152 
153  // Read file until End Of File character is reached
154  ifs.seekg(0,std::ios_base::beg);
155  lineId = 0;
156  unsigned int validLineId = 0;
157  std::string tmpString;
158  while ((lineId < numLines) && (ifs.eof() == false)) {
159  //*m_env.subDisplayFile() << "Beginning read of line (in ASCII table file) of id = " << lineId << std::endl;
160  bool endOfLineAchieved = false;
161 
162  iRC = MiscReadCharsAndDoubleFromFile(ifs, tmpString, NULL, endOfLineAchieved);
163  queso_require_msg(!(iRC), "failed reading a first column during the valid lines reading loop");
164 
165  lineId++;
166  if (tmpString[0] == '#') {
167  if (!endOfLineAchieved) ifs.ignore(maxCharsPerLine,'\n');
168  continue;
169  }
170 
171  DistArray<std::string>& firstColumn = *m_stringColumns[0];
172  firstColumn(validLineId,0) = tmpString;
173 
174  // Check 'validLineId' before setting one more valid line
175  if (validLineId >= numValidLines) {
176  char errorExplanation[512];
177  sprintf(errorExplanation,"validLineId (%u) got too large during reading of ASCII table file",validLineId);
178  queso_error_msg(errorExplanation);
179  }
180 
181  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 5)) {
182  *m_env.subDisplayFile() << "Just read a string: table[" << validLineId
183  << "," << 0 // j=0
184  << "] = " << firstColumn(validLineId,0)
185  << std::endl;
186  }
187 
188  for (unsigned int j=1; j < m_numCols; ++j) {
189  queso_require_msg(!(endOfLineAchieved), "failed reading all columns in a valid line");
190  if (m_colIsString[j]) {
191  DistArray<std::string>& arrayOfStrings = *m_stringColumns[j];
192  iRC = MiscReadCharsAndDoubleFromFile(ifs, arrayOfStrings(validLineId,0), NULL, endOfLineAchieved);
193  queso_require_msg(!(iRC), "failed reading a string column in a valid line");
194  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 5)) {
195  *m_env.subDisplayFile() << "Just read a string: table[" << validLineId
196  << "," << j
197  << "] = " << arrayOfStrings(validLineId,0)
198  << std::endl;
199  }
200  }
201  else {
202  iRC = MiscReadCharsAndDoubleFromFile(ifs, tmpString, &(*m_doubleColumns[j])[validLineId], endOfLineAchieved);
203  queso_require_msg(!(iRC), "failed reading a double column in a valid line");
204  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 5)) {
205  *m_env.subDisplayFile() << "Just read a double: table[" << validLineId
206  << "," << j
207  << "] = " << (*m_doubleColumns[j])[validLineId]
208  << std::endl;
209  }
210  }
211  }
212  if (!endOfLineAchieved) ifs.ignore(maxCharsPerLine,'\n');
213 
214  validLineId++;
215  }
216 
217  queso_require_equal_to_msg(lineId, numLines, "the second number of lines read is not consistent");
218  queso_require_equal_to_msg(validLineId, numValidLines, "the number of valid lines just read is not consistent");
219 
220  if (m_env.displayVerbosity() >= 5) {
221  if (m_env.subDisplayFile()) {
222  *m_env.subDisplayFile() << "Finished reading table '" << m_fileName
223  << "'. Its contents per column are:"
224  << std::endl;
225  *m_env.subDisplayFile() << *this; // FIX ME: output might need to be in parallel
226  *m_env.subDisplayFile() << std::endl;
227  }
228  }
229 
230  return;
231 }
unsigned int displayVerbosity() const
Definition: Environment.C:396
std::vector< DistArray< std::string > * > m_stringColumns
Definition: AsciiTable.h:92
const Map * m_map
Definition: AsciiTable.h:91
std::vector< bool > m_colIsString
Definition: AsciiTable.h:88
#define queso_error_msg(msg)
Definition: asserts.h:47
const BaseEnvironment & m_env
Definition: AsciiTable.h:85
#define queso_require_equal_to_msg(expr1, expr2, msg)
Definition: asserts.h:85
unsigned int m_numRows
Definition: AsciiTable.h:86
#define queso_require_msg(asserted, msg)
Definition: asserts.h:69
std::ofstream * subDisplayFile() const
Access function for m_subDisplayFile (displays file on stream).
Definition: Environment.C:274
std::vector< V * > m_doubleColumns
Definition: AsciiTable.h:93
unsigned int m_numCols
Definition: AsciiTable.h:87
int MiscReadCharsAndDoubleFromFile(std::ifstream &ifs, std::string &termString, double *termValue, bool &endOfLineAchieved)
std::string m_fileName
Definition: AsciiTable.h:89
int MiscReadStringAndDoubleFromFile(std::ifstream &ifs, std::string &termString, double *termValue)
template<class V , class M >
const DistArray< std::string > & QUESO::AsciiTable< V, M >::stringColumn ( unsigned int  j) const

Returns the string stored in column j.

Definition at line 250 of file AsciiTable.C.

References queso_require_less_msg, and queso_require_msg.

251 {
252  queso_require_less_msg(j, m_numCols, "invalid j");
253 
254  queso_require_msg(m_stringColumns[j], "string column is not ready");
255 
256  return *m_stringColumns[j];
257 }
std::vector< DistArray< std::string > * > m_stringColumns
Definition: AsciiTable.h:92
#define queso_require_msg(asserted, msg)
Definition: asserts.h:69
#define queso_require_less_msg(expr1, expr2, msg)
Definition: asserts.h:87
unsigned int m_numCols
Definition: AsciiTable.h:87

Member Data Documentation

template<class V = GslVector, class M = GslMatrix>
std::vector<bool> QUESO::AsciiTable< V, M >::m_colIsString
private

Definition at line 88 of file AsciiTable.h.

Referenced by QUESO::AsciiTable< V, M >::AsciiTable().

template<class V = GslVector, class M = GslMatrix>
std::vector<V*> QUESO::AsciiTable< V, M >::m_doubleColumns
private

Definition at line 93 of file AsciiTable.h.

Referenced by QUESO::AsciiTable< V, M >::AsciiTable().

template<class V = GslVector, class M = GslMatrix>
const BaseEnvironment& QUESO::AsciiTable< V, M >::m_env
private

Definition at line 85 of file AsciiTable.h.

Referenced by QUESO::AsciiTable< V, M >::AsciiTable().

template<class V = GslVector, class M = GslMatrix>
std::string QUESO::AsciiTable< V, M >::m_fileName
private

Definition at line 89 of file AsciiTable.h.

template<class V = GslVector, class M = GslMatrix>
const Map* QUESO::AsciiTable< V, M >::m_map
private

Definition at line 91 of file AsciiTable.h.

template<class V = GslVector, class M = GslMatrix>
unsigned int QUESO::AsciiTable< V, M >::m_numCols
private

Definition at line 87 of file AsciiTable.h.

Referenced by QUESO::AsciiTable< V, M >::AsciiTable().

template<class V = GslVector, class M = GslMatrix>
unsigned int QUESO::AsciiTable< V, M >::m_numRows
private

Definition at line 86 of file AsciiTable.h.

template<class V = GslVector, class M = GslMatrix>
std::vector<DistArray<std::string>*> QUESO::AsciiTable< V, M >::m_stringColumns
private

Definition at line 92 of file AsciiTable.h.

Referenced by QUESO::AsciiTable< V, M >::AsciiTable().


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

Generated on Thu Jun 11 2015 13:52:35 for queso-0.53.0 by  doxygen 1.8.5