Ignore:
Timestamp:
09/15/15 15:15:57 (9 years ago)
Author:
rlacroix
Message:

Use the NetCDF wrapper for inputs for better error checking.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • XIOS/trunk/src/io/netCdfInterface_impl.hpp

    r685 r686  
    55   \since 06 Oct 2014 
    66 
    7    \brief Implemention of some templated functions in netCdfInterface 
     7   \brief Implementation of some templated functions in netCdfInterface 
    88 */ 
    99 
     
    1616namespace xios 
    1717{ 
    18    /*! 
    19    This function makes a request to netcdf with its id, to add or change a variable attribute or gloabl attribute, 
    20    given its name, type, number of values provided for attribute 
    21    \param [in] ncid Id of groupd(or File Id) 
    22    \param [in] varId Id of the variable 
    23    \param [in] attrName Name of the attribute 
    24    \param [in] numVal Number of values 
    25    \param [in] op Array of values provided for attribute 
    26    \return Error code 
    27    */ 
     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  */ 
    2853  template<typename T> 
    2954  int CNetCdfInterface::putAttType(int ncid, int varId, const StdString& attrName, 
    30                                    StdSize numVal, const T* op) 
     55                                   StdSize numVal, const T* data) 
    3156  { 
    32     int status = ncPutAttType(ncid, varId, attrName.c_str(), numVal, op); 
     57    int status = ncPutAttType(ncid, varId, attrName.c_str(), numVal, data); 
    3358    if (NC_NOERR != status) 
    34      { 
    35        StdString errormsg(nc_strerror(status)); 
    36        StdStringStream sstr; 
    37        sstr << "Error in calling function " << "ncPutAttType(ncid, varId, attrName.c_str(), numVal, op)" << std::endl; 
    38        sstr << errormsg << std::endl; 
    39        sstr << "Unable to set attribute " << attrName << " for a variable with id : " << varId 
    40          << " with number of attribute  " << numVal << std::endl; 
    41        StdString e = sstr.str(); 
    42        throw CNetCdfException(e); 
    43      } 
     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    } 
    4467 
    45      return status; 
     68    return status; 
    4669  } 
    4770 
    48    /*! 
    49    This function makes a request to netcdf with its id, to write variable values into netcdf file, 
    50    \param [in] ncid Id of groupd(or File Id) 
    51    \param [in] varId Id of the variable 
    52    \param [in] start Array specifying the index in the variable where the first data value will be written 
    53    \param [in] count Array specifying the edge lengths along each dimension of block data 
    54    \param [in] op Array of values provided for attribute 
    55    \return Error code 
    56    */ 
     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  */ 
    5780  template<typename T> 
    58   int CNetCdfInterface::putVaraType(int ncid, int varId, const StdSize* start, const StdSize* count, const T* op) 
     81  int CNetCdfInterface::getVaraType(int ncid, int varId, const StdSize* start, const StdSize* count, T* data) 
    5982  { 
    60     int status = ncPutVaraType(ncid, varId, start, count, op); 
     83    int status = ncGetVaraType(ncid, varId, start, count, data); 
    6184    if (NC_NOERR != status) 
    62      { 
    63        StdString errormsg(nc_strerror(status)); 
    64        StdStringStream sstr; 
    65        sstr << "Error in calling function " << "ncPutVaraType(ncid, varId, start, count, op)" << std::endl; 
    66        sstr << errormsg << std::endl; 
    67        sstr << "Unable to write value of a variable with id : " << varId << std::endl; 
    68        StdString e = sstr.str(); 
    69        throw CNetCdfException(e); 
    70      } 
     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    } 
    7192 
    72      return status; 
     93    return status; 
    7394  } 
    7495 
     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  } 
    75120} 
    76121 
Note: See TracChangeset for help on using the changeset viewer.