Index: trunk/Ohana/src/addstar/Makefile
===================================================================
--- trunk/Ohana/src/addstar/Makefile	(revision 34404)
+++ trunk/Ohana/src/addstar/Makefile	(revision 34405)
@@ -47,4 +47,5 @@
 $(SRC)/SkyTableFromTychoIndex.$(ARCH).o \
 $(SRC)/addstar.$(ARCH).o \
+$(SRC)/addstar_create_ID.$(ARCH).o \
 $(SRC)/airmass.$(ARCH).o \
 $(SRC)/args.$(ARCH).o \
Index: trunk/Ohana/src/addstar/include/addstar.h
===================================================================
--- trunk/Ohana/src/addstar/include/addstar.h	(revision 34404)
+++ trunk/Ohana/src/addstar/include/addstar.h	(revision 34405)
@@ -59,5 +59,5 @@
 typedef struct sockaddr_in SockAddress;
 
-enum {ADDSTAR_MODE_NONE, ADDSTAR_MODE_IMAGE, ADDSTAR_MODE_REFLIST, ADDSTAR_MODE_REFCAT, ADDSTAR_MODE_FAKEIMAGE, ADDSTAR_MODE_RESORT};
+enum {ADDSTAR_MODE_NONE, ADDSTAR_MODE_IMAGE, ADDSTAR_MODE_REFLIST, ADDSTAR_MODE_REFCAT, ADDSTAR_MODE_FAKEIMAGE, ADDSTAR_MODE_RESORT, ADDSTAR_MODE_CREATE_ID};
 enum {NONE, SIMPLE_CMP, SIMPLE_CMF, SIMPLE_MEF, MOSAIC_CMP, MOSAIC_CMF, MOSAIC_MEF, MOSAIC_PHU, SDSS_OBJ};
 /* note: MEF implies CMF */
@@ -269,14 +269,4 @@
 int 	   NewReflist_Thread	  PROTO((int BindSocket));
 
-Stars     *Convert_SMPDATA	  PROTO((FTable *table, unsigned int *nstars));
-Stars     *Convert_PS1_DEV_0	  PROTO((FTable *table, unsigned int *nstars));
-Stars     *Convert_PS1_DEV_1	  PROTO((FTable *table, unsigned int *nstars));
-Stars     *Convert_PS1_V1	  PROTO((FTable *table, unsigned int *nstars));
-Stars     *Convert_PS1_V1_Alt	  PROTO((FTable *table, unsigned int *nstars));
-Stars     *Convert_PS1_V2	  PROTO((FTable *table, unsigned int *nstars));
-Stars     *Convert_PS1_V3	  PROTO((FTable *table, unsigned int *nstars));
-Stars     *Convert_PS1_SV1	  PROTO((FTable *table, unsigned int *nstars));
-Stars     *Convert_PS1_SV1_Alt	  PROTO((FTable *table, unsigned int *nstars));
-
 int        InitStar               PROTO((Stars *star));
 
@@ -303,4 +293,5 @@
 // this is a gnu extension?? caution!
 void *memrchr(const void *s, int c, size_t n);
+int addstar_create_ID ();
 
 /** 
Index: trunk/Ohana/src/addstar/src/MatchHeaders.c
===================================================================
--- trunk/Ohana/src/addstar/src/MatchHeaders.c	(revision 34404)
+++ trunk/Ohana/src/addstar/src/MatchHeaders.c	(revision 34405)
@@ -58,5 +58,7 @@
     if (!strcmp (exttype, "PS1_V2")) goto keep;
     if (!strcmp (exttype, "PS1_V3")) goto keep;
+    if (!strcmp (exttype, "PS1_V4")) goto keep;
     if (!strcmp (exttype, "PS1_SV1")) goto keep;
+    if (!strcmp (exttype, "PS1_SV2")) goto keep;
     continue;
 
Index: trunk/Ohana/src/addstar/src/ReadStarsFITS.c
===================================================================
--- trunk/Ohana/src/addstar/src/ReadStarsFITS.c	(revision 34404)
+++ trunk/Ohana/src/addstar/src/ReadStarsFITS.c	(revision 34405)
@@ -1,3 +1,18 @@
 # include "addstar.h"
+
+// this is the complete list of FITS format input files the addstar knows 
+// (excluding SDSS data and reference database info, such as 2MASS)
+// NOTE: these must also be listed in MatchHeaders.c (line ~ 62)
+Stars     *Convert_SMPDATA	  PROTO((FTable *table, unsigned int *nstars));
+Stars     *Convert_PS1_DEV_0	  PROTO((FTable *table, unsigned int *nstars));
+Stars     *Convert_PS1_DEV_1	  PROTO((FTable *table, unsigned int *nstars));
+Stars     *Convert_PS1_V1	  PROTO((FTable *table, unsigned int *nstars));
+Stars     *Convert_PS1_V1_Alt	  PROTO((FTable *table, unsigned int *nstars));
+Stars     *Convert_PS1_V2	  PROTO((FTable *table, unsigned int *nstars));
+Stars     *Convert_PS1_V3	  PROTO((FTable *table, unsigned int *nstars));
+Stars     *Convert_PS1_V4	  PROTO((FTable *table, unsigned int *nstars));
+Stars     *Convert_PS1_SV1	  PROTO((FTable *table, unsigned int *nstars));
+Stars     *Convert_PS1_SV1_Alt	  PROTO((FTable *table, unsigned int *nstars));
+Stars     *Convert_PS1_SV2	  PROTO((FTable *table, unsigned int *nstars));
 
 // given a file with the pointer at the start of the table block and the 
@@ -54,6 +69,12 @@
     stars = Convert_PS1_V3 (&table, &Nstars);
   }
+  if (!strcmp (type, "PS1_V4")) {
+    stars = Convert_PS1_V4 (&table, &Nstars);
+  }
   if (!strcmp (type, "PS1_SV1")) {
     stars = Convert_PS1_SV1 (&table, &Nstars);
+  }
+  if (!strcmp (type, "PS1_SV2")) {
+    stars = Convert_PS1_SV2 (&table, &Nstars);
   }
   if (stars == NULL) {
@@ -120,5 +141,5 @@
 
     stars[i].measure.psfChisq 	= NAN;	     // not provided by SMPDATA:
-    stars[i].measure.psfQual  	= NAN;	     // not provided by SMPDATA:
+    stars[i].measure.psfQF  	= NAN;	     // not provided by SMPDATA:
     stars[i].measure.psfNdof    = NAN_S_INT; // not provided by SMPDATA:
     stars[i].measure.psfNpix    = NAN_S_INT; // not provided by SMPDATA:
@@ -189,5 +210,5 @@
 
     stars[i].measure.psfChisq 	= ps1data[i].psfChisq;
-    stars[i].measure.psfQual  	= ps1data[i].psfQual;
+    stars[i].measure.psfQF  	= ps1data[i].psfQF;
     stars[i].measure.psfNdof    = NAN_S_INT; // not provided by PS1_DEV_0:
     stars[i].measure.psfNpix    = NAN_S_INT; // not provided by PS1_DEV_0:
@@ -261,5 +282,5 @@
 
     stars[i].measure.psfChisq   = ps1data[i].psfChisq;
-    stars[i].measure.psfQual    = ps1data[i].psfQual;
+    stars[i].measure.psfQF      = ps1data[i].psfQF;
     stars[i].measure.psfNdof    = NAN_S_INT; // not provided by PS1_DEV_1:
     stars[i].measure.psfNpix    = NAN_S_INT; // not provided by PS1_DEV_1:
@@ -345,5 +366,5 @@
                         
     stars[i].measure.psfChisq   = ps1data[i].psfChisq;
-    stars[i].measure.psfQual    = ps1data[i].psfQual;
+    stars[i].measure.psfQF      = ps1data[i].psfQF;
     stars[i].measure.psfNdof    = ps1data[i].psfNdof;
     stars[i].measure.psfNpix    = ps1data[i].psfNpix;
@@ -430,5 +451,5 @@
                         
     stars[i].measure.psfChisq   = ps1data[i].psfChisq;
-    stars[i].measure.psfQual    = ps1data[i].psfQual;
+    stars[i].measure.psfQF      = ps1data[i].psfQF;
     stars[i].measure.psfNdof    = ps1data[i].psfNdof;
     stars[i].measure.psfNpix    = ps1data[i].psfNpix;
@@ -507,5 +528,5 @@
                         
     stars[i].measure.psfChisq   = ps1data[i].psfChisq;
-    stars[i].measure.psfQual    = ps1data[i].psfQual;
+    stars[i].measure.psfQF      = ps1data[i].psfQF;
     stars[i].measure.psfNdof    = ps1data[i].psfNdof;
     stars[i].measure.psfNpix    = ps1data[i].psfNpix;
@@ -584,5 +605,5 @@
                         
     stars[i].measure.psfChisq   = ps1data[i].psfChisq;
-    stars[i].measure.psfQual    = ps1data[i].psfQual;
+    stars[i].measure.psfQF      = ps1data[i].psfQF;
     stars[i].measure.psfNdof    = ps1data[i].psfNdof;
     stars[i].measure.psfNpix    = ps1data[i].psfNpix;
@@ -616,5 +637,5 @@
 }
 
-Stars *Convert_PS1_SV1 (FTable *table, unsigned int *nstars) {
+Stars *Convert_PS1_V4 (FTable *table, unsigned int *nstars) {
 
   off_t Nstars; 
@@ -622,12 +643,7 @@
   double ZeroPt;
   Stars *stars;
-  CMF_PS1_SV1 *ps1data;
-
-  if (table[0].header[0].Naxis[0] == 196) {
-    stars = Convert_PS1_SV1_Alt (table, nstars);
-    return (stars);
-  }
-
-  ps1data = gfits_table_get_CMF_PS1_SV1 (table, &Nstars, NULL);
+  CMF_PS1_V4 *ps1data;
+
+  ps1data = gfits_table_get_CMF_PS1_V4 (table, &Nstars, NULL);
   if (!ps1data) {
     fprintf (stderr, "skipping inconsistent entry\n");
@@ -658,5 +674,5 @@
     stars[i].measure.Mkron      = (ps1data[i].kronFlux > 0.0) ? -2.5*log10(ps1data[i].kronFlux) + ZeroPt : NAN;
     stars[i].measure.dMkron     = (ps1data[i].kronFlux > 0.0) ? ps1data[i].kronFluxErr / ps1data[i].kronFlux : NAN;
-
+                        
     // these fluxes are converted from counts to counts/sec in FilterStars.c
     stars[i].measure.FluxPSF    = ps1data[i].Flux;
@@ -669,5 +685,5 @@
                         
     stars[i].measure.psfChisq   = ps1data[i].psfChisq;
-    stars[i].measure.psfQual    = ps1data[i].psfQual;
+    stars[i].measure.psfQF      = ps1data[i].psfQF;
     stars[i].measure.psfNdof    = ps1data[i].psfNdof;
     stars[i].measure.psfNpix    = ps1data[i].psfNpix;
@@ -689,6 +705,9 @@
 
     // the Average fields and the following Measure fields are set in FilterStars after
-    // the image metadata is in hand:  dR, dD, Mcal, dt, airmass, az, t, imageID, extID, 
-    // averef is set in find_matches, dbFlags is zero on ingest.
+    // the image metadata is in hand:  dR, dD, Mcal, dt, airmass, az, t, imageID, extID.
+
+    // averef is set in find_matches
+
+    // dbFlags is zero on ingest.
 
     // the following fields are currently not being set anywhere: t_msec
@@ -698,5 +717,5 @@
 }
 
-Stars *Convert_PS1_SV1_Alt (FTable *table, unsigned int *nstars) {
+Stars *Convert_PS1_SV1 (FTable *table, unsigned int *nstars) {
 
   off_t Nstars; 
@@ -706,7 +725,10 @@
   CMF_PS1_SV1 *ps1data;
 
-  // some test output files were produced called CMF_PS1_SV1 but with mismatch byte boundaries
-
-  ps1data = gfits_table_get_CMF_PS1_SV1_Alt (table, &Nstars, NULL);
+  if (table[0].header[0].Naxis[0] == 196) {
+    stars = Convert_PS1_SV1_Alt (table, nstars);
+    return (stars);
+  }
+
+  ps1data = gfits_table_get_CMF_PS1_SV1 (table, &Nstars, NULL);
   if (!ps1data) {
     fprintf (stderr, "skipping inconsistent entry\n");
@@ -748,5 +770,5 @@
                         
     stars[i].measure.psfChisq   = ps1data[i].psfChisq;
-    stars[i].measure.psfQual    = ps1data[i].psfQual;
+    stars[i].measure.psfQF      = ps1data[i].psfQF;
     stars[i].measure.psfNdof    = ps1data[i].psfNdof;
     stars[i].measure.psfNpix    = ps1data[i].psfNpix;
@@ -777,2 +799,159 @@
 }
 
+Stars *Convert_PS1_SV1_Alt (FTable *table, unsigned int *nstars) {
+
+  off_t Nstars; 
+  unsigned int i;
+  double ZeroPt;
+  Stars *stars;
+  CMF_PS1_SV1 *ps1data;
+
+  // some test output files were produced called CMF_PS1_SV1 but with mismatch byte boundaries
+
+  ps1data = gfits_table_get_CMF_PS1_SV1_Alt (table, &Nstars, NULL);
+  if (!ps1data) {
+    fprintf (stderr, "skipping inconsistent entry\n");
+    return (NULL);
+  }
+  ZeroPt = GetZeroPoint();
+
+  ALLOCATE (stars, Stars, Nstars);
+  for (i = 0; i < Nstars; i++) {
+    InitStar (&stars[i]);
+    stars[i].measure.Xccd       = ps1data[i].X;
+    stars[i].measure.Yccd       = ps1data[i].Y;
+    stars[i].measure.dXccd      = ToShortPixels(ps1data[i].dX);
+    stars[i].measure.dYccd      = ToShortPixels(ps1data[i].dY);
+
+    stars[i].measure.posangle   = ToShortDegrees(ps1data[i].posangle);
+    stars[i].measure.pltscale   = ps1data[i].pltscale;
+
+    if ((ps1data[i].M >= 0.0) || isnan(ps1data[i].M)) {
+      stars[i].measure.M      = NAN;
+    } else {
+      stars[i].measure.M      = ps1data[i].M + ZeroPt;
+    }
+    stars[i].measure.dM         = ps1data[i].dM;
+    stars[i].measure.dMcal      = ps1data[i].dMcal;
+    stars[i].measure.Map        = ps1data[i].Map + ZeroPt;
+                        
+    stars[i].measure.Mkron      = (ps1data[i].kronFlux > 0.0) ? -2.5*log10(ps1data[i].kronFlux) + ZeroPt : NAN;
+    stars[i].measure.dMkron     = (ps1data[i].kronFlux > 0.0) ? ps1data[i].kronFluxErr / ps1data[i].kronFlux : NAN;
+
+    // these fluxes are converted from counts to counts/sec in FilterStars.c
+    stars[i].measure.FluxPSF    = ps1data[i].Flux;
+    stars[i].measure.dFluxPSF   = ps1data[i].dFlux;
+    stars[i].measure.FluxKron   = ps1data[i].kronFlux;
+    stars[i].measure.dFluxKron  = ps1data[i].kronFluxErr;
+
+    stars[i].measure.Sky        = ps1data[i].sky;
+    stars[i].measure.dSky       = ps1data[i].dSky;
+                        
+    stars[i].measure.psfChisq   = ps1data[i].psfChisq;
+    stars[i].measure.psfQF      = ps1data[i].psfQF;
+    stars[i].measure.psfNdof    = ps1data[i].psfNdof;
+    stars[i].measure.psfNpix    = ps1data[i].psfNpix;
+    stars[i].measure.crNsigma   = ps1data[i].crNsigma;
+    stars[i].measure.extNsigma  = ps1data[i].extNsigma;
+
+    stars[i].measure.FWx        = ToShortPixels(ps1data[i].fx);
+    stars[i].measure.FWy        = ToShortPixels(ps1data[i].fy);
+    stars[i].measure.theta      = ToShortDegrees(ps1data[i].df);
+
+    stars[i].measure.Mxx        = ToShortPixels(ps1data[i].Mxx);
+    stars[i].measure.Mxy        = ToShortPixels(ps1data[i].Mxy);
+    stars[i].measure.Myy        = ToShortPixels(ps1data[i].Myy);
+                        
+    stars[i].measure.photFlags  = ps1data[i].flags;
+
+    // this is may optionally be replaced by the internal sequence (see FilterStars.c)
+    stars[i].measure.detID      = ps1data[i].detID; 
+
+    // the Average fields and the following Measure fields are set in FilterStars after
+    // the image metadata is in hand:  dR, dD, Mcal, dt, airmass, az, t, imageID, extID, 
+    // averef is set in find_matches, dbFlags is zero on ingest.
+
+    // the following fields are currently not being set anywhere: t_msec
+  }    
+  *nstars = Nstars;
+  return (stars);
+}
+
+Stars *Convert_PS1_SV2 (FTable *table, unsigned int *nstars) {
+
+  off_t Nstars; 
+  unsigned int i;
+  double ZeroPt;
+  Stars *stars;
+  CMF_PS1_SV2 *ps1data;
+
+  ps1data = gfits_table_get_CMF_PS1_SV2 (table, &Nstars, NULL);
+  if (!ps1data) {
+    fprintf (stderr, "skipping inconsistent entry\n");
+    return (NULL);
+  }
+  ZeroPt = GetZeroPoint();
+
+  ALLOCATE (stars, Stars, Nstars);
+  for (i = 0; i < Nstars; i++) {
+    InitStar (&stars[i]);
+    stars[i].measure.Xccd       = ps1data[i].X;
+    stars[i].measure.Yccd       = ps1data[i].Y;
+    stars[i].measure.dXccd      = ToShortPixels(ps1data[i].dX);
+    stars[i].measure.dYccd      = ToShortPixels(ps1data[i].dY);
+
+    stars[i].measure.posangle   = ToShortDegrees(ps1data[i].posangle);
+    stars[i].measure.pltscale   = ps1data[i].pltscale;
+
+    if ((ps1data[i].M >= 0.0) || isnan(ps1data[i].M)) {
+      stars[i].measure.M      = NAN;
+    } else {
+      stars[i].measure.M      = ps1data[i].M + ZeroPt;
+    }
+    stars[i].measure.dM         = ps1data[i].dM;
+    stars[i].measure.dMcal      = ps1data[i].dMcal;
+    stars[i].measure.Map        = ps1data[i].Map + ZeroPt;
+                        
+    stars[i].measure.Mkron      = (ps1data[i].kronFlux > 0.0) ? -2.5*log10(ps1data[i].kronFlux) + ZeroPt : NAN;
+    stars[i].measure.dMkron     = (ps1data[i].kronFlux > 0.0) ? ps1data[i].kronFluxErr / ps1data[i].kronFlux : NAN;
+
+    // these fluxes are converted from counts to counts/sec in FilterStars.c
+    stars[i].measure.FluxPSF    = ps1data[i].Flux;
+    stars[i].measure.dFluxPSF   = ps1data[i].dFlux;
+    stars[i].measure.FluxKron   = ps1data[i].kronFlux;
+    stars[i].measure.dFluxKron  = ps1data[i].kronFluxErr;
+
+    stars[i].measure.Sky        = ps1data[i].sky;
+    stars[i].measure.dSky       = ps1data[i].dSky;
+                        
+    stars[i].measure.psfChisq   = ps1data[i].psfChisq;
+    stars[i].measure.psfQF      = ps1data[i].psfQF;
+    stars[i].measure.psfNdof    = ps1data[i].psfNdof;
+    stars[i].measure.psfNpix    = ps1data[i].psfNpix;
+    stars[i].measure.crNsigma   = ps1data[i].crNsigma;
+    stars[i].measure.extNsigma  = ps1data[i].extNsigma;
+
+    stars[i].measure.FWx        = ToShortPixels(ps1data[i].fx);
+    stars[i].measure.FWy        = ToShortPixels(ps1data[i].fy);
+    stars[i].measure.theta      = ToShortDegrees(ps1data[i].df);
+
+    stars[i].measure.Mxx        = ToShortPixels(ps1data[i].Mxx);
+    stars[i].measure.Mxy        = ToShortPixels(ps1data[i].Mxy);
+    stars[i].measure.Myy        = ToShortPixels(ps1data[i].Myy);
+                        
+    stars[i].measure.photFlags  = ps1data[i].flags;
+
+    // this is may optionally be replaced by the internal sequence (see FilterStars.c)
+    stars[i].measure.detID      = ps1data[i].detID; 
+
+    // the Average fields and the following Measure fields are set in FilterStars after
+    // the image metadata is in hand:  dR, dD, Mcal, dt, airmass, az, t, imageID, extID, 
+    // averef is set in find_matches, dbFlags is zero on ingest.
+
+    // the following fields are currently not being set anywhere: t_msec
+  }    
+  *nstars = Nstars;
+  return (stars);
+}
+
+
Index: trunk/Ohana/src/addstar/src/SEDfit.c
===================================================================
--- trunk/Ohana/src/addstar/src/SEDfit.c	(revision 34404)
+++ trunk/Ohana/src/addstar/src/SEDfit.c	(revision 34405)
@@ -147,27 +147,10 @@
     // construct an average object for this object
     // XXX for now, the output objects will have limited astrometric interpretation...
+    dvo_average_init (&outcat[0].average[Nave]);
     outcat[0].average[Nave].R         = incat[0].average[i].R;
     outcat[0].average[Nave].D         = incat[0].average[i].D;
-    outcat[0].average[Nave].dR    = 0;
-    outcat[0].average[Nave].dD    = 0;
-    outcat[0].average[Nave].uR    = 0;
-    outcat[0].average[Nave].uD    = 0;
-    outcat[0].average[Nave].duR   = 0;
-    outcat[0].average[Nave].duD   = 0;
-    outcat[0].average[Nave].P     = 0;
-    outcat[0].average[Nave].dP    = 0;
-
-    // XXX for now, set the average mag data to NULL
-    outcat[0].average[Nave].Nmeasure  	  = 0;
-    outcat[0].average[Nave].Nmissing  	  = 0;
-    outcat[0].average[Nave].Xp        	  = NAN_S_SHORT;
-    outcat[0].average[Nave].measureOffset = Nmeas;
-    outcat[0].average[Nave].missingOffset = -1;
-    outcat[0].average[Nave].flags         = 0;
 
     for (j = 0; j < Nsec; j++) {
-      outcat[0].secfilt[Nave*Nsec+j].M  = NAN;
-      outcat[0].secfilt[Nave*Nsec+j].dM = NAN;
-      outcat[0].secfilt[Nave*Nsec+j].Xm = NAN_S_SHORT;
+      dvo_secfilt_init (&outcat[0].secfilt[Nave*Nsec+j]);
     }
 
@@ -181,4 +164,5 @@
     for (j = 0; valid && (j < Nmodel); j++) {
       n = modelRow[j];
+      dvo_measure_init (&outcat[0].measure[Nmeas]);
       outcat[0].measure[Nmeas].dR        = 0.0;
       outcat[0].measure[Nmeas].dD        = 0.0;
Index: trunk/Ohana/src/addstar/src/addstar.c
===================================================================
--- trunk/Ohana/src/addstar/src/addstar.c	(revision 34404)
+++ trunk/Ohana/src/addstar/src/addstar.c	(revision 34405)
@@ -27,4 +27,9 @@
   options = args (argc, argv, options);
 
+  if (options.mode == ADDSTAR_MODE_CREATE_ID) {
+    addstar_create_ID ();
+    exit (0);
+  }
+
   sky = SkyTableLoadOptimal (CATDIR, SKY_TABLE, GSCFILE, TRUE, SKY_DEPTH, VERBOSE);
   if (sky == NULL) {
Index: trunk/Ohana/src/addstar/src/addstar_create_ID.c
===================================================================
--- trunk/Ohana/src/addstar/src/addstar_create_ID.c	(revision 34405)
+++ trunk/Ohana/src/addstar/src/addstar_create_ID.c	(revision 34405)
@@ -0,0 +1,43 @@
+# include "addstar.h"
+
+// If this function is unable to open the image table, it creates a new one
+// set the database ID in the Image.dat header
+int addstar_create_ID () {
+
+  int status;
+  FITS_DB db;
+
+  /*** update the image table ***/
+  /* setup image table format and lock */
+  db.mode   = dvo_catalog_catmode (CATMODE);
+  db.format = dvo_catalog_catformat (CATFORMAT);
+  status    = dvo_image_lock (&db, ImageCat, 2.0, LCK_XCLD);  // shorter timeout?
+  if (!status) Shutdown ("ERROR: failure to lock image catalog %s", db.filename);
+
+  /* load or create the image table */
+  if (db.dbstate == LCK_EMPTY) {
+    if (VERBOSE) fprintf (stderr, "can't find %s, creating a new one\n", ImageCat);
+    dvo_image_create (&db, GetZeroPoint());
+  } else {
+    if (!dvo_image_load (&db, VERBOSE, FORCE_READ)) {
+      Shutdown ("can't read image catalog %s", db.filename);
+    }
+    if (!dvo_image_createID (&db.header)) {
+      fprintf (stderr, "failed to add database ID\n");
+      exit (1);
+    }
+  }
+  fseeko (db.f, 0, SEEK_SET);
+
+  status = fwrite (db.header.buffer, 1, db.header.datasize, db.f);
+  if (status != db.header.datasize) {
+    fprintf (stderr, "failed to write data to image header\n");
+    exit (1);
+  }
+
+  dvo_image_unlock (&db);
+
+  return TRUE;
+}
+
+// NOTE: Photcodes (and ZERO_POINT) and ImageCat are set in ConfigInit.c
Index: trunk/Ohana/src/addstar/src/args.c
===================================================================
--- trunk/Ohana/src/addstar/src/args.c	(revision 34404)
+++ trunk/Ohana/src/addstar/src/args.c	(revision 34405)
@@ -32,4 +32,8 @@
   if ((N = get_argument (argc, argv, "-resort"))) {
     options.mode = ADDSTAR_MODE_RESORT;
+    remove_argument (N, &argc, argv);
+  }
+  if ((N = get_argument (argc, argv, "-create-id"))) {
+    options.mode = ADDSTAR_MODE_CREATE_ID;
     remove_argument (N, &argc, argv);
   }
@@ -101,4 +105,5 @@
       if (options.mode == ADDSTAR_MODE_FAKEIMAGE) goto allow;
       if (options.mode == ADDSTAR_MODE_REFLIST) goto allow;
+      if (options.mode == ADDSTAR_MODE_CREATE_ID) goto allow;
       if (options.mode == ADDSTAR_MODE_REFCAT) {
 	  fprintf (stderr, "you have requested uploading from a catalog to the entire sky in one pass\n");
@@ -364,4 +369,5 @@
   }
 
+  if ((options.mode == ADDSTAR_MODE_CREATE_ID) && (argc == 1)) return (options);
   if ((options.mode == ADDSTAR_MODE_RESORT) && (argc == 1)) return (options);
   if (argc == 2) return (options);
@@ -378,4 +384,5 @@
   fprintf (stderr, "USAGE: addstar -fakeimage (ra) (dec) (theta) (name)\n");
   fprintf (stderr, "USAGE: addstar -resort (SkyRegion)\n");
+  fprintf (stderr, "USAGE: addstar -add-id\n");
   fprintf (stderr, "USAGE: addstar -ppm (filename)\n");
   exit (2);
Index: trunk/Ohana/src/addstar/src/load2mass_catalog.c
===================================================================
--- trunk/Ohana/src/addstar/src/load2mass_catalog.c	(revision 34404)
+++ trunk/Ohana/src/addstar/src/load2mass_catalog.c	(revision 34405)
@@ -34,5 +34,5 @@
     catalog[0].average[Nave].Nmeasure  	   = 0;
     catalog[0].average[Nave].Nmissing  	   = 0;
-    catalog[0].average[Nave].Xp        	   = NAN_S_SHORT;
+    catalog[0].average[Nave].ChiSqAve  	   = NAN_S_SHORT;
     catalog[0].average[Nave].measureOffset = Nmeas;
     catalog[0].average[Nave].missingOffset = -1;
Index: trunk/Ohana/src/addstar/src/mkcmf.c
===================================================================
--- trunk/Ohana/src/addstar/src/mkcmf.c	(revision 34404)
+++ trunk/Ohana/src/addstar/src/mkcmf.c	(revision 34405)
@@ -14,4 +14,5 @@
 void gauss_init (int Nbin);
 double rnd_gauss (double mean, double sigma);
+void writeStars_PS1_V4 (FTable *ftable, double *X, double *Y, double *M, unsigned int *Flag, int Nstars);
 void writeStars_PS1_V3 (FTable *ftable, double *X, double *Y, double *M, unsigned int *Flag, int Nstars);
 void writeStars_PS1_V2 (FTable *ftable, double *X, double *Y, double *M, unsigned int *Flag, int Nstars);
@@ -283,4 +284,8 @@
     found = TRUE;
   }
+  if (!strcmp(type, "PS1_V4")) {
+    writeStars_PS1_V4 (&ftable, X, Y, M, Flag, Nstars); 
+    found = TRUE;
+  }
   if (!found) {
     fprintf (stderr, "ERROR: unknown CMF type %s\n", type);
@@ -419,5 +424,5 @@
     stars[i].fy        = FY;
     stars[i].df        = DF;
-    stars[i].psfQual   = PSFQUAL;
+    stars[i].psfQF   = PSFQUAL;
     stars[i].nFrames   = 1;
   }
@@ -466,5 +471,5 @@
     stars[i].fy        = FY;
     stars[i].df        = DF;
-    stars[i].psfQual   = PSFQUAL;
+    stars[i].psfQF   = PSFQUAL;
     stars[i].nFrames   = 1;
     stars[i].flags     = FLAGS;
@@ -514,5 +519,5 @@
     stars[i].fy        = FY;
     stars[i].df        = DF;
-    stars[i].psfQual   = PSFQUAL;
+    stars[i].psfQF   = PSFQUAL;
     stars[i].nFrames   = 1;
     stars[i].flags     = FLAGS;
@@ -551,7 +556,7 @@
     // randomly give poor PSFQF values
     if ((BAD_PSFQF_FRAC > 0.0) && (drand48() < BAD_PSFQF_FRAC)) {
-      stars[i].psfQual   = 0.25;
+      stars[i].psfQF   = 0.25;
     } else {
-      stars[i].psfQual   = PSFQUAL;
+      stars[i].psfQF   = PSFQUAL;
     }
     
@@ -606,7 +611,7 @@
     // randomly give poor PSFQF values
     if ((BAD_PSFQF_FRAC > 0.0) && (drand48() < BAD_PSFQF_FRAC)) {
-      stars[i].psfQual   = 0.25;
+      stars[i].psfQF   = 0.25;
     } else {
-      stars[i].psfQual   = PSFQUAL;
+      stars[i].psfQF   = PSFQUAL;
     }
     
@@ -635,2 +640,62 @@
 }
 
+void writeStars_PS1_V4 (FTable *ftable, double *X, double *Y, double *M, unsigned int *Flag, int Nstars) {
+
+  int i;
+  CMF_PS1_V4 *stars;
+  float flux, fSN;
+
+  // XXX add gaussian-distributed noise based on counts
+  // this needs to make different output 'stars' entries depending on the desired type
+  ALLOCATE (stars, CMF_PS1_V4, Nstars);
+  gauss_init (2048);
+  for (i = 0; i < Nstars; i++) {
+    stars[i].detID = i;
+
+    flux = pow (10.0, -0.4*M[i]);
+    fSN = 1.0 / sqrt(flux);
+
+    stars[i].X = X[i];
+    stars[i].Y = Y[i];
+    stars[i].M = M[i];
+    stars[i].Map = M[i] - 0.05;
+
+    if (ADDNOISE) {
+      stars[i].X += FX * fSN * rnd_gauss(0.0, 1.0);
+      stars[i].Y += FY * fSN * rnd_gauss(0.0, 1.0);
+      stars[i].M += fSN*rnd_gauss(0.0, 1.0);
+    }
+
+    // randomly give poor PSFQF values
+    if ((BAD_PSFQF_FRAC > 0.0) && (drand48() < BAD_PSFQF_FRAC)) {
+      stars[i].psfQF     = 0.25;
+      stars[i].psfQFperf = 0.24;
+    } else {
+      stars[i].psfQF     = PSFQUAL;
+      stars[i].psfQFperf = MAX(PSFQUAL - 0.01, 0.0);
+    }
+    
+    stars[i].dX = FX * fSN;
+    stars[i].dY = FY * fSN;
+    stars[i].dM = fSN;
+
+    stars[i].Mpeak     = M[i] + 1.0;
+    stars[i].sky       = SKY;
+    stars[i].dSky      = DSKY;
+    stars[i].psfChisq  = PSFCHI;
+    stars[i].crNsigma  = CRN;
+    stars[i].extNsigma = EXTN;
+    stars[i].fx        = FX;
+    stars[i].fy        = FY;
+    stars[i].df        = DF;
+    stars[i].nFrames   = 1;
+    stars[i].flags     = Flag[i];
+
+    stars[i].kronFlux  = flux * 1.25;
+    stars[i].kronFluxErr = fSN * flux * 1.25;
+  }
+
+  gfits_table_set_CMF_PS1_V4 (ftable, stars, Nstars);
+  gfits_modify (ftable->header, "EXTTYPE",   "%s", 1, "PS1_V4");
+}
+
Index: trunk/Ohana/src/addstar/src/update_coords.c
===================================================================
--- trunk/Ohana/src/addstar/src/update_coords.c	(revision 34404)
+++ trunk/Ohana/src/addstar/src/update_coords.c	(revision 34405)
@@ -47,6 +47,6 @@
   dR2 = r2 / Npt - r*r;
   dD2 = d2 / Npt - d*d;
-  average[0].Xp = sqrt (dD2 + dR2 / SQ(cos(d*RAD_DEG)));
-  /* Xp is scatter in position in hundredths of arcsec */
+  average[0].ChiSqAve = sqrt (dD2 + dR2 / SQ(cos(d*RAD_DEG)));
+  /* ChiSqAve is supposed to be a chisq */
 
   return;
Index: trunk/Ohana/src/addstar/test/simple.dvo
===================================================================
--- trunk/Ohana/src/addstar/test/simple.dvo	(revision 34404)
+++ trunk/Ohana/src/addstar/test/simple.dvo	(revision 34405)
@@ -4,4 +4,5 @@
 
 macro test.all
+  # test.fields (DVO schema) (CMF format)
   test.fields PS1_DEV_0 PS1_V1
   test.fields PS1_DEV_1 PS1_V1
@@ -9,4 +10,5 @@
   test.fields PS1_V2 	PS1_V1
   test.fields PS1_V3 	PS1_V1
+  test.fields PS1_V4 	PS1_V1
 
   test.fields PS1_DEV_0 PS1_V2
@@ -15,4 +17,5 @@
   test.fields PS1_V2 	PS1_V2
   test.fields PS1_V3 	PS1_V2
+  test.fields PS1_V4 	PS1_V4
 
   test.fields PS1_DEV_0 PS1_V3
@@ -21,4 +24,5 @@
   test.fields PS1_V2 	PS1_V3
   test.fields PS1_V3 	PS1_V3
+  test.fields PS1_V4 	PS1_V4
 
   test.fields PS1_DEV_0 PS1_V4
@@ -27,4 +31,5 @@
   test.fields PS1_V2 	PS1_V4
   test.fields PS1_V3 	PS1_V4
+  test.fields PS1_V4 	PS1_V4
 end  
 
@@ -36,5 +41,6 @@
   end
 
-  tapPLAN 51
+  echo ""
+  echo "testing --- CMF = $1, DVO = $2 ---"
 
   exec rm -rf catdir.test
@@ -45,7 +51,14 @@
   mkinput
   exec mkcmf test.in.txt test.cmf -date 2008/1/1 -time 01:00:00 -radec $RA $DEC -type $1
-  exec addstar -D CATDIR catdir.test -D CAMERA simtest test.cmf -D CATFORMAT $2 -quick-airmass
+  if ($TAP_VERBOSE)
+    exec addstar -D CATDIR catdir.test -D CAMERA simtest test.cmf -D CATFORMAT $2 -quick-airmass
+  else
+    exec addstar -D CATDIR catdir.test -D CAMERA simtest test.cmf -D CATFORMAT $2 -quick-airmass >& tmp.log
+  end
 
   list testfields -copy testfields_$1
+
+  # set the number of tests based on the testfields list
+  tapPLAN {3 + 3*($testfields:n - 1)}
 
   catdir catdir.test
@@ -77,6 +90,12 @@
   for i 1 $testfields:n
     list name -split $testfields:$i
-    if ("$name:0" == "SKIP") continue
-    if ("$name:2" == "SKIP") continue
+    if ("$name:0" == "SKIP") 
+      tapSKIP 3
+      continue
+    end
+    if ("$name:2" == "SKIP") 
+      tapSKIP 3
+      continue
+    end
     # must be the same length
     set v1 = $name:0
@@ -106,5 +125,5 @@
     # THETA is stored to only (360/65536) deg accuracy
     if ("$name:0" == "PSF_THETA")
-      echo $MEAN
+      # echo $MEAN
       tapOK {abs($MEAN)  < 0.006} "$name:0 vs $name:2 (MEAN)"
       tapOK {abs($SIGMA) < 0.001} "$name:0 vs $name:2 (SIGMA)"
@@ -304,2 +323,52 @@
   N_FRAMES          : SKIP # not ingested into DVO
 end
+
+# this list is good for PS1_V4
+list testfields_PS1_V4
+  IPP_IDET          : detid	
+  X_PSF             : xccd	
+  Y_PSF             : yccd	
+  X_PSF_SIG         : xccd:err # FAIL
+  Y_PSF_SIG         : yccd:err # FAIL
+  POSANGLE          : SKIP # astrometry is not calibrated in the cmf
+  PLTSCALE          : SKIP # astrometry is not calibrated in the cmf
+  PSF_INST_MAG      : mag:inst	
+  PSF_INST_MAG_SIG  : mag:err	
+  PSF_INST_FLUX     : SKIP # not ingested into DVO
+  PSF_INST_FLUX_SIG : SKIP # not ingested into DVO
+  AP_MAG_STANDARD   : mag:aperinst # FAIL
+  AP_MAG_RAW        : SKIP # not ingested into DVO
+  AP_MAG_RADIUS     : SKIP # not ingested into DVO
+  CAL_PSF_MAG       : SKIP # photometry is not calibrated in the cmf
+  CAL_PSF_MAG_SIG   : SKIP # photometry is not calibrated in the cmf
+  RA_PSF            : SKIP # astrometry is not calibrated in the cmf
+  DEC_PSF           : SKIP # astrometry is not calibrated in the cmf
+  PEAK_FLUX_AS_MAG  : SKIP # not ingested into DVO
+  SKY               : sky	
+  SKY_SIG           : sky_err	
+  PSF_CHISQ         : psf_chisq	
+  CR_NSIGMA         : cr_nsigma	
+  EXT_NSIGMA        : ext_nsigma
+  PSF_MAJOR         : FWHM_MAJ	
+  PSF_MINOR         : FWHM_MIN	
+  PSF_THETA         : THETA # FAIL
+  PSF_QF            : PSF_QF	
+  PSF_QF_PERFECT    : SKIP # not ingested into DVO
+  PSF_NDOF          : PSF_NDOF
+  PSF_NPIX          : PSF_NPIX
+  MOMENTS_XX        : MXX
+  MOMENTS_XY        : MXY
+  MOMENTS_YY        : MYY
+  MOMENTS_M3C       : SKIP # not ingested into DVO
+  MOMENTS_M3S       : SKIP # not ingested into DVO
+  MOMENTS_M4C       : SKIP # not ingested into DVO
+  MOMENTS_M4S       : SKIP # not ingested into DVO
+  MOMENTS_R1        : SKIP # not ingested into DVO
+  MOMENTS_RH        : SKIP # not ingested into DVO
+  KRON_FLUX         : mag:kroninst
+  KRON_FLUX_ERR     : mag:kronerr
+  KRON_FLUX_INNER   : SKIP # not ingested into DVO
+  KRON_FLUX_OUTER   : SKIP # not ingested into DVO
+  FLAGS             : phot_flags
+  N_FRAMES          : SKIP # not ingested into DVO
+end
Index: trunk/Ohana/src/addstar/test/tap.dvo
===================================================================
--- trunk/Ohana/src/addstar/test/tap.dvo	(revision 34404)
+++ trunk/Ohana/src/addstar/test/tap.dvo	(revision 34405)
@@ -2,4 +2,5 @@
 
 if (not($?TAP_BREAK)) set TAP_BREAK = 0
+if (not($?TAP_VERBOSE)) set TAP_VERBOSE = 0
 
 macro tapOK
@@ -10,5 +11,7 @@
 
   if ($1)
-    echo "ok : $2"
+    if ($TAP_VERBOSE)
+      echo "ok : $2"
+    end
     $TAP_LAST = 1
   else
@@ -42,6 +45,6 @@
   end
 
-  $TAP_NSKIP ++
-  $TAP_NDONE ++
+  $TAP_NSKIP += $1
+  $TAP_NDONE += $1
 end
 
Index: trunk/Ohana/src/dvomerge/Makefile
===================================================================
--- trunk/Ohana/src/dvomerge/Makefile	(revision 34404)
+++ trunk/Ohana/src/dvomerge/Makefile	(revision 34405)
@@ -33,4 +33,5 @@
 $(SRC)/dvomergeUpdate_catalogs.$(ARCH).o \
 $(SRC)/dvomergeUpdate_threaded.$(ARCH).o \
+$(SRC)/dvomergeHistory.$(ARCH).o \
 $(SRC)/dvomergeFromList.$(ARCH).o \
 $(SRC)/dvomergeImageIDs.$(ARCH).o \
@@ -56,4 +57,5 @@
 $(SRC)/dvomerge_client.$(ARCH).o \
 $(SRC)/dvomergeUpdate_catalogs.$(ARCH).o \
+$(SRC)/dvomergeHistory.$(ARCH).o \
 $(SRC)/dvomergeImageIDs.$(ARCH).o \
 $(SRC)/dvo_image_merge_dbs.$(ARCH).o \
Index: trunk/Ohana/src/dvomerge/doc/failsafe.txt
===================================================================
--- trunk/Ohana/src/dvomerge/doc/failsafe.txt	(revision 34404)
+++ trunk/Ohana/src/dvomerge/doc/failsafe.txt	(revision 34405)
@@ -1,2 +1,28 @@
+
+20120809
+
+more on failsafe
+
+I need to improve the technique I'm using for failsafe ops / rerun.
+
+ * Image.dat PHU keywords:
+   DVO_DB_ID : %s : hex string with 32 byte "unique" string for DB
+   DM_NMERGE : %d : number of databases merged into this onea
+   DM_nnnnnn : %s : DVO_DB_ID of merge (nnnnn)
+
+   Note: nnnnnn may be greater than NMERGE if entries are skipped
+     (only a total of NMERGE nnnnnn values are allowed)
+
+ * region.cpt PHU keywords:
+   DM_NMERGE : %d : number of databases merged into this one (needed?)
+   MS_nnnnnn : %d : size of merged db entry nnnnnn
+   MT_nnnnnn : %s : date/time of merged db entry nnnnnn
+   
+ Is there a reason for the image entries?  (additional record keeping)
+
+ 
+
+
+201206??
 
 we have a problem with dvomerge and flaky hosts.  the problem is that
Index: trunk/Ohana/src/dvomerge/include/dvomerge.h
===================================================================
--- trunk/Ohana/src/dvomerge/include/dvomerge.h	(revision 34404)
+++ trunk/Ohana/src/dvomerge/include/dvomerge.h	(revision 34405)
@@ -44,4 +44,35 @@
   char *notFound;
 } IDmapType;
+
+// struct to describe a sequence of dvomerges (Image.dat header)
+typedef struct {
+  int Nmerge;
+  char **IDs;
+} dmhImage;
+
+// struct to describe a sequence of dvomerges (Object table : cpt header)
+typedef struct {
+  int   Nmerge;
+  off_t  *size;
+  time_t *time;
+  char  **date;
+} dmhObject;
+
+// data on a single table, populated when a new file is merged
+typedef struct {
+  off_t  size;
+  time_t time;
+  char  *date;
+} dmhObjectStats;
+
+// struct to describe the current status of a single output file:
+// is it on this machine (valid)? have we already merged or not (missed)?
+// what is the collection of past merges (history)?  what is the real filename?
+typedef struct {
+  int valid;		      // is this object table on this machine?
+  int missed;		      // did we fail to merge into this table yet?
+  dmhObject *history;	      // complete sequence of previous merges
+  char *filename;	      // true filename on disk 
+} OutputStatus;
 
 int        main                   PROTO((int argc, char **argv));
@@ -130,2 +161,21 @@
 IDmapType *IDmapLoad               PROTO((char *filename));
 int        create_IDmap_lookup     PROTO((IDmapType *IDmap));
+
+// dvomerge history functions
+OutputStatus *OutputStatusInit (int N);
+int OutputStatusFree (OutputStatus *outstat, int N);
+
+int dmhObjectAdd (dmhObject *history, Header *header, dmhObjectStats *inStats);
+int dmhObjectCheck (dmhObject *history, dmhObjectStats *inStats);
+dmhObject *dmhObjectRead (char *filename);
+
+void dmhObjectStatsFree (dmhObjectStats *stats);
+dmhObjectStats *dmhObjectStatsRead (char *filename);
+
+int dmhImageAdd (FITS_DB *db, dmhImage *history, char *dbID);
+dmhImage *dmhImageRead (FITS_DB *db) ;
+int dmhImageCheck (dmhImage *history, char *dbID);
+
+char *dmhImageReadID (FITS_DB *db);
+int dvoCreateID (char *catdir);
+
Index: trunk/Ohana/src/dvomerge/include/dvoverify.h
===================================================================
--- trunk/Ohana/src/dvomerge/include/dvoverify.h	(revision 34404)
+++ trunk/Ohana/src/dvomerge/include/dvoverify.h	(revision 34405)
@@ -34,4 +34,5 @@
 int    NNotSorted;
 int    CHECK_TOPLEVEL;
+int    CHECK_IMAGE_ID;
 int    LIST_MISSING;
 
@@ -52,2 +53,5 @@
 void AddFailures (char *filename);
 char **GetFailures (int *N);
+
+int LoadImageIDs (char *catdir);
+int CheckImageID (Catalog *catalog);
Index: trunk/Ohana/src/dvomerge/src/dvomergeHistory.c
===================================================================
--- trunk/Ohana/src/dvomerge/src/dvomergeHistory.c	(revision 34405)
+++ trunk/Ohana/src/dvomerge/src/dvomergeHistory.c	(revision 34405)
@@ -0,0 +1,277 @@
+# include "dvomerge.h"
+
+// We track the dvomerge history (dmh) in the headers of the Image table (Image.dat) and
+// each of the Object tables (CPT files).  In the Image table, we track the IDs of the dvo
+// databases which have been merged.  In the object tables, we track the date and size of
+// the CPT files already merged.  When we merge a new dvodb, we block against re-merging
+// any Object table if the current one matches one of the already ingested tables.
+
+// XXX should I require all IDs to be in sequence?  The sequence has no meaning except to
+// ensure the keyword numbering.  If we require the sequence, then it simplifies the code
+// somewhat
+
+// we have a sequence of dvomerge events.  
+// in the image table header, we want to track the IDs of the databases 
+// which have been merged
+
+// some (possible?) functions needed to track the history
+
+// user must pass in a char * of length DVO_DBID_LEN
+char *dmhImageReadID (FITS_DB *db) {
+
+  char dbID[33];
+  int status = gfits_scan (&db->header, "DVO_DBID", "%s", 1, dbID);      
+  if (!status) {
+    return NULL;
+  }
+
+  char *ID = strcreate (dbID);
+  return ID;
+}
+
+dmhImage *dmhImageRead (FITS_DB *db) {
+
+  if (!db) return FALSE;
+  if (!db->header.buffer) return FALSE;
+
+  dmhImage *history = NULL;
+  ALLOCATE (history, dmhImage, 1);
+
+  // should track the number in the header (would overwrite)
+  int status = gfits_scan (&db->header, "NMERGE", "%d", 1, &history->Nmerge); 
+  if (!status) {
+    history->Nmerge = 0;
+  }
+
+  // ALLOCATE full list
+  ALLOCATE (history->IDs, char *, history->Nmerge);
+
+  int i;
+  char name[16], ID[80];
+  for (i = 0; i < history->Nmerge; i++) {
+    snprintf (name, 16, "DM_%05d", i);
+    status = gfits_scan (&db->header, name, "%s", 1, ID);
+    if (!status) {
+      fprintf (stderr, "failed to read %s\n", name);
+      exit (6);
+    }
+    history->IDs[i] = strcreate (ID);
+  }
+  return history;
+}
+
+// compare stats for the input to the full history for the output
+// have we merged this file?
+int dmhImageCheck (dmhImage *history, char *dbID) {
+
+  // if the input catalog does not exist, we did not miss it
+  // if the output catalog does not exist, we must have missed it
+
+  if (!dbID) return FALSE; // no ID to merge, probably an error to get here
+  if (!history) return FALSE; // we must not have merged since it does not exist
+
+  int i;
+  for (i = 0; i < history->Nmerge; i++) {
+    // do we match this size?
+    if (!strcmp(history->IDs[i], dbID)) return TRUE; // we match (already merged)
+  }
+  return FALSE; // no match, not yet merged
+}
+
+int dmhImageAdd (FITS_DB *db, dmhImage *history, char *dbID) {
+
+  // update the arrays
+  int last = history->Nmerge;
+
+  history->Nmerge ++;
+  REALLOCATE (history->IDs, char *, history->Nmerge);
+
+  history->IDs[last] = strcreate (dbID);
+
+  char name[16];
+  snprintf (name, 16, "DM_%05d", last);
+  gfits_modify (&db->header, name, "%s", 1, dbID);
+
+  gfits_modify (&db->header, "NMERGE", "%d", 1, history->Nmerge);
+  return TRUE;
+}
+
+dmhObjectStats *dmhObjectStatsRead (char *filename) {
+
+  // instats.st_size & instats.st_mtime
+
+  // get the stats on this input file (for comparison with the output headers)
+  struct stat instats;
+  int stat_result = stat (filename, &instats);
+  if (stat_result) {
+    if (errno == ENOENT) return NULL;
+    fprintf (stderr, "cannot read stats on input file %s\n", filename);
+    perror ("stats error message:");
+    exit (2);
+  }
+  
+  dmhObjectStats *stats = NULL;  
+  ALLOCATE (stats, dmhObjectStats, 1);
+  stats->size = instats.st_size;
+  stats->time = instats.st_mtime;
+  stats->date = ohana_sec_to_date (instats.st_mtime);
+  return stats;
+}
+
+void dmhObjectStatsFree (dmhObjectStats *stats) {
+  if (!stats) return;
+  if (stats->date) free (stats->date);
+  free (stats);
+}
+
+// read the array of merged history information from this file's header
+dmhObject *dmhObjectRead (char *filename) {
+
+  // if the file does not exist, return NULL (means 'missed')
+  struct stat outstats;
+  int stat_result = stat (filename, &outstats);
+  if (stat_result) {
+    if (errno == ENOENT) return NULL;
+    fprintf (stderr, "cannot read stats on output file %s\n", filename);
+    perror ("stats error message:");
+    exit (2);
+  }
+
+  FILE *fout = fopen (filename, "r");
+  if (!fout) {
+    fprintf (stderr, "problem opening output file to read header %s\n", filename);
+    perror ("stats error message:");
+    exit (2);
+  }
+
+  Header outheader;
+  if (!gfits_fread_header (fout, &outheader)) {
+    fprintf (stderr, "problem reading header for output file %s\n", filename);
+    exit (2);
+  }
+  fclose (fout);
+
+  dmhObject *history = NULL;
+  ALLOCATE (history, dmhObject, 1);
+
+  int status = gfits_scan (&outheader, "NMERGE", "%d", 1, &history->Nmerge);      
+  if (!status) {
+    history->Nmerge = 0;
+  }
+
+  // ALLOCATE full list
+  ALLOCATE (history->size, off_t,  history->Nmerge);
+  ALLOCATE (history->time, time_t, history->Nmerge);
+  ALLOCATE (history->date, char *, history->Nmerge);
+
+  int i;
+  for (i = 0; i < history->Nmerge; i++) {
+    off_t  size;
+    char   date[80];
+
+    char name[16];
+    
+    snprintf (name, 16, "MS_%05d", i);
+    status = gfits_scan (&outheader, name, OFF_T_FMT, 1, &size);
+    if (!status) {
+      fprintf (stderr, "failed to read %s\n", name);
+      exit (6);
+    }
+    history->size[i] = size;
+
+    snprintf (name, 16, "MT_%05d", i);
+    status = gfits_scan (&outheader, name, "%s", 1, date);
+    if (!status) {
+      fprintf (stderr, "failed to read %s\n", name);
+      exit (7);
+    }
+    history->date[i] = strcreate (date);
+    history->time[i] = ohana_date_to_sec (date);
+  }
+  return history;
+}
+  
+// compare stats for the input to the full history for the output
+// have we merged this file?
+int dmhObjectCheck (dmhObject *history, dmhObjectStats *inStats) {
+
+  // if the input catalog does not exist, we did not miss it
+  // if the output catalog does not exist, we must have missed it
+
+  if (!inStats) return TRUE; // we do not need to merge something that does not exist...
+  if (!history) return FALSE; // we must not have merged since it does not exist
+
+  int i;
+  for (i = 0; i < history->Nmerge; i++) {
+    
+    // do we match this size?
+    if (history->size[i] != inStats->size) continue;
+
+    // do we match this date?
+    if (history->time[i] != inStats->time) continue;
+
+    return TRUE;  // we match (already merged)
+  }
+  return FALSE; // no match, not yet merged
+}
+
+// add the stats for the given input file to the output structure and header
+int dmhObjectAdd (dmhObject *history, Header *header, dmhObjectStats *inStats) {
+
+  // update the arrays
+  int last = history->Nmerge;
+
+  history->Nmerge ++;
+  REALLOCATE (history->size, off_t,  history->Nmerge);
+  REALLOCATE (history->time, time_t, history->Nmerge);
+  REALLOCATE (history->date, char *, history->Nmerge);
+
+  history->size[last] = inStats->size;
+  history->time[last] = inStats->time;
+  history->date[last] = inStats->date;
+
+  char name[16];
+  snprintf (name, 16, "MS_%05d", last);
+
+  int status = gfits_modify (header, name, OFF_T_FMT, 1, inStats->size); 
+  if (!status) { 
+    fprintf (stderr, "error: failed to add size to header (%d)\n", (int) inStats->size);
+    exit (1);
+  }
+
+  snprintf (name, 16, "MT_%05d", last);
+  status = gfits_modify (header, name, "%s", 1, inStats->date);
+  if (!status) {     
+    fprintf (stderr, "error: failed to add date to header (%s)\n", inStats->date);
+    exit (1);
+  }
+
+  gfits_modify (header, "NMERGE", "%d", 1, history->Nmerge);
+  return TRUE;
+}
+
+OutputStatus *OutputStatusInit (int N) {
+
+  int i;
+
+  OutputStatus *outstat = NULL;
+  ALLOCATE (outstat, OutputStatus, N);
+  for (i = 0; i < N; i++) {
+    outstat[i].valid = FALSE;
+    outstat[i].missed = FALSE;
+    outstat[i].history = NULL;
+    outstat[i].filename = NULL;
+  }
+  return outstat;
+}
+
+int OutputStatusFree (OutputStatus *outstat, int N) {
+
+  int i;
+  for (i = 0; i < N; i++) {
+    if (outstat[i].history)  { free (outstat[i].history); }
+    if (outstat[i].filename) { free (outstat[i].filename); }
+  }
+  free (outstat);
+  return TRUE;
+}
Index: trunk/Ohana/src/dvomerge/src/dvomergeImageIDs.c
===================================================================
--- trunk/Ohana/src/dvomerge/src/dvomergeImageIDs.c	(revision 34404)
+++ trunk/Ohana/src/dvomerge/src/dvomergeImageIDs.c	(revision 34405)
@@ -21,7 +21,15 @@
     return TRUE;
   }
+  // this operation reads the PHU header (inDB.header)
   if (!dvo_image_load (&inDB, VERBOSE, TRUE)) {
     Shutdown ("can't read input image catalog %s", inDB.filename);
   }
+  dvo_image_unlock (&inDB); // unlock input
+
+  // read the header for the database ID?
+  char *indbID = dmhImageReadID (&inDB);
+  if (!indbID) { 
+    Shutdown ("this database is missing a DVO database ID; please generate one before merging\n"); 
+  } 
 
   /*** load output/Images.dat ***/
@@ -41,8 +49,27 @@
   }
 
+  dmhImage *history = dmhImageRead (&outDB);
+  if (!history) { 
+    Shutdown ("error reading history for output database\n"); 
+  }
+
+  // have we already merged this database?
+  if (dmhImageCheck(history, indbID)) {
+    // if so, then just match the image IDs 
+    if (VERBOSE) fprintf (stderr, "already merged image table\n");
+    dvo_image_match_dbs(IDmap, &outDB, &inDB);
+    dvo_image_unlock (&outDB); // unlock output
+    return TRUE;
+  }
+
   // convert database table to internal structure & add to output image db
   dvo_image_merge_dbs(IDmap, &outDB, &inDB);
-  dvo_image_unlock (&inDB); // unlock input
 
+  // add the new image db to merge history
+  // (updates header as well as history structure
+  if (!dmhImageAdd (&outDB, history, indbID)) { 
+    Shutdown ("error reading history for output database\n"); 
+  }
+    
   SetProtect (TRUE);
   dvo_image_save (&outDB, VERBOSE);
Index: trunk/Ohana/src/dvomerge/src/dvomergeUpdate_catalogs.c
===================================================================
--- trunk/Ohana/src/dvomerge/src/dvomergeUpdate_catalogs.c	(revision 34404)
+++ trunk/Ohana/src/dvomerge/src/dvomergeUpdate_catalogs.c	(revision 34405)
@@ -31,33 +31,35 @@
     outlist = SkyListByBounds (outsky, -1, inlist[0].regions[i][0].Rmin + dPos, inlist[0].regions[i][0].Rmax - dPos, inlist[0].regions[i][0].Dmin + dPos, inlist[0].regions[i][0].Dmax - dPos);
 
-    // there may be more than one output region for a given input region.  are any of these output regions relevant to this machine?
+    OutputStatus *outstat = OutputStatusInit (outlist->Nregions);
+
+    // there may be more than one output region for a given input region.  
+    // are any of these output regions relevant to this machine?
     int found = FALSE;
-    for (j = 0; !found && (j < outlist[0].Nregions); j++) {
-      found = (found || HostTableTestHost(outlist[0].regions[j], HOST_ID));
+    for (j = 0; j < outlist[0].Nregions; j++) {
+      outstat[j].valid = HostTableTestHost(outlist[0].regions[j], HOST_ID);
+      found = (found || outstat[j].valid);
     }
 
     // skip this input table for if no output files are on this machine
     if (!found) {
+      OutputStatusFree (outstat, outlist->Nregions);
       SkyListFree (outlist); 
       continue; 
     }
 
-    // get the stats on this input file (for comparison with the output headers)
-    struct stat instats;
-    int stat_result = stat (inlist[0].filename[i], &instats);
-    if (stat_result) {
-      if (errno == ENOENT) continue;
-      fprintf (stderr, "cannot read stats on input file %s\n", inlist[0].filename[i]);
-      perror ("stats error message:");
-      exit (2);
-    }
-
-    // instats.st_size & instats.st_mtime
-
-    // check if any of the output files have NOT yet received data from this input file
-
+    // get stats for history check, skip input catalog if file not found (NULL inStats)
+    dmhObjectStats *inStats = dmhObjectStatsRead (inlist[0].filename[i]);
+    if (!inStats) {
+      if (VERBOSE) fprintf (stderr, "skipping %s, empty \n", inlist[0].filename[i]);
+      OutputStatusFree (outstat, outlist->Nregions);
+      SkyListFree (outlist); 
+      continue;
+    }
+
+    // Check if any of the output files have NOT yet received data from this input file
+    // If none have been missed, we can skip the input file completely
     int missed = FALSE;
-    for (j = 0; !missed && (j < outlist[0].Nregions); j++) {
-      if (!HostTableTestHost(outlist[0].regions[j], HOST_ID)) continue;
+    for (j = 0; j < outlist[0].Nregions; j++) {
+      if (!outstat[j].valid) continue;
 
       // set the parameters which guide catalog open/load/create
@@ -65,54 +67,17 @@
       snprintf (hostfile, DVO_MAX_PATH, "%s/%s.cpt", HOSTDIR, outlist[0].regions[j]->name);
       char *filename = HOST_ID ? hostfile : outlist[0].filename[j];
-
-      // get the stats on this input file (for comparison with the output headers)
-      struct stat outstats;
-      stat_result = stat (filename, &outstats);
-      if (stat_result) {
-	if (errno == ENOENT) {
-	  missed = TRUE;
-	  break;
-	}
-	fprintf (stderr, "cannot read stats on output file %s\n", filename);
-	perror ("stats error message:");
-	exit (2);
-      }
-
-      FILE *fout = fopen (filename, "r");
-      if (!fout) {
-	fprintf (stderr, "problem opening output file to read header %s\n", filename);
-	perror ("stats error message:");
-	exit (2);
-      }
-
-      Header outheader;
-      if (!gfits_fread_header (fout, &outheader)) {
-	fprintf (stderr, "problem reading header for output file %s\n", filename);
-	exit (2);
-      }
-      
-      fclose (fout);
-
-      // XXX note that we are hardwired to v 2
-      // check the header of output catalog for an existing merge
-      long long last_size;
-      char last_moddate[80];
-      gfits_scan (&outheader, "LMRG_SZ2", "%lld", 1, &last_size);      
-      gfits_scan (&outheader, "LMRG_DT2", "%s", 1, last_moddate);      
-
-      time_t last_mod = ohana_date_to_sec (last_moddate);
-
-      if (last_size != instats.st_size) {
-	missed = TRUE;
-	break;
-      }
-      
-      if (last_mod != instats.st_mtime) {
-	missed = TRUE;
-	break;
-      }
+      outstat[j].filename = strcreate (filename);
+
+      outstat[j].history = dmhObjectRead (outstat[j].filename);
+
+      // have we already merged this database?
+      outstat[j].missed = !dmhObjectCheck (outstat[j].history, inStats);
+      missed = (missed || outstat[j].missed);
     }
     if (!missed) {
-      fprintf (stderr, "skipping %s, already merged\n", inlist[0].filename[i]);
+      if (VERBOSE) fprintf (stderr, "skipping %s, empty or already merged\n", inlist[0].filename[i]);
+      OutputStatusFree (outstat, outlist->Nregions);
+      dmhObjectStatsFree (inStats);
+      SkyListFree (outlist); 
       continue;
     }
@@ -125,21 +90,22 @@
 	dvo_catalog_unlock (&incatalog);
 	dvo_catalog_free (&incatalog);
+	OutputStatusFree (outstat, outlist->Nregions);
+	dmhObjectStatsFree (inStats);
 	SkyListFree (outlist); 
 	continue;
     }
-
-    char *moddate = ohana_sec_to_date (instats.st_mtime);
 
     // merge input into the appropriate output tables
     for (j = 0; j < outlist[0].Nregions; j++) {
       // skip tables for which the output files are not on this machine
-      if (!HostTableTestHost(outlist[0].regions[j], HOST_ID)) continue; 
+      if (!outstat[j].valid) continue; 
+
+      // skip if we have already done the merge
+      if (!outstat[j].missed) continue; 
 
       if (VERBOSE) fprintf (stderr, "output : %s\n", outlist[0].regions[j][0].name);
 
-      // set the parameters which guide catalog open/load/create
-      char hostfile[DVO_MAX_PATH];
-      snprintf (hostfile, DVO_MAX_PATH, "%s/%s.cpt", HOSTDIR, outlist[0].regions[j]->name);
-      outcatalog.filename  = HOST_ID ? hostfile : outlist[0].filename[j];
+      // the real filename
+      outcatalog.filename = outstat[j].filename; 
 
       // load input catalog
@@ -151,26 +117,5 @@
       outcatalog.catflags = LOAD_AVES | LOAD_MEAS | LOAD_MISS | LOAD_SECF;
 
-# if (0)
-      // get the LMRG data from the previous pass
-      long long last_size;
-      char last_moddate[80];
-      int status_size = gfits_scan (&outcatalog.header, "LMRG_SZ", "%lld", 1, &last_size);      
-      int status_date = gfits_scan (&outcatalog.header, "LMRG_DT", "%s", 1, last_moddate);      
-
-      // save the LMRG data from the previous pass
-      if (status_size && status_date) {
-	gfits_modify (&outcatalog.header, "LMRG_SZ1", "%lld", 1, (long long) last_size);      
-	gfits_modify (&outcatalog.header, "LMRG_DT1", "%s", 1, last_moddate);      
-      }
-
-      // update header of output catalog
-      gfits_modify (&outcatalog.header, "LMRG_SZ", "%lld", 1, (long long) instats.st_size);      
-      gfits_modify (&outcatalog.header, "LMRG_DT", "%s", 1, moddate);      
-# endif
-
-      // update header of output catalog
-      // XXX note that we are hardwired to v 2
-      gfits_modify (&outcatalog.header, "LMRG_SZ2", "%lld", 1, (long long) instats.st_size);      
-      gfits_modify (&outcatalog.header, "LMRG_DT2", "%s", 1, moddate);      
+      dmhObjectAdd (outstat[j].history, &outcatalog.header, inStats);
 
       if (!dvo_catalog_backup (&outcatalog, TRUE)) {
@@ -196,6 +141,8 @@
       fprintf (stderr, "merged %s into %s\n", inlist[0].regions[i][0].name, outlist[0].regions[j][0].name);
     }
+
+    OutputStatusFree (outstat, outlist->Nregions);
     SkyListFree (outlist);
-    free (moddate);
+    dmhObjectStatsFree (inStats);
 
     dvo_catalog_unlock (&incatalog);
Index: trunk/Ohana/src/dvomerge/src/dvoverify.c
===================================================================
--- trunk/Ohana/src/dvomerge/src/dvoverify.c	(revision 34404)
+++ trunk/Ohana/src/dvomerge/src/dvoverify.c	(revision 34405)
@@ -42,4 +42,8 @@
   }
 
+  if (CHECK_IMAGE_ID) {
+    LoadImageIDs (CATDIR);
+  }
+
   // load the sky table for the existing database
   sky = SkyTableLoadOptimal (CATDIR, NULL, NULL, FALSE, SKY_DEPTH_HST, VERBOSE);
Index: trunk/Ohana/src/dvomerge/src/dvoverify_args.c
===================================================================
--- trunk/Ohana/src/dvomerge/src/dvoverify_args.c	(revision 34404)
+++ trunk/Ohana/src/dvomerge/src/dvoverify_args.c	(revision 34405)
@@ -53,4 +53,10 @@
   if ((N = get_argument (*argc, argv, "-skip-toplevel"))) {
     CHECK_TOPLEVEL = FALSE;
+    remove_argument (N, argc, argv);
+  }
+
+  CHECK_IMAGE_ID = TRUE;
+  if ((N = get_argument (*argc, argv, "-skip-image-ids"))) {
+    CHECK_IMAGE_ID = FALSE;
     remove_argument (N, argc, argv);
   }
Index: trunk/Ohana/src/dvomerge/src/dvoverify_catalogs.c
===================================================================
--- trunk/Ohana/src/dvomerge/src/dvoverify_catalogs.c	(revision 34404)
+++ trunk/Ohana/src/dvomerge/src/dvoverify_catalogs.c	(revision 34405)
@@ -100,7 +100,8 @@
 
     char tmpline[DVO_MAX_PATH];
-    if (VERBOSE)      { snprintf (tmpline, DVO_MAX_PATH, "%s -v", command); strcpy (command, tmpline); }
-    if (CHECKSORTED)  { snprintf (tmpline, DVO_MAX_PATH, "%s -s", command); strcpy (command, tmpline); }
-    if (LIST_MISSING) { snprintf (tmpline, DVO_MAX_PATH, "%s -list-missing", command); strcpy (command, tmpline); }
+    if (VERBOSE)         { snprintf (tmpline, DVO_MAX_PATH, "%s -v", command); strcpy (command, tmpline); }
+    if (CHECKSORTED)     { snprintf (tmpline, DVO_MAX_PATH, "%s -s", command); strcpy (command, tmpline); }
+    if (!CHECK_IMAGE_ID) { snprintf (tmpline, DVO_MAX_PATH, "%s -skip-image-ids", command); strcpy (command, tmpline); }
+    if (LIST_MISSING)    { snprintf (tmpline, DVO_MAX_PATH, "%s -list-missing", command); strcpy (command, tmpline); }
 
     fprintf (stderr, "command: %s\n", command);
Index: trunk/Ohana/src/dvomerge/src/dvoverify_client.c
===================================================================
--- trunk/Ohana/src/dvomerge/src/dvoverify_client.c	(revision 34404)
+++ trunk/Ohana/src/dvomerge/src/dvoverify_client.c	(revision 34405)
@@ -19,4 +19,10 @@
   skylist = SkyListByPatch (sky, -1, &UserPatch);
   
+  if (CHECK_IMAGE_ID) {
+    // XXX in client mode, we should be reading a reduced table generated by the calling
+    // serial program
+    LoadImageIDs (CATDIR);
+  }
+
   dvoverify_catalogs (skylist, &Nbad);
 
Index: trunk/Ohana/src/dvomerge/src/dvoverify_utils.c
===================================================================
--- trunk/Ohana/src/dvomerge/src/dvoverify_utils.c	(revision 34404)
+++ trunk/Ohana/src/dvomerge/src/dvoverify_utils.c	(revision 34405)
@@ -49,4 +49,6 @@
 
 // is this file a consistent FITS file?
+// note that VerifyTableFile only has to read the headers,
+// not the data blocks (it uses stat for sizes)
 int VerifyTableFile (char *filename) {
 
@@ -194,5 +196,5 @@
   // \sum average[].Nmeasure = Nmeasure
 
-  // if the table is NOT SORTED, we have a subset of checks we can make
+  // if the table is NOT SORTED, do we have a subset of checks we can make?
   if (!catalog.sorted) {
     fprintf (stderr, "!");
@@ -231,4 +233,6 @@
   }
 
+  // if we have a problem with Nmeasure and/or measureOffset values, we
+  // cannot do any further check -- we risk segfaults
   if (!status) {
     dvo_catalog_unlock (&catalog);
@@ -263,12 +267,12 @@
   }
 
-//  for (i = 0; i < catalog.Naverage; i++) {
-//    m = catalog.average[i].measureOffset;
-//    for (j = 0; i < catalog.Nmeasure; i++) {
-//      objIDsOK &= (catalog.average[i].objID == catalog.measure[m+j].objID);
-//      catIDsOK &= (catalog.average[i].catID == catalog.measure[m+j].catID);
-//      averefOK &= (catalog.measure[m+j].averef = i);
-//    }
-//  }
+  // check the image ID here?
+  if (CHECK_IMAGE_ID) {
+    int Nfail = CheckImageID (&catalog);
+    if (Nfail > 0) {
+      fprintf (stderr, "ERROR: catalog %s has invalid %d unmatched image IDs\n", catalog.filename, Nfail);
+      status = FALSE;
+    }
+  }
 
   dvo_catalog_unlock (&catalog);
@@ -278,6 +282,95 @@
 }
 
-// gfits_scan(&cpmHeaderTBL, "NAXIS1", "%d", 1, &NbytesPerRow);
-// gfits_scan(&cpmHeaderTBL, "NAXIS2", "%d", 1, &Nrows);
-    
+static int maxID = 0;
+static int *IDlist = NULL;
+
+// check that every measure->imageID (if set) matches an existing 
+// image->ID.  return the number of failures.
+int CheckImageID (Catalog *catalog) {
+
+  off_t i, j, m, id;
+  int Nfail = 0;
+
+  for (i = 0; i < catalog[0].Naverage; i++) {
+    m = catalog[0].average[i].measureOffset;
+    for (j = 0; j < catalog[0].average[i].Nmeasure; j++) {
+      id = catalog[0].measure[m+j].imageID;
+      if (id > maxID) {
+	Nfail ++;
+	continue;
+	// is this sufficient to catch IDs set without an image table?
+      }
+      if (IDlist) {
+	if (IDlist[id] < 0) {
+	  Nfail ++;
+	  continue;
+	}
+      } else {
+	if (id > 0) {
+	  Nfail ++;
+	  continue;
+	}
+      }
+    }
+  }
+  return Nfail;
+}
+
+int LoadImageIDs (char *catdir) {
+
+  int status;
+  off_t Nimages, i;
+  Image *images;
+  FITS_DB inDB;
+
+  char ImageCat[DVO_MAX_PATH];
+  snprintf (ImageCat, DVO_MAX_PATH, "%s/Images.dat", catdir);
+
+  // load the iage database table
+  status = dvo_image_lock (&inDB, ImageCat, 3600.0, LCK_SOFT);  // shorter timeout?
+  if (!status) {
+    fprintf (stderr, "ERROR: failure to lock image catalog %s", inDB.filename);
+    exit (3);
+  }
+
+  // load the image table 
+  if (inDB.dbstate == LCK_EMPTY) {
+    dvo_image_unlock (&inDB); // unlock input
+    // this is not an error: we can have no image table for, eg, 2MASS only db
+    return TRUE;
+  }
+  if (!dvo_image_load (&inDB, VERBOSE, TRUE)) {
+    fprintf (stderr, "can't read input image catalog %s", inDB.filename);
+    exit (4);
+  }
+
+  images = gfits_table_get_Image (&inDB.ftable, &Nimages, &inDB.swapped);
+  if (!images) {
+    fprintf (stderr, "ERROR: failed to read images from src\n");
+    exit (2);
+  }
+
+  // generate a lookup table for the images
   
+  // first, find the max imageID
+  for (i = 0; i < Nimages; i++) {
+    maxID = MAX(maxID, images[i].imageID);
+  }
+
+  ALLOCATE (IDlist, int, maxID + 1);
+  for (i = 0; i < maxID + 1; i++) {
+    IDlist[i] = -1;
+  }
+
+  for (i = 0; i < Nimages; i++) {
+    int id = images[i].imageID;
+    IDlist[id] = i;
+  }
+
+  // free image table here?
+  // (in the future, I'll have to do the image table read in segments
+  // it is just getting to be too large...)
+  dvo_image_unlock (&inDB); // unlock input
+
+  return TRUE;
+}
Index: trunk/Ohana/src/dvomerge/src/merge_catalogs_old.c
===================================================================
--- trunk/Ohana/src/dvomerge/src/merge_catalogs_old.c	(revision 34404)
+++ trunk/Ohana/src/dvomerge/src/merge_catalogs_old.c	(revision 34405)
@@ -320,5 +320,5 @@
     output[0].average[Nave].dP        	   = 0;
 
-    output[0].average[Nave].Xp        	   = 0;
+    output[0].average[Nave].stargal   	   = 0;
     output[0].average[Nave].ChiSqAve   	   = 0.0;
     output[0].average[Nave].ChiSqPM   	   = 0.0;
Index: trunk/Ohana/src/dvomerge/test/catdir.grizy/Images.dat
===================================================================
--- trunk/Ohana/src/dvomerge/test/catdir.grizy/Images.dat	(revision 34404)
+++ trunk/Ohana/src/dvomerge/test/catdir.grizy/Images.dat	(revision 34405)
@@ -1,1 +1,1 @@
-SIMPLE  =                    T /                                                BITPIX  =                    8 /                                                NAXIS   =                    0 /                                                PCOUNT  =                    0 /                                                GCOUNT  =                    1 /                                                BSCALE  =         1.0000000000 /                                                BZERO   =         0.0000000000 /                                                EXTEND  =                    T /                                                NIMAGES =                    0 /                                                ZERO_PT =        25.0000000000 /                                                FORMAT  = 'ELIXIR            ' /                                                END                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             XTENSION= 'BINTABLE          ' /                                                BITPIX  =                    8 /                                                NAXIS   =                    2 /                                                NAXIS1  =                  240 /                                                NAXIS2  =                    0 /                                                PCOUNT  =                    0 /                                                GCOUNT  =                    1 /                                                TFIELDS =                   50 /                                                EXTNAME = 'DVO_IMAGE_ELIXIR  ' /                                                TTYPE1  = 'CRVAL1            ' / coordinate at reference pixel                  TUNIT1  = '                  ' /                                                TFORM1  = 'D                 ' /                                                TSCAL1  =         1.0000000000 /                                                TZERO1  =         0.0000000000 /                                                TTYPE2  = 'CRVAL2            ' / coordinate at reference pixel                  TUNIT2  = '                  ' /                                                TFORM2  = 'D                 ' /                                                TSCAL2  =         1.0000000000 /                                                TZERO2  =         0.0000000000 /                                                TTYPE3  = 'CRPIX1            ' / coordinate of reference pixel                  TUNIT3  = '                  ' /                                                TFORM3  = 'E                 ' /                                                TSCAL3  =         1.0000000000 /                                                TZERO3  =         0.0000000000 /                                                TTYPE4  = 'CRPIX2            ' / coordinate of reference pixel                  TUNIT4  = '                  ' /                                                TFORM4  = 'E                 ' /                                                TSCAL4  =         1.0000000000 /                                                TZERO4  =         0.0000000000 /                                                TTYPE5  = 'CDELT1            ' / degrees per pixel                              TUNIT5  = '                  ' /                                                TFORM5  = 'E                 ' /                                                TSCAL5  =         1.0000000000 /                                                TZERO5  =         0.0000000000 /                                                TTYPE6  = 'CDELT2            ' / degrees per pixel                              TUNIT6  = '                  ' /                                                TFORM6  = 'E                 ' /                                                TSCAL6  =         1.0000000000 /                                                TZERO6  =         0.0000000000 /                                                TTYPE7  = 'PC1_1             ' / rotation matrix                                TUNIT7  = '                  ' /                                                TFORM7  = 'E                 ' /                                                TSCAL7  =         1.0000000000 /                                                TZERO7  =         0.0000000000 /                                                TTYPE8  = 'PC1_2             ' / rotation matrix                                TUNIT8  = '                  ' /                                                TFORM8  = 'E                 ' /                                                TSCAL8  =         1.0000000000 /                                                TZERO8  =         0.0000000000 /                                                TTYPE9  = 'PC2_1             ' / rotation matrix                                TUNIT9  = '                  ' /                                                TFORM9  = 'E                 ' /                                                TSCAL9  =         1.0000000000 /                                                TZERO9  =         0.0000000000 /                                                TTYPE10 = 'PC2_2             ' / rotation matrix                                TUNIT10 = '                  ' /                                                TFORM10 = 'E                 ' /                                                TSCAL10 =         1.0000000000 /                                                TZERO10 =         0.0000000000 /                                                TTYPE11 = 'POLYTERMS         ' / higher order warping terms                     TUNIT11 = '                  ' /                                                TFORM11 = '14E               ' /                                                TSCAL11 =         1.0000000000 /                                                TZERO11 =         0.0000000000 /                                                TTYPE12 = 'CTYPE             ' / coordinate type                                TUNIT12 = '                  ' /                                                TFORM12 = '15A               ' /                                                TSCAL12 =         1.0000000000 /                                                TZERO12 =         0.0000000000 /                                                TTYPE13 = 'NPOLYTERMS        ' / order of polynomial                            TUNIT13 = '                  ' /                                                TFORM13 = 'A                 ' /                                                TSCAL13 =         1.0000000000 /                                                TZERO13 =         0.0000000000 /                                                TTYPE14 = 'TZERO             ' / readout time (row 0)                           TUNIT14 = '                  ' /                                                TFORM14 = 'J                 ' /                                                TSCAL14 =         1.0000000000 /                                                TZERO14 =         0.0000000000 /                                                TTYPE15 = 'NSTAR             ' / number of stars on image                       TUNIT15 = '                  ' /                                                TFORM15 = 'J                 ' /                                                TSCAL15 =         1.0000000000 /                                                TZERO15 =         0.0000000000 /                                                TTYPE16 = 'SECZ              ' / airmass                                        TUNIT16 = 'milliairmass      ' /                                                TFORM16 = 'I                 ' /                                                TSCAL16 =         1.0000000000 /                                                TZERO16 =         0.0000000000 /                                                TTYPE17 = 'NX                ' / image width                                    TUNIT17 = '                  ' /                                                TFORM17 = 'I                 ' /                                                TSCAL17 =         1.0000000000 /                                                TZERO17 =         0.0000000000 /                                                TTYPE18 = 'NY                ' / image height                                   TUNIT18 = '                  ' /                                                TFORM18 = 'I                 ' /                                                TSCAL18 =         1.0000000000 /                                                TZERO18 =         0.0000000000 /                                                TTYPE19 = 'APMIFIT           ' / aperture correction                            TUNIT19 = 'millimag          ' /                                                TFORM19 = 'I                 ' /                                                TSCAL19 =         1.0000000000 /                                                TZERO19 =         0.0000000000 /                                                TTYPE20 = 'DAPMIFIT          ' / apmifit error                                  TUNIT20 = 'millimag          ' /                                                TFORM20 = 'I                 ' /                                                TSCAL20 =         1.0000000000 /                                                TZERO20 =         0.0000000000 /                                                TTYPE21 = 'SOURCE            ' / identifier for CCD,                            TUNIT21 = '                  ' /                                                TFORM21 = 'I                 ' /                                                TSCAL21 =         1.0000000000 /                                                TZERO21 =         0.0000000000 /                                                TTYPE22 = 'MCAL              ' / calibration mag                                TUNIT22 = 'millimag          ' /                                                TFORM22 = 'I                 ' /                                                TSCAL22 =         1.0000000000 /                                                TZERO22 =         0.0000000000 /                                                TTYPE23 = 'DMCAL             ' / error on Mcal                                  TUNIT23 = 'millimag          ' /                                                TFORM23 = 'I                 ' /                                                TSCAL23 =         1.0000000000 /                                                TZERO23 =         0.0000000000 /                                                TTYPE24 = 'XM                ' / image chisq                                    TUNIT24 = '10*log(value)     ' /                                                TFORM24 = 'I                 ' /                                                TSCAL24 =         1.0000000000 /                                                TZERO24 =         0.0000000000 /                                                TTYPE25 = 'NAME              ' / name of original image                         TUNIT25 = '                  ' /                                                TFORM25 = '32A               ' /                                                TSCAL25 =         1.0000000000 /                                                TZERO25 =         0.0000000000 /                                                TTYPE26 = 'DETECTION_LIMIT   ' / detection limit                                TUNIT26 = '10*mag            ' /                                                TFORM26 = 'B                 ' /                                                TSCAL26 =         1.0000000000 /                                                TZERO26 =         0.0000000000 /                                                TTYPE27 = 'SATURATION_LIMIT  ' / saturation limit                               TUNIT27 = '10*mag            ' /                                                TFORM27 = 'B                 ' /                                                TSCAL27 =         1.0000000000 /                                                TZERO27 =         0.0000000000 /                                                TTYPE28 = 'CERROR            ' / astrometric error                              TUNIT28 = '50*arcsec         ' /                                                TFORM28 = 'B                 ' /                                                TSCAL28 =         1.0000000000 /                                                TZERO28 =         0.0000000000 /                                                TTYPE29 = 'FWHM_X            ' / PSF x width                                    TUNIT29 = '25*arcsec         ' /                                                TFORM29 = 'B                 ' /                                                TSCAL29 =         1.0000000000 /                                                TZERO29 =         0.0000000000 /                                                TTYPE30 = 'FWHM_Y            ' / PSF y width                                    TUNIT30 = '25*arcsec         ' /                                                TFORM30 = 'B                 ' /                                                TSCAL30 =         1.0000000000 /                                                TZERO30 =         0.0000000000 /                                                TTYPE31 = 'TRATE             ' / scan rate                                      TUNIT31 = '100 usec/pixel    ' /                                                TFORM31 = 'B                 ' /                                                TSCAL31 =         1.0000000000 /                                                TZERO31 =         0.0000000000 /                                                TTYPE32 = 'EXPTIME           ' / exposure time                                  TUNIT32 = 'seconds           ' /                                                TFORM32 = 'E                 ' /                                                TSCAL32 =         1.0000000000 /                                                TZERO32 =         0.0000000000 /                                                TTYPE33 = 'CODE              ' / image quality flag                             TUNIT33 = '                  ' /                                                TFORM33 = 'A                 ' /                                                TSCAL33 =         1.0000000000 /                                                TZERO33 =         0.0000000000 /                                                TTYPE34 = 'CCDNUM            ' / CCD ID number                                  TUNIT34 = '                  ' /                                                TFORM34 = 'B                 ' /                                                TSCAL34 =         1.0000000000 /                                                TZERO34 =         0.0000000000 /                                                TTYPE35 = 'DUMMY             ' / unused                                         TUNIT35 = '                  ' /                                                TFORM35 = '20A               ' /                                                TSCAL35 =         1.0000000000 /                                                TZERO35 =         0.0000000000 /                                                TTYPE36 = 'ORDER             ' / Mrel 2D polynomical order                      TUNIT36 = '                  ' /                                                TFORM36 = 'I                 ' /                                                TSCAL36 =         1.0000000000 /                                                TZERO36 =         0.0000000000 /                                                TTYPE37 = 'MX                ' / Mrel polyterm                                  TUNIT37 = '                  ' /                                                TFORM37 = 'I                 ' /                                                TSCAL37 =         1.0000000000 /                                                TZERO37 =         0.0000000000 /                                                TTYPE38 = 'MY                ' / Mrel polyterm                                  TUNIT38 = '                  ' /                                                TFORM38 = 'I                 ' /                                                TSCAL38 =         1.0000000000 /                                                TZERO38 =         0.0000000000 /                                                TTYPE39 = 'MXX               ' / Mrel polyterm                                  TUNIT39 = '                  ' /                                                TFORM39 = 'I                 ' /                                                TSCAL39 =         1.0000000000 /                                                TZERO39 =         0.0000000000 /                                                TTYPE40 = 'MXY               ' / Mrel polyterm                                  TUNIT40 = '                  ' /                                                TFORM40 = 'I                 ' /                                                TSCAL40 =         1.0000000000 /                                                TZERO40 =         0.0000000000 /                                                TTYPE41 = 'MYY               ' / Mrel polyterm                                  TUNIT41 = '                  ' /                                                TFORM41 = 'I                 ' /                                                TSCAL41 =         1.0000000000 /                                                TZERO41 =         0.0000000000 /                                                TTYPE42 = 'MXXX              ' / Mrel polyterm                                  TUNIT42 = '                  ' /                                                TFORM42 = 'I                 ' /                                                TSCAL42 =         1.0000000000 /                                                TZERO42 =         0.0000000000 /                                                TTYPE43 = 'MXXY              ' / Mrel polyterm                                  TUNIT43 = '                  ' /                                                TFORM43 = 'I                 ' /                                                TSCAL43 =         1.0000000000 /                                                TZERO43 =         0.0000000000 /                                                TTYPE44 = 'MXYY              ' / Mrel polyterm                                  TUNIT44 = '                  ' /                                                TFORM44 = 'I                 ' /                                                TSCAL44 =         1.0000000000 /                                                TZERO44 =         0.0000000000 /                                                TTYPE45 = 'MYYY              ' / Mrel polyterm                                  TUNIT45 = '                  ' /                                                TFORM45 = 'I                 ' /                                                TSCAL45 =         1.0000000000 /                                                TZERO45 =         0.0000000000 /                                                TTYPE46 = 'MXXXX             ' / Mrel polyterm                                  TUNIT46 = '                  ' /                                                TFORM46 = 'I                 ' /                                                TSCAL46 =         1.0000000000 /                                                TZERO46 =         0.0000000000 /                                                TTYPE47 = 'MXXXY             ' / Mrel polyterm                                  TUNIT47 = '                  ' /                                                TFORM47 = 'I                 ' /                                                TSCAL47 =         1.0000000000 /                                                TZERO47 =         0.0000000000 /                                                TTYPE48 = 'MXXYY             ' / Mrel polyterm                                  TUNIT48 = '                  ' /                                                TFORM48 = 'I                 ' /                                                TSCAL48 =         1.0000000000 /                                                TZERO48 =         0.0000000000 /                                                TTYPE49 = 'MXYYY             ' / Mrel polyterm                                  TUNIT49 = '                  ' /                                                TFORM49 = 'I                 ' /                                                TSCAL49 =         1.0000000000 /                                                TZERO49 =         0.0000000000 /                                                TTYPE50 = 'MYYYY             ' / Mrel polyterm                                  TUNIT50 = '                  ' /                                                TFORM50 = 'I                 ' /                                                TSCAL50 =         1.0000000000 /                                                TZERO50 =         0.0000000000 /                                                END                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
+SIMPLE  =                    T /                                                BITPIX  =                    8 /                                                NAXIS   =                    0 /                                                PCOUNT  =                    0 /                                                GCOUNT  =                    1 /                                                BSCALE  =         1.0000000000 /                                                BZERO   =         0.0000000000 /                                                EXTEND  =                    T /                                                NIMAGES =                    0 /                                                ZERO_PT =        25.0000000000 /                                                FORMAT  = 'ELIXIR            ' /                                                DVO_DBID= 'c2fa2cdbac0df952ae954ed04381e219' /                                  END                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             XTENSION= 'BINTABLE          ' /                                                BITPIX  =                    8 /                                                NAXIS   =                    2 /                                                NAXIS1  =                  240 /                                                NAXIS2  =                    0 /                                                PCOUNT  =                    0 /                                                GCOUNT  =                    1 /                                                TFIELDS =                   50 /                                                EXTNAME = 'DVO_IMAGE_ELIXIR  ' /                                                TTYPE1  = 'CRVAL1            ' / coordinate at reference pixel                  TUNIT1  = '                  ' /                                                TFORM1  = 'D                 ' /                                                TSCAL1  =         1.0000000000 /                                                TZERO1  =         0.0000000000 /                                                TTYPE2  = 'CRVAL2            ' / coordinate at reference pixel                  TUNIT2  = '                  ' /                                                TFORM2  = 'D                 ' /                                                TSCAL2  =         1.0000000000 /                                                TZERO2  =         0.0000000000 /                                                TTYPE3  = 'CRPIX1            ' / coordinate of reference pixel                  TUNIT3  = '                  ' /                                                TFORM3  = 'E                 ' /                                                TSCAL3  =         1.0000000000 /                                                TZERO3  =         0.0000000000 /                                                TTYPE4  = 'CRPIX2            ' / coordinate of reference pixel                  TUNIT4  = '                  ' /                                                TFORM4  = 'E                 ' /                                                TSCAL4  =         1.0000000000 /                                                TZERO4  =         0.0000000000 /                                                TTYPE5  = 'CDELT1            ' / degrees per pixel                              TUNIT5  = '                  ' /                                                TFORM5  = 'E                 ' /                                                TSCAL5  =         1.0000000000 /                                                TZERO5  =         0.0000000000 /                                                TTYPE6  = 'CDELT2            ' / degrees per pixel                              TUNIT6  = '                  ' /                                                TFORM6  = 'E                 ' /                                                TSCAL6  =         1.0000000000 /                                                TZERO6  =         0.0000000000 /                                                TTYPE7  = 'PC1_1             ' / rotation matrix                                TUNIT7  = '                  ' /                                                TFORM7  = 'E                 ' /                                                TSCAL7  =         1.0000000000 /                                                TZERO7  =         0.0000000000 /                                                TTYPE8  = 'PC1_2             ' / rotation matrix                                TUNIT8  = '                  ' /                                                TFORM8  = 'E                 ' /                                                TSCAL8  =         1.0000000000 /                                                TZERO8  =         0.0000000000 /                                                TTYPE9  = 'PC2_1             ' / rotation matrix                                TUNIT9  = '                  ' /                                                TFORM9  = 'E                 ' /                                                TSCAL9  =         1.0000000000 /                                                TZERO9  =         0.0000000000 /                                                TTYPE10 = 'PC2_2             ' / rotation matrix                                TUNIT10 = '                  ' /                                                TFORM10 = 'E                 ' /                                                TSCAL10 =         1.0000000000 /                                                TZERO10 =         0.0000000000 /                                                TTYPE11 = 'POLYTERMS         ' / higher order warping terms                     TUNIT11 = '                  ' /                                                TFORM11 = '14E               ' /                                                TSCAL11 =         1.0000000000 /                                                TZERO11 =         0.0000000000 /                                                TTYPE12 = 'CTYPE             ' / coordinate type                                TUNIT12 = '                  ' /                                                TFORM12 = '15A               ' /                                                TSCAL12 =         1.0000000000 /                                                TZERO12 =         0.0000000000 /                                                TTYPE13 = 'NPOLYTERMS        ' / order of polynomial                            TUNIT13 = '                  ' /                                                TFORM13 = 'A                 ' /                                                TSCAL13 =         1.0000000000 /                                                TZERO13 =         0.0000000000 /                                                TTYPE14 = 'TZERO             ' / readout time (row 0)                           TUNIT14 = '                  ' /                                                TFORM14 = 'J                 ' /                                                TSCAL14 =         1.0000000000 /                                                TZERO14 =         0.0000000000 /                                                TTYPE15 = 'NSTAR             ' / number of stars on image                       TUNIT15 = '                  ' /                                                TFORM15 = 'J                 ' /                                                TSCAL15 =         1.0000000000 /                                                TZERO15 =         0.0000000000 /                                                TTYPE16 = 'SECZ              ' / airmass                                        TUNIT16 = 'milliairmass      ' /                                                TFORM16 = 'I                 ' /                                                TSCAL16 =         1.0000000000 /                                                TZERO16 =         0.0000000000 /                                                TTYPE17 = 'NX                ' / image width                                    TUNIT17 = '                  ' /                                                TFORM17 = 'I                 ' /                                                TSCAL17 =         1.0000000000 /                                                TZERO17 =         0.0000000000 /                                                TTYPE18 = 'NY                ' / image height                                   TUNIT18 = '                  ' /                                                TFORM18 = 'I                 ' /                                                TSCAL18 =         1.0000000000 /                                                TZERO18 =         0.0000000000 /                                                TTYPE19 = 'APMIFIT           ' / aperture correction                            TUNIT19 = 'millimag          ' /                                                TFORM19 = 'I                 ' /                                                TSCAL19 =         1.0000000000 /                                                TZERO19 =         0.0000000000 /                                                TTYPE20 = 'DAPMIFIT          ' / apmifit error                                  TUNIT20 = 'millimag          ' /                                                TFORM20 = 'I                 ' /                                                TSCAL20 =         1.0000000000 /                                                TZERO20 =         0.0000000000 /                                                TTYPE21 = 'SOURCE            ' / identifier for CCD,                            TUNIT21 = '                  ' /                                                TFORM21 = 'I                 ' /                                                TSCAL21 =         1.0000000000 /                                                TZERO21 =         0.0000000000 /                                                TTYPE22 = 'MCAL              ' / calibration mag                                TUNIT22 = 'millimag          ' /                                                TFORM22 = 'I                 ' /                                                TSCAL22 =         1.0000000000 /                                                TZERO22 =         0.0000000000 /                                                TTYPE23 = 'DMCAL             ' / error on Mcal                                  TUNIT23 = 'millimag          ' /                                                TFORM23 = 'I                 ' /                                                TSCAL23 =         1.0000000000 /                                                TZERO23 =         0.0000000000 /                                                TTYPE24 = 'XM                ' / image chisq                                    TUNIT24 = '10*log(value)     ' /                                                TFORM24 = 'I                 ' /                                                TSCAL24 =         1.0000000000 /                                                TZERO24 =         0.0000000000 /                                                TTYPE25 = 'NAME              ' / name of original image                         TUNIT25 = '                  ' /                                                TFORM25 = '32A               ' /                                                TSCAL25 =         1.0000000000 /                                                TZERO25 =         0.0000000000 /                                                TTYPE26 = 'DETECTION_LIMIT   ' / detection limit                                TUNIT26 = '10*mag            ' /                                                TFORM26 = 'B                 ' /                                                TSCAL26 =         1.0000000000 /                                                TZERO26 =         0.0000000000 /                                                TTYPE27 = 'SATURATION_LIMIT  ' / saturation limit                               TUNIT27 = '10*mag            ' /                                                TFORM27 = 'B                 ' /                                                TSCAL27 =         1.0000000000 /                                                TZERO27 =         0.0000000000 /                                                TTYPE28 = 'CERROR            ' / astrometric error                              TUNIT28 = '50*arcsec         ' /                                                TFORM28 = 'B                 ' /                                                TSCAL28 =         1.0000000000 /                                                TZERO28 =         0.0000000000 /                                                TTYPE29 = 'FWHM_X            ' / PSF x width                                    TUNIT29 = '25*arcsec         ' /                                                TFORM29 = 'B                 ' /                                                TSCAL29 =         1.0000000000 /                                                TZERO29 =         0.0000000000 /                                                TTYPE30 = 'FWHM_Y            ' / PSF y width                                    TUNIT30 = '25*arcsec         ' /                                                TFORM30 = 'B                 ' /                                                TSCAL30 =         1.0000000000 /                                                TZERO30 =         0.0000000000 /                                                TTYPE31 = 'TRATE             ' / scan rate                                      TUNIT31 = '100 usec/pixel    ' /                                                TFORM31 = 'B                 ' /                                                TSCAL31 =         1.0000000000 /                                                TZERO31 =         0.0000000000 /                                                TTYPE32 = 'EXPTIME           ' / exposure time                                  TUNIT32 = 'seconds           ' /                                                TFORM32 = 'E                 ' /                                                TSCAL32 =         1.0000000000 /                                                TZERO32 =         0.0000000000 /                                                TTYPE33 = 'CODE              ' / image quality flag                             TUNIT33 = '                  ' /                                                TFORM33 = 'A                 ' /                                                TSCAL33 =         1.0000000000 /                                                TZERO33 =         0.0000000000 /                                                TTYPE34 = 'CCDNUM            ' / CCD ID number                                  TUNIT34 = '                  ' /                                                TFORM34 = 'B                 ' /                                                TSCAL34 =         1.0000000000 /                                                TZERO34 =         0.0000000000 /                                                TTYPE35 = 'DUMMY             ' / unused                                         TUNIT35 = '                  ' /                                                TFORM35 = '20A               ' /                                                TSCAL35 =         1.0000000000 /                                                TZERO35 =         0.0000000000 /                                                TTYPE36 = 'ORDER             ' / Mrel 2D polynomical order                      TUNIT36 = '                  ' /                                                TFORM36 = 'I                 ' /                                                TSCAL36 =         1.0000000000 /                                                TZERO36 =         0.0000000000 /                                                TTYPE37 = 'MX                ' / Mrel polyterm                                  TUNIT37 = '                  ' /                                                TFORM37 = 'I                 ' /                                                TSCAL37 =         1.0000000000 /                                                TZERO37 =         0.0000000000 /                                                TTYPE38 = 'MY                ' / Mrel polyterm                                  TUNIT38 = '                  ' /                                                TFORM38 = 'I                 ' /                                                TSCAL38 =         1.0000000000 /                                                TZERO38 =         0.0000000000 /                                                TTYPE39 = 'MXX               ' / Mrel polyterm                                  TUNIT39 = '                  ' /                                                TFORM39 = 'I                 ' /                                                TSCAL39 =         1.0000000000 /                                                TZERO39 =         0.0000000000 /                                                TTYPE40 = 'MXY               ' / Mrel polyterm                                  TUNIT40 = '                  ' /                                                TFORM40 = 'I                 ' /                                                TSCAL40 =         1.0000000000 /                                                TZERO40 =         0.0000000000 /                                                TTYPE41 = 'MYY               ' / Mrel polyterm                                  TUNIT41 = '                  ' /                                                TFORM41 = 'I                 ' /                                                TSCAL41 =         1.0000000000 /                                                TZERO41 =         0.0000000000 /                                                TTYPE42 = 'MXXX              ' / Mrel polyterm                                  TUNIT42 = '                  ' /                                                TFORM42 = 'I                 ' /                                                TSCAL42 =         1.0000000000 /                                                TZERO42 =         0.0000000000 /                                                TTYPE43 = 'MXXY              ' / Mrel polyterm                                  TUNIT43 = '                  ' /                                                TFORM43 = 'I                 ' /                                                TSCAL43 =         1.0000000000 /                                                TZERO43 =         0.0000000000 /                                                TTYPE44 = 'MXYY              ' / Mrel polyterm                                  TUNIT44 = '                  ' /                                                TFORM44 = 'I                 ' /                                                TSCAL44 =         1.0000000000 /                                                TZERO44 =         0.0000000000 /                                                TTYPE45 = 'MYYY              ' / Mrel polyterm                                  TUNIT45 = '                  ' /                                                TFORM45 = 'I                 ' /                                                TSCAL45 =         1.0000000000 /                                                TZERO45 =         0.0000000000 /                                                TTYPE46 = 'MXXXX             ' / Mrel polyterm                                  TUNIT46 = '                  ' /                                                TFORM46 = 'I                 ' /                                                TSCAL46 =         1.0000000000 /                                                TZERO46 =         0.0000000000 /                                                TTYPE47 = 'MXXXY             ' / Mrel polyterm                                  TUNIT47 = '                  ' /                                                TFORM47 = 'I                 ' /                                                TSCAL47 =         1.0000000000 /                                                TZERO47 =         0.0000000000 /                                                TTYPE48 = 'MXXYY             ' / Mrel polyterm                                  TUNIT48 = '                  ' /                                                TFORM48 = 'I                 ' /                                                TSCAL48 =         1.0000000000 /                                                TZERO48 =         0.0000000000 /                                                TTYPE49 = 'MXYYY             ' / Mrel polyterm                                  TUNIT49 = '                  ' /                                                TFORM49 = 'I                 ' /                                                TSCAL49 =         1.0000000000 /                                                TZERO49 =         0.0000000000 /                                                TTYPE50 = 'MYYYY             ' / Mrel polyterm                                  TUNIT50 = '                  ' /                                                TFORM50 = 'I                 ' /                                                TSCAL50 =         1.0000000000 /                                                TZERO50 =         0.0000000000 /                                                END                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
Index: trunk/Ohana/src/libautocode/Makefile.Targets
===================================================================
--- trunk/Ohana/src/libautocode/Makefile.Targets	(revision 34404)
+++ trunk/Ohana/src/libautocode/Makefile.Targets	(revision 34405)
@@ -70,5 +70,7 @@
 $(ASRC)/cmf-ps1-v2.$(ARCH).o \
 $(ASRC)/cmf-ps1-v3.$(ARCH).o \
+$(ASRC)/cmf-ps1-v4.$(ARCH).o \
 $(ASRC)/cmf-ps1-sv1.$(ARCH).o \
+$(ASRC)/cmf-ps1-sv2.$(ARCH).o \
 $(ASRC)/cmf-smpdata.$(ARCH).o \
 $(ASRC)/getstar-ps1-dev-0.$(ARCH).o \
@@ -154,5 +156,7 @@
 $(AINC)/cmf-ps1-v2.h \
 $(AINC)/cmf-ps1-v3.h \
+$(AINC)/cmf-ps1-v4.h \
 $(AINC)/cmf-ps1-sv1.h \
+$(AINC)/cmf-ps1-sv2.h \
 $(AINC)/cmf-smpdata.h \
 $(AINC)/getstar-ps1-dev-0.h \
Index: trunk/Ohana/src/libautocode/def/Stars.d
===================================================================
--- trunk/Ohana/src/libautocode/def/Stars.d	(revision 34404)
+++ trunk/Ohana/src/libautocode/def/Stars.d	(revision 34405)
@@ -1,2 +1,5 @@
+*** note that this file / structure has been deprecated
+
+
 STRUCT  Stars
 EXTNAME STARS
Index: trunk/Ohana/src/libautocode/def/average-ps1-v4.d
===================================================================
--- trunk/Ohana/src/libautocode/def/average-ps1-v4.d	(revision 34404)
+++ trunk/Ohana/src/libautocode/def/average-ps1-v4.d	(revision 34405)
@@ -2,5 +2,5 @@
 EXTNAME      DVO_AVERAGE_PS1_V4
 TYPE         BINTABLE
-SIZE         120
+SIZE         128
 DESCRIPTION  DVO Average Object Table
 
@@ -25,5 +25,8 @@
 FIELD Trange,         TIME_RANGE,  int,   	    mean epoch (PM,PAR ref),       unix time seconds
 
-FIELD Xp,             SIGMA_POS,   float, 	    position scatter,   	  1/100 arcsec
+FIELD psfQF,          PSF_QF,      float,           psf coverage (bad masks)
+FIELD psfQFperf,      PSF_QF_PERF, float,           psf coverage (all masks)
+
+FIELD stargal,        STARGAL_SEP, float,           star / galaxy separator,       1/100 arcsec
 FIELD Npos,           NUMBER_POS,  unsigned short,  number of detections used for astrometry
 
Index: trunk/Ohana/src/libautocode/def/average.d
===================================================================
--- trunk/Ohana/src/libautocode/def/average.d	(revision 34404)
+++ trunk/Ohana/src/libautocode/def/average.d	(revision 34405)
@@ -2,5 +2,5 @@
 EXTNAME      DVO_AVERAGE
 TYPE         BINTABLE
-SIZE         120
+SIZE         128
 DESCRIPTION  DVO Average Object Table
 
@@ -25,5 +25,8 @@
 FIELD Trange,         TIME_RANGE,  int,   	    mean epoch (PM,PAR ref),       unix time seconds
 
-FIELD Xp,             SIGMA_POS,   float,           position scatter,             1/100 arcsec
+FIELD psfQF,          PSF_QF,      float,           psf coverage (bad masks)
+FIELD psfQFperf,      PSF_QF_PERF, float,           psf coverage (all masks)
+
+FIELD stargal,        STARGAL_SEP, float,           star / galaxy separator,       1/100 arcsec
 FIELD Npos,           NUMBER_POS,  unsigned short,  number of detections used for astrometry
 
Index: trunk/Ohana/src/libautocode/def/cmf-ps1-dev-0.d
===================================================================
--- trunk/Ohana/src/libautocode/def/cmf-ps1-dev-0.d	(revision 34404)
+++ trunk/Ohana/src/libautocode/def/cmf-ps1-dev-0.d	(revision 34405)
@@ -20,5 +20,5 @@
 FIELD fy,     	PSF_WIDTH_Y,      float,    semi-minor,           pixels
 FIELD df,     	PSF_THETA,        float,    ellipse angle,        degrees
-FIELD psfQual, 	PSF_QF,           float,    quality factor
+FIELD psfQF, 	PSF_QF,           float,    quality factor
 FIELD nFrames, 	N_FRAMES,         short,    images overlapping peak
 FIELD dummy,  	DUMMY,            short,    padding
Index: trunk/Ohana/src/libautocode/def/cmf-ps1-dev-1.d
===================================================================
--- trunk/Ohana/src/libautocode/def/cmf-ps1-dev-1.d	(revision 34404)
+++ trunk/Ohana/src/libautocode/def/cmf-ps1-dev-1.d	(revision 34405)
@@ -22,5 +22,5 @@
 FIELD fy,     	 PSF_WIDTH_Y,      float,    semi-minor,           pixels
 FIELD df,     	 PSF_THETA,        float,    ellipse angle,        degrees
-FIELD psfQual, 	 PSF_QF,           float,    quality factor
+FIELD psfQF, 	 PSF_QF,           float,    quality factor
 FIELD nFrames, 	 N_FRAMES,         short,    images overlapping peak
 FIELD flags,  	 FLAGS,            short,    padding
Index: trunk/Ohana/src/libautocode/def/cmf-ps1-sv1.d
===================================================================
--- trunk/Ohana/src/libautocode/def/cmf-ps1-sv1.d	(revision 34404)
+++ trunk/Ohana/src/libautocode/def/cmf-ps1-sv1.d	(revision 34405)
@@ -51,6 +51,6 @@
 
 FIELD df,             PSF_THETA,         float,    ellipse angle,         degrees
-FIELD psfQual,        PSF_QF,            float,    quality factor
-FIELD psfQualPerfect, PSF_QF_PERFECT,    float,    quality factor perfect
+FIELD psfQF,          PSF_QF,            float,    quality factor
+FIELD psfQFperf,      PSF_QF_PERFECT,    float,    quality factor perfect
 FIELD psfNdof,        PSF_NDOF,          int,      psf degrees of freedom
 
@@ -112,6 +112,6 @@
 # FIELD fy,             PSF_MINOR,         PSF_MINOR          1E      float,    psf fit minor axis,    pixels
 # FIELD df,             PSF_THETA,         PSF_THETA          1E      float,    ellipse angle,         degrees
-# FIELD psfQual,        PSF_QF,            PSF_QF             1E      float,    quality factor
-# FIELD psfQualPerfect, PSF_QF_PERFECT,    PSF_QF_PERFECT     1E      float,    quality factor perfect
+# FIELD psfQF,          PSF_QF,            PSF_QF             1E      float,    quality factor
+# FIELD psfQFperf,      PSF_QF_PERFECT,    PSF_QF_PERFECT     1E      float,    quality factor perfect
 # FIELD psfNdof,        PSF_NDOF,          PSF_NDOF           1J      int,      psf degrees of freedom
 # FIELD psfNpix,        PSF_NPIX,          PSF_NPIX           1J      int,      psf number of pixels
Index: trunk/Ohana/src/libautocode/def/cmf-ps1-sv2.d
===================================================================
--- trunk/Ohana/src/libautocode/def/cmf-ps1-sv2.d	(revision 34405)
+++ trunk/Ohana/src/libautocode/def/cmf-ps1-sv2.d	(revision 34405)
@@ -0,0 +1,140 @@
+# name of structure type
+STRUCT  CMF_PS1_SV2
+EXTNAME CMF_PS1_SV2
+TYPE    BINTABLE
+SIZE    208
+
+## note that this definition is inconsistent with the definition in
+## pmModules/src/objects/pmSourceIO_CMF.c.in.  The version in
+## pmSourceIO*.c creates a table with data not properly aligned with
+## the 8-byte boundaries. The structure defined by libautocode does
+## this, but has a different order of elements (and adds padding2 to
+## fix things). We have generated may files with PS1_SV1 as is, so
+## I'll leave it. But in future a PS1_SV2 should be forced to match
+## libautocode. Note that addstar knows to detect the alternate
+## version of PS1_SV1 and correctly interpret its fields.
+
+# elements of data structure / FITS table
+FIELD detID,          IPP_IDET,          unsigned int, detection ID                     
+FIELD X,              X_PSF,             float,    x coord,               pixels
+FIELD Y,              Y_PSF,             float,    y coord,               pixels
+FIELD dX,             X_PSF_SIG,         float,    x coord error,         pixels
+
+FIELD dY,             Y_PSF_SIG,         float,    y coord error,         pixels
+FIELD posangle,       POSANGLE,          float,    Posangle at source,    degrees
+FIELD pltscale,       PLTSCALE,          float,    Plate Scale at source, arcsec/pixel
+FIELD M,              PSF_INST_MAG,      float,    inst mags,             mags
+
+FIELD dM,             PSF_INST_MAG_SIG,  float,    inst mag error,        mags
+FIELD Flux,           PSF_INST_FLUX,     float,    psf flux,	       counts
+FIELD dFlux,          PSF_INST_FLUX_SIG, float,    psf flux error,        counts      
+FIELD Map,            AP_MAG_STANDARD,   float,    standard aperture mag, mags
+
+FIELD MapRaw,         AP_MAG_RAW,        float,    raw aperture mag,      mags
+FIELD apRadius,       AP_MAG_RADIUS,     float,    radius used for fit,   pixels
+FIELD apFlux,         AP_FLUX,           float,    aperture flux,         counts
+FIELD apFluxErr,      AP_FLUX_SIG,       float,    error on ap flux,      counts
+
+FIELD Mcalib,         CAL_PSF_MAG,       float,    calibrated psf mag,    mags
+FIELD dMcal,          CAL_PSF_MAG_SIG,   float,    zero point scatter,    mags
+
+# NOTE: RA & DEC (both double) need to be on an 8-byte boundary...
+FIELD RA,             RA_PSF,            double,   PSF RA coord,          degrees
+FIELD DEC,            DEC_PSF,           double,   PSF DEC coord,         degrees
+
+FIELD Mpeak,          PEAK_FLUX_AS_MAG,  float,    peak flux as a mag,    mags
+FIELD sky,            SKY,               float,    sky flux,              cnts/sec
+FIELD dSky,           SKY_SIG,           float,    sky flux error,        cnts/sec
+FIELD psfChisq,       PSF_CHISQ,         float,    psf fit chisq
+
+FIELD crNsigma,       CR_NSIGMA,         float,    Nsigma deviations from PSF to CF
+FIELD extNsigma,      EXT_NSIGMA,        float,    Nsigma deviations from PSF to EXT
+FIELD fx,             PSF_MAJOR,         float,    psf fit major axis,    pixels
+FIELD fy,             PSF_MINOR,         float,    psf fit minor axis,    pixels
+
+FIELD df,             PSF_THETA,         float,    ellipse angle,         degrees
+FIELD psfQF,          PSF_QF,            float,    quality factor
+FIELD psfQFperf,      PSF_QF_PERFECT,    float,    quality factor perfect
+FIELD psfNdof,        PSF_NDOF,          int,      psf degrees of freedom
+
+FIELD psfNpix,        PSF_NPIX,          int,      psf number of pixels
+FIELD Mxx,            MOMENTS_XX,        float,    second moment X,       pixels^2
+FIELD Mxy,            MOMENTS_XY,        float,    second moment Y,       pixels^2
+FIELD Myy,            MOMENTS_YY,        float,    second moment XY,      pixels^2
+
+FIELD M3c,            MOMENTS_M3C,       float,    third moment cos(t),   pixels^3
+FIELD M3s,            MOMENTS_M3S,       float,    third moment sin(t),   pixels^3
+FIELD M4c,            MOMENTS_M4C,       float,    fourth moment cos(t),  pixels^4
+FIELD M4s,            MOMENTS_M4S,       float,    fourth moment sin(t),  pixels^4
+
+FIELD Mr1,            MOMENTS_R1,        float,    first radial moment,   pixels
+FIELD Mrh,            MOMENTS_RH,        float,    half radial moment,    pixels^1/2
+FIELD kronFlux,       KRON_FLUX,         float,    kron flux,             counts
+FIELD kronFluxErr,    KRON_FLUX_ERR,     float,    kron flux error,       counts
+
+FIELD kronInner,      KRON_FLUX_INNER,   float,    kron flux 1<R<2.5,     counts
+FIELD kronOuter,      KRON_FLUX_OUTER,   float,    kron flux 2.5<R<4,     counts
+FIELD flags,          FLAGS,             int,      analysis flags
+FIELD flags2,         FLAGS2,            int,      analysis flags (2)
+
+FIELD padding2,       PADDING2,          int,      padding for 8byte records
+FIELD nFrames,        N_FRAMES,          short,    images overlapping peak
+FIELD padding,        PADDING,           short,    padding for 8byte records
+
+# for an object in an image, we have three triplets that tell us about the shape:
+# second moments: Mxx, Mxy, Myy 
+# model shape parameters: F_major, F_minor, F_theta
+# centroid errors: sigma_X, sigma_Y, sigma_XY
+
+# # elements of data structure / FITS table
+# FIELD detID,          IPP_IDET,          IPP_IDET           1J      unsigned int, detection ID                     
+# FIELD X,              X_PSF,             X_PSF              1E      float,    x coord,               pixels
+# FIELD Y,              Y_PSF,             Y_PSF              1E      float,    y coord,               pixels
+# FIELD dX,             X_PSF_SIG,         X_PSF_SIG          1E      float,    x coord error,         pixels
+# FIELD dY,             Y_PSF_SIG,         Y_PSF_SIG          1E      float,    y coord error,         pixels
+# FIELD posangle,       POSANGLE,          POSANGLE           1E      float,    Posangle at source,    degrees
+# FIELD pltscale,       PLTSCALE,          PLTSCALE           1E      float,    Plate Scale at source, arcsec/pixel
+# FIELD M,              PSF_INST_MAG,      PSF_INST_MAG       1E      float,    inst mags,             mags
+# FIELD dM,             PSF_INST_MAG_SIG,  PSF_INST_MAG_SIG   1E      float,    inst mag error,        mags
+# FIELD flux,           PSF_INST_FLUX,     PSF_INST_FLUX      1E      float,    psf flux,	       counts
+# FIELD flux,           PSF_INST_FLUX_SIG, PSF_INST_FLUX_SIG  1E      float,    psf flux error,        counts      
+# FIELD Map,            AP_MAG_STANDARD,   AP_MAG             1E      float,    standard aperture mag, mags
+# FIELD Map,            AP_MAG_RAW,        AP_MAG_RAW         1E      float,    raw aperture mag,      mags
+# FIELD apRadius,       AP_MAG_RADIUS,     AP_MAG_RADIUS      1E      float,    radius used for fit,   pixels
+# FIELD Mpeak,          PEAK_FLUX_AS_MAG,  PEAK_FLUX_AS_MAG   1E      float,    peak flux as a mag,    mags
+# FIELD Mcalib,         CAL_PSF_MAG,       CAL_PSF_MAG        1E      float,    calibrated psf mag,    mags
+# FIELD dMcal,          CAL_PSF_MAG_SIG,   CAL_PSF_MAG_SIG    1E      float,    zero point scatter,    mags
+# FIELD RA,             RA_PSF,            RA_PSF             1D      double,   PSF RA coord,          degrees
+# FIELD DEC,            DEC_PSF,           DEC_PSF            1D      double,   PSF DEC coord,         degrees
+# FIELD sky,            SKY,               SKY                1E      float,    sky flux,              cnts/sec
+# FIELD dSky,           SKY_SIG,           SKY_SIGMA          1E      float,    sky flux error,        cnts/sec
+# FIELD psfChisq,       PSF_CHISQ,         PSF_CHISQ          1E      float,    psf fit chisq
+# FIELD crNsigma,       CR_NSIGMA,         CR_NSIGMA          1E      float,    Nsigma deviations from PSF to CF
+# FIELD extNsigma,      EXT_NSIGMA,        EXT_NSIGMA         1E      float,    Nsigma deviations from PSF to EXT
+# FIELD fx,             PSF_MAJOR,         PSF_MAJOR          1E      float,    psf fit major axis,    pixels
+# FIELD fy,             PSF_MINOR,         PSF_MINOR          1E      float,    psf fit minor axis,    pixels
+# FIELD df,             PSF_THETA,         PSF_THETA          1E      float,    ellipse angle,         degrees
+# FIELD psfQF,          PSF_QF,            PSF_QF             1E      float,    quality factor
+# FIELD psfQFperf,      PSF_QF_PERFECT,    PSF_QF_PERFECT     1E      float,    quality factor perfect
+# FIELD psfNdof,        PSF_NDOF,          PSF_NDOF           1J      int,      psf degrees of freedom
+# FIELD psfNpix,        PSF_NPIX,          PSF_NPIX           1J      int,      psf number of pixels
+# FIELD Mxx,            MOMENTS_XX,        MOMENTS_XX         1E      float,    second moment X,       pixels^2
+# FIELD Mxy,            MOMENTS_XY,        MOMENTS_XY         1E      float,    second moment Y,       pixels^2
+# FIELD Myy,            MOMENTS_YY,        MOMENTS_YY         1E      float,    second moment XY,      pixels^2
+# FIELD M3c,            MOMENTS_M3C,       MOMENTS_M3C        1E      float,    third moment cos(t),   pixels^3
+# FIELD M3s,            MOMENTS_M3S,       MOMENTS_M3S        1E      float,    third moment sin(t),   pixels^3
+# FIELD M4c,            MOMENTS_M4C,       MOMENTS_M4C        1E      float,    fourth moment cos(t),  pixels^4
+# FIELD M4s,            MOMENTS_M4S,       MOMENTS_M4S        1E      float,    fourth moment sin(t),  pixels^4
+# FIELD Mr1,            MOMENTS_R1,        MOMENTS_R1         1E      float,    first radial moment,   pixels
+# FIELD Mrh,            MOMENTS_RH,        MOMENTS_RH         1E      float,    half radial moment,    pixels^1/2
+# FIELD kronFlux,       KRON_FLUX,         KRON_FLUX          1E      float,    kron flux,             counts
+# FIELD kronFluxErr,    KRON_FLUX_ERR,     KRON_FLUX_ERR      1E      float,    kron flux error,       counts
+# FIELD kronInner,      KRON_FLUX_INNER,   KRON_FLUX_INNER    1E      float,    kron flux 1<R<2.5,     counts
+# FIELD kronOuter,      KRON_FLUX_OUTER,   KRON_FLUX_OUTER    1E      float,    kron flux 2.5<R<4,     counts
+# FIELD flags,          FLAGS,             FLAGS              1J      int,      analysis flags
+# FIELD flags2,         FLAGS,             FLAGS2             1J      int,      analysis flags (2)
+# FIELD nFrames,        N_FRAMES,          N_FRAMES           1I      short,    images overlapping peak
+# FIELD padding,        PADDING,           PADDING            1I      short,    padding for 8byte records
+
+      
+
Index: trunk/Ohana/src/libautocode/def/cmf-ps1-v1.d
===================================================================
--- trunk/Ohana/src/libautocode/def/cmf-ps1-v1.d	(revision 34404)
+++ trunk/Ohana/src/libautocode/def/cmf-ps1-v1.d	(revision 34405)
@@ -30,5 +30,5 @@
 FIELD fy,     	 PSF_MINOR,        float,    psf fit minor axis,    pixels
 FIELD df,     	 PSF_THETA,        float,    ellipse angle,         degrees
-FIELD psfQual, 	 PSF_QF,           float,    quality factor
+FIELD psfQF, 	 PSF_QF,           float,    quality factor
 FIELD psfNdof, 	 PSF_NDOF,         int,      psf degrees of freedom
 FIELD psfNpix, 	 PSF_NPIX,         int,      psf number of pixels
Index: trunk/Ohana/src/libautocode/def/cmf-ps1-v2.d
===================================================================
--- trunk/Ohana/src/libautocode/def/cmf-ps1-v2.d	(revision 34404)
+++ trunk/Ohana/src/libautocode/def/cmf-ps1-v2.d	(revision 34405)
@@ -30,5 +30,5 @@
 FIELD fy,     	 PSF_MINOR,        float,    psf fit minor axis,    pixels
 FIELD df,     	 PSF_THETA,        float,    ellipse angle,         degrees
-FIELD psfQual, 	 PSF_QF,           float,    quality factor
+FIELD psfQF, 	 PSF_QF,           float,    quality factor
 FIELD psfNdof, 	 PSF_NDOF,         int,      psf degrees of freedom
 FIELD psfNpix, 	 PSF_NPIX,         int,      psf number of pixels
Index: trunk/Ohana/src/libautocode/def/cmf-ps1-v3.d
===================================================================
--- trunk/Ohana/src/libautocode/def/cmf-ps1-v3.d	(revision 34404)
+++ trunk/Ohana/src/libautocode/def/cmf-ps1-v3.d	(revision 34405)
@@ -36,6 +36,6 @@
 FIELD fy,             PSF_MINOR,         float,    psf fit minor axis,    pixels
 FIELD df,             PSF_THETA,         float,    ellipse angle,         degrees
-FIELD psfQual,        PSF_QF,            float,    quality factor
-FIELD psfQualPerfect, PSF_QF_PERFECT,    float,    quality factor perfect
+FIELD psfQF,          PSF_QF,            float,    quality factor
+FIELD psfQFperf,      PSF_QF_PERFECT,    float,    quality factor perfect
 FIELD psfNdof,        PSF_NDOF,          int,      psf degrees of freedom
 FIELD psfNpix,        PSF_NPIX,          int,      psf number of pixels
@@ -68,5 +68,5 @@
 # FIELD dFlux,          PSF_INST_FLUX_SIG, float,    psf flux error,        counts      
 # FIELD MapRaw,         AP_MAG_RAW,        float,    raw aperture mag,      mags
-# FIELD psfQualPerfect, PSF_QF_PERFECT,    float,    quality factor perfect
+# FIELD psfQFperf,      PSF_QF_PERFECT,    float,    quality factor perfect
 # FIELD M3c,            MOMENTS_M3C,       float,    third moment cos(t),   pixels^3
 # FIELD M3s,            MOMENTS_M3S,       float,    third moment sin(t),   pixels^3
Index: trunk/Ohana/src/libautocode/def/cmf-ps1-v4.d
===================================================================
--- trunk/Ohana/src/libautocode/def/cmf-ps1-v4.d	(revision 34405)
+++ trunk/Ohana/src/libautocode/def/cmf-ps1-v4.d	(revision 34405)
@@ -0,0 +1,85 @@
+# name of structure type
+STRUCT  CMF_PS1_V4
+EXTNAME CMF_PS1_V4
+TYPE    BINTABLE
+SIZE    216
+
+# elements of data structure / FITS table
+FIELD detID,          IPP_IDET,          unsigned int, detection ID                     
+FIELD X,              X_PSF,             float,    x coord,               pixels
+FIELD Y,              Y_PSF,             float,    y coord,               pixels
+FIELD dX,             X_PSF_SIG,         float,    x coord error,         pixels
+FIELD dY,             Y_PSF_SIG,         float,    y coord error,         pixels
+FIELD posangle,       POSANGLE,          float,    Posangle at source,    degrees
+FIELD pltscale,       PLTSCALE,          float,    Plate Scale at source, arcsec/pixel
+FIELD M,              PSF_INST_MAG,      float,    inst mags,             mags
+FIELD dM,             PSF_INST_MAG_SIG,  float,    inst mag error,        mags
+FIELD Flux,           PSF_INST_FLUX,     float,    psf flux,	       counts
+FIELD dFlux,          PSF_INST_FLUX_SIG, float,    psf flux error,        counts      
+FIELD Map,            AP_MAG_STANDARD,   float,    standard aperture mag, mags
+FIELD MapRaw,         AP_MAG_RAW,        float,    raw aperture mag,      mags
+FIELD apRadius,       AP_MAG_RADIUS,     float,    radius used for fit,   pixels
+FIELD apFlux,         AP_FLUX,           float,    aperture flux,         counts
+FIELD apFluxErr,      AP_FLUX_SIG,       float,    error on ap flux,      counts
+FIELD Mcalib,         CAL_PSF_MAG,       float,    calibrated psf mag,    mags
+FIELD dMcal,          CAL_PSF_MAG_SIG,   float,    zero point scatter,    mags
+
+# NOTE: RA & DEC (both double) need to be on an 8-byte boundary...
+FIELD RA,             RA_PSF,            double,   PSF RA coord,          degrees
+FIELD DEC,            DEC_PSF,           double,   PSF DEC coord,         degrees
+
+FIELD Mpeak,          PEAK_FLUX_AS_MAG,  float,    peak flux as a mag,    mags
+FIELD sky,            SKY,               float,    sky flux,              cnts/sec
+FIELD dSky,           SKY_SIG,           float,    sky flux error,        cnts/sec
+FIELD psfChisq,       PSF_CHISQ,         float,    psf fit chisq
+FIELD crNsigma,       CR_NSIGMA,         float,    Nsigma deviations from PSF to CF
+FIELD extNsigma,      EXT_NSIGMA,        float,    Nsigma deviations from PSF to EXT
+FIELD fx,             PSF_MAJOR,         float,    psf fit major axis,    pixels
+FIELD fy,             PSF_MINOR,         float,    psf fit minor axis,    pixels
+FIELD df,             PSF_THETA,         float,    ellipse angle,         degrees
+FIELD psfQF,          PSF_QF,            float,    quality factor
+FIELD psfQFperf,      PSF_QF_PERFECT,    float,    quality factor perfect
+FIELD psfNdof,        PSF_NDOF,          int,      psf degrees of freedom
+FIELD psfNpix,        PSF_NPIX,          int,      psf number of pixels
+FIELD Mxx,            MOMENTS_XX,        float,    second moment X,       pixels^2
+FIELD Mxy,            MOMENTS_XY,        float,    second moment Y,       pixels^2
+FIELD Myy,            MOMENTS_YY,        float,    second moment XY,      pixels^2
+FIELD M3c,            MOMENTS_M3C,       float,    third moment cos(t),   pixels^3
+FIELD M3s,            MOMENTS_M3S,       float,    third moment sin(t),   pixels^3
+FIELD M4c,            MOMENTS_M4C,       float,    fourth moment cos(t),  pixels^4
+FIELD M4s,            MOMENTS_M4S,       float,    fourth moment sin(t),  pixels^4
+FIELD Mr1,            MOMENTS_R1,        float,    first radial moment,   pixels
+FIELD Mrh,            MOMENTS_RH,        float,    half radial moment,    pixels^1/2
+FIELD kronFlux,       KRON_FLUX,         float,    kron flux,             counts
+FIELD kronFluxErr,    KRON_FLUX_ERR,     float,    kron flux error,       counts
+FIELD kronInner,      KRON_FLUX_INNER,   float,    kron flux 1<R<2.5,     counts
+FIELD kronOuter,      KRON_FLUX_OUTER,   float,    kron flux 2.5<R<4,     counts
+FIELD skyLimitRad,    SKY_LIMIT_RAD,     float,    profile to sky limit (radius)
+FIELD skyLimitFlux,   SKY_LIMIT_FLUX,    float,    profile to sky limit (flux)
+FIELD skyLimitSlope,  SKY_LIMIT_SLOPE,   float,    profile to sky limit (slope)
+FIELD flags,          FLAGS,             int,      analysis flags
+FIELD flags2,         FLAGS,             int,      analysis flags (2)
+FIELD nFrames,        N_FRAMES,          short,    images overlapping peak
+FIELD padding,        PADDING,           short,    padding for 8byte records
+
+# for an object in an image, we have three triplets that tell us about the shape:
+# second moments: Mxx, Mxy, Myy 
+# model shape parameters: F_major, F_minor, F_theta
+# centroid errors: sigma_X, sigma_Y, sigma_XY
+
+# fields added since PS1_V2:
+# FIELD Flux,           PSF_INST_FLUX,     float,    psf flux,	       counts
+# FIELD dFlux,          PSF_INST_FLUX_SIG, float,    psf flux error,        counts      
+# FIELD MapRaw,         AP_MAG_RAW,        float,    raw aperture mag,      mags
+# FIELD psfQFPerf,      PSF_QF_PERFECT,    float,    quality factor perfect
+# FIELD M3c,            MOMENTS_M3C,       float,    third moment cos(t),   pixels^3
+# FIELD M3s,            MOMENTS_M3S,       float,    third moment sin(t),   pixels^3
+# FIELD M4c,            MOMENTS_M4C,       float,    fourth moment cos(t),  pixels^4
+# FIELD M4s,            MOMENTS_M4S,       float,    fourth moment sin(t),  pixels^4
+# FIELD Mr1,            MOMENTS_R1,        float,    first radial moment,   pixels
+# FIELD Mrh,            MOMENTS_RH,        float,    half radial moment,    pixels^1/2
+# FIELD kronFlux,       KRON_FLUX,         float,    kron flux,             counts
+# FIELD kronFluxErr,    KRON_FLUX_ERR,     float,    kron flux error,       counts
+# FIELD kronInner,      KRON_FLUX_INNER,   float,    kron flux 1<R<2.5,     counts
+# FIELD kronOuter,      KRON_FLUX_OUTER,   float,    kron flux 2.5<R<4,     counts
+# FIELD flags2,         FLAGS,             int,      analysis flags (2)
Index: trunk/Ohana/src/libautocode/def/measure-panstarrs-dev-0.d
===================================================================
--- trunk/Ohana/src/libautocode/def/measure-panstarrs-dev-0.d	(revision 34404)
+++ trunk/Ohana/src/libautocode/def/measure-panstarrs-dev-0.d	(revision 34405)
@@ -46,5 +46,5 @@
 
 # do we need more resolution than a short? should this be a log?
-FIELD psfQual,        PSF_QF,       short,          psf coverage/quality factor
+FIELD psfQF,          PSF_QF,       short,          psf coverage/quality factor
 
 FIELD dophot,         DOPHOT,       char,           dophot type
Index: trunk/Ohana/src/libautocode/def/measure-panstarrs-dev-1.d
===================================================================
--- trunk/Ohana/src/libautocode/def/measure-panstarrs-dev-1.d	(revision 34404)
+++ trunk/Ohana/src/libautocode/def/measure-panstarrs-dev-1.d	(revision 34405)
@@ -46,5 +46,5 @@
 
 # do we need more resolution than a short? should this be a log?
-FIELD psfQual,        PSF_QF,       short,          psf coverage/quality factor
+FIELD psfQF,          PSF_QF,       short,          psf coverage/quality factor
 
 FIELD dophot,         DOPHOT,       char,           dophot type
Index: trunk/Ohana/src/libautocode/def/measure-ps1-dev-1.d
===================================================================
--- trunk/Ohana/src/libautocode/def/measure-ps1-dev-1.d	(revision 34404)
+++ trunk/Ohana/src/libautocode/def/measure-ps1-dev-1.d	(revision 34405)
@@ -33,5 +33,5 @@
 
 # do we need more resolution than a short? should this be a log?
-FIELD psfQual,        PSF_QF,       float,          psf coverage/quality factor
+FIELD psfQF,          PSF_QF,       float,          psf coverage/quality factor
 FIELD psfChisq,       PSF_CHISQ,    float,          psf coverage/quality factor
 FIELD crNsigma,       CR_NSIGMA,    float,          psf coverage/quality factor
Index: trunk/Ohana/src/libautocode/def/measure-ps1-dev-2.d
===================================================================
--- trunk/Ohana/src/libautocode/def/measure-ps1-dev-2.d	(revision 34404)
+++ trunk/Ohana/src/libautocode/def/measure-ps1-dev-2.d	(revision 34405)
@@ -34,5 +34,5 @@
 
 # do we need more resolution than a short? should this be a log?
-FIELD psfQual,        PSF_QF,       float,          psf coverage/quality factor
+FIELD psfQF,          PSF_QF,       float,          psf coverage/quality factor
 FIELD psfChisq,       PSF_CHISQ,    float,          psf fit chisq
 FIELD crNsigma,       CR_NSIGMA,    float,          Nsigma deviation towards CR
Index: trunk/Ohana/src/libautocode/def/measure-ps1-v1.d
===================================================================
--- trunk/Ohana/src/libautocode/def/measure-ps1-v1.d	(revision 34404)
+++ trunk/Ohana/src/libautocode/def/measure-ps1-v1.d	(revision 34405)
@@ -39,5 +39,5 @@
 
 # do we need more resolution than a short? should this be a log?
-FIELD psfQual,        PSF_QF,       float,          psf coverage/quality factor
+FIELD psfQF,          PSF_QF,       float,          psf coverage/quality factor
 FIELD psfChisq,       PSF_CHISQ,    float,          psf fit chisq
 FIELD psfNdof,        PSF_NDOF,     int,            psf degrees of freedom
Index: trunk/Ohana/src/libautocode/def/measure-ps1-v2.d
===================================================================
--- trunk/Ohana/src/libautocode/def/measure-ps1-v2.d	(revision 34404)
+++ trunk/Ohana/src/libautocode/def/measure-ps1-v2.d	(revision 34405)
@@ -39,5 +39,5 @@
 
 # do we need more resolution than a short? should this be a log?
-FIELD psfQual,        PSF_QF,       float,          psf coverage/quality factor
+FIELD psfQF,          PSF_QF,       float,          psf coverage/quality factor
 FIELD psfChisq,       PSF_CHISQ,    float,          psf fit chisq
 FIELD psfNdof,        PSF_NDOF,     int,            psf degrees of freedom
Index: trunk/Ohana/src/libautocode/def/measure-ps1-v3.d
===================================================================
--- trunk/Ohana/src/libautocode/def/measure-ps1-v3.d	(revision 34404)
+++ trunk/Ohana/src/libautocode/def/measure-ps1-v3.d	(revision 34405)
@@ -39,5 +39,5 @@
 
 # do we need more resolution than a short? should this be a log?
-FIELD psfQual,        PSF_QF,       float,          psf coverage/quality factor
+FIELD psfQF,          PSF_QF,       float,          psf coverage/quality factor
 FIELD psfChisq,       PSF_CHISQ,    float,          psf fit chisq
 FIELD psfNdof,        PSF_NDOF,     int,            psf degrees of freedom
Index: trunk/Ohana/src/libautocode/def/measure-ps1-v4.d
===================================================================
--- trunk/Ohana/src/libautocode/def/measure-ps1-v4.d	(revision 34404)
+++ trunk/Ohana/src/libautocode/def/measure-ps1-v4.d	(revision 34405)
@@ -47,10 +47,12 @@
 
 # do we need more resolution than a short? should this be a log?
-FIELD psfQual,        PSF_QF,       float,          psf coverage/quality factor
-FIELD psfChisq,       PSF_CHISQ,    float,          psf fit chisq
-FIELD psfNdof,        PSF_NDOF,     int,            psf degrees of freedom
-FIELD psfNpix,        PSF_NPIX,     int,            psf number of pixels
-FIELD crNsigma,       CR_NSIGMA,    float,          Nsigma deviation towards CR
-FIELD extNsigma,      EXT_NSIGMA,   float,          Nsigma deviation towards EXT
+FIELD psfQF,          PSF_QF,        float,          psf coverage/quality factor
+FIELD psfQFperf,      PSF_QF_PEFECT, float,          psf coverage / quality factor (all mask bits)
+FIELD psfChisq,       PSF_CHISQ,     float,          psf fit chisq
+
+FIELD psfNdof,        PSF_NDOF,      int,            psf degrees of freedom
+FIELD psfNpix,        PSF_NPIX,      int,            psf number of pixels
+FIELD crNsigma,       CR_NSIGMA,     float,          Nsigma deviation towards CR
+FIELD extNsigma,      EXT_NSIGMA,    float,          Nsigma deviation towards EXT
 
 # model shape parameters
@@ -73,6 +75,4 @@
 FIELD dRsys,          POS_SYS_ERR,  short,          systematic error from astrom,   1/100 of pixels
 
-FIELD pad,            PAD,          char[4],        padding
-
 # local astrometry scales
 FIELD posangle,       POSANGLE,     short,          position angle sky to chip,     (0xffff/360) deg
Index: trunk/Ohana/src/libautocode/def/measure.d
===================================================================
--- trunk/Ohana/src/libautocode/def/measure.d	(revision 34404)
+++ trunk/Ohana/src/libautocode/def/measure.d	(revision 34405)
@@ -2,5 +2,5 @@
 EXTNAME      DVO_MEASURE
 TYPE         BINTABLE
-SIZE         172
+SIZE         176
 DESCRIPTION  DVO Detection Measurement Table 
 
@@ -47,10 +47,12 @@
 
 # do we need more resolution than a short? should this be a log?
-FIELD psfQual,        PSF_QF,       float,          psf coverage/quality factor
-FIELD psfChisq,       PSF_CHISQ,    float,          psf fit chisq
-FIELD psfNdof,        PSF_NDOF,     int,            psf degrees of freedom
-FIELD psfNpix,        PSF_NPIX,     int,            psf number of pixels
-FIELD crNsigma,       CR_NSIGMA,    float,          Nsigma deviation towards CR
-FIELD extNsigma,      EXT_NSIGMA,   float,          Nsigma deviation towards EXT
+FIELD psfQF,          PSF_QF,        float,          psf coverage/quality factor
+FIELD psfQFperf,      PSF_QF_PEFECT, float,          psf coverage / quality factor (all mask bits)
+FIELD psfChisq,       PSF_CHISQ,     float,          psf fit chisq
+
+FIELD psfNdof,        PSF_NDOF,      int,            psf degrees of freedom
+FIELD psfNpix,        PSF_NPIX,      int,            psf number of pixels
+FIELD crNsigma,       CR_NSIGMA,     float,          Nsigma deviation towards CR
+FIELD extNsigma,      EXT_NSIGMA,    float,          Nsigma deviation towards EXT
 
 # model shape parameters
Index: trunk/Ohana/src/libdvo/doc/updates-psps.txt
===================================================================
--- trunk/Ohana/src/libdvo/doc/updates-psps.txt	(revision 34405)
+++ trunk/Ohana/src/libdvo/doc/updates-psps.txt	(revision 34405)
@@ -0,0 +1,17 @@
+
+2012.09.03 
+
+Things that we need to add to the DVO schema to support the desired PSPS stuff:
+
+* measure.psfQualPerfect (PSF_QF_PERFECT) [OK in ps1-v4]
+* average.stargal [add to ps1-v4, old average@ps1-v4 need to be read as average@ps1-v3]
+  (re-purpose average.Xp, which should be deprecated anyway)
+* error on kron radius : skipping this (is proportional to R1 / (S/N))
+
+Here is what I've done:
+* updated mksource.pl to handle the cmf series a bit more cleanly
+* added CMF_PS1_SV2 and CMF_PS1_DV3
+* added AP_FLUX and AP_FLUX_SIG to PS1_V4, PS1_SV2 formats
+* added RA_EXT, DEC_EXT, POSANGLE, PLTSCALE to PS1_DV3
+* PLTSCALE has parity +1 if image has sky parity, -1 for opposite parity
+
Index: trunk/Ohana/src/libdvo/include/dvo.h
===================================================================
--- trunk/Ohana/src/libdvo/include/dvo.h	(revision 34404)
+++ trunk/Ohana/src/libdvo/include/dvo.h	(revision 34405)
@@ -623,4 +623,5 @@
 int dvo_image_save_raw (FITS_DB *db, int VERBOSE);
 int dvo_image_addrows (FITS_DB *db, Image *new, off_t Nnew);
+int dvo_image_createID (Header *header);
 void dvo_image_create (FITS_DB *db, double ZeroPoint);
 
Index: trunk/Ohana/src/libdvo/src/dbExtractAverages.c
===================================================================
--- trunk/Ohana/src/libdvo/src/dbExtractAverages.c	(revision 34404)
+++ trunk/Ohana/src/libdvo/src/dbExtractAverages.c	(revision 34405)
@@ -147,8 +147,4 @@
     case AVE_TRANGE:
       value.Flt = TimeValue (average[0].Trange, 0, TimeFormat);
-      break;
-
-    case AVE_Xp:
-      value.Flt = 0.01*average[0].Xp;
       break;
 
Index: trunk/Ohana/src/libdvo/src/dbExtractMeasures.c
===================================================================
--- trunk/Ohana/src/libdvo/src/dbExtractMeasures.c	(revision 34404)
+++ trunk/Ohana/src/libdvo/src/dbExtractMeasures.c	(revision 34405)
@@ -303,4 +303,5 @@
     case MEAS_SECFILT_FLAGS: /* OK */
       equiv = GetPhotcodeEquivbyCode (measure[0].photcode);
+      if (!equiv) break;
       Nsec = GetPhotcodeNsec (equiv->code);
       if (Nsec == -1) break;
@@ -479,5 +480,5 @@
       break;
     case MEAS_PSF_QF: /* OK */
-      value.Flt = measure[0].psfQual;
+      value.Flt = measure[0].psfQF;
       break;
     case MEAS_PSF_QF_PERFECT: /* OK */
Index: trunk/Ohana/src/libdvo/src/dbFields.c
===================================================================
--- trunk/Ohana/src/libdvo/src/dbFields.c	(revision 34404)
+++ trunk/Ohana/src/libdvo/src/dbFields.c	(revision 34405)
@@ -335,8 +335,4 @@
   if (!strcasecmp (fieldName, "EXTID_LO"))  ESCAPE (AVE_EXTID_LO,  MAG_NONE, OPIHI_INT);
 
-  // if (!strcasecmp (fieldName, "Xp"))      ESCAPE (AVE_Xp,        MAG_NONE, OPIHI_FLT);
-  // if (!strcasecmp (fieldName, "TYPE"))    ESCAPE (AVE_TYPE,      MAG_NONE, OPIHI_INT);
-  // for words that don't parse, try a photcode
-
   // check for code:mode in photcode name 
   code = ParsePhotcodeField (fieldName, &mode, MAG_AVE);
Index: trunk/Ohana/src/libdvo/src/dvo_catalog.c
===================================================================
--- trunk/Ohana/src/libdvo/src/dvo_catalog.c	(revision 34404)
+++ trunk/Ohana/src/libdvo/src/dvo_catalog.c	(revision 34405)
@@ -89,5 +89,5 @@
   average->Trange   	   = 0;
 
-  average->Xp        	   = 0;
+  average->stargal    	   = 0.0;
   average->Npos    	   = 0;
 
@@ -174,5 +174,5 @@
  measure->extID     = 0;
 
- measure->psfQual   = NAN;
+ measure->psfQF     = NAN;
  measure->psfChisq  = NAN;
  measure->psfNdof   = 0;
Index: trunk/Ohana/src/libdvo/src/dvo_convert_PS1_DEV_1.c
===================================================================
--- trunk/Ohana/src/libdvo/src/dvo_convert_PS1_DEV_1.c	(revision 34404)
+++ trunk/Ohana/src/libdvo/src/dvo_convert_PS1_DEV_1.c	(revision 34405)
@@ -27,5 +27,5 @@
     out[i].detID      = in[i].detID;
     out[i].imageID    = in[i].imageID;
-    out[i].psfQual    = in[i].psfQual;
+    out[i].psfQF      = in[i].psfQF;
     out[i].psfChisq   = in[i].psfChisq;
     out[i].crNsigma   = in[i].crNsigma;
@@ -65,4 +65,5 @@
     out[i].FluxKron   = NAN;
     out[i].dFluxKron  = NAN;
+    out[i].psfQFperf  = NAN;
   }
   return (out);
@@ -93,5 +94,5 @@
     out[i].detID      = in[i].detID;
     out[i].imageID    = in[i].imageID;
-    out[i].psfQual    = in[i].psfQual;
+    out[i].psfQF      = in[i].psfQF;
     out[i].psfChisq   = in[i].psfChisq;
     out[i].crNsigma   = in[i].crNsigma;
@@ -126,5 +127,4 @@
     out[i].R        	 = in[i].R;      
     out[i].D        	 = in[i].D;      
-    out[i].Xp       	 = in[i].Xp;     
     out[i].dR       	 = in[i].dR;
     out[i].dD       	 = in[i].dD;
@@ -159,4 +159,9 @@
     out[i].photFlagsUpper = 0;
     out[i].photFlagsLower = 0;
+
+    // added for PS1_V4 (Xp dropped in V4 onward, was not really used anyway)
+    out[i].psfQF         = NAN;
+    out[i].psfQFperf     = NAN;
+    out[i].stargal     	 = NAN;
   }
   return (out);
@@ -174,5 +179,4 @@
     out[i].R        	 = in[i].R;      
     out[i].D        	 = in[i].D;      
-    out[i].Xp       	 = in[i].Xp;     
     out[i].dR       	 = in[i].dR;
     out[i].dD       	 = in[i].dD;
Index: trunk/Ohana/src/libdvo/src/dvo_convert_PS1_DEV_2.c
===================================================================
--- trunk/Ohana/src/libdvo/src/dvo_convert_PS1_DEV_2.c	(revision 34404)
+++ trunk/Ohana/src/libdvo/src/dvo_convert_PS1_DEV_2.c	(revision 34405)
@@ -29,5 +29,5 @@
     out[i].detID      = in[i].detID;
     out[i].imageID    = in[i].imageID;
-    out[i].psfQual    = in[i].psfQual;
+    out[i].psfQF      = in[i].psfQF;
     out[i].psfChisq   = in[i].psfChisq;
     out[i].crNsigma   = in[i].crNsigma;
@@ -62,5 +62,6 @@
     out[i].FluxKron   = NAN;
     out[i].dFluxKron  = NAN;
-  }
+    out[i].psfQFperf  = NAN;
+ }
   return (out);
 }
@@ -92,5 +93,5 @@
     out[i].detID      = in[i].detID;
     out[i].imageID    = in[i].imageID;
-    out[i].psfQual    = in[i].psfQual;
+    out[i].psfQF      = in[i].psfQF;
     out[i].psfChisq   = in[i].psfChisq;
     out[i].crNsigma   = in[i].crNsigma;
@@ -130,5 +131,4 @@
     out[i].P        	 = in[i].P;
     out[i].dP       	 = in[i].dP;
-    out[i].Xp       	 = in[i].Xp;     
     out[i].Nmeasure      = in[i].Nmeasure;     
     out[i].Nmissing      = in[i].Nmissing;     
@@ -153,4 +153,9 @@
     out[i].photFlagsUpper = 0;
     out[i].photFlagsLower = 0;
+
+    // added for PS1_V4 (Xp dropped in V4 onward, was not really used anyway)
+    out[i].psfQF         = NAN;
+    out[i].psfQFperf     = NAN;
+    out[i].stargal     	 = NAN;
   }
   return (out);
@@ -176,5 +181,4 @@
     out[i].P        	 = in[i].P;
     out[i].dP       	 = in[i].dP;
-    out[i].Xp       	 = in[i].Xp;     
     out[i].Nmeasure      = in[i].Nmeasure;     
     out[i].Nmissing      = in[i].Nmissing;     
Index: trunk/Ohana/src/libdvo/src/dvo_convert_PS1_REF.c
===================================================================
--- trunk/Ohana/src/libdvo/src/dvo_convert_PS1_REF.c	(revision 34404)
+++ trunk/Ohana/src/libdvo/src/dvo_convert_PS1_REF.c	(revision 34405)
@@ -33,5 +33,5 @@
     out[i].catID      = in[i].catID;
     out[i].extID      = 0;
-    out[i].psfQual    = 1.0; // we do not save a value, but some operations (relphot/average) filter on this value..
+    out[i].psfQF      = 1.0; // we do not save a value, but some operations (relphot/average) filter on this value..
     out[i].psfChisq   = NAN;
     out[i].psfNdof    = 0;
@@ -60,4 +60,5 @@
     out[i].FluxKron   = NAN;
     out[i].dFluxKron  = NAN;
+    out[i].psfQFperf  = NAN;
   }
   return (out);
@@ -105,5 +106,4 @@
     out[i].P        	 = NAN;
     out[i].dP       	 = NAN;
-    out[i].Xp       	 = NAN_S_SHORT;
     out[i].ChiSqAve    	 = NAN;
     out[i].ChiSqPM    	 = NAN;
@@ -126,4 +126,9 @@
     out[i].photFlagsUpper = 0;
     out[i].photFlagsLower = 0;
+
+    // added for PS1_V4 (Xp dropped in V4 onward, was not really used anyway)
+    out[i].psfQF         = NAN;
+    out[i].psfQFperf     = NAN;
+    out[i].stargal     	 = NAN;
   }
   return (out);
Index: trunk/Ohana/src/libdvo/src/dvo_convert_PS1_V1.c
===================================================================
--- trunk/Ohana/src/libdvo/src/dvo_convert_PS1_V1.c	(revision 34404)
+++ trunk/Ohana/src/libdvo/src/dvo_convert_PS1_V1.c	(revision 34405)
@@ -33,5 +33,5 @@
     out[i].catID      = in[i].catID;
     out[i].extID      = in[i].extID;
-    out[i].psfQual    = in[i].psfQual;
+    out[i].psfQF      = in[i].psfQF;
     out[i].psfChisq   = in[i].psfChisq;
     out[i].psfNdof    = in[i].psfNdof;
@@ -60,4 +60,5 @@
     out[i].FluxKron   = NAN;
     out[i].dFluxKron  = NAN;
+    out[i].psfQFperf  = NAN;
   }
   return (out);
@@ -94,5 +95,5 @@
     out[i].catID      = in[i].catID;
     out[i].extID      = in[i].extID;
-    out[i].psfQual    = in[i].psfQual;
+    out[i].psfQF      = in[i].psfQF;
     out[i].psfChisq   = in[i].psfChisq;
     out[i].psfNdof    = in[i].psfNdof;
@@ -137,5 +138,4 @@
     out[i].P        	 = in[i].P;
     out[i].dP       	 = in[i].dP;
-    out[i].Xp       	 = in[i].Xp;     
     out[i].ChiSqAve    	 = in[i].ChiSq;     
     out[i].ChiSqPM    	 = NAN;
@@ -158,4 +158,9 @@
     out[i].photFlagsUpper = 0;
     out[i].photFlagsLower = 0;
+
+    // added for PS1_V4 (Xp dropped in V4 onward, was not really used anyway)
+    out[i].psfQF         = NAN;
+    out[i].psfQFperf     = NAN;
+    out[i].stargal     	 = NAN;
   }
   return (out);
@@ -181,5 +186,4 @@
     out[i].P        	 = in[i].P;
     out[i].dP       	 = in[i].dP;
-    out[i].Xp       	 = in[i].Xp;     
     out[i].ChiSq    	 = in[i].ChiSqAve;     
     out[i].Npos       	 = in[i].Npos;     
Index: trunk/Ohana/src/libdvo/src/dvo_convert_PS1_V2.c
===================================================================
--- trunk/Ohana/src/libdvo/src/dvo_convert_PS1_V2.c	(revision 34404)
+++ trunk/Ohana/src/libdvo/src/dvo_convert_PS1_V2.c	(revision 34405)
@@ -33,5 +33,5 @@
     out[i].catID      = in[i].catID;
     out[i].extID      = in[i].extID;
-    out[i].psfQual    = in[i].psfQual;
+    out[i].psfQF      = in[i].psfQF;
     out[i].psfChisq   = in[i].psfChisq;
     out[i].psfNdof    = in[i].psfNdof;
@@ -61,4 +61,5 @@
     out[i].FluxKron   = NAN;
     out[i].dFluxKron  = NAN;
+    out[i].psfQFperf  = NAN;
   }
   return (out);
@@ -95,5 +96,5 @@
     out[i].catID      = in[i].catID;
     out[i].extID      = in[i].extID;
-    out[i].psfQual    = in[i].psfQual;
+    out[i].psfQF      = in[i].psfQF;
     out[i].psfChisq   = in[i].psfChisq;
     out[i].psfNdof    = in[i].psfNdof;
@@ -138,5 +139,4 @@
     out[i].P        	 = in[i].P;
     out[i].dP       	 = in[i].dP;
-    out[i].Xp       	 = in[i].Xp;     
     out[i].ChiSqAve    	 = in[i].ChiSqAve;     
     out[i].ChiSqPM    	 = in[i].ChiSqPM;     
@@ -159,4 +159,9 @@
     out[i].photFlagsUpper = 0;
     out[i].photFlagsLower = 0;
+
+    // added for PS1_V4 (Xp dropped in V4 onward, was not really used anyway)
+    out[i].psfQF         = NAN;
+    out[i].psfQFperf     = NAN;
+    out[i].stargal     	 = NAN;
   }
   return (out);
@@ -182,5 +187,4 @@
     out[i].P        	 = in[i].P;
     out[i].dP       	 = in[i].dP;
-    out[i].Xp       	 = in[i].Xp;     
     out[i].ChiSqAve    	 = in[i].ChiSqAve;     
     out[i].ChiSqPM     	 = in[i].ChiSqPM;     
Index: trunk/Ohana/src/libdvo/src/dvo_convert_PS1_V3.c
===================================================================
--- trunk/Ohana/src/libdvo/src/dvo_convert_PS1_V3.c	(revision 34404)
+++ trunk/Ohana/src/libdvo/src/dvo_convert_PS1_V3.c	(revision 34405)
@@ -33,5 +33,5 @@
     out[i].catID      = in[i].catID;
     out[i].extID      = in[i].extID;
-    out[i].psfQual    = in[i].psfQual;
+    out[i].psfQF      = in[i].psfQF;
     out[i].psfChisq   = in[i].psfChisq;
     out[i].psfNdof    = in[i].psfNdof;
@@ -61,4 +61,5 @@
     out[i].FluxKron   = NAN;
     out[i].dFluxKron  = NAN;
+    out[i].psfQFperf  = NAN;
   }
   return (out);
@@ -95,5 +96,5 @@
     out[i].catID      = in[i].catID;
     out[i].extID      = in[i].extID;
-    out[i].psfQual    = in[i].psfQual;
+    out[i].psfQF      = in[i].psfQF;
     out[i].psfChisq   = in[i].psfChisq;
     out[i].psfNdof    = in[i].psfNdof;
@@ -138,5 +139,4 @@
     out[i].P        	 = in[i].P;
     out[i].dP       	 = in[i].dP;
-    out[i].Xp       	 = in[i].Xp;     
     out[i].ChiSqAve    	 = in[i].ChiSqAve;     
     out[i].ChiSqPM    	 = in[i].ChiSqPM;     
@@ -157,4 +157,9 @@
     out[i].catID 	 = in[i].catID;
     out[i].extID 	 = in[i].extID;
+
+    // added for PS1_V4 (Xp dropped in V4 onward, was not really used anyway)
+    out[i].psfQF         = NAN;
+    out[i].psfQFperf     = NAN;
+    out[i].stargal     	 = NAN;
   }
   return (out);
@@ -180,5 +185,4 @@
     out[i].P        	 = in[i].P;
     out[i].dP       	 = in[i].dP;
-    out[i].Xp       	 = in[i].Xp;     
     out[i].ChiSqAve    	 = in[i].ChiSqAve;     
     out[i].ChiSqPM     	 = in[i].ChiSqPM;     
Index: trunk/Ohana/src/libdvo/src/dvo_convert_PS1_V4.c
===================================================================
--- trunk/Ohana/src/libdvo/src/dvo_convert_PS1_V4.c	(revision 34404)
+++ trunk/Ohana/src/libdvo/src/dvo_convert_PS1_V4.c	(revision 34405)
@@ -39,5 +39,6 @@
     out[i].catID      = in[i].catID;
     out[i].extID      = in[i].extID;
-    out[i].psfQual    = in[i].psfQual;
+    out[i].psfQF      = in[i].psfQF;
+    out[i].psfQFperf  = in[i].psfQFperf;
     out[i].psfChisq   = in[i].psfChisq;
     out[i].psfNdof    = in[i].psfNdof;
@@ -99,5 +100,6 @@
     out[i].catID      = in[i].catID;
     out[i].extID      = in[i].extID;
-    out[i].psfQual    = in[i].psfQual;
+    out[i].psfQF      = in[i].psfQF;
+    out[i].psfQFperf  = in[i].psfQFperf;
     out[i].psfChisq   = in[i].psfChisq;
     out[i].psfNdof    = in[i].psfNdof;
@@ -142,5 +144,7 @@
     out[i].P        	 = in[i].P;
     out[i].dP       	 = in[i].dP;
-    out[i].Xp       	 = in[i].Xp;     
+    out[i].psfQF         = in[i].psfQF;
+    out[i].psfQFperf     = in[i].psfQFperf;
+    out[i].stargal     	 = in[i].stargal;     
     out[i].ChiSqAve    	 = in[i].ChiSqAve;     
     out[i].ChiSqPM    	 = in[i].ChiSqPM;     
@@ -184,5 +188,7 @@
     out[i].P        	 = in[i].P;
     out[i].dP       	 = in[i].dP;
-    out[i].Xp       	 = in[i].Xp;     
+    out[i].psfQF         = in[i].psfQF;
+    out[i].psfQFperf     = in[i].psfQFperf;
+    out[i].stargal     	 = in[i].stargal;     
     out[i].ChiSqAve    	 = in[i].ChiSqAve;     
     out[i].ChiSqPM     	 = in[i].ChiSqPM;     
Index: trunk/Ohana/src/libdvo/src/dvo_convert_elixir.c
===================================================================
--- trunk/Ohana/src/libdvo/src/dvo_convert_elixir.c	(revision 34404)
+++ trunk/Ohana/src/libdvo/src/dvo_convert_elixir.c	(revision 34405)
@@ -35,5 +35,5 @@
     out[i].Sky       = 0;
     out[i].dSky      = 0;
-    out[i].psfQual   = 0;
+    out[i].psfQF     = 0;
     out[i].psfChisq  = 0;
     out[i].crNsigma  = 0;
@@ -72,4 +72,5 @@
     out[i].FluxKron   = NAN;
     out[i].dFluxKron  = NAN;
+    out[i].psfQFperf  = NAN;
   }
   return (out);
@@ -127,5 +128,4 @@
     out[i].R       	 = in[i].R;      
     out[i].D       	 = in[i].D;      
-    out[i].Xp      	 = in[i].Xp;     
 
     // changed for PANSTARRS_DEV_0 (moved from Average to Measure)
@@ -170,4 +170,9 @@
     out[i].photFlagsUpper = 0;
     out[i].photFlagsLower = 0;
+
+    // added for PS1_V4 (Xp dropped in V4 onward, was not really used anyway)
+    out[i].psfQF         = NAN;
+    out[i].psfQFperf     = NAN;
+    out[i].stargal     	 = NAN;
   }
   return (out);
@@ -185,5 +190,4 @@
     out[i].R       = in[i].R;      
     out[i].D       = in[i].D;      
-    out[i].Xp      = in[i].Xp;     
 
     // changed for PANSTARRS_DEV_0 (moved from Average to Measure)
Index: trunk/Ohana/src/libdvo/src/dvo_convert_loneos.c
===================================================================
--- trunk/Ohana/src/libdvo/src/dvo_convert_loneos.c	(revision 34404)
+++ trunk/Ohana/src/libdvo/src/dvo_convert_loneos.c	(revision 34405)
@@ -39,5 +39,5 @@
     out[i].Sky       = 0;
     out[i].dSky      = 0;
-    out[i].psfQual   = 0;
+    out[i].psfQF     = 0;
     out[i].psfChisq  = 0;
     out[i].crNsigma  = 0;
@@ -74,4 +74,5 @@
     out[i].FluxKron   = NAN;
     out[i].dFluxKron  = NAN;
+    out[i].psfQFperf  = NAN;
   }
   return (out);
@@ -118,5 +119,4 @@
     out[i].R       	 = in[i].R;      
     out[i].D       	 = in[i].D;      
-    out[i].Xp      	 = in[i].Xp;     
 
     // added for ELIXIR
@@ -162,4 +162,9 @@
     out[i].photFlagsUpper = 0;
     out[i].photFlagsLower = 0;
+
+    // added for PS1_V4 (Xp dropped in V4 onward, was not really used anyway)
+    out[i].psfQF         = NAN;
+    out[i].psfQFperf     = NAN;
+    out[i].stargal     	 = NAN;
   }
   return (out);
@@ -177,5 +182,4 @@
     out[i].R       = in[i].R;      
     out[i].D       = in[i].D;      
-    out[i].Xp      = in[i].Xp;     
 
     // changed for PANSTARRS_DEV_0 (moved from Average to Measure)
Index: trunk/Ohana/src/libdvo/src/dvo_convert_panstarrs_DEV_0.c
===================================================================
--- trunk/Ohana/src/libdvo/src/dvo_convert_panstarrs_DEV_0.c	(revision 34404)
+++ trunk/Ohana/src/libdvo/src/dvo_convert_panstarrs_DEV_0.c	(revision 34405)
@@ -36,5 +36,5 @@
 
     // changed or added for PS1_DEV_1 (2008.02.26)
-    out[i].psfQual    = (in[i].psfQual == NAN_S_SHORT) ? NAN : in[i].psfQual;
+    out[i].psfQF      = (in[i].psfQF == NAN_S_SHORT) ? NAN : in[i].psfQF;
     out[i].dbFlags    = in[i].flags;
     out[i].detID      = in[i].detID_lo;
@@ -65,8 +65,9 @@
     out[i].Mkron      = NAN;
     out[i].dMkron     = NAN;
-    out[i].FluxPSF     = NAN;
-    out[i].dFluxPSF    = NAN;
-    out[i].FluxKron    = NAN;
-    out[i].dFluxKron   = NAN;
+    out[i].FluxPSF    = NAN;
+    out[i].dFluxPSF   = NAN;
+    out[i].FluxKron   = NAN;
+    out[i].dFluxKron  = NAN;
+    out[i].psfQFperf  = NAN;
   }
   return (out);
@@ -105,5 +106,5 @@
     // changed or added for PS1_DEV_1 (2008.02.26)
     out[i].flags      = in[i].dbFlags;
-    out[i].psfQual    = isnan(in[i].psfQual) ? NAN_S_SHORT : in[i].psfQual;
+    out[i].psfQF      = isnan(in[i].psfQF) ? NAN_S_SHORT : in[i].psfQF;
     out[i].detID_hi   = 0;
     out[i].detID_lo   = in[i].detID;
@@ -133,5 +134,4 @@
     out[i].R        	 = in[i].R;      
     out[i].D        	 = in[i].D;      
-    out[i].Xp       	 = in[i].Xp;     
     out[i].dR       	 = in[i].dR;
     out[i].dD       	 = in[i].dD;
@@ -166,4 +166,9 @@
     out[i].photFlagsUpper = 0;
     out[i].photFlagsLower = 0;
+
+    // added for PS1_V4 (Xp dropped in V4 onward, was not really used anyway)
+    out[i].psfQF         = NAN;
+    out[i].psfQFperf     = NAN;
+    out[i].stargal     	 = NAN;
   }
   return (out);
@@ -182,5 +187,4 @@
     out[i].R        = in[i].R;      
     out[i].D        = in[i].D;      
-    out[i].Xp       = in[i].Xp;     
     out[i].dR       = in[i].dR;
     out[i].dD       = in[i].dD;
Index: trunk/Ohana/src/libdvo/src/dvo_convert_panstarrs_DEV_1.c
===================================================================
--- trunk/Ohana/src/libdvo/src/dvo_convert_panstarrs_DEV_1.c	(revision 34404)
+++ trunk/Ohana/src/libdvo/src/dvo_convert_panstarrs_DEV_1.c	(revision 34405)
@@ -36,5 +36,5 @@
 
     // changed or added for PS1_DEV_1 (2008.02.26)
-    out[i].psfQual    = (in[i].psfQual == NAN_S_SHORT) ? NAN : in[i].psfQual;
+    out[i].psfQF      = (in[i].psfQF == NAN_S_SHORT) ? NAN : in[i].psfQF;
     out[i].dbFlags    = in[i].flags;
     out[i].detID      = in[i].detID_lo;
@@ -65,8 +65,9 @@
     out[i].Mkron      = NAN;
     out[i].dMkron     = NAN;
-    out[i].FluxPSF     = NAN;
-    out[i].dFluxPSF    = NAN;
-    out[i].FluxKron    = NAN;
-    out[i].dFluxKron   = NAN;
+    out[i].FluxPSF    = NAN;
+    out[i].dFluxPSF   = NAN;
+    out[i].FluxKron   = NAN;
+    out[i].dFluxKron  = NAN;
+    out[i].psfQFperf  = NAN;
   }
   return (out);
@@ -105,5 +106,5 @@
     // changed or added for PS1_DEV_1 (2008.02.26)
     out[i].flags      = in[i].dbFlags;
-    out[i].psfQual    = isnan(in[i].psfQual) ? NAN_S_SHORT : in[i].psfQual;
+    out[i].psfQF      = isnan(in[i].psfQF) ? NAN_S_SHORT : in[i].psfQF;
     out[i].detID_hi   = 0;
     out[i].detID_lo   = in[i].detID;
@@ -133,5 +134,4 @@
     out[i].R        	 = in[i].R;      
     out[i].D        	 = in[i].D;      
-    out[i].Xp       	 = in[i].Xp;     
     out[i].dR       	 = in[i].dR;
     out[i].dD       	 = in[i].dD;
@@ -166,4 +166,9 @@
     out[i].photFlagsUpper = 0;
     out[i].photFlagsLower = 0;
+
+    // added for PS1_V4 (Xp dropped in V4 onward, was not really used anyway)
+    out[i].psfQF         = NAN;
+    out[i].psfQFperf     = NAN;
+    out[i].stargal     	 = NAN;
   }
   return (out);
@@ -182,5 +187,4 @@
     out[i].R        = in[i].R;      
     out[i].D        = in[i].D;      
-    out[i].Xp       = in[i].Xp;     
     out[i].dR       = in[i].dR;
     out[i].dD       = in[i].dD;
Index: trunk/Ohana/src/libdvo/src/dvo_image.c
===================================================================
--- trunk/Ohana/src/libdvo/src/dvo_image.c	(revision 34404)
+++ trunk/Ohana/src/libdvo/src/dvo_image.c	(revision 34405)
@@ -200,4 +200,6 @@
   gfits_modify (&db[0].header, "NIMAGES", "%d", 1, 0);
   gfits_modify (&db[0].header, "ZERO_PT", "%lf", 1, ZeroPoint);
+  
+  dvo_image_createID (&db[0].header);
 
   if (db[0].format == DVO_FORMAT_INTERNAL)  	  gfits_modify (&db[0].header, "FORMAT", "%s", 1, "INTERNAL");
@@ -217,2 +219,45 @@
   return;
 }
+
+// given a dvo image db, add an ID to the header
+int dvo_image_createID (Header *header) {
+  
+  char dbID[33];
+
+  if (!header->buffer) return FALSE;
+
+  int status = gfits_scan (header, "DVO_DBID", "%s", 1, dbID);      
+  if (status) {
+    // do not add a DVO_DBID to a table which already has one
+    return TRUE;
+  }
+
+  // I have a header, I want to add ONE line.  double check there is enough space
+  char *p = gfits_header_field (header, "END", 1);
+  if (p == NULL) {
+    fprintf (stderr, "header is missing END\n");
+    return FALSE;
+  }
+  if (p - header->buffer + 80 == header->datasize) {
+    fprintf (stderr, "no free space in block, can't insert keyword\n");
+    return FALSE;
+  }
+
+  int start_size = header->datasize;
+
+  long A = time(NULL);
+  srand48(A);
+  
+  int i;
+  for (i = 0; i < 32; i++) {
+    sprintf (&dbID[i], "%1x", (int)(16.0*drand48()));
+  }
+
+  gfits_modify (header, "DVO_DBID", "%s", 1, dbID);      
+  if (start_size != header->datasize) {
+    fprintf (stderr, "error: header buffer grew? (%d to %d bytes)\n", (int) start_size, (int) header->datasize);
+    return FALSE;
+  }
+  return TRUE;
+}
+
Index: trunk/Ohana/src/libfits/header/F_scan.c
===================================================================
--- trunk/Ohana/src/libfits/header/F_scan.c	(revision 34404)
+++ trunk/Ohana/src/libfits/header/F_scan.c	(revision 34405)
@@ -85,4 +85,6 @@
 
   // XXX is this safe for 64bit off_t and 32bit off_t?
+  // XXX the problem is that we read FITS files on many machine types: I need to ensure 
+  // that we are portable -- this sseems inconsistent
   if (!strcmp (mode, "%jd"))  { *va_arg (argp, intmax_t *) 	     = value; return (TRUE); }
 
@@ -220,13 +222,14 @@
   if ((*q == 'd') || (*q == 'D')) value *= pow (10.0, atof (q + 1));
 
-  if (!strcmp (mode, "%d"))   { *va_arg (argp, int *)       	     = value; return (TRUE); }
-  if (!strcmp (mode, "%ld"))  { *va_arg (argp, long *)      	     = value; return (TRUE); }
-  if (!strcmp (mode, OFF_T_FMT)) { *va_arg (argp, long long *) 	     = value; return (TRUE); }
-  if (!strcmp (mode, "%Ld"))  { *va_arg (argp, long long *) 	     = value; return (TRUE); }
-  if (!strcmp (mode, "%u"))   { *va_arg (argp, unsigned *)  	     = value; return (TRUE); }
-  if (!strcmp (mode, "%lu"))  { *va_arg (argp, unsigned long *)      = value; return (TRUE); }
-  if (!strcmp (mode, "%llu")) { *va_arg (argp, unsigned long long *) = value; return (TRUE); }
-  if (!strcmp (mode, "%Lu"))  { *va_arg (argp, unsigned long long *) = value; return (TRUE); }
-  if (!strcmp (mode, "%hd"))  { *va_arg (argp, short *)     	     = value; return (TRUE); }
+  if (!strcmp (mode, "%d"))   	 { *va_arg (argp, int *)       	        = value; return (TRUE); }
+  if (!strcmp (mode, "%ld"))  	 { *va_arg (argp, long *)      	        = value; return (TRUE); }
+  if (!strcmp (mode, OFF_T_FMT)) { *va_arg (argp, off_t *) 	        = value; return (TRUE); }
+  if (!strcmp (mode, "%Ld"))     { *va_arg (argp, long long *) 	        = value; return (TRUE); }
+  if (!strcmp (mode, "%u"))      { *va_arg (argp, unsigned *)  	        = value; return (TRUE); }
+  if (!strcmp (mode, "%lu"))     { *va_arg (argp, unsigned long *)      = value; return (TRUE); }
+  if (!strcmp (mode, "%llu"))    { *va_arg (argp, unsigned long long *) = value; return (TRUE); }
+  if (!strcmp (mode, "%Lu"))     { *va_arg (argp, unsigned long long *) = value; return (TRUE); }
+  if (!strcmp (mode, "%hd"))     { *va_arg (argp, short *)     	        = value; return (TRUE); }
+  if (!strcmp (mode, "%jd"))     { *va_arg (argp, intmax_t *) 	        = value; return (TRUE); }
 
   /* no valid mode found */
Index: trunk/Ohana/src/opihi/dvo/avextract.c
===================================================================
--- trunk/Ohana/src/opihi/dvo/avextract.c	(revision 34404)
+++ trunk/Ohana/src/opihi/dvo/avextract.c	(revision 34405)
@@ -325,5 +325,4 @@
     gprint (GP_ERR, "  <photcode>:nphot : number of measurements used for average magnitude in this photcode\n");
 
-    // gprint (GP_ERR, "  Xp : NOT VALID\n");
     // gprint (GP_ERR, "  type : dophot type (unused)\n");
     // gprint (GP_ERR, "  typefrac : dophot type fraction (unused)\n");
Index: trunk/Ohana/src/opihi/dvo/avmatch.c
===================================================================
--- trunk/Ohana/src/opihi/dvo/avmatch.c	(revision 34404)
+++ trunk/Ohana/src/opihi/dvo/avmatch.c	(revision 34405)
@@ -351,5 +351,4 @@
     gprint (GP_ERR, "  <photcode>:nphot : number of measurements used for average magnitude in this photcode\n");
 
-    // gprint (GP_ERR, "  Xp : NOT VALID\n");
     // gprint (GP_ERR, "  type : dophot type (unused)\n");
     // gprint (GP_ERR, "  typefrac : dophot type fraction (unused)\n");
Index: trunk/Ohana/src/opihi/dvo/gstar.c
===================================================================
--- trunk/Ohana/src/opihi/dvo/gstar.c	(revision 34404)
+++ trunk/Ohana/src/opihi/dvo/gstar.c	(revision 34405)
@@ -190,5 +190,5 @@
 	gprint (GP_LOG, "%5.2f ",   3600.0*sqrt(r));
 	gprint (GP_LOG, "%3d   ",  catalog.average[k].Nmeasure);
-	gprint (GP_LOG, "%4.1f ",  0.01*catalog.average[k].Xp);
+	gprint (GP_LOG, "%4.1f ",  catalog.average[k].ChiSqAve);
 	gprint (GP_LOG, "%5x ",    catalog.average[k].flags);
 	gprint (GP_LOG, "%x ",     catalog.average[k].objID);
@@ -205,5 +205,4 @@
 	    gprint (GP_LOG, "%f   ",     catalog.average[k].dP);
 
-	    gprint (GP_LOG, "%f   ",     catalog.average[k].ChiSqAve);
 	    gprint (GP_LOG, "%f   ",     catalog.average[k].ChiSqPM);
 	    gprint (GP_LOG, "%f   ",     catalog.average[k].ChiSqPar);
@@ -292,5 +291,5 @@
 		gprint (GP_LOG, "0x%08x ", catalog.measure[m].detID);
 		gprint (GP_LOG, "0x%08x ", catalog.measure[m].imageID);
-		gprint (GP_LOG, "%.3f ", catalog.measure[m].psfQual);
+		gprint (GP_LOG, "%.3f ", catalog.measure[m].psfQF);
 		gprint (GP_LOG, "%.1f ", catalog.measure[m].psfChisq);
 		gprint (GP_LOG, "%.1f ", catalog.measure[m].crNsigma);
Index: trunk/Ohana/src/opihi/dvo/photometry.c
===================================================================
--- trunk/Ohana/src/opihi/dvo/photometry.c	(revision 34404)
+++ trunk/Ohana/src/opihi/dvo/photometry.c	(revision 34405)
@@ -226,5 +226,4 @@
   if (!strcasecmp (parname, "Nmeas")) param = AVE_NMEAS;
   if (!strcasecmp (parname, "Nmiss")) param = AVE_NMISS;
-  if (!strcasecmp (parname, "Xp"))    param = AVE_Xp;
   if (!strcasecmp (parname, "Xm"))    param = AVE_Xm;
   if (!strcasecmp (parname, "flag"))  param = AVE_OBJ_FLAGS;
@@ -294,5 +293,5 @@
 void GetAverageParamHelp () {
   gprint (GP_ERR, "value may be one of the following:\n");
-  gprint (GP_ERR, " ra dec dmag Nmeas Nmiss Xm Xp Nphot Ncode flag type typefrac\n\n");
+  gprint (GP_ERR, " ra dec dmag Nmeas Nmiss Xm Nphot Ncode flag type typefrac\n\n");
   gprint (GP_ERR, "value may also be a valid photcode\n");
   gprint (GP_ERR, "photcodes or 'mag' may have optional magnitude mode: mag,[Mave, Mref]\n");
@@ -637,7 +636,4 @@
     case AVE_NMISS:
       value = average[0].Nmissing;
-      break;
-    case AVE_Xp:
-      value = 0.01*average[0].Xp;
       break;
     case AVE_OBJ_FLAGS:
Index: trunk/Ohana/src/relastro/src/BrightCatalog.c
===================================================================
--- trunk/Ohana/src/relastro/src/BrightCatalog.c	(revision 34404)
+++ trunk/Ohana/src/relastro/src/BrightCatalog.c	(revision 34405)
@@ -154,5 +154,5 @@
     GET_COLUMN(Tmean,          "MEAN_EPOCH",  int);
     GET_COLUMN(Trange,         "TIME_RANGE",  int);
-    GET_COLUMN(Xp,             "SIGMA_POS",   float);
+    GET_COLUMN(stargal,        "STARGAL_SEP", float);
     GET_COLUMN(Npos,           "NUMBER_POS",  short);
     GET_COLUMN(Nmeasure,       "NMEASURE",    short);
@@ -189,5 +189,5 @@
       average[i].Tmean           = Tmean[i]           ;
       average[i].Trange          = Trange[i]          ;
-      average[i].Xp              = Xp[i]              ;
+      average[i].stargal         = stargal[i]         ;
       average[i].Npos            = Npos[i]            ;
       average[i].Nmeasure        = Nmeasure[i]        ;
@@ -221,5 +221,5 @@
     free (Tmean);
     free (Trange);
-    free (Xp);
+    free (stargal);
     free (Npos);
     free (Nmeasure);
@@ -487,5 +487,5 @@
     int      *Tmean         ; ALLOCATE (Tmean         , int     , catalog->Naverage);
     int      *Trange        ; ALLOCATE (Trange        , int     , catalog->Naverage);
-    float    *Xp            ; ALLOCATE (Xp            , float   , catalog->Naverage);
+    float    *stargal       ; ALLOCATE (stargal       , float   , catalog->Naverage);
     short    *Npos          ; ALLOCATE (Npos          , short   , catalog->Naverage);
     short    *Nmeasure      ; ALLOCATE (Nmeasure      , short   , catalog->Naverage);
@@ -520,5 +520,5 @@
       Tmean[i]           = average[i].Tmean           ;
       Trange[i]          = average[i].Trange          ;
-      Xp[i]              = average[i].Xp              ;
+      stargal[i]         = average[i].stargal         ;
       Npos[i]            = average[i].Npos            ;
       Nmeasure[i]        = average[i].Nmeasure        ;
@@ -552,5 +552,5 @@
     gfits_set_bintable_column (&theader, &ftable, "MEAN_EPOCH",  Tmean,           catalog->Naverage);
     gfits_set_bintable_column (&theader, &ftable, "TIME_RANGE",  Trange,          catalog->Naverage);
-    gfits_set_bintable_column (&theader, &ftable, "SIGMA_POS",   Xp,              catalog->Naverage);
+    gfits_set_bintable_column (&theader, &ftable, "STARGAL_SEP", stargal,         catalog->Naverage);
     gfits_set_bintable_column (&theader, &ftable, "NUMBER_POS",  Npos,            catalog->Naverage);
     gfits_set_bintable_column (&theader, &ftable, "NMEASURE",    Nmeasure,        catalog->Naverage);
@@ -582,5 +582,5 @@
     free (Tmean);
     free (Trange);
-    free (Xp);
+    free (stargal);
     free (Npos);
     free (Nmeasure);
Index: trunk/Ohana/src/relastro/src/UpdateObjectOffsets.c
===================================================================
--- trunk/Ohana/src/relastro/src/UpdateObjectOffsets.c	(revision 34404)
+++ trunk/Ohana/src/relastro/src/UpdateObjectOffsets.c	(revision 34405)
@@ -95,5 +95,5 @@
     // RESET (-reset)
     // TimeSelect -time
-    // (note that psfQual is applied rigidly at 0.85, as is the galaxy test)
+    // (note that psfQF is applied rigidly at 0.85, as is the galaxy test)
     // ImagSelect, ImagMin, ImagMax
     // MaxDensityUse, MaxDensityValue
Index: trunk/Ohana/src/relastro/src/UpdateObjects.c
===================================================================
--- trunk/Ohana/src/relastro/src/UpdateObjects.c	(revision 34404)
+++ trunk/Ohana/src/relastro/src/UpdateObjects.c	(revision 34405)
@@ -352,10 +352,7 @@
       catalog[i].average[j].dP  = fit.dp; // parallax error in arcsec
 
-      // Xp is supposed to be the position scatter, not the chisq : fix this:
-      // catalog[i].average[j].Xp  = (fit.Nfit > 1) ? 100.0*log10(fit.chisq) : NAN_S_SHORT;
       catalog[i].average[j].ChiSqAve  = fitAve.chisq;
       catalog[i].average[j].ChiSqPM   = fitPM.chisq;
       catalog[i].average[j].ChiSqPar  = fitPAR.chisq;
-      catalog[i].average[j].Xp        = 0.0;
       catalog[i].average[j].Tmean = (Tmean * 86400 * 365.26) + T2000;
       catalog[i].average[j].Trange = (Trange * 86400 * 365.26);
Index: trunk/Ohana/src/relastro/src/load_catalogs.c
===================================================================
--- trunk/Ohana/src/relastro/src/load_catalogs.c	(revision 34404)
+++ trunk/Ohana/src/relastro/src/load_catalogs.c	(revision 34405)
@@ -137,5 +137,5 @@
     // RESET (-reset)
     // TimeSelect -time
-    // (note that psfQual is applied rigidly at 0.85, as is the galaxy test)
+    // (note that psfQF is applied rigidly at 0.85, as is the galaxy test)
     // ImagSelect, ImagMin, ImagMax
     // MaxDensityUse, MaxDensityValue
Index: trunk/Ohana/src/relastro/src/relastro_objects.c
===================================================================
--- trunk/Ohana/src/relastro/src/relastro_objects.c	(revision 34404)
+++ trunk/Ohana/src/relastro/src/relastro_objects.c	(revision 34405)
@@ -123,5 +123,5 @@
     // RESET (-reset)
     // TimeSelect -time
-    // (note that psfQual is applied rigidly at 0.85, as is the galaxy test)
+    // (note that psfQF is applied rigidly at 0.85, as is the galaxy test)
     // ImagSelect, ImagMin, ImagMax
     // MaxDensityUse, MaxDensityValue
Index: trunk/Ohana/src/relphot/src/bcatalog.c
===================================================================
--- trunk/Ohana/src/relphot/src/bcatalog.c	(revision 34404)
+++ trunk/Ohana/src/relphot/src/bcatalog.c	(revision 34405)
@@ -117,5 +117,5 @@
 
       // skip garbage measurements
-      if (catalog[0].measure[offset].psfQual < 0.85) { Npsfqf ++; continue; }
+      if (catalog[0].measure[offset].psfQF < 0.85) { Npsfqf ++; continue; }
       if (isnan(catalog[0].measure[offset].M)) { Nnan ++; continue; }
 
Index: trunk/Ohana/src/relphot/src/load_catalogs.c
===================================================================
--- trunk/Ohana/src/relphot/src/load_catalogs.c	(revision 34404)
+++ trunk/Ohana/src/relphot/src/load_catalogs.c	(revision 34405)
@@ -136,5 +136,5 @@
     // TimeSelect -time
     // DophotSelect
-    // (note that psfQual is applied rigidly at 0.85, as is the galaxy test)
+    // (note that psfQF is applied rigidly at 0.85, as is the galaxy test)
     // MAG_LIM
     // SIGMA_LIM
Index: trunk/Ohana/src/relphot/src/relphot_objects.c
===================================================================
--- trunk/Ohana/src/relphot/src/relphot_objects.c	(revision 34404)
+++ trunk/Ohana/src/relphot/src/relphot_objects.c	(revision 34405)
@@ -143,5 +143,5 @@
     // TimeSelect -time
     // DophotSelect
-    // (note that psfQual is applied rigidly at 0.85, as is the galaxy test)
+    // (note that psfQF is applied rigidly at 0.85, as is the galaxy test)
     // MAG_LIM
     // SIGMA_LIM
Index: trunk/Ohana/src/relphot/src/setMrelFinal.c
===================================================================
--- trunk/Ohana/src/relphot/src/setMrelFinal.c	(revision 34404)
+++ trunk/Ohana/src/relphot/src/setMrelFinal.c	(revision 34405)
@@ -237,5 +237,5 @@
 	  if (catalog[0].measure[m].photFlags & code->photomBadMask) goto skip;
 	  if ((catalog[0].measure[m].photcode > 10000) && (catalog[0].measure[m].photcode < 10500)) {
-	    if (catalog[0].measure[m].psfQual < 0.85) goto skip;
+	    if (catalog[0].measure[m].psfQF < 0.85) goto skip;
 	  }
 	}
