# include "imregister.h"
# include "photreg.h"

int *match_criteria (PhotPars *photdata, int Nphotdata, int *Nmatch) {

  int i, j;
  int N, NMATCH;
  int *match;
  int reject;

  /* create selection index */
  N = 0;
  NMATCH = 1000;
  ALLOCATE (match, int, NMATCH);

  /* find entries that matches criteria */
  for (i = 0; i < Nphotdata; i++) {
    for (j = 0, reject = TRUE; reject && (j < criteria.Ntimes); j++) {
      reject = (photdata[i].tstop < criteria.tstart[j]) || (photdata[i].tstart > criteria.tstop[j]);
    }
    if (criteria.Ntimes && reject) continue;
    if (criteria.PhotCodeSelect && (photdata[i].photcode != criteria.photcode)) continue;
    if (criteria.LabelSelect && strcmp (photdata[i].label, criteria.Label)) continue;

    match[N] = i;
    N ++;
    if (N == NMATCH) {
      NMATCH += 1000;
      REALLOCATE (match, int, NMATCH);
    }
  }
  *Nmatch = N;
  return (match);
}
