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 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  const dictionary& dict
41 )
42 {
43  // Writing of columns was forced to be ASCII,
44  // do the same when reading
45 
46  labelList cols;
47 
48  ITstream& is = dict.lookup(name);
49  is.format(IOstream::ASCII);
50  is >> cols;
51  dict.checkITstream(is, name);
52 
53  if (cols.size() != pTraits<Type>::nComponents)
54  {
56  << name << " with " << cols
57  << " does not have the expected length "
58  << pTraits<Type>::nComponents << nl
59  << exit(FatalIOError);
60  }
61 
62  return cols;
63 }
64 
65 
66 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
67 
68 namespace Foam
69 {
70  template<>
71  label csvTableReader<label>::readValue
72  (
73  const List<string>& strings
74  ) const
75  {
76  return readLabel(strings[componentColumns_[0]]);
77  }
78 
79 
80  template<>
81  scalar csvTableReader<scalar>::readValue
82  (
83  const List<string>& strings
84  ) const
85  {
86  return readScalar(strings[componentColumns_[0]]);
87  }
88 
89 } // End namespace Foam
90 
91 
92 template<class Type>
94 (
95  const List<string>& strings
96 ) const
97 {
98  Type result;
99 
100  for (label i = 0; i < pTraits<Type>::nComponents; ++i)
101  {
102  result[i] = readScalar(strings[componentColumns_[i]]);
103  }
104 
105  return result;
106 }
107 
108 
109 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
110 
111 template<class Type>
113 :
114  tableReader<Type>(dict),
115  headerLine_(dict.get<bool>("hasHeaderLine")),
116  refColumn_(dict.get<label>("timeColumn")),
117  componentColumns_(getComponentColumns("valueColumns", dict)),
118  separator_(dict.getOrDefault<string>("separator", ",")[0])
119 {}
120 
121 
122 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
123 
124 template<class Type>
126 (
127  const fileName& fName,
129 )
130 {
131  autoPtr<ISstream> isPtr(fileHandler().NewIFstream(fName));
132  ISstream& is = isPtr();
133 
134  const label maxEntry =
135  max(refColumn_, componentColumns_[findMax(componentColumns_)]);
136 
137  string line;
138  label lineNo = 0;
139 
140  // Skip header
141  if (headerLine_)
142  {
143  is.getLine(line);
144  ++lineNo;
145  }
146 
148  DynamicList<string> strings(maxEntry+1); // reserve
149 
150  while (is.good())
151  {
152  is.getLine(line);
153  ++lineNo;
154 
155  strings.clear();
156 
157  std::size_t pos = 0;
158 
159  for
160  (
161  label n = 0;
162  (pos != std::string::npos) && (n <= maxEntry);
163  ++n
164  )
165  {
166  const auto nPos = line.find(separator_, pos);
167 
168  if (nPos == std::string::npos)
169  {
170  strings.append(line.substr(pos));
171  pos = nPos;
172  }
173  else
174  {
175  strings.append(line.substr(pos, nPos-pos));
176  pos = nPos + 1;
177  }
178  }
179 
180  if (strings.size() <= 1)
181  {
182  break;
183  }
184 
185  if (strings.size() <= maxEntry)
186  {
188  << "Not enough columns near line " << lineNo
189  << ". Require " << (maxEntry+1) << " but found "
190  << strings << nl
191  << exit(FatalError);
192  }
193 
194  scalar x = readScalar(strings[refColumn_]);
195  Type value = readValue(strings);
196 
197  values.append(Tuple2<scalar,Type>(x, value));
198  }
199 
201 }
202 
203 
204 template<class Type>
206 (
207  const fileName& fName,
209 )
210 {
212 }
213 
214 
215 template<class Type>
217 {
219 
220  os.writeEntry("hasHeaderLine", headerLine_);
221  os.writeEntry("timeColumn", refColumn_);
222 
223  // Force writing labelList in ascii
224  const enum IOstream::streamFormat fmt = os.format();
226  os.writeEntry("componentColumns", componentColumns_);
227  os.format(fmt);
228 
229  os.writeEntry("separator", string(separator_));
230 }
231 
232 
233 // ************************************************************************* //
csvTableReader.H
Foam::labelList
List< label > labelList
A List of labels.
Definition: List.H:71
Foam::ISstream::getLine
ISstream & getLine(std::string &str, char delim='\n')
Raw, low-level getline (until delimiter) into a string.
Definition: ISstreamI.H:76
Foam::fileName
A class for handling file names.
Definition: fileName.H:69
Foam::DynamicList
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects.
Definition: DynamicList.H:55
Foam::HashTableOps::values
List< T > values(const HashTable< T, Key, Hash > &tbl, const bool doSort=false)
List of values from HashTable, optionally sorted.
Definition: HashOps.H:149
Foam::IOstreamOption::format
streamFormat format() const noexcept
Get the current stream format.
Definition: IOstreamOption.H:289
Foam::ISstream
Generic input stream using a standard (STL) stream.
Definition: ISstream.H:55
Foam::fileHandler
const fileOperation & fileHandler()
Get current file handler.
Definition: fileOperation.C:1170
Foam::FatalIOError
IOerror FatalIOError
Foam::tableReader
Base class to read table data for the interpolationTable.
Definition: tableReader.H:60
Foam::csvTableReader::csvTableReader
csvTableReader(const dictionary &dict)
Construct from dictionary.
Definition: csvTableReader.C:112
Foam::string
A class for handling character strings derived from std::string.
Definition: string.H:73
Foam::dictionary::transfer
void transfer(dictionary &dict)
Transfer the contents of the argument and annul the argument.
Definition: dictionary.C:933
Foam::csvTableReader::write
virtual void write(Ostream &os) const
Write the remaining parameters.
Definition: csvTableReader.C:216
n
label n
Definition: TABSMDCalcMethod2.H:31
NotImplemented
#define NotImplemented
Issue a FatalErrorIn for a function not currently implemented.
Definition: error.H:436
Foam::name
word name(const complex &c)
Return string representation of complex.
Definition: complex.C:76
Foam::DynamicList::append
DynamicList< T, SizeMin > & append(const T &val)
Append an element to the end of this list.
Definition: DynamicListI.H:472
Foam::DynamicList::clear
void clear()
Clear the addressed list, i.e. set the size to zero.
Definition: DynamicListI.H:348
Foam::max
label max(const labelHashSet &set, label maxValue=labelMin)
Find the max value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:47
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::IOstreamOption::streamFormat
streamFormat
Data format (ascii | binary)
Definition: IOstreamOption.H:70
Foam::csvTableReader
Reads an interpolation table from a file - CSV-format.
Definition: csvTableReader.H:53
Foam::FatalError
error FatalError
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:121
fileOperation.H
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::autoPtr
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: HashPtrTable.H:53
Foam::IOstreamOption::ASCII
"ascii" (normal default)
Definition: IOstreamOption.H:72
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:372
Foam::nl
constexpr char nl
Definition: Ostream.H:385
Foam::readLabel
label readLabel(const char *buf)
Parse entire buffer as a label, skipping leading/trailing whitespace.
Definition: label.H:66
Foam::findMax
label findMax(const ListType &input, label start=0)
Foam::List< label >
bool
bool
Definition: EEqn.H:20
x
x
Definition: LISASMDCalcMethod2.H:52
Foam::line
A line primitive.
Definition: line.H:59
Foam::Ostream::writeEntry
Ostream & writeEntry(const keyType &key, const T &value)
Write a keyword/value entry.
Definition: Ostream.H:232
Foam::tableReader::write
virtual void write(Ostream &os) const
Write additional information.
Definition: tableReader.C:72
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:392
DynamicList.H
ListOps.H
Various functions to operate on Lists.
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::Tuple2
A 2-tuple for storing two objects of dissimilar types. The container is similar in purpose to std::pa...
Definition: Tuple2.H:57
Foam::IOstream::good
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:224
Foam::data
Database for solution data, solver performance and other reduced data.
Definition: data.H:54
Foam::pos
dimensionedScalar pos(const dimensionedScalar &ds)
Definition: dimensionedScalar.C:177