Index: /branches/eam_branches/ipp-20140904/Ohana/src/fakeastro/Makefile
===================================================================
--- /branches/eam_branches/ipp-20140904/Ohana/src/fakeastro/Makefile	(revision 37463)
+++ /branches/eam_branches/ipp-20140904/Ohana/src/fakeastro/Makefile	(revision 37464)
@@ -39,7 +39,9 @@
 $(SRC)/gaussian.$(ARCH).o \
 $(SRC)/fakeastro_images.$(ARCH).o \
+$(SRC)/fakeastro_images_region.$(ARCH).o \
 $(SRC)/load_template_images.$(ARCH).o \
 $(SRC)/make_fake_images.$(ARCH).o \
 $(SRC)/get_image_patch.$(ARCH).o \
+$(SRC)/load_fake_stars.$(ARCH).o \
 $(SRC)/make_fake_stars.$(ARCH).o \
 $(SRC)/make_fake_stars_catalog.$(ARCH).o \
Index: /branches/eam_branches/ipp-20140904/Ohana/src/fakeastro/include/fakeastro.h
===================================================================
--- /branches/eam_branches/ipp-20140904/Ohana/src/fakeastro/include/fakeastro.h	(revision 37463)
+++ /branches/eam_branches/ipp-20140904/Ohana/src/fakeastro/include/fakeastro.h	(revision 37464)
@@ -114,9 +114,18 @@
 Image *make_fake_images (Image *image, int *nfakeImage);
 
+Image *fakeastro_images_region (Image *image, int *nimage, int *NIMAGE, Image *refImage, int NrefImage, SkyTable *skyTableInput, SkyTable *skyTableOutput, SkyRegion *region);
+
 SkyRegion *get_image_patch (Image *image);
-Stars *make_fake_stars (SkyTable *sky, SkyRegion *patch, Image *image, int *nstars);
+int SkyRegionsOverlap (SkyRegion *region, SkyRegion *patch);
+int SkyRegionHasPoint (SkyRegion *region, double R, double D);
+SkyRegion *SkyRegionExpand (SkyRegion *region, float boundary);
+
+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 (Stars *stars, int *nstars, SkyRegion *patch, Catalog *catalog, Image *image);
-int save_fake_stars (SkyTable *sky, SkyRegion *patch, Image *image, Stars *stars, int Nstars);
-int match_fake_stars (SkyRegion *region, Stars *stars, unsigned int NstarsIn, 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 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 37463)
+++ /branches/eam_branches/ipp-20140904/Ohana/src/fakeastro/src/fakeastro_images.c	(revision 37464)
@@ -3,9 +3,8 @@
 int fakeastro_images () {
 
-  int i, j;
 
   FITS_DB db;
 
-  /*** update the image table ***/
+  // we are creating a new image table (what about db ID?)
   snprintf (ImageCat, DVO_MAX_PATH, "%s/Images.dat", CATDIR_OUTPUT);
 
@@ -32,4 +31,9 @@
   SkyTableSetFilenames (skyTableOutput, CATDIR_OUTPUT, "cpt");
 
+  // subset out the DIS exposures here:
+
+  int NrefImage;
+  Image *refImage = load_template_images (&NrefImage);
+  
   int Nimage = 0;
   int NIMAGE = 1000;
@@ -38,42 +42,5 @@
   ALLOCATE (image, Image, NIMAGE);
 
-  int Nrefimage;
-  Image *refimage = load_template_images (&Nrefimage);
-  
-  for (i = 0; i < Nrefimage; i++) {
-
-    // we only want to make fake images for the exposures
-    if (strcmp(&refimage[i].coords.ctype[4], "-DIS")) continue;
-
-    int NfakeImage;
-    Image *fakeImage = make_fake_images (&refimage[i], &NfakeImage);
-    
-    for (j = 0; j < NfakeImage; j++) {
-
-      // we only want to make fake stars for the fake chips
-      if (strcmp(&fakeImage[j].coords.ctype[4], "-WRP")) continue;
-
-      SkyRegion *patch = get_image_patch (&fakeImage[j]);
-
-      int NfakeStars;
-      Stars *fakeStars = make_fake_stars (skyTableInput, patch, &fakeImage[j], &NfakeStars);
-      
-      save_fake_stars (skyTableOutput, patch, &fakeImage[j], fakeStars, NfakeStars);
-
-      free (fakeStars);
-      free (patch);
-    }
-
-    if (Nimage + NfakeImage >= NIMAGE) {
-      NIMAGE += 1000;
-      REALLOCATE (image, Image, NIMAGE);
-    }
-
-    for (j = 0; j < NfakeImage; j++) {
-      memcpy (&image[Nimage], &fakeImage[j], sizeof(Image));
-    }
-    
-    free (fakeImage);
-  }
+  image = fakeastro_images_region (image, &Nimage, &NIMAGE, refImage, NrefImage, skyTableInput, skyTableOutput, &UserPatch);
 
   /* add the new image and save */
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 37464)
+++ /branches/eam_branches/ipp-20140904/Ohana/src/fakeastro/src/fakeastro_images_region.c	(revision 37464)
@@ -0,0 +1,85 @@
+# include "fakeastro.h"
+
+Image *fakeastro_images_region (Image *image, int *nimage, int *NIMAGE, Image *refImage, int NrefImage, SkyTable *skyTableInput, SkyTable *skyTableOutput, SkyRegion *innerRegion) {
+
+  int Nimage = *nimage;
+
+  // extend the outer region by 2.0 degrees
+  SkyRegion *outerRegion = SkyRegionExpand (innerRegion, 2.0);
+
+  SkyList *skyListInput  = SkyListByPatch (skyTableInput, -1, outerRegion);
+
+  // load all catalogs for this extended region (catalog and skyListInput are 1-to-1 in regions)
+  int Ncatalog;
+  Catalog *catalog = load_fake_stars (skyListInput, &Ncatalog);
+
+  int i;
+  for (i = 0; i < NrefImage; i++) {
+
+    // we only want to make fake images for the exposures.  NOTE : we should define
+    // template to only include DIS, but this is for safety.
+    if (strcmp(&refImage[i].coords.ctype[4], "-DIS")) continue;
+
+    // choose only images in this (inner) region
+    double Rc, Dc;
+    XY_to_RD (&Rc, &Dc, 0.0, 0.0, &refImage[i].coords);
+    Rc = ohana_normalize_angle (Rc);
+
+    // exposure center must be in innerRegion:
+    if (!SkyRegionHasPoint(innerRegion, Rc, Dc)) continue;
+
+    int NfakeImage;
+    Image *fakeImage = make_fake_images (&refImage[i], &NfakeImage);
+    
+    int j;
+    for (j = 0; j < NfakeImage; j++) {
+
+      // we only want to make fake stars for the fake chips
+      if (strcmp(&fakeImage[j].coords.ctype[4], "-WRP")) continue;
+
+      int NfakeStars;
+      Stars *fakeStars = make_fake_stars (catalog, Ncatalog, skyListInput, &fakeImage[j], &NfakeStars);
+      
+      save_fake_stars (skyTableOutput, &fakeImage[j], fakeStars, NfakeStars);
+
+      free (fakeStars);
+    }
+
+    if (Nimage + NfakeImage >= *NIMAGE) {
+      *NIMAGE += 1000 + NfakeImage;
+      REALLOCATE (image, Image, *NIMAGE);
+    }
+
+    for (j = 0; j < NfakeImage; j++) {
+      memcpy (&image[Nimage], &fakeImage[j], sizeof(Image));
+      Nimage ++;
+    }
+    
+    free (fakeImage);
+  }
+
+  SkyListFree (skyListInput);
+
+
+  for (i = 0; i < Ncatalog; i++) {
+    dvo_catalog_free (&catalog[i]);
+  }
+
+  *nimage = Nimage;
+  return (image);
+}
+
+/* 
+   
+   given a modest sized region (Rmin,Rmax,Dmin,Dmax):
+   define an exterior region 
+
+   * define an outer region with borders 2 linear deg wider 
+
+   * load all input catalogs for the outer region
+
+   * select all images with exposure centers in the inner region
+
+   * generate fake stars for the selected images
+
+   */
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 37463)
+++ /branches/eam_branches/ipp-20140904/Ohana/src/fakeastro/src/get_image_patch.c	(revision 37464)
@@ -35,2 +35,39 @@
   return region;
 }
+
+int SkyRegionHasPoint (SkyRegion *region, double R, double D) {
+
+  if (D >= region->Dmin) return FALSE;
+  if (D <  region->Dmax) return FALSE;
+  if (R >= region->Rmin) return FALSE;
+  if (R <  region->Rmax) return FALSE;
+  return TRUE;
+}
+
+int SkyRegionsOverlap (SkyRegion *region, SkyRegion *patch) {
+
+  if (region->Rmin >= patch->Rmax) return FALSE;
+  if (region->Rmax <= patch->Rmin) return FALSE;
+  if (region->Dmin >= patch->Dmax) return FALSE;
+  if (region->Dmax <= patch->Dmin) return FALSE;
+  return TRUE;
+}
+
+SkyRegion *SkyRegionExpand (SkyRegion *region, float boundary) {
+
+  SkyRegion *output = NULL;
+  ALLOCATE (output, SkyRegion, 1);
+
+  output->Dmin = region->Dmin - boundary;
+  output->Dmax = region->Dmax + boundary;
+
+  float dRmin = boundary / cos (RAD_DEG*region->Dmin);
+  float dRmax = boundary / cos (RAD_DEG*region->Dmin);
+
+  float dR = MAX (dRmin, dRmax);
+
+  output->Rmin = region->Rmin - dR;
+  output->Rmax = region->Rmax + dR;
+
+  return output;
+}
Index: /branches/eam_branches/ipp-20140904/Ohana/src/fakeastro/src/load_fake_stars.c
===================================================================
--- /branches/eam_branches/ipp-20140904/Ohana/src/fakeastro/src/load_fake_stars.c	(revision 37464)
+++ /branches/eam_branches/ipp-20140904/Ohana/src/fakeastro/src/load_fake_stars.c	(revision 37464)
@@ -0,0 +1,34 @@
+# include "fakeastro.h"
+
+Catalog *load_fake_stars (SkyList *skylist, int *ncatalog) {
+
+  Catalog *catalog = NULL;
+  ALLOCATE (catalog, Catalog, skylist->Nregions);
+
+  // load stars from database in these regions
+  int i;
+  for (i = 0; i < skylist->Nregions; i++) {
+    dvo_catalog_init (&catalog[i], TRUE);
+
+    // set the parameters which guide catalog open/load/create
+    catalog[i].filename  = skylist[0].filename[i];
+    catalog[i].catformat = dvo_catalog_catformat (CATFORMAT);  // set the default catformat from config data
+    catalog[i].catmode   = dvo_catalog_catmode (CATMODE);      // set the default catmode from config data
+    catalog[i].catflags  = LOAD_AVES | LOAD_SECF | LOAD_STARPAR;
+    catalog[i].Nsecfilt  = GetPhotcodeNsecfilt ();
+    if (!dvo_catalog_open (&catalog[i], skylist[0].regions[i], VERBOSE, "r")) {
+      fprintf (stderr, "ERROR: failure reading catalog %s\n", catalog[i].filename);
+      exit (1);
+    }
+    dvo_catalog_unlock (&catalog[i]);
+
+    if (!catalog[i].Naves_disk) {
+	if (VERBOSE2) { fprintf (stderr, "no data in %s, skipping\n", catalog[i].filename); }
+	dvo_catalog_free (&catalog[i]);
+	continue;
+    }
+  }
+
+  *ncatalog = skylist->Nregions;
+  return catalog;
+}
Index: /branches/eam_branches/ipp-20140904/Ohana/src/fakeastro/src/load_template_images.c
===================================================================
--- /branches/eam_branches/ipp-20140904/Ohana/src/fakeastro/src/load_template_images.c	(revision 37463)
+++ /branches/eam_branches/ipp-20140904/Ohana/src/fakeastro/src/load_template_images.c	(revision 37464)
@@ -1,5 +1,5 @@
 # include "fakeastro.h"
 
-Image *load_template_images (int *nimage) {
+Image *load_template_images (int *nrefimage) {
 
   FITS_DB db;
@@ -13,6 +13,6 @@
   if (db.dbstate == LCK_EMPTY) {
     dvo_image_unlock (&db); // unlock input
-    *nimage = 0;
-    return NULL;
+    fprintf (stderr, "ERROR: no template images in %s\n", IMAGES_INPUT);
+    exit (3);
   }
 
@@ -26,9 +26,29 @@
   Image *image = gfits_table_get_Image (&db.ftable, &Nimage, &db.swapped);
   if (!image) {
-    fprintf (stderr, "ERROR: failed to read images\n");
-    return (NULL);
+    fprintf (stderr, "ERROR: no template images in %s (though db exists)\n", IMAGES_INPUT);
+    exit (3);
   }
 
-  *nimage = Nimage;
-  return image;
+  int NREFIMAGE = 1000;
+  int Nrefimage = 0;
+  Image *refimage;
+  ALLOCATE (refimage, Image, NREFIMAGE);
+
+  int i;
+  for (i = 0; i < Nimage; i++) {
+
+    // we only want to make fake images for the exposures
+    if (strcmp(&image[i].coords.ctype[4], "-DIS")) continue;
+
+    refimage[Nrefimage] = image[i];
+
+    Nrefimage ++;
+    CHECK_REALLOCATE (refimage, Image, NREFIMAGE, Nrefimage, 1000);
+  }
+
+  // free the input image database
+  gfits_db_free (&db);
+
+  *nrefimage = Nrefimage;
+  return refimage;
 }
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 37463)
+++ /branches/eam_branches/ipp-20140904/Ohana/src/fakeastro/src/make_fake_stars.c	(revision 37464)
@@ -1,35 +1,25 @@
 # include "fakeastro.h"
 
-Stars *make_fake_stars (SkyTable *sky, SkyRegion *patch, Image *image, int *nstars) {
+Stars *make_fake_stars (Catalog *catalog, int Ncatalog, SkyList *skylist, Image *image, int *nstars) {
 
-  SkyList *skylist  = SkyListByPatch (sky, -1, patch);
+  // patch is generous region around image, but limited ot this image
+  SkyRegion *imagePatch = get_image_patch (image);
 
   int Nstars = 0;
   Stars *stars = NULL;
 
-  Catalog catalog;
-
   // load stars from database in these regions
   int i;
-  for (i = 0; i < skylist->Nregions; i++) {
+  for (i = 0; i < Ncatalog; i++) {
+    // skylist matches catalog
 
-    // set the parameters which guide catalog open/load/create
-    catalog.filename  = skylist[0].filename[i];
-    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_STARPAR;
-    catalog.Nsecfilt  = GetPhotcodeNsecfilt ();
-    if (!dvo_catalog_open (&catalog, skylist[0].regions[i], VERBOSE, "r")) {
-      continue;
-    }
+    // XXX check that catalog overlaps patch
+    if (!SkyRegionsOverlap(skylist[0].regions[i], imagePatch)) continue;
 
     // generate fake measurements for this image
-    stars = make_fake_stars_catalog (stars, &Nstars, patch, &catalog, image);
-   
-    dvo_catalog_unlock (&catalog);
-    dvo_catalog_free (&catalog);
+    stars = make_fake_stars_catalog (stars, &Nstars, imagePatch, &catalog[i], image);
   }
 
-  SkyListFree (skylist);
+  free (imagePatch);
 
   *nstars = Nstars;
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 37463)
+++ /branches/eam_branches/ipp-20140904/Ohana/src/fakeastro/src/make_fake_stars_catalog.c	(revision 37464)
@@ -10,15 +10,20 @@
 // * what about QSOs?
 
-Stars *make_fake_stars_catalog (Stars *stars, int *nstars, SkyRegion *region, Catalog *catalog, Image *image) {
+/* used in find_matches, find_matches_refstars */
+# define IN_PATCH(REGION,R,D) (			     \
+((D) >= REGION[0].Dmin) && ((D) < REGION[0].Dmax) && \
+((R) >= REGION[0].Rmin) && ((R) < REGION[0].Rmax))
+
+// region corresponds to the catalog
+Stars *make_fake_stars_catalog (Stars *stars, int *nstars, SkyRegion *imagePatch, Catalog *catalog, Image *image) {
 
   int Nstars = *nstars;
-  *nstars += catalog->Naverage;
 
   time_t timeRef = ohana_date_to_sec("2011/05/11,00:00:00");
 
   if (!stars) {
-    ALLOCATE (stars, Stars, *nstars);
+    ALLOCATE (stars, Stars, catalog->Naverage);
   } else {
-    REALLOCATE (stars, Stars, *nstars);
+    REALLOCATE (stars, Stars, Nstars + catalog->Naverage);
   }
 
@@ -49,5 +54,5 @@
 
     // true position from src catalog
-    if (!IN_REGION(average[i].R, average[i].D)) continue;
+    if (!IN_PATCH(imagePatch, average[i].R, average[i].D)) continue;
 
     int nStar = average[i].starparOffset;
@@ -116,8 +121,8 @@
     // this is may optionally be replaced by the internal sequence (see FilterStars.c)
     stars[Nstars].measure.detID      = Nstars + 1;
-    
     Nstars ++;
   }
 
+  *nstars = Nstars;
   return stars;
 }
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 37463)
+++ /branches/eam_branches/ipp-20140904/Ohana/src/fakeastro/src/match_fake_stars.c	(revision 37464)
@@ -1,5 +1,5 @@
 # include "fakeastro.h"
 
-int match_fake_stars (SkyRegion *region, Stars *stars, unsigned int NstarsIn, Catalog *catalog, Image *image) {
+int match_fake_stars (Stars *stars, unsigned int NstarsIn, SkyRegion *region, Catalog *catalog, Image *image) {
   
   off_t i, j, n, N, J, Jmin, status, Nstars;
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 37463)
+++ /branches/eam_branches/ipp-20140904/Ohana/src/fakeastro/src/save_fake_stars.c	(revision 37464)
@@ -3,10 +3,13 @@
 # define RESETTIME { gettimeofday (&startTimer, (void *) NULL); }
 
-int save_fake_stars (SkyTable *sky, SkyRegion *patch, Image *image, Stars *stars, int Nstars) {
+int save_fake_stars (SkyTable *sky, Image *image, Stars *stars, int Nstars) {
 
   Catalog catalog;
 
-  // will these match, or should I mangle the input cpt names?
-  SkyList *skylist = SkyListByPatch (sky, -1, patch);
+  // patch is generous region around image, but limited to this image
+  SkyRegion *imagePatch = get_image_patch (image);
+
+  // list of regions which cover this image
+  SkyList *skylist = SkyListByPatch (sky, -1, imagePatch);
 
   int Naverage = 0;
@@ -15,5 +18,4 @@
   INITTIME;
 
-  // load stars from database in these regions
   int i;
   for (i = 0; i < skylist->Nregions; i++) {
@@ -26,4 +28,5 @@
     catalog.Nsecfilt  = GetPhotcodeNsecfilt ();
 
+    // I want to do an update here
     if (!dvo_catalog_open (&catalog, skylist[0].regions[i], VERBOSE, "w")) {
       fprintf (stderr, "ERROR: failure to open/create catalog file %s\n", catalog.filename);
@@ -31,5 +34,5 @@
     }
 
-    match_fake_stars (skylist[0].regions[i], stars, Nstars, &catalog, image);
+    match_fake_stars (stars, Nstars, skylist[0].regions[i], &catalog, image);
 
     /* report total updated values */
@@ -38,5 +41,5 @@
 
     SetProtect (TRUE);
-    catalog.catflags  = LOAD_AVES | LOAD_MEAS | LOAD_MISS | LOAD_SECF | LOAD_LENSING;
+    catalog.catflags  = LOAD_AVES | LOAD_MEAS | LOAD_MISS | LOAD_SECF;
     dvo_catalog_update (&catalog, VERBOSE);
     SetProtect (FALSE);
@@ -47,5 +50,8 @@
   }
 
+  free (imagePatch);
   SkyListFree (skylist);
   return TRUE;
 }
+
+// XXX what about starpar values?
