[219] | 1 | #include "inetcdf4.hpp" |
---|
| 2 | |
---|
| 3 | #include <boost/algorithm/string.hpp> |
---|
| 4 | |
---|
[335] | 5 | namespace xios |
---|
[219] | 6 | { |
---|
| 7 | /// ////////////////////// Définitions ////////////////////// /// |
---|
| 8 | CINetCDF4::CINetCDF4(const StdString & filename) |
---|
| 9 | { |
---|
| 10 | CheckError(nc_open(filename.c_str(), NC_NOWRITE, &this->ncidp)); |
---|
| 11 | } |
---|
| 12 | |
---|
| 13 | CINetCDF4::~CINetCDF4(void) |
---|
| 14 | { |
---|
| 15 | CheckError(nc_close(this->ncidp)); |
---|
| 16 | } |
---|
| 17 | |
---|
| 18 | ///-------------------------------------------------------------- |
---|
| 19 | |
---|
| 20 | void CINetCDF4::CheckError(int status) |
---|
| 21 | { |
---|
| 22 | if (status != NC_NOERR) |
---|
| 23 | { |
---|
| 24 | StdString errormsg (nc_strerror(status)); // fuite mémoire ici ? |
---|
| 25 | ERROR("CINetCDF4::CheckError(int status)", |
---|
| 26 | << "[ status = " << status << " ] " << errormsg); |
---|
| 27 | } |
---|
| 28 | } |
---|
| 29 | |
---|
| 30 | //--------------------------------------------------------------- |
---|
| 31 | |
---|
| 32 | int CINetCDF4::getGroup(const CVarPath * const path) |
---|
| 33 | { |
---|
| 34 | int retvalue = this->ncidp; |
---|
| 35 | if (path == NULL) return (retvalue); |
---|
| 36 | CVarPath::const_iterator it = path->begin(), end = path->end(); |
---|
| 37 | |
---|
| 38 | for (; it != end; it++) |
---|
| 39 | { |
---|
| 40 | const StdString & groupid = *it; |
---|
| 41 | CheckError(nc_inq_ncid(retvalue, const_cast<char*>(groupid.c_str()), &retvalue)); |
---|
| 42 | } |
---|
| 43 | |
---|
| 44 | return (retvalue); |
---|
| 45 | } |
---|
| 46 | |
---|
| 47 | int CINetCDF4::getVariable(const StdString & varname, |
---|
| 48 | const CVarPath * const path) |
---|
| 49 | { |
---|
| 50 | int varid = 0; |
---|
| 51 | int grpid = this->getGroup(path); |
---|
| 52 | CheckError(nc_inq_varid (grpid, varname.c_str(), &varid)); |
---|
| 53 | return (varid); |
---|
| 54 | } |
---|
| 55 | |
---|
| 56 | int CINetCDF4::getDimension(const StdString & dimname, |
---|
| 57 | const CVarPath * const path) |
---|
| 58 | { |
---|
| 59 | int dimid = 0; |
---|
| 60 | int grpid = this->getGroup(path); |
---|
| 61 | CheckError(nc_inq_dimid (grpid, dimname.c_str(), &dimid)); |
---|
| 62 | return (dimid); |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | std::pair<nc_type, StdSize> CINetCDF4::getAttribute |
---|
| 66 | (const StdString & attname, |
---|
| 67 | const StdString * const var, |
---|
| 68 | const CVarPath * const path) |
---|
| 69 | { |
---|
| 70 | std::pair<nc_type, StdSize> retvalue; |
---|
| 71 | int grpid = this->getGroup(path); |
---|
| 72 | int varid = (var != NULL) |
---|
| 73 | ? this->getVariable(*var, path) : NC_GLOBAL; |
---|
| 74 | CheckError(nc_inq_att(grpid, varid, attname.c_str(), |
---|
| 75 | &retvalue.first, &retvalue.second)); |
---|
| 76 | return (retvalue); |
---|
| 77 | } |
---|
| 78 | |
---|
| 79 | int CINetCDF4::getUnlimitedDimension(const CVarPath * const path) |
---|
| 80 | { |
---|
| 81 | int dimid = 0; |
---|
| 82 | int grpid = this->getGroup(path); |
---|
| 83 | CheckError(nc_inq_unlimdim (grpid, &dimid)); |
---|
| 84 | return (dimid); |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | StdString CINetCDF4::getUnlimitedDimensionName(const CVarPath * const path) |
---|
| 88 | { |
---|
| 89 | char full_name_in[NC_MAX_NAME +1]; |
---|
| 90 | int grpid = this->getGroup(path); |
---|
| 91 | int dimid = this->getUnlimitedDimension(path); |
---|
| 92 | CheckError(nc_inq_dimname(grpid, dimid, full_name_in)); |
---|
| 93 | |
---|
| 94 | StdString dimname(full_name_in); |
---|
| 95 | return (dimname); |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | //--------------------------------------------------------------- |
---|
| 99 | StdSize CINetCDF4::getNbVertex(const StdString & name, |
---|
| 100 | const CVarPath * const path) |
---|
| 101 | { |
---|
| 102 | |
---|
| 103 | if (this->isRectilinear(name, path) || |
---|
| 104 | this->isCurvilinear(name, path)) |
---|
| 105 | { |
---|
| 106 | if (this->is3Dim(name, path)) return (8); |
---|
| 107 | else return (4); |
---|
| 108 | } |
---|
| 109 | if (this->isUnstructured(name, path)) |
---|
| 110 | { |
---|
| 111 | StdString bound = this->getBoundsId |
---|
| 112 | (this->getCoordinatesIdList(name, path).back(), path); |
---|
| 113 | StdString dim = this->getDimensionsList(&bound, path).back(); |
---|
| 114 | return (this->getDimensions(&bound, path)[dim]); |
---|
| 115 | } |
---|
| 116 | return ((size_t)(-1)); |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | //--------------------------------------------------------------- |
---|
| 120 | |
---|
| 121 | std::list<StdString> CINetCDF4::getGroups(const CVarPath * const path) |
---|
| 122 | { |
---|
| 123 | StdSize strlen = 0; |
---|
| 124 | char full_name_in[NC_MAX_NAME +1]; |
---|
| 125 | int nbgroup = 0, *groupid = NULL; |
---|
| 126 | int grpid = this->getGroup(path); |
---|
| 127 | std::list<StdString> retvalue; |
---|
| 128 | |
---|
| 129 | CheckError(nc_inq_grps(grpid, &nbgroup, NULL)); |
---|
| 130 | groupid = new int[nbgroup](); |
---|
| 131 | CheckError(nc_inq_grps(grpid, NULL, groupid)); |
---|
| 132 | |
---|
| 133 | for (int i = 0; i < nbgroup; i++) |
---|
| 134 | { |
---|
| 135 | CheckError(nc_inq_grpname_full(groupid[i], &strlen, full_name_in)); |
---|
| 136 | StdString groupname(full_name_in, strlen); |
---|
| 137 | retvalue.push_back(groupname); |
---|
| 138 | } |
---|
| 139 | |
---|
| 140 | delete [] groupid; |
---|
| 141 | return (retvalue); |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | std::list<StdString> CINetCDF4::getVariables(const CVarPath * const path) |
---|
| 145 | { |
---|
| 146 | char full_name_in[NC_MAX_NAME +1]; |
---|
| 147 | int nbvar = 0, *varid = NULL; |
---|
| 148 | int grpid = this->getGroup(path); |
---|
| 149 | std::list<StdString> retvalue; |
---|
| 150 | |
---|
| 151 | CheckError(nc_inq_varids(grpid, &nbvar, NULL)); |
---|
| 152 | varid = new int[nbvar](); |
---|
| 153 | CheckError(nc_inq_varids(grpid, NULL, varid)); |
---|
| 154 | |
---|
| 155 | for (int i = 0; i < nbvar; i++) |
---|
| 156 | { |
---|
| 157 | CheckError(nc_inq_varname(grpid, varid[i], full_name_in)); |
---|
| 158 | StdString varname(full_name_in); |
---|
| 159 | retvalue.push_back(varname); |
---|
| 160 | } |
---|
| 161 | |
---|
| 162 | delete [] varid; |
---|
| 163 | return (retvalue); |
---|
| 164 | } |
---|
| 165 | |
---|
| 166 | StdSize CINetCDF4::getNbOfTimestep(const CVarPath * const path) |
---|
| 167 | { |
---|
| 168 | return (this->getDimensions(NULL, path)[this->getUnlimitedDimensionName(path)]); |
---|
| 169 | } |
---|
| 170 | |
---|
| 171 | std::set<StdString> CINetCDF4::getBoundVariables(const CVarPath * const path) |
---|
| 172 | { |
---|
| 173 | std::set<StdString> retvalue; |
---|
| 174 | std::list<StdString> variables = this->getVariables(path); |
---|
| 175 | std::list<StdString>::const_iterator it = variables.begin(), end = variables.end(); |
---|
| 176 | for (; it != end; it++) |
---|
| 177 | { |
---|
| 178 | const StdString & var = *it; |
---|
| 179 | if (this->hasBounds(var, path)) |
---|
| 180 | retvalue.insert(retvalue.end(), this->getBoundsId(var, path)); |
---|
| 181 | } |
---|
| 182 | return (retvalue); |
---|
| 183 | } |
---|
| 184 | |
---|
| 185 | std::set<StdString> CINetCDF4::getCoordVariables(const CVarPath * const path) |
---|
| 186 | { |
---|
| 187 | std::set<StdString> retvalue; |
---|
| 188 | std::list<StdString> variables = this->getVariables(path); |
---|
| 189 | std::list<StdString>::const_iterator it = variables.begin(), end = variables.end(); |
---|
| 190 | for (; it != end; it++) |
---|
| 191 | { |
---|
| 192 | const StdString & var = *it; |
---|
| 193 | std::list<StdString> coords = this->getCoordinatesIdList(var, path); |
---|
| 194 | std::list<StdString>::const_iterator it = coords.begin(), end = coords.end(); |
---|
| 195 | for (; it != end; it++) |
---|
| 196 | { |
---|
| 197 | const StdString & coord = *it; |
---|
| 198 | if (this->hasVariable(coord, path)) |
---|
| 199 | retvalue.insert(retvalue.end(), coord); |
---|
| 200 | } |
---|
| 201 | } |
---|
| 202 | return (retvalue); |
---|
| 203 | } |
---|
| 204 | |
---|
| 205 | std::list<StdString> CINetCDF4::getDimensionsList |
---|
| 206 | (const StdString * const var, const CVarPath * const path) |
---|
| 207 | { |
---|
| 208 | char full_name_in[NC_MAX_NAME +1]; |
---|
| 209 | int nbdim = 0, *dimid = NULL; |
---|
| 210 | int grpid = this->getGroup(path); |
---|
| 211 | int varid = (var != NULL) |
---|
| 212 | ? this->getVariable(*var, path) : NC_GLOBAL; |
---|
| 213 | std::list<StdString> retvalue; |
---|
| 214 | |
---|
| 215 | if (var != NULL) |
---|
| 216 | { |
---|
| 217 | CheckError(nc_inq_varndims(grpid, varid, &nbdim)); |
---|
| 218 | dimid = new int[nbdim](); |
---|
| 219 | CheckError(nc_inq_vardimid(grpid, varid, dimid)); |
---|
| 220 | } |
---|
| 221 | else |
---|
| 222 | { |
---|
| 223 | CheckError(nc_inq_dimids(grpid, &nbdim, NULL, 1)); |
---|
| 224 | dimid = new int[nbdim](); |
---|
| 225 | CheckError(nc_inq_dimids(grpid, NULL, dimid, 1)); |
---|
| 226 | } |
---|
| 227 | |
---|
| 228 | for (int i = 0; i < nbdim; i++) |
---|
| 229 | { |
---|
| 230 | CheckError(nc_inq_dimname(grpid, dimid[i], full_name_in)); |
---|
| 231 | StdString dimname(full_name_in); |
---|
| 232 | retvalue.push_back(dimname); |
---|
| 233 | } |
---|
| 234 | delete [] dimid; |
---|
| 235 | |
---|
| 236 | return (retvalue); |
---|
| 237 | } |
---|
| 238 | |
---|
| 239 | std::map<StdString, StdSize> CINetCDF4::getDimensions |
---|
| 240 | (const StdString * const var, const CVarPath * const path) |
---|
| 241 | { |
---|
| 242 | StdSize size = 0; |
---|
| 243 | char full_name_in[NC_MAX_NAME +1]; |
---|
| 244 | int nbdim = 0, *dimid = NULL; |
---|
| 245 | int grpid = this->getGroup(path); |
---|
| 246 | int varid = (var != NULL) |
---|
| 247 | ? this->getVariable(*var, path) : NC_GLOBAL; |
---|
| 248 | std::map<StdString, StdSize> retvalue; |
---|
| 249 | |
---|
| 250 | if (var != NULL) |
---|
| 251 | { |
---|
| 252 | CheckError(nc_inq_varndims(grpid, varid, &nbdim)); |
---|
| 253 | dimid = new int[nbdim](); |
---|
| 254 | CheckError(nc_inq_vardimid(grpid, varid, dimid)); |
---|
| 255 | } |
---|
| 256 | else |
---|
| 257 | { |
---|
| 258 | CheckError(nc_inq_dimids(grpid, &nbdim, NULL, 1)); |
---|
| 259 | dimid = new int[nbdim](); |
---|
| 260 | CheckError(nc_inq_dimids(grpid, NULL, dimid, 1)); |
---|
| 261 | } |
---|
| 262 | |
---|
| 263 | for (int i = 0; i < nbdim; i++) |
---|
| 264 | { |
---|
| 265 | CheckError(nc_inq_dimname(grpid, dimid[i], full_name_in)); |
---|
| 266 | CheckError(nc_inq_dimlen (grpid, dimid[i], &size)); |
---|
| 267 | |
---|
| 268 | StdString dimname(full_name_in); |
---|
| 269 | retvalue.insert(retvalue.end(), std::make_pair(dimname, size)); |
---|
| 270 | } |
---|
| 271 | delete [] dimid; |
---|
| 272 | |
---|
| 273 | return (retvalue); |
---|
| 274 | } |
---|
| 275 | |
---|
| 276 | std::list<StdString> CINetCDF4::getAttributes |
---|
| 277 | (const StdString * const var, const CVarPath * const path ) |
---|
| 278 | { |
---|
| 279 | int nbatt = 0; |
---|
| 280 | char full_name_in[NC_MAX_NAME +1]; |
---|
| 281 | std::list<StdString> retvalue; |
---|
| 282 | int grpid = this->getGroup(path); |
---|
| 283 | int varid = (var != NULL) |
---|
| 284 | ? this->getVariable(*var, path) : NC_GLOBAL; |
---|
| 285 | |
---|
| 286 | if (var != NULL) |
---|
| 287 | CheckError(nc_inq_varnatts (grpid, varid, &nbatt)); |
---|
| 288 | else |
---|
| 289 | CheckError(nc_inq_natts(grpid, &nbatt)); |
---|
| 290 | |
---|
| 291 | for (int i = 0; i < nbatt; i++) |
---|
| 292 | { |
---|
| 293 | CheckError(nc_inq_attname(grpid, varid, i, full_name_in)); |
---|
| 294 | StdString attname(full_name_in); |
---|
| 295 | retvalue.push_back(attname); |
---|
| 296 | } |
---|
| 297 | return (retvalue); |
---|
| 298 | } |
---|
| 299 | |
---|
| 300 | int CINetCDF4::getAttributeId(const StdString & name, |
---|
| 301 | const StdString * const var, |
---|
| 302 | const CVarPath * const path) |
---|
| 303 | { |
---|
| 304 | int retvalue = 0; |
---|
| 305 | std::list<StdString> atts = this->getAttributes(var, path); |
---|
| 306 | std::list<StdString>::const_iterator it = atts.begin(), end = atts.end(); |
---|
| 307 | for (; it != end; it++) |
---|
| 308 | { |
---|
| 309 | const StdString & attname = *it; |
---|
| 310 | if (attname.compare(name) == 0) |
---|
| 311 | return (retvalue); |
---|
| 312 | retvalue++; |
---|
| 313 | } |
---|
| 314 | return (-1); |
---|
| 315 | } |
---|
| 316 | |
---|
| 317 | //--------------------------------------------------------------- |
---|
| 318 | |
---|
| 319 | bool CINetCDF4::hasMissingValue(const StdString & name, |
---|
| 320 | const CVarPath * const path) |
---|
| 321 | { |
---|
| 322 | return (this->hasAttribute("missing_value", &name, path) || |
---|
| 323 | this->hasAttribute("_FillValue", &name, path)); |
---|
| 324 | } |
---|
| 325 | |
---|
| 326 | bool CINetCDF4::hasAttribute(const StdString & name, |
---|
| 327 | const StdString * const var , |
---|
| 328 | const CVarPath * const path) |
---|
| 329 | { |
---|
| 330 | std::list<StdString> atts = this->getAttributes(var, path); |
---|
| 331 | std::list<StdString>::const_iterator it = atts.begin(), end = atts.end(); |
---|
| 332 | for (; it != end; it++) |
---|
| 333 | { |
---|
| 334 | const StdString & attname = *it; |
---|
| 335 | if (attname.compare(name) == 0) return (true); |
---|
| 336 | } |
---|
| 337 | return (false); |
---|
| 338 | } |
---|
| 339 | |
---|
| 340 | bool CINetCDF4::hasVariable(const StdString & name, |
---|
| 341 | const CVarPath * const path) |
---|
| 342 | { |
---|
| 343 | std::list<StdString> variables = this->getVariables(path); |
---|
| 344 | std::list<StdString>::const_iterator it = variables.begin(), end = variables.end(); |
---|
| 345 | for (; it != end; it++) |
---|
| 346 | { |
---|
| 347 | const StdString & varname = *it; |
---|
| 348 | if (varname.compare(name) == 0) return (true); |
---|
| 349 | } |
---|
| 350 | return (false); |
---|
| 351 | } |
---|
| 352 | |
---|
| 353 | bool CINetCDF4::hasCoordinates(const StdString & name, |
---|
| 354 | const CVarPath * const path) |
---|
| 355 | { |
---|
| 356 | return (this->hasAttribute("coordinates", &name, path)); |
---|
| 357 | } |
---|
| 358 | |
---|
| 359 | bool CINetCDF4::hasBounds(const StdString & name, |
---|
| 360 | const CVarPath * const path) |
---|
| 361 | { |
---|
| 362 | return (this->hasAttribute("bounds", &name, path)); |
---|
| 363 | } |
---|
| 364 | |
---|
| 365 | bool CINetCDF4::hasTemporalDim(const CVarPath * const path) |
---|
| 366 | { |
---|
| 367 | return (this->getUnlimitedDimension(path) != -1); |
---|
| 368 | } |
---|
| 369 | |
---|
| 370 | //--------------------------------------------------------------- |
---|
| 371 | |
---|
| 372 | #define GET_ATTRIBUTE_VALUE(type, func_ext, type_enum) \ |
---|
| 373 | template <> \ |
---|
| 374 | std::vector<type> CINetCDF4::getAttributeValue \ |
---|
| 375 | (const StdString & name, const StdString * const var, \ |
---|
| 376 | const CVarPath * const path) \ |
---|
| 377 | { \ |
---|
| 378 | int grpid = this->getGroup(path); \ |
---|
| 379 | int varid = (var != NULL) \ |
---|
| 380 | ? this->getVariable(*var, path) : NC_GLOBAL; \ |
---|
| 381 | std::pair<nc_type , StdSize> attinfos = \ |
---|
| 382 | this->getAttribute(name, var, path); \ |
---|
| 383 | std::vector<type> retvalue(attinfos.second); \ |
---|
| 384 | if (attinfos.first != type_enum) \ |
---|
| 385 | ERROR("CINetCDF4::getAttributeValue<double>(name, var, path", \ |
---|
| 386 | << "[ name : " << name \ |
---|
| 387 | << ", type requested :" << attinfos.first \ |
---|
| 388 | << ", type stored : " << type_enum << "]" \ |
---|
| 389 | << " Invalid type !"); \ |
---|
| 390 | CheckError(nc_get_att_##func_ext \ |
---|
| 391 | (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 <> |
---|
| 401 | StdString CINetCDF4::getAttributeValue |
---|
| 402 | (const StdString & name, const StdString * const var, |
---|
| 403 | const CVarPath * const path) |
---|
| 404 | { |
---|
| 405 | std::vector<char> chart = this->getAttributeValue |
---|
| 406 | <std::vector<char> >(name, var, path); |
---|
| 407 | StdString retvalue(&(chart[0]), chart.size()); |
---|
| 408 | return (retvalue); |
---|
| 409 | } |
---|
| 410 | |
---|
| 411 | template <> |
---|
| 412 | int CINetCDF4::getMissingValue(const StdString & name, |
---|
| 413 | const CVarPath * const path) |
---|
| 414 | { |
---|
| 415 | if (this->hasAttribute("missing_value", &name, path)) |
---|
| 416 | return (this->getAttributeValue<std::vector<int> >("missing_value", &name, path)[0]); |
---|
| 417 | if (this->hasAttribute("_FillValue", &name, path)) |
---|
| 418 | return (this->getAttributeValue<std::vector<int> >("_FillValue", &name, path)[0]); |
---|
| 419 | return (0); |
---|
| 420 | } |
---|
| 421 | |
---|
| 422 | template <> |
---|
| 423 | double CINetCDF4::getMissingValue(const StdString & name, |
---|
| 424 | const CVarPath * const path) |
---|
| 425 | { |
---|
| 426 | if (this->hasAttribute("missing_value", &name, path)) |
---|
| 427 | return (this->getAttributeValue<std::vector<double> >("missing_value", &name, path)[0]); |
---|
| 428 | if (this->hasAttribute("_FillValue", &name, path)) |
---|
| 429 | return (this->getAttributeValue<std::vector<double> >("_FillValue", &name, path)[0]); |
---|
| 430 | return (0); |
---|
| 431 | } |
---|
| 432 | |
---|
| 433 | template <> |
---|
| 434 | float CINetCDF4::getMissingValue(const StdString & name, |
---|
| 435 | const CVarPath * const path) |
---|
| 436 | { |
---|
| 437 | if (this->hasAttribute("missing_value", &name, path)) |
---|
| 438 | return (this->getAttributeValue<std::vector<float> >("missing_value", &name, path)[0]); |
---|
| 439 | if (this->hasAttribute("_FillValue", &name, path)) |
---|
| 440 | return (this->getAttributeValue<std::vector<float> >("_FillValue", &name, path)[0]); |
---|
| 441 | return (0); |
---|
| 442 | } |
---|
| 443 | |
---|
| 444 | //--------------------------------------------------------------- |
---|
| 445 | |
---|
| 446 | std::list<StdString> CINetCDF4::getCoordinatesIdList |
---|
| 447 | (const StdString & name, const CVarPath * const path) |
---|
| 448 | { |
---|
| 449 | std::list<StdString> retvalue; |
---|
| 450 | StdString value = this->getCoordinatesId(name, path); |
---|
| 451 | |
---|
| 452 | boost::split(retvalue, value, boost::is_any_of(" ")); |
---|
| 453 | |
---|
| 454 | std::list<StdString>::iterator it = retvalue.begin(), end = retvalue.end(); |
---|
| 455 | for (; it != end; it++) |
---|
| 456 | { |
---|
| 457 | StdString & coord = *it; |
---|
| 458 | coord.assign(coord.data()); |
---|
| 459 | } |
---|
| 460 | return (retvalue); |
---|
| 461 | } |
---|
| 462 | |
---|
| 463 | StdString CINetCDF4::getCoordinatesId |
---|
| 464 | (const StdString & name, const CVarPath * const path) |
---|
| 465 | { |
---|
| 466 | StdString retvalue; |
---|
| 467 | if (this->hasAttribute("coordinates", &name, path)) |
---|
| 468 | { |
---|
| 469 | return (this->getAttributeValue<StdString>("coordinates", &name, path)); |
---|
| 470 | } |
---|
| 471 | else |
---|
| 472 | { |
---|
| 473 | std::list<StdString> dims = this->getDimensionsList(&name, path); |
---|
| 474 | std::list<StdString>::const_iterator it = dims.begin(), end = dims.end(); |
---|
| 475 | for (; it != end; it++) |
---|
| 476 | { |
---|
| 477 | const StdString & value = *it; |
---|
| 478 | retvalue.append(value).push_back(' '); |
---|
| 479 | } |
---|
| 480 | retvalue.erase (retvalue.end()-1) ; |
---|
| 481 | } |
---|
| 482 | |
---|
| 483 | return (retvalue); |
---|
| 484 | } |
---|
| 485 | |
---|
| 486 | StdString CINetCDF4::getBoundsId(const StdString & name, |
---|
| 487 | const CVarPath * const path) |
---|
| 488 | { |
---|
| 489 | StdString retvalue; |
---|
| 490 | if (this->hasAttribute("bounds", &name, path)) |
---|
| 491 | return (this->getAttributeValue<StdString>("bounds", &name, path)); |
---|
| 492 | return (retvalue); |
---|
| 493 | } |
---|
| 494 | |
---|
| 495 | //--------------------------------------------------------------- |
---|
| 496 | |
---|
| 497 | bool CINetCDF4::isBound(const StdString & name, |
---|
| 498 | const CVarPath * const path) |
---|
| 499 | { |
---|
| 500 | std::set<StdString> bounds = this->getBoundVariables(path); |
---|
| 501 | return (bounds.find(name) != bounds.end()); |
---|
| 502 | } |
---|
| 503 | |
---|
| 504 | bool CINetCDF4::isCoordinate(const StdString & name, |
---|
| 505 | const CVarPath * const path) |
---|
| 506 | { |
---|
| 507 | std::set<StdString> coords = this->getCoordVariables(path); |
---|
| 508 | return (coords.find(name) != coords.end()); |
---|
| 509 | } |
---|
| 510 | |
---|
| 511 | bool CINetCDF4::isRectilinear(const StdString & name, const CVarPath * const path) |
---|
| 512 | { |
---|
| 513 | std::list<StdString> coords = this->getCoordinatesIdList(name, path); |
---|
| 514 | std::list<StdString>::const_iterator it = coords.begin(), end = coords.end(); |
---|
| 515 | for (; it != end; it++) |
---|
| 516 | { |
---|
| 517 | const StdString & coord = *it; |
---|
| 518 | if (this->hasVariable(coord, path) && !this->isTemporal(coord, path)) |
---|
| 519 | { |
---|
| 520 | std::map<StdString, StdSize> dimvar = this->getDimensions(&coord, path); |
---|
| 521 | if ((dimvar.size() == 1) && (dimvar.find(coord) != dimvar.end())) |
---|
| 522 | continue; |
---|
| 523 | else |
---|
| 524 | return (false); |
---|
| 525 | } |
---|
| 526 | } |
---|
| 527 | return (true); |
---|
| 528 | } |
---|
| 529 | |
---|
| 530 | bool CINetCDF4::isCurvilinear(const StdString & name, const CVarPath * const path) |
---|
| 531 | { |
---|
| 532 | if (this->isRectilinear(name, path) || |
---|
| 533 | !this->hasCoordinates(name, path)) |
---|
| 534 | return (false); |
---|
| 535 | |
---|
| 536 | std::list<StdString> coords = this->getCoordinatesIdList(name, path); |
---|
| 537 | std::list<StdString>::const_iterator it = coords.begin(), end = coords.end(); |
---|
| 538 | for (; it != end; it++) |
---|
| 539 | { |
---|
| 540 | const StdString & coord = *it; |
---|
| 541 | if (this->hasVariable(coord, path)) |
---|
| 542 | { |
---|
| 543 | std::map<StdString, StdSize> dimvar = this->getDimensions(&coord, path); |
---|
| 544 | if (dimvar.size() != 2) return (false); |
---|
| 545 | } |
---|
| 546 | else return (false); |
---|
| 547 | } |
---|
| 548 | return (true); |
---|
| 549 | } |
---|
| 550 | |
---|
| 551 | bool CINetCDF4::isUnstructured(const StdString & name, const CVarPath * const path) |
---|
| 552 | { |
---|
| 553 | if (this->isRectilinear(name, path) || |
---|
| 554 | this->isCurvilinear(name, path) || |
---|
| 555 | !this->hasCoordinates(name, path)) |
---|
| 556 | return (false); |
---|
| 557 | |
---|
| 558 | StdString dimname = this->getDimensionsList(&name, path).back(); |
---|
| 559 | |
---|
| 560 | std::list<StdString> coords = this->getCoordinatesIdList(name, path); |
---|
| 561 | std::list<StdString>::const_iterator it = coords.begin(), end = coords.end(); |
---|
| 562 | for (; it != end; it++) |
---|
| 563 | { |
---|
| 564 | const StdString & coord = *it; |
---|
| 565 | if (this->hasVariable(coord, path)) |
---|
| 566 | { |
---|
| 567 | std::map<StdString, StdSize> dimvar = this->getDimensions(&coord, path); |
---|
| 568 | if ((dimvar.size() == 1) && |
---|
| 569 | (dimvar.find(dimname) != dimvar.end())) |
---|
| 570 | continue; |
---|
| 571 | else |
---|
| 572 | return (false); |
---|
| 573 | } |
---|
| 574 | else return (false); |
---|
| 575 | } |
---|
| 576 | |
---|
| 577 | return (true); |
---|
| 578 | } |
---|
| 579 | |
---|
| 580 | bool CINetCDF4::isUnknown(const StdString & name, const CVarPath * const path) |
---|
| 581 | { |
---|
| 582 | return !(this->isRectilinear(name, path) || |
---|
| 583 | this->isCurvilinear(name, path) || |
---|
| 584 | this->isUnstructured(name, path)); |
---|
| 585 | } |
---|
| 586 | |
---|
| 587 | bool CINetCDF4::isTemporal(const StdString & name, const CVarPath * const path) |
---|
| 588 | { |
---|
| 589 | if (!this->hasTemporalDim(path)) return (false); |
---|
| 590 | std::map<StdString, StdSize> dims = this->getDimensions(&name, path); |
---|
| 591 | if (dims.find(this->getUnlimitedDimensionName(path)) != dims.end()) |
---|
| 592 | return (true); |
---|
| 593 | return (false); |
---|
| 594 | } |
---|
| 595 | |
---|
| 596 | bool CINetCDF4::is3Dim(const StdString & name, const CVarPath * const path) |
---|
| 597 | { |
---|
| 598 | int i = 0; |
---|
| 599 | std::list<StdString> coords = this->getCoordinatesIdList(name, path); |
---|
| 600 | std::list<StdString>::const_iterator it = coords.begin(), end = coords.end(); |
---|
| 601 | for (; it != end; it++) |
---|
| 602 | { |
---|
| 603 | const StdString & coord = *it; |
---|
| 604 | if (this->hasVariable(coord, path)) |
---|
| 605 | { |
---|
| 606 | if (this->isTemporal(coord, path)) |
---|
| 607 | continue; |
---|
| 608 | i++; |
---|
| 609 | } |
---|
| 610 | else |
---|
| 611 | { |
---|
| 612 | if (coord.compare(this->getUnlimitedDimensionName()) == 0) |
---|
| 613 | continue; |
---|
| 614 | i++; |
---|
| 615 | } |
---|
| 616 | } |
---|
| 617 | return (i == 3); |
---|
| 618 | } |
---|
| 619 | |
---|
| 620 | bool CINetCDF4::isCellGrid(const StdString & name, const CVarPath * const path) |
---|
| 621 | { |
---|
| 622 | if (this->isCoordinate(name, path)) |
---|
| 623 | { |
---|
| 624 | return (this->hasBounds(name, path)); |
---|
| 625 | } |
---|
| 626 | else |
---|
| 627 | { |
---|
| 628 | std::list<StdString> coords = this->getCoordinatesIdList(name, path); |
---|
| 629 | std::list<StdString>::const_iterator it = coords.begin(), end = coords.end(); |
---|
| 630 | for (; it != end; it++) |
---|
| 631 | { |
---|
| 632 | const StdString & coord = *it; |
---|
| 633 | if (this->hasVariable(coord, path)) |
---|
| 634 | { |
---|
| 635 | if (this->isTemporal(coord, path)) |
---|
| 636 | continue; |
---|
| 637 | if (this->isCellGrid(coord, path)) |
---|
| 638 | continue; |
---|
| 639 | return (false); |
---|
| 640 | } |
---|
| 641 | else |
---|
| 642 | { |
---|
| 643 | if (coord.compare(this->getUnlimitedDimensionName()) == 0) |
---|
| 644 | continue; |
---|
| 645 | return (false); |
---|
| 646 | } |
---|
| 647 | } |
---|
| 648 | } |
---|
| 649 | |
---|
| 650 | return (true); |
---|
| 651 | } |
---|
| 652 | |
---|
| 653 | //--------------------------------------------------------------- |
---|
| 654 | |
---|
| 655 | std::list<StdString> CINetCDF4::getDataVariables(bool _is3D, bool _isRecti, |
---|
| 656 | bool _isCurvi, bool _isUnstr, |
---|
| 657 | bool _isCellData, bool _isTemporal, |
---|
| 658 | const CVarPath * const path) |
---|
| 659 | { |
---|
| 660 | std::list<StdString> retvalue; |
---|
| 661 | std::list<StdString> allvars = this->getVariables(path); |
---|
| 662 | std::set<StdString> allcoords = this->getCoordVariables(path); |
---|
| 663 | |
---|
| 664 | std::list<StdString>::const_iterator it = allvars.begin(), end = allvars.end(); |
---|
| 665 | for (; it != end; it++) |
---|
| 666 | { |
---|
| 667 | const StdString & var = *it; |
---|
| 668 | if (this->isCoordinate(var, path)) continue; |
---|
| 669 | |
---|
| 670 | if (!_isRecti && this->isRectilinear(var, path) ) continue; |
---|
| 671 | if (!_isCurvi && this->isCurvilinear(var, path) ) continue; |
---|
| 672 | if (!_isUnstr && this->isUnstructured(var, path)) continue; |
---|
| 673 | |
---|
| 674 | if (!_isTemporal && this->isTemporal(var, path)) continue; |
---|
| 675 | if (!_is3D && this->is3Dim(var, path) ) continue; |
---|
| 676 | if (!_isCellData && this->isCellGrid(var, path)) continue; |
---|
| 677 | |
---|
| 678 | if (this->isUnknown(var, path)) continue; |
---|
| 679 | |
---|
| 680 | retvalue.push_back(var); |
---|
| 681 | } |
---|
| 682 | return (retvalue); |
---|
| 683 | } |
---|
| 684 | |
---|
| 685 | //--------------------------------------------------------------- |
---|
| 686 | |
---|
| 687 | void CINetCDF4::getDataInfo(const StdString & var, const CVarPath * const path, StdSize record, |
---|
| 688 | std::vector<StdSize> & start, std::vector<StdSize> & count, |
---|
| 689 | StdSize & array_size) |
---|
| 690 | { |
---|
| 691 | std::list<StdString> dimlist = this->getDimensionsList(&var, path); |
---|
| 692 | std::map<StdString, StdSize> dimmap = this->getDimensions(&var, path); |
---|
| 693 | std::list<StdString>::iterator it = dimlist.begin(); |
---|
| 694 | if (this->isTemporal(var, path)) |
---|
| 695 | { |
---|
| 696 | if (record != UNLIMITED_DIM) |
---|
| 697 | start.push_back(record); |
---|
| 698 | else |
---|
| 699 | start.push_back(0); |
---|
| 700 | count.push_back(1); |
---|
| 701 | it++; |
---|
| 702 | } |
---|
| 703 | for (; it != dimlist.end(); it++) |
---|
| 704 | { |
---|
| 705 | start.push_back(0); |
---|
| 706 | count.push_back(dimmap[*it]); |
---|
| 707 | array_size *= dimmap[*it]; |
---|
| 708 | } |
---|
| 709 | } |
---|
| 710 | |
---|
| 711 | template <> |
---|
[369] | 712 | void CINetCDF4::getData(CArray<int, 1>& data, const StdString & var, |
---|
[219] | 713 | const CVarPath * const path, StdSize record) |
---|
| 714 | { |
---|
| 715 | |
---|
| 716 | std::vector<StdSize> start, count; |
---|
| 717 | int grpid = this->getGroup(path); |
---|
| 718 | int varid = this->getVariable(var, path); |
---|
| 719 | StdSize array_size = 1; |
---|
| 720 | this->getDataInfo(var, path, record, start, count, array_size); |
---|
[369] | 721 | data.resize(array_size); |
---|
| 722 | CheckError(nc_get_vara_int (grpid, varid, &(start[0]), &(count[0]), data.dataFirst())); |
---|
[219] | 723 | } |
---|
| 724 | |
---|
| 725 | template <> |
---|
[369] | 726 | void CINetCDF4::getData(CArray<double, 1>& data, const StdString & var, |
---|
[219] | 727 | const CVarPath * const path, StdSize record) |
---|
| 728 | { |
---|
| 729 | std::vector<StdSize> start, count; |
---|
| 730 | int grpid = this->getGroup(path); |
---|
| 731 | int varid = this->getVariable(var, path); |
---|
| 732 | StdSize array_size = 1; |
---|
| 733 | this->getDataInfo(var, path, record, start, count, array_size); |
---|
[369] | 734 | data.resize(array_size); |
---|
| 735 | CheckError(nc_get_vara_double (grpid, varid, &(start[0]), &(count[0]), data.dataFirst())); |
---|
[219] | 736 | } |
---|
| 737 | |
---|
| 738 | template <> |
---|
[369] | 739 | void CINetCDF4::getData(CArray<float, 1>& data, const StdString & var, |
---|
[219] | 740 | const CVarPath * const path, StdSize record) |
---|
| 741 | { |
---|
| 742 | std::vector<StdSize> start, count; |
---|
| 743 | int grpid = this->getGroup(path); |
---|
| 744 | int varid = this->getVariable(var, path); |
---|
| 745 | StdSize array_size = 1; |
---|
| 746 | this->getDataInfo(var, path, record, start, count, array_size); |
---|
[369] | 747 | data.resize(array_size); |
---|
| 748 | CheckError(nc_get_vara_float (grpid, varid, &(start[0]), &(count[0]), data.dataFirst())); |
---|
[219] | 749 | } |
---|
| 750 | |
---|
| 751 | //--------------------------------------------------------------- |
---|
| 752 | StdString CINetCDF4::getLonCoordName(const StdString & varname, |
---|
| 753 | const CVarPath * const path) |
---|
| 754 | { |
---|
| 755 | std::list<StdString> clist = |
---|
| 756 | this->getCoordinatesIdList(varname, path); |
---|
| 757 | if (this->hasCoordinates(varname, path)) |
---|
| 758 | return (*clist.begin()); |
---|
| 759 | else |
---|
| 760 | return (*clist.rbegin()); |
---|
| 761 | } |
---|
| 762 | |
---|
| 763 | StdString CINetCDF4::getLatCoordName(const StdString & varname, |
---|
| 764 | const CVarPath * const path) |
---|
| 765 | { |
---|
| 766 | std::list<StdString> clist = |
---|
| 767 | this->getCoordinatesIdList(varname, path); |
---|
| 768 | if (this->hasCoordinates(varname, path)) |
---|
| 769 | return (*(++clist.begin())); |
---|
| 770 | else |
---|
| 771 | return (*(++clist.rbegin())); |
---|
| 772 | } |
---|
| 773 | |
---|
| 774 | StdString CINetCDF4::getVertCoordName(const StdString & varname, |
---|
| 775 | const CVarPath * const path) |
---|
| 776 | { |
---|
| 777 | if (!this->is3Dim(varname, path)) return (""); |
---|
| 778 | std::list<StdString> clist = |
---|
| 779 | this->getCoordinatesIdList(varname, path); |
---|
| 780 | if (this->hasCoordinates(varname, path)) |
---|
| 781 | return (*(++(++clist.begin()))); |
---|
| 782 | else |
---|
| 783 | return (*(++(++clist.rbegin()))); |
---|
| 784 | } |
---|
| 785 | |
---|
| 786 | ///-------------------------------------------------------------- |
---|
| 787 | |
---|
[335] | 788 | } // namespace xios |
---|