Index: trunk/ippTools/src/detselect.c
===================================================================
--- trunk/ippTools/src/detselect.c	(revision 16170)
+++ trunk/ippTools/src/detselect.c	(revision 18561)
@@ -81,4 +81,10 @@
 
     psMetadata *where = psMetadataAlloc();
+
+    PXOPT_COPY_STR(config->args, where, "-inst",      "camera", "==");
+    PXOPT_COPY_STR(config->args, where, "-telescope", "telescope", "==");
+    PXOPT_COPY_STR(config->args, where, "-det_type",  "det_type", "==");
+    PXOPT_COPY_STR(config->args, where, "-type",      "det_type", "==");
+    PXOPT_COPY_STR(config->args, where, "-filter",    "filter", "==");
 
     // airmass_min  < airmass  < airmass_max
@@ -111,18 +117,11 @@
     }
 
-    if (config->where) {
-        psString whereClause = psDBGenerateWhereConditionSQL(config->where, NULL);
-        psStringAppend(&query, " AND %s", whereClause);
-        psFree(whereClause);
-    }
-
-    {
+    // use psDBGenerateWhereConditionalSQL with AND ... because the SQL ends in a WHERE
+    if (where && psListLength(where->list)) {
         psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
-        psFree(where);
-        if (whereClause) {
-            psStringAppend(&query, " AND %s", whereClause);
-            psFree(whereClause);
-        }
-    }
+	psStringAppend(&query, " AND %s", whereClause);
+	psFree(whereClause);
+    }
+    psFree(where);
 
     // we choose the single detrend image which matches all criteria and has
@@ -171,4 +170,9 @@
     PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
 
+    psMetadata *where = psMetadataAlloc();
+    PXOPT_COPY_STR(config->args, where, "-det_id",    "det_id", "==");
+    PXOPT_COPY_S32(config->args, where, "-iteration", "iteration", "==");
+    PXOPT_COPY_STR(config->args, where, "-class_id",  "class_id", "==");
+
     psString query = pxDataGet("detselect_select.sql");
     if (!query) {
@@ -177,10 +181,11 @@
     }
 
-
-    if (config->where) {
-        psString whereClause = psDBGenerateWhereConditionSQL(config->where, NULL);
+    // use psDBGenerateWhereConditionalSQL with AND ... because the SQL ends in a WHERE
+    if (where && psListLength(where->list)) {
+        psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
         psStringAppend(&query, " AND %s", whereClause);
         psFree(whereClause);
     }
+    psFree(where);
 
     if (!p_psDBRunQuery(config->dbh, query)) {
