[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" |
---|
[1638] | 12 | #include "ep_mpi.hpp" |
---|
[525] | 13 | namespace xios |
---|
| 14 | { |
---|
| 15 | /*! |
---|
| 16 | This function creates a new netcdf file and return its id |
---|
| 17 | \param [in] fileName Name of the file |
---|
| 18 | \param [in] cMode create mode |
---|
| 19 | \param [in/out] ncId id of the created file |
---|
| 20 | \return Status code |
---|
| 21 | */ |
---|
| 22 | int CNetCdfInterface::create(const StdString& fileName, int cMode, int& ncId) |
---|
| 23 | { |
---|
[686] | 24 | int status = nc_create(fileName.c_str(), cMode, &ncId); |
---|
[525] | 25 | if (NC_NOERR != status) |
---|
| 26 | { |
---|
| 27 | StdString errormsg(nc_strerror(status)); |
---|
| 28 | StdStringStream sstr; |
---|
[686] | 29 | sstr << "Error when calling function: nc_create(fileName.c_str(), cMode, &ncId) " << std::endl |
---|
[525] | 30 | << errormsg << std::endl |
---|
[686] | 31 | << "Unable to create file, given its name: " << fileName |
---|
[1370] | 32 | << " and its creation mode " << creationMode2String(cMode) << std::endl; |
---|
[525] | 33 | StdString e = sstr.str(); |
---|
| 34 | throw CNetCdfException(e); |
---|
| 35 | } |
---|
[498] | 36 | |
---|
[525] | 37 | return status; |
---|
| 38 | } |
---|
[498] | 39 | |
---|
[525] | 40 | /*! |
---|
| 41 | This function creates a new netcdf file on parallel file system |
---|
| 42 | \param [in] fileName Name of the file |
---|
| 43 | \param [in] cMode create mode |
---|
| 44 | \param [in] comm MPI communicator |
---|
| 45 | \param [in] info MPI information |
---|
| 46 | \param [in/out] ncId id of the created file |
---|
| 47 | \return Status code |
---|
| 48 | */ |
---|
[1638] | 49 | int CNetCdfInterface::createPar(const StdString& fileName, int cMode, ep_lib::MPI_Comm comm, ep_lib::MPI_Info info, int& ncId) |
---|
[525] | 50 | { |
---|
[1638] | 51 | #ifdef _usingMPI |
---|
[686] | 52 | int status = xios::nc_create_par(fileName.c_str(), cMode, comm, info, &ncId); |
---|
[1638] | 53 | #elif _usingEP |
---|
| 54 | int status = xios::nc_create_par(fileName.c_str(), cMode, to_mpi_comm(comm->mpi_comm), to_mpi_info(info), &ncId); |
---|
| 55 | #endif |
---|
[525] | 56 | if (NC_NOERR != status) |
---|
| 57 | { |
---|
| 58 | StdString errormsg(nc_strerror(status)); |
---|
| 59 | StdStringStream sstr; |
---|
[686] | 60 | sstr << "Error when calling function: nc_create_par(fileName.c_str(), cMode, comm, info, &ncId) " << std::endl |
---|
[525] | 61 | << errormsg << std::endl |
---|
[686] | 62 | << "Unable to create file on parallel file system, given its name: " << std::endl |
---|
[525] | 63 | << "and its creation mode " << creationMode2String(cMode) << std::endl; |
---|
| 64 | StdString e = sstr.str(); |
---|
| 65 | throw CNetCdfException(e); |
---|
| 66 | } |
---|
[498] | 67 | |
---|
[525] | 68 | return status; |
---|
| 69 | } |
---|
[498] | 70 | |
---|
[525] | 71 | /*! |
---|
| 72 | This function opens a netcdf file, given its name and open mode, return its id |
---|
| 73 | \param [in] fileName Name of the file |
---|
| 74 | \param [in] oMode open mode |
---|
| 75 | \param [in/out] ncId id of the opening file |
---|
| 76 | \return Status code |
---|
| 77 | */ |
---|
| 78 | int CNetCdfInterface::open(const StdString& fileName, int oMode, int& ncId) |
---|
| 79 | { |
---|
[686] | 80 | int status = nc_open(fileName.c_str(), oMode, &ncId); |
---|
[525] | 81 | if (NC_NOERR != status) |
---|
| 82 | { |
---|
| 83 | StdString errormsg(nc_strerror(status)); |
---|
| 84 | StdStringStream sstr; |
---|
[686] | 85 | sstr << "Error when calling function: nc_open(fileName.c_str(), oMode, &ncId) "<< std::endl |
---|
[525] | 86 | << errormsg << std::endl |
---|
[686] | 87 | << "Unable to open file, given its name: " << fileName |
---|
[525] | 88 | << "and its open mode " << openMode2String(oMode) << std::endl; |
---|
| 89 | StdString e = sstr.str(); |
---|
| 90 | throw CNetCdfException(e); |
---|
| 91 | } |
---|
[498] | 92 | |
---|
[525] | 93 | return status; |
---|
| 94 | } |
---|
[498] | 95 | |
---|
| 96 | |
---|
[525] | 97 | /*! |
---|
| 98 | This function opens a new netcdf file on parallel file system |
---|
| 99 | \param [in] fileName Name of the file |
---|
| 100 | \param [in] oMode open mode |
---|
| 101 | \param [in] comm MPI communicator |
---|
| 102 | \param [in] info MPI information |
---|
| 103 | \param [in/out] ncId id of the opened file |
---|
| 104 | \return Status code |
---|
| 105 | */ |
---|
[1638] | 106 | int CNetCdfInterface::openPar(const StdString& fileName, int oMode, ep_lib::MPI_Comm comm, ep_lib::MPI_Info info, int& ncId) |
---|
[525] | 107 | { |
---|
[1638] | 108 | #ifdef _usingMPI |
---|
[686] | 109 | int status = xios::nc_open_par(fileName.c_str(), oMode, comm, info, &ncId); |
---|
[1638] | 110 | #elif _usingEP |
---|
| 111 | int status = xios::nc_open_par(fileName.c_str(), oMode, to_mpi_comm(comm->mpi_comm), to_mpi_info(info), &ncId); |
---|
| 112 | #endif |
---|
[525] | 113 | if (NC_NOERR != status) |
---|
| 114 | { |
---|
| 115 | StdString errormsg(nc_strerror(status)); |
---|
| 116 | StdStringStream sstr; |
---|
[686] | 117 | sstr << "Error when calling function nc_open_par(fileName.c_str(), oMode, comm, info, &ncId) " << std::endl |
---|
[525] | 118 | << errormsg << std::endl |
---|
[686] | 119 | << "Unable to open file on parallel file system, given its name: " << fileName |
---|
[525] | 120 | << "and its open mode " << openMode2String(oMode) << std::endl; |
---|
| 121 | StdString e = sstr.str(); |
---|
| 122 | throw CNetCdfException(e); |
---|
| 123 | } |
---|
[498] | 124 | |
---|
[525] | 125 | return status; |
---|
| 126 | } |
---|
[498] | 127 | |
---|
[525] | 128 | /*! |
---|
| 129 | This function closes a netcdf file, given its id |
---|
| 130 | \param [in] ncId id of the opening netcdf file |
---|
| 131 | \return Status code |
---|
| 132 | */ |
---|
| 133 | int CNetCdfInterface::close(int ncId) |
---|
| 134 | { |
---|
| 135 | int status = nc_close(ncId); |
---|
| 136 | if (NC_NOERR != status) |
---|
| 137 | { |
---|
| 138 | StdString errormsg(nc_strerror(status)); |
---|
| 139 | StdStringStream sstr; |
---|
[686] | 140 | sstr << "Error when calling function nc_close(ncId)" << std::endl |
---|
[498] | 141 | << errormsg << std::endl |
---|
[686] | 142 | << "Unable to close file, given its id: " << ncId << std::endl; |
---|
[525] | 143 | StdString e = sstr.str(); |
---|
| 144 | throw CNetCdfException(e); |
---|
| 145 | } |
---|
[498] | 146 | |
---|
[525] | 147 | return status; |
---|
| 148 | } |
---|
[498] | 149 | |
---|
[525] | 150 | /*! |
---|
| 151 | This function put a netcdf file into define mode, given its id |
---|
| 152 | \param [in] ncId id of the opening netcdf file to be put into define mode |
---|
| 153 | \return Status code |
---|
| 154 | */ |
---|
| 155 | int CNetCdfInterface::reDef(int ncId) |
---|
| 156 | { |
---|
| 157 | int status = nc_redef(ncId); |
---|
| 158 | if (NC_NOERR != status) |
---|
| 159 | { |
---|
| 160 | StdString errormsg(nc_strerror(status)); |
---|
| 161 | StdStringStream sstr; |
---|
[686] | 162 | sstr << "Error when calling function nc_redef(ncId)" << std::endl |
---|
[525] | 163 | << errormsg << std::endl |
---|
[686] | 164 | << "Unable to put this file into define mode 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 ends a netcdf file 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::endDef(int ncId) |
---|
| 178 | { |
---|
| 179 | int status = nc_enddef(ncId); |
---|
| 180 | if (NC_NOERR != status) |
---|
| 181 | { |
---|
| 182 | StdString errormsg(nc_strerror(status)); |
---|
| 183 | StdStringStream sstr; |
---|
[498] | 184 | |
---|
[686] | 185 | sstr << "Error when calling function nc_enddef(ncId)" << std::endl |
---|
[525] | 186 | << errormsg << std::endl |
---|
[686] | 187 | << "Unable to end define mode of this file, given its id: " << ncId << std::endl; |
---|
[525] | 188 | StdString e = sstr.str(); |
---|
| 189 | throw CNetCdfException(e); |
---|
| 190 | } |
---|
[498] | 191 | |
---|
[525] | 192 | return status; |
---|
| 193 | } |
---|
[498] | 194 | |
---|
[525] | 195 | /*! |
---|
| 196 | This function makes a request to netcdf with ncid and group name then return ncid of the named group |
---|
| 197 | \param [in] ncid Groupd id (or File Id) |
---|
| 198 | \param [in] grpName Name of the desired group (or file) |
---|
| 199 | \param [in/out] grpId Group id if the group is found |
---|
| 200 | \return Status code |
---|
| 201 | */ |
---|
| 202 | int CNetCdfInterface::inqNcId(int ncid, const StdString& grpName, int& grpId) |
---|
| 203 | { |
---|
[686] | 204 | int status = nc_inq_ncid(ncid, grpName.c_str(), &grpId); |
---|
[525] | 205 | if (NC_NOERR != status) |
---|
| 206 | { |
---|
| 207 | StdString errormsg(nc_strerror(status)); |
---|
| 208 | StdStringStream sstr; |
---|
[498] | 209 | |
---|
[686] | 210 | sstr << "Error when calling function nc_inq_ncid(ncid, grpName.c_str(), &grpId)" << std::endl |
---|
[525] | 211 | << errormsg << std::endl |
---|
[686] | 212 | << "Unable to get id of a group (File), given its name: " << grpName << std::endl; |
---|
[525] | 213 | StdString e = sstr.str(); |
---|
| 214 | throw CNetCdfException(e); |
---|
| 215 | } |
---|
[498] | 216 | |
---|
[525] | 217 | return status; |
---|
| 218 | } |
---|
[498] | 219 | |
---|
| 220 | |
---|
[525] | 221 | /*! |
---|
| 222 | This function makes a request to netcdf with ncid and variable name then return ncid of the named variable |
---|
| 223 | \param [in] ncid Groupd id (or File Id) |
---|
| 224 | \param [in] varName Name of the desired variable |
---|
| 225 | \param [in/out] varId Variable id if this variable is found |
---|
| 226 | \return Status code |
---|
| 227 | */ |
---|
[686] | 228 | int CNetCdfInterface::inqVarId(int ncid, const StdString& varName, int& varId) |
---|
[525] | 229 | { |
---|
[686] | 230 | int status = nc_inq_varid(ncid, varName.c_str(), &varId); |
---|
[525] | 231 | if (NC_NOERR != status) |
---|
| 232 | { |
---|
| 233 | StdString errormsg(nc_strerror(status)); |
---|
| 234 | StdStringStream sstr; |
---|
[498] | 235 | |
---|
[686] | 236 | sstr << "Error when calling function: nc_inq_varid(ncid, varName.c_str(), &varId)" << std::endl |
---|
[525] | 237 | << (errormsg) << std::endl |
---|
[686] | 238 | << "Unable to get id of variable with name: " << varName << std::endl; |
---|
[525] | 239 | StdString e = sstr.str(); |
---|
| 240 | throw CNetCdfException(e); |
---|
| 241 | } |
---|
[498] | 242 | |
---|
[525] | 243 | return status; |
---|
| 244 | } |
---|
[498] | 245 | |
---|
[525] | 246 | /*! |
---|
| 247 | This function makes a request to netcdf with a netCdf dimension name then return ncid of the named dimension |
---|
| 248 | \param [in] ncid Groupd id (or File Id) |
---|
| 249 | \param [in] dimName Name of the desired dimension |
---|
| 250 | \param [in/out] dimId Dimension id if this dimension is found |
---|
| 251 | \return Status code |
---|
| 252 | */ |
---|
[686] | 253 | int CNetCdfInterface::inqDimId(int ncid, const StdString& dimName, int& dimId) |
---|
[525] | 254 | { |
---|
[686] | 255 | int status = nc_inq_dimid(ncid, dimName.c_str(), &dimId); |
---|
[525] | 256 | if (NC_NOERR != status) |
---|
| 257 | { |
---|
| 258 | StdString errormsg(nc_strerror(status)); |
---|
| 259 | StdStringStream sstr; |
---|
[498] | 260 | |
---|
[686] | 261 | sstr << "Error when calling function nc_inq_dimid(ncid, dimName.c_str(), &dimId)" << std::endl |
---|
[498] | 262 | << errormsg << std::endl |
---|
[686] | 263 | << "Unable to get id of dimension, given its name: " << dimName << std::endl; |
---|
[525] | 264 | StdString e = sstr.str(); |
---|
| 265 | throw CNetCdfException(e); |
---|
| 266 | } |
---|
[498] | 267 | |
---|
[525] | 268 | return status; |
---|
| 269 | } |
---|
[498] | 270 | |
---|
[525] | 271 | /*! |
---|
[686] | 272 | This function queries the name of a variable given its id. |
---|
| 273 | \param [in] ncid Groupd id (or File Id) |
---|
| 274 | \param [in] varId Id of desired variable |
---|
| 275 | \param [out] varName name of desired variable |
---|
| 276 | \return Status code |
---|
| 277 | */ |
---|
| 278 | int CNetCdfInterface::inqVarName(int ncid, int varId, StdString& varName) |
---|
| 279 | { |
---|
| 280 | char varNameBuff[NC_MAX_NAME + 1]; |
---|
| 281 | int status = nc_inq_varname(ncid, varId, varNameBuff); |
---|
| 282 | if (NC_NOERR != status) |
---|
| 283 | { |
---|
| 284 | StdString errormsg(nc_strerror(status)); |
---|
| 285 | StdStringStream sstr; |
---|
| 286 | |
---|
| 287 | sstr << "Error when calling function nc_inq_varname(ncid, varId, varNameBuff)" << std::endl |
---|
| 288 | << errormsg << std::endl |
---|
[833] | 289 | << "Unable to get variable name: "<< varName << " given its id: " << varId << std::endl; |
---|
[686] | 290 | StdString e = sstr.str(); |
---|
| 291 | throw CNetCdfException(e); |
---|
| 292 | } |
---|
| 293 | varName = varNameBuff; |
---|
| 294 | return status; |
---|
| 295 | } |
---|
| 296 | |
---|
| 297 | /*! |
---|
[525] | 298 | This function makes a request to netcdf with a netCdf dimension name then return ncid of the named dimension |
---|
| 299 | \param [in] ncid Groupd id (or File Id) |
---|
| 300 | \param [in/out] dimId Dimension id if this dimension is found |
---|
| 301 | \return Status code |
---|
| 302 | */ |
---|
| 303 | int CNetCdfInterface::inqUnLimDim(int ncid, int& dimId) |
---|
| 304 | { |
---|
| 305 | int status = nc_inq_unlimdim(ncid, &dimId); |
---|
| 306 | if (NC_NOERR != status) |
---|
| 307 | { |
---|
| 308 | StdString errormsg(nc_strerror(status)); |
---|
| 309 | StdStringStream sstr; |
---|
[498] | 310 | |
---|
[686] | 311 | sstr << "Error when calling function nc_inq_dimid" << std::endl |
---|
[525] | 312 | << errormsg << std::endl |
---|
| 313 | << "Unable to get id of unlimited dimension " << std::endl; |
---|
| 314 | StdString e = sstr.str(); |
---|
| 315 | throw CNetCdfException(e); |
---|
| 316 | } |
---|
[498] | 317 | |
---|
[525] | 318 | return status; |
---|
| 319 | } |
---|
[498] | 320 | |
---|
[525] | 321 | /*! |
---|
| 322 | This function makes a request to netcdf, returns name of a dimension, given its id |
---|
| 323 | \param [in] ncid Groupd id (or File Id) |
---|
| 324 | \param [in] dimId Id of desired dimension |
---|
[686] | 325 | \param [out] dimName Name of desired dimension |
---|
[525] | 326 | \return Status code |
---|
| 327 | */ |
---|
| 328 | int CNetCdfInterface::inqDimName(int ncid, int dimId, StdString& dimName) |
---|
| 329 | { |
---|
[686] | 330 | char fullNameIn[NC_MAX_NAME + 1]; |
---|
[525] | 331 | int status = nc_inq_dimname(ncid, dimId, fullNameIn); |
---|
| 332 | if (NC_NOERR != status) |
---|
| 333 | { |
---|
| 334 | StdString errormsg(nc_strerror(status)); |
---|
| 335 | StdStringStream sstr; |
---|
[498] | 336 | |
---|
[686] | 337 | sstr << "Error when calling function nc_inq_dimname(ncid, dimId, fullNameIn)" << std::endl |
---|
[525] | 338 | << errormsg << std::endl |
---|
[833] | 339 | << "Unable to get dimension name: " << dimName << " given its id: " << dimId << std::endl; |
---|
[525] | 340 | StdString e = sstr.str(); |
---|
| 341 | throw CNetCdfException(e); |
---|
| 342 | } |
---|
| 343 | dimName = StdString(fullNameIn); |
---|
| 344 | return status; |
---|
| 345 | } |
---|
[498] | 346 | |
---|
[525] | 347 | /*! |
---|
| 348 | This function makes a request to netcdf, returns length of a dimension, given its id |
---|
| 349 | \param [in] ncid Groupd id (or File Id) |
---|
| 350 | \param [in] dimId Id of desired dimension |
---|
| 351 | \param [in/out] dimLen Length of desired dimension |
---|
| 352 | \return Status code |
---|
| 353 | */ |
---|
| 354 | int CNetCdfInterface::inqDimLen(int ncid, int dimId, StdSize& dimLen) |
---|
| 355 | { |
---|
| 356 | int status = nc_inq_dimlen(ncid, dimId, &dimLen); |
---|
| 357 | if (NC_NOERR != status) |
---|
| 358 | { |
---|
| 359 | StdString errormsg(nc_strerror(status)); |
---|
| 360 | StdStringStream sstr; |
---|
[498] | 361 | |
---|
[686] | 362 | sstr << "Error when calling function nc_inq_dimlen(ncid, dimId, &dimLen)" << std::endl |
---|
[525] | 363 | << errormsg << std::endl |
---|
[686] | 364 | << "Unable to get dimension length given its id: " << dimId << std::endl; |
---|
[525] | 365 | StdString e = sstr.str(); |
---|
| 366 | throw CNetCdfException(e); |
---|
| 367 | } |
---|
[498] | 368 | |
---|
[525] | 369 | return status; |
---|
| 370 | } |
---|
[498] | 371 | |
---|
[525] | 372 | /*! |
---|
| 373 | This function makes a request to netcdf, returns number of dimensions of a variable, given its id |
---|
| 374 | \param [in] ncid Groupd id (or File Id) |
---|
| 375 | \param [in] varId Id of variable |
---|
| 376 | \param [in/out] ndims number of dimension of the variable |
---|
| 377 | \return Status code |
---|
| 378 | */ |
---|
| 379 | int CNetCdfInterface::inqVarNDims(int ncid, int varId, int& nDims) |
---|
| 380 | { |
---|
| 381 | int status = nc_inq_varndims(ncid, varId, &nDims); |
---|
| 382 | if (NC_NOERR != status) |
---|
| 383 | { |
---|
| 384 | StdString errormsg(nc_strerror(status)); |
---|
| 385 | StdStringStream sstr; |
---|
[498] | 386 | |
---|
[686] | 387 | sstr << "Error when calling function nc_inq_varndims(ncid, varId, &nDims)" << std::endl |
---|
[525] | 388 | << errormsg << std::endl |
---|
[686] | 389 | << "Unable to get the number of dimension of variable with Id: " << varId << std::endl; |
---|
[525] | 390 | StdString e = sstr.str(); |
---|
| 391 | throw CNetCdfException(e); |
---|
| 392 | } |
---|
[498] | 393 | |
---|
[525] | 394 | return status; |
---|
| 395 | } |
---|
[498] | 396 | |
---|
[525] | 397 | /*! |
---|
| 398 | This function makes a request to netcdf, returns a list of dimension ID describing the shape of the variable, given its id |
---|
| 399 | \param [in] ncid Groupd id (or File Id) |
---|
| 400 | \param [in] varId Id of variable |
---|
| 401 | \param [in/out] dimIds list of dimension of the variable |
---|
| 402 | \return Status code |
---|
| 403 | */ |
---|
| 404 | int CNetCdfInterface::inqVarDimId(int ncid, int varId, int* dimIds) |
---|
| 405 | { |
---|
| 406 | int status = nc_inq_vardimid(ncid, varId, dimIds); |
---|
| 407 | if (NC_NOERR != status) |
---|
| 408 | { |
---|
| 409 | StdString errormsg(nc_strerror(status)); |
---|
| 410 | StdStringStream sstr; |
---|
[498] | 411 | |
---|
[686] | 412 | sstr << "Error when calling function nc_inq_vardimid(ncid, varId, dimIds)" << std::endl |
---|
[525] | 413 | << errormsg << std::endl |
---|
| 414 | << "Unable to get list of dimension id of the variable with id " << varId << std::endl; |
---|
| 415 | StdString e = sstr.str(); |
---|
| 416 | throw CNetCdfException(e); |
---|
| 417 | } |
---|
[498] | 418 | |
---|
[525] | 419 | return status; |
---|
| 420 | } |
---|
[498] | 421 | |
---|
[525] | 422 | /*! |
---|
| 423 | This function makes a request to netcdf, to find all dimension in a group |
---|
| 424 | \param [in] ncid Groupd id (or File Id) |
---|
| 425 | \param [in/out] nDims number of list of dimension |
---|
| 426 | \param [in/out] dimIds list of dimension in a group or any of its parent |
---|
| 427 | \param [in] includeParents number of parents |
---|
| 428 | \return Status code |
---|
| 429 | */ |
---|
| 430 | int CNetCdfInterface::inqDimIds(int ncid, int& nDims, int* dimIds, int includeParents) |
---|
| 431 | { |
---|
| 432 | int status = nc_inq_dimids(ncid, &nDims, dimIds, includeParents); |
---|
| 433 | if (NC_NOERR != status) |
---|
| 434 | { |
---|
| 435 | StdString errormsg(nc_strerror(status)); |
---|
| 436 | StdStringStream sstr; |
---|
[498] | 437 | |
---|
[686] | 438 | sstr << "Error when calling function nc_inq_dimids(ncid, &nDims, dimIds, includeParents)" << std::endl; |
---|
[525] | 439 | sstr << errormsg << std::endl; |
---|
[686] | 440 | sstr << "Unable to retrieve number of dimension in the group with id: " << ncid << std::endl; |
---|
[525] | 441 | sstr << "With number of Parents " << includeParents << std::endl; |
---|
| 442 | StdString e = sstr.str(); |
---|
| 443 | throw CNetCdfException(e); |
---|
| 444 | } |
---|
[498] | 445 | |
---|
[525] | 446 | return status; |
---|
| 447 | } |
---|
[498] | 448 | |
---|
[525] | 449 | /*! |
---|
[686] | 450 | This function queries the full name of a group given its id. |
---|
| 451 | \param [in] ncid Groupd id (or File Id) |
---|
| 452 | \param [in/out] grpFullName the full name of the group |
---|
| 453 | \return Status code |
---|
| 454 | */ |
---|
| 455 | int CNetCdfInterface::inqGrpFullName(int ncid, StdString& grpFullName) |
---|
| 456 | { |
---|
| 457 | StdSize strlen = 0; |
---|
| 458 | std::vector<char> buff; |
---|
| 459 | int status = nc_inq_grpname_full(ncid, &strlen, NULL); |
---|
| 460 | if (NC_NOERR == status) |
---|
| 461 | { |
---|
| 462 | buff.resize(strlen + 1); |
---|
| 463 | status = nc_inq_grpname_full(ncid, NULL, &buff[0]); |
---|
| 464 | } |
---|
| 465 | |
---|
| 466 | if (NC_NOERR != status) |
---|
| 467 | { |
---|
| 468 | StdString errormsg(nc_strerror(status)); |
---|
| 469 | StdStringStream sstr; |
---|
| 470 | |
---|
| 471 | sstr << "Error when calling function nc_inq_grpname_full(ncid, &strlen, &buff[0])" << std::endl |
---|
| 472 | << errormsg << std::endl |
---|
| 473 | << "Unable to get the full group name given its id: " << ncid << std::endl; |
---|
| 474 | StdString e = sstr.str(); |
---|
| 475 | throw CNetCdfException(e); |
---|
| 476 | } |
---|
| 477 | |
---|
| 478 | grpFullName.assign(buff.begin(), buff.end()); |
---|
| 479 | |
---|
| 480 | return status; |
---|
| 481 | } |
---|
| 482 | |
---|
| 483 | /*! |
---|
| 484 | This function queries the list of group ids given a location id. |
---|
| 485 | \param [in] ncid Groupd id (or File Id) |
---|
| 486 | \param [in/out] numgrps number of groups |
---|
| 487 | \param [in/out] ncids list of group ids |
---|
| 488 | \return Status code |
---|
| 489 | */ |
---|
| 490 | int CNetCdfInterface::inqGrpIds(int ncid, int& numgrps, int* ncids) |
---|
| 491 | { |
---|
| 492 | int status = nc_inq_grps(ncid, &numgrps, ncids); |
---|
| 493 | if (NC_NOERR != status) |
---|
| 494 | { |
---|
| 495 | StdString errormsg(nc_strerror(status)); |
---|
| 496 | StdStringStream sstr; |
---|
| 497 | |
---|
| 498 | sstr << "Error when calling function nc_inq_grps(ncid, &numgrps, ncids)" << std::endl; |
---|
| 499 | sstr << errormsg << std::endl; |
---|
| 500 | sstr << "Unable to retrieve the list of groups for location id: " << ncid << std::endl; |
---|
| 501 | StdString e = sstr.str(); |
---|
| 502 | throw CNetCdfException(e); |
---|
| 503 | } |
---|
| 504 | |
---|
| 505 | return status; |
---|
| 506 | } |
---|
| 507 | |
---|
| 508 | /*! |
---|
| 509 | This function queries the list of variable ids given a location id. |
---|
| 510 | \param [in] ncid Groupd id (or File Id) |
---|
| 511 | \param [in/out] nvars number of variables |
---|
| 512 | \param [in/out] varids list of variable ids |
---|
| 513 | \return Status code |
---|
| 514 | */ |
---|
| 515 | int CNetCdfInterface::inqVarIds(int ncid, int& nvars, int* varids) |
---|
| 516 | { |
---|
| 517 | int status = nc_inq_varids(ncid, &nvars, varids); |
---|
| 518 | if (NC_NOERR != status) |
---|
| 519 | { |
---|
| 520 | StdString errormsg(nc_strerror(status)); |
---|
| 521 | StdStringStream sstr; |
---|
| 522 | |
---|
| 523 | sstr << "Error when calling function nc_inq_varids(ncid, &nvars, varids)" << std::endl; |
---|
| 524 | sstr << errormsg << std::endl; |
---|
| 525 | sstr << "Unable to retrieve the list of variables for location id: " << ncid << std::endl; |
---|
| 526 | StdString e = sstr.str(); |
---|
| 527 | throw CNetCdfException(e); |
---|
| 528 | } |
---|
| 529 | |
---|
| 530 | return status; |
---|
| 531 | } |
---|
| 532 | |
---|
| 533 | /*! |
---|
| 534 | 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. |
---|
| 535 | \param [in] ncid Groupd id (or File Id) |
---|
| 536 | \param [in] varid the id of the variable to which the attribute is attached |
---|
| 537 | \param [in] name the name of the attribute |
---|
| 538 | \param [out] type the type of the attribute |
---|
| 539 | \param [out] len the size of the attribute |
---|
| 540 | \return Status code |
---|
| 541 | */ |
---|
| 542 | int CNetCdfInterface::inqAtt(int ncid, int varid, const StdString& name, nc_type& type, size_t& len) |
---|
| 543 | { |
---|
| 544 | int status = nc_inq_att(ncid, varid, name.c_str(), &type, &len); |
---|
| 545 | if (NC_NOERR != status) |
---|
| 546 | { |
---|
| 547 | StdString errormsg(nc_strerror(status)); |
---|
| 548 | StdStringStream sstr; |
---|
| 549 | |
---|
| 550 | sstr << "Error when calling function nc_inq_att(ncid, varid, name.c_str(), &type, &len)" << std::endl; |
---|
| 551 | sstr << errormsg << std::endl; |
---|
| 552 | sstr << "Unable to query the attribute information given its name: " << name << " and its variable id:" << varid << std::endl; |
---|
| 553 | StdString e = sstr.str(); |
---|
| 554 | throw CNetCdfException(e); |
---|
| 555 | } |
---|
| 556 | |
---|
| 557 | return status; |
---|
| 558 | } |
---|
| 559 | |
---|
| 560 | /*! |
---|
| 561 | This function queries the number of global attributes given a location id. |
---|
| 562 | \param [in] ncid Groupd id (or File Id) |
---|
| 563 | \param [out] ngatts the number of global attributes |
---|
| 564 | \return Status code |
---|
| 565 | */ |
---|
| 566 | int CNetCdfInterface::inqNAtts(int ncid, int& ngatts) |
---|
| 567 | { |
---|
| 568 | int status = nc_inq_natts(ncid, &ngatts); |
---|
| 569 | if (NC_NOERR != status) |
---|
| 570 | { |
---|
| 571 | StdString errormsg(nc_strerror(status)); |
---|
| 572 | StdStringStream sstr; |
---|
| 573 | |
---|
| 574 | sstr << "Error when calling function nc_inq_natts(ncid, &ngatts)" << std::endl; |
---|
| 575 | sstr << errormsg << std::endl; |
---|
| 576 | sstr << "Unable to query the number of global attributes given the location id:" << ncid << std::endl; |
---|
| 577 | StdString e = sstr.str(); |
---|
| 578 | throw CNetCdfException(e); |
---|
| 579 | } |
---|
| 580 | |
---|
| 581 | return status; |
---|
| 582 | } |
---|
| 583 | |
---|
| 584 | /*! |
---|
| 585 | This function queries the number of global attributes given a location id and a variable id. |
---|
| 586 | \param [in] ncid Groupd id (or File Id) |
---|
| 587 | \param [in] varid the id of the variable |
---|
| 588 | \param [out] natts the number of global attributes |
---|
| 589 | \return Status code |
---|
| 590 | */ |
---|
| 591 | int CNetCdfInterface::inqVarNAtts(int ncid, int varid, int& natts) |
---|
| 592 | { |
---|
| 593 | int status = nc_inq_varnatts(ncid, varid, &natts); |
---|
| 594 | if (NC_NOERR != status) |
---|
| 595 | { |
---|
| 596 | StdString errormsg(nc_strerror(status)); |
---|
| 597 | StdStringStream sstr; |
---|
| 598 | |
---|
| 599 | sstr << "Error when calling function nc_inq_varnatts(ncid, varid, &natts)" << std::endl; |
---|
| 600 | sstr << errormsg << std::endl; |
---|
| 601 | sstr << "Unable to query the number of attributes given the location id:" << ncid << " and the variable id:" << varid << std::endl; |
---|
| 602 | StdString e = sstr.str(); |
---|
| 603 | throw CNetCdfException(e); |
---|
| 604 | } |
---|
| 605 | |
---|
| 606 | return status; |
---|
| 607 | } |
---|
| 608 | |
---|
| 609 | |
---|
| 610 | //! Query the name of an attribute given a location id, a variable id and the attribute number |
---|
| 611 | int CNetCdfInterface::inqAttName(int ncid, int varid, int attnum, StdString& name) |
---|
| 612 | { |
---|
[782] | 613 | std::vector<char> attName(NC_MAX_NAME + 1,' '); |
---|
| 614 | int status = nc_inq_attname(ncid, varid, attnum, &attName[0]); |
---|
[686] | 615 | if (NC_NOERR != status) |
---|
| 616 | { |
---|
| 617 | StdString errormsg(nc_strerror(status)); |
---|
| 618 | StdStringStream sstr; |
---|
| 619 | |
---|
| 620 | sstr << "Error when calling function nc_inq_attname(ncid, varid, attnum, attName)" << std::endl; |
---|
| 621 | sstr << errormsg << std::endl; |
---|
[833] | 622 | sstr << "Unable to query the name: " << name << " of attribute " << attnum << " given the location id:" << ncid << " and the variable id:" << varid << std::endl; |
---|
[686] | 623 | StdString e = sstr.str(); |
---|
| 624 | throw CNetCdfException(e); |
---|
| 625 | } |
---|
| 626 | |
---|
[782] | 627 | int nameSize = 0; |
---|
| 628 | while ((nameSize < NC_MAX_NAME) && (' ' != attName[nameSize] )) ++nameSize; |
---|
| 629 | name.resize(nameSize); |
---|
| 630 | // for (int idx = 0; idx < nameSize; ++idx) name.at(idx) = attName[idx]; |
---|
| 631 | std::copy(&attName[0], &attName[nameSize-1], name.begin()); |
---|
| 632 | |
---|
[686] | 633 | return status; |
---|
| 634 | } |
---|
| 635 | |
---|
| 636 | /*! |
---|
[525] | 637 | 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 |
---|
| 638 | \param [in] parentNcid Id of parent groupd(or File Id) |
---|
| 639 | \param [in] grpName Name of the desired group |
---|
| 640 | \param [in/out] grpId Group id if this group is created sucessfully |
---|
| 641 | \return Status code |
---|
| 642 | */ |
---|
| 643 | int CNetCdfInterface::defGrp(int parentNcid, const StdString& grpName, int& grpId) |
---|
| 644 | { |
---|
[686] | 645 | int status = nc_def_grp(parentNcid, grpName.c_str(), &grpId); |
---|
[525] | 646 | if (NC_NOERR != status) |
---|
| 647 | { |
---|
| 648 | StdString errormsg(nc_strerror(status)); |
---|
| 649 | StdStringStream sstr; |
---|
[498] | 650 | |
---|
[686] | 651 | sstr << "Error when calling function nc_def_grp(parentNcid, grpName.c_str(), &grpId)" << std::endl; |
---|
[525] | 652 | sstr << errormsg << std::endl; |
---|
[686] | 653 | sstr << "Unable to create group Id, given its name: " << grpName << std::endl; |
---|
[525] | 654 | StdString e = sstr.str(); |
---|
| 655 | throw CNetCdfException(e); |
---|
| 656 | } |
---|
[498] | 657 | |
---|
[525] | 658 | return status; |
---|
| 659 | } |
---|
[498] | 660 | |
---|
[525] | 661 | /*! |
---|
| 662 | This function makes a request to netcdf, add a new dimension to an open netcdf in define mode |
---|
| 663 | \param [in] ncid Id of groupd(or File Id) |
---|
| 664 | \param [in] dimName Name of the desired dimension |
---|
| 665 | \param [in/out] grpId Group id if this group is created sucessfully |
---|
| 666 | \return Status code |
---|
| 667 | */ |
---|
| 668 | int CNetCdfInterface::defDim(int ncid, const StdString& dimName, StdSize dimLen, int& dimId) |
---|
| 669 | { |
---|
[686] | 670 | int status = nc_def_dim(ncid, dimName.c_str(), dimLen, &dimId); |
---|
[525] | 671 | if (NC_NOERR != status) |
---|
| 672 | { |
---|
| 673 | StdString errormsg(nc_strerror(status)); |
---|
| 674 | StdStringStream sstr; |
---|
[498] | 675 | |
---|
[686] | 676 | sstr << "Error when calling function nc_def_dim(ncid, dimName.c_str(), dimLen, &dimId)" << std::endl; |
---|
[525] | 677 | sstr << errormsg << std::endl; |
---|
[686] | 678 | sstr << "Unable to create dimension with name: " << dimName |
---|
[525] | 679 | << " and with length " << dimLen << std::endl; |
---|
| 680 | StdString e = sstr.str(); |
---|
| 681 | throw CNetCdfException(e); |
---|
| 682 | } |
---|
[498] | 683 | |
---|
[525] | 684 | return status; |
---|
| 685 | } |
---|
[498] | 686 | |
---|
[525] | 687 | /*! |
---|
| 688 | This function makes a request to netcdf with its id, to add a new variable to an open netCdf in define mode, |
---|
| 689 | return a variable id, given its name, type, the number of dimensions and list of dimension id |
---|
| 690 | \param [in] ncid Id of groupd(or File Id) |
---|
| 691 | \param [in] varName Name of the desired dimension |
---|
| 692 | \param [in] xtypes One of the set of predefined netCDF data types |
---|
| 693 | \param [in] nDims Number of dimension for the variable |
---|
| 694 | \param [in] dimIds List of ndims dimension ids corresponding to the variable dimensions |
---|
| 695 | \param [in/out] varId Variable id if it is added sucessfully |
---|
| 696 | \return Status code |
---|
| 697 | */ |
---|
[686] | 698 | int CNetCdfInterface::defVar(int ncid, const StdString& varName, nc_type xtype, |
---|
| 699 | int nDims, const int dimIds[], int& varId) |
---|
[525] | 700 | { |
---|
[686] | 701 | int status = nc_def_var(ncid, varName.c_str(), xtype, nDims, dimIds, &varId); |
---|
[525] | 702 | if (NC_NOERR != status) |
---|
| 703 | { |
---|
| 704 | StdString errormsg(nc_strerror(status)); |
---|
| 705 | StdStringStream sstr; |
---|
[498] | 706 | |
---|
[686] | 707 | sstr << "Error when calling function nc_def_var(ncid, varName.c_str(), xtype, nDims, dimIds, &varId)" << std::endl; |
---|
[525] | 708 | sstr << errormsg << std::endl; |
---|
[686] | 709 | sstr << "Unable to add a new variable with name: " << varName |
---|
[525] | 710 | << " with type " << xtype |
---|
| 711 | << " and number of dimension " << nDims << std::endl; |
---|
| 712 | StdString e = sstr.str(); |
---|
| 713 | throw CNetCdfException(e); |
---|
| 714 | } |
---|
[498] | 715 | |
---|
[525] | 716 | return status; |
---|
| 717 | } |
---|
[498] | 718 | |
---|
[525] | 719 | /*! |
---|
| 720 | This function makes a request to netcdf with a ncid, to set the chunking size of a variable, |
---|
| 721 | given variable id and type of storage |
---|
| 722 | \param [in] ncid Id groupd(or File Id) |
---|
| 723 | \param [in] varId Id of the variable |
---|
[686] | 724 | \param [in] storage Type of storage (It can be: NC_CONTIGUOUS, NC_CHUNKED) |
---|
[525] | 725 | \param [in/out] chunkSize array list of chunk sizes |
---|
| 726 | \return Status code |
---|
| 727 | */ |
---|
| 728 | int CNetCdfInterface::defVarChunking(int ncid, int varId, int storage, StdSize chunkSize[]) |
---|
| 729 | { |
---|
| 730 | int status = nc_def_var_chunking(ncid, varId, storage, chunkSize); |
---|
| 731 | if (NC_NOERR != status) |
---|
[517] | 732 | { |
---|
[525] | 733 | StdString errormsg(nc_strerror(status)); |
---|
| 734 | StdStringStream sstr; |
---|
[517] | 735 | |
---|
[686] | 736 | sstr << "Error when calling function nc_def_var_chunking(ncid, varId, storage, chunkSize)" << std::endl; |
---|
[525] | 737 | sstr << errormsg << std::endl; |
---|
[686] | 738 | sstr << "Unable to set chunk size of the variable with id: " << varId |
---|
[525] | 739 | << " and storage type " << storage << std::endl; |
---|
| 740 | StdString e = sstr.str(); |
---|
| 741 | throw CNetCdfException(e); |
---|
[517] | 742 | } |
---|
| 743 | |
---|
[525] | 744 | return status; |
---|
| 745 | } |
---|
[498] | 746 | |
---|
[525] | 747 | /*! |
---|
[606] | 748 | This function sets the compression level to the specified variable |
---|
| 749 | \param [in] ncid Groud id (or file id) |
---|
| 750 | \param [in] varId Id of the variable |
---|
| 751 | \param [in] compressionLevel The compression level from 0 to 9 (0 disables the compression, 9 is the higher compression) |
---|
| 752 | \return Status code |
---|
| 753 | */ |
---|
| 754 | int CNetCdfInterface::defVarDeflate(int ncid, int varId, int compressionLevel) |
---|
| 755 | { |
---|
[972] | 756 | |
---|
| 757 | if (compressionLevel == 0) return NC_NOERR ; |
---|
[1454] | 758 | int status = nc_def_var_deflate(ncid, varId, (compressionLevel > 0), (compressionLevel > 0), compressionLevel); |
---|
[606] | 759 | if (NC_NOERR != status) |
---|
| 760 | { |
---|
| 761 | StdString errormsg(nc_strerror(status)); |
---|
| 762 | StdStringStream sstr; |
---|
| 763 | |
---|
[686] | 764 | sstr << "Error when calling function nc_def_var_deflate(ncid, varId, false, (compressionLevel > 0), compressionLevel)" << std::endl; |
---|
[606] | 765 | sstr << errormsg << std::endl; |
---|
| 766 | sstr << "Unable to set the compression level of the variable with id: " << varId |
---|
| 767 | << " and compression level: " << compressionLevel << std::endl; |
---|
| 768 | StdString e = sstr.str(); |
---|
| 769 | throw CNetCdfException(e); |
---|
| 770 | } |
---|
| 771 | |
---|
| 772 | return status; |
---|
| 773 | } |
---|
| 774 | |
---|
| 775 | /*! |
---|
[525] | 776 | Set or unset the fill mode for a NetCDF file specified by its file id. |
---|
| 777 | \param [in] ncid File id |
---|
| 778 | \param [in] fill Define whether the fill mode should be enabled or not |
---|
| 779 | \return Status code |
---|
| 780 | */ |
---|
| 781 | int CNetCdfInterface::setFill(int ncid, bool fill) |
---|
| 782 | { |
---|
| 783 | int old_fill_mode; |
---|
[686] | 784 | int status = nc_set_fill(ncid, fill ? NC_FILL: NC_NOFILL, &old_fill_mode); |
---|
[525] | 785 | if (NC_NOERR != status) |
---|
| 786 | { |
---|
| 787 | StdString errormsg(nc_strerror(status)); |
---|
| 788 | StdStringStream sstr; |
---|
[498] | 789 | |
---|
[686] | 790 | sstr << "Error when calling function nc_set_fill(ncid, fill ? NC_FILL: NC_NOFILL, &old_fill_mode)" << std::endl; |
---|
[525] | 791 | sstr << errormsg << std::endl; |
---|
[686] | 792 | sstr << "Unable to set the fill mode to: " << (fill ? "NC_FILL": "NC_NOFILL") << std::endl; |
---|
[525] | 793 | StdString e = sstr.str(); |
---|
| 794 | throw CNetCdfException(e); |
---|
| 795 | } |
---|
[498] | 796 | |
---|
[525] | 797 | return status; |
---|
| 798 | } |
---|
[498] | 799 | |
---|
[525] | 800 | /*! |
---|
| 801 | This function makes a request to netcdf with a ncid, to set the fill parameters for a variable, |
---|
| 802 | given variable id and type of fill |
---|
| 803 | \param [in] ncid Id groupd(or File Id) |
---|
| 804 | \param [in] varId Id of the variable |
---|
| 805 | \param [in] noFill turn on/off nofill mode on a variable |
---|
| 806 | \param [in/out] fillValue |
---|
| 807 | \return Status code |
---|
| 808 | */ |
---|
| 809 | int CNetCdfInterface::defVarFill(int ncid, int varId, int noFill, void* fillValue) |
---|
| 810 | { |
---|
| 811 | int status = nc_def_var_fill(ncid, varId, noFill, fillValue); |
---|
| 812 | if (NC_NOERR != status) |
---|
| 813 | { |
---|
| 814 | StdString errormsg(nc_strerror(status)); |
---|
| 815 | StdStringStream sstr; |
---|
[498] | 816 | |
---|
[686] | 817 | sstr << "Error when calling function nc_def_var_fill(ncid, varId, noFill, fillValue)" << std::endl; |
---|
[525] | 818 | sstr << errormsg << std::endl; |
---|
[686] | 819 | sstr << "Unable to set fill parameters of the variable with id: " << varId |
---|
[525] | 820 | << " and fill option " << noFill << std::endl; |
---|
| 821 | StdString e = sstr.str(); |
---|
| 822 | throw CNetCdfException(e); |
---|
| 823 | } |
---|
[498] | 824 | |
---|
[525] | 825 | return status; |
---|
| 826 | } |
---|
[498] | 827 | |
---|
[525] | 828 | /*! |
---|
| 829 | This function makes a request to netcdf with a ncid, to change the way read/write operations are performed |
---|
| 830 | collectively or independently on the variable. |
---|
| 831 | \param [in] ncid Id groupd(or File Id) |
---|
| 832 | \param [in] varId Id of the variable |
---|
| 833 | \param [in] noFill turn on/off nofill mode on a variable |
---|
| 834 | \param [in/out] fillValue |
---|
| 835 | \return Status code |
---|
| 836 | */ |
---|
| 837 | int CNetCdfInterface::varParAccess(int ncid, int varId, int access) |
---|
| 838 | { |
---|
| 839 | int status = nc_var_par_access(ncid, varId, access); |
---|
| 840 | if (NC_NOERR != status) |
---|
| 841 | { |
---|
| 842 | StdString errormsg(nc_strerror(status)); |
---|
| 843 | StdStringStream sstr; |
---|
[498] | 844 | |
---|
[686] | 845 | sstr << "Error when calling function nc_var_par_access(ncid, varId, access)" << std::endl; |
---|
[525] | 846 | sstr << errormsg << std::endl; |
---|
[686] | 847 | sstr << "Unable to change read/write option of the variable with id: " << varId << std::endl; |
---|
[525] | 848 | StdString e = sstr.str(); |
---|
| 849 | throw CNetCdfException(e); |
---|
| 850 | } |
---|
[498] | 851 | |
---|
[525] | 852 | return status; |
---|
| 853 | } |
---|
[498] | 854 | |
---|
[525] | 855 | /*! |
---|
| 856 | This function makes a synchronisation of the disk copy of a netCDF dataset. |
---|
| 857 | \param [in] ncid Id groupd(or File Id) |
---|
| 858 | \return Status code |
---|
| 859 | */ |
---|
| 860 | int CNetCdfInterface::sync(int ncid) |
---|
| 861 | { |
---|
| 862 | int status = nc_sync(ncid); |
---|
| 863 | if (NC_NOERR != status) |
---|
[498] | 864 | { |
---|
[525] | 865 | StdString errormsg(nc_strerror(status)); |
---|
| 866 | StdStringStream sstr; |
---|
[498] | 867 | |
---|
[686] | 868 | sstr << "Error when calling function nc_sync(ncid)" << std::endl; |
---|
[525] | 869 | sstr << errormsg << std::endl; |
---|
[686] | 870 | sstr << "Unable to make a synchronization of a netCDF file with id: " << ncid << std::endl; |
---|
[525] | 871 | StdString e = sstr.str(); |
---|
| 872 | throw CNetCdfException(e); |
---|
[498] | 873 | } |
---|
| 874 | |
---|
[525] | 875 | return status; |
---|
| 876 | } |
---|
[498] | 877 | |
---|
[686] | 878 | // Some specializations of getAttributeType |
---|
| 879 | template<> |
---|
| 880 | int CNetCdfInterface::ncGetAttType(int ncid, int varid, const char* attrName, double* data) |
---|
[525] | 881 | { |
---|
[686] | 882 | return nc_get_att_double(ncid, varid, attrName, data); |
---|
| 883 | } |
---|
[498] | 884 | |
---|
[686] | 885 | template<> |
---|
| 886 | int CNetCdfInterface::ncGetAttType(int ncid, int varid, const char* attrName, float* data) |
---|
| 887 | { |
---|
| 888 | return nc_get_att_float(ncid, varid, attrName, data); |
---|
| 889 | } |
---|
[498] | 890 | |
---|
[686] | 891 | template<> |
---|
| 892 | int CNetCdfInterface::ncGetAttType(int ncid, int varid, const char* attrName, int* data) |
---|
| 893 | { |
---|
| 894 | return nc_get_att_int(ncid, varid, attrName, data); |
---|
[525] | 895 | } |
---|
[498] | 896 | |
---|
[525] | 897 | template<> |
---|
[686] | 898 | int CNetCdfInterface::ncGetAttType(int ncid, int varid, const char* attrName, long* data) |
---|
| 899 | { |
---|
| 900 | return nc_get_att_long(ncid, varid, attrName, data); |
---|
| 901 | } |
---|
| 902 | |
---|
| 903 | template<> |
---|
| 904 | int CNetCdfInterface::ncGetAttType(int ncid, int varid, const char* attrName, short* data) |
---|
| 905 | { |
---|
| 906 | return nc_get_att_short(ncid, varid, attrName, data); |
---|
| 907 | } |
---|
| 908 | |
---|
| 909 | template<> |
---|
| 910 | int CNetCdfInterface::ncGetAttType(int ncid, int varid, const char* attrName, char* data) |
---|
| 911 | { |
---|
| 912 | return nc_get_att_text(ncid, varid, attrName, data); |
---|
| 913 | } |
---|
| 914 | |
---|
| 915 | // Some specializations of putAttributeType |
---|
| 916 | template<> |
---|
[525] | 917 | int CNetCdfInterface::ncPutAttType(int ncid, int varid, const char* attrName, |
---|
[686] | 918 | StdSize numVal, const double* data) |
---|
[525] | 919 | { |
---|
[686] | 920 | return nc_put_att_double(ncid, varid, attrName, NC_DOUBLE, numVal, data); |
---|
[525] | 921 | } |
---|
[498] | 922 | |
---|
[525] | 923 | template<> |
---|
| 924 | int CNetCdfInterface::ncPutAttType(int ncid, int varid, const char* attrName, |
---|
[686] | 925 | StdSize numVal, const float* data) |
---|
[525] | 926 | { |
---|
[686] | 927 | return nc_put_att_float(ncid, varid, attrName, NC_FLOAT, numVal, data); |
---|
[525] | 928 | } |
---|
[498] | 929 | |
---|
[525] | 930 | template<> |
---|
| 931 | int CNetCdfInterface::ncPutAttType(int ncid, int varid, const char* attrName, |
---|
[686] | 932 | StdSize numVal, const int* data) |
---|
[525] | 933 | { |
---|
[686] | 934 | return nc_put_att_int(ncid, varid, attrName, NC_INT, numVal, data); |
---|
[525] | 935 | } |
---|
[498] | 936 | |
---|
[525] | 937 | template<> |
---|
| 938 | int CNetCdfInterface::ncPutAttType(int ncid, int varid, const char* attrName, |
---|
[686] | 939 | StdSize numVal, const long* data) |
---|
[525] | 940 | { |
---|
[686] | 941 | return nc_put_att_long(ncid, varid, attrName, NC_LONG, numVal, data); |
---|
[525] | 942 | } |
---|
[498] | 943 | |
---|
[525] | 944 | template<> |
---|
| 945 | int CNetCdfInterface::ncPutAttType(int ncid, int varid, const char* attrName, |
---|
[686] | 946 | StdSize numVal, const short* data) |
---|
[525] | 947 | { |
---|
[686] | 948 | return nc_put_att_short(ncid, varid, attrName, NC_SHORT, numVal, data); |
---|
[525] | 949 | } |
---|
| 950 | |
---|
[686] | 951 | template<> |
---|
| 952 | int CNetCdfInterface::ncPutAttType(int ncid, int varid, const char* attrName, |
---|
| 953 | StdSize numVal, const char* data) |
---|
| 954 | { |
---|
| 955 | return nc_put_att_text(ncid, varid, attrName, numVal, data); |
---|
| 956 | } |
---|
[525] | 957 | |
---|
[686] | 958 | // Some specializations of getVariableType |
---|
[525] | 959 | template<> |
---|
[686] | 960 | int CNetCdfInterface::ncGetVaraType(int ncid, int varid, const StdSize* start, const StdSize* count, double* data) |
---|
[525] | 961 | { |
---|
[686] | 962 | return nc_get_vara_double(ncid, varid, start, count, data); |
---|
[525] | 963 | } |
---|
| 964 | |
---|
| 965 | template<> |
---|
[686] | 966 | int CNetCdfInterface::ncGetVaraType(int ncid, int varid, const StdSize* start, const StdSize* count, float* data) |
---|
[525] | 967 | { |
---|
[686] | 968 | return nc_get_vara_float(ncid, varid, start, count, data); |
---|
[525] | 969 | } |
---|
| 970 | |
---|
| 971 | template<> |
---|
[686] | 972 | int CNetCdfInterface::ncGetVaraType(int ncid, int varid, const StdSize* start, const StdSize* count, int* data) |
---|
[525] | 973 | { |
---|
[686] | 974 | return nc_get_vara_int(ncid, varid, start, count, data); |
---|
[525] | 975 | } |
---|
| 976 | |
---|
[1158] | 977 | template<> |
---|
| 978 | int CNetCdfInterface::ncGetVaraType(int ncid, int varid, const StdSize* start, const StdSize* count, char* data) |
---|
| 979 | { |
---|
| 980 | return nc_get_vara_text(ncid, varid, start, count, data); |
---|
| 981 | } |
---|
| 982 | |
---|
[686] | 983 | // Some specializations of putVariableType |
---|
| 984 | template<> |
---|
| 985 | int CNetCdfInterface::ncPutVaraType(int ncid, int varid, const StdSize* start, const StdSize* count, const double* data) |
---|
| 986 | { |
---|
| 987 | return nc_put_vara_double(ncid, varid, start, count, data); |
---|
| 988 | } |
---|
| 989 | |
---|
| 990 | template<> |
---|
| 991 | int CNetCdfInterface::ncPutVaraType(int ncid, int varid, const StdSize* start, const StdSize* count, const float* data) |
---|
| 992 | { |
---|
| 993 | return nc_put_vara_float(ncid, varid, start, count, data); |
---|
| 994 | } |
---|
| 995 | |
---|
| 996 | template<> |
---|
| 997 | int CNetCdfInterface::ncPutVaraType(int ncid, int varid, const StdSize* start, const StdSize* count, const int* data) |
---|
| 998 | { |
---|
| 999 | return nc_put_vara_int(ncid, varid, start, count, data); |
---|
| 1000 | } |
---|
| 1001 | |
---|
[1158] | 1002 | template<> |
---|
| 1003 | int CNetCdfInterface::ncPutVaraType(int ncid, int varid, const StdSize* start, const StdSize* count, const char* data) |
---|
| 1004 | { |
---|
| 1005 | return nc_put_vara_text(ncid, varid, start, count, data); |
---|
| 1006 | } |
---|
| 1007 | |
---|
[525] | 1008 | /*! |
---|
| 1009 | This function verifies an existence of a variable by using its name. |
---|
| 1010 | Be careful, althoug false means variable doens't exist, it could show that netCDF file doesn't either |
---|
| 1011 | \param [in] ncid Id of groupd(or File Id) |
---|
| 1012 | \param [in] attrName Name of the variable |
---|
| 1013 | \return Existence of variable |
---|
| 1014 | */ |
---|
| 1015 | bool CNetCdfInterface::isVarExisted(int ncId, const StdString& varName) |
---|
| 1016 | { |
---|
| 1017 | int varId = 0; |
---|
| 1018 | return (NC_NOERR == (nc_inq_varid(ncId, varName.c_str(), &varId))); |
---|
| 1019 | } |
---|
| 1020 | |
---|
[811] | 1021 | bool CNetCdfInterface::isDimExisted(int ncId, const StdString& dimName) |
---|
| 1022 | { |
---|
| 1023 | int dimId = 0; |
---|
| 1024 | return (NC_NOERR == (nc_inq_dimid(ncId, dimName.c_str(), &dimId))); |
---|
| 1025 | } |
---|
| 1026 | |
---|
[525] | 1027 | StdString CNetCdfInterface::openMode2String(int oMode) |
---|
| 1028 | { |
---|
| 1029 | StdString modeMes; |
---|
| 1030 | switch (oMode) |
---|
[498] | 1031 | { |
---|
[525] | 1032 | case NC_NOWRITE: |
---|
[686] | 1033 | modeMes = StdString("NC_NOWRITE: Opening netCDF file with read-only access with buffering and caching access"); |
---|
[525] | 1034 | break; |
---|
| 1035 | case NC_SHARE: |
---|
[686] | 1036 | modeMes = StdString("NC_SHARE: Several processes can read the file concurrently"); |
---|
[525] | 1037 | break; |
---|
| 1038 | case NC_WRITE: |
---|
[686] | 1039 | modeMes = StdString("NC_WRITE: NetCDF file is readable and writable"); |
---|
[525] | 1040 | break; |
---|
| 1041 | default: |
---|
| 1042 | modeMes = StdString("In the composed opening mode"); |
---|
| 1043 | break; |
---|
[578] | 1044 | } |
---|
[525] | 1045 | return modeMes; |
---|
| 1046 | } |
---|
[498] | 1047 | |
---|
[525] | 1048 | StdString CNetCdfInterface::creationMode2String(int cMode) |
---|
| 1049 | { |
---|
| 1050 | StdString modeMes; |
---|
| 1051 | switch (cMode) |
---|
[498] | 1052 | { |
---|
[525] | 1053 | case NC_NOCLOBBER: |
---|
[686] | 1054 | modeMes = StdString("NC_NOCLOBBER: Not overwrite an exisiting netCDF file "); |
---|
[525] | 1055 | break; |
---|
| 1056 | case NC_SHARE: |
---|
[686] | 1057 | modeMes = StdString("NC_SHARE: Several processes can read from and write into the file concurrently"); |
---|
[525] | 1058 | break; |
---|
| 1059 | case NC_64BIT_OFFSET: |
---|
[686] | 1060 | modeMes = StdString("NC_64BIT_OFFSET: NetCDF file is 64-bit offset"); |
---|
[525] | 1061 | break; |
---|
| 1062 | case NC_NETCDF4: |
---|
[686] | 1063 | modeMes = StdString("NC_NETCDF4: NetCDF file is HDF5/NetCDF-4"); |
---|
[525] | 1064 | break; |
---|
| 1065 | case NC_CLASSIC_MODEL: |
---|
[686] | 1066 | modeMes = StdString("NC_CLASSIC_MODEL: NetCDF file is classical model"); |
---|
[525] | 1067 | break; |
---|
| 1068 | default: |
---|
| 1069 | modeMes = StdString("In the composed creation mode"); |
---|
| 1070 | break; |
---|
[578] | 1071 | } |
---|
[525] | 1072 | return modeMes; |
---|
| 1073 | } |
---|
[498] | 1074 | |
---|
[525] | 1075 | } |
---|