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