#!/usr/bin/perl

# make GIF images of the FITS files in this directory and the html code to
# display them

$i = 0;
$file = "../calstars_70_567MC.dat";
open (FILE, $file) or die "Cannot open $file: $!";
while (<FILE>) {
   next if /^#/;
   @line = split;
   $idl_aorkey[$i] = $line[1];
   $idl_hdnum[$i] = $line[2];
   $i++;
}
close (FILE);

$file = "images.html";
open (FILE, ">$file") or die "Cannot open $file: $!";

print FILE "<html>\n";
print FILE "<head><title>Calibrator Images</title></head>\n";
print FILE "<body>\n";
print FILE "<h1>70&micro;m Wide-field Calibrator Images</h1>\n";
print FILE "<table border=\"1\" cellpadding=\"5\" cellspacing=\"2\">\n";

$counter = 0;
open (LS, "ls *.fits |");
while (<LS>) {
   @line = split ("_", $_);
   chop ($fitsname = $_);
   $gifname = $fitsname;
   $gifname =~ s/fits/gif/;
   $fitsname .= '[1]';
   $aorkey = $line[2];
   for ($j = 0; $j <= $#idl_aorkey; $j++) {
      if ($aorkey == $idl_aorkey[$j]) {
         $hdnum = $idl_hdnum[$j];
         last;
      }
   }
   system "fitsplot.pl -s 5 -g 3 -f -m heat -c -t $hdnum -o $gifname/gif $fitsname";
   if ($counter == 0) {print FILE "<tr>\n";}
   print FILE "<td align=center>";
   print FILE "<a href=\"./$gifname\">";
   print FILE "<img height=220 width=170 src=\"./$gifname\">";
   print FILE "</a>";
   print FILE "<br>$hdnum";
   print FILE "</td>\n";
   $counter++;
   if ($counter == 6) {
      print FILE "</tr>\n";
      $counter = 0;
   }
}
close (LS);

print FILE "</table>\n";
print FILE "</body>\n";
print FILE "</html>\n";

close (FILE);
