REMORA
Energy Research and Forecasting: An Atmospheric Modeling Code
ncutils::NCGroup Class Reference

Representation of a NetCDF group. More...

#include <NCInterface.H>

Inheritance diagram for ncutils::NCGroup:
Collaboration diagram for ncutils::NCGroup:

Public Member Functions

std::string name () const
 Name of this group. More...
 
std::string full_name () const
 Full name for this group. More...
 
int num_groups () const
 Number of sub-groups within this group. More...
 
int num_dimensions () const
 Number of dimensions in this group. More...
 
int num_variables () const
 Number of variables within this group. More...
 
int num_attributes () const
 Number of attributes within this group. More...
 
bool has_group (const std::string &) const
 Check if a group exists. More...
 
bool has_dim (const std::string &) const
 Check if a dimension exists by name. More...
 
bool has_var (const std::string &) const
 Check if a variable exists by name. More...
 
bool has_attr (const std::string &) const
 Check if an attribute exists. More...
 
NCGroup group (const std::string &) const
 
NCDim dim (const std::string &) const
 Get the dimension instance by name. More...
 
NCVar var (const std::string &) const
 Get the variable instance by name. More...
 
NCGroup def_group (const std::string &) const
 Define new group. More...
 
NCDim def_dim (const std::string &, const size_t len) const
 Define new dimension. More...
 
NCVar def_scalar (const std::string &name, const nc_type dtype) const
 Define a scalar variable, i.e., 0-dimensional array. More...
 
NCVar def_array (const std::string &name, const nc_type dtype, const std::vector< std::string > &) const
 Define an array. More...
 
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) More...
 
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
 
std::vector< NCGroupall_groups () const
 Return a list of all groups defined in this group. More...
 
std::vector< NCDimall_dims () const
 Return a list of all dimensions defined in this group. More...
 
std::vector< NCVarall_vars () const
 Return a list of all variables defined in this group. More...
 
void enter_def_mode () const
 Enter definition mode (not needed for NetCDF4 format) More...
 
void exit_def_mode () const
 Exit definition mode. More...
 

Public Attributes

const int ncid
 Identifier used with NetCDF API calls. More...
 

Protected Member Functions

 NCGroup (const int id)
 
 NCGroup (const int id, const NCGroup *)
 

Detailed Description

Representation of a NetCDF group.

Constructor & Destructor Documentation

◆ NCGroup() [1/2]

ncutils::NCGroup::NCGroup ( const int  id)
inlineprotected
276 : ncid(id) {}
const int ncid
Identifier used with NetCDF API calls.
Definition: NCInterface.H:182

Referenced by def_group(), and group().

Here is the caller graph for this function:

◆ NCGroup() [2/2]

ncutils::NCGroup::NCGroup ( const int  id,
const NCGroup  
)
inlineprotected
277 : ncid(id) {}

Member Function Documentation

◆ all_dims()

std::vector< NCDim > ncutils::NCGroup::all_dims ( ) const

Return a list of all dimensions defined in this group.

536 {
537  std::vector<NCDim> adims;
538  int ndims = num_dimensions();
539  adims.reserve(ndims);
540  for (int i = 0; i < ndims; ++i) {
541  adims.emplace_back(NCDim{ncid, i});
542  }
543  return adims;
544 }
int num_dimensions() const
Number of dimensions in this group.
Definition: NCInterface.cpp:408
Here is the call graph for this function:

◆ all_groups()

std::vector< NCGroup > ncutils::NCGroup::all_groups ( ) const

Return a list of all groups defined in this group.

521 {
522  std::vector<NCGroup> grps;
523  int ngrps = num_groups();
524 
525  // Empty list of groups return early without error
526  if (ngrps < 1) return grps;
527 
528  std::vector<int> gids(ngrps);
529  check_nc_error(nc_inq_grps(ncid, &ngrps, gids.data()));
530  grps.reserve(ngrps);
531  for (int i = 0; i < ngrps; ++i) grps.emplace_back(NCGroup(gids[i], this));
532  return grps;
533 }
int num_groups() const
Number of sub-groups within this group.
Definition: NCInterface.cpp:401
NCGroup(const int id)
Definition: NCInterface.H:276
Here is the call graph for this function:

◆ all_vars()

std::vector< NCVar > ncutils::NCGroup::all_vars ( ) const

Return a list of all variables defined in this group.

547 {
548  std::vector<NCVar> avars;
549  int nvars = num_variables();
550  avars.reserve(nvars);
551  for (int i = 0; i < nvars; ++i) {
552  avars.emplace_back(NCVar{ncid, i});
553  }
554  return avars;
555 }
int num_variables() const
Number of variables within this group.
Definition: NCInterface.cpp:422
Here is the call graph for this function:

◆ def_array()

NCVar ncutils::NCGroup::def_array ( const std::string &  name,
const nc_type  dtype,
const std::vector< std::string > &  dnames 
) const

Define an array.

381 {
382  int newid;
383  int ndims = dnames.size();
384  std::vector<int> dimids(ndims);
385  for (int i = 0; i < ndims; ++i) {
386  dimids[i] = dim(dnames[i]).dimid;
387  }
388 
389  check_nc_error(
390  nc_def_var(ncid, name.data(), dtype, ndims, dimids.data(), &newid));
391  return NCVar{ncid, newid};
392 }
std::string name() const
Name of this group.
Definition: NCInterface.cpp:322
NCDim dim(const std::string &) const
Get the dimension instance by name.
Definition: NCInterface.cpp:356
const int dimid
Dimension ID used with NetCDF API.
Definition: NCInterface.H:42

Referenced by def_var().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ def_dim()

NCDim ncutils::NCGroup::def_dim ( const std::string &  name,
const size_t  len 
) const

Define new dimension.

364 {
365  int newid;
366  check_nc_error(nc_def_dim(ncid, name.data(), len, &newid));
367  return NCDim{ncid, newid};
368 }
Here is the call graph for this function:

◆ def_group()

NCGroup ncutils::NCGroup::def_group ( const std::string &  name) const

Define new group.

343 {
344  int newid;
345  check_nc_error(nc_def_grp(ncid, name.data(), &newid));
346  return NCGroup(newid, this);
347 }
Here is the call graph for this function:

◆ def_scalar()

NCVar ncutils::NCGroup::def_scalar ( const std::string &  name,
const nc_type  dtype 
) const

Define a scalar variable, i.e., 0-dimensional array.

371 {
372  int newid;
373  check_nc_error(nc_def_var(ncid, name.data(), dtype, 0, NULL, &newid));
374  return NCVar{ncid, newid};
375 }
Here is the call graph for this function:

◆ def_var()

NCVar ncutils::NCGroup::def_var ( const std::string &  name,
const nc_type  dtype,
const std::vector< std::string > &  dnames 
) const
inline

Define a variable (wrapper for def_array)

246  {
247  return def_array(name, dtype, dnames);
248  }
NCVar def_array(const std::string &name, const nc_type dtype, const std::vector< std::string > &) const
Define an array.
Definition: NCInterface.cpp:377
Here is the call graph for this function:

◆ dim()

NCDim ncutils::NCGroup::dim ( const std::string &  name) const

Get the dimension instance by name.

357 {
358  int newid;
359  check_nc_error(nc_inq_dimid(ncid, name.data(), &newid));
360  return NCDim{ncid, newid};
361 }

Referenced by def_array().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ enter_def_mode()

void ncutils::NCGroup::enter_def_mode ( ) const

Enter definition mode (not needed for NetCDF4 format)

558 {
559  int ierr;
560  ierr = nc_redef(ncid);
561 
562  // Ignore already in define mode error
563  if (ierr == NC_EINDEFINE) return;
564  // Handle all other errors
565  check_nc_error(ierr);
566 }

◆ exit_def_mode()

void ncutils::NCGroup::exit_def_mode ( ) const

Exit definition mode.

568 { check_nc_error(nc_enddef(ncid)); }

◆ full_name()

std::string ncutils::NCGroup::full_name ( ) const

Full name for this group.

333 {
334  size_t nlen;
335  std::vector<char> grpname;
336  check_nc_error(nc_inq_grpname_full(ncid, &nlen, NULL));
337  grpname.resize(nlen);
338  check_nc_error(nc_inq_grpname_full(ncid, &nlen, grpname.data()));
339  return std::string{grpname.begin(), grpname.end()};
340 }

◆ get_attr() [1/4]

std::string ncutils::NCGroup::get_attr ( const std::string &  name) const
483 {
484  size_t lenp;
485  std::vector<char> aval;
486  check_nc_error(nc_inq_attlen(ncid, NC_GLOBAL, name.data(), &lenp));
487  aval.resize(lenp);
488  check_nc_error(nc_get_att_text(ncid, NC_GLOBAL, name.data(), aval.data()));
489  return std::string{aval.begin(), aval.end()};
490 }
Here is the call graph for this function:

◆ get_attr() [2/4]

void ncutils::NCGroup::get_attr ( const std::string &  name,
std::vector< double > &  value 
) const
494 {
495  size_t lenp;
496  check_nc_error(nc_inq_attlen(ncid, NC_GLOBAL, name.data(), &lenp));
497  values.resize(lenp);
498  check_nc_error(
499  nc_get_att_double(ncid, NC_GLOBAL, name.data(), values.data()));
500 }
Here is the call graph for this function:

◆ get_attr() [3/4]

void ncutils::NCGroup::get_attr ( const std::string &  name,
std::vector< float > &  value 
) const
504 {
505  size_t lenp;
506  check_nc_error(nc_inq_attlen(ncid, NC_GLOBAL, name.data(), &lenp));
507  values.resize(lenp);
508  check_nc_error(
509  nc_get_att_float(ncid, NC_GLOBAL, name.data(), values.data()));
510 }
Here is the call graph for this function:

◆ get_attr() [4/4]

void ncutils::NCGroup::get_attr ( const std::string &  name,
std::vector< int > &  value 
) const
513 {
514  size_t lenp;
515  check_nc_error(nc_inq_attlen(ncid, NC_GLOBAL, name.data(), &lenp));
516  values.resize(lenp);
517  check_nc_error(nc_get_att_int(ncid, NC_GLOBAL, name.data(), values.data()));
518 }
Here is the call graph for this function:

◆ group()

NCGroup ncutils::NCGroup::group ( const std::string &  name) const

Get the group by name

Throws error if the group doesn't exist, use has_group to check

350 {
351  int newid;
352  check_nc_error(nc_inq_ncid(ncid, name.data(), &newid));
353  return NCGroup(newid, this);
354 }
Here is the call graph for this function:

◆ has_attr()

bool ncutils::NCGroup::has_attr ( const std::string &  name) const

Check if an attribute exists.

448 {
449  int ierr;
450  size_t lenp;
451  ierr = nc_inq_att(ncid, NC_GLOBAL, name.data(), NULL, &lenp);
452  return (ierr == NC_NOERR);
453 }
Here is the call graph for this function:

◆ has_dim()

bool ncutils::NCGroup::has_dim ( const std::string &  name) const

Check if a dimension exists by name.

436 {
437  int ierr = nc_inq_dimid(ncid, name.data(), NULL);
438  return (ierr == NC_NOERR);
439 }
Here is the call graph for this function:

◆ has_group()

bool ncutils::NCGroup::has_group ( const std::string &  name) const

Check if a group exists.

430 {
431  int ierr = nc_inq_ncid(ncid, name.data(), NULL);
432  return (ierr == NC_NOERR);
433 }
Here is the call graph for this function:

◆ has_var()

bool ncutils::NCGroup::has_var ( const std::string &  name) const

Check if a variable exists by name.

442 {
443  int ierr = nc_inq_varid(ncid, name.data(), NULL);
444  return (ierr == NC_NOERR);
445 }
Here is the call graph for this function:

◆ name()

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

Name of this group.

323 {
324  size_t nlen;
325  std::vector<char> grpname;
326  check_nc_error(nc_inq_grpname_len(ncid, &nlen));
327  grpname.resize(nlen + 1);
328  check_nc_error(nc_inq_grpname(ncid, grpname.data()));
329  return std::string{grpname.begin(), grpname.end()};
330 }

Referenced by ncutils::NCFile::create(), ncutils::NCFile::create_par(), def_array(), def_dim(), def_group(), def_scalar(), def_var(), dim(), get_attr(), group(), has_attr(), has_dim(), has_group(), has_var(), ncutils::NCFile::open(), ncutils::NCFile::open_par(), put_attr(), and var().

Here is the caller graph for this function:

◆ num_attributes()

int ncutils::NCGroup::num_attributes ( ) const

Number of attributes within this group.

416 {
417  int nattrs;
418  check_nc_error(nc_inq(ncid, NULL, NULL, &nattrs, NULL));
419  return nattrs;
420 }

◆ num_dimensions()

int ncutils::NCGroup::num_dimensions ( ) const

Number of dimensions in this group.

409 {
410  int ndims;
411  check_nc_error(nc_inq(ncid, &ndims, NULL, NULL, NULL));
412  return ndims;
413 }

Referenced by all_dims().

Here is the caller graph for this function:

◆ num_groups()

int ncutils::NCGroup::num_groups ( ) const

Number of sub-groups within this group.

402 {
403  int ngrps;
404  check_nc_error(nc_inq_grps(ncid, &ngrps, NULL));
405  return ngrps;
406 }

Referenced by all_groups().

Here is the caller graph for this function:

◆ num_variables()

int ncutils::NCGroup::num_variables ( ) const

Number of variables within this group.

423 {
424  int nvars;
425  check_nc_error(nc_inq(ncid, NULL, &nvars, NULL, NULL));
426  return nvars;
427 }

Referenced by all_vars().

Here is the caller graph for this function:

◆ put_attr() [1/4]

void ncutils::NCGroup::put_attr ( const std::string &  name,
const std::string &  value 
) const
456 {
457  check_nc_error(nc_put_att_text(
458  ncid, NC_GLOBAL, name.data(), value.size(), value.data()));
459 }
Here is the call graph for this function:

◆ put_attr() [2/4]

void ncutils::NCGroup::put_attr ( const std::string &  name,
const std::vector< double > &  value 
) const
463 {
464  check_nc_error(nc_put_att_double(
465  ncid, NC_GLOBAL, name.data(), NC_DOUBLE, value.size(), value.data()));
466 }
Here is the call graph for this function:

◆ put_attr() [3/4]

void ncutils::NCGroup::put_attr ( const std::string &  name,
const std::vector< float > &  value 
) const
470 {
471  check_nc_error(nc_put_att_float(
472  ncid, NC_GLOBAL, name.data(), NC_FLOAT, value.size(), value.data()));
473 }
Here is the call graph for this function:

◆ put_attr() [4/4]

void ncutils::NCGroup::put_attr ( const std::string &  name,
const std::vector< int > &  value 
) const
477 {
478  check_nc_error(nc_put_att_int(
479  ncid, NC_GLOBAL, name.data(), NC_INT, value.size(), value.data()));
480 }
Here is the call graph for this function:

◆ var()

NCVar ncutils::NCGroup::var ( const std::string &  name) const

Get the variable instance by name.

395 {
396  int varid;
397  check_nc_error(nc_inq_varid(ncid, name.data(), &varid));
398  return NCVar{ncid, varid};
399 }
Here is the call graph for this function:

Member Data Documentation

◆ ncid


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