<%doc>
=head1 NAME

exposures.html

=head1 SYNOPSIS

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

This mason component displays a table showing the chunks downloaded from the 
telescope to IPP. 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

17-Feb-2012

=cut
</%doc>

<script type="text/javascript">
$(document).ready(function() {
		
	// NAME:
	// 	getExposures
	//
	// DESCRIPTION
	// 	Generates a table listing total and ingested exposures using data
	//  from MOPS and IPP and places the table into the exposure div.
	//
	// PARAMETERS
	//	database: MOPS schema to interrogate.
	//  nn:				Observation night on which exposures were taken.
	function getExposures(database, nn) {
			$.get("exposures.html", {instance: database, nn: nn},
		    function(data) {
					$('#exposures').html(data);
				}, "html");
	}

});
</script>

<form id="expForm" action="update_chunk_status.html" method="post">
  <!-- Create hidden controls to store night number and database instance. -->
  <input name="instance" value="<% $instance %>" type="hidden" id="database"/>
  <input name="nn" value="<% $nn %>" type="hidden" id="nn"/>
	<input value="Mark chunks as ready" type="submit" id="update"/>
	</br></br>
  <table border=1 cellspacing=0 cellpadding=2>
    <tr>
      <th class="darkheader" scope="col" width="250">Chunk Name</th>
	  	<th class="darkheader" scope="col">Registered<br>in IPP</th>
  	  <th class="darkheader" scope="col">Ingested<br>into MOPS</th>
      <th class="darkheader" scope="col">Missing<br>exposures</th>
	  	<th class="darkheader" scope="col">Ready for<br>processing</th>
  	</tr>
% foreach $chunk (sort(keys(%chunks))) {
    <%perl>
      # Dim row if chunk has any status other than ingested.
      my $res = PS::MOPS::Admin::chunk_has_status($instance, $nn, $chunk, $FIELD_STATUS_INGESTED);
    </%perl>
	  <tr >
      <td align=center>
	      <span <% (!$res) ? 'style="opacity: .5"' : '' %>>
        	<% $chunk %>  <!-- Chunk Name -->
        </span>
      </td>
	    <td align=center>
      	<span <% (!$res) ? 'style="opacity: .5"' : '' %>>
		    	<!-- # of exposures downloaded to ipp -->
		      <% $chunks{$chunk}[0] %>  
        </span>
	    </td>
	    <td align=center>
      	<span <% (!$res) ? 'style="opacity: .5"' : '' %>>
	      	<!-- # of exposures ingested into mops -->
		      <% $chunks{$chunk}[1] %>  
        </span>
	    </td>
	    <td align=center> 
    	<%perl>
      	# Create url that will be used to show missing exposures. This URL  
        # will call the mason compnent missing_exposures.html with a list of 
        # missing exposures.
        my $url = "missing_exposures.html?";
        my $exp;
      
        # Generate the list of exposures to be passed.
        foreach $exp (@{$missing_exp_ref->{$chunk}}) {
            $url .= "exp=$exp&";
        }

        # Remove trailing ampersand from $url
        $url = substr($url, 0, -1);
      </%perl>

% if ($chunks{$chunk}[0] == $chunks{$chunk}[1]) {
    None
% } else {
	    <a target="_blank" href="<% $url %>"> <% $chunks{$chunk}[0] - $chunks{$chunk}[1] %> </a>
% }
	    </td>
      <td align="center">
    	<%perl>
      	# Show check box only if a field that makes up the chunks has a
      	# status of I.
     	  $res = PS::MOPS::Admin::chunk_has_status($instance, $nn, $chunk, $FIELD_STATUS_INGESTED);
    	</%perl>     
% if ($res) {        
      <input type="checkbox" name="chunks" value="<% $chunk %>" id="<% $chunk %>" autocomplete="off" <% ($chunks{$chunk}[0] == $chunks{$chunk}[1]) ? 'checked' : '' %> />
% }		
	    </td>
    </tr>
% }
	</table>
	</br>
	<input value="Mark chunks as ready" type="submit" id="update"/>
</form>

<script type="text/javascript">
$("#expForm").submit(
	function() {
		// Display downloading gif
		var ajax_load = "<img src='../images/ajax-loader.gif' alt='updating...' />"; 
		$("#exposures").html(ajax_load);

		// Update chunk status.
  	$.get(
			"update_chunk_status.html", 
			$(this).serialize(), 
			function(data) {
				$("#exposures").html(data);
			}, 
			"html"
		);
	
		// Cancel the actual form submit action by returning false.
		return false;
	}
);
</script>

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

<%init>
use PS::MOPS::Admin qw(:all);
use PS::MOPS::Constants qw($FIELD_STATUS_INGESTED);

# 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];
}
</%init>

<!-- Java script that will display a pop-up window with the missing exposures. -->
<script language="javascript">
function showMissingExp(url)
{
    var w;
    w = window.open(url,
            "Missing Exposures", 
            "width=400,height=250,status=no,location=no,menubar=no,toolbar=no,resizable=yes, scrollbars=1");
    w.focus();
}
</script>
