Index: /trunk/Ohana/src/relphot/include/relphot.h
===================================================================
--- /trunk/Ohana/src/relphot/include/relphot.h	(revision 41472)
+++ /trunk/Ohana/src/relphot/include/relphot.h	(revision 41473)
@@ -30,4 +30,5 @@
 
 typedef enum {
+  ZPT_STARS,
   ZPT_TGROUP,
   ZPT_MOSAIC,
@@ -458,5 +459,6 @@
 // int            PhotNsec;
 
-PhotCode      *refPhotcode;
+PhotCode *refPhotcode;
+int       USE_REFERENCE_WEIGHT;
 
 int MaxDensityUse;
@@ -633,7 +635,7 @@
 int           setMcalOutput       PROTO((Catalog *catalog, int Ncatalog, FlatCorrectionTable *flatcorr));
 
-void          setMcal             PROTO((Catalog *catalog, int Nloop, int Poor, FlatCorrectionTable *flatcorr));
-int           setMmos             PROTO((Catalog *catalog, int Nloop, int Poor, FlatCorrectionTable *flatcorr));
-int           setMgrp             PROTO((Catalog *catalog, int Nloop, int Poor, FlatCorrectionTable *flatcorr));
+void          setMcal             PROTO((Catalog *catalog, int Poor, FlatCorrectionTable *flatcorr));
+int           setMmos             PROTO((Catalog *catalog, int Poor, FlatCorrectionTable *flatcorr));
+int           setMgrp             PROTO((Catalog *catalog, int Poor, FlatCorrectionTable *flatcorr));
 
 int           setMrel             PROTO((Catalog *catalog, int Ncatalog, FlatCorrectionTable *flatcorr));
@@ -834,4 +836,7 @@
 void dump_catalog (Catalog *catalog, off_t c, int Npass);
 
-void SetZeroPointModes (int nloop);
-int UseStandardOLS (int nloop, ZptFitModeType mode);
+void SetZptIteration (int current);
+int GetZptIteration (void);
+
+void SetZeroPointModes (void);
+int UseStandardOLS (ZptFitModeType mode);
Index: /trunk/Ohana/src/relphot/src/ImageOps.c
===================================================================
--- /trunk/Ohana/src/relphot/src/ImageOps.c	(revision 41472)
+++ /trunk/Ohana/src/relphot/src/ImageOps.c	(revision 41473)
@@ -581,5 +581,5 @@
 
 /* determine Mcal values for all images */
-void setMcal (Catalog *catalog, int Nloop, int PoorImages, FlatCorrectionTable *flatcorr) {
+void setMcal (Catalog *catalog, int PoorImages, FlatCorrectionTable *flatcorr) {
 
   off_t i, j, m, c, n;
@@ -611,5 +611,5 @@
   // until the analysis has converged a bit, do not use the IRLS analysis
   // default is MaxIterations = 10
-  if (UseStandardOLS(Nloop, ZPT_IMAGES)) {
+  if (UseStandardOLS(ZPT_IMAGES)) {
     brightStars.MaxIterations = 0;
     kronStars.MaxIterations = 0;
@@ -799,6 +799,6 @@
 
     // bright end scatter
-    // fit1d_irls (&brightStars, Nbright);
-    // image[i].dMagSys = brightStars.sigma;
+    fit1d_irls (&brightStars, Nbright);
+    image[i].dMagSys = brightStars.sigma;
 
     if (image[i].McalPSF < -CLOUD_TOLERANCE) {
Index: /trunk/Ohana/src/relphot/src/MosaicOps.c
===================================================================
--- /trunk/Ohana/src/relphot/src/MosaicOps.c	(revision 41472)
+++ /trunk/Ohana/src/relphot/src/MosaicOps.c	(revision 41473)
@@ -306,5 +306,5 @@
     mosaic[j].nValPhotom = 0;
 
-    mosaic[j].dMsys     = subset[i].flags;
+    mosaic[j].dMsys     = subset[i].dMagSys;
     mosaic[j].flags     = subset[i].flags;
     mosaic[j].secz      = subset[i].secz;
@@ -446,5 +446,5 @@
     mosaic[j].dMcal     = 0.0;
     mosaic[j].McalChiSq = 0.0;
-    mosaic[j].dMsys     = image[i].flags;
+    mosaic[j].dMsys     = image[i].dMagSys;
     mosaic[j].flags     = image[i].flags;
     mosaic[j].secz      = image[i].secz;
@@ -743,4 +743,5 @@
       image[im].flags 	   |= (mosaic[i].flags & ID_IMAGE_PHOTOM_FEW);
       image[im].flags 	   |= (mosaic[i].flags & ID_IMAGE_PHOTOM_POOR);
+      image[im].flags 	   |= (mosaic[i].flags & ID_IMAGE_MOSAIC_POOR);
     }
     mosaic[i].McalPSF  = 0.0;
@@ -973,5 +974,4 @@
   int Nsys;
   int Nskip;
-  int Nloop;
   off_t Nmax;
   int PoorImages;
@@ -994,7 +994,7 @@
 int setMmos_mosaic (Mosaic *mosaic, off_t Nmos, Image *image, Catalog *catalog, SetMmosInfo *info, FlatCorrectionTable *flatcorr);
 void *setMmos_worker (void *data);
-int setMmos_threaded (Catalog *catalog, int Nloop, int PoorImages, FlatCorrectionTable *flatcorr);
-
-void SetMmosInfoInit (SetMmosInfo *info, off_t Nmax, int allocLists, int PoorImages, int Nloop) {
+int setMmos_threaded (Catalog *catalog, int PoorImages, FlatCorrectionTable *flatcorr);
+
+void SetMmosInfoInit (SetMmosInfo *info, off_t Nmax, int allocLists, int PoorImages) {
   info->Nfew = 0;
   info->Nbad = 0;
@@ -1006,5 +1006,4 @@
 
   info->Nmax = Nmax;
-  info->Nloop = Nloop;
   info->PoorImages = PoorImages;
 
@@ -1016,5 +1015,5 @@
 
     // until the analysis has converged a bit, do not use the IRLS analysis
-    if (UseStandardOLS(Nloop, ZPT_MOSAIC)) {
+    if (UseStandardOLS(ZPT_MOSAIC)) {
       info->brightStars.MaxIterations = 0;
       info->kronStars.MaxIterations = 0;
@@ -1062,5 +1061,5 @@
 }
 
-int setMmos (Catalog *catalog, int Nloop, int PoorImages, FlatCorrectionTable *flatcorr) {
+int setMmos (Catalog *catalog, int PoorImages, FlatCorrectionTable *flatcorr) {
 
   off_t i, N, Nmax;
@@ -1075,5 +1074,5 @@
   // so do not run setMmos in threaded mode if PLOTSTUFF is set
   if (NTHREADS && !PLOTSTUFF) {
-    int status = setMmos_threaded (catalog, Nloop, PoorImages, flatcorr);
+    int status = setMmos_threaded (catalog, PoorImages, flatcorr);
     return status;
   }
@@ -1095,5 +1094,5 @@
 
   SetMmosInfo info;
-  SetMmosInfoInit (&info, Nmax, TRUE, PoorImages, Nloop);
+  SetMmosInfoInit (&info, Nmax, TRUE, PoorImages);
 
   // int savePlotDelay = PLOTDELAY;
@@ -1338,5 +1337,5 @@
 
   fit1d_irls (psfStars, N);
-  // if (VERBOSE2 && info->PoorImages) fprintf (stderr, "Mmos: %f %f %d %d\n", stats.mean, stats.sigma, stats.Nmeas, N);
+  fit1d_irls (brightStars, Nbright);
 
   // for now, I have no reason to measure these separately for camera-level images
@@ -1347,6 +1346,6 @@
   myMosaic[0].nValPhotom = N;	    
 
-  // fit1d_irls (kronStars, N);    // does it make sense to calculate a zero point based kron mags?
-  // myMosaic->McalAPER   = ???
+  fit1d_irls (kronStars, N);
+  myMosaic->McalAPER   = kronStars->bSaveArray[0][0];
 
   if (testImage) {
@@ -1360,6 +1359,6 @@
 
   // bright end scatter
-  // fit1d_irls (brightStars, N);  // does it make sense to calculate a separate bright star zero point?
-  // myMosaic->dMsys = ??? 
+  fit1d_irls (brightStars, Nbright);
+  myMosaic->dMsys = brightStars->sigma; // stdev of bright star mags
 
   if (myMosaic[0].McalPSF < -CLOUD_TOLERANCE) {
@@ -1381,5 +1380,4 @@
     off_t im = MosaicToImage[Nmos][j];
     image[im].ubercalDist = myMosaic[0].ubercalDist;
-    // fprintf (stderr, "%d %d %d\n", (int) i, (int) im, image[im].ubercalDist);
   }
   
@@ -1390,5 +1388,5 @@
 }
   
-int setMmos_threaded (Catalog *catalog, int Nloop, int PoorImages, FlatCorrectionTable *flatcorr) {
+int setMmos_threaded (Catalog *catalog, int PoorImages, FlatCorrectionTable *flatcorr) {
 
   int i;
@@ -1411,5 +1409,5 @@
 
   SetMmosInfo summary;
-  SetMmosInfoInit (&summary, Nmax, FALSE, PoorImages, Nloop);
+  SetMmosInfoInit (&summary, Nmax, FALSE, PoorImages);
 
   pthread_attr_t attr;
@@ -1437,5 +1435,5 @@
     // we do NOT allocate the arrays here, we only supply basic info (Nmax, Nloop,
     // PoorImages) used in the threads to allocate the arrays and set the MaxIterations
-    SetMmosInfoInit (&threadinfo[i].info, Nmax, FALSE, PoorImages, Nloop);
+    SetMmosInfoInit (&threadinfo[i].info, Nmax, FALSE, PoorImages);
     pthread_create (&threads[i], NULL, setMmos_worker, &threadinfo[i]);
   }
@@ -1495,5 +1493,5 @@
 
   SetMmosInfo results;
-  SetMmosInfoInit (&results, threadinfo->info.Nmax, TRUE, threadinfo->info.PoorImages, threadinfo->info.Nloop);
+  SetMmosInfoInit (&results, threadinfo->info.Nmax, TRUE, threadinfo->info.PoorImages);
 
   while (1) {
Index: /trunk/Ohana/src/relphot/src/StarOps.c
===================================================================
--- /trunk/Ohana/src/relphot/src/StarOps.c	(revision 41472)
+++ /trunk/Ohana/src/relphot/src/StarOps.c	(revision 41473)
@@ -1084,4 +1084,6 @@
 void dump_catalog (Catalog *catalog, off_t c, int Npass) {
 
+  return;
+
   int Nsecfilt = GetPhotcodeNsecfilt ();
 
Index: /trunk/Ohana/src/relphot/src/TGroupOps.c
===================================================================
--- /trunk/Ohana/src/relphot/src/TGroupOps.c	(revision 41472)
+++ /trunk/Ohana/src/relphot/src/TGroupOps.c	(revision 41473)
@@ -427,11 +427,11 @@
 	image[im].dMagSys     = tgroup[i].dMsys;
 	image[im].nFitPhotom  = tgroup[i].nFitPhotom;
-	image[im].flags 	   |= (tgroup[i].flags & ID_IMAGE_NIGHT_POOR); // XXX copy this down?
-	image[im].flags 	   |= (tgroup[i].flags & ID_IMAGE_MOSAIC_POOR); // XXX copy this down?
+	image[im].flags 	   |= (tgroup[i].flags & ID_IMAGE_NIGHT_POOR); // probably not necessary : it is set in markBadTGroup
+	image[im].flags 	   |= (tgroup[i].flags & ID_IMAGE_MOSAIC_POOR); // probably not necessary : it is set in markBadTGroup
 	image[im].flags 	   |= (tgroup[i].flags & ID_IMAGE_PHOTOM_FEW);
 	image[im].flags 	   |= (tgroup[i].flags & ID_IMAGE_PHOTOM_POOR);
 	image[im].flags 	   |= (tgroup[i].flags & ID_IMAGE_PHOTOM_UBERCAL);
 	
-	fprintf (stderr, "TG to IMAGE: %f +/- %f -> %f (%d)\n", tgroup[i].McalPSF, tgroup[i].dMcal, image[im].McalPSF, tgroup[i].nFitPhotom);
+	// fprintf (stderr, "TG to IMAGE: %f +/- %f -> %f (%d)\n", tgroup[i].McalPSF, tgroup[i].dMcal, image[im].McalPSF, tgroup[i].nFitPhotom);
       }
       tgroup[i].McalPSF  = 0.0;
@@ -547,8 +547,10 @@
   }
   fprintf (stderr, "Matched %d detections to tgroups\n", Nmatch);
-  for (i = 0; i < NtgroupTimes; i++) {
+
+  // XXX print results for testing
+  for (i = 0; FALSE && (i < NtgroupTimes); i++) {
     TGroup *tgroup = tgroupTimes[i][0].byCode;
     for (off_t j = 0; j < tgroupTimes[i][0].nCode; j++) {
-      fprintf (stderr, "  TGROUP time %.0f photcode %d, Nmeasure: %d, Nimage: %d\n", ohana_sec_to_mjd(tgroupTimes[i][0].start), tgroup[j].photcode, (int) tgroup[j].Nmeasure, (int) tgroup[j].Nimage);
+      if (0) fprintf (stderr, "  TGROUP time %.0f photcode %d, Nmeasure: %d, Nimage: %d\n", ohana_sec_to_mjd(tgroupTimes[i][0].start), tgroup[j].photcode, (int) tgroup[j].Nmeasure, (int) tgroup[j].Nimage);
     }
   }
@@ -643,5 +645,4 @@
   int Nsys;
   int Nskip;
-  int Nloop;
   off_t Nmax;
   int PoorImages;
@@ -664,7 +665,7 @@
 int   setMgrp_tgroup (TGroup *tgroup, off_t Nmos, Image *image, Catalog *catalog, SetMgrpInfo *info, FlatCorrectionTable *flatcorr);
 void *setMgrp_worker (void *data);
-int   setMgrp_threaded (Catalog *catalog, int Nloop, int PoorImages, FlatCorrectionTable *flatcorr);
-
-void SetMgrpInfoInit (SetMgrpInfo *info, off_t Nmax, int allocLists, int PoorImages, int Nloop) {
+int   setMgrp_threaded (Catalog *catalog, int PoorImages, FlatCorrectionTable *flatcorr);
+
+void SetMgrpInfoInit (SetMgrpInfo *info, off_t Nmax, int allocLists, int PoorImages) {
   info->Nfew = 0;
   info->Nbad = 0;
@@ -677,5 +678,4 @@
 
   info->Nmax = Nmax;
-  info->Nloop = Nloop;
   info->PoorImages = PoorImages;
 
@@ -688,5 +688,5 @@
 
     // until the analysis has converged a bit, do not use the IRLS analysis
-    if (UseStandardOLS(Nloop, ZPT_TGROUP)) {
+    if (UseStandardOLS(ZPT_TGROUP)) {
       info->brightStars.MaxIterations = 0;
       info->kronStars.MaxIterations = 0;
@@ -740,5 +740,5 @@
 }
 
-int setMgrp (Catalog *catalog, int Nloop, int PoorImages, FlatCorrectionTable *flatcorr) {
+int setMgrp (Catalog *catalog, int PoorImages, FlatCorrectionTable *flatcorr) {
 
   off_t N;
@@ -751,5 +751,5 @@
   // so do not run setMgrp in threaded mode if PLOTSTUFF is set
   if (NTHREADS && !PLOTSTUFF) {
-    int status = setMgrp_threaded (catalog, Nloop, PoorImages, flatcorr);
+    int status = setMgrp_threaded (catalog, PoorImages, flatcorr);
     return status;
   }
@@ -766,5 +766,5 @@
 
   SetMgrpInfo info;
-  SetMgrpInfoInit (&info, Nmax, TRUE, PoorImages, Nloop);
+  SetMgrpInfoInit (&info, Nmax, TRUE, PoorImages);
 
   for (off_t i = 0; i < NtgroupTimes; i++) {
@@ -772,5 +772,5 @@
     for (off_t j = 0; j < tgroupTimes[i][0].nCode; j++) {
       setMgrp_tgroup (&tgroup[j], i, image, catalog, &info, flatcorr);
-      fprintf (stderr, "TGROUP time %.0f photcode %d Mcal: %f, dK: %f, dMcal: %f, sigma: %f, chisq: %f, %d of %d, limiting negative clouds to %f\n", ohana_sec_to_mjd(tgroupTimes[i][0].start), tgroup[j].photcode, tgroup[j].McalPSF, tgroup[j].dKlam, tgroup[j].dMcal, tgroup[j].stdev, tgroup[j].McalChiSq, tgroup[j].nFitPhotom, tgroup[j].nValPhotom, CLOUD_TOLERANCE);
+      // fprintf (stderr, "TGROUP time %.0f photcode %d Mcal: %f, dK: %f, dMcal: %f, sigma: %f, chisq: %f, %d of %d, limiting negative clouds to %f\n", ohana_sec_to_mjd(tgroupTimes[i][0].start), tgroup[j].photcode, tgroup[j].McalPSF, tgroup[j].dKlam, tgroup[j].dMcal, tgroup[j].stdev, tgroup[j].McalChiSq, tgroup[j].nFitPhotom, tgroup[j].nValPhotom, CLOUD_TOLERANCE);
     }
   }
@@ -971,7 +971,7 @@
   myTGroup->dKlam      = psfStars->bSaveArray[1][0];
 
-  // XXX drop this for tgroups?
-  // fit1d_irls (kronStars, N);    // does it make sense to calculate a zero point based kron mags?
-  // myTGroup->McalAPER   = stats.mean;
+  // XXX this does not make sense: I need to apply the airmass slope calculated above first
+  fit1d_irls (kronStars, N);
+  myTGroup->McalAPER   = kronStars->bSaveArray[0][0];
 
   if (testImage) {
@@ -979,7 +979,4 @@
     fprintf (stderr, "test night %.0f aper: %f %f %d ... ", ohana_sec_to_mjd(parent->start), myTGroup->McalPSF, myTGroup->dMcal, myTGroup->nFitPhotom);
   }
-
-  // fprintf (stderr, "Mgrp: %6.3f +/- %6.3f %5d of %5d | %.2f %.0f\n", myTGroup->McalPSF, myTGroup->dMcal, myTGroup->nFitPhotom, myTGroup->nValPhotom, myTGroup->McalChiSq, ohana_sec_to_mjd(tgroup->start));
-
   if (PLOTSTUFF) {
     fprintf (stderr, "Mgrp: %6.3f +/- %6.3f %5d of %5d | %.0f\n", myTGroup->McalPSF, myTGroup->dMcal, myTGroup->nFitPhotom, N, ohana_sec_to_mjd(tgroup->start));
@@ -990,6 +987,6 @@
   // XXX this does not make sense: I need to apply the airmass slope calculated above first
   // redo this by calculating the corrected bright stars mags 
-  // fit1d_irls (brightStars, N);  // does it make sense to calculate a separate bright star zero point?
-  // myTGroup->dMsys = stats.sigma;
+  fit1d_irls (brightStars, Nbright);
+  myTGroup->dMsys = brightStars->sigma;
 
   // keep this??
@@ -1005,5 +1002,5 @@
 }
   
-int setMgrp_threaded (Catalog *catalog, int Nloop, int PoorImages, FlatCorrectionTable *flatcorr) {
+int setMgrp_threaded (Catalog *catalog, int PoorImages, FlatCorrectionTable *flatcorr) {
 
   int i;
@@ -1023,5 +1020,5 @@
 
   SetMgrpInfo summary;
-  SetMgrpInfoInit (&summary, Nmax, FALSE, PoorImages, Nloop);
+  SetMgrpInfoInit (&summary, Nmax, FALSE, PoorImages);
 
   pthread_attr_t attr;
@@ -1047,7 +1044,7 @@
     threadinfo[i].flatcorr = flatcorr;
 
-    // we do NOT allocate the arrays here, we only supply basic info (Nmax, Nloop,
+    // we do NOT allocate the arrays here, we only supply basic info (Nmax,
     // PoorImages) used in the threads to allocate the arrays and set the MaxIterations
-    SetMgrpInfoInit (&threadinfo[i].info, Nmax, FALSE, PoorImages, Nloop);
+    SetMgrpInfoInit (&threadinfo[i].info, Nmax, FALSE, PoorImages);
     pthread_create (&threads[i], NULL, setMgrp_worker, &threadinfo[i]);
   }
@@ -1104,5 +1101,5 @@
 
   SetMgrpInfo results;
-  SetMgrpInfoInit (&results, threadinfo->info.Nmax, TRUE, threadinfo->info.PoorImages, threadinfo->info.Nloop); // allocate list, dlist arrays here
+  SetMgrpInfoInit (&results, threadinfo->info.Nmax, TRUE, threadinfo->info.PoorImages); // allocate list, dlist arrays here
 
   while (1) {
@@ -1427,4 +1424,6 @@
 void dump_tgroups (Catalog *catalog, int Npass) {
 
+  return;
+
   int Nsecfilt = GetPhotcodeNsecfilt ();
 
Index: /trunk/Ohana/src/relphot/src/ZeroPointModes.c
===================================================================
--- /trunk/Ohana/src/relphot/src/ZeroPointModes.c	(revision 41472)
+++ /trunk/Ohana/src/relphot/src/ZeroPointModes.c	(revision 41473)
@@ -1,8 +1,18 @@
 # include "relphot.h"
 
-void SetZeroPointModes (int nloop) {
+static int CurrentLoop = -1; // track the number of iterations
+
+void SetZptIteration (int current) {
+  CurrentLoop = current;
+}
+
+int GetZptIteration (void) {
+  return CurrentLoop;
+}
+
+void SetZeroPointModes (void) {
 
   if (TGROUP_ZEROPT && MOSAIC_ZEROPT) {
-    if ((nloop > 4) && (nloop <= 8)) {
+    if ((CurrentLoop > 4) && (CurrentLoop <= 8)) {
       // after iterating a few times on the TGroup zero points:
       // * identify the photometric nights
@@ -13,5 +23,5 @@
       clean_tgroups(); // do this on every pass to update the status of nights 
     }
-    if ((nloop > 8) && (nloop <= 999)) {
+    if ((CurrentLoop > 8) && (CurrentLoop <= 999)) {
       // after iterating a few times on the TGroup & Mosaic zero points:
       // * identify the good / bad Mosaics
@@ -26,5 +36,5 @@
 
   if (TGROUP_ZEROPT && !MOSAIC_ZEROPT) {
-    if ((nloop > 4) && (nloop <= 999)) {
+    if ((CurrentLoop > 4) && (CurrentLoop <= 999)) {
       // after iterating a few times on the TGroup zero points:
       // * identify the photometric nights
@@ -38,5 +48,5 @@
 
   if (!TGROUP_ZEROPT && MOSAIC_ZEROPT) {
-    if ((nloop > 4) && (nloop <= 999)) {
+    if ((CurrentLoop > 4) && (CurrentLoop <= 999)) {
       // after iterating a few times on the Mosaic zero points:
       // * identify the good / bad mosaics
@@ -50,5 +60,5 @@
 
   if (!TGROUP_ZEROPT && !MOSAIC_ZEROPT) {
-    if ((nloop > 4) && (nloop <= 999)) {
+    if ((CurrentLoop > 4) && (CurrentLoop <= 999)) {
       // after iterating a few times on the Image zero points:
       // * ???
@@ -61,18 +71,21 @@
 
 // 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) {
+// should we use IRLS or OLS?  depends on CurrentLoop and which stage we are in.
+int UseStandardOLS (ZptFitModeType mode) {
 
   if (TGROUP_ZEROPT && MOSAIC_ZEROPT) {
     switch (mode) {
+      case ZPT_STARS: 
+	if (CurrentLoop < 3) return TRUE;
+	return FALSE;
       case ZPT_TGROUP: 
-	if (nloop < 3) return TRUE;
+	if (CurrentLoop < 3) return TRUE;
 	return FALSE;
       case ZPT_MOSAIC:
-	if (nloop < 7) return TRUE;
+	if (CurrentLoop < 7) return TRUE;
 	return FALSE;
       case ZPT_IMAGES:
       default:
-	if (nloop < 11) return TRUE;
+	if (CurrentLoop < 11) return TRUE;
 	return FALSE;
     }
@@ -81,11 +94,14 @@
   if (TGROUP_ZEROPT && !MOSAIC_ZEROPT) {
     switch (mode) {
+      case ZPT_STARS: 
+	if (CurrentLoop < 3) return TRUE;
+	return FALSE;
       case ZPT_TGROUP: 
       case ZPT_MOSAIC:
-	if (nloop < 3) return TRUE;
+	if (CurrentLoop < 3) return TRUE;
 	return FALSE;
       case ZPT_IMAGES:
       default:
-	if (nloop < 7) return TRUE;
+	if (CurrentLoop < 7) return TRUE;
 	return FALSE;
     }
@@ -94,11 +110,14 @@
   if (!TGROUP_ZEROPT && MOSAIC_ZEROPT) {
     switch (mode) {
+      case ZPT_STARS: 
+	if (CurrentLoop < 3) return TRUE;
+	return FALSE;
       case ZPT_TGROUP: 
       case ZPT_MOSAIC:
-	if (nloop < 3) return TRUE;
+	if (CurrentLoop < 3) return TRUE;
 	return FALSE;
       case ZPT_IMAGES:
       default:
-	if (nloop < 7) return TRUE;
+	if (CurrentLoop < 7) return TRUE;
 	return FALSE;
     }
@@ -107,9 +126,12 @@
   if (!TGROUP_ZEROPT && !MOSAIC_ZEROPT) {
     switch (mode) {
+      case ZPT_STARS: 
+	if (CurrentLoop < 3) return TRUE;
+	return FALSE;
       case ZPT_TGROUP: 
       case ZPT_MOSAIC:
       case ZPT_IMAGES:
       default:
-	if (nloop < 3) return TRUE;
+	if (CurrentLoop < 3) return TRUE;
 	return FALSE;
     }
Index: /trunk/Ohana/src/relphot/src/args.c
===================================================================
--- /trunk/Ohana/src/relphot/src/args.c	(revision 41472)
+++ /trunk/Ohana/src/relphot/src/args.c	(revision 41473)
@@ -446,4 +446,9 @@
     }
     remove_argument (N, &argc, argv);
+  }
+  USE_REFERENCE_WEIGHT = FALSE;
+  if ((N = get_argument (argc, argv, "-ref-weight"))) {
+    remove_argument (N, &argc, argv);
+    USE_REFERENCE_WEIGHT = TRUE;
   }
 
Index: /trunk/Ohana/src/relphot/src/relphot_images.c
===================================================================
--- /trunk/Ohana/src/relphot/src/relphot_images.c	(revision 41472)
+++ /trunk/Ohana/src/relphot/src/relphot_images.c	(revision 41473)
@@ -116,4 +116,5 @@
     /* determine fit values */
     for (i = 0; i < NLOOP; i++) {
+      SetZptIteration (i);
       dump_tgroups (catalog, i);
       dump_catalog (catalog, 0, i); // for a test, just dump a specific catalog
@@ -123,7 +124,7 @@
       dump_catalog (catalog, 0, i + 20); // for a test, just dump a specific catalog
 
-      setMcal  (catalog, i, FALSE, flatcorr);
-      setMmos  (catalog, i, FALSE, flatcorr);
-      setMgrp  (catalog, i, FALSE, flatcorr);
+      setMcal  (catalog, FALSE, flatcorr);
+      setMmos  (catalog, FALSE, flatcorr);
+      setMgrp  (catalog, FALSE, flatcorr);
       dump_tgroups (catalog, i + 40);
       dump_catalog (catalog, 0, i + 40); // for a test, just dump a specific catalog
@@ -143,5 +144,5 @@
       global_stats (catalog, Ncatalog, flatcorr, i);
 
-      SetZeroPointModes (i);
+      SetZeroPointModes ();
       MARKTIME("-- finished loop %d: %f sec\n", i, dtime);
       fprintf (stderr, "----- continue loop %d -----\n", i);
@@ -162,7 +163,7 @@
     int onlyPoorImages = !CALIBRATE_STACKS_AND_WARPS;
     onlyPoorImages = FALSE; // XXX deactivate for now
-    setMcal  (catalog, 999, onlyPoorImages, flatcorr); // max loop since this is a final pass
-    setMmos  (catalog, 999, onlyPoorImages, flatcorr); // max loop since this is a final pass
-    setMgrp  (catalog, 999, onlyPoorImages, flatcorr); // max loop since this is a final pass
+    setMcal  (catalog, onlyPoorImages, flatcorr); // max loop since this is a final pass
+    setMmos  (catalog, onlyPoorImages, flatcorr); // max loop since this is a final pass
+    setMgrp  (catalog, onlyPoorImages, flatcorr); // max loop since this is a final pass
     MARKTIME("-- finalize Mcal values: %f sec\n", dtime);
 
Index: /trunk/Ohana/src/relphot/src/relphot_parallel_images.c
===================================================================
--- /trunk/Ohana/src/relphot/src/relphot_parallel_images.c	(revision 41472)
+++ /trunk/Ohana/src/relphot/src/relphot_parallel_images.c	(revision 41473)
@@ -95,10 +95,8 @@
   }
 
-  // XXX : add this in the loop at various points
-  // if (PLOTSTUFF) plot_scatter (catalog, Ncatalog, flatcorr); 
-
   /* determine fit values */
   client_logger_message ("starting the loops : %d \n", NLOOP);
   for (i = 0; i < NLOOP; i++) {
+    SetZptIteration (i);
 
     // set the mean stellar mags given the measurements and the image calibrations
@@ -114,6 +112,6 @@
 
     // set the image (Mcal) and mosaic (Mmos) zero point offsets given the mean mags and measurements
-    setMcal  (catalog, i, FALSE, flatcorr);
-    setMmos  (catalog, i, FALSE, flatcorr);
+    setMcal  (catalog, FALSE, flatcorr);
+    setMmos  (catalog, FALSE, flatcorr);
     // setMgrp  (catalog, FALSE, flatcorr); XXX think this through: this may not make sense for tgroups
     MARKTIME("-- set Mrel, Mcal, Mmos, Mgrid : %f sec\n", dtime);
@@ -126,10 +124,7 @@
     client_logger_message ("slurped image data : loop %d \n", i);
 
-    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_images ();
+    global_stats (catalog, Ncatalog, flatcorr, i);
 
-    global_stats (catalog, Ncatalog, flatcorr, i);
+    SetZeroPointModes ();
     MARKTIME("-- finished loop %d: %f sec\n", i, dtime);
   }
@@ -151,6 +146,6 @@
   /* set Mcal & Mmos for bad images (for stack_and_warps, force images to be measured) */
   int onlyPoorImages = !CALIBRATE_STACKS_AND_WARPS;
-  setMcal  (catalog, 999, onlyPoorImages, flatcorr);
-  setMmos  (catalog, 999, onlyPoorImages, flatcorr);
+  setMcal  (catalog, onlyPoorImages, flatcorr);
+  setMmos  (catalog, onlyPoorImages, flatcorr);
   // setMgrp  (catalog, onlyPoorImages, flatcorr); XXX see above
   MARKTIME("-- finalize Mcal values: %f sec\n", dtime);
Index: /trunk/Ohana/src/relphot/src/setMrelCatalog.c
===================================================================
--- /trunk/Ohana/src/relphot/src/setMrelCatalog.c	(revision 41472)
+++ /trunk/Ohana/src/relphot/src/setMrelCatalog.c	(revision 41473)
@@ -19,5 +19,5 @@
 
 // # define MAG_STATS_BY_RANKING magStatsByRankingIRLS
-# define MAG_STATS_BY_RANKING magStatsByRanking
+// # define MAG_STATS_BY_RANKING magStatsByRanking
 
 # define UBERCAL_WEIGHT 100.0
@@ -60,4 +60,6 @@
 FILE *TESTFILE = NULL;
 
+int useOLS = TRUE; // chosen based on iteration below
+
 int setMrel_catalog_alt (Catalog *catalog, int Nc, int isSetMrelFinal, FlatCorrectionTable *flatcorr, SetMrelInfo *results, int Nsecfilt) {
 
@@ -73,4 +75,5 @@
   TESTFILE = fopen ("mytest.dat", "w");
   
+  useOLS = UseStandardOLS (ZPT_STARS);
   for (j = 0; j < catalog[Nc].Naverage; j++) {
 
@@ -258,5 +261,9 @@
     if (isUbercal) results->haveUbercal[Nsec] = TRUE; // haveUbercal is set per secfilt, isUbercal is per measure XXX define this array
 
-    int useUbercalWeight = isUbercal || (refPhotcode && (code->code == refPhotcode->code));
+    // logic for choosing a modified weight:
+    float modifiedWeight = 1.0;
+    if (isUbercal) modifiedWeight = UBERCAL_WEIGHT;
+    if (refPhotcode && (code->code == refPhotcode->code)) modifiedWeight = UBERCAL_WEIGHT;
+    if (USE_REFERENCE_WEIGHT && (code->type == PHOT_REF)) modifiedWeight = code->photomErrSys; // we are overloading this field for now
 
     // combine the various errors in quadrature here:
@@ -277,5 +284,5 @@
 	results->aperData[Nsec].flxlist[Nap] = Map - Mflat - Mcal - Mmos - Mgrp - Mgrid; // this is consistent with PhotRel
 	results->aperData[Nsec].errlist[Nap] = dMap;
-	results->aperData[Nsec].wgtlist[Nap] = useUbercalWeight ? UBERCAL_WEIGHT : 1.0;
+	results->aperData[Nsec].wgtlist[Nap] = modifiedWeight;
 	results->aperData[Nsec].ranking[Nap] = measureRank[k];
 	results->aperData[Nsec].measSeq[Nap] = k;
@@ -297,5 +304,5 @@
       results->kronData[Nsec].flxlist[Nkron] = Mkron - Mflat - Mcal - Mmos - Mgrp - Mgrid; // this is consistent with PhotRel
       results->kronData[Nsec].errlist[Nkron] = dMkron;
-      results->kronData[Nsec].wgtlist[Nkron] = isUbercal ? UBERCAL_WEIGHT : 1.0;
+      results->kronData[Nsec].wgtlist[Nkron] = modifiedWeight;
       results->kronData[Nsec].ranking[Nkron] = measureRank ? measureRank[k] : 0;
       results->kronData[Nsec].measSeq[Nkron] = k;
@@ -310,5 +317,5 @@
       results->psfData[Nsec].flxlist[Npsf] = Mpsf - Mflat - Mcal - Mmos - Mgrp - Mgrid; // this is consistent with PhotRel
       results->psfData[Nsec].errlist[Npsf] = dMpsf;
-      results->psfData[Nsec].wgtlist[Npsf] = isUbercal ? UBERCAL_WEIGHT : 1.0;
+      results->psfData[Nsec].wgtlist[Npsf] = modifiedWeight;
       results->psfData[Nsec].ranking[Npsf] = measureRank ? measureRank[k] : 0;
       results->psfData[Nsec].measSeq[Npsf] = k;
@@ -406,5 +413,6 @@
     // if too few valid measurements meet the minimum criteria, go to the next entry
     StatType *psfstats = &results->psfstats;
-    int NrankingPSF = MAG_STATS_BY_RANKING (&results->psfData[Nsec], psfstats);
+
+    int NrankingPSF = useOLS ? magStatsByRanking (&results->psfData[Nsec], psfstats) : magStatsByRankingIRLS (&results->psfData[Nsec], psfstats);
     if (NrankingPSF < Nminmeas) { 
       secfilt[Nsec].flags |= ID_OBJ_FEW;
@@ -422,5 +430,5 @@
 
     StatType *kronstats = &results->kronstats;
-    int NrankingKRON = MAG_STATS_BY_RANKING (&results->kronData[Nsec], kronstats);
+    int NrankingKRON = useOLS ? magStatsByRanking (&results->kronData[Nsec], kronstats) : magStatsByRankingIRLS (&results->kronData[Nsec], kronstats);
     if (NrankingKRON) {
       secfilt[Nsec].MkronChp  = kronstats->mean; 
@@ -455,5 +463,5 @@
 
       StatType *apstats = &results->apstats;
-      int NrankingAPER = MAG_STATS_BY_RANKING (&results->aperData[Nsec], apstats);
+      int NrankingAPER = useOLS ? magStatsByRanking (&results->aperData[Nsec], apstats) : magStatsByRankingIRLS (&results->aperData[Nsec], apstats);
       if (NrankingAPER) {
 	secfilt[Nsec].MapChp  = apstats->mean; 
@@ -1032,5 +1040,6 @@
     // if too few valid measurements meet the minimum criteria, go to the next entry
     StatType *psfstats = &results->psfstats;
-    Nranking = MAG_STATS_BY_RANKING (&results->psfData[Nsec], psfstats);
+    // Nranking = MAG_STATS_BY_RANKING (&results->psfData[Nsec], psfstats);
+    Nranking = useOLS ? magStatsByRanking (&results->psfData[Nsec], psfstats) : magStatsByRankingIRLS (&results->psfData[Nsec], psfstats);
     if (Nranking) {
       secfilt[Nsec].FpsfWrp  = psfstats->mean;
@@ -1045,5 +1054,6 @@
     // if too few valid measurements meet the minimum criteria, go to the next entry
     StatType *apstats = &results->apstats;
-    Nranking = MAG_STATS_BY_RANKING (&results->aperData[Nsec], apstats);
+    // Nranking = MAG_STATS_BY_RANKING (&results->aperData[Nsec], apstats);
+    Nranking = useOLS ? magStatsByRanking (&results->aperData[Nsec], apstats) : magStatsByRankingIRLS (&results->aperData[Nsec], apstats);
     if (Nranking) {
       secfilt[Nsec].FapWrp     = apstats->mean;
@@ -1058,5 +1068,6 @@
     // if too few valid measurements meet the minimum criteria, go to the next entry
     StatType *kronstats = &results->kronstats;
-    Nranking = MAG_STATS_BY_RANKING (&results->kronData[Nsec], kronstats);
+    // Nranking = MAG_STATS_BY_RANKING (&results->kronData[Nsec], kronstats);
+    Nranking = useOLS ? magStatsByRanking (&results->kronData[Nsec], kronstats) : magStatsByRankingIRLS (&results->kronData[Nsec], kronstats);
     if (Nranking) {
       secfilt[Nsec].FkronWrp     = kronstats->mean;
