Index: /trunk/Ohana/src/relastro/Makefile
===================================================================
--- /trunk/Ohana/src/relastro/Makefile	(revision 15589)
+++ /trunk/Ohana/src/relastro/Makefile	(revision 15590)
@@ -55,4 +55,4 @@
 $(SRC)/write_coords.$(ARCH).o
 
-$(RELASTRO): $(INC)/relastro.h
-$(BIN)/relastro.$(ARCH): $(RELASTRO)
+$(RELASTRO): $(INC)/relastro.h $(KAPA_INCS)
+$(BIN)/relastro.$(ARCH): $(RELASTRO) $(KAPA_LIBS)
Index: /trunk/Ohana/src/relastro/include/relastro.h
===================================================================
--- /trunk/Ohana/src/relastro/include/relastro.h	(revision 15589)
+++ /trunk/Ohana/src/relastro/include/relastro.h	(revision 15590)
@@ -3,4 +3,10 @@
 # include <kapa.h>
 # include <signal.h>
+
+typedef enum {
+  MODE_SIMPLE,
+  MODE_CHIP,
+  MODE_MOSAIC,
+} CoordMode;
 
 typedef struct {
@@ -277,5 +283,5 @@
 int FitPMandPar (PMFit *fit, double *X, double *dX, double *Y, double *dY, double *T, double *pR, double *pD, int Npts);
 
-Image *getMosaicForImage (int N);
+Mosaic *getMosaicForImage (int N);
 
 StarData *getImageRef (Catalog *catalog, int Ncatalog, int im, int *Nstars, int isMosaic);
Index: /trunk/Ohana/src/relastro/src/ImageOps.c
===================================================================
--- /trunk/Ohana/src/relastro/src/ImageOps.c	(revision 15589)
+++ /trunk/Ohana/src/relastro/src/ImageOps.c	(revision 15590)
@@ -176,9 +176,9 @@
 // return StarData values for detections in the specified image, converting coordinates from the
 // chip positions: X,Y -> L,M -> P,Q -> R,D
-StarData *getImageRaw (Catalog *catalog, int Ncatalog, int im, int *Nstars, int isMosaic) {
+StarData *getImageRaw (Catalog *catalog, int Ncatalog, int im, int *Nstars, CoordMode mode) {
 
   int i, m, c;
-
-  Image *mosaic;
+  
+  Mosaic *mosaic;
   Coords *moscoords;
   StarData *raw;
@@ -188,11 +188,21 @@
   mosaic = NULL;
   moscoords = NULL;
-  if (isMosaic) {
-    mosaic = getMosaicForImage (im);
-    if (mosaic == NULL) {
-      fprintf (stderr, "mosaic not found for image %s\n", image[i].name);
-      exit (1);
-    }
-    moscoords = &mosaic[0].coords;
+  switch (mode) {
+    case MODE_SIMPLE:
+      break;
+    case MODE_CHIP:
+      mosaic = getMosaicForImage (im);
+      if (mosaic == NULL) {
+	fprintf (stderr, "mosaic not found for image %s\n", image[i].name);
+	exit (1);
+      }
+      moscoords = &mosaic[0].coords;
+      break;
+    case MODE_MOSAIC:
+      // XXX find all images which are chips for this mosaic image
+      // XXX count the stars in the superset of lists?
+      fprintf (stderr, "problem with the mosaic mode (need to grab the correct set of stars)");
+      abort ();
+      break;
   }
 
@@ -208,16 +218,28 @@
     raw[i].dMag = catalog[c].measure[m].dM;
 
-    /* note that for a Simple image, L,M = P,Q */
-    XY_to_LM (&raw[i].L, &raw[i].M, raw[i].X, raw[i].Y, &image[im].coords);
-    if (isMosaic) {
-      XY_to_LM (&raw[i].P, &raw[i].Q, raw[i].L, raw[i].M, moscoords);
-      LM_to_RD (&raw[i].R, &raw[i].D, raw[i].P, raw[i].Q, moscoords);
-    } else {
-      raw[i].P = raw[i].L;
-      raw[i].Q = raw[i].M;
-      LM_to_RD (&raw[i].R, &raw[i].D, raw[i].P, raw[i].Q, &image[im].coords);
+    raw[i].mask = FALSE;
+
+    switch (mode) {
+      case MODE_SIMPLE:
+	/* note that for a Simple image, L,M = P,Q */
+	XY_to_LM (&raw[i].L, &raw[i].M, raw[i].X, raw[i].Y, &image[im].coords);
+	raw[i].P = raw[i].L;
+	raw[i].Q = raw[i].M;
+	LM_to_RD (&raw[i].R, &raw[i].D, raw[i].P, raw[i].Q, &image[im].coords);
+	break;
+      case MODE_CHIP:
+	XY_to_LM (&raw[i].L, &raw[i].M, raw[i].X, raw[i].Y, &image[im].coords);
+	XY_to_LM (&raw[i].P, &raw[i].Q, raw[i].L, raw[i].M, moscoords);
+	LM_to_RD (&raw[i].R, &raw[i].D, raw[i].P, raw[i].Q, moscoords);
+	break;
+      case MODE_MOSAIC:
+	XY_to_LM (&raw[i].L, &raw[i].M, raw[i].X, raw[i].Y, &image[im].coords);
+	raw[i].P = raw[i].L;
+	raw[i].Q = raw[i].M;
+	LM_to_RD (&raw[i].R, &raw[i].D, raw[i].P, raw[i].Q, &image[im].coords);
+	break;
     }
-  }
-  
+  }  
+
   *Nstars = Nlist[im];
   return (raw);
@@ -230,5 +252,5 @@
   int i, m, c, n;
 
-  Image *mosaic;
+  Mosaic *mosaic;
   Coords *moscoords;
   StarData *ref;
@@ -251,11 +273,13 @@
     ref[i].R = catalog[c].average[n].R;
     ref[i].D = catalog[c].average[n].D;
+    ref[i].mask = FALSE;
 
     /* note that for a Simple image, L,M = P,Q */
-    RD_to_LM (&ref[i].P, &ref[i].Q, ref[i].R, ref[i].D, &image[im].coords);
     if (isMosaic) {
+      RD_to_LM (&ref[i].P, &ref[i].Q, ref[i].R, ref[i].D, moscoords);
       LM_to_XY (&ref[i].M, &ref[i].L, ref[i].P, ref[i].Q, moscoords);
-      LM_to_XY (&ref[i].X, &ref[i].Y, ref[i].L, ref[i].M, moscoords);
+      LM_to_XY (&ref[i].X, &ref[i].Y, ref[i].L, ref[i].M, &image[im].coords);
     } else {
+      RD_to_LM (&ref[i].P, &ref[i].Q, ref[i].R, ref[i].D, &image[im].coords);
       ref[i].L = ref[i].P;
       ref[i].M = ref[i].Q;
Index: /trunk/Ohana/src/relastro/src/MosaicOps.c
===================================================================
--- /trunk/Ohana/src/relastro/src/MosaicOps.c	(revision 15589)
+++ /trunk/Ohana/src/relastro/src/MosaicOps.c	(revision 15590)
@@ -11,5 +11,5 @@
 
 static int    Nimages;
-static int   **moslist; /* image -> mosaic */
+static int    *moslist; /* image -> mosaic */
 
 static int   **clist;  /* mosaic -> catalog[] */
@@ -81,4 +81,5 @@
     mosaic[Nmosaic].code  = image[i].code;
     mosaic[Nmosaic].secz  = image[i].secz;
+    mosaic[Nmosaic].coords = image[i].coords;
 
     /* add image to mosaic -> image list */
@@ -289,8 +290,8 @@
 }
 
-Image *getMosaicForImage (int Nim) {
+Mosaic *getMosaicForImage (int Nim) {
 
   int i;
-  Image *mosaic;
+  Mosaic *myMosaic;
 
   // search for the mosaic that 
@@ -298,6 +299,6 @@
   if (i < 0) return NULL;
 
-  mosaic = &mosaic[i];
-  return mosaic;
+  myMosaic = &mosaic[i];
+  return myMosaic;
 }
 
Index: /trunk/Ohana/src/relastro/src/UpdateChips.c
===================================================================
--- /trunk/Ohana/src/relastro/src/UpdateChips.c	(revision 15589)
+++ /trunk/Ohana/src/relastro/src/UpdateChips.c	(revision 15590)
@@ -16,8 +16,8 @@
 
     /* convert measure coordinates to raw entries */
-    raw = getImageRaw (catalog, Ncatalog, i, &Nstars, TRUE);
+    raw = getImageRaw (catalog, Ncatalog, i, &Nstars, MODE_CHIP);
 
     /* convert average coordinates to ref entries */
-    ref = getImageRef (catalog, Ncatalog, i, &Nstars, TRUE);
+    ref = getImageRef (catalog, Ncatalog, i, &Nstars, MODE_CHIP);
 
     FitChip (raw, ref, Nstars, &image[i].coords);
Index: /trunk/Ohana/src/relastro/src/UpdateMosaic.c
===================================================================
--- /trunk/Ohana/src/relastro/src/UpdateMosaic.c	(revision 15589)
+++ /trunk/Ohana/src/relastro/src/UpdateMosaic.c	(revision 15590)
@@ -18,8 +18,8 @@
 
     /* convert measure coordinates to raw entries */
-    raw = getImageRaw (catalog, Ncatalog, i, &Nstars, TRUE);
+    raw = getImageRaw (catalog, Ncatalog, i, &Nstars, MODE_MOSAIC);
 
     /* convert average coordinates to ref entries */
-    ref = getImageRef (catalog, Ncatalog, i, &Nstars, TRUE);
+    ref = getImageRef (catalog, Ncatalog, i, &Nstars, MODE_MOSAIC);
 
     FitMosaic (raw, ref, Nstars, &image[i].coords);
Index: /trunk/Ohana/src/relastro/src/UpdateSimple.c
===================================================================
--- /trunk/Ohana/src/relastro/src/UpdateSimple.c	(revision 15589)
+++ /trunk/Ohana/src/relastro/src/UpdateSimple.c	(revision 15590)
@@ -17,8 +17,8 @@
 
     /* convert measure coordinates to raw entries */
-    raw = getImageRaw (catalog, Ncatalog, i, &Nstars, FALSE);
+    raw = getImageRaw (catalog, Ncatalog, i, &Nstars, MODE_SIMPLE);
 
     /* convert average coordinates to ref entries */
-    ref = getImageRef (catalog, Ncatalog, i, &Nstars, FALSE);
+    ref = getImageRef (catalog, Ncatalog, i, &Nstars, MODE_SIMPLE);
 
     FitSimple (raw, ref, Nstars, &image[i].coords);
Index: /trunk/Ohana/src/relastro/src/fitpoly.c
===================================================================
--- /trunk/Ohana/src/relastro/src/fitpoly.c	(revision 15589)
+++ /trunk/Ohana/src/relastro/src/fitpoly.c	(revision 15590)
@@ -130,6 +130,4 @@
   }
 
-  dgaussj (matrix, fit[0].Nelems, vector, 2); 
-
   for (i = 0; i < fit[0].Nelems; i++) {
     ix = i % fit[0].Nterms;
@@ -139,4 +137,13 @@
   }	
 
+  dgaussj (matrix, fit[0].Nelems, vector, 2); 
+
+  for (i = 0; i < fit[0].Nelems; i++) {
+    ix = i % fit[0].Nterms;
+    iy = i / fit[0].Nterms;
+    fprintf (stderr, "x2 : x^%dy^%d: %10.4g    y2 : x^%dy^%d: %10.4g \n", 
+	     ix, iy, vector[i][0], ix, iy, vector[i][1]);
+  }	
+
   /* remap the vector terms into xfit,yfit */
   for (i = 0; i < fit[0].Nelems; i++) {
@@ -256,22 +263,26 @@
 
   // set the polyterm elements 
-  coords[0].polyterms[0][0] = modfit[0].xfit[2][0];
-  coords[0].polyterms[1][0] = modfit[0].xfit[1][1];
-  coords[0].polyterms[2][0] = modfit[0].xfit[0][2];
-
-  coords[0].polyterms[0][1] = modfit[0].yfit[2][0];
-  coords[0].polyterms[1][1] = modfit[0].yfit[1][1];
-  coords[0].polyterms[2][1] = modfit[0].yfit[0][2];
+  if (coords->Npolyterms > 1) {
+    coords[0].polyterms[0][0] = modfit[0].xfit[2][0];
+    coords[0].polyterms[1][0] = modfit[0].xfit[1][1];
+    coords[0].polyterms[2][0] = modfit[0].xfit[0][2];
+
+    coords[0].polyterms[0][1] = modfit[0].yfit[2][0];
+    coords[0].polyterms[1][1] = modfit[0].yfit[1][1];
+    coords[0].polyterms[2][1] = modfit[0].yfit[0][2];
+  }
 
   // I need to validate Norder
-  coords[0].polyterms[3][0] = modfit[0].xfit[3][0];
-  coords[0].polyterms[4][0] = modfit[0].xfit[2][1];
-  coords[0].polyterms[5][0] = modfit[0].xfit[1][2];
-  coords[0].polyterms[6][0] = modfit[0].xfit[0][3];
-
-  coords[0].polyterms[3][1] = modfit[0].yfit[3][0];
-  coords[0].polyterms[4][1] = modfit[0].yfit[2][1];
-  coords[0].polyterms[5][1] = modfit[0].yfit[1][2];
-  coords[0].polyterms[6][1] = modfit[0].yfit[0][3];
+  if (coords->Npolyterms > 2) {
+    coords[0].polyterms[3][0] = modfit[0].xfit[3][0];
+    coords[0].polyterms[4][0] = modfit[0].xfit[2][1];
+    coords[0].polyterms[5][0] = modfit[0].xfit[1][2];
+    coords[0].polyterms[6][0] = modfit[0].xfit[0][3];
+
+    coords[0].polyterms[3][1] = modfit[0].yfit[3][0];
+    coords[0].polyterms[4][1] = modfit[0].yfit[2][1];
+    coords[0].polyterms[5][1] = modfit[0].yfit[1][2];
+    coords[0].polyterms[6][1] = modfit[0].yfit[0][3];
+  }
 
   /* we do not modify crval1,2: these are kept at the default values */
Index: /trunk/Ohana/src/relastro/src/mkpolyterm.c
===================================================================
--- /trunk/Ohana/src/relastro/src/mkpolyterm.c	(revision 15589)
+++ /trunk/Ohana/src/relastro/src/mkpolyterm.c	(revision 15590)
@@ -100,8 +100,8 @@
   yPx = poly2d_copy (input->yfit, Nx, Ny);
 
-  for (i = 0; i <= input->Nterms; i++) {
+  for (i = 0; i < input->Nterms; i++) {
     xPy = poly2d_copy (xPx, Nx, Ny);
     yPy = poly2d_copy (yPx, Nx, Ny);
-    for (j = 0; j <= input->Nterms; j++) {
+    for (j = 0; j < input->Nterms; j++) {
       output->xfit[i][j] = poly2d_eval (xPy, Nx, Ny, Xo, Yo) / factorial(i) / factorial(j);
       output->yfit[i][j] = poly2d_eval (yPy, Nx, Ny, Xo, Yo) / factorial(i) / factorial(j);
