Index: trunk/ippTools/share/disttool_pending_raw.sql
===================================================================
--- trunk/ippTools/share/disttool_pending_raw.sql	(revision 30387)
+++ trunk/ippTools/share/disttool_pending_raw.sql	(revision 30653)
@@ -12,5 +12,6 @@
     TRIM(TRAILING '.fits' FROM rawImfile.uri) AS path_base,
     CAST(NULL AS CHAR(255)) AS alt_path_base,
-    chipProcessedImfile.path_base as chip_path_base,
+    -- pass camera stage path base since we want the camera mask file. The script knows what to do
+    camProcessedExp.path_base as chip_path_base,
     CAST(NULL AS CHAR(255)) AS state,
     CAST(NULL AS CHAR(255)) AS data_state,
@@ -26,4 +27,6 @@
 JOIN chipProcessedImfile
     USING(chip_id, class_id)
+JOIN camRun USING(chip_id)
+JOIN camProcessedExp using(cam_id)
 LEFT JOIN distComponent 
     ON distRun.dist_id = distComponent.dist_id 
@@ -36,4 +39,7 @@
     AND distComponent.dist_id IS NULL
     AND (rawExp.magicked OR distRun.no_magic)
+    -- need to have magicked the chip image which makes the camera mask
+    AND chipProcessedImfile.magicked != 0
+    AND camRun.magicked > 0 
     AND (Label.active OR Label.active IS NULL)
 UNION
@@ -52,5 +58,6 @@
     TRIM(TRAILING '.fits' FROM rawImfile.uri) AS path_base,
     magicDSFile.backup_path_base AS alt_path_base,
-    chipProcessedImfile.path_base AS chip_path_base,
+    -- pass camera stage path base since we want the camera mask file. The script knows what to do
+    camProcessedExp.path_base AS chip_path_base,
     CAST('full' AS CHAR(255)) AS state,
     CAST('full' AS CHAR(255)) AS data_state,
@@ -77,6 +84,6 @@
     AND distRun.clean = 0
     AND distComponent.dist_id IS NULL
-    AND chipProcessedImfile.data_state = 'full'
-    AND chipProcessedImfile.magicked > 0
+    AND chipProcessedImfile.magicked != 0
+    AND camRun.magicked > 0 
     AND (Label.active OR Label.active IS NULL)
 UNION
