<%doc>
=head1 NAME

old_exposures.html

=head1 SYNOPSIS

Use:
    old_exposures.html?instance="database_name"&nn="night_number"
    
=head1 Description

This mason component displays a table that shows the chunks that still need to 
be processed by mops. For each chunk the table shows the number of exposures in the 
chunk as well as the number of exposures that have been ingested into MOPS.

=head1 ARGUMENTS

=over 4

=item instance

Name of the database containing the MOPS data.

=item nn

The night number from which the chunks will be retrieved.
    
=back

=head1 AUTHOR

Denver Green, E<lt>dgreen@ifa.hawaii.eduE<gt>.

=head1 COPYRIGHT

Copyright 2012 by Institute for Astronomy, University of Hawaii.

=head1 DATE

18-Apr-2012

=cut
</%doc>


<!--Start of foreach loop.-->
<%perl>
	my $show_chunk; 
  foreach $chunk (sort(keys(%chunks))) {
   	# Determine if the chunk has any fields with the status of ingested. As only
    # nights that have ingested unprocessed fields are to be displayed.
  	$show_chunk = PS::MOPS::Admin::chunk_has_status($instance, $nn, $chunk, $FIELD_STATUS_INGESTED);
		next if !$show_chunk;
</%perl>
<a href="admin.html?instance='<% $instance %>'&nn=<% $nn %>">
	<span class="L12" id=<% $date_id %>><% $utdate_str %> / NN <% $nn %></span>
</a><br />
<%perl>
		# Exit for loop as we have found an ingested chunk that needs to be processed
		last;
	}
</%perl>
<!--End of foreach loop.--> 

<!-- Debugging fields. Uncomment if you want to examine values.
<p>MJD: <input id="<% $mjd_id %>" type="text"></p>
<p>Year: <input id="<% $year_id %>" type="text"></p>
<p>Month: <input id="<% $month_id %>" type="text"></p>
<p>Day: <input id="<% $day_id %>" type="text"></p>
<p>UT Date: <input id="<% $utDate_id %>" type="text"></p>
<p>NN: <input id="<% $nn_id %>" type="text"></p>

<script language="javascript" type="text/javascript">
$(document).ready(function() {

	$("#<% $mjd_id %>").val("<% $mjd %>");
	$("#<% $year_id %>").val("<% $year %>");
	$("#<% $month_id %>").val("<% $mon %>");
	$("#<% $day_id %>").val("<% $day %>");
	$("#<% $utDate_id %>").val("<% $utdate_str %>");
	$("#<% $nn_id %>").val("<% $nn %>");
});
</script>
-->

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

<%init>
use PS::MOPS::Admin qw(:all);
use PS::MOPS::Constants qw($FIELD_STATUS_INGESTED);
use PS::MOPS::Lib qw(:all);
use POSIX qw(strftime);
use Astro::Time;

my $date_id = "date_" . $nn;

# Debugging defines
my $mjd_id = "mjd_" . $nn;
my $year_id = "year_" . $nn;
my $month_id = "month_" . $nn;
my $day_id = "day_" . $nn;
my $utDate_id = "utDate_" . $nn;
my $nn_id = "nn_" . $nn;

# Convert night number to a nicely formated date string.
my $mjd = mopslib_nn2mjd(($nn + 1), -10); # Add 1 to nn so at to compensate for HI time zone
my ($day, $mon, $year) = mjd2cal($mjd);
my $utdate_str = make_nice_date_str($mjd);

# Get list of exposures downloaded to IPP from the summit.
my $ipp_exp_ref = PS::MOPS::Admin::list_ipp_fields($nn);

# Get list of exposures ingested into MOPS.
my $mops_exp_ref = PS::MOPS::Admin::list_mops_fields($instance, $nn);

# Get list of missing exposures.
my $missing_exp_ref = PS::MOPS::Admin::list_missing_exposures($instance, $nn);

# Combine IPP and MOPS exposures into one hash so as to make
# it easy to generate a table reporting the data.
my %chunks;
my $chunk;
my $total_exposures;
my $ingested_exposures;

foreach $chunk (keys(%$ipp_exp_ref)) {
	$total_exposures = $ipp_exp_ref->{$chunk};
    if (exists $mops_exp_ref->{$chunk}) {
    	$ingested_exposures = $mops_exp_ref->{$chunk};
    } else {
    	$ingested_exposures = 0;
    }
	$chunks{$chunk} = [$total_exposures, $ingested_exposures];
}

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>