/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
8
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#include "AMReX_REAL.H"
20
21using namespace amrex::literals;
22
23namespace ncutils {
24
26struct NCDType
27{
28 static constexpr nc_type Int = NC_INT;
29#ifdef AMREX_USE_FLOAT
30 static constexpr nc_type Real = NC_FLOAT;
31#else
32 static constexpr nc_type Real = NC_DOUBLE;
33#endif
34};
35
37struct NCDim
38{
40 const int ncid;
41
43 const int dimid;
44
46 std::string name() const;
47
49 size_t len() const;
50};
51
53struct NCVar
54{
56 const int ncid;
57
59 const int varid;
60
62 std::string name() const;
63
65 int ndim() const;
66
68 std::vector<size_t> shape() const;
69
71 void put(const amrex::Real* /*ptr*/) const;
72 void put(const float* /*ptr*/) const;
73 void put(const int* /*ptr*/) const;
74
76 void
77 put(const amrex::Real* /*dptr*/,
78 const std::vector<size_t>& /*start*/,
79 const std::vector<size_t>& /*count*/) const;
80
83 void
84 put(const amrex::Real* /*dptr*/,
85 const std::vector<size_t>& /*start*/,
86 const std::vector<size_t>& /*count*/,
87 const std::vector<ptrdiff_t>& /*stride*/) const;
89 void
90 put(const float* /*dptr*/,
91 const std::vector<size_t>& /*start*/,
92 const std::vector<size_t>& /*count*/) const;
93
96 void
97 put(const float* /*dptr*/,
98 const std::vector<size_t>& /*start*/,
99 const std::vector<size_t>& /*count*/,
100 const std::vector<ptrdiff_t>& /*stride*/) const;
101 void
102 put(const int* /*dptr*/,
103 const std::vector<size_t>& /*start*/,
104 const std::vector<size_t>& /*count*/) const;
105 void
106 put(const int* /*dptr*/,
107 const std::vector<size_t>& /*start*/,
108 const std::vector<size_t>& /*count*/,
109 const std::vector<ptrdiff_t>& /*stride*/) const;
110
112 void get(amrex::Real* /*ptr*/) const;
113 void get(float* /*ptr*/) const;
114 void get(int* /*ptr*/) const;
115
117 void
118 get(amrex::Real* /*dptr*/,
119 const std::vector<size_t>& /*start*/,
120 const std::vector<size_t>& /*count*/) const;
121
123 void
124 get(amrex::Real* /*dptr*/,
125 const std::vector<size_t>& /*start*/,
126 const std::vector<size_t>& /*count*/,
127 const std::vector<ptrdiff_t>& /*stride*/) const;
128
130 void
131 get(float* /*dptr*/,
132 const std::vector<size_t>& /*start*/,
133 const std::vector<size_t>& /*count*/) const;
134
136 void
137 get(float* /*dptr*/,
138 const std::vector<size_t>& /*start*/,
139 const std::vector<size_t>& /*count*/,
140 const std::vector<ptrdiff_t>& /*stride*/) const;
141
142 void
143 get(int* /*dptr*/,
144 const std::vector<size_t>& /*start*/,
145 const std::vector<size_t>& /*count*/) const;
146 void
147 get(int* /*dptr*/,
148 const std::vector<size_t>& /*start*/,
149 const std::vector<size_t>& /*count*/,
150 const std::vector<ptrdiff_t>& /*stride*/) const;
151
152 bool has_attr(const std::string& name) const;
153 void put_attr(const std::string& name, const std::string& value) const;
154 void put_attr(
155 const std::string& name, const std::vector<amrex::Real>& value) const;
156 void
157 put_attr(const std::string& name, const std::vector<float>& value) const;
158 void put_attr(const std::string& name, const std::vector<int>& value) const;
159
160 std::string get_attr(const std::string& name) const;
161 void
162 get_attr(const std::string& name, std::vector<amrex::Real>& value) const;
163 void get_attr(const std::string& name, std::vector<float>& value) const;
164 void get_attr(const std::string& name, std::vector<int>& value) const;
165 void par_access(const int cmode) const;
166};
167
170{
171public:
173 const int ncid;
174
176 bool is_root() const { return (m_parent != nullptr); }
177
179 const NCGroup& parent() const { return (is_root() ? *this : *m_parent); }
180
182 const NCGroup& root() const
183 {
184 return (is_root() ? *this : m_parent->root());
185 }
186
188 std::string name() const;
189
191 std::string full_name() const;
192
194 int num_groups() const;
195
197 int num_dimensions() const;
198
200 int num_variables() const;
201
203 int num_attributes() const;
204
206 bool has_group(const std::string& /*name*/) const;
207
209 bool has_dim(const std::string& /*name*/) const;
210
212 bool has_var(const std::string& /*name*/) const;
213
215 bool has_attr(const std::string& /*name*/) const;
216
221 NCGroup group(const std::string& /*name*/) const;
222
224 NCDim dim(const std::string& /*name*/) const;
225
227 NCVar var(const std::string& /*name*/) const;
228
230 NCGroup def_group(const std::string& /*name*/) const;
231
233 NCDim def_dim(const std::string& /*name*/, const size_t len) const;
234
236 NCVar def_scalar(const std::string& name, const nc_type dtype) const;
237
240 const std::string& name,
241 const nc_type dtype,
242 const std::vector<std::string>& /*dnames*/) const;
243
246 const std::string& name,
247 const nc_type dtype,
248 const std::vector<std::string>& dnames) const
249 {
250 return def_array(name, dtype, dnames);
251 }
252
253 void put_attr(const std::string& name, const std::string& value) const;
254 void put_attr(
255 const std::string& name, const std::vector<amrex::Real>& value) const;
256 void
257 put_attr(const std::string& name, const std::vector<float>& value) const;
258 void put_attr(const std::string& name, const std::vector<int>& value) const;
259
260 std::string get_attr(const std::string& name) const;
261 void
262 get_attr(const std::string& name, std::vector<amrex::Real>& value) const;
263 void get_attr(const std::string& name, std::vector<float>& value) const;
264 void get_attr(const std::string& name, std::vector<int>& value) const;
265
267 std::vector<NCGroup> all_groups() const;
268
270 std::vector<NCDim> all_dims() const;
271
273 std::vector<NCVar> all_vars() const;
274
276 void enter_def_mode() const;
277
279 void exit_def_mode() const;
280
281protected:
282 explicit NCGroup(const int id) : ncid(id) {}
283 NCGroup(const int id, const NCGroup* par) : ncid(id), m_parent(par) {}
284
285 const NCGroup* m_parent{nullptr};
286};
287
292class NCFile : public NCGroup
293{
294public:
295 static NCFile
296 create(const std::string& name, const int cmode = NC_CLOBBER | NC_NETCDF4);
297
298 static NCFile open(const std::string& name, const int cmode = NC_NOWRITE);
299
300 static NCFile create_par(
301 const std::string& name,
302 const int cmode = NC_CLOBBER | NC_NETCDF4 | NC_MPIIO,
303 MPI_Comm comm = MPI_COMM_WORLD,
304 MPI_Info info = MPI_INFO_NULL);
305
306 static NCFile open_par(
307 const std::string& name,
308 const int cmode = NC_NOWRITE,
309 MPI_Comm comm = MPI_COMM_WORLD,
310 MPI_Info info = MPI_INFO_NULL);
311
312 ~NCFile();
313
314 void close();
315
316protected:
317 explicit NCFile(const int id) : NCGroup(id), m_is_open{true} {}
318
319 bool m_is_open{false};
320};
321
322} // namespace ncutils
323
324#else
325
326namespace ncutils {
327
328struct NCDim
329{
330 const int ncid{-1};
331 const int dimid{-1};
332};
333
334struct NCVar
335{
336 const int ncid{-1};
337 const int varid{-1};
338};
339
340class NCGroup
341{
342public:
343 const int ncid{-1};
344};
345
346class NCFile : public NCGroup
347{};
348
349} // namespace ncutils
350
351#endif
352
353#endif /* NC_INTERFACE_H */
Definition nc_interface.H:293
static NCFile open(const std::string &name, const int cmode=NC_NOWRITE)
Definition nc_interface.cpp:552
static NCFile create(const std::string &name, const int cmode=NC_CLOBBER|NC_NETCDF4)
Definition nc_interface.cpp:545
~NCFile()
Definition nc_interface.cpp:575
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:567
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:559
bool m_is_open
Definition nc_interface.H:319
NCFile(const int id)
Definition nc_interface.H:317
void close()
Definition nc_interface.cpp:582
Representation of a NetCDF group.
Definition nc_interface.H:170
bool has_group(const std::string &) const
Check if a group exists.
Definition nc_interface.cpp:397
const NCGroup & root() const
Return the root (file instance)
Definition nc_interface.H:182
bool has_attr(const std::string &) const
Check if an attribute exists.
Definition nc_interface.cpp:416
std::string get_attr(const std::string &name) const
Definition nc_interface.cpp:451
const NCGroup * m_parent
Definition nc_interface.H:285
void exit_def_mode() const
Exit definition mode.
Definition nc_interface.cpp:543
std::string name() const
Name of this group.
Definition nc_interface.cpp:289
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:337
int num_dimensions() const
Number of dimensions in this group.
Definition nc_interface.cpp:376
int num_variables() const
Number of variables within this group.
Definition nc_interface.cpp:390
int num_groups() const
Number of sub-groups within this group.
Definition nc_interface.cpp:369
std::string full_name() const
Full name for this group.
Definition nc_interface.cpp:299
NCGroup group(const std::string &) const
Definition nc_interface.cpp:316
NCVar var(const std::string &) const
Get the variable instance by name.
Definition nc_interface.cpp:362
const NCGroup & parent() const
Return parent group, for root it just returns itself.
Definition nc_interface.H:179
std::vector< NCGroup > all_groups() const
Return a list of all groups defined in this group.
Definition nc_interface.cpp:489
const int ncid
Identifier used with NetCDF API calls.
Definition nc_interface.H:173
std::vector< NCDim > all_dims() const
Return a list of all dimensions defined in this group.
Definition nc_interface.cpp:508
NCGroup(const int id)
Definition nc_interface.H:282
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:245
bool has_var(const std::string &) const
Check if a variable exists by name.
Definition nc_interface.cpp:409
bool has_dim(const std::string &) const
Check if a dimension exists by name.
Definition nc_interface.cpp:403
NCGroup def_group(const std::string &) const
Define new group.
Definition nc_interface.cpp:309
NCVar def_array(const std::string &name, const nc_type dtype, const std::vector< std::string > &) const
Define an array.
Definition nc_interface.cpp:344
void put_attr(const std::string &name, const std::string &value) const
Definition nc_interface.cpp:424
NCGroup(const int id, const NCGroup *par)
Definition nc_interface.H:283
NCDim dim(const std::string &) const
Get the dimension instance by name.
Definition nc_interface.cpp:323
NCDim def_dim(const std::string &, const size_t len) const
Define new dimension.
Definition nc_interface.cpp:330
bool is_root() const
Is this the NetCDF file (root group)?
Definition nc_interface.H:176
void enter_def_mode() const
Enter definition mode (not needed for NetCDF4 format)
Definition nc_interface.cpp:530
int num_attributes() const
Number of attributes within this group.
Definition nc_interface.cpp:383
std::vector< NCVar > all_vars() const
Return a list of all variables defined in this group.
Definition nc_interface.cpp:519
Definition nc_interface.cpp:12
Wrapper around NetCDF data types.
Definition nc_interface.H:27
static constexpr nc_type Real
Definition nc_interface.H:32
static constexpr nc_type Int
Definition nc_interface.H:28
Representation of NetCDF dimension.
Definition nc_interface.H:38
const int dimid
Dimension ID used with NetCDF API.
Definition nc_interface.H:43
const int ncid
File/Group Identifier.
Definition nc_interface.H:40
std::string name() const
Name of this dimension.
Definition nc_interface.cpp:27
size_t len() const
Length of this dimension.
Definition nc_interface.cpp:33
Representation of a NetCDF variable.
Definition nc_interface.H:54
bool has_attr(const std::string &name) const
Definition nc_interface.cpp:214
void get(amrex::Real *) const
Read the entire variable from file.
Definition nc_interface.cpp:142
const int ncid
File/Group identifier.
Definition nc_interface.H:56
const int varid
Variable ID used with NetCDF API.
Definition nc_interface.H:59
std::string name() const
Name of this variable.
Definition nc_interface.cpp:40
void put(const amrex::Real *) const
Write out the entire variable.
Definition nc_interface.cpp:70
std::string get_attr(const std::string &name) const
Definition nc_interface.cpp:249
void par_access(const int cmode) const
Definition nc_interface.cpp:284
void put_attr(const std::string &name, const std::string &value) const
Definition nc_interface.cpp:222
std::vector< size_t > shape() const
Shape of the array (size in each array dimension)
Definition nc_interface.cpp:53
int ndim() const
Number of array dimensions for this variable.
Definition nc_interface.cpp:46