Index: trunk/ippTools/share/Makefile.am
===================================================================
--- trunk/ippTools/share/Makefile.am	(revision 23497)
+++ trunk/ippTools/share/Makefile.am	(revision 23502)
@@ -76,4 +76,6 @@
      difftool_definebyquery_part2.sql \
      difftool_definebyquery_temp_create.sql \
+     difftool_definewarpwarp_select.sql \
+     difftool_definewarpwarp_insert.sql \
      difftool_donecleanup.sql \
      difftool_inputskyfile.sql \
Index: trunk/ippTools/share/difftool_definewarpwarp_insert.sql
===================================================================
--- trunk/ippTools/share/difftool_definewarpwarp_insert.sql	(revision 23502)
+++ trunk/ippTools/share/difftool_definewarpwarp_insert.sql	(revision 23502)
@@ -0,0 +1,16 @@
+INSERT INTO diffInputSkyfile
+SELECT
+    %s,                         -- diff_id
+    skycell_id,
+    %s,                         -- input warp_id
+    0,                          -- input stack_id
+    %s,                         -- template warp_id
+    0,                          -- template stack_id
+    tess_id,
+    0                           -- diff_skyfile_id
+FROM warpSkyfile AS inputWarpSkyfile
+JOIN warpSkyfile AS templateWarpSkyfile USING(skycell_id, tess_id)
+WHERE inputWarpSkyfile.ignored = 0
+    AND templateWarpSkyfile.ignored = 0
+    AND inputWarpSkyfile.warp_id = %s
+    AND templateWarpSkyfile.warp_id = %s
Index: trunk/ippTools/share/difftool_definewarpwarp_select.sql
===================================================================
--- trunk/ippTools/share/difftool_definewarpwarp_select.sql	(revision 23502)
+++ trunk/ippTools/share/difftool_definewarpwarp_select.sql	(revision 23502)
@@ -0,0 +1,25 @@
+SELECT
+    inputWarpRun.warp_id AS input_warp_id,
+    inputRawExp.exp_id AS input_exp_id,
+    inputWarpRun.tess_id AS tess_id,
+    SUBSTRING_INDEX(GROUP_CONCAT(templateWarpRun.warp_id ORDER BY ABS(ASIN(SQRT(POW(SIN(0.5*(inputRawExp.decl - templateRawExp.decl)),2) + COS(inputRawExp.decl) * COS(templateRawExp.decl) * POW(SIN(0.5*(inputRawExp.ra - templateRawExp.ra)),2))))), ',', 1) AS template_warp_id
+FROM warpRun AS inputWarpRun
+JOIN fakeRun AS inputFakeRun USING(fake_id)
+JOIN camRun AS inputCamRun USING(cam_id)
+JOIN chipRun AS inputChipRun USING(chip_id)
+JOIN rawExp AS inputRawExp USING(exp_id)
+-- To find exposures that haven't been diffed:%s LEFT JOIN diffRun ON diffRun.exp_id = inputRawExp.exp_id
+JOIN warpRun AS templateWarpRun
+    ON templateWarpRun.warp_id != inputWarpRun.warp_id -- Don't use self as template!
+    AND templateWarpRun.tess_id = inputWarpRun.tess_id -- Ensure using same tessellation
+JOIN fakeRun AS templateFakeRun
+    ON templateFakeRun.fake_id = templateWarpRun.fake_id
+JOIN camRun AS templateCamRun
+    ON templateCamRun.cam_id = templateFakeRun.cam_id
+JOIN chipRun AS templateChipRun
+    ON templateChipRun.chip_id = templateCamRun.chip_id
+JOIN rawExp AS templateRawExp
+    ON templateRawExp.exp_id = templateChipRun.exp_id
+    AND templateRawExp.filter = inputRawExp.filter
+-- WHERE hook %s
+GROUP BY input_id
