Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

[23b465]: HDF5 / Group.pm Maximize Restore History

Download this file

Group.pm    379 lines (233 with data), 7.2 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
package PDL::HDF5::Group;
use Carp;
=head1 NAME
PDL::HDF5::Group - PDL::HDF5 Helper Object representing HDF5 groups.
=head1 DESCRIPTION
This is a helper-object used by PDL::HDF5 to interface with HDF5 format's group 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::HDF5>
=head1 MEMBER DATA
=over 1
=item groupID
ID number given to the group by the HDF5 library
=item groupname
Name of the group. (Absolute to the root group '/'. e.g. /maingroup/subgroup)
=item fileID
fileID of the HDF file that owns this group.
=item filename
filename of the HDF file that owns this group
=back
=head1 METHODS
####---------------------------------------------------------
=head2 new
=for ref
PDL::HDF5::Group 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::HDF5> object to build the object.
$a = new PDL::HDF5:Group( name => $name, filename => $filename, fileID => $fileID );
Args:
$name Name of the group
$filename Filename that owns this group
$fileID FileID of the file that owns this group
=cut
sub new{
my $type = shift;
my %parms = @_;
my $self = {};
my @DataMembers = qw( name filename fileID );
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 $fileID = $self->{fileID};
my $filename = $self->{filename};
my $name = $self->{name};
my $groupID;
# Turn Error Reporting off for the following, so H5 lib doesn't complain
# if the group isn't found.
PDL::HDF5::H5errorOff();
my $rc = PDL::HDF5::H5Gget_objinfo($fileID, $name,1,0);
PDL::HDF5::H5errorOn();
# See if the group exists:
if( $rc >= 0){
#Group Exists open it:
$groupID = PDL::HDF5::H5Gopen($fileID, $name);
}
else{ # group didn't exist, create it:
$groupID = PDL::HDF5::H5Gcreate($fileID, $name, 0);
}
# Try Opening the Group First (Assume it already exists)
if($groupID < 0 ){
carp "Error Calling ".__PACKAGE__." Constuctor: Can't open or create group '$name'\n";
return undef;
}
$self->{groupID} = $groupID;
bless $self, $type;
return $self;
}
=head2 DESTROY
=for ref
PDL::HDF5 Destructor - Closes the HDF5::Group Object.
B<Usage:>
=for usage
No Usage. Automatically called
=cut
sub DESTROY {
my $self = shift;
if( H5Gclose($self->{groupID}) < 0){
warn("Error closing HDF5 Group '".$self->{name}."' in file '".$self->{filename}."'\n");
}
}
=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
$group->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 $groupID = $self->{groupID};
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::HDF5::H5Tcopy(PDL::HDF5::H5T_C_S1());
PDL::HDF5::H5Tset_size($typeID, length($value)); # make legth of type eaual to length of $value
$dataspaceID = PDL::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::HDF5::H5errorOff(); # keep h5 lib from complaining
PDL::HDF5::H5Adelete($groupID, $key);
PDL::HDF5::H5errorOn();
$attrID = PDL::HDF5::H5Acreate($groupID, $key, $typeID, $dataspaceID, PDL::HDF5::H5P_DEFAULT());
if($attrID < 0 ){
carp "Error in ".__PACKAGE__." attrSet; Can't create attribute '$key'\n";
PDL::HDF5::H5Sclose($dataspaceID);
PDL::HDF5::H5Tclose($typeID); # Cleanup
return undef;
}
# Write the attribute data.
if( PDL::HDF5::H5Awrite($attrID, $typeID, $value) < 0){
carp "Error in ".__PACKAGE__." attrSet; Can't write attribute '$key'\n";
PDL::HDF5::H5Aclose($attrID);
PDL::HDF5::H5Sclose($dataspaceID);
PDL::HDF5::H5Tclose($typeID); # Cleanup
return undef;
}
# Cleanup
PDL::HDF5::H5Aclose($attrID);
PDL::HDF5::H5Sclose($dataspaceID);
PDL::HDF5::H5Tclose($typeID);
}
return 1;
}
=head2 attrDel
=for ref
Delete attribute(s)
B<Usage:>
=for usage
$group->attrDel( 'attr1',
'attr2',
.
.
.
);
Returns undef on failure, 1 on success.
=cut
sub attrDel {
my $self = shift;
my @attrs = @_; # get atribute names
my $groupID = $self->{groupID};
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::HDF5::H5Adelete($groupID, $attr) < 0){
carp "Error in ".__PACKAGE__." attrDel; Error Deleting attribute '$attr'\n";
}
}
return 1;
}
=head2 attrs
=for ref
Get a list of all attribute names in a group
B<Usage:>
=for usage
@attrs = $group->attrs;
=cut
sub attrs {
my $self = shift;
my $groupID = $self->{groupID};
my $defaultMaxSize = 256; # default max size of a attribute name
my $noAttr = PDL::HDF5::H5Aget_num_attrs($groupID); # 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::HDF5::H5Aopen_idx($groupID, $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::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::HDF5::H5Aget_name($attributeID, $attrNameSize, $attrName );
}
push @attrNames, $attrName;
# Close the attr:
PDL::HDF5::H5Aclose($attributeID);
}
return @attrNames;
}
=head2 datasets
=for ref
Get a list of all dataset names in a group
B<Usage:>
=for usage
@datasets = $group->datasets;
=cut
sub datasets {
my $self = shift;
my $groupID = $self->{groupID};
my $groupName = $self->{name};
my @totalDatasets = PDL::HDF5::H5GgetDatasetNames($groupID,$groupName);
return @totalDatasets;
}
1;