Index: trunk/ippTools/src/addtool.c
===================================================================
--- trunk/ippTools/src/addtool.c	(revision 30912)
+++ 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);
 
