Index: trunk/Ohana/src/opihi/dvo/photometry.c
===================================================================
--- trunk/Ohana/src/opihi/dvo/photometry.c	(revision 4606)
+++ trunk/Ohana/src/opihi/dvo/photometry.c	(revision 4679)
@@ -244,4 +244,5 @@
   if (!strcasecmp (parname, "Nphot")) param = AVE_NPHOT;
   if (!strcasecmp (parname, "Ncode")) param = AVE_NCODE;
+  if (!strcasecmp (parname, "Ncrit")) param = AVE_NCRIT;
   return (param);
 }
@@ -250,5 +251,5 @@
 int TestPhotSelections (PhotCode **code, int *mode, int param) {
 
-  int NeedPhotcode;
+  int NeedPhotcode, Needcode;
 
   /* if i've supplied a photcode (code != NULL), i'm not allowed to restrict it */
@@ -262,14 +263,14 @@
   }
 
-  /* if I have an average or ensemble restriction, I must have a photcode */
+  /* for measure tests, supply MEAS_ZERO */
+
+  /* if I have an average or ensemble restriction, I must have a PRI/SEC photcode */
   NeedPhotcode = FALSE;
   NeedPhotcode |= ChiSelect;
   NeedPhotcode |= NphotSelect;
-  NeedPhotcode |= NcodeSelect;
   NeedPhotcode |= ErrSelect;
   NeedPhotcode |= TypeSelect;
   NeedPhotcode |= TypefracSelect;
   
-  /* for measure tests, supply MEAS_ZERO */
   NeedPhotcode |= (param == AVE_Xm);
   NeedPhotcode |= (param == AVE_MAG);
@@ -277,12 +278,19 @@
   NeedPhotcode |= (param == AVE_TYPE);
   NeedPhotcode |= (param == AVE_NPHOT);
-  NeedPhotcode |= (param == AVE_NCODE);
-
-  if (NeedPhotcode || PhotcodeSelect) {
-    if (!PhotcodeSelect) return (FALSE);
+  Needcode = (param == AVE_NCODE);
+
+  if (NeedPhotcode || Needcode || NcodeSelect || PhotcodeSelect) {
+    if (!PhotcodeSelect) {
+      fprintf (stderr, "photcode selection problem: value requires photcode\n");
+      return (FALSE);
+    }
     code[0] = PhotcodeValue;
     mode[0] = PhotcodeMode;
-    if (code[0][0].type == PHOT_DEP) return (FALSE);
-    if (code[0][0].type == PHOT_REF) return (FALSE);
+  }
+  if (NeedPhotcode) {
+    if (code[0][0].type == PHOT_PRI) return (TRUE);
+    if (code[0][0].type == PHOT_SEC) return (TRUE);
+    fprintf (stderr, "photcode selection problem: average value requires PRI/SEC photcode\n");
+    return (FALSE);
   }
   return (TRUE);
@@ -615,5 +623,5 @@
       value = 0;
       for (i = 0; i < average[0].Nm; i++) {
-	if (code[0].code != GetPhotcodeEquivCodebyCode (measure[i].source)) continue;
+	if ((code != NULL) && (code[0].code != GetPhotcodeEquivCodebyCode (measure[i].source))) continue;
 	if (ErrSelect && (measure[i].dM > ErrValue)) continue;
 	if (FlagSelect && (measure[i].flags != FlagValue)) continue;
@@ -738,21 +746,4 @@
   if (code == NULL) return (FALSE);
 
-  /* only PRI/SEC photcodes apply the filter */
-  if (code[0].type == PHOT_DEP) return (TRUE);
-  if (code[0].type == PHOT_REF) return (TRUE);
-
-  /* exclusions based on average.params  */
-  if (ChiSelect) {
-    Xm = PhotXm (code, average, secfilt);
-    if (Xm == -1) return (FALSE);
-    if (Xm > ChiLimit) return (FALSE);
-  }
-  
-  /* for ErrSelect, check average errors */
-  if (ErrSelect) {
-    dM = iPhotdM (code, average, secfilt);
-    if (dM > ErrValue) return (NO_MAG);
-  }
-  
   /* for NcodeSelect, count Nmeas for appropriate photcode */
   if (NcodeSelect) {
@@ -777,4 +768,21 @@
   }
 
+  /* only PRI/SEC photcodes apply the filter */
+  if (code[0].type == PHOT_DEP) return (TRUE);
+  if (code[0].type == PHOT_REF) return (TRUE);
+
+  /* exclusions based on average.params  */
+  if (ChiSelect) {
+    Xm = PhotXm (code, average, secfilt);
+    if (Xm == -1) return (FALSE);
+    if (Xm > ChiLimit) return (FALSE);
+  }
+  
+  /* for ErrSelect, check average errors */
+  if (ErrSelect) {
+    dM = iPhotdM (code, average, secfilt);
+    if (dM > ErrValue) return (NO_MAG);
+  }
+  
   /* for NphotSelect, count Nmeas for appropriate photcode */
   if (NphotSelect) {
