Index: trunk/Ohana/src/opihi/cmd.data/box.c
===================================================================
--- trunk/Ohana/src/opihi/cmd.data/box.c	(revision 5849)
+++ trunk/Ohana/src/opihi/cmd.data/box.c	(revision 5850)
@@ -4,37 +4,38 @@
   
   int i, N, Ngraph, Xgraph;
-  char Ticks[16], Axis[16], Labels[16];
   Graphdata graphmode;
   
-  strcpy (Ticks, "2222");
+  if (!GetGraph (&graphmode, &Xgraph, &Ngraph)) return (FALSE);
+
+  strcpy (graphmode.ticks, "2222");
   if ((N = get_argument (argc, argv, "-ticks"))) {
     remove_argument (N, &argc, argv);
-    strcpy (Ticks, argv[N]);
+    strcpy (graphmode.ticks, argv[N]);
     remove_argument (N, &argc, argv);
-    if (strlen (Ticks) != 4) { goto usage; }
-    for (i = 0; i < strlen (Ticks); i++) {
-      if ((Ticks[i] != '0') && (Ticks[i] != '1') && (Ticks[i] != '2')) { goto usage; }
+    if (strlen (graphmode.ticks) != 4) { goto usage; }
+    for (i = 0; i < strlen (graphmode.ticks); i++) {
+      if ((graphmode.ticks[i] != '0') && (graphmode.ticks[i] != '1') && (graphmode.ticks[i] != '2')) { goto usage; }
     }
   }
   
-  strcpy (Labels, "2222");
+  strcpy (graphmode.labels, "2222");
   if ((N = get_argument (argc, argv, "-labels"))) {
     remove_argument (N, &argc, argv);
-    strcpy (Labels, argv[N]);
+    strcpy (graphmode.labels, argv[N]);
     remove_argument (N, &argc, argv);
-    if (strlen (Labels) != 4) { goto usage; }
-    for (i = 0; i < strlen (Labels); i++) {
-      if ((Labels[i] != '0') && (Labels[i] != '1') && (Labels[i] != '2')) { goto usage; }
+    if (strlen (graphmode.labels) != 4) { goto usage; }
+    for (i = 0; i < strlen (graphmode.labels); i++) {
+      if ((graphmode.labels[i] != '0') && (graphmode.labels[i] != '1') && (graphmode.labels[i] != '2')) { goto usage; }
     }
   }
 
-  strcpy (Axis, "2222");
+  strcpy (graphmode.axis, "2222");
   if ((N = get_argument (argc, argv, "-axis"))) {
     remove_argument (N, &argc, argv);
-    strcpy (Axis, argv[N]);
+    strcpy (graphmode.axis, argv[N]);
     remove_argument (N, &argc, argv);
-    if (strlen (Axis) != 4) { goto usage; }
-    for (i = 0; i < strlen (Axis); i++) {
-      if ((Axis[i] != '0') && (Axis[i] != '1') && (Axis[i] != '2')) { goto usage; }
+    if (strlen (graphmode.axis) != 4) { goto usage; }
+    for (i = 0; i < strlen (graphmode.axis); i++) {
+      if ((graphmode.axis[i] != '0') && (graphmode.axis[i] != '1') && (graphmode.axis[i] != '2')) { goto usage; }
     }
   }
@@ -46,9 +47,8 @@
     remove_argument (N, &argc, argv);
   }
-  if (!GetGraph (&graphmode, &Xgraph, &Ngraph)) return (FALSE);
 
   if (argc != 1) goto usage;
 
-  KapaBox (Xgraph, &graphmode, Axis, Labels, Ticks);
+  KapaBox (Xgraph, &graphmode);
   return (TRUE);
       
@@ -73,6 +73,4 @@
    -labels 1100
    -ticks 1111
-   
-
 
    messages to kapa:
Index: trunk/Ohana/src/opihi/cmd.data/clear.c
===================================================================
--- trunk/Ohana/src/opihi/cmd.data/clear.c	(revision 5849)
+++ trunk/Ohana/src/opihi/cmd.data/clear.c	(revision 5850)
@@ -25,9 +25,5 @@
   }
 
-  if (ClearSection) {
-    SendGraphCommand (Xgraph, 4, "ERAS");
-  } else {
-    SendGraphCommand (Xgraph, 4, "ERSS");
-  }
+  KapaClear (Xgraph, ClearSection);
   return (TRUE);
 }
Index: trunk/Ohana/src/opihi/cmd.data/dot.c
===================================================================
--- trunk/Ohana/src/opihi/cmd.data/dot.c	(revision 5849)
+++ trunk/Ohana/src/opihi/cmd.data/dot.c	(revision 5850)
@@ -47,15 +47,8 @@
   if ((N = get_argument (argc, argv, "-c"))) {
     remove_argument (N, &argc, argv);
-    graphmode.color = GetColor (argv[N]);
+    graphmode.color = KapaColorByName (argv[N]);
     if (graphmode.color == -1) return (FALSE);
     remove_argument (N, &argc, argv);
   }
-  /* only -x 2 makes sense here
-  if ((N = get_argument (argc, argv, "-x"))) {
-    remove_argument (N, &argc, argv);
-    graphmode.style = atof(argv[N]);
-    remove_argument (N, &argc, argv);
-  }
-  */
   graphmode.style = 2;
 
Index: trunk/Ohana/src/opihi/cmd.data/fit2d.c
===================================================================
--- trunk/Ohana/src/opihi/cmd.data/fit2d.c	(revision 5849)
+++ trunk/Ohana/src/opihi/cmd.data/fit2d.c	(revision 5850)
@@ -160,5 +160,21 @@
     }
 
+    fprintf (stderr, "matrix:\n");
+    for (k = 0; k < wterm; k++) {
+	for (K = 0; K < wterm; K++) {
+	    fprintf (stderr, "%f ", c[k][K]);
+	}
+	fprintf (stderr, "\n");
+    }
+
     gaussj (c, wterm, b, 1);
+
+    fprintf (stderr, "inverse:\n");
+    for (k = 0; k < wterm; k++) {
+	for (K = 0; K < wterm; K++) {
+	    fprintf (stderr, "%f ", c[k][K]);
+	}
+	fprintf (stderr, "\n");
+    }
 
     /** test print **/
Index: trunk/Ohana/src/opihi/cmd.data/init.c
===================================================================
--- trunk/Ohana/src/opihi/cmd.data/init.c	(revision 5849)
+++ trunk/Ohana/src/opihi/cmd.data/init.c	(revision 5850)
@@ -144,4 +144,6 @@
   {"interpolate",  interpolate,	     "interpolate between vector pairs"},
   {"jpeg",         jpeg,	     "write text line on graph"},
+  {"png",          jpeg,	     "write text line on graph"},
+  {"ppm",          jpeg,	     "write text line on graph"},
   {"kern",         kern,	     "convolve with 3x3 kernel"},
   {"keyword",      keyword,	     "extract a FITS keyword from buffer header"},
Index: trunk/Ohana/src/opihi/cmd.data/interpolate.c
===================================================================
--- trunk/Ohana/src/opihi/cmd.data/interpolate.c	(revision 5849)
+++ trunk/Ohana/src/opihi/cmd.data/interpolate.c	(revision 5850)
@@ -43,5 +43,5 @@
 	yout[0].elements[i] = (dy/dx)*(xout[0].elements[i] - x0) + y0;
       }
-      if ((j == xin[0].Nelements - 2) && (xout[0].elements[i] > x1)) {
+      if ((j == xin[0].Nelements - 2) && (xout[0].elements[i] >= x1)) {
 	yout[0].elements[i] = (dy/dx)*(xout[0].elements[i] - x0) + y0;
       }
Index: trunk/Ohana/src/opihi/cmd.data/jpeg.c
===================================================================
--- trunk/Ohana/src/opihi/cmd.data/jpeg.c	(revision 5849)
+++ trunk/Ohana/src/opihi/cmd.data/jpeg.c	(revision 5850)
@@ -3,6 +3,6 @@
 int jpeg (int argc, char **argv) {
 
-  char filename[1024], buffer[20], type[16];
-  int N, Source, Nsource, IsImage;
+  char filename[1024];
+  int N, Source, Nsource, IsImage, IsPNG;
   
   if ((N = get_argument (argc, argv, "--help"))) {
@@ -12,8 +12,8 @@
 
   /* image type */
-  strcpy (type, "PNGF");
+  IsPNG = TRUE;
   if ((N = get_argument (argc, argv, "-ppm"))) {
     remove_argument (N, &argc, argv);
-    strcpy (type, "PPMF");
+    IsPNG = FALSE;
   }
 
@@ -45,17 +45,17 @@
     if (!GetImage (&Source, &Nsource)) return (FALSE);
     if (!filename[0]) strcpy (filename, "Ximage.jpg");
-    strcpy (type, "JPEG");
-    KiiJpeg (Source, filename);
+    KiiJPEG (Source, filename);
   } else {
     if (!GetGraph (NULL, &Source, &Nsource)) return (FALSE);
     if (!filename[0]) strcpy (filename, "Xgraph.png");
-    SendGraphCommand (Source, 4, type);
-    SendGraphCommand (Source, 16, "LEN: %11d", strlen(filename));
-    write (Source, filename, strlen(filename));
-    read (Source, buffer, 4);
+    if (IsPNG) {
+      KapaPNG (Source, filename);
+    } else {
+      KapaPPM (Source, filename);
+    }
   }
   return (TRUE);
 }
 
-/* jpeg -g converts graph to png
+/* jpeg converts graph to png or ppm
    jpeg converts image to jpeg */
Index: trunk/Ohana/src/opihi/cmd.data/labels.c
===================================================================
--- trunk/Ohana/src/opihi/cmd.data/labels.c	(revision 5849)
+++ trunk/Ohana/src/opihi/cmd.data/labels.c	(revision 5850)
@@ -36,12 +36,10 @@
     size = atof (argv[N]);
     remove_argument (N, &argc, argv);
-    SendGraphCommand (Xgraph, 4, "FONT");
-    SendGraphCommand (Xgraph, 16, "%s", name);
-    SendGraphCommand (Xgraph, 16, "%d", size);
+    KapaSetFont (Xgraph, name, size);
   } 
 
   if ((N = get_argument (argc, argv, "-x"))) {
     remove_argument (N, &argc, argv);
-    SendLabel (argv[N], Xgraph, 0);
+    KapaSendLabel (Xgraph, argv[N], 0);
     remove_argument (N, &argc, argv);
   }
@@ -49,5 +47,5 @@
   if ((N = get_argument (argc, argv, "-y"))) {
     remove_argument (N, &argc, argv);
-    SendLabel (argv[N], Xgraph, 1);
+    KapaSendLabel (Xgraph, argv[N], 1);
     remove_argument (N, &argc, argv);
   }
@@ -55,5 +53,5 @@
   if ((N = get_argument (argc, argv, "+x"))) {
     remove_argument (N, &argc, argv);
-    SendLabel (argv[N], Xgraph, 2);
+    KapaSendLabel (Xgraph, argv[N], 2);
     remove_argument (N, &argc, argv);
   }
@@ -61,5 +59,5 @@
   if ((N = get_argument (argc, argv, "+y"))) {
     remove_argument (N, &argc, argv);
-    SendLabel (argv[N], Xgraph, 3);
+    KapaSendLabel (Xgraph, argv[N], 3);
     remove_argument (N, &argc, argv);
   }
@@ -67,5 +65,5 @@
   if ((N = get_argument (argc, argv, "-ul"))) {
     remove_argument (N, &argc, argv);
-    SendLabel (argv[N], Xgraph, 4);
+    KapaSendLabel (Xgraph, argv[N], 4);
     remove_argument (N, &argc, argv);
   }
@@ -73,5 +71,5 @@
   if ((N = get_argument (argc, argv, "-ur"))) {
     remove_argument (N, &argc, argv);
-    SendLabel (argv[N], Xgraph, 5);
+    KapaSendLabel (Xgraph, argv[N], 5);
     remove_argument (N, &argc, argv);
   }
@@ -79,5 +77,5 @@
   if ((N = get_argument (argc, argv, "-ll"))) {
     remove_argument (N, &argc, argv);
-    SendLabel (argv[N], Xgraph, 6);
+    KapaSendLabel (Xgraph, argv[N], 6);
     remove_argument (N, &argc, argv);
   }
@@ -85,5 +83,5 @@
   if ((N = get_argument (argc, argv, "-lr"))) {
     remove_argument (N, &argc, argv);
-    SendLabel (argv[N], Xgraph, 7);
+    KapaSendLabel (Xgraph, argv[N], 7);
     remove_argument (N, &argc, argv);
   }
Index: trunk/Ohana/src/opihi/cmd.data/limits.c
===================================================================
--- trunk/Ohana/src/opihi/cmd.data/limits.c	(revision 5849)
+++ trunk/Ohana/src/opihi/cmd.data/limits.c	(revision 5850)
@@ -71,6 +71,5 @@
  success:
   SetLimits (xvec, yvec, &graphmode);
-  ApplyLimits (Xgraph, &graphmode, APPLY);
+  if (APPLY) KapaSetLimits (Xgraph, &graphmode);
   return (TRUE);
-
 }
Index: trunk/Ohana/src/opihi/cmd.data/line.c
===================================================================
--- trunk/Ohana/src/opihi/cmd.data/line.c	(revision 5849)
+++ trunk/Ohana/src/opihi/cmd.data/line.c	(revision 5850)
@@ -47,5 +47,5 @@
   if ((N = get_argument (argc, argv, "-c"))) {
     remove_argument (N, &argc, argv);
-    graphmode.color = GetColor (argv[N]);
+    graphmode.color = KapaColorByName (argv[N]);
     if (graphmode.color == -1) return (FALSE);
     remove_argument (N, &argc, argv);
Index: trunk/Ohana/src/opihi/cmd.data/plot.c
===================================================================
--- trunk/Ohana/src/opihi/cmd.data/plot.c	(revision 5849)
+++ trunk/Ohana/src/opihi/cmd.data/plot.c	(revision 5850)
@@ -47,5 +47,5 @@
   if ((N = get_argument (argc, argv, "-c"))) {
     remove_argument (N, &argc, argv);
-    graphmode.color = GetColor (argv[N]);
+    graphmode.color = KapaColorByName (argv[N]);
     if (graphmode.color == -1) return (FALSE);
     remove_argument (N, &argc, argv);
Index: trunk/Ohana/src/opihi/cmd.data/ps.c
===================================================================
--- trunk/Ohana/src/opihi/cmd.data/ps.c	(revision 5849)
+++ trunk/Ohana/src/opihi/cmd.data/ps.c	(revision 5850)
@@ -6,8 +6,6 @@
   int N, Source, Nsource, RawImage, NoScale, IsImage;
   
-  if ((N = get_argument (argc, argv, "--help"))) {
-    fprintf (stderr, "USAGE: ps [-name file.ps] [-g | -i] [-n device] [-raw] [-scale]\n");
-    return (FALSE);
-  }
+  if ((N = get_argument (argc, argv, "--help"))) goto help;
+  if ((N = get_argument (argc, argv, "-h"))) goto help;
 
   /* raw image? */
@@ -58,4 +56,8 @@
   KiiPS (Source, NoScale, RawImage, filename);
   return (TRUE);
+
+help:
+  fprintf (stderr, "USAGE: ps [-name file.ps] [-g | -i] [-n device] [-raw] [-scale]\n");
+  return (FALSE);
 }
 
Index: trunk/Ohana/src/opihi/cmd.data/section.c
===================================================================
--- trunk/Ohana/src/opihi/cmd.data/section.c	(revision 5849)
+++ trunk/Ohana/src/opihi/cmd.data/section.c	(revision 5850)
@@ -6,4 +6,5 @@
   int Ngraph, Xgraph;
   Graphdata graphmode;
+  KapaSection section;
 
   List = FALSE;
@@ -21,8 +22,7 @@
   if (!GetGraph (&graphmode, &Xgraph, &Ngraph)) return (FALSE);
 
+  /* list sections */
   if (argc == 1) {
-    /* list section */
-    SendGraphCommand (Xgraph, 4, "LSEC");
-    SendGraphMessage (Xgraph, "* %s", argv[1]);
+    KapaGetSection (Xgraph, "*");
     fprintf (stderr, "USAGE: section name [x y dx dy]\n");
     return (TRUE);
@@ -30,18 +30,21 @@
   
   if (argc == 2) {
-    /* set section */
+    /* select / show section */
     if (List) {
-      SendGraphCommand (Xgraph, 4, "LSEC");
+      KapaGetSection (Xgraph, argv[1]);
     } else {
-      SendGraphCommand (Xgraph, 4, "SSEC");
+      KapaSelectSection (Xgraph, argv[1]);
     }
-    SendGraphMessage (Xgraph, "%s", argv[1]);
     return (TRUE);
   } 
   
   if (argc == 6) {
-    /* define section */
-    SendGraphCommand (Xgraph, 4, "DSEC");
-    SendGraphMessage (Xgraph, "%s %6.3f %6.3f %6.3f %6.3f", argv[1], atof (argv[2]), atof (argv[3]), atof (argv[4]), atof (argv[5]));
+    /* set section */
+    section.name = argv[1];
+    section.x = atof (argv[2]);
+    section.y = atof (argv[3]);
+    section.dx = atof (argv[4]);
+    section.dy = atof (argv[5]);
+    KapaSetSection (Xgraph, &section);
     return (TRUE);
   }
@@ -50,6 +53,6 @@
 }
 
-    /* should do some range checking on x y dx dy
-       should be between 0.0 and 1.0, precision of 0.001
-       is sufficient
-    */
+/* should do some range checking on x y dx dy
+   should be between 0.0 and 1.0, precision of 0.001
+   is sufficient
+*/
Index: trunk/Ohana/src/opihi/cmd.data/style.c
===================================================================
--- trunk/Ohana/src/opihi/cmd.data/style.c	(revision 5849)
+++ trunk/Ohana/src/opihi/cmd.data/style.c	(revision 5850)
@@ -29,5 +29,4 @@
     remove_argument (N, &argc, argv);
   }
-
   if ((N = get_argument (argc, argv, "-lw"))) {
     remove_argument (N, &argc, argv);
@@ -35,5 +34,4 @@
     remove_argument (N, &argc, argv);
   }
-
   if ((N = get_argument (argc, argv, "-pt"))) {
     remove_argument (N, &argc, argv);
@@ -41,15 +39,12 @@
     remove_argument (N, &argc, argv);
   }
-
   if ((N = get_argument (argc, argv, "+eb"))) {
     remove_argument (N, &argc, argv);
     graphmode.ebar = TRUE;
   }
-
   if ((N = get_argument (argc, argv, "-eb"))) {
     remove_argument (N, &argc, argv);
     graphmode.ebar = FALSE;
   }
-
   if ((N = get_argument (argc, argv, "-sz"))) {
     remove_argument (N, &argc, argv);
@@ -57,12 +52,10 @@
     remove_argument (N, &argc, argv);
   }
-
   if ((N = get_argument (argc, argv, "-c"))) {
     remove_argument (N, &argc, argv);
-    graphmode.color = GetColor (argv[N]);
+    graphmode.color = KapaColorByName (argv[N]);
     if (graphmode.color == -1) return (FALSE);
     remove_argument (N, &argc, argv);
   }
-
   if ((N = get_argument (argc, argv, "-x"))) {
     remove_argument (N, &argc, argv);
Index: trunk/Ohana/src/opihi/cmd.data/textline.c
===================================================================
--- trunk/Ohana/src/opihi/cmd.data/textline.c	(revision 5849)
+++ trunk/Ohana/src/opihi/cmd.data/textline.c	(revision 5850)
@@ -23,7 +23,5 @@
     size = atof (argv[N]);
     remove_argument (N, &argc, argv);
-    SendGraphCommand (Xgraph, 4, "FONT");
-    SendGraphCommand (Xgraph, 16, "%s", name);
-    SendGraphCommand (Xgraph, 16, "%d", size);
+    KapaSetFont (Xgraph, name, size);
   } 
 
@@ -60,10 +58,5 @@
   }    
 
-  /* WARNING: this is a bit dangerous - convert to SendGraphMessage format */
-  SendGraphCommand (Xgraph, 4, "PTXT");
-  SendGraphCommand (Xgraph, 16, "%f", x);
-  SendGraphCommand (Xgraph, 16, "%f", y);
-  SendGraphCommand (Xgraph, 16, "%f", angle);
-  SendGraphCommand (Xgraph, 128, "%s", argv[3]);
+  KapaSendTextline (Xgraph, argv[3], x, y, angle);
   return (TRUE);
 }
Index: trunk/Ohana/src/opihi/cmd.data/vbin.c
===================================================================
--- trunk/Ohana/src/opihi/cmd.data/vbin.c	(revision 5849)
+++ trunk/Ohana/src/opihi/cmd.data/vbin.c	(revision 5850)
@@ -3,24 +3,14 @@
 int vbin (int argc, char **argv) {
   
-  int i, j, status, ExactScale;
-  int n, nx, ny, Nx, Ny, x, y, N, Normalize, *Npix, *Vn;
-  int Ignore, IgnoreValue, VERBOSE;
-  float *Vout, *Vin, *Out, *In;
-  double scale, scale2, fx, fy, dX, dY;
-  char temp[1024];
-  Buffer *in, *out;
+  int i, j, n, N, Nin, Nout;
+  int Normalize, Ignore;
+  float *Vout, *Vin, IgnoreValue;
+  double scale;
+  Vector *in, *out;
 
-  Npix = NULL;
-  Vn = NULL;
   Normalize = FALSE;
   if ((N = get_argument (argc, argv, "-norm"))) {
     remove_argument (N, &argc, argv);
     Normalize = TRUE;
-  }
-
-  VERBOSE = FALSE;
-  if ((N = get_argument (argc, argv, "-v"))) {
-    remove_argument (N, &argc, argv);
-    VERBOSE = TRUE;
   }
 
@@ -40,190 +30,37 @@
   }
 
-  if ((in  = SelectBuffer (argv[1], OLDBUFFER, TRUE)) == NULL) return (FALSE);
-  if ((out = SelectBuffer (argv[2], ANYBUFFER, TRUE)) == NULL) return (FALSE);
-  fits_free_matrix (&out[0].matrix);
-  fits_free_header (&out[0].header);
+  if ((in  = SelectVector (argv[1], OLDVECTOR, TRUE)) == NULL) return (FALSE);
+  if ((out = SelectVector (argv[2], ANYVECTOR, TRUE)) == NULL) return (FALSE);
 
   scale  = atof (argv[3]);
-  if ((scale == (int) scale) || ((1.0/scale) == (int)(1.0/scale))) {
-    ExactScale = TRUE;
-    if (scale > 0) {
-      nx = in[0].header.Naxis[0] / scale;
-      ny = in[0].header.Naxis[1] / scale;
-    } else {
-      nx = in[0].header.Naxis[0] * fabs(scale);
-      ny = in[0].header.Naxis[1] * fabs(scale);
-    }    
-  } else {
-    ExactScale = FALSE;
-    if (scale > 0) {
-      nx = (int) (in[0].header.Naxis[0] / scale) + 1;
-      ny = (int) (in[0].header.Naxis[1] / scale) + 1;
-    } else {
-      nx = (int) (in[0].header.Naxis[0] * fabs(scale)) + 1;
-      ny = (int) (in[0].header.Naxis[1] * fabs(scale)) + 1;
-    }      
-  }
-  if (VERBOSE) fprintf (GetOutfile(), "rebin %s to %s (%d,%d to %d,%d)\n", argv[1], argv[2], in[0].header.Naxis[0], in[0].header.Naxis[1], nx, ny);
-
-  Nx = in[0].header.Naxis[0];
-  Ny = in[0].header.Naxis[1];
-  out[0].bitpix = in[0].bitpix;
-  out[0].unsign = in[0].unsign;
-  out[0].bscale = in[0].bscale;
-  out[0].bzero  = in[0].bzero;
-  fits_copy_header (&in[0].header, &out[0].header);
-  fits_modify (&out[0].header, "NAXIS1", "%d", 1, nx);
-  fits_modify (&out[0].header, "NAXIS2", "%d", 1, ny);
-
-  status =  fits_scan (&out[0].header, "CDELT1", "%lf", 1, &dX);
-  status &= fits_scan (&out[0].header, "CDELT2", "%lf", 1, &dY);
-  if (scale > 0) {
-    dX *= scale;
-    dY *= scale;
-  } else {
-    dX /= fabs(scale);
-    dY /= fabs(scale);
-  }    
-  if (status) {
-    fits_modify (&out[0].header, "CDELT1", "%lf", 1, dX);
-    fits_modify (&out[0].header, "CDELT2", "%lf", 1, dY);
+  if ((int)(scale) != scale) {
+    fprintf (stderr, "integer binning only, please\n");
+    return (FALSE);
   }
 
-  status =  fits_scan (&out[0].header, "CRPIX1", "%lf", 1, &dX);
-  status &= fits_scan (&out[0].header, "CRPIX2", "%lf", 1, &dY);
-  if (scale > 0) {
-    dX /= scale;
-    dY /= scale;
-  } else {
-    dX *= fabs(scale);
-    dY *= fabs(scale);
-  }    
-  if (status) {
-    fits_modify (&out[0].header, "CRPIX1", "%lf", 1, dX);
-    fits_modify (&out[0].header, "CRPIX2", "%lf", 1, dY);
-  }
+  Nin  = in[0].Nelements;
+  Nout = Nin / scale;
 
-  out[0].header.Naxis[0] = nx;
-  out[0].header.Naxis[1] = ny;
-  fits_create_matrix (&out[0].header, &out[0].matrix);
-  temp[0] = 0;
-  if ((in[0].file[0] != '*') && (in[0].file[0] != '(')) {
-    strcpy (temp, "*");
-  }
-  strcat (temp, in[0].file);
-  strcpy (out[0].file, temp);
+  REALLOCATE (out[0].elements, float, Nout);
+  out[0].Nelements = Nout;
 
-  if (Normalize) {
-    ALLOCATE (Npix, int, nx*ny);
-    bzero (Npix, nx*ny*sizeof(int));
-  }
-
-  if (ExactScale) {
-    n = scale;
-    if (n > 0) {
-      for (j = 0; j < ny; j++) {
-	for (y = 0; y < n; y++) {
-	  Vout = (float *)(out[0].matrix.buffer) + j*nx;
-	  Vin  = (float *)(in[0].matrix.buffer)  + (j*n + y)*in[0].header.Naxis[0];
-	  if (Normalize) { Vn = Npix + j*nx; }
-	  for (i = 0; i < nx; i++, Vout++) {
-	    for (x = 0; x < n; x++, Vin++) {
-	      if (Ignore && (*Vin == IgnoreValue)) continue;
-	      *Vout += *Vin;
-	      if (Normalize) {(*Vn) ++;}
-	    }
-	    if (Normalize) {Vn ++;}
-	  }
-	}
-      }
-    } else {
-      n = fabs (n);
-      for (j = 0; j < in[0].header.Naxis[1]; j++) {
-	for (y = 0; y < n; y++) {
-	  Vout = (float *)(out[0].matrix.buffer) + (j*n + y)*nx;
-	  Vin  = (float *)(in[0].matrix.buffer)  + j*in[0].header.Naxis[0];
-	  if (Normalize) { Vn = Npix + j*nx; }
-	  for (i = 0; i < in[0].header.Naxis[0]; i++, Vin++) {
-	    if (Ignore && (*Vin == IgnoreValue)) { 
-	      Vout += n; 
-	      if (Normalize) Vn += n; 
-	      continue; 
-	    }
-	    for (x = 0; x < n; x++, Vout++) {
-	      *Vout = *Vin;
-	      if (Normalize) {(*Vn) ++; Vn ++;}
-	    }
-	  }
-	}
-      }
-    }
+  Vin  = in[0].elements;
+  Vout = out[0].elements;
+  for (n = j = 0; j < Nout; j++, Vout++) {
+    *Vout = 0;
+    for (N = i = 0; (i < scale) && (n < Nin); n++, i++, Vin++) {
+      if (!finite (*Vin)) continue;
+      if (Ignore && (*Vin == IgnoreValue)) continue;
+      *Vout += *Vin;
+      N ++;
+    } 
     if (Normalize) {
-      Vn = Npix;
-      Vout = (float *)out[0].matrix.buffer;
-      for (i = 0; i < nx*ny; i++, Vout++, Vn++) {
-	if (*Vn) { 
-	  *Vout /= *Vn; 
-	} else {
-	  *Vout = 0;
-	}
-      }
-    }
-  } else {
-    
-    if (Normalize) { fprintf (stderr, "normalize not enabled for fractional scaling\n"); }
-
-    if (scale < 0) scale = 1.0 / fabs(scale);
-    In = (float *)in[0].matrix.buffer;
-    Out = (float *)out[0].matrix.buffer;
-    scale2 = scale*scale;
-    if (scale > 1) {
-      for (i = 0; i < Ny; i++) {
-	y = 0.5 + (i - 0.5) / scale;
-	fy = 0.5 + MIN (0.5, (y + 0.5) * scale - i);
-	for (j = 0; j < Nx; j++, In++) {
-	  x = 0.5 + (j - 0.5) / scale;
-	  fx = 0.5 + MIN (0.5, (x + 0.5) * scale - j);
-	  Vout = Out + y*nx + x;
-	  *Vout += fx*fy*(*In);
-	  if (fx < 1) {
-	    *(Vout+1)    += (1-fx)*fy*(*In);
-	  }
-	  if (fy < 1) {
-	    *(Vout+nx) += fx*(1-fy)*(*In);
-	  }
-	  if ((fx < 1) && (fy < 1)) {
-	    *(Vout+1+nx) += (1-fx)*(1-fy)*(*In);
-	  }
-	}
-      }
-    } else {
-      for (i = 0; i < ny; i++) {
-	y = 0.5 + (i - 0.5) * scale;
-	fy = 0.5 + MIN (0.5, (y + 0.5) / scale - i);
-	for (j = 0; j < nx; j++, Out++) {
-	  x = 0.5 + (j - 0.5) * scale;
-	  fx = 0.5 + MIN (0.5, (x + 0.5) / scale - j);
-	  Vin = In + y*Nx + x;
-	  *Out += *Vin*fx*fy;
-	  if (fx < 1) {
-	    *Out += *(Vin+1)*(1-fx)*fy;
-	  }
-	  if (fy < 1) {
-	    *Out += *(Vin+Nx)*fx*(1-fy);
-	  }
-	  if ((fx < 1) && (fy < 1)) {
-	    *Out += *(Vin+1+Nx)*(1-fx)*(1-fy);
-	  }
-	  *Out = *Out * scale2;
-	}
+      if (N > 0) { 
+	*Vout /= (float) N; 
+      } else {
+	*Vout = 0;
       }
     }
   }
-
-  if (Normalize) free (Npix);
-
   return (TRUE);
-
 }
-
