Index: trunk/ippTools/src/stacktool.c
===================================================================
--- trunk/ippTools/src/stacktool.c	(revision 11851)
+++ trunk/ippTools/src/stacktool.c	(revision 11866)
@@ -94,14 +94,4 @@
     // required options
     bool status = false;
-    psString mode = psMetadataLookupStr(&status, config->args, "-mode");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -survey_mode");
-        return false;
-    }
-    if (!mode) {
-        psError(PS_ERR_UNKNOWN, true, "-mode is required");
-        return false;
-    }
-
     psString workdir = psMetadataLookupStr(&status, config->args, "-workdir");
     if (!status) {
@@ -129,7 +119,6 @@
     }
 
-    p4RunRow *p4Run = p4RunRowAlloc(
+    p6RunRow *run = p6RunRowAlloc(
             0,          // ID
-            mode,
             "reg",      // state
             workdir,
@@ -137,16 +126,16 @@
     );
     psFree(registered);
-    if (!p4Run) {
-        psError(PS_ERR_UNKNOWN, false, "failed to alloc p4Run object");
+    if (!run) {
+        psError(PS_ERR_UNKNOWN, false, "failed to alloc p6Run object");
         return true;
     }
-    if (!p4RunInsertObject(config->dbh, p4Run)) {
+    if (!p6RunInsertObject(config->dbh, run)) {
         psError(PS_ERR_UNKNOWN, false, "database error");
-        psFree(p4Run);
+        psFree(run);
         return true;
     }
 
     // get the assigned p4_id
-    p4Run->p4_id = psDBLastInsertID(config->dbh);
+    run->p6_id = psDBLastInsertID(config->dbh);
 
     bool simple = false;
@@ -156,16 +145,16 @@
         if (!status) {
             psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -simple");
-            psFree(p4Run);
-            return false;
-        }
-    }
-
-    if (!p4RunPrintObject(stdout, p4Run, !simple)) {
+            psFree(run);
+            return false;
+        }
+    }
+
+    if (!p6RunPrintObject(stdout, run, !simple)) {
             psError(PS_ERR_UNKNOWN, false, "failed to print object");
-            psFree(p4Run);
-            return false;
-    }
-
-    psFree(p4Run);
+            psFree(run);
+            return false;
+    }
+
+    psFree(run);
 
     return true;
@@ -178,7 +167,52 @@
 
     bool status = false;
+    psString p6_id = psMetadataLookupStr(&status, config->args, "-p6_id");
+    if (!status) {
+        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -p6_id");
+        return false;
+    }
+    if (!p6_id) {
+        psError(PS_ERR_UNKNOWN, true, "-p6_id is required");
+        return false;
+    }
+
+    psString state = psMetadataLookupStr(&status, config->args, "-state");
+    if (!status) {
+        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -state");
+        return false;
+    }
+    if (!state) {
+        psError(PS_ERR_UNKNOWN, true, "-state is required");
+        return false;
+    }
+
+    if (state) {
+        // set detRun.state to state
+        return setp6RunState(config, p6_id, state);
+    }
+
+    return true;
+}
+
+
+static bool addinputscfileMode(pxConfig *config)
+{
+    PS_ASSERT_PTR_NON_NULL(config, false);
+
+    bool status = false;
+    psString p6_id = psMetadataLookupStr(&status, config->args, "-p6_id");
+    if (!status) {
+        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -p6_id");
+        return false;
+    }
+    if (!p6_id) {
+        psError(PS_ERR_UNKNOWN, true, "-p6_id is required");
+        return false;
+
+    }
+
     psString p4_id = psMetadataLookupStr(&status, config->args, "-p4_id");
     if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -p4t_id");
+        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -p4_id");
         return false;
     }
@@ -188,59 +222,21 @@
     }
 
-    psString state = psMetadataLookupStr(&status, config->args, "-state");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -state");
-        return false;
-    }
-    if (!state) {
-        psError(PS_ERR_UNKNOWN, true, "-state is required");
-        return false;
-    }
-
-    if (state) {
-        // set detRun.state to state
-        return setp6RunState(config, p4_id, state);
-    }
-
-    return true;
-}
-
-
-static bool addinputscfileMode(pxConfig *config)
-{
-    PS_ASSERT_PTR_NON_NULL(config, false);
-
-    bool status = false;
-    psString p4_id = psMetadataLookupStr(&status, config->args, "-p4_id");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -p4_id");
-        return false;
-    }
-    if (!p4_id) {
-        psError(PS_ERR_UNKNOWN, true, "-p4_id is required");
-        return false;
-    }
-
-    psString exp_tag = psMetadataLookupStr(&status, config->args, "-exp_tag");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -exp_tag");
-        return false;
-    }
-    if (!exp_tag) {
-        psError(PS_ERR_UNKNOWN, true, "-exp_tag is required");
-        return false;
-    }
-
-    // defaults to 0
-    psS32 p3_version = psMetadataLookupS32(&status, config->args, "-p3_version");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -p3_version");
-        return false;
-    }
-
-    // defaults to false
-    bool magiced = psMetadataLookupBool(&status, config->args, "-p3_version");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -p3_version");
+    psString skycell_id = psMetadataLookupStr(&status, config->args, "-skycell_id");
+    if (!status) {
+        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -skycell_id");
+        return false;
+    }
+    if (!skycell_id) {
+        psError(PS_ERR_UNKNOWN, true, "-skycell_id is required");
+        return false;
+    }
+
+    psString tess_id = psMetadataLookupStr(&status, config->args, "-tess_id");
+    if (!status) {
+        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -tess_id");
+        return false;
+    }
+    if (!tess_id) {
+        psError(PS_ERR_UNKNOWN, true, "-tess_id is required");
         return false;
     }
@@ -249,9 +245,9 @@
     // XXX instead of validiting it here we should just use forgein key
     // constrants
-    if (!p4InputExpInsert(config->dbh, 
+    if (!p6InputScfileInsert(config->dbh, 
+            (psS32)atoi(p6_id),
             (psS32)atoi(p4_id),
-            exp_tag,
-            p3_version,
-            magiced
+            skycell_id,
+            tess_id
         )) {
         psError(PS_ERR_UNKNOWN, false, "database error");
@@ -277,15 +273,17 @@
     psString query = psStringCopy( 
         "SELECT\n"
-        "   rawImfile.*\n"
-        " FROM p4Run\n"
-        " JOIN p4InputExp\n"
-        "   USING(p4_id)\n"
-        " JOIN rawImfile\n -- is there any reason not to refer back to rawimfiles?"
-        "   ON p4InputExp.exp_tag = rawImfile.exp_tag\n"
+        "   p4Scfile.*\n"
+        " FROM p6Run\n"
+        " JOIN p6InputScfile\n"
+        "   USING(p6_id)\n"
+        " JOIN p4Scfile\n"
+        "   ON p6InputScfile.p4_id = p4Scfile.p4_id\n"
+        "   AND p6InputScfile.skycell_id = p4Scfile.skycell_id\n"
+        "   AND p6InputScfile.tess_id = p4Scfile.tess_id\n"
         " WHERE\n"
-        "   p4Run.state = 'run'\n" 
+        "   p6Run.state = 'run'\n" 
     );
     if (config->where) {
-        psString whereClause = psDBGenerateWhereConditionSQL(config->where, "p4InputExp");
+        psString whereClause = psDBGenerateWhereConditionSQL(config->where, "p6InputScfile");
         psStringAppend(&query, " AND %s", whereClause);
         psFree(whereClause);
@@ -321,5 +319,5 @@
     }
     if (!psArrayLength(output)) {
-        psError(PS_ERR_UNKNOWN, false, "no pending rawImfile rows found");
+        psError(PS_ERR_UNKNOWN, false, "no p6InputScfile rows found");
         psFree(output);
         return true;
@@ -338,5 +336,5 @@
     if (psArrayLength(output)) {
         // negative simple so the default is true
-        if (!ippdbPrintMetadatas(stdout, output, "p4InputImfile", !simple)) {
+        if (!ippdbPrintMetadatas(stdout, output, "p6InputScfile", !simple)) {
             psError(PS_ERR_UNKNOWN, false, "failed to print array");
             psFree(output);
@@ -365,25 +363,15 @@
     psString query = psStringCopy( 
         "SELECT\n"
-        "   p3ProcessedExp.*\n"
-        " FROM p4Run\n"
-        " JOIN p4InputExp\n"
-        "   USING(p4_id)\n"
-        " JOIN p3ProcessedExp\n"
-        "   ON p4InputExp.exp_tag = p3ProcessedExp.exp_tag\n"
-        "   AND p4InputExp.p3_version = p3ProcessedExp.p3_version\n"
-        " LEFT JOIN p4SkyCellMap\n"
-        "   ON p4InputExp.p4_id = p4SkyCellMap.p4_id\n"
-        "   AND p4InputExp.exp_tag = p4SkyCellMap.exp_tag\n"
-        "   AND p4InputExp.p3_version = p4SkyCellMap.p3_version\n"
+        "   p6Run.p6_id\n"
+        " FROM p6Run\n"
+        " LEFT JOIN p4SumScfile\n"
+        "   USING(p6_id)\n"
         " WHERE\n"
-        "   p4Run.state = 'run'\n" 
-        "   AND p3ProcessedExp.fault = 0\n"
-        "   AND p4SkyCellMap.p4_id IS NULL\n"
-        "   AND p4SkyCellMap.exp_tag IS NULL\n"
-        "   AND p4SkyCellMap.p3_version IS NULL\n"
+        "   p6Run.state = 'run'\n" 
+        "   AND p6SumScfile.p6_id IS NULL\n"
     );
 
     if (config->where) {
-        psString whereClause = psDBGenerateWhereConditionSQL(config->where, "p4InputExp");
+        psString whereClause = psDBGenerateWhereConditionSQL(config->where, "p6SumScfile");
         psStringAppend(&query, " AND %s", whereClause);
         psFree(whereClause);
@@ -419,5 +407,5 @@
     }
     if (!psArrayLength(output)) {
-        psError(PS_ERR_UNKNOWN, false, "no pending rawImfile rows found");
+        psError(PS_ERR_UNKNOWN, false, "no p6SumScfile rows found");
         psFree(output);
         return true;
@@ -436,5 +424,5 @@
     if (psArrayLength(output)) {
         // negative simple so the default is true
-        if (!ippdbPrintMetadatas(stdout, output, "p4InputExp", !simple)) {
+        if (!ippdbPrintMetadatas(stdout, output, "p6SumScfile", !simple)) {
             psError(PS_ERR_UNKNOWN, false, "failed to print array");
             psFree(output);
@@ -454,21 +442,69 @@
 
     bool status = false;
-    psString mapfile = psMetadataLookupStr(&status, config->args, "-mapfile");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -mapfile");
-        return false;
-    }
-    if (!mapfile) {
-        psError(PS_ERR_UNKNOWN, true, "-mapfile is required");
-        return false;
-    }
-
-    if (!psDBTransaction(config->dbh)) {
-        psError(PS_ERR_UNKNOWN, false, "database error");
-        return false;
-    }
-
-    // point of no return
-    if (!psDBCommit(config->dbh)) {
+    psString p6_id = psMetadataLookupStr(&status, config->args, "-p6_id");
+    if (!status) {
+        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -p6_id");
+        return false;
+    }
+    if (!p6_id) {
+        psError(PS_ERR_UNKNOWN, true, "-p6_id is required");
+        return false;
+
+    }
+
+    psString skycell_id = psMetadataLookupStr(&status, config->args, "-skycell_id");
+    if (!status) {
+        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -skycell_id");
+        return false;
+    }
+    if (!skycell_id) {
+        psError(PS_ERR_UNKNOWN, true, "-skycell_id is required");
+        return false;
+    }
+
+    psString tess_id = psMetadataLookupStr(&status, config->args, "-tess_id");
+    if (!status) {
+        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -tess_id");
+        return false;
+    }
+    if (!tess_id) {
+        psError(PS_ERR_UNKNOWN, true, "-tess_id is required");
+        return false;
+    }
+
+    psString uri = psMetadataLookupStr(&status, config->args, "-uri");
+    if (!status) {
+        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -uri");
+        return false;
+    }
+    if (!uri) {
+        psError(PS_ERR_UNKNOWN, true, "-uri is required");
+        return false;
+    }
+
+    // optional
+    psF64 bg = psMetadataLookupF64(&status, config->args, "-bg");
+    if (!status) {
+        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -bg");
+        return false;
+    }
+
+    psF64 bg_mean_stdev = psMetadataLookupF64(&status, config->args, "-bg_mean_stdev");
+    if (!status) {
+        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -bg_mean_stdev");
+        return false;
+    }
+
+    // XXX need to validate the p6_id here
+    // XXX instead of validiting it here we should just use forgein key
+    // constrants
+    if (!p6SumScfileInsert(config->dbh, 
+            (psS32)atoi(p6_id),
+            skycell_id,
+            tess_id,
+            uri,
+            bg,
+            bg_mean_stdev
+        )) {
         psError(PS_ERR_UNKNOWN, false, "database error");
         return false;
@@ -493,14 +529,14 @@
     psString query = psStringCopy( 
         "SELECT\n"
-        "   p4Scfile.*\n"
-        " FROM p4Run\n"
-        " JOIN p4Scfile\n"
-        "   USING(p4_id)\n"
+        "   p6SumScfile.*\n"
+        " FROM p6Run\n"
+        " JOIN p6SumScfile\n"
+        "   USING(p6_id)\n"
         " WHERE\n"
-        "   p4Run.state = 'run'\n" 
+        "   p6Run.state = 'run'\n" 
     );
 
     if (config->where) {
-        psString whereClause = psDBGenerateWhereConditionSQL(config->where, "p4Scfile");
+        psString whereClause = psDBGenerateWhereConditionSQL(config->where, "p6SumScfile");
         psStringAppend(&query, " AND %s", whereClause);
         psFree(whereClause);
@@ -552,6 +588,5 @@
 
     if (psArrayLength(output)) {
-        // negative simple so the default is true
-        if (!ippdbPrintMetadatas(stdout, output, "p4Scfile", !simple)) {
+        if (!ippdbPrintMetadatas(stdout, output, "p6SumScfile", !simple)) {
             psError(PS_ERR_UNKNOWN, false, "failed to print array");
             psFree(output);
@@ -583,5 +618,5 @@
     }
 
-    char *query = "UPDATE p4Run SET state = '%s' WHERE p4_id = '%s'";
+    char *query = "UPDATE p6Run SET state = '%s' WHERE p6_id = '%s'";
     if (!p_psDBRunQuery(config->dbh, query, state, p6_id)) {
         psError(PS_ERR_UNKNOWN, false,
