Index: /trunk/ippTools/src/dettool.c
===================================================================
--- /trunk/ippTools/src/dettool.c	(revision 18147)
+++ /trunk/ippTools/src/dettool.c	(revision 18148)
@@ -603,6 +603,6 @@
       psArray *subset = psArrayAlloc (randomLimit);
       for (int i = 0; i < randomLimit; i++ ){
-	int j = indexVector->data.S32[i];
-	subset->data[i] = psMemIncrRefCounter (detrendExps->data[j]);
+        int j = indexVector->data.S32[i];
+        subset->data[i] = psMemIncrRefCounter (detrendExps->data[j]);
       }
       psFree (detrendExps);
Index: /trunk/ippTools/src/stacktool.c
===================================================================
--- /trunk/ippTools/src/stacktool.c	(revision 18147)
+++ /trunk/ippTools/src/stacktool.c	(revision 18148)
@@ -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;
+            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(randdomVector);
+
+        // 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);
 
Index: /trunk/ippTools/src/stacktoolConfig.c
===================================================================
--- /trunk/ippTools/src/stacktoolConfig.c	(revision 18147)
+++ /trunk/ippTools/src/stacktoolConfig.c	(revision 18148)
@@ -91,4 +91,8 @@
     psMetadataAddF64(queueArgs, PS_LIST_TAIL, "-select_solang_max",  0,
             "define max solar angle", NAN);
+    psMetadataAddBool(definebyqueryArgs, PS_LIST_TAIL, "-random_subset",  0,
+            "use a random subset of elements", false);
+    psMetadataAddS32(definebyqueryArgs, PS_LIST_TAIL, "-random_limit",  0,
+            "use this number of random elements", 20);
 
     psMetadataAddBool(queueArgs, PS_LIST_TAIL, "-all",  0,
