Index: /trunk/ippScripts/scripts/diff_skycell.pl
===================================================================
--- /trunk/ippScripts/scripts/diff_skycell.pl	(revision 30825)
+++ /trunk/ippScripts/scripts/diff_skycell.pl	(revision 30826)
@@ -68,8 +68,19 @@
 my $ipprc = PS::IPP::Config->new() or my_die( "Unable to set up", $diff_id, $skycell_id, $PS_EXIT_CONFIG_ERROR ); # IPP configuration
 
-# XXX camera is not known here; cannot use filerules...
+my $neb;
+my $scheme = file_scheme($outroot);
+if ($scheme and $scheme eq 'neb') {
+    $neb = $ipprc->nebulous();
+}
+
+# XXX camera is not known here; cannot use filerules... 
+# bills 2011-03-07 This is unfortunate because it prevents us from using prepare_output ...
 # my $logDest = $ipprc->filename("LOG.EXP", $outroot);
 my $logDest = "$outroot.log";
-$logDest .= ".update" if $run_state eq "update";
+if ($run_state eq 'update') {
+    $logDest .= '.update';
+    # ... well we can just do the dirty work here
+    $ipprc->kill_file($logDest);
+}
 $ipprc->redirect_output($logDest) or my_die( "Unable to redirect output", $diff_id, $skycell_id, $PS_EXIT_SYS_ERROR ) if $redirect;
 
@@ -249,27 +260,41 @@
 
 # Get the output filenames
-my $outputName = $ipprc->filename("PPSUB.OUTPUT", $outroot);
-my $outputMask = $ipprc->filename("PPSUB.OUTPUT.MASK", $outroot);
-my $outputVariance = $ipprc->filename("PPSUB.OUTPUT.VARIANCE", $outroot);
-my $outputSources = $ipprc->filename("PPSUB.OUTPUT.SOURCES", $outroot);
-my $jpeg1Name = $ipprc->filename("PPSUB.OUTPUT.JPEG1", $outroot);
-my $jpeg2Name = $ipprc->filename("PPSUB.OUTPUT.JPEG2", $outroot);
-my $configuration = $ipprc->filename("PPSUB.CONFIG", $outroot);
-my $outputStats = $ipprc->filename("SKYCELL.STATS", $outroot);
-my $traceDest = $ipprc->filename("TRACE.EXP", $outroot);
-
-if ($run_state eq 'update') {
-    $traceDest .= '.update';
-    $outputStats .= '.update';
-
+my $configuration;
+my $outputStats;
+my $traceDest;
+my $do_photom = 1;
+my $dump_config = 1; 
+if ($run_state eq 'new') {
+    $configuration = prepare_output("PPSUB.CONFIG", $outroot, 1);
+    $traceDest = prepare_output("TRACE.EXP", $outroot, 1);
+    $outputStats = prepare_output("SKYCELL.STATS", $outroot, 1);
+} else {
+    $do_photom = 0;
+    $dump_config = 0;
+    $traceDest = prepare_output("TRACE.EXP.UPDATE", $outroot, 1);
+    # we rerun stats because we need the quality value
+    $outputStats = prepare_output("SKYCELL.STATS.UPDATE", $outroot, 1);
+    $configuration = $ipprc->filename("PPSUB.CONFIG", $outroot);
+    if (!$ipprc->file_exists($configuration)) {
+       print STDERR "WARNING: Config dump file $configuration is missing. Using current recipes and file rules.\n";
+       $configuration = undef;
+    }
+    # use WARPSTATS if we're updating, as we don't care about the new stuff from the STACK and DIFF STATS recipes
     $recipe_ppstats = 'WARPSTATS';
 }
+
+my $outputName = prepare_output("PPSUB.OUTPUT", $outroot, 1);
+my $outputMask = prepare_output("PPSUB.OUTPUT.MASK", $outroot, 1);
+my $outputVariance = prepare_output("PPSUB.OUTPUT.VARIANCE", $outroot, 1);
+my $outputSources = prepare_output("PPSUB.OUTPUT.SOURCES", $outroot, 1) if $do_photom;
+my $jpeg1Name = prepare_output("PPSUB.OUTPUT.JPEG1", $outroot, 1);
+my $jpeg2Name = prepare_output("PPSUB.OUTPUT.JPEG2", $outroot, 1);
 
 my ($inverseName, $inverseMask, $inverseVariance, $inverseSources);
 if ($inverse) {
-    $inverseName = $ipprc->filename("PPSUB.INVERSE", $outroot);
-    $inverseMask = $ipprc->filename("PPSUB.INVERSE.MASK", $outroot);
-    $inverseVariance = $ipprc->filename("PPSUB.INVERSE.VARIANCE", $outroot);
-    $inverseSources = $ipprc->filename("PPSUB.INVERSE.SOURCES", $outroot);
+    $inverseName = prepare_output("PPSUB.INVERSE", $outroot, 1);
+    $inverseMask = prepare_output("PPSUB.INVERSE.MASK", $outroot, 1);
+    $inverseVariance = prepare_output("PPSUB.INVERSE.VARIANCE", $outroot, 1);
+    $inverseSources = prepare_output("PPSUB.INVERSE.SOURCES", $outroot, 1) if $do_photom;
 }
 
@@ -289,9 +314,8 @@
     $command .= " -stats $outputStats";
     $command .= " -threads $threads" if defined $threads;
-    if ($run_state eq "new") {
+    if ($dump_config) {
         $command .= " -dumpconfig $configuration";
-    } else {
-        my $configurationReal = $ipprc->file_resolve($configuration) or &my_die("Couldn't resolve configuration file: $configuration", $diff_id, $skycell_id, $PS_EXIT_SYS_ERROR);
-        $command .= " -ipprc $configurationReal";
+    } elsif ($configuration) {
+        $command .= " -ipprc $configuration";
     }
     $command .= " -save-inconv" if defined $saveInConv and $run_state eq "new";
@@ -303,5 +327,5 @@
     $command .= " -F PSPHOT.OUTPUT PSPHOT.OUT.CMF.MEF";
     $command .= " -F PSPHOT.BACKMDL PSPHOT.BACKMDL.MEF";
-    if ($run_state eq "new") {
+    if ($do_photom) {
         $command .= " -photometry";
     } else {
@@ -322,6 +346,6 @@
         }
 
+        check_output($outputStats, 1);
         my $outputStatsReal = $ipprc->file_resolve($outputStats);
-        &my_die("Couldn't find expected output file: $outputStats", $diff_id, $skycell_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($outputStatsReal);
 
         # measure chip stats
@@ -341,17 +365,17 @@
 
         if (!$quality) {
-            &my_die("Couldn't find expected output file: $outputName", $diff_id, $skycell_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($outputName);
-            &my_die("Couldn't find expected output file: $outputMask", $diff_id, $skycell_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($outputMask);
-            &my_die("Couldn't find expected output file: $outputVariance", $diff_id, $skycell_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($outputVariance);
-            &my_die("Couldn't find expected output file: $outputSources", $diff_id, $skycell_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($outputSources);
-            &my_die("Couldn't find expected output file: $jpeg1Name",    $diff_id, $skycell_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($jpeg1Name);
-            &my_die("Couldn't find expected output file: $jpeg2Name",    $diff_id, $skycell_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($jpeg2Name);
+            check_output($outputName, 0);
+            check_output($outputMask, 0);
+            check_output($outputVariance, 0);
+            check_output($outputSources, 1) if $do_photom;
+            check_output($jpeg1Name, 1);
+            check_output($jpeg2Name, 1);
             if ($inverse) {
-                &my_die("Couldn't find expected output file: $inverseName", $diff_id, $skycell_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($inverseName);
-                &my_die("Couldn't find expected output file: $inverseMask", $diff_id, $skycell_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($inverseMask);
-                &my_die("Couldn't find expected output file: $inverseVariance", $diff_id, $skycell_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($inverseVariance);
-                &my_die("Couldn't find expected output file: $inverseSources", $diff_id, $skycell_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($inverseSources);
+                check_output($inverseName, 0);
+                check_output($inverseMask, 0);
+                check_output($inverseVariance, 0);
+                check_output($inverseSources, 1) if $do_photom;
                 if ($run_state eq 'new') {
-                    &my_die("Couldn't find expected output file: $configuration", $diff_id, $skycell_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($configuration);
+                    check_output($configuration, 1);
                 }
             }
@@ -391,4 +415,39 @@
     }
 }
+exit 0;
+
+
+# Prepare to write to an output file
+#   Lookup the filename in the rules.
+#   Make sure that if file exists and is a nebulous file that there is only one instance
+#   Deal with files that have been lost.
+sub prepare_output
+{
+    my $filerule = shift;
+    my $outroot  = shift;
+    my $delete = shift;
+    $delete = 0 if !defined $delete;
+
+    my $error;
+    my $output = $ipprc->prepare_output($filerule, $outroot, undef, $delete, \$error)
+                    or &my_die("failed to prepare output file for: $filerule", $diff_id, $skycell_id, $error);
+    return $output;
+}
+
+sub check_output
+{
+    my $file = shift;
+    my $replicate = shift;
+
+    if (!defined $file) {
+        return;
+    }
+
+    &my_die("Couldn't find expected output file: $file",  $diff_id, $skycell_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($file);
+
+    if ($replicate and $neb) {
+        $ipprc->replicate_file($file) or &my_die("failed to replicate: $file\n",  $diff_id, $skycell_id, $PS_EXIT_SYS_ERROR);
+    }
+}
 
 
