Index: trunk/Ohana/src/relphot/include/relphot.h
===================================================================
--- trunk/Ohana/src/relphot/include/relphot.h	(revision 41484)
+++ trunk/Ohana/src/relphot/include/relphot.h	(revision 41485)
@@ -841,2 +841,3 @@
 void SetZeroPointModes (void);
 int UseStandardOLS (ZptFitModeType mode);
+int GetActivePhotcodeIndex (int photcode);
Index: trunk/Ohana/src/relphot/src/ImageOps.c
===================================================================
--- trunk/Ohana/src/relphot/src/ImageOps.c	(revision 41484)
+++ trunk/Ohana/src/relphot/src/ImageOps.c	(revision 41485)
@@ -290,20 +290,13 @@
 void findImages (Catalog *catalog, int Ncatalog, int doImageList) {
 
-  off_t j;
-  int i, ecode, Ns, found;
-  
   int Nmatch = 0;
-  for (i = 0; i < Ncatalog; i++) {
-    for (j = 0; j < catalog[i].Nmeasure; j++) {
+  for (int i = 0; i < Ncatalog; i++) {
+    for (off_t j = 0; j < catalog[i].Nmeasure; j++) {
       catalog[i].measureT[j].myDet = FALSE; // a detetion is not mine until proven otherwise
-      ecode = GetPhotcodeEquivCodebyCode (catalog[i].measureT[j].photcode);
 
       // skip measurements which do not match one of the requested photcodes (
       // (do we not already exclude in bcatalog -- maybe needed for reload_objects?
-      found = FALSE;
-      for (Ns = 0; !found && (Ns < Nphotcodes); Ns++) {
-	if (ecode == photcodes[Ns][0].code) found = TRUE;
-      }
-      if (!found) continue; 
+      int Ns = GetActivePhotcodeIndex (catalog[i].measureT[j].photcode);
+      if (Ns < 0) continue;
 
       // if we match one of our images, myDet gets set to TRUE
@@ -317,5 +310,5 @@
 int findCCD (off_t idx, off_t meas, int cat, MeasureTiny *measure) {
 
-  int ccdnum, found, Ns, ecode;
+  int ccdnum;
   double X, Y;
   char *pname, *filter, *p, base[256];
@@ -324,11 +317,8 @@
   pname = GetPhotcodeNamebyCode (image[idx].photcode);
 
-  // XXX this seems quite terrible...
-  ecode = GetPhotcodeEquivCodebyCode (measure[0].photcode);
-  found = FALSE;
-  for (Ns = 0; !found && (Ns < Nphotcodes); Ns++) {
-    if (ecode == photcodes[Ns][0].code) found = TRUE;
-  }
-  if (!found) return (FALSE); 
+  // skip measurements which do not match one of the requested photcodes (
+  // (do we not already exclude in bcatalog -- maybe needed for reload_objects?
+  int Ns = GetActivePhotcodeIndex (measure[0].photcode);
+  if (Ns < 0) return FALSE;
 
   filter = photcodes[Ns][0].name;
Index: trunk/Ohana/src/relphot/src/MosaicOps.c
===================================================================
--- trunk/Ohana/src/relphot/src/MosaicOps.c	(revision 41484)
+++ trunk/Ohana/src/relphot/src/MosaicOps.c	(revision 41485)
@@ -856,7 +856,4 @@
 
 int findMosaics (Catalog *catalog, int Ncatalog, int doMosaicList) {
-  
-  int i, ecode, found, Ns;
-  off_t j;
 
   if (!MOSAIC_ZEROPT) return (FALSE);
@@ -864,6 +861,6 @@
 
   int Nmatch = 0;
-  for (i = 0; i < Ncatalog; i++) {
-    for (j = 0; j < catalog[i].Nmeasure; j++) {
+  for (int i = 0; i < Ncatalog; i++) {
+    for (off_t j = 0; j < catalog[i].Nmeasure; j++) {
       catalog[i].measureT[j].myDet = FALSE; // a detetion is not mine until proven otherwise
 
@@ -872,10 +869,7 @@
 	if (catalog[i].measureT[j].t > TSTOP) continue;
       }
-      ecode = GetPhotcodeEquivCodebyCode (catalog[i].measureT[j].photcode);
-      found = FALSE;
-      for (Ns = 0; !found && (Ns < Nphotcodes); Ns++) {
-	if (ecode == photcodes[Ns][0].code) found = TRUE;
-      }
-      if (!found) continue;
+
+      int Ns = GetActivePhotcodeIndex (catalog[i].measureT[j].photcode);
+      if (Ns < 0) continue;
       matchMosaics (catalog, j, i, doMosaicList);
       Nmatch ++;
@@ -1598,4 +1592,9 @@
 
 	// skip unused measurements
+
+	// XXX replace with:
+	// Ns = GetActivePhotcodeIndex (photcode);
+	// if (Ns < 0) continue;
+
 	int Nsec;
 	int ecode = GetPhotcodeEquivCodebyCode (catalog[i].measureT[m].photcode);
@@ -1620,4 +1619,5 @@
 	if (nMos == -1) continue;
 
+	// XXX how can this not be true?  
 	if (mosaic[nMos].photcode != ecode) {
 	  fprintf (stderr, "*");
Index: trunk/Ohana/src/relphot/src/TGroupOps.c
===================================================================
--- trunk/Ohana/src/relphot/src/TGroupOps.c	(revision 41484)
+++ trunk/Ohana/src/relphot/src/TGroupOps.c	(revision 41485)
@@ -67,4 +67,7 @@
     tgroupTimes[i][0].start = tgroupTimesRaw[i];
     tgroupTimes[i][0].stop  = tgroupTimesRaw[i] + 86399;
+
+    // One TGroup per *active* photcode.  Check if photcode is active with:
+    // GetActivePhotcodeIndex (photcode) (returns -1 if not active)
 
     ALLOCATE (tgroupTimes[i][0].byCode, TGroup, Nphotcodes);
@@ -198,4 +201,7 @@
     }
 
+    // skip inactive photcodes (do not assign to a TGroup)
+    if (GetActivePhotcodeIndex (subset[i].photcode) < 0) continue;
+    
     TGroup *myGroup = findTGroup(subset[i].tzero, subset[i].photcode);
     if (!myGroup) {
@@ -358,12 +364,6 @@
 TGroup *findTGroup (unsigned int start, int photcode) {
 
-  // first find the matching photcode (our list of active secondary photcodes
-  // does not necessarily match the list of all secondary photcodes)
-  int Ns = -1;
-  int ecode = GetPhotcodeEquivCodebyCode (photcode);
-  for (int i = 0; (Ns < 0) && (i < Nphotcodes); i++) {
-    if (photcodes[i][0].code != ecode) continue;
-    Ns = i;
-  }
+  // find index of matching photcode from our list of active secondary photcodes
+  int Ns = GetActivePhotcodeIndex (photcode);
   if (Ns < 0) return NULL;
 
@@ -519,13 +519,10 @@
 int findTGroups (Catalog *catalog, int Ncatalog) {
   
-  int i, ecode, found, Ns;
-  off_t j;
-
   if (!TGROUP_ZEROPT) return (FALSE);
   // if we are calibrating by tgroup, redefine myDet == on one of my tgroups
 
   int Nmatch = 0;
-  for (i = 0; i < Ncatalog; i++) {
-    for (j = 0; j < catalog[i].Nmeasure; j++) {
+  for (int i = 0; i < Ncatalog; i++) {
+    for (off_t j = 0; j < catalog[i].Nmeasure; j++) {
       catalog[i].measureT[j].myDet = FALSE; // a detetion is not mine until proven otherwise
 
@@ -535,10 +532,7 @@
       }
 
-      ecode = GetPhotcodeEquivCodebyCode (catalog[i].measureT[j].photcode);
-      found = FALSE;
-      for (Ns = 0; !found && (Ns < Nphotcodes); Ns++) {
-	if (ecode == photcodes[Ns][0].code) found = TRUE;
-      }
-      if (!found) continue;
+      // check if this measure has a relevant photcode 
+      int Ns = GetActivePhotcodeIndex (catalog[i].measureT[j].photcode);
+      if (Ns < 0) continue;
 
       matchTGroups (catalog, j, i);
@@ -549,5 +543,5 @@
 
   // XXX print results for testing
-  for (i = 0; FALSE && (i < NtgroupTimes); i++) {
+  for (int i = 0; FALSE && (i < NtgroupTimes); i++) {
     TGroup *tgroup = tgroupTimes[i][0].byCode;
     for (off_t j = 0; j < tgroupTimes[i][0].nCode; j++) {
@@ -909,4 +903,5 @@
     float MsysKron = PhotSysTiny (&catalog[c].measureT[m], &catalog[c].averageT[n], &catalog[c].secfilt[n*Nsecfilt], MAG_CLASS_KRON);
 
+    // XXX byCode : here we are only checking for equiv photcodes (not active photcodes)
     PhotCode *code = GetPhotcodebyCode (catalog[c].measureT[m].photcode);
     if (!code) goto skip;
Index: trunk/Ohana/src/relphot/src/args.c
===================================================================
--- trunk/Ohana/src/relphot/src/args.c	(revision 41484)
+++ trunk/Ohana/src/relphot/src/args.c	(revision 41485)
@@ -537,4 +537,5 @@
   }
 
+  // (photcodes, Nphotcodes) is the list of active average photcodes
   PhotcodeList = NULL;
   photcodes = NULL;
@@ -569,4 +570,20 @@
 }
 
+// for the given measurement photcode, find the sequence number of
+// the equivalent active photcode (-1 if none match)
+// XXX this could be optimized by making a lookup table of active equiv codes
+int GetActivePhotcodeIndex (int photcode) {
+
+  // first find the matching photcode (our list of active secondary photcodes
+  // does not necessarily match the list of all secondary photcodes)
+  int Ns = -1;
+  int ecode = GetPhotcodeEquivCodebyCode (photcode);
+  for (int i = 0; (Ns < 0) && (i < Nphotcodes); i++) {
+    if (photcodes[i][0].code != ecode) continue;
+    Ns = i;
+  }
+  return Ns;
+}
+
 // XXX need to free sky
 void relphot_free (SkyTable *sky, SkyList *skylist) {
Index: trunk/Ohana/src/relphot/src/assign_images.c
===================================================================
--- trunk/Ohana/src/relphot/src/assign_images.c	(revision 41484)
+++ trunk/Ohana/src/relphot/src/assign_images.c	(revision 41485)
@@ -59,5 +59,4 @@
 int select_images_hostregion (RegionHostTable *regionHosts, Image *image, off_t Nimage) {
 
-  int ecode, found;
   off_t i, j;
 
@@ -73,11 +72,6 @@
     
     /* exclude images by photcode */
-    ecode = GetPhotcodeEquivCodebyCode (image[j].photcode);
-    found = FALSE;
-    int Ns;
-    for (Ns = 0; !found && (Ns < Nphotcodes); Ns++) {
-      if (ecode == photcodes[Ns][0].code) found = TRUE;
-    }
-    if (!found) continue;
+    int Ns = GetActivePhotcodeIndex (image[j].photcode);
+    if (Ns < 0) continue;
 
     /* exclude images by time */
Index: trunk/Ohana/src/relphot/src/select_images.c
===================================================================
--- trunk/Ohana/src/relphot/src/select_images.c	(revision 41484)
+++ trunk/Ohana/src/relphot/src/select_images.c	(revision 41485)
@@ -22,5 +22,5 @@
   off_t i, j, k, m, nStart, iSky, nimage, NIMAGE, D_NIMAGE;
   off_t *line_number;
-  int InRange, ecode, found;
+  int InRange, found;
   double Ri[5], Di[5], Xi[5], Yi[5];
   Coords tcoords;
@@ -106,13 +106,8 @@
     // goto found_it;
 
-    /* exclude images by photcode */
+    /* exclude images by photcode (we are not doing this, but I'm not sure why) */
     if (FALSE) {
-      ecode = GetPhotcodeEquivCodebyCode (timage[i].photcode);
-      found = FALSE;
-      int Ns;
-      for (Ns = 0; !found && (Ns < Nphotcodes); Ns++) {
-	if (ecode == photcodes[Ns][0].code) found = TRUE;
-      }
-      if (!found) continue;
+      int Ns = GetActivePhotcodeIndex (timage[i].photcode);
+      if (Ns < 0) continue;
     }
 
Index: trunk/Ohana/src/relphot/src/setExclusions.c
===================================================================
--- trunk/Ohana/src/relphot/src/setExclusions.c	(revision 41484)
+++ trunk/Ohana/src/relphot/src/setExclusions.c	(revision 41485)
@@ -9,5 +9,4 @@
 
   off_t i, j, k, m, Narea, Nnocal, Ngood;
-  int ecode, found, Ns;
   Coords *coords;
   double r, d, x, y;
@@ -20,10 +19,6 @@
 
 	/* select measurements by photcode */
-	ecode = GetPhotcodeEquivCodebyCode (catalog[i].measureT[m].photcode);
-	found = FALSE;
-	for (Ns = 0; !found && (Ns < Nphotcodes); Ns++) {
-	    if (ecode == photcodes[Ns][0].code) found = TRUE;
-	}
-	if (!found) goto mark_nocal;
+	int Ns = GetActivePhotcodeIndex (catalog[i].measureT[m].photcode);
+	if (Ns < 0) goto mark_nocal;
 	
 	/* select measurements by time */
