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-2026 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 Coordinate, class Value>
111 class Csv
112 :
113  public TableFileReader<Coordinate, Value>
114 {
115  // Private Typedefs
116 
117  //- Type of the value indices
118  typedef Tuple2
119  <
122  > columnIndices;
123 
124 
125  // Private Data
126 
127  //- Number of header lines
128  const label nHeaderLine_;
129 
130  //- Column indices
131  const columnIndices columns_;
132 
133  //- Separator character
134  const char separator_;
135 
136  //- Merge separators flag; e.g. ',,,' becomes ','
137  bool mergeSeparators_;
138 
139 
140  // Private Member functions
141 
142  //- Read a 1D table
143  virtual void read(ISstream&, List<Tuple2<Coordinate, Value>>&) const;
144 
145 
146 public:
147 
148  //- Runtime type information
149  TypeName("csv");
150 
151 
152  // Constructors
153 
154  //- Construct from name and dictionary
155  Csv
156  (
157  const word& name,
158  const Function1s::unitSets& units,
159  const dictionary& dict
160  );
161 
162  //- Construct and return a copy
164  {
165  return
167  (
168  new Csv<Coordinate, Value>(*this)
169  );
170  }
171 
172 
173  //- Destructor
174  virtual ~Csv();
175 
176 
177  // Member Functions
178 
179  //- Write settings and values
180  virtual void write
181  (
182  Ostream& os,
183  const Function1s::unitSets& units,
185  const word& valuesKeyword=word::null
186  ) const;
187 };
188 
189 
190 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
191 
192 } // End namespace TableReaders
193 } // End namespace Foam
194 
195 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
196 
197 #ifdef NoRepository
198  #include "CsvTableReader.C"
199 #endif
200 
201 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
202 
203 #endif
204 
205 // ************************************************************************* //
A 1D vector of objects of type <T> with a fixed size <Size>.
Definition: FixedList.H:78
Generic input stream.
Definition: ISstream.H:55
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: List.H:91
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....
virtual autoPtr< TableReader< Coordinate, Value > > clone() const
Construct and return a copy.
virtual ~Csv()
Destructor.
virtual void write(Ostream &os, const Function1s::unitSets &units, const List< Tuple2< Coordinate, Value >> &table, const word &valuesKeyword=word::null) const
Write settings and values.
Csv(const word &name, const Function1s::unitSets &units, const dictionary &dict)
Construct from name and dictionary.
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 keywords followed by any number of values (e.g. words and numbers) or sub-dictionaries.
Definition: dictionary.H:162
Traits class for primitives.
Definition: pTraits.H:53
A class for handling words, derived from string.
Definition: word.H:63
static const word null
An empty word.
Definition: word.H:78
const HashTable< dimensionSet > table
Table of dimensions.
Definition: dimensions.C:74
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
label & setComponent(label &l, const direction)
Definition: label.H:86
word name(const LagrangianState state)
Return a string representation of a Lagrangian state enumeration.
uint8_t direction
Definition: direction.H:45
dictionary dict
Struct containing two unitSets for use in converting both the argument and the value of a Function1.
FixedList< label, 1 > oldType
type operator()(const oldType &ot)