Ignore:
Timestamp:
10/09/14 12:28:26 (10 years ago)
Author:
mhnguyen
Message:

Making a wrapper of NetCdf? C functions

+) Add a wrapper class for NetCdf? functions
+) Add a class to manage exception for NetCdf? functions
+) Replace direct call to netcdf functions by functions of the wrapper

Test
+) On Curie
+) In case of error, exception is thrown and more information is provided

File:
1 edited

Legend:

Unmodified
Added
Removed
  • XIOS/trunk/src/output/onetcdf4.cpp

    r472 r498  
    33#include "mpi.hpp" 
    44#include "netcdf.hpp" 
     5#include "netCdfInterface.hpp" 
     6#include "netCdfException.hpp" 
    57 
    68namespace xios 
     
    1517         this->initialize(filename, exist, comm,multifile); 
    1618      } 
    17        
    18       //--------------------------------------------------------------- 
    19        
    20        
     19 
     20      //--------------------------------------------------------------- 
     21 
    2122 
    2223      CONetCDF4::~CONetCDF4(void) 
     
    3435            if (comm != NULL) 
    3536            { 
    36                if (!multifile) CheckError(xios::nc_create_par(filename.c_str(), NC_NETCDF4|NC_MPIIO, *comm, MPI_INFO_NULL, &this->ncidp)); 
    37                else CheckError(nc_create(filename.c_str(), NC_NETCDF4, &this->ncidp)); 
     37               if (!multifile) (CNetCdfInterface::createPar(filename, NC_NETCDF4|NC_MPIIO, *comm, MPI_INFO_NULL, (this->ncidp))); 
     38               else (CNetCdfInterface::create(filename, NC_NETCDF4, this->ncidp)); 
    3839            } 
    39             else CheckError(nc_create(filename.c_str(), NC_NETCDF4, &this->ncidp)); 
     40            else (CNetCdfInterface::create(filename, NC_NETCDF4, this->ncidp)); 
    4041         } 
    4142         else 
     
    4344            if (comm != NULL) 
    4445            { 
    45                if (!multifile) CheckError(xios::nc_open_par(filename.c_str(), NC_NETCDF4|NC_MPIIO, *comm, MPI_INFO_NULL, &this->ncidp)); 
    46                else CheckError(nc_open(filename.c_str(), NC_NETCDF4, &this->ncidp)); 
     46               if (!multifile) (CNetCdfInterface::openPar(filename, NC_NETCDF4|NC_MPIIO, *comm, MPI_INFO_NULL, this->ncidp)); 
     47               else (CNetCdfInterface::open(filename, NC_NETCDF4, this->ncidp)); 
    4748            } 
    48             else  CheckError(nc_open(filename.c_str(), NC_NETCDF4, &this->ncidp)); 
    49          } 
    50       } 
    51        
     49            else  (CNetCdfInterface::open(filename, NC_NETCDF4, this->ncidp)); 
     50         } 
     51      } 
     52 
    5253      void CONetCDF4::close() 
    5354      { 
    54         CheckError(nc_close(this->ncidp)); 
    55       } 
    56        
    57       //--------------------------------------------------------------- 
    58        
     55        (CNetCdfInterface::close(this->ncidp)); 
     56      } 
     57 
     58      //--------------------------------------------------------------- 
     59 
    5960      void CONetCDF4::definition_start(void) 
    60       {  
    61          CheckError(nc_redef(this->ncidp)); 
    62       } 
    63        
    64       //--------------------------------------------------------------- 
    65        
     61      { 
     62         (CNetCdfInterface::reDef(this->ncidp)); 
     63      } 
     64 
     65      //--------------------------------------------------------------- 
     66 
    6667      void CONetCDF4::definition_end(void) 
    67       {  
    68          CheckError(nc_enddef(this->ncidp)); 
    69       } 
    70        
    71       //--------------------------------------------------------------- 
    72        
    73       void CONetCDF4::CheckError(int status) 
    74       { 
    75          if (status != NC_NOERR) 
    76          { 
    77             StdString errormsg (nc_strerror(status)); // fuite mémoire ici ? 
    78             ERROR("CONetCDF4::CheckError(int status)", 
    79                   << "[ status = " << status << " ] " << errormsg); 
    80          } 
    81       } 
    82  
    83       //--------------------------------------------------------------- 
    84        
     68      { 
     69         (CNetCdfInterface::endDef(this->ncidp)); 
     70      } 
     71 
     72      //--------------------------------------------------------------- 
     73 
     74// Don't need it anymore? 
     75//      void CONetCDF4::CheckError(int status) 
     76//      { 
     77//         if (status != NC_NOERR) 
     78//         { 
     79//            StdString errormsg (nc_strerror(status)); // fuite mémoire ici ? 
     80//            ERROR("CONetCDF4::CheckError(int status)", 
     81//                  << "[ status = " << status << " ] " << errormsg); 
     82//         } 
     83//      } 
     84 
     85      //--------------------------------------------------------------- 
     86 
    8587      int CONetCDF4::getCurrentGroup(void) 
    8688      { 
    8789         return (this->getGroup(this->getCurrentPath())); 
    8890      } 
    89        
    90       //--------------------------------------------------------------- 
    91        
     91 
     92      //--------------------------------------------------------------- 
     93 
    9294      int CONetCDF4::getGroup(const CONetCDF4Path & path) 
    9395      { 
    9496         int retvalue = this->ncidp; 
    95           
     97 
    9698         CONetCDF4Path::const_iterator 
    9799            it  = path.begin(), end = path.end(); 
     
    100102         { 
    101103            const StdString & groupid = *it; 
    102             CheckError(nc_inq_ncid(retvalue, const_cast<char*>(groupid.c_str()), &retvalue)); 
     104            (CNetCdfInterface::inqNcId(retvalue, groupid, retvalue)); 
    103105         } 
    104106         return (retvalue); 
    105107      } 
    106        
    107       //--------------------------------------------------------------- 
    108        
     108 
     109      //--------------------------------------------------------------- 
     110 
    109111      int CONetCDF4::getVariable(const StdString & varname) 
    110112      { 
    111113         int varid = 0; 
    112114         int grpid = this->getCurrentGroup(); 
    113          CheckError(nc_inq_varid (grpid, varname.c_str(), &varid)); 
     115         (CNetCdfInterface::inqVarId(grpid, varname, varid)); 
    114116         return (varid); 
    115117      } 
    116        
    117       //--------------------------------------------------------------- 
    118        
     118 
     119      //--------------------------------------------------------------- 
     120 
    119121      int CONetCDF4::getDimension(const StdString & dimname) 
    120122      { 
    121123         int dimid = 0; 
    122124         int grpid = this->getCurrentGroup(); 
    123          CheckError(nc_inq_dimid (grpid, dimname.c_str(), &dimid)); 
     125         (CNetCdfInterface::inqDimId(grpid, dimname, dimid)); 
    124126         return (dimid); 
    125127      } 
    126        
    127       //--------------------------------------------------------------- 
    128        
     128 
     129      //--------------------------------------------------------------- 
     130 
    129131      int CONetCDF4::getUnlimitedDimension(void) 
    130132      { 
    131133         int dimid = 0; 
    132134         int grpid = this->getCurrentGroup(); 
    133          CheckError(nc_inq_unlimdim (grpid, &dimid)); 
     135         (CNetCdfInterface::inqUnLimDim(grpid, dimid)); 
    134136         return (dimid); 
    135137      } 
    136        
     138 
    137139      StdString CONetCDF4::getUnlimitedDimensionName(void) 
    138140      { 
    139          char full_name_in[NC_MAX_NAME +1]; 
    140141         int grpid = this->getGroup(path); 
    141142         int dimid = this->getUnlimitedDimension(); 
    142                                
     143 
    143144         if (dimid == -1) return (std::string()); 
    144             CheckError(nc_inq_dimname(grpid, dimid, full_name_in)); 
    145                                            
    146          StdString dimname(full_name_in); 
     145         StdString dimname; 
     146         (CNetCdfInterface::inqDimName(grpid, dimid, dimname)); 
     147 
    147148         return (dimname); 
    148149      } 
    149        
    150       //--------------------------------------------------------------- 
    151        
     150 
     151      //--------------------------------------------------------------- 
     152 
    152153      std::vector<StdSize> CONetCDF4::getDimensions(const StdString & varname) 
    153154      { 
     
    158159         int nbdim = 0, *dimid = NULL; 
    159160 
    160          CheckError(nc_inq_varndims(grpid, varid, &nbdim)); 
     161         (CNetCdfInterface::inqVarNDims(grpid, varid, nbdim)); 
    161162         dimid = new int[nbdim](); 
    162          CheckError(nc_inq_vardimid(grpid, varid, dimid)); 
     163         (CNetCdfInterface::inqVarDimId(grpid, varid, dimid)); 
    163164 
    164165         for (int i = 0; i < nbdim; i++) 
    165166         { 
    166             CheckError(nc_inq_dimlen (grpid, dimid[i], &size)); 
     167            (CNetCdfInterface::inqDimLen(grpid, dimid[i], size)); 
    167168            if (size == NC_UNLIMITED) 
    168169                size = UNLIMITED_DIM; 
     
    175176      std::vector<std::string> CONetCDF4::getDimensionsIdList (const std::string * _varname) 
    176177      { 
    177          char full_name_in[NC_MAX_NAME +1]; 
     178         int nDimNull = 0; 
    178179         int nbdim = 0, *dimid = NULL; 
    179180         int grpid = this->getCurrentGroup(); 
    180181         int varid = (_varname != NULL) ? this->getVariable(*_varname) : NC_GLOBAL; 
    181182         std::vector<std::string> retvalue; 
    182                                     
     183 
    183184         if (_varname != NULL) 
    184185         { 
    185             CheckError(nc_inq_varndims(grpid, varid, &nbdim)); 
     186            (CNetCdfInterface::inqVarNDims(grpid, varid, nbdim)); 
    186187            dimid = new int[nbdim](); 
    187             CheckError(nc_inq_vardimid(grpid, varid, dimid)); 
     188            (CNetCdfInterface::inqVarDimId(grpid, varid, dimid)); 
    188189         } 
    189190         else 
    190191         { 
    191             CheckError(nc_inq_dimids(grpid, &nbdim, NULL, 1)); 
     192            (CNetCdfInterface::inqDimIds(grpid, nbdim, NULL, 1)); 
    192193            dimid = new int[nbdim](); 
    193             CheckError(nc_inq_dimids(grpid, NULL, dimid, 1)); 
    194          } 
    195                                          
     194            (CNetCdfInterface::inqDimIds(grpid, nDimNull, dimid, 1)); 
     195         } 
     196 
    196197         for (int i = 0; i < nbdim; i++) 
    197198         { 
    198             CheckError(nc_inq_dimname(grpid, dimid[i], full_name_in)); 
    199             std::string dimname(full_name_in); 
     199            std::string dimname; 
     200            (CNetCdfInterface::inqDimName(grpid, dimid[i], dimname)); 
    200201            retvalue.push_back(dimname); 
    201202         } 
    202203         delete [] dimid; 
    203                                                                                                                                                        
     204 
    204205         return (retvalue); 
    205206      } 
     
    220221         int retvalue = 0; 
    221222         int grpid = this->getCurrentGroup(); 
    222          CheckError(nc_def_grp(grpid, const_cast<char*>(name.c_str()), &retvalue)); 
     223         (CNetCdfInterface::defGrp(grpid, name, retvalue)); 
    223224         return (retvalue); 
    224225      } 
    225        
    226       //--------------------------------------------------------------- 
    227        
     226 
     227      //--------------------------------------------------------------- 
     228 
    228229      int CONetCDF4::addDimension(const StdString& name, const StdSize size) 
    229230      { 
     
    231232         int grpid = this->getCurrentGroup(); 
    232233         if (size != UNLIMITED_DIM) 
    233             CheckError(nc_def_dim (grpid, name.c_str(), size, &retvalue)); 
     234            (CNetCdfInterface::defDim(grpid, name, size, retvalue)); 
    234235         else 
    235             CheckError(nc_def_dim (grpid, name.c_str(), NC_UNLIMITED, &retvalue)); 
     236            (CNetCdfInterface::defDim(grpid, name, NC_UNLIMITED, retvalue)); 
    236237         return (retvalue); 
    237238      } 
    238        
    239       //--------------------------------------------------------------- 
    240        
     239 
     240      //--------------------------------------------------------------- 
     241 
    241242      int CONetCDF4::addVariable(const StdString & name, nc_type type, 
    242243                                  const std::vector<StdString> & dim) 
     
    248249         StdSize totalSize ; 
    249250         StdSize maxSize=1024*1024*256 ; // == 2GB/8 if output double 
    250           
    251          int grpid = this->getCurrentGroup(); 
    252           
     251 
     252         int grpid = this->getCurrentGroup(); 
     253 
    253254         std::vector<StdString>::const_iterator 
    254255            it  = dim.begin(), end = dim.end(); 
     
    258259            const StdString & dimid = *it; 
    259260            dimids.push_back(this->getDimension(dimid)); 
    260             CheckError(nc_inq_dimlen (grpid, this->getDimension(dimid), &size)); 
     261            (CNetCdfInterface::inqDimLen(grpid, this->getDimension(dimid), size)); 
    261262            if (size==NC_UNLIMITED) size=1 ; 
    262263            dimsizes.push_back(size) ; 
    263264         } 
    264           
    265          CheckError(nc_def_var (grpid, name.c_str(), type, dimids.size(), &(dimids[0]), &varid)); 
     265 
     266         (CNetCdfInterface::defVar(grpid, name, type, dimids.size(), &(dimids[0]), varid)); 
    266267 
    267268// set chunksize : size of one record 
     
    274275         } 
    275276 
    276          CheckError(nc_def_var_chunking (grpid, varid, NC_CHUNKED, &(dimsizes[0]))); 
    277          CheckError(nc_def_var_fill(grpid, varid, true, NULL)); 
     277         (CNetCdfInterface::defVarChunking(grpid, varid, NC_CHUNKED, &(dimsizes[0]))); 
     278         (CNetCdfInterface::defVarFill(grpid, varid, true, NULL)); 
    278279         return (varid); 
    279280      } 
     
    287288         int grpid = this->getCurrentGroup(); 
    288289         int varid = (varname == NULL) ? NC_GLOBAL : this->getVariable(*varname); 
    289          CheckError(nc_put_att(grpid, varid, name.c_str(), NC_CHAR, value.size(), value.c_str())); 
     290         (CNetCdfInterface::putAtt(grpid, varid, name, NC_CHAR, value.size(), value.c_str())); 
    290291         //CheckError(nc_put_att_string(grpid, varid, name.c_str(), 1, &str)); 
    291292      } 
    292        
    293       //--------------------------------------------------------------- 
    294        
     293 
     294      //--------------------------------------------------------------- 
     295 
    295296      template <> 
    296297         void CONetCDF4::addAttribute 
     
    299300         int grpid = this->getCurrentGroup(); 
    300301         int varid = (varname == NULL) ? NC_GLOBAL : this->getVariable(*varname); 
    301          CheckError(nc_put_att_double(grpid, varid, name.c_str(), NC_DOUBLE,1, &value)); 
     302         (CNetCdfInterface::putAttType(grpid, varid, name, 1, &value)); 
    302303      } 
    303304 
     
    308309         int grpid = this->getCurrentGroup(); 
    309310         int varid = (varname == NULL) ? NC_GLOBAL : this->getVariable(*varname); 
    310          CheckError(nc_put_att_double(grpid, varid, name.c_str(), NC_DOUBLE,value.numElements(), value.dataFirst())); 
    311       }      
    312       //--------------------------------------------------------------- 
    313        
     311         (CNetCdfInterface::putAttType(grpid, varid, name, value.numElements(), value.dataFirst())); 
     312      } 
     313      //--------------------------------------------------------------- 
     314 
    314315      template <> 
    315316         void CONetCDF4::addAttribute 
     
    318319         int grpid = this->getCurrentGroup(); 
    319320         int varid = (varname == NULL) ? NC_GLOBAL : this->getVariable(*varname); 
    320          CheckError(nc_put_att_float(grpid, varid, name.c_str(), NC_FLOAT, 1, &value)); 
     321         (CNetCdfInterface::putAttType(grpid, varid, name, 1, &value)); 
    321322      } 
    322323 
     
    327328         int grpid = this->getCurrentGroup(); 
    328329         int varid = (varname == NULL) ? NC_GLOBAL : this->getVariable(*varname); 
    329          CheckError(nc_put_att_float(grpid, varid, name.c_str(), NC_FLOAT,value.numElements(), value.dataFirst())); 
    330       }      
    331        
    332       //--------------------------------------------------------------- 
    333        
     330         (CNetCdfInterface::putAttType(grpid, varid, name, value.numElements(), value.dataFirst())); 
     331      } 
     332 
     333      //--------------------------------------------------------------- 
     334 
    334335      template <> 
    335336         void CONetCDF4::addAttribute 
     
    338339         int grpid = this->getCurrentGroup(); 
    339340         int varid = (varname == NULL) ? NC_GLOBAL : this->getVariable(*varname); 
    340          CheckError(nc_put_att_int(grpid, varid, name.c_str(), NC_INT,1, &value)); 
     341         (CNetCdfInterface::putAttType(grpid, varid, name, 1, &value)); 
    341342      } 
    342343 
     
    347348         int grpid = this->getCurrentGroup(); 
    348349         int varid = (varname == NULL) ? NC_GLOBAL : this->getVariable(*varname); 
    349          CheckError(nc_put_att_int(grpid, varid, name.c_str(), NC_INT,value.numElements(), value.dataFirst())); 
    350       }      
    351        
    352        
    353   
     350         (CNetCdfInterface::putAttType(grpid, varid, name, value.numElements(), value.dataFirst())); 
     351      } 
     352 
    354353      template <> 
    355354         void CONetCDF4::addAttribute 
     
    358357         int grpid = this->getCurrentGroup(); 
    359358         int varid = (varname == NULL) ? NC_GLOBAL : this->getVariable(*varname); 
    360          CheckError(nc_put_att_short(grpid, varid, name.c_str(), NC_SHORT,1, &value)); 
     359         (CNetCdfInterface::putAttType(grpid, varid, name, 1, &value)); 
    361360      } 
    362361 
     
    367366         int grpid = this->getCurrentGroup(); 
    368367         int varid = (varname == NULL) ? NC_GLOBAL : this->getVariable(*varname); 
    369          CheckError(nc_put_att_short(grpid, varid, name.c_str(), NC_SHORT,value.numElements(), value.dataFirst())); 
    370       }      
    371        
    372        
    373        
     368         (CNetCdfInterface::putAttType(grpid, varid, name, value.numElements(), value.dataFirst())); 
     369      } 
     370 
     371 
     372 
    374373      template <> 
    375374         void CONetCDF4::addAttribute 
     
    378377         int grpid = this->getCurrentGroup(); 
    379378         int varid = (varname == NULL) ? NC_GLOBAL : this->getVariable(*varname); 
    380          CheckError(nc_put_att_long(grpid, varid, name.c_str(), NC_LONG,1, &value)); 
     379         (CNetCdfInterface::putAttType(grpid, varid, name, 1, &value)); 
    381380      } 
    382381 
     
    387386         int grpid = this->getCurrentGroup(); 
    388387         int varid = (varname == NULL) ? NC_GLOBAL : this->getVariable(*varname); 
    389          CheckError(nc_put_att_long(grpid, varid, name.c_str(), NC_LONG, value.numElements(), value.dataFirst())); 
    390       }  
    391        
    392        
    393        
     388         (CNetCdfInterface::putAttType(grpid, varid, name, value.numElements(), value.dataFirst())); 
     389      } 
     390 
     391 
     392 
    394393                //--------------------------------------------------------------- 
    395394 
     
    399398                                        const std::vector<StdSize> * start, 
    400399                                        const std::vector<StdSize> * count) 
    401       {    
     400      { 
    402401         std::vector<std::size_t> sizes  = this->getDimensions(name); 
    403          std::vector<std::string> iddims = this->getDimensionsIdList (&name);    
     402         std::vector<std::string> iddims = this->getDimensionsIdList (&name); 
    404403         std::vector<std::size_t>::const_iterator 
    405404            it  = sizes.begin(), end = sizes.end(); 
     
    416415 
    417416         for (;it != end; it++) 
    418          {       
     417         {  
    419418            if ((start != NULL) && (count != NULL)) 
    420419            { 
     
    432431            } 
    433432         } 
    434           
    435       } 
    436        
    437                       
    438   
     433 
     434      } 
     435 
     436 
     437 
    439438      template <> 
    440439         void CONetCDF4::writeData_(int grpid, int varid, 
     
    442441                                    const std::vector<StdSize> & scount, const double * data) 
    443442      { 
    444          CheckError(nc_put_vara_double(grpid, varid, &(sstart[0]), &(scount[0]), data)); 
     443         (CNetCdfInterface::putVaraType(grpid, varid, &(sstart[0]), &(scount[0]), data)); 
    445444//         sync() ; 
    446445      } 
    447        
    448       //--------------------------------------------------------------- 
    449        
     446 
     447      //--------------------------------------------------------------- 
     448 
    450449      template <> 
    451450         void CONetCDF4::writeData_(int grpid, int varid, 
     
    453452                                    const std::vector<StdSize> & scount, const int * data) 
    454453      { 
    455           CheckError(nc_put_vara_int(grpid, varid, &(sstart[0]), &(scount[0]), data)); 
     454          (CNetCdfInterface::putVaraType(grpid, varid, &(sstart[0]), &(scount[0]), data)); 
    456455//          sync() ; 
    457456      } 
    458        
    459       //--------------------------------------------------------------- 
    460        
     457 
     458      //--------------------------------------------------------------- 
     459 
    461460      template <> 
    462461         void CONetCDF4::writeData_(int grpid, int varid, 
     
    464463                                    const std::vector<StdSize> & scount, const float * data) 
    465464      { 
    466           CheckError(nc_put_vara_float(grpid, varid, &(sstart[0]), &(scount[0]), data)); 
     465          (CNetCdfInterface::putVaraType(grpid, varid, &(sstart[0]), &(scount[0]), data)); 
    467466//          sync() ; 
    468467      } 
     
    486485         int grpid = this->getCurrentGroup(); 
    487486         int varid = this->getVariable(name); 
    488           
     487 
    489488         map<int,size_t>::iterator it=timeAxis.find(varid) ; 
    490489         if (it==timeAxis.end()) timeAxis[varid]=record ; 
    491          else  
     490         else 
    492491         { 
    493492           if (it->second >= record) return ; 
    494493           else it->second =record ; 
    495494         } 
    496           
     495 
    497496         StdSize array_size = 1; 
    498497         std::vector<StdSize> sstart, scount; 
    499           
     498 
    500499         if (this->wmpi && collective) 
    501          CheckError(nc_var_par_access(grpid, varid, NC_COLLECTIVE)); 
     500         (CNetCdfInterface::varParAccess(grpid, varid, NC_COLLECTIVE)); 
    502501         if (this->wmpi && !collective) 
    503          CheckError(nc_var_par_access(grpid, varid, NC_INDEPENDENT)); 
    504           
     502         (CNetCdfInterface::varParAccess(grpid, varid, NC_INDEPENDENT)); 
     503 
    505504         this->getWriteDataInfos(name, record, array_size,  sstart, scount, NULL, NULL); 
    506505         if (using_netcdf_internal)  if (!isRoot) { sstart[0]=sstart[0]+1 ; scount[0]=0 ;} 
     
    509508 
    510509      //--------------------------------------------------------------- 
    511        
     510 
    512511      bool CONetCDF4::varExist(const StdString & varname) 
    513512      { 
    514          int varid = 0; 
    515          int grpid = this->getCurrentGroup(); 
    516          return (nc_inq_varid (grpid, varname.c_str(), &varid) == NC_NOERR); 
     513         int grpid = this->getCurrentGroup(); 
     514         return (CNetCdfInterface::isVarExisted(grpid, varname)); 
    517515      } 
    518516 
    519517      void CONetCDF4::sync(void) 
    520518      { 
    521          CheckError(nc_sync(this->ncidp)) ; 
    522       }  
     519         (CNetCdfInterface::sync(this->ncidp)) ; 
     520      } 
    523521      ///-------------------------------------------------------------- 
    524522 } // namespace xios 
Note: See TracChangeset for help on using the changeset viewer.