# include "dvo.h"

Image *LoadImages (int *Nimage) {

  char filename[256];
  int nimage, NIMAGE;
  FILE *f;
  Header header;
  Image *image;
  
  VarConfig ("IMAGE_CATALOG", "%s", filename);

  /* check if image datafile exists, get header */
  if (!fits_read_header (filename, &header)) {
    fprintf (stderr, "No available image catalog %s (1)\n", filename);
    *Nimage = 0;
    return ((Image *) NULL);
  }

  /* get ready to read data on images */ 
  f = fopen (filename, "r");
  if (f == (FILE *) NULL) {
    fprintf (stderr, "No available image catalog: %s (2)\n", filename);
    free (header.buffer);
    *Nimage = 0;
    return ((Image *) NULL);
  }

  NIMAGE = 0;
  fits_scan (&header, "NIMAGES", "%d", 1, &NIMAGE);
  if (NIMAGE == 0) {
    fprintf (stderr, "no images found\n");
    *Nimage = NIMAGE;
    fclose (f);
    return ((Image *) NULL);
  }

  ALLOCATE (image, Image, NIMAGE);
  fseek (f, header.size, SEEK_SET); 
  nimage = Fread (image, sizeof(Image), NIMAGE, f, "image");
  if (nimage != NIMAGE) {
    fprintf (stderr, "failed to read in all images (%d of %d)\n", nimage, NIMAGE);
  }

  fclose (f);
  *Nimage = nimage;
  return (image);


}
  
