Index: /branches/eam_branches/ipp-20111122/Ohana/src/relphot/src/StarOps.c
===================================================================
--- /branches/eam_branches/ipp-20111122/Ohana/src/relphot/src/StarOps.c	(revision 33457)
+++ /branches/eam_branches/ipp-20111122/Ohana/src/relphot/src/StarOps.c	(revision 33458)
@@ -232,4 +232,23 @@
 	  minUbercalDist = MIN(minUbercalDist, myUbercalDist);
 
+	  // count the extended detections
+	  if ((catalog[i].measure[m].photcode >= 10000) && (catalog[i].measure[m].photcode <= 10500)) {
+	    if (!isnan(catalog[i].measure[m].Map)) {
+	      if (catalog[i].measure[m].M - catalog[i].measure[m].Map > 0.5) {
+		Next ++;
+	      }
+	    }
+	  }
+	  // count extended detections for 2MASS (XXX NOTE hardwired photcodes 2011, 2012, 2013)
+	  if ((catalog[i].measure[m].photcode >= 2011) && (catalog[i].measure[m].photcode <= 2013)) {
+	    if (catalog[i].measure[m].photFlags & 0x00c00000) {
+	      Next ++;
+	    }
+	    if ((pass == 0) && !(catalog[i].measure[m].photFlags & 0x00000007)) {
+	      // detections without one of these bits should only be used in PASS_1
+	      continue;
+	    }
+	  }
+
 	  // dlist gives the error, which is used as the weight in WT_MEAN.
 	  // we can modify the resulting weight in a few ways:
@@ -255,24 +274,4 @@
 	    }
 	  }
-
-	  // count the extended detections
-	  if ((catalog[i].measure[m].photcode >= 10000) && (catalog[i].measure[m].photcode <= 10500)) {
-	    if (!isnan(catalog[i].measure[m].Map)) {
-	      if (catalog[i].measure[m].M - catalog[i].measure[m].Map > 0.5) {
-		Next ++;
-	      }
-	    }
-	  }
-	  // count extended detections for 2MASS (XXX NOTE hardwired photcodes 2011, 2012, 2013)
-	  if ((catalog[i].measure[m].photcode >= 2011) && (catalog[i].measure[m].photcode <= 2013)) {
-	    if (catalog[i].measure[m].photFlags & 0x00c00000) {
-	      Next ++;
-	    }
-	    if ((pass == 0) && !(catalog[i].measure[m].photFlags & 0x00000007)) {
-	      // detections without one of these bits should only be used in PASS_1
-	      continue;
-	    }
-	  }
-
 	  N++;
 	}
@@ -312,5 +311,5 @@
 
 	if ((Next > 0) && (Next > 0.5*N)) {
-	    catalog[i].secfilt[Nsecfilt*j+Nsec].flags |= ID_SECF_OBJ_EXT;
+	  catalog[i].secfilt[Nsecfilt*j+Nsec].flags |= ID_SECF_OBJ_EXT;
 	}
       }
@@ -322,4 +321,13 @@
   return (TRUE);
 }
+
+/* grab Nsec for named photcode */
+# define NAMED_PHOTCODE_NSEC(MY_NSEC,NAME)	\
+  short MY_NSEC = -1;				\
+  {						\
+  PhotCode *code = GetPhotcodebyName (NAME);	\
+  if (code) {					\
+    MY_NSEC = GetPhotcodeNsec (code->equiv);	\
+  } }
 
 // For each average object, set the average mags based on existing equiv photometry.
@@ -335,5 +343,4 @@
   double *list, *dlist;
   StatType stats;
-  PhotCode *code;
   DVOAverageFlags flagBits;
 
@@ -352,5 +359,9 @@
   Nsecfilt = GetPhotcodeNsecfilt ();
 
-# define PSFQUALSTATS 1
+  // we want to raise some bits on the 2MASS (JHK) secfilt flags, if we have 2MASS data
+  NAMED_PHOTCODE_NSEC (Nsec_J, "2MASS_J");
+  NAMED_PHOTCODE_NSEC (Nsec_H, "2MASS_H");
+  NAMED_PHOTCODE_NSEC (Nsec_K, "2MASS_K");
+
   for (i = 0; i < Ncatalog; i++) {
     for (j = 0; j < catalog[i].Naverage; j++) {
@@ -361,7 +372,9 @@
       // them in a single pass
 
+      int Next = 0;
+
       for (Ns = 0; Ns < Nsecfilt; Ns++) {
 
-	code = GetPhotcodebyNsec (Ns);
+	PhotCode *code = GetPhotcodebyNsec (Ns);
 	Nc = code[0].code;
 	
@@ -375,7 +388,13 @@
 	  if (isnan(Msys)) continue;
 
+	  // reject POOR detections (PSF_QF < 0.85) and count extended detections
 	  // XXX only apply this filter for psphot data from GPC1 for now...
-	  if (PSFQUALSTATS && (catalog[i].measure[m].photcode > 10000) && (catalog[i].measure[m].photcode < 10500)) {
-	      if (catalog[i].measure[m].psfQual < 0.85) continue;
+	  if ((catalog[i].measure[m].photcode > 10000) && (catalog[i].measure[m].photcode < 10500)) {
+	    if (catalog[i].measure[m].psfQual < 0.85) continue;
+	    if (!isnan(catalog[i].measure[m].Map)) {
+	      if (catalog[i].measure[m].M - catalog[i].measure[m].Map > 0.5) {
+		Next ++;
+	      }
+	    }
 	  }
 
@@ -398,63 +417,89 @@
 	catalog[i].secfilt[Nsecfilt*j+Ns].M_80 = 1000 * stats.Upper80;
 	catalog[i].secfilt[Nsecfilt*j+Ns].M_20 = 1000 * stats.Lower20;
+
+	if ((Next > 0) && (Next > 0.5*N)) {
+	  catalog[i].secfilt[Nsecfilt*j+Ns].flags |= ID_SECF_OBJ_EXT;
+	}
       }
 
       // update average flags based on the detection stats.  
-      // XXX we need to clean this up -- this is a serious set of hacks...
-      if (PSFQUALSTATS) { 
-	int Galaxy2MASS, GalaxySDSS, goodPS1, nEXT, nPSF, good2MASS;
-
-	Galaxy2MASS = FALSE; // best guess for galaxy based on 2MASS J measurements (gal_contam == measure.flags[0x00400000 | 0x00800000])
-	GalaxySDSS = FALSE;  // best guess for galaxy based on SDSS measurements (XXX need to fix SDSS flags)
-	goodPS1 = FALSE;     // true if any PS1 measurements have psfQual > 0.85
-	good2MASS = FALSE;   // true if 2MASS J measurements have significant detections
-	nEXT = nPSF = 0;     // number of PS1 PSF vs EXT measurements
-
-	m = catalog[i].average[j].measureOffset;
-	for (k = 0; k < catalog[i].average[j].Nmeasure; k++, m++) {
-
-	  // PS1 data :
-	  if ((catalog[i].measure[m].photcode >= 10000) && (catalog[i].measure[m].photcode <= 10500)) {
-	    if (catalog[i].measure[m].psfQual > 0.85) {
-	      goodPS1 = TRUE;
-	      if (!isnan(catalog[i].measure[m].Map)) {
-		if (catalog[i].measure[m].M - catalog[i].measure[m].Map > 0.5) {
-		  nEXT ++;
-		} else {
-		  nPSF ++;
-		}
+
+      int Galaxy2MASS = FALSE; // best guess for galaxy based on 2MASS J measurements (gal_contam == measure.flags[0x00400000 | 0x00800000])
+      int goodPS1 = FALSE;     // true if any PS1 measurements have psfQual > 0.85
+      int good2MASS = FALSE;   // true if 2MASS J measurements have significant detections
+      int nEXT = 0;
+      int nPSF = 0;     // number of PS1 PSF vs EXT measurements
+      int have2MASS = FALSE;
+      
+      // count, flag good and extended detections
+      m = catalog[i].average[j].measureOffset;
+      for (k = 0; k < catalog[i].average[j].Nmeasure; k++, m++) {
+
+	// PS1 data :
+	if ((catalog[i].measure[m].photcode >= 10000) && (catalog[i].measure[m].photcode <= 10500)) {
+	  if (catalog[i].measure[m].psfQual > 0.85) {
+	    goodPS1 = TRUE;
+	    if (!isnan(catalog[i].measure[m].Map)) {
+	      if (catalog[i].measure[m].M - catalog[i].measure[m].Map > 0.5) {
+		nEXT ++;
+	      } else {
+		nPSF ++;
 	      }
 	    }
 	  }
+	}
 	  
-	  // 2MASS data:
-	  if (catalog[i].measure[m].photcode == 2011) {
-	    if (catalog[i].measure[m].photFlags & 0x00c00000) {
-	      Galaxy2MASS = TRUE;
-	    }
-	    if (catalog[i].measure[m].photFlags & 0x00000007) {
-	      good2MASS = TRUE; 
-	    }
-	  }  
-	}
-
-	// we attempt to set a few flags here; reset those bits before trying:
-	catalog[i].average[j].flags &= ~flagBits;
-
-	// XXX set the secfilt bits?
-	if (nEXT && (nEXT > nPSF)) {
-	  catalog[i].average[j].flags |= ID_OBJ_EXT;
-	}
-	if (goodPS1) {
-	  catalog[i].average[j].flags |= ID_OBJ_GOOD;
-	}
-	if (Galaxy2MASS) {
-	  catalog[i].average[j].flags |= ID_OBJ_EXT_ALT;
-	  // XXX set the secfilt bits for PASS_0 or PASS_1 here?
-	}
-	if (good2MASS) {
-	  catalog[i].average[j].flags |= ID_OBJ_GOOD_ALT;
-	  // XXX set the secfilt bits for PASS_0 or PASS_1 here?
-	}
+	// 2MASS data J-band flags
+	if (catalog[i].measure[m].photcode == 2011) {
+	  // only need to do this once (always have JHK triplet; galaxy flag is same for all 3)
+	  have2MASS = TRUE;
+	  if (catalog[i].measure[m].photFlags & 0x00c00000) {
+	    Galaxy2MASS = TRUE;
+	  }
+	  if (catalog[i].measure[m].photFlags & 0x00000007) {
+	    good2MASS = TRUE; 
+	    if (Nsec_J > -1) catalog[i].secfilt[j*Nsecfilt + Nsec_J].flags |= ID_PHOTOM_PASS_0;
+	  } else {
+	    if (Nsec_J > -1) catalog[i].secfilt[j*Nsecfilt + Nsec_J].flags |= ID_PHOTOM_PASS_1;
+	  }
+	}  
+	// 2MASS data H-band flags
+	if (catalog[i].measure[m].photcode == 2012) {
+	  if (catalog[i].measure[m].photFlags & 0x00000007) {
+	    good2MASS = TRUE; 
+	    if (Nsec_H > -1) catalog[i].secfilt[j*Nsecfilt + Nsec_H].flags |= ID_PHOTOM_PASS_0;
+	  } else {
+	    if (Nsec_H > -1) catalog[i].secfilt[j*Nsecfilt + Nsec_H].flags |= ID_PHOTOM_PASS_1;
+	  }
+	}  
+	// 2MASS data K-band flags
+	if (catalog[i].measure[m].photcode == 2013) {
+	  if (catalog[i].measure[m].photFlags & 0x00000007) {
+	    good2MASS = TRUE; 
+	    if (Nsec_K > -1) catalog[i].secfilt[j*Nsecfilt + Nsec_K].flags |= ID_PHOTOM_PASS_0;
+	  } else {
+	    if (Nsec_K > -1) catalog[i].secfilt[j*Nsecfilt + Nsec_K].flags |= ID_PHOTOM_PASS_1;
+	  }
+	}  
+      }
+
+      // we attempt to set a few flags here; reset those bits before trying:
+      catalog[i].average[j].flags &= ~flagBits;
+
+      // XXX set the secfilt bits?
+      if (nEXT && (nEXT > nPSF)) {
+	catalog[i].average[j].flags |= ID_OBJ_EXT;
+      }
+      if (goodPS1) {
+	catalog[i].average[j].flags |= ID_OBJ_GOOD;
+      }
+      if (Galaxy2MASS) {
+	catalog[i].average[j].flags |= ID_OBJ_EXT_ALT;
+	if (Nsec_J > -1) catalog[i].secfilt[j*Nsecfilt + Nsec_J].flags |= ID_SECF_OBJ_EXT;
+	if (Nsec_H > -1) catalog[i].secfilt[j*Nsecfilt + Nsec_H].flags |= ID_SECF_OBJ_EXT;
+	if (Nsec_K > -1) catalog[i].secfilt[j*Nsecfilt + Nsec_K].flags |= ID_SECF_OBJ_EXT;
+      }
+      if (good2MASS) {
+	catalog[i].average[j].flags |= ID_OBJ_GOOD_ALT;
       }
     }
