Index: trunk/ippTools/share/Makefile.am
===================================================================
--- trunk/ippTools/share/Makefile.am	(revision 19492)
+++ trunk/ippTools/share/Makefile.am	(revision 19521)
@@ -146,4 +146,6 @@
      stacktool_sumskyfile.sql \
      stacktool_tosum.sql \
+     warptool_change_skyfile_data_state.sql \
+     warptool_change_run_state.sql \
      warptool_definebyquery.sql \
      warptool_donecleanup.sql \
@@ -156,3 +158,4 @@
      warptool_tooverlap.sql \
      warptool_towarped.sql \
+     warptool_updateskyfile.sql \
      warptool_warped.sql
Index: trunk/ippTools/share/warptool_change_run_state.sql
===================================================================
--- trunk/ippTools/share/warptool_change_run_state.sql	(revision 19521)
+++ trunk/ippTools/share/warptool_change_run_state.sql	(revision 19521)
@@ -0,0 +1,15 @@
+-- change state of warpRun from goto_cleaned to cleaned or goto_purged to purged
+-- when all of the consituant skyfiles are in the end state
+-- arguments are new state (cleaned or purged) warp_id and new state again for 
+-- the chipProcessedImfile sub query
+UPDATE warpRun
+    SET state = '%s'
+    WHERE
+    warpRun.warp_id = %lld
+    AND (SELECT
+        COUNT(warp_id)
+        FROM warpSkyfile
+        WHERE
+            warpSkyfile.warp_id = warpRun.warp_id
+            AND data_state != '%s'
+        ) = 0
Index: trunk/ippTools/share/warptool_change_skyfile_data_state.sql
===================================================================
--- trunk/ippTools/share/warptool_change_skyfile_data_state.sql	(revision 19521)
+++ trunk/ippTools/share/warptool_change_skyfile_data_state.sql	(revision 19521)
@@ -0,0 +1,15 @@
+-- handle changes in warpSkyfile.data_state.
+-- Used for the modes tocleanedskyfile and topurgedskyfile
+-- args are new data_state, warp_id, skycell_id and current expected state for warpRun
+UPDATE warpSkyfile
+    SET 
+    data_state = '%s'
+WHERE
+    warp_id = %lld
+    AND skycell_id = '%s'
+    -- only update if chipRun.state has the expected value
+    AND (
+        SELECT state from warpRun where warpRun.warp_id = warpSkyfile.warp_id
+    ) = '%s'
+    
+
Index: trunk/ippTools/share/warptool_pendingcleanuprun.sql
===================================================================
--- trunk/ippTools/share/warptool_pendingcleanuprun.sql	(revision 19492)
+++ trunk/ippTools/share/warptool_pendingcleanuprun.sql	(revision 19521)
@@ -13,3 +13,3 @@
 USING (exp_id)
 WHERE
-    warpRun.state = 'goto_cleaned'
+    (warpRun.state = 'goto_cleaned' OR warpRun.state = 'goto_purged')
Index: trunk/ippTools/share/warptool_pendingcleanupskyfile.sql
===================================================================
--- trunk/ippTools/share/warptool_pendingcleanupskyfile.sql	(revision 19492)
+++ trunk/ippTools/share/warptool_pendingcleanupskyfile.sql	(revision 19521)
@@ -10,3 +10,5 @@
     USING(warp_id)
 WHERE
-    warpRun.state = 'goto_cleaned'
+    (warpRun.state = 'goto_cleaned' AND warpSkyfile.data_state = 'full')
+    OR
+    (warpRun.state = 'goto_purged' AND warpSkyfile.data_state != 'purged')
Index: trunk/ippTools/share/warptool_updateskyfile.sql
===================================================================
--- trunk/ippTools/share/warptool_updateskyfile.sql	(revision 19521)
+++ trunk/ippTools/share/warptool_updateskyfile.sql	(revision 19521)
@@ -0,0 +1,1 @@
+UPDATE warpSkyfile SET fault = %d WHERE warp_id = %lld AND skycell_id = '%s'
