Index: /branches/eam_branches/ipp-20140610/ippTools/share/Makefile.am
===================================================================
--- /branches/eam_branches/ipp-20140610/ippTools/share/Makefile.am	(revision 37025)
+++ /branches/eam_branches/ipp-20140610/ippTools/share/Makefile.am	(revision 37026)
@@ -508,3 +508,4 @@
 	remotetool_definebyquery_warp.sql \
 	remotetool_definebyquery_stack.sql \
+	remotetool_dropcomponent.sql \
 	remotetool_updatepoll.sql
Index: /branches/eam_branches/ipp-20140610/ippTools/share/remotetool_definebyquery_cam.sql
===================================================================
--- /branches/eam_branches/ipp-20140610/ippTools/share/remotetool_definebyquery_cam.sql	(revision 37025)
+++ /branches/eam_branches/ipp-20140610/ippTools/share/remotetool_definebyquery_cam.sql	(revision 37026)
@@ -1,12 +1,21 @@
-SELECT 
-       cam_id AS stage_id
-FROM camRun 
-       LEFT JOIN remoteComponent ON (remoteComponent.stage_id = camRun.cam_id) 
-       LEFT JOIN remoteRun ON (
-       	    remoteComponent.remote_id = remoteRun.remote_id AND 
-	    remoteRun.label = camRun.label
-	    )
+SELECT stage_id FROM
+
+( SELECT cam_id AS stage_id
+    FROM camRun
 WHERE
-camRun.state = 'new' AND
-(remoteRun.stage = 'camera' OR remoteRun.state IS NULL) AND
-remoteComponent.remote_id IS NULL
+camRun.state = 'new'
+-- where hook %s
+GROUP BY cam_id
+) AS W
+
+LEFT JOIN
+
+(SELECT remote_id,remoteRun.stage,remoteRun.label,remoteComponent.stage_id,remoteRun.state 
+   FROM remoteRun 
+   JOIN remoteComponent USING (remote_id)
+WHERE
+remoteRun.stage = 'camera'
+-- where hook %s
+) AS R
+USING (stage_id)
+WHERE R.remote_id IS NULL
Index: /branches/eam_branches/ipp-20140610/ippTools/share/remotetool_definebyquery_chip.sql
===================================================================
--- /branches/eam_branches/ipp-20140610/ippTools/share/remotetool_definebyquery_chip.sql	(revision 37025)
+++ /branches/eam_branches/ipp-20140610/ippTools/share/remotetool_definebyquery_chip.sql	(revision 37026)
@@ -1,14 +1,24 @@
-SELECT 
-       chip_id AS stage_id
-FROM chipRun 
-       LEFT JOIN remoteComponent ON (remoteComponent.stage_id = chipRun.chip_id) 
-       LEFT JOIN remoteRun ON (
-       	    remoteComponent.remote_id = remoteRun.remote_id AND 
-	    remoteRun.label = chipRun.label
-	    )
+SELECT stage_id FROM
+
+( SELECT chip_id AS stage_id
+    FROM chipRun
 WHERE
-chipRun.state = 'new' AND
-(remoteRun.stage = 'chip' OR remoteRun.stage IS NULL) AND
-remoteComponent.remote_id IS NULL
+chipRun.state = 'new'
+-- where hook %s
+GROUP BY chip_id
+) AS W
+
+LEFT JOIN
+
+(SELECT remote_id,remoteRun.stage,remoteRun.label,remoteComponent.stage_id,remoteRun.state 
+   FROM remoteRun 
+   JOIN remoteComponent USING (remote_id)
+WHERE
+remoteRun.stage = 'chip'
+-- where hook %s
+) AS R
+USING (stage_id)
+WHERE R.remote_id IS NULL
+
 
 
Index: /branches/eam_branches/ipp-20140610/ippTools/share/remotetool_definebyquery_stack.sql
===================================================================
--- /branches/eam_branches/ipp-20140610/ippTools/share/remotetool_definebyquery_stack.sql	(revision 37025)
+++ /branches/eam_branches/ipp-20140610/ippTools/share/remotetool_definebyquery_stack.sql	(revision 37026)
@@ -1,12 +1,21 @@
-SELECT 
-       stack_id AS stage_id
-FROM stackRun 
-       LEFT JOIN remoteComponent ON (remoteComponent.stage_id = stackRun.stack_id) 
-       LEFT JOIN remoteRun ON (
-       	    remoteComponent.remote_id = remoteRun.remote_id AND 
-	    remoteRun.label = stackRun.label
-	    )
+SELECT stage_id FROM
+
+( SELECT stack_id AS stage_id
+    FROM stackRun
 WHERE
-stackRun.state = 'new' AND
-(remoteRun.stage = 'stack' OR remoteRun.state IS NULL) AND
-remoteComponent.remote_id IS NULL
+stackRun.state = 'new'
+-- where hook %s
+GROUP BY stack_id
+) AS W
+
+LEFT JOIN
+
+(SELECT remote_id,remoteRun.stage,remoteRun.label,remoteComponent.stage_id,remoteRun.state 
+   FROM remoteRun 
+   JOIN remoteComponent USING (remote_id)
+WHERE
+remoteRun.stage = 'stack'
+-- where hook %s
+) AS R
+USING (stage_id)
+WHERE R.remote_id IS NULL
Index: /branches/eam_branches/ipp-20140610/ippTools/share/remotetool_definebyquery_warp.sql
===================================================================
--- /branches/eam_branches/ipp-20140610/ippTools/share/remotetool_definebyquery_warp.sql	(revision 37025)
+++ /branches/eam_branches/ipp-20140610/ippTools/share/remotetool_definebyquery_warp.sql	(revision 37026)
@@ -1,15 +1,24 @@
-SELECT warp_id AS stage_id FROM (
-SELECT warp_id, count(warpSkyCellMap.skycell_id) AS N
+SELECT stage_id FROM
+
+( SELECT warp_id AS stage_id, count(warpSkyCellMap.skycell_id) AS N
     FROM warpRun
     LEFT JOIN warpSkyCellMap USING(warp_id)
-    LEFT JOIN remoteComponent ON (remoteComponent.stage_id = warpRun.warp_id)
-    LEFT JOIN remoteRun ON (remoteComponent.remote_id = remoteRun.remote_id
-                        AND remoteRun.label = warpRun.label             )
-WHERE warpRun.state = 'new'
-AND (remoteRun.stage = 'warp' OR remoteRun.state IS NULL)
-AND remoteComponent.remote_id IS NULL
+WHERE
+warpRun.state = 'new'
 -- where hook %s
 GROUP BY warp_id
-) AS warpTry
-WHERE N != 0
+HAVING N != 0
+) AS W
 
+LEFT JOIN
+
+(SELECT remote_id,remoteRun.stage,remoteRun.label,remoteComponent.stage_id,remoteRun.state 
+   FROM remoteRun 
+   JOIN remoteComponent USING (remote_id)
+WHERE
+remoteRun.stage = 'warp'
+-- where hook %s
+) AS R
+USING (stage_id)
+WHERE R.remote_id IS NULL
+
Index: /branches/eam_branches/ipp-20140610/ippTools/share/remotetool_dropcomponent.sql
===================================================================
--- /branches/eam_branches/ipp-20140610/ippTools/share/remotetool_dropcomponent.sql	(revision 37026)
+++ /branches/eam_branches/ipp-20140610/ippTools/share/remotetool_dropcomponent.sql	(revision 37026)
@@ -0,0 +1,4 @@
+DELETE FROM remoteComponent
+WHERE 
+remote_id = %ld AND
+stage_id  = %ld
Index: /branches/eam_branches/ipp-20140610/ippTools/src/camtoolConfig.c
===================================================================
--- /branches/eam_branches/ipp-20140610/ippTools/src/camtoolConfig.c	(revision 37025)
+++ /branches/eam_branches/ipp-20140610/ippTools/src/camtoolConfig.c	(revision 37026)
@@ -205,5 +205,5 @@
     psMetadataAddF32(addprocessedexpArgs, PS_LIST_TAIL, "-deteff_inst_uq", 0, "define deteff_uq", NAN);
     psMetadataAddS16(addprocessedexpArgs, PS_LIST_TAIL, "-background_model", 0, "set background_model value", 0);
-    psMetadataAddS64(addprocessedexpArgs, PS_LIST_TAIL, "-astrom_chips", 0, "chips with successful astrom", -1);
+    psMetadataAddS64(addprocessedexpArgs, PS_LIST_TAIL, "-astrom_chips", 0, "chips with successful astrom", 0);
 
     // -processedexp
Index: /branches/eam_branches/ipp-20140610/ippTools/src/remotetool.c
===================================================================
--- /branches/eam_branches/ipp-20140610/ippTools/src/remotetool.c	(revision 37025)
+++ /branches/eam_branches/ipp-20140610/ippTools/src/remotetool.c	(revision 37026)
@@ -23,4 +23,5 @@
 
 // Update
+static bool dropcomponentMode(pxConfig *config);
 static bool updaterunMode(pxConfig *config);
 static bool updatepollMode(pxConfig *config);
@@ -49,4 +50,5 @@
     MODECASE(REMOTETOOL_MODE_LISTRUN,       listrunMode);
     MODECASE(REMOTETOOL_MODE_LISTCOMPONENT, listcomponentMode);
+    MODECASE(REMOTETOOL_MODE_DROPCOMPONENT, dropcomponentMode);
     MODECASE(REMOTETOOL_MODE_UPDATERUN,     updaterunMode);
     MODECASE(REMOTETOOL_MODE_UPDATEPOLL,    updatepollMode);
@@ -94,4 +96,5 @@
 
   // Get a list of the things we can insert into a new component
+  // CZW: these labels no longer need to be linked to the table, as I've split stageRun and remoteRun into sub-queries.
   psString query = NULL;
   psString whereOption = psStringCopy("");
@@ -104,5 +107,5 @@
     
     if (label) {
-      psStringAppend(&query, " AND (chipRun.label = '%s')", label);
+      psStringAppend(&whereOption, "\n AND (label = '%s')", label);
     }
   }
@@ -115,5 +118,5 @@
     
     if (label) {
-      psStringAppend(&query, " AND (camRun.label = '%s')", label);
+      psStringAppend(&whereOption, "\n AND (label = '%s')", label);
     }
   }    
@@ -128,5 +131,5 @@
       // Because warp has to check to see if something other than the state is set correctly, I have to use this wonky way to pass the label in.
       //      psStringAppend(&query, " AND (warpRun.label = '%s')", label);
-      psStringAppend(&whereOption, "\n AND (warpRun.label = '%s')", label);
+      psStringAppend(&whereOption, "\n AND (label = '%s')", label);
     }
   }    
@@ -139,5 +142,5 @@
     
     if (label) {
-      psStringAppend(&query, " AND (stackRun.label = '%s')", label);
+      psStringAppend(&whereOption, "\n AND (label = '%s')", label);
     }
   }    
@@ -152,5 +155,5 @@
     psFree(limitString);
   }
-  if (!p_psDBRunQueryF(config->dbh, query,whereOption)) {
+  if (!p_psDBRunQueryF(config->dbh, query,whereOption,whereOption)) { // This needs to be here twice because we need to restrict on label twice.
     psError(PS_ERR_UNKNOWN, false, "database error");
     psFree(query);
@@ -313,4 +316,41 @@
 // Update
 
+static bool dropcomponentMode(pxConfig *config)
+{
+  // Assert that we have a unique component to operate on.
+  PXOPT_LOOKUP_S64(remote_id,config->args, "-remote_id",true, false);
+  psAssert(remote_id,"This must exist for this mode to be safe.");
+  PXOPT_LOOKUP_S64(stage_id, config->args, "-stage_id", true, false);
+  psAssert(stage_id,"This must exist for this mode to be safe.");
+
+  psString query = pxDataGet("remotetool_dropcomponent.sql");
+  if (!query) {
+    psError(PXTOOLS_ERR_SYS, false, "Failed to retreive SQL statement");
+    return false;
+  }
+  
+  if (!psDBTransaction(config->dbh)) {
+    psError(PS_ERR_UNKNOWN, false, "database error");
+    return false;
+  }
+
+  if (!p_psDBRunQueryF(config->dbh, query, remote_id, stage_id)) {
+    psError(PS_ERR_UNKNOWN, false, "Database error");
+    psFree(query);
+    return false;
+  }
+  psFree(query);
+  
+  
+
+  // point of no return
+  if (!psDBCommit(config->dbh)) {
+    psError(PS_ERR_UNKNOWN, false, "database error");
+    return false;
+  }
+  
+  return true;
+}
+
 static bool updaterunMode(pxConfig *config)
 {
Index: /branches/eam_branches/ipp-20140610/ippTools/src/remotetool.h
===================================================================
--- /branches/eam_branches/ipp-20140610/ippTools/src/remotetool.h	(revision 37025)
+++ /branches/eam_branches/ipp-20140610/ippTools/src/remotetool.h	(revision 37026)
@@ -13,4 +13,5 @@
   REMOTETOOL_MODE_LISTRUN,
   REMOTETOOL_MODE_LISTCOMPONENT,
+  REMOTETOOL_MODE_DROPCOMPONENT,
   REMOTETOOL_MODE_UPDATERUN,
   REMOTETOOL_MODE_UPDATEPOLL,
Index: /branches/eam_branches/ipp-20140610/ippTools/src/remotetoolConfig.c
===================================================================
--- /branches/eam_branches/ipp-20140610/ippTools/src/remotetoolConfig.c	(revision 37025)
+++ /branches/eam_branches/ipp-20140610/ippTools/src/remotetoolConfig.c	(revision 37026)
@@ -64,4 +64,10 @@
   ADD_OPT(S16, listcomponentArgs, "-limit",              "limit to number of runs to display", 0);
   ADD_OPT(Bool, listcomponentArgs, "-simple",            "simple print format", false);
+
+  // -dropcomponent
+  psMetadata *dropcomponentArgs = psMetadataAlloc();
+  ADD_OPT(S64, dropcomponentArgs, "-remote_id",         "run to update", 0);
+  ADD_OPT(S64, dropcomponentArgs, "-stage_id",          "stage_id to remote", 0);
+  
   // -updaterun
   psMetadata *updaterunArgs = psMetadataAlloc();
@@ -95,4 +101,5 @@
   PXOPT_ADD_MODE("-listrun",           "", REMOTETOOL_MODE_LISTRUN,        listrunArgs);
   PXOPT_ADD_MODE("-listcomponent",     "", REMOTETOOL_MODE_LISTCOMPONENT,  listcomponentArgs);
+  PXOPT_ADD_MODE("-dropcomponent",     "", REMOTETOOL_MODE_DROPCOMPONENT,  dropcomponentArgs);
   PXOPT_ADD_MODE("-updaterun",         "", REMOTETOOL_MODE_UPDATERUN,      updaterunArgs);
   PXOPT_ADD_MODE("-updatepoll",        "", REMOTETOOL_MODE_UPDATEPOLL,     updatepollArgs);
