4 #ifndef tools_hdf5_tools
5 #define tools_hdf5_tools
15 if(
sizeof(
bool)!=1)
return false;
16 if(
sizeof(
char)!=1)
return false;
17 if(
sizeof(
short)!=2)
return false;
18 if(
sizeof(
int)!=4)
return false;
19 if(
sizeof(
float)!=4)
return false;
20 if(
sizeof(
double)!=8)
return false;
28 hid_t datatype = ::H5Tcopy(H5T_C_S1);
29 if(datatype<0)
return failure();
31 if(::H5Tset_size(datatype,aSize)<0) {
36 if(::H5Tset_strpad(datatype,H5T_STR_NULLTERM)<0) {
45 hid_t datatype = ::H5Tcopy(H5T_C_S1);
46 if(datatype<0)
return failure();
47 if(::H5Tset_size(datatype,H5T_VARIABLE)<0) {
51 if(::H5Tset_strpad(datatype,H5T_STR_NULLTERM)<0) {
59 H5T_class_t mclass = H5Tget_class(a_file_type);
60 size_t msize = H5Tget_size(a_file_type);
61 if(mclass==H5T_INTEGER) {
62 H5T_sign_t msign = H5Tget_sign(a_file_type);
64 if(msign==H5T_SGN_NONE) {
65 return H5Tcopy(H5T_NATIVE_UCHAR);
67 return H5Tcopy(H5T_NATIVE_CHAR);
70 if(msign==H5T_SGN_NONE) {
71 return H5Tcopy(H5T_NATIVE_UINT);
73 return H5Tcopy(H5T_NATIVE_INT);
76 if(msign==H5T_SGN_NONE) {
77 return H5Tcopy(H5T_NATIVE_UINT64);
79 return H5Tcopy(H5T_NATIVE_INT64);
84 }
else if(mclass==H5T_FLOAT) {
86 return H5Tcopy(H5T_NATIVE_FLOAT);
88 return H5Tcopy(H5T_NATIVE_DOUBLE);
92 }
else if(mclass==H5T_STRING) {
93 return H5Tcopy(a_file_type);
109 H5T_class_t t_class = H5Tget_class(a_file_type);
110 if(t_class!=H5T_COMPOUND)
return failure();
112 size_t sz = H5Tget_size(a_file_type);
115 hid_t mem_type = ::H5Tcreate(H5T_COMPOUND,sz);
116 if(mem_type<0)
return failure();
120 int mn = H5Tget_nmembers(a_file_type);
121 std::vector<unsigned int> szs(
mn);
123 for(
int index=0;index<
mn;index++) {
124 char* mname = H5Tget_member_name(a_file_type,index);
125 size_t moffset = H5Tget_member_offset(a_file_type,index);
126 hid_t mtype = H5Tget_member_type(a_file_type,index);
129 {H5T_class_t mclass = H5Tget_class(mtype);
130 if( (mclass==H5T_INTEGER) ||
131 (mclass==H5T_STRING) ||
132 (mclass==H5T_FLOAT) ) {
137 ::H5Tclose(mem_type);
140 if(H5Tinsert(mem_type,mname,moffset,mmem_type)<0) {
141 ::H5Tclose(mmem_type);
144 ::H5Tclose(mem_type);
147 ::H5Tclose(mmem_type);
149 }
else if(mclass==H5T_ARRAY) {
150 int dimn = ::H5Tget_array_ndims(mtype);
151 hsize_t* dims =
new hsize_t[dimn];
152 int* perms =
new int[dimn];
158 ::H5Tclose(mem_type);
161 hid_t base_type = H5Tget_super(mtype);
167 ::H5Tclose(mem_type);
174 ::H5Tclose(base_type);
177 ::H5Tclose(mem_type);
180 ::H5Tclose(base_type);
185 ::H5Tclose(mmem_type);
188 ::H5Tclose(mem_type);
191 ::H5Tclose(mmem_type);
193 if(H5Tinsert(mem_type,mname,moffset,array_type)<0) {
194 ::H5Tclose(array_type);
197 ::H5Tclose(mem_type);
200 ::H5Tclose(array_type);
202 }
else if(mclass==H5T_COMPOUND) {
207 ::H5Tclose(mem_type);
210 if(H5Tinsert(mem_type,mname,moffset,mmem_type)<0) {
211 ::H5Tclose(mmem_type);
214 ::H5Tclose(mem_type);
217 ::H5Tclose(mmem_type);
221 ::H5Tclose(mem_type);
240 inline bool read_atb(hid_t a_id,
const std::string& a_name,std::string& a_data,
unsigned int aSize = 100){
243 char* b =
new char[aSize];
249 a_data = std::string(b);
254 inline bool read_atb(hid_t a_id,
const std::string& a_name,
unsigned int& a_data){
260 inline bool read_atb(hid_t a_id,
const std::string& a_name,
int& a_data){
283 hid_t file_space = H5Dget_space(dataset);
290 int dimn = H5Sget_simple_extent_ndims(file_space);
292 ::H5Sclose(file_space);
298 ::H5Sclose(file_space);
306 {
if(H5Sget_simple_extent_dims(file_space,dims,NULL)<0) {
307 ::H5Sclose(file_space);
313 ::H5Sclose(file_space);
320 inline bool write_atb(hid_t a_id,
const std::string& a_name,
const std::string& a_data){
324 if(H5Adelete(a_id,a_name.c_str())<0)
return false;
328 if(datatype<0)
return false;
330 hid_t scalar = ::H5Screate(H5S_SCALAR);
332 ::H5Tclose(datatype);
336 hid_t aid =
tools_H5Acreate(a_id,a_name.c_str(),datatype,scalar,H5P_DEFAULT);
339 ::H5Tclose(datatype);
343 if(H5Awrite(aid,datatype,a_data.c_str())<0) {
346 ::H5Tclose(datatype);
352 ::H5Tclose(datatype);
357 inline bool write_bool(hid_t a_loc,
const std::string& a_name,
bool a_data) {
358 hid_t scalar = ::H5Screate(H5S_SCALAR);
359 if(scalar<0)
return false;
361 hid_t compact = ::H5Pcreate(H5P_DATASET_CREATE);
366 if(H5Pset_layout(compact,H5D_COMPACT)<0) {
379 unsigned char data = a_data?1:0;
380 if(::H5Dwrite(dataset,H5T_NATIVE_UCHAR,H5S_ALL,H5S_ALL,H5P_DEFAULT,&data)<0) {
393 inline bool write_string(hid_t a_loc,
const std::string& a_name,
const std::string& a_string) {
394 hid_t scalar = ::H5Screate(H5S_SCALAR);
395 if(scalar<0)
return false;
397 hid_t compact = ::H5Pcreate(H5P_DATASET_CREATE);
403 if(H5Pset_layout(compact,H5D_COMPACT)<0) {
417 hid_t dataset =
tools_H5Dcreate(a_loc,a_name.c_str(),file_type,scalar,compact);
421 ::H5Tclose(file_type);
425 hid_t mem_type = file_type;
426 if(H5Dwrite(dataset,mem_type,H5S_ALL,H5S_ALL,H5P_DEFAULT,a_string.c_str())<0) {
430 ::H5Tclose(file_type);
437 ::H5Tclose(file_type);
443 unsigned int a_chunked,
unsigned int a_compress,
444 const std::string& a_string) {
446 if(a_compress || a_chunked) {
447 cpt = ::H5Pcreate(H5P_DATASET_CREATE);
448 if(cpt<0)
return false;
450 if(H5Pset_layout(cpt,H5D_CHUNKED)<0) {
455 cdims[0] = a_chunked;
456 if(H5Pset_chunk(cpt,1,cdims)<0) {
461 if(H5Pset_layout(cpt,H5D_COMPACT)<0) {
467 if(H5Pset_deflate(cpt,a_compress>9?9:a_compress)<0) {
478 if(cpt>=0) ::H5Pclose(cpt);
482 hid_t file_space = -1;
487 mx_dims[0] = H5S_UNLIMITED;
488 file_space = ::H5Screate_simple(1,dims,mx_dims);
490 file_space = ::H5Screate_simple(1,dims,NULL);
492 if(file_space<0) {
if(cpt>=0) ::H5Pclose(cpt);::H5Tclose(file_type);
return false;}}
494 hid_t dataset =
tools_H5Dcreate(a_loc,a_name.c_str(),file_type,file_space,cpt);
495 if(cpt>=0) ::H5Pclose(cpt);
496 ::H5Sclose(file_space);
498 ::H5Tclose(file_type);
502 hid_t mem_type = file_type;
504 const char* wdata[1];
505 wdata[0] = a_string.c_str();
507 if(H5Dwrite(dataset,mem_type,H5S_ALL,H5S_ALL,H5P_DEFAULT,wdata)<0) {
509 ::H5Tclose(file_type);
513 ::H5Tclose(file_type);
520 const std::string& a_string,
521 unsigned int a_chunked = 0,
unsigned int a_compress = 0) {
526 hsize_t old_size = 0;
528 {hid_t dataspace = H5Dget_space(a_dataset);
529 if(dataspace<0)
return false;
531 if(H5Sget_simple_extent_dims(dataspace,dims,NULL)<0) {
532 ::H5Sclose(dataspace);
536 ::H5Sclose(dataspace);}
539 exts[0] = old_size+1;
541 if(H5Dset_extent(a_dataset,exts)<0)
return false;}
543 hid_t file_space = H5Dget_space(a_dataset);
544 if(file_space<0)
return false;
547 offset[0] = old_size;
550 if(H5Sselect_hyperslab(file_space,H5S_SELECT_SET,offset,NULL,count,NULL)<0) {
551 ::H5Sclose(file_space);
557 hid_t mem_space = ::H5Screate_simple(1,dims,NULL);
559 ::H5Sclose(file_space);
565 ::H5Sclose(mem_space);
566 ::H5Sclose(file_space);
570 const char* wdata[1];
571 wdata[0] = a_string.c_str();
573 if(H5Dwrite(a_dataset,mem_type,mem_space,file_space,H5P_DEFAULT,wdata)<0) {
574 ::H5Tclose(mem_type);
575 ::H5Sclose(mem_space);
576 ::H5Sclose(file_space);
580 ::H5Tclose(mem_type);
581 ::H5Sclose(mem_space);
582 ::H5Sclose(file_space);
594 inline bool write_array_string(hid_t a_loc,
const std::string& a_name,
const std::vector<std::string>& a_array) {
595 hid_t scalar = ::H5Screate(H5S_SCALAR);
596 if(scalar<0)
return false;
613 hid_t dataset =
tools_H5Dcreate(a_loc,a_name.c_str(),file_type,scalar,H5P_DEFAULT);
616 ::H5Tclose(file_type);
621 hid_t mem_type = file_type;
622 if(H5Dwrite(dataset,mem_type,H5S_ALL,H5S_ALL,H5P_DEFAULT,buffer)<0) {
625 ::H5Tclose(file_type);
633 ::H5Tclose(file_type);
638 inline bool write_object(hid_t a_loc,
const std::string& a_name,hid_t a_file_type,
char* a_data) {
639 unsigned int chunked = 0;
640 unsigned int compress = 0;
643 if(compress || chunked) {
644 cpt = ::H5Pcreate(H5P_DATASET_CREATE);
645 if(cpt<0)
return false;
647 if(H5Pset_layout(cpt,H5D_CHUNKED)<0) {
653 if(H5Pset_chunk(cpt,1,cdims)<0) {
658 if(H5Pset_layout(cpt,H5D_COMPACT)<0) {
664 if(H5Pset_deflate(cpt,compress>9?9:compress)<0) {
675 hid_t simple = ::H5Screate_simple(1,dims,NULL);
677 if(cpt>=0) ::H5Pclose(cpt);
684 if(cpt>=0) ::H5Pclose(cpt);
688 hid_t dataset =
tools_H5Dcreate(a_loc,a_name.c_str(),a_file_type,simple,cpt);
690 ::H5Tclose(mem_type);
692 if(cpt>=0) ::H5Pclose(cpt);
696 if(H5Dwrite(dataset,mem_type,H5S_ALL,H5S_ALL,H5P_DEFAULT,a_data)<0) {
698 ::H5Tclose(mem_type);
700 if(cpt>=0) ::H5Pclose(cpt);
705 ::H5Tclose(mem_type);
707 if(cpt>=0) ::H5Pclose(cpt);
712 inline bool read_string(hid_t a_loc,
const std::string& a_name,std::string& a_string) {
720 hid_t file_type = H5Dget_type(dataset);
727 H5T_class_t t_class = H5Tget_class(file_type);
728 if(t_class!=H5T_STRING) {
729 ::H5Tclose(file_type);
735 size_t sz = H5Tget_size(file_type);
736 ::H5Tclose(file_type);
752 char* buff =
new char[sz];
753 herr_t stat = H5Dread(dataset,mem_type,H5S_ALL,H5S_ALL,H5P_DEFAULT,buff);
754 ::H5Tclose(mem_type);
763 a_string.resize(len,0);
764 for(
size_t index=0;index<len;index++) a_string[index] = buff[index];
771 inline bool read_sub_string(hid_t a_loc,
const std::string& a_name,
unsigned int a_offset,std::string& a_string) {
778 hid_t file_space = H5Dget_space(dataset);
785 {
int dimn = H5Sget_simple_extent_ndims(file_space);
787 ::H5Sclose(file_space);
793 ::H5Sclose(file_space);
800 {
if(H5Sget_simple_extent_dims(file_space,dims,NULL)<0) {
801 ::H5Sclose(file_space);
807 {
unsigned int sz = (
unsigned int)dims[0];
809 ::H5Sclose(file_space);
817 int remain = sz-a_offset;
819 ::H5Sclose(file_space);
826 offset[0] = a_offset;
829 if(H5Sselect_hyperslab(file_space,H5S_SELECT_SET,offset,NULL,count,NULL)<0) {
830 ::H5Sclose(file_space);
837 hid_t mem_space = ::H5Screate_simple(1,dims,NULL);
839 ::H5Sclose(file_space);
845 hid_t file_type = H5Dget_type(dataset);
847 ::H5Sclose(file_space);
853 H5T_class_t t_class = H5Tget_class(file_type);
854 if(t_class!=H5T_STRING) {
855 ::H5Tclose(file_type);
856 ::H5Sclose(file_space);
871 ::H5Tclose(file_type);
875 ::H5Sclose(file_space);
882 herr_t stat = H5Dread(dataset,mem_type,mem_space,file_space,H5P_DEFAULT,rdata);
884 ::H5Dvlen_reclaim(mem_type,mem_space, H5P_DEFAULT,rdata);
885 ::H5Tclose(mem_type);
886 ::H5Sclose(mem_space);
887 ::H5Sclose(file_space);
893 char* buff = rdata[0];
895 size_t len = ::strlen(buff);
896 a_string.resize(len,0);
897 for(
size_t index=0;index<len;index++) a_string[index] = buff[index];
899 ::H5Dvlen_reclaim(mem_type,mem_space, H5P_DEFAULT,rdata);
901 ::H5Tclose(mem_type);
902 ::H5Sclose(mem_space);
903 ::H5Sclose(file_space);
909 inline bool read_object(hid_t a_loc,
const std::string& a_name,
size_t& a_size,
char*& a_data) {
917 hid_t file_type = H5Dget_type(dataset);
925 H5T_class_t t_class = H5Tget_class(file_type);
926 if(t_class!=H5T_COMPOUND) {
927 ::H5Tclose(file_type);
934 size_t sz = H5Tget_size(file_type);
936 ::H5Tclose(file_type);
945 ::H5Tclose(file_type);
952 ::H5Tclose(file_type);
954 hid_t dataspace = H5Dget_space(dataset);
956 ::H5Tclose(mem_type);
963 hid_t scalar = ::H5Screate(H5S_SCALAR);
965 ::H5Sclose(dataspace);
966 ::H5Tclose(mem_type);
973 char* buffer =
new char[sz];
974 if(H5Dread(dataset,mem_type,scalar,dataspace,H5P_DEFAULT,buffer)<0) {
977 ::H5Sclose(dataspace);
978 ::H5Tclose(mem_type);
986 ::H5Sclose(dataspace);
987 ::H5Tclose(mem_type);
995 inline bool read_array_string(hid_t a_loc,
const std::string& a_name,std::vector<std::string>& a_array) {
998 if(dataset<0)
return false;
1000 hid_t file_type = H5Dget_type(dataset);
1002 ::H5Dclose(dataset);
1006 H5T_class_t t_class = H5Tget_class(file_type);
1007 if(t_class!=H5T_STRING) {
1008 ::H5Tclose(file_type);
1009 ::H5Dclose(dataset);
1013 size_t sz = H5Tget_size(file_type);
1014 ::H5Tclose(file_type);
1016 ::H5Dclose(dataset);
1024 ::H5Dclose(dataset);
1028 char* buffer =
new char[sz];
1029 herr_t stat = H5Dread(dataset,mem_type,H5S_ALL,H5S_ALL,H5P_DEFAULT,buffer);
1030 ::H5Tclose(mem_type);
1031 ::H5Dclose(dataset);