Index: branches/eam_branches/ipp-20121219/ippScripts/scripts/camera_exp.pl
===================================================================
--- branches/eam_branches/ipp-20121219/ippScripts/scripts/camera_exp.pl	(revision 34903)
+++ branches/eam_branches/ipp-20121219/ippScripts/scripts/camera_exp.pl	(revision 35096)
@@ -41,5 +41,5 @@
 
 my ( $exp_tag, $cam_id, $camera, $outroot, $dbname, $reduction, $dvodb, $verbose, $no_update,
-     $no_op, $redirect, $save_temps, $run_state, $skip_binned, $skip_masks);
+     $no_op, $redirect, $save_temps, $run_state, $skip_binned, $skip_masks, $bkg_only);
 GetOptions(
     'exp_tag=s'         => \$exp_tag, # Exposure identifier
@@ -53,4 +53,5 @@
     'skip-binned'       => \$skip_binned, # override recipe - don't create binned images
     'skip-refmask'      => \$skip_masks, # override recipe - don't create refmask
+    'bkg-only'          => \$bkg_only,  # override recipe - only do background continuity
     'verbose'           => \$verbose,   # Print to stdout
     'no-update'         => \$no_update, # Update the database?
@@ -111,4 +112,5 @@
 &my_die("Unrecognised PSASTRO recipe", $cam_id, $PS_EXIT_CONFIG_ERROR) unless defined $recipe_psastro;
 
+my $bkg_recipe     = 'PPIMAGE_BKGCONT'; # Add to reduction?
 my $mdcParser = PS::IPP::Metadata::Config->new; # Parser for metadata config files
 
@@ -215,6 +217,10 @@
 my ($list4File, $list4Name) = tempfile( "/tmp/$exp_tag.cm.$cam_id.b4.list.XXXX", UNLINK => !$save_temps ); # For astrometry
 
+### Create temp file for background models
+my ($list5File, $list5Name) = tempfile( "/tmp/$exp_tag.cm.$cam_id.b5.list.XXXX", UNLINK => !$save_temps ); # For background models
+
 # XXX we perform astrometry iff photometry output exists
 my @outMasks;                   # Names of output masks
+my @bkg_models;                 # Names of output background models
 foreach my $file (@$files) {
     next if $file->{quality} != 0;
@@ -226,11 +232,13 @@
     my $chipObjects = $ipprc->filename("PSPHOT.OUTPUT", $file->{path_base}, $class_id);
     my $chipMask   = $ipprc->filename("PPIMAGE.CHIP.MASK", $file->{path_base}, $class_id);
-
+    
     print $list1File ($ipprc->filename("PPIMAGE.BIN1", $file->{path_base}, $class_id) . "\n");
     print $list2File ($ipprc->filename("PPIMAGE.BIN2", $file->{path_base}, $class_id) . "\n");
     print $list3File ($chipObjects . "\n");
     print $list4File ($chipMask . "\n");
+    print $list5File ($ipprc->filename("PSPHOT.BACKMDL", $file->{path_base}, $class_id) . "\n");
 
     push @outMasks, prepare_output("PSASTRO.OUTPUT.MASK", $outroot, $class_id, 1) if $produceMasks;
+    push @bkg_models, prepare_output("PPIMAGE.BACKMDL", $outroot, $class_id, 1);
 }
 close $list1File;
@@ -238,5 +246,5 @@
 close $list3File;
 close $list4File;
-
+close $list5File;
 # Output products
 $ipprc->outroot_prepare($outroot);
@@ -291,5 +299,5 @@
     }
 
-    {
+    if (!$bkg_only) {
         # run psastro on the chipObjects, producing fpaObjects
         my $command;
@@ -353,4 +361,22 @@
             }
         }
+    }
+    # Construct FPA continuity corrected background images
+    {
+	my $command;
+	$command = "$ppImage";
+	$command .= " -list $list5Name $outroot";
+	$command .= " -recipe PPIMAGE $bkg_recipe";
+	$command .= " -dbname $dbname" if defined $dbname;
+
+	my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+	    run(command => $command, verbose => $verbose);
+	unless ($success) {
+	    $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
+	    &my_die("Unable to perform ppImage to fix background: $error_code", $cam_id, $error_code);
+	}
+	foreach my $bkgModel (@bkg_models) {
+	    check_output($bkgModel, $replicateOutputs);
+	}
     }
 }
Index: branches/eam_branches/ipp-20121219/ippScripts/scripts/ipp_cleanup.pl
===================================================================
--- branches/eam_branches/ipp-20121219/ippScripts/scripts/ipp_cleanup.pl	(revision 34903)
+++ branches/eam_branches/ipp-20121219/ippScripts/scripts/ipp_cleanup.pl	(revision 35096)
@@ -65,4 +65,10 @@
         &my_die("Unable to redirect ouput", $stage, $stage_id, $PS_EXIT_UNKNOWN_ERROR) if $logfile;
 
+
+my $hostname = `hostname`;
+chomp $hostname;
+print STDERR "starting cleanup for $stage $stage_id on $hostname\n" ;
+
+my $nebulous_server;
 
 my $bzip2 = can_run('bzip2') or die 'cannot find bzip2\n';
@@ -2280,9 +2286,24 @@
     if (my $resolved = $ipprc->file_resolve($filename)) {
         my $bzip2_filename = $filename . '.bz2';
-        if ($ipprc->file_exists($bzip2_filename)) {
-            $ipprc->kill_file($bzip2_filename);
-        }
+	if (file_scheme($bzip2_filename) eq 'neb') {
+	    # XXX: GRRRR $ipprc->file_exists() returns false if storage object exists but instance has
+	    # size of zero
+	    $nebulous_server = $ipprc->nebulous() if !$nebulous_server;
+	    if ($nebulous_server->storage_object_exists($bzip2_filename)) {
+		print STDERR "$bzip2_filename exists, killing\n";
+                $ipprc->kill_file($bzip2_filename);
+	    } else {
+	        print STDERR "$bzip2_filename does not exist\n" if $very_verbose;
+	    }
+	} else {
+	    if ($ipprc->file_exists($bzip2_filename)) {
+	        print STDERR "$bzip2_filename exists, killing\n";
+	        $ipprc->kill_file($bzip2_filename);
+	    } else {
+	        print STDERR "$bzip2_filename does not exist\n" if $very_verbose;
+	    }
+	}
         my $bzip2_file = $ipprc->file_create($bzip2_filename);
-        my_die("Unable to create $bzip2_filename", $stage_id, $PS_EXIT_SYS_ERROR) unless $bzip2_file;
+        my_die("Unable to create $bzip2_filename", $stage_id, $stage_id, $PS_EXIT_SYS_ERROR) unless $bzip2_file;
 
         my $command = "$bzip2 < $resolved > $bzip2_file";
