Index: trunk/ippScripts/Build.PL
===================================================================
--- trunk/ippScripts/Build.PL	(revision 34792)
+++ trunk/ippScripts/Build.PL	(revision 34800)
@@ -128,4 +128,5 @@
         scripts/listvideocells.pl
         scripts/skycalibration.pl
+        scripts/regenerate_background.pl
     )],
     dist_abstract => 'Scripts for running the Pan-STARRS IPP',
Index: trunk/ippScripts/MANIFEST
===================================================================
--- trunk/ippScripts/MANIFEST	(revision 34792)
+++ trunk/ippScripts/MANIFEST	(revision 34800)
@@ -46,3 +46,4 @@
 scripts/skycell_jpeg.pl
 scripts/lap_science.pl
+scripts/regenerate_background.pl
 t/00_distribution.t
Index: trunk/ippScripts/scripts/detrend_process_imfile.pl
===================================================================
--- trunk/ippScripts/scripts/detrend_process_imfile.pl	(revision 34792)
+++ trunk/ippScripts/scripts/detrend_process_imfile.pl	(revision 34800)
@@ -27,4 +27,6 @@
 my $ppImage = can_run('ppImage') or (warn "Can't find ppImage" and $missing_tools = 1);
 my $ppStatsFromMetadata = can_run('ppStatsFromMetadata') or (warn "Can't find ppStatsFromMetadata" and $missing_tools = 1);
+my $nebrepair = can_run('neb-repair') or (warn "Can't find neb-repair" and $missing_tools = 1);
+
 if ($missing_tools) {
     warn("Can't find required tools.");
@@ -124,4 +126,11 @@
 # Run ppImage
 unless ($no_op) {
+    my $repair_cmd = "$nebrepair $input_uri";
+    my ($repair_success, $repair_error_code, $repair_full_buf, $repair_stdout_buf, $repair_stderr_buf ) = run(command => $repair_cmd, verbose => $verbose);
+    unless ($repair_success) {
+	&my_die("Unable to attempt repair: $input_uri $repair_error_code", $det_id, $exp_id, $class_id $PS_EXIT_SYS_ERROR);
+    }
+
+
     my $command = "$ppImage -file $input_uri $outroot";
     $command .= " -recipe PPIMAGE $ppimage_recipe";
Index: trunk/ippScripts/scripts/regenerate_background.pl
===================================================================
--- trunk/ippScripts/scripts/regenerate_background.pl	(revision 34800)
+++ trunk/ippScripts/scripts/regenerate_background.pl	(revision 34800)
@@ -0,0 +1,358 @@
+#! /usr/bin/env perl
+
+# Script to call the appropriate commands to regenerate the background models
+# for stages subsequent to the chip stage.  
+
+use warnings;
+use strict;
+use Carp;
+
+use IPC::Cmd 0.36 qw( can_run run );
+use File::Spec;
+use File::Temp qw( tempfile );
+use PS::IPP::Config 1.01 qw( :standard );
+use Getopt::Long qw (GetOptions :config auto_help auto_version gnu_getopt );
+use Pod::Usage qw( pod2usage );
+use PS::IPP::Metadata::Config;
+use PS::IPP::Metadata::List qw( parse_md_list );
+
+
+
+my $ppConfigDump = can_run('ppConfigDump');
+my $pswarp       = can_run('pswarp');
+
+# Parse command line options.
+my ($stage, $camera, $stage_id, $skycell_id, $dbname, $verbose, $logfile, $save_temps);
+GetOptions('stage=s'         => \$stage,       # which analysis stage to generate bkg model
+	   'camera|i=s'      => \$camera,      # user-supplied camera name
+	   'stage_id=s'      => \$stage_id,    # id for this stage
+	   'skycell_id=s'    => \$skycell_id,  # skycell_id
+	   'verbose'         => \$verbose,     # verbose commands
+	   'logfile=s'       => \$logfile,     # destination for stdout and stderr  
+	   'save_temps'      => \$save_temps,  # save temporary files.
+	   'dbname=s'        => \$dbname       # database name
+    ) or pod2usage ( 2 );
+
+pod2usage( -msg => "Usage: $0 --camera (name) --stage (stage) --stage_id (stage_id) [--dbname (dbname)]",
+	   -exitval => 2 ) if scalar @ARGV;
+
+pod2usage( -msg => "Required options: --camera (name) --stage (stage) --stage_id (stage_id)",
+	   -exitval => 3 ) unless
+    defined $camera and
+    defined $stage and
+    defined $stage_id;
+
+pod2usage( -msg => "Required options: --camera (name) --stage (stage) --stage_id (stage_id) --skycell_id (skycell_id)",
+	   -exitval => 3) if (($stage eq 'warp')&&(!defined($skycell_id)));
+
+
+my $ipprc = PS::IPP::Config->new ( $camera ) or 
+    &my_die("Unable to set up", $stage, $stage_id, $PS_EXIT_CONFIG_ERROR); # used for path/neb info
+
+# We can only regenerate a background for certain stages.
+unless (($stage eq "warp") || ($stage eq "stack") || ($stage eq "warpstack")) {
+    &my_die("Invalid stage to regenerate", $stage, $stage_id, $PS_EXIT_CONFIG_ERROR);
+}
+
+$ipprc->redirect_output($logfile) or
+    &my_die("Unable to redirect output to logfile", $stage, $stage_id, $PS_EXIT_UNKNOWN_ERROR) if $logfile;
+
+my $mdcParser = PS::IPP::Metadata::Config->new; # parser for metadata config files
+
+
+## warp stage
+if ($stage eq 'warp') {
+    my $warp_id = $stage_id; # Because I think I used the wrong id in places.
+    &my_die("--stage_id required for stage warp", $stage, $stage_id, $PS_EXIT_CONFIG_ERROR) if !$stage_id;
+    &my_die("--skycell_id required for stage warp", $stage, $stage_id, $PS_EXIT_CONFIG_ERROR) if !$skycell_id;
+
+    # Configuration stuff
+    my $warptool = can_run('warptool') or 
+	&my_die("Can't find warptool",$stage,$stage_id, $PS_EXIT_UNKNOWN_ERROR);
+
+    my $astromSource;               # The astrometry source
+    {
+	my $command = "$ppConfigDump -camera $camera -recipe PSWARP BACKGROUND -dump-recipe PSWARP -";
+	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 ppConfigDump: $error_code", 
+		    $stage, $stage_id, $error_code);
+	}
+	my $metadata = $mdcParser->parse(join "", @$stdout_buf) or
+	    &my_die("Unable to parse metadata config doc", 
+		    $stage, $stage_id, $PS_EXIT_PROG_ERROR);
+	$astromSource = metadataLookupStr($metadata, 'ASTROM.SOURCE');
+    }
+
+    
+    my $imfiles;   # Array of component files
+    my $command = "$warptool -warped -warp_id $stage_id -skycell_id $skycell_id";
+    $command .= " -dbname $dbname " if defined $dbname;
+
+    my ( $success, $error_code, $full_buf, $stdout_buf, $stderrr_buf ) = 
+	run(command => $command, verbose => $verbose);
+    unless ($success) {
+	$error_code = (($error_code >> 8)  or $PS_EXIT_PROG_ERROR);
+	&my_die("Unable to perform warptool: $error_code", $stage, $stage_id, $error_code);
+    }
+
+    if (@$stdout_buf == 0) {
+	# No results
+    }
+
+    my $in_md = $mdcParser->parse(join "", @$stdout_buf) or
+	&my_die("Unable to parse metadata config doc", $stage, $stage_id, $PS_EXIT_PROG_ERROR);
+    $imfiles  = parse_md_list($in_md) or
+	&my_die("Unable to parse metadata", $stage, $stage_id, $PS_EXIT_PROG_ERROR);
+    
+    foreach my $imfile (@$imfiles) {
+	my $skycell_id = $imfile->{skycell_id};
+	my $path_base  = $imfile->{path_base};
+	my $tess_id    = $imfile->{tess_id};
+
+	# Parse the results to determine if there are any problems that prevent this from
+	# being processed.  I think I might need to think about this a bit more.
+	my $status = 1;
+	$status = 0 unless defined $path_base and $path_base ne "NULL";
+	my $poor_quality = $imfile->{quality} > 0;
+	
+# 	my $bkg_file = $ipprc->filename("PSWARP.OUTPUT.BKGMODEL",$path_base,$skycell_id);
+# 	my $bkg_exists = $ipprc->file_exists($bkg_file);
+	my $bkg_exists = $imfile->{background_model};
+	if ($bkg_exists) { $status = 0; } # We do not need to remake this.	
+
+	# Construct temp files
+	my ($bkgList_file,$bkgList_name) = tempfile("/tmp/bkgreg.warp.bkg.${warp_id}.${skycell_id}.XXXX",
+						    UNLINK => !$save_temps);
+	my ($astromList_file,$astromList_name) = tempfile("/tmp/bkgreg.warp.astrom.${warp_id}.${skycell_id}.XXXX",
+							 UNLINK => !$save_temps);
+	my $chipFiles;
+	{
+	    my $inputs_command = "$warptool -scmap -warp_id $warp_id ";
+	    $inputs_command   .= " -skycell_id $skycell_id ";
+	    $inputs_command   .= " -dbname $dbname " if defined $dbname;
+	    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+		run (command => $inputs_command, verbose => $verbose);
+	    unless ($success) {
+		$error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
+		&my_die("Unable to perform warptool -scmap: $error_code", 
+			$skycell_id,$warp_id,$PS_EXIT_SYS_ERROR);
+	    }
+	    my $md = $mdcParser->parse(join "", @$stdout_buf) or
+		&my_die("Unable to parse warptool -scmap: $error_code", 
+			$skycell_id,$warp_id,$PS_EXIT_SYS_ERROR);
+	    $chipFiles = parse_md_list($md) or
+		&my_die("Unable to parse warptool -scmap: $error_code", 
+			$skycell_id,$warp_id,$PS_EXIT_SYS_ERROR);
+	}
+	my $wrote_astrom = 0;
+	foreach my $chipFile (@$chipFiles) {
+	    my $bkgModel = $ipprc->filename("PSPHOT.BACKMDL",
+					    $chipFile->{chip_path_base},
+					    $chipFile->{class_id});
+	    my $astrom   = $ipprc->filename($astromSource,$chipFile->{cam_path_base});
+	    print $bkgList_file "$bkgModel\n";
+	    if (!$wrote_astrom) {
+		print $astromList_file "$astrom\n";
+		$wrote_astrom = 1;
+	    }
+	}
+	close ($bkgList_file);
+	close ($astromList_file);
+
+	# Construct skyfile if needed
+	my $skyCell_name = $ipprc->filename("SKYCELL.TEMPLATE",$path_base,$skycell_id);
+	my $skyCell_exists = $ipprc->file_exists($skyCell_name);
+	if (!$skyCell_exists) { # Generate it
+	    my $skyFile = prepare_output("SKYCELL.TEMPLATE", $path_base, $skycell_id, 1);
+	    $ipprc->skycell_file( $tess_id, $skycell_id, $skyFile, $verbose ) or
+		&my_die("Unable to generate template skycell",$skycell_id,$warp_id,$PS_EXIT_SYS_ERROR);
+	}
+	my $bkgOut_log = prepare_output("LOG.EXP",$path_base . ".BKG_REG",$skycell_id,1);
+
+	# Construct pswarp command
+	my $pswarp_command = "$pswarp ";
+	$pswarp_command .= " -list $bkgList_name -astromlist $astromList_name -bkglist $bkgList_name ";
+	$pswarp_command .= " ${path_base} ";
+	$pswarp_command .= " $skyCell_name ";
+	$pswarp_command .= " -F PSPHOT.PSF.SAVE PSPHOT.PSF.SKY.SAVE -F PSPHOT.OUTPUT PSPHOT.OUT.CMF.MEF ";
+	$pswarp_command .= " -F PSPHOT.BACKMDL PSPHOT.BACKMDL.MEF ";
+	$pswarp_command .= " -F SOURCE.PLOT.MOMENTS SOURCE.PLOT.SKY.MOMENTS ";
+	$pswarp_command .= " -F SOURCE.PLOT.PSFMODEL SOURCE.PLOT.SKY.PSFMODEL ";
+	$pswarp_command .= " -F SOURCE.PLOT.APRESID SOURCE.PLOT.SKY.APRESID ";
+	$pswarp_command .= " -recipe PSWARP WARP -Db PSF FALSE -Db BACKGROUND.MODEL TRUE ";
+	$pswarp_command .= " -Db SOURCES FALSE ";
+	$pswarp_command .= " -log $bkgOut_log -threads 1 ";
+	$pswarp_command .= " -dbname $dbname " if defined $dbname;
+
+	# Execute
+	my ( $success, $error_code, $full_buf, $stdout_buf, $stderrr_buf ) = 
+	    run(command => $pswarp_command, verbose => $verbose);
+	unless ($success) {
+	    $error_code = (($error_code >> 8)  or $PS_EXIT_PROG_ERROR);
+	    &my_die("Unable to perform pswarp: $error_code", $stage, $stage_id, $error_code);
+	}
+	
+	my $update_command = "$warptool ";
+	$update_command .= " -updateskyfile -warp_id $warp_id -skycell_id $skycell_id ";
+	$update_command .= " -set_background_model 1 ";
+	$update_command .= " -dbname $dbname " if defined $dbname;
+
+	( $success, $error_code, $full_buf, $stdout_buf, $stderrr_buf ) = 
+	    run(command => $update_command, verbose => $verbose);
+	unless ($success) {
+	    $error_code = (($error_code >> 8)  or $PS_EXIT_PROG_ERROR);
+	    &my_die("Unable to perform warptool: $error_code", $stage, $stage_id, $error_code);
+	}
+	
+
+    }
+}
+## stack stage
+elsif ($stage eq 'stack') {
+    &my_die("--stage_id required for stage stack", $stage, $stage_id, $PS_EXIT_CONFIG_ERROR) if !$stage_id;
+    my $stack_id = $stage_id; # Same as above.  Alias this so I don't make mistakes.
+    
+    # Configuration stuff
+    my $stacktool = can_run('stacktool') or
+	&my_die("Can't find stacktool",$stage,$stage_id,$PS_EXIT_UNKNOWN_ERROR);
+    my $ppStackMedian = can_run('ppStackMedian') or
+	&my_die("Can't find ppStackMedian",$stage,$stage_id,$PS_EXIT_UNKNOWN_ERROR);
+
+    # Get the information about this run
+    my $st_command = "$stacktool -sumskyfile -stack_id $stack_id";
+    $st_command   .= " -dbname $dbname " if defined $dbname;
+    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+	run(command => $st_command, verbose => $verbose);
+    unless($success) {
+	$error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
+	&my_die("Unable to perform stacktool -sumskyfile",$stage,$stage_id,$error_code);
+    }
+    if (@$stdout_buf == 0) {
+	# Nothing to do;
+    }
+    my $in_md = $mdcParser->parse(join "", @$stdout_buf) or
+        &my_die("Unable to parse metadata config doc", $stage, $stage_id, $PS_EXIT_PROG_ERROR);
+    my $stacks  = parse_md_list($in_md) or
+        &my_die("Unable to parse metadata", $stage, $stage_id, $PS_EXIT_PROG_ERROR);
+    my $stack = shift(@$stacks);
+    my $path_base = $stack->{path_base};
+
+    # Get inputs
+    my $imfiles;  # Array of component files
+    my $command = "$stacktool -inputskyfile -stack_id $stack_id";
+    $command .= " -dbname $dbname " if defined $dbname;
+    ( $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 stacktool -inputskyfile",$stage,$stage_id,$error_code);
+    }
+    if (@$stdout_buf == 0) {
+	# Nothing to do;
+    }
+    $in_md = $mdcParser->parse(join "", @$stdout_buf) or
+        &my_die("Unable to parse metadata config doc", $stage, $stage_id, $PS_EXIT_PROG_ERROR);
+    $imfiles  = parse_md_list($in_md) or
+        &my_die("Unable to parse metadata", $stage, $stage_id, $PS_EXIT_PROG_ERROR);
+
+    my $num = 0;
+    my $expTime;
+    my ($inputMDC_file, $inputMDC_name) = tempfile("/tmp/bkgreg.stack.mdc.${stage_id}.XXXX",
+						   UNLINK => !$save_temps);
+    foreach my $imfile (@$imfiles) {
+	if ($imfile->{ignored}) {next; }
+	unless ($imfile->{background_model}) {
+	    &my_die("Not all inputs have valid background models",
+		    $stage,$stage_id,$PS_EXIT_PROG_ERROR);
+	}
+	print $inputMDC_file "INPUT${num}\tMETADATA\n";
+	
+	print $inputMDC_file "\tIMAGE\tSTR\t" . $ipprc->filename( "PSWARP.OUTPUT.BKGMODEL",
+								  $imfile->{path_base} ) . "\n";
+	print $inputMDC_file "\tSOURCES\tSTR\t" . $ipprc->filename( "PSWARP.OUTPUT.SOURCES",
+								    $imfile->{path_base} ) . "\n";
+	print $inputMDC_file "END\n\n";
+	$num++;
+
+	$expTime = $imfile->{exp_time}; # I need something here.
+    }
+    close($inputMDC_file);
+
+    my $bkgOut_log = prepare_output("LOG.EXP",$path_base . ".BKG_REG",1);    
+
+    my $ppStack_command = "$ppStackMedian -input $inputMDC_name ${path_base}.mdl ";
+    $ppStack_command   .= " -recipe PPSTACK STACK -recipe PPSUB STACK -recipe PSPHOT STACK ";
+    $ppStack_command   .= " -recipe PPSTATS STACKSTATS -stack-type DEEP_STACK ";
+#    $ppStack_command   .= " -F PSPHOT.PSF.SAVE PSPHOT.PSF.SKY.SAVE ";
+#    $ppStack_command   .= " -F PSPHOT.OUTPUT PSPHOT.OUT.CMF.MEF -F PSPHOT.BACKMDL PSPHOT.BACKMDL.MEF ";
+#    $ppStack_command   .= " -F SOURCE.PLOT.MOMENTS SOURCE.PLOT.SKY.MOMENTS ";
+#    $ppStack_command   .= " -F SOURCE.PLOT.PSFMODEL SOURCE.PLOT.SKY.PSFMODEL ";
+#    $ppStack_command   .= " -F SOURCE.PLOT.APRESID SOURCE.PLOT.SKY.APRESID ";
+    $ppStack_command   .= " -Db PHOTOMETRY F -Db VARIANCE F -Db CONVOLVE F ";
+    $ppStack_command   .= " -Df DEFAULT.EXPTIME $expTime ";
+    $ppStack_command   .= " -threads 1 -log $bkgOut_log ";
+    $ppStack_command   .= " -dbname $dbname " if defined $dbname;
+
+    ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+	run(command => $ppStack_command, verbose => $verbose);
+    unless($success) {
+	$error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
+	&my_die("Unable to perform ppStackMedian",$stage,$stage_id,$error_code);
+    }
+
+    my $update_command = "$stacktool ";
+    $update_command .= " -updatesumskyfile -stack_id $stack_id ";
+    $update_command .= " -set_background_model 1 -fault 0 ";
+    $update_command .= " -dbname $dbname " if defined $dbname;
+    
+    ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) = 
+	run(command => $update_command, verbose => $verbose);
+    unless ($success) {
+	$error_code = (($error_code >> 8)  or $PS_EXIT_PROG_ERROR);
+	&my_die("Unable to perform stacktool: $error_code", $stage, $stage_id, $error_code);
+    }
+    
+
+
+}
+## do both warp and stack
+elsif ($stage eq 'warpstack') {
+
+}
+
+sub prepare_output
+{
+    my $filerule = shift;
+    my $outroot  = shift;
+    my $skycell_id = shift;
+    my $delete = shift;
+    $delete = 0 if !defined $delete;
+
+    my $error;
+    my $output = $ipprc->prepare_output($filerule, $outroot, $skycell_id, $delete, \$error)
+	or &my_die("failed to prepare output file for: $filerule", $skycell_id, $stage_id, $error);
+    return $output;
+}
+
+
+sub my_die {
+    my $msg = shift;       # Warning message on die
+    my $stage = shift;     # stage name
+    my $stage_id = shift;  # identifier
+    my $exit_code = shift; # exit code
+
+    carp($msg);
+    exit $exit_code;
+}
+
+
+
+
+	
+    
+
+
+
Index: trunk/ippScripts/scripts/skycell_jpeg.pl
===================================================================
--- trunk/ippScripts/scripts/skycell_jpeg.pl	(revision 34792)
+++ trunk/ippScripts/scripts/skycell_jpeg.pl	(revision 34800)
@@ -268,4 +268,12 @@
 
     my %tangents = ();
+
+    my %products = ('image' => "PPSTACK.UNCONV",
+		    'mask'  => "PPSTACK.UNCONV.MASK",
+		    'variance' => "PPSTACK.UNCONV.VARIANCE",
+		    'exp'   => "PPSTACK.UNCONV.EXP",
+		    'num'   => "PPSTACK.UNCONV.EXPNUM",
+		    'bkg'   => "PPSTACK.OUTPUT.BKGMODEL"
+	);
     
     foreach my $imfile (@$imfiles) {
@@ -281,42 +289,51 @@
 
 	$projection_cell =~ s/^(.*)\..*$/$1/;
-	
+
 	unless (exists($tangents{$projection_cell})) {
 	    # Make a temp file and fill, but be sure to save 
-	    ($tempFile, $tempName) = tempfile("/tmp/skycell.$projection_cell.XXXX",
-						 UNLINK => !$save_temps);
-	    $tangents{$projection_cell}{FILE} = $tempFile;
-	    $tangents{$projection_cell}{NAME} = $tempName;
-	    if ($masks) {
-		my ($maskFile, $maskName) = tempfile("/tmp/skycell.$projection_cell.masks.XXXX",
-						     UNLINK => !$save_temps);
-		$tangents{$projection_cell}{MFILE} = $maskFile;
-		$tangents{$projection_cell}{MNAME} = $maskName;
-	    }		
-	}
-	print "$skycell_id $projection_cell\n";	
-	my $file = $ipprc->filename("PPSTACK.OUTPUT", $path_base, $skycell_id);
-	print "$file $state $quality\n";
-	my $f_fh = $tangents{$projection_cell}{FILE};
-	print $f_fh "$file\n";
-	if ($masks) {
-	    my $mask = $ipprc->filename("PPSTACK.OUTPUT.MASK", $path_base, $skycell_id);
-	    print "$mask\n";
-	    my $m_fh = $tangents{$projection_cell}{MFILE};
-	    print $m_fh "$mask\n";
+	    foreach my $key (keys %products) {
+		($tempFile, $tempName) = tempfile("/tmp/skycell.$projection_cell.$key.XXXX",
+						  UNLINK => !$save_temps);
+		$tangents{$projection_cell}{$key}{FILE} = $tempFile;
+		$tangents{$projection_cell}{$key}{NAME} = $tempName;
+	    }
+	}
+	foreach my $key (keys %products) {
+	    print "$skycell_id $projection_cell\n";	
+	    my $file = $ipprc->filename($products{$key}, $path_base, $skycell_id);
+	    print "$file $state $quality\n";
+	    my $f_fh = $tangents{$projection_cell}{$key}{FILE};
+	    print $f_fh "$file\n";
 	}
     }
     foreach my $projection_cell (keys %tangents) {
-	$command = "$ppSkycell -images $tangents{$projection_cell}{NAME}";
-	if ($masks) {
-	    $command .= " -masks $tangents{$projection_cell}{MNAME} ";
-	}
-	$command .= " ${outroot}.${projection_cell} ";
-	print "$command\n";
-	( $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 ppSkycell: $error_code", $stage_id, $error_code);
-	}
+	## Loop over results here.
+	# Images
+	# Masks
+	# Variances
+	# Nexptime
+	# Nexp
+	# Backgrounds
+	
+	foreach my $key (keys %products) {
+	    $command = "$ppSkycell -images $tangents{$projection_cell}{$key}{NAME}";
+	    $command .= " ${outroot}.${projection_cell}.${key} ";
+	    if ($key eq 'bkg') {
+		$command .= " -Di BIN1 1 -Di BIN2 1 ";
+	    }
+	    elsif ($key eq 'image') {
+		$command .= " -masks $tangents{$projection_cell}{mask}{NAME} ";
+	    }
+	    elsif ($key eq 'mask') { 
+		next; # This should be made with the images.
+	    }
+	    print "$command\n";
+	    ( $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 ppSkycell: $error_code", $stage_id, $error_code);
+	    }
+	}
+
 	# Update database:
 	$command = "$stacktool -addsummary -sass_id $stage_id -projection_cell $projection_cell -path_base $outroot";
Index: trunk/ippScripts/scripts/stack_skycell.pl
===================================================================
--- trunk/ippScripts/scripts/stack_skycell.pl	(revision 34792)
+++ trunk/ippScripts/scripts/stack_skycell.pl	(revision 34800)
@@ -229,4 +229,6 @@
     my $sources = $ipprc->filename("PSWARP.OUTPUT.SOURCES", $file->{path_base}); # Sources name
 
+    my $bkgmodel = $ipprc->filename("PSWARP.OUTPUT.BKGMODEL", $file->{path_base});
+
     &my_die("Image $image does not exist", $stack_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists( $image );
     &my_die("Mask $mask does not exist", $stack_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists( $mask );
@@ -240,4 +242,5 @@
     print $listFile "\tPSF\tSTR\t" . $psf . "\n" if $convolve;
     print $listFile "\tSOURCES\tSTR\t" . $sources . "\n";
+    print $listFile "\tBKGMODEL\tSTR\t" . $bkgmodel . "\n" if $ipprc->file_exists( $bkgmodel );
 
     print $listFile "END\n\n";
Index: trunk/ippScripts/scripts/warp_skycell.pl
===================================================================
--- trunk/ippScripts/scripts/warp_skycell.pl	(revision 34792)
+++ trunk/ippScripts/scripts/warp_skycell.pl	(revision 34800)
@@ -128,4 +128,5 @@
 # Where do we get the astrometry source from?
 my $astromSource;               # The astrometry source
+my $doBackground;               # Do we want to make background models?
 {
     my $command = "$ppConfigDump -camera $camera -recipe PSWARP $recipe_pswarp -dump-recipe PSWARP -";
@@ -139,4 +140,5 @@
         &my_die("Unable to parse metadata config doc", $warp_id, $skycell_id, $tess_dir, $PS_EXIT_PROG_ERROR);
     $astromSource = metadataLookupStr($metadata, 'ASTROM.SOURCE');
+    $doBackground = metadataLookupBool($metadata, 'BACKGROUND.MODEL');    
 }
 
@@ -168,4 +170,8 @@
 if ($do_stats) {
     $outputStats = prepare_output ("SKYCELL.STATS", $outroot, $skycell_id, 1) if $do_stats;
+}
+my $outputBKGs;
+if ($doBackground) {
+    $outputBKGs = prepare_output ("PSWARP.OUTPUT.BKGMODEL", $outroot, $skycell_id, 1);
 }
 my $configuration;
@@ -209,5 +215,8 @@
 my ($weightFile, $weightName) = tempfile( "$tempOutRoot.weight.list.XXXX", UNLINK => !$save_temps);
 my ($astromFile, $astromName) = tempfile( "$tempOutRoot.astrom.list.XXXX", UNLINK => !$save_temps);
-
+my ($bkgFile, $bkgName);
+if ($doBackground) {
+    ($bkgFile, $bkgName) = tempfile( "$tempOutRoot.bkg.list.XXXX", UNLINK => !$save_temps);
+}
 my $wrote_astrom = 0;
 foreach my $imfile (@$imfiles) {
@@ -235,4 +244,9 @@
     print $maskFile   "$mask\n";
     print $weightFile "$weight\n";
+    my $bkg;
+    if ($doBackground) {
+	$bkg    = $ipprc->filename("PSPHOT.BACKMDL", $imfile->{chip_path_base}, $imfile->{class_id});
+	print $bkgFile "$bkg\n";
+    }
 
     if (!$wrote_astrom) {
@@ -245,5 +259,7 @@
 close $weightFile;
 close $astromFile;
-
+if ($doBackground) {
+    close($bkgFile);
+}
 # We need the recipe to determine if we care whether the PSF is generated or not
 my $recipe;
@@ -269,4 +285,5 @@
     $command .= " -variancelist $weightName";
     $command .= " -astromlist $astromName";
+    $command .= " -bkglist $bkgName" if ($doBackground);
     $command .= " $outroot $skyFile";
     $command .= " -F PSPHOT.PSF.SAVE PSPHOT.PSF.SKY.SAVE";
