Index: /branches/eam_branches/ipp-20101103/Ohana/src/dvomerge/src/dvorepairFixCPT.c
===================================================================
--- /branches/eam_branches/ipp-20101103/Ohana/src/dvomerge/src/dvorepairFixCPT.c	(revision 29786)
+++ /branches/eam_branches/ipp-20101103/Ohana/src/dvomerge/src/dvorepairFixCPT.c	(revision 29787)
@@ -8,29 +8,14 @@
 //   * obj_id, cat_id are defined in detection
 
+int FixCPTfile (char *rootname, Image *image, off_t Nimage);
+
 int dvorepairFixCPT (int argc, char **argv) {
 
+  int i, j, N, Nfiles, NFILES;
+  off_t Nimage;
   FITS_DB db;  // database handle pointing to input image table
-
-  off_t Nmeasure, Nimage;
-  int i, N, Nbytes, NaveMax, Naverage, NAVERAGE, Nave, Nold;
-  int *found;
-
-  Image *image, *thisImage;
-  Average *average;
-  Measure *measure;
-
-  Matrix matrix;
-
-  char *cpmFilename, *cptFilenameSrc, *cptFilenameTgt, *imageFilename;
-
-  Header cptHeaderPHU, cptHeaderTBL;
-  Header cpmHeaderPHU, cpmHeaderTBL;
-  FTable cpmFtable, cptFtable;
-
-  FILE *cptFileSrc = NULL;
-  FILE *cptFileTgt = NULL;
-  FILE *cpmFile = NULL;
-
-  char catformat;
+  Image *image;
+  char *listFilename, *imageFilename;
+  char **files;
 
   N = get_argument (argc, argv, "-fix-cpt");
@@ -38,16 +23,11 @@
   remove_argument (N, &argc, argv);
 
-  if (argc != 5) {
-    fprintf (stderr, "USAGE: dvorepair -fix-cpt (images) (cpm) (cptInput) (cptOutput)\n");
+  if (argc != 3) {
+    fprintf (stderr, "USAGE: dvorepair -fix-cpt (images) (filelist)\n");
     exit (2);
   }
 
   imageFilename  = argv[1];
-  cpmFilename    = argv[2];
-  cptFilenameSrc = argv[3];
-  cptFilenameTgt = argv[4];
-
-  cpmFtable.header = &cpmHeaderTBL;
-  cptFtable.header = &cptHeaderTBL;
+  listFilename   = argv[2];
 
   // XXX don't bother locking for now: this is totally manual..
@@ -57,6 +37,65 @@
   BuildChipMatch (image, Nimage);
 
+  FILE *file = fopen (listFilename, "r");
+  myAssert(file, "failed to open list");
+
+  Nfiles = 0;
+  NFILES = 100;
+  ALLOCATE(files, char *, NFILES);
+  for (i = 0; i < NFILES; i++) {
+    ALLOCATE (files[i], char, 256);
+    memset (files[i], 0, 256);
+  }
+
+  for (i = 0; TRUE; i++) {
+    if (fscanf (file, "%s", files[i]) == EOF) {
+      break;
+    }
+    if (i == NFILES - 1) {
+      NFILES += 100;
+      REALLOCATE(files, char *, NFILES);
+      for (j = i + 1; j < NFILES; j++) {
+	ALLOCATE (files[j], char, 256);
+	memset (files[j], 0, 256);
+      }
+    }
+  }
+  Nfiles = i;
+  
+  for (i = 0; i < Nfiles; i++) {
+    FixCPTfile(files[i], image, Nimage);
+  }
+
+  exit (0);
+}
+
+// fix the CPT file.  in the process, we rewrite the CPM file so that the averef entries are correctly set.
+int FixCPTfile (char *rootFilename, Image *image, off_t Nimage) {
+
+  char cptFilenameSrc[256], cptFilenameTgt[256], cpsFilenameSrc[256], cpsFilenameTgt[256], cpmFilenameSrc[256], cpmFilenameTgt[256];
+
+  Header cpmHeaderPHU, cpmHeaderTBL;
+  FTable cpmFtable;
+  FILE  *cpmFile = NULL;
+
+  off_t Nbytes, Nmeasure;
+
+  Measure *measure;
+
+  Matrix matrix;
+
+  char catformat;
+
+  cpmFtable.header = &cpmHeaderTBL;
+
+  sprintf (cpmFilenameSrc, "%s.cpm", rootFilename);
+  sprintf (cpmFilenameTgt, "%s.cpm.fixed", rootFilename);
+  sprintf (cptFilenameSrc, "%s.cpt", rootFilename);
+  sprintf (cptFilenameTgt, "%s.cpt.fixed", rootFilename);
+  sprintf (cpsFilenameSrc, "%s.cps", rootFilename);
+  sprintf (cpsFilenameTgt, "%s.cps.fixed", rootFilename);
+
   // open cpm file
-  cpmFile = fopen(cpmFilename, "r");
+  cpmFile = fopen(cpmFilenameSrc, "r");
   myAssert(cpmFile, "failed to open cpm file");
 
@@ -75,4 +114,5 @@
   }
   // read Measure table data : format is irrelevant here */
+  // XXX this should not be running on broken CPM files
   if (!gfits_fread_ftable_data (cpmFile, &cpmFtable, FALSE)) { 
     myAbort("can't read data for cpm table");
@@ -82,109 +122,38 @@
   myAssert(measure, "failed to convert ftable to measure data");
 
-  NaveMax = 0;
-  NAVERAGE = 1000;
-  ALLOCATE (average, Average, NAVERAGE);
-  memset (average, 0, NAVERAGE*sizeof(Average));
+  // the CPT and CPS tables need to be regenerated.  This must happen first because, in the process, we also update measure->averef
+  RepairTableCPT(cptFilenameSrc, cptFilenameTgt, cpsFilenameSrc, cpsFilenameTgt, measure, Nmeasure, image, Nimage, catformat);
 
-  ALLOCATE (found, int, NAVERAGE);
-  memset (found, 0, NAVERAGE*sizeof(int));
-
-  // examine all measurements and new objects as needed
-  for (i = 0; i < Nmeasure; i++) {
-    Nave = measure[i].averef;
-    if (found[Nave]) {
-      average[Nave].Nmeasure ++;
-      myAssert(average[Nave].objID == measure[i].objID, "objIDs do not match!");
-      myAssert(average[Nave].catID == measure[i].catID, "catIDs do not match!");
-      continue;
-    }
-
-    if (Nave >= NAVERAGE) {
-      Nold = NAVERAGE;
-      NAVERAGE = MAX(Nave + 1000, NAVERAGE + 1000);
-      REALLOCATE (average, Average, NAVERAGE);
-      memset (&average[Nold], 0, (NAVERAGE - Nold)*sizeof(Average));
-
-      REALLOCATE (found, int, NAVERAGE);
-      memset (&found[Nold], 0, (NAVERAGE - Nold)*sizeof(int));
-    }
-
-    NaveMax = MAX(Nave, NaveMax);
-
-    found[Nave] = TRUE;
-
-    // we are going to leave most of the elements of average unset: they are the result of 
-    // the relastro analysis for this object and can be recreated with a call to relastro
-
-    // fields we have to set:
-
-    // need to find image so we can use ccd coordinates to determine RA & DEC
-    thisImage = MatchImage (image, Nimage, measure[i].t, measure[i].photcode, measure[i].imageID);
-    XY_to_RD (&average[Nave].R, &average[Nave].D, measure[i].Xccd, measure[i].Yccd, &thisImage[0].coords);
-
-    average[Nave].Nmeasure = 1;
-    average[Nave].Nmissing = 0;
-
-    // assume the resulting table set is unsorted
-    average[Nave].measureOffset = -1;
-    average[Nave].missingOffset = -1;
-    average[Nave].extendOffset = -1;
-
-    average[Nave].objID = measure[i].objID;
-    average[Nave].catID = measure[i].catID;
-    average[Nave].extID = CreatePSPSObjectID(average[Nave].R, average[Nave].D);
-  }
-  Naverage = NaveMax + 1;
-
-  // have we created all objects in the range 0 - Naverage?
-  for (i = 0; i < Naverage; i++) {
-    myAssert(found[i], "failed to find one");
-  }
-
-  // open source cpt file
-  cptFileSrc = fopen(cptFilenameSrc, "r");
-  myAssert(cptFileSrc, "failed to open cpt file");
-
-  // load the cpt header (use for CATID, RA,DEC range, filenames)
-  if (!gfits_fread_header (cptFileSrc, &cptHeaderPHU)) {
-    myAbort("failure to cpt header");
-  }
-
-  // update the output header
-  gfits_modify (&cptHeaderPHU, "NSTARS",     "%d",      1,  Naverage);
-  gfits_modify (&cptHeaderPHU, "NMEAS",      OFF_T_FMT, 1,  Nmeasure);
-  gfits_modify (&cptHeaderPHU, "NMISS",      "%d",      1,  0);
-  gfits_modify_alt (&cptHeaderPHU, "SORTED", "%t",      1,  FALSE);
-
-  /* convert internal to external format */
-  if (!AverageToFtable (&cptFtable, average, Naverage, catformat, NULL)) {
+  // convert internal to external format 
+  if (!MeasureToFtable (&cpmFtable, measure, Nmeasure, catformat)) {
     myAbort("trouble converting format");
   }
 
   // create and write the output file
-  cptFileTgt = fopen(cptFilenameTgt, "w");
-  myAssert(cptFileTgt, "failed to open cpt file");
-    
+  cpmFile = fopen(cpmFilenameTgt, "w");
+  myAssert(cpmFile, "failed to open cpt file");
+	
   // write PHU header
-  if (!gfits_fwrite_header (cptFileTgt, &cptHeaderPHU)) {
+  if (!gfits_fwrite_header (cpmFile, &cpmHeaderPHU)) {
     myAbort("can't write primary header");
   }
 
   // write the PHU matrix; this is probably a NOP, do I have to keep it in?
-  gfits_create_matrix (&cptHeaderPHU, &matrix);
-  if (!gfits_fwrite_matrix  (cptFileTgt, &matrix)) {
+  gfits_create_matrix (&cpmHeaderPHU, &matrix);
+  if (!gfits_fwrite_matrix  (cpmFile, &matrix)) {
     myAbort("can't write primary matrix");
   }
   gfits_free_matrix (&matrix);
-
+	
   // write the table data
-  if (!gfits_fwrite_ftable_range (cptFileTgt, &cptFtable, 0, Naverage, 0, Naverage)) {
+  if (!gfits_fwrite_ftable_range (cpmFile, &cpmFtable, 0, Nmeasure, 0, Nmeasure)) {
     myAbort("can't write table data");
   }
+
+  gfits_free_header (&cpmHeaderPHU);
+  gfits_free_header (&cpmHeaderTBL);
+  free (measure);
+  fclose(cpmFile);
   
-  fclose(cptFileTgt);
-  fclose(cptFileSrc);
-  fclose(cpmFile);
-
-  exit (0);
+  return TRUE;
 }
Index: /branches/eam_branches/ipp-20101103/Ohana/src/dvomerge/src/dvoverify.c
===================================================================
--- /branches/eam_branches/ipp-20101103/Ohana/src/dvomerge/src/dvoverify.c	(revision 29786)
+++ /branches/eam_branches/ipp-20101103/Ohana/src/dvomerge/src/dvoverify.c	(revision 29787)
@@ -81,5 +81,5 @@
     if (!inlist[0].regions[i][0].table) continue;
 
-    // if (i % 1000 == 0) fprintf (stderr, ".");
+    if (i % 1000 == 0) fprintf (stderr, ".");
 
     sprintf (filename, "%s/%s.cpt", catdir, inlist[0].regions[i][0].name);
Index: /branches/eam_branches/ipp-20101103/Ohana/src/dvomerge/src/help.c
===================================================================
--- /branches/eam_branches/ipp-20101103/Ohana/src/dvomerge/src/help.c	(revision 29786)
+++ /branches/eam_branches/ipp-20101103/Ohana/src/dvomerge/src/help.c	(revision 29787)
@@ -98,5 +98,5 @@
 
   fprintf (stderr, "USAGE\n");
-  fprintf (stderr, "  dvorepair -fix-cpt (images) (cpm) (cptInput) (cptOutput) - regenerate a specific cpt file from the cpm file\n");
+  fprintf (stderr, "  dvorepair -fix-cpt (images) (rootlist) - regenerate cpt & cps files from the cpm files\n");
   fprintf (stderr, "  dvorepair -images-vs-measures (catdir) (Ntol) - find images with too many missing detections\n");
   fprintf (stderr, "  dvorepair -delete-image-list (catdir) (deleteList) - delete a set of images based on image IDs (output from -images-vs-measures)\n");
