queso-0.51.1
AsciiTable.C
Go to the documentation of this file.
1 //-----------------------------------------------------------------------bl-
2 //--------------------------------------------------------------------------
3 //
4 // QUESO - a library to support the Quantification of Uncertainty
5 // for Estimation, Simulation and Optimization
6 //
7 // Copyright (C) 2008,2009,2010,2011,2012,2013 The PECOS Development Team
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the Version 2.1 GNU Lesser General
11 // Public License as published by the Free Software Foundation.
12 //
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Lesser General Public License for more details.
17 //
18 // You should have received a copy of the GNU Lesser General Public
19 // License along with this library; if not, write to the Free Software
20 // Foundation, Inc. 51 Franklin Street, Fifth Floor,
21 // Boston, MA 02110-1301 USA
22 //
23 //-----------------------------------------------------------------------el-
24 
25 #include <queso/AsciiTable.h>
26 
27 namespace QUESO {
28 
29 // Default constructor -------------------------------------------------
30 template <class V, class M>
32  const BaseEnvironment& env,
33  unsigned int numRows,
34  unsigned int numExtraCols,
35  const std::vector<bool>* extraColIsString,
36  const std::string& fileName)
37  :
38  m_env (env),
39  m_numRows (numRows),
40  m_numCols (1+numExtraCols),
41  m_colIsString (1,true),
42  m_fileName (fileName),
43  m_map (newMap()),
44  m_stringColumns(0),
45  m_doubleColumns(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 }
73 
74 // Destructor ----------------------------------------------------------
75 template <class V, class M>
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 }
85 
86 // Private method ------------------------------------------------------
87 template <class V, class M>
88 void
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 }
264 
265 // Property methods-----------------------------------------------------
266 template <class V, class M>
267 unsigned int
269 {
270  return m_numRows;
271 }
272 
273 template <class V, class M>
274 unsigned int
276 {
277  return m_numCols;
278 }
279 
280 template <class V, class M>
282 AsciiTable<V,M>::stringColumn(unsigned int j) const
283 {
284  UQ_FATAL_TEST_MACRO(j >= m_numCols,
285  m_env.worldRank(),
286  "AsciiTable<V,M>::stringColumn()",
287  "invalid j");
288 
289  UQ_FATAL_TEST_MACRO(m_stringColumns[j] == NULL,
290  m_env.worldRank(),
291  "AsciiTable<V,M>::stringColumn()",
292  "string column is not ready");
293 
294  return *m_stringColumns[j];
295 }
296 
297 template <class V, class M>
298 const V&
299 AsciiTable<V,M>::doubleColumn(unsigned int j) const
300 {
301  UQ_FATAL_TEST_MACRO(j >= m_numCols,
302  m_env.worldRank(),
303  "AsciiTable<V,M>::doubleColumn()",
304  "invalid j");
305 
306  UQ_FATAL_TEST_MACRO(m_doubleColumns[j] == NULL,
307  m_env.worldRank(),
308  "AsciiTable<V,M>::doubleColumn()",
309  "double column is not ready");
310 
311  return *m_doubleColumns[j];
312 }
313 
314 // I/O methods----------------------------------------------------------
315 template <class V, class M>
316 void
317 AsciiTable<V,M>::print(std::ostream& os) const
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 }
349 
350 template<class V, class M>
351 std::ostream& operator<<(std::ostream& os, const AsciiTable<V,M>& obj)
352 {
353  obj.print(os);
354 
355  return os;
356 }
357 
358 } // End namespace QUESO
std::vector< bool > m_colIsString
Definition: AsciiTable.h:85
const DistArray< std::string > & stringColumn(unsigned int j) const
Returns the string stored in column j.
Definition: AsciiTable.C:282
void print(std::ostream &os) const
Prints the table.
Definition: AsciiTable.C:317
int MiscReadStringAndDoubleFromFile(std::ifstream &ifs, std::string &termString, double *termValue)
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
int MiscReadCharsAndDoubleFromFile(std::ifstream &ifs, std::string &termString, double *termValue, bool &endOfLineAchieved)
This (virtual) class sets up the environment underlying the use of the QUESO library by an executable...
Definition: Environment.h:187
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
const V & doubleColumn(unsigned int j) const
Returns the value (double) stored in column j.
Definition: AsciiTable.C:299
~AsciiTable()
Destructor.
Definition: AsciiTable.C:76
AsciiTable(const BaseEnvironment &env, unsigned int numRows, unsigned int numExtraCols, const std::vector< bool > *extraColIsString, const std::string &fileName)
Default constructor.
Definition: AsciiTable.C:31
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
unsigned int numCols() const
Returns the number of columns in the table.
Definition: AsciiTable.C:275
void readColumnsFromFile()
Definition: AsciiTable.C:89

Generated on Thu Apr 23 2015 19:26:15 for queso-0.51.1 by  doxygen 1.8.5