Index: /branches/eam_branches/relphot.20210521/src/GridOps.c
===================================================================
--- /branches/eam_branches/relphot.20210521/src/GridOps.c	(revision 41607)
+++ /branches/eam_branches/relphot.20210521/src/GridOps.c	(revision 41608)
@@ -188,4 +188,7 @@
 	if (isnan(Mmos)) continue;
 
+	float Mflat = getMflat (nm, nc, catalog);
+	if (isnan(Mflat)) continue;
+
 	// Mrel* is the average magnitude for this star.  For PS1 stacks, we have too much
 	// PSF variability.  We need to calibrate the PSF magnitudes separately from the
@@ -200,6 +203,5 @@
 	if (isnan(MsysPSF)) continue;
 
-	// what about Mflat?
-	float Moff =  Mcal + Mgrp + Mmos;
+	float Moff =  Mcal + Mgrp + Mmos + Mflat;
 
 	// Msys = Mrel + Moff + Mgrid
@@ -307,6 +309,6 @@
   for (off_t j = 0; j < catalog->Nmeasure; j++) {
     float Mgrid = getMgrid (&catalog->measure[j]);
-    catalog->measure[j].Mflat -= Mgrid;
-  }
-}
-
+    catalog->measure[j].Mflat += Mgrid;
+  }
+}
+
Index: /branches/eam_branches/relphot.20210521/src/ImageOps.c
===================================================================
--- /branches/eam_branches/relphot.20210521/src/ImageOps.c	(revision 41607)
+++ /branches/eam_branches/relphot.20210521/src/ImageOps.c	(revision 41608)
@@ -636,5 +636,6 @@
       // apply that offset as well here for this image (in other words, each detection is
       // being compared to the model, excluding the zero point, Mcal.  The model includes
-      // the flat-correction.  NOTE the sign of Mflat (Image.Mcal = Measure.Mcal - Mflat)
+      // the flat-correction.  NOTE the sign of Mflat (Image.Mcal = Measure.Mcal + Mflat)
+      // this was inconsistent w.r.t. PhotRel pre-r41606
 
       float Mflat = getMflat (m, c, catalog);
@@ -647,5 +648,5 @@
       }
 
-      float Moff = Mmos + Mgrp + Mgrid - Mflat;
+      float Moff = Mmos + Mgrp + Mgrid + Mflat;
 
       PhotCode *code = GetPhotcodebyCode (catalog[c].measureT[m].photcode);
@@ -830,5 +831,5 @@
       // apply that offset as well here for this image (in other words, each detection is
       // being compared to the model, excluding the zero point, Mcal.  The model includes
-      // the flat-correction.  NOTE the sign of Mflat (Image.Mcal = Measure.Mcal - Mflat)
+      // the flat-correction.  NOTE the sign of Mflat (Image.Mcal = Measure.Mcal + Mflat)
 
       float Mflat = getMflat (m, c, catalog);
@@ -842,5 +843,5 @@
       }
 
-      float Moff = Mmos + Mgrp + Mgrid - Mflat;
+      float Moff = Mmos + Mgrp + Mgrid + Mflat;
 
       psfStars.alldata-> yVector[Nref] = MsysPSF - MrelPSF - Moff;
Index: /branches/eam_branches/relphot.20210521/src/MagResidSave.c
===================================================================
--- /branches/eam_branches/relphot.20210521/src/MagResidSave.c	(revision 41607)
+++ /branches/eam_branches/relphot.20210521/src/MagResidSave.c	(revision 41608)
@@ -233,5 +233,5 @@
     // apply that offset as well here for this image (in other words, each detection is
     // being compared to the model, excluding the zero point, Mcal.  The model includes
-    // the flat-correction.  NOTE the sign of Mflat (Image.Mcal = Measure.Mcal - Mflat)
+    // the flat-correction.  NOTE the sign of Mflat (Image.Mcal = Measure.Mcal + Mflat)
 
     float Mflat = getMflat (m, c, catalog);
@@ -248,5 +248,5 @@
     if (isnan(MsysPSF)) continue;
 
-    float Moff = Mcal + Mmos + Mgrp + Mgrid - Mflat;
+    float Moff = Mcal + Mmos + Mgrp + Mgrid + Mflat;
 
     // Mrel is the true average apparent magnitude of this star
Index: /branches/eam_branches/relphot.20210521/src/MosaicOps.c
===================================================================
--- /branches/eam_branches/relphot.20210521/src/MosaicOps.c	(revision 41607)
+++ /branches/eam_branches/relphot.20210521/src/MosaicOps.c	(revision 41608)
@@ -1069,5 +1069,6 @@
     // apply that offset as well here for this image (in other words, each detection is
     // being compared to the model, excluding the zero point, Mcal.  The model includes
-    // the flat-correction.  NOTE the sign of Mflat (Image.Mcal = Measure.Mcal - Mflat)
+    // the flat-correction.  NOTE the sign of Mflat (Image.Mcal = Measure.Mcal + Mflat).
+    // this was inconsistent pre r41606
 
     float Mflat = getMflat (m, c, catalog);
@@ -1094,5 +1095,5 @@
     assert (Nbright >= 0);
 
-    float Moff =  Mcal + Mgrp + Mgrid - Mflat;
+    float Moff =  Mcal + Mgrp + Mgrid + Mflat;
 
     psfStars->alldata-> yVector[N] = MsysPSF - MrelPSF - Moff;
@@ -1205,5 +1206,5 @@
     float MsysPSF = PhotSysTiny (&catalog[c].measureT[m], &catalog[c].averageT[n], &catalog[c].secfilt[n*Nsecfilt], MAG_CLASS_PSF);
     
-    float delta = MsysPSF - MrelPSF - Mcal - Mgrid + Mflat;
+    float delta = MsysPSF - MrelPSF - Mcal - Mgrid - Mflat;
 
     int isBad = (catalog[c].measureT[m].dbFlags & MEAS_BAD);
Index: /branches/eam_branches/relphot.20210521/src/TGroupOps.c
===================================================================
--- /branches/eam_branches/relphot.20210521/src/TGroupOps.c	(revision 41607)
+++ /branches/eam_branches/relphot.20210521/src/TGroupOps.c	(revision 41608)
@@ -874,5 +874,6 @@
     // apply that offset as well here for this image (in other words, each detection is
     // being compared to the model, excluding the zero point, Mcal.  The model includes
-    // the flat-correction.  NOTE the sign of Mflat (Image.Mcal = Measure.Mcal - Mflat)
+    // the flat-correction.  NOTE the sign of Mflat (Image.Mcal = Measure.Mcal + Mflat)
+    // this was inconsistent with PhotRel pre-r41606
 
     float Mflat = getMflat (m, c, catalog);
@@ -899,5 +900,5 @@
     assert (Nbright >= 0);
 
-    float Moff =  Mcal + Mmos + Mgrid - Mflat;
+    float Moff =  Mcal + Mmos + Mgrid + Mflat;
 
     psfStars->alldata-> yVector[N] = MsysPSF - MrelPSF - Moff;
@@ -1022,5 +1023,5 @@
 
     // for historical reasons, Mflat is defined with the wrong sign
-    float delta = MsysPSF - MrelPSF - Mcal - Mmos - Mgrid + Mflat - myTGroup->McalPSF;
+    float delta = MsysPSF - MrelPSF - Mcal - Mmos - Mgrid - Mflat - myTGroup->McalPSF;
 
     int isBad = (catalog[c].measureT[m].dbFlags & MEAS_BAD);
@@ -1503,5 +1504,5 @@
 	float MrelPSF  = getMrel  (catalog, m, c, MAG_CLASS_PSF, MAG_SRC_CHP); // average magnitude
 	// float Mgrid    = getMgridTiny (&catalog[c].measureT[m]); // camera offset (deprecated?)
-	// float Mflat    = getMflat (m, c, catalog); // flat-field correction
+	float Mflat    = getMflat (m, c, catalog); // flat-field correction
 
 	off_t n = catalog[c].measureT[m].averef;
@@ -1511,5 +1512,5 @@
 
 	// for historical reasons, Mflat is defined with the wrong sign
-	float delta = MsysPSF - MrelPSF - Mcal - Mmos - Mgrp;
+	float delta = MsysPSF - MrelPSF - Mcal - Mmos - Mgrp - Mflat;
 
 	fprintf (fout, "%f %f : %f %f : %f %f %f  : %f %f\n", catalog[c].averageT[n].R, catalog[c].averageT[n].D, MsysPSF, MrelPSF, Mcal, Mmos, Mgrp, catalog[c].measureT[m].airmass, delta);
Index: /branches/eam_branches/relphot.20210521/src/setMrelCatalog.c
===================================================================
--- /branches/eam_branches/relphot.20210521/src/setMrelCatalog.c	(revision 41607)
+++ /branches/eam_branches/relphot.20210521/src/setMrelCatalog.c	(revision 41608)
@@ -271,6 +271,6 @@
     float dMap = NAN;
 
-    // XXX I am not convinced the sign on Mflat here is correct
-    float Moff =  Mcal + Mmos + Mgrp + Mgrid - Mflat;
+    // This definition is consistent with PhotRel: Mrel = Msys - Mcal - Mflat
+    float Moff =  Mcal + Mmos + Mgrp + Mgrid + Mflat;
 
     if (isSetMrelFinal) {
Index: /branches/eam_branches/relphot.20210521/src/setMrelFinal.c
===================================================================
--- /branches/eam_branches/relphot.20210521/src/setMrelFinal.c	(revision 41607)
+++ /branches/eam_branches/relphot.20210521/src/setMrelFinal.c	(revision 41608)
@@ -103,5 +103,5 @@
   ALLOCATE (catalog[0].measureRank, char, catalog[0].Nmeasure);
   setMeasureRank (catalog);
-  setMrelOutput (catalog, 1); // sets the values secfilt.MpsfChp = <measure.M + measure.Mflat - image.Mcal>
+  setMrelOutput (catalog, 1); // sets the values secfilt.MpsfChp = <measure.M - image.Mcal - measure.Mflat>
   setMcalOutput (catalog, 1); // sets measure.Mcal = image.Mcal
 
