[498] | 1 | /*! |
---|
| 2 | \file netCdfInterface_impl.hpp |
---|
| 3 | \author Ha NGUYEN |
---|
| 4 | \date 08 Oct 2014 |
---|
| 5 | \since 06 Oct 2014 |
---|
| 6 | |
---|
[686] | 7 | \brief Implementation of some templated functions in netCdfInterface |
---|
[498] | 8 | */ |
---|
| 9 | |
---|
| 10 | #ifndef __NETCDF_INTERFACE_IMPL_HPP__ |
---|
| 11 | #define __NETCDF_INTERFACE_IMPL_HPP__ |
---|
| 12 | |
---|
| 13 | #include "netCdfInterface.hpp" |
---|
| 14 | #include "netCdfException.hpp" |
---|
[1138] | 15 | // mpi_std.hpp |
---|
[498] | 16 | |
---|
| 17 | namespace xios |
---|
| 18 | { |
---|
[686] | 19 | /*! |
---|
| 20 | This function reads a variable attribute or a global attribute |
---|
| 21 | given a location id, a variable id and the attribute name |
---|
| 22 | \param [in] ncid Id of group (or file id) |
---|
| 23 | \param [in] varId Id of the variable |
---|
| 24 | \param [in] attrName Name of the attribute |
---|
| 25 | \param [out] data Array of values |
---|
| 26 | \return Status code |
---|
| 27 | */ |
---|
[498] | 28 | template<typename T> |
---|
[686] | 29 | int CNetCdfInterface::getAttType(int ncid, int varId, const StdString& attrName, T* data) |
---|
| 30 | { |
---|
| 31 | int status = ncGetAttType(ncid, varId, attrName.c_str(), data); |
---|
| 32 | if (NC_NOERR != status) |
---|
| 33 | { |
---|
| 34 | StdStringStream sstr; |
---|
[833] | 35 | StdString varName; |
---|
[686] | 36 | sstr << "Error when calling function ncGetAttType(ncid, varId, attrName.c_str(), data)" << std::endl; |
---|
| 37 | sstr << nc_strerror(status) << std::endl; |
---|
[833] | 38 | inqVarName(ncid, varId, varName); |
---|
| 39 | sstr << "Unable to read attribute " << attrName << " given the location id: " << ncid << " and the variable whose id: " << varId << " and name: " << varName << std::endl; |
---|
[686] | 40 | throw CNetCdfException(sstr.str()); |
---|
| 41 | } |
---|
| 42 | |
---|
| 43 | return status; |
---|
| 44 | } |
---|
| 45 | |
---|
| 46 | /*! |
---|
| 47 | This function adds or modifies a variable attribute or a global attribute |
---|
| 48 | given a location id, a variable id and the attribute name |
---|
| 49 | \param [in] ncid Id of group (or file id) |
---|
| 50 | \param [in] varId Id of the variable |
---|
| 51 | \param [in] attrName Name of the attribute |
---|
| 52 | \param [in] numVal Number of values to set |
---|
| 53 | \param [in] data Array of values |
---|
| 54 | \return Status code |
---|
| 55 | */ |
---|
| 56 | template<typename T> |
---|
[498] | 57 | int CNetCdfInterface::putAttType(int ncid, int varId, const StdString& attrName, |
---|
[686] | 58 | StdSize numVal, const T* data) |
---|
[498] | 59 | { |
---|
[686] | 60 | int status = ncPutAttType(ncid, varId, attrName.c_str(), numVal, data); |
---|
[498] | 61 | if (NC_NOERR != status) |
---|
[686] | 62 | { |
---|
| 63 | StdStringStream sstr; |
---|
[833] | 64 | StdString varName; |
---|
[686] | 65 | sstr << "Error when calling function ncPutAttType(ncid, varId, attrName.c_str(), numVal, data)" << std::endl; |
---|
| 66 | sstr << nc_strerror(status) << std::endl; |
---|
[833] | 67 | inqVarName(ncid, varId, varName); |
---|
| 68 | sstr << "Unable to set attribute " << attrName << " given the location id: " << ncid << " and the variable whose id: " << varId << " and name: " << varName << std::endl |
---|
[686] | 69 | << " with " << numVal << " elements." << std::endl; |
---|
| 70 | throw CNetCdfException(sstr.str()); |
---|
| 71 | } |
---|
[498] | 72 | |
---|
[686] | 73 | return status; |
---|
[498] | 74 | } |
---|
| 75 | |
---|
[686] | 76 | /*! |
---|
| 77 | This function reads data for the specified variable. |
---|
| 78 | \param [in] ncid Id of group (or file id) |
---|
| 79 | \param [in] varId Id of the variable |
---|
| 80 | \param [in] start Array specifying the index in the variable where the first data value will be written |
---|
| 81 | \param [in] count Array specifying the edge lengths along each dimension of the data block |
---|
| 82 | \param [out] data Array of values |
---|
| 83 | \return Status code |
---|
| 84 | */ |
---|
[498] | 85 | template<typename T> |
---|
[686] | 86 | int CNetCdfInterface::getVaraType(int ncid, int varId, const StdSize* start, const StdSize* count, T* data) |
---|
[498] | 87 | { |
---|
[1146] | 88 | int status; |
---|
| 89 | #pragma omp critical (_netcdf) |
---|
| 90 | status = ncGetVaraType(ncid, varId, start, count, data); |
---|
[498] | 91 | if (NC_NOERR != status) |
---|
[686] | 92 | { |
---|
| 93 | StdStringStream sstr; |
---|
[833] | 94 | StdString varName; |
---|
[686] | 95 | sstr << "Error when calling function ncGetVaraType(ncid, varId, start, count, data)" << std::endl; |
---|
| 96 | sstr << nc_strerror(status) << std::endl; |
---|
[833] | 97 | inqVarName(ncid, varId, varName); |
---|
| 98 | sstr << "Unable to read data given the location id: " << ncid << " and the variable whose id: " << varId << " and name: " << varName << std::endl; |
---|
[686] | 99 | throw CNetCdfException(sstr.str()); |
---|
| 100 | } |
---|
[498] | 101 | |
---|
[686] | 102 | return status; |
---|
[498] | 103 | } |
---|
| 104 | |
---|
[686] | 105 | /*! |
---|
| 106 | This function writes the given data for the specified variable. |
---|
| 107 | \param [in] ncid Id of group (or file id) |
---|
| 108 | \param [in] varId Id of the variable |
---|
| 109 | \param [in] start Array specifying the index in the variable where the first data value will be written |
---|
| 110 | \param [in] count Array specifying the edge lengths along each dimension of the data block |
---|
| 111 | \param [in] data Array of values |
---|
| 112 | \return Status code |
---|
| 113 | */ |
---|
| 114 | template<typename T> |
---|
| 115 | int CNetCdfInterface::putVaraType(int ncid, int varId, const StdSize* start, const StdSize* count, const T* data) |
---|
| 116 | { |
---|
| 117 | int status = ncPutVaraType(ncid, varId, start, count, data); |
---|
| 118 | if (NC_NOERR != status) |
---|
| 119 | { |
---|
| 120 | StdStringStream sstr; |
---|
[833] | 121 | StdString varName; |
---|
[686] | 122 | sstr << "Error when calling function ncPutVaraType(ncid, varId, start, count, data)" << std::endl; |
---|
| 123 | sstr << nc_strerror(status) << std::endl; |
---|
[833] | 124 | inqVarName(ncid, varId, varName); |
---|
| 125 | sstr << "Unable to write data given the location id: " << ncid << " and the variable whose id: " << varId << " and name: " << varName << std::endl; |
---|
[686] | 126 | throw CNetCdfException(sstr.str()); |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | return status; |
---|
| 130 | } |
---|
[498] | 131 | } |
---|
| 132 | |
---|
| 133 | #endif // __NETCDF_INTERFACE_IMPL_HPP__ |
---|