# include "data.h"

int wd (int argc, char **argv) {
  
  int N, Extend;
  int newUnsign, newBitpix, newScale, newZero;
  int outUnsign, outBitpix;
  double outScale, outZero;
  Header temp_header;
  Matrix temp_matrix;
  Buffer *buf;

  Extend  = FALSE;
  if (N = get_argument (argc, argv, "-extend")) {
    remove_argument (N, &argc, argv);
    Extend  = TRUE;
  }

  newZero  = FALSE;
  if (N = get_argument (argc, argv, "-bzero")) {
    remove_argument (N, &argc, argv);
    outZero  = atof(argv[N]);
    newZero  = TRUE;
    remove_argument (N, &argc, argv);
  }

  newScale = FALSE;
  if (N = get_argument (argc, argv, "-bscale")) {
    remove_argument (N, &argc, argv);
    outScale = atof(argv[N]);
    newScale = TRUE;
    remove_argument (N, &argc, argv);
  }

  newBitpix = FALSE;
  if (N = get_argument (argc, argv, "-bitpix")) {
    remove_argument (N, &argc, argv);
    outBitpix = atof(argv[N]);
    newBitpix = TRUE;
    remove_argument (N, &argc, argv);
  }

  newUnsign = FALSE;
  if (N = get_argument (argc, argv, "-unsign")) {
    remove_argument (N, &argc, argv);
    outUnsign = -1;
    if (!strcasecmp (argv[N], "t") || !strcasecmp (argv[N], "true")) outUnsign = TRUE;
    if (!strcasecmp (argv[N], "f") || !strcasecmp (argv[N], "false")) outUnsign = FALSE;
    if (outUnsign == -1) {
      fprintf (stderr, "-unsign options: t, f, true, false\n");
      return (FALSE);
    }
    newUnsign = TRUE;
    remove_argument (N, &argc, argv);
  }

  if (argc != 3) {
    fprintf (stderr, "USAGE: wd <buffer> <filename> [-bitpix N] [-bscale X] [-bzero X] [-newplane]\n");
    return (FALSE);
  }

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

  if (!newBitpix) outBitpix = buf[0].bitpix;
  if (!newScale) outScale = buf[0].bscale;
  if (!newZero) outZero = buf[0].bzero;
  if (!newUnsign) outUnsign = buf[0].unsign;

  /* Convert the buffer from (float) to correct format */
  /* save the (float) version, write out a temporary buffer */
  temp_matrix = buf[0].matrix;
  ALLOCATE (temp_matrix.buffer, char, MAX(1, temp_matrix.size));
  memcpy (temp_matrix.buffer, buf[0].matrix.buffer, temp_matrix.size);
  temp_header = buf[0].header;
  ALLOCATE (temp_header.buffer, char, MAX(1, temp_header.size));
  memcpy (temp_header.buffer, buf[0].header.buffer, temp_header.size);

  if (temp_header.Naxes) fits_convert_format (&temp_header, &temp_matrix, outBitpix, outScale, outZero, outUnsign);

  if (Extend) {
    Header Xhead;
    FILE *f;
    int status, Nextend;

    /* assume failure means non-existent file */
    if (!fits_read_header (argv[2], &Xhead)) {

      Xhead.simple = TRUE;
      Xhead.bitpix = 16;
      Xhead.Naxes = 0;
      
      Xhead.extend = TRUE;
      Xhead.bscale = 1.0;
      Xhead.bzero  = 0.0;
      Xhead.unsign = FALSE;
      
      fits_create_header (&Xhead);
      fits_modify (&Xhead, "NEXTEND", "%d", 1, 0);
      f = fopen (argv[2], "w");
      fclose (f);
    }

    fits_modify (&Xhead, "EXTEND", "%t", 1, TRUE);

    Nextend = 0;
    fits_scan (&Xhead, "NEXTEND", "%d", 1, &Nextend);
    Nextend ++;
    fits_modify (&Xhead, "NEXTEND", "%d", 1, Nextend);

    /* write the main header to the start of the file */
    f = fopen (argv[2], "r+");
    if (f == NULL) {
      fprintf (stderr, "failed to write file\n");
      status = FALSE;
      goto done1;
    }
    
    /* position to begining of file to write header */
    fseek (f, 0, SEEK_SET);
    status = fwrite (Xhead.buffer, 1, Xhead.size, f);
    if (status != Xhead.size) {
      fprintf (stderr, "ERROR: failed writing data to image header\n");
      status = FALSE;
      goto done1;
    }
    
    /* fix up header */
    {
      static char simple[] = "XTENSION= 'IMAGE  '            / Image extension";
      int Ns, No;
      Ns = strlen (simple);
      No = 80 - Ns;
      strncpy (temp_header.buffer, simple, Ns);
      memset (&temp_header.buffer[Ns], ' ', No);
    }

    /* position to end of file to write new extend */
    fseek (f, 0, SEEK_END);
    status = fwrite (temp_header.buffer, 1, temp_header.size, f);
    fclose (f);
    if (status != temp_header.size) {
      fprintf (stderr, "failed to write file\n");
      status = FALSE;
      goto done1;
    }
    /* write the matrix buffer (automatically goes to end of file */
    if (!fits_write_matrix (argv[2], &temp_matrix)) {
      fprintf (stderr, "failed to write file\n");
      status = FALSE;
      goto done1;
    }
    status = TRUE;
  done1:
    fits_free_header (&Xhead);
    fits_free_header (&temp_header);
    fits_free_matrix (&temp_matrix);
    return (status);
  }
  
  /* the actual write-to-disk goes here */
  if (!fits_write_header (argv[2], &temp_header)) {
    fprintf (stderr, "failed to write header\n");
    fits_free_header (&temp_header);
    fits_free_matrix (&temp_matrix);
    return (FALSE);
  }
  
  if (!fits_write_matrix (argv[2], &temp_matrix)) {
    fprintf (stderr, "failed to write matrix\n");
    fits_free_header (&temp_header);
    fits_free_matrix (&temp_matrix);
    return (FALSE);
  }

  fits_free_header (&temp_header);
  fits_free_matrix (&temp_matrix);
  
  return (TRUE);
}
