CSV.H
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) 2017-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 Class
28  Foam::Function1Types::CSV
29 
30 Description
31  Templated CSV function.
32 
33  Reference column is always a scalar, e.g. time.
34 
35  Usage:
36  \verbatim
37  <entryName> csvFile;
38  <entryName>Coeffs
39  {
40  nHeaderLine 4; // number of header lines
41  refColumn 0; // reference column index
42  componentColumns (1 2 3); // component column indices
43  separator ","; // optional (defaults to ",")
44  mergeSeparators no; // merge multiple separators
45  file "fileXYZ"; // name of csv data file
46  outOfBounds clamp; // optional out-of-bounds handling
47  interpolationScheme linear; // optional interpolation scheme
48  }
49  \endverbatim
50 
51 SourceFiles
52  CSV.C
53 
54 \*---------------------------------------------------------------------------*/
55 
56 #ifndef Foam_Function1Types_CSV_H
57 #define Foam_Function1Types_CSV_H
58 
59 #include "Function1.H"
60 #include "TableBase.H"
61 #include "Tuple2.H"
62 #include "labelList.H"
63 
64 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
65 
66 namespace Foam
67 {
68 namespace Function1Types
69 {
70 
71 /*---------------------------------------------------------------------------*\
72  Class CSV Declaration
73 \*---------------------------------------------------------------------------*/
74 
75 template<class Type>
76 class CSV
77 :
78  public TableBase<Type>
79 {
80  // Private Data
81 
82  //- Number header lines
83  const label nHeaderLine_;
84 
85  //- Column of the time
86  const label refColumn_;
87 
88  //- Labels of the components
89  const labelList componentColumns_;
90 
91  //- Separator character
92  const char separator_;
93 
94  //- Merge separators flag, e.g. ',,,' becomes ','
95  const bool mergeSeparators_;
96 
97  //- File name for csv table
98  fileName fName_;
99 
100 
101  // Private Member Functions
102 
103  //- Get component columns entry
104  static labelList getComponentColumns
105  (
106  const word& name,
107  const dictionary& dict
108  );
109 
110  //- Read csv data table
111  void read();
112 
113  //- Read component values from the split string
114  Type readValue(const UList<string>& strings) const;
115 
116 
117 public:
118 
119  // Generated Methods
120 
121  //- No copy assignment
122  void operator=(const CSV<Type>&) = delete;
123 
124 
125  //- Declare type-name, virtual type (with debug switch)
126  TypeName("csvFile");
127 
128 
129  // Constructors
130 
131  //- Construct from entry name, dictionary and optional registry
132  CSV
133  (
134  const word& entryName,
135  const dictionary& dict,
136  const objectRegistry* obrPtr = nullptr,
137  const fileName& fName = fileName::null
138  );
139 
140  //- Copy construct
141  explicit CSV(const CSV<Type>& csv);
142 
143  //- Construct and return a clone
144  virtual tmp<Function1<Type>> clone() const
145  {
146  return tmp<Function1<Type>>(new CSV<Type>(*this));
147  }
148 
149 
150  //- Destructor
151  virtual ~CSV() = default;
152 
153 
154  // Member Functions
155 
156  //- Return const access to the file name
157  virtual const fileName& fName() const;
158 
159  //- Write in dictionary format
160  virtual void writeData(Ostream& os) const;
161 
162  //- Write coefficient entries in dictionary format
163  virtual void writeEntries(Ostream& os) const;
164 };
165 
166 
167 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
168 
169 } // End namespace Function1Types
170 } // End namespace Foam
171 
172 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
173 
174 #ifdef NoRepository
175  #include "CSV.C"
176 #endif
177 
178 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
179 
180 #endif
181 
182 // ************************************************************************* //
A class for handling file names.
Definition: fileName.H:72
const word const dictionary & dict
Definition: Function1.H:140
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
Templated CSV function.
Definition: CSV.H:71
static const fileName null
An empty fileName.
Definition: fileName.H:111
TypeName("csvFile")
Declare type-name, virtual type (with debug switch)
virtual void writeEntries(Ostream &os) const
Write coefficient entries in dictionary format.
Definition: CSV.C:236
const word & entryName
Definition: Function1.H:140
virtual tmp< Function1< Type > > clone() const
Construct and return a clone.
Definition: CSV.H:167
Base class for table with bounds handling, interpolation and integration.
Definition: TableBase.H:56
virtual ~CSV()=default
Destructor.
A class for handling words, derived from Foam::string.
Definition: word.H:63
virtual void writeData(Ostream &os) const
Write in dictionary format.
Definition: CSV.C:257
void operator=(const CSV< Type > &)=delete
No copy assignment.
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: HashTable.H:105
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
OBJstream os(runTime.globalPath()/outputName)
const word const dictionary const objectRegistry * obrPtr
Definition: Function1.H:140
virtual const fileName & fName() const
Return const access to the file name.
Definition: CSV.C:229
A class for managing temporary objects.
Definition: HashPtrTable.H:50
Registry of regIOobjects.
Namespace for OpenFOAM.
const word & name() const noexcept
The name of the entry.
CSV(const word &entryName, const dictionary &dict, const objectRegistry *obrPtr=nullptr, const fileName &fName=fileName::null)
Construct from entry name, dictionary and optional registry.
Definition: CSV.C:192