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