Index: /branches/eam_branches/ipp-20101205/ppSub/src/ppSubLoop.c
===================================================================
--- /branches/eam_branches/ipp-20101205/ppSub/src/ppSubLoop.c	(revision 30105)
+++ /branches/eam_branches/ipp-20101205/ppSub/src/ppSubLoop.c	(revision 30106)
@@ -36,4 +36,6 @@
     psAssert(config, "Require configuration.");
 
+    bool success = true;
+
     pmConfigCamerasCull(config, NULL);
     pmConfigRecipesCull(config, "PPSUB,PPSTATS,PSPHOT,PSASTRO,MASKS,JPEG");
@@ -61,5 +63,6 @@
 	if (!ppSubInputDetections(&foundDetections, "PPSUB.POS1.SOURCES", "PPSUB.INPUT", data)) {
 	    psError(psErrorCodeLast(), false, "Unable to measure positive detections (1)");
-	    return false;
+	    success = false;
+            goto ESCAPE;
 	}
 	// if nothing was found, don't bother doing the forced photometry below
@@ -70,29 +73,52 @@
     }
     if (data->forcedPhot2) {
+        // Change the recipe to use a higher nsigma limit and quit after pass1
+        psMetadata *recipe = psMetadataLookupPtr (NULL, config->recipes, PSPHOT_RECIPE);
+
+        psF32 nsigma_peak_save = psMetadataLookupF32 (NULL, recipe, "PEAKS_NSIGMA_LIMIT");
+        char *breakPt_save =  psMetadataLookupStr (NULL, recipe, "BREAK_POINT");
+
+        bool mdok;
+        psF32 pos2_nsigma_peak = psMetadataLookupF32 (&mdok, recipe, "PEAKS_POS2_NSIGMA_LIMIT");
+        if (!mdok) {
+            psWarning("PEAKS_POS2_NSIGMA_LIMIT not found in recipe. Will use 25.\n");
+            pos2_nsigma_peak = 25.;
+        }
+        psMetadataAddF32(recipe, PS_LIST_TAIL, "PEAKS_NSIGMA_LIMIT", PS_META_REPLACE, "", pos2_nsigma_peak);
+        psMetadataAddStr(recipe, PS_LIST_TAIL, "BREAK_POINT", PS_META_REPLACE, "", "PASS1");
+
 	bool foundDetections = false;
 	if (!ppSubInputDetections(&foundDetections, "PPSUB.POS2.SOURCES", "PPSUB.REF", data)) {
 	    psError(psErrorCodeLast(), false, "Unable to measure positive detections (2)");
-	    return false;
-	}
+            psMetadataAddF32(recipe, PS_LIST_TAIL, "PEAKS_NSIGMA_LIMIT", PS_META_REPLACE, "", nsigma_peak_save);
+            psMetadataAddStr(recipe, PS_LIST_TAIL, "BREAK_POINT", PS_META_REPLACE, "", breakPt_save);
+	    success = false;
+            goto ESCAPE;
+	}
+        psMetadataAddF32(recipe, PS_LIST_TAIL, "PEAKS_NSIGMA_LIMIT", PS_META_REPLACE, "", nsigma_peak_save);
+        psMetadataAddStr(recipe, PS_LIST_TAIL, "BREAK_POINT", PS_META_REPLACE, "", breakPt_save);
 	// if nothing was found, don't bother doing the forced photometry below
 	if (!foundDetections) {
-	    psWarning ("no sources found in positive image 1, skipping forced photometry");
-	    data->forcedPhot1 = false;
-	}
-    }
-
-    // XXX if it exists, use the POS1, POS2 results for the FWHMs
+	    psWarning ("no sources found in positive image 2, skipping forced photometry");
+	    data->forcedPhot2 = false;
+	}
+    }
+
+    // XXX if it exists, use the POS1, POS2 successs for the FWHMs
     if (!ppSubMatchPSFs(data)) {
         psError(psErrorCodeLast(), false, "Unable to match PSFs.");
-        return false;
+        success = false;
+        goto ESCAPE;
     }
     if (data->quality) {
         // Can't do anything at all
-        return true;
+        success = false;
+        goto ESCAPE;
     }
     // generate the residual stamp grid for visualization
     if (!ppSubResidualSampleJpeg(config)) {
         psError(psErrorCodeLast(), false, "Unable to update.");
-        return false;
+        success = false;
+        goto ESCAPE;
     }
 
@@ -106,10 +132,12 @@
     if (!ppSubFilesIterateUp(config, PPSUB_FILES_INPUT)) {
         psError(PPSUB_ERR_IO, false, "Unable to close input files.");
-        return false;
+        success = false;
+        goto ESCAPE;
     }
 
     if (!ppSubLowThreshold(data)) {
         psError(psErrorCodeLast(), false, "Unable to threshold images.");
-        return false;
+        success = false;
+        goto ESCAPE;
     }
 
@@ -117,15 +145,18 @@
     if (!ppSubFilesIterateDown(config, PPSUB_FILES_SUB)) {
         psError(PPSUB_ERR_IO, false, "Unable to set up subtraction files.");
-        return false;
+        success = false;
+        goto ESCAPE;
     }
 
     if (!ppSubDefineOutput("PPSUB.OUTPUT", config)) {
         psError(psErrorCodeLast(), false, "Unable to define output.");
-        return false;
+        success = false;
+        goto ESCAPE;
     }
 
     if (!data->quality && !ppSubMakePSF(data)) {
         psError(psErrorCodeLast(), false, "Unable to generate PSF.");
-        return false;
+        success = false;
+        goto ESCAPE;
     }
 
@@ -142,5 +173,6 @@
     if (!ppSubReadoutSubtract(config)) {
         psError(psErrorCodeLast(), false, "Unable to subtract images.");
-        return false;
+        success = false;
+        goto ESCAPE;
     }
     // dumpout(config, "diff.1.fits");
@@ -149,5 +181,6 @@
     if (!ppSubFilesIterateUp(config, PPSUB_FILES_PSF | PPSUB_FILES_CONV)) {
         psError(PPSUB_ERR_IO, false, "Unable to close input files.");
-        return false;
+        success = false;
+        goto ESCAPE;
     }
     // dumpout(config, "diff.2a.fits");
@@ -156,5 +189,6 @@
     if (!ppSubBackground(config)) {
         psError(psErrorCodeLast(), false, "Unable to subtract background.");
-        return false;
+        success = false;
+        goto ESCAPE;
     }
     // dumpout(config, "diff.2b.fits");
@@ -163,5 +197,6 @@
     if (!ppSubVarianceRescale(config, data)) {
         psError(psErrorCodeLast(), false, "Unable to rescale variance.");
-        return false;
+        success = false;
+        goto ESCAPE;
     }
     // dumpout(config, "diff.2c.fits");
@@ -169,15 +204,18 @@
     if (data->quality) {
         // Done all we can do up to this point
-        return true;
+        success = false;
+        goto ESCAPE;
     }
 
     if (!ppSubFilesIterateDown(config, PPSUB_FILES_PHOT_SUB)) {
         psError(PPSUB_ERR_IO, false, "Unable to set up photometry files.");
-        return false;
+        success = false;
+        goto ESCAPE;
     }
 
     if (!data->quality && !ppSubReadoutPhotometry("PPSUB.OUTPUT", data)) {
         psError(psErrorCodeLast(), false, "Unable to perform photometry.");
-        return false;
+        success = false;
+        goto ESCAPE;
     }
     // dumpout(config, "diff.3.fits");
@@ -187,5 +225,6 @@
 	if (!ppSubReadoutForcedPhot("PPSUB.FORCED1.SOURCES", "PPSUB.OUTPUT", "PPSUB.POS1.SOURCES", data)) {
 	    psError(psErrorCodeLast(), false, "Unable to perform photometry.");
-	    return false;
+	    success = false;
+            goto ESCAPE;
 	}
     }
@@ -195,5 +234,6 @@
 	if (!ppSubReadoutForcedPhot("PPSUB.FORCED2.SOURCES", "PPSUB.OUTPUT", "PPSUB.POS2.SOURCES", data)) {
 	    psError(psErrorCodeLast(), false, "Unable to perform photometry.");
-	    return false;
+	    success = false;
+            goto ESCAPE;
 	}
     }
@@ -201,5 +241,6 @@
     if (!ppSubFilesIterateUp(config, PPSUB_FILES_PHOT_SUB)) {
         psError(PPSUB_ERR_IO, false, "Unable to set up photometry files.");
-        return false;
+        success = false;
+        goto ESCAPE;
     }
 
@@ -207,5 +248,6 @@
     if (!ppSubReadoutStats(data)) {
         psError(psErrorCodeLast(), false, "Unable to collect statistics");
-        return false;
+        success = false;
+        goto ESCAPE;
     }
     // Do Mask Stats
@@ -214,5 +256,6 @@
       if (!ppSubMaskStats(config, view,data->stats)) {
 	psError(psErrorCodeLast(), false, "Unable to generate mask statistics");
-	return(false);
+	success = false;
+        goto ESCAPE;
       }
     }
@@ -222,5 +265,6 @@
     if (!ppSubReadoutJpeg(config)) {
         psError(psErrorCodeLast(), false, "Unable to update.");
-        return false;
+        success = false;
+        goto ESCAPE;
     }
 
@@ -229,15 +273,18 @@
         if (!ppSubFilesIterateDown(config, PPSUB_FILES_INV)) {
             psError(PPSUB_ERR_IO, false, "Unable to set up inverse files.");
-            return false;
+            success = false;
+            goto ESCAPE;
         }
 
         if (data->inverse && !ppSubDefineOutput("PPSUB.INVERSE", config)) {
             psError(psErrorCodeLast(), false, "Unable to define inverse.");
-            return false;
+            success = false;
+            goto ESCAPE;
         }
 
         if (!ppSubReadoutInverse(config)) {
             psError(psErrorCodeLast(), false, "Unable to invert images.");
-            return false;
+            success = false;
+            goto ESCAPE;
         }
 
@@ -245,15 +292,18 @@
         if (!ppSubFilesIterateUp(config, PPSUB_FILES_SUB)) {
             psError(PPSUB_ERR_IO, false, "Unable to close subtraction files.");
-            return false;
+            success = false;
+            goto ESCAPE;
         }
 
         if (!ppSubFilesIterateDown(config, PPSUB_FILES_PHOT_INV)) {
             psError(PPSUB_ERR_IO, false, "Unable to set up inverse files.");
-            return false;
+            success = false;
+            goto ESCAPE;
         }
 
         if (!data->quality && !ppSubReadoutPhotometry("PPSUB.INVERSE", data)) {
             psError(psErrorCodeLast(), false, "Unable to perform photometry.");
-            return false;
+            success = false;
+            goto ESCAPE;
         }
 
@@ -261,5 +311,6 @@
         if (!ppSubFilesIterateUp(config, PPSUB_FILES_INV | PPSUB_FILES_PHOT_INV)) {
             psError(PPSUB_ERR_IO, false, "Unable to close subtraction files.");
-            return false;
+            success = false;
+            goto ESCAPE;
         }
     } else {
@@ -268,12 +319,14 @@
         if (!ppSubFilesIterateUp(config, PPSUB_FILES_SUB)) {
             psError(PPSUB_ERR_IO, false, "Unable to close subtraction files.");
-            return false;
-        }
-    }
-
+            success = false;
+            goto ESCAPE;
+        }
+    }
+
+ESCAPE:
     pmFPAfileDropInternal(config->files, "PSPHOT.BACKGND");
     pmFPAfileDropInternal(config->files, "PSPHOT.BACKMDL");
     pmFPAfileDropInternal(config->files, "PSPHOT.BACKMDL.STDEV");
 
-    return true;
+    return success;
 }
