Index: trunk/Ohana/src/relphot/include/relphot.h
===================================================================
--- trunk/Ohana/src/relphot/include/relphot.h	(revision 41463)
+++ trunk/Ohana/src/relphot/include/relphot.h	(revision 41467)
@@ -28,4 +28,10 @@
   int NINDEX;
 } myIndexType;
+
+typedef enum {
+  ZPT_TGROUP,
+  ZPT_MOSAIC,
+  ZPT_IMAGES,
+} ZptFitModeType;
 
 typedef enum {
@@ -791,4 +797,5 @@
 void relphot_free (SkyTable *sky, SkyList *skylist);
 void freeImages (char *dbImagePtr);
+void freeMosaics (void);
 void relphot_client_free (SkyTable *sky, SkyList *skylist);
 void BrightCatalogFree (BrightCatalog *bcatalog);
@@ -828,2 +835,3 @@
 
 void SetZeroPointModes (int nloop);
+int UseStandardOLS (int nloop, ZptFitModeType mode);
Index: trunk/Ohana/src/relphot/src/ImageOps.c
===================================================================
--- trunk/Ohana/src/relphot/src/ImageOps.c	(revision 41463)
+++ trunk/Ohana/src/relphot/src/ImageOps.c	(revision 41467)
@@ -610,10 +610,10 @@
 
   // until the analysis has converged a bit, do not use the IRLS analysis
-  if (Nloop < 3) {
+  // default is MaxIterations = 10
+  if (UseStandardOLS(Nloop, ZPT_IMAGES)) {
     brightStars.MaxIterations = 0;
     kronStars.MaxIterations = 0;
     psfStars.MaxIterations = 0;
   }
-
 
   Nfew = Nbad = Nmos = Ngrp = Ngrid = Nrel = Nsys = 0;
@@ -635,10 +635,12 @@
     }      
 
-    // if requested, freeze mosaic chips:
-    if (FREEZE_IMAGES && isMosaicChip (image[i].photcode)) continue;
     if (image[i].photcode == 0) continue; // skip the PHU images
 
     int badNight  = (image[i].flags & ID_IMAGE_NIGHT_POOR);
     int badMosaic = (image[i].flags & ID_IMAGE_MOSAIC_POOR);
+    int isMosaic  = isMosaicChip(image[i].photcode);
+
+    // if requested, freeze mosaic chips:
+    if (FREEZE_IMAGES && isMosaic) continue;
 
     // in BAD_NIGHT mode, we fit ONLY images in bad nights
@@ -650,10 +652,10 @@
     if ((IMAGE_ZPT_MODE == IMAGE_ZPT_MODE_BAD_NIGHT_BAD_MOSAIC)) {
       if (!badNight) continue;
-      if (!badMosaic) continue;
+      if (!badMosaic && isMosaic) continue; // skip good mosaics (but not good images)
     }
 
     // in BAD_MOSAIC mode, we fit bad mosaics ignoring night state
     if ((IMAGE_ZPT_MODE == IMAGE_ZPT_MODE_BAD_MOSAIC)) {
-      if (badMosaic) continue;
+      if (badMosaic && isMosaic) continue; // skip bad mosaics (but not bad images)
     }
 
Index: trunk/Ohana/src/relphot/src/MosaicOps.c
===================================================================
--- trunk/Ohana/src/relphot/src/MosaicOps.c	(revision 41463)
+++ trunk/Ohana/src/relphot/src/MosaicOps.c	(revision 41467)
@@ -321,9 +321,4 @@
   fprintf (stderr, "matched %d images to %d mosaics, %d simple chips not matched to mosaics\n", (int) (Nsubset - Nsimple), (int) Nmosaic, (int) Nsimple);
   return;
-}
-
-void freeMosaics () {
-  
-
 }
 
@@ -845,4 +840,18 @@
 }
 
+void freeMosaics () {
+  
+  if (!MOSAIC_ZEROPT) return;
+
+  for (int i = 0; i < Nmosaic; i++) {
+    FREE (MosaicToImage[i]);
+  }
+  
+  FREE (mosaic);
+  FREE (MosaicToImage);
+  FREE (MosaicN_Image);
+  FREE (ImageToMosaic); // mosaic to which image belongs
+}
+
 int findMosaics (Catalog *catalog, int Ncatalog, int doMosaicList) {
   
@@ -1007,5 +1016,5 @@
 
     // until the analysis has converged a bit, do not use the IRLS analysis
-    if (Nloop < 3) {
+    if (UseStandardOLS(Nloop, ZPT_MOSAIC)) {
       info->brightStars.MaxIterations = 0;
       info->kronStars.MaxIterations = 0;
@@ -1133,4 +1142,6 @@
   int badNight  = (myMosaic[0].flags & ID_IMAGE_NIGHT_POOR);
   int badMosaic = (myMosaic[0].flags & ID_IMAGE_MOSAIC_POOR);
+
+  myMosaic->flags &= ~ID_IMAGE_MOSAIC_PHOTCAL;  // unset this flag
 
   // if we are fitting TGroup zero points, when we identify the bad TGroups, we proceed
@@ -1356,4 +1367,6 @@
   }
 
+  myMosaic->flags |= ID_IMAGE_MOSAIC_PHOTCAL;  // set this flag
+
   if (testImage) {
     fprintf (stderr, "%f %f  :  %f\n", myMosaic[0].McalPSF, myMosaic[0].dMsys, myMosaic[0].McalChiSq);
@@ -1371,4 +1384,7 @@
   }
   
+  fprintf (stderr, "MOSAIC time %.6f photcode %d Mcal: %f, dMcal: %f, chisq: %f, %d of %d\n",
+	   ohana_sec_to_mjd(myMosaic[0].start), myMosaic[0].photcode, myMosaic[0].McalPSF, myMosaic[0].dMcal, myMosaic[0].McalChiSq, myMosaic[0].nFitPhotom, myMosaic[0].nValPhotom);
+
   return TRUE;
 }
@@ -1862,5 +1878,6 @@
 
   for (i = N = 0; i < Nmosaic; i++) {
-    if (mosaic[i].flags & IMAGE_BAD) continue;
+    // if (mosaic[i].flags & IMAGE_BAD) continue;
+    if (!(mosaic[i].flags & ID_IMAGE_MOSAIC_PHOTCAL)) continue;
     if (mosaic[i].skipCal) continue;
     mlist[N] = mosaic[i].McalPSF;
Index: trunk/Ohana/src/relphot/src/TGroupOps.c
===================================================================
--- trunk/Ohana/src/relphot/src/TGroupOps.c	(revision 41463)
+++ trunk/Ohana/src/relphot/src/TGroupOps.c	(revision 41467)
@@ -324,4 +324,5 @@
 	Mosaic *mosaic = getMosaicForImage (imageIdx);
 
+	if (!mosaic) continue; // this is a simple chip, not part of a mosaic
 	if (mosaic->inTGroup) continue;
 
@@ -687,5 +688,5 @@
 
     // until the analysis has converged a bit, do not use the IRLS analysis
-    if (Nloop < 100) {
+    if (UseStandardOLS(Nloop, ZPT_TGROUP)) {
       info->brightStars.MaxIterations = 0;
       info->kronStars.MaxIterations = 0;
Index: trunk/Ohana/src/relphot/src/ZeroPointModes.c
===================================================================
--- trunk/Ohana/src/relphot/src/ZeroPointModes.c	(revision 41463)
+++ trunk/Ohana/src/relphot/src/ZeroPointModes.c	(revision 41467)
@@ -13,5 +13,5 @@
       clean_tgroups(); // do this on every pass to update the status of nights 
     }
-    if ((nloop > 8) && (nloop <= 12)) {
+    if ((nloop > 8) && (nloop <= 999)) {
       // after iterating a few times on the TGroup & Mosaic zero points:
       // * identify the good / bad Mosaics
@@ -26,5 +26,5 @@
 
   if (TGROUP_ZEROPT && !MOSAIC_ZEROPT) {
-    if ((nloop > 4) && (nloop <= 8)) {
+    if ((nloop > 4) && (nloop <= 999)) {
       // after iterating a few times on the TGroup zero points:
       // * identify the photometric nights
@@ -38,5 +38,5 @@
 
   if (!TGROUP_ZEROPT && MOSAIC_ZEROPT) {
-    if ((nloop > 4) && (nloop <= 8)) {
+    if ((nloop > 4) && (nloop <= 999)) {
       // after iterating a few times on the Mosaic zero points:
       // * identify the good / bad mosaics
@@ -50,5 +50,5 @@
 
   if (!TGROUP_ZEROPT && !MOSAIC_ZEROPT) {
-    if ((nloop > 4) && (nloop <= 8)) {
+    if ((nloop > 4) && (nloop <= 999)) {
       // after iterating a few times on the Image zero points:
       // * ???
@@ -60,16 +60,59 @@
 }
 
-      // if (i < NLOOP - 1) rationalize_mosaics (catalog, Ncatalog);
-      // if (i % 6 == 1) rationalize_images ();
+// mode = TGROUP, MOSAIC, IMAGE
+// should we use IRLS or OLS?  depends on nloop and which stage we are in.
+int UseStandardOLS (int nloop, ZptFitModeType mode) {
 
-      // NOTE : in the past, I was not iterating enough before cleaning.  make sure we do
-      // at least 8 loops first -- that should get the systematic errors down to the ~1%
-      // level, even in cases where we have an even split between photometric data and
-      // data with 1 mag of extinction.
+  if (TGROUP_ZEROPT && MOSAIC_ZEROPT) {
+    switch (mode) {
+      case ZPT_TGROUP: 
+	if (nloop < 3) return TRUE;
+	return FALSE;
+      case ZPT_MOSAIC:
+	if (nloop < 7) return TRUE;
+	return FALSE;
+      case ZPT_IMAGES:
+      default:
+	if (nloop < 11) return TRUE;
+	return FALSE;
+    }
+  }
 
-      // if ((i > 8) && (i % 8 == 2)) clean_measures (catalog, Ncatalog, FALSE, flatcorr); 
-      // if ((i > 8) && (i % 8 == 3)) clean_stars (catalog, Ncatalog);
-      // if ((i > 8) && (i % 8 == 5)) clean_mosaics ();
-      // if ((i > 8) && (i % 8 == 5)) clean_tgroups ();
-      // if ((i > 8) && (i % 8 == 5)) clean_images ();
+  if (TGROUP_ZEROPT && !MOSAIC_ZEROPT) {
+    switch (mode) {
+      case ZPT_TGROUP: 
+      case ZPT_MOSAIC:
+	if (nloop < 3) return TRUE;
+	return FALSE;
+      case ZPT_IMAGES:
+      default:
+	if (nloop < 7) return TRUE;
+	return FALSE;
+    }
+  }
 
+  if (!TGROUP_ZEROPT && MOSAIC_ZEROPT) {
+    switch (mode) {
+      case ZPT_TGROUP: 
+      case ZPT_MOSAIC:
+	if (nloop < 3) return TRUE;
+	return FALSE;
+      case ZPT_IMAGES:
+      default:
+	if (nloop < 7) return TRUE;
+	return FALSE;
+    }
+  }
+
+  if (!TGROUP_ZEROPT && !MOSAIC_ZEROPT) {
+    switch (mode) {
+      case ZPT_TGROUP: 
+      case ZPT_MOSAIC:
+      case ZPT_IMAGES:
+      default:
+	if (nloop < 3) return TRUE;
+	return FALSE;
+    }
+  }
+  return FALSE;
+}
Index: trunk/Ohana/src/relphot/src/extra.c
===================================================================
--- trunk/Ohana/src/relphot/src/extra.c	(revision 41463)
+++ trunk/Ohana/src/relphot/src/extra.c	(revision 41467)
@@ -14,11 +14,21 @@
 int whichGPC1filter (int photcode) {
 
-  if (((photcode > 10000) && (photcode < 10077)) || (photcode == 4100)) return PS1_g; // g-band 
-  if (((photcode > 10100) && (photcode < 10177)) || (photcode == 4200)) return PS1_r; // r-band
-  if (((photcode > 10200) && (photcode < 10277)) || (photcode == 4300)) return PS1_i; // i-band
-  if (((photcode > 10300) && (photcode < 10377)) || (photcode == 4400)) return PS1_z; // z-band
-  if (((photcode > 10400) && (photcode < 10477)) || (photcode == 4500)) return PS1_y; // y-band
-  if (((photcode > 10500) && (photcode < 10577)) || (photcode == 4600)) return PS1_w; // w-band
-
+# if (1)
+  // disable the corner chips:
+  if (((photcode >  10000) && (photcode <  10077)) || (photcode == 4100)) return PS1_g; // g-band 
+  if (((photcode >  10100) && (photcode <  10177)) || (photcode == 4200)) return PS1_r; // r-band
+  if (((photcode >  10200) && (photcode <  10277)) || (photcode == 4300)) return PS1_i; // i-band
+  if (((photcode >  10300) && (photcode <  10377)) || (photcode == 4400)) return PS1_z; // z-band
+  if (((photcode >  10400) && (photcode <  10477)) || (photcode == 4500)) return PS1_y; // y-band
+  if (((photcode >  10500) && (photcode <  10577)) || (photcode == 4600)) return PS1_w; // w-band
+# else
+  // for testing, enable the corner chips:
+  if (((photcode >= 10000) && (photcode <= 10077)) || (photcode == 4100)) return PS1_g; // g-band 
+  if (((photcode >= 10100) && (photcode <= 10177)) || (photcode == 4200)) return PS1_r; // r-band
+  if (((photcode >= 10200) && (photcode <= 10277)) || (photcode == 4300)) return PS1_i; // i-band
+  if (((photcode >= 10300) && (photcode <= 10377)) || (photcode == 4400)) return PS1_z; // z-band
+  if (((photcode >= 10400) && (photcode <= 10477)) || (photcode == 4500)) return PS1_y; // y-band
+  if (((photcode >= 10500) && (photcode <= 10577)) || (photcode == 4600)) return PS1_w; // w-band
+# endif
   return PS1_none;
 }
@@ -27,10 +37,19 @@
 int isGPC1chip (int photcode) {
 
-  if (((photcode > 10000) && (photcode < 10077)) || (photcode == 4100)) return TRUE; // g-band
-  if (((photcode > 10100) && (photcode < 10177)) || (photcode == 4200)) return TRUE; // r-band
-  if (((photcode > 10200) && (photcode < 10277)) || (photcode == 4300)) return TRUE; // i-band
-  if (((photcode > 10300) && (photcode < 10377)) || (photcode == 4400)) return TRUE; // z-band
-  if (((photcode > 10400) && (photcode < 10477)) || (photcode == 4500)) return TRUE; // y-band
-  if (((photcode > 10500) && (photcode < 10577)) || (photcode == 4600)) return TRUE; // w-band
+# if (1)
+  if (((photcode >  10000) && (photcode <  10077)) || (photcode == 4100)) return TRUE; // g-band
+  if (((photcode >  10100) && (photcode <  10177)) || (photcode == 4200)) return TRUE; // r-band
+  if (((photcode >  10200) && (photcode <  10277)) || (photcode == 4300)) return TRUE; // i-band
+  if (((photcode >  10300) && (photcode <  10377)) || (photcode == 4400)) return TRUE; // z-band
+  if (((photcode >  10400) && (photcode <  10477)) || (photcode == 4500)) return TRUE; // y-band
+  if (((photcode >  10500) && (photcode <  10577)) || (photcode == 4600)) return TRUE; // w-band
+# else
+  if (((photcode >= 10000) && (photcode <= 10077)) || (photcode == 4100)) return TRUE; // g-band
+  if (((photcode >= 10100) && (photcode <= 10177)) || (photcode == 4200)) return TRUE; // r-band
+  if (((photcode >= 10200) && (photcode <= 10277)) || (photcode == 4300)) return TRUE; // i-band
+  if (((photcode >= 10300) && (photcode <= 10377)) || (photcode == 4400)) return TRUE; // z-band
+  if (((photcode >= 10400) && (photcode <= 10477)) || (photcode == 4500)) return TRUE; // y-band
+  if (((photcode >= 10500) && (photcode <= 10577)) || (photcode == 4600)) return TRUE; // w-band
+# endif
 
   return FALSE;
Index: trunk/Ohana/src/relphot/src/relphot_images.c
===================================================================
--- trunk/Ohana/src/relphot/src/relphot_images.c	(revision 41463)
+++ trunk/Ohana/src/relphot/src/relphot_images.c	(revision 41467)
@@ -279,4 +279,5 @@
 
   freeImages (db.ftable.buffer);
+  freeMosaics ();
   gfits_db_free (&db);
 
Index: trunk/Ohana/src/relphot/test/relphot.tgroups.dvo
===================================================================
--- trunk/Ohana/src/relphot/test/relphot.tgroups.dvo	(revision 41463)
+++ trunk/Ohana/src/relphot/test/relphot.tgroups.dvo	(revision 41467)
@@ -484,2 +484,23 @@
   end
 end
+
+macro plot.tcat.data
+  if ($0 != 2)
+    echo "USAGE: plot.tcat.data (Nlast)"
+    break
+  end
+
+  $BSTYLE = -tickpad 0.2 +xpad 0.2 +ypad 0.2 -ypad 5
+
+  for i 0 $1
+    sprintf name tcat.%02d.dat $i
+    data $name
+    dev -n $i
+    resize 900 500
+    clear;
+    box $BSTYLE
+    read Msys 4 Mrel 5 Mcal 7 Mmos 8 Mgrp 9 airmass 11 dMag 12;
+    lim Msys dMag; plot Msys dMag -pt cir -op 0.2 -c blue
+    echo $name
+  end
+end
