Index: trunk/ippScripts/scripts/nightly_science.pl
===================================================================
--- trunk/ippScripts/scripts/nightly_science.pl	(revision 32968)
+++ trunk/ippScripts/scripts/nightly_science.pl	(revision 33371)
@@ -1415,4 +1415,10 @@
 		}
 	    }
+	    if ((defined($science_config{$target}{SELF_WSDIFFS})) && ($science_config{$target}{SELF_WSDIFFS} == 1)) {
+		foreach my $filter (@filter_list) {
+		    # This one needs to return a state to see if we need to wait on stacking before checking again.
+		    $metadata_out{nsDiffState} = self_WS_diff_queue($date,$target,$filter,$pretend);
+		}
+	    }
 	}
     }
@@ -1707,31 +1713,87 @@
 
     my ($lunation_start,$lunation_end) = get_lunation_extent($date);
-    
+
+
+# Get a list of exposures that could be diffed    
     my $new_data_group = "${data_group}.offnight";
     my $new_dist_group = "${dist_group}.offnight";
-    my $cmd = "$difftool -dbname $dbname  -definewarpwarp ";
-    $cmd .= "-input_label $label  -template_label $label ";
-    $cmd .= "-set_workdir $workdir  -set_dist_group $new_dist_group  -set_data_group $new_data_group ";
-    $cmd .= " -mintimediff 40000 ";
-    $cmd .= " -simple  -set_label $label -filter $filter ";
-    $cmd .= " -dateobs_begin ${lunation_start}T00:00:00 -dateobs_end ${lunation_end}T23:59:59 -distance 1.5 ";
-
-    if (defined($pretend)) {
-	$cmd .= ' -pretend ';
-    }
-    if (defined($debug)) {
-	print STDERR "ON_diffs wants to run this command: $cmd\n";
-    }
-    
-    if (($debug == 0)&&(!defined($pretend))) {
-	my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
-	    run ( command => $cmd, verbose => $verbose );
-	unless ($success) {
-	    $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
-	    &my_die("Unable to perform difftool: $error_code", 0,0,$date, $PS_EXIT_SYS_ERROR);
-	}
-    }
-}
+    my $check_cmd = "$difftool -dbname $dbname  -definewarpwarp ";
+    $check_cmd .= "-input_label $label  -template_label $label ";
+    $check_cmd .= "-set_workdir $workdir  -set_dist_group $new_dist_group  -set_data_group $new_data_group ";
+    $check_cmd .= " -mintimediff 40000 ";
+    $check_cmd .= " -pretend -simple  -rerun -set_label $label -filter $filter ";
+    $check_cmd .= " -dateobs_begin ${lunation_start}T00:00:00 -dateobs_end ${lunation_end}T23:59:59 -distance 1.5 ";
+
+    my ($success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) = 
+	run ( command => $check_cmd, verbose => $verbose );
+    unless ($success) {
+	$error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
+	&my_die("Unable to perform difftool: $error_code", 0,0,$date, $PS_EXIT_SYS_ERROR);
+    }
+    
+    # Parse results
+    my $diffs = $mdcParser->parse_list(join "", @$stdout_buf) or
+	&my_die("Unable to parse metadata from difftool -definewarpwarp", 0, 0, $date, $PS_EXIT_PROG_ERROR);
+    foreach my $diff (@$diffs) {
+	unless (multi_date_verify_uniqueness_diff($diff->{input_warp_id},$diff->{template_warp_id},$date,$target)) {
+	    # If we don't already have a diff with these inputs, make a diff with these inputs.
+	    my $cmd = "$difftool -dbname $dbname -definewarpwarp ";
+	    $cmd   .= "-input_label $label -template_label $label ";
+	    $cmd   .= "-warp_id $diff->{input_warp_id} -template_warp_id $diff->{template_warp_id} ";
+	    $cmd .= "-set_workdir $workdir  -set_dist_group $new_dist_group  -set_data_group $new_data_group ";
+	    $cmd .= " -mintimediff 40000 ";
+	    $cmd .= " -simple  -rerun -set_label $label -filter $filter ";
+	    $cmd .= " -dateobs_begin ${lunation_start}T00:00:00 -dateobs_end ${lunation_end}T23:59:59 -distance 1.5 ";
+
+	    if (defined($pretend)) {
+		$cmd .= ' -pretend ';
+	    }
+	    print STDERR "ON_diffs wants to run this command: $cmd\n";
 	    
+	    if (($debug == 0)&&(!defined($pretend))) {
+		my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+		    run ( command => $cmd, verbose => $verbose );
+		unless ($success) {
+		    $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
+		    &my_die("Unable to perform difftool: $error_code", 0,0,$date, $PS_EXIT_SYS_ERROR);
+		}
+	    }
+	}
+    }
+}
+
+sub intranight_WS_diff_queue {
+    my $date = shift;
+    my $target = shift;
+    my $filter = shift;
+    my $pretend = shift;
+    my ($label,$workdir,$obs_mode,$object,$comment,$tess_id,$dist_group,$data_group,$reduction) = get_tool_parameters($date,$target);
+    my $db = init_gpc_db();
+
+    my ($stacks_to_finish,$stacks_total) = check_stack_count($label);
+    if (($stacks_to_finish == 0)&&($stacks_total != 0)) {
+	my $cmd = "$difftool -dbname $dbname -definewarpstack ";
+	$cmd .= " -good_frac 0.2 ";
+	$cmd .= " -warp_label $label -stack_label $label -set_label $label ";
+	$cmd .= " -set_workdir $workdir -available -set_reduction WARPSTACK -set_dist_group $dist_group ";
+	$cmd .= " -rerun ";
+    }
+}	    
+
+sub check_stack_count {
+    my $label = shift;
+
+    my $stacks_to_finish = 0;
+    my $stacks_total = 0;
+
+    my $sth = "SELECT DISTINCT stack_id from stackRun where label = '$label' AND state = 'new'";
+    my $data_ref = $db->selectall_arrayref( $sth );
+    $stacks_to_finish = $#{ $data_ref } + 1;
+
+    $sth = "SELECT DISTINCT stack_id from stackRun where label = '$label'";
+    $data_ref = $db->selectall_arrayref( $sth );
+    $stacks_total = $#{ $data_ref } + 1;
+    return($stacks_to_finish,$stacks_total);
+}
 
 
