Index: /branches/eam_branches/ipp-20150625/Ohana/src/uniphot/Makefile
===================================================================
--- /branches/eam_branches/ipp-20150625/Ohana/src/uniphot/Makefile	(revision 38630)
+++ /branches/eam_branches/ipp-20150625/Ohana/src/uniphot/Makefile	(revision 38631)
@@ -67,4 +67,5 @@
 $(SRC)/update_dvo_setphot.$(ARCH).o \
 $(SRC)/update_catalog_setphot.$(ARCH).o \
+$(SRC)/repair_catalog_by_objID.$(ARCH).o \
 $(SRC)/SetSignals.$(ARCH).o	    \
 $(SRC)/Shutdown.$(ARCH).o	    
@@ -77,4 +78,5 @@
 $(SRC)/update_dvo_setphot.$(ARCH).o \
 $(SRC)/update_catalog_setphot.$(ARCH).o \
+$(SRC)/repair_catalog_by_objID.$(ARCH).o \
 $(SRC)/initialize_setphot_client.$(ARCH).o \
 $(SRC)/SetSignals.$(ARCH).o	    \
Index: /branches/eam_branches/ipp-20150625/Ohana/src/uniphot/include/setphot.h
===================================================================
--- /branches/eam_branches/ipp-20150625/Ohana/src/uniphot/include/setphot.h	(revision 38630)
+++ /branches/eam_branches/ipp-20150625/Ohana/src/uniphot/include/setphot.h	(revision 38631)
@@ -35,4 +35,5 @@
 int          PARALLEL_SERIAL;
 int          IMAGES_ONLY;
+int          REPAIR_BY_OBJID;
 
 SkyRegion    UserPatch;
@@ -76,2 +77,4 @@
 int           parse_zpt_offsets           PROTO((char *ZPT_OFFSET_FILTERS, char *ZPT_OFFSET_VALUES));
 float         apply_zpt_offset            PROTO((short code));
+
+int repair_catalog_by_objID (Catalog *catalog);
Index: anches/eam_branches/ipp-20150625/Ohana/src/uniphot/include/setphot_client.h
===================================================================
--- /branches/eam_branches/ipp-20150625/Ohana/src/uniphot/include/setphot_client.h	(revision 38630)
+++ 	(revision )
@@ -1,170 +1,0 @@
-# include <ohana.h>
-# include <dvo.h>
-# include <signal.h>
-
-# ifdef ANSI
-#   define F_SETFL      4   
-#   define O_NONBLOCK   0200000  
-#   define AF_UNIX      1          
-#   define SOCK_STREAM  1          
-#   define ENOENT       2       /* No such file or directory    */
-# endif /* ANSI */
-
-typedef struct {
-  double median;
-  double mean;
-  double sigma;
-  double error;
-  double chisq;
-  double min;
-  double max;
-  double total;
-  int    Nmeas;
-} StatType;
-
-typedef struct {
-  void *tgroup;
-  void *sgroup;
-} ImageLink;
-
-typedef struct {
-  char tstart[64];
-  char tstop[64];
-  char label[64];
-  float M;
-  float dM;
-  float dMsub;
-  double v1, v2;
-  Image **image;
-  ImageLink **imlink;
-  int Nimage, Ngood;
-} Group;
-
-typedef struct {
-    float zpt;
-    float zpt_err;
-    e_time time;
-    int found;
-} ZptTable;
-
-typedef struct {
-    float fwhm_major;
-    float fwhm_minor;
-    e_time time;
-    int found;
-    unsigned short photcode;
-} FWHMTable;
-
-/* global variables set in parameter file */
-char         ImageCat[256];
-char         CATDIR[256];
-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;
-int          UBERCAL; // load the supplied ubercal zero point fits table (with flat-field corrections)
-int          NO_METADATA; // the supplied ubercal data has no descriptive metadata
-int          NLOOP;
-int          TimeSelect;
-int          VERBOSE;
-int          UPDATE;
-int          IMAGE_BAD;
-double       RADIUS;
-double       TRANGE;
-time_t 	     TSTART;
-time_t 	     TSTOP;
-PhotCode    *photcode;
-
-SkyRegion    UserPatch;
-
-enum {black, white, red, orange, yellow, green, blue, indigo, violet};
-
-typedef struct {
-  double xmin, xmax, ymin, ymax;
-  int style, ptype, ltype, etype, color;
-  double lweight, size;
-} UPGraphdata;
-
-/***** prototypes ****/
-void          ConfigInit          PROTO((int *argc, char **argv));
-void          DonePlotting        PROTO((UPGraphdata *graphmode, int N));
-void          JpegPlot            PROTO((UPGraphdata *graphmode, int N, char *filename));
-void          PSPlot              PROTO((UPGraphdata *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, UPGraphdata *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          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_dvo_uniphot  PROTO((FITS_DB *db, Group *sgroup, int Nsgroup));
-void          update_catalog_uniphot PROTO((Catalog *catalog, Group *sgroup, int warn));
-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_dvo_setphot     PROTO((Image *image, off_t Nimage, FlatCorrectionTable *flatcorr));
-void          update_catalog_setphot PROTO((Catalog *catalog, Image *image, off_t *index, off_t Nimage, FlatCorrectionTable *flatcorr));
-
-ZptTable *load_zpt_ubercal (char *filename, int *nzpts, FlatCorrectionTable *flatcorrTable);
-ZptTable *load_zpt_ubercal_nometadata (char *filename, int *nzpts, FlatCorrectionTable *flatcorrTable);
-int match_flatcorr_to_images (Image *image, off_t Nimage, FlatCorrectionTable *flatcorrTable);
-
-/*** time/coord conversion functions not supplied by libohana ***/
-time_t        TimeRef               PROTO((double time, time_t TimeReference, int TimeFormat));
-double        TimeValue             PROTO((time_t time, time_t TimeReference, int TimeFormat));
-
-int           hh_hms                PROTO((double hh, int *hr, int *mn, double *sc));
-int           dd_dms                PROTO((double dd, int *dg, int *mn, double *sc));
-int           hms_format            PROTO((char *line, double value));
-int           dms_format            PROTO((char *line, double value));
-int           hh_hm                 PROTO((double hh, int *hr, double *mn));
-int           day_to_sec            PROTO((char *string, time_t *second));
-int           hms_to_sec            PROTO((char *string, time_t *second));
-char         *ohana_sec_to_hms      PROTO((time_t second));
-char         *ohana_sec_to_day      PROTO((time_t second));
-
-char         *meade_deg_to_str      PROTO((double deg));
-char         *meade_ra_to_str       PROTO((double deg));
-char         *meade_dec_to_str      PROTO((double deg));
-char         *strptime              PROTO((const char *s, const char *format, struct tm *tm));
-time_t        GetTimeReference      PROTO((char *reference));
-int           GetTimeUnits          PROTO((char *name));
-
-void          initialize_setfwhm    PROTO((int argc, char **argv));
-int           args_setfwhm          PROTO((int argc, char **argv));
-FWHMTable    *load_fwhm_table       PROTO((char *filename, int *nfwhm));
-int           match_fwhm_to_images  PROTO((Image *image, off_t Nimage, FWHMTable *fwhm, int Nfwhm));
-
Index: /branches/eam_branches/ipp-20150625/Ohana/src/uniphot/src/initialize_setphot.c
===================================================================
--- /branches/eam_branches/ipp-20150625/Ohana/src/uniphot/src/initialize_setphot.c	(revision 38630)
+++ /branches/eam_branches/ipp-20150625/Ohana/src/uniphot/src/initialize_setphot.c	(revision 38631)
@@ -49,4 +49,10 @@
   if ((N = get_argument (argc, argv, "-reset"))) {
     RESET = TRUE;
+    remove_argument (N, &argc, argv);
+  }
+
+  REPAIR_BY_OBJID = FALSE;
+  if ((N = get_argument (argc, argv, "-repair-by-objid"))) {
+    REPAIR_BY_OBJID = TRUE;
     remove_argument (N, &argc, argv);
   }
Index: /branches/eam_branches/ipp-20150625/Ohana/src/uniphot/src/repair_catalog_by_objID.c
===================================================================
--- /branches/eam_branches/ipp-20150625/Ohana/src/uniphot/src/repair_catalog_by_objID.c	(revision 38631)
+++ /branches/eam_branches/ipp-20150625/Ohana/src/uniphot/src/repair_catalog_by_objID.c	(revision 38631)
@@ -0,0 +1,82 @@
+# include "setphot.h"
+
+int repair_catalog_by_objID (Catalog *catalog) {
+
+  // first find the entries which are bad (there may be none)
+  int Nbad = 0;
+
+  Average *average = catalog->average;
+  Measure *measure = catalog->measure;
+
+  off_t i, j;
+  for (i = 0; i < catalog->Naverage; i++) {
+    off_t m = average[i].measureOffset;
+    for (j = 0; j < average[i].Nmeasure; j++) {
+      if (average[i].objID == measure[m+j].objID) continue;
+      
+      // this is a bad entry.  now we need to decide if it can be repaired
+      
+      Measure *myMeasure = &measure[m+j];
+      int iTest = myMeasure->objID;
+      myAssert (average[iTest].objID == iTest, "cannot find target source");
+      
+      double dR = 3600.0*(average[iTest].R - myMeasure->R) * cos (RAD_DEG*myMeasure->D);
+      double dD = 3600.0*(average[iTest].D - myMeasure->D);
+      double dRad = hypot (dR, dD);
+      myAssert (dRad < 2.0, "bad detection does not seem to match target source");
+      
+      char *date = ohana_sec_to_date (myMeasure->t);
+      fprintf (stderr, "bad detection found: matches averef %d, objID %d, %s : %6.3f %5d : %f, %f = %f\n", iTest, myMeasure->objID, date, myMeasure->M, myMeasure->photcode, dR, dD, dRad);
+      free (date);
+      Nbad ++;
+    }
+  }
+  if (!Nbad) return TRUE;
+
+  // we have at least one bad detection
+  // make a matched list of the measurement sequence and the objID values
+
+  off_t Nmeasure = catalog->Nmeasure;
+  ALLOCATE_PTR (seq,   off_t, Nmeasure);
+  ALLOCATE_PTR (objID, off_t, Nmeasure);
+
+  for (i = 0; i < Nmeasure; i++) {
+    seq[i]   = i;
+    objID[i] = measure[i].objID;
+  }    
+
+  // sort seq by objID
+  llsortpair (objID, seq, Nmeasure);
+
+  // create a new array of measures in objID order
+  Measure *measureNew = NULL;
+  ALLOCATE (measureNew, Measure, Nmeasure);
+  
+  for (i = 0; i < Nmeasure; i++) {
+    j = seq[i];
+    measureNew[i] = measure[j];
+  }
+
+  // reset averef and Nmeasure values for the average entries
+  off_t N = 0;
+  off_t myObjID = objID[0];
+  off_t averef = 0;
+  average[averef].measureOffset = 0;
+  for (i = 0; i < Nmeasure; i++) {
+    if (objID[i] != myObjID) {
+      // we have hit the next entry in the list
+      average[averef].Nmeasure = N;
+      N = 0;
+      myObjID = objID[i];
+      averef ++;
+      average[averef].measureOffset = i;
+    }
+    N++;
+  }
+  average[averef].Nmeasure = N;
+
+  free (catalog->measure);
+  catalog->measure = measureNew;
+
+  return TRUE;
+}
Index: /branches/eam_branches/ipp-20150625/Ohana/src/uniphot/src/update_dvo_setphot.c
===================================================================
--- /branches/eam_branches/ipp-20150625/Ohana/src/uniphot/src/update_dvo_setphot.c	(revision 38630)
+++ /branches/eam_branches/ipp-20150625/Ohana/src/uniphot/src/update_dvo_setphot.c	(revision 38631)
@@ -65,4 +65,10 @@
       dvo_catalog_free (&catalog);
       continue;
+    }
+
+    if (REPAIR_BY_OBJID) {
+      // For gpc1 / PV3, I broke some catalogs with dvomerge -replace: the last object can
+      // contaminate the first set of new measurements
+      repair_catalog_by_objID (&catalog);
     }
 
