1 /*
2  * Example illustrates the use of SZIP compression in HDF5
3  */
4 
5 #include <stdio.h>
6 #include "hdf5.h"
7 
8 #define NX 500
9 #define NY 600
10 #define CH_NX 100
11 #define CH_NY 25
12 
13 static void initialize(void);
14 static int compare(void);
15 
16 static float buf[NX][NY];
17 static float buf_r[NX][NY];
18 
19 static const char* filename = "test.h5";
20 
21 static int
writeszip()22 writeszip()
23 {
24    hid_t file;
25    hid_t dataset32;
26    hid_t properties;
27    hid_t lcpl_id, dapl_id;
28    hid_t data_space;
29    hsize_t dims[2], chunk_size[2];
30    unsigned szip_options_mask;
31    unsigned szip_pixels_per_block;
32 
33   /*
34    * Create a new file using read/write access, default file
35    * creation properties, and default file access properties.
36    */
37    file = H5Fcreate (filename, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
38 
39    /* Describe the size of the array. */
40    dims[0] = NX;
41    dims[1] = NY;
42    data_space = H5Screate_simple (2, dims, NULL);
43 
44   /*
45    * Set the dataset creation property list to specify that
46    * the raw data is to be partitioned into 100x100 element
47    * chunks and that each chunk is to be compressed.
48    */
49    chunk_size[0] = CH_NX;
50    chunk_size[1] = CH_NY;
51    properties = H5Pcreate (H5P_DATASET_CREATE);
52    H5Pset_chunk (properties, 2, chunk_size);
53 
54   /*
55    * Set parameters for SZIP compression; check the description of
56    * the H5Pset_szip function in the HDF5 Reference Manual for more
57    * information.
58    */
59    szip_options_mask=H5_SZIP_NN_OPTION_MASK;
60    szip_pixels_per_block=32;
61 
62    H5Pset_szip (properties, szip_options_mask, szip_pixels_per_block);
63 
64   /*
65    * Create a new dataset within the file.  The datatype
66    * and data space describe the data on disk, which may
67    * be different from the format used in the application's
68    * memory.
69    */
70 
71    lcpl_id = H5Pcreate (H5P_LINK_CREATE);
72    dapl_id = H5Pcreate (H5P_DATASET_ACCESS);
73 
74    dataset32 = H5Dcreate (file, "datasetF32", H5T_NATIVE_FLOAT, data_space, lcpl_id, properties, dapl_id);
75 
76   /*
77    * Write the array to the file.  The datatype and dataspace
78    * describe the format of the data in the `buf' buffer.
79    * The raw data is translated to the format required on disk,
80    * as defined above.  We use default raw data transfer properties.
81    */
82 
83    H5Dwrite (dataset32, H5T_NATIVE_FLOAT, H5S_ALL, H5S_ALL,
84             H5P_DEFAULT, buf);
85 
86    H5Sclose (data_space);
87    H5Pclose(lcpl_id);
88    H5Pclose(dapl_id);
89    H5Pclose (properties);
90    H5Dclose (dataset32);
91    H5Fclose (file);
92 
93    return 1;
94 }
95 
96 static int
readszip()97 readszip()
98 {
99     hid_t file;
100     hid_t dataset32;
101     hid_t properties;
102     int errcnt = 0;
103 
104     file = H5Fopen (filename, H5F_ACC_RDONLY, H5P_DEFAULT);
105     properties = H5Pcreate(H5P_DATASET_ACCESS);
106     dataset32 = H5Dopen(file, "datasetF32", properties);
107 
108     /*
109      * Read the array.  This is similar to writing data,
110      * except the data flows in the opposite direction.
111      * Note: Decompression is automatic.
112      */
113 
114     H5Dread(dataset32, H5T_NATIVE_FLOAT, H5S_ALL, H5S_ALL, H5P_DEFAULT, buf_r);
115 
116     errcnt = compare();
117 
118     H5Pclose(properties);
119     H5Dclose (dataset32);
120     H5Fclose (file);
121 
122     return (errcnt==0 ? 1 : 0);
123 }
124 
125 static int
compare(void)126 compare(void)
127 {
128     int i,j;
129     int errs = 0;
130 
131     /* Do comparison */
132     for (i=0; i < NX; i++) {
133 	for (j=0; j < NY; j++) {
134             if(buf[i][j] != buf_r[i][j]) {
135 		errs++;
136 	        printf("mismatch: [%d][%d]: write = %f read=%f\n",
137 		        i,j,buf[i][j],buf_r[i][j]);
138 	}
139      }
140    }
141    return errs;
142 }
143 
144 static void
initialize(void)145 initialize(void)
146 {
147    int i, j;
148    /* Initialize data buffer with some bogus data. */
149    for(i=0; i < NX; i++) {
150      for(j=0; j < NY; j++) {
151        buf[i][j] = (float)(i + j);
152      }
153    }
154 }
155 
156 int
main(int argc,char ** argv)157 main(int argc, char** argv)
158 {
159     int extfile = 0;
160     if(argc > 1) {
161 	filename = argv[1];
162 	extfile = 1;
163     }
164 
165     initialize();
166     if(!extfile) {
167 	if(!writeszip()) {
168 	    fprintf(stderr,"writeszip failed.\n");
169 	    goto fail;
170 	}
171     }
172 
173     if(!readszip()) {
174 	fprintf(stderr,"openfile failed.\n");
175 	goto fail;
176     }
177     fprintf(stderr,"***PASS\n");
178     return 0;
179 fail:
180     fprintf(stderr,"***FAIL\n");
181     return 1;
182 }
183