Index: /branches/eam_branches/ipp-20211108/Ohana/src/relphot/src/setMrelCatalog.c
===================================================================
--- /branches/eam_branches/ipp-20211108/Ohana/src/relphot/src/setMrelCatalog.c	(revision 42065)
+++ /branches/eam_branches/ipp-20211108/Ohana/src/relphot/src/setMrelCatalog.c	(revision 42066)
@@ -133,5 +133,4 @@
 int setMrelAverageExposure (Catalog *catalog, int cat, off_t ave, int Nsecfilt, int isSetMrelFinal, SetMrelInfo *results) {
 
-  int Nsec;
   off_t k;
 
@@ -179,5 +178,6 @@
     // these bits should not be set unless we use them in this pass
     // (note that we can only un-set them in the final pass when we actually have measure, not just measureT
-    if (measure) measure[k].dbFlags &= ~(ID_MEAS_PHOTOM_PSF | ID_MEAS_PHOTOM_APER | ID_MEAS_PHOTOM_KRON);
+    // CCL updated 2021.12.09:  the following line will reset the dbFlags for measure so I comment it out.
+    // if (measure) measure[k].dbFlags &= ~(ID_MEAS_PHOTOM_PSF | ID_MEAS_PHOTOM_APER | ID_MEAS_PHOTOM_KRON);
 
     // skip measurements that do not have a valid photcode (raise exception?)
@@ -272,5 +272,8 @@
 
       // the flat-field correction is stored in measure.Mflat
-      Mflat = isnan (measureT[k].Mflat) ? 0.0 : measureT[k].Mflat;
+      //Mflat = isnan (measureT[k].Mflat) ? 0.0 : measureT[k].Mflat;
+      if (measure) {
+        Mflat = isnan (measure[k].Mflat) ? 0.0 : measure[k].Mflat;
+      }
 
       // see note above re: final output vs image analysis
@@ -322,5 +325,5 @@
 	results->aperData[Nsec].errlist[Nap] = dMap;
 	results->aperData[Nsec].wgtlist[Nap] = modifiedWeight;
-	results->aperData[Nsec].ranking[Nap] = measureRank[k];
+	results->aperData[Nsec].ranking[Nap] = measureRank ? measureRank[k] : 0;
 	results->aperData[Nsec].measSeq[Nap] = k;
 	results->aperData[Nsec].msklist[Nap] = 0;
@@ -383,6 +386,8 @@
   float psfQfPerfMax = 0.0;
 
-  // now calculate the mean stats for the Nsec bands.
-  for (Nsec = 0; Nsec < Nsecfilt; Nsec++) {
+  // now calculate the mean stats for the selected Nsec bands.
+  for (int Ns = 0; Ns < Nphotcodes; Ns++) {
+    int thisCode = photcodes[Ns][0].code;
+    int Nsec = GetPhotcodeNsec(thisCode);
 
     // -preserve-ps1 means keep an existing average PS1 value
@@ -477,5 +482,6 @@
       secfilt[Nsec].dMkronChp = kronstats->error; 
       secfilt[Nsec].sMkronChp = kronstats->sigma; 
-      secfilt[Nsec].NusedKron = NrankingKRON;
+      // actually used is Nmeas, NrankingKRON is including masked data
+      secfilt[Nsec].NusedKron = kronstats->Nmeas;
     }
 
@@ -521,5 +527,6 @@
 	secfilt[Nsec].dMapChp = apstats->error; 
 	secfilt[Nsec].sMapChp = apstats->sigma; 
-	secfilt[Nsec].NusedAp = NrankingAPER;
+        // actually used is Nmeas, NrankingAPER is including masked data
+	secfilt[Nsec].NusedAp = apstats->Nmeas;
       }
       int minRankAper = (NrankingAPER > 0) ? results->aperData[Nsec].ranking[0] : 10;
