ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
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...
 
int get_id (const std::string &) const
 
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
285 : ncid(id) {}
const int ncid
Identifier used with NetCDF API calls.
Definition: ERF_NCInterface.H:193

Referenced by group().

Here is the caller graph for this function:

◆ NCGroup() [2/2]

ncutils::NCGroup::NCGroup ( const int  id,
const NCGroup  
)
inlineprotected
286 : 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.

666 {
667  std::vector<NCDim> adims;
668  int ndims = num_dimensions();
669  adims.reserve(ndims);
670  for (int i = 0; i < ndims; ++i) {
671  adims.emplace_back(NCDim{ncid, i});
672  }
673  return adims;
674 }
int num_dimensions() const
Number of dimensions in this group.
Definition: ERF_NCInterface.cpp:532
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.

651 {
652  std::vector<NCGroup> grps;
653  int ngrps = num_groups();
654 
655  // Empty list of groups return early without error
656  if (ngrps < 1) return grps;
657 
658  std::vector<int> gids(ngrps);
659  check_nc_error(nc_inq_grps(ncid, &ngrps, gids.data()));
660  grps.reserve(ngrps);
661  for (int i = 0; i < ngrps; ++i) grps.emplace_back(NCGroup(gids[i], this));
662  return grps;
663 }
int num_groups() const
Number of sub-groups within this group.
Definition: ERF_NCInterface.cpp:525
NCGroup(const int id)
Definition: ERF_NCInterface.H:285
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.

677 {
678  std::vector<NCVar> avars;
679  int nvars = num_variables();
680  avars.reserve(nvars);
681  for (int i = 0; i < nvars; ++i) {
682  avars.emplace_back(NCVar{ncid, i});
683  }
684  return avars;
685 }
@ nvars
Definition: ERF_DataStruct.H:74
int num_variables() const
Number of variables within this group.
Definition: ERF_NCInterface.cpp:546
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.

508 {
509  int newid;
510  int ndims = dnames.size();
511  std::vector<int> dimids(ndims);
512  for (int i = 0; i < ndims; ++i) dimids[i] = dim(dnames[i]).dimid;
513 
514  check_nc_error(
515  nc_def_var(ncid, name.data(), dtype, ndims, dimids.data(), &newid));
516 }
std::string name() const
Name of this group.
Definition: ERF_NCInterface.cpp:464
NCDim dim(const std::string &) const
Get the dimension instance by name.
Definition: ERF_NCInterface.cpp:491
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.

499 {
500  int newid;
501  check_nc_error(nc_def_dim(ncid, name.data(), len, &newid));
502  // NCDim{ncid, newid};
503 }
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)

255  {
256  def_array(name, dtype, dnames);
257  }
void def_array(const std::string &name, nc_type dtype, const std::vector< std::string > &) const
Define an array.
Definition: ERF_NCInterface.cpp:505
Here is the call graph for this function:

◆ dim()

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

Get the dimension instance by name.

492 {
493  int newid;
494  check_nc_error(nc_inq_dimid(ncid, name.data(), &newid));
495  return NCDim{ncid, newid};
496 }

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)

688 {
689  int ierr;
690  ierr = nc_redef(ncid);
691 
692  // Ignore already in define mode error
693  if (ierr == NC_EINDEFINE) return;
694  // Handle all other errors
695  check_nc_error(ierr);
696 }

◆ exit_def_mode()

void ncutils::NCGroup::exit_def_mode ( ) const

Exit definition mode.

698 { check_nc_error(nc_enddef(ncid)); }

◆ full_name()

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

Full name for this group.

475 {
476  size_t nlen;
477  std::vector<char> grpname;
478  check_nc_error(nc_inq_grpname_full(ncid, &nlen, nullptr));
479  grpname.resize(nlen);
480  check_nc_error(nc_inq_grpname_full(ncid, &nlen, grpname.data()));
481  return std::string{grpname.begin(), grpname.end()};
482 }

◆ get_attr() [1/4]

std::string ncutils::NCGroup::get_attr ( const std::string &  name) const
615 {
616  size_t lenp;
617  std::vector<char> aval;
618  check_nc_error(nc_inq_attlen(ncid, NC_GLOBAL, name.data(), &lenp));
619  aval.resize(lenp);
620  check_nc_error(nc_get_att_text(ncid, NC_GLOBAL, name.data(), aval.data()));
621  return std::string{aval.begin(), aval.end()};
622 }
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
625 {
626  size_t lenp;
627  check_nc_error(nc_inq_attlen(ncid, NC_GLOBAL, name.data(), &lenp));
628  values.resize(lenp);
629  check_nc_error(
630  nc_get_att_double(ncid, NC_GLOBAL, name.data(), values.data()));
631 }
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
634 {
635  size_t lenp;
636  check_nc_error(nc_inq_attlen(ncid, NC_GLOBAL, name.data(), &lenp));
637  values.resize(lenp);
638  check_nc_error(
639  nc_get_att_float(ncid, NC_GLOBAL, name.data(), values.data()));
640 }
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
643 {
644  size_t lenp;
645  check_nc_error(nc_inq_attlen(ncid, NC_GLOBAL, name.data(), &lenp));
646  values.resize(lenp);
647  check_nc_error(nc_get_att_int(ncid, NC_GLOBAL, name.data(), values.data()));
648 }
Here is the call graph for this function:

◆ get_id()

int ncutils::NCGroup::get_id ( const std::string &  name) const

Return id of variable by name

Throws error if variable does not exist, use 'has_var' to check

566 {
567  int temp_id = 0;
568  int ierr = nc_inq_varid (ncid, name.data(), &temp_id);
569  if (ierr == NC_ENOTVAR) {
570  amrex::Abort("NetCDF variable '" + name + "' does not exist");
571  }
572  return temp_id;
573 }
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

485 {
486  int newid;
487  check_nc_error(nc_inq_ncid(ncid, name.data(), &newid));
488  return NCGroup(newid, this);
489 }
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.

583 {
584  int ierr;
585  size_t lenp;
586  ierr = nc_inq_att(ncid, NC_GLOBAL, name.data(), nullptr, &lenp);
587  return (ierr == NC_NOERR);
588 }
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.

560 {
561  int ierr = nc_inq_dimid(ncid, name.data(), nullptr);
562  return (ierr == NC_NOERR);
563 }
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.

554 {
555  int ierr = nc_inq_ncid(ncid, name.data(), nullptr);
556  return (ierr == NC_NOERR);
557 }
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.

576 {
577  int rh_id = 0;
578  int ierr = nc_inq_varid(ncid, name.data(), &rh_id);
579  return (ierr == NC_NOERR);
580 }
Here is the call graph for this function:

◆ name()

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

Name of this group.

465 {
466  size_t nlen;
467  std::vector<char> grpname;
468  check_nc_error(nc_inq_grpname_len(ncid, &nlen));
469  grpname.resize(nlen + 1);
470  check_nc_error(nc_inq_grpname(ncid, grpname.data()));
471  return std::string{grpname.begin(), grpname.end()};
472 }

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

540 {
541  int nattrs;
542  check_nc_error(nc_inq(ncid, nullptr, nullptr, &nattrs, nullptr));
543  return nattrs;
544 }

◆ num_dimensions()

int ncutils::NCGroup::num_dimensions ( ) const

Number of dimensions in this group.

533 {
534  int ndims;
535  check_nc_error(nc_inq(ncid, &ndims, nullptr, nullptr, nullptr));
536  return ndims;
537 }

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.

526 {
527  int ngrps;
528  check_nc_error(nc_inq_grps(ncid, &ngrps, nullptr));
529  return ngrps;
530 }

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.

547 {
548  int nvars;
549  check_nc_error(nc_inq(ncid, nullptr, &nvars, nullptr, nullptr));
550  return nvars;
551 }

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
591 {
592  check_nc_error(nc_put_att_text(
593  ncid, NC_GLOBAL, name.data(), value.size(), value.data()));
594 }
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
597 {
598  check_nc_error(nc_put_att_double(
599  ncid, NC_GLOBAL, name.data(), NC_DOUBLE, value.size(), value.data()));
600 }
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
603 {
604  check_nc_error(nc_put_att_float(
605  ncid, NC_GLOBAL, name.data(), NC_FLOAT, value.size(), value.data()));
606 }
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
609 {
610  check_nc_error(nc_put_att_int(
611  ncid, NC_GLOBAL, name.data(), NC_INT, value.size(), value.data()));
612 }
Here is the call graph for this function:

◆ var()

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

Get the variable instance by name.

519 {
520  int varid;
521  check_nc_error(nc_inq_varid(ncid, name.data(), &varid));
522  return NCVar{ncid, varid};
523 }
Here is the call graph for this function:

Member Data Documentation

◆ ncid


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