Index: trunk/ippScripts/scripts/automate_stacks.pl
===================================================================
--- trunk/ippScripts/scripts/automate_stacks.pl	(revision 28899)
+++ trunk/ippScripts/scripts/automate_stacks.pl	(revision 28916)
@@ -132,4 +132,5 @@
 my %stackable_list = ();
 my %reduction_class = ();
+my @unrecoverable_quality = ();
 my @detrend_list = ();
 my %dettype_list = ();
@@ -177,4 +178,7 @@
     elsif (${ $entry }{name} eq 'FILTERS') {
         push @filter_list, ${ $entry }{value};
+    }
+    elsif (${ $entry }{name} eq 'UNRECOVERABLE_QUALITY') {
+        push @unrecoverable_quality, ${ $entry }{value};
     }
     elsif (${ $entry }{name} eq 'TARGETS') {
@@ -1062,5 +1066,5 @@
     my ($label,$workdir,$obs_mode,$object,$comment,$tess_id,$dist_group,$data_group,$reduction) = get_tool_parameters($date,$target);
 
-    my $sth = "SELECT skycell_id from stackRun where data_group = '$data_group' AND skycell_id = '$skycell' AND filter = '$filter'";
+    my $sth = "SELECT skycell_id from stackRun where data_group = '$data_group' AND skycell_id = '$skycell' AND filter = '$filter' AND tess_id = '$tess_id'";
     my $data_ref = $db->selectall_arrayref( $sth );
 
@@ -1080,8 +1084,17 @@
 
     my $where = " label = '$label' AND data_group = '$data_group' ";
+
+    my $where_possibly_faulted = $where . " AND ( " ;
+    foreach my $acceptable_quality (@unrecoverable_quality) {
+	$where_possibly_faulted .= " quality = $acceptable_quality OR ";
+    }
+    $where_possibly_faulted .= " 0 )";
+
     my $chip_sth = "SELECT * from chipRun WHERE (state = 'full' OR state = 'new') AND $where ";
+    my $cam_sth  = "SELECT * from camRun JOIN camProcessedExp USING(cam_id) WHERE state = 'full' AND $where_possibly_faulted ";
     my $warp_sth = "SELECT * from warpRun WHERE state = 'full' AND $where ";
 
     my $chip_ref = $db->selectall_arrayref( $chip_sth );
+    my $cam_ref  = $db->selectall_arrayref( $cam_sth );
     my $warp_ref = $db->selectall_arrayref( $warp_sth );
 
@@ -1104,5 +1117,5 @@
     }
 
-    return($#input_exposures  + 1, $#{ $chip_ref } + 1, $#{ $warp_ref } + 1, $Nalready);
+    return($#input_exposures  + 1, $#{ $chip_ref } + 1, $#{ $cam_ref } + $#{ $warp_ref } + 2, $Nalready);
 }
 
