Index: trunk/ippScripts/scripts/camera_exp.pl
===================================================================
--- trunk/ippScripts/scripts/camera_exp.pl	(revision 35367)
+++ trunk/ippScripts/scripts/camera_exp.pl	(revision 35715)
@@ -53,5 +53,5 @@
     'skip-binned'       => \$skip_binned, # override recipe - don't create binned images
     'skip-refmask'      => \$skip_masks, # override recipe - don't create refmask
-    'bkg-only'          => \$bkg_only,  # override recipe - only do background continuity
+#    'bkg-only'          => \$bkg_only,  # override recipe - only do background continuity
     'verbose'           => \$verbose,   # Print to stdout
     'no-update'         => \$no_update, # Update the database?
@@ -79,4 +79,6 @@
 
 my $replicateOutputs = 1;
+
+$ipprc->outroot_prepare($outroot);
 
 my $logDest;
@@ -193,5 +195,5 @@
 
 
-my $produceMasks;               # Produce masks?
+my $do_masks;               # Produce masks?
 if (!$skip_masks) {
     # Get the PSASTRO recipe
@@ -206,5 +208,5 @@
         &my_die("Unable to parse metadata config doc", $cam_id, $PS_EXIT_CONFIG_ERROR);
 
-    $produceMasks = metadataLookupBool($recipeData, 'REFSTAR_MASK');
+    $do_masks = metadataLookupBool($recipeData, 'REFSTAR_MASK');
 }
 
@@ -220,9 +222,65 @@
 my ($list5File, $list5Name) = tempfile( "/tmp/$exp_tag.cm.$cam_id.b5.list.XXXX", UNLINK => !$save_temps ); # For background models
 
-# XXX we perform astrometry iff photometry output exists
+
+my $do_stats;
+my $do_bkg;
+my $do_jpegs;
+my $fpaStats; 
+my $psastroInputArg;
+if ($run_state eq 'new') {
+    $do_stats = 1;
+    $do_bkg = 1;
+    $do_jpegs = !$skip_binned;
+    $fpaStats = prepare_output("PSASTRO.STATS",      $outroot, undef, 1);
+    $psastroInputArg = " -list $list3Name";
+} else {
+    # for $run_state eq 'update' we onlly rebuild the masks using psastro
+    $do_stats = 0;
+    $do_jpegs = 0;
+    $do_bkg = 1;        # we could skip this step if camProcessedExp.backgroun_model is non zero
+
+    if (!$do_masks) {
+        &my_die("run_state is update but do_masks is F. I have nothing to do!!", $cam_id, $PS_EXIT_UNKNOWN_ERROR)
+    }
+
+    # the input to psastro is the original PSASTRO.OUTPUT
+    
+    # Check for file with rule PSASTRO.OUTPUT.ORGINAL
+    # This file will exist if we attempted to update this camRun before but faulted
+    # XXX: make sure cleanup deals with these files
+    my $inputObjects = $ipprc->filename("PSASTRO.OUTPUT.ORIGINAL", $outroot, undef);
+    if (!$ipprc->file_exists($inputObjects)) {
+        # not found so original file should still be in place. 
+        # Rename it.
+        my $originalObjects = $ipprc->filename("PSASTRO.OUTPUT", $outroot, undef);
+        if ($ipprc->file_exists($originalObjects)) {
+
+            unless ($ipprc->file_rename($originalObjects, $inputObjects)) {
+                &my_die("failed to rename $originalObjects to $inputObjects", $cam_id, $PS_EXIT_UNKNOWN_ERROR);
+            }
+
+            # ok ready to go
+        } else {
+            print STDERR "failed to find input objects to update\n";
+            print STDERR "Original file name: $originalObjects\n";
+            print STDERR "Saved file name:    $inputObjects\n";
+
+            &my_die("Cannot proceed.", $cam_id, $PS_EXIT_DATA_ERROR);
+
+            # XXX: actually we could use the chip stage cmfs as inputs and turn on astrometry 
+            # ... except that somebody (me) got the bright idea to save space and 
+            # changed chip stage cleanup to remove them. So we're kind of stuck.
+        }
+    }
+    $psastroInputArg = " -file $inputObjects -skipastro";
+}
+    
+
 my @outMasks;                   # Names of output masks
 my @bkg_models;                 # Names of output background models
 foreach my $file (@$files) {
+    # we perform astrometry iff photometry output exists
     next if $file->{quality} != 0;
+
     # use the path_base as OUTPUT root and convert the filenames with ipprc->filename:
     my $class_id = $file->{class_id};
@@ -239,6 +297,6 @@
     print $list5File ($ipprc->filename("PSPHOT.BACKMDL", $file->{path_base}, $class_id) . "\n");
 
-    push @outMasks, prepare_output("PSASTRO.OUTPUT.MASK", $outroot, $class_id, 1) if $produceMasks;
-    push @bkg_models, prepare_output("PPIMAGE.BACKMDL", $outroot, $class_id, 1);
+    push @outMasks, prepare_output("PSASTRO.OUTPUT.MASK", $outroot, $class_id, 1) if $do_masks;
+    push @bkg_models, prepare_output("PPIMAGE.BACKMDL", $outroot, $class_id, 1) if $do_bkg;
 }
 close $list1File;
@@ -247,21 +305,21 @@
 close $list4File;
 close $list5File;
-# Output products
-$ipprc->outroot_prepare($outroot);
+
+# Prepare the Output products
 
 # the camera configurations should define the psastro output to be a single file (MEF), regardless of the inputs
-my $jpeg1      = prepare_output("PPIMAGE.JPEG1",      $outroot, undef, 1);
-my $jpeg2      = prepare_output("PPIMAGE.JPEG2",      $outroot, undef, 1);
-my $fpaObjects = prepare_output("PSASTRO.OUTPUT",     $outroot, undef, 1);
-my $configuration = prepare_output("PSASTRO.CONFIG",  $outroot, undef, 1);
-
-my $do_stats;
-my $fpaStats; 
+my $jpeg1      = prepare_output("PPIMAGE.JPEG1",      $outroot, undef, 1) if $do_jpegs;
+my $jpeg2      = prepare_output("PPIMAGE.JPEG2",      $outroot, undef, 1) if $do_jpegs;
+my $fpaObjects = prepare_output("PSASTRO.OUTPUT",     $outroot, undef, 1) if !$bkg_only;
+
+my $configuration;
 if ($run_state eq 'new') {
-    $do_stats = 1;
-    $fpaStats = prepare_output("PSASTRO.STATS",      $outroot, undef, 1);
+    $configuration = prepare_output("PSASTRO.CONFIG",  $outroot, undef, 1);
 } else {
-    $do_stats = 0;
-}
+    # Do not use the original recipes because they might contain the 
+    # recipe values that caused the masks to get fouled up in the first place
+    # $configuration = $ipprc->filename("PSASTRO.CONFIG",  $outroot, undef);
+}
+
 
 unless ($no_op) {
@@ -269,6 +327,6 @@
     ## build the output JPEG images first so we get them even if the astrometry fails
 
-    # Make the jpeg for binning 1
-    if (!$skip_binned and ($run_state eq 'new')) {
+    if ($do_jpegs) {
+        # Make the jpeg for binning 1
         my $command = "$ppImage -list $list1Name $outroot"; # Command to run
         $command .= " -recipe PPIMAGE $recipe1";
@@ -282,13 +340,12 @@
         }
         check_output($jpeg1, $replicateOutputs);
-    }
-
-    # Make the jpeg for binning 2
-    if (!$skip_binned and ($run_state eq 'new')) {
-        my $command = "$ppImage -list $list2Name $outroot"; # Command to run
+
+        # Make the jpeg for binning 2
+
+        $command = "$ppImage -list $list2Name $outroot"; # Command to run
         $command .= " -recipe PPIMAGE $recipe2";
         $command .= " -dbname $dbname" if defined $dbname;
 
-        my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+        ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
             run(command => $command, verbose => $verbose);
         unless ($success) {
@@ -302,10 +359,10 @@
         # run psastro on the chipObjects, producing fpaObjects
         my $command;
-        $command  = "$psastro";
-        $command .= " -list $list3Name";
-        $command .= " -masklist $list4Name" if $produceMasks;
+        $command  = $psastro;
+        $command .= " $psastroInputArg";
+        $command .= " -masklist $list4Name" if $do_masks;
         $command .= " $outroot";
         $command .= " -recipe PSASTRO $recipe_psastro";
-        $command .= " -Db PSASTRO:REFSTAR_MASK F" if $skip_masks;
+        $command .= " -Db PSASTRO:REFSTAR_MASK F" if !$do_masks;
         $command .= " -tracedest $traceDest -log $logDest";
         $command .= " -dbname $dbname" if defined $dbname;
@@ -314,5 +371,5 @@
             $command .= " -dumpconfig $configuration";
         } elsif ($run_state eq 'update') {
-            $command .= " -ipprc $configuration";
+            $command .= " -ipprc $configuration" if $configuration;
         } else {
             &my_die("invalid value for run-state: $run_state", $cam_id, $PS_EXIT_CONFIG_ERROR);
@@ -362,8 +419,9 @@
         }
     }
+
     # Construct FPA continuity corrected background images
     if ($camera =~ /ISP/) {
 	print "Skipping FPA continuity corrected background images for ISP\n";
-    } else {
+    } elsif ($do_bkg) {
 	my $command;
 	$command = "$ppImage";
@@ -381,5 +439,16 @@
 	    check_output($bkgModel, $replicateOutputs);
 	}
-	$cmdflags = $cmdflags . " -background_model 1 ";
+        if ($run_state eq 'new') {
+            $cmdflags .= " -background_model 1 ";
+        } else {
+            my $command = "camtool -updateprocessedexp -set_background_model 1 -fault 0 -cam_id $cam_id";
+            $command .= " -dbname $dbname" if $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 updateprocessedexp to set background_model: $error_code", $cam_id, $error_code);
+            }
+        }
     }
 }
