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 | www.openfoam.com
6 \\/ M anipulation |
7-------------------------------------------------------------------------------
8 Copyright (C) 2011-2017 OpenFOAM Foundation
9 Copyright (C) 2020-2021 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
12 This file is part of OpenFOAM.
13
14 OpenFOAM is free software: you can redistribute it and/or modify it
15 under the terms of the GNU General Public License as published by
16 the Free Software Foundation, either version 3 of the License, or
17 (at your option) any later version.
18
19 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26
27\*---------------------------------------------------------------------------*/
28
29#include "csvTableReader.H"
30#include "fileOperation.H"
31#include "DynamicList.H"
32#include "ListOps.H"
33
34// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
35
36template<class Type>
38(
39 const word& name,
40 std::initializer_list<std::pair<const char*,int>> compat,
41 const dictionary& dict
42)
43{
44 // Writing of columns was forced to be ASCII,
45 // do the same when reading
46
47 labelList cols;
48
49 ITstream& is = dict.lookupCompat(name, compat);
50 is.format(IOstream::ASCII);
51 is >> cols;
52 dict.checkITstream(is, name);
53
54 if (cols.size() != pTraits<Type>::nComponents)
55 {
57 << name << " with " << cols
58 << " does not have the expected length "
59 << pTraits<Type>::nComponents << nl
60 << exit(FatalIOError);
61 }
62
63 return cols;
64}
65
66
67// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
68
69namespace Foam
70{
71 template<>
72 label csvTableReader<label>::readValue
73 (
74 const List<string>& strings
75 ) const
76 {
77 return readLabel(strings[componentColumns_[0]]);
78 }
79
80
81 template<>
82 scalar csvTableReader<scalar>::readValue
83 (
84 const List<string>& strings
85 ) const
86 {
87 return readScalar(strings[componentColumns_[0]]);
88 }
89
90} // End namespace Foam
91
92
93template<class Type>
95(
96 const List<string>& strings
97) const
98{
99 Type result;
100
101 for (label i = 0; i < pTraits<Type>::nComponents; ++i)
102 {
103 result[i] = readScalar(strings[componentColumns_[i]]);
104 }
105
106 return result;
107}
108
109
110// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
111
112template<class Type>
114:
115 tableReader<Type>(dict),
116 headerLine_(dict.get<bool>("hasHeaderLine")),
117 refColumn_(dict.getCompat<label>("refColumn", {{"timeColumn", 1912}})),
118 componentColumns_
119 (
120 getComponentColumns("componentColumns", {{"valueColumns", 1912}}, dict)
121 ),
122 separator_(dict.getOrDefault<string>("separator", ",")[0])
123{}
124
125
126// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
127
128template<class Type>
130(
131 const fileName& fName,
133)
134{
135 autoPtr<ISstream> isPtr(fileHandler().NewIFstream(fName));
136 ISstream& is = isPtr();
137
138 const label maxEntry =
139 max(refColumn_, componentColumns_[findMax(componentColumns_)]);
140
141 string line;
142 label lineNo = 0;
143
144 // Skip header
145 if (headerLine_)
146 {
147 is.getLine(line);
148 ++lineNo;
149 }
150
152 DynamicList<string> strings(maxEntry+1); // reserve
153
154 while (is.good())
155 {
156 is.getLine(line);
157 ++lineNo;
158
159 strings.clear();
160
161 std::size_t pos = 0;
162
163 for
164 (
165 label n = 0;
166 (pos != std::string::npos) && (n <= maxEntry);
167 ++n
168 )
169 {
170 const auto nPos = line.find(separator_, pos);
171
172 if (nPos == std::string::npos)
173 {
174 strings.append(line.substr(pos));
175 pos = nPos;
176 }
177 else
178 {
179 strings.append(line.substr(pos, nPos-pos));
180 pos = nPos + 1;
181 }
182 }
183
184 if (strings.size() <= 1)
185 {
186 break;
187 }
188
189 if (strings.size() <= maxEntry)
190 {
192 << "Not enough columns near line " << lineNo
193 << ". Require " << (maxEntry+1) << " but found "
194 << strings << nl
195 << exit(FatalError);
196 }
197
198 scalar x = readScalar(strings[refColumn_]);
199 Type value = readValue(strings);
200
201 values.append(Tuple2<scalar,Type>(x, value));
202 }
203
204 data.transfer(values);
205}
206
207
208template<class Type>
210(
211 const fileName& fName,
213)
214{
216}
217
218
219template<class Type>
221{
223
224 os.writeEntry("hasHeaderLine", headerLine_);
225 os.writeEntry("refColumn", refColumn_);
226
227 // Force writing labelList in ASCII
228 const auto oldFmt = os.format(IOstream::ASCII);
229 os.writeEntry("componentColumns", componentColumns_);
230 os.format(oldFmt);
231
232 os.writeEntry("separator", string(separator_));
233}
234
235
236// ************************************************************************* //
Various functions to operate on Lists.
label n
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects.
Definition: DynamicList.H:72
void clear() noexcept
Clear the addressed list, i.e. set the size to zero.
Definition: DynamicListI.H:391
void append(const T &val)
Copy append an element to the end of this list.
Definition: DynamicListI.H:503
streamFormat format() const noexcept
Get the current stream format.
bool good() const noexcept
True if next operation might succeed.
Definition: IOstream.H:233
Generic input stream using a standard (STL) stream.
Definition: ISstream.H:58
ISstream & getLine(std::string &str, char delim='\n')
Raw, low-level getline (until delimiter) into a string.
Definition: ISstreamI.H:76
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:62
Ostream & writeEntry(const keyType &key, const T &value)
Write a keyword/value entry.
Definition: Ostream.H:239
A 2-tuple for storing two objects of dissimilar types. The container is similar in purpose to std::pa...
Definition: Tuple2.H:58
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:114
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: autoPtr.H:66
Reads an interpolation table from a file - CSV-format.
Database for solution data, solver performance and other reduced data.
Definition: data.H:58
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:126
void transfer(dictionary &dict)
Transfer the contents of the argument and annul the argument.
Definition: dictionary.C:866
T getOrDefault(const word &keyword, const T &deflt, enum keyType::option matchOpt=keyType::REGEX) const
friend Ostream & operator(Ostream &, const faMatrix< Type > &)
A class for handling file names.
Definition: fileName.H:76
virtual bool write()
Write the output fields.
A line primitive.
Definition: line.H:68
Base class to read table data for the interpolationTable.
Definition: tableReader.H:61
bool
Definition: EEqn.H:20
#define NotImplemented
Issue a FatalErrorIn for a function not currently implemented.
Definition: error.H:517
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:473
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
OBJstream os(runTime.globalPath()/outputName)
Namespace for OpenFOAM.
const fileOperation & fileHandler()
Get current file handler.
dimensionedScalar pos(const dimensionedScalar &ds)
label max(const labelHashSet &set, label maxValue=labelMin)
Find the max value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:47
List< label > labelList
A List of labels.
Definition: List.H:66
label readLabel(const char *buf)
Parse entire buffer as a label, skipping leading/trailing whitespace.
Definition: label.H:66
error FatalError
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
label findMax(const ListType &input, label start=0)
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53
dictionary dict