Index: /branches/eam_branches/ipp-20230313/ippScripts/Build.PL
===================================================================
--- /branches/eam_branches/ipp-20230313/ippScripts/Build.PL	(revision 42559)
+++ /branches/eam_branches/ipp-20230313/ippScripts/Build.PL	(revision 42560)
@@ -156,4 +156,5 @@
         scripts/remote_md5sum.pl
         scripts/fpcamera_exp.pl
+        scripts/xcstack_skycell.pl
     )],
     dist_abstract => 'Scripts for running the Pan-STARRS IPP',
Index: /branches/eam_branches/ipp-20230313/ippScripts/scripts/stack_skycell.pl
===================================================================
--- /branches/eam_branches/ipp-20230313/ippScripts/scripts/stack_skycell.pl	(revision 42559)
+++ /branches/eam_branches/ipp-20230313/ippScripts/scripts/stack_skycell.pl	(revision 42560)
@@ -185,6 +185,5 @@
 my $recipe_psphot  = $ipprc->reduction($reduction, 'STACK_PSPHOT'); # Recipe to use for psphot
 my $recipe_ppstats = 'STACKSTATS';
-if ($
-run_state eq 'update') {
+if ($run_state eq 'update') {
      $recipe_ppstats = 'WARPSTATS';
 }
Index: /branches/eam_branches/ipp-20230313/ippScripts/scripts/xcstack_skycell.pl
===================================================================
--- /branches/eam_branches/ipp-20230313/ippScripts/scripts/xcstack_skycell.pl	(revision 42559)
+++ /branches/eam_branches/ipp-20230313/ippScripts/scripts/xcstack_skycell.pl	(revision 42560)
@@ -40,43 +40,43 @@
 }
 
-my ($xcstack_id, $dbname, $outroot, $debug, $run_state, $threads, $reduction, $verbose, $no_update, $no_op, $redirect, $save_temps, $delete_convolved_images);
+my ($xcstack_id, $dbname, $camera, $outroot, $debug, $run_state, $threads, $reduction, $verbose, $no_update, $no_op, $redirect, $save_temps);
 GetOptions(
-    'xcstack_id|d=s'    => \$xcstack<_id, # xcstack identifier
-    'dbname|d=s'        => \$dbname, # Database name
-    'outroot=s'         => \$outroot, # Output root name
-    'run-state=s'       => \$run_state,
-    'debug'             => \$debug,   # Print to stdout
-    'threads=s'         => \$threads,   # Number of threads to use for ppStack
-    'reduction=s'       => \$reduction, # Reduction class
-    'verbose'           => \$verbose,   # Print to stdout
-    'no-update'         => \$no_update, # Don't update the database?
-    'no-op'             => \$no_op, # Don't do any operations?
-    'redirect-output'   => \$redirect,
-    'save-temps'        => \$save_temps, # Save temporary files?
-    'delete-convolved'  => \$delete_convolved_images,
+    'xcstack_id|d=s'    => \$xcstack_id,  # xcstack identifier
+    'dbname|d=s'        => \$dbname,      # Database name
+    'camera|c=s'        => \$camera,      # Camera name
+    'outroot=s'         => \$outroot,     # Output root name
+    'run-state=s'       => \$run_state,   # new or update?
+    'debug'             => \$debug,       # Print to stdout
+    'threads=s'         => \$threads,     # Number of threads to use for ppStack
+    'reduction=s'       => \$reduction,   # Reduction class
+    'verbose'           => \$verbose,     # Print to stdout
+    'no-update'         => \$no_update,   # Don't update the database?
+    'no-op'             => \$no_op,       # Don't do any operations?
+    'redirect-output'   => \$redirect,    # send output to the logfile
+    'save-temps'        => \$save_temps,  # Save temporary files?
     ) or pod2usage( 2 );
 
 pod2usage( -msg => "Unknown option: @ARGV", -exitval => 2 ) if @ARGV;
 pod2usage(
-    -msg => "Required options: --xcstack_id --outroot --run-state",
+    -msg => "Required options: --xcstack_id --outroot --run-state --dbname --camera",
     -exitval => 3,
-    ) unless defined $xcstack_id
-    and defined $outroot
-    and defined $run_state;
+    ) unless defined $xcstack_id and defined $outroot and defined $run_state and defined $dbname and defined $camera;
 
 # lists of file rules which we expect to produce output when convolving
 my @outputList = qw(
-    PPXCSTACK.OUTPUT
-    PPXCSTACK.OUTPUT.MASK
-    PPXCSTACK.OUTPUT.VARIANCE
-    PPXCSTACK.OUTPUT.EXP          
-    PPXCSTACK.OUTPUT.EXPNUM       
-    PPXCSTACK.OUTPUT.EXPWT
-    PPXCSTACK.TARGET.PSF          
-    );
+    PPSTACK.UNCONV
+    PPSTACK.UNCONV.MASK
+    PPSTACK.UNCONV.VARIANCE
+    PPSTACK.UNCONV.EXP          
+    PPSTACK.UNCONV.EXPNUM       
+    PPSTACK.UNCONV.EXPWT
+    PPSTACK.CONFIG              
+);
 
 my $ipprc = PS::IPP::Config->new() or my_die( "Unable to set up", $xcstack_id, $PS_EXIT_CONFIG_ERROR ); # IPP configuration
 $| = 1;
 print "I've set up: $xcstack_id\n";
+
+$ipprc->define_camera($camera);
 
 # XXX camera is not known here; cannot use filerules...
@@ -96,122 +96,58 @@
 my $source_id = $ipprc->source_id($dbname, $PS_TABLE_ID_XCSTACK);
 
+# temporary file to list the inputs (in metadata config format)
+my ($listFile, $listName) = tempfile("/tmp/xcstk.$xcstack_id.list.XXXX", UNLINK => !$save_temps );
+my $num = 0;
+
 # Get list of cameras for this xcstack
 my $mdcParser = PS::IPP::Metadata::Config->new; # Parser for metadata config files
-my $cameras;
-{
-    my $command = "$xcstacktool -inputcameras -xcstack_id $xcstack_id";
-    $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 xcstacktool -inputcameras: $error_code", $xcstack_id, $error_code);
-    }
-
-    if (@$stdout_buf == 0) {
-        &my_die("No input cameras selected", $xcstack_id, $PS_EXIT_PROG_ERROR);
-    }
-    my $metadata = $mdcParser->parse(join "", @$stdout_buf) or &my_die("Unable to parse metadata config doc", $xcstack_id, $PS_EXIT_PROG_ERROR);
-    $cameras = parse_md_list($metadata) or &my_die("Unable to parse metadata list", $xcstack_id, $PS_EXIT_PROG_ERROR);
-}
-
-&my_die("no cameras listed for xcstack", $xcstack_id, $PS_EXIT_DATA_ERROR) unless scalar @$files > 0;
-
+my $cameras = &run_command_ipptool ("$xcstacktool -dbname $dbname -inputcameras -xcstack_id $xcstack_id");
+&my_die("no cameras listed for xcstack", $xcstack_id, $PS_EXIT_DATA_ERROR) unless scalar @$cameras > 0;
 print "I've loaded my cameras: $xcstack_id\n";
 
 # Gather the list of input stacks from each of the cameras.  There may be multiple stacks per camera
-foreach my $camera (@$cameras) {
+foreach my $cameraData (@$cameras) {
     # Get list of components for xcstacking
     my $mdcParser = PS::IPP::Metadata::Config->new; # Parser for metadata config files
 
-    $camera_name = $camera->{camera};
+    my $camera_name = $cameraData->{camera};
     
-    my $inputs;
-    {
-	my $command = "$xcstacktool -inputskyfile -xcstack_id $xcstack_id -xcamera $camera_name";
-	$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 xcstacktool -inputskyfile: $error_code", $xcstack_id, $error_code);
-	}
-
-	if (@$stdout_buf == 0) {
-	    &my_die("No input skyfiles selected", $xcstack_id, $PS_EXIT_PROG_ERROR);
-	}
-	my $metadata = $mdcParser->parse(join "", @$stdout_buf) or &my_die("Unable to parse metadata config doc", $xcstack_id, $PS_EXIT_PROG_ERROR);
-	$inputs = parse_md_list($metadata) or &my_die("Unable to parse metadata list", $xcstack_id, $PS_EXIT_PROG_ERROR);
-    }
+    my $inputs = &run_command_ipptool ("$xcstacktool -dbname $dbname -inputskyfile -xcstack_id $xcstack_id -xcamera $camera_name");
+    &my_die("no inputs for camera $camera_name", $xcstack_id, $PS_EXIT_DATA_ERROR) unless scalar @$inputs > 0;
 
     foreach my $input (@$inputs) {
-	$stack_id = $input->{stack_id};
-
-	my $files;
-	{
-	    my $command = "$stacktool -sumskyfile -dbname $camera_name -stack_id $stack_id";
-	    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 xcstacktool -inputskyfile: $error_code", $xcstack_id, $error_code);
-	    }
-
-	    if (@$stdout_buf == 0) {
-		&my_die("No input skyfiles selected", $xcstack_id, $PS_EXIT_PROG_ERROR);
-	    }
-	    my $metadata = $mdcParser->parse(join "", @$stdout_buf) or
-		&my_die("Unable to parse metadata config doc", $xcstack_id, $PS_EXIT_PROG_ERROR);
-	    $files = parse_md_list($metadata) or
-		&my_die("Unable to parse metadata list", $xcstack_id, $PS_EXIT_PROG_ERROR);
-	}
-
-	foreach my $file (@$files) {
-	    # check for the input files
-	    my $image    = $ipprc->filename("PPSTACK.OUTPUT", $file->{path_base} ); # Image name
-	    my $mask     = $ipprc->filename( "PPSTACK.OUTPUT.MASK", $file->{path_base} ); # Mask name
-	    my $weight   = $ipprc->filename( "PPSTACK.OUTPUT.VARIANCE", $file->{path_base} ); # Weight name
-	    my $sources  = $ipprc->filename("PPSTACK.OUTPUT.SOURCES", $file->{path_base}); # Sources name
-
-	    my $have_image   = $ipprc->file_exists( $image );
-	    my $have_mask    = $ipprc->file_exists( $mask );
-	    my $have_weight  = $ipprc->file_exists( $weight );
-	    my $have_sources = $ipprc->file_exists( $sources );
-
-	    if ($skip_missing_inputs) {
-		my $missing_inputs = 0;
-		unless ($have_image)  { print "WARNING: MISSING INPUT $image  \n"; $missing_inputs = 1; }
-		unless ($have_mask)   { print "WARNING: MISSING INPUT $mask   \n"; $missing_inputs = 1; }
-		unless ($have_weight) { print "WARNING: MISSING INPUT $weight \n"; $missing_inputs = 1; }
-		unless ($have_sources){ print "WARNING: MISSING INPUT $sources\n"; $missing_inputs = 1; }
-		if ($missing_inputs) { next; }
-	    } else {
-		&my_die("Image $image does not exist", $stack_id, $PS_EXIT_SYS_ERROR) unless $have_image;
-		&my_die("Mask $mask does not exist", $stack_id, $PS_EXIT_SYS_ERROR) unless $have_mask;
-		&my_die("Weight $weight does not exist", $stack_id, $PS_EXIT_SYS_ERROR) unless $have_weight;
-		&my_die("Sources $sources does not exist", $stack_id, $PS_EXIT_SYS_ERROR) unless $have_sources;
-	    }
-
-	    print $listFile "INPUT$num\tMETADATA\n";
-
-	    print $listFile "\tIMAGE\tSTR\t"    . $image    . "\n";
-	    print $listFile "\tMASK\tSTR\t"     . $mask     . "\n";
-	    print $listFile "\tVARIANCE\tSTR\t" . $weight   . "\n";
-	    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";
-	    $num++;
-	}
+	my $stack_id = $input->{stack_id};
+
+	# check for the input files
+	my $image    = $ipprc->filename("PPSTACK.UNCONV",          $input->{path_base}); # Image name
+	my $mask     = $ipprc->filename("PPSTACK.UNCONV.MASK",     $input->{path_base}); # Mask name
+	my $weight   = $ipprc->filename("PPSTACK.UNCONV.VARIANCE", $input->{path_base}); # Weight name
+	my $sources  = $ipprc->filename("PPSTACK.OUTPUT.SOURCES",  $input->{path_base}); # Sources name
+	
+	my $have_image   = $ipprc->file_exists( $image );
+	my $have_mask    = $ipprc->file_exists( $mask );
+	my $have_weight  = $ipprc->file_exists( $weight );
+	my $have_sources = $ipprc->file_exists( $sources );
+	
+	&my_die("Image $image does not exist", $stack_id, $PS_EXIT_SYS_ERROR) unless $have_image;
+	&my_die("Mask $mask does not exist", $stack_id, $PS_EXIT_SYS_ERROR) unless $have_mask;
+	&my_die("Weight $weight does not exist", $stack_id, $PS_EXIT_SYS_ERROR) unless $have_weight;
+	&my_die("Sources $sources does not exist", $stack_id, $PS_EXIT_SYS_ERROR) unless $have_sources;
+	
+	print $listFile "INPUT$num\tMETADATA\n";
+	
+	print $listFile "  IMAGE     STR " . $image    . "\n";
+	print $listFile "  MASK      STR " . $mask     . "\n";
+	print $listFile "  VARIANCE  STR " . $weight   . "\n";
+	print $listFile "  SOURCES   STR " . $sources  . "\n";
+	print $listFile "END\n\n";
+	$num++;
     }
 }
 close($listFile);
 
-&my_die("Xcstack list contains less than two elements", $xcstack_id, $PS_EXIT_DATA_ERROR) unless
-    scalar @$files >= 2;
+&my_die("input list contains less than two elements", $xcstack_id, $PS_EXIT_DATA_ERROR) unless scalar $num >= 2;
 
 print "I've loaded my inputs: $xcstack_id\n";
-
-&my_die("Can't find camera", $xcstack_id, $PS_EXIT_SYS_ERROR) unless defined $camera;
-$ipprc->define_camera($camera);
 
 print "I've configured everything: $xcstack_id\n";
@@ -219,27 +155,14 @@
 # Recipes to use based on reduction class
 $reduction = 'DEFAULT' unless defined $reduction;
-my $recipe_ppStack = $ipprc->reduction($reduction, 'STACK_PPSTACK'); # Recipe to use for ppStack
-unless ($recipe_ppStack) {
-    &my_die("Couldn't find selected reduction for STACK_PPSTACK: $reduction\n", $xcstack_id, $PS_EXIT_CONFIG_ERROR);
-}
-
-# XXX maybe have different reduction class name (XCSTACK?)
-my $recipe;
-{
-    my $command = "$ppConfigDump -camera $camera -recipe PPSTACK $recipe_ppStack -dump-recipe PPSTACK -";
-    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", $xcstack_id, $error_code);
-    }
-    $recipe = $mdcParser->parse(join "", @$stdout_buf) or
-	&my_die("Unable to parse metadata config doc", $xcstack_id, $PS_EXIT_PROG_ERROR);
-}
-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 $output_deepexp = metadataLookupBool($recipe, 'OUTPUT.DEEPEXP'); # change filerules to produce exptime map with more depth
-my $replicate_outputs = (defined($neb) and metadataLookupBool($recipe, 'OUTPUT.REPLICATE')); # replicate output images
-my $skip_missing_inputs = metadataLookupBool($recipe, 'SKIP.MISSING.INPUTS'); # ignore missing inputs 
+my $recipe_ppStack = $ipprc->reduction($reduction, 'XCSTACK_PPSTACK'); # Recipe to use for ppStack
+my $recipe_ppstats = 'STACKSTATS';
+&my_die("Couldn't find selected reduction for XCSTACK_PPSTACK: $reduction\n", $xcstack_id, $PS_EXIT_CONFIG_ERROR) unless ($recipe_ppStack);
+
+my $recipe = &run_command ("$ppConfigDump -camera $camera -recipe PPSTACK $recipe_ppStack -dump-recipe PPSTACK -");
+
+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 $output_deepexp      = metadataLookupBool($recipe, 'OUTPUT.DEEPEXP'); # change filerules to produce exptime map with more depth
+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.",$xcstack_id, $PS_EXIT_CONFIG_ERROR);
@@ -250,71 +173,9 @@
 my $stack_type = metadataLookupStr($recipe, 'STACK.TYPE');
 &my_die("STACK.TYPE not found in recipe. Check config.",$xcstack_id, $PS_EXIT_CONFIG_ERROR) unless $stack_type;
-## XXX not sure how stack type is used in ppStack
-
-## XXX first ask for the list of cameras relevant to this stack
-
-# Generate MDC file with the inputs
-my $tess_base = basename($tess_id);
-my ($listFile, $listName) = tempfile("/tmp/$tess_base.$skycell_id.stk.$xcstack_id.list.XXXX", UNLINK => !$save_temps );
-my $num = 0;
-my $inputSources;               # Sources to use as stamps
-foreach my $file (@$files) {
-    if ($file->{ignored}) { next; }
-
-    # check for the input files
-    my $image    = $ipprc->filename("PSWARP.OUTPUT", $file->{path_base} ); # Image name
-    my $mask     = $ipprc->filename( "PSWARP.OUTPUT.MASK", $file->{path_base} ); # Mask name
-    my $weight   = $ipprc->filename( "PSWARP.OUTPUT.VARIANCE", $file->{path_base} ); # Weight name
-    my $psf      = $convolve ? $ipprc->filename( "PSPHOT.PSF.SKY.SAVE", $file->{path_base} ) : undef; # PSF name
-    my $sources  = $ipprc->filename("PSWARP.OUTPUT.SOURCES", $file->{path_base}); # Sources name
-    my $bkgmodel = $ipprc->filename("PSWARP.OUTPUT.BKGMODEL", $file->{path_base});
-
-    my $have_image   = $ipprc->file_exists( $image );
-    my $have_mask    = $ipprc->file_exists( $mask );
-    my $have_weight  = $ipprc->file_exists( $weight );
-    my $have_sources = $ipprc->file_exists( $sources );
-    my $have_psf;
-    if ($convolve) { $have_psf = $ipprc->file_exists( $psf ); }
-
-    if ($skip_missing_inputs) {
-	my $missing_inputs = 0;
-	unless ($have_image)  { print "WARNING: MISSING INPUT $image  \n"; $missing_inputs = 1; }
-	unless ($have_mask)   { print "WARNING: MISSING INPUT $mask   \n"; $missing_inputs = 1; }
-	unless ($have_weight) { print "WARNING: MISSING INPUT $weight \n"; $missing_inputs = 1; }
-	unless ($have_sources){ print "WARNING: MISSING INPUT $sources\n"; $missing_inputs = 1; }
-	if ($convolve) { unless ($have_psf) { print "WARNING: MISSING INPUT $psf\n"; $missing_inputs = 1;}}
-	if ($missing_inputs) { next; }
-    } else {
-	&my_die("Image $image does not exist", $xcstack_id, $PS_EXIT_SYS_ERROR) unless $have_image;
-	&my_die("Mask $mask does not exist", $xcstack_id, $PS_EXIT_SYS_ERROR) unless $have_mask;
-	&my_die("Weight $weight does not exist", $xcstack_id, $PS_EXIT_SYS_ERROR) unless $have_weight;
-	&my_die("Sources $sources does not exist", $xcstack_id, $PS_EXIT_SYS_ERROR) unless $have_sources;
-	&my_die("PSF $psf does not exist", $xcstack_id, $PS_EXIT_SYS_ERROR) if ($convolve and not $have_psf);
-    }
-
-    print $listFile "INPUT$num\tMETADATA\n";
-
-    print $listFile "\tIMAGE\tSTR\t"    . $image    . "\n";
-    print $listFile "\tMASK\tSTR\t"     . $mask     . "\n";
-    print $listFile "\tVARIANCE\tSTR\t" . $weight   . "\n";
-    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";
-    $num++;
-}
-close($listFile);
-
-print "I've checked everything: $xcstack_id\n";
+# stack_type must be one of: DEEP_STACK, NIGHTLY_STACK, IQ_STACK.  this does not affect the analysis, but is added to the header
 
 # Prepare the output files
 my @outputFiles;
-prepare_outputs(\@outputFiles, \@outputList,       $outroot) if $convolve;
-prepare_outputs(\@outputFiles, \@outputListPhotom, $outroot) if $photometry;
-prepare_outputs(\@outputFiles, \@outputListUnconv, $outroot); # unconvolved output always go here
-
-# we need the output image name for the database
-my $outputName = $outputFiles[0];
+prepare_outputs(\@outputFiles, \@outputList, $outroot); # this list 
 
 ## use an explicit xcstack name for psphot output objects
@@ -344,14 +205,6 @@
     $command .= " -stats $outputStats" if $do_stats;
     $command .= " -recipe PPSTACK $recipe_ppStack";
-    $command .= " -recipe PPSUB $recipe_ppSub";
-    $command .= " -recipe PSPHOT $recipe_psphot";
     $command .= " -recipe PPSTATS $recipe_ppstats" if $do_stats;;
-    $command .= " -stack-type $xcstack_type";
-    $command .= " -F PSPHOT.PSF.SAVE PSPHOT.PSF.SKY.SAVE";
-    $command .= " -F PSPHOT.OUTPUT PSPHOT.OUT.CMF.MEF";
-    $command .= " -F PSPHOT.BACKMDL PSPHOT.BACKMDL.MEF";
-    $command .= " -F SOURCE.PLOT.MOMENTS SOURCE.PLOT.SKY.MOMENTS";
-    $command .= " -F SOURCE.PLOT.PSFMODEL SOURCE.PLOT.SKY.PSFMODEL";
-    $command .= " -F SOURCE.PLOT.APRESID SOURCE.PLOT.SKY.APRESID";
+    $command .= " -stack-type $stack_type";
     if ($output_nocomp) {
 	$command .= " -F PPSTACK.OUTPUT PPSTACK.OUTPUT.NOCOMP";
@@ -383,8 +236,4 @@
     $command .= " -source_id $source_id" if defined $source_id;
 
-    $command .= " -xcstack_id   $xcstack_id"   if defined $xcstack_id;
-    $command .= " -skycell_id $skycell_id" if defined $skycell_id;
-    $command .= " -tess_id    $tess_base"  if defined $tess_base;
-
     if ($run_state eq 'new') {
 	$command .= " -dumpconfig $configuration";
@@ -394,4 +243,5 @@
 
 
+    # cannot use run_command here (no metadata output to parse)
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
 	run(command => $command, verbose => $verbose);
@@ -401,5 +251,6 @@
     }
 
-    my $quality;                # Quality flag
+    # extract the stats flags, and capture the -quality flag
+    my $quality;
     if ($do_stats) {
 	my $outputStatsReal = $ipprc->file_resolve($outputStats);
@@ -408,13 +259,10 @@
 	# measure stats
 	$command = "$ppStatsFromMetadata $outputStatsReal - XCSTACK_SKYCELL";
-	( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
-	    run(command => $command, verbose => $verbose);
+	( $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 ppStatsFromMetadata: $error_code", $xcstack_id, $error_code);
 	}
-	foreach my $line (@$stdout_buf) {
-	    $cmdflags .= " $line";
-	}
+	foreach my $line (@$stdout_buf) { $cmdflags .= " $line"; }
 	chomp $cmdflags;
 
@@ -423,31 +271,9 @@
 
     if (!$quality) {
-	
-
 	check_outputs(\@outputFiles, $replicate_outputs);
-
-	if (($convolve)&&($delete_convolved_images)) { # We made convolved products, but do not wish to keep them anymore
-	    my @products_to_clear = ('PPSTACK.OUTPUT',
-				     'PPSTACK.OUTPUT.MASK',
-				     'PPSTACK.OUTPUT.VARIANCE',
-				     'PPSTACK.OUTPUT.EXP',
-				     'PPSTACK.OUTPUT.EXPNUM',
-				     'PPSTACK.OUTPUT.EXPWT');
-	    foreach my $product (@products_to_clear) {
-		my $file = $ipprc->filename($product,$outroot,$skycell_id);
-		print "Deleting unwanted convolved product: $file\n";
-		my $error= $ipprc->kill_file($file);
-		if ($error_code) {
-		    print "Failed to delete unwanted convolved product: $error_code\n";
-		}
-	    }
-	}
-
     }
 
     print "I've stacked $listName: $xcstack_id\n";
 }
-
-delete_temps() unless ($save_temps);
 
 unless ($no_update) {
@@ -459,5 +285,5 @@
 	if ($run_state eq 'new') {
 	    $mode = "-addsumskyfile";
-	    $command .= " -uri $outputName -path_base $outroot";
+	    $command .= " -path_base $outroot";
 	    $command .= " -hostname $host" if defined $host;
 	    $command .= (" -dtime_script " . ((DateTime->now->mjd - $mjd_start) * 86400));
@@ -481,6 +307,12 @@
 print "I've updated the database: $xcstack_id\n";
 
-
-my $my_die_called = 0;
+print "I've reached the end of processing with a code of $?: $xcstack_id\n";
+
+END {
+    my $exit = $?;
+    system("sync") == 0 or die "failed to execute sync: $!";
+    $? = $exit;
+}
+
 sub my_die
 {
@@ -493,15 +325,10 @@
     carp($msg);
 
-    unless ($my_die_called) {
-	$my_die_called = 1;
-	delete_temps() unless ($save_temps);
-    }
-
     if (defined $xcstack_id and not $no_update) {
 	my $command = "$xcstacktool -xcstack_id $xcstack_id -fault $exit_code";
 	if ($run_state eq 'new') {
 	    $command .= " -addsumskyfile";
+	    $command .= " -path_base $outroot" if defined $outroot;
 	    $command .= " -hostname $host" if defined $host;
-	    $command .= " -path_base $outroot" if defined $outroot;
 	    $command .= (" -dtime_script " . ((DateTime->now->mjd - $mjd_start) * 86400));
 	} else {
@@ -514,35 +341,4 @@
 }
 
-# Ensure temporary images are deleted
-sub delete_temps
-{
-    unless ($temp_images_exist) {
-	print "No temporary images yet generated\n";
-	return 1;
-    }
-
-    print "Ensuring temporary images are deleted.\n";
-
-    my $temp_delete = defined($recipe) and metadataLookupBool($recipe, 'TEMP.DELETE'); # Delete temp files?
-    if ($temp_delete) {
-	my $temp_dir = metadataLookupStr($ipprc->{_siteConfig}, 'TEMP.DIR') or &my_die("Unable to find temporary directory in site configuration", $xcstack_id, $PS_EXIT_CONFIG_ERROR); # Directory with temporary files
-	my $temp_image = metadataLookupStr($recipe, 'TEMP.IMAGE'); # Suffix for image
-	my $temp_mask = metadataLookupStr($recipe, 'TEMP.MASK'); # Suffix for mask
-	my $temp_weight = metadataLookupStr($recipe, 'TEMP.VARIANCE'); # Suffix for weight
-	my $temp_root = basename($outroot); # Root name for temporary files
-
-	if (not defined $temp_dir or not defined $temp_image or not defined $temp_mask or
-	    not defined $temp_weight) {
-	    &my_die("Unable to find details for temporary images.", $xcstack_id, $PS_EXIT_CONFIG_ERROR);
-	}
-
-	for (my $i = 0; $i < $num; $i++) {
-	    foreach my $ext ( $temp_image, $temp_mask, $temp_weight ) {
-		my $file = $temp_dir . '/' . $temp_root . '.' . $i . '.' . $ext;
-		unlink $file if -e $file;
-	    }
-	}
-    }
-}
 sub prepare_output
 {
@@ -614,29 +410,29 @@
     # Get list of cameras for this xcstack
     my $mdcParser = PS::IPP::Metadata::Config->new; # Parser for metadata config files
-    {
-	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 $command: $error_code");
-	}
-
-	if (@$stdout_buf == 0) {
-	    &my_die("No input cameras selected", $xcstack_id, $PS_EXIT_PROG_ERROR);
-	}
-	my $metadata = $mdcParser->parse(join "", @$stdout_buf) or &my_die("Unable to parse metadata config doc", $xcstack_id, $PS_EXIT_PROG_ERROR);
-	$cameras = parse_md_list($metadata) or &my_die("Unable to parse metadata list", $xcstack_id, $PS_EXIT_PROG_ERROR);
-    }
-
-
-
-}
-print "I've reached the end of processing with a code of $?: $xcstack_id\n";
-
-END {
-    my $exit = $?;
-    system("sync") == 0 or die "failed to execute sync: $!";
-    $? = $exit;
-}
-
+
+    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 $command", $xcstack_id, $error_code);
+    }
+
+    # is the result allowed to be empty?
+    if (@$stdout_buf == 0) {
+	&my_die("No output from command $command", $xcstack_id, $PS_EXIT_PROG_ERROR);
+    }
+    my $metadata = $mdcParser->parse(join "", @$stdout_buf) or &my_die("Unable to parse metadata config doc", $xcstack_id, $PS_EXIT_PROG_ERROR);
+
+    return $metadata;
+}
+
+sub run_command_ipptool {
+
+    my $command = shift;
+
+    my $metadata = &run_command ($command);
+    my $result = parse_md_list($metadata) or &my_die("Unable to parse metadata list", $xcstack_id, $PS_EXIT_PROG_ERROR);
+    return $result;
+}
 
 __END__
+
