# include "relphot.h"

void reload_catalogs (GSCRegion *region, int Nregion, GSCRegion *fullregion) {

  int i;
  char filename[256];
  Catalog catalog;

  if (VERBOSE) fprintf (stderr, "re-loading catalog data\n");

  /* load data from each region file */
  for (i = 0; i < Nregion; i++) {
    sprintf (filename, "%s/%s", CATDIR, region[i].filename);
    catalog.filename = filename;  /* don't free region before catalog! */
    switch (lock_catalog (&catalog, LCK_XCLD)) {
    case 0:
      fprintf (stderr, "ERROR: can't lock file %s\n", region[i].filename);
      exit (1);
    case 1:
      gcatalog (&catalog, TRUE); /* load from disk */
      break;
    case 2:
      if (VERBOSE) fprintf (stderr, "no data in %s, skipping\n", catalog.filename);
      unlock_catalog (&catalog);
      continue;
      break;
    default:
      fprintf (stderr, "weird lock_catalog exit state\n");
      exit (1);
    }
    initImageBins  (&catalog, 1);
    initMosaicBins (&catalog, 1);
    initGridBins   (&catalog, 1);

    findImages (&catalog, 1);
    findMosaics (&catalog, 1);

    setMrelFinal (&catalog);
    wcatalog (&catalog); 
    unlock_catalog (&catalog);

    freeImageBins (1);
    freeMosaicBins (1);
    freeGridBins (1);
  }
}
