# include "imregister.h"
# include "imreg.h"

int *match_criteria (RegImage *image, int Nimage, int *Nmatch) {

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

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

  Nname = 0;
  if (criteria.NameSelect) Nname = strlen (criteria.Name);
  
  /* find entries that matches criteria */
  for (i = 0; i < Nimage; i++) {
    for (j = 0, reject = TRUE; reject && (j < criteria.Ntimes); j++) {
      reject = (image[i].obstime + image[i].exptime < criteria.tstart[j]) || (image[i].obstime > criteria.tstop[j]);
    }
    if (criteria.Ntimes && reject) continue;
    if (criteria.FilterSelect  && (strcasecmp (image[i].filter, criteria.Filter))) continue;
    if (criteria.ModeSelect    && (image[i].mode != criteria.Mode)) continue;
    if (criteria.CCDSelect     && (image[i].ccd != criteria.CCD)) continue;
    if (criteria.TypeSelect    && (image[i].type != criteria.Type)) continue;
    if (criteria.ExptimeSelect && (fabs (image[i].exptime - criteria.Exptime) > 5.0)) continue;
    if (criteria.NameSelect    && (strncasecmp (image[i].filename, criteria.Name, Nname))) continue;
    if (criteria.ProcSelect    && (criteria.Proc ^ (image[i].bias != 0.0))) continue;
    if (criteria.DistSelect    && (criteria.Dist ^ (image[i].flag && IMREG_DIST))) continue;

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

int *match_images (RegImage *image, int Nimage, RegImage *subset, int Nsubset, int *Nmatch) {
  
  int i, j, N, Nfound;
  int *match;

  /* find matching images - very inefficient : sort by obstime, find those first? */
  ALLOCATE (match, int, Nsubset);
  for (j = 0; j < Nsubset; j++) {
    match[j] = -1;
    for (i = 0; (match[j] == -1) && (i < Nimage); i++) {
      if (image[i].obstime > subset[j].obstime + 1) continue;
      if (image[i].obstime < subset[j].obstime - 1) continue;
      if (image[i].ccd != subset[j].ccd) continue;
      match[j] = i;
    }
  }
  
  Nfound = 0;
  for (i = 0; i < Nsubset; i++) {
    /* set the new values for this image */
    N = match[i];
    if (N == -1) continue;
    image[N].fwhm = subset[i].fwhm; 
    image[N].bias = subset[i].bias; 
    image[N].sky  = subset[i].sky; 
    image[N].ra   = subset[i].ra; 
    image[N].dec  = subset[i].dec; 
    /* if the image is MEF, these were not correctly assigned by imsort.
       this step uses the values from the split ccd image */
    Nfound ++;
  }
  *Nmatch = Nfound;
  return (match);
}
