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