Index: trunk/ippTools/src/dettool.c
===================================================================
--- trunk/ippTools/src/dettool.c	(revision 8291)
+++ trunk/ippTools/src/dettool.c	(revision 8293)
@@ -40,4 +40,5 @@
 static bool rerunMode(pxConfig *config);
 
+static bool mapPositionToDetRun(psArray *mds);
 static psArray *validDetInputClassIds(pxConfig *config, const char *det_id);
 static psArray *searchInputImfiles(pxConfig *config, const char *det_id);
@@ -308,15 +309,8 @@
 
     // map position -> det_id
-    // but leave position in the metadata as ippdbPrintMetadatas() will strip it
-    // for us
-    for (long i = 0; i < psArrayLength(detRuns); i++) {
-        bool status = false;
-        psS32 position = psMetadataLookupS32(&status, detRuns->data[i], "position");
-        if (!status) {
-            psError(PS_ERR_UNKNOWN, false, "failed to lookup value for position");
-            psFree(detRuns);
-            return false;
-        }
-        psMetadataAddS32(detRuns->data[i], PS_LIST_HEAD, "det_id", 0, NULL, position);
+    if (!mapPositionToDetRun(detRuns)) {
+        psError(PS_ERR_UNKNOWN, false, "failed to map position -> det_id");
+        psFree(detRuns);
+        return false;
     }
 
@@ -328,4 +322,22 @@
     }
     psFree(detRuns);
+
+    return true;
+}
+
+static bool mapPositionToDetRun(psArray *mds)
+{ 
+    PS_ASSERT_PTR_NON_NULL(mds, false);
+
+    for (long i = 0; i < psArrayLength(mds); i++) {
+        bool status = false;
+        psS32 position = psMetadataLookupS32(&status, mds->data[i], "position");
+        if (!status) {
+            psError(PS_ERR_UNKNOWN, false, "failed to lookup value for position");
+            psFree(mds);
+            return false;
+        }
+        psMetadataAddS32(mds->data[i], PS_LIST_HEAD, "det_id", 0, NULL, position);
+    }
 
     return true;
@@ -502,15 +514,8 @@
 
     // map position -> det_id
-    // but leave position in the metadata as ippdbPrintMetadatas() will strip it
-    // for us
-    for (long i = 0; i < psArrayLength(detRuns); i++) {
-        bool status = false;
-        psS32 position = psMetadataLookupS32(&status, detRuns->data[i], "position");
-        if (!status) {
-            psError(PS_ERR_UNKNOWN, false, "failed to lookup value for position");
-            psFree(detRuns);
-            return false;
-        }
-        psMetadataAddS32(detRuns->data[i], PS_LIST_HEAD, "det_id", 0, NULL, position);
+    if (!mapPositionToDetRun(detRuns)) {
+        psError(PS_ERR_UNKNOWN, false, "failed to map position -> det_id");
+        psFree(detRuns);
+        return false;
     }
 
@@ -538,53 +543,10 @@
     }
 
-    // XXX simplify this but just editing the psMetadata in the runs array
-    // print imfile list
-    psMetadata *output = psMetadataAlloc();
-    for (long i = 0; i < psArrayLength(runs); i++) {
-        psMetadata *run = runs->data[i];
-        psMetadata *md = psMetadataAlloc();
-        bool status = false;
-        // we need to change "position" to "det_id"
-        // lookup values
-        psS32 det_id = psMetadataLookupS32(&status, run, "position");
-        if (!status) {
-            psError(PS_ERR_UNKNOWN, false,
-                    "failed to lookup value for position");
-            psFree(output);
-            psFree(md);
-            psFree(runs);
-            return false;
-        }
-        psString det_type = psMetadataLookupStr(&status, run, "det_type");
-        if (!status) {
-            psError(PS_ERR_UNKNOWN, false,
-                    "failed to lookup value for det_type");
-            psFree(output);
-            psFree(md);
-            psFree(runs);
-            return false;
-        }
-        // add values
-        if (!psMetadataAddS32(md, PS_LIST_TAIL, "det_id", 0, NULL, det_id)) {
-            psError(PS_ERR_UNKNOWN, false, "failed to add item det_id");
-            psFree(output);
-            psFree(md);
-            psFree(runs);
-            return false;
-        }
-        if (!psMetadataAddStr(md, PS_LIST_TAIL, "det_type", 0, NULL, det_type)) {
-            psError(PS_ERR_UNKNOWN, false, "failed to add item det_type");
-            psFree(output);
-            psFree(md);
-            psFree(runs);
-            return false;
-        }
-        psMetadataAddMetadata(
-            output, PS_LIST_TAIL, "detRun", PS_META_DUPLICATE_OK, NULL, md
-        );
-        psFree(md);
-    }
-
-    psFree(runs);
+    // map position -> det_id
+    if (!mapPositionToDetRun(runs)) {
+        psError(PS_ERR_UNKNOWN, false, "failed to map position -> det_id");
+        psFree(runs);
+        return false;
+    }
 
     bool simple = false;
@@ -599,9 +561,11 @@
 
     // negative simple so the default is true
-    if (!ippdbPrintMetadata(stdout, output, !simple)) {
+    if (!ippdbPrintMetadatas(stdout, runs, "detRun", !simple)) {
         psError(PS_ERR_UNKNOWN, false, "failed to print array");
-        psFree(output);
-        return false;
-    }
+        psFree(runs);
+        return false;
+    }
+
+    psFree(runs);
 
     return true;
