NetCDF  4.4.0
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  if (recdimid == -1)
251  return NC_NOERR;
252 
253  status = numrecvars(ncid, &nrvars, rvarids);
254  if(status != NC_NOERR)
255  return status;
256 
257  if (nrecvarsp != NULL)
258  *nrecvarsp = (size_t)nrvars;
259 
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  int ndims = 0;
868  if(map == NULL)
869  return ncvarputs(ncid, varid, start, count, stride, value);
870  /* else */
871  {
872  ptrdiff_t *imp=NULL;
873  if (map != NULL) {
874  int ret = NC_NOERR;
875  /* make map[ndims-1] number of elements instead of bytes */
876  int i, el_size;
877  nc_type type;
878  ret = nc_inq_varndims(ncid, varid, &ndims);
879  if(ret) return ret;
880  ret = nc_inq_vartype(ncid, varid, &type);
881  if(ret) return ret;
882  el_size = nctypelen(type);
883  imp = (ptrdiff_t*) malloc(ndims * sizeof(ptrdiff_t));
884  for (i=0; i<ndims; i++) imp[i] = map[i] / el_size;
885  }
886 
887  {
888  A_DECL(stp, size_t, ndims, start);
889  A_DECL(cntp, size_t, ndims, count);
890  A_DECL(strdp, ptrdiff_t, ndims, stride);
891  A_INIT(stp, size_t, ndims, start);
892  A_INIT(cntp, size_t, ndims, count);
893  A_INIT(strdp, ptrdiff_t, ndims, stride);
894  {
895  const int status = nc_put_varm(ncid, varid,
896  stp, cntp, strdp, imp, value);
897  if (imp!=NULL) free(imp);
898  A_FREE(strdp);
899  A_FREE(cntp);
900  A_FREE(stp);
901  if(status != NC_NOERR)
902  {
903  nc_advise("ncvarputg", status, "ncid %d", ncid);
904  return -1;
905  }
906  }
907  return 0;
908  }
909  }
910 }
911 
912 
913 int
914 ncvargetg(
915  int ncid,
916  int varid,
917  const long* start,
918  const long* count,
919  const long* stride,
920  const long* map,
921  void* value
922 )
923 {
924  int ndims = 0;
925  if(map == NULL)
926  return ncvargets(ncid, varid, start, count, stride, value);
927  /* else */
928  {
929  ptrdiff_t *imp=NULL;
930  if (map != NULL) {
931  int ret = NC_NOERR;
932  /* make map[ndims-1] number of elements instead of bytes */
933  int i, el_size;
934  nc_type type;
935  ret = nc_inq_varndims(ncid, varid, &ndims);
936  if(ret) return ret;
937  ret = nc_inq_vartype(ncid, varid, &type);
938  if(ret) return ret;
939  el_size = nctypelen(type);
940  imp = (ptrdiff_t*) malloc(ndims * sizeof(ptrdiff_t));
941  for (i=0; i<ndims; i++) imp[i] = map[i] / el_size;
942  }
943 
944  {
945  A_DECL(stp, size_t, ndims, start);
946  A_DECL(cntp, size_t, ndims, count);
947  A_DECL(strdp, ptrdiff_t, ndims, stride);
948  A_INIT(stp, size_t, ndims, start);
949  A_INIT(cntp, size_t, ndims, count);
950  A_INIT(strdp, ptrdiff_t, ndims, stride);
951  {
952  const int status = nc_get_varm(ncid, varid,
953  stp, cntp, strdp, imp, value);
954  if (imp!=NULL) free(imp);
955  A_FREE(strdp);
956  A_FREE(cntp);
957  A_FREE(stp);
958  if(status != NC_NOERR)
959  {
960  nc_advise("ncvargetg", status, "ncid %d", ncid);
961  return -1;
962  }
963  }
964  return 0;
965  }
966  }
967 }
968 
969 
970 int
971 ncvarrename(
972  int ncid,
973  int varid,
974  const char* name
975 )
976 {
977  const int status = nc_rename_var(ncid, varid, name);
978  if(status != NC_NOERR)
979  {
980  nc_advise("ncvarrename", status, "ncid %d", ncid);
981  return -1;
982  }
983  return varid;
984 }
985 
986 
987 int
988 ncattput(
989  int ncid,
990  int varid,
991  const char* name,
992  nc_type datatype,
993  int len,
994  const void* value
995 )
996 {
997  const int status = nc_put_att(ncid, varid, name, datatype, len, value);
998  if(status != NC_NOERR)
999  {
1000  nc_advise("ncattput", status, "ncid %d", ncid);
1001  return -1;
1002  }
1003  return 0;
1004 }
1005 
1006 
1007 int
1008 ncattinq(
1009  int ncid,
1010  int varid,
1011  const char* name,
1012  nc_type* datatype,
1013  int* len
1014 )
1015 {
1016  size_t ll;
1017  const int status = nc_inq_att(ncid, varid, name, datatype, &ll);
1018  if(status != NC_NOERR)
1019  {
1020  nc_advise("ncattinq", status,
1021  "ncid %d; varid %d; attname \"%s\"",
1022  ncid, varid, name);
1023  return -1;
1024  }
1025 
1026  if(len != NULL)
1027  *len = (int) ll;
1028 
1029  return 1;
1030 
1031 }
1032 
1033 
1034 int
1035 ncattget(
1036  int ncid,
1037  int varid,
1038  const char* name,
1039  void* value
1040 )
1041 {
1042  const int status = nc_get_att(ncid, varid, name, value);
1043  if(status != NC_NOERR)
1044  {
1045  nc_advise("ncattget", status, "ncid %d", ncid);
1046  return -1;
1047  }
1048  return 1;
1049 }
1050 
1051 
1052 int
1053 ncattcopy(
1054  int ncid_in,
1055  int varid_in,
1056  const char* name,
1057  int ncid_out,
1058  int varid_out
1059 )
1060 {
1061  const int status = nc_copy_att(ncid_in, varid_in, name, ncid_out, varid_out);
1062  if(status != NC_NOERR)
1063  {
1064  nc_advise("ncattcopy", status, "%s", name);
1065  return -1;
1066  }
1067  return 0;
1068 }
1069 
1070 
1071 int
1072 ncattname(
1073  int ncid,
1074  int varid,
1075  int attnum,
1076  char* name
1077 )
1078 {
1079  const int status = nc_inq_attname(ncid, varid, attnum, name);
1080  if(status != NC_NOERR)
1081  {
1082  nc_advise("ncattname", status, "ncid %d", ncid);
1083  return -1;
1084  }
1085  return attnum;
1086 }
1087 
1088 
1089 int
1090 ncattrename(
1091  int ncid,
1092  int varid,
1093  const char* name,
1094  const char* newname
1095 )
1096 {
1097  const int status = nc_rename_att(ncid, varid, name, newname);
1098  if(status != NC_NOERR)
1099  {
1100  nc_advise("ncattrename", status, "ncid %d", ncid);
1101  return -1;
1102  }
1103  return 1;
1104 }
1105 
1106 
1107 int
1108 ncattdel(
1109  int ncid,
1110  int varid,
1111  const char* name
1112 )
1113 {
1114  const int status = nc_del_att(ncid, varid, name);
1115  if(status != NC_NOERR)
1116  {
1117  nc_advise("ncattdel", status, "ncid %d", ncid);
1118  return -1;
1119  }
1120  return 1;
1121 }
1122 
1123 #endif /* NO_NETCDF_2 */
1124 
1125 #ifndef NO_NETCDF_2
1126 
1127 int
1128 ncsetfill(
1129  int ncid,
1130  int fillmode
1131 )
1132 {
1133  int oldmode = -1;
1134  const int status = nc_set_fill(ncid, fillmode, &oldmode);
1135  if(status != NC_NOERR)
1136  {
1137  nc_advise("ncsetfill", status, "ncid %d", ncid);
1138  return -1;
1139  }
1140  return oldmode;
1141 }
1142 
1143 
1144 int
1145 ncrecinq(
1146  int ncid,
1147  int* nrecvars,
1148  int* recvarids,
1149  long* recsizes
1150 )
1151 {
1152  size_t nrv = 0;
1153  size_t *rs = NULL;
1154  int status = NC_NOERR;
1155 
1156  rs = (size_t*)malloc(sizeof(size_t)*NC_MAX_VARS);
1157  if(rs == NULL)
1158  return NC_ENOMEM;
1159 
1160  status = nc_inq_rec(ncid, &nrv, recvarids, rs);
1161  if(status != NC_NOERR)
1162  {
1163  nc_advise("ncrecinq", status, "ncid %d", ncid);
1164  if(rs != NULL) free(rs);
1165  return -1;
1166  }
1167 
1168  if(nrecvars != NULL)
1169  *nrecvars = (int) nrv;
1170 
1171  if(recsizes != NULL)
1172  {
1173  size_t ii;
1174  for(ii = 0; ii < nrv; ii++)
1175  {
1176  recsizes[ii] = (long) rs[ii];
1177  }
1178  }
1179 
1180  if(rs != NULL) free(rs);
1181 
1182  return (int) nrv;
1183 }
1184 
1185 
1186 int
1187 ncrecget(
1188  int ncid,
1189  long recnum,
1190  void** datap
1191 )
1192 {
1193  const int status = nc_get_rec(ncid, (size_t)recnum, datap);
1194  if(status != NC_NOERR)
1195  {
1196  nc_advise("ncrecget", status, "ncid %d", ncid);
1197  return -1;
1198  }
1199  return 0;
1200 }
1201 
1202 
1203 int
1204 ncrecput(
1205  int ncid,
1206  long recnum,
1207  void* const* datap
1208 )
1209 {
1210  const int status = nc_put_rec(ncid, (size_t)recnum, datap);
1211  if(status != NC_NOERR)
1212  {
1213  nc_advise("ncrecput", status, "ncid %d", ncid);
1214  return -1;
1215  }
1216  return 0;
1217 }
1218 
1219 #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:1799
#define NC_ENOMEM
Memory allocation (malloc) failure.
Definition: netcdf.h:395
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_def_dim(int ncid, const char *name, size_t len, int *idp)
Define a new dimension.
Definition: ddim.c:123
EXTERNL int nc_redef(int ncid)
Put open netcdf dataset into define mode.
Definition: dfile.c:840
EXTERNL int nc_rename_dim(int ncid, int dimid, const char *name)
Rename a dimension.
Definition: ddim.c:276
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:573
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:215
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:1349
EXTERNL int nc_inq(int ncid, int *ndimsp, int *nvarsp, int *nattsp, int *unlimdimidp)
Inquire about a file or group.
Definition: dfile.c:1454
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:28
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:1109
#define NC_EDIMSIZE
Invalid dimension size.
Definition: netcdf.h:397
#define MAX_NC_VARS
Backward compatible alias.
Definition: netcdf.h:1801
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:1182
EXTERNL int nc_inq_dimlen(int ncid, int dimid, size_t *lenp)
Find the length of a dimension.
Definition: ddim.c:450
EXTERNL int nc_get_att(int ncid, int varid, const char *name, void *ip)
Get an attribute of any type.
Definition: dattget.c:44
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:822
EXTERNL int nc_put_var1(int ncid, int varid, const size_t *indexp, const void *op)
Write one datum.
Definition: dvarput.c:763
EXTERNL int nc_set_fill(int ncid, int fillmode, int *old_modep)
Change the fill-value mode to improve write performance.
Definition: dfile.c:1300
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:229
#define NC_ISSYSERR(err)
The netcdf version 3 functions all return integer error status.
Definition: netcdf.h:313
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_inq_dimid(int ncid, const char *name, int *idp)
Find the ID of a dimension from the name.
Definition: ddim.c:152
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:1183
EXTERNL int nc_inq_unlimdim(int ncid, int *unlimdimidp)
Find the ID of the unlimited dimension.
Definition: ddim.c:336
#define NC_MAX_VARS
Maximum for classic library.
Definition: netcdf.h:267
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:315
EXTERNL int nc_open(const char *path, int mode, int *ncidp)
Open an existing netCDF file.
Definition: dfile.c:608
EXTERNL int nc_enddef(int ncid)
Leave define mode.
Definition: dfile.c:904
EXTERNL const char * nc_strerror(int ncerr)
Given an error number, return an error message.
Definition: derror.c:88
EXTERNL int nc_sync(int ncid)
Synchronize an open netcdf dataset to disk.
Definition: dfile.c:1072
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:626
EXTERNL int nc_create(const char *path, int cmode, int *ncidp)
Create a new netCDF file.
Definition: dfile.c:403
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 Thu Jan 21 2016 19:12:45 for NetCDF. NetCDF is a Unidata library.