Index: trunk/ippScripts/scripts/staticsky.pl
===================================================================
--- trunk/ippScripts/scripts/staticsky.pl	(revision 38515)
+++ trunk/ippScripts/scripts/staticsky.pl	(revision 38516)
@@ -43,4 +43,5 @@
     print "USAGE: staticsky.pl --sky_id (sky_id) --outroot (root) --camera (camera) [options]\n";
     print "  --sky_id (sky_id)   : run identifier\n";
+    print "  --run-state (state) : run state (new or update)\n";
     print "  --camera (camera)   : Camera name\n";
     print "  --dbname (dbname)   : Database name\n";
@@ -62,6 +63,9 @@
 
 my ($sky_id, $camera, $dbname, $threads, $outroot, $reduction, $require_sources, $verbose, $no_update, $no_op, $redirect, $save_temps);
+my $run_state ='new';
+
 GetOptions(
     'sky_id=s'          => \$sky_id, # Diff identifier
+    'run-state=s'       => \$run_state, 
     'camera|c=s'        => \$camera, # Camera name
     'dbname|d=s'        => \$dbname, # Database name
@@ -88,4 +92,8 @@
     defined $camera;
 
+
+&my_die("unexpected run state: $run_state",  $sky_id, $PS_EXIT_PROG_ERROR) unless ($run_state eq 'new') or ($run_state eq 'update');
+my $updatemode = $run_state eq 'update';
+
 my $ipprc = PS::IPP::Config->new( $camera ) or my_die( "Unable to set up", $sky_id, $PS_EXIT_CONFIG_ERROR ); # IPP configuration
 
@@ -126,5 +134,5 @@
 my $nInputs = @$files;
 
-if (1) {
+{
         my $recipe_psphot  = $ipprc->reduction($reduction, 'STACKPHOT_PSPHOT'); # Recipe to use for psphot
         my $recipe_ppsub   = $ipprc->reduction($reduction, 'STACKPHOT_PPSUB'); # Recipe to use for ppsub
@@ -163,5 +171,33 @@
             my $expnumRaw = $ipprc->filename("PPSTACK.UNCONV.EXPNUM",   $path_base ); # Expnum name
 
-            my $sources   = $ipprc->filename("PSPHOT.OUT.CMF.MEF",      $path_base ); # Sources name
+            my $sources;
+            my ($psfRaw, $backmdlRaw);
+            if (!$updatemode) {
+                # sources from stack run. 
+                # XXX: We don't need this file anymore.
+                $sources   = $ipprc->filename("PSPHOT.OUT.CMF.MEF",      $path_base ); # Sources name
+            } else {
+                # name of output sources from this run when in new state
+                $require_sources = 1 unless $no_op;
+                $sources  = $ipprc->filename("PSPHOT.STACK.OUTPUT", $outroot, $stack_id);
+                # During update we rename the sources file to this
+                my $originalSources = "$sources.original";
+                if (!$ipprc->file_exists($originalSources)) {
+                    if ($ipprc->file_exists($sources)) {
+                        unless ($no_op) {
+                            if (!$ipprc->file_rename($sources, $originalSources)) {
+                                &my_die("failed to rename $sources $originalSources",  $sky_id, $PS_EXIT_PROG_ERROR);
+                            }
+                        }
+                    } else {
+                        # maybe this should be a fault 2
+                        &my_die("failed to to find either sources file to update as either $sources or $originalSources",  $sky_id, $PS_EXIT_PROG_ERROR);
+                    }
+                }
+                $sources = $originalSources;
+                $psfRaw  = $ipprc->filename("PSPHOT.STACK.PSF.SAVE", $outroot, $stack_id);
+                $backmdlRaw  = $ipprc->filename("PSPHOT.STACK.BACKMDL", $outroot, $stack_id);
+            }
+
 
             # XXX is this the correct PSF file?
@@ -194,4 +230,10 @@
 		print $listFile "  SOURCES       STR  " . $sources   . "\n";
 	    }
+            if ($updatemode) {
+                &my_die("Couldn't find input: $psfRaw", $sky_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists("$psfRaw");
+                &my_die("Couldn't find input: $backmdlRaw",    $sky_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists("$backmdlRaw");
+                print $listFile "  RAW:PSF       STR  " . $psfRaw . "\n";
+                print $listFile "  RAW:BACKMDL   STR  " . $backmdlRaw . "\n";
+            }
             if ($needConvolvedImages) {
                 print $listFile "  CNV:IMAGE     STR  " . $imageCnv  . "\n";
@@ -205,9 +247,10 @@
         }
 
-        # my $cmdflags;
+        close $listFile;
 
         # Perform stack photometry analysis
         {
             my $command = "$psphotStack $outroot";
+            $command .= " -updatemode" if $updatemode;
             $command .= " -input $listName";
             $command .= " -threads $threads" if defined $threads;
@@ -275,4 +318,8 @@
                     # &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);
+
+                    if (!$updatemode) {
+                        # XXX: should be checking for existence of psf and backmdl output files
+                    }
                 }
             } else {
@@ -280,94 +327,4 @@
             }
         }
-} else {
-    # XXX This mode is no longer used. We run psphotStack even if there is only one filter
-        # single input. Run psphot
-        # find the recipe
-        my $recipe_psphot  = $ipprc->reduction($reduction, 'STACKPHOT_SINGLE_PSPHOT'); # Recipe to use for psphot
-        unless ($recipe_psphot) {
-            &my_die("Couldn't find selected reduction for STACKPHOT: $reduction\n", $sky_id, $PS_EXIT_CONFIG_ERROR);
-        }
-
-        print "reduction:      $reduction\n";
-        print "recipe_psphot:  $recipe_psphot\n";
-
-        my $configuration = $ipprc->filename("PSPHOT.SKY.CONFIG", $outroot);
-
-        my $file = $files->[0];
-
-        # XXX if we take the input from 'warp', we will need to make different selections here
-        my $path_base = $file->{path_base};
-        print "input: $path_base\n";
-
-        # examine the recipe to determine whether to analyze the "raw" or convolved images
-        my $command = "$ppConfigDump -camera $camera -dump-recipe PSPHOT -recipe PSPHOT $recipe_psphot -";
-        my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
-        run(command => $command, verbose => 0);
-        unless ($success) {
-            $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
-            &my_die("Unable to perform ppConfigDump: $error_code", $sky_id, $PS_EXIT_SYS_ERROR);
-        }
-        my $recipeData = $mdcParser->parse(join "", @$stdout_buf) or
-            &my_die("Unable to parse metadata config doc", $sky_id, $PS_EXIT_SYS_ERROR);
-
-        my $use_raw = metadataLookupBool($recipeData, 'PSPHOT.STACK.USE.RAW');
-
-        my ($image, $mask, $variance, $expnum);
-        if ($use_raw) {
-            $image  = $ipprc->filename("PPSTACK.UNCONV",          $path_base ); # Image name
-            $mask   = $ipprc->filename("PPSTACK.UNCONV.MASK",     $path_base ); # Mask name
-            $variance = $ipprc->filename("PPSTACK.UNCONV.VARIANCE", $path_base ); # Weight name
-            $expnum = $ipprc->filename("PPSTACK.OUTPUT.EXPNUM", $path_base ); # Expnum name
-        } else {
-            $image  = $ipprc->filename("PPSTACK.OUTPUT",          $path_base ); # Image name
-            $mask   = $ipprc->filename("PPSTACK.OUTPUT.MASK",     $path_base ); # Mask name
-            $variance = $ipprc->filename("PPSTACK.OUTPUT.VARIANCE", $path_base ); # Weight name
-            $expnum = $ipprc->filename("PPSTACK.OUTPUT.EXPNUM", $path_base ); # Expnum name
-        }
-
-        my $output_sources_filerule = "PSPHOT.OUT.CMF.MEF";
-        my $output_psf_filerule = "PSPHOT.PSF.SKY.SAVE";
-
-        # XXX we could make some different choices if some inputs do not exist...
-        &my_die("Couldn't find input: $image",  $sky_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists("$image");
-        &my_die("Couldn't find input: $mask",   $sky_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists("$mask");
-        &my_die("Couldn't find input: $variance", $sky_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists("$variance");
-        &my_die("Couldn't find input: $expnum", $sky_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists("$expnum");
-
-        # Perform stack photometry analysis
-        {
-            my $command = "$psphot $outroot";
-            $command .= " -file $image";
-            $command .= " -mask $mask";
-            $command .= " -variance $variance";
-            $command .= " -expnum $expnum";
-            $command .= " -threads $threads" if defined $threads;
-            $command .= " -recipe PSPHOT  $recipe_psphot";
-            $command .= " -dumpconfig $configuration" if $configuration;
-            $command .= " -tracedest $traceDest -log $logDest";
-            $command .= " -F PSPHOT.OUTPUT $output_sources_filerule";
-            $command .= " -F PSPHOT.PSF.SAVE $output_psf_filerule";
-
-            unless ($no_op) {
-                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 psphot: $error_code", $sky_id, $error_code);
-                }
-
-                my $quality = 0;
-                if (!$quality) {
-                    my $outputSources  = $ipprc->filename($output_sources_filerule, $outroot);
-                    &my_die("Couldn't find expected output file: $outputSources", $sky_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($outputSources);
-                    my $outputPSF  = $ipprc->filename($output_psf_filerule, $outroot);
-                    &my_die("Couldn't find expected output file: $outputPSF", $sky_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($outputPSF);
-                }
-
-                &my_die("Couldn't find expected output file: $configuration", $sky_id, $PS_EXIT_SYS_ERROR)
-                    unless $ipprc->file_exists($configuration);
-            } else {
-                print "Not executing: $command\n";
-            }
-        }
 }
 
@@ -377,10 +334,13 @@
     {
         my $command = "$staticskytool -sky_id $sky_id";
-        $command .= " -addresult -path_base $outroot";
-        $command .= " -num_inputs $nInputs";
-        # $command .= " $cmdflags";
-        $command .= (" -dtime_script " . ((DateTime->now->mjd - $mjd_start) * 86400));
-        $command .= " -hostname $host" if defined $host;
-        $command .= " -dbname $dbname" if defined $dbname;
+        if ($updatemode) {
+            $command .= " -updaterun -set_state full";
+        } else {
+            $command .= " -addresult -path_base $outroot";
+            $command .= " -num_inputs $nInputs";
+            $command .= (" -dtime_script " . ((DateTime->now->mjd - $mjd_start) * 86400));
+            $command .= " -hostname $host" if defined $host;
+            $command .= " -dbname $dbname" if defined $dbname;
+        }
 
         my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
@@ -388,5 +348,5 @@
         unless ($success) {
             $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
-            my $err_message = "Unable to perform staticskytool -addresult";
+            my $err_message = "Unable to perform: $command\n";
             warn($err_message);
             exit $error_code;
@@ -406,10 +366,15 @@
     warn($msg);
     if (defined $sky_id and not $no_update) {
+        
         my $command = "$staticskytool -sky_id $sky_id -fault $exit_code";
-        $command .= " -addresult";
-        $command .= (" -dtime_script " . ((DateTime->now->mjd - $mjd_start) * 86400));
-        $command .= " -hostname $host" if defined $host;
-        $command .= " -path_base $outroot" if defined $outroot;
-        $command .= " -dbname $dbname" if defined $dbname;
+        if ($updatemode) {
+            $command .= " -updateresult";
+        } else {
+            $command .= " -addresult";
+            $command .= (" -dtime_script " . ((DateTime->now->mjd - $mjd_start) * 86400));
+            $command .= " -hostname $host" if defined $host;
+            $command .= " -path_base $outroot" if defined $outroot;
+            $command .= " -dbname $dbname" if defined $dbname;
+        }
         run(command => $command, verbose => $verbose);
     }
