Index: trunk/Ohana/src/addstar/src/MatchHeaders.c
===================================================================
--- trunk/Ohana/src/addstar/src/MatchHeaders.c	(revision 37727)
+++ trunk/Ohana/src/addstar/src/MatchHeaders.c	(revision 37729)
@@ -64,4 +64,5 @@
     if (!strcmp (exttype, "PS1_SV2")) goto keep;
     if (!strcmp (exttype, "PS1_SV3")) goto keep;
+    if (!strcmp (exttype, "PS1_SV4")) goto keep;
 
     if (!strcmp (exttype, "PS1_DV1")) goto keep;
Index: trunk/Ohana/src/addstar/src/ReadStarsFITS.c
===================================================================
--- trunk/Ohana/src/addstar/src/ReadStarsFITS.c	(revision 37727)
+++ trunk/Ohana/src/addstar/src/ReadStarsFITS.c	(revision 37729)
@@ -14,8 +14,10 @@
 Stars     *Convert_PS1_V5	  PROTO((FTable *table, unsigned int *nstars));
 Stars     *Convert_PS1_V5_Lensing PROTO((FTable *table, unsigned int *nstars));
+Stars     *Convert_PS1_V5_Lensing_Alt 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));
 Stars     *Convert_PS1_SV3	  PROTO((FTable *table, unsigned int *nstars));
+Stars     *Convert_PS1_SV4	  PROTO((FTable *table, unsigned int *nstars));
 Stars     *Convert_PS1_DV3        PROTO((FTable *table, unsigned int *nstars));
 Stars     *Convert_PS1_DV4        PROTO((FTable *table, unsigned int *nstars));
@@ -78,8 +80,17 @@
   }
   if (!strcmp (type, "PS1_V5")) {
-    if (table.header[0].Naxis[0] == 312) {
-      stars = Convert_PS1_V5_Lensing (&table, &Nstars);
-    } else {
-      stars = Convert_PS1_V5 (&table, &Nstars);
+    switch (table.header[0].Naxis[0]) {
+      case 232:
+	stars = Convert_PS1_V5 (&table, &Nstars);
+	break;
+      case 312:
+	stars = Convert_PS1_V5_Lensing_Alt (&table, &Nstars);
+	break;
+      case 320:
+	stars = Convert_PS1_V5_Lensing (&table, &Nstars);
+	break;
+      default:
+	fprintf (stderr, "invalid PS1_V5 table size %d\n", (int) table.header[0].Naxis[0]);
+	return NULL;
     }
   }
@@ -92,4 +103,7 @@
   if (!strcmp (type, "PS1_SV3")) {
     stars = Convert_PS1_SV3 (&table, &Nstars);
+  }
+  if (!strcmp (type, "PS1_SV4")) {
+    stars = Convert_PS1_SV4 (&table, &Nstars);
   }
   if (!strcmp (type, "PS1_DV3")) {
@@ -971,4 +985,129 @@
 }
 
+Stars *Convert_PS1_V5_Lensing_Alt (FTable *table, unsigned int *nstars) {
+
+  off_t Nstars; 
+  unsigned int i;
+  double ZeroPt;
+  Stars *stars;
+  CMF_PS1_V5_Lensing_Alt *ps1data;
+
+  ps1data = gfits_table_get_CMF_PS1_V5_Lensing_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.dMap       = (ps1data[i].apFlux > 0.0) ? fabs(ps1data[i].apFluxErr / ps1data[i].apFlux) : NAN;
+                        
+    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    = GetFluxFromFluxOrMag (ps1data[i].Flux, ps1data[i].M); 
+    stars[i].measure.dFluxPSF   = GetFluxErrFromFluxOrMag (ps1data[i].dFlux, stars[i].measure.FluxPSF, ps1data[i].dM);
+    stars[i].measure.FluxKron   = ps1data[i].kronFlux;
+    stars[i].measure.dFluxKron  = ps1data[i].kronFluxErr;
+    stars[i].measure.FluxAp     = GetFluxFromFluxOrMag (ps1data[i].apFlux, ps1data[i].Map); 
+    stars[i].measure.dFluxAp    = GetFluxErrFromFluxOrMag (ps1data[i].apFluxErr, stars[i].measure.FluxAp, stars[i].measure.dMap);
+
+    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.psfQFperf  = ps1data[i].psfQFperf;
+
+    stars[i].measure.psfNdof    = ps1data[i].psfNdof;
+    stars[i].measure.psfNpix    = ps1data[i].psfNpix;
+    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;
+    stars[i].measure.photFlags2 = ps1data[i].flags2;
+
+    // this is may optionally be replaced by the internal sequence (see FilterStars.c)
+    stars[i].measure.detID      = ps1data[i].detID; 
+
+    ALLOCATE (stars[i].lensing, Lensing, 1);
+    dvo_lensing_init (stars[i].lensing);
+
+    stars[i].lensing->X11_sm_obj  = ps1data[i].X11_sm_obj;
+    stars[i].lensing->X12_sm_obj  = ps1data[i].X12_sm_obj;
+    stars[i].lensing->X22_sm_obj  = ps1data[i].X22_sm_obj;
+    stars[i].lensing->E1_sm_obj   = ps1data[i].E1_sm_obj;
+    stars[i].lensing->E2_sm_obj   = ps1data[i].E2_sm_obj;
+
+    stars[i].lensing->X11_sh_obj  = ps1data[i].X11_sh_obj;
+    stars[i].lensing->X12_sh_obj  = ps1data[i].X12_sh_obj;
+    stars[i].lensing->X22_sh_obj  = ps1data[i].X22_sh_obj;
+    stars[i].lensing->E1_sh_obj   = ps1data[i].E1_sh_obj;
+    stars[i].lensing->E2_sh_obj   = ps1data[i].E2_sh_obj;
+
+    stars[i].lensing->X11_sm_psf  = ps1data[i].X11_sm_psf;
+    stars[i].lensing->X12_sm_psf  = ps1data[i].X12_sm_psf;
+    stars[i].lensing->X22_sm_psf  = ps1data[i].X22_sm_psf;
+    stars[i].lensing->E1_sm_psf   = ps1data[i].E1_sm_psf;
+    stars[i].lensing->E2_sm_psf   = ps1data[i].E2_sm_psf;
+
+    stars[i].lensing->X11_sh_psf  = ps1data[i].X11_sh_psf;
+    stars[i].lensing->X12_sh_psf  = ps1data[i].X12_sh_psf;
+    stars[i].lensing->X22_sh_psf  = ps1data[i].X22_sh_psf;
+    stars[i].lensing->E1_sh_psf   = ps1data[i].E1_sh_psf;
+    stars[i].lensing->E2_sh_psf   = ps1data[i].E2_sh_psf;
+
+    // stars[i].lensing->F_ApR5    = ps1data[i].F_ApR5;
+    // stars[i].lensing->dF_ApR5   = ps1data[i].dF_ApR5;
+    // stars[i].lensing->sF_ApR5   = ps1data[i].sF_ApR5;
+    // stars[i].lensing->fF_ApR5   = ps1data[i].fF_ApR5;
+    // 
+    // stars[i].lensing->F_ApR6    = ps1data[i].F_ApR6;
+    // stars[i].lensing->dF_ApR6   = ps1data[i].dF_ApR6;
+    // stars[i].lensing->sF_ApR6   = ps1data[i].sF_ApR6;
+    // stars[i].lensing->fF_ApR6   = ps1data[i].fF_ApR6;
+
+    // this is may optionally be replaced by the internal sequence (see FilterStars.c)
+    stars[i].lensing->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_SV1 (FTable *table, unsigned int *nstars) {
 
@@ -1304,5 +1443,5 @@
 }
 
-Stars *Convert_PS1_DV3 (FTable *table, unsigned int *nstars) {
+Stars *Convert_PS1_SV4 (FTable *table, unsigned int *nstars) {
 
   off_t Nstars; 
@@ -1310,7 +1449,7 @@
   double ZeroPt;
   Stars *stars;
-  CMF_PS1_DV3 *ps1data;
-
-  ps1data = gfits_table_get_CMF_PS1_DV3 (table, &Nstars, NULL);
+  CMF_PS1_SV4 *ps1data;
+
+  ps1data = gfits_table_get_CMF_PS1_SV4 (table, &Nstars, NULL);
   if (!ps1data) {
     fprintf (stderr, "skipping inconsistent entry\n");
@@ -1342,5 +1481,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    = GetFluxFromFluxOrMag (ps1data[i].Flux, ps1data[i].M); 
@@ -1357,4 +1496,5 @@
     stars[i].measure.psfQF      = ps1data[i].psfQF;
     stars[i].measure.psfQFperf  = ps1data[i].psfQFperf;
+
     stars[i].measure.psfNdof    = ps1data[i].psfNdof;
     stars[i].measure.psfNpix    = ps1data[i].psfNpix;
@@ -1376,9 +1516,6 @@
 
     // 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
@@ -1388,5 +1525,5 @@
 }
 
-Stars *Convert_PS1_DV4 (FTable *table, unsigned int *nstars) {
+Stars *Convert_PS1_DV3 (FTable *table, unsigned int *nstars) {
 
   off_t Nstars; 
@@ -1394,7 +1531,7 @@
   double ZeroPt;
   Stars *stars;
-  CMF_PS1_DV4 *ps1data;
-
-  ps1data = gfits_table_get_CMF_PS1_DV4 (table, &Nstars, NULL);
+  CMF_PS1_DV3 *ps1data;
+
+  ps1data = gfits_table_get_CMF_PS1_DV3 (table, &Nstars, NULL);
   if (!ps1data) {
     fprintf (stderr, "skipping inconsistent entry\n");
@@ -1402,6 +1539,4 @@
   }
   ZeroPt = GetZeroPoint();
-
-  fprintf (stderr, "WARNING: Convert_PS1_DV4 not yet updated to match real format\n");
 
   ALLOCATE (stars, Stars, Nstars);
@@ -1474,4 +1609,90 @@
 }
 
-
-
+Stars *Convert_PS1_DV4 (FTable *table, unsigned int *nstars) {
+
+  off_t Nstars; 
+  unsigned int i;
+  double ZeroPt;
+  Stars *stars;
+  CMF_PS1_DV4 *ps1data;
+
+  ps1data = gfits_table_get_CMF_PS1_DV4 (table, &Nstars, NULL);
+  if (!ps1data) {
+    fprintf (stderr, "skipping inconsistent entry\n");
+    return (NULL);
+  }
+  ZeroPt = GetZeroPoint();
+
+  fprintf (stderr, "WARNING: Convert_PS1_DV4 not yet updated to match real format\n");
+
+  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.dMap       = (ps1data[i].apFlux > 0.0) ? fabs(ps1data[i].apFluxErr / ps1data[i].apFlux) : NAN;
+                        
+    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    = GetFluxFromFluxOrMag (ps1data[i].Flux, ps1data[i].M); 
+    stars[i].measure.dFluxPSF   = GetFluxErrFromFluxOrMag (ps1data[i].dFlux, stars[i].measure.FluxPSF, ps1data[i].dM);
+    stars[i].measure.FluxKron   = ps1data[i].kronFlux;
+    stars[i].measure.dFluxKron  = ps1data[i].kronFluxErr;
+    stars[i].measure.FluxAp     = GetFluxFromFluxOrMag (ps1data[i].apFlux, ps1data[i].Map); 
+    stars[i].measure.dFluxAp    = GetFluxErrFromFluxOrMag (ps1data[i].apFluxErr, stars[i].measure.FluxAp, stars[i].measure.dMap);
+
+    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.psfQFperf  = ps1data[i].psfQFperf;
+    stars[i].measure.psfNdof    = ps1data[i].psfNdof;
+    stars[i].measure.psfNpix    = ps1data[i].psfNpix;
+    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;
+    stars[i].measure.photFlags2 = ps1data[i].flags2;
+
+    // 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/libautocode/def/cmf-ps1-sv4.d
===================================================================
--- trunk/Ohana/src/libautocode/def/cmf-ps1-sv4.d	(revision 37729)
+++ trunk/Ohana/src/libautocode/def/cmf-ps1-sv4.d	(revision 37729)
@@ -0,0 +1,72 @@
+# name of structure type
+STRUCT  CMF_PS1_SV4
+EXTNAME CMF_PS1_SV4
+TYPE    BINTABLE
+SIZE    224
+
+# 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,   	 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 apNpix,         AP_NPIX,           int,      pixels used for aper
+FIELD Mcalib,         CAL_PSF_MAG,       float,    calibrated psf mag,    mags
+FIELD dMcal,          CAL_PSF_MAG_SIG,   float,    zero point scatter,    mags
+
+# this field is in the wrong order (is between DEC and sky in cmf, but breaks byte boundary).
+# I need to move these bytes around on read (fix PS1_V5?)
+FIELD Mpeak,          PEAK_FLUX_AS_MAG,  float,    peak flux as a mag,    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 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 k,              PSF_CORE,          float,    extra PSF parameter,   unitless
+FIELD fwhmMaj,        PSF_FWHM_MAJ,      float,    true fwhm of psf,      pixels
+FIELD fwhmMin,        PSF_FWHM_MIN,      float,    true fwhm (minor),     pixels
+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 nFrames,        N_FRAMES,          short,    images overlapping peak
+FIELD padding,        PADDING,           short,    padding for 8byte records
+FIELD padding2,       PADDING2,          int,      padding for 8byte records
+
Index: trunk/Ohana/src/libautocode/def/cmf-ps1-v5-lensing-alt.d
===================================================================
--- trunk/Ohana/src/libautocode/def/cmf-ps1-v5-lensing-alt.d	(revision 37729)
+++ trunk/Ohana/src/libautocode/def/cmf-ps1-v5-lensing-alt.d	(revision 37729)
@@ -0,0 +1,100 @@
+# name of structure type
+STRUCT  CMF_PS1_V5
+EXTNAME CMF_PS1_V5
+TYPE    BINTABLE
+SIZE    312
+
+# 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,            float,    standard aperture mag, mags
+FIELD MapRaw,         AP_MAG_RAW,        float,    raw aperture mag,      mags
+FIELD apRadius,       AP_MAG_RADIUS,     float,    radius used for aper,  pixels
+FIELD apFlux,         AP_FLUX,           float,    aperture flux,         counts
+FIELD apFluxErr,      AP_FLUX_SIG,       float,    error on ap flux,      counts
+FIELD apNpix,         AP_NPIX,           int,      pixels used by aper,   pixels
+FIELD Mcalib,         CAL_PSF_MAG,       float,    calibrated psf mag,    mags
+FIELD dMcal,          CAL_PSF_MAG_SIG,   float,    zero point scatter,    mags
+
+# this field is in the wrong order (is between DEC and sky in cmf, but breaks byte boundary).
+# I need to move these bytes around on read (fix PS1_V5?)
+FIELD Mpeak,          PEAK_FLUX_AS_MAG,  float,    peak flux as a mag,    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 sky,            SKY,               float,    sky flux,              cnts/sec
+FIELD dSky,           SKY_SIGMA,         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 k,              PSF_CORE,          float,    extra PSF parameter,   unitless
+FIELD fwhmMaj,        PSF_FWHM_MAJ,      float,    true fwhm of psf,      pixels
+FIELD fwhmMin,        PSF_FWHM_MIN,      float,    true fwhm (minor),     pixels
+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 X11_sm_obj,     X11_SM_OBJ,        float,    lensing smear
+FIELD X12_sm_obj,     X12_SM_OBJ,        float,    lensing smear
+FIELD X22_sm_obj,     X22_SM_OBJ,        float,    lensing smear
+FIELD E1_sm_obj,      E1_SM_OBJ,         float,    lensing smear
+FIELD E2_sm_obj,      E2_SM_OBJ,         float,    lensing smear
+
+FIELD X11_sh_obj,     X11_SH_OBJ,        float,    lensing shear
+FIELD X12_sh_obj,     X12_SH_OBJ,        float,    lensing shear
+FIELD X22_sh_obj,     X22_SH_OBJ,        float,    lensing shear
+FIELD E1_sh_obj,      E1_SH_OBJ,         float,    lensing shear
+FIELD E2_sh_obj,      E2_SH_OBJ,         float,    lensing shear
+
+FIELD X11_sm_psf,     X11_SM_PSF,        float,    lensing smear
+FIELD X12_sm_psf,     X12_SM_PSF,        float,    lensing smear
+FIELD X22_sm_psf,     X22_SM_PSF,        float,    lensing smear
+FIELD E1_sm_psf,      E1_SM_PSF,         float,    lensing smear
+FIELD E2_sm_psf,      E2_SM_PSF,         float,    lensing smear
+
+FIELD X11_sh_psf,     X11_SH_PSF,        float,    lensing shear
+FIELD X12_sh_psf,     X12_SH_PSF,        float,    lensing shear
+FIELD X22_sh_psf,     X22_SH_PSF,        float,    lensing shear
+FIELD E1_sh_psf,      E1_SH_PSF,         float,    lensing shear
+FIELD E2_sh_psf,      E2_SH_PSF,         float,    lensing shear
+
+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,         FLAGS2,            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
Index: trunk/Ohana/src/libautocode/def/cmf-ps1-v5-lensing.d
===================================================================
--- trunk/Ohana/src/libautocode/def/cmf-ps1-v5-lensing.d	(revision 37727)
+++ trunk/Ohana/src/libautocode/def/cmf-ps1-v5-lensing.d	(revision 37729)
@@ -3,5 +3,5 @@
 EXTNAME CMF_PS1_V5
 TYPE    BINTABLE
-SIZE    312
+SIZE    320
 
 # elements of data structure / FITS table
@@ -81,4 +81,9 @@
 FIELD E2_sh_psf,      E2_SH_PSF,         float,    lensing shear
 
+FIELD srcChipNum,     SRC_CHIP_NUM,      short,    source chip in warp
+FIELD srcChipX,       SRC_CHIP_X,        short,    source chip x coordinate
+FIELD srcChipY,       SRC_CHIP_Y,        short,    source chip y coordinate
+FIELD padding3,       SRC_CHIP_PAD,      short,    source chip in warp
+
 FIELD Mr1,            MOMENTS_R1,        float,    first radial moment,   pixels
 FIELD Mrh,            MOMENTS_RH,        float,    half radial moment,    pixels^1/2
Index: trunk/Ohana/src/libdvo/Makefile
===================================================================
--- trunk/Ohana/src/libdvo/Makefile	(revision 37727)
+++ trunk/Ohana/src/libdvo/Makefile	(revision 37729)
@@ -41,5 +41,7 @@
 $(DESTINC)/cmf-ps1-v5.h \
 $(DESTINC)/cmf-ps1-sv3.h \
-$(DESTINC)/cmf-ps1-v5-lensing.h
+$(DESTINC)/cmf-ps1-sv4.h \
+$(DESTINC)/cmf-ps1-v5-lensing.h \
+$(DESTINC)/cmf-ps1-v5-lensing-alt.h
 
 INCS = $(DEFS) $(DESTINC)/dvo.h $(DESTINC)/autocode.h $(DESTINC)/dvo_util.h $(DESTINC)/dvodb.h $(DESTINC)/libdvo_astro.h $(DESTINC)/convert.h $(DESTINC)/get_graphdata.h
@@ -92,6 +94,8 @@
 $(SRC)/cmf-ps1-dv3.$(ARCH).o \
 $(SRC)/cmf-ps1-sv3.$(ARCH).o \
+$(SRC)/cmf-ps1-sv4.$(ARCH).o \
 $(SRC)/cmf-ps1-v5.$(ARCH).o \
 $(SRC)/cmf-ps1-v5-lensing.$(ARCH).o \
+$(SRC)/cmf-ps1-v5-lensing-alt.$(ARCH).o \
 $(SRC)/dvo_util.$(ARCH).o \
 $(SRC)/dbBooleanCond.$(ARCH).o		\
Index: trunk/Ohana/src/libdvo/include/cmf-ps1-sv4.h
===================================================================
--- trunk/Ohana/src/libdvo/include/cmf-ps1-sv4.h	(revision 37729)
+++ trunk/Ohana/src/libdvo/include/cmf-ps1-sv4.h	(revision 37729)
@@ -0,0 +1,66 @@
+
+/** STRUCT DEFINITION **/
+typedef struct {
+  unsigned int     detID;                // detection ID                     
+  float            X;                    // x coord (pixels)
+  float            Y;                    // y coord (pixels)
+  float            dX;                   // x coord error (pixels)
+  float            dY;                   // y coord error (pixels)
+  float            posangle;             // Posangle at source (degrees)
+  float            pltscale;             // Plate Scale at source (arcsec/pixel)
+  float            M;                    // inst mags (mags)
+  float            dM;                   // inst mag error (mags)
+  float            Flux;                 // psf flux (counts)
+  float            dFlux;                // psf flux error (counts      )
+  float            Map;                  // standard aperture mag (mags)
+  float            MapRaw;               // raw aperture mag (mags)
+  float            apRadius;             // radius used for fit (pixels)
+  float            apFlux;               // aperture flux (counts)
+  float            apFluxErr;            // error on ap flux (counts)
+  int              apNpix;               // pixels used for aper
+  float            Mcalib;               // calibrated psf mag (mags)
+  float            dMcal;                // zero point scatter (mags)
+  float            Mpeak;                // peak flux as a mag (mags)
+  double           RA;                   // PSF RA coord (degrees)
+  double           DEC;                  // PSF DEC coord (degrees)
+  float            sky;                  // sky flux (cnts/sec)
+  float            dSky;                 // sky flux error (cnts/sec)
+  float            psfChisq;             // psf fit chisq
+  float            crNsigma;             // Nsigma deviations from PSF to CF
+  float            extNsigma;            // Nsigma deviations from PSF to EXT
+  float            fx;                   // psf fit major axis (pixels)
+  float            fy;                   // psf fit minor axis (pixels)
+  float            df;                   // ellipse angle (degrees)
+  float            k;                    // extra PSF parameter (unitless)
+  float            fwhmMaj;              // true fwhm of psf (pixels)
+  float            fwhmMin;              // true fwhm (minor) (pixels)
+  float            psfQF;                // quality factor
+  float            psfQFperf;            // quality factor perfect
+  int              psfNdof;              // psf degrees of freedom
+  int              psfNpix;              // psf number of pixels
+  float            Mxx;                  // second moment X (pixels^2)
+  float            Mxy;                  // second moment Y (pixels^2)
+  float            Myy;                  // second moment XY (pixels^2)
+  float            M3c;                  // third moment cos(t) (pixels^3)
+  float            M3s;                  // third moment sin(t) (pixels^3)
+  float            M4c;                  // fourth moment cos(t) (pixels^4)
+  float            M4s;                  // fourth moment sin(t) (pixels^4)
+  float            Mr1;                  // first radial moment (pixels)
+  float            Mrh;                  // half radial moment (pixels^1/2)
+  float            kronFlux;             // kron flux (counts)
+  float            kronFluxErr;          // kron flux error (counts)
+  float            kronInner;            // kron flux 1<R<2.5 (counts)
+  float            kronOuter;            // kron flux 2.5<R<4 (counts)
+  int              flags;                // analysis flags
+  int              flags2;               // analysis flags (2)
+  short            nFrames;              // images overlapping peak
+  short            padding;              // padding for 8byte records
+  int              padding2;             // padding for 8byte records
+} CMF_PS1_SV4;
+
+CMF_PS1_SV4 *gfits_table_get_CMF_PS1_SV4 (FTable *table, off_t *Ndata, char *swapped);
+int      gfits_table_set_CMF_PS1_SV4 (FTable *ftable, CMF_PS1_SV4 *data, off_t Ndata);
+int      gfits_table_mkheader_CMF_PS1_SV4 (Header *header);
+unsigned char *gfits_convert_CMF_PS1_SV4 (unsigned char *data, off_t size, off_t nitems, char toStruct);
+int      Send_CMF_PS1_SV4 (int device, CMF_PS1_SV4 *data, int Ndata, int copy);
+int      Recv_CMF_PS1_SV4 (int device, CMF_PS1_SV4 **data, int *Ndata);
Index: trunk/Ohana/src/libdvo/include/cmf-ps1-v5-lensing-alt.h
===================================================================
--- trunk/Ohana/src/libdvo/include/cmf-ps1-v5-lensing-alt.h	(revision 37729)
+++ trunk/Ohana/src/libdvo/include/cmf-ps1-v5-lensing-alt.h	(revision 37729)
@@ -0,0 +1,93 @@
+
+/** STRUCT DEFINITION **/
+typedef struct {
+  unsigned int     detID;                // detection ID                     
+  float            X;                    // x coord (pixels)
+  float            Y;                    // y coord (pixels)
+  float            dX;                   // x coord error (pixels)
+  float            dY;                   // y coord error (pixels)
+  float            posangle;             // Posangle at source (degrees)
+  float            pltscale;             // Plate Scale at source (arcsec/pixel)
+  float            M;                    // inst mags (mags)
+  float            dM;                   // inst mag error (mags)
+  float            Flux;                 // psf flux (counts)
+  float            dFlux;                // psf flux error (counts      )
+  float            Map;                  // standard aperture mag (mags)
+  float            MapRaw;               // raw aperture mag (mags)
+  float            apRadius;             // radius used for aper (pixels)
+  float            apFlux;               // aperture flux (counts)
+  float            apFluxErr;            // error on ap flux (counts)
+  int              apNpix;               // pixels used by aper (pixels)
+  float            Mcalib;               // calibrated psf mag (mags)
+  float            dMcal;                // zero point scatter (mags)
+  float            Mpeak;                // peak flux as a mag (mags)
+  double           RA;                   // PSF RA coord (degrees)
+  double           DEC;                  // PSF DEC coord (degrees)
+  float            sky;                  // sky flux (cnts/sec)
+  float            dSky;                 // sky flux error (cnts/sec)
+  float            psfChisq;             // psf fit chisq
+  float            crNsigma;             // Nsigma deviations from PSF to CF
+  float            extNsigma;            // Nsigma deviations from PSF to EXT
+  float            fx;                   // psf fit major axis (pixels)
+  float            fy;                   // psf fit minor axis (pixels)
+  float            df;                   // ellipse angle (degrees)
+  float            k;                    // extra PSF parameter (unitless)
+  float            fwhmMaj;              // true fwhm of psf (pixels)
+  float            fwhmMin;              // true fwhm (minor) (pixels)
+  float            psfQF;                // quality factor
+  float            psfQFperf;            // quality factor perfect
+  int              psfNdof;              // psf degrees of freedom
+  int              psfNpix;              // psf number of pixels
+  float            Mxx;                  // second moment X (pixels^2)
+  float            Mxy;                  // second moment Y (pixels^2)
+  float            Myy;                  // second moment XY (pixels^2)
+  float            M3c;                  // third moment cos(t) (pixels^3)
+  float            M3s;                  // third moment sin(t) (pixels^3)
+  float            M4c;                  // fourth moment cos(t) (pixels^4)
+  float            M4s;                  // fourth moment sin(t) (pixels^4)
+
+  float            X11_sm_obj;           // lensing smear
+  float            X12_sm_obj;           // lensing smear
+  float            X22_sm_obj;           // lensing smear
+  float            E1_sm_obj;            // lensing smear
+  float            E2_sm_obj;            // lensing smear
+
+  float            X11_sh_obj;           // lensing shear
+  float            X12_sh_obj;           // lensing shear
+  float            X22_sh_obj;           // lensing shear
+  float            E1_sh_obj;            // lensing shear
+  float            E2_sh_obj;            // lensing shear
+
+  float            X11_sm_psf;           // lensing smear
+  float            X12_sm_psf;           // lensing smear
+  float            X22_sm_psf;           // lensing smear
+  float            E1_sm_psf;            // lensing smear
+  float            E2_sm_psf;            // lensing smear
+
+  float            X11_sh_psf;           // lensing shear
+  float            X12_sh_psf;           // lensing shear
+  float            X22_sh_psf;           // lensing shear
+  float            E1_sh_psf;            // lensing shear
+  float            E2_sh_psf;            // lensing shear
+
+  float            Mr1;                  // first radial moment (pixels)
+  float            Mrh;                  // half radial moment (pixels^1/2)
+  float            kronFlux;             // kron flux (counts)
+  float            kronFluxErr;          // kron flux error (counts)
+  float            kronInner;            // kron flux 1<R<2.5 (counts)
+  float            kronOuter;            // kron flux 2.5<R<4 (counts)
+  float            skyLimitRad;          // profile to sky limit (radius)
+  float            skyLimitFlux;         // profile to sky limit (flux)
+  float            skyLimitSlope;        // profile to sky limit (slope)
+  int              flags;                // analysis flags
+  int              flags2;               // analysis flags (2)
+  short            nFrames;              // images overlapping peak
+  short            padding;              // padding for 8byte records
+} CMF_PS1_V5_Lensing_Alt;
+
+CMF_PS1_V5_Lensing_Alt *gfits_table_get_CMF_PS1_V5_Lensing_Alt (FTable *table, off_t *Ndata, char *swapped);
+int      gfits_table_set_CMF_PS1_V5_Lensing_Alt (FTable *ftable, CMF_PS1_V5_Lensing_Alt *data, off_t Ndata);
+int      gfits_table_mkheader_CMF_PS1_V5_Lensing_Alt (Header *header);
+int      gfits_convert_CMF_PS1_V5_Lensing_Alt (unsigned char *data, off_t size, off_t nitems, char toStruct);
+int      Send_CMF_PS1_V5_Lensing_Alt (int device, CMF_PS1_V5_Lensing_Alt *data, int Ndata, int copy);
+int      Recv_CMF_PS1_V5_Lensing_Alt (int device, CMF_PS1_V5_Lensing_Alt **data, int *Ndata);
Index: trunk/Ohana/src/libdvo/include/cmf-ps1-v5-lensing.h
===================================================================
--- trunk/Ohana/src/libdvo/include/cmf-ps1-v5-lensing.h	(revision 37727)
+++ trunk/Ohana/src/libdvo/include/cmf-ps1-v5-lensing.h	(revision 37729)
@@ -71,4 +71,8 @@
   float            E2_sh_psf;            // lensing shear
 
+  short            srcChipNum;
+  short            srcChipX;
+  short            srcChipY;
+
   float            Mr1;                  // first radial moment (pixels)
   float            Mrh;                  // half radial moment (pixels^1/2)
Index: trunk/Ohana/src/libdvo/include/dvo.h
===================================================================
--- trunk/Ohana/src/libdvo/include/dvo.h	(revision 37727)
+++ trunk/Ohana/src/libdvo/include/dvo.h	(revision 37729)
@@ -338,6 +338,8 @@
 # include "cmf-ps1-dv3.h"
 # include "cmf-ps1-sv3.h"
+# include "cmf-ps1-sv4.h"
 # include "cmf-ps1-v5.h"
 # include "cmf-ps1-v5-lensing.h"
+# include "cmf-ps1-v5-lensing-alt.h"
 
 typedef struct {
Index: trunk/Ohana/src/libdvo/src/cmf-ps1-sv4.c
===================================================================
--- trunk/Ohana/src/libdvo/src/cmf-ps1-sv4.c	(revision 37729)
+++ trunk/Ohana/src/libdvo/src/cmf-ps1-sv4.c	(revision 37729)
@@ -0,0 +1,376 @@
+# include "dvo.h"
+
+// ST_SIZE is the size of the structure (must be modulo 8 bytes)
+// FT_SIZE is the size of a row in the FITS table (inpt or output)
+# define ST_SIZE 224
+# define FT_SIZE 220
+
+// this function is based on the one generated by the autocode of cmf-ps1-sv4.d
+// this format is identical to PS1_SV3 (only non-psf tables changed)
+unsigned char *gfits_convert_CMF_PS1_SV4 (unsigned char *input, off_t size, off_t nitems, char toStruct) {
+
+  off_t i;
+  unsigned char *byte = NULL;
+  unsigned char *output = NULL;
+
+  if (size != ST_SIZE) { 
+    fprintf (stderr, "WARNING: mismatch in data types CMF_PS1_SV4: "OFF_T_FMT" vs %d\n",  size,  ST_SIZE);
+    return (NULL);
+  }
+
+  /* provide initial values to avoid compiler warnings for non-BYTE_SWAP arch */
+  i = 0;
+
+  // BYTE & WORD swap below is valid for the structure, not the FITS buffer
+  if (toStruct) {
+    // allocate a new output data buffer
+    ALLOCATE (output, unsigned char, nitems*sizeof(CMF_PS1_SV4));
+    unsigned char *inbyte = input;
+    unsigned char *otbyte = output;
+
+    // the data in the input table does not line up with the output structure: copy carefully.
+    for (i = 0; i < nitems; i++, inbyte += FT_SIZE, otbyte += ST_SIZE) {
+      memcpy (&otbyte[  0], &inbyte[  0],  76); // IPP_IDET to CAL_PSF_MAG_SIG
+      memcpy (&otbyte[ 80], &inbyte[ 76],  16); // RA_PSF, DEC_PSF
+      memcpy (&otbyte[ 76], &inbyte[ 92],   8); // PEAK_FLUX_AS_MAG
+      memcpy (&otbyte[ 96], &inbyte[ 96], 124); // SKY to PADDING
+      memset (&otbyte[220], 0, 4); // PADDING2
+    }
+
+    // after we have moved around PEAK_FLUX_AS_MAG, RA_PSF, DEC_PSF, we can swap as needed for this platform
+    byte = output;
+  } else {
+    byte = input;
+  }
+
+# ifdef BYTE_SWAP
+  char tmp = 0;
+  for (i = 0; i < nitems; i++, byte += ST_SIZE) {
+    /** BYTE SWAP **/
+    SWAP_WORD (0);   // IPP_IDET
+    SWAP_WORD (4);   // X_PSF
+    SWAP_WORD (8);   // Y_PSF
+    SWAP_WORD (12);  // X_PSF_SIG
+    SWAP_WORD (16);  // Y_PSF_SIG
+    SWAP_WORD (20);  // POSANGLE
+    SWAP_WORD (24);  // PLTSCALE
+    SWAP_WORD (28);  // PSF_INST_MAG
+    SWAP_WORD (32);  // PSF_INST_MAG_SIG
+    SWAP_WORD (36);  // PSF_INST_FLUX
+    SWAP_WORD (40);  // PSF_INST_FLUX_SIG
+    SWAP_WORD (44);  // AP_MAG
+    SWAP_WORD (48);  // AP_MAG_RAW
+    SWAP_WORD (52);  // AP_MAG_RADIUS
+    SWAP_WORD (56);  // AP_FLUX
+    SWAP_WORD (60);  // AP_FLUX_SIG
+    SWAP_WORD (64);  // AP_NPIX
+    SWAP_WORD (68);  // CAL_PSF_MAG
+    SWAP_WORD (72);  // CAL_PSF_MAG_SIG
+
+    // order in structure
+    SWAP_WORD (76);  // PEAK_FLUX_AS_MAG
+    SWAP_DBLE (80);  // RA_PSF
+    SWAP_DBLE (88);  // DEC_PSF
+
+    // order in FITS table
+    // SWAP_WORD (76);  // RA_PSF
+    // SWAP_DBLE (84);  // DEC_PSF
+    // SWAP_DBLE (92);  // PEAK_FLUX_AS_MAG
+
+    SWAP_WORD (96);  // SKY
+    SWAP_WORD (100); // SKY_SIG
+    SWAP_WORD (104); // PSF_CHISQ
+    SWAP_WORD (108); // CR_NSIGMA
+    SWAP_WORD (112); // EXT_NSIGMA
+    SWAP_WORD (116); // PSF_MAJOR
+    SWAP_WORD (120); // PSF_MINOR
+    SWAP_WORD (124); // PSF_THETA
+    SWAP_WORD (128); // PSF_CORE
+    SWAP_WORD (132); // PSF_FWHM_MAJ
+    SWAP_WORD (136); // PSF_FWHM_MIN
+    SWAP_WORD (140); // PSF_QF
+    SWAP_WORD (144); // PSF_QF_PERFECT
+    SWAP_WORD (148); // PSF_NDOF
+    SWAP_WORD (152); // PSF_NPIX
+    SWAP_WORD (156); // MOMENTS_XX
+    SWAP_WORD (160); // MOMENTS_XY
+    SWAP_WORD (164); // MOMENTS_YY
+    SWAP_WORD (168); // MOMENTS_M3C
+    SWAP_WORD (172); // MOMENTS_M3S
+    SWAP_WORD (176); // MOMENTS_M4C
+    SWAP_WORD (180); // MOMENTS_M4S
+    SWAP_WORD (184); // MOMENTS_R1
+    SWAP_WORD (188); // MOMENTS_RH
+    SWAP_WORD (192); // KRON_FLUX
+    SWAP_WORD (196); // KRON_FLUX_ERR
+    SWAP_WORD (200); // KRON_FLUX_INNER
+    SWAP_WORD (204); // KRON_FLUX_OUTER
+    SWAP_WORD (208); // FLAGS
+    SWAP_WORD (212); // FLAGS2
+    SWAP_BYTE (216); // N_FRAMES
+    SWAP_BYTE (218); // PADDING
+    SWAP_WORD (220); // PADDING2
+  }
+# endif  
+
+  // BYTE & WORD swap below is valid for the structure, not the FITS buffer
+  if (!toStruct) {
+    // allocate a new output data buffer
+    ALLOCATE (output, unsigned char, nitems*FT_SIZE);
+    unsigned char *inbyte = input;
+    unsigned char *otbyte = output;
+
+    // the data in the input table does not line up with the output structure: copy carefully.
+    for (i = 0; i < nitems; i++, inbyte += ST_SIZE, otbyte += FT_SIZE) {
+      memcpy (&otbyte[  0], &inbyte[  0],  76); // IPP_IDET to CAL_PSF_MAG_SIG
+      memcpy (&otbyte[ 76], &inbyte[ 80],  16); // RA_PSF, DEC_PSF
+      memcpy (&otbyte[ 92], &inbyte[ 76],   4); // PEAK_FLUX_AS_MAG
+      memcpy (&otbyte[ 96], &inbyte[ 96], 124); // SKY to PADDING
+    }
+  }
+
+  return (output);
+} 
+
+int gfits_table_scale_storage_CMF_PS1_SV4 (CMF_PS1_SV4 *data, off_t Ndata) {
+
+  off_t i;
+  for (i = 0; i < Ndata; i++) {
+    data[i].detID   -= 0x80000000;
+    data[i].flags   -= 0x80000000;
+    data[i].flags2  -= 0x80000000;
+    data[i].nFrames -= 0x8000;
+  }
+  return TRUE;
+}
+
+/*** add test of EXTNAME and header-defined columns? ***/
+/* return internal structure representation */
+CMF_PS1_SV4 *gfits_table_get_CMF_PS1_SV4 (FTable *ftable, off_t *Ndata, char *swapped) {
+
+  int Ncols;
+  CMF_PS1_SV4 *data;
+
+  Ncols = ftable[0].header[0].Naxis[0];
+  if (Ncols != FT_SIZE) {
+    fprintf (stderr, "ERROR: mis-match in table size: width is %d but should be %d bytes\n", Ncols, FT_SIZE);
+    return NULL;
+  }
+
+  *Ndata = ftable[0].header[0].Naxis[1];
+  data = (CMF_PS1_SV4 *) ftable[0].buffer;
+  if ((swapped == NULL) || (*swapped == FALSE)) {
+    unsigned char *newdata = gfits_convert_CMF_PS1_SV4 ((unsigned char *) data, sizeof (CMF_PS1_SV4), *Ndata, TRUE);
+    if (!newdata) return NULL;
+    free (ftable[0].buffer);
+    ftable[0].buffer = (char *) newdata;
+    data = (CMF_PS1_SV4 *) ftable[0].buffer;
+    gfits_table_scale_storage_CMF_PS1_SV4 (data, *Ndata);
+    if (swapped != NULL) *swapped = TRUE;
+  }
+  return (data);
+}
+
+int gfits_table_set_CMF_PS1_SV4 (FTable *ftable, CMF_PS1_SV4 *data, off_t Ndata) {
+
+  Header *header;
+
+  header = ftable[0].header;
+
+  /* create table header */
+  if (!gfits_create_table_header (header, "BINTABLE", "CMF_PS1_SV4")) return (FALSE);
+
+  /* define table layout */
+  /** TABLE DEFINITION **/
+  gfits_define_bintable_column (header, "J",    "IPP_IDET",         "detection ID                     ", "",                  1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "X_PSF",            "x coord",                         "pixels",            1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "Y_PSF",            "y coord",                         "pixels",            1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "X_PSF_SIG",        "x coord error",                   "pixels",            1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "Y_PSF_SIG",        "y coord error",                   "pixels",            1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "POSANGLE",         "Posangle at source",              "degrees",           1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "PLTSCALE",         "Plate Scale at source",           "arcsec/pixel",      1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "PSF_INST_MAG",     "inst mags",                       "mags",              1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "PSF_INST_MAG_SIG", "inst mag error",                  "mags",              1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "PSF_INST_FLUX",    "psf flux",                        "counts",            1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "PSF_INST_FLUX_SIG", "psf flux error",                  "counts      ",      1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "AP_MAG",           "standard aperture mag",           "mags",              1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "AP_MAG_RAW",       "raw aperture mag",                "mags",              1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "AP_MAG_RADIUS",    "radius used for fit",             "pixels",            1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "AP_FLUX",          "aperture flux",                   "counts",            1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "AP_FLUX_SIG",      "error on ap flux",                "counts",            1.0, 0.0);
+  gfits_define_bintable_column (header, "J",    "AP_NPIX",          "pixels used for aper",            "",                  1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "CAL_PSF_MAG",      "calibrated psf mag",              "mags",              1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "CAL_PSF_MAG_SIG",  "zero point scatter",              "mags",              1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "PEAK_FLUX_AS_MAG", "peak flux as a mag",              "mags",              1.0, 0.0);
+  gfits_define_bintable_column (header, "D",    "RA_PSF",           "PSF RA coord",                    "degrees",           1.0, 0.0);
+  gfits_define_bintable_column (header, "D",    "DEC_PSF",          "PSF DEC coord",                   "degrees",           1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "SKY",              "sky flux",                        "cnts/sec",          1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "SKY_SIG",          "sky flux error",                  "cnts/sec",          1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "PSF_CHISQ",        "psf fit chisq",                   "",                  1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "CR_NSIGMA",        "Nsigma deviations from PSF to CF", "",                  1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "EXT_NSIGMA",       "Nsigma deviations from PSF to EXT", "",                  1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "PSF_MAJOR",        "psf fit major axis",              "pixels",            1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "PSF_MINOR",        "psf fit minor axis",              "pixels",            1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "PSF_THETA",        "ellipse angle",                   "degrees",           1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "PSF_CORE",         "extra PSF parameter",             "unitless",          1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "PSF_FWHM_MAJ",     "true fwhm of psf",                "pixels",            1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "PSF_FWHM_MIN",     "true fwhm (minor)",               "pixels",            1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "PSF_QF",           "quality factor",                  "",                  1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "PSF_QF_PERFECT",   "quality factor perfect",          "",                  1.0, 0.0);
+  gfits_define_bintable_column (header, "J",    "PSF_NDOF",         "psf degrees of freedom",          "",                  1.0, 0.0);
+  gfits_define_bintable_column (header, "J",    "PSF_NPIX",         "psf number of pixels",            "",                  1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "MOMENTS_XX",       "second moment X",                 "pixels^2",          1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "MOMENTS_XY",       "second moment Y",                 "pixels^2",          1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "MOMENTS_YY",       "second moment XY",                "pixels^2",          1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "MOMENTS_M3C",      "third moment cos(t)",             "pixels^3",          1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "MOMENTS_M3S",      "third moment sin(t)",             "pixels^3",          1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "MOMENTS_M4C",      "fourth moment cos(t)",            "pixels^4",          1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "MOMENTS_M4S",      "fourth moment sin(t)",            "pixels^4",          1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "MOMENTS_R1",       "first radial moment",             "pixels",            1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "MOMENTS_RH",       "half radial moment",              "pixels^1/2",        1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "KRON_FLUX",        "kron flux",                       "counts",            1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "KRON_FLUX_ERR",    "kron flux error",                 "counts",            1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "KRON_FLUX_INNER",  "kron flux 1<R<2.5",               "counts",            1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "KRON_FLUX_OUTER",  "kron flux 2.5<R<4",               "counts",            1.0, 0.0);
+  gfits_define_bintable_column (header, "J",    "FLAGS",            "analysis flags",                  "",                  1.0, 0.0);
+  gfits_define_bintable_column (header, "J",    "FLAGS2",           "analysis flags (2)",              "",                  1.0, 0.0);
+  gfits_define_bintable_column (header, "I",    "N_FRAMES",         "images overlapping peak",         "",                  1.0, 0.0);
+  gfits_define_bintable_column (header, "I",    "PADDING",          "padding for 8byte records",       "",                  1.0, 0.0);
+  gfits_define_bintable_column (header, "J",    "PADDING2",         "padding for 8byte records",       "",                  1.0, 0.0);
+
+  /* create table */
+  if (!gfits_create_table (header, ftable)) return (FALSE);
+
+  /* add data values */
+  if (!gfits_table_scale_data (ftable)) return (FALSE);
+  unsigned char *newdata = gfits_convert_CMF_PS1_SV4 ((unsigned char *) data, sizeof (CMF_PS1_SV4), Ndata, FALSE);
+  if (!newdata) return (FALSE);
+  free (ftable[0].buffer);
+  ftable[0].buffer = (char *) newdata;
+  if (!gfits_add_rows (ftable, (char *) newdata, Ndata, sizeof (CMF_PS1_SV4))) return (FALSE);
+
+  return (TRUE);
+}
+
+int gfits_table_mkheader_CMF_PS1_SV4 (Header *header) {
+
+  /* create table header */
+  if (!gfits_create_table_header (header, "BINTABLE", "CMF_PS1_SV4")) return (FALSE);
+
+  /* define table layout */
+  /** TABLE DEFINITION **/
+  gfits_define_bintable_column (header, "J",    "IPP_IDET",         "detection ID                     ", "",                  1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "X_PSF",            "x coord",                         "pixels",            1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "Y_PSF",            "y coord",                         "pixels",            1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "X_PSF_SIG",        "x coord error",                   "pixels",            1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "Y_PSF_SIG",        "y coord error",                   "pixels",            1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "POSANGLE",         "Posangle at source",              "degrees",           1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "PLTSCALE",         "Plate Scale at source",           "arcsec/pixel",      1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "PSF_INST_MAG",     "inst mags",                       "mags",              1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "PSF_INST_MAG_SIG", "inst mag error",                  "mags",              1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "PSF_INST_FLUX",    "psf flux",                        "counts",            1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "PSF_INST_FLUX_SIG", "psf flux error",                  "counts      ",      1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "AP_MAG",           "standard aperture mag",           "mags",              1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "AP_MAG_RAW",       "raw aperture mag",                "mags",              1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "AP_MAG_RADIUS",    "radius used for fit",             "pixels",            1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "AP_FLUX",          "aperture flux",                   "counts",            1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "AP_FLUX_SIG",      "error on ap flux",                "counts",            1.0, 0.0);
+  gfits_define_bintable_column (header, "J",    "AP_NPIX",          "pixels used for aper",            "",                  1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "CAL_PSF_MAG",      "calibrated psf mag",              "mags",              1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "CAL_PSF_MAG_SIG",  "zero point scatter",              "mags",              1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "PEAK_FLUX_AS_MAG", "peak flux as a mag",              "mags",              1.0, 0.0);
+  gfits_define_bintable_column (header, "D",    "RA_PSF",           "PSF RA coord",                    "degrees",           1.0, 0.0);
+  gfits_define_bintable_column (header, "D",    "DEC_PSF",          "PSF DEC coord",                   "degrees",           1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "SKY",              "sky flux",                        "cnts/sec",          1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "SKY_SIG",          "sky flux error",                  "cnts/sec",          1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "PSF_CHISQ",        "psf fit chisq",                   "",                  1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "CR_NSIGMA",        "Nsigma deviations from PSF to CF", "",                  1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "EXT_NSIGMA",       "Nsigma deviations from PSF to EXT", "",                  1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "PSF_MAJOR",        "psf fit major axis",              "pixels",            1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "PSF_MINOR",        "psf fit minor axis",              "pixels",            1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "PSF_THETA",        "ellipse angle",                   "degrees",           1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "PSF_CORE",         "extra PSF parameter",             "unitless",          1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "PSF_FWHM_MAJ",     "true fwhm of psf",                "pixels",            1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "PSF_FWHM_MIN",     "true fwhm (minor)",               "pixels",            1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "PSF_QF",           "quality factor",                  "",                  1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "PSF_QF_PERFECT",   "quality factor perfect",          "",                  1.0, 0.0);
+  gfits_define_bintable_column (header, "J",    "PSF_NDOF",         "psf degrees of freedom",          "",                  1.0, 0.0);
+  gfits_define_bintable_column (header, "J",    "PSF_NPIX",         "psf number of pixels",            "",                  1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "MOMENTS_XX",       "second moment X",                 "pixels^2",          1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "MOMENTS_XY",       "second moment Y",                 "pixels^2",          1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "MOMENTS_YY",       "second moment XY",                "pixels^2",          1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "MOMENTS_M3C",      "third moment cos(t)",             "pixels^3",          1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "MOMENTS_M3S",      "third moment sin(t)",             "pixels^3",          1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "MOMENTS_M4C",      "fourth moment cos(t)",            "pixels^4",          1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "MOMENTS_M4S",      "fourth moment sin(t)",            "pixels^4",          1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "MOMENTS_R1",       "first radial moment",             "pixels",            1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "MOMENTS_RH",       "half radial moment",              "pixels^1/2",        1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "KRON_FLUX",        "kron flux",                       "counts",            1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "KRON_FLUX_ERR",    "kron flux error",                 "counts",            1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "KRON_FLUX_INNER",  "kron flux 1<R<2.5",               "counts",            1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "KRON_FLUX_OUTER",  "kron flux 2.5<R<4",               "counts",            1.0, 0.0);
+  gfits_define_bintable_column (header, "J",    "FLAGS",            "analysis flags",                  "",                  1.0, 0.0);
+  gfits_define_bintable_column (header, "J",    "FLAGS2",           "analysis flags (2)",              "",                  1.0, 0.0);
+  gfits_define_bintable_column (header, "I",    "N_FRAMES",         "images overlapping peak",         "",                  1.0, 0.0);
+  gfits_define_bintable_column (header, "I",    "PADDING",          "padding for 8byte records",       "",                  1.0, 0.0);
+  gfits_define_bintable_column (header, "J",    "PADDING2",         "padding for 8byte records",       "",                  1.0, 0.0);
+
+  return (TRUE);
+}
+
+int Send_CMF_PS1_SV4 (int device, CMF_PS1_SV4 *data, int Ndata, int copy) {
+
+  int Nwrite, Nbytes;
+  CMF_PS1_SV4 *tmpdata;
+
+  Nbytes = Ndata * sizeof (CMF_PS1_SV4);
+
+  if (copy) {
+    ALLOCATE (tmpdata, CMF_PS1_SV4, Ndata);
+    memcpy (tmpdata, data, Nbytes);
+  } else {
+    tmpdata = data;
+  }
+
+  unsigned char *newdata = gfits_convert_CMF_PS1_SV4 ((unsigned char *) tmpdata, sizeof (CMF_PS1_SV4), Ndata, FALSE);
+  if (!newdata) return FALSE;
+
+  SendCommand (device, 16, "NVALUE: %6d", Ndata);
+  SendCommand (device, 16, "NBYTES: %6d", Nbytes);
+  Nwrite = write (device, newdata, Nbytes);
+  if (Nwrite != Nbytes) {
+    return (FALSE);
+  }
+  
+  /* perform handshaking? */
+
+  return (TRUE);
+}
+
+int Recv_CMF_PS1_SV4 (int device, CMF_PS1_SV4 **data, int *Ndata) {
+
+  int ndata;
+  IOBuffer message;
+  CMF_PS1_SV4 *tmpdata;
+
+  ExpectCommand (device, 16, 1.0, &message);
+  sscanf (message.buffer, "%*s %d", &ndata);
+  FreeIOBuffer (&message);
+  
+  /* what is reasonable for timeout? */
+  ExpectMessage (device, 1.0, &message);
+  
+  tmpdata = (CMF_PS1_SV4 *) message.buffer;
+  unsigned char *newdata = gfits_convert_CMF_PS1_SV4 ((unsigned char *) tmpdata, sizeof (CMF_PS1_SV4), ndata, TRUE);
+  if (!newdata) return FALSE;
+  free (message.buffer);
+
+  /* double-check data length? */
+  /* perform handshaking? */
+
+  *Ndata = ndata;
+  *data = (CMF_PS1_SV4 *) newdata;
+
+  return (TRUE);
+}
+
Index: trunk/Ohana/src/libdvo/src/cmf-ps1-v5-lensing-alt.c
===================================================================
--- trunk/Ohana/src/libdvo/src/cmf-ps1-v5-lensing-alt.c	(revision 37729)
+++ trunk/Ohana/src/libdvo/src/cmf-ps1-v5-lensing-alt.c	(revision 37729)
@@ -0,0 +1,456 @@
+# include "dvo.h"
+# define ST_SIZE 312
+
+// this function is based on the one generated by the autocode of cmf-ps1-v5.d
+int gfits_convert_CMF_PS1_V5_Lensing_Alt (unsigned char *data, off_t size, off_t nitems, char toStruct) {
+
+  off_t i;
+  unsigned char tmp;
+
+  if (size != ST_SIZE) { 
+    fprintf (stderr, "WARNING: mismatch in data types CMF_PS1_V5_Lensing_Alt: "OFF_T_FMT" vs %d\n",  size,  ST_SIZE);
+    return (FALSE);
+  }
+
+  /* provide initial values to avoid compiler warnings for non-BYTE_SWAP arch */
+  i = tmp = 0;
+
+  // allocate a temporary output 
+  unsigned char tmpdata[64];
+
+  // move the bytes corresponding to PEAK_FLUX_AS_MAG, RA_PSF, DEC_PSF
+  if (toStruct) {
+    unsigned char *row = data;
+    for (i = 0; i < nitems; i++, row += size) {
+      memcpy  (tmpdata, &row[92], 4); // save PEAK_FLUX_AS_MAG (92-95)
+      memmove (&row[80], &row[76], 16); // move RA_PSF & DEC_PSF
+      memcpy  (&row[76], tmpdata, 4); // replace PEAK_FLUX_AS_MAG
+    }
+  }
+
+  // after we have moved around PEAK_FLUX_AS_MAG, RA_PSF, DEC_PSF, we can swap as needed for this platform
+  unsigned char *byte = data;
+
+# ifdef BYTE_SWAP
+  byte = (unsigned char *) data;
+  for (i = 0; i < nitems; i++, byte += ST_SIZE) {
+    /** BYTE SWAP **/
+    SWAP_WORD (0);   // IPP_IDET                     
+    SWAP_WORD (4);   // X_PSF
+    SWAP_WORD (8);   // Y_PSF
+    SWAP_WORD (12);  // X_PSF_SIG
+    SWAP_WORD (16);  // Y_PSF_SIG
+    SWAP_WORD (20);  // POSANGLE
+    SWAP_WORD (24);  // PLTSCALE
+    SWAP_WORD (28);  // PSF_INST_MAG
+    SWAP_WORD (32);  // PSF_INST_MAG_SIG
+    SWAP_WORD (36);  // PSF_INST_FLUX
+    SWAP_WORD (40);  // PSF_INST_FLUX_SIG
+    SWAP_WORD (44);  // AP_MAG
+    SWAP_WORD (48);  // AP_MAG_RAW
+    SWAP_WORD (52);  // AP_MAG_RADIUS
+    SWAP_WORD (56);  // AP_FLUX
+    SWAP_WORD (60);  // AP_FLUX_SIG
+    SWAP_WORD (64);  // AP_NPIX
+    SWAP_WORD (68);  // CAL_PSF_MAG
+    SWAP_WORD (72);  // CAL_PSF_MAG_SIG
+    SWAP_WORD (76);  // PEAK_FLUX_AS_MAG
+    SWAP_DBLE (80);  // RA_PSF
+    SWAP_DBLE (88);  // DEC_PSF
+    SWAP_WORD (96);  // SKY
+    SWAP_WORD (100); // SKY_SIGMA
+    SWAP_WORD (104); // PSF_CHISQ
+    SWAP_WORD (108); // CR_NSIGMA
+    SWAP_WORD (112); // EXT_NSIGMA
+    SWAP_WORD (116); // PSF_MAJOR
+    SWAP_WORD (120); // PSF_MINOR
+    SWAP_WORD (124); // PSF_THETA
+    SWAP_WORD (128); // PSF_CORE
+    SWAP_WORD (132); // PSF_FWHM_MAJ
+    SWAP_WORD (136); // PSF_FWHM_MIN
+    SWAP_WORD (140); // PSF_QF
+    SWAP_WORD (144); // PSF_QF_PERFECT
+    SWAP_WORD (148); // PSF_NDOF
+    SWAP_WORD (152); // PSF_NPIX
+    SWAP_WORD (156); // MOMENTS_XX
+    SWAP_WORD (160); // MOMENTS_XY
+    SWAP_WORD (164); // MOMENTS_YY
+    SWAP_WORD (168); // MOMENTS_M3C
+    SWAP_WORD (172); // MOMENTS_M3S
+    SWAP_WORD (176); // MOMENTS_M4C
+    SWAP_WORD (180); // MOMENTS_M4S
+    SWAP_WORD (184); // X11_SM_OBJ
+    SWAP_WORD (188); // X12_SM_OBJ
+    SWAP_WORD (192); // X22_SM_OBJ
+    SWAP_WORD (196); // E1_SM_OBJ
+    SWAP_WORD (200); // E2_SM_OBJ
+    SWAP_WORD (204); // X11_SH_OBJ
+    SWAP_WORD (208); // X12_SH_OBJ
+    SWAP_WORD (212); // X22_SH_OBJ
+    SWAP_WORD (216); // E1_SH_OBJ
+    SWAP_WORD (220); // E2_SH_OBJ
+    SWAP_WORD (224); // X11_SM_PSF
+    SWAP_WORD (228); // X12_SM_PSF
+    SWAP_WORD (232); // X22_SM_PSF
+    SWAP_WORD (236); // E1_SM_PSF
+    SWAP_WORD (240); // E2_SM_PSF
+    SWAP_WORD (244); // X11_SH_PSF
+    SWAP_WORD (248); // X12_SH_PSF
+    SWAP_WORD (252); // X22_SH_PSF
+    SWAP_WORD (256); // E1_SH_PSF
+    SWAP_WORD (260); // E2_SH_PSF
+    SWAP_WORD (264); // MOMENTS_R1
+    SWAP_WORD (268); // MOMENTS_RH
+    SWAP_WORD (272); // KRON_FLUX
+    SWAP_WORD (276); // KRON_FLUX_ERR
+    SWAP_WORD (280); // KRON_FLUX_INNER
+    SWAP_WORD (284); // KRON_FLUX_OUTER
+    SWAP_WORD (288); // SKY_LIMIT_RAD
+    SWAP_WORD (292); // SKY_LIMIT_FLUX
+    SWAP_WORD (296); // SKY_LIMIT_SLOPE
+    SWAP_WORD (300); // FLAGS
+    SWAP_WORD (304); // FLAGS2
+    SWAP_BYTE (308); // N_FRAMES
+    SWAP_BYTE (310); // PADDING                       
+  }
+# endif  
+
+  // move the bytes corresponding to PEAK_FLUX_AS_MAG, RA_PSF, DEC_PSF
+  if (!toStruct) {
+    unsigned char *row = data;
+    for (i = 0; i < nitems; i++, row += size) {
+      memcpy  (tmpdata, &row[76], 4); // save PEAK_FLUX_AS_MAG (92-95)
+      memmove (&row[76], &row[80], 16); // move RA_PSF & DEC_PSF
+      memcpy  (&row[92], tmpdata, 4); // replace PEAK_FLUX_AS_MAG
+    }
+  }
+
+  return (TRUE);
+} 
+
+// data organization (input vs output)
+//                FITS               struct
+// WORD   (0); // IPP_IDET           IPP_IDET           / 1J                  
+// WORD   (4); // X_PSF              X_PSF              / 1E                  
+// WORD   (8); // Y_PSF              Y_PSF              / 1E                  
+// WORD  (12); // X_PSF_SIG          X_PSF_SIG          / 1E                  
+// WORD  (16); // Y_PSF_SIG          Y_PSF_SIG          / 1E                  
+// WORD  (20); // POSANGLE           POSANGLE           / 1E                  
+// WORD  (24); // PLTSCALE           PLTSCALE           / 1E                  
+// WORD  (28); // PSF_INST_MAG       PSF_INST_MAG       / 1E                  
+// WORD  (32); // PSF_INST_MAG_SIG   PSF_INST_MAG_SIG   / 1E                  
+// WORD  (36); // PSF_INST_FLUX      PSF_INST_FLUX      / 1E                  
+// WORD  (40); // PSF_INST_FLUX_SIG  PSF_INST_FLUX_SIG  / 1E                  
+// WORD  (44); // AP_MAG             AP_MAG             / 1E                  
+// WORD  (48); // AP_MAG_RAW         AP_MAG_RAW         / 1E                  
+// WORD  (52); // AP_MAG_RADIUS      AP_MAG_RADIUS      / 1E                  
+// WORD  (56); // AP_FLUX            AP_FLUX            / 1E                  
+// WORD  (60); // AP_FLUX_SIG        AP_FLUX_SIG        / 1E                  
+// WORD  (64); // AP_NPIX            AP_NPIX            / 1J                  
+// WORD  (68); // CAL_PSF_MAG        CAL_PSF_MAG        / 1E                  
+// WORD  (72); // CAL_PSF_MAG_SIG    CAL_PSF_MAG_SIG    / 1E                  
+// WORD  (76); // PEAK_FLUX_AS_MAG   RA_PSF             / 1D                  
+// DBLE  (80); // RA_PSF             DEC_PSF            / 1D                  
+// DBLE  (88); // DEC_PSF            PEAK_FLUX_AS_MAG   / 1E                  
+// WORD  (96); // SKY                SKY                / 1E                  
+// WORD (100); // SKY_SIGMA          SKY_SIGMA          / 1E                  
+// WORD (104); // PSF_CHISQ          PSF_CHISQ          / 1E                  
+// WORD (108); // CR_NSIGMA          CR_NSIGMA          / 1E                  
+// WORD (112); // EXT_NSIGMA         EXT_NSIGMA         / 1E                  
+// WORD (116); // PSF_MAJOR          PSF_MAJOR          / 1E                  
+// WORD (120); // PSF_MINOR          PSF_MINOR          / 1E                  
+// WORD (124); // PSF_THETA          PSF_THETA          / 1E                  
+// WORD (128); // PSF_CORE           PSF_CORE           / 1E                  
+// WORD (132); // PSF_FWHM_MAJ       PSF_FWHM_MAJ       / 1E                  
+// WORD (136); // PSF_FWHM_MIN       PSF_FWHM_MIN       / 1E                  
+// WORD (140); // PSF_QF             PSF_QF             / 1E                  
+// WORD (144); // PSF_QF_PERFECT     PSF_QF_PERFECT     / 1E                  
+// WORD (148); // PSF_NDOF           PSF_NDOF           / 1J                  
+// WORD (152); // PSF_NPIX           PSF_NPIX           / 1J                  
+// WORD (156); // MOMENTS_XX         MOMENTS_XX         / 1E                  
+// WORD (160); // MOMENTS_XY         MOMENTS_XY         / 1E                  
+// WORD (164); // MOMENTS_YY         MOMENTS_YY         / 1E                  
+// WORD (168); // MOMENTS_M3C        MOMENTS_M3C        / 1E                  
+// WORD (172); // MOMENTS_M3S        MOMENTS_M3S        / 1E                  
+// WORD (176); // MOMENTS_M4C        MOMENTS_M4C        / 1E                  
+// WORD (180); // MOMENTS_M4S        MOMENTS_M4S        / 1E                  
+// WORD (184); // MOMENTS_R1         MOMENTS_R1         / 1E                  
+// WORD (188); // MOMENTS_RH         MOMENTS_RH         / 1E                  
+// WORD (192); // KRON_FLUX          KRON_FLUX          / 1E                  
+// WORD (196); // KRON_FLUX_ERR      KRON_FLUX_ERR      / 1E                  
+// WORD (200); // KRON_FLUX_INNER    KRON_FLUX_INNER    / 1E                  
+// WORD (204); // KRON_FLUX_OUTER    KRON_FLUX_OUTER    / 1E                  
+// WORD (208); // SKY_LIMIT_RAD      SKY_LIMIT_RAD      / 1E                  
+// WORD (212); // SKY_LIMIT_FLUX     SKY_LIMIT_FLUX     / 1E                  
+// WORD (216); // SKY_LIMIT_SLOPE    SKY_LIMIT_SLOPE    / 1E                  
+// WORD (220); // FLAGS              FLAGS              / 1J                  
+// WORD (224); // FLAGS2             FLAGS2             / 1J                  
+// BYTE (228); // N_FRAMES           N_FRAMES           / 1I                  
+// BYTE (230); // PADDING            PADDING            / 1I                  
+
+/*** add test of EXTNAME and header-defined columns? ***/
+/* return internal structure representation */
+CMF_PS1_V5_Lensing_Alt *gfits_table_get_CMF_PS1_V5_Lensing_Alt (FTable *ftable, off_t *Ndata, char *swapped) {
+
+  int Ncols;
+  CMF_PS1_V5_Lensing_Alt *data;
+
+  Ncols = ftable[0].header[0].Naxis[0];
+  if (Ncols != ST_SIZE) {
+    fprintf (stderr, "ERROR: mis-match in table size: width is %d but should be %d bytes\n", Ncols, ST_SIZE);
+    return NULL;
+  }
+
+  *Ndata = ftable[0].header[0].Naxis[1];
+  data = (CMF_PS1_V5_Lensing_Alt *) ftable[0].buffer;
+  if ((swapped == NULL) || (*swapped == FALSE)) {
+    if (!gfits_convert_CMF_PS1_V5_Lensing_Alt ((unsigned char *) data, sizeof (CMF_PS1_V5_Lensing_Alt), *Ndata, TRUE)) {
+      return NULL;
+    }
+    gfits_table_scale_data (ftable);
+    if (swapped != NULL) *swapped = TRUE;
+  }
+  return (data);
+}
+
+int gfits_table_set_CMF_PS1_V5_Lensing_Alt (FTable *ftable, CMF_PS1_V5_Lensing_Alt *data, off_t Ndata) {
+
+  Header *header;
+
+  header = ftable[0].header;
+
+  /* create table header */
+  if (!gfits_create_table_header (header, "BINTABLE", "CMF_PS1_V5_Lensing_Alt")) return (FALSE);
+
+  /* define table layout */
+  /** TABLE DEFINITION **/
+  gfits_define_bintable_column (header, "J",    "IPP_IDET",         "detection ID                     ", "",                  1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "X_PSF",            "x coord",                         "pixels",            1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "Y_PSF",            "y coord",                         "pixels",            1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "X_PSF_SIG",        "x coord error",                   "pixels",            1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "Y_PSF_SIG",        "y coord error",                   "pixels",            1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "POSANGLE",         "Posangle at source",              "degrees",           1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "PLTSCALE",         "Plate Scale at source",           "arcsec/pixel",      1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "PSF_INST_MAG",     "inst mags",                       "mags",              1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "PSF_INST_MAG_SIG", "inst mag error",                  "mags",              1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "PSF_INST_FLUX",    "psf flux",                        "counts",            1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "PSF_INST_FLUX_SIG", "psf flux error",                  "counts      ",      1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "AP_MAG",           "standard aperture mag",           "mags",              1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "AP_MAG_RAW",       "raw aperture mag",                "mags",              1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "AP_MAG_RADIUS",    "radius used for aper",            "pixels",            1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "AP_FLUX",          "aperture flux",                   "counts",            1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "AP_FLUX_SIG",      "error on ap flux",                "counts",            1.0, 0.0);
+  gfits_define_bintable_column (header, "J",    "AP_NPIX",          "pixels used by aper",             "pixels",            1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "CAL_PSF_MAG",      "calibrated psf mag",              "mags",              1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "CAL_PSF_MAG_SIG",  "zero point scatter",              "mags",              1.0, 0.0);
+  gfits_define_bintable_column (header, "D",    "RA_PSF",           "PSF RA coord",                    "degrees",           1.0, 0.0);
+  gfits_define_bintable_column (header, "D",    "DEC_PSF",          "PSF DEC coord",                   "degrees",           1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "PEAK_FLUX_AS_MAG", "peak flux as a mag",              "mags",              1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "SKY",              "sky flux",                        "cnts/sec",          1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "SKY_SIGMA",        "sky flux error",                  "cnts/sec",          1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "PSF_CHISQ",        "psf fit chisq",                   "",                  1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "CR_NSIGMA",        "Nsigma deviations from PSF to CF", "",                  1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "EXT_NSIGMA",       "Nsigma deviations from PSF to EXT", "",                  1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "PSF_MAJOR",        "psf fit major axis",              "pixels",            1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "PSF_MINOR",        "psf fit minor axis",              "pixels",            1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "PSF_THETA",        "ellipse angle",                   "degrees",           1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "PSF_CORE",         "extra PSF parameter",             "unitless",          1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "PSF_FWHM_MAJ",     "true fwhm of psf",                "pixels",            1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "PSF_FWHM_MIN",     "true fwhm (minor)",               "pixels",            1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "PSF_QF",           "quality factor",                  "",                  1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "PSF_QF_PERFECT",   "quality factor perfect",          "",                  1.0, 0.0);
+  gfits_define_bintable_column (header, "J",    "PSF_NDOF",         "psf degrees of freedom",          "",                  1.0, 0.0);
+  gfits_define_bintable_column (header, "J",    "PSF_NPIX",         "psf number of pixels",            "",                  1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "MOMENTS_XX",       "second moment X",                 "pixels^2",          1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "MOMENTS_XY",       "second moment Y",                 "pixels^2",          1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "MOMENTS_YY",       "second moment XY",                "pixels^2",          1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "MOMENTS_M3C",      "third moment cos(t)",             "pixels^3",          1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "MOMENTS_M3S",      "third moment sin(t)",             "pixels^3",          1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "MOMENTS_M4C",      "fourth moment cos(t)",            "pixels^4",          1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "MOMENTS_M4S",      "fourth moment sin(t)",            "pixels^4",          1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "X11_SM_OBJ",       "lensing smear",                   "",                  1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "X12_SM_OBJ",       "lensing smear",                   "",                  1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "X22_SM_OBJ",       "lensing smear",                   "",                  1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "E1_SM_OBJ",        "lensing smear",                   "",                  1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "E2_SM_OBJ",        "lensing smear",                   "",                  1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "X11_SH_OBJ",       "lensing shear",                   "",                  1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "X12_SH_OBJ",       "lensing shear",                   "",                  1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "X22_SH_OBJ",       "lensing shear",                   "",                  1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "E1_SH_OBJ",        "lensing shear",                   "",                  1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "E2_SH_OBJ",        "lensing shear",                   "",                  1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "X11_SM_PSF",       "lensing smear",                   "",                  1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "X12_SM_PSF",       "lensing smear",                   "",                  1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "X22_SM_PSF",       "lensing smear",                   "",                  1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "E1_SM_PSF",        "lensing smear",                   "",                  1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "E2_SM_PSF",        "lensing smear",                   "",                  1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "X11_SH_PSF",       "lensing shear",                   "",                  1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "X12_SH_PSF",       "lensing shear",                   "",                  1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "X22_SH_PSF",       "lensing shear",                   "",                  1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "E1_SH_PSF",        "lensing shear",                   "",                  1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "E2_SH_PSF",        "lensing shear",                   "",                  1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "MOMENTS_R1",       "first radial moment",             "pixels",            1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "MOMENTS_RH",       "half radial moment",              "pixels^1/2",        1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "KRON_FLUX",        "kron flux",                       "counts",            1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "KRON_FLUX_ERR",    "kron flux error",                 "counts",            1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "KRON_FLUX_INNER",  "kron flux 1<R<2.5",               "counts",            1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "KRON_FLUX_OUTER",  "kron flux 2.5<R<4",               "counts",            1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "SKY_LIMIT_RAD",    "profile to sky limit (radius)",   "",                  1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "SKY_LIMIT_FLUX",   "profile to sky limit (flux)",     "",                  1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "SKY_LIMIT_SLOPE",  "profile to sky limit (slope)",    "",                  1.0, 0.0);
+  gfits_define_bintable_column (header, "J",    "FLAGS",            "analysis flags",                  "",                  1.0, 0.0);
+  gfits_define_bintable_column (header, "J",    "FLAGS2",           "analysis flags (2)",              "",                  1.0, 0.0);
+  gfits_define_bintable_column (header, "I",    "N_FRAMES",         "images overlapping peak",         "",                  1.0, 0.0);
+  gfits_define_bintable_column (header, "I",    "PADDING",          "padding for 8byte records",       "",                  1.0, 0.0);
+
+  /* create table */
+  if (!gfits_create_table (header, ftable)) return (FALSE);
+
+  /* add data values */
+  if (!gfits_table_scale_data (ftable)) return (FALSE);
+  if (!gfits_convert_CMF_PS1_V5_Lensing_Alt ((unsigned char *) data, sizeof (CMF_PS1_V5_Lensing_Alt), Ndata, FALSE)) return (FALSE);
+  if (!gfits_add_rows (ftable, (char *) data, Ndata, sizeof (CMF_PS1_V5_Lensing_Alt))) return (FALSE);
+
+  return (TRUE);
+}
+
+int gfits_table_mkheader_CMF_PS1_V5_Lensing_Alt (Header *header) {
+
+  /* create table header */
+  if (!gfits_create_table_header (header, "BINTABLE", "CMF_PS1_V5_Lensing_Alt")) return (FALSE);
+
+  /* define table layout */
+  /** TABLE DEFINITION **/
+  gfits_define_bintable_column (header, "J",    "IPP_IDET",         "detection ID                     ", "",                  1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "X_PSF",            "x coord",                         "pixels",            1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "Y_PSF",            "y coord",                         "pixels",            1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "X_PSF_SIG",        "x coord error",                   "pixels",            1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "Y_PSF_SIG",        "y coord error",                   "pixels",            1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "POSANGLE",         "Posangle at source",              "degrees",           1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "PLTSCALE",         "Plate Scale at source",           "arcsec/pixel",      1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "PSF_INST_MAG",     "inst mags",                       "mags",              1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "PSF_INST_MAG_SIG", "inst mag error",                  "mags",              1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "PSF_INST_FLUX",    "psf flux",                        "counts",            1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "PSF_INST_FLUX_SIG", "psf flux error",                  "counts      ",      1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "AP_MAG",           "standard aperture mag",           "mags",              1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "AP_MAG_RAW",       "raw aperture mag",                "mags",              1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "AP_MAG_RADIUS",    "radius used for aper",            "pixels",            1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "AP_FLUX",          "aperture flux",                   "counts",            1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "AP_FLUX_SIG",      "error on ap flux",                "counts",            1.0, 0.0);
+  gfits_define_bintable_column (header, "J",    "AP_NPIX",          "pixels used by aper",             "pixels",            1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "CAL_PSF_MAG",      "calibrated psf mag",              "mags",              1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "CAL_PSF_MAG_SIG",  "zero point scatter",              "mags",              1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "PEAK_FLUX_AS_MAG", "peak flux as a mag",              "mags",              1.0, 0.0);
+  gfits_define_bintable_column (header, "D",    "RA_PSF",           "PSF RA coord",                    "degrees",           1.0, 0.0);
+  gfits_define_bintable_column (header, "D",    "DEC_PSF",          "PSF DEC coord",                   "degrees",           1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "SKY",              "sky flux",                        "cnts/sec",          1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "SKY_SIGMA",        "sky flux error",                  "cnts/sec",          1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "PSF_CHISQ",        "psf fit chisq",                   "",                  1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "CR_NSIGMA",        "Nsigma deviations from PSF to CF", "",                  1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "EXT_NSIGMA",       "Nsigma deviations from PSF to EXT", "",                  1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "PSF_MAJOR",        "psf fit major axis",              "pixels",            1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "PSF_MINOR",        "psf fit minor axis",              "pixels",            1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "PSF_THETA",        "ellipse angle",                   "degrees",           1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "PSF_CORE",         "extra PSF parameter",             "unitless",          1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "PSF_FWHM_MAJ",     "true fwhm of psf",                "pixels",            1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "PSF_FWHM_MIN",     "true fwhm (minor)",               "pixels",            1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "PSF_QF",           "quality factor",                  "",                  1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "PSF_QF_PERFECT",   "quality factor perfect",          "",                  1.0, 0.0);
+  gfits_define_bintable_column (header, "J",    "PSF_NDOF",         "psf degrees of freedom",          "",                  1.0, 0.0);
+  gfits_define_bintable_column (header, "J",    "PSF_NPIX",         "psf number of pixels",            "",                  1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "MOMENTS_XX",       "second moment X",                 "pixels^2",          1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "MOMENTS_XY",       "second moment Y",                 "pixels^2",          1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "MOMENTS_YY",       "second moment XY",                "pixels^2",          1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "MOMENTS_M3C",      "third moment cos(t)",             "pixels^3",          1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "MOMENTS_M3S",      "third moment sin(t)",             "pixels^3",          1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "MOMENTS_M4C",      "fourth moment cos(t)",            "pixels^4",          1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "MOMENTS_M4S",      "fourth moment sin(t)",            "pixels^4",          1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "X11_SM_OBJ",       "lensing smear",                   "",                  1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "X12_SM_OBJ",       "lensing smear",                   "",                  1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "X22_SM_OBJ",       "lensing smear",                   "",                  1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "E1_SM_OBJ",        "lensing smear",                   "",                  1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "E2_SM_OBJ",        "lensing smear",                   "",                  1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "X11_SH_OBJ",       "lensing shear",                   "",                  1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "X12_SH_OBJ",       "lensing shear",                   "",                  1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "X22_SH_OBJ",       "lensing shear",                   "",                  1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "E1_SH_OBJ",        "lensing shear",                   "",                  1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "E2_SH_OBJ",        "lensing shear",                   "",                  1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "X11_SM_PSF",       "lensing smear",                   "",                  1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "X12_SM_PSF",       "lensing smear",                   "",                  1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "X22_SM_PSF",       "lensing smear",                   "",                  1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "E1_SM_PSF",        "lensing smear",                   "",                  1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "E2_SM_PSF",        "lensing smear",                   "",                  1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "X11_SH_PSF",       "lensing shear",                   "",                  1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "X12_SH_PSF",       "lensing shear",                   "",                  1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "X22_SH_PSF",       "lensing shear",                   "",                  1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "E1_SH_PSF",        "lensing shear",                   "",                  1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "E2_SH_PSF",        "lensing shear",                   "",                  1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "MOMENTS_R1",       "first radial moment",             "pixels",            1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "MOMENTS_RH",       "half radial moment",              "pixels^1/2",        1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "KRON_FLUX",        "kron flux",                       "counts",            1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "KRON_FLUX_ERR",    "kron flux error",                 "counts",            1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "KRON_FLUX_INNER",  "kron flux 1<R<2.5",               "counts",            1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "KRON_FLUX_OUTER",  "kron flux 2.5<R<4",               "counts",            1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "SKY_LIMIT_RAD",    "profile to sky limit (radius)",   "",                  1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "SKY_LIMIT_FLUX",   "profile to sky limit (flux)",     "",                  1.0, 0.0);
+  gfits_define_bintable_column (header, "E",    "SKY_LIMIT_SLOPE",  "profile to sky limit (slope)",    "",                  1.0, 0.0);
+  gfits_define_bintable_column (header, "J",    "FLAGS",            "analysis flags",                  "",                  1.0, 0.0);
+  gfits_define_bintable_column (header, "J",    "FLAGS2",           "analysis flags (2)",              "",                  1.0, 0.0);
+  gfits_define_bintable_column (header, "I",    "N_FRAMES",         "images overlapping peak",         "",                  1.0, 0.0);
+  gfits_define_bintable_column (header, "I",    "PADDING",          "padding for 8byte records",       "",                  1.0, 0.0);
+
+  return (TRUE);
+}
+
+int Send_CMF_PS1_V5_Lensing_Alt (int device, CMF_PS1_V5_Lensing_Alt *data, int Ndata, int copy) {
+
+  int Nwrite, Nbytes;
+  CMF_PS1_V5_Lensing_Alt *tmpdata;
+
+  Nbytes = Ndata * sizeof (CMF_PS1_V5_Lensing_Alt);
+
+  if (copy) {
+    ALLOCATE (tmpdata, CMF_PS1_V5_Lensing_Alt, Ndata);
+    memcpy (tmpdata, data, Nbytes);
+  } else {
+    tmpdata = data;
+  }
+
+  if (!gfits_convert_CMF_PS1_V5_Lensing_Alt ((unsigned char *) tmpdata, sizeof (CMF_PS1_V5_Lensing_Alt), Ndata, FALSE)) return (FALSE);
+
+  SendCommand (device, 16, "NVALUE: %6d", Ndata);
+  SendCommand (device, 16, "NBYTES: %6d", Nbytes);
+  Nwrite = write (device, tmpdata, Nbytes);
+  if (Nwrite != Nbytes) {
+    return (FALSE);
+  }
+  
+  /* perform handshaking? */
+
+  return (TRUE);
+}
+
+int Recv_CMF_PS1_V5_Lensing_Alt (int device, CMF_PS1_V5_Lensing_Alt **data, int *Ndata) {
+
+  int ndata;
+  IOBuffer message;
+  CMF_PS1_V5_Lensing_Alt *tmpdata;
+
+  ExpectCommand (device, 16, 1.0, &message);
+  sscanf (message.buffer, "%*s %d", &ndata);
+  FreeIOBuffer (&message);
+  
+  /* what is reasonable for timeout? */
+  ExpectMessage (device, 1.0, &message);
+  
+  tmpdata = (CMF_PS1_V5_Lensing_Alt *) message.buffer;
+  if (!gfits_convert_CMF_PS1_V5_Lensing_Alt ((unsigned char *) tmpdata, sizeof (CMF_PS1_V5_Lensing_Alt), ndata, TRUE)) return (FALSE);
+
+  /* double-check data length? */
+  /* perform handshaking? */
+
+  *Ndata = ndata;
+  *data = tmpdata;
+
+  return (TRUE);
+}
+
Index: trunk/Ohana/src/libdvo/src/cmf-ps1-v5-lensing.c
===================================================================
--- trunk/Ohana/src/libdvo/src/cmf-ps1-v5-lensing.c	(revision 37727)
+++ trunk/Ohana/src/libdvo/src/cmf-ps1-v5-lensing.c	(revision 37729)
@@ -1,4 +1,4 @@
 # include "dvo.h"
-# define ST_SIZE 312
+# define ST_SIZE 320
 
 // this function is based on the one generated by the autocode of cmf-ps1-v5.d
@@ -100,17 +100,22 @@
     SWAP_WORD (256); // E1_SH_PSF
     SWAP_WORD (260); // E2_SH_PSF
-    SWAP_WORD (264); // MOMENTS_R1
-    SWAP_WORD (268); // MOMENTS_RH
-    SWAP_WORD (272); // KRON_FLUX
-    SWAP_WORD (276); // KRON_FLUX_ERR
-    SWAP_WORD (280); // KRON_FLUX_INNER
-    SWAP_WORD (284); // KRON_FLUX_OUTER
-    SWAP_WORD (288); // SKY_LIMIT_RAD
-    SWAP_WORD (292); // SKY_LIMIT_FLUX
-    SWAP_WORD (296); // SKY_LIMIT_SLOPE
-    SWAP_WORD (300); // FLAGS
-    SWAP_WORD (304); // FLAGS2
-    SWAP_BYTE (308); // N_FRAMES
-    SWAP_BYTE (310); // PADDING                       
+
+    SWAP_BYTE (264); // SRC_CHIP_NUM
+    SWAP_BYTE (266); // SRC_CHIP_X
+    SWAP_BYTE (268); // SRC_CHIP_Y
+
+    SWAP_WORD (272); // MOMENTS_R1
+    SWAP_WORD (276); // MOMENTS_RH
+    SWAP_WORD (280); // KRON_FLUX
+    SWAP_WORD (284); // KRON_FLUX_ERR
+    SWAP_WORD (288); // KRON_FLUX_INNER
+    SWAP_WORD (292); // KRON_FLUX_OUTER
+    SWAP_WORD (296); // SKY_LIMIT_RAD
+    SWAP_WORD (300); // SKY_LIMIT_FLUX
+    SWAP_WORD (304); // SKY_LIMIT_SLOPE
+    SWAP_WORD (308); // FLAGS
+    SWAP_WORD (312); // FLAGS2
+    SWAP_BYTE (316); // N_FRAMES
+    SWAP_BYTE (318); // PADDING                       
   }
 # endif  
@@ -289,4 +294,10 @@
   gfits_define_bintable_column (header, "E",    "E1_SH_PSF",        "lensing shear",                   "",                  1.0, 0.0);
   gfits_define_bintable_column (header, "E",    "E2_SH_PSF",        "lensing shear",                   "",                  1.0, 0.0);
+
+  gfits_define_bintable_column (header, "I",    "SRC_CHIP_NUM",     "number of source chip for warp",  "",                  1.0, 0.0);
+  gfits_define_bintable_column (header, "I",    "SRC_CHIP_X",       "x-coord in source chip",          "",                  1.0, 0.0);
+  gfits_define_bintable_column (header, "I",    "SRC_CHIP_Y",       "y-coord in source chip",          "",                  1.0, 0.0);
+  gfits_define_bintable_column (header, "I",    "PADDING3",         "padding for 8byte records",       "",                  1.0, 0.0);
+
   gfits_define_bintable_column (header, "E",    "MOMENTS_R1",       "first radial moment",             "pixels",            1.0, 0.0);
   gfits_define_bintable_column (header, "E",    "MOMENTS_RH",       "half radial moment",              "pixels^1/2",        1.0, 0.0);
@@ -385,4 +396,10 @@
   gfits_define_bintable_column (header, "E",    "E1_SH_PSF",        "lensing shear",                   "",                  1.0, 0.0);
   gfits_define_bintable_column (header, "E",    "E2_SH_PSF",        "lensing shear",                   "",                  1.0, 0.0);
+
+  gfits_define_bintable_column (header, "I",    "SRC_CHIP_NUM",     "number of source chip for warp",  "",                  1.0, 0.0);
+  gfits_define_bintable_column (header, "I",    "SRC_CHIP_X",       "x-coord in source chip",          "",                  1.0, 0.0);
+  gfits_define_bintable_column (header, "I",    "SRC_CHIP_Y",       "y-coord in source chip",          "",                  1.0, 0.0);
+  gfits_define_bintable_column (header, "I",    "PADDING3",         "padding for 8byte records",       "",                  1.0, 0.0);
+
   gfits_define_bintable_column (header, "E",    "MOMENTS_R1",       "first radial moment",             "pixels",            1.0, 0.0);
   gfits_define_bintable_column (header, "E",    "MOMENTS_RH",       "half radial moment",              "pixels^1/2",        1.0, 0.0);
Index: trunk/Ohana/src/libdvo/src/cmf-ps1-v5.c
===================================================================
--- trunk/Ohana/src/libdvo/src/cmf-ps1-v5.c	(revision 37727)
+++ trunk/Ohana/src/libdvo/src/cmf-ps1-v5.c	(revision 37729)
@@ -1,3 +1,4 @@
 # include "dvo.h"
+# define ST_SIZE 232
 
 // this function is based on the one generated by the autocode of cmf-ps1-v5.d
@@ -7,6 +8,6 @@
   unsigned char tmp;
 
-  if (size != 232) { 
-    fprintf (stderr, "WARNING: mismatch in data types CMF_PS1_V5: "OFF_T_FMT" vs %d\n",  size,  232);
+  if (size != ST_SIZE) { 
+    fprintf (stderr, "WARNING: mismatch in data types CMF_PS1_V5: "OFF_T_FMT" vs %d\n",  size,  ST_SIZE);
     return (FALSE);
   }
@@ -33,5 +34,5 @@
 # ifdef BYTE_SWAP
   byte = (unsigned char *) data;
-  for (i = 0; i < nitems; i++, byte += 232) {
+  for (i = 0; i < nitems; i++, byte += ST_SIZE) {
     /** BYTE SWAP **/
     SWAP_WORD (0); // IPP_IDET                     
@@ -176,6 +177,6 @@
 
   Ncols = ftable[0].header[0].Naxis[0];
-  if (Ncols != 232) {
-    fprintf (stderr, "ERROR: mis-match in table size: width is %d but should be %d bytes\n", Ncols, 232);
+  if (Ncols != ST_SIZE) {
+    fprintf (stderr, "ERROR: mis-match in table size: width is %d but should be %d bytes\n", Ncols, ST_SIZE);
     return NULL;
   }
