<& /htmlheader &>

<& modelheader &>
<h2> Nonsynthetic Quad+ Tracklets (<% scalar @rows %> objects)</h2>
<h3>MOPS Night Number <% $nn %> (TJD <% $nn + 1 %>)<br>
<% mopslib_mjd2utctimestr($nn) %> to <% mopslib_mjd2utctimestr($nn + 1) %>
</h3>

% if ($limit_str) {
<b>Showing <% ($offset + 1) %> through <% ($offset + $limit) %>.<br>
<a href="?nn=<% $nn %>&v_max=<% $v_max%>&sort=<% $sort %>&offset=0&limit=<% $limit %>">First</a> |
% if ($offset > 0) {
<a href="?nn=<% $nn %>&v_max=<% $v_max%>&sort=<% $sort %>&offset=<% $offset - $limit > 0 ? $offset - $limit : 0 %>&limit=<% $limit %>">Previous <% $limit %></a> |
% }
<a href="?nn=<% $nn %>&v_max=<% $v_max%>&sort=<% $sort %>&offset=<% $offset + $limit %>&limit=<% $limit %>">Next <% $limit %></a> <p>
% }

<table border=1 cellspacing=0 cellpadding=2 width="100%">
<tr>
    <th class="darkheader"> Tracklet ID </th>
    <th class="darkheader"> digest2 (MOPS)<br>
        <a href="?limit=100&nn=<% $nn %>&v_max=<% $v_max%>&sort=d">Sort</a> 
    </th>
    <th class="darkheader"> Position </th>
    <th class="darkheader"> V<sub>tot</sub> (deg/day)<br>
        <a href="?limit=100&nn=<% $nn %>&v_max=<% $v_max %>&sort=v">Sort</a>
    </th>
    <th class="darkheader"> Pos Ang (deg)<br>
        <a href="?limit=100&nn=<% $nn %>&v_max=<% $v_max %>&sort=p">Sort</a>
    </th>
    <th class="darkheader"> Known As </th>
    <th class="darkheader"> Known q<br>
        <a href="?limit=100&nn=<% $nn %>&v_max=<% $v_max %>&sort=q">Sort</a>
    </th>
    <th class="darkheader"> Stamps </th>
</tr>
<tr>
% foreach $href (@rows) {
% my $trkId = $href->{tracklet_id};
% my $trk = modct_retrieve($inst, trackletId => $trkId);
<td align="center"> <span class="L14bold"><a target="_blank" href="tracklet?tid=<% $trkId %>"><% $trkId %></a></span><br>
    <a target="_blank" href="mpcheckt.html?trkId=<% $trkId%>">MPCheck</a><br>
    <a target="_blank" href="neodigest.html?trkId=T<% $trkId%>">NEO Digest (MOPS)</a><br>
    <a target="_blank" href="mpcneodigest.html?trkId=T<% $trkId%>">NEO Digest (MPC)</a><br>
    <a target="_blank" href="neoburn.html?trkId=<% $trkId%>">OORB NEO</a><br>
    <a target="_blank" href="mtmpc.txt?id=T<% $trkId%>">MPC Dets</a>
</td>
<td align="center"> <% sprintf "%.1f", $trk->digest %> </td>
<td>
    <table border=0 cellpadding=0 cellspacing=4>
    <tr>
        <td align="left">&alpha;: <% sprintf "%.6f", $trk->extRA %><br><span class="L10G"><% turn2str(deg2turn($trk->extRA), 'H', 2, 'hms') %></span></td>
        <td>&delta;: <% sprintf "%.6f", $trk->extDEC %><br> <span class="L10G"><% turn2str(deg2turn($trk->extDEC), 'D', 2, 'deg') %></span></td>
    </tr>
    <tr><td colspan=2>&nbsp;</td></tr>
    <tr>
        <td>S Elong: <% sprintf "%.2f", $href->{sol_elong} %></td>
        <td>Ecl &beta;: <% sprintf "%+.2f", $href->{ecl_lat} %></td>
    </tr>
    </table>
</td>

<td align="center"> <% sprintf "%.3f", $trk->vTot %> </td>
<td align="center"> <% sprintf "%.1f", $trk->posAng %> </td>
<td align="center"> <& /mpc_link, desig => $href->{known_name} &> </td>
<td <% ($href->{q} && $href->{q} <= 1.3) ? 'style="background:red"' : '' %> align="center"> <% $href->{q} ? sprintf("%.2f", $href->{q}) : 'N/A' %> </td>
<td> 
<& stamp4_td, dets => $trk->detections &>
</td>
</tr>
% }
</table>

% if ($limit_str) {
<a href="?nn=<% $nn %>&v_max=<% $v_max%>&sort=<% $sort %>&offset=0&limit=<% $limit %>">First</a> |
% if ($offset > 0) {
<a href="?nn=<% $nn %>&v_max=<% $v_max%>&sort=<% $sort %>&offset=<% $offset - $limit > 0 ? $offset - $limit : 0 %>&limit=<% $limit %>">Previous <% $limit %></a> |
% }
<a href="?nn=<% $nn %>&v_max=<% $v_max%>&sort=<% $sort %>&offset=<% $offset + $limit %>&limit=<% $limit %>">Next <% $limit %></a> <p>
% }

<& /htmlfooter &>

<%args>
$nn
$limit => undef
$sort => 'd'       # 'd', 'v', 'q', 'p' allowed (digest, velocity, q, posang)
$offset => undef
$v_max => 0.15
</%args>

<%init>
use Astro::Time;
use PS::MOPS::DC::Orbit;
use PS::MOPS::Lib qw(:all);
use PS::MOPS::Constants qw(:all);
use PS::MOPS::DC::Tracklet;
use PS::MOPS::DC::Known;

my $limit_str = '';
my $sort_str = '';

if (defined($offset)) {
    $limit = 100 unless $limit;
    $limit_str = sprintf 'limit %d,%d', $offset, $limit;
}
else {
    $limit_str = $limit ? "limit $limit" : '';
}

if ($sort eq 'q') {
    $sort_str = 'order by if(q is null,999,q)';
}
elsif ($sort eq 'v') {
    $sort_str = 'order by v_tot desc';
}
elsif ($sort eq 'p') {
    $sort_str = 'order by pos_ang_deg desc';
}
else {
    $sort_str = 'order by digest desc, v_tot desc';
}

my $inst = $m->notes('INST');
my $model = $m->notes('MODEL');
my $dbh = $inst->dbh();
my $sth = $dbh->prepare(<<"SQL") or die $dbh->errstr;
select
    t.tracklet_id tracklet_id,
    t.digest digest,
    k.q q,
    k.known_name known_name,
    f.se_deg sol_elong,
    f.eb_deg ecl_lat
from fields f
    join tracklets t using(field_id)
    left join known k using(known_id)
    join tracklet_attrib ta using(tracklet_id)
where f.nn = $nn and t.classification='N'
and t.v_tot < $v_max group by ta.tracklet_id
having count(tracklet_id) > 3
$sort_str
$limit_str
SQL
$sth->execute() or die $sth->errstr;

my @rows;
my $href;
while ($href = $sth->fetchrow_hashref()) {
    push @rows, $href;
}

# Build result list of tracklets.
</%init>
