# include "relastro.h"

/* XXX I think all of these, except for X,Y selection, are done / can be done in bcatalog */
int setExclusions (Catalog *catalog, int Ncatalog) {

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

  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 */
	// XXXX is this done when loaded, or is it needed for reload?
	// 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;
	}
	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);
  return (TRUE);
}
