Index: trunk/psastro/src/psastroLuminosityFunction.c
===================================================================
--- trunk/psastro/src/psastroLuminosityFunction.c	(revision 9731)
+++ trunk/psastro/src/psastroLuminosityFunction.c	(revision 10787)
@@ -68,4 +68,5 @@
   // exclude bins with no stars
   // exclude points after the peak with N < 0.8*peak
+  FILE *f = fopen ("psastro.dat", "w");
   int n = 0;
   for (int i = 0; i < nMags->n; i++) {
@@ -74,11 +75,13 @@
     lnMag->data.F32[n] = log10 (nMags->data.F32[i]);
     Mag->data.F32[n] = mMin + (i + 0.5)*dMag;
+    fprintf (f, "%d  %f %.0f %f\n", n, Mag->data.F32[n], nMags->data.F32[i], lnMag->data.F32[n]);
     n++;
   }
+  fclose (f);
   lnMag->n = n;
   Mag->n = n;
   psLogMsg ("psastro", 4, "fitting %d points to luminosity function\n", n);
 
-  psVector *mask = psVectorAlloc (nBin, PS_TYPE_MASK);
+  psVector *mask = psVectorAlloc (Mag->n, PS_TYPE_MASK);
   psVectorInit (mask, 0);
 
@@ -88,5 +91,8 @@
   stats->clipIter = 3;
 
-  line = psVectorClipFitPolynomial1D(line, stats, mask, 0xff, lnMag, NULL, Mag);
+  if (!psVectorClipFitPolynomial1D(line, stats, mask, 0xff, lnMag, NULL, Mag)) {
+      psError(PS_ERR_UNKNOWN, false, "Failed the fit the luminosity function\n");
+      return(NULL);
+  }
 
   // find min and max unmasked magnitudes
