queso-0.51.1
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, class M>
class QUESO::AsciiTable< V, M >

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

Definition at line 42 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 31 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().

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

Destructor.

Definition at line 76 of file AsciiTable.C.

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

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 299 of file AsciiTable.C.

References UQ_FATAL_TEST_MACRO.

300 {
302  m_env.worldRank(),
303  "AsciiTable<V,M>::doubleColumn()",
304  "invalid j");
305 
307  m_env.worldRank(),
308  "AsciiTable<V,M>::doubleColumn()",
309  "double column is not ready");
310 
311  return *m_doubleColumns[j];
312 }
int worldRank() const
Returns the process world rank.
Definition: Environment.C:235
unsigned int m_numCols
Definition: AsciiTable.h:84
const BaseEnvironment & m_env
Definition: AsciiTable.h:82
std::vector< V * > m_doubleColumns
Definition: AsciiTable.h:90
#define UQ_FATAL_TEST_MACRO(test, givenRank, where, what)
Definition: Defines.h:223
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:82
unsigned int m_numRows
Definition: AsciiTable.h:83
const MpiComm & selfComm() const
Access function for MpiComm self-communicator.
Definition: Environment.C:279
template<class V , class M >
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 275 of file AsciiTable.C.

276 {
277  return m_numCols;
278 }
unsigned int m_numCols
Definition: AsciiTable.h:84
template<class V , class M >
unsigned int QUESO::AsciiTable< V, M >::numRows ( ) const

Returns the number of rows in the table.

Definition at line 268 of file AsciiTable.C.

269 {
270  return m_numRows;
271 }
unsigned int m_numRows
Definition: AsciiTable.h:83
template<class V , class M >
void QUESO::AsciiTable< V, M >::print ( std::ostream &  os) const

Prints the table.

Definition at line 317 of file AsciiTable.C.

References UQ_FATAL_TEST_MACRO.

318 {
319  for (unsigned int j = 0; j < m_numCols; ++j) {
320  UQ_FATAL_TEST_MACRO((m_stringColumns[j] != NULL) && (m_doubleColumns[j] != NULL),
321  m_env.worldRank(),
322  "AsciiTable<V,M>::print()",
323  "column is not null on both possible ways");
324  UQ_FATAL_TEST_MACRO((m_stringColumns[j] == NULL) && (m_doubleColumns[j] == NULL),
325  m_env.worldRank(),
326  "AsciiTable<V,M>::print()",
327  "column is null on both possible ways");
328 
329  os << "\nContents of table '" << m_fileName
330  << "', column " << j
331  << ":"
332  << std::endl;
333  if (m_stringColumns[j] != NULL) {
334  os << *m_stringColumns[j];
335  //DistArray<std::string>& arrayOfStrings = *m_stringColumns[j];
336  //for (unsigned int i = 0; i < m_numRows; ++i) {
337  // os << arrayOfStrings(i,0)
338  // << std::endl;
339  //}
340  }
341  else {
342  os << *m_doubleColumns[j];
343  }
344  os << std::endl;
345  }
346 
347  return;
348 }
int worldRank() const
Returns the process world rank.
Definition: Environment.C:235
unsigned int m_numCols
Definition: AsciiTable.h:84
const BaseEnvironment & m_env
Definition: AsciiTable.h:82
std::vector< DistArray< std::string > * > m_stringColumns
Definition: AsciiTable.h:89
std::string m_fileName
Definition: AsciiTable.h:86
std::vector< V * > m_doubleColumns
Definition: AsciiTable.h:90
#define UQ_FATAL_TEST_MACRO(test, givenRank, where, what)
Definition: Defines.h:223
template<class V , class M >
void QUESO::AsciiTable< V, M >::readColumnsFromFile ( )
private

Definition at line 89 of file AsciiTable.C.

References QUESO::MiscReadCharsAndDoubleFromFile(), QUESO::MiscReadStringAndDoubleFromFile(), and UQ_FATAL_TEST_MACRO.

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

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

References UQ_FATAL_TEST_MACRO.

283 {
285  m_env.worldRank(),
286  "AsciiTable<V,M>::stringColumn()",
287  "invalid j");
288 
290  m_env.worldRank(),
291  "AsciiTable<V,M>::stringColumn()",
292  "string column is not ready");
293 
294  return *m_stringColumns[j];
295 }
int worldRank() const
Returns the process world rank.
Definition: Environment.C:235
unsigned int m_numCols
Definition: AsciiTable.h:84
const BaseEnvironment & m_env
Definition: AsciiTable.h:82
std::vector< DistArray< std::string > * > m_stringColumns
Definition: AsciiTable.h:89
#define UQ_FATAL_TEST_MACRO(test, givenRank, where, what)
Definition: Defines.h:223

Member Data Documentation

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

Definition at line 85 of file AsciiTable.h.

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

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

Definition at line 90 of file AsciiTable.h.

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

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

Definition at line 82 of file AsciiTable.h.

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

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

Definition at line 86 of file AsciiTable.h.

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

Definition at line 88 of file AsciiTable.h.

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

Definition at line 84 of file AsciiTable.h.

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

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

Definition at line 83 of file AsciiTable.h.

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

Definition at line 89 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 Apr 23 2015 19:26:17 for queso-0.51.1 by  doxygen 1.8.5