Index: trunk/ippScripts/scripts/nightly_science.pl
===================================================================
--- trunk/ippScripts/scripts/nightly_science.pl	(revision 30280)
+++ trunk/ippScripts/scripts/nightly_science.pl	(revision 30293)
@@ -278,4 +278,5 @@
 
 $metadata_out{nsObservingState} = &get_observing_state($date);
+$metadata_out{nsRegistrationState} = &get_registration_state($date);
 #
 # Mode selection
@@ -1100,4 +1101,12 @@
     my $Nqueued = 0;
     my $is_processing = 0;
+    my $is_registering;
+    if ($metadata_out{nsRegistrationState} eq 'REGISTERED') {
+	$is_registering = 0;
+    }
+    else {
+	$is_registering = 1;
+    }
+
     foreach my $target (sort (keys %science_config)) {
 	if ($science_config{$target}{STACKABLE} == 1) {
@@ -1161,5 +1170,5 @@
     $metadata_out{nsStackPotential} = $Npotential;
     $metadata_out{nsStackQueued}    = $Nqueued;
-    if (($Npotential == $Nqueued)&&($metadata_out{nsObservingState} eq 'END_OF_NIGHT')&&($is_processing == 0)) {
+    if (($Npotential == $Nqueued)&&($metadata_out{nsObservingState} eq 'END_OF_NIGHT')&&($is_processing == 0)&&($is_registering == 0)) {
 	$metadata_out{nsStackState} = 'FINISHED_STACKS';
     }	
@@ -1294,4 +1303,12 @@
     my $Nnoexp     = 0;
     my $is_processing = 0;
+    my $is_registering;
+    if ($metadata_out{nsRegistrationState} eq 'REGISTERED') {
+	$is_registering = 0;
+    }
+    else {
+	$is_registering = 1;
+    }
+
     foreach my $target (sort (keys %science_config)) {
 	if ($science_config{$target}{DIFFABLE} == 1) {
@@ -1334,8 +1351,31 @@
         }
     }
-    if (($Npotential == $Nnoexp)&&($metadata_out{nsObservingState} eq 'END_OF_NIGHT')&&($is_processing == 0)) {
-	$metadata_out{nsDiffState} = 'FINISHED_DIFFS';
-    }	
-
+    if ($metadata_out{nsObservingState} eq 'END_OF_NIGHT') {
+	if ($is_processing == 0) {
+	    $metadata_out{nsDiffState} = 'DIFFING';
+	}
+	elsif ($is_registering == 0) {
+	    if ($Npotential == $Nnoexp) {
+		$metadata_out{nsDiffState} = 'FINISHED_DIFFS';
+	    }
+	    elsif ($metadata_out{nsDiffPotential} == $metadata_out{nsDiffQueued}) {
+		$metadata_out{nsDiffState} = 'FINISHED_DIFFS';
+	    }
+	}
+	else {
+	    $metadata_out{nsDiffState} = 'DIFFING';
+	}
+    }
+    else {
+	$metadata_out{nsDiffState} = 'DIFFING';
+    }
+
+#     if (($Npotential == $Nnoexp)&&($metadata_out{nsObservingState} eq 'END_OF_NIGHT')&&($is_processing == 0)) {
+# 	$metadata_out{nsDiffState} = 'FINISHED_DIFFS';
+#     }	
+
+#     if ($is_processing == 1) {
+# 	$metadata_out{nsDiffState} = 'DIFFING';
+#     }
 }
 
@@ -1461,7 +1501,7 @@
     $metadata_out{nsDiffPotential} += $Npotential;
     $metadata_out{nsDiffQueued}    += $Nqueued;
-    if (($metadata_out{nsDiffPotential} == $metadata_out{nsDiffQueued})&&($metadata_out{nsObservingState} eq 'END_OF_NIGHT')) {
-	$metadata_out{nsDiffState} = 'FINISHED_DIFFS';
-    }	
+#      if (($metadata_out{nsDiffPotential} == $metadata_out{nsDiffQueued})&&($metadata_out{nsObservingState} eq 'END_OF_NIGHT')) {
+#  	$metadata_out{nsDiffState} = 'FINISHED_DIFFS';
+#      }	
 
 }
@@ -1580,5 +1620,5 @@
 	}
     }	
-
+#    print "$datetime $eon_dt\n";
     if (DateTime->compare($datetime,$eon_dt) < 1) {
 	return("OBSERVING");
@@ -1588,4 +1628,33 @@
     }
 }
+
+# This basically does the end of night check, but does it "the hard way," to prevent the time from fooling us.
+sub get_registration_state {
+    my $date = shift;
+
+    foreach my $eon (keys %eon_config) {
+	my $command = "$regtool -processedexp -simple ";
+	$command .= " -dbname $dbname ";
+	$command .= " -dateobs_begin ${date}T00:00:00 -dateobs_end ${date}T23:59:59 ";
+	$command .= " -object $eon_config{$eon}{OBJECT} " if defined($eon_config{$eon}{OBJECT});
+	$command .= " -obs_mode $eon_config{$eon}{OBSMODE} " if defined($eon_config{$eon}{OBSMODE});
+	$command .= " -exp_type $eon_config{$eon}{EXPTYPE} " if defined($eon_config{$eon}{EXPTYPE});
+	$command .= " -comment $eon_config{$eon}{COMMENT} " if defined($eon_config{$eon}{COMMENT});
+
+	my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+	    run ( command => $command, verbose => $verbose );
+	unless ($success) {
+	    $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
+	    &my_die("Unable to perform regtool: $error_code", 0,0,$date, $PS_EXIT_SYS_ERROR);
+	}
+	my @eon_exposures = split /\n/, (join '', @$stdout_buf);
+	if ($#eon_exposures >= 0) {
+	    return("REGISTERED");
+	}
+    }	
+    return("NOT_REGISTERED");
+}
+
+    
 
 sub get_tool_parameters {
