Index: trunk/ippScripts/scripts/warp_skycell.pl
===================================================================
--- trunk/ippScripts/scripts/warp_skycell.pl	(revision 19396)
+++ trunk/ippScripts/scripts/warp_skycell.pl	(revision 19561)
@@ -25,5 +25,5 @@
 my $ipprc = PS::IPP::Config->new(); # IPP configuration
 
-my ($warp_id, $skycell_id, $tess_dir, $camera, $dbname, $outroot, $threads, $verbose, $no_update, $no_op, $redirect, $save_temps);
+my ($warp_id, $skycell_id, $tess_dir, $camera, $dbname, $outroot, $threads, $run_state, $verbose, $no_update, $no_op, $redirect, $save_temps);
 GetOptions(
     'warp_id|i=s'       => \$warp_id, # Warp identifier
@@ -33,5 +33,6 @@
     'dbname|d=s'        => \$dbname, # Database name
     'outroot=s'         => \$outroot, # Output root name
-    'threads=s'         => \$threads,   # Number of threads to use for ppImage
+    'threads=s'         => \$threads,   # Number of threads to use for pswarp
+    'run-state=s'       => \$run_state,  # 'new' or 'update'
     'verbose'           => \$verbose,   # Print to stdout
     'no-update'         => \$no_update, # Don't update the database?
@@ -43,5 +44,5 @@
 pod2usage( -msg => "Unknown option: @ARGV", -exitval => 2 ) if @ARGV;
 pod2usage(
-    -msg => "Required options: --warp_id --skycell_id --tess_dir --camera --outroot",
+    -msg => "Required options: --warp_id --skycell_id --tess_dir --camera --outroot --run-state",
     -exitval => 3,
 ) unless defined $warp_id
@@ -49,9 +50,11 @@
     and defined $tess_dir
     and defined $camera
-    and defined $outroot;
+    and defined $outroot
+    and defined $run_state;
 
 $ipprc->define_camera($camera);
 
 my $logDest = $ipprc->filename("LOG.EXP", $outroot, $skycell_id);
+$logDest .= ".update" if ($run_state eq 'update');
 
 $ipprc->redirect_output($logDest) if $redirect;
@@ -127,4 +130,9 @@
 my $configuration =  $ipprc->filename("PSWARP.CONFIG", $outroot, $skycell_id);
 
+if ($run_state eq 'update') {
+    $traceDest .= ".update";
+    $outputStats .= ".update";
+}
+
 my $skyFile = $ipprc->filename("SKYCELL.TEMPLATE", $outroot, $skycell_id );
 $ipprc->skycell_file( $tess_dir, $skycell_id, $skyFile, $verbose ) or &my_die("Unable to generate template skycell", $warp_id, $skycell_id, $tess_dir, $PS_EXIT_SYS_ERROR);
@@ -170,4 +178,5 @@
 # Run pswarp
 my $accept = 1;                 # Accept the skycell?
+my $do_stats;
 unless ($no_op) {
     my $command = "$pswarp";
@@ -177,6 +186,4 @@
     $command .= " -astromlist $astromName";
     $command .= " $outroot $skyFile";
-    $command .= " -stats $outputStats";
-    $command .= " -recipe PPSTATS WARPSTATS";
     $command .= " -F PSPHOT.PSF.SAVE PSPHOT.PSF.SKY.SAVE";
     $command .= " -F PSPHOT.OUTPUT PSPHOT.OUT.CMF.MEF";
@@ -185,6 +192,15 @@
     $command .= " -tracedest $traceDest -log $logDest";
     $command .= " -threads $threads" if defined $threads;
-    $command .= " -dumpconfig $configuration";
     $command .= " -dbname $dbname" if defined $dbname;
+    if ($run_state eq 'new') {
+        $command .= " -dumpconfig $configuration";
+        $do_stats = 1;
+    } else {
+        $command .= " -ipprc $configuration";
+    }
+    if ($do_stats) {
+        $command .= " -recipe PPSTATS WARPSTATS";
+        $command .= " -stats $outputStats";
+    }
 
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
@@ -195,55 +211,73 @@
     }
 
-    # Check first for the stats file, and if the ACCEPT flag is set.
-    &my_die("Couldn't find expected output file: $outputStats", $warp_id, $skycell_id, $tess_dir, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($outputStats);
-    # Get the statistics on the warped image
-    my $statsFile;              # File handle
-    open $statsFile, $ipprc->file_resolve($outputStats) or die "Can't open statistics file $outputStats: $!\n";
-    my @contents = <$statsFile>; # Contents of file
-    close $statsFile;
-    my $contents = join "", @contents;
-
-    my $metadata = $mdcParser->parse($contents)
-        or &my_die("Unable to parse metadata config", $warp_id, $skycell_id, $tess_dir, $PS_EXIT_PROG_ERROR);
-    $accept = metadataLookupBool($metadata, "ACCEPT");
-
-    # $accept is set above based on the fraction of lit pixels
-    # XXX for some files, there may not be enough stars to find a good psf.  these should be dropped as well
-    if ($accept && !$ipprc->file_exists($outputPSF)) {
-        $accept = 0;
-    }
-
-    if ($accept) {
-        $stats->parse($metadata) or &my_die("Unable to find all values in statistics output.", $warp_id, $skycell_id, $tess_dir, $PS_EXIT_PROG_ERROR);
-
-        &my_die("Couldn't find expected output file: $outputImage", $warp_id, $skycell_id, $tess_dir, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($outputImage);
-        &my_die("Couldn't find expected output file: $outputMask", $warp_id, $skycell_id, $tess_dir, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($outputMask);
-        &my_die("Couldn't find expected output file: $outputWeight", $warp_id, $skycell_id, $tess_dir, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($outputWeight);
-        &my_die("Couldn't find expected output file: $outputSources", $warp_id, $skycell_id, $tess_dir, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($outputSources);
-#    &my_die("Couldn't find expected output file: $outputPSF", $warp_id, $skycell_id, $tess_dir, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($outputPSF);
-#    &my_die("Couldn't find expected output file: $outputBin1", $warp_id, $skycell_id, $tess_dir, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($outputBin1);
-#    &my_die("Couldn't find expected output file: $outputBin2", $warp_id, $skycell_id, $tess_dir, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($outputBin2);
-    }
-}
-
-unless ($no_update) {
-    # XXX change -tess_id to -tess_dir when db is updated
-    my $command = "$warptool -addwarped";
-    $command .= " -warp_id $warp_id";
-    $command .= " -skycell_id $skycell_id";
-    $command .= " -tess_id $tess_dir";
-    $command .= " -path_base $outroot"; # needed for logfile lookups
-    $command .= " -ignore"           if not $accept; # Completed succesfully, but can't produce product
-    $command .= " -uri $outputImage" if $accept;
-    $command .= $stats->cmdflags()   if $accept;
-    $command .= " -hostname $host"   if defined $host;
-    $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);
-        warn("Unable to perform warptool -addwarped: $error_code\n");
-        exit($error_code);
+    if ($do_stats) {
+        # Check first for the stats file, and if the ACCEPT flag is set.
+        &my_die("Couldn't find expected output file: $outputStats", $warp_id, $skycell_id, $tess_dir, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($outputStats);
+        # Get the statistics on the warped image
+        my $statsFile;              # File handle
+        open $statsFile, $ipprc->file_resolve($outputStats) or die "Can't open statistics file $outputStats: $!\n";
+        my @contents = <$statsFile>; # Contents of file
+        close $statsFile;
+        my $contents = join "", @contents;
+
+        my $metadata = $mdcParser->parse($contents)
+            or &my_die("Unable to parse metadata config", $warp_id, $skycell_id, $tess_dir, $PS_EXIT_PROG_ERROR);
+        $accept = metadataLookupBool($metadata, "ACCEPT");
+
+        # $accept is set above based on the fraction of lit pixels
+        # XXX for some files, there may not be enough stars to find a good psf.  these should be dropped as well
+        if ($accept && !$ipprc->file_exists($outputPSF)) {
+            $accept = 0;
+        }
+
+        if ($accept) {
+            $stats->parse($metadata) or &my_die("Unable to find all values in statistics output.", $warp_id, $skycell_id, $tess_dir, $PS_EXIT_PROG_ERROR);
+
+            &my_die("Couldn't find expected output file: $outputImage", $warp_id, $skycell_id, $tess_dir, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($outputImage);
+            &my_die("Couldn't find expected output file: $outputMask", $warp_id, $skycell_id, $tess_dir, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($outputMask);
+            &my_die("Couldn't find expected output file: $outputWeight", $warp_id, $skycell_id, $tess_dir, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($outputWeight);
+            &my_die("Couldn't find expected output file: $outputSources", $warp_id, $skycell_id, $tess_dir, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($outputSources);
+    #    &my_die("Couldn't find expected output file: $outputPSF", $warp_id, $skycell_id, $tess_dir, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($outputPSF);
+    #    &my_die("Couldn't find expected output file: $outputBin1", $warp_id, $skycell_id, $tess_dir, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($outputBin1);
+    #    &my_die("Couldn't find expected output file: $outputBin2", $warp_id, $skycell_id, $tess_dir, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($outputBin2);
+        }
+
+        unless ($no_update) {
+            # XXX change -tess_id to -tess_dir when db is updated
+            my $command = "$warptool -addwarped";
+            $command .= " -warp_id $warp_id";
+            $command .= " -skycell_id $skycell_id";
+            $command .= " -tess_id $tess_dir";
+            $command .= " -path_base $outroot"; # needed for logfile lookups
+            $command .= " -ignore"           if not $accept; # Completed succesfully, but can't produce product
+            $command .= " -uri $outputImage" if $accept;
+            $command .= $stats->cmdflags()   if $accept;
+            $command .= " -hostname $host"   if defined $host;
+            $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);
+                warn("Unable to perform warptool -addwarped: $error_code\n");
+                exit($error_code);
+            }
+        }
+    } else {
+        # $run_state eq 'update'
+        unless ($no_update) {
+            my $command = "$warptool -tofullskyfile";
+            $command .= " -warp_id $warp_id";
+            $command .= " -skycell_id $skycell_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);
+                warn("Unable to perform warptool -addwarped: $error_code\n");
+                exit($error_code);
+            }
+        }
     }
 }
@@ -260,11 +294,16 @@
     if (defined $warp_id and defined $skycell_id and defined $tess_dir and not $no_update) {
         # XXX change -tess_id to -tess_dir when db is updated
-        my $command = "$warptool -addwarped";
+        my $command = "$warptool";
+        if ($run_state eq 'new') {
+            $command .= " -addwarped";
+            $command .= " -tess_id $tess_dir";
+            $command .= " -path_base $outroot";
+            $command .= " -hostname $host" if defined $host;
+        } else {
+            $command .= " -updateskyfile";
+        }
         $command .= " -warp_id $warp_id";
         $command .= " -skycell_id $skycell_id";
-        $command .= " -tess_id $tess_dir";
         $command .= " -code $exit_code";
-        $command .= " -path_base $outroot";
-        $command .= " -hostname $host" if defined $host;
         $command .= " -dbname $dbname" if defined $dbname;
         run(command => $command, verbose => $verbose);
