CsvTableReader.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2019 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 "DynamicList.H"
28 #include "Field.H"
29 
30 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
31 
32 namespace Foam
33 {
34 namespace TableReaders
35 {
36 
37 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
38 
39 template<>
40 label Csv<label>::readValue(const List<string>& split) const
41 {
42  if (componentColumns_[0] >= split.size())
43  {
45  << "No column " << componentColumns_[0] << " in "
46  << split << endl
47  << exit(FatalError);
48  }
49 
50  return readLabel(IStringStream(split[componentColumns_[0]])());
51 }
52 
53 
54 template<>
55 scalar Csv<scalar>::readValue(const List<string>& split) const
56 {
57  if (componentColumns_[0] >= split.size())
58  {
60  << "No column " << componentColumns_[0] << " in "
61  << split << endl
62  << exit(FatalError);
63  }
64 
65  return readScalar(IStringStream(split[componentColumns_[0]])());
66 }
67 
68 
69 template<class Type>
70 Type Csv<Type>::readValue(const List<string>& split) const
71 {
72  Type result;
73 
74  for(label i = 0;i < pTraits<Type>::nComponents; i++)
75  {
76  if (componentColumns_[i] >= split.size())
77  {
79  << "No column " << componentColumns_[i] << " in "
80  << split << endl
81  << exit(FatalError);
82  }
83 
84  result[i] = readScalar
85  (
86  IStringStream(split[componentColumns_[i]])()
87  );
88  }
89 
90  return result;
91 }
92 
93 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
94 
95 } // End namespace TableReaders
96 } // End namespace Foam
97 
98 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
99 
100 template<class Type>
102 (
103  ISstream& is,
105 ) const
106 {
108 
109  // Skip header
110  for (label i = 0; i < nHeaderLine_; i++)
111  {
112  string line;
113  is.getLine(line);
114  }
115 
116  const label nEntries = max(refColumn_, max(componentColumns_));
117 
118  // Read data
119  while (is.good())
120  {
121  string line;
122  is.getLine(line);
123 
124  label n = 0;
125  std::size_t pos = 0;
126  DynamicList<string> split;
127 
128  if (mergeSeparators_)
129  {
130  std::size_t nPos = 0;
131 
132  while ((pos != std::string::npos) && (n <= nEntries))
133  {
134  bool found = false;
135  while (!found)
136  {
137  nPos = line.find(separator_, pos);
138 
139  if ((nPos != std::string::npos) && (nPos - pos == 0))
140  {
141  pos = nPos + 1;
142  }
143  else
144  {
145  found = true;
146  }
147  }
148 
149  nPos = line.find(separator_, pos);
150 
151  if (nPos == std::string::npos)
152  {
153  split.append(line.substr(pos));
154  pos = nPos;
155  n++;
156  }
157  else
158  {
159  split.append(line.substr(pos, nPos - pos));
160  pos = nPos + 1;
161  n++;
162  }
163  }
164  }
165  else
166  {
167  while ((pos != std::string::npos) && (n <= nEntries))
168  {
169  std::size_t nPos = line.find(separator_, pos);
170 
171  if (nPos == std::string::npos)
172  {
173  split.append(line.substr(pos));
174  pos = nPos;
175  n++;
176  }
177  else
178  {
179  split.append(line.substr(pos, nPos - pos));
180  pos = nPos + 1;
181  n++;
182  }
183  }
184  }
185 
186  if (split.size() <= 1)
187  {
188  break;
189  }
190 
191  scalar x = readScalar(IStringStream(split[refColumn_])());
192  Type value = readValue(split);
193 
194  values.append(Tuple2<scalar,Type>(x, value));
195  }
196 
197  data.transfer(values);
198 }
199 
200 
201 template<class Type>
203 (
204  ISstream&,
206 ) const
207 {
209 }
210 
211 
212 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
213 
214 template<class Type>
216 :
217  TableReader<Type>(dict),
218  nHeaderLine_(dict.lookup<label>("nHeaderLine")),
219  refColumn_(dict.lookup<label>("refColumn")),
220  componentColumns_(dict.lookup("componentColumns")),
221  separator_(dict.lookupOrDefault<string>("separator", string(","))[0]),
222  mergeSeparators_(readBool(dict.lookup("mergeSeparators")))
223 {
224  if (componentColumns_.size() != pTraits<Type>::nComponents)
225  {
227  << componentColumns_ << " does not have the expected length "
229  << exit(FatalError);
230  }
231 }
232 
233 
234 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
235 
236 template<class Type>
238 {}
239 
240 
241 template<class Type>
243 {
245 
246  writeEntry(os, "nHeaderLine", nHeaderLine_);
247  writeEntry(os, "refColumn", refColumn_);
248  if (os.format() == IOstream::BINARY)
249  {
251  writeEntry(os, "componentColumns", componentColumns_);
253  }
254  else
255  {
256  writeEntry(os, "componentColumns", componentColumns_);
257  }
258  writeEntry(os, "separator", string(separator_));
259  writeEntry(os, "mergeSeparators", mergeSeparators_);
260 }
261 
262 
263 // ************************************************************************* //
Base class to read table data for tables.
Definition: TableReader.H:52
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
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:158
dimensioned< Type > max(const dimensioned< Type > &, const dimensioned< Type > &)
#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:65
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:164
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
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:1397
virtual void write(Ostream &os) const
Write the remaining parameters.
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:333
Reads an interpolation table from a file in CSV-format. Entries govern the layout of the CSV file...
virtual ~Csv()
Destructor.
dimensionedScalar pos(const dimensionedScalar &ds)
Csv(const dictionary &dict)
Construct from dictionary.
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:296
bool readScalar(const char *buf, doubleScalar &s)
Read whole of buf as a scalar. Return true if successful.
Definition: doubleScalar.H:75
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:54
Database for solution and other reduced data.
Definition: data.H:51
void writeEntry(Ostream &os, const HashTable< T, Key, Hash > &ht)
Definition: HashTableIO.C:96
virtual void write(Ostream &os) const
Write additional information.
Definition: TableReader.C:72
Generic input stream.
Definition: ISstream.H:51
Input from memory buffer stream.
Definition: IStringStream.H:49
label n
#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:76
bool found
Namespace for OpenFOAM.
ISstream & getLine(string &)
Read line into a string.
Definition: ISstream.C:772