Debian Codegraph @

SD/SD.pd

raw
1 use strict;
2 use warnings;
3 pp_addpm({At => 'Top'}, <<'EOD');
4
5 =head1 NAME
6
7 PDL::IO::HDF::SD - PDL interface to the HDF4 SD library.
8
9 =head1 SYNOPSIS
10
11 use PDL;
12 use PDL::IO::HDF::SD;
13
14 #
15 # Creating and writing an HDF file
16 #
17
18 # Create an HDF file:
19 my $hdf = PDL::IO::HDF::SD->new("-test.hdf");
20
21 # Define some data
22 my $data = sequence(short, 500, 5);
23
24 # Put data in file as 'myData' dataset with the names
25 # of dimensions ('dim1' and 'dim2')
26 $hdf->SDput("myData", $data , ['dim1','dim2']);
27
28 # Put some local attributes in 'myData'
29 #
30 # Set the fill value to 0
31 my $res = $hdf->SDsetfillvalue("myData", 0);
32 # Set the valid range from 0 to 2000
33 $res = $hdf->SDsetrange("myData", [0, 2000]);
34 # Set the default calibration for 'myData' (scale factor = 1, other = 0)
35 $res = $hdf->SDsetcal("myData");
36
37 # Set a global text attribute
38 $res = $hdf->SDsettextattr('This is a global text test!!', "myGText" );
39 # Set a local text attribute for 'myData'
40 $res = $hdf->SDsettextattr('This is a local text testl!!', "myLText", "myData" );
41
42 # Set a global value attribute (you can put all values you want)
43 $res = $hdf->SDsetvalueattr( PDL::short( 20 ), "myGValue");
44
45 # Set a local value attribute (you can put all values you want)
46 $res = $hdf->SDsetvalueattr( PDL::long( [20, 15, 36] ), "myLValues", "myData" );
47
48 # Close the file
49 $hdf->close();
50
51 #
52 # Reading from an HDF file:
53 #
54
55 # Open an HDF file in read only mode:
56 my $hdf = PDL::IO::HDF::SD->new("test.hdf");
57
58 # Get a list of all datasets:
59 my @dataset_list = $hdf->SDgetvariablename();
60
61 # Get a list of the names of all global attributes:
62 my @globattr_list = $hdf->SDgetattributenames();
63
64 # Get a list of the names of all local attributes for a dataset:
65 my @locattr_list = $hdf->SDgetattributenames("myData");
66
67 # Get the value of local attribute for a dataset:
68 my $value = $hdf->SDgetattribut("myLText","myData");
69
70 # Get a PDL var of the entire dataset 'myData':
71 my $data = $hdf->SDget("myData");
72
73 # Apply the scale factor of 'myData'
74 $data *= $hdf->SDgetscalefactor("myData");
75
76 # Get the fill value and fill the PDL var in with BAD:
77 $data->inplace->setvaltobad( $hdf->SDgetfillvalue("myData") );
78
79 # Get the valid range of a dataset:
80 my @range = $hdf->SDgetrange("myData");
81
82 #Now you can do what you want with your data
83 $hdf->close();
84
85
86 =head1 DESCRIPTION
87
88 This library provides functions to read, write, and manipulate
89 HDF4 files with HDF's SD interface.
90
91 For more information on HDF4, see http://hdf.ncsa.uiuc.edu/
92
93 There have been a lot of changes starting with version 2.0, and these may affect
94 your code. PLEASE see the 'Changes' file for a detailed description of what
95 has been changed. If your code used to work with the circa 2002 version of this
96 module, and does not work anymore, reading the 'Changes' is your best bet.
97
98 In the documentation, the terms dataset and SDS (Scientific Data Set) are used
99 interchangeably.
100
101 =cut
102
103 use strict;
104 use warnings;
105
106 EOD
107
108
109 pp_addhdr(<<'EOH');
110
111 #include <hdf.h>
112 #include <mfhdf.h>
113 #include <string.h>
114 #include <stdlib.h>
115
116 #define PDLchar pdl
117 #define PDLuchar pdl
118 #define PDLshort pdl
119 #define PDLint pdl
120 #define PDLlong pdl
121 #define PDLfloat pdl
122 #define PDLdouble pdl
123 #define PDLvoid pdl
124 #define uchar unsigned char
125
126
127 #define COMP_CODE_NONE 0
128 #define COMP_CODE_RLE 1
129 #define COMP_CODE_SKPHUFF 3
130 #define COMP_CODE_DEFLATE 4
131
132 EOH
133
134 use FindBin;
135 use lib "$FindBin::Bin/..";
136 use buildfunc;
137
138 #-------------------------------------------------------------------------
139 # Create low level interface from HDF SD header file.
140 #-------------------------------------------------------------------------
141
142 create_low_level (<<'EODEF');
143 #
144 # SDS Interface
145 #
146 int SDstart(const char *filename, int access_mode);
147 int SDfileinfo(int sd_id, int *ndatasets, int *global_attr);
148 int SDattrinfo(int s_id, int attr_index, char *attr_name, int *number_type, int *count);
149 #int SDreadattr(int s_id, int attr_index, void *data);
150 int SDreadattr(int s_id, int attr_index, PDLvoid *data);
151 int SDgetinfo(int sds_id, char *sds_name, int *rank, int *dimsizes, int *number_type, int *nattrs);
152 int SDselect(int sd_id, int index);
153 int SDgetdimid(int sds_id, int dim_number);
154 int SDdiminfo(int dim_id, char *name, int *count, int *number_type, int *nattrs);
155 int SDnametoindex(int sd_id, const char *sds_name);
156 #int SDreaddata(int sds_id, int *start, int *stride, int *edge, void *buffer);
157 int SDreaddata(int sds_id, int *start, int *stride, int *edge, PDLvoid *buffer);
158 #int SDsetfillvalue(int sds_id, const void *fill_val);
159 int SDsetfillvalue(int sds_id, const PDLvoid *fill_val);
160 #int SDsetrange(int sds_id, const void *max, const void *min);
161 int SDsetrange(int sds_id, const PDLvoid *max, const PDLvoid *min);
162 #int SDwritedata(int sds_id, const int *start, const int *stride, const int *edge, const void *data);
163 int SDwritedata(int sds_id, const int *start, const int *stride, const int *edge, const PDLvoid *data);
164 int SDsetexternalfile(int sds_id, const char *filename, int offset);
165 int SDsetdimstrs(int dim_id, const char *label, const char *unit, const char *format);
166 int SDsetdimscale(int dim_id, int count, int number_type, const void *data);
167 int SDsetdimname(int dim_id, const char *dim_name);
168 int SDsetdatastrs(int sds_id, const char *label, const char *unit, const char *format, const char *coordsys);
169 int SDsetcal(int sds_id, double cal, double cal_err, double offset, double offset_err, int number_type);
170 #int SDsetcal(int sds_id, float cal, float cal_err, float offset, float offset_err, int number_type);
171 int SDsetattr(int s_id, const char *attr_name, int num_type, int count, const void *values);
172 int SDreftoindex(int sd_id, int sds_ref);
173 int SDiscoordvar(int sds_id);
174 int SDidtoref(int sds_id);
175 int SDgetdimstrs(int dim_id, char *label, char *unit, char *format, int len);
176 int SDgetdimscale(int dim_id, void *data);
177 int SDgetdatastrs(int sds_id, char *label, char *unit, char *format, char *coordsys, int len);
178
179
180 #ORIG:
181 #int SDgetcal(int sds_id, double cal, double cal_err, double offset, double offset_err, double number_type);
182 #int SDgetcal(int sds_id, float cal, float cal_err, float offset, float offset_err, int number_type);
183 #int SDgetcal(int sds_id, double *cal, double *cal_err, float64 *offset, float64 *offset_err, int *number_type);
184
185 int SDendaccess(int sds_id);
186 int SDend(int sd_id);
187 int SDcreate(int sd_id, const char *name, int number_type, int rank, const int *dimsizes);
188
189 int SDwritechunk(int sds_id, const int* origin, const PDLvoid *data);
190 int SDsetchunkcache(int sds_id, int maxcache, int flag);
191
192 EODEF
193
194 pp_addxs('',<<'ENDXS');
195
196 void
197 _HEprint(int level)
198 CODE:
199 HEprint(stderr, level);
200
201 int
202 _SDgetcal(sds_id, cal, cal_err, offset, offset_err, number_type)
203 int sds_id
204 double cal
205 double cal_err
206 double offset
207 double offset_err
208 int* number_type
209 CODE:
210 RETVAL = SDgetcal(sds_id, &cal, &cal_err, &offset, &offset_err, number_type);
211 OUTPUT:
212 RETVAL
213
214 void
215 UnpackSBigEndianPDL(size, buff, p)
216 int size
217 unsigned char * buff
218 PDLint * p
219 CODE:
220 int i, INTtmp;
221 unsigned char bch1, bch2;
222 int * data;
223
224 data = p->data;
225
226 for(i=0; i<size; i++)
227 {
228 bch1 = buff[i*2];
229 bch2 = buff[i*2+1];
230
231 INTtmp = (bch1 << 8) + bch2;
232
233 if( INTtmp >= 32768 )
234 { INTtmp -= 65536; }
235
236 data[i] = INTtmp;
237 }
238 OUTPUT:
239 p
240
241 int
242 _SDsetcompress(sd_id, ldef);
243 int sd_id
244 int ldef
245 CODE:
246 comp_info c_info;
247 c_info.deflate.level = ldef;
248 RETVAL = SDsetcompress(sd_id, COMP_CODE_DEFLATE, &c_info) + 1;
249 OUTPUT:
250 RETVAL
251
252 int
253 _SDsetchunk(sds_id, rank, chunk_lengths);
254 int sds_id
255 int rank
256 int* chunk_lengths
257 CODE:
258 HDF_CHUNK_DEF c_def;
259 int i;
260 int32 status = FAIL;
261 for(i = 0; i < rank; i++)
262 {
263 /* fprintf(stderr, "_SDsetchunk(): chunk_lengths[%d] = %d\n", i , chunk_lengths[i]); */
264 c_def.chunk_lengths[i] = chunk_lengths[i];
265 c_def.comp.chunk_lengths[i] = chunk_lengths[i];
266 }
267 c_def.comp.comp_type = COMP_CODE_DEFLATE;
268 c_def.comp.cinfo.deflate.level = 6;
269 status = SDsetchunk(sds_id, c_def, (HDF_CHUNK | HDF_COMP) );
270 if( status == FAIL )
271 {
272 fprintf(stderr, "_SDsetchunk(): return status = %d\n", status);
273 HEprint(stderr, 0);
274 }
275 RETVAL = status;
276 OUTPUT:
277 RETVAL
278
279 int
280 _SDinitchunk(sds_id, type, rank, chunk_lengths);
281 int sds_id
282 int type
283 int rank
284 int* chunk_lengths
285 CODE:
286 void* data = NULL;
287 int* origin = NULL;
288 int i;
289 size_t size;
290 int status;
291 origin = malloc( sizeof( int ) * rank );
292 for( i = 0; i < rank; i++ )
293 origin[i] = 0;
294 /* Just use the largest datatype here: */
295 size = DFKNTsize(type) * chunk_lengths[0];
296 if( rank > 1 )
297 {
298 for( i = 1; i < rank; i++ )
299 size *= chunk_lengths[i];
300 }
301 data = malloc( size );
302 status = SDwritechunk(sds_id, origin, data);
303 if( status == FAIL )
304 {
305 fprintf(stderr, "_SDinitchunk(): return status = %d\n", status);
306 HEprint(stderr, 0);
307 }
308 free( data );
309 free( origin );
310 RETVAL = status;
311 OUTPUT:
312 RETVAL
313
314 int
315 Hishdf(filename);
316 char* filename
317 CODE:
318 RETVAL = Hishdf(filename);
319 OUTPUT:
320 RETVAL
321
322 int
323 _SDgetunlimiteddim(sds_id, dim);
324 int sds_id
325 int dim
326 CODE:
327 char sds_name[250];
328 int rank;
329 int dimsizes[32];
330 int num_type;
331 int nattrs;
332 RETVAL = SDgetinfo(sds_id, sds_name, &rank, dimsizes, &num_type, &nattrs) + 1;
333 if(RETVAL==1){RETVAL = dimsizes[dim];}
334 OUTPUT:
335 RETVAL
336
337 int
338 _SDsetattr_text(s_id, name, text, size);
339 int s_id
340 char * name
341 char * text
342 int size
343 CODE:
344 RETVAL = SDsetattr(s_id, name, 4, size, text);
345 OUTPUT:
346 RETVAL
347
348 int
349 _SDsetattr_values(s_id, name, values, size, type);
350 int s_id
351 char * name
352 pdl * values
353 int size
354 int type
355 CODE:
356 RETVAL = SDsetattr(s_id, name, type, size, values->data);
357 OUTPUT:
358 RETVAL
359
360 ENDXS
361
362 pp_addpm(<<'EOPM');
363
364 use PDL::Primitive;
365 use PDL::Basic;
366
367 use PDL::IO::HDF;
368
369 require POSIX;
370
371 sub _pkg_name
372 { return "PDL::IO::HDF::SD::" . shift() . "()"; }
373
374 # Convert a byte to a char:
375 sub Byte2Char
376 {
377 my ($strB) = @_;
378 my $strC;
379 for(my $i=0; $i<$strB->nelem; $i++)
380 {
381 $strC .= chr( $strB->at($i) );
382 }
383 return($strC);
384 } # End of Byte2Char()...
385
386 =head1 CLASS METHODS
387
388 =head2 new
389
390 =for ref
391
392 Open or create a new HDF object.
393
394 =for usage
395
396 Arguments:
397 1 : The name of the file.
398 if you want to write to it, prepend the name with the '+' character : "+name.hdf"
399 if you want to create it, prepend the name with the '-' character : "-name.hdf"
400 otherwise the file will be open in read only mode
401
402 Returns the hdf object (die on error)
403
404 =for example
405
406 my $hdf = PDL::IO::HDF::SD->new("file.hdf");
407
408 =cut
409
410
411 sub new
412 {
413 # General:
414 my $type = shift;
415 my $filename = shift;
416
417 my $sub = _pkg_name( 'new' );
418
419 my $debug = 0;
420
421 my $self = {};
422
423 if (substr($filename, 0, 1) eq '+')
424 { # open for writing
425 $filename = substr ($filename, 1); # chop off +
426 $self->{ACCESS_MODE} = PDL::IO::HDF->DFACC_WRITE + PDL::IO::HDF->DFACC_READ;
427 }
428 if (substr($filename, 0, 1) eq '-')
429 { # Create new file
430 $filename = substr ($filename, 1); # chop off -
431 print "$sub: Creating HDF File $filename\n"
432 if $debug;
433 $self->{ACCESS_MODE} = PDL::IO::HDF->DFACC_CREATE;
434 $self->{SDID} = PDL::IO::HDF::SD::_SDstart( $filename, $self->{ACCESS_MODE} );
435 my $res = PDL::IO::HDF::SD::_SDend( $self->{SDID} );
436 die "$sub: _ERR::Create\n"
437 if( ($self->{SDID} == PDL::IO::HDF->FAIL ) || ( $res == PDL::IO::HDF->FAIL ));
438 $self->{ACCESS_MODE} = PDL::IO::HDF->DFACC_WRITE + PDL::IO::HDF->DFACC_READ;
439 }
440 unless( defined( $self->{ACCESS_MODE} ) )
441 { # Default to Read-only access:
442 $self->{ACCESS_MODE} = PDL::IO::HDF->DFACC_READ;
443 }
444 $self->{FILE_NAME} = $filename;
445
446 # SD interface:
447 print "$sub: Loading HDF File $self->{FILE_NAME}\n"
448 if $debug;
449
450 $self->{SDID} = PDL::IO::HDF::SD::_SDstart( $self->{FILE_NAME}, $self->{ACCESS_MODE} );
451 die "$sub: _ERR::SDstart\n"
452 if( $self->{SDID} == PDL::IO::HDF->FAIL );
453
454 my $num_datasets = -999;
455 my $num_global_attrs = -999;
456 my $res = _SDfileinfo( $self->{SDID}, $num_datasets, $num_global_attrs );
457 die "$sub: ** sdFileInfo **\n"
458 if($res == PDL::IO::HDF->FAIL);
459
460 foreach my $i ( 0 .. $num_global_attrs-1 )
461 {
462 print "$sub: Loading Global Attribute #$i\n"
463 if $debug;
464
465 my $attrname = " "x(PDL::IO::HDF->MAX_NC_NAME+1);
466 my $type = 0;
467 my $count = 0;
468
469 $res = _SDattrinfo( $self->{SDID}, $i, $attrname, $type, $count );
470 die "$sub: ** sdAttrInfo **\n"
471 if($res == PDL::IO::HDF->FAIL);
472
473 print "$sub: \$attrname = \'$attrname\'\n"
474 if $debug;
475
476 $self->{GLOBATTR}->{$attrname} = zeroes( $PDL::IO::HDF::SDinvtypeTMAP2->{$type}, $count );
477 $res = _SDreadattr( $self->{SDID}, $i, $self->{GLOBATTR}->{$attrname} );
478 die "$sub: ** sdReadAttr **\n"
479 if($res == PDL::IO::HDF->FAIL);
480
481 if( $type == PDL::IO::HDF->DFNT_CHAR )
482 {
483 $self->{GLOBATTR}->{$attrname} = Byte2Char( $self->{GLOBATTR}->{$attrname} );
484 }
485 }
486
487 my @dataname;
488 foreach my $i ( 0 .. $num_datasets-1 )
489 {
490 print "$sub: Loading SDS #$i\n"
491 if $debug;
492
493 my $sds_id = _SDselect( $self->{SDID}, $i );
494 die "$sub: ** sdSelect **\n"
495 if($sds_id == PDL::IO::HDF->FAIL);
496
497 my $name = " "x(PDL::IO::HDF->MAX_NC_NAME+1);
498 my $rank = 0;
499 my $dimsize = " "x( (4 * PDL::IO::HDF->MAX_VAR_DIMS) + 1 );
500 my $numtype = 0;
501 my $num_attrs = 0;
502
503 $res = _SDgetinfo($sds_id, $name, $rank, $dimsize, $numtype, $num_attrs);
504 die "$sub: ** sdGetInfo **\n"
505 if($res == PDL::IO::HDF->FAIL);
506
507 print "$sub: \$name = \'$name\'\n"
508 if $debug;
509 print "$sub: \$dimsize = \'$dimsize\'\n"
510 if $debug;
511
512 $self->{DATASET}->{$name}->{TYPE} = $numtype;
513 $self->{DATASET}->{$name}->{RANK} = $rank;
514 $self->{DATASET}->{$name}->{SDSID} = $sds_id;
515
516 # Load up information on the dimensions (named, unlimited, etc...):
517 #
518 foreach my $j ( 0 .. $self->{DATASET}->{$name}->{RANK}-1 )
519 {
520 print "$sub: Loading SDS($i) Dimension #$j\n"
521 if $debug;
522
523 my $dim_id = _SDgetdimid( $sds_id, $j );
524 die "$sub: ** sdGetDimId **\n"
525 if($dim_id == PDL::IO::HDF->FAIL);
526
527 my $dimname = " "x(PDL::IO::HDF->MAX_NC_NAME+1);
528 my $size = 0;
529 my $num_type = 0;
530 my $num_dim_attrs = 0;
531
532 $res = _SDdiminfo( $dim_id, $dimname, $size, $num_type, $num_dim_attrs );
533 die "$sub: ** sdDimInfo **\n"
534 if($res == PDL::IO::HDF->FAIL);
535
536 print "$sub: \$dimname = \'$dimname\'\n"
537 if $debug;
538
539 $self->{DATASET}->{$name}->{DIMS}->{$j}->{DIMID} = $dim_id;
540 $self->{DATASET}->{$name}->{DIMS}->{$j}->{SIZE} = $size;
541 $self->{DATASET}->{$name}->{DIMS}->{$j}->{NAME} = $dimname;
542
543 # The size comes back as 0 if it has the HDF unlimited dimension thing going on:
544 # So, lets figure out what the size is currently at:
545 unless ( $size )
546 {
547 $self->{DATASET}->{$name}->{DIMS}->{$j}->{REAL_SIZE} = _SDgetunlimiteddim( $sds_id, $j);
548 }
549 }
550
551 # Load up info on the SDS's attributes:
552 #
553 foreach my $j ( 0 .. $num_attrs-1 )
554 {
555 print "$sub: Loading SDS($i) Attribute #$j\n"
556 if $debug;
557
558 my $attrname = " "x(PDL::IO::HDF->MAX_NC_NAME+1);
559 my $type = 0;
560 my $count = 0;
561
562 $res = _SDattrinfo( $sds_id, $j, $attrname, $type, $count);
563 die "$sub: ** sdAttrInfo **\n"
564 if($res == PDL::IO::HDF->FAIL);
565
566 print "$sub: \$attrname = \'$attrname\'\n"
567 if $debug;
568
569 $self->{DATASET}->{$name}->{ATTRS}->{$attrname} =
570 zeroes( $PDL::IO::HDF::SDinvtypeTMAP2->{$type}, $count );
571
572 $res = _SDreadattr( $sds_id, $j, $self->{DATASET}->{$name}->{ATTRS}->{$attrname} );
573 die "$sub: ** sdReadAttr **\n"
574 if($res == PDL::IO::HDF->FAIL);
575
576 # FIXME: This should be a constant
577 if( $type == PDL::IO::HDF->DFNT_CHAR )
578 {
579 $self->{DATASET}->{$name}->{ATTRS}->{$attrname} =
580 Byte2Char( $self->{DATASET}->{$name}->{ATTRS}->{$attrname} );
581 }
582 }
583 }
584
585 bless $self, $type;
586
587 # Now that we're blessed, run our own accessors:
588
589 # Default to using this (it's a good thing :)
590 $self->Chunking( 1 );
591
592 return $self;
593 } # End of new()...
594
595 =head2 Chunking
596
597 =for ref
598
599 Accessor for the chunking mode on this HDF file.
600
601 'Chunking' is an internal compression and tiling the HDF library can
602 perform on an SDS.
603
604 This variable only affects they way SDput() works, and is ON by default.
605
606 The code modifications enabled by this flag automatically partition the
607 dataset to chunks of at least 100x100 values in size. The logic on this
608 is pretty fancy, and would take a while to doc out here. If you
609 _really_ have to know how it auto-partitions the data, then look at
610 the code.
611
612 Someday over the rainbow, I'll add some features for better control of the
613 chunking parameters, if the need arises. For now, it's just stupid easy
614 to use.
615
616 =for usage
617
618 Arguments:
619 1 (optional): new value for the chunking flag.
620
621 =for example
622
623 # See if chunking is currently on for this file:
624 my $chunkvar = $hdf->Chunking();
625
626 # Turn the chunking off:
627 my $newvar = $hdf->Chunking( 0 );
628
629 # Turn the chunking back on:
630 my $newvar = $hdf->Chunking( 1 );
631
632 =cut
633
634
635 # See the changelog for more docs on this feature:
636 sub Chunking
637 {
638 my $self = shift;
639 my $var = shift;
640 if( defined( $var ) )
641 {
642 $self->{CHUNKING} = $var ? 1 : 0;
643 }
644 return $self->{CHUNKING};
645 } # End of Chunking()...
646
647 =head2 SDgetvariablenames
648
649 =for ref
650
651 get the list of datasets.
652
653 =for usage
654
655 No arguments
656 Returns the list of dataset or undef on error.
657
658 =for example
659
660 my @DataList = $hdfobj->SDgetvariablenames();
661
662 =cut
663
664
665 sub SDgetvariablenames
666 {
667 my($self) = @_;
668 return sort keys %{$self->{DATASET}};
669 } # End of SDgetvariablenames()...
670 sub SDgetvariablename
671 {
672 my $self = shift;
673 return $self->SDgetvariablenames( @_ );
674 } # End of SDgetvariablename()...
675
676
677 =head2 SDgetattributenames
678
679 =for ref
680
681 Get a list of the names of the global or SDS attributes.
682
683 =for usage
684
685 Arguments:
686 1 (optional) : The name of the SD dataset from which you want to get
687 the attributes. This arg is optional, and without it, it will
688 return the list of global attribute names.
689
690 Returns a list of names or undef on error.
691
692 =for example
693
694 # For global attributes :
695 my @attrList = $hdf->SDgetattributenames();
696
697 # For SDS attributes :
698 my @attrList = $hdf->SDgetattributenames("dataset_name");
699
700 =cut
701
702
703 sub SDgetattributenames
704 {
705 my($self, $name) = @_;
706 if( defined( $name ) )
707 {
708 return( undef )
709 unless defined( $self->{DATASET}->{$name} );
710 return sort keys %{ $self->{DATASET}->{$name}->{ATTRS} };
711 }
712 else
713 {
714 return sort keys %{ $self->{GLOBATTR} };
715 }
716 } # End of SDgetattributenames()...
717 # Wrapper (this is now defunct):
718 sub SDgetattributname
719 {
720 my $self = shift;
721 return $self->SDgetattributenames( @_ );
722 } # End of SDgetattributname()...
723
724 =head2 SDgetattribute
725
726 =for ref
727
728 Get a global or SDS attribute value.
729
730 =for usage
731
732 Arguments:
733 1 : The name of the attribute.
734 2 (optional): The name of the SDS from which you want to get the attribute
735 value. Without this arg, it returns the global attribute value of that name.
736
737 Returns an attribute value or undef on error.
738
739 =for example
740
741 # for global attributs :
742 my $attr = $hdf->SDgetattribute("attr_name");
743
744 # for local attributs :
745 my $attr = $hdf->SDgetattribute("attr_name", "dataset_name");
746
747 =cut
748
749
750 sub SDgetattribute
751 {
752 my($self, $name, $dataset) = @_;
753 if( defined($dataset) )
754 { # It's an SDS attribute:
755 return( undef )
756 unless defined( $self->{DATASET}->{$dataset} );
757 return $self->{DATASET}->{$dataset}->{ATTRS}->{$name};
758 }
759 else
760 { # Global attribute:
761 return( undef )
762 unless defined( $self->{GLOBATTR}->{$name} );
763 return $self->{GLOBATTR}->{$name};
764 }
765 } # End of SDgetattribute()...
766 # Wrapper (this is now defunct):
767 sub SDgetattribut
768 {
769 my $self = shift;
770 return $self->SDgetattribute( @_ );
771 } # End of SDgetattribut()...
772
773 =head2 SDgetfillvalue
774
775 =for ref
776
777 Get the fill value of an SDS.
778
779 =for usage
780
781 Arguments:
782 1 : The name of the SDS from which you want to get the fill value.
783
784 Returns the fill value or undef on error.
785
786 =for example
787
788 my $fillvalue = $hdf->SDgetfillvalue("dataset_name");
789
790 =cut
791
792
793 sub SDgetfillvalue
794 {
795 my($self, $name) = @_;
796 return( undef )
797 unless defined( $self->{DATASET}->{$name} );
798 return ($self->{DATASET}->{$name}->{ATTRS}->{_FillValue})->at(0);
799 } # End of SDgetfillvalue()...
800
801 =head2 SDgetrange
802
803 =for ref
804
805 Get the valid range of an SDS.
806
807 =for usage
808
809 Arguments:
810 1 : the name of the SDS from which you want to get the valid range.
811
812 Returns a list of two elements [min, max] or undef on error.
813
814 =for example
815
816 my @range = $hdf->SDgetrange("dataset_name");
817
818 =cut
819
820
821 sub SDgetrange
822 {
823 my($self, $name) = @_;
824 return( undef )
825 unless defined( $self->{DATASET}->{$name} );
826 return $self->{DATASET}->{$name}->{ATTRS}->{valid_range};
827 } # End of SDgetrange()...
828
829 =head2 SDgetscalefactor
830
831 =for ref
832
833 Get the scale factor of an SDS.
834
835 =for usage
836
837 Arguments:
838 1 : The name of the SDS from which you want to get the scale factor.
839
840 Returns the scale factor or undef on error.
841
842 =for example
843
844 my $scale = $hdf->SDgetscalefactor("dataset_name");
845
846 =cut
847
848
849 sub SDgetscalefactor
850 {
851 my($self, $name) = @_;
852 return( undef )
853 unless defined( $self->{DATASET}->{$name} );
854
855 return ($self->{DATASET}->{$name}->{ATTRS}->{scale_factor})->at(0);
856 } # End of SDgetscalefactor()...
857
858 =head2 SDgetdimsize
859
860 =for ref
861
862 Get the dimensions of a dataset.
863
864 =for usage
865
866 Arguments:
867 1 : The name of the SDS from which you want to get the dimensions.
868
869 Returns an array of n dimensions with their sizes or undef on error.
870
871 =for example
872
873 my @dim = $hdf->SDgetdimsize("dataset_name");
874
875 =cut
876
877
878 sub SDgetdimsize
879 {
880 my ($self, $name) = @_;
881 return( undef )
882 unless defined( $self->{DATASET}->{$name} );
883 my @dims;
884 foreach( sort keys %{ $self->{DATASET}->{$name}->{DIMS} } )
885 {
886 push @dims, $self->{DATASET}->{$name}->{DIMS}->{$_}->{SIZE};
887 }
888
889 return( @dims );
890 } # End of SDgetdimsize()...
891
892 =head2 SDgetunlimiteddimsize
893
894 =for ref
895
896 Get the actual dimensions of an SDS with 'unlimited' dimensions.
897
898 =for usage
899
900 Arguments:
901 1 : The name of the SDS from which you want to the dimensions.
902
903 Returns an array of n dimensions with the dim sizes or undef on error.
904
905 =for example
906
907 my @dims = $hdf->SDgetunlimiteddimsize("dataset_name");
908
909 =cut
910
911
912 sub SDgetunlimiteddimsize
913 {
914 my ($self, $name) = @_;
915
916 return( undef )
917 unless defined( $self->{DATASET}->{$name} );
918
919 my @dim;
920 foreach( sort keys %{$self->{DATASET}{$name}{DIMS}} )
921 {
922 if( $self->{DATASET}->{$name}->{DIMS}->{$_}->{SIZE} == 0 )
923 {
924 $dim[ $_ ] =
925 $self->{DATASET}->{$name}->{DIMS}->{$_}->{REAL_SIZE};
926 }
927 else
928 {
929 $dim[ $_ ] =
930 $self->{DATASET}->{$name}->{DIMS}->{$_}->{SIZE};
931 }
932 }
933 return(@dim);
934 } # End of SDgetunlimiteddimsize()...
935 # Wrapper (this is now defunct):
936 sub SDgetdimsizeunlimit
937 {
938 my $self = shift;
939 return $self->SDgetunlimiteddimsize( @_ );
940 } # End of SDgetdimsizeunlimit()...
941
942 =head2 SDgetdimnames
943
944 =for ref
945
946 Get the names of the dimensions of a dataset.
947
948 =for usage
949
950 Arguments:
951 1 : the name of a dataset you want to get the dimensions'names .
952
953 Returns an array of n dimensions with their names or an empty list if error.
954
955 =for example
956
957 my @dim_names = $hdf->SDgetdimnames("dataset_name");
958
959 =cut
960
961
962 sub SDgetdimnames
963 {
964 my ($self, $name) = @_;
965
966 return( undef )
967 unless defined( $self->{DATASET}->{$name} );
968
969 my @dims=();
970 foreach( sort keys %{ $self->{DATASET}->{$name}->{DIMS} } )
971 {
972 push @dims,$self->{DATASET}->{$name}->{DIMS}->{$_}->{NAME};
973 }
974 return(@dims);
975 } # End of SDgetdimnames()...
976 sub SDgetdimname
977 {
978 my $self = shift;
979 return $self->SDgetdimnames( @_ );
980 } # End of SDgetdimname();
981
982 =head2 SDgetcal
983
984 =for ref
985
986 Get the calibration factor from an SDS.
987
988 =for usage
989
990 Arguments:
991 1 : The name of the SDS
992
993 Returns (scale factor, scale factor error, offset, offset error, data type), or undef on error.
994
995 =for example
996
997 my ($cal, $cal_err, $off, $off_err, $d_type) = $hdf->SDgetcal("dataset_name");
998
999 =cut
1000
1001
1002 sub SDgetcal
1003 {
1004 my ($self, $name ) = @_;
1005
1006 my ($cal, $cal_err, $off, $off_err, $type);
1007
1008 return( undef )
1009 unless defined( $self->{DATASET}->{$name} );
1010 return( undef )
1011 unless defined( $self->{DATASET}->{$name}->{ATTRS}->{scale_factor} );
1012
1013 $cal = $self->{DATASET}->{$name}->{ATTRS}->{scale_factor};
1014 $cal_err = $self->{DATASET}->{$name}->{ATTRS}->{scale_factor_err};
1015 $off = $self->{DATASET}->{$name}->{ATTRS}->{add_offset};
1016 $off_err = $self->{DATASET}->{$name}->{ATTRS}->{add_offset_err};
1017 $type = $self->{DATASET}->{$name}->{ATTRS}->{calibrated_nt};
1018
1019 return( $cal, $cal_err, $off, $off_err, $type );
1020 } # End of SDgetcal()...
1021
1022 =head2 SDget
1023
1024 =for ref
1025
1026 Get a the data from and SDS, or just a slice of that SDS.
1027
1028 =for usage
1029
1030 Arguments:
1031 1 : The name of the SDS you want to get.
1032 2 (optional): The start array ref of the slice.
1033 3 (optional): The size array ref of the slice (HDF calls this the 'edge').
1034 4 (optional): The stride array ref of the slice.
1035
1036 Returns a PDL of data if ok, PDL::null on error.
1037
1038 If the slice arguments are not given, this function will read the entire
1039 SDS from the file.
1040
1041 The type of the returned PDL variable is the PDL equivalent of what was
1042 stored in the HDF file.
1043
1044 =for example
1045
1046 # Get the entire SDS:
1047 my $pdldata = $hdf->SDget("dataset_name");
1048
1049 # get a slice of the dataset
1050 my $start = [10,50,10]; # the start position of the slice is [10, 50, 10]
1051 my $edge = [20,20,20]; # read 20 values on each dimension from @start
1052 my $stride = [1, 1, 1]; # Don't skip values
1053 my $pdldata = $hdf->SDget( "dataset_name", $start, $edge, $stride );
1054
1055 =cut
1056
1057
1058 sub SDget
1059 {
1060 my($self, $name, $start, $end, $stride) = @_;
1061 my $sub = _pkg_name( 'SDget' );
1062
1063 return null
1064 unless defined( $self->{DATASET}->{$name} );
1065
1066 unless( defined( $end ) )
1067 { # \@end was not passed in, so we need to set everything else to defaults:
1068 ($start, $end) = [];
1069 my @dimnames=$self->SDgetdimnames($name);
1070 for my $dim (0 .. $#dimnames)
1071 {
1072 my $use_size = $self->{DATASET}->{$name}->{DIMS}->{$dim}->{SIZE}
1073 || $self->{DATASET}->{$name}->{DIMS}->{$dim}->{REAL_SIZE};
1074
1075 $$end[ $dim ] = $use_size;
1076 $$start[ $dim ] = 0;
1077 $$stride[ $dim ] = 1;
1078 }
1079 }
1080
1081 my $c_start = pack ("L*", @$start);
1082 my $c_end = pack ("L*", @$end);
1083 my $c_stride = pack ("L*", @$stride);
1084 #print STDERR "$sub: start:[".join(',',@$start)
1085 # ."]=>$c_start end:[".join(',',@$end)
1086 # ."]=>$c_end stride:[".join(',',@$stride)."]=>$c_stride\n";
1087
1088 my $buff = zeroes( $PDL::IO::HDF::SDinvtypeTMAP2->{$self->{DATASET}->{$name}->{TYPE}}, reverse @$end );
1089
1090 my $res = _SDreaddata( $self->{DATASET}->{$name}->{SDSID}, $c_start, $c_stride, $c_end, $buff );
1091 if($res == PDL::IO::HDF->FAIL)
1092 {
1093 $buff = null;
1094 print "$sub: Error returned from _SDreaddata()!\n";
1095 }
1096
1097 return $buff;
1098 } # End of SDget()...
1099
1100 =head2 SDsetfillvalue
1101
1102 =for ref
1103
1104 Set the fill value for an SDS.
1105
1106 =for usage
1107
1108 Arguments:
1109 1 : The name of the SDS.
1110 2 : The fill value.
1111
1112 Returns true on success, undef on error.
1113
1114 =for example
1115
1116 my $res = $hdf->SDsetfillvalue("dataset_name",$fillvalue);
1117
1118 =cut
1119
1120
1121 sub SDsetfillvalue
1122 {
1123 my ($self, $name, $value) = @_;
1124
1125 return( undef )
1126 unless defined( $self->{DATASET}->{$name} );
1127
1128 $value = &{$PDL::IO::HDF::SDinvtypeTMAP->{$self->{DATASET}->{$name}->{TYPE}}}($value);
1129 $self->{DATASET}->{$name}->{ATTRS}->{_FillValue} = $value;
1130
1131 return( _SDsetfillvalue($self->{DATASET}->{$name}->{SDSID}, $value) + 1 );
1132 } # End of SDsetfillvalue()...
1133
1134 =head2 SDsetrange
1135
1136 =for ref
1137
1138 Set the valid range of an SDS.
1139
1140 =for usage
1141
1142 Arguments:
1143 1 : The name of the SDS
1144 2 : an anonymous array of two elements : [min, max].
1145
1146 Returns true on success, undef on error.
1147
1148 =for example
1149
1150 my $res = $hdf->SDsetrange("dataset_name", [$min, $max]);
1151
1152 =cut
1153
1154
1155 sub SDsetrange
1156 {
1157 my ($self, $name, $range) = @_;
1158
1159 return undef
1160 unless defined( $self->{DATASET}->{$name} );
1161
1162 my $min = &{$PDL::IO::HDF::SDinvtypeTMAP->{$self->{DATASET}->{$name}->{TYPE}}}($$range[0]);
1163 my $max = &{$PDL::IO::HDF::SDinvtypeTMAP->{$self->{DATASET}->{$name}->{TYPE}}}($$range[1]);
1164 $range = &{$PDL::IO::HDF::SDinvtypeTMAP->{$self->{DATASET}->{$name}->{TYPE}}}($range);
1165 $self->{DATASET}->{$name}->{ATTRS}->{valid_range} = $range;
1166
1167 return( _SDsetrange($self->{DATASET}->{$name}->{SDSID}, $max, $min) + 1 );
1168 } # End of SDsetrange()...
1169
1170 =head2 SDsetcal
1171
1172 =for ref
1173
1174 Set the HDF calibration for an SDS.
1175
1176 In HDF lingo, this means to define:
1177 scale factor
1178 scale factor error
1179 offset
1180 offset error
1181
1182 =for usage
1183
1184 Arguments:
1185 1 : The name of the SDS.
1186 2 (optional): the scale factor (default is 1)
1187 3 (optional): the scale factor error (default is 0)
1188 4 (optional): the offset (default is 0)
1189 5 (optional): the offset error (default is 0)
1190
1191 Returns true on success, undef on error.
1192
1193 NOTE: This is not required to make a valid HDF SDS, but is there if you want to use it.
1194
1195 =for example
1196
1197 # Create the dataset:
1198 my $res = $hdf->SDsetcal("dataset_name");
1199
1200 # To just set the scale factor:
1201 $res = $hdf->SDsetcal("dataset_name", $scalefactor);
1202
1203 # To set all calibration parameters:
1204 $res = $hdf->SDsetcal("dataset_name", $scalefactor, $scale_err, $offset, $off_err);
1205
1206 =cut
1207
1208
1209 sub SDsetcal
1210 {
1211 my $self = shift;
1212 my $name = shift;
1213
1214 return( undef )
1215 unless defined( $self->{DATASET}->{$name} );
1216
1217 $self->{DATASET}->{$name}->{ATTRS}->{scale_factor} = shift || 1;
1218 $self->{DATASET}->{$name}->{ATTRS}->{scale_factor_err} = shift || 0;
1219 $self->{DATASET}->{$name}->{ATTRS}->{add_offset} = shift || 0;
1220 $self->{DATASET}->{$name}->{ATTRS}->{add_offset_err} = shift || 0;
1221 # PDL_Double is the default type:
1222 $self->{DATASET}->{$name}->{ATTRS}->{calibrated_nt} = shift || 6;
1223
1224 return(
1225 _SDsetcal(
1226 $self->{DATASET}->{$name}->{SDSID},
1227 $self->{DATASET}->{$name}->{ATTRS}->{scale_factor},
1228 $self->{DATASET}->{$name}->{ATTRS}->{scale_factor_err},
1229 $self->{DATASET}->{$name}->{ATTRS}->{add_offset},
1230 $self->{DATASET}->{$name}->{ATTRS}->{add_offset_err},
1231 $self->{DATASET}->{$name}->{ATTRS}->{calibrated_nt}
1232 ) + 1);
1233 } # End of SDsetcal()...
1234
1235 =head2 SDsetcompress
1236
1237 =for ref
1238
1239 Set the internal compression on an SDS.
1240
1241 =for usage
1242
1243 Arguments:
1244 1 : The name of the SDS.
1245 2 (optional): The gzip compression level ( 1 - 9 ). If not
1246 specified, then 6 is used.
1247
1248 Returns true on success, undef on failure.
1249
1250 WARNING: This is a fairly buggy feature with many version of the HDF library.
1251 Please just use the 'Chunking' features instead, as they work far better, and
1252 are more reliable.
1253
1254 =for example
1255
1256 my $res = $hdf->SDsetfillvalue("dataset_name",$deflate_value);
1257
1258 =cut
1259
1260
1261 sub SDsetcompress
1262 {
1263 my ($self, $name) = @_;
1264
1265 return( undef )
1266 unless defined( $self->{DATASET}->{$name} );
1267
1268 # NOTE: Behavior change from the old version:
1269 # it used to set to 6 if the passed value was greater than 8
1270 # it now sets it to 9 if it's greater than 9.
1271 my $deflate = shift || 6;
1272 $deflate = 9
1273 if( $deflate > 9 );
1274
1275 return( 1 + _SDsetcompress( $self->{DATASET}->{$name}->{SDSID}, $deflate ) );
1276 } # End of SDsetcompress()...
1277
1278 =head2 SDsettextattr
1279
1280 =for ref
1281
1282 Add a text HDF attribute, either globally, or to an SDS.
1283
1284 =for usage
1285
1286 Arguments:
1287 1 : The text you want to add.
1288 2 : The name of the attribute
1289 3 (optional): The name of the SDS.
1290
1291 Returns true on success, undef on failure.
1292
1293 =for example
1294
1295 # Set a global text attribute:
1296 my $res = $hdf->SDsettextattr("my_text", "attribut_name");
1297
1298 # Set a local text attribute for 'dataset_name':
1299 $res = $hdf->SDsettextattr("my_text", "attribut_name", "dataset_name");
1300
1301 =cut
1302
1303
1304 sub SDsettextattr
1305 {
1306 my ($self, $text, $name, $dataset) = @_;
1307
1308 if( defined($dataset) )
1309 {
1310 return( undef )
1311 unless defined( $self->{DATASET}->{$dataset} );
1312
1313 $self->{DATASET}->{$dataset}->{ATTRS}->{$name} = $text;
1314 return( _SDsetattr_text( $self->{DATASET}->{$dataset}->{SDSID}, $name, $text, length($text) ) + 1 );
1315 }
1316
1317 # Implied else it's a global attribute:
1318 $self->{GLOBATTR}->{$name} = $text;
1319 return( _SDsetattr_text( $self->{SDID}, $name, $text, length($text) ) + 1);
1320 } # End of SDsettextattr()...
1321
1322 =head2 SDsetvalueattr
1323
1324 =for ref
1325
1326 Add a non-text HDF attribute, either globally, or to an SDS.
1327
1328 =for usage
1329
1330 Arguments:
1331 1 : A pdl of value(s) you want to store.
1332 2 : The name of the attribute.
1333 3 (optional): the name of the SDS.
1334
1335 Returns true on success, undef on failure.
1336
1337 =for example
1338
1339 my $attr = sequence( long, 4 );
1340
1341 # Set a global attribute:
1342 my $res = $hdf->SDsetvalueattr($attribute, "attribute_name");
1343
1344 # Set a local attribute for 'dataset_name':
1345 $res = $hdf->SDsetvalueattr($attribute, "attribute_name", "dataset_name");
1346
1347 =cut
1348
1349
1350 sub SDsetvalueattr
1351 {
1352 my ($self, $values, $name, $dataset) = @_;
1353
1354 if( defined($dataset) )
1355 {
1356 return( undef )
1357 unless defined( $self->{DATASET}->{$dataset} );
1358
1359 $self->{DATASET}->{$dataset}->{ATTRS}->{$name} = $values;
1360 return( _SDsetattr_values(
1361 $self->{DATASET}->{$dataset}->{SDSID}, $name, $values,
1362 $values->nelem(), $PDL::IO::HDF::SDtypeTMAP->{$values->get_datatype()} ) + 1);
1363 }
1364 # Implied else it's a global attribute:
1365 $self->{GLOBATTR}->{$name} = $values;
1366 return( _SDsetattr_values(
1367 $self->{SDID}, $name, $values,
1368 $values->nelem(), $PDL::IO::HDF::SDtypeTMAP->{$values->get_datatype()} ) + 1);
1369 } # End of SDsetvalueattr()...
1370
1371 =head2 SDsetdimname
1372
1373 =for ref
1374
1375 Set or rename the dimensions of an SDS.
1376
1377 =for usage
1378
1379 Arguments:
1380 1 : The name of the SDS.
1381 2 : An anonymous array with the dimensions names. For dimensions you want
1382 to leave alone, leave 'undef' placeholders.
1383
1384 Returns true on success, undef on failure.
1385
1386 =for example
1387
1388 # Rename all dimensions
1389 my $res = $hdf->SDsetdimname("dataset_name", ['dim1','dim2','dim3']);
1390
1391 # Rename some dimensions
1392 $res = $hdf->SDsetdimname("dataset_name", ['dim1', undef ,'dim3']);
1393
1394 =cut
1395
1396
1397 # FIXME: There are several problems with this:
1398 # - The return code is an aggregate, and not necessarily accurate
1399 # - It bails on the first error without trying the rest. If that is still
1400 # desired, then it should run the check first, and if it's ok, then actually
1401 # make the HDF library call.
1402 sub SDsetdimname
1403 {
1404 my ($self, $name, $dimname) = @_;
1405
1406 return undef
1407 unless defined( $self->{DATASET}->{$name} );
1408
1409 my $res = 0;
1410 foreach( sort keys %{$self->{DATASET}->{$name}->{DIMS}} )
1411 {
1412 return( undef )
1413 unless defined( $$dimname[ $_ ] );
1414
1415 $res = _SDsetdimname(
1416 $self->{DATASET}->{$name}->{DIMS}->{$_}->{DIMID},
1417 $$dimname[ $_ ] ) + 1;
1418 }
1419 return( $res );
1420 } # End of SDsetdimname()...
1421
1422 =head2 SDput
1423
1424 =for ref
1425
1426 Write to a SDS in an HDF file or create and write to it if it doesn't exist.
1427
1428 =for usage
1429
1430 Arguments:
1431 1 : The name of the SDS.
1432 2 : A pdl of data.
1433 3 (optional): An anonymous array of the dim names (only for creation)
1434 4 (optional): An anonymous array of the start of the slice to store
1435 (only for putting a slice)
1436
1437 Returns true on success, undef on failure.
1438
1439 The datatype of the SDS in the HDF file will match the PDL equivalent as
1440 much as possible.
1441
1442 =for example
1443
1444 my $data = sequence( float, 10, 20, 30 ); #any value you want
1445
1446 # Simple case: create a new dataset with a $data pdl
1447 my $result = $hdf->SDput("dataset_name", $data);
1448
1449 # Above, but also naming the dims:
1450 $res = $hdf->SDput("dataset_name", $data, ['dim1','dim2','dim3']);
1451
1452 # Just putting a slice in there:
1453 my $start = [x,y,z];
1454 $res = $hdf->SDput("dataset_name", $data->slice("..."), undef, $start);
1455
1456 =cut
1457
1458
1459 sub SDput
1460 {
1461 my($self, $name, $data, $dimname_p, $from) = @_;
1462
1463 my $sub = _pkg_name( 'SDput' );
1464
1465 my $rank = $data->getndims();
1466 my $dimsize = pack ("L*", reverse $data->dims);
1467
1468 # If this dataset doesn't already exist, then create it:
1469 #
1470 unless ( defined( $self->{DATASET}->{$name} ) )
1471 {
1472 my $hdf_type = $PDL::IO::HDF::SDtypeTMAP->{$data->get_datatype()};
1473
1474 my $res = _SDcreate( $self->{SDID}, $name, $hdf_type, $rank, $dimsize );
1475 return( undef )
1476 if ($res == PDL::IO::HDF->FAIL);
1477
1478 $self->{DATASET}->{$name}->{SDSID} = $res;
1479 $self->{DATASET}->{$name}->{TYPE} = $hdf_type;
1480 $self->{DATASET}->{$name}->{RANK} = $rank;
1481
1482 if( $self->Chunking() )
1483 {
1484 # Setup chunking on this dataset:
1485 my @chunk_lens;
1486 my $min_chunk_size = 100;
1487 my $num_chunks = 10;
1488 my $total_chunks = 1;
1489 foreach my $dimsize ( $data->dims() )
1490 {
1491 my $chunk_size = ($dimsize + 9) / $num_chunks;
1492 my $num_chunks_this_dim = $num_chunks;
1493 if( $chunk_size < $min_chunk_size )
1494 {
1495 $chunk_size = $min_chunk_size;
1496 # Re-calc the num_chunks_per_dim:
1497 $num_chunks_this_dim = POSIX::ceil( $dimsize / $chunk_size );
1498 }
1499 push(@chunk_lens, $chunk_size);
1500 $total_chunks *= $num_chunks_this_dim;
1501 }
1502 my $chunk_lengths = pack("L*", reverse @chunk_lens);
1503
1504 $res = _SDsetchunk( $self->{DATASET}->{$name}->{SDSID}, $rank, $chunk_lengths );
1505 return( undef )
1506 if ($res == PDL::IO::HDF->FAIL);
1507
1508 $res = _SDsetchunkcache( $self->{DATASET}->{$name}->{SDSID}, $total_chunks, 0);
1509 return( undef )
1510 if ($res == PDL::IO::HDF->FAIL);
1511 } # End of chunking section...
1512 } # End of dataset creation...
1513
1514 my $start = [];
1515 my $stride = [];
1516 if( defined( $from ) )
1517 {
1518 $start = $from;
1519 foreach($data->dims)
1520 { push(@$stride, 1); }
1521 }
1522 else
1523 { # $from was not defined, so assume we're doing all of it:
1524 foreach($data->dims)
1525 {
1526 push(@$start, 0);
1527 push(@$stride, 1);
1528 }
1529 }
1530 $start = pack ("L*", @$start);
1531 $stride = pack ("L*", @$stride);
1532 $data->make_physical();
1533
1534 my $res = _SDwritedata( $self->{DATASET}->{$name}->{SDSID}, $start, $stride, $dimsize, $data );
1535 return( undef )
1536 if ($res == PDL::IO::HDF->FAIL);
1537
1538 foreach my $j ( 0 .. $rank-1 )
1539 {
1540 # Probably not a good way to bail:
1541 my $dim_id = _SDgetdimid( $self->{DATASET}->{$name}->{SDSID}, $j );
1542 return( undef )
1543 if( $dim_id == PDL::IO::HDF->FAIL);
1544
1545 if( defined( @$dimname_p[$j] ) )
1546 {
1547 $res = _SDsetdimname( $dim_id, @$dimname_p[$j] );
1548 return( undef )
1549 if( $res == PDL::IO::HDF->FAIL );
1550 }
1551
1552 my $dimname = " "x(PDL::IO::HDF->MAX_NC_NAME);
1553 my $size = 0;
1554 my $num_dim_attrs = 0;
1555 $res = _SDdiminfo( $dim_id, $dimname, $size, my $numtype=0, $num_dim_attrs);
1556
1557 return( undef )
1558 if ($res == PDL::IO::HDF->FAIL);
1559 $self->{DATASET}->{$name}->{DIMS}->{$j}->{NAME} = $dimname;
1560 $self->{DATASET}->{$name}->{DIMS}->{$j}->{SIZE} = $size;
1561 $self->{DATASET}->{$name}->{DIMS}->{$j}->{DIMID} = $dim_id;
1562 }
1563 return( 1 );
1564 } # End of SDput()...
1565
1566 =head2 close
1567
1568 =for ref
1569
1570 Close an HDF file.
1571
1572 =for usage
1573
1574 No arguments.
1575
1576 =for example
1577
1578 my $result = $hdf->close();
1579
1580 =cut
1581
1582
1583 # NOTE: This may not be enough, since there may be opened datasets as well! SDendaccess()!
1584 sub close
1585 {
1586 my $self = shift;
1587 my $sdid = $self->{SDID};
1588 $self = undef;
1589 return( _SDend( $sdid ) + 1);
1590 } # End of close()...
1591
1592 sub DESTROY
1593 {
1594 my $self = shift;
1595 $self->close;
1596 } # End of DESTROY()...
1597
1598 EOPM
1599
1600 #
1601 # Add the tail of the documentation to the module:
1602 #
1603 pp_addpm(<<'EOD');
1604
1605 =head1 CURRENT AUTHOR & MAINTAINER
1606
1607 Judd Taylor, Orbital Systems, Ltd.
1608 judd dot t at orbitalsystems dot com
1609
1610 =head1 PREVIOUS AUTHORS
1611
1612 Patrick Leilde [email protected]
1613 contribs of Olivier Archer [email protected]
1614
1615 =head1 SEE ALSO
1616
1617 perl(1), PDL(1), PDL::IO::HDF(1).
1618
1619 =cut
1620
1621
1622 EOD
1623
1624 pp_done();