blib/lib/PDL/IO/FlexRaw.pm

filter: Statementstmt 68.3% 218 / 319 Branchbran 42.5% 102 / 240 Conditioncond 33.3% 22 / 66 MC/DCmcdc 10.0% 4 / 40 Subroutinesub 93.3% 14 / 15 Podpod 57.1% 4 / 7 totaltotal 52.9% 364 / 687 CC 152 SCAR 57.6
CC
152
Cov
55%
readflex
742.0
mapflex
273.7
_read_flexhdr
93.6
CRAP 316.8
« blib/lib/PDL/IO/Misc.pm ↑ Summary blib/lib/PDL/IO/FITS.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 $a = (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 Within PDL, readflex and writeflex can be used to write several pdls
108 to a single file -- e.g.
109  
110 use PDL;
111 use PDL::IO::FlexRaw;
112  
113 @pdls = ($pdl1, $pdl2, ...);
114 $hdr = writeflex("fname",@pdls);
115 @pdl2 = readflex("fname",$hdr);
116  
117 writeflexhdr("fname",$hdr); # not needed if $PDL::IO::FlexRaw::writeflexhdr is set
118 @pdl3 = readflex("fname");
119  
120 -- C<writeflex> produces the data file and returns the file header as an
121 anonymous hash, which can be written to a .hdr file using
122 C<writeflexhdr>.
123  
124 If the package variable C<$PDL::IO::FlexRaw::writeflexhdr>
125 is true, and the C<writeflex> call was with a I<filename> and not
126 a handle, C<writeflexhdr> will be called automatically (as done by
127 C<writefraw>.
128  
129 The reading of compressed data is switched on automatically if the
130 filename requested ends in .gz or .Z, or if the originally specified
131 filename does not exist, but one of these compressed forms does.
132  
133 If C<writeflex> and C<readflex> are given a reference to a
134 file handle as a first parameter instead of a filename, then
135 the data is read or written to the open filehandle. This
136 gives an easy way to read an arbitrary slice in a big data
137 volume, as in the following example:
138  
139 use PDL;
140 use PDL::IO::FastRaw;
141  
142 open(DATA, "raw3d.dat");
143 binmode(DATA);
144  
145 # assume we know the data size from an external source
146 ($width, $height, $data_size) = (256,256, 4);
147  
148 my $slice_num = 64; # slice to look at
149 # Seek to slice
150 seek(DATA, $width*$height*$data_size * $slice_num, 0);
151 $pdl = readflex \*DATA, [{Dims=>[$width, $height], Type=>'long'}];
152  
153 WARNING: In later versions of perl (5.8 and up) you must
154 be sure that your file is in "raw" mode (see the perlfunc
155 man page entry for "binmode", for details). Both readflex
156 and writeflex automagically switch the file to raw mode for
157 you -- but in code like the snipped above, you could end up
158 seeking the wrong byte if you forget to make the binmode() call.
159  
160 C<mapflex> memory maps, rather than reads, the data files. Its interface
161 is similar to C<readflex>. Extra options specify if the data is to be
162 loaded `ReadOnly', if the data file is to be `Creat'-ed anew on the
163 basis of the header information or `Trunc'-ated to the length of the
164 data read. The extra speed of access brings with it some limitations:
165 C<mapflex> won't read compressed data, auto-detect f77 files, or read f77
166 files written by more than a single unformatted write statement. More
167 seriously, data alignment constraints mean that C<mapflex> cannot read
168 some files, depending on the requirements of the host OS (it may also
169 vary depending on the setting of the `uac' flag on any given machine).
170 You may have run into similar problems with common blocks in FORTRAN.
171  
172 For instance, floating point numbers may have to align on 4 byte
173 boundaries -- if the data file consists of 3 bytes then a float, it
174 cannot be read. C<mapflex> will warn about this problem when it occurs,
175 and return the PDLs mapped before the problem arose. This can be
176 dealt with either by reorganizing the data file (large types first
177 helps, as a rule-of-thumb), or more simply by using C<readflex>.
178  
179 =head1 BUGS
180  
181 The test on two dimensional byte arrays fail using g77 2.7.2, but not
182 Sun f77. I hope this isn't my problem!
183  
184 Assumes gzip is on the PATH.
185  
186 Can't auto-swap compressed files, because it can't seek on them.
187  
188 The header format may not agree with that used elsewhere.
189  
190 Should it handle handles?
191  
192 Mapflex should warn and fallback to reading on SEGV? Would have to
193 make sure that the data was written back after it was `destroyed'.
194  
195 =head1 FUNCTIONS
196  
197 =head2 readflex
198  
199 =for ref
200  
201 Read a binary file with flexible format specification
202  
203 =for usage
204  
205 Usage:
206  
207 ($x,$y,...) = readflex("filename" [, $hdr])
208 ($x,$y,...) = readflex(FILEHANDLE [, $hdr])
209  
210  
211 =head2 writeflex
212  
213 =for ref
214  
215 Write a binary file with flexible format specification
216  
217 =for usage
218  
219 Usage:
220  
221 $hdr = writeflex($file, $pdl1, $pdl2,...) # or
222 $hdr = writeflex(FILEHANDLE, $pdl1, $pdl2,...)
223 # now you must call writeflexhdr()
224 writeflexhdr($file, $hdr)
225  
226 or
227  
228 $PDL::IO::FlexRaw::writeflexhdr = 1; # set so we don't have to call writeflexhdr
229  
230 $hdr = writeflex($file, $pdl1, $pdl2,...) # remember, $file must be filename
231 writeflex($file, $pdl1, $pdl2,...) # remember, $file must be filename
232  
233 =head2 writeflexhdr
234  
235 =for ref
236  
237 Write the header file corresponding to a previous writeflex call
238  
239 =for usage
240  
241 Usage:
242  
243 writeflexhdr($file, $hdr)
244  
245 $file or "filename" is the filename used in a previous writeflex
246 If $file is actually a "filename" then writeflexhdr() will be
247 called automatically if $PDL::IO::FlexRaw::writeflexhdr is true.
248 If writeflex() was to a FILEHANDLE, you will need to call
249 writeflexhdr() yourself since the filename cannot be determined
250 (at least easily).
251  
252 =head2 mapflex
253  
254 =for ref
255  
256 Memory map a binary file with flexible format specification
257  
258 =for usage
259  
260 Usage:
261  
262 ($x,$y,...) = mapflex("filename" [, $hdr] [, $opts])
263  
264 =for options
265  
266 All of these options default to false unless set true:
267  
268 ReadOnly - Data should be readonly
269 Creat - Create file if it doesn't exist
270 Trunc - File should be truncated to a length that conforms
271 with the header
272  
273 =head2 _read_flexhdr
274  
275 Read a FlexRaw header file and return a header structure.
276  
277 =for usage
278  
279 Usage:
280  
281 $hdr = PDL::IO::FlexRaw::_read_flexhdr($file)
282  
283 Note that C<_read_flexhdr> is supposed to be an internal function. It
284 was not originally documented and it is not tested. However, there
285 appeared to be no other method for obtaining a header structure from
286 a file, so I figured I would write a small bit of documentation on it.
287  
288 =head1 Bad Value Support
289  
290 As of PDL-2.4.8, L<PDL::IO::FlexRaw|PDL::IO::FlexRaw> has support for reading and writing
291 pdls with L<bad|PDL::Bad> values in them.
292  
293 On C<writeflex>, a piddle
294 argument with C<< $pdl->badflag == 1 >> will have the keyword/token "badvalue"
295 added to the header file after the dimension list and an additional token
296 with the bad value for that pdl if C<< $pdl->badvalue != $pdl->orig_badvalue >>.
297  
298 On C<readflex>, a pdl with the "badvalue" token in the header will
299 automatically have its L<badflag|PDL::Bad/#badflag> set and its
300 L<badvalue|PDL::Bad/#badvalue> as well if it is not the standard default for that type.
301  
302 =for example
303  
304 The new badvalue support required some additions to the header
305 structure. However, the interface is still being finalized. For
306 reference the current C<$hdr> looks like this:
307  
308 $hdr = {
309 Type => 'byte', # data type
310 NDims => 2, # number of dimensions
311 Dims => [640,480], # dims
312 BadFlag => 1, # is set/set badflag
313 BadValue => undef, # undef==default
314 };
315  
316 $badpdl = readflex('badpdl', [$hdr]);
317  
318 If you use bad values and try the new L<PDL::IO::FlexRaw|PDL::IO::FlexRaw> bad value
319 support, please let us know via the perldl mailing list.
320 Suggestions and feedback are also welcome.
321  
322  
323 =head1 AUTHOR
324  
325 Copyright (C) Robin Williams <rjrw@ast.leeds.ac.uk> 1997.
326 All rights reserved. There is no warranty. You are allowed
327 to redistribute this software / documentation under certain
328 conditions. For details, see the file COPYING in the PDL
329 distribution. If this file is separated from the PDL distribution,
330 the copyright notice should be included in the file.
331  
332 Documentation contributions copyright (C) David Mertens, 2010.
333  
334 =cut
335  
336 package PDL::IO::FlexRaw;
337  
338 BEGIN {
339 3 ▶ our $have_file_map = 0;
Subroutine BEGIN: called CC=2 SCAR=7.0
340  
341 3 ▶ eval "use File::Map 0.57 qw(map_file)";
Subroutine BEGIN: called CC=1 SCAR=0.0
342 3 ▶ $have_file_map = 1 unless $@;
Branch: 1 branch
BranchTrueFalseTotal
unless $@ 3 0 3
343 }
344  
345 3 ▶use PDL;
Subroutine BEGIN: called CC=1 SCAR=0.0
346 3 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
347 3 ▶use FileHandle;
Subroutine BEGIN: called CC=1 SCAR=0.0
348 3 ▶use PDL::Types ':All';
Subroutine BEGIN: called CC=1 SCAR=0.0
349 3 ▶use PDL::IO::Misc qw(bswap2 bswap4 bswap8);
Subroutine BEGIN: called CC=1 SCAR=0.0
350  
351 3 @PDL::IO::FlexRaw::ISA = qw/Exporter/;
352  
353 3 @EXPORT = qw/writeflex writeflexhdr readflex mapflex/;
354  
355 # Cast type numbers in concrete, for external file's sake...
356 3 %flexnames = ( map {(typefld($_,'numval') => typefld($_,'ioname'))}
357 typesrtkeys());
358 3 %flextypes = ( map {(typefld($_,'ioname') => typefld($_,'numval'),
359 typefld($_,'numval') => typefld($_,'numval'),
360 lc typefld($_,'ppsym') => typefld($_,'numval'),
361 )}
362 typesrtkeys());
363 3 %flexswap = ( map {my $val = typefld($_,'numval');
364 24 my $nb = PDL::Core::howbig($val);
365 24 ▶ ($val => $nb > 1 ? "bswap$nb" : undef)}
Branch: 1 branch
BranchTrueFalseTotal
$nb > 1 ? : 21 3 24
366 typesrtkeys());
367  
368 # use Data::Dumper;
369 # print Dumper \%flexnames;
370 # print Dumper \%flextypes;
371 # print Dumper \%flexswap;
372  
373 # %flexnames = (
374 # $PDL_B => 'byte', $PDL_S => 'short',
375 # $PDL_US => 'ushort', $PDL_L => 'long',
376 # $PDL_F => 'float', $PDL_D => 'double');
377  
378 # %flextypes = (
379 # 'byte' => $PDL_B, '0' => $PDL_B, 'b' => $PDL_B, 'c' => $PDL_B,
380 # 'short' => $PDL_S, '1' => $PDL_S, 's' => $PDL_S,
381 # 'ushort' => $PDL_US,'2' => $PDL_US,'u' => $PDL_US,
382 # 'long' => $PDL_L, '3' => $PDL_L, 'l' => $PDL_L,
383 # 'float' => $PDL_F, '4' => $PDL_F, 'f' => $PDL_F,
384 # 'double' => $PDL_D, '5' => $PDL_D, 'd' => $PDL_D
385 # );
386  
387 3 $PDL::IO::FlexRaw::verbose = 0;
388 3 ▶$PDL::IO::FlexRaw::writeflexhdr = defined($PDL::FlexRaw::IO::writeflexhdr) ? $PDL::FlexRaw::IO::writeflexhdr : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $PDL::FlexRaw::IO::writeflexhdr ? : 0 3 3
389  
390 sub _read_flexhdr {
391 12 ▶ my ($hname) = @_;
Subroutine _read_flexhdr: called CC=34 SCAR=45.4
392 12 ▶ my $hfile = new FileHandle "$hname"
Branch: 1 branch
BranchTrueFalseTotal
my $hfile = "FileHandle"->new("$hname") or barf("Couldn't open '${hname}' for reading") 0 12 12
393 or barf "Couldn't open '$hname' for reading";
394 12 binmode $hfile;
395 12 my ($newfile) = 1;
396 12 my ($tid, @str);
397 12 my (@ret);
398 # check for ENVI files and bail (for now)
399 12 my $line1 = scalar <$hfile>;
400 12 ▶ barf "This is an ENVI format file, please use readenvi()\n" if $line1 =~ /^ENVI\r?$/;
Branch: 1 branch
BranchTrueFalseTotal
if $line1 =~ /^ENVI\r?$/ 0 12 12
401 12 seek $hfile, 0, 0; # reset file pointer to beginning
402 ITEM:
403 12 ▶ while (!eof($hfile)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eof $hfile) 25 0 25
404 25 my (@dims) = (); my ($ndims) = -1, ($mode) = -2;
405 25 my ($have_badvalue) = undef;
406 25 my ($badvalue) = undef;
407 LINE:
408 25 ▶ while (<$hfile>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $hfile)) 64 12 76
409 ### print STDERR "processing line '$_'\n";
410 64 ▶ next LINE if /^#/ or /^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if /^#/ or /^\s*$/ 25 39 64
Condition100%3/3
/^#/ or /^\s*$/
l!l&&r!l&&!r
121339
MC/DC100%2/2
/^#/ or /^\s*$/
/^#/ /^\s*$/
Truth table100%3/3
A or B
A= /^#/B= /^\s*$/
ABresult
1X1
011
000
411 39 chop;
412 39 tr/A-Z/a-z/;
413 39 @str = split;
414 39 TOKEN:
415 ### print STDERR "Got tokens: " . join(',',@str) . "\n";
416 my $numtokens = scalar @str;
417 39 foreach my $token (@str) {
418 44 ▶ next LINE if $token =~ /^#/;
Branch: 1 branch
BranchTrueFalseTotal
if $token =~ /^#/ 0 44 44
419 44 ▶ if ($mode == -2) { # type
Branch: 5 branches
BranchTrueFalseTotal
if ($mode == -2) { } 13 31 44
elsif ($mode == -1) { } 13 18 31
elsif ($mode < $ndims) { } 18 0 18
elsif ($mode == $ndims and not $have_badvalue) { } 0 0 0
elsif ($mode == $ndims and $have_badvalue and $numtokens > 0) { } 0 0 0
420 ### print STDERR " \$mode == -2: #tokens=$numtokens, '$token'\n";
421 13 ▶ if ($newfile) {
Branch: 1 branch
BranchTrueFalseTotal
if ($newfile) 12 1 13
422 12 ▶ if ($token eq 'f77' || $token eq 'swap') {
Branch: 1 branch
BranchTrueFalseTotal
if ($token eq "f77" or $token eq "swap") 0 12 12
Condition33%1/3
$token eq "f77" or $token eq "swap"
l!l&&r!l&&!r
0012
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
423 0 push @ret, {
424 Type => $token
425 };
426 0 $numtokens--;
427 0 next ITEM;
428 }
429 }
430 13 ▶ barf("Bad typename '$token' in readflex") if (!exists($flextypes{$token}));
Branch: 1 branch
BranchTrueFalseTotal
unless exists $flextypes{$token} 0 13 13
431 13 $tid = $flextypes{$token};
432 13 $numtokens--;
433 13 $newfile = 0;
434 13 $mode++;
435 } elsif ($mode == -1) { #ndims
436 ### print STDERR " \$mode == -1: #tokens=$numtokens, '$token'\n";
437 13 ▶ barf("Not number for ndims in readflex") if $token !~ /^\d*$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $token =~ /^\d*$/ 0 13 13
438 13 $ndims = $token;
439 13 ▶ barf("Bad ndims in readflex") if ($ndims < 0);
Branch: 1 branch
BranchTrueFalseTotal
if $ndims < 0 0 13 13
440 13 $numtokens--;
441 13 $mode++;
442 13 ▶ if ($mode == $ndims and $numtokens == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode == $ndims and $numtokens == 0) 0 13 13
Condition33%1/3
$mode == $ndims and $numtokens == 0
!ll&&!rl&&r
1300
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
443 0 last LINE;
444 }
445 } elsif ($mode < $ndims) { # get dims
446 ### print STDERR " # get dims: #tokens=$numtokens, '$token'\n";
447 18 ▶ barf("Not number for dimension in readflex")
Branch: 1 branch
BranchTrueFalseTotal
unless $token =~ /^\d*$/ 0 18 18
448 if $token !~ /^\d*$/;
449 18 push(@dims,$token);
450 18 $numtokens--;
451 18 $mode++;
452 18 ▶ if ($mode == $ndims and $numtokens == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode == $ndims and $numtokens == 0) 13 5 18
Condition66%2/3
$mode == $ndims and $numtokens == 0
!ll&&!rl&&r
5013
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
453 13 last LINE;
454 }
455 ▶ } 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
456 ### print STDERR " # ! \$have_badvalue: #tokens=$numtokens, '$token'\n";
457 0 ▶ if ($token =~ /^badvalue$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token =~ /^badvalue$/) { } 0 0 0
458 0 $have_badvalue = 1;
459 0 $numtokens--;
460 0 ▶ last LINE if $numtokens==0; # using default bad value
Branch: 1 branch
BranchTrueFalseTotal
if $numtokens == 0 0 0 0
461 } else {
462 0 last LINE;
463 }
464 ▶ } 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
465 ### print STDERR " # \$have_badvalue: #tokens = $numtokens, '$token'\n";
466 0 $badvalue = $token;
467 0 last LINE;
468 }
469 }
470 }
471 25 ▶ last ITEM if $mode == -2;
Branch: 1 branch
BranchTrueFalseTotal
if $mode == -2 12 13 25
472 13 ▶ barf("Bad format in readflex header file ($ndims, $mode)") if ($ndims < 0 || $mode != $ndims);
Branch: 1 branch
BranchTrueFalseTotal
if $ndims < 0 or $mode != $ndims 0 13 13
Condition33%1/3
$ndims < 0 or $mode != $ndims
l!l&&r!l&&!r
0013
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
473 13 ▶ push @ret, {
Branch: 1 branch
BranchTrueFalseTotal
$have_badvalue ? : 0 13 13
474 Type => $tid,
475 Dims => \@dims,
476 NDims => $ndims,
477 BadFlag => (($have_badvalue) ? 1 : 0),
478 BadValue => $badvalue,
479 };
480 }
481 12 return \@ret;
482 }
483  
484 sub readchunk {
485 12 ▶ my ($d, $pdl, $len, $name) = @_;
Subroutine readchunk: called CC=3 SCAR=11.1
Pod undocumented
486 12 my ($nread);
487 12 ▶ print "Reading $len at $offset from $name\n"
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 12 12
488 if $PDL::IO::FlexRaw::verbose;
489 12 ▶ ($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") 0 12 12
490 or barf "Couldn't read $len bytes at offset $offset from '$name', got $nread";
491 12 $pdl->upd_data();
492 12 $offset += $len;
493 12 return 1;
494 }
495  
496 sub myhandler {
497 0 ▶ $flexmapok = 0;
Subroutine myhandler: not called CC=1 SCAR=6.9
Pod undocumented
498 0 barf "Data out of alignment, can't map further\n";
499 0 die;
500 }
501  
502 sub mapchunk {
503 2 ▶ my ($orig, $pdl, $len, $name) = @_;
Subroutine mapchunk: called CC=2 SCAR=6.9
Pod undocumented
504 # link $len at $offset from $orig to $pdl.
505 # print "linking $len bytes from $offset\n";
506 2 $pdl->set_data_by_offset($orig,$offset);
507 2 local ($flexmapok)=1;
508 2 ▶ local $SIG{BUS} = \&myhandler unless $^O =~ /MSWin32/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $^O =~ /MSWin32/i 2 0 2
509 2 local $SIG{FPE} = \&myhandler;
510 2 eval {$pdl->clump(-1)->at(0)};
511 2 $offset += $len;
512 2 $flexmapok;
513 }
514  
515 sub readflex {
516 11 ▶ barf 'Usage ($x,$y,...) = readflex("filename"|FILEHANDLE [, \@hdr])'
Subroutine readflex: called CC=48 SCAR=66.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if $#_ > 1 0 11 11
517 if $#_ > 1;
518 11 my ($name,$h) = @_;
519 11 my ($hdr, $pdl, $len, @out, $chunk, $chunkread, $data);
520 11 local ($offset) = 0;
521 11 my ($newfile, $swapbyte, $f77mode, $zipt) = (1,0,0,0);
522 11 my $d;
523 # print("readflex: name is $name\n");
524 # Test if $name is a file handle
525 11 ▶ if (defined fileno($name)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined fileno $name) { } 0 11 11
526 0 $d = $name;
527 0 binmode($d);
528 }
529 else {
530 11 $name =~ s/\.(gz|Z)$//; # strip any trailing compression suffix
531 11 $data = $name;
532 11 ▶ if(! -e $name ) { # If it's still not found, then...
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $name) 0 11 11
533 0 suffix: for my $suffix('gz','Z') {
534 0 ▶ if( -e "$name.$suffix" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e "$name.$suffix") 0 0 0
535  
536 ## This little fillip detects gzip if we need it, and caches
537 ## the version in a package-global variable. The return string
538 ## is undefined if there is no gzip in the path.
539 0 our $gzip_version;
540 0 ▶ unless(defined($gzip_version)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $gzip_version) 0 0 0
541 # Try running gzip -V to get the version. Redirect STDERR to STDOUT since
542 # Apple'z gzip writes its version to STDERR.
543 0 $gzip_version = `gzip -V 2>&1`;
544 0 ▶ unless(defined($gzip_version)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $gzip_version) 0 0 0
545 # That may or may not work on Microsoft Windows, so if it doesn't,
546 # try running gzip again without the redirect.
547 0 $gzip_version = `gzip -V`;
548 }
549 0 ▶ barf "FlexRaw: couldn't find the external gzip utility (to parse $name.$suffix)!" unless(defined($gzip_version));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $gzip_version 0 0 0
550 }
551
552 0 ▶ if($gzip_version =~ m/^Apple/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($gzip_version =~ /^Apple/) { } 0 0 0
553 # Apple gzip requires a suffix
554 0 $data = "gzip -dcq $name.$suffix |";
555 } else {
556 # Other gzips apparently don't require a suffix - they find it automagically.
557 0 $data = "gzip -dcq $name |";
558 }
559  
560 0 $zipt = 1;
561 0 last suffix;
562 }
563 }
564 }
565 11 my ($size) = (stat $name)[7];
566 11 ▶ $d = new FileHandle $data
Branch: 1 branch
BranchTrueFalseTotal
$d = "FileHandle"->new($data) or barf("Couldn't open '${data}' for reading") 0 11 11
567 or barf "Couldn't open '$data' for reading";
568 11 binmode $d;
569 11 ▶ $h = _read_flexhdr("$name.hdr")
Branch: 1 branch
BranchTrueFalseTotal
unless $h 11 0 11
570 unless $h;
571 }
572  
573 # Go through headers which reconfigure
574 11 foreach $hdr (@$h) {
575 11 my ($type) = $hdr->{Type};
576 11 ▶ if ($type eq 'swap') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'swap') { } 0 11 11
elsif ($type ne 'f77') { } 11 0 11
577 0 $swapbyte = 1;
578 } elsif ($type ne 'f77') {
579 11 last;
580 }
581 }
582  
583 READ:
584 11 foreach $hdr (@$h) {
585 12 my ($type) = $hdr->{Type};
586 # Case convert when we have user data
587 12 ▶ $type =~ tr/A-Z/a-z/ if $#_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $#_ == 1 0 12 12
588 12 ▶ if ($newfile) {
Branch: 1 branch
BranchTrueFalseTotal
if ($newfile) 11 1 12
589 11 ▶ if ($type eq 'f77') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'f77') { } 0 11 11
elsif ($type eq 'swap') { } 0 11 11
590 0 $hdr = {
591 Type => $PDL_L,
592 Dims => [ ],
593 NDims => 0
594 };
595 0 $type = $PDL_L;
596 0 $f77mode = 1;
597 } elsif ($type eq 'swap') {
598 0 next READ;
599 } else {
600 11 $newfile = 0;
601 }
602 }
603 12 ▶ if ($#_ == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($#_ == 1) 0 12 12
604 barf("Bad typename '$type' in readflex")
605 0 ▶ if (!defined($flextypes{$type}));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $flextypes{$type} 0 0 0
606 0 $type = $flextypes{$type};
607 }
608 $pdl = PDL->zeroes ((new PDL::Type($type)),
609 12 ▶ ref $hdr->{Dims} ? @{$hdr->{Dims}} : $hdr->{Dims});
Branch: 1 branch
BranchTrueFalseTotal
ref $hdr->{'Dims'} ? : 12 0 12
610 12 $len = length $ {$pdl->get_dataref};
611  
612 12 ▶ &readchunk($d,$pdl,$len,$name) or last READ;
Branch: 1 branch
BranchTrueFalseTotal
&readchunk($d, $pdl, $len, $name) or last READ 0 12 12
613 12 $chunkread += $len;
614 12 ▶ if ($swapbyte) {
Branch: 1 branch
BranchTrueFalseTotal
if ($swapbyte) 0 12 12
615 0 my $method = $flexswap{$type};
616 0 ▶ $pdl->$method if $method;
Branch: 1 branch
BranchTrueFalseTotal
if $method 0 0 0
617 # bswap2($pdl) if $pdl->get_datatype == $PDL_S;
618 # bswap4($pdl) if $pdl->get_datatype == $PDL_L
619 # || $pdl->get_datatype == $PDL_F;
620 # bswap8($pdl) if $pdl->get_datatype == $PDL_D;
621 }
622 12 ▶ if ($newfile && $f77mode) {
Branch: 1 branch
BranchTrueFalseTotal
if ($newfile and $f77mode) 0 12 12
Condition33%1/3
$newfile and $f77mode
!ll&&!rl&&r
1200
MC/DC0%0/2
$newfile and $f77mode
$newfile $f77mode
Truth table33%1/3
A and B
A= $newfileB= $f77mode
ABresult
0X0
100
111
623 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
624 0 $chunk = $pdl->copy;
625 0 $chunkread = 0;
626 0 next READ;
627 } else {
628 SWAP:
629 0 foreach (0,1) {
630 0 seek($d,4,0);
631 0 $swapbyte = $_;
632 0 ▶ bswap4($pdl) if $swapbyte;
Branch: 1 branch
BranchTrueFalseTotal
if $swapbyte 0 0 0
633 0 $chunk = $pdl->copy;
634 0 ▶ next SWAP if ! seek($d,$pdl->at,1);
Branch: 1 branch
BranchTrueFalseTotal
unless seek $d, $pdl->at, 1 0 0 0
635 next SWAP if
636 0 ▶ read($d,$ {$chunk->get_dataref},$len) != $len;
Branch: 1 branch
BranchTrueFalseTotal
if read($d, ${$chunk->get_dataref;}, $len) != $len 0 0 0
637 0 $chunk->upd_data;
638 0 ▶ bswap4($chunk) if $swapbyte;
Branch: 1 branch
BranchTrueFalseTotal
if $swapbyte 0 0 0
639 0 ▶ next SWAP if ($pdl->at != $chunk->at);
Branch: 1 branch
BranchTrueFalseTotal
if $pdl->at != $chunk->at 0 0 0
640 0 $chunkread = 0;
641 0 ▶ barf "Error can't rewind" if !seek($d,4,0);
Branch: 1 branch
BranchTrueFalseTotal
unless seek $d, 4, 0 0 0 0
642 # print "OK".($swapbyte?", swapped":""),"\n";
643 0 next READ;
644 }
645 0 barf "Error: Doesn't look like f77 file (even swapped)";
646 }
647 }
648  
649 12 ▶ if ($hdr->{BadFlag}) { # set badflag and badvalue if needed
Branch: 1 branch
BranchTrueFalseTotal
if ($hdr->{"BadFlag"}) 0 12 12
650 0 $pdl->badflag($hdr->{BadFlag});
651 0 ▶ $pdl->badvalue($hdr->{BadValue}) if defined $hdr->{BadValue};
Branch: 1 branch
BranchTrueFalseTotal
if defined $hdr->{"BadValue"} 0 0 0
652 }
653 12 push (@out,$pdl);
654  
655 12 ▶ if ($f77mode && $chunk->at == $chunkread) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f77mode and $chunk->at == $chunkread) 0 12 12
Condition33%1/3
$f77mode and $chunk->at == $chunkread
!ll&&!rl&&r
1200
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
656 0 $chunkread = 0;
657 0 my ($check) = $chunk->copy;
658 0 ▶ &readchunk($d,$check,4,$name) or last READ;
Branch: 1 branch
BranchTrueFalseTotal
&readchunk($d, $check, 4, $name) or last READ 0 0 0
659 0 ▶ bswap4($check) if $swapbyte;
Branch: 1 branch
BranchTrueFalseTotal
if $swapbyte 0 0 0
660 0 ▶ if ($check->at ne $chunk->at) {
Branch: 1 branch
BranchTrueFalseTotal
if ($check->at ne $chunk->at) 0 0 0
661 0 barf "F77 file format error for $check cf $chunk";
662 0 last READ;
663 }
664 0 ▶ if (!eof($d)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not eof $d) { } 0 0 0
665 0 ▶ &readchunk($d,$chunk,4,$name) or last READ;
Branch: 1 branch
BranchTrueFalseTotal
&readchunk($d, $chunk, 4, $name) or last READ 0 0 0
666 0 ▶ bswap4($chunk) if $swapbyte;
Branch: 1 branch
BranchTrueFalseTotal
if $swapbyte 0 0 0
667 } else {
668 0 last READ;
669 }
670 }
671 }
672 11 ▶ wantarray ? @out : $out[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 10 11
673 }
674  
675 sub mapflex {
676 2 ▶ my ($usage)
Subroutine mapflex: called CC=49 SCAR=56.1
Pod documented
677 = 'Usage ($x,$y,...) = mapflex("filename" [, \@hdr] [,\%opts])';
678 2 my $name = shift;
679 # reference to header array
680 2 my ($h, $size);
681 # reference to options array, with defaults
682 2 my (%opts) = ( 'ReadOnly' => 0, 'Creat' => 0, 'Trunc' => 0 );
683  
684 2 my ($hdr, $d, $pdl, $len, @out, $chunk, $chunkread);
685 2 local ($offset) = 0;
686 2 my ($newfile, $swapbyte, $f77mode, $zipt) = (1,0,0,0);
687  
688 2 foreach (@_) {
689 2 ▶ if (ref($_) eq "ARRAY") {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $_ eq 'ARRAY') { } 1 1 2
elsif (ref $_ eq 'HASH') { } 1 0 1
690 1 $h = $_;
691 } elsif (ref($_) eq "HASH") {
692 1 %opts = (%opts,%$_);
693 } else {
694 0 warn $usage;
695 }
696 }
697  
698 2 ▶ if ($name =~ s/\.gz$// || $name =~ s/\.Z$// ||
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ s/\.gz$// or $name =~ s/\.Z$// or not -e $name and -e $name . '.gz' || -e $name . '.Z') 0 2 2
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
002
$name =~ s/\.gz$// or $name =~ s/\.Z$//
l!l&&r!l&&!r
002
not -e $name and -e $name . '.gz' || -e $name . '.Z'
!ll&&!rl&&r
110
-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 table16%1/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
699 (! -e $name && (-e $name.'.gz' || -e $name.'.Z'))) {
700 0 barf "Can't map compressed file";
701 }
702  
703 2 ▶ if (!defined $h) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $h) 1 1 2
704 1 $h = _read_flexhdr("$name.hdr");
705 }
706  
707 # Go through headers which reconfigure
708 2 foreach $hdr (@$h) {
709 2 my ($type) = $hdr->{Type};
710 2 ▶ if ($type eq 'swap') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'swap') { } 0 2 2
elsif ($type eq 'f77') { } 0 2 2
711 0 barf "Can't map byte swapped file";
712 } elsif ($type eq 'f77') {
713 0 $f77mode = 1;
714 } else {
715 2 my($si) = 1;
716 2 ▶ foreach (ref $hdr->{Dims} ? @{$hdr->{Dims}} : $hdr->{Dims}) {
Branch: 1 branch
BranchTrueFalseTotal
ref $hdr->{'Dims'} ? : 2 0 2
717 4 $si *= $_;
718 }
719 barf("Bad typename '$type' in mapflex")
720 2 ▶ unless defined $flextypes{$type};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $flextypes{$type} 0 2 2
721 2 $type = $flextypes{$type};
722 2 $size += $si * PDL::Core::howbig ($type);
723 }
724 }
725 # $s now contains estimated size of data in header --
726 # setting $f77mode means that it will be 8 x n bigger in reality
727 2 ▶ $size += 8 if ($f77mode);
Branch: 1 branch
BranchTrueFalseTotal
if $f77mode 0 2 2
728 2 ▶ if (!($opts{Creat})) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opts{"Creat"}) 1 1 2
729 1 my ($s) = $size;
730 1 $size = (stat $name)[7];
731 1 ▶ barf "File looks too small ($size cf header $s)" if $size < $s;
Branch: 1 branch
BranchTrueFalseTotal
if $size < $s 0 1 1
732 }
733 # print "Size $size f77mode $f77mode\n";
734  
735 2 $d = PDL->zeroes(byte());
736  
737 # print "Mapping total size $size\n";
738 # use Data::Dumper;
739 # print "Options: ", Dumper(\%opts), "\n";
740 2 ▶ if ($have_file_map and not defined($PDL::force_use_mmap_code) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($have_file_map and not defined $PDL::force_use_mmap_code) { } 2 0 2
Condition33%1/3
$have_file_map and not defined $PDL::force_use_mmap_code
!ll&&!rl&&r
002
MC/DC0%0/2
$have_file_map and not defined $PDL::force_use_mmap_code
$have_file_map not defined $PDL::force_use_mmap_code
Truth table33%1/3
A and not(B)
A= $have_file_mapB= not defined $PDL::force_use_mmap_code
ABresult
0X0
100
111
741 $d->set_data_by_file_map($name,
742 $size,
743 1,
744 ($opts{ReadOnly}?0:1),
745 ($opts{Creat}?1:0),
746 (0644),
747 2 ▶ ($opts{Creat} || $opts{Trunc} ? 1:0)
Branch: 3 branches
BranchTrueFalseTotal
$opts{'ReadOnly'} ? : 0 2 2
$opts{'Creat'} ? : 1 1 2
$opts{'Creat'} || $opts{'Trunc'} ? : 1 1 2
Condition66%2/3
$opts{"Creat"} or $opts{"Trunc"}
l!l&&r!l&&!r
101
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
748 );
749 } else {
750 0 warn "mapflex: direct mmap support being deprecated, please install File::Map\n";
751 $d->set_data_by_mmap($name,
752 $size,
753 1,
754 ($opts{ReadOnly}?0:1),
755 ($opts{Creat}?1:0),
756 (0644),
757 0 ▶ ($opts{Creat} || $opts{Trunc} ? 1:0)
Branch: 3 branches
BranchTrueFalseTotal
$opts{'ReadOnly'} ? : 0 0 0
$opts{'Creat'} ? : 0 0 0
$opts{'Creat'} || $opts{'Trunc'} ? : 0 0 0
Condition0%0/3
$opts{"Creat"} or $opts{"Trunc"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts{"Creat"} or $opts{"Trunc"}
$opts{"Creat"} $opts{"Trunc"}
Truth table0%0/3
A or B
A= $opts{"Creat"}B= $opts{"Trunc"}
ABresult
1X1
011
000
758 );
759 }
760 READ:
761 2 foreach $hdr (@$h) {
762 2 my ($type) = $hdr->{Type};
763 # Case convert when we have user data
764 2 ▶ $type =~ tr/A-Z/a-z/ if $#_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $#_ == 1 1 1 2
765 2 ▶ if ($newfile) {
Branch: 1 branch
BranchTrueFalseTotal
if ($newfile) 2 0 2
766 2 ▶ if ($type eq 'f77') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'f77') { } 0 2 2
767 0 $hdr = {
768 Type => $PDL_L,
769 Dims => [ ],
770 NDims => 0
771 };
772 0 $type = $PDL_L;
773 } else {
774 2 $newfile = 0;
775 }
776 }
777 2 ▶ if ($#_ == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($#_ == 1) 1 1 2
778 barf("Bad typename '$type' in mapflex")
779 1 ▶ unless defined $flextypes{$type};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $flextypes{$type} 0 1 1
780 1 $type = $flextypes{$type};
781 }
782 my $pdl = PDL->zeroes ((new PDL::Type($type)),
783 2 ▶ ref $hdr->{Dims} ? @{$hdr->{Dims}} : $hdr->{Dims});
Branch: 1 branch
BranchTrueFalseTotal
ref $hdr->{'Dims'} ? : 2 0 2
784 2 $len = length $ {$pdl->get_dataref};
785  
786 2 ▶ &mapchunk($d,$pdl,$len,$name) or last READ;
Branch: 1 branch
BranchTrueFalseTotal
&mapchunk($d, $pdl, $len, $name) or last READ 0 2 2
787 2 $chunkread += $len;
788 2 ▶ if ($newfile && $f77mode) {
Branch: 1 branch
BranchTrueFalseTotal
if ($newfile and $f77mode) 0 2 2
Condition33%1/3
$newfile and $f77mode
!ll&&!rl&&r
200
MC/DC0%0/2
$newfile and $f77mode
$newfile $f77mode
Truth table33%1/3
A and B
A= $newfileB= $f77mode
ABresult
0X0
100
111
789 0 ▶ if ($opts{Creat}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{'Creat'}) { } 0 0 0
790 0 $pdl->set(0,$size - 8);
791 } else {
792 0 $chunk = $pdl->copy;
793 }
794 0 $chunkread = 0;
795 0 next READ;
796 }
797  
798 2 ▶ if ($hdr->{BadFlag}) { # set badflag and badvalue if needed
Branch: 1 branch
BranchTrueFalseTotal
if ($hdr->{"BadFlag"}) 0 2 2
799 0 $pdl->badflag($hdr->{BadFlag});
800 0 ▶ $pdl->badvalue($hdr->{BadValue}) if defined $hdr->{BadValue};
Branch: 1 branch
BranchTrueFalseTotal
if defined $hdr->{"BadValue"} 0 0 0
801 }
802 2 push (@out,$pdl);
803  
804 2 ▶ if ($f77mode && $chunk->at == $chunkread) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f77mode and $chunk->at == $chunkread) 0 2 2
Condition33%1/3
$f77mode and $chunk->at == $chunkread
!ll&&!rl&&r
200
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
805 0 $chunkread = 0;
806 0 my ($check) = $chunk->copy;
807 0 ▶ &mapchunk($d,$check,4,$name) or last READ;
Branch: 1 branch
BranchTrueFalseTotal
&mapchunk($d, $check, 4, $name) or last READ 0 0 0
808 0 ▶ if ($opts{Creat}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{'Creat'}) { } 0 0 0
809 0 $check->set(0,$size-8);
810 } else {
811 0 ▶ if ($check->at ne $chunk->at) {
Branch: 1 branch
BranchTrueFalseTotal
if ($check->at ne $chunk->at) 0 0 0
812 0 barf "F77 file format error for $check cf $chunk";
813 0 last READ;
814 }
815 }
816 0 ▶ barf "Will only map first f77 data statement" if ($offset < $size);
Branch: 1 branch
BranchTrueFalseTotal
if $offset < $size 0 0 0
817 0 last READ;
818 }
819 }
820 2 ▶ wantarray ? @out : $out[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 2 2
821 }
822  
823 sub writeflex {
824 9 ▶ my $usage = 'Usage $hdr = writeflex("filename"|FILEHANDLE,$pdl,...)';
Subroutine writeflex: called CC=13 SCAR=30.4
Pod documented
825 9 ▶ barf $usage if $#_<0;
Branch: 1 branch
BranchTrueFalseTotal
if $#_ < 0 0 9 9
826 9 my($name) = shift;
827 9 my $isname = 0;
828 9 my $hdr;
829 my $d;
830  
831 # Test if $name is a file handle
832 9 ▶ if (defined fileno($name)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined fileno $name) { } 0 9 9
833 0 $d = $name;
834 0 binmode $d;
835 }
836 else {
837 9 ▶ barf $usage if ref $name;
Branch: 1 branch
BranchTrueFalseTotal
if ref $name 0 9 9
838 9 $isname = 1;
839 9 ▶ my $modename = ($name =~ /^[+]?[><|]/) ? $name : ">$name";
Branch: 1 branch
BranchTrueFalseTotal
$name =~ /^[+]?[><|]/ ? : 0 9 9
840 9 ▶ $d = new FileHandle $modename
Branch: 1 branch
BranchTrueFalseTotal
$d = "FileHandle"->new($modename) or barf("Couldn't open '${name}' for writing") 0 9 9
841 or barf "Couldn't open '$name' for writing";
842 9 binmode $d;
843 }
844 9 foreach $pdl (@_) {
845 10 ▶ barf $usage if ! ref $pdl;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $pdl 0 10 10
846 # print join(' ',$pdl->getndims,$pdl->dims),"\n";
847 10 ▶ push @{$hdr}, {
Branch: 1 branch
BranchTrueFalseTotal
$pdl->badvalue == $pdl->orig_badvalue ? : 10 0 10
848 10 Type => $flexnames{$pdl->get_datatype},
849 Dims => [ $pdl->dims ],
850 NDims => $pdl->getndims,
851 BadFlag => $pdl->badflag,
852 BadValue => (($pdl->badvalue == $pdl->orig_badvalue) ? undef : $pdl->badvalue),
853 };
854 10 print $d $ {$pdl->get_dataref};
855 }
856 9 ▶ if (defined wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined wantarray) { } 9 0 9
857 # list or scalar context
858 9 ▶ writeflexhdr($name, $hdr) if $isname and $PDL::IO::FlexRaw::writeflexhdr;
Branch: 1 branch
BranchTrueFalseTotal
if $isname and $writeflexhdr 0 9 9
Condition33%1/3
$isname and $writeflexhdr
!ll&&!rl&&r
090
MC/DC0%0/2
$isname and $writeflexhdr
$isname $writeflexhdr
Truth table33%1/3
A and B
A= $isnameB= $writeflexhdr
ABresult
0X0
100
111
859 9 return $hdr;
860 } else {
861 # void context so write header file
862 0 ▶ writeflexhdr($name, $hdr) if $isname;
Branch: 1 branch
BranchTrueFalseTotal
if $isname 0 0 0
863 0 return;
864 }
865 }
866  
867 sub writeflexhdr {
868 10 ▶ barf 'Usage writeflex("filename", $hdr)' if $#_!=1 || !ref $_[1];
Subroutine writeflexhdr: called CC=8 SCAR=23.4
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if $#_ != 1 or not ref $_[1] 0 10 10
Condition33%1/3
$#_ != 1 or not ref $_[1]
l!l&&r!l&&!r
0010
MC/DC0%0/2
$#_ != 1 or not ref $_[1]
$#_ != 1 not ref $_[1]
Truth table33%1/3
A or not(B)
A= $#_ != 1B= not ref $_[1]
ABresult
1X1
011
000
869 10 my($name) = shift; my ($hdr) = shift;
870 10 my $hname = "$name.hdr";
871 10 ▶ my $h = new FileHandle ">$hname"
Branch: 1 branch
BranchTrueFalseTotal
my $h = "FileHandle"->new(">$hname") or barf("Couldn't open '${hname}' for writing") 0 10 10
872 or barf "Couldn't open '$hname' for writing";
873 10 binmode $h;
874 10 print $h
875 "# Output from PDL::IO::writeflex, data in $name\n";
876 10 foreach (@$hdr) {
877 11 my ($type) = $_->{Type};
878 11 ▶ if (! exists $flextypes{$type}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $flextypes{$type}) 0 11 11
879 0 barf "Writeflexhdr: will only print data elements, not $type";
880 0 next;
881 }
882 print $h join("\n",$_->{Type},
883 $_->{NDims},
884 11 ▶ (join ' ',ref $_->{Dims} ? @{$_->{Dims}} : $_->{Dims}) . (($_->{BadFlag}) ? " badvalue $_->{BadValue}" : '')),
Branch: 2 branches
BranchTrueFalseTotal
ref $_->{'Dims'} ? : 11 0 11
$_->{'BadFlag'} ? : 0 11 11
885 "\n\n";
886 }
887 }
888  
889 3 1;
890  
891  
« blib/lib/PDL/IO/Misc.pm ↑ Summary blib/lib/PDL/IO/FITS.pm »