Index: /trunk/ippTools/src/releasetool.c
===================================================================
--- /trunk/ippTools/src/releasetool.c	(revision 35459)
+++ /trunk/ippTools/src/releasetool.c	(revision 35460)
@@ -856,7 +856,24 @@
     }
 
+    psString joinConditions = psStringCopy("");
+    // Finish up the selectors for the JOIN to previousRelStack
+    // join to rows with our stack_type
+    psStringAppend(&joinConditions, "\nAND previousRelStack.stack_type = '%s'", stack_type);
+
+    if (!strcmp(stack_type, "nightly")) {
+        // detect nightly stack entries that already exist for this day
+        psStringAppend(&joinConditions, "\nAND previousRelStack.mjd_obs = floor(stackSumSkyfile.mjd_obs)");
+    }
+
+    // Add in the where conditions
     psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
-    psStringAppend(&query, " AND %s", whereClause);
+    psStringAppend(&query, "\nAND %s", whereClause);
     psFree(whereClause);
+
+    if (!strcmp(stack_type, "nightly")) {
+        // avoid stacks with NAN mjd_obs. These are old skycells that were lost due to system failure
+        // prior to the time that the mjd_obs was extracted from the headers and added to stackSumSkyfile
+        psStringAppend(&query, "\nAND stackSumSkyfile.mjd_obs IS NOT NULL");
+    }
 
     psFree(where);
@@ -868,10 +885,12 @@
     }
 
-    if (!p_psDBRunQuery(config->dbh, query)) {
+    if (!p_psDBRunQueryF(config->dbh, query, joinConditions)) {
         psError(PS_ERR_UNKNOWN, false, "database error");
         psFree(query);
+        psFree(joinConditions);
         return false;
     }
     psFree(query);
+    psFree(joinConditions);
 
     psArray *output = p_psDBFetchResult(config->dbh);
@@ -993,8 +1012,7 @@
     PXOPT_COPY_F32(config->args, where, "-mjd_min",    "stackSumSkyfile.mjd_obs", ">=");
     PXOPT_COPY_F32(config->args, where, "-mjd_max",    "stackSumSkyfile.mjd_obs", "<=");
-    PXOPT_COPY_F32(config->args, where, "-fwhm_min",    "skycalResult.fwhm_minor", ">=");
-    PXOPT_COPY_F32(config->args, where, "-fwhm_max",    "skycalresult.fwhm_major", "<=");
     PXOPT_COPY_STR(config->args, where, "-tess_id",     "relStack.tess_id", "==");
     PXOPT_COPY_STR(config->args, where, "-skycell_id",  "relStack.skycell_id", "LIKE");
+    PXOPT_COPY_STR(config->args, where, "-stack_type",  "relStack.stack_type", "==");
     PXOPT_COPY_STR(config->args, where, "-stack_data_group",  "stackRun.data_group", "LIKE");
     PXOPT_COPY_STR(config->args, where, "-skycal_data_group", "skycalRun.data_group", "LIKE");
@@ -1003,4 +1021,7 @@
     PXOPT_COPY_S32(config->args, where, "-rel_id",      "relExp.rel_id", "==");
     pxskycellAddWhere(config, where);
+
+    PXOPT_COPY_F32(config->args, where, "-fwhm_min",    "IFNULL(skycalResult.fwhm_major, 999)", ">=");
+    PXOPT_COPY_F32(config->args, where, "-fwhm_max",    "IFNULL(skycalResult.fwhm_major, 0)", "<=");
 
     PXOPT_LOOKUP_BOOL(priority_order, config->args, "-priority_order", false);
@@ -1495,5 +1516,5 @@
     PXOPT_COPY_TIME(config->args, where, "-dateobs_end", "rawExp.dateobs", "<=");
     PXOPT_COPY_F32(config->args, where, "-fwhm_min",    "skycalResult.fwhm_major", ">=");
-    PXOPT_COPY_F32(config->args, where, "-fwhm_max",    "skycalresult.fwhm_major", "<=");
+    PXOPT_COPY_F32(config->args, where, "-fwhm_max",    "skycalResult.fwhm_major", "<=");
     PXOPT_COPY_STR(config->args, where, "-exp_name",    "rawExp.exp_ame", "==");
     PXOPT_COPY_S64(config->args, where, "-exp_id",      "relExp.exp_id", "==");
Index: /trunk/ippTools/src/releasetoolConfig.c
===================================================================
--- /trunk/ippTools/src/releasetoolConfig.c	(revision 35459)
+++ /trunk/ippTools/src/releasetoolConfig.c	(revision 35460)
@@ -227,4 +227,5 @@
     psMetadataAddStr(listrelstackArgs,  PS_LIST_TAIL, "-tess_id", 0, "select by tess_id", NULL);
     psMetadataAddStr(listrelstackArgs,  PS_LIST_TAIL, "-skycell_id", 0, "select by skycell_id (LIKE comparision)", NULL);
+    psMetadataAddStr(listrelstackArgs,  PS_LIST_TAIL, "-stack_type", 0, "select by stack_type", NULL);
 
     psMetadataAddStr(listrelstackArgs,  PS_LIST_TAIL, "-stack_data_group", 0, "select by stackRun.data_group (LIKE comparison)", NULL);
