ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
ncutils::NCVar Struct Reference

Representation of a NetCDF variable. More...

#include <ERF_NCInterface.H>

Collaboration diagram for ncutils::NCVar:

Public Member Functions

std::string name () const
 Name of this variable. More...
 
int ndim () const
 Number of array dimensions for this variable. More...
 
std::vector< size_t > shape () const
 Shape of the array (size in each array dimension) More...
 
std::vector< std::string > dimnames () const
 Dimension names of the array (str in each array dimension) More...
 
void put (const double *) const
 Write out the entire variable. More...
 
void put (const float *) const
 
void put (const int *) const
 
void put (const double *, const std::vector< size_t > &, const std::vector< size_t > &) const
 Write out a slice of data. More...
 
void put (const double *, const std::vector< size_t > &, const std::vector< size_t > &, const std::vector< ptrdiff_t > &) const
 
void put (const float *, const std::vector< size_t > &, const std::vector< size_t > &) const
 Write out a slice of data. More...
 
void put (const float *, const std::vector< size_t > &, const std::vector< size_t > &, const std::vector< ptrdiff_t > &) const
 
void put (const int *, const std::vector< size_t > &, const std::vector< size_t > &) const
 
void put (const int *, const std::vector< size_t > &, const std::vector< size_t > &, const std::vector< ptrdiff_t > &) const
 
void put (const char **, const std::vector< size_t > &, const std::vector< size_t > &) const
 
void put (const char **dptr, const std::vector< size_t > &start, const std::vector< size_t > &count, const std::vector< ptrdiff_t > &stride) const
 
void get (double *) const
 Read the entire variable from file. More...
 
void get (float *) const
 
void get (int *) const
 
void get (double *, const std::vector< size_t > &, const std::vector< size_t > &) const
 Read a chunk of data from the file. More...
 
void get (double *, const std::vector< size_t > &, const std::vector< size_t > &, const std::vector< ptrdiff_t > &) const
 Read a chunk of data with strides. More...
 
void get (float *, const std::vector< size_t > &, const std::vector< size_t > &) const
 Read a chunk of data from the file. More...
 
void get (float *, const std::vector< size_t > &, const std::vector< size_t > &, const std::vector< ptrdiff_t > &) const
 Read a chunk of data with strides. More...
 
void get (int *, const std::vector< size_t > &, const std::vector< size_t > &) const
 
void get (int *, const std::vector< size_t > &, const std::vector< size_t > &, const std::vector< ptrdiff_t > &) const
 
void get (char *, const std::vector< size_t > &, const std::vector< size_t > &) const
 Read a chunk of data from the file. More...
 
void get (char *, const std::vector< size_t > &, const std::vector< size_t > &, const std::vector< ptrdiff_t > &) const
 Read a chunk of data with strides. More...
 
bool has_attr (const std::string &name) const
 
void put_attr (const std::string &name, const std::string &value) const
 
void put_attr (const std::string &name, const std::vector< double > &value) const
 
void put_attr (const std::string &name, const std::vector< float > &value) const
 
void put_attr (const std::string &name, const std::vector< int > &value) const
 
std::string get_attr (const std::string &name) const
 
void get_attr (const std::string &name, std::vector< double > &value) const
 
void get_attr (const std::string &name, std::vector< float > &value) const
 
void get_attr (const std::string &name, std::vector< int > &value) const
 
void par_access (int cmode) const
 

Public Attributes

const int ncid
 File/Group identifier. More...
 
const int varid
 Variable ID used with NetCDF API. More...
 

Detailed Description

Representation of a NetCDF variable.

Member Function Documentation

◆ dimnames()

std::vector< std::string > ncutils::NCVar::dimnames ( ) const

Dimension names of the array (str in each array dimension)

Error-checking function to get the name of each dimension from a NetCDF identity

71 {
72  int ndims = ndim();
73  std::vector<int> dimids(ndims);
74  std::vector<std::string> vnames(ndims);
75 
76  for (int i = 0; i < ndims; ++i)
77  check_nc_error(nc_inq_vardimid(ncid, varid, dimids.data()));
78 
79  char buf[80];
80  for (int i = 0; i < ndims; ++i) {
81  check_nc_error(nc_inq_dimname(ncid, dimids[i], buf));
82  vnames[i] = std::string(buf);
83  }
84 
85  return vnames;
86 }
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
int ndim() const
Number of array dimensions for this variable.
Definition: ERF_NCInterface.cpp:60
Here is the call graph for this function:

◆ get() [1/11]

void ncutils::NCVar::get ( char *  dptr,
const std::vector< size_t > &  start,
const std::vector< size_t > &  count 
) const

Read a chunk of data from the file.

378 {
379  check_nc_error(
380  nc_get_vara_text(ncid, varid, start.data(), count.data(), dptr));
381 }

◆ get() [2/11]

void ncutils::NCVar::get ( char *  dptr,
const std::vector< size_t > &  start,
const std::vector< size_t > &  count,
const std::vector< ptrdiff_t > &  stride 
) const

Read a chunk of data with strides.

387 {
388  check_nc_error(nc_get_vars_text(
389  ncid, varid, start.data(), count.data(), stride.data(), dptr));
390 }

◆ get() [3/11]

void ncutils::NCVar::get ( double *  ptr) const

Read the entire variable from file.

Error-checking wrapper for NetCDF function nc_get_var_double

Parameters
ptrPointer to the data location we use to get
270 {
271  check_nc_error(nc_get_var_double(ncid, varid, ptr));
272 }

◆ get() [4/11]

void ncutils::NCVar::get ( double *  dptr,
const std::vector< size_t > &  start,
const std::vector< size_t > &  count 
) const

Read a chunk of data from the file.

Error-checking wrapper for NetCDF function nc_get_vara_double

Parameters
dptrPointer to the data location for get
startStarting indices
countCount sizes
304 {
305  check_nc_error(
306  nc_get_vara_double(ncid, varid, start.data(), count.data(), dptr));
307 }

◆ get() [5/11]

void ncutils::NCVar::get ( double *  dptr,
const std::vector< size_t > &  start,
const std::vector< size_t > &  count,
const std::vector< ptrdiff_t > &  stride 
) const

Read a chunk of data with strides.

Error-checking wrapper for NetCDF function nc_get_vars_double

Parameters
dptrPointer to the data location for get
startStarting indices
countCount sizes
strideStride length for the data
321 {
322  check_nc_error(nc_get_vars_double(
323  ncid, varid, start.data(), count.data(), stride.data(), dptr));
324 }

◆ get() [6/11]

void ncutils::NCVar::get ( float *  ptr) const

Error-checking wrapper for NetCDF function nc_get_var_float

Parameters
ptrPointer to the data location we use to get
280 {
281  check_nc_error(nc_get_var_float(ncid, varid, ptr));
282 }

◆ get() [7/11]

void ncutils::NCVar::get ( float *  dptr,
const std::vector< size_t > &  start,
const std::vector< size_t > &  count 
) const

Read a chunk of data from the file.

Error-checking wrapper for NetCDF function nc_get_vara_float

Parameters
dptrPointer to the data location for get
startStarting indices
countCount sizes
336 {
337  check_nc_error(
338  nc_get_vara_float(ncid, varid, start.data(), count.data(), dptr));
339 }

◆ get() [8/11]

void ncutils::NCVar::get ( float *  dptr,
const std::vector< size_t > &  start,
const std::vector< size_t > &  count,
const std::vector< ptrdiff_t > &  stride 
) const

Read a chunk of data with strides.

Error-checking wrapper for NetCDF function nc_get_vars_float

Parameters
dptrPointer to the data location for get
startStarting indices
countCount sizes
strideStride length for the data
353 {
354  check_nc_error(nc_get_vars_float(
355  ncid, varid, start.data(), count.data(), stride.data(), dptr));
356 }

◆ get() [9/11]

void ncutils::NCVar::get ( int *  ptr) const

Error-checking wrapper for NetCDF function nc_get_var_int

Parameters
ptrPointer to the data location we use to get
290 {
291  check_nc_error(nc_get_var_int(ncid, varid, ptr));
292 }

◆ get() [10/11]

void ncutils::NCVar::get ( int *  dptr,
const std::vector< size_t > &  start,
const std::vector< size_t > &  count 
) const
361 {
362  check_nc_error(
363  nc_get_vara_int(ncid, varid, start.data(), count.data(), dptr));
364 }

◆ get() [11/11]

void ncutils::NCVar::get ( int *  dptr,
const std::vector< size_t > &  start,
const std::vector< size_t > &  count,
const std::vector< ptrdiff_t > &  stride 
) const
370 {
371  check_nc_error(nc_get_vars_int(
372  ncid, varid, start.data(), count.data(), stride.data(), dptr));
373 }

◆ get_attr() [1/4]

std::string ncutils::NCVar::get_attr ( const std::string &  name) const
425 {
426  size_t lenp;
427  std::vector<char> aval;
428  check_nc_error(nc_inq_attlen(ncid, varid, name.data(), &lenp));
429  aval.resize(lenp);
430  check_nc_error(nc_get_att_text(ncid, varid, name.data(), aval.data()));
431  return std::string{aval.begin(), aval.end()};
432 }
std::string name() const
Name of this variable.
Definition: ERF_NCInterface.cpp:51
Here is the call graph for this function:

◆ get_attr() [2/4]

void ncutils::NCVar::get_attr ( const std::string &  name,
std::vector< double > &  value 
) const
435 {
436  size_t lenp;
437  check_nc_error(nc_inq_attlen(ncid, varid, name.data(), &lenp));
438  values.resize(lenp);
439  check_nc_error(nc_get_att_double(ncid, varid, name.data(), values.data()));
440 }
Here is the call graph for this function:

◆ get_attr() [3/4]

void ncutils::NCVar::get_attr ( const std::string &  name,
std::vector< float > &  value 
) const
443 {
444  size_t lenp;
445  check_nc_error(nc_inq_attlen(ncid, varid, name.data(), &lenp));
446  values.resize(lenp);
447  check_nc_error(nc_get_att_float(ncid, varid, name.data(), values.data()));
448 }
Here is the call graph for this function:

◆ get_attr() [4/4]

void ncutils::NCVar::get_attr ( const std::string &  name,
std::vector< int > &  value 
) const
451 {
452  size_t lenp;
453  check_nc_error(nc_inq_attlen(ncid, varid, name.data(), &lenp));
454  values.resize(lenp);
455  check_nc_error(nc_get_att_int(ncid, varid, name.data(), values.data()));
456 }
Here is the call graph for this function:

◆ has_attr()

bool ncutils::NCVar::has_attr ( const std::string &  name) const
393 {
394  int ierr;
395  size_t lenp;
396  ierr = nc_inq_att(ncid, varid, name.data(), nullptr, &lenp);
397  return (ierr == NC_NOERR);
398 }
Here is the call graph for this function:

◆ name()

std::string ncutils::NCVar::name ( ) const

Name of this variable.

Error-checking wrapper for NetCDF function nc_inq_varname

52 {
53  check_nc_error(nc_inq_varname(ncid, varid, recname));
54  return std::string(recname);
55 }

Referenced by get_attr(), has_attr(), and put_attr().

Here is the caller graph for this function:

◆ ndim()

int ncutils::NCVar::ndim ( ) const

Number of array dimensions for this variable.

Error-checking wrapper for NetCDF function nc_inq_varndims

61 {
62  int ndims;
63  check_nc_error(nc_inq_varndims(ncid, varid, &ndims));
64  return ndims;
65 }

Referenced by dimnames(), and shape().

Here is the caller graph for this function:

◆ par_access()

void ncutils::NCVar::par_access ( int  cmode) const
460 {
461  check_nc_error(nc_var_par_access(ncid, varid, cmode));
462 }

◆ put() [1/11]

void ncutils::NCVar::put ( const char **  dptr,
const std::vector< size_t > &  start,
const std::vector< size_t > &  count 
) const

Error-checking wrapper for NetCDF function nc_put_vara_string

Parameters
dptrPointer to the data to put
startStarting indices
countCount sizes
242 {
243  check_nc_error(
244  nc_put_vara_string(ncid, varid, start.data(), count.data(), dptr));
245 }

◆ put() [2/11]

void ncutils::NCVar::put ( const char **  dptr,
const std::vector< size_t > &  start,
const std::vector< size_t > &  count,
const std::vector< ptrdiff_t > &  stride 
) const

Error-checking wrapper for NetCDF function nc_put_vars_string

Parameters
dptrPointer to the data to put
startStarting indices
countCount sizes
strideStride length for the data
259 {
260  check_nc_error(nc_put_vars_string(
261  ncid, varid, start.data(), count.data(), stride.data(), dptr));
262 }

◆ put() [3/11]

void ncutils::NCVar::put ( const double *  ptr) const

Write out the entire variable.

Error-checking wrapper for NetCDF function nc_put_var_double

Parameters
ptrPointer to the data to put
112 {
113  check_nc_error(nc_put_var_double(ncid, varid, ptr));
114 }

◆ put() [4/11]

void ncutils::NCVar::put ( const double *  dptr,
const std::vector< size_t > &  start,
const std::vector< size_t > &  count 
) const

Write out a slice of data.

Error-checking wrapper for NetCDF function nc_put_vara_double

Parameters
dptrPointer to the data to put
startStarting indices
countCount sizes
146 {
147  check_nc_error(
148  nc_put_vara_double(ncid, varid, start.data(), count.data(), dptr));
149 }

◆ put() [5/11]

void ncutils::NCVar::put ( const double *  dptr,
const std::vector< size_t > &  start,
const std::vector< size_t > &  count,
const std::vector< ptrdiff_t > &  stride 
) const

Write out a slice of data with with strides (see hyperslab definition in NetCDF)

Error-checking wrapper for NetCDF function nc_put_vars_double

Parameters
dptrPointer to the data to put
startStarting indices
countCount sizes
strideStride length for the data
163 {
164  check_nc_error(nc_put_vars_double(
165  ncid, varid, start.data(), count.data(), stride.data(), dptr));
166 }

◆ put() [6/11]

void ncutils::NCVar::put ( const float *  ptr) const

Error-checking wrapper for NetCDF function nc_put_var_float

Parameters
ptrPointer to the data to put
122 {
123  check_nc_error(nc_put_var_float(ncid, varid, ptr));
124 }

◆ put() [7/11]

void ncutils::NCVar::put ( const float *  dptr,
const std::vector< size_t > &  start,
const std::vector< size_t > &  count 
) const

Write out a slice of data.

Error-checking wrapper for NetCDF function nc_put_vara_float

Parameters
dptrPointer to the data to put
startStarting indices
countCount sizes
178 {
179  check_nc_error(
180  nc_put_vara_float(ncid, varid, start.data(), count.data(), dptr));
181 }

◆ put() [8/11]

void ncutils::NCVar::put ( const float *  dptr,
const std::vector< size_t > &  start,
const std::vector< size_t > &  count,
const std::vector< ptrdiff_t > &  stride 
) const

Write out a slice of data with with strides (see hyperslab definition in NetCDF)

Error-checking wrapper for NetCDF function nc_put_vars_float

Parameters
dptrPointer to the data to put
startStarting indices
countCount sizes
strideStride length for the data
195 {
196  check_nc_error(nc_put_vars_float(
197  ncid, varid, start.data(), count.data(), stride.data(), dptr));
198 }

◆ put() [9/11]

void ncutils::NCVar::put ( const int *  ptr) const

Error-checking wrapper for NetCDF function nc_put_var_int

Parameters
ptrPointer to the data to put
132 {
133  check_nc_error(nc_put_var_int(ncid, varid, ptr));
134 }

◆ put() [10/11]

void ncutils::NCVar::put ( const int *  dptr,
const std::vector< size_t > &  start,
const std::vector< size_t > &  count 
) const

Error-checking wrapper for NetCDF function nc_put_vara_int

Parameters
dptrPointer to the data to put
startStarting indices
countCount sizes
210 {
211  check_nc_error(
212  nc_put_vara_int(ncid, varid, start.data(), count.data(), dptr));
213 }

◆ put() [11/11]

void ncutils::NCVar::put ( const int *  dptr,
const std::vector< size_t > &  start,
const std::vector< size_t > &  count,
const std::vector< ptrdiff_t > &  stride 
) const

Error-checking wrapper for NetCDF function nc_put_vars_int

Parameters
dptrPointer to the data to put
startStarting indices
countCount sizes
strideStride length for the data
227 {
228  check_nc_error(nc_put_vars_int(
229  ncid, varid, start.data(), count.data(), stride.data(), dptr));
230 }

◆ put_attr() [1/4]

void ncutils::NCVar::put_attr ( const std::string &  name,
const std::string &  value 
) const
401 {
402  check_nc_error(
403  nc_put_att_text(ncid, varid, name.data(), value.size(), value.data()));
404 }
Here is the call graph for this function:

◆ put_attr() [2/4]

void ncutils::NCVar::put_attr ( const std::string &  name,
const std::vector< double > &  value 
) const
407 {
408  check_nc_error(nc_put_att_double(
409  ncid, varid, name.data(), NC_DOUBLE, value.size(), value.data()));
410 }
Here is the call graph for this function:

◆ put_attr() [3/4]

void ncutils::NCVar::put_attr ( const std::string &  name,
const std::vector< float > &  value 
) const
413 {
414  check_nc_error(nc_put_att_float(
415  ncid, varid, name.data(), NC_FLOAT, value.size(), value.data()));
416 }
Here is the call graph for this function:

◆ put_attr() [4/4]

void ncutils::NCVar::put_attr ( const std::string &  name,
const std::vector< int > &  value 
) const
419 {
420  check_nc_error(nc_put_att_int(
421  ncid, varid, name.data(), NC_INT, value.size(), value.data()));
422 }
Here is the call graph for this function:

◆ shape()

std::vector< size_t > ncutils::NCVar::shape ( ) const

Shape of the array (size in each array dimension)

Error-checking function to get the length of each dimension from a NetCDF identity

92 {
93  int ndims = ndim();
94  std::vector<int> dimids(ndims);
95  std::vector<size_t> vshape(ndims);
96 
97  for (int i = 0; i < ndims; ++i)
98  check_nc_error(nc_inq_vardimid(ncid, varid, dimids.data()));
99 
100  for (int i = 0; i < ndims; ++i)
101  check_nc_error(nc_inq_dimlen(ncid, dimids[i], &vshape[i]));
102 
103  return vshape;
104 }
Here is the call graph for this function:

Member Data Documentation

◆ ncid

const int ncutils::NCVar::ncid

File/Group identifier.

Referenced by dimnames(), get(), get_attr(), has_attr(), name(), ndim(), par_access(), put(), put_attr(), and shape().

◆ varid

const int ncutils::NCVar::varid

Variable ID used with NetCDF API.

Referenced by dimnames(), get(), get_attr(), has_attr(), name(), ndim(), par_access(), put(), put_attr(), and shape().


The documentation for this struct was generated from the following files: