#!/star/Perl/bin/perl -w

use warnings;
use CGI;
use CGI::Carp;
use File::Basename;

$user="wsaro";
$userpw="wsaropw";
$server="ramses5";
$database="ukidssdr4plus";
#$server="ahmose";
#$database="wsa";

$release="ukidssdr4plus";

$lasLimits="24 0 -10 60 1 10";
$gpsLimits="24 2 -40 65 1 10";
$gcsLimits="18 2 -40 65 1 10";
$dxsLimits=""; #placeholder treated separatedly
$udsLimits="2.8 1.8 -7.5 -2.5 0.5 2.0";

$survey="LAS";
#@surveyFilters=(["Y","J_1","H","K"],["J","H","K"]);
@LASfilters=("Y","J_1","H","K");
#@LASfilters=("Y","J_1","H","K","J_2");
#@GPSfilters=("J","H","K_1","H2");
@GPSfilters=("J","H","K_1");
@GCSfilters=("Z","Y","J","H","K_1");
@DXSfilters=("J","K");
@UDSfilters=("J","K");
$las="LAS";
$gps="GPS";
$gcs="GCS";
$dxs="DXS";
$uds="UDS";
$gpsExtra="GPS";
@dxsAreas=("(XMM-LSS)","(Lockman)","(ELAIS N1)","(VIMOS 4)");
@dxsC=("X","L","E","V");
@dxsLA=("2.9 1.9 -9.5 0.5 0.5 2.0","11.5 10.5 53 63 0.5 2.0","16.7 15.7 49 59 0.5 2.0","22.8 21.8 -5.0 5. 0.5 2.0");
@cFiles=("lasCoverage.lis","gpsCoverage.lis","gcsCoverage.lis","dxsCoverage.lis","udsCoverage.lis");
@limits=($lasLimits,$gpsLimits,$gcsLimits,$dxsLimits,$udsLimits);
@surveys=($las,$gps,$gcs,$dxs,$uds);
$noSurveys=@surveys;
$gifSize=" 800 600 ";
print "$noSurveys\n";
#for ($k=0; $k < $noSurveys;$k++) {
for ($k=0; $k < 5;$k++) {
    if ($surveys[$k] eq $las) {
	@filters=@LASfilters;
    }
    if ($surveys[$k] eq $gps) {
	@filters=@GPSfilters;
    }

    if ($surveys[$k] eq $gcs) {
	@filters=@GCSfilters;
    }
    if ($surveys[$k] eq $dxs) {
	@filters=@DXSfilters;
         $gifSize=" 400 400 ";
    }
    if ($surveys[$k] eq $uds) {
	@filters=@UDSfilters;
        $gifSize=" 500 500 ";
    }

$noFilters=@filters;
print "$noFilters\n";

$fullWhere="";
    $fTitle="";
for ($i=0;$i<$noFilters;$i++){
print "$i\n";

$where=" and ".$surveys[$k]."mergelog.deprecated=0 and multiframe.multiframeid=".$surveys[$k]."mergelog.".$filters[$i]."mfid and currentastrometry.extnum=".$filters[$i]."enum ";

unless ($surveys[$k] eq $gps && $filters[$i] eq "H2"){
$fullWhere=$fullWhere." and  ".$surveys[$k]."mergelog.".$filters[$i]."mfid > 0" ;
$fTitle=$fTitle.$filters[$i]." ";
}

$command="java -Djava.net.preferIPv4Stack=true -classpath :./mssqlserver.jar:jtds-1.0.3.jar:msbase.jar:msutil.jar SimpleSQL $user $userpw $database $server \"select minra,maxra,mindec,maxdec,multiframe.multiframeid from ".$surveys[$k]."mergelog,multiframe,currentastrometry where currentastrometry.multiframeid=multiframe.multiframeid ".$where." \" > res.lis ";

print "$command \n";
 system ($command);


if ($surveys[$k] ne $dxs){
$gifName=$surveys[$k]."_plot_".$filters[$i].".gif";
$title=$surveys[$k]." survey - ".$filters[$i]." filter"; 
$command="./survey_plot2.e $cFiles[$k] $limits[$k] \"". $title."\" res.lis $gifSize ";
print "$command \n";
 system ($command);
rename ("pgplot.gif",$release."_".$gifName);
rename ("res.lis",$gifName.".res");
} else {
    for ($m=0;$m<4;$m++){
$gifName=$surveys[$k].$dxsC[$m]."_plot_".$filters[$i].".gif";
$title=$surveys[$k]." survey ".$dxsAreas[$m]." - ".$filters[$i]." filter"; 
$command="./survey_plot2.e $cFiles[$k] $dxsLA[$m] \"". $title."\" res.lis $gifSize ";
 system ($command);
rename ("pgplot.gif",$release."_".$gifName);
#rename ("res.lis",$gifName.".res");
    }
rename ("res.lis",$gifName.".res");
}


}


$command="java -Djava.net.preferIPv4Stack=true -classpath :./mssqlserver.jar:jtds-1.0.3.jar:msbase.jar:msutil.jar SimpleSQL $user $userpw $database $server \"select minra,maxra,mindec,maxdec,multiframe.multiframeid from ".$surveys[$k]."mergelog,multiframe,currentastrometry where currentastrometry.multiframeid=multiframe.multiframeid ".$where.$fullWhere." \" > res.lis ";

    print "$command\n";
 system ($command);
if ($surveys[$k] ne $dxs){
$gifName=$surveys[$k]."_plot_all.gif";
$title=$surveys[$k]." survey - $fTitle filters"; 
$command="./survey_plot2.e $cFiles[$k] $limits[$k] \"". $title."\" res.lis $gifSize ";
 system ($command);
rename ("pgplot.gif",$release."_".$gifName);
rename ("res.lis",$gifName.".res");
} else {
    for ($m=0;$m<4;$m++){
$gifName=$surveys[$k].$dxsC[$m]."_plot_all.gif";
$title=$surveys[$k]." survey ".$dxsAreas[$m]." - @filters filter"; 
$command="./survey_plot2.e $cFiles[$k] $dxsLA[$m] \"". $title."\" res.lis $gifSize ";
 system ($command);
rename ("pgplot.gif",$release."_".$gifName);
#rename ("res.lis",$gifName.".res");
}
rename ("res.lis",$gifName.".res");
}

print "$where\n";
print "$fullWhere\n";

}
