1 /* Copyright 2018-2018 University Corporation for Atmospheric
2    Research/Unidata. */
3 /**
4  * @file This header file contains macros, types, and prototypes for
5  * the HDF5 code in libhdf5. This header should not be included in
6  * code outside libhdf5.
7  *
8  * @author Ed Hartnett
9  */
10 
11 #ifndef _HDF5INTERNAL_
12 #define _HDF5INTERNAL_
13 
14 #include "config.h"
15 #include <vtk_hdf5.h>
16 #include "nc4internal.h"
17 #include "ncdimscale.h"
18 #include "nc4dispatch.h"
19 #include "hdf5dispatch.h"
20 #include "netcdf_filter.h"
21 
22 #define NC_MAX_HDF5_NAME (NC_MAX_NAME + 10)
23 
24 /* These have to do with creating chunked datasets in HDF5. */
25 #define NC_HDF5_UNLIMITED_DIMSIZE (0)
26 #define NC_HDF5_CHUNKSIZE_FACTOR (10)
27 #define NC_HDF5_MIN_CHUNK_SIZE (2)
28 
29 #define NC_EMPTY_SCALE "NC_EMPTY_SCALE"
30 
31 /* This is an attribute I had to add to handle multidimensional
32  * coordinate variables. See nc4internal.h:NC_ATT_COORDINATES.
33  * in nc4internal.h.
34  */
35 #define COORDINATES NC_ATT_COORDINATES
36 #define COORDINATES_LEN (NC_MAX_NAME * 5)
37 
38 /* This is used when the user defines a non-coordinate variable with
39  * same name as a dimension. */
40 #define NON_COORD_PREPEND "_nc4_non_coord_"
41 
42 /* An attribute in the HDF5 root group of this name means that the
43  * file must follow strict netCDF classic format rules. */
44 #define NC3_STRICT_ATT_NAME NC_ATT_NC3_STRICT_NAME
45 
46 /* If this attribute is present on a dimscale variable, use the value
47  * as the netCDF dimid. */
48 #define NC_DIMID_ATT_NAME NC_ATT_DIMID_NAME /*See nc4internal.h*/
49 
50 /** This is the name of the class HDF5 dimension scale attribute. */
51 #define HDF5_DIMSCALE_CLASS_ATT_NAME NC_ATT_CLASS /*See nc4internal.h*/
52 
53 /** This is the name of the name HDF5 dimension scale attribute. */
54 #define HDF5_DIMSCALE_NAME_ATT_NAME NC_ATT_NAME
55 
56 /* forward */
57 struct NCauth;
58 
59 /** Struct to hold HDF5-specific info for the file. */
60 typedef struct NC_HDF5_FILE_INFO {
61    hid_t hdfid;
62 #if defined(ENABLE_BYTERANGE) || defined(ENABLE_HDF5_ROS3) || defined(ENABLE_S3_SDK)
63    struct HTTP {
64 	NCURI* uri; /* Parse of the incoming path, if url */
65 	int iosp; /* We are using the S3 rawvirtual file driver */
66 	struct NCauth* auth;
67    } http;
68 #endif
69 } NC_HDF5_FILE_INFO_T;
70 
71 /* This is a struct to handle the dim metadata. */
72 typedef struct NC_HDF5_DIM_INFO
73 {
74     hid_t hdf_dimscaleid;        /* Non-zero if a DIM_WITHOUT_VARIABLE dataset is in use (no coord var). */
75     HDF5_OBJID_T hdf5_objid;
76 } NC_HDF5_DIM_INFO_T;
77 
78 /** Strut to hold HDF5-specific info for attributes. */
79 typedef struct  NC_HDF5_ATT_INFO
80 {
81     hid_t native_hdf_typeid;     /* Native HDF5 datatype for attribute's data */
82 } NC_HDF5_ATT_INFO_T;
83 
84 /* Struct to hold HDF5-specific info for a group. */
85 typedef struct NC_HDF5_GRP_INFO
86 {
87     hid_t hdf_grpid;
88 } NC_HDF5_GRP_INFO_T;
89 
90 /* Struct to hold HDF5-specific info for a variable. */
91 typedef struct NC_HDF5_VAR_INFO
92 {
93     hid_t hdf_datasetid;
94     HDF5_OBJID_T *dimscale_hdf5_objids;
95     nc_bool_t dimscale;          /**< True if var is a dimscale. */
96     nc_bool_t *dimscale_attached;  /**< Array of flags that are true if dimscale is attached for that dim index. */
97     int flags;
98 #       define NC_HDF5_VAR_FILTER_MISSING 1 /* if any filter is missing */
99 } NC_HDF5_VAR_INFO_T;
100 
101 /* Struct to hold HDF5-specific info for a field. */
102 typedef struct NC_HDF5_FIELD_INFO
103 {
104     hid_t hdf_typeid;
105     hid_t native_hdf_typeid;
106 } NC_HDF5_FIELD_INFO_T;
107 
108 /* Struct to hold HDF5-specific info for a type. */
109 typedef struct NC_HDF5_TYPE_INFO
110 {
111     hid_t hdf_typeid;
112     hid_t native_hdf_typeid;
113 } NC_HDF5_TYPE_INFO_T;
114 
115 /* Logging and debugging. */
116 void reportopenobjects(int log, hid_t);
117 int hdf5_set_log_level();
118 void nc_log_hdf5(void);
119 
120 /* These functions deal with HDF5 dimension scales. */
121 int rec_detach_scales(NC_GRP_INFO_T *grp, int dimid, hid_t dimscaleid);
122 int rec_reattach_scales(NC_GRP_INFO_T *grp, int dimid, hid_t dimscaleid);
123 int delete_dimscale_dataset(NC_GRP_INFO_T *grp, int dimid, NC_DIM_INFO_T *dim);
124 
125 /* Write metadata. */
126 int nc4_rec_write_metadata(NC_GRP_INFO_T *grp, nc_bool_t bad_coord_order);
127 int nc4_rec_write_groups_types(NC_GRP_INFO_T *grp);
128 
129 /* Adjust the cache. */
130 int nc4_adjust_var_cache(NC_GRP_INFO_T *grp, NC_VAR_INFO_T * var);
131 
132 /* Open a HDF5 dataset. */
133 int nc4_open_var_grp2(NC_GRP_INFO_T *grp, int varid, hid_t *dataset);
134 
135 /* Find types. */
136 NC_TYPE_INFO_T *nc4_rec_find_hdf_type(NC_FILE_INFO_T* h5,
137                                       hid_t target_hdf_typeid);
138 int nc4_get_hdf_typeid(NC_FILE_INFO_T *h5, nc_type xtype,
139                        hid_t *hdf_typeid, int endianness);
140 
141 /* Enddef and closing files. */
142 int nc4_close_hdf5_file(NC_FILE_INFO_T *h5, int abort, NC_memio *memio);
143 int nc4_rec_grp_HDF5_del(NC_GRP_INFO_T *grp);
144 int nc4_enddef_netcdf4_file(NC_FILE_INFO_T *h5);
145 int nc4_HDF5_close_type(NC_TYPE_INFO_T* type);
146 
147 /* Break & reform coordinate variables */
148 int nc4_break_coord_var(NC_GRP_INFO_T *grp, NC_VAR_INFO_T *coord_var, NC_DIM_INFO_T *dim);
149 int nc4_reform_coord_var(NC_GRP_INFO_T *grp, NC_VAR_INFO_T *coord_var, NC_DIM_INFO_T *dim);
150 
151 /* In-memory functions */
152 extern hid_t NC4_image_init(NC_FILE_INFO_T* h5);
153 extern void NC4_image_finalize(void*);
154 
155 /* Create HDF5 dataset for dim without a coord var. */
156 extern int nc4_create_dim_wo_var(NC_DIM_INFO_T *dim);
157 
158 /* Give a var a secret HDF5 name, for use when there is a dim of this
159  * name, but the var is not a coord var of that dim. */
160 extern int nc4_give_var_secret_name(NC_VAR_INFO_T *var);
161 
162 /* Find file, group, var, and att info, doing lazy reads if needed. */
163 int nc4_hdf5_find_grp_var_att(int ncid, int varid, const char *name, int attnum,
164                               int use_name, char *norm_name, NC_FILE_INFO_T **h5,
165                               NC_GRP_INFO_T **grp, NC_VAR_INFO_T **var,
166                               NC_ATT_INFO_T **att);
167 
168 /* Find var, doing lazy var metadata read if needed. */
169 int nc4_hdf5_find_grp_h5_var(int ncid, int varid, NC_FILE_INFO_T **h5,
170                              NC_GRP_INFO_T **grp, NC_VAR_INFO_T **var);
171 
172 int nc4_HDF5_close_att(NC_ATT_INFO_T *att);
173 
174 /* Perform lazy read of the rest of the metadata for a var. */
175 int nc4_get_var_meta(NC_VAR_INFO_T *var);
176 
177 /* Get the file chunk cache settings from HDF5. */
178 int nc4_hdf5_get_chunk_cache(int ncid, size_t *sizep, size_t *nelemsp,
179 			     float *preemptionp);
180 /* Filter Dispatch Entries */
181 int NC4_hdf5_def_var_filter(int ncid, int varid, unsigned int filterid, size_t nparams, const unsigned int *params);
182 int NC4_hdf5_inq_var_filter_ids(int ncid, int varid, size_t* nfiltersp, unsigned int *filterids);
183 int NC4_hdf5_inq_var_filter_info(int ncid, int varid, unsigned int filterid, size_t* nparamsp, unsigned int *params);
184 
185 /* Filterlist management */
186 
187 /* The NC_VAR_INFO_T->filters field is an NClist of this struct */
188 struct NC_HDF5_Filter {
189     int flags;             /**< Flags describing state of this filter. */
190 #       define NC_HDF5_FILTER_MISSING 1 /* Filter implementation is not accessible */
191     unsigned int filterid; /**< ID for arbitrary filter. */
192     size_t nparams;        /**< nparams for arbitrary filter. */
193     unsigned int* params;  /**< Params for arbitrary filter. */
194 };
195 
196 int NC4_hdf5_filter_remove(NC_VAR_INFO_T* var, unsigned int id);
197 int NC4_hdf5_filter_lookup(NC_VAR_INFO_T* var, unsigned int id, struct NC_HDF5_Filter** fi);
198 int NC4_hdf5_addfilter(NC_VAR_INFO_T* var, unsigned int id, size_t nparams, const unsigned int* params, int flags);
199 int NC4_hdf5_filter_freelist(NC_VAR_INFO_T* var);
200 int NC4_hdf5_find_missing_filter(NC_VAR_INFO_T* var, unsigned int* idp);
201 
202 /* Support functions for provenance info (defined in nc4hdf.c) */
203 extern int NC4_hdf5get_libversion(unsigned*,unsigned*,unsigned*);/*libsrc4/nc4hdf.c*/
204 extern int NC4_hdf5get_superblock(struct NC_FILE_INFO*, int*);/*libsrc4/nc4hdf.c*/
205 extern int NC4_isnetcdf4(struct NC_FILE_INFO*); /*libsrc4/nc4hdf.c*/
206 
207 extern int nc4_find_default_chunksizes2(NC_GRP_INFO_T *grp, NC_VAR_INFO_T *var);
208 
209 EXTERNL hid_t nc4_H5Fopen(const char *filename, unsigned flags, hid_t fapl_id);
210 EXTERNL hid_t nc4_H5Fcreate(const char *filename, unsigned flags, hid_t fcpl_id, hid_t fapl_id);
211 
212 #endif /* _HDF5INTERNAL_ */
213