Index: /branches/eam_branches/ipp-20121219/Ohana/src/uniphot/Makefile
===================================================================
--- /branches/eam_branches/ipp-20121219/Ohana/src/uniphot/Makefile	(revision 34867)
+++ /branches/eam_branches/ipp-20121219/Ohana/src/uniphot/Makefile	(revision 34868)
@@ -118,5 +118,5 @@
 $(SRC)/update_catalog_setastrom.$(ARCH).o \
 $(SRC)/astrom_correction.$(ARCH).o \
-$(SRC)/initialize_setastrom_client.$(ARCH).o
+$(SRC)/initialize_setastrom.$(ARCH).o
 
 $(SETASTROM_CLIENT): $(INC)/setastrom.h
Index: /branches/eam_branches/ipp-20121219/Ohana/src/uniphot/include/setastrom.h
===================================================================
--- /branches/eam_branches/ipp-20121219/Ohana/src/uniphot/include/setastrom.h	(revision 34867)
+++ /branches/eam_branches/ipp-20121219/Ohana/src/uniphot/include/setastrom.h	(revision 34868)
@@ -2,4 +2,12 @@
 # include <dvo.h>
 # include <signal.h>
+
+// the interpolating spline information: this is based on the spline used by opihi/cmd.data/spline.c
+typedef struct {
+  int Nknots;
+  double *xk;
+  double *yk;
+  double *y2;
+} Spline;
 
 /* global variables set in parameter file */
@@ -14,9 +22,14 @@
 int          PARALLEL_MANUAL;
 int          PARALLEL_SERIAL;
+char        *UPDATE_CATFORMAT;
+
+SkyRegion    UserPatch;
 
 /***** prototypes ****/
 int           main                            PROTO((int argc, char **argv));
 
+void          GetConfig                       PROTO((char *config, char *field, char *format, int N, void *ptr));
 void          ConfigInit                      PROTO((int *argc, char **argv));
+
 void          usage_setastrom                 PROTO(());
 void          initialize_setastrom            PROTO((int argc, char **argv));
@@ -32,7 +45,11 @@
 // int           SetSignals                      PROTO((void));
 
-int           update_dvo_setastrom            PROTO(());
-int           update_dvo_setastrom_parallel   PROTO((SkyTable *sky));
+int           update_dvo_setastrom            PROTO((char *corrfile));
+int           update_dvo_setastrom_parallel   PROTO((SkyTable *sky, char *corrfile));
 
-void          update_catalog_setastrom        PROTO((Catalog *catalog));
+int           update_catalog_setastrom        PROTO((Catalog *catalog));
 
+// spline correction functions
+int load_astrom_correction (char *filename);
+int get_astrom_correction (int sub, int chip, double *dX, double *dY, float Minst);
+double spline_apply_dbl (double *x, double *y, double *y2, int N, double X);
Index: /branches/eam_branches/ipp-20121219/Ohana/src/uniphot/src/astrom_correction.c
===================================================================
--- /branches/eam_branches/ipp-20121219/Ohana/src/uniphot/src/astrom_correction.c	(revision 34867)
+++ /branches/eam_branches/ipp-20121219/Ohana/src/uniphot/src/astrom_correction.c	(revision 34868)
@@ -13,13 +13,4 @@
 // these are stored in an N-D array of (Spline *) splineset[sub][dir][chip]
 
-// the interpolating spline information: this is based on the spline used by opihi/cmd.data/spline.c
-typedef struct {
-  int Nknots;
-  double *xk;
-  double *yk;
-  double *y2;
-  char *name;
-} Spline;
-
 # define N_SUB 2
 # define N_DIR 2
@@ -30,4 +21,10 @@
 
 int load_astrom_correction (char *filename) {
+
+  int Ncol;
+  off_t Nrow;
+
+  char type[16];
+  char extname[80];
 
   Header header;
@@ -54,11 +51,12 @@
 
   // we have a N-D array of splines:
-  // splineset[subset][dir][chip]
-  ALLOCATE (splineset, Spline ***, 2);
-  for (sub = 0; sub < 2; sub++) {
-    ALLOCATE (splineset[sub], Spline **, 2);
-    for (dir = 0; dir < 2; dir++) {
-      ALLOCATE (splineset[sub][dir], Spline *, 78);
-      for (chip = 0; chip < 78; chip ++) {
+  // splineset[sub][dir][chip]
+
+  ALLOCATE (splineset, Spline ***, N_SUB);
+  for (sub = 0; sub < N_SUB; sub++) {
+    ALLOCATE (splineset[sub], Spline **, N_DIR);
+    for (dir = 0; dir < N_DIR; dir++) {
+      ALLOCATE (splineset[sub][dir], Spline *, N_CHIP);
+      for (chip = 0; chip < N_CHIP; chip ++) {
 	splineset[sub][dir][chip] = NULL;
       }
@@ -70,35 +68,109 @@
     if (!gfits_load_header (f, &theader)) break;
     
-    if (!gfits_fread_ftable_data (f, &table, FALSE)) return (FALSE);
+    if (!gfits_fread_ftable_data (f, &ftable, FALSE)) return (FALSE);
     
-    if (!gfits_scan (header, "EXTNAME", "%s", 1, extname)) return (FALSE);
+    if (!gfits_scan (&theader, "EXTNAME", "%s", 1, extname)) return (FALSE);
 
     // figure out the chip ID, diretion, and sequence from the EXTNAME
 
-    int chip = XX;
-    int dir = XX;
-    int subset = XX;
+    // EXTNAME is of the form XY%d%d.D%s.T%s where 
+    char chipname[3];
+    chipname[0] = extname[2];
+    chipname[1] = extname[3];
+    chipname[2] = 0;
+    int chip = atoi(chipname);
+    myAssert (chip >   0, "error in extname %s not(chip >   0)\n", extname);
+    myAssert (chip <  77, "error in extname %s not(chip <  77)\n", extname);
+    myAssert (chip !=  7, "error in extname %s not(chip !=  7)\n", extname);
+    myAssert (chip != 70, "error in extname %s not(chip != 70)\n", extname);
 
-    splineset[sub][dir][chip] = CreateSpline (Nrow);
+    int dir = -1;
+    if (extname[6] == 'X') {
+      dir = 0;
+    }
+    if (extname[6] == 'Y') {
+      dir = 1;
+    }
+    myAssert (dir != -1, "error in extname %s (dir)\n", extname);
 
-    // XXX: need to handle case of spline data not existing...
-    
+    int sub = -1;
+    if (extname[6] == '0') {
+      sub = 0;
+    }
+    if (extname[6] == '1') {
+      sub = 1;
+    }
+    myAssert (sub != -1, "error in extname %s (sub)\n", extname);
+
     // need to create and assign to flat-field correction
     double *xk = gfits_get_bintable_column_data (&theader, &ftable, "X_KNOT", type, &Nrow, &Ncol);
-    assert (!strcmp(type, "double"));
+    myAssert (!strcmp(type, "double"), "error reading X_KNOT");
     
     // need to create and assign to flat-field correction
     double *yk = gfits_get_bintable_column_data (&theader, &ftable, "Y_KNOT", type, &Nrow, &Ncol);
-    assert (!strcmp(type, "double"));
+    myAssert (!strcmp(type, "double"), "error reading X_KNOT");
     
     // need to create and assign to flat-field correction
     double *y2 = gfits_get_bintable_column_data (&theader, &ftable, "DY2_DX", type, &Nrow, &Ncol);
-    assert (!strcmp(type, "double"));
+    myAssert (!strcmp(type, "double"), "error reading X_KNOT");
     
+    ALLOCATE (splineset[sub][dir][chip], Spline, 1);
     splineset[sub][dir][chip]->xk = xk;
     splineset[sub][dir][chip]->yk = yk;
     splineset[sub][dir][chip]->y2 = y2;
-    
+    splineset[sub][dir][chip]->Nknots = Nrow;
   }
   return (TRUE);
 }
+
+int get_astrom_correction (int sub, int chip, double *dX, double *dY, float Minst) {
+
+  Spline *spline = NULL;
+
+  *dX = 0.0;
+  *dY = 0.0;
+
+  spline = splineset[sub][0][chip];
+  if (spline) {
+    *dX = spline_apply_dbl (spline->xk, spline->yk, spline->y2, spline->Nknots, Minst);
+  }
+
+  spline = splineset[sub][1][chip];
+  if (spline) {
+    *dY = spline_apply_dbl (spline->xk, spline->yk, spline->y2, spline->Nknots, Minst);
+  }
+  return (TRUE);
+}
+
+/* evaluate spline for x, y, y2 at X */
+double spline_apply_dbl (double *x, double *y, double *y2, int N, double X) {
+
+  int i, lo, hi;
+  double dx, a, b, value;
+  
+  /* find correct element in array (x must be sorted) */
+  lo = 0;
+  hi = N-1;
+  while (hi - lo > 1) {
+    i = 0.5*(hi+lo);
+    if (x[i] > X) {
+      hi = i;
+    } else {
+      lo = i;
+    }
+  }
+
+  /* error condition: duplicate abssisca */
+  dx = x[hi] - x[lo];
+  if (dx == 0.0) {
+    return (HUGE_VAL);
+  }
+
+  /* evaluate spline */
+  a = (x[hi] - X) / dx;
+  b = (X - x[lo]) / dx;
+
+  value = a*y[lo] + b*y[hi] + ((a*a*a - a)*y2[lo] + (b*b*b - b)*y2[hi])*(dx*dx) / 6.0;
+  return (value);
+
+}
Index: /branches/eam_branches/ipp-20121219/Ohana/src/uniphot/src/initialize_setastrom.c
===================================================================
--- /branches/eam_branches/ipp-20121219/Ohana/src/uniphot/src/initialize_setastrom.c	(revision 34867)
+++ /branches/eam_branches/ipp-20121219/Ohana/src/uniphot/src/initialize_setastrom.c	(revision 34868)
@@ -2,7 +2,9 @@
 
 void usage_setastrom () {
-    fprintf (stderr, "USAGE: setastrom [options]\n");
+    fprintf (stderr, "USAGE: setastrom (corrections) [options]\n");
     fprintf (stderr, "  options:\n");
     fprintf (stderr, "    -v : verbose mode\n");
+    fprintf (stderr, "    -region Rmin Rmax Dmin Dmax\n");
+    fprintf (stderr, "    -update-catformat (format) : change database schema on output\n");
     fprintf (stderr, "    -update : actually write results to detections tables\n");
     fprintf (stderr, "    -parallel : run in parallel mode\n");
@@ -46,4 +48,28 @@
 
   int N;
+
+  /* specify portion of the sky : allow default of all sky? */
+  UserPatch.Rmin = 0;
+  UserPatch.Rmax = 360;
+  UserPatch.Dmin = -90;
+  UserPatch.Dmax = +90;
+  if ((N = get_argument (argc, argv, "-region"))) {
+    remove_argument (N, &argc, argv);
+    UserPatch.Rmin = atof (argv[N]);
+    remove_argument (N, &argc, argv);
+    UserPatch.Rmax = atof (argv[N]);
+    remove_argument (N, &argc, argv);
+    UserPatch.Dmin = atof (argv[N]);
+    remove_argument (N, &argc, argv);
+    UserPatch.Dmax = atof (argv[N]);
+    remove_argument (N, &argc, argv);
+  } 
+
+  UPDATE_CATFORMAT = NULL;
+  if ((N = get_argument (argc, argv, "-update-catformat"))) {
+    remove_argument (N, &argc, argv);
+    UPDATE_CATFORMAT = strcreate (argv[N]);
+    remove_argument (N, &argc, argv);
+  }
 
   VERBOSE = FALSE;
@@ -85,5 +111,5 @@
   }
 
-  if (argc != 1) usage_setastrom();
+  if (argc != 2) usage_setastrom();
 
   return (TRUE);
@@ -91,6 +117,8 @@
 
 void usage_setastrom_client () {
-  fprintf (stderr, "USAGE: setastrom_client -hostID (hostID) -catdir (catdir) -hostdir (hostdir) -images (images) [options]\n");
+  fprintf (stderr, "USAGE: setastrom_client (correction) -hostID (hostID) -catdir (catdir) -hostdir (hostdir) [options]\n");
   fprintf (stderr, "  options:\n");
+  fprintf (stderr, "    -region Rmin Rmax Dmin Dmax\n");
+  fprintf (stderr, "    -update-catformat (format) : change database schema on output\n");
   fprintf (stderr, "    -v : verbose mode\n");
   fprintf (stderr, "    -update : actually write results to detections tables\n");
@@ -129,4 +157,28 @@
   int N;
 
+  /* specify portion of the sky : allow default of all sky? */
+  UserPatch.Rmin = 0;
+  UserPatch.Rmax = 360;
+  UserPatch.Dmin = -90;
+  UserPatch.Dmax = +90;
+  if ((N = get_argument (argc, argv, "-region"))) {
+    remove_argument (N, &argc, argv);
+    UserPatch.Rmin = atof (argv[N]);
+    remove_argument (N, &argc, argv);
+    UserPatch.Rmax = atof (argv[N]);
+    remove_argument (N, &argc, argv);
+    UserPatch.Dmin = atof (argv[N]);
+    remove_argument (N, &argc, argv);
+    UserPatch.Dmax = atof (argv[N]);
+    remove_argument (N, &argc, argv);
+  } 
+
+  UPDATE_CATFORMAT = NULL;
+  if ((N = get_argument (argc, argv, "-update-catformat"))) {
+    remove_argument (N, &argc, argv);
+    UPDATE_CATFORMAT = strcreate (argv[N]);
+    remove_argument (N, &argc, argv);
+  }
+
   VERBOSE = FALSE;
   if ((N = get_argument (argc, argv, "-v"))) {
@@ -164,17 +216,7 @@
   if (!CATDIR) usage_setastrom_client();
 
-  IMAGES = NULL;
-  if ((N = get_argument (argc, argv, "-images"))) {
-    remove_argument (N, &argc, argv);
-    IMAGES = strcreate (argv[N]);
-    remove_argument (N, &argc, argv);
-  }
-  if (!IMAGES) usage_setastrom_client();
-
-  if (argc != 1) usage_setastrom_client();
+  if (argc != 2) usage_setastrom_client();
   return (TRUE);
 }
-
-void GetConfig (char *config, char *field, char *format, int N, void *ptr);
 
 void ConfigInit (int *argc, char **argv) {
Index: /branches/eam_branches/ipp-20121219/Ohana/src/uniphot/src/setastrom.c
===================================================================
--- /branches/eam_branches/ipp-20121219/Ohana/src/uniphot/src/setastrom.c	(revision 34867)
+++ /branches/eam_branches/ipp-20121219/Ohana/src/uniphot/src/setastrom.c	(revision 34868)
@@ -4,12 +4,10 @@
 
   int status;
-  FITS_DB db;
 
   /* get configuration info, args, lockfile */
   initialize_setastrom (argc, argv);
 
-  load_correction ();
-
-  status = update_dvo_setastrom (subset, Nimage);
+  // argv[1] holds the correction file
+  status = update_dvo_setastrom (argv[1]);
 
   if (!status) exit (1);
Index: /branches/eam_branches/ipp-20121219/Ohana/src/uniphot/src/setastrom_client.c
===================================================================
--- /branches/eam_branches/ipp-20121219/Ohana/src/uniphot/src/setastrom_client.c	(revision 34868)
+++ /branches/eam_branches/ipp-20121219/Ohana/src/uniphot/src/setastrom_client.c	(revision 34868)
@@ -0,0 +1,20 @@
+# include "setastrom.h"
+
+// setastrom_client is run on a remote host and is responsible for updating the catalogs
+// owned by that host.  
+
+// setastrom_client -hostname (hostname) -catdir (catdir) -hostdir (hostdir) -images (images)
+// load the SkyTable and HostTable from (catdir) [contains the host responsibilities]
+// load the image data from (images) [contains the zp infomation to apply]
+// loop over tables from SkyTable with my host ID
+// the calling program tells the client which host they are (or host ID?); we do not actually have to run this on the real host
+
+int main (int argc, char **argv) {
+
+  // get configuration info, args, lockfile (set CATDIR, HOST_ID, HOSTDIR, IMAGES)
+  initialize_setastrom_client (argc, argv);
+
+  update_dvo_setastrom (argv[1]);
+
+  exit (0);
+}
Index: /branches/eam_branches/ipp-20121219/Ohana/src/uniphot/src/update_catalog_setastrom.c
===================================================================
--- /branches/eam_branches/ipp-20121219/Ohana/src/uniphot/src/update_catalog_setastrom.c	(revision 34867)
+++ /branches/eam_branches/ipp-20121219/Ohana/src/uniphot/src/update_catalog_setastrom.c	(revision 34868)
@@ -7,7 +7,7 @@
 int update_catalog_setastrom (Catalog *catalog) {
 
-  off_t i, j, found;
+  off_t i, found;
 
-  int timeRef = somefunc("2011/05/11");
+  time_t timeRef = ohana_date_to_sec("2011/05/11,00:00:00");
 
   for (i = 0; i < catalog[0].Nmeasure; i++) {
@@ -19,15 +19,22 @@
     double Xccd = catalog[0].measure[i].Xccd;
     double Yccd = catalog[0].measure[i].Yccd;
-    double posAngle = FromShortDegrees(catalog[0].measure[i].posangle);
     double pltScale = fabs(catalog[0].measure[i].pltscale);
+
+    // this is not needed, is it?
+    // double posAngle = FromShortDegrees(catalog[0].measure[i].posangle);
 
     // I need to know which chip we have.  use photcode assuming range
     int chip = catalog[0].measure[i].photcode % 100;
 
+    float Minst = PhotInst (&catalog[0].measure[i]);
+
+    double dX;
+    double dY;
+
     // correction is in arcseconds
     if (catalog[0].measure[i].t < timeRef) {
-      get_correction (0, chip, &dX, &dY);
+      get_astrom_correction (0, chip, &dX, &dY, Minst);
     } else {
-      get_correction (1, chip, &dX, &dY);
+      get_astrom_correction (1, chip, &dX, &dY, Minst);
     }
 
Index: /branches/eam_branches/ipp-20121219/Ohana/src/uniphot/src/update_dvo_setastrom.c
===================================================================
--- /branches/eam_branches/ipp-20121219/Ohana/src/uniphot/src/update_dvo_setastrom.c	(revision 34867)
+++ /branches/eam_branches/ipp-20121219/Ohana/src/uniphot/src/update_dvo_setastrom.c	(revision 34868)
@@ -1,11 +1,10 @@
 # include "setastrom.h"
 
-int update_dvo_setastrom () {
+int update_dvo_setastrom (char *corrfile) {
 
-  SkyRegion UserPatch;
   SkyTable *sky = NULL;
   SkyList *skylist = NULL;
   Catalog catalog;
-  off_t i, maxID, *index;
+  off_t i;
 
   // load the current sky table (layout of all SkyRegions) 
@@ -14,13 +13,11 @@
   
   if (PARALLEL && !HOST_ID) {
-    update_dvo_setastrom_parallel (sky);
-    return TRUE;
+    int status = update_dvo_setastrom_parallel (sky, corrfile);
+    return status;
   }
 
+  load_astrom_correction (corrfile);
+
   // determine the populated SkyRegions overlapping the requested area (default depth)
-  UserPatch.Rmin = 0;
-  UserPatch.Rmax = 360;
-  UserPatch.Dmin = -90;
-  UserPatch.Dmax = +90;
   skylist = SkyListByPatch (sky, -1, &UserPatch);
 
@@ -60,5 +57,8 @@
     }
     
-  XXX: update format here
+    // modify the output format as desired (ignore current format on disk)
+    if (UPDATE_CATFORMAT) {
+      catalog.catformat = dvo_catalog_catformat (UPDATE_CATFORMAT);
+    }
 
     if (VERBOSE) fprintf (stderr, "saving catalog %s\n", catalog.filename);
@@ -73,7 +73,9 @@
 # define DEBUG 1
 
-int update_dvo_setastrom_parallel (SkyTable *sky) {
+int update_dvo_setastrom_parallel (SkyTable *sky, char *corrfile) {
 
   // now launch the setastrom_client jobs to the parallel hosts
+
+  char *abscorrfile = abspath (corrfile, DVO_MAX_PATH);
 
   // load the list of hosts
@@ -89,12 +91,14 @@
 
     char command[1024];
-    snprintf (command, 1024, "setastrom_client -hostID %d -catdir %s -hostdir %s", 
-	      table->hosts[i].hostID, CATDIR, table->hosts[i].pathname);
+    snprintf (command, 1024, "setastrom_client %s -hostID %d -catdir %s -hostdir %s -region %f %f %f %f", 
+	      abscorrfile, table->hosts[i].hostID, CATDIR, table->hosts[i].pathname, 
+	      UserPatch.Rmin, UserPatch.Rmax, UserPatch.Dmin, UserPatch.Dmax);
 
     fprintf (stderr, "command: %s\n", command);
 
     char tmpline[1024];
-    if (VERBOSE)       { snprintf (tmpline, 1024, "%s -v",              command);                    strcpy (command, tmpline); }
-    if (UPDATE)        { snprintf (tmpline, 1024, "%s -update",         command); 		     strcpy (command, tmpline); }
+    if (VERBOSE)       	  { snprintf (tmpline, 1024, "%s -v",                	command); 		    strcpy (command, tmpline); }
+    if (UPDATE)        	  { snprintf (tmpline, 1024, "%s -update",           	command); 		    strcpy (command, tmpline); }
+    if (UPDATE_CATFORMAT) { snprintf (tmpline, 1024, "%s -update-catformat %s", command, UPDATE_CATFORMAT); strcpy (command, tmpline); }
 
     if (PARALLEL_MANUAL) continue;
@@ -117,4 +121,5 @@
     }
   }
+  free (abscorrfile);
 
   // wait for the remote jobs to be completed
