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