Index: trunk/ippTools/src/warptool.c
===================================================================
--- trunk/ippTools/src/warptool.c	(revision 15532)
+++ trunk/ippTools/src/warptool.c	(revision 16170)
@@ -108,14 +108,5 @@
 
     // 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;
-    }
+    PXOPT_LOOKUP_STR(mode, config->args, "-mode", true, false);
     // check mode
     if (mode && !isValidMode(config, mode)) {
@@ -123,29 +114,14 @@
         return false;
     }
-
-    psString workdir = psMetadataLookupStr(&status, config->args, "-workdir");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -workdir");
-        return false;
-    }
-    if (!workdir) {
-        psError(PS_ERR_UNKNOWN, true, "-workdir is required");
-        return false;
-    }
-
-    psTime *registered = NULL;
-    {
-        psString registeredStr = psMetadataLookupStr(&status, config->args, "-registered");
-        if (!status) {
-            psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -registered");
-            return false;
-        }
-        // pass through NULL as this is an optional field
-        if (registeredStr) {
-            registered = psTimeFromISO(registeredStr, PS_TIME_UTC);
-        } else {
-            registered = NULL;
-        }
-    }
+    PXOPT_LOOKUP_STR(workdir, config->args, "-workdir", true, false);
+
+    // optional
+    PXOPT_LOOKUP_STR(dvodb, config->args, "-dvodb", false, false);
+    PXOPT_LOOKUP_STR(tess_id, config->args, "-tess_id", false, false);
+    PXOPT_LOOKUP_STR(end_stage, config->args, "-end_stage", false, false);
+
+    // default
+    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
+    PXOPT_LOOKUP_TIME(registered, config->args, "-registered", false, false);
 
     warpRunRow *warpRun = warpRunRowAlloc(
@@ -154,8 +130,10 @@
             "reg",      // state
             workdir,
-            NULL,       // dvodb
+            "dirty",    // workdir_state
+            dvodb,
+            tess_id,
+            end_stage,
             registered
     );
-    psFree(registered);
     if (!warpRun) {
         psError(PS_ERR_UNKNOWN, false, "failed to alloc warpRun object");
@@ -171,15 +149,4 @@
     psS64 warp_id = psDBLastInsertID(config->dbh);
     warpRun->warp_id = warp_id;
-
-    bool simple = false;
-    {
-        bool status = false;
-        simple = psMetadataLookupBool(&status, config->args, "-simple");
-        if (!status) {
-            psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -simple");
-            psFree(warpRun);
-            return false;
-        }
-    }
 
     if (!warpRunPrintObject(stdout, warpRun, !simple)) {
@@ -255,24 +222,6 @@
     PS_ASSERT_PTR_NON_NULL(config, false);
 
-    bool status = false;
-    psString warp_id = psMetadataLookupStr(&status, config->args, "-warp_id");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -warpt_id");
-        return false;
-    }
-    if (!warp_id) {
-        psError(PS_ERR_UNKNOWN, true, "-warp_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;
-    }
+    PXOPT_LOOKUP_STR(warp_id, config->args, "-warp_id", true, false);
+    PXOPT_LOOKUP_STR(state, config->args, "-state", true, false);
 
     if (state) {
@@ -289,35 +238,10 @@
     PS_ASSERT_PTR_NON_NULL(config, false);
 
-    bool status = false;
-    psString warp_id = psMetadataLookupStr(&status, config->args, "-warp_id");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -warp_id");
-        return false;
-    }
-    if (!warp_id) {
-        psError(PS_ERR_UNKNOWN, true, "-warp_id is required");
-        return false;
-    }
-
-    psString cam_id = psMetadataLookupStr(&status, config->args, "-cam_id");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -cam_id");
-        return false;
-    }
-    if (!cam_id) {
-        psError(PS_ERR_UNKNOWN, true, "-cam_id is required");
-        return false;
-    }
+    PXOPT_LOOKUP_STR(warp_id, config->args, "-warp_id", true, false);
+    PXOPT_LOOKUP_STR(cam_id, config->args, "-cam_id", true, false);
 
     // defaults to false
-    bool magiced = psMetadataLookupBool(&status, config->args, "-magiced");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -magiced");
-        return false;
-    }
-
-    // XXX need to validate the warp_id here
-    // XXX instead of validiting it here we should just use forgein key
-    // constrants
+    PXOPT_LOOKUP_BOOL(magiced, config->args, "-magiced", false);
+
     if (!warpInputExpInsert(config->dbh,
             (psS64)atoll(warp_id),
@@ -337,10 +261,6 @@
     PS_ASSERT_PTR_NON_NULL(config, false);
 
-    bool status = false;
-    psU64 limit = psMetadataLookupU64(&status, config->args, "-limit");
-    if (!status) {
-        psError(PXTOOLS_ERR_PROG, false, "failed to lookup value for -limit");
-        return false;
-    }
+    PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
+    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
 
     // find all rawImfiles matching the default query
@@ -391,14 +311,4 @@
     }
 
-    bool simple = false;
-    {
-        bool status = false;
-        simple = psMetadataLookupBool(&status, config->args, "-simple");
-        if (!status) {
-            psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -simple");
-            return false;
-        }
-    }
-
     if (psArrayLength(output)) {
         if (!convertIdToStr(output)) {
@@ -426,10 +336,6 @@
     PS_ASSERT_PTR_NON_NULL(config, false);
 
-    bool status = false;
-    psU64 limit = psMetadataLookupU64(&status, config->args, "-limit");
-    if (!status) {
-        psError(PXTOOLS_ERR_PROG, false, "failed to lookup value for -limit");
-        return false;
-    }
+    PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
+    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
 
     // find all rawImfiles matching the default query
@@ -480,14 +386,4 @@
     }
 
-    bool simple = false;
-    {
-        bool status = false;
-        simple = psMetadataLookupBool(&status, config->args, "-simple");
-        if (!status) {
-            psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -simple");
-            return false;
-        }
-    }
-
     if (psArrayLength(output)) {
         if (!convertIdToStr(output)) {
@@ -515,10 +411,6 @@
     PS_ASSERT_PTR_NON_NULL(config, false);
 
-    bool status = false;
-    psU64 limit = psMetadataLookupU64(&status, config->args, "-limit");
-    if (!status) {
-        psError(PXTOOLS_ERR_PROG, false, "failed to lookup value for -limit");
-        return false;
-    }
+    PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
+    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
 
     // find all rawImfiles matching the default query
@@ -569,14 +461,4 @@
     }
 
-    bool simple = false;
-    {
-        bool status = false;
-        simple = psMetadataLookupBool(&status, config->args, "-simple");
-        if (!status) {
-            psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -simple");
-            return false;
-        }
-    }
-
     if (psArrayLength(output)) {
         if (!convertIdToStr(output)) {
@@ -599,19 +481,10 @@
 }
 
-
+    
 static bool addoverlapMode(pxConfig *config)
 {
     PS_ASSERT_PTR_NON_NULL(config, false);
 
-    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;
-    }
+    PXOPT_LOOKUP_STR(mapfile, config->args, "-mapfile", false, false);
 
     if (!psDBTransaction(config->dbh)) {
@@ -703,10 +576,6 @@
     PS_ASSERT_PTR_NON_NULL(config, false);
 
-    bool status = false;
-    psU64 limit = psMetadataLookupU64(&status, config->args, "-limit");
-    if (!status) {
-        psError(PXTOOLS_ERR_PROG, false, "failed to lookup value for -limit");
-        return false;
-    }
+    PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
+    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
 
     // find all rawImfiles matching the default query
@@ -757,14 +626,4 @@
     }
 
-    bool simple = false;
-    {
-        bool status = false;
-        simple = psMetadataLookupBool(&status, config->args, "-simple");
-        if (!status) {
-            psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -simple");
-            return false;
-        }
-    }
-
     if (psArrayLength(output)) {
         if (!convertIdToStr(output)) {
@@ -792,10 +651,6 @@
     PS_ASSERT_PTR_NON_NULL(config, false);
 
-    bool status = false;
-    psU64 limit = psMetadataLookupU64(&status, config->args, "-limit");
-    if (!status) {
-        psError(PXTOOLS_ERR_PROG, false, "failed to lookup value for -limit");
-        return false;
-    }
+    PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
+    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
 
     // find all rawImfiles matching the default query
@@ -846,14 +701,4 @@
     }
 
-    bool simple = false;
-    {
-        bool status = false;
-        simple = psMetadataLookupBool(&status, config->args, "-simple");
-        if (!status) {
-            psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -simple");
-            return false;
-        }
-    }
-
     if (psArrayLength(output)) {
         if (!convertIdToStr(output)) {
@@ -881,78 +726,18 @@
     PS_ASSERT_PTR_NON_NULL(config, false);
 
-    bool status = false;
-    psString warp_id = psMetadataLookupStr(&status, config->args, "-warp_id");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -warp_id");
-        return false;
-    }
-    if (!warp_id) {
-        psError(PS_ERR_UNKNOWN, true, "-warp_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;
-    }
-
-    psString path_base = psMetadataLookupStr(&status, config->args, "-path_base");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -path_base");
-        return false;
-    }
+    PXOPT_LOOKUP_STR(warp_id, config->args, "-warp_id", true, false);
+    PXOPT_LOOKUP_STR(skycell_id, config->args, "-skycell_id", true, false);
+    PXOPT_LOOKUP_STR(tess_id, config->args, "-tess_id", true, 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_stdev = psMetadataLookupF64(&status, config->args, "-bg_stdev");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -bg_stdev");
-        return false;
-    }
-
-    psF64 good_frac = psMetadataLookupF64(&status, config->args, "-good_frac");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -good_frac");
-        return false;
-    }
-
-    bool ignore = psMetadataLookupBool(&status, config->args, "-ignore");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -ignore");
-        return false;
-    }
+    PXOPT_LOOKUP_STR(uri, config->args, "-uri", false, false);
+    PXOPT_LOOKUP_STR(path_base, config->args, "-path_base", false, false);
+    PXOPT_LOOKUP_F64(bg, config->args, "-bg", false, false);
+    PXOPT_LOOKUP_F64(bg_stdev, config->args, "-bg_stdev", false, false);
+    PXOPT_LOOKUP_F64(good_frac, config->args, "-good_frac", false, false);
+    PXOPT_LOOKUP_BOOL(ignore, config->args, "-ignore", false);
 
     // default values
-    psS16 code = psMetadataLookupS16(&status, config->args, "-code");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -code");
-        return false;
-    }
+    PXOPT_LOOKUP_S16(code, config->args, "-code", false, false);
 
     // we don't want to insert the last skyfile in a run but then not mark the
@@ -1071,5 +856,5 @@
         "       USING(warp_id)\n"
         "   LEFT JOIN warpSkyfile\n"
-        "       USING(warp_id, skycell_id, tess_id)\n"
+        "       USING(warp_id, skycell_id)\n"
         "   WHERE\n"
         "       warpRun.state = 'run'\n"
@@ -1111,10 +896,6 @@
     PS_ASSERT_PTR_NON_NULL(config, false);
 
-    bool status = false;
-    psU64 limit = psMetadataLookupU64(&status, config->args, "-limit");
-    if (!status) {
-        psError(PXTOOLS_ERR_PROG, false, "failed to lookup value for -limit");
-        return false;
-    }
+    PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
+    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
 
     // find all rawImfiles matching the default query
@@ -1165,14 +946,4 @@
     }
 
-    bool simple = false;
-    {
-        bool status = false;
-        simple = psMetadataLookupBool(&status, config->args, "-simple");
-        if (!status) {
-            psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -simple");
-            return false;
-        }
-    }
-
     if (psArrayLength(output)) {
         if (!convertIdToStr(output)) {
