1 | /** \file |
---|
2 | The V2 API Funtions. |
---|
3 | |
---|
4 | Copyright 1996, University Corporation for Atmospheric Research |
---|
5 | See \ref copyright file for copying and redistribution conditions. |
---|
6 | */ |
---|
7 | |
---|
8 | #ifndef NO_NETCDF_2 |
---|
9 | |
---|
10 | #include <config.h> |
---|
11 | #include <stdlib.h> |
---|
12 | #include <stdio.h> |
---|
13 | #include <stdarg.h> |
---|
14 | #include "netcdf.h" |
---|
15 | |
---|
16 | /* The subroutines in error.c emit no messages unless NC_VERBOSE bit |
---|
17 | * is on. They call exit() when NC_FATAL bit is on. */ |
---|
18 | int ncopts = (NC_FATAL | NC_VERBOSE) ; |
---|
19 | int ncerr = NC_NOERR ; |
---|
20 | |
---|
21 | #if SIZEOF_LONG == SIZEOF_SIZE_T |
---|
22 | /* |
---|
23 | * We don't have to copy the arguments to switch from 'long' |
---|
24 | * to 'size_t' or 'ptrdiff_t'. Use dummy macros. |
---|
25 | */ |
---|
26 | |
---|
27 | # define NDIMS_DECL |
---|
28 | # define A_DECL(name, type, ndims, rhs) \ |
---|
29 | const type *const name = ((const type *)(rhs)) |
---|
30 | |
---|
31 | # define A_FREE(name) |
---|
32 | |
---|
33 | # define A_INIT(lhs, type, ndims, rhs) |
---|
34 | |
---|
35 | #else |
---|
36 | /* |
---|
37 | * We do have to copy the arguments to switch from 'long' |
---|
38 | * to 'size_t' or 'ptrdiff_t'. In my tests on an SGI, |
---|
39 | * any additional cost was lost in measurement variation. |
---|
40 | * |
---|
41 | * This stanza is true on Windows with MinGW-64 |
---|
42 | */ |
---|
43 | |
---|
44 | # include "onstack.h" |
---|
45 | |
---|
46 | static size_t |
---|
47 | nvdims(int ncid, int varid) |
---|
48 | { |
---|
49 | int ndims=-1, status; |
---|
50 | |
---|
51 | if ((status = nc_inq_varndims(ncid, varid, &ndims))) |
---|
52 | { |
---|
53 | nc_advise("ncvdims", status, "ncid %d", ncid); |
---|
54 | return -1; |
---|
55 | } |
---|
56 | return ndims; |
---|
57 | } |
---|
58 | |
---|
59 | #define NDIMS_DECL const size_t ndims = nvdims(ncid, varid); |
---|
60 | |
---|
61 | # define A_DECL(name, type, ndims, rhs) \ |
---|
62 | ALLOC_ONSTACK(name, type, ndims) |
---|
63 | |
---|
64 | # define A_FREE(name) \ |
---|
65 | FREE_ONSTACK(name) |
---|
66 | |
---|
67 | # define A_INIT(lhs, type, ndims, rhs) \ |
---|
68 | { \ |
---|
69 | const long *lp = rhs; \ |
---|
70 | type *tp = lhs; \ |
---|
71 | type *const end = lhs + ndims; \ |
---|
72 | while(tp < end) \ |
---|
73 | { \ |
---|
74 | *tp++ = (type) *lp++; \ |
---|
75 | } \ |
---|
76 | } |
---|
77 | |
---|
78 | |
---|
79 | #endif |
---|
80 | |
---|
81 | typedef signed char schar; |
---|
82 | |
---|
83 | /* |
---|
84 | * Computes number of record variables in an open netCDF file, and an array of |
---|
85 | * the record variable ids, if the array parameter is non-null. |
---|
86 | */ |
---|
87 | static int |
---|
88 | numrecvars(int ncid, int *nrecvarsp, int *recvarids) |
---|
89 | { |
---|
90 | int status = NC_NOERR; |
---|
91 | int nvars = 0; |
---|
92 | int ndims = 0; |
---|
93 | int nrecvars = 0; |
---|
94 | int varid; |
---|
95 | int recdimid; |
---|
96 | int dimids[MAX_NC_DIMS]; |
---|
97 | |
---|
98 | status = nc_inq_nvars(ncid, &nvars); |
---|
99 | if(status != NC_NOERR) |
---|
100 | return status; |
---|
101 | |
---|
102 | status = nc_inq_unlimdim(ncid, &recdimid); |
---|
103 | if(status != NC_NOERR) |
---|
104 | return status; |
---|
105 | |
---|
106 | if (recdimid == -1) { |
---|
107 | *nrecvarsp = 0; |
---|
108 | return NC_NOERR; |
---|
109 | } |
---|
110 | nrecvars = 0; |
---|
111 | for (varid = 0; varid < nvars; varid++) { |
---|
112 | status = nc_inq_varndims(ncid, varid, &ndims); |
---|
113 | if(status != NC_NOERR) |
---|
114 | return status; |
---|
115 | status = nc_inq_vardimid(ncid, varid, dimids); |
---|
116 | if(status != NC_NOERR) |
---|
117 | return status; |
---|
118 | if (ndims > 0 && dimids[0] == recdimid) { |
---|
119 | if (recvarids != NULL) |
---|
120 | recvarids[nrecvars] = varid; |
---|
121 | nrecvars++; |
---|
122 | } |
---|
123 | } |
---|
124 | *nrecvarsp = nrecvars; |
---|
125 | return NC_NOERR; |
---|
126 | } |
---|
127 | |
---|
128 | |
---|
129 | /* |
---|
130 | * Computes record size (in bytes) of the record variable with a specified |
---|
131 | * variable id. Returns size as 0 if not a record variable. |
---|
132 | */ |
---|
133 | static int |
---|
134 | ncrecsize(int ncid, int varid, size_t *recsizep) |
---|
135 | { |
---|
136 | int status = NC_NOERR; |
---|
137 | int recdimid; |
---|
138 | nc_type type; |
---|
139 | int ndims; |
---|
140 | int dimids[MAX_NC_DIMS]; |
---|
141 | int id; |
---|
142 | size_t size; |
---|
143 | |
---|
144 | *recsizep = 0; |
---|
145 | status = nc_inq_unlimdim(ncid, &recdimid); |
---|
146 | if(status != NC_NOERR) |
---|
147 | return status; |
---|
148 | status = nc_inq_vartype(ncid, varid, &type); |
---|
149 | if(status != NC_NOERR) |
---|
150 | return status; |
---|
151 | status = nc_inq_varndims(ncid, varid, &ndims); |
---|
152 | if(status != NC_NOERR) |
---|
153 | return status; |
---|
154 | status = nc_inq_vardimid(ncid, varid, dimids); |
---|
155 | if(status != NC_NOERR) |
---|
156 | return status; |
---|
157 | if (ndims == 0 || dimids[0] != recdimid) { |
---|
158 | return NC_NOERR; |
---|
159 | } |
---|
160 | size = nctypelen(type); |
---|
161 | for (id = 1; id < ndims; id++) { |
---|
162 | size_t len; |
---|
163 | status = nc_inq_dimlen(ncid, dimids[id], &len); |
---|
164 | if(status != NC_NOERR) |
---|
165 | return status; |
---|
166 | size *= len; |
---|
167 | } |
---|
168 | *recsizep = size; |
---|
169 | return NC_NOERR; |
---|
170 | } |
---|
171 | |
---|
172 | |
---|
173 | /* |
---|
174 | * Retrieves the dimension sizes of a variable with a specified variable id in |
---|
175 | * an open netCDF file. Returns -1 on error. |
---|
176 | */ |
---|
177 | static int |
---|
178 | dimsizes(int ncid, int varid, size_t *sizes) |
---|
179 | { |
---|
180 | int status = NC_NOERR; |
---|
181 | int ndims; |
---|
182 | int id; |
---|
183 | int dimids[MAX_NC_DIMS]; |
---|
184 | |
---|
185 | status = nc_inq_varndims(ncid, varid, &ndims); |
---|
186 | if(status != NC_NOERR) |
---|
187 | return status; |
---|
188 | status = nc_inq_vardimid(ncid, varid, dimids); |
---|
189 | if(status != NC_NOERR) |
---|
190 | return status; |
---|
191 | if (ndims == 0 || sizes == NULL) |
---|
192 | return NC_NOERR; |
---|
193 | for (id = 0; id < ndims; id++) { |
---|
194 | size_t len; |
---|
195 | status = nc_inq_dimlen(ncid, dimids[id], &len); |
---|
196 | if(status != NC_NOERR) |
---|
197 | return status; |
---|
198 | sizes[id] = len; |
---|
199 | } |
---|
200 | return NC_NOERR; |
---|
201 | } |
---|
202 | |
---|
203 | |
---|
204 | /* |
---|
205 | * Retrieves the number of record variables, the record variable ids, and the |
---|
206 | * record size of each record variable. If any pointer to info to be returned |
---|
207 | * is null, the associated information is not returned. Returns -1 on error. |
---|
208 | */ |
---|
209 | int |
---|
210 | nc_inq_rec( |
---|
211 | int ncid, |
---|
212 | size_t *nrecvarsp, |
---|
213 | int *recvarids, |
---|
214 | size_t *recsizes) |
---|
215 | { |
---|
216 | int status = NC_NOERR; |
---|
217 | int nvars = 0; |
---|
218 | int recdimid; |
---|
219 | int varid; |
---|
220 | int rvarids[MAX_NC_VARS]; |
---|
221 | int nrvars = 0; |
---|
222 | |
---|
223 | status = nc_inq_nvars(ncid, &nvars); |
---|
224 | if(status != NC_NOERR) |
---|
225 | return status; |
---|
226 | |
---|
227 | status = nc_inq_unlimdim(ncid, &recdimid); |
---|
228 | if(status != NC_NOERR) |
---|
229 | return status; |
---|
230 | |
---|
231 | *nrecvarsp = 0; |
---|
232 | if (recdimid == -1) |
---|
233 | return NC_NOERR; |
---|
234 | |
---|
235 | status = numrecvars(ncid, &nrvars, rvarids); |
---|
236 | if(status != NC_NOERR) |
---|
237 | return status; |
---|
238 | |
---|
239 | if (nrecvarsp != NULL) |
---|
240 | *nrecvarsp = nrvars; |
---|
241 | if (recvarids != NULL) |
---|
242 | for (varid = 0; varid < nrvars; varid++) |
---|
243 | recvarids[varid] = rvarids[varid]; |
---|
244 | |
---|
245 | if (recsizes != NULL) |
---|
246 | for (varid = 0; varid < nrvars; varid++) { |
---|
247 | size_t rsize; |
---|
248 | status = ncrecsize(ncid, rvarids[varid], &rsize); |
---|
249 | if (status != NC_NOERR) |
---|
250 | return status; |
---|
251 | recsizes[varid] = rsize; |
---|
252 | } |
---|
253 | return NC_NOERR; |
---|
254 | } |
---|
255 | |
---|
256 | |
---|
257 | /* |
---|
258 | * Write one record's worth of data, except don't write to variables for which |
---|
259 | * the address of the data to be written is NULL. Return -1 on error. This is |
---|
260 | * the same as the ncrecput() in the library, except that can handle errors |
---|
261 | * better. |
---|
262 | */ |
---|
263 | int |
---|
264 | nc_put_rec( |
---|
265 | int ncid, |
---|
266 | size_t recnum, |
---|
267 | void* const* datap) |
---|
268 | { |
---|
269 | int status = NC_NOERR; |
---|
270 | int varid; |
---|
271 | int rvarids[MAX_NC_VARS]; |
---|
272 | int nrvars; |
---|
273 | size_t start[MAX_NC_DIMS]; |
---|
274 | size_t edges[MAX_NC_DIMS]; |
---|
275 | |
---|
276 | status = numrecvars(ncid, &nrvars, rvarids); |
---|
277 | if(status != NC_NOERR) |
---|
278 | return status; |
---|
279 | |
---|
280 | if (nrvars == 0) |
---|
281 | return NC_NOERR; |
---|
282 | |
---|
283 | start[0] = recnum; |
---|
284 | for (varid = 1; varid < nrvars; varid++) |
---|
285 | start[varid] = 0; |
---|
286 | |
---|
287 | for (varid = 0; varid < nrvars; varid++) { |
---|
288 | if (datap[varid] != NULL) { |
---|
289 | status = dimsizes(ncid, rvarids[varid], edges); |
---|
290 | if(status != NC_NOERR) |
---|
291 | return status; |
---|
292 | |
---|
293 | edges[0] = 1; /* only 1 record's worth */ |
---|
294 | status = nc_put_vara(ncid, rvarids[varid], start, edges, datap[varid]); |
---|
295 | if(status != NC_NOERR) |
---|
296 | return status; |
---|
297 | } |
---|
298 | } |
---|
299 | return 0; |
---|
300 | } |
---|
301 | |
---|
302 | |
---|
303 | /* |
---|
304 | * Read one record's worth of data, except don't read from variables for which |
---|
305 | * the address of the data to be read is null. Return -1 on error. This is |
---|
306 | * the same as the ncrecget() in the library, except that can handle errors |
---|
307 | * better. |
---|
308 | */ |
---|
309 | int |
---|
310 | nc_get_rec( |
---|
311 | int ncid, |
---|
312 | size_t recnum, |
---|
313 | void **datap) |
---|
314 | { |
---|
315 | int status = NC_NOERR; |
---|
316 | int varid; |
---|
317 | int rvarids[MAX_NC_VARS]; |
---|
318 | int nrvars; |
---|
319 | size_t start[MAX_NC_DIMS]; |
---|
320 | size_t edges[MAX_NC_DIMS]; |
---|
321 | |
---|
322 | status = numrecvars(ncid, &nrvars, rvarids); |
---|
323 | if(status != NC_NOERR) |
---|
324 | return status; |
---|
325 | |
---|
326 | if (nrvars == 0) |
---|
327 | return NC_NOERR; |
---|
328 | |
---|
329 | start[0] = recnum; |
---|
330 | for (varid = 1; varid < nrvars; varid++) |
---|
331 | start[varid] = 0; |
---|
332 | |
---|
333 | for (varid = 0; varid < nrvars; varid++) { |
---|
334 | if (datap[varid] != NULL) { |
---|
335 | status = dimsizes(ncid, rvarids[varid], edges); |
---|
336 | if(status != NC_NOERR) |
---|
337 | return status; |
---|
338 | edges[0] = 1; /* only 1 record's worth */ |
---|
339 | status = nc_get_vara(ncid, rvarids[varid], start, edges, datap[varid]); |
---|
340 | if(status != NC_NOERR) |
---|
341 | return status; |
---|
342 | } |
---|
343 | } |
---|
344 | return 0; |
---|
345 | } |
---|
346 | |
---|
347 | /* |
---|
348 | */ |
---|
349 | void |
---|
350 | nc_advise(const char *routine_name, int err, const char *fmt,...) |
---|
351 | { |
---|
352 | va_list args; |
---|
353 | |
---|
354 | if(NC_ISSYSERR(err)) |
---|
355 | ncerr = NC_SYSERR; |
---|
356 | else |
---|
357 | ncerr = err; |
---|
358 | |
---|
359 | if( ncopts & NC_VERBOSE ) |
---|
360 | { |
---|
361 | (void) fprintf(stderr,"%s: ", routine_name); |
---|
362 | va_start(args ,fmt); |
---|
363 | (void) vfprintf(stderr,fmt,args); |
---|
364 | va_end(args); |
---|
365 | if(err != NC_NOERR) |
---|
366 | { |
---|
367 | (void) fprintf(stderr,": %s", |
---|
368 | nc_strerror(err)); |
---|
369 | } |
---|
370 | (void) fputc('\n',stderr); |
---|
371 | (void) fflush(stderr); /* to ensure log files are current */ |
---|
372 | } |
---|
373 | |
---|
374 | if( (ncopts & NC_FATAL) && err != NC_NOERR ) |
---|
375 | { |
---|
376 | exit(ncopts); |
---|
377 | } |
---|
378 | } |
---|
379 | |
---|
380 | /* End error handling */ |
---|
381 | |
---|
382 | int |
---|
383 | nccreate(const char* path, int cmode) |
---|
384 | { |
---|
385 | int ncid; |
---|
386 | const int status = nc_create(path, cmode, &ncid); |
---|
387 | if(status != NC_NOERR) |
---|
388 | { |
---|
389 | nc_advise("nccreate", status, "filename \"%s\"", path); |
---|
390 | return -1; |
---|
391 | } |
---|
392 | return ncid; |
---|
393 | } |
---|
394 | |
---|
395 | |
---|
396 | int |
---|
397 | ncopen(const char *path, int mode) |
---|
398 | { |
---|
399 | int ncid; |
---|
400 | const int status = nc_open(path, mode, &ncid); |
---|
401 | if(status != NC_NOERR) |
---|
402 | { |
---|
403 | nc_advise("ncopen", status, "filename \"%s\"", path); |
---|
404 | return -1; |
---|
405 | } |
---|
406 | return ncid; |
---|
407 | } |
---|
408 | |
---|
409 | |
---|
410 | int |
---|
411 | ncredef(int ncid) |
---|
412 | { |
---|
413 | const int status = nc_redef(ncid); |
---|
414 | if(status != NC_NOERR) |
---|
415 | { |
---|
416 | nc_advise("ncredef", status, "ncid %d", ncid); |
---|
417 | return -1; |
---|
418 | } |
---|
419 | return 0; |
---|
420 | } |
---|
421 | |
---|
422 | |
---|
423 | int |
---|
424 | ncendef(int ncid) |
---|
425 | { |
---|
426 | const int status = nc_enddef(ncid); |
---|
427 | if(status != NC_NOERR) |
---|
428 | { |
---|
429 | nc_advise("ncendef", status, "ncid %d", ncid); |
---|
430 | return -1; |
---|
431 | } |
---|
432 | return 0; |
---|
433 | } |
---|
434 | |
---|
435 | |
---|
436 | int |
---|
437 | ncclose(int ncid) |
---|
438 | { |
---|
439 | const int status = nc_close(ncid); |
---|
440 | if(status != NC_NOERR) |
---|
441 | { |
---|
442 | nc_advise("ncclose", status, "ncid %d", ncid); |
---|
443 | return -1; |
---|
444 | |
---|
445 | } |
---|
446 | return 0; |
---|
447 | } |
---|
448 | |
---|
449 | |
---|
450 | int |
---|
451 | ncinquire( |
---|
452 | int ncid, |
---|
453 | int* ndims, |
---|
454 | int* nvars, |
---|
455 | int* natts, |
---|
456 | int* recdim |
---|
457 | ) |
---|
458 | { |
---|
459 | int nd, nv, na; |
---|
460 | const int status = nc_inq(ncid, &nd, &nv, &na, recdim); |
---|
461 | |
---|
462 | if(status != NC_NOERR) |
---|
463 | { |
---|
464 | nc_advise("ncinquire", status, "ncid %d", ncid); |
---|
465 | return -1; |
---|
466 | } |
---|
467 | /* else */ |
---|
468 | |
---|
469 | if(ndims != NULL) |
---|
470 | *ndims = (int) nd; |
---|
471 | |
---|
472 | if(nvars != NULL) |
---|
473 | *nvars = (int) nv; |
---|
474 | |
---|
475 | if(natts != NULL) |
---|
476 | *natts = (int) na; |
---|
477 | |
---|
478 | return ncid; |
---|
479 | } |
---|
480 | |
---|
481 | |
---|
482 | int |
---|
483 | ncsync(int ncid) |
---|
484 | { |
---|
485 | const int status = nc_sync(ncid); |
---|
486 | if(status != NC_NOERR) |
---|
487 | { |
---|
488 | nc_advise("ncsync", status, "ncid %d", ncid); |
---|
489 | return -1; |
---|
490 | |
---|
491 | } |
---|
492 | return 0; |
---|
493 | } |
---|
494 | |
---|
495 | |
---|
496 | int |
---|
497 | ncabort(int ncid) |
---|
498 | { |
---|
499 | const int status = nc_abort(ncid); |
---|
500 | if(status != NC_NOERR) |
---|
501 | { |
---|
502 | nc_advise("ncabort", status, "ncid %d", ncid); |
---|
503 | return -1; |
---|
504 | } |
---|
505 | return 0; |
---|
506 | } |
---|
507 | |
---|
508 | |
---|
509 | int |
---|
510 | ncdimdef( |
---|
511 | int ncid, |
---|
512 | const char* name, |
---|
513 | long length |
---|
514 | ) |
---|
515 | { |
---|
516 | int dimid; |
---|
517 | int status = NC_NOERR; |
---|
518 | if(length < 0) { |
---|
519 | status = NC_EDIMSIZE; |
---|
520 | nc_advise("ncdimdef", status, "ncid %d", ncid); |
---|
521 | return -1; |
---|
522 | } |
---|
523 | status = nc_def_dim(ncid, name, (size_t)length, &dimid); |
---|
524 | if(status != NC_NOERR) |
---|
525 | { |
---|
526 | nc_advise("ncdimdef", status, "ncid %d", ncid); |
---|
527 | return -1; |
---|
528 | } |
---|
529 | return dimid; |
---|
530 | } |
---|
531 | |
---|
532 | |
---|
533 | int |
---|
534 | ncdimid(int ncid, const char* name) |
---|
535 | { |
---|
536 | int dimid; |
---|
537 | const int status = nc_inq_dimid(ncid, name, &dimid); |
---|
538 | if(status != NC_NOERR) |
---|
539 | { |
---|
540 | nc_advise("ncdimid", status, "ncid %d", ncid); |
---|
541 | return -1; |
---|
542 | } |
---|
543 | return dimid; |
---|
544 | } |
---|
545 | |
---|
546 | |
---|
547 | int |
---|
548 | ncdiminq( |
---|
549 | int ncid, |
---|
550 | int dimid, |
---|
551 | char* name, |
---|
552 | long* length |
---|
553 | ) |
---|
554 | { |
---|
555 | size_t ll; |
---|
556 | const int status = nc_inq_dim(ncid, dimid, name, &ll); |
---|
557 | |
---|
558 | if(status != NC_NOERR) |
---|
559 | { |
---|
560 | nc_advise("ncdiminq", status, "ncid %d", ncid); |
---|
561 | return -1; |
---|
562 | } |
---|
563 | /* else */ |
---|
564 | |
---|
565 | if(length != NULL) |
---|
566 | *length = (int) ll; |
---|
567 | |
---|
568 | return dimid; |
---|
569 | } |
---|
570 | |
---|
571 | |
---|
572 | int |
---|
573 | ncdimrename( |
---|
574 | int ncid, |
---|
575 | int dimid, |
---|
576 | const char* name |
---|
577 | ) |
---|
578 | { |
---|
579 | const int status = nc_rename_dim(ncid, dimid, name); |
---|
580 | if(status != NC_NOERR) |
---|
581 | { |
---|
582 | nc_advise("ncdimrename", status, "ncid %d", ncid); |
---|
583 | return -1; |
---|
584 | } |
---|
585 | return dimid; |
---|
586 | } |
---|
587 | |
---|
588 | |
---|
589 | int |
---|
590 | ncvardef( |
---|
591 | int ncid, |
---|
592 | const char* name, |
---|
593 | nc_type datatype, |
---|
594 | int ndims, |
---|
595 | const int* dim |
---|
596 | ) |
---|
597 | { |
---|
598 | int varid = -1; |
---|
599 | const int status = nc_def_var(ncid, name, datatype, ndims, dim, &varid); |
---|
600 | if(status != NC_NOERR) |
---|
601 | { |
---|
602 | nc_advise("ncvardef", status, "ncid %d", ncid); |
---|
603 | return -1; |
---|
604 | } |
---|
605 | return varid; |
---|
606 | } |
---|
607 | |
---|
608 | |
---|
609 | int |
---|
610 | ncvarid( |
---|
611 | int ncid, |
---|
612 | const char* name |
---|
613 | ) |
---|
614 | { |
---|
615 | int varid = -1; |
---|
616 | const int status = nc_inq_varid(ncid, name, &varid); |
---|
617 | if(status != NC_NOERR) |
---|
618 | { |
---|
619 | nc_advise("ncvarid", status, "ncid %d", ncid); |
---|
620 | return -1; |
---|
621 | } |
---|
622 | return varid; |
---|
623 | } |
---|
624 | |
---|
625 | |
---|
626 | int |
---|
627 | ncvarinq( |
---|
628 | int ncid, |
---|
629 | int varid, |
---|
630 | char* name, |
---|
631 | nc_type* datatype, |
---|
632 | int* ndims, |
---|
633 | int* dim, |
---|
634 | int* natts |
---|
635 | ) |
---|
636 | { |
---|
637 | int nd, na; |
---|
638 | const int status = nc_inq_var(ncid, varid, name, datatype, |
---|
639 | &nd, dim, &na); |
---|
640 | |
---|
641 | if(status != NC_NOERR) |
---|
642 | { |
---|
643 | nc_advise("ncvarinq", status, "ncid %d", ncid); |
---|
644 | return -1; |
---|
645 | } |
---|
646 | /* else */ |
---|
647 | |
---|
648 | if(ndims != NULL) |
---|
649 | *ndims = (int) nd; |
---|
650 | |
---|
651 | if(natts != NULL) |
---|
652 | *natts = (int) na; |
---|
653 | |
---|
654 | return varid; |
---|
655 | } |
---|
656 | |
---|
657 | |
---|
658 | int |
---|
659 | ncvarput1( |
---|
660 | int ncid, |
---|
661 | int varid, |
---|
662 | const long* index, |
---|
663 | const void* value |
---|
664 | ) |
---|
665 | { |
---|
666 | NDIMS_DECL |
---|
667 | A_DECL(coordp, size_t, ndims, index); |
---|
668 | A_INIT(coordp, size_t, ndims, index); |
---|
669 | { |
---|
670 | const int status = nc_put_var1(ncid, varid, coordp, value); |
---|
671 | A_FREE(coordp); |
---|
672 | if(status != NC_NOERR) |
---|
673 | { |
---|
674 | nc_advise("ncvarput1", status, "ncid %d", ncid); |
---|
675 | return -1; |
---|
676 | } |
---|
677 | } |
---|
678 | return 0; |
---|
679 | } |
---|
680 | |
---|
681 | |
---|
682 | int |
---|
683 | ncvarget1( |
---|
684 | int ncid, |
---|
685 | int varid, |
---|
686 | const long* index, |
---|
687 | void* value |
---|
688 | ) |
---|
689 | { |
---|
690 | NDIMS_DECL |
---|
691 | A_DECL(coordp, size_t, ndims, index); |
---|
692 | A_INIT(coordp, size_t, ndims, index); |
---|
693 | { |
---|
694 | const int status = nc_get_var1(ncid, varid, coordp, value); |
---|
695 | A_FREE(coordp); |
---|
696 | if(status != NC_NOERR) |
---|
697 | { |
---|
698 | nc_advise("ncdimid", status, "ncid %d", ncid); |
---|
699 | return -1; |
---|
700 | } |
---|
701 | } |
---|
702 | return 0; |
---|
703 | } |
---|
704 | |
---|
705 | |
---|
706 | int |
---|
707 | ncvarput( |
---|
708 | int ncid, |
---|
709 | int varid, |
---|
710 | const long* start, |
---|
711 | const long* count, |
---|
712 | const void* value |
---|
713 | ) |
---|
714 | { |
---|
715 | NDIMS_DECL |
---|
716 | A_DECL(stp, size_t, ndims, start); |
---|
717 | A_DECL(cntp, size_t, ndims, count); |
---|
718 | A_INIT(stp, size_t, ndims, start); |
---|
719 | A_INIT(cntp, size_t, ndims, count); |
---|
720 | { |
---|
721 | const int status = nc_put_vara(ncid, varid, stp, cntp, value); |
---|
722 | A_FREE(cntp); |
---|
723 | A_FREE(stp); |
---|
724 | if(status != NC_NOERR) |
---|
725 | { |
---|
726 | nc_advise("ncvarput", status, "ncid %d", ncid); |
---|
727 | return -1; |
---|
728 | } |
---|
729 | } |
---|
730 | return 0; |
---|
731 | } |
---|
732 | |
---|
733 | |
---|
734 | int |
---|
735 | ncvarget( |
---|
736 | int ncid, |
---|
737 | int varid, |
---|
738 | const long* start, |
---|
739 | const long* count, |
---|
740 | void* value |
---|
741 | ) |
---|
742 | { |
---|
743 | NDIMS_DECL |
---|
744 | A_DECL(stp, size_t, ndims, start); |
---|
745 | A_DECL(cntp, size_t, ndims, count); |
---|
746 | A_INIT(stp, size_t, ndims, start); |
---|
747 | A_INIT(cntp, size_t, ndims, count); |
---|
748 | { |
---|
749 | const int status = nc_get_vara(ncid, varid, stp, cntp, value); |
---|
750 | A_FREE(cntp); |
---|
751 | A_FREE(stp); |
---|
752 | if(status != NC_NOERR) |
---|
753 | { |
---|
754 | nc_advise("ncvarget", status, "ncid %d; varid %d", ncid, varid); |
---|
755 | return -1; |
---|
756 | } |
---|
757 | } |
---|
758 | return 0; |
---|
759 | } |
---|
760 | |
---|
761 | |
---|
762 | int |
---|
763 | ncvarputs( |
---|
764 | int ncid, |
---|
765 | int varid, |
---|
766 | const long* start, |
---|
767 | const long* count, |
---|
768 | const long* stride, |
---|
769 | const void* value |
---|
770 | ) |
---|
771 | { |
---|
772 | if(stride == NULL) |
---|
773 | return ncvarput(ncid, varid, start, count, value); |
---|
774 | /* else */ |
---|
775 | { |
---|
776 | NDIMS_DECL |
---|
777 | A_DECL(stp, size_t, ndims, start); |
---|
778 | A_DECL(cntp, size_t, ndims, count); |
---|
779 | A_DECL(strdp, ptrdiff_t, ndims, stride); |
---|
780 | A_INIT(stp, size_t, ndims, start); |
---|
781 | A_INIT(cntp, size_t, ndims, count); |
---|
782 | A_INIT(strdp, ptrdiff_t, ndims, stride); |
---|
783 | { |
---|
784 | const int status = nc_put_vars(ncid, varid, stp, cntp, strdp, value); |
---|
785 | A_FREE(strdp); |
---|
786 | A_FREE(cntp); |
---|
787 | A_FREE(stp); |
---|
788 | if(status != NC_NOERR) |
---|
789 | { |
---|
790 | nc_advise("ncvarputs", status, "ncid %d", ncid); |
---|
791 | return -1; |
---|
792 | } |
---|
793 | } |
---|
794 | return 0; |
---|
795 | } |
---|
796 | } |
---|
797 | |
---|
798 | |
---|
799 | int |
---|
800 | ncvargets( |
---|
801 | int ncid, |
---|
802 | int varid, |
---|
803 | const long* start, |
---|
804 | const long* count, |
---|
805 | const long* stride, |
---|
806 | void* value |
---|
807 | ) |
---|
808 | { |
---|
809 | if(stride == NULL) |
---|
810 | return ncvarget(ncid, varid, start, count, value); |
---|
811 | /* else */ |
---|
812 | { |
---|
813 | NDIMS_DECL |
---|
814 | A_DECL(stp, size_t, ndims, start); |
---|
815 | A_DECL(cntp, size_t, ndims, count); |
---|
816 | A_DECL(strdp, ptrdiff_t, ndims, stride); |
---|
817 | A_INIT(stp, size_t, ndims, start); |
---|
818 | A_INIT(cntp, size_t, ndims, count); |
---|
819 | A_INIT(strdp, ptrdiff_t, ndims, stride); |
---|
820 | { |
---|
821 | const int status = nc_get_vars(ncid, varid, stp, cntp, strdp, value); |
---|
822 | A_FREE(strdp); |
---|
823 | A_FREE(cntp); |
---|
824 | A_FREE(stp); |
---|
825 | if(status != NC_NOERR) |
---|
826 | { |
---|
827 | nc_advise("ncvargets", status, "ncid %d", ncid); |
---|
828 | return -1; |
---|
829 | } |
---|
830 | } |
---|
831 | return 0; |
---|
832 | } |
---|
833 | } |
---|
834 | |
---|
835 | |
---|
836 | int |
---|
837 | ncvarputg( |
---|
838 | int ncid, |
---|
839 | int varid, |
---|
840 | const long* start, |
---|
841 | const long* count, |
---|
842 | const long* stride, |
---|
843 | const long* map, |
---|
844 | const void* value |
---|
845 | ) |
---|
846 | { |
---|
847 | if(map == NULL) |
---|
848 | return ncvarputs(ncid, varid, start, count, stride, value); |
---|
849 | /* else */ |
---|
850 | { |
---|
851 | NDIMS_DECL |
---|
852 | A_DECL(stp, size_t, ndims, start); |
---|
853 | A_DECL(cntp, size_t, ndims, count); |
---|
854 | A_DECL(strdp, ptrdiff_t, ndims, stride); |
---|
855 | A_DECL(imp, ptrdiff_t, ndims, map); |
---|
856 | A_INIT(stp, size_t, ndims, start); |
---|
857 | A_INIT(cntp, size_t, ndims, count); |
---|
858 | A_INIT(strdp, ptrdiff_t, ndims, stride); |
---|
859 | A_INIT(imp, ptrdiff_t, ndims, map); |
---|
860 | { |
---|
861 | const int status = nc_put_varm(ncid, varid, |
---|
862 | stp, cntp, strdp, imp, value); |
---|
863 | A_FREE(imp); |
---|
864 | A_FREE(strdp); |
---|
865 | A_FREE(cntp); |
---|
866 | A_FREE(stp); |
---|
867 | if(status != NC_NOERR) |
---|
868 | { |
---|
869 | nc_advise("ncvarputg", status, "ncid %d", ncid); |
---|
870 | return -1; |
---|
871 | } |
---|
872 | } |
---|
873 | return 0; |
---|
874 | } |
---|
875 | } |
---|
876 | |
---|
877 | |
---|
878 | int |
---|
879 | ncvargetg( |
---|
880 | int ncid, |
---|
881 | int varid, |
---|
882 | const long* start, |
---|
883 | const long* count, |
---|
884 | const long* stride, |
---|
885 | const long* map, |
---|
886 | void* value |
---|
887 | ) |
---|
888 | { |
---|
889 | if(map == NULL) |
---|
890 | return ncvargets(ncid, varid, start, count, stride, value); |
---|
891 | /* else */ |
---|
892 | { |
---|
893 | NDIMS_DECL |
---|
894 | A_DECL(stp, size_t, ndims, start); |
---|
895 | A_DECL(cntp, size_t, ndims, count); |
---|
896 | A_DECL(strdp, ptrdiff_t, ndims, stride); |
---|
897 | A_DECL(imp, ptrdiff_t, ndims, map); |
---|
898 | A_INIT(stp, size_t, ndims, start); |
---|
899 | A_INIT(cntp, size_t, ndims, count); |
---|
900 | A_INIT(strdp, ptrdiff_t, ndims, stride); |
---|
901 | A_INIT(imp, ptrdiff_t, ndims, map); |
---|
902 | { |
---|
903 | const int status = nc_get_varm(ncid, varid, |
---|
904 | stp, cntp, strdp, imp, value); |
---|
905 | A_FREE(imp); |
---|
906 | A_FREE(strdp); |
---|
907 | A_FREE(cntp); |
---|
908 | A_FREE(stp); |
---|
909 | if(status != NC_NOERR) |
---|
910 | { |
---|
911 | nc_advise("ncvargetg", status, "ncid %d", ncid); |
---|
912 | return -1; |
---|
913 | } |
---|
914 | } |
---|
915 | return 0; |
---|
916 | } |
---|
917 | } |
---|
918 | |
---|
919 | |
---|
920 | int |
---|
921 | ncvarrename( |
---|
922 | int ncid, |
---|
923 | int varid, |
---|
924 | const char* name |
---|
925 | ) |
---|
926 | { |
---|
927 | const int status = nc_rename_var(ncid, varid, name); |
---|
928 | if(status != NC_NOERR) |
---|
929 | { |
---|
930 | nc_advise("ncvarrename", status, "ncid %d", ncid); |
---|
931 | return -1; |
---|
932 | } |
---|
933 | return varid; |
---|
934 | } |
---|
935 | |
---|
936 | |
---|
937 | int |
---|
938 | ncattput( |
---|
939 | int ncid, |
---|
940 | int varid, |
---|
941 | const char* name, |
---|
942 | nc_type datatype, |
---|
943 | int len, |
---|
944 | const void* value |
---|
945 | ) |
---|
946 | { |
---|
947 | const int status = nc_put_att(ncid, varid, name, datatype, len, value); |
---|
948 | if(status != NC_NOERR) |
---|
949 | { |
---|
950 | nc_advise("ncattput", status, "ncid %d", ncid); |
---|
951 | return -1; |
---|
952 | } |
---|
953 | return 0; |
---|
954 | } |
---|
955 | |
---|
956 | |
---|
957 | int |
---|
958 | ncattinq( |
---|
959 | int ncid, |
---|
960 | int varid, |
---|
961 | const char* name, |
---|
962 | nc_type* datatype, |
---|
963 | int* len |
---|
964 | ) |
---|
965 | { |
---|
966 | size_t ll; |
---|
967 | const int status = nc_inq_att(ncid, varid, name, datatype, &ll); |
---|
968 | if(status != NC_NOERR) |
---|
969 | { |
---|
970 | nc_advise("ncattinq", status, |
---|
971 | "ncid %d; varid %d; attname \"%s\"", |
---|
972 | ncid, varid, name); |
---|
973 | return -1; |
---|
974 | } |
---|
975 | |
---|
976 | if(len != NULL) |
---|
977 | *len = (int) ll; |
---|
978 | |
---|
979 | return 1; |
---|
980 | |
---|
981 | } |
---|
982 | |
---|
983 | |
---|
984 | int |
---|
985 | ncattget( |
---|
986 | int ncid, |
---|
987 | int varid, |
---|
988 | const char* name, |
---|
989 | void* value |
---|
990 | ) |
---|
991 | { |
---|
992 | const int status = nc_get_att(ncid, varid, name, value); |
---|
993 | if(status != NC_NOERR) |
---|
994 | { |
---|
995 | nc_advise("ncattget", status, "ncid %d", ncid); |
---|
996 | return -1; |
---|
997 | } |
---|
998 | return 1; |
---|
999 | } |
---|
1000 | |
---|
1001 | |
---|
1002 | int |
---|
1003 | ncattcopy( |
---|
1004 | int ncid_in, |
---|
1005 | int varid_in, |
---|
1006 | const char* name, |
---|
1007 | int ncid_out, |
---|
1008 | int varid_out |
---|
1009 | ) |
---|
1010 | { |
---|
1011 | const int status = nc_copy_att(ncid_in, varid_in, name, ncid_out, varid_out); |
---|
1012 | if(status != NC_NOERR) |
---|
1013 | { |
---|
1014 | nc_advise("ncattcopy", status, "%s", name); |
---|
1015 | return -1; |
---|
1016 | } |
---|
1017 | return 0; |
---|
1018 | } |
---|
1019 | |
---|
1020 | |
---|
1021 | int |
---|
1022 | ncattname( |
---|
1023 | int ncid, |
---|
1024 | int varid, |
---|
1025 | int attnum, |
---|
1026 | char* name |
---|
1027 | ) |
---|
1028 | { |
---|
1029 | const int status = nc_inq_attname(ncid, varid, attnum, name); |
---|
1030 | if(status != NC_NOERR) |
---|
1031 | { |
---|
1032 | nc_advise("ncattname", status, "ncid %d", ncid); |
---|
1033 | return -1; |
---|
1034 | } |
---|
1035 | return attnum; |
---|
1036 | } |
---|
1037 | |
---|
1038 | |
---|
1039 | int |
---|
1040 | ncattrename( |
---|
1041 | int ncid, |
---|
1042 | int varid, |
---|
1043 | const char* name, |
---|
1044 | const char* newname |
---|
1045 | ) |
---|
1046 | { |
---|
1047 | const int status = nc_rename_att(ncid, varid, name, newname); |
---|
1048 | if(status != NC_NOERR) |
---|
1049 | { |
---|
1050 | nc_advise("ncattrename", status, "ncid %d", ncid); |
---|
1051 | return -1; |
---|
1052 | } |
---|
1053 | return 1; |
---|
1054 | } |
---|
1055 | |
---|
1056 | |
---|
1057 | int |
---|
1058 | ncattdel( |
---|
1059 | int ncid, |
---|
1060 | int varid, |
---|
1061 | const char* name |
---|
1062 | ) |
---|
1063 | { |
---|
1064 | const int status = nc_del_att(ncid, varid, name); |
---|
1065 | if(status != NC_NOERR) |
---|
1066 | { |
---|
1067 | nc_advise("ncattdel", status, "ncid %d", ncid); |
---|
1068 | return -1; |
---|
1069 | } |
---|
1070 | return 1; |
---|
1071 | } |
---|
1072 | |
---|
1073 | #endif /* NO_NETCDF_2 */ |
---|
1074 | |
---|
1075 | #ifndef NO_NETCDF_2 |
---|
1076 | |
---|
1077 | int |
---|
1078 | ncsetfill( |
---|
1079 | int ncid, |
---|
1080 | int fillmode |
---|
1081 | ) |
---|
1082 | { |
---|
1083 | int oldmode = -1; |
---|
1084 | const int status = nc_set_fill(ncid, fillmode, &oldmode); |
---|
1085 | if(status != NC_NOERR) |
---|
1086 | { |
---|
1087 | nc_advise("ncsetfill", status, "ncid %d", ncid); |
---|
1088 | return -1; |
---|
1089 | } |
---|
1090 | return oldmode; |
---|
1091 | } |
---|
1092 | |
---|
1093 | |
---|
1094 | int |
---|
1095 | ncrecinq( |
---|
1096 | int ncid, |
---|
1097 | int* nrecvars, |
---|
1098 | int* recvarids, |
---|
1099 | long* recsizes |
---|
1100 | ) |
---|
1101 | { |
---|
1102 | size_t nrv = 0; |
---|
1103 | size_t rs[NC_MAX_VARS]; /* TODO */ |
---|
1104 | const int status = nc_inq_rec(ncid, &nrv, recvarids, rs); |
---|
1105 | if(status != NC_NOERR) |
---|
1106 | { |
---|
1107 | nc_advise("ncrecinq", status, "ncid %d", ncid); |
---|
1108 | return -1; |
---|
1109 | } |
---|
1110 | |
---|
1111 | if(nrecvars != NULL) |
---|
1112 | *nrecvars = (int) nrv; |
---|
1113 | |
---|
1114 | if(recsizes != NULL) |
---|
1115 | { |
---|
1116 | size_t ii; |
---|
1117 | for(ii = 0; ii < nrv; ii++) |
---|
1118 | { |
---|
1119 | recsizes[ii] = (long) rs[ii]; |
---|
1120 | } |
---|
1121 | } |
---|
1122 | |
---|
1123 | return (int) nrv; |
---|
1124 | } |
---|
1125 | |
---|
1126 | |
---|
1127 | int |
---|
1128 | ncrecget( |
---|
1129 | int ncid, |
---|
1130 | long recnum, |
---|
1131 | void** datap |
---|
1132 | ) |
---|
1133 | { |
---|
1134 | const int status = nc_get_rec(ncid, (size_t)recnum, datap); |
---|
1135 | if(status != NC_NOERR) |
---|
1136 | { |
---|
1137 | nc_advise("ncrecget", status, "ncid %d", ncid); |
---|
1138 | return -1; |
---|
1139 | } |
---|
1140 | return 0; |
---|
1141 | } |
---|
1142 | |
---|
1143 | |
---|
1144 | int |
---|
1145 | ncrecput( |
---|
1146 | int ncid, |
---|
1147 | long recnum, |
---|
1148 | void* const* datap |
---|
1149 | ) |
---|
1150 | { |
---|
1151 | const int status = nc_put_rec(ncid, (size_t)recnum, datap); |
---|
1152 | if(status != NC_NOERR) |
---|
1153 | { |
---|
1154 | nc_advise("ncrecput", status, "ncid %d", ncid); |
---|
1155 | return -1; |
---|
1156 | } |
---|
1157 | return 0; |
---|
1158 | } |
---|
1159 | |
---|
1160 | #endif /* NO_NETCDF_2 */ |
---|