Index: trunk/psastro/src/psastroZeroPoint.c
===================================================================
--- trunk/psastro/src/psastroZeroPoint.c	(revision 33643)
+++ trunk/psastro/src/psastroZeroPoint.c	(revision 37552)
@@ -20,4 +20,5 @@
     pmCell *cell = NULL;
     pmReadout *readout = NULL;
+    float refminMag,refmaxMag;
 
     // select the current recipe
@@ -59,6 +60,15 @@
     }
 
+    // MEH -- RefMagLimit for ZP with photcode option (merr probably better but not an option) 
+    // -- defualt is to use general config value (no hardcode..)
+    if (!psastroZeroPointRefMagLimitFromRecipe (&refminMag, &refmaxMag, fpa, recipe)) {
+        psLogMsg ("psastro", PS_LOG_INFO, "failed to load ref min,maxMag data from recipe");
+	return false;
+    }
+
     // if we measure the zero point by exposure, accumulate the dMag values here:
     psVector *dMag = NULL;
+    // MEH -- also the number of refcat sources used
+    int zpt_nref = 0;
 
     float fpaZP = 0.0;                  // Average zero point
@@ -81,10 +91,16 @@
 
                 // calculate dMag for the matched stars
-                dMag = psastroZeroPointReadoutAccum (dMag, readout, exptime);
-
+                //dMag = psastroZeroPointReadoutAccum (dMag, readout, exptime);
+                // MEH -- additional limits should be available for ZP if just robust mean and w/ stack skycal 
+		dMag = psastroZeroPointReadoutAccum (dMag, readout, exptime, refminMag, refmaxMag);
                 if (!byExposure) {
                     // calculate dMag for the matched stars just for this readout (well, chip)
                     psMetadata *header = psMetadataLookupMetadata (&status, readout->analysis, "PSASTRO.HEADER");
                     psastroZeroPointAnalysis (header, dMag, zeropt, recipe);
+		    // MEH -- per readout N refcat, min/max mag  -- added here, not part of median/edge process
+		    zpt_nref += dMag->n;
+		    psMetadataAddS32 (header, PS_LIST_TAIL, "ZPT_NREF", PS_META_REPLACE, "N refcat sources for zero point", dMag->n);
+		    psMetadataAddF32 (header, PS_LIST_TAIL, "ZPT_MIN", PS_META_REPLACE, "min refcat mag for zero point", refminMag);
+		    psMetadataAddF32 (header, PS_LIST_TAIL, "ZPT_MAX", PS_META_REPLACE, "max refcat mag for zero point", refmaxMag);
                     psFree (dMag);
                     dMag = NULL;
@@ -95,4 +111,5 @@
                         numZP++;
                     }
+
 
 
@@ -110,4 +127,6 @@
         }
         psastroZeroPointAnalysis (header, dMag, zeropt, recipe);
+	//MEH -- all N refcat sources here
+	zpt_nref = dMag->n;
         psFree (dMag);
         dMag = NULL;
@@ -141,9 +160,12 @@
                     // calculate dMag for the matched stars just for this readout (well, chip)
                     psMetadata *header = psMetadataLookupMetadata (&status, readout->analysis, "PSASTRO.HEADER");
-
                     psMetadataAddF32 (header, PS_LIST_TAIL, "ZPT_OBS", PS_META_REPLACE, "measured zero point",  zptObs);
                     psMetadataAddF32 (header, PS_LIST_TAIL, "ZPT_REF", PS_META_REPLACE, "reference zero point", zptRef);
                     psMetadataAddF32 (header, PS_LIST_TAIL, "ZPT_ERR", PS_META_REPLACE, "error on zero point",  zptErr);
                     psMetadataAddF32 (header, PS_LIST_TAIL, "ZPT_OFF", PS_META_REPLACE, "zero point offset",    zptOff);
+                    //MEH -- per readout  N refcat, MIN/MAX for consistency
+                    psMetadataAddS32 (header, PS_LIST_TAIL, "ZPT_NREF", PS_META_REPLACE, "N refcat sources for zero point", zpt_nref);
+		    psMetadataAddF32 (header, PS_LIST_TAIL, "ZPT_MIN", PS_META_REPLACE, "min refcat mag for zero point", refminMag);
+		    psMetadataAddF32 (header, PS_LIST_TAIL, "ZPT_MAX", PS_META_REPLACE, "max refcat mag for zero point", refmaxMag);
                 }
             }
@@ -151,4 +173,14 @@
     }
 
+    // MEH add N refcat, min/max mag to primary header 
+    psMetadata *header = psMetadataLookupMetadata (&status, fpa->analysis, "PSASTRO.HEADER");
+    if (!header) {
+	header = psMetadataAlloc ();
+	psMetadataAddMetadata (fpa->analysis, PS_LIST_TAIL, "PSASTRO.HEADER",  PS_META_REPLACE, "psastro header stats", header);
+	psFree (header);
+    }
+    psMetadataAddS32 (header, PS_LIST_TAIL, "ZPT_NREF", PS_META_REPLACE, "Total N refcat sources for zero point", zpt_nref);
+    psMetadataAddF32 (header, PS_LIST_TAIL, "ZPT_MIN", PS_META_REPLACE, "min refcat mag for zero point", refminMag);
+    psMetadataAddF32 (header, PS_LIST_TAIL, "ZPT_MAX", PS_META_REPLACE, "max refcat mag for zero point", refmaxMag);
 
     psMetadataItem *item = psMetadataLookup(fpa->concepts, "FPA.ZP");
@@ -162,5 +194,5 @@
  * accumulate the dMag values from this readout
  */
-psVector *psastroZeroPointReadoutAccum(psVector *dMag, pmReadout *readout, float exptime) {
+psVector *psastroZeroPointReadoutAccum(psVector *dMag, pmReadout *readout, float exptime, float refminMag, float refmaxMag) {
 
     bool status;
@@ -191,6 +223,9 @@
       // XXX I should be applying the airmass term here or in psastroLoadRefstars
       // (ie, ref->Mag would be a prediction for a given star.
-      float value = ref->Mag - raw->Mag - 2.5*log10(exptime);
-      psVectorAppend (dMag, value);
+      // MEH -- only append if in allowed ref mag range -- may want merr but not available..
+      if ( ref->Mag >= refminMag && ref->Mag <= refmaxMag ){
+        float value = ref->Mag - raw->Mag - 2.5*log10(exptime);
+        psVectorAppend (dMag, value);
+      }
     }
     return dMag;
@@ -490,5 +525,9 @@
                 continue;
             }
-        }
+	    //MEH null is a pain.. so only log if set
+            psLogMsg ("psastro", PS_LOG_INFO, "found GHOST_MAX_MAG %f",*ghostMaxMag);
+        }
+	//MEH what zpt is set to 
+        psLogMsg ("psastro", PS_LOG_INFO, "found ZEROPT  %f",*zeropt);
         psFree (iter);
         return true;
@@ -497,2 +536,75 @@
     return false;
 }
+
+
+
+bool psastroZeroPointRefMagLimitFromRecipe (float *refminMag, float *refmaxMag, pmFPA *fpa, psMetadata *recipe) {
+
+    bool status;
+    float tmpmin,tmpmax;
+
+    // MEH -- duplicate psastroZeroPointFromRecipe for ZeroPoint adding RefMag limits with photcode option for extra restrictions
+    // must have non-photcode dependent setting -- use as default rather than a hardcoded value 
+    *refminMag = psMetadataLookupF32 (&status, recipe, "REFSTAR.ZP.MIN.MAG");
+    //if (!status) *refminMag = -5.0;
+    if (!status) {
+        psLogMsg ("psastro", PS_LOG_INFO, "RefMagLimit is missing REFSTAR.ZP.MIN.MAG");
+        return false;
+    }
+    *refmaxMag = psMetadataLookupF32 (&status, recipe, "REFSTAR.ZP.MAX.MAG");
+    //if (!status) *refmaxMag = 30.0;
+    if (!status) {
+        psLogMsg ("psastro", PS_LOG_INFO, "RefMagLimit is missing REFSTAR.ZP.MAX.MAG");
+        return false;
+    }
+
+    // select the filter; default to any photcode and mag limit otherwise
+    char *filter = psMetadataLookupStr (&status, fpa->concepts, "FPA.FILTERID");
+    if (!status) ESCAPE ("RefMagLimit missing FPA.FILTER in concepts");
+
+    // need to select the PHOTCODE.DATA folder that matches our filter
+    psMetadataItem *item = psMetadataLookup (recipe, "PHOTCODE.DATA");
+    if (!item) ESCAPE ("RefMagLimit PHOTCODE.DATA folders missing");
+    if (item->type != PS_DATA_METADATA_MULTI) ESCAPE ("RefMagLimit PHOTCODE.DATA not a multi");
+
+    // PHOTCODE.DATA is a multi of metadata items
+    psListIterator *iter = psListIteratorAlloc(item->data.list, PS_LIST_HEAD, false);
+
+    psMetadataItem *refItem = NULL;
+    while ((refItem = psListGetAndIncrement (iter))) {
+        if (refItem->type != PS_DATA_METADATA) ESCAPE ("RefMagLimit PHOTCODE.DATA entry is not a metadata folder");
+
+        char *refFilter = psMetadataLookupStr (&status, refItem->data.md, "FILTER");
+        if (!status) {
+	    // always seems to happen once, commented out in ZeroPoint above as well
+            //psLogMsg ("psastro", PS_LOG_INFO, "RefMagLimit PHOTCODE.DATA recipe folder is missing FILTER");
+            continue;
+        }
+
+        // does this entry match the current filter?
+        if (strcmp (refFilter, filter)) continue;
+
+        psLogMsg ("psastro", PS_LOG_DETAIL, "RefMagLimit PHOTCODE.DATA found for filter %s", filter);
+
+        // -- may set one or other or both -- no reason to log if found or often not
+        tmpmin = psMetadataLookupF32 (&status, refItem->data.md, "REFSTAR.ZP.MIN.MAG");
+	if (status) {
+	    *refminMag = tmpmin;
+	} 
+        tmpmax = psMetadataLookupF32 (&status, refItem->data.md, "REFSTAR.ZP.MAX.MAG");
+	if (status) {
+	    *refmaxMag = tmpmax;
+	}
+        // once finds filter, should free and return true but multiple groups cause issue so just go through all groups now
+        //psFree (iter);
+        //return true;
+    }
+
+    // log out set values
+    psLogMsg ("psastro", PS_LOG_INFO, "using REFSTAR.ZP.MIN.MAG %f",*refminMag);
+    psLogMsg ("psastro", PS_LOG_INFO, "using REFSTAR.ZP.MAX.MAG %f",*refmaxMag);
+
+    psFree (iter);
+    return true;
+}
+
