#!/usr/bin/perl

use CGI qw/:standard/;
use MicModule;
$q = new CGI;

$ctyp= 'surfobs';

if ($ctyp eq 'surfobs'){
    $ctms = @::MicModule::ctmlist ;
    $gcms = @::MicModule::gcmlist ;
    @rlist = (@::MicModule::ctmlist,@::MicModule::gcmlist) ;
}

if ($ctyp eq 'lidar'){
    @rlist = @::MicModule::lidarlist;
    $ctms=@rlist;
    $gcms=0;
}

@table = ([]);
while ( @rlist > @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 = ''; 

    if  ( $ctyp =~ /synthesis/ )  { 
	$prfxst = '_';
    }	
    if  ( $ctyp =~ /surfobs/ )  { 
	$httpmodels = $httpbase . 'SURFOBS/';
	$dirunix = $dirunix . 'SURFOBS/';
	$prfxst = '_';
    }	
    if  ( $ctyp =~ /lidar/ )  { 
	$httpmodels = $httpbase . 'LIDAR/';
	$dirunix = $dirunix . 'LIDAR/';
	$prfxst = '_';
    }	


@slist = ::MicModule::pss($ctyp,'ALL','LSCE',$dirunix,'NA');

my @ylist =();
for($i=0; $i<@rlist; $i++) {
    for($p=0; $p<@slist; $p++) {
	@ylist0=();
	@ylist0 = ::MicModule::psy($ctyp,$slist[$p],$rlist[$i],$dirunix);
	@ylist = (@ylist,@ylist0);
    }
}

@ylist = sort( grep { ++$count{$_} < 2 } @ylist );


# checking for availaibily of image    
# row info parameter+year
# column info models
# for surfobs one file in total
# for lidar one file per station

if ($ctyp eq 'lidar'){
    @stlist = ();
    for($i=0; $i<@rlist; $i++) {
	chdir $dirunix . $rlist[$i] . "/plots" || next;
	@flist=();
	@flist=`ls *ALLYEAR*`;
	for($j=0; $j<@flist; $j++) {
	    my @a = split /[\_\.]/, $flist[$j];
	    $aa[$j] = $a[4];
	    @stlist = (@stlist,@aa);
	}
    }
    my %count =();
    @stlist = sort (grep { ++$count{$_} < 2 } @stlist);
}
if ($ctyp eq 'surfobs'){@stlist='dummy'}

for($st=0; $st<@stlist; $st++) {

@tablesum=();
for($i=0; $i<@rlist; $i++) {
    chdir $dirunix . $rlist[$i] . "/plots" || next;
    for($p=0; $p<@slist; $p++) {
	for($s=0; $s<@ylist; $s++) {
	    $tt = $slist[$p].'_'.$ylist[$s].'_';

	    @flist=();
	    if ($ctyp eq 'surfobs') {@flist=`ls $tt*`;}
	    if ($ctyp eq 'lidar'){@flist=`ls $tt*$stlist[$st]*`;}

	    $cellnum=$p*@ylist+$s;
	    if (@flist>0) {
		$table[$cellnum][$i] = 1;
		$tablesum[$cellnum]=$tablesum[$cellnum]+1;
	    }
	    else {
		$table[$cellnum][$i] = 0;
		$tablesum[$cellnum]=$tablesum[$cellnum]+0;
		if ($ctyp eq 'lidar') {$tablesum[$cellnum]=1}
	    }
	}
    }
}


####################################" printing header
chdir $dirunixbase . "INTERFACE_DOC";

if ($ctyp eq 'surfobs') {
$outfile = ">surfobsov.html";
open (OUT , $outfile); print OUT "";
$outfile2 = ">>surfobsov.html";
open (OUT , $outfile2);
$illuimage=$httpmodels.'LSCE/plots/OD550AER_MAP_an2000_mALLYEAR_WORLD.ps.png';
}

if ($ctyp eq 'lidar') {
$outfile = ">lidar".$stlist[$st].".html";
open (OUT , $outfile); print OUT "";
$outfile2 = ">>lidar".$stlist[$st].".html";
open (OUT , $outfile2);
$illuimage=$httpmodels.'LSCE/plots/EC355_AER_an2000_mALLYEAR_'.$stlist[$st].'.ps.png';
}

print OUT
"<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN'>
<html>
<head>
<title>OVERVIEW TABLE AEROCOM ".$ctyp." COMPARISON </title>
<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>
</head>
<body>
<table width='900' border='0' cellspacing='0' cellpadding='0'>
  <tr>
    <td width='420 height='250' valign='middle' background='".$illuimage."'>&nbsp;<br></td>
    <td width='480' height='250' align ='center' valign='middle' ><p><strong><em>Overview of availability of 
        images for<br><font size='4' color='#FF0000'>
        COMPARISON AEROSOL MODEL TO OBSERVATIONS
        </font>
        <br>
        on AEROCOM ".$ctyp." <a href='../".$ctyp.".html'>WEB INTERFACE</a></em></strong><br>
        (note: images contain model results <br>but not necessarily observations)
        <br>
        <a href='javascript:history.back()'>&lt;- BACK to images</a><br>
      </p>";
if ($ctyp eq 'lidar') {
    print OUT "<br>This is the overview of available images for station <br>
                <font size=+2 color='red'><strong>".$stlist[$st]."</strong></font>
                <br>see also Overview tables for<br>";

    for($sti=0; $sti<@stlist; $sti++) {
	print OUT '<a href="'.$httpbase.'INTERFACE_DOC/lidar'
		    .$stlist[$sti].'.html">'.$stlist[$sti].'</a>, ';
	}

}
print OUT"
      </td>
  </tr>
</table>
<br>";

if ($ctyp eq 'lidar') {
print OUT "<br>Parameter explanation";
print OUT $q->startform('POST','surfobs_annualrs.pl');
print OUT $q->popup_menu(-name=>'SLIST',-values=>\@slist,-default=>'EC355_AER',-labels=>\%::MicModule::psxlabelhash,-override=>1);
}
########################################
# 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 rows
for($p=0; $p<@slist; $p++) {
    for($s=0; $s<@ylist; $s++) {
	$test = $slist[$p].'_'.$ylist[$s];
	$cellnum=$p*@ylist+$s;
	    
	if ($tablesum[$cellnum] < 1) {next; }

### intermediate line with model names again
	if (($cellnum==40) || ($cellnum==80)) {
	    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>";
         }

# start printing row
         print OUT "
        <tr><td align='center'";
	if ($s==0){print OUT "bgcolor='#33BB33'"}
        print OUT ">
        <font size='-3' face='Arial, Helvetica, sans-serif'><strong>",
         $test,"</td>";

# print table cell info f(cellnum)
        for($i=0; $i<@rlist; $i++) {
	   if ($table[$cellnum][$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;

# end loop over stlist
}







