<& /htmlheader, title => "TJD $tjd $model" &>
<& modelheader &>

<table width="90%">
<tr><td valign="top">

<div class="L24bold"><% $tjd_str %></div>
<div class="L14">
	<% qq{$utdate_str<br><a href="http://ipp0022.ifa.hawaii.edu/ps1sc/denneau/$ocnum/hammer.html">OC $ocnum</a>} %>
</div>
<p height="20px">

<table border=1 cellspacing=0 cellpadding=2>
	<tr>
		<th class="darkheader">Chunk Name</th>
		<th class="darkheader">Last Processing Status</th>
		<th class="darkheader">Num Exposures</th>
	</tr>

% foreach my $k (@chunks_ingested) {
	<tr>
		<td><% $k->{cn} %></td>
		<td align="center"><% $statmap{$k->{status}} || 'UNKNOWN' %></td>
		<td align="center"><% $k->{ct} %></td>
	</tr>
% }
</table>

<h3>Czaring</h3>
<ul>
% if ($have_quads) {
<!--Display quads processed for the night.-->
	<li>
		<a target="_blank" href="/czar/<% $model %>/tracklets.html?sort=d&limit=100&nn=<% $nn %>&minv=0.1">
			All Quads
		</a>
	</li>
%		foreach my $rk (sort {$b <=> $a} keys %runs_quad) {
	<div class="indent1">
		<% $rk ? "<b>Run ".$rk."</b><br/>" : ''%>
	</div>
  <div class="indent2">
%			for(my $i = 0; $i < @{$runs_quad{$rk}}; $i++) {
%   		my ($qv) = $quadchunkmap{$runs_quad{$rk}[$i]{name}};
%     	my $ch = $qv->{name}; my $qct = $qv->{nfoot};
%     	my $urlenc = $qv->{sel}; $urlenc = uri_escape($urlenc);
  	<% qq{<a target="_blank" href="/czar/$model/tracklets.html?sort=d&limit=100&nn=$nn&minv=0.1&sm=$urlenc">$ch</a>} %> 
	  <% $statmap{$runs_quad{$rk}[$i]{status}} || 'UNKNOWN' %> <% qq{($qct footprint} . ($qct > 1 ? 's' : '') . qq{)} %>
  	<br/>
	  <form target="_blank" action="/czar/<% $model %>/tracklets.html" method="GET" style="display: inline">
			<b>Start</b><input type="text" name="offset" value="0" size=4>
    	<b>Num</b><input type="text" name="limit" value="100" size="4">
	    <input type="hidden" name="sort" value="d">
  	  <input type="hidden" name="minv" value="0.1">
    	<input type="hidden" name="sm" value="<% $urlenc %>">
	    <input type="hidden" name="nn" value="<% $nn %>">
  	  <input type="submit" value="Go">
	  </form>
  <br/><br/>
% 		}
	</div>
% 	}
	<a target="_blank" href="/czar/<% $model %>/tracklets.html?sort=d&limit=100&nn=<% $nn %>&minv=0.0&maxv=0.1">Slow Movers</a> (experts only)<br/><br/>
% }      
	
% if ($have_mds) {
	<li>
		<a target="_blank" href="/czar/<% $model %>/tracklets.html?sort=d&limit=100&minv=.1&nn=<% $nn %>&sm=MD">
    	All MDs
    </a>
  </li>
%		foreach my $rk (sort {$b <=> $a} keys %runs_md) {
	<div class="indent1">
		<% $rk ? "<b>Run ".$rk."</b><br/>" : ''%>
  </div>
  <div class="indent2">
%			for(my $i = 0; $i < @{$runs_md{$rk}}; $i++) {
%   		my ($mv) = $quadchunkmap{$runs_md{$rk}[$i]{name}};
%   		my $ch = $mv->{name}; my $mct = $mv->{nfoot};
%   		my $urlenc = $mv->{sel}; $urlenc = uri_escape($urlenc);
 		<% qq{<a target="_blank" href="/czar/$model/tracklets.html?sort=d&limit=100&nn=$nn&minv=0.1&sm=$urlenc">$ch</a>} %> 
    <% $statmap{$runs_md{$rk}[$i]{status}} || 'UNKNOWN' %> <% qq{($mct footprint} . ($mct > 1 ? 's' : '') . qq{)} %>
    <br/>    
% 		}
	</div>
	<br/>
% 	}
% }      

% if ($have_pairs) {
	<li>
  	Pairs (experimental)
  </li>
%		foreach my $rk (sort {$b <=> $a} keys %runs_pair) {
	<div class="indent1">
		<% $rk ? "<b>Run ".$rk."</b><br/>" : ''%> 
  </div>
  <div class="indent2">
%			for(my $i = 0; $i < @{$runs_pair{$rk}}; $i++) {
%   		my ($pv) = $pairchunkmap{$runs_pair{$rk}[$i]{name}};
%     	my $ch = $pv->{name}; my $pct = $pv->{nfoot};
%     	my $urlenc = $pv->{sel}; $urlenc = uri_escape($urlenc);
%     	my $pminv = $guru ? .15 : .3;
	  <% qq{<a target="_blank" href="/czar/$model/pairs.html?minv=$pminv&nn=$nn&sm=$urlenc">$ch</a>} %>
    <% $statmap{$runs_pair{$rk}[$i]{status}} || 'UNKNOWN' %> <% qq{($pct footprint} . ($pct > 1 ? 's' : '') . qq{)} %>
    <br/>
%  		}
	</div>
	<br/>
%		}
% }


	<li>Comets <a target="_blank" href="mbos.html?nn=<% $nn %>">MBOs</a> | Unknown (<a target="_blank" href="comets.html?nn=<% $nn %>&show=diff">Diff</a> | <a target="_blank" href="comets.html?nn=<% $nn %>&show=chip">Chip</a>)</li>
	<li>Derived Objects (<a target="_blank" href="dobjs.html?sort=d&limit=100&nn=<% $nn %>"><% $tjd %> only</a>, <a target="_blank" href="dobjs.html?sort=d&limit=100&ocnum=<% $ocnum %>">OC <% $ocnum %></a>)</li>
	<li><a target="_blank" href="/czar/<% $model %>/known.html?nostamp=1&limit=500&nn=<% $nn %>">Known Pairs</a></li>
	<li><a target="_blank" href="/submitted_neos.html?tjd=<% $tjd %>">Submitted NEOs</a></li>
</ul>

<h3>Efficiency</h3>
<ul>
<li><a target="_blank" href="/model/<% $model %>/efft.html?nn=<% $nn %>">Tracklets</a></li>
<li><a target="_blank" href="/model/<% $model %>/effl.html?ocnum=<% $ocnum %>">Derived objects and PANDAs</a></li>
<li><a target="_blank" href="/model/<% $model %>/deteff?nn=<% $nn %>">Known object efficiency</a></li>
</ul>

<h3>Custom</h3>
<ul>
<li><a target="_blank" href="/model/<% $model %>/custom_themis.html?nn=<% $nn %>">Themis Family</a></li>
<li><a target="_blank" href="/model/<% $model %>/custom_extremelightcurve.html?nn=<% $nn %>">Extreme Lightcurve</a></li>
</ul>

<div>
<a href="/model/<% $model %>/fields.html?nn=<% $nn %>">Night <% $tjd %> field list</a><br>
<a href="/model/<% $model %>/nightlyindex.html">Old index page</a><br>
</div>

</td>

<td align="right" valign="top">
    <canvas id="map" width="700" height="467" style="border: 1px solid #333333; background-color: #000000;">
    <p>Your lame browser doesn't support canvas.</p>
    </canvas>
    <div style="align: right"><a href="?tjd=<% $tjd %>&proj=h">Hammer</a> | <a href="?tjd=<% $tjd %>&proj=s">Spherical</a></div>

<!-- night nav below plot -->
    <table width="700"><tr>
    <td valign="top" width="20px">

% if (defined($prev_tjd)) {
<a href="<% qq{?tjd=$prev_tjd&proj=$proj} %>"><img src="/images/left.png"></a>
</td>
<td align="left" valign="top">
<a href="<% qq{?tjd=$prev_tjd&proj=$proj} %>"><% $prev_tjd_str %></a><br><% "$prev_utdate_str<br>OC $prev_oc" %>
% }
    </td>

    <td align="right" valign="top">
% if (defined($next_tjd)) {
<a href="<% qq{?tjd=$next_tjd&proj=$proj} %>"><% $next_tjd_str %></a><br><% "$next_utdate_str<br>OC $next_oc" %>
    </td>
    <td valign="top" width="20px">
<a href="<% qq{?tjd=$next_tjd&proj=$proj} %>"><img src="/images/right.png"></a>
% } 
    </td>
    </tr></table>

</tr></table>

% if ($proj eq 's') {
<script type="text/javascript" src="/hammer/s3d.js"></script>
<& fieldmapsph.js, tjd => $tjd, show_plan => 's23456' &>
% } else {
<script type="text/javascript" src="/hammer/hammer.js"></script>
<& fieldmap.js, tjd => $tjd, show_plan => 's23456' &>
% }

<& /htmlfooter &>

<%args>
$today => undef
$nn => undef
$tjd => undef
$utdate => undef
$proj => 'h'
$guru => undef
</%args>

<%init>
use Data::Dumper;
use POSIX qw(strftime);
use Astro::Time;
use URI::Escape;
use PS::MOPS::Lib;
use PS::MOPS::DC::Field;

my %statmap = (
    I => 'INGESTED',
    N => 'READY',
    D => 'SYNTH',
    T => 'TRACKLET',
    U => 'POSTTRACKLET',
    A => 'ATTRIB',
    K => 'LINKING',
    L => 'COMPLETED',
);

my $inst = $m->notes('INST');
my $mc = $inst->getConfig();
my $model = $m->notes('MODEL');
my $dbh = $inst->dbh;

# Today's info.
my ($year, $mon, $day);
my $today_nn;
my $today_tjd;
my @schmoo = gmtime;
$year = $schmoo[5] + 1900;
$mon = $schmoo[4] + 1;
$day = $schmoo[3];
$today_tjd = int(cal2mjd($day, $mon, $year, 0)) - 50000;
$today_nn = $today_tjd - 1 + 50000;
my $today_tjd_str = sprintf "TJD %d", $today_nn + 1 - 50000;
my $today_utdate_str = make_nice_date_str($tjd + 50000);
#$today_utdate_str =~ s/T.*//; # trim everything after date
my $today_oc = mopslib_mjd2ocnum($today_nn);

# User info.
if ($tjd) {
    $nn = $tjd - 1 + 50000;
}
elsif ($utdate) {
    if ($utdate =~ /today/) {
        $nn = $today_nn;
    }
    elsif ($utdate =~ /^(\d\d\d\d)[\/.-]?(\d\d)[\/.-]?(\d\d)$/) {
        ($year, $mon, $day) = ($1, $2, $3);
        $nn = int(cal2mjd($day, $mon, $year, 0)) - 1;
    }
}
else {
    # nothing specified, use latest.
    ($nn) = $dbh->selectrow_array('select nn from fields order by nn desc limit 1');
    $nn = $today_nn unless $nn; # no fields, oh well
}

$tjd = $nn - 50000 + 1;
my $tjd_str = sprintf "TJD %d", $nn + 1 - 50000;
my $utdate_str = make_nice_date_str($tjd + 50000);
#$utdate_str =~ s/T.*//; # trim everything after date
#$utdate_str .= ' UT';
my $ocnum = mopslib_mjd2ocnum($nn);

my $off;
my $next_tjd;
my $next_nn;
my $next_tjd_str;
my $next_utdate_str;
my $next_oc;

my $prev_tjd;
my $prev_nn;
my $prev_tjd_str;
my $prev_utdate_str;
my $prev_oc;

$off = get_nearest_nn_offset($dbh, $nn, 1);
if ($off) {
    $next_tjd = $tjd + $off;
    $next_nn = $nn + $off;
    $next_tjd_str = sprintf "TJD %d", $next_tjd;
    $next_utdate_str = make_nice_date_str($next_tjd + 50000);
#    $next_utdate_str =~ s/T.*//; # trim everything after date
#    $next_utdate_str .= ' UT';
    $next_oc = mopslib_mjd2ocnum($next_tjd + 50000);
}

$off = get_nearest_nn_offset($dbh, $nn, -1);
if ($off) {
    $prev_tjd = $tjd + $off;
    $prev_nn = $nn + $off;
    $prev_tjd_str = sprintf "TJD %d", $prev_tjd;
    $prev_utdate_str = make_nice_date_str($prev_tjd + 50000);
#    $prev_utdate_str =~ s/T.*//; # trim everything after date
#    $prev_utdate_str .= ' UT';
    $prev_oc = mopslib_mjd2ocnum($prev_tjd + 50000);
}


my $sth;
#
# Figure out what we've ingested.
#
$sth = $dbh->prepare(<<"SQL") or die $dbh->errstr;
select survey_mode, status, count(*) ct
from fields 
where nn=?
group by survey_mode, status
order by survey_mode, status
SQL
$sth->execute($nn) or die $sth->errstr;
my @chunks_ingested;
while (my $href = $sth->fetchrow_hashref) {
   push @chunks_ingested, { cn => $href->{survey_mode}, status => $href->{status}, ct => $href->{ct} };
}
$sth->finish;

#
# Determine if simulation contains a runs table. If it does not contain the run table then create a
# temporary runs table so that subsequent queries that use it don't fail.
#

$sth = $dbh->prepare(<<"SQL") or die $dbh->errstr;
SELECT COUNT(*) 
FROM information_schema.tables  
WHERE table_schema = ? 
AND table_name = ?
SQL
$sth->execute($model, "runs") or die $dbh->errstr;
my $count = $sth->fetchrow_array();
$sth->finish;
if ($count < 1) {
	$dbh->do(<<"SQL") or die $dbh->errstr;
CREATE TEMPORARY TABLE runs
 (`run_id` BIGINT,
  `field_id` BIGINT,
  `status` CHAR,
  `run_date` TIMESTAMP)
SQL
}

#
# Create a temporary table which will report all of the runs processed during the night along with the 
# chunks processed during the run.
#
$dbh->do(<<"SQL") or die $dbh->errstr;
CREATE TEMPORARY TABLE chunks_processed
(run_id BIGINT,
 survey_mode VARCHAR(120),
 type VARCHAR(120),
 status CHAR,
 footprints INT)
SQL

#
# Insert all quads (including MDs) processed for the night into the temporary table.
#

$sth = $dbh->prepare(<<"SQL") or die $dbh->errstr;
INSERT INTO chunks_processed (run_id, survey_mode, status, type, footprints)
SELECT temp.run_id, temp.quad_name survey_mode, status, type, count(*) 
FROM (
	SELECT run_id, group_concat(distinct survey_mode order by survey_mode separator ' ') quad_name,
  r.status, IF(survey_mode LIKE 'MD%', 'MD', 'Quad') type 
  FROM fields LEFT JOIN runs r 
  USING (field_id) 
  WHERE nn=? 
  GROUP BY run_id, IF(parent_id is null, field_id, parent_id) 
  HAVING count(*) >= 3) AS temp 
GROUP BY run_id, quad_name
SQL
$sth->execute($nn) or die $sth->errstr; 
$sth->finish;

#
# Insert all pairs processed for the night into the temporary table.
#
$sth = $dbh->prepare(<<"SQL") or die $dbh->errstr;
INSERT INTO chunks_processed (run_id, survey_mode, status, type, footprints)
SELECT temp.run_id, temp.quad_name survey_mode, status, type, count(*) 
FROM (
	SELECT run_id, group_concat(distinct survey_mode order by survey_mode separator ' ') quad_name,
  r.status, 'Pair' type 
  FROM fields LEFT JOIN runs r 
  USING (field_id) 
  WHERE nn=? 
  GROUP BY run_id, IF(parent_id is null, field_id, parent_id) 
  HAVING count(*) = 2) AS temp 
GROUP BY run_id, quad_name
SQL
$sth->execute($nn) or die $sth->errstr; 
$sth->finish;

#
# This section creates a hash keyed on run_id, for each run_id there is a single array 
# which contains one or more hash elements. Each hash element consists of two keys :
#   name which contains the name of the chunk
#   status which contains the status the chunk was processued up to during the run
#
my (%runs_pair, %runs_quad, %runs_md);
$sth = $dbh->prepare(<<"SQL") or die $dbh->errstr;
SELECT run_id, survey_mode, type, status
FROM chunks_processed
ORDER BY run_id DESC
SQL
$sth->execute() or die $sth->errstr;
while (my $href = $sth->fetchrow_hashref) {
	my %run_data = ( name => $href->{survey_mode}, status => $href->{status});
	if ($href->{type} eq "Quad") {
 		push @{$runs_quad{$href->{run_id}}}, { %run_data };
  } 
 	elsif ($href->{type} eq "MD") {
		push @{$runs_md{$href->{run_id}}}, { %run_data };
  } 
 	elsif ($guru or $href->{survey_mode} !~ /(z|y)$/) {
 		push @{$runs_pair{$href->{run_id}}}, { %run_data };
  }
}
$sth->finish;

#
# Figure out what quads we have.
#
my %quads_ingested;
my $have_mds;
my $have_quads;
$sth = $dbh->prepare(<<"SQL") or die $dbh->errstr;
SELECT survey_mode, footprints 
FROM chunks_processed 
WHERE type IN ('MD', 'Quad')
SQL
$sth->execute() or die $sth->errstr;
while (my $href = $sth->fetchrow_hashref) {
    if ($href->{survey_mode} =~ /^MD/) {
        $have_mds = 1;
    }
    else {
        $have_quads = 1;
    }
    $quads_ingested{$href->{survey_mode}} = $href->{footprints};
}
$sth->finish;

#
# This section creates a map of queried quad names to a substring
# select for czaring.  In other words, the GROUP_CONCAT() of all
# survey_modes in a quad may include several survey_modes; this
# maps the GROUP_CONCAT() result to a single, shorter string
# used to select tracklets for czaring, e.g.
# 140.3PI.00.ANE1.Q1.g+140.3PI.00.ANE1 => 140.3PI.00.ANE1.Q
#
my %quadchunkmap;
my $sm;
foreach my $ch (keys %quads_ingested) {
    if ($ch =~ /^MD/) {
        $sm = $ch;
    }
    elsif ($ch =~ /CNP/) {
        $sm = 'CNP';
    }
    elsif ($ch =~ /3PI/) {
        my $fooz = $ch;
        $fooz =~ s/\,.*//g;                 # reduce multicolor quads to single string
        my @foo = split /\./, $fooz;
        if ($ch =~ /^\d+\.3PI\.\d\d\.([\w\+-]+)/) {
            $sm = $1;
        }
        else {
            $sm = join('.', @foo[0..$#foo - 2]);
        }
    }
    elsif ($ch =~ /SS/ ) {
        my $fooz = $ch;
        $fooz =~ s/\,.*//g;                 # reduce multicolor quads to single string
        my @foo = split /\./, $fooz;
        $sm = join('.', @foo[0..$#foo - 2]);
    }
    $quadchunkmap{$ch} = { name => $ch, sel => $sm, nfoot => $quads_ingested{$ch} };
}

#
# Figure out what pairs we have.
#
my %pairs_ingested;
$sth = $dbh->prepare(<<"SQL") or die $dbh->errstr;
SELECT survey_mode, footprints 
FROM chunks_processed 
WHERE type = 'Pair'
SQL
$sth->execute() or die $sth->errstr;
my $have_pairs;
while (my $href = $sth->fetchrow_hashref) {
    $have_pairs = 1;
    $pairs_ingested{$href->{survey_mode}} = $href->{footprints};
}
$sth->finish;

#
# This section creates a map of queried pair names to a substring
# select for czaring.  In other words, the GROUP_CONCAT() of all
# survey_modes in a quad may include several survey_modes; this
# maps the GROUP_CONCAT() result to a single, shorter string
# used to select tracklets for czaring, e.g.
# 140.3PI.00.ANE1.Q1.g+140.3PI.00.ANE1 => 140.3PI.00.ANE1.Q
#
my %pairchunkmap;
foreach my $ch (keys %pairs_ingested) {
    next if $ch =~ /MD/ or (!$guru and $ch =~ /(z|y)$/);
    $sm = $ch;
    $pairchunkmap{$ch} = { name => $ch, sel => $sm, nfoot => $pairs_ingested{$ch} };
}

sub get_nearest_nn_offset {
    # return the offset to the TJD in the direction specified (forward or backward) from the
    # specified TJD.  Dir should simply be 1 or -1.  Return none if there is no data in that direction.
    my ($dbh, $nn, $dir) = @_;
    my $foo;
    my $off;
    if ($dir > 0) {
        $foo = $dbh->selectcol_arrayref(<<"SQL") or die $dbh->errstr;
select nn from fields where nn > $nn order by nn limit 1
SQL
    }
    else {
        $foo = $dbh->selectcol_arrayref(<<"SQL") or die $dbh->errstr;
select nn from fields where nn < $nn order by nn desc limit 1
SQL
    }

    if ($foo && $foo->[0]) {
        $off = $foo->[0] - $nn;     # offset from current night
    }
    return $off;
}

sub make_nice_date_str {
    my ($mjd) = @_;
    my $unixtime = ($mjd - 40587) * 86400;  # seconds since Unix epoch (40587 => Unix epoch Jan 1 1970)
    return uc strftime("%a %d %b %Y UT", gmtime($unixtime));
}
</%init>
