1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2  * Copyright by The HDF Group.                                               *
3  * Copyright by the Board of Trustees of the University of Illinois.         *
4  * All rights reserved.                                                      *
5  *                                                                           *
6  * This file is part of HDF5.  The full HDF5 copyright notice, including     *
7  * terms governing use, modification, and redistribution, is contained in    *
8  * the COPYING file, which can be found at the root of the source code       *
9  * distribution tree, or in https://support.hdfgroup.org/ftp/HDF5/releases.  *
10  * If you do not have access to either file, you may request a copy from     *
11  * help@hdfgroup.org.                                                        *
12  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
13 
14 /*
15  *  This example illustrates how to create a compressed dataset.
16  *  It is used in the HDF5 Tutorial.
17  */
18 
19 #include <iostream>
20 using std::cout;
21 using std::endl;
22 
23 #include <string>
24 #include "H5Cpp.h"
25 using namespace H5;
26 
27 const H5std_string      FILE_NAME("h5tutr_cmprss.h5");
28 const H5std_string      DATASET_NAME("Compressed_Data");
29 const int       DIM0 = 100;
30 const int       DIM1 = 20;
31 
main(void)32 int main (void)
33 {
34     hsize_t dims[2] = { DIM0, DIM1 };   // dataset dimensions
35     hsize_t chunk_dims[2] = { 20, 20 }; // chunk dimensions
36     int     i,j, buf[DIM0][DIM1];
37 
38     // Try block to detect exceptions raised by any of the calls inside it
39     try
40     {
41         // Turn off the auto-printing when failure occurs so that we can
42         // handle the errors appropriately
43         Exception::dontPrint();
44 
45         // Create a new file using the default property lists.
46         H5File file(FILE_NAME, H5F_ACC_TRUNC);
47 
48         // Create the data space for the dataset.
49         DataSpace *dataspace = new DataSpace(2, dims);
50 
51         // Modify dataset creation property to enable chunking
52         DSetCreatPropList  *plist = new  DSetCreatPropList;
53         plist->setChunk(2, chunk_dims);
54 
55         // Set ZLIB (DEFLATE) Compression using level 6.
56         // To use SZIP compression comment out this line.
57         plist->setDeflate(6);
58 
59         // Uncomment these lines to set SZIP Compression
60         // unsigned szip_options_mask = H5_SZIP_NN_OPTION_MASK;
61         // unsigned szip_pixels_per_block = 16;
62         // plist->setSzip(szip_options_mask, szip_pixels_per_block);
63 
64         // Create the dataset.
65         DataSet *dataset = new DataSet(file.createDataSet( DATASET_NAME,
66                                 PredType::STD_I32BE, *dataspace, *plist) );
67 
68         for (i = 0; i< DIM0; i++)
69           for (j=0; j<DIM1; j++)
70               buf[i][j] = i+j;
71 
72         // Write data to dataset.
73         dataset->write(buf, PredType::NATIVE_INT);
74 
75         // Close objects and file.  Either approach will close the HDF5 item.
76         delete dataspace;
77         delete dataset;
78         delete plist;
79         file.close();
80 
81         // -----------------------------------------------
82         // Re-open the file and dataset, retrieve filter
83         // information for dataset and read the data back.
84         // -----------------------------------------------
85 
86         int        rbuf[DIM0][DIM1];
87         int        numfilt;
88         size_t     nelmts={1}, namelen={1};
89         unsigned  flags, filter_info, cd_values[1], idx;
90         char       name[1];
91         H5Z_filter_t filter_type;
92 
93         // Open the file and the dataset in the file.
94         file.openFile(FILE_NAME, H5F_ACC_RDONLY);
95         dataset = new DataSet(file.openDataSet( DATASET_NAME));
96 
97         // Get the create property list of the dataset.
98         plist = new DSetCreatPropList(dataset->getCreatePlist ());
99 
100         // Get the number of filters associated with the dataset.
101         numfilt = plist->getNfilters();
102         cout << "Number of filters associated with dataset: " << numfilt << endl;
103 
104         for (idx=0; idx < numfilt; idx++) {
105             nelmts = 0;
106 
107             filter_type = plist->getFilter(idx, flags, nelmts, cd_values, namelen, name , filter_info);
108 
109             cout << "Filter Type: ";
110 
111             switch (filter_type) {
112               case H5Z_FILTER_DEFLATE:
113                    cout << "H5Z_FILTER_DEFLATE" << endl;
114                    break;
115               case H5Z_FILTER_SZIP:
116                    cout << "H5Z_FILTER_SZIP" << endl;
117                    break;
118               default:
119                    cout << "Other filter type included." << endl;
120               }
121         }
122 
123         // Read data.
124         dataset->read(rbuf, PredType::NATIVE_INT);
125 
126         delete plist;
127         delete dataset;
128         file.close();   // can be skipped
129 
130     }  // end of try block
131 
132     // catch failure caused by the H5File operations
133     catch(FileIException error)
134     {
135         error.printErrorStack();
136         return -1;
137     }
138 
139     // catch failure caused by the DataSet operations
140     catch(DataSetIException error)
141     {
142         error.printErrorStack();
143         return -1;
144     }
145 
146     // catch failure caused by the DataSpace operations
147     catch(DataSpaceIException error)
148     {
149         error.printErrorStack();
150         return -1;
151     }
152 
153     return 0;  // successfully terminated
154 }
155 
156