Index: /branches/czw_branch/20110406/ippScripts/scripts/diff_skycell.pl
===================================================================
--- /branches/czw_branch/20110406/ippScripts/scripts/diff_skycell.pl	(revision 31428)
+++ /branches/czw_branch/20110406/ippScripts/scripts/diff_skycell.pl	(revision 31429)
@@ -31,4 +31,5 @@
 my $ppSub = can_run('ppSub') or (warn "Can't find ppSub" and $missing_tools = 1);
 my $ppStatsFromMetadata = can_run('ppStatsFromMetadata') or (warn "Can't find ppStatsFromMetadata" and $missing_tools = 1);
+my $nebInsert = can_run('neb-insert') or (warn "Can't find neb-insert" and $missing_tools = 1);
 if ($missing_tools) {
     warn("Can't find required tools.");
@@ -383,4 +384,17 @@
                 }
             }
+	    if ($reduction eq 'NOCONVDIFF') {
+		my $refConv = prepare_output("PPSUB.REF.CONV", $outroot, 0);
+		my $templateFile = $ipprc->file_resolve($template,0);
+		$command = "$nebInsert $refConv $templateFile";
+		( $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 neb-insert: $error_code", $diff_id, $skycell_id, $error_code);
+		}
+	    }
+
+
         } elsif ($run_state eq 'update') {
             &my_die("Update resulted in poor quality image: $quality", $diff_id, $skycell_id, $PS_EXIT_SYS_ERROR);
Index: /branches/czw_branch/20110406/ippScripts/scripts/lap_science.pl
===================================================================
--- /branches/czw_branch/20110406/ippScripts/scripts/lap_science.pl	(revision 31428)
+++ /branches/czw_branch/20110406/ippScripts/scripts/lap_science.pl	(revision 31429)
@@ -82,5 +82,5 @@
 }
 if (defined($cleanup_mode)) {
-    unless (($lapRunInfo{state} eq 'done')||
+    unless (($lapRunInfo{state} eq 'full')||
 	    ($lapRunInfo{state} eq 'drop')) {
 	&my_die("Cannot run cleanup_mode if lapRun != done!", $lap_id);
@@ -356,8 +356,19 @@
 	    $complete_qstacks++;
 	}
+	elsif (($stack->{quick_state})&&($stack->{quick_state} eq 'new')&&
+	       ($stack->{quick_fault} >= 4)&&($stack->{quick_fault} != 32767)) {
+	    printf STDERR "Faulted quick stack: $stack->{quick_stack_id} $stack->{quick_fault}\n";
+	    $complete_qstacks++;# This is not the best solution, but if they continually fail, there's not much we can do.
+	}
 	if (($stack->{final_state})&&
 	    ($stack->{final_state} eq 'full')) {
 	    $complete_fstacks++;
 	}
+	elsif (($stack->{final_state})&&($stack->{final_state} eq 'new')&&
+	       ($stack->{final_fault} >= 4)&&($stack->{final_fault} != 32767)) {
+	    printf STDERR "Faulted final stack: $stack->{final_stack_id} $stack->{final_fault}\n";
+	    $complete_fstacks++; # This is not the best solution, but if they continually fail, there's not much we can do.
+	}
+
     }
     if (($complete_qstacks > 0)&&
@@ -433,15 +444,29 @@
 	    $exposure = remake_this_exposure($exposure);
 	}
-	if ($companion) { # Validate that there are no problems with the companion exposure
-	    if ($companion->{cam_quality}) { # Maybe other things here?
-		$exposure->{private} = 1;
-		$exposure->{data_state} = 'drop';
-		&update_this_exposure($exposure);
-		$needs_qstack = 1;
+	if ($exposure->{cam_quality}) {
+	    $needs_qstack = 1;
+	    $needs_something_private = 1;
+	    if ($companion) {
+		$companion->{private} = 1;
+		$companion->{pairwise} = 0;
+		&update_this_exposure($companion);
 	    }
-	}
-	if (($exposure->{warpRun_state})&&  # This exposure has a warp
-	    (($exposure->{warpRun_state} eq 'full')|| # This exposure's warp is either done or impossible.
-	     ($exposure->{data_state} eq 'drop'))) {
+	    $exposure->{private} = 1;
+	    $exposure->{pairwise} = 0;
+	    $exposure->{data_state} = 'drop';
+	    &update_this_exposure($exposure);
+
+	}
+# 	if ($companion) { # Validate that there are no problems with the companion exposure
+# 	    if ($companion->{cam_quality}) { # Maybe other things here?
+# 		$exposure->{private} = 1;
+# 		$exposure->{data_state} = 'drop';
+# 		&update_this_exposure($exposure);
+# 		$needs_qstack = 1;
+# 	    }
+# 	}
+	if ((($exposure->{warpRun_state})&&  # This exposure has a warp
+	    ($exposure->{warpRun_state} eq 'full'))|| # This exposure's warp is either done or impossible.
+	     ($exposure->{data_state} eq 'drop')) {
 	    $can_qstack ++;
 	    $can_diff ++;
@@ -535,5 +560,6 @@
     my $data_group = "${label}.${date}";
 
-    my $command = "$stacktool -pretend";
+    my $command = "$stacktool ";
+    $command .= " -pretend " if defined $debug;
     $command .= " -dbname $dbname " if defined $dbname;
     $command .= " -definebyquery -select_label $label -select_skycell_id ${proj_cell}.% -select_filter $filter ";
@@ -567,6 +593,8 @@
     my $sass_id = $stack->{sass_id};
     unless (defined($sass_id)) {
-	&my_die("Unable to parse metadata from laptool", $lap_id, "");
-    }
+	&my_die("Unable to parse metadata from stacktool for sass_id", $lap_id, "");
+    }
+
+    print "QUICK_SASS_ID: $sass_id\n";
     $command = "$laptool -updaterun -lap_id $lap_id -set_quick_sass_id $sass_id";
     $command .= " -dbname $dbname " if defined $dbname;
@@ -635,5 +663,5 @@
     my $sass_id = $stack->{sass_id};
     unless (defined($sass_id)) {
-	&my_die("Unable to parse metadata from laptool", $lap_id, "");
+	&my_die("Unable to parse metadata from stacktool for sass_id", $lap_id, "");
     }
     print "FINAL_SASS_ID: $sass_id\n";
@@ -667,5 +695,5 @@
 	    next;
 	}
-	if ($exposure->{diff_id}&&S64_IS_NOT_NULL($exposure->{diff_id})) { # Not sure how this would happen, but still.
+	if ($exposure->{diff_id}&&S64_IS_NOT_NULL($exposure->{diff_id})) { # Not sure how this would happen, but still. ## This happens when we inherit a complete exposure.
 	    next;
 	}
@@ -705,5 +733,5 @@
 	}
 	else { # warp-qstack
-	    $command .= " -definewarpstack -set_reduction WARPQSTACK -available -good_frac 0.2 ";
+	    $command .= " -definewarpstack -set_reduction NOCONVDIFF -available -good_frac 0.2 ";
 	    $command .= " -warp_id $exposure->{warp_id} -stack_label ${label}.quick ";
 	    $already_queued{$exposure->{warp_id}} = 1;
@@ -716,5 +744,14 @@
 	    &my_die("unable to perform difftool: $error_code", $exposure->{lap_id}, $exposure->{proj_cell});
 	}
-
+	
+	my $diffs = $mdcParser->parse_list(join "", @$stdout_buf) or
+	    &my_die("Unable to parse metadata from laptool", $lap_id, "");
+	
+	my $diff = ${ $diffs }[0];
+	my $diff_id = $diff->{diff_id};
+	unless (defined($diff_id)) {
+	    $exposure->{data_state} = 'drop';
+	    &update_this_exposure($exposure);
+	}
 	
     }
@@ -735,60 +772,53 @@
 ################################################################################
 
-# sub cleanup_exposures {
-#     my $command = "$laptool -inactive ";
-#     $command .= " -dbname $dbname " if defined $dbname;
-
-#     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 laptool: $error_code", "none", "none");
-#     }
-#     if (@$stdout_buf == 0) {
-# 	# Nothing to do.
-# 	return(0);
-#     }
-    
-#     my $exposures = $mdcParser->parse_list(join "", @$stdout_buf) or
-# 	&my_die("Unable to parse metadata from laptool", $lap_id);
-
-#     foreach my $exposure (@$exposures) {
-# 	foreach my $clean_mode (@clean_modes) {
-# 	    $command = $clean_mode->{COMMAND};
-# 	    $clean_id = $clean_mode->{CLEAN_ID};
-# 	    $clean_state = $clean_mode->{CLEAN_STATE};
-# 	    if (($exposure->{$clean_id} != 0)&&
-# 		(($exposure->{$clean_state} eq 'full')||
-# 		 ($exposure->{$clean_state} eq 'drop'))) {
-
-# 		$command =~ s/\@DBNAME\@/$dbname/g;
-# 		$command =~ s/\@CHIP_ID\@/$exposure->{chip_id}/;
-# 		$command =~ s/\@CHIP_STATE\@/$exposure->{chip_state}/;
-# 		$command =~ s/\@CHIP_LABEL\@/$exposure->{chip_label}/;
-		
-# 		$command =~ s/\@WARP_ID\@/$exposure->{chip_id}/;
-# 		$command =~ s/\@WARP_STATE\@/$exposure->{chip_state}/;
-# 		$command =~ s/\@WARP_LABEL\@/$exposure->{chip_label}/;
-		
-# 		$command =~ s/\@DIFF_ID\@/$exposure->{diff_id}/;
-# 		$command =~ s/\@DIFF_STATE\@/$exposure->{diff_state}/;
-# 		$command =~ s/\@DIFF_LABEL\@/$exposure->{diff_label}/;
-		
-# 		$command =~ s/\@MAGICDS_ID\@/$exposure->{magicDS_id}/;
-# 		$command =~ s/\@MAGICDS_STATE\@/$exposure->{magicDS_state}/;
-# 		$command =~ s/\@MAGICDS_LABEL\@/$exposure->{magicDS_label}/;
-		
-# 		($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 laptool: $error_code", "none", "none");
-# 		}
-# 	    }
-# 	}
-# 	$exposure->{data_state} = 'cleaned';
-# 	update_this_exposure($exposure);
-#     }
-# }
+sub cleanup_mode {
+    my $lap_id = shift;
+    my $command = "$laptool -inactiveexp -lap_id $lap_id ";
+    $command .= " -dbname $dbname " if defined $dbname;
+
+    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 laptool: $error_code", "none", "none");
+    }
+    if (@$stdout_buf == 0) {
+	# Nothing to do.
+	return(0);
+    }
+    
+    my $exposures = $mdcParser->parse_list(join "", @$stdout_buf) or
+	&my_die("Unable to parse metadata from laptool", $lap_id);
+
+    my @clean_modes = (
+	'chiptool -dbname @DBNAME@ -updaterun -set_state goto_cleaned -state full -set_label goto_cleaned -label @LABEL@ -chip_id @CHIP_ID@',
+	'warptool -dbname @DBNAME@ -updaterun -set_state goto_cleaned -state full -set_label goto_cleaned -label @LABEL@ -warp_id @WARP_ID@',
+	'difftool -dbname @DBNAME@ -updaterun -set_state goto_cleaned -state full -set_label goto_cleaned -label @LABEL@ -diff_id @DIFF_ID@',
+	'magicdstool -dbname @DBNAME@ -updaterun -set_state goto_cleaned -state full -set_label goto_cleaned -stage chip -stage_id @CHIP_ID@',
+	'magicdstool -dbname @DBNAME@ -updaterun -set_state goto_cleaned -state full -set_label goto_cleaned -stage warp -stage_id @WARP_ID@',
+	'magicdstool -dbname @DBNAME@ -updaterun -set_state goto_cleaned -state full -set_label goto_cleaned -stage diff -stage_id @DIFF_ID@');
+    foreach my $exposure (@$exposures) {
+	if ($exposure->{is_in_use}) {
+	    next;
+	}
+ 	foreach my $clean_mode (@clean_modes) {
+ 	    my $command = $clean_mode;
+	    $command =~ s/\@DBNAME\@/$dbname/g;
+	    $command =~ s/\@CHIP_ID\@/$exposure->{chip_id}/;
+	    $command =~ s/\@WARP_ID\@/$exposure->{warp_id}/;
+	    $command =~ s/\@DIFF_ID\@/$exposure->{diff_id}/;
+	    $command =~ s/\@LABEL\@/$exposure->{label}/;
+	    
+	    ($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 laptool: $error_code", "none", "none");
+	    }
+ 	}
+ 	$exposure->{data_state} = 'cleaned';
+ 	update_this_exposure($exposure);
+    }
+}
     
 	    
Index: /branches/czw_branch/20110406/ippTools/share/laptool_inactiveexp.sql
===================================================================
--- /branches/czw_branch/20110406/ippTools/share/laptool_inactiveexp.sql	(revision 31428)
+++ /branches/czw_branch/20110406/ippTools/share/laptool_inactiveexp.sql	(revision 31429)
@@ -1,4 +1,4 @@
 SELECT DISTINCT 
-    D.*,diffRun.state AS diff_state,CONVERT(sum(others.active),SIGNED) AS is_in_use FROM (
+    D.*,diffRun.state AS diff_state,coalesce(CONVERT(sum(others.active),SIGNED),0) AS is_in_use FROM (
   SELECT DISTINCT 
       W.*,IFNULL(diff1.diff_id,diff2.diff_id) AS diff_id FROM (
@@ -27,4 +27,8 @@
     WHERE lapExp.active = FALSE
     AND @WHERE@
+    AND (warpSkyfile.quality IS NULL OR
+         (warpSkyfile.quality != 8007      -- known cases where quality != 0, but everything's fine.
+          AND warpSkyfile.quality != 3006  -- known cases where quality != 0, but everything's fine.
+          ))
     GROUP BY lap_id,exp_id
     ) AS W
@@ -34,4 +38,4 @@
 ) AS D
 LEFT JOIN diffRun USING(diff_id)
-JOIN lapExp AS others ON (D.chip_id = others.chip_id AND D.lap_id != others.lap_id)
+LEFT JOIN lapExp AS others ON (D.chip_id = others.chip_id AND D.lap_id != others.lap_id)
 GROUP BY lap_id,exp_id
Index: /branches/czw_branch/20110406/ippTools/src/laptool.c
===================================================================
--- /branches/czw_branch/20110406/ippTools/src/laptool.c	(revision 31428)
+++ /branches/czw_branch/20110406/ippTools/src/laptool.c	(revision 31429)
@@ -473,5 +473,4 @@
       }
     }
-
   }
 
@@ -690,4 +689,5 @@
   PXOPT_LOOKUP_S64(set_chip_id, config->args, "-set_chip_id", false, false);
   PXOPT_LOOKUP_S64(set_pair_id, config->args, "-set_pair_id", false, false);
+  PXOPT_LOOKUP_STR(set_data_state, config->args, "-set_data_state", false, false);
   PXOPT_LOOKUP_BOOL(private,    config->args, "-private",     false);
   PXOPT_LOOKUP_BOOL(public,     config->args, "-public",      false);
@@ -711,5 +711,5 @@
   PXOPT_COPY_S64(config->args, where, "-exp_id", "exp_id", "==");
   PXOPT_COPY_S64(config->args, where, "-chip_id", "chip_id", "==");
-  PXOPT_COPY_STR(config->args, where, "-set_data_state", "data_state", "==");
+
 
   psMetadata *values = psMetadataAlloc();
@@ -738,5 +738,7 @@
     psMetadataAddBool(values, PS_LIST_TAIL, "pairwise", 0, "==", false);
   }
-  
+  if (set_data_state) {
+    PXOPT_COPY_STR(config->args, values, "-set_data_state", "data_state", "==");
+  }
   long rows = psDBUpdateRows(config->dbh,"lapExp",where,values);
   if (rows) {
@@ -755,5 +757,6 @@
 {
   PS_ASSERT_PTR_NON_NULL(config, false);
-
+  PXOPT_LOOKUP_S64(lap_id,          config->args, "-lap_id",          true, false);
+  
   psMetadata *where = psMetadataAlloc();
   PXOPT_COPY_S64(config->args, where, "-lap_id", "lap_id", "==");
