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

Return to the Main Unidata NetCDF page.
Generated on Sun Nov 23 2014 16:17:49 for NetCDF. NetCDF is a Unidata library.