Geant4 Cross Reference |
1 // Copyright (C) 2010, Guy Barrand. All rights 2 // See the file tools.license for terms. 3 4 #ifndef toolx_hdf5_T_tools 5 #define toolx_hdf5_T_tools 6 7 #include "hdf5_h" 8 9 #include <tools/typedefs> 10 #include <tools/forit> 11 #include <tools/num2s> 12 #include <tools/vdata> 13 //#include <map> 14 15 namespace toolx { 16 namespace hdf5 { 17 18 inline hid_t to_T_file_type(char) {r 19 inline hid_t to_T_file_type(short) {r 20 inline hid_t to_T_file_type(int) {r 21 inline hid_t to_T_file_type(tools::int64) {r 22 23 inline hid_t to_T_file_type(float) {r 24 inline hid_t to_T_file_type(double) {r 25 26 inline hid_t to_T_file_type(unsigned char) {r 27 inline hid_t to_T_file_type(unsigned short) {r 28 inline hid_t to_T_file_type(unsigned int) {r 29 inline hid_t to_T_file_type(tools::uint64) {r 30 31 inline hid_t to_T_mem_type(char) {re 32 inline hid_t to_T_mem_type(short) {re 33 inline hid_t to_T_mem_type(int) {re 34 inline hid_t to_T_mem_type(tools::int64) {re 35 36 inline hid_t to_T_mem_type(float) {re 37 inline hid_t to_T_mem_type(double) {re 38 39 inline hid_t to_T_mem_type(unsigned char) {re 40 inline hid_t to_T_mem_type(unsigned short) {re 41 inline hid_t to_T_mem_type(unsigned int) {re 42 inline hid_t to_T_mem_type(tools::uint64) {re 43 44 template <class T> 45 inline bool write_array(hid_t a_loc,const std: 46 hid_t a_file_type,hid_ 47 unsigned int a_chunked 48 unsigned int a_size,co 49 if(!a_size) return false; 50 51 hid_t cpt = -1; 52 if(a_compress || a_chunked) { 53 cpt = ::H5Pcreate(H5P_DATASET_CREATE); 54 if(cpt<0) return false; 55 if(a_chunked) { 56 if(H5Pset_layout(cpt,H5D_CHUNKED)<0) { 57 ::H5Pclose(cpt); 58 return false; 59 } 60 hsize_t cdims[1]; 61 cdims[0] = a_chunked; 62 if(H5Pset_chunk(cpt,1,cdims)<0) { 63 ::H5Pclose(cpt); 64 return false; 65 } 66 } else { 67 if(H5Pset_layout(cpt,H5D_COMPACT)<0) { 68 ::H5Pclose(cpt); 69 return false; 70 } 71 } 72 if(a_compress) { 73 if(H5Pset_deflate(cpt,a_compress>9?9:a_c 74 ::H5Pclose(cpt); 75 return false; 76 } 77 } 78 } else { 79 cpt = H5P_DEFAULT; 80 } 81 82 hid_t dataset = -1; 83 84 {hsize_t dims[1]; 85 dims[0] = a_size; 86 hid_t file_space = -1; 87 if(a_chunked) { 88 hsize_t mx_dims[1]; 89 mx_dims[0] = H5S_UNLIMITED; //extendable. 90 file_space = ::H5Screate_simple(1,dims,mx_ 91 } else { 92 file_space = ::H5Screate_simple(1,dims,NUL 93 } 94 if(file_space<0) {if(cpt>=0) ::H5Pclose(cpt) 95 dataset = toolx_H5Dcreate(a_loc,a_name.c_str 96 if(cpt>=0) ::H5Pclose(cpt); 97 ::H5Sclose(file_space); 98 if(dataset<0) return false;} 99 100 if(H5Dwrite(dataset,a_mem_type,H5S_ALL,H5S_A 101 ::H5Dclose(dataset); 102 return false; 103 } 104 ::H5Dclose(dataset); 105 106 return true; 107 } 108 109 template <class T> 110 inline bool write_vlen(hid_t a_loc,const std:: 111 hid_t a_file_type,hid_t 112 unsigned int a_chunked, 113 unsigned int a_size,con 114 hid_t cpt = -1; 115 if(a_compress || a_chunked) { 116 cpt = ::H5Pcreate(H5P_DATASET_CREATE); 117 if(cpt<0) return false; 118 if(a_chunked) { 119 if(H5Pset_layout(cpt,H5D_CHUNKED)<0) { 120 ::H5Pclose(cpt); 121 return false; 122 } 123 hsize_t cdims[1]; 124 cdims[0] = a_chunked; 125 if(H5Pset_chunk(cpt,1,cdims)<0) { 126 ::H5Pclose(cpt); 127 return false; 128 } 129 } else { 130 if(H5Pset_layout(cpt,H5D_COMPACT)<0) { 131 ::H5Pclose(cpt); 132 return false; 133 } 134 } 135 if(a_compress) { 136 if(H5Pset_deflate(cpt,a_compress>9?9:a_c 137 ::H5Pclose(cpt); 138 return false; 139 } 140 } 141 } else { 142 cpt = H5P_DEFAULT; 143 } 144 145 hid_t dataset = -1; 146 147 {hsize_t dims[1]; 148 dims[0] = 1; 149 hid_t file_space = -1; 150 if(a_chunked) { 151 hsize_t mx_dims[1]; 152 mx_dims[0] = H5S_UNLIMITED; //extendable. 153 file_space = ::H5Screate_simple(1,dims,mx_ 154 } else { 155 file_space = ::H5Screate_simple(1,dims,NUL 156 } 157 if(file_space<0) {if(cpt>=0) ::H5Pclose(cpt) 158 159 hid_t file_type = ::H5Tvlen_create(a_file_ty 160 161 dataset = toolx_H5Dcreate(a_loc,a_name.c_str 162 if(cpt>=0) ::H5Pclose(cpt); 163 ::H5Sclose(file_space); 164 ::H5Tclose(file_type); 165 if(dataset<0) return false;} 166 167 hid_t mem_type = ::H5Tvlen_create(a_mem_type 168 169 hvl_t wdata[1]; 170 wdata[0].len = a_size; 171 wdata[0].p = (void*)a_array; 172 173 if(H5Dwrite(dataset,mem_type,H5S_ALL,H5S_ALL 174 ::H5Tclose(mem_type); 175 ::H5Dclose(dataset); 176 return false; 177 } 178 ::H5Tclose(mem_type); 179 ::H5Dclose(dataset); 180 181 return true; 182 } 183 184 template <class T> 185 inline bool write_sub_array(hid_t a_loc,const 186 hid_t a_file_type, 187 bool a_create, 188 unsigned int a_chu 189 unsigned int a_siz 190 unsigned int a_off 191 192 int remain = a_size-a_offset; 193 int number = (int(a_number)<=remain) ? int(a 194 if(number<=0) return false; 195 196 hid_t cpt = -1; 197 198 if(a_create) { 199 if(a_compress || a_chunked) { 200 cpt = ::H5Pcreate(H5P_DATASET_CREATE); 201 if(cpt<0) return false; 202 if(a_chunked) { 203 if(H5Pset_layout(cpt,H5D_CHUNKED)<0) { 204 ::H5Pclose(cpt); 205 return false; 206 } 207 hsize_t cdims[1]; 208 //cdims[0] = (a_size<=32?a_size:32); 209 cdims[0] = a_chunked; 210 if(H5Pset_chunk(cpt,1,cdims)<0) { 211 ::H5Pclose(cpt); 212 return false; 213 } 214 } else { 215 if(H5Pset_layout(cpt,H5D_COMPACT)<0) { 216 ::H5Pclose(cpt); 217 return false; 218 } 219 } 220 if(a_compress) { 221 if(H5Pset_deflate(cpt,a_compress>9?9:a 222 ::H5Pclose(cpt); 223 return false; 224 } 225 } 226 } else { 227 cpt = H5P_DEFAULT; 228 } 229 } 230 231 hid_t dataset = -1; 232 233 hid_t file_space = -1; 234 235 if(a_create) { 236 hsize_t dims[1]; 237 dims[0] = a_size; 238 file_space = ::H5Screate_simple(1,dims,NUL 239 if(file_space<0) { 240 if(cpt>=0) ::H5Pclose(cpt); 241 return false; 242 } 243 244 {hsize_t offset[1]; 245 offset[0] = a_offset; 246 hsize_t count[1]; 247 count[0] = number; 248 if(H5Sselect_hyperslab(file_space,H5S_SELE 249 ::H5Sclose(file_space); 250 if(cpt>=0) ::H5Pclose(cpt); 251 return false; 252 }} 253 254 dataset = toolx_H5Dcreate(a_loc,a_name.c_s 255 if(dataset<0) { 256 ::H5Sclose(file_space); 257 if(cpt>=0) ::H5Pclose(cpt); 258 return false; 259 } 260 261 } else { //open an existing dataset : 262 dataset = toolx_H5Dopen(a_loc,a_name.c_str 263 if(dataset<0) { 264 if(cpt>=0) ::H5Pclose(cpt); 265 return false; 266 } 267 268 file_space = H5Dget_space(dataset); 269 if(file_space<0) { 270 ::H5Dclose(dataset); 271 if(cpt>=0) ::H5Pclose(cpt); 272 return false; 273 } 274 275 {hsize_t offset[1]; 276 offset[0] = a_offset; 277 hsize_t count[1]; 278 count[0] = number; 279 if(H5Sselect_hyperslab(file_space,H5S_SELE 280 ::H5Sclose(file_space); 281 ::H5Dclose(dataset); 282 if(cpt>=0) ::H5Pclose(cpt); 283 return false; 284 }} 285 286 } 287 288 hsize_t dims[1]; 289 dims[0] = number; 290 hid_t mem_space = ::H5Screate_simple(1,dims, 291 if(mem_space<0) { 292 ::H5Sclose(file_space); 293 ::H5Dclose(dataset); 294 if(cpt>=0) ::H5Pclose(cpt); 295 return false; 296 } 297 298 if(H5Dwrite(dataset,a_mem_type,mem_space,fil 299 ::H5Sclose(file_space); 300 ::H5Dclose(dataset); 301 if(cpt>=0) ::H5Pclose(cpt); 302 return false; 303 } 304 305 ::H5Sclose(file_space); 306 ::H5Dclose(dataset); 307 if(cpt>=0) ::H5Pclose(cpt); 308 309 return true; 310 } 311 312 template <class T> 313 inline bool write_append_array_dataset(hid_t a 314 unsigne 315 hsize_t old_size = 0; 316 317 {hid_t dataspace = H5Dget_space(a_dataset); 318 if(dataspace<0) return false; 319 hsize_t dims[1]; 320 if(H5Sget_simple_extent_dims(dataspace,dims, 321 ::H5Sclose(dataspace); 322 return false; 323 } 324 old_size = dims[0]; 325 ::H5Sclose(dataspace);} 326 327 {hsize_t exts[1]; 328 exts[0] = old_size+a_number; 329 // if(H5Dextend(dataset,exts)<0) { 330 if(H5Dset_extent(a_dataset,exts)<0) return f 331 332 hid_t file_space = H5Dget_space(a_dataset); 333 if(file_space<0) return false; 334 335 {hsize_t offset[1]; 336 offset[0] = old_size; 337 hsize_t count[1]; 338 count[0] = a_number; 339 if(H5Sselect_hyperslab(file_space,H5S_SELECT 340 ::H5Sclose(file_space); 341 return false; 342 }} 343 344 hsize_t dims[1]; 345 dims[0] = a_number; 346 hid_t mem_space = ::H5Screate_simple(1,dims, 347 if(mem_space<0) { 348 ::H5Sclose(file_space); 349 return false; 350 } 351 352 if(H5Dwrite(a_dataset,a_mem_type,mem_space,f 353 ::H5Sclose(mem_space); 354 ::H5Sclose(file_space); 355 return false; 356 } 357 358 ::H5Sclose(mem_space); 359 ::H5Sclose(file_space); 360 361 return true; 362 } 363 364 template <class T> 365 inline bool write_append_vlen_dataset(hid_t a_ 366 unsigned 367 hsize_t old_size = 0; 368 369 {hid_t dataspace = H5Dget_space(a_dataset); 370 if(dataspace<0) return false; 371 hsize_t dims[1]; 372 if(H5Sget_simple_extent_dims(dataspace,dims, 373 ::H5Sclose(dataspace); 374 return false; 375 } 376 old_size = dims[0]; 377 ::H5Sclose(dataspace);} 378 379 {hsize_t exts[1]; 380 exts[0] = old_size+1; 381 // if(H5Dextend(dataset,exts)<0) { 382 if(H5Dset_extent(a_dataset,exts)<0) return f 383 384 hid_t file_space = H5Dget_space(a_dataset); 385 if(file_space<0) return false; 386 387 {hsize_t offset[1]; 388 offset[0] = old_size; 389 hsize_t count[1]; 390 count[0] = 1; 391 if(H5Sselect_hyperslab(file_space,H5S_SELECT 392 ::H5Sclose(file_space); 393 return false; 394 }} 395 396 hsize_t dims[1]; 397 dims[0] = 1; 398 hid_t mem_space = ::H5Screate_simple(1,dims, 399 if(mem_space<0) { 400 ::H5Sclose(file_space); 401 return false; 402 } 403 404 hid_t mem_type = ::H5Tvlen_create(a_mem_type 405 406 hvl_t wdata[1]; 407 wdata[0].len = a_number; 408 wdata[0].p = (void*)a_array; 409 410 if(H5Dwrite(a_dataset,mem_type,mem_space,fil 411 ::H5Tclose(mem_type); 412 ::H5Sclose(mem_space); 413 ::H5Sclose(file_space); 414 return false; 415 } 416 417 ::H5Tclose(mem_type); 418 ::H5Sclose(mem_space); 419 ::H5Sclose(file_space); 420 421 return true; 422 } 423 424 template <class T> 425 inline bool write_append_array(hid_t a_loc,con 426 unsigned int a_ 427 hid_t dataset = toolx_H5Dopen(a_loc,a_name.c 428 if(dataset<0) return false; 429 bool status = write_append_array_dataset(dat 430 ::H5Dclose(dataset); 431 return status; 432 } 433 434 /* 435 template <class T> 436 inline bool write_array_struct( 437 hid_t a_loc 438 ,const std::string& a_name 439 ,hid_t a_create_type 440 ,hid_t aWriteType 441 ,unsigned int a_size 442 ,const T a_array[] 443 ){ 444 hsize_t dims[1]; 445 dims[0] = a_size; 446 hid_t dataspace = ::H5Screate_simple(1,dims, 447 if(dataspace<0) return false; 448 449 hid_t dataset = toolx_H5Dcreate(a_loc,a_name 450 if(dataset<0) { 451 ::H5Sclose(dataspace); 452 return false; 453 } 454 455 if(H5Dwrite(dataset,aWriteType,H5S_ALL,H5S_A 456 ::H5Dclose(dataset); 457 ::H5Sclose(dataspace); 458 return false; 459 } 460 461 ::H5Dclose(dataset); 462 ::H5Sclose(dataspace); 463 464 return true; 465 } 466 */ 467 468 template <class T> 469 inline bool read_scalar(hid_t a_loc,const std: 470 hid_t dataset = toolx_H5Dopen(a_loc,a_name.c 471 if(dataset<0) return false; 472 473 hid_t file_space = H5Dget_space(dataset); 474 if(file_space<0) { 475 ::H5Dclose(dataset); 476 return false; 477 } 478 479 hid_t mem_space = ::H5Screate(H5S_SCALAR); 480 if(mem_space<0) { 481 ::H5Sclose(file_space); 482 ::H5Dclose(dataset); 483 return false; 484 } 485 486 if(H5Dread(dataset,a_mem_type,mem_space,file 487 ::H5Sclose(mem_space); 488 ::H5Sclose(file_space); 489 ::H5Dclose(dataset); 490 return false; 491 } 492 493 ::H5Sclose(mem_space); 494 ::H5Sclose(file_space); 495 ::H5Dclose(dataset); 496 497 return true; 498 } 499 500 template <class T> 501 inline bool read_array(hid_t a_loc,const std:: 502 hid_t dataset = toolx_H5Dopen(a_loc,a_name.c 503 if(dataset<0) { 504 a_size = 0; 505 if(a_alloc) a_array = 0; 506 return false; // data set not found. 507 } 508 509 hid_t file_space = H5Dget_space(dataset); 510 if(file_space<0) { 511 ::H5Dclose(dataset); 512 a_size = 0; 513 if(a_alloc) a_array = 0; 514 return false; 515 } 516 517 {int dimn = H5Sget_simple_extent_ndims(file_s 518 if(dimn<0) { 519 ::H5Sclose(file_space); 520 ::H5Dclose(dataset); 521 a_size = 0; 522 if(a_alloc) a_array = 0; 523 return false; 524 } 525 if(dimn!=1) { 526 ::H5Sclose(file_space); 527 ::H5Dclose(dataset); 528 a_size = 0; 529 if(a_alloc) a_array = 0; 530 return false; 531 } 532 //printf("debug : read dimn %d\n",dimn); 533 } 534 535 hsize_t dims[1]; 536 {if(H5Sget_simple_extent_dims(file_space,dims 537 ::H5Sclose(file_space); 538 ::H5Dclose(dataset); 539 a_size = 0; 540 if(a_alloc) a_array = 0; 541 return false; 542 }} 543 544 a_size = (unsigned int)dims[0]; 545 if(!a_size) { 546 ::H5Sclose(file_space); 547 ::H5Dclose(dataset); 548 a_size = 0; 549 if(a_alloc) a_array = 0; 550 return true; //It is ok. 551 } 552 553 hid_t mem_space = ::H5Screate_simple(1,dims, 554 if(mem_space<0) { 555 ::H5Sclose(file_space); 556 ::H5Dclose(dataset); 557 a_size = 0; 558 if(a_alloc) a_array = 0; 559 return false; 560 } 561 562 if(a_alloc) a_array = new T[a_size]; 563 if(H5Dread(dataset,a_mem_type,mem_space,file 564 if(a_alloc) delete [] a_array; 565 ::H5Sclose(mem_space); 566 ::H5Sclose(file_space); 567 ::H5Dclose(dataset); 568 a_size = 0; 569 if(a_alloc) a_array = 0; 570 return false; 571 } 572 573 574 ::H5Sclose(mem_space); 575 ::H5Sclose(file_space); 576 ::H5Dclose(dataset); 577 578 return true; 579 } 580 581 /* 582 template <class T> 583 inline bool read_vlen(hid_t a_loc,const std::s 584 hid_t dataset = toolx_H5Dopen(a_loc,a_name.c 585 if(dataset<0) { 586 a_size = 0; 587 a_array = 0; 588 return false; // data set not found. 589 } 590 591 hid_t file_space = H5Dget_space(dataset); 592 if(file_space<0) { 593 ::H5Dclose(dataset); 594 a_size = 0; 595 a_array = 0; 596 return false; 597 } 598 599 {int dimn = H5Sget_simple_extent_ndims(file_s 600 if(dimn<0) { 601 ::H5Sclose(file_space); 602 ::H5Dclose(dataset); 603 a_size = 0; 604 a_array = 0; 605 return false; 606 } 607 if(dimn!=1) { 608 ::H5Sclose(file_space); 609 ::H5Dclose(dataset); 610 a_size = 0; 611 a_array = 0; 612 return false; 613 } 614 //printf("debug : read dimn %d\n",dimn); 615 } 616 617 hsize_t dims[1]; 618 {if(H5Sget_simple_extent_dims(file_space,dims 619 ::H5Sclose(file_space); 620 ::H5Dclose(dataset); 621 a_size = 0; 622 a_array = 0; 623 return false; 624 }} 625 626 unsigned int _size = (unsigned int)dims[0]; 627 if(_size!=1) { 628 ::H5Sclose(file_space); 629 ::H5Dclose(dataset); 630 a_size = 0; 631 a_array = 0; 632 return false; 633 } 634 635 hid_t mem_space = ::H5Screate_simple(1,dims, 636 if(mem_space<0) { 637 ::H5Sclose(file_space); 638 ::H5Dclose(dataset); 639 a_size = 0; 640 a_array = 0; 641 return false; 642 } 643 644 hid_t mem_type = ::H5Tvlen_create(a_mem_type 645 646 hvl_t rdata[1]; 647 if(H5Dread(dataset,mem_type,mem_space,file_s 648 //::H5Dvlen_reclaim(mem_type,mem_space,H5P 649 ::H5Tclose(mem_type); 650 ::H5Sclose(mem_space); 651 ::H5Sclose(file_space); 652 ::H5Dclose(dataset); 653 a_size = 0; 654 a_array = 0; 655 return false; 656 } 657 658 hsize_t len = rdata[0].len; 659 if(!len) { 660 //a_array = new T[1]; 661 a_array = 0; //it is ok. 662 } else { 663 a_array = new T[len]; 664 T* _data = (T*)rdata[0].p; 665 T* pos = a_array; 666 for(hsize_t index=0;index<len;index++,pos+ 667 } 668 a_size = len; 669 670 ::H5Dvlen_reclaim(mem_type,mem_space,H5P_DEF 671 672 ::H5Tclose(mem_type); 673 ::H5Sclose(mem_space); 674 ::H5Sclose(file_space); 675 ::H5Dclose(dataset); 676 677 return true; 678 } 679 */ 680 681 template <class T> 682 inline bool read_sub_array(hid_t a_loc,const s 683 unsigned int a_offs 684 unsigned int& a_siz 685 hid_t dataset = toolx_H5Dopen(a_loc,a_name.c 686 if(dataset<0) { 687 a_size = 0; 688 a_array = 0; 689 return false; // data set not found. 690 } 691 692 hid_t file_space = H5Dget_space(dataset); 693 if(file_space<0) { 694 ::H5Dclose(dataset); 695 a_size = 0; 696 a_array = 0; 697 return false; 698 } 699 700 {int dimn = H5Sget_simple_extent_ndims(file_s 701 if(dimn<0) { 702 ::H5Sclose(file_space); 703 ::H5Dclose(dataset); 704 a_size = 0; 705 a_array = 0; 706 return false; 707 } 708 if(dimn!=1) { 709 ::H5Sclose(file_space); 710 ::H5Dclose(dataset); 711 a_size = 0; 712 a_array = 0; 713 return false; 714 } 715 //printf("debug : read dimn %d\n",dimn); 716 } 717 718 hsize_t dims[1]; 719 {if(H5Sget_simple_extent_dims(file_space,dims 720 ::H5Sclose(file_space); 721 ::H5Dclose(dataset); 722 a_size = 0; 723 a_array = 0; 724 return false; 725 }} 726 727 unsigned int sz = (unsigned int)dims[0]; 728 if(!sz) { 729 ::H5Sclose(file_space); 730 ::H5Dclose(dataset); 731 a_size = 0; 732 a_array = 0; 733 return true; //It is ok. 734 } 735 736 // abcdef 737 // 012345 738 int remain = sz-a_offset; 739 if(remain<=0) { 740 ::H5Sclose(file_space); 741 ::H5Dclose(dataset); 742 a_size = 0; 743 a_array = 0; 744 return a_number?false:true; 745 } 746 747 int number = (int(a_number)<=remain) ? int(a 748 if(number<=0) { 749 ::H5Sclose(file_space); 750 ::H5Dclose(dataset); 751 a_size = 0; 752 a_array = 0; 753 return true; //It is ok. 754 } 755 756 {hsize_t offset[1]; 757 offset[0] = a_offset; 758 hsize_t count[1]; 759 count[0] = number; 760 if(H5Sselect_hyperslab(file_space,H5S_SELECT 761 ::H5Sclose(file_space); 762 ::H5Dclose(dataset); 763 a_size = 0; 764 a_array = 0; 765 return false; 766 }} 767 768 dims[0] = number; 769 hid_t mem_space = ::H5Screate_simple(1,dims, 770 if(mem_space<0) { 771 ::H5Sclose(file_space); 772 ::H5Dclose(dataset); 773 a_size = 0; 774 a_array = 0; 775 return false; 776 } 777 778 a_array = new T[number]; 779 if(H5Dread(dataset,a_mem_type,mem_space,file 780 delete [] a_array; 781 ::H5Sclose(mem_space); 782 ::H5Sclose(file_space); 783 ::H5Dclose(dataset); 784 a_array = 0; 785 return false; 786 } 787 788 ::H5Sclose(mem_space); 789 ::H5Sclose(file_space); 790 ::H5Dclose(dataset); 791 792 a_size = number; 793 794 return true; 795 } 796 797 template <class T> 798 inline bool read_sub_vlen(hid_t a_loc,const st 799 unsigned int a_offse 800 unsigned int& a_size 801 hid_t dataset = toolx_H5Dopen(a_loc,a_name.c 802 if(dataset<0) { 803 a_size = 0; 804 a_array = 0; 805 return false; // data set not found. 806 } 807 808 hid_t file_space = H5Dget_space(dataset); 809 if(file_space<0) { 810 ::H5Dclose(dataset); 811 a_size = 0; 812 a_array = 0; 813 return false; 814 } 815 816 {int dimn = H5Sget_simple_extent_ndims(file_s 817 if(dimn<0) { 818 ::H5Sclose(file_space); 819 ::H5Dclose(dataset); 820 a_size = 0; 821 a_array = 0; 822 return false; 823 } 824 if(dimn!=1) { 825 ::H5Sclose(file_space); 826 ::H5Dclose(dataset); 827 a_size = 0; 828 a_array = 0; 829 return false; 830 } 831 //printf("debug : read dimn %d\n",dimn); 832 } 833 834 hsize_t dims[1]; 835 {if(H5Sget_simple_extent_dims(file_space,dims 836 ::H5Sclose(file_space); 837 ::H5Dclose(dataset); 838 a_size = 0; 839 a_array = 0; 840 return false; 841 }} 842 843 unsigned int sz = (unsigned int)dims[0]; 844 if(!sz) { 845 ::H5Sclose(file_space); 846 ::H5Dclose(dataset); 847 a_size = 0; 848 a_array = 0; 849 return true; //It is ok. 850 } 851 852 // abcdef 853 // 012345 854 int remain = sz-a_offset; 855 if(remain<=0) { 856 ::H5Sclose(file_space); 857 ::H5Dclose(dataset); 858 a_size = 0; 859 a_array = 0; 860 return false; 861 } 862 863 {hsize_t offset[1]; 864 offset[0] = a_offset; 865 hsize_t count[1]; 866 count[0] = 1; 867 if(H5Sselect_hyperslab(file_space,H5S_SELECT 868 ::H5Sclose(file_space); 869 ::H5Dclose(dataset); 870 a_size = 0; 871 a_array = 0; 872 return false; 873 }} 874 875 dims[0] = 1; 876 hid_t mem_space = ::H5Screate_simple(1,dims, 877 if(mem_space<0) { 878 ::H5Sclose(file_space); 879 ::H5Dclose(dataset); 880 a_size = 0; 881 a_array = 0; 882 return false; 883 } 884 885 hid_t mem_type = ::H5Tvlen_create(a_mem_type 886 887 hvl_t rdata[1]; 888 if(H5Dread(dataset,mem_type,mem_space,file_s 889 //::H5Dvlen_reclaim(mem_type,mem_space,H5P 890 ::H5Tclose(mem_type); 891 ::H5Sclose(mem_space); 892 ::H5Sclose(file_space); 893 ::H5Dclose(dataset); 894 a_size = 0; 895 a_array = 0; 896 return false; 897 } 898 899 hsize_t len = rdata[0].len; 900 if(!len) { 901 //a_array = new T[1]; 902 a_array = 0; //it is ok. 903 } else { 904 a_array = new T[len]; 905 T* _data = (T*)rdata[0].p; 906 T* pos = a_array; 907 for(hsize_t index=0;index<len;index++,pos+ 908 } 909 a_size = len; 910 911 ::H5Dvlen_reclaim(mem_type,mem_space,H5P_DEF 912 913 ::H5Tclose(mem_type); 914 ::H5Sclose(mem_space); 915 ::H5Sclose(file_space); 916 ::H5Dclose(dataset); 917 918 return true; 919 } 920 921 template <class TYPE> 922 inline bool read_scalar(hid_t a_loc,const std: 923 return read_scalar<TYPE>(a_loc,a_name,to_T_m 924 } 925 926 template <class T> 927 inline bool read_std_vec(hid_t a_loc,const std 928 tools::uint64 sz; 929 if(!read_scalar<tools::uint64>(a_loc,a_name+ 930 if(!sz) {a_vec.clear();return true;} //it is 931 a_vec.resize((size_t)sz); 932 T* data = tools::vec_data(a_vec); 933 unsigned int _sz; 934 if(!read_array(a_loc,a_name,a_mem_type,_sz,d 935 if(tools::uint64(_sz)!=sz) {a_vec.clear();re 936 return true; 937 } 938 939 template <class TYPE> 940 inline bool read_std_vec_vec(hid_t a_loc,const 941 tools::uint64 sz; 942 if(!read_scalar<tools::uint64>(a_loc,a_name+ 943 a_vec_vec.resize((size_t)sz); 944 std::string scount; 945 for(size_t count=0;count<(size_t)sz;count++) 946 tools::num2s(tools::uint64(count),scount); 947 if(!read_std_vec<TYPE>(a_loc,a_name+"_elem 948 } 949 return true; 950 } 951 952 /* 953 template <class T> 954 inline bool read_array_struct( 955 hid_t a_loc 956 ,const std::string& a_name 957 ,hid_t aReadType 958 ,unsigned int& a_size 959 ,T*& a_array 960 ){ 961 hid_t dataset = toolx_H5Dopen(a_loc,a_name.c 962 if(dataset<0) { 963 a_size = 0; 964 a_array = 0; 965 return false; // data set not found. 966 } 967 968 hid_t dataspace = H5Dget_space(dataset); 969 if(dataspace<0) { 970 ::H5Dclose(dataset); 971 a_size = 0; 972 a_array = 0; 973 return false; 974 } 975 976 int dimn = H5Sget_simple_extent_ndims(datasp 977 if(dimn<0) { 978 ::H5Sclose(dataspace); 979 ::H5Dclose(dataset); 980 a_size = 0; 981 a_array = 0; 982 return false; 983 } 984 //printf("debug : read dimn %d\n",dimn); 985 986 hsize_t* dims = new hsize_t[dimn]; 987 {int rdimn = H5Sget_simple_extent_dims(datasp 988 if(rdimn<0) { 989 delete [] dims; 990 ::H5Sclose(dataspace); 991 ::H5Dclose(dataset); 992 a_size = 0; 993 a_array = 0; 994 return false; 995 }} 996 997 hid_t memspace = ::H5Screate_simple(dimn,dim 998 if(memspace<0) { 999 delete [] dims; 1000 ::H5Sclose(dataspace); 1001 ::H5Dclose(dataset); 1002 a_size = 0; 1003 a_array = 0; 1004 return false; 1005 } 1006 1007 a_size = (unsigned int)dims[0]; 1008 delete [] dims; 1009 if(!a_size) { 1010 ::H5Sclose(memspace); 1011 ::H5Sclose(dataspace); 1012 ::H5Dclose(dataset); 1013 a_size = 0; 1014 a_array = 0; 1015 return false; 1016 } 1017 1018 a_array = new T[a_size]; 1019 if(H5Dread(dataset,aReadType,memspace,datas 1020 delete [] a_array; 1021 ::H5Sclose(memspace); 1022 ::H5Sclose(dataspace); 1023 ::H5Dclose(dataset); 1024 a_size = 0; 1025 a_array = 0; 1026 return false; 1027 } 1028 1029 ::H5Sclose(memspace); 1030 ::H5Sclose(dataspace); 1031 ::H5Dclose(dataset); 1032 1033 return true; 1034 } 1035 */ 1036 1037 template <class T> 1038 inline bool read_struct(hid_t a_loc,const std 1039 hid_t dataset = toolx_H5Dopen(a_loc,a_name. 1040 if(dataset<0) return false; 1041 1042 hid_t file_space = H5Dget_space(dataset); 1043 if(file_space<0) { 1044 ::H5Dclose(dataset); 1045 return false; 1046 } 1047 1048 hid_t mem_space = ::H5Screate(H5S_SCALAR); 1049 if(mem_space<0) { 1050 ::H5Sclose(file_space); 1051 ::H5Dclose(dataset); 1052 return false; 1053 } 1054 1055 if(H5Dread(dataset,aReadType,mem_space,file 1056 ::H5Sclose(mem_space); 1057 ::H5Sclose(file_space); 1058 ::H5Dclose(dataset); 1059 return false; 1060 } 1061 1062 ::H5Sclose(mem_space); 1063 ::H5Sclose(file_space); 1064 ::H5Dclose(dataset); 1065 return true; 1066 } 1067 1068 template <class TYPE> 1069 inline bool write_scalar(hid_t a_loc,const st 1070 hid_t scalar = ::H5Screate(H5S_SCALAR); 1071 if(scalar<0) return false; 1072 1073 hid_t compact = ::H5Pcreate(H5P_DATASET_CRE 1074 if(compact<0) { 1075 ::H5Sclose(scalar); 1076 return false; 1077 } 1078 1079 if(H5Pset_layout(compact,H5D_COMPACT)<0) { 1080 ::H5Pclose(compact); 1081 ::H5Sclose(scalar); 1082 return false; 1083 } 1084 1085 hid_t dataset = toolx_H5Dcreate(a_loc,a_nam 1086 if(dataset<0) { 1087 ::H5Pclose(compact); 1088 ::H5Sclose(scalar); 1089 return false; 1090 } 1091 1092 if(::H5Dwrite(dataset,to_T_mem_type(TYPE()) 1093 ::H5Pclose(compact); 1094 ::H5Sclose(scalar); 1095 ::H5Dclose(dataset); 1096 return false; 1097 } 1098 1099 ::H5Pclose(compact); 1100 ::H5Sclose(scalar); 1101 ::H5Dclose(dataset); 1102 return true; 1103 } 1104 1105 template <class T> 1106 inline bool write_struct(hid_t a_loc,const st 1107 hid_t a_create_type, 1108 hid_t scalar = ::H5Screate(H5S_SCALAR); 1109 if(scalar<0) return false; 1110 1111 hid_t compact = ::H5Pcreate(H5P_DATASET_CRE 1112 if(compact<0) { 1113 ::H5Sclose(scalar); 1114 return false; 1115 } 1116 1117 if(H5Pset_layout(compact,H5D_COMPACT)<0) { 1118 ::H5Pclose(compact); 1119 ::H5Sclose(scalar); 1120 return false; 1121 } 1122 1123 hid_t dataset = toolx_H5Dcreate(a_loc,a_nam 1124 if(dataset<0) { 1125 ::H5Pclose(compact); 1126 ::H5Sclose(scalar); 1127 return false; 1128 } 1129 1130 if(H5Dwrite(dataset,aWriteType,H5S_ALL,H5S_ 1131 ::H5Pclose(compact); 1132 ::H5Sclose(scalar); 1133 ::H5Dclose(dataset); 1134 return false; 1135 } 1136 1137 ::H5Pclose(compact); 1138 ::H5Sclose(scalar); 1139 ::H5Dclose(dataset); 1140 return true; 1141 } 1142 1143 ///////////////////////////////////////////// 1144 ///////////////////////////////////////////// 1145 ///////////////////////////////////////////// 1146 template <class TYPE> 1147 inline bool write_array(hid_t a_loc,const std 1148 unsigned int a_size,c 1149 unsigned int a_chunke 1150 return hdf5::write_array<TYPE>(a_loc,a_name 1151 a_chunked,a_ 1152 } 1153 1154 template <class TYPE> 1155 inline bool write_vlen(hid_t a_loc,const std: 1156 unsigned int a_size,co 1157 unsigned int a_chunked 1158 return hdf5::write_vlen<TYPE>(a_loc,a_name, 1159 a_chunked,a_c 1160 } 1161 1162 template <class T> 1163 inline bool write_std_vec(hid_t a_loc,const s 1164 hid_t a_file_type,h 1165 unsigned int a_chun 1166 const std::vector<T 1167 if(!write_scalar<tools::uint64>(a_loc,a_nam 1168 if(a_vec.empty()) return true; //it is ok. 1169 const T* data = tools::vec_data(a_vec); 1170 return write_array(a_loc,a_name,a_file_type 1171 } 1172 1173 template <class TYPE> 1174 inline bool write_std_vec(hid_t a_loc,const s 1175 unsigned int a_chun 1176 return hdf5::write_std_vec<TYPE>(a_loc,a_na 1177 } 1178 1179 template <class TYPE> 1180 inline bool write_std_vec_vec(hid_t a_loc,con 1181 unsigned int /* 1182 if(!write_scalar<tools::uint64>(a_loc,a_nam 1183 unsigned int count = 0; //uint for num2s. 1184 std::string scount; 1185 tools_typename_vforcit(std::vector<TYPE>,a_ 1186 tools::num2s(count,scount); 1187 if(!write_std_vec<TYPE>(a_loc,a_name+"_el 1188 count++; 1189 } 1190 return true; 1191 } 1192 1193 //template <class TKEY,class TVALUE> 1194 //inline bool write_std_map(hid_t a_loc,const 1195 // unsigned int a_ch 1196 // if(!write_scalar<tools::uint64>(a_loc,a_n 1197 // unsigned int count = 0; //uint for num2s. 1198 // std::string scount; 1199 // tools_typename_mforcit(TKEY,TVALUE,a_map, 1200 // tools::num2s(count,scount); 1201 // if(!write_scalar<TKEY>(a_loc,a_name+"_e 1202 // if(!write_scalar<TVALUE>(a_loc,a_name+" 1203 // count++; 1204 // } 1205 // return true; 1206 //} 1207 1208 template <class TYPE> 1209 inline bool write_sub_array(hid_t a_loc,const 1210 unsigned int a_size,unsi 1211 bool a_create = true,uns 1212 return hdf5::write_sub_array<TYPE>(a_loc,a_ 1213 a_create 1214 a_size,a 1215 } 1216 1217 template <class TYPE> 1218 inline bool write_append_array_dataset(hid_t 1219 return hdf5::write_append_array_dataset<TYP 1220 } 1221 1222 template <class TYPE> 1223 inline bool write_append_vlen_dataset(hid_t a 1224 return hdf5::write_append_vlen_dataset<TYPE 1225 } 1226 1227 template <class TYPE> 1228 inline bool write_append_array(hid_t a_loc,co 1229 return hdf5::write_append_array<TYPE>(a_loc 1230 } 1231 1232 template <class TYPE> 1233 inline bool read_array(hid_t a_loc,const std: 1234 return read_array<TYPE>(a_loc,a_name,to_T_m 1235 } 1236 1237 //template <class TYPE> 1238 //inline bool read_vlen(hid_t a_loc,const std 1239 // return read_vlen<TYPE>(a_loc,a_name,to_T_ 1240 //} 1241 1242 template <class TYPE> 1243 inline bool read_std_vec(hid_t a_loc,const st 1244 return read_std_vec<TYPE>(a_loc,a_name,to_T 1245 } 1246 1247 template <class TYPE> 1248 inline bool read_std_vec_vec(hid_t a_loc,cons 1249 return read_std_vec_vec<TYPE>(a_loc,a_name, 1250 } 1251 1252 template <class TYPE> 1253 inline bool read_sub_array(hid_t a_loc,const 1254 unsigned int& a_si 1255 return read_sub_array<TYPE>(a_loc,a_name,to 1256 } 1257 1258 template <class TYPE> 1259 inline bool read_sub_vlen(hid_t a_loc,const s 1260 unsigned int& a_siz 1261 return read_sub_vlen<TYPE>(a_loc,a_name,to_ 1262 } 1263 1264 inline bool read_bool(hid_t a_loc,const std:: 1265 unsigned char value = 0; 1266 if(!read_scalar<unsigned char>(a_loc,a_name 1267 aValue = false; 1268 return false; 1269 } 1270 if((value!=0) && (value!=1)) { 1271 aValue = false; 1272 return false; 1273 } 1274 aValue = (value==1?true:false); 1275 return true; 1276 } 1277 1278 }} 1279 1280 #include "atb" 1281 1282 namespace toolx { 1283 namespace hdf5 { 1284 1285 template <class TYPE> 1286 inline bool write_scalar_atb(hid_t aDS,const 1287 int has_attr = H5LT_find_attribute(aDS,a_na 1288 if(has_attr==1) { 1289 if(H5Adelete(aDS,a_name.c_str())<0) retur 1290 } 1291 1292 hid_t scalar = ::H5Screate(H5S_SCALAR); 1293 if(scalar<0) return false; 1294 1295 hid_t aid = toolx_H5Acreate(aDS,a_name.c_st 1296 if(aid<0) { 1297 ::H5Sclose(scalar); 1298 return false; 1299 } 1300 1301 if(H5Awrite(aid,to_T_mem_type(TYPE()),&aDat 1302 ::H5Sclose(scalar); 1303 ::H5Aclose(aid); 1304 return false; 1305 } 1306 1307 ::H5Sclose(scalar); 1308 ::H5Aclose(aid); 1309 1310 return true; 1311 } 1312 1313 }} 1314 1315 /* 1316 #include <tools/buf2lines> 1317 1318 namespace toolx { 1319 namespace hdf5 { 1320 1321 inline bool write_strings(hid_t a_loc,const s 1322 size_t a_number,con 1323 unsigned int a_chun 1324 size_t sz;char* buffer; 1325 if(!tools::strings2buf(a_number,a_strings,s 1326 sz--; 1327 bool status = hdf5::write_array<char>(a_loc 1328 a_chu 1329 delete [] buffer; 1330 return status; 1331 } 1332 1333 inline bool write_strings(hid_t a_loc,const s 1334 const std::vector<s 1335 unsigned int a_chun 1336 size_t sz;char* buffer; 1337 if(!tools::strings2buf(a_strings,sz,buffer) 1338 sz--; 1339 bool status = hdf5::write_array<char>(a_loc 1340 a_chu 1341 delete [] buffer; 1342 return status; 1343 } 1344 1345 inline bool write_append_strings(hid_t a_loc, 1346 size_t sz;char* buffer; 1347 if(!tools::strings2buf(a_number,a_strings,s 1348 sz--; 1349 bool status = hdf5::write_append_array<char 1350 delete [] buffer; 1351 return status; 1352 } 1353 1354 inline bool write_append_strings(hid_t a_loc, 1355 size_t sz;char* buffer; 1356 if(!tools::strings2buf(a_strings,sz,buffer) 1357 sz--; 1358 bool status = hdf5::write_append_array<char 1359 delete [] buffer; 1360 return status; 1361 } 1362 1363 }} 1364 */ 1365 1366 #endif