NetCDF  4.8.1
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 
36 
37 int ncerr = NC_NOERR ;
39 #if SIZEOF_LONG == SIZEOF_SIZE_T
40 /*
41  * We don't have to copy the arguments to switch from 'long'
42  * to 'size_t' or 'ptrdiff_t'. Use dummy macros.
43  */
44 
45 # define NDIMS_DECL
48 # define A_DECL(name, type, ndims, rhs) \
49  const type *const name = ((const type *)(rhs))
50 
51 # define A_FREE(name)
53 # define A_INIT(lhs, type, ndims, rhs)
55 #else
56 /*
57  * We do have to copy the arguments to switch from 'long'
58  * to 'size_t' or 'ptrdiff_t'. In my tests on an SGI,
59  * any additional cost was lost in measurement variation.
60  *
61  * This stanza is true on Windows with MinGW-64
62  */
63 
64 # include "onstack.h"
65 
66 static int
67 nvdims(int ncid, int varid)
68 {
69  int ndims=-1, status;
70 
71  if ((status = nc_inq_varndims(ncid, varid, &ndims)))
72  {
73  nc_advise("ncvdims", status, "ncid %d", ncid);
74  return -1;
75  }
76  return ndims;
77 }
78 
79 /* Used to avoid errors on 64-bit windows related to
80  c89 macros and flow control/conditionals. */
81 static void* nvmalloc(off_t size) {
82  if(size < 0)
83  return NULL;
84 
85  return malloc(size);
86 
87 }
88 
89 #define NDIMS_DECL const int ndims = nvdims(ncid, varid); \
90 
91 
92 # define A_DECL(name, type, ndims, rhs) \
93  type *const name = (type*) nvmalloc((ndims) * sizeof(type))
94 
95 
96 #if 0
97  ALLOC_ONSTACK(name, type, ndims)
98 #endif
99 
100 # define A_FREE(name) \
101  FREE_ONSTACK(name)
102 
103 # define A_INIT(lhs, type, ndims, rhs) \
104  { \
105  if((off_t)ndims >= 0) { \
106  const long *lp = rhs; \
107  type *tp = lhs; \
108  type *const end = lhs + ndims; \
109  while(tp < end) \
110  { \
111  *tp++ = (type) *lp++; \
112  } \
113  } \
114  } \
115  \
116  if ((off_t)ndims < 0) {nc_advise("nvdims",NC_EMAXDIMS,"ndims %d",ndims); return -1;}
117 
118 
119 #endif
120 
121 typedef signed char schar;
135 static int
136 numrecvars(int ncid, int* nrecvarsp, int *recvarids)
137 {
138  int status = NC_NOERR;
139  int nvars = 0;
140  int ndims = 0;
141  int nrecvars = 0;
142  int varid;
143  int recdimid;
144  int dimids[MAX_NC_DIMS];
145 
146  status = nc_inq_nvars(ncid, &nvars);
147  if(status != NC_NOERR)
148  return status;
149 
150  status = nc_inq_unlimdim(ncid, &recdimid);
151  if(status != NC_NOERR)
152  return status;
153 
154  if (recdimid == -1) {
155  *nrecvarsp = 0;
156  return NC_NOERR;
157  }
158  nrecvars = 0;
159  for (varid = 0; varid < nvars; varid++) {
160  status = nc_inq_varndims(ncid, varid, &ndims);
161  if(status != NC_NOERR)
162  return status;
163  status = nc_inq_vardimid(ncid, varid, dimids);
164  if(status != NC_NOERR)
165  return status;
166  if (ndims > 0 && dimids[0] == recdimid) {
167  if (recvarids != NULL)
168  recvarids[nrecvars] = varid;
169  nrecvars++;
170  }
171  }
172  *nrecvarsp = nrecvars;
173  return NC_NOERR;
174 }
175 
176 
187 static int
188 ncrecsize(int ncid, int varid, size_t *recsizep)
189 {
190  int status = NC_NOERR;
191  int recdimid;
192  nc_type type;
193  int ndims;
194  int dimids[MAX_NC_DIMS];
195  int id;
196  int size;
197 
198  *recsizep = 0;
199  status = nc_inq_unlimdim(ncid, &recdimid);
200  if(status != NC_NOERR)
201  return status;
202  status = nc_inq_vartype(ncid, varid, &type);
203  if(status != NC_NOERR)
204  return status;
205  status = nc_inq_varndims(ncid, varid, &ndims);
206  if(status != NC_NOERR)
207  return status;
208  status = nc_inq_vardimid(ncid, varid, dimids);
209  if(status != NC_NOERR)
210  return status;
211  if (ndims == 0 || dimids[0] != recdimid) {
212  return NC_NOERR;
213  }
214  size = nctypelen(type);
215  for (id = 1; id < ndims; id++) {
216  size_t len;
217  status = nc_inq_dimlen(ncid, dimids[id], &len);
218  if(status != NC_NOERR)
219  return status;
220  size *= (int)len;
221  }
222  *recsizep = (size_t)size;
223  return NC_NOERR;
224 }
225 
226 
239 static int
240 dimsizes(int ncid, int varid, size_t *sizes)
241 {
242  int status = NC_NOERR;
243  int ndims;
244  int id;
245  int dimids[MAX_NC_DIMS];
246 
247  status = nc_inq_varndims(ncid, varid, &ndims);
248  if(status != NC_NOERR)
249  return status;
250  status = nc_inq_vardimid(ncid, varid, dimids);
251  if(status != NC_NOERR)
252  return status;
253  if (ndims == 0 || sizes == NULL)
254  return NC_NOERR;
255  for (id = 0; id < ndims; id++) {
256  size_t len;
257  status = nc_inq_dimlen(ncid, dimids[id], &len);
258  if(status != NC_NOERR)
259  return status;
260  sizes[id] = len;
261  }
262  return NC_NOERR;
263 }
264 
288 int
290  int ncid,
291  size_t *nrecvarsp,
292  int *recvarids,
293  size_t *recsizes)
294 {
295  int status = NC_NOERR;
296  int nvars = 0;
297  int recdimid;
298  int varid;
299  int rvarids[MAX_NC_VARS];
300  int nrvars = 0;
301 
302  status = nc_inq_nvars(ncid, &nvars);
303  if(status != NC_NOERR)
304  return status;
305 
306  status = nc_inq_unlimdim(ncid, &recdimid);
307  if(status != NC_NOERR)
308  return status;
309 
310  if (recdimid == -1)
311  return NC_NOERR;
312 
313  status = numrecvars(ncid, &nrvars, rvarids);
314  if(status != NC_NOERR)
315  return status;
316 
317  if (nrecvarsp != NULL)
318  *nrecvarsp = (size_t)nrvars;
319 
320  if (recvarids != NULL)
321  for (varid = 0; varid < nrvars; varid++)
322  recvarids[varid] = rvarids[varid];
323 
324  if (recsizes != NULL)
325  for (varid = 0; varid < nrvars; varid++) {
326  size_t rsize;
327  status = ncrecsize(ncid, rvarids[varid], &rsize);
328  if (status != NC_NOERR)
329  return status;
330  recsizes[varid] = rsize;
331  }
332  return NC_NOERR;
333 }
334 
354 int
356  int ncid,
357  size_t recnum,
358  void* const* datap)
359 {
360  int status = NC_NOERR;
361  int varid;
362  int rvarids[MAX_NC_VARS];
363  int nrvars;
364  size_t start[MAX_NC_DIMS];
365  size_t edges[MAX_NC_DIMS];
366 
367  status = numrecvars(ncid, &nrvars, rvarids);
368  if(status != NC_NOERR)
369  return status;
370 
371  if (nrvars == 0)
372  return NC_NOERR;
373 
374  start[0] = recnum;
375  for (varid = 1; varid < nrvars; varid++)
376  start[varid] = 0;
377 
378  for (varid = 0; varid < nrvars; varid++) {
379  if (datap[varid] != NULL) {
380  status = dimsizes(ncid, rvarids[varid], edges);
381  if(status != NC_NOERR)
382  return status;
383 
384  edges[0] = 1; /* only 1 record's worth */
385  status = nc_put_vara(ncid, rvarids[varid], start, edges, datap[varid]);
386  if(status != NC_NOERR)
387  return status;
388  }
389  }
390  return 0;
391 }
392 
393 
415 int
417  int ncid,
418  size_t recnum,
419  void **datap)
420 {
421  int status = NC_NOERR;
422  int varid;
423  int rvarids[MAX_NC_VARS];
424  int nrvars;
425  size_t start[MAX_NC_DIMS];
426  size_t edges[MAX_NC_DIMS];
427 
428  status = numrecvars(ncid, &nrvars, rvarids);
429  if(status != NC_NOERR)
430  return status;
431 
432  if (nrvars == 0)
433  return NC_NOERR;
434 
435  start[0] = recnum;
436  for (varid = 1; varid < nrvars; varid++)
437  start[varid] = 0;
438 
439  for (varid = 0; varid < nrvars; varid++) {
440  if (datap[varid] != NULL) {
441  status = dimsizes(ncid, rvarids[varid], edges);
442  if(status != NC_NOERR)
443  return status;
444  edges[0] = 1; /* only 1 record's worth */
445  status = nc_get_vara(ncid, rvarids[varid], start, edges, datap[varid]);
446  if(status != NC_NOERR)
447  return status;
448  }
449  }
450  return 0;
451 }
452 
465 void
466 nc_advise(const char *routine_name, int err, const char *fmt,...)
467 {
468  va_list args;
469 
470  if(NC_ISSYSERR(err))
471  ncerr = NC_SYSERR;
472  else
473  ncerr = err;
474 
475  if( ncopts & NC_VERBOSE )
476  {
477  (void) fprintf(stderr,"%s: ", routine_name);
478  va_start(args ,fmt);
479  (void) vfprintf(stderr,fmt,args);
480  va_end(args);
481  if(err != NC_NOERR)
482  {
483  (void) fprintf(stderr,": %s",
484  nc_strerror(err));
485  }
486  (void) fputc('\n',stderr);
487  (void) fflush(stderr); /* to ensure log files are current */
488  }
489 
490  if( (ncopts & NC_FATAL) && err != NC_NOERR )
491  {
492  exit(ncopts);
493  }
494 }
495 
496 /* End error handling */
497 
510 int
511 nccreate(const char* path, int cmode)
512 {
513  int ncid;
514  const int status = nc_create(path, cmode, &ncid);
515  if(status != NC_NOERR)
516  {
517  nc_advise("nccreate", status, "filename \"%s\"", path);
518  return -1;
519  }
520  return ncid;
521 }
522 
535 int
536 ncopen(const char *path, int mode)
537 {
538  int ncid;
539  const int status = nc_open(path, mode, &ncid);
540  if(status != NC_NOERR)
541  {
542  nc_advise("ncopen", status, "filename \"%s\"", path);
543  return -1;
544  }
545  return ncid;
546 }
547 
559 int
560 ncredef(int ncid)
561 {
562  const int status = nc_redef(ncid);
563  if(status != NC_NOERR)
564  {
565  nc_advise("ncredef", status, "ncid %d", ncid);
566  return -1;
567  }
568  return 0;
569 }
570 
582 int
583 ncendef(int ncid)
584 {
585  const int status = nc_enddef(ncid);
586  if(status != NC_NOERR)
587  {
588  nc_advise("ncendef", status, "ncid %d", ncid);
589  return -1;
590  }
591  return 0;
592 }
593 
605 int
606 ncclose(int ncid)
607 {
608  const int status = nc_close(ncid);
609  if(status != NC_NOERR)
610  {
611  nc_advise("ncclose", status, "ncid %d", ncid);
612  return -1;
613 
614  }
615  return 0;
616 }
617 
634 int
636  int ncid,
637  int* ndims,
638  int* nvars,
639  int* natts,
640  int* recdim
641 )
642 {
643  int nd, nv, na;
644  const int status = nc_inq(ncid, &nd, &nv, &na, recdim);
645 
646  if(status != NC_NOERR)
647  {
648  nc_advise("ncinquire", status, "ncid %d", ncid);
649  return -1;
650  }
651  /* else */
652 
653  if(ndims != NULL)
654  *ndims = (int) nd;
655 
656  if(nvars != NULL)
657  *nvars = (int) nv;
658 
659  if(natts != NULL)
660  *natts = (int) na;
661 
662  return ncid;
663 }
664 
676 int
677 ncsync(int ncid)
678 {
679  const int status = nc_sync(ncid);
680  if(status != NC_NOERR)
681  {
682  nc_advise("ncsync", status, "ncid %d", ncid);
683  return -1;
684 
685  }
686  return 0;
687 }
688 
699 int
700 ncabort(int ncid)
701 {
702  const int status = nc_abort(ncid);
703  if(status != NC_NOERR)
704  {
705  nc_advise("ncabort", status, "ncid %d", ncid);
706  return -1;
707  }
708  return 0;
709 }
710 
725 int
727  int ncid,
728  const char* name,
729  long length
730 )
731 {
732  int dimid;
733  int status = NC_NOERR;
734  if(length < 0) {
735  status = NC_EDIMSIZE;
736  nc_advise("ncdimdef", status, "ncid %d", ncid);
737  return -1;
738  }
739  status = nc_def_dim(ncid, name, (size_t)length, &dimid);
740  if(status != NC_NOERR)
741  {
742  nc_advise("ncdimdef", status, "ncid %d", ncid);
743  return -1;
744  }
745  return dimid;
746 }
747 
760 int
761 ncdimid(int ncid, const char* name)
762 {
763  int dimid;
764  const int status = nc_inq_dimid(ncid, name, &dimid);
765  if(status != NC_NOERR)
766  {
767  nc_advise("ncdimid", status, "ncid %d", ncid);
768  return -1;
769  }
770  return dimid;
771 }
772 
787 int
789  int ncid,
790  int dimid,
791  char* name,
792  long* length
793 )
794 {
795  size_t ll;
796  const int status = nc_inq_dim(ncid, dimid, name, &ll);
797 
798  if(status != NC_NOERR)
799  {
800  nc_advise("ncdiminq", status, "ncid %d", ncid);
801  return -1;
802  }
803  /* else */
804 
805  if(length != NULL)
806  *length = (int) ll;
807 
808  return dimid;
809 }
810 
824 int
826  int ncid,
827  int dimid,
828  const char* name
829 )
830 {
831  const int status = nc_rename_dim(ncid, dimid, name);
832  if(status != NC_NOERR)
833  {
834  nc_advise("ncdimrename", status, "ncid %d", ncid);
835  return -1;
836  }
837  return dimid;
838 }
839 
855 int
857  int ncid,
858  const char* name,
859  nc_type datatype,
860  int ndims,
861  const int* dim
862 )
863 {
864  int varid = -1;
865  const int status = nc_def_var(ncid, name, datatype, ndims, dim, &varid);
866  if(status != NC_NOERR)
867  {
868  nc_advise("ncvardef", status, "ncid %d", ncid);
869  return -1;
870  }
871  return varid;
872 }
873 
886 int
888  int ncid,
889  const char* name
890 )
891 {
892  int varid = -1;
893  const int status = nc_inq_varid(ncid, name, &varid);
894  if(status != NC_NOERR)
895  {
896  nc_advise("ncvarid", status, "ncid %d", ncid);
897  return -1;
898  }
899  return varid;
900 }
901 
919 int
921  int ncid,
922  int varid,
923  char* name,
924  nc_type* datatype,
925  int* ndims,
926  int* dim,
927  int* natts
928 )
929 {
930  int nd, na;
931  const int status = nc_inq_var(ncid, varid, name, datatype,
932  &nd, dim, &na);
933 
934  if(status != NC_NOERR)
935  {
936  nc_advise("ncvarinq", status, "ncid %d", ncid);
937  return -1;
938  }
939  /* else */
940 
941  if(ndims != NULL)
942  *ndims = (int) nd;
943 
944  if(natts != NULL)
945  *natts = (int) na;
946 
947  return varid;
948 }
949 
964 int
966  int ncid,
967  int varid,
968  const long* index,
969  const void* value
970 )
971 {
972  NDIMS_DECL
973  A_DECL(coordp, size_t, (size_t)ndims, index);
974  A_INIT(coordp, size_t, (size_t)ndims, index);
975  {
976  const int status = nc_put_var1(ncid, varid, coordp, value);
977  A_FREE(coordp);
978  if(status != NC_NOERR)
979  {
980  nc_advise("ncvarput1", status, "ncid %d", ncid);
981  return -1;
982  }
983  }
984  return 0;
985 }
986 
1001 int
1003  int ncid,
1004  int varid,
1005  const long* index,
1006  void* value
1007 )
1008 {
1009  NDIMS_DECL
1010  A_DECL(coordp, size_t, ndims, index);
1011  A_INIT(coordp, size_t, ndims, index);
1012  {
1013  const int status = nc_get_var1(ncid, varid, coordp, value);
1014  A_FREE(coordp);
1015  if(status != NC_NOERR)
1016  {
1017  nc_advise("ncdimid", status, "ncid %d", ncid);
1018  return -1;
1019  }
1020  }
1021  return 0;
1022 }
1023 
1039 int
1041  int ncid,
1042  int varid,
1043  const long* start,
1044  const long* count,
1045  const void* value
1046 )
1047 {
1048  NDIMS_DECL
1049  A_DECL(stp, size_t, ndims, start);
1050  A_DECL(cntp, size_t, ndims, count);
1051  A_INIT(stp, size_t, ndims, start);
1052  A_INIT(cntp, size_t, ndims, count);
1053  {
1054  const int status = nc_put_vara(ncid, varid, stp, cntp, value);
1055  A_FREE(cntp);
1056  A_FREE(stp);
1057  if(status != NC_NOERR)
1058  {
1059  nc_advise("ncvarput", status, "ncid %d", ncid);
1060  return -1;
1061  }
1062  }
1063  return 0;
1064 }
1065 
1081 int
1083  int ncid,
1084  int varid,
1085  const long* start,
1086  const long* count,
1087  void* value
1088 )
1089 {
1090  NDIMS_DECL
1091  A_DECL(stp, size_t, ndims, start);
1092  A_DECL(cntp, size_t, ndims, count);
1093  A_INIT(stp, size_t, ndims, start);
1094  A_INIT(cntp, size_t, ndims, count);
1095  {
1096  const int status = nc_get_vara(ncid, varid, stp, cntp, value);
1097  A_FREE(cntp);
1098  A_FREE(stp);
1099  if(status != NC_NOERR)
1100  {
1101  nc_advise("ncvarget", status, "ncid %d; varid %d", ncid, varid);
1102  return -1;
1103  }
1104  }
1105  return 0;
1106 }
1107 
1124 int
1126  int ncid,
1127  int varid,
1128  const long* start,
1129  const long* count,
1130  const long* stride,
1131  const void* value
1132 )
1133 {
1134  if(stride == NULL)
1135  return ncvarput(ncid, varid, start, count, value);
1136  /* else */
1137  {
1138 
1139  NDIMS_DECL
1140  A_DECL(stp, size_t, ndims, start);
1141  A_DECL(cntp, size_t, ndims, count);
1142  A_DECL(strdp, ptrdiff_t, ndims, stride);
1143  A_INIT(stp, size_t, ndims, start);
1144  A_INIT(cntp, size_t, ndims, count);
1145  A_INIT(strdp, ptrdiff_t, ndims, stride);
1146  {
1147  const int status = nc_put_vars(ncid, varid, stp, cntp, strdp, value);
1148  A_FREE(strdp);
1149  A_FREE(cntp);
1150  A_FREE(stp);
1151  if(status != NC_NOERR)
1152  {
1153  nc_advise("ncvarputs", status, "ncid %d", ncid);
1154  return -1;
1155  }
1156  }
1157  return 0;
1158  }
1159 }
1160 
1177 int
1179  int ncid,
1180  int varid,
1181  const long* start,
1182  const long* count,
1183  const long* stride,
1184  void* value
1185 )
1186 {
1187  if(stride == NULL)
1188  return ncvarget(ncid, varid, start, count, value);
1189  /* else */
1190  {
1191  NDIMS_DECL
1192  A_DECL(stp, size_t, ndims, start);
1193  A_DECL(cntp, size_t, ndims, count);
1194  A_DECL(strdp, ptrdiff_t, ndims, stride);
1195  A_INIT(stp, size_t, ndims, start);
1196  A_INIT(cntp, size_t, ndims, count);
1197  A_INIT(strdp, ptrdiff_t, ndims, stride);
1198  {
1199  const int status = nc_get_vars(ncid, varid, stp, cntp, strdp, value);
1200  A_FREE(strdp);
1201  A_FREE(cntp);
1202  A_FREE(stp);
1203  if(status != NC_NOERR)
1204  {
1205  nc_advise("ncvargets", status, "ncid %d", ncid);
1206  return -1;
1207  }
1208  }
1209  return 0;
1210  }
1211 }
1212 
1230 int
1232  int ncid,
1233  int varid,
1234  const long* start,
1235  const long* count,
1236  const long* stride,
1237  const long* map,
1238  const void* value
1239 )
1240 {
1241  int ndims = 0;
1242  if(map == NULL)
1243  return ncvarputs(ncid, varid, start, count, stride, value);
1244  /* else */
1245  {
1246  ptrdiff_t *imp=NULL;
1247  if (map != NULL) {
1248  int ret = NC_NOERR;
1249  /* make map[ndims-1] number of elements instead of bytes */
1250  int i, el_size;
1251  nc_type type;
1252  ret = nc_inq_varndims(ncid, varid, &ndims);
1253  if(ret) return ret;
1254  ret = nc_inq_vartype(ncid, varid, &type);
1255  if(ret) return ret;
1256  el_size = nctypelen(type);
1257  imp = (ptrdiff_t*) malloc(ndims * sizeof(ptrdiff_t));
1258  for (i=0; i<ndims; i++) imp[i] = map[i] / el_size;
1259  }
1260 
1261  {
1262  A_DECL(stp, size_t, ndims, start);
1263  A_DECL(cntp, size_t, ndims, count);
1264  A_DECL(strdp, ptrdiff_t, ndims, stride);
1265  A_INIT(stp, size_t, ndims, start);
1266  A_INIT(cntp, size_t, ndims, count);
1267  A_INIT(strdp, ptrdiff_t, ndims, stride);
1268  {
1269  const int status = nc_put_varm(ncid, varid,
1270  stp, cntp, strdp, imp, value);
1271  if (imp!=NULL) free(imp);
1272  A_FREE(strdp);
1273  A_FREE(cntp);
1274  A_FREE(stp);
1275  if(status != NC_NOERR)
1276  {
1277  nc_advise("ncvarputg", status, "ncid %d", ncid);
1278  return -1;
1279  }
1280  }
1281  return 0;
1282  }
1283  }
1284 }
1285 
1303 int
1305  int ncid,
1306  int varid,
1307  const long* start,
1308  const long* count,
1309  const long* stride,
1310  const long* map,
1311  void* value
1312 )
1313 {
1314  int ndims = 0;
1315  if(map == NULL)
1316  return ncvargets(ncid, varid, start, count, stride, value);
1317  /* else */
1318  {
1319  ptrdiff_t *imp=NULL;
1320  if (map != NULL) {
1321  int ret = NC_NOERR;
1322  /* make map[ndims-1] number of elements instead of bytes */
1323  int i, el_size;
1324  nc_type type;
1325  ret = nc_inq_varndims(ncid, varid, &ndims);
1326  if(ret) return ret;
1327  ret = nc_inq_vartype(ncid, varid, &type);
1328  if(ret) return ret;
1329  el_size = nctypelen(type);
1330  imp = (ptrdiff_t*) malloc(ndims * sizeof(ptrdiff_t));
1331  for (i=0; i<ndims; i++) imp[i] = map[i] / el_size;
1332  }
1333 
1334  {
1335  A_DECL(stp, size_t, ndims, start);
1336  A_DECL(cntp, size_t, ndims, count);
1337  A_DECL(strdp, ptrdiff_t, ndims, stride);
1338  A_INIT(stp, size_t, ndims, start);
1339  A_INIT(cntp, size_t, ndims, count);
1340  A_INIT(strdp, ptrdiff_t, ndims, stride);
1341  {
1342  const int status = nc_get_varm(ncid, varid,
1343  stp, cntp, strdp, imp, value);
1344  if (imp!=NULL) free(imp);
1345  A_FREE(strdp);
1346  A_FREE(cntp);
1347  A_FREE(stp);
1348  if(status != NC_NOERR)
1349  {
1350  nc_advise("ncvargetg", status, "ncid %d", ncid);
1351  return -1;
1352  }
1353  }
1354  return 0;
1355  }
1356  }
1357 }
1358 
1372 int
1374  int ncid,
1375  int varid,
1376  const char* name
1377 )
1378 {
1379  const int status = nc_rename_var(ncid, varid, name);
1380  if(status != NC_NOERR)
1381  {
1382  nc_advise("ncvarrename", status, "ncid %d", ncid);
1383  return -1;
1384  }
1385  return varid;
1386 }
1387 
1404 int
1406  int ncid,
1407  int varid,
1408  const char* name,
1409  nc_type datatype,
1410  int len,
1411  const void* value
1412 )
1413 {
1414  const int status = nc_put_att(ncid, varid, name, datatype, len, value);
1415  if(status != NC_NOERR)
1416  {
1417  nc_advise("ncattput", status, "ncid %d", ncid);
1418  return -1;
1419  }
1420  return 0;
1421 }
1422 
1439 int
1441  int ncid,
1442  int varid,
1443  const char* name,
1444  nc_type* datatype,
1445  int* len
1446 )
1447 {
1448  size_t ll;
1449  const int status = nc_inq_att(ncid, varid, name, datatype, &ll);
1450  if(status != NC_NOERR)
1451  {
1452  nc_advise("ncattinq", status,
1453  "ncid %d; varid %d; attname \"%s\"",
1454  ncid, varid, name);
1455  return -1;
1456  }
1457 
1458  if(len != NULL)
1459  *len = (int) ll;
1460 
1461  return 1;
1462 }
1463 
1478 int
1480  int ncid,
1481  int varid,
1482  const char* name,
1483  void* value
1484 )
1485 {
1486  const int status = nc_get_att(ncid, varid, name, value);
1487  if(status != NC_NOERR)
1488  {
1489  nc_advise("ncattget", status, "ncid %d", ncid);
1490  return -1;
1491  }
1492  return 1;
1493 }
1494 
1510 int
1512  int ncid_in,
1513  int varid_in,
1514  const char* name,
1515  int ncid_out,
1516  int varid_out
1517 )
1518 {
1519  const int status = nc_copy_att(ncid_in, varid_in, name, ncid_out, varid_out);
1520  if(status != NC_NOERR)
1521  {
1522  nc_advise("ncattcopy", status, "%s", name);
1523  return -1;
1524  }
1525  return 0;
1526 }
1527 
1542 int
1544  int ncid,
1545  int varid,
1546  int attnum,
1547  char* name
1548 )
1549 {
1550  const int status = nc_inq_attname(ncid, varid, attnum, name);
1551  if(status != NC_NOERR)
1552  {
1553  nc_advise("ncattname", status, "ncid %d", ncid);
1554  return -1;
1555  }
1556  return attnum;
1557 }
1558 
1573 int
1575  int ncid,
1576  int varid,
1577  const char* name,
1578  const char* newname
1579 )
1580 {
1581  const int status = nc_rename_att(ncid, varid, name, newname);
1582  if(status != NC_NOERR)
1583  {
1584  nc_advise("ncattrename", status, "ncid %d", ncid);
1585  return -1;
1586  }
1587  return 1;
1588 }
1589 
1603 int
1605  int ncid,
1606  int varid,
1607  const char* name
1608 )
1609 {
1610  const int status = nc_del_att(ncid, varid, name);
1611  if(status != NC_NOERR)
1612  {
1613  nc_advise("ncattdel", status, "ncid %d", ncid);
1614  return -1;
1615  }
1616  return 1;
1617 }
1618 
1619 #endif /* NO_NETCDF_2 */
1620 
1621 #ifndef NO_NETCDF_2
1622 
1635 int
1637  int ncid,
1638  int fillmode
1639 )
1640 {
1641  int oldmode = -1;
1642  const int status = nc_set_fill(ncid, fillmode, &oldmode);
1643  if(status != NC_NOERR)
1644  {
1645  nc_advise("ncsetfill", status, "ncid %d", ncid);
1646  return -1;
1647  }
1648  return oldmode;
1649 }
1650 
1665 int
1667  int ncid,
1668  int* nrecvars,
1669  int* recvarids,
1670  long* recsizes
1671 )
1672 {
1673  size_t nrv = 0;
1674  size_t *rs = NULL;
1675  int status = NC_NOERR;
1676 
1677  rs = (size_t*)malloc(sizeof(size_t)*NC_MAX_VARS);
1678  if(rs == NULL)
1679  return NC_ENOMEM;
1680 
1681  status = nc_inq_rec(ncid, &nrv, recvarids, rs);
1682  if(status != NC_NOERR)
1683  {
1684  nc_advise("ncrecinq", status, "ncid %d", ncid);
1685  if(rs != NULL) free(rs);
1686  return -1;
1687  }
1688 
1689  if(nrecvars != NULL)
1690  *nrecvars = (int) nrv;
1691 
1692  if(recsizes != NULL)
1693  {
1694  size_t ii;
1695  for(ii = 0; ii < nrv; ii++)
1696  {
1697  recsizes[ii] = (long) rs[ii];
1698  }
1699  }
1700 
1701  if(rs != NULL) free(rs);
1702 
1703  return (int) nrv;
1704 }
1705 
1722 int
1724  int ncid,
1725  long recnum,
1726  void** datap
1727 )
1728 {
1729  const int status = nc_get_rec(ncid, (size_t)recnum, datap);
1730  if(status != NC_NOERR)
1731  {
1732  nc_advise("ncrecget", status, "ncid %d", ncid);
1733  return -1;
1734  }
1735  return 0;
1736 }
1737 
1753 int
1755  int ncid,
1756  long recnum,
1757  void* const* datap
1758 )
1759 {
1760  const int status = nc_put_rec(ncid, (size_t)recnum, datap);
1761  if(status != NC_NOERR)
1762  {
1763  nc_advise("ncrecput", status, "ncid %d", ncid);
1764  return -1;
1765  }
1766  return 0;
1767 }
1768 
1769 #endif /* NO_NETCDF_2 */
NC_NOERR
#define NC_NOERR
No Error.
Definition: netcdf.h:333
ncvardef
int ncvardef(int ncid, const char *name, nc_type datatype, int ndims, const int *dim)
Definition: dv2i.c:856
ncdimid
int ncdimid(int ncid, const char *name)
Definition: dv2i.c:761
nc_inq_varndims
EXTERNL int nc_inq_varndims(int ncid, int varid, int *ndimsp)
Learn how many dimensions are associated with a variable.
Definition: dvarinq.c:202
ncclose
int ncclose(int ncid)
Definition: dv2i.c:606
ncvarid
int ncvarid(int ncid, const char *name)
Definition: dv2i.c:887
nc_redef
EXTERNL int nc_redef(int ncid)
Definition: dfile.c:954
numrecvars
static int numrecvars(int ncid, int *nrecvarsp, int *recvarids)
Computes number of record variables in an open netCDF file, and an array of the record variable ids,...
Definition: dv2i.c:136
ncattdel
int ncattdel(int ncid, int varid, const char *name)
Definition: dv2i.c:1604
nc_def_var
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:212
ncvargets
int ncvargets(int ncid, int varid, const long *start, const long *count, const long *stride, void *value)
Definition: dv2i.c:1178
ncrecinq
int ncrecinq(int ncid, int *nrecvars, int *recvarids, long *recsizes)
Definition: dv2i.c:1666
nc_def_dim
EXTERNL int nc_def_dim(int ncid, const char *name, size_t len, int *idp)
Define a new dimension.
Definition: ddim.c:121
ncredef
int ncredef(int ncid)
Definition: dv2i.c:560
ncvargetg
int ncvargetg(int ncid, int varid, const long *start, const long *count, const long *stride, const long *map, void *value)
Definition: dv2i.c:1304
ncattcopy
int ncattcopy(int ncid_in, int varid_in, const char *name, int ncid_out, int varid_out)
Definition: dv2i.c:1511
MAX_NC_VARS
#define MAX_NC_VARS
Backward compatible alias.
Definition: netcdf.h:1830
ncdiminq
int ncdiminq(int ncid, int dimid, char *name, long *length)
Definition: dv2i.c:788
nc_put_vars
EXTERNL int nc_put_vars(int ncid, int varid, const size_t *startp, const size_t *countp, const ptrdiff_t *stridep, const void *op)
Definition: dvarput.c:1043
ncattget
int ncattget(int ncid, int varid, const char *name, void *value)
Definition: dv2i.c:1479
ncdimrename
int ncdimrename(int ncid, int dimid, const char *name)
Definition: dv2i.c:825
ncvarput
int ncvarput(int ncid, int varid, const long *start, const long *count, const void *value)
Definition: dv2i.c:1040
ncinquire
int ncinquire(int ncid, int *ndims, int *nvars, int *natts, int *recdim)
Definition: dv2i.c:635
nc_inq
EXTERNL int nc_inq(int ncid, int *ndimsp, int *nvarsp, int *nattsp, int *unlimdimidp)
Definition: dfile.c:1626
nc_put_vara
EXTERNL int nc_put_vara(int ncid, int varid, const size_t *startp, const size_t *countp, const void *op)
Definition: dvarput.c:631
ncvarget1
int ncvarget1(int ncid, int varid, const long *index, void *value)
Definition: dv2i.c:1002
nc_del_att
EXTERNL int nc_del_att(int ncid, int varid, const char *name)
Delete an attribute.
Definition: datt.c:177
nc_inq_dim
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:216
nc_inq_dimlen
EXTERNL int nc_inq_dimlen(int ncid, int dimid, size_t *lenp)
Find the length of a dimension.
Definition: ddim.c:467
nccreate
int nccreate(const char *path, int cmode)
Definition: dv2i.c:511
nc_put_var1
EXTERNL int nc_put_var1(int ncid, int varid, const size_t *indexp, const void *op)
Definition: dvarput.c:781
nc_rename_var
EXTERNL int nc_rename_var(int ncid, int varid, const char *name)
Rename a variable.
Definition: dvar.c:839
ncsync
int ncsync(int ncid)
Definition: dv2i.c:677
nc_get_varm
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)
Definition: dvarget.c:1359
ncattrename
int ncattrename(int ncid, int varid, const char *name, const char *newname)
Definition: dv2i.c:1574
ncendef
int ncendef(int ncid)
Definition: dv2i.c:583
nc_put_varm
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)
Definition: dvarput.c:1235
ncvarputs
int ncvarputs(int ncid, int varid, const long *start, const long *count, const long *stride, const void *value)
Definition: dv2i.c:1125
ncvarputg
int ncvarputg(int ncid, int varid, const long *start, const long *count, const long *stride, const long *map, const void *value)
Definition: dv2i.c:1231
nc_close
EXTERNL int nc_close(int ncid)
Definition: dfile.c:1291
ncattput
int ncattput(int ncid, int varid, const char *name, nc_type datatype, int len, const void *value)
Definition: dv2i.c:1405
nc_inq_dimid
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
nc_inq_att
EXTERNL int nc_inq_att(int ncid, int varid, const char *name, nc_type *xtypep, size_t *lenp)
Definition: dattinq.c:85
MAX_NC_DIMS
#define MAX_NC_DIMS
Backward compatible alias.
Definition: netcdf.h:1828
ncabort
int ncabort(int ncid)
Definition: dv2i.c:700
nc_inq_unlimdim
EXTERNL int nc_inq_unlimdim(int ncid, int *unlimdimidp)
Find the ID of the unlimited dimension.
Definition: ddim.c:350
dimsizes
static int dimsizes(int ncid, int varid, size_t *sizes)
Retrieves the dimension sizes of a variable with a specified variable id in an open netCDF file.
Definition: dv2i.c:240
netcdf.h
nc_inq_varid
EXTERNL int nc_inq_varid(int ncid, const char *name, int *varidp)
Definition: dvarinq.c:60
nc_abort
EXTERNL int nc_abort(int ncid)
Definition: dfile.c:1238
ncsetfill
int ncsetfill(int ncid, int fillmode)
Definition: dv2i.c:1636
NC_MAX_VARS
#define NC_MAX_VARS
Maximum for classic library.
Definition: netcdf.h:275
nc_copy_att
EXTERNL int nc_copy_att(int ncid_in, int varid_in, const char *name, int ncid_out, int varid_out)
Copy an attribute from one open file to another.
Definition: dcopy.c:628
ncrecput
int ncrecput(int ncid, long recnum, void *const *datap)
Definition: dv2i.c:1754
nc_set_fill
EXTERNL int nc_set_fill(int ncid, int fillmode, int *old_modep)
Definition: dfile.c:1465
A_INIT
#define A_INIT(lhs, type, ndims, rhs)
Init a variable.
Definition: dv2i.c:53
nc_open
EXTERNL int nc_open(const char *path, int mode, int *ncidp)
Open an existing netCDF file.
Definition: dfile.c:655
nc_put_att
EXTERNL int nc_put_att(int ncid, int varid, const char *name, nc_type xtype, size_t len, const void *op)
Definition: dattput.c:222
ncvarget
int ncvarget(int ncid, int varid, const long *start, const long *count, void *value)
Definition: dv2i.c:1082
nc_type
int nc_type
The nc_type type is just an int.
Definition: netcdf.h:25
nc_inq_vartype
EXTERNL int nc_inq_vartype(int ncid, int varid, nc_type *xtypep)
Learn the type of a variable.
Definition: dvarinq.c:178
nc_get_var1
EXTERNL int nc_get_var1(int ncid, int varid, const size_t *indexp, void *ip)
Definition: dvarget.c:889
NC_FATAL
#define NC_FATAL
For V2 API, exit on error.
Definition: netcdf.h:1849
ncvarput1
int ncvarput1(int ncid, int varid, const long *index, const void *value)
Definition: dv2i.c:965
nc_get_vara
EXTERNL int nc_get_vara(int ncid, int varid, const size_t *startp, const size_t *countp, void *ip)
Definition: dvarget.c:741
NC_ENOMEM
#define NC_ENOMEM
Memory allocation (malloc) failure.
Definition: netcdf.h:413
NC_VERBOSE
#define NC_VERBOSE
For V2 API, be verbose on error.
Definition: netcdf.h:1850
ncattinq
int ncattinq(int ncid, int varid, const char *name, nc_type *datatype, int *len)
Definition: dv2i.c:1440
ncrecsize
static int ncrecsize(int ncid, int varid, size_t *recsizep)
Computes record size (in bytes) of the record variable with a specified variable id.
Definition: dv2i.c:188
nc_strerror
EXTERNL const char * nc_strerror(int ncerr)
Given an error number, return an error message.
Definition: derror.c:87
nc_inq_nvars
EXTERNL int nc_inq_nvars(int ncid, int *nvarsp)
Learn the number of variables in a file or group.
Definition: dfile.c:1645
nc_put_rec
int nc_put_rec(int ncid, size_t recnum, void *const *datap)
Definition: dv2i.c:355
NC_SYSERR
#define NC_SYSERR
V2 API system error.
Definition: netcdf.h:1843
NC_EDIMSIZE
#define NC_EDIMSIZE
Invalid dimension size.
Definition: netcdf.h:415
ncerr
int ncerr
V2 API error code.
Definition: dv2i.c:37
nc_inq_attname
EXTERNL int nc_inq_attname(int ncid, int varid, int attnum, char *name)
Definition: dattinq.c:254
nc_enddef
EXTERNL int nc_enddef(int ncid)
Definition: dfile.c:1018
ncvarinq
int ncvarinq(int ncid, int varid, char *name, nc_type *datatype, int *ndims, int *dim, int *natts)
Definition: dv2i.c:920
nc_advise
void nc_advise(const char *routine_name, int err, const char *fmt,...)
Definition: dv2i.c:466
nc_get_vars
EXTERNL int nc_get_vars(int ncid, int varid, const size_t *startp, const size_t *countp, const ptrdiff_t *stridep, void *ip)
Definition: dvarget.c:1165
ncattname
int ncattname(int ncid, int varid, int attnum, char *name)
Definition: dv2i.c:1543
A_FREE
#define A_FREE(name)
Free a variable.
Definition: dv2i.c:51
ncopts
int ncopts
The subroutines in error.c emit no messages unless NC_VERBOSE bit is on.
Definition: dv2i.c:35
nc_rename_att
EXTERNL int nc_rename_att(int ncid, int varid, const char *name, const char *newname)
Rename an attribute.
Definition: datt.c:113
nc_get_rec
int nc_get_rec(int ncid, size_t recnum, void **datap)
Definition: dv2i.c:416
NC_ISSYSERR
#define NC_ISSYSERR(err)
The netcdf version 3 functions all return integer error status.
Definition: netcdf.h:331
nc_create
EXTERNL int nc_create(const char *path, int cmode, int *ncidp)
Definition: dfile.c:389
ncvarrename
int ncvarrename(int ncid, int varid, const char *name)
Definition: dv2i.c:1373
nc_sync
EXTERNL int nc_sync(int ncid)
Definition: dfile.c:1186
nc_rename_dim
EXTERNL int nc_rename_dim(int ncid, int dimid, const char *name)
Rename a dimension.
Definition: ddim.c:285
ncopen
int ncopen(const char *path, int mode)
Definition: dv2i.c:536
schar
signed char schar
Signed character type.
Definition: dv2i.c:121
nc_inq_var
EXTERNL int nc_inq_var(int ncid, int varid, char *name, nc_type *xtypep, int *ndimsp, int *dimidsp, int *nattsp)
Definition: dvarinq.c:124
nc_inq_rec
int nc_inq_rec(int ncid, size_t *nrecvarsp, int *recvarids, size_t *recsizes)
Definition: dv2i.c:289
NDIMS_DECL
#define NDIMS_DECL
NDIMS declaration.
Definition: dv2i.c:45
nc_get_att
EXTERNL int nc_get_att(int ncid, int varid, const char *name, void *ip)
Definition: dattget.c:96
ncrecget
int ncrecget(int ncid, long recnum, void **datap)
Definition: dv2i.c:1723
nc_inq_vardimid
EXTERNL int nc_inq_vardimid(int ncid, int varid, int *dimidsp)
Learn the dimension IDs associated with a variable.
Definition: dvarinq.c:225
ncdimdef
int ncdimdef(int ncid, const char *name, long length)
Definition: dv2i.c:726