# include "addstar.h"

// this is the complete list of FITS format input files the addstar knows 
// (excluding SDSS data and reference database info, such as 2MASS)
// NOTE: these must also be listed in MatchHeaders.c (line ~ 62)
Stars     *Convert_SMPDATA	  PROTO((FTable *table, unsigned int *nstars));
Stars     *Convert_PS1_DEV_0	  PROTO((FTable *table, unsigned int *nstars));
Stars     *Convert_PS1_DEV_1	  PROTO((FTable *table, unsigned int *nstars));
Stars     *Convert_PS1_V1	  PROTO((FTable *table, unsigned int *nstars));
Stars     *Convert_PS1_V1_Alt	  PROTO((FTable *table, unsigned int *nstars));
Stars     *Convert_PS1_V2	  PROTO((FTable *table, unsigned int *nstars));
Stars     *Convert_PS1_V3	  PROTO((FTable *table, unsigned int *nstars));
Stars     *Convert_PS1_V4	  PROTO((FTable *table, unsigned int *nstars));
Stars     *Convert_PS1_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));

// given a file with the pointer at the start of the table block and the 
// corresponding image header, load the stars from the table
Stars *ReadStarsFITS (FILE *f, Header *header, Header *in_theader, unsigned int *nstars) {

  off_t Nskip;
  unsigned int Nstars;
  char type[80];
  Header theader;
  FTable table;
  Stars *stars; // Stars contains Average and Measure
  
  if (in_theader == NULL) {
    table.header = &theader;
    if (!gfits_fread_header (f, table.header)) Shutdown ("ERROR: can't read table header");
  } else {
    table.header = in_theader;
    Nskip = in_theader[0].datasize;
    fseeko (f, Nskip, SEEK_CUR); 
  }

  /* load the table data */
  if (!gfits_fread_ftable_data (f, &table, FALSE)) {
    fprintf (stderr, "ERROR: can't read table header\n");
    exit (1);
  }

  if (!gfits_scan (table.header, "EXTTYPE", "%s", 1, type)) {
    strcpy (type, "SMPDATA");
  }

  stars = NULL;
  if (!strcmp (type, "SMPDATA")) {
    stars = Convert_SMPDATA (&table, &Nstars);
  }
  if (!strcmp (type, "PS1_DEV_0")) {
    stars = Convert_PS1_DEV_0 (&table, &Nstars);
  }
  if (!strcmp (type, "PS1_DEV_1")) {
    stars = Convert_PS1_DEV_1 (&table, &Nstars);
  }
  if (!strcmp (type, "PS1_V1")) {
    stars = Convert_PS1_V1 (&table, &Nstars);
  }
  if (!strcmp (type, "PS1_V2")) {
    if (table.header[0].Naxis[0] == 20) {
      // skip the invalid DETEFF tables which were mistakenly labeled as PS1_V2
      return (NULL);
    }
    stars = Convert_PS1_V2 (&table, &Nstars);
  }
  if (!strcmp (type, "PS1_V3")) {
    stars = Convert_PS1_V3 (&table, &Nstars);
  }
  if (!strcmp (type, "PS1_V4")) {
    stars = Convert_PS1_V4 (&table, &Nstars);
  }
  if (!strcmp (type, "PS1_V5")) {
    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;
    }
  }
  if (!strcmp (type, "PS1_SV1")) {
    stars = Convert_PS1_SV1 (&table, &Nstars);
  }
  if (!strcmp (type, "PS1_SV2")) {
    stars = Convert_PS1_SV2 (&table, &Nstars);
  }
  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")) {
    stars = Convert_PS1_DV3 (&table, &Nstars);
  }
  if (!strcmp (type, "PS1_DV4")) {
    stars = Convert_PS1_DV4 (&table, &Nstars);
  }
  if (stars == NULL) {
    fprintf (stderr, "invalid table type %s\n", type);
    return (NULL);
  }
  // Nstars is not necessarily == *nstars (The former is the number of detections, the
  // latter are the 'good' detections reported by the photometry system.
  *nstars = Nstars;

  return stars;
}

float GetFluxFromFluxOrMag (float flux, float mag) {

  if (isnan(mag) && isnan(flux)) return NAN;

  if (isnan(flux)) return pow(10.0,-0.4*mag);
  return flux;
}

float GetFluxErrFromFluxOrMag (float dFlux, float flux, float dMag) {

  if (isnan(dMag) && isnan(dFlux)) return NAN;
  if (isnan(dFlux) && isnan(flux)) return NAN;

  if (isnan(dFlux)) return (fabs(dMag * flux));
  return dFlux;
}

Stars *Convert_SMPDATA (FTable *table, unsigned int *nstars) {

  off_t Nstars;
  unsigned int i;
  char swapped;
  double ZeroPt;
  Stars *stars = NULL;
  SMPData *smpdata = NULL;

  swapped = FALSE;
  smpdata = gfits_table_get_SMPData (table, &Nstars, &swapped);
  if (!smpdata) {
    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       = smpdata[i].X;
    stars[i].measure.Yccd       = smpdata[i].Y;
    stars[i].measure.dXccd    	= NAN_S_SHORT; // not provided by SMPDATA:
    stars[i].measure.dYccd    	= NAN_S_SHORT; // not provided by SMPDATA:
   
    stars[i].measure.posangle 	= NAN_S_SHORT; // not provided by SMPDATA:
    stars[i].measure.pltscale 	= NAN;         // not provided by SMPDATA:

    if ((smpdata[i].M >= ZeroPt) || isnan(smpdata[i].M)) {
      stars[i].measure.M        = NAN;
      stars[i].measure.Map      = NAN;
      stars[i].measure.FluxPSF  = NAN;
      stars[i].measure.dFluxPSF = NAN;
    } else {
      stars[i].measure.M        = smpdata[i].M;
      stars[i].measure.Map      = smpdata[i].M;
      stars[i].measure.FluxPSF  = pow(10.0, -0.4*smpdata[i].M);
      stars[i].measure.dFluxPSF = stars[i].measure.FluxPSF * smpdata[i].dM;
    }
    stars[i].measure.dM         = smpdata[i].dM*0.001;
    stars[i].measure.dMcal    	= NAN; // not provided by SMPDATA:

    stars[i].measure.Mkron    	= NAN; // not provided by SMPDATA:
    stars[i].measure.dMkron   	= NAN; // not provided by SMPDATA:
    stars[i].measure.FluxKron   = NAN; // not provided by SMPDATA:
    stars[i].measure.dFluxKron  = NAN; // not provided by SMPDATA:

    stars[i].measure.Sky      	= NAN; // not provided by SMPDATA:
    stars[i].measure.dSky     	= NAN; // not provided by SMPDATA:

    stars[i].measure.psfChisq 	= NAN;	     // not provided by SMPDATA:
    stars[i].measure.psfQF  	= NAN;	     // not provided by SMPDATA:
    stars[i].measure.psfNdof    = NAN_S_INT; // not provided by SMPDATA:
    stars[i].measure.psfNpix    = NAN_S_INT; // not provided by SMPDATA:
    stars[i].measure.extNsigma  = NAN;	     // not provided by SMPDATA:

    stars[i].measure.FWx        = ToShortPixels (smpdata[i].fx);
    stars[i].measure.FWy        = ToShortPixels (smpdata[i].fy);
    stars[i].measure.theta      = ToShortDegrees (smpdata[i].df);

    stars[i].measure.Mxx        = NAN_S_SHORT; // not provided by SMPDATA:
    stars[i].measure.Mxy        = NAN_S_SHORT; // not provided by SMPDATA:
    stars[i].measure.Myy        = NAN_S_SHORT; // not provided by SMPDATA:
                        
    // the dophot type information gets pushed into the upper 2 bytes of photFlags
    stars[i].measure.photFlags  = (smpdata[i].dophot << 16);
  }    
  *nstars = Nstars;
  return (stars);
}

Stars *Convert_PS1_DEV_0 (FTable *table, unsigned int *nstars) {

  off_t Nstars;
  unsigned int i;
  double ZeroPt;
  Stars *stars;
  PS1_DEV_0 *ps1data;

  ps1data = gfits_table_get_PS1_DEV_0 (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 	= NAN_S_SHORT; // not provided by PS1_DEV_0:
    stars[i].measure.pltscale 	= NAN;         // not provided by PS1_DEV_0:

    if ((ps1data[i].M >= 0.0) || isnan(ps1data[i].M)) {
      stars[i].measure.M      	= NAN;
      stars[i].measure.FluxPSF  = NAN;
      stars[i].measure.dFluxPSF = NAN;
    } else {
      stars[i].measure.M      	= ps1data[i].M + ZeroPt;
      stars[i].measure.FluxPSF  = pow(10.0, -0.4*ps1data[i].M);
      stars[i].measure.dFluxPSF = stars[i].measure.FluxPSF * ps1data[i].dM;
    }
    stars[i].measure.dM       	= ps1data[i].dM;
    stars[i].measure.dMcal    	= NAN; // not provided by PS1_DEV_0:
    stars[i].measure.Map      	= NAN; // not provided by PS1_DEV_0:

    stars[i].measure.Mkron    	= NAN; // not provided by PS1_DEV_0:
    stars[i].measure.dMkron   	= NAN; // not provided by PS1_DEV_0:
    stars[i].measure.FluxKron   = NAN; // not provided by PS1_DEV_0:
    stars[i].measure.dFluxKron  = NAN; // not provided by PS1_DEV_0:

    stars[i].measure.Sky      	= ps1data[i].sky;
    stars[i].measure.dSky     	= ps1data[i].dSky;

    stars[i].measure.psfChisq 	= ps1data[i].psfChisq;
    stars[i].measure.psfQF  	= ps1data[i].psfQF;
    stars[i].measure.psfNdof    = NAN_S_INT; // not provided by PS1_DEV_0:
    stars[i].measure.psfNpix    = NAN_S_INT; // not provided by PS1_DEV_0:
    stars[i].measure.extNsigma  = NAN;        // not provided by PS1_DEV_0:

    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        = NAN_S_SHORT; // not provided by PS1_DEV_0:
    stars[i].measure.Mxy        = NAN_S_SHORT; // not provided by PS1_DEV_0:
    stars[i].measure.Myy        = NAN_S_SHORT; // not provided by PS1_DEV_0:
                        
    stars[i].measure.photFlags  = 0; // not provided by PS1_DEV_0:

    stars[i].measure.detID      = ps1data[i].detID;
  }    
  *nstars = Nstars;
  return (stars);
}

// XXX I need to make the IPP I/O functions and these functions
// consistent wrt ZERO_POINT....
Stars *Convert_PS1_DEV_1 (FTable *table, unsigned int *nstars) {

  off_t Nstars; 
  unsigned int i;
  double ZeroPt;
  Stars *stars;
  PS1_DEV_1 *ps1data;

  ps1data = gfits_table_get_PS1_DEV_1 (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   = NAN_S_SHORT; // not provided by PS1_DEV_1:
    stars[i].measure.pltscale   = NAN;         // not provided by PS1_DEV_1:

    if ((ps1data[i].M >= 0.0) || isnan(ps1data[i].M)) {
      stars[i].measure.M      = NAN;
      stars[i].measure.FluxPSF  = NAN;
      stars[i].measure.dFluxPSF = NAN;
    } else {
      stars[i].measure.M      = ps1data[i].M + ZeroPt;
      stars[i].measure.FluxPSF  = pow(10.0, -0.4*ps1data[i].M);
      stars[i].measure.dFluxPSF = stars[i].measure.FluxPSF * ps1data[i].dM;
    }
    stars[i].measure.dM         = ps1data[i].dM;
    stars[i].measure.dMcal      = NAN; // not provided by PS1_DEV_1:
    stars[i].measure.Map        = NAN; // not provided by PS1_DEV_1:

    stars[i].measure.Mkron      = NAN; // not provided by PS1_DEV_1:
    stars[i].measure.dMkron     = NAN; // not provided by PS1_DEV_1:
    stars[i].measure.FluxKron   = NAN; // not provided by PS1_DEV_1:
    stars[i].measure.dFluxKron  = NAN; // not provided by PS1_DEV_1:

    stars[i].measure.Sky        = ps1data[i].sky;
    stars[i].measure.dSky       = ps1data[i].dSky;

    stars[i].measure.psfChisq   = ps1data[i].psfChisq;
    stars[i].measure.psfQF      = ps1data[i].psfQF;
    stars[i].measure.psfNdof    = NAN_S_INT; // not provided by PS1_DEV_1:
    stars[i].measure.psfNpix    = NAN_S_INT; // not provided by PS1_DEV_1:
    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        = NAN_S_SHORT; // not provided by PS1_DEV_1:
    stars[i].measure.Mxy        = NAN_S_SHORT; // not provided by PS1_DEV_1:
    stars[i].measure.Myy        = NAN_S_SHORT; // not provided by PS1_DEV_1:
                        
    stars[i].measure.photFlags  = ps1data[i].flags;

    // this is may optionally be replaced by the internal sequence (see FilterStars.c)
    stars[i].measure.detID      = ps1data[i].detID;
  }    
  *nstars = Nstars;
  return (stars);
}

Stars *Convert_PS1_V1 (FTable *table, unsigned int *nstars) {

  off_t Nstars; 
  unsigned int i;
  double ZeroPt;
  Stars *stars;
  CMF_PS1_V1 *ps1data;

  // CMF_PS1_V1 was modified 2009.05.26 (r24251) to use doubles for ra & dec.  this was a
  // mistake in two ways: a new format should have been defined (eg, CMF_PS1_V2), and the
  // layout used did not have clean byte-boundaries for the corresponding structure.  The
  // former means we have two varieties of CMF_PS1_V1 out there; the latter means that the
  // autocode tools do not work to read in the new version, even if we recognize it.  Here we
  // test for the existence of the broken version (table[0].headers[0].Naxis[0] == 136), and
  // call a special conversion function if it is found.

  if (table[0].header[0].Naxis[0] == 136) {
    stars = Convert_PS1_V1_Alt (table, nstars);
    return (stars);
  }

  ps1data = gfits_table_get_CMF_PS1_V1 (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;
      stars[i].measure.FluxPSF  = NAN;
      stars[i].measure.dFluxPSF = NAN;
    } else {
      stars[i].measure.M      = ps1data[i].M + ZeroPt;
      stars[i].measure.FluxPSF  = pow(10.0, -0.4*ps1data[i].M);
      stars[i].measure.dFluxPSF = stars[i].measure.FluxPSF * ps1data[i].dM;
    }
    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].dM; // a proxy measure
                        
    stars[i].measure.Mkron      = NAN; // not provided by PS1_V1:
    stars[i].measure.dMkron     = NAN; // not provided by PS1_V1:
    stars[i].measure.FluxKron   = NAN; // not provided by PS1_V1:
    stars[i].measure.dFluxKron  = NAN; // not provided by PS1_V1:

    stars[i].measure.Sky        = ps1data[i].sky;
    stars[i].measure.dSky       = ps1data[i].dSky;
                        
    stars[i].measure.psfChisq   = ps1data[i].psfChisq;
    stars[i].measure.psfQF      = ps1data[i].psfQF;
    stars[i].measure.psfNdof    = ps1data[i].psfNdof;
    stars[i].measure.psfNpix    = ps1data[i].psfNpix;
    stars[i].measure.extNsigma  = ps1data[i].extNsigma;

    stars[i].measure.FWx        = ToShortPixels(ps1data[i].fx);
    stars[i].measure.FWy        = ToShortPixels(ps1data[i].fy);
    stars[i].measure.theta      = ToShortDegrees(ps1data[i].df);

    stars[i].measure.Mxx        = ToShortPixels(ps1data[i].Mxx);
    stars[i].measure.Mxy        = ToShortPixels(ps1data[i].Mxy);
    stars[i].measure.Myy        = ToShortPixels(ps1data[i].Myy);
                        
    stars[i].measure.photFlags  = ps1data[i].flags;

    // this is may optionally be replaced by the internal sequence (see FilterStars.c)
    stars[i].measure.detID      = ps1data[i].detID; 

    // the Average fields and the following Measure fields are set in FilterStars after
    // the image metadata is in hand:  dR, dD, Mcal, dt, airmass, az, t, imageID, extID, 
    // averef is set in find_matches, dbFlags is zero on ingest.

    // the following fields are currently not being set anywhere: t_msec
  }    
  *nstars = Nstars;
  return (stars);
}

Stars *Convert_PS1_V1_Alt (FTable *table, unsigned int *nstars) {

  off_t Nstars; 
  unsigned int i;
  double ZeroPt;
  Stars *stars;
  CMF_PS1_V2 *ps1data;

  // CMF_PS1_V1 was modified 2009.05.26 (r24251) to use doubles for ra & dec.  this was a
  // mistake in two ways: a few format should have been defined (eg, CMF_PS1_V2), and the
  // layout used did not have clean byte-boundaries for the corresponding structure.  The
  // former means we have two varieties of CMF_PS1_V1 out there; the latter means that the
  // autocode tools do not work to read in the new version, even if we recognize it.  Here we
  // test for the existence of the broken version (table[0].headers[0].Naxis[0] == 136), and
  // call a special conversion function if it is found.

  ps1data = gfits_table_get_CMF_PS1_V1_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;
      stars[i].measure.FluxPSF  = NAN;
      stars[i].measure.dFluxPSF = NAN;
    } else {
      stars[i].measure.M        = ps1data[i].M + ZeroPt;
      stars[i].measure.FluxPSF  = pow(10.0, -0.4*ps1data[i].M);
      stars[i].measure.dFluxPSF = stars[i].measure.FluxPSF * ps1data[i].dM;
    }
    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].dM; // a proxy measure
                        
    stars[i].measure.Mkron      = NAN; // not provided by PS1_V1_Alt:
    stars[i].measure.dMkron     = NAN; // not provided by PS1_V1_Alt:
    stars[i].measure.FluxKron   = NAN; // not provided by PS1_V1_Alt:
    stars[i].measure.dFluxKron  = NAN; // not provided by PS1_V1_Alt:

    stars[i].measure.Sky        = ps1data[i].sky;
    stars[i].measure.dSky       = ps1data[i].dSky;
                        
    stars[i].measure.psfChisq   = ps1data[i].psfChisq;
    stars[i].measure.psfQF      = ps1data[i].psfQF;
    stars[i].measure.psfNdof    = ps1data[i].psfNdof;
    stars[i].measure.psfNpix    = ps1data[i].psfNpix;
    stars[i].measure.extNsigma  = ps1data[i].extNsigma;

    stars[i].measure.FWx        = ToShortPixels(ps1data[i].fx);
    stars[i].measure.FWy        = ToShortPixels(ps1data[i].fy);
    stars[i].measure.theta      = ToShortDegrees(ps1data[i].df);

    stars[i].measure.Mxx        = ToShortPixels(ps1data[i].Mxx);
    stars[i].measure.Mxy        = ToShortPixels(ps1data[i].Mxy);
    stars[i].measure.Myy        = ToShortPixels(ps1data[i].Myy);
                        
    stars[i].measure.photFlags  = ps1data[i].flags;

    // this is may optionally be replaced by the internal sequence (see FilterStars.c)
    stars[i].measure.detID      = ps1data[i].detID; 

    // the Average fields and the following Measure fields are set in FilterStars after
    // the image metadata is in hand:  dR, dD, Mcal, dt, airmass, az, t, imageID, extID, 
    // averef is set in find_matches, dbFlags is zero on ingest.

    // the following fields are currently not being set anywhere: t_msec
  }    
  *nstars = Nstars;
  return (stars);
}

Stars *Convert_PS1_V2 (FTable *table, unsigned int *nstars) {

  off_t Nstars; 
  unsigned int i;
  double ZeroPt;
  Stars *stars;
  CMF_PS1_V2 *ps1data;

  ps1data = gfits_table_get_CMF_PS1_V2 (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;
      stars[i].measure.FluxPSF  = NAN;
      stars[i].measure.dFluxPSF = NAN;
    } else {
      stars[i].measure.M      = ps1data[i].M + ZeroPt;
      stars[i].measure.FluxPSF  = pow(10.0, -0.4*ps1data[i].M);
      stars[i].measure.dFluxPSF = stars[i].measure.FluxPSF * ps1data[i].dM;
    }
    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].dM; // a proxy measure
                        
    stars[i].measure.Mkron      = NAN; // not provided by PS1_V2:
    stars[i].measure.dMkron     = NAN; // not provided by PS1_V2:
    stars[i].measure.FluxKron   = NAN; // not provided by PS1_V2:
    stars[i].measure.dFluxKron  = NAN; // not provided by PS1_V2:

    stars[i].measure.Sky        = ps1data[i].sky;
    stars[i].measure.dSky       = ps1data[i].dSky;
                        
    stars[i].measure.psfChisq   = ps1data[i].psfChisq;
    stars[i].measure.psfQF      = ps1data[i].psfQF;
    stars[i].measure.psfNdof    = ps1data[i].psfNdof;
    stars[i].measure.psfNpix    = ps1data[i].psfNpix;
    stars[i].measure.extNsigma  = ps1data[i].extNsigma;

    stars[i].measure.FWx        = ToShortPixels(ps1data[i].fx);
    stars[i].measure.FWy        = ToShortPixels(ps1data[i].fy);
    stars[i].measure.theta      = ToShortDegrees(ps1data[i].df);

    stars[i].measure.Mxx        = ToShortPixels(ps1data[i].Mxx);
    stars[i].measure.Mxy        = ToShortPixels(ps1data[i].Mxy);
    stars[i].measure.Myy        = ToShortPixels(ps1data[i].Myy);
                        
    stars[i].measure.photFlags  = ps1data[i].flags;

    // this is may optionally be replaced by the internal sequence (see FilterStars.c)
    stars[i].measure.detID      = ps1data[i].detID; 

    // the Average fields and the following Measure fields are set in FilterStars after
    // the image metadata is in hand:  dR, dD, Mcal, dt, airmass, az, t, imageID, extID, 
    // averef is set in find_matches, dbFlags is zero on ingest.

    // the following fields are currently not being set anywhere: t_msec
  }    
  *nstars = Nstars;
  return (stars);
}

Stars *Convert_PS1_V3 (FTable *table, unsigned int *nstars) {

  off_t Nstars; 
  unsigned int i;
  double ZeroPt;
  Stars *stars;
  CMF_PS1_V3 *ps1data;

  ps1data = gfits_table_get_CMF_PS1_V3 (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].dM; // a proxy measure
                        
    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 (NAN, ps1data[i].Map); 
    stars[i].measure.dFluxAp    = GetFluxErrFromFluxOrMag (NAN, 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);
}

Stars *Convert_PS1_V4 (FTable *table, unsigned int *nstars) {

  off_t Nstars; 
  unsigned int i;
  double ZeroPt;
  Stars *stars;
  CMF_PS1_V4 *ps1data;

  ps1data = gfits_table_get_CMF_PS1_V4 (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; 

    // 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_V5 (FTable *table, unsigned int *nstars) {

  off_t Nstars; 
  unsigned int i;
  double ZeroPt;
  Stars *stars;
  CMF_PS1_V5 *ps1data;

  ps1data = gfits_table_get_CMF_PS1_V5 (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; 

    // 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_V5_Lensing (FTable *table, unsigned int *nstars) {

  off_t Nstars; 
  unsigned int i;
  double ZeroPt;
  Stars *stars;
  CMF_PS1_V5_Lensing *ps1data;

  ps1data = gfits_table_get_CMF_PS1_V5_Lensing (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_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) {

  off_t Nstars; 
  unsigned int i;
  double ZeroPt;
  Stars *stars;
  CMF_PS1_SV1 *ps1data;

  if (table[0].header[0].Naxis[0] == 196) {
    stars = Convert_PS1_SV1_Alt (table, nstars);
    return (stars);
  }

  ps1data = gfits_table_get_CMF_PS1_SV1 (table, &Nstars, NULL);
  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].dM; // a proxy measure
                        
    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 (NAN, ps1data[i].Map); 
    stars[i].measure.dFluxAp    = GetFluxErrFromFluxOrMag (NAN, 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);
}

Stars *Convert_PS1_SV1_Alt (FTable *table, unsigned int *nstars) {

  off_t Nstars; 
  unsigned int i;
  double ZeroPt;
  Stars *stars;
  CMF_PS1_SV1 *ps1data;

  // some test output files were produced called CMF_PS1_SV1 but with mismatch byte boundaries

  ps1data = gfits_table_get_CMF_PS1_SV1_Alt (table, &Nstars, NULL);
  if (!ps1data) {
    fprintf (stderr, "skipping inconsistent entry\n");
    return (NULL);
  }
  ZeroPt = GetZeroPoint();

  ALLOCATE (stars, Stars, Nstars);
  for (i = 0; i < Nstars; i++) {
    InitStar (&stars[i]);
    stars[i].measure.Xccd       = ps1data[i].X;
    stars[i].measure.Yccd       = ps1data[i].Y;
    stars[i].measure.dXccd      = ToShortPixels(ps1data[i].dX);
    stars[i].measure.dYccd      = ToShortPixels(ps1data[i].dY);

    stars[i].measure.posangle   = ToShortDegrees(ps1data[i].posangle);
    stars[i].measure.pltscale   = ps1data[i].pltscale;

    if ((ps1data[i].M >= 0.0) || isnan(ps1data[i].M)) {
      stars[i].measure.M      = NAN;
    } else {
      stars[i].measure.M      = ps1data[i].M + ZeroPt;
    }
    stars[i].measure.dM         = ps1data[i].dM;
    stars[i].measure.dMcal      = ps1data[i].dMcal;
    stars[i].measure.Map        = ps1data[i].Map + ZeroPt;
    stars[i].measure.dMap       = ps1data[i].dM; // a proxy measure
                        
    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 (NAN, ps1data[i].Map); 
    stars[i].measure.dFluxAp    = GetFluxErrFromFluxOrMag (NAN, 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.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);
}

Stars *Convert_PS1_SV2 (FTable *table, unsigned int *nstars) {

  off_t Nstars; 
  unsigned int i;
  double ZeroPt;
  Stars *stars;
  CMF_PS1_SV2 *ps1data;

  ps1data = gfits_table_get_CMF_PS1_SV2 (table, &Nstars, NULL);
  if (!ps1data) {
    fprintf (stderr, "skipping inconsistent entry\n");
    return (NULL);
  }
  ZeroPt = GetZeroPoint();

  ALLOCATE (stars, Stars, Nstars);
  for (i = 0; i < Nstars; i++) {
    InitStar (&stars[i]);
    stars[i].measure.Xccd       = ps1data[i].X;
    stars[i].measure.Yccd       = ps1data[i].Y;
    stars[i].measure.dXccd      = ToShortPixels(ps1data[i].dX);
    stars[i].measure.dYccd      = ToShortPixels(ps1data[i].dY);

    stars[i].measure.posangle   = ToShortDegrees(ps1data[i].posangle);
    stars[i].measure.pltscale   = ps1data[i].pltscale;

    if ((ps1data[i].M >= 0.0) || isnan(ps1data[i].M)) {
      stars[i].measure.M      = NAN;
    } else {
      stars[i].measure.M      = ps1data[i].M + ZeroPt;
    }
    stars[i].measure.dM         = ps1data[i].dM;
    stars[i].measure.dMcal      = ps1data[i].dMcal;
    stars[i].measure.Map        = ps1data[i].Map + ZeroPt;
    stars[i].measure.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);
}

Stars *Convert_PS1_SV3 (FTable *table, unsigned int *nstars) {

  off_t Nstars; 
  unsigned int i;
  double ZeroPt;
  Stars *stars;
  CMF_PS1_SV3 *ps1data;

  ps1data = gfits_table_get_CMF_PS1_SV3 (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; 

    // 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_SV4 (FTable *table, unsigned int *nstars) {

  off_t Nstars; 
  unsigned int i;
  double ZeroPt;
  Stars *stars;
  CMF_PS1_SV4 *ps1data;

  ps1data = gfits_table_get_CMF_PS1_SV4 (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; 

    // 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_DV3 (FTable *table, unsigned int *nstars) {

  off_t Nstars; 
  unsigned int i;
  double ZeroPt;
  Stars *stars;
  CMF_PS1_DV3 *ps1data;

  ps1data = gfits_table_get_CMF_PS1_DV3 (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; 

    // 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_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);
}



