# include "data.h"

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

  if (argc != 5) {
    fprintf (stderr, "USAGE: dimenup <vector> <buffer> Nx Ny\n");
    return (FALSE);
  }

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

  Npix = vec[0].Nelements;
  Nx = atof (argv[3]);
  Ny = atof (argv[4]);
  if (Npix != Nx * Ny) {
    fprintf (stderr, "dimensions don't match\n");
    return (FALSE);
  }

  buf[0].matrix.Naxis[0] = Nx;
  buf[0].matrix.Naxis[1] = Ny;

  /* I should encapsulate this in a create_default_buffer */
  fits_free_matrix (&buf[0].matrix);
  fits_free_header (&buf[0].header);
  buf[0].header.bitpix = buf[0].bitpix = -32;
  buf[0].header.unsign = buf[0].unsign = FALSE;
  buf[0].header.bscale = buf[0].bscale = 1.0;
  buf[0].header.bzero  = buf[0].bzero  = 0.0;
  buf[0].header.Naxes = 2;
  buf[0].header.Naxis[0] = Nx;
  buf[0].header.Naxis[1] = Ny;
  fits_create_header (&buf[0].header);
  fits_create_matrix (&buf[0].header, &buf[0].matrix);

  out = (float *) buf[0].matrix.buffer;
  in = vec[0].elements;

  for (i = 0; i < Npix; i++, in++, out++) {
    *out = *in;
  }

  return (TRUE);
}

