9 #ifndef ERF_NC_INTERFACE_H
10 #define ERF_NC_INTERFACE_H
14 #include <unordered_map>
18 #include <netcdf_par.h>
25 static constexpr nc_type
Int = NC_INT;
26 #ifdef AMREX_USE_FLOAT
27 static constexpr nc_type
Real = NC_FLOAT;
30 static constexpr nc_type
Real = NC_DOUBLE;
45 [[nodiscard]] std::string
name()
const;
48 [[nodiscard]]
size_t len()
const;
61 [[nodiscard]] std::string
name ()
const;
64 [[nodiscard]]
int ndim ()
const;
67 [[nodiscard]] std::vector<size_t>
shape ()
const;
70 void put (
const double*)
const;
71 void put (
const float*)
const;
72 void put (
const int*)
const;
77 const std::vector<size_t>&,
78 const std::vector<size_t>&)
const;
84 const std::vector<size_t>&,
85 const std::vector<size_t>&,
86 const std::vector<ptrdiff_t>&)
const;
90 const std::vector<size_t>&,
91 const std::vector<size_t>&)
const;
97 const std::vector<size_t>&,
98 const std::vector<size_t>&,
99 const std::vector<ptrdiff_t>&)
const;
101 void put (
const int*,
const std::vector<size_t>&,
const std::vector<size_t>&)
const;
105 const std::vector<size_t>&,
106 const std::vector<size_t>&,
107 const std::vector<ptrdiff_t>&)
const;
109 void put (
const char**,
const std::vector<size_t>&,
const std::vector<size_t>&)
const;
112 put (
const char** dptr,
113 const std::vector<size_t>& start,
114 const std::vector<size_t>& count,
115 const std::vector<ptrdiff_t>& stride)
const;
118 void get (
double*)
const;
119 void get (
float*)
const;
120 void get (
int*)
const;
124 get (
double*,
const std::vector<size_t>&,
const std::vector<size_t>&)
const;
129 const std::vector<size_t>&,
130 const std::vector<size_t>&,
131 const std::vector<ptrdiff_t>&)
const;
135 get (
float*,
const std::vector<size_t>&,
const std::vector<size_t>&)
const;
140 const std::vector<size_t>&,
141 const std::vector<size_t>&,
142 const std::vector<ptrdiff_t>&)
const;
145 get (
int*,
const std::vector<size_t>&,
const std::vector<size_t>&)
const;
149 const std::vector<size_t>&,
150 const std::vector<size_t>&,
151 const std::vector<ptrdiff_t>&)
const;
155 get (
char*,
const std::vector<size_t>&,
const std::vector<size_t>&)
const;
160 const std::vector<size_t>&,
161 const std::vector<size_t>&,
162 const std::vector<ptrdiff_t>&)
const;
164 [[nodiscard]]
bool has_attr (
const std::string&
name)
const;
166 void put_attr (
const std::string&
name,
const std::string& value)
const;
169 put_attr (
const std::string&
name,
const std::vector<double>& value)
const;
172 put_attr (
const std::string&
name,
const std::vector<float>& value)
const;
174 void put_attr (
const std::string&
name,
const std::vector<int>& value)
const;
176 [[nodiscard]] std::string
get_attr (
const std::string&
name)
const;
178 void get_attr (
const std::string&
name, std::vector<double>& value)
const;
179 void get_attr (
const std::string&
name, std::vector<float>& value)
const;
180 void get_attr (
const std::string&
name, std::vector<int>& value)
const;
193 [[nodiscard]] std::string
name ()
const;
196 [[nodiscard]] std::string
full_name ()
const;
211 [[nodiscard]]
bool has_group (
const std::string&)
const;
214 [[nodiscard]]
bool has_dim (
const std::string&)
const;
217 [[nodiscard]]
bool has_var (
const std::string&)
const;
220 [[nodiscard]]
bool has_attr (
const std::string&)
const;
229 [[nodiscard]]
NCDim dim (
const std::string&)
const;
232 [[nodiscard]]
NCVar var (
const std::string&)
const;
235 void def_dim (
const std::string&,
size_t len)
const;
240 const std::vector<std::string>&)
const;
245 const std::vector<std::string>& dnames)
const
250 void put_attr (
const std::string&
name,
const std::string& value)
const;
251 void put_attr (
const std::string&
name,
const std::vector<double>& value)
const;
252 void put_attr (
const std::string&
name,
const std::vector<float>& value)
const;
253 void put_attr (
const std::string&
name,
const std::vector<int>& value)
const;
255 [[nodiscard]] std::string
get_attr (
const std::string&
name)
const;
256 void get_attr (
const std::string&
name, std::vector<double>& value)
const;
257 void get_attr (
const std::string&
name, std::vector<float>& value)
const;
258 void get_attr (
const std::string&
name, std::vector<int>& value)
const;
261 [[nodiscard]] std::vector<NCGroup>
all_groups ()
const;
264 [[nodiscard]] std::vector<NCDim>
all_dims ()
const;
267 [[nodiscard]] std::vector<NCVar>
all_vars ()
const;
288 create (
const std::string&
name,
const int cmode = NC_CLOBBER | NC_NETCDF4);
290 static NCFile open (
const std::string&
name,
const int cmode = NC_NOWRITE);
293 const int cmode = NC_CLOBBER | NC_NETCDF4 | NC_MPIIO,
294 MPI_Comm comm = MPI_COMM_WORLD,
295 MPI_Info info = MPI_INFO_NULL);
298 const int cmode = NC_NOWRITE,
299 MPI_Comm comm = MPI_COMM_WORLD,
300 MPI_Info info = MPI_INFO_NULL);
336 class NCFile :
public NCGroup
Definition: ERF_NCInterface.H:285
static NCFile open(const std::string &name, const int cmode=NC_NOWRITE)
Definition: ERF_NCInterface.cpp:674
static NCFile create(const std::string &name, const int cmode=NC_CLOBBER|NC_NETCDF4)
Definition: ERF_NCInterface.cpp:667
~NCFile()
Definition: ERF_NCInterface.cpp:696
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: ERF_NCInterface.cpp:689
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: ERF_NCInterface.cpp:681
bool is_open
Definition: ERF_NCInterface.H:309
NCFile(const int id)
Definition: ERF_NCInterface.H:307
void close()
Definition: ERF_NCInterface.cpp:701
Representation of a NetCDF group.
Definition: ERF_NCInterface.H:187
bool has_group(const std::string &) const
Check if a group exists.
Definition: ERF_NCInterface.cpp:531
bool has_attr(const std::string &) const
Check if an attribute exists.
Definition: ERF_NCInterface.cpp:549
std::string get_attr(const std::string &name) const
Definition: ERF_NCInterface.cpp:581
void exit_def_mode() const
Exit definition mode.
Definition: ERF_NCInterface.cpp:665
std::string name() const
Name of this group.
Definition: ERF_NCInterface.cpp:442
int num_dimensions() const
Number of dimensions in this group.
Definition: ERF_NCInterface.cpp:510
int num_variables() const
Number of variables within this group.
Definition: ERF_NCInterface.cpp:524
int num_groups() const
Number of sub-groups within this group.
Definition: ERF_NCInterface.cpp:503
std::string full_name() const
Full name for this group.
Definition: ERF_NCInterface.cpp:452
NCGroup(const int id, const NCGroup *)
Definition: ERF_NCInterface.H:277
NCGroup group(const std::string &) const
Definition: ERF_NCInterface.cpp:462
NCVar var(const std::string &) const
Get the variable instance by name.
Definition: ERF_NCInterface.cpp:496
std::vector< NCGroup > all_groups() const
Return a list of all groups defined in this group.
Definition: ERF_NCInterface.cpp:617
const int ncid
Identifier used with NetCDF API calls.
Definition: ERF_NCInterface.H:190
void def_dim(const std::string &, size_t len) const
Define new dimension.
Definition: ERF_NCInterface.cpp:476
std::vector< NCDim > all_dims() const
Return a list of all dimensions defined in this group.
Definition: ERF_NCInterface.cpp:632
NCGroup(const int id)
Definition: ERF_NCInterface.H:276
bool has_var(const std::string &) const
Check if a variable exists by name.
Definition: ERF_NCInterface.cpp:543
bool has_dim(const std::string &) const
Check if a dimension exists by name.
Definition: ERF_NCInterface.cpp:537
void put_attr(const std::string &name, const std::string &value) const
Definition: ERF_NCInterface.cpp:557
void def_array(const std::string &name, nc_type dtype, const std::vector< std::string > &) const
Define an array.
Definition: ERF_NCInterface.cpp:483
NCDim dim(const std::string &) const
Get the dimension instance by name.
Definition: ERF_NCInterface.cpp:469
void 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: ERF_NCInterface.H:243
void enter_def_mode() const
Enter definition mode (not needed for NetCDF4 format)
Definition: ERF_NCInterface.cpp:654
int num_attributes() const
Number of attributes within this group.
Definition: ERF_NCInterface.cpp:517
std::vector< NCVar > all_vars() const
Return a list of all variables defined in this group.
Definition: ERF_NCInterface.cpp:643
Definition: ERF_NCInterface.cpp:8
Wrapper around NetCDF data types.
Definition: ERF_NCInterface.H:24
double RType
Definition: ERF_NCInterface.H:31
static constexpr nc_type Real
Definition: ERF_NCInterface.H:30
static constexpr nc_type Int
Definition: ERF_NCInterface.H:25
Representation of NetCDF dimension.
Definition: ERF_NCInterface.H:37
const int dimid
Dimension ID used with NetCDF API.
Definition: ERF_NCInterface.H:42
const int ncid
File/Group Identifier.
Definition: ERF_NCInterface.H:39
std::string name() const
Name of this dimension.
Definition: ERF_NCInterface.cpp:31
size_t len() const
Length of this dimension.
Definition: ERF_NCInterface.cpp:40
Representation of a NetCDF variable.
Definition: ERF_NCInterface.H:53
bool has_attr(const std::string &name) const
Definition: ERF_NCInterface.cpp:370
const int ncid
File/Group identifier.
Definition: ERF_NCInterface.H:55
const int varid
Variable ID used with NetCDF API.
Definition: ERF_NCInterface.H:58
std::string name() const
Name of this variable.
Definition: ERF_NCInterface.cpp:50
void par_access(int cmode) const
Definition: ERF_NCInterface.cpp:437
std::string get_attr(const std::string &name) const
Definition: ERF_NCInterface.cpp:402
void put(const double *) const
Write out the entire variable.
Definition: ERF_NCInterface.cpp:89
void put_attr(const std::string &name, const std::string &value) const
Definition: ERF_NCInterface.cpp:378
void get(double *) const
Read the entire variable from file.
Definition: ERF_NCInterface.cpp:247
std::vector< size_t > shape() const
Shape of the array (size in each array dimension)
Definition: ERF_NCInterface.cpp:69
int ndim() const
Number of array dimensions for this variable.
Definition: ERF_NCInterface.cpp:59