blib/lib/Text/NumericData/App/txdbaseline.pm untested

filter: Statementstmt 0.0% 0 / 38 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 2 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 5 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 62 SCAR 42.8
CC
8
Cov
0%
process_file
42.0
prepare
12.0
new
2.0
CRAP 72.0
1 package Text::NumericData::App::txdbaseline;
2  
3 use Text::NumericData::App;
4  
5 use strict;
6  
7 # This is just a placeholder because of a past build system bug.
8 # The one and only version for Text::NumericData is kept in
9 # the Text::NumericData module itself.
10 our $VERSION = '1';
11 $VERSION = eval $VERSION;
12  
13 #the infostring says it all
14 my $infostring = 'shift a data set down to have the baseline at zero, including optional limiting of undershoots
15  
16 Orginal motivation was correcting of measured optical spectra for an offset inherent in the measurement apparatus.';
17  
18 our @ISA = ('Text::NumericData::App');
19  
20  
21 sub new
22 {
23 my $class = shift;
24 my @pars =
25 (
26 'xcol',1,'x','the x column (i.e. wavelength)',
27 'ycol',2,'y','the y column (i.e. intensity)',
28 'begin',0,'b','begin of baseline interval',
29 'end',1,'e','end of baseline interval',
30 'lowest',0,'l','take the ... lowest values instead of interval',
31 'positive',0,'p','cut values below zero (making them zero; leaving only non-negative numbers)'
32 );
33 return $class->SUPER::new
34 ({
35 parconf=>
36 {
37 info=>$infostring # default version,
38 # default author
39 # default copyright
40 }
41 ,pardef=>\@pars
42 ,filemode=>1
43 ,pipemode=>1
44 ,pipe_init=>\&prepare
45 ,pipe_file=>\&process_file
46 });
47 }
48  
49 sub prepare
50 {
51 my $self = shift;
52 my $param = $self->{param};
53 for('xcol','ycol')
54 {
55 $param->{$_} = int($param->{$_}); #paranoia
56 return $self->error('Invalid column!') unless $param->{$_} > 0;
57 }
58 return $self->error('Negative number of values???') if $param->{lowest} < 0;
59 }
60  
61 sub process_file
62 {
63 my $self = shift;
64 my $param = $self->{param};
65 my $mean;
66 unless($param->{lowest} > 0)
67 {
68 #calculate mean over interval
69 $mean = $self->{txd}->mean($param->{ycol}-1, $param->{xcol}-1, $param->{begin}, $param->{end});
70 }
71 else
72 {
73 #calculate mean over lowest values
74 my $y = $param->{ycol}-1;
75 my $d = $self->{txd}->get_sorted_data($y);
76 #the n lowest values are in the n first elements of @{$d}
77 my $count = 0;
78 foreach my $i (@{$d})
79 {
80 $mean += $i->[$y];
81 last if ++$count == $param->{lowest};
82 }
83 $mean /= $count if $count > 0;
84 }
85 #substract mean
86 if(defined $mean)
87 {
88 my $formula = '['.$param->{ycol}.'] -= '.$mean;
89 $formula .= '; ['.$param->{ycol}.'] = 0 if ['.$param->{ycol}.'] < 0' if $param->{positive};
90 $self->{txd}->calc($formula) or die "Error with calculation!\n";
91 }
92 $self->{txd}->write_all($self->{out});
93 }
94  
95 1;