Index: trunk/PS-IPP-PStamp/lib/PS/IPP/PStamp/Job.pm
===================================================================
--- trunk/PS-IPP-PStamp/lib/PS/IPP/PStamp/Job.pm	(revision 29054)
+++ trunk/PS-IPP-PStamp/lib/PS/IPP/PStamp/Job.pm	(revision 29154)
@@ -287,4 +287,5 @@
             $choose_components = 1;
         }
+        $command .= " -template" if $inverse;
         $id_opt = $use_imfile_id ? "-diff_skyfile_id" : "-diff_id";
         $image_name  = "PPSUB.OUTPUT";
@@ -361,5 +362,5 @@
         # The image selectors are such that multiple runs my have be returned for the same exposure.
         # Return only the latest one.
-        $images = filterRuns($stage, $need_magic, $images, $verbose);
+        $images = filterRuns($stage, $need_magic, $images, $inverse, $verbose);
     }
     if ($choose_components) {
@@ -479,5 +480,5 @@
     my $listrun = 0;
     if ($byid) {
-        if ($skycell_id) {
+        if ($skycell_id and ($skycell_id ne 'all')) {
             $command .= " -diffskyfile -diff_id $id -skycell_id $skycell_id";
         } else {
@@ -1235,4 +1236,5 @@
     my $need_magic = shift;
     my $inputs     = shift;
+    my $inverse     = shift;
     my $verbose    = shift;
 
@@ -1254,5 +1256,14 @@
     my $printed = 0;
     foreach my $input (@$inputs) {
-        my $exp_id = $input->{exp_id};
+        my $exp_id;
+        if ($stage ne "diff") {
+            $exp_id = $input->{exp_id};
+        } elsif (!$inverse) {
+            $exp_id = $input->{exp_id_1};
+            $input->{exp_id} = $exp_id;
+        } else {
+            $exp_id = $input->{exp_id_2};
+            $input->{exp_id} = $exp_id;
+        }
         my $run_id = $input->{$id_name};
         my $magicked = $input->{magicked};  # this will be either stageRun.magicked or stage%file.magicked
@@ -1260,4 +1271,5 @@
 
         # can't process run in these states
+        # XXX: also goto_purged or goto_scrubbed or drop
         if (($state eq 'new') or ($state eq 'purged') or ($state eq 'scrubbed')) {
             print "skipping ${stage}Run $run_id for exp_id $exp_id in state $state\n";
