Gri: running means

Chapters: 1: Introduction 2: Simple example 3: Invocation 4: Finer Control 5: X-Y Plots 6: Contour Plots 7: Image Plots 8: Examples 9: Gri Commands 10: Programming 11: Environment 12: Emacs Mode 13: History 14: Installation 15: Gri Bugs 16: Test Suite 17: Gri in Press 18: Acknowledgments 19: License Indices: Concepts Commands Variables | ## 8.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