Index: /branches/eam_branches/ipp-20140904/Ohana/src/fakeastro/include/fakeastro.h
===================================================================
--- /branches/eam_branches/ipp-20140904/Ohana/src/fakeastro/include/fakeastro.h	(revision 37531)
+++ /branches/eam_branches/ipp-20140904/Ohana/src/fakeastro/include/fakeastro.h	(revision 37532)
@@ -155,4 +155,6 @@
 
 SkyRegion *get_image_patch (Image *image);
+SkyRegion *get_mosaic_patch (Image *image);
+
 int SkyRegionsOverlap (SkyRegion *region, SkyRegion *patch);
 int SkyRegionHasPoint (SkyRegion *region, double R, double D);
@@ -161,9 +163,9 @@
 Catalog *load_fake_stars (SkyList *skylist, int *ncatalog);
 
-Stars *make_fake_stars (Catalog *catalog, int Ncatalog, SkyList *skylist, Image *image, int *nstars);
+Stars *make_fake_stars (Catalog *catalog, int Ncatalog, SkyList *skylist, Image *image, Stars *stars, int *nstars);
 Stars *make_fake_stars_catalog (Stars *stars, int *nstars, SkyRegion *patch, Catalog *catalog, Image *image);
 
 int save_fake_stars (SkyTable *sky, Image *image, Stars *stars, int Nstars);
-int match_fake_stars (Stars *stars, unsigned int NstarsIn, SkyRegion *region, Catalog *catalog, Image *image);
+int match_fake_stars (Stars *stars, unsigned int NstarsIn, SkyRegion *region, Catalog *catalog);
 
 int InitStar (Stars *star);
Index: /branches/eam_branches/ipp-20140904/Ohana/src/fakeastro/src/fakeastro_images.c
===================================================================
--- /branches/eam_branches/ipp-20140904/Ohana/src/fakeastro/src/fakeastro_images.c	(revision 37531)
+++ /branches/eam_branches/ipp-20140904/Ohana/src/fakeastro/src/fakeastro_images.c	(revision 37532)
@@ -4,4 +4,6 @@
 
 int fakeastro_images () {
+
+  INITTIME;
 
   FITS_DB dbfake, dbtrue;
@@ -37,4 +39,6 @@
   save_image_db (&dbfake, imageInfo.fakeImage, imageInfo.NfakeImage);
   save_image_db (&dbtrue, imageInfo.trueImage, imageInfo.NtrueImage);
+
+  MARKTIME ("generate fake stars in %f sec\n", dtime)
 
   exit (0);
Index: /branches/eam_branches/ipp-20140904/Ohana/src/fakeastro/src/fakeastro_images_region.c
===================================================================
--- /branches/eam_branches/ipp-20140904/Ohana/src/fakeastro/src/fakeastro_images_region.c	(revision 37531)
+++ /branches/eam_branches/ipp-20140904/Ohana/src/fakeastro/src/fakeastro_images_region.c	(revision 37532)
@@ -40,4 +40,7 @@
     }
 
+    int NfakeStars = 0;
+    Stars *fakeStars = NULL;
+
     for (j = 0; j < NfakeImage; j++) {
 
@@ -45,13 +48,15 @@
       if (strcmp(&fakeImage[j].coords.ctype[4], "-WRP")) continue;
 
-      int NfakeStars;
-      Stars *fakeStars = make_fake_stars (catalog, Ncatalog, skyListInput, &fakeImage[j], &NfakeStars);
+      int Nstart = NfakeStars;
+      fakeStars = make_fake_stars (catalog, Ncatalog, skyListInput, &fakeImage[j], fakeStars, &NfakeStars);
       
-      fit_fake_stars (fakeStars, NfakeStars, &fakeImage[j]);
+      // only fit the new stars to this image
+      fit_fake_stars (&fakeStars[Nstart], NfakeStars - Nstart, &fakeImage[j]);
 
-      save_fake_stars (skyTableOutput, &fakeImage[j], fakeStars, NfakeStars);
-
-      free (fakeStars);
+      fprintf (stderr, "%s : %d\n", fakeImage[j].name, NfakeStars - Nstart);
     }
+    // send in just the PHU image for reference
+    save_fake_stars (skyTableOutput, &fakeImage[0], fakeStars, NfakeStars);
+    free (fakeStars);
 
     // append the new fake images to the end of the full set:
Index: /branches/eam_branches/ipp-20140904/Ohana/src/fakeastro/src/get_image_patch.c
===================================================================
--- /branches/eam_branches/ipp-20140904/Ohana/src/fakeastro/src/get_image_patch.c	(revision 37531)
+++ /branches/eam_branches/ipp-20140904/Ohana/src/fakeastro/src/get_image_patch.c	(revision 37532)
@@ -9,4 +9,42 @@
   static double Xpt[] = {0.0, 0.5, 1.0, 0.0, 1.0, 0.0, 0.5, 1.0};
   static double Ypt[] = {0.0, 0.0, 0.0, 0.5, 0.5, 1.0, 1.0, 1.0};
+
+  double Rmin = +480.0;
+  double Rmax = -360.0;
+  double Dmin =  +90.0;
+  double Dmax =  -90.0;
+
+  int i;
+  for (i = 0; i < 8; i++) {
+    double R, D;
+    XY_to_RD (&R, &D, Xpt[i]*image->NX, Ypt[i]*image->NY, &image->coords);
+
+    // fprintf (stderr, "%f %f -> %f %f\n", Xpt[i]*image->NX, Ypt[i]*image->NY, R, D); 
+    Rmin = MIN(R,Rmin);
+    Rmax = MAX(R,Rmax);
+    Dmin = MIN(D,Dmin);
+    Dmax = MAX(D,Dmax);
+  }
+
+  ALLOCATE (region, SkyRegion, 1);
+
+  region->Rmin = Rmin;
+  region->Rmax = Rmax;
+  region->Dmin = Dmin;
+  region->Dmax = Dmax;
+
+  // fprintf (stderr, "%f %f , %f %f\n", Rmin, Rmax, Dmin, Dmax);
+
+  return region;
+}
+
+SkyRegion *get_mosaic_patch (Image *image) {
+
+  SkyRegion *region;
+
+  // find the R,D coords of the 4 corners and 4 edge midpoints
+
+  static double Xpt[] = {-0.5,  0.0,  0.5, -0.5, 0.5, -0.5, 0.0, 0.5};
+  static double Ypt[] = {-0.5, -0.5, -0.5,  0.0, 0.0,  0.5, 0.5, 0.5};
 
   double Rmin = +480.0;
Index: /branches/eam_branches/ipp-20140904/Ohana/src/fakeastro/src/make_fake_stars.c
===================================================================
--- /branches/eam_branches/ipp-20140904/Ohana/src/fakeastro/src/make_fake_stars.c	(revision 37531)
+++ /branches/eam_branches/ipp-20140904/Ohana/src/fakeastro/src/make_fake_stars.c	(revision 37532)
@@ -1,5 +1,5 @@
 # include "fakeastro.h"
 
-Stars *make_fake_stars (Catalog *catalog, int Ncatalog, SkyList *skylist, Image *image, int *nstars) {
+Stars *make_fake_stars (Catalog *catalog, int Ncatalog, SkyList *skylist, Image *image, Stars *stars, int *nstars) {
 
   // patch is generous region around image, but limited ot this image
@@ -8,6 +8,5 @@
   // fprintf (stderr, "image patch: %f %f , %f %f\n", imagePatch->Rmin, imagePatch->Rmax, imagePatch->Dmin, imagePatch->Dmax);
 
-  int Nstars = 0;
-  Stars *stars = NULL;
+  int Nstars = *nstars;
 
   // load stars from database in these regions
Index: /branches/eam_branches/ipp-20140904/Ohana/src/fakeastro/src/make_fake_stars_catalog.c
===================================================================
--- /branches/eam_branches/ipp-20140904/Ohana/src/fakeastro/src/make_fake_stars_catalog.c	(revision 37531)
+++ /branches/eam_branches/ipp-20140904/Ohana/src/fakeastro/src/make_fake_stars_catalog.c	(revision 37532)
@@ -41,5 +41,6 @@
 
   // XXX put in airmass?
-  float ZP = SCALE*code->C - image->Mcal + Mtime;
+  float ZP  = SCALE*code->C - image->Mcal + Mtime;
+  float ZPo = 25.0 - image->Mcal + Mtime;
   // float ZP = code[0].K*(measure[0].airmass - 1.000) + SCALE*code[0].C - measure[0].Mcal;
   
@@ -74,4 +75,7 @@
 
     // which filter?
+    // float Mraw = Minst + measure[0].dt + 25.0;
+    // float Minst = Msky - SCALE*code->C - measure[0].dt
+
     double Minst = secfilt[i*Nsecfilt + Nsec].M  - ZP;
     double Counts = pow(10.0, -0.4*Minst);
@@ -80,8 +84,11 @@
     double SN = Counts / sqrt(SkyCts + Counts);
 
+    // XXX skip stars with low S/N
+    if (SN < 5.0) continue;
+
     // true position from src catalog
     double Rtru = average[i].R;
     double Dtru = average[i].D;
-
+    
     stars[Nstars].starpar = starpar[nStar]; // keep this so we can save it on the generated stars
 
@@ -140,5 +147,5 @@
     // stars[Nstars].measure.pltscale   = ps1data[i].pltscale;
 
-    stars[Nstars].measure.M      = Minst + ZP; // XX I need to compensate for the internal zero point of 25.0
+    stars[Nstars].measure.M      = Minst + ZPo; // XX I need to compensate for the internal zero point of 25.0
     stars[Nstars].measure.dM     = 1.0 / SN;
 
@@ -152,4 +159,6 @@
     stars[Nstars].average.R = Robs;
     stars[Nstars].average.D = Dobs;
+
+    stars[Nstars].measure.photcode = image->photcode;
 
     stars[Nstars].measure.airmass = airmass (image->secz, stars[Nstars].average.R, stars[Nstars].average.D, image->sidtime, image->latitude);
Index: /branches/eam_branches/ipp-20140904/Ohana/src/fakeastro/src/match_fake_stars.c
===================================================================
--- /branches/eam_branches/ipp-20140904/Ohana/src/fakeastro/src/match_fake_stars.c	(revision 37531)
+++ /branches/eam_branches/ipp-20140904/Ohana/src/fakeastro/src/match_fake_stars.c	(revision 37532)
@@ -1,8 +1,5 @@
 # include "fakeastro.h"
 
-// Image *image is only used to set the equiv photcode, which in turn is used to set Nsec, which 
-// is only used to set the average photometry, but is not really needed.
-
-int match_fake_stars (Stars *stars, unsigned int NstarsIn, SkyRegion *region, Catalog *catalog, Image *image) {
+int match_fake_stars (Stars *stars, unsigned int NstarsIn, SkyRegion *region, Catalog *catalog) {
   
   off_t i, j, n, N, J, Jmin, status, Nstars;
@@ -16,9 +13,4 @@
 
   int Nsecfilt = GetPhotcodeNsecfilt ();
-
-  // which equiv photcode are we?
-  PhotCode *code = GetPhotcodebyCode (image->photcode);
-  int Nsec = GetPhotcodeNsec (code->equiv);
-  myAssert (Nsec >= 0, "undefined Nsec?");
 
   /** allocate local arrays (stars) **/
@@ -197,12 +189,4 @@
     }
 
-    /* set the average magnitude if not already set and the photcode.equiv is not 0 */
-    /* in UPDATE mode, this value is not saved; use relphot to recalculate */
-    if (Nsec > -1) { 
-      if (isnan(catalog[0].secfilt[n*Nsecfilt+Nsec].M)) {
-	catalog[0].secfilt[n*Nsecfilt+Nsec].M = PhotCat (&catalog[0].measure[Nmeas], MAG_CLASS_PSF);
-      }
-    }
-
     /* Nm is updated, but not written out in -update mode (for existing entries)
        Nm is recalculated in build_meas_links if loaded table is not sorted */
@@ -265,10 +249,4 @@
     catalog[0].measure[Nmeas].catID    = catalog[0].catID;
 
-    /* set the average magnitude if not already set and the photcode.equiv is not 0 */
-    /* XXX NOTE : this value is not saved; use relphot to recalculate */
-    if (Nsec > -1) { 
-      catalog[0].secfilt[Nave*Nsecfilt+Nsec].M = PhotCat (&catalog[0].measure[Nmeas], MAG_CLASS_PSF);
-    }
-    
     // supply the starpar values from this detection
     dvo_starpar_init (&catalog[0].starpar[Nstarpar]);
Index: /branches/eam_branches/ipp-20140904/Ohana/src/fakeastro/src/save_fake_stars.c
===================================================================
--- /branches/eam_branches/ipp-20140904/Ohana/src/fakeastro/src/save_fake_stars.c	(revision 37531)
+++ /branches/eam_branches/ipp-20140904/Ohana/src/fakeastro/src/save_fake_stars.c	(revision 37532)
@@ -1,13 +1,13 @@
 # include "fakeastro.h"
 
-int save_fake_stars (SkyTable *sky, Image *image, Stars *stars, int Nstars) {
+int save_fake_stars (SkyTable *sky, Image *mosaic, Stars *stars, int Nstars) {
 
   Catalog catalog;
 
   // patch is generous region around image, but limited to this image
-  SkyRegion *imagePatch = get_image_patch (image);
+  SkyRegion *mosaicPatch = get_mosaic_patch (mosaic);
 
   // list of regions which cover this image
-  SkyList *skylist = SkyListByPatch (sky, -1, imagePatch);
+  SkyList *skylist = SkyListByPatch (sky, -1, mosaicPatch);
 
   int Naverage = 0;
@@ -23,5 +23,6 @@
     catalog.catformat = dvo_catalog_catformat (CATFORMAT);  // set the default catformat from config data
     catalog.catmode   = dvo_catalog_catmode (CATMODE);      // set the default catmode from config data
-    catalog.catflags  = LOAD_AVES | LOAD_SECF | LOAD_MEAS | LOAD_STARPAR;
+    // catalog.catflags  = LOAD_AVES | LOAD_SECF | LOAD_MEAS | LOAD_STARPAR; // use this for non-update mode
+    catalog.catflags  = LOAD_AVES | LOAD_SECF | LOAD_STARPAR;
     catalog.Nsecfilt  = GetPhotcodeNsecfilt ();
 
@@ -32,5 +33,5 @@
     }
 
-    match_fake_stars (stars, Nstars, skylist[0].regions[i], &catalog, image);
+    match_fake_stars (stars, Nstars, skylist[0].regions[i], &catalog);
 
     /* report total updated values */
@@ -47,5 +48,5 @@
   }
 
-  free (imagePatch);
+  free (mosaicPatch);
   SkyListFree (skylist);
   return TRUE;
