#!/usr/bin/perl

use CGI qw/:standard/;
use MicModule;

$q = new CGI;

$ctms = @::MicModule::ctmlist ;
$gcms = @::MicModule::gcmlist ;
@rlist = (@::MicModule::ctmlist,@::MicModule::gcmlist) ;
@table = ([]);
while ($ctms+$gcms > @table){
    @table = (@table,([]));
}


## switch to activate other web servers with other 
## directories
$machine="dataipsl";

$httpbase='http://dataipsl.ipsl.jussieu.fr/AEROCOM/DATA/';
$dirperl='/cgi-bin/AEROCOM/';
$dirunix = '/bipbip/AEROCOM/';

if ( $machine eq 'boulimix' ) {
    $httpbase='http://boulimix/w3data/schulz/AEROCOM/';
    $dirperl='/cgi-bin/w3data/schulz/';
    $dirunix = '/home/aerocom0/';
}

if ( $ctyp eq 'inca_atlas' ) {$dirperl=$dirperl.'inca_work/';}
elsif ( $ctyp eq 'synthesis' ) {$dirperl=$dirperl.'surfobs/';}
else {($dirperl=$dirperl.$ctyp.'/');}

$httpmodels=$httpbase;
$dirunixbase = $dirunix;
$prfxst = ''; 


@plist0 = ();
@slist0 = ();
@ylist0 = ();
@mlist0 = ();

for($i=0; $i<@rlist; $i++) {
    chdir  $dirunix . $rlist[$i] . "/plots" || next;
    @flist=();
    @flist=`ls`;
    my @aa = ();
    my @bb = ();
    my @cc = ();
    my @dd = ();
    
    for($j=0; $j<@flist; $j++) {
	my @a = split /[\_\.]/, $flist[$j];
# parameters
	$aa[$j] = $a[0]; 
# species
	$bb[$j] = $a[1];
# year(s)
	$cc[$j] = $a[2];
# period
	$dd[$j] = $a[3];
    }
    @plist0 = ( @plist0 , @aa );
    @slist0 = ( @slist0 , @bb );
    @ylist0 = ( @ylist0 , @cc );
    @mlist0 = ( @mlist0 , @dd );
}

my %count =();
@plist = sort (grep { ++$count{$_} < 2 } @plist0);
my %count =();
@slist = sort (grep { ++$count{$_} < 2 } @slist0);
my %count =();
@ylist = grep { ++$count{$_} < 2 } @ylist0;
my %count =();
@mlist = grep { ++$count{$_} < 2 } @mlist0;


# checking for availaibily of image    
# looping over all species / printing one file per species
for($s=0; $s<@slist; $s++) {
#for($s=0; $s<1; $s++) {
    @tablesum=();
    for($i=0; $i<@rlist; $i++) {
	chdir $dirunix . $rlist[$i] . "/plots" || next;
    	for($p=0; $p<@plist; $p++) {
	    $tt = $plist[$p].'_'.$slist[$s].'_';
	    @flist=();
	    @flist=`ls $tt*`;
	    if (@flist>0) {
		$table[$p][$i] = 1;
		$tablesum[$p]=$tablesum[$p]+1;
	    }
	    else {
		$table[$p][$i] = 0;
		$tablesum[$p]=$tablesum[$p]+0;
	    }
	}
    }

######################## printing header
chdir $dirunix . "INTERFACE_DOC";
$outfile = ">".$slist[$s].".html";
open (OUT , $outfile); print OUT "";
$outfile2 = ">>".$slist[$s].".html";
open (OUT , $outfile2);

print OUT "<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN'>
<html>
<head>
<title>OVERVIEW TABLE AEROCOM ",$slist[$s],"</title>
<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>
</head>
<body>
<table  border='0' cellspacing='0' cellpadding='0'>
  <tr>
    <td width='339' height='216' valign='middle' background='../LSCE/plots/EMI_", $slist[$s],
    "_an2000_mALLYEAR.ps.png'><strong><br>
    </strong> </td>
    <td  align ='center'><p><strong><em>Overview of availability of 
    images for<br><font size='5' color='#FF0000'>", $slist[$s],"
    </font><br>
    on AEROCOM WEB INTERFACE</em></strong><br>
        <br>
        see also Overview tables for<br>
        <a href='".$httpbase."INTERFACE_DOC/DUST.html'>Dust</a> 
        - <a href='".$httpbase."INTERFACE_DOC/SO4.html'>Sulfate</a> 
        - <a href='".$httpbase."INTERFACE_DOC/SS.html'>SeaSalt</a> 
        - <a href='".$httpbase."INTERFACE_DOC/BC.html'>BC</a> 
        - <a href='".$httpbase."INTERFACE_DOC/POM.html'>POM</a> 
        - <a href='".$httpbase."INTERFACE_DOC/BCPOM.html'>BCPOM</a> <br>
        <a href='".$httpbase."INTERFACE_DOC/AER.html'>Total Aerosol</a> 
        - <a href='".$httpbase."INTERFACE_DOC/AERN.html'>Aerosol Number</a> 
        - <a href='".$httpbase."INTERFACE_DOC/DMS.html'>DMS</a> 
        - <a href='".$httpbase."INTERFACE_DOC/SO2.html'>SO2</a> 
        - <a href='".$httpbase."INTERFACE_DOC/METEO.html'>Meteo Variables</a> 
      </p>
      <a href='javascript:history.back()'>&lt;- BACK to images</a><br><br>
      Parameter explanation";
print OUT $q->startform('POST','/cgi-bin/AEROCOM/surfobs/surfobs_annualrs.pl');
print OUT $q->popup_menu(-name=>'PLIST',-values=>\@plist,-default=>'CONC1000',-labels=>\%::MicModule::pxlabelhash,-override=>1);
print OUT "
      <br> Note that sometimes an image for a parameter-model combination
      <br> is available only for one of the years 96/97/00/01
      </td>
  </tr>
</table>
<br>";


########################################
# print header of table
print OUT "
     <table width=",102+($ctms+$gcms)*61," border='1' cellspacing='0' cellpadding='0' bordercolor='#0099FF'>
   
    <tr>
    <td>&nbsp;</td>
    <td colspan='",$ctms,"' bgcolor='#0099FF'> 
    <font size='-3' face='Arial, Helvetica, sans-serif'><strong>
    Nudged GCMs or CTMs</font></td>
    <td colspan='",$gcms,"' bgcolor='#33CCFF'>
    <font size='-3' face='Arial, Helvetica, sans-serif'><strong>
    AGCMs</td>
    </tr>

    <tr align='center' bgcolor='#99FFFF'>
    <td width='100'><font size='-3' face='Arial, Helvetica, sans-serif'><strong>Parameter</font></td>";

for($i=0; $i<@rlist; $i++) {
    print OUT "
    <td width='60'><font size='-3' face='Arial, Helvetica, sans-serif'><strong>",$rlist[$i],"</font></td>"
    }		   
    print OUT  "</tr>";
    
# print row information
    for($p=0; $p<@plist; $p++) {
	if ($tablesum[$p] < 1) {next; }
	if ($p==40) {
	    print OUT "
            <tr align='center' bgcolor='#99FFFF'>
            <td>&nbsp;</td>";
	    
	for($i=0; $i<@rlist; $i++) {
	    print OUT "
            <td><font size='-3' face='Arial, Helvetica, sans-serif'><strong>",
	    $rlist[$i],"</td>";
	}	   
	print OUT  "</tr>";
    }

    print OUT "
    <tr><td align='center'>
    <font size='-3' face='Arial, Helvetica, sans-serif'><strong>",
    $plist[$p],"</td>";

# print table cell info 
    for($i=0; $i<@rlist; $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 "</strong></font></tr>";
}
print OUT "</table>";
close OUT;
}


