CsvTableReader.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2024 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 Class
25  Foam::TableReaders::Csv
26 
27 Description
28  Reads an interpolation table from a file in CSV-format. Entries govern the
29  layout of the CSV file. The indices of the columns of the table that are to
30  be used are given by the columns entry. This is a tuple for which the first
31  part is the index of the column used for the x-axis, and the second part is
32  the column index used for the scalar values, or a list of column indices
33  used for the components of vector, tensor, etc..., values.
34 
35 Usage
36  \verbatim
37  nHeaderLine 4; // number of header lines
38  columns (0 (1 2 3)); // column indices for vector values
39  separator ","; // optional (defaults to ",")
40  mergeSeparators no; // merge multiple separators
41  \endverbatim
42 
43 SourceFiles
44  CsvTableReader.C
45 
46 \*---------------------------------------------------------------------------*/
47 
48 #ifndef CsvTableReader_H
49 #define CsvTableReader_H
50 
51 #include "TableFileReader.H"
52 #include "labelList.H"
53 
54 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
55 
56 namespace Foam
57 {
58 namespace TableReaders
59 {
60 
61 /*---------------------------------------------------------------------------*\
62  Using CsvVoid Declaration
63 \*---------------------------------------------------------------------------*/
64 
65 template<typename T>
66 using CsvVoid = void;
67 
68 
69 /*---------------------------------------------------------------------------*\
70  Struct CsvLabelType Declaration
71 \*---------------------------------------------------------------------------*/
72 
73 template<class Type, typename = void>
74 struct CsvLabelType
75 {
76  typedef label type;
77 
79 
80  type operator()(const oldType& ot)
81  {
82  return ot[0];
83  }
84 };
85 
86 
87 template<class Type>
88 struct CsvLabelType<Type, CsvVoid<typename pTraits<Type>::labelType>>
89 {
90  typedef typename pTraits<Type>::labelType type;
91 
93 
94  type operator()(const oldType& ot) const
95  {
96  type t;
97  for (direction i = 0; i < pTraits<Type>::nComponents; ++ i)
98  {
99  setComponent(t, i) = ot[i];
100  }
101  return t;
102  }
103 };
104 
105 
106 /*---------------------------------------------------------------------------*\
107  Class Csv Declaration
108 \*---------------------------------------------------------------------------*/
109 
110 template<class Type>
111 class Csv
112 :
113  public TableFileReader<Type>
114 {
115  // Private Typedefs
116 
117  //- Type of the column indices
119 
120 
121  // Private Data
122 
123  //- Number of header lines
124  const label nHeaderLine_;
125 
126  //- Column indices
127  const columnIndices columns_;
128 
129  //- Separator character
130  const char separator_;
131 
132  //- Merge separators flag; e.g. ',,,' becomes ','
133  bool mergeSeparators_;
134 
135 
136  // Private Member functions
137 
138  //- Read the next value from the split string
139  Type readValue(const List<string>&) const;
140 
141  //- Read a 1D table
142  virtual void read(ISstream&, List<Tuple2<scalar, Type>>&) const;
143 
144 
145 public:
146 
147  //- Runtime type information
148  TypeName("csv");
149 
150 
151  // Constructors
152 
153  //- Construct from name and dictionary
154  Csv
155  (
156  const word& name,
158  const dictionary& dict
159  );
160 
161  //- Construct and return a copy
162  virtual autoPtr<TableReader<Type>> clone() const
163  {
164  return autoPtr<TableReader<Type>>(new Csv<Type>(*this));
165  }
166 
167 
168  //- Destructor
169  virtual ~Csv();
170 
171 
172  // Member Functions
173 
174  //- Write settings and values
175  virtual void write
176  (
177  Ostream& os,
179  const List<Tuple2<scalar, Type>>& table
180  ) const;
181 };
182 
183 
184 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
185 
186 } // End namespace TableReaders
187 } // End namespace Foam
188 
189 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
190 
191 #ifdef NoRepository
192  #include "CsvTableReader.C"
193 #endif
194 
195 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
196 
197 #endif
198 
199 // ************************************************************************* //
A 1D vector of objects of type <T> with a fixed size <Size>.
Definition: FixedList.H:78
Generic input stream.
Definition: ISstream.H:55
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:57
Base class to read table data for tables.
Reads an interpolation table from a file in CSV-format. Entries govern the layout of the CSV file....
Csv(const word &name, const Function1s::unitConversions &units, const dictionary &dict)
Construct from name and dictionary.
virtual ~Csv()
Destructor.
virtual autoPtr< TableReader< Type > > clone() const
Construct and return a copy.
virtual void write(Ostream &os, const Function1s::unitConversions &units, const List< Tuple2< scalar, Type >> &table) const
Write settings and values.
TypeName("csv")
Runtime type information.
A 2-tuple for storing two objects of different types.
Definition: Tuple2.H:66
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: autoPtr.H:51
A list of keyword definitions, which are a keyword followed by any number of values (e....
Definition: dictionary.H:162
Traits class for primitives.
Definition: pTraits.H:53
A class for handling words, derived from string.
Definition: word.H:62
Namespace for OpenFOAM.
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:59
word name(const bool)
Return a word representation of a bool.
Definition: boolIO.C:39
label & setComponent(label &l, const direction)
Definition: label.H:86
const HashTable< unitConversion > & units()
Get the table of unit conversions.
uint8_t direction
Definition: direction.H:45
dictionary dict
FixedList< label, 1 > oldType
type operator()(const oldType &ot)