# include "data.h"

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

  if (argc != 5) {
    fprintf (stderr, "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")) {
    fprintf (stderr, "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) {
    fprintf (stderr, "selection out of range\n");
    return (FALSE);
  }

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

