# include "absphot.h"

Catalog *load_catalogs (SkyList *skylist) {

  int i, Nstar;
  Catalog *catalog, tcatalog;

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

  ALLOCATE (catalog, Catalog, 1);
  catalog[0].Naverage = catalog[0].Nmeasure = 0;

  // load data from each region file, only use bright stars
  for (i = 0; i < skylist[0].Nregions; i++) {

    // set up the basic catalog info
    tcatalog.filename = skylist[0].filename[i];
    tcatalog.catformat = dvo_catalog_catformat (CATFORMAT);    // set the default catformat from config data
    tcatalog.catmode   = dvo_catalog_catmode (CATMODE);        // set the default catmode from config data
    tcatalog.catflags  = LOAD_AVES | LOAD_MEAS | LOAD_SECF;    // don't need to load all data at this point
    tcatalog.Nsecfilt  = GetPhotcodeNsecfilt ();               // set the desired number in case we need to create the catalog

    if (!dvo_catalog_open (&tcatalog, skylist[0].regions[i], VERBOSE, "r")) {
      fprintf (stderr, "ERROR: failure reading catalog %s\n", tcatalog.filename);
      exit (1);
    }
    if (VERBOSE && !tcatalog.Nave_disk) fprintf (stderr, "no data in %s, skipping\n", tcatalog.filename);

    // select only the stars with source & target data
    subset_catalog (catalog, &tcatalog);

    dvo_catalog_unlock (&tcatalog);
    dvo_catalog_free (&tcatalog);
  }

  return (catalog);
}
