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 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 \*---------------------------------------------------------------------------*/
27 
28 #include "csvTableReader.H"
29 #include "fileOperation.H"
30 #include "DynamicList.H"
31 
32 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
33 
34 template<class Type>
36 :
37  tableReader<Type>(dict),
38  headerLine_(dict.get<bool>("hasHeaderLine")),
39  timeColumn_(dict.get<label>("timeColumn")),
40  componentColumns_(dict.lookup("valueColumns")),
41  separator_(dict.lookupOrDefault<string>("separator", ",")[0])
42 {
43  if (componentColumns_.size() != pTraits<Type>::nComponents)
44  {
46  << componentColumns_ << " does not have the expected length "
48  << exit(FatalError);
49  }
50 }
51 
52 
53 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
54 
55 template<class Type>
57 {}
58 
59 
60 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
61 
62 namespace Foam
63 {
64  // doesn't recognize specialization otherwise
65  template<>
66  scalar csvTableReader<scalar>::readValue(const List<string>& splitted)
67  {
68  if (componentColumns_[0] >= splitted.size())
69  {
71  << "No column " << componentColumns_[0] << " in "
72  << splitted << endl
73  << exit(FatalError);
74  }
75 
76  return readScalar(splitted[componentColumns_[0]]);
77  }
78 
79 
80  template<class Type>
81  Type csvTableReader<Type>::readValue(const List<string>& splitted)
82  {
83  Type result;
84 
85  for (label i = 0; i < pTraits<Type>::nComponents; ++i)
86  {
87  if (componentColumns_[i] >= splitted.size())
88  {
90  << "No column " << componentColumns_[i] << " in "
91  << splitted << endl
92  << exit(FatalError);
93  }
94 
95  result[i] = readScalar(splitted[componentColumns_[i]]);
96  }
97 
98  return result;
99  }
100 }
101 
102 
103 template<class Type>
105 (
106  const fileName& fName,
108 )
109 {
110  //IFstream in(fName);
111  autoPtr<ISstream> inPtr(fileHandler().NewIFstream(fName));
112  ISstream& in = inPtr();
113 
115 
116  // Skip header
117  if (headerLine_)
118  {
119  string line;
120  in.getLine(line);
121  }
122 
123  while (in.good())
124  {
125  string line;
126  in.getLine(line);
127 
128  DynamicList<string> splitted;
129 
130  std::size_t pos = 0;
131  while (pos != std::string::npos)
132  {
133  std::size_t nPos = line.find(separator_, pos);
134 
135  if (nPos == std::string::npos)
136  {
137  splitted.append(line.substr(pos));
138  pos=nPos;
139  }
140  else
141  {
142  splitted.append(line.substr(pos, nPos-pos));
143  pos=nPos+1;
144  }
145  }
146 
147  if (splitted.size() <= 1)
148  {
149  break;
150  }
151 
152  scalar time = readScalar(splitted[timeColumn_]);
153  Type value = readValue(splitted);
154 
155  values.append(Tuple2<scalar,Type>(time, value));
156  }
157 
159 }
160 
161 
162 template<class Type>
164 (
165  const fileName& fName,
167 )
168 {
170 }
171 
172 
173 template<class Type>
175 {
177 
178  os.writeEntry("hasHeaderLine", headerLine_);
179  os.writeEntry("timeColumn", timeColumn_);
180 
181  // Force writing labelList in ascii
182  os.writeKeyword("valueColumns");
183  if (os.format() == IOstream::BINARY)
184  {
186  os << componentColumns_;
188  }
189  else
190  {
191  os << componentColumns_;
192  }
193  os << token::END_STATEMENT << nl;
194 
195  os.writeEntry("separator", string(separator_));
196 }
197 
198 
199 // ************************************************************************* //
csvTableReader.H
Foam::ISstream::getLine
ISstream & getLine(std::string &str, char delim='\n')
Raw, low-level getline into a string function.
Definition: ISstreamI.H:78
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:57
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:273
Foam::ISstream
Generic input stream using standard (STL) streams.
Definition: ISstream.H:54
Foam::fileHandler
const fileOperation & fileHandler()
Get current file handler.
Definition: fileOperation.C:1181
Foam::tableReader
Base class to read table data for the interpolationTable.
Definition: tableReader.H:59
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:337
Foam::csvTableReader::csvTableReader
csvTableReader(const dictionary &dict)
Construct from dictionary.
Definition: csvTableReader.C:35
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:928
Foam::csvTableReader::write
virtual void write(Ostream &os) const
Write the remaining parameters.
Definition: csvTableReader.C:174
NotImplemented
#define NotImplemented
Issue a FatalErrorIn for a function not currently implemented.
Definition: error.H:419
Foam::label
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:62
Foam::csvTableReader::~csvTableReader
virtual ~csvTableReader()
Destructor.
Definition: csvTableReader.C:56
Foam::DynamicList::append
DynamicList< T, SizeMin > & append(const T &val)
Append an element to the end of this list.
Definition: DynamicListI.H:472
Foam::token::END_STATEMENT
End entry [isseparator].
Definition: token.H:116
Foam::radiation::lookup
Lookup type of boundary radiation properties.
Definition: lookup.H:63
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::csvTableReader
Reads an interpolation table from a file - CSV-format.
Definition: csvTableReader.H:52
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::Ostream::writeKeyword
virtual Ostream & writeKeyword(const keyType &kw)
Write the keyword followed by an appropriate indentation.
Definition: Ostream.C:57
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::BINARY
"binary"
Definition: IOstreamOption.H:67
Foam::IOstreamOption::ASCII
"ascii"
Definition: IOstreamOption.H:66
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:355
Foam::nl
constexpr char nl
Definition: Ostream.H:372
Foam::List< string >
Foam::pTraits
Traits class for primitives.
Definition: pTraits.H:52
bool
bool
Definition: EEqn.H:20
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:219
Foam::tableReader::write
virtual void write(Ostream &os) const
Write additional information.
Definition: tableReader.C:79
DynamicList.H
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:216
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