Index: branches/eam_branches/relphot.20210521/src/ResetOps.c
===================================================================
--- branches/eam_branches/relphot.20210521/src/ResetOps.c	(revision 41624)
+++ branches/eam_branches/relphot.20210521/src/ResetOps.c	(revision 41625)
@@ -75,4 +75,7 @@
     }
   }
+  if (RESET_FLATCORR) {
+    measure->Mflat = 0.0;
+  }
   measure->dbFlags &= ~photomBitsMeasure;
 }
@@ -88,5 +91,5 @@
   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);
+    subset[i].flags &= ~(ID_IMAGE_PHOTOM_POOR | ID_IMAGE_PHOTOM_FEW);
     if (RESET) {
       if (RESET_ZEROPTS) {
@@ -117,10 +120,12 @@
 
   for (off_t i = 0; i < catalog->Naverage; i++) {
-    catalog[0].average[i].psfQF     = NAN;	// force recalculation in setMrelCatalog
-    catalog[0].average[i].psfQFperf = NAN;	// force recalculation in setMrelCatalog
-    catalog[0].average[i].stargal   = NAN;	// force recalculation in setMrelCatalog
-    catalog[0].average[i].photFlagsUpper = 0;	// reset (will be re-calculated)
-    catalog[0].average[i].photFlagsLower = 0;	// reset (will be re-calculated)
-    catalog[0].average[i].NwarpOK        = 0;	// reset (will be re-calculated)
+    catalog->average[i].psfQF     = NAN;	// force recalculation in setMrelCatalog
+    catalog->average[i].psfQFperf = NAN;	// force recalculation in setMrelCatalog
+    catalog->average[i].stargal   = NAN;	// force recalculation in setMrelCatalog
+    catalog->average[i].photFlagsUpper = 0;	// reset (will be re-calculated)
+    catalog->average[i].photFlagsLower = 0;	// reset (will be re-calculated)
+    catalog->average[i].NwarpOK        = 0;	// reset (will be re-calculated)
+
+    // RESET_FLATCORR independent of RESET?
 
     if (!RESET) continue;
@@ -128,38 +133,61 @@
     catalog->average[i].flags    &= ~photomBitsAverage; // reset all photometry bits (but not astrom)
 
-    for (int Ns = 0; Ns < Nsecfilt; Ns++) {
+    // Reset secfilt values only for the active photcodes
+    for (int Ns = 0; Ns < Nphotcodes; Ns++) {
+      
+      int thisCode = photcodes[Ns][0].code;
+      int Nsec = GetPhotcodeNsec(thisCode);
+      off_t N = Nsecfilt*i+Nsec;
+      
+      dvo_secfilt_init (&catalog->secfilt[N], SECFILT_RESET_ALL);
+    }
+
+    // Reset measure values only for the active photcodes
+    
+    off_t m = catalog->average[i].measureOffset;
+    
+    for (off_t j = 0; j < catalog->average[i].Nmeasure; j++, m++) {
+      
+      /* select measurements for active photcodes */
+      PhotCode *code = GetPhotcodebyCode (catalog->measure[m].photcode);
+      if (!code) continue;
+      // photcode exists, but is it one of the active ones?
+      int found = FALSE;
+      for (int Ns = 0; !found && (Ns < Nphotcodes); Ns++) {
+	if (code->equiv != photcodes[Ns][0].code) continue;
+	found = TRUE;
+      }
+      if (!found) continue; 
+
+      // reset the Mflat value if requested
+      if (RESET_FLATCORR) {
+	catalog->measure[m].Mflat = 0.0;
+      }
+      
+      /* select measurements by time */
+      if (TimeSelect) {
+	if (catalog->measure[m].t < TSTART) continue;
+	if (catalog->measure[m].t > TSTOP) continue;
+      }
 	
-      off_t N = Nsecfilt*i+Ns;
-      dvo_secfilt_init (&catalog->secfilt[N], SECFILT_RESET_ALL);
+      // only reset Mcal for measures with a matching image
+      // do not reset Mcal for ubercal images unless explicitly requested
 
-      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;
+      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;
 	}
-	
-	// 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;
       }
+      catalog->measure[m].dbFlags &= ~photomBitsMeasure;
     }
   }
 }
 
+
