Index: trunk/ippTools/src/regtool.c
===================================================================
--- trunk/ippTools/src/regtool.c	(revision 18399)
+++ trunk/ippTools/src/regtool.c	(revision 18401)
@@ -301,15 +301,23 @@
     PXOPT_LOOKUP_BOOL(faulted, config->args, "-faulted", false);
 
+    psMetadata *where = psMetadataAlloc();
+    PXOPT_COPY_S64(config->args, where,  "-exp_id", "exp_id", "==");
+    PXOPT_COPY_STR(config->args, where,  "-exp_name", "exp_name", "==");
+    PXOPT_COPY_STR(config->args, where,  "-class_id", "class_id", "==");
+    PXOPT_COPY_STR(config->args, where,  "-telescope", "telescope", "==");
+
     psString query = pxDataGet("regtool_processedimfile.sql");
     if (!query) {
         psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
-        return false;
-    }
-
-    if (config->where) {
-        psString whereClause = psDBGenerateWhereConditionSQL(config->where, "rawImfile");
+        psFree(where);
+        return false;
+    }
+
+    if (where) {
+        psString whereClause = psDBGenerateWhereConditionSQL(where, "rawImfile");
         psStringAppend(&query, " AND %s", whereClause);
         psFree(whereClause);
     }
+    psFree(where);
 
     if (faulted) {
@@ -792,54 +800,51 @@
     }
 
-    if (!config->where) {
-    config->where = psMetadataAlloc();
-    }
-
     // XX test this out; need to make this consistent with the list in regtoolConfig.c
-    PXOPT_COPY_S64(config->args, config->where,  "-exp_id", "exp_id", "==");
-    PXOPT_COPY_STR(config->args, config->where,  "-exp_name", "exp_name", "==");
-    PXOPT_COPY_STR(config->args, config->where,  "-inst", "camera", "==");
-    PXOPT_COPY_STR(config->args, config->where,  "-telescope", "telescope", "==");
-    PXOPT_COPY_TIME(config->args, config->where, "-dateobs_begin", "dateobs", ">=");
-    PXOPT_COPY_TIME(config->args, config->where, "-dateobs_end", "dateobs", "<=");
-    PXOPT_COPY_STR(config->args, config->where,  "-exp_tag", "exp_tag", "==");
-    PXOPT_COPY_STR(config->args, config->where,  "-exp_type", "exp_type", "==");
-    PXOPT_COPY_STR(config->args, config->where,  "-filelevel", "filelevel", "==");
-    PXOPT_COPY_STR(config->args, config->where,  "-reduction", "reduction", "==");
-    PXOPT_COPY_STR(config->args, config->where,  "-filter", "filter", "==");
-    PXOPT_COPY_F64(config->args, config->where,  "-airmass_min", "airmass", ">=");
-    PXOPT_COPY_F64(config->args, config->where,  "-airmass_max", "airmass", "<");
-    PXOPT_COPY_F64(config->args, config->where,  "-ra_min", "ra", ">=");
-    PXOPT_COPY_F64(config->args, config->where,  "-ra_max", "ra", "<");
-    PXOPT_COPY_F64(config->args, config->where,  "-decl_min", "decl", ">=");
-    PXOPT_COPY_F64(config->args, config->where,  "-decl_max", "decl", "<");
-    PXOPT_COPY_F32(config->args, config->where,  "-exp_time_min", "exp_time", ">=");
-    PXOPT_COPY_F32(config->args, config->where,  "-exp_time_max", "exp_time", "<");
-    PXOPT_COPY_F32(config->args, config->where,  "-sat_pixel_frac_min", "sat_pixel_frac", ">=");
-    PXOPT_COPY_F32(config->args, config->where,  "-sat_pixel_frac_max", "sat_pixel_frac", "<");
-    PXOPT_COPY_F64(config->args, config->where,  "-bg_min", "bg", ">=");
-    PXOPT_COPY_F64(config->args, config->where,  "-bg_max", "bg", "<");
-    PXOPT_COPY_F64(config->args, config->where,  "-bg_stdev_min", "bg_stdev", ">=");
-    PXOPT_COPY_F64(config->args, config->where,  "-bg_stdev_max", "bg_stdev", "<");
-    PXOPT_COPY_F64(config->args, config->where,  "-bg_mean_stdev_min", "bg_mean_stdev", ">=");
-    PXOPT_COPY_F64(config->args, config->where,  "-bg_mean_stdev_max", "bg_mean_stdev", "<");
-    PXOPT_COPY_F64(config->args, config->where,  "-alt_min", "alt", ">=");
-    PXOPT_COPY_F64(config->args, config->where,  "-alt_max", "alt", "<");
-    PXOPT_COPY_F64(config->args, config->where,  "-az_min", "az", ">=");
-    PXOPT_COPY_F64(config->args, config->where,  "-az_max", "az", "<");
-    PXOPT_COPY_F32(config->args, config->where,  "-ccd_temp_min", "ccd_temp", ">=");
-    PXOPT_COPY_F32(config->args, config->where,  "-ccd_temp_max", "ccd_temp", "<");
-    PXOPT_COPY_F64(config->args, config->where,  "-posang_min", "posang", ">=");
-    PXOPT_COPY_F64(config->args, config->where,  "-posang_max", "posang", "<");
-    PXOPT_COPY_STR(config->args, config->where,  "-object", "object", "==");
-    PXOPT_COPY_F32(config->args, config->where,  "-solang_min", "solang", ">=");
-    PXOPT_COPY_F32(config->args, config->where,  "-solang_max", "solang", "<");
-
-    // XXX where does config->where get set??
-    if (config->where) {
-        psString whereClause = psDBGenerateWhereConditionSQL(config->where, "rawExp");
+    psMetadata *where = psMetadataAlloc();
+    PXOPT_COPY_S64(config->args, where,  "-exp_id", "exp_id", "==");
+    PXOPT_COPY_STR(config->args, where,  "-exp_name", "exp_name", "==");
+    PXOPT_COPY_STR(config->args, where,  "-inst", "camera", "==");
+    PXOPT_COPY_STR(config->args, where,  "-telescope", "telescope", "==");
+    PXOPT_COPY_TIME(config->args, where, "-dateobs_begin", "dateobs", ">=");
+    PXOPT_COPY_TIME(config->args, where, "-dateobs_end", "dateobs", "<=");
+    PXOPT_COPY_STR(config->args, where,  "-exp_tag", "exp_tag", "==");
+    PXOPT_COPY_STR(config->args, where,  "-exp_type", "exp_type", "==");
+    PXOPT_COPY_STR(config->args, where,  "-filelevel", "filelevel", "==");
+    PXOPT_COPY_STR(config->args, where,  "-reduction", "reduction", "==");
+    PXOPT_COPY_STR(config->args, where,  "-filter", "filter", "==");
+    PXOPT_COPY_F64(config->args, where,  "-airmass_min", "airmass", ">=");
+    PXOPT_COPY_F64(config->args, where,  "-airmass_max", "airmass", "<");
+    PXOPT_COPY_F64(config->args, where,  "-ra_min", "ra", ">=");
+    PXOPT_COPY_F64(config->args, where,  "-ra_max", "ra", "<");
+    PXOPT_COPY_F64(config->args, where,  "-decl_min", "decl", ">=");
+    PXOPT_COPY_F64(config->args, where,  "-decl_max", "decl", "<");
+    PXOPT_COPY_F32(config->args, where,  "-exp_time_min", "exp_time", ">=");
+    PXOPT_COPY_F32(config->args, where,  "-exp_time_max", "exp_time", "<");
+    PXOPT_COPY_F32(config->args, where,  "-sat_pixel_frac_min", "sat_pixel_frac", ">=");
+    PXOPT_COPY_F32(config->args, where,  "-sat_pixel_frac_max", "sat_pixel_frac", "<");
+    PXOPT_COPY_F64(config->args, where,  "-bg_min", "bg", ">=");
+    PXOPT_COPY_F64(config->args, where,  "-bg_max", "bg", "<");
+    PXOPT_COPY_F64(config->args, where,  "-bg_stdev_min", "bg_stdev", ">=");
+    PXOPT_COPY_F64(config->args, where,  "-bg_stdev_max", "bg_stdev", "<");
+    PXOPT_COPY_F64(config->args, where,  "-bg_mean_stdev_min", "bg_mean_stdev", ">=");
+    PXOPT_COPY_F64(config->args, where,  "-bg_mean_stdev_max", "bg_mean_stdev", "<");
+    PXOPT_COPY_F64(config->args, where,  "-alt_min", "alt", ">=");
+    PXOPT_COPY_F64(config->args, where,  "-alt_max", "alt", "<");
+    PXOPT_COPY_F64(config->args, where,  "-az_min", "az", ">=");
+    PXOPT_COPY_F64(config->args, where,  "-az_max", "az", "<");
+    PXOPT_COPY_F32(config->args, where,  "-ccd_temp_min", "ccd_temp", ">=");
+    PXOPT_COPY_F32(config->args, where,  "-ccd_temp_max", "ccd_temp", "<");
+    PXOPT_COPY_F64(config->args, where,  "-posang_min", "posang", ">=");
+    PXOPT_COPY_F64(config->args, where,  "-posang_max", "posang", "<");
+    PXOPT_COPY_STR(config->args, where,  "-object", "object", "==");
+    PXOPT_COPY_F32(config->args, where,  "-solang_min", "solang", ">=");
+    PXOPT_COPY_F32(config->args, where,  "-solang_max", "solang", "<");
+
+    if (psListLength(where->list)) {
+        psString whereClause = psDBGenerateWhereConditionSQL(where, "rawExp");
         psStringAppend(&query, " AND %s", whereClause);
         psFree(whereClause);
     }
+    psFree(where);
 
     if (faulted) {
