NetCDF  4.9.2
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 
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 */
#define NDIMS_DECL
NDIMS declaration.
Definition: dv2i.c:45
signed char schar
Signed character type.
Definition: dv2i.c:121
MSC_EXTRA int ncerr
V2 API error code.
Definition: dv2i.c:37
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
#define A_INIT(lhs, type, ndims, rhs)
Init a variable.
Definition: dv2i.c:53
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
MSC_EXTRA int ncopts
The subroutines in error.c emit no messages unless NC_VERBOSE bit is on.
Definition: dv2i.c:35
#define A_FREE(name)
Free a variable.
Definition: dv2i.c:51
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
EXTERNL int nc_put_att(int ncid, int varid, const char *name, nc_type xtype, size_t len, const void *op)
Write an attribute of any type.
Definition: dattput.c:222
EXTERNL int nc_get_att(int ncid, int varid, const char *name, void *ip)
Get an attribute of any type.
Definition: dattget.c:133
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:86
EXTERNL int nc_del_att(int ncid, int varid, const char *name)
Delete an attribute.
Definition: datt.c:177
EXTERNL int nc_rename_att(int ncid, int varid, const char *name, const char *newname)
Rename an attribute.
Definition: datt.c:113
EXTERNL int nc_inq_attname(int ncid, int varid, int attnum, char *name)
Find the name of an attribute.
Definition: dattinq.c:255
EXTERNL int nc_close(int ncid)
Close an open netCDF dataset.
Definition: dfile.c:1302
EXTERNL int nc_abort(int ncid)
No longer necessary for user to invoke manually.
Definition: dfile.c:1249
EXTERNL int nc_create(const char *path, int cmode, int *ncidp)
Create a new netCDF file.
Definition: dfile.c:400
EXTERNL int nc_inq(int ncid, int *ndimsp, int *nvarsp, int *nattsp, int *unlimdimidp)
Inquire about a file or group.
Definition: dfile.c:1637
EXTERNL int nc_set_fill(int ncid, int fillmode, int *old_modep)
Change the fill-value mode to improve write performance.
Definition: dfile.c:1476
EXTERNL int nc_enddef(int ncid)
Leave define mode.
Definition: dfile.c:1029
EXTERNL int nc_redef(int ncid)
Put open netcdf dataset into define mode.
Definition: dfile.c:965
EXTERNL int nc_open(const char *path, int mode, int *ncidp)
Open an existing netCDF file.
Definition: dfile.c:666
EXTERNL int nc_sync(int ncid)
Synchronize an open netcdf dataset to disk.
Definition: dfile.c:1197
EXTERNL int nc_inq_dimlen(int ncid, int dimid, size_t *lenp)
Find the length of a dimension.
Definition: ddim.c:467
EXTERNL int nc_def_dim(int ncid, const char *name, size_t len, int *idp)
Define a new dimension.
Definition: ddim.c:121
EXTERNL int nc_rename_dim(int ncid, int dimid, const char *name)
Rename a dimension.
Definition: ddim.c:285
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
EXTERNL int nc_inq_unlimdim(int ncid, int *unlimdimidp)
Find the ID of the unlimited dimension.
Definition: ddim.c:350
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 const char * nc_strerror(int ncerr)
Given an error number, return an error message.
Definition: derror.c:87
int ncvardef(int ncid, const char *name, nc_type datatype, int ndims, const int *dim)
Define a variable.
Definition: dv2i.c:856
int ncclose(int ncid)
Close a file.
Definition: dv2i.c:606
int ncvarputg(int ncid, int varid, const long *start, const long *count, const long *stride, const long *map, const void *value)
Write mapped data.
Definition: dv2i.c:1231
int ncattget(int ncid, int varid, const char *name, void *value)
Read an attribute.
Definition: dv2i.c:1479
int ncattinq(int ncid, int varid, const char *name, nc_type *datatype, int *len)
Learn about an attribute.
Definition: dv2i.c:1440
int ncattrename(int ncid, int varid, const char *name, const char *newname)
Rename an attribute.
Definition: dv2i.c:1574
int ncattname(int ncid, int varid, int attnum, char *name)
Learn attribute name from its number.
Definition: dv2i.c:1543
int ncvarput1(int ncid, int varid, const long *index, const void *value)
Write 1 data value.
Definition: dv2i.c:965
int ncvarid(int ncid, const char *name)
Learn a variable ID from the name.
Definition: dv2i.c:887
int nccreate(const char *path, int cmode)
Create a netCDF file.
Definition: dv2i.c:511
int ncendef(int ncid)
End define mode for file.
Definition: dv2i.c:583
int ncopen(const char *path, int mode)
Open a netCDF file.
Definition: dv2i.c:536
int ncrecput(int ncid, long recnum, void *const *datap)
Write one record's worth of data, except don't write to variables for which the address of the data t...
Definition: dv2i.c:1754
int ncattcopy(int ncid_in, int varid_in, const char *name, int ncid_out, int varid_out)
Copy an attribute.
Definition: dv2i.c:1511
int ncdimrename(int ncid, int dimid, const char *name)
Rename a dimension.
Definition: dv2i.c:825
int nc_get_rec(int ncid, size_t recnum, void **datap)
Read one record's worth of data, except don't read from variables for which the address of the data t...
Definition: dv2i.c:416
int ncattdel(int ncid, int varid, const char *name)
Delete an attribute.
Definition: dv2i.c:1604
int ncvarputs(int ncid, int varid, const long *start, const long *count, const long *stride, const void *value)
Write strided data.
Definition: dv2i.c:1125
int ncvarput(int ncid, int varid, const long *start, const long *count, const void *value)
Write some data.
Definition: dv2i.c:1040
int ncdiminq(int ncid, int dimid, char *name, long *length)
Learn about a dimension.
Definition: dv2i.c:788
int ncvarget1(int ncid, int varid, const long *index, void *value)
Read 1 data value.
Definition: dv2i.c:1002
int ncvargetg(int ncid, int varid, const long *start, const long *count, const long *stride, const long *map, void *value)
Read mapped data.
Definition: dv2i.c:1304
int ncinquire(int ncid, int *ndims, int *nvars, int *natts, int *recdim)
Learn about a file.
Definition: dv2i.c:635
int ncvarinq(int ncid, int varid, char *name, nc_type *datatype, int *ndims, int *dim, int *natts)
Learn about a variable.
Definition: dv2i.c:920
int ncattput(int ncid, int varid, const char *name, nc_type datatype, int len, const void *value)
Write an attribute.
Definition: dv2i.c:1405
void nc_advise(const char *routine_name, int err, const char *fmt,...)
Show an error message and exit (based on ncopts).
Definition: dv2i.c:466
int ncabort(int ncid)
Abort defining a file.
Definition: dv2i.c:700
int ncsync(int ncid)
Sync a file.
Definition: dv2i.c:677
int ncrecinq(int ncid, int *nrecvars, int *recvarids, long *recsizes)
Learn record variables and the lengths of the record dimension.
Definition: dv2i.c:1666
int ncrecget(int ncid, long recnum, void **datap)
Read one record's worth of data, except don't read from variables for which the address of the data t...
Definition: dv2i.c:1723
int ncsetfill(int ncid, int fillmode)
Set the fill mode.
Definition: dv2i.c:1636
int ncdimdef(int ncid, const char *name, long length)
Define a dimension.
Definition: dv2i.c:726
int ncvarget(int ncid, int varid, const long *start, const long *count, void *value)
Read some data.
Definition: dv2i.c:1082
int ncvargets(int ncid, int varid, const long *start, const long *count, const long *stride, void *value)
Read strided data.
Definition: dv2i.c:1178
int nc_put_rec(int ncid, size_t recnum, void *const *datap)
Write one record's worth of data, except don't write to variables for which the address of the data t...
Definition: dv2i.c:355
int ncredef(int ncid)
Put file in define mode.
Definition: dv2i.c:560
int ncvarrename(int ncid, int varid, const char *name)
Rename a variable.
Definition: dv2i.c:1373
int ncdimid(int ncid, const char *name)
Find dimension ID from name.
Definition: dv2i.c:761
int nc_inq_rec(int ncid, size_t *nrecvarsp, int *recvarids, size_t *recsizes)
Retrieves the number of record variables, the record variable ids, and the record size of each record...
Definition: dv2i.c:289
EXTERNL int nc_inq_varndims(int ncid, int varid, int *ndimsp)
Learn how many dimensions are associated with a variable.
Definition: dvarinq.c:202
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:1165
EXTERNL int nc_inq_vartype(int ncid, int varid, nc_type *xtypep)
Learn the type of a variable.
Definition: dvarinq.c:178
EXTERNL int nc_inq_varid(int ncid, const char *name, int *varidp)
Find the ID of a variable, from the name.
Definition: dvarinq.c:60
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:1235
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:741
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:1043
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:1359
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:124
EXTERNL int nc_inq_vardimid(int ncid, int varid, int *dimidsp)
Learn the dimension IDs associated with a variable.
Definition: dvarinq.c:225
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:889
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:214
EXTERNL int nc_rename_var(int ncid, int varid, const char *name)
Rename a variable.
Definition: dvar.c:946
EXTERNL int nc_put_var1(int ncid, int varid, const size_t *indexp, const void *op)
Write one datum.
Definition: dvarput.c:781
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:631
Main header file for the C API.
EXTERNL int nc_inq_nvars(int ncid, int *nvarsp)
Learn the number of variables in a file or group.
Definition: dfile.c:1656
#define NC_MAX_VARS
Maximum for classic library.
Definition: netcdf.h:280
#define NC_SYSERR
V2 API system error.
Definition: netcdf.h:1953
#define NC_VERBOSE
For V2 API, be verbose on error.
Definition: netcdf.h:1960
#define MSC_EXTRA
Needed for DLL build.
Definition: netcdf.h:553
#define MAX_NC_DIMS
Backward compatible alias.
Definition: netcdf.h:1938
#define NC_ENOMEM
Memory allocation (malloc) failure.
Definition: netcdf.h:448
#define NC_EDIMSIZE
Invalid dimension size.
Definition: netcdf.h:450
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:659
#define NC_ISSYSERR(err)
The netcdf version 3 functions all return integer error status.
Definition: netcdf.h:366
#define NC_NOERR
No Error.
Definition: netcdf.h:368
#define NC_FATAL
For V2 API, exit on error.
Definition: netcdf.h:1959
#define MAX_NC_VARS
Backward compatible alias.
Definition: netcdf.h:1940
int nc_type
The nc_type type is just an int.
Definition: netcdf.h:25