Index: /branches/eam_branches/relphot.20210521/include/relphot.h
===================================================================
--- /branches/eam_branches/relphot.20210521/include/relphot.h	(revision 41606)
+++ /branches/eam_branches/relphot.20210521/include/relphot.h	(revision 41607)
@@ -500,12 +500,4 @@
 RelphotStages STAGES;
 
-# ifdef GRID_V1
-int setGridMeasure (off_t meas, int cat, double X, double Y);
-# endif
-
-# ifdef GRID_V2
-int setGridMeasure (off_t meas, int cat, double X, double Y, int ccdnum);
-# endif
-
 /*** relphot prototypes ***/
 void          ConfigInit          PROTO((int *argc, char **argv));
@@ -568,4 +560,5 @@
 void          global_stats        PROTO((Catalog *catalog, int Ncatalog, int nloop));
 void          initGrid            PROTO(());
+void          setMflatFromGrid    PROTO((Catalog *catalog));
 void          initGridBins        PROTO(());
 void          initImageBins       PROTO((Catalog *catalog, int Ncatalog, int doImageList));
Index: /branches/eam_branches/relphot.20210521/src/GridOps.c
===================================================================
--- /branches/eam_branches/relphot.20210521/src/GridOps.c	(revision 41606)
+++ /branches/eam_branches/relphot.20210521/src/GridOps.c	(revision 41607)
@@ -301,2 +301,12 @@
   return Mgrid;
 }
+
+// for historical reasons, Mflat and Mgrid have opposite signs
+void setMflatFromGrid (Catalog *catalog) {
+  if (!GRID_ZEROPT) return;
+  for (off_t j = 0; j < catalog->Nmeasure; j++) {
+    float Mgrid = getMgrid (&catalog->measure[j]);
+    catalog->measure[j].Mflat -= Mgrid;
+  }
+}
+
Index: /branches/eam_branches/relphot.20210521/src/StarOps.c
===================================================================
--- /branches/eam_branches/relphot.20210521/src/StarOps.c	(revision 41606)
+++ /branches/eam_branches/relphot.20210521/src/StarOps.c	(revision 41607)
@@ -291,9 +291,11 @@
 
 // setMrel and setMrelOutput are extremely similar, but have slightly different implications:
-// * setMrel uses the internal Tiny structures only
-// * setMrelOutput skips stars for which there are too few good measurements 
-// * setMrelOutput is meant to be called repeatedly, relaxing the criteria for 'good' on each pass
+// * setMrel only uses the internal Tiny structures
+// * setMrel skips stars for which there are too few good measurements (setMrelOutput assigns an average mag with only 1)
+// * setMrelOutput sets psfQF, psfQFperf, extNsigma, rank info, etc
+// * setMrelOutput sets average Map
 // * setMrelOutput updates 2MASS average flags
 // * setMrelOutput updates average EXT flags (PS1 and 2MASS)
+// * setMrelOutput sets average stack & warp photometry (if requested)
 
 int setMrel (Catalog *catalog, int Ncatalog) {
Index: /branches/eam_branches/relphot.20210521/src/reload_catalogs.c
===================================================================
--- /branches/eam_branches/relphot.20210521/src/reload_catalogs.c	(revision 41606)
+++ /branches/eam_branches/relphot.20210521/src/reload_catalogs.c	(revision 41607)
@@ -100,4 +100,6 @@
 
     initMrel (&catalog, 1);
+
+    setMflatFromGrid (&catalog); // Mgrid is used to set Mflat; Mgrid is the ignored in setMrelFinal/setMrelCatalog.c
     setMrelFinal (&catalog, FALSE);
     TIMESTAMP(time5);
Index: /branches/eam_branches/relphot.20210521/src/relphot_images.c
===================================================================
--- /branches/eam_branches/relphot.20210521/src/relphot_images.c	(revision 41606)
+++ /branches/eam_branches/relphot.20210521/src/relphot_images.c	(revision 41607)
@@ -89,5 +89,5 @@
       SetZptIteration (i);
 
-      setMrel  (catalog, Ncatalog); // threaded
+      setMrel  (catalog, Ncatalog); // threaded (calls setMrel_catalog_alt in setMrelCatalog.c)
 
       setMcal  (catalog);
@@ -160,5 +160,5 @@
      clients */
   if (!CALIBRATE_STACKS_AND_WARPS) {
-    reload_catalogs (skylist, 0, NULL);
+    reload_catalogs (skylist, 0, NULL); // calls setMrelFinal which setMrelOutput which calls setMrel_catalog_alt
     MARKTIME("-- updated all catalogs: %f sec\n", dtime);
   }
Index: /branches/eam_branches/relphot.20210521/src/setMrelCatalog.c
===================================================================
--- /branches/eam_branches/relphot.20210521/src/setMrelCatalog.c	(revision 41606)
+++ /branches/eam_branches/relphot.20210521/src/setMrelCatalog.c	(revision 41607)
@@ -233,5 +233,4 @@
       // data for which the associated image has not been loaded (probably because of
       // overlaps).  Msys + measure.Mcal is our best guess of the true magnitude
-      Mmos = Mgrid = 0;
       Mcal = measureT[k].McalPSF; // check that this is zero for loaded REF value
     } else {
@@ -243,8 +242,11 @@
       Mgrp  = getMgrp  (meas, cat, measureT[k].airmass, &dMgrp);
       if (isnan(Mgrp))  SKIP_THIS_MEAS(Ngrp);
-      Mgrid = getMgridTiny (&measureT[k]);
+      Mgrid = isSetMrelFinal ? 0.0 : getMgridTiny (&measureT[k]); 
       if (isnan(Mgrid)) SKIP_THIS_MEAS(Ngrid);
       Mflat = isnan (measureT[k].Mflat) ? 0.0 : measureT[k].Mflat;
     }
+    // on the final calculation of Mrel the value of Mgrid has been applied to Mflat, but
+    // it has not been removed from the GridOps.c structures.  In order to avoid double-counting,
+    // we need to skip Mgrid on the final calculation.
 
     int myUbercalDist = getUbercalDist(meas, cat);
@@ -562,5 +564,5 @@
   SecFilt *secfilt = &catalog[0].secfilt[ave*Nsecfilt];
 
-  float McalPSF = 0, McalAPER = 0, Mmos = 0, Mgrid = 0;
+  float McalPSF = 0, McalAPER = 0;
 
   // set the primary projection cell and skycell for this coordinate
@@ -726,12 +728,7 @@
       McalPSF  = measure[meas].McalPSF; // check that this is zero for loaded REF value
       McalAPER = McalPSF; // check that this is zero for loaded REF value
-      Mmos     = 0.0;
-      Mgrid    = 0.0;
     } else {
       McalPSF   = getMcal  (measSeq, cat, MAG_CLASS_PSF);
       McalAPER  = USE_MCAL_PSF_FOR_STACK_APER ? getMcal  (measSeq, cat, MAG_CLASS_PSF) : getMcal  (measSeq, cat, MAG_CLASS_KRON);
-      Mmos      = getMmos  (measSeq, cat);
-      Mgrid     = getMgrid (&measure[meas]);
-      // XXX can Mmos and Mgrid exist for stacks?
     }
     
@@ -769,6 +766,6 @@
     // get the zero point for the selected image
     // Use a different zero point for the PSF-like and APERTURE-like magnitudes
-    float zpPSF  = PhotZeroPoint (&measure[meas], &average[0], &secfilt[0]) - (McalPSF  + Mmos + Mgrid);
-    float zpAPER = PhotZeroPoint (&measure[meas], &average[0], &secfilt[0]) - (McalAPER + Mmos + Mgrid);
+    float zpPSF  = PhotZeroPoint (&measure[meas], &average[0], &secfilt[0]) - McalPSF;
+    float zpAPER = PhotZeroPoint (&measure[meas], &average[0], &secfilt[0]) - McalAPER;
 
     // zpFactor to go from instrumental flux to Janskies
Index: /branches/eam_branches/relphot.20210521/src/setMrelFinal.c
===================================================================
--- /branches/eam_branches/relphot.20210521/src/setMrelFinal.c	(revision 41606)
+++ /branches/eam_branches/relphot.20210521/src/setMrelFinal.c	(revision 41607)
@@ -101,8 +101,4 @@
   }
 
-  // XXX make this optional? (do not clean for -averages?)
-  // XXX : for the moment, disable clean_measures
-  if (!simpleAverage && FALSE) clean_measures (catalog, 1, TRUE);    /* mark outliers ID_MEAS_POOR_PHOTOM */
-
   ALLOCATE (catalog[0].measureRank, char, catalog[0].Nmeasure);
   setMeasureRank (catalog);
@@ -271,4 +267,6 @@
 }
 
+// XXX review this, especially the good/bad images
+// by this point, we have propagated the mosaic and tgroup flags to each image
 void setMeasureRank (Catalog *catalog) {
 
