Index: trunk/psastro/src/psastroRefstarSubset.c
===================================================================
--- trunk/psastro/src/psastroRefstarSubset.c	(revision 9587)
+++ trunk/psastro/src/psastroRefstarSubset.c	(revision 9627)
@@ -5,9 +5,15 @@
   // select the raw objects for this readout
   psArray *rawstars = psMetadataLookupPtr (NULL, readout->analysis, "PSASTRO.RAWSTARS");
-  if (rawstars == NULL) { continue; }
+  if (rawstars == NULL)  {
+    psError(PSASTRO_ERR_DATA, false, "missing rawstars in psastroRefstarSubset\n");
+    return false;
+  }
 
   // select the raw objects for this readout
   psArray *refstars = psMetadataLookupPtr (NULL, readout->analysis, "PSASTRO.REFSTARS");
-  if (refstars == NULL) { continue; }
+  if (refstars == NULL)  {
+    psError(PSASTRO_ERR_DATA, false, "missing refstars in psastroRefstarSubset\n");
+    return false;
+  }
 
   // calculate luminosity functions for rawstars and refstars
@@ -15,14 +21,27 @@
   // is needed...
   pmLumFunc *rawfunc = psastroLuminosityFunction (rawstars);
+  if (rawfunc == NULL) {
+    psError(PSASTRO_ERR_DATA, false, "error measuring rawstar luminosity function\n");
+    return NULL;
+  }
   pmLumFunc *reffunc = psastroLuminosityFunction (refstars);
+  if (reffunc == NULL) {
+    psError(PSASTRO_ERR_DATA, false, "error measuring refstar luminosity function\n");
+    return NULL;
+  }
+  
+  // XXXX test
+  // psFree (rawfunc);
+  // psFree (reffunc);
+  // return true;
 
   // what is the offset between the two lines at the average magnitude? 
-  mRef = 0.5*(reffunc->mMin + reffunc->mMax);
-  logRho = mRef * reffunc->slope + reffunc->offset;
-  mRaw = (logRho - rawfunc->offset) / rawfunc->slope;
+  double mRef = 0.5*(reffunc->mMin + reffunc->mMax);
+  double logRho = mRef * reffunc->slope + reffunc->offset;
+  double mRaw = (logRho - rawfunc->offset) / rawfunc->slope;
 
   psLogMsg ("psastro", 4, "mRef: %f, logRho: %f, mRaw: %f\n", mRef, logRho, mRaw);
 
-  mRefMax = rawfunc->mMax - mRaw + mRef;
+  double mRefMax = rawfunc->mMax - mRaw + mRef;
   psLogMsg ("psastro", 4, "clipping stars fainter than %f\n", mRefMax);
 
@@ -31,12 +50,16 @@
     pmAstromObj *ref = refstars->data[i];
     if (ref->Mag > mRefMax) continue;
-    psArrayAdd (subset, ref);
+    psArrayAdd (subset, 100, ref);
   }
 
-  psLogMsg ("psastro", 4, "keeping %d of %d reference stars\n", subset->n, refstars->n);
+  psLogMsg ("psastro", 4, "keeping %ld of %ld reference stars\n", subset->n, refstars->n);
 
   psMetadataRemoveKey (readout->analysis, "PSASTRO.REFSTARS");
   psMetadataAdd (readout->analysis, PS_LIST_TAIL, "PSASTRO.REFSTARS", PS_DATA_ARRAY, "astrometry matches", subset);
 
+  psFree (rawfunc);
+  psFree (reffunc);
+  psFree (subset);
+
   return true;
 }
