#!/usr/bin/perl

$dir=".";

chdir $dir;



## construction of list of models, experiments, output file types
@flist = glob ("*/plots");
@modlist = ();
@explist = ();
@outlist = ();
for($i=0; $i<@flist; $i++) {
    my @a = split /[\/]/, $flist[$i];
    $modlist[$i] = $a[0];
    $explist[$i] = $a[1];
    $outlist[$i] = $a[2];
    print $flist[O], $a[0];
    exit;
}
@modlist = grep { ++$count{$_} < 2 } @modlist;
@explist = sort (grep { ++$count{$_} < 2 } @explist );
@outlist = grep { ++$count{$_} < 2 } @outlist;


$year='2001';


###
`rm -f ~/VAR_OVERVIEW/*html`;

@plist=();
%dimlist=();
for($j=0; $j<@explist; $j++) {
for($i=0; $i<@modlist; $i++) {
for($k=0; $k<@outlist; $k++) {
    $file=$modlist[$i].'_'.$explist[$j].'_'.$outlist[$k].'_'.$year.'.nc';
    if ( -e $file ) {
# get new variables
	@b =  `~/bin/ncvarlist $file`;
	@plist=(@plist,@b);

# check for dimensions per variable
#	@c =  `~/bin/ncvarlist2 $file`;
#	for($d=0; $d<@c; $d++) { %dimlist=( %dimlist,($b[$d]=>$c[$d]-2) ) }
#	@kk=keys (%dimlist);
#	for($d=0; $d<@c; $d++) { print $kk[$d],$dimlist{$kk[$d]} }
#	exit;

    }
}
}
# all inklusive variable list from all exps, models and output files
%count =();
@plist = sort (grep { ++$count{$_} < 2 } @plist);
}

##=====================================================
##construction of table

for($j=0; $j<@explist; $j++) {

@table = ([]);
while ($modlist > @table){@table = (@table,([]));}


for($i=0; $i<@modlist; $i++) {
    @al=();
    for($k=0; $k<@outlist; $k++) {
	$file=$modlist[$i].'_'.$explist[$j].'_'.$outlist[$k].'_'.$year.'.nc';
	if ( -e $file ) {
	    @c =  `~/bin/ncvarlist $file`;
	    @al=(@al,@c);
	}
    }
    %count =();
    @all = ( grep { ++$count{$_} < 2 } @al  );
    
    for($p=0; $p<@plist; $p++) {
	$ff =  grep { /^$plist[$p]$/ } @all  ;
	$table[$p][$i] = $ff;
    }
}

##=====================================================
##printing of html table

####################################" printing header
$tfile='/htap/users/htap06/VAR_OVERVIEW/'.$explist[$j].'_'.$year.'models_variables.html';

$outfile = ">".$tfile;
open (OUT , $outfile); print OUT "";
$outfile2 = ">>".$tfile;
open (OUT , $outfile2);

print OUT
"<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN'>
<html>
<head>
<title>OVERVIEW TABLE HTAP COMPARISON </title>
<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>
</head>
<body>
<p>HTAP model vs variable overview <br>";
my ($Sekunden, $Minuten, $Stunden, $Monatstag, $Monat,
    $Jahr, $Wochentag, $Jahrestag, $Sommerzeit) = localtime(time);
print OUT "as of ",$Monatstag,".",$Monat+1,".",$Jahr-100+2000;

print OUT "based on Julich server directory files <br> <strong>Experiment ".$explist[$j]."</strong></p>
Other experiment overviews: <br>";
for($jj=0; $jj<@explist; $jj++) {
    $srfile=$explist[$jj].'_'.$year.'models_variables.html';
    print OUT "<a href='",$srfile,"'>",$explist[$jj],"</a> - ";
}


########################################

# print table structure
print OUT "<br>
    <table width=",102+@modlist*61+102," border='1' cellspacing='0' cellpadding='0' bordercolor='#0099FF'>";

header();

# print table with parameter at start and end 
print OUT "<tr>";
$counter=0;
for($p=0; $p<@plist; $p++) {
    next if ($plist[$p] =~ /\=/ );
    print OUT "<td>",$plist[$p],"</td>";
    for($i=0; $i<@modlist; $i++) {
	if ($table[$p][$i]==1 ) {
              print OUT "<td align='center' bgcolor='#00FF00'>
              <font size='-3' face='Arial, Helvetica, sans-serif'><strong>x</td>";
	  }
	else {print OUT "<td bgcolor='#FFFFCC'>&nbsp;</td>";}
    }
    print OUT "<td>",$plist[$p],"</td>";
    print OUT "</tr>";
    if ($counter == 20) {
	header ();
	$counter=0;
    }
    $counter=$counter+1;
}
#end of table writing
print OUT "</table>";
close OUT;
#end of experiment loop
}

sub header {
# print first line with table header 
print OUT "
    <tr align='center' bgcolor='#99FFFF'>
    <td width='100'><font size='-3' face='Arial, Helvetica, sans-serif'><strong>Parameter</font></td>";

for($ii=0; $ii<@modlist; $ii++) {
   $model = (split /[\-]/, $modlist[$ii])[0];
   $version = (split /[\-]/, $modlist[$ii])[1];
   $version2 = (split /[\-]/, $modlist[$ii])[2];
   $version3 = (split /[\-]/, $modlist[$ii])[3];
   $version4 = (split /[\-]/, $modlist[$ii])[4];
   print OUT "
    <td width='60'><font size='-3' face='Arial, Helvetica, sans-serif'><strong>",
	$model,"<br>",$version,"<br>",$version2,"<br>",$version3,"<br>",$version4,"</font></td>";
}
print OUT "
    <td width='100'><font size='-3' face='Arial, Helvetica, sans-serif'><strong>Parameter</font></td>
    </tr>"
}
