Index: /trunk/ippTools/src/difftool.c
===================================================================
--- /trunk/ippTools/src/difftool.c	(revision 11788)
+++ /trunk/ippTools/src/difftool.c	(revision 11789)
@@ -40,5 +40,4 @@
 
 static bool setp5RunState(pxConfig *config, const char *p4_id, const char *state);
-static bool isValidMode(pxConfig *config, const char *mode);
 
 # define MODECASE(caseName, func) \
@@ -90,19 +89,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;
-    }
-    // check mode
-    if (mode && !isValidMode(config, mode)) {
-        psError(PS_ERR_UNKNOWN, false, "invalud mode");
-        return false;
-    }
-
     psString workdir = psMetadataLookupStr(&status, config->args, "-workdir");
     if (!status) {
@@ -130,7 +114,6 @@
     }
 
-    p4RunRow *p4Run = p4RunRowAlloc(
+    p5RunRow *run = p5RunRowAlloc(
             0,          // ID
-            mode,
             "reg",      // state
             workdir,
@@ -138,11 +121,11 @@
     );
     psFree(registered);
-    if (!p4Run) {
-        psError(PS_ERR_UNKNOWN, false, "failed to alloc p4Run object");
+    if (!run) {
+        psError(PS_ERR_UNKNOWN, false, "failed to alloc p5Run object");
         return true;
     }
-    if (!p4RunInsertObject(config->dbh, p4Run)) {
+    if (!p5RunInsertObject(config->dbh, run)) {
         psError(PS_ERR_UNKNOWN, false, "database error");
-        psFree(p4Run);
+        psFree(run);
         return true;
     }
@@ -154,16 +137,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 (!p5RunPrintObject(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;
@@ -176,11 +159,11 @@
 
     bool status = false;
-    psString p4_id = psMetadataLookupStr(&status, config->args, "-p4_id");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -p4t_id");
-        return false;
-    }
-    if (!p4_id) {
-        psError(PS_ERR_UNKNOWN, true, "-p4_id is required");
+    psString p5_id = psMetadataLookupStr(&status, config->args, "-p5_id");
+    if (!status) {
+        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -p5_id");
+        return false;
+    }
+    if (!p5_id) {
+        psError(PS_ERR_UNKNOWN, true, "-p5_id is required");
         return false;
     }
@@ -198,5 +181,5 @@
     if (state) {
         // set detRun.state to state
-        return setp5RunState(config, p4_id, state);
+        return setp5RunState(config, p5_id, state);
     }
 
@@ -210,4 +193,14 @@
 
     bool status = false;
+    psString p5_id = psMetadataLookupStr(&status, config->args, "-p5_id");
+    if (!status) {
+        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -p5_id");
+        return false;
+    }
+    if (!p5_id) {
+        psError(PS_ERR_UNKNOWN, true, "-p5_id is required");
+        return false;
+    }
+
     psString p4_id = psMetadataLookupStr(&status, config->args, "-p4_id");
     if (!status) {
@@ -215,9 +208,28 @@
         return false;
     }
-    if (!p4_id) {
-        psError(PS_ERR_UNKNOWN, true, "-p4_id is required");
-        return false;
-    }
-
+    if (!p5_id) {
+        psError(PS_ERR_UNKNOWN, true, "-p5_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 exp_tag = psMetadataLookupStr(&status, config->args, "-exp_tag");
     if (!status) {
@@ -237,8 +249,14 @@
     }
 
+    psString kind = psMetadataLookupStr(&status, config->args, "-kind");
+    if (!status) {
+        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -kind");
+        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");
+    bool template = psMetadataLookupBool(&status, config->args, "-template");
+    if (!status) {
+        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -template");
         return false;
     }
@@ -247,9 +265,13 @@
     // XXX instead of validiting it here we should just use forgein key
     // constrants
-    if (!p4InputExpInsert(config->dbh, 
+    if (!p5InputScfileInsert(config->dbh, 
+            (psS32)atoi(p5_id),
             (psS32)atoi(p4_id),
+            skycell_id,
+            tess_id,
             exp_tag,
             p3_version,
-            magiced
+            kind,
+            template
         )) {
         psError(PS_ERR_UNKNOWN, false, "database error");
@@ -275,18 +297,20 @@
     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"
+        "   p4Scfile.*\n"
+        " FROM p5Run\n"
+        " JOIN p5InputScfile\n"
+        "   USING(p5_id)\n"
+        " JOIN p4Scfile\n"
+        "   ON p5InputScile.p4_id      = p4Scfile.p4_id\n"
+        "   ON p5InputScile.skycell_id = p4Scfile.skycell_id\n"
+        "   ON p5InputScile.tess_id    = p4Scfile.tess_id\n"
+        "   ON p5InputScile.exp_tag    = p4Scfile.exp_tag\n"
+        "   ON p5InputScile.p3_version = p4Scfile.p3_versiong\n"
         " WHERE\n"
-        "   p4Run.state = 'run'\n" 
-        "   AND p3ProcessedExp.fault = 0\n"
+        "   p5Run.state = 'run'\n" 
     );
 
     if (config->where) {
-        psString whereClause = psDBGenerateWhereConditionSQL(config->where, "p4InputExp");
+        psString whereClause = psDBGenerateWhereConditionSQL(config->where, "p5InputScfile");
         psStringAppend(&query, " AND %s", whereClause);
         psFree(whereClause);
@@ -322,5 +346,5 @@
     }
     if (!psArrayLength(output)) {
-        psError(PS_ERR_UNKNOWN, false, "no pending rawImfile rows found");
+        psError(PS_ERR_UNKNOWN, false, "no p5InputScfile rows found");
         psFree(output);
         return true;
@@ -339,5 +363,5 @@
     if (psArrayLength(output)) {
         // negative simple so the default is true
-        if (!ippdbPrintMetadatas(stdout, output, "p4InputExp", !simple)) {
+        if (!ippdbPrintMetadatas(stdout, output, "p5InputScfile", !simple)) {
             psError(PS_ERR_UNKNOWN, false, "failed to print array");
             psFree(output);
@@ -366,25 +390,27 @@
     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"
+        "   p4Scfile.*\n"
+        " FROM p5Run\n"
+        " JOIN p5InputScfile\n"
+        "   USING(p5_id)\n"
+        " JOIN p4Scfile\n"
+        "   ON p5InputScile.p4_id      = p4Scfile.p4_id\n"
+        "   ON p5InputScile.skycell_id = p4Scfile.skycell_id\n"
+        "   ON p5InputScile.tess_id    = p4Scfile.tess_id\n"
+        "   ON p5InputScile.exp_tag    = p4Scfile.exp_tag\n"
+        "   ON p5InputScile.p3_version = p4Scfile.p3_versiong\n"
+        " LEFT JOIN p5DiffScfile\n"
+        "   USING(p5_id, skycell_id, tess_id, exp_tag, p3_version)\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"
+        "   p5Run.state = 'run'\n" 
+        "   AND p4DiffScfile.p5_id IS NULL\n"
+        "   AND p4DiffScfile.skycell_id IS NULL\n"
+        "   AND p4DiffScfile.tess_id IS NULL\n"
+        "   AND p4DiffScfile.exp_tag IS NULL\n"
+        "   AND p4DiffScfile.p3_version IS NULL\n"
     );
 
     if (config->where) {
-        psString whereClause = psDBGenerateWhereConditionSQL(config->where, "p4InputExp");
+        psString whereClause = psDBGenerateWhereConditionSQL(config->where, "p5Scfile");
         psStringAppend(&query, " AND %s", whereClause);
         psFree(whereClause);
@@ -420,5 +446,5 @@
     }
     if (!psArrayLength(output)) {
-        psError(PS_ERR_UNKNOWN, false, "no pending rawImfile rows found");
+        psError(PS_ERR_UNKNOWN, false, "no p5Scfile rows found");
         psFree(output);
         return true;
@@ -437,5 +463,5 @@
     if (psArrayLength(output)) {
         // negative simple so the default is true
-        if (!ippdbPrintMetadatas(stdout, output, "p4InputExp", !simple)) {
+        if (!ippdbPrintMetadatas(stdout, output, "p5Scfile", !simple)) {
             psError(PS_ERR_UNKNOWN, false, "failed to print array");
             psFree(output);
@@ -455,11 +481,11 @@
 
     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");
+    psString p5_id = psMetadataLookupStr(&status, config->args, "-p5_id");
+    if (!status) {
+        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -p5_id");
+        return false;
+    }
+    if (!p5_id) {
+        psError(PS_ERR_UNKNOWN, true, "-p5_id is required");
         return false;
     }
@@ -527,6 +553,6 @@
 
     // XXX need to validate that this coresponds to an p4InputImfile
-    if (!p4ScfileInsert(config->dbh, 
-            (psS32)atoi(p4_id),
+    if (!p5DiffScfileInsert(config->dbh, 
+            (psS32)atoi(p5_id),
             skycell_id,
             tess_id,
@@ -544,4 +570,5 @@
 }
 
+
 static bool diffscfileMode(pxConfig *config)
 {
@@ -558,14 +585,14 @@
     psString query = psStringCopy( 
         "SELECT\n"
-        "   p4Scfile.*\n"
-        " FROM p4Run\n"
-        " JOIN p4Scfile\n"
-        "   USING(p4_id)\n"
+        "   p5DiffScfile.*\n"
+        " FROM p5Run\n"
+        " JOIN p5Scfile\n"
+        "   USING(p5_id)\n"
         " WHERE\n"
-        "   p4Run.state = 'run'\n" 
+        "   p5Run.state = 'run'\n" 
     );
 
     if (config->where) {
-        psString whereClause = psDBGenerateWhereConditionSQL(config->where, "p4Scfile");
+        psString whereClause = psDBGenerateWhereConditionSQL(config->where, "p5DiffScfile");
         psStringAppend(&query, " AND %s", whereClause);
         psFree(whereClause);
@@ -601,5 +628,5 @@
     }
     if (!psArrayLength(output)) {
-        psError(PS_ERR_UNKNOWN, false, "no p4Scfile rows found");
+        psError(PS_ERR_UNKNOWN, false, "no p5DiffScfile rows found");
         psFree(output);
         return true;
@@ -618,5 +645,5 @@
     if (psArrayLength(output)) {
         // negative simple so the default is true
-        if (!ippdbPrintMetadatas(stdout, output, "p4Scfile", !simple)) {
+        if (!ippdbPrintMetadatas(stdout, output, "p5DiffScfile", !simple)) {
             psError(PS_ERR_UNKNOWN, false, "failed to print array");
             psFree(output);
@@ -656,23 +683,2 @@
     return true;
 }
-
-static bool isValidMode(pxConfig *config, const char *mode)
-{
-    PS_ASSERT_PTR_NON_NULL(config, false);
-    PS_ASSERT_PTR_NON_NULL(mode, false);
-
-    // check that state is a valid string value
-    if (!(
-            (strncmp(mode, "warp", 5) == 0)
-            || (strncmp(mode, "diff", 5) == 0)
-            || (strncmp(mode, "stack", 6) == 0)
-            || (strncmp(mode, "magic", 6) == 0)
-        )
-    ) {
-        psError(PS_ERR_UNKNOWN, false,
-                "invalid detRun mode: %s", mode);
-        return false;
-    }
-
-    return true;
-}
Index: /trunk/ippTools/src/difftoolConfig.c
===================================================================
--- /trunk/ippTools/src/difftoolConfig.c	(revision 11788)
+++ /trunk/ippTools/src/difftoolConfig.c	(revision 11789)
@@ -72,4 +72,6 @@
     psMetadataAddStr(addinputscfileArgs, PS_LIST_TAIL, "-p5_id", 0,
             "define p5 ID (required)", NULL);
+    psMetadataAddStr(addinputscfileArgs, PS_LIST_TAIL, "-p4_id", 0,
+            "define p4 ID (required)", NULL);
     psMetadataAddStr(addinputscfileArgs, PS_LIST_TAIL, "-skycell_id", 0,
             "define by skycell ID", NULL);
@@ -89,4 +91,6 @@
     psMetadataAddStr(inputscfileArgs, PS_LIST_TAIL, "-p5_id", 0,
             "search by p5 ID (required)", NULL);
+    psMetadataAddStr(inputscfileArgs, PS_LIST_TAIL, "-p4_id", 0,
+            "search by p4 ID (required)", NULL);
     psMetadataAddStr(inputscfileArgs, PS_LIST_TAIL, "-skycell_id", 0,
             "search by skycell ID", NULL);
