Index: /branches/eam_branches/ipp-20130509/Ohana/src/uniphot/Makefile
===================================================================
--- /branches/eam_branches/ipp-20130509/Ohana/src/uniphot/Makefile	(revision 35663)
+++ /branches/eam_branches/ipp-20130509/Ohana/src/uniphot/Makefile	(revision 35664)
@@ -1,3 +1,3 @@
-default: uniphot setphot setphot_client setfwhm setposangle setposangle_client setastrom setastrom_client fiximids fiximids_client
+default: uniphot setphot setphot_client setfwhm setposangle setposangle_client setastrom setastrom_client fiximids fiximids_client fixstkids fixstkids_client
 help:
 @echo "make options: uniphot setphot setfwhm default help install default (uniphot setphot setfwhm)"
@@ -27,6 +27,8 @@
 fiximids: $(BIN)/fiximids.$(ARCH)
 fiximids_client: $(BIN)/fiximids_client.$(ARCH)
+fixstkids: $(BIN)/fixstkids.$(ARCH)
+fixstkids_client: $(BIN)/fixstkids_client.$(ARCH)
 
-install: $(DESTBIN)/uniphot $(DESTBIN)/setfwhm $(DESTBIN)/setphot $(DESTBIN)/setphot_client $(DESTBIN)/setposangle $(DESTBIN)/setposangle_client $(DESTBIN)/setastrom $(DESTBIN)/setastrom_client $(DESTBIN)/fiximids $(DESTBIN)/fiximids_client
+install: $(DESTBIN)/uniphot $(DESTBIN)/setfwhm $(DESTBIN)/setphot $(DESTBIN)/setphot_client $(DESTBIN)/setposangle $(DESTBIN)/setposangle_client $(DESTBIN)/setastrom $(DESTBIN)/setastrom_client $(DESTBIN)/fiximids $(DESTBIN)/fiximids_client $(DESTBIN)/fixstkids $(DESTBIN)/fixstkids_client
 
 UNIPHOT = \
@@ -166,2 +168,28 @@
 $(BIN)/fiximids_client.$(ARCH): $(FIXIMIDS_CLIENT)
 
+FIXSTKIDS = \
+$(SRC)/fixstkids.$(ARCH).o	         \
+$(SRC)/initialize_fixstkids.$(ARCH).o     \
+$(SRC)/load_images_fixstkids.$(ARCH).o    \
+$(SRC)/ImageSubsetFixImageIDs.$(ARCH).o  \
+$(SRC)/update_dvo_fixstkids.$(ARCH).o     \
+$(SRC)/update_catalog_fixstkids.$(ARCH).o \
+$(SRC)/SetSignals.$(ARCH).o	         \
+$(SRC)/Shutdown.$(ARCH).o	    
+
+$(FIXSTKIDS): $(INC)/fixstkids.h
+$(BIN)/fixstkids.$(ARCH): $(FIXSTKIDS)
+
+FIXSTKIDS_CLIENT = \
+$(SRC)/fixstkids_client.$(ARCH).o	 \
+$(SRC)/initialize_fixstkids.$(ARCH).o     \
+$(SRC)/load_images_fixstkids.$(ARCH).o    \
+$(SRC)/ImageSubsetFixStackIDs.$(ARCH).o  \
+$(SRC)/update_dvo_fixstkids.$(ARCH).o     \
+$(SRC)/update_catalog_fixstkids.$(ARCH).o \
+$(SRC)/SetSignals.$(ARCH).o	         \
+$(SRC)/Shutdown.$(ARCH).o
+
+$(FIXSTKIDS_CLIENT): $(INC)/fixstkids.h
+$(BIN)/fixstkids_client.$(ARCH): $(FIXSTKIDS_CLIENT)
+
Index: /branches/eam_branches/ipp-20130509/Ohana/src/uniphot/include/fixstkids.h
===================================================================
--- /branches/eam_branches/ipp-20130509/Ohana/src/uniphot/include/fixstkids.h	(revision 35663)
+++ /branches/eam_branches/ipp-20130509/Ohana/src/uniphot/include/fixstkids.h	(revision 35664)
@@ -34,11 +34,11 @@
 void          ConfigInit                        PROTO((int *argc, char **argv));
 
-void          usage_fixstkids                    PROTO(());
-void          initialize_fixstkids               PROTO((int argc, char **argv));
-int           args_fixstkids                     PROTO((int argc, char **argv));
+void          usage_fixstkids                   PROTO(());
+void          initialize_fixstkids              PROTO((int argc, char **argv));
+int           args_fixstkids                    PROTO((int argc, char **argv));
 					        
-void          usage_fixstkids_client             PROTO(());
-void          initialize_fixstkids_client        PROTO((int argc, char **argv));
-int           args_fixstkids_client              PROTO((int argc, char **argv));
+void          usage_fixstkids_client            PROTO(());
+void          initialize_fixstkids_client       PROTO((int argc, char **argv));
+int           args_fixstkids_client             PROTO((int argc, char **argv));
 
 ImageSubset  *ImageSubsetLoad                   PROTO((char *filename, off_t *nimage));
@@ -52,12 +52,13 @@
 int           SetSignals                        PROTO((void));
 
-Image        *load_images_fixstkids              PROTO((FITS_DB *db, off_t *Nimage));
-int           update_dvo_fixstkids               PROTO((ImageSubset *image, off_t Nimage));
-int           update_dvo_fixstkids_parallel      PROTO((SkyTable *sky, ImageSubset *image, off_t Nimage));
+Image        *load_images_fixstkids             PROTO((FITS_DB *db, off_t *Nimage));
+int           update_dvo_fixstkids              PROTO((ImageSubset *image, off_t Nimage));
+int           update_dvo_fixstkids_parallel     PROTO((SkyTable *sky, ImageSubset *image, off_t Nimage));
 					        
-void          update_catalog_fixstkids           PROTO((Catalog *catalog));
+void          update_catalog_fixstkids          PROTO((Catalog *catalog));
 
+void          sort_image_externID               PROTO((unsigned int *I, unsigned int *E, short *C, unsigned int *Q, off_t N));
 void  	      sort_image_times 			PROTO((e_time *S, e_time *E, short *C, unsigned int *I, unsigned int *Q, off_t N));
-int           FindImageID                       PROTO((off_t *ID, off_t *Seq, e_time time, short photcode));
+int           FindImageID                       PROTO((off_t *ID, off_t *Seq, short *photcode, off_t extID));
 void  	      initImageIndex   			PROTO((ImageSubset *image, off_t Nimage_init));
 
@@ -69,2 +70,4 @@
 int 	      ImageValidSave  		        PROTO((char *filename));
 int 	      ImageValidLoad		        PROTO((char *filename));
+
+int           UnpackPSPSStackDetectionID        PROTO((int *sourceID, int *imageID, int *detID, uint64_t pspsStackID));
Index: /branches/eam_branches/ipp-20130509/Ohana/src/uniphot/src/ImageSubsetFixStackIDs.c
===================================================================
--- /branches/eam_branches/ipp-20130509/Ohana/src/uniphot/src/ImageSubsetFixStackIDs.c	(revision 35663)
+++ /branches/eam_branches/ipp-20130509/Ohana/src/uniphot/src/ImageSubsetFixStackIDs.c	(revision 35664)
@@ -58,5 +58,5 @@
   GET_COLUMN (imageID,    "IMAGE_ID",   int);
   GET_COLUMN (externID,   "EXTERN_ID",  int);
-  GET_COLUMN (nstar,      "nstar",      int);
+  GET_COLUMN (nstar,      "NSTAR",      int);
   GET_COLUMN (photcode,   "PHOTCODE",   short);
 
Index: /branches/eam_branches/ipp-20130509/Ohana/src/uniphot/src/initialize_fixstkids.c
===================================================================
--- /branches/eam_branches/ipp-20130509/Ohana/src/uniphot/src/initialize_fixstkids.c	(revision 35663)
+++ /branches/eam_branches/ipp-20130509/Ohana/src/uniphot/src/initialize_fixstkids.c	(revision 35664)
@@ -68,9 +68,4 @@
     remove_argument (N, &argc, argv);
   }
-
-  // XXX I want to avoid running this on all data all the time
-  // some options: gpc1 only (hardwired), require photcodes (fairly tedious),
-  // exclude some photcodes?
-  // XXX for now, hardwire gpc1 photcodes
 
   SINGLE_CPT = NULL;
Index: /branches/eam_branches/ipp-20130509/Ohana/src/uniphot/src/load_images_fixstkids.c
===================================================================
--- /branches/eam_branches/ipp-20130509/Ohana/src/uniphot/src/load_images_fixstkids.c	(revision 35663)
+++ /branches/eam_branches/ipp-20130509/Ohana/src/uniphot/src/load_images_fixstkids.c	(revision 35664)
@@ -2,10 +2,9 @@
 # define FT_BZERO_INT32 1.0*0x80000000
 
-off_t   Nimage = 0;
-e_time *startImage = NULL;
-e_time *stopImage = NULL;
-short  *photcodeImage = NULL;
-unsigned int *imageID = NULL;
-unsigned int *imageSeq = NULL;
+off_t Nimage = 0;
+short        *photcodeImage = NULL;
+unsigned int *imageID       = NULL;
+unsigned int *externID      = NULL;
+unsigned int *imageSeq      = NULL;
 int *Nimage_valid = NULL;
 int *Nimage_invalid = NULL;
@@ -93,4 +92,14 @@
     *ID = imageID[N];
     *Seq = imageSeq[N];
+    
+    for (Ni = N + 1; Ni < Nimage; Ni++) {
+      if (extID < externID[Ni]) break;
+      if (externID[Ni] == extID) {
+	fprintf (stderr, "found second image ID matching extern ID?\n");
+	fprintf (stderr, "first  ext ID: %d, image ID: %d, photcode: %d\n", externID[N], imageID[N], photcodeImage[N]);
+	fprintf (stderr, "second ext ID: %d, image ID: %d, photcode: %d\n", externID[Ni], imageID[Ni], photcodeImage[Ni]);
+	exit (2);
+      }
+    }
     return TRUE;
   }
@@ -122,7 +131,6 @@
     if (image[i].photcode == 0) continue; // skip images with 0 photcode (eg, PHU)
 
-    char *date = ohana_sec_to_date (image[i].tzero);
     char *code = GetPhotcodeNamebyCode (image[i].photcode);
-    fprintf (stderr, "image %s %s : %d  %d  %d  : %f\n", date, code, image[i].nstar, Nimage_valid[i], Nimage_invalid[i], 
+    fprintf (stderr, "image %s extID %d imageID %d : %d  %d  %d  : %f\n", code, image[i].imageID, image[i].externID, image[i].nstar, Nimage_valid[i], Nimage_invalid[i], 
 	     (Nimage_valid[i] + Nimage_invalid[i]) / (float) image[i].nstar);
   }
@@ -166,5 +174,5 @@
   tmp_u = Q[A]; Q[A] = Q[B]; Q[B] = tmp_u; \
 }
-# define COMPARE(A,B)(S[A] < S[B])
+# define COMPARE(A,B)(E[A] < E[B])
 
   OHANA_SORT (N, COMPARE, SWAPFUNC);
Index: /branches/eam_branches/ipp-20130509/Ohana/src/uniphot/src/update_catalog_fixstkids.c
===================================================================
--- /branches/eam_branches/ipp-20130509/Ohana/src/uniphot/src/update_catalog_fixstkids.c	(revision 35663)
+++ /branches/eam_branches/ipp-20130509/Ohana/src/uniphot/src/update_catalog_fixstkids.c	(revision 35664)
@@ -9,5 +9,5 @@
   off_t Ninvalid = 0;    
 
-  off_t imageIDs[MAX_DUPS];
+  int sourceID, externID, detID;
 
   for (i = 0; i < catalog[0].Nmeasure; i++) {
@@ -18,5 +18,5 @@
 
     // reverse engineer the detID, imageID, sourceID from the psps det ID (extID)
-    UnpackPSPSstackDetID (&sourceID, &externID, &detID, catalog[0].measure[i].extID);
+    UnpackPSPSStackDetectionID (&sourceID, &externID, &detID, catalog[0].measure[i].extID);
     myAssert (sourceID = 35, "problem with source ID %d (%0x08 %0x08)", sourceID, catalog[0].measure[i].objID, catalog[0].measure[i].catID);
     myAssert (detID = catalog[0].measure[i].detID, "problem with det ID %d (%0x08 %0x08)", detID, catalog[0].measure[i].objID, catalog[0].measure[i].catID);
@@ -24,4 +24,5 @@
     // we now have the externID of the given detection -- find the real image in the image table
     off_t imageID, Seq;
+    short Photcode;
     if (!FindImageID (&imageID, &Seq, &Photcode, externID)) continue;
 
@@ -32,5 +33,5 @@
 
     // validate the photcode and time?
-    myAssert (catalog[0].measure[i].photcode == Photcode, "bad photcode match? objID, catID: (%0x08 %0x08), imageID, externID: %d, %d", catalog[0].measure[i].objID, catalog[0].measure[i].catID, imageID, externID)
+    myAssert (catalog[0].measure[i].photcode == Photcode, "bad photcode match? objID, catID: (%0x08 %0x08), imageID, externID: "OFF_T_FMT", %d", catalog[0].measure[i].objID, catalog[0].measure[i].catID, imageID, externID)
 
     if (imageID == catalog[0].measure[i].imageID) {
@@ -53,2 +54,21 @@
 }
 
+int UnpackPSPSStackDetectionID(int *sourceID, int *imageID, int *detID, uint64_t pspsStackID)
+{
+  // sourceID : ID of database + table that tracked the image (< 0x7f = 127)
+  // imageID : external ID of the image which provided the detections (< 0x1000.0000 ~ 2.7e8)
+  // detID : detection sequence in image (< 0x1000.0000 ~ 2.7e8)
+
+  // 0x0000.0000.0000.0000
+
+  // bits  0 - 27 : 0x0000.0000.0fff.ffff
+  // bits 28 - 55 : 0x00ff.ffff.f000.0000
+  // bits 56 - 63 : 0xff00.0000.0000.0000
+
+  *sourceID = (pspsStackID & (uint64_t) 0xff00000000000000) >> 56;
+  *imageID  = (pspsStackID & (uint64_t) 0x00fffffff0000000) >> 28;
+  *detID    = (pspsStackID & (uint64_t) 0x000000000fffffff);
+
+  return TRUE;
+}
+    
