Index: trunk/Ohana/src/opihi/dvo/ImageOps.c
===================================================================
--- trunk/Ohana/src/opihi/dvo/ImageOps.c	(revision 7680)
+++ trunk/Ohana/src/opihi/dvo/ImageOps.c	(revision 14401)
@@ -2,5 +2,5 @@
 
 void image_subset (Image *image, int Nimage, int **Subset, int *Nsubset,
-		   Graphdata *graph, int RegionSelect, 
+		   SkyRegionSelection *selection, 
 		   unsigned long int tzero, double trange, int TimeSelect) 
 {
@@ -10,11 +10,25 @@
   int N, n, npts;
   double r, d, X, Y, x[4], y[4], Rmin, Rmax, Rmid;
+  Graphdata graph;
+  SkyRegion patch;
 
   Rmin = Rmax = Rmid = 0;
-  if (RegionSelect) {
-    Rmin = graph[0].coords.crval1 - 182.0;
-    Rmax = graph[0].coords.crval1 + 182.0;
+
+  if (selection->useDisplay) {
+    if (!GetGraphData (&graph, NULL, NULL)) {
+      gprint (GP_ERR, "region display not available\n");
+      return;
+    }
+    Rmin = graph.coords.crval1 - 182.0;
+    Rmax = graph.coords.crval1 + 182.0;
     Rmid = 0.5*(Rmin + Rmax);
     BuildChipMatch (image, Nimage);
+  }
+
+  if (selection->useSkyregion) {
+    get_skyregion (&patch.Rmin, &patch.Rmax, &patch.Dmin, &patch.Dmax);
+    Rmin = patch.Rmin - 182.0;
+    Rmax = patch.Rmax + 182.0;
+    Rmid = 0.5*(Rmin + Rmax);
   }
 
@@ -29,5 +43,5 @@
   for (i = 0; i < Nimage; i++) {
     if (TimeSelect && ((image[i].tzero < tzero) || (image[i].tzero+image[i].trate*image[i].NY > tzero + trange))) continue;
-    if (RegionSelect) {
+    if (selection->useDisplay) {
       if (!FindMosaicForImage (image, Nimage, i)) continue;
       /* project this image to screen display coords */
@@ -44,10 +58,37 @@
 	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);
+	status = RD_to_XY (&X, &Y, r, d, &graph.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;
+	if (X < graph.xmin) continue;
+	if (X > graph.xmax) continue;
+	if (Y < graph.ymin) continue;
+	if (Y > graph.ymax) continue;
+	goto in_region;
+	/** 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 **/
+      }
+      continue;
+    }
+    if (selection->useSkyregion) {
+      if (!FindMosaicForImage (image, Nimage, i)) continue;
+      /* project this image to screen display coords */
+      x[0] = 0;           y[0] = 0;
+      x[1] = image[i].NX; y[1] = 0;
+      x[2] = image[i].NX; y[2] = image[i].NY;
+      x[3] = 0;           y[3] = image[i].NY;
+      InPic = flipped = FALSE;
+      for (j = 0; j < 4; j++) {
+	XY_to_RD (&r, &d, x[j], y[j], &image[i].coords);
+	/* 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;
+	if (r < patch.Rmin) continue;
+	if (r > patch.Rmax) continue;
+	if (d < patch.Dmin) continue;
+	if (d > patch.Dmax) continue;
 	goto in_region;
 	/** we miss any images which surround the region.  we are also
