# include "data.h"

int mslice (int argc, char **argv) {
  
  int i;
  Buffer *in, *out;

  if (argc != 4) {
    gprint (GP_ERR, "USAGE: mget <input> <output> plane\n");
    return (FALSE);
  }

  int plane = atoi(argv[3]);

  if ((in = SelectBuffer (argv[1], OLDBUFFER, TRUE)) == NULL) return (FALSE);
  if (in[0].matrix.Naxes < 3) {
    gprint (GP_ERR, "buffer is not 3D\n");
    return FALSE;
  }
  if ((out = SelectBuffer (argv[2], ANYBUFFER, TRUE)) == NULL) return (FALSE);

  int Nx = in[0].matrix.Naxis[0];
  int Ny = in[0].matrix.Naxis[1];
  int Nz = in[0].matrix.Naxis[2];

  int invalid = FALSE;
  invalid = invalid || (plane < 0);
  invalid = invalid || (plane >= Nz);
  if (invalid) {
    gprint (GP_ERR, "plane %d out of range\n", plane);
    return (FALSE);
  }

  /* I should encapsulate this in a create_default_buffer */
  gfits_free_matrix (&out[0].matrix);
  gfits_free_header (&out[0].header);
  if (!CreateBuffer (out, Nx, Ny, -32, 1.0, 0.0)) return FALSE;

  float *inF  = (float *) in[0].matrix.buffer + plane*Nx*Ny;
  float *outF = (float *) out[0].matrix.buffer;

  for (i = 0; i < Nx*Ny; i++, inF ++, outF++) {
    *outF = *inF;
  }
  return (TRUE);
}
