Index: trunk/ippTools/share/bgtool_advancechip.sql
===================================================================
--- trunk/ippTools/share/bgtool_advancechip.sql	(revision 35572)
+++ trunk/ippTools/share/bgtool_advancechip.sql	(revision 35672)
@@ -3,11 +3,19 @@
     chipBackgroundImfile.magicked
 FROM chipBackgroundRun
-JOIN chipProcessedImfile USING(chip_id)
-LEFT JOIN chipBackgroundImfile USING(chip_bg_id, class_id)
+JOIN camRun using(cam_id)
+JOIN chipProcessedImfile ON chipBackgroundRun.chip_id = chipProcessedImfile.chip_id
+JOIN chipRun AS bgsub_chipRun on camRun.chip_id = bgsub_chipRun.chip_id
+JOIN chipProcessedImfile AS bgsub_chipProcessedImfile 
+    ON (bgsub_chipRun.chip_id = bgsub_chipProcessedImfile.chip_id 
+        AND bgsub_chipProcessedImfile.class_id = chipProcessedImfile.class_id)
+LEFT JOIN chipBackgroundImfile 
+    ON (chipBackgroundRun.chip_bg_id = chipBackgroundImfile.chip_bg_id
+        AND chipBackgroundImfile.class_id = chipProcessedImfile.class_id)
 WHERE chipBackgroundRun.state = 'new'
     AND chipProcessedImfile.quality = 0
     AND chipProcessedImfile.fault = 0
+    AND bgsub_chipProcessedImfile.quality = 0
 -- WHERE hook %s
-GROUP BY chip_bg_id
+GROUP BY chipBackgroundRun.chip_bg_id
 HAVING
     COUNT(chipBackgroundImfile.class_id) = COUNT(chipProcessedImfile.class_id)
Index: trunk/ippTools/share/bgtool_definewarp.sql
===================================================================
--- trunk/ippTools/share/bgtool_definewarp.sql	(revision 35572)
+++ trunk/ippTools/share/bgtool_definewarp.sql	(revision 35672)
@@ -15,5 +15,5 @@
 JOIN chipBackgroundRun ON chipRun.chip_id = chipBackgroundRun.chip_id 
     AND (chipBackgroundRun.cam_id = camRun.cam_id OR chipBackgroundRun.cam_id = 0)
-LEFT JOIN warpBackgroundRun ON chipBackgroundRun.chip_bg_id = warpBackgroundRun.warp_bg_id -- label hook %s
+LEFT JOIN warpBackgroundRun ON chipBackgroundRun.chip_bg_id = warpBackgroundRun.chip_bg_id -- label hook %s
 WHERE chipBackgroundRun.state = 'full'
     AND warpRun.state IN ('full', 'cleaned', 'goto_cleaned') -- need warp to have completed so warpSkyCellMap is populated
Index: trunk/ippTools/share/bgtool_tochip.sql
===================================================================
--- trunk/ippTools/share/bgtool_tochip.sql	(revision 35572)
+++ trunk/ippTools/share/bgtool_tochip.sql	(revision 35672)
@@ -1,5 +1,5 @@
 SELECT
     chipBackgroundRun.*,
-    class_id,
+    chipProcessedImfile.class_id,
     rawExp.exp_tag,
     rawExp.camera
@@ -8,5 +8,12 @@
 JOIN rawExp USING(exp_id)
 JOIN chipProcessedImfile USING(chip_id)
-LEFT JOIN chipBackgroundImfile USING(chip_bg_id, class_id)
+JOIN camRun using(cam_id)
+JOIN chipRun AS bgsub_chipRun on camRun.chip_id = bgsub_chipRun.chip_id
+JOIN chipProcessedImfile AS bgsub_chipProcessedImfile 
+    ON (bgsub_chipRun.chip_id = bgsub_chipProcessedImfile.chip_id 
+        AND bgsub_chipProcessedImfile.class_id = chipProcessedImfile.class_id)
+LEFT JOIN chipBackgroundImfile  
+    ON (chipBackgroundRun.chip_bg_id = chipBackgroundImfile.chip_bg_id 
+        AND chipBackgroundImfile.class_id = chipProcessedImfile.class_id)
 LEFT JOIN Label ON chipBackgroundRun.label = Label.label
 WHERE chipBackgroundImfile.chip_bg_id IS NULL
@@ -14,4 +21,5 @@
     AND chipProcessedImfile.fault = 0
     AND chipProcessedImfile.quality = 0
+    AND bgsub_chipProcessedImfile.quality = 0
     AND (Label.active OR Label.active IS NULL)
 -- WHERE hook %s
