Index: /trunk/ippTools/src/dettool.c
===================================================================
--- /trunk/ippTools/src/dettool.c	(revision 9945)
+++ /trunk/ippTools/src/dettool.c	(revision 9946)
@@ -806,90 +806,50 @@
     PS_ASSERT_PTR_NON_NULL(config, false);
 
-    // what type of detRun is this?
-    // det_type is required
-    psString det_type = psMetadataLookupStr(&status, config->args, "-det_type");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -det_type");
-        return false;
-    }
-    if (!det_type) {
-        psError(PS_ERR_UNKNOWN, true, "-det_type is required");
-        return false;
-    }
-
-    psMetadata *where = psMetadataAlloc();
+    // det_id is the only required input
+    psString det_id = psMetadataLookupStr(&status, config->args, "-det_id");
+    if (!status) {
+        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -det_id");
+        return false;
+    }
+    if (!det_id) {
+        psError(PS_ERR_UNKNOWN, true, "-det_id is required");
+        return false;
+    }
+    
+    // lookup the detRun that we will be basing this one on
+    psArray *detRuns = NULL;
     {
-        bool status = false;
-        psString exp_type = psMetadataLookupStr(&status, config->args, "-exp_type");        if (!status) {
-            psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -exp_type");
-            return false;
-        }
-        if (exp_type) {
-            if (!psMetadataAddStr(where, PS_LIST_TAIL, "exp_type", 0, "==", exp_type)) {
-                psError(PS_ERR_UNKNOWN, false, "failed to add item exp_type");
-                psFree(where);
-                return false;
-            }
-        }
-        // map -inst -> camera
-        psString camera = psMetadataLookupStr(&status, config->args, "-inst");
-        if (!status) {
-            psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -inst");
-            return false;
-        }
-        if (camera) {
-            if (!psMetadataAddStr(where, PS_LIST_TAIL, "camera", 0, "==", camera)) {
-                psError(PS_ERR_UNKNOWN, false, "failed to add item inst");
-                psFree(where);
-                return false;
-            }
-        }
-        psString telescope = psMetadataLookupStr(&status, config->args, "-telescope");        if (!status) {
-            psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -telescope");
-            return false;
-        }
-        if (telescope) {
-            if (!psMetadataAddStr(where, PS_LIST_TAIL, "telescope", 0, "==", telescope)) {
-                psError(PS_ERR_UNKNOWN, false, "failed to add item telescope");
-                psFree(where);
-                return false;
-            }
-        }
-        psString filter = psMetadataLookupStr(&status, config->args, "-filter");
-        if (!status) {
-            psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -filter");
-            return false;
-        }
-        if (filter) {
-            if (!psMetadataAddStr(where, PS_LIST_TAIL, "filter", 0, "==", filter)) {
-                psError(PS_ERR_UNKNOWN, false, "failed to add item filter");
-                psFree(where);
-                return false;
-            }
-        }
-        psString uri = psMetadataLookupStr(&status, config->args, "-uri");
-        if (!status) {
-            psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -filter");
-            return false;
-        }
-        if (uri) {
-            if (!psMetadataAddStr(where, PS_LIST_TAIL, "uri", 0, "==", uri)) {
-                psError(PS_ERR_UNKNOWN, false, "failed to add item uri");
-                psFree(where);
-                return false;
-            }
-        }
-    }
-    if (!psListLength(where->list)) {
+        psMetadata *where = psMetadataAlloc();
+        psMetadataAddS32(where, PS_LIST_TAIL, "det_id", 0, "==", (psS32)atoi(det_id));
+        detRuns = detRunSelectRowObjects(config->dbh, where, 0);
         psFree(where);
-        where = NULL;
-    }
-
-    // there is some namespace overlap between the names of the fields we'd
-    // like to search by to setup a detrun and the names of the fields we'd
-    // like to assign values to so I've seperated them but prepending set- to
-    // the assigned values
-
-    // optional
+    }
+    if (!detRuns) {
+        psError(PS_ERR_UNKNOWN, false, "database error");
+        return false;
+    }
+    // sanity check the result... we should have only found one det_id
+    if (psArrayLength(detRuns) != 1) {
+        psAbort(config->argv[0], "found more then one detRun matching det_id %d(this should not happen)", (psS32)atoi(det_id));
+        return false;
+    }
+   
+    // pull the detRun object out the result array
+    detRunRow *detRun = psMemIncrRefCounter(detRuns->data[0]);
+ 
+    // discard the resultarray
+    psFree(detRuns)
+
+    // set the det_id to 0/NULL so the database can assign it
+    detRun->det_id = 0;
+    
+    // reset the iteration to 0
+    detRun->iteration = 0;
+    
+    // reset the state to "run"
+    psFree(detRun->state);
+    detRun->state = psStringCopy("run");
+
+    // walk through the optional values and update the detRun as required
     psString exp_type = psMetadataLookupStr(&status, config->args, "-set_exp_type");
     if (!status) {
@@ -897,4 +857,7 @@
         return false;
     }
+    if (exp_type) {
+        detRun->exp_type = exp_type;
+    }
 
     psString filter = psMetadataLookupStr(&status, config->args, "-set_filter");
@@ -903,4 +866,7 @@
         return false;
     }
+    if (filter) {
+        detRun->filter = filter;
+    }
 
     psF32 airmass = psMetadataLookupF32(&status, config->args, "-set_airmass");
@@ -909,4 +875,7 @@
         return false;
     }
+    if (!isnan(airmass)) {
+        detRun->airmass = airmass;
+    }
 
     psF32 exp_time = psMetadataLookupF32(&status, config->args, "-set_exp_time");
@@ -915,4 +884,7 @@
         return false;
     }
+    if (!isnan(exp_time)) {
+        detRun->exp_time = exp_time;
+    }
 
     psF32 ccd_temp = psMetadataLookupF32(&status, config->args, "-set_ccd_temp");
@@ -921,4 +893,7 @@
         return false;
     }
+    if (!isnan(ccd_temp)) {
+        detRun->ccd_temp = ccd_temp;
+    }
 
     psF64 posang = psMetadataLookupF32(&status, config->args, "-set_posang");
@@ -927,4 +902,7 @@
         return false;
     }
+    if (!isnan(posang)) {
+        detRun->posang = posang;
+    }
 
     psString object = psMetadataLookupStr(&status, config->args, "-set_object");
@@ -933,4 +911,8 @@
         return false;
     }
+    if (object) {
+        detRun->object = object;
+    }
+
     psTime *registered = NULL;
     {
@@ -942,7 +924,8 @@
         // pass through NULL as this is an optional field
         if (registeredStr) {
+            psFree(registered);
             registered = psTimeFromISO(registeredStr, PS_TIME_UTC);
-        } else {
-            registered = NULL;
+            detRun->registered = psMemIncrRefCounter(registered);
+            psFree(registered);
         }
     }
@@ -957,7 +940,8 @@
         // pass through NULL as this is an optional field
         if (use_beginStr) {
+            psFree(use_begin);
             use_begin = psTimeFromISO(use_beginStr, PS_TIME_UTC);
-        } else {
-            use_begin = NULL;
+            detRun->use_begin = psMemIncrRefCounter(use_begin);
+            psFree(use_begin);
         }
     }
@@ -972,17 +956,9 @@
         // pass through NULL as this is an optional field
         if (use_endStr) {
+            psFree(use_end);
             use_end = psTimeFromISO(use_endStr, PS_TIME_UTC);
-        } else {
-            use_end = NULL;
-        }
-    }
-
-    // search for rawDetrendExps with the specified options
-    psArray *detrendExps = rawDetrendExpSelectRowObjects(config->dbh, where, 0);
-    psFree(where);
-    // make sure that we found at least one rawDetrendExp
-    if (!detrendExps) {
-        psError(PS_ERR_UNKNOWN, false, "no rawDetrendExp rows found");
-        return false;
+            detRun->use_end = psMemIncrRefCounter(use_end);
+            psFree(use_end);
+        }
     }
 
@@ -990,47 +966,9 @@
     if (!psDBTransaction(config->dbh)) {
         psError(PS_ERR_UNKNOWN, false, "database error");
-        psFree(detrendExps);
-        return false;
-    }
-
-    // the first iteration is always 0
-    // XXX det_id
-    detRunInsert(config->dbh,
-            0,
-            0,
-            det_type,
-            "run",
-            exp_type,
-            filter,
-            airmass,
-            exp_time,
-            ccd_temp,
-            posang,
-            object,
-            registered,
-            use_begin,
-            use_end
-        );
-    psFree(registered);
-    psFree(use_begin);
-    psFree(use_end);
-    long det_id = psDBLastInsertID(config->dbh);
-
-    // create new detInputExp row(s) from the rawDetrendExp row(s)
-    psArray *inputExps = psArrayAllocEmpty(psArrayLength(detrendExps));
-    for (long i = 0; i < psArrayLength(detrendExps); i++) {
-        detInputExpRow *inputExp = rawDetrenTodetInputExpRow(
-            detrendExps->data[i],
-            det_id,
-            0 // the first iteration is explicitly 0
-        );
-        psArrayAdd(inputExps, 0, inputExp);
-        psFree(inputExp);
-    }
-
-    psFree(detrendExps);
-
-    // insert detInputExp objects into the database
-    if (!detInputExpInsertObjects(config->dbh, inputExps)) {
+        psFree(detRun);
+        return false;
+    }
+
+    if (!detRunInsertObject(config->dbh, detRun)) {
         psError(PS_ERR_UNKNOWN, false, "database error");
         // rollback
@@ -1038,8 +976,26 @@
             psError(PS_ERR_UNKNOWN, false, "database error");
         }
-        psFree(inputExps);
-        return false;
-    }
-    psFree(inputExps);
+        psFree(detRun);
+        return false;
+    }
+    psFree(detRun);
+
+    // get the det_id 
+    long newDet_id = psDBLastInsertID(config->dbh);
+
+    char *query = 
+        "INSERT INTO detInputExp"
+        "   SELECT"
+        "       %d,"
+        "       0,"
+        "       detResidExp.exp_tag,"
+        "       detResidExp.accept"
+        "   FROM detResidExp"
+        "   WHERE det_id = %d";
+
+    if (!p_psDBRunQuery(config->dbh, query, (psS32)newDet_id, (psS32)atoi(det_id))) {
+        psError(PS_ERR_UNKNOWN, false, "database error");
+        return false;
+    }
 
     // point of no return for det_id creation
@@ -1059,9 +1015,10 @@
     }
 
+    {
     // print the new det_id
     psArray *detRuns = NULL;
     {
         psMetadata *where = psMetadataAlloc();
-        psMetadataAddS32(where, PS_LIST_TAIL, "det_id", 0, "==", det_id);
+        psMetadataAddS32(where, PS_LIST_TAIL, "det_id", 0, "==", newDet_id);
         detRuns = psDBSelectRows(config->dbh, "detRun", where, 0);
         psFree(where);
@@ -1071,7 +1028,7 @@
         return false;
     }
-    // sanity check results
+    // sanity check the result... we should have only found one det_id
     if (psArrayLength(detRuns) != 1) {
-        psAbort(config->argv[0], "found more then one detRun matching det_id %ld(this should not happen)", det_id);
+        psAbort(config->argv[0], "found more then one detRun matching det_id %ld(this should not happen)", newDet_id);
         return false;
     }
@@ -1091,4 +1048,5 @@
     }
     psFree(detRuns);
+    }
 
     return true;
Index: /trunk/ippTools/src/dettoolConfig.c
===================================================================
--- /trunk/ippTools/src/dettoolConfig.c	(revision 9945)
+++ /trunk/ippTools/src/dettoolConfig.c	(revision 9946)
@@ -93,16 +93,6 @@
     // -definebyquery
     psMetadata *definebyqueryArgs = psMetadataAlloc();
-    psMetadataAddStr(definebyqueryArgs, PS_LIST_TAIL, "-det_type",  0,
-            "define the type of detrend run (required)", NULL);
-    psMetadataAddStr(definebyqueryArgs, PS_LIST_TAIL, "-exp_type",  0,
-            "search by exposure type", NULL);
-    psMetadataAddStr(definebyqueryArgs, PS_LIST_TAIL, "-inst",  0,
-            "search by camera", NULL);
-    psMetadataAddStr(definebyqueryArgs, PS_LIST_TAIL, "-telescope",  0,
-            "search by telescope", NULL);
-    psMetadataAddStr(definebyqueryArgs, PS_LIST_TAIL, "-filter",  0,
-            "search by filter", NULL);
-    psMetadataAddStr(definebyqueryArgs, PS_LIST_TAIL, "-uri",  0,
-            "search by URL", NULL);
+    psMetadataAddStr(definebyqueryArgs, PS_LIST_TAIL, "-det_id",  0,
+            "det ID to base the new detrun on (required)", NULL);
     psMetadataAddStr(definebyqueryArgs, PS_LIST_TAIL, "-set_exp_type",  0,
         "define exposure type", NULL);
@@ -130,16 +120,6 @@
     // -definebydetrun
     psMetadata *definebydetrunArgs = psMetadataAlloc();
-    psMetadataAddStr(definebydetrunArgs, PS_LIST_TAIL, "-det_type",  0,
-            "define the type of detrend run (required)", NULL);
-    psMetadataAddStr(definebydetrunArgs, PS_LIST_TAIL, "-exp_type",  0,
-            "search by exposure type", NULL);
-    psMetadataAddStr(definebydetrunArgs, PS_LIST_TAIL, "-inst",  0,
-            "search by camera", NULL);
-    psMetadataAddStr(definebydetrunArgs, PS_LIST_TAIL, "-telescope",  0,
-            "search by telescope", NULL);
-    psMetadataAddStr(definebydetrunArgs, PS_LIST_TAIL, "-filter",  0,
-            "search by filter", NULL);
-    psMetadataAddStr(definebydetrunArgs, PS_LIST_TAIL, "-uri",  0,
-            "search by URL", NULL);
+    psMetadataAddStr(definebydetrunArgs, PS_LIST_TAIL, "-det_id",  0,
+            "det ID to base a new detRun on (required)", NULL);
     psMetadataAddStr(definebydetrunArgs, PS_LIST_TAIL, "-set_exp_type",  0,
         "define exposure type", NULL);
