<& /htmlheader &>

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

% foreach my $fldkey (@fldkeys) {
<table width="75%" 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"> PA (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>
</tr>
%   my $last_epoch = undef;
%   foreach my $fld (@{$fldtbl{$fldkey}}) {
%     my $field = $allfields{$fld->{field_id}};
<tr>
<td> 
    <b><% "Field ID " . $fld->{field_id} . " ($fld->{epoch})" %></b><br>
    <b><% $field->fpaId %></b><br>
    <b><% $field->surveyMode %></b><br>
    <% mopslib_mjd2localtimestr($field->epoch, 0, 'T') . ' UT' %>

% if (defined($last_epoch)) {
<br> TTI: <% sprintf "%.1f", (($field->epoch - $last_epoch) * 1440) %> min
% }
% $last_epoch = $field->epoch;
<p>

    Solar Elongation: <% sprintf "%.2f", $field->se_deg %>&deg; <br>
    Ecliptic Latitude: <% sprintf "%+.2f", _ecllat($field->ra, $field->dec, $field->epoch) %>&deg; <br>
    Galactic Latitude: <% sprintf "%+.2f", $field->gb_deg %>&deg; <br>
</td>
<td align="right"> <% $fld->{ra} %> </td>
<td align="right"> <% sprintf "%+f", $fld->{dec} %> </td>
<td align="right"> <% $fld->{pa_deg} %> </td>
<td align="center"> <% $fld->{filter} %> </td>
<td align="center"> <% $fld->{exposure_time} . 's' %> </td>
<td align="center"> <% $fld->{num_dets} %> </td>

<td align="center" valign="top"> 
<a href="field_summary.html?field_id=<% $fld->{field_id} %>"> Summary </a>|
<a href="plot_detections.html?field_id=<% $fld->{field_id} %>"> Detections </a>|
% if ($fld->{parent_id} eq 'NULL') {
<a href="plot_tracklets.html?field_id=<% $fld->{field_id} %>"> Tracklets </a>|
% }
<a href="plot_motion?imgtype=png&field_id=<% $fld->{field_id} %>"> Motion </a>

</td>
</tr>
%   } # @{$flds}
</table>
<p>
% } # @{$fldtbl}

<& /htmlfooter &>

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

<%init>
use Astro::Time;
use Astro::SLA;
use PS::MOPS::Constants qw($DEG_PER_RAD);
use PS::MOPS::Lib qw(:all);
use PS::MOPS::DC::Field;

# 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 %fldtbl;
my @fldkeys;
my %allfields;

@stuff = grep !/^#/, @stuff;
foreach (@stuff) {
    @things = split /\s+/, $_;
    my $sort_key;
    
    if ($tupled) {
        $sort_key = ($things[9] eq 'NULL' ? $things[0] : $things[9]);
    }
    else {
        $sort_key = $mjd;
    }

    push @{$fldtbl{$sort_key}}, {
        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],
    };

    $allfields{$things[0]} = modcf_retrieve($inst, fieldId => $things[0]);
}

@fldkeys = sort { $a <=> $b } keys %fldtbl;

sub _mjd2datestr {
    my ($mjd) = @_;
    my ($day, $month, $year, $ut) = mjd2cal($mjd);
    my ($sign, $hour, $min, $sec) = time2hms($ut, 'H', 2);
    return sprintf "%4d-%02d-%02dT%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 _ecllat {
    my ($ra_deg, $dec_deg, $epoch) = @_;
    my ($ecllong, $ecllat);
    slaEqecl($ra_deg / $DEG_PER_RAD, $dec_deg / $DEG_PER_RAD, $epoch, $ecllong, $ecllat);
    return $ecllat * $DEG_PER_RAD;
}


</%init>
