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