| 1 | package Text::NumericData::App::txdrecycle; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use Text::NumericData::App; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 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 | 1 | our $VERSION = '1'; | |||||||||||||||||||||||||||
| 11 | 1 | $VERSION = eval $VERSION; | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | #the infostring says it all | ||||||||||||||||||||||||||||
| 14 | 1 | my $infostring = 'Rearrange lines (records) in files in accordance to changing the viewport along a cyclic coordinate. | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | This is experimental work, so no usage example yet. But one hint: I designed this one with moving the viewport of gnuplot plots of cyclic data. It assumest sorted data along the coordinate (ascending or descending). To make it work with 3D plots, it processes blocks of data (separated by blank line) as independent "scans" (for gnuplot pm3d mode, for example).'; | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 1 | our @ISA = ('Text::NumericData::App'); | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | sub new | ||||||||||||||||||||||||||||
| 21 | { | ||||||||||||||||||||||||||||
| 22 | 1 | ▶ | my $class = shift; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 23 | 1 | my @pars = | |||||||||||||||||||||||||||
| 24 | ( | ||||||||||||||||||||||||||||
| 25 | 'column', 1, 'c', | ||||||||||||||||||||||||||||
| 26 | 'the column to use as coordiante' | ||||||||||||||||||||||||||||
| 27 | , 'shift', 0.25, 's', | ||||||||||||||||||||||||||||
| 28 | 'shift the viewport by that value (nearest existing data point), direction is subject to misunderstandings' | ||||||||||||||||||||||||||||
| 29 | , 'smother', 0, 'm', | ||||||||||||||||||||||||||||
| 30 | 'compute arithmetic mean of initial outer boundaries that are supposed to be identical, but are not' | ||||||||||||||||||||||||||||
| 31 | , 'smother-except', [], 'x', | ||||||||||||||||||||||||||||
| 32 | 'list of columns to extempt from smothering (in addition to the coordinate column)' | ||||||||||||||||||||||||||||
| 33 | ); | ||||||||||||||||||||||||||||
| 34 | 1 | return $class->SUPER::new | |||||||||||||||||||||||||||
| 35 | ({ | ||||||||||||||||||||||||||||
| 36 | parconf=> | ||||||||||||||||||||||||||||
| 37 | { | ||||||||||||||||||||||||||||
| 38 | info=>$infostring # default version, | ||||||||||||||||||||||||||||
| 39 | # default author | ||||||||||||||||||||||||||||
| 40 | # default copyright | ||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | ,pardef=>\@pars | ||||||||||||||||||||||||||||
| 43 | ,filemode=>0 | ||||||||||||||||||||||||||||
| 44 | ,pipemode=>1 | ||||||||||||||||||||||||||||
| 45 | ,pipe_init=>\&preinit | ||||||||||||||||||||||||||||
| 46 | ,pipe_begin=>\&begin | ||||||||||||||||||||||||||||
| 47 | ,pipe_line=>\&line | ||||||||||||||||||||||||||||
| 48 | ,pipe_end=>\&end | ||||||||||||||||||||||||||||
| 49 | }); | ||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | sub preinit | ||||||||||||||||||||||||||||
| 53 | { | ||||||||||||||||||||||||||||
| 54 | 1 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
preinit: called CC=2 SCAR=7.6
Pod
undocumented
| |||||||||||||||||||||||||||||
| 55 | 1 | my $param = $self->{param}; | |||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 1 | $self->{col} = $param->{column}-1; | |||||||||||||||||||||||||||
| 58 | 1 | ▶ | if($self->{col} < 0) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | { | ||||||||||||||||||||||||||||
| 60 | 0 | print STDERR "txdrecycle: Non-positive column does not work!\n"; | |||||||||||||||||||||||||||
| 61 | 0 | return -1; | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 1 | return 0; | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | sub line | ||||||||||||||||||||||||||||
| 68 | { | ||||||||||||||||||||||||||||
| 69 | 841 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
line: called CC=6 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 841 | ▶ | if(not $self->{data}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | { | ||||||||||||||||||||||||||||
| 73 | $self->{data} = 1 | ||||||||||||||||||||||||||||
| 74 | 3 | ▶ | if $self->{txd}->line_check($_[0]); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 841 | ▶ | if($self->{data}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | { | ||||||||||||||||||||||||||||
| 79 | 839 | ▶ | if($_[0] =~ /^\s*$/) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | { | ||||||||||||||||||||||||||||
| 81 | 13 | my $future = $_[0]; | |||||||||||||||||||||||||||
| 82 | 13 | $_[0] = ''; | |||||||||||||||||||||||||||
| 83 | 13 | $self->finish_block($_[0]); | |||||||||||||||||||||||||||
| 84 | 13 | $_[0] .= $future; | |||||||||||||||||||||||||||
| 85 | 13 | return; | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 826 | my $d = $self->{txd}->line_data($_[0]); | |||||||||||||||||||||||||||
| 89 | 826 | ▶ | if(defined $d) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | { | ||||||||||||||||||||||||||||
| 91 | 826 | push(@{$self->{block}}, $d); | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | 826 | $_[0] = ''; | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | sub begin | ||||||||||||||||||||||||||||
| 98 | { | ||||||||||||||||||||||||||||
| 99 | 1 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
begin: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 1 | $self->new_txd(); | |||||||||||||||||||||||||||
| 102 | 1 | $self->{block} = []; | |||||||||||||||||||||||||||
| 103 | 1 | $self->{data} = 0; | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | sub end | ||||||||||||||||||||||||||||
| 107 | { | ||||||||||||||||||||||||||||
| 108 | 1 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
end: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 1 | $self->finish_block($_[0]); | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | sub finish_block | ||||||||||||||||||||||||||||
| 114 | { | ||||||||||||||||||||||||||||
| 115 | 14 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
finish_block: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 116 | 14 | $self->recycle_block(); | |||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 826 | $_[0] .= ${$self->{txd}->data_line($_)} | |||||||||||||||||||||||||||
| 119 | 14 | for (@{$self->{block}}); | |||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | # Think about caching periods 'n' stuff and check consistency. | ||||||||||||||||||||||||||||
| 122 | 14 | $self->{block} = []; | |||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | # The scheme: | ||||||||||||||||||||||||||||
| 126 | # shift 'abcdea' by 3 letters -> 'deabcd' | ||||||||||||||||||||||||||||
| 127 | sub recycle_block | ||||||||||||||||||||||||||||
| 128 | { | ||||||||||||||||||||||||||||
| 129 | 14 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
recycle_block: called CC=14 SCAR=31.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 130 | 14 | my $param = $self->{param}; | |||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | # Nothing to do for such small data sets... cannot possibly make any sense. | ||||||||||||||||||||||||||||
| 133 | # I require the end points being identical, so to have something, there must be some data in between. | ||||||||||||||||||||||||||||
| 134 | 14 | ▶ | return if @{$self->{block}} < 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | # I support ascending and descending data. | ||||||||||||||||||||||||||||
| 137 | 14 | my $period = $self->{block}[$#{$self->{block}}][$self->{col}] - $self->{block}[0][$self->{col}]; | |||||||||||||||||||||||||||
| 138 | 14 | ▶ | my $dir = $period > 0 ? +1 : -1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 139 | 14 | $period = abs($period); | |||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | # Yeah, that check is not very floating-point safe. | ||||||||||||||||||||||||||||
| 142 | 14 | ▶ | return unless $period > 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | # Shift needs to be oriented according to sorting order. | ||||||||||||||||||||||||||||
| 145 | 14 | my $shift=$param->{shift}*$dir; | |||||||||||||||||||||||||||
| 146 | # Shift withing one period. | ||||||||||||||||||||||||||||
| 147 | 14 | $shift -= int($shift/$period)*$period; | |||||||||||||||||||||||||||
| 148 | # ... but still, positive! | ||||||||||||||||||||||||||||
| 149 | 14 | ▶ | $shift += $period if $shift < 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 150 | #print STDERR "real shift: $shift\n"; | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | # The point of split, beginning plus shift. | ||||||||||||||||||||||||||||
| 153 | 14 | my $i = nearest_index($self->{block}[0][$self->{col}]+$dir*$shift, $dir, \@{$self->{block}}); | |||||||||||||||||||||||||||
| 154 | #print STDERR "wrap point: $i\n"; | ||||||||||||||||||||||||||||
| 155 | 14 | ▶ | return if ($i == 0 or $i == $#{$self->{block}}); # No need to split there. | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$i == 0 or $i == $#{$$self{"block"};}
MC/DC0%0/2
$i == 0 or $i == $#{$$self{"block"};}
$i == 0
$i == $#{$$self{"block"};}
Truth table33%1/3
A or B
A= $i == 0B= $i == $#{$$self{"block"};}
| |||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 14 | my @a = @{$self->{block}}; # Remember, just a bunch of references. | |||||||||||||||||||||||||||
| 158 | # Need a copy before messing with the references. | ||||||||||||||||||||||||||||
| 159 | 14 | my @boundary = @{$a[$i]}; | |||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | # Start filling the recycled data. | ||||||||||||||||||||||||||||
| 162 | 14 | @{$self->{block}} = (@a[$i..$#a-1]); | |||||||||||||||||||||||||||
| 163 | # The data got moved one pace back, adjust coordinate. | ||||||||||||||||||||||||||||
| 164 | 14 | $_->[$self->{col}] -= $dir*$period for (@{$self->{block}}); | |||||||||||||||||||||||||||
| 165 | # Shove in the unchanged remaining data, plus the new boundary. | ||||||||||||||||||||||||||||
| 166 | # $a[0] is supposed to the identical to $a[$#a]. But in might not be so, | ||||||||||||||||||||||||||||
| 167 | # for a DG model, for example. Let's smother it a bit. | ||||||||||||||||||||||||||||
| 168 | 14 | ▶ | if($param->{smother}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 169 | { | ||||||||||||||||||||||||||||
| 170 | 0 | my @except = ($self->{col}, @{$param->{'smother-except'}}); | |||||||||||||||||||||||||||
| 171 | 0 | ▶ | for(my $i = 0; $i< @{$a[0]}; ++$i) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 172 | { | ||||||||||||||||||||||||||||
| 173 | $a[0][$i] = 0.5*($a[0][$i] + $a[$#a][$i]) | ||||||||||||||||||||||||||||
| 174 | 0 | ▶ | if(not grep { $_ == $i } @except and $a[0][$i] != $a[$#a][$i]); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not grep({$_ == $i;} @except) and $a[0][$i] != $a[$#a][$i]
MC/DC0%0/2
not grep({$_ == $i;} @except) and $a[0][$i] != $a[$#a][$i]
not grep({$_ == $i;} @except)
$a[0][$i] != $a[$#a][$i]
Truth table0%0/3
not(A) and B
A= not grep({$_ == $i;} @except)B= $a[0][$i] != $a[$#a][$i]
| |||||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||
| 177 | 14 | push(@{$self->{block}}, @a[0 .. $i-1]); | |||||||||||||||||||||||||||
| 178 | 14 | push(@{$self->{block}}, \@boundary); | |||||||||||||||||||||||||||
| 179 | # Bring the coordinates back into a sane range. | ||||||||||||||||||||||||||||
| 180 | # Am I sure that this is correct? Caring for $dir is tedious. | ||||||||||||||||||||||||||||
| 181 | # Examples: | ||||||||||||||||||||||||||||
| 182 | 14 | ▶ | if($dir*$self->{block}[0][$self->{col}] < -$period) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 183 | { | ||||||||||||||||||||||||||||
| 184 | $_->[$self->{col}] += $dir*$period | ||||||||||||||||||||||||||||
| 185 | 14 | for (@{$self->{block}}); | |||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | sub nearest_index | ||||||||||||||||||||||||||||
| 190 | { | ||||||||||||||||||||||||||||
| 191 | 14 | ▶ | my ($val, $dir, $arr) = (@_); | ||||||||||||||||||||||||||
|
Subroutine
nearest_index: called CC=6 SCAR=18.7
Pod
undocumented
| |||||||||||||||||||||||||||||
| 192 | 14 | ▶ | my $lower = $dir == +1 ? 0 : $#{$arr}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 193 | 14 | ▶ | my $upper = $dir == +1 ? $#{$arr} : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | # Hacking around to get a loop that runs both ways... | ||||||||||||||||||||||||||||
| 196 | 14 | ▶ | for(my $i=$lower+$dir; $i!=$upper+$dir; $i+=$dir) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 197 | { | ||||||||||||||||||||||||||||
| 198 | 154 | ▶ | if($arr->[$i][0] >= $val) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 199 | { | ||||||||||||||||||||||||||||
| 200 | 14 | $upper = $i; | |||||||||||||||||||||||||||
| 201 | 14 | $lower = $i-$dir; | |||||||||||||||||||||||||||
| 202 | 14 | last; | |||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||
| 205 | 14 | ▶ | return ($val - $arr->[$lower][0] < $arr->[$upper][0] - $val) ? $lower : $upper; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 206 | } | ||||||||||||||||||||||||||||