Index: /trunk/Ohana/src/addstar/src/SEDfit.c
===================================================================
--- /trunk/Ohana/src/addstar/src/SEDfit.c	(revision 8001)
+++ /trunk/Ohana/src/addstar/src/SEDfit.c	(revision 8002)
@@ -1,3 +1,5 @@
 # include "sedstar.h"
+
+// XXX a couple of fixes should speed this up a bit: test
 
 int SEDfitCatalog (Catalog *outcat, Catalog *incat, SEDtable *table) {
@@ -7,8 +9,37 @@
   unsigned short USNOred, USNOblu;
   float color;
-  int *found, valid;
+  int *found, valid, *modelRow, *reqRow;
 
   SEDtableRow sourceValue, sourceError;
   SEDfit minFit, testFit;
+
+  Nmodel = Nreq = 0;
+  for (i = 0; i < table[0].Nfilter; i++) {
+    if (table[0].mode[i] == SED_REQ) Nreq ++;
+    if (table[0].mode[i] == SED_MODEL) Nmodel ++;
+  }
+  if (Nmodel < 1) {
+    fprintf (stderr, "no model filter defined\n!");
+    exit (2);
+  }
+  if (Nreq < 1) {
+    fprintf (stderr, "no required filter defined\n!");
+    exit (2);
+  }
+
+  ALLOCATE (modelRow, int, Nmodel);
+  for (j = i = 0; i < table[0].Nfilter; i++) {
+    if (table[0].mode[i] == SED_MODEL) { 
+      modelRow[j] = i;
+      j++;
+    }
+  }
+  ALLOCATE (reqRow, int, Nreq);
+  for (j = i = 0; i < table[0].Nfilter; i++) {
+    if (table[0].mode[i] == SED_REQ) { 
+      reqRow[j] = i;
+      j++;
+    }
+  }
 
   sourceValue.mags = NULL;
@@ -64,7 +95,8 @@
     if (Nphot < 3) continue;
 
+    // XXX pre-select list of REQ entries; loop over only those?
     valid = TRUE;
-    for (j = 0; valid && (j < table[0].Nfilter); j++) {
-      if ((table[0].mode[j] == SED_REQ) && !found[j]) valid = FALSE;
+    for (j = 0; valid && (j < Nreq); j++) {
+      if ((table[0].mode[reqRow[j]] == SED_REQ) && !found[reqRow[j]]) valid = FALSE;
     }
     if (!valid) continue;
@@ -77,5 +109,5 @@
     // find tableRow within 0.1 mag of color 
     // XXX : check on the delta value
-    start = SEDcolorBracket (table, color);
+    start = SEDcolorBracket (table, color, 0.05);
     minFit = SEDchisq (table[0].row[start], &sourceValue, &sourceError, table[0].Nfilter);
     minFit.row = start;
@@ -91,5 +123,5 @@
 	minFit.row = row;
       }
-      if (fabs(table[0].row[row][0].color - color) > 0.5) done = TRUE;
+      if (fabs(table[0].row[row][0].color - color) > 0.25) done = TRUE;
       row --;
     }
@@ -105,5 +137,5 @@
 	minFit.row = row;
       }
-      if (fabs(table[0].row[row][0].color - color) > 0.5) done = TRUE;
+      if (fabs(table[0].row[row][0].color - color) > 0.25) done = TRUE;
       row ++;
     }
@@ -145,14 +177,20 @@
 
     // we now have the min chisq row. use this to supply the other filter values....
-    for (j = 0; valid && (j < table[0].Nfilter); j++) {
-      if (table[0].mode[j] != SED_MODEL) continue;
+    // XXX pre-select the SED_MODEL rows...
+    if (Nmeas + table[0].Nfilter >= NMEAS) {
+	NMEAS += 100 + table[0].Nfilter;
+	REALLOCATE (outcat[0].measure, Measure, NMEAS);
+    }
+
+    for (j = 0; valid && (j < Nmodel; j++) {
+      n = modelRow[j];
       outcat[0].measure[Nmeas].dR_PS       = 0.0;
       outcat[0].measure[Nmeas].dD_PS       = 0.0;
-      outcat[0].measure[Nmeas].M_PS        = MIN (table[0].row[minFit.row][0].mags[j] + minFit.Md,  NO_MAG);
+      outcat[0].measure[Nmeas].M_PS        = MIN (table[0].row[minFit.row][0].mags[n] + minFit.Md,  NO_MAG);
       outcat[0].measure[Nmeas].dM_PS       = 0.0;
       outcat[0].measure[Nmeas].Mcal_PS     = 0;
       outcat[0].measure[Nmeas].t           = TIMEREF;
       outcat[0].measure[Nmeas].averef      = Nave;
-      outcat[0].measure[Nmeas].source      = table[0].code[j];
+      outcat[0].measure[Nmeas].source      = table[0].code[n];
       outcat[0].measure[Nmeas].dophot      = 0;
       outcat[0].measure[Nmeas].flags       = 0;
@@ -167,5 +205,4 @@
       outcat[0].average[Nave].Nm++;
       Nmeas ++;
-      CHECK_REALLOCATE (outcat[0].measure, Measure, NMEAS, Nmeas, 100);
     }
 
@@ -180,4 +217,8 @@
   outcat[0].Nmeasure = Nmeas;
   
+  free (sourceValue.mags);
+  free (sourceError.mags);
+  free (found);
+
   SEDfitClear ();
 }
Index: /trunk/Ohana/src/addstar/src/SEDops.c
===================================================================
--- /trunk/Ohana/src/addstar/src/SEDops.c	(revision 8001)
+++ /trunk/Ohana/src/addstar/src/SEDops.c	(revision 8002)
@@ -34,5 +34,5 @@
 
 // find the first table row within 0.1 mag of the requested color (or within 10)
-int SEDcolorBracket (SEDtable *table, float color) {
+int SEDcolorBracket (SEDtable *table, float color, float delta) {
 
   int Nlo, Nhi, N;
@@ -41,5 +41,5 @@
   N = Nlo = 0; Nhi = table[0].Nrow;
   tcolor = table[0].row[Nlo][0].color;
-  while ((Nhi - Nlo > 10) && (fabs(tcolor-color) > 0.1)) {
+  while ((Nhi - Nlo > 10) && (fabs(tcolor-color) > delta)) {
     N = 0.5*(Nlo + Nhi);
     N = MAX (N, 0);
Index: /trunk/Ohana/src/addstar/src/sedstar.c
===================================================================
--- /trunk/Ohana/src/addstar/src/sedstar.c	(revision 8001)
+++ /trunk/Ohana/src/addstar/src/sedstar.c	(revision 8002)
@@ -3,6 +3,6 @@
 int main (int argc, char **argv) {
 
-  char *root, *ext;
-  int i, N, Nrefcat, status;
+  char *root, *ext, tmp;
+  int i, N, Nbytes, Nrefcat, status;
   SkyList *skylist;
   SkyTable *sky, *sky2mass;
@@ -42,6 +42,9 @@
 
     ext = incatalog.filename + strlen(CATDIR);
-    ALLOCATE (outcatalog.filename, char, strlen(argv[2]) + strlen(ext) + 2);
-    sprintf (outcatalog.filename, "%s/%s", argv[2], ext);
+    while (*ext == '/') ext++;
+
+    Nbytes = snprintf (&tmp, 0, "%s/%s", argv[2], ext);
+    ALLOCATE (outcatalog.filename, char, Nbytes + 1);
+    snprintf (outcatalog.filename, Nbytes + 1, "%s/%s", argv[2], ext);
 
     if (!check_file_access (outcatalog.filename, TRUE, TRUE)) Shutdown ("can't create output directory");
@@ -56,6 +59,8 @@
     unlock_catalog (&outcatalog);
     free (outcatalog.filename);
+    free_catalog (&outcatalog);
 
     unlock_catalog (&incatalog);
+    free_catalog (&incatalog);
   }
 
