#!/usr/local/bin/perl
# summarize referee reports for ISCA

# pno = paper number
# ptl = paper title
# cnf = confidence rating
# imp = importance
# qty = quality
# tch = technical 
# act = action
# rnm = referee name
# raf = referee affiliation

# nrv = number of reviews
# apno = active paper number
# ccnf = cumulative confidence rating
# cimp = cumulative importance
# cqty = cumulative quality
# ctch = cumulative technical 
# cact = cumulative action
# @act = all action scores
# cvar = cumulative variance--prints sqrt

sub INIT {
    # initialize state for new paper
    &OUTPUT if ($apno != -1);
    $apno = $pno;
    $aptl = $ptl;
    $nrv = 0;
    $ccnf = 0;
    $cimp = 0;
    $cqty = 0;
    $ctch = 0;
    $cact = 0;
}

sub OUTPUT {
    # calculate averages
    $ccnf /= $nrv;
    $cimp /= $nrv;
    $cqty /= $nrv;
    $ctch /= $nrv;
    $cact /= $nrv;

    # calculate variance
    $i = $nrv;
    $cvar = 0;
    $cvar += ($cact[$i--] - $cact)**2 while ($i);
    $cvar /= $nrv;
    $cvar = sqrt($cvar);
    
    $~ = DATAV if ($verbose);
    write;
    $~ = ECHO if ($verbose);
}

$verbose = 0;

while ($_ = $ARGV[0], /^-/) {
    shift;
    last if /^--$/;
    /^-b/ && ($verbose = 0);
    /^-v/ && ($verbose = 1);
}

$~ = $verbose ? ECHO : DATA;
$^ = $verbose ? TOPV : TOP;

$apno = -1;
$pno = -1;
&INIT;

while (<>) {
    ($pno, $cnf, $imp, $qty, $tch, $act, $rnm, $ptl) = split(/:%:/);
    &INIT if ($pno != $apno);
    write if ($verbose);
    
    $cact[++$nrv] = $act;
    $ccnf += $cnf;
    $cimp += $imp;
    $cqty += $qty;
    $ctch += $tch;
    $cact += $act;
}
&INIT;

format TOP =
 pno   #   Conf.  Import.  Pres.  Tech.  ACTION   StdD  title
============================================================================================================================
.

format DATA =
@###  @#    @.#     @.#     @.#    @.#    @.#    @#.#   @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$apno,$nrv,$ccnf,  $cimp,  $cqty, $ctch, $cact, $cvar,  $aptl
.

format TOPV =
 pno       Conf.  Import.  Pres.  Tech.  ACTION  refname
===============================================================================
.

format ECHO =
 @##       @.#     @.#     @.#    @.#    @.#    @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$apno,     $cnf,   $imp,  $qty,   $tch,  $act,  $rnm
.

format DATAV =
--------------------------------------------------------------------------------
 @##       @.#     @.#     @.#    @.#    @.#    AVERAGES (variance =@#.##)
$apno,     $ccnf,  $cimp,  $cqty, $ctch, $cact,                     $cvar

.

