Changeset 498 for XIOS/trunk/src/output/onetcdf4.cpp
- Timestamp:
- 10/09/14 12:28:26 (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
XIOS/trunk/src/output/onetcdf4.cpp
r472 r498 3 3 #include "mpi.hpp" 4 4 #include "netcdf.hpp" 5 #include "netCdfInterface.hpp" 6 #include "netCdfException.hpp" 5 7 6 8 namespace xios … … 15 17 this->initialize(filename, exist, comm,multifile); 16 18 } 17 18 //--------------------------------------------------------------- 19 20 19 20 //--------------------------------------------------------------- 21 21 22 22 23 CONetCDF4::~CONetCDF4(void) … … 34 35 if (comm != NULL) 35 36 { 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)); 38 39 } 39 else CheckError(nc_create(filename.c_str(), NC_NETCDF4, &this->ncidp));40 else (CNetCdfInterface::create(filename, NC_NETCDF4, this->ncidp)); 40 41 } 41 42 else … … 43 44 if (comm != NULL) 44 45 { 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)); 47 48 } 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 52 53 void CONetCDF4::close() 53 54 { 54 CheckError(nc_close(this->ncidp));55 } 56 57 //--------------------------------------------------------------- 58 55 (CNetCdfInterface::close(this->ncidp)); 56 } 57 58 //--------------------------------------------------------------- 59 59 60 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 66 67 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 85 87 int CONetCDF4::getCurrentGroup(void) 86 88 { 87 89 return (this->getGroup(this->getCurrentPath())); 88 90 } 89 90 //--------------------------------------------------------------- 91 91 92 //--------------------------------------------------------------- 93 92 94 int CONetCDF4::getGroup(const CONetCDF4Path & path) 93 95 { 94 96 int retvalue = this->ncidp; 95 97 96 98 CONetCDF4Path::const_iterator 97 99 it = path.begin(), end = path.end(); … … 100 102 { 101 103 const StdString & groupid = *it; 102 CheckError(nc_inq_ncid(retvalue, const_cast<char*>(groupid.c_str()), &retvalue));104 (CNetCdfInterface::inqNcId(retvalue, groupid, retvalue)); 103 105 } 104 106 return (retvalue); 105 107 } 106 107 //--------------------------------------------------------------- 108 108 109 //--------------------------------------------------------------- 110 109 111 int CONetCDF4::getVariable(const StdString & varname) 110 112 { 111 113 int varid = 0; 112 114 int grpid = this->getCurrentGroup(); 113 CheckError(nc_inq_varid (grpid, varname.c_str(), &varid));115 (CNetCdfInterface::inqVarId(grpid, varname, varid)); 114 116 return (varid); 115 117 } 116 117 //--------------------------------------------------------------- 118 118 119 //--------------------------------------------------------------- 120 119 121 int CONetCDF4::getDimension(const StdString & dimname) 120 122 { 121 123 int dimid = 0; 122 124 int grpid = this->getCurrentGroup(); 123 CheckError(nc_inq_dimid (grpid, dimname.c_str(), &dimid));125 (CNetCdfInterface::inqDimId(grpid, dimname, dimid)); 124 126 return (dimid); 125 127 } 126 127 //--------------------------------------------------------------- 128 128 129 //--------------------------------------------------------------- 130 129 131 int CONetCDF4::getUnlimitedDimension(void) 130 132 { 131 133 int dimid = 0; 132 134 int grpid = this->getCurrentGroup(); 133 CheckError(nc_inq_unlimdim (grpid, &dimid));135 (CNetCdfInterface::inqUnLimDim(grpid, dimid)); 134 136 return (dimid); 135 137 } 136 138 137 139 StdString CONetCDF4::getUnlimitedDimensionName(void) 138 140 { 139 char full_name_in[NC_MAX_NAME +1];140 141 int grpid = this->getGroup(path); 141 142 int dimid = this->getUnlimitedDimension(); 142 143 143 144 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 147 148 return (dimname); 148 149 } 149 150 //--------------------------------------------------------------- 151 150 151 //--------------------------------------------------------------- 152 152 153 std::vector<StdSize> CONetCDF4::getDimensions(const StdString & varname) 153 154 { … … 158 159 int nbdim = 0, *dimid = NULL; 159 160 160 CheckError(nc_inq_varndims(grpid, varid, &nbdim));161 (CNetCdfInterface::inqVarNDims(grpid, varid, nbdim)); 161 162 dimid = new int[nbdim](); 162 CheckError(nc_inq_vardimid(grpid, varid, dimid));163 (CNetCdfInterface::inqVarDimId(grpid, varid, dimid)); 163 164 164 165 for (int i = 0; i < nbdim; i++) 165 166 { 166 CheckError(nc_inq_dimlen (grpid, dimid[i], &size));167 (CNetCdfInterface::inqDimLen(grpid, dimid[i], size)); 167 168 if (size == NC_UNLIMITED) 168 169 size = UNLIMITED_DIM; … … 175 176 std::vector<std::string> CONetCDF4::getDimensionsIdList (const std::string * _varname) 176 177 { 177 char full_name_in[NC_MAX_NAME +1];178 int nDimNull = 0; 178 179 int nbdim = 0, *dimid = NULL; 179 180 int grpid = this->getCurrentGroup(); 180 181 int varid = (_varname != NULL) ? this->getVariable(*_varname) : NC_GLOBAL; 181 182 std::vector<std::string> retvalue; 182 183 183 184 if (_varname != NULL) 184 185 { 185 CheckError(nc_inq_varndims(grpid, varid, &nbdim));186 (CNetCdfInterface::inqVarNDims(grpid, varid, nbdim)); 186 187 dimid = new int[nbdim](); 187 CheckError(nc_inq_vardimid(grpid, varid, dimid));188 (CNetCdfInterface::inqVarDimId(grpid, varid, dimid)); 188 189 } 189 190 else 190 191 { 191 CheckError(nc_inq_dimids(grpid, &nbdim, NULL, 1));192 (CNetCdfInterface::inqDimIds(grpid, nbdim, NULL, 1)); 192 193 dimid = new int[nbdim](); 193 CheckError(nc_inq_dimids(grpid, NULL, dimid, 1));194 } 195 194 (CNetCdfInterface::inqDimIds(grpid, nDimNull, dimid, 1)); 195 } 196 196 197 for (int i = 0; i < nbdim; i++) 197 198 { 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)); 200 201 retvalue.push_back(dimname); 201 202 } 202 203 delete [] dimid; 203 204 204 205 return (retvalue); 205 206 } … … 220 221 int retvalue = 0; 221 222 int grpid = this->getCurrentGroup(); 222 CheckError(nc_def_grp(grpid, const_cast<char*>(name.c_str()), &retvalue));223 (CNetCdfInterface::defGrp(grpid, name, retvalue)); 223 224 return (retvalue); 224 225 } 225 226 //--------------------------------------------------------------- 227 226 227 //--------------------------------------------------------------- 228 228 229 int CONetCDF4::addDimension(const StdString& name, const StdSize size) 229 230 { … … 231 232 int grpid = this->getCurrentGroup(); 232 233 if (size != UNLIMITED_DIM) 233 CheckError(nc_def_dim (grpid, name.c_str(), size, &retvalue));234 (CNetCdfInterface::defDim(grpid, name, size, retvalue)); 234 235 else 235 CheckError(nc_def_dim (grpid, name.c_str(), NC_UNLIMITED, &retvalue));236 (CNetCdfInterface::defDim(grpid, name, NC_UNLIMITED, retvalue)); 236 237 return (retvalue); 237 238 } 238 239 //--------------------------------------------------------------- 240 239 240 //--------------------------------------------------------------- 241 241 242 int CONetCDF4::addVariable(const StdString & name, nc_type type, 242 243 const std::vector<StdString> & dim) … … 248 249 StdSize totalSize ; 249 250 StdSize maxSize=1024*1024*256 ; // == 2GB/8 if output double 250 251 int grpid = this->getCurrentGroup(); 252 251 252 int grpid = this->getCurrentGroup(); 253 253 254 std::vector<StdString>::const_iterator 254 255 it = dim.begin(), end = dim.end(); … … 258 259 const StdString & dimid = *it; 259 260 dimids.push_back(this->getDimension(dimid)); 260 CheckError(nc_inq_dimlen (grpid, this->getDimension(dimid), &size));261 (CNetCdfInterface::inqDimLen(grpid, this->getDimension(dimid), size)); 261 262 if (size==NC_UNLIMITED) size=1 ; 262 263 dimsizes.push_back(size) ; 263 264 } 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)); 266 267 267 268 // set chunksize : size of one record … … 274 275 } 275 276 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)); 278 279 return (varid); 279 280 } … … 287 288 int grpid = this->getCurrentGroup(); 288 289 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())); 290 291 //CheckError(nc_put_att_string(grpid, varid, name.c_str(), 1, &str)); 291 292 } 292 293 //--------------------------------------------------------------- 294 293 294 //--------------------------------------------------------------- 295 295 296 template <> 296 297 void CONetCDF4::addAttribute … … 299 300 int grpid = this->getCurrentGroup(); 300 301 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)); 302 303 } 303 304 … … 308 309 int grpid = this->getCurrentGroup(); 309 310 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 314 315 template <> 315 316 void CONetCDF4::addAttribute … … 318 319 int grpid = this->getCurrentGroup(); 319 320 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)); 321 322 } 322 323 … … 327 328 int grpid = this->getCurrentGroup(); 328 329 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 334 335 template <> 335 336 void CONetCDF4::addAttribute … … 338 339 int grpid = this->getCurrentGroup(); 339 340 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)); 341 342 } 342 343 … … 347 348 int grpid = this->getCurrentGroup(); 348 349 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 354 353 template <> 355 354 void CONetCDF4::addAttribute … … 358 357 int grpid = this->getCurrentGroup(); 359 358 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)); 361 360 } 362 361 … … 367 366 int grpid = this->getCurrentGroup(); 368 367 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 374 373 template <> 375 374 void CONetCDF4::addAttribute … … 378 377 int grpid = this->getCurrentGroup(); 379 378 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)); 381 380 } 382 381 … … 387 386 int grpid = this->getCurrentGroup(); 388 387 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 394 393 //--------------------------------------------------------------- 395 394 … … 399 398 const std::vector<StdSize> * start, 400 399 const std::vector<StdSize> * count) 401 { 400 { 402 401 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); 404 403 std::vector<std::size_t>::const_iterator 405 404 it = sizes.begin(), end = sizes.end(); … … 416 415 417 416 for (;it != end; it++) 418 { 417 { 419 418 if ((start != NULL) && (count != NULL)) 420 419 { … … 432 431 } 433 432 } 434 435 } 436 437 438 433 434 } 435 436 437 439 438 template <> 440 439 void CONetCDF4::writeData_(int grpid, int varid, … … 442 441 const std::vector<StdSize> & scount, const double * data) 443 442 { 444 CheckError(nc_put_vara_double(grpid, varid, &(sstart[0]), &(scount[0]), data));443 (CNetCdfInterface::putVaraType(grpid, varid, &(sstart[0]), &(scount[0]), data)); 445 444 // sync() ; 446 445 } 447 448 //--------------------------------------------------------------- 449 446 447 //--------------------------------------------------------------- 448 450 449 template <> 451 450 void CONetCDF4::writeData_(int grpid, int varid, … … 453 452 const std::vector<StdSize> & scount, const int * data) 454 453 { 455 CheckError(nc_put_vara_int(grpid, varid, &(sstart[0]), &(scount[0]), data));454 (CNetCdfInterface::putVaraType(grpid, varid, &(sstart[0]), &(scount[0]), data)); 456 455 // sync() ; 457 456 } 458 459 //--------------------------------------------------------------- 460 457 458 //--------------------------------------------------------------- 459 461 460 template <> 462 461 void CONetCDF4::writeData_(int grpid, int varid, … … 464 463 const std::vector<StdSize> & scount, const float * data) 465 464 { 466 CheckError(nc_put_vara_float(grpid, varid, &(sstart[0]), &(scount[0]), data));465 (CNetCdfInterface::putVaraType(grpid, varid, &(sstart[0]), &(scount[0]), data)); 467 466 // sync() ; 468 467 } … … 486 485 int grpid = this->getCurrentGroup(); 487 486 int varid = this->getVariable(name); 488 487 489 488 map<int,size_t>::iterator it=timeAxis.find(varid) ; 490 489 if (it==timeAxis.end()) timeAxis[varid]=record ; 491 else 490 else 492 491 { 493 492 if (it->second >= record) return ; 494 493 else it->second =record ; 495 494 } 496 495 497 496 StdSize array_size = 1; 498 497 std::vector<StdSize> sstart, scount; 499 498 500 499 if (this->wmpi && collective) 501 CheckError(nc_var_par_access(grpid, varid, NC_COLLECTIVE));500 (CNetCdfInterface::varParAccess(grpid, varid, NC_COLLECTIVE)); 502 501 if (this->wmpi && !collective) 503 CheckError(nc_var_par_access(grpid, varid, NC_INDEPENDENT));504 502 (CNetCdfInterface::varParAccess(grpid, varid, NC_INDEPENDENT)); 503 505 504 this->getWriteDataInfos(name, record, array_size, sstart, scount, NULL, NULL); 506 505 if (using_netcdf_internal) if (!isRoot) { sstart[0]=sstart[0]+1 ; scount[0]=0 ;} … … 509 508 510 509 //--------------------------------------------------------------- 511 510 512 511 bool CONetCDF4::varExist(const StdString & varname) 513 512 { 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)); 517 515 } 518 516 519 517 void CONetCDF4::sync(void) 520 518 { 521 CheckError(nc_sync(this->ncidp)) ;522 } 519 (CNetCdfInterface::sync(this->ncidp)) ; 520 } 523 521 ///-------------------------------------------------------------- 524 522 } // namespace xios
Note: See TracChangeset
for help on using the changeset viewer.