Index: /trunk/ippScripts/scripts/camera_exp.pl
===================================================================
--- /trunk/ippScripts/scripts/camera_exp.pl	(revision 35714)
+++ /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);
+            }
+        }
     }
 }
Index: /trunk/ippconfig/recipes/filerules-split.mdc
===================================================================
--- /trunk/ippconfig/recipes/filerules-split.mdc	(revision 35714)
+++ /trunk/ippconfig/recipes/filerules-split.mdc	(revision 35715)
@@ -14,4 +14,5 @@
 
 PSASTRO.OUTPUT     STR PSASTRO.OUT.CMF.MEF
+PSASTRO.OUTPUT.ORIGINAL     STR PSASTRO.OUT.ORIGINAL.CMF.MEF
 # PSASTRO.OUTPUT.SPL STR PSASTRO.OUT.CMF.SPL
 # PSASTRO.OUTPUT.MEF STR PSASTRO.OUT.CMF.MEF
@@ -275,4 +276,6 @@
 PSASTRO.CONFIG               OUTPUT {OUTPUT}.psastro.mdc              TEXT            NONE       FPA        TRUE      NONE
 PSASTRO.OUTPUT.MASK          OUTPUT {OUTPUT}.{CHIP.NAME}.mk.fits      MASK            COMP_MASK  CHIP       TRUE      NONE
+
+PSASTRO.OUT.ORIGINAL.CMF.MEF OUTPUT {OUTPUT}.smf.original             CMF             NONE       FPA        TRUE      MEF
                                                                                                               
 PSWARP.OUTPUT                OUTPUT {OUTPUT}.fits                     IMAGE           COMP_IMG   FPA        TRUE      NONE
Index: /trunk/psastro/src/psastro.h
===================================================================
--- /trunk/psastro/src/psastro.h	(revision 35714)
+++ /trunk/psastro/src/psastro.h	(revision 35715)
@@ -83,5 +83,5 @@
 bool              psastroOneChipGrid (pmFPA *fpa, pmChip *chip, psArray *refset, psArray *rawset, psMetadata *recipe, psMetadata *updates);
 bool              psastroOneChipFit (pmFPA *fpa, pmChip *chip, pmReadout *readout, psArray *refset, psArray *rawset, psMetadata *recipe, psMetadata *updates);
-bool              psastroChooseRefstars (pmConfig *config, psArray *refs, const char *source);
+bool              psastroChooseRefstars (pmConfig *config, psArray *refs, const char *source, bool saveExistingMatchedRefs);
 bool              psastroRefstarSubset (pmReadout *readout);
 pmLumFunc        *psastroLuminosityFunction (psArray *stars, pmLumFunc *rawFunc);
Index: /trunk/psastro/src/psastroAnalysis.c
===================================================================
--- /trunk/psastro/src/psastroAnalysis.c	(revision 35714)
+++ /trunk/psastro/src/psastroAnalysis.c	(revision 35715)
@@ -86,5 +86,7 @@
     }
 
-    if (!psastroChooseRefstars (config, refs, "PSASTRO.INPUT")) {
+    bool skipastro = psMetadataLookupBool (&status, config->arguments, "PSASTRO.SKIP.ASTRO");
+
+    if (!psastroChooseRefstars (config, refs, "PSASTRO.INPUT", skipastro)) {
         psError (PSASTRO_ERR_UNKNOWN, false, "failed to select reference data for chips\n");
         psFree(refs);
@@ -108,5 +110,10 @@
         mosastro  = psMetadataLookupBool (&status, recipe, "PSASTRO.MOSAIC.MODE");
     }
-    if (!chipastro && !mosastro) {
+
+    if (skipastro) {
+        chipastro = false;
+        mosastro = false;
+        psLogMsg ("psastro", 3, "skip astrometry mode, accepting input astrometry\n");
+    } else if (!chipastro && !mosastro) {
         psLogMsg ("psastro", 3, "no astrometry mode selected, assuming chip astrometry\n");
         chipastro = true;
@@ -144,12 +151,14 @@
     }
 
-    if (!psastroZeroPoint (config)) {
-        psError(psErrorCodeLast(), false, "Failed to calculate zero point.");
-        return false;
-    }
+    if (!skipastro) {
+        if (!psastroZeroPoint (config)) {
+            psError(psErrorCodeLast(), false, "Failed to calculate zero point.");
+            return false;
+        }
 
-    if (!psastroAstromGuessCheck (config)) {
-        psError(psErrorCodeLast(), false, "Failed to check astrometry guess.");
-        return false;
+        if (!psastroAstromGuessCheck (config)) {
+            psError(psErrorCodeLast(), false, "Failed to check astrometry guess.");
+            return false;
+        }
     }
 
Index: /trunk/psastro/src/psastroArguments.c
===================================================================
--- /trunk/psastro/src/psastroArguments.c	(revision 35714)
+++ /trunk/psastro/src/psastroArguments.c	(revision 35715)
@@ -74,4 +74,5 @@
 
     // apply mosastro mode?
+    bool mosastro = false;
     if ((N = psArgumentGet (argc, argv, "-mosastro"))) {
         psArgumentRemove (N, &argc, argv);
@@ -81,7 +82,9 @@
         psArgumentRemove (N, &argc, argv);
         psMetadataAddBool (config->arguments, PS_LIST_TAIL, "PSASTRO.MOSAIC.MODE", PS_META_REPLACE, "", true);
+        mosastro = true;
     }
 
     // apply chipastro mode?
+    bool chipastro = false;
     if ((N = psArgumentGet (argc, argv, "-chipastro"))) {
         psArgumentRemove (N, &argc, argv);
@@ -91,4 +94,18 @@
         psArgumentRemove (N, &argc, argv);
         psMetadataAddBool (config->arguments, PS_LIST_TAIL, "PSASTRO.CHIP.MODE", PS_META_REPLACE, "", true);
+        chipastro = true;
+    }
+    if ((N = psArgumentGet (argc, argv, "-skipastro"))) {
+        psArgumentRemove (N, &argc, argv);
+        if (mosastro) {
+            psError(PSASTRO_ERR_ARGUMENTS, true, "cannot specify +mosastro with -skipastro");
+            psErrorStackPrint(stderr, "exit");
+            return false;
+        }
+        if (chipastro) {
+            psError(PSASTRO_ERR_ARGUMENTS, true, "cannot specify +chipastro with -skipastro");
+            return false;
+        }
+        psMetadataAddBool (config->arguments, PS_LIST_TAIL, "PSASTRO.SKIP.ASTRO", PS_META_REPLACE, "", true);
     }
 
Index: /trunk/psastro/src/psastroChooseRefstars.c
===================================================================
--- /trunk/psastro/src/psastroChooseRefstars.c	(revision 35714)
+++ /trunk/psastro/src/psastroChooseRefstars.c	(revision 35715)
@@ -20,5 +20,5 @@
 }
 
-bool psastroChooseRefstars (pmConfig *config, psArray *refs, const char *source) {
+bool psastroChooseRefstars (pmConfig *config, psArray *refs, const char *source, bool saveExistingMatchedRefs) {
 
     bool status;
@@ -55,5 +55,7 @@
 	// analysis.  however, we are re-doing the astrometry here, so remove
 	// that prior set of matched references
-	psMetadataRemoveKey (fpa->analysis, "MATCHED_REFS");
+        if (!saveExistingMatchedRefs) {
+            psMetadataRemoveKey (fpa->analysis, "MATCHED_REFS");
+        }
     }
 
Index: /trunk/psastro/src/psastroExtractAnalysis.c
===================================================================
--- /trunk/psastro/src/psastroExtractAnalysis.c	(revision 35714)
+++ /trunk/psastro/src/psastroExtractAnalysis.c	(revision 35715)
@@ -44,5 +44,5 @@
     }
 
-    if (!psastroChooseRefstars (config, refs, "PSASTRO.EXTRACT.ASTROM")) {
+    if (!psastroChooseRefstars (config, refs, "PSASTRO.EXTRACT.ASTROM", false)) {
         psError (PSASTRO_ERR_UNKNOWN, false, "failed to select reference data for chips\n");
         psFree(refs);
