# include "uniphot.h"

wimages (Image *image, int Nimage) {

  int status, mode;
  char line[256];
  FILE *f;
  Header header;

  /* make backup */
  sprintf (line, "cp %s %s~", ImageCat, ImageCat);
  if (system (line)) {
    fprintf (stderr, "ERROR: failed to make backup copy of image table\n");
    exit (1);
  }

  /* load image catalog */
  f = fopen (ImageCat, "r+");
  if (f == NULL) {
    fprintf (stderr, "ERROR: can't open image catalog file to append: %s\n", ImageCat);
    exit (1);
  }
  if (!fits_fread_header (f, &header)) {
    fprintf (stderr, "ERROR: can't read header %s\n", ImageCat);
    exit (1);
  }
  mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
  chmod (ImageCat, mode);
  fseek (f, header.size, SEEK_SET);

  /* write image data */
  if (VERBOSE) fprintf (stderr, "writing out %d images\n", Nimage);
  status = Fwrite (image, sizeof(Image), Nimage, f, "image");
  if (status != Nimage) {
    fprintf (stderr, "ERROR: failed writing data to image catalog\n");
    exit (1);
  }
  fclose (f);

}
