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-2021 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,
104  List<Tuple2<scalar, Type>>& data
105 ) const
106 {
107  DynamicList<Tuple2<scalar, Type>> values;
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 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
202 
203 template<class Type>
205 (
206  const word& name,
207  const dictionary& dict,
208  List<Tuple2<scalar, Type>>& table
209 )
210 :
211  TableFileReader<Type>(dict),
212  nHeaderLine_(dict.lookup<label>("nHeaderLine")),
213  refColumn_(dict.lookup<label>("refColumn")),
214  componentColumns_(dict.lookup("componentColumns")),
215  separator_(dict.lookupOrDefault<string>("separator", string(","))[0]),
216  mergeSeparators_(readBool(dict.lookup("mergeSeparators")))
217 {
218  if (componentColumns_.size() != pTraits<Type>::nComponents)
219  {
221  << componentColumns_ << " does not have the expected length "
223  << exit(FatalError);
224  }
225 
227 }
228 
229 
230 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
231 
232 template<class Type>
234 {}
235 
236 
237 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
238 
239 template<class Type>
241 (
242  Ostream& os,
243  const List<Tuple2<scalar, Type>>& table
244 ) const
245 {
246  TableFileReader<Type>::write(os, table);
247 
248  writeEntry(os, "nHeaderLine", nHeaderLine_);
249  writeEntry(os, "refColumn", refColumn_);
250  writeEntry(os, "componentColumns", componentColumns_);
251  writeEntry(os, "separator", string(separator_));
252  writeEntry(os, "mergeSeparators", mergeSeparators_);
253 }
254 
255 
256 // ************************************************************************* //
bool found
label n
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: List.H:91
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:164
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:57
Base class to read table data for tables.
virtual void write(Ostream &os, const List< Tuple2< scalar, Type >> &table) const
Write additional information.
Reads an interpolation table from a file in CSV-format. Entries govern the layout of the CSV file....
Csv(const word &name, const dictionary &dict, List< Tuple2< scalar, Type >> &table)
Construct from dictionary.
virtual void write(Ostream &os, const List< Tuple2< scalar, Type >> &table) const
Write the CSV parameters.
virtual ~Csv()
Destructor.
A 2-tuple for storing two objects of different types.
Definition: Tuple2.H:63
A list of keyword definitions, which are a keyword followed by any number of values (e....
Definition: dictionary.H:160
Traits class for primitives.
Definition: pTraits.H:53
A class for handling character strings derived from std::string.
Definition: string.H:79
A class for handling words, derived from string.
Definition: word.H:62
#define readScalar
Definition: doubleScalar.C:38
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:306
Namespace for OpenFOAM.
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
dimensionedScalar pos(const dimensionedScalar &ds)
bool readBool(Istream &)
Definition: boolIO.C:60
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
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
void writeEntry(Ostream &os, const HashTable< T, Key, Hash > &ht)
Definition: HashTableIO.C:96
bool readScalar(const char *buf, doubleScalar &s)
Read whole of buf as a scalar. Return true if successful.
Definition: doubleScalar.H:75
layerAndWeight max(const layerAndWeight &a, const layerAndWeight &b)
label readLabel(Istream &is)
Definition: label.H:64
error FatalError
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
dictionary dict