Index: /branches/eam_branches/ipp-20111122/Ohana/src/relphot/doc/flatcorr.txt
===================================================================
--- /branches/eam_branches/ipp-20111122/Ohana/src/relphot/doc/flatcorr.txt	(revision 33116)
+++ /branches/eam_branches/ipp-20111122/Ohana/src/relphot/doc/flatcorr.txt	(revision 33117)
@@ -1,2 +1,19 @@
+
+2012.01.18
+
+I have updated libdvo to define a flatcorr table, and have the APIs to
+read and write the table.  I have also updated setphot to load the
+ubercal measurements and to define the flatcorr table and links (from
+image -> flatcorr).  Now I need to understand where to include this in
+relphot.
+
+One option would be to include it as part of the function 'getMcal'.
+This function goes from (cat,meas) -> (image) to find the image zero
+point (Mcal).  It could also check for a photom_map_id value for that
+image, and include that in the calculation (if it exists).
+
+
+Another option would be to add a new function (getMmap or something)
+and to include this essentially everywhere getMcal is called.  
 
 2012.01.13
Index: /branches/eam_branches/ipp-20111122/Ohana/src/relphot/include/relphot.h
===================================================================
--- /branches/eam_branches/ipp-20111122/Ohana/src/relphot/include/relphot.h	(revision 33116)
+++ /branches/eam_branches/ipp-20111122/Ohana/src/relphot/include/relphot.h	(revision 33117)
@@ -133,5 +133,5 @@
 int           bcatalog            PROTO((Catalog *subcatalog, Catalog *catalog));
 void          clean_images        PROTO((void));
-void          clean_measures      PROTO((Catalog *catalog, int Ncatalog, int final));
+void          clean_measures      PROTO((Catalog *catalog, int Ncatalog, int final, FlatCorrectionTable *flatcorr));
 void          clean_mosaics       PROTO((void));
 void          clean_stars         PROTO((Catalog *catalog, int Ncatalog));
@@ -156,5 +156,5 @@
 Coords       *getCoords           PROTO((off_t meas, int cat));
 off_t         getImageEntry       PROTO((off_t meas, int cat));
-float         getMcal             PROTO((off_t meas, int cat));
+float         getMcal             PROTO((off_t meas, int cat, FlatCorrectionTable *flatcorr, Catalog *catalog));
 float         getMgrid            PROTO((off_t meas, int cat));
 float         getMmos             PROTO((off_t meas, int cat));
@@ -162,5 +162,5 @@
 Image        *getimage            PROTO((off_t N));
 Image        *getimages           PROTO((off_t *N, off_t **LineNumber));
-void          global_stats        PROTO((Catalog *catalog, int Ncatalog));
+void          global_stats        PROTO((Catalog *catalog, int Ncatalog, FlatCorrectionTable *flatcorr));
 void          initGrid            PROTO((int dX, int dY));
 void          initGridBins        PROTO((Catalog *catalog, int Ncatalog));
@@ -185,27 +185,27 @@
 void          plot_chisq          PROTO((Catalog *catalog, int Ncatalog));
 void          plot_defaults       PROTO((Graphdata *graphdata));
-void          plot_grid           PROTO((Catalog *catalog));
+void          plot_grid           PROTO((Catalog *catalog, FlatCorrectionTable *flatcorr));
 void          plot_images         PROTO((void));
 void          plot_list           PROTO((Graphdata *graphdata, double *xlist, double *ylist, int N, char *label, char *format, ...) OHANA_FORMAT(printf, 6, 7) );
 void          plot_mosaic_fields  PROTO((Catalog *catalog));
 void          plot_mosaics        PROTO((void));
-void          plot_scatter        PROTO((Catalog *catalog, int Ncatalog));
+void          plot_scatter        PROTO((Catalog *catalog, int Ncatalog, FlatCorrectionTable *flatcorr));
 void          plot_star_coords    PROTO((Catalog *catalog, int Ncatalog));
 void          plot_stars          PROTO((Catalog *catalog, int Ncatalog));
-void          reload_catalogs     PROTO((SkyList *skylist));
+void          reload_catalogs     PROTO((SkyList *skylist, FlatCorrectionTable *flatcorr));
 int           reload_images       PROTO((FITS_DB *db));
 int           setExclusions       PROTO((Catalog *catalog, int Ncatalog));
 void          setMcal             PROTO((Catalog *catalog, int Poor));
 void          setMcalFinal        PROTO((void));
-int           setMcalOutput       PROTO((Catalog *catalog, int Ncatalog));
-void          setMgrid            PROTO((Catalog *catalog));
-int           setMmos             PROTO((Catalog *catalog, int Poor));
-int           setMrel             PROTO((Catalog *catalog, int Ncatalog));
-void          setMrelFinal        PROTO((Catalog *catalog));
-int           setMrelOutput       PROTO((Catalog *catalog, int Ncatalog, int mark));
+int           setMcalOutput       PROTO((Catalog *catalog, int Ncatalog, FlatCorrectionTable *flatcorr));
+void          setMgrid            PROTO((Catalog *catalog, FlatCorrectionTable *flatcorr));
+int           setMmos             PROTO((Catalog *catalog, int Poor, FlatCorrectionTable *flatcorr));
+int           setMrel             PROTO((Catalog *catalog, int Ncatalog, FlatCorrectionTable *flatcorr));
+void          setMrelFinal        PROTO((Catalog *catalog, FlatCorrectionTable *flatcorr));
+int           setMrelOutput       PROTO((Catalog *catalog, int Ncatalog, int mark, FlatCorrectionTable *flatcorr));
 int           setMave             PROTO((Catalog *catalog, int Ncatalog));
 void          set_ZP              PROTO((double ZERO));
 int           setrefcode          PROTO((Image *image, off_t Nimage)); 
-void          skip_measurements   PROTO((Catalog *catalog, int pass));
+void          skip_measurements   PROTO((Catalog *catalog, int pass, FlatCorrectionTable *flatcorr));
 void          sortA               PROTO((double *X, int N));
 void          sortB               PROTO((double *X, double *Y, int N));
@@ -213,12 +213,12 @@
 void          sortD               PROTO((double *X, double *Y, double *Z, int N));
 StatType      statsImageM         PROTO((Catalog *catalog));
-StatType      statsImageN         PROTO((Catalog *catalog));
+StatType      statsImageN         PROTO((Catalog *catalog, FlatCorrectionTable *flatcorr));
 StatType      statsImageX         PROTO((Catalog *catalog));
 StatType      statsImagedM        PROTO((Catalog *catalog));
 StatType      statsMosaicM        PROTO((Catalog *catalog));
-StatType      statsMosaicN        PROTO((Catalog *catalog));
+StatType      statsMosaicN        PROTO((Catalog *catalog, FlatCorrectionTable *flatcorr));
 StatType      statsMosaicX        PROTO((Catalog *catalog));
 StatType      statsMosaicdM       PROTO((Catalog *catalog));
-StatType      statsStarN          PROTO((Catalog *catalog, int Ncatalog, int Nsec, int seccode));
+StatType      statsStarN          PROTO((Catalog *catalog, int Ncatalog, int Nsec, int seccode, FlatCorrectionTable *flatcorr));
 StatType      statsStarS          PROTO((Catalog *catalog, int Ncatalog, int Nsec));
 StatType      statsStarX          PROTO((Catalog *catalog, int Ncatalog, int Nsec));
Index: /branches/eam_branches/ipp-20111122/Ohana/src/relphot/src/GridOps.c
===================================================================
--- /branches/eam_branches/ipp-20111122/Ohana/src/relphot/src/GridOps.c	(revision 33116)
+++ /branches/eam_branches/ipp-20111122/Ohana/src/relphot/src/GridOps.c	(revision 33117)
@@ -284,5 +284,5 @@
 
 /* direct (non-iterative) solution for Mgrid values for all grid bins */
-void setMgridDirect (Catalog *catalog, int Ncatalog) {
+void setMgridDirect (Catalog *catalog, int Ncatalog, FlatCorrectionTable *flatcorr) {
 
   int **gotstar, **gridmeas;
@@ -399,5 +399,5 @@
 
 	// skip images marked as BAD
-	Mcal = getMcal  (m, c);
+	Mcal = getMcal  (m, c, flatcorr, catalog);
 	if (isnan(Mcal)) {
 	  Ncal ++;
@@ -522,5 +522,5 @@
 
 /* determine Mgrid values for all grid bins */
-void setMgrid (Catalog *catalog) {
+void setMgrid (Catalog *catalog, FlatCorrectionTable *flatcorr) {
 
   int i, j, m, c, n, N, Nmax, Nbad, Nmos, Ncal, Nrel, Nsys, Nfit;
@@ -554,5 +554,5 @@
 	continue;
       }
-      Mcal = getMcal  (m, c);
+      Mcal = getMcal  (m, c, flatcorr, catalog);
       if (isnan(Mcal)) {
 	Ncal ++;
@@ -609,5 +609,5 @@
 }
 
-void plot_grid (Catalog *catalog) {
+void plot_grid (Catalog *catalog, FlatCorrectionTable *flatcorr) {
 
   int i, j, m, c, n, N, Narea;
@@ -641,5 +641,5 @@
 	continue;
       }
-      Mcal  = getMcal  (m, c);
+      Mcal  = getMcal  (m, c, flatcorr, catalog);
       if (isnan(Mcal)) continue;
       Mmos  = getMmos  (m, c);
Index: /branches/eam_branches/ipp-20111122/Ohana/src/relphot/src/ImageOps.c
===================================================================
--- /branches/eam_branches/ipp-20111122/Ohana/src/relphot/src/ImageOps.c	(revision 33116)
+++ /branches/eam_branches/ipp-20111122/Ohana/src/relphot/src/ImageOps.c	(revision 33117)
@@ -253,8 +253,8 @@
 }
 
-float getMcal (off_t meas, int cat) {
+float getMcal (off_t meas, int cat, FlatCorrectionTable *flatcorr, Catalog *catalog) {
 
   off_t i;
-  float value;
+  float value, offset;
 
   i = MeasureToImage[cat][meas];
@@ -263,4 +263,13 @@
   if (image[i].flags & IMAGE_BAD) return (NAN);  
   value = image[i].Mcal;
+  offset = 0.0;
+
+  // to do this, I need to pass in the catalog and flatcorr pointers
+  int flat_id = image[i].photom_map_id;
+  if (flat_id) {
+    offset = FlatCorrectionOffset (flatcorr, flat_id, catalog[cat].measure[meas].Xccd, catalog[cat].measure[meas].Yccd);
+  }
+  value += offset;
+
   return (value);
 }
@@ -493,5 +502,5 @@
 }
 
-StatType statsImageN (Catalog *catalog) {
+StatType statsImageN (Catalog *catalog, FlatCorrectionTable *flatcorr) {
 
   off_t i, j, m, c, n, N;
@@ -516,5 +525,5 @@
       c = ImageToCatalog[i][j];
 
-      Mcal  = getMcal  (m, c);
+      Mcal  = getMcal  (m, c, flatcorr, catalog);
       if (isnan(Mcal)) continue;
       Mmos  = getMmos  (m, c);
Index: /branches/eam_branches/ipp-20111122/Ohana/src/relphot/src/MosaicOps.c
===================================================================
--- /branches/eam_branches/ipp-20111122/Ohana/src/relphot/src/MosaicOps.c	(revision 33116)
+++ /branches/eam_branches/ipp-20111122/Ohana/src/relphot/src/MosaicOps.c	(revision 33117)
@@ -376,5 +376,5 @@
 }
 
-int setMmos (Catalog *catalog, int PoorImages) {
+int setMmos (Catalog *catalog, int PoorImages, FlatCorrectionTable *flatcorr) {
 
   off_t i, j, m, c, N, Nmax;
@@ -425,5 +425,5 @@
 	  continue;
       }
-      Mcal  = getMcal  (m, c);
+      Mcal  = getMcal  (m, c, flatcorr, catalog);
       if (isnan(Mcal)) {
 	  Ncal++;
@@ -745,5 +745,5 @@
 }
 
-StatType statsMosaicN (Catalog *catalog) {
+StatType statsMosaicN (Catalog *catalog, FlatCorrectionTable *flatcorr) {
 
   off_t i, j, m, c, n, N;
@@ -769,5 +769,5 @@
       c = MosaicToCatalog[i][j];
 
-      Mcal = getMcal  (m, c);
+      Mcal = getMcal  (m, c, flatcorr, catalog);
       if (isnan(Mcal)) continue;
       Mgrid = getMgrid (m, c);
Index: /branches/eam_branches/ipp-20111122/Ohana/src/relphot/src/StarOps.c
===================================================================
--- /branches/eam_branches/ipp-20111122/Ohana/src/relphot/src/StarOps.c	(revision 33116)
+++ /branches/eam_branches/ipp-20111122/Ohana/src/relphot/src/StarOps.c	(revision 33117)
@@ -47,5 +47,5 @@
 }
 
-int setMrel (Catalog *catalog, int Ncatalog) {
+int setMrel (Catalog *catalog, int Ncatalog, FlatCorrectionTable *flatcorr) {
 
   off_t j, k, m;
@@ -88,5 +88,5 @@
 	    Mcal = Mmos = Mgrid = 0;
 	  } else {
-	    Mcal  = getMcal  (m, i);
+	    Mcal  = getMcal  (m, i, flatcorr, catalog);
 	    if (isnan(Mcal)) { 
 	      Ncal ++;
@@ -144,5 +144,5 @@
 }
 
-int setMrelOutput (Catalog *catalog, int Ncatalog, int mark) {
+int setMrelOutput (Catalog *catalog, int Ncatalog, int mark, FlatCorrectionTable *flatcorr) {
 
   int i, N;
@@ -190,5 +190,5 @@
 	    Mcal = Mmos = Mgrid = 0;
 	  } else {
-	    Mcal  = getMcal  (m, i);
+	    Mcal  = getMcal  (m, i, flatcorr, catalog);
 	    if (isnan(Mcal)) continue;
 	    Mmos  = getMmos  (m, i);
@@ -253,4 +253,8 @@
 
       // update average photometry for each of the average filters
+
+      // XXX Note that this would be faster if we had an array of results and accumulated
+      // them in a single pass
+
       for (Ns = 0; Ns < Nsecfilt; Ns++) {
 
@@ -353,5 +357,5 @@
 
 /* set measure.Mcal for all measures except ID_MEAS_NOCAL and ID_IMAGE_PHOTOM_NOCAL */
-int setMcalOutput (Catalog *catalog, int Ncatalog) {
+int setMcalOutput (Catalog *catalog, int Ncatalog, FlatCorrectionTable *flatcorr) {
 
   int i;
@@ -368,5 +372,5 @@
       for (k = 0; k < catalog[i].averageT[j].Nmeasure; k++, m++) {
 	if (catalog[i].measureT[m].dbFlags & MEAS_BAD) continue;
-	Mcal  = getMcal  (m, i);
+	Mcal  = getMcal  (m, i, flatcorr, catalog);
 	if (isnan(Mcal)) continue;
 	Mmos  = getMmos  (m, i);
@@ -456,5 +460,5 @@
 # define NSIGMA_CLIP 3.0
 # define NSIGMA_REJECT 5.0
-void clean_measures (Catalog *catalog, int Ncatalog, int final) {
+void clean_measures (Catalog *catalog, int Ncatalog, int final, FlatCorrectionTable *flatcorr) {
 
   off_t j, k, m, Nmax, Ndel, Nave;
@@ -510,5 +514,5 @@
 
 	  /* if (catalog[i].measureT[m].dbFlags & MEAS_BAD) continue; */
-	  Mcal  = getMcal  (m, i);
+	  Mcal  = getMcal  (m, i, flatcorr, catalog);
 	  if (isnan(Mcal)) { Ncal ++; continue; }
 	  Mmos  = getMmos  (m, i);
@@ -554,5 +558,5 @@
 
 	  /* if (catalog[i].measureT[m].dbFlags & MEAS_BAD) continue; */
-	  Mcal  = getMcal  (m, i);
+	  Mcal  = getMcal  (m, i, flatcorr, catalog);
 	  if (isnan(Mcal)) continue;
 	  Mmos  = getMmos  (m, i);
@@ -593,5 +597,5 @@
 }
 
-StatType statsStarN (Catalog *catalog, int Ncatalog, int Nsec, int seccode) {
+StatType statsStarN (Catalog *catalog, int Ncatalog, int Nsec, int seccode, FlatCorrectionTable *flatcorr) {
 
   off_t j, k, m, Ntot;
@@ -625,5 +629,5 @@
 	int ecode = GetPhotcodeEquivCodebyCode (catalog[i].measureT[m].photcode);
 	if (ecode != seccode) { continue;}
-	Mcal = getMcal  (m, i);
+	Mcal = getMcal  (m, i, flatcorr, catalog);
 	if (isnan(Mcal)) { continue;}
 	Mmos = getMmos  (m, i);
Index: /branches/eam_branches/ipp-20111122/Ohana/src/relphot/src/global_stats.c
===================================================================
--- /branches/eam_branches/ipp-20111122/Ohana/src/relphot/src/global_stats.c	(revision 33116)
+++ /branches/eam_branches/ipp-20111122/Ohana/src/relphot/src/global_stats.c	(revision 33117)
@@ -1,5 +1,5 @@
 # include "relphot.h"
 
-void global_stats (Catalog *catalog, int Ncatalog) {
+void global_stats (Catalog *catalog, int Ncatalog, FlatCorrectionTable *flatcorr) {
 
   StatType stN, stX, stS, imN, imX, imM, imD, msM, msX, msN, msD;
@@ -17,5 +17,5 @@
     int seccode = photcodes[Ns][0].code;
 
-    stN = statsStarN (catalog, Ncatalog, Nsec, seccode);
+    stN = statsStarN (catalog, Ncatalog, Nsec, seccode, flatcorr);
     stX = statsStarX (catalog, Ncatalog, Nsec);
     stS = statsStarS (catalog, Ncatalog, Nsec);
@@ -27,10 +27,10 @@
   }
   
-  imN = statsImageN (catalog);
+  imN = statsImageN (catalog, flatcorr);
   imX = statsImageX (catalog);
   imM = statsImageM (catalog);
   imD = statsImagedM (catalog);
   
-  msN = statsMosaicN (catalog);
+  msN = statsMosaicN (catalog, flatcorr);
   msM = statsMosaicM (catalog);
   msD = statsMosaicdM (catalog);
Index: /branches/eam_branches/ipp-20111122/Ohana/src/relphot/src/plot_scatter.c
===================================================================
--- /branches/eam_branches/ipp-20111122/Ohana/src/relphot/src/plot_scatter.c	(revision 33116)
+++ /branches/eam_branches/ipp-20111122/Ohana/src/relphot/src/plot_scatter.c	(revision 33117)
@@ -1,5 +1,5 @@
 # include "relphot.h"
    
-void plot_scatter (Catalog *catalog, int Ncatalog) {
+void plot_scatter (Catalog *catalog, int Ncatalog, FlatCorrectionTable *flatcorr) {
 
   off_t i, j, k, m, N, Ntot;
@@ -40,5 +40,5 @@
 
 		if (catalog[i].measureT[m].dbFlags & MEAS_BAD) continue;
-		Mcal = getMcal  (m, i);
+		Mcal = getMcal  (m, i, flatcorr, catalog);
 		if (isnan(Mcal)) continue;
 		Mmos = getMmos  (m, i);
Index: /branches/eam_branches/ipp-20111122/Ohana/src/relphot/src/reload_catalogs.c
===================================================================
--- /branches/eam_branches/ipp-20111122/Ohana/src/relphot/src/reload_catalogs.c	(revision 33116)
+++ /branches/eam_branches/ipp-20111122/Ohana/src/relphot/src/reload_catalogs.c	(revision 33117)
@@ -7,5 +7,5 @@
     gettimeofday (&start, (void *) NULL);
 
-void reload_catalogs (SkyList *skylist) {
+void reload_catalogs (SkyList *skylist, FlatCorrectionTable *flatcorr) {
 
   int i;
@@ -69,5 +69,5 @@
     TIMESTAMP(time4);
 
-    setMrelFinal (&catalog);
+    setMrelFinal (&catalog, flatcorr);
     TIMESTAMP(time5);
 
Index: /branches/eam_branches/ipp-20111122/Ohana/src/relphot/src/relphot.c
===================================================================
--- /branches/eam_branches/ipp-20111122/Ohana/src/relphot/src/relphot.c	(revision 33116)
+++ /branches/eam_branches/ipp-20111122/Ohana/src/relphot/src/relphot.c	(revision 33117)
@@ -1,10 +1,3 @@
 # include "relphot.h"
-# define USE_DIRECT 0
-
-# define TIMESTAMP(TIME) \
-    gettimeofday (&stop, (void *) NULL);	\
-    dtime = DTIME (stop, start);		\
-    TIME += dtime;				\
-    gettimeofday (&start, (void *) NULL);
 
 # define MARKTIME(MSG,...) { \
@@ -24,82 +17,4 @@
   gettimeofday (&start, (void *) NULL);
 
-  // XXX quick and stupid test:
-  if (0) {
-    int i, j; 
-    off_t *Nlist, *NLIST, **mlist;
-    off_t *NlistI, *NLISTI, **mlistI;
-    int **clist;
-    int **clistI;
-    float dtime;
-    float time1 = 0.0;
-    float time2 = 0.0;
-
-    int Nmosaic = 10000;
-    int Nimage = 600000;
-
-    for (j = 0; j < 19000; j++) {
-
-      // mosaic indexes
-      ALLOCATE (Nlist, off_t,   Nmosaic);
-      ALLOCATE (NLIST, off_t,   Nmosaic);
-      ALLOCATE (clist, int *,   Nmosaic);
-      ALLOCATE (mlist, off_t *, Nmosaic);
-    
-      for (i = 0; i < Nmosaic; i++) {
-	Nlist[i] = 0;
-	NLIST[i] = 100;
-	ALLOCATE (clist[i], int,   NLIST[i]);
-	ALLOCATE (mlist[i], off_t, NLIST[i]);
-      }
-
-      // image indexes
-      ALLOCATE (NlistI, off_t,   Nimage);
-      ALLOCATE (NLISTI, off_t,   Nimage);
-      ALLOCATE (clistI, int *,   Nimage);
-      ALLOCATE (mlistI, off_t *, Nimage);
-    
-      for (i = 0; i < Nimage; i++) {
-	NlistI[i] = 0;
-	NLISTI[i] = 100;
-	ALLOCATE (clistI[i], int,   NLISTI[i]);
-	ALLOCATE (mlistI[i], off_t, NLISTI[i]);
-      }
-
-      TIMESTAMP(time1);
-
-      // free mosaic indexes
-      for (i = 0; i < Nmosaic; i++) {
-	free (clist[i]);
-	free (mlist[i]);
-      }
-      free (Nlist);
-      free (NLIST);
-      free (clist);
-      free (mlist);
-
-      // free image indexes
-      for (i = 0; i < Nimage; i++) {
-	free (clistI[i]);
-	free (mlistI[i]);
-      }
-      free (NlistI);
-      free (NLISTI);
-      free (clistI);
-      free (mlistI);
-
-      TIMESTAMP(time2);
-
-      if (j % 100 == 0) {
-	fprintf (stderr, "done with %d\n", j);
-	fprintf (stderr, "time1  %f : init mosaic\n", time1);
-	fprintf (stderr, "time2  %f : free mosaic\n", time2);
-      }
-    }
-    
-    fprintf (stderr, "time1  %f : init mosaic\n", time1);
-    fprintf (stderr, "time2  %f : free mosaic\n", time2);
-    exit (1);
-  }
-
   /* get configuration info, args */
   initialize (argc, argv);
@@ -125,7 +40,13 @@
   // LCK_EMPTY (if UPDATE) or LCK_MISSING (if !UPDATE)
   if ((db.dbstate == LCK_EMPTY) || (db.dbstate == LCK_MISSING)) {
-    // XXX get ZERO_POINT from config
-    dvo_image_create (&db, 25.0);
-    // Shutdown ("ERROR: No images in catalog %s (1)", db.filename);
+
+    // 25.0 is the nominal zero point for measurements in the database
+    // for all measurements except PHOT_REF:
+    // measure.M contains: -2.5*log(DN) + 2.5*log(exptime) + 25.0 
+    // for measurements using PHOT_REF:
+    // measure.M contains: -2.5*log(DN) + 2.5*log(exptime) + True Zero Point (but exptime and ZP are known)
+    dvo_image_create (&db, 25.0); 
+
+    // XXX why are we running relphot on an empty database?
   } else {
     if (!dvo_image_load (&db, VERBOSE, FALSE)) Shutdown ("can't read image catalog %s", db.filename);
@@ -143,4 +64,9 @@
   if (!UPDATE) dvo_image_unlock (&db); 
 
+  // load the flat correction table (if defined)
+  char flatcorrfile[256];
+  sprintf (flatcorrfile, "%s/flatcorr.fits", CATDIR);
+  FlatCorrectionTable *flatcorr = FlatCorrectionLoad (flatcorrfile, VERBOSE);
+
   /* load catalog data from region files */
   catalog = load_catalogs (skylist, &Ncatalog);
@@ -167,5 +93,5 @@
   setExclusions (catalog, Ncatalog);
 
-  global_stats (catalog, Ncatalog);
+  global_stats (catalog, Ncatalog, flatcorr);
 
   if (PLOTSTUFF) {
@@ -184,7 +110,7 @@
       for (i = 0; i < NGRID; i++) {
 	  STAR_BAD = ID_STAR_POOR;
-	  setMrel  (catalog, Ncatalog);
+	  setMrel  (catalog, Ncatalog, flatcorr);
 	  STAR_BAD  = ID_STAR_POOR | ID_STAR_FEW;
-	  setMgrid (catalog);
+	  setMgrid (catalog, flatcorr);
       }
       STAR_BAD  = ID_STAR_POOR | ID_STAR_FEW;
@@ -194,12 +120,12 @@
   /* determine fit values */
   for (i = 0; i < NLOOP; i++) {
-    setMrel  (catalog, Ncatalog);
+    setMrel  (catalog, Ncatalog, flatcorr);
     setMcal  (catalog, FALSE);
-    setMmos  (catalog, FALSE);
-    setMgrid (catalog);
+    setMmos  (catalog, FALSE, flatcorr);
+    setMgrid (catalog, flatcorr);
     
     if (PLOTSTUFF) {
-      plot_scatter (catalog, Ncatalog); 
-      plot_grid (catalog); 
+      plot_scatter (catalog, Ncatalog, flatcorr); 
+      plot_grid (catalog, flatcorr); 
       plot_mosaics ();
       plot_images ();
@@ -209,5 +135,5 @@
     // if (i < NLOOP - 1) rationalize_mosaics (catalog, Ncatalog);
     // if (i % 6 == 1) rationalize_images ();
-    if (i % 6 == 2) clean_measures (catalog, Ncatalog, FALSE); 
+    if (i % 6 == 2) clean_measures (catalog, Ncatalog, FALSE, flatcorr); 
     if (i % 6 == 3) clean_stars (catalog, Ncatalog);
     if (i % 6 == 5) clean_mosaics ();
@@ -218,11 +144,11 @@
     // if ((i == 4) || (i == 8) || (i == 12) || (i == 16)) clean_mosaics ();
     // if ((i == 4) || (i == 8) || (i == 12) || (i == 16)) clean_images ();
-    global_stats (catalog, Ncatalog);
+    global_stats (catalog, Ncatalog, flatcorr);
     MARKTIME("-- finished loop %d: %f sec\n", i, dtime);
   }
 
   if (PLOTSTUFF) {
-    plot_scatter (catalog, Ncatalog); 
-    plot_grid (catalog); 
+    plot_scatter (catalog, Ncatalog, flatcorr); 
+    plot_grid (catalog, flatcorr); 
     plot_mosaics ();
     plot_images ();
@@ -235,5 +161,5 @@
   /* set Mcal & Mmos for bad images */
   setMcal  (catalog, TRUE);
-  setMmos  (catalog, TRUE);
+  setMmos  (catalog, TRUE, flatcorr);
   MARKTIME("-- finalize Mcal values: %f sec\n", dtime);
 
@@ -309,5 +235,5 @@
 
   /* load catalog data from region files, update Mrel include all data */
-  reload_catalogs (skylist);
+  reload_catalogs (skylist, flatcorr);
   MARKTIME("-- updated all catalogs: %f sec\n", dtime);
 
Index: /branches/eam_branches/ipp-20111122/Ohana/src/relphot/src/relphot_objects.c
===================================================================
--- /branches/eam_branches/ipp-20111122/Ohana/src/relphot/src/relphot_objects.c	(revision 33116)
+++ /branches/eam_branches/ipp-20111122/Ohana/src/relphot/src/relphot_objects.c	(revision 33117)
@@ -1,3 +1,8 @@
 # include "relphot.h"
+
+// apply the current calibration values to the measurements to determine the average
+// magnitudes, applying the desired clipping analysis.  Note that this does not depend on
+// the form of the image zero point measurement.  If there is a grid or map term, and this
+// has been correctly propagated to the measurement, the average will respect that value.
 
 int relphot_objects () {
Index: /branches/eam_branches/ipp-20111122/Ohana/src/relphot/src/setMrelFinal.c
===================================================================
--- /branches/eam_branches/ipp-20111122/Ohana/src/relphot/src/setMrelFinal.c	(revision 33116)
+++ /branches/eam_branches/ipp-20111122/Ohana/src/relphot/src/setMrelFinal.c	(revision 33117)
@@ -5,5 +5,5 @@
 // output dbFlags values
 
-void setMrelFinal (Catalog *catalog) {
+void setMrelFinal (Catalog *catalog, FlatCorrectionTable *flatcorr) {
 
   off_t i, j, m;
@@ -60,10 +60,10 @@
 
   for (i = 0; i < 5; i++) {
-    skip_measurements (catalog, i);       /* set ID_MEAS_SKIP for measures to be skipped */
-    setMrelOutput  (catalog, 1, FALSE);   /* set Mrel using all measures not skipped */
-    clean_measures (catalog, 1, TRUE);    /* mark outliers ID_MEAS_POOR_PHOTOM */
-    setMrelOutput  (catalog, 1, TRUE);    /* set Mrel using remaining measures */
+    skip_measurements (catalog, i, flatcorr);       /* set ID_MEAS_SKIP for measures to be skipped */
+    setMrelOutput  (catalog, 1, FALSE, flatcorr);   /* set Mrel using all measures not skipped */
+    clean_measures (catalog, 1, TRUE, flatcorr);    /* mark outliers ID_MEAS_POOR_PHOTOM */
+    setMrelOutput  (catalog, 1, TRUE, flatcorr);    /* set Mrel using remaining measures */
   }
-  setMcalOutput (catalog, 1);
+  setMcalOutput (catalog, 1, flatcorr);
 
   int Nsecfilt = GetPhotcodeNsecfilt ();
@@ -86,5 +86,5 @@
 
 /* ID_MEAS_SKIP marks measurements which were not used to calculate Mrel */
-void skip_measurements (Catalog *catalog, int pass) {
+void skip_measurements (Catalog *catalog, int pass, FlatCorrectionTable *flatcorr) {
 
   off_t i, k, m;
@@ -134,5 +134,5 @@
       /* skip measurements from BAD images and mosaics */
       /* do NOT skip measurements without a matching image */
-      if (isnan(getMcal (m, 0))) goto skip;
+      if (isnan(getMcal (m, 0, flatcorr, catalog))) goto skip;
       if (isnan(getMmos (m, 0))) goto skip;
 
