Index: /branches/end_stage/ippTools/src/detselect.c
===================================================================
--- /branches/end_stage/ippTools/src/detselect.c	(revision 16104)
+++ /branches/end_stage/ippTools/src/detselect.c	(revision 16105)
@@ -71,72 +71,48 @@
 }
 
+#define ADDRENAMEPARAMF(from, to, type, oldname, newname, comment) \
+{ \
+    bool status = false; \
+    ps##type var = psMetadataLookup##type(&status, from, oldname); \
+    if (!status) { \
+        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for " oldname); \
+        return false; \
+    } \
+    if (!isnan(var)) { \
+        if (!psMetadataAdd##type(to, PS_LIST_TAIL, newname, PS_META_DUPLICATE_OK, comment, var)) { \
+            psError(PS_ERR_UNKNOWN, false, "failed to add item " newname); \
+            psFree(to); \
+            return false; \
+        } \
+    } \
+}
+
+#define ADDPARAMF(from, to, type, name) \
+  ADDRENAMEPARAMF(from, to, type, name, name, "==")
+
+
 static bool searchMode(pxConfig *config)
 {
     PS_ASSERT_PTR_NON_NULL(config, false);
 
+    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
+    PXOPT_LOOKUP_BOOL(unlimit, config->args, "-unlimit", false);
+
     psMetadata *where = psMetadataAlloc();
 
     // airmass_min  < airmass  < airmass_max
-    {
-        bool status = false;
-        psF32 airmass = psMetadataLookupF32(&status, config->args, "-airmass");
-        if (!status) {
-            psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -airmass");
-            psFree(where);
-            return false;
-        }
-
-        if (!isnan(airmass)) {
-            psMetadataAddF32(where, PS_LIST_TAIL, "airmass_min", 0, "<=", airmass);
-            psMetadataAddF32(where, PS_LIST_TAIL, "airmass_max", 0, ">=", airmass);
-        }
-    }
+    ADDRENAMEPARAMF(config->args, where, F32, "-airmass", "airmass_min", "<=");
+    ADDRENAMEPARAMF(config->args, where, F32, "-airmass", "airmass_max", ">=");
 
     // exp_time_min < exp_time < exp_time_max
-    {
-        bool status = false;
-        psF32 exp_time = psMetadataLookupF32(&status, config->args, "-exp_time");
-        if (!status) {
-            psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -exp_time");
-            psFree(where);
-            return false;
-        }
-
-        if (!isnan(exp_time)) {
-            psMetadataAddF32(where, PS_LIST_TAIL, "exp_time_min", 0, "<=", exp_time);
-            psMetadataAddF32(where, PS_LIST_TAIL, "exp_time_max", 0, ">=", exp_time);
-        }
-    }
+    ADDRENAMEPARAMF(config->args, where, F32, "-exp_time", "exp_time_min", "<=");
+    ADDRENAMEPARAMF(config->args, where, F32, "-exp_time", "exp_time_max", ">=");
 
     // ccd_temp_min < ccd_temp < ccd_temp_max
-    {
-        bool status = false;
-        psF32 ccd_temp = psMetadataLookupF32(&status, config->args, "-ccd_temp");
-        if (!status) {
-            psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -ccd_temp");
-            psFree(where);
-            return false;
-        }
-
-        if (!isnan(ccd_temp)) {
-            psMetadataAddF32(where, PS_LIST_TAIL, "ccd_temp_min", 0, "<=", ccd_temp);
-            psMetadataAddF32(where, PS_LIST_TAIL, "ccd_temp_max", 0, ">=", ccd_temp);
-        }
-    }
-
-    {
-        bool status = false;
-        psF64 posang = psMetadataLookupF64(&status, config->args, "-posang");
-        if (!status) {
-            psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -posang");
-            psFree(where);
-            return false;
-        }
-
-        if (!isnan(posang)) {
-            psMetadataAddF32(where, PS_LIST_TAIL, "posang_min", 0, "<=", posang);
-            psMetadataAddF32(where, PS_LIST_TAIL, "posang_max", 0, ">=", posang);
-        }
-    }
+    ADDRENAMEPARAMF(config->args, where, F32, "-ccd_temp", "ccd_temp_min", "<=");
+    ADDRENAMEPARAMF(config->args, where, F32, "-ccd_temp", "ccd_temp_max", ">=");
+
+    ADDRENAMEPARAMF(config->args, where, F64, "-posang", "posang_min", "<=");
+    ADDRENAMEPARAMF(config->args, where, F64, "-posang", "posang_max", ">=");
 
     // time_begin    < time     < time_end
@@ -189,10 +165,10 @@
     }
 
-    // we choose the single detrend image which matches all criteria and has the latest
-    // insertion date
+    // we choose the single detrend image which matches all criteria and has
+    // the latest insertion date
 
     // unless explicitly specified by the user, list all possible matches
-    if (!psMetadataLookupBool(NULL, config->args, "-unlimit")) {
-	psStringAppend(&query, " ORDER BY registered DESC LIMIT 1");
+    if (!unlimit) {
+        psStringAppend(&query, " ORDER BY registered DESC LIMIT 1");
     }
 
@@ -216,14 +192,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;
-        }
-    }
-
     // negate simple so the default is true
     if (!ippdbPrintMetadatas(stdout, output, "detExp", !simple)) {
@@ -241,4 +207,6 @@
 {
     PS_ASSERT_PTR_NON_NULL(config, false);
+
+    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
 
     psString query = pxDataGet("detselect_select.sql");
@@ -274,14 +242,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;
-        }
-    }
-
     // negative simple so the default is true
     if (!ippdbPrintMetadatas(stdout, output, "detNormalizedImfile", !simple)) {
