Index: /branches/eam_branches/ipp-20140610/Ohana/src/addstar/src/mkcmf.c
===================================================================
--- /branches/eam_branches/ipp-20140610/Ohana/src/addstar/src/mkcmf.c	(revision 36905)
+++ /branches/eam_branches/ipp-20140610/Ohana/src/addstar/src/mkcmf.c	(revision 36906)
@@ -1,4 +1,5 @@
 # include "mkcmf.h"
 
+# define ZERO_POINT 25.0
 # define SKY 100.0
 # define DSKY 2.0
@@ -14,4 +15,5 @@
 void gauss_init (int Nbin);
 double rnd_gauss (double mean, double sigma);
+void writeStars_PS1_V5 (FTable *ftable, double *X, double *Y, double *M, unsigned int *Flag, int Nstars);
 void writeStars_PS1_V4 (FTable *ftable, double *X, double *Y, double *M, unsigned int *Flag, int Nstars);
 void writeStars_PS1_V3 (FTable *ftable, double *X, double *Y, double *M, unsigned int *Flag, int Nstars);
@@ -24,4 +26,7 @@
 float BAD_PSFQF_FRAC = 0.0;
 
+static float exptime = 1.0;
+static Coords coords;
+
 int main (int argc, char **argv) {
 
@@ -38,5 +43,4 @@
   Matrix matrix;
   FTable ftable;
-  Coords coords;
 
   int APPEND = FALSE;
@@ -105,5 +109,4 @@
   }
 
-  float exptime = 1.0;
   if ((N = get_argument (argc, argv, "-exptime"))) {
     remove_argument (N, &argc, argv);
@@ -288,5 +291,5 @@
     gfits_modify (&header, "UTC-OBS",  "%s", 1, time);
   }
-  gfits_modify (&header, "ZERO_PT", "%lf", 1, 25.0);
+  gfits_modify (&header, "ZERO_PT", "%lf", 1, ZERO_POINT);
   gfits_modify (&header, "EXPTIME", "%lf", 1, exptime);
   gfits_modify (&header, "AIRMASS", "%lf", 1, airmass);
@@ -327,4 +330,8 @@
   if (!strcmp(type, "PS1_V4")) {
     writeStars_PS1_V4 (&ftable, X, Y, M, Flag, Nstars); 
+    found = TRUE;
+  }
+  if (!strcmp(type, "PS1_V5")) {
+    writeStars_PS1_V5 (&ftable, X, Y, M, Flag, Nstars); 
     found = TRUE;
   }
@@ -764,2 +771,109 @@
 }
 
+void writeStars_PS1_V5 (FTable *ftable, double *X, double *Y, double *M, unsigned int *Flag, int Nstars) {
+
+  int i;
+  CMF_PS1_V5 *stars;
+  float flux, fSN;
+
+  // XXX add gaussian-distributed noise based on counts
+  // this needs to make different output 'stars' entries depending on the desired type
+  ALLOCATE (stars, CMF_PS1_V5, Nstars);
+  for (i = 0; i < Nstars; i++) {
+
+    flux = pow (10.0, -0.4*M[i]);
+    fSN = 1.0 / sqrt(flux);
+
+    stars[i].detID = i;
+    stars[i].X = X[i];
+    stars[i].Y = Y[i];
+    stars[i].dX = FX * fSN;
+    stars[i].dY = FY * fSN;
+
+    stars[i].posangle = 10.0;
+    stars[i].pltscale = 0.25;
+
+    stars[i].M = M[i];
+    stars[i].dM = fSN;
+
+    stars[i].Flux = flux;
+    stars[i].dFlux = flux * fSN;
+
+    stars[i].Map = M[i] - 0.05;
+    stars[i].MapRaw = M[i] - 0.10;
+
+    stars[i].apRadius = 8.0;
+
+    stars[i].apFlux = pow(10.0, -0.4*stars[i].Map);
+    stars[i].apFluxErr = stars[i].apFlux * fSN;
+
+    stars[i].Mcalib = M[i] + ZERO_POINT + 2.5*log10(exptime);
+    stars[i].dMcal = 0.05;
+
+    stars[i].dMcal = 0.05;
+
+    XY_to_RD (&stars[i].RA, &stars[i].DEC, X[i], Y[i], &coords);
+    stars[i].apNpix = 3.14*8.0*8.0;
+
+    stars[i].Mpeak     = M[i] + 1.0;
+    stars[i].sky       = SKY;
+    stars[i].dSky      = DSKY;
+    stars[i].psfChisq  = PSFCHI;
+    stars[i].crNsigma  = CRN;
+    stars[i].extNsigma = EXTN;
+    stars[i].fx        = FX;
+    stars[i].fy        = FY;
+    stars[i].df        = DF;
+
+    stars[i].k         = 1.0;
+    stars[i].fwhmMaj   = FX*2.8;
+    stars[i].fwhmMin   = FY*2.8;
+
+    // randomly give poor PSFQF values
+    if ((BAD_PSFQF_FRAC > 0.0) && (drand48() < BAD_PSFQF_FRAC)) {
+      stars[i].psfQF     = 0.25;
+      stars[i].psfQFperf = 0.24;
+    } else {
+      stars[i].psfQF     = PSFQUAL;
+      stars[i].psfQFperf = MAX(PSFQUAL - 0.01, 0.0);
+    }
+
+    stars[i].psfNdof   = 1;
+    stars[i].psfNpix   = 2;
+
+    stars[i].Mxx       = FX;
+    stars[i].Mxy       = 0.01;
+    stars[i].Myy       = FX;
+    stars[i].M3c       = FX;
+    stars[i].M3s       = FX;
+    stars[i].M4c       = FX;
+    stars[i].M4s       = FX;
+    stars[i].Mr1       = FX;
+    stars[i].Mrh       = FX;
+
+    stars[i].kronFlux  = flux * 1.25;
+    stars[i].kronFluxErr = fSN * flux * 1.25;
+
+    stars[i].kronInner = fSN * flux * 0.9;
+    stars[i].kronOuter = fSN * flux * 1.5;
+
+    stars[i].skyLimitRad = 1;
+    stars[i].skyLimitFlux = 2;
+    stars[i].skyLimitSlope = 0.2;
+
+    stars[i].flags     = Flag[i];
+    stars[i].flags2    = 0x80;
+
+    stars[i].nFrames   = 1;
+
+    if (ADDNOISE) {
+      stars[i].X += FX * fSN * rnd_gauss(0.0, 1.0);
+      stars[i].Y += FY * fSN * rnd_gauss(0.0, 1.0);
+      stars[i].M += fSN*rnd_gauss(0.0, 1.0);
+    }
+  }
+
+  gfits_table_set_CMF_PS1_V5 (ftable, stars, Nstars);
+  gfits_modify (ftable->header, "EXTTYPE",   "%s", 1, "PS1_V5");
+}
+
