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-2017 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 "fileOperation.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  autoPtr<ISstream> inPtr(fileHandler().NewIFstream(fName));
113  ISstream& in = inPtr();
114 
116 
117  // Skip header
118  if (headerLine_)
119  {
120  string line;
121  in.getLine(line);
122  }
123 
124  while (in.good())
125  {
126  string line;
127  in.getLine(line);
128 
129  DynamicList<string> splitted;
130 
131  std::size_t pos = 0;
132  while (pos != std::string::npos)
133  {
134  std::size_t nPos = line.find(separator_, pos);
135 
136  if (nPos == std::string::npos)
137  {
138  splitted.append(line.substr(pos));
139  pos=nPos;
140  }
141  else
142  {
143  splitted.append(line.substr(pos, nPos-pos));
144  pos=nPos+1;
145  }
146  }
147 
148  if (splitted.size() <= 1)
149  {
150  break;
151  }
152 
153  scalar time = readScalar(IStringStream(splitted[timeColumn_])());
154  Type value = readValue(splitted);
155 
156  values.append(Tuple2<scalar,Type>(time, value));
157  }
158 
159  data.transfer(values);
160 }
161 
162 
163 template<class Type>
165 (
166  const fileName& fName,
168 )
169 {
171 }
172 
173 
174 template<class Type>
176 {
178 
179  os.writeKeyword("hasHeaderLine")
180  << headerLine_ << token::END_STATEMENT << nl;
181  os.writeKeyword("timeColumn")
182  << timeColumn_ << token::END_STATEMENT << nl;
183 
184  // Force writing labelList in ascii
185  os.writeKeyword("valueColumns");
186  if (os.format() == IOstream::BINARY)
187  {
189  os << componentColumns_;
191  }
192  os << token::END_STATEMENT << nl;
193 
194  os.writeKeyword("separator")
195  << string(separator_) << token::END_STATEMENT << nl;
196 }
197 
198 
199 // ************************************************************************* //
virtual ~csvTableReader()
Destructor.
Reads an interpolation table from a file - CSV-format.
dictionary dict
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: HashTable.H:66
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:163
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:1144
virtual void write(Ostream &os) const
Write additional information.
Definition: tableReader.C:77
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:333
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
const fileOperation & fileHandler()
Get current file handler.
bool readScalar(const char *buf, doubleScalar &s)
Read whole of buf as a scalar. Return true if succesful.
Definition: doubleScalar.H:63
streamFormat format() const
Return current stream format.
Definition: IOstream.H:377
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
Ostream & writeKeyword(const keyType &)
Write the keyword followed by an appropriate indentation.
Definition: Ostream.C:54
csvTableReader(const dictionary &dict)
Construct from dictionary.
Generic input stream.
Definition: ISstream.H:51
Input from memory buffer stream.
Definition: IStringStream.H:49
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: PtrList.H:52
virtual void write(Ostream &os) const
Write the remaining parameters.
#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