# include "addstar.h"

Stars *find_subset (GSCRegion *region, Stars *stars, int Nstars, int **Subref, int *NSTARS) {

  int i, N, NSUBSET, *subref;
  Stars *subset;
  double RA0, RA1, DEC0, DEC1;

  NSUBSET = 100;
  ALLOCATE (subset, Stars, NSUBSET);
  ALLOCATE (subref, int, NSUBSET);

  RA0 = region[0].RA[0];
  RA1 = region[0].RA[1];
  DEC0 = region[0].DEC[0];
  DEC1 = region[0].DEC[1];

  if (VERBOSE) fprintf (stderr, "subset for %f - %f, %f - %f\n", RA0, RA1, DEC0, DEC1);
  /* read in stars line-by-line */
  for (i = N = 0; i < Nstars; i++) {
    if ((stars[i].R >= RA0)  && (stars[i].R < RA1) &&
	(stars[i].D >= DEC0) && (stars[i].D < DEC1)) {
      subset[N] = stars[i];
      subref[N] = i;
      N++;
      if (N == NSUBSET - 1) {
	NSUBSET += 100;
	REALLOCATE (subset, Stars, NSUBSET);
	REALLOCATE (subref, int, NSUBSET);
      }
    }
  }
  *NSTARS = N;
  *Subref = subref;
  return (subset);
}

