nc.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. /*
  2. * Copyright 1996, University Corporation for Atmospheric Research
  3. * See netcdf/COPYRIGHT file for copying and redistribution conditions.
  4. */
  5. #ifndef _NC_H_
  6. #define _NC_H_
  7. /*
  8. * netcdf library 'private' data structures, objects and interfaces
  9. */
  10. #include <config.h>
  11. #include <stddef.h> /* size_t */
  12. #ifndef HAVE_STDINT_H
  13. # include "pstdint.h" /* attempts to define uint32_t etc portably */
  14. #else
  15. # include <stdint.h>
  16. #endif /* HAVE_STDINT_H */
  17. #include <sys/types.h> /* off_t */
  18. #ifdef USE_PARALLEL
  19. #include <netcdf_par.h>
  20. #else
  21. #include <netcdf.h>
  22. #endif /* USE_PARALLEL */
  23. #if 0
  24. #include "ncio.h"
  25. #include "fbits.h"
  26. #endif
  27. /*#ifndef HAVE_SSIZE_T
  28. #define ssize_t int
  29. #endif*/
  30. #ifndef NC_ARRAY_GROWBY
  31. #define NC_ARRAY_GROWBY 4
  32. #endif
  33. /*
  34. * The extern size of an empty
  35. * netcdf version 1 file.
  36. * The initial value of ncp->xsz.
  37. */
  38. #define MIN_NC_XSZ 32
  39. /* Forward */
  40. typedef struct NC NC; /* forward reference */
  41. struct ncio;
  42. /*
  43. * The internal data types
  44. */
  45. typedef enum {
  46. NC_UNSPECIFIED = 0,
  47. /* future NC_BITFIELD = 7, */
  48. /* NC_STRING = 8, */
  49. NC_DIMENSION = 10,
  50. NC_VARIABLE = 11,
  51. NC_ATTRIBUTE = 12
  52. } NCtype;
  53. /*
  54. * Counted string for names and such
  55. */
  56. typedef struct {
  57. /* all xdr'd */
  58. size_t nchars;
  59. char *cp;
  60. } NC_string;
  61. /* Begin defined in string.c */
  62. extern void
  63. free_NC_string(NC_string *ncstrp);
  64. extern int
  65. NC_check_name(const char *name);
  66. extern NC_string *
  67. new_NC_string(size_t slen, const char *str);
  68. extern int
  69. set_NC_string(NC_string *ncstrp, const char *str);
  70. /* End defined in string.c */
  71. /*
  72. * NC dimension stucture
  73. */
  74. typedef struct {
  75. /* all xdr'd */
  76. NC_string *name;
  77. uint32_t hash;
  78. size_t size;
  79. } NC_dim;
  80. typedef struct NC_dimarray {
  81. size_t nalloc; /* number allocated >= nelems */
  82. /* below gets xdr'd */
  83. /* NCtype type = NC_DIMENSION */
  84. size_t nelems; /* length of the array */
  85. NC_dim **value;
  86. } NC_dimarray;
  87. /* Begin defined in dim.c */
  88. extern void
  89. free_NC_dim(NC_dim *dimp);
  90. extern NC_dim *
  91. new_x_NC_dim(NC_string *name);
  92. extern int
  93. find_NC_Udim(const NC_dimarray *ncap, NC_dim **dimpp);
  94. /* dimarray */
  95. extern void
  96. free_NC_dimarrayV0(NC_dimarray *ncap);
  97. extern void
  98. free_NC_dimarrayV(NC_dimarray *ncap);
  99. extern int
  100. dup_NC_dimarrayV(NC_dimarray *ncap, const NC_dimarray *ref);
  101. extern NC_dim *
  102. elem_NC_dimarray(const NC_dimarray *ncap, size_t elem);
  103. /* End defined in dim.c */
  104. /*
  105. * NC attribute
  106. */
  107. typedef struct {
  108. size_t xsz; /* amount of space at xvalue */
  109. /* below gets xdr'd */
  110. NC_string *name;
  111. nc_type type; /* the discriminant */
  112. size_t nelems; /* length of the array */
  113. void *xvalue; /* the actual data, in external representation */
  114. } NC_attr;
  115. typedef struct NC_attrarray {
  116. size_t nalloc; /* number allocated >= nelems */
  117. /* below gets xdr'd */
  118. /* NCtype type = NC_ATTRIBUTE */
  119. size_t nelems; /* length of the array */
  120. NC_attr **value;
  121. } NC_attrarray;
  122. /* Begin defined in attr.c */
  123. extern void
  124. free_NC_attr(NC_attr *attrp);
  125. extern NC_attr *
  126. new_x_NC_attr(
  127. NC_string *strp,
  128. nc_type type,
  129. size_t nelems);
  130. extern NC_attr **
  131. NC_findattr(const NC_attrarray *ncap, const char *name);
  132. /* attrarray */
  133. extern void
  134. free_NC_attrarrayV0(NC_attrarray *ncap);
  135. extern void
  136. free_NC_attrarrayV(NC_attrarray *ncap);
  137. extern int
  138. dup_NC_attrarrayV(NC_attrarray *ncap, const NC_attrarray *ref);
  139. extern NC_attr *
  140. elem_NC_attrarray(const NC_attrarray *ncap, size_t elem);
  141. /* End defined in attr.c */
  142. /*
  143. * NC variable: description and data
  144. */
  145. typedef struct NC_var {
  146. size_t xsz; /* xszof 1 element */
  147. size_t *shape; /* compiled info: dim->size of each dim */
  148. off_t *dsizes; /* compiled info: the right to left product of shape */
  149. /* below gets xdr'd */
  150. NC_string *name;
  151. uint32_t hash;
  152. /* next two: formerly NC_iarray *assoc */ /* user definition */
  153. size_t ndims; /* assoc->count */
  154. int *dimids; /* assoc->value */
  155. NC_attrarray attrs;
  156. nc_type type; /* the discriminant */
  157. size_t len; /* the total length originally allocated */
  158. off_t begin;
  159. } NC_var;
  160. typedef struct NC_vararray {
  161. size_t nalloc; /* number allocated >= nelems */
  162. /* below gets xdr'd */
  163. /* NCtype type = NC_VARIABLE */
  164. size_t nelems; /* length of the array */
  165. NC_var **value;
  166. } NC_vararray;
  167. /* Begin defined in lookup3.c */
  168. extern uint32_t
  169. hash_fast(const void *key, size_t length);
  170. /* End defined in lookup3.c */
  171. /* Begin defined in var.c */
  172. extern void
  173. free_NC_var(NC_var *varp);
  174. extern NC_var *
  175. new_x_NC_var(
  176. NC_string *strp,
  177. size_t ndims);
  178. /* vararray */
  179. extern void
  180. free_NC_vararrayV0(NC_vararray *ncap);
  181. extern void
  182. free_NC_vararrayV(NC_vararray *ncap);
  183. extern int
  184. dup_NC_vararrayV(NC_vararray *ncap, const NC_vararray *ref);
  185. extern int
  186. NC_var_shape(NC_var *varp, const NC_dimarray *dims);
  187. extern int
  188. NC_findvar(const NC_vararray *ncap, const char *name, NC_var **varpp);
  189. extern int
  190. NC_check_vlen(NC_var *varp, size_t vlen_max);
  191. extern NC_var *
  192. NC_lookupvar(NC *ncp, int varid);
  193. /* End defined in var.c */
  194. #define IS_RECVAR(vp) \
  195. ((vp)->shape != NULL ? (*(vp)->shape == NC_UNLIMITED) : 0 )
  196. #ifdef LOCKNUMREC
  197. /*
  198. * typedef SHMEM type
  199. * for whenever the SHMEM functions can handle other than shorts
  200. */
  201. typedef unsigned short int ushmem_t;
  202. typedef short int shmem_t;
  203. #endif
  204. /* Warning: fields from BEGIN COMMON to END COMMON must be same for:
  205. 1. NCcommon (include/ncdispatch.h)
  206. 2. NC (libsrc/nc.h)
  207. 3. NC_FILE_INFO (libsrc4/nc4internal.h)
  208. 4. whatever libdiskless uses
  209. */
  210. struct NC {
  211. /*BEGIN COMMON (see include/ncdispatch.h: struct NCcommon) */
  212. int ext_ncid;
  213. int int_ncid;
  214. struct NC_Dispatch* dispatch;
  215. void* dispatchdata;
  216. char* path;
  217. int substrate;
  218. void* instance; /* per-instance data specific to netcdf3,4,dap,etc.
  219. Currently only used by librpc, will retrofit other
  220. dispatch kinds over time. */
  221. /*END COMMON*/
  222. /* contains the previous NC during redef. */
  223. struct NC *old;
  224. /* flags */
  225. #define NC_CREAT 2 /* in create phase, cleared by ncendef */
  226. #define NC_INDEF 8 /* in define mode, cleared by ncendef */
  227. #define NC_NSYNC 0x10 /* synchronise numrecs on change */
  228. #define NC_HSYNC 0x20 /* synchronise whole header on change */
  229. #define NC_NDIRTY 0x40 /* numrecs has changed */
  230. #define NC_HDIRTY 0x80 /* header info has changed */
  231. /* NC_NOFILL in netcdf.h, historical interface */
  232. int flags;
  233. struct ncio* nciop;
  234. size_t chunk; /* largest extent this layer will request from ncio->get() */
  235. size_t xsz; /* external size of this header, == var[0].begin */
  236. off_t begin_var; /* position of the first (non-record) var */
  237. off_t begin_rec; /* position of the first 'record' */
  238. /* Don't constrain maximum size of record unnecessarily */
  239. #if SIZEOF_OFF_T > SIZEOF_SIZE_T
  240. off_t recsize; /* length of 'record' */
  241. #else
  242. size_t recsize; /* length of 'record' */
  243. #endif
  244. /* below gets xdr'd */
  245. size_t numrecs; /* number of 'records' allocated */
  246. NC_dimarray dims;
  247. NC_attrarray attrs;
  248. NC_vararray vars;
  249. #ifdef LOCKNUMREC
  250. /* size and named indexes for the lock array protecting NC.numrecs */
  251. # define LOCKNUMREC_DIM 4
  252. # define LOCKNUMREC_VALUE 0
  253. # define LOCKNUMREC_LOCK 1
  254. # define LOCKNUMREC_SERVING 2
  255. # define LOCKNUMREC_BASEPE 3
  256. /* Used on Cray T3E MPP to maintain the
  257. * integrity of numrecs for an unlimited dimension
  258. */
  259. ushmem_t lock[LOCKNUMREC_DIM];
  260. #endif
  261. };
  262. #define NC_readonly(ncp) \
  263. (!fIsSet(ncp->nciop->ioflags, NC_WRITE))
  264. #define NC_IsNew(ncp) \
  265. fIsSet((ncp)->flags, NC_CREAT)
  266. #define NC_indef(ncp) \
  267. (NC_IsNew(ncp) || fIsSet((ncp)->flags, NC_INDEF))
  268. #define set_NC_ndirty(ncp) \
  269. fSet((ncp)->flags, NC_NDIRTY)
  270. #define NC_ndirty(ncp) \
  271. fIsSet((ncp)->flags, NC_NDIRTY)
  272. #define set_NC_hdirty(ncp) \
  273. fSet((ncp)->flags, NC_HDIRTY)
  274. #define NC_hdirty(ncp) \
  275. fIsSet((ncp)->flags, NC_HDIRTY)
  276. #define NC_dofill(ncp) \
  277. (!fIsSet((ncp)->flags, NC_NOFILL))
  278. #define NC_doHsync(ncp) \
  279. fIsSet((ncp)->flags, NC_HSYNC)
  280. #define NC_doNsync(ncp) \
  281. fIsSet((ncp)->flags, NC_NSYNC)
  282. #ifndef LOCKNUMREC
  283. # define NC_get_numrecs(ncp) \
  284. ((ncp)->numrecs)
  285. # define NC_set_numrecs(ncp, nrecs) \
  286. {((ncp)->numrecs = (nrecs));}
  287. # define NC_increase_numrecs(ncp, nrecs) \
  288. {if((nrecs) > (ncp)->numrecs) ((ncp)->numrecs = (nrecs));}
  289. #else
  290. size_t NC_get_numrecs(const NC *ncp);
  291. void NC_set_numrecs(NC *ncp, size_t nrecs);
  292. void NC_increase_numrecs(NC *ncp, size_t nrecs);
  293. #endif
  294. /* Begin defined in nc.c */
  295. extern int
  296. NC_check_id(int ncid, NC **ncpp);
  297. extern int
  298. nc_cktype(nc_type datatype);
  299. extern size_t
  300. ncx_howmany(nc_type type, size_t xbufsize);
  301. extern int
  302. read_numrecs(NC *ncp);
  303. extern int
  304. write_numrecs(NC *ncp);
  305. extern int
  306. NC_sync(NC *ncp);
  307. extern int
  308. NC_calcsize(const NC *ncp, off_t *filesizep);
  309. /* End defined in nc.c */
  310. /* Begin defined in v1hpg.c */
  311. extern size_t
  312. ncx_len_NC(const NC *ncp, size_t sizeof_off_t);
  313. extern int
  314. ncx_put_NC(const NC *ncp, void **xpp, off_t offset, size_t extent);
  315. extern int
  316. nc_get_NC( NC *ncp);
  317. /* End defined in v1hpg.c */
  318. /* Begin defined in putget.c */
  319. extern int
  320. fill_NC_var(NC *ncp, const NC_var *varp, size_t varsize, size_t recno);
  321. extern int
  322. nc_inq_rec(int ncid, size_t *nrecvars, int *recvarids, size_t *recsizes);
  323. extern int
  324. nc_get_rec(int ncid, size_t recnum, void **datap);
  325. extern int
  326. nc_put_rec(int ncid, size_t recnum, void *const *datap);
  327. /* End defined in putget.c */
  328. extern int add_to_NCList(NC*);
  329. extern void del_from_NCList(NC*);/* does not free object */
  330. extern NC* find_in_NCList(int ext_ncid);
  331. extern void free_NCList(void);/* reclaim whole list */
  332. extern int count_NCList(void); /* return # of entries in NClist */
  333. /* Create a pseudo file descriptor that does not
  334. overlap real file descriptors
  335. */
  336. extern int nc__pseudofd(void);
  337. #endif /* _NC_H_ */