Index: /trunk/Ohana/src/opihi/cmd.astro/coords.c
===================================================================
--- /trunk/Ohana/src/opihi/cmd.astro/coords.c	(revision 3392)
+++ /trunk/Ohana/src/opihi/cmd.astro/coords.c	(revision 3393)
@@ -1,3 +1,5 @@
 # include "astro.h"
+
+enum {NONE, SKY, PIXEL};
 
 int coords (int argc, char **argv) {
@@ -5,8 +7,16 @@
   int mode, N;
   double X, Y, R, D;
-  Coords coords;
-  Buffer *buf;
+  char *MOSAIC;
+  Coords coords, moscoords;
+  Buffer *buf, *mosbuffer;
 
-  mode = 0;
+  MOSAIC = NULL;
+  if ((N = get_argument (argc, argv, "-mosaic"))) {
+    remove_argument (N, &argc, argv);
+    MOSAIC = strcreate (argv[N]);
+    remove_argument (N, &argc, argv);
+  }
+
+  mode = NONE;
   if ((N = get_argument (argc, argv, "-p"))) {
     remove_argument (N, &argc, argv);
@@ -15,11 +25,8 @@
     Y = atof(argv[N]);
     remove_argument (N, &argc, argv);
-    mode = 1;
+    mode = SKY;
   }
   if ((N = get_argument (argc, argv, "-c"))) {
-    if (mode == 1) {
-      fprintf (stderr, "only one of -p or -c can be used\n");
-      return (FALSE);
-    }
+    if (mode == SKY) goto syntax;
     remove_argument (N, &argc, argv);
     R = atof(argv[N]);
@@ -27,29 +34,43 @@
     D = atof(argv[N]);
     remove_argument (N, &argc, argv);
-    mode = 2;
+    mode = PIXEL;
   }
-  if ((mode == 0) || (argc != 2)) {
-    fprintf (stderr, "USAGE: coords [buffer] (-c R D) / (-p X Y)\n");
-    return (FALSE);
-  }    
+  if (mode == NONE) goto syntax;
+  if (argc != 2) goto syntax;
 
-  if ((buf = SelectBuffer (argv[1], OLDBUFFER, TRUE)) == NULL) return (FALSE);
+  if ((buf = SelectBuffer (argv[1], OLDBUFFER, TRUE)) == NULL) goto escape;
   GetCoords (&coords, &buf[0].header);
+  if (strcmp(&coords.ctype[4], "-WRP")) {
+    if (MOSAIC == NULL) {
+      fprintf (stderr, "must supply mosaic for WRP coords\n");
+      return (FALSE);
+    }
+    if ((mosbuffer = SelectBuffer (MOSAIC, OLDBUFFER, TRUE)) == NULL) goto escape;
+    GetCoords (&moscoords, &mosbuffer[0].header);
+    RegisterMosaic (&moscoords);
+  }
   
-  if (mode == 1) {
+  if (mode == SKY) {
     XY_to_RD (&R, &D, X, Y, &coords);
     fprintf (stderr, "%10.6f %10.6f\n", R, D);
     set_variable ("RA", R);
     set_variable ("DEC", D);
+    return (TRUE);
   }
     
-  if (mode == 2) {
+  if (mode == PIXEL) {
     RD_to_XY (&X, &Y, R, D, &coords);
     fprintf (stderr, "%7.2f %7.2f\n", X, Y);
     set_variable ("Xc", X);
     set_variable ("Yc", Y);
+    return (TRUE);
   }
+  return (FALSE);
 
-  return (TRUE);
-
+ syntax:
+  fprintf (stderr, "USAGE: coords [buffer] (-c R D) / (-p X Y)\n");
+  fprintf (stderr, "only one of -p or -c can be used\n");
+ escape:
+  if (MOSAIC != NULL) free (MOSAIC);
+  return (FALSE);
 }
Index: /trunk/Ohana/src/opihi/cmd.astro/drizzle.c
===================================================================
--- /trunk/Ohana/src/opihi/cmd.astro/drizzle.c	(revision 3392)
+++ /trunk/Ohana/src/opihi/cmd.astro/drizzle.c	(revision 3393)
@@ -1,3 +1,5 @@
 # include "astro.h"
+
+/*** needs mosaic astrometry ***/
 
 static double XO, XX, XY;
Index: /trunk/Ohana/src/opihi/dvo/ImageOps.c
===================================================================
--- /trunk/Ohana/src/opihi/dvo/ImageOps.c	(revision 3392)
+++ /trunk/Ohana/src/opihi/dvo/ImageOps.c	(revision 3393)
@@ -9,6 +9,5 @@
   int *subset;
   int N, n, npts;
-  double r, d, x[4], y[4], Rmin, Rmax, Rmid;
-  double X[8], Y[8];
+  double r, d, X, Y, x[4], y[4], Rmin, Rmax, Rmid;
 
   if (RegionSelect) {
@@ -16,4 +15,5 @@
     Rmax = graph[0].coords.crval1 + 182.0;
     Rmid = 0.5*(Rmin + Rmax);
+    BuildChipMatch (image, Nimage);
   }
 
@@ -22,5 +22,5 @@
     trange = fabs (trange);
   }
-  status = InPic = TRUE;
+
   npts = 200;
   ALLOCATE (subset, int, npts);
@@ -29,4 +29,5 @@
     if (TimeSelect && ((image[i].tzero < tzero) || (image[i].tzero+image[i].trate*image[i].NY > tzero + trange))) continue;
     if (RegionSelect) {
+      if (!FindMosaicForImage (image, Nimage, i)) continue;
       /* project this image to screen display coords */
       x[0] = 0;           y[0] = 0;
@@ -34,38 +35,31 @@
       x[2] = image[i].NX; y[2] = image[i].NY;
       x[3] = 0;           y[3] = image[i].NY;
-      status = FALSE;
-      flipped = FALSE;
+      InPic = flipped = FALSE;
       for (j = 0; j < 4; j++) {
 	XY_to_RD (&r, &d, x[j], y[j], &image[i].coords);
-	while ((j == 0) && (r < Rmin)) { flipped = TRUE; r += 360.0; }
-	while ((j == 0) && (r > Rmax)) { flipped = TRUE; r -= 360.0; }
-	if ((j > 0) && flipped) {
-	  while (r < Rmid) r+= 360.0;
-	  while (r > Rmid) r-= 360.0;
+	/* use same side of 0,360 boundary for all corners */
+	if ((j == 0) && (r < Rmin)) flipped = TRUE; 
+	if ((j == 0) && (r > Rmax)) flipped = TRUE; 
+	while (flipped && (r < Rmid)) r+= 360.0;
+	while (flipped && (r > Rmid)) r-= 360.0;
+	status = RD_to_XY (&X, &Y, r, d, &graph[0].coords);
+
+	if (!status) continue;
+	if (X < graph[0].xmin) continue;
+	if (X > graph[0].xmax) continue;
+	if (Y < graph[0].ymin) continue;
+	if (Y > graph[0].ymax) continue;
+	subset[n] = i;
+	n++;
+	if (n > npts - 1) {
+	  npts += 200;
+	  REALLOCATE (subset, int, npts);
 	}
-	status |= RD_to_XY (&X[2*j], &Y[2*j], r, d, &graph[0].coords);
-	if (j > 0) {
-	  X[2*j - 1] = X[2*j];
-	  Y[2*j - 1] = Y[2*j];
-	}
+	break;
       }
-      X[7] = X[0];
-      Y[7] = Y[0];
-      InPic = FALSE;
-      for (j = 0; status && (j < 8); j+=2) {
-	if ((X[j] >= graph[0].xmin) && 
-	    (X[j] <= graph[0].xmax) && 
-	    (Y[j] >= graph[0].ymin) && 
-	    (Y[j] <= graph[0].ymax))
-	  InPic = TRUE;
-      }
-    }
-    if (InPic && status) {
-      subset[n] = i;
-      n++;
-      if (n > npts - 1) {
-	npts += 200;
-	REALLOCATE (subset, int, npts);
-      }
+      /** we miss any images which surround the region.  we are also
+	  missing the DIS images for which the corners don't touch
+	  the region, but which are needed for WRP images with
+	  corners touching the region **/
     }
   }
Index: /trunk/Ohana/src/opihi/dvo/LoadImages.c
===================================================================
--- /trunk/Ohana/src/opihi/dvo/LoadImages.c	(revision 3392)
+++ /trunk/Ohana/src/opihi/dvo/LoadImages.c	(revision 3393)
@@ -11,9 +11,18 @@
   VarConfig ("IMAGE_CATALOG", "%s", filename);
 
+  NIMAGE = 0;
+  Nimage = 0;
+  image  = NULL;
+
   /* check if image datafile exists, get header */
   if (!fits_read_header (filename, &header)) {
     fprintf (stderr, "No available image catalog %s (1)\n", filename);
-    *Nimage = 0;
-    return ((Image *) NULL);
+    return (NULL);
+  }
+
+  fits_scan (&header, "NIMAGES", "%d", 1, &NIMAGE);
+  if (NIMAGE == 0) {
+    fprintf (stderr, "no images found\n");
+    return (NULL);
   }
 
@@ -23,15 +32,5 @@
     fprintf (stderr, "No available image catalog: %s (2)\n", filename);
     free (header.buffer);
-    *Nimage = 0;
-    return ((Image *) NULL);
-  }
-
-  NIMAGE = 0;
-  fits_scan (&header, "NIMAGES", "%d", 1, &NIMAGE);
-  if (NIMAGE == 0) {
-    fprintf (stderr, "no images found\n");
-    *Nimage = NIMAGE;
-    fclose (f);
-    return ((Image *) NULL);
+    return (NULL);
   }
 
@@ -46,6 +45,3 @@
   *Nimage = nimage;
   return (image);
-
-
 }
-  
Index: /trunk/Ohana/src/opihi/dvo/avextract.c
===================================================================
--- /trunk/Ohana/src/opihi/dvo/avextract.c	(revision 3392)
+++ /trunk/Ohana/src/opihi/dvo/avextract.c	(revision 3393)
@@ -62,5 +62,5 @@
   if ((vec = SelectVector (argv[2], ANYVECTOR, TRUE)) == NULL) goto escape;
 
-  /* need valid code for some params: */
+  /* Need valid code for some params: */
   switch (param) {
     case AVE_MAG:
@@ -84,4 +84,5 @@
     regions = find_regions (graphsky.coords.crval1, graphsky.coords.crval2, Radius, &Nregions);
     if (!SetImageSelection (param, &graphsky, TRUE)) goto escape;
+    /* this is not really used by avextract -- no parameters require images */
   } else {
     Nregions = 1;
Index: /trunk/Ohana/src/opihi/dvo/dvomisc.c
===================================================================
--- /trunk/Ohana/src/opihi/dvo/dvomisc.c	(revision 3392)
+++ /trunk/Ohana/src/opihi/dvo/dvomisc.c	(revision 3393)
@@ -70,4 +70,5 @@
 }
 
+/** this does not seem to be used at the moment */
 /* values are ave[i].R, ave[i].D, ave[i].M */
 void sort_images (Image *image, int N) {
Index: /trunk/Ohana/src/opihi/dvo/gimages.c
===================================================================
--- /trunk/Ohana/src/opihi/dvo/gimages.c	(revision 3392)
+++ /trunk/Ohana/src/opihi/dvo/gimages.c	(revision 3393)
@@ -71,19 +71,11 @@
   if (!str_to_radec (&Ra, &Dec, argv[1], argv[2])) return (FALSE);
 
-  image = LoadImages (&Nimage);
-  if (Nimage == 0) {
-    return (FALSE);
-  }
+  if ((image = LoadImages (&Nimage)) == NULL) return (FALSE);
   image_subset (image, Nimage, &subset, &Nsubset, (Graphdata *) NULL, FALSE, tzero, trange, TimeSelect);
+  BuildChipMatch (image, Nimage);
 
   Nfound = 0;
   for (j = 0; j < Nsubset; j++) {
     i = subset[j];
-    status = RD_to_XY (&X, &Y, Ra, Dec, &image[i].coords);
-    if (!status || (X < 0) || (X > image[i].NX) || (Y < 0) || (Y > image[i].NY)) continue;
-    XY_to_RD (&ra, &dec, 0.5*image[i].NX, 0.5*image[i].NY, &image[i].coords);
-    Mcal = applyMcal (&image[i], 2048.0, 2048.0);
-    /* t = TimeValue (image[i].tzero, TimeReference, TimeFormat); */
-    date = sec_to_date (image[i].tzero);
     if (PhotCodeSelect) {
       if ((code[0].type == PHOT_REF) || (code[0].type == PHOT_DEP)) {
@@ -94,8 +86,21 @@
       } 
     }      
+    if (!FindMosaicForImage (image, Nimage, i)) continue;
+
+    status = RD_to_XY (&X, &Y, Ra, Dec, &image[i].coords);
+    if (!status) continue;
+    if (X < 0) continue;
+    if (Y < 0) continue;
+    if (X > image[i].NX) continue;
+    if (Y > image[i].NY) continue;
+
+    Mcal = applyMcal (&image[i], 2048.0, 2048.0);
+    date = sec_to_date (image[i].tzero);
+
     if (PixelCoords) {
       fprintf (GetOutfile(), "%3d %s %6.1f %6.1f %20s %5d %2d %4.2f %6.3f %5.3f %5.3f %4x\n", 
 	       Nfound, image[i].name, X, Y, date, image[i].nstar, image[i].source, 0.001*image[i].secz, 0.001*Mcal, 0.001*image[i].dMcal, image[i].exptime, image[i].code);
     } else {
+      XY_to_RD (&ra, &dec, 0.5*image[i].NX, 0.5*image[i].NY, &image[i].coords);
       fprintf (GetOutfile(), "%3d %s %8.4f %8.4f %20s %5d %2d %4.2f %6.3f %5.3f %5.3f %4x\n", 
 	       Nfound, image[i].name, ra, dec, date, image[i].nstar, image[i].source, 0.001*image[i].secz, 0.001*Mcal, 0.001*image[i].dMcal, image[i].exptime, image[i].code);
Index: /trunk/Ohana/src/opihi/dvo/images.c
===================================================================
--- /trunk/Ohana/src/opihi/dvo/images.c	(revision 3392)
+++ /trunk/Ohana/src/opihi/dvo/images.c	(revision 3393)
@@ -68,8 +68,6 @@
   }
   
-  image = LoadImages (&Nimage);
-  if (Nimage == 0) {
-    return (TRUE);
-  }
+  if ((image = LoadImages (&Nimage)) == NULL) return (FALSE);
+  BuildChipMatch (image, Nimage);
 
   Rmin = graphmode.coords.crval1 - 180.0;
@@ -85,4 +83,6 @@
     if (ByName && strcmp (name, image[i].name)) continue;
     if (TimeSelect && ((image[i].tzero < tzero) || (image[i].tzero+image[i].trate*image[i].NY > tzero + trange))) continue;
+    if (!FindMosaicForImage (image, Nimage, i)) continue;
+
     /* project this image to screen display coords */
     x[0] = 0;           y[0] = 0;
Index: /trunk/Ohana/src/opihi/dvo/imdata.c
===================================================================
--- /trunk/Ohana/src/opihi/dvo/imdata.c	(revision 3392)
+++ /trunk/Ohana/src/opihi/dvo/imdata.c	(revision 3393)
@@ -4,5 +4,5 @@
 int imdata (int argc, char **argv) {
   
-  int i, j, k;
+  int i, j, k, I;
   int Nimage, N, NPTS, skip, mode, TimeSelect;
   int n, Nregion, Ntregion, TimeFormat;
@@ -74,9 +74,7 @@
   if ((vec = SelectVector (argv[1], ANYVECTOR, TRUE)) == NULL) return (FALSE);
 
-  image = LoadImages (&Nimage);
-  if (Nimage == 0) {
-    return (FALSE);
-  }
+  if ((image = LoadImages (&Nimage)) == NULL) return (FALSE);
   image_subset (image, Nimage, &subset, &Nsubset, (Graphdata *) NULL, FALSE, tzero, trange, TimeSelect);
+  BuildChipMatch (image, Nimage);
   GetTimeFormat (&TimeReference, &TimeFormat);
 
@@ -85,5 +83,7 @@
   ALLOCATE (region, GSCRegion, 1);
   for (i = 0; i < Nsubset; i++) {
-    tregion = get_regions (&image[subset[i]], &Ntregion);
+    I = subset[i];
+    if (!FindMosaicForImage (image, Nimage, I)) continue;
+    tregion = get_regions (&image[I], &Ntregion);
     REALLOCATE (region, GSCRegion, MAX (1, Nregion + Ntregion));
     for (j = 0; j < Ntregion; j++) {
Index: /trunk/Ohana/src/opihi/dvo/imdense.c
===================================================================
--- /trunk/Ohana/src/opihi/dvo/imdense.c	(revision 3392)
+++ /trunk/Ohana/src/opihi/dvo/imdense.c	(revision 3393)
@@ -20,8 +20,6 @@
   if (!GetGraph (&graphmode, NULL, &Ngraph)) return (FALSE);
 
-  image = LoadImages (&Nimage);
-  if (Nimage == 0) {
-    return (FALSE);
-  }
+  if ((image = LoadImages (&Nimage)) == NULL) return (FALSE);
+  BuildChipMatch (image, Nimage);
 
   Rmin = graphmode.coords.crval1 - 182.0;
@@ -42,4 +40,5 @@
     /* project this image to screen display coords */
     status = FALSE;
+    if (!FindMosaicForImage (image, Nimage, i)) continue;
     XY_to_RD (&r, &d, x, y, &image[i].coords);
     while (r < Rmin) r += 360.0; 
Index: /trunk/Ohana/src/opihi/dvo/imextract.c
===================================================================
--- /trunk/Ohana/src/opihi/dvo/imextract.c	(revision 3392)
+++ /trunk/Ohana/src/opihi/dvo/imextract.c	(revision 3393)
@@ -97,9 +97,7 @@
   if ((vec = SelectVector (argv[1], ANYVECTOR, TRUE)) == NULL) return (FALSE);
 
-  image = LoadImages (&Nimage);
-  if (Nimage == 0) {
-    return (FALSE);
-  }
+  if ((image = LoadImages (&Nimage)) == NULL) return (FALSE);
   image_subset (image, Nimage, &subset, &Nsubset, &graphsky, RegionSelect, tzero, trange, TimeSelect);
+  if ((mode == RA) || (mode == DEC)) BuildChipMatch (image, Nimage);
 
   GetTimeFormat (&TimeReference, &TimeFormat);
@@ -123,4 +121,5 @@
     switch (mode) {
     case (RA):
+      if (!FindMosaicForImage (image, Nimage, j)) continue;
       x = 0.5*image[j].NX;
       y = 0.5*image[j].NY;
@@ -129,4 +128,5 @@
       break;
     case (DEC):
+      if (!FindMosaicForImage (image, Nimage, j)) continue;
       x = 0.5*image[j].NX;
       y = 0.5*image[j].NY;
Index: /trunk/Ohana/src/opihi/dvo/imlist.c
===================================================================
--- /trunk/Ohana/src/opihi/dvo/imlist.c	(revision 3392)
+++ /trunk/Ohana/src/opihi/dvo/imlist.c	(revision 3393)
@@ -64,9 +64,7 @@
   }
   
-  image = LoadImages (&Nimage);
-  if (Nimage == 0) {
-    return (FALSE);
-  }
+  if ((image = LoadImages (&Nimage)) == NULL) return (FALSE);
   image_subset (image, Nimage, &subset, &Nsubset, &graphmode, RegionSelect, tzero, trange, TimeSelect);
+  BuildChipMatch (image, Nimage);
 
   GetTimeFormat (&TimeReference, &TimeFormat);
@@ -82,4 +80,5 @@
       }
     }
+    if (!FindMosaicForImage (image, Nimage, i)) continue;
     t = TimeValue (image[i].tzero, TimeReference, TimeFormat);
     XY_to_RD (&r, &d, 0.5*image[i].NX, 0.5*image[i].NY, &image[i].coords);
Index: /trunk/Ohana/src/opihi/dvo/imphot.c
===================================================================
--- /trunk/Ohana/src/opihi/dvo/imphot.c	(revision 3392)
+++ /trunk/Ohana/src/opihi/dvo/imphot.c	(revision 3393)
@@ -51,8 +51,5 @@
   fprintf (stderr, "searching in range %ds - %ds (%f seconds)\n", (int)tzero, (int)(tzero + trange), trange);
   
-  image = LoadImages (&Nimage);
-  if (Nimage == 0) {
-    return (FALSE);
-  }
+  if ((image = LoadImages (&Nimage)) == NULL) return (FALSE);
   image_subset (image, Nimage, &subset, &Nsubset, (Graphdata *) NULL, FALSE, tzero, trange, TRUE);
 
Index: /trunk/Ohana/src/opihi/dvo/imstats.c
===================================================================
--- /trunk/Ohana/src/opihi/dvo/imstats.c	(revision 3392)
+++ /trunk/Ohana/src/opihi/dvo/imstats.c	(revision 3393)
@@ -35,8 +35,6 @@
   }
 
-  image = LoadImages (&Nimage);
-  if (Nimage == 0) {
-    return (FALSE);
-  }
+  if ((image = LoadImages (&Nimage)) == NULL) return (FALSE);
+  BuildChipMatch (image, Nimage);
 
   /* assign vector values */
@@ -52,4 +50,5 @@
     else 
       Yvec.elements[i] = 0.001*image[i].dMcal;
+    if (!FindMosaicForImage (image, Nimage, i)) continue;
     XY_to_RD (&r, &d, 0.5*image[i].NX, 0.5*image[i].NY, &image[i].coords);
     fprintf (stderr, "%d %8.4f %8.4f %10d %6d  %5.3f %6.3f %6.3f\n", 
Index: /trunk/Ohana/src/opihi/dvo/photometry.c
===================================================================
--- /trunk/Ohana/src/opihi/dvo/photometry.c	(revision 3392)
+++ /trunk/Ohana/src/opihi/dvo/photometry.c	(revision 3393)
@@ -268,10 +268,7 @@
   }
 
-  image = LoadImages (&Nimage);
-  if (Nimage == 0) {
-    return (FALSE);
-  }
+  if ((image = LoadImages (&Nimage)) == NULL) return (FALSE);
+  BuildChipMatch (image, Nimage);
   image_subset (image, Nimage, &subset, &Nsubset, graphsky, RegionSelect, tzero, (double) tend - tzero, TimeSelect);
-
   sort_image_subset (image, subset, Nsubset);
   return (TRUE);
@@ -282,4 +279,6 @@
   if (image != NULL) free (image);
   if (subset != NULL) free (subset);
+  image = NULL;
+  subset = NULL;
   return;
 }
@@ -487,4 +486,5 @@
 
     /* assign value */
+    value = 0;
     switch (param) {
     case MEAS_MAG: /* magnitudes are already determined above */
@@ -531,8 +531,6 @@
       dec = average[0].D - measure[i].dD / 360000.0;
       m = match_image_subset (image, subset, Nsubset, measure[i].t, measure[i].source);
-      if (m == -1) {
-	value = 0.0;
-	break;
-      }
+      if (m == -1) break;
+      if (!FindMosaicForImage (image, Nimage, m)) break;
       RD_to_XY (&x, &y, ra, dec, &image[m].coords);
       value = x;
@@ -542,8 +540,6 @@
       dec = average[0].D - measure[i].dD / 360000.0;
       m = match_image_subset (image, subset, Nsubset, measure[i].t, measure[i].source);
-      if (m == -1) {
-	value = 0.0;
-	break;
-      }
+      if (m == -1) break;
+      if (!FindMosaicForImage (image, Nimage, m)) break;
       RD_to_XY (&x, &y, ra, dec, &image[m].coords);
       value = y;
@@ -553,8 +549,8 @@
       dec = average[0].D - measure[i].dD / 360000.0;
       m = match_image_subset (image, subset, Nsubset, measure[i].t, measure[i].source);
-      if (m == -1) {
-	value = 0.0;
-	break;
-      }
+      if (m == -1) break;
+      /* this should use the mosaic assoicated with the image */
+      /* the solution here depends on image.coords having crref1,2 set to match the boresite center */
+      /* XXX set the mosaic.coords to have a unity WRP transformation in which x,y = ra,dec */
       mosaic.crval1 = image[m].coords.crval1;
       mosaic.crval2 = image[m].coords.crval2;
@@ -566,8 +562,6 @@
       dec = average[0].D - measure[i].dD / 360000.0;
       m = match_image_subset (image, subset, Nsubset, measure[i].t, measure[i].source);
-      if (m == -1) {
-	value = 0.0;
-	break;
-      }
+      if (m == -1) break;
+      /* same comments as above */
       mosaic.crval1 = image[m].coords.crval1;
       mosaic.crval2 = image[m].coords.crval2;
@@ -1008,9 +1002,8 @@
       TESTMEASURE (measure[j]);
       SETMAG(M2, measure[j], mode[1]);
-      if (use_first) 
-	n = i;
-      else 
-	n = j;
+      n = (use_first) ? i : j;
+
       /* assign value */
+      value = 0;
       switch (param) {
       case MEAS_MAG: /* magnitudes are already determined above */
@@ -1057,8 +1050,6 @@
       dec = average[0].D - measure[n].dD / 360000.0;
       m = match_image_subset (image, subset, Nsubset, measure[n].t, measure[n].source);
-      if (m == -1) {
-	value = 0.0;
-	break;
-      }
+      if (m == -1) break;
+      if (!FindMosaicForImage (image, Nimage, m)) break;
       RD_to_XY (&x, &y, ra, dec, &image[m].coords);
       value = x;
@@ -1068,8 +1059,6 @@
       dec = average[0].D - measure[n].dD / 360000.0;
       m = match_image_subset (image, subset, Nsubset, measure[n].t, measure[n].source);
-      if (m == -1) {
-	value = 0.0;
-	break;
-      }
+      if (m == -1) break;
+      if (!FindMosaicForImage (image, Nimage, m)) break;
       RD_to_XY (&x, &y, ra, dec, &image[m].coords);
       value = y;
@@ -1079,8 +1068,5 @@
       dec = average[0].D - measure[n].dD / 360000.0;
       m = match_image_subset (image, subset, Nsubset, measure[n].t, measure[n].source);
-      if (m == -1) {
-	value = 0.0;
-	break;
-      }
+      if (m == -1) break;
       mosaic.crval1 = image[m].coords.crval1;
       mosaic.crval2 = image[m].coords.crval2;
@@ -1092,8 +1078,5 @@
       dec = average[0].D - measure[n].dD / 360000.0;
       m = match_image_subset (image, subset, Nsubset, measure[n].t, measure[n].source);
-      if (m == -1) {
-	value = 0.0;
-	break;
-      }
+      if (m == -1) break;
       mosaic.crval1 = image[m].coords.crval1;
       mosaic.crval2 = image[m].coords.crval2;
Index: /trunk/Ohana/src/opihi/dvo/subpix.c
===================================================================
--- /trunk/Ohana/src/opihi/dvo/subpix.c	(revision 3392)
+++ /trunk/Ohana/src/opihi/dvo/subpix.c	(revision 3393)
@@ -120,6 +120,9 @@
 
   /* load all images, extract those touching Ra, Dec */
-  image = LoadImages (&Nimage);
+  if ((image = LoadImages (&Nimage)) == NULL) return (FALSE);
+  BuildChipMatch (image, Nimage);
+
   for (i = 0; i < Nimage; i++) {
+    if (!FindMosaicForImage (image, Nimage, i)) continue;
     status = RD_to_XY (&X, &Y, Ra, Dec, &image[i].coords);
     if (!status || (X < 0) || (X > image[i].NX) || (Y < 0) || (Y > image[i].NY)) continue;
@@ -137,4 +140,5 @@
   for (i = 0; i < Nsub; i++) {
     I = index[i];
+    if (!FindMosaicForImage (image, Nimage, I)) continue;
     Timage = image[I].tzero;
     for (j = 0; j < Nmeasure; j++) {
