top banner
Examples
1: Introduction
2: Simple example
3: Fancy example
4: Running Gri
5: Programming Gri
6: General Issues
7: X-Y Plots
8: Contour Plots
9: Image Plots
10: Examples
11: Handling Data
12: Gri Commands
13: Gri Extras
14: Evolution of Gri
15: Installing Gri
16: Gri Bugs
17: System Tools
18: Acknowledgments
19: License
20: Newsgroup

21: Concept Index
navigate navigate navigate navigate navigate navigate

10.10: Running-Mean Skyline Diagram

Timeseries data are often cast into running means; e.g. a temperature record might be cast into monthly mean values. The following example shows how to use a perl script to accomplish this easily, producing a graph with both the raw data (bullets) and the running mean (a skyline plot).
`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
bottom banner