#!/star/Perl/bin/perl
#use warnings;
use CGI;
use CGI::Carp;
require 'cgiSubs.pl';
require 'server.pl';
use Astro::FITS::CFITSIO;
use File::Basename;


print "Content-type: text/html\n\n";
$q = new CGI;
$filename=$q->param("filename");
$mode=$q->param("mode");
$extNum=$q->param("extNum");
$jpegBase=$q->param("jpegBase");
$extNum--;

$filename=~ s/djoser://g;
$base = basename($filename);

$jpegFile=$jpegBase."_".$extNum.".jpg";

my $directory = basename(dirname($filename));
#$filename="/disk32/vsa/ingest/fits/20091108_v0.8/v20091108_00549_st.fit";
#$extNum="1";
#sleep 5;
print "<div class=\"ext\">";#extNum:", $extNum+1,"</div>";
print "Currently displaying:<br><b> $filename</b><br>  extNum: <b>", $extNum+1 ,"</b><br>";
    if (-e $jpegFile) {
print "<img width=300 src=\"http://surveys.roe.ac.uk/wsa/cgi-bin/jpeg.cgi?file=",$jpegFile,"\">\n";
}
if ($mode eq "WFAU") {
print <<END;
<form action="./showHeaders.cgi" method="GET">
<input type="hidden" name="mode" value="$mode">
File:<input type="text" name="filename" size="22">
<input class="FontSans" type="submit" value="Go" />
</form>
END
    ;
}
print "</div>";

if (-e $filename ) {
    $base = basename($filename);


$fitsFileName=$filename."[".$extNum."]";
my $fptr =Astro::FITS::CFITSIO::open_file($fitsFileName,Astro::FITS::CFITSIO::READONLY(),$status);
$|=1;
if ($status==0) {
Astro::FITS::CFITSIO::fits_get_hdrspace($fptr,$numKeys, $moreKeys,$status);

if ($status==0) {
    print "<pre>\n";
for ($i=0; $i<$numKeys; $i++) {
Astro::FITS::CFITSIO::fits_read_record($fptr, $i, $card, $status);
print "$card\n";
}
print "END\n\n";
    print "</pre>\n";
}
else {
    print "Unable to read FITS header\n";
}
}
else {
    print "Unable to open FITS file\n";
}

}
else {
 print "Unable to open FITS file\n";
}
