Index: trunk/ippTools/src/detselect.c
===================================================================
--- trunk/ippTools/src/detselect.c	(revision 15857)
+++ trunk/ippTools/src/detselect.c	(revision 16170)
@@ -75,96 +75,33 @@
     PS_ASSERT_PTR_NON_NULL(config, false);
 
+    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
+    PXOPT_LOOKUP_BOOL(unlimit, config->args, "-unlimit", false);
+
+    PXOPT_LOOKUP_TIME(time, config->args, "-time", false, 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);
-        }
-    }
+    PXOPT_COPY_F32(config->args, where, "-airmass", "airmass_min", "<=");
+    PXOPT_COPY_F32(config->args, where, "-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);
-        }
-    }
+    PXOPT_COPY_F32(config->args, where, "-exp_time", "exp_time_min", "<=");
+    PXOPT_COPY_F32(config->args, where, "-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);
-        }
-    }
+    PXOPT_COPY_F32(config->args, where, "-ccd_temp", "ccd_temp_min", "<=");
+    PXOPT_COPY_F32(config->args, where, "-ccd_temp", "ccd_temp_max", ">=");
+
+    PXOPT_COPY_F64(config->args, where, "-posang", "posang_min", "<=");
+    PXOPT_COPY_F64(config->args, where, "-posang", "posang_max", ">=");
 
     // time_begin    < time     < time_end
-    {
-        bool status = false;
-        psString timeStr = psMetadataLookupStr(&status, config->args, "-time");
-        if (!status) {
-            psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -time");
-            psFree(where);
-            return false;
-        }
-
-        if (timeStr) {
-            psTime *time = psTimeFromISO(timeStr, PS_TIME_UTC);
-            if (!time) {
-                psError(PS_ERR_UNKNOWN, false, "error in time format %s", timeStr);
-                psFree(where);
-                return false;
-            }
-
-            // the == NULL tests invokes some psDB magic to make an OR
-            // conditional query
-            psMetadataAddTime(where, PS_LIST_TAIL, "time_begin", 0, "<=", time);
-            psMetadataAddTime(where, PS_LIST_TAIL, "time_begin", PS_META_DUPLICATE_OK, "==", NULL);
-            psMetadataAddTime(where, PS_LIST_TAIL, "time_end", 0, ">=", time);
-            psMetadataAddTime(where, PS_LIST_TAIL, "time_end", PS_META_DUPLICATE_OK, "==", NULL);
-            psFree(time);
-        }
-    }
+    // the == NULL tests invokes some psDB magic to make an OR
+    // conditional query
+    psMetadataAddTime(where, PS_LIST_TAIL, "time_begin", 0, "<=", time);
+    psMetadataAddTime(where, PS_LIST_TAIL, "time_begin", PS_META_DUPLICATE_OK, "==", NULL);
+    psMetadataAddTime(where, PS_LIST_TAIL, "time_end", 0, ">=", time);
+    psMetadataAddTime(where, PS_LIST_TAIL, "time_end", PS_META_DUPLICATE_OK, "==", NULL);
 
     psString query = pxDataGet("detselect_search.sql");
@@ -189,10 +126,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 +153,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 +168,6 @@
 {
     PS_ASSERT_PTR_NON_NULL(config, false);
+
+    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
 
     psString query = pxDataGet("detselect_select.sql");
@@ -274,14 +203,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)) {
