<& /htmlheader &>

<& modelheader &>

<h2>Nightly Summary</h2>
<table border=1 cellspacing=0 cellpadding=2>
<tr>
<th class="darkheader"> OC Num </th>
<th class="darkheader"> Night Number </th>
<th class="darkheader" colspan=2> Fields </th>
<th class="darkheader"> Per-Field<br>Density </th>
<th class="darkheader"> Detections </th>
<th class="darkheader"> Linking Status </th>
<th class="darkheader"> Details </th>
</tr>

% my $last_ocnum; 
% foreach my $item (@goo) {

% if (!defined($last_ocnum) or $item->{ocnum} != $last_ocnum) {
<tr> <td colspan=8> <a href="effl.html?ocnum=<% $item->{ocnum} %>">Total Linking Efficiency for OC <% $item->{ocnum} %></a> </td> </tr>
% }


<tr>
<td> <% $item->{ocnum} %> </td>
<td> <% $item->{epoch_mjd} %> <br><small><% $item->{date} %><small></td>
<td> <a href="fields.html?nn=<% $item->{epoch_mjd} %>"> <% $item->{fields} %> </a> </td>
<td> <a href="fields.html?nn=<% $item->{epoch_mjd} %>"> <img border=0 src="minifplot?nn=<% $item->{epoch_mjd} %>"> </a> </td>

<td> <a href="density?imgtype=png&nn=<% $item->{epoch_mjd} %>"> PNG </a> |
<a href="density?imgtype=epsc&nn=<% $item->{epoch_mjd} %>"> EPS </a> </td>

<td> <a href="detections.obs?nn=<% $item->{epoch_mjd} %>"> MPC </a> |
<a href="detections.obs?fmt=miti&nn=<% $item->{epoch_mjd} %>"> MITI </a> </td>

<td align="center"> <% $item->{link_status} %> </td>

<td>
<b>NEO Czaring</b><br>
3+ <a href="/czar/<% $model %>/tracklets.html?sort=d&limit=100&nn=<% $item->{epoch_mjd} %>&minv=0.1"> &gt; 0.1deg/day</a> |
<a href="/czar/<% $model %>/tracklets.html?sort=d&limit=100&nn=<% $item->{epoch_mjd} %>">All</a><br>
4+ <a href="/czar/<% $model %>/tracklets.html?sort=d&limit=100&nn=<% $item->{epoch_mjd} %>&minv=0.1&min_dets=4"> &gt; 0.1deg/day</a> |
<a href="/czar/<% $model %>/tracklets.html?sort=d&limit=100&nn=<% $item->{epoch_mjd} %>&min_dets=4">All</a><br>
<a href="/czar/<% $model %>/tracklets.html?sort=d&limit=100&minv=.1&nn=<% $item->{epoch_mjd} %>&sm=MD">MD stacks</a><br>
<p>
<b>Comet</b><br>
<a href="comets.html?limit=100&nn=<% $item->{epoch_mjd} %>">Candidates</a><br>
<p>
<b>Efficiency</b><br>
<a href="efft.html?nn=<% $item->{epoch_mjd} %>">Tracklets</a><br>
<a href="efflddf.html?category=D&nn=<% $item->{epoch_mjd} %>">Linking </a><br>
<a href="dobjs.html?sort=d&limit=100&nn=<% $item->{epoch_mjd} %>">Derived Objs</a><br>
</td>
</tr>

% $last_ocnum = $item->{ocnum};
% }

</table>

<& /htmlfooter &>

<%init>
use Astro::Time;
my $inst = $m->notes('INST');
my $mc = $inst->getConfig();
my $model = $m->notes('MODEL');

# Get basic obsTool summary output.
my @goo;
my @things;
my @cal;
my @stuff = grep !/^#/, `obsTool --instance $model --linkstuff --ocnum all 2>&1 `;
my $link_str;
my $num;
foreach (@stuff) {
    @things = split /\s+/, $_;
    @cal = mjd2cal($things[1]);

    # Figger out link status.

    $link_str = 'NOT STARTED';
    $num = $things[2];

    # OK, here are the rules here for displaying link progress.
    # The number of linkable fields is $things[2], or $num.
    # If $things[4] > 0, slow is done, and fast is completed to $things[4] / $num.
    # Else if $things[3] > 0, slow is completed to $things[3] / $num.
    # Else linking as not started (both == 0).

    if ($things[4] == $num) {
        $link_str = sprintf "COMPLETED<br>%d fields", $num;
    }
    elsif ($things[4] > 0) { 
        $link_str = sprintf "SLOW COMPLETE<br>FAST %d%% (%d/%d)", int(100 * $things[4] / ($num || 1)), $things[4], $num;
    }
    elsif ($things[3] > 0) {
        $link_str = sprintf "SLOW %d%% (%d/%d)<br>FAST NOT STARTED", int(100 * $things[3] / ($num || 1)), $things[3], $num;
    }
    else {
        $link_str = sprintf "UNLINKED";
    }

    push @goo, {
        ocnum => $things[0],
        epoch_mjd => $things[1],
        date => sprintf("%4d-%02d-%02d", @cal[2, 1, 0]),
        fields => $things[2],
        link_status => $link_str
    };
}
</%init>
