queso-0.57.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
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-2017 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 <algorithm>
26 #include <queso/AsciiTable.h>
27 
28 namespace QUESO {
29 
30 // Default constructor -------------------------------------------------
31 template <class V, class M>
33  const BaseEnvironment& env,
34  unsigned int numRows,
35  unsigned int numExtraCols,
36  const std::vector<bool>* extraColIsString,
37  const std::string& fileName)
38  :
39  m_env (env),
40  m_numRows (numRows),
41  m_numCols (1+numExtraCols),
42  m_colIsString (1,true),
43  m_fileName (fileName),
44  m_map (newMap()),
45  m_stringColumns(0),
46  m_doubleColumns(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 }
74 
75 // Destructor ----------------------------------------------------------
76 template <class V, class M>
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 }
86 
87 // Private method ------------------------------------------------------
88 template <class V, class M>
89 void
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 }
232 
233 // Property methods-----------------------------------------------------
234 template <class V, class M>
235 unsigned int
237 {
238  return m_numRows;
239 }
240 
241 template <class V, class M>
242 unsigned int
244 {
245  return m_numCols;
246 }
247 
248 template <class V, class M>
250 AsciiTable<V,M>::stringColumn(unsigned int j) const
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 }
258 
259 template <class V, class M>
260 const V&
261 AsciiTable<V,M>::doubleColumn(unsigned int j) const
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 }
269 
270 // I/O methods----------------------------------------------------------
271 template <class V, class M>
272 void
273 AsciiTable<V,M>::print(std::ostream& os) const
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 }
299 
300 template<class V, class M>
301 std::ostream& operator<<(std::ostream& os, const AsciiTable<V,M>& obj)
302 {
303  obj.print(os);
304 
305  return os;
306 }
307 
308 } // End namespace QUESO
AsciiTable(const BaseEnvironment &env, unsigned int numRows, unsigned int numExtraCols, const std::vector< bool > *extraColIsString, const std::string &fileName)
Default constructor.
Definition: AsciiTable.C:32
int MiscReadStringAndDoubleFromFile(std::ifstream &ifs, std::string &termString, double *termValue)
std::vector< V * > m_doubleColumns
Definition: AsciiTable.h:93
This (virtual) class sets up the environment underlying the use of the QUESO library by an executable...
Definition: Environment.h:198
unsigned int m_numCols
Definition: AsciiTable.h:87
~AsciiTable()
Destructor.
Definition: AsciiTable.C:77
unsigned int numCols() const
Returns the number of columns in the table.
Definition: AsciiTable.C:243
std::vector< DistArray< std::string > * > m_stringColumns
Definition: AsciiTable.h:92
const V & doubleColumn(unsigned int j) const
Returns the value (double) stored in column j.
Definition: AsciiTable.C:261
MonteCarloSGOptions::MonteCarloSGOptions(const BaseEnvironment &env, const char *prefix, const McOptionsValues &alternativeOptionsValues queso_require_equal_to_msg)(m_env.optionsInputFileName(), std::string(""), std::string("this constructor is incompatible with the existence of an options input file"))
void readColumnsFromFile()
Definition: AsciiTable.C:90
unsigned int displayVerbosity() const
Definition: Environment.C:450
void print(std::ostream &os) const
Prints the table.
Definition: AsciiTable.C:273
std::vector< bool > m_colIsString
Definition: AsciiTable.h:88
int MiscReadCharsAndDoubleFromFile(std::ifstream &ifs, std::string &termString, double *termValue, bool &endOfLineAchieved)
const BaseEnvironment & m_env
Definition: AsciiTable.h:85
const DistArray< std::string > & stringColumn(unsigned int j) const
Returns the string stored in column j.
Definition: AsciiTable.C:250
unsigned int numRows() const
Returns the number of rows in the table.
Definition: AsciiTable.C:236
std::ofstream * subDisplayFile() const
Access function for m_subDisplayFile (displays file on stream).
Definition: Environment.C:320

Generated on Tue Jun 5 2018 19:48:55 for queso-0.57.1 by  doxygen 1.8.5