Index: /branches/eam_branches/ipp-20130509/Ohana/src/delstar/Makefile
===================================================================
--- /branches/eam_branches/ipp-20130509/Ohana/src/delstar/Makefile	(revision 35710)
+++ /branches/eam_branches/ipp-20130509/Ohana/src/delstar/Makefile	(revision 35711)
@@ -31,4 +31,5 @@
 $(SRC)/delete_duplicate_images.$(ARCH).o \
 $(SRC)/delete_duplicate_measures.$(ARCH).o \
+$(SRC)/delete_fix_LAP.$(ARCH).o \
 $(SRC)/delete_image_photcodes.$(ARCH).o \
 $(SRC)/delete_photcodes.$(ARCH).o \
@@ -40,4 +41,6 @@
 $(SRC)/parse_time.$(ARCH).o       \
 $(SRC)/ImageIDsOps.$(ARCH).o       \
+$(SRC)/ImageOpsFixLAP.$(ARCH).o       \
+$(SRC)/ImageSubsetFixLAP.$(ARCH).o       \
 $(SRC)/check_permissions.$(ARCH).o \
 $(SRC)/Shutdown.$(ARCH).o          
@@ -50,7 +53,10 @@
 $(SRC)/delete_duplicate_images.$(ARCH).o \
 $(SRC)/delete_duplicate_measures.$(ARCH).o \
+$(SRC)/delete_fix_LAP.$(ARCH).o \
 $(SRC)/delete_photcodes.$(ARCH).o \
 $(SRC)/delete_photcodes_single.$(ARCH).o \
 $(SRC)/ImageIDsOps.$(ARCH).o       \
+$(SRC)/ImageOpsFixLAP.$(ARCH).o       \
+$(SRC)/ImageSubsetFixLAP.$(ARCH).o       \
 $(SRC)/Shutdown.$(ARCH).o          
 
Index: /branches/eam_branches/ipp-20130509/Ohana/src/delstar/include/delstar.h
===================================================================
--- /branches/eam_branches/ipp-20130509/Ohana/src/delstar/include/delstar.h	(revision 35710)
+++ /branches/eam_branches/ipp-20130509/Ohana/src/delstar/include/delstar.h	(revision 35711)
@@ -21,4 +21,18 @@
 } IndexArray;
 
+// to determine the image ID, we need time (tzero + trate) and photcode
+typedef struct {
+  unsigned int     imageID;
+  unsigned int     externID;
+  unsigned short   NX;                   // image width
+  unsigned short   NY;                   // image height
+  unsigned int     nstar;                // number of stars on the image
+  e_time           tzero;                // readout time (row 0)
+  e_time           tmin;                 // start exp - 1
+  e_time           tmax;                 // stop exp + 1
+  unsigned char    trate;                // scan rate (100 usec/pixel)
+  short            photcode;
+} ImageSubset;
+
 /* global variables set in parameter file */
 char   ImageCat[DVO_MAX_PATH];
@@ -33,4 +47,5 @@
 
 char  *IMAGENAME;
+char  *IMAGES;
 
 double NSIGMA;
@@ -63,5 +78,5 @@
 
 int    MODE;
-enum {MODE_NONE, MODE_IMAGENAME, MODE_IMAGEFILE, MODE_TIME, MODE_ORPHAN, MODE_MISSED, MODE_PHOTCODES, MODE_DUP_IMAGES, MODE_DUP_MEASURES};
+enum {MODE_NONE, MODE_IMAGENAME, MODE_IMAGEFILE, MODE_TIME, MODE_ORPHAN, MODE_MISSED, MODE_PHOTCODES, MODE_DUP_IMAGES, MODE_DUP_MEASURES, MODE_FIX_LAP};
 
 char DateKeyword[64], DateMode[64], UTKeyword[64], MJDKeyword[64], JDKeyword[64];
@@ -131,2 +146,17 @@
 int delete_duplicate_measures_catalog (Catalog *catalog);
 
+int delete_fix_LAP (ImageSubset *image, off_t Nimage);
+int delete_fix_LAP_catalog (Catalog *catalog, ImageSubset *image, off_t Nimage);
+int delete_fix_LAP_measures (off_t *measureDrop, Catalog *catalog, ImageSubset *image, off_t Nimage);
+int delete_fix_LAP_parallel (SkyList *sky, ImageSubset *image, off_t Nimage);
+
+void initImageIndex (ImageSubset *image, off_t Nimage_init);
+int *getImageIndex ();
+int FindIDexp (off_t *ID, off_t *Seq, e_time time, short photcode);
+int FindIDstk (off_t *ID, off_t *Seq, short *photcode, off_t extID);
+
+ImageSubset *ImageSubsetLoad(char *filename, off_t *nimage);
+int ImageSubsetSave(char *filename, ImageSubset *image, off_t Nimage);
+ImageSubset *ImagesToSubset (Image *image, off_t N);
+
+void sort_fullIDs (uint64_t *I, int *S, off_t N);
Index: /branches/eam_branches/ipp-20130509/Ohana/src/delstar/src/ImageOpsFixLAP.c
===================================================================
--- /branches/eam_branches/ipp-20130509/Ohana/src/delstar/src/ImageOpsFixLAP.c	(revision 35711)
+++ /branches/eam_branches/ipp-20130509/Ohana/src/delstar/src/ImageOpsFixLAP.c	(revision 35711)
@@ -0,0 +1,385 @@
+# include "delstar.h"
+# define FT_BZERO_INT32 1.0*0x80000000
+
+off_t   Nimage = 0;
+e_time *startImage_exp = NULL;
+e_time *stopImage_exp = NULL;
+short  *photcodeImage_exp = NULL;
+unsigned int *imageID_exp = NULL;
+unsigned int *imageSeq_exp = NULL;
+
+unsigned int *externID_stk = NULL;
+short  *photcodeImage_stk = NULL;
+unsigned int *imageID_stk = NULL;
+unsigned int *imageSeq_stk = NULL;
+
+int *imageIndex = NULL;
+
+// XX int *Nimage_valid = NULL;
+// XX int *Nimage_invalid = NULL;
+
+void sort_image_externID (unsigned int *E, short *C, unsigned int *I, unsigned int *Q, off_t N);
+void sort_image_times (e_time *S, e_time *E, short *C, unsigned int *I, unsigned int *Q, off_t N);
+
+// find mosaic frames (unique time periods & photcode name matches mosaic) 
+void initImageIndex (ImageSubset *image, off_t Nimage_init) {
+
+  off_t i;
+
+  Nimage = Nimage_init;
+  ALLOCATE (startImage_exp, e_time, Nimage);
+  ALLOCATE (stopImage_exp, e_time, Nimage);
+  ALLOCATE (photcodeImage_exp, short, Nimage);
+  ALLOCATE (imageID_exp, unsigned int, Nimage);
+  ALLOCATE (imageSeq_exp, unsigned int, Nimage);
+
+  ALLOCATE (externID_stk, unsigned int, Nimage);
+  ALLOCATE (photcodeImage_stk, short, Nimage);
+  ALLOCATE (imageID_stk, unsigned int, Nimage);
+  ALLOCATE (imageSeq_stk, unsigned int, Nimage);
+
+  int maxID = 0;
+
+  // save the image data in the local static arrays
+  for (i = 0; i < Nimage; i++) {
+
+    // set image time range (small boundary buffer) 
+    e_time start = image[i].tzero - MAX(0.01*image[i].trate*image[i].NY, 1);
+    e_time stop  = image[i].tzero + MAX(1.01*image[i].trate*image[i].NY, 1);
+
+    startImage_exp[i] = start;
+    stopImage_exp[i] = stop;
+    photcodeImage_exp[i] = image[i].photcode;
+    imageID_exp[i] = image[i].imageID;
+    imageSeq_exp[i] = i;
+
+    externID_stk[i] = image[i].externID;
+    photcodeImage_stk[i] = image[i].photcode;
+    imageID_stk[i] = image[i].imageID;
+    imageSeq_stk[i] = i;
+
+    maxID = MAX(image[i].imageID, maxID);
+  }
+
+  // sort the index, start, and stop by the start times:
+  sort_image_times (startImage_exp, stopImage_exp, photcodeImage_exp, imageID_exp, imageSeq_exp, Nimage);
+
+  // sort the index, start, and stop by the start times:
+  sort_image_externID (externID_stk, photcodeImage_stk, imageID_stk, imageSeq_stk, Nimage);
+  
+  ALLOCATE (imageIndex, int, maxID + 1);
+  for (i = 0; i < maxID + 1; i++) {
+    imageIndex[i] = -1;
+  }
+  for (i = 0; i < Nimage; i++) {
+    if (image[i].imageID == 0) continue;
+    imageIndex[image[i].imageID] = i;
+  }
+
+  return;
+}
+
+int *getImageIndex () {
+  return imageIndex;
+}
+
+// sort two times vectors and an index by first time vector
+void sort_image_times (e_time *S, e_time *E, short *C, unsigned int *I, unsigned int *Q, off_t N) {
+
+# define SWAPFUNC(A,B){ e_time tmp_t; unsigned int tmp_u; short tmp_c;	\
+  tmp_t = S[A]; S[A] = S[B]; S[B] = tmp_t; \
+  tmp_t = E[A]; E[A] = E[B]; E[B] = tmp_t; \
+  tmp_c = C[A]; C[A] = C[B]; C[B] = tmp_c; \
+  tmp_u = I[A]; I[A] = I[B]; I[B] = tmp_u; \
+  tmp_u = Q[A]; Q[A] = Q[B]; Q[B] = tmp_u; \
+}
+# define COMPARE(A,B)(S[A] < S[B])
+
+  OHANA_SORT (N, COMPARE, SWAPFUNC);
+
+# undef SWAPFUNC
+# undef COMPARE
+
+}
+
+// sort two times vectors and an index by first time vector
+void sort_image_externID (unsigned int *E, short *C, unsigned int *I, unsigned int *Q, off_t N) {
+
+# define SWAPFUNC(A,B){ unsigned int tmp_u; short tmp_c;	\
+  tmp_u = I[A]; I[A] = I[B]; I[B] = tmp_u; \
+  tmp_u = E[A]; E[A] = E[B]; E[B] = tmp_u; \
+  tmp_c = C[A]; C[A] = C[B]; C[B] = tmp_c; \
+  tmp_u = Q[A]; Q[A] = Q[B]; Q[B] = tmp_u; \
+}
+# define COMPARE(A,B)(E[A] < E[B])
+
+  OHANA_SORT (N, COMPARE, SWAPFUNC);
+
+# undef SWAPFUNC
+# undef COMPARE
+
+}
+
+// find the image by time. this search makes the following assumptions:
+// 1) there is only a single EXPOSURE from a single camera with the given exposure time
+// 2) there may be multiple IMAGES from that exposure, distinguished by photcode
+// 3) the image data hav been sorted by start time
+// 4) once we have (time < start[i]) we are past the desired exposure
+int FindIDexp (off_t *ID, off_t *Seq, e_time time, short photcode) {
+
+  off_t Nlo, Nhi, N, Ni;
+
+  *ID = 0;
+  *Seq = -1;
+
+  // Find the last image ending BEFORE 'time' (time > start[i]).
+  // Use bisection to find the overlapping images.
+  Nlo = 0; Nhi = Nimage;
+  while (Nhi - Nlo > 10) {
+    N = 0.5*(Nlo + Nhi);
+    if (stopImage_exp[N] < time) {
+      Nlo = MAX(N, 0);
+    } else {
+      Nhi = MIN(N + 1, Nimage);
+    }
+  }
+
+  // check for the matched images starting from Nlo 
+  for (N = Nlo; N < Nimage; N++) { 
+    if (time > stopImage_exp[N]) continue;
+    if (time < startImage_exp[N]) return FALSE;
+    // a possible image match (start <= time <= stop) 
+    for (Ni = N; (Ni < Nimage) && (time <= stopImage_exp[Ni]); Ni++) {
+      if (photcodeImage_exp[Ni] == photcode) {
+	*ID = imageID_exp[Ni];
+	*Seq = imageSeq_exp[Ni];
+	return TRUE;
+      }
+    }
+    return FALSE;
+  }
+  return FALSE;
+}
+
+// find the image by extern ID
+// 1) extID is unique among images
+// 3) the image data have been sorted by extID
+// 4) once we have (extID < externID[i]) we are past the desired exposure
+int FindIDstk (off_t *ID, off_t *Seq, short *photcode, off_t extID) {
+
+  off_t Nlo, Nhi, N, Ni;
+
+  *ID = 0;
+  *Seq = -1;
+  *photcode = 0;
+
+  // Find the last image with externID < extID
+  // Use bisection to find the overlapping images.
+  Nlo = 0; Nhi = Nimage;
+  while (Nhi - Nlo > 10) {
+    N = 0.5*(Nlo + Nhi);
+    if (externID_stk[N] < extID) {
+      Nlo = MAX(N, 0);
+    } else {
+      Nhi = MIN(N + 1, Nimage);
+    }
+  }
+
+  // check for the matched images starting from Nlo 
+  for (N = Nlo; N < Nimage; N++) { 
+    if (extID > externID_stk[N]) continue;
+    if (extID < externID_stk[N]) return FALSE;
+
+    // try to find other matches?
+    *photcode = photcodeImage_stk[N];
+    *ID = imageID_stk[N];
+    *Seq = imageSeq_stk[N];
+    
+    for (Ni = N + 1; Ni < Nimage; Ni++) {
+      if (extID < externID_stk[Ni]) break;
+      if (externID_stk[Ni] == extID) {
+	fprintf (stderr, "WARNING: found duplicate extern IDs : ext ID %d, image ID 1: %d (%d), image ID 2: %d (%d)\n", externID_stk[N], imageID_stk[N], photcodeImage_stk[N], imageID_stk[Ni], photcodeImage_stk[Ni]);
+      }
+    }
+    return TRUE;
+  }
+  return FALSE;
+}
+
+# if (0)
+// increment the count of valid detections for this image
+void BumpValidImage (int Seq) {
+  myAssert (Seq >= 0, "impossible seq");
+  myAssert (Seq < Nimage, "impossible seq");
+  Nimage_valid[Seq] ++;
+}
+
+// increment the count of invalid detections for this image
+void BumpInvalidImage (int Seq) {
+  myAssert (Seq >= 0, "impossible seq");
+  myAssert (Seq < Nimage, "impossible seq");
+  Nimage_invalid[Seq] ++;
+}
+
+void CompareImageCounts (ImageSubset *image, off_t Nimage_comp) {
+
+  off_t i;
+
+  for (i = 0; i < Nimage_comp; i++) {
+    // XXX can I limit the images to those in range of the user region?
+
+    if (Nimage_valid[i] + Nimage_invalid[i] == 0) continue;     // XXX for now, skip (optionally?) images with no matched detections
+    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], 
+	     (Nimage_valid[i] + Nimage_invalid[i]) / (float) image[i].nstar);
+  }
+}
+
+void SummaryImageStats (ImageSubset *image, off_t Nimage_comp) {
+
+  off_t i;
+
+  // let's report the following:
+  // Nimages with corrected image IDs
+  // Nimages with Nsum / Nstar < 0.95
+  // Nimages with Nsum / Nstar < 0.99
+
+  int Nfixed = 0;
+  int Nbad_1 = 0;
+  int Nbad_2 = 0;
+
+  for (i = 0; i < Nimage_comp; i++) {
+    if (Nimage_valid[i] + Nimage_invalid[i] == 0) continue;     // XXX for now, skip (optionally?) images with no matched detections
+    if (image[i].photcode == 0) continue; // skip images with 0 photcode (eg, PHU)
+
+    if (Nimage_invalid[i] > 0) Nfixed ++;
+    
+    float found_ratio = (Nimage_valid[i] + Nimage_invalid[i]) / (float) image[i].nstar;
+    
+    if (found_ratio < 0.99) Nbad_1 ++;
+    if (found_ratio < 0.95) Nbad_2 ++;
+  }
+
+  fprintf (stderr, "Nfixed: %d, Ndrop (>1%%) %d, Ndrop (>5%%) %d\n", Nfixed, Nbad_1, Nbad_2);
+}
+
+# define GET_COLUMN(OUT,NAME,TYPE) \
+  TYPE *OUT = gfits_get_bintable_column_data (&theader, &ftable, NAME, type, &Nrow, &Ncol); \
+  myAssert (!strcmp(type, #TYPE), "wrong column type");
+
+int ImageValidLoad(char *filename) {
+
+  int Ncol;
+  off_t i;
+  off_t Nrow;
+  Header header;
+  Header theader;
+  Matrix matrix;
+  FTable ftable;
+
+  FILE *f = fopen (filename, "r");
+  if (!f) {
+    fprintf (stderr, "ERROR: cannot open image subset file %s\n", filename);
+    return FALSE;
+  }
+
+  /* load in PHU segment (ignore) */
+  if (!gfits_fread_header (f, &header)) {
+    if (VERBOSE) fprintf (stderr, "can't read image subset header\n");
+    fclose (f);
+    return FALSE;
+  }
+  if (!gfits_fread_matrix (f, &matrix, &header)) {
+    if (VERBOSE) fprintf (stderr, "can't read image subset matrix\n");
+    gfits_free_header (&header);
+    fclose (f);
+    return FALSE;
+  }
+
+  ftable.header = &theader;
+
+  // load data for this header 
+  if (!gfits_load_header (f, &theader)) {
+    if (VERBOSE) fprintf (stderr, "can't read table header\n");
+    fclose (f);
+    return FALSE;
+  }
+
+  // read the fits table bytes
+  if (!gfits_fread_ftable_data (f, &ftable, FALSE)) {
+    if (VERBOSE) fprintf (stderr, "can't read table data\n");
+    fclose (f);
+    return FALSE;
+  }
+  fclose (f);
+
+  char type[16];
+
+  GET_COLUMN (Nvalid,     "NVALID",   int);
+  GET_COLUMN (Ninvalid,   "NINVALID", int);
+
+  myAssert (Nrow == Nimage, "wrong size imstats result file\n");
+
+  // merge the new arrays into the single array?
+  for (i = 0; i < Nrow; i++) {
+    Nimage_valid[i] += Nvalid[i];
+    Nimage_invalid[i] += Ninvalid[i];
+  }
+  fprintf (stderr, "loaded data for %lld images\n", (long long) Nrow);
+
+  free (Nvalid);
+  free (Ninvalid);
+
+  return TRUE;
+}
+
+int ImageValidSave(char *filename) {
+
+  Header header;
+  Header theader;
+  Matrix matrix;
+  FTable ftable;
+
+  gfits_init_header (&header);
+  header.extend = TRUE;
+  gfits_create_header (&header);
+  gfits_create_matrix (&header, &matrix);
+
+  gfits_create_table_header (&theader, "BINTABLE", "IMAGE_VALID");
+
+  // create the table layout
+  gfits_define_bintable_column (&theader, "J",   "NVALID",     "tmp", 	   "tmp", 1.0, FT_BZERO_INT32);
+  gfits_define_bintable_column (&theader, "J",   "NINVALID",   "tmp", 	   "tmp", 1.0, FT_BZERO_INT32);
+
+  // generate the output array that carries the data
+  gfits_create_table (&theader, &ftable);
+
+  // add the columns to the output array
+  gfits_set_bintable_column (&theader, &ftable, "NVALID",     Nimage_valid,   Nimage);
+  gfits_set_bintable_column (&theader, &ftable, "NINVALID",   Nimage_invalid, Nimage);
+
+  FILE *f = fopen (filename, "w");
+  if (!f) {
+    fprintf (stderr, "ERROR: cannot open image subset file for output %s\n", filename);
+    return FALSE;
+  }
+
+  gfits_fwrite_header  (f, &header);
+  gfits_fwrite_matrix  (f, &matrix);
+  gfits_fwrite_Theader (f, &theader);
+  gfits_fwrite_table   (f, &ftable);
+
+  gfits_free_header (&header);
+  gfits_free_matrix (&matrix);
+  gfits_free_header (&theader);
+  gfits_free_table (&ftable);
+
+  fclose (f);
+  fflush (f);
+
+  return TRUE;
+}
+# endif
Index: /branches/eam_branches/ipp-20130509/Ohana/src/delstar/src/ImageSubsetFixLAP.c
===================================================================
--- /branches/eam_branches/ipp-20130509/Ohana/src/delstar/src/ImageSubsetFixLAP.c	(revision 35711)
+++ /branches/eam_branches/ipp-20130509/Ohana/src/delstar/src/ImageSubsetFixLAP.c	(revision 35711)
@@ -0,0 +1,216 @@
+# include "delstar.h"
+# define FT_BZERO_INT16 1.0*0x8000	  
+# define FT_BZERO_INT32 1.0*0x80000000
+
+# define GET_COLUMN(OUT,NAME,TYPE) \
+  TYPE *OUT = gfits_get_bintable_column_data (&theader, &ftable, NAME, type, &Nrow, &Ncol); \
+  myAssert (!strcmp(type, #TYPE), "wrong column type");
+
+ImageSubset *ImageSubsetLoad(char *filename, off_t *nimage) {
+
+  int Ncol;
+  off_t i;
+  off_t Nrow;
+  Header header;
+  Header theader;
+  Matrix matrix;
+  FTable ftable;
+
+  *nimage = 0;
+  ImageSubset *image = NULL;
+
+  FILE *f = fopen (filename, "r");
+  if (!f) {
+    fprintf (stderr, "ERROR: cannot open image subset file %s\n", filename);
+    return NULL;
+  }
+
+  /* load in PHU segment (ignore) */
+  if (!gfits_fread_header (f, &header)) {
+    if (VERBOSE) fprintf (stderr, "can't read image subset header\n");
+    fclose (f);
+    return NULL;
+  }
+  if (!gfits_fread_matrix (f, &matrix, &header)) {
+    if (VERBOSE) fprintf (stderr, "can't read image subset matrix\n");
+    gfits_free_header (&header);
+    fclose (f);
+    return NULL;
+  }
+
+  ftable.header = &theader;
+
+  // load data for this header 
+  if (!gfits_load_header (f, &theader)) {
+    fclose (f);
+    return NULL;
+  }
+
+  // read the fits table bytes
+  if (!gfits_fread_ftable_data (f, &ftable, FALSE)) {
+    fclose (f);
+    return NULL;
+  }
+  fclose (f);
+
+  char type[16];
+
+  GET_COLUMN (imageID,    "IMAGE_ID",   int);
+  GET_COLUMN (externID,   "EXTERN_ID",  int);
+  GET_COLUMN (NX,         "NX",         short);
+  GET_COLUMN (NY,         "NY",         short);
+  GET_COLUMN (nstar,      "nstar",      int);
+  GET_COLUMN (tzero,      "TZERO",      int);
+  GET_COLUMN (trate,      "TRATE",      short);
+  GET_COLUMN (photcode,   "PHOTCODE"  , short);
+
+  ALLOCATE (image, ImageSubset, Nrow);
+  for (i = 0; i < Nrow; i++) {
+    image[i].imageID           = imageID[i];
+    image[i].externID          = externID[i];
+    image[i].NX                = NX[i];
+    image[i].NY                = NY[i];
+    image[i].nstar             = nstar[i];
+    image[i].tzero             = tzero[i];
+    image[i].trate             = trate[i];
+    image[i].photcode          = photcode[i];
+    image[i].tmin              = image[i].tzero - 1;
+    image[i].tmax              = image[i].tzero + image[i].trate*image[i].NY + 1;
+  }
+  fprintf (stderr, "loaded data for %lld images\n", (long long) Nrow);
+
+  free (imageID);
+  free (externID);
+  free (NX);
+  free (NY);
+  free (nstar);
+  free (tzero);
+  free (trate);
+  free (photcode);
+
+  *nimage = Nrow;
+  return image;
+}
+
+int ImageSubsetSave(char *filename, ImageSubset *image, off_t Nimage) {
+
+  off_t i;
+  Header header;
+  Header theader;
+  Matrix matrix;
+  FTable ftable;
+
+  gfits_init_header (&header);
+  header.extend = TRUE;
+  gfits_create_header (&header);
+  gfits_create_matrix (&header, &matrix);
+
+  gfits_create_table_header (&theader, "BINTABLE", "IMAGE_SUBSET");
+
+  // an unsigned int needs to have bzero of 0x8000
+  gfits_define_bintable_column (&theader, "J",   "IMAGE_ID",   "image ID",  "tmp", 1.0, FT_BZERO_INT32);
+  gfits_define_bintable_column (&theader, "J",   "EXTERN_ID",  "extern ID", "tmp", 1.0, FT_BZERO_INT32);
+  gfits_define_bintable_column (&theader, "I",   "NX",         "tmp", 	    "tmp", 1.0, 0.0);
+  gfits_define_bintable_column (&theader, "I",   "NY",         "tmp", 	    "tmp", 1.0, 0.0);
+  gfits_define_bintable_column (&theader, "J",   "NSTAR",      "tmp", 	    "tmp", 1.0, FT_BZERO_INT32);
+  gfits_define_bintable_column (&theader, "J",   "TZERO",      "tmp", 	    "tmp", 1.0, 0.0);
+  gfits_define_bintable_column (&theader, "I",   "TRATE",      "tmp", 	    "tmp", 1.0, 0.0);
+  gfits_define_bintable_column (&theader, "I",   "PHOTCODE",   "tmp", 	    "tmp", 1.0, 0.0);
+
+  // generate the output array that carries the data
+  gfits_create_table (&theader, &ftable);
+
+  unsigned int *imageID, *externID, *nstar;
+  short *NX, *NY;
+  e_time *tzero;
+  short *trate;
+  short *photcode;
+
+  // create intermediate storage arrays
+  ALLOCATE (imageID,    unsigned int, Nimage);
+  ALLOCATE (externID,   unsigned int, Nimage);
+  ALLOCATE (NX,         short,        Nimage);
+  ALLOCATE (NY,         short,        Nimage);
+  ALLOCATE (nstar,      unsigned int, Nimage);
+  ALLOCATE (tzero,      e_time,       Nimage);
+  ALLOCATE (trate,      short,        Nimage);
+  ALLOCATE (photcode,   short,        Nimage);
+
+  // assign the storage arrays
+  for (i = 0; i < Nimage; i++) {
+    imageID[i] 	= image[i].imageID ;
+    externID[i] = image[i].externID;
+    NX[i]      	= image[i].NX      ;	    
+    NY[i]      	= image[i].NY      ;	    
+    nstar[i]  	= image[i].nstar   ;	    
+    tzero[i]   	= image[i].tzero   ;  
+    trate[i]   	= image[i].trate   ;  
+    photcode[i] = image[i].photcode;
+  }
+
+  // add the columns to the output array
+  gfits_set_bintable_column (&theader, &ftable, "IMAGE_ID",   imageID,  Nimage);
+  gfits_set_bintable_column (&theader, &ftable, "EXTERN_ID",  externID, Nimage);
+  gfits_set_bintable_column (&theader, &ftable, "NX",         NX,       Nimage);
+  gfits_set_bintable_column (&theader, &ftable, "NY",         NY,       Nimage);
+  gfits_set_bintable_column (&theader, &ftable, "NSTAR",      nstar,    Nimage);
+  gfits_set_bintable_column (&theader, &ftable, "TZERO",      tzero,    Nimage);
+  gfits_set_bintable_column (&theader, &ftable, "TRATE",      trate,    Nimage);
+  gfits_set_bintable_column (&theader, &ftable, "PHOTCODE",   photcode, Nimage);
+
+  free (imageID );
+  free (externID);
+  free (NX      );
+  free (NY      );
+  free (nstar   );
+  free (tzero   );
+  free (trate   );
+  free (photcode);
+
+  FILE *f = fopen (filename, "w");
+  if (!f) {
+    fprintf (stderr, "ERROR: cannot open image subset file for output %s\n", filename);
+    return FALSE;
+  }
+
+  gfits_fwrite_header  (f, &header);
+  gfits_fwrite_matrix  (f, &matrix);
+  gfits_fwrite_Theader (f, &theader);
+  gfits_fwrite_table  (f, &ftable);
+
+  gfits_free_header (&header);
+  gfits_free_matrix (&matrix);
+  gfits_free_header (&theader);
+  gfits_free_table (&ftable);
+
+  fclose (f);
+  fflush (f);
+
+  return TRUE;
+}
+
+ImageSubset *ImagesToSubset (Image *image, off_t N) {
+
+  off_t i;
+
+  // we have been given an ImageSubset array, containing a reduced set of image fields
+  // create full a Image array and save the needed values
+  ImageSubset *subset = NULL;
+  ALLOCATE (subset, ImageSubset, N);
+
+  for (i = 0; i < N; i++) {
+    subset[i].imageID  = image[i].imageID  ;
+    subset[i].externID = image[i].externID ;
+    subset[i].NX       = image[i].NX       ;
+    subset[i].NY       = image[i].NY       ;
+    subset[i].nstar    = image[i].nstar    ;
+    subset[i].tzero    = image[i].tzero    ;
+    subset[i].trate    = image[i].trate    ;
+    subset[i].tmin     = image[i].tzero - 1;
+    subset[i].tmax     = image[i].trate*image[i].NY + 1;
+    subset[i].photcode = image[i].photcode ;
+  }
+  return subset;
+}
+
+
Index: /branches/eam_branches/ipp-20130509/Ohana/src/delstar/src/args.c
===================================================================
--- /branches/eam_branches/ipp-20130509/Ohana/src/delstar/src/args.c	(revision 35710)
+++ /branches/eam_branches/ipp-20130509/Ohana/src/delstar/src/args.c	(revision 35711)
@@ -175,4 +175,9 @@
     remove_argument (N, &argc, argv);
     SKIP_IMAGES = TRUE; // we do not need to load the images for -dup-measures
+  }
+  if ((N = get_argument (argc, argv, "-fix-LAP"))) {
+    if (MODE != MODE_NONE) usage();
+    MODE = MODE_FIX_LAP;
+    remove_argument (N, &argc, argv);
   }
   if ((N = get_argument (argc, argv, "-photcodes"))) {
@@ -344,4 +349,11 @@
   }
 
+  IMAGES = NULL;
+  if ((N = get_argument (argc, argv, "-images"))) {
+    remove_argument (N, &argc, argv);
+    IMAGES = strcreate (argv[N]);
+    remove_argument (N, &argc, argv);
+  }
+
   MODE = MODE_NONE;
   if ((N = get_argument (argc, argv, "-photcodes"))) {
@@ -362,4 +374,10 @@
     remove_argument (N, &argc, argv);
   }
+  if ((N = get_argument (argc, argv, "-fix-LAP"))) {
+    if (MODE != MODE_NONE) usage();
+    MODE = MODE_FIX_LAP;
+    remove_argument (N, &argc, argv);
+    if (!IMAGES) delstar_client_usage();
+  }
 
   if (MODE == MODE_NONE) delstar_client_usage ();
Index: /branches/eam_branches/ipp-20130509/Ohana/src/delstar/src/delete_duplicate_measures.c
===================================================================
--- /branches/eam_branches/ipp-20130509/Ohana/src/delstar/src/delete_duplicate_measures.c	(revision 35710)
+++ /branches/eam_branches/ipp-20130509/Ohana/src/delstar/src/delete_duplicate_measures.c	(revision 35711)
@@ -153,6 +153,4 @@
 }
 
-void sort_fullIDs (uint64_t *I, int *S, off_t N);
-
 int delete_duplicate_measures_catalog (Catalog *catalog) {
 
Index: /branches/eam_branches/ipp-20130509/Ohana/src/delstar/src/delete_fix_LAP.c
===================================================================
--- /branches/eam_branches/ipp-20130509/Ohana/src/delstar/src/delete_fix_LAP.c	(revision 35711)
+++ /branches/eam_branches/ipp-20130509/Ohana/src/delstar/src/delete_fix_LAP.c	(revision 35711)
@@ -0,0 +1,558 @@
+# include "delstar.h"
+
+// this function identifies detections to be deleted as being duplicates based on imageID + detID
+
+int delete_fix_LAP (ImageSubset *image, off_t Nimage) {
+
+  int i;
+  Catalog catalog;
+
+  // load the current sky table (layout of all SkyRegions) 
+  SkyTable *sky = SkyTableLoadOptimal (CATDIR, NULL, NULL, TRUE, -1, VERBOSE);
+  if (!sky) {
+    fprintf (stderr, "ERROR running loading sky table from %s\n", CATDIR);
+    exit (2);
+  }
+  SkyTableSetFilenames (sky, CATDIR, "cpt");
+
+  // determine the populated SkyRegions overlapping the requested area (default depth)
+  SkyList *skylist = SINGLE_CPT ? SkyRegionByCPT (sky, SINGLE_CPT) : SkyListByPatch (sky, -1, &UserPatch);
+  if (!skylist) {
+    fprintf (stderr, "ERROR setting up skylist for %s\n", CATDIR);
+    exit (2);
+  }
+
+  // launch the remote jobs
+  if (PARALLEL && !HOST_ID) {
+    int status = delete_fix_LAP_parallel (skylist, image, Nimage);
+    return status;
+  }
+
+  initImageIndex (image, Nimage);
+
+  // delete detections from region
+  for (i = 0; i < skylist[0].Nregions; i++) {
+
+    // does this host ID match the desired location for the table?
+    if (!HostTableTestHost(skylist[0].regions[i], HOST_ID)) continue;
+
+    // set up the basic catalog info
+    char hostfile[1024];
+    snprintf (hostfile, 1024, "%s/%s.cpt", HOSTDIR, skylist[0].regions[i]->name);
+    catalog.filename  = HOST_ID ? hostfile : skylist[0].filename[i];
+    catalog.Nsecfilt  = GetPhotcodeNsecfilt ();
+    catalog.catflags = LOAD_AVES | LOAD_MEAS | LOAD_MISS | LOAD_SECF;
+
+    if (VERBOSE) fprintf (stderr, "deleting from %s\n", catalog.filename);
+
+    // an error exit status here is a significant error
+    if (!dvo_catalog_open (&catalog, skylist[0].regions[i], VERBOSE2, "a")) {
+      fprintf (stderr, "ERROR: failure to open catalog file %s\n", catalog.filename);
+      exit (2);
+    }
+    if (!catalog.Naves_disk) {
+      if (VERBOSE2) fprintf (stderr, "no data in %s, skipping\n", catalog.filename);
+      dvo_catalog_unlock (&catalog);
+      dvo_catalog_free (&catalog);
+      continue;
+    }
+
+    off_t NaverageStart = catalog.Naverage;
+    off_t NmeasureStart = catalog.Nmeasure;
+    delete_fix_LAP_catalog (&catalog, image, Nimage);
+
+    // skip if nothing was deleted
+    if (UPDATE && (NaverageStart == catalog.Naverage) && (NmeasureStart == catalog.Nmeasure)) {
+      dvo_catalog_save_complete (&catalog, VERBOSE2);
+    }
+
+    dvo_catalog_unlock (&catalog);
+    dvo_catalog_free (&catalog);
+  }
+  return TRUE;
+}
+
+// CATDIR is supplied globally
+int delete_fix_LAP_parallel (SkyList *sky, ImageSubset *image, off_t Nimage) {
+
+  char uniquer[12];
+  int TIME = time(NULL);
+  int PID = getpid();
+  snprintf (uniquer, 12, "%05d.%05d", PID, TIME % 100000);
+
+  // write out the subset table of image information
+  char imageFile[512];
+  snprintf (imageFile, 512, "%s/delstar.fixLAP.%s.dat", CATDIR, uniquer);
+
+  if (!ImageSubsetSave (imageFile, image, Nimage)) {
+    fprintf (stderr, "failed to write image subset\n");
+    exit (1);
+  }
+
+  // launch the delstar_client jobs to the parallel hosts
+
+  // load the list of hosts
+  HostTable *table = HostTableLoad (CATDIR, sky->hosts);
+  if (!table) {
+    fprintf (stderr, "ERROR: failure reading Host Table %s for database %s\n", sky->hosts, CATDIR);
+    exit (1);
+  }    
+
+  int i, j;
+  for (i = 0; i < table->Nhosts; i++) {
+
+    if (sky->Nregions < table->Nhosts) {
+      // do any of the regions want this host?
+      int wantThisHost = FALSE;
+      for (j = 0; j < sky->Nregions; j++) {
+	if (HostTableTestHost (sky->regions[j], table->hosts[i].hostID)) {
+	  wantThisHost = TRUE;
+	  break;
+	}
+      }
+      if (!wantThisHost) {
+	// fprintf (stderr, "skip host %s\n", table->hosts[i].hostname);
+	continue;
+      }
+    }
+
+    // ensure that the paths are absolute path names
+    char *tmppath = abspath (table->hosts[i].pathname, DVO_MAX_PATH);
+    free (table->hosts[i].pathname);
+    table->hosts[i].pathname = tmppath;
+
+    char command[1024];
+    snprintf (command, 1024, "delstar_client -hostID %d -D CATDIR %s -hostdir %s -images %s -region %f %f %f %f -fix-LAP", 
+	      table->hosts[i].hostID, CATDIR, table->hosts[i].pathname, imageFile,
+	      UserPatch.Rmin, UserPatch.Rmax, UserPatch.Dmin, UserPatch.Dmax);
+
+    char tmpline[1024];
+    if (VERBOSE)    { snprintf (tmpline, 1024, "%s -v",      command);             strcpy (command, tmpline); }
+    if (VERBOSE2)   { snprintf (tmpline, 1024, "%s -vv",     command);             strcpy (command, tmpline); }
+    if (SINGLE_CPT) { snprintf (tmpline, 1024, "%s -cpt %s", command, SINGLE_CPT); strcpy (command, tmpline); }
+    if (UPDATE)     { snprintf (tmpline, 1024, "%s -update", command);             strcpy (command, tmpline); }
+
+    fprintf (stderr, "command: %s\n", command);
+
+    if (PARALLEL_MANUAL) continue;
+
+    if (PARALLEL_SERIAL) {
+      int status = system (command);
+      if (status) {
+	fprintf (stderr, "ERROR running delstar_client\n");
+	exit (2);
+      }
+    } else {
+      // launch the job on the remote machine (no handshake)
+      int errorInfo = 0;
+      int pid = rconnect ("ssh", table->hosts[i].hostname, command, table->hosts[i].stdio, &errorInfo, FALSE);
+      if (!pid) {
+	fprintf (stderr, "failure to start %s (error %d)\n", table->hosts[i].hostname, errorInfo);
+	exit (1);
+      }
+      table->hosts[i].pid = pid; // save for future reference
+    }
+  }
+
+  if (PARALLEL_MANUAL) {
+    fprintf (stderr, "run the client commands above.  when these are done, hit return\n");
+    getchar();
+  }
+  if (!PARALLEL_MANUAL && !PARALLEL_SERIAL) {
+    HostTableWaitJobsGetIO (table, __FILE__, __LINE__, VERBOSE);
+  }
+
+  return TRUE;
+}
+
+int UnpackPSPSStackDetectionID(int *sourceID, int *imageID, int *detID, uint64_t pspsStackID);
+
+int delete_fix_LAP_catalog (Catalog *catalog, ImageSubset *image, off_t Nimage) {
+
+  off_t i, j, n, m, N, D, currentAve;
+
+  Measure *measureOut = NULL;
+  Average *averageOut = NULL;
+  SecFilt *secfiltOut = NULL;
+
+  off_t *measureDrop, *measureSeqRaw, *measureSeqOut, *measureRefOut, *measureAveRaw, *averageNmeas, *averageDmeas, *averageSeqOut, *averageRefOut, *measureAveOut;
+
+  int Nsecfilt = GetPhotcodeNsecfilt ();
+
+  /* internal counters */
+  off_t Nmeasure = catalog[0].Nmeasure;
+  off_t Naverage = catalog[0].Naverage;
+
+  Measure *measure = catalog[0].measure;
+  Average *average = catalog[0].average;
+  SecFilt *secfilt = catalog[0].secfilt;
+  
+  if (VERBOSE) fprintf (stderr, "starting with Nave, Nmeas: "OFF_T_FMT" "OFF_T_FMT"\n",  catalog[0].Naverage,  catalog[0].Nmeasure);
+
+  // we have a table of average objects and an unsorted table of measurements.  each measurement
+  // has a reference to the average object sequence (as well as an ID)
+  // measure[i].averef -> average[averef]
+  // measure[i].objID = average[averef].objID
+  // measure[i].catID = average[averef].catID
+
+  // we want a sorted measure array with all averef entries in sequence, skipping the entries which match our criteria
+
+  // arrays 
+  ALLOCATE (measureDrop,   off_t, Nmeasure);
+  delete_fix_LAP_measures (measureDrop, catalog, image, Nimage);
+
+  ALLOCATE (measureSeqRaw, off_t, Nmeasure);
+  ALLOCATE (measureSeqOut, off_t, Nmeasure);
+  ALLOCATE (measureRefOut, off_t, Nmeasure);
+  ALLOCATE (measureAveRaw, off_t, Nmeasure);
+  ALLOCATE (averageNmeas,  off_t, Naverage);
+  ALLOCATE (averageDmeas,  off_t, Naverage);
+  ALLOCATE (averageSeqOut, off_t, Naverage);
+  ALLOCATE (averageRefOut, off_t, Naverage);
+
+  // set up the measure sequence lists
+  for (i = 0; i < Nmeasure; i++) {
+    measureSeqRaw[i] = i;
+    measureAveRaw[i] = measure[i].averef;
+  }
+  // sort measureSeqRaw and measureAveRaw in order of measureAveRaw
+  SortAveMeasMatch(measureSeqRaw, measureAveRaw, Nmeasure);
+
+  // generate the measureSeqOut array
+  off_t NmeasOut = 0;
+  for (i = 0; i < Nmeasure; i++) {
+    j = measureSeqRaw[i];
+    if (measureDrop[j]) {
+	continue;
+    }
+    measureSeqOut[NmeasOut] = j;
+    measureRefOut[j] = NmeasOut;
+    NmeasOut ++;
+  }
+  // n = measureSeqOut[i] : measureOut[i] = measure[n]  (i = 0 -- NmeasOut, n = 0 -- Nmeasure)
+  // n = measureRefOut[i] : measureOut[n] = measure[i]
+  ALLOCATE (measureAveOut, off_t, NmeasOut);
+
+  // count the number of measures for each averef
+  N =  0;
+  D = -1;
+  currentAve = measureAveRaw[0];
+  for (i = 0; i < Nmeasure; i++) {
+    if (measureAveRaw[i] != currentAve) {
+      // we have hit the next entry in the list
+      averageNmeas[currentAve] = N; // number of measures
+      averageDmeas[currentAve] = D; // first measure 
+      N =  0;
+      D = -1;
+      currentAve = measureAveRaw[i];
+    }
+    j = measureSeqRaw[i];
+    if (measureDrop[j]) continue;
+    N++;
+    if (D == -1) { 
+      // first valid measureOut for this averef
+      D = measureRefOut[j];
+    }
+  }
+  averageNmeas[currentAve] = N; // number of measures
+  averageDmeas[currentAve] = D; // first measure 
+
+  // generate the new average sequence, skipping entries with no measurements
+  off_t NaveOut = 0;
+  for (i = 0; i < Naverage; i++) {
+    if (averageNmeas[i] == 0) continue;
+    averageSeqOut[NaveOut] = i;
+    averageRefOut[i] = NaveOut;
+    NaveOut ++;
+  }
+  // n = averageSeqOut[i] : averageOut[i] = average[n]  (i = 0 -- NaveOut, n = 0 -- Naverage)
+  // n = averageRefOut[i] : averageOut[n] = average[i]
+
+  // generate the output averefs
+  for (i = 0; i < NmeasOut; i++) {
+    j = measureSeqOut[i]; // measure[j] = measureOut[i]
+    n = measureAveRaw[j]; // average[n] : measure[j]
+    N = averageRefOut[n]; // averageOut[N] = average[n];
+    measureAveOut[i] = N; // measureOut[i].averef = measureAveOut[i] 
+  }
+
+  // copy the (kept) measurements in the sorted order
+  ALLOCATE (measureOut, Measure, NmeasOut);
+  for (i = 0; i < NmeasOut; i++) {
+    j = measureSeqOut[i];
+    measureOut[i] = measure[j];
+    measureOut[i].averef = measureAveOut[i];
+  }
+
+  // copy the (kept) average entries
+  ALLOCATE (averageOut, Average, NaveOut);
+  for (i = 0; i < NaveOut; i++) {
+    j = averageSeqOut[i];
+    averageOut[i] = average[j];
+    averageOut[i].Nmeasure      = averageNmeas[j]; 
+    averageOut[i].measureOffset = averageDmeas[j];
+  }
+
+  // copy the secfilt entries for the (kept) average entries
+  ALLOCATE (secfiltOut, SecFilt, NaveOut*Nsecfilt);
+  for (i = 0; i < NaveOut; i++) {
+    j = averageSeqOut[i];
+    for (m = 0; m < Nsecfilt; m++) {
+      secfiltOut[i*Nsecfilt + m] = secfilt[j*Nsecfilt + m];
+    }
+  }
+
+  // update the values of average.measureOffset and average.Nmeasure
+  FREE(measure);
+  FREE(average);
+  FREE(secfilt);
+  catalog[0].measure = measureOut;
+  catalog[0].average = averageOut;
+  catalog[0].secfilt = secfiltOut;
+  catalog[0].Nmeasure = NmeasOut;
+  catalog[0].Naverage = NaveOut;
+
+  // update catalog.Nmeasure and catalog.Naverage, double check 
+  int NmeasureTotal = 0;
+  int measureOffsetOK = TRUE;
+  int averefOK = TRUE;
+  for (i = 0; i < NaveOut; i++) {
+    NmeasureTotal += catalog[0].average[i].Nmeasure;
+    if (VERBOSE2 && !(NmeasureTotal <= catalog[0].Nmeasure)) {
+      fprintf (stderr, "too few measurements: %d %d %d\n", (int) i, NmeasureTotal, (int) catalog[0].Nmeasure);
+    }
+    measureOffsetOK &= (catalog[0].average[i].measureOffset < catalog[0].Nmeasure);
+    if (VERBOSE2 && !(catalog[0].average[i].measureOffset < catalog[0].Nmeasure)) {
+      fprintf (stderr, "offset too large: %d %d %d\n", (int) i, catalog[0].average[i].Nmeasure, (int) catalog[0].Nmeasure);
+    }
+    measureOffsetOK &= (catalog[0].average[i].measureOffset + catalog[0].average[i].Nmeasure <= catalog[0].Nmeasure);
+    if (VERBOSE2 && !(catalog[0].average[i].measureOffset + catalog[0].average[i].Nmeasure <= catalog[0].Nmeasure)) {
+      fprintf (stderr, "orrset + Nmeasure too large: %d + %d > %d %d\n", (int) i, catalog[0].average[i].measureOffset, catalog[0].average[i].Nmeasure, (int) catalog[0].Nmeasure);
+    }
+    m = catalog[0].average[i].measureOffset;
+    for (j = 0; j < catalog[0].average[i].Nmeasure; j++) {
+      averefOK &= (catalog[0].measure[m+j].averef == i);
+      if (VERBOSE2 && !(catalog[0].measure[m+j].averef == i)) {
+	fprintf (stderr, "averef broken: %d vs %d (measure %d)\n", (int) i, catalog[0].measure[m+j].averef, (int) (m+j));
+      }
+    }
+  }
+
+  if (!measureOffsetOK) {
+    fprintf (stderr, "ERROR: catalog %s has an invalid measureOffset\n", catalog[0].filename);
+  }
+
+  if (!averefOK) {
+    fprintf (stderr, "ERROR: catalog %s has invalid averefs\n", catalog[0].filename);
+  }
+
+  if (NmeasureTotal != catalog[0].Nmeasure) {
+    fprintf (stderr, "ERROR: catalog %s has an invalid Nmeasure\n", catalog[0].filename);
+  }
+
+  // MARKTIME("  match time %9.4f sec for %7lld measures, %6lld average\n", dtime, (long long) Nmeasure, (long long) Naverage);
+
+  catalog[0].sorted = TRUE;
+
+  if (VERBOSE) fprintf (stderr, "ending with Nave, Nmeas: "OFF_T_FMT" "OFF_T_FMT"\n",  catalog[0].Naverage,  catalog[0].Nmeasure);
+
+  off_t NdelAves = Naverage - catalog[0].Naverage;
+  off_t NdelMeas = Nmeasure - catalog[0].Nmeasure;
+  if (NdelAves || NdelMeas) {
+    fprintf (stderr, "deleting "OFF_T_FMT" measures and "OFF_T_FMT" averages : %s\n",  NdelMeas, NdelAves, catalog[0].filename);
+  }
+
+  FREE (measureDrop);
+  FREE (measureSeqRaw);
+  FREE (measureSeqOut);
+  FREE (measureRefOut);
+  FREE (measureAveRaw);
+  FREE (averageNmeas);
+  FREE (averageDmeas);
+  FREE (averageSeqOut);
+  FREE (averageRefOut);
+  FREE (measureAveOut);
+
+  return TRUE;
+}
+
+// this function makes the needed modifications to the measure table (to imageIDs) and updates
+// the array measureDrop to mark the entries we want to remove
+
+// needed modifications:
+// * delete all photcodes corresponding to 2mass, synth, or wise (not gpc1 or stack)
+// * delete all duplicates
+// * check the imageID 
+// * re-assign the imageID if needed
+
+int delete_fix_LAP_measures (off_t *measureDrop, Catalog *catalog, ImageSubset *image, off_t Nimage) {
+
+  /* internal counters */
+  off_t Nmeasure = catalog[0].Nmeasure;
+
+  Measure *measure = catalog[0].measure;
+  Average *average = catalog[0].average;
+  
+  uint64_t *fullID;
+  int *seq, *duplicates;
+
+  ALLOCATE (fullID,     uint64_t, Nmeasure);
+  ALLOCATE (seq,             int, Nmeasure);
+  ALLOCATE (duplicates,      int, Nmeasure);
+
+  memset (measureDrop, 0, sizeof(off_t) * Nmeasure);
+  memset (duplicates,  0, sizeof(int) * Nmeasure);
+
+  // make an array of the full IDs
+  off_t i;
+  for (i = 0; i < Nmeasure; i++) {
+    fullID[i] = ((uint64_t) (measure[i].imageID) << 32) | (measure[i].detID);
+    seq[i] = i;
+  }
+
+  // sort the array and sequence number
+  sort_fullIDs (fullID, seq, catalog[0].Nmeasure);
+
+  uint64_t current = fullID[0];	      // current unique value
+  int count = 0;		      // number of duplicates for the current unique value
+  duplicates[0] = count;	      // duplicate sequence
+
+  for (i = 1; i < catalog[0].Nmeasure; i++) {
+    off_t j = seq[i];
+    int newID = (measure[j].imageID == 0) || (fullID[i] != current);
+    if (newID) {
+      count = 0;
+      current = fullID[i];
+    } else {
+      count ++;
+    }
+    duplicates[i] = count;
+  }
+    
+  // mark the measures to be dropped
+  for (i = 0; i < Nmeasure; i++) {
+    off_t j = seq[i];
+
+    int keep = TRUE;
+    if (duplicates[i]) keep = FALSE;
+    if (measure[j].photcode < 10000) keep = FALSE;  // Drop 2MASS, WISE, SYNTH here
+
+    if (!keep) {
+      measureDrop[j] = TRUE;
+      off_t N = measure[j].averef;
+      fprintf (stderr, "0x%08x 0x%08x %8.4f %8.4f %5d\n", measure[j].imageID, measure[j].detID, average[N].R, average[N].D, measure[j].photcode);
+    }
+  }
+
+  int *imageIndex = getImageIndex();
+
+  // check the image IDs for the remaining measures
+  for (i = 0; i < Nmeasure; i++) {
+    off_t j = seq[i];
+    if (measureDrop[j]) continue;
+
+    // for gpc1 exposures, 10000 < photcode < 10600
+    if ((10000 < measure[j].photcode) && (measure[j].photcode < 10600)) {
+      int resetID = FALSE;
+      off_t imageID = measure[j].imageID;
+      if (imageID == 0) {
+	resetID = TRUE;
+	goto resetID_exp;
+      }
+      off_t imageN = imageIndex[imageID];
+      if (imageN < 0) {
+	// image ID for this measure is not in the image table
+	resetID = TRUE;
+      }
+      if ((measure[j].t < image[imageN].tmin) || (measure[j].t > image[imageN].tmax)) {
+	// measure.imageID does not match correct image
+	resetID = TRUE;
+      }
+      if (measure[j].photcode != image[imageN].photcode) {
+	// measure.imageID does not match correct image
+	resetID = TRUE;
+      }
+
+    resetID_exp:
+      if (resetID) {
+	// update the image ID
+	short photcode = measure[j].photcode;
+	e_time time = measure[j].t;
+	
+	off_t imageID, Seq;
+	if (!FindIDexp (&imageID, &Seq, time, photcode)) {
+	  fprintf (stderr, "error cannot find matching image ID\n");
+	  exit (5);
+	}
+	if (imageID <= 0) {
+	  fprintf (stderr, "error cannot find matching image ID\n");
+	  exit (5);
+	}
+	measure[j].imageID = imageID;
+      }
+    }
+
+    // for gpc1 exposures, 10000 < photcode < 10600
+    if ((11000 <= measure[j].photcode) && (measure[j].photcode < 11600)) {
+      int resetID = FALSE;
+      off_t imageID = measure[j].imageID;
+      if (imageID == 0) {
+	resetID = TRUE;
+	goto resetID_stk;
+      }
+
+      off_t imageN = imageIndex[imageID];
+      if (imageN < 0) {
+	// image ID for this measure is not in the image table
+	resetID = TRUE;
+      }
+      int sourceID, externID, detID;
+      UnpackPSPSStackDetectionID (&sourceID, &externID, &detID, measure[j].extID);
+      if (externID != image[imageN].externID) {
+	// measure.imageID does not match correct image
+	resetID = TRUE;
+      }
+      if (measure[j].photcode != image[imageN].photcode) {
+	// measure.imageID does not match correct image
+	resetID = TRUE;
+      }
+
+    resetID_stk:
+      if (resetID) {
+	// update the image ID
+	off_t imageID, Seq;
+	short Photcode;
+	if (!FindIDstk (&imageID, &Seq, &Photcode, externID)) {
+	  fprintf (stderr, "error : cannot find matching stack image ID\n");
+	  exit (6);
+	}
+	measure[j].imageID = imageID;
+      }
+    }
+  }
+
+  FREE (fullID);
+  FREE (seq);
+  FREE (duplicates);
+
+  return TRUE;
+}
+
+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;
+}
Index: /branches/eam_branches/ipp-20130509/Ohana/src/delstar/src/delstar.c
===================================================================
--- /branches/eam_branches/ipp-20130509/Ohana/src/delstar/src/delstar.c	(revision 35710)
+++ /branches/eam_branches/ipp-20130509/Ohana/src/delstar/src/delstar.c	(revision 35711)
@@ -28,4 +28,20 @@
       exit (0);
       break;
+    case MODE_FIX_LAP: {
+	off_t Nimage;
+	Image *image = gfits_table_get_Image (&db.ftable, &Nimage, &db.swapped);
+	if (!image) {
+	  fprintf (stderr, "ERROR: failed to read images\n");
+	  exit (2);
+	}
+	dvo_image_unlock (&db); 
+
+	// the imageSubset here is a reduced set of fields, not a reduced set of images
+	ImageSubset *subset = ImagesToSubset (image, Nimage);
+
+	if (!delete_fix_LAP (subset, Nimage)) exit (1);
+	exit (0);
+	break;
+      }
     case MODE_IMAGEFILE:
       delete_imagefile (&db);
Index: /branches/eam_branches/ipp-20130509/Ohana/src/delstar/src/delstar_client.c
===================================================================
--- /branches/eam_branches/ipp-20130509/Ohana/src/delstar/src/delstar_client.c	(revision 35710)
+++ /branches/eam_branches/ipp-20130509/Ohana/src/delstar/src/delstar_client.c	(revision 35711)
@@ -29,4 +29,18 @@
       exit (0);
       break;
+
+    case MODE_FIX_LAP: {
+      off_t Nimage;
+      ImageSubset *image = ImageSubsetLoad (IMAGES, &Nimage);
+      if (!image) {
+	fprintf (stderr, "failed to read image table\n");
+	exit (1);
+      }
+
+      if (!delete_fix_LAP (image, Nimage)) exit (1);
+      exit (0);
+      break;
+    }
+
     case MODE_IMAGEFILE:
       break;
