blib/lib/PDL/IO/IDL.pm untested

filter: Statementstmt 0.0% 0 / 367 Branchbran 0.0% 0 / 116 Conditioncond 0.0% 0 / 54 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 24 Podpod 0.0% 0 / 19 totaltotal 0.0% 0 / 580 CC 59 SCAR 45.8
CC
59
Cov
0%
r_var
182.0
r_structdesc
156.0
r_struct
156.0
CRAP 97.4
1 =head1 NAME
2
3 PDL::IO::IDL -- I/O of IDL Save Files
4
5 =head1 DESCRIPTION
6
7 PDL::IO::IDL allows you to read and write IDL(tm) data files.
8
9 Currently, only reading is implemented. Scalars, arrays,
10 and structures are all supported. Heap pointers, compiled code, and
11 objects are not supported. Of those three, only heap pointers are
12 likely to be supported in the future.
13
14 This code was not developed by RSI, makers of IDL.
15
16 =head1 NOTES
17
18 These things seem to work:
19
20 =over 3
21
22 =item BYTE, SHORT, LONG, FLOAT, and DOUBLE numeric types and arrays
23
24 All of these types seem to work fine. The corresponding variable is
25 stored as a PDL in the hash element with the same name as the original
26 variable in the file. Arrays are byteswapped as needed and are read in so
27 that the dim list has the same indexing order within PDL as it did within IDL.
28
29 =item STRINGs and arrays of STRINGs
30
31 String types are stored as Perl list refs, in the hash element with
32 the same name as the original variable in the file.
33
34 =item Structures
35
36 Structures are stored as hash refs. The elements of the hash may be
37 accessed as values within the hash.
38
39 =item Common blocks
40
41 Variables that are notated as being in a common block are read as
42 normal. Common-block names are collected in the special hash value
43 '+common', which contains a hash each keyword of which is the name of
44 a common block and each value of which is an array of variable names.
45
46 =back
47
48 These things are known to be not working and may one day be fixed:
49
50 =over 3
51
52 =item COMPLEX numbers
53
54 These could be implemented as 2-arrays or as PDL::Complex values, but aren't yet.
55
56 =item PTR types
57
58 These could be implemented as perl refs but currently aren't.
59
60 =item writing
61
62 Maybe one day -- but why bother writing a broken file format? NetCDF is better.
63
64 =back
65
66 These things are known to be not working and will probably never be fixed
67
68 =over 3
69
70 =item Compiled code
71
72 Decompiling IDL code is a violation of the IDL end-user license. To
73 implement this, someone who does not hold an IDL license would have to
74 reverse-engineer a set of .SAV files sent to that person by someone
75 else with an IDL license.
76
77 =item Objects
78
79 IDL objects contain compiled code.
80
81 =back
82
83 =head1 FUNCTIONS
84
85 =cut
86
87 package PDL::IO::IDL;
88
89 BEGIN {
90
91 use Exporter ();
92 package PDL::IO::IDL;
93 @ISA = ( Exporter );
94 @EXPORT_OK = qw( ridl );
95 @EXPORT = @EXPORT_OK;
96 @EXPORT_TAGS = ( Func=>[@EXPORT_OK] );
97
98 our $VERSION = "0.5";
99 $VERSION = eval $VERSION;
100
101 use PDL;
102 use PDL::Exporter;
103 use Carp;
104
105 }
106 use strict;
107
108 =head2 ridl
109
110 =for usage
111
112 $a = ridl("foo.sav");
113
114 =for ref
115
116 Read an IDL save file from a file.
117
118 Upon successful completion, $a is a hash ref containing all of the
119 variables that are present in the save file, indexed by original
120 variable name.
121
122 IDL identifiers are case insensitive; they're all converted to
123 upper-case in the hash that gets returned. This may be adjustable at
124 a future date. Furthermore, because IDL identifiers can't contain
125 special characters, some fields that start with '+' are used to store
126 metadata about the file itself.
127
128 Numeric arrays are stored as PDLs, structures are stored as hashes,
129 and string and structure arrays are stored as perl lists. Named
130 structure types don't exist in perl in the same way that they do in
131 IDL, so named structures are described in the 'structs' field of the
132 global metadata. Anonymous structures are treated as simple hashes.
133 Named structures are also simple hashes, but they also contain a field
134 '+name' that refers to the name of the structure type.
135
136 =cut
137
138
139 sub ridl {
140 my( $name ) = shift;
141
142 STDERR->autoflush(1);
143
144 open(IDLSAV,"<$name") || barf("ridl: Can't open `$name' for reading\n");
145
146 my $hash = read_preamble();
147
148 read_records($hash);
149
150 my @snames = sort keys %{$PDL::IO::IDL::struct_table};
151 @snames = grep(!m/^\+/,@snames);
152 if(@snames) {
153 $hash->{'+structs'}={};
154 local $_;
155 for(@snames) {
156 $hash->{'+structs'}->{$_} =
157 $PDL::IO::IDL::struct_table->{$_}->{'names'};
158 }
159 }
160
161 return $hash;
162 }
163
164
165 ############################################################
166 ##
167 ## Data structure definitions...
168 ##
169 ## This is a list, each element of which contains a description and
170 ## subroutine to read that particular record type.
171 ##
172
173 our $types = [ ['START_MARKER',undef] # 0 (start of SAVE file)
174 ,['COMMON_BLOCK',\&r_com] # 1 (COMMON block definition)
175 ,['VARIABLE',\&r_var] # 2 (Variable data)
176 ,['SYSTEM_VARIABLE',undef] # 3 (System variable data)
177 ,undef # 4 (??)
178 ,undef # 5 (??)
179 ,['END_MARKER',\&r_end] # 6 (End of SAVE file)
180 ,undef # 7 (??)
181 ,undef # 8 (??)
182 ,undef # 9 (??)
183 ,['TIMESTAMP',\&r_ts] # 10 (Timestamp of the save file)
184 ,undef # 11 (??)
185 ,['COMPILED',undef] # 12 (Compiled procedure or func)
186 ,['IDENTIFICATION',undef] # 13 (Author identification)
187 ,['VERSION',\&r_v] # 14 (IDL Version information)
188 ,['HEAP_HEADER',undef] # 15 (Heap index information)
189 ,['HEAP_DATA',undef] # 16 (Heap data)
190 ,['PROMOTE64',\&r_p64] # 17 (Starts 64-bit file offsets)
191 ];
192
193
194 ############################################################
195 ##
196 ## Vtypes -- Representations of IDL scalar variable types.
197 ## The first element is the name, the second element is either a
198 ## perl string (that should be fed to unpack) or a code ref to a
199 ## sub that decodes the type.
200 ##
201
202 our $vtypes = [
203 undef # 0
204 ,["Byte", \&r_byte_pdl, [] ] # 1
205 ,["Short", \&r_n_cast, [long,short] ] # 2
206 ,["Long", \&r_n_pdl, [long] ] # 3
207 ,["Float", \&r_n_pdl, [float] ] # 4
208 ,["Double", \&r_n_pdl, [double] ] # 5
209 ,["Complex", undef ] # 6
210 ,["String", \&r_strvar, [] ] # 7
211 ,["Structure", sub {}, [] ] # 8
212 ,["ComplexDbl",undef ] # 9
213 ,["HeapPtr", undef ] # 10
214 ,["Object", undef ] # 11
215 ,["UShort", \&r_n_cast, [long,ushort] ] # 12
216 ,["ULong", \&r_n_pdl, [long] ] # 13
217 ,["LongLong", undef ] # 14
218 ,["ULongLong", undef ] # 15
219 ];
220
221
222 ###
223 # Cheesy way to check if 64-bit is OK
224 our $quad_ok = eval { my @a = unpack "q","00000001"; $a[0]; };
225
226 ### Initialized in read_preamble.
227 our $little_endian;
228 our $swab;
229 our $p64;
230
231
232 ##############################
233 #
234 # read_preamble
235 #
236 # Reads the preamble of a file and returns the preamble as a hash
237 # ref. In case of failure, it barfs. Also initializes the structure table.
238 #
239
240 sub read_preamble {
241 my $buf;
242 my $out;
243
244 sysread(IDLSAV,$buf,4) || barf ("PDL::IO::IDL: Couldn't read preamble\n");
245 my @sig = unpack("a2S",$buf);
246
247 barf("PDL::IO::IDL: This isn't an IDL save file (wrong magic)\n")
248 if($sig[0] ne 'SR');
249
250 if($sig[1] == 1024 || $sig[1] == 4) {
251 $little_endian = ($sig[1] == 1024);
252 } else {
253 barf "Unrecognized IDL save file type\n";
254 }
255
256 $swab = $little_endian;
257
258 $p64 = 0;
259
260 $PDL::IO::IDL::struct_table = {};
261
262 return {"+meta"=>{}};
263 }
264
265 ##############################
266 #
267 # read_records
268 #
269 # Reads all the records of the file. Splits out into several other
270 # types of record reader...
271 #
272 #
273
274 sub read_records {
275 my $hash = shift;
276 my ($buf, $tbuf);
277
278 my $retval;
279
280 my %nexts;
281 my $tag_count = 0;
282 do {
283
284 ### Read header of the record
285
286 sysread(IDLSAV, $tbuf, 4) || barf("PDL::IO::IDL: unexpected EOF\n");
287 my $type = unpack "N",$tbuf;
288
289 ### Record the next seek location
290 ### (and discard 8 more bytes)
291
292 my $next;
293 if($p64) {
294 print "Reading 64-bit location..." if($PDL::debug);
295 sysread(IDLSAV,$buf,8 + 8);
296 my @next = unpack "NN",$buf;
297 $next = $next[1] + 2**32 * $next[0];
298 } else {
299 print "Reading 32-bit location..." if($PDL::debug);
300 sysread(IDLSAV,$buf,4 + 8);
301 $next = unpack "N",$buf;
302 }
303 print "$next\n" if($PDL::debug);
304
305 ###
306 ### Infinite-loop detector
307 ###
308
309 barf("Repeat index finder was activated! This is a bug or a problem with your file.\n")
310 if($nexts{$next}) ;
311 $nexts{$next} = 1;
312
313 ###
314 ### Call the appropriate handling routine
315 ###
316
317 $retval = 1;
318
319 if(defined $types->[$type]) {
320 if(defined ($types->[$type]->[1])) {
321 print "Found record of type $types->[$type]->[0]...\n" if($PDL::debug || $PDL::IO::IDL::test);
322 $retval = &{$types->[$type]->[1]}($hash);
323 print "OK.\n" if($PDL::debug);
324 } else {
325 print STDERR "Ignoring record of type ".$types->[$type]->[0]." - not implemented.\n";
326 }
327 } else {
328 print STDERR "\nIgnoring record of unknown type $type - not implemented.\n";
329 }
330 print "Seeking $next ($tag_count tags read so far...)\n" if($PDL::debug || $PDL::IO::IDL::test);
331 $tag_count++;
332 sysseek(IDLSAV, $next, 0);
333 $FOO::hash = $hash;
334 } while($retval);
335
336 }
337
338
339
340
341 ##############################
342 # r_com
343 #
344 # Jumptable entry for the COMMONBLOCK keyword -- this loads
345 # the variable names that belong in the COMMON block into a
346 # metavariable.
347
348 sub r_com {
349 my $hash = shift;
350 my $buf;
351
352 sysread(IDLSAV,$buf,4);
353 my $nvars = unpack "N",$buf;
354
355 my $name = r_string();
356 $hash->{"+common"}->{$name} = [];
357
358 for my $i(1..$nvars) {
359 push(@{$hash->{"+common"}->{$name}},r_string());
360 }
361
362 return 1;
363 }
364
365
366
367
368 ##############################
369 # r_end
370 #
371 # Jumptable entry for the END TABLE keyword -- just return 0.
372
373 sub r_end { 0; }
374
375
376 ##############################
377 # r_ts
378 #
379 # TIMESTAMP record handler
380 #
381 sub r_ts {
382 my $hash = shift;
383 my $buf;
384
385 ### Read and discard a LONARR(258) -- why? I don't know.
386 sysread(IDLSAV,$buf,1024);
387 $hash->{"+meta"}->{t_date} = r_string();
388 $hash->{"+meta"}->{t_user} = r_string();
389 $hash->{"+meta"}->{t_host} = r_string();
390
391 return 1;
392 }
393
394
395
396 ##############################
397 # r_version
398 #
399 # VERSION record handler
400 #
401 sub r_v {
402 my $hash = shift;
403 my $buf;
404 my $version;
405
406 sysread(IDLSAV,$buf,4);
407 $version = $hash->{"+meta"}->{v_fmt} = unpack "N",$buf;
408
409 # barf("Unknown IDL save file version ".$version)
410 print STDERR "Warning: IDL file is v$version (neither 5 nor 6); winging it. Check results!\n"
411 if($version != 5 && $version != 6);
412
413 $hash->{"+meta"}->{v_arch} = r_string();
414 $hash->{"+meta"}->{v_os} = r_string();
415 $hash->{"+meta"}->{v_release} = r_string();
416 return 1;
417 }
418
419 ##############################
420 # r_p64
421 sub r_p64 {
422 my $hash = shift;
423 $p64 = 1;
424 }
425
426 ##############################
427 # r_var
428 #
429 # VARIABLE reader - parse a single variable out of a VARIABLE record.
430 #
431
432 sub r_var {
433 my $hash = shift;
434
435 ### Read in the variable name
436 my $name = r_string();
437
438
439 ### Read in and parse the type
440
441 my $buf;
442 sysread(IDLSAV,$buf,8);
443 my ($type,$flags) = unpack "NN",$buf;
444
445 unless(defined $vtypes->[$type]) {
446 barf("PDL::IO::IDL: Unknown variable type $type");
447 }
448
449 unless(defined $vtypes->[$type]->[1]) {
450 print STDERR "Ignoring variable $name: unsupported type ".$vtypes->[$type]->[0]."\n";
451 return 1;
452 }
453
454 print "Variable $name found (flags is $flags)...\n" if($PDL::debug);
455
456 if((($flags & 4) == 0) and (($flags & 32) == 0)) {
457 print "it's a scalar\n" if($PDL::debug);
458
459
460 sysread(IDLSAV,$buf,4);
461 my($seven) = unpack "N",$buf;
462 if($seven != 7) {
463 print STDERR "Warning: expected data-start key (7) but got $seven, for variable $name\n";
464 }
465
466 ## Scalar case
467 $hash->{$name} =
468 &{$vtypes->[$type]->[1]}
469 ($flags, [], @{$vtypes->[$type]->[2]})
470 } else {
471 ## Array case
472
473 my($arrdesc) = r_arraydesc();
474
475 if(($flags & 32) == 0) {
476
477 ## Simple array case
478 sysread(IDLSAV,$buf,4);
479 my($indicator) = unpack "N",$buf;
480
481 print STDERR "Warning: Reading data from an array but got code $indicator (expected 7)\n"
482 if($indicator != 7);
483
484 print "simple array...type=$type\n" if($PDL::debug);
485
486 my @args= ($flags,[ @{$arrdesc->{dims}}[0..$arrdesc->{ndims}-1]],
487 @{$vtypes->[$type]->[2]});
488 my $pdl = &{$vtypes->[$type]->[1]}(@args);
489 $hash->{$name} = $pdl;
490
491 } else {
492
493 ## Structure case
494 print "structure...\n" if($PDL::debug);
495 my($sname) = r_structdesc();
496
497 my @structs;
498 print "Reading $arrdesc->{nelem} structures....\n" if($PDL::debug || $PDL::IO::IDL::test);
499 my $i;
500
501 {my $buf; sysread(IDLSAV,$buf,4);}
502
503 for ($i=0;$i<$arrdesc->{nelem};$i++) {
504 if($PDL::IO::IDL::test && !($i%100)){
505 print "$i of $arrdesc->{nelem}...\n";
506 }
507
508 push(@structs,r_struct($sname));
509 }
510
511 # Make a multi-dimensional list that contains the structs
512 $hash->{$name} = multi_dimify($arrdesc,\@structs);
513
514 }
515 }
516
517
518 return 1;
519 }
520
521
522 ##############################
523 # multi_dimify
524 #
525 # Take a linear list of items and an array descriptor, and
526 # hand back a multi-dimensional perl list with the correct dimension
527 # according to the descriptor. (This isn't necessary for PDL types,
528 # only for structures and strings).
529 #
530
531 sub multi_dimify {
532
533 my($arrdesc,$structs,$n) = @_;
534
535 return shift @{$structs}
536 if($arrdesc->{ndims} <= $n or
537 $arrdesc->{ndims} == 0 or
538 $arrdesc->{ndims}-$n == 1 && $arrdesc->{dims}->[$n]==1);
539
540
541 if($arrdesc->{ndims} - $n == 1){
542 my @ret = splice @{$structs},0,$arrdesc->{dims}->[$n];
543 return \@ret;
544 }
545
546
547 my $out = [];
548 my $i;
549 for ($i=0;$i<$arrdesc->{dims}->[$n];$i++) {
550 push(@{$out},multi_dimify($arrdesc,$structs,$n+1));
551 }
552
553 return $out;
554 }
555
556
557
558 ######################################################################
559 ######################################################################
560
561
562 #
563 # r_arraydesc - read an array descriptor from the file
564 #
565
566 our $r_arraydesc_table = ['a','b','nbytes','nelem','ndims','c','d','nmax'];
567
568 sub r_arraydesc {
569 my $out = {};
570 my $buf;
571
572 sysread(IDLSAV,$buf,4*8);
573
574 my(@vals) = unpack("N"x8,$buf);
575 print STDERR "r_arraydesc_table: vals[0]=".$vals[0]." (should be 8)\n"
576 if($vals[0] != 8);
577 for my $i(0..7) {
578 $out->{$r_arraydesc_table->[$i]} = $vals[$i];
579 }
580 my $nmax = $vals[7];
581 my $nelem = $vals[3];
582
583 sysread(IDLSAV,$buf,$nmax*4);
584 $out->{dims} = [unpack("N"x$nmax,$buf)];
585 my $dims = pdl(@{$out->{dims}});
586
587 $out->{pdldims} = $dims;
588
589 print STDERR "PDL::IO::IDL: Inconsistent array dimensions in variable (nelem=$nelem, dims=".join("x",@{$out->{dims}}).")"
590 if($nelem != $dims->prod);
591
592 $out;
593 }
594
595
596 ##############################
597 #
598 # r_structdesc reads a structure description and stores it in the struct_table.
599 # You get back the name of the structure.
600 #
601
602 sub r_structdesc {
603 my $buf;
604
605 print "Reading a structure description...\n" if($PDL::IO::IDL::test);
606
607 sysread(IDLSAV,$buf,4); # Discard initial long (value=9) from descriptor
608 my($name) = r_string(); # Have to store structures in the structure table.
609 $name =~ s/\s//g;
610
611 $name = "+anon".scalar(keys %{$PDL::IO::IDL::struct_table})
612 if($name eq '');
613
614 sysread(IDLSAV,$buf,4*3);
615 my($predef,$ntags,$nbytes) = unpack("N"x3,$buf);
616 print "predef=$predef,ntags=$ntags,nbytes=$nbytes\n" if($PDL::debug);
617 if(!($predef & 1)) {
618 my $i;
619 print "not predefined. ntags=$ntags..\n" if($PDL::debug || $PDL::IO::IDL::test);
620
621 my $st = $PDL::IO::IDL::struct_table->{$name} = {
622 "ntags" => $ntags
623 ,"nbytes"=> $nbytes
624 ,"names" => []
625 ,"arrays" => []
626 ,"structs" => []
627 };
628
629 ### Read tag descriptors.
630 sysread(IDLSAV,$buf,3*4*$ntags);
631 $st->{descrip} = [(unpack "N"x(3*$ntags), $buf)];
632
633
634 print "ntags is $ntags\n" if($PDL::debug || $PDL::IO::IDL::test);
635 ### Read tag names.
636 for $i(0..$ntags-1) {
637 push(@{$st->{names}},r_string());
638 }
639
640 ### Search for nested arrays & structures
641 my ($nstructs,$narrays);
642
643 for $i(0..$ntags-1) {
644 my $a = $st->{descrip}->[$i*3+2];
645
646 $nstructs++ if($a & 32);
647 $narrays++ if($a & 38);
648 }
649
650 print "narrays=$narrays\n" if($PDL::debug || $PDL::IO::IDL::test);
651 for $i(0..($narrays-1)) {
652 push( @{$st->{arrays}}, r_arraydesc() );
653 }
654
655 print "nstructs=$nstructs\n" if($PDL::debug || $PDL::IO::IDL::test);
656 for $i(0..($nstructs-1)) {
657 push( @{$st->{structs}}, r_structdesc() );
658 }
659
660 }
661 print "finished with structure desc...\n" if($PDL::IO::IDL::test);
662 return $name;
663 }
664
665 ##############################
666 #
667 # r_struct
668 #
669 # Given the name of a structure type, read in exactly one of them.
670 # If I were smarter, this would be the same code as the variable
671 # reader, but I'm not so it's only similar.
672 #
673 our $r_struct_recursion = 0;
674
675 sub r_struct {
676 my($sname) = shift;
677
678
679 print ("_ "x$r_struct_recursion) . "Reading a structure...\n" if($PDL::IO::IDL::test);
680 my $zz=$r_struct_recursion;
681 local($r_struct_recursion) = $zz++;
682
683 # Get the structure descriptor from the table.
684 my($sd) = $PDL::IO::IDL::struct_table->{$sname};
685 barf "Unknown structure type $sname" unless defined($sd);
686
687 # Initialize the structure itself and the array and structure indices.
688 my($struct) = {};
689 $struct->{'+name'} = $sname unless($sname =~ m/^\+/);
690
691 my($array_no, $struct_no);
692
693 # Loop over tags and snarf each one
694 my($i);
695 for($i=0;$i<$sd->{ntags};$i++) {
696 my($name) = $sd->{names}->[$i];
697
698 my($type) = $sd->{descrip}->[$i*3+1];
699 my($flags) = $sd->{descrip}->[$i*3+2];
700
701 print "reading tag #$i ($sd->{names}->[$i])\n" if($PDL::debug);
702
703 barf("PDL::IO::IDL: Unknown variable type $type in structure")
704 unless defined($vtypes->[$type]);
705
706 unless(defined($vtypes->[$type]->[1])) {
707 print "Skipping tag $name in structure - unsupported type ".$vtypes->[$type]->[0]."\n";
708 $array_no++ if($flags & 38);
709 $struct_no++ if($flags & 32);
710
711 } else {
712
713 if( (($flags & 4)==0) and (($flags & 32)==0) ) {
714 ## Scalar tag case
715 $struct->{$name} = &{$vtypes->[$type]->[1]}
716 ($flags, [], @{$vtypes->[$type]->[2]});
717 } else {
718
719 ### Array and/or structure case ###
720
721 my($arrdesc) = $sd->{arrays}->[$array_no++];
722 # sysread(IDLSAV,my $buf,4); # skip indicator
723
724 if(($flags & 32) == 0) {
725
726 ### Tag is a simple array ###
727
728 my @args = ($flags,[ @{$arrdesc->{dims}}[0..$arrdesc->{ndims}-1]],
729 @{$vtypes->[$type]->[2]});
730 my $pdl = &{$vtypes->[$type]->[1]}(@args);
731 print " pdl is $pdl\n" if($PDL::debug);
732 $struct->{$name} = $pdl;
733
734 } else {
735
736 ### Tag is a structure ###
737
738 my $tsname = $sd->{structs}->[$struct_no++];
739 my @structs = ();
740 for $i(1..$arrdesc->{nelem}) {
741 push(@structs,r_struct($tsname));
742 }
743
744 $struct->{$name} = multi_dimify($arrdesc,\@structs);
745
746 }
747 }
748 }
749 } # end of ntags loop
750
751 return $struct;
752 }
753
754
755
756
757 ##############################
758 #
759 # r_string
760 #
761 # Reads a string value, leaving the file pointer correctly aligned
762 # on a 32-bit boundary (if it started that way). Returns the string as
763 # a perl scalar.
764 #
765 sub r_string{
766 my ($buf,$foo);
767 sysread(IDLSAV, $buf, 4); # Read the length...
768
769 my ($len) = unpack "N",$buf;
770 # Pad the length out to the next 32-bit boundary
771
772 my $plen = $len - ($len % -4) ;
773
774 sysread(IDLSAV,$buf,$plen);
775 return unpack "A$len",$buf;
776 }
777
778
779 ##############################
780 #
781 # r_strvar
782 #
783 # Reads a string variable (different than r_string because
784 # of the extra length duplication in the IDL file...)
785 #
786 sub r_strvar {
787 my $buf;
788 my $flags = shift;
789 sysread(IDLSAV,$buf,4);
790 return r_string();
791 }
792
793 ##############################
794 #
795 # r_byte_pdl
796 #
797 # Reads a byte PDL (stored as a strvar)
798 #
799 sub r_byte_pdl {
800 my($flags,$dims) = @_;
801
802 sysread(IDLSAV,my $buf,4)
803 if($#$dims > 1);
804
805 $a = r_string();
806
807 my $pdl = new PDL;
808 $pdl->set_datatype(byte->enum);
809 $pdl->setdims($dims);
810 ${ $pdl->get_dataref() } = $a;
811 $pdl->upd_data;
812
813 $pdl;
814 }
815
816 ##############################
817 #
818 # r_n_pdl
819 #
820 # Reads <n> normal integer-type numerical values as a pdl.
821 # You feed in the dimlist and type, you get back the
822 # final pdl. The read is padded to the nearest word boundary.
823 #
824
825 sub r_n_pdl {
826 my($flags,$dims,$type) = @_;
827
828 my $nelem = pdl($dims)->prod;
829 my $dsize = PDL::Core::howbig($type);
830 my $hunksize = $dsize * $nelem;
831
832 my $pdl = PDL->new_from_specification($type,@$dims);
833 my $dref = $pdl->get_dataref();
834
835 my $len = sysread(IDLSAV, $$dref, $hunksize - ($hunksize % -4) );
836 $pdl->upd_data;
837
838 print "bytes were ",join(",",unpack "C"x($hunksize-($hunksize%-4)),$$dref),"\n" if($PDL::debug);
839
840
841 if($swab) {
842 bswap2($pdl) if($dsize==2);
843 bswap4($pdl) if($dsize==4);
844 bswap8($pdl) if($dsize==8);
845 }
846
847 $pdl;
848 }
849
850 sub r_n_cast {
851 my($flags,$dims,$type1,$type2) = @_;
852
853 (r_n_pdl($flags,$dims,$type1))->convert($type2);
854 }
855
856
857 =head1 AUTHOR, LICENSE, NO WARRANTY
858
859 THIS CODE IS PROVIDED WITH NO WARRANTY and may be distributed and/or
860 modified under the same terms as PDL itself.
861
862 This code is based on the analysis of the IDL save file format
863 published by Craig Markwardt in 2002.
864
865 IDL is a trademark of Research Systems Incorporated (RSI). The PDL
866 development team, and authors of this code, are not affiliated with RSI.
867
868 =cut
869
870 1;