Index: trunk/Ohana/src/addstar/src/loadgalphot_readstars.c
===================================================================
--- trunk/Ohana/src/addstar/src/loadgalphot_readstars.c	(revision 39261)
+++ trunk/Ohana/src/addstar/src/loadgalphot_readstars.c	(revision 39262)
@@ -204,4 +204,6 @@
   fit->ClipNsigma = 5.0;
 
+  // use the list of index values from above to join entries with the same index
+
   int Nbad = 0;
   for (i = 0; i < Nrow; i++) {
Index: trunk/Ohana/src/dvomerge/src/merge_catalogs_old.c
===================================================================
--- trunk/Ohana/src/dvomerge/src/merge_catalogs_old.c	(revision 39261)
+++ trunk/Ohana/src/dvomerge/src/merge_catalogs_old.c	(revision 39262)
@@ -85,5 +85,5 @@
     N1[Nstars] = i;
     Nstars ++;
-    input[0].found_t[i] = -1;
+    input[0].found_t[i] = FALSE;
   }
   if (Nstars < 1) {
@@ -180,4 +180,6 @@
     off_t N = N1[i];
 
+    input[0].found_t[N] = TRUE;
+
     /* make sure there is space for next Nmeasure entries */
     if (Nmeas + input[0].average[N].Nmeasure >= NMEAS) {
@@ -209,5 +211,4 @@
       Nreplace = replace_tycho (&output[0].average[n], output[0].measure, next_meas, &input[0].average[N], &input[0].measure[Minp]);
       if (Nreplace == 6) {
-	input[0].found_t[N] = Nmeas;
 	output[0].found_t[n] = Nmeas;
 	i++;
@@ -224,5 +225,4 @@
 	// XXX this does not support lensing, starpar, or galphot measurements
 	if (replace_match (&output[0].average[n], output[0].measure, next_meas, &input[0].average[N], &input[0].measure[offset])) {
-	  input[0].found_t[N] = output[0].average[n].measureOffset;  
 	  continue;
 	}
@@ -274,5 +274,4 @@
         }
       }
-      input[0].found_t[N] = Nmeas;
       output[0].average[n].Nmeasure ++;
       Nmeas ++;
@@ -394,9 +393,11 @@
     }
 
-    if (input[0].found_t[N] >= 0) continue;
+    if (input[0].found_t[N]) continue;
 
     // if we are using MATCHED_TABLES, we are going to leave the edge cases in their
     // source catalog, even if they have leaked beyond the edge
     if (!MATCHED_TABLES && !IN_REGION (input[0].average[N].R, input[0].average[N].D)) continue;
+
+    input[0].found_t[N] = TRUE;
 
     // XXX should we accept the input measurements for these fields?
@@ -450,5 +451,4 @@
 
       /* we set next[Nmeas] to -1 here, and update correctly below */
-      input[0].found_t[N] = Nmeas;
       next_meas[Nmeas] = -1;
       Nmeas ++;
@@ -559,13 +559,4 @@
   }
 
-  /* note stars which have been found in this catalog */
-  for (i = 0; i < input[0].Naverage; i++) {
-    if (input[0].found_t[i] > -1) {
-      input[0].found_t[i] = -2;
-    } else {
-      input[0].found_t[i] = -3;
-    }
-  }
-
   /* check if the catalog has changed?  if no change, no need to write */
   output[0].objID    = objID; // new max value, save on catalog close
Index: trunk/Ohana/src/libautocode/def/measure-ps1-sim.d
===================================================================
--- trunk/Ohana/src/libautocode/def/measure-ps1-sim.d	(revision 39261)
+++ trunk/Ohana/src/libautocode/def/measure-ps1-sim.d	(revision 39262)
@@ -27,6 +27,7 @@
 FIELD XoffDCR,        X_OFF_DCR,     float,          X offset from correction,     pixels
 FIELD YoffDCR,        Y_OFF_DCR,     float,          Y offset from correction,     pixels
-FIELD RoffGAL,        R_OFF_GAL,     float,          RA offset from correction,    arcsec
-FIELD DoffGAL,        D_OFF_GAL,     float,          DEC offset from correction,   arcsec
+
+FIELD Mflat,          M_FLAT,        float,          Static Flat-field offset,     mag
+FIELD dummy2,         PADDING,       int,            unused 4 bytes
 
 FIELD t,              TIME,         int,            time in seconds (UNIX)
@@ -48,5 +49,5 @@
 FIELD dRsys,          POS_SYS_ERR,  short,          systematic error from astrom,   1/100 of pixels
 
-FIELD dummy,          PADDING,      short,          padding
+FIELD dummy,          PADDING2,     short,          padding
 FIELD posangle,       POSANGLE,     short,          position angle sky to chip,     (0xffff/360) deg
 FIELD pltscale,       PLTSCALE,     float,          plate scale,                    arcsec/pixel
Index: trunk/Ohana/src/libautocode/def/measure-ps1-v5.d
===================================================================
--- trunk/Ohana/src/libautocode/def/measure-ps1-v5.d	(revision 39261)
+++ trunk/Ohana/src/libautocode/def/measure-ps1-v5.d	(revision 39262)
@@ -42,6 +42,7 @@
 FIELD XoffCAM,        X_OFF_CAM,     float,          X offset from correction,     pixels
 FIELD YoffCAM,        Y_OFF_CAM,     float,          Y offset from correction,     pixels
-FIELD RoffGAL,        R_OFF_GAL,     float,          RA offset from correction,    arcsec
-FIELD DoffGAL,        D_OFF_GAL,     float,          DEC offset from correction,   arcsec
+
+FIELD Mflat,          M_FLAT,        float,          Static Flat-field offset,     mag
+FIELD dummy2,         PADDING,       int,            unused 4 bytes
 
 # could these be packed into fewer bits?
Index: trunk/Ohana/src/libautocode/def/measure.d
===================================================================
--- trunk/Ohana/src/libautocode/def/measure.d	(revision 39261)
+++ trunk/Ohana/src/libautocode/def/measure.d	(revision 39262)
@@ -43,9 +43,6 @@
 FIELD YoffCAM,        Y_OFF_CAM,     float,          Y offset from correction,     pixels
 
-# XXX I can deprecate these as I am going to apply the correct uR,uD offset
-# XXX not sure how to use this yet...
-# XXX replace RoffGall with Moff -> static component of the zpt correction
-FIELD RoffGAL,        R_OFF_GAL,     float,          RA offset from correction,    arcsec
-FIELD DoffGAL,        D_OFF_GAL,     float,          DEC offset from correction,   arcsec
+FIELD Mflat,          M_FLAT,        float,          Static Flat-field offset,     mag
+FIELD dummy2,         PADDING,       int,            unused 4 bytes
 
 # could these be packed into fewer bits?
Index: trunk/Ohana/src/libdvo/include/dvo.h
===================================================================
--- trunk/Ohana/src/libdvo/include/dvo.h	(revision 39261)
+++ trunk/Ohana/src/libdvo/include/dvo.h	(revision 39262)
@@ -476,8 +476,7 @@
   double         R;
   double         D;
-  float          RoffGAL;
-  float          DoffGAL;
   float          M;
   float          Mcal;
+  float          Mflat;
   float          dM;
   float          airmass;
@@ -620,6 +619,6 @@
   float            XoffDCR;              // X offset from correction (pixels)
   float            YoffDCR;              // Y offset from correction (pixels)
-  float            RoffGAL;              // RA offset from correction (arcsec)
-  float            DoffGAL;              // DEC offset from correction (arcsec)
+  float            Mflat;                // flat offset from correction (arcsec)
+  int              padding2;             // dummy
   float            Sky;                  // local estimate of sky flux (counts/sec)
   float            dSky;                 // local estimate of sky flux (counts/sec)
Index: trunk/Ohana/src/libdvo/src/dbExtractMeasures.c
===================================================================
--- trunk/Ohana/src/libdvo/src/dbExtractMeasures.c	(revision 39261)
+++ trunk/Ohana/src/libdvo/src/dbExtractMeasures.c	(revision 39262)
@@ -597,6 +597,4 @@
     case MEAS_XOFF_CAM: { value.Flt = measure[0].XoffCAM; break; }
     case MEAS_YOFF_CAM: { value.Flt = measure[0].YoffCAM; break; }
-    case MEAS_ROFF_GAL: { value.Flt = measure[0].RoffGAL; break; }
-    case MEAS_DOFF_GAL: { value.Flt = measure[0].DoffGAL; break; }
 
     case MEAS_XFIELD: /* offset relative to exposure center in ra,dec space */
@@ -723,17 +721,5 @@
 
     case MEAS_MCAL_OFFSET: { value.Flt = measure[0].Mcal; break; }
-
-    case MEAS_FLAT: /* OK */
-      // flat = measure.Mcal - image.Mcal
-      if (REMOTE_CLIENT) {
-	ImageMetadata *image = MatchImageMetadataDVO (measure[0].imageID);
-	if (image == NULL) break;
-	value.Flt = measure[0].Mcal - image[0].Mcal;
-      } else {
-	Image *image = MatchImageDVO (measure[0].t, measure[0].photcode, measure[0].imageID);
-	if (image == NULL) break;
-	value.Flt = measure[0].Mcal - image[0].Mcal;
-      }
-      break;
+    case MEAS_FLAT: { value.Flt = measure[0].Mflat; break; }
 
       // we have measure[0].Xccd,Yccd and image[0].NX,NY.  Find the distance to the center
Index: trunk/Ohana/src/libdvo/src/dvo_catalog.c
===================================================================
--- trunk/Ohana/src/libdvo/src/dvo_catalog.c	(revision 39261)
+++ trunk/Ohana/src/libdvo/src/dvo_catalog.c	(revision 39262)
@@ -332,6 +332,7 @@
  measure->XoffCAM   = NAN;
  measure->YoffCAM   = NAN;
- measure->RoffGAL   = NAN;
- measure->DoffGAL   = NAN;
+
+ measure->Mflat     = 0.0;
+ measure->dummy2    = 0;
 
  measure->Sky       = NAN;
Index: trunk/Ohana/src/libdvo/src/dvo_convert_PS1_SIM.c
===================================================================
--- trunk/Ohana/src/libdvo/src/dvo_convert_PS1_SIM.c	(revision 39261)
+++ trunk/Ohana/src/libdvo/src/dvo_convert_PS1_SIM.c	(revision 39262)
@@ -29,6 +29,5 @@
     out[i].XoffDCR    = in[i].XoffDCR;
     out[i].YoffDCR    = in[i].YoffDCR;
-    out[i].RoffGAL    = in[i].RoffGAL;
-    out[i].DoffGAL    = in[i].DoffGAL;
+    out[i].Mflat      = in[i].Mflat;
     out[i].t          = in[i].t;
     out[i].averef     = in[i].averef;
@@ -75,6 +74,5 @@
     out[i].XoffDCR    = in[i].XoffDCR;
     out[i].YoffDCR    = in[i].YoffDCR;
-    out[i].RoffGAL    = in[i].RoffGAL;
-    out[i].DoffGAL    = in[i].DoffGAL;
+    out[i].Mflat      = in[i].Mflat;
     out[i].t          = in[i].t;
     out[i].averef     = in[i].averef;
Index: trunk/Ohana/src/libdvo/src/dvo_convert_PS1_V5.c
===================================================================
--- trunk/Ohana/src/libdvo/src/dvo_convert_PS1_V5.c	(revision 39261)
+++ trunk/Ohana/src/libdvo/src/dvo_convert_PS1_V5.c	(revision 39262)
@@ -42,6 +42,5 @@
     out[i].XoffCAM    = in[i].XoffCAM;
     out[i].YoffCAM    = in[i].YoffCAM;
-    out[i].RoffGAL    = in[i].RoffGAL;
-    out[i].DoffGAL    = in[i].DoffGAL;
+    out[i].Mflat      = in[i].Mflat;
     out[i].Sky        = in[i].Sky;
     out[i].dSky       = in[i].dSky;
@@ -116,6 +115,5 @@
     out[i].XoffCAM    = in[i].XoffCAM;
     out[i].YoffCAM    = in[i].YoffCAM;
-    out[i].RoffGAL    = in[i].RoffGAL;
-    out[i].DoffGAL    = in[i].DoffGAL;
+    out[i].Mflat      = in[i].Mflat;
     out[i].Sky        = in[i].Sky;
     out[i].dSky       = in[i].dSky;
@@ -1678,6 +1676,5 @@
     out[i].XoffDCR    = in[i].XoffDCR;
     out[i].YoffDCR    = in[i].YoffDCR;
-    out[i].RoffGAL    = in[i].RoffGAL;
-    out[i].DoffGAL    = in[i].DoffGAL;
+    out[i].Mflat      = in[i].Mflat;
     out[i].Sky        = in[i].Sky;
     out[i].dSky       = in[i].dSky;
Index: trunk/Ohana/src/libdvo/src/dvo_tiny_values.c
===================================================================
--- trunk/Ohana/src/libdvo/src/dvo_tiny_values.c	(revision 39261)
+++ trunk/Ohana/src/libdvo/src/dvo_tiny_values.c	(revision 39262)
@@ -22,6 +22,5 @@
   measureT[0].dM         = measure[0].dM;
   measureT[0].airmass    = measure[0].airmass;
-  measureT[0].RoffGAL    = measure[0].RoffGAL;
-  measureT[0].DoffGAL    = measure[0].DoffGAL;
+  measureT[0].Mflat      = measure[0].Mflat;
   measureT[0].Xccd       = measure[0].Xccd;
   measureT[0].Yccd       = measure[0].Yccd;
Index: trunk/Ohana/src/relastro/src/BrightCatalog.c
===================================================================
--- trunk/Ohana/src/relastro/src/BrightCatalog.c	(revision 39261)
+++ trunk/Ohana/src/relastro/src/BrightCatalog.c	(revision 39262)
@@ -67,6 +67,4 @@
     GET_COLUMN(R,         "RA",   	double);
     GET_COLUMN(D,         "DEC",  	double);
-    GET_COLUMN(RoffGAL,   "ROFF_GAL",  	float);
-    GET_COLUMN(DoffGAL,   "DOFF_GAL",  	float);
     GET_COLUMN(M,         "MAG_SYS",  	float);
     GET_COLUMN(Mcal,      "MAG_CAL",  	float);
@@ -94,6 +92,4 @@
       measure[i].R         = R[i];
       measure[i].D         = D[i];
-      measure[i].RoffGAL   = RoffGAL[i];
-      measure[i].DoffGAL   = DoffGAL[i];
       measure[i].M         = M[i];
       measure[i].Mcal      = Mcal[i];
@@ -121,6 +117,4 @@
     free (R       );
     free (D       );
-    free (RoffGAL );
-    free (DoffGAL );
     free (M       );
     free (Mcal    );
@@ -362,6 +356,4 @@
     gfits_define_bintable_column (&theader, "D", "RA",       "ra",                         "degrees", 1.0, 0.0);
     gfits_define_bintable_column (&theader, "D", "DEC",      "dec",                        "degrees", 1.0, 0.0);
-    gfits_define_bintable_column (&theader, "E", "ROFF_GAL", "ra offset",                  "arcsec", 1.0, 0.0);
-    gfits_define_bintable_column (&theader, "E", "DOFF_GAL", "dec offset",                 "arcsec", 1.0, 0.0);
     gfits_define_bintable_column (&theader, "E", "MAG_SYS",  "magnitude (sys)",             NULL,    1.0, 0.0);
     gfits_define_bintable_column (&theader, "E", "MAG_CAL",  "magnitude (cal)",             NULL,    1.0, 0.0);
@@ -382,13 +374,9 @@
 
     // generate the output array that carries the data
-    ohana_memcheck (1);
     gfits_create_table (&theader, &ftable);
-    ohana_memcheck (1);
 
     // create intermediate storage arrays
     double *R         ; ALLOCATE (R        ,  double, catalog->Nmeasure);
     double *D         ; ALLOCATE (D        ,  double, catalog->Nmeasure);
-    float  *RoffGAL   ; ALLOCATE (RoffGAL  ,  float,  catalog->Nmeasure);
-    float  *DoffGAL   ; ALLOCATE (DoffGAL  ,  float,  catalog->Nmeasure);
     float  *M         ; ALLOCATE (M        ,  float,  catalog->Nmeasure);
     float  *Mcal      ; ALLOCATE (Mcal     ,  float,  catalog->Nmeasure);
@@ -407,5 +395,4 @@
     int    *catID     ; ALLOCATE (catID    ,  int  ,  catalog->Nmeasure);
     short  *photcode  ; ALLOCATE (photcode ,  short,  catalog->Nmeasure);
-    ohana_memcheck (1);
 
     // assign the storage arrays
@@ -414,6 +401,4 @@
       R[i]        = measure[i].R        ;
       D[i]        = measure[i].D        ;
-      RoffGAL[i]  = measure[i].RoffGAL  ;
-      DoffGAL[i]  = measure[i].DoffGAL  ;
       M[i]  	  = measure[i].M        ;
       Mcal[i]     = measure[i].Mcal     ;
@@ -433,13 +418,8 @@
       photcode[i] = measure[i].photcode ;
     }
-    ohana_memcheck (1);
 
     // add the columns to the output array
     gfits_set_bintable_column (&theader, &ftable, "RA",   	R,         catalog->Nmeasure);
-    ohana_memcheck (1);
     gfits_set_bintable_column (&theader, &ftable, "DEC",  	D,         catalog->Nmeasure);
-    ohana_memcheck (1);
-    gfits_set_bintable_column (&theader, &ftable, "ROFF_GAL",   RoffGAL,   catalog->Nmeasure);
-    gfits_set_bintable_column (&theader, &ftable, "DOFF_GAL",   DoffGAL,   catalog->Nmeasure);
     gfits_set_bintable_column (&theader, &ftable, "MAG_SYS",  	M,         catalog->Nmeasure);
     gfits_set_bintable_column (&theader, &ftable, "MAG_CAL",  	Mcal,      catalog->Nmeasure);
@@ -461,6 +441,4 @@
     free (R       );
     free (D       );
-    free (RoffGAL );
-    free (DoffGAL );
     free (M       );
     free (Mcal    );
Index: trunk/Ohana/src/relastro/src/ImageOps.c
===================================================================
--- trunk/Ohana/src/relastro/src/ImageOps.c	(revision 39261)
+++ trunk/Ohana/src/relastro/src/ImageOps.c	(revision 39262)
@@ -769,12 +769,4 @@
       ref[i].R += dTime * catalog[c].average[n].uR / 3600.0 / cos(ref[i].D*RAD_DEG);
       ref[i].D += dTime * catalog[c].average[n].uD / 3600.0;
-    }
-
-    // if we are correcting for the Galaxy Motion Model, we assume the mean R,D is at the J2000 epoch position
-    if (0) {
-      myAssert (!isnan(measure[0].RoffGAL), "oops");
-      myAssert (!isnan(measure[0].DoffGAL), "oops");
-      ref[i].R += measure[0].RoffGAL / 3600.0;
-      ref[i].D += measure[0].DoffGAL / 3600.0;
     }
 
Index: trunk/Ohana/src/relphot/src/BrightCatalog.c
===================================================================
--- trunk/Ohana/src/relphot/src/BrightCatalog.c	(revision 39261)
+++ trunk/Ohana/src/relphot/src/BrightCatalog.c	(revision 39262)
@@ -67,4 +67,5 @@
     GET_COLUMN(M,         "MAG_SYS",  	float);
     GET_COLUMN(Mcal,      "MAG_CAL",  	float);
+    GET_COLUMN(Mflat,     "MAG_FLAT",  	float);
     GET_COLUMN(dM,        "MAG_ERR",  	float);
     GET_COLUMN(airmass,   "AIRMASS",  	float);
@@ -89,4 +90,5 @@
       measure[i].M         = M[i];
       measure[i].Mcal      = Mcal[i];
+      measure[i].Mflat     = Mflat[i];
       measure[i].dM        = dM[i];
       measure[i].airmass   = airmass[i];
@@ -109,4 +111,5 @@
     free (M       );
     free (Mcal    );
+    free (Mflat   );
     free (dM      );
     free (airmass );
@@ -269,4 +272,5 @@
     gfits_define_bintable_column (&theader, "E", "MAG_SYS",  "magnitude (sys)",             NULL,    1.0, 0.0);
     gfits_define_bintable_column (&theader, "E", "MAG_CAL",  "magnitude (cal)",             NULL,    1.0, 0.0);
+    gfits_define_bintable_column (&theader, "E", "MAG_FLAT", "magnitude (flat)",            NULL,    1.0, 0.0);
     gfits_define_bintable_column (&theader, "E", "MAG_ERR",  "magnitude (err)",             NULL,    1.0, 0.0);
     gfits_define_bintable_column (&theader, "E", "AIRMASS",  "airmass",                	    NULL,    1.0, 0.0);
@@ -290,4 +294,5 @@
     float  *M         ; ALLOCATE (M        ,  float,  catalog->Nmeasure);
     float  *Mcal      ; ALLOCATE (Mcal     ,  float,  catalog->Nmeasure);
+    float  *Mflat     ; ALLOCATE (Mflat    ,  float,  catalog->Nmeasure);
     float  *dM        ; ALLOCATE (dM       ,  float,  catalog->Nmeasure);
     float  *airmass   ; ALLOCATE (airmass  ,  float,  catalog->Nmeasure);
@@ -310,4 +315,5 @@
       M[i]  	  = measure[i].M        ;
       Mcal[i]     = measure[i].Mcal     ;
+      Mflat[i]    = measure[i].Mflat    ;
       dM[i]       = measure[i].dM       ;
       airmass[i]  = measure[i].airmass  ;
@@ -329,4 +335,5 @@
     gfits_set_bintable_column (&theader, &ftable, "MAG_SYS",  	M,         catalog->Nmeasure);
     gfits_set_bintable_column (&theader, &ftable, "MAG_CAL",  	Mcal,      catalog->Nmeasure);
+    gfits_set_bintable_column (&theader, &ftable, "MAG_FLAT",  	Mflat,     catalog->Nmeasure);
     gfits_set_bintable_column (&theader, &ftable, "MAG_ERR",  	dM,        catalog->Nmeasure);
     gfits_set_bintable_column (&theader, &ftable, "AIRMASS",  	airmass,   catalog->Nmeasure);
@@ -346,4 +353,5 @@
     free (M       );
     free (Mcal    );
+    free (Mflat   );
     free (dM      );
     free (airmass );
Index: trunk/Ohana/src/relphot/src/setMrelCatalog.c
===================================================================
--- trunk/Ohana/src/relphot/src/setMrelCatalog.c	(revision 39261)
+++ trunk/Ohana/src/relphot/src/setMrelCatalog.c	(revision 39262)
@@ -1,3 +1,11 @@
 # include "relphot.h"
+
+/*
+# define TEST_OBJ_ID 0x3ae2
+# define TEST_CAT_ID 0x16c5f
+*/
+# define TEST_OBJ_ID 0
+# define TEST_CAT_ID 0
+
 int markMeasureByRanking (StatDataSet *dataset, Measure *measure, int minrank, DVOMeasureFlags flags);
 
@@ -113,5 +121,5 @@
 
   // option for a test print
-  if (FALSE && (averageT[0].objID == 0x7146) && (averageT[0].catID == 0x49d8)) {
+  if ((averageT[0].objID == TEST_OBJ_ID) && (averageT[0].catID == TEST_CAT_ID)) {
     fprintf (stderr, "test obj\n");
     print_measure_set_alt (average, secfilt, measure);
@@ -353,5 +361,5 @@
 
     if (isSetMrelFinal) {
-      if (FALSE && (average->objID == 0x1) && (average->catID)) {
+      if ((average[0].objID == TEST_OBJ_ID) && (average[0].catID == TEST_CAT_ID)) {
 	fprintf (stderr, "test obj\n");
       }
@@ -723,5 +731,5 @@
 
   // option for a test print
-  if (FALSE && (average[0].objID == 0x0000049c) && (average[0].catID == 0x00016c5e)) {
+  if ((average[0].objID == TEST_OBJ_ID) && (average[0].catID == TEST_CAT_ID)) {
     fprintf (stderr, "test obj\n");
     print_measure_set_alt (average, secfilt, measure);
Index: trunk/Ohana/src/uniphot/Makefile
===================================================================
--- trunk/Ohana/src/uniphot/Makefile	(revision 39261)
+++ trunk/Ohana/src/uniphot/Makefile	(revision 39262)
@@ -1,3 +1,5 @@
-default: uniphot setphot setphot_client setfwhm setposangle setposangle_client setastrom setastrom_client setgalmodel setgalmodel_client fiximids fiximids_client fixstkids fixstkids_client ckids ckids_client
+# default: uniphot setphot setphot_client setfwhm setposangle setposangle_client setastrom setastrom_client setgalmodel setgalmodel_client fiximids fiximids_client fixstkids fixstkids_client ckids ckids_client
+default: uniphot setfwhm setposangle setposangle_client setastrom setastrom_client setgalmodel setgalmodel_client fiximids fiximids_client fixstkids fixstkids_client ckids ckids_client
+
 help:
 @echo "make options: uniphot setphot setfwhm default help install default (uniphot setphot setfwhm)"
@@ -34,5 +36,6 @@
 ckids_client: $(BIN)/ckids_client.$(ARCH)
 
-install: $(DESTBIN)/uniphot $(DESTBIN)/setfwhm $(DESTBIN)/setphot $(DESTBIN)/setphot_client $(DESTBIN)/setposangle $(DESTBIN)/setposangle_client $(DESTBIN)/setastrom $(DESTBIN)/setastrom_client $(DESTBIN)/setgalmodel $(DESTBIN)/setgalmodel_client $(DESTBIN)/fiximids $(DESTBIN)/fiximids_client $(DESTBIN)/fixstkids $(DESTBIN)/fixstkids_client $(DESTBIN)/ckids $(DESTBIN)/ckids_client
+# install: $(DESTBIN)/uniphot $(DESTBIN)/setfwhm $(DESTBIN)/setphot $(DESTBIN)/setphot_client $(DESTBIN)/setposangle $(DESTBIN)/setposangle_client $(DESTBIN)/setastrom $(DESTBIN)/setastrom_client $(DESTBIN)/setgalmodel $(DESTBIN)/setgalmodel_client $(DESTBIN)/fiximids $(DESTBIN)/fiximids_client $(DESTBIN)/fixstkids $(DESTBIN)/fixstkids_client $(DESTBIN)/ckids $(DESTBIN)/ckids_client
+install: $(DESTBIN)/uniphot $(DESTBIN)/setfwhm $(DESTBIN)/setposangle $(DESTBIN)/setposangle_client $(DESTBIN)/setastrom $(DESTBIN)/setastrom_client $(DESTBIN)/setgalmodel $(DESTBIN)/setgalmodel_client $(DESTBIN)/fiximids $(DESTBIN)/fiximids_client $(DESTBIN)/fixstkids $(DESTBIN)/fixstkids_client $(DESTBIN)/ckids $(DESTBIN)/ckids_client
 
 UNIPHOT = \
Index: trunk/Ohana/src/uniphot/include/setphot.h
===================================================================
--- trunk/Ohana/src/uniphot/include/setphot.h	(revision 39261)
+++ trunk/Ohana/src/uniphot/include/setphot.h	(revision 39262)
@@ -17,4 +17,25 @@
   unsigned int flags;
 } ImageSubset;
+
+// we have one correction (an image) for each filter and chip
+typedef struct {
+  int Nx;       // number of chips in x
+  int Ny;       // number of chips in y
+  int Nfilter;  // number of filters
+  int Nseason;  // number of correction peridos
+
+  int Nchips;	// chip offset (Nx*Ny)
+  int Nflats;	// season offset (Nx*Ny*Nfilters)
+  int Nvalues;  // Nx*Ny*Nfilters*Nseason
+
+  int dX;       // superpixel size
+  int dY;	// superpixel size
+
+  int NxCCD;	// number of pixels
+  int NyCCD;	// number of pixels
+
+  Matrix **matrix; // allocate an array of pointers
+  // index = ix + iy*Nx + filter*Nchips + dir*Ngroup
+} CamCorrection;
 
 /* global variables set in parameter file */
Index: trunk/Ohana/src/uniphot/src/cam_correction.c
===================================================================
--- trunk/Ohana/src/uniphot/src/cam_correction.c	(revision 39261)
+++ trunk/Ohana/src/uniphot/src/cam_correction.c	(revision 39262)
@@ -29,6 +29,6 @@
 
   int Ndir, Nfilter, Nx, Ny, dX, dY, NxCCD, NyCCD;
-  if (!gfits_scan (&header, "NDIR",    "%d", 1, &Ndir))    return FALSE;
-  if (!gfits_scan (&header, "NFILTER", "%d", 1, &Nfilter)) return FALSE;
+  if (!gfits_scan (&header, "NDIR",     "%d", 1, &Ndir))    return FALSE;
+  if (!gfits_scan (&header, "NFILTER",  "%d", 1, &Nfilter)) return FALSE;
   if (!gfits_scan (&header, "NX", 	"%d", 1, &Nx)) 	    return FALSE;
   if (!gfits_scan (&header, "NY", 	"%d", 1, &Ny)) 	    return FALSE;
@@ -61,5 +61,4 @@
   }
 
-  int found = FALSE;
   while (TRUE) {
     Header theader;
@@ -90,8 +89,4 @@
     // assert that cam->matrix[index] is NULL?
     cam->matrix[index] = matrix;
-
-    if (!found) {
-      
-    }
   }
   return TRUE;
Index: trunk/Ohana/src/uniphot/src/initialize_setphot.c
===================================================================
--- trunk/Ohana/src/uniphot/src/initialize_setphot.c	(revision 39261)
+++ trunk/Ohana/src/uniphot/src/initialize_setphot.c	(revision 39262)
@@ -33,4 +33,17 @@
 
   int N;
+
+  CAM_FILE = NULL;
+  if ((N = get_argument (argc, argv, "-CAM"))) {
+    remove_argument (N, &argc, argv);
+    char *tmpfile = strcreate (argv[N]);
+    CAM_FILE = abspath (tmpfile, DVO_MAX_PATH);
+    remove_argument (N, &argc, argv);
+  }
+  CAM_RESET = FALSE;
+  if ((N = get_argument (argc, argv, "-CAM-reset"))) {
+    remove_argument (N, &argc, argv);
+    CAM_RESET = TRUE;
+  }
 
   VERBOSE = FALSE;
Index: trunk/Ohana/src/uniphot/src/initialize_setphot_client.c
===================================================================
--- trunk/Ohana/src/uniphot/src/initialize_setphot_client.c	(revision 39261)
+++ trunk/Ohana/src/uniphot/src/initialize_setphot_client.c	(revision 39262)
@@ -26,4 +26,17 @@
 
   int N;
+
+  CAM_FILE = NULL;
+  if ((N = get_argument (argc, argv, "-CAM"))) {
+    remove_argument (N, &argc, argv);
+    char *tmpfile = strcreate (argv[N]);
+    CAM_FILE = abspath (tmpfile, DVO_MAX_PATH);
+    remove_argument (N, &argc, argv);
+  }
+  CAM_RESET = FALSE;
+  if ((N = get_argument (argc, argv, "-CAM-reset"))) {
+    remove_argument (N, &argc, argv);
+    CAM_RESET = TRUE;
+  }
 
   VERBOSE = FALSE;
Index: trunk/Ohana/src/uniphot/src/setphot.c
===================================================================
--- trunk/Ohana/src/uniphot/src/setphot.c	(revision 39261)
+++ trunk/Ohana/src/uniphot/src/setphot.c	(revision 39262)
@@ -8,5 +8,6 @@
   ZptTable *zpts;
   Image *image;
-  FlatCorrectionTable flatcorrTable;
+
+  // FlatCorrectionTable flatcorrTable;
 
   /* get configuration info, args, lockfile */
@@ -33,4 +34,22 @@
   }
 
+  // if supplied, read in the camera-static flat-field correction
+  if (CAM_FILE) {
+    if (!load_cam_correction (CAM_FILE)) {
+      fprintf (stderr, "failed to load camera-static flat-field correction\n");
+      exit (2);
+    }
+    merge_flatcorr_with_cam (flatcorrTable);
+  } else {
+    convert_flatcorr_to_cam (flatcorrTable);
+  }
+
+  { 
+    char newflatfile[DVO_MAX_PATH];
+    int size = snprintf (newflatfile, DVO_MAX_PATH, "%s/flatfield.fits", CATDIR);
+    assert (size < DVO_MAX_PATH);
+    save_cam_correction (newflatfile);
+  }
+
   if (!zpts) Shutdown ("failed to load zero points, or empty table");
 
@@ -42,9 +61,11 @@
 
   if (UBERCAL) {
-    match_flatcorr_to_images (image, Nimage, &flatcorrTable);
+    // we are going to deprecate the flatcorr imaage lookup
+    // match_flatcorr_to_images (image, Nimage, &flatcorrTable);
+    match_camcorr_to_images (image, Nimage);
   } 
 
   if (!IMAGES_ONLY) {
-    status = update_dvo_setphot (image, Nimage, &flatcorrTable);
+    status = update_dvo_setphot (image, Nimage);
   }
 
Index: trunk/Ohana/src/uniphot/src/update_catalog_setphot.c
===================================================================
--- trunk/Ohana/src/uniphot/src/update_catalog_setphot.c	(revision 39261)
+++ trunk/Ohana/src/uniphot/src/update_catalog_setphot.c	(revision 39262)
@@ -1,5 +1,5 @@
 # include "setphot.h"
 
-void update_catalog_setphot (Catalog *catalog, Image *image, off_t *index, off_t Nimage, FlatCorrectionTable *flatcorr) {
+void update_catalog_setphot (Catalog *catalog, Image *image, off_t *index, off_t Nimage) {
 
   off_t i, j, found;
@@ -7,4 +7,6 @@
   // if we are resetting, reset all flags
   DVOMeasureFlags photomFlags = ID_MEAS_POOR_PHOTOM | ID_MEAS_SKIP_PHOTOM | ID_MEAS_PHOTOM_UBERCAL;
+
+  CamCorrection *camcorr = get_cam_correction_ptr ();  
 
   found = 0;    
@@ -21,13 +23,14 @@
       float Mcal = image[id].Mcal;
       float dMcal = image[id].dMcal;
-      float Mcal_offset = 0.0;
+      float Mflat = 0.0;
 
       // if we know about a flat-field correction, then we need to apply the sub-chip correction
       int flat_id = image[id].photom_map_id;
       if (flat_id > 0) {
-	  Mcal_offset = FlatCorrectionOffset (flatcorr, flat_id, catalog[0].measure[m].Xccd, catalog[0].measure[m].Yccd);
+	Mflat = CamCorrectionOffset (camcorr, flat_id, catalog[0].measure[m].Xccd, catalog[0].measure[m].Yccd);
       }
 
-      catalog[0].measure[m].Mcal = Mcal - Mcal_offset;
+      catalog[0].measure[m].Mcal = Mcal;
+      catalog[0].measure[m].Mflat = Mflat;
       catalog[0].measure[m].dMcal = dMcal;
       myAssert(isfinite(catalog[0].measure[m].Mcal), "oops: ubercal made a nan");
