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