Index: trunk/ippTools/share/magictool_definebyquery.sql
===================================================================
--- trunk/ippTools/share/magictool_definebyquery.sql	(revision 18663)
+++ trunk/ippTools/share/magictool_definebyquery.sql	(revision 18668)
@@ -10,5 +10,5 @@
 --------------------------------------------------------------------
 
-
+-- magictool_definebyquery_temp_create.sql
 CREATE TEMPORARY TABLE magicBestDiffs (
 exp_id BIGINT,
@@ -20,4 +20,5 @@
 
 
+-- magictool_definebyquery_temp_insert.sql
 -- List of best differences for each exposure
 INSERT INTO magicBestDiffs
@@ -41,7 +42,6 @@
 JOIN diffSkyfile USING(diff_id)
 WHERE
-    warpSkyfile.good_frac > 0.2 -- XXX Must update!
-    AND diffSkyfile.good_frac > 0.2 -- XXX Must update!
-    AND diffSkyfile.fault = 0
+    diffSkyfile.fault = 0
+-- magictool_definebyquery_temp_insert_groupby.sql
 GROUP BY
     exp_id,
@@ -50,5 +50,7 @@
 
 
--- Get list of exposures ready for magic
+-- magictool_definebyquery_select_part1.sql
+-- This is part 1 of 2 of a query to get a list of exposures on which magic may be performed
+-- After this follows magictool_definebyquery_select_part2.sql
 SELECT
     *
@@ -69,5 +71,9 @@
         warpSkyfile.ignored = 0
         AND warpRun.state = 'stop'
-        AND warpSkyfile.good_frac > 0.2 -- XXX Must update!
+    -- INSERT HERE any additional restrictions (e.g., exp_id, warpSkyfile.good_frac)
+-- INSERT HERE magictool_definebyquery_select_part2.sql
+-- magictool_definebyquery_select_part2.sql
+-- This is part 2 of 2 of a query to get a list of exposures on which magic may be performed
+-- This follows magictool_definebyquery_select_part1.sql
     GROUP BY
         exp_id
@@ -82,16 +88,16 @@
         exp_id
     ) AS magicDiffNums USING(exp_id)
+LEFT JOIN magicRun USING(exp_id)
 WHERE
     num_done = num_todo
-;
+    AND magicRun.magic_id IS NULL
 
 
+-- magictool_definebyquery_insert.sql
 -- Insert the best list of diffs as magic inputs
 INSERT INTO magicInputSkyfile
 SELECT
-    12345,
+    @MAGIC_ID@, -- Update this with the appropriate magic_id
     diff_id,
     CONCAT(tess_id, '.', skycell_id) AS node
 FROM magicBestDiffs
-WHERE
-    exp_id = 3;
