#!/usr/local/bin/perl

if (@ARGV == 0) 
{
        die "USAGE: the_script data_file SKIP_STEPS TOT_BLOCK TIME_STEP\n\t OUTPUT GIVES AVERAGE OF COLS 2-4";
}

# NUMBER OF STEPS TO SKIP BEFORE STARTING AVERAGING
$skip      = $ARGV[1];
# TOTAL NUMBER OF STEPS, SKIP + AVE STEPS, PER BLOCK
$tot_block = $ARGV[2];

# TIME STEP
$step      = $ARGV[3];

$counter_block_old = 0;

$accum_1 = 0;
$accum_2 = 0; 
$accum_3 = 0;
$tot_num = 0;

while(<>)
{
    if (/^\#/)
    {
    }
    else
    {
       @process = split(' ',$_);

       $counter_block = ($process[0]/$step) % $tot_block;

       # CHECK FOR WRAPAROUND
       if ( $counter_block_old > $counter_block )
       {
         $accum_1 /= $tot_num;
         $accum_2 /= $tot_num;
         $accum_3 /= $tot_num;
         print $accum_1," ",$accum_2," ",$accum_3,"\n";

         $accum_1 = 0;
         $accum_2 = 0; 
         $accum_3 = 0;
         $tot_num = 0;
       }

       # DO AVERAGES AFTER THERMALIZING AND BEFORE THE NEXT BLOCK
       if ( $counter_block >= $skip )
       {
         # ENERGY ACCUMULATOR
         $accum_1 += $process[1];
         $accum_2 += $process[2];
         $accum_3 += $process[3];

         $tot_num += 1;
       }

       $counter_block_old = $counter_block;
    }

}