Index: /branches/eam_branches/ipp-20150625/Ohana/src/libdvo/include/dvo.h
===================================================================
--- /branches/eam_branches/ipp-20150625/Ohana/src/libdvo/include/dvo.h	(revision 38666)
+++ /branches/eam_branches/ipp-20150625/Ohana/src/libdvo/include/dvo.h	(revision 38667)
@@ -902,4 +902,6 @@
 float PhotZeroPoint (Measure *measure, Average *average, SecFilt *secfilt);
 
+int PhotSecfiltFlags (PhotCode *code, Average *average, SecFilt *secfilt);
+int PhotNcode (PhotCode *code, Average *average, SecFilt *secfilt);
 int   PhotNphot (PhotCode *code, Average *average, SecFilt *secfilt, dvoMagClassType class, dvoMagSourceType source);
 float PhotMstdev (PhotCode *code, Average *average, SecFilt *secfilt, dvoMagClassType class, dvoMagSourceType source);
Index: /branches/eam_branches/ipp-20150625/Ohana/src/libdvo/src/dbExtractMeasures.c
===================================================================
--- /branches/eam_branches/ipp-20150625/Ohana/src/libdvo/src/dbExtractMeasures.c	(revision 38666)
+++ /branches/eam_branches/ipp-20150625/Ohana/src/libdvo/src/dbExtractMeasures.c	(revision 38667)
@@ -241,12 +241,15 @@
 	  break;
 
-	case MAG_OPTION_STDEV:
-	case MAG_OPTION_CHISQ:
-	case MAG_OPTION_MIN:
-	case MAG_OPTION_MAX:
-	case MAG_OPTION_NCODE:
-	case MAG_OPTION_NPHOT:
-	case MAG_OPTION_UC_DIST:
-	case MAG_OPTION_FLAGS:
+	  // the following are all values which are come from the secfit table
+	case MAG_OPTION_STDEV:   { value.Flt = PhotMstdev (equiv, average, secfilt, field->magClass, field->magSource); break; }
+	case MAG_OPTION_CHISQ:   { value.Flt = PhotXm (equiv, average, secfilt); break; }
+	case MAG_OPTION_MIN:     { value.Flt = PhotMmin (equiv, average, secfilt); break;}
+	case MAG_OPTION_MAX:     { value.Flt = PhotMmax (equiv, average, secfilt); break;}
+	case MAG_OPTION_NCODE:   { value.Int = PhotNcode (equiv, average, secfilt); break; }
+	case MAG_OPTION_NPHOT:   { value.Int = PhotNphot (equiv, average, secfilt, field->magClass, field->magSource); break; }
+	case MAG_OPTION_UC_DIST: { value.Flt = PhotUCdist (equiv, average, secfilt); break; }
+	case MAG_OPTION_FLAGS:   { value.Int = PhotSecfiltFlags (equiv, average, secfilt); break; }
+	  
+	  // the following values come from the mean lensobj table
 	case MAG_OPTION_X11_SM_OBJ: 
 	case MAG_OPTION_X12_SM_OBJ: 
Index: /branches/eam_branches/ipp-20150625/Ohana/src/libdvo/src/dbFields.c
===================================================================
--- /branches/eam_branches/ipp-20150625/Ohana/src/libdvo/src/dbFields.c	(revision 38666)
+++ /branches/eam_branches/ipp-20150625/Ohana/src/libdvo/src/dbFields.c	(revision 38667)
@@ -316,4 +316,5 @@
     dvoMagSourceType source = GetMagSource (word);
     if (source != MAG_SRC_NONE) {
+      // if mySource is already set, then we have two 'source' elements (an error)
       if (mySource != MAG_SRC_NONE) { fprintf (stderr, "invalid selection %s in %s\n", word, fieldName); free (word); return FALSE; } 
       mySource = source;
@@ -324,4 +325,5 @@
     dvoMagLevelType level = GetMagLevel (word);
     if (level != MAG_LEVEL_NONE) {
+      // if myLevel is already set, then we have two 'level' elements (an error)
       if (myLevel != MAG_LEVEL_NONE) { fprintf (stderr, "invalid selection %s in %s\n", word, fieldName); free (word); return FALSE; } 
       myLevel = level;
@@ -332,4 +334,5 @@
     dvoMagClassType class = GetMagClass (word);
     if (class != MAG_CLASS_NONE) {
+      // if myClass is already set, then we have two 'class' elements (an error)
       if (myClass != MAG_CLASS_NONE) { fprintf (stderr, "invalid selection %s in %s\n", word, fieldName); free (word); return FALSE; } 
       myClass = class;
@@ -340,36 +343,22 @@
     dvoMagOptionType option = GetMagOption (word);
     if (option != MAG_OPTION_NONE) {
-      // some combinations are OK: mag + err -> err
-      if ((myOption == MAG_OPTION_MAG) && (option == MAG_OPTION_ERR)) {
+      // if myClass is already set, then we have two 'class' elements
+      // this is an error unless the request was of the form mag:value
+      // if so, then code->type will be PHOT_MAG
+
+      if (code->type == PHOT_MAG) {
+	// mag:err -> magerr
+	if ((myOption == MAG_OPTION_MAG) && (option == MAG_OPTION_ERR)) {
+	  myOption = MAG_OPTION_ERR;
+	  ptr = skipword (ptr);
+	  continue;
+	} 
+	// flux:err -> fluxerr
+	if ((myOption == MAG_OPTION_FLUX) && (option == MAG_OPTION_ERR)) {
+	  myOption = MAG_OPTION_FLUX_ERR;
+	  ptr = skipword (ptr);
+	  continue;
+	} 
 	myOption = option;
-	ptr = skipword (ptr);
-	continue;
-      }
-      // some combinations are OK: err + mag -> err
-      if ((myOption == MAG_OPTION_ERR) && (option == MAG_OPTION_MAG)) {
-	ptr = skipword (ptr);
-	continue;
-      }
-      // some combinations are OK: flux + err -> FLUX_ERR
-      if ((myOption == MAG_OPTION_FLUX) && (option == MAG_OPTION_ERR)) {
-	myOption = MAG_OPTION_FLUX_ERR;
-	ptr = skipword (ptr);
-	continue;
-      }
-      // some combinations are OK: err + flux -> FLUX_ERR
-      if ((myOption == MAG_OPTION_ERR) && (option == MAG_OPTION_FLUX)) {
-	myOption = MAG_OPTION_FLUX_ERR;
-	ptr = skipword (ptr);
-	continue;
-      }
-      // some combinations are OK: flux + fluxerr -> FLUX_ERR
-      if ((myOption == MAG_OPTION_FLUX) && (option == MAG_OPTION_FLUX_ERR)) {
-	myOption = MAG_OPTION_FLUX_ERR;
-	ptr = skipword (ptr);
-	continue;
-      }
-      // some combinations are OK: fluxerr + flux -> FLUX_ERR
-      if ((myOption == MAG_OPTION_FLUX_ERR) && (option == MAG_OPTION_FLUX)) {
-	myOption = MAG_OPTION_FLUX_ERR;
 	ptr = skipword (ptr);
 	continue;
Index: /branches/eam_branches/ipp-20150625/Ohana/src/libdvo/src/dvo_catalog_split.c
===================================================================
--- /branches/eam_branches/ipp-20150625/Ohana/src/libdvo/src/dvo_catalog_split.c	(revision 38666)
+++ /branches/eam_branches/ipp-20150625/Ohana/src/libdvo/src/dvo_catalog_split.c	(revision 38667)
@@ -100,4 +100,6 @@
     return FALSE;
   }
+
+  if (OHANA_MEMCHECK) ohana_memcheck_func (TRUE);
 
   // write PHU header
@@ -120,4 +122,6 @@
   gfits_free_matrix (&matrix);
 # endif
+
+  if (OHANA_MEMCHECK) ohana_memcheck_func (TRUE);
 
   FTable *outtable = ftable;
@@ -844,4 +848,6 @@
   }
 
+  if (OHANA_MEMCHECK) ohana_memcheck_func (TRUE);
+
   Nmeasure_disk_new = MAX (catalog[0].Nmeasure_disk, catalog[0].Nmeasure + catalog[0].Nmeasure_off);
   Nmissing_disk_new = MAX (catalog[0].Nmissing_disk, catalog[0].Nmissing + catalog[0].Nmissing_off);
@@ -867,4 +873,6 @@
   /* in split mode, we can save only part of the data */ 
 
+  if (OHANA_MEMCHECK) ohana_memcheck_func (TRUE);
+
   /*** Average Table ***/
   if ((catalog[0].catflags & DVO_LOAD_AVERAGE) && (catalog[0].average != NULL)) {
@@ -892,8 +900,12 @@
     }
 
+    if (OHANA_MEMCHECK) ohana_memcheck_func (TRUE);
+
     if (!dvo_catalog_save_subcat (catalog, &ftable, start, Nrows, catalog[0].Naverage_disk, Naverage_disk_new)) {
       fprintf (stderr, "failure writing Average table\n");
       goto failure;
     }
+
+    if (OHANA_MEMCHECK) ohana_memcheck_func (TRUE);
 
     gfits_free_header (&header);
@@ -924,4 +936,6 @@
     assert (catalog[0].Nmeasure_disk >= catalog[0].Nmeasure_off);
 
+    if (OHANA_MEMCHECK) ohana_memcheck_func (TRUE);
+
     // if we are going to compress, we need to receive unswapped data -- 
     int swapFromNative = !output_is_compressed (start, Nrows, Nmeasure_disk_new, catalog->catcompress);
@@ -932,4 +946,6 @@
       goto failure;
     }
+
+    if (OHANA_MEMCHECK) ohana_memcheck_func (TRUE);
 
     // write out Measure table
@@ -939,4 +955,6 @@
       goto failure;
     }
+    if (OHANA_MEMCHECK) ohana_memcheck_func (TRUE);
+
 
     gfits_free_header (&header);
@@ -988,4 +1006,6 @@
     int swapFromNative = !output_is_compressed (start, Nrows, Nsecfilt_disk_new, catalog->catcompress);
 
+    if (OHANA_MEMCHECK) ohana_memcheck_func (TRUE);
+
     // convert to external table format
     if (!SecFiltToFtable (&ftable, &secfilt[first], Nrows, catalog[0].catformat, swapFromNative)) {
@@ -993,4 +1013,6 @@
       goto failure;
     }
+
+    if (OHANA_MEMCHECK) ohana_memcheck_func (TRUE);
 
     // write out SecFilt table
Index: /branches/eam_branches/ipp-20150625/Ohana/src/libdvo/src/dvo_photcode_ops.c
===================================================================
--- /branches/eam_branches/ipp-20150625/Ohana/src/libdvo/src/dvo_photcode_ops.c	(revision 38666)
+++ /branches/eam_branches/ipp-20150625/Ohana/src/libdvo/src/dvo_photcode_ops.c	(revision 38667)
@@ -747,4 +747,26 @@
 }
 
+// return the number of detections in this filter (gpc1)
+int PhotNcode (PhotCode *code, Average *average, SecFilt *secfilt) {
+
+  if (code == NULL) return 0;
+
+  int Ns = photcodes[0].hashNsec[code[0].code];
+  if (Ns == -1) return 0;
+
+  return secfilt[Ns].Ncode;
+}
+
+// return the number of detections in this filter (gpc1)
+int PhotSecfiltFlags (PhotCode *code, Average *average, SecFilt *secfilt) {
+
+  if (code == NULL) return 0;
+
+  int Ns = photcodes[0].hashNsec[code[0].code];
+  if (Ns == -1) return 0;
+
+  return secfilt[Ns].flags;
+}
+
 int PhotNphot (PhotCode *code, Average *average, SecFilt *secfilt, dvoMagClassType class, dvoMagSourceType source) {
 
