# include "astro.h"

void alter_header (head, argv, argc, X_O, X_X, X_Y, dX, Y_O, Y_X, Y_Y, dY, N)
char head[], **argv;
int argc;
double X_O, X_X, X_Y, dX, Y_O, Y_X, Y_Y, dY;
int N;
{

  Header header;
  char tmp[200], line[500], *c;
  int i;

  fits_read_header (head, &header);

  fits_modify (&header, "RA_O", "%lf", 1, X_O);
  fits_modify (&header, "DEC_O", "%lf", 1, Y_O);
  if (ROTATE) {
    fits_modify (&header, "RA_X", "%le", 1, X_Y);
    fits_modify (&header, "RA_Y", "%le", 1, X_X);
    fits_modify (&header, "DEC_X", "%le", 1, Y_Y);
    fits_modify (&header, "DEC_Y", "%le", 1, Y_X);
  }
  else {
    fits_modify (&header, "RA_X", "%le", 1, X_X);
    fits_modify (&header, "RA_Y", "%le", 1, X_Y);
    fits_modify (&header, "DEC_X", "%le", 1, Y_X);
    fits_modify (&header, "DEC_Y", "%le", 1, Y_Y);
  }    
  fits_modify (&header,  "dRA", "%lf", 1,  dX);
  fits_modify (&header,  "dDEC", "%lf", 1,  dY);
  fits_modify (&header,  "Nastro", "%d", 1,  N);
  for (i = 3; i < argc; i++) {
    if ((c = strrchr (argv[i], '/')) != NULL) 
      fits_modify (&header, "REF", "%s", i-2, c+1);
    else
      fits_modify (&header, "REF", "%s", i-2, argv[i]);
  }

  fits_modify (&header, "RA_O", "%C", 1, "astro coeff");
  fits_modify (&header, "RA_X", "%C", 1, "astro coeff");
  fits_modify (&header, "RA_Y", "%C", 1, "astro coeff");
  fits_modify (&header,  "dRA", "%C", 1, "error (pixels)");
  fits_modify (&header, "DEC_O", "%C", 1, "astro coeff");
  fits_modify (&header, "DEC_X", "%C", 1, "astro coeff");
  fits_modify (&header, "DEC_Y", "%C", 1, "astro coeff");
  fits_modify (&header,  "dDEC", "%C", 1, "error (pixels)");
  fits_modify (&header,  "Nastro", "%C", 1, "Number of stars used");
  for (i = 3; i < argc; i++) 
    fits_modify (&header, "REF", "%C", i-2, "astro ref file");

  strcpy (tmp, head);
  strcat (tmp, "~");
  sprintf (line, "mv %s %s\0", head, tmp);
  system (line);

  fits_write_header (head, &header);
  fits_free_header (&header);

}
