Changeset 686 for XIOS/trunk/src/io/inetcdf4.cpp
- Timestamp:
- 09/15/15 15:15:57 (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
XIOS/trunk/src/io/inetcdf4.cpp
r685 r686 20 20 { /* Nothing to do */ } 21 21 22 ///--------------------------------------------------------------23 24 void CINetCDF4::CheckError(int status)25 {26 if (status != NC_NOERR)27 {28 StdString errormsg(nc_strerror(status)); // fuite mémoire ici ?29 ERROR("CINetCDF4::CheckError(int status)",30 << "[ status = " << status << " ] " << errormsg);31 }32 }33 34 22 //--------------------------------------------------------------- 35 23 36 24 void CINetCDF4::close(void) 37 25 { 38 C heckError(nc_close(this->ncidp));26 CNetCdfInterface::close(this->ncidp); 39 27 } 40 28 … … 50 38 { 51 39 const StdString& groupid = *it; 52 C heckError(nc_inq_ncid(retvalue, const_cast<char*>(groupid.c_str()), &retvalue));40 CNetCdfInterface::inqNcId(retvalue, groupid, retvalue); 53 41 } 54 42 … … 61 49 int varid = 0; 62 50 int grpid = this->getGroup(path); 63 C heckError(nc_inq_varid(grpid, varname.c_str(), &varid));51 CNetCdfInterface::inqVarId(grpid, varname, varid); 64 52 return varid; 65 53 } … … 70 58 int dimid = 0; 71 59 int grpid = this->getGroup(path); 72 C heckError(nc_inq_dimid(grpid, dimname.c_str(), &dimid));60 CNetCdfInterface::inqDimId(grpid, dimname, dimid); 73 61 return dimid; 74 62 } … … 81 69 int grpid = this->getGroup(path); 82 70 int varid = (var != NULL) ? this->getVariable(*var, path) : NC_GLOBAL; 83 CheckError(nc_inq_att(grpid, varid, attname.c_str(), 84 &retvalue.first, &retvalue.second)); 71 CNetCdfInterface::inqAtt(grpid, varid, attname, retvalue.first, retvalue.second); 85 72 return retvalue; 86 73 } … … 90 77 int dimid = 0; 91 78 int grpid = this->getGroup(path); 92 C heckError(nc_inq_unlimdim(grpid, &dimid));79 CNetCdfInterface::inqUnLimDim(grpid, dimid); 93 80 return dimid; 94 81 } … … 96 83 StdString CINetCDF4::getUnlimitedDimensionName(const CVarPath* const path) 97 84 { 98 char full_name_in[NC_MAX_NAME +1];99 85 int grpid = this->getGroup(path); 100 86 int dimid = this->getUnlimitedDimension(path); 101 CheckError(nc_inq_dimname(grpid, dimid, full_name_in)); 102 103 StdString dimname(full_name_in); 87 88 StdString dimname; 89 if (dimid != -1) 90 CNetCdfInterface::inqDimName(grpid, dimid, dimname); 104 91 return dimname; 105 92 } 106 93 107 94 //--------------------------------------------------------------- 95 108 96 StdSize CINetCDF4::getNbVertex(const StdString& name, 109 97 const CVarPath* const path) … … 121 109 (this->getCoordinatesIdList(name, path).back(), path); 122 110 StdString dim = this->getDimensionsList(&bound, path).back(); 123 return (this->getDimensions(&bound, path)[dim]);111 return this->getDimensions(&bound, path)[dim]; 124 112 } 125 113 return size_t(-1); … … 130 118 std::list<StdString> CINetCDF4::getGroups(const CVarPath* const path) 131 119 { 132 StdSize strlen = 0;133 char full_name_in[NC_MAX_NAME +1];134 120 int nbgroup = 0, *groupid = NULL; 135 121 int grpid = this->getGroup(path); 136 122 std::list<StdString> retvalue; 137 123 138 C heckError(nc_inq_grps(grpid, &nbgroup, NULL));124 CNetCdfInterface::inqGrpIds(grpid, nbgroup, NULL); 139 125 groupid = new int[nbgroup](); 140 C heckError(nc_inq_grps(grpid, NULL, groupid));126 CNetCdfInterface::inqGrpIds(grpid, nbgroup, groupid); 141 127 142 128 for (int i = 0; i < nbgroup; i++) 143 129 { 144 CheckError(nc_inq_grpname_full(groupid[i], &strlen, full_name_in));145 StdString groupname(full_name_in, strlen);146 retvalue.push_back( groupname);130 StdString fullGrpName; 131 CNetCdfInterface::inqGrpFullName(groupid[i], fullGrpName); 132 retvalue.push_back(fullGrpName); 147 133 } 148 134 … … 153 139 std::list<StdString> CINetCDF4::getVariables(const CVarPath* const path) 154 140 { 155 char full_name_in[NC_MAX_NAME +1];156 141 int nbvar = 0, *varid = NULL; 157 142 int grpid = this->getGroup(path); 158 143 std::list<StdString> retvalue; 159 144 160 C heckError(nc_inq_varids(grpid, &nbvar, NULL));145 CNetCdfInterface::inqVarIds(grpid, nbvar, NULL); 161 146 varid = new int[nbvar](); 162 C heckError(nc_inq_varids(grpid, NULL, varid));147 CNetCdfInterface::inqVarIds(grpid, nbvar, varid); 163 148 164 149 for (int i = 0; i < nbvar; i++) 165 150 { 166 CheckError(nc_inq_varname(grpid, varid[i], full_name_in));167 StdString varname(full_name_in);168 retvalue.push_back(var name);151 StdString varName; 152 CNetCdfInterface::inqVarName(grpid, varid[i], varName); 153 retvalue.push_back(varName); 169 154 } 170 155 … … 175 160 StdSize CINetCDF4::getNbOfTimestep(const CVarPath* const path) 176 161 { 177 return (this->getDimensions(NULL, path)[this->getUnlimitedDimensionName(path)]);162 return this->getDimensions(NULL, path)[this->getUnlimitedDimensionName(path)]; 178 163 } 179 164 … … 214 199 std::list<StdString> CINetCDF4::getDimensionsList(const StdString* const var, const CVarPath* const path) 215 200 { 216 char full_name_in[NC_MAX_NAME +1];217 201 int nbdim = 0, *dimid = NULL; 218 202 int grpid = this->getGroup(path); … … 222 206 if (var != NULL) 223 207 { 224 C heckError(nc_inq_varndims(grpid, varid, &nbdim));208 CNetCdfInterface::inqVarNDims(grpid, varid, nbdim); 225 209 dimid = new int[nbdim](); 226 C heckError(nc_inq_vardimid(grpid, varid, dimid));210 CNetCdfInterface::inqVarDimId(grpid, varid, dimid); 227 211 } 228 212 else 229 213 { 230 C heckError(nc_inq_dimids(grpid, &nbdim, NULL, 1));214 CNetCdfInterface::inqDimIds(grpid, nbdim, NULL, 1); 231 215 dimid = new int[nbdim](); 232 C heckError(nc_inq_dimids(grpid, NULL, dimid, 1));216 CNetCdfInterface::inqDimIds(grpid, nbdim, dimid, 1); 233 217 } 234 218 235 219 for (int i = 0; i < nbdim; i++) 236 220 { 237 CheckError(nc_inq_dimname(grpid, dimid[i], full_name_in));238 StdString dimname(full_name_in);221 std::string dimname; 222 CNetCdfInterface::inqDimName(grpid, dimid[i], dimname); 239 223 retvalue.push_back(dimname); 240 224 } … … 246 230 std::map<StdString, StdSize> CINetCDF4::getDimensions(const StdString* const var, const CVarPath* const path) 247 231 { 248 StdSize size = 0;249 char full_name_in[NC_MAX_NAME + 1];250 232 int nbdim = 0, *dimid = NULL; 251 233 int grpid = this->getGroup(path); … … 255 237 if (var != NULL) 256 238 { 257 C heckError(nc_inq_varndims(grpid, varid, &nbdim));239 CNetCdfInterface::inqVarNDims(grpid, varid, nbdim); 258 240 dimid = new int[nbdim](); 259 C heckError(nc_inq_vardimid(grpid, varid, dimid));241 CNetCdfInterface::inqVarDimId(grpid, varid, dimid); 260 242 } 261 243 else 262 244 { 263 C heckError(nc_inq_dimids(grpid, &nbdim, NULL, 1));245 CNetCdfInterface::inqDimIds(grpid, nbdim, NULL, 1); 264 246 dimid = new int[nbdim](); 265 C heckError(nc_inq_dimids(grpid, NULL, dimid, 1));247 CNetCdfInterface::inqDimIds(grpid, nbdim, dimid, 1); 266 248 } 267 249 268 250 for (int i = 0; i < nbdim; i++) 269 251 { 270 CheckError(nc_inq_dimname(grpid, dimid[i], full_name_in)); 271 CheckError(nc_inq_dimlen (grpid, dimid[i], &size)); 272 273 StdString dimname(full_name_in); 252 std::string dimname; 253 CNetCdfInterface::inqDimName(grpid, dimid[i], dimname); 254 StdSize size = 0; 255 CNetCdfInterface::inqDimLen(grpid, dimid[i], size); 256 274 257 retvalue.insert(retvalue.end(), std::make_pair(dimname, size)); 275 258 } … … 279 262 } 280 263 281 std::list<StdString> CINetCDF4::getAttributes(const StdString* const var, const CVarPath* const path 264 std::list<StdString> CINetCDF4::getAttributes(const StdString* const var, const CVarPath* const path) 282 265 { 283 266 int nbatt = 0; 284 char full_name_in[NC_MAX_NAME +1];285 267 std::list<StdString> retvalue; 286 268 int grpid = this->getGroup(path); … … 288 270 289 271 if (var != NULL) 290 C heckError(nc_inq_varnatts(grpid, varid, &nbatt));272 CNetCdfInterface::inqVarNAtts(grpid, varid, nbatt); 291 273 else 292 C heckError(nc_inq_natts(grpid, &nbatt));274 CNetCdfInterface::inqNAtts(grpid, nbatt); 293 275 294 276 for (int i = 0; i < nbatt; i++) 295 277 { 296 CheckError(nc_inq_attname(grpid, varid, i, full_name_in));297 StdString attname(full_name_in);278 StdString attname; 279 CNetCdfInterface::inqAttName(grpid, varid, i, attname); 298 280 retvalue.push_back(attname); 299 281 } … … 323 305 const CVarPath* const path) 324 306 { 325 return (this->hasAttribute("missing_value", &name, path) || 326 this->hasAttribute("_FillValue", &name, path)); 307 return (this->hasAttribute("missing_value", &name, path) || this->hasAttribute("_FillValue", &name, path)); 327 308 } 328 309 … … 357 338 const CVarPath* const path) 358 339 { 359 return (this->hasAttribute("coordinates", &name, path));340 return this->hasAttribute("coordinates", &name, path); 360 341 } 361 342 … … 363 344 const CVarPath* const path) 364 345 { 365 return (this->hasAttribute("bounds", &name, path));346 return this->hasAttribute("bounds", &name, path); 366 347 } 367 348 … … 373 354 //--------------------------------------------------------------- 374 355 375 #define GET_ATTRIBUTE_VALUE(type, func_ext, type_enum) \ 376 template <> \ 377 std::vector<type> CINetCDF4::getAttributeValue(const StdString& name, \ 378 const StdString* const var, \ 379 const CVarPath* const path) \ 380 { \ 381 int grpid = this->getGroup(path); \ 382 int varid = (var != NULL) ? this->getVariable(*var, path) : NC_GLOBAL; \ 383 std::pair<nc_type , StdSize> attinfos = this->getAttribute(name, var, path); \ 384 std::vector<type> retvalue(attinfos.second); \ 385 if (attinfos.first != type_enum) \ 386 ERROR("CINetCDF4::getAttributeValue<double>(name, var, path", \ 387 << "[ name : " << name \ 388 << ", type requested :" << attinfos.first \ 389 << ", type stored : " << type_enum << "]" \ 390 << " Invalid type !"); \ 391 CheckError(nc_get_att_##func_ext(grpid, varid, name.c_str(), &(retvalue[0]))); \ 392 return retvalue; \ 393 } 394 395 GET_ATTRIBUTE_VALUE(double, double, NC_DOUBLE) 396 GET_ATTRIBUTE_VALUE(float, float, NC_FLOAT) 397 GET_ATTRIBUTE_VALUE(int, int , NC_INT) 398 GET_ATTRIBUTE_VALUE(char, text, NC_CHAR) 399 400 template <> 356 template <class T> 357 std::vector<T> CINetCDF4::getAttributeValue(const StdString& name, 358 const StdString* const var, 359 const CVarPath* const path) 360 { 361 int grpid = this->getGroup(path); 362 int varid = (var != NULL) ? this->getVariable(*var, path) : NC_GLOBAL; 363 std::pair<nc_type , StdSize> attinfos = this->getAttribute(name, var, path); 364 std::vector<T> retvalue(attinfos.second); 365 nc_type type = CNetCdfInterface::getNcType<T>(); 366 if (attinfos.first != type) 367 ERROR("CINetCDF4::getAttributeValue<T>(name, var, path)", 368 << "[ name : " << name 369 << ", type requested :" << attinfos.first 370 << ", type stored : " << type << "]" 371 << " Invalid type !"); 372 CNetCdfInterface::getAttType(grpid, varid, name.c_str(), &retvalue[0]); 373 return retvalue; 374 } 375 376 template std::vector<double> CINetCDF4::getAttributeValue(const StdString& name, 377 const StdString* const var, 378 const CVarPath* const path); 379 template std::vector<float> CINetCDF4::getAttributeValue(const StdString& name, 380 const StdString* const var, 381 const CVarPath* const path); 382 template std::vector<int> CINetCDF4::getAttributeValue(const StdString& name, 383 const StdString* const var, 384 const CVarPath* const path); 385 template std::vector<char> CINetCDF4::getAttributeValue(const StdString& name, 386 const StdString* const var, 387 const CVarPath* const path); 388 401 389 StdString CINetCDF4::getAttributeValue(const StdString& name, 402 390 const StdString* const var, 403 391 const CVarPath* const path) 404 392 { 405 std::vector<char> chart = this->getAttributeValue<std::vector<char> >(name, var, path); 406 StdString retvalue(&(chart[0]), chart.size()); 407 return retvalue; 408 } 409 410 template <> 411 int CINetCDF4::getMissingValue(const StdString& name, 412 const CVarPath* const path) 393 std::vector<char> data = this->getAttributeValue<char>(name, var, path); 394 395 return StdString(data.begin(), data.end()); 396 } 397 398 template <class T> 399 T CINetCDF4::getMissingValue(const StdString& name, const CVarPath* const path) 413 400 { 414 401 if (this->hasAttribute("missing_value", &name, path)) 415 return (this->getAttributeValue<std::vector<int> >("missing_value", &name, path)[0]);402 return this->getAttributeValue<T>("missing_value", &name, path)[0]; 416 403 if (this->hasAttribute("_FillValue", &name, path)) 417 return (this->getAttributeValue<std::vector<int> >("_FillValue", &name, path)[0]);404 return this->getAttributeValue<T>("_FillValue", &name, path)[0]; 418 405 return 0; 419 406 } 420 407 421 template <> 422 double CINetCDF4::getMissingValue(const StdString& name, 423 const CVarPath* const path) 424 { 425 if (this->hasAttribute("missing_value", &name, path)) 426 return (this->getAttributeValue<std::vector<double> >("missing_value", &name, path)[0]); 427 if (this->hasAttribute("_FillValue", &name, path)) 428 return (this->getAttributeValue<std::vector<double> >("_FillValue", &name, path)[0]); 429 return 0; 430 } 431 432 template <> 433 float CINetCDF4::getMissingValue(const StdString& name, 434 const CVarPath* const path) 435 { 436 if (this->hasAttribute("missing_value", &name, path)) 437 return (this->getAttributeValue<std::vector<float> >("missing_value", &name, path)[0]); 438 if (this->hasAttribute("_FillValue", &name, path)) 439 return (this->getAttributeValue<std::vector<float> >("_FillValue", &name, path)[0]); 440 return 0; 441 } 408 template double CINetCDF4::getMissingValue(const StdString& name, const CVarPath* const path); 409 template float CINetCDF4::getMissingValue(const StdString& name, const CVarPath* const path); 410 template int CINetCDF4::getMissingValue(const StdString& name, const CVarPath* const path); 411 template char CINetCDF4::getMissingValue(const StdString& name, const CVarPath* const path); 442 412 443 413 //--------------------------------------------------------------- … … 464 434 if (this->hasAttribute("coordinates", &name, path)) 465 435 { 466 return (this->getAttributeValue<StdString>("coordinates", &name, path));436 return this->getAttributeValue("coordinates", &name, path); 467 437 } 468 438 else … … 486 456 StdString retvalue; 487 457 if (this->hasAttribute("bounds", &name, path)) 488 ret urn (this->getAttributeValue<StdString>("bounds", &name, path));458 retvalue = this->getAttributeValue("bounds", &name, path); 489 459 return retvalue; 490 460 } … … 583 553 if (!this->hasTemporalDim(path)) return false; 584 554 std::map<StdString, StdSize> dims = this->getDimensions(&name, path); 585 if (dims.find(this->getUnlimitedDimensionName(path)) != dims.end()) 586 return true; 587 return false; 555 return (dims.find(this->getUnlimitedDimensionName(path)) != dims.end()); 588 556 } 589 557 … … 616 584 if (this->isCoordinate(name, path)) 617 585 { 618 return (this->hasBounds(name, path));586 return this->hasBounds(name, path); 619 587 } 620 588 else … … 712 680 } 713 681 714 template < >715 void CINetCDF4::getData(CArray< int, 1>& data, const StdString& var,682 template <class T> 683 void CINetCDF4::getData(CArray<T, 1>& data, const StdString& var, 716 684 const CVarPath* const path, StdSize record) 717 685 { 718 719 686 std::vector<StdSize> start, count; 720 687 int grpid = this->getGroup(path); … … 723 690 this->getDataInfo(var, path, record, start, count, array_size); 724 691 data.resize(array_size); 725 CheckError(nc_get_vara_int(grpid, varid, &(start[0]), &(count[0]), data.dataFirst())); 726 } 727 692 CNetCdfInterface::getVaraType(grpid, varid, &start[0], &count[0], data.dataFirst()); 693 } 694 695 template <> 696 void CINetCDF4::getData(CArray<int, 1>& data, const StdString& var, 697 const CVarPath* const path, StdSize record); 728 698 template <> 729 699 void CINetCDF4::getData(CArray<double, 1>& data, const StdString& var, 730 const CVarPath* const path, StdSize record) 731 { 732 std::vector<StdSize> start, count; 733 int grpid = this->getGroup(path); 734 int varid = this->getVariable(var, path); 735 StdSize array_size = 1; 736 this->getDataInfo(var, path, record, start, count, array_size); 737 data.resize(array_size); 738 CheckError(nc_get_vara_double(grpid, varid, &(start[0]), &(count[0]), data.dataFirst())); 739 } 740 700 const CVarPath* const path, StdSize record); 741 701 template <> 742 702 void CINetCDF4::getData(CArray<float, 1>& data, const StdString& var, 743 const CVarPath* const path, StdSize record) 744 { 745 std::vector<StdSize> start, count; 746 int grpid = this->getGroup(path); 747 int varid = this->getVariable(var, path); 748 StdSize array_size = 1; 749 this->getDataInfo(var, path, record, start, count, array_size); 750 data.resize(array_size); 751 CheckError(nc_get_vara_float(grpid, varid, &(start[0]), &(count[0]), data.dataFirst())); 752 } 703 const CVarPath* const path, StdSize record); 753 704 754 705 template <> … … 762 713 if (this->mpi && collective) 763 714 CNetCdfInterface::varParAccess(ncidp, varid, NC_COLLECTIVE); 764 if (this->mpi && !collective)715 else if (this->mpi && !collective) 765 716 CNetCdfInterface::varParAccess(ncidp, varid, NC_INDEPENDENT); 766 717 … … 777 728 } 778 729 779 C heckError(nc_get_vara_double(ncidp, varid, &(sstart[0]), &(scount[0]), data.dataFirst()));730 CNetCdfInterface::getVaraType(ncidp, varid, &sstart[0], &scount[0], data.dataFirst()); 780 731 } 781 732 … … 787 738 std::list<StdString> clist = this->getCoordinatesIdList(varname, path); 788 739 if (this->hasCoordinates(varname, path)) 789 return (*clist.begin());740 return *clist.begin(); 790 741 else 791 return (*clist.rbegin());742 return *clist.rbegin(); 792 743 } 793 744 … … 797 748 std::list<StdString> clist = this->getCoordinatesIdList(varname, path); 798 749 if (this->hasCoordinates(varname, path)) 799 return (*(++clist.begin()));750 return *(++clist.begin()); 800 751 else 801 return (*(++clist.rbegin()));752 return *(++clist.rbegin()); 802 753 } 803 754 … … 808 759 std::list<StdString> clist = this->getCoordinatesIdList(varname, path); 809 760 if (this->hasCoordinates(varname, path)) 810 return (*(++(++clist.begin())));761 return *(++(++clist.begin())); 811 762 else 812 return (*(++(++clist.rbegin())));763 return *(++(++clist.rbegin())); 813 764 } 814 765 } // namespace xios
Note: See TracChangeset
for help on using the changeset viewer.