Index: branches/tap_branches/ippTools/src/camtool.c
===================================================================
--- branches/tap_branches/ippTools/src/camtool.c	(revision 25900)
+++ branches/tap_branches/ippTools/src/camtool.c	(revision 27838)
@@ -109,11 +109,11 @@
     psMetadata *where = psMetadataAlloc();
     pxcamGetSearchArgs (config, where);
-    pxAddLabelSearchArgs (config, where, "-label", "camRun.label", "==");
-    PXOPT_COPY_STR(config->args, where, "-reduction", "camRun.reduction", "==");
+    pxAddLabelSearchArgs (config, where, "-label", "chipRun.label", "=="); // define using chipRun label
+    PXOPT_COPY_STR(config->args, where, "-reduction", "chipRun.reduction", "==");
 
     if (!psListLength(where->list) &&
         !psMetadataLookupBool(NULL, config->args, "-all")) {
         psFree(where);
-        psError(PXTOOLS_ERR_DATA, false, "search parameters are required");
+        psError(PXTOOLS_ERR_CONFIG, false, "search parameters are required");
         return false;
     }
@@ -133,5 +133,5 @@
     psString query = pxDataGet("camtool_find_chip_id.sql");
     if (!query) {
-        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
+        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
         psFree(where);
         return false;
@@ -184,8 +184,8 @@
         bool status;
         char *end_stage = psMetadataLookupStr(&status, md, "end_stage");
-	if (end_stage && strcasecmp(end_stage, "warp")) continue;
+        if (end_stage && strcasecmp(end_stage, "warp")) continue;
 
         char *raw_tess_id   = psMetadataLookupStr(&status, md, "tess_id");
-	if (raw_tess_id || tess_id) continue;
+        if (raw_tess_id || tess_id) continue;
 
         char *label  = psMetadataLookupStr(&status, md, "label");
@@ -193,5 +193,5 @@
 
         if (!status) {
-	    psError(PS_ERR_UNKNOWN, false, "cannot queue analysis to WARP without a defined tess id: label: %s, exp_id %" PRId64, label, exp_id);
+            psError(PS_ERR_UNKNOWN, false, "cannot queue analysis to WARP without a defined tess id: label: %s, exp_id %" PRId64, label, exp_id);
             psFree(output);
             return false;
@@ -199,11 +199,11 @@
     }
 
-    // loop over our list of camRun rows
+    // loop over our list of chipRun rows
     for (long i = 0; i < psArrayLength(output); i++) {
         psMetadata *md = output->data[i];
 
-        camRunRow *row = camRunObjectFromMetadata(md);
+        chipRunRow *row = chipRunObjectFromMetadata(md);
         if (!row) {
-            psError(PS_ERR_UNKNOWN, false, "failed to convert metadata into camRun");
+            psError(PS_ERR_UNKNOWN, false, "failed to convert metadata into chipRun");
             psFree(output);
             return false;
@@ -222,4 +222,5 @@
                     tess_id     ? tess_id   : row->tess_id,
                     end_stage   ? end_stage : row->end_stage,
+                    row->magicked,
                     note
         )) {
@@ -254,4 +255,5 @@
     PXOPT_COPY_S64(config->args, where, "-cam_id",    "camRun.cam_id", "==");
     PXOPT_COPY_STR(config->args, where, "-label",     "camRun.label", "==");
+    PXOPT_COPY_STR(config->args, where, "-data_group","camRun.data_group", "==");
     PXOPT_COPY_STR(config->args, where, "-state",     "camRun.state", "==");
     PXOPT_COPY_STR(config->args, where, "-reduction", "camRun.reduction", "==");
@@ -259,5 +261,5 @@
     if (!psListLength(where->list)) {
         psFree(where);
-        psError(PXTOOLS_ERR_DATA, false, "search parameters are required");
+        psError(PXTOOLS_ERR_CONFIG, false, "search parameters are required");
         return false;
     }
@@ -265,7 +267,7 @@
 
     // pxUpdateRun gets parameters from config->args and updates
-    bool result = pxUpdateRun(config, where, &query, "camRun", true);
+    bool result = pxUpdateRun(config, where, &query, "camRun", "cam_id", "camProcessedExp", true);
     if (!result) {
-        psError(PXTOOLS_ERR_DATA, false, "pxUpdateRun failed");
+        psError(psErrorCodeLast(), false, "pxUpdateRun failed");
     }
 
@@ -292,5 +294,5 @@
     psString query = pxDataGet("camtool_find_pendingexp.sql");
     if (!query) {
-        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
+        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
         return false;
     }
@@ -357,5 +359,5 @@
     psString query = pxDataGet("camtool_find_pendingimfile.sql");
     if (!query) {
-        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
+        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
         return false;
     }
@@ -479,6 +481,7 @@
     PXOPT_LOOKUP_S16(fault, config->args, "-fault", false, false);
     PXOPT_LOOKUP_S16(quality, config->args, "-quality", false, false);
-    
-    PXOPT_LOOKUP_S64(magicked, config->args, "-magicked", false, false);
+
+//    Get this from the chipRun
+//    PXOPT_LOOKUP_S64(magicked, config->args, "-magicked", false, false);
 
     // generate restrictions
@@ -488,5 +491,5 @@
     psString query = pxDataGet("camtool_find_pendingexp.sql");
     if (!query) {
-        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
+        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
         return false;
     }
@@ -523,4 +526,7 @@
         return false;
     }
+
+    bool status;
+    psS64 chip_magicked = psMetadataLookupS64(&status, output->data[0], "chip_magicked");
 
     camRunRow *pendingRow = camRunObjectFromMetadata(output->data[0]);
@@ -603,16 +609,5 @@
     }
 
-    // since there is only one exp per 'new' set camRun.state = 'full'
-    if (!pxcamRunSetState(config, row->cam_id, "full", magicked)) {
-        psError(PS_ERR_UNKNOWN, false, "failed to change camRun.state for cam_id: %" PRId64, row->cam_id);
-        psFree(row);
-        psFree(pendingRow);
-        return false;
-    }
-
-    // NULL for end_stage means go as far as possible
-    // EAM : skip here if fault != 0
-    // Also, we can run fake even if tess_id is not defined
-    if (fault || (pendingRow->end_stage && psStrcasestr(pendingRow->end_stage, "cam"))) {
+    if (fault) {
         psFree(row);
         psFree(pendingRow);
@@ -623,6 +618,26 @@
         return true;
     }
+    // else continue on...
+
+    // since there is only one exp per 'new' set camRun.state = 'full'
+    // propagate magicked state from chipRun
+    if (!pxcamRunSetState(config, row->cam_id, "full", chip_magicked)) {
+        psError(PS_ERR_UNKNOWN, false, "failed to change camRun.state for cam_id: %" PRId64, row->cam_id);
+        psFree(row);
+        psFree(pendingRow);
+        return false;
+    }
     psFree(row);
-    // else continue on...
+
+    // EAM:  NULL for end_stage means go as far as possible
+    // Also, we can run fake even if tess_id is not defined
+    if (pendingRow->end_stage && psStrcasestr(pendingRow->end_stage, "cam")) {
+        psFree(pendingRow);
+        if (!psDBCommit(config->dbh)) {
+            psError(PS_ERR_UNKNOWN, false, "database error");
+            return false;
+        }
+        return true;
+    }
 
     if (!pxfakeQueueByCamID(config,
@@ -666,4 +681,5 @@
     PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
     PXOPT_LOOKUP_BOOL(faulted, config->args, "-faulted", false);
+    PXOPT_LOOKUP_BOOL(pstamp_order, config->args, "-pstamp_order", false);
 
     // generate restrictions
@@ -672,10 +688,20 @@
     PXOPT_COPY_S64(config->args, where, "-cam_id",    "camRun.cam_id",    "==");
     pxAddLabelSearchArgs (config, where, "-label",    "camRun.label",     "==");
+    pxAddLabelSearchArgs (config, where, "-data_group", "camRun.data_group",     "LIKE");
     PXOPT_COPY_STR(config->args, where, "-reduction", "camRun.reduction", "==");
 
-    if (!psListLength(where->list) &&
+    psString where2 = NULL;
+    if (!pxspaceAddWhere(config, &where2, "rawExp")) {
+        psError(psErrorCodeLast(), false, "pxSpaceAddWhere failed");
+        return false;
+    }
+    if (!pxmagicAddWhere(config, &where2, "chipRun")) {
+        psError(psErrorCodeLast(), false, "pxSpaceAddWhere failed");
+        return false;
+    }
+    if (!psListLength(where->list) && !where2 &&
         !psMetadataLookupBool(NULL, config->args, "-all")) {
         psFree(where);
-        psError(PXTOOLS_ERR_DATA, false, "search parameters (or -all) are required");
+        psError(PXTOOLS_ERR_CONFIG, false, "search parameters (or -all) are required");
         return false;
     }
@@ -683,5 +709,5 @@
     psString query = pxDataGet("camtool_find_processedexp.sql");
     if (!query) {
-        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
+        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
         return false;
     }
@@ -691,5 +717,5 @@
         psStringAppend(&query, " WHERE %s", whereClause);
         psFree(whereClause);
-    } 
+    }
 
     // we either add AND (condition) or WHERE (condition):
@@ -697,5 +723,5 @@
         // list only faulted rows
         psStringAppend(&query, " %s", " AND camProcessedExp.fault != 0");
-    } 
+    }
     if (where->list && !faulted) {
         // don't list faulted rows
@@ -705,5 +731,5 @@
         // list only faulted rows
         psStringAppend(&query, " %s", " WHERE camProcessedExp.fault != 0");
-    } 
+    }
     if (!where->list && !faulted) {
         // don't list faulted rows
@@ -711,7 +737,15 @@
     }
     psFree(where);
-
-    // order by cam_id so that the postage stamp parser can easliy find the 'latest' astrometry
-    psStringAppend(&query, " ORDER BY cam_id");
+    if (where2) {
+        psStringAppend(&query, " %s", where2);
+        psFree(where2);
+    }
+
+
+    if (pstamp_order) {
+        // put runs in order of exposure id with newest chip Runs first
+        // The postage stamp parser depends on this behavior
+        psStringAppend(&query, " ORDER BY exp_id, cam_id DESC");
+    }
 
     // treat limit == 0 as "no limit"
@@ -766,5 +800,5 @@
     if (!psListLength(where->list) && !psMetadataLookupBool(NULL, config->args, "-all")) {
         psFree(where);
-        psError(PXTOOLS_ERR_DATA, false, "search parameters are required");
+        psError(PXTOOLS_ERR_CONFIG, false, "search parameters are required");
         return false;
     }
@@ -779,5 +813,5 @@
         psString query = pxDataGet("camtool_reset_faulted_runs.sql");
         if (!query) {
-            psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
+            psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
             psFree(where);
             return false;
@@ -811,5 +845,5 @@
                 psError(PS_ERR_UNKNOWN, false, "database error");
             }
-            psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
+            psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
             psFree(where);
             return false;
@@ -951,5 +985,5 @@
     psString query = pxDataGet("camtool_pendingcleanuprun.sql");
     if (!query) {
-        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
+        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
         return false;
     }
@@ -1016,5 +1050,5 @@
     psString query = pxDataGet("camtool_pendingcleanupexp.sql");
     if (!query) {
-        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
+        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
         return false;
     }
@@ -1077,5 +1111,5 @@
     psString query = pxDataGet("camtool_donecleanup.sql");
     if (!query) {
-        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
+        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
         return false;
     }
@@ -1163,5 +1197,5 @@
     psString query = pxDataGet(tables[i].sqlFilename);
     if (!query) {
-      psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
+      psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
       return false;
     }
