Index: /trunk/Ohana/src/opihi/cmd.data/minterpolate.c
===================================================================
--- /trunk/Ohana/src/opihi/cmd.data/minterpolate.c	(revision 3673)
+++ /trunk/Ohana/src/opihi/cmd.data/minterpolate.c	(revision 3674)
@@ -3,12 +3,21 @@
 int minterp (int argc, char **argv) {
   
-  int i, j, status, nx, ny, Nx, Ny;
+  int i, j, status, nx, ny, Nx, Ny, N, Extrapolate;
+  int ic, jc, dx, dy, Npix;
   char temp[1024];
   double scale, scale2, dX, dY;
   float *V00, *V01, *V10, *V11, *Vout, dV1, dV2, dV3;
+  float *buf, I, J, x, y, xs, xe, ys, ye;
   Buffer *in, *out;
 
+  /* choose the appropriate graphing window */
+  Extrapolate = FALSE;
+  if ((N = get_argument (argc, argv, "-extrapolate"))) {
+    remove_argument (N, &argc, argv);
+    Extrapolate = TRUE;
+  }
+
   if (argc != 4) {
-    fprintf (stderr, "USAGE: minterpolate <from> <to> scale \n");
+    fprintf (stderr, "USAGE: minterpolate <from> <to> scale [-extrapolate]\n");
     return (FALSE);
   }
@@ -68,13 +77,11 @@
   dX = dY = scale;
 
-  {
+  buf = (float *)in[0].matrix.buffer;
+  Npix = 0;
 
-    int dx, dy, Npix;
-    float I, J, x, y, xs, xe, ys, ye;
-
-    Npix = 0;
+  if (Extrapolate) {
     for (j = 0; j < Ny - 1; j++) {
       for (i = 0; i < Nx - 1; i++) {
-	V00 = (float *)(in[0].matrix.buffer) + j*Nx + i;
+	V00 = buf + i + j*Nx;
 	V10 = V00 + 1;
 	V01 = V00 + Nx;
@@ -87,13 +94,53 @@
 	y = (j + 0.5) * scale;
 
-	xs = 0;
-	if (i == 0) { xs = -0.5*scale; }
-	xe = scale;
-	if (i == Nx - 2) { xe = 1.5*scale; }
+	xs = ys = 0;
+	xe = ye = scale;
 
-	ys = 0;
-	if (j == 0) { ys = -0.5*scale; }
-	ye = scale;
-	if (j == Ny - 2) { ye = 1.5*scale; }
+	if (i == 0)      { xs = -0.5*scale; }
+	if (i == Nx - 2) { xe =  1.5*scale; }
+
+	if (j == 0)      { ys = -0.5*scale; }
+	if (j == Ny - 2) { ye =  1.5*scale; }
+
+	for (J = ys; J < ye; J += 1.0) {
+	  dx = (x + xs);
+	  dy = (y + J);
+	  Vout = (float *)(out[0].matrix.buffer) + dy*nx + dx;
+	  for (I = xs; I < xe; I += 1.0, Vout++) {
+	    *Vout = dV1 * (I*J) + dV2 * J + dV3 * I + *V00;
+	    Npix ++;
+	  }
+	}
+      }
+    }
+  } else {
+     for (j = -1; j < Ny; j++) {
+      for (i = -1; i < Nx; i++) {
+	ic = MIN (MAX (i, 0), Nx-1);  /* we never actually reach Nx, Ny */
+	jc = MIN (MAX (j, 0), Ny-1);
+	V00 = buf + ic + jc*Nx;
+	V10 = V00 + 1;
+	V01 = V00 + Nx;
+	V11 = V01 + 1;
+
+	if ((i == -1) || (i == Nx - 1)) { V10 = V00; } else { V10 = V00 + 1; }
+	if ((j == -1) || (j == Ny - 1)) { V01 = V00; } else { V01 = V00 + Nx; }
+	if ((i == -1) || (i == Nx - 1)) { V11 = V01; } else { V11 = V01 + 1; }
+
+	dV1 = (*V11 + *V00 - *V01 - *V10) / scale2;
+	dV2 = (*V01 - *V00) / scale;
+	dV3 = (*V10 - *V00) / scale;
+
+	x = (i + 0.5) * scale;
+	y = (j + 0.5) * scale;
+
+	xs = ys = 0;
+	xe = ye = scale;
+
+	if (i == -1)     { xs = 0.5*scale; }
+	if (i == Nx - 1) { xe = 0.5*scale; }
+
+	if (j == -1)     { ys = 0.5*scale; }
+	if (j == Ny - 1) { ye = 0.5*scale; }
 
 	for (J = ys; J < ye; J += 1.0) {
@@ -109,7 +156,4 @@
     }
   }
-
   return (TRUE);
-
 }
-
