source: XIOS/trunk/src/io/netCdfInterface_impl.hpp @ 686

Last change on this file since 686 was 686, checked in by rlacroix, 9 years ago

Use the NetCDF wrapper for inputs for better error checking.

  • 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.4 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      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>
54  int CNetCdfInterface::putAttType(int ncid, int varId, const StdString& attrName,
55                                   StdSize numVal, const T* data)
56  {
57    int status = ncPutAttType(ncid, varId, attrName.c_str(), numVal, data);
58    if (NC_NOERR != status)
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    }
67
68    return status;
69  }
70
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  */
80  template<typename T>
81  int CNetCdfInterface::getVaraType(int ncid, int varId, const StdSize* start, const StdSize* count, T* data)
82  {
83    int status = ncGetVaraType(ncid, varId, start, count, data);
84    if (NC_NOERR != status)
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    }
92
93    return status;
94  }
95
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  }
120}
121
122#endif // __NETCDF_INTERFACE_IMPL_HPP__
Note: See TracBrowser for help on using the repository browser.