Index: trunk/ippScripts/scripts/diff_skycell.pl
===================================================================
--- trunk/ippScripts/scripts/diff_skycell.pl	(revision 24179)
+++ trunk/ippScripts/scripts/diff_skycell.pl	(revision 26567)
@@ -35,5 +35,5 @@
 }
 
-my ($diff_id, $dbname, $threads, $outroot, $reduction, $inverse, $verbose, $no_update, $no_op, $redirect);
+my ($diff_id, $dbname, $threads, $outroot, $reduction, $inverse, $run_state, $verbose, $no_update, $no_op, $redirect);
 my ($skycell_id, $diff_skyfile_id);
 GetOptions(
@@ -43,4 +43,5 @@
     'dbname|d=s'        => \$dbname, # Database name
     'threads=s'         => \$threads,   # Number of threads to use
+    'run-state=s'       => \$run_state,   # state for run: 'new' or 'update'
     'outroot=s'         => \$outroot, # Output root name
     'inverse'           => \$inverse, # Make inverse subtraction?
@@ -54,9 +55,10 @@
 pod2usage( -msg => "Unknown option: @ARGV", -exitval => 2 ) if @ARGV;
 pod2usage(
-    -msg => "Required options: --diff_id --skycell_id --outroot",
+    -msg => "Required options: --diff_id --skycell_id --outroot --run-state",
     -exitval => 3,
           ) unless defined $diff_id
     and defined $skycell_id
     and defined $diff_skyfile_id
+    and defined $run_state
     and defined $outroot;
 
@@ -66,4 +68,5 @@
 # my $logDest = $ipprc->filename("LOG.EXP", $outroot);
 my $logDest = "$outroot.log";
+$logDest .= ".update" if $run_state eq "update";
 $ipprc->redirect_output($logDest) or my_die( "Unable to redirect output", $diff_id, $skycell_id, $PS_EXIT_SYS_ERROR ) if $redirect;
 
@@ -155,7 +158,5 @@
 # The output file will be considered magicked if the input has been magicked and the
 # template is either a stack or a warp that has been magicked.
-# note that difftool -inputskyfile outputs the magicked boolean as an int not T or F
-# because the output is constructed from a union of two selects
-my $magicked = $inputMagic && $templateMagic;
+my $magicked = $inputMagic && $templateMagic ? $inputMagic : 0;
 
 # Recipes to use based on reduction class
@@ -212,4 +213,9 @@
 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);
@@ -236,4 +242,10 @@
     $command .= " -stats $outputStats";
     $command .= " -threads $threads" if defined $threads;
+    my $configurationReal = $ipprc->file_resolve($configuration);
+    if ($run_state eq "new") {
+        $command .= " -dumpconfig $configurationReal";
+    } else {
+        $command .= " -ipprc $configurationReal";
+    }
     $command .= " -recipe PPSUB $recipe_ppSub";
     $command .= " -recipe PSPHOT $recipe_psphot";
@@ -245,5 +257,4 @@
     $command .= " -inverse" if $inverse;
     $command .= " -tracedest $traceDest -log $logDest";
-    $command .= " -dumpconfig $configuration";
     $command .= " -dbname $dbname" if defined $dbname;
     $command .= " -image_id $diff_skyfile_id" if defined $diff_skyfile_id;
@@ -295,9 +306,14 @@
     # Add the subtraction result
     {
-        my $command = "$difftool -adddiffskyfile -diff_id $diff_id -skycell_id $skycell_id -path_base $outroot";
-        $command .= " $cmdflags";
-        $command .= " -magicked" if $magicked;
-        $command .= (" -dtime_script " . ((DateTime->now->mjd - $mjd_start) * 86400));
-        $command .= " -hostname $host" if defined $host;
+        my $command = "$difftool -diff_id $diff_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";
+        }
         $command .= " -dbname $dbname" if defined $dbname;
 
@@ -306,5 +322,8 @@
         unless ($success) {
             $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
-            &my_die("Unable to perform difftool -adddiffskyfile: $error_code", $diff_id, $skycell_id, $error_code);
+            my $err_message = $run_state eq "update" ? 
+                "Unable to perform difftool -adddiffskyfile" :
+                "Unable to perform difftool -tofullskyfile";
+            &my_die("$err_message: $error_code", $diff_id, $skycell_id, $error_code);
         }
     }
@@ -323,9 +342,14 @@
     warn($msg);
     if (defined $diff_id and defined $skycell_id and not $no_update) {
-        my $command = "$difftool -adddiffskyfile -diff_id $diff_id -skycell_id $skycell_id -fault $exit_code";
-        $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;
+        my $command = "$difftool -diff_id $diff_id -skycell_id $skycell_id -fault $exit_code";
+        if ($run_state eq 'new') {
+            $command .= " -adddiffskyfile";
+            $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;
+        } else {
+            $command .= " -updatediffskyfile";
+        }
         run(command => $command, verbose => $verbose);
     }
