#!/usr/bin/perl

%timevals;
$n_comments=0;
$n_values=0;

$bin100=0;
$threshold=0;

$nargs = scalar(@ARGV);
if($nargs > 0){
    $threshold=int($ARGV[0]);
    print STDERR "threshold for checking = $threshold\n";
}
else{
    print STDERR "no threshold argument; no checking will be done.\n";
}

$is_over_threshold=0;
while(<STDIN>){
    chomp;
    if(m/^\D.*/){
	$n_comments++;
	if($is_over_threshold){
	    print STDERR "over threshold line; [comment=$n_comments]: $last_marker_line\n";
	}
	$is_over_threshold=0;
	$last_marker_line = $_;
    }
    else{
	$n_values++;
	($time,$value) = $_ =~ m/\s*\d+\s+(\d+)\s+(\d+)\s+.+/;
	$timekey = sprintf("%09d",$time);
#	print "time = $timekey, value = $value\n";
	if($timevals{$timekey}){
	    $timevals{$timekey} += $value;
	}
	else{
	    $timevals{$timekey} = $value;
	}
	if($threshold && ((int($time)) gt $threshold)){
	    $is_over_threshold=1;
	}
    }
}

#print "There were $n_comments comment lines\n";
#print "and $n_values lines with values.\n";
#print "printing out time histogram:\n\n";

#while(($time,$value)=each(%timevals)){
#    print " histo time=$time = $value (counts)\n";
#}

#print "the 100 bin is $bin100\n";
foreach $time (sort keys %timevals){
#    print " histo time($time) value = $timevals{$time} counts\n";
    print " $time $timevals{$time}\n";
}

