source: XIOS/dev/branch_openmp/extern/src_netcdf4/nc4internal.h @ 1501

Last change on this file since 1501 was 409, checked in by ymipsl, 11 years ago

Add improved nectdf internal library src

YM

  • Property svn:eol-style set to native
File size: 13.9 KB
Line 
1/** \file
2  This header file contains the definitions of structs used to hold
3  netCDF file metadata in memory.
4
5  Copyright 2005-2011 University Corporation for Atmospheric
6  Research/Unidata.
7*/
8
9#ifndef _NC4INTERNAL_
10#define _NC4INTERNAL_
11
12#include <config.h>
13#include <stdio.h>
14#include <stdlib.h>
15#include <ctype.h>
16#include <stdarg.h>
17#include <string.h>
18#include <hdf5.h>
19#include <ncdimscale.h>
20#include <nc_logging.h>
21
22#ifdef USE_PARALLEL
23#include <netcdf_par.h>
24#else
25#define MPI_Info int
26#define MPI_Comm int
27#include <netcdf.h>
28#endif /* USE_PARALLEL */
29#include <netcdf_f.h>
30
31#ifdef USE_HDF4
32#include <mfhdf.h>
33#endif
34
35#define FILE_ID_MASK (0xffff0000)
36#define GRP_ID_MASK (0x0000ffff)
37#define ID_SHIFT (16)
38
39typedef enum {GET, PUT} NC_PG_T;
40typedef enum {VAR, DIM, ATT} NC_OBJ_T;
41
42#define NC_MAX_HDF5_NAME (NC_MAX_NAME + 10)
43#define NC_V2_ERR (-1)
44
45/* The name of the root group. */
46#define NC_GROUP_NAME "/"
47
48#define MEGABYTE 1048576
49
50/*
51 * limits of the external representation
52 */
53#define X_SCHAR_MIN     (-128)
54#define X_SCHAR_MAX     127
55#define X_UCHAR_MAX     255U
56#define X_SHORT_MIN     (-32768)
57#define X_SHRT_MIN      X_SHORT_MIN     /* alias compatible with limits.h */
58#define X_SHORT_MAX     32767
59#define X_SHRT_MAX      X_SHORT_MAX     /* alias compatible with limits.h */
60#define X_USHORT_MAX    65535U
61#define X_USHRT_MAX     X_USHORT_MAX    /* alias compatible with limits.h */
62#define X_INT_MIN       (-2147483647-1)
63#define X_INT_MAX       2147483647
64#define X_LONG_MIN      X_INT_MIN
65#define X_LONG_MAX      X_INT_MAX
66#define X_UINT_MAX      4294967295U
67#ifdef WIN32 /* Windows, of course, has to be a *little* different. */
68#define X_FLOAT_MAX     3.402823466e+38f
69#else
70#define X_FLOAT_MAX     3.40282347e+38f
71#endif /* WIN32 */
72#define X_FLOAT_MIN     (-X_FLOAT_MAX)
73#define X_DOUBLE_MAX    1.7976931348623157e+308
74#define X_DOUBLE_MIN    (-X_DOUBLE_MAX)
75
76/* These have to do with creating chuncked datasets in HDF5. */
77#define NC_HDF5_UNLIMITED_DIMSIZE (0)
78#define NC_HDF5_CHUNKSIZE_FACTOR (10)
79#define NC_HDF5_MIN_CHUNK_SIZE (2)
80
81#define NC_EMPTY_SCALE "NC_EMPTY_SCALE"
82
83/* This is an attribute I had to add to handle multidimensional
84 * coordinate variables. */
85#define COORDINATES "_Netcdf4Coordinates"
86#define COORDINATES_LEN (NC_MAX_NAME * 5)
87
88/* This is used when the user defines a non-coordinate variable with
89 * same name as a dimension. */
90#define NON_COORD_PREPEND "_nc4_non_coord_"
91
92/* An attribute in the HDF5 root group of this name means that the
93 * file must follow strict netCDF classic format rules. */
94#define NC3_STRICT_ATT_NAME "_nc3_strict"
95
96/* If this attribute is present on a dimscale variable, use the value
97 * as the netCDF dimid. */
98#define NC_DIMID_ATT_NAME "_Netcdf4Dimid"
99
100/* This is a struct to handle the dim metadata. */
101typedef struct NC_DIM_INFO
102{
103   char *name;
104   size_t len;
105   int dimid;
106   int unlimited;
107   int extended;
108   struct NC_DIM_INFO *next;
109   struct NC_DIM_INFO *prev;
110   hid_t hdf_dimscaleid;
111   char *old_name; /* only used to rename dim */
112   int dirty;
113   unsigned char coord_var_in_grp;
114   struct NC_VAR_INFO *coord_var; /* The coord var, if it exists. */
115   int too_long; /* True if len it too big to fit in local size_t. */
116} NC_DIM_INFO_T;
117
118typedef struct NC_ATT_INFO
119{
120   int len;
121   char *name;
122   struct NC_ATT_INFO *next;
123   struct NC_ATT_INFO *prev;
124   int dirty;
125   int created;
126   nc_type xtype;
127   hid_t native_typeid;
128   int attnum;
129   void *data;
130   nc_vlen_t *vldata; /* only used for vlen */
131   char **stdata; /* only for string type. */
132   int class;
133} NC_ATT_INFO_T;
134
135/* This is a struct to handle the var metadata. */
136typedef struct NC_VAR_INFO
137{
138   char *name;
139   char *hdf5_name; /* used if different from name */
140   int ndims;
141   int *dimids;
142   NC_DIM_INFO_T **dim;
143   int varid;
144   int natts;
145   struct NC_VAR_INFO *next;
146   struct NC_VAR_INFO *prev;
147   int dirty;
148   int created;
149   int written_to;
150   int *dimscale_attached;
151   struct NC_TYPE_INFO *type_info;
152   nc_type xtype;
153   hid_t hdf_datasetid;
154   NC_ATT_INFO_T *att;
155   int no_fill;
156   void *fill_value;
157   size_t *chunksizes;
158   int contiguous;
159   int parallel_access;
160   int dimscale;
161   HDF5_OBJID_T *dimscale_hdf5_objids;
162   int deflate;
163   int deflate_level;
164   int shuffle;
165   int fletcher32;
166   int options_mask;
167   int pixels_per_block;
168   size_t chunk_cache_size, chunk_cache_nelems;
169   float chunk_cache_preemption;
170   /* Stuff below is for hdf4 files. */
171   int sdsid;
172   int hdf4_data_type;
173   /* Stuff below for diskless data files. */
174   void *diskless_data;
175} NC_VAR_INFO_T;
176
177typedef struct NC_FIELD_INFO
178{
179   struct NC_FIELD_INFO *next;
180   struct NC_FIELD_INFO *prev;
181   nc_type nctype;
182   hid_t hdf_typeid;
183   hid_t native_typeid;
184   size_t offset;
185   char *name;
186   int fieldid;
187   int ndims;
188   int *dim_size;
189} NC_FIELD_INFO_T;
190
191typedef struct NC_ENUM_MEMBER_INFO
192{
193   struct NC_ENUM_MEMBER_INFO *next;
194   struct NC_ENUM_MEMBER_INFO *prev;
195   char *name;
196   void *value;
197} NC_ENUM_MEMBER_INFO_T;
198
199typedef struct NC_TYPE_INFO
200{
201   struct NC_TYPE_INFO *next;
202   struct NC_TYPE_INFO *prev;
203   nc_type nc_typeid;
204   hid_t hdf_typeid;
205   hid_t native_typeid;
206   size_t size;
207   int committed; /* What the pig is, but the hen isn't, at breakfast. */
208   char *name;
209   int class; /* NC_VLEN, NC_COMPOUND, NC_OPAQUE, or NC_ENUM */
210   int num_enum_members;
211   NC_ENUM_MEMBER_INFO_T *enum_member;
212   NC_FIELD_INFO_T *field; /* Used for compound types. */
213   int num_fields;
214   nc_type base_nc_type; /* for VLEN and ENUM only */
215   hid_t base_hdf_typeid; /* for VLEN only */
216   int close_hdf_typeid; /* True when hdf_typeid must be H5Tclosed. */
217   int endianness;
218} NC_TYPE_INFO_T;
219
220/* This holds information for one group. Groups reproduce with
221 * parthenogenesis. */
222typedef struct NC_GRP_INFO
223{
224   int nc_grpid;
225   struct NC_GRP_INFO *parent;
226   struct NC_GRP_INFO *children;
227   struct NC_GRP_INFO *next; /* points to siblings */
228   struct NC_GRP_INFO *prev; /* points to siblings */
229   NC_VAR_INFO_T *var;
230   NC_DIM_INFO_T *dim;
231   NC_ATT_INFO_T *att;
232   int nvars;
233   int ndims;
234   int natts;
235   struct NC_FILE_INFO *file;
236   char *name;
237   hid_t hdf_grpid;
238   NC_TYPE_INFO_T *type;
239} NC_GRP_INFO_T;
240
241/* These constants apply to the cmode parameter in the
242 * HDF5_FILE_INFO_T defined below. */
243#define NC_CREAT 2      /* in create phase, cleared by ncendef */
244#define NC_INDEF 8      /* in define mode, cleared by ncendef */
245#define NC_NSYNC 0x10   /* synchronise numrecs on change */
246#define NC_HSYNC 0x20   /* synchronise whole header on change */
247#define NC_NDIRTY 0x40  /* numrecs has changed */
248#define NC_HDIRTY 0x80  /* header info has changed */
249
250/* This is the metadata we need to keep track of for each
251   netcdf-4/HDF5 file. */
252typedef struct 
253{
254   hid_t hdfid;
255   int flags;
256   int cmode;
257   int nvars;
258   int ndims;
259   int natts;
260   int parallel;  /* true if file is open for parallel access */
261   int redef;
262   char *path;
263   int fill_mode;
264   int no_write; /* true if nc_open has mode NC_NOWRITE. */
265   NC_GRP_INFO_T *root_grp;
266   short next_nc_grpid;
267   NC_TYPE_INFO_T *type;
268   int next_typeid;
269   int next_dimid;
270   int ignore_creationorder;
271   int hdf4;
272   int sdid;
273} NC_HDF5_FILE_INFO_T;
274
275/* In the nc_file array there will be one entry for each open file.*/
276
277   /* There's an external ncid (ext_ncid) and an internal ncid
278    * (int_ncid). The ext_ncid is the ncid returned to the user. If
279    * the user has opened or created a netcdf-4 file, then the
280    * ext_ncid is the same as the int_ncid. If he has opened or
281    * created a netcdf-3 file ext_ncid (which the user sees) is
282    * different from the int_ncid, which is the ncid returned by the
283    * netcdf-3 layer, which insists on inventing its own ncids,
284    * regardless of what is already in use due to previously opened
285    * netcdf-4 files. The ext_ncid contains the ncid for the root
286    * group (i.e. group zero). */
287
288/* Warning: fields from BEGIN COMMON to END COMMON must be same for:
289        1. NCcommon (include/ncdispatch.h)
290        2. NC (libsrc/nc.h)
291        3. NC_FILE_INFO (libsrc4/nc4internal.h)
292        4. whatever libdiskless uses
293*/
294typedef struct NC_FILE_INFO
295{
296/*BEGIN COMMON*/
297   int ext_ncid;
298   int int_ncid;
299   struct NC_Dispatch* dispatch;       
300   void* dispatchdata;
301   char* path;
302   int substrate;
303/*END COMMON*/
304
305#ifdef USE_PNETCDF
306   /* pnetcdf_file will be true if the file is created/opened with the
307    * parallel-netcdf library. pnetcdf_access_mode keeps track of
308    * whether independpent or collective mode is
309    * desired. pnetcdf_ndims keeps track of how many dims each var
310    * has, which I need to know to convert start, count, and stride
311    * arrays from size_t to MPI_Offset. (I can't use an inq function
312    * to find out the number of dims, because these are collective in
313    * pnetcdf.) */
314   int pnetcdf_file;
315   int pnetcdf_access_mode;
316   int pnetcdf_ndims[NC_MAX_VARS];
317#endif /* USE_PNETCDF */
318
319   /* The nc4_info pointer will remain NULL for netcdf3 files,
320    * otherwise it points to information about the netcdf-4 file. */
321   NC_HDF5_FILE_INFO_T *nc4_info;
322} NC_FILE_INFO_T;
323
324/* These functions only use the netcdf API calls, so they will work on
325   both new format and old format files. */
326/*int copy_dataset(int ncid_in, int ncid_out);*/
327
328
329/* These functions convert beteen netcdf and HDF5 types. */
330int nc4_get_typelen_mem(NC_HDF5_FILE_INFO_T *h5, nc_type xtype, 
331                        int is_long, size_t *len);
332int nc4_convert_type(const void *src, void *dest, 
333                     const nc_type src_type, const nc_type dest_type, 
334                     const size_t len, int *range_error, 
335                     const void *fill_value, int strict_nc3, int src_long,
336                     int dest_long);
337
338/* These functions do HDF5 things. */
339int nc4_open_var_grp2(NC_GRP_INFO_T *grp, int varid, hid_t *dataset);
340int pg_var(NC_PG_T pg, NC_FILE_INFO_T *nc, int ncid, int varid, nc_type xtype, int is_long, 
341           void *ip);
342int nc4_pg_var1(NC_PG_T pg, NC_FILE_INFO_T *nc, int ncid, int varid, const size_t *indexp, 
343                nc_type xtype, int is_long, void *ip);
344int nc4_put_vara(NC_FILE_INFO_T *nc, int ncid, int varid, const size_t *startp, 
345                 const size_t *countp, nc_type xtype, int is_long, void *op);
346int nc4_get_vara(NC_FILE_INFO_T *nc, int ncid, int varid, const size_t *startp, 
347                 const size_t *countp, nc_type xtype, int is_long, void *op);
348int nc4_pg_varm(NC_PG_T pg, NC_FILE_INFO_T *nc, int ncid, int varid, const size_t *startp, 
349                const size_t *countp, const ptrdiff_t *stridep,
350                const ptrdiff_t *imapp, nc_type xtype, int is_long, void *op);
351int nc4_rec_match_dimscales(NC_GRP_INFO_T *grp);
352int nc4_rec_write_metadata(NC_GRP_INFO_T *grp);
353int nc4_rec_write_types(NC_GRP_INFO_T *grp);
354int nc4_enddef_netcdf4_file(NC_HDF5_FILE_INFO_T *h5);
355int nc4_reopen_dataset(NC_GRP_INFO_T *grp, NC_VAR_INFO_T *var);
356int nc4_adjust_var_cache(NC_GRP_INFO_T *grp, NC_VAR_INFO_T * var);
357
358/* The following functions manipulate the in-memory linked list of
359   metadata, without using HDF calls. */
360int nc4_find_nc_grp_h5(int ncid, NC_FILE_INFO_T **nc, NC_GRP_INFO_T **grp, 
361                       NC_HDF5_FILE_INFO_T **h5);
362int nc4_find_grp_h5(int ncid, NC_GRP_INFO_T **grp, NC_HDF5_FILE_INFO_T **h5);
363int nc4_find_nc4_grp(int ncid, NC_GRP_INFO_T **grp);
364NC_GRP_INFO_T *nc4_find_nc_grp(int ncid);
365NC_GRP_INFO_T *nc4_rec_find_grp(NC_GRP_INFO_T *start_grp, int target_nc_grpid);
366NC_FILE_INFO_T *nc4_find_nc_file(int ncid);
367int nc4_find_dim(NC_GRP_INFO_T *grp, int dimid, NC_DIM_INFO_T **dim, NC_GRP_INFO_T **dim_grp);
368int nc4_find_dim_len(NC_GRP_INFO_T *grp, int dimid, size_t **len);
369int nc4_find_type(NC_HDF5_FILE_INFO_T *h5, int typeid, NC_TYPE_INFO_T **type);
370NC_TYPE_INFO_T *nc4_rec_find_nc_type(NC_GRP_INFO_T *start_grp, hid_t target_nc_typeid);
371NC_TYPE_INFO_T *nc4_rec_find_hdf_type(NC_GRP_INFO_T *start_grp, hid_t target_hdf_typeid);
372NC_TYPE_INFO_T *nc4_rec_find_named_type(NC_GRP_INFO_T *start_grp, char *name);
373NC_TYPE_INFO_T *nc4_rec_find_equal_type(NC_GRP_INFO_T *start_grp, int ncid1, NC_TYPE_INFO_T *type);
374int nc4_find_nc_att(int ncid, int varid, const char *name, int attnum,
375                    NC_ATT_INFO_T **att);
376int nc4_find_g_var_nc(NC_FILE_INFO_T *nc, int ncid, int varid, 
377                      NC_GRP_INFO_T **grp, NC_VAR_INFO_T **var);
378int nc4_find_grp_att(NC_GRP_INFO_T *grp, int varid, const char *name, int attnum,
379                     NC_ATT_INFO_T **att);
380int nc4_get_hdf_typeid(NC_HDF5_FILE_INFO_T *h5, nc_type xtype, 
381                       hid_t *hdf_typeid, int endianness);
382/*int var_info_nc(NC_PG_T pg, hid_t dataset, NC_VAR_INFO_T *var_info);*/
383
384/* These list functions add and delete vars, atts, and files. */
385int nc4_file_list_add(NC_FILE_INFO_T**, struct NC_Dispatch*);
386void nc4_file_list_free(void);
387
388int nc4_nc4f_list_add(NC_FILE_INFO_T *nc, const char *path, int mode);
389int nc4_var_list_add(NC_VAR_INFO_T **list, NC_VAR_INFO_T **var);
390int nc4_dim_list_add(NC_DIM_INFO_T **list);
391int nc4_dim_list_del(NC_DIM_INFO_T **list, NC_DIM_INFO_T *dim);
392int nc4_att_list_add(NC_ATT_INFO_T **list);
393int nc4_type_list_add(NC_TYPE_INFO_T **list, NC_TYPE_INFO_T **new_type);
394int nc4_field_list_add(NC_FIELD_INFO_T **list, int fieldid, const char *name,
395                       size_t offset, hid_t field_hdf_typeid, hid_t native_typeid, 
396                       nc_type xtype, int ndims, const int *dim_sizesp);
397void nc4_file_list_del(NC_FILE_INFO_T *nc);
398int nc4_att_list_del(NC_ATT_INFO_T **list, NC_ATT_INFO_T *att);
399int nc4_grp_list_add(NC_GRP_INFO_T **list, int new_nc_grpid, NC_GRP_INFO_T *parent_grp, 
400                     NC_FILE_INFO_T *nc, char *name, NC_GRP_INFO_T **grp);
401int nc4_rec_grp_del(NC_GRP_INFO_T **list, NC_GRP_INFO_T *grp);
402int nc4_enum_member_add(NC_ENUM_MEMBER_INFO_T **list, size_t size,
403                        const char *name, const void *value);
404
405int NC_check_name(const char *name);
406
407/* Check and normalize names. */
408int nc4_check_name(const char *name, char *norm_name);
409int nc4_normalize_name(const char *name, char *norm_name);
410
411/* Check for name collisions. */
412int nc4_check_dup_name(NC_GRP_INFO_T *grp, char *norm_name);
413
414/* Insert and read one element into an already allocated vlen array
415 * element (this is for F77). */
416/*int nc_put_vlen_element(int ncid, int typeid, void *vlen_element, size_t len, const void *data);
417  int nc_get_vlen_element(int ncid, int typeid, const void *vlen_element, size_t *len, void *data);*/
418
419/* This is only included if --enable-logging is used for configure; it
420   prints info about the metadata to stderr. */
421#ifdef LOGGING
422int log_metadata_nc(NC_FILE_INFO_T *nc);
423#endif
424
425#endif /* _NETCDF4_ */
Note: See TracBrowser for help on using the repository browser.