[498] | 1 | /*! |
---|
| 2 | \file netCdfInterface.cpp |
---|
| 3 | \author Ha NGUYEN |
---|
| 4 | \date 08 Oct 2014 |
---|
| 5 | \since 03 Oct 2014 |
---|
| 6 | |
---|
| 7 | \brief Wrapper of netcdf functions. |
---|
| 8 | */ |
---|
| 9 | |
---|
[525] | 10 | #include "netCdfInterface.hpp" |
---|
| 11 | #include "netCdfException.hpp" |
---|
[1601] | 12 | #include "ep_mpi.hpp" |
---|
[1646] | 13 | |
---|
[525] | 14 | namespace xios |
---|
| 15 | { |
---|
| 16 | /*! |
---|
| 17 | This function creates a new netcdf file and return its id |
---|
| 18 | \param [in] fileName Name of the file |
---|
| 19 | \param [in] cMode create mode |
---|
| 20 | \param [in/out] ncId id of the created file |
---|
| 21 | \return Status code |
---|
| 22 | */ |
---|
| 23 | int CNetCdfInterface::create(const StdString& fileName, int cMode, int& ncId) |
---|
| 24 | { |
---|
[1601] | 25 | int status; |
---|
| 26 | #pragma omp critical (_netcdf) |
---|
| 27 | { |
---|
[1646] | 28 | info(200)<<"start nc_create"<<std::endl; |
---|
[1601] | 29 | status = nc_create(fileName.c_str(), cMode, &ncId); |
---|
[1646] | 30 | info(200)<<"end nc_create"<<std::endl; |
---|
[1601] | 31 | } |
---|
[525] | 32 | if (NC_NOERR != status) |
---|
| 33 | { |
---|
| 34 | StdString errormsg(nc_strerror(status)); |
---|
| 35 | StdStringStream sstr; |
---|
[686] | 36 | sstr << "Error when calling function: nc_create(fileName.c_str(), cMode, &ncId) " << std::endl |
---|
[525] | 37 | << errormsg << std::endl |
---|
[686] | 38 | << "Unable to create file, given its name: " << fileName |
---|
[1370] | 39 | << " and its creation mode " << creationMode2String(cMode) << std::endl; |
---|
[525] | 40 | StdString e = sstr.str(); |
---|
| 41 | throw CNetCdfException(e); |
---|
| 42 | } |
---|
[498] | 43 | |
---|
[525] | 44 | return status; |
---|
| 45 | } |
---|
[498] | 46 | |
---|
[525] | 47 | /*! |
---|
| 48 | This function creates a new netcdf file on parallel file system |
---|
| 49 | \param [in] fileName Name of the file |
---|
| 50 | \param [in] cMode create mode |
---|
| 51 | \param [in] comm MPI communicator |
---|
| 52 | \param [in] info MPI information |
---|
| 53 | \param [in/out] ncId id of the created file |
---|
| 54 | \return Status code |
---|
| 55 | */ |
---|
| 56 | int CNetCdfInterface::createPar(const StdString& fileName, int cMode, MPI_Comm comm, MPI_Info info, int& ncId) |
---|
| 57 | { |
---|
[1646] | 58 | #ifdef _usingEP |
---|
[1601] | 59 | int status = xios::nc_create_par(fileName.c_str(), cMode, comm, to_mpi_info(MPI_INFO_NULL), &ncId); |
---|
[1665] | 60 | #else |
---|
[1646] | 61 | int status = xios::nc_create_par(fileName.c_str(), cMode, comm, MPI_INFO_NULL, &ncId); |
---|
| 62 | #endif |
---|
[1601] | 63 | |
---|
[525] | 64 | if (NC_NOERR != status) |
---|
| 65 | { |
---|
| 66 | StdString errormsg(nc_strerror(status)); |
---|
| 67 | StdStringStream sstr; |
---|
[686] | 68 | sstr << "Error when calling function: nc_create_par(fileName.c_str(), cMode, comm, info, &ncId) " << std::endl |
---|
[525] | 69 | << errormsg << std::endl |
---|
[686] | 70 | << "Unable to create file on parallel file system, given its name: " << std::endl |
---|
[525] | 71 | << "and its creation mode " << creationMode2String(cMode) << std::endl; |
---|
| 72 | StdString e = sstr.str(); |
---|
| 73 | throw CNetCdfException(e); |
---|
| 74 | } |
---|
[498] | 75 | |
---|
[525] | 76 | return status; |
---|
| 77 | } |
---|
[498] | 78 | |
---|
[525] | 79 | /*! |
---|
| 80 | This function opens a netcdf file, given its name and open mode, return its id |
---|
| 81 | \param [in] fileName Name of the file |
---|
| 82 | \param [in] oMode open mode |
---|
| 83 | \param [in/out] ncId id of the opening file |
---|
| 84 | \return Status code |
---|
| 85 | */ |
---|
| 86 | int CNetCdfInterface::open(const StdString& fileName, int oMode, int& ncId) |
---|
| 87 | { |
---|
[1601] | 88 | int status; |
---|
| 89 | #pragma omp critical (_netcdf) |
---|
| 90 | { |
---|
[1646] | 91 | info(200)<<"start nc_open"<<std::endl; |
---|
[1601] | 92 | status = nc_open(fileName.c_str(), oMode, &ncId); |
---|
[1646] | 93 | info(200)<<"end nc_open"<<std::endl; |
---|
[1601] | 94 | } |
---|
[525] | 95 | if (NC_NOERR != status) |
---|
| 96 | { |
---|
| 97 | StdString errormsg(nc_strerror(status)); |
---|
| 98 | StdStringStream sstr; |
---|
[686] | 99 | sstr << "Error when calling function: nc_open(fileName.c_str(), oMode, &ncId) "<< std::endl |
---|
[525] | 100 | << errormsg << std::endl |
---|
[686] | 101 | << "Unable to open file, given its name: " << fileName |
---|
[525] | 102 | << "and its open mode " << openMode2String(oMode) << std::endl; |
---|
| 103 | StdString e = sstr.str(); |
---|
| 104 | throw CNetCdfException(e); |
---|
| 105 | } |
---|
[498] | 106 | |
---|
[525] | 107 | return status; |
---|
| 108 | } |
---|
[498] | 109 | |
---|
| 110 | |
---|
[525] | 111 | /*! |
---|
| 112 | This function opens a new netcdf file on parallel file system |
---|
| 113 | \param [in] fileName Name of the file |
---|
| 114 | \param [in] oMode open mode |
---|
| 115 | \param [in] comm MPI communicator |
---|
| 116 | \param [in] info MPI information |
---|
| 117 | \param [in/out] ncId id of the opened file |
---|
| 118 | \return Status code |
---|
| 119 | */ |
---|
| 120 | int CNetCdfInterface::openPar(const StdString& fileName, int oMode, MPI_Comm comm, MPI_Info info, int& ncId) |
---|
| 121 | { |
---|
[1646] | 122 | #ifdef _usingEP |
---|
[1601] | 123 | int status = xios::nc_open_par(fileName.c_str(), oMode, comm, to_mpi_info(MPI_INFO_NULL), &ncId); |
---|
[1665] | 124 | #else |
---|
[1646] | 125 | int status = xios::nc_open_par(fileName.c_str(), oMode, comm, MPI_INFO_NULL, &ncId); |
---|
| 126 | #endif |
---|
[1601] | 127 | |
---|
[525] | 128 | if (NC_NOERR != status) |
---|
| 129 | { |
---|
| 130 | StdString errormsg(nc_strerror(status)); |
---|
| 131 | StdStringStream sstr; |
---|
[686] | 132 | sstr << "Error when calling function nc_open_par(fileName.c_str(), oMode, comm, info, &ncId) " << std::endl |
---|
[525] | 133 | << errormsg << std::endl |
---|
[686] | 134 | << "Unable to open file on parallel file system, given its name: " << fileName |
---|
[525] | 135 | << "and its open mode " << openMode2String(oMode) << std::endl; |
---|
| 136 | StdString e = sstr.str(); |
---|
| 137 | throw CNetCdfException(e); |
---|
| 138 | } |
---|
[498] | 139 | |
---|
[525] | 140 | return status; |
---|
| 141 | } |
---|
[498] | 142 | |
---|
[525] | 143 | /*! |
---|
| 144 | This function closes a netcdf file, given its id |
---|
| 145 | \param [in] ncId id of the opening netcdf file |
---|
| 146 | \return Status code |
---|
| 147 | */ |
---|
| 148 | int CNetCdfInterface::close(int ncId) |
---|
| 149 | { |
---|
[1601] | 150 | int status = NC_NOERR; |
---|
| 151 | #pragma omp critical (_netcdf) |
---|
| 152 | { |
---|
[1646] | 153 | info(200)<<"start nc_close"<<std::endl; |
---|
[1601] | 154 | status = nc_close(ncId); |
---|
[1646] | 155 | info(200)<<"end nc_close"<<std::endl; |
---|
[1601] | 156 | } |
---|
| 157 | |
---|
[525] | 158 | if (NC_NOERR != status) |
---|
| 159 | { |
---|
| 160 | StdString errormsg(nc_strerror(status)); |
---|
| 161 | StdStringStream sstr; |
---|
[686] | 162 | sstr << "Error when calling function nc_close(ncId)" << std::endl |
---|
[498] | 163 | << errormsg << std::endl |
---|
[686] | 164 | << "Unable to close file, given its id: " << ncId << std::endl; |
---|
[525] | 165 | StdString e = sstr.str(); |
---|
| 166 | throw CNetCdfException(e); |
---|
| 167 | } |
---|
[498] | 168 | |
---|
[525] | 169 | return status; |
---|
| 170 | } |
---|
[498] | 171 | |
---|
[525] | 172 | /*! |
---|
| 173 | This function put a netcdf file into define mode, given its id |
---|
| 174 | \param [in] ncId id of the opening netcdf file to be put into define mode |
---|
| 175 | \return Status code |
---|
| 176 | */ |
---|
| 177 | int CNetCdfInterface::reDef(int ncId) |
---|
| 178 | { |
---|
[1601] | 179 | int status; |
---|
| 180 | #pragma omp critical (_netcdf) |
---|
| 181 | { |
---|
[1646] | 182 | info(200)<<"start nc_reDef"<<std::endl; |
---|
[1601] | 183 | status = nc_redef(ncId); |
---|
[1646] | 184 | info(200)<<"end nc_reDef"<<std::endl; |
---|
[1601] | 185 | } |
---|
| 186 | |
---|
[525] | 187 | if (NC_NOERR != status) |
---|
| 188 | { |
---|
| 189 | StdString errormsg(nc_strerror(status)); |
---|
| 190 | StdStringStream sstr; |
---|
[686] | 191 | sstr << "Error when calling function nc_redef(ncId)" << std::endl |
---|
[525] | 192 | << errormsg << std::endl |
---|
[686] | 193 | << "Unable to put this file into define mode given its id: " << ncId << std::endl; |
---|
[525] | 194 | StdString e = sstr.str(); |
---|
| 195 | throw CNetCdfException(e); |
---|
| 196 | } |
---|
[498] | 197 | |
---|
[525] | 198 | return status; |
---|
| 199 | } |
---|
[498] | 200 | |
---|
[525] | 201 | /*! |
---|
| 202 | This function ends a netcdf file define mode, given its id |
---|
| 203 | \param [in] ncId id of the opening netcdf file to be put into define mode |
---|
| 204 | \return Status code |
---|
| 205 | */ |
---|
| 206 | int CNetCdfInterface::endDef(int ncId) |
---|
| 207 | { |
---|
[1601] | 208 | int status; |
---|
| 209 | #pragma omp critical (_netcdf) |
---|
| 210 | { |
---|
[1646] | 211 | info(200)<<"start nc_enddef"<<std::endl; |
---|
[1601] | 212 | status = nc_enddef(ncId); |
---|
[1646] | 213 | info(200)<<"end nc_enddef"<<std::endl; |
---|
[1601] | 214 | } |
---|
[525] | 215 | if (NC_NOERR != status) |
---|
| 216 | { |
---|
| 217 | StdString errormsg(nc_strerror(status)); |
---|
| 218 | StdStringStream sstr; |
---|
[498] | 219 | |
---|
[686] | 220 | sstr << "Error when calling function nc_enddef(ncId)" << std::endl |
---|
[525] | 221 | << errormsg << std::endl |
---|
[686] | 222 | << "Unable to end define mode of this file, given its id: " << ncId << std::endl; |
---|
[525] | 223 | StdString e = sstr.str(); |
---|
| 224 | throw CNetCdfException(e); |
---|
| 225 | } |
---|
[498] | 226 | |
---|
[525] | 227 | return status; |
---|
| 228 | } |
---|
[498] | 229 | |
---|
[525] | 230 | /*! |
---|
| 231 | This function makes a request to netcdf with ncid and group name then return ncid of the named group |
---|
| 232 | \param [in] ncid Groupd id (or File Id) |
---|
| 233 | \param [in] grpName Name of the desired group (or file) |
---|
| 234 | \param [in/out] grpId Group id if the group is found |
---|
| 235 | \return Status code |
---|
| 236 | */ |
---|
| 237 | int CNetCdfInterface::inqNcId(int ncid, const StdString& grpName, int& grpId) |
---|
| 238 | { |
---|
[1601] | 239 | int status; |
---|
| 240 | #pragma omp critical (_netcdf) |
---|
| 241 | { |
---|
[1646] | 242 | info(200)<<"start nc_inq_ncid"<<std::endl; |
---|
[1601] | 243 | status = nc_inq_ncid(ncid, grpName.c_str(), &grpId); |
---|
[1646] | 244 | info(200)<<"end nc_inq_ncid"<<std::endl; |
---|
[1601] | 245 | } |
---|
| 246 | |
---|
[525] | 247 | if (NC_NOERR != status) |
---|
| 248 | { |
---|
| 249 | StdString errormsg(nc_strerror(status)); |
---|
| 250 | StdStringStream sstr; |
---|
[498] | 251 | |
---|
[686] | 252 | sstr << "Error when calling function nc_inq_ncid(ncid, grpName.c_str(), &grpId)" << std::endl |
---|
[525] | 253 | << errormsg << std::endl |
---|
[686] | 254 | << "Unable to get id of a group (File), given its name: " << grpName << std::endl; |
---|
[525] | 255 | StdString e = sstr.str(); |
---|
| 256 | throw CNetCdfException(e); |
---|
| 257 | } |
---|
[498] | 258 | |
---|
[525] | 259 | return status; |
---|
| 260 | } |
---|
[498] | 261 | |
---|
| 262 | |
---|
[525] | 263 | /*! |
---|
| 264 | This function makes a request to netcdf with ncid and variable name then return ncid of the named variable |
---|
| 265 | \param [in] ncid Groupd id (or File Id) |
---|
| 266 | \param [in] varName Name of the desired variable |
---|
| 267 | \param [in/out] varId Variable id if this variable is found |
---|
| 268 | \return Status code |
---|
| 269 | */ |
---|
[686] | 270 | int CNetCdfInterface::inqVarId(int ncid, const StdString& varName, int& varId) |
---|
[525] | 271 | { |
---|
[1601] | 272 | int status; |
---|
| 273 | #pragma omp critical (_netcdf) |
---|
| 274 | { |
---|
[1646] | 275 | info(200)<<"start nc_inq_varid"<<std::endl; |
---|
[1601] | 276 | status = nc_inq_varid(ncid, varName.c_str(), &varId); |
---|
[1646] | 277 | info(200)<<"end nc_inq_varid"<<std::endl; |
---|
[1601] | 278 | } |
---|
[525] | 279 | if (NC_NOERR != status) |
---|
| 280 | { |
---|
| 281 | StdString errormsg(nc_strerror(status)); |
---|
| 282 | StdStringStream sstr; |
---|
[498] | 283 | |
---|
[686] | 284 | sstr << "Error when calling function: nc_inq_varid(ncid, varName.c_str(), &varId)" << std::endl |
---|
[525] | 285 | << (errormsg) << std::endl |
---|
[686] | 286 | << "Unable to get id of variable with name: " << varName << std::endl; |
---|
[525] | 287 | StdString e = sstr.str(); |
---|
| 288 | throw CNetCdfException(e); |
---|
| 289 | } |
---|
[498] | 290 | |
---|
[525] | 291 | return status; |
---|
| 292 | } |
---|
[498] | 293 | |
---|
[525] | 294 | /*! |
---|
| 295 | This function makes a request to netcdf with a netCdf dimension name then return ncid of the named dimension |
---|
| 296 | \param [in] ncid Groupd id (or File Id) |
---|
| 297 | \param [in] dimName Name of the desired dimension |
---|
| 298 | \param [in/out] dimId Dimension id if this dimension is found |
---|
| 299 | \return Status code |
---|
| 300 | */ |
---|
[686] | 301 | int CNetCdfInterface::inqDimId(int ncid, const StdString& dimName, int& dimId) |
---|
[525] | 302 | { |
---|
[1601] | 303 | int status; |
---|
| 304 | #pragma omp critical (_netcdf) |
---|
| 305 | { |
---|
[1646] | 306 | info(200)<<"start nc_inq_dimid"<<std::endl; |
---|
[1601] | 307 | status = nc_inq_dimid(ncid, dimName.c_str(), &dimId); |
---|
[1646] | 308 | info(200)<<"end nc_inq_dimid"<<std::endl; |
---|
[1601] | 309 | } |
---|
| 310 | |
---|
[525] | 311 | if (NC_NOERR != status) |
---|
| 312 | { |
---|
| 313 | StdString errormsg(nc_strerror(status)); |
---|
| 314 | StdStringStream sstr; |
---|
[498] | 315 | |
---|
[686] | 316 | sstr << "Error when calling function nc_inq_dimid(ncid, dimName.c_str(), &dimId)" << std::endl |
---|
[498] | 317 | << errormsg << std::endl |
---|
[686] | 318 | << "Unable to get id of dimension, given its name: " << dimName << std::endl; |
---|
[525] | 319 | StdString e = sstr.str(); |
---|
| 320 | throw CNetCdfException(e); |
---|
| 321 | } |
---|
[498] | 322 | |
---|
[525] | 323 | return status; |
---|
| 324 | } |
---|
[498] | 325 | |
---|
[525] | 326 | /*! |
---|
[686] | 327 | This function queries the name of a variable given its id. |
---|
| 328 | \param [in] ncid Groupd id (or File Id) |
---|
| 329 | \param [in] varId Id of desired variable |
---|
| 330 | \param [out] varName name of desired variable |
---|
| 331 | \return Status code |
---|
| 332 | */ |
---|
| 333 | int CNetCdfInterface::inqVarName(int ncid, int varId, StdString& varName) |
---|
| 334 | { |
---|
| 335 | char varNameBuff[NC_MAX_NAME + 1]; |
---|
[1601] | 336 | int status; |
---|
| 337 | #pragma omp critical (_netcdf) |
---|
| 338 | { |
---|
[1646] | 339 | info(200)<<"start nc_inq_varname"<<std::endl; |
---|
[1601] | 340 | status = nc_inq_varname(ncid, varId, varNameBuff); |
---|
[1646] | 341 | info(200)<<"end nc_inq_varname"<<std::endl; |
---|
[1601] | 342 | } |
---|
[686] | 343 | if (NC_NOERR != status) |
---|
| 344 | { |
---|
| 345 | StdString errormsg(nc_strerror(status)); |
---|
| 346 | StdStringStream sstr; |
---|
| 347 | |
---|
| 348 | sstr << "Error when calling function nc_inq_varname(ncid, varId, varNameBuff)" << std::endl |
---|
| 349 | << errormsg << std::endl |
---|
[833] | 350 | << "Unable to get variable name: "<< varName << " given its id: " << varId << std::endl; |
---|
[686] | 351 | StdString e = sstr.str(); |
---|
| 352 | throw CNetCdfException(e); |
---|
| 353 | } |
---|
| 354 | varName = varNameBuff; |
---|
| 355 | return status; |
---|
| 356 | } |
---|
| 357 | |
---|
| 358 | /*! |
---|
[525] | 359 | This function makes a request to netcdf with a netCdf dimension name then return ncid of the named dimension |
---|
| 360 | \param [in] ncid Groupd id (or File Id) |
---|
| 361 | \param [in/out] dimId Dimension id if this dimension is found |
---|
| 362 | \return Status code |
---|
| 363 | */ |
---|
| 364 | int CNetCdfInterface::inqUnLimDim(int ncid, int& dimId) |
---|
| 365 | { |
---|
[1601] | 366 | int status; |
---|
| 367 | #pragma omp critical (_netcdf) |
---|
| 368 | { |
---|
[1646] | 369 | info(200)<<"start nc_inq_unlimdim"<<std::endl; |
---|
[1601] | 370 | status = nc_inq_unlimdim(ncid, &dimId); |
---|
[1646] | 371 | info(200)<<"end nc_inq_unlimdim"<<std::endl; |
---|
[1601] | 372 | } |
---|
[525] | 373 | if (NC_NOERR != status) |
---|
| 374 | { |
---|
| 375 | StdString errormsg(nc_strerror(status)); |
---|
| 376 | StdStringStream sstr; |
---|
[498] | 377 | |
---|
[686] | 378 | sstr << "Error when calling function nc_inq_dimid" << std::endl |
---|
[525] | 379 | << errormsg << std::endl |
---|
| 380 | << "Unable to get id of unlimited dimension " << std::endl; |
---|
| 381 | StdString e = sstr.str(); |
---|
| 382 | throw CNetCdfException(e); |
---|
| 383 | } |
---|
[498] | 384 | |
---|
[525] | 385 | return status; |
---|
| 386 | } |
---|
[498] | 387 | |
---|
[525] | 388 | /*! |
---|
| 389 | This function makes a request to netcdf, returns name of a dimension, given its id |
---|
| 390 | \param [in] ncid Groupd id (or File Id) |
---|
| 391 | \param [in] dimId Id of desired dimension |
---|
[686] | 392 | \param [out] dimName Name of desired dimension |
---|
[525] | 393 | \return Status code |
---|
| 394 | */ |
---|
| 395 | int CNetCdfInterface::inqDimName(int ncid, int dimId, StdString& dimName) |
---|
| 396 | { |
---|
[686] | 397 | char fullNameIn[NC_MAX_NAME + 1]; |
---|
[1601] | 398 | int status; |
---|
| 399 | #pragma omp critical (_netcdf) |
---|
| 400 | { |
---|
[1646] | 401 | info(200)<<"start nc_inq_dimname"<<std::endl; |
---|
[1601] | 402 | status = nc_inq_dimname(ncid, dimId, fullNameIn); |
---|
[1646] | 403 | info(200)<<"end nc_inq_dimname"<<std::endl; |
---|
[1601] | 404 | } |
---|
[525] | 405 | if (NC_NOERR != status) |
---|
| 406 | { |
---|
| 407 | StdString errormsg(nc_strerror(status)); |
---|
| 408 | StdStringStream sstr; |
---|
[498] | 409 | |
---|
[686] | 410 | sstr << "Error when calling function nc_inq_dimname(ncid, dimId, fullNameIn)" << std::endl |
---|
[525] | 411 | << errormsg << std::endl |
---|
[833] | 412 | << "Unable to get dimension name: " << dimName << " given its id: " << dimId << std::endl; |
---|
[525] | 413 | StdString e = sstr.str(); |
---|
| 414 | throw CNetCdfException(e); |
---|
| 415 | } |
---|
| 416 | dimName = StdString(fullNameIn); |
---|
| 417 | return status; |
---|
| 418 | } |
---|
[498] | 419 | |
---|
[525] | 420 | /*! |
---|
| 421 | This function makes a request to netcdf, returns length of a dimension, given its id |
---|
| 422 | \param [in] ncid Groupd id (or File Id) |
---|
| 423 | \param [in] dimId Id of desired dimension |
---|
| 424 | \param [in/out] dimLen Length of desired dimension |
---|
| 425 | \return Status code |
---|
| 426 | */ |
---|
| 427 | int CNetCdfInterface::inqDimLen(int ncid, int dimId, StdSize& dimLen) |
---|
| 428 | { |
---|
[1601] | 429 | int status; |
---|
| 430 | #pragma omp critical (_netcdf) |
---|
| 431 | { |
---|
[1646] | 432 | info(200)<<"start nc_inq_dimlen"<<std::endl; |
---|
[1601] | 433 | status = nc_inq_dimlen(ncid, dimId, &dimLen); |
---|
[1646] | 434 | info(200)<<"end nc_inq_dimlen"<<std::endl; |
---|
[1601] | 435 | } |
---|
[525] | 436 | if (NC_NOERR != status) |
---|
| 437 | { |
---|
| 438 | StdString errormsg(nc_strerror(status)); |
---|
| 439 | StdStringStream sstr; |
---|
[498] | 440 | |
---|
[686] | 441 | sstr << "Error when calling function nc_inq_dimlen(ncid, dimId, &dimLen)" << std::endl |
---|
[525] | 442 | << errormsg << std::endl |
---|
[686] | 443 | << "Unable to get dimension length given its id: " << dimId << std::endl; |
---|
[525] | 444 | StdString e = sstr.str(); |
---|
| 445 | throw CNetCdfException(e); |
---|
| 446 | } |
---|
[498] | 447 | |
---|
[525] | 448 | return status; |
---|
| 449 | } |
---|
[498] | 450 | |
---|
[525] | 451 | /*! |
---|
| 452 | This function makes a request to netcdf, returns number of dimensions of a variable, given its id |
---|
| 453 | \param [in] ncid Groupd id (or File Id) |
---|
| 454 | \param [in] varId Id of variable |
---|
| 455 | \param [in/out] ndims number of dimension of the variable |
---|
| 456 | \return Status code |
---|
| 457 | */ |
---|
| 458 | int CNetCdfInterface::inqVarNDims(int ncid, int varId, int& nDims) |
---|
| 459 | { |
---|
[1601] | 460 | int status; |
---|
| 461 | #pragma omp critical (_netcdf) |
---|
| 462 | { |
---|
[1646] | 463 | info(200)<<"start nc_inq_varndims"<<std::endl; |
---|
[1601] | 464 | status = nc_inq_varndims(ncid, varId, &nDims); |
---|
[1646] | 465 | info(200)<<"end nc_inq_varndims"<<std::endl; |
---|
[1601] | 466 | } |
---|
[525] | 467 | if (NC_NOERR != status) |
---|
| 468 | { |
---|
| 469 | StdString errormsg(nc_strerror(status)); |
---|
| 470 | StdStringStream sstr; |
---|
[498] | 471 | |
---|
[686] | 472 | sstr << "Error when calling function nc_inq_varndims(ncid, varId, &nDims)" << std::endl |
---|
[525] | 473 | << errormsg << std::endl |
---|
[686] | 474 | << "Unable to get the number of dimension of variable with Id: " << varId << std::endl; |
---|
[525] | 475 | StdString e = sstr.str(); |
---|
| 476 | throw CNetCdfException(e); |
---|
| 477 | } |
---|
[498] | 478 | |
---|
[525] | 479 | return status; |
---|
| 480 | } |
---|
[498] | 481 | |
---|
[525] | 482 | /*! |
---|
| 483 | This function makes a request to netcdf, returns a list of dimension ID describing the shape of the variable, given its id |
---|
| 484 | \param [in] ncid Groupd id (or File Id) |
---|
| 485 | \param [in] varId Id of variable |
---|
| 486 | \param [in/out] dimIds list of dimension of the variable |
---|
| 487 | \return Status code |
---|
| 488 | */ |
---|
| 489 | int CNetCdfInterface::inqVarDimId(int ncid, int varId, int* dimIds) |
---|
| 490 | { |
---|
[1601] | 491 | int status; |
---|
| 492 | #pragma omp critical (_netcdf) |
---|
| 493 | { |
---|
[1646] | 494 | info(200)<<"start nc_inq_vardimid"<<std::endl; |
---|
[1601] | 495 | status = nc_inq_vardimid(ncid, varId, dimIds); |
---|
[1646] | 496 | info(200)<<"end nc_inq_vardimid"<<std::endl; |
---|
[1601] | 497 | } |
---|
[525] | 498 | if (NC_NOERR != status) |
---|
| 499 | { |
---|
| 500 | StdString errormsg(nc_strerror(status)); |
---|
| 501 | StdStringStream sstr; |
---|
[498] | 502 | |
---|
[686] | 503 | sstr << "Error when calling function nc_inq_vardimid(ncid, varId, dimIds)" << std::endl |
---|
[525] | 504 | << errormsg << std::endl |
---|
| 505 | << "Unable to get list of dimension id of the variable with id " << varId << std::endl; |
---|
| 506 | StdString e = sstr.str(); |
---|
| 507 | throw CNetCdfException(e); |
---|
| 508 | } |
---|
[498] | 509 | |
---|
[525] | 510 | return status; |
---|
| 511 | } |
---|
[498] | 512 | |
---|
[525] | 513 | /*! |
---|
| 514 | This function makes a request to netcdf, to find all dimension in a group |
---|
| 515 | \param [in] ncid Groupd id (or File Id) |
---|
| 516 | \param [in/out] nDims number of list of dimension |
---|
| 517 | \param [in/out] dimIds list of dimension in a group or any of its parent |
---|
| 518 | \param [in] includeParents number of parents |
---|
| 519 | \return Status code |
---|
| 520 | */ |
---|
| 521 | int CNetCdfInterface::inqDimIds(int ncid, int& nDims, int* dimIds, int includeParents) |
---|
| 522 | { |
---|
[1601] | 523 | int status; |
---|
| 524 | #pragma omp critical (_netcdf) |
---|
| 525 | { |
---|
[1646] | 526 | info(200)<<"start nc_inq_dimids"<<std::endl; |
---|
[1601] | 527 | status = nc_inq_dimids(ncid, &nDims, dimIds, includeParents); |
---|
[1646] | 528 | info(200)<<"end nc_inq_dimids"<<std::endl; |
---|
[1601] | 529 | } |
---|
[525] | 530 | if (NC_NOERR != status) |
---|
| 531 | { |
---|
| 532 | StdString errormsg(nc_strerror(status)); |
---|
| 533 | StdStringStream sstr; |
---|
[498] | 534 | |
---|
[686] | 535 | sstr << "Error when calling function nc_inq_dimids(ncid, &nDims, dimIds, includeParents)" << std::endl; |
---|
[525] | 536 | sstr << errormsg << std::endl; |
---|
[686] | 537 | sstr << "Unable to retrieve number of dimension in the group with id: " << ncid << std::endl; |
---|
[525] | 538 | sstr << "With number of Parents " << includeParents << std::endl; |
---|
| 539 | StdString e = sstr.str(); |
---|
| 540 | throw CNetCdfException(e); |
---|
| 541 | } |
---|
[498] | 542 | |
---|
[525] | 543 | return status; |
---|
| 544 | } |
---|
[498] | 545 | |
---|
[525] | 546 | /*! |
---|
[686] | 547 | This function queries the full name of a group given its id. |
---|
| 548 | \param [in] ncid Groupd id (or File Id) |
---|
| 549 | \param [in/out] grpFullName the full name of the group |
---|
| 550 | \return Status code |
---|
| 551 | */ |
---|
| 552 | int CNetCdfInterface::inqGrpFullName(int ncid, StdString& grpFullName) |
---|
| 553 | { |
---|
| 554 | StdSize strlen = 0; |
---|
| 555 | std::vector<char> buff; |
---|
[1601] | 556 | int status; |
---|
| 557 | #pragma omp critical (_netcdf) |
---|
[686] | 558 | { |
---|
[1646] | 559 | info(200)<<"start nc_inq_grpname_full"<<std::endl; |
---|
[1601] | 560 | status = nc_inq_grpname_full(ncid, &strlen, NULL); |
---|
[1646] | 561 | info(200)<<"end nc_inq_grpname_full"<<std::endl; |
---|
[1601] | 562 | |
---|
| 563 | if (NC_NOERR == status) |
---|
| 564 | { |
---|
| 565 | buff.resize(strlen + 1); |
---|
| 566 | status = nc_inq_grpname_full(ncid, NULL, &buff[0]); |
---|
| 567 | } |
---|
[1646] | 568 | info(200)<<"start nc_inq_grpname_full"<<std::endl; |
---|
[686] | 569 | } |
---|
[1661] | 570 | |
---|
[686] | 571 | if (NC_NOERR != status) |
---|
| 572 | { |
---|
| 573 | StdString errormsg(nc_strerror(status)); |
---|
| 574 | StdStringStream sstr; |
---|
| 575 | |
---|
| 576 | sstr << "Error when calling function nc_inq_grpname_full(ncid, &strlen, &buff[0])" << std::endl |
---|
| 577 | << errormsg << std::endl |
---|
| 578 | << "Unable to get the full group name given its id: " << ncid << std::endl; |
---|
| 579 | StdString e = sstr.str(); |
---|
| 580 | throw CNetCdfException(e); |
---|
| 581 | } |
---|
| 582 | |
---|
| 583 | grpFullName.assign(buff.begin(), buff.end()); |
---|
| 584 | |
---|
| 585 | return status; |
---|
| 586 | } |
---|
| 587 | |
---|
| 588 | /*! |
---|
| 589 | This function queries the list of group ids given a location id. |
---|
| 590 | \param [in] ncid Groupd id (or File Id) |
---|
| 591 | \param [in/out] numgrps number of groups |
---|
| 592 | \param [in/out] ncids list of group ids |
---|
| 593 | \return Status code |
---|
| 594 | */ |
---|
| 595 | int CNetCdfInterface::inqGrpIds(int ncid, int& numgrps, int* ncids) |
---|
| 596 | { |
---|
[1601] | 597 | int status; |
---|
| 598 | #pragma omp critical (_netcdf) |
---|
| 599 | { |
---|
[1646] | 600 | info(200)<<"start nc_inq_grps"<<std::endl; |
---|
[1601] | 601 | status = nc_inq_grps(ncid, &numgrps, ncids); |
---|
[1646] | 602 | info(200)<<"end nc_inq_grps"<<std::endl; |
---|
[1601] | 603 | } |
---|
[686] | 604 | if (NC_NOERR != status) |
---|
| 605 | { |
---|
| 606 | StdString errormsg(nc_strerror(status)); |
---|
| 607 | StdStringStream sstr; |
---|
| 608 | |
---|
| 609 | sstr << "Error when calling function nc_inq_grps(ncid, &numgrps, ncids)" << std::endl; |
---|
| 610 | sstr << errormsg << std::endl; |
---|
| 611 | sstr << "Unable to retrieve the list of groups for location id: " << ncid << std::endl; |
---|
| 612 | StdString e = sstr.str(); |
---|
| 613 | throw CNetCdfException(e); |
---|
| 614 | } |
---|
| 615 | |
---|
| 616 | return status; |
---|
| 617 | } |
---|
| 618 | |
---|
| 619 | /*! |
---|
| 620 | This function queries the list of variable ids given a location id. |
---|
| 621 | \param [in] ncid Groupd id (or File Id) |
---|
| 622 | \param [in/out] nvars number of variables |
---|
| 623 | \param [in/out] varids list of variable ids |
---|
| 624 | \return Status code |
---|
| 625 | */ |
---|
| 626 | int CNetCdfInterface::inqVarIds(int ncid, int& nvars, int* varids) |
---|
| 627 | { |
---|
[1601] | 628 | int status; |
---|
| 629 | #pragma omp critical (_netcdf) |
---|
| 630 | { |
---|
[1646] | 631 | info(200)<<"start nc_inq_varids"<<std::endl; |
---|
[1601] | 632 | status = nc_inq_varids(ncid, &nvars, varids); |
---|
[1646] | 633 | info(200)<<"end nc_inq_varids"<<std::endl; |
---|
[1601] | 634 | } |
---|
[686] | 635 | if (NC_NOERR != status) |
---|
| 636 | { |
---|
| 637 | StdString errormsg(nc_strerror(status)); |
---|
| 638 | StdStringStream sstr; |
---|
| 639 | |
---|
| 640 | sstr << "Error when calling function nc_inq_varids(ncid, &nvars, varids)" << std::endl; |
---|
| 641 | sstr << errormsg << std::endl; |
---|
| 642 | sstr << "Unable to retrieve the list of variables for location id: " << ncid << std::endl; |
---|
| 643 | StdString e = sstr.str(); |
---|
| 644 | throw CNetCdfException(e); |
---|
| 645 | } |
---|
| 646 | |
---|
| 647 | return status; |
---|
| 648 | } |
---|
| 649 | |
---|
| 650 | /*! |
---|
| 651 | This function queries the type and the size of an attribute given its name and the id of the variable to which it is attached. |
---|
| 652 | \param [in] ncid Groupd id (or File Id) |
---|
| 653 | \param [in] varid the id of the variable to which the attribute is attached |
---|
| 654 | \param [in] name the name of the attribute |
---|
| 655 | \param [out] type the type of the attribute |
---|
| 656 | \param [out] len the size of the attribute |
---|
| 657 | \return Status code |
---|
| 658 | */ |
---|
| 659 | int CNetCdfInterface::inqAtt(int ncid, int varid, const StdString& name, nc_type& type, size_t& len) |
---|
| 660 | { |
---|
[1601] | 661 | int status; |
---|
| 662 | #pragma omp critical (_netcdf) |
---|
| 663 | { |
---|
[1646] | 664 | info(200)<<"start nc_inq_att"<<std::endl; |
---|
[1601] | 665 | status = nc_inq_att(ncid, varid, name.c_str(), &type, &len); |
---|
[1646] | 666 | info(200)<<"end nc_inq_att"<<std::endl; |
---|
[1601] | 667 | } |
---|
| 668 | |
---|
[686] | 669 | if (NC_NOERR != status) |
---|
| 670 | { |
---|
| 671 | StdString errormsg(nc_strerror(status)); |
---|
| 672 | StdStringStream sstr; |
---|
| 673 | |
---|
| 674 | sstr << "Error when calling function nc_inq_att(ncid, varid, name.c_str(), &type, &len)" << std::endl; |
---|
| 675 | sstr << errormsg << std::endl; |
---|
| 676 | sstr << "Unable to query the attribute information given its name: " << name << " and its variable id:" << varid << std::endl; |
---|
| 677 | StdString e = sstr.str(); |
---|
| 678 | throw CNetCdfException(e); |
---|
| 679 | } |
---|
| 680 | |
---|
| 681 | return status; |
---|
| 682 | } |
---|
| 683 | |
---|
| 684 | /*! |
---|
| 685 | This function queries the number of global attributes given a location id. |
---|
| 686 | \param [in] ncid Groupd id (or File Id) |
---|
| 687 | \param [out] ngatts the number of global attributes |
---|
| 688 | \return Status code |
---|
| 689 | */ |
---|
| 690 | int CNetCdfInterface::inqNAtts(int ncid, int& ngatts) |
---|
| 691 | { |
---|
[1601] | 692 | int status; |
---|
| 693 | #pragma omp critical (_netcdf) |
---|
| 694 | { |
---|
[1646] | 695 | info(200)<<"start nc_inq_natts"<<std::endl; |
---|
[1601] | 696 | status = nc_inq_natts(ncid, &ngatts); |
---|
[1646] | 697 | info(200)<<"end nc_inq_natts"<<std::endl; |
---|
[1601] | 698 | } |
---|
[686] | 699 | if (NC_NOERR != status) |
---|
| 700 | { |
---|
| 701 | StdString errormsg(nc_strerror(status)); |
---|
| 702 | StdStringStream sstr; |
---|
| 703 | |
---|
| 704 | sstr << "Error when calling function nc_inq_natts(ncid, &ngatts)" << std::endl; |
---|
| 705 | sstr << errormsg << std::endl; |
---|
| 706 | sstr << "Unable to query the number of global attributes given the location id:" << ncid << std::endl; |
---|
| 707 | StdString e = sstr.str(); |
---|
| 708 | throw CNetCdfException(e); |
---|
| 709 | } |
---|
| 710 | |
---|
| 711 | return status; |
---|
| 712 | } |
---|
| 713 | |
---|
| 714 | /*! |
---|
| 715 | This function queries the number of global attributes given a location id and a variable id. |
---|
| 716 | \param [in] ncid Groupd id (or File Id) |
---|
| 717 | \param [in] varid the id of the variable |
---|
| 718 | \param [out] natts the number of global attributes |
---|
| 719 | \return Status code |
---|
| 720 | */ |
---|
| 721 | int CNetCdfInterface::inqVarNAtts(int ncid, int varid, int& natts) |
---|
| 722 | { |
---|
[1601] | 723 | int status; |
---|
| 724 | #pragma omp critical (_netcdf) |
---|
| 725 | { |
---|
[1646] | 726 | info(200)<<"start nc_inq_varnatts"<<std::endl; |
---|
[1601] | 727 | status = nc_inq_varnatts(ncid, varid, &natts); |
---|
[1646] | 728 | info(200)<<"end nc_inq_varnatts"<<std::endl; |
---|
[1601] | 729 | } |
---|
[686] | 730 | if (NC_NOERR != status) |
---|
| 731 | { |
---|
| 732 | StdString errormsg(nc_strerror(status)); |
---|
| 733 | StdStringStream sstr; |
---|
| 734 | |
---|
| 735 | sstr << "Error when calling function nc_inq_varnatts(ncid, varid, &natts)" << std::endl; |
---|
| 736 | sstr << errormsg << std::endl; |
---|
| 737 | sstr << "Unable to query the number of attributes given the location id:" << ncid << " and the variable id:" << varid << std::endl; |
---|
| 738 | StdString e = sstr.str(); |
---|
| 739 | throw CNetCdfException(e); |
---|
| 740 | } |
---|
| 741 | |
---|
| 742 | return status; |
---|
| 743 | } |
---|
| 744 | |
---|
| 745 | |
---|
| 746 | //! Query the name of an attribute given a location id, a variable id and the attribute number |
---|
| 747 | int CNetCdfInterface::inqAttName(int ncid, int varid, int attnum, StdString& name) |
---|
| 748 | { |
---|
[782] | 749 | std::vector<char> attName(NC_MAX_NAME + 1,' '); |
---|
[1601] | 750 | int status; |
---|
| 751 | #pragma omp critical (_netcdf) |
---|
| 752 | { |
---|
[1646] | 753 | info(200)<<"start nc_inq_attname"<<std::endl; |
---|
[1601] | 754 | status = nc_inq_attname(ncid, varid, attnum, &attName[0]); |
---|
[1646] | 755 | info(200)<<"end nc_inq_attname"<<std::endl; |
---|
[1601] | 756 | } |
---|
[686] | 757 | if (NC_NOERR != status) |
---|
| 758 | { |
---|
| 759 | StdString errormsg(nc_strerror(status)); |
---|
| 760 | StdStringStream sstr; |
---|
| 761 | |
---|
| 762 | sstr << "Error when calling function nc_inq_attname(ncid, varid, attnum, attName)" << std::endl; |
---|
| 763 | sstr << errormsg << std::endl; |
---|
[833] | 764 | sstr << "Unable to query the name: " << name << " of attribute " << attnum << " given the location id:" << ncid << " and the variable id:" << varid << std::endl; |
---|
[686] | 765 | StdString e = sstr.str(); |
---|
| 766 | throw CNetCdfException(e); |
---|
| 767 | } |
---|
| 768 | |
---|
[782] | 769 | int nameSize = 0; |
---|
| 770 | while ((nameSize < NC_MAX_NAME) && (' ' != attName[nameSize] )) ++nameSize; |
---|
| 771 | name.resize(nameSize); |
---|
| 772 | // for (int idx = 0; idx < nameSize; ++idx) name.at(idx) = attName[idx]; |
---|
| 773 | std::copy(&attName[0], &attName[nameSize-1], name.begin()); |
---|
| 774 | |
---|
[686] | 775 | return status; |
---|
| 776 | } |
---|
| 777 | |
---|
| 778 | /*! |
---|
[525] | 779 | This function makes a request to netcdf with a id of a prent groupd and then return id of the created group, given its name |
---|
| 780 | \param [in] parentNcid Id of parent groupd(or File Id) |
---|
| 781 | \param [in] grpName Name of the desired group |
---|
| 782 | \param [in/out] grpId Group id if this group is created sucessfully |
---|
| 783 | \return Status code |
---|
| 784 | */ |
---|
| 785 | int CNetCdfInterface::defGrp(int parentNcid, const StdString& grpName, int& grpId) |
---|
| 786 | { |
---|
[1601] | 787 | int status; |
---|
| 788 | #pragma omp critical (_netcdf) |
---|
| 789 | { |
---|
[1646] | 790 | info(200)<<"start nc_def_grp"<<std::endl; |
---|
[1601] | 791 | status = nc_def_grp(parentNcid, grpName.c_str(), &grpId); |
---|
[1646] | 792 | info(200)<<"end nc_def_grp"<<std::endl; |
---|
[1601] | 793 | } |
---|
[525] | 794 | if (NC_NOERR != status) |
---|
| 795 | { |
---|
| 796 | StdString errormsg(nc_strerror(status)); |
---|
| 797 | StdStringStream sstr; |
---|
[498] | 798 | |
---|
[686] | 799 | sstr << "Error when calling function nc_def_grp(parentNcid, grpName.c_str(), &grpId)" << std::endl; |
---|
[525] | 800 | sstr << errormsg << std::endl; |
---|
[686] | 801 | sstr << "Unable to create group Id, given its name: " << grpName << std::endl; |
---|
[525] | 802 | StdString e = sstr.str(); |
---|
| 803 | throw CNetCdfException(e); |
---|
| 804 | } |
---|
[498] | 805 | |
---|
[525] | 806 | return status; |
---|
| 807 | } |
---|
[498] | 808 | |
---|
[525] | 809 | /*! |
---|
| 810 | This function makes a request to netcdf, add a new dimension to an open netcdf in define mode |
---|
| 811 | \param [in] ncid Id of groupd(or File Id) |
---|
| 812 | \param [in] dimName Name of the desired dimension |
---|
| 813 | \param [in/out] grpId Group id if this group is created sucessfully |
---|
| 814 | \return Status code |
---|
| 815 | */ |
---|
| 816 | int CNetCdfInterface::defDim(int ncid, const StdString& dimName, StdSize dimLen, int& dimId) |
---|
| 817 | { |
---|
[1601] | 818 | int status; |
---|
| 819 | #pragma omp critical (_netcdf) |
---|
| 820 | { |
---|
[1646] | 821 | info(200)<<"start nc_def_dim"<<std::endl; |
---|
[1601] | 822 | status = nc_def_dim(ncid, dimName.c_str(), dimLen, &dimId); |
---|
[1646] | 823 | info(200)<<"end nc_def_dim"<<std::endl; |
---|
[1601] | 824 | } |
---|
[525] | 825 | if (NC_NOERR != status) |
---|
| 826 | { |
---|
| 827 | StdString errormsg(nc_strerror(status)); |
---|
| 828 | StdStringStream sstr; |
---|
[498] | 829 | |
---|
[686] | 830 | sstr << "Error when calling function nc_def_dim(ncid, dimName.c_str(), dimLen, &dimId)" << std::endl; |
---|
[525] | 831 | sstr << errormsg << std::endl; |
---|
[686] | 832 | sstr << "Unable to create dimension with name: " << dimName |
---|
[525] | 833 | << " and with length " << dimLen << std::endl; |
---|
| 834 | StdString e = sstr.str(); |
---|
| 835 | throw CNetCdfException(e); |
---|
| 836 | } |
---|
[498] | 837 | |
---|
[525] | 838 | return status; |
---|
| 839 | } |
---|
[498] | 840 | |
---|
[525] | 841 | /*! |
---|
| 842 | This function makes a request to netcdf with its id, to add a new variable to an open netCdf in define mode, |
---|
| 843 | return a variable id, given its name, type, the number of dimensions and list of dimension id |
---|
| 844 | \param [in] ncid Id of groupd(or File Id) |
---|
| 845 | \param [in] varName Name of the desired dimension |
---|
| 846 | \param [in] xtypes One of the set of predefined netCDF data types |
---|
| 847 | \param [in] nDims Number of dimension for the variable |
---|
| 848 | \param [in] dimIds List of ndims dimension ids corresponding to the variable dimensions |
---|
| 849 | \param [in/out] varId Variable id if it is added sucessfully |
---|
| 850 | \return Status code |
---|
| 851 | */ |
---|
[686] | 852 | int CNetCdfInterface::defVar(int ncid, const StdString& varName, nc_type xtype, |
---|
| 853 | int nDims, const int dimIds[], int& varId) |
---|
[525] | 854 | { |
---|
[1601] | 855 | int status; |
---|
| 856 | #pragma omp critical (_netcdf) |
---|
| 857 | { |
---|
[1646] | 858 | info(200)<<"start nc_def_var"<<std::endl; |
---|
[1601] | 859 | status = nc_def_var(ncid, varName.c_str(), xtype, nDims, dimIds, &varId); |
---|
[1646] | 860 | info(200)<<"end nc_def_var"<<std::endl; |
---|
[1601] | 861 | } |
---|
[525] | 862 | if (NC_NOERR != status) |
---|
| 863 | { |
---|
| 864 | StdString errormsg(nc_strerror(status)); |
---|
| 865 | StdStringStream sstr; |
---|
[498] | 866 | |
---|
[686] | 867 | sstr << "Error when calling function nc_def_var(ncid, varName.c_str(), xtype, nDims, dimIds, &varId)" << std::endl; |
---|
[525] | 868 | sstr << errormsg << std::endl; |
---|
[686] | 869 | sstr << "Unable to add a new variable with name: " << varName |
---|
[525] | 870 | << " with type " << xtype |
---|
| 871 | << " and number of dimension " << nDims << std::endl; |
---|
| 872 | StdString e = sstr.str(); |
---|
| 873 | throw CNetCdfException(e); |
---|
| 874 | } |
---|
[498] | 875 | |
---|
[525] | 876 | return status; |
---|
| 877 | } |
---|
[498] | 878 | |
---|
[525] | 879 | /*! |
---|
| 880 | This function makes a request to netcdf with a ncid, to set the chunking size of a variable, |
---|
| 881 | given variable id and type of storage |
---|
| 882 | \param [in] ncid Id groupd(or File Id) |
---|
| 883 | \param [in] varId Id of the variable |
---|
[686] | 884 | \param [in] storage Type of storage (It can be: NC_CONTIGUOUS, NC_CHUNKED) |
---|
[525] | 885 | \param [in/out] chunkSize array list of chunk sizes |
---|
| 886 | \return Status code |
---|
| 887 | */ |
---|
| 888 | int CNetCdfInterface::defVarChunking(int ncid, int varId, int storage, StdSize chunkSize[]) |
---|
| 889 | { |
---|
[1601] | 890 | int status; |
---|
| 891 | #pragma omp critical (_netcdf) |
---|
| 892 | { |
---|
[1646] | 893 | info(200)<<"start nc_def_var_chunking"<<std::endl; |
---|
[1601] | 894 | status = nc_def_var_chunking(ncid, varId, storage, chunkSize); |
---|
[1646] | 895 | info(200)<<"end nc_def_var_chunking"<<std::endl; |
---|
[1601] | 896 | } |
---|
[525] | 897 | if (NC_NOERR != status) |
---|
[517] | 898 | { |
---|
[525] | 899 | StdString errormsg(nc_strerror(status)); |
---|
| 900 | StdStringStream sstr; |
---|
[517] | 901 | |
---|
[686] | 902 | sstr << "Error when calling function nc_def_var_chunking(ncid, varId, storage, chunkSize)" << std::endl; |
---|
[525] | 903 | sstr << errormsg << std::endl; |
---|
[686] | 904 | sstr << "Unable to set chunk size of the variable with id: " << varId |
---|
[525] | 905 | << " and storage type " << storage << std::endl; |
---|
| 906 | StdString e = sstr.str(); |
---|
| 907 | throw CNetCdfException(e); |
---|
[517] | 908 | } |
---|
| 909 | |
---|
[525] | 910 | return status; |
---|
| 911 | } |
---|
[498] | 912 | |
---|
[525] | 913 | /*! |
---|
[606] | 914 | This function sets the compression level to the specified variable |
---|
| 915 | \param [in] ncid Groud id (or file id) |
---|
| 916 | \param [in] varId Id of the variable |
---|
| 917 | \param [in] compressionLevel The compression level from 0 to 9 (0 disables the compression, 9 is the higher compression) |
---|
| 918 | \return Status code |
---|
| 919 | */ |
---|
| 920 | int CNetCdfInterface::defVarDeflate(int ncid, int varId, int compressionLevel) |
---|
| 921 | { |
---|
[972] | 922 | |
---|
| 923 | if (compressionLevel == 0) return NC_NOERR ; |
---|
[1601] | 924 | int status; |
---|
| 925 | #pragma omp critical (_netcdf) |
---|
| 926 | { |
---|
[1646] | 927 | info(200)<<"start nc_def_var_deflate"<<std::endl; |
---|
[1601] | 928 | status = nc_def_var_deflate(ncid, varId, (compressionLevel > 0), (compressionLevel > 0), compressionLevel); |
---|
[1646] | 929 | info(200)<<"end nc_def_var_deflate"<<std::endl; |
---|
[1601] | 930 | } |
---|
[606] | 931 | if (NC_NOERR != status) |
---|
| 932 | { |
---|
| 933 | StdString errormsg(nc_strerror(status)); |
---|
| 934 | StdStringStream sstr; |
---|
| 935 | |
---|
[686] | 936 | sstr << "Error when calling function nc_def_var_deflate(ncid, varId, false, (compressionLevel > 0), compressionLevel)" << std::endl; |
---|
[606] | 937 | sstr << errormsg << std::endl; |
---|
| 938 | sstr << "Unable to set the compression level of the variable with id: " << varId |
---|
| 939 | << " and compression level: " << compressionLevel << std::endl; |
---|
| 940 | StdString e = sstr.str(); |
---|
| 941 | throw CNetCdfException(e); |
---|
| 942 | } |
---|
| 943 | |
---|
| 944 | return status; |
---|
| 945 | } |
---|
| 946 | |
---|
| 947 | /*! |
---|
[525] | 948 | Set or unset the fill mode for a NetCDF file specified by its file id. |
---|
| 949 | \param [in] ncid File id |
---|
| 950 | \param [in] fill Define whether the fill mode should be enabled or not |
---|
| 951 | \return Status code |
---|
| 952 | */ |
---|
| 953 | int CNetCdfInterface::setFill(int ncid, bool fill) |
---|
| 954 | { |
---|
| 955 | int old_fill_mode; |
---|
[1601] | 956 | int status; |
---|
| 957 | #pragma omp critical (_netcdf) |
---|
| 958 | { |
---|
[1646] | 959 | info(200)<<"start nc_set_fill"<<std::endl; |
---|
[1601] | 960 | status = nc_set_fill(ncid, fill ? NC_FILL: NC_NOFILL, &old_fill_mode); |
---|
[1646] | 961 | info(200)<<"end nc_set_fill"<<std::endl; |
---|
[1601] | 962 | } |
---|
[525] | 963 | if (NC_NOERR != status) |
---|
| 964 | { |
---|
| 965 | StdString errormsg(nc_strerror(status)); |
---|
| 966 | StdStringStream sstr; |
---|
[498] | 967 | |
---|
[686] | 968 | sstr << "Error when calling function nc_set_fill(ncid, fill ? NC_FILL: NC_NOFILL, &old_fill_mode)" << std::endl; |
---|
[525] | 969 | sstr << errormsg << std::endl; |
---|
[686] | 970 | sstr << "Unable to set the fill mode to: " << (fill ? "NC_FILL": "NC_NOFILL") << std::endl; |
---|
[525] | 971 | StdString e = sstr.str(); |
---|
| 972 | throw CNetCdfException(e); |
---|
| 973 | } |
---|
[498] | 974 | |
---|
[525] | 975 | return status; |
---|
| 976 | } |
---|
[498] | 977 | |
---|
[525] | 978 | /*! |
---|
| 979 | This function makes a request to netcdf with a ncid, to set the fill parameters for a variable, |
---|
| 980 | given variable id and type of fill |
---|
| 981 | \param [in] ncid Id groupd(or File Id) |
---|
| 982 | \param [in] varId Id of the variable |
---|
| 983 | \param [in] noFill turn on/off nofill mode on a variable |
---|
| 984 | \param [in/out] fillValue |
---|
| 985 | \return Status code |
---|
| 986 | */ |
---|
| 987 | int CNetCdfInterface::defVarFill(int ncid, int varId, int noFill, void* fillValue) |
---|
| 988 | { |
---|
[1601] | 989 | int status; |
---|
| 990 | #pragma omp critical (_netcdf) |
---|
| 991 | { |
---|
[1646] | 992 | info(200)<<"start nc_def_var_fill"<<std::endl; |
---|
[1601] | 993 | status = nc_def_var_fill(ncid, varId, noFill, fillValue); |
---|
[1646] | 994 | info(200)<<"end nc_def_var_fill"<<std::endl; |
---|
[1601] | 995 | } |
---|
[525] | 996 | if (NC_NOERR != status) |
---|
| 997 | { |
---|
| 998 | StdString errormsg(nc_strerror(status)); |
---|
| 999 | StdStringStream sstr; |
---|
[498] | 1000 | |
---|
[686] | 1001 | sstr << "Error when calling function nc_def_var_fill(ncid, varId, noFill, fillValue)" << std::endl; |
---|
[525] | 1002 | sstr << errormsg << std::endl; |
---|
[686] | 1003 | sstr << "Unable to set fill parameters of the variable with id: " << varId |
---|
[525] | 1004 | << " and fill option " << noFill << std::endl; |
---|
| 1005 | StdString e = sstr.str(); |
---|
| 1006 | throw CNetCdfException(e); |
---|
| 1007 | } |
---|
[498] | 1008 | |
---|
[525] | 1009 | return status; |
---|
| 1010 | } |
---|
[498] | 1011 | |
---|
[525] | 1012 | /*! |
---|
| 1013 | This function makes a request to netcdf with a ncid, to change the way read/write operations are performed |
---|
| 1014 | collectively or independently on the variable. |
---|
| 1015 | \param [in] ncid Id groupd(or File Id) |
---|
| 1016 | \param [in] varId Id of the variable |
---|
| 1017 | \param [in] noFill turn on/off nofill mode on a variable |
---|
| 1018 | \param [in/out] fillValue |
---|
| 1019 | \return Status code |
---|
| 1020 | */ |
---|
| 1021 | int CNetCdfInterface::varParAccess(int ncid, int varId, int access) |
---|
| 1022 | { |
---|
[1601] | 1023 | int status; |
---|
| 1024 | #pragma omp critical (_netcdf) |
---|
| 1025 | { |
---|
[1646] | 1026 | info(200)<<"start nc_var_par_access"<<std::endl; |
---|
[1601] | 1027 | status = nc_var_par_access(ncid, varId, access); |
---|
[1646] | 1028 | info(200)<<"end nc_var_par_access"<<std::endl; |
---|
[1601] | 1029 | } |
---|
[525] | 1030 | if (NC_NOERR != status) |
---|
| 1031 | { |
---|
| 1032 | StdString errormsg(nc_strerror(status)); |
---|
| 1033 | StdStringStream sstr; |
---|
[498] | 1034 | |
---|
[686] | 1035 | sstr << "Error when calling function nc_var_par_access(ncid, varId, access)" << std::endl; |
---|
[525] | 1036 | sstr << errormsg << std::endl; |
---|
[686] | 1037 | sstr << "Unable to change read/write option of the variable with id: " << varId << std::endl; |
---|
[525] | 1038 | StdString e = sstr.str(); |
---|
| 1039 | throw CNetCdfException(e); |
---|
| 1040 | } |
---|
[498] | 1041 | |
---|
[525] | 1042 | return status; |
---|
| 1043 | } |
---|
[498] | 1044 | |
---|
[525] | 1045 | /*! |
---|
| 1046 | This function makes a synchronisation of the disk copy of a netCDF dataset. |
---|
| 1047 | \param [in] ncid Id groupd(or File Id) |
---|
| 1048 | \return Status code |
---|
| 1049 | */ |
---|
| 1050 | int CNetCdfInterface::sync(int ncid) |
---|
| 1051 | { |
---|
[1601] | 1052 | int status; |
---|
| 1053 | #pragma omp critical (_netcdf) |
---|
| 1054 | { |
---|
[1646] | 1055 | info(200)<<"start nc_sync"<<std::endl; |
---|
[1601] | 1056 | status = nc_sync(ncid); |
---|
[1646] | 1057 | info(200)<<"end nc_sync"<<std::endl; |
---|
[1601] | 1058 | } |
---|
[525] | 1059 | if (NC_NOERR != status) |
---|
[498] | 1060 | { |
---|
[525] | 1061 | StdString errormsg(nc_strerror(status)); |
---|
| 1062 | StdStringStream sstr; |
---|
[498] | 1063 | |
---|
[686] | 1064 | sstr << "Error when calling function nc_sync(ncid)" << std::endl; |
---|
[525] | 1065 | sstr << errormsg << std::endl; |
---|
[686] | 1066 | sstr << "Unable to make a synchronization of a netCDF file with id: " << ncid << std::endl; |
---|
[525] | 1067 | StdString e = sstr.str(); |
---|
| 1068 | throw CNetCdfException(e); |
---|
[498] | 1069 | } |
---|
| 1070 | |
---|
[525] | 1071 | return status; |
---|
| 1072 | } |
---|
[498] | 1073 | |
---|
[686] | 1074 | // Some specializations of getAttributeType |
---|
| 1075 | template<> |
---|
| 1076 | int CNetCdfInterface::ncGetAttType(int ncid, int varid, const char* attrName, double* data) |
---|
[525] | 1077 | { |
---|
[1601] | 1078 | int status; |
---|
| 1079 | #pragma omp critical (_netcdf) |
---|
| 1080 | { |
---|
[1646] | 1081 | info(200)<<"start nc_get_att_double"<<std::endl; |
---|
[1601] | 1082 | status = nc_get_att_double(ncid, varid, attrName, data); |
---|
[1646] | 1083 | info(200)<<"end nc_get_att_double"<<std::endl; |
---|
[1601] | 1084 | } |
---|
| 1085 | return status; |
---|
[686] | 1086 | } |
---|
[498] | 1087 | |
---|
[686] | 1088 | template<> |
---|
| 1089 | int CNetCdfInterface::ncGetAttType(int ncid, int varid, const char* attrName, float* data) |
---|
| 1090 | { |
---|
[1601] | 1091 | int status; |
---|
| 1092 | #pragma omp critical (_netcdf) |
---|
| 1093 | { |
---|
[1646] | 1094 | info(200)<<"start nc_get_att_float"<<std::endl; |
---|
[1601] | 1095 | status = nc_get_att_float(ncid, varid, attrName, data); |
---|
[1646] | 1096 | info(200)<<"end nc_get_att_float"<<std::endl; |
---|
[1601] | 1097 | } |
---|
| 1098 | return status; |
---|
[686] | 1099 | } |
---|
[498] | 1100 | |
---|
[686] | 1101 | template<> |
---|
| 1102 | int CNetCdfInterface::ncGetAttType(int ncid, int varid, const char* attrName, int* data) |
---|
| 1103 | { |
---|
[1601] | 1104 | int status; |
---|
| 1105 | #pragma omp critical (_netcdf) |
---|
| 1106 | { |
---|
[1646] | 1107 | info(200)<<"start nc_get_att_int"<<std::endl; |
---|
[1601] | 1108 | status = nc_get_att_int(ncid, varid, attrName, data); |
---|
[1646] | 1109 | info(200)<<"end nc_get_att_int"<<std::endl; |
---|
[1601] | 1110 | } |
---|
| 1111 | return status; |
---|
[525] | 1112 | } |
---|
[498] | 1113 | |
---|
[525] | 1114 | template<> |
---|
[686] | 1115 | int CNetCdfInterface::ncGetAttType(int ncid, int varid, const char* attrName, long* data) |
---|
| 1116 | { |
---|
[1601] | 1117 | int status; |
---|
| 1118 | #pragma omp critical (_netcdf) |
---|
| 1119 | { |
---|
[1646] | 1120 | info(200)<<"start nc_get_att_long"<<std::endl; |
---|
[1601] | 1121 | status = nc_get_att_long(ncid, varid, attrName, data); |
---|
[1646] | 1122 | info(200)<<"end nc_get_att_long"<<std::endl; |
---|
[1601] | 1123 | } |
---|
| 1124 | return status; |
---|
[686] | 1125 | } |
---|
| 1126 | |
---|
| 1127 | template<> |
---|
| 1128 | int CNetCdfInterface::ncGetAttType(int ncid, int varid, const char* attrName, short* data) |
---|
| 1129 | { |
---|
[1601] | 1130 | int status; |
---|
| 1131 | #pragma omp critical (_netcdf) |
---|
| 1132 | { |
---|
[1646] | 1133 | info(200)<<"start nc_get_att_short"<<std::endl; |
---|
[1601] | 1134 | status = nc_get_att_short(ncid, varid, attrName, data); |
---|
[1646] | 1135 | info(200)<<"end nc_get_att_short"<<std::endl; |
---|
[1601] | 1136 | } |
---|
| 1137 | return status; |
---|
[686] | 1138 | } |
---|
| 1139 | |
---|
| 1140 | template<> |
---|
| 1141 | int CNetCdfInterface::ncGetAttType(int ncid, int varid, const char* attrName, char* data) |
---|
| 1142 | { |
---|
[1601] | 1143 | int status; |
---|
| 1144 | #pragma omp critical (_netcdf) |
---|
| 1145 | { |
---|
[1646] | 1146 | info(200)<<"start nc_get_att_text"<<std::endl; |
---|
[1601] | 1147 | status = nc_get_att_text(ncid, varid, attrName, data); |
---|
[1646] | 1148 | info(200)<<"end nc_get_att_text"<<std::endl; |
---|
[1601] | 1149 | } |
---|
| 1150 | return status; |
---|
[686] | 1151 | } |
---|
| 1152 | |
---|
| 1153 | // Some specializations of putAttributeType |
---|
| 1154 | template<> |
---|
[525] | 1155 | int CNetCdfInterface::ncPutAttType(int ncid, int varid, const char* attrName, |
---|
[686] | 1156 | StdSize numVal, const double* data) |
---|
[525] | 1157 | { |
---|
[1601] | 1158 | int status; |
---|
| 1159 | #pragma omp critical (_netcdf) |
---|
| 1160 | { |
---|
[1646] | 1161 | info(200)<<"start nc_put_att_double"<<std::endl; |
---|
[1601] | 1162 | status = nc_put_att_double(ncid, varid, attrName, NC_DOUBLE, numVal, data); |
---|
[1646] | 1163 | info(200)<<"end nc_put_att_double"<<std::endl; |
---|
[1601] | 1164 | } |
---|
| 1165 | return status; |
---|
[525] | 1166 | } |
---|
[498] | 1167 | |
---|
[525] | 1168 | template<> |
---|
| 1169 | int CNetCdfInterface::ncPutAttType(int ncid, int varid, const char* attrName, |
---|
[686] | 1170 | StdSize numVal, const float* data) |
---|
[525] | 1171 | { |
---|
[1601] | 1172 | int status; |
---|
| 1173 | #pragma omp critical (_netcdf) |
---|
| 1174 | { |
---|
[1646] | 1175 | info(200)<<"start nc_put_att_float"<<std::endl; |
---|
[1601] | 1176 | status = nc_put_att_float(ncid, varid, attrName, NC_FLOAT, numVal, data); |
---|
[1646] | 1177 | info(200)<<"end nc_put_att_float"<<std::endl; |
---|
[1601] | 1178 | } |
---|
| 1179 | return status; |
---|
[525] | 1180 | } |
---|
[498] | 1181 | |
---|
[525] | 1182 | template<> |
---|
| 1183 | int CNetCdfInterface::ncPutAttType(int ncid, int varid, const char* attrName, |
---|
[686] | 1184 | StdSize numVal, const int* data) |
---|
[525] | 1185 | { |
---|
[1601] | 1186 | int status; |
---|
| 1187 | #pragma omp critical (_netcdf) |
---|
| 1188 | { |
---|
[1646] | 1189 | info(200)<<"start nc_put_att_int"<<std::endl; |
---|
[1601] | 1190 | status = nc_put_att_int(ncid, varid, attrName, NC_INT, numVal, data); |
---|
[1646] | 1191 | info(200)<<"end nc_put_att_int"<<std::endl; |
---|
[1601] | 1192 | } |
---|
| 1193 | return status; |
---|
[525] | 1194 | } |
---|
[498] | 1195 | |
---|
[525] | 1196 | template<> |
---|
| 1197 | int CNetCdfInterface::ncPutAttType(int ncid, int varid, const char* attrName, |
---|
[686] | 1198 | StdSize numVal, const long* data) |
---|
[525] | 1199 | { |
---|
[1601] | 1200 | int status; |
---|
| 1201 | #pragma omp critical (_netcdf) |
---|
| 1202 | { |
---|
[1646] | 1203 | info(200)<<"start nc_put_att_long"<<std::endl; |
---|
[1601] | 1204 | status = nc_put_att_long(ncid, varid, attrName, NC_LONG, numVal, data); |
---|
[1646] | 1205 | info(200)<<"end nc_put_att_long"<<std::endl; |
---|
[1601] | 1206 | } |
---|
| 1207 | return status; |
---|
[525] | 1208 | } |
---|
[498] | 1209 | |
---|
[525] | 1210 | template<> |
---|
| 1211 | int CNetCdfInterface::ncPutAttType(int ncid, int varid, const char* attrName, |
---|
[686] | 1212 | StdSize numVal, const short* data) |
---|
[525] | 1213 | { |
---|
[1601] | 1214 | int status; |
---|
| 1215 | #pragma omp critical (_netcdf) |
---|
| 1216 | { |
---|
[1646] | 1217 | info(200)<<"start nc_put_att_short"<<std::endl; |
---|
[1601] | 1218 | status = nc_put_att_short(ncid, varid, attrName, NC_SHORT, numVal, data); |
---|
[1646] | 1219 | info(200)<<"end nc_put_att_short"<<std::endl; |
---|
[1601] | 1220 | } |
---|
| 1221 | return status; |
---|
[525] | 1222 | } |
---|
| 1223 | |
---|
[686] | 1224 | template<> |
---|
| 1225 | int CNetCdfInterface::ncPutAttType(int ncid, int varid, const char* attrName, |
---|
| 1226 | StdSize numVal, const char* data) |
---|
| 1227 | { |
---|
[1601] | 1228 | int status; |
---|
| 1229 | #pragma omp critical (_netcdf) |
---|
| 1230 | { |
---|
[1646] | 1231 | info(200)<<"start nc_put_att_text"<<std::endl; |
---|
[1601] | 1232 | status = nc_put_att_text(ncid, varid, attrName, numVal, data); |
---|
[1646] | 1233 | info(200)<<"end nc_put_att_text"<<std::endl; |
---|
[1601] | 1234 | } |
---|
| 1235 | return status; |
---|
[686] | 1236 | } |
---|
[525] | 1237 | |
---|
[686] | 1238 | // Some specializations of getVariableType |
---|
[525] | 1239 | template<> |
---|
[686] | 1240 | int CNetCdfInterface::ncGetVaraType(int ncid, int varid, const StdSize* start, const StdSize* count, double* data) |
---|
[525] | 1241 | { |
---|
[1601] | 1242 | int status; |
---|
| 1243 | #pragma omp critical (_netcdf) |
---|
| 1244 | { |
---|
[1646] | 1245 | info(200)<<"start nc_get_vara_double"<<std::endl; |
---|
[1601] | 1246 | status = nc_get_vara_double(ncid, varid, start, count, data); |
---|
[1646] | 1247 | info(200)<<"end nc_get_vara_double"<<std::endl; |
---|
[1601] | 1248 | } |
---|
| 1249 | return status; |
---|
[525] | 1250 | } |
---|
| 1251 | |
---|
| 1252 | template<> |
---|
[686] | 1253 | int CNetCdfInterface::ncGetVaraType(int ncid, int varid, const StdSize* start, const StdSize* count, float* data) |
---|
[525] | 1254 | { |
---|
[1601] | 1255 | int status; |
---|
| 1256 | #pragma omp critical (_netcdf) |
---|
| 1257 | { |
---|
[1646] | 1258 | info(200)<<"start nc_get_vara_float"<<std::endl; |
---|
[1601] | 1259 | status = nc_get_vara_float(ncid, varid, start, count, data); |
---|
[1646] | 1260 | info(200)<<"end nc_get_vara_float"<<std::endl; |
---|
[1601] | 1261 | } |
---|
| 1262 | return status; |
---|
[525] | 1263 | } |
---|
| 1264 | |
---|
| 1265 | template<> |
---|
[686] | 1266 | int CNetCdfInterface::ncGetVaraType(int ncid, int varid, const StdSize* start, const StdSize* count, int* data) |
---|
[525] | 1267 | { |
---|
[1601] | 1268 | int status; |
---|
| 1269 | #pragma omp critical (_netcdf) |
---|
| 1270 | { |
---|
[1646] | 1271 | info(200)<<"start nc_get_vara_int"<<std::endl; |
---|
[1601] | 1272 | status = nc_get_vara_int(ncid, varid, start, count, data); |
---|
[1646] | 1273 | info(200)<<"end nc_get_vara_int"<<std::endl; |
---|
[1601] | 1274 | } |
---|
| 1275 | return status; |
---|
[525] | 1276 | } |
---|
| 1277 | |
---|
[1158] | 1278 | template<> |
---|
| 1279 | int CNetCdfInterface::ncGetVaraType(int ncid, int varid, const StdSize* start, const StdSize* count, char* data) |
---|
| 1280 | { |
---|
[1601] | 1281 | int status; |
---|
| 1282 | #pragma omp critical (_netcdf) |
---|
| 1283 | { |
---|
[1646] | 1284 | info(200)<<"start nc_get_vara_text"<<std::endl; |
---|
[1601] | 1285 | status = nc_get_vara_text(ncid, varid, start, count, data); |
---|
[1646] | 1286 | info(200)<<"end nc_get_vara_text"<<std::endl; |
---|
[1601] | 1287 | } |
---|
| 1288 | return status; |
---|
[1158] | 1289 | } |
---|
| 1290 | |
---|
[686] | 1291 | // Some specializations of putVariableType |
---|
| 1292 | template<> |
---|
| 1293 | int CNetCdfInterface::ncPutVaraType(int ncid, int varid, const StdSize* start, const StdSize* count, const double* data) |
---|
| 1294 | { |
---|
[1601] | 1295 | int status; |
---|
| 1296 | #pragma omp critical (_netcdf) |
---|
| 1297 | { |
---|
[1646] | 1298 | info(200)<<"start nc_put_vara_double"<<std::endl; |
---|
[1601] | 1299 | status = nc_put_vara_double(ncid, varid, start, count, data); |
---|
[1646] | 1300 | info(200)<<"end nc_put_vara_double"<<std::endl; |
---|
[1601] | 1301 | } |
---|
| 1302 | return status; |
---|
[686] | 1303 | } |
---|
| 1304 | |
---|
| 1305 | template<> |
---|
| 1306 | int CNetCdfInterface::ncPutVaraType(int ncid, int varid, const StdSize* start, const StdSize* count, const float* data) |
---|
| 1307 | { |
---|
[1601] | 1308 | int status; |
---|
| 1309 | #pragma omp critical (_netcdf) |
---|
| 1310 | { |
---|
[1646] | 1311 | info(200)<<"start nc_put_vara_float"<<std::endl; |
---|
[1601] | 1312 | status = nc_put_vara_float(ncid, varid, start, count, data); |
---|
[1646] | 1313 | info(200)<<"end nc_put_vara_float"<<std::endl; |
---|
[1601] | 1314 | } |
---|
| 1315 | return status; |
---|
[686] | 1316 | } |
---|
| 1317 | |
---|
| 1318 | template<> |
---|
| 1319 | int CNetCdfInterface::ncPutVaraType(int ncid, int varid, const StdSize* start, const StdSize* count, const int* data) |
---|
| 1320 | { |
---|
[1601] | 1321 | int status; |
---|
| 1322 | #pragma omp critical (_netcdf) |
---|
| 1323 | { |
---|
[1646] | 1324 | info(200)<<"start nc_put_vara_int"<<std::endl; |
---|
[1601] | 1325 | status = nc_put_vara_int(ncid, varid, start, count, data); |
---|
[1646] | 1326 | info(200)<<"end nc_put_vara_int"<<std::endl; |
---|
[1601] | 1327 | } |
---|
| 1328 | return status; |
---|
[686] | 1329 | } |
---|
| 1330 | |
---|
[1158] | 1331 | template<> |
---|
| 1332 | int CNetCdfInterface::ncPutVaraType(int ncid, int varid, const StdSize* start, const StdSize* count, const char* data) |
---|
| 1333 | { |
---|
[1601] | 1334 | int status; |
---|
| 1335 | #pragma omp critical (_netcdf) |
---|
| 1336 | { |
---|
[1646] | 1337 | info(200)<<"start nc_put_vara_text"<<std::endl; |
---|
[1601] | 1338 | status = nc_put_vara_text(ncid, varid, start, count, data); |
---|
[1646] | 1339 | info(200)<<"end nc_put_vara_text"<<std::endl; |
---|
[1601] | 1340 | } |
---|
| 1341 | return status; |
---|
[1158] | 1342 | } |
---|
| 1343 | |
---|
[525] | 1344 | /*! |
---|
| 1345 | This function verifies an existence of a variable by using its name. |
---|
| 1346 | Be careful, althoug false means variable doens't exist, it could show that netCDF file doesn't either |
---|
| 1347 | \param [in] ncid Id of groupd(or File Id) |
---|
| 1348 | \param [in] attrName Name of the variable |
---|
| 1349 | \return Existence of variable |
---|
| 1350 | */ |
---|
| 1351 | bool CNetCdfInterface::isVarExisted(int ncId, const StdString& varName) |
---|
| 1352 | { |
---|
| 1353 | int varId = 0; |
---|
[1601] | 1354 | int status; |
---|
| 1355 | #pragma omp critical (_netcdf) |
---|
| 1356 | { |
---|
[1646] | 1357 | info(200)<<"start isVarExisted"<<std::endl; |
---|
[1601] | 1358 | status = nc_inq_varid(ncId, varName.c_str(), &varId); |
---|
[1646] | 1359 | info(200)<<"end isVarExisted"<<std::endl; |
---|
[1601] | 1360 | } |
---|
| 1361 | return (NC_NOERR == status); |
---|
[525] | 1362 | } |
---|
| 1363 | |
---|
[811] | 1364 | bool CNetCdfInterface::isDimExisted(int ncId, const StdString& dimName) |
---|
| 1365 | { |
---|
| 1366 | int dimId = 0; |
---|
[1601] | 1367 | int status; |
---|
| 1368 | #pragma omp critical (_netcdf) |
---|
| 1369 | { |
---|
[1646] | 1370 | info(200)<<"start isDimExisted"<<std::endl; |
---|
[1601] | 1371 | status = nc_inq_dimid(ncId, dimName.c_str(), &dimId); |
---|
[1646] | 1372 | info(200)<<"end isDimExisted"<<std::endl; |
---|
[1601] | 1373 | } |
---|
| 1374 | return (NC_NOERR == status); |
---|
[811] | 1375 | } |
---|
| 1376 | |
---|
[525] | 1377 | StdString CNetCdfInterface::openMode2String(int oMode) |
---|
| 1378 | { |
---|
| 1379 | StdString modeMes; |
---|
| 1380 | switch (oMode) |
---|
[498] | 1381 | { |
---|
[525] | 1382 | case NC_NOWRITE: |
---|
[686] | 1383 | modeMes = StdString("NC_NOWRITE: Opening netCDF file with read-only access with buffering and caching access"); |
---|
[525] | 1384 | break; |
---|
| 1385 | case NC_SHARE: |
---|
[686] | 1386 | modeMes = StdString("NC_SHARE: Several processes can read the file concurrently"); |
---|
[525] | 1387 | break; |
---|
| 1388 | case NC_WRITE: |
---|
[686] | 1389 | modeMes = StdString("NC_WRITE: NetCDF file is readable and writable"); |
---|
[525] | 1390 | break; |
---|
| 1391 | default: |
---|
| 1392 | modeMes = StdString("In the composed opening mode"); |
---|
| 1393 | break; |
---|
[578] | 1394 | } |
---|
[525] | 1395 | return modeMes; |
---|
| 1396 | } |
---|
[498] | 1397 | |
---|
[525] | 1398 | StdString CNetCdfInterface::creationMode2String(int cMode) |
---|
| 1399 | { |
---|
| 1400 | StdString modeMes; |
---|
| 1401 | switch (cMode) |
---|
[498] | 1402 | { |
---|
[525] | 1403 | case NC_NOCLOBBER: |
---|
[686] | 1404 | modeMes = StdString("NC_NOCLOBBER: Not overwrite an exisiting netCDF file "); |
---|
[525] | 1405 | break; |
---|
| 1406 | case NC_SHARE: |
---|
[686] | 1407 | modeMes = StdString("NC_SHARE: Several processes can read from and write into the file concurrently"); |
---|
[525] | 1408 | break; |
---|
| 1409 | case NC_64BIT_OFFSET: |
---|
[686] | 1410 | modeMes = StdString("NC_64BIT_OFFSET: NetCDF file is 64-bit offset"); |
---|
[525] | 1411 | break; |
---|
| 1412 | case NC_NETCDF4: |
---|
[686] | 1413 | modeMes = StdString("NC_NETCDF4: NetCDF file is HDF5/NetCDF-4"); |
---|
[525] | 1414 | break; |
---|
| 1415 | case NC_CLASSIC_MODEL: |
---|
[686] | 1416 | modeMes = StdString("NC_CLASSIC_MODEL: NetCDF file is classical model"); |
---|
[525] | 1417 | break; |
---|
| 1418 | default: |
---|
| 1419 | modeMes = StdString("In the composed creation mode"); |
---|
| 1420 | break; |
---|
[578] | 1421 | } |
---|
[525] | 1422 | return modeMes; |
---|
| 1423 | } |
---|
[498] | 1424 | |
---|
[525] | 1425 | } |
---|