csvTableReader.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 2011-2013 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "csvTableReader.H"
27 #include "IFstream.H"
28 #include "DynamicList.H"
29 
30 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
31 
32 template<class Type>
34 :
35  tableReader<Type>(dict),
36  headerLine_(readBool(dict.lookup("hasHeaderLine"))),
37  timeColumn_(readLabel(dict.lookup("timeColumn"))),
38  componentColumns_(dict.lookup("valueColumns")),
39  separator_(dict.lookupOrDefault<string>("separator", string(","))[0])
40 {
41  if (componentColumns_.size() != pTraits<Type>::nComponents)
42  {
43  FatalErrorIn("csvTableReader<Type>::csvTableReader(const dictionary&)")
44  << componentColumns_ << " does not have the expected length "
46  << exit(FatalError);
47  }
48 }
49 
50 
51 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
52 
53 template<class Type>
55 {}
56 
57 
58 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
59 
60 namespace Foam
61 {
62  // doesn't recognize specialization otherwise
63  template<>
64  scalar csvTableReader<scalar>::readValue(const List<string>& splitted)
65  {
66  if (componentColumns_[0] >= splitted.size())
67  {
69  (
70  "csvTableReader<scalar>::readValue(const List<string>&)"
71  ) << "No column " << componentColumns_[0] << " in "
72  << splitted << endl
73  << exit(FatalError);
74  }
75 
76  return readScalar(IStringStream(splitted[componentColumns_[0]])());
77  }
78 
79 
80  template<class Type>
81  Type csvTableReader<Type>::readValue(const List<string>& splitted)
82  {
83  Type result;
84 
85  for(label i = 0;i < pTraits<Type>::nComponents; i++)
86  {
87  if (componentColumns_[i] >= splitted.size())
88  {
90  (
91  "csvTableReader<Type>::readValue(const List<string>&)"
92  ) << "No column " << componentColumns_[i] << " in "
93  << splitted << endl
94  << exit(FatalError);
95  }
96 
97  result[i] = readScalar
98  (
99  IStringStream(splitted[componentColumns_[i]])()
100  );
101  }
102 
103  return result;
104  }
105 }
106 
107 
108 template<class Type>
110 (
111  const fileName& fName,
113 )
114 {
115  IFstream in(fName);
116 
118 
119  // Skip header
120  if (headerLine_)
121  {
122  string line;
123  in.getLine(line);
124  }
125 
126  while (in.good())
127  {
128  string line;
129  in.getLine(line);
130 
131  DynamicList<string> splitted;
132 
133  std::size_t pos = 0;
134  while (pos != std::string::npos)
135  {
136  std::size_t nPos = line.find(separator_, pos);
137 
138  if (nPos == std::string::npos)
139  {
140  splitted.append(line.substr(pos));
141  pos=nPos;
142  }
143  else
144  {
145  splitted.append(line.substr(pos, nPos-pos));
146  pos=nPos+1;
147  }
148  }
149 
150  if (splitted.size() <= 1)
151  {
152  break;
153  }
154 
155  scalar time = readScalar(IStringStream(splitted[timeColumn_])());
156  Type value = readValue(splitted);
157 
158  values.append(Tuple2<scalar,Type>(time, value));
159  }
160 
161  data.transfer(values);
162 }
163 
164 
165 template<class Type>
167 (
168  const fileName& fName,
170 )
171 {
172  notImplemented("csvTableReader<Type>::operator()");
173 }
174 
175 
176 template<class Type>
178 {
180 
181  os.writeKeyword("hasHeaderLine")
182  << headerLine_ << token::END_STATEMENT << nl;
183  os.writeKeyword("timeColumn")
184  << timeColumn_ << token::END_STATEMENT << nl;
185 
186  // Force writing labelList in ascii
187  os.writeKeyword("valueColumns");
188  if (os.format() == IOstream::BINARY)
189  {
191  os << componentColumns_;
193  }
194  os << token::END_STATEMENT << nl;
195 
196  os.writeKeyword("separator")
197  << string(separator_) << token::END_STATEMENT << nl;
198 }
199 
200 
201 // ************************************************************************* //
Input from memory buffer stream.
Definition: IStringStream.H:49
bool readBool(Istream &)
Definition: boolIO.C:60
A 2-tuple for storing two objects of different types.
Definition: Tuple2.H:47
virtual ~csvTableReader()
Destructor.
virtual void write(Ostream &os) const
Write additional information.
Definition: tableReader.C:79
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
A class for handling character strings derived from std::string.
Definition: string.H:74
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:76
DynamicList< T, SizeInc, SizeMult, SizeDiv > & append(const T &)
Append an element at the end of the list.
Definition: DynamicListI.H:310
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
Database for solution data, solver performance and other reduced data.
Definition: data.H:52
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:137
Input from file stream.
Definition: IFstream.H:81
Namespace for OpenFOAM.
label readLabel(Istream &is)
Definition: label.H:64
A line primitive.
Definition: line.H:56
dictionary dict
static const char nl
Definition: Ostream.H:260
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
stressControl lookup("compactNormalStress") >> compactNormalStress
bool readScalar(const char *buf, doubleScalar &s)
Read whole of buf as a scalar. Return true if succesful.
Definition: doubleScalar.H:63
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:333
streamFormat format() const
Return current stream format.
Definition: IOstream.H:377
Reads an interpolation table from a file - CSV-format.
Base class to read table data for the interpolationTable.
Definition: tableReader.H:57
csvTableReader(const dictionary &dict)
Construct from dictionary.
ISstream & getLine(string &)
Raw, low-level getline into a string function.
Definition: ISstreamI.H:77
Ostream & writeKeyword(const keyType &)
Write the keyword followed by an appropriate indentation.
Definition: Ostream.C:59
#define FatalErrorIn(functionName)
Report an error message using Foam::FatalError.
Definition: error.H:314
Traits class for primitives.
Definition: pTraits.H:50
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects...
Definition: DynamicList.H:56
error FatalError
A class for handling file names.
Definition: fileName.H:69
virtual void write(Ostream &os) const
Write the remaining parameters.
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:53
dimensionedScalar pos(const dimensionedScalar &ds)
#define notImplemented(functionName)
Issue a FatalErrorIn for a function not currently implemented.
Definition: error.H:356
void transfer(dictionary &)
Transfer the contents of the argument and annul the argument.
Definition: dictionary.C:1071