/home/runner/work/amr-wind/amr-wind/amr-wind/utilities/ncutils/nc_interface.H Source File

AMR-Wind API: /home/runner/work/amr-wind/amr-wind/amr-wind/utilities/ncutils/nc_interface.H Source File
AMR-Wind API v0.1.0
CFD solver for wind plant simulations
Loading...
Searching...
No Matches
nc_interface.H
Go to the documentation of this file.
1
9#ifndef NC_INTERFACE_H
10#define NC_INTERFACE_H
11
12#ifdef AMR_WIND_USE_NETCDF
13#include <string>
14#include <unordered_map>
15#include <vector>
16
17#include "netcdf.h"
18#include "netcdf_par.h"
19
20namespace ncutils {
21
23struct NCDType
24{
25 static constexpr nc_type Int = NC_INT;
26#ifdef AMREX_USE_FLOAT
27 static constexpr nc_type Real = NC_FLOAT;
28#else
29 static constexpr nc_type Real = NC_DOUBLE;
30#endif
31};
32
34struct NCDim
35{
37 const int ncid;
38
40 const int dimid;
41
43 std::string name() const;
44
46 size_t len() const;
47};
48
50struct NCVar
51{
53 const int ncid;
54
56 const int varid;
57
59 std::string name() const;
60
62 int ndim() const;
63
65 std::vector<size_t> shape() const;
66
68 void put(const double* /*ptr*/) const;
69 void put(const float* /*ptr*/) const;
70 void put(const int* /*ptr*/) const;
71
73 void
74 put(const double* /*dptr*/,
75 const std::vector<size_t>& /*start*/,
76 const std::vector<size_t>& /*count*/) const;
77
80 void
81 put(const double* /*dptr*/,
82 const std::vector<size_t>& /*start*/,
83 const std::vector<size_t>& /*count*/,
84 const std::vector<ptrdiff_t>& /*stride*/) const;
86 void
87 put(const float* /*dptr*/,
88 const std::vector<size_t>& /*start*/,
89 const std::vector<size_t>& /*count*/) const;
90
93 void
94 put(const float* /*dptr*/,
95 const std::vector<size_t>& /*start*/,
96 const std::vector<size_t>& /*count*/,
97 const std::vector<ptrdiff_t>& /*stride*/) const;
98 void
99 put(const int* /*dptr*/,
100 const std::vector<size_t>& /*start*/,
101 const std::vector<size_t>& /*count*/) const;
102 void
103 put(const int* /*dptr*/,
104 const std::vector<size_t>& /*start*/,
105 const std::vector<size_t>& /*count*/,
106 const std::vector<ptrdiff_t>& /*stride*/) const;
107
109 void get(double* /*ptr*/) const;
110 void get(float* /*ptr*/) const;
111 void get(int* /*ptr*/) const;
112
114 void
115 get(double* /*dptr*/,
116 const std::vector<size_t>& /*start*/,
117 const std::vector<size_t>& /*count*/) const;
118
120 void
121 get(double* /*dptr*/,
122 const std::vector<size_t>& /*start*/,
123 const std::vector<size_t>& /*count*/,
124 const std::vector<ptrdiff_t>& /*stride*/) const;
125
127 void
128 get(float* /*dptr*/,
129 const std::vector<size_t>& /*start*/,
130 const std::vector<size_t>& /*count*/) const;
131
133 void
134 get(float* /*dptr*/,
135 const std::vector<size_t>& /*start*/,
136 const std::vector<size_t>& /*count*/,
137 const std::vector<ptrdiff_t>& /*stride*/) const;
138
139 void
140 get(int* /*dptr*/,
141 const std::vector<size_t>& /*start*/,
142 const std::vector<size_t>& /*count*/) const;
143 void
144 get(int* /*dptr*/,
145 const std::vector<size_t>& /*start*/,
146 const std::vector<size_t>& /*count*/,
147 const std::vector<ptrdiff_t>& /*stride*/) const;
148
149 bool has_attr(const std::string& name) const;
150 void put_attr(const std::string& name, const std::string& value) const;
151 void
152 put_attr(const std::string& name, const std::vector<double>& value) const;
153 void
154 put_attr(const std::string& name, const std::vector<float>& value) const;
155 void put_attr(const std::string& name, const std::vector<int>& value) const;
156
157 std::string get_attr(const std::string& name) const;
158 void get_attr(const std::string& name, std::vector<double>& value) const;
159 void get_attr(const std::string& name, std::vector<float>& value) const;
160 void get_attr(const std::string& name, std::vector<int>& value) const;
161 void par_access(const int cmode) const;
162};
163
166{
167public:
169 const int ncid;
170
172 bool is_root() const { return (m_parent != nullptr); }
173
175 const NCGroup& parent() const { return (is_root() ? *this : *m_parent); }
176
178 const NCGroup& root() const
179 {
180 return (is_root() ? *this : m_parent->root());
181 }
182
184 std::string name() const;
185
187 std::string full_name() const;
188
190 int num_groups() const;
191
193 int num_dimensions() const;
194
196 int num_variables() const;
197
199 int num_attributes() const;
200
202 bool has_group(const std::string& /*name*/) const;
203
205 bool has_dim(const std::string& /*name*/) const;
206
208 bool has_var(const std::string& /*name*/) const;
209
211 bool has_attr(const std::string& /*name*/) const;
212
217 NCGroup group(const std::string& /*name*/) const;
218
220 NCDim dim(const std::string& /*name*/) const;
221
223 NCVar var(const std::string& /*name*/) const;
224
226 NCGroup def_group(const std::string& /*name*/) const;
227
229 NCDim def_dim(const std::string& /*name*/, const size_t len) const;
230
232 NCVar def_scalar(const std::string& name, const nc_type dtype) const;
233
236 const std::string& name,
237 const nc_type dtype,
238 const std::vector<std::string>& /*dnames*/) const;
239
242 const std::string& name,
243 const nc_type dtype,
244 const std::vector<std::string>& dnames) const
245 {
246 return def_array(name, dtype, dnames);
247 }
248
249 void put_attr(const std::string& name, const std::string& value) const;
250 void
251 put_attr(const std::string& name, const std::vector<double>& value) const;
252 void
253 put_attr(const std::string& name, const std::vector<float>& value) const;
254 void put_attr(const std::string& name, const std::vector<int>& value) const;
255
256 std::string get_attr(const std::string& name) const;
257 void get_attr(const std::string& name, std::vector<double>& value) const;
258 void get_attr(const std::string& name, std::vector<float>& value) const;
259 void get_attr(const std::string& name, std::vector<int>& value) const;
260
262 std::vector<NCGroup> all_groups() const;
263
265 std::vector<NCDim> all_dims() const;
266
268 std::vector<NCVar> all_vars() const;
269
271 void enter_def_mode() const;
272
274 void exit_def_mode() const;
275
276protected:
277 explicit NCGroup(const int id) : ncid(id) {}
278 NCGroup(const int id, const NCGroup* par) : ncid(id), m_parent(par) {}
279
280 const NCGroup* m_parent{nullptr};
281};
282
287class NCFile : public NCGroup
288{
289public:
290 static NCFile
291 create(const std::string& name, const int cmode = NC_CLOBBER | NC_NETCDF4);
292
293 static NCFile open(const std::string& name, const int cmode = NC_NOWRITE);
294
295 static NCFile create_par(
296 const std::string& name,
297 const int cmode = NC_CLOBBER | NC_NETCDF4 | NC_MPIIO,
298 MPI_Comm comm = MPI_COMM_WORLD,
299 MPI_Info info = MPI_INFO_NULL);
300
301 static NCFile open_par(
302 const std::string& name,
303 const int cmode = NC_NOWRITE,
304 MPI_Comm comm = MPI_COMM_WORLD,
305 MPI_Info info = MPI_INFO_NULL);
306
307 ~NCFile();
308
309 void close();
310
311protected:
312 explicit NCFile(const int id) : NCGroup(id), m_is_open{true} {}
313
314 bool m_is_open{false};
315};
316
317} // namespace ncutils
318
319#else
320
321namespace ncutils {
322
323struct NCDim
324{
325 const int ncid{-1};
326 const int dimid{-1};
327};
328
329struct NCVar
330{
331 const int ncid{-1};
332 const int varid{-1};
333};
334
335class NCGroup
336{
337public:
338 const int ncid{-1};
339};
340
341class NCFile : public NCGroup
342{};
343
344} // namespace ncutils
345
346#endif
347
348#endif /* NC_INTERFACE_H */
Definition nc_interface.H:288
static NCFile open(const std::string &name, const int cmode=NC_NOWRITE)
Definition nc_interface.cpp:548
static NCFile create(const std::string &name, const int cmode=NC_CLOBBER|NC_NETCDF4)
Definition nc_interface.cpp:541
~NCFile()
Definition nc_interface.cpp:571
static NCFile open_par(const std::string &name, const int cmode=NC_NOWRITE, MPI_Comm comm=MPI_COMM_WORLD, MPI_Info info=MPI_INFO_NULL)
Definition nc_interface.cpp:563
static NCFile create_par(const std::string &name, const int cmode=NC_CLOBBER|NC_NETCDF4|NC_MPIIO, MPI_Comm comm=MPI_COMM_WORLD, MPI_Info info=MPI_INFO_NULL)
Definition nc_interface.cpp:555
bool m_is_open
Definition nc_interface.H:314
NCFile(const int id)
Definition nc_interface.H:312
void close()
Definition nc_interface.cpp:578
Representation of a NetCDF group.
Definition nc_interface.H:166
bool has_group(const std::string &) const
Check if a group exists.
Definition nc_interface.cpp:393
const NCGroup & root() const
Return the root (file instance)
Definition nc_interface.H:178
bool has_attr(const std::string &) const
Check if an attribute exists.
Definition nc_interface.cpp:412
std::string get_attr(const std::string &name) const
Definition nc_interface.cpp:447
const NCGroup * m_parent
Definition nc_interface.H:280
void exit_def_mode() const
Exit definition mode.
Definition nc_interface.cpp:539
std::string name() const
Name of this group.
Definition nc_interface.cpp:285
NCVar def_scalar(const std::string &name, const nc_type dtype) const
Define a scalar variable, i.e., 0-dimensional array.
Definition nc_interface.cpp:333
int num_dimensions() const
Number of dimensions in this group.
Definition nc_interface.cpp:372
int num_variables() const
Number of variables within this group.
Definition nc_interface.cpp:386
int num_groups() const
Number of sub-groups within this group.
Definition nc_interface.cpp:365
std::string full_name() const
Full name for this group.
Definition nc_interface.cpp:295
NCGroup group(const std::string &) const
Definition nc_interface.cpp:312
NCVar var(const std::string &) const
Get the variable instance by name.
Definition nc_interface.cpp:358
const NCGroup & parent() const
Return parent group, for root it just returns itself.
Definition nc_interface.H:175
std::vector< NCGroup > all_groups() const
Return a list of all groups defined in this group.
Definition nc_interface.cpp:485
const int ncid
Identifier used with NetCDF API calls.
Definition nc_interface.H:169
std::vector< NCDim > all_dims() const
Return a list of all dimensions defined in this group.
Definition nc_interface.cpp:504
NCGroup(const int id)
Definition nc_interface.H:277
NCVar def_var(const std::string &name, const nc_type dtype, const std::vector< std::string > &dnames) const
Define a variable (wrapper for def_array)
Definition nc_interface.H:241
bool has_var(const std::string &) const
Check if a variable exists by name.
Definition nc_interface.cpp:405
bool has_dim(const std::string &) const
Check if a dimension exists by name.
Definition nc_interface.cpp:399
NCGroup def_group(const std::string &) const
Define new group.
Definition nc_interface.cpp:305
NCVar def_array(const std::string &name, const nc_type dtype, const std::vector< std::string > &) const
Define an array.
Definition nc_interface.cpp:340
void put_attr(const std::string &name, const std::string &value) const
Definition nc_interface.cpp:420
NCGroup(const int id, const NCGroup *par)
Definition nc_interface.H:278
NCDim dim(const std::string &) const
Get the dimension instance by name.
Definition nc_interface.cpp:319
NCDim def_dim(const std::string &, const size_t len) const
Define new dimension.
Definition nc_interface.cpp:326
bool is_root() const
Is this the NetCDF file (root group)?
Definition nc_interface.H:172
void enter_def_mode() const
Enter definition mode (not needed for NetCDF4 format)
Definition nc_interface.cpp:526
int num_attributes() const
Number of attributes within this group.
Definition nc_interface.cpp:379
std::vector< NCVar > all_vars() const
Return a list of all variables defined in this group.
Definition nc_interface.cpp:515
Definition nc_interface.cpp:9
Wrapper around NetCDF data types.
Definition nc_interface.H:24
static constexpr nc_type Real
Definition nc_interface.H:29
static constexpr nc_type Int
Definition nc_interface.H:25
Representation of NetCDF dimension.
Definition nc_interface.H:35
const int dimid
Dimension ID used with NetCDF API.
Definition nc_interface.H:40
const int ncid
File/Group Identifier.
Definition nc_interface.H:37
std::string name() const
Name of this dimension.
Definition nc_interface.cpp:24
size_t len() const
Length of this dimension.
Definition nc_interface.cpp:30
Representation of a NetCDF variable.
Definition nc_interface.H:51
bool has_attr(const std::string &name) const
Definition nc_interface.cpp:211
const int ncid
File/Group identifier.
Definition nc_interface.H:53
const int varid
Variable ID used with NetCDF API.
Definition nc_interface.H:56
std::string name() const
Name of this variable.
Definition nc_interface.cpp:37
std::string get_attr(const std::string &name) const
Definition nc_interface.cpp:246
void put(const double *) const
Write out the entire variable.
Definition nc_interface.cpp:67
void par_access(const int cmode) const
Definition nc_interface.cpp:280
void put_attr(const std::string &name, const std::string &value) const
Definition nc_interface.cpp:219
void get(double *) const
Read the entire variable from file.
Definition nc_interface.cpp:139
std::vector< size_t > shape() const
Shape of the array (size in each array dimension)
Definition nc_interface.cpp:50
int ndim() const
Number of array dimensions for this variable.
Definition nc_interface.cpp:43