#!/usr/local/bin/perl
#-----------------------------
# mkweb.pl SC 20020225
# Driver script to generate web page from text listing and kntr plots
# Syntax: mkweb.pl B0450+55
# Expects file B0450+55.txt and ...aips/FITS/B0450+55.ICL*.EPS
# txt file format:
# B0450+55.006  04 53 01.56980  55 51 51.6846  flux  RMS  comments about src

$epspath = '/home/space1/aips/FITS/';
$epspath = '/home/cool3/shami/temp/';

$creategifs = "NO";
# $creategifs = "YES";
# Set to YES to convert EPS to gif images.

$name  = shift(@ARGV) || die "Specify pulsar name! Failed";
$inname = $name.".txt";
$outname = $name.".html";

open(INPF, $inname) || die "Input file missing";
system ("cat head.html > $outname");
open(OUTF, ">>$outname") || die "Failed to open output";

#-----------------------------
# Generate HTML header
printf OUTF "<h2> %s at 1.4 GHz </h2>\n", $name;

$htmlheader = <<EOF;
This table is automatically generated. 
See <A HREF=\"http://www.astro.cornell.edu/~shami/psrvlb/vla\">this page</a> 
for an explanation of table entries.<p><hr><p>

<table border=1 cellpadding=2 cellspacing=2 align=center>
<TR align=center valign=middle>
<TH align=center>Source</TH>
<TH align=center>RA (J2000)</TH>
<TH align=center>Dec (J2000)</TH>
<TH align=center>Flux (mJy)</TH>
<TH align=center>RMS (mJy)</TH>
<TH align=center>D(')</TH>
<TH align=center>Comments</TH>
<TH align=center>Image</TH></TR>
<TBODY>
EOF
    
print(OUTF $htmlheader);

#-----------------------------
# Parse text file
while (<INPF>){
    if((/^(\s+)/)||(/^\#(.*)/)){
        # discard comments and blank lines;
    } elsif (/^\S(.*)/)  {
	($src, $rah, $ram, $ras, $dd, $dm, $ds, $flux, $rms, $comment)
	    = split(' ', $_, 10);
	chop($comment);
	($srcname, $index) = split('\.', $src);
	if($srcname ne $name){
	    die "Source name does not match $name";
	}
        $index=substr($index,0,3);
        if(!($index>0 && $index<1000)){
            die "Index $index cannot be right!";
        }
	#-----------------------------
	# Calculate angular separation
	printf("Processing %s...\n",$index);
	if($index==1){
	    $rah0=$rah; $ram0 = $ram; $ras0 = $ras;
	    $dd0=$dd;   $dm0 = $dm;   $ds0 = $ds;
	}
	open(ANGL, "./anglsep $rah $ram $ras $dd $dm $ds $rah0 $ram0 $ras0 $dd0 $dm0 $ds0 |");
	    while(<ANGL>){
		if(/Separation (.*)/){
		    ($sep, $is, $theta, $arcmin) = split(' ',$_);
		}
	    }
	close(<ANGL>);
	# print("Seperation is $theta arcmin  ");

	#-----------------------------
	# Generate GIF image from 5in by 5in EPS file from KNTR
	$epsname = $epspath.$srcname.".ICL".$index.".EPS";
	$gifname = "images/".$src.".gif";
	if($creategifs eq "YES"){
	    system "pstopnm -xsize 360 -ysize 360 -xborder 0 -yborder 0 -portrait -stdout $epsname | ppmtogif > $gifname";
	} else {
	    printf("creategifs set to NO: assuming $gifname is okay\n");
	}

	#-----------------------------
	# Generate the HTML table
	printf OUTF "<tr><td> %s </td>\n", $src;
	printf OUTF "<td> %s %s %s </td>\n", $rah, $ram, $ras;
	printf OUTF "<td> %s %s %s </td>\n", $dd, $dm, $ds;
	printf OUTF "<td align=center> %s </td>\n", $flux;
	printf OUTF "<td align=center> %s </td>\n", $rms;
	printf OUTF "<td align=center> %5.2f </td>\n", $theta;
	printf OUTF "<td align=center> %s </td>\n", $comment;
	printf OUTF "<td><a href=\"%s\"><img src=\"%s\" height=\"60\" width=\"60\" alt=\"Click for larger image\"></a></td>\n", $gifname, $gifname;
	printf OUTF "</tr>\n";
    }
}    
    
printf OUTF "</table>\n";
close(INPF);
close(OUTF);

$Date = localtime(time);
system ("cat tail.html | sed s/TODAYS_DATE/\"$Date\"/g >> $outname");

printf("<li>%s: <A href=\"%s.html\">HTML</a> with images.\n", $name, $name);
printf("              (Also, <A href=\"%s.txt\">plain text</a>.)\n", $name);
exit;
