NetCDFFile Class Reference

Kynema API: kynema::util::NetCDFFile Class Reference
Kynema API
A flexible multibody structural dynamics code for wind turbines
Loading...
Searching...
No Matches
kynema::util::NetCDFFile Class Reference

Class for managing NetCDF files for writing outputs. More...

#include <netcdf_file.hpp>

Public Member Functions

 NetCDFFile (const std::string &file_path, bool create=true)
 Constructor to create a NetCDFFile object.
 
 NetCDFFile (const NetCDFFile &)=delete
 
NetCDFFileoperator= (const NetCDFFile &)=delete
 
 NetCDFFile (NetCDFFile &&)=delete
 
NetCDFFileoperator= (NetCDFFile &&)=delete
 
 ~NetCDFFile ()
 Destructor to close the NetCDF file.
 
int AddDimension (const std::string &name, size_t length) const
 Adds a dimension to the NetCDF file.
 
template<typename T >
int AddVariable (const std::string &name, std::span< const int > dim_ids) const
 Adds a variable to the NetCDF file.
 
void AddAttribute (const std::string &var_name, const std::string &attr_name, float value) const
 Adds an attribute to a variable in the NetCDF file.
 
void AddAttribute (const std::string &var_name, const std::string &attr_name, double value) const
 
void AddAttribute (const std::string &var_name, const std::string &attr_name, int value) const
 
void AddAttribute (const std::string &var_name, const std::string &attr_name, const std::string &value) const
 
void WriteVariable (const std::string &name, std::span< const float > data) const
 Writes data to a variable in the NetCDF file.
 
void WriteVariable (const std::string &name, std::span< const double > data) const
 
void WriteVariable (const std::string &name, std::span< const int > data) const
 
void WriteVariable (const std::string &name, std::span< const std::string > data) const
 
void WriteVariableAt (const std::string &name, std::span< const size_t > start, std::span< const size_t > count, std::span< const float > data) const
 Writes data to a variable at specific indices in the NetCDF file.
 
void WriteVariableAt (const std::string &name, std::span< const size_t > start, std::span< const size_t > count, std::span< const double > data) const
 
void WriteVariableAt (const std::string &name, std::span< const size_t > start, std::span< const size_t > count, std::span< const int > data) const
 
void WriteVariableAt (const std::string &name, std::span< const size_t > start, std::span< const size_t > count, std::span< const std::string > data) const
 
void Sync () const
 Synchronizes (flushes) the NetCDF file to disk.
 
int GetNetCDFId () const
 Returns the NetCDF file ID.
 
int GetDimensionId (const std::string &name) const
 Returns the dimension ID for a given dimension name.
 
int GetVariableId (const std::string &name) const
 Returns the variable ID for a given variable name.
 
size_t GetNumberOfDimensions (const std::string &var_name) const
 Gets the number of dimensions of a variable in the NetCDF file.
 
size_t GetDimensionLength (int dim_id) const
 Gets the length of a dimension in the NetCDF file.
 
size_t GetDimensionLength (const std::string &name) const
 Gets the length of a dimension in the NetCDF file based on name.
 
std::vector< size_t > GetShape (const std::string &var_name) const
 Gets the shape (dimension lengths) of a variable in the NetCDF file.
 
void ReadVariable (const std::string &name, float *data) const
 Reads data from a variable in the NetCDF file.
 
void ReadVariable (const std::string &name, double *data) const
 
void ReadVariable (const std::string &name, int *data) const
 
void ReadVariableAt (const std::string &name, std::span< const size_t > start, std::span< const size_t > count, float *data) const
 Reads data from a variable at specific indices in the NetCDF file.
 
void ReadVariableAt (const std::string &name, std::span< const size_t > start, std::span< const size_t > count, double *data) const
 
void ReadVariableAt (const std::string &name, std::span< const size_t > start, std::span< const size_t > count, int *data) const
 
void ReadVariableWithStride (const std::string &name, std::span< const size_t > start, std::span< const size_t > count, std::span< const ptrdiff_t > stride, float *data) const
 Reads data from a variable with specified stride in the NetCDF file.
 
void ReadVariableWithStride (const std::string &name, std::span< const size_t > start, std::span< const size_t > count, std::span< const ptrdiff_t > stride, double *data) const
 
void ReadVariableWithStride (const std::string &name, std::span< const size_t > start, std::span< const size_t > count, std::span< const ptrdiff_t > stride, int *data) const
 
template<>
int AddVariable (const std::string &name, std::span< const int > dim_ids) const
 
template<>
int AddVariable (const std::string &name, std::span< const int > dim_ids) const
 
template<>
int AddVariable (const std::string &name, std::span< const int > dim_ids) const
 

Detailed Description

Class for managing NetCDF files for writing outputs.

Constructor & Destructor Documentation

◆ NetCDFFile() [1/3]

kynema::util::NetCDFFile::NetCDFFile ( const std::string &  file_path,
bool  create = true 
)
explicit

Constructor to create a NetCDFFile object.

This constructor creates a new NetCDF file if the create flag is true. Otherwise, it opens an existing NetCDF file.

◆ NetCDFFile() [2/3]

kynema::util::NetCDFFile::NetCDFFile ( const NetCDFFile )
delete

◆ NetCDFFile() [3/3]

kynema::util::NetCDFFile::NetCDFFile ( NetCDFFile &&  )
delete

◆ ~NetCDFFile()

kynema::util::NetCDFFile::~NetCDFFile ( )

Destructor to close the NetCDF file.

This function is a wrapper around the NetCDF library's "nc_close" function. It closes the NetCDF file with the given (valid) ID.

Member Function Documentation

◆ AddAttribute() [1/4]

void kynema::util::NetCDFFile::AddAttribute ( const std::string &  var_name,
const std::string &  attr_name,
const std::string &  value 
) const

◆ AddAttribute() [2/4]

void kynema::util::NetCDFFile::AddAttribute ( const std::string &  var_name,
const std::string &  attr_name,
double  value 
) const

◆ AddAttribute() [3/4]

void kynema::util::NetCDFFile::AddAttribute ( const std::string &  var_name,
const std::string &  attr_name,
float  value 
) const

Adds an attribute to a variable in the NetCDF file.

This function is a wrapper around the NetCDF library's "nc_put_att_text" and "nc_put_att" functions. It adds an attribute (e.g. metadata) to a variable in the NetCDF file.

◆ AddAttribute() [4/4]

void kynema::util::NetCDFFile::AddAttribute ( const std::string &  var_name,
const std::string &  attr_name,
int  value 
) const

◆ AddDimension()

int kynema::util::NetCDFFile::AddDimension ( const std::string &  name,
size_t  length 
) const

Adds a dimension to the NetCDF file.

This function is a wrapper around the NetCDF library's "nc_def_dim" function. It creates a new dimension with the given name and length in the NetCDF file.

◆ AddVariable() [1/4]

template<>
int kynema::util::NetCDFFile::AddVariable ( const std::string &  name,
std::span< const int >  dim_ids 
) const

◆ AddVariable() [2/4]

template<>
int kynema::util::NetCDFFile::AddVariable ( const std::string &  name,
std::span< const int >  dim_ids 
) const

◆ AddVariable() [3/4]

template<>
int kynema::util::NetCDFFile::AddVariable ( const std::string &  name,
std::span< const int >  dim_ids 
) const

◆ AddVariable() [4/4]

template<typename T >
int kynema::util::NetCDFFile::AddVariable ( const std::string &  name,
std::span< const int >  dim_ids 
) const

Adds a variable to the NetCDF file.

This function is a wrapper around the NetCDF library's "nc_def_var" function. It creates a new variable with the given name and dimension IDs in the NetCDF file. The NetCDF type is automatically determined from the template parameter.

◆ GetDimensionId()

int kynema::util::NetCDFFile::GetDimensionId ( const std::string &  name) const

Returns the dimension ID for a given dimension name.

◆ GetDimensionLength() [1/2]

size_t kynema::util::NetCDFFile::GetDimensionLength ( const std::string &  name) const

Gets the length of a dimension in the NetCDF file based on name.

◆ GetDimensionLength() [2/2]

size_t kynema::util::NetCDFFile::GetDimensionLength ( int  dim_id) const

Gets the length of a dimension in the NetCDF file.

Parameters
dim_idThe ID of the dimension
Returns
The length of the dimension

◆ GetNetCDFId()

int kynema::util::NetCDFFile::GetNetCDFId ( ) const

Returns the NetCDF file ID.

◆ GetNumberOfDimensions()

size_t kynema::util::NetCDFFile::GetNumberOfDimensions ( const std::string &  var_name) const

Gets the number of dimensions of a variable in the NetCDF file.

Parameters
var_nameThe name of the variable
Returns
The number of dimensions

◆ GetShape()

std::vector< size_t > kynema::util::NetCDFFile::GetShape ( const std::string &  var_name) const

Gets the shape (dimension lengths) of a variable in the NetCDF file.

Parameters
var_nameThe name of the variable
Returns
Vector containing the length of each dimension of the variable

◆ GetVariableId()

int kynema::util::NetCDFFile::GetVariableId ( const std::string &  name) const

Returns the variable ID for a given variable name.

◆ operator=() [1/2]

NetCDFFile & kynema::util::NetCDFFile::operator= ( const NetCDFFile )
delete

◆ operator=() [2/2]

NetCDFFile & kynema::util::NetCDFFile::operator= ( NetCDFFile &&  )
delete

◆ ReadVariable() [1/3]

void kynema::util::NetCDFFile::ReadVariable ( const std::string &  name,
double *  data 
) const

◆ ReadVariable() [2/3]

void kynema::util::NetCDFFile::ReadVariable ( const std::string &  name,
float *  data 
) const

Reads data from a variable in the NetCDF file.

This function is a wrapper around the NetCDF library's type-specific get functions. Supports the following types:

  • float (NC_FLOAT)
  • double (NC_DOUBLE)
  • int (NC_INT)
Parameters
nameThe name of the variable to read from
dataPointer to the buffer where data will be stored

◆ ReadVariable() [3/3]

void kynema::util::NetCDFFile::ReadVariable ( const std::string &  name,
int *  data 
) const

◆ ReadVariableAt() [1/3]

void kynema::util::NetCDFFile::ReadVariableAt ( const std::string &  name,
std::span< const size_t >  start,
std::span< const size_t >  count,
double *  data 
) const

◆ ReadVariableAt() [2/3]

void kynema::util::NetCDFFile::ReadVariableAt ( const std::string &  name,
std::span< const size_t >  start,
std::span< const size_t >  count,
float *  data 
) const

Reads data from a variable at specific indices in the NetCDF file.

Template Parameters
TThe data type to read (float, double, or int)
Parameters
nameThe name of the variable to read from
startArray specifying the starting index in each dimension
countArray specifying the number of values to read in each dimension
dataPointer to the buffer where data will be stored

◆ ReadVariableAt() [3/3]

void kynema::util::NetCDFFile::ReadVariableAt ( const std::string &  name,
std::span< const size_t >  start,
std::span< const size_t >  count,
int *  data 
) const

◆ ReadVariableWithStride() [1/3]

void kynema::util::NetCDFFile::ReadVariableWithStride ( const std::string &  name,
std::span< const size_t >  start,
std::span< const size_t >  count,
std::span< const ptrdiff_t >  stride,
double *  data 
) const

◆ ReadVariableWithStride() [2/3]

void kynema::util::NetCDFFile::ReadVariableWithStride ( const std::string &  name,
std::span< const size_t >  start,
std::span< const size_t >  count,
std::span< const ptrdiff_t >  stride,
float *  data 
) const

Reads data from a variable with specified stride in the NetCDF file.

Template Parameters
TThe data type to read (float, double, or int)
Parameters
nameThe name of the variable to read from
startArray specifying the starting index in each dimension
countArray specifying the number of values to read in each dimension
strideArray specifying the stride in each dimension
dataPointer to the buffer where data will be stored

◆ ReadVariableWithStride() [3/3]

void kynema::util::NetCDFFile::ReadVariableWithStride ( const std::string &  name,
std::span< const size_t >  start,
std::span< const size_t >  count,
std::span< const ptrdiff_t >  stride,
int *  data 
) const

◆ Sync()

void kynema::util::NetCDFFile::Sync ( ) const

Synchronizes (flushes) the NetCDF file to disk.

◆ WriteVariable() [1/4]

void kynema::util::NetCDFFile::WriteVariable ( const std::string &  name,
std::span< const double >  data 
) const

◆ WriteVariable() [2/4]

void kynema::util::NetCDFFile::WriteVariable ( const std::string &  name,
std::span< const float >  data 
) const

Writes data to a variable in the NetCDF file.

This function is a wrapper around the NetCDF library's "nc_put_var" and "nc_put_var_string" functions. It writes the provided data to the variable with the given name. Supports the following types:

  • float (NC_FLOAT)
  • double (NC_DOUBLE)
  • int (NC_INT)
  • std::string (NC_STRING)

◆ WriteVariable() [3/4]

void kynema::util::NetCDFFile::WriteVariable ( const std::string &  name,
std::span< const int >  data 
) const

◆ WriteVariable() [4/4]

void kynema::util::NetCDFFile::WriteVariable ( const std::string &  name,
std::span< const std::string >  data 
) const

◆ WriteVariableAt() [1/4]

void kynema::util::NetCDFFile::WriteVariableAt ( const std::string &  name,
std::span< const size_t >  start,
std::span< const size_t >  count,
std::span< const double >  data 
) const

◆ WriteVariableAt() [2/4]

void kynema::util::NetCDFFile::WriteVariableAt ( const std::string &  name,
std::span< const size_t >  start,
std::span< const size_t >  count,
std::span< const float >  data 
) const

Writes data to a variable at specific indices in the NetCDF file.

This function is a wrapper around the NetCDF library's "nc_put_vara" and "nc_put_vara_string" functions. It writes the provided data to the variable with the given name at the specified indices. Supports the following types:

  • float (NC_FLOAT)
  • double (NC_DOUBLE)
  • int (NC_INT)
  • std::string (NC_STRING)
Template Parameters
TThe data type of the variable
Parameters
nameThe name of the variable to write to
startArray specifying the starting index in each dimension
countArray specifying the number of values to write in each dimension
dataThe vector containing the data to write

◆ WriteVariableAt() [3/4]

void kynema::util::NetCDFFile::WriteVariableAt ( const std::string &  name,
std::span< const size_t >  start,
std::span< const size_t >  count,
std::span< const int >  data 
) const

◆ WriteVariableAt() [4/4]

void kynema::util::NetCDFFile::WriteVariableAt ( const std::string &  name,
std::span< const size_t >  start,
std::span< const size_t >  count,
std::span< const std::string >  data 
) const

The documentation for this class was generated from the following files:
  • /home/runner/work/kynema/kynema/kynema/src/utilities/netcdf/netcdf_file.hpp
  • /home/runner/work/kynema/kynema/kynema/src/utilities/netcdf/netcdf_file.cpp