#!/usr/bin/perl -w # # dbmovingstats # Copyright (C) 1999 by John Heidemann # $Id: dbcolmovingstats,v 1.8 2003/05/23 04:17:12 johnh Exp $ # # This program is distributed under terms of the GNU general # public license, version 2. See the file COPYING # in $dblibdir for details. # sub usage { print STDERR <getopt) { $ch = $dbopts->opt; if ($ch eq 'f') { $format = $dbopts->optarg; } elsif ($ch eq 'a') { $bogus_are_ignored = 0; # $save_data = 1; } elsif ($ch eq 'e') { $empty_value = $dbopts->optarg; } elsif ($ch eq 'n') { $req_acc = $dbopts->optarg; } else { &usage; }; }; &usage if ($#ARGV != 0); my($xfcol) = $ARGV[0]; &readprocess_header; die ("$prog: unknown column ``$xfcol''.\n") if (!defined($colnametonum{$xfcol})); my($xf) = $colnametonum{$xfcol}; $mean_f = &col_create("moving_mean"); &write_header; my(@d) = (); my($sx) = 0; # my($sxx) = 0; # my($minmaxinit) = 0; # my($save_data_filename); # if ($save_data) { # $save_data_filename = db_tmpfile(TMP); # close TMP; # open(SAVE_DATA, "|$dbbindir/dbsort -n data >$save_data_filename") || die "$prog: cannot run dbsort.\n"; # print SAVE_DATA "$col_headertag data\n"; # } # # Read and process the data. # while () { &pass_comments && next; &split_cols; $x = &force_numeric($f[$xf], $bogus_are_ignored); next if (!defined($x)); push(@d, $x); $sx += $x; # $sxx += $x * $x; # print SAVE_DATA "$x\n" if ($save_data); if ($#d >= $req_acc) { $ox = shift @d; $sx -= $ox; # $sxx -= $ox * $ox; }; # if (!$minmaxinit) { # $min = $max = $x; # $minmaxinit = 1; # } else { # $min = $x if ($x < $min); # $max = $x if ($x > $max); # }; if ($empty_value && $#d+1 < $req_acc) { $mean = $empty_value; } else { $mean = $sx / ($#d + 1); $mean = sprintf("$format", $mean); }; $f[$mean_f] = $mean; &write_cols; }; print "# | $prog " . join(" ", @orig_argv) . "\n"; exit 0; # supress warings # error supression { my($dummy) = $f[0]; $dummy = $default_format; }