Index: trunk/ippTools/src/stacktool.c
===================================================================
--- trunk/ippTools/src/stacktool.c	(revision 19105)
+++ trunk/ippTools/src/stacktool.c	(revision 19106)
@@ -105,47 +105,46 @@
     PXOPT_LOOKUP_STR(workdir, config->args, "-workdir", true, false);
 
-    psMetadata *whereRE = psMetadataAlloc(); // rawExp where
-    psMetadata *whereWSF = psMetadataAlloc(); // warpSkyfile where
+    psMetadata *where = psMetadataAlloc();
     psMetadata *having = psMetadataAlloc(); // HAVING clause
-    // map -inst -> camera, etc
-    PXOPT_COPY_STR(config->args, whereRE, "-select_inst", "camera", "==");
-    PXOPT_COPY_STR(config->args, whereRE, "-select_telescope", "telescope", "==");
-    PXOPT_COPY_STR(config->args, whereRE, "-select_filter", "filter", "==");
-    PXOPT_COPY_STR(config->args, whereRE, "-select_uri", "uri", "==");
-    PXOPT_COPY_TIME(config->args, whereRE, "-select_dateobs_begin", "dateobs", ">=");
-    PXOPT_COPY_TIME(config->args, whereRE, "-select_dateobs_end", "dateobs", "<=");
-    PXOPT_COPY_F32(config->args, whereRE, "-select_airmass_min", "airmass", ">=");
-    PXOPT_COPY_F32(config->args, whereRE, "-select_airmass_max", "airmass", "<=");
-    PXOPT_COPY_F32(config->args, whereRE, "-select_sat_pixel_frac_max", "sat_pixel_frac", "<=");
-    PXOPT_COPY_F32(config->args, whereRE, "-select_exp_time_min", "exp_time", ">=");
-    PXOPT_COPY_F32(config->args, whereRE, "-select_exp_time_max", "exp_time", "<=");
-    PXOPT_COPY_F32(config->args, whereRE, "-select_ccd_temp_min", "ccd_temp", ">=");
-    PXOPT_COPY_F32(config->args, whereRE, "-select_ccd_temp_max", "ccd_temp", "<=");
-    PXOPT_COPY_F32(config->args, whereRE, "-select_posang_min", "posang", ">=");
-    PXOPT_COPY_F32(config->args, whereRE, "-select_posang_max", "posang", "<=");
-    PXOPT_COPY_F32(config->args, whereRE, "-select_solang_min", "solang", ">=");
-    PXOPT_COPY_F32(config->args, whereRE, "-select_solang_max", "solang", "<=");
-
-    PXOPT_COPY_STR(config->args, having, "-select_skycell_id", "skycell_id", "==");
+
+    // select based on properties of the raw exposures
+    PXOPT_COPY_STR(config->args,  where, "-select_inst",               "rawExp.camera", "==");
+    PXOPT_COPY_STR(config->args,  where, "-select_telescope",          "rawExp.telescope", "==");
+    PXOPT_COPY_STR(config->args,  where, "-select_filter",             "rawExp.filter", "==");
+    PXOPT_COPY_STR(config->args,  where, "-select_uri",                "rawExp.uri", "==");
+    PXOPT_COPY_TIME(config->args, where, "-select_dateobs_begin",      "rawExp.dateobs", ">=");
+    PXOPT_COPY_TIME(config->args, where, "-select_dateobs_end",        "rawExp.dateobs", "<=");
+    PXOPT_COPY_F32(config->args,  where, "-select_airmass_min",        "rawExp.airmass", ">=");
+    PXOPT_COPY_F32(config->args,  where, "-select_airmass_max",        "rawExp.airmass", "<=");
+    PXOPT_COPY_F32(config->args,  where, "-select_sat_pixel_frac_max", "rawExp.sat_pixel_frac", "<=");
+    PXOPT_COPY_F32(config->args,  where, "-select_exp_time_min",       "rawExp.exp_time", ">=");
+    PXOPT_COPY_F32(config->args,  where, "-select_exp_time_max",       "rawExp.exp_time", "<=");
+    PXOPT_COPY_F32(config->args,  where, "-select_ccd_temp_min",       "rawExp.ccd_temp", ">=");
+    PXOPT_COPY_F32(config->args,  where, "-select_ccd_temp_max",       "rawExp.ccd_temp", "<=");
+    PXOPT_COPY_F32(config->args,  where, "-select_posang_min",         "rawExp.posang", ">=");
+    PXOPT_COPY_F32(config->args,  where, "-select_posang_max",         "rawExp.posang", "<=");
+    PXOPT_COPY_F32(config->args,  where, "-select_solang_min",         "rawExp.solang", ">=");
+    PXOPT_COPY_F32(config->args,  where, "-select_solang_max",         "rawExp.solang", "<=");
+    PXOPT_COPY_STR(config->args,  where, "-select_exp_type",           "rawExp.exp_type", "==");
+    PXOPT_COPY_F32(config->args,  where, "-select_good_frac_min",      "warpSkyfile.good_frac", ">=");
+    PXOPT_COPY_STR(config->args,  where, "-select_skycell_id",         "warpSkyfile.skycell_id", "==");
+
+    // these are used to build the HAVING restriction
     PXOPT_COPY_S32(config->args, having, "-min_num", "num_warp", ">=");
 
-    PXOPT_LOOKUP_S32(randomLimit, config->args, "-random", false, false);
-
-    PXOPT_LOOKUP_S32(min_new, config->args, "-min_new", false, false);
-    PXOPT_LOOKUP_F32(min_frac, config->args, "-min_frac", false, false);
-
-    if (!psListLength(whereRE->list) && !psListLength(whereWSF->list) &&
-        !psMetadataLookupBool(NULL, config->args, "-all")) {
-        psFree(whereRE);
-        psFree(whereWSF);
-        psError(PXTOOLS_ERR_DATA, false, "search parameters are required");
-        return false;
-    }
-
-    // default
+    // other options applied outside of the WHERE
+    PXOPT_LOOKUP_S32(randomLimit, config->args, "-random",   false, false);
+    PXOPT_LOOKUP_S32(min_new,     config->args, "-min_new",  false, false);
+    PXOPT_LOOKUP_F32(min_frac,    config->args, "-min_frac", false, false);
+
     PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
     PXOPT_LOOKUP_TIME(registered, config->args, "-registered", false, false);
-    PXOPT_COPY_STR(config->args, whereRE, "-select_exp_type", "exp_type", "==");
-    PXOPT_COPY_F32(config->args, whereWSF, "-select_good_frac_min", "good_frac", ">=");
+
+    if (!psListLength(where->list) && 
+	!psMetadataLookupBool(NULL, config->args, "-all")) {
+        psFree(where);
+        psError(PXTOOLS_ERR_DATA, false, "search parameters are required");
+        return false;
+    }
 
     psString select = pxDataGet("stacktool_definebyquery_part1.sql");
@@ -155,17 +154,10 @@
     }
 
-    if (psListLength(whereRE->list)) {
-        psString whereClause = psDBGenerateWhereConditionSQL(whereRE, "rawExp");
+    if (psListLength(where->list)) {
+        psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
         psStringAppend(&select, " AND %s", whereClause);
         psFree(whereClause);
     }
-    psFree(whereRE);
-
-    if (psListLength(whereWSF->list)) {
-        psString whereClause = psDBGenerateWhereConditionSQL(whereWSF, "warpSkyfile");
-        psStringAppend(&select, " AND %s", whereClause);
-        psFree(whereClause);
-    }
-    psFree(whereWSF);
+    psFree(where);
 
     psString groupby = pxDataGet("stacktool_definebyquery_part2.sql");
@@ -206,5 +198,4 @@
     }
     psFree(having);
-
 
     if (!p_psDBRunQuery(config->dbh, select)) {
@@ -295,12 +286,12 @@
         // create a new stackRun for this stack
         stackRunRow *run = stackRunRowAlloc(
-	    0, 				// ID
-	    "new", 			// state
-	    workdir, 
-	    NULL, 			// workdir_state
-	    registered, 
-	    skycell_id, 
-	    tess_id, 
-	    filter);
+            0,                          // ID
+            "new",                      // state
+            workdir, 
+            NULL,                       // workdir_state
+            registered, 
+            skycell_id, 
+            tess_id, 
+            filter);
 
         if (!stackRunInsertObject(config->dbh, run)) {
@@ -333,4 +324,8 @@
         psFree(idString);
 
+	// XXX this insert uses a select to generate the list of warp_ids for the stack,
+	// we have applied a set of criteria above (WHERE) to select the relevant warps
+	// this insert needs to use exactly the same restrictions (race condition is probably not critical)
+	// the insert below seems to only restrict matches to the skycell, tess, and filter
         if ((randomLimit > 0 && !p_psDBRunQuery(config->dbh, thisInsert, skycell_id, tess_id, filter,
                                                 randomLimit)) ||
