Index: trunk/ippScripts/scripts/camera_exp.pl
===================================================================
--- trunk/ippScripts/scripts/camera_exp.pl	(revision 19560)
+++ trunk/ippScripts/scripts/camera_exp.pl	(revision 19561)
@@ -27,5 +27,5 @@
 
 my ( $exp_tag, $cam_id, $camera, $outroot, $recipe, $dbname, $reduction, $dvodb, $verbose, $no_update,
-     $no_op, $redirect, $save_temps );
+     $no_op, $redirect, $save_temps, $run_state);
 GetOptions(
     'exp_tag=s'          => \$exp_tag, # Exposure identifier
@@ -37,4 +37,5 @@
     'reduction=s'       => \$reduction, # Reduction class
     'dvodb|w=s'         => \$dvodb,  # output DVO database
+    'run-state=s'       => \$run_state, # 'new' or 'update'
     'verbose'           => \$verbose,   # Print to stdout
     'no-update'         => \$no_update, # Update the database?
@@ -57,4 +58,7 @@
 
 my $logDest = $ipprc->filename("LOG.EXP", $outroot) or &my_die("Missing entry from camera config", $cam_id, $PS_EXIT_CONFIG_ERROR);
+if ($run_state eq 'update') {
+    $logDest .= '.update';
+}
 
 $ipprc->redirect_output($logDest) if $redirect;
@@ -196,4 +200,10 @@
 my $fpaStats   = $ipprc->filename("PSASTRO.STATS",      $outroot) or &my_die("Missing entry from camera config", $cam_id, $PS_EXIT_CONFIG_ERROR);
 my $traceDest  = $ipprc->filename("TRACE.EXP",          $outroot) or &my_die("Missing entry from camera config", $cam_id, $PS_EXIT_CONFIG_ERROR);
+my $configuration = $ipprc->filename("PSASTRO.CONFIG",  $outroot) or &my_die("Missing entry from camera config", $cam_id, $PS_EXIT_CONFIG_ERROR);
+
+if ($run_state eq 'update') {
+    $traceDest .= '.update';
+    $fpaStats .= '.update';
+}
 
 # convert supplied DVO database name to UNIX filename
@@ -210,5 +220,5 @@
 
     # Make the jpeg for binning 1
-    {
+    if ($run_state eq 'new') {
         my $command = "$ppImage -list $list1Name $outroot -recipe PPIMAGE $recipe1"; # Command to run
         $command .= " -dbname $dbname" if defined $dbname;
@@ -224,5 +234,5 @@
 
     # Make the jpeg for binning 2
-    {
+    if ($run_state eq 'new') {
         my $command = "$ppImage -list $list2Name $outroot -recipe PPIMAGE $recipe2"; # Command to run
         $command .= " -dbname $dbname" if defined $dbname;
@@ -244,6 +254,16 @@
         $command  = "$psastro -list $list3Name $outroot";
         $command .= " -tracedest $traceDest -log $logDest";
-        $command .= " -stats $fpaStats -recipe PPSTATS CHIPSTATS";
         $command .= " -dbname $dbname" if defined $dbname;
+
+        my $do_stats;
+        if ($run_state eq 'new') {
+            $command .= " -stats $fpaStats -recipe PPSTATS CHIPSTATS";
+            $command .= " -dumpconfig $configuration";
+            $do_stats = 1;
+        } elsif ($run_state eq 'update') {
+            $command .= " -ipprc $configuration";
+        } else {
+            &my_die("invalid value for run-state: $run_state", $cam_id, $PS_EXIT_CONFIG_ERROR);
+        }
 
         my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
@@ -255,26 +275,29 @@
         # XXX do we want to give an error if astrometry fails here?
         &my_die("Unable to find expected output file: $fpaObjects", $cam_id, $PS_EXIT_PROG_ERROR) unless -f $ipprc->file_resolve($fpaObjects);
-        &my_die("Unable to find expected output file: $fpaStats",   $cam_id, $PS_EXIT_PROG_ERROR) unless -f $ipprc->file_resolve($fpaObjects);
-
-        # Get the statistics on the processed image
-        my $statsFile;              # File handle
-        open $statsFile, $ipprc->file_resolve($fpaStats) or &my_die("Can't open statistics file $fpaStats: $!", $cam_id, $PS_EXIT_SYS_ERROR);
-        my @contents = <$statsFile>; # Contents of file
-        close $statsFile;
-
-        # parse the statistics MDC file
-        my $mdcParser = PS::IPP::Metadata::Config->new();   # Parser for metadata config files
-        my $metadata = $mdcParser->parse(join "", @contents);
-        unless ($metadata) {
-            &my_die("Unable to parse metadata config doc", $cam_id, $PS_EXIT_PROG_ERROR);
-        }
-
-        # extract the stats from the metadata
-        unless ($camStats->parse($metadata)) {
-            &my_die("Failure extracting metadata from the statistics output file.\n", $cam_id, $PS_EXIT_PROG_ERROR);
+
+        if ($do_stats) {
+            &my_die("Unable to find expected output file: $fpaStats",   $cam_id, $PS_EXIT_PROG_ERROR) unless -f $ipprc->file_resolve($fpaObjects);
+
+            # Get the statistics on the processed image
+            my $statsFile;              # File handle
+            open $statsFile, $ipprc->file_resolve($fpaStats) or &my_die("Can't open statistics file $fpaStats: $!", $cam_id, $PS_EXIT_SYS_ERROR);
+            my @contents = <$statsFile>; # Contents of file
+            close $statsFile;
+
+            # parse the statistics MDC file
+            my $mdcParser = PS::IPP::Metadata::Config->new();   # Parser for metadata config files
+            my $metadata = $mdcParser->parse(join "", @contents);
+            unless ($metadata) {
+                &my_die("Unable to parse metadata config doc", $cam_id, $PS_EXIT_PROG_ERROR);
+            }
+
+            # extract the stats from the metadata
+            unless ($camStats->parse($metadata)) {
+                &my_die("Failure extracting metadata from the statistics output file.\n", $cam_id, $PS_EXIT_PROG_ERROR);
+            }
         }
 
         # run addstar on the output fpaObjects (if a DVO database is defined)
-        if (defined $dvodbReal) {
+        if (defined $dvodbReal and ($run_state eq 'new')) {
 
             ## XXX the camera analysis can either save the full set of
@@ -319,12 +342,17 @@
 }
 
-my $fpaCommand = "$camtool -addprocessedexp";
-$fpaCommand .= " -cam_id $cam_id";
-$fpaCommand .= " -uri UNKNOWN";
-$fpaCommand .= " -path_base $outroot";
-$fpaCommand .= " -hostname $host" if defined $host;
+
+my $fpaCommand = "$camtool -cam_id $cam_id";
+if ($run_state eq 'new') {
+    $fpaCommand .= " -addprocessedexp";
+    $fpaCommand .= " -uri UNKNOWN";
+    $fpaCommand .= " -path_base $outroot";
+    $fpaCommand .= $chipStats->cmdflags();
+    $fpaCommand .= $camStats->cmdflags();
+    $fpaCommand .= " -hostname $host" if defined $host;
+} else {
+    $fpaCommand .= " -updaterun -state full";
+}
 $fpaCommand .= " -dbname $dbname" if defined $dbname;
-$fpaCommand .= $chipStats->cmdflags();
-$fpaCommand .= $camStats->cmdflags();
 
 # Add the result into the database
@@ -350,12 +378,17 @@
     carp($msg);
     if (defined $cam_id and not $no_update) {
-        my $command = "$camtool -addprocessedexp";
-        $command .= " -cam_id $cam_id";
-        $command .= " -uri UNKNOWN";
-        $command .= " -code $exit_code";
-        $command .= " -path_base $outroot";
+        my $command = "$camtool -cam_id $cam_id";
+        if ($run_state eq 'new') {
+            $command .= " -addprocessedexp";
+            $command .= " -uri UNKNOWN";
+            $command .= " -code $exit_code";
+            $command .= " -path_base $outroot";
+            $command .= " -path_base $outroot" if defined $outroot;
+        } else {
+            $command .= " -updateprocessedexp";
+            $command .= " -code $exit_code";
+        }
         $command .= " -hostname $host" if defined $host;
         $command .= " -dbname $dbname" if defined $dbname;
-        $command .= " -path_base $outroot" if defined $outroot;
         system ($command);
     }
Index: trunk/ippScripts/scripts/ipp_cleanup.pl
===================================================================
--- trunk/ippScripts/scripts/ipp_cleanup.pl	(revision 19560)
+++ trunk/ippScripts/scripts/ipp_cleanup.pl	(revision 19561)
@@ -50,13 +50,17 @@
 }
 
+my %stages = ( chip => 1, camera => 1, fake => 1, warp => 1, stack => 1, diff  => 1);
+unless ($stages{$stage}) {
+    die "unknown stage $stage for ipp_cleanup.pl\n";
+}
+
 $ipprc->define_camera($camera);
 
 my $mdcParser = PS::IPP::Metadata::Config->new; # Parser for metadata config files
-
-my $fault = 0;
 
 # choice of files to delete depends on the stage
 if ($stage eq "chip") {
     
+    die "--stage_id required for stage chip\n" if !$stage_id;
     ### select the imfiles for this entry
 
@@ -85,28 +89,43 @@
 	my $class_id = $imfile->{class_id};
 	my $path_base = $imfile->{path_base};
-
-	# array of actual filenames to delete
-	my @files = ();
-
-	# delete the temporary image datafiles
-	addFilename (\@files, "PPIMAGE.OUTPUT", $path_base, $class_id);
-	addFilename (\@files, "PPIMAGE.OUTPUT.MASK", $path_base, $class_id);
-	addFilename (\@files, "PPIMAGE.OUTPUT.WEIGHT", $path_base, $class_id);
-	addFilename (\@files, "PPIMAGE.CHIP", $path_base, $class_id);
-	addFilename (\@files, "PPIMAGE.CHIP.MASK", $path_base, $class_id);
-	addFilename (\@files, "PPIMAGE.CHIP.WEIGHT", $path_base, $class_id);
-	if ($mode eq "goto_purged") {
-	    # additional files to remove for 'purge' mode
-	    addFilename (\@files, "PPIMAGE.OUTPUT.FPA1", $path_base, $class_id);
-	    addFilename (\@files, "PPIMAGE.OUTPUT.FPA2", $path_base, $class_id);
-	    addFilename (\@files, "PPIMAGE.BIN1", $path_base, $class_id);
-	    addFilename (\@files, "PPIMAGE.BIN2", $path_base, $class_id);
-	    addFilename (\@files, "PPIMAGE.JPEG1", $path_base, $class_id);
-	    addFilename (\@files, "PPIMAGE.JPEG", $path_base, $class_id);
-	    addFilename (\@files, "PPIMAGE.STATS", $path_base, $class_id);
-	}
+        my $status = 1;
+
+        # don't clean up unless the data needed to update is available
+        if ($mode eq "goto_cleaned") {
+            my $config_file = $ipprc->filename("PPIMAGE.CONFIG", $path_base, $class_id);
+
+            if (!$config_file or ! -e $config_file) {
+                print STDERR "skipping cleanup for chipRun $stage_id $class_id "
+                    . " because config file is missing\n";
+                $status = 0;
+            }
+        }
+
+        if ($status) {
+            # array of actual filenames to delete
+            my @files = ();
+
+            # delete the temporary image datafiles
+            addFilename (\@files, "PPIMAGE.OUTPUT", $path_base, $class_id);
+            addFilename (\@files, "PPIMAGE.OUTPUT.MASK", $path_base, $class_id);
+            addFilename (\@files, "PPIMAGE.OUTPUT.WEIGHT", $path_base, $class_id);
+            addFilename (\@files, "PPIMAGE.CHIP", $path_base, $class_id);
+            addFilename (\@files, "PPIMAGE.CHIP.MASK", $path_base, $class_id);
+            addFilename (\@files, "PPIMAGE.CHIP.WEIGHT", $path_base, $class_id);
+            if ($mode eq "goto_purged") {
+                # additional files to remove for 'purge' mode
+                addFilename (\@files, "PPIMAGE.OUTPUT.FPA1", $path_base, $class_id);
+                addFilename (\@files, "PPIMAGE.OUTPUT.FPA2", $path_base, $class_id);
+                addFilename (\@files, "PPIMAGE.BIN1", $path_base, $class_id);
+                addFilename (\@files, "PPIMAGE.BIN2", $path_base, $class_id);
+                addFilename (\@files, "PPIMAGE.JPEG1", $path_base, $class_id);
+                addFilename (\@files, "PPIMAGE.JPEG", $path_base, $class_id);
+                addFilename (\@files, "PPIMAGE.STATS", $path_base, $class_id);
+                addFilename (\@files, "PPIMAGE.CONFIG", $path_base, $class_id);
+            }
 	
-	# actual command to delete the files
-	my $status = &delete_files (\@files);
+            # actual command to delete the files
+            $status = &delete_files (\@files);
+        }
 
 	if ($status)  {
@@ -119,5 +138,6 @@
 	    $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);
@@ -125,43 +145,18 @@
 	    }
         } else {
-	    my $command = "$chiptool -updateimfile -chip_id $stage_id -class_id $class_id -code 1";
+	    my $command = "$chiptool -updateprocessedimfile -chip_id $stage_id -class_id $class_id -code 1";
 	    $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 chiptool: $error_code", "chip", $stage_id, $error_code);
 	    }
-	    $fault = 1;
 	}
     }
 
-if (0) {
-    # This block is not necessary
-    # chiptool -tocleanedimfile and -topurgedimfile automatically update chipRun when the
-    # last image gets set to purged or cleaned
-
-    if (! $fault) {
-	# set the state of the chipRun entry to "cleaned" or "purged" as needed
-	my $command = "$chiptool -updaterun -chip_id $stage_id";
-	if ($mode eq "goto_purged") {
-	    $command .= " -state purged";
-	} else {
-	    $command .= " -state cleaned";
-	}
-	$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 chiptool: $error_code", "chip", $stage_id, $error_code);
-	}
-    }
-}
-    exit 0;
-}
-
-# choice of files to delete depends on the stage
-if ($stage eq "camera") {
+} elsif ($stage eq "camera") {
+    die "--stage_id required for stage camera\n" if !$stage_id;
     # this stage uses 'camtool'
     my $camtool = can_run('camtool') or die "Can't find camtool";
@@ -169,5 +164,5 @@
     # Get list of component imfiles
     # XXX may need a different my_die for each stage
-    my $imfiles;                      # Array of component files
+    my $exps;                      # Array of component files
     my $command = "$camtool -pendingcleanupexp -cam_id $stage_id"; # Command to run
     $command .= " -dbname $dbname" if defined $dbname;
@@ -175,17 +170,167 @@
     unless ($success) {
         $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
-        &my_die("Unable to perform camtool: $error_code", "cam", $stage_id, $error_code);
+        &my_die("Unable to perform camtool: $error_code", "camera", $stage_id, $error_code);
     }
     my $metadata = $mdcParser->parse(join "", @$stdout_buf) or
-        &my_die("Unable to parse metadata config doc", "cam", $stage_id, $PS_EXIT_PROG_ERROR);
-
+        &my_die("Unable to parse metadata config doc", "camera", $stage_id, $PS_EXIT_PROG_ERROR);
+
+    $exps = parse_md_list($metadata) or
+        &my_die("Unable to parse metadata list", "camera", $stage_id, $PS_EXIT_PROG_ERROR);
+
+    my $n_exps = @$exps;
+    &my_die("unexpected number of exposures $n_exps", "camera", $stage_id, $PS_EXIT_PROG_ERROR)
+        if $n_exps != 1;
+
+    my $exp = $exps->[0];
+    my $path_base = $exp->{path_base};
+
+    my $status = 1;
+    # don't clean up unless the data needed to update is available
+    if ($mode eq "goto_cleaned") {
+        my $config_file = $ipprc->filename("PSASTRO.CONFIG", $path_base);
+
+        if (!$config_file or ! -e $config_file) {
+            print STDERR "skipping cleanup for camRun $stage_id because config file is missing\n";
+            $status = 0;
+        }
+    }
+    if ($status) {
+        my @files = ();
+        # delete the temporary image datafiles
+        addFilename (\@files, "PSASTRO.OUTPUT", $path_base);
+        if ($mode eq "goto_purged") {
+            # additional files to remove for 'purge' mode
+            addFilename (\@files, "PPIMAGE.JPEG1", $path_base);
+            addFilename (\@files, "PPIMAGE.JPEG2", $path_base);
+            addFilename (\@files, "PSASTRO.STATS", $path_base);
+        }
+        # actual command to delete the files
+        $status = &delete_files (\@files);
+    }
+
+    if ($status)  {
+        my $command = "$camtool -cam_id $stage_id -updaterun";
+        if ($mode eq "goto_cleaned") {
+            $command .= " -state cleaned";
+        } else {
+            $command .= " -state purged";
+        }
+        $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 camtool: $error_code", "camera", $stage_id, $error_code);
+        }
+    } else {
+        my $command = "$camtool -updateprocessedexp -cam_id $stage_id -code 1";
+        $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 camtool: $error_code", "camera", $stage_id, $error_code);
+        }
+        exit $PS_EXIT_UNKNOWN_ERROR;
+    }
     exit 0;
-}
-
+} elsif ($stage eq "warp") {
+    die "--stage_id required for stage warp\n" if !$stage_id;
+    # this stage uses 'warptool'
+    my $warptool = can_run('warptool') or die "Can't find warptool";
+
+    # Get list of component imfiles
+    # XXX may need a different my_die for each stage
+    my $skyfiles;                      # Array of component files
+    my $command = "$warptool -pendingcleanupskyfile -warp_id $stage_id"; # Command to run
+    $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 warptool: $error_code", "warp", $stage_id, $error_code);
+    }
+    my $metadata = $mdcParser->parse(join "", @$stdout_buf) or
+        &my_die("Unable to parse metadata config doc", "warp", $stage_id, $PS_EXIT_PROG_ERROR);
+
+    $skyfiles = parse_md_list($metadata) or
+        &my_die("Unable to parse metadata list", "warp", $stage_id, $PS_EXIT_PROG_ERROR);
+
+    my @files = ();
+    foreach my $skyfile (@$skyfiles) {
+        my $path_base = $skyfile->{path_base};
+        my $skycell_id = $skyfile->{skycell_id};
+
+        my $status = 1;
+        if ($mode eq "goto_cleaned") {
+            my $config_file = $ipprc->filename("PSWARP.CONFIG", $path_base, $skycell_id);
+
+            if (!$config_file or ! -e $config_file) {
+                print STDERR "skipping cleanup for warpRun $stage_id $skycell_id" .
+                    " because config file is missing\n";
+                $status = 0;
+            }
+        }
+        if ($status) {
+            # delete the temporary image datafiles
+            addFilename(\@files, "PSWARP.OUTPUT", $path_base, $skycell_id );
+            addFilename(\@files, "PSWARP.OUTPUT.MASK", $path_base, $skycell_id);
+            addFilename(\@files, "PSWARP.OUTPUT.WEIGHT", $path_base, $skycell_id);
+            addFilename(\@files, "PSWARP.OUTPUT.SOURCES", $path_base, $skycell_id);
+
+            if ($mode eq "goto_purged") {
+                # additional files to remove for 'purge' mode
+                addFilename(\@files, "PSWARP.BIN1", $path_base, $skycell_id );
+                addFilename(\@files, "PSWARP.BIN2", $path_base, $skycell_id );
+                addFilename(\@files, "SKYCELL.STATS", $path_base, $skycell_id );
+                # addFilename(\@files, "PSPHOT.PSF.SKY.SAVE", $path_base);
+
+                # XXX: do we want to delete these?
+                # addFilename(\@files, "TRACE.EXP", $path_base, $skycell_id);
+                # addFilename(\@files, "PSWARP.CONFIG", $path_base, $skycell_id);
+            }
+            # actual command to delete the files
+            $status = &delete_files (\@files);
+        }
+
+	if ($status)  {
+	    my $command = "$warptool -warp_id $stage_id -skycell_id $skycell_id";
+            if ($mode eq "goto_purged") {
+                $command .= " -topurgedskyfile";
+            } else {
+                $command .= " -tocleanedskyfile";
+            }
+	    $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 warptool: $error_code", "warp", $stage_id, $error_code);
+	    }
+         } else {
+            # XXX: -updateskyfile mode does not exist, need to add it
+	    my $command = "$warptool -updateskyfile -warp_id $stage_id -skycell_id $skycell_id -code 1";
+	    $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 warptool: $error_code", "warp", $stage_id, $error_code);
+	    }
+            exit $PS_EXIT_UNKNOWN_ERROR;
+	}
+    }
+    exit 0;
+}
+
+# left TODO
 # fake : faketool : -pendingcleanupimfile (loop over imfiles)
-# warp : warptool : -pendingcleanupskyfile (loop over skyfiles)
-
-die "unknown stage for ipp_cleanup.pl\n";
-exit 1;
+# stack: stacktool : -pendingcleanupskyfile (loop over skyfiles)
+# diff:  difftool : -pendingcleanupskyfile
+
+die "ipp_cleanup.pl -stage $stage not yet implemented\n";
 
 sub delete_files 
@@ -196,4 +341,5 @@
     foreach my $file (@$files) {
 	print STDERR "unlinking $file\n";
+        # XXX: need to handle files in nebulous
 	unlink $file;
     }
@@ -217,8 +363,8 @@
 {
     my $msg = shift; # Warning message on die
-    my $stage = shift; # Chiptool identifier
-    my $stage_id = shift; # Chiptool identifier
-    my $exit_code = shift; # Exit code to add
-    # outputImage and outroot are globals
+    my $stage = shift; # stage name
+    my $stage_id = shift; #  identifier
+    my $exit_code = shift; # Exit code
+    # outputImage and path_base are globals
 
     carp($msg);
Index: trunk/ippScripts/scripts/stack_skycell.pl
===================================================================
--- trunk/ippScripts/scripts/stack_skycell.pl	(revision 19560)
+++ trunk/ippScripts/scripts/stack_skycell.pl	(revision 19561)
@@ -28,9 +28,10 @@
 use Pod::Usage qw( pod2usage );
 
-my ($stack_id, $dbname, $outroot, $debug, $threads, $verbose, $no_update, $no_op, $redirect, $save_temps);
+my ($stack_id, $dbname, $outroot, $debug, $run_state, $threads, $verbose, $no_update, $no_op, $redirect, $save_temps);
 GetOptions(
     'stack_id|d=s'      => \$stack_id, # Stack identifier
     'dbname|d=s'        => \$dbname, # Database name
     'outroot=s'         => \$outroot, # Output root name
+    'run-state=s'       => \$run_state,
     'debug'             => \$debug,   # Print to stdout
     'threads=s'       	=> \$threads,   # Number of threads to use for ppStack
@@ -44,8 +45,9 @@
 pod2usage( -msg => "Unknown option: @ARGV", -exitval => 2 ) if @ARGV;
 pod2usage(
-    -msg => "Required options: --stack_id --outroot",
+    -msg => "Required options: --stack_id --outroot --run-state",
     -exitval => 3,
           ) unless defined $stack_id
-    and defined $outroot;
+    and defined $outroot
+    and defined $run_state;
 
 # XXX camera is not known here; cannot use filerules...
Index: trunk/ippScripts/scripts/warp_skycell.pl
===================================================================
--- trunk/ippScripts/scripts/warp_skycell.pl	(revision 19560)
+++ 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);
