blib/lib/Astro/Funtools/Parse.pm

filter: Statementstmt 99.2% 136 / 137 Branchbran 75.7% 53 / 70 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 13 / 13 Podpod 100% 4 / 4 totaltotal 91.9% 206 / 224 CC 36 SCAR 19.4
CC
36
Cov
91%
_parse_table
12.4
parse_funcnts
9.0
_parse_funcnts_regions
7.0
CRAP 7.0
↑ Summary
1 package Astro::Funtools::Parse;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 require Exporter;
10  
11 1 our @ISA = qw(Exporter);
12  
13 # Items to export into callers namespace by default. Note: do not export
14 # names by default without a very good reason. Use EXPORT_OK instead.
15 # Do not simply export all your public functions/methods/constants.
16  
17 # This allows declaration use Astro::Parse::Funtools ':all';
18 # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
19 # will save memory.
20 1 our %EXPORT_TAGS = ( 'all' => [ qw(
21 parse_funcnts
22 parse_funcnts_file
23 parse_funhist
24 parse_funhist_file
25 ) ] );
26  
27 1 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
28  
29 1 our @EXPORT = qw(
30
31 );
32  
33 1 our $VERSION = '0.08';
34  
35 1 ▶use Data::LineBuffer;
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37  
38 # Preloaded methods go here.
39  
40 sub parse_funhist_file
41 1 ▶{
Subroutine parse_funhist_file: called CC=2 SCAR=7.1
Pod documented
42 1 my $file = shift;
43 1 ▶ my $fh = new IO::File $file
Branch: 1 branch
BranchTrueFalseTotal
my $fh = "IO::File"->new($file) or croak(__PACKAGE__, "::parse_funhist_file: unable to open $file\n") 1 0 1
44 or croak( __PACKAGE__, "::parse_funhist_file: unable to open $file\n" );
45  
46 1 parse_funhist( $fh );
47 }
48  
49 sub parse_funhist
50 1 ▶{
Subroutine parse_funhist: called CC=3 SCAR=11.1
Pod documented
51 1 my $what = shift;
52  
53 1 my %results;
54  
55 1 ▶ my $src = new Data::LineBuffer $what
Branch: 1 branch
BranchTrueFalseTotal
my $src = "Data::LineBuffer"->new($what) or croak(__PACKAGE__, "parse_funcnts: something wrong with argument\n") 1 0 1
56 or croak( __PACKAGE__, "parse_funcnts: something wrong with argument\n");
57  
58 1 my $header = _parse_header( $src );
59  
60 1 ▶ if ( exists $header->{_TOP}{'min,max,bins'} )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $header->{"_TOP"}{"min,max,bins"}) 1 0 1
61 {
62 1 my ( $min, $max, $bins ) = split( ' ', $header->{_TOP}{'min,max,bins'} );
63 1 delete $header->{_TOP}{'min,max,bins'};
64 1 @{$header->{_TOP}}{qw( min max bins) } = ( $min, $max, $bins );
65 }
66  
67 # grab the first thing; it's a table
68 1 my $table = _parse_table( $src );
69  
70 1 ( $header->{_TOP}, $table );
71 }
72  
73 sub parse_funcnts_file
74 14 ▶{
Subroutine parse_funcnts_file: called CC=2 SCAR=7.1
Pod documented
75 14 my $file = shift;
76 14 ▶ my $fh = new IO::File $file
Branch: 1 branch
BranchTrueFalseTotal
my $fh = "IO::File"->new($file) or croak(__PACKAGE__, "::parse_funcnts_file: unable to open $file\n") 14 0 14
77 or croak( __PACKAGE__, "::parse_funcnts_file: unable to open $file\n" );
78  
79 14 parse_funcnts( $fh );
80 }
81  
82  
83 sub parse_funcnts
84 14 ▶{
Subroutine parse_funcnts: called CC=9 SCAR=22.0
Pod documented
85 14 my $what = shift;
86  
87 14 my @results;
88  
89 14 ▶ my $src = new Data::LineBuffer $what
Branch: 1 branch
BranchTrueFalseTotal
my $src = "Data::LineBuffer"->new($what) or croak(__PACKAGE__, "parse_funcnts: something wrong with argument\n") 14 0 14
90 or croak( __PACKAGE__, "parse_funcnts: something wrong with argument\n");
91  
92 14 LINE: while ( 1 )
93 {
94 40 my %results;
95  
96 40 $results{hdr} = _parse_header( $src );
97  
98 40 ▶ last unless %{$results{hdr}};
Branch: 1 branch
BranchTrueFalseTotal
unless %{$results{"hdr"};} 14 26 40
99
100 # grab the first thing; it's a table
101 26 my $ln = $src->pos;
102 26 my $table = _parse_table( $src );
103
104 # is it a summed background-subtracted table?
105 26 ▶ if ( grep { 'upto' eq $_ } @{$table->{names}} )
Branch: 1 branch
BranchTrueFalseTotal
if (grep {'upto' eq $_;} @{$$table{'names'};}) { } 10 16 26
106 {
107 10 $results{sum_bkgd_sub}{table} = $table;
108 # next thing is the real background subtracted table, then
109 10 $ln = $src->pos;
110 10 $results{bkgd_sub}{table} = _parse_table( $src );
111
112 # but we'll check on that!
113 croak( __PACKAGE__,
114 "::parse_funcnts: line $ln: expected a background-subtracted ",
115 "table but didn't find one!\n" )
116 84 unless grep { 'reg' eq $_ }
117 10 ▶ @{$results{bkgd_sub}{table}->{names}};
Branch: 1 branch
BranchTrueFalseTotal
unless grep {"reg" eq $_;} @{$results{"bkgd_sub"}{"table"}{"names"};} 0 10 10
118 }
119
120 # nope, must be a background-subtracted table
121 else
122 {
123 16 $results{bkgd_sub}{table} = $table;
124 croak( __PACKAGE__,
125 "::parse_funcnts: line $ln: expected a background-subtracted ",
126 "table but didn't find one!\n" )
127 132 unless grep { 'reg' eq $_ }
128 16 ▶ @{$table->{names}};
Branch: 1 branch
BranchTrueFalseTotal
unless grep {"reg" eq $_;} @{$$table{"names"};} 0 16 16
129 }
130
131 # ok, now we're looking for regions, source and possibly background
132 # each region has a table after it.
133
134 26 $results{source}{regions} = _parse_funcnts_regions( $src );
135  
136 26 $results{source}{table} = _parse_table( $src );
137
138 # if there's a region left, it'll be the background
139
140 26 my $regions = _parse_funcnts_regions( $src );
141 26 ▶ if ( %$regions )
Branch: 1 branch
BranchTrueFalseTotal
if (%$regions) 24 2 26
142 {
143 24 ▶ if ( @{$regions->{regions}} )
Branch: 1 branch
BranchTrueFalseTotal
if (@{$regions->{"regions"};}) 20 4 24
144 {
145 20 $results{bkgd}{regions} = $regions;
146 20 $results{bkgd}{table} = _parse_table( $src );
147 }
148 24 _skip_past_formfeed( $src );
149 }
150 26 push @results, \%results;
151 }
152 return wantarray
153 ? @results
154 14 ▶ : $results[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 14 0 14
155 }
156  
157 sub _skip_past_formfeed
158 24 ▶{
Subroutine _skip_past_formfeed: called CC=3 SCAR=11.0
159 24 my $src = shift;
160  
161 24 local $_;
162  
163 24 ▶ for( my $ln = $src->pos; defined ($_ = $src->get) ; $ln = $src->pos )
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = $src->get)) 10 14 24
164 {
165 10 ▶ last if /^\f$/;
Branch: 1 branch
BranchTrueFalseTotal
if /^\f$/ 10 0 10
166 }
167 }
168  
169  
170 sub _parse_funcnts_regions
171 52 ▶{
Subroutine _parse_funcnts_regions: called CC=7 SCAR=19.5
172 52 my $src = shift;
173  
174 52 local $_;
175  
176 52 my $title;
177 my @regions;
178  
179 52 ▶ while( defined ($_ = $src->get) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = $src->get)) 100 4 104
180 {
181 100 ▶ return {} if /^\f$/;
Branch: 1 branch
BranchTrueFalseTotal
if /^\f$/ 2 98 100
182 98 ▶ next unless /^#\s+(.*)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless /^#\s+(.*)$/ 52 46 98
183 46 /^#\s+((source|background)_region\(s\))/;
184 46 ▶ return unless $_;
Branch: 1 branch
BranchTrueFalseTotal
unless $_ 0 46 46
185 46 $title = $1;
186 46 last;
187 }
188  
189 50 ▶ while( defined ($_ = $src->get) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = $src->get)) 273 4 277
190 {
191 273 ▶ last unless /^#\s+(.*)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless /^#\s+(.*)$/ 46 227 273
192 227 push @regions, $1;
193 }
194  
195 50 { title => $title, regions => \@regions };
196 }
197  
198  
199  
200 sub _parse_header
201 41 ▶{
Subroutine _parse_header: called CC=5 SCAR=16.1
202 41 my $src = shift;
203  
204 41 my %hdr;
205
206 41 local $_;
207  
208 41 my $key = '_TOP';
209 41 ▶ for( my $ln = $src->pos; defined ($_ = $src->get) ; $ln = $src->pos )
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = $src->get)) 302 14 316
210 {
211 302 ▶ last unless /^\#/;
Branch: 1 branch
BranchTrueFalseTotal
unless /^\#/ 27 275 302
212 275 ▶ if ( /:/ )
Branch: 1 branch
BranchTrueFalseTotal
if (/:/) { } 197 78 275
213 {
214 197 ▶ croak( __PACKAGE__,
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 0 197 197
215 "::_parse_header: line $ln: missing key in header\n" )
216 unless defined $key;
217
218 197 my ( $subkey, $val ) = /^\#\s+(.*):\s+(.*)/;
219 197 $subkey =~ s/\s+$//;
220 197 $subkey =~ s/\s/_/g;
221 197 $val =~ s/\s+$//;
222 197 $hdr{$key}{$subkey} = $val;
223 }
224
225 else
226 {
227 78 ( $key ) = /^\#\s+(.*)/;
228 78 $key =~ s/\s+$//;
229 }
230 }
231
232 41 \%hdr;
233 }
234  
235 sub _parse_table
236 83 ▶{
Subroutine _parse_table: called CC=12 SCAR=25.2
237 83 my $src = shift;
238  
239 83 my @records;
240 my @comments;
241 83 my @names;
242  
243 83 local $_;
244  
245 83 my $ln;
246  
247 # search for start of table. look for a leading `-'
248 # ignore empty lines. anything with a leading `#' is a comment.
249 # anything else is the list of column names.
250 83 ▶ for( $ln = $src->pos ; defined ( $_ = $src->get ) ; $ln = $src->pos )
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = $src->get)) 258 0 258
251 {
252 258 ▶ next if /^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if /^\s*$/ 10 248 258
253  
254 248 ▶ last if /^-+/;
Branch: 1 branch
BranchTrueFalseTotal
if /^-+/ 83 165 248
255  
256 165 ▶ if ( /^\#(.*)$/ )
Branch: 1 branch
BranchTrueFalseTotal
if (/^\#(.*)$/) { } 82 83 165
257 {
258 82 push @comments, $1;
259 }
260  
261 else
262 {
263 83 ▶ croak( __PACKAGE__,
Branch: 1 branch
BranchTrueFalseTotal
if @names 0 83 83
264 "::_parse_table: line $ln: more than one set of column headers?\n" )
265 if @names;
266  
267 83 chomp;
268 83 @names = split;
269 }
270 }
271  
272 83 ▶ croak( __PACKAGE__, "::_parse_table: line $ln: no table here!\n" )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_ 0 83 83
273 unless defined $_;
274  
275 83 my @widths = map { length($_) } split(' ', $_ );
276 83 ▶ croak( __PACKAGE__,
Branch: 1 branch
BranchTrueFalseTotal
if @names != @widths 0 83 83
277 "::_parse_table: line $ln: inconsistent number of column names and separators" )
278 if @names != @widths;
279  
280  
281 # work around extra blank line between header and data
282 83 $_ = $src->get;
283 83 ▶ $src->unget( $_ )
Branch: 1 branch
BranchTrueFalseTotal
unless /^\s*$/ 83 0 83
284 unless /^\s*$/;
285  
286 83 ▶ for ( my $ln = $src->pos; defined ($_ = $src->get) ;$ln = $src->pos )
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = $src->get)) 530 1 531
287 {
288 530 ▶ last if /^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if /^\s*$/ 82 448 530
289  
290 448 chomp;
291 448 my @data = split;
292 448 ▶ unless ( @data == @names )
Branch: 1 branch
BranchTrueFalseTotal
unless (@data == @names) 0 448 448
293 {
294 0 croak( __PACKAGE__,
295 "::_parse_table: line $ln: number of columns and number of data elements differ" )
296 }
297 448 my %data;
298 448 @data{@names} = @data;
299 448 push @records, \%data;
300 }
301  
302 83 return { comments => \@comments,
303 names => \@names,
304 widths => \@widths,
305 records => \@records };
306 }
307  
308  
309 1 1;
310 __END__
↑ Summary