<& /htmlheader &>

<& modelheader &>
<h2>Tracklet Efficiency, MJD <% $epoch_mjd %> </h2>
<table border=1 cellspacing=0 cellpadding=2>
<tr>
<th class="darkheader"> Field ID </th>
<th class="darkheader"> Available </th>
<th class="darkheader" colspan=2> Found (%) </th>
<th class="darkheader" colspan=2> Clean (%) </th>
<th class="darkheader" colspan=2> Unfound (%) </th>
<th class="darkheader" colspan=2> Mixed (%) </th>
<th class="darkheader" colspan=2> Bad (%) </th>
<th class="darkheader"> Nonsynthetic </th>
<th class="darkheader"> Details </th>
</tr>
% my $tot_found = 0;
% my $tot_avail = 0;
% foreach my $item ($goo->[-1], @{$goo}) {
% if ($item->{fieldId}) {
%   $field = modcf_retrieve($inst, fieldId => $item->{fieldId});
<tr>
<td align="left"> <a href="plot_detections?imgtype=png&field_id=<% $item->{fieldId} %>"><% $item->{fieldId}%> </a> </td>
% } else {
<td align="left"> <% 'Total' %> </td>
% }
<td class="htable1" align="right"> <% $item->{avail} %> </td>

<td class="htable2" align="right"> <% $item->{clean} %> </td>
<td class="htable2" align="right"> <% sprintf "%.1f", 100 * $item->{clean} / ($item->{avail} || 1) %> </td>

% { my $color_str = "#FFFFFF";
% $color_str = "#FFFF99" if $item->{clean} < $item->{avail};
% $color_str = "#FFFF00" if $item->{clean} < 0.75*$item->{avail};
% $color_str = "#FF6600" if $item->{clean} < 0.5*$item->{avail};
% $color_str = "#FF0000" if $item->{clean} < 0.25*$item->{avail};
<td class="htable1" align="right" bgcolor="<% $color_str %>"> <% $item->{clean} %> </td>
<td class="htable1" align="right" bgcolor="<% $color_str %>"> <% sprintf "%.1f", 100 * $item->{clean} / ($item->{avail} || 1) %> </td>
% }

<td class="htable2" align="right"> <% $item->{unfound} %> </td>
<td class="htable2" align="right"> <% sprintf "%.1f", 100 * $item->{unfound} / ($item->{avail} || 1) %> </td>

% { my $color_str = "#FFFFFF";
% $color_str = "#FFFF99" if $item->{mixed} > 0.;
<td class="htable1" align="right" bgcolor="<% $color_str %>"> <% $item->{mixed} %> </td>
<td class="htable1" align="right" bgcolor="<% $color_str %>"> <% sprintf "%.1f", 100 * $item->{mixed} / ($item->{avail} || 1) %> </td>
% }

% { my $color_str = "#FFFFFF";
% $color_str = "#FFFF99" if $item->{bad} > 0.;
<td class="htable2" align="right" bgcolor="<% $color_str %>"> <% $item->{bad} %> </td>
<td class="htable2" align="right" bgcolor="<% $color_str %>"> <% sprintf "%.1f", 100 * $item->{bad} / ($item->{avail} || 1) %> </td>
% }

<td class="htable1" align="right"> <b><% $item->{nonsynthetic} %> </b></td>

<td align="right"> <a href="efftd.html?field_id=<% $item->{fieldId} %>">Details</a></td>
</tr>
% }
</table>

<& /htmlfooter &>

<%args>
$epoch_mjd => undef
$nn => undef
</%args>

<%init>
use PS::MOPS::Lib qw(:all);
use PS::MOPS::DC::Field;
use PS::MOPS::DC::Efficiency;

my $inst = $m->notes('INST');
my $config = $inst->getConfig();
my $gmt_offset_hours = $config->{site}->{gmt_offset_hours};
if ($nn) {
    $epoch_mjd = mopslib_nn2mjd($nn, $gmt_offset_hours);
}

sub _sum {
    my @data = @_;
    my $tot = 0;
    $tot += $_ foreach @_;
    return $tot;
}

my $field;
my $goo = modce_retrieve($inst, category => $EFF_TRACKLETS, epoch_mjd => $epoch_mjd);

# Create sum row.  Cheesy.
push @{$goo}, {
    fieldID => undef,
    found => _sum(map { $_->{found} } @{$goo}),
    avail => _sum(map { $_->{avail} } @{$goo}),
    clean => _sum(map { $_->{clean} } @{$goo}),
    unfound => _sum(map { $_->{unfound} } @{$goo}),
    mixed => _sum(map { $_->{mixed} } @{$goo}),
    bad => _sum(map { $_->{bad} } @{$goo}),
    nonsynthetic => _sum(map { $_->{nonsynthetic} } @{$goo}),
};
</%init>
