# include "relphot.h"

int setExclusions (Catalog *catalog, int Ncatalog) {

  int i, j, k, m, Narea, Nnocal, Ngood, ecode;
  Coords *coords;
  double r, d, x, y;

  Ngood = Nnocal = Narea = 0;
  for (i = 0; i < Ncatalog; i++) {
    for (j = 0; j < catalog[i].Naverage; j++) {
      m = catalog[i].average[j].offset;
      for (k = 0; k < catalog[i].average[j].Nm; k++, m++) {

	/* select measurements by photcode */
	ecode = GetPhotcodeEquivCodebyCode (catalog[i].measure[m].photcode);
	if (ecode != photcode[0].code) goto mark_nocal;
	
	/* select measurements by time */
	if (TimeSelect) {
	  if (catalog[i].measure[m].t < TSTART) goto mark_nocal;
	  if (catalog[i].measure[m].t > TSTOP) goto mark_nocal;
	}

	/* select measurements by mag limit */
	if (AreaSelect) {
	  r = catalog[i].average[j].R + catalog[i].measure[m].dR / 3600.0;
	  d = catalog[i].average[j].D + catalog[i].measure[m].dD / 3600.0;
	  if ((coords = getCoords (m, i)) == NULL) goto markbad;
	  RD_to_XY (&x, &y, r, d, coords);
	  if (x < AreaXmin) goto markbad;
	  if (x > AreaXmax) goto markbad;
	  if (y < AreaYmin) goto markbad;
	  if (y > AreaYmax) goto markbad;
	}
	Ngood ++;
	continue;

      markbad:
	catalog[i].measure[m].flags |= ID_MEAS_AREA;
	Narea ++;
	continue;
	
      mark_nocal:
	catalog[i].measure[m].flags |= ID_MEAS_NOCAL;
	Nnocal ++;
	continue;
      }
    }
  }
  if (VERBOSE) fprintf (stderr, "%d measurements marked by area\n", Narea);
  if (VERBOSE) fprintf (stderr, "%d measurements marked nocal\n", Nnocal);
  if (VERBOSE) fprintf (stderr, "%d measurements kept for analysis\n", Ngood);
  return (TRUE);
}
