#!/usr/bin/perl # # favg [] # # # Read command line parameters # $iopt=0; if (substr($ARGV[0],0,1) eq "-") { $StartStr = $ARGV[1]; $StopStr = $ARGV[2]; if ($StartStr ne "-") { $WaitForStart = 1; } if ($StopStr ne "-") { $LookForStop = 1; } $iopt+=3; } # Number of variables $NumVar = $#ARGV+1-$iopt; # # Test for no input # if ($NumVar<1) { print "\n"; print "favg [-s \"start\" \"stop\"] file\n"; print "\n"; print "Read columns of number from data file and returns the\n"; print "average of each column for lines beginning with a number.\n"; print "Also returns the estimated error, the estimated correlated\n"; print "error, and the estimated correlation length.\n"; print "\n"; print "Optional \"start\" and \"stop\" strings determine first and\n"; print "last file lines. The first line containing the string\n"; print "\"start\" signals the following line as the first data line.\n"; print "The first string containing the string \"stop\" signals the\n"; print "previous lines as the end of data. The special character -\n"; print "(the minus sign) can be used in place of \"start\" or \"stop\".\n"; print "It means start at the file beginning, or stop at the file\n"; print "end.\n"; print "\n"; print "Sample output...\n"; print "\n"; print <\n"; exit; } # # Read and store values # $NumRead = 0; $NumVar = 0; # # Scan each line in file # while ($line = ) { # # Remove trailing \n character # chop $line; # # Look for start string # if ($WaitForStart) { # if ($line =~ /[ \t]+$StartStr/) if ($line =~ /$StartStr/) { $WaitForStart = 0; } next; } # # Look for stop string # if ($LookForStop) { # if ($line =~ /[ \t]+$StopStr/) if ($line =~ /$StopStr/) { last; } } # # Split into tokens # $line =~ s/^[ \t]+//; @Token = split(/[ \t\n]+/,$line); # # if first token is blank, throw it away # if ($Token[0] eq "") { shift(@Token); } # # Is first token a number? # if (! ($Token[0] =~ /^[0-9\-\+\.]+$/) ) { next; } # # Sum tokens # for ($i=0; $i<=$#Token; $i++) { # Use first value as offset if ($Count[$i]==0) { $Offset [$i] = $Token[$i]; $Sum [$i] = 0; $Sum2 [$i] = 0; $CumSum2N[$i] = 0; $CumSumN [$i] = 0; $CumN [$i] = 0; } # Increment variable count $Count[$i] ++; $NumRead++; # Condition this variable $Token[$i] -= $Offset[$i]; # Take sums $Sum[$i] += $Token[$i]; $Sum2[$i] += $Token[$i]*$Token[$i]; $CumSum2N[$i] += $Sum[$i]*$Sum[$i]/$Count[$i]; $CumSumN [$i] += $Sum[$i] /$Count[$i]; $CumN [$i] += 1 /$Count[$i]; } # # Record maximun number of variables # if ($#Token+1>$NumVar) { $NumVar = $#Token+1; } } # # Close file # close InputFile; # # Write results # print "Column Count Average Error Corr. Error Corr. Length\n"; print "------ -------- -------------- ------------ ------------ ------------\n"; for ($i=0; $i<$NumVar; $i++) { if ($Count[$i]==0) { } elsif ($Count[$i]==1) { printf ("%3i %3i %14.6e\n", $i, $Count[$i], $Sum[$i]); } else { $Avg = $Sum[$i]/$Count[$i]; $Err = ($Sum2[$i]/$Count[$i]-$Avg[$i]*$Avg[$i])/$Count[$i]; $CorrErr = ($CumSum2N[$i] - $Avg*$CumSumN[$i] + $Avg*$Avg*$CumN[$i]); $CorrErr /= $Count[$i]*$Count[$i]; # Calculate correlation length $CorrLen = 0; if ($Err != 0) { $CorrLen = $CorrErr/$Err; } # Take sqrt() if ($Err>0) { $Err = sqrt($Err); } if ($CorrErr>0) { $CorrErr = sqrt($CorrErr); } } # Note - must add Offset[] only after Avg is used in above statements $Avg += $Offset[$i]; printf ( "%6d %8d %14.6le %12.4le %12.4le %12.4le\n", $i+1, $Count[$i], $Avg, $Err, $CorrErr, $CorrLen ); }