<& /htmlheader &>
<style type="text/css">
	table.mt {border-width: 1px;border-spacing:0px ;border-style: solid;border-color: #cfcfcf;border-collapse: collapse;background-color: transparent;}
</style>

<h1>Alert generated by <% $rule %> rule.</h1></br>
% foreach my $orbit (@orbits) {
<h2>Orbital Elements at Epoch <% $orbit->{epoch} %></h2>

<table id="orbit" class="mt" cellspacing="0" cellpadding="2" width="350px">
  <tr class="darkheader">
    <th scope="col">Element</th>
    <th scope="col">Value</th>
    <th scope="col">Units</th>
  </tr>
  <tr align=center>
    <th scope="row">Semi-major Axis (q)</th>
    <td align=right><% $orbit->{q} %></td>
    <td>AU</td>
  </tr>
  <tr align=center>
    <th scope="row">Eccentricity (e)</th>
    <td align=right><% $orbit->{e} %></td>
    <td>&nbsp;</td>
  </tr>
  <tr align=center>
    <th scope="row">Inclination (i)</th>
    <td align=right><% $orbit->{i} %></td>
    <td>deg</td>
  </tr>
  <tr align=center>
    <th scope="row">Ascending Node</th>
    <td align=right><% $orbit->{node} %></td>
    <td>deg</td>
  </tr>
  <tr align=center>
    <th scope="row">Argument of Perihelion</th>
    <td align=right><% $orbit->{aop} %></td>
    <td>deg</td>
  </tr>
  <tr align=center>
    <th scope="row">Time of Perihelion</th>
    <td align=right><% $orbit->{mjd} %></td>
    <td>MJD</td>
  </tr>
  <tr align=center>
    <th scope="row">Absolute Magnitude</th>
    <td align=right><% $orbit->{absMag} %></td>
    <td>&nbsp;</td>
  </tr>
  <tr align=center>
    <th scope="row">RMS Residuals</th>
    <td align=right><% $orbit->{residuals} %></td>
    <td>arcsec</td>
  </tr>
  <tr align=center>
    <th scope="row">chiSq</th>
    <td align=right><% $orbit->{chiSq} %></td>
    <td>&nbsp;</td>
  </tr>
  <tr align=center>
    <th scope="row">JPL convergence code</th>
    <td align=right><% $orbit->{convCode} %></td>
    <td>&nbsp;</td>
  </tr>
  <tr align=center>
    <th scope="row">moid1</th>
    <td align=right><% $orbit->{moid1} %></td>
    <td>&nbsp;</td>
  </tr>
  <tr align=center>
    <th scope="row">moid2&nbsp;</th>
    <td align=right><% $orbit->{moid2} %></td>
    <td>&nbsp;</td>
  </tr>
</table>
% }

<table id="observations" class="mt" cellspacing="0" cellpadding="2" width="550px">
	<h2>
    Detections
  </h2>

	<tr class="darkheader"> 
   	<th>
    	Date
   	</th>
   	<th>
    	Magnitude
   	</th>
   	<th>
    	RA (deg)
    </th>
    <th>
    	DEC (deg)
    </th>
	</tr>
% foreach my $observation (@observations) {
		<tr>
			<td align=center>
				<% $observation->{time} %>
			</td>
			<td align=center>
				<% $observation->{mag} %>
			</td>
			<td align=center>
				<% $observation->{ra} %>
			</td>
			<td align=center>
				<% $observation->{dec} %>
			</td>
		</tr>
% }
</table>

<table class="mt" width="550" cellspacing="0" cellpadding="2">
	<h2>Geocentric Ephemerides</h2>
  <tr class="darkheader">
    <th scope="col">Date</th>
    <th scope="col">Magnitude</th>
    <th scope="col">RA</th>
    <th scope="col">Err RA</th>
    <th scope="col">DEC</th>
    <th scope="col">Err DEC</th>
  </tr>
% foreach my $ephemeris (@ephemerides) {
		<tr>
			<td align=center>
				<% $ephemeris->{time} %>
			</td>
			<td align=center>
				<% $ephemeris->{mag} %>
			</td>
			<td align=center>
				<% $ephemeris->{ra} %>
			</td>
			<td align=center>
				<% $ephemeris->{errRa} %>
			</td>
			<td align=center>
				<% $ephemeris->{dec} %>
			</td>
			<td align=center>
				<% $ephemeris->{errDec} %>
			</td>
			
		</tr>
% }
</table>

% foreach my $orbit (@orbits) {
<table class="mt" width="550" cellspacing="0" cellpadding="2" summary="EC = eccentricity
QR = perihelion distance
TP = time perihelion
OM = node
W  = arg perihelion
IN = inclination">
  <h2>
    Orbit covariance matrix
  </h2>
  <tr class="darkheader">
    <th scope="col">&nbsp;</th>
    <th scope="col">EC</th>
    <th scope="col">QR</th>
    <th scope="col">TP</th>
    <th scope="col">OM</th>
    <th scope="col">W</th>
    <th scope="col">IN</th>
  </tr>
  <tr align=center>
    <th scope="row" class="darkheader">EC</th>
    <td> <% $orbit->{c01} %> </td>
    <td> <% $orbit->{c02} %> </td>
    <td> <% $orbit->{c04} %> </td>
    <td> <% $orbit->{c07} %> </td>
    <td> <% $orbit->{c11} %> </td>
    <td> <% $orbit->{c16} %> </td>
  </tr>
  <tr align=center>
    <th scope="row" class="darkheader">QR</th>
    <td>&nbsp;</td>
    <td> <% $orbit->{c03} %> </td>
    <td> <% $orbit->{c05} %> </td>
    <td> <% $orbit->{c08} %> </td>
    <td> <% $orbit->{c12} %> </td>
    <td> <% $orbit->{c17} %> </td>
  </tr>
  <tr align=center>
    <th scope="row" class="darkheader">TP</th>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td> <% $orbit->{c06} %> </td>
    <td> <% $orbit->{c09} %> </td>
    <td> <% $orbit->{c13} %> </td>
    <td> <% $orbit->{c18} %> </td>
  </tr>
  <tr align=center>
    <th scope="row" class="darkheader">OM</th>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td> <% $orbit->{c10} %> </td>
    <td> <% $orbit->{c14} %> </td>
    <td> <% $orbit->{c19} %> </td>
  </tr>
  <tr align=center>
    <th scope="row" class="darkheader">W</th>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td> <% $orbit->{c15} %> </td>
    <td> <% $orbit->{c20} %> </td>
  </tr>
  <tr align=center>
    <th scope="row" class="darkheader">IN</th>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td> <% $orbit->{c21} %> </td>
  </tr>
</table>
% }
<caption>
		EC = eccentricity;
		QR = perihelion distance;
		TP = time perihelion;
		OM = node;
		W  = arg perihelion;
		IN = inclination
</caption>

<& /htmlfooter &>

<%args>
$alertId
$rule
</%args>

<%once>
use DBI;
# intialize database variables.
my $dbserver = "mops00.ifa.hawaii.edu";
my $user = "mops";
my $passwd = "mops";
my $inst = "export";
my $connectionStr = "dbi:mysql:host=" . $dbserver . ";database=" . $inst . ";port=3306";

# connect to database
my $dbh = DBI->connect($connectionStr, $user, $passwd) ||
	die dbh->errstr;
</%once>

<%init>
use strict; 
use warnings; 
use XML::Simple; 

# get the voEvent associated with the alert.
my $sql = "select vo_event from export.alerts where alert_id = ?";
my $cursor = $dbh->prepare($sql) || die dbh->errstr;
$cursor->execute($alertId);
my $result = $cursor->fetchrow_arrayref;
my $voEvent = $result->[0];

# ParsevoEvent. 
my $voEvent_xml = XMLin($voEvent, forcearray=>0);

 
my $whereWhen = $voEvent_xml->{WhereWhen};
my $obsDataLocation = $whereWhen->{ObsDataLocation};
my @observations;
my @orbits;
my @ephemerides;

my $coord;
foreach my $obs (@$obsDataLocation){
	$coord = $obs->{ObservationLocation}->{AstroCoords};
	if (uc($obs->{description}) eq "OBSERVATIONS") {
		my %observation;
		$observation{mag} = $coord->{ScalarCoordinate}->{Value};
		$observation{time} = $coord->{Time}->{TimeInstant}->{MJDTime};
		$observation{ra} = $coord->{Position2D}->{Value2}->{C1};
		$observation{dec} = $coord->{Position2D}->{Value2}->{C2};
		push(@observations, \%observation);
	}
	elsif (uc($obs->{description}) eq "ORBIT") {
		my %orbit;
		$orbit{epoch} = $coord->{Time}->{TimeInstant}->{MJDTime};
		$orbit{q} = $coord->{Orbit}->{q}->{content}; # semi-major axis, AU
		$orbit{e} = $coord->{Orbit}->{e}; # eccentricity
		$orbit{i} = $coord->{Orbit}->{i}->{content}; # inclination, degrees
		$orbit{node} = $coord->{Orbit}->{Node}->{content}; # longitude of ascending node, degrees
		$orbit{aop} = $coord->{Orbit}->{Aop}->{content}; # argument of perihelion, degrees
		$orbit{mjd} = $coord->{Orbit}->{T}->{MJDTime}; # time of perihelion, MJD
		$orbit{absMag} = $coord->{Orbit}->{h_v}; # abosolute magnitude
		$orbit{residuals} = $coord->{Orbit}->{residuals}; # RMS residual, arcsec
		$orbit{chiSq} = $coord->{Orbit}->{chiSq}; # chi-squared statistic
		$orbit{convCode} = $coord->{Orbit}->{convCode}; # JPL convergence code
		$orbit{moid1} = $coord->{Orbit}->{moid1}; # minimum orbital intersection distance soln 1
		$orbit{moid2} = $coord->{Orbit}->{moid2}; # minimum orbital intersection distance soln 2
		
   	# Orbit covariance matrices are upper-triangular matrices obtained from the JPL
		# differential corrector.  The values are passed as a linear array whose elements
		# are numbered as follows:
		#
    	# 		EC    QR    TP    OM    W     IN
		# EC   	 1     2     4     7    11    16
		# QR           3     5     8    12    17
		# TP               	 6     9    13    18
		# OM                      10    14    19
		# W                             15    20
		# IN                                  21
		#
		# EC = eccentricity
		# QR = perihelion distance
		# TP = time perihelion
		# OM = node
		# W  = arg perihelion
		# IN = inclination
		$orbit{c01} = $coord->{Orbit}->{SRC}->{Value21}->{C01};
		$orbit{c02} = $coord->{Orbit}->{SRC}->{Value21}->{C02};
		$orbit{c03} = $coord->{Orbit}->{SRC}->{Value21}->{C03};
		$orbit{c04} = $coord->{Orbit}->{SRC}->{Value21}->{C04};
		$orbit{c05} = $coord->{Orbit}->{SRC}->{Value21}->{C05};
		$orbit{c06} = $coord->{Orbit}->{SRC}->{Value21}->{C06};
		$orbit{c07} = $coord->{Orbit}->{SRC}->{Value21}->{C07};
		$orbit{c08} = $coord->{Orbit}->{SRC}->{Value21}->{C08};
		$orbit{c09} = $coord->{Orbit}->{SRC}->{Value21}->{C09};
		$orbit{c10} = $coord->{Orbit}->{SRC}->{Value21}->{C10};
		$orbit{c11} = $coord->{Orbit}->{SRC}->{Value21}->{C11};
		$orbit{c12} = $coord->{Orbit}->{SRC}->{Value21}->{C12};
		$orbit{c13} = $coord->{Orbit}->{SRC}->{Value21}->{C13};
		$orbit{c14} = $coord->{Orbit}->{SRC}->{Value21}->{C14};
		$orbit{c15} = $coord->{Orbit}->{SRC}->{Value21}->{C15};
		$orbit{c16} = $coord->{Orbit}->{SRC}->{Value21}->{C16};
		$orbit{c17} = $coord->{Orbit}->{SRC}->{Value21}->{C17};
		$orbit{c18} = $coord->{Orbit}->{SRC}->{Value21}->{C18};
		$orbit{c19} = $coord->{Orbit}->{SRC}->{Value21}->{C19};
		$orbit{c20} = $coord->{Orbit}->{SRC}->{Value21}->{C20};
		$orbit{c21} = $coord->{Orbit}->{SRC}->{Value21}->{C21};
		push(@orbits, \%orbit);
	}
	elsif (uc($obs->{description}) eq "GEOCENTRIC EPHEMERIDES") {
		my %ephemeris;
		$ephemeris{mag} = $coord->{ScalarCoordinate}->{Value};
		$ephemeris{time} = $coord->{Time}->{TimeInstant}->{MJDTime};
		$ephemeris{ra} = $coord->{Position2D}->{Value2}->{C1};
		$ephemeris{dec} = $coord->{Position2D}->{Value2}->{C2};
		$ephemeris{errRa} = $coord->{Position2D}->{Error2}->{C1};
		$ephemeris{errDec} = $coord->{Position2D}->{Error2}->{C2};
		push(@ephemerides, \%ephemeris);
	}
}
</%init>