# include "data.h"

int mget (int argc, char **argv) {
  
  int i, Nx, Ny, Npix, xdir, Nset;
  float *in;
  opihi_flt *out;
  Buffer *buf;
  Vector *vec;

  if (argc != 5) {
    gprint (GP_ERR, "USAGE: mget <buffer> <vector> <-x/-y> <N>\n");
    return (FALSE);
  }

  if ((buf = SelectBuffer (argv[1], OLDBUFFER, TRUE)) == NULL) return (FALSE);
  if ((vec = SelectVector (argv[2], ANYVECTOR, TRUE)) == NULL) return (FALSE);

  if (strcasecmp (argv[3], "-x") && strcasecmp (argv[3], "-y")) {
    gprint (GP_ERR, "USAGE: mget <buffer> <vector> <-x/-y> <N>\n");
    return (FALSE);
  }
  xdir = TRUE;
  if (!strcasecmp (argv[3], "-y")) xdir = FALSE;

  Nx = buf[0].matrix.Naxis[0];
  Ny = buf[0].matrix.Naxis[1];
  Nset = atof (argv[4]);
  if (Nset < 0) {
    gprint (GP_ERR, "selection out of range\n");
    return (FALSE);
  }

  if (xdir) {
    Npix = Nx;
    ResetVector (vec, OPIHI_FLT, Nx);
    if (Nset >= Ny) {
      gprint (GP_ERR, "row out of range\n");
      return (FALSE);
    }
    in  = (float *) buf[0].matrix.buffer + Nx*Nset;
    out = vec[0].elements.Flt;
    for (i = 0; i < Npix; i++, in++, out++) {
      *out = *in;
    }
  } else {
    Npix = Ny;
    ResetVector (vec, OPIHI_FLT, Ny);
    if (Nset >= Nx) {
      gprint (GP_ERR, "column out of range\n");
      return (FALSE);
    }
    in  = (float *) buf[0].matrix.buffer + Nset;
    out = vec[0].elements.Flt;
    for (i = 0; i < Npix; i++, in+=Nx, out++) {
      *out = *in;
    }
  }
  return (TRUE);
}
