Index: /trunk/Ohana/src/relastro/Makefile
===================================================================
--- /trunk/Ohana/src/relastro/Makefile	(revision 15599)
+++ /trunk/Ohana/src/relastro/Makefile	(revision 15600)
@@ -25,4 +25,5 @@
 $(SRC)/FitMosaic.$(ARCH).o       \
 $(SRC)/FitPM.$(ARCH).o           \
+$(SRC)/FitPar.$(ARCH).o          \
 $(SRC)/FitPMandPar.$(ARCH).o     \
 $(SRC)/FitSimple.$(ARCH).o       \
@@ -50,5 +51,4 @@
 $(SRC)/sort.$(ARCH).o	         \
 $(SRC)/relastro.$(ARCH).o	 \
-$(SRC)/reload_catalogs.$(ARCH).o \
 $(SRC)/save_catalogs.$(ARCH).o   \
 $(SRC)/setExclusions.$(ARCH).o 	 \
Index: /trunk/Ohana/src/relastro/doc/notes.txt
===================================================================
--- /trunk/Ohana/src/relastro/doc/notes.txt	(revision 15599)
+++ /trunk/Ohana/src/relastro/doc/notes.txt	(revision 15600)
@@ -1,2 +1,14 @@
+
+2007.11.12
+
+  relastro is working for the SIMPLE (single-chip) and CHIP
+  (mosaic-chip) modes.  it is not yet working for the MOSAIC mode.
+  The image table contains one mosaic entry and N chip entries for a
+  mosaic with N chips.  The mosaic entry has coord.ctype = DIS, while
+  the corresponding chips have coord.ctype = WRP (and matching
+  time/photcodes).
+
+  (this is fairly weak. we should just define the relationship when we
+  register the chips...)
 
 2007.10.06
Index: /trunk/Ohana/src/relastro/include/relastro.h
===================================================================
--- /trunk/Ohana/src/relastro/include/relastro.h	(revision 15599)
+++ /trunk/Ohana/src/relastro/include/relastro.h	(revision 15600)
@@ -135,5 +135,5 @@
 
 int FIT_MODE;
-enum {FIT_NONE, FIT_AVERAGE, FIT_PM_ONLY, FIT_PM_AND_PAR};
+enum {FIT_NONE, FIT_AVERAGE, FIT_PM_ONLY, FIT_PAR_ONLY, FIT_PM_AND_PAR};
 
 int FIT_TARGET;
@@ -256,5 +256,4 @@
 void          write_coords        PROTO((Header *header, Coords *coords));
 
-
 double **array_init (int Nx, int Ny);
 void array_free (double **array, int Nx);
@@ -281,10 +280,17 @@
 int ParFactor (double *pR, double *pD, double R, double D, time_t T);
 int FitPM (PMFit *fit, double *X, double *dX, double *Y, double *dY, double *T, int Npts);
+int FitPar (PMFit *fit, double *X, double *dX, double *Y, double *dY, double *pR, double *pD, int Npts);
 int FitPMandPar (PMFit *fit, double *X, double *dX, double *Y, double *dY, double *T, double *pR, double *pD, int Npts);
 
 Mosaic *getMosaicForImage (int N);
 
-StarData *getImageRef (Catalog *catalog, int Ncatalog, int im, int *Nstars, int isMosaic);
-StarData *getImageRaw (Catalog *catalog, int Ncatalog, int im, int *Nstars, int isMosaic);
+StarData *getImageRef (Catalog *catalog, int Ncatalog, int im, int *Nstars, CoordMode mode);
+StarData *getImageRaw (Catalog *catalog, int Ncatalog, int im, int *Nstars, CoordMode mode);
+
+Mosaic *getmosaics (int *N);
+void initMosaics (Image *image, int Nimage);
+StarData *getMosaicRaw (Catalog *catalog, int Ncatalog, int mos, int *Nstars);
+StarData *getMosaicRef (Catalog *catalog, int Ncatalog, int mos, int *Nstars);
+Mosaic *getMosaicForImage (int im);
 
 double getMeanR (Measure *measure, Average *average, SecFilt *secfilt);
Index: /trunk/Ohana/src/relastro/src/FitChip.c
===================================================================
--- /trunk/Ohana/src/relastro/src/FitChip.c	(revision 15599)
+++ /trunk/Ohana/src/relastro/src/FitChip.c	(revision 15600)
@@ -13,4 +13,5 @@
   fit_eval (fit);
   fit_apply_coords (fit, coords);
+  fit_free (fit);
 
   // apply new coords to raw (X,Y -> L,M)
Index: /trunk/Ohana/src/relastro/src/FitPar.c
===================================================================
--- /trunk/Ohana/src/relastro/src/FitPar.c	(revision 15600)
+++ /trunk/Ohana/src/relastro/src/FitPar.c	(revision 15600)
@@ -0,0 +1,74 @@
+# include "relastro.h"
+
+/* do we want an init function which does the alloc and a clear function to free? */
+int FitPar (PMFit *fit, double *X, double *dX, double *Y, double *dY, double *pR, double *pD, int Npts) {
+
+  int i;
+
+  double **A, **B;
+  double wx, wy, Wx, Wy, Tx, Ty, Tx2, Ty2, Xs, Ys, XT, YT;
+  double PR, PD, PRT, PDT, PRX, PDY, PR2, PD2;
+
+  A = array_init (3, 3);
+  B = array_init (3, 1);
+
+  Wx = Wy = Xs = Ys = 0.0;
+  PR = PD = PRX = PDY = PR2 = PD2 = 0.0;
+  for (i = 0; i < Npts; i++) {
+    /* handle case where dX or dY = 0.0 */
+    wx = 1.0 / SQ(dX[i]);
+    wy = 1.0 / SQ(dY[i]);
+
+    Wx += wx;
+    Wy += wy;
+
+    PR += pR[i]*wx;
+    PD += pD[i]*wy;
+    
+    PRX += pR[i]*X[i]*wx;
+    PDY += pD[i]*Y[i]*wy;
+    
+    PR2 += SQ(pR[i])*wx;
+    PD2 += SQ(pD[i])*wy;
+
+    Xs += X[i]*wx;
+    Ys += Y[i]*wy;
+  }
+
+  A[0][0] = Wx;
+  A[0][2] = PR;
+
+  A[1][1] = Wy;
+  A[1][2] = PD;
+
+  A[2][0] = PR;
+  A[2][1] = PD;
+  A[2][2] = PR2 + PD2;
+
+  B[0][0] = Xs;
+  B[1][0] = Ys;
+  B[2][0] = PRX + PDY;
+
+  dgaussj (A, 3, B, 1);
+
+  fit[0].Ro = B[0][0];
+  fit[0].Do = B[1][0];
+  fit[0].p  = B[2][0];
+
+  fit[0].uR = 0.0;
+  fit[0].uD = 0.0;
+  
+  fit[0].dRo = sqrt(A[0][0]);
+  fit[0].dDo = sqrt(A[2][2]);
+  fit[0].dp  = sqrt(A[4][4]);
+  
+  fit[0].duR = 0.0;
+  fit[0].duD = 0.0;
+
+  array_free (A, 3);
+  array_free (B, 3);
+
+  /* get the chisq from the matrix values */
+
+  return (TRUE);
+}
Index: /trunk/Ohana/src/relastro/src/FitSimple.c
===================================================================
--- /trunk/Ohana/src/relastro/src/FitSimple.c	(revision 15599)
+++ /trunk/Ohana/src/relastro/src/FitSimple.c	(revision 15600)
@@ -13,4 +13,5 @@
   fit_eval (fit);
   fit_apply_coords (fit, coords);
+  fit_free (fit);
 
   // apply new coords to raw (X,Y -> P,Q)
Index: /trunk/Ohana/src/relastro/src/ImageOps.c
===================================================================
--- /trunk/Ohana/src/relastro/src/ImageOps.c	(revision 15599)
+++ /trunk/Ohana/src/relastro/src/ImageOps.c	(revision 15600)
@@ -96,4 +96,6 @@
   
   measure = &catalog[cat].measure[meas];
+
+  /* find the image that supplied this measurement */
   for (i = 0; i < Nimage; i++) {
     if (image[0].photcode == -1) continue;
@@ -102,7 +104,11 @@
     if (measure[0].t > stop[i]) continue;
     
+    // index for (catalog, measure) -> image
     bin[cat][meas] = i;
 
+    // index for image, Nentry -> catalog
     clist[i][Nlist[i]] = cat;
+
+    // index for image, Nentry -> measure
     mlist[i][Nlist[i]] = meas;
     Nlist[i] ++;
@@ -188,21 +194,11 @@
   mosaic = NULL;
   moscoords = NULL;
-  switch (mode) {
-    case MODE_SIMPLE:
-      break;
-    case MODE_CHIP:
+  if (mode == MODE_MOSAIC) {
       mosaic = getMosaicForImage (im);
       if (mosaic == NULL) {
-	fprintf (stderr, "mosaic not found for image %s\n", image[i].name);
+	fprintf (stderr, "mosaic not found for image %s\n", image[im].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;
   }
 
@@ -228,15 +224,12 @@
 	LM_to_RD (&raw[i].R, &raw[i].D, raw[i].P, raw[i].Q, &image[im].coords);
 	break;
-      case MODE_CHIP:
+      case MODE_MOSAIC:
 	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;
+    default:
+      fprintf (stderr, "error: invalid mode in getImageRaw");
+      abort ();
     }
   }  
@@ -248,5 +241,5 @@
 // return StarData values for averages positions in the specified image, converting coordinates from
 // the sky positions: R,D -> P,Q -> L,M -> X,Y
-StarData *getImageRef (Catalog *catalog, int Ncatalog, int im, int *Nstars, int isMosaic) {
+StarData *getImageRef (Catalog *catalog, int Ncatalog, int im, int *Nstars, CoordMode mode) {
 
   int i, m, c, n;
@@ -260,6 +253,10 @@
   mosaic = NULL;
   moscoords = NULL;
-  if (isMosaic) {
+  if (mode == MODE_MOSAIC) {
     mosaic = getMosaicForImage (im);
+    if (mosaic == NULL) {
+      fprintf (stderr, "mosaic not found for image %s\n", image[im].name);
+      exit (1);
+    }
     moscoords = &mosaic[0].coords;
   }
@@ -273,16 +270,23 @@
     ref[i].R = catalog[c].average[n].R;
     ref[i].D = catalog[c].average[n].D;
+    
+    ref[i].Mag  = catalog[c].measure[m].M;
+    ref[i].dMag = catalog[c].measure[m].dM;
+
     ref[i].mask = FALSE;
 
     /* note that for a Simple image, L,M = P,Q */
-    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, &image[im].coords);
-    } else {
+    switch (mode) {
+      case MODE_SIMPLE:
       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;
       LM_to_XY (&ref[i].X, &ref[i].Y, ref[i].L, ref[i].M, &image[im].coords);
+      break;
+      case MODE_MOSAIC:
+      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, &image[im].coords);
+      break;
     }
   }
Index: /trunk/Ohana/src/relastro/src/MosaicOps.c
===================================================================
--- /trunk/Ohana/src/relastro/src/MosaicOps.c	(revision 15599)
+++ /trunk/Ohana/src/relastro/src/MosaicOps.c	(revision 15600)
@@ -1,23 +1,23 @@
 # include "relastro.h"
 
-Image *getimages (int *N);
-
+// array of mosaic definition structures
 static int    Nmosaic;
 static Mosaic *mosaic;
 
-static int    *Nimlist;
-static int   **imlist; /* mosaic -> image[] */
-static int   **bin;    /* catalog, measure -> mosaic */
+// list of all images associated with a mosaic
+static int   *Nmosaic_own_images; // number of images for this mosaic
+static int   *Amosaic_own_images; // size of allocated array
+static int   **mosaic_own_images; // array of arrays: mosaic -> images
 
-static int    Nimages;
-static int    *moslist; /* image -> mosaic */
+// list of mosaic associated with each image  
+static int    Nmosaic_for_images; // number of images (for internal checks)
+static int    *mosaic_for_images; // array of: image -> mosaic
 
-static int   **clist;  /* mosaic -> catalog[] */
-static int   **mlist;  /* mosiac -> measure[] */
-static int    *Nlist;
-static int    *NLIST;
+Mosaic *getmosaics (int *N) {
+  *N = Nmosaic;
+  return (mosaic);
+}
 
-/* find mosaic frames (unique time periods & photcode name matches mosaic) */
-/* XXX what is a mosaic?  do we need to use the 'DIS/WRP' info to track? */
+// find mosaic frames (unique time periods & photcode name matches mosaic) 
 void initMosaics (Image *image, int Nimage) {
 
@@ -26,26 +26,54 @@
   char *pname;
 
-  // if (!MOSAICNAME[0]) return;
-
   Nmosaic = 0;
   NMOSAIC = 10;
   ALLOCATE (mosaic, Mosaic, NMOSAIC);
 
-  ALLOCATE (imlist, int *, NMOSAIC);
-  ALLOCATE (Nimlist, int, NMOSAIC);
-  ALLOCATE (NIMLIST, int, NMOSAIC);
+  ALLOCATE (Nmosaic_own_images, int, NMOSAIC);
+  ALLOCATE (Amosaic_own_images, int, NMOSAIC);
+  ALLOCATE (mosaic_own_images, int *, NMOSAIC);
 
-  Nimages = Nimage;
-  ALLOCATE (moslist, int, Nimages);
+  /* find the mosaic images (coords.ctype = DIS); generate list of unique mosaics */
+  for (i = 0; i < Nimage; i++) {
+    if (strcmp(&image[i].coords.ctype[4], "-DIS")) continue;
 
-  /* generate list of unique mosaics */
+    /* set image time range */
+    start = image[i].tzero - MAX(0.05*image[i].trate*image[i].NY, 1);
+    stop  = image[i].tzero + MAX(1.05*image[i].trate*image[i].NY, 1);
+
+    /* a new mosaic, define ranges */
+    mosaic[Nmosaic].start = start;
+    mosaic[Nmosaic].stop  = stop;
+    mosaic[Nmosaic].Mcal  = 0.0;
+    mosaic[Nmosaic].dMcal = 0.0;
+    mosaic[Nmosaic].Xm    = 0.0;
+    mosaic[Nmosaic].code  = image[i].code;
+    mosaic[Nmosaic].secz  = image[i].secz;
+    mosaic[Nmosaic].coords = image[i].coords;
+
+    // init the mosaic_own_images array data
+    Nmosaic_own_images[Nmosaic] = 0;
+    Amosaic_own_images[Nmosaic] = 10;
+    ALLOCATE (mosaic_own_images[Nmosaic], int, Amosaic_own_images[Nmosaic]);
+
+    Nmosaic ++;
+    if (Nmosaic == NMOSAIC) {
+      NMOSAIC += 10;
+      REALLOCATE (mosaic, Mosaic, NMOSAIC);
+      REALLOCATE (mosaic_own_images, int *, NMOSAIC);
+      REALLOCATE (Nmosaic_own_images, int, NMOSAIC);
+      REALLOCATE (Amosaic_own_images, int, NMOSAIC);
+    }
+  }
+
+  // array to store image->mosaic index
+  Nmosaic_for_images = Nimage;
+  ALLOCATE (mosaic_for_images, int, Nmosaic_for_images);
+
+  /* now assign the WRP images to these mosaics */
   for (i = 0; i < Nimage; i++) {
+    mosaic_for_images[i] = -1; // default value for no mosaic found
 
-    moslist[i] = -1; // default value for no mosaic found
-
-    /* select valid mosaic images by photcode
-    pname = GetPhotcodeNamebyCode (image[i].photcode);
-    status = strncmp (pname, MOSAICNAME, strlen (MOSAICNAME));
-    if (status) continue; */
+    if (strcmp(&image[i].coords.ctype[4], "-WRP")) continue;
 
     /* set image time range */
@@ -59,263 +87,101 @@
       if (start > mosaic[j].stop)  continue;
       found = TRUE;
+      break;
+    }
+    /* if no matching mosaic exists, skip this image */
+    if (!found) continue;
 
-      /* add image to mosaic image list */
-      imlist[j][Nimlist[j]] = i;
-      Nimlist[j] ++;
-      if (Nimlist[j] == NIMLIST[j]) {
-	NIMLIST[j] += 10;
-	REALLOCATE (imlist[j], int, NIMLIST[j]);
-      }
+    // mosaic corresponding to this image
+    mosaic_for_images[i] = j;
 
-      /* add mosaic to image -> mosaic list */
-      moslist[i] = j;
+    // add image to mosaic_own_image list 
+    mosaic_own_images[j][Nmosaic_own_images[j]] = i;
+    Nmosaic_own_images[j] ++;
+    if (Nmosaic_own_images[j] == Amosaic_own_images[j]) {
+      Amosaic_own_images[j] += 10;
+      REALLOCATE (mosaic_own_images[j], int, Amosaic_own_images[j]);
     }
-    if (found) continue;
-    
-    /* a new mosaic, define ranges */
-    mosaic[Nmosaic].start = start;
-    mosaic[Nmosaic].stop  = stop;
-    mosaic[Nmosaic].Mcal  = 0.0;
-    mosaic[Nmosaic].dMcal = 0.0;
-    mosaic[Nmosaic].Xm    = 0.0;
-    mosaic[Nmosaic].code  = image[i].code;
-    mosaic[Nmosaic].secz  = image[i].secz;
-    mosaic[Nmosaic].coords = image[i].coords;
+    assert (Nmosaic_own_images[j] < Amosaic_own_images[j]);
+  }
 
-    /* add image to mosaic -> image list */
-    NIMLIST[Nmosaic] = 10;
-    Nimlist[Nmosaic] = 1;
-    ALLOCATE (imlist[Nmosaic], int, NIMLIST[Nmosaic]);
-    imlist[Nmosaic][0] = i;
-
-    /* add mosaic to image -> mosaic list */
-    moslist[i] = Nmosaic;
-
-    Nmosaic ++;
-    if (Nmosaic == NMOSAIC) {
-      NMOSAIC += 10;
-      REALLOCATE (mosaic, Mosaic, NMOSAIC);
-      REALLOCATE (imlist, int *, NMOSAIC);
-      REALLOCATE (Nimlist, int, NMOSAIC);
-      REALLOCATE (NIMLIST, int, NMOSAIC);
-    }
-  }
   return;
 }
 
-void initMosaicBins (Catalog *catalog, int Ncatalog) {
+// return StarData values for detections in the specified image, converting coordinates from the
+// chip positions: X,Y -> L,M -> P,Q -> R,D
+StarData *getMosaicRaw (Catalog *catalog, int Ncatalog, int mos, int *Nstars) {
 
-  int i, j;
+  int i, j, im, Nraw, Nnew;
+  StarData *raw, *new;
 
-  /* measure -> mosaic */
-  if (!MOSAICNAME[0]) return;
-  ALLOCATE (bin, int *, Ncatalog);
-  for (i = 0; i < Ncatalog; i++) {
-    ALLOCATE (bin[i], int, MAX (catalog[i].Nmeasure, 1));
-    for (j = 0; j < catalog[i].Nmeasure; j++) bin[i][j] = -1;
+  Nraw = 0;
+  ALLOCATE (raw, StarData, 1);
+
+  // loop over the images owned by this mosaic
+  for (i = 0; i < Nmosaic_own_images[mos]; i++) {
+
+    im = mosaic_own_images[mos][i];
+    
+    // retrieve stars for this chip, applying chip and mosaic astrometry
+    // this function does the reverse-lookup for the mosaic corresponding to this image
+    new = getImageRaw (catalog, Ncatalog, im, &Nnew, MODE_MOSAIC);
+    
+    // merge new and raw
+    REALLOCATE (raw, StarData, Nraw + Nnew);
+    for (j = 0; j < Nnew; j++) {
+      raw[Nraw+j] = new[j];
+    }
+    Nraw += Nnew;
+
+    free (new);
   }
 
-  /* mosaic -> measure */
-  ALLOCATE (Nlist, int, Nmosaic);
-  ALLOCATE (NLIST, int, Nmosaic);
-  ALLOCATE (clist, int *, Nmosaic);
-  ALLOCATE (mlist, int *, Nmosaic);
-
-  for (i = 0; i < Nmosaic; i++) {
-    Nlist[i] = 0;
-    NLIST[i] = 100;
-    ALLOCATE (clist[i], int, NLIST[i]);
-    ALLOCATE (mlist[i], int, NLIST[i]);
-  }
+  *Nstars = Nraw;
+  return (raw);
 }
 
-void freeMosaicBins (int Ncatalog) {
+// return StarData values for averages positions in the specified image, converting coordinates from
+// the sky positions: R,D -> P,Q -> L,M -> X,Y
+StarData *getMosaicRef (Catalog *catalog, int Ncatalog, int mos, int *Nstars) {
 
-  int i;
+  int i, j, im, Nref, Nnew;
+  StarData *ref, *new;
+  
+  Nref = 0;
+  ALLOCATE (ref, StarData, 1);
 
-  /* measure -> mosaic */
-  if (!MOSAICNAME[0]) return;
-  for (i = 0; i < Ncatalog; i++) {
-    free (bin[i]);
+  for (i = 0; i < Nmosaic_own_images[mos]; i++) {
+
+    im = mosaic_own_images[mos][i];
+    
+    // retrieve stars for this chip, applying chip and mosaic astrometry
+    // this function does the reverse-lookup for the mosaic corresponding to this image
+    new = getImageRef (catalog, Ncatalog, im, &Nnew, MODE_MOSAIC);
+    
+    // merge new and ref
+    REALLOCATE (ref, StarData, Nref + Nnew);
+    for (j = 0; j < Nnew; j++) {
+      ref[Nref+j] = new[j];
+    }
+    Nref += Nnew;
+
+    free (new);
   }
-  free (bin);
 
-  /* mosaic -> measure */
-  for (i = 0; i < Nmosaic; i++) {
-    free (clist[i]);
-    free (mlist[i]);
-  }
-  free (Nlist);
-  free (NLIST);
-  free (clist);
-  free (mlist);
+  *Nstars = Nref;
+  return (ref);
 }
 
-int findMosaics (Catalog *catalog, int Ncatalog) {
-  
-  int i, j;
+Mosaic *getMosaicForImage (int im) {
 
-  if (!MOSAICNAME[0]) return (FALSE);
-  for (i = 0; i < Ncatalog; i++) {
-    for (j = 0; j < catalog[i].Nmeasure; j++) {
-      if (TimeSelect) {
-	if (catalog[i].measure[j].t < TSTART) continue;
-	if (catalog[i].measure[j].t > TSTOP) continue;
-      }
-      matchMosaics (catalog, j, i);
-    }
-  }
-  return (TRUE);
-}
+  int mos;
 
-/* modify this function to use the measure->imageID field ? */
-void matchMosaics (Catalog *catalog, int meas, int cat) {
-
-  int i;
-
-  for (i = 0; i < Nmosaic; i++) {
-    if (catalog[cat].measure[meas].t < mosaic[i].start) continue;
-    if (catalog[cat].measure[meas].t > mosaic[i].stop) continue;
-    
-    bin[cat][meas] = i;
-
-    clist[i][Nlist[i]] = cat;
-    mlist[i][Nlist[i]] = meas;
-    Nlist[i] ++;
-    
-    if (Nlist[i] == NLIST[i]) {
-      NLIST[i] += 100;
-      REALLOCATE (clist[i], int, NLIST[i]);
-      REALLOCATE (mlist[i], int, NLIST[i]);
-    }	
-    return;
-  }
-  return;
-}
-
-void plot_mosaic_fields (Catalog *catalog) {
-
-  int i, j, m, c, N, ave, Nimage;
-  double *xlist, *ylist;
-  double Xmin, Xmax, Ymin, Ymax;
-  char string[64];
-  Image *image;
-  Graphdata graphdata;
-
-  if (!MOSAICNAME[0]) return;
-
-  image = getimages (&Nimage);
-
-  N = 0;
-  for (i = 0; i < Nmosaic; i++) 
-    N = MAX (N, Nlist[i]);
-
-  ALLOCATE (xlist, double, N);
-  ALLOCATE (ylist, double, N);
-
-  for (i = 0; i < Nmosaic; i++) {
-    N = 0;
-    Xmin = Ymin = +360.0;
-    Xmax = Ymax = -360.0;
-    for (j = 0; j < Nlist[i]; j++) {
-      
-      m = mlist[i][j];
-      c = clist[i][j];
-      
-      if (catalog[c].measure[m].dbFlags & (ID_MEAS_AREA | ID_MEAS_NOCAL)) continue;
-
-      ave = catalog[c].measure[m].averef;
-      xlist[N] = catalog[c].average[ave].R - catalog[c].measure[m].dR / 3600.0;
-      ylist[N] = catalog[c].average[ave].D - catalog[c].measure[m].dD / 3600.0;
-      N++;
-    }
-  
-    sprintf (string, "Mosaic %d", i);
-    plot_defaults (&graphdata);
-    plot_list (&graphdata, xlist, ylist, N, string, NULL);
-  }
-
-  free (ylist);
-  free (xlist);
-}
-
-void plot_mosaics () {
-
-  int i, bin;
-  double *xlist, *Mlist, *dlist;
-  Graphdata graphdata;
-
-  if (!MOSAICNAME[0]) return;
-
-  ALLOCATE (xlist, double, Nmosaic);
-  ALLOCATE (dlist, double, Nmosaic);
-  ALLOCATE (Mlist, double, Nmosaic);
-
-  for (i = 0; i < Nmosaic; i++) {
-    Mlist[i] = mosaic[i].Mcal;
-    dlist[i] = mosaic[i].dMcal;
-    xlist[i] = mosaic[i].secz;
-  }
-
-  plot_defaults (&graphdata);
-  graphdata.xmin = 0.95;
-  graphdata.xmax = 2.50;
-  graphdata.ymin = PlotdMmin;
-  graphdata.ymax = PlotdMmax;
-  plot_list (&graphdata, xlist, Mlist, Nmosaic, "airmass vs Mcal", "airmass.png");
-  plot_defaults (&graphdata);
-  graphdata.size = 1.5;
-  graphdata.ptype = 7;
-  plot_list (&graphdata, Mlist, dlist, Nmosaic, "Mcal vs dMcal", "MdM.png");
-
-# define NBIN 200
-  REALLOCATE (xlist, double, NBIN);
-  REALLOCATE (Mlist, double, NBIN);
-
-  /**** dMcal histgram ****/
-  for (i = 0; i < NBIN; i++) xlist[i] = 0.00005*i;
-  bzero (Mlist, NBIN*sizeof(double));
-  for (i = 0; i < Nmosaic; i++) {
-    bin = mosaic[i].dMcal / 0.00005;
-    bin = MAX (0, MIN (NBIN - 1, bin));
-    Mlist[bin] += 1.0;
-  }
-  plot_defaults (&graphdata);
-  graphdata.style = 1;
-  plot_list (&graphdata, xlist, Mlist, NBIN, "dMcal hist", "dMcalhist.png");
-
-  free (dlist);
-  free (xlist);
-  free (Mlist);
-}
-
-Mosaic *getMosaicForImage (int Nim) {
-
-  int i;
-  Mosaic *myMosaic;
+  if (im < 0) abort();
+  if (im >= Nmosaic_for_images) abort();
 
   // search for the mosaic that 
-  i = moslist[Nim];
-  if (i < 0) return NULL;
+  mos = mosaic_for_images[im];
+  if (mos < 0) return NULL;
 
-  myMosaic = &mosaic[i];
-  return myMosaic;
+  return &mosaic[mos];
 }
-
-int *SelectRefMosaic (Mosaic **refmosaic, int *Nimage) {
-
-  int i, Imax, Nmax;
-
-  Imax = 0;
-  Nmax = Nimlist[0];
-  for (i = 0; i < Nmosaic; i++) {
-    if (Nimlist[i] > Nmax) {
-      Imax = i;
-      Nmax = Nimlist[i];
-    }
-  }
-
-  *refmosaic = &mosaic[Imax];
-  *Nimage = Nmax;
-  return (imlist[Imax]);
-}
Index: /trunk/Ohana/src/relastro/src/UpdateChips.c
===================================================================
--- /trunk/Ohana/src/relastro/src/UpdateChips.c	(revision 15599)
+++ /trunk/Ohana/src/relastro/src/UpdateChips.c	(revision 15600)
@@ -16,8 +16,8 @@
 
     /* convert measure coordinates to raw entries */
-    raw = getImageRaw (catalog, Ncatalog, i, &Nstars, MODE_CHIP);
+    raw = getImageRaw (catalog, Ncatalog, i, &Nstars, MODE_MOSAIC);
 
     /* convert average coordinates to ref entries */
-    ref = getImageRef (catalog, Ncatalog, i, &Nstars, MODE_CHIP);
+    ref = getImageRef (catalog, Ncatalog, i, &Nstars, MODE_MOSAIC);
 
     FitChip (raw, ref, Nstars, &image[i].coords);
Index: /trunk/Ohana/src/relastro/src/UpdateMosaic.c
===================================================================
--- /trunk/Ohana/src/relastro/src/UpdateMosaic.c	(revision 15599)
+++ /trunk/Ohana/src/relastro/src/UpdateMosaic.c	(revision 15600)
@@ -4,24 +4,20 @@
 
   /* we can measure new image parameters for each mosaic independently */
-  int i, Nimage, Nstars;
-  Image *image;
+  int i, Nmosaic, Nstars;
+  Mosaic *mosaic;
   StarData *raw, *ref;
 
-  image = getimages (&Nimage);
+  mosaic = getmosaics (&Nmosaic);
 
-  for (i = 0; i < Nimage; i++) {
-
-    /* skip all except DIS images */
-    if (strcmp(&image[i].coords.ctype[4], "-DIS")) continue;
-
-    /* XXX I probably need modes for getImageRaw/Ref to distinguish SIMPLE, CHIP, MOSAIC */
+  for (i = 0; i < Nmosaic; i++) {
 
     /* convert measure coordinates to raw entries */
-    raw = getImageRaw (catalog, Ncatalog, i, &Nstars, MODE_MOSAIC);
+    raw = getMosaicRaw (catalog, Ncatalog, i, &Nstars);
 
     /* convert average coordinates to ref entries */
-    ref = getImageRef (catalog, Ncatalog, i, &Nstars, MODE_MOSAIC);
+    ref = getMosaicRef (catalog, Ncatalog, i, &Nstars);
 
-    FitMosaic (raw, ref, Nstars, &image[i].coords);
+    // XXX : I'll need to supply these back to the image[] entry
+    FitMosaic (raw, ref, Nstars, &mosaic[i].coords);
 
     free (raw);
Index: /trunk/Ohana/src/relastro/src/UpdateObjects.c
===================================================================
--- /trunk/Ohana/src/relastro/src/UpdateObjects.c	(revision 15599)
+++ /trunk/Ohana/src/relastro/src/UpdateObjects.c	(revision 15600)
@@ -153,4 +153,15 @@
 	  break;
 
+	case FIT_PAR_ONLY:
+	  for (k = 0; k < N; k++) {
+	    ParFactor (&pX[k], &pY[k], R[k], D[k], T[k]);
+	  }
+	  FitPar (&fit, X, dX, Y, dY, pX, pY, N);
+
+	  // project Ro, Do back to RA,DEC
+	  XY_to_RD (&fit.Ro, &fit.Do, fit.Ro, fit.Do, &coords);
+	  Npar ++;
+	  break;
+
 	case FIT_PM_AND_PAR:
 	  for (k = 0; k < N; k++) {
@@ -161,4 +172,5 @@
 	  Npar ++;
 	  break;
+
 	default:
 	  fprintf (stderr, "programming error at %s, %s", __FILE__, __LINE__);
Index: /trunk/Ohana/src/relastro/src/args.c
===================================================================
--- /trunk/Ohana/src/relastro/src/args.c	(revision 15599)
+++ /trunk/Ohana/src/relastro/src/args.c	(revision 15600)
@@ -18,4 +18,8 @@
 	remove_argument (N, &argc, argv);
 	FIT_MODE = FIT_PM_ONLY;
+    }
+    if ((N = get_argument (argc, argv, "-par"))) {
+	remove_argument (N, &argc, argv);
+	FIT_MODE = FIT_PAR_ONLY;
     }
     if ((N = get_argument (argc, argv, "-pmpar"))) {
Index: /trunk/Ohana/src/relastro/src/relastro.c
===================================================================
--- /trunk/Ohana/src/relastro/src/relastro.c	(revision 15599)
+++ /trunk/Ohana/src/relastro/src/relastro.c	(revision 15600)
@@ -27,14 +27,11 @@
   catalog = load_catalogs (skylist, &Ncatalog, (FIT_TARGET != TARGET_OBJECTS));
 
-  /* match measurements with images, mosaics */
-  initImageBins  (catalog, Ncatalog);
-  // initMosaicBins (catalog, Ncatalog);
-
+  /* match measurements with images */
+  initImageBins (catalog, Ncatalog);
   findImages (catalog, Ncatalog);
-  // findMosaics (catalog, Ncatalog);  /* also sets Grid values */
 
   if (PLOTSTUFF) {
     // plot_star_coords (catalog, Ncatalog);
-    plot_mosaic_fields (catalog);
+    // plot_mosaic_fields (catalog);
   }
 
@@ -62,13 +59,4 @@
   }
 
-  /* put these types of functions inside the update functions
-      plot_scatter (catalog, Ncatalog); 
-      plot_grid (catalog); 
-      plot_mosaics ();
-      plot_images ();
-      plot_stars (catalog, Ncatalog);
-      plot_chisq (catalog, Ncatalog);
-  */
-
   if (!UPDATE) exit (0);
 
