source: XIOS3/branches/xios-3.0-beta/src/io/netCdfInterface_impl.hpp @ 2427

Last change on this file since 2427 was 2427, checked in by jderouillat, 17 months ago

Backport the system to log the memory consumption (commit ID [2418-2420,2425-2426])

  • Property copyright set to
    Software name : XIOS (Xml I/O Server)
    http://forge.ipsl.jussieu.fr/ioserver
    Creation date : January 2009
    Licence : CeCCIL version2
    see license file in root directory : Licence_CeCILL_V2-en.txt
    or http://www.cecill.info/licences/Licence_CeCILL_V2-en.html
    Holder : CEA/LSCE (Laboratoire des Sciences du CLimat et de l'Environnement)
    CNRS/IPSL (Institut Pierre Simon Laplace)
    Project Manager : Yann Meurdesoif
    yann.meurdesoif@cea.fr
File size: 5.2 KB
Line 
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#include "mem_checker.hpp"
16
17namespace xios
18{
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  */
28  template<typename T>
29  int CNetCdfInterface::getAttType(int ncid, int varId, const StdString& attrName, T* data)
30  {
31    CMemChecker::get("NetCDF get/put").resume();
32    int status = ncGetAttType(ncid, varId, attrName.c_str(), data);
33    if (NC_NOERR != status)
34    {
35      StdStringStream sstr;
36      StdString varName;
37      sstr << "Error when calling function ncGetAttType(ncid, varId, attrName.c_str(), data)" << std::endl;
38      sstr << nc_strerror(status) << std::endl;
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;
41      throw CNetCdfException(sstr.str());
42    }
43    CMemChecker::get("NetCDF get/put").suspend();
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>
59  int CNetCdfInterface::putAttType(int ncid, int varId, const StdString& attrName,
60                                   StdSize numVal, const T* data)
61  {
62    CMemChecker::get("NetCDF get/put").resume();
63    int status = ncPutAttType(ncid, varId, attrName.c_str(), numVal, data);
64    if (NC_NOERR != status)
65    {
66      StdStringStream sstr;
67      StdString varName;
68      sstr << "Error when calling function ncPutAttType(ncid, varId, attrName.c_str(), numVal, data)" << std::endl;
69      sstr << nc_strerror(status) << std::endl;
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
72           << " with " << numVal << " elements." << std::endl;
73      throw CNetCdfException(sstr.str());
74    }
75    CMemChecker::get("NetCDF get/put").suspend();
76
77    return status;
78  }
79
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  */
89  template<typename T>
90  int CNetCdfInterface::getVaraType(int ncid, int varId, const StdSize* start, const StdSize* count, T* data)
91  {
92    CMemChecker::get("NetCDF get/put").resume();
93    int status = ncGetVaraType(ncid, varId, start, count, data);
94    if (NC_NOERR != status)
95    {
96      StdStringStream sstr;
97      StdString varName;
98      sstr << "Error when calling function ncGetVaraType(ncid, varId, start, count, data)" << std::endl;
99      sstr << nc_strerror(status) << std::endl;
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;
102      throw CNetCdfException(sstr.str());
103    }
104    CMemChecker::get("NetCDF get/put").suspend();
105
106    return status;
107  }
108
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  {
121    CMemChecker::get("NetCDF get/put").resume();
122    int status = ncPutVaraType(ncid, varId, start, count, data);
123    if (NC_NOERR != status)
124    {
125      StdStringStream sstr;
126      StdString varName;
127      sstr << "Error when calling function ncPutVaraType(ncid, varId, start, count, data)" << std::endl;
128      sstr << nc_strerror(status) << std::endl;
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;
131      throw CNetCdfException(sstr.str());
132    }
133    CMemChecker::get("NetCDF get/put").suspend();
134
135    return status;
136  }
137}
138
139#endif // __NETCDF_INTERFACE_IMPL_HPP__
Note: See TracBrowser for help on using the repository browser.