/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#ifndef NC_INTERFACE_H
2#define NC_INTERFACE_H
3
4#ifdef AMR_WIND_USE_NETCDF
5#include <string>
6#include <unordered_map>
7#include <vector>
8
9#include "netcdf.h"
10#include "netcdf_par.h"
11#include "AMReX_REAL.H"
12
13using namespace amrex::literals;
14
15namespace ncutils {
16
18struct NCDType
19{
20 static constexpr nc_type Int = NC_INT;
21#ifdef AMREX_USE_FLOAT
22 static constexpr nc_type Real = NC_FLOAT;
23#else
24 static constexpr nc_type Real = NC_DOUBLE;
25#endif
26};
27
29struct NCDim
30{
32 const int ncid;
33
35 const int dimid;
36
38 std::string name() const;
39
41 size_t len() const;
42};
43
45struct NCVar
46{
48 const int ncid;
49
51 const int varid;
52
54 std::string name() const;
55
57 int ndim() const;
58
60 std::vector<size_t> shape() const;
61
63 void put(const amrex::Real* /*ptr*/) const;
64 void put(const float* /*ptr*/) const;
65 void put(const int* /*ptr*/) const;
66
68 void
69 put(const amrex::Real* /*dptr*/,
70 const std::vector<size_t>& /*start*/,
71 const std::vector<size_t>& /*count*/) const;
72
75 void
76 put(const amrex::Real* /*dptr*/,
77 const std::vector<size_t>& /*start*/,
78 const std::vector<size_t>& /*count*/,
79 const std::vector<ptrdiff_t>& /*stride*/) const;
81 void
82 put(const float* /*dptr*/,
83 const std::vector<size_t>& /*start*/,
84 const std::vector<size_t>& /*count*/) const;
85
88 void
89 put(const float* /*dptr*/,
90 const std::vector<size_t>& /*start*/,
91 const std::vector<size_t>& /*count*/,
92 const std::vector<ptrdiff_t>& /*stride*/) const;
93 void
94 put(const int* /*dptr*/,
95 const std::vector<size_t>& /*start*/,
96 const std::vector<size_t>& /*count*/) const;
97 void
98 put(const int* /*dptr*/,
99 const std::vector<size_t>& /*start*/,
100 const std::vector<size_t>& /*count*/,
101 const std::vector<ptrdiff_t>& /*stride*/) const;
102
104 void get(amrex::Real* /*ptr*/) const;
105 void get(float* /*ptr*/) const;
106 void get(int* /*ptr*/) const;
107
109 void
110 get(amrex::Real* /*dptr*/,
111 const std::vector<size_t>& /*start*/,
112 const std::vector<size_t>& /*count*/) const;
113
115 void
116 get(amrex::Real* /*dptr*/,
117 const std::vector<size_t>& /*start*/,
118 const std::vector<size_t>& /*count*/,
119 const std::vector<ptrdiff_t>& /*stride*/) const;
120
122 void
123 get(float* /*dptr*/,
124 const std::vector<size_t>& /*start*/,
125 const std::vector<size_t>& /*count*/) const;
126
128 void
129 get(float* /*dptr*/,
130 const std::vector<size_t>& /*start*/,
131 const std::vector<size_t>& /*count*/,
132 const std::vector<ptrdiff_t>& /*stride*/) const;
133
134 void
135 get(int* /*dptr*/,
136 const std::vector<size_t>& /*start*/,
137 const std::vector<size_t>& /*count*/) const;
138 void
139 get(int* /*dptr*/,
140 const std::vector<size_t>& /*start*/,
141 const std::vector<size_t>& /*count*/,
142 const std::vector<ptrdiff_t>& /*stride*/) const;
143
144 bool has_attr(const std::string& name) const;
145 void put_attr(const std::string& name, const std::string& value) const;
146 void put_attr(
147 const std::string& name, const std::vector<amrex::Real>& value) const;
148 void
149 put_attr(const std::string& name, const std::vector<float>& value) const;
150 void put_attr(const std::string& name, const std::vector<int>& value) const;
151
152 std::string get_attr(const std::string& name) const;
153 void
154 get_attr(const std::string& name, std::vector<amrex::Real>& value) const;
155 void get_attr(const std::string& name, std::vector<float>& value) const;
156 void get_attr(const std::string& name, std::vector<int>& value) const;
157 void par_access(const int cmode) const;
158};
159
162{
163public:
165 const int ncid;
166
168 bool is_root() const { return (m_parent != nullptr); }
169
171 const NCGroup& parent() const { return (is_root() ? *this : *m_parent); }
172
174 const NCGroup& root() const
175 {
176 return (is_root() ? *this : m_parent->root());
177 }
178
180 std::string name() const;
181
183 std::string full_name() const;
184
186 int num_groups() const;
187
189 int num_dimensions() const;
190
192 int num_variables() const;
193
195 int num_attributes() const;
196
198 bool has_group(const std::string& /*name*/) const;
199
201 bool has_dim(const std::string& /*name*/) const;
202
204 bool has_var(const std::string& /*name*/) const;
205
207 bool has_attr(const std::string& /*name*/) const;
208
213 NCGroup group(const std::string& /*name*/) const;
214
216 NCDim dim(const std::string& /*name*/) const;
217
219 NCVar var(const std::string& /*name*/) const;
220
222 NCGroup def_group(const std::string& /*name*/) const;
223
225 NCDim def_dim(const std::string& /*name*/, const size_t len) const;
226
228 NCVar def_scalar(const std::string& name, const nc_type dtype) const;
229
232 const std::string& name,
233 const nc_type dtype,
234 const std::vector<std::string>& /*dnames*/) const;
235
238 const std::string& name,
239 const nc_type dtype,
240 const std::vector<std::string>& dnames) const
241 {
242 return def_array(name, dtype, dnames);
243 }
244
245 void put_attr(const std::string& name, const std::string& value) const;
246 void put_attr(
247 const std::string& name, const std::vector<amrex::Real>& value) const;
248 void
249 put_attr(const std::string& name, const std::vector<float>& value) const;
250 void put_attr(const std::string& name, const std::vector<int>& value) const;
251
252 std::string get_attr(const std::string& name) const;
253 void
254 get_attr(const std::string& name, std::vector<amrex::Real>& value) const;
255 void get_attr(const std::string& name, std::vector<float>& value) const;
256 void get_attr(const std::string& name, std::vector<int>& value) const;
257
259 std::vector<NCGroup> all_groups() const;
260
262 std::vector<NCDim> all_dims() const;
263
265 std::vector<NCVar> all_vars() const;
266
268 void enter_def_mode() const;
269
271 void exit_def_mode() const;
272
273protected:
274 explicit NCGroup(const int id) : ncid(id) {}
275 NCGroup(const int id, const NCGroup* par) : ncid(id), m_parent(par) {}
276
277 const NCGroup* m_parent{nullptr};
278};
279
284class NCFile : public NCGroup
285{
286public:
287 static NCFile
288 create(const std::string& name, const int cmode = NC_CLOBBER | NC_NETCDF4);
289
290 static NCFile open(const std::string& name, const int cmode = NC_NOWRITE);
291
292 static NCFile create_par(
293 const std::string& name,
294 const int cmode = NC_CLOBBER | NC_NETCDF4 | NC_MPIIO,
295 MPI_Comm comm = MPI_COMM_WORLD,
296 MPI_Info info = MPI_INFO_NULL);
297
298 static NCFile open_par(
299 const std::string& name,
300 const int cmode = NC_NOWRITE,
301 MPI_Comm comm = MPI_COMM_WORLD,
302 MPI_Info info = MPI_INFO_NULL);
303
304 ~NCFile();
305
306 void close();
307
308protected:
309 explicit NCFile(const int id) : NCGroup(id), m_is_open{true} {}
310
311 bool m_is_open{false};
312};
313
314} // namespace ncutils
315
316#else
317
318namespace ncutils {
319
320struct NCDim
321{
322 const int ncid{-1};
323 const int dimid{-1};
324};
325
326struct NCVar
327{
328 const int ncid{-1};
329 const int varid{-1};
330};
331
332class NCGroup
333{
334public:
335 const int ncid{-1};
336};
337
338class NCFile : public NCGroup
339{};
340
341} // namespace ncutils
342
343#endif
344
345#endif /* NC_INTERFACE_H */
Definition nc_interface.H:285
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:311
NCFile(const int id)
Definition nc_interface.H:309
void close()
Definition nc_interface.cpp:582
Representation of a NetCDF group.
Definition nc_interface.H:162
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:174
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:277
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:171
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:165
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:274
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:237
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:275
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:168
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:19
static constexpr nc_type Real
Definition nc_interface.H:24
static constexpr nc_type Int
Definition nc_interface.H:20
Representation of NetCDF dimension.
Definition nc_interface.H:30
const int dimid
Dimension ID used with NetCDF API.
Definition nc_interface.H:35
const int ncid
File/Group Identifier.
Definition nc_interface.H:32
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:46
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:48
const int varid
Variable ID used with NetCDF API.
Definition nc_interface.H:51
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