Index: /trunk/Ohana/src/photdbc/Makefile
===================================================================
--- /trunk/Ohana/src/photdbc/Makefile	(revision 8629)
+++ /trunk/Ohana/src/photdbc/Makefile	(revision 8630)
@@ -24,6 +24,10 @@
 $(SRC)/ConfigInit.$(ARCH).o	   \
 $(SRC)/args.$(ARCH).o		   \
+$(SRC)/copy_images.$(ARCH).o	   \
+$(SRC)/Shutdown.$(ARCH).o          \
+$(SRC)/make_subcatalog.$(ARCH).o        
+
+OLD = \
 $(SRC)/liststats.$(ARCH).o	   \
-$(SRC)/load_images.$(ARCH).o	   \
 $(SRC)/name_region.$(ARCH).o	   \
 $(SRC)/find_images.$(ARCH).o	   \
@@ -32,5 +36,4 @@
 $(SRC)/load_catalogs.$(ARCH).o	   \
 $(SRC)/gcatalog.$(ARCH).o 	   \
-$(SRC)/misc.$(ARCH).o		   \
 $(SRC)/join_stars.$(ARCH).o        \
 $(SRC)/unique_measures.$(ARCH).o   \
@@ -40,9 +43,10 @@
 $(SRC)/overlap_funcs.$(ARCH).o     \
 $(SRC)/image-db.$(ARCH).o          \
+$(SRC)/check_permissions.$(ARCH).o \
+$(SRC)/misc.$(ARCH).o		   \
 $(SRC)/SetSignals.$(ARCH).o        \
 $(SRC)/Shutdown.$(ARCH).o          \
-$(SRC)/check_permissions.$(ARCH).o \
-$(SRC)/subcatalog.$(ARCH).o        \
 $(SRC)/wcatalog.$(ARCH).o
+
 
 OBJ = $(SRCLIST)
Index: /trunk/Ohana/src/photdbc/include/photdbc.h
===================================================================
--- /trunk/Ohana/src/photdbc/include/photdbc.h	(revision 8629)
+++ /trunk/Ohana/src/photdbc/include/photdbc.h	(revision 8630)
@@ -53,5 +53,14 @@
 double SIGMA_MAX;
 int    NMEAS_MIN;
+double ZERO_POINT;
 
+int ExcludeByInstMag;
+double INST_MAG_MIN;
+double INST_MAG_MAX;
+
+int ExcludeByMaxMinMag;
+double MAX_MIN_MAG;
+
+SkyRegion REGION;
 PhotCodeData photcodes;
 
@@ -92,5 +101,5 @@
 void          unique_measures (Catalog *catalog);
 void          wcatalog (Catalog *catalog);
-Catalog      *subcatalog (Catalog *catalog);
+int           make_subcatalog (Catalog *subcatalog, Catalog *catalog);
 void          check_directory (char *basefile);
 
@@ -103,2 +112,5 @@
 void SetProtect (int mode);
 int SetSignals ();
+
+
+int copy_images (char *outdir);
Index: /trunk/Ohana/src/photdbc/src/ConfigInit.c
===================================================================
--- /trunk/Ohana/src/photdbc/src/ConfigInit.c	(revision 8629)
+++ /trunk/Ohana/src/photdbc/src/ConfigInit.c	(revision 8630)
@@ -12,5 +12,4 @@
 void ConfigInit (int *argc, char **argv) {
 
-  double ZERO_POINT, tmp;
   char *config, *file;
 
@@ -25,26 +24,21 @@
   if (VERBOSE) fprintf (stderr, "loaded config file: %s\n", file);
 
-  WarnConfig (config, "JOIN_RADIUS",            "%lf", 0, &JOIN_RADIUS);
-  WarnConfig (config, "UNIQ_RADIUS",            "%lf", 0, &UNIQ_RADIUS);
+  // WarnConfig (config, "JOIN_RADIUS",            "%lf", 0, &JOIN_RADIUS);
+  // WarnConfig (config, "UNIQ_RADIUS",            "%lf", 0, &UNIQ_RADIUS);
 
-  WarnConfig (config, "XMIN",                   "%lf", 0, &XMIN);
-  WarnConfig (config, "XMAX",                   "%lf", 0, &XMAX);
-  WarnConfig (config, "YMIN",                   "%lf", 0, &YMIN);
-  WarnConfig (config, "YMAX",                   "%lf", 0, &YMAX);
+  // WarnConfig (config, "XMIN",                   "%lf", 0, &XMIN);
+  // WarnConfig (config, "XMAX",                   "%lf", 0, &XMAX);
+  // WarnConfig (config, "YMIN",                   "%lf", 0, &YMIN);
+  // WarnConfig (config, "YMAX",                   "%lf", 0, &YMAX);
+  
+  // WarnConfig (config, "DMCAL_MIN",              "%lf", 0, &tmp);  DMCAL_MIN = 1000*tmp;
+  // WarnConfig (config, "MMIN",                   "%lf", 0, &tmp);  MMIN      = 1000*tmp;
+  // WarnConfig (config, "MMAX",                   "%lf", 0, &tmp);  MMAX      = 1000*tmp;
+  // WarnConfig (config, "DMSYS",                  "%lf", 0, &tmp);  DMSYS     = SQ(1000*tmp);
+  // WarnConfig (config, "DMGAIN",                 "%lf", 0, &DMGAIN); 
+  // WarnConfig (config, "CHISQ_MAX",              "%lf", 0, &CHISQ_MAX);
 
-  WarnConfig (config, "RMIN",                   "%lf", 0, &RMIN);
-  WarnConfig (config, "RMAX",                   "%lf", 0, &RMAX);
-  WarnConfig (config, "DMIN",                   "%lf", 0, &DMIN);
-  WarnConfig (config, "DMAX",                   "%lf", 0, &DMAX);
-
-  WarnConfig (config, "DMCAL_MIN",              "%lf", 0, &tmp);  DMCAL_MIN = 1000*tmp;
-  WarnConfig (config, "MMIN",                   "%lf", 0, &tmp);  MMIN      = 1000*tmp;
-  WarnConfig (config, "MMAX",                   "%lf", 0, &tmp);  MMAX      = 1000*tmp;
-  WarnConfig (config, "DMSYS",                  "%lf", 0, &tmp);  DMSYS     = SQ(1000*tmp);
-  WarnConfig (config, "DMGAIN",                 "%lf", 0, &DMGAIN); 
-  WarnConfig (config, "SIGMA_MAX",              "%lf", 0, &SIGMA_MAX); SIGMA_MAX *= 1000;
-
-  WarnConfig (config, "CHISQ_MAX",              "%lf", 0, &CHISQ_MAX);
-  WarnConfig (config, "NMEAS_MIN",              "%d",  0, &NMEAS_MIN);
+  ScanConfig (config, "SIGMA_MAX",              "%lf", 0, &SIGMA_MAX);
+  ScanConfig (config, "NMEAS_MIN",              "%d",  0, &NMEAS_MIN);
 
   WarnConfig (config, "GSCFILE",                "%s",  0, GSCFILE);
@@ -74,4 +68,3 @@
   free (config);
   free (file);
-
 }
Index: /trunk/Ohana/src/photdbc/src/Shutdown.c
===================================================================
--- /trunk/Ohana/src/photdbc/src/Shutdown.c	(revision 8629)
+++ /trunk/Ohana/src/photdbc/src/Shutdown.c	(revision 8630)
@@ -1,9 +1,3 @@
 # include "photdbc.h"
-
-static FITS_DB *db;
-
-void set_db (FITS_DB *in) {
-  db = in;
-}
 
 /* clean up open / locked ImageCat before shutting down */
@@ -21,7 +15,5 @@
   va_end (argp);
 
-  SetProtect (TRUE);
-  gfits_db_close (db);
-  fprintf (stderr, "ERROR: addstar halted\n");
+  fprintf (stderr, "ERROR: photdbc halted\n");
   exit (1);
 }
Index: /trunk/Ohana/src/photdbc/src/args.c
===================================================================
--- /trunk/Ohana/src/photdbc/src/args.c	(revision 8629)
+++ /trunk/Ohana/src/photdbc/src/args.c	(revision 8630)
@@ -17,9 +17,86 @@
   }
 
+  ExcludeByInstMag = FALSE;
+  if ((N = get_argument (argc, argv, "-instmag"))) {
+    remove_argument (N, &argc, argv);
+    ExcludeByInstMag = TRUE;
+    remove_argument (N, &argc, argv);
+    INST_MAG_MIN = atof(argv[N]);
+    remove_argument (N, &argc, argv);
+    INST_MAG_MAX = atof(argv[N]);
+  }
+
+  ExcludeByMaxMinMag = FALSE;
+  if ((N = get_argument (argc, argv, "-maxminmag"))) {
+    remove_argument (N, &argc, argv);
+    ExcludeByMaxMinMag = TRUE;
+    remove_argument (N, &argc, argv);
+    MAX_MIN_MAG = atof(argv[N]);
+  }
+
+  /* specify portion of the sky */
+  REGION.Rmin = 0;
+  REGION.Rmax = 360;
+  REGION.Dmin = -90;
+  REGION.Dmax = +90;
+  if ((N = get_argument (argc, argv, "-region"))) {
+    remove_argument (N, &argc, argv);
+    REGION.Rmin = atof (argv[N]);
+    remove_argument (N, &argc, argv);
+    REGION.Rmax = atof (argv[N]);
+    remove_argument (N, &argc, argv);
+    REGION.Dmin = atof (argv[N]);
+    remove_argument (N, &argc, argv);
+    REGION.Dmax = atof (argv[N]);
+    remove_argument (N, &argc, argv);
+
+    if (REGION.Rmin == REGION.Rmax) {
+      fprintf (stderr, "ERROR: selected region is ill-defined: Rmin == Rmax\n");
+      exit (2);
+    }
+    if (REGION.Dmin == REGION.Dmax) {
+      fprintf (stderr, "ERROR: selected region is ill-defined: Dmin == Dmax\n");
+      exit (2);
+    }
+  }
+
   if (argc != 2) {
-    fprintf (stderr, "USAGE: photdbc (region)\n");
+    fprintf (stderr, "USAGE: photdbc (output)\n");
     exit (2);
   } 
+
+  if ((REGION.Rmin == 0) && (REGION.Rmax == 360) && (REGION.Dmin == -90) && (REGION.Dmax == +90)) {
+    int i;
+    fprintf (stderr, "you have requested a copy of the entire sky in one pass\n");
+    fprintf (stderr, "this could be a time consuming operation.  type Ctrl-C within 5 seconds to cancel\n");
+    for (i = 5; i > 0; i--) {
+      fprintf (stderr, "%d.. ", i);
+      usleep (1000000);
+    }
+    fprintf (stderr, "\n");
+  }
+
   return (TRUE);
 }
 
+/**
+
+ this program takes an existing DVO database and makes a copy, applying a number of optional
+ filters in the process.  
+
+ photdbc (output)
+
+ allowed filters / restrictions include:
+
+ -region ra dec ra dec : limit operation to the specified region 
+ -join                 : join measurements between stars using JOIN_RADIUS
+ -ccdregion X Y X Y    : only keep detections within the specified detector region
+                         (can this be limited to specific photcodes? cameras? detectors?)
+ -photcode_limits code Mmin Mmax : allow multiples of these
+
+ SIGMA_MAX
+ NMEAS_MIN
+ INST_MAG_MAX
+ INST_MAG_MIN
+
+**/
Index: /trunk/Ohana/src/photdbc/src/copy_images.c
===================================================================
--- /trunk/Ohana/src/photdbc/src/copy_images.c	(revision 8630)
+++ /trunk/Ohana/src/photdbc/src/copy_images.c	(revision 8630)
@@ -0,0 +1,53 @@
+# include "photdbc.h"
+
+int copy_images (char *outdir) {
+
+  int status, Nimage;
+  char *ImageOut;
+  FITS_DB in;
+  FITS_DB out;
+  Image *image;
+  struct stat filestat;
+  char *path;
+
+  path = pathname (ImageCat);
+  status = stat (path, &filestat);
+  if (status == -1) Shutdown ("ERROR: CATDIR %s does not exist, no data in database", path);
+
+  status = dvo_image_lock (&in, ImageCat, 60.0, LCK_SOFT);
+  if (!status) {
+    fprintf (stderr, "No images in catalog %s\n", ImageCat);
+    return (TRUE);
+  }
+  if (in.dbstate == LCK_EMPTY) {
+    fprintf (stderr, "No images in catalog %s\n", ImageCat);
+    dvo_image_unlock (&in);
+    return (TRUE);
+  }
+
+  // load the input image data
+  if (!dvo_image_load (&in, VERBOSE, FALSE)) Shutdown ("can't read image catalog %s", in.filename);
+  dvo_image_unlock (&in);
+
+  // define output filename (replace CATDIR)
+  ImageOut = strsubs (in.filename, CATDIR, outdir);
+  if (ImageOut == NULL) Shutdown ("error with input or output catalog name");
+
+  // lock the output catalog
+  status = dvo_image_lock (&out, ImageOut, 60.0, LCK_SOFT);
+  if (!status) Shutdown ("ERROR: failure to lock image catalog %s", ImageOut);
+  if (out.dbstate != LCK_EMPTY) Shutdown ("ERROR: image table exists %s", ImageOut);
+
+  out.mode   = dvo_catalog_catmode (CATMODE);
+  out.format = dvo_catalog_catformat (CATFORMAT);
+  dvo_image_create (&out, ZERO_POINT);
+    
+  image = gfits_table_get_Image (&in.ftable, &Nimage, &in.swapped);
+  dvo_image_addrows (&out, image, Nimage);
+
+  dvo_image_save (&out, VERBOSE);
+  dvo_image_unlock (&out);
+  return (TRUE);
+}
+
+// globals: ImageCat, VERBOSE, CATMODE, CATFORMAT, ZeroPt
Index: unk/Ohana/src/photdbc/src/load_catalogs.c
===================================================================
--- /trunk/Ohana/src/photdbc/src/load_catalogs.c	(revision 8629)
+++ 	(revision )
@@ -1,33 +1,0 @@
-# include "photdbc.h"
-
-Catalog *load_catalogs (GSCRegion *region, int Nregion) {
-
-  int i;
-  Catalog *catalog;
-
-  if (VERBOSE) fprintf (stderr, "loading catalog data\n");
-
-  ALLOCATE (catalog, Catalog, Nregion);
-
-  /* load data from each region file */
-  for (i = 0; i < Nregion; i++) {
-    catalog[i].filename = region[i].filename;  /* don't free region before catalog! */
-    catalog[i].catmode  = dvo_catalog_catmode (CATMODE);      // set the default catmode from config data
-    catalog[i].filename = skylist[0].filename[i];
-    catalog[i].Nsecfilt = GetPhotcodeNsecfilt ();
-    catalog[i].catflags = LOAD_AVES | LOAD_MEAS | LOAD_MISS | LOAD_SECF;
-    if (options.update) catalog[i].catflags = LOAD_AVES | LOAD_MEAS_META | LOAD_MISS | LOAD_SECF;
-
-    // an error exit status here is a significant error
-    if (!dvo_catalog_open (&catalog[i], skylist[0].regions[i], VERBOSE, "w")) {
-      fprintf (stderr, "ERROR: failure to open/create catalog file %s\n", catalog.filename);
-      exit (2);
-    }
-    if (!catalog[i].Nave_disk) {
-      dvo_catalog_unlock (&catalog[i]);
-      dvo_catalog_free (&catalog[i]);
-      continue;
-    }
-  }
-  return (catalog);
-}
Index: unk/Ohana/src/photdbc/src/load_images.c
===================================================================
--- /trunk/Ohana/src/photdbc/src/load_images.c	(revision 8629)
+++ 	(revision )
@@ -1,23 +1,0 @@
-# include "photdbc.h"
-
-GSCRegion *load_images (FITS_DB *db, char *seed, int *nregion) {
-
-  int        Nregion;
-  GSCRegion *region;
-  int        Nimage;
-  int       *Line;
-  Image     *image;
-
-  region = name_region (seed, &Nregion);
-
-  /* read entire db table */
-  if (!dvo_image_load (db, VERBOSE, FALSE)) Shutdown ("can't read image catalog %s", db[0].filename);
-
-  /* find images which supplied all measurements */
-  image = find_images (db, region, Nregion, &Nimage, &Line);
-  *nregion = Nregion;
-  
-  gfits_vtable_from_ftable (&db[0].ftable, &db[0].vtable, Line, Nimage);
-
-  return (region);
-}
Index: /trunk/Ohana/src/photdbc/src/make_subcatalog.c
===================================================================
--- /trunk/Ohana/src/photdbc/src/make_subcatalog.c	(revision 8630)
+++ /trunk/Ohana/src/photdbc/src/make_subcatalog.c	(revision 8630)
@@ -0,0 +1,109 @@
+# include "photdbc.h"
+
+// copy a catalog to a new subcatalog, applying some filters
+// the supplied subcatalog must already be locked, opened, and created
+int make_subcatalog (Catalog *subcatalog, Catalog *catalog) {
+  
+  int i, j, offset;
+  int NAVERAGE, NMEASURE, Naverage, Nmeasure, Nm, Nsecfilt;
+  double mag, minMag;
+
+  Nsecfilt = GetPhotcodeNsecfilt ();
+
+  /* we are moving only the subset of measurements from catalog[0] to subcatalog[0] */
+  NAVERAGE = 50;
+  NMEASURE = 1000;
+  Nmeasure = Naverage = 0;
+  REALLOCATE (subcatalog[0].average, Average, NAVERAGE);
+  REALLOCATE (subcatalog[0].secfilt, SecFilt, NAVERAGE*Nsecfilt);
+  REALLOCATE (subcatalog[0].measure, Measure, NMEASURE);
+
+  for (i = 0; i < catalog[0].Naverage; i++) {
+    // perform exclusions based on average properties
+
+    // exclude stars with too few measurements
+    if (NMEAS_MIN && (catalog[0].average[i].Nm < NMEAS_MIN)) continue; 
+
+    /* assign average and secfilt values */
+    subcatalog[0].average[Naverage] = catalog[0].average[i];
+    subcatalog[0].average[Naverage].offset = Nmeasure;
+    for (j = 0; j < Nsecfilt; j++) {
+      subcatalog[0].secfilt[Nsecfilt*Naverage+j] = catalog[0].secfilt[Nsecfilt*i+j];
+    }
+
+    minMag = 32;
+    Nm = 0;
+    for (j = 0; j < catalog[0].average[i].Nm; j++) {
+
+      offset = catalog[0].average[i].offset + j;
+
+      # if 0
+      if (DropPhotcode) {
+	ecode = GetPhotcodeEquivCodebyCode (catalog[0].measure[offset].source);
+	if (ecode == photcode[0].code) continue;
+      }
+      if (DropNonStellar && catalog[0].measure[offset].dophot != 1) continue;
+      # endif
+
+      /* exclude measurements by measurement error */
+      if (SIGMA_MAX && (catalog[0].measure[offset].dM_PS > SIGMA_MAX)) continue;
+
+      /* select measurements by mag limit */
+      if (ExcludeByInstMag) {
+	mag = PhotInst (&catalog[0].measure[offset]);
+	if (mag < INST_MAG_MIN) continue;
+	if (mag > INST_MAG_MAX) continue;
+      }
+
+      if (ExcludeByMaxMinMag) {
+	mag = PhotSys (&catalog[0].measure[offset], &catalog[0].average[i], &catalog[0].secfilt[i*Nsecfilt]);
+	minMag = MIN (minMag, mag);
+      }
+
+      subcatalog[0].measure[Nmeasure]        = catalog[0].measure[offset];
+      subcatalog[0].measure[Nmeasure].averef = Naverage;
+      Nmeasure ++;
+      Nm ++;
+      if (Nmeasure == NMEASURE) {
+	NMEASURE += 1000;
+	REALLOCATE (subcatalog[0].measure, Measure, NMEASURE);
+      }
+    }
+
+    // exclude faint objects
+    if (ExcludeByMaxMinMag && (minMag > MAX_MIN_MAG)) {
+      Nmeasure -= Nm;
+      continue; 
+    }
+
+    // after measurement exclusion, exclude stars with too few measurements
+    if (Nm < NMEAS_MIN) {
+      Nmeasure -= Nm;
+      continue; 
+    }
+    subcatalog[0].average[Naverage].Nn = 0;
+    subcatalog[0].average[Naverage].Nm = Nm;
+    Naverage ++;
+    if (Naverage == NAVERAGE) {
+      NAVERAGE += 50;
+      REALLOCATE (subcatalog[0].average, Average, NAVERAGE);
+      REALLOCATE (subcatalog[0].secfilt, SecFilt, NAVERAGE*Nsecfilt);
+    }
+  }
+  REALLOCATE (subcatalog[0].average, Average, MAX (Naverage, 1));
+  REALLOCATE (subcatalog[0].measure, Measure, MAX (Nmeasure, 1));
+  REALLOCATE (subcatalog[0].secfilt, SecFilt, Nsecfilt*MAX (Naverage, 1));
+  subcatalog[0].Naverage = Naverage;
+  subcatalog[0].Nmeasure = Nmeasure;
+  subcatalog[0].Nsecfilt = catalog[0].Nsecfilt;
+
+  // XXX for now, don't copy the missing entries (these should be re-computed)
+  ALLOCATE (subcatalog[0].missing, Missing, 1);
+  subcatalog[0].Nmissing = 0;
+
+  if (VERBOSE) {
+    fprintf (stderr, "%d: using %d stars (%d measures) for catalog\n", i, 
+	     subcatalog[0].Naverage, subcatalog[0].Nmeasure);
+  }
+  return (TRUE);
+}
Index: /trunk/Ohana/src/photdbc/src/photdbc.c
===================================================================
--- /trunk/Ohana/src/photdbc/src/photdbc.c	(revision 8629)
+++ /trunk/Ohana/src/photdbc/src/photdbc.c	(revision 8630)
@@ -3,58 +3,76 @@
 int main (int argc, char **argv) {
 
-  int Nregion, status;
-  char filename[1024];
-  Catalog *catalog;
-  GSCRegion *region;
-  FITS_DB db;
+  int i;
+  char *skyfile;
+  Catalog incatalog;
+  Catalog outcatalog;
+  SkyTable *sky;
+  SkyList *skylist;
 
   /* get configuration info, args, lockfile */
   initialize (argc, argv);
 
-  set_db (&db);
-  status = dvo_image_lock (&db, ImageCat, 60.0, LCK_XCLD); /* XCLD */
-  if (!status) Shutdown ("ERROR: failure to lock image catalog %s", db.filename);
-  if (db.dbstate == LCK_EMPTY) Shutdown ("ERROR: No images in catalog %s (1)", db.filename);
+  // load and copy the image table
+  copy_images (argv[1]);
 
-  /* load region and images overlapping region */
-  region = load_images (&db, argv[1], &Nregion);
+  // the output catalog needs to inherit the SKY_DEPTH of the input catalog
+  sky = SkyTableLoadOptimal (CATDIR, NULL, GSCFILE, SKY_DEPTH_HST, VERBOSE);
+  SkyTableSetFilenames (sky, CATDIR, "cpt");
+  skylist = SkyListByPatch (sky, -1, &REGION);
+  for (i = 0; i < skylist[0].Nregions; i++) {
 
-  /* load catalog data corresponding to region file */
-  catalog = load_catalogs (region, Nregion);
+    // set the parameters which guide catalog open/load/create
+    incatalog.filename = skylist[0].filename[i];
+    incatalog.Nsecfilt = GetPhotcodeNsecfilt ();
+    incatalog.catflags = LOAD_AVES | LOAD_MEAS | LOAD_SECF;
 
-  /* limit number of measures based on selections */
-  catalog = subcatalog (catalog);
+    // an error exit status here is a significant error
+    if (!dvo_catalog_open (&incatalog, skylist[0].regions[i], VERBOSE, "r")) {
+      fprintf (stderr, "ERROR: failure to open/create catalog file %s\n", incatalog.filename);
+      exit (2);
+    }
+    // skip empty input catalogs
+    if (!incatalog.Nave_disk) {
+      dvo_catalog_unlock (&incatalog);
+      dvo_catalog_free (&incatalog);
+      continue;
+    }
 
-  /* limit number of measures based on selections */
-  join_stars (catalog);
+    // create output catalog filename
+    outcatalog.filename = strsubs (incatalog.filename, CATDIR, argv[1]);
+    if (outcatalog.filename == NULL) Shutdown ("error with input catalog name");
 
-  unique_measures (catalog);
+    // define outcatalog open parameters
+    outcatalog.catformat = dvo_catalog_catformat (CATFORMAT);  // set the default catformat from config data
+    outcatalog.catmode   = dvo_catalog_catmode (CATMODE);      // set the default catmode from config data
+    outcatalog.Nsecfilt  = incatalog.Nsecfilt;                 // inherit from the incatalog
+    outcatalog.catflags  = LOAD_AVES | LOAD_MEAS | LOAD_MISS | LOAD_SECF;
 
-  flag_measures (&db, catalog);
+    // output catalogs always represent the same skyregions as the input catalogs
+    if (!dvo_catalog_open (&outcatalog, skylist[0].regions[i], VERBOSE, "w")) {
+      fprintf (stderr, "ERROR: failure to open/create catalog file %s\n", outcatalog.filename);
+      exit (2);
+    }
 
-  get_mags (catalog);
+    /* limit number of measures based on selections */
+    make_subcatalog (&outcatalog, &incatalog);
+	
+    // XXX add other filters here:
+    // join_stars (catalog);
+    // unique_measures (catalog);
+    // flag_measures (&db, catalog);
+    // get_mags (catalog);
 
-  // XXX this is all messed up: are we working on more than one region or not??
-  for (i = 0; i < Nregion; i++) {
-    // XXX empty catalogs are already unlocked and freed 
-    dvo_catalog_unlock (&catalog[i]);
-    dvo_catalog_free (&catalog[i]);
+    dvo_catalog_save (&outcatalog, VERBOSE);
+    dvo_catalog_unlock (&outcatalog);
+    dvo_catalog_free (&outcatalog);
+
+    dvo_catalog_unlock (&incatalog);
+    dvo_catalog_free (&incatalog);
   }
 
-  // XXX?????
-  sprintf (filename, "newdir/%s", argv[1]);
-  catalog[0].filename = filename;
-
-  check_directory (catalog[0].filename);
-  if (!dvo_catalog_open (catalog, ...)) { 
-    fprintf (stderr, "can't lock new catalog file %s\n", catalog[0].filename);
-    exit (1);
-  }
-  dvo_catalog_save (catalog);
+  ALLOCATE (skyfile, char, strlen(argv[1]) + strlen("/SkyTable.fits") + 1);
+  sprintf (skyfile, "%s/SkyTable.fits", argv[1]);
+  SkyTableSave (sky, skyfile);
   exit (0);
 }
-
-/** this program is somewhat rough: minimal error checking
-    -
-    only one catalog is loaded, corresponding to region file
- */
Index: unk/Ohana/src/photdbc/src/subcatalog.c
===================================================================
--- /trunk/Ohana/src/photdbc/src/subcatalog.c	(revision 8629)
+++ 	(revision )
@@ -1,107 +1,0 @@
-# include "photdbc.h"
-
-Catalog *subcatalog (Catalog *catalog) {
-  
-  int i, j, offset;
-  int NAVERAGE, NMEASURE, Naverage, Nmeasure, Nm, Nsecfilt;
-  int mag;
-  Catalog *subcatalog;
-
-  ALLOCATE (subcatalog, Catalog, 1);
-
-  Nsecfilt = GetPhotcodeNsecfilt ();
-
-  subcatalog[0] = catalog[0];
-
-  /* we are moving only the subset of measurements from catalog[0] to subcatalog[0] */
-  NAVERAGE = 50;
-  NMEASURE = 1000;
-  ALLOCATE (subcatalog[0].average, Average, NAVERAGE);
-  ALLOCATE (subcatalog[0].secfilt, SecFilt, NAVERAGE*Nsecfilt);
-  ALLOCATE (subcatalog[0].measure, Measure, NMEASURE);
-  Nmeasure = Naverage = 0;
-
-  for (i = 0; i < catalog[0].Naverage; i++) {
-    /* exclude stars with too few measurements */
-    if (catalog[0].average[i].Nm < NMEAS_MIN) continue; 
-
-    /* exclude stars not in range */
-    if (catalog[0].average[i].R_PS < RMIN) continue;
-    if (catalog[0].average[i].R_PS > RMAX) continue;
-    if (catalog[0].average[i].D_PS < DMIN) continue;
-    if (catalog[0].average[i].D_PS > DMAX) continue;
-
-    /* assign average and secfilt values */
-    subcatalog[0].average[Naverage] = catalog[0].average[i];
-    subcatalog[0].average[Naverage].offset = Nmeasure;
-    for (j = 0; j < Nsecfilt; j++) {
-      subcatalog[0].secfilt[Nsecfilt*Naverage+j] = catalog[0].secfilt[Nsecfilt*i+j];
-    }
-
-    Nm = 0;
-    for (j = 0; j < catalog[0].average[i].Nm; j++) {
-
-      offset = catalog[0].average[i].offset + j;
-
-      /* exclude measurements by photcode 
-      if (DropPhotcode) {
-	ecode = GetPhotcodeEquivCodebyCode (catalog[0].measure[offset].source);
-	if (ecode == photcode[0].code) continue;
-      }*/
-
-      /* select measurements by quality 
-      if (DropNonStellar && catalog[0].measure[offset].dophot != 1) continue;
-      */
-
-      /* exclude measurements by measurement error */
-      if (catalog[0].measure[offset].dM > SIGMA_MAX) continue;
-
-      /* select measurements by mag limit */
-      mag = iPhotInst (&catalog[0].measure[offset]);
-      if (mag < MMIN) continue;
-      if (mag > MMAX) continue;
-
-      subcatalog[0].measure[Nmeasure]        = catalog[0].measure[offset];
-      subcatalog[0].measure[Nmeasure].averef = Naverage;
-      Nmeasure ++;
-      Nm ++;
-      if (Nmeasure == NMEASURE) {
-	NMEASURE += 1000;
-	REALLOCATE (subcatalog[0].measure, Measure, NMEASURE);
-      }
-    }
-    if (Nm < NMEAS_MIN) { /* enough measurements meet criteria? */
-      Nmeasure -= Nm;
-      continue; 
-    }
-    subcatalog[0].average[Naverage].Nn = 0;
-    subcatalog[0].average[Naverage].Nm = Nm;
-    Naverage ++;
-    if (Naverage == NAVERAGE) {
-      NAVERAGE += 50;
-      REALLOCATE (subcatalog[0].average, Average, NAVERAGE);
-      REALLOCATE (subcatalog[0].secfilt, SecFilt, NAVERAGE*Nsecfilt);
-    }
-  }
-  REALLOCATE (subcatalog[0].average, Average, MAX (Naverage, 1));
-  REALLOCATE (subcatalog[0].measure, Measure, MAX (Nmeasure, 1));
-  REALLOCATE (subcatalog[0].secfilt, SecFilt, Nsecfilt*MAX (Naverage, 1));
-  subcatalog[0].Naverage = Naverage;
-  subcatalog[0].Nmeasure = Nmeasure;
-  subcatalog[0].Nsecfilt = catalog[0].Nsecfilt;
-
-  ALLOCATE (subcatalog[0].missing, Missing, 1);
-  subcatalog[0].Nmissing = 0;
-
-  if (catalog[0].Naverage > 0) free (catalog[0].average);
-  if (catalog[0].Nmeasure > 0) free (catalog[0].measure);
-  if (catalog[0].Naverage > 0) free (catalog[0].secfilt);
-
-  if (VERBOSE) {
-    fprintf (stderr, "%d: using %d stars (%d measures) for catalog\n", i, 
-	     subcatalog[0].Naverage, subcatalog[0].Nmeasure);
-  }
-  
-  return (subcatalog);
-
-}
Index: /trunk/Ohana/src/photdbc/src/unique_measures.c
===================================================================
--- /trunk/Ohana/src/photdbc/src/unique_measures.c	(revision 8629)
+++ /trunk/Ohana/src/photdbc/src/unique_measures.c	(revision 8630)
@@ -102,2 +102,9 @@
 }
 
+/*** XXX 
+     this function does not quite do the right thing.  it should remove duplicate measurements 
+     for a single average object.  unique measurements have a unique combination of time and photcode
+     (IS this true?  only for DET, not for REF...)
+***/
+
+
