Index: trunk/Ohana/src/addstar/Makefile
===================================================================
--- trunk/Ohana/src/addstar/Makefile	(revision 3376)
+++ trunk/Ohana/src/addstar/Makefile	(revision 3389)
@@ -1,2 +1,4 @@
+#install:
+#	@echo "addstar under re-construction"
 default: addstar
 help:
@@ -110,5 +112,5 @@
 # utilities #################################################
 
-install:
+# install:
 	for i in $(INSTALL); do make $$i.install; done
 
Index: trunk/Ohana/src/addstar/include/addstar.h
===================================================================
--- trunk/Ohana/src/addstar/include/addstar.h	(revision 3376)
+++ trunk/Ohana/src/addstar/include/addstar.h	(revision 3389)
@@ -62,4 +62,5 @@
 PhotCode *thiscode;
 GSCRegion UserPatch;
+Coords *MOSAIC;
 
 char DateKeyword[64];
Index: trunk/Ohana/src/addstar/src/args.c
===================================================================
--- trunk/Ohana/src/addstar/src/args.c	(revision 3376)
+++ trunk/Ohana/src/addstar/src/args.c	(revision 3389)
@@ -14,5 +14,5 @@
 int args (int argc, char **argv) {
   
-  int i, N;
+  int N;
 
   /* check for help request */
@@ -61,11 +61,33 @@
   /* provide a time for dataset */
   TIMEREF = 0; 
-  if ((i = get_argument (argc, argv, "-time"))) {
-    remove_argument (i, &argc, argv);
-    if (!str_to_time (argv[i], &TIMEREF)) { 
+  if ((N = get_argument (argc, argv, "-time"))) {
+    remove_argument (N, &argc, argv);
+    if (!str_to_time (argv[N], &TIMEREF)) { 
       fprintf (stderr, "syntax error in time\n");
       exit (1);
     }
-    remove_argument (i, &argc, argv);
+    remove_argument (N, &argc, argv);
+  }
+  /* provide a mosaic for distortion */
+  MOSAIC = NULL;
+  if ((N = get_argument (argc, argv, "-mosaic"))) {
+    Header header;
+    ALLOCATE (MOSAIC, Coords, 1);
+
+    remove_argument (N, &argc, argv);
+    if (!fits_read_header (argv[N], &header)) {
+      fprintf (stderr, "ERROR: can't read header for mosaic %s\n", argv[N]);
+      exit (1);
+    }
+    if (!GetCoords (MOSAIC, &header)) {
+      fprintf (stderr, "ERROR: no astrometric solution in header\n");
+      exit (1);
+    }
+    if (strcmp(&MOSAIC[0].ctype[4], "-DIS")) {
+      fprintf (stderr, "ERROR: not a mosaic distortion header\n");
+      exit (1);
+    }
+    remove_argument (N, &argc, argv);
+    fits_free_header (&header);
   }
   
@@ -73,7 +95,7 @@
   /* only add to existing objects */
   ONLY_MATCH = FALSE;
-  if ((i = get_argument (argc, argv, "-only-match"))) {
+  if ((N = get_argument (argc, argv, "-only-match"))) {
     ONLY_MATCH = TRUE;
-    remove_argument (i, &argc, argv);
+    remove_argument (N, &argc, argv);
   }
   /* don't add missed pts to Missed table (image only) */
@@ -85,7 +107,7 @@
   /* replace measurement, don't duplicate (ref/cat only) */
   REPLACE = FALSE;
-  if ((i = get_argument (argc, argv, "-replace"))) {
+  if ((N = get_argument (argc, argv, "-replace"))) {
     REPLACE = TRUE;
-    remove_argument (i, &argc, argv);
+    remove_argument (N, &argc, argv);
   }
   /* only add image potion to image table */
@@ -133,5 +155,5 @@
     fprintf (stderr, "USAGE: addstar -ref (filename)\n");
     fprintf (stderr, "  cur: %s ", argv[0]);
-    for (i = 0; i < argc; i++) { fprintf (stderr, "%s ", argv[i]); }
+    for (N = 0; N < argc; N++) { fprintf (stderr, "%s ", argv[N]); }
     fprintf (stderr, "\n");  
     exit (2);
Index: trunk/Ohana/src/addstar/src/find_matches.c
===================================================================
--- trunk/Ohana/src/addstar/src/find_matches.c	(revision 3376)
+++ trunk/Ohana/src/addstar/src/find_matches.c	(revision 3389)
@@ -49,12 +49,22 @@
   REALLOCATE (catalog[0].missing, Missing, NMISS);
   
-  /* project onto rectilinear grid with 1 arcsec pixels, sort by X */
-  /* reference for coords is this image */
-  tcoords = image[0].coords;
-  tcoords.cdelt1 = tcoords.cdelt2 = 1.0 / 3600.0;
-  tcoords.pc1_1 = tcoords.pc2_2 = 1.0;
-  tcoords.pc1_2 = tcoords.pc2_1 = 0.0;
-  strcpy (tcoords.ctype, "RA---TAN");
-  
+  /* project onto rectilinear grid with 1 arcsec pixels */
+  /* we keep the original crpix1,2 and crref1,2 */
+  /* for mosaic astrometry, the grid should be w.r.t. the tangent-plane, not chip coords */
+  if (!strcmp (&image[0].coords.ctype[4], "-WRP")) {
+    tcoords = MOSAIC[0];
+    tcoords.cdelt1 = tcoords.cdelt2 = 1.0 / 3600.0;
+    tcoords.pc1_1 = tcoords.pc2_2 = 1.0;
+    tcoords.pc1_2 = tcoords.pc2_1 = 0.0;
+    tcoords.Npolyterms = 1;
+    strcpy (tcoords.ctype, "RA---TAN");
+  } else {
+    tcoords = image[0].coords;
+    tcoords.cdelt1 = tcoords.cdelt2 = 1.0 / 3600.0;
+    tcoords.pc1_1 = tcoords.pc2_2 = 1.0;
+    tcoords.pc1_2 = tcoords.pc2_1 = 0.0;
+    strcpy (tcoords.ctype, "RA---TAN");
+  }
+
   for (i = 0; i < Nstars; i++) {
     fRD_to_XY (&X1[i], &Y1[i], stars[i].R, stars[i].D, &tcoords);
@@ -134,5 +144,4 @@
       /* calculate accurate per-star airmass */
       secz = airmass (image[0].secz, stars[N].R, stars[N].D, SiderealTime, Latitude);
-
       
       /** add measurements for this star **/
@@ -162,7 +171,7 @@
       if (*Mval == NO_MAG) *Mval = Mrel;
 
+      /** this is a little tricky: the new measures are not in the measure sequence
+	  this call can run into the next star if we have had two measures found at this location. **/
       if (CALIBRATE) {
-	/** this is a little tricky: the new measures are not in the measure sequence
-	    this call can run into the next star if we have had two measures found at this location. **/
 	AddToCalibration (&catalog[0].average[n], &catalog[0].measure[m], &catalog[0].measure[Nmeas], N);
       }
@@ -202,4 +211,5 @@
 
   /* add reference for undetected catalog stars */
+  if (!strcmp (&image[0].coords.ctype[4], "-WRP")) RegisterMosaic (MOSAIC);
   for (j = 0; (j < Nave) && !SKIP_MISSED; j++) {
     n = N2[j];
@@ -287,4 +297,5 @@
     /** now add references from all previous non-detection observations of this spot on the sky */
     for (j = 0; (j < Noverlap) && !SKIP_MISSED; j++) {
+      if (!FindMosaicForImage (overlap, Noverlap, j)) continue;
       if (!in_image (catalog[0].average[Nave].R, catalog[0].average[Nave].D, &overlap[j])) continue;
       if (catalog[0].average[Nave].Nn < 1) {
Index: trunk/Ohana/src/addstar/src/gimages.c
===================================================================
--- trunk/Ohana/src/addstar/src/gimages.c	(revision 3376)
+++ trunk/Ohana/src/addstar/src/gimages.c	(revision 3389)
@@ -38,9 +38,21 @@
   /* project onto rectilinear grid with 1 arcsec pixels */
   /* we keep the original crpix1,2 and crref1,2 */
-  tcoords = image[0].coords;
-  tcoords.cdelt1 = tcoords.cdelt2 = 1.0 / 3600.0;
-  tcoords.pc1_1 = tcoords.pc2_2 = 1.0;
-  tcoords.pc1_2 = tcoords.pc2_1 = 0.0;
-  strcpy (tcoords.ctype, "RA---TAN");
+  /* for mosaic astrometry, the grid should be w.r.t. the tangent-plane, not chip coords */
+  if (!strcmp (&image[0].coords.ctype[4], "-WRP")) {
+    tcoords = MOSAIC[0];
+    tcoords.cdelt1 = tcoords.cdelt2 = 1.0 / 3600.0;
+    tcoords.pc1_1 = tcoords.pc2_2 = 1.0;
+    tcoords.pc1_2 = tcoords.pc2_1 = 0.0;
+    tcoords.Npolyterms = 1;
+    strcpy (tcoords.ctype, "RA---TAN");
+    RegisterMosaic (MOSAIC);
+    /* register so image->sky conversions below have correct mosaic */
+  } else {
+    tcoords = image[0].coords;
+    tcoords.cdelt1 = tcoords.cdelt2 = 1.0 / 3600.0;
+    tcoords.pc1_1 = tcoords.pc2_2 = 1.0;
+    tcoords.pc1_2 = tcoords.pc2_1 = 0.0;
+    strcpy (tcoords.ctype, "RA---TAN");
+  }
 
   /* define original corners */
@@ -51,4 +63,5 @@
   Xo[4] = 0;           Yo[4] = 0;  /* so we can make a loop easily */
   for (j = 0; j < 5; j++) {
+    /* XY-to_RD is two-level if ctype == WRP */
     XY_to_RD (&r, &d, Xo[j], Yo[j], &image[0].coords);
     RD_to_XY (&Xo[j], &Yo[j], r, d, &tcoords);
@@ -77,5 +90,6 @@
   } 
 
-  NTIMAGE = 100;
+  /* kind of dangerous, see note below */
+  NTIMAGE = MAX (MIN (Nimage, 1000), 1);
   ALLOCATE (timage, Image, NTIMAGE);
 
@@ -87,5 +101,15 @@
   for (Ntimage = 0; Ntimage < Nimage; Ntimage += ntimage) {
     ntimage = Fread (timage, sizeof(Image), NTIMAGE, f, "image");
+    if (ntimage == 0) Shutdown ("expected %d images, only found %d", Nimage, Ntimage);
+    BuildChipMatch (timage, ntimage);
+
     for (i = 0; i < ntimage; i++) {
+
+      /* if any of these images are WRP images, need to find matching DIS */
+      if (!FindMosaicForImage (timage, ntimage, i)) {
+	fprintf (stderr, "!");
+	continue;
+      }
+
       addtolist = FALSE;
       /* define image corners */
@@ -116,9 +140,7 @@
       }
     }
+  }
 
-    if (ntimage == 0) {
-      Shutdown ("expected %d images, only found %d", Nimage, Ntimage);
-    }
-  }
+  BuildChipMatch (pimage, npimage);
 
   if (VERBOSE) fprintf (stderr, "found %d overlapping images\n", npimage);
@@ -129,2 +151,9 @@
 }
   
+/* this function tries to find overlapping images with the current image.
+   it loads the image table in blocks (up to 1000 at a time) and looks
+   for overlaps within the set.  the search is not very robust. in 
+   addition, it needs to look for mosaic DIS images matching any WRP images.
+   if the WRP image is loaded near the beginning of a block, then the corresponding DIS 
+   may be in the previous block, and thus missed 
+*/
Index: trunk/Ohana/src/addstar/src/gregion_image.c
===================================================================
--- trunk/Ohana/src/addstar/src/gregion_image.c	(revision 3376)
+++ trunk/Ohana/src/addstar/src/gregion_image.c	(revision 3389)
@@ -12,4 +12,9 @@
   impatch.DEC[0] = +90;
   impatch.DEC[1] = -90;
+
+  if (!strcmp (&image[0].coords.ctype[4], "-WRP")) {
+    if (MOSAIC == NULL) Shutdown ("no mosaic for WRP image (use -mosaic)");
+    RegisterMosaic (MOSAIC);
+  }    
 
   for (x = 0; x <= 1; x++) {
Index: trunk/Ohana/src/addstar/src/gstars.c
===================================================================
--- trunk/Ohana/src/addstar/src/gstars.c	(revision 3376)
+++ trunk/Ohana/src/addstar/src/gstars.c	(revision 3389)
@@ -52,4 +52,11 @@
     exit (1);
   }
+  if (!strcmp (&image[0].coords.ctype[4], "-WRP")) {
+    if (MOSAIC == NULL) {
+      fprintf (stderr, "ERROR: no mosaic for WRP image (use -mosaic)\n");
+      exit (1);
+    }
+    RegisterMosaic (MOSAIC);
+  }    
     
   /* get ST (used for airmass calculation) */
