Index: /branches/eam_branches/relphot.20210521/Makefile
===================================================================
--- /branches/eam_branches/relphot.20210521/Makefile	(revision 41619)
+++ /branches/eam_branches/relphot.20210521/Makefile	(revision 41620)
@@ -31,4 +31,5 @@
 $(SRC)/GridOps.$(ARCH).o	 \
 $(SRC)/GridIO.$(ARCH).o	 \
+$(SRC)/ResetOps.$(ARCH).o	 \
 $(SRC)/ImageOps.$(ARCH).o	 \
 $(SRC)/ImageSubset.$(ARCH).o	 \
@@ -91,4 +92,5 @@
 $(SRC)/GridOps.$(ARCH).o	 \
 $(SRC)/GridIO.$(ARCH).o	 \
+$(SRC)/ResetOps.$(ARCH).o	 \
 $(SRC)/ImageOps.$(ARCH).o	 \
 $(SRC)/ImageSubset.$(ARCH).o	 \
Index: /branches/eam_branches/relphot.20210521/include/relphot.h
===================================================================
--- /branches/eam_branches/relphot.20210521/include/relphot.h	(revision 41619)
+++ /branches/eam_branches/relphot.20210521/include/relphot.h	(revision 41620)
@@ -442,4 +442,5 @@
 int    RESET;
 int    RESET_ZEROPTS;
+int    RESET_FLATCORR;
 int    REPAIR_WARPS;
 int    PRESERVE_PS1;
@@ -578,7 +579,7 @@
 ImageSubset  *getimages_subset    PROTO((off_t *N));
 void          global_stats        PROTO((Catalog *catalog, int Ncatalog, int nloop));
-void          initGrid            PROTO(());
+void          initGrid            PROTO((void));
 void          setMflatFromGrid    PROTO((Catalog *catalog));
-void          initGridBins        PROTO(());
+void          initGridBins        PROTO((void));
 GridCorrectionType *getGridCorrByCode PROTO((int code));
 GridCorrectionType *getGridCorrNext   PROTO((int *Nlast));
@@ -749,5 +750,5 @@
 StatDataSet *StatDataSetAlloc (int Nsecfilt, int Nmax);
 
-int init_synthetic_mags ();
+int init_synthetic_mags (void);
 int add_synthetic_mags (AverageTiny *average, SecFilt *secfilt, MeasureTiny *measure, off_t *Nmeasure, off_t *Nm);
 
@@ -810,5 +811,5 @@
 
 int SynthZeroPointsLoad (char *filename);
-SynthZeroPoints *SynthZeroPointsGet ();
+SynthZeroPoints *SynthZeroPointsGet (void);
 
 int relphot_synthphot (SkyList *sky, int hostID, char *hostpath);
@@ -829,5 +830,5 @@
 
 void put_astrom_table (AstromOffsetTable *myTable);
-void free_astrom_table ();
+void free_astrom_table (void);
 
 uint64_t CreatePSPSObjectID(double ra, double dec);
@@ -852,2 +853,10 @@
 
 int MagResidSave(char *filename, Catalog *catalog);
+
+void ResetAverageAndMeasure (Catalog *catalog);
+void ResetAverageObjects (Catalog *catalog);
+void ResetImages (Image *subset, off_t Nsubset);
+void ResetMeasureZeroPoints (MeasureTiny *measure, off_t Nmeasure, off_t Ncat);
+void ResetAverageActivePhotcodes (SecFilt *secfilt);
+
+
Index: /branches/eam_branches/relphot.20210521/src/GridOps.c
===================================================================
--- /branches/eam_branches/relphot.20210521/src/GridOps.c	(revision 41619)
+++ /branches/eam_branches/relphot.20210521/src/GridOps.c	(revision 41620)
@@ -29,5 +29,5 @@
 # define NY_BIN 2
 
-void initGridBins () {
+void initGridBins (void) {
 
   // allocate the full possible range of GridCorrectionType pointers, Nphotcode
@@ -116,5 +116,5 @@
 /* for GPC1, we have 60 chips, 5 filters, 16x16 grid cells = 2MB of memory for this stuff
    if we go to 64x64 grid cells (~75 pixels), then it is still only 29MB */
-void initGrid () {
+void initGrid (void) {
 
   if (!GRID_ZEROPT) return;
Index: /branches/eam_branches/relphot.20210521/src/ResetOps.c
===================================================================
--- /branches/eam_branches/relphot.20210521/src/ResetOps.c	(revision 41620)
+++ /branches/eam_branches/relphot.20210521/src/ResetOps.c	(revision 41620)
@@ -0,0 +1,178 @@
+# include "relphot.h"
+
+static DVOAverageFlags photomBitsAverage = 
+  ID_OBJ_EXT             | // extended in our data (eg, PS)
+  ID_OBJ_EXT_ALT         | // extended in external data (eg, 2MASS)
+  ID_OBJ_GOOD            | // good-quality measurement in our data (eg,PS)
+  ID_OBJ_GOOD_ALT        | // good-quality measurement in  external data (eg, 2MASS)
+  ID_OBJ_GOOD_STACK      | // good-quality object in the stack (> 1 good stack)
+  ID_OBJ_BEST_STACK      | // the primary stack measurement are the best measurements
+  ID_OBJ_SUSPECT_STACK   | // suspect object in the stack (> 1 good or suspect stack, < 2 good)
+  ID_OBJ_BAD_STACK;        // good-quality object in the stack (> 1 good stack)
+
+// flags used by the photometry analysis (excluding UBERCAL)
+static DVOMeasureFlags photomBitsMeasure = 
+  ID_MEAS_NOCAL          | // detection ignored for this analysis (photcode, time range)
+  ID_MEAS_POOR_PHOTOM    | // detection is photometry outlier 
+  ID_MEAS_SKIP_PHOTOM    | // detection was ignored for photometry measurement 
+  ID_MEAS_AREA           | // detetion was outside acceptable area of device
+  ID_MEAS_SYNTH_MAG      | // magnitude is synthetic
+  ID_MEAS_STACK_PRIMARY  | // this stack measurement is in the primary skycell
+  ID_MEAS_STACK_PHOT_SRC | // this measurement supplied the stack photometry
+  ID_MEAS_PHOTOM_PSF     | // this measurement is used for the mean psf mag
+  ID_MEAS_PHOTOM_APER    | // this measurement is used for the mean ap mag
+  ID_MEAS_PHOTOM_KRON    ; // this measurement is used for the mean kron mag
+
+// ID_MEAS_PHOTOM_UBERCAL -- externally-supplied zero point from ubercal analysis
+// this is set by 'setphot', do not reset here
+
+// flags used by the photometry analysis (excluding UBERCAL)
+// unsigned int secfiltFlags = 
+//   ID_PHOTOM_PASS_0 | // average measured at pass 0
+//   ID_PHOTOM_PASS_1 | // average measured at pass 1
+//   ID_PHOTOM_PASS_2 | // average measured at pass 2
+//   ID_PHOTOM_PASS_3 | // average measured at pass 3
+//   ID_PHOTOM_PASS_4 | // average measured at pass 3
+//   ID_SECF_USE_SYNTH | // average measured at pass 3
+//   ID_SECF_USE_UBERCAL | // average measured at pass 3
+//   ID_SECF_OBJ_EXT; // average measured at pass 3
+    
+
+// Used in bcatalog.c
+void ResetAverageActivePhotcodes (SecFilt *secfilt) {
+
+  if (!RESET) return;
+
+  int Ns;
+
+  // only loop over the active photcodes
+  for (Ns = 0; Ns < Nphotcodes; Ns++) {
+
+    int thisCode = photcodes[Ns][0].code;
+    int Nsec = GetPhotcodeNsec(thisCode);
+
+    dvo_secfilt_init (&secfilt[Nsec], SECFILT_RESET_ALL);
+  }
+}
+
+// Used in bcatalog.c
+void ResetMeasureZeroPoints (MeasureTiny *measure, off_t Nmeasure, off_t Ncat) {
+
+  if (!RESET) return;
+
+  // only reset Mcal for measures with a matching image
+  // do not reset Mcal for ubercal images unless explicitly requested
+  if (measure->dbFlags & ID_MEAS_PHOTOM_UBERCAL) {
+    if (!KEEP_UBERCAL) {
+      measure->McalPSF  = 0.0;
+      measure->McalAPER = 0.0;
+      measure->dbFlags &= ~ID_MEAS_PHOTOM_UBERCAL;
+    } 
+  } else {
+    if (getImageEntry (Nmeasure, Ncat) >= 0) {
+      measure->McalPSF  = 0.0;
+      measure->McalAPER = 0.0;
+    }
+  }
+  measure->dbFlags &= ~photomBitsMeasure;
+}
+
+// Used in load_images.c:
+// XXX NOTE : This resets all images regardless of active photcode
+void ResetImages (Image *subset, off_t Nsubset) {
+
+  // reset image values as needed.  always allow 'few' images to succeed, if possible (new
+  // images / detections may have been added
+
+  for (off_t i = 0; i < Nsubset; i++) {
+    // reset these bits regardless (we will re-determine)
+    subset[i].flags &= ~(ID_IMAGE_PHOTOM_NOCAL | ID_IMAGE_PHOTOM_POOR | ID_IMAGE_PHOTOM_SKIP | ID_IMAGE_PHOTOM_FEW);
+    if (RESET) {
+      if (RESET_ZEROPTS) {
+	if (!KEEP_UBERCAL || !(subset[i].flags & ID_IMAGE_PHOTOM_UBERCAL)) {
+	  subset[i].McalPSF  = 0.0;
+	  subset[i].McalAPER = 0.0;
+	  subset[i].dMcal    = NAN;
+	  subset[i].flags   &= ~ID_IMAGE_PHOTOM_UBERCAL;
+	}
+      }
+      subset[i].flags &= ~(ID_IMAGE_MOSAIC_POOR | ID_IMAGE_NIGHT_POOR);
+      subset[i].flags &= ~(ID_IMAGE_IMAGE_PHOTCAL | ID_IMAGE_MOSAIC_PHOTCAL | ID_IMAGE_TGROUP_PHOTCAL);
+      subset[i].ubercalDist = 1000;
+    }
+  }
+}
+
+// Used in relphot_objects.c:
+void ResetAverageObjects (Catalog *catalog) {
+
+  // reset the calculated average magnitudes (does not affect ubercal-tied measurements or images)
+  if (!RESET) return;
+
+  int Nsecfilt = catalog->Nsecfilt;
+
+  for (off_t j = 0; j < catalog->Naverage; j++) {
+    catalog->average[j].flags    &= ~photomBitsAverage; // reset all photometry bits (but not astrom)
+    catalog->average[j].psfQF     = NAN;     // reset (will be re-calculated here)
+    catalog->average[j].psfQFperf = NAN;	// reset (will be re-calculated here)
+    catalog->average[j].stargal   = NAN;	// reset (will be re-calculated here)
+    catalog->average[j].photFlagsUpper = 0;	// reset (will be re-calculated here)
+    catalog->average[j].photFlagsLower = 0;	// reset (will be re-calculated here)
+    catalog->average[j].NwarpOK        = 0;	// reset (will be re-calculated here)
+    for (off_t k = 0; k < Nsecfilt; k++) {
+      dvo_secfilt_init (&catalog->secfilt[j*Nsecfilt + k], SECFILT_RESET_ALL);
+    }
+  }
+}
+
+// Used in setMrelFinal.c
+void ResetAverageAndMeasure (Catalog *catalog) {
+
+  /*** RESET photometry flags and Mcal values ***/
+  if (!RESET) return;
+
+  int Nsecfilt = GetPhotcodeNsecfilt ();
+
+  // XXX I should really deprecate the concept of applying the average 
+  // calculation to a limited set of photcodes.  
+  // for now, just do all photcodes here (
+  // as it stands, only stacks are limited by photcode; mean exp and forced warp 
+  // are applied to all Nsecfilt
+
+  for (off_t i = 0; i < catalog->Naverage; i++) {
+    for (int Ns = 0; Ns < Nsecfilt; Ns++) {
+	
+      off_t N = Nsecfilt*i+Ns;
+      dvo_secfilt_init (&catalog->secfilt[N], SECFILT_RESET_ALL);
+
+      off_t m = catalog->average[i].measureOffset;
+
+      for (off_t j = 0; j < catalog->average[i].Nmeasure; j++, m++) {
+	
+	/* select measurements by time */
+	if (TimeSelect) {
+	  if (catalog->measure[m].t < TSTART) continue;
+	  if (catalog->measure[m].t > TSTOP) continue;
+	}
+	
+	// only reset Mcal for measures with a matching image
+	// do not reset Mcal for ubercal images unless explicitly requested
+
+	if (catalog->measure[m].dbFlags & ID_MEAS_PHOTOM_UBERCAL) {
+	  if (!KEEP_UBERCAL) {
+	    catalog->measure[m].McalPSF  = 0.0;
+	    catalog->measure[m].McalAPER = 0.0;
+	    catalog->measure[m].dbFlags &= ~ID_MEAS_PHOTOM_UBERCAL;
+	  } 
+	} else {
+	  if (RESET_ZEROPTS && (getImageEntry (m, 0) >= 0)) {
+	    catalog->measure[m].McalPSF  = 0.0;
+	    catalog->measure[m].McalAPER = 0.0;
+	  }
+	}
+	catalog->measure[m].dbFlags &= ~photomBitsMeasure;
+      }
+    }
+  }
+}
+
Index: /branches/eam_branches/relphot.20210521/src/args.c
===================================================================
--- /branches/eam_branches/relphot.20210521/src/args.c	(revision 41619)
+++ /branches/eam_branches/relphot.20210521/src/args.c	(revision 41620)
@@ -220,9 +220,13 @@
     RESET = TRUE;
   }
-
   RESET_ZEROPTS = FALSE;
   if ((N = get_argument (argc, argv, "-reset-zpts"))) {
     remove_argument (N, &argc, argv);
     RESET_ZEROPTS = TRUE;
+  }
+  RESET_FLATCORR = FALSE;
+  if ((N = get_argument (argc, argv, "-reset-flat"))) {
+    remove_argument (N, &argc, argv);
+    RESET_FLATCORR = TRUE;
   }
 
@@ -759,9 +763,13 @@
     RESET = TRUE;
   }
-
   RESET_ZEROPTS = FALSE;
   if ((N = get_argument (argc, argv, "-reset-zpts"))) {
     remove_argument (N, &argc, argv);
     RESET_ZEROPTS = TRUE;
+  }
+  RESET_FLATCORR = FALSE;
+  if ((N = get_argument (argc, argv, "-reset-flat"))) {
+    remove_argument (N, &argc, argv);
+    RESET_FLATCORR = TRUE;
   }
 
Index: /branches/eam_branches/relphot.20210521/src/bcatalog.c
===================================================================
--- /branches/eam_branches/relphot.20210521/src/bcatalog.c	(revision 41619)
+++ /branches/eam_branches/relphot.20210521/src/bcatalog.c	(revision 41620)
@@ -35,17 +35,4 @@
   ALLOCATE (Nvalid, int, Nsecfilt);
 
-  // flags used by the photometry analysis (excluding UBERCAL)
-  unsigned int PHOTOM_FLAGS = 
-    ID_MEAS_NOCAL          | // detection ignored for this analysis (photcode, time range)
-    ID_MEAS_POOR_PHOTOM    | // detection is photometry outlier 
-    ID_MEAS_SKIP_PHOTOM    | // detection was ignored for photometry measurement 
-    ID_MEAS_AREA           | // detetion was outside acceptable area of device
-    ID_MEAS_SYNTH_MAG      | // magnitude is synthetic
-    ID_MEAS_STACK_PRIMARY  | // this stack measurement is in the primary skycell
-    ID_MEAS_STACK_PHOT_SRC | // this measurement supplied the stack photometry
-    ID_MEAS_PHOTOM_PSF     | // this measurement is used for the mean psf mag
-    ID_MEAS_PHOTOM_APER    | // this measurement is used for the mean ap mag
-    ID_MEAS_PHOTOM_KRON    ; // this measurement is used for the mean kron mag
-    
   /* exclude stars not in range or with too few measurements */
   for (i = 0; i < catalog[0].Naverage; i++) {
@@ -61,16 +48,6 @@
     }
 
-    // reset the calculated average magnitudes (does not affect ubercal-tied measurements or images)
-    if (RESET) {
-      int Ns;
-
-      for (Ns = 0; Ns < Nphotcodes; Ns++) {
-
-	int thisCode = photcodes[Ns][0].code;
-	int Nsec = GetPhotcodeNsec(thisCode);
-
-	dvo_secfilt_init (&subcatalog[0].secfilt[Nsecfilt*Naverage+Nsec], SECFILT_RESET_ALL);
-      }
-    }
+    // reset the calculated average magnitudes for active photcode only
+    ResetAverageActivePhotcodes (&subcatalog[0].secfilt[Nsecfilt*Naverage]);
 
     Nm = 0;
@@ -111,5 +88,5 @@
       if (isnan(catalog[0].measure[offset].Mkron)) { Nnan ++; continue; }
 
-      // require 0x01 in photFlags (fitted with a PSF)
+      // require 0x01 in photFlags (fitted with a PSF) -- add to the test data
 // XXX EAM : test is inconsistent -- fix      if ((catalog[0].measure[offset].photFlags & 0x01) == 0) { Nbad ++; continue; }
 
@@ -156,21 +133,6 @@
       subcatalog[0].measureT[Nmeasure].dbFlags &= ~ID_MEAS_SKIP_PHOTOM;
       subcatalog[0].measureT[Nmeasure].averef = Naverage;
-      if (RESET) { 
-	// only reset Mcal for measures with a matching image
-	// do not reset Mcal for ubercal images unless explicitly requested
-	if (subcatalog[0].measureT[Nmeasure].dbFlags & ID_MEAS_PHOTOM_UBERCAL) {
-	  if (!KEEP_UBERCAL) {
-	    subcatalog[0].measureT[Nmeasure].McalPSF  = 0.0;
-	    subcatalog[0].measureT[Nmeasure].McalAPER = 0.0;
-	    subcatalog[0].measureT[Nmeasure].dbFlags &= ~ID_MEAS_PHOTOM_UBERCAL;
-	  } 
-	} else {
-	  if (getImageEntry (Nmeasure, Ncat) >= 0) {
-	    subcatalog[0].measureT[Nmeasure].McalPSF  = 0.0;
-	    subcatalog[0].measureT[Nmeasure].McalAPER = 0.0;
-	  }
-	}
-	subcatalog[0].measureT[Nmeasure].dbFlags &= ~PHOTOM_FLAGS;
-      }
+      ResetMeasureZeroPoints (&subcatalog[0].measureT[Nmeasure], Nmeasure, Ncat);
+
       Nmeasure ++;
       Nm ++;
Index: /branches/eam_branches/relphot.20210521/src/launch_region_hosts.c
===================================================================
--- /branches/eam_branches/relphot.20210521/src/launch_region_hosts.c	(revision 41619)
+++ /branches/eam_branches/relphot.20210521/src/launch_region_hosts.c	(revision 41620)
@@ -62,4 +62,5 @@
     if (RESET)         	     	    strextend (&command, "-reset");
     if (RESET_ZEROPTS) 	     	    strextend (&command, "-reset-zpts");
+    if (RESET_FLATCORR)	     	    strextend (&command, "-reset-flat");
     if (!KEEP_UBERCAL) 	     	    strextend (&command, "-reset-ubercal");
     if (DophotSelect)  	     	    strextend (&command, "-dophot %d", DophotValue);
Index: /branches/eam_branches/relphot.20210521/src/load_catalogs.c
===================================================================
--- /branches/eam_branches/relphot.20210521/src/load_catalogs.c	(revision 41619)
+++ /branches/eam_branches/relphot.20210521/src/load_catalogs.c	(revision 41620)
@@ -165,4 +165,5 @@
     if (RESET)         	     { strextend (&command, "-reset"); }
     if (RESET_ZEROPTS) 	     { strextend (&command, "-reset-zpts"); }
+    if (RESET_FLATCORR)	     { strextend (&command, "-reset-flat"); }
     if (!KEEP_UBERCAL) 	     { strextend (&command, "-reset-ubercal"); }
     if (DophotSelect)  	     { strextend (&command, "-dophot %d", DophotValue); }
Index: /branches/eam_branches/relphot.20210521/src/load_images.c
===================================================================
--- /branches/eam_branches/relphot.20210521/src/load_images.c	(revision 41619)
+++ /branches/eam_branches/relphot.20210521/src/load_images.c	(revision 41620)
@@ -57,21 +57,5 @@
   // reset image values as needed.  always allow 'few' images to succeed, if possible (new
   // images / detections may have been added
-  for (off_t i = 0; i < Nsubset; i++) {
-    // reset these two regardless (we will re-determine)
-    subset[i].flags &= ~(ID_IMAGE_PHOTOM_NOCAL | ID_IMAGE_PHOTOM_POOR | ID_IMAGE_PHOTOM_SKIP | ID_IMAGE_PHOTOM_FEW);
-    if (RESET) {
-      if (RESET_ZEROPTS) {
-	if (!KEEP_UBERCAL || !(subset[i].flags & ID_IMAGE_PHOTOM_UBERCAL)) {
-	  subset[i].McalPSF  = 0.0;
-	  subset[i].McalAPER = 0.0;
-	  subset[i].dMcal    = NAN;
-	  subset[i].flags   &= ~ID_IMAGE_PHOTOM_UBERCAL;
-	}
-      }
-      subset[i].flags &= ~(ID_IMAGE_MOSAIC_POOR | ID_IMAGE_NIGHT_POOR);
-      subset[i].flags &= ~(ID_IMAGE_IMAGE_PHOTCAL | ID_IMAGE_MOSAIC_PHOTCAL | ID_IMAGE_TGROUP_PHOTCAL);
-      subset[i].ubercalDist = 1000;
-    }
-  }
+  ResetImages (subset, Nsubset);
 
   // XXX consider allowing relphot to skip the AstroMap load (do we need precise astrometry here?)
@@ -115,4 +99,11 @@
   return TRUE;
 }
+
+// This function re-creates the image subset array using the array of LineNumbers saved
+// initially by load_images.  This function is only called by relphot_images.c after the
+// loops have been completed (if any).  I am not certain this function is doing anything
+// at all : the values in the image subset are copied back to the master table in
+// save_images_updates() so the values copied from the master to the subset should already
+// be there.
 
 int reload_images (FITS_DB *db) {
Index: /branches/eam_branches/relphot.20210521/src/reload_catalogs.c
===================================================================
--- /branches/eam_branches/relphot.20210521/src/reload_catalogs.c	(revision 41619)
+++ /branches/eam_branches/relphot.20210521/src/reload_catalogs.c	(revision 41620)
@@ -90,5 +90,5 @@
     initTGroupBins (&catalog, 1);
 
-    initGridBins   (&catalog, 1); 
+    initGridBins   (); 
     TIMESTAMP(time3b);
 
@@ -237,28 +237,22 @@
     char *command = NULL;
     strextend (&command, "relphot_client %s -update-catalogs %s", PhotcodeList, imageFile);
-    strextend (&command, "-hostID %d -D CATDIR %s -hostdir %s", group->hosts[i][0].hostID, CATDIR, group->hosts[i][0].pathname);
+    strextend (&command, "-hostID %d", group->hosts[i][0].hostID);
+    strextend (&command, "-D CATDIR %s", CATDIR);
+    strextend (&command, "-hostdir %s", group->hosts[i][0].pathname);
     strextend (&command, "-region %f %f %f %f", UserPatch.Rmin, UserPatch.Rmax, UserPatch.Dmin, UserPatch.Dmax);
     strextend (&command, "-statmode %s", STATMODE);
     strextend (&command, "-D CAMERA %s", CAMERA);
-    strextend (&command, "-D STAR_TOOFEW %d -minerror %f", STAR_TOOFEW, MIN_ERROR);
-
-    // options & configs which affect relphot_client -update-catalogs
-    // VERBOSE, VERBOSE2
-    // RESET
-    // RESET_ZEROPTS
-    // TimeSelect
-    // AreaSelect
-    // STATMODE
-    // STAR_TOOFEW
-    // MIN_ERROR
-    
+    strextend (&command, "-D STAR_TOOFEW %d", STAR_TOOFEW);
+    strextend (&command, "-minerror %f", MIN_ERROR);
+
     if (VERBOSE)           { strextend (&command, "-v"); }
     if (VERBOSE2)          { strextend (&command, "-vv"); }
     if (RESET)             { strextend (&command, "-reset"); }
     if (RESET_ZEROPTS)     { strextend (&command, "-reset-zpts"); }
+    if (RESET_FLATCORR)    { strextend (&command, "-reset-flat"); }
+    if (!KEEP_UBERCAL)     { strextend (&command, "-reset-ubercal"); }
     if (PRESERVE_PS1)      { strextend (&command, "-preserve-ps1"); }
+    if (IS_DIFF_DB)        { strextend (&command, "-is-diff-db"); }
     if (UPDATE)            { strextend (&command, "-update"); }
-    if (IS_DIFF_DB)        { strextend (&command, "-is-diff-db"); }
-    if (!KEEP_UBERCAL)     { strextend (&command, "-reset-ubercal"); }
     if (UPDATE_CATFORMAT)  { strextend (&command, "-update-catformat %s", UPDATE_CATFORMAT); }
     if (BOUNDARY_TREE)     { strextend (&command, "-boundary-tree %s", BOUNDARY_TREE); }
@@ -266,4 +260,5 @@
     if (GRID_ZEROPT)       { strextend (&command, "-grid %s", GRID_MEANFILE); } 
     if (USE_BASIC_CHECK)   {  strextend (&command, "-basic-image-search"); }
+    // if (USE_ALL_IMAGES)    { strextend (&command, "-use-all-images"); }
     if (USE_MCAL_PSF_FOR_STACK_APER) { strextend (&command, "-use-mcal-psf-for-stack-aper"); }
 
Index: /branches/eam_branches/relphot.20210521/src/relphot_images.c
===================================================================
--- /branches/eam_branches/relphot.20210521/src/relphot_images.c	(revision 41619)
+++ /branches/eam_branches/relphot.20210521/src/relphot_images.c	(revision 41620)
@@ -38,5 +38,12 @@
   MARKTIME("-- load image data: %f sec\n", dtime);
 
-  /* load regions and images based on specified sky patch and/or catalog */
+  // load regions and images based on specified sky patch and/or catalog 
+  // NOTE: load_images transfers zero points from images to mosaics (initMosaicMcal)
+  // and to tgroups (initTGroupMcal), resetting the image zero points to 0.0.
+
+  // (if both tgroups and mosaics are used, the mosaics will hold the value and tgroups
+  // will get values of 0.0.  This is not clearly the right way to go, but may not matter
+  // if we reset everything).
+
   load_images (&db, skylist, &UserPatch, FALSE, USE_ALL_IMAGES);
   MARKTIME("-- load images: %f sec\n", dtime);
@@ -45,5 +52,5 @@
   if (!UPDATE) dvo_image_unlock (&db); 
 
-  initGridBins (); // allocates the empty array of corrections (elements are build below)
+  initGridBins (); // allocates the empty array of corrections (elements are built below)
 
   if (CALIBRATE_STACKS_AND_WARPS || (NLOOP > 0)) {
Index: /branches/eam_branches/relphot.20210521/src/relphot_objects.c
===================================================================
--- /branches/eam_branches/relphot.20210521/src/relphot_objects.c	(revision 41619)
+++ /branches/eam_branches/relphot.20210521/src/relphot_objects.c	(revision 41620)
@@ -10,6 +10,5 @@
 int relphot_objects (SkyList *skylist, int hostID, char *hostpath) {
 
-  off_t i, j, k;
-  int Nsecfilt;
+  off_t i;
   struct stat filestat;
 
@@ -85,30 +84,5 @@
 
     // reset the calculated average magnitudes (does not affect ubercal-tied measurements or images)
-    if (RESET) {
-      Nsecfilt = catalog.Nsecfilt;
-
-      DVOAverageFlags photomBits = 
-	ID_OBJ_EXT             | // extended in our data (eg, PS)
-	ID_OBJ_EXT_ALT         | // extended in external data (eg, 2MASS)
-	ID_OBJ_GOOD            | // good-quality measurement in our data (eg,PS)
-	ID_OBJ_GOOD_ALT        | // good-quality measurement in  external data (eg, 2MASS)
-	ID_OBJ_GOOD_STACK      | // good-quality object in the stack (> 1 good stack)
-	ID_OBJ_BEST_STACK      | // the primary stack measurement are the best measurements
-	ID_OBJ_SUSPECT_STACK   | // suspect object in the stack (> 1 good or suspect stack, < 2 good)
-	ID_OBJ_BAD_STACK;        // good-quality object in the stack (> 1 good stack)
-      
-      for (j = 0; j < catalog.Naverage; j++) {
-	catalog.average[j].flags    &= ~photomBits; // reset all except astrometry bits
-	catalog.average[j].psfQF     = NAN;     // reset (will be re-calculated here)
-	catalog.average[j].psfQFperf = NAN;	// reset (will be re-calculated here)
-	catalog.average[j].stargal   = NAN;	// reset (will be re-calculated here)
-	catalog.average[j].photFlagsUpper = 0;	// reset (will be re-calculated here)
-	catalog.average[j].photFlagsLower = 0;	// reset (will be re-calculated here)
-	catalog.average[j].NwarpOK        = 0;	// reset (will be re-calculated here)
-	for (k = 0; k < Nsecfilt; k++) {
-	  dvo_secfilt_init (&catalog.secfilt[j*Nsecfilt + k], SECFILT_RESET_ALL);
-	}
-      }
-    }
+    ResetAverageObjects(&catalog);
 
     populate_tiny_values(&catalog, DVO_TV_MEASURE | DVO_TV_AVERAGE);
@@ -119,5 +93,5 @@
     // XXX reload_catalogs.c calls initTGroupBins here 
 
-    initGridBins   (&catalog, 1); 
+    initGridBins   (); 
 
     findImages (&catalog, 1, FALSE);
@@ -235,11 +209,13 @@
     if (RESET)         	   { strextend (&command, "-reset"); }
     if (RESET_ZEROPTS) 	   { strextend (&command, "-reset-zpts"); }
+    if (RESET_FLATCORR)	   { strextend (&command, "-reset-flat"); }
+    if (!KEEP_UBERCAL) 	   { strextend (&command, "-reset-ubercal"); }
     if (PRESERVE_PS1)      { strextend (&command, "-preserve-ps1"); }
     if (IS_DIFF_DB)        { strextend (&command, "-is-diff-db"); }
     if (UPDATE)        	   { strextend (&command, "-update"); }
-    if (!KEEP_UBERCAL) 	   { strextend (&command, "-reset-ubercal"); }
     if (UPDATE_CATFORMAT)  { strextend (&command, "-update-catformat %s", UPDATE_CATFORMAT); }
     if (BOUNDARY_TREE)     { strextend (&command, "-boundary-tree %s", BOUNDARY_TREE); }
     if (SYNTH_ZERO_POINTS) { strextend (&command, "-synthphot-zpts %s", SYNTH_ZERO_POINTS); }
+    // if (GRID_ZEROPT)       { strextend (&command, "-grid %s", GRID_MEANFILE); } 
     if (USE_BASIC_CHECK)   { strextend (&command, "-basic-image-search"); }
     if (USE_ALL_IMAGES)    { strextend (&command, "-use-all-images"); }
@@ -251,4 +227,6 @@
 
     // if (SET_MREL_VERSION != 1) { strextend (&command, "-set-mrel-version %d", SET_MREL_VERSION); } 
+
+    // missing AreaSelect & TimeSelect
 
     fprintf (stderr, "command: %s\n", command);
Index: /branches/eam_branches/relphot.20210521/src/select_images.c
===================================================================
--- /branches/eam_branches/relphot.20210521/src/select_images.c	(revision 41619)
+++ /branches/eam_branches/relphot.20210521/src/select_images.c	(revision 41620)
@@ -102,7 +102,4 @@
       
     if (!(i % 300000)) fprintf (stderr, ".");
-
-    // XXX I am just going for force this for the moment:
-    // goto found_it;
 
     /* exclude images by photcode (we are not doing this, but I'm not sure why) */
Index: /branches/eam_branches/relphot.20210521/src/setMrelCatalog.c
===================================================================
--- /branches/eam_branches/relphot.20210521/src/setMrelCatalog.c	(revision 41619)
+++ /branches/eam_branches/relphot.20210521/src/setMrelCatalog.c	(revision 41620)
@@ -351,5 +351,6 @@
   for (Nsec = 0; Nsec < Nsecfilt; Nsec++) {
 
-    // if we detected this object in PS1, or do not request -preserve-ps1, keep the mean photometry values
+    // -preserve-ps1 means keep an existing average PS1 value
+    // if we did not detect this object in PS1, or do not request -preserve-ps1, keep the mean photometry values
     if (!PRESERVE_PS1 || !(secfilt[Nsec].flags & ID_SECF_HAS_PS1)) {
       dvo_secfilt_init (&secfilt[Nsec], SECFILT_RESET_CHIP); // this does not reset astrometry or STACK bits
@@ -606,18 +607,5 @@
 
     // reset all stack-related values for this secfilt:
-    secfilt[Nsec].stackBestOff = -1;
-    secfilt[Nsec].stackPrmryOff = -1;
-    secfilt[Nsec].FpsfStk   = NAN;
-    secfilt[Nsec].dFpsfStk  = NAN;
-    secfilt[Nsec].FkronStk  = NAN;
-    secfilt[Nsec].dFkronStk = NAN;
-    secfilt[Nsec].FapStk    = NAN;
-    secfilt[Nsec].dFapStk   = NAN;
-    secfilt[Nsec].MpsfStk   = NAN;
-    secfilt[Nsec].MkronStk  = NAN;
-    secfilt[Nsec].MapStk    = NAN;
-    secfilt[Nsec].Nstack    = 0;
-    secfilt[Nsec].NstackDet = 0;
-    secfilt[Nsec].flags    &= ~ID_SECF_STACK_FLAGS; // reset the stack flags
+    dvo_secfilt_init (&secfilt[Nsec], SECFILT_RESET_STACK);
 
     for (off_t k = 0; k < Nmeasure; k++) {
Index: /branches/eam_branches/relphot.20210521/src/setMrelFinal.c
===================================================================
--- /branches/eam_branches/relphot.20210521/src/setMrelFinal.c	(revision 41619)
+++ /branches/eam_branches/relphot.20210521/src/setMrelFinal.c	(revision 41620)
@@ -11,74 +11,6 @@
   int Nsecfilt = GetPhotcodeNsecfilt ();
 
-  /*** RESET photometry flags and Mcal values ***/
-  if (RESET) {
-    // flags used by the photometry analysis (excluding UBERCAL)
-    unsigned int PHOTOM_FLAGS = 
-      ID_MEAS_NOCAL |          // detection ignored for this analysis (photcode, time range)
-      ID_MEAS_POOR_PHOTOM |    // detection is photometry outlier 
-      ID_MEAS_SKIP_PHOTOM |    // detection was ignored for photometry measurement 
-      ID_MEAS_AREA |	       // detetion was outside acceptable area of device
-      ID_MEAS_SYNTH_MAG |      // magnitude is synthetic
-      ID_MEAS_STACK_PRIMARY |  // this stack measurement is in the primary skycell
-      ID_MEAS_STACK_PHOT_SRC;  // this measurement supplied the stack photometry
-
-    // ID_MEAS_PHOTOM_UBERCAL -- externally-supplied zero point from ubercal analysis
-    // this is set by 'setphot', do not reset here
-
-    // flags used by the photometry analysis (excluding UBERCAL)
-    // unsigned int secfiltFlags = 
-    //   ID_PHOTOM_PASS_0 | // average measured at pass 0
-    //   ID_PHOTOM_PASS_1 | // average measured at pass 1
-    //   ID_PHOTOM_PASS_2 | // average measured at pass 2
-    //   ID_PHOTOM_PASS_3 | // average measured at pass 3
-    //   ID_PHOTOM_PASS_4 | // average measured at pass 3
-    //   ID_SECF_USE_SYNTH | // average measured at pass 3
-    //   ID_SECF_USE_UBERCAL | // average measured at pass 3
-    //   ID_SECF_OBJ_EXT; // average measured at pass 3
-    
-    // XXX I should really deprecate the concept of applying the average 
-    // calculation to a limited set of photcodes.  
-    // for now, just do all photcodes here (
-    // as it stands, only stacks are limited by photcode; mean exp and forced warp 
-    // are applied to all Nsecfilt
-
-    for (i = 0; i < catalog[0].Naverage; i++) {
-      int Ns;
-      for (Ns = 0; Ns < Nsecfilt; Ns++) {
-	
-	off_t N = Nsecfilt*i+Ns;
-	dvo_secfilt_init (&catalog[0].secfilt[N], SECFILT_RESET_ALL);
-
-	off_t m = catalog[0].average[i].measureOffset;
-	off_t j;
-	for (j = 0; j < catalog[0].average[i].Nmeasure; j++, m++) {
-	
-	  /* select measurements by time */
-	  if (TimeSelect) {
-	    if (catalog[0].measure[m].t < TSTART) continue;
-	    if (catalog[0].measure[m].t > TSTOP) continue;
-	  }
-	
-	  // only reset Mcal for measures with a matching image
-	  // do not reset Mcal for ubercal images unless explicitly requested
-
-	  if (catalog[0].measure[m].dbFlags & ID_MEAS_PHOTOM_UBERCAL) {
-	    if (!KEEP_UBERCAL) {
-	      catalog[0].measure[m].McalPSF  = 0.0;
-	      catalog[0].measure[m].McalAPER = 0.0;
-	      catalog[0].measure[m].dbFlags &= ~ID_MEAS_PHOTOM_UBERCAL;
-	    } 
-	  } else {
-	    if (RESET_ZEROPTS && (getImageEntry (m, 0) >= 0)) {
-	      catalog[0].measure[m].McalPSF  = 0.0;
-	      catalog[0].measure[m].McalAPER = 0.0;
-	    }
-	  }
-	  catalog[0].measure[m].dbFlags &= ~PHOTOM_FLAGS;
-	}
-      }
-    }
-  }
-
+  ResetAverageAndMeasure (catalog);
+  
   // this sets flags in the measureT element, not the measure element
   setExclusions (catalog, 1, VERBOSE);  /* mark by area */
