blib/lib/PDL/IO/FlexRaw.pm

filter: Statementstmt 73.8% 251 / 340 Branchbran 48.4% 122 / 252 Conditioncond 39.1% 36 / 92 MC/DCmcdc 12.9% 7 / 54 Subroutinesub 93.7% 15 / 16 Podpod 62.5% 5 / 8 totaltotal 57.2% 436 / 762 CC 167 SCAR 52.8
CC
167
Cov
60%
readflex
494.1
mapflex
144.5
_read_flexhdr
96.9
CRAP 196.9
« lib/PDL/Core/Dev.pm ↑ Summary blib/lib/PDL/Doc/Perldl.pm »
1 =head1 NAME
2  
3 PDL::IO::FlexRaw -- A flexible binary I/O format for PerlDL
4  
5 =head1 SYNOPSIS
6  
7 use PDL;
8 use PDL::IO::FlexRaw;
9  
10 # To obtain the header for reading (if multiple files use the
11 # same header, for example):
12 #
13 $hdr = PDL::IO::FlexRaw::_read_flexhdr("filename.hdr")
14  
15 ($x,$y,...) = readflex("filename" [, $hdr])
16 ($x,$y,...) = mapflex("filename" [, $hdr] [, $opts])
17  
18 $hdr = writeflex($file, $pdl1, $pdl2,...)
19 writeflexhdr($file, $hdr)
20  
21 # if $PDL::IO::FlexRaw::writeflexhdr is true and
22 # $file is a filename, writeflexhdr() is called automatically
23 #
24 $hdr = writeflex($file, $pdl1, $pdl2,...) # need $hdr for something
25 writeflex($file, $pdl1, $pdl2,...) # ..if $hdr not needed
26  
27 =head1 DESCRIPTION
28  
29 FlexRaw is a generic method for the input and output of `raw' data
30 arrays. In particular, it is designed to read output from FORTRAN 77
31 UNFORMATTED files and the low-level C write function, even if the
32 files are compressed or gzipped. As in FastRaw, the data file is
33 supplemented by a header file (although this can be replaced by the
34 optional C<$hdr> argument). More information can be included in the
35 header file than for FastRaw -- the description can be extended to
36 several data objects within a single input file.
37  
38 For example, to read the output of a FORTRAN program
39  
40 real*4 a(4,600,600)
41 open (8,file='banana',status='new',form='unformatted')
42 write (8) a
43 close (8)
44  
45 the header file (`banana.hdr') could look like
46  
47 # FlexRaw file header
48 # Header word for F77 form=unformatted
49 Byte 1 4
50 # Data
51 Float 3 # this is ignored
52 4 600 600
53 Byte 1 4 As is this, as we've got all dims
54  
55 The data can then be input using
56  
57 $x = (readflex('banana'))[1];
58  
59 The format of the hdr file is an extension of that used by FastRaw.
60 Comment lines (starting with #) are allowed, as are descriptive names
61 (as elsewhere: byte, short, ushort, long, float, double) for the data
62 types -- note that case is ignored by FlexRaw. After the type, one
63 integer specifies the number of dimensions of the data `chunk', and
64 subsequent integers the size of each dimension. So the specifier
65 above (`Float 3 4 600 600') describes our FORTRAN array. A scalar can
66 be described as `float 0' (or `float 1 1', or `float 2 1 1', etc.).
67  
68 When all the dimensions are read -- or a # appears after whitespace --
69 the rest of the current input line is ignored, I<unless> badvalues
70 are being read or written. In that case, the next token will be the
71 string C<badvalue> followed by the bad value used, if needed.
72  
73 What about the extra 4 bytes at the head and tail, which we just threw
74 away? These are added by FORTRAN (at least on Suns, Alphas and
75 Linux), and specify the number of bytes written by each WRITE -- the
76 same number is put at the start and the end of each chunk of data.
77 You I<may> need to know all this in some cases. In general, FlexRaw
78 tries to handle it itself, if you simply add a line saying `f77' to
79 the header file, I<before> any data specifiers:
80  
81 # FlexRaw file header for F77 form=unformatted
82 F77
83 # Data
84 Float 3
85 4 600 600
86  
87 -- the redundancy in FORTRAN data files even allows FlexRaw to
88 automatically deal with files written on other machines which use
89 back-to-front byte ordering. This won't always work -- it's a 1 in 4
90 billion chance it won't, even if you regularly read 4Gb files! Also,
91 it currently doesn't work for compressed files, so you can say `swap'
92 (again before any data specifiers) to make certain the byte order is
93 swapped.
94  
95 The optional C<$hdr> argument allows the use of an anonymous array to
96 give header information, rather than using a .hdr file. For example,
97  
98 $header = [
99 {Type => 'f77'},
100 {Type => 'float', NDims => 3, Dims => [ 4,600,600 ] }
101 ];
102 @a = readflex('banana',$header);
103  
104 reads our example file again. As a special case, when NDims is 1, Dims
105 may be given as a scalar.
106  
107 The highest dimension can be given as C<undef>, which will read as many
108 frames as possible of the given size (but only if only one hash-ref is given):
109  
110 $video = readflex('frames.raw', [
111 { Type=>'byte', NDims=>4, Dims=>[4,640,480,undef] },
112 ]);
113  
114 Within PDL, readflex and writeflex can be used to write several pdls
115 to a single file -- e.g.
116  
117 use PDL;
118 use PDL::IO::FlexRaw;
119  
120 @pdls = ($pdl1, $pdl2, ...);
121 $hdr = writeflex("fname",@pdls);
122 @pdl2 = readflex("fname",$hdr);
123  
124 writeflexhdr("fname",$hdr); # not needed if $PDL::IO::FlexRaw::writeflexhdr is set
125 @pdl3 = readflex("fname");
126  
127 -- C<writeflex> produces the data file and returns the file header as an
128 anonymous hash, which can be written to a .hdr file using
129 C<writeflexhdr>.
130  
131 If the package variable C<$PDL::IO::FlexRaw::writeflexhdr>
132 is true, and the C<writeflex> call was with a I<filename> and not
133 a handle, C<writeflexhdr> will be called automatically (as done by
134 C<writefraw>.
135  
136 The reading of compressed data is switched on automatically if the
137 filename requested ends in .gz or .Z, or if the originally specified
138 filename does not exist, but one of these compressed forms does.
139  
140 If C<writeflex> and C<readflex> are given a reference to a
141 file handle as a first parameter instead of a filename, then
142 the data is read or written to the open filehandle. This
143 gives an easy way to read an arbitrary slice in a big data
144 volume, as in the following example:
145  
146 use PDL;
147 use PDL::IO::FastRaw;
148  
149 open(DATA, "raw3d.dat");
150 binmode(DATA);
151  
152 # assume we know the data size from an external source
153 ($width, $height, $data_size) = (256,256, 4);
154  
155 my $slice_num = 64; # slice to look at
156 # Seek to slice
157 seek(DATA, $width*$height*$data_size * $slice_num, 0);
158 $pdl = readflex \*DATA, [{Dims=>[$width, $height], Type=>'long'}];
159  
160 WARNING: In later versions of perl (5.8 and up) you must
161 be sure that your file is in "raw" mode (see the perlfunc
162 man page entry for "binmode", for details). Both readflex
163 and writeflex automagically switch the file to raw mode for
164 you -- but in code like the snippet above, you could end up
165 seeking the wrong byte if you forget to make the binmode() call.
166  
167 C<mapflex> memory maps, rather than reads, the data files. Its interface
168 is similar to C<readflex>. Extra options specify if the data is to be
169 loaded `ReadOnly', if the data file is to be `Creat'-ed anew on the
170 basis of the header information or `Trunc'-ated to the length of the
171 data read. The extra speed of access brings with it some limitations:
172 C<mapflex> won't read compressed data, auto-detect f77 files, or read f77
173 files written by more than a single unformatted write statement. More
174 seriously, data alignment constraints mean that C<mapflex> cannot read
175 some files, depending on the requirements of the host OS (it may also
176 vary depending on the setting of the `uac' flag on any given machine).
177 You may have run into similar problems with common blocks in FORTRAN.
178  
179 For instance, floating point numbers may have to align on 4 byte
180 boundaries -- if the data file consists of 3 bytes then a float, it
181 cannot be read. C<mapflex> will warn about this problem when it occurs,
182 and return the PDLs mapped before the problem arose. This can be
183 dealt with either by reorganizing the data file (large types first
184 helps, as a rule-of-thumb), or more simply by using C<readflex>.
185  
186 =head2 Fortran code to create data
187  
188 Until PDL 2.099, the test file F<t/flexraw_fortran.t> compiled a
189 Fortran program, ran it, then byte-swapped its output, to test this
190 module's ability to do that. Version 2.099 has dropped external
191 dependencies, including the use of Fortran. The code it used is
192 shown here for historical curiosity:
193  
194 c Program to test i/o of F77 unformatted files
195 program rawtest
196 implicit none
197 integer i
198 $f77type a($ndata)
199 do i = 1, $ndata
200 a(i) = $val
201 enddo
202 open(8,file=
203 \$'$data'
204 \$,status='new',form='unformatted')
205 i = $ndata
206 write (8) i
207 write (8) a
208 close(8)
209 end
210  
211 with this FlexRaw header:
212  
213 # FlexRaw file header
214 f77
215 long 1 1
216 # Data
217 $pdltype 1 $ndata
218  
219 C<$ndata> was set to 10, C<$val> was C<100.*sin(0.01* i)>, C<$data>
220 was a filename. C<$f77type> was set to C<real*4> and C<real*8>.
221  
222 There was also a more complex program:
223  
224 c Program to test i/o of F77 unformatted files
225 program rawtest
226 implicit none
227 character a
228 integer*2 i
229 integer*4 l
230 real*4 f
231 real*8 d
232 d = 4*atan(1.)
233 f = d
234 l = 10**d
235 i = l
236 a = ' '
237 open(8,file=
238 \$'$data'
239 \$,status='new',form='unformatted')
240 c Choose bad boundaries...
241 write (8) a,i,l,f,d
242 close(8)
243 end
244  
245 with this FlexRaw header:
246  
247 # FlexRaw file header
248 byte 1 4
249 byte 0
250 short 0
251 long 0
252 float 0
253 double 0
254 byte 1 4
255  
256 =head1 FUNCTIONS
257  
258 =cut
259  
260 package PDL::IO::FlexRaw;
261 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
262 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
263 2 ▶use PDL;
Subroutine BEGIN: called CC=1 SCAR=0.0
264 2 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
265 2 ▶use PDL::Types ':All';
Subroutine BEGIN: called CC=1 SCAR=0.0
266 2 ▶use PDL::IO::Misc qw(bswap4);
Subroutine BEGIN: called CC=1 SCAR=0.0
267  
268 2 our @ISA = qw/Exporter/;
269 2 our @EXPORT = qw/writeflex writeflexhdr readflex mapflex glueflex/;
270  
271 # Cast type numbers in concrete, for external file's sake...
272 2 my %flexnames = map +($_->enum => $_->ioname), types();
273 2 my %flextypes = map +($_->ioname => $_->enum,
274 $_->enum => $_->enum,
275 $_->ppsym => $_->enum,
276 ), types();
277 my %flexswap = map {
278 2 my $nb = PDL::Core::howbig(my $val = $_->enum);
279 30 ▶ $nb > 1 ? ($val => "bswap$nb") : ()
Branch: 1 branch
BranchTrueFalseTotal
$nb > 1 ? : 26 4 30
280 } types();
281  
282 2 our $verbose = 0;
283 2 ▶our $writeflexhdr //= 0;
Condition50%1/2
our $writeflexhdr //= 0
l!l
02
MC/DC0%0/1
our $writeflexhdr //= 0
our $writeflexhdr
Truth table50%1/2
A
A= our $writeflexhdr
Aresult
11
00
284  
285 24 ▶sub _read_flexhdr {
Subroutine _read_flexhdr: called CC=34 SCAR=45.7
286 24 my ($hname) = @_;
287 24 ▶ open my $hfile, $hname or barf "Couldn't open '$hname' for reading: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $hfile, $hname or barf("Couldn't open '${hname}' for reading: $!") 24 0 24
288 24 binmode $hfile;
289 24 my ($newfile, $tid, @str, @ret) = 1;
290 # check for ENVI files and bail (for now)
291 24 my $line1 = scalar <$hfile>;
292 24 ▶ barf "This is an ENVI format file, please use readenvi()\n" if $line1 =~ /^ENVI\r?$/;
Branch: 1 branch
BranchTrueFalseTotal
if $line1 =~ /^ENVI\r?$/ 0 24 24
293 24 seek $hfile, 0, 0; # reset file pointer to beginning
294 24 ▶ ITEM: while (!eof($hfile)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eof $hfile) 52 0 52
295 52 my ($ndims, $mode, @dims) = (-1, -2);
296 52 my ($have_badvalue) = undef;
297 52 my ($badvalue) = undef;
298 52 ▶ LINE: while (<$hfile>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $hfile)) 136 24 160
299 136 ▶ next LINE if /^#/ or /^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if /^#/ or /^\s*$/ 52 84 136
Condition100%3/3
/^#/ or /^\s*$/
l!l&&r!l&&!r
242884
MC/DC100%2/2
/^#/ or /^\s*$/
/^#/ /^\s*$/
Truth table100%3/3
A or B
A= /^#/B= /^\s*$/
ABresult
1X1
011
000
300 84 chop;
301 84 tr/A-Z/a-z/;
302 84 @str = split;
303 84 my $numtokens = scalar @str;
304 84 TOKEN: foreach my $token (@str) {
305 92 ▶ next LINE if $token =~ /^#/;
Branch: 1 branch
BranchTrueFalseTotal
if $token =~ /^#/ 0 92 92
306 92 ▶ if ($mode == -2) { # type
Branch: 5 branches
BranchTrueFalseTotal
if ($mode == -2) { } 28 64 92
elsif ($mode == -1) { } 28 36 64
elsif ($mode < $ndims) { } 36 0 36
elsif ($mode == $ndims and not $have_badvalue) { } 0 0 0
elsif ($mode == $ndims and $have_badvalue and $numtokens > 0) { } 0 0 0
307 28 ▶ if ($newfile) {
Branch: 1 branch
BranchTrueFalseTotal
if ($newfile) 24 4 28
308 24 ▶ if ($token eq 'f77' || $token eq 'swap') {
Branch: 1 branch
BranchTrueFalseTotal
if ($token eq "f77" or $token eq "swap") 0 24 24
Condition33%1/3
$token eq "f77" or $token eq "swap"
l!l&&r!l&&!r
0024
MC/DC0%0/2
$token eq "f77" or $token eq "swap"
$token eq "f77" $token eq "swap"
Truth table33%1/3
A or B
A= $token eq "f77"B= $token eq "swap"
ABresult
1X1
011
000
309 0 push @ret, {
310 Type => $token
311 };
312 0 $numtokens--;
313 0 next ITEM;
314 }
315 }
316 28 ▶ barf("Bad typename '$token' in readflex") if (!exists($flextypes{$token}));
Branch: 1 branch
BranchTrueFalseTotal
unless exists $flextypes{$token} 0 28 28
317 28 $tid = $flextypes{$token};
318 28 $numtokens--;
319 28 $newfile = 0;
320 28 $mode++;
321 } elsif ($mode == -1) { #ndims
322 28 ▶ barf("Not number for ndims in readflex") if $token !~ /^\d*$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $token =~ /^\d*$/ 0 28 28
323 28 $ndims = $token;
324 28 ▶ barf("Bad ndims in readflex") if ($ndims < 0);
Branch: 1 branch
BranchTrueFalseTotal
if $ndims < 0 0 28 28
325 28 $numtokens--;
326 28 $mode++;
327 28 ▶ last LINE if $mode == $ndims and $numtokens == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $mode == $ndims and $numtokens == 0 0 28 28
Condition33%1/3
$mode == $ndims and $numtokens == 0
!ll&&!rl&&r
2800
MC/DC0%0/2
$mode == $ndims and $numtokens == 0
$mode == $ndims $numtokens == 0
Truth table33%1/3
A and B
A= $mode == $ndimsB= $numtokens == 0
ABresult
0X0
100
111
328 } elsif ($mode < $ndims) { # get dims
329 36 ▶ barf("Not number for dimension in readflex")
Branch: 1 branch
BranchTrueFalseTotal
unless $token =~ /^\d*$/ 0 36 36
330 if $token !~ /^\d*$/;
331 36 push(@dims,$token);
332 36 $numtokens--;
333 36 $mode++;
334 36 ▶ last LINE if $mode == $ndims and $numtokens == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $mode == $ndims and $numtokens == 0 28 8 36
Condition66%2/3
$mode == $ndims and $numtokens == 0
!ll&&!rl&&r
8028
MC/DC50%1/2
$mode == $ndims and $numtokens == 0
$mode == $ndims $numtokens == 0
Truth table66%2/3
A and B
A= $mode == $ndimsB= $numtokens == 0
ABresult
0X0
100
111
335 ▶ } elsif ($mode == $ndims and ! $have_badvalue) { # check for badvalue info
Condition0%0/3
$mode == $ndims and not $have_badvalue
!ll&&!rl&&r
000
MC/DC0%0/2
$mode == $ndims and not $have_badvalue
$mode == $ndims not $have_badvalue
Truth table0%0/3
A and not(B)
A= $mode == $ndimsB= not $have_badvalue
ABresult
0X0
100
111
336 0 ▶ if ($token =~ /^badvalue$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token =~ /^badvalue$/) { } 0 0 0
337 0 $have_badvalue = 1;
338 0 $numtokens--;
339 0 ▶ last LINE if $numtokens==0; # using default bad value
Branch: 1 branch
BranchTrueFalseTotal
if $numtokens == 0 0 0 0
340 } else {
341 0 last LINE;
342 }
343 ▶ } elsif ($mode == $ndims and $have_badvalue and $numtokens > 0) {
Condition0%0/6
$mode == $ndims and $have_badvalue and $numtokens > 0
!ll&&!rl&&r
000
$mode == $ndims and $have_badvalue
!ll&&!rl&&r
000
MC/DC0%0/3
$mode == $ndims and $have_badvalue and $numtokens > 0
$mode == $ndims $have_badvalue $numtokens > 0
Truth table0%0/4
A and B and C
A= $mode == $ndimsB= $have_badvalueC= $numtokens > 0
ABCresult
0XX0
10X0
1100
1111
344 0 $badvalue = $token;
345 0 last LINE;
346 }
347 }
348 }
349 52 ▶ last ITEM if $mode == -2;
Branch: 1 branch
BranchTrueFalseTotal
if $mode == -2 24 28 52
350 28 ▶ barf "Bad format in readflex header file ($ndims, $mode)"
Branch: 1 branch
BranchTrueFalseTotal
if $ndims < 0 or $mode != $ndims 0 28 28
Condition33%1/3
$ndims < 0 or $mode != $ndims
l!l&&r!l&&!r
0028
MC/DC0%0/2
$ndims < 0 or $mode != $ndims
$ndims < 0 $mode != $ndims
Truth table33%1/3
A or B
A= $ndims < 0B= $mode != $ndims
ABresult
1X1
011
000
351 if $ndims < 0 || $mode != $ndims;
352 28 ▶ push @ret, {
Branch: 1 branch
BranchTrueFalseTotal
$have_badvalue ? : 0 28 28
353 Type => $tid,
354 Dims => \@dims,
355 NDims => $ndims,
356 BadFlag => (($have_badvalue) ? 1 : 0),
357 BadValue => $badvalue,
358 };
359 }
360 24 return \@ret;
361 }
362  
363 27 ▶sub readchunk {
Subroutine readchunk: called CC=3 SCAR=11.2
Pod undocumented
364 27 my ($d, $pdl, $len, $name, $offset) = @_;
365 27 my ($nread);
366 27 ▶ print "Reading $len at $offset from $name\n"
Branch: 1 branch
BranchTrueFalseTotal
if $PDL::IO::FlexRaw::verbose 0 27 27
367 if $verbose;
368 27 ▶ ($nread = read($d, ${$pdl->get_dataref}, $len)) == $len
Branch: 1 branch
BranchTrueFalseTotal
($nread = read($d, ${$pdl->get_dataref;}, $len)) == $len or barf("Couldn't read $len bytes at offset $offset from '${name}', got $nread") 27 0 27
369 or barf "Couldn't read $len bytes at offset $offset from '$name', got $nread";
370 27 $pdl->upd_data();
371 27 $len;
372 }
373  
374 2 our $flexmapok;
375 0 ▶sub myhandler {
Subroutine myhandler: not called CC=1 SCAR=6.9
Pod undocumented
376 0 $flexmapok = 0;
377 0 barf "Data out of alignment, can't map further\n";
378 }
379 4 ▶sub mapchunk {
Subroutine mapchunk: called CC=4 SCAR=14.1
Pod undocumented
380 4 my ($svref, $type, $dims, $offset, $len) = @_;
381 4 my $pdl = PDL->new_around_datasv(0+$svref, $offset);
382 4 $pdl->set_datatype($type);
383 4 ▶ $pdl->setdims(ref $dims ? $dims : [$dims]);
Branch: 1 branch
BranchTrueFalseTotal
ref $dims ? : 4 0 4
384 4 $pdl->set_donttouchdata($len);
385 4 local $flexmapok=1;
386 4 ▶ local $SIG{BUS} = \&myhandler unless $^O =~ /MSWin32/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $^O =~ /MSWin32/i 4 0 4
387 4 local $SIG{FPE} = \&myhandler;
388 4 eval {$pdl->at((0) x $pdl->ndims)}; # "->flat" allocs copy of whole array
389 4 ▶ $flexmapok ? $pdl : undef;
Branch: 1 branch
BranchTrueFalseTotal
$PDL::IO::FlexRaw::flexmapok ? : 4 0 4
390 }
391  
392 =head2 glueflex
393  
394 =for ref
395  
396 Append a single data item to an existing binary file written by
397 L</writeflex>. Must be to the last data item in that file. Error if
398 dims not compatible with existing data.
399  
400 =for usage
401  
402 $hdr = glueflex($file, $pdl[, $hdr]); # or
403 $hdr = glueflex(FILEHANDLE, $pdl[, $hdr]);
404 # now you must call writeflexhdr()
405 writeflexhdr($file, $hdr);
406  
407 or
408  
409 $PDL::IO::FlexRaw::writeflexhdr = 1; # set so we don't have to call writeflexhdr
410 $hdr = glueflex($file, $pdl[, $hdr]) # remember, $file must be filename
411 glueflex($file, $pdl[, $hdr]) # remember, $file must be filename
412  
413 =cut
414  
415 1 ▶sub glueflex {
Subroutine glueflex: called CC=18 SCAR=37.1
Pod documented
416 1 my $usage = 'Usage $hdr = glueflex("filename"|FILEHANDLE,$pdl[,$hdr])';
417 1 my ($name,$pdl,$hdr) = @_;
418 1 ▶ barf $usage if @_ < 2 or @_ > 3 or !UNIVERSAL::isa($pdl, 'PDL');
Branch: 1 branch
BranchTrueFalseTotal
if @_ < 2 or @_ > 3 or not UNIVERSAL::isa($pdl, "PDL") 0 1 1
Condition33%2/6
@_ < 2 or @_ > 3 or not UNIVERSAL::isa($pdl, "PDL")
l!l&&r!l&&!r
001
@_ < 2 or @_ > 3
l!l&&r!l&&!r
001
MC/DC0%0/3
@_ < 2 or @_ > 3 or not UNIVERSAL::isa($pdl, "PDL")
@_ < 2 @_ > 3 not UNIVERSAL::isa($pdl, "PDL")
Truth table25%1/4
A or B or not(C)
A= @_ < 2B= @_ > 3C= not UNIVERSAL::isa($pdl, "PDL")
ABCresult
1XX1
01X1
0011
0000
419 1 my $isname = 0;
420 1 my $d;
421 # Test if $name is a file handle
422 1 ▶ if (defined fileno($name)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined fileno $name) { } 0 1 1
423 0 $d = $name;
424 } else {
425 1 ▶ barf $usage if ref $name;
Branch: 1 branch
BranchTrueFalseTotal
if ref $name 0 1 1
426 1 ▶ barf "'$name' must be real filename: $!" if !-f $name;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $name 0 1 1
427 1 $isname = 1;
428 1 ▶ open $d, '>>', $name or barf "Couldn't open '$name' for appending: $!";
Branch: 1 branch
BranchTrueFalseTotal
open $d, ">>", $name or barf("Couldn't open '${name}' for appending: $!") 1 0 1
429 }
430 1 binmode $d;
431 1 ▶ $hdr ||= _read_flexhdr("$name.hdr");
Condition33%1/3
$hdr ||= _read_flexhdr("$name.hdr")
l!l&&r!l&&!r
010
MC/DC0%0/2
$hdr ||= _read_flexhdr("$name.hdr")
$hdr _read_flexhdr("$name.hdr")
Truth table33%1/3
A ||= B
A= $hdrB= _read_flexhdr("$name.hdr")
ABresult
1X1
011
000
432 1 ▶ my $hash = $hdr->[-1] || barf "glueflex: need valid header-hash";
Condition33%1/3
$hdr->[-1] || barf('glueflex: need valid header-hash')
l!l&&r!l&&!r
100
MC/DC0%0/2
$hdr->[-1] || barf('glueflex: need valid header-hash')
$hdr->[-1] barf('glueflex: need valid header-hash')
Truth table33%1/3
A || B
A= $hdr->[-1]B= barf('glueflex: need valid header-hash')
ABresult
1X1
011
000
433 0 barf "glueflex: ndarray has type '@{[$pdl->type]}' but last hash has type '$hash->{Type}'"
434 1 ▶ if $pdl->type != PDL::Type->new($hash->{Type});
Branch: 1 branch
BranchTrueFalseTotal
if $pdl->type != "PDL::Type"->new($hash->{"Type"}) 0 1 1
435 1 ▶ my @dims = ref $hash->{Dims} ? @{$hash->{Dims}} : $hash->{Dims};
Branch: 1 branch
BranchTrueFalseTotal
ref $hash->{'Dims'} ? : 1 0 1
436 1 ▶ barf "glueflex: header dims needs at least 2 dims, got (@dims)" if @dims < 2;
Branch: 1 branch
BranchTrueFalseTotal
if @dims < 2 0 1 1
437 1 my @ldims = @dims[0..$#dims-1];
438 0 ▶ barf "glueflex: incompatible lower dims, ndarray (@{[$pdl->dims]}) vs header (@ldims)"
Branch: 1 branch
BranchTrueFalseTotal
unless all($pdl->shape == pdl(@ldims)) 0 1 1
439 if !all($pdl->shape == pdl(@ldims));
440 1 print $d ${$pdl->get_dataref};
441 1 $dims[-1]++;
442 1 $hash->{Dims} = \@dims;
443 1 ▶ if (defined wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined wantarray) { } 0 1 1
444 # list or scalar context
445 0 ▶ writeflexhdr($name, $hdr) if $isname and $writeflexhdr;
Branch: 1 branch
BranchTrueFalseTotal
if $isname and $PDL::IO::FlexRaw::writeflexhdr 0 0 0
Condition0%0/3
$isname and $PDL::IO::FlexRaw::writeflexhdr
!ll&&!rl&&r
000
MC/DC0%0/2
$isname and $PDL::IO::FlexRaw::writeflexhdr
$isname $PDL::IO::FlexRaw::writeflexhdr
Truth table0%0/3
A and B
A= $isnameB= $PDL::IO::FlexRaw::writeflexhdr
ABresult
0X0
100
111
446 0 return $hdr;
447 } else {
448 # void context so write header file
449 1 ▶ writeflexhdr($name, $hdr) if $isname;
Branch: 1 branch
BranchTrueFalseTotal
if $isname 1 0 1
450 1 return;
451 }
452 }
453  
454 =head2 readflex
455  
456 =for ref
457  
458 Read a binary file with flexible format specification
459  
460 =for usage
461  
462 Usage:
463  
464 ($x,$y,...) = readflex("filename" [, $hdr])
465 ($x,$y,...) = readflex(FILEHANDLE [, $hdr])
466  
467 =cut
468  
469 23 ▶sub readflex {
Subroutine readflex: called CC=52 SCAR=62.0
Pod documented
470 23 ▶ barf 'Usage ($x,$y,...) = readflex("filename"|FILEHANDLE [, \@hdr])'
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 2 0 23 23
471 if @_ > 2;
472 23 my ($name,$h) = @_;
473 23 my ($hdr, $pdl, $len, @out, $chunk, $chunkread, $data);
474 23 my $offset = 0;
475 23 my ($newfile, $swapbyte, $f77mode, $zipt) = (1,0,0,0);
476 23 my $d;
477 # Test if $name is a file handle
478 23 ▶ if (defined fileno($name)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined fileno $name) { } 0 23 23
479 0 $d = $name;
480 } else {
481 23 $name =~ s/\.(gz|Z)$//; # strip any trailing compression suffix
482 23 $data = $name;
483 23 ▶ if (! -e $name ) { # If it's still not found, then...
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $name) 0 23 23
484 0 suffix: for my $suffix (grep -e "$name.$_", 'gz','Z') {
485 ## This little fillip detects gzip if we need it, and caches
486 ## the version in a package-global variable. The return string
487 ## is undefined if there is no gzip in the path.
488 0 our $gzip_version;
489 0 ▶ unless (defined($gzip_version)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $PDL::IO::FlexRaw::gzip_version) 0 0 0
490 # Try running gzip -V to get the version. Redirect STDERR to STDOUT since
491 # Apple'z gzip writes its version to STDERR.
492 0 $gzip_version = `gzip -V 2>&1`;
493 0 ▶ unless(defined($gzip_version)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $PDL::IO::FlexRaw::gzip_version) 0 0 0
494 # That may or may not work on Microsoft Windows, so if it doesn't,
495 # try running gzip again without the redirect.
496 0 $gzip_version = `gzip -V`;
497 }
498 0 ▶ barf "FlexRaw: couldn't find the external gzip utility (to parse $name.$suffix)!" unless(defined($gzip_version));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $PDL::IO::FlexRaw::gzip_version 0 0 0
499 }
500 0 ▶ if($gzip_version =~ m/^Apple/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($PDL::IO::FlexRaw::gzip_version =~ /^Apple/) { } 0 0 0
501 # Apple gzip requires a suffix
502 0 $data = "gzip -dcq $name.$suffix |";
503 } else {
504 # Other gzips apparently don't require a suffix - they find it automagically.
505 0 $data = "gzip -dcq $name |";
506 }
507 0 $zipt = 1;
508 0 last suffix;
509 }
510 }
511 23 my $size = (stat $name)[7];
512 23 ▶ open $d, $data
Branch: 1 branch
BranchTrueFalseTotal
open $d, $data or barf("Couldn't open '${data}' for reading: $!") 23 0 23
513 or barf "Couldn't open '$data' for reading: $!";
514 23 ▶ $h ||= _read_flexhdr("$name.hdr");
Condition66%2/3
$h ||= _read_flexhdr("$name.hdr")
l!l&&r!l&&!r
3200
MC/DC0%0/2
$h ||= _read_flexhdr("$name.hdr")
$h _read_flexhdr("$name.hdr")
Truth table66%2/3
A ||= B
A= $hB= _read_flexhdr("$name.hdr")
ABresult
1X1
011
000
515 }
516 23 binmode $d;
517 barf "Last dim given as undef but >1 header-hash given"
518 23 ▶ if ref $h->[0]{Dims} and @{$h->[0]{Dims}} and !defined $h->[0]{Dims}[-1] and @$h > 1;
Branch: 1 branch
BranchTrueFalseTotal
if ref $h->[0]{"Dims"} and @{$h->[0]{"Dims"};} and not defined $h->[0]{"Dims"}[-1] and @$h > 1 1 22 23
Condition66%6/9
ref $h->[0]{"Dims"} and @{$h->[0]{"Dims"};} and not defined $h->[0]{"Dims"}[-1] and @$h > 1
!ll&&!rl&&r
2111
ref $h->[0]{"Dims"} and @{$h->[0]{"Dims"};} and not defined $h->[0]{"Dims"}[-1]
!ll&&!rl&&r
0212
ref $h->[0]{"Dims"} and @{$h->[0]{"Dims"};}
!ll&&!rl&&r
0023
MC/DC50%2/4
ref $h->[0]{"Dims"} and @{$h->[0]{"Dims"};} and not defined $h->[0]{"Dims"}[-1] and @$h > 1
ref $h->[0]{"Dims"} @{$h->[0]{"Dims"};} not defined $h->[0]{"Dims"}[-1] @$h > 1
Truth table60%3/5
A and B and not(C) and D
A= ref $h->[0]{"Dims"}B= @{$h->[0]{"Dims"};}C= not defined $h->[0]{"Dims"}[-1]D= @$h > 1
ABCDresult
0XXX0
10XX0
110X0
11100
11111
519 # Go through headers which reconfigure
520 22 foreach $hdr (@$h) {
521 22 my ($type) = $hdr->{Type};
522 22 ▶ if ($type eq 'swap') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'swap') { } 0 22 22
elsif ($type ne 'f77') { } 22 0 22
523 0 $swapbyte = 1;
524 } elsif ($type ne 'f77') {
525 22 last;
526 }
527 }
528 22 READ: foreach $hdr (@$h) {
529 25 my ($type) = $hdr->{Type};
530 # Case convert when we have user data
531 25 ▶ $type =~ tr/A-Z/a-z/ if @_ == 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 2 2 23 25
532 25 ▶ if ($newfile) {
Branch: 1 branch
BranchTrueFalseTotal
if ($newfile) 22 3 25
533 22 ▶ if ($type eq 'f77') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'f77') { } 0 22 22
elsif ($type eq 'swap') { } 0 22 22
534 0 $hdr = { Type => $PDL_L, Dims => [ ], NDims => 0 };
535 0 $type = $PDL_L;
536 0 $f77mode = 1;
537 } elsif ($type eq 'swap') {
538 0 next READ;
539 } else {
540 22 $newfile = 0;
541 }
542 }
543 25 ▶ if (@_ == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 2) 2 23 25
544 2 ▶ barf "Bad typename '$type' in readflex" if !defined $flextypes{$type};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $flextypes{$type} 0 2 2
545 2 $type = $flextypes{$type};
546 }
547 25 ▶ my @dims = ref $hdr->{Dims} ? @{$hdr->{Dims}} : $hdr->{Dims};
Branch: 1 branch
BranchTrueFalseTotal
ref $hdr->{'Dims'} ? : 25 0 25
548 25 ▶ my @rdims = @dims[0..($#dims - (defined $dims[-1] ? 0 : 1))];
Branch: 1 branch
BranchTrueFalseTotal
defined $dims[-1] ? : 24 1 25
549 25 $len = pdl(PDL::Core::howbig($type), @rdims)->prodover->sclr;
550 25 ▶ if (@dims and !defined $dims[-1]) {
Branch: 1 branch
BranchTrueFalseTotal
if (@dims and not defined $dims[-1]) { } 1 24 25
Condition66%2/3
@dims and not defined $dims[-1]
!ll&&!rl&&r
0241
MC/DC50%1/2
@dims and not defined $dims[-1]
@dims not defined $dims[-1]
Truth table66%2/3
A and not(B)
A= @dimsB= not defined $dims[-1]
ABresult
0X0
100
111
551 1 my ($count, @pdls) = 0;
552 1 ▶ while (!eof $d) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eof $d) 3 1 4
553 3 push @pdls, PDL->zeroes(PDL::Type->new($type), @rdims);
554 3 $offset += readchunk($d,$pdls[-1],$len,$name, $offset);
555 3 $count++;
556 }
557 1 $pdl = pdl(@pdls);
558 1 $len *= $count;
559 } else {
560 24 $pdl = PDL->zeroes(PDL::Type->new($type), @dims);
561 24 $offset += readchunk($d,$pdl,$len,$name, $offset);
562 }
563 25 $chunkread += $len;
564 25 ▶ if ($swapbyte) {
Branch: 1 branch
BranchTrueFalseTotal
if ($swapbyte) 0 25 25
565 0 my $method = $flexswap{$type};
566 0 ▶ $pdl->$method if $method;
Branch: 1 branch
BranchTrueFalseTotal
if $method 0 0 0
567 }
568 25 ▶ if ($newfile && $f77mode) {
Branch: 1 branch
BranchTrueFalseTotal
if ($newfile and $f77mode) 0 25 25
Condition33%1/3
$newfile and $f77mode
!ll&&!rl&&r
2500
MC/DC0%0/2
$newfile and $f77mode
$newfile $f77mode
Truth table33%1/3
A and B
A= $newfileB= $f77mode
ABresult
0X0
100
111
569 0 ▶ if ($zipt || $swapbyte) {
Branch: 1 branch
BranchTrueFalseTotal
if ($zipt or $swapbyte) { } 0 0 0
Condition0%0/3
$zipt or $swapbyte
l!l&&r!l&&!r
000
MC/DC0%0/2
$zipt or $swapbyte
$zipt $swapbyte
Truth table0%0/3
A or B
A= $ziptB= $swapbyte
ABresult
1X1
011
000
570 0 $chunk = $pdl->copy;
571 0 $chunkread = 0;
572 0 next READ;
573 } else {
574 0 SWAP: foreach (0,1) {
575 0 seek($d,4,0);
576 0 $swapbyte = $_;
577 0 ▶ bswap4($pdl) if $swapbyte;
Branch: 1 branch
BranchTrueFalseTotal
if $swapbyte 0 0 0
578 0 $chunk = $pdl->copy;
579 0 ▶ next SWAP if !seek($d,$pdl->at,1);
Branch: 1 branch
BranchTrueFalseTotal
unless seek $d, $pdl->at, 1 0 0 0
580 0 ▶ next SWAP if read($d,${$chunk->get_dataref},$len) != $len;
Branch: 1 branch
BranchTrueFalseTotal
if read($d, ${$chunk->get_dataref;}, $len) != $len 0 0 0
581 0 $chunk->upd_data;
582 0 ▶ bswap4($chunk) if $swapbyte;
Branch: 1 branch
BranchTrueFalseTotal
if $swapbyte 0 0 0
583 0 ▶ next SWAP if $pdl->at != $chunk->at;
Branch: 1 branch
BranchTrueFalseTotal
if $pdl->at != $chunk->at 0 0 0
584 0 $chunkread = 0;
585 0 ▶ barf "Error can't rewind" if !seek($d,4,0);
Branch: 1 branch
BranchTrueFalseTotal
unless seek $d, 4, 0 0 0 0
586 0 next READ;
587 }
588 0 barf "Error: Doesn't look like f77 file (even swapped)";
589 }
590 }
591 25 ▶ if ($hdr->{BadFlag}) { # set badflag and badvalue if needed
Branch: 1 branch
BranchTrueFalseTotal
if ($hdr->{"BadFlag"}) 0 25 25
592 0 $pdl->badflag($hdr->{BadFlag});
593 0 ▶ $pdl->badvalue($hdr->{BadValue}) if defined $hdr->{BadValue};
Branch: 1 branch
BranchTrueFalseTotal
if defined $hdr->{"BadValue"} 0 0 0
594 }
595 25 push @out, $pdl;
596 25 ▶ if ($f77mode && $chunk->at == $chunkread) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f77mode and $chunk->at == $chunkread) 0 25 25
Condition33%1/3
$f77mode and $chunk->at == $chunkread
!ll&&!rl&&r
2500
MC/DC0%0/2
$f77mode and $chunk->at == $chunkread
$f77mode $chunk->at == $chunkread
Truth table33%1/3
A and B
A= $f77modeB= $chunk->at == $chunkread
ABresult
0X0
100
111
597 0 $chunkread = 0;
598 0 my ($check) = $chunk->copy;
599 0 $offset += readchunk($d,$check,4,$name,$offset);
600 0 ▶ bswap4($check) if $swapbyte;
Branch: 1 branch
BranchTrueFalseTotal
if $swapbyte 0 0 0
601 0 ▶ if ($check->at ne $chunk->at) {
Branch: 1 branch
BranchTrueFalseTotal
if ($check->at ne $chunk->at) 0 0 0
602 0 barf "F77 file format error for $check cf $chunk";
603 0 last READ;
604 }
605 0 ▶ last READ if eof $d;
Branch: 1 branch
BranchTrueFalseTotal
if eof $d 0 0 0
606 0 $offset += readchunk($d,$chunk,4,$name,$offset);
607 0 ▶ bswap4($chunk) if $swapbyte;
Branch: 1 branch
BranchTrueFalseTotal
if $swapbyte 0 0 0
608 }
609 }
610 22 ▶ wantarray ? @out : $out[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 6 16 22
611 }
612  
613 =head2 mapflex
614  
615 =for ref
616  
617 Memory map a binary file with flexible format specification
618  
619 =for usage
620  
621 Usage:
622  
623 ($x,$y,...) = mapflex("filename" [, $hdr] [, $opts])
624  
625 =for options
626  
627 All of these options default to false unless set true:
628  
629 ReadOnly - Data should be readonly
630 Creat - Create file if it doesn't exist
631 Trunc - File should be truncated to a length that conforms
632 with the header
633  
634 =cut
635  
636 4 ▶sub mapflex {
Subroutine mapflex: called CC=40 SCAR=49.7
Pod documented
637 4 my $name = shift;
638 # reference to header array
639 4 my ($h, $size);
640 # reference to options array, with defaults
641 4 my %opts = ( 'ReadOnly' => 0, 'Creat' => 0, 'Trunc' => 0 );
642 4 my (@out, $chunk, $chunkread);
643 4 my $offset = 0;
644 4 my ($newfile, $swapbyte, $f77mode, $zipt) = (1,0,0,0);
645 4 foreach (@_) {
646 2 ▶ if (ref($_) eq "ARRAY") {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $_ eq 'ARRAY') { } 1 1 2
elsif (ref $_ eq 'HASH') { } 1 0 1
647 1 $h = $_;
648 } elsif (ref($_) eq "HASH") {
649 1 %opts = (%opts,%$_);
650 } else {
651 0 warn 'Usage ($x,$y,...) = mapflex("filename" [, \@hdr] [,\%opts])';
652 }
653 }
654 4 ▶ barf "Can't map compressed file"
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ s/\.gz$// or $name =~ s/\.Z$// or not -e $name and -e $name . '.gz' || -e $name . '.Z' 0 4 4
Condition41%5/12
$name =~ s/\.gz$// or $name =~ s/\.Z$// or not -e $name and -e $name . '.gz' || -e $name . '.Z'
l!l&&r!l&&!r
004
$name =~ s/\.gz$// or $name =~ s/\.Z$//
l!l&&r!l&&!r
004
not -e $name and -e $name . '.gz' || -e $name . '.Z'
!ll&&!rl&&r
310
-e $name . '.gz' or -e $name . '.Z'
l!l&&r!l&&!r
001
MC/DC0%0/5
$name =~ s/\.gz$// or $name =~ s/\.Z$// or not -e $name and -e $name . '.gz' || -e $name . '.Z'
$name =~ s/\.gz$// $name =~ s/\.Z$// not -e $name -e $name . '.gz' -e $name . '.Z'
Truth table33%2/6
A or B or not(C) and D or E
A= $name =~ s/\.gz$//B= $name =~ s/\.Z$//C= not -e $nameD= -e $name . '.gz'E= -e $name . '.Z'
ABCDEresult
1XXXX1
01XXX1
0011X1
001011
000XX0
001000
655 if $name =~ s/\.gz$// || $name =~ s/\.Z$// ||
656 (!-e $name && (-e $name.'.gz' || -e $name.'.Z'));
657 4 ▶ $h = _read_flexhdr("$name.hdr") if !defined $h;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $h 3 1 4
658 # Go through headers which reconfigure
659 4 for my $hdr (@$h) {
660 4 my $type = $hdr->{Type};
661 4 ▶ barf "Can't map byte swapped file" if $type eq 'swap';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "swap" 0 4 4
662 4 ▶ if ($type eq 'f77') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'f77') { } 0 4 4
663 0 $f77mode = 1;
664 } else {
665 4 ▶ barf "Bad typename '$type' in mapflex" if !defined $flextypes{$type};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $flextypes{$type} 0 4 4
666 4 $type = $flextypes{$type};
667 4 $size += _data_size_in_bytes($type, $hdr->{Dims});
668 }
669 }
670 # $s now contains estimated size of data in header --
671 # setting $f77mode means that it will be 8 x n bigger in reality
672 4 ▶ $size += 8 if $f77mode;
Branch: 1 branch
BranchTrueFalseTotal
if $f77mode 0 4 4
673 4 ▶ if (!$opts{Creat}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opts{"Creat"}) 3 1 4
674 3 my $s = $size;
675 3 $size = (stat $name)[7];
676 3 ▶ barf "File looks too small ($size cf header $s)" if $size < $s;
Branch: 1 branch
BranchTrueFalseTotal
if $size < $s 0 3 3
677 }
678 4 my $mapped_sv; do {
679 4 ▶ my $writable = $opts{ReadOnly}?0:1;
Branch: 1 branch
BranchTrueFalseTotal
$opts{'ReadOnly'} ? : 0 4 4
680 4 ▶ my $creat = $opts{Creat}?1:0;
Branch: 1 branch
BranchTrueFalseTotal
$opts{'Creat'} ? : 1 3 4
681 4 ▶ my $trunc = $opts{Creat} || $opts{Trunc} ? 1:0;
Branch: 1 branch
BranchTrueFalseTotal
$opts{'Creat'} || $opts{'Trunc'} ? : 1 3 4
Condition66%2/3
$opts{"Creat"} or $opts{"Trunc"}
l!l&&r!l&&!r
103
MC/DC50%1/2
$opts{"Creat"} or $opts{"Trunc"}
$opts{"Creat"} $opts{"Trunc"}
Truth table66%2/3
A or B
A= $opts{"Creat"}B= $opts{"Trunc"}
ABresult
1X1
011
000
682 4 my $fh = PDL::Core::_file_map_open($name,$size,1,$writable,$creat,0644,$trunc);
683 4 PDL::Core::_file_map_sv(\$mapped_sv,$fh,$size,1,$writable);
684 };
685 4 READ: for my $hdr (@$h) {
686 4 my ($type) = $hdr->{Type};
687 # Case convert when we have user data
688 4 ▶ $type =~ tr/A-Z/a-z/ if @_ == 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 2 1 3 4
689 4 ▶ if ($newfile) {
Branch: 1 branch
BranchTrueFalseTotal
if ($newfile) 4 0 4
690 4 ▶ if ($type eq 'f77') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'f77') { } 0 4 4
691 0 $hdr = { Type => $PDL_L, Dims => [], NDims => 0 };
692 0 $type = $PDL_L;
693 } else {
694 4 $newfile = 0;
695 }
696 }
697 4 ▶ if (@_ == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 2) 1 3 4
698 1 ▶ barf "Bad typename '$type' in mapflex" if !defined $flextypes{$type};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $flextypes{$type} 0 1 1
699 1 $type = $flextypes{$type};
700 }
701 4 my $len = _data_size_in_bytes($type, $hdr->{Dims});
702 4 my $pdl = mapchunk(\$mapped_sv,$type,$hdr->{Dims},$offset,$len);
703 4 ▶ last READ if !defined $pdl;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pdl 0 4 4
704 4 $offset += $len;
705 4 $chunkread += $len;
706 4 ▶ if ($newfile && $f77mode) {
Branch: 1 branch
BranchTrueFalseTotal
if ($newfile and $f77mode) 0 4 4
Condition33%1/3
$newfile and $f77mode
!ll&&!rl&&r
400
MC/DC0%0/2
$newfile and $f77mode
$newfile $f77mode
Truth table33%1/3
A and B
A= $newfileB= $f77mode
ABresult
0X0
100
111
707 0 ▶ if ($opts{Creat}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{'Creat'}) { } 0 0 0
708 0 $pdl->set(0,$size - 8);
709 } else {
710 0 $chunk = $pdl->copy;
711 }
712 0 $chunkread = 0;
713 0 next READ;
714 }
715 4 ▶ if ($hdr->{BadFlag}) { # set badflag and badvalue if needed
Branch: 1 branch
BranchTrueFalseTotal
if ($hdr->{"BadFlag"}) 0 4 4
716 0 $pdl->badflag($hdr->{BadFlag});
717 0 ▶ $pdl->badvalue($hdr->{BadValue}) if defined $hdr->{BadValue};
Branch: 1 branch
BranchTrueFalseTotal
if defined $hdr->{"BadValue"} 0 0 0
718 }
719 4 push @out, $pdl;
720 4 ▶ if ($f77mode && $chunk->at == $chunkread) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f77mode and $chunk->at == $chunkread) 0 4 4
Condition33%1/3
$f77mode and $chunk->at == $chunkread
!ll&&!rl&&r
400
MC/DC0%0/2
$f77mode and $chunk->at == $chunkread
$f77mode $chunk->at == $chunkread
Truth table33%1/3
A and B
A= $f77modeB= $chunk->at == $chunkread
ABresult
0X0
100
111
721 0 $chunkread = 0;
722 0 my $check = mapchunk(\$mapped_sv,$type,$hdr->{Dims},$offset,4);
723 0 ▶ last READ if !defined $check;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $check 0 0 0
724 0 $offset += 4;
725 0 ▶ if ($opts{Creat}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{'Creat'}) { } 0 0 0
726 0 $check->set(0,$size-8);
727 } else {
728 0 ▶ barf "F77 file format error for $check cf $chunk"
Branch: 1 branch
BranchTrueFalseTotal
if $check->at ne $chunk->at 0 0 0
729 if $check->at ne $chunk->at;
730 }
731 0 ▶ barf "Will only map first f77 data statement" if $offset < $size;
Branch: 1 branch
BranchTrueFalseTotal
if $offset < $size 0 0 0
732 0 last READ;
733 }
734 }
735 4 ▶ wantarray ? @out : $out[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 4 4
736 }
737  
738 8 ▶sub _data_size_in_bytes {
Subroutine _data_size_in_bytes: called CC=3 SCAR=11.1
739 8 my ($type, $dims) = @_;
740 8 my $si = 1;
741 8 ▶ $si *= $_ for ref $dims ? @$dims : $dims;
Branch: 1 branch
BranchTrueFalseTotal
ref $dims ? : 8 0 8
742 8 $si * PDL::Core::howbig($type);
743 }
744  
745 =head2 writeflex
746  
747 =for ref
748  
749 Write a binary file with flexible format specification
750  
751 =for usage
752  
753 Usage:
754  
755 $hdr = writeflex($file, $pdl1, $pdl2,...) # or
756 $hdr = writeflex(FILEHANDLE, $pdl1, $pdl2,...)
757 # now you must call writeflexhdr()
758 writeflexhdr($file, $hdr)
759  
760 or
761  
762 $PDL::IO::FlexRaw::writeflexhdr = 1; # set so we don't have to call writeflexhdr
763  
764 $hdr = writeflex($file, $pdl1, $pdl2,...) # remember, $file must be filename
765 writeflex($file, $pdl1, $pdl2,...) # remember, $file must be filename
766  
767 =cut
768  
769 20 ▶sub writeflex {
Subroutine writeflex: called CC=13 SCAR=27.9
Pod documented
770 20 my $usage = 'Usage $hdr = writeflex("filename"|FILEHANDLE,$pdl,...)';
771 20 ▶ barf $usage if !@_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 20 20
772 20 my $name = shift;
773 20 my $isname = 0;
774 20 my $hdr;
775 my $fh;
776 # Test if $name is a file handle
777 20 ▶ if (defined fileno($name)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined fileno $name) { } 0 20 20
778 0 $fh = $name;
779 } else {
780 20 ▶ barf $usage if ref $name;
Branch: 1 branch
BranchTrueFalseTotal
if ref $name 0 20 20
781 20 $isname = 1;
782 20 ▶ my $modename = ($name =~ /^[+]?[><|]/) ? $name : ">$name";
Branch: 1 branch
BranchTrueFalseTotal
$name =~ /^[+]?[><|]/ ? : 0 20 20
783 20 ▶ open $fh, $modename or barf "Couldn't open '$name' for writing: $!";
Branch: 1 branch
BranchTrueFalseTotal
open $fh, $modename or barf("Couldn't open '${name}' for writing: $!") 20 0 20
784 }
785 20 binmode $fh;
786 20 foreach my $pdl (@_) {
787 22 ▶ barf $usage if !ref $pdl;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $pdl 0 22 22
788 push @$hdr, {
789 22 ▶ Type => $flexnames{$pdl->get_datatype},
Branch: 1 branch
BranchTrueFalseTotal
$pdl->badvalue == $pdl->orig_badvalue ? : 22 0 22
790 Dims => [ $pdl->dims ],
791 NDims => $pdl->getndims,
792 BadFlag => $pdl->badflag,
793 BadValue => (($pdl->badvalue == $pdl->orig_badvalue) ? undef : $pdl->badvalue),
794 };
795 22 print $fh ${$pdl->get_dataref};
796 }
797 20 ▶ if (defined wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined wantarray) { } 19 1 20
798 # list or scalar context
799 19 ▶ writeflexhdr($name, $hdr) if $isname and $writeflexhdr;
Branch: 1 branch
BranchTrueFalseTotal
if $isname and $PDL::IO::FlexRaw::writeflexhdr 0 19 19
Condition33%1/3
$isname and $PDL::IO::FlexRaw::writeflexhdr
!ll&&!rl&&r
0190
MC/DC0%0/2
$isname and $PDL::IO::FlexRaw::writeflexhdr
$isname $PDL::IO::FlexRaw::writeflexhdr
Truth table33%1/3
A and B
A= $isnameB= $PDL::IO::FlexRaw::writeflexhdr
ABresult
0X0
100
111
800 19 return $hdr;
801 } else {
802 # void context so write header file
803 1 ▶ writeflexhdr($name, $hdr) if $isname;
Branch: 1 branch
BranchTrueFalseTotal
if $isname 1 0 1
804 1 return;
805 }
806 }
807  
808 =head2 writeflexhdr
809  
810 =for ref
811  
812 Write the header file corresponding to a previous writeflex call
813  
814 =for usage
815  
816 Usage:
817  
818 writeflexhdr($file, $hdr)
819  
820 $file or "filename" is the filename used in a previous writeflex
821 If $file is actually a "filename" then writeflexhdr() will be
822 called automatically if $PDL::IO::FlexRaw::writeflexhdr is true.
823 If writeflex() was to a FILEHANDLE, you will need to call
824 writeflexhdr() yourself since the filename cannot be determined
825 (at least easily).
826  
827 =cut
828  
829 21 ▶sub writeflexhdr {
Subroutine writeflexhdr: called CC=8 SCAR=22.6
Pod documented
830 21 ▶ barf 'Usage writeflex("filename", $hdr)' if @_!=2 || !ref $_[1];
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 2 or not ref $_[1] 0 21 21
Condition33%1/3
@_ != 2 or not ref $_[1]
l!l&&r!l&&!r
0021
MC/DC0%0/2
@_ != 2 or not ref $_[1]
@_ != 2 not ref $_[1]
Truth table33%1/3
A or not(B)
A= @_ != 2B= not ref $_[1]
ABresult
1X1
011
000
831 21 my($name, $hdr) = @_;
832 21 my $hname = "$name.hdr";
833 21 ▶ open my $h, '>', $hname or barf "Couldn't open '$hname' for writing: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $h, ">", $hname or barf("Couldn't open '${hname}' for writing: $!") 21 0 21
834 21 binmode $h;
835 21 print $h "# Output from PDL::IO::writeflex, data in $name\n";
836 21 foreach (@$hdr) {
837 24 my $type = $_->{Type};
838 barf "Writeflexhdr: will only print data elements, not $type"
839 24 ▶ if !exists $flextypes{$type};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $flextypes{$type} 0 24 24
840 print $h join("\n",$type, $_->{NDims},
841 24 ▶ (join ' ',ref $_->{Dims} ? @{$_->{Dims}} : $_->{Dims}) . (($_->{BadFlag}) ? " badvalue $_->{BadValue}" : '')),
Branch: 2 branches
BranchTrueFalseTotal
ref $_->{'Dims'} ? : 24 0 24
$_->{'BadFlag'} ? : 0 24 24
842 "\n\n";
843 }
844 }
845  
846 =head1 BAD VALUE SUPPORT
847  
848 As of PDL-2.4.8, L<PDL::IO::FlexRaw> has support for reading and writing
849 pdls with L<bad|PDL::Bad> values in them.
850  
851 On C<writeflex>, an ndarray
852 argument with C<< $pdl->badflag == 1 >> will have the keyword/token "badvalue"
853 added to the header file after the dimension list and an additional token
854 with the bad value for that pdl if C<< $pdl->badvalue != $pdl->orig_badvalue >>.
855  
856 On C<readflex>, a pdl with the "badvalue" token in the header will
857 automatically have its L<badflag|PDL::Bad/#badflag> set and its
858 L<badvalue|PDL::Bad/#badvalue> as well if it is not the standard default for that type.
859  
860 =for example
861  
862 The new badvalue support required some additions to the header
863 structure. However, the interface is still being finalized. For
864 reference the current C<$hdr> looks like this:
865  
866 $hdr = {
867 Type => 'byte', # data type
868 NDims => 2, # number of dimensions
869 Dims => [640,480], # dims
870 BadFlag => 1, # is set/set badflag
871 BadValue => undef, # undef==default
872 };
873  
874 $badpdl = readflex('badpdl', [$hdr]);
875  
876 If you use bad values and try the new L<PDL::IO::FlexRaw> bad value
877 support, please let us know via the perldl mailing list.
878 Suggestions and feedback are also welcome.
879  
880 =head1 BUGS
881  
882 The test on two dimensional byte arrays fail using g77 2.7.2, but not
883 Sun f77. I hope this isn't my problem!
884  
885 Assumes gzip is on the PATH.
886  
887 Can't auto-swap compressed files, because it can't seek on them.
888  
889 The header format may not agree with that used elsewhere.
890  
891 Should it handle handles?
892  
893 Mapflex should warn and fallback to reading on SEGV? Would have to
894 make sure that the data was written back after it was `destroyed'.
895  
896 =head1 AUTHOR
897  
898 Copyright (C) Robin Williams <rjrw@ast.leeds.ac.uk> 1997.
899 All rights reserved. There is no warranty. You are allowed
900 to redistribute this software / documentation under certain
901 conditions. For details, see the file COPYING in the PDL
902 distribution. If this file is separated from the PDL distribution,
903 the copyright notice should be included in the file.
904  
905 Documentation contributions copyright (C) David Mertens, 2010.
906  
907 =cut
908  
909 2 1;
« lib/PDL/Core/Dev.pm ↑ Summary blib/lib/PDL/Doc/Perldl.pm »