Index: trunk/ippScripts/scripts/stack_skycell.pl
===================================================================
--- trunk/ippScripts/scripts/stack_skycell.pl	(revision 30636)
+++ trunk/ippScripts/scripts/stack_skycell.pl	(revision 30840)
@@ -65,4 +65,31 @@
     and defined $run_state;
 
+# lists of file rules which we expect to produce output output
+my @outputList = qw(
+PPSTACK.OUTPUT
+PPSTACK.OUTPUT.MASK
+PPSTACK.OUTPUT.VARIANCE
+PPSTACK.OUTPUT.EXP          
+PPSTACK.OUTPUT.EXPNUM       
+PPSTACK.OUTPUT.EXPWT
+PPSTACK.TARGET.PSF          
+PPSTACK.OUTPUT.JPEG1        
+PPSTACK.OUTPUT.JPEG2        
+PPSTACK.CONFIG              
+);
+# produced if we run photometry
+my @outputListPhotom = qw(
+PSPHOT.OUT.CMF.MEF
+);
+# extra outputs when convolving
+my @outputListUnconv = qw(
+PPSTACK.UNCONV
+PPSTACK.UNCONV.MASK
+PPSTACK.UNCONV.VARIANCE
+PPSTACK.UNCONV.EXP          
+PPSTACK.UNCONV.EXPNUM       
+PPSTACK.UNCONV.EXPWT
+);
+
 my $ipprc = PS::IPP::Config->new() or my_die( "Unable to set up", $stack_id, $PS_EXIT_CONFIG_ERROR ); # IPP configuration
 $| = 1;
@@ -73,12 +100,16 @@
 my $logDest = "$outroot.log";
 
-my $do_stats;
-if ($run_state eq 'new') {
-    $do_stats = 1;
-} else {
+if ($run_state eq 'update') {
     $logDest .= ".update";
 }
 
 $ipprc->redirect_output($logDest) or my_die( "Unable to redirect output", $stack_id, $PS_EXIT_SYS_ERROR ) if $redirect;
+
+my $neb;
+my $scheme = file_scheme($outroot);
+if ($scheme and $scheme eq 'neb') {
+    $neb = $ipprc->nebulous();
+}
+
 
 my $temp_images_exist = 0;
@@ -173,5 +204,5 @@
 my $output_nocomp = metadataLookupBool($recipe, 'OUTPUT.NOCOMP'); # change filerules to produced uncompressed output images
 my $output_logflux = metadataLookupBool($recipe, 'OUTPUT.LOGFLUX'); # change filerules to produce logflux compressed output images.
-
+my $replicate_outputs = (defined($neb) and metadataLookupBool($recipe, 'OUTPUT.REPLICATE')); # replicate output images
 if ($output_nocomp and $output_logflux) {
     &my_die("Unable to not compress and logflux compress simultaneously. Check config.",$stack_id, $PS_EXIT_CONFIG_ERROR);
@@ -212,30 +243,35 @@
 print "I've checked everything: $stack_id\n";
 
-# Get the output filenames
-my $outputName = $ipprc->filename("PPSTACK.OUTPUT", $outroot);
-my $outputMask = $ipprc->filename("PPSTACK.OUTPUT.MASK", $outroot);
-my $outputWeight = $ipprc->filename("PPSTACK.OUTPUT.VARIANCE", $outroot);
-my $outputSources = $photometry ? $ipprc->filename("PSPHOT.OUT.CMF.MEF", $outroot) : undef; ## this must be consistent with the value in diff_skycell.pl:101
+# Prepare the output files
+my @outputFiles;
+prepare_outputs(\@outputFiles, \@outputList, $outroot);
+prepare_outputs(\@outputFiles, \@outputListPhotom, $outroot) if $photometry;
+prepare_outputs(\@outputFiles, \@outputListUnconv, $outroot) if $convolve;
+
+# we need the output image name for the database
+my $outputName = $outputFiles[0];
 
 ## use an explicit stack name for psphot output objects
-#my $bin1Name =  $ipprc->filename("PPSTACK.BIN1", $outroot);
-#my $bin2Name =  $ipprc->filename("PPSTACK.BIN2", $outroot);
-my $outputStats = $ipprc->filename("SKYCELL.STATS", $outroot);
-my $traceDest = $ipprc->filename("TRACE.EXP", $outroot);
-$traceDest .= ".update" if $run_state eq 'update';
-my $configuration = $ipprc->filename("PPSTACK.CONFIG", $outroot);
+my $do_stats = 1;   
+my $outputStats;
+my $traceDest;
+if ($run_state eq 'new') {
+    $outputStats = prepare_output("SKYCELL.STATS", $outroot, 1);
+    $traceDest = prepare_output("TRACE.EXP", $outroot, 1);
+} else {
+    # we need to do stats regardless because we need the quality flag
+    # $do_stats = 0;
+    $outputStats = prepare_output("SKYCELL.STATS.UPDATE", $outroot, 1) if $do_stats;
+    $traceDest = prepare_output("TRACE.EXP.UPDATE", $outroot, 1);
+}
+my $configuration = prepare_output("PPSTACK.CONFIG", $outroot, 1);
 $temp_images_exist = 1;  # failures after this point should attempt to delete the temp images
 
-# for update we need to resolve the config filename here because the code that reads it
-# doesn't know how to resolve paths. (The information is stored in the configuration file)
-$configuration = $ipprc->file_resolve($configuration) if ($run_state eq 'update');
-
 my $cmdflags;
-
 
 # Perform stacking
 unless ($no_op) {
     my $command = "$ppStack -input $listName $outroot";
-    $command .= " -stats $outputStats" if $do_stats;;
+    $command .= " -stats $outputStats" if $do_stats;
     $command .= " -recipe PPSTACK $recipe_ppStack";
     $command .= " -recipe PPSUB $recipe_ppSub";
@@ -311,15 +347,9 @@
 
     if (!$quality) {
-        &my_die("Couldn't find expected output file: $outputName", $stack_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($outputName);
-        &my_die("Couldn't find expected output file: $outputMask", $stack_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($outputMask);
-        &my_die("Couldn't find expected output file: $outputWeight", $stack_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($outputWeight);
-        if ($photometry) {
-            &my_die("Couldn't find expected output file: $outputSources", $stack_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($outputSources);
-        }
+        check_outputs(\@outputFiles, $replicate_outputs);
         if ($run_state eq 'new') {
             &my_die("Couldn't find expected output file: $configuration", $stack_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($configuration);
-        }
-#       &my_die("Couldn't find expected output file: $bin1Name",    $stack_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($bin1Name);
-#       &my_die("Couldn't find expected output file: $bin2Name",    $stack_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($bin2Name);
+            $ipprc->replicate_file($outputStats) if $neb;
+        }
     }
 
@@ -357,5 +387,5 @@
 }
 
-print "I've updated teh database: $stack_id\n";
+print "I've updated the database: $stack_id\n";
 
 
@@ -423,4 +453,52 @@
     }
 }
+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", $stack_id, $error);
+    return $output;
+}
+
+sub prepare_outputs
+{
+    my $fileList = shift;
+    my $ruleList = shift;
+    my $outroot = shift;
+    
+    foreach my $rule (@$ruleList) {
+        push @$fileList, prepare_output($rule, $outroot, 1);
+    }
+}
+
+sub check_output
+{
+    my $file = shift;
+    my $replicate = shift;
+
+    if (!defined $file) {
+        return;
+    }
+
+    &my_die("Couldn't find expected output file: $file",  $stack_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",  $stack_id, $PS_EXIT_SYS_ERROR);
+    }
+}
+
+sub check_outputs
+{
+    my $fileList = shift;
+    my $replicate = shift;
+    foreach my $file (@$fileList) {
+        check_output($file, $replicate);
+    }
+}
 
 print "I've reached the end of processing with a code of $?: $stack_id\n";
@@ -432,3 +510,4 @@
 }
 
+
 __END__
