#!/usr/bin/perl


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

if ($#ARGV<3) {
    print "USE: create_web.pl DATE(YYYYMMDD) DOFIGURES SETUP PIPEVERSION\n";
    print "SETUP VALUES\n";
    print "0 = V300\n";
    print "1 = V600\n";
    print "2 = V500\n";
    print "3 = V1200\n";
    exit;
}
$TODAY=$ARGV[0];
$TOMORROW=$TODAY+1;
$DO=$ARGV[1];
$SETUP=$ARGV[2];
$PVERSION=$ARGV[3];

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

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

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

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


$call="get_res.pl ".$a_setup[$SETUP];
system($call);




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



if ($SETUP==3) {
    open(VSTAR," ls vstar_*.sobj.fits |");
    while($vstar=<VSTAR>) {
	chop($vstar);
	if ($vstar =~ $TODAY) {
	    $pre=$vstar;
	    $cut="sobj.fits";
	    $pre =~ s/$cut//;
	    $PS=$pre."ps";
	    $call="spec2D_plot_max.pl ".$vstar." ".$PS."/CPS";
	    system($call);
#	$call="cp ".$vstar." VSTAR_".$TODAY."_".$pre."sobj.fits";
#	system($call);
#	$call="mv VSTAR_".$TODAY."_".$pre."sobj.fits ../images/";
#	system($call);
	}
    }
    close(VSTAR);
}

#
# Transforming and copying all the PS files
#
$call="table_plot.pl auto_redu_V500.sky_mag 13 1 'Julian DATE' 'Night-sky V-band (mag/arcsec2d)' sky_mag_date.ps/CPS";
system($call);
$call="table_plot.pl auto_redu_V500.sky_mag 12 1 'Airmass' 'Night-sky V-band (mag/arcsec2d)' sky_mag_airmass.ps/CPS";
system($call);


    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 ($TODAY)</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 "($TODAY)<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=\"\#qc\">Symmary of the Quality Control.</a></li><p>\n";
print WEB "<li><a href=\"\#flat\">Variations in the FiberFlat.</a></li><p>\n";
print WEB "<li><a href=\"\#fwhm\">Variations of the FWHM of the Cross-dispersion projection.</a></li><p>\n";
print WEB "<li><a href=\"\#wave\">Accuracy of the Wavelength Calibration.</a></li><p>\n";
print WEB "<li><a href=\"\#wave_range\">Stability of the Wavelength Calibration along the wavelength range.</a></li><p>\n";
print WEB "<li><a href=\"\#res\">Empirical Spectral Resolution.</a></li><p>\n";
print WEB "<li><a href=\"\#sub\">Accuracy of the Sky-Subtraction.</a></li><p>\n";
print WEB "<li><a href=\"\#sky\">Properties of the Night-Sky Spectrum.</a></li><p>\n";
print WEB "<li><a href=\"\#flux\">Stability of the flux calibration along the night.</a></li><p>\n";
print WEB "<li><a href=\"\#flex\">Flexure Pattern along the night.</a></li><p>\n";
print WEB "<li><a href=\"\#DAR_cor\">Differential Atmospheric Refraction Calibration</a></li><p>\n";
print WEB "<li><a href=\"\#flux_recal\">Flux Recalibration.</a></li><p>\n";
print WEB "<li><a href=\"\#depth\">Estimation of the Depth of the datacubes.</a></li><p>\n";
if ($SETUP==3) {
print WEB "<li><a href=\"\#vstar\">Velocity Stars Standards.</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=5 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. $a_band[$SETUP]-band image</th><th>20\" Aperture Spectrum</th></tr>\n";
$n=0;
open(FH,"<auto_redu.out");
while ($line=<FH>) {
    chop($line);
    @s=split(/\./,$line);
    if ($s[1] eq "cube") {
	$cube[$n]=$line;
	$rss[$n]="mos_".$line;
	$rss[$n] =~ s/cube/rss/;
	$pt[$n]=$rss[$n];
	$pt[$n] =~ s/rss/pt/;
	$pt[$n] =~ s/fits/txt/;
	$spec[$n]=$line;
	$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]=$line;
	$cut=".cube.fits";
	$OBJ[$n] =~ s/$cut//;

	$Vfits=$OBJ[$n].".".$a_band[$SETUP].".fits";
	$Vps[$n]=$OBJ[$n].".".$a_band[$SETUP].".ps";
	$Vjpg[$n]=$OBJ[$n].".".$a_band[$SETUP].".jpg";
#	$call="plot_maps.pl ".$Vfits." -0.1 1.1 0.6 0.5 \"".$OBJ[$n]."\" 1 web/".$Vps[$n]."/CPS";
	$call="plot_maps_cont.pl ".$Vfits." 0 0.5 0.51 0.51 '".$OBJ[$n]."' 1 web/".$Vps[$n]."/CPS 59 ".$Vfits." 20 0.025 0.025 68 idl5 1 0";
	system($call);
	$call="convert -rotate 90 web/".$Vps[$n]." web/".$Vjpg[$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";
	}
	system($call);
	$call="cp ".$specPS[$n]." web/";
	system($call);
	$call="convert -rotate 90 web/".$specPS[$n]." web/".$specJPG[$n];
	system($call);

	if ($SETUP==2) {
	    $call="cp ".$OBJ[$n].".comp_SDSS.ps web/";
	    system($call);
	    $call="convert -rotate 90 web/".$OBJ[$n].".comp_SDSS.ps web/".$OBJ[$n].".comp_SDSS.jpg";
	    system($call);
	} else {
	    $call="cp spec_comp_V500_V1200.".$OBJ[$n].".ps web/";
	    system($call);
	    $call="convert -rotate 90 web/spec_comp_V500_V1200.".$OBJ[$n].".ps web/spec_comp_V500_V1200.".$OBJ[$n].".jpg";
	    system($call);
	}
	$call="cp check_w_range.".$OBJ[$n]."_p1.arc.ps web/";
	system($call);
	$call="convert -rotate 90 web/check_w_range.".$OBJ[$n]."_p1.arc.ps web/check_w_range.".$OBJ[$n]."_p1.arc.jpg";
	system($call);

	$call="cp DAR_".$OBJ[$n].".cube.ps web/";
	system($call);
	$call="convert -rotate 90 web/DAR_".$OBJ[$n].".cube.ps web/DAR_".$OBJ[$n].".cube.jpg";
	system($call);
	
	$rscube[$n]=$OBJ[$n].".rscube.fits";
	
	
	print WEB "<tr>\n";
	print WEB "<td><a href=\"ftp://mycalifa@ftp.caha.es/reduced/$a_setup[$SETUP]/reduced_v$PVERSION/$cube[$n]\">$rscube[$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=\"ftp://mycalifa@ftp.caha.es/reduced/$a_setup[$SETUP]/reduced_v$PVERSION/$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><a href=\"$OBJ[$n].comp_SDSS.ps\"><img width=50 height=50 border=0 src=\"$OBJ[$n].comp_SDSS.jpg\"></a></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";

print WEB "<tr><td align=\"center\">\n";
#
# STABILITY OF THE FIBERFLAT
#
print WEB "<table border=1><tr><th align=center colspan=6 bgcolor=#FF8800><a name=\"flat\"><span style=\"color:#1100FF\">FIBERFLAT</span></th></tr>\n";
print WEB "<tr><th>FITSFILE</th><th>MEAN</th><th>MEDIAN</th><th>STDDEV</th><th>MIN</th><th>MAX</th>\n";


$call="cp fiberflat.".$TODAY.".fits web/";
system($call);
print WEB "<tr><td><a href=\"fiberflat.$TODAY.fits\">fiberflat.$TODAY.fits</td>\n";
open(FH,"<imstats.fiberflat");
while ($line=<FH>) {
    chop($line);
    @s=split(" ",$line);
    print WEB "<td>$s[0]</td><td>$s[2]</td><td>$s[1]</td><td>$s[3]</td><td>$s[4]</td>\n";
    print WEB "</tr>\n";
}
close(FH);

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


#
# STABILITY OF INSTRUMENTAL WIDTH
#
print WEB "<tr><td align=\"center\">\n";
print WEB "<table border=1><tr><th align=center colspan=5 bgcolor=#FF8800><a name=\"fwhm\"><span style=\"color:#1100FF\">INSTRUMENTAL PROFILE WIDTH</span></th></tr>\n";
print WEB "<tr><th>HISTOGRAM (ALL)</th><th>&nbsp;</th><th>Num. Profiles Fitted</th><th>MEAN FHWM</th><th>STDDEV FWHM</th></tr>\n";
open(FH,"all.mean.width");
$line=<FH>;
chop($line);
@data=split(" ",$line);
for ($i=1;$i<$#data+1;$i++) {
    $data[$i]=apr($data[$i]);
}
close(FH);
print WEB "<tr valign=top>\n";
print WEB "<td><a href=\"all.mean.width.ps\"><img width=100 height=20 border=0 src=\"all.mean.width.jpg\"></a></td>\n";
print WEB "<td>&nbsp;</td><td>$data[0]</td><td>$data[3]</td><td>$data[4]</td>\n";
print WEB "</tr>\n";
print WEB "<tr><th>INDIVIDUAL PROFILE FIT</th><th>OBJ. NAME</th><th>Num. Profiles Fitted</th><th>MEAN FHWM</th><th>STDDEV FWHM</th></tr>\n";
open(DIR,"ls *.width |");
while ($file=<DIR>) {
#    print "$file\n";
    chop($file);
    if (($file !~ "all")&&($file !~ "obj")&&($file !~ "run")&&($file !~ "junk")&&($file !~ "TEST")) {
	$cut=".mean.width";
	$ocut=".width.ps";
	$psfile=$file;
	$psfile =~ s/$cut/$ocut/;
	$jpgfile=$psfile;
	$jpgfile =~ s/ps/jpg/;
	$name=$file;
	$name =~ s/$cut//;
	$name_s=$name;
	$cut="_p1";
	$name_s =~ s/$cut//;
	$cut="_p2";
	$name_s =~ s/$cut//;
	$cut="_p3";
	$name_s =~ s/$cut//;

	if ($name_s ne "") {
	    open(FH,"<$file");
	    $line=<FH>;
	    chop($line);
	    @data=split(" ",$line);
	    close(FH);
	    for ($i=1;$i<$#data+1;$i++) {
		$data[$i]=apr($data[$i]);
	    }
	    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[0]</td><td>$data[3]</td><td>$data[4]</td>\n";
	    print WEB "</tr>\n";
	    
	    $nameP=$name;
	    $cut="_p1";
	    $nameP =~ s/$cut//;
	    $cut="_p2";
	    $nameP =~ s/$cut//;
	    $cut="_p3";
	    $nameP =~ s/$cut//;
	    $stddev_fwhm{$nameP}=$stddev_fwhm{$nameP}+$data[4];
	    $mean_fwhm{$nameP}=$mean_fwhm{$nameP}+$data[3];
	    $N_fwhm{$nameP}=$N_fwhm{$nameP}+1;
	}
    }
}
close(DIR);

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



#
# STABILITY OF WAVELENGTH CALIBRATION
#
print WEB "<tr><td align=\"center\">\n";
print WEB "<table border=1><tr><th align=center colspan=2 bgcolor=#FF8800><a name=\"wave\"><span style=\"color:#1100FF\">ACCURACY OF THE WAVELENGTH CALIBRATION</span></th></tr>\n";
print WEB "<tr><th align=center colspan=2>CRVAL=$crval AA, CDELT=$cdelt AA/pix, RES_FWHM=$fwhm</th></tr>\n";
print WEB "<tr><th>HISTOGRAM </th><th>LINES</th></tr>\n";
open(DIR,"ls check_w.*.out |");
while ($file=<DIR>) {
    if ($file !~ "arc") {
	chop($file);
	($junk,$name_long,$end)=split(/\./,$file);
	($name,$end)=split(/\_/,$name_long);
	print "$name_long | $name\n";
	if (($file !~ "all")&&($file !~ "obj")&&($file !~ "run")&&($file !~ "junk")&&($name ne "")) {
	    $psfile=$file;
	    $psfile =~ s/out/ps/;
	    $jpgfile=$psfile;
	    $jpgfile =~ s/ps/jpg/;
	    ($junk1,$NAME,$junk2)=split(/\./,$file);
	    print WEB "<tr><td><a href=\"$psfile\"><img width=200 border=0 src=\"$jpgfile\"></a></td>\n";
	    print WEB "<td>\n";
	    print WEB "<table border=1>\n";
	    print WEB "<tr><th colspan=4>$NAME</th></tr>\n";
	    print WEB "<tr><td>SKY LINE</td><td>Nominal Wavelength</td><td>Mean Measured Wavelength</td><td>Std.Dev.</td></tr>\n";
	    open(FH,"<$file");
	    $K=0;
	    while($line=<FH>) {
		chop($line);
		# if (($line !~ "#")&&($line !~ "LINES")) {
		if (($line !~ "#")&&($line !~ "3650")) {
		@data=split(" ",$line);
		for ($i=2;$i<$#data+1;$i++) {
		    $data[$i]=apr($data[$i]);
		}
		$DAT[$K]=$data[4];
		$K++;


		if ($data[4]>0.5) {
		    $color="red";
		} else {
		    $color="black";
		}
		if ($line !~ "LINES") {
		    print WEB "<tr valign=top>\n";
		    print WEB "<td>$data[0]</td><td>$data[1]</td><td>$data[2]</td><td><span style=\"color:$color\">$data[4]</span></td>\n";
		    print WEB "</tr>\n";
		    $nameP=$NAME;
		    $cut="_p1";
		    $nameP =~ s/$cut//;
		    $cut="_p2";
		    $nameP =~ s/$cut//;
		    $cut="_p3";
		    $nameP =~ s/$cut//;
		    $stddev_wave{$nameP}=$stddev_wave{$nameP}+$data[4];
		    $N_wave{$nameP}=$N_wave{$nameP}+1;
		    
		} else {
		    $med=median(@DAT);
		    $stddev_wave{$nameP}=$med;
		    if ($med>0.3) {
			$color="red";
		    } else {
			$color="black";
		    }

		    print WEB "<tr valign=top>\n";
#		    print WEB "<td>ALL LINES = $data[1]</td><td>&nbsp; </td><td>&nbsp; </td><td><span style=\"color:$color\">$data[4]</span></td>\n";
		    print WEB "<td>ALL LINES = $data[1]</td><td>&nbsp; </td><td>&nbsp; </td><td><span style=\"color:$color\">$med</span></td>\n";
		    print WEB "</tr>\n";
		}
		}
	    }
	    close(FH);
	    print WEB "</table></td></tr>\n";
	}
    }
}
close(DIR);

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

#
# STABILITY OF WAVELENGTH CALIBRATION ALONG THE WAVELENGTH
#
print WEB "<tr><td align=\"center\">\n";
print WEB "<table border=1><tr><th align=center colspan=2 bgcolor=#FF8800><a name=\"wave_range\"><span style=\"color:#1100FF\">STABILITY OF THE WAVELENGTH CALIBRATION WITHIN THE WAVELENGTH RANGE</span></th></tr>\n";
print WEB "<tr><th align=center colspan=2>CRVAL=$crval AA, CDELT=$cdelt AA/pix, RES_FWHM=$fwhm</th></tr>\n";
print WEB "<tr><th>ARC_OBJECT </th><th>PLOT</th></tr>\n";
open(DIR,"ls check_w_range.*_p1.arc.ps |");

while ($file=<DIR>) {

    $name_now=$file;
    $cut="check_w_range.";
    $name_now =~ s/$cut//;
    $cut="_p1.arc.ps";
    $name_now =~ s/$cut//;
    $file_jpg=$file;
    $cut=".ps";
    $new=".jpg";
    $file_jpg =~ s/$cut/$new/;
    print WEB "<tr><td>$name_now</td><td><a href=\"$file\"><img width=200 border=0 src=\"$file_jpg\"></a></td></tr>\n";

}
close(DIR);

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


#
# SPECTRAL RESOLUTION
#
open(RES,"<med_get_res.out");
$line=<RES>;
chop($line);
($mean,$sigma,$med,$min,$max)=split(" ",$line);
$fwhm_med=apr(2.345*$med);
$fwhm_sigma=apr(2.345*$sigma);
$med=apr($med);
$sigma=apr($sigma);

close(RES);
print WEB "<tr><td align=\"center\">\n";
print WEB "<table border=1><tr><th align=center colspan=2 bgcolor=#FF8800><a name=\"res\"><span style=\"color:#1100FF\">Spectral Resolution</span></th></tr>\n";
print WEB "<tr><th align=center colspan=2>CRVAL=$crval AA, CDELT=$cdelt AA/pix, RES_FWHM=$fwhm</th></tr>\n";
print WEB "<tr><th>HISTOGRAM </th><th>Sigma (FWHM) of the sky-lines</th></tr>\n";
print WEB "<tr><td><a href=\"get_res.ps\"><img width=200 border=0 src=\"get_res.jpg\"></a></td>\n";
print WEB "<td> $med +- $sigma ( $fwhm_med +- $fwhm_sigma )\n";
print WEB "</td></tr>\n";
print WEB "</table>\n";
print WEB "<hr>\n";
print WEB "</td>\n";
print WEB "</tr>\n";


#
#
#




#
# ACURACY OF THE SKY SUBTRACTION
#
print WEB "<tr><td align=\"center\">\n";
print WEB "<table border=1><tr><th align=center colspan=6 bgcolor=#FF8800><a name=\"sub\"><span style=\"color:#1100FF\">ACCURACY OF THE SKY SUBTRACTION</span></th></tr>\n";
print WEB "<tr><th align=center colspan=6>Measured flux of the $a_line[$SETUP] AA sky line</th></tr>\n";
print WEB "<tr><th colspan=2>HISTOGRAMS (ORIGINAL/SKY SUBTRACTED)</th><th>OBJ. NAME</th><th>ORIGINAL FLUX</th><th>SKY SUB. FLUX</th><th>STD DEV</th></tr>\n";
open(DIR,"ls *.obj.sky |");
while ($file=<DIR>) {
#    print "$file\n";
    chop($file);
    if (($file !~ "all")&&($file !~ "run")&&($file !~ "junk")) {
	$psfile=$file;
	$psfile =~ s/sky/ps/;
	$jpgfile=$psfile;
	$jpgfile =~ s/ps/jpg/;

	$sfile=$file;
	$sfile =~ s/obj/sobj/;
	$pssfile=$sfile;
	$pssfile =~ s/sky/ps/;
	$jpgsfile=$pssfile;
	$jpgsfile =~ s/ps/jpg/;
	$cut="_".$a_line[$SETUP].".obj.sky";
	$name=$file;
	$name =~ s/$cut//;



	open(FH,"<$file");
	$line=<FH>;
	chop($line);
	@data=split(" ",$line);
	close(FH);
	for ($i=0;$i<$#data+1;$i++) {
	    $data[$i]=apr($data[$i]);
	}

	open(FH,"<$sfile");
	$line=<FH>;
	chop($line);
	@sdata=split(" ",$line);
	close(FH);
	for ($i=0;$i<$#sdata+1;$i++) {
	    $sdata[$i]=apr($sdata[$i]);
	}
	print WEB "<tr>\n";
	print WEB "<td><a href=\"$psfile\"><img width=100 height=20 border=0 src=\"$jpgfile\"></a></td>\n";
	print WEB "<td><a href=\"$pssfile\"><img width=100 height=20 border=0 src=\"$jpgsfile\"></a></td>\n";
	print WEB "<td>$name</td><td align=right>$data[0]</td><td align=right>$sdata[0]</td><td align=right>$data[1]</td>\n";
	print WEB "</tr>\n";


	$nameP=$name;
	$cut="_p1";
	$nameP =~ s/$cut//;
	$cut="_p2";
	$nameP =~ s/$cut//;
	$cut="_p3" ;
	$nameP =~ s/$cut//;
	if ($data[1]<2) {
	    $stddev_5577{$nameP}=$stddev_5577{$nameP}+$data[1];
	    $N_5577{$nameP}=$N_5577{$nameP}+1;
	}
    }
}
close(DIR);

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


#
# SKY BRIGHTNESS
#
print WEB "<tr><td align=\"center\">\n";
print WEB "<table border=1><tr><th align=center colspan=4 bgcolor=#FF8800><a name=\"sky\"><span style=\"color:#1100FF\">NIGHT SKY SPECTRUM AND BRIGHTNESS</span></th></tr>\n";
print WEB "<tr><th>SPECTRA</th><th>OBJECT NAME</th><th>DATE/UT HOUR</th><th>V-band Magnitude</th>\n";
$FILE="auto_redu_".$a_setup[$SETUP].".sky_mag";
open(FH,"<$FILE");
while ($line=<FH>) {
#    print "$file\n";
    chop($line);
    
    if ($line !~ "#") {
	@data=split(" ",$line);
	$file=$data[0];
	$psfile=$file;
	$psfile =~ s/txt/ps/;
	$jpgfile=$psfile;
	$jpgfile =~ s/ps/jpg/;
	$mag=apr($data[1]);
	$date=$data[5];
	$name=$file;
	$cut="spec_";
	$name =~ s/$cut//;
	$cut=".msky.txt";
	$name =~ s/$cut//;
	$name_s=$name;
	$cut="_p1";
	$name_s =~ s/$cut//;
	$cut="_p2";
	$name_s =~ s/$cut//;
	$cut="_p3";
	$name_s =~ s/$cut//;

	if ($name_s ne "") {
	print WEB "<tr>\n";
	print WEB "<td><a href=\"$psfile\"><img width=250 height=50 border=0 src=\"$jpgfile\"></a></td>\n";
	print WEB "<td>$name</td><td align=right>$date</td><td align=right>$mag</td>\n";
	print WEB "</tr>\n";
	$nameP=$name;
	$cut="_p1";
	$nameP =~ s/$cut//;
	$cut="_p2";
	$nameP =~ s/$cut//;
	$cut="_p3";
	$nameP =~ s/$cut//;
	$mag_night{$nameP}=$mag_night{$nameP}+$mag;
	$N_night{$nameP}=$N_night{$nameP}+1;
	}
#	print "NAME $nameP $mag_night{$nameP} $N_night{$nameP}\n";
    }
}
close(FH);


print WEB "<tr>\n";
print WEB "<td><a href=\"skys_plot.ps\"><img width=250 height=50 border=0 src=\"skys_plot.jpg\"></a></td>\n";
print WEB "<td>ALL OBJECTS</td><td align=right>&nbsp;</td><td align=right>&nbsp;</td>\n";
print WEB "</tr>\n";
print WEB "</table>\n";
print WEB "<p>\n";
print WEB "<p>\n";
print WEB "<table border=1><tr><th align=center colspan=2 bgcolor=#FF8800><span style=\"color:#1100FF\">VARIATION OF THE NIGHT-SKY BRIGHTNESS</span></th></tr>\n";
print WEB "<tr><th>Along the Time</th><th>Along the Airmass</th></tr>\n";
print WEB "<tr>\n";
print WEB "<td><a href=\"sky_mag_date.ps\"><img width=350 border=0 src=\"sky_mag_date.jpg\"></a></td>\n";
print WEB "<td><a href=\"sky_mag_airmass.ps\"><img width=350  border=0 src=\"sky_mag_airmass.jpg\"></a></td>\n";
print WEB "</tr>\n";
print WEB "</table>\n";

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


print WEB "<tr><td align=\"center\">\n";
#
# STABILITY OF THE FLUX CALIBRATION
#

open(FH,"<calib_test.out");
$line=<FH>;
chop($line);
close(FH);
@s=split(" ",$line);
$s0=apr($s[0]);
$s2=apr($s[2]);

if ($s0>0) {
print WEB "<table border=1><tr><th align=center colspan=3 bgcolor=#FF8800><a name=\"flux\"><span style=\"color:#1100FF\">STABILITY OF THE FLUX CALIBRATION</span></th></tr>\n";
print WEB "<tr><th align=center colspan=3>Comparison of the 2nd calibration star spectrum with the expected one</th></tr>\n";
print WEB "<tr><th>FITSFILE</th><th>MEAN RATIO</th><th>STDDEV RATIO</th></tr>\n";
print WEB "<tr>\n";
print WEB "<td><a href=\"calib_test.ps\"><img width=350 border=0 src=\"calib_test.jpg\"></a></td>\n";
print WEB "<td align=center valign=top>$s0</td><td  align=center valign=top>$s2</td>\n";
print WEB "</tr>\n";
print WEB "</table>\n";
print WEB "<hr>\n";
print WEB "</td>\n";
print WEB "</tr>\n";

}
#
#
#

if ($SETUP==2) {
print WEB "<tr><td align=\"center\">\n";
#
# DAR CORRECTION
#
print WEB "<table border=1><tr><th align=center colspan=2 bgcolor=#FF8800><a name=\"DAR_cor\"><span style=\"color:#1100FF\">Differential Atmospheric Correction</span></th></tr>\n";
print WEB "<tr><th align=center colspan=2>Corrected in the basis of the centroid determination along the wavelength</th></tr>\n";
print WEB "<tr><th>FITSFILE</th><th>DAR CORRECTION</th>";
for ($i=0;$i<$n;$i++) {
    print WEB "<tr>\n";
    print WEB "<td>$OBJ[$i]</td>\n";
    print WEB "<td><a href=\"DAR_$OBJ[$i].cube.ps\"><img width=100 height=50 border=0 src=\"DAR_$OBJ[$i].cube.jpg\"></a></td>\n";
    print WEB "</tr>\n";
}
print WEB "<tr><td colspan=2>The red(blue) stars shows the X(Y) coordinates of the centroid of the galaxy along the wavelength used to recenter the cube, correcting the DAR </td></tr>\n";
print WEB "</table>\n";
print WEB "<hr>\n";
print WEB "</td>\n";
print WEB "</tr>\n";

#
#
#

}

print WEB "<tr><td align=\"center\">\n";
#
# FLUX RE-CALIBRATION
#
print WEB "<table border=1><tr><th align=center colspan=6 bgcolor=#FF8800><a name=\"flux_recal\"><span style=\"color:#1100FF\">FLUX RE-CALIBRATION</span></th></tr>\n";
print WEB "<tr><th align=center colspan=6>Flux ratio between pointings and with respect to the SDSS photometry</th></tr>\n";
print WEB "<tr><th>FITSFILE</th><th>Ratio P1</th><th>Ratio P2</th><th>Ratio P3</th>\n";
if ($SETUP==2) {
    print WEB "<th>Ratio SDSS (g/r)</th>\n";
} else {
    print WEB "<th>----</th>\n";
}
print WEB "<th>Phot. SDSS vs. 30\" spectrum</th>";
for ($i=0;$i<$n;$i++) {
    if ($SETUP==2) {
	$call="dump_header.pl ".$OBJ[$i].".rscube.fits | grep PIPE_FLUX_RATIO1 > DUMP.txt";
	system($call);
	open(DUMP,"<DUMP.txt");
	$DUMP=<DUMP>;
	chop($DUMP);
	close(DUMP);
	($head,$rat1)=split(/\|/,$DUMP);
	$call="dump_header.pl ".$OBJ[$i].".rscube.fits | grep PIPE_FLUX_RATIO2 > DUMP.txt";
	system($call);
	open(DUMP,"<DUMP.txt");
	$DUMP=<DUMP>;
	chop($DUMP);
	close(DUMP);
	($head,$rat2)=split(/\|/,$DUMP);
	$call="dump_header.pl ".$OBJ[$i].".rscube.fits | grep PIPE_FLUX_RATIO3 > DUMP.txt";
	system($call);
	open(DUMP,"<DUMP.txt");
	$DUMP=<DUMP>;
	chop($DUMP);
	close(DUMP);
	($head,$rat3)=split(/\|/,$DUMP);
	$call="dump_header.pl ".$OBJ[$i].".rscube.fits | grep PIPE_FLUX_RECAL > DUMP.txt";
	system($call);
	open(DUMP,"<DUMP.txt");
	$DUMP=<DUMP>;
	chop($DUMP);
	close(DUMP);
	($head,$flux_recal)=split(/\|/,$DUMP);
	$call="dump_header.pl ".$OBJ[$i].".rscube.fits | grep PIPE_FLUX_RECAL_G > DUMP.txt";
	system($call);
	open(DUMP,"<DUMP.txt");
	$DUMP=<DUMP>;
	chop($DUMP);
	close(DUMP);
	($head,$flux_recal_g)=split(/\|/,$DUMP);
	$call="dump_header.pl ".$OBJ[$i].".rscube.fits | grep PIPE_FLUX_RECAL_R > DUMP.txt";
	system($call);
	open(DUMP,"<DUMP.txt");
	$DUMP=<DUMP>;
	chop($DUMP);
	close(DUMP);
	($head,$flux_recal_r)=split(/\|/,$DUMP);
    } else {
	$call="dump_header.pl ".$OBJ[$i].".V1200.rscube.fits | grep PIPE_FLUX_RATIO1 > DUMP.txt";
	system($call);
	open(DUMP,"<DUMP.txt");
	$DUMP=<DUMP>;
	chop($DUMP);
	close(DUMP);
	($head,$rat1)=split(/\|/,$DUMP);
	$call="dump_header.pl ".$OBJ[$i].".V1200.rscube.fits | grep PIPE_FLUX_RATIO2 > DUMP.txt";
	system($call);
	open(DUMP,"<DUMP.txt");
	$DUMP=<DUMP>;
	chop($DUMP);
	close(DUMP);
	($head,$rat2)=split(/\|/,$DUMP);
	$call="dump_header.pl ".$OBJ[$i].".V1200.rscube.fits | grep PIPE_FLUX_RATIO3 > DUMP.txt";
	system($call);
	open(DUMP,"<DUMP.txt");
	$DUMP=<DUMP>;
	chop($DUMP);
	close(DUMP);
	($head,$rat3)=split(/\|/,$DUMP);
    }
    @rat=($rat1,$rat2,$rat3);
    $m_rat=mean(@rat);
    if ($m_rat!=0) {
	$frat1=$rat1/$m_rat;
	$frat2=$rat2/$m_rat;
	$frat3=$rat3/$m_rat;
    }
    $color="black";
    print WEB "<tr>\n";
    print WEB "<td>$OBJ[$i]</td>\n";
    if (($frat1<0.6)||($frat1>1.4)) {
	$color="red";
    } else {
	$color="black";
    }
    print WEB "<td><span style=\"color:$color\">$frat1</span></td>\n";
    if (($frat2<0.6)||($frat2>1.4)) {
	$color="red";
    } else {
	$color="black";
    }
    print WEB "<td><span style=\"color:$color\">$frat2</span></td>\n";
    if (($frat3<0.6)||($frat3>1.4)) {
	$color="red";
    } else {
	$color="black";
    }
    print WEB "<td><span style=\"color:$color\">$frat3</span></td>\n";



    if ($SETUP==2) {
	print WEB "<td>$flux_recal($flux_recal_g/$flux_recal_r)</td>\n";
	print WEB "<td><a href=\"$OBJ[$i].comp_SDSS.ps\"><img width=100 height=50 border=0 src=\"$OBJ[$i].comp_SDSS.jpg\"></a></td>\n";
    } else {
	print WEB "<td>----</td>\n";
	print WEB "<td><a href=\"spec_comp_V500_V1200.$OBJ[$i].ps\"><img width=100 height=50 border=0 src=\"spec_comp_V500_V1200.$OBJ[$i].jpg\"></a></td>\n";
    }
    print WEB "</tr>\n";
}
    if ($SETUP==2) {
print WEB "<tr><th align=center colspan=6>The blue squares correspond to the 30\" diameter photometry obtained from the u,g,r and i-band SDSS images. The red circles corresponds to the B, V and R, Johnson fluxes derived from the previous ones using the transformations included in the SDSS webpage.</th></tr>\n";
} else {
print WEB "<tr><th align=center colspan=6>Comparison between the flux-recalibrated 20\" diameter spectra ectracted from the V500 datacube (black) and the V1200 one (red). The residual is indicated in green.</th></tr>\n";
}
print WEB "</table>\n";
print WEB "<hr>\n";
print WEB "</td>\n";
print WEB "</tr>\n";

#
#
#

#
# STABILITY OF THE FLEXURES
#
print WEB "<table border=1><tr><th align=center colspan=3 bgcolor=#FF8800><a name=\"flex\"><span style=\"color:#1100FF\">STABILITY OF THE FLEXURE PATTERN</span></th></tr>\n";
print WEB "<tr>\n";
print WEB "<td colspan=\"3\" align=\"center\"><a href=\"spots_$TODAY.ps\"><img width=600 border=0 src=\"spots_$TODAY.jpg\"></a></td>\n";
print WEB "</tr>\n";
print WEB "<tr>\n";
print WEB "<td colspan=\"3\">Relative offsets in the dispersion (squares) and cross-dispersion (circles) axis, with respect to the average value along the night, for the different pointings (colors). The offsets for a certain pointing should be larger than 0.1 pixels to perform a shift of the data.</td>\n";
print WEB "</tr>\n";
$nn=0;
#open(FLEX,"ls obj_*_spots.ps |");
open(FLEX,"ls *_spots.ps |");
print WEB "<tr>\n";
while($flex=<FLEX>) {
    chop($flex);
    $flexjpg=$flex;
    $flexjpg =~ s/ps/jpg/;
    print WEB "<td ><a href=\"$flex\"><img width=200 border=0 src=\"$flexjpg\"></a></td>\n";
    $nn++;
    if ($nn>2) {
	$nn=0;
	print WEB "</tr><tr>\n";
    }
}


close(FLEX);

print WEB "</table>\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);
	$psfile=$file;
	$psfile =~ s/out/ps/;
	$jpgfile=$psfile;
	$jpgfile =~ s/ps/jpg/;
	$name=$file;
	$cut=".out";
	$name =~ s/$cut//;
	$cut="SN_";
	$name =~ s/$cut//;
    if (($file !~ "all")&&($file !~ "obj")&&($file !~ "run")&&($file !~ "junk")&&($file !~ "1P")&&($file !~ "2P")&&($name ne "")) {

	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[5];
    }
}
close(DIR);

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

#
# DATA PRODUCTS
#
print WEB "<table border=1><tr><th align=center colspan=6 bgcolor=#FF8800 width=1024><a name=\"qc\"><span style=\"color:#1100FF\">SUMMARY</span></th></tr>\n";
print WEB "<tr><th>CUBE</th><th>MAG.LIMIT</th><th>MAG. Night-sky</th>\n";
print WEB "<th>STDDEV sky sub. $a_line[$SETUP]</th>\n";
print WEB "<th>Inst. Cross-disp Profile FWHM</th>\n";
print WEB "<th>Accuracy of the Wavelength Calibration</th>\n";
print WEB "</tr>\n";
for ($i=0;$i<$n;$i++) {
    print WEB "<tr>\n";
    print WEB "<td><a href=\"ftp://mycalifa@ftp.caha.es/reduced/$a_setup[$SETUP]/reduced$PVERSION/$cube[$i]\">$cube[$i]</a></td>\n";
    print WEB "<td>$mag_lim{$OBJ[$i]}</td>\n";
    if ($N_night{$OBJ[$i]}>0) {
	$mag=apr_n($mag_night{$OBJ[$i]}/$N_night{$OBJ[$i]},3);
    }
    print WEB "<td>$mag</td>\n";
    if ($N_5577{$OBJ[$i]}>0) {
	$mag=apr_n($stddev_5577{$OBJ[$i]}/$N_5577{$OBJ[$i]},3);
    }
    print WEB "<td>$mag</td>\n";
    if ($N_fwhm{$OBJ[$i]}>0) {
	$mag=apr_n($mean_fwhm{$OBJ[$i]}/$N_fwhm{$OBJ[$i]},3);
	$mag2=apr_n($stddev_fwhm{$OBJ[$i]}/$N_fwhm{$OBJ[$i]},3);
    }
    print WEB "<td>$mag +- $mag2 (pixels)</td>\n";
    if ($N_wave{$OBJ[$i]}>0) {
	$mag=apr_n($stddev_wave{$OBJ[$i]}/$N_wave{$OBJ[$i]},3);
    }
    print WEB "<td>$mag (AA)</td>\n";
    print WEB "</tr>\n";

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



if ($SETUP==3) {
    print WEB "<table border=1><tr><th align=center colspan=6 bgcolor=#FF8800 width=1024><a name=\"qc\"><span style=\"color:#1100FF\">Velocity Calibration Stars</span></th></tr>\n";
    print WEB "<tr><th>NAME</th><th>Spectrum</th><th>RSS file (331 spectra)</th></tr>\n";
    open(VSTAR,"ls vstar_*.sobj.fits |");
    while($vstar=<VSTAR>) {
	chop($vstar);
	if ($vstar =~ $TODAY) {
	    $pre=$vstar;
	    $cut="sobj.fits";
	    $pre =~ s/$cut//;
	    $PS=$pre."ps";
	    $JPG=$pre."jpg";
	    $call="cp ".$vstar." web/";
	    system($call);
	    
	    print WEB "<tr valign=top>\n";
	    print WEB "<td>$pre</td>\n";
	    print WEB "<td><a href=\"$PS\"><img width=100 height=20 border=0 src=\"$JPG\"></a></td>\n";	
	    print WEB "<td><a href=\"ftp://mycalifa@ftp.caha.es/reduced/$a_setup[$SETUP]/reduced$PVERSION/$vstar\">$vstar</a></td>\n";	
	    print WEB "</tr>\n";
	}   
    }
    close(VSTAR);

}




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



exit;








