1 /* This is part of the netCDF package.
2    Copyright 2005 University Corporation for Atmospheric Research/Unidata
3    See COPYRIGHT file for conditions of use.
4 
5    Test netcdf-4 variables.
6    Ed Hartnett, Russ Rew, Dennis Heimbigner, Ward Fisher
7 */
8 
9 #include <nc_tests.h>
10 #include "err_macros.h"
11 #include "netcdf.h"
12 
13 #define FILE_NAME "tst_vars3.nc"
14 #define NDIMS1 1
15 #define NDIMS2 2
16 #define D_SMALL "small_dim"
17 #define D_SMALL_LEN 16
18 #define D_MEDIUM "medium_dim"
19 #define D_MEDIUM_LEN 65546
20 #define D_LARGE "large_dim"
21 #define D_LARGE_LEN 1048586
22 #define V_SMALL "small_var"
23 #define V_MEDIUM "medium_var"
24 #define V_LARGE "large_var"
25 #define D_MAX_ONE_D 16384
26 
27 int
main(int argc,char ** argv)28 main(int argc, char **argv)
29 {
30 
31    printf("\n*** Testing netcdf-4 variable functions, some more.\n");
32    printf("**** testing definition of coordinate variable after endef/redef...");
33    {
34 #define NX 6
35 #define NY 36
36 #define ZD1_NAME "zD1"
37 #define D2_NAME "D2"
38 
39       int ncid, x_dimid, y_dimid, varid2;
40       char name_in[NC_MAX_NAME + 1];
41 
42       /* Create file with two dims, two 1D vars. */
43       if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
44       if (nc_def_dim(ncid, ZD1_NAME, NX, &x_dimid)) ERR;
45       if (nc_def_dim(ncid, D2_NAME, NY, &y_dimid)) ERR;
46       if (nc_enddef(ncid)) ERR;
47 
48       /* Go back into define mode and add a coordinate variable. Now
49        * dimensions will be out of order. Thanks for confusing my poor
50        * library. Why can't you just make up your bloody mind? */
51       if (nc_redef(ncid)) ERR;
52       if (nc_def_var(ncid, ZD1_NAME, NC_DOUBLE, NDIMS1, &x_dimid, &varid2)) ERR;
53       if (nc_close(ncid)) ERR;
54 
55       /* Reopen file and check the name of the first dimension. Even
56        * though you've changed the order after doing a redef, you will
57        * still expect to get D1_NAME. I sure hope you appreciate how
58        * hard you are making life for a poor C library, just trying to
59        * do its best in a demanding world. Next time, why don't you
60        * try and be a little bit more considerate? Jerk. */
61       if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR;
62       if (nc_inq_dimname(ncid, 0, name_in)) ERR;
63       if (strcmp(name_in, ZD1_NAME)) ERR;
64       if (nc_inq_dimname(ncid, 1, name_in)) ERR;
65       if (strcmp(name_in, D2_NAME)) ERR;
66       if (nc_close(ncid)) ERR;
67    }
68    SUMMARIZE_ERR;
69    printf("**** testing definition of coordinate variable with some data...");
70    {
71 #define NX 6
72 #define NY 36
73 #define V1_NAME "V1"
74 #define D1_NAME "D1"
75 #define D2_NAME "D2"
76 
77       int ncid, x_dimid, y_dimid, varid1, varid2;
78       int nvars, ndims, ngatts, unlimdimid, dimids_in[2], natts;
79       size_t len_in;
80       char name_in[NC_MAX_NAME + 1];
81       nc_type xtype_in;
82 #if 0
83       int x, y;
84       double data_outx[NX], data_outy[NY];
85       int retval;
86 #endif
87 
88 #if 0
89       /* Create some pretend data. */
90       for (x = 0; x < NX; x++)
91      	 data_outx[x] = x;
92       for (y = 0; y < NY; y++)
93      	 data_outy[y] = y;
94 #endif
95 
96       /* Create file with two dims, two 1D vars. */
97       if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
98       if (nc_def_dim(ncid, D1_NAME, NX, &x_dimid)) ERR;
99       if (nc_def_dim(ncid, D2_NAME, NY, &y_dimid)) ERR;
100       if (nc_def_var(ncid, V1_NAME, NC_DOUBLE, NDIMS1, &y_dimid, &varid1)) ERR;
101       if (nc_enddef(ncid)) ERR;
102       if (nc_redef(ncid)) ERR;
103       if (nc_def_var(ncid, D1_NAME, NC_DOUBLE, NDIMS1, &x_dimid, &varid2)) ERR;
104 
105 /*       if (nc_put_var_double(ncid, varid1, &data_outy[0])) ERR; */
106 /*       if (nc_put_var_double(ncid, varid2, &data_outx[0])) ERR; */
107 /*       if (nc_sync(ncid)) ERR; */
108 
109       /* Check the file. */
110       if (nc_inq(ncid, &ndims, &nvars, &ngatts, &unlimdimid)) ERR;
111       if (nvars != 2 || ndims != 2 || ngatts != 0 || unlimdimid != -1) ERR;
112 
113       /* Check the dimensions. */
114       if (nc_inq_dimids(ncid, &ndims, dimids_in, 1)) ERR;
115       if (ndims != 2 || dimids_in[0] != x_dimid || dimids_in[1] != y_dimid) ERR;
116       if (nc_inq_dim(ncid, dimids_in[0], name_in, &len_in)) ERR;
117       if (strcmp(name_in, D1_NAME) || len_in != NX) ERR;
118       if (nc_inq_dim(ncid, dimids_in[1], name_in, &len_in)) ERR;
119       if (strcmp(name_in, D2_NAME) || len_in != NY) ERR;
120 
121       /* Check the variables. */
122       if (nc_inq_var(ncid, 0, name_in, &xtype_in, &ndims, dimids_in, &natts)) ERR;
123       if (strcmp(name_in, V1_NAME) || xtype_in != NC_DOUBLE || ndims != 1 ||
124 	  natts != 0 || dimids_in[0] != y_dimid) ERR;
125       if (nc_inq_var(ncid, 1, name_in, &xtype_in, &ndims, dimids_in, &natts)) ERR;
126       if (strcmp(name_in, D1_NAME) || xtype_in != NC_DOUBLE || ndims != 1 ||
127 	  natts != 0 || dimids_in[0] != x_dimid) ERR;
128 
129       /* Close the file. */
130       if (nc_close(ncid)) ERR;
131 
132       /* Reopen and check the file. */
133       if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR;
134       if (nc_inq(ncid, &ndims, &nvars, &ngatts, &unlimdimid)) ERR;
135       if (nvars != 2 || ndims != 2 || ngatts != 0 || unlimdimid != -1) ERR;
136 
137       /* Check the dimensions. */
138       if (nc_inq_dimids(ncid, &ndims, dimids_in, 1)) ERR;
139       if (ndims != 2 || dimids_in[0] != x_dimid || dimids_in[1] != y_dimid) ERR;
140       if (nc_inq_dim(ncid, dimids_in[0], name_in, &len_in)) ERR;
141       if (strcmp(name_in, D1_NAME) || len_in != NX) ERR;
142       if (nc_inq_dim(ncid, dimids_in[1], name_in, &len_in)) ERR;
143       if (strcmp(name_in, D2_NAME) || len_in != NY) ERR;
144 
145       /* Check the variables. */
146       if (nc_inq_var(ncid, 0, name_in, &xtype_in, &ndims, dimids_in, &natts)) ERR;
147       if (strcmp(name_in, V1_NAME) || xtype_in != NC_DOUBLE || ndims != 1 ||
148 	  natts != 0 || dimids_in[0] != y_dimid) ERR;
149       if (nc_inq_var(ncid, 1, name_in, &xtype_in, &ndims, dimids_in, &natts)) ERR;
150       if (strcmp(name_in, D1_NAME) || xtype_in != NC_DOUBLE || ndims != 1 ||
151 	  natts != 0 || dimids_in[0] != x_dimid) ERR;
152 
153       if (nc_close(ncid)) ERR;
154 
155    }
156    SUMMARIZE_ERR;
157    printf("**** testing endianness of compound type variable...");
158    {
159 #define COMPOUND_NAME "Billy-Bob"
160 #define BILLY "Billy"
161 #define BOB "Bob"
162 #define VAR_NAME1 "Buddy-Joe"
163 #define NDIMS 2
164 #define TEXT_LEN 15
165       int ncid, nvars_in, varids_in[1], typeid, varid;
166       int nvars, ndims, ngatts, unlimdimid;
167       int ndims_in, natts_in, dimids_in[NDIMS];
168       char var_name_in[NC_MAX_NAME + 1];
169       nc_type xtype_in;
170       struct billy_bob
171       {
172 	    int billy;
173 	    int bob;
174       };
175 
176       /* Create a netcdf-4 file with scalar compound var. */
177       if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
178       if (nc_def_compound(ncid, sizeof(struct billy_bob), COMPOUND_NAME, &typeid)) ERR;
179       if (nc_insert_compound(ncid, typeid, BILLY, NC_COMPOUND_OFFSET(struct billy_bob, billy), NC_INT)) ERR;
180       if (nc_insert_compound(ncid, typeid, BOB, NC_COMPOUND_OFFSET(struct billy_bob, bob), NC_INT)) ERR;
181       if (nc_def_var(ncid, VAR_NAME1, typeid, 0, NULL, &varid)) ERR;
182       if (nc_def_var_endian(ncid, varid, NC_ENDIAN_BIG)) ERR;
183       if (nc_close(ncid)) ERR;
184 
185       /* Open the file and check. */
186       if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR;
187       if (nc_inq(ncid, &ndims, &nvars, &ngatts, &unlimdimid)) ERR;
188       if (nvars != 1 || ndims != 0 || ngatts != 0 || unlimdimid != -1) ERR;
189       if (nc_inq_varids(ncid, &nvars_in, varids_in)) ERR;
190       if (nvars_in != 1 || varids_in[0] != 0) ERR;
191       if (nc_inq_var(ncid, 0, var_name_in, &xtype_in, &ndims_in, dimids_in, &natts_in)) ERR;
192       if (strcmp(var_name_in, VAR_NAME1) || xtype_in <= NC_STRING || ndims_in != 0 ||
193 	  natts_in != 0) ERR;
194       if (nc_close(ncid)) ERR;
195    }
196    SUMMARIZE_ERR;
197    printf("**** testing that fixed vars with no filter end up being contiguous...");
198    {
199 #define VAR_NAME2 "Yoman_of_the_Guard"
200 #define NDIMS 2
201 #define D0_NAME1 "Tower_warders_under_orders"
202 #define D0_LEN 55
203 #define D1_NAME1 "When_our_gallent_Norman_Foes"
204 #define D1_LEN 99
205       int ncid, varid;
206       int nvars, ndims, ngatts, unlimdimid;
207       int dimids[NDIMS], contig;
208       int ndims_in, natts_in, dimids_in[NDIMS];
209       char var_name_in[NC_MAX_NAME + 1];
210       nc_type xtype_in;
211 
212       /* Create a netcdf-4 file with 2D fixed var. */
213       if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
214       if (nc_def_dim(ncid, D0_NAME1, D0_LEN, &dimids[0])) ERR;
215       if (nc_def_dim(ncid, D1_NAME1, D1_LEN, &dimids[1])) ERR;
216       if (nc_def_var(ncid, VAR_NAME2, NC_UINT64, NDIMS, dimids, &varid)) ERR;
217       if (nc_def_var_endian(ncid, varid, NC_ENDIAN_BIG)) ERR;
218       if (nc_close(ncid)) ERR;
219 
220       /* Open the file and check. */
221       if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR;
222       if (nc_inq(ncid, &ndims, &nvars, &ngatts, &unlimdimid)) ERR;
223       if (nvars != 1 || ndims != 2 || ngatts != 0 || unlimdimid != -1) ERR;
224       if (nc_inq_var(ncid, 0, var_name_in, &xtype_in, &ndims_in, dimids_in, &natts_in)) ERR;
225       if (strcmp(var_name_in, VAR_NAME2) || xtype_in != NC_UINT64 || ndims_in != 2 ||
226 	  natts_in != 0) ERR;
227       if (nc_inq_var_chunking(ncid, varid, &contig, NULL)) ERR;
228       if (!contig) ERR;
229       if (nc_close(ncid)) ERR;
230    }
231    SUMMARIZE_ERR;
232    printf("**** testing typeless access for classic model...");
233    {
234 #define RANK_P 3
235 #define LEN 4
236       int ncid, dimids[RANK_P], time_id, p_id;
237       int ndims, dimids_in[RANK_P];
238 
239       double data[1] = {3.14159};
240       size_t start[1] = {0}, count[1] = {1};
241       static float P_data[LEN];
242       size_t cor[RANK_P] = {0, 1, 0};
243       size_t edg[RANK_P] = {1, 1, LEN};
244       int i;
245 
246       /* Create a 3D test file. */
247       if (nc_create(FILE_NAME, NC_CLASSIC_MODEL|NC_NETCDF4, &ncid)) ERR;
248 
249       /* define dimensions */
250       if (nc_def_dim(ncid, "Time", NC_UNLIMITED, &dimids[0])) ERR;
251       if (nc_def_dim(ncid, "X", 4, &dimids[2])) ERR;
252       if (nc_def_dim(ncid, "Y", 3, &dimids[1])) ERR;
253 
254       /* define variables */
255       if (nc_def_var(ncid, "Time", NC_DOUBLE, 1, dimids, &time_id)) ERR;
256       if (nc_def_var(ncid, "P", NC_FLOAT, RANK_P, dimids, &p_id)) ERR;
257       if (nc_enddef(ncid)) ERR;
258 
259       /* Add one record in coordinate variable. */
260       if (nc_put_vara(ncid, time_id, start, count, data)) ERR;
261 
262       /* The other variable should show an increase in size, since it
263        * uses the unlimited dimension. */
264       if (nc_inq_var(ncid, 1, NULL, NULL, &ndims, dimids_in, NULL)) ERR;
265       if (ndims != 3 || dimids_in[0] != 0 || dimids_in[1] != 2 || dimids_in[2] != 1) ERR;
266 
267       /* These will not work due to bad parameters. */
268       if (nc_get_vara(ncid + MILLION, 1, cor, edg, P_data) != NC_EBADID) ERR;
269       if (nc_get_vara(ncid + TEST_VAL_42, 1, cor, edg, P_data) != NC_EBADID) ERR;
270 
271       /* Read the record of non-existent data. */
272       if (nc_get_vara(ncid, 1, cor, edg, P_data)) ERR;
273       for (i = 0; i < LEN; i++)
274 	 if (P_data[i] != NC_FILL_FLOAT) ERR;
275 
276       /* That's it! */
277       if (nc_close(ncid)) ERR;
278 
279       /* Reopen the file and read the second slice. */
280       if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR;
281 
282       if (nc_inq_var(ncid, 1, NULL, NULL, &ndims, dimids_in, NULL)) ERR;
283       if (ndims != 3 || dimids_in[0] != 0 || dimids_in[1] != 2 || dimids_in[2] != 1) ERR;
284       if (nc_get_vara(ncid, 1, cor, edg, P_data)) ERR;
285       for (i = 0; i < LEN; i++)
286 	 if (P_data[i] != NC_FILL_FLOAT) ERR;
287 
288       if (nc_close(ncid)) ERR;
289    }
290    SUMMARIZE_ERR;
291    printf("**** testing large number of vars with unlimited dimension...");
292    {
293 #define D0_NAME3 "dim0"
294 #define NUM_VARS 1000
295 
296       int ncid, varid;
297       int nvars, ndims, ngatts, unlimdimid;
298       int dimid;
299       char var_name[NC_MAX_NAME + 1];
300       int v;
301 
302       /* Create a netcdf-4 file with lots of 1D unlim dim vars. */
303       if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
304       if (nc_def_dim(ncid, D0_NAME3, NC_UNLIMITED, &dimid)) ERR;
305       for (v = 0; v < NUM_VARS; v++)
306       {
307 	 sprintf(var_name, "var_%d", v);
308 	 if (nc_def_var(ncid, var_name, NC_INT, 1, &dimid, &varid)) ERR_RET;
309 	 if (nc_set_var_chunk_cache(ncid, varid, 0, 0, 0.75)) ERR_RET;
310       }
311       if (nc_close(ncid)) ERR;
312 
313       /* Open the file and check. */
314       if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR;
315       if (nc_inq(ncid, &ndims, &nvars, &ngatts, &unlimdimid)) ERR;
316       if (nvars != NUM_VARS || ndims != 1 || ngatts != 0 || unlimdimid != 0) ERR;
317       if (nc_close(ncid)) ERR;
318    }
319    SUMMARIZE_ERR;
320    printf("**** testing fix of bug in checking coordinate variables out of order...");
321    {
322 #define GRP_NAME "group_G"
323 #define TIME_DIMNAME "time"
324 #define B_DIMNAME "bDim"
325 #define C_DIMNAME "cDim"
326 #define C_VARNAME "c"
327 #define D_VARNAME "dd"
328 #define E_VARNAME "ee"
329       int ncid, grpid;
330       int timeDimId, bDimId, cDimId, dimidIn;
331       size_t timeDimSize = 2, bDimSize = 3, cDimSize = 1;
332       int cNdims = 1, eeNdims = 1, ddNdims = 1 ;
333       int cVarId, eeVarId, ddVarId ;
334       size_t index = 0;
335       double s1Data = 10;
336       const double timeVar[] = {1.3, 4.6 };
337 
338       if ( nc_create(FILE_NAME, NC_NETCDF4, &ncid) ) ERR;
339       if ( nc_def_grp(ncid, GRP_NAME, &grpid) ) ERR;
340       if ( nc_def_dim(ncid, TIME_DIMNAME, timeDimSize, &timeDimId) ) ERR;
341       if ( nc_def_dim(grpid, B_DIMNAME, bDimSize, &bDimId) ) ERR;
342       if ( nc_def_dim(grpid, C_DIMNAME, cDimSize, &cDimId) ) ERR;
343       if ( nc_def_var(grpid, C_VARNAME, NC_DOUBLE, cNdims, &cDimId, &cVarId) ) ERR;
344       if ( nc_def_var(ncid, E_VARNAME, NC_DOUBLE, eeNdims, &timeDimId, &eeVarId) ) ERR;
345       /* worked without this enddef, but in 4.2.1.1 and earlier, inserting this caused failure */
346       if ( nc_enddef(ncid)) ERR;
347       if ( nc_def_var(grpid, D_VARNAME, NC_DOUBLE, ddNdims, &timeDimId, &ddVarId)) ERR;
348       if ( nc_put_var1(grpid, cVarId, &index, &s1Data) ) ERR;
349       if ( nc_put_var_double(grpid, ddVarId, timeVar)) ERR;
350       if ( nc_close(ncid)) ERR;
351 
352       /* Open the file and check. */
353       if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR;
354       if (nc_inq_grp_ncid(ncid, GRP_NAME, &grpid)) ERR;
355       if (nc_inq_varid(grpid, D_VARNAME, &ddVarId)) ERR;
356       if (nc_inq_vardimid(grpid, ddVarId, &dimidIn)) ERR;
357       if (nc_inq_dimid(ncid, TIME_DIMNAME, &timeDimId)) ERR;
358       if (nc_inq_dimid(grpid, C_DIMNAME, &cDimId)) ERR;
359       if (dimidIn == cDimId || cDimId == timeDimId) ERR; /* bug in 4.2.1.1 and earlier */
360       if (nc_close(ncid)) ERR;
361    }
362    SUMMARIZE_ERR;
363    printf("**** testing fix of bug in non-coordinate scalar variable with same name as dimension ...");
364    {
365 #define DIMNAME "abc"
366 #define SCALAR_VARNAME DIMNAME
367       int ncid;
368       int dimid, varid;
369       int ndims = 1;
370       size_t dimsize = 3;
371       char varname_in[NC_MAX_NAME];
372 
373       if ( nc_create(FILE_NAME, NC_NETCDF4, &ncid) ) ERR;
374       if ( nc_def_dim(ncid, DIMNAME, dimsize, &dimid) ) ERR;
375       if ( nc_def_var(ncid, SCALAR_VARNAME, NC_FLOAT, ndims, &dimid, &varid) ) ERR;
376       if ( nc_close(ncid)) ERR;
377 
378       /* Open the file and check varname. */
379       if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR;
380       if (nc_inq_varid(ncid, SCALAR_VARNAME, &varid)) ERR;
381       if (nc_inq_varname(ncid, varid, varname_in)) ERR;
382       if (strcmp(varname_in, SCALAR_VARNAME) != 0) ERR;
383       if (nc_close(ncid)) ERR;
384    }
385    SUMMARIZE_ERR;
386    printf("**** testing bad inputs to put/get_vara calls...");
387    {
388       int ncid, dimid[NDIMS2], varid;
389       size_t start[NDIMS2] = {0, 0}, count[NDIMS2] = {NX, NY};
390       double double_data[NX * NY];
391 
392       /* Create file with two dims, one 2D var. */
393       if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
394       if (nc_def_dim(ncid, ZD1_NAME, NX, &dimid[0])) ERR;
395       if (nc_def_dim(ncid, D2_NAME, NY, &dimid[1])) ERR;
396       if (nc_def_var(ncid, ZD1_NAME, NC_DOUBLE, NDIMS2, dimid, &varid)) ERR;
397       if (nc_enddef(ncid)) ERR;
398 
399       /* Try to write some data, but fail. */
400       if (nc_put_vara_double(ncid + MILLION, 0, start, count, double_data) != NC_EBADID) ERR;
401       if (nc_put_vara_double(ncid + TEST_VAL_42, 0, start, count, double_data) != NC_EBADID) ERR;
402 
403       if (nc_close(ncid)) ERR;
404    }
405    SUMMARIZE_ERR;
406 #ifdef USE_SZIP
407    printf("**** testing simple szip filter setup...");
408    {
409 #define NDIMS1 1
410 #define DIM_NAME_1 "one_dim"
411 #define DIM_LEN_1 100
412 #define VAR_NAME "var1"
413 #define H5_FILTER_SZIP 4
414 #define NUM_PARAMS_IN 2
415 #define NUM_PARAMS_OUT 4
416 #define NC_SZIP_NN_OPTION_MASK 32 /**< @internal SZIP NN option mask. */
417 #define NC_SZIP_EC_OPTION_MASK 4  /**< @internal SZIP EC option mask. */
418 #define NC_SZIP_EC_BPP_IN 32  /**< @internal bits per pixel input. */
419 #define NC_SZIP_EC_BPP_OUT 64  /**< @internal bits per pixel output. */
420       int ncid;
421       int dimid;
422       int varid;
423       unsigned int params[NUM_PARAMS_IN];
424       int options_mask, bits_per_pixel;
425       size_t nparams;
426       unsigned int filterid;
427       unsigned int params_out[NUM_PARAMS_OUT];
428       unsigned int tmp;
429 
430       /* Create a netcdf-4 file with one dimensions. */
431       if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
432       if (nc_def_dim(ncid, DIM_NAME_1, DIM_LEN_1, &dimid)) ERR;
433 
434       /* Add a var. Turn on szip filter. */
435       if (nc_def_var(ncid, V_SMALL, NC_INT64, NDIMS1, &dimid, &varid)) ERR;
436       params[0] = NC_SZIP_NN_OPTION_MASK; /* options_mask */
437       params[1] = NC_SZIP_EC_BPP_IN; /* bits_per_pixel */
438       if (nc_def_var_chunking(ncid, varid, NC_CHUNKED, NULL)) ERR;
439       if (nc_def_var_filter(ncid, varid, H5_FILTER_SZIP, NUM_PARAMS_IN, params)) ERR;
440       if (nc_close(ncid)) ERR;
441 
442       /* Open the file and check. */
443       if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR;
444       /* The following code should work, but doesn't. See issue 972 in github. */
445       if (nc_inq_var_szip(ncid, varid, &options_mask, &bits_per_pixel)) ERR;
446       /* H5Zszip code will sometimes bump the bits_per_pixel from 32 to 64
447          and may add other flags to the options_mask */
448       tmp = options_mask & NC_SZIP_NN_OPTION_MASK;
449       if (tmp != NC_SZIP_NN_OPTION_MASK) ERR;
450       if (bits_per_pixel !=  NC_SZIP_EC_BPP_IN && bits_per_pixel !=  NC_SZIP_EC_BPP_OUT)
451 		ERR;
452 
453       /* Also check using nc_inq_var_filter */
454       if (nc_inq_var_filter(ncid, varid, &filterid, &nparams, params_out)) ERR;
455       if (filterid != H5_FILTER_SZIP || nparams != 4) ERR;
456       /* According to H5Zszip, the mapping should be as follows */
457       if(params_out[0] != options_mask) ERR;
458       if(params[1] !=  bits_per_pixel) ERR;
459       if (nc_close(ncid)) ERR;
460    }
461    SUMMARIZE_ERR;
462    printf("**** testing more complex use of szip...");
463    {
464 #define NDIMS1 1
465 #define D_SMALL "small_dim"
466 #define D_SMALL_LEN1 100
467 #define D_MEDIUM "medium_dim"
468 #define D_MEDIUM_LEN1 D_SMALL_LEN1 * 2
469 #define D_LARGE "large_dim"
470 #define D_LARGE_LEN1 D_SMALL_LEN1 * 4
471 #define V_SMALL "small_var"
472 #define V_MEDIUM "medium_var"
473 #define V_LARGE "large_var"
474 #define H5_FILTER_SZIP 4
475 #define NUM_PARAMS 2
476 #define NC_SZIP_NN_OPTION_MASK 32 /**< @internal SZIP NN option mask. */
477 #define NC_SZIP_EC_OPTION_MASK 4  /**< @internal SZIP EC option mask. */
478       int ncid;
479       int nvars, ndims, ngatts, unlimdimid;
480       int ndims_in, natts_in, dimids_in;
481       int small_dimid, medium_dimid, large_dimid;
482       int small_varid, medium_varid, large_varid;
483       char var_name_in[NC_MAX_NAME + 1];
484       nc_type xtype_in;
485       /* int options_mask_in, bits_per_pixel_in; */
486       long long small_data[D_SMALL_LEN1], small_data_in[D_SMALL_LEN1];
487       long long medium_data[D_MEDIUM_LEN1], medium_data_in[D_MEDIUM_LEN1];
488       long long large_data[D_LARGE_LEN1], large_data_in[D_LARGE_LEN1];
489       unsigned int params[NUM_PARAMS];
490       int i;
491 
492       for (i = 0; i < D_SMALL_LEN1; i++)
493 	 small_data[i] = i;
494       for (i = 0; i < D_MEDIUM_LEN1; i++)
495 	 medium_data[i] = i;
496       for (i = 0; i < D_LARGE_LEN1; i++)
497 	 large_data[i] = i;
498 
499       /* Create a netcdf-4 file with three dimensions. */
500       if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
501       if (nc_def_dim(ncid, D_SMALL, D_SMALL_LEN1, &small_dimid)) ERR;
502       if (nc_def_dim(ncid, D_MEDIUM, D_MEDIUM_LEN1, &medium_dimid)) ERR;
503       if (nc_def_dim(ncid, D_LARGE, D_LARGE_LEN1, &large_dimid)) ERR;
504 
505       /* Add three vars. Turn on szip for two of them. */
506       if (nc_def_var(ncid, V_SMALL, NC_INT64, NDIMS1, &small_dimid, &small_varid)) ERR;
507 
508       if (nc_def_var(ncid, V_MEDIUM, NC_INT64, NDIMS1, &medium_dimid, &medium_varid)) ERR;
509       params[0] = NC_SZIP_NN_OPTION_MASK;
510       params[1] = 32;
511       if (nc_def_var_chunking(ncid, medium_varid, NC_CHUNKED, NULL)) ERR;
512       if (nc_def_var_filter(ncid, medium_varid, H5_FILTER_SZIP, NUM_PARAMS, params)) ERR;
513       if (nc_def_var(ncid, V_LARGE, NC_INT64, NDIMS1, &large_dimid, &large_varid)) ERR;
514       params[1] = 32;
515       if (nc_def_var_chunking(ncid, large_varid, NC_CHUNKED, NULL)) ERR;
516       if (nc_def_var_filter(ncid, large_varid, H5_FILTER_SZIP, NUM_PARAMS, params)) ERR;
517 
518       /* Write data. */
519       if (nc_put_var_longlong(ncid, small_varid, small_data)) ERR;
520       if (nc_put_var_longlong(ncid, medium_varid, medium_data)) ERR;
521       if (nc_put_var_longlong(ncid, large_varid, large_data)) ERR;
522 
523       if (nc_close(ncid)) ERR;
524 
525       /* Open the file and check. */
526       if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR;
527       if (nc_inq(ncid, &ndims, &nvars, &ngatts, &unlimdimid)) ERR;
528       if (nvars != 3 || ndims != 3 || ngatts != 0 || unlimdimid != -1) ERR;
529       if (nc_inq_var(ncid, 0, var_name_in, &xtype_in, &ndims_in, &dimids_in, &natts_in)) ERR;
530       if (strcmp(var_name_in, V_SMALL) || xtype_in != NC_INT64 || ndims_in != 1 ||
531           natts_in != 0) ERR;
532 
533       /* The following code should work, but doesn't. See issue 972 in github. */
534       /* Make sure we have the szip settings we expect. */
535       /* if (nc_inq_var_szip(ncid, small_varid, &options_mask_in, &bits_per_pixel_in)) ERR; */
536       /* if (options_mask_in != 0 || bits_per_pixel_in !=0) ERR; */
537       /* if (nc_inq_var_szip(ncid, medium_varid, &options_mask_in, &bits_per_pixel_in)) ERR; */
538       /* if (!(options_mask_in & NC_SZIP_EC_OPTION_MASK) || bits_per_pixel_in != 32) ERR; */
539       /* if (nc_inq_var_szip(ncid, large_varid, &options_mask_in, &bits_per_pixel_in)) ERR; */
540       /* if (!(options_mask_in & NC_SZIP_NN_OPTION_MASK) || bits_per_pixel_in != 16) ERR; */
541 
542       /* Read data. */
543       if (nc_get_var_longlong(ncid, small_varid, small_data_in)) ERR;
544       if (nc_get_var_longlong(ncid, medium_varid, medium_data_in)) ERR;
545       if (nc_get_var_longlong(ncid, large_varid, large_data_in)) ERR;
546 
547       /* Check data. */
548       for (i = 0; i < D_SMALL_LEN1; i++)
549 	 if (small_data[i] != small_data_in[i]) ERR;
550       for (i = 0; i < D_MEDIUM_LEN1; i++)
551          if (medium_data[i] != medium_data_in[i]) ERR;
552       for (i = 0; i < D_LARGE_LEN1; i++)
553          if (large_data[i] != large_data_in[i]) ERR;
554 
555       if (nc_close(ncid)) ERR;
556    }
557    SUMMARIZE_ERR;
558 #endif /* USE_SZIP */
559    FINAL_RESULTS;
560 }
561