#!/usr/bin/perl


$F3D=$ENV{FIT3D_PATH}; require("$F3D/scripts/my.pl"); #("/home/sanchez/sda1/perl/MY/my.pl");

if ($#ARGV<2) {
    print "USE: create_web_second.pl DOFIGURES SETUP VERSION(1.2)\n";
    print "V300,V600,V500,V1200\n";
    exit;
}

$DO=$ARGV[0];
$SETUP=$ARGV[1];
$PVERSION=$ARGV[2];

@a_setup=("V300","V600","V500","V1200");
@a_crval=(3620,3845,3745,3400);
@a_cdelt=(3.2,1.5,2.0,0.7);
@a_fwhm=(10,6,0,0);
@a_filter=("V","V","V","B");
#@a_setup=("V300","V600","V500","V1200");

#$crval=$a_crval[$SETUP];
#$cdelt=$a_cdelt[$SETUP];
#$fwhm=$a_fwhm[$SETUP];

if ($DO==1) {
system("rm -rf web");

$call="mkdir web";
system($call);



#print "$crval $cdelt $fwhm\n";

#
# Transforming and copying all the PS files
#
    open(DIR,"ls *.ps |");
    while($file=<DIR>) {
	chop($file);
	if ($file !~ "radial") {
	    $outfile=$file;
	    $outfile =~ s/ps/jpg/;
	    $call="convert -rotate 90 ".$file." web/".$outfile;
	    system($call);
	    $call="cp ".$file." web/";
	    system($call);
	}
    }
    close(DIR);
}



open(WEB,">web/index.html");
print WEB "<html><head>\n";
print WEB "<title>Auto Reduction output (SECOND GLUE)</title>\n";
print WEB "<link rel=\"stylesheet\" href=\"/sanchez/legacy/oa/index.css\" >";
print WEB "</head>\n";
print WEB "<body>\n";
print WEB "<table>\n";
print WEB "<tr>\n";
print WEB "<th width=1024 colspan=5 bgcolor=#FF8800 ><h2><span style=\"color:#1100FF\">\n";
print WEB "<p>CALIFA auto-reduction Summary<p><p>\n";
print WEB "(SECOND GLUE)<p><p></h2></span>\n";
print WEB "</th>\n";
print WEB "</tr>\n";
print WEB "<tr><td align=\"center\"><hr></td></tr>\n";
print WEB "<tr><td align=\"center\">\n";

print WEB "<table border=1><tr><th align=center bgcolor=#FF8800 width=800><span style=\"color:#1100FF\">MENU</span></th></tr>\n";
print WEB "<tr><td>\n";
print WEB "<ul>\n";
print WEB "<li><a href=\"\#data\">Data Products.</a></li><p>\n";
print WEB "<li><a href=\"\#depth\">Estimation of the Depth of the datacubes.</a></li><p>\n";
print WEB "</ul>\n";
print WEB "</td></tr>\n";

close(FH);
print WEB "</table>\n";
print WEB "<hr>\n";
print WEB "</td>\n";
print WEB "</tr>\n";

print WEB "<tr><td align=\"center\">\n";
#
# DATA PRODUCTS
#
print WEB "<table border=1><tr><th align=center colspan=7 bgcolor=#FF8800><a name=\"data\"><span style=\"color:#1100FF\">DATA PRODUCTS</span></th></tr>\n";
print WEB "<tr><th>CUBE</th><th>RSS</th><th>Pos. Table</th><th>Reconst. V-band image</th><th>20\" Aperture Spectrum</th><th>SETUP</th><th>V-bad Mag 3sigma lim.</th></tr>\n";
$n=0;
open(FH,"<second_glue.out");
while ($line=<FH>) {
    chop($line);
    @data=split(" ",$line);
    @s=split(/\./,$data[0]);
    if ($s[1] eq "cube") {
	$cdelt[$n]=$data[1];
	for ($i=0;$i<$#a_cdelt;$i++) {
	    if ($cdelt[$n]==$a_cdelt[$i]) {
		$crval[$n]=$a_crval[$i];
		$fwhm[$n]=$a_fwhm[$i];
		$setup[$n]=$a_setup[$i];
	    }
	}
	print "$cdelt[$n] $setup[$n]\n";
	$file=$data[0];


	$cube[$n]=$file;
	$rscube[$n]=$file;
	if ($SETUO==3) {
	    $rscube[$n] =~ s/cube/V1200.rscube/;
	} else {
	    $rscube[$n] =~ s/cube/rscube/;
	}
	$rss[$n]="mos_".$file;
	$rss[$n] =~ s/cube/rss/;
	$pt[$n]=$rss[$n];
	$pt[$n] =~ s/rss/pt/;
	$pt[$n] =~ s/fits/txt/;
	$spec[$n]=$file;
	$spec[$n] =~ s/cube/spec20/;
	$spec[$n] =~ s/fits/txt/;
	$specPS[$n]=$spec[$n];
	$specJPG[$n]=$spec[$n];
	$specPS[$n] =~ s/txt/ps/;
	$specJPG[$n] =~ s/txt/jpg/;

#	$call="cp ".$cube[$n]." web/".$cube[$n];
#	system($call);
#	$call="cp ".$rss[$n]." web/".$rss[$n];
#	system($call);
	$call="cp ".$pt[$n]." web/".$pt[$n];
	system($call);
	$OBJ[$n]=$file;
	$cut=".cube.fits";
	$OBJ[$n] =~ s/$cut//;

	$Vfits=$OBJ[$n].".".$a_filter[$SETUP].".fits";
	$Vps[$n]=$OBJ[$n].".".$a_filter[$SETUP].".ps";
	$Vjpg[$n]=$OBJ[$n].".".$a_filter[$SETUP].".jpg";
	$call="plot_maps.pl ".$Vfits." -0.1 1.1 0.6 0.5 \"".$OBJ[$n]."\" 1 web/".$Vps[$n]."/CPS";
	system($call);
	$call="convert -rotate 90 web/".$Vps[$n]." web/".$Vjpg[$n];
	system($call);



	$call="radial_sum_cube.pl ".$cube[$n]." 10 34 35 JUNK.RSS.fits 0";
	system($call);
	$call="img2spec.pl JUNK.RSS.fits 0 ".$spec[$n];
	system($call);
	if ($SETUP==3) {
#	    $call="spec_plot.pl ".$spec[$n]." ".$specPS[$n]."/CPS -1 500 3700 4800";
	    $call="cp spec_comp_V500_V1200.".$OBJ[$n].".ps ".$specPS[$n];
	    system($call);
	} else {
#	    $call="radial_sum_cube.pl ".$rscube[$n]." 10 34 35 JUNK.RSS.fits 0";
#	    system($call);
#	    $call="img2spec.pl JUNK.RSS.fits 0 ".$spec[$n];
#	    system($call);
	    $spec[$n]=$OBJ[$n].".spec_20.txt";
	    $call="spec_plot.pl ".$spec[$n]." ".$specPS[$n]."/CPS -1 300";
	}


#	$call="spec_plot.pl ".$spec[$n]." ".$specPS[$n]."/CPS -1 700";
	system($call);
	$call="cp ".$specPS[$n]." web/";
	system($call);
	$call="convert -rotate 90 web/".$specPS[$n]." web/".$specJPG[$n];
	system($call);

	$name[$n]=$s[0];
	$file_SN="SN_".$name[$n].".out";

	open(SN,"<$file_SN");
	while($FILE=<SN>) {
	    chop($FILE);
	    if (($FILE !~ "#")&&($FILE !~ "")) {
		@data=split(" ",$FILE);
		for ($i=0;$i<$#data+1;$i++) {
		    $data[$i]=apr_n($data[$i],4);
		}
	    }
	}
	$flux_lim[$n]=$data[3];
	$mag_lim[$n]=$data[8];
	close(SN);

	print WEB "<tr>\n";
	print WEB "<td><a href=\"ftp://mycalifa@ftp.caha.es/reduced/$a_setup[$SETUP]/reduced_v$PVERSION/$cube[$n]\">$cube[$n]</a></td>\n";
	print WEB "<td><a href=\"ftp://mycalifa@ftp.caha.es/reduced/$a_setup[$SETUP]/reduced_v$PVERSION/$rss[$n]\">$rss[$n]</a></td>\n";
	print WEB "<td><a href=\"$pt[$n]\">$pt[$n]</a></td>\n";
	print WEB "<td><a href=\"$Vps[$n]\"><img width=50 height=50 border=0 src=\"$Vjpg[$n]\"></a></td>\n";
	print WEB "<td><a href=\"$specPS[$n]\"><img width=50 height=50 border=0 src=\"$specJPG[$n]\"></a></td>\n";
	print WEB "<td>$setup[$n]</td>\n";
	print WEB "<td>$mag_lim[$n]</td>\n";
	print WEB "</tr>\n";


	    $n++;
    }
}
close(FH);
print WEB "</table>\n";
print WEB "<hr>\n";
print WEB "</td>\n";
print WEB "</tr>\n";
print WEB "<tr><th align=\"center\" bgcolor=#FF8800 ><span style=\"color:#1100FF\">\n";
print WEB "<p>AUTOMATIC QUALITY CONTROLS<p></span>\n";
print WEB "</th>\n";
print WEB "</tr>\n";
print WEB "<tr><td align=\"center\">\n";
print WEB "<hr>\n";
print WEB "</td>\n";
print WEB "</tr>\n";

#
# FINAL SN
#
print WEB "<tr><td align=\"center\">\n";
print WEB "<table border=1><tr><th align=center colspan=5 bgcolor=#FF8800><a name=\"depth\"><span style=\"color:#1100FF\">DEPTH ANALYSIS: S/N vs. FLUX INTENSITY (CUBES)</span></th></tr>\n";
print WEB "<tr><th align=center colspan=5>Flux units: 1e-16 Erg s-1 cm-2 A-1 arcsec-2, MAG= mag/arcsec^2</th></tr>\n";

print WEB "<tr><th>FIGURE</th><th>OBJ. NAME</th><th>3sigma Flux limit</th><th>Std. Dev.</th><th>3sgima V-band mag limit</th></tr>\n";
open(DIR,"ls -tr SN_*.out |");
while ($file=<DIR>) {
#    print "$file\n";
    chop($file);
    if (($file !~ "all")&&($file !~ "obj")&&($file !~ "run")&&($file !~ "junk")) {
	$psfile=$file;
	$psfile =~ s/out/ps/;
	$jpgfile=$psfile;
	$jpgfile =~ s/ps/jpg/;
	$name=$file;
	$cut=".out";
	$name =~ s/$cut//;
	$cut="SN_";
	$name =~ s/$cut//;
	open(FH,"<$file");
	while($line=<FH>) {
	    chop($line);
	    if (($line !~ "#")&&($line !~ "")) {
		@data=split(" ",$line);
		close(FH);
		for ($i=0;$i<$#data+1;$i++) {
		    $data[$i]=apr_n($data[$i],4);
		}
	    }
	}
	print WEB "<tr valign=top>\n";
	print WEB "<td><a href=\"$psfile\"><img width=100 height=20 border=0 src=\"$jpgfile\"></a></td>\n";
	print WEB "<td>$name</td><td>$data[2]</td><td>$data[3]</td><td>$data[5]</td>\n";
	print WEB "</tr>\n";
	$flux_lim{$name}=$data[3];
	$mag_lim{$name}=$data[8];
    }
}
close(DIR);

print WEB "</table>\n";
print WEB "<hr>\n";
print WEB "</td>\n";
print WEB "</tr>\n";
#
#
#


#print WEB "</td></tr></table></body></html>\n";
print WEB "</body></html>\n";



exit;








