2016-02-22 16:26:35 -05:00
|
|
|
#ifndef NETCDF_READER
|
|
|
|
|
#define NETCDF_READER
|
|
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
|
|
#include "common/Array.h"
|
2016-06-28 16:00:30 -04:00
|
|
|
#include "common/Communication.h"
|
2021-03-17 10:24:14 -04:00
|
|
|
#include "common/MPI.h"
|
2016-02-22 16:26:35 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace netcdf {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//! Enum to hold variable type
|
2016-06-16 14:03:39 -04:00
|
|
|
enum VariableType { BYTE, SHORT, USHORT, INT, UINT, INT64, UINT64, FLOAT, DOUBLE, STRING, UNKNOWN };
|
2016-02-22 16:26:35 -05:00
|
|
|
|
2016-06-27 10:10:46 -04:00
|
|
|
//! Enum to hold variable type
|
|
|
|
|
enum FileMode { READ, WRITE, CREATE };
|
|
|
|
|
|
|
|
|
|
|
2016-05-25 14:00:28 -04:00
|
|
|
//! Convert the VariableType to a string
|
|
|
|
|
std::string VariableTypeName( VariableType type );
|
|
|
|
|
|
2016-02-22 16:26:35 -05:00
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* @brief Open netcdf file
|
2020-05-28 13:12:09 -04:00
|
|
|
* @details This function opens a netcdf file
|
2016-02-22 16:26:35 -05:00
|
|
|
* @return This function returns a handle to the file
|
|
|
|
|
* @param filename File to open
|
2016-06-27 10:10:46 -04:00
|
|
|
* @param mode Open the file for reading or writing
|
|
|
|
|
* @param comm MPI communicator to use (MPI_COMM_WORLD: don't use parallel netcdf)
|
2021-03-17 10:24:14 -04:00
|
|
|
*/
|
|
|
|
|
int open( const std::string &filename, FileMode mode, const Utilities::MPI &comm = MPI_COMM_NULL );
|
2016-02-22 16:26:35 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* @brief Close netcdf file
|
2020-05-28 13:12:09 -04:00
|
|
|
* @details This function closes a netcdf file
|
2016-02-22 16:26:35 -05:00
|
|
|
* @param fid Handle to the open file
|
2021-03-17 10:24:14 -04:00
|
|
|
*/
|
2016-02-22 16:26:35 -05:00
|
|
|
void close( int fid );
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* @brief Read the variable names
|
2020-05-28 13:12:09 -04:00
|
|
|
* @details This function reads a list of the variable names in the file
|
2016-02-22 16:26:35 -05:00
|
|
|
* @param fid Handle to the open file
|
2021-03-17 10:24:14 -04:00
|
|
|
*/
|
2016-02-22 16:26:35 -05:00
|
|
|
std::vector<std::string> getVarNames( int fid );
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* @brief Read the attribute names
|
2020-05-28 13:12:09 -04:00
|
|
|
* @details This function reads a list of the attribute names in the file
|
2016-02-22 16:26:35 -05:00
|
|
|
* @param fid Handle to the open file
|
2021-03-17 10:24:14 -04:00
|
|
|
*/
|
2016-02-22 16:26:35 -05:00
|
|
|
std::vector<std::string> getAttNames( int fid );
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* @brief Return the variable type
|
2020-05-28 13:12:09 -04:00
|
|
|
* @details This function returns the type for a variable
|
2016-02-22 16:26:35 -05:00
|
|
|
* @param fid Handle to the open file
|
|
|
|
|
* @param var Variable to read
|
2021-03-17 10:24:14 -04:00
|
|
|
*/
|
|
|
|
|
VariableType getVarType( int fid, const std::string &var );
|
2016-02-22 16:26:35 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* @brief Return the attribute type
|
2020-05-28 13:12:09 -04:00
|
|
|
* @details This function returns the type for an attribute
|
2016-02-22 16:26:35 -05:00
|
|
|
* @param fid Handle to the open file
|
|
|
|
|
* @param att Attribute to read
|
2021-03-17 10:24:14 -04:00
|
|
|
*/
|
|
|
|
|
VariableType getAttType( int fid, const std::string &att );
|
2016-02-22 16:26:35 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* @brief Return the variable dimensions
|
2020-05-28 13:12:09 -04:00
|
|
|
* @details This function returns the die for a variable
|
2016-02-22 16:26:35 -05:00
|
|
|
* @param fid Handle to the open file
|
|
|
|
|
* @param var Variable to read
|
2021-03-17 10:24:14 -04:00
|
|
|
*/
|
|
|
|
|
std::vector<size_t> getVarDim( int fid, const std::string &var );
|
2016-02-22 16:26:35 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* @brief Read a variable
|
2020-05-28 13:12:09 -04:00
|
|
|
* @details This function reads a variable with the given name from the file
|
2016-02-22 16:26:35 -05:00
|
|
|
* @param fid Handle to the open file
|
|
|
|
|
* @param var Variable to read
|
2021-03-17 10:24:14 -04:00
|
|
|
*/
|
2016-02-22 16:26:35 -05:00
|
|
|
template<class TYPE>
|
2021-03-17 10:24:14 -04:00
|
|
|
Array<TYPE> getVar( int fid, const std::string &var );
|
2016-02-22 16:26:35 -05:00
|
|
|
|
|
|
|
|
|
2016-05-25 14:00:28 -04:00
|
|
|
/*!
|
|
|
|
|
* @brief Read a strided variable
|
2020-05-28 13:12:09 -04:00
|
|
|
* @details This function reads a strided variable with the given name from the file
|
2016-05-25 14:00:28 -04:00
|
|
|
* @param fid Handle to the open file
|
|
|
|
|
* @param var Variable to read
|
|
|
|
|
* @param start Starting corner for the read
|
|
|
|
|
* @param count Number of elements to read
|
|
|
|
|
* @param stride Stride size for the read
|
2021-03-17 10:24:14 -04:00
|
|
|
*/
|
2016-05-25 14:00:28 -04:00
|
|
|
template<class TYPE>
|
2021-03-17 10:24:14 -04:00
|
|
|
Array<TYPE> getVar( int fid, const std::string &var, const std::vector<int> &start,
|
|
|
|
|
const std::vector<int> &count, const std::vector<int> &stride );
|
2016-05-25 14:00:28 -04:00
|
|
|
|
|
|
|
|
|
2016-02-22 16:26:35 -05:00
|
|
|
/*!
|
|
|
|
|
* @brief Read an attribute
|
2020-05-28 13:12:09 -04:00
|
|
|
* @details This function reads an attribute with the given name from the file
|
2016-02-22 16:26:35 -05:00
|
|
|
* @param fid Handle to the open file
|
|
|
|
|
* @param att Attribute to read
|
2021-03-17 10:24:14 -04:00
|
|
|
*/
|
2016-02-22 16:26:35 -05:00
|
|
|
template<class TYPE>
|
2021-03-17 10:24:14 -04:00
|
|
|
Array<TYPE> getAtt( int fid, const std::string &att );
|
2016-02-22 16:26:35 -05:00
|
|
|
|
|
|
|
|
|
2016-06-27 10:10:46 -04:00
|
|
|
/*!
|
|
|
|
|
* @brief Write the dimensions
|
2021-03-17 10:24:14 -04:00
|
|
|
* @details This function writes the grid dimensions to netcdf.
|
2016-06-27 10:10:46 -04:00
|
|
|
* @param fid Handle to the open file
|
2021-09-13 11:51:21 -04:00
|
|
|
* @param names
|
|
|
|
|
* @param dims
|
2021-03-17 10:24:14 -04:00
|
|
|
*/
|
|
|
|
|
std::vector<int> defDim(
|
|
|
|
|
int fid, const std::vector<std::string> &names, const std::vector<int> &dims );
|
2016-06-27 10:10:46 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* @brief Write a variable
|
2021-03-17 10:24:14 -04:00
|
|
|
* @details This function writes a variable to netcdf.
|
2016-06-27 10:10:46 -04:00
|
|
|
* @param fid Handle to the open file
|
2021-09-13 11:51:21 -04:00
|
|
|
* @param var Variable to read
|
|
|
|
|
* @param dimids
|
|
|
|
|
* @param data
|
|
|
|
|
* @param rank_info
|
2021-03-17 10:24:14 -04:00
|
|
|
*/
|
2016-06-27 10:10:46 -04:00
|
|
|
template<class TYPE>
|
2021-03-17 10:24:14 -04:00
|
|
|
void write( int fid, const std::string &var, const std::vector<int> &dimids,
|
|
|
|
|
const Array<TYPE> &data, const RankInfoStruct &rank_info );
|
2016-06-27 10:10:46 -04:00
|
|
|
|
|
|
|
|
|
2021-06-02 15:36:44 -04:00
|
|
|
} // namespace netcdf
|
|
|
|
|
|
|
|
|
|
|
2016-02-22 16:26:35 -05:00
|
|
|
#endif
|