# include "relastro.h"

SkyList *load_images (FITS_DB *db, SkyRegion *region) {

  Image     *image, *subset;
  int        Nimage, Nsubset;
  int       *LineNumber;

  SkyTable *sky = NULL;
  SkyList *skylist = NULL;

  // load the current sky table (layout of all SkyRegions) 
  sky = SkyTableLoadOptimal (CATDIR, SKY_TABLE, GSCFILE, SKY_DEPTH, VERBOSE);
  SkyTableSetFilenames (sky, CATDIR, "cpt");
  
  // determine the populated SkyRegions overlapping the requested area
  skylist = SkyListByPatch (sky, -1, region);

  // convert database table to internal structure
  image = gfits_table_get_Image (&db[0].ftable, &Nimage, &db[0].swapped);

  // select the images which overlap the selected sky regions
  subset = select_images (skylist, image, Nimage, &LineNumber, &Nsubset);

  gfits_vtable_from_ftable (&db[0].ftable, &db[0].vtable, LineNumber, Nsubset);

  initImages (subset, Nsubset);
  initMosaics (subset, Nsubset);
  
  /* unlock, if we can (else, unlocked below) */
  if (!UPDATE) dvo_image_unlock (db); 

  return (skylist);
}
