Index: /trunk/ippScripts/scripts/detrend_stack.pl
===================================================================
--- /trunk/ippScripts/scripts/detrend_stack.pl	(revision 9416)
+++ /trunk/ippScripts/scripts/detrend_stack.pl	(revision 9417)
@@ -61,8 +61,8 @@
 my $files;			# Array of files to be stacked
 {
-    my $command = "$dettool -tostack -det_id $det_id -class_id $class_id"; # Command to run
+    my $command = "$dettool -processedimfile -det_id $det_id -class_id $class_id -included"; # Command to run
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
 	run(command => $command, verbose => 1);
-    die "Unable to perform dettool -tostack: $error_code\n" if not $success;
+    die "Unable to perform dettool -processedimfile: $error_code\n" if not $success;
     my $metadata = $mdcParser->parse(join "", @$stdout_buf)
         or die "unable to parse metadata config doc";
Index: /trunk/ippTools/src/dettool.c
===================================================================
--- /trunk/ippTools/src/dettool.c	(revision 9416)
+++ /trunk/ippTools/src/dettool.c	(revision 9417)
@@ -1430,4 +1430,7 @@
         " JOIN detRun"
         "   ON detRun.position = detProcessedImfile.det_id"
+        " JOIN detInputExp"
+        "   ON detRun.position = detInputExp.det_id"
+        "   AND detRun.iteration = detInputExp.iteration"
         " WHERE"
         "   detRun.state = 'run'"
@@ -1438,4 +1441,17 @@
         psStringAppend(&query, " AND %s", whereClause);
         psFree(whereClause);
+    }
+
+    {
+        bool status = false;
+        bool included = psMetadataLookupBool(&status, config->args, "-included");
+        if (!status) {
+            psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -simple");
+            return false;
+        }
+        // restrict search to included imfiles
+        if (included) {
+            psStringAppend(&query, " AND detInputExp.include = 1");
+        }
     }
 
Index: /trunk/ippTools/src/dettoolConfig.c
===================================================================
--- /trunk/ippTools/src/dettoolConfig.c	(revision 9416)
+++ /trunk/ippTools/src/dettoolConfig.c	(revision 9417)
@@ -153,4 +153,6 @@
     psMetadataAddBool(processedimfileArgs, PS_LIST_TAIL, "-exp",  0,
         "restrict results to complete 'exposures'", false);
+    psMetadataAddBool(processedimfileArgs, PS_LIST_TAIL, "-included",  0,
+        "restrict results to exposures 'includeded' in the current iteration", false);
     psMetadataAddBool(processedimfileArgs, PS_LIST_TAIL, "-simple",  0,
         "use the simple output format", false);
