source: XIOS/dev/branch_openmp/src/io/netCdfInterface_impl.hpp @ 1544

Last change on this file since 1544 was 1544, checked in by yushan, 3 years ago

bug fix for tests in prod mode

  • 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: 4.8 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
16namespace 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;
88    status = ncGetVaraType(ncid, varId, start, count, data);
89    if (NC_NOERR != status)
90    {
91      StdStringStream sstr;
92      StdString varName;
93      sstr << "Error when calling function ncGetVaraType(ncid, varId, start, count, data)" << std::endl;
94      sstr << nc_strerror(status) << std::endl;
95      inqVarName(ncid, varId, varName);
96      sstr << "Unable to read data given the location id: " << ncid << " and the variable whose id: " << varId << " and name: " << varName << std::endl;
97      throw CNetCdfException(sstr.str());
98    }
99
100    return status;
101  }
102
103  /*!
104  This function writes the given data for the specified variable.
105  \param [in] ncid Id of group (or file id)
106  \param [in] varId Id of the variable
107  \param [in] start Array specifying the index in the variable where the first data value will be written
108  \param [in] count Array specifying the edge lengths along each dimension of the data block
109  \param [in] data Array of values
110  \return Status code
111  */
112  template<typename T>
113  int CNetCdfInterface::putVaraType(int ncid, int varId, const StdSize* start, const StdSize* count, const T* data)
114  {
115    int status = ncPutVaraType(ncid, varId, start, count, data);
116    if (NC_NOERR != status)
117    {
118      StdStringStream sstr;
119      StdString varName;
120      sstr << "Error when calling function ncPutVaraType(ncid, varId, start, count, data)" << std::endl;
121      sstr << nc_strerror(status) << std::endl;
122      inqVarName(ncid, varId, varName);
123      sstr << "Unable to write data given the location id: " << ncid << " and the variable whose id: " << varId << " and name: " << varName << std::endl;
124      throw CNetCdfException(sstr.str());
125    }
126
127    return status;
128  }
129}
130
131#endif // __NETCDF_INTERFACE_IMPL_HPP__
Note: See TracBrowser for help on using the repository browser.