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

<& modelheader &>
<h2>Extreme Light Curve Asteroids</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&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"> &Delta; M </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&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&min_prob=$min_prob} %>">Sort</a>
    </th>
    <th class="darkheader"> Known As </th>
    <th class="darkheader"> Known a<br>
        <a href="<% qq{?limit=100&nn=$nn&sort=q&found=$found&minv=$minv&min_dets=$min_dets&sm=$sm&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&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&min_prob=$min_prob} %>">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>
    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 "%.2f", $href->{deltam} %> </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 align="center"> <% sprintf("%.2f AU", $href->{q} / (1 - $href->{e})) %> </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>
<& stamp4_td, dets => $trk->detections, decider => 1, short => 0 &>
</td>
</tr>
% }
</table>

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

<%args>
$nn
$limit => 100
$offset => 0
$found => undef
$min_dets => 3
$min_prob => 0
$minv => 0
$sm => ''
</%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;

# Constants
my $MIN_SEMI_MAJOR_AXIS = 3.0;
my $MIN_PSF_QUALITY = 0.9;
my $MIN_PSF_EXTENT = 1.0;
my $MAX_PSF_EXTENT = 3.0;
my $ABS_MAG_CUT = 13.0;
my $DELTA_MAG = 0.5;

# 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" : '';
}

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%'";
}

my $inst = $m->notes('INST');
my $model = $m->notes('MODEL');
my $dbh = $inst->dbh();
my $sql = <<"SQL";

SELECT $found_str
    t.tracklet_id AS tracklet_id,       
    t.gcr_arcsec AS gcr_arcsec,
    t.probability AS probability,
    sum(1) AS ndet, 
    max(d.mag) - min(d.mag) AS deltam,
    k.q AS q,      
    k.e AS e,
    k.known_name AS known_name,      
    f.se_deg AS sol_elong,      
    f.eb_deg AS ecl_lat,      
    f.survey_mode AS survey_mode,
    d.psf_extent AS psf_extent,
    d.psf_quality AS psf_quality
FROM  fields f 
    JOIN tracklets t USING(field_id)      
    JOIN known k USING(known_id)      
    JOIN tracklet_attrib ta USING(tracklet_id)  
    JOIN (
        /* Crate a temporary table that contains detections that meet the criteria */
        /* for psf_quality and psf_extent. This temporary table is then joined     */
        /* with the fields, tracklets, known, and tracklet_attrib tables to select */
        /* those tracklets that net the a and magnitude criteria.                  */
        SELECT t.tracklet_id AS tracklet_id,
        d.det_id AS det_id,       
        d.mag AS mag,
        d.filter_id AS filter_id,
        (dr.moments_r1 * dr.moments_r1) / (dr.psf_major * dr.psf_minor) AS psf_extent,
        dr.psf_quality AS psf_quality 
        FROM  fields f 
        JOIN tracklets t USING(field_id)      
        JOIN tracklet_attrib ta USING(tracklet_id)  
        JOIN detections d USING(det_id)
        JOIN det_rawattr_v2 dr USING(det_id)
        WHERE f.nn = $nn AND t.classification='N'
        AND dr.psf_quality > $MIN_PSF_QUALITY
        HAVING psf_extent > $MIN_PSF_EXTENT AND psf_extent < $MAX_PSF_EXTENT
        ) AS d USING(det_id)
WHERE k.h_v < $ABS_MAG_CUT AND k.q / (1 - k.e) > $MIN_SEMI_MAJOR_AXIS
AND t.v_tot > $minv
$prob_str
$sm_str
GROUP BY tracklet_id 
HAVING deltam > $DELTA_MAG
AND ndet >= $min_dets
AND count(distinct d.filter_id) = 1
ORDER BY deltam 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>
