Index: trunk/ippTools/share/warptool_towarped.sql
===================================================================
--- trunk/ippTools/share/warptool_towarped.sql	(revision 28081)
+++ trunk/ippTools/share/warptool_towarped.sql	(revision 28082)
@@ -12,7 +12,8 @@
     workdir,
     magicked,
-    path_base
+    path_base,
+    priority
 FROM (
-    SELECT DISTINCT
+    (SELECT DISTINCT
         warpSkyCellMap.warp_id,
         warpImfile.warp_skyfile_id,
@@ -27,5 +28,6 @@
         warpRun.workdir,
         chipRun.magicked,
-        CAST(NULL AS CHAR(255)) AS path_base
+        CAST(NULL AS CHAR(255)) AS path_base,
+        IFNULL(Label.priority, 10000) AS priority
     FROM warpRun
     JOIN warpSkyCellMap
@@ -50,4 +52,5 @@
     LEFT JOIN warpMask
         ON warpRun.label = warpMask.label
+    LEFT JOIN Label ON warpRun.label = Label.label
     WHERE
         warpRun.state = 'new'
@@ -60,8 +63,10 @@
         AND warpMask.label IS NULL
         AND warpSkyCellMap.fault = 0
+        AND (Label.active OR Label.active IS NULL)
         -- where hook 1 %s
         -- limit hook 1 %s
+    )
 UNION
-    SELECT
+    (SELECT
         warpSkyCellMap.warp_id,
         warpImfile.warp_skyfile_id,
@@ -76,5 +81,6 @@
         warpRun.workdir,
         MIN(chipProcessedImfile.magicked) AS magicked,
-        warpSkyfile.path_base
+        warpSkyfile.path_base,
+        IFNULL(Label.priority, 10000) AS priority
     FROM warpRun
     JOIN warpImfile USING(warp_id)
@@ -86,4 +92,5 @@
     JOIN rawExp USING(exp_id)
     LEFT JOIN chipProcessedImfile USING(chip_id, class_id)
+    LEFT JOIN Label ON warpRun.label = Label.label
     WHERE warpRun.state = 'update'
         AND warpSkyfile.data_state = 'update'
@@ -94,8 +101,9 @@
         -- this blocks processing until all the chip inputs have been destreaked
         AND (warpSkyfile.magicked = 0 OR chipProcessedImfile.magicked >= 0)
+        AND (Label.active OR Label.active IS NULL)
         -- where hook 2 %s
     GROUP BY warp_id, skycell_id
     HAVING COUNT(warpSkyCellMap.class_id) = COUNT(chipProcessedImfile.class_id)
     -- limit hook 2 %s
+    )
 ) as towarped
-
Index: trunk/ippTools/src/warptool.c
===================================================================
--- trunk/ippTools/src/warptool.c	(revision 28081)
+++ trunk/ippTools/src/warptool.c	(revision 28082)
@@ -940,15 +940,12 @@
 
     // treat limit == 0 as "no limit"
-    psString limitString = psStringCopy("\n");
+    psString limitString = psStringCopy("\nORDER BY priority DESC, warp_id");
     if (limit) {
-        // We apply the limit to both sides of the UNION
-        // to avoid slow queries and to the query itself
-        // to satisfy the user's requested limit
+        // We apply the limit to both sides of the UNION to avoid slow queries
+        // and to the query itself to satisfy the user's requested limit
         psStringAppend(&limitString, "%s", psDBGenerateLimitSQL(limit));
         psStringAppend(&query, " %s", limitString);
     }
 
-#define TWO_WHERES
-#ifdef TWO_WHERES
     if (!p_psDBRunQueryF(config->dbh, query, whereStr, limitString, whereStr,  limitString)) {
         psError(PS_ERR_UNKNOWN, false, "database error");
@@ -956,11 +953,4 @@
         return false;
     }
-#else
-    if (!p_psDBRunQueryF(config->dbh, query, whereStr)) {
-        psError(PS_ERR_UNKNOWN, false, "database error");
-        psFree(query);
-        return false;
-    }
-#endif
     psFree(limitString);
     psFree(whereStr);
