Index: /trunk/Ohana/src/addstar/src/calibrate.c
===================================================================
--- /trunk/Ohana/src/addstar/src/calibrate.c	(revision 13140)
+++ /trunk/Ohana/src/addstar/src/calibrate.c	(revision 13141)
@@ -42,9 +42,9 @@
 
 /* use the linked list to navigate the measures; safe for unsorted measures */
-void AddToCalibration (Average *average, Measure *measure, Measure *new, int *next, int Nstar) {
-
-  int i, j, m, found0, found1, found2;
+void AddToCalibration (Average *average, SecFilt *secfilt, Measure *measure, Measure *new, int *next, int Nstar) {
+
+  int i, j, m, Nsec, found0, found1, found2;
   float CalM0, CalM1, CalM2, dCalM;
-  float Mcal, color, factor;
+  float Mcal, color, factor, Minst;
   short CalC0, CalC1, CalC2;
 
@@ -66,9 +66,34 @@
   if (InternalCal) {
     CalC0 = incode[0].code;
+    Nsec  = GetPhotcodeNsec (CalC0);
   } else {
     CalC0 = excode[0].code;
-  }
+    Nsec  = GetPhotcodeNsec (CalC0);
+  }
+  /* check if this reference code is an average magnitude */
+  if (Nsec != -1) {
+    CalM0 = secfilt[Nsec].M;
+    dCalM = secfilt[Nsec].dM;
+    found0 = TRUE;
+  }
+
   CalC1 = mycode[0].c1;
+  Nsec  = GetPhotcodeNsec (CalC1);
+  if (Nsec != -1) {
+    CalM1 = secfilt[Nsec].M;
+    found1 = TRUE;
+  }
+
   CalC2 = mycode[0].c2;
+  Nsec  = GetPhotcodeNsec (CalC2);
+  if (Nsec != -1) {
+    CalM2 = secfilt[Nsec].M;
+    found2 = TRUE;
+  }
+
+  if (!CalC1 && !CalC2) {
+    found1 = found2 = TRUE;
+    CalM1 = CalM2 = 0.0;
+  }
 
   m = average[0].offset;
@@ -99,5 +124,9 @@
       }
       // if we want to apply a Mcal -> Mref color correction, we need the additional color term
-      SaveCalibration (Mcal, new[0].dM, CalM0, dCalM, new[0].M - ZeroPt - new[0].dt, Nstar);
+      Minst = new[0].M - ZeroPt - new[0].dt;
+      SaveCalibration (Mcal, new[0].dM, CalM0, dCalM, Minst, Nstar);
+      if ((DUMP != NULL) && !strcmp (DUMP, "cal")) {
+	fprintf (stdout, "cal-match : %10.6f %10.6f : %7.4f %6.4f  %7.4f %6.4f   %7.4f : %7.4f %7.4f\n", average[0].R, average[0].D, Mcal, new[0].dM, CalM0, dCalM, Minst, airmass, color);
+      }
       return;
     }
@@ -186,5 +215,11 @@
     M2 = sqrt (fabs(M2/N - M1*M1));
     Mw = W1 / W2;
-    fprintf (stderr, "N: %.0f, mean: %f, wt mean: %f, stdev: %f, precision: %f\n", N, M1, Mw, M2, M2 / sqrt (N));
+    fprintf (stdout, "STATUS: SUCCESS\n");
+    fprintf (stdout, "ZERO_POINT_MEAN      = %7.4f\n", M1);
+    fprintf (stdout, "ZERO_POINT_WTMEAN    = %7.4f\n", Mw);
+    fprintf (stdout, "ZERO_POINT_STDEV     = %7.4f\n", M2);
+    fprintf (stdout, "ZERO_POINT_PRECISION = %7.4f\n", M2 / sqrt (N));
+    fprintf (stdout, "ZERO_POINT_NSTARS    =    %4.0f\n", N);
+    // fprintf (stderr, "N: %.0f, mean: %f, wt mean: %f, stdev: %f, precision: %f\n", N, M1, Mw, M2, M2 / sqrt (N));
     image[0].Mcal = M1;
     image[0].dMcal = M2 / sqrt (N);
