Index: trunk/Ohana/src/uniphot/src/cam_zpt_correction.c
===================================================================
--- trunk/Ohana/src/uniphot/src/cam_zpt_correction.c	(revision 39265)
+++ trunk/Ohana/src/uniphot/src/cam_zpt_correction.c	(revision 39267)
@@ -87,5 +87,6 @@
 
   while (TRUE) {
-    Header theader;
+    Header *header = NULL;
+    ALLOCATE (header, H
 
     // load data for this header : if not found, assume we hit the end of the file
@@ -113,5 +114,54 @@
     // assert that cam->matrix[index] is NULL?
     cam->matrix[index] = matrix;
-  }
+    cam->header[index] = header;
+  }
+  return TRUE;
+}
+
+int save_cam_correction (char *filename) {
+
+  int i, ix, iy, dir, filter;
+  Header header;
+
+  /* open file for input */
+  FILE *f = fopen (filename, "r");
+  if (f == (FILE *) NULL) {
+    gprint (GP_ERR, "can't open file for read : %s\n", filename);
+    return FALSE;
+  }
+
+  gfits_init_header (&header);
+  header.extend = TRUE;
+  gfits_create_header (&header);
+  gfits_create_matrix (&header, &matrix);
+
+  gfits_modify (&header, "NSEASON",  "%d", 1, camcoor->Nseason);
+  gfits_modify (&header, "NFILTER",  "%d", 1, camcoor->Nfilter);
+  gfits_modify (&header, "NX", 	     "%d", 1, camcoor->Nx);
+  gfits_modify (&header, "NY", 	     "%d", 1, camcoor->Ny);
+  gfits_modify (&header, "DX", 	     "%d", 1, camcoor->dX);
+  gfits_modify (&header, "DY", 	     "%d", 1, camcoor->dY);
+  gfits_modify (&header, "NX_CHIP",  "%d", 1, camcoor->NxCCD);
+  gfits_modify (&header, "NY_CHIP",  "%d", 1, camcoor->NyCCD);
+
+  gfits_fwrite_header  (f, &header);
+  gfits_fwrite_matrix  (f, &matrix);
+  gfits_free_header (&header);
+  gfits_free_matrix (&matrix);
+
+  for (i = 0; i < camcorr->Nvalues; i++) {
+
+    // I don't need to do this, do I?
+    // if (!gfits_scan (&theader, "FILTER", "%d", 1, &filter)); 
+    // if (!gfits_scan (&theader, "SEASON", "%d", 1, &season)); 
+    // if (!gfits_scan (&theader, "X_CHIP",  "%d", 1, &ix));    
+    // if (!gfits_scan (&theader, "Y_CHIP",  "%d", 1, &iy));    
+
+    if (!cam->matrix[i]) continue;
+
+    gfits_fwrite_header  (f, whatHeader);
+    gfits_fwrite_matrix  (f, cam->matrix[i]);
+  }
+
   return TRUE;
 }
@@ -239,4 +289,5 @@
     myAssert (flatcorrTable->IDtoSeq[ID] == i, "oops");
 
+    // find the season (these are inherited from flatcorr, so they must match
     int season = -1;
     for (j = 0; j < newcorr->Nseason; j++) {
@@ -248,12 +299,16 @@
     int photcode = flatcorr->image[i].photcode;
 
+    // find the chip and filter from photcode:
     iy = flatcorr->image[i].photcode % 10;
     ix = (int)(flatcorr->image[i].photcode / 10) % 10;
     filter = (int)(flatcorr->image[i].photcode / 100) % 10;
 	      
+    // seq is the flatcorr entry of interest
     int seq = ix + iy * newcorr->Nx + filter * newcorr->Nchips + season * camcorr->Nflat;
 
     // we now have newcorr->matrix[seq]
-    newcorr->matrix[seq];
+    Matrix *newDelta = newcorr->matrix[seq];
+
+    int jx, jy;
 
     // now loop over the newcorr pixels to get the flatcorr value:
@@ -261,7 +316,11 @@
       for (jy = 0; jy < newcorr->NyCCD; jy++) {
 	
-	// convert jx,jy in newcorr pixels to Jx,Jy in flatcorr pixels
-	newcorr->matrix[seq]->buffer[jx + jy*NxCCD] += flatcorr->offset[i][Jx][Jy];
-
+	int chipX = jx * newcorr->dX;
+	int chipY = jy * newcorr->dY;
+
+	// get the flatcorr offset from this chip coordinate
+	float delta = FlatCorrectionOffset (flatcorr, flatcorr->image[i].ID, chipX, chipY);
+
+	newDelta->buffer[jx + jy*NxCCD] += flatcorr->offset[i][Jx][Jy];
       }
     }
@@ -271,2 +330,3 @@
 }
 
+
Index: trunk/Ohana/src/uniphot/src/setphot.c
===================================================================
--- trunk/Ohana/src/uniphot/src/setphot.c	(revision 39265)
+++ trunk/Ohana/src/uniphot/src/setphot.c	(revision 39267)
@@ -30,10 +30,5 @@
     FlatCorrectionSave(&flatcorrTable, flatcorrfile);
     // XXX should this program save any old copy of this file?
-  } else {
-    zpts = load_zpt_table (argv[1], &Nzpts);
-  }
 
-  // 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");
@@ -41,13 +36,11 @@
     }
     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);
+  } else {
+    zpts = load_zpt_table (argv[1], &Nzpts);
   }
 
