source: XIOS/dev/branch_yushan_merged/src/io/netCdfInterface_impl.hpp @ 1138

Last change on this file since 1138 was 1138, checked in by yushan, 7 years ago

test_remap back to work. No thread for now

  • 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// mpi_std.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    int status = ncGetAttType(ncid, varId, attrName.c_str(), data);
32    if (NC_NOERR != status)
33    {
34      StdStringStream sstr;
35      StdString varName;
36      sstr << "Error when calling function ncGetAttType(ncid, varId, attrName.c_str(), data)" << std::endl;
37      sstr << nc_strerror(status) << std::endl;
38      inqVarName(ncid, varId, varName);
39      sstr << "Unable to read attribute " << attrName << " given the location id: " << ncid << " and the variable whose id: " << varId << " and name: " << varName << std::endl;
40      throw CNetCdfException(sstr.str());
41    }
42
43    return status;
44  }
45
46  /*!
47  This function adds or modifies a variable attribute or a global attribute
48  given a location id, a variable id and the attribute name
49  \param [in] ncid Id of group (or file id)
50  \param [in] varId Id of the variable
51  \param [in] attrName Name of the attribute
52  \param [in] numVal Number of values to set
53  \param [in] data Array of values
54  \return Status code
55  */
56  template<typename T>
57  int CNetCdfInterface::putAttType(int ncid, int varId, const StdString& attrName,
58                                   StdSize numVal, const T* data)
59  {
60    int status = ncPutAttType(ncid, varId, attrName.c_str(), numVal, data);
61    if (NC_NOERR != status)
62    {
63      StdStringStream sstr;
64      StdString varName;
65      sstr << "Error when calling function ncPutAttType(ncid, varId, attrName.c_str(), numVal, data)" << std::endl;
66      sstr << nc_strerror(status) << std::endl;
67      inqVarName(ncid, varId, varName);
68      sstr << "Unable to set attribute " << attrName << " given the location id: " << ncid << " and the variable whose id: " << varId << " and name: " << varName << std::endl
69           << " with " << numVal << " elements." << std::endl;
70      throw CNetCdfException(sstr.str());
71    }
72
73    return status;
74  }
75
76  /*!
77  This function reads data for the specified variable.
78  \param [in] ncid Id of group (or file id)
79  \param [in] varId Id of the variable
80  \param [in] start Array specifying the index in the variable where the first data value will be written
81  \param [in] count Array specifying the edge lengths along each dimension of the data block
82  \param [out] data Array of values
83  \return Status code
84  */
85  template<typename T>
86  int CNetCdfInterface::getVaraType(int ncid, int varId, const StdSize* start, const StdSize* count, T* data)
87  {
88    int 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.