Index: /branches/eam_branches/Ohana.20100407/src/uniphot/Makefile
===================================================================
--- /branches/eam_branches/Ohana.20100407/src/uniphot/Makefile	(revision 27769)
+++ /branches/eam_branches/Ohana.20100407/src/uniphot/Makefile	(revision 27770)
@@ -1,5 +1,5 @@
-default: uniphot
+default: uniphot setphot
 help:
-	@echo "make options: uniphot (default)"
+@echo "make options: uniphot setphot default help install default (uniphot setphot)"
 
 include ../../Makefile.System
@@ -18,5 +18,6 @@
 
 uniphot: $(BIN)/uniphot.$(ARCH)
-install: $(DESTBIN)/uniphot
+setphot: $(BIN)/setphot.$(ARCH)
+install: $(DESTBIN)/uniphot $(DESTBIN)/setphot 
 
 UNIPHOT = \
@@ -24,5 +25,5 @@
 $(SRC)/initialize.$(ARCH).o	    \
 $(SRC)/ConfigInit.$(ARCH).o	    \
-$(SRC)/args.$(ARCH).o		    \
+$(SRC)/args.$(ARCH).o               \
 $(SRC)/liststats.$(ARCH).o	    \
 $(SRC)/load_images.$(ARCH).o	    \
@@ -39,2 +40,18 @@
 $(UNIPHOT): $(INC)/uniphot.h
 $(BIN)/uniphot.$(ARCH): $(UNIPHOT)
+
+SETPHOT = \
+$(SRC)/setphot.$(ARCH).o	    \
+$(SRC)/initialize.$(ARCH).o	    \
+$(SRC)/ConfigInit.$(ARCH).o	    \
+$(SRC)/args.$(ARCH).o               \
+$(SRC)/liststats.$(ARCH).o	    \
+$(SRC)/load_zpt_table.$(ARCH).o	    \
+$(SRC)/load_images.$(ARCH).o	    \
+$(SRC)/match_zpts_to_images.$(ARCH).o	    \
+$(SRC)/update_catalog_setphot.$(ARCH).o \
+$(SRC)/SetSignals.$(ARCH).o	    \
+$(SRC)/Shutdown.$(ARCH).o	    
+
+$(SETPHOT): $(INC)/uniphot.h
+$(BIN)/setphot.$(ARCH): $(SETPHOT)
Index: /branches/eam_branches/Ohana.20100407/src/uniphot/include/uniphot.h
===================================================================
--- /branches/eam_branches/Ohana.20100407/src/uniphot/include/uniphot.h	(revision 27769)
+++ /branches/eam_branches/Ohana.20100407/src/uniphot/include/uniphot.h	(revision 27770)
@@ -39,4 +39,10 @@
 } Group;
 
+typedef struct {
+    float zpt;
+    e_time time;
+    int found;
+} ZptTable;
+
 /* global variables set in parameter file */
 char         ImageCat[256];
@@ -44,4 +50,7 @@
 char         CATMODE[16];    /* raw, mef, split, mysql */
 char         CATFORMAT[16];  /* internal, elixir, loneos, panstarrs */
+char         SKY_TABLE[256];
+int          SKY_DEPTH;  /** XXX EAM : depth of catalog tables, fix usage */
+char         GSCFILE[256];
 char         STATMODE[64];
 int          VERBOSE;
@@ -66,41 +75,50 @@
 
 /***** prototypes ****/
-void          ConfigInit         PROTO((int *argc, char **argv));
-void          DonePlotting       PROTO((Graphdata *graphmode, int N));
-void          JpegPlot           PROTO((Graphdata *graphmode, int N, char *filename));
-void          PSPlot             PROTO((Graphdata *graphmode, int N, char *filename));
-void          PlotLabel          PROTO((char *string, int N));
-void          PlotVector         PROTO((int Npts, double *vect, int mode, int N));
-void          PrepPlotting       PROTO((int Npts, Graphdata *graphmode, int N));
-void          XDead              PROTO((void));
-int           args               PROTO((int argc, char **argv));
-void          dumpresult         PROTO((void));
-Group        *find_image_sgroups PROTO((FITS_DB *db, ImageLink **imlink, int *Nsgroup));
-Group        *find_image_tgroups PROTO((FITS_DB *db, ImageLink **imlink, int *Ntgroup));
-void          fit_sgroup         PROTO((Group *sgroup, int Nsgroup));
-void          fit_tgroup         PROTO((Group *tgroup, int Ntgroup));
-int           gcatalog           PROTO((Catalog *catalog));
-void          initialize         PROTO((int argc, char **argv));
-void          initstats          PROTO((char *mode));
-int           liststats          PROTO((double *value, double *dvalue, int N, StatType *stats));
-int           load_images        PROTO((FITS_DB *db));
-int           main               PROTO((int argc, char **argv));
-int           open_graph         PROTO((int N));
-void          sort               PROTO((unsigned int *X, int N));
-void          sortB              PROTO((double *X, double *Y, int N));
-void          sortD              PROTO((double *X, double *Y, double *Z, int N));
-void          update_dvo_catalog PROTO((Catalog *catalog, Group *sgroup, int warn));
-void          wcatalog           PROTO((Catalog *catalog));
-void          wimages            PROTO((Image *image, int Nimage));
-void 	      check_permissions  PROTO((char *basefile));
-void 	      lock_image_db 	 PROTO((FITS_DB *db, char *filename));
-void	      unlock_image_db 	 PROTO((FITS_DB *db));
-void	      create_image_db 	 PROTO((FITS_DB *db));
-void	      set_db 		 PROTO((FITS_DB *in));
-int 	      Shutdown 		 PROTO((char *format, ...) OHANA_FORMAT(printf, 1, 2) );
-void 	      TrapSignal 	 PROTO((int sig));
-void 	      SetProtect 	 PROTO((int mode));
-int 	      SetSignals 	 PROTO((void));
-int 	      subset_images 	 PROTO((FITS_DB *db));
-void 	      update 		 PROTO((FITS_DB *db, Group *sgroup, int Nsgroup));
-void 	      sort_time 	 PROTO((unsigned int *value, int N));
+void          ConfigInit          PROTO((int *argc, char **argv));
+void          DonePlotting        PROTO((Graphdata *graphmode, int N));
+void          JpegPlot            PROTO((Graphdata *graphmode, int N, char *filename));
+void          PSPlot              PROTO((Graphdata *graphmode, int N, char *filename));
+void          PlotLabel           PROTO((char *string, int N));
+void          PlotVector          PROTO((int Npts, double *vect, int mode, int N));
+void          PrepPlotting        PROTO((int Npts, Graphdata *graphmode, int N));
+void          XDead               PROTO((void));
+int           args_uniphot        PROTO((int argc, char **argv));
+int           args_setphot        PROTO((int argc, char **argv));
+void          dumpresult          PROTO((void));
+Group        *find_image_sgroups  PROTO((FITS_DB *db, ImageLink **imlink, int *Nsgroup));
+Group        *find_image_tgroups  PROTO((FITS_DB *db, ImageLink **imlink, int *Ntgroup));
+void          fit_sgroup          PROTO((Group *sgroup, int Nsgroup));
+void          fit_tgroup          PROTO((Group *tgroup, int Ntgroup));
+int           gcatalog            PROTO((Catalog *catalog));
+void          initialize_uniphot  PROTO((int argc, char **argv));
+void          initialize_setphot  PROTO((int argc, char **argv));
+void          initstats           PROTO((char *mode));
+int           liststats           PROTO((double *value, double *dvalue, int N, StatType *stats));
+int           load_images_uniphot PROTO((FITS_DB *db));
+int           main                PROTO((int argc, char **argv));
+int           open_graph          PROTO((int N));
+void          sort                PROTO((unsigned int *X, int N));
+void          sortB               PROTO((double *X, double *Y, int N));
+void          sortD               PROTO((double *X, double *Y, double *Z, int N));
+void          update_dvo_catalog  PROTO((Catalog *catalog, Group *sgroup, int warn));
+void          wcatalog            PROTO((Catalog *catalog));
+void          wimages             PROTO((Image *image, int Nimage));
+void 	      check_permissions   PROTO((char *basefile));
+void 	      lock_image_db 	  PROTO((FITS_DB *db, char *filename));
+void	      unlock_image_db 	  PROTO((FITS_DB *db));
+void	      create_image_db 	  PROTO((FITS_DB *db));
+void	      set_db 		  PROTO((FITS_DB *in));
+int 	      Shutdown 		  PROTO((char *format, ...) OHANA_FORMAT(printf, 1, 2) );
+void 	      TrapSignal 	  PROTO((int sig));
+void 	      SetProtect 	  PROTO((int mode));
+int 	      SetSignals 	  PROTO((void));
+int 	      subset_images 	  PROTO((FITS_DB *db));
+void 	      update 		  PROTO((FITS_DB *db, Group *sgroup, int Nsgroup));
+void 	      sort_time 	  PROTO((unsigned int *value, int N));
+
+// setphot-specific prototypes
+ZptTable     *load_zpt_table         PROTO((char *filename, int *nzpts));
+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_setphot         PROTO((Image *image, off_t Nimage));
+void          update_catalog_setphot PROTO((Catalog *catalog, Image *image, off_t *index, off_t Nimage));
Index: /branches/eam_branches/Ohana.20100407/src/uniphot/src/ConfigInit.c
===================================================================
--- /branches/eam_branches/Ohana.20100407/src/uniphot/src/ConfigInit.c	(revision 27769)
+++ /branches/eam_branches/Ohana.20100407/src/uniphot/src/ConfigInit.c	(revision 27770)
@@ -1,3 +1,5 @@
 # include "uniphot.h"
+
+void GetConfig (char *config, char *field, char *format, int N, void *ptr);
 
 void ConfigInit (int *argc, char **argv) {
@@ -18,5 +20,5 @@
   if (VERBOSE) fprintf (stderr, "loaded config file: %s\n", file);
 
-  ScanConfig (config, "CATDIR",                 "%s",  0, CATDIR);
+  GetConfig (config, "CATDIR",                 "%s",  0, CATDIR);
   ScanConfig (config, "CATMODE",                "%s",  0, CATMODE);
   ScanConfig (config, "CATFORMAT",              "%s",  0, CATFORMAT);
@@ -24,4 +26,12 @@
 
   sprintf (ImageCat, "%s/Images.dat", CATDIR);
+
+  GetConfig (config, "GSCFILE",                 "%s",  0, GSCFILE);
+  if (!ScanConfig (config, "SKY_DEPTH",         "%d",  0, &SKY_DEPTH)) {
+    SKY_DEPTH = 2;
+  }
+  if (!ScanConfig (config, "SKY_TABLE",         "%s",  0, SKY_TABLE)) {
+    SKY_TABLE[0] = 0;
+  }
 
   ScanConfig (config, "ZERO_PT",                "%lf", 0, &ZERO_POINT);
@@ -42,2 +52,14 @@
 
 }
+
+void GetConfig (char *config, char *field, char *format, int N, void *ptr) {
+
+  char *status;
+
+  status = ScanConfig (config, field, format, N, ptr);
+  if (status == NULL) {
+    fprintf (stderr, "error in config, cannot find %s\n", field);
+    exit (1);
+  }
+  return;
+}
Index: /branches/eam_branches/Ohana.20100407/src/uniphot/src/Shutdown.c
===================================================================
--- /branches/eam_branches/Ohana.20100407/src/uniphot/src/Shutdown.c	(revision 27769)
+++ /branches/eam_branches/Ohana.20100407/src/uniphot/src/Shutdown.c	(revision 27770)
@@ -23,5 +23,5 @@
   SetProtect (TRUE);
   gfits_db_close (db);
-  fprintf (stderr, "ERROR: addstar halted\n");
+  fprintf (stderr, "ERROR: halted\n");
   exit (1);
 }
Index: /branches/eam_branches/Ohana.20100407/src/uniphot/src/args.c
===================================================================
--- /branches/eam_branches/Ohana.20100407/src/uniphot/src/args.c	(revision 27769)
+++ /branches/eam_branches/Ohana.20100407/src/uniphot/src/args.c	(revision 27770)
@@ -1,5 +1,5 @@
 # include "uniphot.h"
 
-int args (int argc, char **argv) {
+int args_uniphot (int argc, char **argv) {
 
   int N;
@@ -72,2 +72,26 @@
 }
 
+int args_setphot (int argc, char **argv) {
+
+  int N;
+
+  VERBOSE = FALSE;
+  if ((N = get_argument (argc, argv, "-v"))) {
+    VERBOSE = TRUE;
+    remove_argument (N, &argc, argv);
+  }
+
+  UPDATE = FALSE;
+  if ((N = get_argument (argc, argv, "-update"))) {
+    remove_argument (N, &argc, argv);
+    UPDATE = TRUE;
+  }
+
+  if (argc != 2) {
+    fprintf (stderr, "ERROR: USAGE: setphot (zptfile) [options]\n");
+    exit (2);
+  } 
+
+  return (TRUE);
+}
+
Index: /branches/eam_branches/Ohana.20100407/src/uniphot/src/initialize.c
===================================================================
--- /branches/eam_branches/Ohana.20100407/src/uniphot/src/initialize.c	(revision 27769)
+++ /branches/eam_branches/Ohana.20100407/src/uniphot/src/initialize.c	(revision 27770)
@@ -1,9 +1,9 @@
 # include "uniphot.h"
 
-void initialize (int argc, char **argv) {
+void initialize_uniphot (int argc, char **argv) {
 
   /* are these set correctly? */
   ConfigInit (&argc, argv);
-  args (argc, argv);
+  args_uniphot (argc, argv);
 
   if ((photcode = GetPhotcodebyName (argv[1])) == NULL) {
@@ -21,2 +21,9 @@
 }
 
+void initialize_setphot (int argc, char **argv) {
+
+  /* are these set correctly? */
+  ConfigInit (&argc, argv);
+  args_setphot (argc, argv);
+}
+
Index: /branches/eam_branches/Ohana.20100407/src/uniphot/src/load_images.c
===================================================================
--- /branches/eam_branches/Ohana.20100407/src/uniphot/src/load_images.c	(revision 27769)
+++ /branches/eam_branches/Ohana.20100407/src/uniphot/src/load_images.c	(revision 27770)
@@ -1,5 +1,5 @@
 # include "uniphot.h"
 
-int load_images (FITS_DB *db) {
+int load_images_uniphot (FITS_DB *db) {
 
   if (VERBOSE) fprintf (stderr, "finding images\n");
@@ -23,4 +23,6 @@
   image = gfits_table_get_Image (&db[0].ftable, Nimage, &db[0].swapped);
 
+  fprintf (stderr, "loaded %lld images\n", (long long) *Nimage);
+
   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 27769)
+++ /branches/eam_branches/Ohana.20100407/src/uniphot/src/load_zpt_table.c	(revision 27770)
@@ -1,3 +1,3 @@
-# include "setphot.h"
+# include "uniphot.h"
 
 ZptTable *load_zpt_table (char *filename, int *nzpts) {
@@ -23,5 +23,6 @@
   while ((status = fscanf (f, "%lf %lf", &mjd, &zpt)) == 2) {
     zpts[Nzpts].zpt = zpt;
-    zpts[Nzpts].time = ohana_mjd_to_sec(mjd);
+    zpts[Nzpts].time = ohana_mjd_to_sec (mjd);
+    zpts[Nzpts].found = FALSE;
 
     Nzpts ++;
@@ -34,4 +35,6 @@
   }
 
+  fprintf (stderr, "loaded %d zero points\n", Nzpts);
+
   *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 27769)
+++ /branches/eam_branches/Ohana.20100407/src/uniphot/src/match_zpts_to_images.c	(revision 27770)
@@ -2,16 +2,18 @@
 
 /* sort a coordinate pair (X,Y) and the associated index (S) */
-void sort_zpts_by_time (ZptTable *zpts, int Nzpts) {
+static 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;	\
+    tmp = zpts[A]; zpts[A] = zpts[B]; zpts[B] = tmp;	\
   }
-# define COMPARE(A,B)(X[A].time < X[B].time)
+# define COMPARE(A,B)(zpts[A].time < zpts[B].time)
 
-  OHANA_SORT (N, COMPARE, SWAPFUNC);
+  OHANA_SORT (Nzpts, COMPARE, SWAPFUNC);
 
 # undef SWAPFUNC
 # undef COMPARE
 }
+
+# define SCALE 0.001
 
 int match_zpts_to_images (Image *image, off_t Nimage, ZptTable *zpts, int Nzpts) {
@@ -25,4 +27,5 @@
   int dT, NImatch, Nmatch;
   off_t i, Ni, Nz, *index;
+  PhotCode *code;
 
   // create index and sort
@@ -31,5 +34,5 @@
     index[i] = i;
   }
-  sort_image_subset(image, index, Nimage);  // slightly misnamed sort function
+  sort_image_subset(image, index, Nimage);  // slightly misnamed sort function from libdvo
 
   // sort the zpts
@@ -38,4 +41,7 @@
   NImatch = 0; // matched images
   for (i = Nz = 0; (i < Nimage) && (Nz < Nzpts); ) {
+
+    if (i % 1000 == 0) fprintf (stderr, ".");
+    if (Nz % 100 == 0) fprintf (stderr, "!");
 
     Ni = index[i];
@@ -55,6 +61,13 @@
     }
 
+    // check that we have a valid photcode (skip mosaic images)
+    code = GetPhotcodebyCode(image[Ni].photcode);
+    if (!code) { 
+      i++;
+      continue;
+    }
+
     // we have a match: set zpt and record the match
-    image[Ni].Mcal = zpts[Nz].zpt; // XXX watch out for offset..
+    image[Ni].Mcal = SCALE*code[0].C - zpts[Nz].zpt;
     image[Ni].flags &= ~ID_IMAGE_NOCAL; // clear the NOCAL flag
     zpts[Nz].found = TRUE;
@@ -68,8 +81,8 @@
   Nmatch = 0;
   for (Nz = 0; Nz < Nzpts; Nz++) {
-    if (image[Nz].found) Nmatch ++;
+    if (zpts[Nz].found) Nmatch ++;
   }
 
-  fprintf (stderr, "found %d zpt matches\n", 
+  fprintf (stderr, "found %d zpt matches, %d image matches\n", Nmatch, NImatch);
 
   return (TRUE);
Index: /branches/eam_branches/Ohana.20100407/src/uniphot/src/setphot.c
===================================================================
--- /branches/eam_branches/Ohana.20100407/src/uniphot/src/setphot.c	(revision 27769)
+++ /branches/eam_branches/Ohana.20100407/src/uniphot/src/setphot.c	(revision 27770)
@@ -1,8 +1,8 @@
-# include "setphot.h"
+# include "uniphot.h"
 
 int main (int argc, char **argv) {
 
   off_t Nimage;
-  int Nzpts;
+  int status, Nzpts;
   FITS_DB db;
   ZptTable *zpts;
@@ -10,5 +10,5 @@
 
   /* get configuration info, args, lockfile */
-  initialize (argc, argv);
+  initialize_setphot (argc, argv);
 
   set_db (&db);
@@ -16,17 +16,19 @@
   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 */
+  // load images 
   image  = load_images_setphot (&db, &Nimage);
+  if (!UPDATE) dvo_image_unlock (&db); 
   
   match_zpts_to_images (image, Nimage, zpts, Nzpts);
 
-  update_catalogs();
+  update_setphot(image, Nimage);
 
-  /** write image table **/
-  dvo_image_update (db, VERBOSE);
+  // write image table
+  if (UPDATE) {
+    dvo_image_save (&db, VERBOSE);
+  }
 
   exit (0);
Index: /branches/eam_branches/Ohana.20100407/src/uniphot/src/uniphot.c
===================================================================
--- /branches/eam_branches/Ohana.20100407/src/uniphot/src/uniphot.c	(revision 27769)
+++ /branches/eam_branches/Ohana.20100407/src/uniphot/src/uniphot.c	(revision 27770)
@@ -9,5 +9,5 @@
 
   /* get configuration info, args, lockfile */
-  initialize (argc, argv);
+  initialize_uniphot (argc, argv);
 
   set_db (&db);
@@ -18,5 +18,5 @@
 
   /* load images */
-  load_images (&db);
+  load_images_uniphot (&db);
   
   /* filter image list by selection */
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 27769)
+++ /branches/eam_branches/Ohana.20100407/src/uniphot/src/update_catalog_setphot.c	(revision 27770)
@@ -2,12 +2,11 @@
 # include <glob.h>
 
-intax update_setphot (Image *image, off_t Nimage) {
+int update_setphot (Image *image, off_t Nimage) {
 
+  SkyRegion UserPatch;
   SkyTable *sky = NULL;
   SkyList *skylist = NULL;
   Catalog catalog;
-
-  off_t i, Nimage, maxID, *index;
-  int j, status, Nmin;
+  off_t i, maxID, *index;
 
   // create an index for the image IDs
@@ -18,5 +17,5 @@
   ALLOCATE (index, off_t, maxID + 1);
   for (i = 0; i < Nimage; i++) {
-    if (image[i].imageID < 0) continue;
+    if (!image[i].imageID) continue; // images with ID == 0 are virtual
     index[image[i].imageID] = i;
   }
@@ -28,4 +27,8 @@
   
   // 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);
 
@@ -79,5 +82,5 @@
     for (j = 0; j < catalog[0].average[i].Nmeasure; j++, m++) {
       idx = catalog[0].measure[m].imageID;
-      if (idx < 0) continue;
+      if (idx <= 0) continue; // detections with imageID == 0 do not have a valid image (eg, ref photcode)
 
       id = index[idx];
@@ -90,5 +93,5 @@
 
   if (found) {
-    fprintf (stderr, "found %d matches\n", found);
+    fprintf (stderr, "found %lld matches\n", (long long) found);
   }
 }
