[528] | 1 | #include <fstream> |
---|
| 2 | |
---|
[219] | 3 | #include "onetcdf4.hpp" |
---|
[352] | 4 | #include "group_template.hpp" |
---|
[382] | 5 | #include "mpi.hpp" |
---|
[379] | 6 | #include "netcdf.hpp" |
---|
[498] | 7 | #include "netCdfInterface.hpp" |
---|
| 8 | #include "netCdfException.hpp" |
---|
[1158] | 9 | #include "timer.hpp" |
---|
[219] | 10 | |
---|
[335] | 11 | namespace xios |
---|
[219] | 12 | { |
---|
| 13 | /// ////////////////////// Définitions ////////////////////// /// |
---|
| 14 | |
---|
[686] | 15 | CONetCDF4::CONetCDF4(const StdString& filename, bool append, bool useClassicFormat, |
---|
[878] | 16 | bool useCFConvention, |
---|
[802] | 17 | const MPI_Comm* comm, bool multifile, const StdString& timeCounterName) |
---|
[686] | 18 | : path() |
---|
| 19 | , wmpi(false) |
---|
| 20 | , useClassicFormat(useClassicFormat) |
---|
[878] | 21 | , useCFConvention(useCFConvention) |
---|
[219] | 22 | { |
---|
[878] | 23 | this->initialize(filename, append, useClassicFormat, useCFConvention, comm, multifile, timeCounterName); |
---|
[219] | 24 | } |
---|
[498] | 25 | |
---|
[219] | 26 | //--------------------------------------------------------------- |
---|
[286] | 27 | |
---|
[219] | 28 | CONetCDF4::~CONetCDF4(void) |
---|
| 29 | { |
---|
| 30 | } |
---|
| 31 | |
---|
| 32 | ///-------------------------------------------------------------- |
---|
| 33 | |
---|
[878] | 34 | void CONetCDF4::initialize(const StdString& filename, bool append, bool useClassicFormat, bool useCFConvention, |
---|
[802] | 35 | const MPI_Comm* comm, bool multifile, const StdString& timeCounterName) |
---|
[219] | 36 | { |
---|
[517] | 37 | this->useClassicFormat = useClassicFormat; |
---|
[878] | 38 | this->useCFConvention = useCFConvention; |
---|
[517] | 39 | |
---|
| 40 | int mode = useClassicFormat ? 0 : NC_NETCDF4; |
---|
[605] | 41 | |
---|
| 42 | // Don't use parallel mode if there is only one process |
---|
| 43 | if (comm) |
---|
| 44 | { |
---|
| 45 | int commSize = 0; |
---|
| 46 | MPI_Comm_size(*comm, &commSize); |
---|
| 47 | if (commSize <= 1) |
---|
| 48 | comm = NULL; |
---|
| 49 | } |
---|
| 50 | wmpi = comm && !multifile; |
---|
| 51 | |
---|
| 52 | if (wmpi) |
---|
[517] | 53 | mode |= useClassicFormat ? NC_PNETCDF : NC_MPIIO; |
---|
| 54 | |
---|
[528] | 55 | // If the file does not exist, we always create it |
---|
| 56 | if (!append || !std::ifstream(filename.c_str())) |
---|
[219] | 57 | { |
---|
[1158] | 58 | CTimer::get("Files : create").resume(); |
---|
[605] | 59 | if (wmpi) |
---|
| 60 | CNetCdfInterface::createPar(filename, mode, *comm, MPI_INFO_NULL, this->ncidp); |
---|
| 61 | else |
---|
| 62 | CNetCdfInterface::create(filename, mode, this->ncidp); |
---|
[1158] | 63 | CTimer::get("Files : create").suspend(); |
---|
| 64 | |
---|
[528] | 65 | this->appendMode = false; |
---|
[219] | 66 | } |
---|
| 67 | else |
---|
| 68 | { |
---|
[528] | 69 | mode |= NC_WRITE; |
---|
[1158] | 70 | CTimer::get("Files : open").resume(); |
---|
[605] | 71 | if (wmpi) |
---|
| 72 | CNetCdfInterface::openPar(filename, mode, *comm, MPI_INFO_NULL, this->ncidp); |
---|
| 73 | else |
---|
| 74 | CNetCdfInterface::open(filename, mode, this->ncidp); |
---|
[1158] | 75 | CTimer::get("Files : open").suspend(); |
---|
[528] | 76 | this->appendMode = true; |
---|
[219] | 77 | } |
---|
[517] | 78 | |
---|
| 79 | // If the classic NetCDF format is used, we enable the "no-fill mode" globally. |
---|
| 80 | // This is done per variable for the NetCDF4 format. |
---|
| 81 | if (useClassicFormat) |
---|
[606] | 82 | CNetCdfInterface::setFill(this->ncidp, false); |
---|
[802] | 83 | |
---|
| 84 | this->timeCounterName = timeCounterName; |
---|
[219] | 85 | } |
---|
[498] | 86 | |
---|
[286] | 87 | void CONetCDF4::close() |
---|
| 88 | { |
---|
[1158] | 89 | CTimer::get("Files : close").resume(); |
---|
[686] | 90 | CNetCdfInterface::close(this->ncidp); |
---|
[1158] | 91 | CTimer::get("Files : close").suspend(); |
---|
[286] | 92 | } |
---|
[498] | 93 | |
---|
[219] | 94 | //--------------------------------------------------------------- |
---|
[498] | 95 | |
---|
[219] | 96 | void CONetCDF4::definition_start(void) |
---|
[498] | 97 | { |
---|
[686] | 98 | CNetCdfInterface::reDef(this->ncidp); |
---|
[219] | 99 | } |
---|
[498] | 100 | |
---|
[219] | 101 | //--------------------------------------------------------------- |
---|
[498] | 102 | |
---|
[219] | 103 | void CONetCDF4::definition_end(void) |
---|
| 104 | { |
---|
[686] | 105 | CNetCdfInterface::endDef(this->ncidp); |
---|
[219] | 106 | } |
---|
| 107 | |
---|
| 108 | //--------------------------------------------------------------- |
---|
[498] | 109 | |
---|
[219] | 110 | int CONetCDF4::getCurrentGroup(void) |
---|
| 111 | { |
---|
[686] | 112 | return this->getGroup(this->getCurrentPath()); |
---|
[219] | 113 | } |
---|
[498] | 114 | |
---|
[219] | 115 | //--------------------------------------------------------------- |
---|
[498] | 116 | |
---|
[686] | 117 | int CONetCDF4::getGroup(const CONetCDF4Path& path) |
---|
[219] | 118 | { |
---|
| 119 | int retvalue = this->ncidp; |
---|
[498] | 120 | |
---|
[686] | 121 | CONetCDF4Path::const_iterator it = path.begin(), end = path.end(); |
---|
[219] | 122 | |
---|
[686] | 123 | for (; it != end; it++) |
---|
[219] | 124 | { |
---|
[686] | 125 | const StdString& groupid = *it; |
---|
| 126 | CNetCdfInterface::inqNcId(retvalue, groupid, retvalue); |
---|
[219] | 127 | } |
---|
[686] | 128 | return retvalue; |
---|
[219] | 129 | } |
---|
[498] | 130 | |
---|
[219] | 131 | //--------------------------------------------------------------- |
---|
[498] | 132 | |
---|
[686] | 133 | int CONetCDF4::getVariable(const StdString& varname) |
---|
[219] | 134 | { |
---|
| 135 | int varid = 0; |
---|
| 136 | int grpid = this->getCurrentGroup(); |
---|
[686] | 137 | CNetCdfInterface::inqVarId(grpid, varname, varid); |
---|
| 138 | return varid; |
---|
[219] | 139 | } |
---|
[498] | 140 | |
---|
[219] | 141 | //--------------------------------------------------------------- |
---|
[498] | 142 | |
---|
[686] | 143 | int CONetCDF4::getDimension(const StdString& dimname) |
---|
[219] | 144 | { |
---|
| 145 | int dimid = 0; |
---|
| 146 | int grpid = this->getCurrentGroup(); |
---|
[686] | 147 | CNetCdfInterface::inqDimId(grpid, dimname, dimid); |
---|
| 148 | return dimid; |
---|
[219] | 149 | } |
---|
[498] | 150 | |
---|
[219] | 151 | //--------------------------------------------------------------- |
---|
[498] | 152 | |
---|
[219] | 153 | int CONetCDF4::getUnlimitedDimension(void) |
---|
| 154 | { |
---|
| 155 | int dimid = 0; |
---|
| 156 | int grpid = this->getCurrentGroup(); |
---|
[686] | 157 | CNetCdfInterface::inqUnLimDim(grpid, dimid); |
---|
| 158 | return dimid; |
---|
[219] | 159 | } |
---|
[498] | 160 | |
---|
[266] | 161 | StdString CONetCDF4::getUnlimitedDimensionName(void) |
---|
| 162 | { |
---|
| 163 | int grpid = this->getGroup(path); |
---|
| 164 | int dimid = this->getUnlimitedDimension(); |
---|
[498] | 165 | |
---|
| 166 | StdString dimname; |
---|
[686] | 167 | if (dimid != -1) |
---|
| 168 | CNetCdfInterface::inqDimName(grpid, dimid, dimname); |
---|
| 169 | return dimname; |
---|
[266] | 170 | } |
---|
[498] | 171 | |
---|
[219] | 172 | //--------------------------------------------------------------- |
---|
[498] | 173 | |
---|
[686] | 174 | std::vector<StdSize> CONetCDF4::getDimensions(const StdString& varname) |
---|
[219] | 175 | { |
---|
| 176 | StdSize size = 0; |
---|
| 177 | std::vector<StdSize> retvalue; |
---|
| 178 | int grpid = this->getCurrentGroup(); |
---|
| 179 | int varid = this->getVariable(varname); |
---|
| 180 | int nbdim = 0, *dimid = NULL; |
---|
| 181 | |
---|
[686] | 182 | CNetCdfInterface::inqVarNDims(grpid, varid, nbdim); |
---|
[219] | 183 | dimid = new int[nbdim](); |
---|
[686] | 184 | CNetCdfInterface::inqVarDimId(grpid, varid, dimid); |
---|
[219] | 185 | |
---|
| 186 | for (int i = 0; i < nbdim; i++) |
---|
| 187 | { |
---|
[686] | 188 | CNetCdfInterface::inqDimLen(grpid, dimid[i], size); |
---|
[219] | 189 | if (size == NC_UNLIMITED) |
---|
| 190 | size = UNLIMITED_DIM; |
---|
| 191 | retvalue.push_back(size); |
---|
| 192 | } |
---|
[401] | 193 | delete [] dimid; |
---|
[686] | 194 | return retvalue; |
---|
[219] | 195 | } |
---|
| 196 | |
---|
[686] | 197 | std::vector<std::string> CONetCDF4::getDimensionsIdList(const std::string* _varname) |
---|
[266] | 198 | { |
---|
[498] | 199 | int nDimNull = 0; |
---|
[266] | 200 | int nbdim = 0, *dimid = NULL; |
---|
| 201 | int grpid = this->getCurrentGroup(); |
---|
| 202 | int varid = (_varname != NULL) ? this->getVariable(*_varname) : NC_GLOBAL; |
---|
| 203 | std::vector<std::string> retvalue; |
---|
[498] | 204 | |
---|
[266] | 205 | if (_varname != NULL) |
---|
| 206 | { |
---|
[686] | 207 | CNetCdfInterface::inqVarNDims(grpid, varid, nbdim); |
---|
[266] | 208 | dimid = new int[nbdim](); |
---|
[686] | 209 | CNetCdfInterface::inqVarDimId(grpid, varid, dimid); |
---|
[266] | 210 | } |
---|
| 211 | else |
---|
| 212 | { |
---|
[686] | 213 | CNetCdfInterface::inqDimIds(grpid, nbdim, NULL, 1); |
---|
[266] | 214 | dimid = new int[nbdim](); |
---|
[686] | 215 | CNetCdfInterface::inqDimIds(grpid, nDimNull, dimid, 1); |
---|
[266] | 216 | } |
---|
[498] | 217 | |
---|
[266] | 218 | for (int i = 0; i < nbdim; i++) |
---|
| 219 | { |
---|
[498] | 220 | std::string dimname; |
---|
[686] | 221 | CNetCdfInterface::inqDimName(grpid, dimid[i], dimname); |
---|
[266] | 222 | retvalue.push_back(dimname); |
---|
| 223 | } |
---|
| 224 | delete [] dimid; |
---|
[498] | 225 | |
---|
[686] | 226 | return retvalue; |
---|
[266] | 227 | } |
---|
| 228 | |
---|
[219] | 229 | //--------------------------------------------------------------- |
---|
| 230 | |
---|
[707] | 231 | void CONetCDF4::getTimeAxisBounds(CArray<double,2>& timeAxisBounds, const StdString& name, bool collective) |
---|
| 232 | { |
---|
| 233 | int grpid = this->getCurrentGroup(); |
---|
| 234 | int varid = this->getVariable(name); |
---|
| 235 | |
---|
| 236 | std::vector<StdSize> start(2), count(2); |
---|
| 237 | start[0] = 0; |
---|
| 238 | // Find out how many temporal records have been written already to the file we are opening |
---|
| 239 | int ncUnlimitedDimId; |
---|
| 240 | CNetCdfInterface::inqUnLimDim(this->ncidp, ncUnlimitedDimId); |
---|
| 241 | CNetCdfInterface::inqDimLen(this->ncidp, ncUnlimitedDimId, count[0]); |
---|
| 242 | start[1] = 0; |
---|
| 243 | count[1] = 2; |
---|
| 244 | |
---|
| 245 | timeAxisBounds.resize(count[1], count[0]); |
---|
| 246 | |
---|
| 247 | if (this->wmpi && collective) |
---|
| 248 | CNetCdfInterface::varParAccess(grpid, varid, NC_COLLECTIVE); |
---|
| 249 | if (this->wmpi && !collective) |
---|
| 250 | CNetCdfInterface::varParAccess(grpid, varid, NC_INDEPENDENT); |
---|
| 251 | |
---|
| 252 | CNetCdfInterface::getVaraType(grpid, varid, &start[0], &count[0], timeAxisBounds.dataFirst()); |
---|
| 253 | } |
---|
| 254 | |
---|
[1158] | 255 | void CONetCDF4::getTimeAxisBounds(CArray<double,2>& timeAxisBounds, const StdString& name, bool collective, size_t record) |
---|
| 256 | { |
---|
| 257 | int grpid = this->getCurrentGroup(); |
---|
| 258 | int varid = this->getVariable(name); |
---|
[707] | 259 | |
---|
[1158] | 260 | std::vector<StdSize> start(2), count(2); |
---|
| 261 | start[0] = record; |
---|
| 262 | count[0] = 1 ; |
---|
| 263 | start[1] = 0; |
---|
| 264 | count[1] = 2; |
---|
| 265 | |
---|
| 266 | timeAxisBounds.resize(2, 1); |
---|
| 267 | |
---|
| 268 | if (this->wmpi && collective) |
---|
| 269 | CNetCdfInterface::varParAccess(grpid, varid, NC_COLLECTIVE); |
---|
| 270 | if (this->wmpi && !collective) |
---|
| 271 | CNetCdfInterface::varParAccess(grpid, varid, NC_INDEPENDENT); |
---|
| 272 | |
---|
| 273 | CNetCdfInterface::getVaraType(grpid, varid, &start[0], &count[0], timeAxisBounds.dataFirst()); |
---|
| 274 | } |
---|
| 275 | |
---|
| 276 | |
---|
| 277 | |
---|
[686] | 278 | const CONetCDF4::CONetCDF4Path& CONetCDF4::getCurrentPath(void) const |
---|
| 279 | { return this->path; } |
---|
[219] | 280 | |
---|
[686] | 281 | void CONetCDF4::setCurrentPath(const CONetCDF4Path& path) |
---|
[219] | 282 | { this->path = path; } |
---|
| 283 | |
---|
| 284 | //--------------------------------------------------------------- |
---|
| 285 | |
---|
[686] | 286 | int CONetCDF4::addGroup(const StdString& name) |
---|
[219] | 287 | { |
---|
| 288 | int retvalue = 0; |
---|
| 289 | int grpid = this->getCurrentGroup(); |
---|
[686] | 290 | CNetCdfInterface::defGrp(grpid, name, retvalue); |
---|
| 291 | return retvalue; |
---|
[219] | 292 | } |
---|
[498] | 293 | |
---|
[219] | 294 | //--------------------------------------------------------------- |
---|
[498] | 295 | |
---|
[219] | 296 | int CONetCDF4::addDimension(const StdString& name, const StdSize size) |
---|
| 297 | { |
---|
| 298 | int retvalue = 0; |
---|
| 299 | int grpid = this->getCurrentGroup(); |
---|
| 300 | if (size != UNLIMITED_DIM) |
---|
[686] | 301 | CNetCdfInterface::defDim(grpid, name, size, retvalue); |
---|
[219] | 302 | else |
---|
[686] | 303 | CNetCdfInterface::defDim(grpid, name, NC_UNLIMITED, retvalue); |
---|
| 304 | return retvalue; |
---|
[219] | 305 | } |
---|
[498] | 306 | |
---|
[219] | 307 | //--------------------------------------------------------------- |
---|
[498] | 308 | |
---|
[686] | 309 | int CONetCDF4::addVariable(const StdString& name, nc_type type, |
---|
| 310 | const std::vector<StdString>& dim) |
---|
[219] | 311 | { |
---|
[350] | 312 | int varid = 0; |
---|
[219] | 313 | std::vector<int> dimids; |
---|
[606] | 314 | std::vector<StdSize> dimsizes; |
---|
[857] | 315 | int dimSize = dim.size(); |
---|
[878] | 316 | |
---|
[606] | 317 | StdSize size; |
---|
| 318 | StdSize totalSize; |
---|
| 319 | StdSize maxSize = 1024 * 1024 * 256; // == 2GB/8 if output double |
---|
[498] | 320 | |
---|
[219] | 321 | int grpid = this->getCurrentGroup(); |
---|
[498] | 322 | |
---|
[606] | 323 | std::vector<StdString>::const_iterator it = dim.begin(), end = dim.end(); |
---|
[219] | 324 | |
---|
[878] | 325 | for (int idx = 0; it != end; it++, ++idx) |
---|
[219] | 326 | { |
---|
[686] | 327 | const StdString& dimid = *it; |
---|
[219] | 328 | dimids.push_back(this->getDimension(dimid)); |
---|
[606] | 329 | CNetCdfInterface::inqDimLen(grpid, this->getDimension(dimid), size); |
---|
| 330 | if (size == NC_UNLIMITED) size = 1; |
---|
| 331 | dimsizes.push_back(size); |
---|
[219] | 332 | } |
---|
[453] | 333 | |
---|
[606] | 334 | CNetCdfInterface::defVar(grpid, name, type, dimids.size(), &dimids[0], varid); |
---|
[498] | 335 | |
---|
[517] | 336 | // The classic NetCDF format does not support chunking nor fill parameters |
---|
| 337 | if (!useClassicFormat) |
---|
[453] | 338 | { |
---|
[517] | 339 | // set chunksize : size of one record |
---|
| 340 | // but must not be > 2GB (netcdf or HDF5 problem) |
---|
| 341 | totalSize = 1; |
---|
| 342 | for (vector<StdSize>::reverse_iterator it = dimsizes.rbegin(); it != dimsizes.rend(); ++it) |
---|
| 343 | { |
---|
| 344 | totalSize *= *it; |
---|
| 345 | if (totalSize >= maxSize) *it = 1; |
---|
| 346 | } |
---|
[857] | 347 | int storageType = (0 == dimSize) ? NC_CONTIGUOUS : NC_CHUNKED; |
---|
| 348 | CNetCdfInterface::defVarChunking(grpid, varid, storageType, &dimsizes[0]); |
---|
[517] | 349 | CNetCdfInterface::defVarFill(grpid, varid, true, NULL); |
---|
[453] | 350 | } |
---|
| 351 | |
---|
[606] | 352 | return varid; |
---|
[219] | 353 | } |
---|
| 354 | |
---|
| 355 | //--------------------------------------------------------------- |
---|
| 356 | |
---|
[606] | 357 | void CONetCDF4::setCompressionLevel(const StdString& varname, int compressionLevel) |
---|
| 358 | { |
---|
[607] | 359 | if (compressionLevel < 0 || compressionLevel > 9) |
---|
| 360 | ERROR("void CONetCDF4::setCompressionLevel(const StdString& varname, int compressionLevel)", |
---|
| 361 | "Invalid compression level for variable \"" << varname << "\", the value should range between 0 and 9."); |
---|
| 362 | if (compressionLevel && wmpi) |
---|
| 363 | ERROR("void CONetCDF4::setCompressionLevel(const StdString& varname, int compressionLevel)", |
---|
| 364 | "Impossible to use compression for variable \"" << varname << "\" when using parallel mode."); |
---|
| 365 | |
---|
[606] | 366 | int grpid = this->getCurrentGroup(); |
---|
| 367 | int varid = this->getVariable(varname); |
---|
| 368 | CNetCdfInterface::defVarDeflate(grpid, varid, compressionLevel); |
---|
| 369 | } |
---|
| 370 | |
---|
| 371 | //--------------------------------------------------------------- |
---|
| 372 | |
---|
[219] | 373 | template <> |
---|
[686] | 374 | void CONetCDF4::addAttribute(const StdString& name, const StdString& value, const StdString* varname) |
---|
[219] | 375 | { |
---|
| 376 | int grpid = this->getCurrentGroup(); |
---|
| 377 | int varid = (varname == NULL) ? NC_GLOBAL : this->getVariable(*varname); |
---|
[686] | 378 | CNetCdfInterface::putAttType(grpid, varid, name, value.size(), value.c_str()); |
---|
[219] | 379 | } |
---|
[498] | 380 | |
---|
[219] | 381 | //--------------------------------------------------------------- |
---|
[498] | 382 | |
---|
[219] | 383 | template <> |
---|
[686] | 384 | void CONetCDF4::addAttribute(const StdString& name, const double& value, const StdString* varname) |
---|
[219] | 385 | { |
---|
| 386 | int grpid = this->getCurrentGroup(); |
---|
| 387 | int varid = (varname == NULL) ? NC_GLOBAL : this->getVariable(*varname); |
---|
[686] | 388 | CNetCdfInterface::putAttType(grpid, varid, name, 1, &value); |
---|
[219] | 389 | } |
---|
[391] | 390 | |
---|
[686] | 391 | template <> |
---|
| 392 | void CONetCDF4::addAttribute(const StdString& name, const CArray<double,1>& value, const StdString* varname) |
---|
[391] | 393 | { |
---|
| 394 | int grpid = this->getCurrentGroup(); |
---|
| 395 | int varid = (varname == NULL) ? NC_GLOBAL : this->getVariable(*varname); |
---|
[686] | 396 | CNetCdfInterface::putAttType(grpid, varid, name, value.numElements(), value.dataFirst()); |
---|
[498] | 397 | } |
---|
[219] | 398 | //--------------------------------------------------------------- |
---|
[498] | 399 | |
---|
[219] | 400 | template <> |
---|
[686] | 401 | void CONetCDF4::addAttribute(const StdString& name, const float& value, const StdString* varname) |
---|
[219] | 402 | { |
---|
| 403 | int grpid = this->getCurrentGroup(); |
---|
| 404 | int varid = (varname == NULL) ? NC_GLOBAL : this->getVariable(*varname); |
---|
[686] | 405 | CNetCdfInterface::putAttType(grpid, varid, name, 1, &value); |
---|
[219] | 406 | } |
---|
[391] | 407 | |
---|
[686] | 408 | template <> |
---|
| 409 | void CONetCDF4::addAttribute(const StdString& name, const CArray<float,1>& value, const StdString* varname) |
---|
[391] | 410 | { |
---|
| 411 | int grpid = this->getCurrentGroup(); |
---|
| 412 | int varid = (varname == NULL) ? NC_GLOBAL : this->getVariable(*varname); |
---|
[686] | 413 | CNetCdfInterface::putAttType(grpid, varid, name, value.numElements(), value.dataFirst()); |
---|
[498] | 414 | } |
---|
| 415 | |
---|
[219] | 416 | //--------------------------------------------------------------- |
---|
[498] | 417 | |
---|
[219] | 418 | template <> |
---|
[686] | 419 | void CONetCDF4::addAttribute(const StdString& name, const int& value, const StdString* varname) |
---|
[219] | 420 | { |
---|
| 421 | int grpid = this->getCurrentGroup(); |
---|
| 422 | int varid = (varname == NULL) ? NC_GLOBAL : this->getVariable(*varname); |
---|
[686] | 423 | CNetCdfInterface::putAttType(grpid, varid, name, 1, &value); |
---|
[219] | 424 | } |
---|
| 425 | |
---|
[686] | 426 | template <> |
---|
| 427 | void CONetCDF4::addAttribute(const StdString& name, const CArray<int,1>& value, const StdString* varname) |
---|
[391] | 428 | { |
---|
| 429 | int grpid = this->getCurrentGroup(); |
---|
| 430 | int varid = (varname == NULL) ? NC_GLOBAL : this->getVariable(*varname); |
---|
[686] | 431 | CNetCdfInterface::putAttType(grpid, varid, name, value.numElements(), value.dataFirst()); |
---|
[498] | 432 | } |
---|
| 433 | |
---|
[472] | 434 | template <> |
---|
[686] | 435 | void CONetCDF4::addAttribute(const StdString& name, const short int& value, const StdString* varname) |
---|
[472] | 436 | { |
---|
| 437 | int grpid = this->getCurrentGroup(); |
---|
| 438 | int varid = (varname == NULL) ? NC_GLOBAL : this->getVariable(*varname); |
---|
[686] | 439 | CNetCdfInterface::putAttType(grpid, varid, name, 1, &value); |
---|
[472] | 440 | } |
---|
[219] | 441 | |
---|
[686] | 442 | template <> |
---|
| 443 | void CONetCDF4::addAttribute(const StdString& name, const CArray<short int,1>& value, const StdString* varname) |
---|
[472] | 444 | { |
---|
| 445 | int grpid = this->getCurrentGroup(); |
---|
| 446 | int varid = (varname == NULL) ? NC_GLOBAL : this->getVariable(*varname); |
---|
[686] | 447 | CNetCdfInterface::putAttType(grpid, varid, name, value.numElements(), value.dataFirst()); |
---|
[498] | 448 | } |
---|
| 449 | |
---|
[472] | 450 | template <> |
---|
[686] | 451 | void CONetCDF4::addAttribute(const StdString& name, const long int& value, const StdString* varname) |
---|
[472] | 452 | { |
---|
| 453 | int grpid = this->getCurrentGroup(); |
---|
| 454 | int varid = (varname == NULL) ? NC_GLOBAL : this->getVariable(*varname); |
---|
[686] | 455 | CNetCdfInterface::putAttType(grpid, varid, name, 1, &value); |
---|
[472] | 456 | } |
---|
| 457 | |
---|
[686] | 458 | template <> |
---|
| 459 | void CONetCDF4::addAttribute(const StdString& name, const CArray<long int,1>& value, const StdString* varname) |
---|
[472] | 460 | { |
---|
| 461 | int grpid = this->getCurrentGroup(); |
---|
| 462 | int varid = (varname == NULL) ? NC_GLOBAL : this->getVariable(*varname); |
---|
[686] | 463 | CNetCdfInterface::putAttType(grpid, varid, name, value.numElements(), value.dataFirst()); |
---|
[498] | 464 | } |
---|
| 465 | |
---|
[686] | 466 | //--------------------------------------------------------------- |
---|
[498] | 467 | |
---|
[686] | 468 | void CONetCDF4::getWriteDataInfos(const StdString& name, StdSize record, StdSize& array_size, |
---|
| 469 | std::vector<StdSize>& sstart, |
---|
| 470 | std::vector<StdSize>& scount, |
---|
| 471 | const std::vector<StdSize>* start, |
---|
| 472 | const std::vector<StdSize>* count) |
---|
[498] | 473 | { |
---|
[266] | 474 | std::vector<std::size_t> sizes = this->getDimensions(name); |
---|
[857] | 475 | if (sizes.size()==0) |
---|
[266] | 476 | { |
---|
[857] | 477 | array_size=1 ; |
---|
| 478 | sstart.push_back(0); |
---|
[606] | 479 | scount.push_back(1); |
---|
[266] | 480 | } |
---|
[857] | 481 | else |
---|
| 482 | { |
---|
| 483 | std::vector<std::string> iddims = this->getDimensionsIdList (&name); |
---|
| 484 | std::vector<std::size_t>::const_iterator |
---|
| 485 | it = sizes.begin(), end = sizes.end(); |
---|
| 486 | int i = 0; |
---|
[219] | 487 | |
---|
[857] | 488 | if (iddims.begin()->compare(timeCounterName) == 0) |
---|
| 489 | { |
---|
| 490 | sstart.push_back(record); |
---|
| 491 | scount.push_back(1); |
---|
| 492 | if ((start == NULL) && |
---|
| 493 | (count == NULL)) i++; |
---|
| 494 | it++; |
---|
| 495 | } |
---|
| 496 | |
---|
| 497 | for (;it != end; it++) |
---|
| 498 | { |
---|
| 499 | if ((start != NULL) && (count != NULL)) |
---|
| 500 | { |
---|
| 501 | sstart.push_back((*start)[i]); |
---|
| 502 | scount.push_back((*count)[i]); |
---|
| 503 | array_size *= (*count)[i]; |
---|
| 504 | i++; |
---|
| 505 | } |
---|
| 506 | else |
---|
| 507 | { |
---|
| 508 | sstart.push_back(0); |
---|
| 509 | scount.push_back(sizes[i]); |
---|
| 510 | array_size *= sizes[i]; |
---|
| 511 | i++; |
---|
| 512 | } |
---|
| 513 | } |
---|
| 514 | |
---|
[219] | 515 | } |
---|
| 516 | } |
---|
[498] | 517 | |
---|
[857] | 518 | |
---|
[219] | 519 | template <> |
---|
[686] | 520 | void CONetCDF4::writeData_(int grpid, int varid, |
---|
| 521 | const std::vector<StdSize>& sstart, |
---|
| 522 | const std::vector<StdSize>& scount, const double* data) |
---|
[219] | 523 | { |
---|
[686] | 524 | CNetCdfInterface::putVaraType(grpid, varid, &sstart[0], &scount[0], data); |
---|
[219] | 525 | } |
---|
[498] | 526 | |
---|
[219] | 527 | //--------------------------------------------------------------- |
---|
[498] | 528 | |
---|
[219] | 529 | template <> |
---|
[686] | 530 | void CONetCDF4::writeData_(int grpid, int varid, |
---|
| 531 | const std::vector<StdSize>& sstart, |
---|
[1158] | 532 | const std::vector<StdSize>& scount, char* data) |
---|
| 533 | { |
---|
| 534 | CNetCdfInterface::putVaraType(grpid, varid, &sstart[0], &scount[0], data); |
---|
| 535 | } |
---|
| 536 | |
---|
| 537 | template <> |
---|
[1201] | 538 | |
---|
[1158] | 539 | void CONetCDF4::writeData_(int grpid, int varid, |
---|
| 540 | const std::vector<StdSize>& sstart, |
---|
[686] | 541 | const std::vector<StdSize>& scount, const int* data) |
---|
[219] | 542 | { |
---|
[686] | 543 | CNetCdfInterface::putVaraType(grpid, varid, &sstart[0], &scount[0], data); |
---|
[219] | 544 | } |
---|
| 545 | //--------------------------------------------------------------- |
---|
[498] | 546 | |
---|
[219] | 547 | template <> |
---|
[686] | 548 | void CONetCDF4::writeData_(int grpid, int varid, |
---|
| 549 | const std::vector<StdSize>& sstart, |
---|
| 550 | const std::vector<StdSize>& scount, const float* data) |
---|
[219] | 551 | { |
---|
[686] | 552 | CNetCdfInterface::putVaraType(grpid, varid, &sstart[0], &scount[0], data); |
---|
[219] | 553 | } |
---|
| 554 | |
---|
| 555 | //--------------------------------------------------------------- |
---|
| 556 | |
---|
[686] | 557 | void CONetCDF4::writeData(const CArray<int, 2>& data, const StdString& name) |
---|
[219] | 558 | { |
---|
| 559 | int grpid = this->getCurrentGroup(); |
---|
| 560 | int varid = this->getVariable(name); |
---|
| 561 | StdSize array_size = 1; |
---|
| 562 | std::vector<StdSize> sstart, scount; |
---|
| 563 | |
---|
| 564 | this->getWriteDataInfos(name, 0, array_size, sstart, scount, NULL, NULL); |
---|
[1158] | 565 | |
---|
[369] | 566 | this->writeData_(grpid, varid, sstart, scount, data.dataFirst()); |
---|
[1158] | 567 | |
---|
[219] | 568 | } |
---|
| 569 | |
---|
[686] | 570 | void CONetCDF4::writeTimeAxisData(const CArray<double, 1>& data, const StdString& name, |
---|
[413] | 571 | bool collective, StdSize record, bool isRoot) |
---|
| 572 | { |
---|
| 573 | int grpid = this->getCurrentGroup(); |
---|
| 574 | int varid = this->getVariable(name); |
---|
[498] | 575 | |
---|
[686] | 576 | map<int,size_t>::iterator it=timeAxis.find(varid); |
---|
| 577 | if (it == timeAxis.end()) timeAxis[varid] = record; |
---|
[498] | 578 | else |
---|
[413] | 579 | { |
---|
[686] | 580 | if (it->second >= record) return; |
---|
| 581 | else it->second =record; |
---|
[413] | 582 | } |
---|
[498] | 583 | |
---|
[413] | 584 | StdSize array_size = 1; |
---|
| 585 | std::vector<StdSize> sstart, scount; |
---|
[498] | 586 | |
---|
[413] | 587 | if (this->wmpi && collective) |
---|
[686] | 588 | CNetCdfInterface::varParAccess(grpid, varid, NC_COLLECTIVE); |
---|
[413] | 589 | if (this->wmpi && !collective) |
---|
[686] | 590 | CNetCdfInterface::varParAccess(grpid, varid, NC_INDEPENDENT); |
---|
[498] | 591 | |
---|
[413] | 592 | this->getWriteDataInfos(name, record, array_size, sstart, scount, NULL, NULL); |
---|
[1158] | 593 | this->writeData_(grpid, varid, sstart, scount, data.dataFirst()); |
---|
| 594 | } |
---|
| 595 | |
---|
| 596 | void CONetCDF4::writeTimeAxisDataBounds(const CArray<double, 1>& data, const StdString& name, |
---|
| 597 | bool collective, StdSize record, bool isRoot) |
---|
| 598 | { |
---|
| 599 | int grpid = this->getCurrentGroup(); |
---|
| 600 | int varid = this->getVariable(name); |
---|
| 601 | |
---|
| 602 | map<int,size_t>::iterator it=timeAxis.find(varid); |
---|
| 603 | if (it == timeAxis.end()) timeAxis[varid] = record; |
---|
| 604 | else |
---|
[686] | 605 | { |
---|
[1158] | 606 | if (it->second >= record) return; |
---|
| 607 | else it->second =record; |
---|
[686] | 608 | } |
---|
[1158] | 609 | |
---|
| 610 | StdSize array_size = 1; |
---|
| 611 | std::vector<StdSize> sstart, scount; |
---|
| 612 | |
---|
| 613 | if (this->wmpi && collective) |
---|
| 614 | CNetCdfInterface::varParAccess(grpid, varid, NC_COLLECTIVE); |
---|
| 615 | if (this->wmpi && !collective) |
---|
| 616 | CNetCdfInterface::varParAccess(grpid, varid, NC_INDEPENDENT); |
---|
| 617 | |
---|
| 618 | this->getWriteDataInfos(name, record, array_size, sstart, scount, NULL, NULL); |
---|
[413] | 619 | this->writeData_(grpid, varid, sstart, scount, data.dataFirst()); |
---|
| 620 | } |
---|
| 621 | |
---|
[1158] | 622 | |
---|
[219] | 623 | //--------------------------------------------------------------- |
---|
[498] | 624 | |
---|
[686] | 625 | bool CONetCDF4::varExist(const StdString& varname) |
---|
[219] | 626 | { |
---|
| 627 | int grpid = this->getCurrentGroup(); |
---|
[686] | 628 | return CNetCdfInterface::isVarExisted(grpid, varname); |
---|
[219] | 629 | } |
---|
| 630 | |
---|
[878] | 631 | bool CONetCDF4::dimExist(const StdString& dimname) |
---|
| 632 | { |
---|
| 633 | int grpid = this->getCurrentGroup(); |
---|
| 634 | return CNetCdfInterface::isDimExisted(grpid, dimname); |
---|
| 635 | } |
---|
| 636 | |
---|
[286] | 637 | void CONetCDF4::sync(void) |
---|
| 638 | { |
---|
[686] | 639 | CNetCdfInterface::sync(this->ncidp); |
---|
[498] | 640 | } |
---|
[219] | 641 | ///-------------------------------------------------------------- |
---|
[337] | 642 | } // namespace xios |
---|