Index: trunk/ippTools/src/addtool.c
===================================================================
--- trunk/ippTools/src/addtool.c	(revision 31301)
+++ trunk/ippTools/src/addtool.c	(revision 31375)
@@ -119,9 +119,26 @@
     pxcamGetSearchArgs (config, where);
     PXOPT_COPY_S64(config->args, where,  "-cam_id",    "camRun.cam_id", "==");
+    PXOPT_COPY_S64(config->args, where,  "-stack_id",    "stackRun.stack_id", "==");
+ PXOPT_COPY_S64(config->args, where,  "-sky_id",    "staticskyRun.sky_id", "==");
+    PXOPT_LOOKUP_STR(stage,       config->args, "-stage", false, false);
+    if (strcmp(stage, "cam")== 0) {
+
     pxAddLabelSearchArgs (config, where, "-label",     "camRun.label", "=="); // define using camRun label
     pxAddLabelSearchArgs (config, where, "-data_group","camRun.data_group", "=="); // define using camRun label
     PXOPT_COPY_STR(config->args, where,  "-reduction", "camRun.reduction", "==");
-
-
+    }
+    if (strcmp(stage, "stack")== 0) {
+
+    pxAddLabelSearchArgs (config, where, "-label",     "stackRun.label", "=="); // define using camRun label
+    pxAddLabelSearchArgs (config, where, "-data_group","stackRun.data_group", "=="); // define using camRun label
+    PXOPT_COPY_STR(config->args, where,  "-reduction", "stackRun.reduction", "==");
+    }
+    if (strcmp(stage, "staticsky")== 0) {
+
+    pxAddLabelSearchArgs (config, where, "-label",     "staticskyRun.label", "=="); // define using camRun label
+    pxAddLabelSearchArgs (config, where, "-data_group","staticskyRun.data_group", "=="); // define using camRun label
+    PXOPT_COPY_STR(config->args, where,  "-reduction", "staticskyyRun.reduction", "==");
+    }
+    
     if (!psListLength(where->list)) {
         psFree(where);
@@ -129,5 +146,5 @@
         return false;
     }
-
+    // PXOPT_LOOKUP_STR(stage,       config->args, "-stage", false, false);
     PXOPT_LOOKUP_STR(workdir,     config->args, "-set_workdir", false, false);
     PXOPT_LOOKUP_STR(dvodb,       config->args, "-set_dvodb", false, false);
@@ -148,6 +165,8 @@
     psString dvodb_string = NULL;
     psString bare_query = NULL;
+
+    if (strcmp(stage,"cam") == 0 ) {
     if (dvodb) {
-      psTrace("addtool.c", PS_LOG_INFO, "dvodb argument found (%s) using addtool_find_cam_id_dvo.sql\n", dvodb);
+      psTrace("addtool.c", PS_LOG_INFO, "dvodb argument found (%s) using addtool_find_cam_id_dvo.sql\n%s\n", dvodb,stage);
         // find the cam_id of all the exposures that we want to queue up.
         bare_query = pxDataGet("addtool_find_cam_id_dvo.sql");
@@ -155,5 +174,5 @@
 	psStringAppend(&dvodb_string, "addRun.dvodb = '%s'", dvodb);
     } else {
-        psTrace("addtool.c", PS_LOG_INFO, "dvodb argument not found using addtool_find_cam_id.sql\n");
+      psTrace("addtool.c", PS_LOG_INFO, "dvodb argument not found using addtool_find_cam_id.sql\n%s\n",stage);
         // find the cam_id of all the exposures that we want to queue up.
         bare_query = pxDataGet("addtool_find_cam_id.sql");
@@ -161,4 +180,38 @@
         psStringAppend(&dvodb_string, "(camRun.dvodb IS NOT NULL AND previous_dvodb = camRun.dvodb)");
     }
+    }
+    if (strcmp(stage,"stack") == 0) {
+    if (dvodb ) {
+      psTrace("addtool.c", PS_LOG_INFO, "dvodb argument found (%s) using addtool_find_stack_id_dvo.sql\n%s\n", dvodb,stage);
+        // find the cam_id of all the exposures that we want to queue up.
+        bare_query = pxDataGet("addtool_find_stack_id_dvo.sql");
+	// user supplied dvodb
+	psStringAppend(&dvodb_string, "addRun.dvodb = '%s'", dvodb);
+    } else {
+      psTrace("addtool.c", PS_LOG_INFO, "dvodb argument not found using addtool_find_stack_id.sql\n%s\n",stage);
+        // find the cam_id of all the exposures that we want to queue up.
+        bare_query = pxDataGet("addtool_find_stack_id.sql");
+        // inherit dvodb from camRun, avoid matching NULL
+        psStringAppend(&dvodb_string, "(stackRun.dvodb IS NOT NULL AND previous_dvodb = stackRun.dvodb)");
+    }
+    }
+    if (strcmp(stage,"staticsky") == 0) {
+    if (dvodb ) {
+      psTrace("addtool.c", PS_LOG_INFO, "dvodb argument found (%s) using addtool_find_sky_id_dvo.sql\n%s\n", dvodb,stage);
+        // find the cam_id of all the exposures that we want to queue up.
+        bare_query = pxDataGet("addtool_find_sky_id_dvo.sql");
+	// user supplied dvodb
+	psStringAppend(&dvodb_string, "addRun.dvodb = '%s'", dvodb);
+    } else {
+      psTrace("addtool.c", PS_LOG_INFO, "dvodb argument not found using addtool_find_sky_id.sql\n%s\n",stage);
+        // find the cam_id of all the exposures that we want to queue up.
+        bare_query = pxDataGet("addtool_find_sky_id.sql");
+        // inherit dvodb from camRun, avoid matching NULL
+        psStringAppend(&dvodb_string, "(staticskyRun.dvodb IS NOT NULL AND previous_dvodb = staticskyRun.dvodb)");
+    }
+    }
+
+
+
     if (!bare_query) {
         psError(PXTOOLS_ERR_SYS, false, "failed to retrieve SQL statement");
@@ -184,9 +237,17 @@
 
     if (destreaked) {
+      if (strcmp(stage,"cam") == 0) {
         psStringAppend(&query, " AND (camRun.magicked > 0)");
-    }
+      }
+      if (strcmp(stage,"stack") == 0) {
+	psStringAppend(&query, " AND (stackRun.magicked > 0)");
+      }
+      // staticSky has no magicked column.
+    }
+
+    psTrace("addtool.c", PS_LOG_INFO,"query: \n\n%s\n\n",query);
 
     if (!p_psDBRunQuery(config->dbh, query)) {
-        psError(PS_ERR_UNKNOWN, false, "database error");
+      psError(PS_ERR_UNKNOWN, false, "database error, \n%s\n", query);
         psFree(query);
         return false;
@@ -217,8 +278,10 @@
 
     // loop over our list of camRun rows to check the supplied and selected dvodb and workdir values:
+    if (strcmp(stage,"cam") == 0) {
     for (long i = 0; i < psArrayLength(output); i++) {
         psMetadata *md = output->data[i];
 
-        camRunRow *row = camRunObjectFromMetadata(md);
+	camRunRow *row = camRunObjectFromMetadata(md);
+	
         if (!row) {
             psError(PS_ERR_UNKNOWN, false, "failed to convert metadata into camRun");
@@ -240,4 +303,57 @@
         psFree(row);
     }
+    }
+    if (strcmp(stage,"stack") == 0) {
+    for (long i = 0; i < psArrayLength(output); i++) {
+        psMetadata *md = output->data[i];
+
+	stackRunRow *row = stackRunObjectFromMetadata(md);
+	
+        if (!row) {
+            psError(PS_ERR_UNKNOWN, false, "failed to convert metadata into camRun");
+            psFree(output);
+            return false;
+        }
+
+        if (!dvodb && !row->dvodb) {
+            psError(PS_ERR_UNKNOWN, false, "cannot queue addstar run without a defined dvodb: label: %s, stack_id %" PRId64, row->label, row->stack_id);
+            psFree(output);
+            return false;
+        }
+        if (!workdir && !row->workdir) {
+            psError(PS_ERR_UNKNOWN, false, "cannot queue addstar run without a defined workdir: label: %s, stack_id %" PRId64, row->label, row->stack_id);
+            psFree(output);
+            return false;
+        }
+
+        psFree(row);
+    }
+    }
+    if (strcmp(stage,"staticsky") == 0) {
+    for (long i = 0; i < psArrayLength(output); i++) {
+        psMetadata *md = output->data[i];
+
+	staticskyRunRow *row = staticskyRunObjectFromMetadata(md);
+	
+        if (!row) {
+            psError(PS_ERR_UNKNOWN, false, "failed to convert metadata into camRun");
+            psFree(output);
+            return false;
+        }
+
+        if (!dvodb) {  //there's no staticsky.dvodb
+            psError(PS_ERR_UNKNOWN, false, "cannot queue addstar run without a defined dvodb: label: %s, sky_id %" PRId64, row->label, row->sky_id);
+            psFree(output);
+            return false;
+        }
+        if (!workdir && !row->workdir) {
+            psError(PS_ERR_UNKNOWN, false, "cannot queue addstar run without a defined workdir: label: %s, sky_id %" PRId64, row->label, row->sky_id);
+            psFree(output);
+            return false;
+        }
+
+        psFree(row);
+    }
+    }
 
     // start a transaction so we don't end up with an exp without any associted
@@ -255,8 +371,12 @@
 
     // loop over our list of camRun rows
+    if (strcmp(stage,"cam") == 0) {
     for (long i = 0; i < psArrayLength(output); i++) {
         psMetadata *md = output->data[i];
-
-        camRunRow *row = camRunObjectFromMetadata(md);
+	psS64 stage_id =0; 
+	
+		  camRunRow *row = camRunObjectFromMetadata(md);
+	  stage_id = row->cam_id;
+	
         if (!row) {
             psError(PS_ERR_UNKNOWN, false, "failed to convert metadata into camRun");
@@ -267,5 +387,6 @@
         // queue the exp
         if (!pxaddQueueByCamID(config,
-                               row->cam_id,
+			       stage,
+                               stage_id,
                                workdir     ? workdir   : row->workdir,
                                reduction   ? reduction : row->reduction,
@@ -283,5 +404,5 @@
             }
             psError(PS_ERR_UNKNOWN, false,
-                    "failed to trying to queue chip_id: %" PRId64, row->cam_id);
+                    "failed to trying to queue stage %s %" PRId64,stage, stage_id);
             psFree(row);
             psFree(output);
@@ -290,4 +411,88 @@
         psFree(row);
     }
+      }
+    if (strcmp(stage,"stack") == 0) {
+    for (long i = 0; i < psArrayLength(output); i++) {
+        psMetadata *md = output->data[i];
+	psS64 stage_id =0; 
+	
+		  stackRunRow *row = stackRunObjectFromMetadata(md);
+	  stage_id = row->stack_id;
+	
+        if (!row) {
+            psError(PS_ERR_UNKNOWN, false, "failed to convert metadata into camRun");
+            psFree(output);
+            return false;
+        }
+
+        // queue the exp
+        if (!pxaddQueueByCamID(config,
+			       stage,
+                               stage_id,
+                               workdir     ? workdir   : row->workdir,
+                               reduction   ? reduction : row->reduction,
+                               label       ? label     : row->label,
+                               data_group  ? data_group : (row->data_group ? row->data_group :  (label ? label : row->label)),
+                               dvodb       ? dvodb     : row->dvodb,
+                               note        ? note      : NULL,
+                               image_only,
+                               minidvodb,
+                               minidvodb_group,
+                               minidvodb_name
+        )) {
+            if (!psDBRollback(config->dbh)) {
+                psError(PS_ERR_UNKNOWN, false, "database error sfg");
+            }
+            psError(PS_ERR_UNKNOWN, false,
+                    "failed to trying to queue stage %s %" PRId64,stage, stage_id);
+            psFree(row);
+            psFree(output);
+            return false;
+        }
+        psFree(row);
+    }
+      }
+    if (strcmp(stage,"staticsky") == 0) {
+    for (long i = 0; i < psArrayLength(output); i++) {
+        psMetadata *md = output->data[i];
+	psS64 stage_id =0; 
+	
+	staticskyRunRow *row = staticskyRunObjectFromMetadata(md);
+	stage_id = row->sky_id;
+	
+        if (!row) {
+            psError(PS_ERR_UNKNOWN, false, "failed to convert metadata into camRun");
+            psFree(output);
+            return false;
+        }
+
+        // queue the exp
+        if (!pxaddQueueByCamID(config,
+			       stage,
+                               stage_id,
+                               workdir     ? workdir   : row->workdir,
+                               reduction   ? reduction : row->reduction,
+                               label       ? label     : row->label,
+                               data_group  ? data_group : (row->data_group ? row->data_group :  (label ? label : row->label)),
+                               dvodb       ? dvodb     : NULL,
+                               note        ? note      : NULL,
+                               image_only,
+                               minidvodb,
+                               minidvodb_group,
+                               minidvodb_name
+        )) {
+            if (!psDBRollback(config->dbh)) {
+                psError(PS_ERR_UNKNOWN, false, "database error sfg");
+            }
+            psError(PS_ERR_UNKNOWN, false,
+                    "failed to trying to queue stage %s %" PRId64,stage, stage_id);
+            psFree(row);
+            psFree(output);
+            return false;
+        }
+        psFree(row);
+    }
+      }
+
     psFree(output);
 
@@ -308,4 +513,8 @@
     PXOPT_COPY_S64(config->args, where, "-add_id",    "addRun.add_id", "==");
     PXOPT_COPY_S64(config->args, where, "-cam_id",    "camRun.cam_id", "==");
+    PXOPT_COPY_S64(config->args, where, "-stack_id",    "stackRun.stack_id", "==");
+   PXOPT_COPY_S64(config->args, where, "-sky_id",    "staticskyRun.sky_id", "==");
+    
+    PXOPT_LOOKUP_STR(stage,       config->args, "-stage", false, false);
     pxcamGetSearchArgs (config, where); // most search arguments based on camera
     PXOPT_COPY_STR(config->args, where, "-label",     "addRun.label", "==");
@@ -319,6 +528,14 @@
         return false;
     }
-
-    psString query = psStringCopy("UPDATE addRun JOIN camRun USING(cam_id) JOIN chipRun USING(chip_id) JOIN rawExp USING(exp_id)");
+    psString query = NULL;
+    if (strcmp(stage, "cam")==0) {
+    query = psStringCopy("UPDATE addRun JOIN camRun on cam_id = stage_id JOIN chipRun USING(chip_id) JOIN rawExp USING(exp_id)");
+    }
+    if (strcmp(stage, "stack")==0) {
+    query = psStringCopy("UPDATE addRun JOIN stackRun on stack_id = stage_id");
+    }
+    if (strcmp(stage, "staticsky")==0) {
+    query = psStringCopy("UPDATE addRun JOIN staticskyRun on sky_id = stage_id");
+    }
 
     // pxUpdateRun gets parameters from config->args and runs the update query
@@ -340,4 +557,7 @@
     PXOPT_COPY_S64(config->args, where, "-add_id",    "addRun.add_id", "==");
     PXOPT_COPY_S64(config->args, where, "-cam_id",    "camRun.cam_id", "==");
+    PXOPT_COPY_S64(config->args, where, "-stack_id",    "stackRun.stack_id", "==");
+    PXOPT_COPY_S64(config->args, where, "-sky_id",    "staticskyRun.sky_id", "==");
+    PXOPT_LOOKUP_STR(stage,       config->args, "-stage", false, false);
     pxcamGetSearchArgs (config, where);
     pxAddLabelSearchArgs (config, where, "-label", "addRun.label", "==");
@@ -345,6 +565,18 @@
     PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
     PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
-
-    psString query = pxDataGet("addtool_find_pendingexp.sql");
+    
+    psString query = NULL;
+    
+    if (strcmp(stage, "cam")==0) { 
+    query = pxDataGet("addtool_find_pendingexp_cam.sql");
+    }
+    if (strcmp(stage, "stack")==0) { 
+    query = pxDataGet("addtool_find_pendingexp_stack.sql");
+    }
+    if (strcmp(stage, "staticsky")==0) { 
+    query = pxDataGet("addtool_find_pendingexp_staticsky.sql");
+    }
+
+
     if (!query) {
         psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
@@ -359,4 +591,12 @@
     }
     psFree(where);
+    if (strcmp(stage, "stack") == 0) {
+      //this group by is needed to join against all the warps (to get camera)
+      psStringAppend(&query, " GROUP BY %s", "stack_id");
+    }
+    if (strcmp(stage, "staticsky") == 0) {
+      //this group by is needed to join against all the warps (to get camera)
+      psStringAppend(&query, " GROUP BY %s", "sky_id");
+    }
 
     // treat limit == 0 as "no limit"
@@ -522,5 +762,6 @@
     psMetadata *where = psMetadataAlloc();
     PXOPT_COPY_S64(config->args, where, "-add_id",    "addRun.add_id",    "==");
-    PXOPT_COPY_S64(config->args, where, "-cam_id",    "camRun.cam_id",    "==");
+    PXOPT_COPY_S64(config->args, where, "-stage_id",    "addRun.stage_id",    "==");
+    PXOPT_LOOKUP_STR(stage,       config->args, "-stage", false, false);
     pxcamGetSearchArgs (config, where);
     pxAddLabelSearchArgs (config, where, "-label",    "addRun.label",     "==");
@@ -537,6 +778,16 @@
         return false;
     }
-
-    psString query = pxDataGet("addtool_find_processedexp.sql");
+    psString query = NULL;
+
+    if (strcmp (stage,"cam") == 0) {
+    query = pxDataGet("addtool_find_processedexp_cam.sql");
+    }
+    if (strcmp (stage,"stack") == 0) {
+    query = pxDataGet("addtool_find_processedexp_stack.sql");
+    }
+    if (strcmp (stage,"staticsky") == 0) {
+    query = pxDataGet("addtool_find_processedexp_staticsky.sql");
+    }
+   
     if (!query) {
         psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
@@ -567,4 +818,6 @@
         psStringAppend(&query, " %s", " WHERE addProcessedExp.fault = 0");
     }
+    psStringAppend(&query, " AND stage = '%s'", stage);
+
     psFree(where);
 
@@ -616,5 +869,6 @@
     psMetadata *where = psMetadataAlloc();
     PXOPT_COPY_S64(config->args, where, "-add_id",    "addRun.add_id",         "==");
-    PXOPT_COPY_S64(config->args, where, "-cam_id",    "camRun.cam_id",         "==");
+    PXOPT_COPY_S64(config->args, where, "-stage_id",    "addRun.stage_id",         "==");
+    PXOPT_LOOKUP_STR(stage,       config->args, "-stage", false, false);
     pxcamGetSearchArgs (config, where);
     pxAddLabelSearchArgs (config, where, "-label",    "addRun.label",     "==");
@@ -634,8 +888,18 @@
 
     {
-        psString query = pxDataGet("addtool_revertprocessedexp.sql");
-        if (!query) {
+      psString query = NULL;
+      if (strcmp(stage, "cam") == 0) {
+         query = pxDataGet("addtool_revertprocessedexp_cam.sql");
+	}
+	
+    if (strcmp(stage, "stack") == 0) {
+	 query = pxDataGet("addtool_revertprocessedexp_stack.sql");
+      }
+     if (strcmp(stage, "staticsky") == 0) {
+	 query = pxDataGet("addtool_revertprocessedexp_staticsky.sql");
+      }
+      if (!query) {
             // rollback
-            if (!psDBRollback(config->dbh)) {
+	if (!psDBRollback(config->dbh)) {
                 psError(PS_ERR_UNKNOWN, false, "database error");
             }
@@ -681,5 +945,6 @@
     psMetadata *where = psMetadataAlloc();
     PXOPT_COPY_S64(config->args, where, "-add_id",   "add_id",   "==");
-    PXOPT_COPY_S64(config->args, where, "-cam_id",  "cam_id",  "==");
+    PXOPT_COPY_S64(config->args, where, "-stage_id",  "stage_id",  "==");
+    PXOPT_LOOKUP_STR(stage,       config->args, "-stage", false, false);
     PXOPT_LOOKUP_S16(fault, config->args, "-fault", true, false);
 
Index: trunk/ippTools/src/addtoolConfig.c
===================================================================
--- trunk/ippTools/src/addtoolConfig.c	(revision 31301)
+++ trunk/ippTools/src/addtoolConfig.c	(revision 31375)
@@ -49,5 +49,8 @@
     // -definebyquery
     psMetadata *definebyqueryArgs = psMetadataAlloc();
+    psMetadataAddStr(definebyqueryArgs, PS_LIST_TAIL, "-stage",             0, "set the stage (required)", NULL);
     psMetadataAddS64(definebyqueryArgs, PS_LIST_TAIL, "-cam_id",             0, "search by cam_id", 0);
+    psMetadataAddS64(definebyqueryArgs, PS_LIST_TAIL, "-stack_id",             0, "search by stack_id", 0);
+    psMetadataAddS64(definebyqueryArgs, PS_LIST_TAIL, "-sky_id",             0, "search by sky_id", 0);
     pxcamSetSearchArgs(definebyqueryArgs);
     psMetadataAddStr(definebyqueryArgs, PS_LIST_TAIL, "-label", PS_META_DUPLICATE_OK, "search by camRun label", NULL);
@@ -72,6 +75,9 @@
     // -updaterun
     psMetadata *updaterunArgs = psMetadataAlloc();
+    psMetadataAddStr(updaterunArgs, PS_LIST_TAIL, "-stage",             0, "set the stage (required)", NULL);
     psMetadataAddS64(updaterunArgs, PS_LIST_TAIL, "-add_id",                 0, "search by add_id", 0);
     psMetadataAddS64(updaterunArgs, PS_LIST_TAIL, "-cam_id",                 0, "search by cam_id", 0);
+    psMetadataAddS64(updaterunArgs, PS_LIST_TAIL, "-stack_id",                 0, "search by stack_id", 0);
+    psMetadataAddS64(updaterunArgs, PS_LIST_TAIL, "-sky_id",                 0, "search by sky_id", 0);
     pxcamSetSearchArgs(updaterunArgs);
     psMetadataAddStr(updaterunArgs, PS_LIST_TAIL, "-label",                  0, "search by addRun label", NULL);
@@ -89,6 +95,9 @@
     // -pendingexp
     psMetadata *pendingexpArgs = psMetadataAlloc();
+    psMetadataAddStr(pendingexpArgs, PS_LIST_TAIL, "-stage",             0, "set the stage (required)", NULL);
     psMetadataAddS64(pendingexpArgs, PS_LIST_TAIL, "-add_id",            0, "search by add_id", 0);
     psMetadataAddS64(pendingexpArgs, PS_LIST_TAIL, "-cam_id",            0, "search by cam_id", 0);
+    psMetadataAddS64(pendingexpArgs, PS_LIST_TAIL, "-stack_id",                 0, "search by stack_id", 0);
+    psMetadataAddS64(pendingexpArgs, PS_LIST_TAIL, "-sky_id",                 0, "search by sky_id", 0);
     pxcamSetSearchArgs(pendingexpArgs);
     psMetadataAddStr(pendingexpArgs, PS_LIST_TAIL, "-label", PS_META_DUPLICATE_OK, "search by addRun label", NULL);
@@ -109,5 +118,6 @@
     psMetadata *processedexpArgs = psMetadataAlloc();
     psMetadataAddS64(processedexpArgs, PS_LIST_TAIL, "-add_id",   0,            "search by add_id", 0);
-    psMetadataAddS64(processedexpArgs, PS_LIST_TAIL, "-cam_id",   0,            "search by cam_id", 0);
+    psMetadataAddS64(processedexpArgs, PS_LIST_TAIL, "-stage_id",   0,            "search by stage_id", 0);
+    psMetadataAddStr(processedexpArgs, PS_LIST_TAIL, "-stage",             0, "set the stage", NULL);
     pxcamSetSearchArgs(processedexpArgs);
     psMetadataAddStr(processedexpArgs, PS_LIST_TAIL, "-label", PS_META_DUPLICATE_OK, "search by addRun label", NULL);
@@ -122,5 +132,6 @@
     psMetadata *revertprocessedexpArgs = psMetadataAlloc();
     psMetadataAddS64(revertprocessedexpArgs, PS_LIST_TAIL, "-add_id",   0,            "search by add_id", 0);
-    psMetadataAddS64(revertprocessedexpArgs, PS_LIST_TAIL, "-cam_id",   0,            "search by cam_id", 0);
+    psMetadataAddS64(revertprocessedexpArgs, PS_LIST_TAIL, "-stage_id",   0,            "search by stage_id", 0);
+    psMetadataAddStr(revertprocessedexpArgs, PS_LIST_TAIL, "-stage",             0, "set the stage", NULL);
     pxcamSetSearchArgs(revertprocessedexpArgs);
     psMetadataAddStr(revertprocessedexpArgs, PS_LIST_TAIL, "-label",    PS_META_DUPLICATE_OK, "search by addRun label", NULL);
@@ -132,5 +143,6 @@
     psMetadata *updateprocessedexpArgs = psMetadataAlloc();
     psMetadataAddS64(updateprocessedexpArgs, PS_LIST_TAIL, "-add_id", 0,            "search by addtool ID", 0);
-    psMetadataAddS64(updateprocessedexpArgs, PS_LIST_TAIL, "-cam_id",  0,            "search by camtool ID", 0);
+    psMetadataAddS64(updateprocessedexpArgs, PS_LIST_TAIL, "-stage_id",  0,            "search by stage_id", 0);
+    psMetadataAddStr(updateprocessedexpArgs, PS_LIST_TAIL, "-stage",             0, "set the stage", NULL);
     psMetadataAddS16(updateprocessedexpArgs, PS_LIST_TAIL, "-fault",  0,            "set fault code", 0);
 
Index: trunk/ippTools/src/flatcorr.c
===================================================================
--- trunk/ippTools/src/flatcorr.c	(revision 31301)
+++ trunk/ippTools/src/flatcorr.c	(revision 31375)
@@ -664,4 +664,5 @@
         if (!pxaddQueueByCamID(
                 config,
+		"cam",
                 row->cam_id,
                 row->workdir,
Index: trunk/ippTools/src/pxadd.c
===================================================================
--- trunk/ippTools/src/pxadd.c	(revision 31301)
+++ trunk/ippTools/src/pxadd.c	(revision 31375)
@@ -37,5 +37,5 @@
     if (!pxIsValidState(state)) {
         psError(PS_ERR_UNKNOWN, false,
-                "invalid camRun state: %s", state);
+                "invalid stageRun state: %s", state);
         return false;
     }
@@ -51,5 +51,5 @@
 
 
-bool pxaddRunSetStateByQuery(pxConfig *config, psMetadata *where, const char *state)
+bool pxaddRunSetStateByQuery(pxConfig *config, psMetadata *where, const char *stage, const char *state)
 {
     PS_ASSERT_PTR_NON_NULL(config, false);
@@ -59,10 +59,16 @@
     if (!pxIsValidState(state)) {
         psError(PS_ERR_UNKNOWN, false,
-                "invalid chipRun state: %s", state);
+                "invalid stageRun state: %s", state);
         return false;
     }
-
-    psString query = psStringCopy("UPDATE addRun JOIN camRun USING(cam_id) JOIN chipRun USING(chip_id) JOIN rawExp USING(exp_id) SET addRun.state = '%s'");
-
+    psString query = NULL;
+    if (strcmp(stage, "cam") == 0) {
+      query = psStringCopy("UPDATE addRun JOIN camRun USING(cam_id) JOIN chipRun USING(chip_id) JOIN rawExp USING(exp_id) SET addRun.state = '%s'");
+    } 
+    if (strcmp(stage, "stack") == 0) {
+      ///xxx this needs to be fixed
+     
+      /// psString query = psStringCopy("UPDATE addRun JOIN camRun USING(cam_id) JOIN chipRun USING(chip_id) JOIN rawExp USING(exp_id) SET addRun.state = '%s'");
+    } 
     if (where) {
         psString whereClause = psDBGenerateWhereSQL(where, NULL);
@@ -98,9 +104,9 @@
 }
 
-bool pxaddRunSetLabelByQuery(pxConfig *config, psMetadata *where, const char *label)
+bool pxaddRunSetLabelByQuery(pxConfig *config, psMetadata *where, const char *stage, const char *label)
 {
     PS_ASSERT_PTR_NON_NULL(config, false);
     // note label == NULL should be explicitly allowed
-
+    //xxx fix for stack
     psString query = psStringCopy("UPDATE addRun JOIN camRun USING(cam_id) JOIN chipRun USING(chip_id) JOIN rawExp USING(exp_id) SET addRun.label = '%s'");
 
@@ -123,5 +129,6 @@
 
 bool pxaddQueueByCamID(pxConfig *config,
-                       psS64 cam_id,
+		       char *stage,
+                       psS64 stage_id,
                        char *workdir,
                        char *reduction,
@@ -140,6 +147,16 @@
     static psString query = NULL;
     if (!query) {
+      if (strcmp( stage , "cam") == 0) {
         query = pxDataGet("addtool_queue_cam_id.sql");
         psMemSetPersistent(query, true);
+      }
+      if (strcmp(stage,"stack") == 0) {
+	query = pxDataGet("addtool_queue_stack_id.sql");
+        psMemSetPersistent(query, true);
+      }
+      if (strcmp(stage,"staticsky") == 0) {
+	query = pxDataGet("addtool_queue_sky_id.sql");
+        psMemSetPersistent(query, true);
+      }
         if (!query) {
             psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
@@ -148,11 +165,12 @@
     }
 
+    psTrace("addtool.c", PS_LOG_INFO, "pxadd query \n%s\n",query);
     // queue the exp
     // Note: cam_id is being cast here work around psS64 have a different type different
     // on 32/64
     if (!p_psDBRunQueryF(config->dbh, query,
-                         "new", // state
+			 "new", // state
                          workdir  ? workdir   : "NULL",
-                         "dirty", //workdir_state
+			  "dirty", //workdir_state
                          reduction? reduction : "NULL",
                          label    ? label     : "NULL",
@@ -164,5 +182,5 @@
 			 minidvodb_group,
 			 minidvodb_name,
-                         (long long) cam_id
+                         (long long) stage_id
     )) {
       psError(PS_ERR_UNKNOWN, false, "database error %s", query);
Index: trunk/ippTools/src/pxadd.h
===================================================================
--- trunk/ippTools/src/pxadd.h	(revision 31301)
+++ trunk/ippTools/src/pxadd.h	(revision 31375)
@@ -26,10 +26,11 @@
 
 bool pxaddRunSetState(pxConfig *config, psS64 add_id, const char *state);
-bool pxaddRunSetStateByQuery(pxConfig *config, psMetadata *where, const char *state);
+bool pxaddRunSetStateByQuery(pxConfig *config, psMetadata *where, const char *stage, const char *state);
 bool pxaddRunSetLabel(pxConfig *config, psS64 add_id, const char *label);
-bool pxaddRunSetLabelByQuery(pxConfig *config, psMetadata *where, const char *label);
+bool pxaddRunSetLabelByQuery(pxConfig *config, psMetadata *where, const char *stage, const char *label);
 
 bool pxaddQueueByCamID(pxConfig *config,
-		       psS64 cam_id,
+		       char *stage,
+		       psS64 stage_id,
 		       char *workdir,
 		       char *reduction,
