Index: branches/cleanup/ippTools/src/stacktool.c
===================================================================
--- branches/cleanup/ippTools/src/stacktool.c	(revision 17860)
+++ branches/cleanup/ippTools/src/stacktool.c	(revision 18153)
@@ -31,5 +31,5 @@
 #include "stacktool.h"
 
-static bool queueMode(pxConfig *config);
+static bool definebyqueryMode(pxConfig *config);
 static bool definerunMode(pxConfig *config);
 static bool updaterunMode(pxConfig *config);
@@ -61,5 +61,5 @@
 
     switch (config->mode) {
-        MODECASE(STACKTOOL_MODE_QUEUE,              queueMode);
+        MODECASE(STACKTOOL_MODE_DEFINEBYQUERY,              definebyqueryMode);
         MODECASE(STACKTOOL_MODE_DEFINERUN,          definerunMode);
         MODECASE(STACKTOOL_MODE_UPDATERUN,          updaterunMode);
@@ -92,5 +92,5 @@
 
 
-static bool queueMode(pxConfig *config)
+static bool definebyqueryMode(pxConfig *config)
 {
     PS_ASSERT_PTR_NON_NULL(config, false);
@@ -107,5 +107,5 @@
     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_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", "<=");
@@ -120,4 +120,6 @@
     PXOPT_COPY_F32(config->args, whereRE, "-select_solang_max", "solang", "<=");
     PXOPT_COPY_STR(config->args, whereWSF, "-select_skycell_id", "skycell_id", "==");
+    PXOPT_LOOKUP_BOOL(randomSubset, config->args, "-random_subset", false);
+    PXOPT_LOOKUP_S32(randomLimit, config->args, "-random_limit", false, false);
 
     if (!psListLength(whereRE->list) && !psListLength(whereWSF->list) &&
@@ -182,15 +184,77 @@
     }
 
+    // valid_warpids should be NULL unless randomSubset is specified
+    psVector *valid_warpids = NULL;
+    if (randomSubset && (randomLimit < psArrayLength(output))) {
+        // loop over the array of metadata and figure out which warp_id are in
+        // the result set
+        psArray *warps = psArrayAlloc(0);
+        for (long i = 0; i < psArrayLength(output); i++) {
+//            psMetadata *md = output->data[i];
+
+            // pull out the warp_id
+            bool status = false;
+//            psS64 warp_id = psMetadataLookupS64(&status, md, "warp_id");
+            if (!status) {
+                psError(PS_ERR_UNKNOWN, false, "failed to lookup warp_id");
+                psFree(output);
+                psFree(warps);
+                return NULL;
+            }
+
+//            psArrayAdd(warps, 0, warp_id);
+        }
+
+        // generate a random-valued vector, return an index sorted by the
+        // random values
+        psVector *randomVector = psVectorAlloc(psArrayLength(output), PS_TYPE_F32);
+        psRandom *rng = psRandomAlloc(PS_RANDOM_TAUS, 0);
+        for (int i = 0; i < randomVector->n; i++) {
+            randomVector->data.F32[i] = psRandomUniform(rng);
+        }
+        psVector *indexVector = psVectorSortIndex(NULL, randomVector);
+        psFree(randomVector);
+
+        // accept for first n of the sequence
+        valid_warpids = psVectorAlloc(randomLimit, PS_TYPE_S64);
+        for (int i = 0; i < randomLimit; i++ ){
+//            int j = indexVector->data.S32[i];
+//            valid_warpids->data[i] = psMemIncrRefCounter (warps->data[j]);
+        }
+        psFree(indexVector);
+        psFree(warps);
+    }
+
+
     psHash *stacks = psHashAlloc(psArrayLength(output));
 
-    // loop over the array of metadata
+    // loop over the array of metadata and build a hash keyed by tess_id &
+    // skycell_id
     for (long i = 0; i < psArrayLength(output); i++) {
         psMetadata *md = output->data[i];
 
-        // pull out the warp_id, skycell_id, & tess_id
         bool status;
+        if (valid_warpids) {
+//            psS64 warp_id = psMetadataLookupS64(&status, md, "warp_id");
+            if (!status) {
+                psError(PS_ERR_UNKNOWN, false, "failed to lookup warp_id");
+                psFree(valid_warpids);
+                psFree(output);
+                return NULL;
+            }
+
+            // is this a warp_id that should be left out of the stack set(s)?
+//            for (long i = 0; i < psArrayLength(valid_warpids); i++) {
+//                if (valid_warpids->data[i] == warp_id) {
+//                    continue;
+//                }
+//            }
+        }
+
+        // pull out the skycell_id, & tess_id
         psString skycell_id = psMetadataLookupStr(&status, md, "skycell_id");
         if (!status) {
             psError(PS_ERR_UNKNOWN, false, "failed to lookup skycell_id");
+            psFree(valid_warpids);
             psFree(output);
             return NULL;
@@ -200,4 +264,5 @@
         if (!status) {
             psError(PS_ERR_UNKNOWN, false, "failed to lookup tess_id");
+            psFree(valid_warpids);
             psFree(output);
             return NULL;
@@ -220,4 +285,5 @@
 
     }
+    psFree(valid_warpids);
     psFree(output);
 
