Index: /branches/eam_branches/ipp-20101205/Ohana/src/photdbc/include/photdbc.h
===================================================================
--- /branches/eam_branches/ipp-20101205/Ohana/src/photdbc/include/photdbc.h	(revision 30440)
+++ /branches/eam_branches/ipp-20101205/Ohana/src/photdbc/include/photdbc.h	(revision 30441)
@@ -51,8 +51,13 @@
 double DMGAIN;
 double CHISQ_MAX;
+double SIGMA_MIN_KEEP;
 double SIGMA_MAX;
 double AVE_SIGMA_LIM;
 int    NMEAS_MIN;
+int    NMEAS_MIN_FILTERED;
+int    NCODE_MIN;
 double ZERO_POINT;
+
+int ExcludeByMinSigma;
 
 int ExcludeByInstMag;
@@ -65,4 +70,8 @@
 SkyRegion REGION;
 PhotCodeData photcodes;
+
+char          *PHOTCODE_DROP_LIST, *PHOTCODE_SKIP_LIST;
+int           NphotcodesDrop,      NphotcodesSkip;
+PhotCode     **photcodesDrop,     **photcodesSkip;
 
 # define FLAG_AREA            0X0001
Index: /branches/eam_branches/ipp-20101205/Ohana/src/photdbc/src/ConfigInit.c
===================================================================
--- /branches/eam_branches/ipp-20101205/Ohana/src/photdbc/src/ConfigInit.c	(revision 30440)
+++ /branches/eam_branches/ipp-20101205/Ohana/src/photdbc/src/ConfigInit.c	(revision 30441)
@@ -26,7 +26,11 @@
   if (VERBOSE) fprintf (stderr, "loaded config file: %s\n", file);
 
-  WarnConfig (config, "PHOTDBC_JOIN_RADIUS",       "%lf", 0, &JOIN_RADIUS);
+  // XXX join_stars (in photdbc.c) is currently disabled
+  // WarnConfig (config, "PHOTDBC_JOIN_RADIUS",       "%lf", 0, &JOIN_RADIUS);
+
+  // XXX unique_measures (in photdbc.c) is currently disabled
   // WarnConfig (config, "UNIQ_RADIUS",            "%lf", 0, &UNIQ_RADIUS);
 
+  // XXX flag_measures (in photdbc.c) is currently disabled
   // WarnConfig (config, "XMIN",                   "%lf", 0, &XMIN);
   // WarnConfig (config, "XMAX",                   "%lf", 0, &XMAX);
@@ -37,11 +41,14 @@
   // WarnConfig (config, "MMIN",                   "%lf", 0, &tmp);  MMIN      = 1000*tmp;
   // WarnConfig (config, "MMAX",                   "%lf", 0, &tmp);  MMAX      = 1000*tmp;
+  // WarnConfig (config, "CHISQ_MAX",              "%lf", 0, &CHISQ_MAX);
+
+  // XXX get_mags (in photdbc.c) is currently disabled
   // WarnConfig (config, "DMSYS",                  "%lf", 0, &tmp);  DMSYS     = SQ(1000*tmp);
   // WarnConfig (config, "DMGAIN",                 "%lf", 0, &DMGAIN); 
-  // WarnConfig (config, "CHISQ_MAX",              "%lf", 0, &CHISQ_MAX);
 
   ScanConfig (config, "SIGMA_MAX",              "%lf", 0, &SIGMA_MAX);
   ScanConfig (config, "AVE_SIGMA_LIM",          "%lf", 0, &AVE_SIGMA_LIM);
   ScanConfig (config, "NMEAS_MIN",              "%d",  0, &NMEAS_MIN);
+  ScanConfig (config, "NMEAS_MIN_FILTERED",     "%d",  0, &NMEAS_MIN_FILTERED);
 
   WarnConfig (config, "GSCFILE",                "%s",  0, GSCFILE);
Index: /branches/eam_branches/ipp-20101205/Ohana/src/photdbc/src/args.c
===================================================================
--- /branches/eam_branches/ipp-20101205/Ohana/src/photdbc/src/args.c	(revision 30440)
+++ /branches/eam_branches/ipp-20101205/Ohana/src/photdbc/src/args.c	(revision 30441)
@@ -19,5 +19,4 @@
   ExcludeByInstMag = FALSE;
   if ((N = get_argument (argc, argv, "-instmag"))) {
-    remove_argument (N, &argc, argv);
     ExcludeByInstMag = TRUE;
     remove_argument (N, &argc, argv);
@@ -25,12 +24,21 @@
     remove_argument (N, &argc, argv);
     INST_MAG_MAX = atof(argv[N]);
+    remove_argument (N, &argc, argv);
+  }
+
+  ExcludeByMinSigma = FALSE;
+  if ((N = get_argument (argc, argv, "-min-sigma"))) {
+    ExcludeByMinSigma = TRUE;
+    remove_argument (N, &argc, argv);
+    SIGMA_MIN_KEEP = atof(argv[N]);
+    remove_argument (N, &argc, argv);
   }
 
   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]);
+    remove_argument (N, &argc, argv);
   }
 
@@ -61,4 +69,18 @@
   }
 
+  PHOTCODE_DROP_LIST = NULL;
+  if ((N = get_argument (argc, argv, "-photcode-drop"))) {
+    remove_argument (N, &argc, argv);
+    PHOTCODE_DROP_LIST = strcreate(argv[N]);
+    remove_argument (N, &argc, argv);
+  }
+
+  PHOTCODE_SKIP_LIST = NULL;
+  if ((N = get_argument (argc, argv, "-photcode-skip"))) {
+    remove_argument (N, &argc, argv);
+    PHOTCODE_SKIP_LIST = strcreate(argv[N]);
+    remove_argument (N, &argc, argv);
+  }
+
   if (argc != 2) usage();
 
@@ -81,20 +103,35 @@
   fprintf (stderr, "USAGE: photdbc (output)\n\n");
   fprintf (stderr, " this program takes an existing DVO database and makes a copy, applying a number of optional\n");
-  fprintf (stderr, " filters in the process.  \n");
-  fprintf (stderr, "\n");
-  fprintf (stderr, " photdbc (output)\n");
-  fprintf (stderr, "\n");
-  fprintf (stderr, " allowed filters / restrictions include:\n");
-  fprintf (stderr, "\n");
+  fprintf (stderr, " filters in the process.  \n\n");
+
+  fprintf (stderr, " photdbc (output)\n\n");
+
+  fprintf (stderr, " allowed filters / restrictions include:\n\n");
+
   fprintf (stderr, " -region Rmin Rmax Dmin Dmax : limit operation to the specified region \n");
-  fprintf (stderr, " -join                 : join measurements between stars using JOIN_RADIUS\n");
-  fprintf (stderr, " -ccdregion X Y X Y    : only keep detections within the specified detector region\n");
-  fprintf (stderr, "                         (can this be limited to specific photcodes? cameras? detectors?)\n");
-  fprintf (stderr, " -photcode_limits code Mmin Mmax : allow multiples of these\n");
-  fprintf (stderr, "\n");
-  fprintf (stderr, " SIGMA_MAX\n");
-  fprintf (stderr, " NMEAS_MIN\n");
-  fprintf (stderr, " INST_MAG_MAX\n");
-  fprintf (stderr, " INST_MAG_MIN\n");
+  
+  fprintf (stderr, " -photcode-drop   : remove these photcodes from the output (REF or DEP only)\n");
+  fprintf (stderr, " -photcode-skip  : ignore these photcodes when assessing the validity (keep unless object is dropped)\n");
+
+  fprintf (stderr, " -instmag (min) (max) : range of valid instrumental magnitudes (or measurements are dropped)\n");
+  fprintf (stderr, " -min-sigma (sigma)   : object must have one measurement error less than sigma or object is dropped\n");
+
+  fprintf (stderr, " -maxminmag (mag)   : object must have one magnitude less than this or object is dropped\n");
+
+  fprintf (stderr, " option options:\n");
+  fprintf (stderr, " -v : verbose mode\n");
+  fprintf (stderr, " -params : list the current parameters\n\n");
+
+  fprintf (stderr, "ptolemy.rc config values used by this program:\n");
+  fprintf (stderr, " SIGMA_MAX : drop measurements with errors greater than this\n");
+  fprintf (stderr, " NMEAS_MIN : drop objects with fewer measurements than this\n");
+  fprintf (stderr, " NMEAS_MIN_FILTERED : drop objects with fewer measurements than this after filtering above\n");
+  fprintf (stderr, " AVE_SIGMA_LIM : drop objects if all average mags are greater than this\n");
+
   exit (2);
 }
+
+// fprintf (stderr, " -join                 : join measurements between stars using JOIN_RADIUS\n");
+// fprintf (stderr, " -ccdregion X Y X Y    : only keep detections within the specified detector region\n");
+// fprintf (stderr, "                         (can this be limited to specific photcodes? cameras? detectors?)\n");
+// fprintf (stderr, " -photcode_limits code Mmin Mmax : allow multiples of these\n");
Index: /branches/eam_branches/ipp-20101205/Ohana/src/photdbc/src/initialize.c
===================================================================
--- /branches/eam_branches/ipp-20101205/Ohana/src/photdbc/src/initialize.c	(revision 30440)
+++ /branches/eam_branches/ipp-20101205/Ohana/src/photdbc/src/initialize.c	(revision 30441)
@@ -2,4 +2,7 @@
 
 void initialize (int argc, char **argv) {
+
+  int NPHOTCODES;
+  char *codename, *ptr, *list;
 
   /* are these set correctly? */
@@ -11,4 +14,46 @@
   ConfigInit (&argc, argv);
   args (argc, argv);
+
+  NphotcodesDrop = 0;
+  photcodesDrop = NULL;
+  if (PHOTCODE_DROP_LIST != NULL) {
+    NPHOTCODES = 10;
+    ALLOCATE (photcodesDrop, PhotCode *, NPHOTCODES);
+
+    /* parse the comma-separated list of photcodesDrop */
+    list = PHOTCODE_DROP_LIST;
+    while ((codename = strtok_r (list, ",", &ptr)) != NULL) {
+      list = NULL; // pass NULL on successive strtok_r calls
+      fprintf (stderr, "PHOTCODE_LIST: %s\n", PHOTCODE_DROP_LIST);
+      fprintf (stderr, "codename: %s\n", codename);
+      if ((photcodesDrop[NphotcodesDrop] = GetPhotcodebyName (codename)) == NULL) {
+        fprintf (stderr, "ERROR: photcode %s not found in photcode table\n", codename);
+        exit (1);
+      }
+      NphotcodesDrop ++;
+      CHECK_REALLOCATE (photcodesDrop, PhotCode *, NPHOTCODES, NphotcodesDrop, 10);
+    }
+  }
+
+  NphotcodesSkip = 0;
+  photcodesSkip = NULL;
+  if (PHOTCODE_SKIP_LIST != NULL) {
+    NPHOTCODES = 10;
+    ALLOCATE (photcodesSkip, PhotCode *, NPHOTCODES);
+
+    /* parse the comma-separated list of photcodesSkip */
+    list = PHOTCODE_SKIP_LIST;
+    while ((codename = strtok_r (list, ",", &ptr)) != NULL) {
+      list = NULL; // pass NULL on successive strtok_r calls
+      fprintf (stderr, "PHOTCODE_LIST: %s\n", PHOTCODE_SKIP_LIST);
+      fprintf (stderr, "codename: %s\n", codename);
+      if ((photcodesSkip[NphotcodesSkip] = GetPhotcodebyName (codename)) == NULL) {
+        fprintf (stderr, "ERROR: photcode %s not found in photcode table\n", codename);
+        exit (1);
+      }
+      NphotcodesSkip ++;
+      CHECK_REALLOCATE (photcodesSkip, PhotCode *, NPHOTCODES, NphotcodesSkip, 10);
+    }
+  }
 
   if (SHOW_PARAMS) {
Index: /branches/eam_branches/ipp-20101205/Ohana/src/photdbc/src/make_subcatalog.c
===================================================================
--- /branches/eam_branches/ipp-20101205/Ohana/src/photdbc/src/make_subcatalog.c	(revision 30440)
+++ /branches/eam_branches/ipp-20101205/Ohana/src/photdbc/src/make_subcatalog.c	(revision 30441)
@@ -5,11 +5,24 @@
 int make_subcatalog (Catalog *subcatalog, Catalog *catalog) {
   
-  off_t i, j, offset;
+  int found;
+  off_t i, j, k, offset;
   off_t NAVERAGE, NMEASURE, Naverage, Nmeasure, Nm, Nsecfilt;
-  double mag, minMag;
-  int keep;
+  double mag, minMag, minSigma;
+  int keep, *secSkip;
+  PhotCode *photcode;
   
   Nsecfilt = GetPhotcodeNsecfilt ();
   assert (catalog[0].Nsecfilt == Nsecfilt);
+
+  // set up a list of SEC entries to ignore when evaluating a source
+  ALLOCATE (secSkip, int, Nsecfilt);
+  for (i = 0; i < Nsecfilt; i++) {
+      secSkip[i] = FALSE;
+      photcode = GetPhotcodebyNsec(i);
+      for (k = 0; k < NphotcodesSkip; k++) {
+	  if (photcodesSkip[k][0].code != photcode[0].code) continue; 
+	  secSkip[i] = TRUE;
+      }
+  }
 
   /* we are moving only the subset of measurements from catalog[0] to subcatalog[0] */
@@ -38,4 +51,17 @@
     }
 
+    // exclude stars with too few measurements
+    if (NCODE_MIN) {
+      // drop if all of the allowed average photcodes have ncode < NCODE_MIN values
+      keep = FALSE;
+      for (j = 0; !keep && (j < Nsecfilt); j++) {
+	  if (secSkip[j]) continue;
+	  if (catalog[0].secfilt[Nsecfilt*i+j].Ncode >= NCODE_MIN) {
+	      keep = TRUE;
+	}
+      }
+      if (!keep) continue;
+    }
+
     /* assign average and secfilt values */
     subcatalog[0].average[Naverage] = catalog[0].average[i];
@@ -45,5 +71,6 @@
     }
 
-    minMag = 32;
+    minMag   = 32;
+    minSigma = 32;
     Nm = 0;
     for (j = 0; j < catalog[0].average[i].Nmeasure; j++) {
@@ -52,15 +79,31 @@
 
       # if 0
-      if (DropPhotcode) {
-	ecode = GetPhotcodeEquivCodebyCode (catalog[0].measure[offset].photcode);
-	if (ecode == photcode[0].code) continue;
-      }
       if (DropNonStellar && catalog[0].measure[offset].dophot != 1) continue;
       # endif
 
-      /* exclude measurements by measurement error */
+      // remove certain photcodes from the output measurements
+      if (NphotcodesDrop > 0) {
+	  found = FALSE;
+	  for (k = 0; (k < NphotcodesSkip) && !found; k++) {
+	      if (photcodesSkip[k][0].code == catalog[0].measure[offset].photcode) found = TRUE;
+	      if (photcodesSkip[k][0].code == GetPhotcodeEquivCodebyCode(catalog[0].measure[offset].photcode)) found = TRUE;
+	  }
+	  if (found) continue;
+      }  
+  
+      // ignore certain photcodes to assess the measurements
+      if (NphotcodesSkip > 0) {
+	  found = FALSE;
+	  for (k = 0; (k < NphotcodesSkip) && !found; k++) {
+	      if (photcodesSkip[k][0].code == catalog[0].measure[offset].photcode) found = TRUE;
+	      if (photcodesSkip[k][0].code == GetPhotcodeEquivCodebyCode(catalog[0].measure[offset].photcode)) found = TRUE;
+	  }
+	  if (found) goto keep;
+      }  
+  
+      // exclude measurements by measurement error -- drop exactly this measurement 
       if (SIGMA_MAX && (catalog[0].measure[offset].dM > SIGMA_MAX)) continue;
 
-      /* select measurements by mag limit */
+      // select measurements by mag limit -- drop exactly this measurement  
       if (ExcludeByInstMag) {
 	mag = PhotInst (&catalog[0].measure[offset]);
@@ -69,8 +112,16 @@
       }
 
+      // check measurements for this object -- drop object if no measurements pass
+      if (ExcludeByMinSigma) {
+	  minSigma = MIN (minSigma, catalog[0].measure[offset].dM);
+      }
+
+      // check measurements for this object -- drop object if no measurements pass
       if (ExcludeByMaxMinMag) {
 	mag = PhotSys (&catalog[0].measure[offset], &catalog[0].average[i], &catalog[0].secfilt[i*Nsecfilt]);
 	minMag = MIN (minMag, mag);
       }
+
+    keep:
 
       subcatalog[0].measure[Nmeasure]        = catalog[0].measure[offset];
@@ -90,9 +141,16 @@
     }
 
-    // after measurement exclusion, exclude stars with too few measurements
-    if (Nm < NMEAS_MIN) {
+    // exclude faint objects
+    if (ExcludeByMinSigma && (minSigma > SIGMA_MIN_KEEP)) {
       Nmeasure -= Nm;
       continue; 
     }
+
+    // after measurement exclusion, exclude stars with too few measurements
+    if (NMEAS_MIN_FILTERED && (Nm < NMEAS_MIN_FILTERED)) {
+      Nmeasure -= Nm;
+      continue; 
+    }
+
     subcatalog[0].average[Naverage].Nmissing = 0;
     subcatalog[0].average[Naverage].Nmeasure = Nm;
@@ -122,2 +180,24 @@
   return (TRUE);
 }
+
+/** 
+    the purpose of this function is to create a subset database.  there are several ways this could be done:
+
+    * reject all measurements from all objects that fail to meet some criteria (objects may lose measurements and/or be dropped)
+
+    * only reject an object if all measurements fail to meet some criteria (ie, keep all measurements if any measurement passes)
+
+    * only apply the keep / reject criteria to certain photcodes
+
+
+    ***
+
+    * options:
+
+    minimum value for nmeas : NMEAS_MIN
+    minimum value for nmeas : NMEAS_MIN_FILTERED
+    minimum value for ncode : NCODE_MIN (drop if all ncode < NCODE_MIN) (respect photcodes)
+    reject faint meas       : SIGMA_MAX
+    reject faint source     : SIGMA_MIN_KEEP (keep source source if its minimum dMag < this limit)
+
+    **/
