<& /htmlheader &>

<& modelheader &>
<h2>Field Summary</h2>
<h3 class="darkheaderverylarge">
<% sprintf "Night %d (MJD %.5f-%.5f)", $nn, $mjd, $mjd + 1 %><br>
<% _mjd2dateonly($nn) %>
</h3>

<table border=1 cellspacing=0 cellpadding=2>
<tr>
<th class="darkheader" width=200> Field ID </th>
<th class="darkheader"> RA (deg) </th>
<th class="darkheader"> DEC (deg) </th>
<th class="darkheader"> Filter </th>
<th class="darkheader"> Exposure<br>Time </th>
<th class="darkheader"> Num<br>Detections </th>
<th class="darkheader"> Detections </th>
<th class="darkheader"> Motion </th>
</tr>
% foreach my $item (@goo) {
<tr>
<td> 
    <b><% "Field ID " . $item->{field_id} . " ($item->{epoch})" %></b><br>
    <% _mjd2datestr($item->{epoch} + $gmt_offset_hours / 24.0) %><br>
    Observatory <% $item->{obscode} %> 
</td>
<td align="right"> <% $item->{ra} %> </td>
<td align="right"> <% $item->{dec} %> </td>
<td align="center"> <% $item->{filter} %> </td>
<td align="center"> <% $item->{exposure_time} . 's' %> </td>
<td align="center"> <% $item->{num_dets} %> </td>

<td align="center" valign="top"> 
<a href="field_summary.html?field_id=<% $item->{field_id} %>"> Summary </a><br>
% if ($item->{parent_id} eq 'NULL') {
<a href="plot_tracklets.html?field_id=<% $item->{field_id} %>"> Tracklets vs. Known </a><br>
% }
<a href="detections.obs?field_id=<% $item->{field_id} %>"> MPC </a> |
<a href="detections.obs?fmt=miti&field_id=<% $item->{field_id} %>"> MITI </a> |
<a href="plot_detections.html?field_id=<% $item->{field_id} %>"> PNG </a> |
<a href="plot_detections?imgtype=epsc&field_id=<% $item->{field_id} %>"> EPS </a> 
</td>

<td align="center" valign="top"> 
<a href="motion?imgtype=png&field_id=<% $item->{field_id} %>"> PNG </a> |
<a href="motion?imgtype=epsc&field_id=<% $item->{field_id} %>"> EPS </a> |
<a href="motion.txt?field_id=<% $item->{field_id} %>"> TXT </a> </td>

</tr>
% }
</table>

<& /htmlfooter &>

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

<%init>
use Astro::Time;
use PS::MOPS::Lib qw(:all);

# Get basic obsTool summary output.
my $model = $m->notes('MODEL');
my $inst = $m->notes('INST');
my $cfg = $inst->getConfig;
my $gmt_offset_hours = $cfg->{site}->{gmt_offset_hours};
die "can't get local time" unless defined($gmt_offset_hours);

if ($nn) {
    $mjd = mopslib_nn2mjd($nn, $gmt_offset_hours);
}

my @stuff = `obsTool --instance $model --show_filter --show_parent_id --show_exposure_time --mjd $mjd`;
my @cal = mjd2cal($mjd);
my $date = sprintf "%4d-%02d-%02d", @cal[2, 1, 0];
my @things;
my @goo;

@stuff = grep !/^#/, @stuff;
foreach (@stuff) {
    @things = split /\s+/, $_;
    push @goo, {
        field_id => $things[0],
        num_dets => $things[1],
        epoch => $things[2],
        ra => $things[3],
        dec => $things[4],
        obscode => $things[6],
        filter => $things[7],
        exposure_time => $things[8],
        parent_id => $things[9],
    };
}


sub _mjd2datestr {
    my ($mjd) = @_;
    my ($day, $month, $year, $ut) = mjd2cal($mjd);
    my ($sign, $hour, $min, $sec) = time2hms($ut, 'H', 2);
    return sprintf "%4d-%02d-%02d<br>%02d:%02d:%04.1f", $year, $month, $day, $hour, $min, $sec;
}

sub _mjd2dateonly {
    my ($mjd) = @_;
    my ($day, $month, $year, $ut) = mjd2cal($mjd);
    return sprintf "%4d-%02d-%02d", $year, $month, $day;
}

sub _stackify {
    # Given a list of field-like objects, organize them into "stacks", a hash of lists.
    # The last item of each sub-list is the parent field.
    my (@fields) = @_;
    my %stack;
    foreach my $field (@fields) {
        if 
    }
}

</%init>
