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