Index: /branches/eam_branches/Ohana.20100407/src/uniphot/src/load_images.c
===================================================================
--- /branches/eam_branches/Ohana.20100407/src/uniphot/src/load_images.c	(revision 27767)
+++ /branches/eam_branches/Ohana.20100407/src/uniphot/src/load_images.c	(revision 27768)
@@ -10,2 +10,17 @@
   return (TRUE);
 }
+
+Image *load_images_setphot (FITS_DB *db, off_t *Nimage) {
+
+  Image *image;
+
+  if (VERBOSE) fprintf (stderr, "finding images\n");
+
+  /* read entire db table */
+  if (!dvo_image_load (db, VERBOSE, FALSE)) Shutdown ("can't read image catalog %s", db[0].filename);
+
+  /* use a vtable to keep the images to be calibrated */
+  image = gfits_table_get_Image (&db[0].ftable, Nimage, &db[0].swapped);
+
+  return (image);
+}
Index: /branches/eam_branches/Ohana.20100407/src/uniphot/src/load_zpt_table.c
===================================================================
--- /branches/eam_branches/Ohana.20100407/src/uniphot/src/load_zpt_table.c	(revision 27768)
+++ /branches/eam_branches/Ohana.20100407/src/uniphot/src/load_zpt_table.c	(revision 27768)
@@ -0,0 +1,38 @@
+# include "setphot.h"
+
+ZptTable *load_zpt_table (char *filename, int *nzpts) {
+
+  int Nzpts, NZPTS;
+  ZptTable *zpts;
+  double zpt, mjd;
+
+  FILE *f;
+
+  f = fopen (filename, "r");
+  if (!f) {
+    fprintf (stderr, "ERROR: cannot open zpt table file %s\n", filename);
+    exit (1);
+  }
+
+  Nzpts = 0;
+  NZPTS = 100;
+  ALLOCATE (zpts, ZptTable, NZPTS);
+
+  // format is fixed: (time) (zpt)
+  int status;
+  while ((status = fscanf (f, "%lf %lf", &mjd, &zpt)) == 2) {
+    zpts[Nzpts].zpt = zpt;
+    zpts[Nzpts].time = ohana_mjd_to_sec(mjd);
+
+    Nzpts ++;
+    CHECK_REALLOCATE (zpts, ZptTable, NZPTS, Nzpts, 100);
+  }
+
+  if (status != EOF) {
+    fprintf (stderr, "unexpected formatting on line %d\n", Nzpts);
+    exit (2);
+  }
+
+  *nzpts = Nzpts;
+  return zpts;
+}
Index: /branches/eam_branches/Ohana.20100407/src/uniphot/src/match_zpts_to_images.c
===================================================================
--- /branches/eam_branches/Ohana.20100407/src/uniphot/src/match_zpts_to_images.c	(revision 27768)
+++ /branches/eam_branches/Ohana.20100407/src/uniphot/src/match_zpts_to_images.c	(revision 27768)
@@ -0,0 +1,77 @@
+# include "uniphot.h"
+
+/* sort a coordinate pair (X,Y) and the associated index (S) */
+void sort_zpts_by_time (ZptTable *zpts, int Nzpts) {
+  
+# define SWAPFUNC(A,B){ ZptTable tmp;		\
+    tmp = X[A]; X[A] = X[B]; X[B] = tmp;	\
+  }
+# define COMPARE(A,B)(X[A].time < X[B].time)
+
+  OHANA_SORT (N, COMPARE, SWAPFUNC);
+
+# undef SWAPFUNC
+# undef COMPARE
+}
+
+int match_zpts_to_images (Image *image, off_t Nimage, ZptTable *zpts, int Nzpts) {
+
+  // I have two lists.  I need to match images->tzero to zpts->time.  multiple images may match a single zpt
+  
+  // sort both lists by time (or at least get sorted indices)
+  // sweep through both lists, advancing the one that is lagging
+  // apply the matches
+
+  int dT, NImatch, Nmatch;
+  off_t i, Ni, Nz, *index;
+
+  // create index and sort
+  ALLOCATE (index, off_t, Nimage);
+  for (i = 0; i < Nimage; i++) {
+    index[i] = i;
+  }
+  sort_image_subset(image, index, Nimage);  // slightly misnamed sort function
+
+  // sort the zpts
+  sort_zpts_by_time (zpts, Nzpts);
+
+  NImatch = 0; // matched images
+  for (i = Nz = 0; (i < Nimage) && (Nz < Nzpts); ) {
+
+    Ni = index[i];
+    dT = image[Ni].tzero - zpts[Nz].time;
+
+    // negative dT, i is too small (allow a 1sec overlap window)
+    if (dT < -1) {
+      i++;
+      continue;
+    }
+
+    // XXX careful about definition of image->tzero and zpt->time
+    // negative dT, i is too small (allow a 1sec overlap window)
+    if (dT > +1) {
+      Nz++;
+      continue;
+    }
+
+    // we have a match: set zpt and record the match
+    image[Ni].Mcal = zpts[Nz].zpt; // XXX watch out for offset..
+    image[Ni].flags &= ~ID_IMAGE_NOCAL; // clear the NOCAL flag
+    zpts[Nz].found = TRUE;
+    NImatch ++;
+
+    // advance the image counter only -- a single zpt may match more than one image
+    i++;
+  }
+
+  // how many zpts have we matched?
+  Nmatch = 0;
+  for (Nz = 0; Nz < Nzpts; Nz++) {
+    if (image[Nz].found) Nmatch ++;
+  }
+
+  fprintf (stderr, "found %d zpt matches\n", 
+
+  return (TRUE);
+}
+
Index: /branches/eam_branches/Ohana.20100407/src/uniphot/src/setphot.c
===================================================================
--- /branches/eam_branches/Ohana.20100407/src/uniphot/src/setphot.c	(revision 27768)
+++ /branches/eam_branches/Ohana.20100407/src/uniphot/src/setphot.c	(revision 27768)
@@ -0,0 +1,47 @@
+# include "setphot.h"
+
+int main (int argc, char **argv) {
+
+  off_t Nimage;
+  int Nzpts;
+  FITS_DB db;
+  ZptTable *zpts;
+  Image *image;
+
+  /* get configuration info, args, lockfile */
+  initialize (argc, argv);
+
+  set_db (&db);
+  status = dvo_image_lock (&db, ImageCat, 60.0, (UPDATE ? LCK_XCLD : LCK_SOFT));
+  if (!status) Shutdown ("ERROR: failure to lock image catalog %s", db.filename);
+  if (db.dbstate == LCK_EMPTY) Shutdown ("ERROR: No images in catalog %s (1)", db.filename);
+  if (!UPDATE) dvo_image_unlock (&db); 
+
+  zpts = load_zpt_table (argv[1], &Nzpts);
+
+  /* load images */
+  image  = load_images_setphot (&db, &Nimage);
+  
+  match_zpts_to_images (image, Nimage, zpts, Nzpts);
+
+  update_catalogs();
+
+  /** write image table **/
+  dvo_image_update (db, VERBOSE);
+
+  exit (0);
+}
+  
+
+/* setphot : set the zero points for images in the db (perhaps based on external information)
+   setphot (zpt_table)
+
+ * load text table of zpts, time (photcode?)
+ * load images
+ * match images to zpts
+ * set zpts
+ * load catalogs
+ * update detection (& averages?)
+
+ */
+
Index: /branches/eam_branches/Ohana.20100407/src/uniphot/src/update_catalog_setphot.c
===================================================================
--- /branches/eam_branches/Ohana.20100407/src/uniphot/src/update_catalog_setphot.c	(revision 27768)
+++ /branches/eam_branches/Ohana.20100407/src/uniphot/src/update_catalog_setphot.c	(revision 27768)
@@ -0,0 +1,94 @@
+# include "uniphot.h"
+# include <glob.h>
+
+intax update_setphot (Image *image, off_t Nimage) {
+
+  SkyTable *sky = NULL;
+  SkyList *skylist = NULL;
+  Catalog catalog;
+
+  off_t i, Nimage, maxID, *index;
+  int j, status, Nmin;
+
+  // create an index for the image IDs
+  maxID = 0;
+  for (i = 0; i < Nimage; i++) {
+    maxID = MAX(maxID, image[i].imageID);
+  }
+  ALLOCATE (index, off_t, maxID + 1);
+  for (i = 0; i < Nimage; i++) {
+    if (image[i].imageID < 0) continue;
+    index[image[i].imageID] = i;
+  }
+  
+
+  // load the current sky table (layout of all SkyRegions) 
+  sky = SkyTableLoadOptimal (CATDIR, SKY_TABLE, GSCFILE, TRUE, SKY_DEPTH, VERBOSE);
+  SkyTableSetFilenames (sky, CATDIR, "cpt");
+  
+  // determine the populated SkyRegions overlapping the requested area (default depth)
+  skylist = SkyListByPatch (sky, -1, &UserPatch);
+
+  // update measurements for each populated catalog
+  for (i = 0; i < skylist[0].Nregions; i++) {
+
+    // set up the basic catalog info
+    catalog.filename  = skylist[0].filename[i];
+    catalog.catformat = dvo_catalog_catformat (CATFORMAT);    // set the default catformat from config data
+    catalog.catmode   = dvo_catalog_catmode (CATMODE);        // set the default catmode from config data
+    catalog.catflags  = LOAD_AVES | LOAD_MEAS | LOAD_MISS | LOAD_SECF;
+    catalog.Nsecfilt  = GetPhotcodeNsecfilt ();
+
+    if (!dvo_catalog_open (&catalog, skylist[0].regions[i], VERBOSE, "w")) {
+      fprintf (stderr, "ERROR: failure reading catalog %s\n", catalog.filename);
+      exit (1);
+    }
+    if (!catalog.Naves_disk) {
+      if (VERBOSE) fprintf (stderr, "no data in %s, skipping\n", catalog.filename);
+      dvo_catalog_unlock (&catalog);
+      dvo_catalog_free (&catalog);
+      continue;
+    }
+
+    update_catalog_setphot (&catalog, image, index, Nimage);
+
+    if (!UPDATE) {
+      dvo_catalog_unlock (&catalog);
+      dvo_catalog_free (&catalog);
+      continue;
+    }
+    
+    if (VERBOSE) fprintf (stderr, "saving catalog %s\n", catalog.filename);
+    
+    dvo_catalog_save (&catalog, VERBOSE); 
+    dvo_catalog_unlock (&catalog);
+    dvo_catalog_free (&catalog);
+  }
+  return (TRUE);
+}      
+
+void update_catalog_setphot (Catalog *catalog, Image *image, off_t *index, off_t Nimage) {
+
+  off_t i, j, m, id, idx, found;
+  float Mcal;
+
+  found = 0;    
+  for (i = 0; i < catalog[0].Naverage; i++) {
+
+    m = catalog[0].average[i].measureOffset;
+    for (j = 0; j < catalog[0].average[i].Nmeasure; j++, m++) {
+      idx = catalog[0].measure[m].imageID;
+      if (idx < 0) continue;
+
+      id = index[idx];
+      Mcal = image[id].Mcal;
+
+      catalog[0].measure[m].Mcal = image[id].Mcal;
+      found ++;
+    }
+  }
+
+  if (found) {
+    fprintf (stderr, "found %d matches\n", found);
+  }
+}
