Index: trunk/ippTools/share/Makefile.am
===================================================================
--- trunk/ippTools/share/Makefile.am	(revision 24789)
+++ trunk/ippTools/share/Makefile.am	(revision 24882)
@@ -154,4 +154,9 @@
      magictool_revertnode.sql \
      magicdstool_completed_runs.sql \
+     magicdstool_definebyquery_raw.sql \
+     magicdstool_definebyquery_chip.sql \
+     magicdstool_definebyquery_camera.sql \
+     magicdstool_definebyquery_warp.sql \
+     magicdstool_definebyquery_diff.sql \
      magicdstool_getrunids.sql \
      magicdstool_getskycells.sql \
Index: trunk/ippTools/share/magicdstool_definebyquery_camera.sql
===================================================================
--- trunk/ippTools/share/magicdstool_definebyquery_camera.sql	(revision 24882)
+++ trunk/ippTools/share/magicdstool_definebyquery_camera.sql	(revision 24882)
@@ -0,0 +1,27 @@
+SELECT DISTINCT       -- DISTINCT because we are going though diffInputSkyfile
+    magicRun.magic_id,
+    exp_id,
+    'camera' AS stage,
+    camRun.cam_id AS stage_id,
+    camRun.cam_id,
+    magicRun.label,
+    magicRun.workdir
+FROM magicRun
+    JOIN diffRun USING(diff_id)
+    JOIN diffInputSkyfile USING(diff_id)
+    JOIN warpRun ON (!magicRun.inverse AND warp1 = warp_id) 
+                 OR ( magicRun.inverse AND warp2 = warp_id)
+    JOIN fakeRun USING(fake_id)
+    JOIN camRun USING(cam_id)
+    JOIN chipRun USING(chip_id, exp_id)
+    JOIN rawExp USING(exp_id)
+    LEFT JOIN magicDSRun ON magicRun.magic_id = magicDSRun.magic_id
+                         AND magicDSRun.stage  = 'camera'
+WHERE magicRun.state = 'full'
+    AND ( -- rerun HOOK magicdstool sends "\n 1 " if rerun else "\n 0 " %s
+        OR magicDSRun.magic_ds_id IS NULL)
+    AND camRun.magicked  = 0
+    AND chipRun.magicked
+    AND chipRun.state = 'full'
+    AND camRun.state = 'full'
+
Index: trunk/ippTools/share/magicdstool_definebyquery_chip.sql
===================================================================
--- trunk/ippTools/share/magicdstool_definebyquery_chip.sql	(revision 24882)
+++ trunk/ippTools/share/magicdstool_definebyquery_chip.sql	(revision 24882)
@@ -0,0 +1,26 @@
+SELECT DISTINCT       -- DISTINCT because we are going though diffInputSkyfile
+    magicRun.magic_id,
+    exp_id,
+    'chip' AS stage,
+    chip_id AS stage_id,
+    camRun.cam_id,
+    magicRun.label,
+    magicRun.workdir
+FROM magicRun
+    JOIN diffRun USING(diff_id)
+    JOIN diffInputSkyfile USING(diff_id)
+    JOIN warpRun ON (!magicRun.inverse AND warp1 = warp_id) 
+                 OR ( magicRun.inverse AND warp2 = warp_id)
+    JOIN fakeRun USING(fake_id)
+    JOIN camRun USING(cam_id)
+    JOIN chipRun USING(chip_id, exp_id)
+    JOIN rawExp USING(exp_id)
+    LEFT JOIN magicDSRun ON magicRun.magic_id = magicDSRun.magic_id
+                         AND magicDSRun.stage  = 'chip'
+WHERE magicRun.state = 'full'
+    AND ( -- rerun HOOK magicdstool sends "\n1 " if rerun else "\n0 " %s
+        OR magicDSRun.magic_ds_id IS NULL)
+    AND chipRun.magicked  = 0
+    AND chipRun.state = 'full'
+    AND camRun.state = 'full'
+
Index: trunk/ippTools/share/magicdstool_definebyquery_diff.sql
===================================================================
--- trunk/ippTools/share/magicdstool_definebyquery_diff.sql	(revision 24882)
+++ trunk/ippTools/share/magicdstool_definebyquery_diff.sql	(revision 24882)
@@ -0,0 +1,18 @@
+SELECT
+    magicRun.magic_id,
+    exp_id,
+    'diff' AS stage,
+    diff_id AS stage_id,
+    0 AS cam_id,
+    magicRun.label,
+    magicRun.workdir
+FROM magicRun
+    JOIN diffRun USING(diff_id)
+    LEFT JOIN magicDSRun ON magicRun.magic_id = magicDSRun.magic_id
+                         AND magicDSRun.stage  = 'diff'
+WHERE magicRun.state = 'full'
+    AND ( -- rerun HOOK magicdstool sends "\n1 " if rerun else "\n0 " %s
+        OR magicDSRun.magic_ds_id IS NULL)
+    AND diffRun.magicked  = 0
+    AND diffRun.state = 'full'
+
Index: trunk/ippTools/share/magicdstool_definebyquery_raw.sql
===================================================================
--- trunk/ippTools/share/magicdstool_definebyquery_raw.sql	(revision 24882)
+++ trunk/ippTools/share/magicdstool_definebyquery_raw.sql	(revision 24882)
@@ -0,0 +1,25 @@
+SELECT DISTINCT       -- DISTINCT because we are going though diffInputSkyfile
+    magicRun.magic_id,
+    exp_id,
+    'raw' AS stage,
+    exp_id AS stage_id,
+    0 AS cam_id,
+    magicRun.label,
+    magicRun.workdir
+FROM magicRun
+    JOIN diffRun USING(diff_id)
+    JOIN diffInputSkyfile USING(diff_id)
+    JOIN warpRun ON (!magicRun.inverse AND warp1 = warp_id) 
+                 OR ( magicRun.inverse AND warp2 = warp_id)
+    JOIN fakeRun USING(fake_id)
+    JOIN camRun USING(cam_id)
+    JOIN chipRun USING(chip_id, exp_id)
+    JOIN rawExp USING(exp_id)
+    LEFT JOIN magicDSRun ON magicRun.magic_id = magicDSRun.magic_id
+                         AND magicDSRun.stage_id = exp_id
+                         AND magicDSRun.stage  = 'raw'
+WHERE magicRun.state = 'full'
+    AND ( -- rerun HOOK magicdstool sends "\n1 " if rerun else "\n0 " %s
+        OR magicDSRun.magic_ds_id IS NULL)
+    AND rawExp.magicked  = 0
+
Index: trunk/ippTools/share/magicdstool_definebyquery_warp.sql
===================================================================
--- trunk/ippTools/share/magicdstool_definebyquery_warp.sql	(revision 24882)
+++ trunk/ippTools/share/magicdstool_definebyquery_warp.sql	(revision 24882)
@@ -0,0 +1,25 @@
+SELECT DISTINCT       -- DISTINCT because we are going though diffInputSkyfile
+    magicRun.magic_id,
+    exp_id,
+    'warp' AS stage,
+    warp_id AS stage_id,
+    0 AS cam_id,
+    magicRun.label,
+    magicRun.workdir
+FROM magicRun
+    JOIN diffRun USING(diff_id)
+    JOIN diffInputSkyfile USING(diff_id)
+    JOIN warpRun ON (!magicRun.inverse AND warp1 = warp_id) 
+                 OR ( magicRun.inverse AND warp2 = warp_id)
+    JOIN fakeRun USING(fake_id)
+    JOIN camRun USING(cam_id)
+    JOIN chipRun USING(chip_id, exp_id)
+    JOIN rawExp USING(exp_id)
+    LEFT JOIN magicDSRun ON magicRun.magic_id = magicDSRun.magic_id
+                         AND magicDSRun.stage  = 'warp'
+WHERE magicRun.state = 'full'
+    AND ( -- rerun HOOK magicdstool sends "\n1 " if rerun else "\n0 " %s
+        OR magicDSRun.magic_ds_id IS NULL)
+    AND warpRun.magicked  = 0
+    AND warpRun.state = 'full'
+
Index: trunk/ippTools/share/magicdstool_getrunids.sql
===================================================================
--- trunk/ippTools/share/magicdstool_getrunids.sql	(revision 24789)
+++ trunk/ippTools/share/magicdstool_getrunids.sql	(revision 24882)
@@ -5,5 +5,6 @@
     chip_id,
     cam_id,
-    warp_id 
+    warp_id,
+    diffSkyfile.diff_id
 FROM magicRun 
 JOIN magicInputSkyfile USING(magic_id)
Index: trunk/ippTools/share/magicdstool_torevert_diff.sql
===================================================================
--- trunk/ippTools/share/magicdstool_torevert_diff.sql	(revision 24789)
+++ trunk/ippTools/share/magicdstool_torevert_diff.sql	(revision 24882)
@@ -9,5 +9,5 @@
     outroot,
     diffSkyfile.path_base,
-    NULL AS cam_path_base,
+    "NULL" AS cam_path_base,
     0 AS bytes,
     0 AS md5sum
