[84c3c0]: HDF5 / Dataset.pm Maximize Restore History

Download this file

Dataset.pm    1032 lines (713 with data), 29.0 kB

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
package PDL::IO::HDF5::Dataset;
use Carp;
=head1 NAME
PDL::IO::HDF5::Dataset - PDL::IO::HDF5 Helper Object representing HDF5 datasets.
=head1 DESCRIPTION
This is a helper-object used by PDL::IO::HDF5 to interface with HDF5 format's dataset objects.
Information on the HDF5 Format can be found
at the NCSA's web site at http://hdf.ncsa.uiuc.edu/ .
=head1 SYNOPSIS
See L<PDL::IO::HDF5>
=head1 MEMBER DATA
=over 1
=item ID
ID number given to the dataset by the HDF5 library
=item name
Name of the dataset.
=item parent
Ref to parent object (group) that owns this dateset.
=item fileObj
Ref to the L<PDL::IO::HDF5> object that owns this object.
=back
=head1 METHODS
####---------------------------------------------------------
=head2 new
=for ref
PDL::IO::HDF5::Dataset Constructor - creates new object
B<Usage:>
=for usage
This object will usually be created using the calling format detailed in the L<SYNOPSIS>. The
following syntax is used by the L<PDL::IO::HDF5> object to build the object.
$a = new PDL::IO::HDF5:Dataset( name => $name, parent => $parent,
fileObj => $fileObj);
Args:
$name Name of the dataset
$parent Parent Object that owns this dataset
$fileObj PDL::HDF object that owns this dateset.
=cut
sub new{
my $type = shift;
my %parms = @_;
my $self = {};
my @DataMembers = qw( name parent fileObj);
my %DataMembers;
@DataMembers{ @DataMembers } = @DataMembers; # hash for quick lookup
# check for proper supplied names:
my $varName;
foreach $varName(keys %parms){
unless( defined($DataMembers{$varName})){
carp("Error Calling ".__PACKAGE__." Constuctor\n \'$varName\' not a valid data member\n");
return undef;
}
unless( defined($parms{$varName})){
carp("Error Calling ".__PACKAGE__." Constuctor\n \'$varName\' not supplied\n");
return undef;
}
$self->{$varName} = $parms{$varName};
}
my $parent = $self->{parent};
my $groupID = $parent->IDget;
my $groupName = $parent->nameGet;
my $name = $self->{name};
my $datasetID;
#####
# Turn Error Reporting off for the following, so H5 lib doesn't complain
# if the group isn't found.
PDL::IO::HDF5::H5errorOff();
my $rc = PDL::IO::HDF5::H5Gget_objinfo($groupID, $name,1,0);
PDL::IO::HDF5::H5errorOn();
# See if the dataset exists:
if( $rc >= 0){
#DataSet Exists open it:
$datasetID = PDL::IO::HDF5::H5Dopen($groupID, $name);
if($datasetID < 0 ){
carp "Error Calling ".__PACKAGE__." Constuctor: Can't open existing dataset '$name'\n";
return undef;
}
}
else{ # dataset didn't exist, set datasetID = 0
## (Have to put off opening the dataset
### until it is written to (Must know dims, etc to create)
$datasetID = 0;
}
$self->{ID} = $datasetID;
bless $self, $type;
return $self;
}
=head2 DESTROY
=for ref
PDL::IO::HDF5::Dataset Destructor - Closes the dataset object
B<Usage:>
=for usage
No Usage. Automatically called
=cut
sub DESTROY {
my $self = shift;
my $datasetID = $self->{ID};
# print "In DataSet DEstroy\n";
if( $datasetID && (PDL::IO::HDF5::H5Dclose($self->{ID}) < 0 )){
warn("Error closing HDF5 Dataset '".$self->{name}."' in file:group: '".$self->{filename}.":".$self->{group}."'\n");
}
}
=head2 set
=for ref
Write data to the HDF5 dataset
B<Usage:>
=for usage
$dataset->set($pdl); # Write the array data in the dataset
=cut
#############################################################################
# Mapping of PDL types to HDF5 types for writing to a dataset
#
# Mapping of PDL types to what HDF5 calls them while we are dealing with them
# outside of the HDF5 file.
%PDLtoHDF5internalTypeMapping = (
$PDL::Types::PDL_B => PDL::IO::HDF5::H5T_NATIVE_CHAR(),
$PDL::Types::PDL_S => PDL::IO::HDF5::H5T_NATIVE_SHORT(),
$PDL::Types::PDL_L => PDL::IO::HDF5::H5T_NATIVE_LONG(),
$PDL::Types::PDL_F => PDL::IO::HDF5::H5T_NATIVE_FLOAT(),
$PDL::Types::PDL_D => PDL::IO::HDF5::H5T_NATIVE_DOUBLE(),
);
# Mapping of PDL types to what types they are written to in the HDF5 file.
# For 64 Bit machines, we might need to modify this with some smarts to determine
# what is appropriate
%PDLtoHDF5fileMapping = (
$PDL::Types::PDL_B => PDL::IO::HDF5::H5T_STD_I8BE(),
$PDL::Types::PDL_S => PDL::IO::HDF5::H5T_STD_I16BE(),
$PDL::Types::PDL_L => PDL::IO::HDF5::H5T_STD_I32BE(),
$PDL::Types::PDL_F => PDL::IO::HDF5::H5T_IEEE_F32BE(),
$PDL::Types::PDL_D => PDL::IO::HDF5::H5T_IEEE_F64BE(),
);
sub set{
my $self = shift;
my ($pdl) = @_;
my $parent = $self->{parent};
my $groupID = $parent->IDget;
my $datasetID = $self->{ID};
my $name = $self->{name};
my $internalhdf5_type; # hdf5 type that describes the way data is stored in memory
my $hdf5Filetype; # hdf5 type that describes the way data will be stored in the file.
my @dims; # hdf5 equivalent dims for the supplied PDL
my $type = $pdl->get_datatype; # get PDL datatype
if( $pdl->isa('PDL::Char') ){ # Special Case for PDL::Char Objects (fixed length strings)
@dims = $pdl->dims;
my $length = shift @dims; # String length is the first dim of the PDL for PDL::Char
# Create Null-Terminated String Type
$internalhdf5_type = PDL::IO::HDF5::H5Tcopy(PDL::IO::HDF5::H5T_C_S1());
PDL::IO::HDF5::H5Tset_size($internalhdf5_type, $length ); # make legth of type eaual to strings
$hdf5Filetype = $internalhdf5_type; # memory and file storage will be the same type
@dims = reverse(@dims); # HDF5 stores columns/rows in reverse order than pdl
}
else{ # Other PDL Types
unless( defined($PDLtoHDF5internalTypeMapping{$type}) ){
carp "Error Calling ".__PACKAGE__."::set: Can't map PDL type to HDF5 datatype\n";
return undef;
}
$internalhdf5_type = $PDLtoHDF5internalTypeMapping{$type};
unless( defined($PDLtoHDF5fileMapping{$type}) ){
carp "Error Calling ".__PACKAGE__."::set: Can't map PDL type to HDF5 datatype\n";
return undef;
}
$hdf5Filetype = $PDLtoHDF5fileMapping{$type};
@dims = reverse($pdl->dims); # HDF5 stores columns/rows in reverse order than pdl
}
my $dims = PDL::IO::HDF5::packList(@dims);
my $dataspaceID = PDL::IO::HDF5::H5Screate_simple(scalar(@dims), $dims , $dims);
if( $dataspaceID < 0 ){
carp("Can't Open Dataspace in ".__PACKAGE__.":set\n");
return undef;
}
if( $datasetID == 0){ # Dataset not created yet
# /* Create the dataset. */
$datasetID = PDL::IO::HDF5::H5Dcreate($groupID, $name, $hdf5Filetype, $dataspaceID,
PDL::IO::HDF5::H5P_DEFAULT());
if( $datasetID < 0){
carp("Can't Create Dataspace in ".__PACKAGE__.":set\n");
return undef;
}
$self->{ID} = $datasetID;
}
# Write the actual data:
$data = ${$pdl->get_dataref};
if( PDL::IO::HDF5::H5Dwrite($datasetID, $internalhdf5_type, PDL::IO::HDF5::H5S_ALL(), PDL::IO::HDF5::H5S_ALL(), PDL::IO::HDF5::H5P_DEFAULT(),
$data) < 0 ){
carp("Error Writing to dataset in ".__PACKAGE__.":set\n");
return undef;
}
# /* Terminate access to the data space. */
carp("Can't close Dataspace in ".__PACKAGE__.":set\n") if( PDL::IO::HDF5::H5Sclose($dataspaceID) < 0);
return 1;
}
=head2 get
=for ref
Get data from a HDF5 dataset to a PDL
B<Usage:>
=for usage
$pdl = $dataset->get; # Read the Array from the HDF5 dataset, create a PDL from it
# and put in $pdl
# Assuming $dataset is three dimensional
# with dimensions (20,100,90)
The I<get> method can also be used to obtain particular slices or hyperslabs
of the dataset array. For example, if $dataset is three dimensional with dimensions
(20,100,90) then we could do:
$start=pdl([0,0,0]); # We begin the slice at the very beggining
$end=pdl([19,0,0]); # We take the first vector of the array,
$stride=pdl([2,1,1]); # taking only every two values of the vector
$pdl = $dataset->get($start,$end,[$stride]); # Read a slice or
# hyperslab from the HDF5 dataset.
# $start, $end and optionally $stride
# should be PDL vectors with length the
# number of dimensions of the dataset.
# $start gives the starting coordinates
# in the array.
# $end gives the ending coordinate
# in the array
# $stride gives the steps taken from one
# coordinate to the next of the slice
The mapping of HDF5 datatypes in the file to PDL datatypes in memory will be according
to the following table.
HDF5 File Type PDL Type
------------------------ -----------------
PDL::IO::HDF5::H5T_C_S1() => PDL::Char Object (Special Case for Char Strings)
PDL::IO::HDF5::H5T_STD_I8BE() => $PDL::Types::PDL_B
PDL::IO::HDF5::H5T_STD_I8LE() => $PDL::Types::PDL_B,
PDL::IO::HDF5::H5T_STD_I16BE() => $PDL::Types::PDL_S,
PDL::IO::HDF5::H5T_STD_I16LE() => $PDL::Types::PDL_S,
PDL::IO::HDF5::H5T_STD_I32BE() => $PDL::Types::PDL_L,
PDL::IO::HDF5::H5T_STD_I32LE() => $PDL::Types::PDL_L,
PDL::IO::HDF5::H5T_IEEE_F32BE()=> $PDL::Types::PDL_F,
PDL::IO::HDF5::H5T_IEEE_F32LE()=> $PDL::Types::PDL_F,
PDL::IO::HDF5::H5T_IEEE_F64BE()=> $PDL::Types::PDL_D,
PDL::IO::HDF5::H5T_IEEE_F64LE()=> $PDL::Types::PDL_D
For HDF5 File types not in this table, this method will attempt to
map it to the default PDL type PDL_D.
=cut
#############################################################################
# Mapping of HDF5 file types to PDL types
# For 64 Bit machines, we might need to modify this with some smarts to determine
# what is appropriate
%HDF5toPDLfileMapping = (
PDL::IO::HDF5::H5T_STD_I8BE() => $PDL::Types::PDL_B,
PDL::IO::HDF5::H5T_STD_I8LE() => $PDL::Types::PDL_B,
PDL::IO::HDF5::H5T_STD_I16BE() => $PDL::Types::PDL_S,
PDL::IO::HDF5::H5T_STD_I16LE() => $PDL::Types::PDL_S,
PDL::IO::HDF5::H5T_STD_I32BE() => $PDL::Types::PDL_L,
PDL::IO::HDF5::H5T_STD_I32LE() => $PDL::Types::PDL_L,
PDL::IO::HDF5::H5T_IEEE_F32BE() => $PDL::Types::PDL_F,
PDL::IO::HDF5::H5T_IEEE_F32LE() => $PDL::Types::PDL_F,
PDL::IO::HDF5::H5T_IEEE_F64BE() => $PDL::Types::PDL_D,
PDL::IO::HDF5::H5T_IEEE_F64LE() => $PDL::Types::PDL_D
);
$H5T_STRING = PDL::IO::HDF5::H5T_STRING(); #HDF5 string type
sub get{
my $self = shift;
my $start = shift;
my $end = shift;
my $stride = shift;
my $pdl;
my $parent = $self->{parent};
my $groupID = $parent->IDget;
my $datasetID = $self->{ID};
my $name = $self->{name};
my $stringSize; # String size, if we are retrieving a string type
my $PDLtype; # PDL type that the data will be mapped to
my $internalhdf5_type; # Type that represents how HDF5 will store the data in memory (after retreiving from
# the file)
my $ReturnType = 'PDL'; # Default object returned is PDL. If strings are store, then this will
# return PDL::Char
# Get the HDF5 file datatype;
my $HDF5type = PDL::IO::HDF5::H5Dget_type($datasetID );
unless( $HDF5type >= 0 ){
carp "Error Calling ".__PACKAGE__."::get: Can't get HDF5 Dataset type.\n";
return undef;
}
# Check for string type:
if( PDL::IO::HDF5::H5Tget_class($HDF5type ) == $H5T_STRING ){ # String type
$stringSize = PDL::IO::HDF5::H5Tget_size($HDF5type);
unless( $stringSize >= 0 ){
carp "Error Calling ".__PACKAGE__."::get: Can't get HDF5 String Datatype Size.\n";
carp("Can't close Datatype in ".__PACKAGE__.":get\n") if( PDL::IO::HDF5::H5Tclose($HDF5type) < 0);
return undef;
}
$PDLtype = $PDL::Types::PDL_B;
$internalhdf5_type = $HDF5type; # internal storage the same as the file storage.
$ReturnType = 'PDL::Char'; # For strings, we return a PDL::Char
}
else{ # Normal Numeric Type
# Map the HDF5 file datatype to a PDL datatype
$PDLtype = $PDL::Types::PDL_D; # Default type is double
my $defaultType;
foreach $defaultType( keys %HDF5toPDLfileMapping){
if( PDL::IO::HDF5::H5Tequal($defaultType,$HDF5type) > 0){
$PDLtype = $HDF5toPDLfileMapping{$defaultType};
last;
}
}
# Get the HDF5 internal datatype that corresponds to the PDL type
unless( defined($PDLtoHDF5internalTypeMapping{$PDLtype}) ){
carp "Error Calling ".__PACKAGE__."::set: Can't map PDL type to HDF5 datatype\n";
return undef;
}
$internalhdf5_type = $PDLtoHDF5internalTypeMapping{$PDLtype};
}
my $dataspaceID = PDL::IO::HDF5::H5Dget_space($datasetID);
if( $dataspaceID < 0 ){
carp("Can't Open Dataspace in ".__PACKAGE__.":get\n");
carp("Can't close Datatype in ".__PACKAGE__.":get\n") if( PDL::IO::HDF5::H5Tclose($HDF5type) < 0);
return undef;
}
# Get the number of dims:
my $Ndims = PDL::IO::HDF5::H5Sget_simple_extent_ndims($dataspaceID);
if( $Ndims < 0 ){
carp("Can't Get Number of Dims in Dataspace in ".__PACKAGE__.":get\n");
carp("Can't close Datatype in ".__PACKAGE__.":get\n") if( PDL::IO::HDF5::H5Tclose($HDF5type) < 0);
carp("Can't close DataSpace in ".__PACKAGE__.":get\n") if( PDL::IO::HDF5::H5Sclose($dataspaceID) < 0);
return undef;
}
my @dims = ( 0..($Ndims-1));
my ($mem_space,$file_space);
if (not defined $start) {
# Initialize Dims structure:
my $dims = PDL::IO::HDF5::packList(@dims);
my $dims2 = PDL::IO::HDF5::packList(@dims);
my $rc = PDL::IO::HDF5::H5Sget_simple_extent_dims($dataspaceID, $dims, $dims2 );
if( $rc != $Ndims){
carp("Error getting number of dims in dataspace in ".__PACKAGE__.":get\n");
carp("Can't close Datatype in ".__PACKAGE__.":get\n") if( PDL::IO::HDF5::H5Tclose($HDF5type) < 0);
carp("Can't close DataSpace in ".__PACKAGE__.":get\n") if( PDL::IO::HDF5::H5Sclose($dataspaceID) < 0);
return undef;
}
@dims = PDL::IO::HDF5::unpackList($dims); # get the dim sizes from the binary structure
} else {
if ( ($start->getndims != 1) || ($start->getdim(0) != $Ndims) ){
carp("Wrong dimensions in start PDL in ".__PACKAGE__."\n");
carp("Can't close Datatype in ".__PACKAGE__.":get\n") if( PDL::IO::HDF5::H5Tclose($HDF5type) < 0);
carp("Can't close DataSpace in ".__PACKAGE__.":get\n") if( PDL::IO::HDF5::H5Sclose($dataspaceID) < 0);
return undef;
}
my $start2 = PDL::IO::HDF5::packList(reverse($start->list));
if (not defined $end) {
carp("No end supplied in ".__PACKAGE__."\n");
carp("Can't close Datatype in ".__PACKAGE__.":get\n") if( PDL::IO::HDF5::H5Tclose($HDF5type) < 0);
carp("Can't close DataSpace in ".__PACKAGE__.":get\n") if( PDL::IO::HDF5::H5Sclose($dataspaceID) < 0);
return undef;
}
if ( ($end->getndims != 1) || ($end->getdim(0) != $Ndims) ) {
carp("Wrong dimensions in end PDL in ".__PACKAGE__."\n") ;
carp("Can't close Datatype in ".__PACKAGE__.":get\n") if( PDL::IO::HDF5::H5Tclose($HDF5type) < 0);
carp("Can't close DataSpace in ".__PACKAGE__.":get\n") if( PDL::IO::HDF5::H5Sclose($dataspaceID) < 0);
return undef;
}
my $length2;
if (defined $stride) {
if ( ($stride->getndims != 1) ||
($stride->getdim(0) != $Ndims) ) {
carp("Wrong dimensions in stride in ".__PACKAGE__."\n");
carp("Can't close Datatype in ".__PACKAGE__.":get\n") if( PDL::IO::HDF5::H5Tclose($HDF5type) < 0);
carp("Can't close DataSpace in ".__PACKAGE__.":get\n") if( PDL::IO::HDF5::H5Sclose($dataspaceID) < 0);
return undef;
}
@dims=reverse((($end-$start+1)/$stride)->list);
$length2 = PDL::IO::HDF5::packList(@dims);
} else {
@dims=reverse(($end-$start+1)->list);
$length2 = PDL::IO::HDF5::packList(@dims);
$stride=PDL::Core::ones($Ndims);
}
my $mem_dims = PDL::IO::HDF5::packList(@dims);
my $stride2 = PDL::IO::HDF5::packList(reverse($stride->list));
my $block=PDL::Core::ones($Ndims);
my $block2 = PDL::IO::HDF5::packList(reverse($block->list));
# Slice the data
$file_space = PDL::IO::HDF5::H5Dget_space($datasetID);
$rc=PDL::IO::HDF5::H5Sselect_hyperslab($file_space, 0,
$start2, $stride2, $length2, $block2);
if( $rc < 0 ){
carp("Error slicing data from file in ".__PACKAGE__.":get\n");
carp("Can't close Datatype in ".__PACKAGE__.":get\n") if( PDL::IO::HDF5::H5Tclose($HDF5type) < 0);
carp("Can't close DataSpace in ".__PACKAGE__.":get\n") if( PDL::IO::HDF5::H5Sclose($dataspaceID) < 0);
return undef;
}
$mem_space = PDL::IO::HDF5::H5Screate_simple($Ndims, $mem_dims,
$mem_dims);
}
$pdl = $ReturnType->null;
$pdl->set_datatype($PDLtype);
my @pdldims; # dims of the PDL
if( defined( $stringSize )){ # String types
@pdldims = ($stringSize,reverse(@dims)); # HDF5 stores columns/rows in reverse order than pdl,
# 1st PDL dim is the string length (for PDL::Char)
}
else{ # Normal Numeric types
@pdldims = (reverse(@dims)); # HDF5 stores columns/rows in reverse order than pdl,
}
$pdl->setdims(\@pdldims);
my $nelems = 1;
foreach (@pdldims){ $nelems *= $_; }; # calculate the number of elements
my $datasize = $nelems * PDL::howbig($pdl->get_datatype);
# Create empty space for the data
# Incrementally, to get around problem on win32
my $howBig = PDL::howbig($pdl->get_datatype);
my $data = ' ' x $howBig;
foreach my $dim(@pdldims){
$data = $data x $dim;
}
# Read the data:
if (not defined $start) {
$rc = PDL::IO::HDF5::H5Dread($datasetID, $internalhdf5_type, PDL::IO::HDF5::H5S_ALL(), PDL::IO::HDF5::H5S_ALL(),
PDL::IO::HDF5::H5P_DEFAULT(),
$data);
} else {
$rc = PDL::IO::HDF5::H5Dread($datasetID, $internalhdf5_type,
$mem_space, $file_space,
PDL::IO::HDF5::H5P_DEFAULT(),
$data);
}
if( $rc < 0 ){
carp("Error reading data from file in ".__PACKAGE__.":get\n");
carp("Can't close Datatype in ".__PACKAGE__.":get\n") if( PDL::IO::HDF5::H5Tclose($HDF5type) < 0);
carp("Can't close DataSpace in ".__PACKAGE__.":get\n") if( PDL::IO::HDF5::H5Sclose($dataspaceID) < 0);
return undef;
}
# Update the PDL data with the data read from the file
${$pdl->get_dataref()} = $data;
$pdl->upd_data();
# /* Terminate access to the data space. */
carp("Can't close Dataspace in ".__PACKAGE__.":get\n") if( PDL::IO::HDF5::H5Sclose($dataspaceID) < 0);
# /* Terminate access to the data type. */
carp("Can't close Datatype in ".__PACKAGE__.":get\n") if( PDL::IO::HDF5::H5Tclose($HDF5type) < 0);
return $pdl;
}
=head2 dims
=for ref
Get the dims for a HDF5 dataset. For example, a 3 x 4 array would return a perl array
(3,4);
B<Usage:>
=for usage
@pdl = $dataset->dims; # Get an array of dims.
=cut
sub dims{
my $self = shift;
my $parent = $self->{parent};
my $groupID = $parent->IDget;
my $datasetID = $self->{ID};
my $name = $self->{name};
my $dataspaceID = PDL::IO::HDF5::H5Dget_space($datasetID);
if( $dataspaceID < 0 ){
carp("Can't Open Dataspace in ".__PACKAGE__.":get\n");
return undef;
}
# Get the number of dims:
my $Ndims = PDL::IO::HDF5::H5Sget_simple_extent_ndims($dataspaceID);
if( $Ndims < 0 ){
carp("Can't Get Number of Dims in Dataspace in ".__PACKAGE__.":get\n");
return undef;
}
# Initialize Dims structure:
my @dims = ( 0..($Ndims-1));
my $dims = PDL::IO::HDF5::packList(@dims);
my $dims2 = PDL::IO::HDF5::packList(@dims);
my $rc = PDL::IO::HDF5::H5Sget_simple_extent_dims($dataspaceID, $dims, $dims2 );
if( $rc != $Ndims){
carp("Error getting number of dims in dataspace in ".__PACKAGE__.":get\n");
carp("Can't close DataSpace in ".__PACKAGE__.":get\n") if( PDL::IO::HDF5::H5Sclose($dataspaceID) < 0);
return undef;
}
@dims = PDL::IO::HDF5::unpackList($dims); # get the dim sizes from the binary structure
return reverse @dims; # return dims in the order that PDL will store them
}
=head2 attrSet
=for ref
Set the value of an attribute(s)
Currently the only attribute types supported are null-terminated strings.
B<Usage:>
=for usage
$dataset->attrSet( 'attr1' => 'attr1Value',
'attr2' => 'attr2 value',
.
.
.
);
Returns undef on failure, 1 on success.
=cut
sub attrSet {
my $self = shift;
my %attrs = @_; # get atribute hash
my $datasetID = $self->{ID};
unless( $datasetID){ # Error checking
carp("Can't Set Attribute for empty dataset. Try writing some data to it first:\n");
carp(" in file:group: '".$self->{filename}.":".$self->{group}."'\n");
return undef;
}
my($key,$value);
my $typeID; # id used for attribute
my $dataspaceID; # id used for the attribute dataspace
my $attrID;
foreach $key( sort keys %attrs){
$value = $attrs{$key};
# Create Null-Terminated String Type
$typeID = PDL::IO::HDF5::H5Tcopy(PDL::IO::HDF5::H5T_C_S1());
PDL::IO::HDF5::H5Tset_size($typeID, length($value) || 1 ); # make legth of type eaual to length of $value or 1 if zero
$dataspaceID = PDL::IO::HDF5::H5Screate_simple(0, 0, 0);
#Note: If a attr already exists, then it will be deleted an re-written
# Delete the attribute first
PDL::IO::HDF5::H5errorOff(); # keep h5 lib from complaining
PDL::IO::HDF5::H5Adelete($datasetID, $key);
PDL::IO::HDF5::H5errorOn();
$attrID = PDL::IO::HDF5::H5Acreate($datasetID, $key, $typeID, $dataspaceID, PDL::IO::HDF5::H5P_DEFAULT());
if($attrID < 0 ){
carp "Error in ".__PACKAGE__." attrSet; Can't create attribute '$key'\n";
PDL::IO::HDF5::H5Sclose($dataspaceID);
PDL::IO::HDF5::H5Tclose($typeID); # Cleanup
return undef;
}
# Write the attribute data.
if( PDL::IO::HDF5::H5Awrite($attrID, $typeID, $value) < 0){
carp "Error in ".__PACKAGE__." attrSet; Can't write attribute '$key'\n";
PDL::IO::HDF5::H5Aclose($attrID);
PDL::IO::HDF5::H5Sclose($dataspaceID);
PDL::IO::HDF5::H5Tclose($typeID); # Cleanup
return undef;
}
# Cleanup
PDL::IO::HDF5::H5Aclose($attrID);
PDL::IO::HDF5::H5Sclose($dataspaceID);
PDL::IO::HDF5::H5Tclose($typeID);
}
# Clear-out the attribute index, it is no longer valid with the updates
# we just made.
$self->{fileObj}->clearAttrIndex;
return 1;
}
=head2 attrDel
=for ref
Delete attribute(s)
B<Usage:>
=for usage
$dataset->attrDel( 'attr1',
'attr2',
.
.
.
);
Returns undef on failure, 1 on success.
=cut
sub attrDel {
my $self = shift;
my @attrs = @_; # get atribute names
my $datasetID = $self->{ID};
my $attr;
my $rc; #Return code returned by H5Adelete
foreach $attr( @attrs ){
# Note: We don't consider errors here as cause for aborting, we just
# complain using carp
if( PDL::IO::HDF5::H5Adelete($datasetID, $attr) < 0){
carp "Error in ".__PACKAGE__." attrDel; Error Deleting attribute '$attr'\n";
}
}
# Clear-out the attribute index, it is no longer valid with the updates
# we just made.
$self->{fileObj}->clearAttrIndex;
return 1;
}
=head2 attrs
=for ref
Get a list of all attribute names associated with a dataset
B<Usage:>
=for usage
@attrs = $dataset->attrs;
=cut
sub attrs {
my $self = shift;
my $datasetID = $self->{ID};
my $defaultMaxSize = 256; # default max size of a attribute name
my $noAttr = PDL::IO::HDF5::H5Aget_num_attrs($datasetID); # get the number of attributes
my $attrIndex = 0; # attribute Index
my @attrNames = ();
my $attributeID;
my $attrNameSize; # size of the attribute name
my $attrName; # attribute name
# Go thru each attribute and get the name
for( $attrIndex = 0; $attrIndex < $noAttr; $attrIndex++){
$attributeID = PDL::IO::HDF5::H5Aopen_idx($datasetID, $attrIndex );
if( $attributeID < 0){
carp "Error in ".__PACKAGE__." attrs; Error Opening attribute number $attrIndex\n";
next;
}
#init attrname to 256 length string (Maybe this not necessary with
# the typemap)
$attrName = ' ' x 256;
# Get the name
$attrNameSize = PDL::IO::HDF5::H5Aget_name($attributeID, 256, $attrName );
# If the name is greater than 256, try again with the proper size:
if( $attrNameSize > 256 ){
$attrName = ' ' x $attrNameSize;
$attrNameSize = PDL::IO::HDF5::H5Aget_name($attributeID, $attrNameSize, $attrName );
}
push @attrNames, $attrName;
# Close the attr:
PDL::IO::HDF5::H5Aclose($attributeID);
}
return @attrNames;
}
=head2 attrGet
=for ref
Get the value of an attribute(s)
Currently the only attribute types supported are null-terminated strings.
B<Usage:>
=for usage
my @attrs = $dataset->attrGet( 'attr1', 'attr2');
=cut
sub attrGet {
my $self = shift;
my @attrs = @_; # get atribute array
my $datasetID = $self->{ID};
my($attrName,$attrValue);
my @attrValues; #return array
my $typeID; # id used for attribute
my $dataspaceID; # id used for the attribute dataspace
my $attrID;
foreach $attrName( @attrs){
$attrValue = undef;
# Open the Attribute
$attrID = PDL::IO::HDF5::H5Aopen_name($datasetID, $attrName );
unless( $attrID >= 0){
carp "Error Calling ".__PACKAGE__."::attrget: Can't open HDF5 Attribute name '$attrName'.\n";
next;
}
# Open the data-space
$dataspaceID = PDL::IO::HDF5::H5Aget_space($attrID);
if( $dataspaceID < 0 ){
carp("Can't Open Dataspace for Attribute name '$attrName' in ".__PACKAGE__."::attrget\n");
carp("Can't close Attribute in ".__PACKAGE__.":attrGet\n") if( PDL::IO::HDF5::H5Aclose($attrID) < 0);
next;
}
# Check to see if the dataspace is simple
if( PDL::IO::HDF5::H5Sis_simple($dataspaceID) < 0 ){
carp("Warning: Non-Simple Dataspace for Attribute name '$attrName' ".__PACKAGE__."::attrget\n");
carp("Can't close DataSpace in ".__PACKAGE__.":attrGet\n") if( PDL::IO::HDF5::H5Sclose($dataspaceID) < 0);
carp("Can't close Attribute in ".__PACKAGE__.":attrGet\n") if( PDL::IO::HDF5::H5Aclose($attrID) < 0);
next;
}
# Get the number of dims:
my $Ndims = PDL::IO::HDF5::H5Sget_simple_extent_ndims($dataspaceID);
unless( $Ndims == 0){
if( $Ndims < 0 ){
carp("Warning: Can't Get Number of Dims in Attribute name '$attrName' Dataspace in ".__PACKAGE__.":get\n");
}
if( $Ndims > 0 ){
carp("Warning: Non-Scalar Dataspace for Attribute name '$attrName' Dataspace in ".__PACKAGE__.":get\n");
}
carp("Can't close DataSpace in ".__PACKAGE__.":attrGet\n") if( PDL::IO::HDF5::H5Sclose($dataspaceID) < 0);
carp("Can't close Attribute in ".__PACKAGE__.":attrGet\n") if( PDL::IO::HDF5::H5Aclose($attrID) < 0);
next;
}
# Get the HDF5 dataset datatype;
my $HDF5type = PDL::IO::HDF5::H5Aget_type($attrID );
unless( $HDF5type >= 0 ){
carp "Error Calling ".__PACKAGE__."::attrGet: Can't get HDF5 Dataset type in Attribute name '$attrName'.\n";
carp("Can't close DataSpace in ".__PACKAGE__.":attrGet\n") if( PDL::IO::HDF5::H5Sclose($dataspaceID) < 0);
carp("Can't close Attribute in ".__PACKAGE__.":attrGet\n") if( PDL::IO::HDF5::H5Aclose($attrID) < 0);
next;
}
# Get the size so we can allocate space for it
my $size = PDL::IO::HDF5::H5Tget_size($HDF5type);
unless( $size){
carp "Error Calling ".__PACKAGE__."::attrGet: Can't get HDF5 Dataset type size in Attribute name '$attrName'.\n";
carp("Can't close Datatype in ".__PACKAGE__.":attrGet\n") if( PDL::IO::HDF5::H5Tclose($HDF5type) < 0);
carp("Can't close DataSpace in ".__PACKAGE__.":attrGet\n") if( PDL::IO::HDF5::H5Sclose($dataspaceID) < 0);
carp("Can't close Attribute in ".__PACKAGE__.":attrGet\n") if( PDL::IO::HDF5::H5Aclose($attrID) < 0);
next;
}
#init attr value to the length of the type
$attrValue = ' ' x ($size);
if( PDL::IO::HDF5::H5Aread($attrID, $HDF5type, $attrValue) < 0 ){
carp "Error Calling ".__PACKAGE__."::attrGet: Can't read Attribute Value for Attribute name '$attrName'.\n";
carp("Can't close Datatype in ".__PACKAGE__.":attrGet\n") if( PDL::IO::HDF5::H5Tclose($HDF5type) < 0);
carp("Can't close DataSpace in ".__PACKAGE__.":attrGet\n") if( PDL::IO::HDF5::H5Sclose($dataspaceID) < 0);
carp("Can't close Attribute in ".__PACKAGE__.":attrGet\n") if( PDL::IO::HDF5::H5Aclose($attrID) < 0);
next;
}
# Cleanup
carp("Can't close Datatype in ".__PACKAGE__.":attrGet\n") if( PDL::IO::HDF5::H5Tclose($HDF5type) < 0);
carp("Can't close DataSpace in ".__PACKAGE__.":attrGet\n") if( PDL::IO::HDF5::H5Sclose($dataspaceID) < 0);
carp("Can't close Attribute in ".__PACKAGE__.":attrGet\n") if( PDL::IO::HDF5::H5Aclose($attrID) < 0);
}
continue{
push @attrValues, $attrValue;
}
return @attrValues;
}
=head2 IDget
=for ref
Returns the HDF5 library ID for this object
B<Usage:>
=for usage
my $ID = $dataSetObj->IDget;
=cut
sub IDget{
my $self = shift;
return $self->{ID};
}
=head2 nameGet
=for ref
Returns the HDF5 Dataset Name for this object.
B<Usage:>
=for usage
my $name = $datasetObj->nameGet;
=cut
sub nameGet{
my $self = shift;
return $self->{name};
}
1;