# include "getstar.h"

int gcatalog (Catalog *catalog) {
  
  int Nsecfilt;
  char mode;

  /* read catalog header */
  mode = LOAD_AVES | LOAD_MEAS | LOAD_MISS | LOAD_SECF;
  if (!load_catalog (catalog, mode, VERBOSE)) {
    fprintf (stderr, "ERROR: failure loading catalog\n");
    exit (1);
  }

  /* check Nsecfile value, update if needed */
  Nsecfilt = GetPhotcodeNsecfilt ();
  if (catalog[0].Nsecfilt < Nsecfilt) {

    int i, j, Nextra, in, out;
    SecFilt *insec, *outsec;

    Nextra = Nsecfilt - catalog[0].Nsecfilt;
    insec = catalog[0].secfilt;
    ALLOCATE (outsec, SecFilt, catalog[0].Naverage * Nsecfilt);
    for (in = out = i = 0; i < catalog[0].Naverage; i++) {
      for (j = 0; j < catalog[0].Nsecfilt; j++, in++, out++) {
	outsec[out].M = insec[in].M;
	outsec[out].Xm = insec[in].Xm;
      }
      for (j = 0; j < Nextra; j++, out++) {
	outsec[out].M = NO_MAG;
	outsec[out].Xm = NO_MAG;
      }
    }
    free (catalog[0].secfilt);
    catalog[0].secfilt = outsec;
    catalog[0].Nsecfilt = Nsecfilt;
  }

  if (catalog[0].Nsecfilt > Nsecfilt) {
    fprintf (stderr, "ERROR: can't reduce number of secondary filters\n");
    exit (1);
  }
  return (TRUE);
}
