blib/lib/PDL/IO/FITS.pm

filter: Statementstmt 59.9% 644 / 1074 Branchbran 49.1% 298 / 606 Conditioncond 42.5% 103 / 242 MC/DCmcdc 21.5% 30 / 139 Subroutinesub 51.9% 40 / 77 Podpod 30.0% 3 / 10 totaltotal 52.0% 1118 / 2148 CC 422 SCAR 56.0
CC
422
Cov
54%
_rfits_unpack_zimage
756.0
_prep_table
445.9
_rfits_bintable
401.8
CRAP 270.6
« blib/lib/PDL/IO/FlexRaw.pm ↑ Summary blib/lib/PDL/Graphics2D.pm »
1 =head1 NAME
2  
3 PDL::IO::FITS -- Simple FITS support for PDL
4  
5 =head1 SYNOPSIS
6  
7 use PDL;
8 use PDL::IO::FITS;
9  
10 $a = rfits('foo.fits'); # read a FITS file
11 $a->wfits('bar.fits'); # write a FITS file
12  
13 =head1 DESCRIPTION
14  
15 This module provides basic FITS support for PDL, in the sense of
16 reading and writing whole FITS files. (For more complex operations,
17 such as prefiltering rows out of tables or performing operations on
18 the FITS file in-place on disk), you can use the Astro::FITS::CFITSIO
19 module that is available on CPAN.
20  
21 Basic FITS image files are supported, along with BINTABLE and IMAGE extensions.
22 ASCII Table support is planned, as are the HEASARC bintable extensions that
23 are recommended in the 1999 FITS standard.
24  
25 Table support is based on hashes and named columns, rather than the
26 less convenient (but slightly more congruent) technique of perl lists
27 of numbered columns.
28  
29 The principle interface routines are C<rfits> and C<wfits>, for
30 reading and writing respectively. FITS headers are returned as perl
31 hashes or (if the module is present) Astro::FITS::Header objects that
32 are tied to perl hashes. Astro::FITS::Header objects provide
33 convenient access through the tied hash interface, but also allow you
34 to control the card structure in more detail using a separate method
35 interface; see the L<Astro::FITS::Header|Astro::FITS::Header>
36 documentation for details.
37  
38 =head1 AUTHOR
39  
40 Copyright (C) Karl Glazebrook, Craig DeForest, and Doug Burke, 1997-2010.
41 There is no warranty. You are allowed to redistribute and/or modify
42 this software under certain conditions. For details, see the file
43 COPYING in the PDL distribution. If this file is separated from the
44 PDL distribution, the copyright notice should be pasted into in this file.
45  
46 =head1 FUNCTIONS
47  
48 =cut
49  
50 49 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
51  
52 BEGIN {
53  
54 package PDL::IO::FITS;
55  
56 49 ▶ $PDL::IO::FITS::VERSION = 0.92; # Will be 1.0 when ascii table read/write works.
Subroutine BEGIN: called CC=6 SCAR=18.2
57  
58 49 our @EXPORT_OK = qw( rfits rfitshdr wfits );
59 49 our %EXPORT_TAGS = (Func=>[@EXPORT_OK]);
60 49 our @ISA = ('PDL::Exporter');
61  
62 49 ▶ use PDL::Core;
Subroutine BEGIN: called CC=1 SCAR=0.0
63 49 ▶ use PDL::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
64 49 ▶ use PDL::IO::Misc;
Subroutine BEGIN: called CC=1 SCAR=0.0
65 49 ▶ use PDL::Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
66 49 ▶ use PDL::Primitive;
Subroutine BEGIN: called CC=1 SCAR=0.0
67 49 ▶ use PDL::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
68 49 ▶ use PDL::Options;
Subroutine BEGIN: called CC=1 SCAR=0.0
69 49 ▶ use PDL::Bad;
Subroutine BEGIN: called CC=1 SCAR=0.0
70 # use PDL::NiceSlice;
71 49 ▶ use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
72 49 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
73  
74 ##############################
75 #
76 # Check if there's Astro::FITS::Header support, and set flag.
77 # Kludgy but it only has to run once, on first load. --CED
78 #
79 49 ▶ eval "use Astro::FITS::Header;";
Subroutine BEGIN: called CC=1 SCAR=0.0
80 49 $PDL::Astro_FITS_Header = (defined $Astro::FITS::Header::VERSION);
81 49 ▶ if($PDL::Astro_FITS_Header) {
Branch: 1 branch
BranchTrueFalseTotal
if ($PDL::Astro_FITS_Header) 49 0 49
82 49 my($a) = $Astro::FITS::Header::VERSION;
83 49 $a =~ s/[^0-9\.].*//;
84 49 ▶ $PDL::Astro_FITS_Header = 0 if($a < 1.12);
Branch: 1 branch
BranchTrueFalseTotal
if $a < "1.12" 0 49 49
85 }
86  
87 49 ▶ unless($PDL::Astro_FITS_Header) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($PDL::Astro_FITS_Header) 0 49 49
88 0 ▶ unless($ENV{"PDL_FITS_LEGACY"} || $PDL::Config{FITS_LEGACY}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ENV{"PDL_FITS_LEGACY"} or $PDL::Config{"FITS_LEGACY"}) 0 0 0
Condition0%0/3
$ENV{"PDL_FITS_LEGACY"} or $PDL::Config{"FITS_LEGACY"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$ENV{"PDL_FITS_LEGACY"} or $PDL::Config{"FITS_LEGACY"}
$ENV{"PDL_FITS_LEGACY"} $PDL::Config{"FITS_LEGACY"}
Truth table0%0/3
A or B
A= $ENV{"PDL_FITS_LEGACY"}B= $PDL::Config{"FITS_LEGACY"}
ABresult
1X1
011
000
89 0 print(STDERR "\n\nWARNING: Can't find the Astro::FITS::Header module, limiting FITS support.\n\n PDL will use the deprecated legacy perl hash handling code but will not\n properly support tables, FITS extensions, or COMMENT cards. You really\n ought to install the Astro::FITS::Header module, available from\n 'http://www.cpan.org'. (You can also get rid of this message by setting\n the environment variable 'PDL_FITS_LEGACY' or the global PDL config value (in perldl.conf)\n \$PDL::Config{FITS_LEGACY} to 1.\n\n");
90 }
91 }
92 }
93  
94 package PDL::IO::FITS;
95  
96 ## declare subroutines
97  
98 sub _wfits_nullhdu ($);
99 sub _wfits_table ($$$);
100  
101 =head2 rfits()
102  
103 =for ref
104  
105 Simple piddle FITS reader.
106  
107 =for example
108  
109 $pdl = rfits('file.fits'); # Read a simple FITS image
110  
111 Suffix magic:
112  
113 $pdl = rfits('file.fits.gz'); # Read a file with gunzip(1)
114 $pdl = rfits('file.fits.Z'); # Read a file with uncompress(1)
115  
116 $pdl = rfits('file.fits[2]'); # Read 2nd extension
117 $pdl = rfits('file.fits.gz[3]'); # Read 3rd extension
118 @pdls = rfits('file.fits'); # Read primary data and extensions
119  
120 Tilde expansion:
121 #expand leading ~ to home directory (using glob())
122 $pdl = rfits '~/filename.fits';
123  
124 $hdr = rfits('file.fits',{data=>0}); # Options hash changes behavior
125  
126 In list context, C<rfits> reads the primary image and all possible
127 extensions, returning them in the same order that they occurred in the
128 file -- except that, by default, the primary HDU is skipped if it
129 contains no data. In scalar context, the default is to read the first
130 HDU that contains data. One can read other HDU's by using the [n]
131 syntax. Using the [0] syntax forces a read of the first HDU,
132 regardless of whether it contains data or no. Currently recognized
133 extensions are IMAGE and BINTABLE. (See the addendum on EXTENSIONS
134 for details).
135  
136 C<rfits> accepts several options that may be passed in as a hash ref
137 if desired:
138  
139 =over 3
140  
141 =item bscale (default=1)
142  
143 Determines whether the data are linearly scaled using the BSCALE/BZERO keywords
144 in the FITS header. To read in the exact data values in the file, set this
145 to 0.
146  
147 =item data (default=1)
148  
149 Determines whether to read the data, or just the header. If you set this to
150 0, you will get back the FITS header rather than the data themselves. (Note
151 that the header is normally returned as the C<hdr> field of the returned PDL;
152 this causes it to be returned as a hash ref directly.)
153  
154 =item hdrcpy (default=0)
155  
156 Determines whether the L<hdrcpy|PDL::Core/hdrcpy> flag is set in the returned
157 PDL. Setting the flag will cause an explicit deep copy of the header whenever
158 you use the returned PDL in an arithmetic or slicing operation. That is useful
159 in many circumstances but also causes a hit in speed. When two or more PDLs
160 with hdrcpy set are used in an expression, the result gets the header of the
161 first PDL in the expression. See L<hdrcpy|PDL::Core/hdrcpy> for an example.
162  
163 =item expand (default=1)
164  
165 Determines whether auto-expansion of tile-compressed images should happen.
166 Tile-compressed images are transmitted as binary tables with particular
167 fields ("ZIMAGE") set. Leaving this alone does what you want most of the
168 time, unpacking such images transparently and returning the data and header
169 as if they were part of a normal IMAGE extension. Setting "expand" to 0
170 delivers the binary table, rather than unpacking it into an image.
171  
172 =item afh (default=1)
173  
174 By default rfits uses Astro::FITS::Header tied-hash objects to contain
175 the FITS header information. This permits explicit control over FITS
176 card information, and conforms well with the FITS specification. But
177 Astro::FITS::Header objects are about 40-60x more memory intensive
178 than comparable perl hashes, and also use ~10x more CPU to manage.
179 For jobs where header processing performance is important (e.g. reading
180 just the headers of 1,000 FITS files), set afh to 0 to use the legacy parser
181 and get a large boost in speed.
182  
183 =back
184  
185 FITS image headers are stored in the output PDL and can be retrieved
186 with L<hdr|PDL::Core/hdr> or L<gethdr|PDL::Core/gethdr>. The
187 L<hdrcpy|PDL::Core/hdrcpy> flag of the PDL is set so that the header
188 is copied to derived piddles by default. (This is inefficient if you
189 are planning to do lots of small operations on the data; clear
190 the flag with "->hcpy(0)" or via the options hash if that's the case.)
191  
192 The header is a hash whose keys are the keywords in the FITS header.
193 If you have the "Astro::FITS::Header" module installed, the header is
194 actually a tied hash to a FITS header object, which can give you
195 more control over card order, comment fields, and variable types.
196 (see L<Astro::FITS::Header> for details).
197  
198 The header keywords are converted to I<uppercase> per the FITS
199 standard. Access is case-insensitive on the perl side, provided that
200 Astro::FITS::Header is installed.
201  
202 If Astro::FITS::Header is not installed, then a built-in legacy parser
203 is used to generate the header hash. Keyword-associated comments in
204 the headers are stored under the hash key
205 C<< <keyword>_COMMENT> >>. All HISTORY cards in the header are
206 collected into a single multiline string stored in the C<HISTORY> key.
207 All COMMENT cards are similarly collected under the C<COMMENT> key.
208  
209 =head3 BSCALE/BZERO
210  
211 If the BSCALE and/or BZERO keywords are set, they are applied to the
212 image before it is returned. The returned PDL is promoted as
213 necessary to contain the multiplied values, and the BSCALE and BZERO
214 keywords are deleted from the header for clarity. If you don't want
215 this type of processing, set 'bscale=>0' in the options hash.
216  
217 =head3 EXTENSIONS
218  
219 Sometimes a FITS file contains only extensions and a stub header in
220 the first header/data unit ("primary HDU"). In scalar context, you
221 normally only get back the primary HDU -- but in this special case,
222 you get back the first extension HDU. You can force a read of the
223 primary HDU by adding a '[0]' suffix to the file name.
224  
225 =head3 BINTABLE EXTENSIONS
226  
227 Binary tables are handled. Currently only the following PDL
228 datatypes are supported: byte, short, ushort, long, float, and
229 double. At present ushort() data is written as a long rather than
230 as a short with TSCAL/ZERO; this may change.
231  
232 The return value for a binary table is a hash ref containing the names
233 of the columns in the table (in UPPER CASE as per the FITS standard).
234 Each element of the hash contains a PDL (for numerical values) or a
235 perl list (for string values). The PDL's 0th dimension runs across
236 rows; the 1st dimension runs across the repeat index within the row
237 (for rows with more than one value). (Note that this is different from
238 standard threading order - but it allows Least Surprise to work when
239 adding more complicated objects such as collections of numbers (via
240 the repeat count) or variable length arrays.)
241  
242 Thus, if your table contains a column named C<FOO> with type C<5D>,
243 the expression
244  
245 $a->{FOO}->((2))
246  
247 returns a 5-element double-precision PDL containing the values of FOO
248 from the third row of the table.
249  
250 The header of the table itself is parsed as with a normal FITS HDU,
251 and is returned in the element 'hdr' of the returned hash. You can
252 use that to preserve the original column order or access the table at a low
253 level, if you like.
254  
255 Scaling and zero-point adjustment are performed as with BSCALE/BZERO:
256 the appropriate keywords are deleted from the as-returned header. To avoid
257 this behavior, set 'bscale=>0' in the options hash.
258  
259 As appropriate, TSCAL/ZERO and TUNIT are copied into each column-PDL's
260 header as BSCALE/BZERO and BUNIT.
261  
262 The main hash also contains the element 'tbl', which is set
263 to 'binary' to distinguish it from an ASCII table.
264  
265 Because different columns in the table might have identical names in a
266 FITS file, the binary table reader practices collision avoidance. If
267 you have multiple columns named "FOO", then the first one encountered
268 (numerically) gets the name "FOO", the next one gets "FOO_1", and the
269 next "FOO_2", etc. The appropriate TTYPEn fields in the header are
270 changed to match the renamed column fields.
271  
272 Columns with no name are assigned the name "COL_<n>", where <n> starts
273 at 1 and increments for each no-name column found.
274  
275 Variable-length arrays are supported for reading. They are unpacked
276 into PDLs that appear exactly the same as the output for fixed-length
277 rows, except that each row is padded to the maximum length given
278 in the extra characters -- e.g. a row with TFORM of 1PB(300) will
279 yield an NAXIS2x300 output field in the final hash. The padding
280 uses the TNULL<n> keyword for the column, or 0 if TNULL<n> is not
281 present. The output hash also gets an additional field, "len_<name>",
282 that contains the number of elements in each table row.
283  
284 =head3 TILE-COMPRESSED IMAGES
285  
286 CFITSIO and several large projects (including NASA's Solar Dynamics
287 Observatory) now support an unofficial extension to FITS that stores
288 images as a collection of individually compressed tiles within a
289 BINTABLE extension. These images are automagically uncompressed by
290 default, and delivered as if they were normal image files. You can
291 override this behavior by supplying the "expand" key in the options hash.
292  
293 Currently, only Rice compression is supported, though there is a framework
294 in place for adding other compression schemes.
295  
296 =for bad
297  
298 =head3 BAD VALUE HANDLING
299  
300 If a FITS file contains the C<BLANK> keyword (and has C<BITPIX E<gt> 0>),
301 the piddle will have its bad flag set, and those elements which equal the
302 C<BLANK> value will be set bad. For C<BITPIX E<lt> 0>, any NaN's are
303 converted to bad (if necessary).
304  
305  
306 =head2 rfitshdr()
307  
308 =for ref
309  
310 Read only the header of a FITS file or an extension within it.
311  
312 This is syntactic sugar for the C<data=E<gt>0> option to L<rfits|/rfits()>.
313  
314 See L<rfits|/rfits()> for details on header handling. rfitshdr() runs
315 the same code to read the header, but returns it rather than
316 reading in a data structure as well.
317  
318 =cut
319  
320 49 our $rfits_options = new PDL::Options( { bscale=>1, data=>1, hdrcpy=>0, expand=>1, afh=>1 } );
321  
322 sub PDL::rfitshdr {
323 0 ▶ my $class = shift;
Subroutine rfitshdr: not called CC=1 SCAR=6.9
Pod undocumented
324 0 my $file = shift;
325 0 my $u_opt = ifhref(shift);
326 0 $u_opt->{data} = 0;
327 0 PDL::rfits($class,$file,$u_opt);
328 }
329  
330 sub PDL::rfits {
331
332 46 ▶ my $class = shift;
Subroutine rfits: called CC=61 SCAR=58.0
Pod undocumented
333
334 46 ▶ barf 'Usage: $a = rfits($file) -or- $a = PDL->rfits($file)' if (@_ < 1 || @_ > 2);
Branch: 1 branch
BranchTrueFalseTotal
if @_ < 1 or @_ > 2 0 46 46
Condition33%1/3
@_ < 1 or @_ > 2
l!l&&r!l&&!r
0046
MC/DC0%0/2
@_ < 1 or @_ > 2
@_ < 1 @_ > 2
Truth table33%1/3
A or B
A= @_ < 1B= @_ > 2
ABresult
1X1
011
000
335
336 46 my $file = shift;
337
338 46 my $u_opt = ifhref(shift);
339 46 my $opt = $rfits_options->options($u_opt);
340
341 46 my($nbytes, $line, $name, $rest, $size, $i, $bscale, $bzero, $extnum);
342  
343 46 $nbytes = 0;
344  
345 # Modification 02/04/2005 - JB. Earlier version stripped the extension
346 # indicator which cancelled the check for empty primary data array at the end.
347 46 ▶ my $explicit_extension = ($file =~ m/\[\d+\]$/ ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$file =~ /\[\d+\]$/ ? : 3 43 46
348 46 ▶ $extnum = ( ($file =~ s/\[(\d+)\]$//) ? $1 : 0 );
Branch: 1 branch
BranchTrueFalseTotal
$file =~ s/\[(\d+)\]$// ? : 3 43 46
349  
350 46 $file =~ s/^(~)/glob($1)/e; #tilde expansion.
351  
352 46 ▶ $file = "gunzip -c $file |" if $file =~ /\.gz$/; # Handle compression
Branch: 1 branch
BranchTrueFalseTotal
if $file =~ /\.gz$/ 0 46 46
353 46 ▶ $file = "uncompress -c $file |" if $file =~ /\.Z$/;
Branch: 1 branch
BranchTrueFalseTotal
if $file =~ /\.Z$/ 0 46 46
354
355 46 ▶ my $fh = IO::File->new( $file )
Branch: 1 branch
BranchTrueFalseTotal
my $fh = "IO::File"->new($file) or barf("FITS file $file not found") 0 46 46
356 or barf "FITS file $file not found";
357 46 binmode $fh;
358  
359 46 my @extensions; # This accumulates the list in list context...
360 46 my $currentext=0;
361 46 my $pdl;
362  
363 46 ▶ hdu:{do { # Runs over extensions, in list context
Branch: 1 branch
BranchTrueFalseTotal
wantarray and not $fh->eof and do { do { my $ext_type = "IMAGE"; my $foo = {}; my(@history) = (); my(@cards) = (); $pdl = $class->new; if (wantarray and not $opt->{"data"} and @extensions) { while ($fh->read($line, 80) and not $line =~ /^XTENSION=/ and not $fh->eof) { $fh->read($line, 2800); } ; return @extensions if $fh->eof; } else { my $ct = $fh->read($line, 80); barf("file $file is not in FITS-format:\n$line\n") if $nbytes == 0 and not $line =~ /^SIMPLE = +T/; last hdu if $fh->eof or not $ct; } ; $nbytes = 80; if ($line =~ /^XTENSION= \'(\w+)\s*\'/) { $ext_type = $1; } elsif (@extensions) { print "Warning: expected XTENSION, found '${line}'. Exiting.\n" if $PDL::verbose; last hdu; } ; push @cards, $line if $PDL::Astro_FITS_Header; if (not wantarray and $currentext != $extnum) { skipper: while (1) { $nbytes += $fh->read($line, 2800); barf("Unexpected end of FITS file\n") if $fh->eof; $nbytes += $fh->read($line, 80); barf("Unexpected end of FITS file\n") if $fh->eof; ++$currentext if $line =~ /^XTENSION\= \'(\w+)\s*\'/; if ($currentext == $extnum) { $ext_type = $1; last skipper; } ; } ; } ; if ($PDL::Astro_FITS_Header and $opt->{"afh"}) { do { $nbytes += $fh->read($line, 80); push @cards, $line } until $fh->eof or $line =~ /^END(\s|\000)/; $nbytes += $fh->read(my $dummy, 2879 - ($nbytes - 1) % 2880); my($hdr) = "Astro::FITS::Header"->new("Cards", \@cards); my %hdrhash; tie %hdrhash, "Astro::FITS::Header", $hdr; $foo = \%hdrhash; } else { hdr_legacy: { do { no strict 'refs'; if (substr($line, 0, 8) ne " ") { $name = (split(' ', substr($line, 0, 8), 0))[0]; $rest = substr($line, 8); if ($name =~ /^HISTORY/) { push @history, $rest; } else { $foo->{$name} = ""; $foo->{$name} = $1 if $rest =~ m[^= +([^\/\' ][^\/ ]*) *( +/(.*))?$]; $foo->{$name} = $1 if $rest =~ m[^= \'(.*)\' *( +/(.*))?$]; $foo->{"COMMENT"}{$name} = $3 if defined $3; } ; } ; last hdr_legacy if defined $name and $name eq "END"; $nbytes += $fh->read($line, 80) } until $fh->eof; } ; $foo->{"HISTORY"} = \@history if $#history >= 0; my $skip = 2879 - ($nbytes - 1) % 2880; $fh->read(my $dummy, $skip) if $skip; $nbytes += $skip; } ; if (not defined $foo->{"XTENSION"} and $foo->{"NAXIS"} == 0 and not wantarray and not $explicit_extension) { print "rfits: Skipping null primary HDU (use [0] to force read of primary)...\n" if $PDL::verbose; return PDL::rfits($class, $file . '[1]', $opt); } ; if (not $opt->{"data"} or $foo->{"NAXIS"} == 0) { $pdl = $foo; } else { if (ref $PDL::IO::FITS::_Extension->{$ext_type}) { $pdl = &{$PDL::IO::FITS::_Extension->{$ext_type};}($fh, $foo, $opt, $pdl); } else { print STDERR "rfits: Ignoring unknown extension '${ext_type}'...\n" if $PDL::verbose or $PDL::debug; $pdl = undef; } ; } ; push @extensions, $pdl if wantarray; ++$currentext } } 2 43 45
Condition100%3/3
wantarray and not $fh->eof
!ll&&!rl&&r
4212
MC/DC100%2/2
wantarray and not $fh->eof
wantarray not $fh->eof
Truth table100%3/3
A and not(B)
A= wantarrayB= not $fh->eof
ABresult
0X0
100
111
364 48 my $ext_type = 'IMAGE'; # Gets the type of XTENSION if one is detected.
365 48 my $foo={}; # To go in pdl
366 48 my @history=();
367 48 my @cards = ();
368
369 48 $pdl = $class->new;
370
371  
372 # If $opt->{data} is false, then the reading routines leave the
373 # file alone, so the file pointer is left at the end of the last
374 # header. Skip over the unread data to the next extension...
375
376 48 ▶ if( wantarray and !$opt->{data} and @extensions) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray and not $opt->{'data'} and @extensions) { } 0 48 48
Condition50%3/6
wantarray and not $opt->{"data"} and @extensions
!ll&&!rl&&r
4800
wantarray and not $opt->{"data"}
!ll&&!rl&&r
4530
MC/DC0%0/3
wantarray and not $opt->{"data"} and @extensions
wantarray not $opt->{"data"} @extensions
Truth table50%2/4
A and not(B) and C
A= wantarrayB= not $opt->{"data"}C= @extensions
ABCresult
0XX0
10X0
1100
1111
377 0 ▶ while( $fh->read($line,80) && ($line !~ /^XTENSION=/) && !$fh->eof() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fh->read($line, 80) and not $line =~ /^XTENSION=/ and not $fh->eof) 0 0 0
Condition0%0/6
$fh->read($line, 80) and not $line =~ /^XTENSION=/ and not $fh->eof
!ll&&!rl&&r
000
$fh->read($line, 80) and not $line =~ /^XTENSION=/
!ll&&!rl&&r
000
MC/DC0%0/3
$fh->read($line, 80) and not $line =~ /^XTENSION=/ and not $fh->eof
$fh->read($line, 80) not $line =~ /^XTENSION=/ not $fh->eof
Truth table0%0/4
A and not(B) and not(C)
A= $fh->read($line, 80)B= not $line =~ /^XTENSION=/C= not $fh->eof
ABCresult
0XX0
10X0
1100
1111
378 0 $fh->read($line,2880-80);
379 };
380
381 return @extensions
382 0 ▶ if($fh->eof());
Branch: 1 branch
BranchTrueFalseTotal
if $fh->eof 0 0 0
383  
384 } else {
385 48 my $ct = $fh->read($line,80);
386 48 ▶ barf "file $file is not in FITS-format:\n$line\n"
Branch: 1 branch
BranchTrueFalseTotal
if $nbytes == 0 and not $line =~ /^SIMPLE = +T/ 0 48 48
Condition66%2/3
$nbytes == 0 and not $line =~ /^SIMPLE = +T/
!ll&&!rl&&r
2460
MC/DC0%0/2
$nbytes == 0 and not $line =~ /^SIMPLE = +T/
$nbytes == 0 not $line =~ /^SIMPLE = +T/
Truth table66%2/3
A and not(B)
A= $nbytes == 0B= not $line =~ /^SIMPLE = +T/
ABresult
0X0
100
111
387 if( $nbytes==0 && ($line !~ /^SIMPLE = +T/));
388 48 ▶ last hdu if($fh->eof() || !$ct);
Branch: 1 branch
BranchTrueFalseTotal
if $fh->eof or not $ct 0 48 48
Condition33%1/3
$fh->eof or not $ct
l!l&&r!l&&!r
0048
MC/DC0%0/2
$fh->eof or not $ct
$fh->eof not $ct
Truth table33%1/3
A or not(B)
A= $fh->eofB= not $ct
ABresult
1X1
011
000
389 }
390  
391 48 $nbytes = 80; # Number of bytes read from this extension (1 line so far)
392
393 48 ▶ if($line =~ /^XTENSION= \'(\w+)\s*\'/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($line =~ /^XTENSION= \'(\w+)\s*\'/) { } 2 46 48
elsif (@extensions) { } 0 46 46
394 2 $ext_type = $1;
395 } elsif( @extensions ) {
396  
397 0 ▶ print "Warning: expected XTENSION, found '$line'. Exiting.\n"
Branch: 1 branch
BranchTrueFalseTotal
if $PDL::verbose 0 0 0
398 if($PDL::verbose);
399 0 last hdu;
400 }
401  
402 48 ▶ push(@cards,$line) if($PDL::Astro_FITS_Header);
Branch: 1 branch
BranchTrueFalseTotal
if $PDL::Astro_FITS_Header 48 0 48
403
404 #
405 # If we are in scalar context, skip to the desired extension
406 # number. [This implementation is really slow since we have
407 # to read the whole file. Someone Really Ought To rework it to
408 # read individual headers and skip forward an extension at a
409 # a time with seek() calls. ]
410 # --CD
411 #
412
413 48 ▶ if(!wantarray and $currentext != $extnum) {
Branch: 1 branch
BranchTrueFalseTotal
if (not wantarray and $currentext != $extnum) 3 45 48
Condition100%3/3
not wantarray and $currentext != $extnum
!ll&&!rl&&r
3423
MC/DC100%2/2
not wantarray and $currentext != $extnum
not wantarray $currentext != $extnum
Truth table100%3/3
not(A) and B
A= not wantarrayB= $currentext != $extnum
ABresult
0X0
100
111
414  
415 3 skipper: while(1) {
416 # Move to next record
417 3 $nbytes += $fh->read($line,2880-80);
418 3 ▶ barf "Unexpected end of FITS file\n" if $fh->eof();
Branch: 1 branch
BranchTrueFalseTotal
if $fh->eof 0 3 3
419 # Read start of next record
420 3 $nbytes += $fh->read($line,80);
421 3 ▶ barf "Unexpected end of FITS file\n" if $fh->eof();
Branch: 1 branch
BranchTrueFalseTotal
if $fh->eof 0 3 3
422 # Check if we have found the new extension
423 # if not move on
424  
425 3 ▶ $currentext++ if $line =~ /^XTENSION\= \'(\w+)\s*\'/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^XTENSION\= \'(\w+)\s*\'/ 3 0 3
426 3 ▶ if ($currentext == $extnum) {
Branch: 1 branch
BranchTrueFalseTotal
if ($currentext == $extnum) 3 0 3
427 3 $ext_type = $1;
428 3 last skipper;
429 }
430 }
431 } # End of skipping to desired extension
432
433 #
434 # Snarf up the found header, and parse it if Astro::FITS::Header
435 # does not exist.
436 #
437  
438 48 ▶ if($PDL::Astro_FITS_Header and $opt->{afh}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($PDL::Astro_FITS_Header and $opt->{'afh'}) { } 48 0 48
Condition33%1/3
$PDL::Astro_FITS_Header and $opt->{"afh"}
!ll&&!rl&&r
0048
MC/DC0%0/2
$PDL::Astro_FITS_Header and $opt->{"afh"}
$PDL::Astro_FITS_Header $opt->{"afh"}
Truth table33%1/3
A and B
A= $PDL::Astro_FITS_HeaderB= $opt->{"afh"}
ABresult
0X0
100
111
439  
440 ## Astro::FITS::Header parsing. Snarf lines to the END card,
441 ## and pass them to Astro::FITS::Header.
442  
443 48 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$fh->eof or $line =~ /^END(\s|\000)/ or do { do { $nbytes += $fh->read($line, 80); push @cards, $line } } 372 48 420
Condition66%2/3
$fh->eof or $line =~ /^END(\s|\000)/
l!l&&r!l&&!r
048372
MC/DC50%1/2
$fh->eof or $line =~ /^END(\s|\000)/
$fh->eof $line =~ /^END(\s|\000)/
Truth table66%2/3
A or B
A= $fh->eofB= $line =~ /^END(\s|\000)/
ABresult
1X1
011
000
444 420 $nbytes += $fh->read($line, 80);
445 420 push(@cards,$line);
446 } while(!$fh->eof() && $line !~ m/^END(\s|\000)/);
447  
448 48 $nbytes += $fh->read(my $dummy, 2879 - ($nbytes-1)%2880);
449  
450 48 my($hdr) = Astro::FITS::Header->new(Cards => \@cards);
451 48 my(%hdrhash);
452 48 tie %hdrhash,"Astro::FITS::Header",$hdr;
453 48 $foo = \%hdrhash;
454
455 } else {
456
457 ## Legacy (straight header-to-hash-ref) parsing.
458 ## Cheesy but fast.
459
460 0 ▶ hdr_legacy: { do {
Branch: 1 branch
BranchTrueFalseTotal
$fh->eof or do { do { no strict 'refs'; if (substr($line, 0, 8) ne " ") { $name = (split(' ', substr($line, 0, 8), 0))[0]; $rest = substr($line, 8); if ($name =~ /^HISTORY/) { push @history, $rest; } else { $foo->{$name} = ""; $foo->{$name} = $1 if $rest =~ m[^= +([^\/\' ][^\/ ]*) *( +/(.*))?$]; $foo->{$name} = $1 if $rest =~ m[^= \'(.*)\' *( +/(.*))?$]; $foo->{"COMMENT"}{$name} = $3 if defined $3; } ; } ; last hdr_legacy if defined $name and $name eq "END"; $nbytes += $fh->read($line, 80) } } 0 0 0
461 49 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
462 # skip if the first eight characters are ' '
463 # - as seen in headers from the DSS at STScI
464 0 ▶ if (substr($line,0,8) ne " " x 8) { # If non-blank
Branch: 1 branch
BranchTrueFalseTotal
if (substr($line, 0, 8) ne " ") 0 0 0
465
466 0 $name = (split(' ',substr($line,0,8)))[0];
467  
468 0 $rest = substr($line,8);
469
470 0 ▶ if ($name =~ m/^HISTORY/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /^HISTORY/) { } 0 0 0
471 0 push @history, $rest;
472 } else {
473 0 $$foo{$name} = "";
474
475 0 ▶ $$foo{$name}=$1 if $rest =~ m|^= +([^\/\' ][^\/ ]*) *( +/(.*))?$| ;
Branch: 1 branch
BranchTrueFalseTotal
if $rest =~ m[^= +([^\/\' ][^\/ ]*) *( +/(.*))?$] 0 0 0
476 0 ▶ $$foo{$name}=$1 if $rest =~ m|^= \'(.*)\' *( +/(.*))?$| ;
Branch: 1 branch
BranchTrueFalseTotal
if $rest =~ m[^= \'(.*)\' *( +/(.*))?$] 0 0 0
477 0 ▶ $$foo{COMMENT}{$name} = $3 if defined($3);
Branch: 1 branch
BranchTrueFalseTotal
if defined $3 0 0 0
478 }
479 } # non-blank
480 0 ▶ last hdr_legacy if ((defined $name) && $name eq "END");
Branch: 1 branch
BranchTrueFalseTotal
if defined $name and $name eq "END" 0 0 0
Condition0%0/3
defined $name and $name eq "END"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $name and $name eq "END"
defined $name $name eq "END"
Truth table0%0/3
A and B
A= defined $nameB= $name eq "END"
ABresult
0X0
100
111
481 0 $nbytes += $fh->read($line, 80);
482 } while(!$fh->eof()); }
483  
484 # Clean up HISTORY card
485 0 ▶ $$foo{"HISTORY"} = \@history if $#history >= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $#history >= 0 0 0 0
486
487 # Step to end of header block in file
488 0 my $skip = 2879 - ($nbytes-1)%2880;
489 0 ▶ $fh->read(my $dummy, $skip) if $skip;
Branch: 1 branch
BranchTrueFalseTotal
if $skip 0 0 0
490 0 $nbytes += $skip;
491  
492 } # End of legacy header parsing
493
494  
495
496 ##############################
497 # Special case: if the file only contains
498 # extensions then read the first extension in scalar context,
499 # instead of the null zeroth extension.
500 #
501 48 ▶ if( !(defined $foo->{XTENSION}) # Primary header
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $foo->{"XTENSION"} and $foo->{"NAXIS"} == 0 and not wantarray and not $explicit_extension) 3 45 48
Condition88%8/9
not defined $foo->{"XTENSION"} and $foo->{"NAXIS"} == 0 and not wantarray and not $explicit_extension
!ll&&!rl&&r
4503
not defined $foo->{"XTENSION"} and $foo->{"NAXIS"} == 0 and not wantarray
!ll&&!rl&&r
4413
not defined $foo->{"XTENSION"} and $foo->{"NAXIS"} == 0
!ll&&!rl&&r
2424
MC/DC75%3/4
not defined $foo->{"XTENSION"} and $foo->{"NAXIS"} == 0 and not wantarray and not $explicit_extension
not defined $foo->{"XTENSION"} $foo->{"NAXIS"} == 0 not wantarray not $explicit_extension
Truth table80%4/5
not(A) and B and not(C) and not(D)
A= not defined $foo->{"XTENSION"}B= $foo->{"NAXIS"} == 0C= not wantarrayD= not $explicit_extension
ABCDresult
0XXX0
10XX0
110X0
11100
11111
502 and $foo->{NAXIS} == 0 # No data
503 and !wantarray # Scalar context
504 and !$explicit_extension # No HDU specifier
505 ) {
506 3 ▶ print "rfits: Skipping null primary HDU (use [0] to force read of primary)...\n"
Branch: 1 branch
BranchTrueFalseTotal
if $PDL::verbose 0 3 3
507 if($PDL::verbose);
508 3 return PDL::rfits($class,$file.'[1]',$opt);
509 }
510
511  
512 ##########
513 # If we don't want data, return the header from the HDU. Likewise,
514 # if NAXIS is 0 then there are no data, so return the header instead.
515 45 ▶ if( ! $opt->{data} || $foo->{NAXIS}==0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $opt->{'data'} or $foo->{'NAXIS'} == 0) { } 1 44 45
Condition66%2/3
not $opt->{"data"} or $foo->{"NAXIS"} == 0
l!l&&r!l&&!r
0144
MC/DC50%1/2
not $opt->{"data"} or $foo->{"NAXIS"} == 0
not $opt->{"data"} $foo->{"NAXIS"} == 0
Truth table66%2/3
not(A) or B
A= not $opt->{"data"}B= $foo->{"NAXIS"} == 0
ABresult
1X1
011
000
516 # If we're NOT reading data, then return the header instead of the
517 # image.
518  
519 1 $pdl = $foo;
520  
521 } else {
522
523 ##########
524 # Switch based on extension type to do the dirty work of reading
525 # the data. Handlers are listed in the _Extension patch-panel.
526
527 44 ▶ if (ref $PDL::IO::FITS::_Extension->{$ext_type} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $PDL::IO::FITS::_Extension->{$ext_type}) { } 44 0 44
528
529 # Pass $pdl into the extension reader for easier use -- but
530 # it just gets overwritten (and disappears) if ignored.
531
532 44 $pdl = &{$PDL::IO::FITS::_Extension->{$ext_type}}($fh,$foo,$opt,$pdl);
533
534 } else {
535  
536 0 ▶ print STDERR "rfits: Ignoring unknown extension '$ext_type'...\n"
Branch: 1 branch
BranchTrueFalseTotal
if $PDL::verbose or $PDL::debug 0 0 0
Condition0%0/3
$PDL::verbose or $PDL::debug
l!l&&r!l&&!r
000
MC/DC0%0/2
$PDL::verbose or $PDL::debug
$PDL::verbose $PDL::debug
Truth table0%0/3
A or B
A= $PDL::verboseB= $PDL::debug
ABresult
1X1
011
000
537 if($PDL::verbose || $PDL::debug);
538
539 0 $pdl = undef;
540  
541 }
542 }
543  
544 #
545 # Note -- $pdl isn't necessarily a PDL. It's only a $pdl if
546 # the extension was an IMAGE.
547 #
548 45 ▶ push(@extensions,$pdl) if(wantarray);
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 3 42 45
549 45 $currentext++;
550
551 } while( wantarray && !$fh->eof() );} # Repeat if we are in list context
552
553 43 $fh->close;
554
555 43 ▶ if(wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) 1 42 43
556 ## By default, ditch primary HDU placeholder
557 1 ▶ if( ref($extensions[0]) eq 'HASH' and
Branch: 1 branch
BranchTrueFalseTotal
if (ref $extensions[0] eq "HASH" and $extensions[0]{"SIMPLE"} and exists $extensions[0]{"NAXIS"} and $extensions[0]{"NAXIS"} == 0) 1 0 1
Condition33%3/9
ref $extensions[0] eq "HASH" and $extensions[0]{"SIMPLE"} and exists $extensions[0]{"NAXIS"} and $extensions[0]{"NAXIS"} == 0
!ll&&!rl&&r
001
ref $extensions[0] eq "HASH" and $extensions[0]{"SIMPLE"} and exists $extensions[0]{"NAXIS"}
!ll&&!rl&&r
001
ref $extensions[0] eq "HASH" and $extensions[0]{"SIMPLE"}
!ll&&!rl&&r
001
MC/DC0%0/4
ref $extensions[0] eq "HASH" and $extensions[0]{"SIMPLE"} and exists $extensions[0]{"NAXIS"} and $extensions[0]{"NAXIS"} == 0
ref $extensions[0] eq "HASH" $extensions[0]{"SIMPLE"} exists $extensions[0]{"NAXIS"} $extensions[0]{"NAXIS"} == 0
Truth table20%1/5
A and B and C and D
A= ref $extensions[0] eq "HASH"B= $extensions[0]{"SIMPLE"}C= exists $extensions[0]{"NAXIS"}D= $extensions[0]{"NAXIS"} == 0
ABCDresult
0XXX0
10XX0
110X0
11100
11111
558 $extensions[0]->{SIMPLE} and
559 exists($extensions[0]->{NAXIS}) and
560 $extensions[0]->{NAXIS} == 0
561 ) {
562 1 shift @extensions;
563 }
564 # Return all the extensions
565 1 return @extensions;
566 }
567 42 return $pdl;
568 }
569  
570  
571 23 ▶sub rfits { PDL->rfits(@_); }
Subroutine rfits: called CC=1 SCAR=0.0
Pod documented
572  
573 sub rfitshdr {
574 0 ▶ my($file,$opt) = shift;
Subroutine rfitshdr: not called CC=1 SCAR=6.9
Pod documented
575 0 $opt->{data} =0;
576 0 PDL->rfitshdr($file,$opt);
577 }
578  
579  
580 ##############################
581 #
582 # FITS extensions patch-table links extension name to the supported reader.
583 # IMAGE extensions are a special case that gets read just like a normal
584 # FITS file.
585 #
586  
587 49 $PDL::IO::FITS::_Extension = {
588 IMAGE => \&_rfits_image
589 , BINTABLE => \&_rfits_bintable
590 };
591  
592  
593
594 ##############################
595 #
596 # IMAGE extension -- this is also the default reader.
597 49 our $type_table = {
598 8=>$PDL_B,
599 16=>$PDL_S,
600 32=>$PDL_L,
601 64=>$PDL_LL,
602 -32=>$PDL_F,
603 -64=>$PDL_D
604 };
605  
606 49 our $type_table_2 = {
607 8=>byte,
608 16=>short,
609 32=>long,
610 64=>longlong,
611 -32=>float,
612 -64=>double
613 };
614  
615 sub _rfits_image($$$$) {
616 41 ▶ print "Reading IMAGE data...\n" if($PDL::verbose);
Subroutine _rfits_image: called CC=14 SCAR=26.7
Branch: 1 branch
BranchTrueFalseTotal
if $PDL::verbose 0 41 41
617 41 my $fh = shift; # file handle to read from
618 41 my $foo = shift; # $foo contains the pre-read header
619 41 my $opt = shift; # $opt contains the option hash
620 41 my $pdl = shift; # $pdl contains a pre-blessed virgin PDL
621  
622 # Setup piddle structure
623
624 41 ▶ if( defined($type_table->{0 + $foo->{"BITPIX"}}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $$PDL::IO::FITS::type_table{0 + $foo->{'BITPIX'}}) { } 41 0 41
625 41 $pdl->set_datatype( $type_table->{$foo->{"BITPIX"}} );
626 } else {
627 0 die("rfits: strange BITPIX value ".$foo->{"BITPIX"}." in header - I give up!\n");
628 }
629
630 41 my @dims; # Store the dimenions 1..N, compute total number of pixels
631 41 my $i = 1;
632 41 my $size = 1;
633  
634 ##second part of the conditional guards against a poorly-written hdr.
635 41 ▶ while(defined( $$foo{"NAXIS$i"} ) && $i <= $$foo{"NAXIS"}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $$foo{"NAXIS$i"} and $i <= $foo->{"NAXIS"}) 69 41 110
Condition66%2/3
defined $$foo{"NAXIS$i"} and $i <= $foo->{"NAXIS"}
!ll&&!rl&&r
41069
MC/DC50%1/2
defined $$foo{"NAXIS$i"} and $i <= $foo->{"NAXIS"}
defined $$foo{"NAXIS$i"} $i <= $foo->{"NAXIS"}
Truth table66%2/3
A and B
A= defined $$foo{"NAXIS$i"}B= $i <= $foo->{"NAXIS"}
ABresult
0X0
100
111
636 69 $size *= $$foo{"NAXIS$i"};
637 69 push @dims, $$foo{"NAXIS$i"} ; $i++;
638 }
639 41 $pdl->setdims([@dims]);
640
641 41 my $dref = $pdl->get_dataref();
642
643 41 ▶ print "BITPIX = ",$$foo{"BITPIX"}," size = $size pixels \n"
Branch: 1 branch
BranchTrueFalseTotal
if $PDL::verbose 0 41 41
644 if $PDL::verbose;
645
646 # Slurp the FITS binary data
647
648 41 ▶ print "Reading ",$size*PDL::Core::howbig($pdl->get_datatype) , " bytes\n"
Branch: 1 branch
BranchTrueFalseTotal
if $PDL::verbose 0 41 41
649 if $PDL::verbose;
650
651 # Read the data and pad to the next HDU
652 41 my $rdct = $size * PDL::Core::howbig($pdl->get_datatype);
653 41 $fh->read( $$dref, $rdct );
654 41 $fh->read( my $dummy, 2880 - (($rdct-1) % 2880) - 1 );
655 41 $pdl->upd_data();
656  
657 41 ▶ if (!isbigendian() ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (isbigendian()) 41 0 41
658 # Need to byte swap on little endian machines
659 41 ▶ bswap2($pdl) if $pdl->get_datatype == $PDL_S;
Branch: 1 branch
BranchTrueFalseTotal
if $pdl->get_datatype == $PDL::IO::FITS::PDL_S 2 39 41
660 41 ▶ bswap4($pdl) if $pdl->get_datatype == $PDL_L || $pdl->get_datatype == $PDL_F;
Branch: 1 branch
BranchTrueFalseTotal
if $pdl->get_datatype == $PDL::IO::FITS::PDL_L or $pdl->get_datatype == $PDL::IO::FITS::PDL_F 13 28 41
Condition100%3/3
$pdl->get_datatype == $PDL::IO::FITS::PDL_L or $pdl->get_datatype == $PDL::IO::FITS::PDL_F
l!l&&r!l&&!r
5828
MC/DC100%2/2
$pdl->get_datatype == $PDL::IO::FITS::PDL_L or $pdl->get_datatype == $PDL::IO::FITS::PDL_F
$pdl->get_datatype == $PDL::IO::FITS::PDL_L $pdl->get_datatype == $PDL::IO::FITS::PDL_F
Truth table100%3/3
A or B
A= $pdl->get_datatype == $PDL::IO::FITS::PDL_LB= $pdl->get_datatype == $PDL::IO::FITS::PDL_F
ABresult
1X1
011
000
661 41 ▶ bswap8($pdl) if $pdl->get_datatype == $PDL_D || $pdl->get_datatype==$PDL_LL;
Branch: 1 branch
BranchTrueFalseTotal
if $pdl->get_datatype == $PDL::IO::FITS::PDL_D or $pdl->get_datatype == $PDL::IO::FITS::PDL_LL 24 17 41
Condition100%3/3
$pdl->get_datatype == $PDL::IO::FITS::PDL_D or $pdl->get_datatype == $PDL::IO::FITS::PDL_LL
l!l&&r!l&&!r
23117
MC/DC100%2/2
$pdl->get_datatype == $PDL::IO::FITS::PDL_D or $pdl->get_datatype == $PDL::IO::FITS::PDL_LL
$pdl->get_datatype == $PDL::IO::FITS::PDL_D $pdl->get_datatype == $PDL::IO::FITS::PDL_LL
Truth table100%3/3
A or B
A= $pdl->get_datatype == $PDL::IO::FITS::PDL_DB= $pdl->get_datatype == $PDL::IO::FITS::PDL_LL
ABresult
1X1
011
000
662 }
663
664 41 ▶ if(exists $opt->{bscale}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opt->{"bscale"}) 41 0 41
665 41 $pdl = treat_bscale($pdl, $foo);
666 }
667
668 # Header
669
670 41 $pdl->sethdr($foo);
671  
672 41 $pdl->hdrcpy($opt->{hdrcpy});
673  
674 41 return $pdl;
675 }
676  
677 sub treat_bscale($$){
678 41 ▶ my $pdl = shift;
Subroutine treat_bscale: called CC=22 SCAR=39.2
Pod undocumented
679 41 my $foo = shift;
680  
681 41 ▶ print "treating bscale...\n" if($PDL::debug);
Branch: 1 branch
BranchTrueFalseTotal
if $PDL::debug 0 41 41
682  
683 41 ▶ if ( $PDL::Bad::Status ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($PDL::Bad::Status) 41 0 41
684 # do we have bad values? - needs to be done before BSCALE/BZERO
685 # (at least for integers)
686 #
687 41 ▶ if ( $$foo{BITPIX} > 0 and exists $$foo{BLANK} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($foo->{'BITPIX'} > 0 and exists $foo->{'BLANK'}) { } 0 41 41
elsif ($foo->{'BITPIX'} < 0) { } 31 10 41
Condition66%2/3
$foo->{"BITPIX"} > 0 and exists $foo->{"BLANK"}
!ll&&!rl&&r
31100
MC/DC0%0/2
$foo->{"BITPIX"} > 0 and exists $foo->{"BLANK"}
$foo->{"BITPIX"} > 0 exists $foo->{"BLANK"}
Truth table66%2/3
A and B
A= $foo->{"BITPIX"} > 0B= exists $foo->{"BLANK"}
ABresult
0X0
100
111
688 # integer, so bad value == BLANK keyword
689 0 my $blank = $foo->{BLANK};
690 # do we have to do any conversion?
691 0 ▶ if ( $blank == $pdl->badvalue() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($blank == $pdl->badvalue) { } 0 0 0
692 0 $pdl->badflag(1);
693 } else {
694 # we change all BLANK values to the current bad value
695 # (would not be needed with a per-piddle bad value)
696 0 $pdl->inplace->setvaltobad( $blank );
697 }
698 } elsif ( $foo->{BITPIX} < 0 ) {
699 # bad values are stored as NaN's in FITS
700 # let setnanbad decide if we need to change anything
701 31 $pdl->inplace->setnantobad();
702 }
703 41 ▶ print "FITS file may contain bad values.\n"
Branch: 1 branch
BranchTrueFalseTotal
if $pdl->badflag and $PDL::verbose 0 41 41
Condition66%2/3
$pdl->badflag and $PDL::verbose
!ll&&!rl&&r
3920
MC/DC0%0/2
$pdl->badflag and $PDL::verbose
$pdl->badflag $PDL::verbose
Truth table66%2/3
A and B
A= $pdl->badflagB= $PDL::verbose
ABresult
0X0
100
111
704 if $pdl->badflag() and $PDL::verbose;
705 } # if: PDL::Bad::Status
706
707 41 my ($bscale, $bzero);
708 41 $bscale = $$foo{"BSCALE"}; $bzero = $$foo{"BZERO"};
709 41 ▶ print "BSCALE = $bscale && BZERO = $bzero\n" if $PDL::verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $PDL::verbose 0 41 41
710 41 ▶ $bscale = 1 if (!defined($bscale) || $bscale eq "");
Branch: 1 branch
BranchTrueFalseTotal
if not defined $bscale or $bscale eq "" 41 0 41
Condition33%1/3
not defined $bscale or $bscale eq ""
l!l&&r!l&&!r
4100
MC/DC0%0/2
not defined $bscale or $bscale eq ""
not defined $bscale $bscale eq ""
Truth table33%1/3
not(A) or B
A= not defined $bscaleB= $bscale eq ""
ABresult
1X1
011
000
711 41 ▶ $bzero = 0 if (!defined($bzero) || $bzero eq "");
Branch: 1 branch
BranchTrueFalseTotal
if not defined $bzero or $bzero eq "" 41 0 41
Condition33%1/3
not defined $bzero or $bzero eq ""
l!l&&r!l&&!r
4100
MC/DC0%0/2
not defined $bzero or $bzero eq ""
not defined $bzero $bzero eq ""
Truth table33%1/3
not(A) or B
A= not defined $bzeroB= $bzero eq ""
ABresult
1X1
011
000
712
713 # Be clever and work out the final datatype before eating
714 # memory
715 #
716 # ensure we pick an element that is not equal to the bad value
717 # (is this OTT?)
718 41 my $tmp;
719  
720 41 ▶ if ( $pdl->badflag() == 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($pdl->badflag == 0) { } 39 2 41
elsif ($pdl->ngood > 0) { } 2 0 2
721 39 $tmp = $pdl->flat()->slice("0:0");
722 } elsif ( $pdl->ngood > 0 ) {
723 2 my $index = which( $pdl->flat()->isbad() == 0 )->at(0);
724 2 $tmp = $pdl->flat()->slice("${index}:${index}");
725 } else {
726 # all bad, so ignore the type conversion and return
727 # -- too lazy to include this check in the code below,
728 # so just copy the header clean up stuff
729 0 ▶ print "All elements are bad.\n" if $PDL::verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $PDL::verbose 0 0 0
730
731 0 delete $$foo{"BSCALE"}; delete $$foo{"BZERO"};
732 0 $tmp = $pdl;
733 } #end of BSCALE section (whew!)
734
735
736 41 ▶ $tmp = $tmp*$bscale if $bscale != 1; # Dummy run on one element
Branch: 1 branch
BranchTrueFalseTotal
if $bscale != 1 0 41 41
737 41 ▶ $tmp = $tmp+$bzero if $bzero != 0;
Branch: 1 branch
BranchTrueFalseTotal
if $bzero != 0 0 41 41
738
739 41 ▶ $pdl = $pdl->convert($tmp->type) if $tmp->get_datatype != $pdl->get_datatype;
Branch: 1 branch
BranchTrueFalseTotal
if $tmp->get_datatype != $pdl->get_datatype 0 41 41
740
741 41 ▶ $pdl *= $bscale if $bscale != 1;
Branch: 1 branch
BranchTrueFalseTotal
if $bscale != 1 0 41 41
742 41 ▶ $pdl += $bzero if $bzero != 0;
Branch: 1 branch
BranchTrueFalseTotal
if $bzero != 0 0 41 41
743
744 41 delete $$foo{"BSCALE"}; delete $$foo{"BZERO"};
745 41 return $pdl;
746 }
747  
748  
749 ##########
750 #
751 # bintable_handlers -- helper table for bintable_row, below.
752 #
753 # Each element of the table is named by the appropriate type letter
754 # from the FITS specification. The value is a list containing the
755 # reading and writing methods.
756 #
757 # This probably ought to be a separate class, but instead it's a tawdry
758 # imitation. Too bad -- except that the ersatz really does run faster than
759 # genuine.
760 #
761 # 0: either a data type or a constructor.
762 # 1: either a length per element or a read method.
763 # 2: either a length per element or a write method.
764 # 3: 'finish' contains finishing-up code or a byte-count to swap.
765 #
766 # Main bintable type handler table.
767 # Elements: (constructor or type, reader or nbytes, writer or nbytes,
768 # finisher or nbytes). The finisher should convert the internal reading
769 # format into the final output format, e.g. by swapping (which is done
770 # automatically in the basic case). Output has row in the 0th dim.
771 #
772 # If present, the constructor should
773 # accept ($rowlen $extra, $nrows, $$size), where $rowlen is the repeat
774 # specifier in the TFORM field, $extra is the extra characters if any
775 # (for added flavor later, if desired), and $nrows is the number of rows in
776 # the table. $$size points to a scalar value that should be incremented by the
777 # size (in bytes) of a single row of the data, for accounting purposes.
778 #
779 # If a read method is specified, it should accept:
780 # ($thing, $rownum, $strptr, $rpt, $extra)
781 # where $rpt is the repeat count and $extra is the extra characters in the
782 # specifier; and it should cut the used characters off the front of the string.
783 #
784 # If a writer is specified it should accept:
785 # ($thing, $row, $rpt, $extra)
786 # and return the generated binary string.
787 #
788 # The finisher just takes the data itself. It should:
789 # * Byteswap
790 # * Condition the data to final dimensional form (if necessary)
791 # * Apply TSCAL/TZERO keywords (as necessary)
792 #
793 # The magic numbers in the table (1,2,4,8, etc.) are kludgey -- they break
794 # the isolation of PDL size and local code -- but they are a part of the FITS
795 # standard. The code will break anyway (damn) on machines that have other
796 # sizes for these datatypes.
797 #
798  
799 $PDL::IO::FITS_bintable_handlers = {
800 'X' => [ byte # Packed bit field
801 , sub {
802 0 ▶ my( $pdl, $row, $strptr ) = @_; # (ignore repeat and extra)
Subroutine __ANON__: not called CC=1 SCAR=6.9
803 0 my $n = $pdl->dim(0);
804 0 my $s = unpack( "B".$n, substr(${$strptr}, 0, int(($n+7)/8),''));
805 0 $s =~ tr/[01]/[\000\001]/;
806 0 substr( ${$pdl->get_dataref}, $n * $row, length($s)) = $s;
807 }
808 , sub {
809 0 ▶ my( $pdl, $row ) = @_; # Ignore extra and rpt
Subroutine __ANON__: not called CC=1 SCAR=6.9
810 0 my $n = $pdl->dim(0);
811 0 my $p2 = byte(($pdl->slice("($row)") != 0));
812 0 my $s = ${$p2->get_dataref};
813 0 $s =~ tr/[\000\001]/[01]/;
814 0 pack( "B".$pdl->dim(0), $s );
815 }
816 , 1
817 ]
818 ,'A' => [ sub { # constructor # String - handle as perl list
819 1 ▶ my($rowlen, $extra, $nrows, $szptr) = @_;
Subroutine __ANON__: called CC=2 SCAR=6.9
820 1 my($i,@a);
821 1 $$szptr += $rowlen;
822 1 for $i(1..$nrows) { push(@a,' 'x$rowlen); }
823 1 \@a;
824 }
825 , sub { # reader
826 4 ▶ my( $list, $row, $strptr, $rpt ) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
827 4 $list->[$row] = substr(${$strptr},0,$rpt,'');
828 }
829 , sub { # writer
830 0 ▶ my($strs, $row, $rpt ) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
831 0 my $s = substr($strs->[$row],0,$rpt);
832 0 $s . ' 'x($rpt - length $s);
833 }
834 , undef # no finisher needed
835 ]
836 ,'B' => [ byte, 1, 1, 1 ] # byte
837 ,'L' => [ byte, 1, 1, 1 ] # logical - treat as byte
838 ,'I' => [ short, 2, 2, 2 ] # short (no unsigned shorts?)
839 ,'J' => [ long, 4, 4, 4 ] # long
840 ,'K' => [ longlong,8, 8, 8 ] # longlong
841 ,'E' => [ float, 4, 4, 4 ] # single-precision
842 ,'D' => [ double, 8, 8, 8 ] # double-precision
843 0 ▶ ,'C' => [ sub { _nucomplx(float, eval '@_') }, sub { _rdcomplx(float, eval '@_') },
Subroutine __ANON__: not called CC=1 SCAR=6.9
844 0 ▶ sub { _wrcomplx(float, eval '@_') }, sub { _fncomplx(float, eval '@_') }
Subroutine __ANON__: not called CC=1 SCAR=6.9
845 ]
846 0 ▶ ,'M' => [ sub { _nucomplx(double, eval '@_') }, sub { _rdcomplx(double, eval '@_') },
Subroutine __ANON__: not called CC=1 SCAR=6.9
847 0 ▶ sub { _wrcomplx(double, eval '@_') }, sub { _fncomplx(double, eval '@_') }
Subroutine __ANON__: not called CC=1 SCAR=6.9
848 ]
849 0 ▶ ,'PB' => [ sub { _nuP(byte, eval '@_') }, sub { _rdP(byte, eval '@_') },
Subroutine __ANON__: not called CC=1 SCAR=6.9
850 0 ▶ sub { _wrP(byte, eval '@_') }, sub { _fnP(byte, eval '@_') }
Subroutine __ANON__: not called CC=1 SCAR=6.9
851 ]
852 0 ▶ ,'PL' => [ sub { _nuP(byte, eval '@_') }, sub { _rdP(byte, eval '@_') },
Subroutine __ANON__: not called CC=1 SCAR=6.9
853 0 ▶ sub { _wrP(byte, eval '@_') }, sub { _fnP(byte, eval '@_') }
Subroutine __ANON__: not called CC=1 SCAR=6.9
854 ]
855 0 ▶ ,'PI' => [ sub { _nuP(short, eval '@_') }, sub { _rdP(short, eval '@_') },
Subroutine __ANON__: not called CC=1 SCAR=6.9
856 0 ▶ sub { _wrP(short, eval '@_') }, sub { _fnP(short, eval '@_') }
Subroutine __ANON__: not called CC=1 SCAR=6.9
857 ]
858 0 ▶ ,'PJ' => [ sub { _nuP(long, eval '@_') }, sub { _rdP(long, eval '@_') },
Subroutine __ANON__: not called CC=1 SCAR=6.9
859 0 ▶ sub { _wrP(long, eval '@_') }, sub { _fnP(long, eval '@_') }
Subroutine __ANON__: not called CC=1 SCAR=6.9
860 ]
861 0 ▶ ,'PE' => [ sub { _nuP(float, eval '@_') }, sub { _rdP(float, eval '@_') },
Subroutine __ANON__: not called CC=1 SCAR=6.9
862 0 ▶ sub { _wrP(float, eval '@_') }, sub { _fnP(float, eval '@_') }
Subroutine __ANON__: not called CC=1 SCAR=6.9
863 ]
864 0 ▶ ,'PD' => [ sub { _nuP(double, eval '@_') }, sub { _rdP(double, eval '@_') },
Subroutine __ANON__: not called CC=1 SCAR=6.9
865 0 ▶ sub { _wrP(double, eval '@_') }, sub { _fnP(double, eval '@_') }
Subroutine __ANON__: not called CC=1 SCAR=6.9
866 49 ]
867 };
868  
869  
870 ##############################
871 # Helpers for complex numbers (construct/read/write/finish)
872 sub _nucomplx { # complex-number constructor
873 0 ▶ my($type, $rowlen, $extra, $nrows, $szptr) = @_;
Subroutine _nucomplx: not called CC=1 SCAR=6.9
874 0 $szptr += PDL::Core::howbig($type) * $nrows * 2;
875 0 return PDL->new_from_specification($type,2,$rowlen,$nrows);
876 }
877 sub _rdcomplx { # complex-number reader
878 0 ▶ my( $type, $pdl, $row, $strptr, $rpt ) = @_; # ignore extra
Subroutine _rdcomplx: not called CC=1 SCAR=6.9
879 0 my $s = $pdl->get_dataref;
880 0 my $rlen = 2 * PDL::Core::howbig($type) * $rpt;
881 0 substr($$s, $row*$rlen, $rlen) = substr($strptr, 0, $rlen, '');
882 }
883 sub _wrcomplx { # complex-number writer
884 0 ▶ my( $type, $pdl, $row, $rpt ) = @_; # ignore extra
Subroutine _wrcomplx: not called CC=1 SCAR=6.9
885 0 my $rlen = 2 * PDL::Core::howbig($type) * $rpt;
886 0 substr( ${$pdl->get_dataref}, $rlen * $row, $rlen );
887 }
888 sub _fncomplx { # complex-number finisher-upper
889 0 ▶ my( $type, $pdl, $n, $hdr, $opt) = shift;
Subroutine _fncomplx: not called CC=3 SCAR=24.8
890 0 eval 'bswap'.(PDL::Core::howbig($type)).'($pdl)';
891 print STDERR "Ignoring poorly-defined TSCAL/TZERO for complex data in col. $n (".$hdr->{"TTYPE$n"}.").\n"
892 0 ▶ if( length($hdr->{"TSCAL$n"}) or length($hdr->{"TZERO$n"}) );
Branch: 1 branch
BranchTrueFalseTotal
if length $$hdr{"TSCAL$n"} or length $$hdr{"TZERO$n"} 0 0 0
Condition0%0/3
length $$hdr{"TSCAL$n"} or length $$hdr{"TZERO$n"}
l!l&&r!l&&!r
000
MC/DC0%0/2
length $$hdr{"TSCAL$n"} or length $$hdr{"TZERO$n"}
length $$hdr{"TSCAL$n"} length $$hdr{"TZERO$n"}
Truth table0%0/3
A or B
A= length $$hdr{"TSCAL$n"}B= length $$hdr{"TZERO$n"}
ABresult
1X1
011
000
893 0 return $pdl->reorder(2,1,0);
894 }
895  
896 ##############################
897 # Helpers for variable-length array types (construct/read/write/finish)
898 # These look just like the complex-number case, except that they use $extra to determine the
899 # size of the 0 dimension.
900 sub _nuP {
901 0 ▶ my( $type, $rowlen, $extra, $nrows, $szptr, $hdr, $i, $tbl ) = @_;
Subroutine _nuP: not called CC=3 SCAR=24.8
902 0 $extra =~ s/\((.*)\)/$1/; # strip parens from $extra in-place
903 0 $$szptr += 8;
904  
905 0 ▶ if($rowlen != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rowlen != 1) 0 0 0
906 0 die("rfits: variable-length record has a repeat count that isn't unity! (got $rowlen); I give up.");
907 }
908  
909 # declare the PDL. Fill it with the blank value or (failing that) 0.
910 # Since P repeat count is required to be 0 or 1, we don't need an additional dimension for the
911 # repeat count -- the variable-length rows take that role.
912 0 my $pdl = PDL->new_from_specification($type, $extra, $nrows);
913 0 ▶ $pdl .= ($hdr->{"TNULL$i"} || 0);
Condition0%0/2
$$hdr{"TNULL$i"} || 0
l!l
00
MC/DC0%0/1
$$hdr{"TNULL$i"} || 0
$$hdr{"TNULL$i"}
Truth table0%0/2
A
A= $$hdr{"TNULL$i"}
Aresult
11
00
914  
915 0 my $lenpdl = zeroes(long, $nrows);
916 0 $tbl->{"len_".$hdr->{"TTYPE$i"}} = $lenpdl;
917  
918 0 return $pdl;
919 }
920 sub _rdP {
921 0 ▶ my( $type, $pdl, $row, $strptr, $rpt, $extra, $heap_ptr, $tbl, $i ) = @_;
Subroutine _rdP: not called CC=2 SCAR=17.9
922 0 $extra =~ s/\((.*)\)/$1/;
923 0 my $s = $pdl->get_dataref;
924  
925 # Read current offset and length
926 0 my $oflen = pdl(long,0,0);
927 0 my $ofs = $oflen->get_dataref;
928 0 substr($$ofs,0,8) = substr($$strptr, 0, 8, '');
929 0 $oflen->upd_data;
930 0 bswap4($oflen);
931
932 # Now get 'em
933 0 my $rlen = $extra * PDL::Core::howbig($type); # rpt should be unity, otherwise we'd have to multiply it in.
934 0 my $readlen = $oflen->at(0) * PDL::Core::howbig($type);
935  
936 # Store the length of this row in the header field.
937 0 $tbl->{"len_".$tbl->{hdr}->{"TTYPE$i"}}->dice_axis(0,$row) .= $oflen->at(0);
938  
939 0 ▶ print "_rdP: pdl is ",join("x",$pdl->dims),"; reading row $row - readlen is $readlen\n"
Branch: 1 branch
BranchTrueFalseTotal
if $PDL::debug 0 0 0
940 if($PDL::debug);
941  
942 # Copy the data into the output PDL.
943 0 my $of = $oflen->at(1);
944 0 substr($$s, $row*$rlen, $readlen) = substr($$heap_ptr, $of, $readlen);
945 0 $pdl->upd_data;
946 }
947  
948 sub _wrP {
949 0 ▶ die "This code path should never execute - you are trying to write a variable-length array via direct handler, which is wrong. Check the code path in PDL::wfits.\n";
Subroutine _wrP: not called CC=1 SCAR=6.9
950 }
951  
952 sub _fnP {
953 0 ▶ my( $type, $pdl, $n, $hdr, $opt ) = @_;
Subroutine _fnP: not called CC=10 SCAR=47.0
954 0 my $post = PDL::Core::howbig($type);
955 0 ▶ unless( isbigendian() ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (isbigendian()) 0 0 0
956 0 ▶ if( $post == 2 ) { bswap2($pdl); }
Branch: 4 branches
BranchTrueFalseTotal
if ($post == 2) { } 0 0 0
elsif ($post == 4) { } 0 0 0
elsif ($post == 8) { } 0 0 0
elsif ($post != 1) { } 0 0 0
957 0 elsif( $post == 4 ) { bswap4($pdl); }
958 0 elsif( $post == 8 ) { bswap8($pdl); }
959 elsif( $post != 1 ) {
960 0 print STDERR "Unknown swapsize $post! This is a bug. You (may) lose..\n";
961 }
962 }
963  
964 0 ▶ my $tzero = defined($hdr->{"TZERO$n"}) ? $hdr->{"TZERO$n"} : 0.0;
Branch: 1 branch
BranchTrueFalseTotal
defined $$hdr{"TZERO$n"} ? : 0 0 0
965 0 ▶ my $tscal = defined($hdr->{"TSCAL$n"}) ? $hdr->{"TSCAL$n"} : 1.0;
Branch: 1 branch
BranchTrueFalseTotal
defined $$hdr{"TSCAL$n"} ? : 0 0 0
966 0 my $valid_tzero = ($tzero != 0.0);
967 0 my $valid_tscal = ($tscal != 1.0);
968 0 ▶ if( length($hdr->{"TZERO$n"}) or length($hdr->{"TSCAL$n"})) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $$hdr{"TZERO$n"} or length $$hdr{"TSCAL$n"}) 0 0 0
Condition0%0/3
length $$hdr{"TZERO$n"} or length $$hdr{"TSCAL$n"}
l!l&&r!l&&!r
000
MC/DC0%0/2
length $$hdr{"TZERO$n"} or length $$hdr{"TSCAL$n"}
length $$hdr{"TZERO$n"} length $$hdr{"TSCAL$n"}
Truth table0%0/3
A or B
A= length $$hdr{"TZERO$n"}B= length $$hdr{"TSCAL$n"}
ABresult
1X1
011
000
969 0 print STDERR "Ignoring TSCAL/TZERO keywords for binary table array column - sorry, my mind is blown!\n";
970 }
971 0 return $pdl->mv(-1,0);
972 }
973  
974 ##############################
975 #
976 # _rfits_bintable -- snarf up a binary table, returning the named columns
977 # in a hash ref, each element of which is a PDL or list ref according to the
978 # header.
979 #
980  
981 sub _rfits_bintable ($$$$) {
982 3 ▶ my $fh = shift;
Subroutine _rfits_bintable: called CC=58 SCAR=60.0
983 3 my $hdr = shift;
984 3 my $opt = shift;
985 ##shift; ### (ignore $pdl argument)
986  
987 3 ▶ print STDERR "Warning: BINTABLE extension should have BITPIX=8, found ".$hdr->{BITPIX}.". Winging it...\n" unless($hdr->{BITPIX} == 8);
Branch: 1 branch
BranchTrueFalseTotal
unless $hdr->{"BITPIX"} == 8 0 3 3
988
989 ### Allocate the main table hash
990 3 my $tbl = {}; # Table is indexed by name
991 3 $tbl->{hdr} = $hdr;
992 3 $tbl->{tbl} = 'binary';
993  
994 3 my $tmp = []; # Temporary space is indexed by col. no.
995
996
997 ### Allocate all the columns of the table, checking for consistency
998 ### and name duplication.
999
1000 3 ▶ barf "Binary extension has no fields (TFIELDS=0)" unless($hdr->{TFIELDS});
Branch: 1 branch
BranchTrueFalseTotal
unless $hdr->{"TFIELDS"} 0 3 3
1001 3 my $rowlen = 0;
1002
1003 3 for my $i(1..$hdr->{TFIELDS}) {
1004 9 my $iter;
1005 9 ▶ my $name = $tmp->[$i]->{name} = $hdr->{"TTYPE$i"} || "COL";
Condition50%1/2
$$hdr{"TTYPE$i"} || 'COL'
l!l
90
MC/DC0%0/1
$$hdr{"TTYPE$i"} || 'COL'
$$hdr{"TTYPE$i"}
Truth table50%1/2
A
A= $$hdr{"TTYPE$i"}
Aresult
11
00
1006
1007 ### Allocate some temp space for dealing with this column
1008 9 my $tmpcol = $tmp->[$i] = {};
1009
1010 ### Check for duplicate name and change accordingly...
1011 9 ▶ while( defined( $tbl->{ $name } ) || ($name eq "COL") ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tbl->{$name} or $name eq "COL") 0 9 9
Condition33%1/3
defined $tbl->{$name} or $name eq "COL"
l!l&&r!l&&!r
009
MC/DC0%0/2
defined $tbl->{$name} or $name eq "COL"
defined $tbl->{$name} $name eq "COL"
Truth table33%1/3
A or B
A= defined $tbl->{$name}B= $name eq "COL"
ABresult
1X1
011
000
1012 0 $iter++;
1013 0 $name = ($hdr->{"TTYPE$i"} )."_$iter";
1014 }
1015
1016 # (Check avoids scrozzling comment fields unnecessarily)
1017 9 ▶ $hdr->{"TTYPE$i"} = $name unless($hdr->{"TTYPE$i"} eq $name);
Branch: 1 branch
BranchTrueFalseTotal
unless $$hdr{"TTYPE$i"} eq $name 0 9 9
1018 9 $tmpcol->{name} = $name;
1019  
1020 9 ▶ if( ($hdr->{"TFORM$i"}) =~ m/(\d*)(P?.)(.*)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$hdr{"TFORM$i"} =~ /(\d*)(P?.)(.*)/) { } 9 0 9
1021 9 ($tmpcol->{rpt}, $tmpcol->{type}, $tmpcol->{extra}) = ($1,$2,$3);
1022 # added by DJB 03.18/04 - works for my data file but is it correct?
1023 9 ▶ $tmpcol->{rpt} ||= 1;
Condition50%1/2
$tmpcol->{"rpt"} ||= 1
l!l
90
MC/DC0%0/1
$tmpcol->{"rpt"} ||= 1
$tmpcol->{"rpt"}
Truth table50%1/2
A
A= $tmpcol->{"rpt"}
Aresult
11
00
1024 } else {
1025 barf "Couldn't parse BINTABLE form '"
1026 . $hdr->{"TFORM$i"}
1027 . "' for column $i ("
1028 . $hdr->{"TTYPE$i"}
1029 0 ▶ . ")\n" if($hdr->{"TFORM$i"});
Branch: 1 branch
BranchTrueFalseTotal
if $$hdr{"TFORM$i"} 0 0 0
1030 0 barf "BINTABLE header is missing a crucial field, TFORM$i. I give up.\n";
1031 }
1032  
1033 # "A bit array consists of an integral number of bytes with trailing bits zero"
1034 9 ▶ $tmpcol->{rpt} = PDL::ceil($tmpcol->{rpt}/8) if ($tmpcol->{type} eq 'X');
Branch: 1 branch
BranchTrueFalseTotal
if $tmpcol->{"type"} eq "X" 0 9 9
1035  
1036 $tmpcol->{handler} = # sic - assignment
1037 $PDL::IO::FITS_bintable_handlers->{ $tmpcol->{type} }
1038 or
1039 barf "Unknown type ".$hdr->{"TFORM$i"}." in BINTABLE column $i "."("
1040 9 ▶ . $hdr->{"TTYPE$i"}
Branch: 1 branch
BranchTrueFalseTotal
$tmpcol->{"handler"} = $$PDL::IO::FITS_bintable_handlers{$tmpcol->{"type"}} or barf('Unknown type ' . $$hdr{"TFORM$i"} . " in BINTABLE column $i " . '(' . $$hdr{"TTYPE$i"} . ")\n That invalidates the byte count, so I give up.\n") 0 9 9
1041 . ")\n That invalidates the byte count, so I give up.\n" ;
1042
1043
1044 ### Allocate the actual data space and increment the row length
1045
1046 9 my $foo = $tmpcol->{handler}->[0];
1047 9 ▶ if( ref ($foo) eq 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $foo eq 'CODE') { } 1 8 9
1048 $tmpcol->{data} = $tbl->{$name} =
1049 1 &{$foo}( $tmpcol->{rpt}
1050 , $tmpcol->{extra}
1051 , $hdr->{NAXIS2}
1052 1 , \$rowlen
1053 , $hdr # hdr and column number are passed in, in case extra info needs to be gleaned.
1054 , $i
1055 , $tbl
1056 );
1057 } else {
1058 $tmpcol->{data} = $tbl->{$name} =
1059 PDL->new_from_specification(
1060 $foo
1061 , $tmpcol->{rpt},
1062 8 ▶ , $hdr->{NAXIS2} || 1
Condition50%1/2
$hdr->{'NAXIS2'} || 1
l!l
80
MC/DC0%0/1
$hdr->{'NAXIS2'} || 1
$hdr->{'NAXIS2'}
Truth table50%1/2
A
A= $hdr->{'NAXIS2'}
Aresult
11
00
1063 );
1064  
1065 8 $rowlen += PDL::Core::howbig($foo) * $tmpcol->{rpt};
1066 }
1067
1068 9 ▶ print "Prefrobnicated col. $i "."(".$hdr->{"TTYPE$i"}.")\ttype is ".$hdr->{"TFORM$i"}."\t length is now $rowlen\n" if($PDL::debug);
Branch: 1 branch
BranchTrueFalseTotal
if $PDL::debug 0 9 9
1069
1070
1071 } ### End of prefrobnication loop...
1072
1073  
1074 barf "Calculated row length is $rowlen, hdr claims ".$hdr->{NAXIS1}
1075 . ". Giving up. (Set \$PDL::debug for more detailed info)\n"
1076 3 ▶ if($rowlen != $hdr->{NAXIS1});
Branch: 1 branch
BranchTrueFalseTotal
if $rowlen != $hdr->{"NAXIS1"} 0 3 3
1077
1078 ### Snarf up the whole extension, and pad to 2880 bytes...
1079 3 my ($rawtable, $heap, $n1, $n2);
1080  
1081 # n1 gets number of bytes in table plus gap
1082 3 $n1 = $hdr->{NAXIS1} * $hdr->{NAXIS2};
1083 3 ▶ if($hdr->{THEAP}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hdr->{"THEAP"}) 0 3 3
1084 0 ▶ if($hdr->{THEAP} < $n1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hdr->{'THEAP'} < $n1) { } 0 0 0
1085 0 die("Inconsistent THEAP keyword in binary table\n");
1086 } else {
1087 0 $n1 = $hdr->{THEAP};
1088 }
1089 }
1090  
1091 # n2 gets number of bytes in heap (PCOUNT - gap).
1092 3 ▶ $n2 = $hdr->{PCOUNT} + ($hdr->{THEAP} ? ($hdr->{NAXIS1}*$hdr->{NAXIS2} - $hdr->{THEAP}) : 0);
Branch: 1 branch
BranchTrueFalseTotal
$hdr->{'THEAP'} ? : 0 3 3
1093 3 $n2 = ($n1+$n2-1)+2880 - (($n1+$n2-1) % 2880) - $n1;
1094  
1095 3 ▶ print "Reading $n1 bytes of table data and $n2 bytes of heap data....\n"
Branch: 1 branch
BranchTrueFalseTotal
if $PDL::verbose 0 3 3
1096 if($PDL::verbose);
1097 3 $fh->read($rawtable, $n1);
1098  
1099 3 ▶ if($n2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n2) { } 3 0 3
1100 3 $fh->read($heap, $n2)
1101 } else {
1102 0 $heap = which(pdl(0)); # empty PDL
1103 }
1104  
1105 ### Frobnicate the rows, one at a time.
1106 3 for my $row(0..$hdr->{NAXIS2}-1) {
1107 12 my $prelen = length($rawtable);
1108 12 for my $i(1..$hdr->{TFIELDS}) {
1109 36 my $tmpcol = $tmp->[$i];
1110 36 my $reader = $tmpcol->{handler}->[1];
1111 36 ▶ if(ref $reader eq 'CODE') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $reader eq 'CODE') { } 4 32 36
elsif (ref $tmpcol->{'data'} eq 'PDL') { } 32 0 32
1112 4 &{$reader}( $tmpcol->{data}
1113 , $row
1114 , \$rawtable
1115 , $tmpcol->{rpt}
1116 , $tmpcol->{extra}
1117 4 , \$heap
1118 , $tbl
1119 , $i
1120 );
1121 } elsif(ref $tmpcol->{data} eq 'PDL') {
1122 32 my $rlen = $reader * $tmpcol->{rpt};
1123  
1124 32 substr( ${$tmpcol->{data}->get_dataref()}, $rlen * $row, $rlen ) =
1125 substr( $rawtable, 0, $rlen, '');
1126 32 $tmpcol->{data}->upd_data;
1127  
1128 } else {
1129 0 die ("rfits: Bug detected: inconsistent types in BINTABLE reader\n");
1130 }
1131  
1132 } # End of TFIELDS loop
1133  
1134 12 ▶ if(length($rawtable) ne $prelen - $hdr->{NAXIS1}) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $rawtable ne $prelen - $hdr->{"NAXIS1"}) 0 12 12
1135 0 die "rfits BINTABLE: Something got screwed up -- expected a length of $prelen - $hdr->{NAXIS1}, got ".length($rawtable).". Giving up.\n";
1136 }
1137 } # End of NAXIS2 loop
1138  
1139 #
1140 # Note: the above code tickles a bug in most versions of the emacs
1141 # prettyprinter. The following "for my $i..." should be indented
1142 # two spaces.
1143 #
1144  
1145 ### Postfrobnicate the columns.
1146 3 for my $i(1..$hdr->{TFIELDS}) { # Postfrobnication loop
1147 9 my $tmpcol = $tmp->[$i];
1148 9 my $post = $tmpcol->{handler}->[3];
1149
1150 9 ▶ if(ref $post eq 'CODE') {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $post eq 'CODE') { } 0 9 9
elsif (ref $tmpcol->{'data'} eq 'PDL') { } 8 1 9
elsif (defined $post) { } 0 1 1
1151 # Do postprocessing on all special types
1152
1153 0 $tbl->{$tmpcol->{name}} = &$post($tmpcol->{data}, $i, $hdr, $opt);
1154
1155 } elsif( (ref ($tmpcol->{data})) eq 'PDL' ) {
1156 # Do standard PDL-type postprocessing
1157
1158 ## Is this call to upd_data necessary?
1159 ## I think not. (reinstate if there are bugs)
1160 # $tmpcol->{data}->upd_data;
1161
1162
1163 # Do swapping as necessary
1164 8 ▶ unless( isbigendian() ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (isbigendian()) 8 0 8
1165 8 ▶ if( $post == 2 ) { bswap2($tmpcol->{data}); }
Branch: 4 branches
BranchTrueFalseTotal
if ($post == 2) { } 1 7 8
elsif ($post == 4) { } 4 3 7
elsif ($post == 8) { } 2 1 3
elsif ($post != 1) { } 0 1 1
1166 4 elsif( $post == 4 ) { bswap4($tmpcol->{data}); }
1167 2 elsif( $post == 8 ) { bswap8($tmpcol->{data}); }
1168 elsif( $post != 1 ) {
1169 print STDERR "Unknown swapsize $post for column $i ("
1170 0 . $tmpcol->{name} . ")! This is a bug. Winging it.\n";
1171 }
1172 }
1173
1174 # Apply scaling and badval keys, which are illegal for A, L, and X
1175 # types but legal for anyone else. (A shouldn't be here, L and X
1176 # might be)
1177
1178 8 ▶ if($opt->{bscale}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{'bscale'}) { } 8 0 8
1179 8 ▶ my $tzero = defined($hdr->{"TZERO$i"}) ? $hdr->{"TZERO$i"} : 0.0;
Branch: 1 branch
BranchTrueFalseTotal
defined $$hdr{"TZERO$i"} ? : 0 8 8
1180 8 ▶ my $tscal = defined($hdr->{"TSCAL$i"}) ? $hdr->{"TSCAL$i"} : 1.0;
Branch: 1 branch
BranchTrueFalseTotal
defined $$hdr{"TSCAL$i"} ? : 0 8 8
1181
1182 # The $valid_<foo> flags let us avoid unnecessary arithmetic.
1183 8 my $valid_tzero = ($tzero != 0.0);
1184 8 my $valid_tscal = ($tscal != 1.0);
1185
1186 8 ▶ if ( $valid_tzero or $valid_tscal ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($valid_tzero or $valid_tscal) 0 8 8
Condition33%1/3
$valid_tzero or $valid_tscal
l!l&&r!l&&!r
008
MC/DC0%0/2
$valid_tzero or $valid_tscal
$valid_tzero $valid_tscal
Truth table33%1/3
A or B
A= $valid_tzeroB= $valid_tscal
ABresult
1X1
011
000
1187 0 ▶ if ( $tmpcol->{type} =~ m/[ALX]/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tmpcol->{'type'} =~ /[ALX]/i) { } 0 0 0
1188
1189 print STDERR "Ignoring illegal TSCAL/TZERO keywords for col $i (" .
1190 0 $tmpcol->{name} . "); type is $tmpcol->{type})\n";
1191
1192 } else { # Not an illegal type -- do the scaling
1193
1194 # (Normal execution path)
1195 # Use PDL's cleverness to work out the final datatype...
1196
1197 0 my $tmp;
1198 0 my $pdl = $tmpcol->{data};
1199
1200 0 ▶ if($pdl->badflag() == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($pdl->badflag == 0) { } 0 0 0
elsif ($pdl->ngood > 0) { } 0 0 0
1201
1202 0 $tmp = $pdl->flat()->slice("0:0");
1203
1204 } elsif($pdl->ngood > 0) {
1205
1206 0 my $index = which( $pdl->flat()->isbad()==0 )->at(0);
1207 0 $tmp = $pdl->flat()->slice("${index}:${index}");
1208
1209 } else { # Do nothing if it's all bad....
1210 0 $tmp = $pdl;
1211 }
1212
1213 # Figure out the type by scaling the single element.
1214 0 $tmp = ($tmp - $tzero) * $tscal;
1215
1216 # Convert the whole PDL as necessary for the scaling.
1217 0 ▶ $tmpcol->{data} = $pdl->convert($tmp->type)
Branch: 1 branch
BranchTrueFalseTotal
if $tmp->get_datatype != $pdl->get_datatype 0 0 0
1218 if($tmp->get_datatype != $pdl->get_datatype);
1219
1220 # Do the scaling.
1221 0 $tmpcol->{data} -= $tzero;
1222 0 $tmpcol->{data} *= $tscal;
1223  
1224 } # End of legal-type conditional
1225 } # End of valid_<foo> conditional
1226  
1227 8 delete $hdr->{"TZERO$i"};
1228 8 delete $hdr->{"TSCAL$i"};
1229
1230 } else { # $opt->{bscale} is zero; don't scale.
1231 # Instead, copy factors into individual column headers.
1232 0 my %foo = ("TZERO$i"=>"BZERO",
1233 "TSCAL$i"=>"BSCALE",
1234 "TUNIT$i"=>"BUNIT");
1235 0 for my $a(keys %foo) {
1236 $tmpcol->{data}->hdr->{$foo{$a}} = $hdr->{$a}
1237 0 ▶ if( defined($hdr->{$a}) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $hdr->{$a} 0 0 0
1238 }
1239 } # End of bscale checking...
1240  
1241 # Try to grab a TDIM dimension list...
1242 8 my @tdims = ();
1243 8 $tmpcol->{data}->hdrcpy(1);
1244  
1245 8 ▶ if(exists($hdr->{"TDIM$i"})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $$hdr{"TDIM$i"}) { } 0 8 8
1246 0 ▶ if($hdr->{"TDIM$i"} =~ m/\((\s*\d+(\s*\,\s*\d+)*\s*)\)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$hdr{"TDIM$i"} =~ /\((\s*\d+(\s*\,\s*\d+)*\s*)\)/) { } 0 0 0
1247 0 my $a = $1;
1248 0 @tdims = map { $_+0 } split(/\,/,$a);
1249 0 my $tdims = pdl(@tdims);
1250 0 my $tds = $tdims->prodover;
1251 0 ▶ if($tds > $tmpcol->{data}->dim(0)) {
Branch: 2 branches
BranchTrueFalseTotal
if ($tds > $tmpcol->{'data'}->dim(0)) { } 0 0 0
elsif ($tds < $tmpcol->{'data'}->dim(0)) { } 0 0 0
1252 0 die("rfits: TDIM$i is too big in binary table. I give up.\n");
1253 } elsif($tds < $tmpcol->{data}->dim(0)) {
1254 0 print STDERR "rfits: WARNING: TDIM$i is too small in binary table. Carrying on...\n";
1255 }
1256  
1257 0 $tmpcol->{data}->hdrcpy(1);
1258 0 my $td = $tmpcol->{data}->xchg(0,1);
1259 0 $tbl->{$tmpcol->{name}} = $td->reshape($td->dim(0),@tdims);
1260 } else {
1261 0 print STDERR "rfits: WARNING: invalid TDIM$i field in binary table. Ignoring.\n";
1262 }
1263 } else {
1264 # Copy the PDL out to the table itself.
1265 8 ▶ if($hdr->{NAXIS2} > 0 && $tmpcol->{rpt}>0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hdr->{"NAXIS2"} > 0 and $tmpcol->{"rpt"} > 0) 8 0 8
Condition33%1/3
$hdr->{"NAXIS2"} > 0 and $tmpcol->{"rpt"} > 0
!ll&&!rl&&r
008
MC/DC0%0/2
$hdr->{"NAXIS2"} > 0 and $tmpcol->{"rpt"} > 0
$hdr->{"NAXIS2"} > 0 $tmpcol->{"rpt"} > 0
Truth table33%1/3
A and B
A= $hdr->{"NAXIS2"} > 0B= $tmpcol->{"rpt"} > 0
ABresult
0X0
100
111
1266 $tbl->{$tmpcol->{name}} =
1267 ( ( $tmpcol->{data}->dim(0) == 1 )
1268 ? $tmpcol->{data}->slice("(0)")
1269 8 ▶ : $tmpcol->{data}->xchg(0,1)
Branch: 1 branch
BranchTrueFalseTotal
$tmpcol->{'data'}->dim(0) == 1 ? : 8 0 8
1270 );
1271 }
1272 }
1273  
1274 # End of PDL postfrobnication case
1275 } elsif(defined $post) {
1276
1277 print STDERR "Postfrobnication bug detected in column $i ("
1278 0 . $tmpcol->{name}. "). Winging it.\n";
1279
1280 }
1281 } # End of postfrobnication loop over columns
1282  
1283 ### Check whether this is actually a compressed image, in which case we hand it off to the image decompressor
1284 3 ▶ if($hdr->{ZIMAGE} && $hdr->{ZCMPTYPE} && $opt->{expand}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hdr->{"ZIMAGE"} and $hdr->{"ZCMPTYPE"} and $opt->{"expand"}) 0 3 3
Condition33%2/6
$hdr->{"ZIMAGE"} and $hdr->{"ZCMPTYPE"} and $opt->{"expand"}
!ll&&!rl&&r
300
$hdr->{"ZIMAGE"} and $hdr->{"ZCMPTYPE"}
!ll&&!rl&&r
300
MC/DC0%0/3
$hdr->{"ZIMAGE"} and $hdr->{"ZCMPTYPE"} and $opt->{"expand"}
$hdr->{"ZIMAGE"} $hdr->{"ZCMPTYPE"} $opt->{"expand"}
Truth table25%1/4
A and B and C
A= $hdr->{"ZIMAGE"}B= $hdr->{"ZCMPTYPE"}C= $opt->{"expand"}
ABCresult
0XX0
10X0
1100
1111
1285 0 eval 'use PDL::Compression;';
1286 0 ▶ if($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
1287 0 die "rfits: error while loading PDL::Compression to unpack tile-compressed image.\n\t$@\n\tUse option expand=>0 to get the binary table.\n";
1288 }
1289 0 return _rfits_unpack_zimage($tbl,$opt);
1290 }
1291  
1292 ### Done!
1293 3 return $tbl;
1294 }
1295  
1296  
1297 ##############################
1298 ##############################
1299 #
1300 # _rfits_unpack_zimage - unpack a binary table that actually contains a compressed image
1301 #
1302 # This is implemented to support the partial spec by White, Greenfield, Pence, & Tody dated Oct 21, 1999,
1303 # with reverse-engineered bits from the CFITSIO3240 library where the spec comes up short.
1304 #
1305  
1306 ## keyword is a compression algorithm name; value is an array ref containing tile compressor/uncompressor.
1307 ## The compressor/uncompressor takes (nx, ny, data) and returns the compressed/uncompressed data.
1308 ## The four currently (2010) supported-by-CFITSIO compressors are listed. Not all have been ported, hence
1309 ## the "undef"s in the table. --CED.
1310  
1311 ## Master jump table for compressors/uncompressors.
1312 ## 0 element of each array ref is the compressor; 1 element is the uncompressor.
1313 ## Uncompressed tiles are reshaped to rows of a tile table handed in (to the compressor)
1314 ## or out (of the uncompressor); actual tile shape is fed in as $params->{tiledims}, so
1315 ## higher-than-1D compression algorithms can be used.
1316 our $tile_compressors = {
1317 'GZIP_1' => undef
1318 , 'RICE_1' => [ ### RICE_1 compressor
1319 0 ▶ sub { my ($tiles, $tbl, $params) = @_;
Subroutine __ANON__: not called CC=3 SCAR=24.8
1320 0 ▶ my ($compressed,$len) = $tiles->rice_compress($params->{BLOCKSIZE} || 32);
Condition0%0/2
$params->{'BLOCKSIZE'} || 32
l!l
00
MC/DC0%0/1
$params->{'BLOCKSIZE'} || 32
$params->{'BLOCKSIZE'}
Truth table0%0/2
A
A= $params->{'BLOCKSIZE'}
Aresult
11
00
1321 0 $tbl->{ZNAME1} = "BLOCKSIZE";
1322 0 $tbl->{ZVAL1} = $params->{BLOCKSIZE};
1323 0 $tbl->{ZNAME2} = "BYTEPIX";
1324 0 $tbl->{ZVAL2} = PDL::howbig($tiles->get_datatype);
1325 # Convert the compressed data to a byte array...
1326 0 ▶ if($tbl->{ZVAL2} != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tbl->{"ZVAL2"} != 1) 0 0 0
1327 0 my @dims = $compressed->dims;
1328 0 $dims[0] *= $tbl->{ZVAL2};
1329 0 my $cd2 = zeroes( byte, @dims );
1330 0 my $cdr = $compressed->get_dataref;
1331 0 my $cd2r = $cd2->get_dataref;
1332 0 $$cd2r = $$cdr;
1333 0 $cd2->upd_data;
1334 0 $compressed = $cd2;
1335 }
1336 0 $tbl->{COMPRESSED_DATA} = $compressed->mv(0,-1);
1337 0 $tbl->{len_COMPRESSED_DATA} = $len;
1338 },
1339 ### RICE_1 expander
1340 0 ▶ sub { my ($tilesize, $tbl, $params) = @_;
Subroutine __ANON__: not called CC=5 SCAR=34.0
1341 0 my $compressed = $tbl->{COMPRESSED_DATA} -> mv(-1,0);
1342 0 ▶ my $bytepix = $params->{BYTEPIX} || 4;
Condition0%0/2
$params->{'BYTEPIX'} || 4
l!l
00
MC/DC0%0/1
$params->{'BYTEPIX'} || 4
$params->{'BYTEPIX'}
Truth table0%0/2
A
A= $params->{'BYTEPIX'}
Aresult
11
00
1343  
1344 # Put the compressed tile bitstream into a variable of appropriate type.
1345 # This works by direct copying of the PDL data, which sidesteps local
1346 # byteswap issues in the usual case that the compressed stream is type
1347 # byte. But it does add the extra complication that we have to pad the
1348 # compressed array out to a factor-of-n elements in certain cases.
1349  
1350 0 ▶ if( PDL::howbig($compressed->get_datatype) != $bytepix ) {
Branch: 1 branch
BranchTrueFalseTotal
if (PDL::howbig($compressed->get_datatype) != $bytepix) 0 0 0
1351 0 my @dims = $compressed->dims;
1352 0 my $newdim0;
1353 my $scaledim0;
1354  
1355 0 $scaledim0 = $dims[0] * PDL::howbig($compressed->get_datatype) / $bytepix;
1356 0 $newdim0 = pdl($scaledim0)->ceil;
1357  
1358 0 ▶ if($scaledim0 != $newdim0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($scaledim0 != $newdim0) 0 0 0
1359 0 my $padding = zeroes($compressed->type,
1360 ($newdim0-$scaledim0) * $bytepix / PDL::howbig($compressed->get_datatype),
1361 @dims[1..$#dims]
1362 );
1363 0 $compressed = $compressed->append($padding);
1364 }
1365
1366 0 my $c2 = zeroes( $type_table_2->{$bytepix * 8}, $newdim0, @dims[1..$#dims] );
1367  
1368 0 my $c2dr = $c2->get_dataref;
1369 0 my $cdr = $compressed->get_dataref;
1370 0 substr($$c2dr,0,length($$cdr)) = $$cdr;
1371 0 $c2->upd_data;
1372 0 $compressed = $c2;
1373 }
1374  
1375 0 ▶ return $compressed->rice_expand( $tilesize, $params->{BLOCKSIZE} || 32);
Condition0%0/2
$params->{'BLOCKSIZE'} || 32
l!l
00
MC/DC0%0/1
$params->{'BLOCKSIZE'} || 32
$params->{'BLOCKSIZE'}
Truth table0%0/2
A
A= $params->{'BLOCKSIZE'}
Aresult
11
00
1376 }
1377 49 ]
1378 , 'PLIO_1' => undef
1379 , 'HCOMPRESS_1' => undef
1380 };
1381  
1382 ## List of the eight mandatory keywords and their ZIMAGE preservation pigeonholes, for copying after we
1383 ## expand an image.
1384 49 our $hdrconv = {
1385 "ZSIMPLE" => "SIMPLE",
1386 "ZTENSION" => "XTENSION",
1387 "ZEXTEND" => "EXTEND",
1388 "ZBLOCKED" => "BLOCKED",
1389 "ZPCOUNT" => "PCOUNT",
1390 "ZGCOUNT" => "GCOUNT",
1391 "ZHECKSUM" => "CHECKSUM",
1392 "ZDATASUM" => "DATASUM"
1393 };
1394  
1395  
1396 sub _rfits_unpack_zimage($$$) {
1397 0 ▶ my $tbl = shift;
Subroutine _rfits_unpack_zimage: not called CC=27 SCAR=66.3
1398 0 my $opt = shift;
1399  
1400 0 my $hdr = $tbl->{hdr};
1401  
1402 0 my $tc = $tile_compressors->{$hdr->{ZCMPTYPE}};
1403 0 ▶ unless(defined $tc) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $tc) 0 0 0
1404 0 print STDERR "WARNING: rfits: Compressed image has unsupported comp. type ('$hdr->{ZCMPTYPE}').\n";
1405 0 return $tbl;
1406 }
1407  
1408 #############
1409 # Declare the output image
1410 0 my $type;
1411 0 ▶ unless($type_table->{$hdr->{ZBITPIX}}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$PDL::IO::FITS::type_table{$hdr->{'ZBITPIX'}}) { } 0 0 0
1412 0 print STDERR "WARNING: rfits: unrecognized ZBITPIX value $hdr->{ZBITPIX} in compressed image. Assuming -64.\n";
1413 0 $type = $type_table_2->{-64};
1414 } else {
1415 0 $type = $type_table_2->{$hdr->{ZBITPIX}};
1416 }
1417 0 my @dims;
1418 0 for my $i(1..$hdr->{ZNAXIS}) {
1419 0 push(@dims,$hdr->{"ZNAXIS$i"});
1420 }
1421  
1422 0 my $pdl = PDL->new_from_specification( $type, @dims );
1423  
1424 ############
1425 # Calculate tile size and allocate a working tile.
1426 0 my @tiledims;
1427 0 for my $i(1..$hdr->{ZNAXIS}) {
1428 0 ▶ if($hdr->{"ZTILE$i"}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$hdr{"ZTILE$i"}) { } 0 0 0
1429 0 push(@tiledims, $hdr->{"ZTILE$i"});
1430 } else {
1431 0 ▶ push(@tiledims, (($i==1) ? $hdr->{ZNAXIS1} : 1) );
Branch: 1 branch
BranchTrueFalseTotal
$i == 1 ? : 0 0 0
1432 }
1433 }
1434  
1435  
1436 # my $tile = PDL->new_from_specification( $type, @tiledims );
1437 0 my $tiledims = pdl(@tiledims);
1438 0 my $tilesize = $tiledims->prodover;
1439 ###########
1440 # Calculate tile counts and compare to the number of stored tiles
1441 0 my $ntiles = ( pdl(@dims) / pdl(@tiledims) )->ceil;
1442 0 my $tilecount = $ntiles->prodover;
1443  
1444 0 ▶ if($tilecount != $tbl->{COMPRESSED_DATA}->dim(0)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tilecount != $tbl->{"COMPRESSED_DATA"}->dim(0)) 0 0 0
1445 0 printf STDERR "WARNING: rfits: compressed data has $hdr->{NAXIS2} rows; we expected $tilecount (",join("x",list $ntiles),"). Winging it...\n";
1446 }
1447  
1448 ##########
1449 # Quantization - ignore for now
1450 0 ▶ if($hdr->{ZQUANTIZ}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hdr->{"ZQUANTIZ"}) 0 0 0
1451 0 printf STDERR "WARNING: rfits: ignoring quantization/dithering (ZQUANTIZ=$hdr->{ZQUANTIZ})\n";
1452 }
1453
1454 ##########
1455 # Snarf up compression parameters
1456 0 my $params = {};
1457 0 my $i = 1;
1458 0 ▶ while( $hdr->{"ZNAME$i"} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$hdr{"ZNAME$i"}) 0 0 0
1459 0 $params->{ $hdr->{"ZNAME$i"} } = $hdr->{"ZVAL$i"};
1460 0 $i++;
1461 }
1462  
1463 ##########
1464 # Enumerate tile coordinates for looping, and the corresponding row number
1465 0 my ($step, @steps, $steps);
1466 0 $step = 1;
1467 0 for my $i(0..$ntiles->nelem-1) {
1468 0 push(@steps, $step);
1469 0 $step *= $ntiles->at($i);
1470 }
1471 0 $step = pdl(@steps);
1472  
1473 # $tiledex is 2-D (coordinate-index, list-index) and enumerates all tiles by image
1474 # location; $tilerow is 1-D (list-index) and enumerates all tiles by row in the bintable
1475 0 my $tiledex = PDL::ndcoords($ntiles->list)->mv(0,-1)->clump($ntiles->dim(0))->mv(-1,0);
1476 0 $TMP::tiledex = $tiledex;
1477 0 my $tilerow = ($tiledex * $step)->sumover;
1478
1479 ##########
1480 # Restore all the tiles at once
1481 0 my $tiles = &{$tc->[1]}( $tilesize, $tbl, $params ); # gets a (tilesize x ntiles) output
1482 0 my $patchup = which($tbl->{len_COMPRESSED_DATA} <= 0);
1483 0 ▶ if($patchup->nelem) {
Branch: 1 branch
BranchTrueFalseTotal
if ($patchup->nelem) 0 0 0
1484 0 ▶ unless(defined $tbl->{UNCOMPRESSED_DATA}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $tbl->{"UNCOMPRESSED_DATA"}) 0 0 0
1485 0 die "rfits: need some uncompressed data for missing compressed rows, but none were found!\n";
1486 }
1487 0 ▶ if($tbl->{UNCOMPRESSED_DATA}->dim(1) != $tilesize) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tbl->{"UNCOMPRESSED_DATA"}->dim(1) != $tilesize) 0 0 0
1488 0 die "rfits: tile size is $tilesize, but uncompressed data rows have size ".$tbl->{UNCOMPRESSED_DATA}->dim(1)."\n";
1489 }
1490 0 $tiles->dice_axis(1,$patchup) .= $tbl->{UNCOMPRESSED_DATA}->dice_axis(0,$patchup)->xchg(0,1);
1491 }
1492  
1493 ##########
1494 # Slice up the output image plane into tiles, and use the threading engine
1495 # to assign everything to them.
1496 0 my $cutup = $pdl->range( $tiledex, [@tiledims], 't') # < ntiles, tilesize0..tilesizen >
1497 ->mv(0,-1) # < tilesize0..tilesizen, ntiles >
1498 ->clump($tiledims->nelem); # < tilesize, ntiles >
1499  
1500 0 $cutup .= $tiles; # dump all the tiles at once into the image - they flow back to $pdl.
1501 0 undef $cutup; # sever connection to prevent expensive future dataflow.
1502  
1503 ##########
1504 # Perform scaling if necessary ( Just the ZIMAGE quantization step )
1505 # bscaling is handled farther down with treat_bscale.
1506  
1507 0 ▶ $pdl *= $hdr->{ZSCALE} if defined($hdr->{ZSCALE});
Branch: 1 branch
BranchTrueFalseTotal
if defined $hdr->{"ZSCALE"} 0 0 0
1508 0 ▶ $pdl += $hdr->{ZZERO} if defined($hdr->{ZZERO});
Branch: 1 branch
BranchTrueFalseTotal
if defined $hdr->{"ZZERO"} 0 0 0
1509  
1510 ##########
1511 # Put the FITS header into the newly reconstructed image.
1512 0 delete $hdr->{PCOUNT};
1513 0 delete $hdr->{GCOUNT};
1514  
1515 # Copy the mandated name-conversions
1516 0 for my $k(keys %$hdrconv) {
1517 0 ▶ if($hdr->{$k}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hdr->{$k}) 0 0 0
1518 0 $hdr->{$hdrconv->{$k}} = $hdr->{$k};
1519 0 delete $hdr->{$k};
1520 }
1521 }
1522  
1523 # Copy the ZNAXIS* keywords to NAXIS*
1524 0 foreach (grep /^NAXIS/,keys %$hdr){
1525 0 ▶ if (exists($hdr->{'Z'.$_}) && defined($hdr->{'Z'.$_})){
Branch: 1 branch
BranchTrueFalseTotal
if (exists $$hdr{'Z' . $_} and defined $$hdr{'Z' . $_}) 0 0 0
Condition0%0/3
exists $$hdr{'Z' . $_} and defined $$hdr{'Z' . $_}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $$hdr{'Z' . $_} and defined $$hdr{'Z' . $_}
exists $$hdr{'Z' . $_} defined $$hdr{'Z' . $_}
Truth table0%0/3
A and B
A= exists $$hdr{'Z' . $_}B= defined $$hdr{'Z' . $_}
ABresult
0X0
100
111
1526 0 $hdr->{$_} = $hdr->{'Z'.$_};
1527 }
1528 }
1529  
1530 # Clean up the ZFOO extensions and table cruft
1531 0 for my $k(keys %{$hdr}) {
1532 0 ▶ delete $hdr->{$k} if(
Branch: 1 branch
BranchTrueFalseTotal
if $k =~ /^Z/ or $k eq "TFIELDS" or $k =~ /^TTYPE/ or $k =~ /^TFORM/ 0 0 0
Condition0%0/9
$k =~ /^Z/ or $k eq "TFIELDS" or $k =~ /^TTYPE/ or $k =~ /^TFORM/
l!l&&r!l&&!r
000
$k =~ /^Z/ or $k eq "TFIELDS" or $k =~ /^TTYPE/
l!l&&r!l&&!r
000
$k =~ /^Z/ or $k eq "TFIELDS"
l!l&&r!l&&!r
000
MC/DC0%0/4
$k =~ /^Z/ or $k eq "TFIELDS" or $k =~ /^TTYPE/ or $k =~ /^TFORM/
$k =~ /^Z/ $k eq "TFIELDS" $k =~ /^TTYPE/ $k =~ /^TFORM/
Truth table0%0/5
A or B or C or D
A= $k =~ /^Z/B= $k eq "TFIELDS"C= $k =~ /^TTYPE/D= $k =~ /^TFORM/
ABCDresult
1XXX1
01XX1
001X1
00011
00000
1533 $k=~ m/^Z/ ||
1534 $k eq "TFIELDS" ||
1535 $k =~ m/^TTYPE/ ||
1536 $k =~ m/^TFORM/
1537 );
1538 }
1539  
1540 0 ▶ if(exists $hdr->{BSCALE}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $hdr->{"BSCALE"}) 0 0 0
1541 0 $pdl = treat_bscale($pdl, $hdr);
1542 }
1543 0 $pdl->sethdr($hdr);
1544 0 $pdl->hdrcpy($opt->{hdrcpy});
1545  
1546 0 return $pdl;
1547 }
1548  
1549  
1550
1551  
1552 =head2 wfits()
1553  
1554 =for ref
1555  
1556 Simple PDL FITS writer
1557  
1558 =for example
1559  
1560 wfits $pdl, 'filename.fits', [$BITPIX], [$COMPRESSION_OPTIONS];
1561 wfits $hash, 'filename.fits', [$OPTIONS];
1562 $pdl->wfits('foo.fits',-32);
1563  
1564 Suffix magic:
1565  
1566 # Automatically compress through pipe to gzip
1567 wfits $pdl, 'filename.fits.gz';
1568 # Automatically compress through pipe to compress
1569 wfits $pdl, 'filename.fits.Z';
1570  
1571 Tilde expansion:
1572 #expand leading ~ to home directory (using glob())
1573 wfits $pdl, '~/filename.fits';
1574  
1575 =over 3
1576  
1577 =item * Ordinary (PDL) data handling:
1578  
1579 If the first argument is a PDL, then the PDL is written out as an
1580 ordinary FITS file with a single Header/Data Unit of data.
1581  
1582 $BITPIX is then optional and coerces the output data type according to
1583 the standard FITS convention for the BITPIX field (with positive
1584 values representing integer types and negative values representing
1585 floating-point types).
1586  
1587 If C<$pdl> has a FITS header attached to it (actually, any hash that
1588 contains a C<< SIMPLE=>T >> keyword), then that FITS header is written
1589 out to the file. The image dimension tags are adjusted to the actual
1590 dataset. If there's a mismatch between the dimensions of the data and
1591 the dimensions in the FITS header, then the header gets corrected and
1592 a warning is printed.
1593  
1594 If C<$pdl> is a slice of another PDL with a FITS header already
1595 present (and header copying enabled), then you must be careful.
1596 C<wfits> will remove any extraneous C<NAXISn> keywords (per the FITS
1597 standard), and also remove the other keywords associated with that
1598 axis: C<CTYPEn>, C<CRPIXn>, C<CRVALn>, C<CDELTn>, and C<CROTAn>. This
1599 may cause confusion if the slice is NOT out of the last dimension:
1600 C<wfits($a(:,(0),:),'file.fits');> and you would be best off adjusting
1601 the header yourself before calling C<wfits>.
1602  
1603 You can tile-compress images according to the CFITSIO extension to the
1604 FITS standard, by adding an option hash to the arguments:
1605  
1606 =over 3
1607  
1608 =item compress
1609  
1610 This can be either unity, in which case Rice compression is used,
1611 or a (case-insensitive) string matching the CFITSIO compression
1612 type names. Currently supported compression algorithms are:
1613  
1614 =over 3
1615  
1616 =item * RICE_1 - linear Rice compression
1617  
1618 This uses limited-symbol-length Rice compression, which works well on
1619 low entropy image data (where most pixels differ from their neighbors
1620 by much less than the dynamic range of the image).
1621  
1622 =back
1623  
1624 =item tilesize (default C<[-1,1]>)
1625  
1626 This specifies the dimension of the compression tiles, in pixels. You
1627 can hand in a PDL, a scalar, or an array ref. If you specify fewer
1628 dimensions than exist in the image, the last dim is repeated - so "32"
1629 yields 32x32 pixel tiles in a 2-D image. A dim of -1 in any dimension
1630 duplicates the image size, so the default C<[-1,1]> causes compression
1631 along individual rows.
1632  
1633 =item tilesize (RICE_1 only; default C<32>)
1634  
1635 For RICE_1, BLOCKSIZE indicates the number of pixel samples to use
1636 for each compression block within the compression algorithm. The
1637 blocksize is independent of the tile dimensions. For RICE
1638 compression the pixels from each tile are arranged in normal pixel
1639 order (early dims fastest) and compressed as a linear stream.
1640  
1641 =back
1642  
1643 =item * Table handling:
1644  
1645 If you feed in a hash ref instead of a PDL, then the hash ref is
1646 written out as a binary table extension. The hash ref keys are
1647 treated as column names, and their values are treated as the data to
1648 be put in each column.
1649  
1650 For numeric information, the hash values should contain PDLs. The 0th
1651 dim of the PDL runs across rows, and higher dims are written as
1652 multi-value entries in the table (e.g. a 7x5 PDL will yield a single
1653 named column with 7 rows and 5 numerical entries per row, in a binary
1654 table). Note that this is slightly different from the usual concept
1655 of threading, in which dimension 1 runs across rows.
1656  
1657 ASCII tables only allow one entry per column in each row, so
1658 if you plan to write an ASCII table then all of the values of C<$hash>
1659 should have at most one dim.
1660  
1661 All of the columns' 0 dims must agree in the threading sense. That is to
1662 say, the 0th dimension of all of the values of C<$hash> should be the
1663 same (indicating that all columns have the same number of rows). As
1664 an exception, if the 0th dim of any of the values is 1, or if that
1665 value is a PDL scalar (with 0 dims), then that value is "threaded"
1666 over -- copied into all rows.
1667  
1668 Data dimensions higher than 2 are preserved in binary tables,
1669 via the TDIMn field (e.g. a 7x5x3 PDL is stored internally as
1670 seven rows with 15 numerical entries per row, and reconstituted
1671 as a 7x5x3 PDL on read).
1672  
1673 Non-PDL Perl scalars are treated as strings, even if they contain
1674 numerical values. For example, a list ref containing 7 values is
1675 treated as 7 rows containing one string each. There is no such thing
1676 as a multi-string column in FITS tables, so any nonscalar values in
1677 the list are stringified before being written. For example, if you
1678 pass in a perl list of 7 PDLs, each PDL will be stringified before
1679 being written, just as if you printed it to the screen. This is
1680 probably not what you want -- you should use L<glue|glue> to connect
1681 the separate PDLs into a single one. (e.g. C<$a-E<gt>glue(1,$b,$c)-E<gt>mv(1,0)>)
1682  
1683 The column names are case-insensitive, but by convention the keys of
1684 C<$hash> should normally be ALL CAPS, containing only digits, capital
1685 letters, hyphens, and underscores. If you include other characters,
1686 then case is smashed to ALL CAPS, whitespace is converted to
1687 underscores, and unrecognized characters are ignored -- so if you
1688 include the key "Au Purity (%)", it will be written to the file as a
1689 column that is named "AU_PURITY". Since this is not guaranteed to
1690 produce unique column names, subsequent columns by the same name are
1691 disambiguated by the addition of numbers.
1692  
1693 You can specify the use of variable-length rows in the output, saving
1694 space in the file. To specify variable length rows for a column named
1695 "FOO", you can include a separate key "len_FOO" in the hash to be
1696 written. The key's value should be a PDL containing the number of
1697 actual samples in each row. The result is a FITS P-type variable
1698 length column that, upon read with C<rfits()>, will restore to a field
1699 named FOO and a corresponding field named "len_FOO". Invalid data in
1700 the final PDL consist of a padding value (which defaults to 0 but
1701 which you may set by including a TNULL field in the hdr specificaion).
1702 Variable length arrays must be 2-D PDLs, with the variable length in
1703 the 1 dimension.
1704  
1705 Two further special keys, 'hdr' and 'tbl', can contain
1706 meta-information about the type of table you want to write. You may
1707 override them by including an C<$OPTIONS> hash with a 'hdr' and/or
1708 'tbl' key.
1709  
1710 The 'tbl' key, if it exists, must contain either 'ASCII' or 'binary'
1711 (case-insensitive), indicating whether to write an ascii or binary
1712 table. The default is binary. [ASCII table writing is planned but
1713 does not yet exist].
1714  
1715 You can specify the format of the table quite specifically with the
1716 'hdr' key or option field. If it exists, then the 'hdr' key should
1717 contain fields appropriate to the table extension being used. Any
1718 field information that you don't specify will be filled in
1719 automatically, so (for example) you can specify that a particular
1720 column name goes in a particular position, but allow C<wfits> to
1721 arrange the other columns in the usual alphabetical order into any
1722 unused slots that you leave behind. The C<TFORMn>, C<TFIELDS>,
1723 C<PCOUNT>, C<GCOUNT>, C<NAXIS>, and C<NAXISn> keywords are ignored:
1724 their values are calculated based on the hash that you supply. Any
1725 other fields are passed into the final FITS header verbatim.
1726  
1727 As an example, the following
1728  
1729 $a = long(1,2,4);
1730 $b = double(1,2,4);
1731 wfits { 'COLA'=>$a, 'COLB'=>$b }, "table1.fits";
1732  
1733 will create a binary FITS table called F<table1.fits> which
1734 contains two columns called C<COLA> and C<COLB>. The order
1735 of the columns is controlled by setting the C<TTYPEn>
1736 keywords in the header array, so
1737  
1738 $h = { 'TTYPE1'=>'Y', 'TTYPE2'=>'X' };
1739 wfits { 'X'=>$a, 'Y'=>$b, hdr=>$h }, "table2.fits";
1740  
1741 creates F<table2.fits> where the first column is
1742 called C<Y> and the second column is C<X>.
1743  
1744 =item * multi-value handling
1745  
1746 If you feed in a perl list rather than a PDL or a hash, then
1747 each element is written out as a separate HDU in the FITS file.
1748 Each element of the list must be a PDL or a hash. [This is not implemented
1749 yet but should be soon!]
1750  
1751 =item * DEVEL NOTES
1752  
1753 ASCII tables are not yet handled but should be.
1754  
1755 Binary tables currently only handle one vector (up to 1-D array)
1756 per table entry; the standard allows more, and should be fully implemented.
1757 This means that PDL::Complex piddles currently can not be written to
1758 disk.
1759  
1760 Handling multidim arrays implies that perl multidim lists should also be
1761 handled.
1762  
1763 =back
1764  
1765 =for bad
1766  
1767 For integer types (ie C<BITPIX E<gt> 0>), the C<BLANK> keyword is set
1768 to the bad value. For floating-point types, the bad value is
1769 converted to NaN (if necessary) before writing.
1770  
1771 =cut
1772  
1773 49 *wfits = \&PDL::wfits;
1774  
1775 BEGIN {
1776 49 ▶ @PDL::IO::FITS::wfits_keyword_order =
Subroutine BEGIN: called CC=1 SCAR=0.0
1777 ('SIMPLE','BITPIX','NAXIS','NAXIS1','BUNIT','BSCALE','BZERO');
1778 49 @PDL::IO::FITS::wfits_numbered_keywords =
1779 ('CTYPE','CRPIX','CRVAL','CDELT','CROTA');
1780 }
1781  
1782 # Until we do a rewrite these have to be file global since they
1783 # are used by the wheader routine
1784 49 my (%hdr, $nbytes);
1785  
1786 # Local utility routine of wfits()
1787 sub wheader ($$) {
1788 182 ▶ my $fh = shift;
Subroutine wheader: called CC=17 SCAR=35.1
Pod undocumented
1789 182 my $k = shift;
1790
1791 182 ▶ if ($k =~ m/(HISTORY|COMMENT)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k =~ /(HISTORY|COMMENT)/) { } 32 150 182
1792 32 my $hc = $1;
1793 32 ▶ return unless ref($hdr{$k}) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $hdr{$k} eq "ARRAY" 32 0 32
1794 0 foreach my $line (@{$hdr{$k}}) {
1795 0 $fh->printf( "$hc %-72s", substr($line,0,72) );
1796 0 $nbytes += 80;
1797 }
1798 0 delete $hdr{$k};
1799 } else {
1800 # Check that we are dealing with a scalar value in the header
1801 # Need to make sure that the header does not include PDLs or
1802 # other structures. Return unless $hdr{$k} is a scalar.
1803 150 my($hdrk) = $hdr{$k};
1804
1805 150 ▶ if(ref $hdrk eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $hdrk eq "ARRAY") 0 150 150
1806 0 $hdrk = join("\n",@$hdrk);
1807 }
1808
1809 150 ▶ return unless not ref($hdrk);
Branch: 1 branch
BranchTrueFalseTotal
if ref $hdrk 0 150 150
1810
1811 150 ▶ if ($hdrk eq "") {
Branch: 1 branch
BranchTrueFalseTotal
if ($hdrk eq '') { } 0 150 150
1812 0 $fh->printf( "%-80s", substr($k,0,8) );
1813 } else {
1814 150 $fh->printf( "%-8s= ", substr($k,0,8) );
1815
1816 my $com = ( ref $hdr{COMMENT} eq 'HASH' ) ?
1817 150 ▶ $hdr{COMMENT}{$k} : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $hdr{'COMMENT'} eq 'HASH' ? : 0 150 150
1818
1819 150 ▶ if ($hdrk =~ /^ *([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))? *$/) { # Number?
Branch: 2 branches
BranchTrueFalseTotal
if ($hdrk =~ /^ *([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))? *$/) { } 118 32 150
elsif ($hdrk eq 'F' or $hdrk eq 'T') { } 0 32 32
1820 118 ▶ my $cl=60-($com ? 2 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$com ? : 0 118 118
1821 118 my $end=' ' x $cl;
1822 118 ▶ $end =' /'. $com if($com);
Branch: 1 branch
BranchTrueFalseTotal
if $com 0 118 118
1823 118 $fh->printf( "%20s%-50s", substr($hdrk,0,20),
1824 substr($end, 0, 50) );
1825 ▶ } elsif ($hdrk eq 'F' or $hdrk eq 'T') {
Condition33%1/3
$hdrk eq "F" or $hdrk eq "T"
l!l&&r!l&&!r
0032
MC/DC0%0/2
$hdrk eq "F" or $hdrk eq "T"
$hdrk eq "F" $hdrk eq "T"
Truth table33%1/3
A or B
A= $hdrk eq "F"B= $hdrk eq "T"
ABresult
1X1
011
000
1826 # Logical flags ?
1827 0 $fh->printf( "%20s", $hdrk );
1828 0 my $end=' ' x 50;
1829 0 ▶ $end =' /'.$com if($com);
Branch: 1 branch
BranchTrueFalseTotal
if $com 0 0 0
1830 0 $fh->printf( "%-50s", $end );
1831 } else {
1832 # Handle strings, truncating as necessary
1833 # (doesn't do multicard strings like Astro::FITS::Header does)
1834
1835 # Convert single quotes to doubled single quotes
1836 # (per FITS standard)
1837 32 my($st) = $hdrk;
1838 32 $st =~ s/\'/\'\'/g;
1839
1840 32 my $sl=length($st)+2;
1841 32 ▶ my $cl=70-$sl-($com ? 2 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$com ? : 0 32 32
1842 32 $fh->print( "'$st'" );
1843
1844 32 ▶ if (defined $com) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $com) { } 0 32 32
1845 0 $fh->printf( " /%-$ {cl}s", substr($com, 0, $cl) );
1846 } else {
1847 32 $fh->printf( "%-$ {cl}s", ' ' x $cl );
1848 }
1849 }
1850 }
1851 150 $nbytes += 80; delete $hdr{$k};
1852 }
1853 150 ▶ delete $hdr{COMMENT}{$k} if ref $hdr{COMMENT} eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $hdr{"COMMENT"} eq "HASH" 0 150 150
1854 150 1;
1855 }
1856  
1857 # Write a PDL to a FITS format file
1858 #
1859 sub PDL::wfits {
1860 39 ▶ barf 'Usage: wfits($pdl,$file,[$BITPIX],[{options}])' if $#_<1 || $#_>3;
Subroutine wfits: called CC=106 SCAR=58.1
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if $#_ < 1 or $#_ > 3 0 39 39
Condition33%1/3
$#_ < 1 or $#_ > 3
l!l&&r!l&&!r
0039
MC/DC0%0/2
$#_ < 1 or $#_ > 3
$#_ < 1 $#_ > 3
Truth table33%1/3
A or B
A= $#_ < 1B= $#_ > 3
ABresult
1X1
011
000
1861 39 my ($pdl,$file,$a,$b) = @_;
1862 39 my ($opt, $BITPIX);
1863  
1864 39 local $\ = undef; # fix sf.net bug #3394327
1865  
1866 39 ▶ if(ref $a eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $a eq 'HASH') { } 0 39 39
elsif (ref $b eq 'HASH') { } 0 39 39
1867 0 $a = $opt;
1868 0 $BITPIX = $b;
1869 } elsif(ref $b eq 'HASH') {
1870 0 $BITPIX = $a;
1871 0 $opt = $b;
1872 }
1873  
1874 39 my ($k, $buff, $off, $ndims, $sz);
1875  
1876 39 $file =~ s/^(~)/glob($1)/e; # tilde expansion
1877  
1878 39 local $SIG{PIPE};
1879  
1880 39 ▶ if ($file =~ /\.gz$/) { # Handle suffix-style compression
Branch: 2 branches
BranchTrueFalseTotal
if ($file =~ /\.gz$/) { } 0 39 39
elsif ($file =~ /\.Z$/) { } 0 39 39
1881 0 ▶ $SIG{PIPE}= sub {}; # Prevent crashing if gzip dies
Subroutine __ANON__: not called CC=1 SCAR=6.9
1882 0 $file = "|gzip -9 > $file";
1883 }
1884 elsif ($file =~ /\.Z$/) {
1885 0 ▶ $SIG{PIPE}= sub {}; # Prevent crashing if compress dies
Subroutine __ANON__: not called CC=1 SCAR=6.9
1886 0 $file = "|compress > $file";
1887 }
1888 else{
1889 39 $file = ">$file";
1890 }
1891
1892 #### Figure output type
1893  
1894 39 my @outputs = ();
1895 39 my $issue_nullhdu;
1896  
1897 39 ▶ if( UNIVERSAL::isa($pdl,'PDL') ) {
Branch: 4 branches
BranchTrueFalseTotal
if (UNIVERSAL::isa($pdl, 'PDL')) { } 32 7 39
elsif (ref $pdl eq 'HASH') { } 3 4 7
elsif (ref $pdl eq 'ARRAY') { } 1 3 4
elsif (length ref $pdl == 0) { } 3 0 3
1898 32 $issue_nullhdu = 0;
1899 32 push(@outputs, $pdl);
1900 } elsif( ref($pdl) eq 'HASH' ) {
1901 3 $issue_nullhdu = 1;
1902 3 push(@outputs, $pdl);
1903 } elsif( ref($pdl) eq 'ARRAY' ) {
1904 1 $issue_nullhdu = 1;
1905 1 @outputs = @$pdl;
1906 } elsif( length(ref($pdl))==0 ) {
1907 3 barf "wfits: needs a HASH or PDL argument to write out\n";
1908 } else {
1909 0 barf "wfits: unknown ref type ".ref($pdl)."\n";
1910 }
1911  
1912 ## Open file & prepare to write binary info
1913 36 ▶ my $fh = IO::File->new( $file )
Branch: 1 branch
BranchTrueFalseTotal
my $fh = "IO::File"->new($file) or barf("Unable to create FITS file $file\n") 0 36 36
1914 or barf "Unable to create FITS file $file\n";
1915 36 binmode $fh;
1916  
1917 36 ▶ if($issue_nullhdu) {
Branch: 1 branch
BranchTrueFalseTotal
if ($issue_nullhdu) 4 32 36
1918 4 _wfits_nullhdu($fh);
1919 }
1920  
1921 36 for $pdl(@outputs) {
1922  
1923 37 ▶ if(ref($pdl) eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $pdl eq 'HASH') { } 3 34 37
elsif (UNIVERSAL::isa($pdl, 'PDL')) { } 34 0 34
1924 my $table_type = ( exists($pdl->{tbl}) ?
1925 3 ▶ ($pdl->{tbl} =~ m/^a/i ? 'ascii' : 'binary') :
Branch: 2 branches
BranchTrueFalseTotal
exists $pdl->{'tbl'} ? : 0 3 3
$pdl->{'tbl'} =~ /^a/i ? : 0 0 0
1926 "binary"
1927 );
1928 3 _wfits_table($fh,$pdl,$table_type);
1929 } elsif( UNIVERSAL::isa($pdl,'PDL') ) {
1930  
1931 ### Regular image writing.
1932
1933 34 ▶ $BITPIX = "" unless defined $BITPIX;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $BITPIX 33 1 34
1934 34 ▶ if ($BITPIX eq "") {
Branch: 1 branch
BranchTrueFalseTotal
if ($BITPIX eq "") 33 1 34
1935 33 ▶ $BITPIX = 8 if $pdl->get_datatype == $PDL_B;
Branch: 1 branch
BranchTrueFalseTotal
if $pdl->get_datatype == $PDL::IO::FITS::PDL_B 2 31 33
1936 33 ▶ $BITPIX = 16 if $pdl->get_datatype == $PDL_S || $pdl->get_datatype == $PDL_US;
Branch: 1 branch
BranchTrueFalseTotal
if $pdl->get_datatype == $PDL::IO::FITS::PDL_S or $pdl->get_datatype == $PDL::IO::FITS::PDL_US 2 31 33
Condition66%2/3
$pdl->get_datatype == $PDL::IO::FITS::PDL_S or $pdl->get_datatype == $PDL::IO::FITS::PDL_US
l!l&&r!l&&!r
2031
MC/DC50%1/2
$pdl->get_datatype == $PDL::IO::FITS::PDL_S or $pdl->get_datatype == $PDL::IO::FITS::PDL_US
$pdl->get_datatype == $PDL::IO::FITS::PDL_S $pdl->get_datatype == $PDL::IO::FITS::PDL_US
Truth table66%2/3
A or B
A= $pdl->get_datatype == $PDL::IO::FITS::PDL_SB= $pdl->get_datatype == $PDL::IO::FITS::PDL_US
ABresult
1X1
011
000
1937 33 ▶ $BITPIX = 32 if $pdl->get_datatype == $PDL_L;
Branch: 1 branch
BranchTrueFalseTotal
if $pdl->get_datatype == $PDL::IO::FITS::PDL_L 3 30 33
1938 33 ▶ $BITPIX = 64 if $pdl->get_datatype == $PDL_LL;
Branch: 1 branch
BranchTrueFalseTotal
if $pdl->get_datatype == $PDL::IO::FITS::PDL_LL 1 32 33
1939 33 ▶ $BITPIX = -32 if $pdl->get_datatype == $PDL_F;
Branch: 1 branch
BranchTrueFalseTotal
if $pdl->get_datatype == $PDL::IO::FITS::PDL_F 2 31 33
1940 33 ▶ $BITPIX = -64 if $pdl->get_datatype == $PDL_D;
Branch: 1 branch
BranchTrueFalseTotal
if $pdl->get_datatype == $PDL::IO::FITS::PDL_D 23 10 33
1941 33 ▶ $BITPIX = 8 * PDL::Core::howbig($PDL_IND) if($pdl->get_datatype==$PDL_IND);
Branch: 1 branch
BranchTrueFalseTotal
if $pdl->get_datatype == $PDL::IO::FITS::PDL_IND 0 33 33
1942 }
1943 34 ▶ if ($BITPIX eq "") {
Branch: 1 branch
BranchTrueFalseTotal
if ($BITPIX eq "") 0 34 34
1944 0 $BITPIX = -64;
1945 0 warn "wfits: PDL has an unsupported datatype -- defaulting to 64-bit float.\n";
1946 }
1947  
1948 34 ▶ my $convert = sub { return $_[0] }; # Default - do nothing
Subroutine __ANON__: not called CC=1 SCAR=1.2
1949 34 ▶ $convert = sub {byte($_[0])} if $BITPIX == 8;
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if $BITPIX == 8 2 32 34
1950 34 ▶ $convert = sub {short($_[0])} if $BITPIX == 16;
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if $BITPIX == 16 2 32 34
1951 34 ▶ $convert = sub {long($_[0])} if $BITPIX == 32;
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if $BITPIX == 32 3 31 34
1952 34 ▶ $convert = sub {longlong($_[0])} if $BITPIX == 64;
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if $BITPIX == 64 1 33 34
1953 34 ▶ $convert = sub {float($_[0])} if $BITPIX == -32;
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if $BITPIX == -32 2 32 34
1954 34 ▶ $convert = sub {double($_[0])} if $BITPIX == -64;
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if $BITPIX == -64 24 10 34
1955
1956 # Automatically figure output scaling
1957
1958 34 my $bzero = 0; my $bscale = 1;
1959 34 ▶ if ($BITPIX>0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($BITPIX > 0) 8 26 34
1960 8 my $min = $pdl->min;
1961 8 my $max = $pdl->max;
1962 8 ▶ my ($dmin,$dmax) = (0, 2**8-1) if $BITPIX == 8;
Branch: 1 branch
BranchTrueFalseTotal
if $BITPIX == 8 2 6 8
1963 8 ▶ ($dmin,$dmax) = (-2**15, 2**15-1) if $BITPIX == 16;
Branch: 1 branch
BranchTrueFalseTotal
if $BITPIX == 16 2 6 8
1964 8 ▶ ($dmin,$dmax) = (-2**31, 2**31-1) if $BITPIX == 32;
Branch: 1 branch
BranchTrueFalseTotal
if $BITPIX == 32 3 5 8
1965 8 ▶ ($dmin,$dmax) = (-(pdl(longlong,1)<<63), (pdl(longlong,1)<<63)-1) if $BITPIX==64;
Branch: 1 branch
BranchTrueFalseTotal
if $BITPIX == 64 1 7 8
1966  
1967 8 ▶ if ($min<$dmin || $max>$dmax) {
Branch: 1 branch
BranchTrueFalseTotal
if ($min < $dmin or $max > $dmax) 0 8 8
Condition33%1/3
$min < $dmin or $max > $dmax
l!l&&r!l&&!r
008
MC/DC0%0/2
$min < $dmin or $max > $dmax
$min < $dmin $max > $dmax
Truth table33%1/3
A or B
A= $min < $dminB= $max > $dmax
ABresult
1X1
011
000
1968 0 $bzero = $min - $dmin;
1969 0 $max -= $bzero;
1970 0 ▶ $bscale = $max/$dmax if $max>$dmax;
Branch: 1 branch
BranchTrueFalseTotal
if $max > $dmax 0 0 0
1971 }
1972 8 ▶ print "BSCALE = $bscale && BZERO = $bzero\n" if $PDL::verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $PDL::verbose 0 8 8
1973 }
1974
1975 # Check for tile-compression format for the image, and handle it.
1976 # We add the image-compression format tags and reprocess the whole
1977 # shebang as a binary table.
1978 34 ▶ if($opt->{compress}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"compress"}) 0 34 34
1979 0 croak "Placeholder -- tile compression not yet supported\n";
1980 }
1981
1982
1983 ##############################
1984 ## Check header and prepare to write it out
1985
1986 34 my($h) = $pdl->gethdr();
1987
1988 # Extra logic: if we got handed a vanilla hash that that is *not* an Astro::FITS::Header, but
1989 # looks like it's a FITS header encoded in a hash, then attempt to process it with
1990 # Astro::FITS::Header before writing it out -- this helps with cleanup of tags.
1991 34 ▶ if($PDL::Astro_FITS_Header and
Branch: 1 branch
BranchTrueFalseTotal
if ($PDL::Astro_FITS_Header and defined $h and ref $h eq "HASH" and not defined tied %$h) 2 32 34
Condition66%6/9
$PDL::Astro_FITS_Header and defined $h and ref $h eq "HASH" and not defined tied %$h
!ll&&!rl&&r
3202
$PDL::Astro_FITS_Header and defined $h and ref $h eq "HASH"
!ll&&!rl&&r
3202
$PDL::Astro_FITS_Header and defined $h
!ll&&!rl&&r
0322
MC/DC25%1/4
$PDL::Astro_FITS_Header and defined $h and ref $h eq "HASH" and not defined tied %$h
$PDL::Astro_FITS_Header defined $h ref $h eq "HASH" not defined tied %$h
Truth table40%2/5
A and B and C and not(D)
A= $PDL::Astro_FITS_HeaderB= defined $hC= ref $h eq "HASH"D= not defined tied %$h
ABCDresult
0XXX0
10XX0
110X0
11100
11111
1992 defined($h) and
1993 ref($h) eq 'HASH' and
1994 !defined( tied %$h )
1995 ) {
1996
1997 2 my $all_valid_fits = 1;
1998 2 for my $k(keys %$h) {
1999 5 ▶ if(length($k) > 8 or
Branch: 1 branch
BranchTrueFalseTotal
if (length $k > 8 or not $k =~ /^[A-Z_][A-Z\d\_]*$/i) 0 5 5
Condition33%1/3
length $k > 8 or not $k =~ /^[A-Z_][A-Z\d\_]*$/i
l!l&&r!l&&!r
005
MC/DC0%0/2
length $k > 8 or not $k =~ /^[A-Z_][A-Z\d\_]*$/i
length $k > 8 not $k =~ /^[A-Z_][A-Z\d\_]*$/i
Truth table33%1/3
A or not(B)
A= length $k > 8B= not $k =~ /^[A-Z_][A-Z\d\_]*$/i
ABresult
1X1
011
000
2000 $k !~ m/^[A-Z_][A-Z\d\_]*$/i
2001 ) {
2002 0 $all_valid_fits = 0;
2003 0 last;
2004 }
2005 }
2006
2007 2 ▶ if($all_valid_fits) {
Branch: 1 branch
BranchTrueFalseTotal
if ($all_valid_fits) 2 0 2
2008 # All the keys look like valid FITS header keywords -- so
2009 # create a tied FITS header object and use that instead.
2010 2 my $afh = new Astro::FITS::Header( );
2011 2 my %hh;
2012 2 tie %hh, "Astro::FITS::Header", $afh;
2013 2 for (keys %$h) {
2014 5 $hh{$_} = $h->{$_};
2015 }
2016 2 $h = \%hh;
2017 }
2018 }
2019
2020 # Now decide whether to emit a hash or an AFH object
2021 34 ▶ if(defined($h) &&
Branch: 1 branch
BranchTrueFalseTotal
if (defined $h and (defined tied %$h and UNIVERSAL::isa(tied %$h, 'Astro::FITS::Header'))) { } 2 32 34
Condition50%3/6
defined $h and (defined tied %$h and UNIVERSAL::isa(tied %$h, "Astro::FITS::Header"))
!ll&&!rl&&r
3202
defined tied %$h and UNIVERSAL::isa(tied %$h, "Astro::FITS::Header")
!ll&&!rl&&r
002
MC/DC33%1/3
defined $h and (defined tied %$h and UNIVERSAL::isa(tied %$h, "Astro::FITS::Header"))
defined $h defined tied %$h UNIVERSAL::isa(tied %$h, "Astro::FITS::Header")
Truth table50%2/4
A and B and C
A= defined $hB= defined tied %$hC= UNIVERSAL::isa(tied %$h, "Astro::FITS::Header")
ABCresult
0XX0
10X0
1100
1111
2022 ( (defined (tied %$h)) &&
2023 (UNIVERSAL::isa(tied %$h,"Astro::FITS::Header")))
2024 ){
2025 2 my $k;
2026
2027 ##n############################
2028 ## Tied-hash code -- I'm too lazy to incorporate this into KGB's
2029 ## direct hash handler below, so I've more or less just copied and
2030 ## pasted with some translation. --CED
2031 ##
2032 2 my $hdr = tied %$h;
2033
2034 #
2035 # Put advertising comment in the SIMPLE field
2036 #n
2037 2 ▶ if($issue_nullhdu) {
Branch: 1 branch
BranchTrueFalseTotal
if ($issue_nullhdu) { } 0 2 2
2038 0 $h->{XTENSION} = "IMAGE";
2039 } else {
2040 2 $h->{SIMPLE} = 'T';
2041 2 my(@a) = $hdr->itembyname('SIMPLE');
2042 2 $a[0]->comment('Created with PDL (http://pdl.perl.org)');
2043 # and register it as a LOGICAL rather than a string
2044 2 $a[0]->type('LOGICAL');
2045 }
2046
2047 #
2048 # Use tied interface to set all the keywords. Note that this
2049 # preserves existing per-line comments, only changing the values.
2050 #
2051 2 $h->{BITPIX} = $BITPIX;
2052 2 $h->{NAXIS} = $pdl->getndims;
2053 2 my $correction = 0;
2054 2 for $k(1..$h->{NAXIS}) {
2055 $correction |= (exists $h->{"NAXIS$k"} and
2056 4 ▶ $h->{"NAXIS$k"} != $pdl->dim($k-1)
Condition33%1/3
exists $$h{"NAXIS$k"} && $$h{"NAXIS$k"} != $pdl->dim($k - 1)
!ll&&!rl&&r
400
MC/DC0%0/2
exists $$h{"NAXIS$k"} && $$h{"NAXIS$k"} != $pdl->dim($k - 1)
exists $$h{"NAXIS$k"} $$h{"NAXIS$k"} != $pdl->dim($k - 1)
Truth table33%1/3
A && B
A= exists $$h{"NAXIS$k"}B= $$h{"NAXIS$k"} != $pdl->dim($k - 1)
ABresult
0X0
100
111
2057 );
2058 4 $h->{"NAXIS$k"} = $pdl->getdim($k-1);
2059 }
2060 2 ▶ carp("Warning: wfits corrected dimensions of FITS header")
Branch: 1 branch
BranchTrueFalseTotal
if $correction 0 2 2
2061 if($correction);
2062
2063 2 ▶ $h->{BUNIT} = "Data Value" unless exists $h->{BUNIT};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $h->{"BUNIT"} 2 0 2
2064 2 ▶ $h->{BSCALE} = $bscale if($bscale != 1);
Branch: 1 branch
BranchTrueFalseTotal
if $bscale != 1 0 2 2
2065 2 ▶ $h->{BZERO} = $bzero if($bzero != 0);
Branch: 1 branch
BranchTrueFalseTotal
if $bzero != 0 0 2 2
2066
2067 2 ▶ if ( $pdl->badflag() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pdl->badflag) 0 2 2
2068 0 ▶ if ( $BITPIX > 0 ) { my $a = &$convert(pdl(0.0));
Branch: 1 branch
BranchTrueFalseTotal
if ($BITPIX > 0) { } 0 0 0
2069 0 $h->{BLANK} = $a->badvalue(); }
2070 0 else { delete $h->{BLANK}; }
2071 }
2072
2073 # Use object interface to sort the lines. This is complicated by
2074 # the need for an arbitrary number of NAXIS<n> lines in the middle
2075 # of the sorting. Keywords with a trailing '1' in the sorted-order
2076 # list get looped over.
2077 2 my($kk) = 0;
2078 2 my(@removed_naxis) = ();
2079 2 for $k(0..$#PDL::IO::FITS::wfits_keyword_order) {
2080 14 my($kn) = 0;
2081
2082 14 my @index;
2083 14 ▶ do { # Loop over numericised keywords (e.g. NAXIS1)
Branch: 1 branch
BranchTrueFalseTotal
defined $index[0] and $kn and do { do { my $kw = $PDL::IO::FITS::wfits_keyword_order[$k]; $kw .= ++$kn if $kw =~ s/\d$//; @index = $hdr->index($kw); if (defined $index[0]) { if ($kn <= $pdl->getndims) { $hdr->insert($kk, $hdr->remove($index[0])) unless $index[0] == $kk; ++$kk; } else { $hdr->removebyname($kw); push @removed_naxis, $kw; } ; } } } 4 14 18
Condition100%3/3
defined $index[0] and $kn
!ll&&!rl&&r
684
MC/DC100%2/2
defined $index[0] and $kn
defined $index[0] $kn
Truth table100%3/3
A and B
A= defined $index[0]B= $kn
ABresult
0X0
100
111
2084
2085 18 my $kw = $PDL::IO::FITS::wfits_keyword_order[$k]; # $kw get keyword
2086 18 ▶ $kw .= (++$kn) if( $kw =~ s/\d$//); # NAXIS1 -> NAXIS<n>
Branch: 1 branch
BranchTrueFalseTotal
if $kw =~ s/\d$// 6 12 18
2087 18 @index = $hdr->index($kw);
2088
2089 18 ▶ if(defined $index[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $index[0]) 12 6 18
2090 12 ▶ if($kn <= $pdl->getndims){
Branch: 1 branch
BranchTrueFalseTotal
if ($kn <= $pdl->getndims) { } 12 0 12
2091 12 ▶ $hdr->insert($kk, $hdr->remove($index[0]))
Branch: 1 branch
BranchTrueFalseTotal
unless $index[0] == $kk 10 2 12
2092 unless ($index[0] == $kk) ;
2093 12 $kk++;
2094 }
2095 else{ #remove e.g. NAXIS3 from hdr if NAXIS==2
2096 0 $hdr->removebyname($kw);
2097 0 push(@removed_naxis,$kw);
2098 }
2099 }
2100 } while((defined $index[0]) && $kn);
2101 }
2102
2103 2 foreach my $naxis(@removed_naxis){
2104 0 $naxis =~ m/(\d)$/;
2105 0 my $n = $1;
2106 0 foreach my $kw(@PDL::IO::FITS::wfits_numbered_keywords){
2107 0 $hdr->removebyname($kw . $n);
2108 }
2109 }
2110 #
2111 # Delete the END card if necessary (for later addition at the end)
2112 #
2113 2 $hdr->removebyname('END');
2114
2115 #
2116 # Make sure that the HISTORY lines all come at the end
2117 #
2118 2 my @hindex = $hdr->index('HISTORY');
2119 2 for $k(0..$#hindex) {
2120 0 $hdr->insert(-1-$k, $hdr->remove($hindex[-1-$k]));
2121 }
2122
2123 #
2124 # Make sure the last card is an END
2125 #
2126 2 $hdr->insert(scalar($hdr->cards),
2127 Astro::FITS::Header::Item->new(Keyword=>'END'));
2128
2129 #
2130 # Write out all the cards, and note how many bytes for later padding.
2131 #
2132 2 my $s = join("",$hdr->cards);
2133
2134 2 $fh->print( $s );
2135 2 $nbytes = length $s;
2136 } else {
2137 ##
2138 ## Legacy emitter (note different advertisement in the SIMPLE
2139 ## comment, for debugging!)
2140 ##
2141  
2142 32 ▶ if($issue_nullhdu) {
Branch: 1 branch
BranchTrueFalseTotal
if ($issue_nullhdu) { } 2 30 32
2143 2 $fh->printf( "%-80s", "XTENSION= 'IMAGE'" );
2144 } else {
2145 30 $fh->printf( "%-80s", "SIMPLE = T / PDL::IO::FITS::wfits (http://pdl.perl.org)" );
2146 }
2147
2148 32 $nbytes = 80; # Number of bytes written so far
2149
2150 # Write FITS header
2151
2152 32 %hdr = ();
2153 32 ▶ if (defined($h)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $h) 0 32 32
2154 0 for (keys %$h) { $hdr{uc $_} = $$h{$_} } # Copy (ensuring keynames are uppercase)
2155 }
2156
2157 32 delete $hdr{SIMPLE}; delete $hdr{'END'};
2158
2159 32 $hdr{BITPIX} = $BITPIX;
2160 32 ▶ $hdr{BUNIT} = "Data Value" unless exists $hdr{BUNIT};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $hdr{"BUNIT"} 32 0 32
2161 32 wheader($fh, 'BITPIX');
2162
2163 32 $ndims = $pdl->getndims; # Dimensions of data array
2164 32 $hdr{NAXIS} = $ndims;
2165 32 wheader($fh, 'NAXIS');
2166 32 for $k (1..$ndims) { $hdr{"NAXIS$k"} = $pdl->getdim($k-1) }
2167 32 for $k (1..$ndims) { wheader($fh,"NAXIS$k") }
2168
2169 32 ▶ if ($bscale != 1 || $bzero != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bscale != 1 or $bzero != 0) 0 32 32
Condition33%1/3
$bscale != 1 or $bzero != 0
l!l&&r!l&&!r
0032
MC/DC0%0/2
$bscale != 1 or $bzero != 0
$bscale != 1 $bzero != 0
Truth table33%1/3
A or B
A= $bscale != 1B= $bzero != 0
ABresult
1X1
011
000
2170 0 $hdr{BSCALE} = $bscale;
2171 0 $hdr{BZERO} = $bzero;
2172 0 wheader($fh,'BSCALE');
2173 0 wheader($fh,'BZERO');
2174 }
2175 32 wheader($fh,'BUNIT');
2176
2177 # IF badflag is set
2178 # and BITPIX > 0 - ensure the header contains the BLANK keyword
2179 # (make sure it's for the correct type)
2180 # otherwise - make sure the BLANK keyword is removed
2181 32 ▶ if ( $pdl->badflag() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pdl->badflag) 2 30 32
2182 2 ▶ if ( $BITPIX > 0 ) { my $a = &$convert(pdl(0.0)); $hdr{BLANK} = $a->badvalue(); }
Branch: 1 branch
BranchTrueFalseTotal
if ($BITPIX > 0) { } 0 2 2
2183 2 else { delete $hdr{BLANK}; }
2184 }
2185
2186 32 for $k (sort fits_field_cmp keys %hdr) {
2187 0 ▶ wheader($fh,$k) unless $k =~ m/HISTORY/;
Branch: 1 branch
BranchTrueFalseTotal
unless $k =~ /HISTORY/ 0 0 0
2188 }
2189 32 wheader($fh, 'HISTORY'); # Make sure that HISTORY entries come last.
2190 32 $fh->printf( "%-80s", "END" );
2191 32 $nbytes += 80;
2192 }
2193
2194 #
2195 # Pad the header to a legal value and write the rest of the FITS file.
2196 #
2197 34 $nbytes %= 2880;
2198 34 ▶ $fh->print( " "x(2880-$nbytes) )
Branch: 1 branch
BranchTrueFalseTotal
if $nbytes != 0 34 0 34
2199 if $nbytes != 0; # Fill up HDU
2200
2201 # Decide how to byte swap - note does not quite work yet. Needs hack
2202 # to IO.xs
2203
2204 34 ▶ my $bswap = sub {}; # Null routine
Subroutine __ANON__: called CC=1 SCAR=0.0
2205 34 ▶ if ( !isbigendian() ) { # Need to set a byte swap routine
Branch: 1 branch
BranchTrueFalseTotal
unless (isbigendian()) 34 0 34
2206 34 ▶ $bswap = \&bswap2 if $BITPIX==16;
Branch: 1 branch
BranchTrueFalseTotal
if $BITPIX == 16 2 32 34
2207 34 ▶ $bswap = \&bswap4 if $BITPIX==32 || $BITPIX==-32;
Branch: 1 branch
BranchTrueFalseTotal
if $BITPIX == 32 or $BITPIX == -32 5 29 34
Condition100%3/3
$BITPIX == 32 or $BITPIX == -32
l!l&&r!l&&!r
3229
MC/DC100%2/2
$BITPIX == 32 or $BITPIX == -32
$BITPIX == 32 $BITPIX == -32
Truth table100%3/3
A or B
A= $BITPIX == 32B= $BITPIX == -32
ABresult
1X1
011
000
2208 34 ▶ $bswap = \&bswap8 if $BITPIX==-64 || $BITPIX==64;
Branch: 1 branch
BranchTrueFalseTotal
if $BITPIX == -64 or $BITPIX == 64 25 9 34
Condition100%3/3
$BITPIX == -64 or $BITPIX == 64
l!l&&r!l&&!r
2419
MC/DC100%2/2
$BITPIX == -64 or $BITPIX == 64
$BITPIX == -64 $BITPIX == 64
Truth table100%3/3
A or B
A= $BITPIX == -64B= $BITPIX == 64
ABresult
1X1
011
000
2209 }
2210
2211 # Write FITS data
2212
2213 34 my $p1d = $pdl->copy->reshape($pdl->nelem); # Data as 1D stream;
2214
2215 34 $off = 0;
2216 34 $sz = PDL::Core::howbig(&$convert($p1d->slice('0:0'))->get_datatype);
2217
2218 34 $nbytes = $p1d->getdim(0) * $sz;
2219
2220 # Transfer data in blocks (because might need to byte swap)
2221 # Buffer is also type converted on the fly
2222
2223 34 my $BUFFSZ = 360*2880; # = ~1Mb - must be multiple of 2880
2224 34 my $tmp;
2225
2226 34 ▶ if ( $pdl->badflag() and $BITPIX < 0 and $PDL::Bad::UseNaN == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pdl->badflag and $BITPIX < 0 and $PDL::Bad::UseNaN == 0) 2 32 34
Condition66%4/6
$pdl->badflag and $BITPIX < 0 and $PDL::Bad::UseNaN == 0
!ll&&!rl&&r
3202
$pdl->badflag and $BITPIX < 0
!ll&&!rl&&r
3202
MC/DC33%1/3
$pdl->badflag and $BITPIX < 0 and $PDL::Bad::UseNaN == 0
$pdl->badflag $BITPIX < 0 $PDL::Bad::UseNaN == 0
Truth table50%2/4
A and B and C
A= $pdl->badflagB= $BITPIX < 0C= $PDL::Bad::UseNaN == 0
ABCresult
0XX0
10X0
1100
1111
2227 # just print up a message - conversion is actually done in the loop
2228 2 ▶ print "Converting PDL bad value to NaN\n" if $PDL::verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $PDL::verbose 0 2 2
2229 }
2230
2231 34 ▶ while ($nbytes - $off > $BUFFSZ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nbytes - $off > $BUFFSZ) 0 34 34
2232
2233 # Data to be transferred
2234
2235 0 $buff = &$convert( ($p1d->slice( ($off/$sz).":". (($off+$BUFFSZ)/$sz-1))
2236 -$bzero)/$bscale );
2237
2238 # if there are bad values present, and output type is floating-point,
2239 # convert the bad values to NaN's. We can ignore integer types, since
2240 # we have set the BLANK keyword
2241 #
2242 0 ▶ if ( $pdl->badflag() and $BITPIX < 0 and $PDL::Bad::UseNaN == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pdl->badflag and $BITPIX < 0 and $PDL::Bad::UseNaN == 0) 0 0 0
Condition0%0/6
$pdl->badflag and $BITPIX < 0 and $PDL::Bad::UseNaN == 0
!ll&&!rl&&r
000
$pdl->badflag and $BITPIX < 0
!ll&&!rl&&r
000
MC/DC0%0/3
$pdl->badflag and $BITPIX < 0 and $PDL::Bad::UseNaN == 0
$pdl->badflag $BITPIX < 0 $PDL::Bad::UseNaN == 0
Truth table0%0/4
A and B and C
A= $pdl->badflagB= $BITPIX < 0C= $PDL::Bad::UseNaN == 0
ABCresult
0XX0
10X0
1100
1111
2243 0 $buff->inplace->setbadtonan();
2244 }
2245
2246 0 &$bswap($buff);
2247 0 $fh->print( ${$buff->get_dataref} );
2248 0 $off += $BUFFSZ;
2249 }
2250 34 $buff = &$convert( ($p1d->slice($off/$sz.":-1") - $bzero)/$bscale );
2251
2252 34 ▶ if ( $pdl->badflag() and $BITPIX < 0 and $PDL::Bad::UseNaN == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pdl->badflag and $BITPIX < 0 and $PDL::Bad::UseNaN == 0) 2 32 34
Condition66%4/6
$pdl->badflag and $BITPIX < 0 and $PDL::Bad::UseNaN == 0
!ll&&!rl&&r
3202
$pdl->badflag and $BITPIX < 0
!ll&&!rl&&r
3202
MC/DC33%1/3
$pdl->badflag and $BITPIX < 0 and $PDL::Bad::UseNaN == 0
$pdl->badflag $BITPIX < 0 $PDL::Bad::UseNaN == 0
Truth table50%2/4
A and B and C
A= $pdl->badflagB= $BITPIX < 0C= $PDL::Bad::UseNaN == 0
ABCresult
0XX0
10X0
1100
1111
2253 2 $buff->inplace->setbadtonan();
2254 }
2255
2256 34 &$bswap($buff);
2257 34 $fh->print( ${$buff->get_dataref} );
2258 # Fill HDU and close
2259 # note that for the data space the fill character is \0 not " "
2260 #
2261 34 $fh->print( "\0"x(($BUFFSZ - $buff->getdim(0) * $sz)%2880) );
2262 } # end of image writing block
2263 else {
2264 # Not a PDL and not a hash ref
2265 0 barf("wfits: unknown data type - quitting");
2266 }
2267 } # end of output loop
2268 36 $fh->close();
2269 36 1;
2270 }
2271  
2272 ######################################################################
2273 ######################################################################
2274  
2275 # Compare FITS headers in a sensible manner.
2276  
2277 =head2 fits_field_cmp
2278  
2279 =for ref
2280  
2281 fits_field_cmp
2282  
2283 Sorting comparison routine that makes proper sense of the digits at the end
2284 of some FITS header fields. Sort your hash keys using "fits_field_cmp" and
2285 you will get (e.g.) your "TTYPE" fields in the correct order even if there
2286 are 140 of them.
2287  
2288 This is a standard kludgey perl comparison sub -- it uses the magical
2289 $a and $b variables, rather than normal argument passing.
2290  
2291 =cut
2292  
2293 sub fits_field_cmp {
2294 127 ▶ if( $a=~m/^(.*[^\d])(\d+)$/ ) {
Subroutine fits_field_cmp: called CC=4 SCAR=13.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if ($PDL::IO::FITS::a =~ /^(.*[^\d])(\d+)$/) 64 63 127
2295 64 my ($an,$ad) = ($1,$2);
2296 64 ▶ if( $b=~m/^(.*[^\d])(\d+)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($PDL::IO::FITS::b =~ /^(.*[^\d])(\d+)$/) 44 20 64
2297 44 my($bn,$bd) = ($1,$2);
2298
2299 44 ▶ if($an eq $bn) {
Branch: 1 branch
BranchTrueFalseTotal
if ($an eq $bn) 20 24 44
2300 20 return $ad<=>$bd;
2301 }
2302 }
2303 }
2304 107 $a cmp $b;
2305 }
2306  
2307 =head2 _rows()
2308  
2309 =for ref
2310  
2311 Return the number of rows in a variable for table entry
2312  
2313 You feed in a PDL or a list ref, and you get back the 0th dimension.
2314  
2315 =cut
2316  
2317 sub _rows {
2318 9 ▶ my $var = shift;
Subroutine _rows: called CC=5 SCAR=22.7
2319  
2320 9 ▶ return $var->dim(0) if( UNIVERSAL::isa($var,'PDL') );
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($var, "PDL") 8 1 9
2321 1 ▶ return 1+$#$var if(ref $var eq 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
if ref $var eq "ARRAY" 1 0 1
2322 0 ▶ return 1 unless(ref $var);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $var 0 0 0
2323
2324 0 ▶ print STDERR "Warning: _rows found an unacceptable ref. ".ref $var.". Ignoring...\n"
Branch: 1 branch
BranchTrueFalseTotal
if $PDL::verbose 0 0 0
2325 if($PDL::verbose);
2326
2327 0 return undef;
2328 }
2329  
2330  
2331  
2332 =head2 _prep_table()
2333  
2334 =for ref
2335  
2336 Accept a hash ref containing a table, and return a header describing the table
2337 and a string to be written out as the table, or barf.
2338  
2339 You can indicate whether the table should be binary or ascii. The default
2340 is binary; it can be overridden by the "tbl" field of the hash (if present)
2341 or by parameter.
2342  
2343 =cut
2344  
2345 our %bintable_types = (
2346 'byte'=>['B',1],
2347 'short'=>['I',2],
2348 4 ▶ 'ushort'=>['J',4, sub {return long shift;}],
Subroutine __ANON__: called CC=1 SCAR=0.0
2349 'long'=>['J',4],
2350 0 ▶ 'longlong'=>['D', 8, sub {return double shift;}],
Subroutine __ANON__: not called CC=1 SCAR=1.2
2351 'float'=>['E',4],
2352 'double'=>['D',8],
2353 # 'complex'=>['M',8] # Complex doubles are supported (actually, they aren't at the moment)
2354 );
2355  
2356  
2357 sub _prep_table {
2358 3 ▶ my ($hash,$tbl,$nosquish) = @_;
Subroutine _prep_table: called CC=67 SCAR=61.0
2359
2360 3 my $ohash;
2361  
2362 3 my $hdr = $hash->{hdr};
2363
2364 3 my $heap = "";
2365  
2366 # Make a local copy of the header.
2367 3 my $h = {};
2368 3 ▶ if(defined $hdr) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $hdr) 1 2 3
2369 1 local $_;
2370 1 for (keys %$hdr) {$h->{$_} = $hdr->{$_}};
2371 }
2372 3 $hdr = $h;
2373  
2374 3 ▶ $tbl = $hash->{tbl} unless defined($tbl);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tbl 0 3 3
2375  
2376 3 ▶ barf "_prep_table called without a HASH reference as the first argument"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $hash eq "HASH" 0 3 3
2377 unless ref $hash eq 'HASH';
2378  
2379 #####
2380 # Figure out how many columns are in the table
2381 3 ▶ my @colkeys = grep( ( !m/^(hdr|tbl)$/ and !m/^len_/ and defined $hash->{$_}),
Condition66%4/6
!(/^(hdr|tbl)$/ || /^len_/) && defined $hash->{$_}
!ll&&!rl&&r
109
/^(hdr|tbl)$/ || /^len_/
l!l&&r!l&&!r
109
MC/DC33%1/3
!(/^(hdr|tbl)$/ || /^len_/) && defined $hash->{$_}
/^(hdr|tbl)$/ /^len_/ defined $hash->{$_}
Truth table50%2/4
not(A || B) && C
A= /^(hdr|tbl)$/B= /^len_/C= defined $hash->{$_}
ABCresult
1XX0
01X0
0000
0011
2382 sort fits_field_cmp keys %$hash
2383 );
2384 3 my $cols = @colkeys;
2385  
2386 3 ▶ print "Table seems to have $cols columns...\n"
Branch: 1 branch
BranchTrueFalseTotal
if $PDL::verbose 0 3 3
2387 if($PDL::verbose);
2388
2389 #####
2390 # Figure out how many rows are in the table, and store counts...
2391 #
2392 3 my $rows;
2393 my $rkey;
2394 3 for my $key(@colkeys) {
2395 9 my $r = _rows($hash->{$key});
2396 9 ▶ ($rows,$rkey) = ($r,$key) unless(defined($rows) && $rows != 1);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rows and $rows != 1 3 6 9
Condition66%2/3
defined $rows and $rows != 1
!ll&&!rl&&r
306
MC/DC50%1/2
defined $rows and $rows != 1
defined $rows $rows != 1
Truth table66%2/3
A and B
A= defined $rowsB= $rows != 1
ABresult
0X0
100
111
2397 9 ▶ if($r != $rows && $r != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r != $rows and $r != 1) 0 9 9
Condition33%1/3
$r != $rows and $r != 1
!ll&&!rl&&r
900
MC/DC0%0/2
$r != $rows and $r != 1
$r != $rows $r != 1
Truth table33%1/3
A and B
A= $r != $rowsB= $r != 1
ABresult
0X0
100
111
2398 0 barf "_prep_table: inconsistent number of rows ($rkey: $rows vs. $key: $r)\n";
2399 }
2400 }
2401
2402 3 ▶ print "Table seems to have $rows rows...\n"
Branch: 1 branch
BranchTrueFalseTotal
if $PDL::verbose 0 3 3
2403 if($PDL::verbose);
2404  
2405 #####
2406 # Squish and disambiguate column names
2407 #
2408 3 my %keysbyname;
2409 my %namesbykey;
2410  
2411 3 ▶ print "Renaming hash keys...\n"
Branch: 1 branch
BranchTrueFalseTotal
if $PDL::debug 0 3 3
2412 if($PDL::debug);
2413  
2414 3 for my $key(@colkeys) {
2415 9 my $name = $key;
2416  
2417 9 $name =~ tr/[a-z]/[A-Z]/; # Uppercaseify (required by FITS standard)
2418 9 $name =~ s/\s+/_/g; # Remove whitespace (required by FITS standard)
2419
2420 9 ▶ unless($nosquish) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($nosquish) 9 0 9
2421 9 $name =~ s/[^A-Z0-9_-]//g; # Squish (recommended by FITS standard)
2422 }
2423
2424 ### Disambiguate...
2425 9 ▶ if(defined $ohash->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ohash->{$name}) 0 9 9
2426 0 my $iter = 1;
2427 0 my $name2;
2428 0 do { $name2 = $name."_".($iter++); }
2429 0 ▶ while(defined $ohash->{$name2});
Branch: 1 branch
BranchTrueFalseTotal
defined $ohash->{$name2} and do { do { $name2 = $name . '_' . $iter++ } } 0 0 0
2430 0 $name = $name2;
2431 }
2432  
2433 9 $ohash->{$name} = $hash->{$key};
2434 9 $keysbyname{$name} = $key;
2435 9 $namesbykey{$key} = $name;
2436  
2437 9 ▶ print "\tkey '$key'\t-->\tname '$name'\n"
Branch: 1 branch
BranchTrueFalseTotal
if $PDL::debug or $name ne $key and $PDL::verbose 0 9 9
Condition33%2/6
$PDL::debug or $name ne $key and $PDL::verbose
l!l&&r!l&&!r
009
$name ne $key and $PDL::verbose
!ll&&!rl&&r
900
MC/DC0%0/3
$PDL::debug or $name ne $key and $PDL::verbose
$PDL::debug $name ne $key $PDL::verbose
Truth table0%0/4
A or B and C
A= $PDL::debugB= $name ne $keyC= $PDL::verbose
ABCresult
1XX1
0111
00X0
0100
2438 if($PDL::debug || (($name ne $key) and $PDL::verbose));
2439 }
2440  
2441  
2442 # The first element of colnames is ignored (since FITS starts the
2443 # count at 1)
2444 #
2445 3 my @colnames; # Names by number
2446 my %colnums; # Numbers by name
2447  
2448 ### Allocate any table columns that are already in the header...
2449 3 local $_;
2450 3 map { for my $a(1) { # [Shenanigans to make next work right]
2451 2 ▶ next unless m/^TTYPE(\d*)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless /^TTYPE(\d*)$/ 0 2 2
2452  
2453 2 my $num = $1;
2454
2455 2 ▶ if($num > $cols || $num < 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($num > $cols or $num < 1) 0 2 2
Condition33%1/3
$num > $cols or $num < 1
l!l&&r!l&&!r
002
MC/DC0%0/2
$num > $cols or $num < 1
$num > $cols $num < 1
Truth table33%1/3
A or B
A= $num > $colsB= $num < 1
ABresult
1X1
011
000
2456 0 ▶ print "Ignoring illegal column number $num ( should be in range 1..$cols )\n"
Branch: 1 branch
BranchTrueFalseTotal
if $PDL::verbose 0 0 0
2457 if($PDL::verbose);
2458 0 delete $hdr->{$_};
2459 0 next;
2460 }
2461  
2462 2 my $key = $hdr->{$_};
2463  
2464 2 my $name;
2465 2 ▶ unless( $name = $namesbykey{$key}) { # assignment
Branch: 1 branch
BranchTrueFalseTotal
unless ($name = $namesbykey{$key}) 0 2 2
2466 0 $name = $key;
2467 0 ▶ unless( $key = $keysbyname{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($key = $keysbyname{$key}) 0 0 0
2468 0 ▶ print "Ignoring column $num in existing header (unknown name '$key')\n"
Branch: 1 branch
BranchTrueFalseTotal
if $PDL::verbose 0 0 0
2469 if($PDL::verbose);
2470 0 next;
2471 }
2472 }
2473  
2474 2 $colnames[$num] = $name;
2475 2 $colnums{$name} = $num;
2476 } } sort fits_field_cmp keys %$hdr;
2477  
2478 ### Allocate all other table columns in alphabetical order...
2479 3 my $i = 0;
2480 3 for my $k (@colkeys) {
2481 9 my $name = $namesbykey{$k};
2482  
2483 9 ▶ unless($colnums{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($colnums{$name}) { } 2 7 9
2484 7 ▶ while($colnames[++$i]) { }
Branch: 1 branch
BranchTrueFalseTotal
if ($colnames[++$i]) 0 7 7
2485 7 $colnames[$i] = $name;
2486 7 $colnums{$name} = $i;
2487 2 } else { $i++; }
2488 }
2489
2490 3 ▶ print "Assertion failed: i ($i) != colnums ($cols)\n"
Branch: 1 branch
BranchTrueFalseTotal
if $PDL::debug and $i != $cols 0 3 3
Condition33%1/3
$PDL::debug and $i != $cols
!ll&&!rl&&r
300
MC/DC0%0/2
$PDL::debug and $i != $cols
$PDL::debug $i != $cols
Truth table33%1/3
A and B
A= $PDL::debugB= $i != $cols
ABresult
0X0
100
111
2491 if($PDL::debug && $i != $cols);
2492  
2493 print "colnames: " .
2494 3 ▶ join(",", map { $colnames[$_]; } (1..$cols) ) ."\n"
Branch: 1 branch
BranchTrueFalseTotal
if $PDL::debug 0 3 3
2495 if($PDL::debug);
2496  
2497 ########
2498 # OK, now the columns are allocated -- spew out a header.
2499  
2500 3 my @converters = (); # Will fill up with conversion routines for each column
2501 3 my @field_len = (); # Will fill up with field lengths for each column
2502 3 my @internaltype = (); # Gets flag for PDLhood
2503 3 my @fieldvars = (); # Gets refs to all the fields of the hash.
2504  
2505 3 ▶ if($tbl eq 'binary') {
Branch: 2 branches
BranchTrueFalseTotal
if ($tbl eq 'binary') { } 3 0 3
elsif ($tbl eq 'ascii') { } 0 0 0
2506 3 $hdr->{XTENSION} = 'BINTABLE';
2507 3 $hdr->{BITPIX} = 8;
2508 3 $hdr->{NAXIS} = 2;
2509 #$hdr->{NAXIS1} = undef; # Calculated below; inserted here as placeholder.
2510 3 $hdr->{NAXIS2} = $rows;
2511 3 $hdr->{PCOUNT} = 0; # Change this is variable-arrays are adopted
2512 3 $hdr->{GCOUNT} = 1;
2513 3 $hdr->{TFIELDS} = $cols;
2514  
2515 # Figure out data types, and accumulate a row length at the same time.
2516  
2517 3 my $rowlen = 0;
2518  
2519 # NOTE:
2520 # the conversion from ushort to long below is a hack to work
2521 # around the issue that otherwise perl treats it as a 2-byte
2522 # NOT 4-byte string on writing out, which leads to data corruption
2523 # Really ushort arrays should be written out using SCALE/ZERO
2524 # so that it can be written as an Int2 rather than Int4
2525 #
2526 3 for my $i(1..$cols) {
2527 9 $fieldvars[$i] = $hash->{$keysbyname{$colnames[$i]}};
2528 9 my $var = $fieldvars[$i];
2529  
2530 9 $hdr->{"TTYPE$i"} = $colnames[$i];
2531 9 my $tform;
2532
2533 my $tstr;
2534 9 my $rpt;
2535 9 my $bytes;
2536
2537 9 ▶ if( UNIVERSAL::isa($var,'PDL') ) {
Branch: 3 branches
BranchTrueFalseTotal
if (UNIVERSAL::isa($var, 'PDL')) { } 8 1 9
elsif (ref $var eq 'ARRAY') { } 1 0 1
elsif (ref $var) { } 0 0 0
2538  
2539 8 $internaltype[$i] = 'P';
2540  
2541 8 my $t;
2542  
2543 8 my $dims = pdl($var->dims);
2544 8 ($t = $dims->slice("(0)")) .= 1;
2545 8 $rpt = $dims->prod;
2546  
2547 =pod
2548  
2549 =begin WHENCOMPLEXVALUESWORK
2550
2551 if( UNIVERSAL::isa($var,'PDL::Complex') ) {
2552 $rpt = $var->dim(1);
2553 $t = 'complex'
2554 } else {
2555 $t = type $var;
2556 }
2557  
2558 =end WHENCOMPLEXVALUESWORK
2559  
2560 =cut
2561  
2562 8 ▶ barf "Error: wfits() currently can not handle PDL::Complex arrays (column $colnames[$i])\n"
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($var, "PDL::Complex") 0 8 8
2563 if UNIVERSAL::isa($var,'PDL::Complex');
2564 8 $t = $var->type;
2565  
2566 8 $t = $bintable_types{$t};
2567
2568 8 ▶ unless(defined($t)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $t) 0 8 8
2569 0 ▶ print "Warning: converting unknown type $t (column $colnames[$i]) to double...\n"
Branch: 1 branch
BranchTrueFalseTotal
if $PDL::verbose 0 0 0
2570 if($PDL::verbose);
2571 0 $t = $bintable_types{'double'};
2572 }
2573  
2574 8 ($tstr, $bytes, $converters[$i]) = @$t;
2575  
2576 } elsif( ref $var eq 'ARRAY' ) {
2577  
2578 1 $internaltype[$i] = 'A';
2579 1 $bytes = 1;
2580
2581 # Got an array (of strings) -- find the longest element
2582 1 $rpt = 0;
2583 1 for(@$var) {
2584 4 my $l = length($_);
2585 4 ▶ $rpt = $l if($l>$rpt);
Branch: 1 branch
BranchTrueFalseTotal
if $l > $rpt 2 2 4
2586 }
2587 1 ($tstr, $bytes, $converters[$i]) = ('A',1,undef);
2588  
2589 } elsif( ref $var ) {
2590 0 barf "You seem to be writing out a ".(ref $var)." as a table column. I\ndon't know how to do that (yet).\n";
2591
2592 } else { # Scalar
2593 0 $internaltype[$i] = 'A';
2594 0 ($tstr, $bytes, $converters[$i]) = ('A',1,undef);
2595 0 $rpt = length($var);
2596 }
2597  
2598  
2599 # Now check if it's a variable-length array and, if so, insert an
2600 # extra converter
2601 9 my $lname = "len_".$keysbyname{$colnames[$i]};
2602 9 ▶ if(exists $hash->{$lname}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $hash->{$lname}) 0 9 9
2603 0 my $lengths = $hash->{$lname};
2604  
2605 # Variable length array - add extra handling logic.
2606  
2607 # First, check we're legit
2608 0 ▶ if( !UNIVERSAL::isa($var, 'PDL') ||
Branch: 1 branch
BranchTrueFalseTotal
if (not UNIVERSAL::isa($var, "PDL") or $var->ndims != 2 or not UNIVERSAL::isa($lengths, "PDL") or $lengths->ndims != 1 or $lengths->dim(0) != $var->dim(0)) 0 0 0
Condition0%0/12
not UNIVERSAL::isa($var, "PDL") or $var->ndims != 2 or not UNIVERSAL::isa($lengths, "PDL") or $lengths->ndims != 1 or $lengths->dim(0) != $var->dim(0)
l!l&&r!l&&!r
000
not UNIVERSAL::isa($var, "PDL") or $var->ndims != 2 or not UNIVERSAL::isa($lengths, "PDL") or $lengths->ndims != 1
l!l&&r!l&&!r
000
not UNIVERSAL::isa($var, "PDL") or $var->ndims != 2 or not UNIVERSAL::isa($lengths, "PDL")
l!l&&r!l&&!r
000
not UNIVERSAL::isa($var, "PDL") or $var->ndims != 2
l!l&&r!l&&!r
000
MC/DC0%0/5
not UNIVERSAL::isa($var, "PDL") or $var->ndims != 2 or not UNIVERSAL::isa($lengths, "PDL") or $lengths->ndims != 1 or $lengths->dim(0) != $var->dim(0)
not UNIVERSAL::isa($var, "PDL") $var->ndims != 2 not UNIVERSAL::isa($lengths, "PDL") $lengths->ndims != 1 $lengths->dim(0) != $var->dim(0)
Truth table0%0/6
not(A) or B or not(C) or D or E
A= not UNIVERSAL::isa($var, "PDL")B= $var->ndims != 2C= not UNIVERSAL::isa($lengths, "PDL")D= $lengths->ndims != 1E= $lengths->dim(0) != $var->dim(0)
ABCDEresult
1XXXX1
01XXX1
001XX1
0001X1
000011
000000
2609 $var->ndims != 2 ||
2610 !UNIVERSAL::isa($lengths,'PDL') ||
2611 $lengths->ndims != 1 ||
2612 $lengths->dim(0) != $var->dim(0)
2613 ) {
2614 0 die <<'FOO';
2615 wfits(): you specified a 'len_$keysbyname{$colnames[$i]}' field in
2616 your binary table output hash, indicating a variable-length array for
2617 each row of the output table, but I'm having trouble interpreting it.
2618 Either your source column isn't a 2-D PDL, or your length column isn't
2619 a 1-D PDL, or the two lengths don't match. I give up.
2620 FOO
2621 }
2622  
2623 # The definition below wraps around the existing converter,
2624 # dumping the variable to the heap and returning the length
2625 # and index of the data for the current row as a PDL LONG.
2626 # This does the Right Thing below in the write loop, with
2627 # the side effect of putting the data into the heap.
2628 #
2629 # The main downside here is that the heap gets copied
2630 # multiple times as we accumulate it, since we are using
2631 # string concatenation to add onto it. It might be better
2632 # to preallocate a large heap, but I'm too lazy to figure
2633 # out how to do that.
2634 0 my $csub = $converters[$i];
2635 $converters[$i] = sub {
2636 0 ▶ my $var = shift;
Subroutine __ANON__: not called CC=12 SCAR=50.5
2637 0 my $row = shift;
2638 0 my $col = shift;
2639
2640 0 my $len = $hash->{"len_".$keysbyname{$colnames[$i]}};
2641 0 my $l;
2642 0 ▶ if(ref $len eq 'ARRAY') {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $len eq 'ARRAY') { } 0 0 0
elsif (UNIVERSAL::isa($len, 'PDL')) { } 0 0 0
elsif (ref $len) { } 0 0 0
2643 0 $l = $len->[$row];
2644 } elsif( UNIVERSAL::isa($len,'PDL') ) {
2645 0 $l = $len->dice_axis(0,$row);
2646 } elsif( ref $len ) {
2647 0 die "wfits: Couldn't understand length spec 'len_".$keysbyname{$colnames[$i]}."' in bintable output (length spec must be a PDL or array ref).\n";
2648 } else {
2649 0 $l = $len;
2650 }
2651
2652 # The standard says we should give a zero-offset
2653 # pointer if the current row is zero-length; hence
2654 # the ternary operator.
2655 0 ▶ my $ret = pdl( $l, $l ? length($heap) : 0)->long;
Branch: 1 branch
BranchTrueFalseTotal
$l ? : 0 0 0
2656  
2657  
2658 0 ▶ if($l) {
Branch: 1 branch
BranchTrueFalseTotal
if ($l) 0 0 0
2659 # This echoes the normal-table swap and accumulation
2660 # stuff below, except we're accumulating into the heap.
2661 0 ▶ my $tmp = $csub ? &$csub($var, $row, $col) : $var;
Branch: 1 branch
BranchTrueFalseTotal
$csub ? : 0 0 0
2662 0 $tmp = $tmp->slice("0:".($l-1))->sever;
2663
2664 0 ▶ if(!isbigendian()) {
Branch: 1 branch
BranchTrueFalseTotal
unless (isbigendian()) 0 0 0
2665 0 ▶ bswap2($tmp) if($tmp->get_datatype == $PDL_S);
Branch: 1 branch
BranchTrueFalseTotal
if $tmp->get_datatype == $PDL::IO::FITS::PDL_S 0 0 0
2666 0 ▶ bswap4($tmp) if($tmp->get_datatype == $PDL_L ||
Branch: 1 branch
BranchTrueFalseTotal
if $tmp->get_datatype == $PDL::IO::FITS::PDL_L or $tmp->get_datatype == $PDL::IO::FITS::PDL_F 0 0 0
Condition0%0/3
$tmp->get_datatype == $PDL::IO::FITS::PDL_L or $tmp->get_datatype == $PDL::IO::FITS::PDL_F
l!l&&r!l&&!r
000
MC/DC0%0/2
$tmp->get_datatype == $PDL::IO::FITS::PDL_L or $tmp->get_datatype == $PDL::IO::FITS::PDL_F
$tmp->get_datatype == $PDL::IO::FITS::PDL_L $tmp->get_datatype == $PDL::IO::FITS::PDL_F
Truth table0%0/3
A or B
A= $tmp->get_datatype == $PDL::IO::FITS::PDL_LB= $tmp->get_datatype == $PDL::IO::FITS::PDL_F
ABresult
1X1
011
000
2667 $tmp->get_datatype == $PDL_F);
2668 0 ▶ bswap8($tmp) if($tmp->get_datatype == $PDL_D);
Branch: 1 branch
BranchTrueFalseTotal
if $tmp->get_datatype == $PDL::IO::FITS::PDL_D 0 0 0
2669 }
2670 0 my $t = $tmp->get_dataref;
2671 0 $heap .= $$t;
2672 }
2673  
2674 0 return $ret;
2675 0 };
2676  
2677 # Having defined the conversion routine, now modify tstr to make this a heap-array
2678 # reference.
2679 0 $tstr = sprintf("P%s(%d)",$tstr, $hash->{"len_".$keysbyname{$colnames[$i]}}->max );
2680 0 $rpt = 1;
2681 0 $bytes = 8; # two longints per row in the main table.
2682 }
2683  
2684
2685 9 $hdr->{"TFORM$i"} = "$rpt$tstr";
2686  
2687 9 ▶ if(UNIVERSAL::isa($var, 'PDL') and $var->ndims > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($var, "PDL") and $var->ndims > 1) 0 9 9
Condition66%2/3
UNIVERSAL::isa($var, "PDL") and $var->ndims > 1
!ll&&!rl&&r
180
MC/DC0%0/2
UNIVERSAL::isa($var, "PDL") and $var->ndims > 1
UNIVERSAL::isa($var, "PDL") $var->ndims > 1
Truth table66%2/3
A and B
A= UNIVERSAL::isa($var, "PDL")B= $var->ndims > 1
ABresult
0X0
100
111
2688 0 $hdr->{"TDIM$i"} = "(".join(",",$var->slice("(0)")->dims).")";
2689 }
2690  
2691 9 $rowlen += ($field_len[$i] = $rpt * $bytes);
2692 }
2693
2694 3 $hdr->{NAXIS1} = $rowlen;
2695
2696 ## Now accumulate the binary table
2697  
2698 3 my $table = "";
2699
2700 3 for my $r(0..$rows-1) {
2701 12 my $row = "";
2702 12 for my $c(1..$cols) {
2703 36 my $tmp;
2704 36 my $a = $fieldvars[$c];
2705
2706 36 ▶ if($internaltype[$c] eq 'P') { # PDL handling
Branch: 1 branch
BranchTrueFalseTotal
if ($internaltype[$c] eq 'P') { } 32 4 36
2707 $tmp = $converters[$c]
2708 32 ▶ ? &{$converters[$c]}($a->slice("$r")->flat->sever, $r, $c)
Branch: 1 branch
BranchTrueFalseTotal
$converters[$c] ? : 4 28 32
2709 : $a->slice("$r")->flat->sever ;
2710  
2711 ## This would go faster if moved outside the loop but I'm too
2712 ## lazy to do it Right just now. Perhaps after it actually works.
2713 ##
2714 32 ▶ if(!isbigendian()) {
Branch: 1 branch
BranchTrueFalseTotal
unless (isbigendian()) 32 0 32
2715 32 ▶ bswap2($tmp) if($tmp->get_datatype == $PDL_S);
Branch: 1 branch
BranchTrueFalseTotal
if $tmp->get_datatype == $PDL::IO::FITS::PDL_S 4 28 32
2716 32 ▶ bswap4($tmp) if($tmp->get_datatype == $PDL_L ||
Branch: 1 branch
BranchTrueFalseTotal
if $tmp->get_datatype == $PDL::IO::FITS::PDL_L or $tmp->get_datatype == $PDL::IO::FITS::PDL_F 16 16 32
Condition100%3/3
$tmp->get_datatype == $PDL::IO::FITS::PDL_L or $tmp->get_datatype == $PDL::IO::FITS::PDL_F
l!l&&r!l&&!r
12416
MC/DC100%2/2
$tmp->get_datatype == $PDL::IO::FITS::PDL_L or $tmp->get_datatype == $PDL::IO::FITS::PDL_F
$tmp->get_datatype == $PDL::IO::FITS::PDL_L $tmp->get_datatype == $PDL::IO::FITS::PDL_F
Truth table100%3/3
A or B
A= $tmp->get_datatype == $PDL::IO::FITS::PDL_LB= $tmp->get_datatype == $PDL::IO::FITS::PDL_F
ABresult
1X1
011
000
2717 $tmp->get_datatype == $PDL_F);
2718 32 ▶ bswap8($tmp) if($tmp->get_datatype == $PDL_D);
Branch: 1 branch
BranchTrueFalseTotal
if $tmp->get_datatype == $PDL::IO::FITS::PDL_D 8 24 32
2719 }
2720  
2721 32 my $t = $tmp->get_dataref;
2722 32 $tmp = $$t;
2723 } else { # Only other case is ASCII just now...
2724 4 ▶ $tmp = ( ref $a eq 'ARRAY' ) ? # Switch on array or string
Branch: 2 branches
BranchTrueFalseTotal
ref $a eq 'ARRAY' ? : 4 0 4
$a->$#* == 0 ? : 0 4 4
2725 ( $#$a == 0 ? $a->[0] : $a->[$r] ) # Thread arrays as needed
2726 : $a;
2727
2728 4 $tmp .= " " x ($field_len[$c] - length($tmp));
2729 }
2730
2731 # Now $tmp contains the bytes to be written out...
2732 #
2733 36 $row .= substr($tmp,0,$field_len[$c]);
2734 } # for: $c
2735 12 $table .= $row;
2736 } # for: $r
2737  
2738 3 my $table_size = $rowlen * $rows;
2739 3 ▶ if( (length $table) != $table_size ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $table != $table_size) 0 3 3
2740 0 print "Warning: Table length is ".(length $table)."; expected $table_size\n";
2741 }
2742  
2743 3 return ($hdr,$table, $heap);
2744
2745 } elsif($tbl eq 'ascii') {
2746 0 barf "ASCII tables not yet supported...\n";
2747 } else {
2748 0 barf "unknown table type '$tbl' -- giving up.";
2749 }
2750 }
2751  
2752 # the header fill value can be blanks, but the data fill value must
2753 # be zeroes in non-ASCII tables
2754 #
2755 sub _print_to_fits ($$$) {
2756 10 ▶ my $fh = shift;
Subroutine _print_to_fits: called CC=1 SCAR=0.0
2757 10 my $data = shift;
2758 10 my $blank = shift;
2759  
2760 10 my $len = ((length $data) - 1) % 2880 + 1;
2761 10 $fh->print( $data . ($blank x (2880-$len)) );
2762 }
2763
2764 {
2765 49 my $ctr = 0;
2766  
2767 3 ▶ sub reset_hdr_ctr() { $ctr = 0; }
Subroutine reset_hdr_ctr: called CC=1 SCAR=0.0
Pod undocumented
2768  
2769 sub add_hdr_item ($$$$;$) {
2770 48 ▶ my ( $hdr, $key, $value, $type, $comment ) = @_;
Subroutine add_hdr_item: called CC=3 SCAR=11.0
Pod undocumented
2771 48 ▶ $type = uc($type) if defined $type;
Branch: 1 branch
BranchTrueFalseTotal
if defined $type 30 18 48
2772 48 my $item = Astro::FITS::Header::Item->new( Keyword=>$key,
2773 Value=>$value,
2774 Type=>$type );
2775 48 ▶ $item->comment( $comment ) if defined $comment;
Branch: 1 branch
BranchTrueFalseTotal
if defined $comment 12 36 48
2776 48 $hdr->replace( $ctr++, $item );
2777 };
2778 }
2779  
2780 ##############################
2781 #
2782 # _wfits_table -- given a hash ref, try to write it out as a
2783 # table extension. The file FITS should be open when you call it.
2784 # Most of the work of creating the extension header, and all of
2785 # the work of creating the table, is handled by _prep_table().
2786 #
2787 # NOTE:
2788 # can not think of a sensible name for the extension so calling
2789 # it TABLE for now
2790 #
2791 49 sub _wfits_table ($$$) {
2792 3 ▶ my $fh = shift;
Subroutine _wfits_table: called CC=11 SCAR=24.6
2793 3 my $hash = shift;
2794 3 my $tbl = shift;
2795
2796 3 ▶ barf "FITS BINTABLES are not supported without the Astro::FITS::Header module.\nGet it from www.cpan.org.\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $PDL::Astro_FITS_Header 0 3 3
2797 unless($PDL::Astro_FITS_Header);
2798  
2799 3 my ($hdr,$table, $heap) = _prep_table($hash,$tbl,0);
2800 3 ▶ $heap="" unless defined($heap);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $heap 0 3 3
2801  
2802 # Copy the prepared fields into the extension header.
2803 3 tie my %newhdr,'Astro::FITS::Header',my $h = Astro::FITS::Header->new;
2804
2805 3 reset_hdr_ctr();
2806 3 ▶ add_hdr_item $h, "XTENSION", ($tbl eq 'ascii'?"TABLE":"BINTABLE"), 'string', "from perl hash";
Branch: 1 branch
BranchTrueFalseTotal
$tbl eq 'ascii' ? : 0 3 3
2807 3 add_hdr_item $h, "BITPIX", $hdr->{BITPIX}, 'int';
2808 3 add_hdr_item $h, "NAXIS", 2, 'int';
2809 3 add_hdr_item $h, "NAXIS1", $hdr->{NAXIS1}, 'int', 'Bytes per row';
2810 3 add_hdr_item $h, "NAXIS2", $hdr->{NAXIS2}, 'int', 'Number of rows';
2811 3 ▶ add_hdr_item $h, "PCOUNT", length($heap), 'int', ($tbl eq 'ascii' ? undef : "No heap") ;
Branch: 1 branch
BranchTrueFalseTotal
$tbl eq 'ascii' ? : 0 3 3
2812 3 ▶ add_hdr_item $h, "THEAP", "0", "(No gap before heap)" if(length($heap));
Branch: 1 branch
BranchTrueFalseTotal
if length $heap 0 3 3
2813 3 add_hdr_item $h, "GCOUNT", 1, 'int';
2814 3 add_hdr_item $h, "TFIELDS", $hdr->{TFIELDS},'int';
2815 3 add_hdr_item $h, "HDUNAME", "TABLE", 'string';
2816  
2817 3 for my $field( sort fits_field_cmp keys %$hdr ) {
2818 42 ▶ next if( defined $newhdr{$field} or $field =~ m/^end|simple|xtension$/i);
Branch: 1 branch
BranchTrueFalseTotal
if defined $newhdr{$field} or $field =~ /^end|simple|xtension$/i 24 18 42
Condition66%2/3
defined $newhdr{$field} or $field =~ /^end|simple|xtension$/i
l!l&&r!l&&!r
24018
MC/DC50%1/2
defined $newhdr{$field} or $field =~ /^end|simple|xtension$/i
defined $newhdr{$field} $field =~ /^end|simple|xtension$/i
Truth table66%2/3
A or B
A= defined $newhdr{$field}B= $field =~ /^end|simple|xtension$/i
ABresult
1X1
011
000
2819 my $type = (UNIVERSAL::isa($hdr->{field},'PDL') ?
2820 $hdr->{$field}->type :
2821 18 ▶ ((($hdr->{$field})=~ m/^[tf]$/i) ?
Branch: 2 branches
BranchTrueFalseTotal
UNIVERSAL::isa($hdr->{'field'}, 'PDL') ? : 0 18 18
$hdr->{$field} =~ /^[tf]$/i ? : 0 18 18
2822 'logical' :
2823 undef ## 'string' seems to have a bug - 'undef' works OK
2824 ));
2825  
2826 18 add_hdr_item $h, $field, $hdr->{$field}, $type, $hdr->{"${field}_COMMENT"};
2827 }
2828  
2829 3 add_hdr_item $h, "END", undef, 'undef';
2830  
2831 3 $hdr = join("",$h->cards);
2832 3 _print_to_fits( $fh, $hdr, " " );
2833 3 _print_to_fits( $fh, $table.$heap, "\0" ); # use " " if it is an ASCII table
2834 # Add heap dump
2835 }
2836  
2837 sub _wfits_nullhdu ($) {
2838 4 ▶ my $fh = shift;
Subroutine _wfits_nullhdu: called CC=2 SCAR=13.0
2839 4 ▶ if($Astro::FITS::Header) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Astro::FITS::Header) { } 0 4 4
2840 0 my $h = Astro::FITS::Header->new();
2841
2842 0 reset_hdr_ctr();
2843 0 add_hdr_item $h, "SIMPLE", "T", 'logical', "Null HDU (no data, only extensions)";
2844 0 add_hdr_item $h, "BITPIX", -32, 'int', "Needed to make fverify happy";
2845 0 add_hdr_item $h, "NAXIS", 0, 'int';
2846 0 add_hdr_item $h, "EXTEND", "T", 'logical', "File contains extensions";
2847 0 add_hdr_item $h, "COMMENT", "", "comment",
2848 " File written by perl (PDL::IO::FITS::wfits)";
2849 #
2850 # The following seems to cause a problem so removing for now (I don't
2851 # believe it is required, but may be useful for people who aren't
2852 # FITS connoisseurs). It could also be down to a version issue in
2853 # Astro::FITS::Header since it worked on linux with a newer version
2854 # than on Solaris with an older version of the header module)
2855 #
2856 ## add_hdr_item $h, "COMMENT", "", "comment",
2857 ## " FITS (Flexible Image Transport System) format is defined in 'Astronomy";
2858 ## add_hdr_item $h, "COMMENT", "", "comment",
2859 ## " and Astrophysics', volume 376, page 359; bibcode: 2001A&A...376..359H";
2860 0 add_hdr_item $h, "HDUNAME", "PRIMARY", 'string';
2861 0 add_hdr_item $h, "END", undef, 'undef';
2862
2863 0 my $hdr = join("",$h->cards);
2864 0 _print_to_fits( $fh, $hdr, " " );
2865 } else {
2866 4 _print_to_fits( $fh,
2867 q+SIMPLE = T / Null HDU (no data, only extensions) BITPIX = -32 / Needed to make fverify happy NAXIS = 0 EXTEND = T / File contains extensions COMMENT Written by perl (PDL::IO::FITS::wfits) legacy code. COMMENT For best results, install Astro::FITS::Header. HDUNAME = 'PRIMARY ' END +,
2868 " ");
2869 }
2870 }
2871  
2872
2873 49 1;
2874  
« blib/lib/PDL/IO/FlexRaw.pm ↑ Summary blib/lib/PDL/Graphics2D.pm »