<& /htmlheader, title => 'Fuzzy, TJD ' . ($nn + 1 - 50000) &>

<& modelheader &>
<h2>Comet Candidates (Submitted Objects)</h2>
<h3>MOPS Night Number <% $nn %> (TJD <% $nn + 1 - 50000 %>)<br>
<% mopslib_mjd2utctimestr($nn) %> to <% mopslib_mjd2utctimestr($nn + 1) %>
</h3>

<!-- <% $sql %> -->
<!-- <% $found %> -->

<& /lib/pager, rooturi => "?nn=$nn&minv=$minv&min_dets=$min_dets&sm=$sm&show=$show&min_prob=$min_prob", limit => $limit, offset => $offset, found => $found &>

<table border=1 cellspacing=0 cellpadding=2 width="100%">
<tr>
    <th class="darkheader"> Tracklet ID </th>
    <th class="darkheader"> Score <br>
        <a href="<% qq{?limit=100&nn=$nn&found=$found&minv=$minv&min_dets=$min_dets&sm=$sm&show=$show&min_prob=$min_prob} %>">Sort</a>
    </th>
    <th class="darkheader"> Position </th>
    <th class="darkheader"> V<sub>tot</sub> (deg/day)<br>
        <a href="<% qq{?limit=100&nn=$nn&found=$found&minv=$minv&min_dets=$min_dets&sm=$sm&show=$show&min_prob=$min_prob} %>">Sort</a>
    </th>
    <th class="darkheader"> Pos Ang (deg)<br>
        <a href="<% qq{?limit=100&nn=$nn&found=$found&minv=$minv&min_dets=$min_dets&sm=$sm&show=$show&min_prob=$min_prob} %>">Sort</a>
    </th>
    <th class="darkheader"> Known As </th>
    <th class="darkheader"> Known q<br>
        <a href="<% qq{?limit=100&nn=$nn&sort=q&found=$found&minv=$minv&min_dets=$min_dets&sm=$sm&show=$show&min_prob=$min_prob} %>">Sort</a>
    </th>
    <th class="darkheader" valign="top"> GCR (arcsec)<br>
        <a href="<% qq{?limit=100&nn=$nn&sort=g&found=$found&minv=$minv&sm=$sm&min_dets=$min_dets&show=$show&min_prob=$min_prob} %>">Sort</a>
    </th>
    <th class="darkheader" valign="top"> Probability<br>
        <a href="<% qq{?limit=100&nn=$nn&sort=prob&found=$found&minv=$minv&sm=$sm&min_dets=$min_dets&show=$show&min_prob=$min_prob} %>">Sort</a>
    </th>
    <th class="darkheader" valign="top"> Digest </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>
    Digest <a target="_blank" href="neodigest.html?trkId=T<% $trkId %>">MOPS</a>|
    <a target="_blank" href="mpcneodigest.html?trkId=T<% $trkId %>">MPC</a>|
    <a target="_blank" href="neoburn.html?trkId=<% $trkId %>">OORB</a>|
    <a target="_blank" href="alldigest.html?trkId=<% $trkId %>">All</a><br>
    Detections <a target="_blank" href="mtmpc.txt?id=T<% $trkId %>">MPC</a> | <a target="_blank" href="mtdes.txt?id=T<% $trkId %>">DES</a>
</td>
<td align="center"> 
<% sprintf "nfuzz: %d", $href->{nfuzz} %><br>
<% sprintf "ffuzz: %.2f", $href->{ffuzz} %><br>
<a href="stamp_info?trk_id=<% $trkId %>">Stamp Info</a>
</td>
<td>
    <b><% $href->{survey_mode} %></b><br>
    <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 valign="center" align="center"> <% sprintf "%.2f&quot;", $href->{gcr_arcsec} %><br> </td>
<td valign="center" align="center"> <% sprintf "%.2f", $href->{probability} %> </td>
<td valign="center" align="center"> <% $href->{digest} %> </td>

<td>
<& stamp4c_td, dets => $trk->detections, decider => 1, short => 1, show => $show, ffuzzmap => $href->{ffuzzmap} &>
</td>
</tr>
% }
</table>

<& /lib/pager, rooturi => "?nn=$nn&minv=$minv&min_dets=$min_dets&sm=$sm&show=$show&min_prob=$min_prob", limit => $limit, offset => $offset, found => $found &>
<hr style="visibility: hidden; clear: both; margin-top: 14px">
<& /htmlfooter &>

<%args>
$nn
$limit => 250
$offset => 0
$found => undef
$min_dets => 2
$min_prob => 0.4
$minv => 0
$showmd => ''
$sm => ''
$show => 'chip'
</%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::Field;
use PS::MOPS::DC::Tracklet;
use PS::MOPS::DC::Known;

# If $offset and $inc are defined, use them for the mysql query.
# Otherwise use $limit, then as a last resort, all of em.
my $limit_str = '';
my $found_str = '';

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

my $prob_str = '';
if ($min_prob) { 
    if ($min_prob < 0) {
        $prob_str = "and t.probability < " . (-$min_prob);
    }
    else {
        $prob_str = "and t.probability > $min_prob";
    }
}

if (!defined($found)) {
    $found_str = 'SQL_CALC_FOUND_ROWS';
}

my $sm_str = '';
if ($sm) {
    $sm_str = "and survey_mode like '%$sm%'";
}
else {
    unless ($showmd) {
        $sm_str = "and f.survey_mode not like '%MD%'";
    }
}

my $inst = $m->notes('INST');
my $dbname = $inst->dbname;
my $model = $m->notes('MODEL');
my $dbh = $inst->dbh();
my $sql = <<"SQL";
select $found_str
    t.tracklet_id tracklet_id,       
    t.gcr_arcsec gcr_arcsec,
    t.probability probability,
    t.digest digest,
    sum(1) ndet, 
    group_concat(concat(d.det_id,'=',round(sqrt(moments_xx*moments_yy/(psf_major*psf_minor)),2))) ffuzzmap,
    sum(if(dr.psf_quality*dr.ratio_bad*dr.f_pos > 0.2,1,0)) nfuzz,

    /* max of comet fuzz moment ratios */
    avg(if(dr.psf_quality*dr.ratio_bad*dr.f_pos > 0.2,sqrt(moments_xx*moments_yy/(psf_major*psf_minor)),NULL)) as ffuzz,

    f.se_deg sol_elong,      
    f.eb_deg ecl_lat,      
    f.survey_mode 
from  fields f 
    join tracklets t ignore index(classification,known_id) using(field_id)      
    join tracklet_attrib ta using(tracklet_id)  
    join detections d on d.det_id=ta.det_id
    join det_rawattr_v2 dr on d.det_id=dr.det_id
    join export.mpc_sub em on em.det_id=d.det_id
where em.dbname='$dbname' and em.tracklet_id=t.tracklet_id and em.disposition != 'J' and f.nn = $nn and t.classification='N' and t.known_id is null
and t.v_tot > $minv
$prob_str
$sm_str
group by tracklet_id 
having ffuzz > 1.5
and ndet >= $min_dets
and nfuzz > ndet / 2
order by ffuzz desc
$limit_str
SQL

my $sth = $dbh->prepare($sql) or die $dbh->errstr;
$sth->execute() or die $sth->errstr;

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

if ($found_str) {
    ($found) = $dbh->selectrow_array("select FOUND_ROWS()");
}
</%init>
