Index: /trunk/ippScripts/scripts/magic_destreak_revert.pl
===================================================================
--- /trunk/ippScripts/scripts/magic_destreak_revert.pl	(revision 25018)
+++ /trunk/ippScripts/scripts/magic_destreak_revert.pl	(revision 25019)
@@ -180,5 +180,6 @@
     $bsources = $ipprc->filename("PSWARP.OUTPUT.SOURCES", $backup_path_base);
 } elsif ($stage eq "diff") {
-    my $name = $inverse ? "PPSUB.INVERSE" : "PPSUB.OUTPUT"; # Base name for images
+    my $name = "PPSUB.OUTPUT";
+    $name = $inverse ? "PPSUB.INVERSE" : "PPSUB.OUTPUT"; # Base name for images
     $image  = $ipprc->filename($name, $path_base);
     $mask   = $ipprc->filename("$name.MASK", $path_base);
@@ -188,45 +189,20 @@
     $bmask   = $ipprc->filename("$name.MASK", $backup_path_base);
     $bweight = $ipprc->filename("$name.VARIANCE", $backup_path_base);
-    $bsources = $ipprc->filename("PPSUB.OUTPUT.SOURCES", $backup_path_base);
-}
-
-if ($image) {
-    revert_file($image, $bimage) or
-        &my_die("failed to restore image file", $magic_ds_id, $component, $PS_EXIT_CONFIG_ERROR);
-}
-
-if ($mask) {
-    if (!revert_file($mask, $bmask)) {
-        if ($stage eq 'chip') {
-            # don't fail if the mask file (from the camera stage) fails to revert.
-            # It probably doesn't exist
-            # XXX: Handle this properly
-            print STDERR "failure to revert mask file, ignored\n";
-        } else {
-            &my_die("failed to restore mask file", $magic_ds_id, $component, $PS_EXIT_CONFIG_ERROR);
-        }
-    }
-}
-
-if ($ch_mask) {
-    if (!revert_file($ch_mask, $bch_mask)) {
-        &my_die("failed to restore chip mask file", $magic_ds_id, $component, $PS_EXIT_CONFIG_ERROR);
-    }
-} 
-
-
-if ($weight) {
-    revert_file($weight, $bweight) or
-        &my_die("failed to restore variance image", $magic_ds_id, $component, $PS_EXIT_CONFIG_ERROR);
-}
-
-if ($sources) {
-    revert_file($sources, $bsources) or
-        &my_die("failed to restore sources file", $magic_ds_id, $component, $PS_EXIT_CONFIG_ERROR);
-}
-
-if ($astrom) {
-    revert_file($astrom, $bastrom) or
-        &my_die("failed to restore astrometry file", $magic_ds_id, $component, $PS_EXIT_CONFIG_ERROR);
+    $bsources = $ipprc->filename("$name.SOURCES", $backup_path_base);
+}
+
+revert_files($image, $mask, $weight, $sources, $astrom, $bimage, $bmask, $bweight, $bsources, $bastrom);
+
+if ($stage eq "diff") {
+    $name = "PPSUB.INVERSE";
+    $image  = $ipprc->filename($name, $path_base);
+    $mask   = $ipprc->filename("$name.MASK", $path_base);
+    $weight = $ipprc->filename("$name.VARIANCE", $path_base);
+    $sources = $ipprc->filename("PPSUB.OUTPUT.SOURCES", $path_base);
+    $bimage  = $ipprc->filename($name, $backup_path_base);
+    $bmask   = $ipprc->filename("$name.MASK", $backup_path_base);
+    $bweight = $ipprc->filename("$name.VARIANCE", $backup_path_base);
+    $bsources = $ipprc->filename("$name.SOURCES", $backup_path_base);
+    revert_files($image, $mask, $weight, $sources, undef, $bimage, $bmask, $bweight, $bsources, undef);
 }
 
@@ -254,4 +230,57 @@
 
 ### Pau.
+
+sub revert_files {
+    my $image = shift;
+    my $mask = shift;
+    my $weight = shift;
+    my $sources = shift;
+    my $astrom = shift;
+    my $bimage = shift;
+    my $bmask = shift;
+    my $bweight = shift;
+    my $bsources = shift;
+    my $bastrom = shift;
+
+    if ($image) {
+        revert_file($image, $bimage) or
+            &my_die("failed to restore image file", $magic_ds_id, $component, $PS_EXIT_CONFIG_ERROR);
+    }
+
+    if ($mask) {
+        if (!revert_file($mask, $bmask)) {
+            if ($stage eq 'chip') {
+                # don't fail if the mask file (from the camera stage) fails to revert.
+                # It probably doesn't exist
+                # XXX: Handle this properly
+                print STDERR "failure to revert mask file, ignored\n";
+            } else {
+                &my_die("failed to restore mask file", $magic_ds_id, $component, $PS_EXIT_CONFIG_ERROR);
+            }
+        }
+    }
+
+    if ($ch_mask) {
+        if (!revert_file($ch_mask, $bch_mask)) {
+            &my_die("failed to restore chip mask file", $magic_ds_id, $component, $PS_EXIT_CONFIG_ERROR);
+        }
+    } 
+
+
+    if ($weight) {
+        revert_file($weight, $bweight) or
+            &my_die("failed to restore variance image", $magic_ds_id, $component, $PS_EXIT_CONFIG_ERROR);
+    }
+
+    if ($sources) {
+        revert_file($sources, $bsources) or
+            &my_die("failed to restore sources file", $magic_ds_id, $component, $PS_EXIT_CONFIG_ERROR);
+    }
+
+    if ($astrom) {
+        revert_file($astrom, $bastrom) or
+            &my_die("failed to restore astrometry file", $magic_ds_id, $component, $PS_EXIT_CONFIG_ERROR);
+    }
+}
 
 sub check_keyword
