Index: /branches/eam_branches/ipp-20130904/psphot/src/psphotChooseAnalysisOptions.c
===================================================================
--- /branches/eam_branches/ipp-20130904/psphot/src/psphotChooseAnalysisOptions.c	(revision 36331)
+++ /branches/eam_branches/ipp-20130904/psphot/src/psphotChooseAnalysisOptions.c	(revision 36331)
@@ -0,0 +1,170 @@
+# include "psphotInternal.h"
+
+// choose which sources will be processed for the petrosian radii and/or galaxy/trail model fits
+// a source for which we want galaxy models or petrosians gets 
+
+// RULES:
+// * select by Star/Galaxy?
+// * select by S/N?
+// * select by Flux?
+// * select by galactic latitude (global override)?
+// * de-select by density (global override)?
+
+// Fit all sources? 
+// * PSPHOT.EXT.FIT.ALL.SOURCES
+// * PSPHOT.EXT.FIT.ALL.THRESH (density limit)
+
+// galactic lat limit?
+// 
+
+// density limit?
+// magnitude limit?
+// S/N limit?
+
+// DIFF MODE : 
+// * 
+
+// for which sources do we want to run the extended source analysis (petrosian and/or galaxy fits)?
+bool psphotChooseAnalysisOptions (pmConfig *config, const pmFPAview *view, const char *filerule)
+{
+    bool status = true;
+
+    fprintf (stdout, "\n");
+    psLogMsg ("psphot", PS_LOG_INFO, "--- psphot Choose Analysis Options ---");
+
+    // select the appropriate recipe information
+    psMetadata *recipe  = psMetadataLookupPtr (&status, config->recipes, PSPHOT_RECIPE);
+    psAssert (recipe, "missing recipe?");
+
+    int num = psphotFileruleCount(config, filerule);
+
+    // skip the chisq image (optionally?)
+    int chisqNum = psMetadataLookupS32 (&status, config->arguments, "PSPHOT.CHISQ.NUM");
+    if (!status) chisqNum = -1;
+
+    // loop over the available readouts
+    for (int i = 0; i < num; i++) {
+        if (i == chisqNum) continue; // skip chisq image
+        if (!psphotSourceSizeReadout (config, view, filerule, i, recipe)) {
+	    psError (PSPHOT_ERR_CONFIG, false, "failed on source size analysis for %s entry %d", filerule, i);
+            return false;
+        }
+    }
+    return true;
+}
+
+// this function use an internal flag to mark sources which have already been measured
+bool psphotChooseAnalysisOptionsReadout(pmConfig *config, const pmFPAview *view, const char *filerule, int index, psMetadata *recipe)
+{
+    bool status;
+
+    psTimerStart ("psphot.options");
+
+    // find the currently selected readout
+    pmFPAfile *file = pmFPAfileSelectSingle(config->files, filerule, index); // File of interest
+    psAssert (file, "missing file?");
+
+    pmReadout *readout = pmFPAviewThisReadout(view, file->fpa);
+    psAssert (readout, "missing readout?");
+
+    pmDetections *detections = psMetadataLookupPtr (&status, readout->analysis, "PSPHOT.DETECTIONS");
+    psAssert (detections, "missing detections?");
+
+    psArray *sources = detections->allSources;
+    psAssert (sources, "missing sources?");
+
+    if (!sources->n) {
+        psLogMsg ("psphot", PS_LOG_INFO, "no sources, skipping source size");
+        return true;
+    }
+
+    // Option to enable fitting of all objects with extended model.
+    options.extFitAll = psMetadataLookupBool(&status, recipe, "PSPHOT.EXT.FIT.ALL.SOURCES");
+    assert (status);
+
+    // Fitting everything is fine, but if the source density is high, we probably shouldn't.
+    options.extFitAllThresh = psMetadataLookupF32(&status, recipe, "PSPHOT.EXT.FIT.ALL.THRESH");
+    assert (status);
+    
+    // Determine if this readout is above the threshold to ext fit all sources
+    options->extFitAllReadout = false;
+    if (options->extFitAll) {
+      float maskFrac = psMetadataLookupF32(&status,readout->analysis,"READOUT.MASK.FRAC");
+      if (status) {
+	maskFrac = 0.0;
+      }
+      if (sources->n * (1.0 - maskFrac) > options->extFitAllThresh) {
+	options->extFitAllReadout = false;
+      }
+      else {
+	options->extFitAllReadout = true;
+      }
+    }
+    
+    for (psS32 i = 0 ; i < sources->n ; i++) {
+
+        pmSource *source = (pmSource *) sources->data[i];
+
+	// skip saturated stars modeled with a radial profile 
+        if (source->mode2 & PM_SOURCE_MODE2_SATSTAR_PROFILE) continue;
+
+	// If the recipe requests we do extended source fits to everything, set
+	// the EXT_LIMIT flag
+	if (options->extFitAllReadout) {
+	  psTrace("psphotSourceClassRegion.EXTALT",10,"In extFitAll: %d %d\n",options->extFitAll,options->extFitAllReadout);
+	  source->mode |= PM_SOURCE_MODE_EXT_LIMIT;
+	}
+
+        // saturated star (too many saturated pixels or peak above saturation limit).  These
+        // may also be saturated galaxies, or just large saturated regions.
+        if (source->mode & PM_SOURCE_MODE_SATSTAR) {
+            source->tmpFlags |= PM_SOURCE_TMPF_SIZE_MEASURED;
+            continue;
+        }
+
+        // any sources missing a large fraction should just be treated as PSFs
+        if ((source->pixWeightNotBad < 0.9) || (source->pixWeightNotPoor < 0.9)) {
+	    source->tmpFlags |= PM_SOURCE_TMPF_SIZE_MEASURED;
+	    continue;
+        }
+
+        bool isCR = (source->moments->SN > 7.0) && (Mminor < options->sizeLimitCR);
+        if (isCR) {
+            psTrace("psphotSourceClassRegion.CR",4,"CLASS: %g %g\t%g %g\t%g %g\t%g %g\t%g CR\n",
+                    source->peak->xf, source->peak->yf, Mminor, kMag, dMag, nSigmaMAG, options->sizeLimitCR, options->magLimitCR, options->nSigmaApResid);
+            source->mode |= PM_SOURCE_MODE_DEFECT;
+            source->tmpFlags |= PM_SOURCE_TMPF_SIZE_CR_CANDIDATE;
+	    source->tmpFlags |= PM_SOURCE_TMPF_SIZE_MEASURED;
+            Ncr ++;
+            continue;
+        }
+
+	source->mode |= PM_SOURCE_MODE_EXT_LIMIT;
+
+	// Alternate extended source limit calculation
+	if (options->altDiffExt) {
+	  // ratio of major to minor axes
+	  // MRV = Major / Minor
+	  // MRV = (0.5 * (Mxx + Myy) + 0.5 * sqrt( (Mxx + Myy)^2 + 4 Mxy^2)) /
+	  //       (0.5 * (Mxx + Myy) - 0.5 * sqrt( (Mxx + Myy)^2 + 4 Mxy^2))
+	  // MRV = (2 * 0.5 * (Mxx + Myy) - Minor) / Minor
+	  float momentRatioVeres = (Mxx + Myy - Mminor) / Mminor;
+	  bool  isAltEXT = (momentRatioVeres > options->altDiffExtThresh);
+	  if (isAltEXT) {
+	    psTrace("psphotSourceClassRegion.EXT",4,"CLASS: %g %g\t%g %g\t%g %g\t%g %g\t%g ALTEXT\t%g %g\n",
+                    source->peak->xf, source->peak->yf, Mminor, kMag, dMag, nSigmaMAG, options->sizeLimitCR, options->magLimitCR, options->nSigmaApResid,
+		    momentRatioVeres,options->altDiffExtThresh);
+            source->mode |= PM_SOURCE_MODE_EXT_LIMIT;
+            source->tmpFlags |= PM_SOURCE_TMPF_SIZE_MEASURED;
+            Next ++;
+            continue;
+	  }
+	}
+	
+        // Everything else should just be treated as a PSF
+    }
+
+    psLogMsg ("psphot.options", PS_LOG_WARN, "choose analysis options for %ld sources: %f sec\n", sources->n, psTimerMark ("psphot.options"));
+
+    return true;
+}
