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-2023 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
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 
36 template<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(IOstreamOption::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 "
60  << exit(FatalIOError);
61  }
62 
63  return cols;
64 }
65 
66 
67 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
68 
69 template<class Type>
71 (
72  const UList<string>& strings
73 ) const
74 {
75  Type result;
76 
77  if (std::is_integral<Type>::value)
78  {
79  // nComponents == 1
80  setComponent(result, 0) = readLabel(strings[componentColumns_[0]]);
81  }
82  else
83  {
84  for (direction cmpt = 0; cmpt < pTraits<Type>::nComponents; ++cmpt)
85  {
86  setComponent(result, cmpt) =
87  readScalar(strings[componentColumns_[cmpt]]);
88  }
89  }
90 
91  return result;
92 }
93 
94 
95 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
96 
97 template<class Type>
99 :
100  tableReader<Type>(dict),
101  headerLine_(dict.get<bool>("hasHeaderLine")),
102  refColumn_(dict.getCompat<label>("refColumn", {{"timeColumn", 1912}})),
103  componentColumns_
104  (
105  getComponentColumns("componentColumns", {{"valueColumns", 1912}}, dict)
106  ),
107  separator_(dict.getOrDefault<string>("separator", ",")[0])
108 {}
109 
110 
111 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
112 
113 template<class Type>
115 (
116  const fileName& fName,
118 )
119 {
120  autoPtr<ISstream> isPtr(fileHandler().NewIFstream(fName));
121  ISstream& is = isPtr();
122 
123  const label maxEntry =
124  max(refColumn_, componentColumns_[findMax(componentColumns_)]);
125 
126  string line;
127  label lineNo = 0;
128 
129  // Skip header
130  if (headerLine_)
131  {
132  is.getLine(nullptr);
133  ++lineNo;
134  }
135 
136  DynamicList<Tuple2<scalar, Type>> values;
137  DynamicList<string> strings(maxEntry+1); // reserve
138 
139  while (is.good())
140  {
141  is.getLine(line);
142  ++lineNo;
143 
144  strings.clear();
145 
146  std::size_t pos = 0;
147 
148  for
149  (
150  label n = 0;
151  (pos != std::string::npos) && (n <= maxEntry);
152  ++n
153  )
154  {
155  const auto nPos = line.find(separator_, pos);
156 
157  if (nPos == std::string::npos)
158  {
159  strings.push_back(line.substr(pos));
160  pos = nPos;
161  }
162  else
163  {
164  strings.push_back(line.substr(pos, nPos-pos));
165  pos = nPos + 1;
166  }
167  }
168 
169  if (strings.size() <= 1)
170  {
171  break;
172  }
173 
174  if (strings.size() <= maxEntry)
175  {
177  << "Not enough columns near line " << lineNo
178  << ". Require " << (maxEntry+1) << " but found "
179  << strings << nl
180  << exit(FatalError);
181  }
182 
183  scalar x = readScalar(strings[refColumn_]);
184  Type value = readValue(strings);
185 
186  values.emplace_back(x, value);
187  }
189  data.transfer(values);
190 }
191 
192 
193 template<class Type>
195 (
196  const fileName& fName,
198 )
199 {
201 }
202 
203 
204 template<class Type>
206 {
208 
209  os.writeEntry("hasHeaderLine", headerLine_);
210  os.writeEntry("refColumn", refColumn_);
211 
212  // Force writing labelList in ASCII
213  const auto oldFmt = os.format(IOstreamOption::ASCII);
214  os.writeEntry("componentColumns", componentColumns_);
215  os.format(oldFmt);
216 
217  os.writeEntry("separator", string(separator_));
218 }
219 
220 
221 // ************************************************************************* //
List< ReturnType > get(const UPtrList< T > &list, const AccessOp &aop)
List of values generated by applying the access operation to each list item.
Reads an interpolation table from a file - CSV-format.
label findMax(const ListType &input, label start=0)
Linear search for the index of the max element, similar to std::max_element but for lists and returns...
dictionary dict
uint8_t direction
Definition: direction.H:46
A line primitive.
Definition: line.H:52
A class for handling file names.
Definition: fileName.H:72
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
error FatalError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL ERROR&#39; header text and sta...
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:598
label max(const labelHashSet &set, label maxValue=labelMin)
Find the max value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:40
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
"ascii" (normal default)
label readLabel(const char *buf)
Parse entire buffer as a label, skipping leading/trailing whitespace.
Definition: label.H:63
refPtr< fileOperation > fileHandler(std::nullptr_t)
Delete current file handler - forwards to fileOperation::handler()
Ostream & writeEntry(const keyType &key, const T &value)
Write a keyword/value entry.
Definition: Ostream.H:321
::Foam::direction nComponents(const expressions::valueTypeCode) noexcept
The number of components associated with given valueTypeCode.
Definition: exprTraits.C:40
virtual void write(Ostream &os) const
Write additional information.
Definition: tableReader.C:65
Base class to read table data for the interpolationTable.
Definition: tableReader.H:56
Various functions to operate on Lists.
List< T > values(const HashTable< T, Key, Hash > &tbl, const bool doSort=false)
List of values from HashTable, optionally sorted.
Definition: HashOps.H:164
dimensionedScalar pos(const dimensionedScalar &ds)
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition: exprTraits.C:127
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
ISstream & getLine(std::string &str, char delim='\n')
Raw, low-level getline (until delimiter) into a string.
Definition: ISstreamI.H:69
OBJstream os(runTime.globalPath()/outputName)
csvTableReader(const dictionary &dict)
Construct from dictionary.
Generic input stream using a standard (STL) stream.
Definition: ISstream.H:51
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:627
bool good() const noexcept
True if next operation might succeed.
Definition: IOstream.H:281
label n
Pointer management similar to std::unique_ptr, with some additional methods and type checking...
Definition: HashPtrTable.H:48
T getOrDefault(const word &keyword, const T &deflt, enum keyType::option matchOpt=keyType::REGEX) const
Find and return a T, or return the given default value. FatalIOError if it is found and the number of...
List< label > labelList
A List of labels.
Definition: List.H:62
virtual void write(Ostream &os) const
Write the remaining parameters.
label & setComponent(label &val, const direction) noexcept
Non-const access to integer-type (has no components)
Definition: label.H:144
#define NotImplemented
Issue a FatalErrorIn for a function not currently implemented.
Definition: error.H:686
streamFormat format() const noexcept
Get the current stream format.
IOerror FatalIOError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL IO ERROR&#39; header text and ...