1 | /*! |
---|
2 | \file netCdfInterface_impl.hpp |
---|
3 | \author Ha NGUYEN |
---|
4 | \date 08 Oct 2014 |
---|
5 | \since 06 Oct 2014 |
---|
6 | |
---|
7 | \brief Implementation of some templated functions in netCdfInterface |
---|
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 | { |
---|
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 | */ |
---|
27 | template<typename T> |
---|
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 | StdString varName; |
---|
35 | sstr << "Error when calling function ncGetAttType(ncid, varId, attrName.c_str(), data)" << std::endl; |
---|
36 | sstr << nc_strerror(status) << std::endl; |
---|
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; |
---|
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> |
---|
56 | int CNetCdfInterface::putAttType(int ncid, int varId, const StdString& attrName, |
---|
57 | StdSize numVal, const T* data) |
---|
58 | { |
---|
59 | int status = ncPutAttType(ncid, varId, attrName.c_str(), numVal, data); |
---|
60 | if (NC_NOERR != status) |
---|
61 | { |
---|
62 | StdStringStream sstr; |
---|
63 | StdString varName; |
---|
64 | sstr << "Error when calling function ncPutAttType(ncid, varId, attrName.c_str(), numVal, data)" << std::endl; |
---|
65 | sstr << nc_strerror(status) << std::endl; |
---|
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 |
---|
68 | << " with " << numVal << " elements." << std::endl; |
---|
69 | throw CNetCdfException(sstr.str()); |
---|
70 | } |
---|
71 | |
---|
72 | return status; |
---|
73 | } |
---|
74 | |
---|
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 | */ |
---|
84 | template<typename T> |
---|
85 | int CNetCdfInterface::getVaraType(int ncid, int varId, const StdSize* start, const StdSize* count, T* data) |
---|
86 | { |
---|
87 | int status = ncGetVaraType(ncid, varId, start, count, data); |
---|
88 | if (NC_NOERR != status) |
---|
89 | { |
---|
90 | StdStringStream sstr; |
---|
91 | StdString varName; |
---|
92 | sstr << "Error when calling function ncGetVaraType(ncid, varId, start, count, data)" << std::endl; |
---|
93 | sstr << nc_strerror(status) << std::endl; |
---|
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; |
---|
96 | throw CNetCdfException(sstr.str()); |
---|
97 | } |
---|
98 | |
---|
99 | return status; |
---|
100 | } |
---|
101 | |
---|
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; |
---|
118 | StdString varName; |
---|
119 | sstr << "Error when calling function ncPutVaraType(ncid, varId, start, count, data)" << std::endl; |
---|
120 | sstr << nc_strerror(status) << std::endl; |
---|
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; |
---|
123 | throw CNetCdfException(sstr.str()); |
---|
124 | } |
---|
125 | |
---|
126 | return status; |
---|
127 | } |
---|
128 | } |
---|
129 | |
---|
130 | #endif // __NETCDF_INTERFACE_IMPL_HPP__ |
---|