`Bin with x .min. .max. .inc. \in_file \out_file'
Creates \out_file from \in_file. In each of these
files, column 1 represents x and column 2 represents
y. The \out_file file contains the average values
of y in x bands of width .inc., centred at .min.,
(.min.+.inc.), up to .max, and with missing values
inserted in bands with no x-data in \in_file.
Each x-band is represented in \out_file by a
plateau in y, and adjacent bands with
non-missing data are connnected by vertical
lines; the effect is a skyline plot of the
banded means. Sample application: plot
monthly means of a variable.
{
if {rpn \.words. 8 !=}
show "ERROR: `\.proper_usage.' called without"
show " giving all parameters"
quit 1
end if
system perl <<"EOF"
$min = \.word3.;
$max = \.word4.;
$inc = \.word5.;
open(IN, "\.word6.")
|| die "`\.proper_usage': no \\in_file";
open(OUT, ">\.word7.")
|| die "`\.proper_usage': no \\out_file";
$n = ($max - $min) / $inc;
#
# Set up bins.
for($i = 0; $i <= $n; $i++) {
$xx[$i] = 0;
$yy[$i] = 0;
$nn[$i] = 0;
}
while(<IN>) {
($x, $y) = split(' ');
$i = int(0.5 + ($x - $min) / $inc);
$i = 0 if $i < 0;
$i = $n - 1 if $i > $n - 1;
$xx[$i] += $x;
$yy[$i] += $y;
$nn[$i]++;
}
for($i = 0; $i <= $n; $i++) {
if ($nn[$i] > 0) {
$xx[$i] /= $nn[$i];
$yy[$i] /= $nn[$i];
$xleft = $min + $inc * ($i - 0.5);
$xright = $min + $inc * ($i + 0.5);
#
# If datum to left non-missing,
# just draw vertical line
# down to the last yy value.
if ($i > 0 && $nn[$i - 1] > 0) {
print OUT "$xleft $yy[$i - 1]\n";
} else {
print OUT "$xleft \.missingvalue.\n"
}
print OUT "$xleft $yy[$i]\n";
print OUT "$xright $yy[$i]\n";
}
}
EOF
}
# Bin into months
Bin with x 1964 1974 {rpn 1 12 /} \
timeseries.dat tmp.dat
open tmp.dat
read columns x y
close
draw curve # skyline of means
open timeseries.dat
read columns x y
close
draw symbol bullet # data
system rm -f tmp.dat # clean up