Index: /branches/eam_branches/ipp-20230313/ippScripts/scripts/xcstack_skycell.pl
===================================================================
--- /branches/eam_branches/ipp-20230313/ippScripts/scripts/xcstack_skycell.pl	(revision 42547)
+++ /branches/eam_branches/ipp-20230313/ippScripts/scripts/xcstack_skycell.pl	(revision 42548)
@@ -55,5 +55,5 @@
     'save-temps'        => \$save_temps, # Save temporary files?
     'delete-convolved'  => \$delete_convolved_images,
-) or pod2usage( 2 );
+    ) or pod2usage( 2 );
 
 pod2usage( -msg => "Unknown option: @ARGV", -exitval => 2 ) if @ARGV;
@@ -61,5 +61,5 @@
     -msg => "Required options: --xcstack_id --outroot --run-state",
     -exitval => 3,
-          ) unless defined $xcstack_id
+    ) unless defined $xcstack_id
     and defined $outroot
     and defined $run_state;
@@ -67,31 +67,12 @@
 # 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          
-);
-
-# produced if we run photometry
-my @outputListPhotom = qw(
-PSPHOT.OUT.CMF.MEF
-);
-
-# list of file rules produced if convolution is false
-my @outputListUnconv = qw(
-PPSTACK.UNCONV
-PPSTACK.UNCONV.MASK
-PPSTACK.UNCONV.VARIANCE
-PPSTACK.UNCONV.EXP          
-PPSTACK.UNCONV.EXPNUM       
-PPSTACK.UNCONV.EXPWT
-PPSTACK.OUTPUT.JPEG1        
-PPSTACK.OUTPUT.JPEG2        
-PPSTACK.CONFIG              
-);
-
+    PPXCSTACK.OUTPUT
+    PPXCSTACK.OUTPUT.MASK
+    PPXCSTACK.OUTPUT.VARIANCE
+    PPXCSTACK.OUTPUT.EXP          
+    PPXCSTACK.OUTPUT.EXPNUM       
+    PPXCSTACK.OUTPUT.EXPWT
+    PPXCSTACK.TARGET.PSF          
+    );
 
 my $ipprc = PS::IPP::Config->new() or my_die( "Unable to set up", $xcstack_id, $PS_EXIT_CONFIG_ERROR ); # IPP configuration
@@ -102,8 +83,4 @@
 # my $logDest = $ipprc->filename("LOG.EXP", $outroot);
 my $logDest = "$outroot.log";
-
-if ($run_state eq 'update') {
-    $logDest .= ".update";
-}
 
 $ipprc->redirect_to_logfile($logDest) or my_die( "Unable to redirect output", $xcstack_id, $PS_EXIT_SYS_ERROR ) if $redirect;
@@ -115,30 +92,118 @@
 }
 
-
 my $temp_images_exist = 0;
 my $image_id = $xcstack_id;
 my $source_id = $ipprc->source_id($dbname, $PS_TABLE_ID_XCSTACK);
 
-# Get list of components for xcstacking
+# Get list of cameras for this xcstack
 my $mdcParser = PS::IPP::Metadata::Config->new; # Parser for metadata config files
-my $files;
-{
-    my $command = "$xcstacktool -inputskyfile -xcstack_id $xcstack_id";
+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);
+    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);
+        &my_die("Unable to perform xcstacktool -inputcameras: $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);
-}
+        &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;
+
+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) {
+    # Get list of components for xcstacking
+    my $mdcParser = PS::IPP::Metadata::Config->new; # Parser for metadata config files
+
+    $camera_name = $camera->{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);
+    }
+
+    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++;
+	}
+    }
+}
+close($listFile);
 
 &my_die("Xcstack list contains less than two elements", $xcstack_id, $PS_EXIT_DATA_ERROR) unless
@@ -146,30 +211,4 @@
 
 print "I've loaded my inputs: $xcstack_id\n";
-
-# Parse the list of input files to get the tesselation, skycell identifiers and camera
-my $skycell_id;                 # Skycell identifier
-my $tess_id;                    # Tesselation identifier
-my $camera;                     # Camera
-foreach my $file (@$files) {
-    # skip warps which are specified as 'ignored'
-    if ($file->{ignored}) { next; }
-    if (defined $tess_id) {
-        &my_die("Tesselation identifiers don't match", $xcstack_id, $PS_EXIT_DATA_ERROR) unless
-            $file->{tess_id} eq $tess_id;
-    } else {
-        $tess_id = $file->{tess_id};
-    }
-    if (defined $skycell_id) {
-        &my_die("Skycell identifiers don't match", $xcstack_id, $PS_EXIT_DATA_ERROR) unless
-            $file->{skycell_id} eq $skycell_id;
-    } else {
-        $skycell_id = $file->{skycell_id};
-    }
-    if (defined $camera) {
-        &my_die("Cameras don't match", $xcstack_id, $PS_EXIT_DATA_ERROR) unless $file->{camera} eq $camera;
-    } else {
-        $camera = $file->{camera};
-    }
-}
 
 &my_die("Can't find camera", $xcstack_id, $PS_EXIT_SYS_ERROR) unless defined $camera;
@@ -181,29 +220,21 @@
 $reduction = 'DEFAULT' unless defined $reduction;
 my $recipe_ppStack = $ipprc->reduction($reduction, 'STACK_PPSTACK'); # Recipe to use for ppStack
-my $recipe_ppSub = $ipprc->reduction($reduction, 'STACK_PPSUB'); # Recipe to use for ppSub
-my $recipe_psphot  = $ipprc->reduction($reduction, 'STACK_PSPHOT'); # Recipe to use for psphot
-my $recipe_ppstats = 'XCSTACKSTATS';
-if ($
-run_state eq 'update') {
-     $recipe_ppstats = 'WARPSTATS';
-}
-unless ($recipe_ppStack and $recipe_ppSub and $recipe_psphot) {
-    &my_die("Couldn't find selected reduction for XCSTACK_PPSTACK, STACK_PPSUB and STACK_PSPHOT: $reduction\n", $xcstack_id, $PS_EXIT_CONFIG_ERROR);
-}
-
+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);
+	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);
+	$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 $convolve = metadataLookupBool($recipe, 'CONVOLVE'); # Convolve inputs?
-my $photometry = metadataLookupBool($recipe, 'PHOTOMETRY'); # perform photometry?
+	&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.
@@ -217,6 +248,9 @@
     &my_die("Unable to not compress and use deepexp simultaneously. Check config.",$xcstack_id, $PS_EXIT_CONFIG_ERROR);
 }
-my $xcstack_type = metadataLookupStr($recipe, 'XCSTACK.TYPE');
-&my_die("XCSTACK.TYPE not found in recipe. Check config.",$xcstack_id, $PS_EXIT_CONFIG_ERROR) unless $xcstack_type;
+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
@@ -321,17 +355,17 @@
     $command .= " -F SOURCE.PLOT.APRESID SOURCE.PLOT.SKY.APRESID";
     if ($output_nocomp) {
-        $command .= " -F PPSTACK.OUTPUT PPSTACK.OUTPUT.NOCOMP";
-        $command .= " -F PPSTACK.OUTPUT.VARIANCE PPSTACK.OUTPUT.VARIANCE.NOCOMP";
-        $command .= " -F PPSTACK.OUTPUT.EXPWT PPSTACK.OUTPUT.EXPWT.NOCOMP";
-        $command .= " -F PPSTACK.OUTPUT.EXP PPSTACK.OUTPUT.EXP.NOCOMP";
-        $command .= " -F PPSTACK.UNCONV PPSTACK.UNCONV.NOCOMP";
-        $command .= " -F PPSTACK.UNCONV.VARIANCE PPSTACK.UNCONV.VARIANCE.NOCOMP";
-        $command .= " -F PPSTACK.UNCONV.EXPWT PPSTACK.UNCONV.EXPWT.NOCOMP";
-        $command .= " -F PPSTACK.UNCONV.EXP PPSTACK.UNCONV.EXP.NOCOMP";
+	$command .= " -F PPSTACK.OUTPUT PPSTACK.OUTPUT.NOCOMP";
+	$command .= " -F PPSTACK.OUTPUT.VARIANCE PPSTACK.OUTPUT.VARIANCE.NOCOMP";
+	$command .= " -F PPSTACK.OUTPUT.EXPWT PPSTACK.OUTPUT.EXPWT.NOCOMP";
+	$command .= " -F PPSTACK.OUTPUT.EXP PPSTACK.OUTPUT.EXP.NOCOMP";
+	$command .= " -F PPSTACK.UNCONV PPSTACK.UNCONV.NOCOMP";
+	$command .= " -F PPSTACK.UNCONV.VARIANCE PPSTACK.UNCONV.VARIANCE.NOCOMP";
+	$command .= " -F PPSTACK.UNCONV.EXPWT PPSTACK.UNCONV.EXPWT.NOCOMP";
+	$command .= " -F PPSTACK.UNCONV.EXP PPSTACK.UNCONV.EXP.NOCOMP";
     }
     if ($output_logflux) {
 	$command .= " -R PPSTACK.OUTPUT          FITS.TYPE STK_UNIONS_COMP";
 	$command .= " -R PPSTACK.OUTPUT.VARIANCE FITS.TYPE STK_UNIONS_COMP"; 
-        $command .= " -R PPSTACK.UNCONV          FITS.TYPE STK_UNIONS_COMP";
+	$command .= " -R PPSTACK.UNCONV          FITS.TYPE STK_UNIONS_COMP";
 	$command .= " -R PPSTACK.UNCONV.VARIANCE FITS.TYPE STK_UNIONS_COMP";
     }
@@ -354,36 +388,36 @@
 
     if ($run_state eq 'new') {
-        $command .= " -dumpconfig $configuration";
+	$command .= " -dumpconfig $configuration";
     } else {
-        $command .= " -ipprc $configuration";
+	$command .= " -ipprc $configuration";
     }
 
 
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
-        run(command => $command, verbose => $verbose);
+	run(command => $command, verbose => $verbose);
     unless ($success) {
-        $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
-        &my_die("Unable to perform ppStack: $error_code", $xcstack_id, $error_code);
+	$error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
+	&my_die("Unable to perform ppStack: $error_code", $xcstack_id, $error_code);
     }
 
     my $quality;                # Quality flag
     if ($do_stats) {
-        my $outputStatsReal = $ipprc->file_resolve($outputStats);
-        &my_die("Couldn't find expected output file: $outputStats", $xcstack_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($outputStatsReal);
-
-        # measure stats
-        $command = "$ppStatsFromMetadata $outputStatsReal - XCSTACK_SKYCELL";
-        ( $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";
-        }
-        chomp $cmdflags;
-
-        ($quality) = $cmdflags =~ /-quality (\d+)/; # Quality flag
+	my $outputStatsReal = $ipprc->file_resolve($outputStats);
+	&my_die("Couldn't find expected output file: $outputStats", $xcstack_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($outputStatsReal);
+
+	# measure stats
+	$command = "$ppStatsFromMetadata $outputStatsReal - XCSTACK_SKYCELL";
+	( $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";
+	}
+	chomp $cmdflags;
+
+	($quality) = $cmdflags =~ /-quality (\d+)/; # Quality flag
     }
 
@@ -391,5 +425,5 @@
 	
 
-        check_outputs(\@outputFiles, $replicate_outputs);
+	check_outputs(\@outputFiles, $replicate_outputs);
 
 	if (($convolve)&&($delete_convolved_images)) { # We made convolved products, but do not wish to keep them anymore
@@ -421,24 +455,24 @@
     # Add the stack result
     {
-        my $command = "$xcstacktool";
-        my $mode;
-        if ($run_state eq 'new') {
-            $mode = "-addsumskyfile";
-            $command .= " -uri $outputName -path_base $outroot";
-            $command .= " -hostname $host" if defined $host;
-            $command .= (" -dtime_script " . ((DateTime->now->mjd - $mjd_start) * 86400));
-        } else {
-            $mode = "-updaterun -set_state full";
-        }
-        $command .= " $mode -xcstack_id $xcstack_id";
-        $command .= " $cmdflags" if $do_stats;
-        $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 $mode $error_code", $xcstack_id, $error_code);
-        }
+	my $command = "$xcstacktool";
+	my $mode;
+	if ($run_state eq 'new') {
+	    $mode = "-addsumskyfile";
+	    $command .= " -uri $outputName -path_base $outroot";
+	    $command .= " -hostname $host" if defined $host;
+	    $command .= (" -dtime_script " . ((DateTime->now->mjd - $mjd_start) * 86400));
+	} else {
+	    $mode = "-updaterun -set_state full";
+	}
+	$command .= " $mode -xcstack_id $xcstack_id";
+	$command .= " $cmdflags" if $do_stats;
+	$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 $mode $error_code", $xcstack_id, $error_code);
+	}
     }
 
@@ -460,20 +494,20 @@
 
     unless ($my_die_called) {
-        $my_die_called = 1;
-        delete_temps() unless ($save_temps);
+	$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 .= " -hostname $host" if defined $host;
-            $command .= " -path_base $outroot" if defined $outroot;
-            $command .= (" -dtime_script " . ((DateTime->now->mjd - $mjd_start) * 86400));
-        } else {
-            $command .= " -updatesumskyfile";
-        }
-        $command .= " -dbname $dbname" if defined $dbname;
-        system ($command);
+	my $command = "$xcstacktool -xcstack_id $xcstack_id -fault $exit_code";
+	if ($run_state eq 'new') {
+	    $command .= " -addsumskyfile";
+	    $command .= " -hostname $host" if defined $host;
+	    $command .= " -path_base $outroot" if defined $outroot;
+	    $command .= (" -dtime_script " . ((DateTime->now->mjd - $mjd_start) * 86400));
+	} else {
+	    $command .= " -updatesumskyfile";
+	}
+	$command .= " -dbname $dbname" if defined $dbname;
+	system ($command);
     }
     exit $exit_code;
@@ -484,6 +518,6 @@
 {
     unless ($temp_images_exist) {
-        print "No temporary images yet generated\n";
-        return 1;
+	print "No temporary images yet generated\n";
+	return 1;
     }
 
@@ -492,21 +526,21 @@
     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;
-            }
-        }
+	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;
+	    }
+	}
     }
 }
@@ -520,5 +554,5 @@
     my $error;
     my $output = $ipprc->prepare_output($filerule, $outroot, undef, $delete, \$error)
-                    or &my_die("failed to prepare output file for: $filerule", $xcstack_id, $error);
+	or &my_die("failed to prepare output file for: $filerule", $xcstack_id, $error);
     return $output;
 }
@@ -531,5 +565,5 @@
     
     foreach my $rule (@$ruleList) {
-        push @$fileList, prepare_output($rule, $outroot, 1);
+	push @$fileList, prepare_output($rule, $outroot, 1);
     }
 }
@@ -541,5 +575,5 @@
 
     if (!defined $file) {
-        return;
+	return;
     }
 
@@ -549,5 +583,5 @@
     my $diskfile = $ipprc->file_resolve($file);
     if ($diskfile =~ /fits/) {
-        my $funpack  = can_run('funpack') or &my_die ("Can't find funpack", $xcstack_id, $PS_EXIT_SYS_ERROR);
+	my $funpack  = can_run('funpack') or &my_die ("Can't find funpack", $xcstack_id, $PS_EXIT_SYS_ERROR);
 	my $check_command = "$funpack -S $diskfile > /dev/null";
 	my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
@@ -560,5 +594,5 @@
 
     if ($replicate and $neb) {
-        $ipprc->replicate_file($file) or &my_die("failed to replicate: $file\n",  $xcstack_id, $PS_EXIT_SYS_ERROR);
+	$ipprc->replicate_file($file) or &my_die("failed to replicate: $file\n",  $xcstack_id, $PS_EXIT_SYS_ERROR);
     }
 }
@@ -569,8 +603,32 @@
     my $replicate = shift;
     foreach my $file (@$fileList) {
-        check_output($file, $replicate);
-    }
-}
-
+	check_output($file, $replicate);
+    }
+}
+
+sub run_command {
+
+    my $command = shift;
+    my $result;
+
+    # 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";
 
