99// ////////////////////////////////////////
1010
1111#include " hdf5functions_blosc.h"
12- #include < array>
1312
1413
1514/* *
@@ -19,118 +18,83 @@ void register_blosc(){
1918 register_blosc (nullptr , nullptr );
2019}
2120
21+ /* *
22+ * reads data from hdf5
23+ */
2224void hdf5_load_data_blosc (hid_t obj_id, hid_t data_type, void * buff, const char * data_name) {
23- // stuff required to pull the data in
24- hid_t data_id = H5Dopen2 (obj_id , data_name ,H5P_DEFAULT);
25- H5Dread (data_id, data_type, H5S_ALL, H5S_ALL,H5P_DEFAULT, buff);
25+ hid_t data_id = H5Dopen2 (obj_id, data_name ,H5P_DEFAULT);
26+ H5Dread (data_id, data_type, H5S_ALL, H5S_ALL, H5P_DEFAULT, buff);
2627 H5Dclose (data_id);
2728}
2829
29- void hdf5_write_data_blosc (hid_t obj_id,hid_t type_id,const char * ds_name,hsize_t rank,hsize_t * dims, void * data ,unsigned int comp_type,unsigned int comp_level,unsigned int shuffle){
30- // writes data to the hdf5 file or group identified by obj_id of hdf5 datatype data_type
31-
32- unsigned int cd_values[7 ];
33- // Declare the required hdf5 shiz
34- hid_t space_id,dset_id,plist_id;
35- // hsize_t *cdims = new hsize_t[rank]; //chunking dims
36- hsize_t cdims[1 ]; // chunking dims
37-
38- // compression parameters
39-
40-
41- // int szip_options_mask = H5_SZIP_NN_OPTION_MASK;
42- // int szip_pixels_per_block = 8;
30+ /* *
31+ * writes data to the hdf5 file or group identified by obj_id of hdf5 datatype data_type
32+ */
33+ void hdf5_write_data_blosc (hid_t obj_id, hid_t type_id, const char *ds_name, hsize_t rank, hsize_t *dims, void *data ,unsigned int comp_type,unsigned int comp_level,unsigned int shuffle) {
34+ hid_t plist_id = H5Pcreate (H5P_DATASET_CREATE);
35+
36+ // Dataset must be chunked for compression
37+ const uint64_t max_size = 100000 ;
38+ hsize_t cdims = (dims[0 ] < max_size) ? dims[0 ] : max_size;
4339 rank = 1 ;
44- // dataspace id
45- space_id = H5Screate_simple (rank, dims, NULL );
46- plist_id = H5Pcreate (H5P_DATASET_CREATE);
47-
48- /* Dataset must be chunked for compression */
49- // cdims[0] = 20; //Could try playing with these for compression performance
50- // cdims[1] = 20;
51-
52- uint64_t max_size = 100000 ;
53-
54- if (dims[0 ] < max_size){
55- cdims[0 ] = dims[0 ];
56- }else {
57- cdims[0 ] = max_size;
58- }
40+ H5Pset_chunk (plist_id, rank, &cdims);
5941
60- H5Pset_chunk (plist_id, rank, cdims);
61-
6242 // ///SET COMPRESSION TYPE /////
63-
64- /* But you can also taylor Blosc parameters to your needs */
65- /* 0 to 3 (inclusive) param slots are reserved. */
66- cd_values[4 ] = comp_level; /* compression level */
67- cd_values[5 ] = shuffle; /* 0: shuffle not active, 1: shuffle active */
68- cd_values[6 ] = comp_type; /* the actual compressor to use */
69-
70- /* Set the filter with 7 params */
71- H5Pset_filter (plist_id, FILTER_BLOSC, H5Z_FLAG_OPTIONAL, 7 , cd_values);
72-
43+ // But you can also taylor Blosc parameters to your needs
44+ // 0 to 3 (inclusive) param slots are reserved.
45+ const int numOfParams = 7 ;
46+ unsigned int cd_values[numOfParams];
47+ cd_values[4 ] = comp_level; // compression level
48+ cd_values[5 ] = shuffle; // 0: shuffle not active, 1: shuffle active
49+ cd_values[6 ] = comp_type; // the actual compressor to use
50+ H5Pset_filter (plist_id, FILTER_BLOSC, H5Z_FLAG_OPTIONAL, numOfParams, cd_values);
51+
7352 // create write and close
74- dset_id = H5Dcreate2 (obj_id,ds_name,type_id,space_id,H5P_DEFAULT,plist_id,H5P_DEFAULT );
75-
53+ hid_t space_id = H5Screate_simple (rank, dims, NULL );
54+ hid_t dset_id = H5Dcreate2 (obj_id, ds_name, type_id, space_id, H5P_DEFAULT, plist_id, H5P_DEFAULT);
7655 H5Dwrite (dset_id,type_id,H5S_ALL,H5S_ALL,H5P_DEFAULT,data);
77-
7856 H5Dclose (dset_id);
57+
58+ H5Pclose (plist_id);
7959}
8060
61+ /* *
62+ * writes data to the hdf5 file or group identified by obj_id of hdf5 datatype data_type
63+ */
8164void hdf5_write_attribute_blosc (hid_t obj_id,hid_t type_id,const char * attr_name,hsize_t rank,hsize_t * dims, void * data ){
82- // writes data to the hdf5 file or group identified by obj_id of hdf5 datatype data_type
83-
84- // Declare the required hdf5 shiz
85- hid_t space_id, attr_id;
86- // hsize_t *cdims = new hsize_t[rank]; //chunking dims
87-
88- space_id = H5Screate_simple (rank, dims, NULL );
89- // plist_id = H5Pcreate(H5P_ATTRIBUTE_CREATE);
90-
91- attr_id = H5Acreate2 ( obj_id, attr_name, type_id, space_id, H5P_DEFAULT, H5P_DEFAULT);
92-
93- H5Awrite (attr_id, type_id, data );
65+ hid_t space_id = H5Screate_simple (rank, dims, NULL );
66+ hid_t attr_id = H5Acreate2 ( obj_id, attr_name, type_id, space_id, H5P_DEFAULT, H5P_DEFAULT);
67+ H5Awrite (attr_id, type_id, data);
9468 H5Aclose (attr_id);
95-
69+ H5Sclose (space_id);
9670}
9771
98- void hdf5_write_string_blosc (hid_t obj_id,const char * attr_name,std::string output_str){
99- //
100- // Writes string information as an attribute
101- //
102- //
103-
104- hid_t aid = H5Screate (H5S_SCALAR);
105- hid_t atype = H5Tcopy (H5T_C_S1);
106-
72+ /* *
73+ * Writes string information as an attribute
74+ */
75+ void hdf5_write_string_blosc (hid_t obj_id, const char *attr_name, const std::string &output_str) {
10776 if (output_str.size () > 0 ){
108- H5Tset_size (atype, output_str.size ());
77+ hid_t aid = H5Screate (H5S_SCALAR);
78+ hid_t atype = H5Tcopy (H5T_C_S1);
79+ H5Tset_size (atype, output_str.size ());
10980 hid_t attr = H5Acreate2 (obj_id, attr_name, atype, aid, H5P_DEFAULT,H5P_DEFAULT);
11081 H5Awrite (attr, atype,output_str.c_str ());
82+ H5Tclose (atype);
83+ H5Aclose (attr);
84+ H5Sclose (aid);
11185 }
11286}
11387
114- void hdf5_create_file_blosc (std::string file_name){
115- // creates the hdf5 file before you can then write to it
116-
117- hid_t fid; // file id
118-
119- // fid = H5F.create(name,'H5F_ACC_EXCL', 'H5P_DEFAULT', 'H5P_DEFAULT'); %create the file (throws error if it already exists)
120- fid = H5Fcreate (file_name.c_str (),H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); // this writes over the current file
121-
122- // close shiz
123- H5Fclose (fid);
124-
88+ /* *
89+ * creates the hdf5 file before you can then write to it
90+ */
91+ hid_t hdf5_create_file_blosc (std::string file_name){
92+ return H5Fcreate (file_name.c_str (),H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); // this writes over the current file
12593}
12694
12795void write_main_paraview_xdmf_xml (std::string save_loc,std::string file_name,uint64_t num_parts){
128- std::string hdf5_file_name = file_name + " .h5" ;
129- std::string xdmf_file_name = save_loc + file_name + " .xmf" ;
130-
131- std::ofstream myfile;
132- myfile.open (xdmf_file_name);
133-
96+ const std::string hdf5_file_name = file_name + " .h5" ;
97+ std::ofstream myfile (save_loc + file_name + " .xmf" );
13498 myfile << " <?xml version=\" 1.0\" ?>\n " ;
13599 myfile << " <!DOCTYPE Xdmf SYSTEM \" Xdmf.dtd\" []>\n " ;
136100 myfile << " <Xdmf Version=\" 2.0\" xmlns:xi=\" [http://www.w3.org/2001/XInclude]\" >\n " ;
@@ -166,6 +130,5 @@ void write_main_paraview_xdmf_xml(std::string save_loc,std::string file_name,uin
166130 myfile << " </Grid>\n " ;
167131 myfile << " </Domain>\n " ;
168132 myfile << " </Xdmf>\n " ;
169-
170133 myfile.close ();
171134}
0 commit comments