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

Last change on this file since 1334 was 1334, checked in by yushan, 6 years ago

omp_dev

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