Index: trunk/Ohana/src/uniphot/Makefile
===================================================================
--- trunk/Ohana/src/uniphot/Makefile	(revision 39287)
+++ trunk/Ohana/src/uniphot/Makefile	(revision 39288)
@@ -1,4 +1,3 @@
-# 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
+default: uniphot setphot setphot_client setfwhm setposangle setposangle_client setastrom setastrom_client setgalmodel setgalmodel_client fiximids fiximids_client fixstkids fixstkids_client ckids ckids_client
 
 help:
@@ -36,6 +35,5 @@
 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)/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
 
 UNIPHOT = \
@@ -70,6 +68,11 @@
 $(SRC)/match_camcorr_to_images.$(ARCH).o	    \
 $(SRC)/cam_zpt_correction.$(ARCH).o	    \
+$(SRC)/cam_correction.$(ARCH).o \
+$(SRC)/dcr_correction.$(ARCH).o \
+$(SRC)/kh_correction.$(ARCH).o \
+$(SRC)/astrom_correction.$(ARCH).o \
 $(SRC)/update_dvo_setphot.$(ARCH).o \
 $(SRC)/update_catalog_setphot.$(ARCH).o \
+$(SRC)/update_catalog_setastrom.$(ARCH).o \
 $(SRC)/repair_catalog_by_objID.$(ARCH).o \
 $(SRC)/SetSignals.$(ARCH).o	    \
@@ -83,6 +86,12 @@
 $(SRC)/update_dvo_setphot.$(ARCH).o \
 $(SRC)/update_catalog_setphot.$(ARCH).o \
+$(SRC)/update_catalog_setastrom.$(ARCH).o \
 $(SRC)/repair_catalog_by_objID.$(ARCH).o \
 $(SRC)/initialize_setphot_client.$(ARCH).o \
+$(SRC)/cam_zpt_correction.$(ARCH).o	    \
+$(SRC)/cam_correction.$(ARCH).o \
+$(SRC)/dcr_correction.$(ARCH).o \
+$(SRC)/kh_correction.$(ARCH).o \
+$(SRC)/astrom_correction.$(ARCH).o \
 $(SRC)/SetSignals.$(ARCH).o	    \
 $(SRC)/Shutdown.$(ARCH).o	    \
Index: trunk/Ohana/src/uniphot/include/setastrom.h
===================================================================
--- trunk/Ohana/src/uniphot/include/setastrom.h	(revision 39287)
+++ trunk/Ohana/src/uniphot/include/setastrom.h	(revision 39288)
@@ -30,5 +30,5 @@
   Matrix **matrix; // allocate an array of pointers
   // index = ix + iy*Nx + filter*Nchips + dir*Ngroup
-} CamCorrection;
+} CamAstromCorrection;
 
 /* global variables set in parameter file */
@@ -48,6 +48,7 @@
 char        *KH_FILE;
 char        *DCR_FILE;
-char        *CAM_FILE;
 char        *TYC_FILE;
+
+char        *CAM_ASTROM_FILE;
 
 int          KH_RESET;
@@ -91,6 +92,6 @@
 int get_dcr_correction (int filter, double *dX, double *dY, float Color);
 
-int load_cam_correction (char *filename);
-int get_cam_correction (int chipID, int filter, float Xccd, float Yccd, double *dX, double *dY);
+int CamAstromCorrectionLoad (char *filename);
+int CamAstromCorrectionValue (int chipID, int filter, float Xccd, float Yccd, double *dX, double *dY);
 
 int load_tyc_correction (char *filename);
Index: trunk/Ohana/src/uniphot/include/setphot.h
===================================================================
--- trunk/Ohana/src/uniphot/include/setphot.h	(revision 39287)
+++ trunk/Ohana/src/uniphot/include/setphot.h	(revision 39288)
@@ -43,5 +43,5 @@
   
   // index = ix + iy*Nx + filter*Nchips + dir*Ngroup
-} CamCorrection;
+} CamPhotomCorrection;
 
 /* global variables set in parameter file */
@@ -53,5 +53,14 @@
 char        *IMAGES;
 char        *SINGLE_CPT;
-char        *CAM_FILE;
+
+char        *KH_FILE;
+char        *DCR_FILE;
+char        *CAM_PHOTOM_FILE;
+char        *CAM_ASTROM_FILE;
+
+int          KH_RESET;
+int          DCR_RESET;
+int          CAM_RESET;
+
 int          VERBOSE;
 int          RESET;
@@ -96,6 +105,6 @@
 Image        *load_images_setphot    PROTO((FITS_DB *db, off_t *Nimage));
 int           match_zpts_to_images   PROTO((Image *image, off_t Nimage, ZptTable *zpts, int Nzpts));
-int           update_dvo_setphot     PROTO((Image *image, off_t Nimage, CamCorrection *camcorr));
-void          update_catalog_setphot PROTO((Catalog *catalog, Image *image, off_t *index, off_t Nimage, CamCorrection *camcorr));
+int           update_dvo_setphot     PROTO((Image *image, off_t Nimage, CamPhotomCorrection *camcorr));
+void          update_catalog_setphot PROTO((Catalog *catalog, Image *image, off_t *index, off_t Nimage, CamPhotomCorrection *camcorr));
 void          update_catalog_setphot_client PROTO((Catalog *catalog, ImageSubset *image, off_t *index, off_t Nimage, FlatCorrectionTable *flatcorr));
 
@@ -109,11 +118,25 @@
 int           repair_catalog_by_objID     PROTO((Catalog *catalog));
 
-void           free_cam_correction          PROTO((CamCorrection *cam));
-CamCorrection *alloc_cam_correction         PROTO((int Nx, int Ny, int Nfilter, int Nseason));
-CamCorrection *load_cam_correction          PROTO((char *filename));
-int            save_cam_correction          PROTO((CamCorrection *cam, char *filename));
-// int            get_cam_correction        PROTO((int chipID, int filter, float Xccd, float Yccd, double *dX, double *dY));
-float          get_cam_correction           PROTO((CamCorrection *cam, int flat_id, float Xccd, float Yccd));
-CamCorrection *merge_flatcorr_and_camcorr   PROTO((FlatCorrectionTable *flatcorr, CamCorrection *camcorr));
+void          CamPhotomCorrectionFree            PROTO((CamPhotomCorrection *cam));
+CamPhotomCorrection *CamPhotomCorrectionAlloc    PROTO((int Nx, int Ny, int Nfilter, int Nseason));
+CamPhotomCorrection *CamPhotomCorrectionLoad     PROTO((char *filename));
+int            CamPhotomCorrectionSave           PROTO((CamPhotomCorrection *cam, char *filename));
+float          CamPhotomCorrectionValue          PROTO((CamPhotomCorrection *cam, int flat_id, float Xccd, float Yccd));
 
-int            match_camcorr_to_images      PROTO((Image *image, off_t Nimage, CamCorrection *camcorr));
+int            match_camcorr_to_images      PROTO((Image *image, off_t Nimage, CamPhotomCorrection *camcorr));
+CamPhotomCorrection *merge_flatcorr_and_camcorr   PROTO((FlatCorrectionTable *flatcorr, CamPhotomCorrection *camcorr));
+
+// spline correction functions
+double spline_apply_dbl (double *x, double *y, double *y2, int N, double X);
+
+int load_kh_correction (char *filename);
+int get_kh_correction (int sub, int chip, double *dX, double *dY, float Minst);
+
+int load_dcr_correction (char *filename);
+int get_dcr_correction (int filter, double *dX, double *dY, float Color);
+
+int CamAstromCorrectionLoad (char *filename);
+int CamAstromCorrectionValue (int chipID, int filter, float Xccd, float Yccd, double *dX, double *dY);
+
+
+int           update_catalog_setastrom        PROTO((Catalog *catalog));
Index: trunk/Ohana/src/uniphot/src/cam_correction.c
===================================================================
--- trunk/Ohana/src/uniphot/src/cam_correction.c	(revision 39287)
+++ trunk/Ohana/src/uniphot/src/cam_correction.c	(revision 39288)
@@ -6,7 +6,7 @@
 // note that we are allocating pointers for XY00 - XY77, but only the octal elements are set (and not the 00,07,70,77 ones)
 
-static CamCorrection *cam = NULL;
+static CamAstromCorrection *cam = NULL;
 
-int load_cam_correction (char *filename) {
+int CamAstromCorrectionLoad (char *filename) {
 
   int i, ix, iy, dir, filter;
@@ -20,5 +20,5 @@
   }
 
-  ALLOCATE (cam, CamCorrection, 1);
+  ALLOCATE (cam, CamAstromCorrection, 1);
 
   // read the PHU header
@@ -96,5 +96,5 @@
 // chipID == octal chip ID (eg 40 for XY40)
 // filter == (01234 = grizy)
-int get_cam_correction (int chipID, int filter, float Xccd, float Yccd, double *dX, double *dY) {
+int CamAstromCorrectionValue (int chipID, int filter, float Xccd, float Yccd, double *dX, double *dY) {
 
   *dX = 0.0;
Index: trunk/Ohana/src/uniphot/src/cam_zpt_correction.c
===================================================================
--- trunk/Ohana/src/uniphot/src/cam_zpt_correction.c	(revision 39287)
+++ trunk/Ohana/src/uniphot/src/cam_zpt_correction.c	(revision 39288)
@@ -9,5 +9,5 @@
 // note that we are allocating pointers for XY00 - XY77, but only the octal elements are set (and not the 00,07,70,77 ones)
 
-void free_cam_correction (CamCorrection *cam) {
+void CamPhotomCorrectionFree (CamPhotomCorrection *cam) {
 
   if (!cam) return;
@@ -28,9 +28,9 @@
 }
 
-CamCorrection *alloc_cam_correction (int Nx, int Ny, int Nfilter, int Nseason) {
-
-  CamCorrection *cam;
-
-  ALLOCATE (cam, CamCorrection, 1);
+CamPhotomCorrection *CamPhotomCorrectionAlloc (int Nx, int Ny, int Nfilter, int Nseason) {
+
+  CamPhotomCorrection *cam;
+
+  ALLOCATE (cam, CamPhotomCorrection, 1);
 
   cam->phu = NULL;
@@ -59,5 +59,5 @@
 }
 
-CamCorrection *load_cam_correction (char *filename) {
+CamPhotomCorrection *CamPhotomCorrectionLoad (char *filename) {
 
   int i, ix, iy, season, filter;
@@ -88,5 +88,5 @@
   if (!gfits_scan (phu, "NY_CHIP", "%d", 1, &NyCCD))   return FALSE;
 
-  CamCorrection *cam = alloc_cam_correction (Nx, Ny, Nfilter, Nseason);
+  CamPhotomCorrection *cam = CamPhotomCorrectionAlloc (Nx, Ny, Nfilter, Nseason);
   cam->phu = phu;
 
@@ -140,5 +140,5 @@
 }
 
-int save_cam_correction (CamCorrection *cam, char *filename) {
+int CamPhotomCorrectionSave (CamPhotomCorrection *cam, char *filename) {
 
   int i;
@@ -170,56 +170,5 @@
 }
 
-# if (0)
-// input is:
-// chipID == octal chip ID (eg 40 for XY40)
-// filter == (01234 = grizy)
-int get_cam_correction (CamCorrection *cam, int chipID, int filter, float Xccd, float Yccd, double *dX, double *dY) {
-
-  *dX = 0.0;
-  *dY = 0.0;
-
-  int index, jx, jy, NxModel;
-  Matrix *matrix;
-  float *buffer, value;
-
-  // split out the chipID number (eg 43 for XY43) into X and Y elements
-  int ix = (int) (chipID / 10);
-  int iy = (int) (chipID % 10);
-
-  // dX (dir == 0)
-  index = ix + iy*cam->Nx + filter*cam->Nchips;
-
-  matrix = cam->matrix[index];
-  NxModel = cam->matrix[index]->Naxis[0];
-
-  buffer = (float *) matrix->buffer;
-
-  jx = MAX(MIN(cam->NxCCD, Xccd / cam->dX), 0);
-  jy = MAX(MIN(cam->NyCCD, Yccd / cam->dY), 0);
-
-  value = buffer[jx + jy*NxModel];
-
-  *dX = isnan(value) ? 0.0 : value;
-
-  // dY (dir == 1) uses the next block
-  index += cam->Ngroup;
-
-  matrix = cam->matrix[index];
-  NxModel = cam->matrix[index]->Naxis[0];
-
-  buffer = (float *) matrix->buffer;
-
-  jx = MAX(MIN(cam->NxCCD, Xccd / cam->dX), 0);
-  jy = MAX(MIN(cam->NyCCD, Yccd / cam->dY), 0);
-
-  value = buffer[jx + jy*NxModel];
-
-  *dY = isnan(value) ? 0.0 : value;
-
-  return TRUE;
-}
-# endif
-
-float get_cam_correction (CamCorrection *cam, int flat_id, float Xccd, float Yccd) {
+float CamPhotomCorrectionValue (CamPhotomCorrection *cam, int flat_id, float Xccd, float Yccd) {
 
   if (!flat_id) return 0.0;
@@ -249,5 +198,5 @@
 }
 
-CamCorrection *merge_flatcorr_and_camcorr (FlatCorrectionTable *flatcorr, CamCorrection *camcorr) {
+CamPhotomCorrection *merge_flatcorr_and_camcorr (FlatCorrectionTable *flatcorr, CamPhotomCorrection *camcorr) {
 
   int i, j, ix, iy, Nf, Ns;
@@ -260,6 +209,6 @@
   myAssert (camcorr->Nseason == 1, "no code to merge seasons flats to seasonal flats");
 
-  // we need to create a new CamCorrection with Nx,Ny,Nfilter from camcorr and Nseason from flatcorr:
-  CamCorrection *newcorr = alloc_cam_correction (camcorr->Nx, camcorr->Ny, camcorr->Nfilter, flatcorr->Nseason);
+  // we need to create a new CamPhotomCorrection with Nx,Ny,Nfilter from camcorr and Nseason from flatcorr:
+  CamPhotomCorrection *newcorr = CamPhotomCorrectionAlloc (camcorr->Nx, camcorr->Ny, camcorr->Nfilter, flatcorr->Nseason);
   newcorr->phu = gfits_alloc_header();
   gfits_create_header(newcorr->phu);
Index: trunk/Ohana/src/uniphot/src/dcr_correction.c
===================================================================
--- trunk/Ohana/src/uniphot/src/dcr_correction.c	(revision 39287)
+++ trunk/Ohana/src/uniphot/src/dcr_correction.c	(revision 39288)
@@ -26,5 +26,4 @@
 
   char type[16];
-  char extname[80];
 
   Header header;
@@ -67,10 +66,19 @@
     if (!gfits_fread_ftable_data (f, &ftable, FALSE)) return (FALSE);
     
-    if (!gfits_scan (&theader, "EXTNAME", "%s", 1, extname)) return (FALSE);
+    char word[80];
+    if (!gfits_scan (&theader, "EXTNAME", "%s", 1, word)) return (FALSE);
+    char *extname = strcreate(word);
 
     // figure out the filter and direction from the EXTNAME
+    // EXTNAME is of the form DCR.[grizy].dP[xy]
 
-    // EXTNAME is of the form [grizy].[xy].v0 where 
-    char filtname = extname[0];
+    char *ptr = NULL;
+    char *w0 = strtok_r (extname, ".", &ptr); myAssert (w0, "invalid dcr file");
+    char *w1 = strtok_r (NULL, ".", &ptr); myAssert (w1, "invalid dcr file");
+    char *w2 = strtok_r (NULL, ".", &ptr); myAssert (w2, "invalid dcr file");
+    
+    myAssert (!strcmp(w0, "DCR"), "invalid dcr file");
+
+    char filtname = w1[0];
     int filtnum = -1;
     switch (filtname) {
@@ -86,8 +94,8 @@
 
     int dir = -1;
-    if (extname[2] == 'x') {
+    if (w2[2] == 'x') {
       dir = 0;
     }
-    if (extname[2] == 'y') {
+    if (w2[2] == 'y') {
       dir = 1;
     }
Index: trunk/Ohana/src/uniphot/src/initialize_setastrom.c
===================================================================
--- trunk/Ohana/src/uniphot/src/initialize_setastrom.c	(revision 39287)
+++ trunk/Ohana/src/uniphot/src/initialize_setastrom.c	(revision 39288)
@@ -79,9 +79,9 @@
   }
 
-  CAM_FILE = NULL;
+  CAM_ASTROM_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);
+    CAM_ASTROM_FILE = abspath (tmpfile, DVO_MAX_PATH);
     remove_argument (N, &argc, argv);
   }
@@ -174,5 +174,5 @@
   }
 
-  if (!KH_FILE && !DCR_FILE && !CAM_FILE && !TYC_FILE) {
+  if (!KH_FILE && !DCR_FILE && !CAM_ASTROM_FILE && !TYC_FILE) {
     fprintf (stderr, "WARNING: none of -CAM, -KH, -DCR, -TYC supplied\n");
   }
@@ -252,8 +252,8 @@
   }
 
-  CAM_FILE = NULL;
+  CAM_ASTROM_FILE = NULL;
   if ((N = get_argument (argc, argv, "-CAM"))) {
     remove_argument (N, &argc, argv);
-    CAM_FILE = strcreate (argv[N]);
+    CAM_ASTROM_FILE = strcreate (argv[N]);
     remove_argument (N, &argc, argv);
   }
@@ -336,5 +336,5 @@
   if (!CATDIR) usage_setastrom_client();
 
-  if (!KH_FILE && !DCR_FILE && !CAM_FILE && !TYC_FILE) {
+  if (!KH_FILE && !DCR_FILE && !CAM_ASTROM_FILE && !TYC_FILE) {
     fprintf (stderr, "WARNING: none of -CAM, -KH, -DCR, -TYC supplied\n");
   }
Index: trunk/Ohana/src/uniphot/src/initialize_setphot.c
===================================================================
--- trunk/Ohana/src/uniphot/src/initialize_setphot.c	(revision 39287)
+++ trunk/Ohana/src/uniphot/src/initialize_setphot.c	(revision 39288)
@@ -34,9 +34,48 @@
   int N;
 
-  CAM_FILE = NULL;
+  KH_FILE = NULL;
+  if ((N = get_argument (argc, argv, "-KH"))) {
+    remove_argument (N, &argc, argv);
+    char *tmpfile = strcreate (argv[N]);
+    KH_FILE = abspath (tmpfile, DVO_MAX_PATH);
+    remove_argument (N, &argc, argv);
+  }
+  KH_RESET = FALSE;
+  if ((N = get_argument (argc, argv, "-KH-reset"))) {
+    remove_argument (N, &argc, argv);
+    KH_RESET = TRUE;
+  }
+
+  DCR_FILE = NULL;
+  if ((N = get_argument (argc, argv, "-DCR"))) {
+    remove_argument (N, &argc, argv);
+    char *tmpfile = strcreate (argv[N]);
+    DCR_FILE = abspath (tmpfile, DVO_MAX_PATH);
+    remove_argument (N, &argc, argv);
+  }
+  DCR_RESET = FALSE;
+  if ((N = get_argument (argc, argv, "-DCR-reset"))) {
+    remove_argument (N, &argc, argv);
+    DCR_RESET = TRUE;
+  }
+
+  CAM_ASTROM_FILE = NULL;
+  if ((N = get_argument (argc, argv, "-CAM"))) {
+    remove_argument (N, &argc, argv);
+    char *tmpfile = strcreate (argv[N]);
+    CAM_ASTROM_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;
+  }
+
+  CAM_PHOTOM_FILE = NULL;
   if ((N = get_argument (argc, argv, "-cam-flat"))) {
     remove_argument (N, &argc, argv);
     char *tmpfile = strcreate (argv[N]);
-    CAM_FILE = abspath (tmpfile, DVO_MAX_PATH);
+    CAM_PHOTOM_FILE = abspath (tmpfile, DVO_MAX_PATH);
     remove_argument (N, &argc, argv);
   }
@@ -126,5 +165,5 @@
     remove_argument (N, &argc, argv);
     UBERCAL = TRUE;
-    if (!CAM_FILE) {
+    if (!CAM_PHOTOM_FILE) {
       fprintf (stderr, "ubercal now requires a high-res static flat-file: -cam-flat filename\n");
       exit (2);
Index: trunk/Ohana/src/uniphot/src/initialize_setphot_client.c
===================================================================
--- trunk/Ohana/src/uniphot/src/initialize_setphot_client.c	(revision 39287)
+++ trunk/Ohana/src/uniphot/src/initialize_setphot_client.c	(revision 39288)
@@ -27,9 +27,35 @@
   int N;
 
-  CAM_FILE = NULL;
+  KH_FILE = NULL;
+  if ((N = get_argument (argc, argv, "-KH"))) {
+    remove_argument (N, &argc, argv);
+    char *tmpfile = strcreate (argv[N]);
+    KH_FILE = abspath (tmpfile, DVO_MAX_PATH);
+    remove_argument (N, &argc, argv);
+  }
+  KH_RESET = FALSE;
+  if ((N = get_argument (argc, argv, "-KH-reset"))) {
+    remove_argument (N, &argc, argv);
+    KH_RESET = TRUE;
+  }
+
+  DCR_FILE = NULL;
+  if ((N = get_argument (argc, argv, "-DCR"))) {
+    remove_argument (N, &argc, argv);
+    char *tmpfile = strcreate (argv[N]);
+    DCR_FILE = abspath (tmpfile, DVO_MAX_PATH);
+    remove_argument (N, &argc, argv);
+  }
+  DCR_RESET = FALSE;
+  if ((N = get_argument (argc, argv, "-DCR-reset"))) {
+    remove_argument (N, &argc, argv);
+    DCR_RESET = TRUE;
+  }
+
+  CAM_ASTROM_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);
+    CAM_ASTROM_FILE = abspath (tmpfile, DVO_MAX_PATH);
     remove_argument (N, &argc, argv);
   }
@@ -39,4 +65,14 @@
     CAM_RESET = TRUE;
   }
+
+  CAM_PHOTOM_FILE = NULL;
+/*
+  if ((N = get_argument (argc, argv, "-cam-flat"))) {
+    remove_argument (N, &argc, argv);
+    char *tmpfile = strcreate (argv[N]);
+    CAM_PHOTOM_FILE = abspath (tmpfile, DVO_MAX_PATH);
+    remove_argument (N, &argc, argv);
+  }
+*/
 
   VERBOSE = FALSE;
Index: trunk/Ohana/src/uniphot/src/match_camcorr_to_images.c
===================================================================
--- trunk/Ohana/src/uniphot/src/match_camcorr_to_images.c	(revision 39287)
+++ trunk/Ohana/src/uniphot/src/match_camcorr_to_images.c	(revision 39288)
@@ -4,9 +4,9 @@
 // the photcode is used to find the actual flat correction
 
-int match_camcorr_to_images (Image *image, off_t Nimage, CamCorrection *camcorr) {
+int match_camcorr_to_images (Image *image, off_t Nimage, CamPhotomCorrection *camcorr) {
 
   int i, j;
 
-  // we have an array of CamCorrection->matrix values, where each matrix is a flat-field image
+  // we have an array of CamPhotomCorrection->matrix values, where each matrix is a flat-field image
   // the sequence is seq = ix + Nx_chip*iy + filter*Nchips + season*(Nchips*Nfilter)
 
Index: trunk/Ohana/src/uniphot/src/setphot.c
===================================================================
--- trunk/Ohana/src/uniphot/src/setphot.c	(revision 39287)
+++ trunk/Ohana/src/uniphot/src/setphot.c	(revision 39288)
@@ -8,5 +8,5 @@
   ZptTable *zpts;
 
-  CamCorrection *camcorr = NULL;
+  CamPhotomCorrection *camcorr = NULL;
   FlatCorrectionTable flatcorrTable;
 
@@ -31,5 +31,5 @@
     // XXX should this program save any old copy of this file?
 
-    CamCorrection *rawcorr = load_cam_correction (CAM_FILE);
+    CamPhotomCorrection *rawcorr = CamPhotomCorrectionLoad (CAM_PHOTOM_FILE);
     if (!rawcorr) {
       fprintf (stderr, "failed to load camera-static flat-field correction\n");
@@ -41,5 +41,5 @@
     size = snprintf (newflatfile, DVO_MAX_PATH, "%s/flatfield.fits", CATDIR);
     assert (size < DVO_MAX_PATH);
-    save_cam_correction (camcorr, newflatfile);
+    CamPhotomCorrectionSave (camcorr, newflatfile);
   } else {
     zpts = load_zpt_table (argv[1], &Nzpts);
Index: trunk/Ohana/src/uniphot/src/setphot_client.c
===================================================================
--- trunk/Ohana/src/uniphot/src/setphot_client.c	(revision 39287)
+++ trunk/Ohana/src/uniphot/src/setphot_client.c	(revision 39288)
@@ -22,9 +22,14 @@
 
   // load the flat-field correction table from CATDIR
-  char flatcorrFile[1024];
-  snprintf (flatcorrFile, 1024, "%s/flatcorr.fits", CATDIR);
-  FlatCorrectionTable *flatcorrTable = FlatCorrectionLoad (flatcorrFile, VERBOSE);
+  char flatfieldFile[1024];
+  snprintf (flatfieldFile, 1024, "%s/flatfield.fits", CATDIR);
 
-  update_dvo_setphot (image, Nimage, flatcorrTable);
+  CamPhotomCorrection *camcorr = CamPhotomCorrectionLoad (flatfieldFile);
+  if (!camcorr) {
+    fprintf (stderr, "failed to load camera-static flat-field correction\n");
+    exit (2);
+  }
+
+  update_dvo_setphot (image, Nimage, camcorr);
 
   exit (0);
Index: trunk/Ohana/src/uniphot/src/update_catalog_setastrom.c
===================================================================
--- trunk/Ohana/src/uniphot/src/update_catalog_setastrom.c	(revision 39287)
+++ trunk/Ohana/src/uniphot/src/update_catalog_setastrom.c	(revision 39288)
@@ -11,4 +11,5 @@
   off_t i;
 
+  // this is the special date when KH corrections stopped being needed
   time_t timeRef = ohana_date_to_sec("2011/05/11,00:00:00");
 
@@ -121,5 +122,5 @@
     double dX_CAM = 0.0;
     double dY_CAM = 0.0;
-    if (CAM_FILE) {
+    if (CAM_ASTROM_FILE) {
 
       // camera systematic correction ("astroflat") depends on only the X,Y coordinate,
@@ -130,5 +131,5 @@
 
       // correction is in arcseconds
-      get_cam_correction (chipID, filtCode, measure->Xccd, measure->Yccd, &dX_CAM, &dY_CAM);
+      CamAstromCorrectionValue (chipID, filtCode, measure->Xccd, measure->Yccd, &dX_CAM, &dY_CAM);
     }
 
@@ -166,5 +167,7 @@
       }
       int filtSeq = filtCode % 100;
-      if (!isfinite(dColor)) goto skip_DCR;
+      if (!isfinite(dColor)) {
+	goto skip_DCR;
+      }
 
       // I need to get the parallactic angle, but I only have alt & az, s:o 
@@ -195,16 +198,28 @@
       // WARNING: these are NOT sin(ha),cos(ha) but are scaled
       // double ha = DEG_RAD * atan2 (sinh, cosh);
-      double sinh = -cs_alt * sn_az;
-      double cosh =  sn_alt * cs_lat - cs_alt * cs_az * sn_lat;
-      double r_ha = hypot(sinh, cosh);
-      double sn_ha = sinh / r_ha;
-      double cs_ha = cosh / r_ha;
-
-      sinh = -cs_az * sn_alt * sn_ha * sn_lat + sn_az * sn_alt * cs_ha - sn_ha * cs_alt * cs_lat;
-      cosh = -sn_az          * sn_ha * sn_lat - cs_az          * cs_ha;
-      double r_rot = hypot(sinh, cosh);
-
-      double sn_rot = -sinh / r_rot;
-      double cs_rot = +cosh / r_rot;
+
+      // EAM 20151217: this is the old version of the code.  it is mathematically identical but does some extra work:
+      // double sinh = -cs_alt * sn_az;
+      // double cosh =  sn_alt * cs_lat - cs_alt * cs_az * sn_lat;
+      // double r_ha = hypot(sinh, cosh);
+      // double sn_ha = sinh / r_ha;
+      // double cs_ha = cosh / r_ha;
+
+      // sinh = -cs_az * sn_alt * sn_ha * sn_lat + sn_az * sn_alt * cs_ha - sn_ha * cs_alt * cs_lat;
+      // cosh = -sn_az          * sn_ha * sn_lat - cs_az          * cs_ha;
+      // double r_rot = hypot(sinh, cosh);
+      // double sn_rot = -sinh / r_rot;
+      // double cs_rot = +cosh / r_rot;
+
+      // EAM 20151217: this is the new version:
+      double sn_ha = -cs_alt * sn_az;
+      double cs_ha =  sn_alt * cs_lat - cs_alt * cs_az * sn_lat;
+
+      double sn_rot_r = -cs_az * sn_alt * sn_ha * sn_lat + sn_az * sn_alt * cs_ha - sn_ha * cs_alt * cs_lat;
+      double cs_rot_r = -sn_az          * sn_ha * sn_lat - cs_az          * cs_ha;
+      double r_rot = hypot(sn_rot_r, cs_rot_r);
+
+      double sn_rot = -sn_rot_r / r_rot;
+      double cs_rot =  cs_rot_r / r_rot;
 
       double dPx = 0.0; 
@@ -248,4 +263,7 @@
 
     // do not modify the original Xccd,Yccd values
+    // dR,dD are measured as measure.D - average.D
+    // thus we correct back to the truth (average) with
+    // average.D = measure.D - dD
     if (KH_FILE) {
       measure[0].XoffKH = -dX_KH / pltScale;
@@ -264,5 +282,5 @@
       measure[0].YoffDCR = 0.0;
     }
-    if (CAM_FILE) {
+    if (CAM_ASTROM_FILE) {
       measure[0].XoffCAM = -dX_CAM / pltScale;
       measure[0].YoffCAM = -dY_CAM / pltScale;
@@ -291,5 +309,5 @@
 
   if (found) {
-    fprintf (stderr, "found "OFF_T_FMT" matches\n", found);
+    fprintf (stderr, "found "OFF_T_FMT" matches for setastrom\n", found);
   }
 
Index: trunk/Ohana/src/uniphot/src/update_catalog_setphot.c
===================================================================
--- trunk/Ohana/src/uniphot/src/update_catalog_setphot.c	(revision 39287)
+++ trunk/Ohana/src/uniphot/src/update_catalog_setphot.c	(revision 39288)
@@ -1,7 +1,7 @@
 # include "setphot.h"
 
-void update_catalog_setphot (Catalog *catalog, Image *image, off_t *index, off_t Nimage, CamCorrection *camcorr) {
+void update_catalog_setphot (Catalog *catalog, Image *image, off_t *index, off_t Nimage, CamPhotomCorrection *camcorr) {
 
-  off_t i, j, found;
+  off_t i, found;
 
   // if we are resetting, reset all flags
@@ -9,45 +9,48 @@
 
   found = 0;    
-  for (i = 0; i < catalog[0].Naverage; i++) {
+  for (i = 0; i < catalog[0].Nmeasure; i++) {
 
-    off_t m = catalog[0].average[i].measureOffset;
-    for (j = 0; j < catalog[0].average[i].Nmeasure; j++, m++) {
-      off_t idx = catalog[0].measure[m].imageID;
-      if (idx <= 0) continue; // detections with imageID == 0 do not have a valid image (eg, ref photcode)
+    Measure *measure = &catalog[0].measure[i];
 
-      off_t id = index[idx];
-      if (id < 0) continue;
+    // only do GPC1 data for now
+    if (measure[0].photcode < 10000) continue;
+    if (measure[0].photcode > 10600) continue;
+      
+    off_t idx = measure[0].imageID;
+    if (idx <= 0) continue; // detections with imageID == 0 do not have a valid image (eg, ref photcode)
 
-      float Mcal = image[id].Mcal;
-      float dMcal = image[id].dMcal;
-      float Mflat = 0.0;
+    off_t id = index[idx];
+    if (id < 0) continue;
 
-      // 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) {
-	Mflat = get_cam_correction (camcorr, flat_id, catalog[0].measure[m].Xccd, catalog[0].measure[m].Yccd);
-      }
+    float Mcal = image[id].Mcal;
+    float dMcal = image[id].dMcal;
+    float Mflat = 0.0;
 
-      catalog[0].measure[m].Mcal = Mcal;
-      catalog[0].measure[m].Mflat = Mflat; // in the previous version, Mcal_offset (which is added to Mflat) had a negative sign here
-      catalog[0].measure[m].dMcal = dMcal;
-      myAssert(isfinite(catalog[0].measure[m].Mcal), "oops: ubercal made a nan");
+    // 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) {
+      Mflat = CamPhotomCorrectionValue (camcorr, flat_id, measure[0].Xccd, measure[0].Yccd);
+    }
 
-      if (RESET) {
-	catalog[0].measure[m].dbFlags &= ~photomFlags;
-      }
+    measure[0].Mcal = Mcal;
+    measure[0].Mflat = Mflat; // in the previous version, Mcal_offset (which is added to Mflat) had a negative sign here
+    measure[0].dMcal = dMcal;
+    myAssert(isfinite(measure[0].Mcal), "oops: ubercal made a nan");
 
-      // if we are setting the zero points from an UBERCAL database, and this detection is from one of those images,
-      // then tag the measurement as well
-      if (UBERCAL && (image[id].flags & ID_IMAGE_PHOTOM_UBERCAL)) {
-	catalog[0].measure[m].dbFlags |=  ID_MEAS_PHOTOM_UBERCAL;
-      }
+    if (RESET) {
+      measure[0].dbFlags &= ~photomFlags;
+    }
 
-      found ++;
+    // if we are setting the zero points from an UBERCAL database, and this detection is from one of those images,
+    // then tag the measurement as well
+    if (UBERCAL && (image[id].flags & ID_IMAGE_PHOTOM_UBERCAL)) {
+      measure[0].dbFlags |=  ID_MEAS_PHOTOM_UBERCAL;
     }
+
+    found ++;
   }
 
   if (found) {
-    fprintf (stderr, "found "OFF_T_FMT" matches\n", found);
+    fprintf (stderr, "found "OFF_T_FMT" matches for setphot\n", found);
   }
 }
Index: trunk/Ohana/src/uniphot/src/update_dvo_setastrom.c
===================================================================
--- trunk/Ohana/src/uniphot/src/update_dvo_setastrom.c	(revision 39287)
+++ trunk/Ohana/src/uniphot/src/update_dvo_setastrom.c	(revision 39288)
@@ -19,6 +19,7 @@
   if (KH_FILE)  if (!load_kh_correction  (KH_FILE))  { fprintf (stderr, "failed to load KH  correction %s\n", KH_FILE ); exit (1); }
   if (DCR_FILE) if (!load_dcr_correction (DCR_FILE)) { fprintf (stderr, "failed to load DCR correction %s\n", DCR_FILE); exit (1); }
-  if (CAM_FILE) if (!load_cam_correction (CAM_FILE)) { fprintf (stderr, "failed to load CAM correction %s\n", CAM_FILE); exit (1); }
   if (TYC_FILE) if (!load_tyc_correction (TYC_FILE)) { fprintf (stderr, "failed to load TYC correction %s\n", TYC_FILE); exit (1); }
+
+  if (CAM_ASTROM_FILE) if (!CamAstromCorrectionLoad (CAM_ASTROM_FILE)) { fprintf (stderr, "failed to load CAM correction %s\n", CAM_ASTROM_FILE); exit (1); }
 
   // determine the populated SkyRegions overlapping the requested area (default depth)
@@ -83,5 +84,5 @@
     gfits_modify_alt (&catalog.header, "HISTORY", "%S", 0, history); // adds a new entry
 
-    snprintf (history, 128, "setastrom options: KH: %1d, DCR: %1d, CAM: %1d", (KH_FILE != NULL), (DCR_FILE != NULL), (CAM_FILE != NULL));
+    snprintf (history, 128, "setastrom options: KH: %1d, DCR: %1d, CAM: %1d", (KH_FILE != NULL), (DCR_FILE != NULL), (CAM_ASTROM_FILE != NULL));
     gfits_modify_alt (&catalog.header, "HISTORY", "%S", 0, history); // adds a new entry
 
@@ -121,5 +122,5 @@
     if (KH_FILE)      	  { strextend (&command, "-KH %s", KH_FILE); }
     if (DCR_FILE)      	  { strextend (&command, "-DCR %s", DCR_FILE); }
-    if (CAM_FILE)      	  { strextend (&command, "-CAM %s", CAM_FILE); }
+    if (CAM_ASTROM_FILE)      	  { strextend (&command, "-CAM %s", CAM_ASTROM_FILE); }
     if (UPDATE_CATFORMAT) { strextend (&command, "-update-catformat %s", UPDATE_CATFORMAT); }
     if (KH_RESET)      	  { strextend (&command, "-KH-reset"); }
Index: trunk/Ohana/src/uniphot/src/update_dvo_setphot.c
===================================================================
--- trunk/Ohana/src/uniphot/src/update_dvo_setphot.c	(revision 39287)
+++ trunk/Ohana/src/uniphot/src/update_dvo_setphot.c	(revision 39288)
@@ -3,5 +3,5 @@
 
 // XXX convert this to use the ImageSubset to save space
-int update_dvo_setphot (Image *image, off_t Nimage, CamCorrection *camcorr) {
+int update_dvo_setphot (Image *image, off_t Nimage, CamPhotomCorrection *camcorr) {
 
   SkyTable *sky = NULL;
@@ -19,4 +19,9 @@
   }
 
+  if (KH_FILE)  if (!load_kh_correction  (KH_FILE))  { fprintf (stderr, "failed to load KH  correction %s\n", KH_FILE ); exit (1); }
+  if (DCR_FILE) if (!load_dcr_correction (DCR_FILE)) { fprintf (stderr, "failed to load DCR correction %s\n", DCR_FILE); exit (1); }
+
+  if (CAM_ASTROM_FILE) if (!CamAstromCorrectionLoad (CAM_ASTROM_FILE)) { fprintf (stderr, "failed to load CAM correction %s\n", CAM_ASTROM_FILE); exit (1); }
+
   // create an index for the image IDs
   maxID = 0;
@@ -54,4 +59,7 @@
     catalog.filename    = HOST_ID ? localFilename : skylist[0].filename[i];
     catalog.catflags    = DVO_LOAD_AVERAGE | DVO_LOAD_MEASURE;
+    if (KH_FILE || DCR_FILE || CAM_ASTROM_FILE) {
+      catalog.catflags |= DVO_LOAD_SECFILT;
+    }
     catalog.Nsecfilt    = GetPhotcodeNsecfilt ();
 
@@ -74,4 +82,8 @@
 
     update_catalog_setphot (&catalog, image, index, Nimage, camcorr);
+
+    if (KH_FILE || DCR_FILE || CAM_ASTROM_FILE) {
+      update_catalog_setastrom (&catalog);
+    }
 
     if (!UPDATE) {
@@ -92,6 +104,13 @@
     gfits_modify_alt (&catalog.header, "HISTORY", "%S", 0, history); // adds a new entry
 
-    snprintf (history, 128, "setphot options: reset: %1d, ubercal: %1d", RESET, UBERCAL);
+    snprintf (history, 128, "setphot options: reset: %1d, ubercal: %1d, camflat: %1d", RESET, UBERCAL, (CAM_PHOTOM_FILE != NULL));
     gfits_modify_alt (&catalog.header, "HISTORY", "%S", 0, history); // adds a new entry
+
+    if (KH_FILE || DCR_FILE || CAM_ASTROM_FILE) {
+      gfits_modify (&catalog.header, "SETASTR", "%s", 1, moddate); // replaces the single entry
+
+      snprintf (history, 128, "setphot (setastrom) options: KH: %1d, DCR: %1d, CAM: %1d", (KH_FILE != NULL), (DCR_FILE != NULL), (CAM_ASTROM_FILE != NULL));
+      gfits_modify_alt (&catalog.header, "HISTORY", "%S", 0, history); // adds a new entry
+    }
 
     free (moddate);
@@ -139,6 +158,16 @@
     if (RESET)         	 { strextend (&command, "-reset"); }
     if (UPDATE)        	 { strextend (&command, "-update"); }
-    if (UBERCAL)       	 { strextend (&command, "-ubercal"); }
     if (REPAIR_BY_OBJID) { strextend (&command, "-repair-by-objid"); }
+
+    if (UBERCAL)         { strextend (&command, "-ubercal"); }
+    // if (CAM_PHOTOM_FILE) { strextend (&command, "-cam-flat %s", CAM_PHOTOM_FILE); }
+
+    if (KH_FILE)      	  { strextend (&command, "-KH %s", KH_FILE); }
+    if (DCR_FILE)      	  { strextend (&command, "-DCR %s", DCR_FILE); }
+    if (CAM_ASTROM_FILE)  { strextend (&command, "-CAM %s", CAM_ASTROM_FILE); }
+
+    if (KH_RESET)      	  { strextend (&command, "-KH-reset"); }
+    if (DCR_RESET)     	  { strextend (&command, "-DCR-reset"); }
+    if (CAM_RESET)     	  { strextend (&command, "-CAM-reset"); }
 
     fprintf (stderr, "command: %s\n", command);
