Index: /branches/eam_branches/ipp-20110404/Ohana/src/relphot/doc/allsky.txt
===================================================================
--- /branches/eam_branches/ipp-20110404/Ohana/src/relphot/doc/allsky.txt	(revision 31272)
+++ /branches/eam_branches/ipp-20110404/Ohana/src/relphot/doc/allsky.txt	(revision 31272)
@@ -0,0 +1,50 @@
+
+2011.04.12
+
+  I have modified the code to operate on multiple filter at a time,
+  and to have a limited density, with the goal of running the analysis
+  on a huge area.  However, we are somewhat limited by the fact that the
+  'measure' and 'average' structures are huge and have far more than
+  is needed for just the relphot (or even relastro) analysis.  
+
+  I am exploring the idea of using a stripped-down structure in the
+  relphot analysis -- it would be assigned in bcatalog.
+
+  Here are the elements we actually need in relphot proper:
+
+average: 
+  FIELD R,              RA,          double,          RA,                           decimal degrees 
+  FIELD D,              DEC,         double,          DEC,                          decimal degrees 
+  FIELD Nmeasure,       NMEASURE,    unsigned short,  number of psf measurements
+  FIELD measureOffset,  OFF_MEASURE, int,             offset to first psf measurement
+  FIELD flags,          FLAGS,       uint32_t,        average object flags (star; ghost; etc)
+  = 2*8 + 2 + 4 + 4 = 26 bytes
+
+measure:
+  FIELD dR,             D_RA,         float,          RA offset,                	    arcsec
+  FIELD dD,             D_DEC,        float,          DEC offset,               	    arcsec
+  FIELD M,              MAG,          float,          catalog mag,       	       	    mag
+  FIELD Mcal,           M_CAL,        float,          image cal mag,	            mag
+
+  FIELD dM,             MAG_ERR,      float,          mag error,                      mag
+  FIELD airmass,        AIRMASS,      float,          (airmass - 1),		    airmass
+  FIELD Xccd,           X_CCD,        float,          X coord on chip,                pixels
+  FIELD Yccd,           Y_CCD,        float,          Y coord on chip,                pixels
+
+  FIELD t,              TIME,         int,   	      time in seconds (UNIX)
+  FIELD averef,         AVE_REF,      unsigned int,   reference to average entry      
+  FIELD imageID,        IMAGE_ID,     unsigned int,   reference to DVO image ID
+  FIELD dbFlags,        DB_FLAGS,     unsigned int,   flags supplied by analysis in database
+  FIELD photcode,       PHOTCODE,     unsigned short, photcode
+  = 8*4 + 4*4 + 2 = 50 bytes
+
+  (note that psfQual and photFlags are only used in bcatalog to select measurements)
+
+eg, 1 year 3 pi has 1 average and 20 measures per object (and 8 secfilt entries)
+
+* current: 112 + 8*24 + 20*148 = 3264 bytes / star
+* propose: 26  + 8*24 + 20*50  = 1218 bytes / star
+
+for the full 3pi area, in 30GB, we could use 820 stars per square
+degree (5740 per exposure), as opposed to the current limit of 300
+(2100 per exposure).
Index: /branches/eam_branches/ipp-20110404/Ohana/src/relphot/include/relphot.h
===================================================================
--- /branches/eam_branches/ipp-20110404/Ohana/src/relphot/include/relphot.h	(revision 31271)
+++ /branches/eam_branches/ipp-20110404/Ohana/src/relphot/include/relphot.h	(revision 31272)
@@ -11,4 +11,5 @@
   unsigned int start;
   unsigned int stop;
+  short photcode;
   float Mcal;
   float dMcal;
@@ -218,5 +219,5 @@
 StatType      statsMosaicX        PROTO((Catalog *catalog));
 StatType      statsMosaicdM       PROTO((Catalog *catalog));
-StatType      statsStarN          PROTO((Catalog *catalog, int Ncatalog, int seccode));
+StatType      statsStarN          PROTO((Catalog *catalog, int Ncatalog, int Nsec, int seccode));
 StatType      statsStarS          PROTO((Catalog *catalog, int Ncatalog, int Nsec));
 StatType      statsStarX          PROTO((Catalog *catalog, int Ncatalog, int Nsec));
Index: /branches/eam_branches/ipp-20110404/Ohana/src/relphot/src/GridOps.c
===================================================================
--- /branches/eam_branches/ipp-20110404/Ohana/src/relphot/src/GridOps.c	(revision 31271)
+++ /branches/eam_branches/ipp-20110404/Ohana/src/relphot/src/GridOps.c	(revision 31272)
@@ -348,4 +348,8 @@
   // sums for each star which touches as cell on both bases.
 
+  int Nsecfilt = GetPhotcodeNsecfilt ();
+  int thisCode = photcodes[0][0].code;
+  int Nsec = GetPhotcodeNsec(thisCode);
+
   for (i = 0; i < Ngrid; i++) {
     
@@ -369,5 +373,5 @@
 
       // skip stars marked as BAD
-      if (catalog[c].average[n].flags & STAR_BAD) {
+      if (catalog[c].secfilt[n*Nsecfilt+Nsec].flags & STAR_BAD) {
 	Nrel ++;
 	continue;
Index: /branches/eam_branches/ipp-20110404/Ohana/src/relphot/src/ImageOps.c
===================================================================
--- /branches/eam_branches/ipp-20110404/Ohana/src/relphot/src/ImageOps.c	(revision 31271)
+++ /branches/eam_branches/ipp-20110404/Ohana/src/relphot/src/ImageOps.c	(revision 31272)
@@ -134,4 +134,5 @@
   int i, ecode, Ns, found;
   
+  int Nmatch = 0;
  for (i = 0; i < Ncatalog; i++) {
     for (j = 0; j < catalog[i].Nmeasure; j++) {
@@ -143,6 +144,8 @@
       if (!found) continue; 
       matchImage (catalog, j, i);
-    }
-  }
+      Nmatch ++;
+    }
+  }
+  fprintf (stderr, "matched %d detections to images\n", Nmatch);
 }
 
Index: /branches/eam_branches/ipp-20110404/Ohana/src/relphot/src/MosaicOps.c
===================================================================
--- /branches/eam_branches/ipp-20110404/Ohana/src/relphot/src/MosaicOps.c	(revision 31271)
+++ /branches/eam_branches/ipp-20110404/Ohana/src/relphot/src/MosaicOps.c	(revision 31272)
@@ -86,4 +86,5 @@
     mosaic[Nmosaic].flags  = image[i].flags;
     mosaic[Nmosaic].secz  = image[i].secz;
+    mosaic[Nmosaic].photcode = GetPhotcodeEquivCodebyCode (image[i].photcode);
 
     /* add image to mosaic image list */
@@ -267,4 +268,5 @@
   if (!MOSAIC_ZEROPT) return (FALSE);
 
+  int Nmatch = 0;
   for (i = 0; i < Ncatalog; i++) {
     for (j = 0; j < catalog[i].Nmeasure; j++) {
@@ -280,6 +282,8 @@
       if (!found) continue;
       matchMosaics (catalog, j, i);
-    }
-  }
+      Nmatch ++;
+    }
+  }
+  fprintf (stderr, "matched %d detections to mosaics\n", Nmatch);
   return (TRUE);
 }
@@ -507,8 +511,8 @@
 int rationalize_mosaics (Catalog *catalog, int Ncatalog) {
 
-  double *imageOffset, *starOffset;
-  int *starNcount;
+  double *imageOffset, **starOffset;
+  int **starNcount, *seclist;
   int **Slist, *NSlist, *NSLIST;
-  int i, j, k, m, nMos;
+  int i, j, k, m, nMos, Ns, found;
 
   off_t Nimage;
@@ -528,4 +532,5 @@
   ALLOCATE (NSlist, int,   Nmosaic); // number of stars on mosaic
   ALLOCATE (NSLIST, int,   Nmosaic); // number of Slist entries allocated
+  memset (Slist, 0, Nmosaic*sizeof(int *));
 
   // find the images / mosaics with negative clouds and save their offset
@@ -552,18 +557,27 @@
   }
 
-  // allocate an array for star offset
+  int Nsecfilt = GetPhotcodeNsecfilt ();
+  ALLOCATE (seclist, int, Nphotcodes);
+  for (Ns = 0; Ns < Nphotcodes; Ns ++) { 
+    int thisCode = photcodes[Ns][0].code;
+    seclist[Ns] = GetPhotcodeNsec(thisCode);
+  }
+
+  // allocate an array for star offsets
   int Nstars = 0;
   int NSTARS = 1000;
-  ALLOCATE (starOffset, double, NSTARS);
-  ALLOCATE (starNcount, int,    NSTARS);
+  ALLOCATE (starOffset, double *, NSTARS);
+  ALLOCATE (starNcount, int *,    NSTARS);
+  memset (starOffset, 0, NSTARS*sizeof(double *));
+  memset (starNcount, 0, NSTARS*sizeof(int *));
 
   // find the mean offset for each star
   for (i = 0; i < Ncatalog; i++) {
     for (j = 0; j < catalog[i].Naverage; j++) {
-      starOffset[Nstars] = 0.0;
-      starNcount[Nstars] = 0;
-
-      // skip bad stars
-      if (catalog[i].average[j].flags & STAR_BAD) continue;  
+      ALLOCATE (starOffset[Nstars], double, Nphotcodes);
+      ALLOCATE (starNcount[Nstars], int,    Nphotcodes);
+      memset (starOffset[Nstars], 0, Nphotcodes*sizeof(double));
+      memset (starNcount[Nstars], 0, Nphotcodes*sizeof(int));
+      
       m = catalog[i].average[j].measureOffset;
 
@@ -573,4 +587,20 @@
 	// skip unused measurements
 	if (catalog[i].measure[m].dbFlags & MEAS_BAD) continue;
+
+	// skip unused measurements
+	int Nsec;
+	int ecode = GetPhotcodeEquivCodebyCode (catalog[i].measure[m].photcode);
+	found = FALSE;
+	for (Ns = 0; !found && (Ns < Nphotcodes); Ns ++) { 
+	  if (ecode == photcodes[Ns][0].code) {
+	    found = TRUE;
+	    break;
+	  }
+	}
+	if (!found) continue;
+	Nsec = seclist[Ns];
+
+	// bad stars for this secfilt
+	if (catalog[i].secfilt[Nsecfilt*j+Nsec].flags & STAR_BAD) continue;  
 
 	// skip REF measurements (not tied to an image)
@@ -581,7 +611,14 @@
 	if (nMos == -1) continue;
 
+	if (mosaic[nMos].photcode != ecode) {
+	  fprintf (stderr, "*");
+	}
+
+	assert (Ns >= 0);
+	assert (Ns < Nphotcodes);
+
 	// accumulate the offsets from the negative cloud images (others have 0.0 value)
-	starOffset[Nstars] += imageOffset[nMos];
-	starNcount[Nstars] ++;
+	starOffset[Nstars][Ns] += imageOffset[nMos];
+	starNcount[Nstars][Ns] ++;
 	
 	// record the mosaic->star reference
@@ -596,6 +633,8 @@
       if (Nstars == NSTARS) {
 	NSTARS += 1000;
-	REALLOCATE (starOffset, double, NSTARS);
-	REALLOCATE (starNcount, int,    NSTARS);
+	REALLOCATE (starOffset, double *, NSTARS);
+	REALLOCATE (starNcount, int *,    NSTARS);
+	memset (&starOffset[NSTARS-1000], 0, 1000*sizeof(double *));
+	memset (&starNcount[NSTARS-1000], 0, 1000*sizeof(int *));
       }
     }
@@ -604,4 +643,16 @@
   // find the mean offset of the images without negative clouds
   for (i = 0; i < Nmosaic; i++) {
+
+    found = FALSE;
+    for (Ns = 0; !found && (Ns < Nphotcodes); Ns ++) { 
+      if (mosaic[i].photcode == photcodes[Ns][0].code) {
+	found = TRUE;
+	break;
+      }
+    }
+    if (!found) {
+      fprintf (stderr, "invalid photcode for mosaic?\n");
+      abort();
+    }
 
     // a negative cloud image (cloud: Mcal > 0; anti-clouds: Mcal < 0; imageOffset = -Mcal)
@@ -614,6 +665,6 @@
     for (j = 0; j < NSlist[i]; j++) {
       Nstars = Slist[i][j];
-      if (starNcount[Nstars] > 1) {
-	dM += (starOffset[Nstars] / starNcount[Nstars]);
+      if (starNcount[Nstars][Ns] > 1) {
+	dM += (starOffset[Nstars][Ns] / starNcount[Nstars][Ns]);
       }
     }
@@ -631,4 +682,11 @@
     mosaic[i].Mcal += imageOffset[i];
   }
+
+  for (i = 0; i < Nstars; i++) {
+    free (starOffset[i]);
+    free (starNcount[i]);
+  }
+  free (starOffset);
+  free (starNcount);
 
   for (i = 0; i < Nmosaic; i++){
@@ -638,5 +696,5 @@
   free (NSLIST);
   free (imageOffset);
-
+  free (seclist);
   return (TRUE);
 }
Index: /branches/eam_branches/ipp-20110404/Ohana/src/relphot/src/StarOps.c
===================================================================
--- /branches/eam_branches/ipp-20110404/Ohana/src/relphot/src/StarOps.c	(revision 31271)
+++ /branches/eam_branches/ipp-20110404/Ohana/src/relphot/src/StarOps.c	(revision 31272)
@@ -29,17 +29,18 @@
 float getMrel (Catalog *catalog, off_t meas, int cat) {
 
-  int Nsec, Nsecfilt, code;
+  int Nsec, Nsecfilt, photcode;
   int ave;
   float value;
 
   ave = catalog[cat].measure[meas].averef;
-  code = catalog[cat].measure[meas].photcode;
-
-  // XXX DEP : this should use a flag associated with secfilt
-  if (catalog[cat].average[ave].flags & STAR_BAD) return (NAN);  
+  photcode = catalog[cat].measure[meas].photcode;
+
+  int ecode = GetPhotcodeEquivCodebyCode (photcode);
+  Nsec = GetPhotcodeNsec(ecode);
+  Nsecfilt = GetPhotcodeNsecfilt ();
+
+  // is this star OK?
+  if (catalog[cat].secfilt[Nsecfilt*ave+Nsec].flags & STAR_BAD) return (NAN);  
  
-  Nsec = GetPhotcodeNsec(code);
-  Nsecfilt = GetPhotcodeNsecfilt ();
-
   value = catalog[cat].secfilt[Nsecfilt*ave+Nsec].M;
   return (value);
@@ -52,7 +53,6 @@
   float Msys, Mcal, Mmos, Mgrid;
   StatType stats;
-  int Nsec, Nsecfilt, ecode;
-
-  Nsecfilt = GetPhotcodeNsecfilt ();
+
+  int Nsecfilt = GetPhotcodeNsecfilt ();
   Nfew = Nsys = Nbad = Ncal = Nmos = Ngrid = 0;
 
@@ -64,9 +64,10 @@
 
 	int thisCode = photcodes[Ns][0].code;
-	Nsec = GetPhotcodeNsec(thisCode);
+	int Nsec = GetPhotcodeNsec(thisCode);
 
 	/* calculate the average value for a single star */
-	// XXX this flag should be set by secfilt, not average
-	if (catalog[i].average[j].flags & STAR_BAD) continue;  
+
+	// skip bad stars
+	if (catalog[i].secfilt[Nsecfilt*j+Nsec].flags & STAR_BAD) continue;
 	m = catalog[i].average[j].measureOffset;
 
@@ -74,5 +75,5 @@
 	for (k = 0; k < catalog[i].average[j].Nmeasure; k++, m++) {
 	  // skip measurements that do not match the current photcode
-	  ecode = GetPhotcodeEquivCodebyCode (catalog[i].measure[m].photcode);
+	  int ecode = GetPhotcodeEquivCodebyCode (catalog[i].measure[m].photcode);
 	  if (ecode != thisCode) { continue; }
 
@@ -123,10 +124,9 @@
 
 	// when performing the grid analysis, STAR_TOOFEW will be set to 1;
-	// XXX DEP this makes no sense: need a separate flag for each secfilt
 	if (N <= STAR_TOOFEW) { /* too few measurements */
-	  catalog[i].average[j].flags |= ID_STAR_FEW;
+	  catalog[i].secfilt[Nsecfilt*j+Nsec].flags |= ID_STAR_FEW;
 	  Nfew ++;
 	} else {
-	  catalog[i].average[j].flags &= ~ID_STAR_FEW;
+	  catalog[i].secfilt[Nsecfilt*j+Nsec].flags &= ~ID_STAR_FEW;
 	}	
 
@@ -408,5 +408,5 @@
     for (i = Ntot = 0; i < Ncatalog; i++) {
       for (j = 0; j < catalog[i].Naverage; j++) {
-	if (catalog[i].average[j].flags & STAR_BAD) continue;
+	if ( catalog[i].secfilt[Nsecfilt*j+Nsec].flags & STAR_BAD ) continue;
 	Xm = catalog[i].secfilt[Nsecfilt*j+Nsec].Xm;
 	if (Xm == -1) continue;
@@ -434,8 +434,8 @@
 	mark = (dM > MaxScatter) || (Xm == NAN_S_SHORT) || (Chisq > MaxChisq);
 	if (mark) {
-	  catalog[i].average[j].flags |= ID_STAR_POOR;
+	  catalog[i].secfilt[Nsecfilt*j+Nsec].flags |= ID_STAR_POOR;
 	  Ndel ++;
 	} else {
-	  catalog[i].average[j].flags &= ~ID_STAR_POOR;
+	  catalog[i].secfilt[Nsecfilt*j+Nsec].flags &= ~ID_STAR_POOR;
 	}
 	Nave ++;
@@ -492,7 +492,8 @@
 
 	int thisCode = photcodes[Ns][0].code;
-
+	int Nsec = GetPhotcodeNsec(thisCode);
+	
 	/* skip bad stars to prevent them from becoming good (on inner sample) */
-	if (catalog[i].average[j].flags & STAR_BAD) continue;  
+	if (catalog[i].secfilt[Nsecfilt*j+Nsec].flags & STAR_BAD) continue;  
 
 	/* accumulate list of valid measurements */
@@ -582,5 +583,5 @@
 }
 
-StatType statsStarN (Catalog *catalog, int Ncatalog, int seccode) {
+StatType statsStarN (Catalog *catalog, int Ncatalog, int Nsec, int seccode) {
 
   off_t j, k, m, Ntot;
@@ -589,4 +590,8 @@
   float Mcal, Mmos, Mgrid;
   StatType stats;
+  int N1, N2, N3, N4, N0;
+  N1 = N2 = N3 = N4 = N0 = 0;
+
+  int Nsecfilt = GetPhotcodeNsecfilt ();
 
   Ntot = 0;
@@ -603,18 +608,17 @@
 
       /* calculate the average value for a single star */
-      if (catalog[i].average[j].flags & STAR_BAD) continue;  
+      if (catalog[i].secfilt[Nsecfilt*j+Nsec].flags & STAR_BAD) { N1++; continue;  }
       m = catalog[i].average[j].measureOffset;
-
-      int ecode = GetPhotcodeEquivCodebyCode (catalog[i].measure[m].photcode);
-      if (ecode == seccode) continue;
 
       N = 0;
       for (k = 0; k < catalog[i].average[j].Nmeasure; k++, m++) {
+	int ecode = GetPhotcodeEquivCodebyCode (catalog[i].measure[m].photcode);
+	if (ecode != seccode) { N0++; continue;}
 	Mcal = getMcal  (m, i);
-	if (isnan(Mcal)) continue;
+	if (isnan(Mcal)) { N2++; continue;}
 	Mmos = getMmos  (m, i);
-	if (isnan(Mmos)) continue;
+	if (isnan(Mmos)) { N3++; continue; }
 	Mgrid = getMgrid (m, i);
-	if (isnan(Mgrid)) continue;
+	if (isnan(Mgrid)) { N4++; continue;}
 	N++;
       }
@@ -626,4 +630,5 @@
   }
 
+  fprintf (stderr, "N1: %d, N2: %d, N3: %d, N4: %d, N0: %d\n", N1, N2, N3, N4, N0);
   liststats (list, dlist, n, &stats);
   free (list);
@@ -655,5 +660,5 @@
 
       /* calculate the average value for a single star */
-      if (catalog[i].average[j].flags & STAR_BAD) continue;  
+      if (catalog[i].secfilt[Nsecfilt*j+Nsec].flags & STAR_BAD) continue;  
 	
       Xm = catalog[i].secfilt[Nsecfilt*j+Nsec].Xm;
@@ -694,5 +699,5 @@
 
       /* calculate the average value for a single star */
-      if (catalog[i].average[j].flags & STAR_BAD) continue;  
+      if (catalog[i].secfilt[Nsecfilt*j+Nsec].flags & STAR_BAD) continue;  
 
       dM = catalog[i].secfilt[Nsecfilt*j+Nsec].dM;
@@ -733,5 +738,5 @@
     for (i = 0; i < Ncatalog; i++) {
       for (j = 0; j < catalog[i].Naverage; j++) {
-	if (catalog[i].average[j].flags & STAR_BAD) continue;  
+	if (catalog[i].secfilt[Nsecfilt*j+Nsec].flags & STAR_BAD) continue;  
 	dMrel = catalog[i].secfilt[Nsecfilt*j+Nsec].dM;
 	bin = dMrel / 0.00025;
@@ -773,5 +778,5 @@
     for (i = 0; i < Ncatalog; i++) {
       for (j = 0; j < catalog[i].Naverage; j++) {
-	if (catalog[i].average[j].flags & STAR_BAD) continue;
+	if (catalog[i].secfilt[Nsecfilt*j+Nsec].flags & STAR_BAD) continue;
 	xlist[N] = catalog[i].secfilt[Nsecfilt*j+Nsec].M;
 	value    = catalog[i].secfilt[Nsecfilt*j+Nsec].Xm;
Index: /branches/eam_branches/ipp-20110404/Ohana/src/relphot/src/bcatalog.c
===================================================================
--- /branches/eam_branches/ipp-20110404/Ohana/src/relphot/src/bcatalog.c	(revision 31271)
+++ /branches/eam_branches/ipp-20110404/Ohana/src/relphot/src/bcatalog.c	(revision 31272)
@@ -45,6 +45,6 @@
 	subcatalog[0].secfilt[Nsecfilt*Naverage+Nsec].M  = NAN;
 	subcatalog[0].secfilt[Nsecfilt*Naverage+Nsec].dM = NAN;
-	subcatalog[0].average[Naverage].flags &= ~ID_STAR_FEW;
-	subcatalog[0].average[Naverage].flags &= ~ID_STAR_POOR;
+	subcatalog[0].secfilt[Nsecfilt*Naverage+Nsec].flags &= ~ID_STAR_FEW;
+	subcatalog[0].secfilt[Nsecfilt*Naverage+Nsec].flags &= ~ID_STAR_POOR;
       }
     }
@@ -75,6 +75,4 @@
 
       /* select measurements by quality */
-      // XXX ignore this criterion for REF measurements?
-      // XXX chnage this to select by bitflags
       if (DophotSelect && ((catalog[0].measure[offset].photFlags >> 16) != DophotValue)) { Ndophot ++; continue; }
 
Index: /branches/eam_branches/ipp-20110404/Ohana/src/relphot/src/global_stats.c
===================================================================
--- /branches/eam_branches/ipp-20110404/Ohana/src/relphot/src/global_stats.c	(revision 31271)
+++ /branches/eam_branches/ipp-20110404/Ohana/src/relphot/src/global_stats.c	(revision 31272)
@@ -7,4 +7,5 @@
   initstats ("MEAN");
 
+  fprintf (stderr, "\n");
   fprintf (stderr, "STATS            median     mean    sigma      min      max   Nmeas\n");
 
@@ -16,12 +17,12 @@
     int seccode = photcodes[Ns][0].code;
 
-    stN = statsStarN (catalog, Ncatalog, seccode);
+    stN = statsStarN (catalog, Ncatalog, Nsec, seccode);
     stX = statsStarX (catalog, Ncatalog, Nsec);
     stS = statsStarS (catalog, Ncatalog, Nsec);
   
     fprintf (stderr, "   --- stats for %s ---\n", photcodes[Ns][0].name);
-    fprintf (stderr, "meas / image:   %7.0f  %7.0f  %7.0f  %7.0f  %7.0f  %6d\n",   imN.median, imN.mean, imN.sigma, imN.min, imN.max, imN.Nmeas);
-    fprintf (stderr, "meas / mosaic:  %7.0f  %7.0f  %7.0f  %7.0f  %7.0f  %6d\n",   msN.median, msN.mean, msN.sigma, msN.min, msN.max, msN.Nmeas);
     fprintf (stderr, "meas / star:    %7.0f  %7.1f  %7.1f  %7.0f  %7.0f  %6d\n",   stN.median, stN.mean, stN.sigma, stN.min, stN.max, stN.Nmeas);
+    fprintf (stderr, "dMrel star:     %7.4f  %7.4f  %7.4f  %7.4f  %7.4f  %6d\n", stS.median, stS.mean, stS.sigma, stS.min, stS.max, stS.Nmeas);
+    fprintf (stderr, "chisq star:     %7.1f  %7.1f  %7.1f  %7.1f  %7.1f  %6d\n",   stX.median, stX.mean, stX.sigma, stX.min, stX.max, stX.Nmeas);
   }
   
@@ -36,15 +37,14 @@
   msX = statsMosaicX (catalog);
   
-  fprintf (stderr, "chisq image:    %7.1f  %7.1f  %7.1f  %7.1f  %7.1f  %6d\n",   imX.median, imX.mean, imX.sigma, imX.min, imX.max, imX.Nmeas);
-  fprintf (stderr, "chisq mosaic:   %7.1f  %7.1f  %7.1f  %7.1f  %7.1f  %6d\n",   msX.median, msX.mean, msX.sigma, msX.min, msX.max, msX.Nmeas);
-  fprintf (stderr, "chisq star:     %7.1f  %7.1f  %7.1f  %7.1f  %7.1f  %6d\n",   stX.median, stX.mean, stX.sigma, stX.min, stX.max, stX.Nmeas);
-  
+  fprintf (stderr, "meas / image:   %7.0f  %7.0f  %7.0f  %7.0f  %7.0f  %6d\n",   imN.median, imN.mean, imN.sigma, imN.min, imN.max, imN.Nmeas);
   fprintf (stderr, "Mcal image:     %7.4f  %7.4f  %7.4f  %7.4f  %7.4f  %6d\n",   imM.median, imM.mean, imM.sigma, imM.min, imM.max, imM.Nmeas);
   fprintf (stderr, "dMcal image:    %7.4f  %7.4f  %7.4f  %7.4f  %7.4f  %6d\n",   imD.median, imD.mean, imD.sigma, imD.min, imD.max, imD.Nmeas);
-  
+  fprintf (stderr, "chisq image:    %7.1f  %7.1f  %7.1f  %7.1f  %7.1f  %6d\n",   imX.median, imX.mean, imX.sigma, imX.min, imX.max, imX.Nmeas);
+
+  fprintf (stderr, "meas / mosaic:  %7.0f  %7.0f  %7.0f  %7.0f  %7.0f  %6d\n",   msN.median, msN.mean, msN.sigma, msN.min, msN.max, msN.Nmeas);
   fprintf (stderr, "Mcal mosaic:    %7.4f  %7.4f  %7.4f  %7.4f  %7.4f  %6d\n",   msM.median, msM.mean, msM.sigma, msM.min, msM.max, msM.Nmeas);
   fprintf (stderr, "dMcal mosaic:   %7.4f  %7.4f  %7.4f  %7.4f  %7.4f  %6d\n",   msD.median, msD.mean, msD.sigma, msD.min, msD.max, msD.Nmeas);
+  fprintf (stderr, "chisq mosaic:   %7.1f  %7.1f  %7.1f  %7.1f  %7.1f  %6d\n",   msX.median, msX.mean, msX.sigma, msX.min, msX.max, msX.Nmeas);
   
-  fprintf (stderr, "dMrel star:     %7.4f  %7.4f  %7.4f  %7.4f  %7.4f  %6d\n\n", stS.median, stS.mean, stS.sigma, stS.min, stS.max, stS.Nmeas);
 
   initstats (STATMODE);
Index: /branches/eam_branches/ipp-20110404/Ohana/src/relphot/src/plot_scatter.c
===================================================================
--- /branches/eam_branches/ipp-20110404/Ohana/src/relphot/src/plot_scatter.c	(revision 31271)
+++ /branches/eam_branches/ipp-20110404/Ohana/src/relphot/src/plot_scatter.c	(revision 31272)
@@ -31,5 +31,5 @@
 
 	    /* calculate the average value for a single star */
-	    if (catalog[i].average[j].flags & STAR_BAD) continue;  
+	    if (catalog[i].secfilt[Nsecfilt*j+Nsec].flags & STAR_BAD) continue;  
 	    m = catalog[i].average[j].measureOffset;
 
Index: /branches/eam_branches/ipp-20110404/Ohana/src/relphot/src/relphot.c
===================================================================
--- /branches/eam_branches/ipp-20110404/Ohana/src/relphot/src/relphot.c	(revision 31271)
+++ /branches/eam_branches/ipp-20110404/Ohana/src/relphot/src/relphot.c	(revision 31272)
@@ -82,4 +82,6 @@
 
   setExclusions (catalog, Ncatalog);
+
+  global_stats (catalog, Ncatalog);
 
   if (PLOTSTUFF) {
Index: /branches/eam_branches/ipp-20110404/Ohana/src/relphot/src/relphot_objects.c
===================================================================
--- /branches/eam_branches/ipp-20110404/Ohana/src/relphot/src/relphot_objects.c	(revision 31271)
+++ /branches/eam_branches/ipp-20110404/Ohana/src/relphot/src/relphot_objects.c	(revision 31272)
@@ -3,5 +3,5 @@
 int relphot_objects () {
 
-  off_t i, j, k, m;
+  off_t i, j, k;
   int Nsecfilt;
 
@@ -51,9 +51,5 @@
 	  catalog.secfilt[j*Nsecfilt + k].Ncode = 0;
 	  catalog.secfilt[j*Nsecfilt + k].Nused = 0;
-	}
-	m = catalog.average[j].measureOffset;
-	for (k = 0; k < catalog.average[j].Nmeasure; k++) {
-	    // catalog.measure[m+k].dbFlags = 0;
-	    // catalog.measure[m+k].Mcal = 0;
+	  // XXX reset the photometry flags for secfilt entries?
 	}
       }
Index: /branches/eam_branches/ipp-20110404/Ohana/src/relphot/src/setMrelFinal.c
===================================================================
--- /branches/eam_branches/ipp-20110404/Ohana/src/relphot/src/setMrelFinal.c	(revision 31271)
+++ /branches/eam_branches/ipp-20110404/Ohana/src/relphot/src/setMrelFinal.c	(revision 31272)
@@ -62,8 +62,14 @@
   setMcalOutput (catalog, 1);
 
+  int Nsecfilt = GetPhotcodeNsecfilt ();
+
   /* clear ID_STAR_POOR, ID_STAR_FEW, ID_MEAS_NOCAL values before writing ??? */
   for (i = 0; i < catalog[0].Naverage; i++) {
     catalog[0].average[i].flags &= ~ID_STAR_FEW;
     catalog[0].average[i].flags &= ~ID_STAR_POOR;
+    for (j = 0; j < Nsecfilt; j++) {
+	catalog[0].secfilt[i*Nsecfilt+j].flags &= ~ID_STAR_FEW; 
+	catalog[0].secfilt[i*Nsecfilt+j].flags &= ~ID_STAR_POOR; 
+    }
     m = catalog[0].average[i].measureOffset;
     for (j = 0; j < catalog[0].average[i].Nmeasure; j++, m++) {
