Index: trunk/ippScripts/scripts/staticsky.pl
===================================================================
--- trunk/ippScripts/scripts/staticsky.pl	(revision 27964)
+++ trunk/ippScripts/scripts/staticsky.pl	(revision 28092)
@@ -29,5 +29,5 @@
 my $missing_tools;
 my $staticskytool = can_run('staticskytool') or (warn "Can't find staticskytool" and $missing_tools = 1);
-my $ppSub = can_run('ppSub') or (warn "Can't find ppSub" and $missing_tools = 1);
+my $psphotStack = can_run('psphotStack') or (warn "Can't find psphotStack" and $missing_tools = 1);
 my $ppStatsFromMetadata = can_run('ppStatsFromMetadata') or (warn "Can't find ppStatsFromMetadata" and $missing_tools = 1);
 if ($missing_tools) {
@@ -101,35 +101,20 @@
     print $listFile "INPUT   METADATA\n";
 
-    $path_base = $file->{path_base};
-    my $imageCnv  = $ipprc->filename("PPSTACK.OUTPUT", $file->{path_base} ); # Mask name
-    my $maskCnv   = $ipprc->filename("PPSTACK.OUTPUT.MASK", $file->{path_base} ); # Mask name
-    my $weightCnv = $ipprc->filename("PPSTACK.OUTPUT.VARIANCE", $file->{path_base} ); # Weight name
-    my $psfCnv    = $ipprc->filename("PSPHOT.PSF.SKY.SAVE", $file->{path_base} ); # PSF name
-
-    my $imageRaw  = $ipprc->filename("PPSTACK.UNCONV", $file->{path_base} ); # Mask name
-    my $maskRaw   = $ipprc->filename("PPSTACK.UNCONV.MASK", $file->{path_base} ); # Mask name
-    my $weightRaw = $ipprc->filename("PPSTACK.UNCONV.VARIANCE", $file->{path_base} ); # Weight name
-
-    my $sources   = $ipprc->filename("PSPHOT.OUT.CMF.MEF", $file->{path_base}); # Sources name
-
-    &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 );
-    &my_die("Weight $weight does not exist", $stack_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists( $weight );
-    &my_die("PSF $psf does not exist", $stack_id, $PS_EXIT_SYS_ERROR) if ($convolve and not $ipprc->file_exists( $psf ));
-    &my_die("Sources $sources does not exist", $stack_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists( $sources );
-
-    print $listFile "  RAW:IMAGE     STR  " . $imageRaw  . "\n";
-    print $listFile "  RAW:MASK      STR  " . $maskRaw   . "\n";
-    print $listFile "  RAW:VARIANCE  STR  " . $weightRaw . "\n";
-
-    print $listFile "  CNV:IMAGE     STR  " . $imageCnv  . "\n";
-    print $listFile "  CNV:MASK      STR  " . $maskCnv   . "\n";
-    print $listFile "  CNV:VARIANCE  STR  " . $weightCnv . "\n";
-    print $listFile "  CNV:PSF       STR  " . $psfCnv    . "\n";
-
-    print $listFile "  SOURCES       STR  " . $sources   . "\n";
-
-    print $listFile "END\n\n";
-
+    # XXX if we take the input from 'warp', we will need to make different selections here
+    my $path_base = $file->{path_base};
+    my $imageCnv  = $ipprc->filename("PPSTACK.OUTPUT",          $path_base ); # Image name
+    my $maskCnv   = $ipprc->filename("PPSTACK.OUTPUT.MASK",     $path_base ); # Mask name
+    my $weightCnv = $ipprc->filename("PPSTACK.OUTPUT.VARIANCE", $path_base ); # Weight name
+
+    my $imageRaw  = $ipprc->filename("PPSTACK.UNCONV",          $path_base ); # Image name
+    my $maskRaw   = $ipprc->filename("PPSTACK.UNCONV.MASK",     $path_base ); # Mask name
+    my $weightRaw = $ipprc->filename("PPSTACK.UNCONV.VARIANCE", $path_base ); # Weight name
+
+    my $sources   = $ipprc->filename("PSPHOT.OUT.CMF.MEF",      $path_base ); # Sources name
+
+    # XXX is this the correct PSF file?
+    my $psfCnv    = $ipprc->filename("PPSTACK.TARGET.PSF",      $path_base ); # PSF name
+
+    # XXX we could make some different choices if some inputs do not exist...
     &my_die("Couldn't find input: $imageRaw",  $sky_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists("$imageRaw");
     &my_die("Couldn't find input: $maskRaw",   $sky_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists("$maskRaw");
@@ -140,4 +125,17 @@
     &my_die("Couldn't find input: $psfCnv",    $sky_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists("$psfCnv");
     &my_die("Couldn't find input: $sources",   $sky_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists("$sources");
+
+    print $listFile "  RAW:IMAGE     STR  " . $imageRaw  . "\n";
+    print $listFile "  RAW:MASK      STR  " . $maskRaw   . "\n";
+    print $listFile "  RAW:VARIANCE  STR  " . $weightRaw . "\n";
+
+    print $listFile "  CNV:IMAGE     STR  " . $imageCnv  . "\n";
+    print $listFile "  CNV:MASK      STR  " . $maskCnv   . "\n";
+    print $listFile "  CNV:VARIANCE  STR  " . $weightCnv . "\n";
+    print $listFile "  CNV:PSF       STR  " . $psfCnv    . "\n";
+
+    print $listFile "  SOURCES       STR  " . $sources   . "\n";
+
+    print $listFile "END\n\n";
 }
 
@@ -149,30 +147,6 @@
 }
 
-print "reduction: $reduction\n";
+print "reduction:     $reduction\n";
 print "recipe_psphot: $recipe_psphot\n";
-
-# Get the output filenames
-my $outputName = $ipprc->filename("PPSUB.OUTPUT", $outroot);
-my $outputMask = $ipprc->filename("PPSUB.OUTPUT.MASK", $outroot);
-my $outputVariance = $ipprc->filename("PPSUB.OUTPUT.VARIANCE", $outroot);
-my $outputSources = $ipprc->filename("PPSUB.OUTPUT.SOURCES", $outroot);
-my $jpeg1Name = $ipprc->filename("PPSUB.OUTPUT.JPEG1", $outroot);
-my $jpeg2Name = $ipprc->filename("PPSUB.OUTPUT.JPEG2", $outroot);
-my $configuration = $ipprc->filename("PPSUB.CONFIG", $outroot);
-my $outputStats = $ipprc->filename("SKYCELL.STATS", $outroot);
-my $traceDest = $ipprc->filename("TRACE.EXP", $outroot);
-
-if ($run_state eq 'update') {
-    $traceDest .= '.update';
-    $outputStats .= '.update';
-}
-
-my ($inverseName, $inverseMask, $inverseVariance, $inverseSources);
-if ($inverse) {
-    $inverseName = $ipprc->filename("PPSUB.INVERSE", $outroot);
-    $inverseMask = $ipprc->filename("PPSUB.INVERSE.MASK", $outroot);
-    $inverseVariance = $ipprc->filename("PPSUB.INVERSE.VARIANCE", $outroot);
-    $inverseSources = $ipprc->filename("PPSUB.INVERSE.SOURCES", $outroot);
-}
 
 my $cmdflags;
@@ -180,37 +154,13 @@
 # Perform subtraction
 {
-    my $command = "$ppSub $outroot";
-    $command .= " -inimage $input";
-    $command .= " -refimage $template";
-    $command .= " -inmask $inputMask";
-    $command .= " -refmask $templateMask";
-    $command .= " -invariance $inputVariance";
-    $command .= " -refvariance $templateVariance";
-    $command .= " -insources $inputSources";
-    $command .= " -refsources $templateSources";
-    $command .= " -stats $outputStats";
+    my $command = "$psphotStack $outroot";
+    $command .= " -input $listName";
     $command .= " -threads $threads" if defined $threads;
-    if ($run_state eq "new") {
-        $command .= " -dumpconfig $configuration";
-    } else {
-        my $configurationReal = $ipprc->file_resolve($configuration) or &my_die("Couldn't resolve configuration file: $configuration", $sky_id, $skycell_id, $PS_EXIT_SYS_ERROR);
-        $command .= " -ipprc $configurationReal";
-    }
-    $command .= " -save-inconv" if defined $saveInConv;
-    $command .= " -save-refconv" if defined $saveRefConv;
-    $command .= " -recipe PPSUB $recipe_ppSub";
     $command .= " -recipe PSPHOT $recipe_psphot";
-    $command .= " -recipe PPSTATS WARPSTATS";
-    $command .= " -F PSPHOT.PSF.SAVE PSPHOT.PSF.SKY.SAVE";
-    $command .= " -F PSPHOT.OUTPUT PSPHOT.OUT.CMF.MEF";
-    $command .= " -F PSPHOT.BACKMDL PSPHOT.BACKMDL.MEF";
-    if ($run_state eq "new") {
-        $command .= " -photometry";
-    }
-    $command .= " -inverse" if $inverse;
-    $command .= " -tracedest $traceDest -log $logDest";
-    $command .= " -dbname $dbname" if defined $dbname;
-    $command .= " -image_id $diff_skyfile_id" if defined $diff_skyfile_id;
-    $command .= " -source_id $source_id" if defined $source_id;
+    # $command .= " -dumpconfig $configuration";
+    # $command .= " -tracedest $traceDest -log $logDest";
+    # $command .= " -dbname $dbname" if defined $dbname;
+    # $command .= " -image_id $diff_skyfile_id" if defined $diff_skyfile_id;
+    # $command .= " -source_id $source_id" if defined $source_id;
 
     unless ($no_op) {
@@ -219,41 +169,57 @@
         unless ($success) {
             $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
-            &my_die("Unable to perform ppSub: $error_code", $sky_id, $skycell_id, $error_code);
-        }
-
-        my $outputStatsReal = $ipprc->file_resolve($outputStats);
-        &my_die("Couldn't find expected output file: $outputStats", $sky_id, $skycell_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($outputStatsReal);
+            &my_die("Unable to perform ppSub: $error_code", $sky_id, $error_code);
+        }
+
+        # my $outputStatsReal = $ipprc->file_resolve($outputStats);
+        # &my_die("Couldn't find expected output file: $outputStats", $sky_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($outputStatsReal);
 
         # measure chip stats
-        $command = "$ppStatsFromMetadata $outputStatsReal - DIFF_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", $sky_id, $skycell_id, $error_code);
-        }
-        foreach my $line (@$stdout_buf) {
-            $cmdflags .= " $line";
-        }
-        chomp $cmdflags;
+        # $command = "$ppStatsFromMetadata $outputStatsReal - DIFF_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", $sky_id, $error_code);
+        # }
+        # foreach my $line (@$stdout_buf) {
+        #     $cmdflags .= " $line";
+        # }
+        # chomp $cmdflags;
 
         my ($quality) = $cmdflags =~ /-quality (\d+)/; # Quality flag
 
         if (!$quality) {
-            &my_die("Couldn't find expected output file: $outputName", $sky_id, $skycell_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($outputName);
-            &my_die("Couldn't find expected output file: $outputMask", $sky_id, $skycell_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($outputMask);
-            &my_die("Couldn't find expected output file: $outputVariance", $sky_id, $skycell_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($outputVariance);
-            &my_die("Couldn't find expected output file: $outputSources", $sky_id, $skycell_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($outputSources);
-            &my_die("Couldn't find expected output file: $jpeg1Name",    $sky_id, $skycell_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($jpeg1Name);
-            &my_die("Couldn't find expected output file: $jpeg2Name",    $sky_id, $skycell_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($jpeg2Name);
-            if ($inverse) {
-                &my_die("Couldn't find expected output file: $inverseName", $sky_id, $skycell_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($inverseName);
-                &my_die("Couldn't find expected output file: $inverseMask", $sky_id, $skycell_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($inverseMask);
-                &my_die("Couldn't find expected output file: $inverseVariance", $sky_id, $skycell_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($inverseVariance);
-                &my_die("Couldn't find expected output file: $inverseSources", $sky_id, $skycell_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($inverseSources);
-                if ($run_state eq 'new') {
-                    &my_die("Couldn't find expected output file: $configuration", $sky_id, $skycell_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($configuration);
-                }
-            }
+
+	    # Get the output filenames
+	    # we have one set of output files per input file set
+	    for (my $i = 0; $i < @$files; $i++) {
+		my $outputName     = $ipprc->filename("PSPHOT.STACK.OUTPUT.IMAGE", $outroot);
+		my $outputMask     = $ipprc->filename("PSPHOT.STACK.OUTPUT.MASK", $outroot);
+		my $outputVariance = $ipprc->filename("PSPHOT.STACK.OUTPUT.VARIANCE", $outroot);
+		my $outputSources  = $ipprc->filename("PSPHOT.STACK.OUTPUT", $outroot);
+
+		&my_die("Couldn't find expected output file: $outputName", $sky_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($outputName);
+		&my_die("Couldn't find expected output file: $outputMask", $sky_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($outputMask);
+		&my_die("Couldn't find expected output file: $outputVariance", $sky_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($outputVariance);
+		&my_die("Couldn't find expected output file: $outputSources", $sky_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($outputSources);
+	    }
+
+	    #my $configuration  = $ipprc->filename("PPSUB.CONFIG", $outroot);
+	    #my $outputStats    = $ipprc->filename("SKYCELL.STATS", $outroot);
+	    #my $traceDest      = $ipprc->filename("TRACE.EXP", $outroot);
+
+	    # if ($run_state eq 'update') {
+	    # 	$traceDest .= '.update';
+	    # 	$outputStats .= '.update';
+	    # }
+
+	    my $chisqName     = $ipprc->filename("PSPHOT.CHISQ.IMAGE", $outroot);
+	    my $chisqMask     = $ipprc->filename("PSPHOT.CHISQ.MASK", $outroot);
+	    my $chisqVariance = $ipprc->filename("PSPHOT.CHISQ.VARIANCE", $outroot);
+
+            &my_die("Couldn't find expected output file: $chisqName", 	  $sky_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($chisqName);
+            &my_die("Couldn't find expected output file: $chisqMask", 	  $sky_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($chisqMask);
+            &my_die("Couldn't find expected output file: $chisqVariance", $sky_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($chisqVariance);
         }
     } else {
@@ -266,14 +232,13 @@
     # Add the subtraction result
     {
-        my $command = "$difftool -sky_id $sky_id -skycell_id $skycell_id";
-        $command .= " -magicked $magicked" if $magicked;
-        if ($run_state eq 'new') {
-            $command .= " -adddiffskyfile -path_base $outroot";
-            $command .= " $cmdflags";
-            $command .= (" -dtime_script " . ((DateTime->now->mjd - $mjd_start) * 86400));
-            $command .= " -hostname $host" if defined $host;
-        } else {
-            $command .= " -tofullskyfile";
-        }
+        my $command = "$staticskytool -sky_id $sky_id";
+        # if ($run_state eq 'new') {
+	$command .= " -addresult -path_base $outroot";
+	$command .= " $cmdflags";
+	$command .= (" -dtime_script " . ((DateTime->now->mjd - $mjd_start) * 86400));
+	$command .= " -hostname $host" if defined $host;
+        # } else {
+        #     $command .= " -tofullskyfile";
+        # }
         $command .= " -dbname $dbname" if defined $dbname;
 
@@ -285,5 +250,5 @@
                 "Unable to perform difftool -adddiffskyfile" :
                 "Unable to perform difftool -tofullskyfile";
-            &my_die("$err_message: $error_code", $sky_id, $skycell_id, $error_code);
+            &my_die("$err_message: $error_code", $sky_id, $error_code);
         }
     }
@@ -300,6 +265,6 @@
 
     warn($msg);
-    if (defined $sky_id and defined $skycell_id and not $no_update) {
-        my $command = "$difftool -sky_id $sky_id -skycell_id $skycell_id -fault $exit_code";
+    if (defined $sky_id and not $no_update) {
+        my $command = "$staticskytool -sky_id $sky_id -fault $exit_code";
         if ($run_state eq 'new') {
             $command .= " -adddiffskyfile";
