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-2016 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  {
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  << "No column " << componentColumns_[0] << " in "
70  << splitted << endl
71  << exit(FatalError);
72  }
73 
74  return readScalar(IStringStream(splitted[componentColumns_[0]])());
75  }
76 
77 
78  template<class Type>
79  Type csvTableReader<Type>::readValue(const List<string>& splitted)
80  {
81  Type result;
82 
83  for(label i = 0;i < pTraits<Type>::nComponents; i++)
84  {
85  if (componentColumns_[i] >= splitted.size())
86  {
88  << "No column " << componentColumns_[i] << " in "
89  << splitted << endl
90  << exit(FatalError);
91  }
92 
93  result[i] = readScalar
94  (
95  IStringStream(splitted[componentColumns_[i]])()
96  );
97  }
98 
99  return result;
100  }
101 }
102 
103 
104 template<class Type>
106 (
107  const fileName& fName,
109 )
110 {
111  IFstream in(fName);
112 
114 
115  // Skip header
116  if (headerLine_)
117  {
118  string line;
119  in.getLine(line);
120  }
121 
122  while (in.good())
123  {
124  string line;
125  in.getLine(line);
126 
127  DynamicList<string> splitted;
128 
129  std::size_t pos = 0;
130  while (pos != std::string::npos)
131  {
132  std::size_t nPos = line.find(separator_, pos);
133 
134  if (nPos == std::string::npos)
135  {
136  splitted.append(line.substr(pos));
137  pos=nPos;
138  }
139  else
140  {
141  splitted.append(line.substr(pos, nPos-pos));
142  pos=nPos+1;
143  }
144  }
145 
146  if (splitted.size() <= 1)
147  {
148  break;
149  }
150 
151  scalar time = readScalar(IStringStream(splitted[timeColumn_])());
152  Type value = readValue(splitted);
153 
154  values.append(Tuple2<scalar,Type>(time, value));
155  }
156 
157  data.transfer(values);
158 }
159 
160 
161 template<class Type>
163 (
164  const fileName& fName,
166 )
167 {
169 }
170 
171 
172 template<class Type>
174 {
176 
177  os.writeKeyword("hasHeaderLine")
178  << headerLine_ << token::END_STATEMENT << nl;
179  os.writeKeyword("timeColumn")
180  << timeColumn_ << token::END_STATEMENT << nl;
181 
182  // Force writing labelList in ascii
183  os.writeKeyword("valueColumns");
184  if (os.format() == IOstream::BINARY)
185  {
187  os << componentColumns_;
189  }
190  os << token::END_STATEMENT << nl;
191 
192  os.writeKeyword("separator")
193  << string(separator_) << token::END_STATEMENT << nl;
194 }
195 
196 
197 // ************************************************************************* //
virtual ~csvTableReader()
Destructor.
Reads an interpolation table from a file - CSV-format.
dictionary dict
streamFormat format() const
Return current stream format.
Definition: IOstream.H:377
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 line primitive.
Definition: line.H:56
A class for handling file names.
Definition: fileName.H:69
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
error FatalError
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:137
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:319
A 2-tuple for storing two objects of different types.
Definition: Tuple2.H:47
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:76
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:333
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:253
Traits class for primitives.
Definition: pTraits.H:50
bool readBool(Istream &)
Definition: boolIO.C:60
void transfer(dictionary &)
Transfer the contents of the argument and annul the argument.
Definition: dictionary.C:1067
Base class to read table data for the interpolationTable.
Definition: tableReader.H:57
dimensionedScalar pos(const dimensionedScalar &ds)
stressControl lookup("compactNormalStress") >> compactNormalStress
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects...
Definition: DynamicList.H:56
DynamicList< T, SizeInc, SizeMult, SizeDiv > & append(const T &)
Append an element at the end of the list.
Definition: DynamicListI.H:292
bool readScalar(const char *buf, doubleScalar &s)
Read whole of buf as a scalar. Return true if succesful.
Definition: doubleScalar.H:63
label readLabel(Istream &is)
Definition: label.H:64
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:53
static const char nl
Definition: Ostream.H:262
Database for solution data, solver performance and other reduced data.
Definition: data.H:52
Input from file stream.
Definition: IFstream.H:81
Ostream & writeKeyword(const keyType &)
Write the keyword followed by an appropriate indentation.
Definition: Ostream.C:54
csvTableReader(const dictionary &dict)
Construct from dictionary.
Input from memory buffer stream.
Definition: IStringStream.H:49
virtual void write(Ostream &os) const
Write the remaining parameters.
virtual void write(Ostream &os) const
Write additional information.
Definition: tableReader.C:77
#define NotImplemented
Issue a FatalErrorIn for a function not currently implemented.
Definition: error.H:366
A class for handling character strings derived from std::string.
Definition: string.H:74
Namespace for OpenFOAM.
ISstream & getLine(string &)
Raw, low-level getline into a string function.
Definition: ISstreamI.H:77