Index: /branches/eam_branches/ipp-20111122/Ohana/src/relphot/src/StarOps.c
===================================================================
--- /branches/eam_branches/ipp-20111122/Ohana/src/relphot/src/StarOps.c	(revision 33372)
+++ /branches/eam_branches/ipp-20111122/Ohana/src/relphot/src/StarOps.c	(revision 33373)
@@ -68,11 +68,11 @@
 	int Nsec = GetPhotcodeNsec(thisCode);
 
-	/* calculate the average value for a single star */
+	/* calculate the average mag in this SEC photcode for a single star */
 
 	// skip bad stars
 	if (catalog[i].secfilt[Nsecfilt*j+Nsec].flags & STAR_BAD) continue;
+
+	N = 0;
 	m = catalog[i].averageT[j].measureOffset;
-
-	N = 0;
 	for (k = 0; k < catalog[i].averageT[j].Nmeasure; k++, m++) {
 
@@ -82,33 +82,15 @@
 	  if (code->equiv != thisCode) { continue; }
 
-	  // int ecode = GetPhotcodeEquivCodebyCode (catalog[i].measureT[m].photcode);
-	  // if (ecode != thisCode) { continue; }
-
-	  if (catalog[i].measureT[m].dbFlags & MEAS_BAD) {
-	    Nbad ++;
-	    continue;
-	  }
-
-	  // XXX allow REF stars (no Image Entry) to be included in the calculation this
-	  // should be optionally set, and should allow for REF stars to be downweighted by
-	  // more than their reported errors.  how such info is carried is unclear...
+	  if (catalog[i].measureT[m].dbFlags & MEAS_BAD) { Nbad ++; continue; }
+
 	  if (getImageEntry (m, i) < 0) {
 	    Mcal = Mmos = Mgrid = 0;
 	  } else {
 	    Mcal  = getMcal  (m, i, flatcorr, catalog);
-	    if (isnan(Mcal)) { 
-	      Ncal ++;
-	      continue;
-	    }
+	    if (isnan(Mcal)) { Ncal ++; continue; }
 	    Mmos  = getMmos  (m, i);
-	    if (isnan(Mmos)) {
-	      Nmos ++;
-	      continue;
-	    }
+	    if (isnan(Mmos)) { Nmos ++; continue; }
 	    Mgrid = getMgrid (m, i);
-	    if (isnan(Mgrid)) {
-	      Ngrid++;
-	      continue;
-	    }
+	    if (isnan(Mgrid)) { Ngrid++; continue; }
 	  }
 
@@ -184,5 +166,5 @@
   double *list, *dlist;
   StatType stats;
-  int Nsec, ecode;
+  int Nsec;
 
   int Nsecfilt = GetPhotcodeNsecfilt ();
@@ -201,7 +183,4 @@
     for (j = 0; j < catalog[i].Naverage; j++) {
 
-      /* skip stars already calibrated */
-      if (catalog[i].found[j]) continue;  
-
       int Ns;
       for (Ns = 0; Ns < Nphotcodes; Ns++) {
@@ -209,4 +188,7 @@
 	int thisCode = photcodes[Ns][0].code;
 	Nsec = GetPhotcodeNsec(thisCode);
+
+	/* star/photcodes already calibrated */
+	if (catalog[i].found[Nsecfilt*j+Nsec]) continue;  
 
 	N = 0;
@@ -218,7 +200,4 @@
 	  if (!code) continue;
 	  if (code->equiv != thisCode) { continue; }
-
-	  // ecode = GetPhotcodeEquivCodebyCode (catalog[i].measureT[m].photcode);
-	  // if (ecode != thisCode) { continue; }
 
 	  if (catalog[i].measureT[m].dbFlags & MEAS_BAD) continue;
@@ -249,5 +228,5 @@
 	  dlist[N] = MAX (hypot(catalog[i].measureT[m].dM, code->photomErrSys), MIN_ERROR);
 
-	  // up-weight the ubercal values (or convergence can take a long time...)
+	  // up-weight the ubercal values (or convergence can take a long time...) (XXX make this optional?)
 	  if (catalog[i].measureT[m].dbFlags & ID_MEAS_PHOTOM_UBERCAL) {
 	    dlist[N] = MAX (0.1*catalog[i].measureT[m].dM, MIN_ERROR);
@@ -269,5 +248,5 @@
 
 	liststats (list, dlist, N, &stats);
-	if (mark) catalog[i].found[j] = TRUE;
+	if (mark) catalog[i].found[Nsecfilt*j+Nsec] = TRUE;
 
 	/* use sigma or error in dM for output? */
@@ -578,4 +557,5 @@
 	  if (ecode != thisCode) { continue; }
 
+	  // why am I skipping this??
 	  /* if (catalog[i].measureT[m].dbFlags & MEAS_BAD) continue; */
 	  Mcal  = getMcal  (m, i, flatcorr, catalog);
Index: /branches/eam_branches/ipp-20111122/Ohana/src/relphot/src/setMrelFinal.c
===================================================================
--- /branches/eam_branches/ipp-20111122/Ohana/src/relphot/src/setMrelFinal.c	(revision 33372)
+++ /branches/eam_branches/ipp-20111122/Ohana/src/relphot/src/setMrelFinal.c	(revision 33373)
@@ -10,9 +10,15 @@
   int ecode;
 
+  int Nsecfilt = GetPhotcodeNsecfilt ();
+
   /* if we reset the catalog, reset all the current measurements */
   if (RESET) {
-
-    int Nsecfilt = GetPhotcodeNsecfilt ();
-
+    // 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
+    
     int Ns;
     for (Ns = 0; Ns < Nphotcodes; Ns++) {
@@ -39,10 +45,9 @@
 	  }
 	
-	  catalog[0].measure[m].Mcal = 0;
-	  catalog[0].measure[m].dbFlags &= 0xff00;
-	  catalog[0].measure[m].dbFlags &= ~ID_MEAS_POOR_PHOTOM;
-	  catalog[0].measure[m].dbFlags &= ~ID_MEAS_SKIP_PHOTOM;
-	  catalog[0].measure[m].dbFlags &= ~ID_MEAS_AREA;
-	  catalog[0].measure[m].dbFlags &= ~ID_MEAS_NOCAL;
+	  if (!KEEP_UBERCAL) {
+	    catalog[0].measure[m].Mcal = 0;
+	    catalog[0].measure[m].dbFlags &= ~ID_MEAS_PHOTOM_UBERCAL;
+	  }
+	  catalog[0].measure[m].dbFlags &= ~PHOTOM_FLAGS;
 	}
       }
@@ -54,6 +59,6 @@
 
   /* set catalog[0].found[i] = FALSE */
-  ALLOCATE (catalog[0].found, off_t, MAX (1, catalog[0].Naverage));
-  for (i = 0; i < catalog[0].Naverage; i++) {
+  ALLOCATE (catalog[0].found, off_t, MAX (1, Nsecfilt*catalog[0].Naverage));
+  for (i = 0; i < Nsecfilt*catalog[0].Naverage; i++) {
     catalog[0].found[i] = FALSE;
   }
@@ -86,4 +91,16 @@
 
 /* ID_MEAS_SKIP marks measurements which were not used to calculate Mrel */
+// For each star & photcode, we are selecting the measurements to use in the average
+// photometry value.  This function is called several times, lowering the bar to
+// acceptance on each pass. The rejections at the different passes are:
+
+// 0 : only use measurements thought to be not extended and dM < SIGMA_LIM (REMOVED)
+// 1 : use extended and low-sig detections (REMOVED)
+// 3 : accept the measurements marked as outliers; accept images marked as outliers
+// 4 : accept the measurements outside of the instrumental magnitude limits (eg, SAT)
+
+// XXX if I'm reading this correctly, passes 0, 1, 2 are identical & accept all
+// non-outliers with Imag range
+
 void skip_measurements (Catalog *catalog, int pass, FlatCorrectionTable *flatcorr) {
 
@@ -104,5 +121,11 @@
   for (i = 0; i < catalog[0].Naverage; i++) {
     Ntot += catalog[0].average[i].Nmeasure;
+
+    // this function does not check secfilt entries separately, but there is no harm in
+    // twiddling the so skip a star here only we can only 
+    // have we gotten 
+
     if (catalog[0].found[i]) continue;
+  XXX : this should be done by secfilt.  think about this...
 
     m = catalog[0].average[i].measureOffset;
