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-2024 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 (component(columns_.second(), 0) >= split.size())
43  {
45  << "No column " << component(columns_.second(), 0) << " in "
46  << split << endl
47  << exit(FatalError);
48  }
49 
50  return readLabel(IStringStream(split[component(columns_.second(), 0)])());
51 }
52 
53 
54 template<>
55 scalar Csv<scalar>::readValue(const List<string>& split) const
56 {
57  if (component(columns_.second(), 0) >= split.size())
58  {
60  << "No column " << component(columns_.second(), 0) << " in "
61  << split << endl
62  << exit(FatalError);
63  }
64 
65  return readScalar(IStringStream(split[component(columns_.second(), 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 (component(columns_.second(), i) >= split.size())
77  {
79  << "No column " << component(columns_.second(), i) << " in "
80  << split << endl
81  << exit(FatalError);
82  }
83 
84  result[i] = readScalar
85  (
86  IStringStream(split[component(columns_.second(), 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(columns_.first(), cmptMax(columns_.second()));
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[columns_.first()])());
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,
208  const dictionary& dict
209 )
210 :
211  TableFileReader<Type>(units, dict),
212  nHeaderLine_(dict.lookup<label>("nHeaderLine")),
213  columns_
214  (
215  !dict.found("refColumn") || !dict.found("componentColumns")
216  ? columnIndices(dict.lookup("columns"))
217  : columnIndices
218  (
219  dict.lookup<label>("refColumn"),
220  CsvLabelType<Type>()
221  (
222  dict.lookup<typename CsvLabelType<Type>::oldType>
223  (
224  "componentColumns"
225  )
226  )
227  )
228  ),
229  separator_(dict.lookupOrDefault<string>("separator", string(","))[0]),
230  mergeSeparators_(readBool(dict.lookup("mergeSeparators")))
231 {}
232 
233 
234 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
235 
236 template<class Type>
238 {}
239 
240 
241 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
242 
243 template<class Type>
245 (
246  Ostream& os,
248  const List<Tuple2<scalar, Type>>& table
249 ) const
250 {
252 
253  writeEntry(os, "nHeaderLine", nHeaderLine_);
254  writeEntry(os, "columns", columns_);
255  writeEntry(os, "separator", string(separator_));
256  writeEntry(os, "mergeSeparators", mergeSeparators_);
257 }
258 
259 
260 // ************************************************************************* //
bool found
label n
Input from memory buffer stream.
Definition: IStringStream.H:52
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 Function1s::unitConversions &units, 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 Function1s::unitConversions &units, const dictionary &dict)
Construct from name and dictionary.
virtual ~Csv()
Destructor.
virtual void write(Ostream &os, const Function1s::unitConversions &units, const List< Tuple2< scalar, Type >> &table) const
Write settings and values.
A 2-tuple for storing two objects of different types.
Definition: Tuple2.H:66
A list of keyword definitions, which are a keyword followed by any number of values (e....
Definition: dictionary.H:162
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:39
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:334
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:66
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:257
word name(const bool)
Return a word representation of a bool.
Definition: boolIO.C:39
void component(FieldField< Field, typename FieldField< Field, Type >::cmptType > &sf, const FieldField< Field, Type > &f, const direction d)
const HashTable< unitConversion > & units()
Get the table of unit conversions.
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)
void cmptMax(FieldField< Field, typename FieldField< Field, Type >::cmptType > &cf, const FieldField< Field, Type > &f)
label readLabel(Istream &is)
Definition: label.H:64
error FatalError
dictionary dict