#!/usr/bin/perl -w

$fn = shift || die("Please give me the name of the rotacf file");
$gn = "S2.xvg";

open(KKK,"$fn") || die("Can not open $fn for reading");
open(LLL,">$gn") || die("Can not open $gn for writing");

$start = 0;
$res   = 2;
$n     = 0;
@val   = ();
while($line=<KKK>) {
    if (index($line,"\@TYPE") >= 0) {
	$start = 1;
    }
    elsif (index($line,"&") >= 0) {
	$aver = 0;
	for($m=$n/2; ($m<$n); $m++) {
	    $aver += $val[$m];
	}
	printf(LLL "%3d  %7.3f\n",$res,$aver/($n-$n/2));
	@val = ();
	$n = 0;
	$res++;
	if ($res == 19) { $res++; }
	if ($res == 38) {  $res += 2; }
    }
    elsif ($start > 0) {
	@tmp = split(' ',$line);
	$val[$n++] = $tmp[1];
    }
}
close LLL;
close KKK;
