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

Representation of a NetCDF group. More...

#include <ERF_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...
 
void def_dim (const std::string &, size_t len) const
 Define new dimension. More...
 
void def_array (const std::string &name, nc_type dtype, const std::vector< std::string > &) const
 Define an array. More...
 
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) 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: ERF_NCInterface.H:190

Referenced by 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.

633 {
634  std::vector<NCDim> adims;
635  int ndims = num_dimensions();
636  adims.reserve(ndims);
637  for (int i = 0; i < ndims; ++i) {
638  adims.emplace_back(NCDim{ncid, i});
639  }
640  return adims;
641 }
int num_dimensions() const
Number of dimensions in this group.
Definition: ERF_NCInterface.cpp:510
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.

618 {
619  std::vector<NCGroup> grps;
620  int ngrps = num_groups();
621 
622  // Empty list of groups return early without error
623  if (ngrps < 1) return grps;
624 
625  std::vector<int> gids(ngrps);
626  check_nc_error(nc_inq_grps(ncid, &ngrps, gids.data()));
627  grps.reserve(ngrps);
628  for (int i = 0; i < ngrps; ++i) grps.emplace_back(NCGroup(gids[i], this));
629  return grps;
630 }
int num_groups() const
Number of sub-groups within this group.
Definition: ERF_NCInterface.cpp:503
NCGroup(const int id)
Definition: ERF_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.

644 {
645  std::vector<NCVar> avars;
646  int nvars = num_variables();
647  avars.reserve(nvars);
648  for (int i = 0; i < nvars; ++i) {
649  avars.emplace_back(NCVar{ncid, i});
650  }
651  return avars;
652 }
@ nvars
Definition: ERF_DataStruct.H:70
int num_variables() const
Number of variables within this group.
Definition: ERF_NCInterface.cpp:524
Here is the call graph for this function:

◆ def_array()

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

Define an array.

486 {
487  int newid;
488  int ndims = dnames.size();
489  std::vector<int> dimids(ndims);
490  for (int i = 0; i < ndims; ++i) dimids[i] = dim(dnames[i]).dimid;
491 
492  check_nc_error(
493  nc_def_var(ncid, name.data(), dtype, ndims, dimids.data(), &newid));
494 }
std::string name() const
Name of this group.
Definition: ERF_NCInterface.cpp:442
NCDim dim(const std::string &) const
Get the dimension instance by name.
Definition: ERF_NCInterface.cpp:469
const int dimid
Dimension ID used with NetCDF API.
Definition: ERF_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()

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

Define new dimension.

477 {
478  int newid;
479  check_nc_error(nc_def_dim(ncid, name.data(), len, &newid));
480  // NCDim{ncid, newid};
481 }
Here is the call graph for this function:

◆ def_var()

void 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  def_array(name, dtype, dnames);
248  }
void def_array(const std::string &name, nc_type dtype, const std::vector< std::string > &) const
Define an array.
Definition: ERF_NCInterface.cpp:483
Here is the call graph for this function:

◆ dim()

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

Get the dimension instance by name.

470 {
471  int newid;
472  check_nc_error(nc_inq_dimid(ncid, name.data(), &newid));
473  return NCDim{ncid, newid};
474 }

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)

655 {
656  int ierr;
657  ierr = nc_redef(ncid);
658 
659  // Ignore already in define mode error
660  if (ierr == NC_EINDEFINE) return;
661  // Handle all other errors
662  check_nc_error(ierr);
663 }

◆ exit_def_mode()

void ncutils::NCGroup::exit_def_mode ( ) const

Exit definition mode.

665 { check_nc_error(nc_enddef(ncid)); }

◆ full_name()

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

Full name for this group.

453 {
454  size_t nlen;
455  std::vector<char> grpname;
456  check_nc_error(nc_inq_grpname_full(ncid, &nlen, nullptr));
457  grpname.resize(nlen);
458  check_nc_error(nc_inq_grpname_full(ncid, &nlen, grpname.data()));
459  return std::string{grpname.begin(), grpname.end()};
460 }

◆ get_attr() [1/4]

std::string ncutils::NCGroup::get_attr ( const std::string &  name) const
582 {
583  size_t lenp;
584  std::vector<char> aval;
585  check_nc_error(nc_inq_attlen(ncid, NC_GLOBAL, name.data(), &lenp));
586  aval.resize(lenp);
587  check_nc_error(nc_get_att_text(ncid, NC_GLOBAL, name.data(), aval.data()));
588  return std::string{aval.begin(), aval.end()};
589 }
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
592 {
593  size_t lenp;
594  check_nc_error(nc_inq_attlen(ncid, NC_GLOBAL, name.data(), &lenp));
595  values.resize(lenp);
596  check_nc_error(
597  nc_get_att_double(ncid, NC_GLOBAL, name.data(), values.data()));
598 }
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
601 {
602  size_t lenp;
603  check_nc_error(nc_inq_attlen(ncid, NC_GLOBAL, name.data(), &lenp));
604  values.resize(lenp);
605  check_nc_error(
606  nc_get_att_float(ncid, NC_GLOBAL, name.data(), values.data()));
607 }
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
610 {
611  size_t lenp;
612  check_nc_error(nc_inq_attlen(ncid, NC_GLOBAL, name.data(), &lenp));
613  values.resize(lenp);
614  check_nc_error(nc_get_att_int(ncid, NC_GLOBAL, name.data(), values.data()));
615 }
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

463 {
464  int newid;
465  check_nc_error(nc_inq_ncid(ncid, name.data(), &newid));
466  return NCGroup(newid, this);
467 }
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.

550 {
551  int ierr;
552  size_t lenp;
553  ierr = nc_inq_att(ncid, NC_GLOBAL, name.data(), nullptr, &lenp);
554  return (ierr == NC_NOERR);
555 }
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.

538 {
539  int ierr = nc_inq_dimid(ncid, name.data(), nullptr);
540  return (ierr == NC_NOERR);
541 }
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.

532 {
533  int ierr = nc_inq_ncid(ncid, name.data(), nullptr);
534  return (ierr == NC_NOERR);
535 }
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.

544 {
545  int ierr = nc_inq_varid(ncid, name.data(), nullptr);
546  return (ierr == NC_NOERR);
547 }
Here is the call graph for this function:

◆ name()

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

Name of this group.

443 {
444  size_t nlen;
445  std::vector<char> grpname;
446  check_nc_error(nc_inq_grpname_len(ncid, &nlen));
447  grpname.resize(nlen + 1);
448  check_nc_error(nc_inq_grpname(ncid, grpname.data()));
449  return std::string{grpname.begin(), grpname.end()};
450 }

Referenced by ncutils::NCFile::create(), ncutils::NCFile::create_par(), def_array(), def_dim(), 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.

518 {
519  int nattrs;
520  check_nc_error(nc_inq(ncid, nullptr, nullptr, &nattrs, nullptr));
521  return nattrs;
522 }

◆ num_dimensions()

int ncutils::NCGroup::num_dimensions ( ) const

Number of dimensions in this group.

511 {
512  int ndims;
513  check_nc_error(nc_inq(ncid, &ndims, nullptr, nullptr, nullptr));
514  return ndims;
515 }

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.

504 {
505  int ngrps;
506  check_nc_error(nc_inq_grps(ncid, &ngrps, nullptr));
507  return ngrps;
508 }

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.

525 {
526  int nvars;
527  check_nc_error(nc_inq(ncid, nullptr, &nvars, nullptr, nullptr));
528  return nvars;
529 }

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
558 {
559  check_nc_error(nc_put_att_text(
560  ncid, NC_GLOBAL, name.data(), value.size(), value.data()));
561 }
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
564 {
565  check_nc_error(nc_put_att_double(
566  ncid, NC_GLOBAL, name.data(), NC_DOUBLE, value.size(), value.data()));
567 }
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
570 {
571  check_nc_error(nc_put_att_float(
572  ncid, NC_GLOBAL, name.data(), NC_FLOAT, value.size(), value.data()));
573 }
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
576 {
577  check_nc_error(nc_put_att_int(
578  ncid, NC_GLOBAL, name.data(), NC_INT, value.size(), value.data()));
579 }
Here is the call graph for this function:

◆ var()

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

Get the variable instance by name.

497 {
498  int varid;
499  check_nc_error(nc_inq_varid(ncid, name.data(), &varid));
500  return NCVar{ncid, varid};
501 }
Here is the call graph for this function:

Member Data Documentation

◆ ncid


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