# include "data.h"

int imcreate (int argc, char **argv);

int create (int argc, char **argv) {
  
  int i, N;
  float start, end, delta;
  Vector *vec;
  
  if (N = get_argument (argc, argv, "-image")) {
    remove_argument (N, &argc, argv);
    return (imcreate (argc, argv));
  }
  
  if ((argc != 5) && (argc != 4)) {
    fprintf (stderr, "USAGE: create vector start end [delta]\n");
    return (FALSE);
  }

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

  start = atof (argv[2]);
  end   = atof (argv[3]);
  delta = 1;
  if (argc == 5) delta = atof (argv[4]);
  if ((start == end) || (delta == 0)) {
    fprintf (stderr, "error in value: %f to %f, %f\n", start, end, delta);
    return (FALSE);
  }
  delta = fabs (delta);
  if (end - start < 0) {
    delta = -1.0 * delta;
  }

  vec[0].Nelements = (end - start) / delta;
  REALLOCATE (vec[0].elements, float, vec[0].Nelements);

  for (i = 0; i < vec[0].Nelements; i++) {
    vec[0].elements[i] = start + i*delta;
  }

  return (TRUE);
}


int imcreate (int argc, char **argv) {
  
  Buffer *buf;

  if (argc != 4) {
    fprintf (stderr, "USAGE: create -image buffer Nx Ny\n");
    return (FALSE);
  }

  if ((buf = SelectBuffer (argv[1], ANYBUFFER, TRUE)) == NULL) return (FALSE);
  fits_free_matrix (&buf[0].matrix);
  fits_free_header (&buf[0].header);

  buf[0].bitpix = 16;
  buf[0].unsign = FALSE;
  buf[0].bscale = 1.0;
  buf[0].bzero  = 0x8000;

  buf[0].header.bitpix = -32;
  buf[0].header.unsign = FALSE;
  buf[0].header.bscale = 1.0;
  buf[0].header.bzero  = 0.0;

  buf[0].header.Naxes = 2;
  buf[0].header.Naxis[0] = atoi (argv[2]);
  buf[0].header.Naxis[1] = atoi (argv[3]);

  fits_create_header (&buf[0].header);
  fits_create_matrix (&buf[0].header, &buf[0].matrix);

  return (TRUE);

}
