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