Index: /trunk/Ohana/src/getstar/include/getstar.h
===================================================================
--- /trunk/Ohana/src/getstar/include/getstar.h	(revision 6811)
+++ /trunk/Ohana/src/getstar/include/getstar.h	(revision 6812)
@@ -12,4 +12,5 @@
 };
 
+# if (0)
 typedef struct {
   double X;
@@ -24,4 +25,5 @@
   int found;
 } Stars;
+# endif
 
 /* global variables set in parameter file */
Index: /trunk/Ohana/src/getstar/src/Shutdown.c
===================================================================
--- /trunk/Ohana/src/getstar/src/Shutdown.c	(revision 6812)
+++ /trunk/Ohana/src/getstar/src/Shutdown.c	(revision 6812)
@@ -0,0 +1,28 @@
+# include "getstar.h"
+
+static FITS_DB *db;
+
+void set_db (FITS_DB *in) {
+  db = in;
+}
+
+/* clean up open / locked ImageCat before shutting down */
+int Shutdown (char *format, ...) {  
+  va_list argp;
+  char *formatplus;
+  
+  ALLOCATE (formatplus, char, strlen(format));
+  strcpy (formatplus, format);
+  strcat (formatplus, "\n");
+
+  va_start (argp, format);
+  vfprintf (stderr, formatplus, argp);
+  free (formatplus);
+  va_end (argp);
+
+  SetProtect (TRUE);
+  fits_db_close (db);
+  fprintf (stderr, "ERROR: getstar halted\n");
+  exit (1);
+}
+
Index: /trunk/Ohana/src/getstar/src/getstar.c
===================================================================
--- /trunk/Ohana/src/getstar/src/getstar.c	(revision 6811)
+++ /trunk/Ohana/src/getstar/src/getstar.c	(revision 6812)
@@ -12,4 +12,10 @@
   set_db (&db);
 
+  sky = SkyTableLoadOptimal (CATDIR, SKY_TABLE, GSCFILE, SKY_DEPTH, VERBOSE);
+  SkyTableSetFilenames (sky, CATDIR, "cpt");
+
+  /* choose elements to load based on options */
+  catalog.catflags = LOAD_AVES | LOAD_MEAS | LOAD_MISS | LOAD_SECF;
+
   Nstars = 0;
   stars = NULL;
@@ -20,14 +26,25 @@
     case BY_RADIUS:
 
-      /* load corresponding sky regions, associated images */
-      status = dvo_image_lock (&db, ImageCat, 60.0, LCK_SOFT);
-      if (!status) Shutdown ("ERROR: failure to lock image catalog %s", db.filename);
+      /* load corresponding sky regions */
+      skylist = SkyListByPatch (sky, -1, &REGION);
+      for (i = 0; i < skylist[0].Nregions; i++) {
+	catalog.filename = skylist[0].filename[i];
+	load_pt_catalog (&catalog, skylist[0].regions[i]);
+	unlock_catalog (&catalog);
 
-      skylist = SkyListByPatch (sky, -1, &REGION);
-      overlap = gimages (&db, &image, MOSAIC, &Noverlap);
-      dvo_image_unlock (&db);
+	/* skip empty catalogs */
+	if (catalog.Nave_disk == 0) continue;
+	select_by_region (&output, &catalog, &REGION, 0, 0);
+      }
+      break;
 
+    case BY_IMLIST:
+      /* load image list */
+    case BY_IMAGE:
+
+      # if (0)
+      /* load corresponding sky regions */
+      skylist = SkyListByImage (sky, -1, &image);
       for (i = 0; i < skylist[0].Nregions; i++) {
-	  
 	  catalog.filename = skylist[0].filename[i];
 	  load_pt_catalog (&catalog, skylist[0].regions[i]);
@@ -36,7 +53,7 @@
 	  /* skip empty catalogs */
 	  if (catalog.Nave_disk == 0) continue;
-
-	  stars = find_matches (&catalog, &image, 0, 0, stars, &Nstars);
-
+	  stars = select_by_image (&catalog, &image, 0, 0, stars, &Nstars);
+      }
+      # endif
       break;
 
@@ -45,46 +62,10 @@
       break;
 
-    case BY_IMAGE:
-
-      break;
-
-    case BY_IMLIST:
-
+      /* not implemented */
       break;
   }
 
-  image = gimages (argv[1]);
-
-  region = gregions (&image[0], &Nregions);
-
-  stars = (Stars *) NULL;
-  Nstars = 0;
-
-  for (i = 0; i < Nregions; i++) {
-    if (VERBOSE) fprintf (stderr, "extracting from %s\n", region[i].filename);
-    catalog.filename = region[i].filename;  /* don't free region before catalog! */
-    switch (lock_catalog (&catalog, LCK_SOFT)) {
-    case 0:
-      fprintf (stderr, "ERROR: can't lock file\n");
-      exit (1);
-    case 1:
-      gcatalog (&catalog);
-      break;
-    case 2:
-      fprintf (stderr, "no data in %s, skipping\n", catalog.filename);
-      catalog.Naverage = 0;
-      catalog.Nmeasure = 0;
-      break;
-    default:
-      fprintf (stderr, "weird lock_catalog exit state\n");
-      exit (1);
-    }
-    unlock_catalog (&catalog);
-    stars = find_matches (&catalog, &image[0], 0, 0, stars, &Nstars);
-  }
-
   /* write out the selected stars */
-  clean_stars (stars, &Nstars, image);
-  wstars (argv[2], stars, Nstars, image);
+  wcatalog (argv[2], output);
   fprintf (stderr, "SUCCESS\n");
   exit (0);
Index: /trunk/Ohana/src/getstar/src/mkcatalog.c
===================================================================
--- /trunk/Ohana/src/getstar/src/mkcatalog.c	(revision 6812)
+++ /trunk/Ohana/src/getstar/src/mkcatalog.c	(revision 6812)
@@ -0,0 +1,122 @@
+# include "addstar.h"
+
+void mkcatalog (SkyRegion *region, Catalog *catalog) {
+  
+  int length, status;
+  char *path, *root, *file, *line;
+  time_t now;
+
+  if (VERBOSE) fprintf (stderr, "new catalog file: %s\n", catalog[0].filename);
+
+  /* set the recommended CATFORMAT */
+  catalog[0].catformat = DVO_FORMAT_UNDEF;
+  if (!strcasecmp (CATFORMAT, "INTERNAL"))  catalog[0].catformat = DVO_FORMAT_INTERNAL;
+  if (!strcasecmp (CATFORMAT, "LONEOS"))    catalog[0].catformat = DVO_FORMAT_LONEOS;
+  if (!strcasecmp (CATFORMAT, "ELIXIR"))    catalog[0].catformat = DVO_FORMAT_ELIXIR;
+  if (!strcasecmp (CATFORMAT, "PANSTARRS")) catalog[0].catformat = DVO_FORMAT_PANSTARRS;
+  if (!strcasecmp (CATFORMAT, "PMTEST"))    catalog[0].catformat = DVO_FORMAT_PMTEST;
+  if (catalog[0].catformat == DVO_FORMAT_UNDEF) {
+    fprintf (stderr, "invalid output catalog format\n");
+    exit (1);
+  }
+
+  /* set the recommended CATMODE */
+  catalog[0].catmode = DVO_MODE_UNDEF;
+  if (!strcasecmp (CATMODE, "RAW"))  catalog[0].catmode = DVO_MODE_RAW;
+  if (!strcasecmp (CATMODE, "MEF"))  catalog[0].catmode = DVO_MODE_MEF;
+  if (!strcasecmp (CATMODE, "SPLIT"))  catalog[0].catmode = DVO_MODE_SPLIT;
+  if (catalog[0].catmode == DVO_MODE_UNDEF) {
+    fprintf (stderr, "invalid output catalog mode\n");
+    exit (1);
+  }
+
+  fits_init_header (&catalog[0].header);
+
+  if (catalog[0].catmode == DVO_MODE_RAW) {
+    /* make header a fake image */
+    catalog[0].header.bitpix   = 16;
+    catalog[0].header.Naxes    = 2;
+    catalog[0].header.Naxis[0] = 1;
+    catalog[0].header.Naxis[1] = 1;
+  }
+
+  fits_create_header (&catalog[0].header);
+  
+  if (catalog[0].catmode == DVO_MODE_SPLIT) {
+    path = pathname (catalog[0].filename);
+    root = filerootname (catalog[0].filename);
+    length = strlen(path) + strlen(root) + 6;
+
+    /* define measure catalog file */
+    ALLOCATE (catalog[0].measure_catalog, Catalog, 1);
+    fits_init_header (&catalog[0].measure_catalog[0].header);
+    fits_create_header (&catalog[0].measure_catalog[0].header);
+    ALLOCATE (catalog[0].measure_catalog[0].filename, char, length);
+    sprintf (catalog[0].measure_catalog[0].filename, "%s/%s.cpm", path, root);
+    status = lock_catalog (catalog[0].measure_catalog, catalog[0].lockmode);
+    if (status != 2) {
+      fprintf (stderr, "error with file lock\n");
+      exit (2);
+    }
+    file = filebasename (catalog[0].measure_catalog[0].filename);
+    fits_modify (&catalog[0].header, "MEASURE", "%s", 1, file);
+    free (file);
+
+    /* define missing catalog file */
+    ALLOCATE (catalog[0].missing_catalog, Catalog, 1);
+    fits_init_header (&catalog[0].missing_catalog[0].header);
+    fits_create_header (&catalog[0].missing_catalog[0].header);
+    ALLOCATE (catalog[0].missing_catalog[0].filename, char, length);
+    sprintf (catalog[0].missing_catalog[0].filename, "%s/%s.cpn", path, root);
+    if (lock_catalog (catalog[0].missing_catalog, catalog[0].lockmode) != 2) {
+      fprintf (stderr, "error with file lock\n");
+      exit (2);
+    }
+    file = filebasename (catalog[0].missing_catalog[0].filename);
+    fits_modify (&catalog[0].header, "MISSING", "%s", 1, file);
+    free (file);
+
+    /* define secfilt catalog file */
+    ALLOCATE (catalog[0].secfilt_catalog, Catalog, 1);
+    fits_init_header (&catalog[0].secfilt_catalog[0].header);
+    fits_create_header (&catalog[0].secfilt_catalog[0].header);
+    ALLOCATE (catalog[0].secfilt_catalog[0].filename, char, length);
+    sprintf (catalog[0].secfilt_catalog[0].filename, "%s/%s.cps", path, root);
+    if (lock_catalog (catalog[0].secfilt_catalog, catalog[0].lockmode) != 2) {
+      fprintf (stderr, "error with file lock\n");
+      exit (2);
+    }
+    file = filebasename (catalog[0].secfilt_catalog[0].filename);
+    fits_modify (&catalog[0].header, "SECFILT", "%s", 1, file);
+    free (file);
+    free (path);
+    free (root);
+  }    
+
+  /* write RA,DEC range in header */
+  fits_modify (&catalog[0].header, "RA0",  "%lf", 1, region[0].Rmin);
+  fits_modify (&catalog[0].header, "DEC0", "%lf", 1, region[0].Dmin);
+  fits_modify (&catalog[0].header, "RA1",  "%lf", 1, region[0].Rmax);
+  fits_modify (&catalog[0].header, "DEC1", "%lf", 1, region[0].Dmax);
+
+  /* write creation date in header */
+  str_to_time ("now", &now);
+  line = sec_to_date (now);
+  fits_modify (&catalog[0].header, "DATE", "%s", 1, line);
+  free (line);
+
+  /* dummy allocation so realloc will succeed */
+  ALLOCATE (catalog[0].average, Average, 1);
+  ALLOCATE (catalog[0].measure, Measure, 1);
+  ALLOCATE (catalog[0].missing, Missing, 1);
+  ALLOCATE (catalog[0].secfilt, SecFilt, 1);
+  catalog[0].Naverage = catalog[0].Nmeasure = catalog[0].Nmissing = 0;
+  catalog[0].Nave_disk = catalog[0].Nmeas_disk = catalog[0].Nmiss_disk = 0;
+  catalog[0].Nmeas_off = 0;
+  catalog[0].Nsecfilt = GetPhotcodeNsecfilt ();
+
+  /* setup secondary filters to match photcodes:
+   * Nsecfilt is number of filters.  Number of entries in array is
+   * Nsecfilt * Naverage.  At this point, N entries == 0
+   */
+}
Index: /trunk/Ohana/src/getstar/src/select_by_image.c
===================================================================
--- /trunk/Ohana/src/getstar/src/select_by_image.c	(revision 6812)
+++ /trunk/Ohana/src/getstar/src/select_by_image.c	(revision 6812)
@@ -0,0 +1,57 @@
+# include "getstar.h"
+
+Stars *select_by_image (Catalog *catalog, Image *image, int start, int end, Stars *stars, int *Nstar) {
+
+  int i, n, N, NSTARS;
+  int photcode;
+
+  if (stars == (Stars *) NULL) {
+    N = 0;
+    NSTARS = 1000;
+    ALLOCATE (stars, Stars, NSTARS);
+  } else {
+    N = *Nstar;
+    NSTARS = N + 1000;
+    REALLOCATE (stars, Stars, NSTARS);
+  }    
+
+  /* identify selection criteria */
+  photcode = -1;
+  if ((start == 0) && (end == 0)) {
+    start = image[0].tzero;
+    end   = image[0].tzero + 1e-4*image[0].NY*image[0].trate;  /* trate is in 0.1 msec / row */
+    photcode = image[0].source;
+  }
+  if (VERBOSE) fprintf (stderr, "extracting for range %d to %d (photcode %s)\n", start, end, photcode);
+
+  for (i = 0; (i < catalog[0].Nmeasure); i++) {
+    if ((i % 10000) == 0) fprintf (stderr, ". ");
+    if ((catalog[0].measure[i].t >= start) && (catalog[0].measure[i].t <= end) && (photcode == catalog[0].measure[i].source)) { 
+      n = catalog[0].measure[i].averef;
+      stars[N].R      = catalog[0].average[n].R - catalog[0].measure[i].dR / 360000.0;
+      stars[N].D      = catalog[0].average[n].D - catalog[0].measure[i].dD / 360000.0;
+
+      stars[N].M      = 0.001*(catalog[0].measure[i].M - catalog[0].measure[i].dt);
+      stars[N].dM     = catalog[0].measure[i].dM;
+      stars[N].dophot = catalog[0].measure[i].dophot;  
+
+      stars[N].Mgal   = 0.001*(catalog[0].measure[i].Mgal - catalog[0].measure[i].dt);
+
+      stars[N].fx     = 0.01*catalog[0].measure[i].FWx;
+      stars[N].fy     = stars[N].fx * 0.01*catalog[0].measure[i].fwy;
+      stars[N].df     = (360.0/255.0)*catalog[0].measure[i].theta;
+      stars[N].found  = catalog[0].measure[i].flags;
+
+      N ++;
+      if (N == NSTARS) {
+	NSTARS += 1000;
+	REALLOCATE (stars, Stars, NSTARS);
+      }    
+
+    } 
+  }
+  fprintf (stderr, "found %d meas\n", N);
+  *Nstar = N;
+  return (stars);
+}
+
Index: /trunk/Ohana/src/getstar/src/select_by_region.c
===================================================================
--- /trunk/Ohana/src/getstar/src/select_by_region.c	(revision 6812)
+++ /trunk/Ohana/src/getstar/src/select_by_region.c	(revision 6812)
@@ -0,0 +1,59 @@
+# include "getstar.h"
+
+/* return 'stars' or return 'average' / 'measure' entries? */
+bool select_by_region (Catalog *output, Catalog *catalog, SkyRegion *region, int start, int end) {
+
+  int i, n, N, NSTARS;
+  int photcode;
+
+  if (output == NULL) Shutdown ("output not defined");
+
+  /* identify selection criteria */
+  if (VERBOSE) fprintf (stderr, "extracting for range %d to %d (photcode %s)\n", start, end, photcode);
+
+  /* select the average objects in this region */
+  Nave = output[0].Naverage;
+  NAVE = output[0].Naverage + 1000;
+  REALLOCATE (output[0].average, Average, NOUT);
+
+  Nmeas = output[0].Nmeasure;
+  NMEAS = output[0].Nmeasure + 1000;
+  REALLOCATE (output[0].measure, Measure, NMEAS);
+
+  for (i = 0; i < catalog[0].Naverage; i++) {
+    n = catalog[0].measure[i].averef;
+
+    R = catalog[0].average[i].R;
+    D = catalog[0].average[i].D;
+    
+    if (R < region[0].Rmin) continue;
+    if (R > region[0].Rman) continue;
+    if (D < region[0].Dmin) continue;
+    if (D > region[0].Dmax) continue;
+
+    output[0].average[Nave] = catalog[0].average[i];
+    output[0].average[Nave].offset = Nmeas;
+    for (j = 0; j < PhotNsec; j++) {
+      output[0].secfilt[PhotNsec*Nave + j] = catalog[0].secfilt[PhotNsec*i + j];
+    }
+
+    Nm = 0;
+    offset = catalog[0].average[i].offset;
+
+    for (j = 0; j < catalog[0].average[i].Nm; j++) {
+      output[0].measure[Nmeas] = catalog[0].measure[offset + j];
+      output[0].measure[Nmeas].averef = Nave;
+      Nmeas ++;
+      
+      CHECK_REALLOCATE (output[0].measure, Measure, NMEAS, Nmeas, 1000);
+    }      
+
+    Nave ++;
+    if (Nave == NAVE) {
+      NAVE += 1000;
+      REALLOCATE (output[0].average, Average, NAVE);
+      REALLOCATE (output[0].secfilt, SecFilt, NAVE*PhotNsec);
+  }
+  return (TRUE);
+}
+
Index: /trunk/Ohana/src/getstar/src/wcatalog.c
===================================================================
--- /trunk/Ohana/src/getstar/src/wcatalog.c	(revision 6812)
+++ /trunk/Ohana/src/getstar/src/wcatalog.c	(revision 6812)
@@ -0,0 +1,21 @@
+# include "uniphot.h"
+
+void wcatalog (char *filename, Catalog *catalog) {
+
+  /* CATMODE and CATFORMAT determined from catalog */
+
+  /* open file as appropriate */
+  if (!strcasecmp (filename, "stdout")) {
+    catalog[0].f = stdout;
+  } else {
+    catalog[0].f = fopen (filename, "w");
+    if (catalog[0].f == NULL) {
+      fprintf (stderr, "ERROR: failure opening catalog for output\n");
+      exit (1);
+    }
+  }    
+  if (!save_catalog (catalog, VERBOSE)) {
+    fprintf (stderr, "ERROR: failure saving catalog\n");
+    exit (1);
+  }
+}
