#!/usr/bin/perl

$Root = $ENV{'DOCUMENT_ROOT'};
chdir("$Root/code/snr");

$QueryString = $ENV{'QUERY_STRING'};
if ($QueryString) {
  open (MYFILE, '>input');
  @NameValuePairs = split (/&/, $QueryString);
  foreach $NameValue (@NameValuePairs) {
    ($Name, $Value) = split (/=/, $NameValue);
    $Value =~ tr/+/_/;
    $Value =~ s/%([\dA-Fa-f][\dA-Fa-f])/ pack ("C", hex($1))/eg;
    if (!$Value) {
      break;
      $Incomplete = 1;
    }	
    if ($Name ne "timeOrSnr") {
      print MYFILE "$Value\n";
      $Stored[$index++] = $Value;
    }
    else {
      $TimeOrSnr = $Value;
    }
  }
  print MYFILE "$TimeOrSnr\n";
  print MYFILE "0\n";
  close (MYFILE); 
}

print "Content-type: text/html\n\n";
print "<html>\n\n";
print "<head>\n";
print "<title>Signal-to-Noise Calculator</title>\n";
print "</head>\n";
print "<body bgcolor=\"#ABABAB\">\n";
print "<font size=4>\n";
print "<center>\n";
print "<h3>Signal-to-Noise Calculator</h3>\n";
print "</center>\n";
print "</font>\n";

print "<center>\n";
print "<form action=\"snr.pl\" method=\"get\">\n";
print "<table>\n";

print "<tr>\n";
print "<td>\n";
print "<select name=\"PG\">\n";
$Last = selected($Stored[0], "1");
print "  <option value=\"1\"$Last>Exposure Time</option>\n";
$Last = selected($Stored[0], "2");
print "  <option value=\"2\"$Last>SNR</option>\n";
print "</select>\n";
print "</td>\n";
print "<td>\n";
print "<input type=\"text\" name=\"timeOrSnr\" value=\"$TimeOrSnr\"><br>\n";
print "</td>\n";
print "</tr>\n";

print "<p>\n";
print "k is the magnitude zero point.  ADUs are computed using the GRIM II\n";
print "(mid-90s) value of 4.73 e<sup>-</sup>/ADU.\n";
print "</p>\n";

print "<tr>\n";
print "<td>\n";
print "object size (pixels)<br>\n";
print "</td>\n";
print "<td>\n";
print "<input type=\"text\" name=\"R\" value=\"$Stored[1]\">\n";
print "</td>\n";
print "</tr>\n";
print "<tr>\n";
print "<td>\n";
print "readout noise (e<sup>-</sup>/pixel)<br>\n";
print "</td>\n";
print "<td>\n";
print "<input type=\"text\" name=\"sigma\" value=\"$Stored[2]\">\n";
print "</td>\n";
print "</tr>\n";
print "<tr>\n";
print "<td>\n";
print "k (phot/s/cm/cm/&Aring;)<br>\n";
print "</td>\n";
print "<td>\n";
print "<input type=\"text\" name=\"k\" value=\"$Stored[3]\">\n";
print "</td>\n";
print "</tr>\n";
print "<tr>\n";
print "<td>\n";
print "object magnitude<br>\n";
print "</td>\n";
print "<td>\n";
print "<input type=\"text\" name=\"mobj\" value=\"$Stored[4]\">\n";
print "</td>\n";
print "</tr>\n";
print "<tr>\n";
print "<td>\n";
print "sky brightness (mag/arcsec<sup>2</sup>)<br>\n";
print "</td>\n";
print "<td>\n";
print "<input type=\"text\" name=\"msky\" value=\"$Stored[5]\">\n";
print "</td>\n";
print "</tr>\n";
print "<tr>\n";
print "<td>\n";
print "filter bandwidth (&Aring;)<br>\n";
print "</td>\n";
print "<td>\n";
print "<input type=\"text\" name=\"dl\" value=\"$Stored[6]\">\n";
print "</td>\n";
print "</tr>\n";
print "<tr>\n";
print "<td>\n";
print "pixel area (arcsec<sup>2</sup>)<br>\n";
print "</td>\n";
print "<td>\n";
print "<input type=\"text\" name=\"a2\" value=\"$Stored[7]\">\n";
print "</td>\n";
print "</tr>\n";
print "<tr>\n";
print "<td>\n";
print "system efficiency<br>\n";
print "</td>\n";
print "<td>\n";
print "<input type=\"text\" name=\"E\" value=\"$Stored[8]\">\n";
print "</td>\n";
print "</tr>\n";
print "<tr>\n";
print "<td>\n";
print "ND# (e<sup>-</sup>/phot)<br>\n";
print "</td>\n";
print "<td>\n";
print "<input type=\"text\" name=\"ND\" value=\"$Stored[9]\">\n";
print "</td>\n";
print "</tr>\n";
print "<tr>\n";
print "<td>\n";
print "telescope area (cm<sup>2</sup>)<br>\n";
print "</td>\n";
print "<td>\n";
print "<input type=\"text\" name=\"A\" value=\"$Stored[10]\">\n";
print "</td>\n";
print "</tr>\n";
print "<tr>\n";
print "<td>&nbsp;</td>\n";
print "<td>\n";
print "<input type=\"submit\" value=\"Calculate\">\n";
print "</td>\n";
print "</tr>\n";

print "</table>\n";
print "</form>\n";
print "</center>\n";

if ($QueryString) {
  system("$Root/code/snr/snr < input > output");
  open (MYFILE, "output");
  for ($i = 0; $i < 7; $i++) {
    readline (MYFILE);
  }
  for ($i = 0; $i < 7; $i++) {
    $Out = readline (MYFILE);
    print "$Out<br>";
  }
  close (MYFILE); 
  unlink ("input");
  unlink ("output");
}

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

end;

sub selected {
    if ($_[0] eq $_[1]) {
      return " selected"
    }
    return "";
}
