# include "dvomerge.h"
# define PSPS_ID TRUE

# define IN_REGION(R,D) (					\
    ((D) >= region[0].Dmin) && ((D) < region[0].Dmax) &&	\
    ((R) >= region[0].Rmin) && ((R) < region[0].Rmax))

// merge the input data into the output catalog
// input entries are matched by position to the new objects
int merge_catalogs_old (SkyRegion *region, Catalog *output, Catalog *input, double RADIUS, int *secfiltMap) {

  off_t i, j, k, Nin, offset, J, Jmin, status, Nstars;
  double RADIUS2, Rmin;
  double *X1, *Y1, *X2, *Y2;
  double dX, dY, dR;
  off_t *N1, *N2, *next_meas, *next_lens, *next_star;
  off_t Nave, NAVE, Nmeas, NMEAS, Nmatch, Nlens, NLENS, Nstar, NSTAR;
  int NsecfiltIn;
  int NsecfiltOut;
  unsigned int objID, catID;
  Coords tcoords;
  
  // INITTIME;

  NsecfiltOut = output[0].Nsecfilt;
  NsecfiltIn  = input[0].Nsecfilt;
  assert (secfiltMap || (NsecfiltOut == NsecfiltIn));

  /** allocate local arrays (stars) **/
  ALLOCATE (X1, double, input[0].Naverage);
  ALLOCATE (Y1, double, input[0].Naverage);
  ALLOCATE (N1, off_t,  input[0].Naverage);
  if (!input[0].found_t) {
    ALLOCATE (input[0].found_t, off_t, input[0].Naverage);
  } else {
    REALLOCATE (input[0].found_t, off_t, input[0].Naverage);
  }

  /** allocate local arrays (catalog) **/
  NAVE = Nave = output[0].Naverage;
  ALLOCATE (X2, double, NAVE);
  ALLOCATE (Y2, double, NAVE);
  ALLOCATE (N2, off_t,    NAVE);
  if (!output[0].found_t) {
    ALLOCATE (output[0].found_t, off_t, NAVE);
  } else {
    REALLOCATE (output[0].found_t, off_t, NAVE);
  }
  /* for secfilt j and star i, secfilt[i*Nsecfilt+j] */

  /* internal counters */
  Nmatch = 0;
  NMEAS = Nmeas = output[0].Nmeasure;
  NLENS = Nlens = output[0].Nlensing;
  NSTAR = Nstar = output[0].Nstarpar;

  // current max obj ID for this catalog
  objID = output[0].objID;
  catID = output[0].catID;

  /* project onto rectilinear grid with 1 arcsec pixels. the choice of ZEA projection has the
   * advantage that every point in R,D has a mapping to a unique X,Y.  However, note that not all
   * possible X,Y points map back to R,D and the local plate scale changes substantially far from
   * the projection pole.  a better mapping might be ARC, not yet implemented (see
   * coordops.update.c).  We use the center of the region (catalog) for crval1,2. 
   */

  InitCoords (&tcoords, "DEC--ARC");
  tcoords.crval1 = 0.5*(region[0].Rmin + region[0].Rmax);
  if (region[0].Dmax < 90) {
    tcoords.crval2 = 0.5*(region[0].Dmin + region[0].Dmax);
  } else {
    tcoords.crval2 = 90.0;
  }
  tcoords.cdelt1 = tcoords.cdelt2 = 1.0 / 3600.0;

  if (VERBOSE) fprintf (stderr, "merging %s into %s\n", input[0].filename, output[0].filename);

  /* build spatial index (RA sort) referencing input array sequence */
  Nstars = 0;
  for (i = 0; i < input[0].Naverage; i++) {
    status = RD_to_XY (&X1[Nstars], &Y1[Nstars], input[0].average[i].R, input[0].average[i].D, &tcoords);
    if (!status) continue;
    N1[Nstars] = i;
    Nstars ++;
    input[0].found_t[i] = -1;
  }
  if (Nstars < 1) {
    if (VERBOSE) fprintf (stderr, "skipping %s, no overlapping stars\n", output[0].filename);
    free (X1);
    free (Y1);
    free (N1);
    free (X2);
    free (Y2);
    free (N2);
    return (0);
  }
  if (Nstars > 1) sort_coords_index (X1, Y1, N1, Nstars);

  /* build spatial index (RA sort) */
  for (i = 0; i < Nave; i++) {
    RD_to_XY (&X2[i], &Y2[i], output[0].average[i].R, output[0].average[i].D, &tcoords);
    output[0].found_t[i] = -1;
    N2[i] = i;
  }
  if (Nave > 1) sort_coords_index (X2, Y2, N2, Nave);

  if (REPLACE_BY_PHOTCODE && output[0].Nmeasure && !output[0].sorted) {
    fprintf (stderr, "ERROR: attempt to merge with replace into an unsorted database\n");
    exit (3);
  }

  /* set up pointers for linked list of measure */
  if (output[0].sorted && (output[0].Nmeasure >= output[0].Nmeas_disk)) {
    // this version is only valid if we have done a full catalog load, and if the catalog
    // is sorted while processed
    next_meas = init_measure_links (output[0].average, Nave, output[0].measure, Nmeas);
    next_lens = init_lensing_links (output[0].average, Nave, output[0].lensing, Nlens);
    next_star = init_starpar_links (output[0].average, Nave, output[0].starpar, Nstar);
  } else {
    next_meas = build_measure_links (output[0].average, Nave, output[0].measure, Nmeas);
    next_lens = build_lensing_links (output[0].average, Nave, output[0].lensing, Nlens);
    next_star = build_starpar_links (output[0].average, Nave, output[0].starpar, Nstar);
  }    

  /* choose a radius for matches */
  RADIUS2 = RADIUS*RADIUS;

  // MARKTIME("set up structures: %f sec\n", dtime);

  /** find matched stars **/
  for (i = j = 0; (i < Nstars) && (j < Nave); ) {
    if (!finite(X1[i]) || !finite(Y1[i])) { 
      i++; 
      continue;
    }
    if (!finite(X2[j]) || !finite(Y2[j])) { 
      j++; 
      continue;
    }
    
    /* negative dX: j is too large */
    dX = X1[i] - X2[j];
    if (dX <= -1.02*RADIUS) {
      i++;
      continue;
    }
    /* positive dX, i is too large */
    if (dX >= 1.02*RADIUS) {
      j++;
      continue;
    }

    Jmin = -1;
    Rmin = RADIUS2;
    for (J = j; (dX > -1.02*RADIUS) && (J < Nave); J++) {
      /* find closest match for this detection */
      dX = X1[i] - X2[J];
      dY = Y1[i] - Y2[J];
      dR = dX*dX + dY*dY;
      if (dR > RADIUS2) continue;
      if (dR < Rmin) {
	Rmin = dR;
	Jmin  = J;
      }
    }

    /* no match, try next detection */ 
    if (Jmin == -1) {
      i++;
      continue;
    }

    /*** a match is found, add to average, measure ***/
    Nmatch ++;
    off_t n = N2[Jmin];
    off_t N = N1[i];

    /* make sure there is space for next Nmeasure entries */
    if (Nmeas + input[0].average[N].Nmeasure >= NMEAS) {
      NMEAS = Nmeas + input[0].average[N].Nmeasure + 1000;
      REALLOCATE (next_meas, off_t, NMEAS);
      REALLOCATE (output[0].measure, Measure, NMEAS);
    }
    if (Nlens + input[0].average[N].Nlensing >= NLENS) {
      NLENS = Nlens + input[0].average[N].Nlensing + 1000;
      REALLOCATE (next_lens, off_t, NLENS);
      REALLOCATE (output[0].lensing, Lensing, NLENS);
    }
    if (Nstar + input[0].average[N].Nstarpar >= NSTAR) {
      NSTAR = Nstar + input[0].average[N].Nstarpar + 1000;
      REALLOCATE (next_star, off_t, NSTAR);
      REALLOCATE (output[0].starpar, StarPar, NSTAR);
    }

    // 4) average properties from the input and the output db need to be properly merged.

    /** add ALL measurements for this input average object **/
    for (Nin = 0; Nin < input[0].average[N].Nmeasure; Nin ++) {
      offset = input[0].average[N].measureOffset + Nin;

      if (REPLACE_BY_PHOTCODE) {
	// index to first measure for this object
	// XXX this does not support lensing measurements
	int Mout = output[0].average[n].measureOffset;  
	if (replace_match(&output[0].average[n], &output[0].measure[Mout], &input[0].average[N], &input[0].measure[offset])) {
	  input[0].found_t[N] = Mout;
	  continue;
	}
      }
      /* add to end of measurement list */
      add_meas_link (&output[0].average[n], next_meas, Nmeas, NMEAS);
	
      // set the new measurements
      output[0].measure[Nmeas] = input[0].measure[offset];

      // old code: find R,D using average_in[0], the get offset relative to average_out[0].  no longer
      // needed since we carry around R,D
      // Rin = input[0].average[N].R - input[0].measure[offset].dR / 3600.0;
      // Din = input[0].average[N].D - input[0].measure[offset].dD / 3600.0;
      // output[0].measure[Nmeas].dR = 3600.0*(output[0].average[n].R - Rin);
      // output[0].measure[Nmeas].dD = 3600.0*(output[0].average[n].D - Din);

      output[0].measure[Nmeas].dbFlags  = 0;  // XXX why reset these?
      output[0].measure[Nmeas].averef   = n;
      output[0].measure[Nmeas].objID    = output[0].average[n].objID;
      output[0].measure[Nmeas].catID    = output[0].catID;

      assert (output[0].measure[Nmeas].averef < Nave);

      // fprintf (stderr, "Nave : "OFF_T_FMT", Nmeas : "OFF_T_FMT", dR: %f, dD: %f, catID: %d\n",  n,  Nmeas, output[0].measure[Nmeas].dR, output[0].measure[Nmeas].dD, output[0].measure[i].catID);

      float dRoff = dvoOffsetR(&output[0].measure[Nmeas], &output[0].average[n]);

      // rationalize R
      if (dRoff > +180.0*3600.0) {
	// average on high end of boundary, move star up
	output[0].measure[Nmeas].R += 360.0;
	dRoff -= 360.0*3600.0;
      }
      if (dRoff < -180.0*3600.0) {
	// average on low end of boundary, move star down
	output[0].measure[Nmeas].R -= 360.0;
	dRoff += 360.0*3600.0;
      }
      if (fabs(dRoff) > 10*RADIUS) {
        // take declination into account and check again.
        double cosD = cos(RAD_DEG*output[0].average[n].D);
        if (fabs(dRoff*cosD) > 10*RADIUS) {
	  fprintf (stderr, "error: %10.6f,%10.6f vs %10.6f,%10.6f (%f,%f vs %f,%f)\n", 
		   output[0].average[n].R, output[0].average[n].D, 
		   output[0].measure[Nmeas].R, output[0].measure[Nmeas].D,
		   X1[i], X2[Jmin], Y1[i], Y2[Jmin]);
	  // XXX abort on this? -- this is a bad failure...
        }
      }
      input[0].found_t[N] = Nmeas;
      output[0].average[n].Nmeasure ++;
      Nmeas ++;
    }

    // if lensing measurements exist, add them too
    if (output[0].lensing) {
      for (Nin = 0; Nin < input[0].average[N].Nlensing; Nin++) {
	/* add to end of lensing list */
	add_lens_link (&output[0].average[n], next_lens, Nlens, NLENS);
	
	// set the new lensing
	off_t lensoff = input[0].average[N].lensingOffset + Nin;
	output[0].lensing[Nlens] = input[0].lensing[lensoff];

	output[0].lensing[Nlens].averef   = n;
	output[0].lensing[Nlens].objID    = output[0].average[n].objID;
	output[0].lensing[Nlens].catID    = output[0].catID;
	output[0].average[n].Nlensing ++;
	Nlens ++;
      }
    }

    // if lensing measurements exist, add them too
    if (output[0].starpar) {
      for (Nin = 0; Nin < input[0].average[N].Nstarpar; Nin++) {
	/* add to end of lensing list */
	add_star_link (&output[0].average[n], next_star, Nstar, NSTAR);
	
	// set the new starpar
	off_t staroff = input[0].average[N].starparOffset + Nin;
	output[0].starpar[Nstar] = input[0].starpar[staroff];

	output[0].starpar[Nstar].averef   = n;
	output[0].starpar[Nstar].objID    = output[0].average[n].objID;
	output[0].starpar[Nstar].catID    = output[0].catID;
	output[0].average[n].Nstarpar ++;
	Nstar ++;
      }
    }

    // update the average properties to reflect the incoming entries:
    // if the original value is NAN but the input value is not, accept the input:
    if (!secfiltMap) {
      // secfilt tables of input and output are the same
      for (k = 0; k < NsecfiltIn; k++) {
        if ( isfinite(output[0].secfilt[n*NsecfiltIn+k].M)) continue;
        if (!isfinite( input[0].secfilt[N*NsecfiltIn+k].M)) continue;
        output[0].secfilt[n*NsecfiltIn+k] = input[0].secfilt[N*NsecfiltIn+k];
      }
    } else {
      for (k = 0; k < NsecfiltIn; k++) {
	if (secfiltMap[k] < 0) continue;  // skip secfilt entries from input that are not in the output

        // index for this entry in output's secfilt list
        int outputIndex = n * NsecfiltOut + secfiltMap[k];
        
        if ( isfinite(output[0].secfilt[outputIndex].M)) continue;
        if (!isfinite( input[0].secfilt[N*NsecfiltIn+k].M)) continue;
        output[0].secfilt[outputIndex] = input[0].secfilt[N*NsecfiltIn+k];
      }
    }

    /* Nm is updated, but not written out in -update mode (for existing entries)
       Nm is recalculated in build_meas_links if loaded table is not sorted */
    output[0].found_t[n] = Nmeas;
    i++;
  }
  // MARKTIME("find matched stars: %f sec for "OFF_T_FMT","OFF_T_FMT" stars ("OFF_T_FMT" meas)\n", dtime, Nstars, Nave, Nmeas);

  /** incorporate unmatched image stars, if this star is in field of this catalog **/
  /* these new entries are all written out in UPDATE mode */ 
  for (i = 0; i < Nstars; i++) {
    off_t N = N1[i];

    /* make sure there is space for next entry */
    if (Nmeas + input[0].average[N].Nmeasure >= NMEAS) {
      NMEAS = Nmeas + input[0].average[N].Nmeasure + 1000;
      REALLOCATE (next_meas, off_t, NMEAS);
      REALLOCATE (output[0].measure, Measure, NMEAS);
    }
    if (Nlens + input[0].average[N].Nlensing >= NLENS) {
      NLENS = Nlens + input[0].average[N].Nlensing + 1000;
      REALLOCATE (next_lens, off_t, NLENS);
      REALLOCATE (output[0].lensing, Lensing, NLENS);
    }
    if (Nstar + input[0].average[N].Nstarpar >= NSTAR) {
      NSTAR = Nstar + input[0].average[N].Nstarpar + 1000;
      REALLOCATE (next_star, off_t, NSTAR);
      REALLOCATE (output[0].starpar, StarPar, NSTAR);
    }
    if (Nave >= NAVE) {
      NAVE = Nave + 1000;
      REALLOCATE (output[0].average, Average, NAVE);
      REALLOCATE (output[0].secfilt, SecFilt, NAVE*NsecfiltOut);
    }

    if (input[0].found_t[N] >= 0) continue;

    // if we are using MATCHED_TABLES, we are going to leave the edge cases in their
    // source catalog, even if they have leaked beyond the edge
    if (!MATCHED_TABLES && !IN_REGION (input[0].average[N].R, input[0].average[N].D)) continue;

    // XXX should we accept the input measurements for these fields?
    dvo_average_init (&output[0].average[Nave]);
    output[0].average[Nave].R         	   = input[0].average[N].R;
    output[0].average[Nave].D         	   = input[0].average[N].D;
    output[0].average[Nave].measureOffset  = Nmeas;
    output[0].average[Nave].objID     	   = objID; // we create objID values in the context of the output db
    output[0].average[Nave].catID     	   = catID; // we create catID values in the context of the output db

    if (PSPS_ID) {
      output[0].average[Nave].extID = CreatePSPSObjectID(output[0].average[Nave].R, output[0].average[Nave].D);
    } 

    objID ++;

    // init the new secfilt entries
    for (j = 0; j < NsecfiltOut; j++) {
        int outputIndex = (Nave * NsecfiltOut) + j;
	dvo_secfilt_init (&output[0].secfilt[outputIndex]);
    }

    for (j = 0; j < NsecfiltIn; j++) {
      int outputIndex;
      if (secfiltMap) {
        outputIndex = (Nave * NsecfiltOut) + secfiltMap[j];
      } else {
        outputIndex = (Nave * NsecfiltIn) + j;
      }
    
      if (isfinite(input[0].secfilt[N*NsecfiltIn+j].M)) {
	output[0].secfilt[outputIndex] = input[0].secfilt[N*NsecfiltIn+j];
      }
    }

    /** add measurements for this input average object **/
    for (Nin = 0; Nin < input[0].average[N].Nmeasure; Nin ++) {
      offset = input[0].average[N].measureOffset + Nin;

      // supply the measurments from this detection
      output[0].measure[Nmeas]           = input[0].measure[offset];

      // the following measure elements cannot be set until here:
      output[0].measure[Nmeas].dbFlags  = 0;
      output[0].measure[Nmeas].averef   = Nave;
      output[0].measure[Nmeas].objID    = output[0].average[Nave].objID;
      output[0].measure[Nmeas].catID    = output[0].catID;

      // as we add measurements, update Nmeasure to match
      output[0].average[Nave].Nmeasure ++;

      /* we set next[Nmeas] to -1 here, and update correctly below */
      input[0].found_t[N] = Nmeas;
      next_meas[Nmeas] = -1;
      Nmeas ++;
    }
    int Ngroup = input[0].average[N].Nmeasure;
    for (j = 0; j < Ngroup - 1; j++) {
      next_meas[Nmeas - Ngroup + j] = Nmeas - Ngroup + j + 1;
    }

    /** add lensing for this input average object **/
    if (output[0].lensing) {
      output[0].average[Nave].lensingOffset  = Nlens;
      for (Nin = 0; Nin < input[0].average[N].Nlensing; Nin ++) {
	// supply the lensing values from this detection
	off_t lensoff = input[0].average[N].lensingOffset + Nin;
	output[0].lensing[Nlens]           = input[0].lensing[lensoff];

	// the following lensing elements cannot be set until here:
	output[0].lensing[Nlens].averef   = Nave;
	output[0].lensing[Nlens].objID    = output[0].average[Nave].objID;
	output[0].lensing[Nlens].catID    = output[0].catID;

	// as we add lensing, update Nlensing to match
	output[0].average[Nave].Nlensing ++;

	/* we set next[Nlens] to -1 here, and update correctly below */
	next_lens[Nlens] = -1;
	Nlens ++;
      }
      int Ngroup = input[0].average[N].Nlensing;
      for (j = 0; j < Ngroup - 1; j++) {
	next_lens[Nlens - Ngroup + j] = Nlens - Ngroup + j + 1;
      }
    }

    /** add starpar for this input average object **/
    if (output[0].starpar) {
      output[0].average[Nave].starparOffset  = Nstar;
      for (Nin = 0; Nin < input[0].average[N].Nstarpar; Nin ++) {
	// supply the starpar values from this detection
	off_t staroff = input[0].average[N].starparOffset + Nin;
	output[0].starpar[Nstar]           = input[0].starpar[staroff];

	// the following starpar elements cannot be set until here:
	output[0].starpar[Nstar].averef   = Nave;
	output[0].starpar[Nstar].objID    = output[0].average[Nave].objID;
	output[0].starpar[Nstar].catID    = output[0].catID;

	// as we add starpar, update Nstarpar to match
	output[0].average[Nave].Nstarpar ++;

	/* we set next[Nstar] to -1 here, and update correctly below */
	next_star[Nstar] = -1;
	Nstar ++;
      }
      int Ngroup = input[0].average[N].Nstarpar;
      for (j = 0; j < Ngroup - 1; j++) {
	next_star[Nstar - Ngroup + j] = Nstar - Ngroup + j + 1;
      }
    }

    Nave ++;
  }
      
  // MARKTIME("save unmatched stars: %f sec\n", dtime);

  REALLOCATE (output[0].average, Average, Nave);
  REALLOCATE (output[0].measure, Measure, Nmeas);
  REALLOCATE (output[0].lensing, Lensing, Nlens);
  REALLOCATE (output[0].starpar, StarPar, Nstar);
 
# define NOSORT 0
  if (NOSORT) {
    output[0].sorted = FALSE;
  } else {
    output[0].sorted = TRUE;
    output[0].measure = sort_measure (output[0].average, Nave, output[0].measure, Nmeas, next_meas);
    output[0].lensing = sort_lensing (output[0].average, Nave, output[0].lensing, Nlens, next_lens);
    output[0].starpar = sort_starpar (output[0].average, Nave, output[0].starpar, Nstar, next_star);
  }

  /* note stars which have been found in this catalog */
  for (i = 0; i < input[0].Naverage; i++) {
    if (input[0].found_t[i] > -1) {
      input[0].found_t[i] = -2;
    } else {
      input[0].found_t[i] = -3;
    }
  }

  /* check if the catalog has changed?  if no change, no need to write */
  output[0].objID    = objID; // new max value, save on catalog close
  output[0].Naverage = Nave;
  output[0].Nmeasure = Nmeas;
  output[0].Nlensing = Nlens;
  output[0].Nstarpar = Nstar;
  output[0].Nsecf_mem = Nave*NsecfiltOut;
  if (VERBOSE) fprintf (stderr, "Nstars, Nave, Nmeas, Nlens: "OFF_T_FMT" "OFF_T_FMT" "OFF_T_FMT" "OFF_T_FMT", ("OFF_T_FMT" matches)\n",  Nstars,  Nave,  Nmeas,  Nlens, Nmatch);

  free (next_meas);
  free (next_lens);
  free (next_star);

  free (X2);
  free (Y2);
  free (N2);
  free (X1);
  free (Y1);
  free (N1);

  // MARKTIME("cleanup: %f sec\n", dtime);
  return (Nmatch);
}

/* 
   notes:
   
   for finding if a catalog star is in an image or an image star is in the catalog:
   
   catalogs have boundaries defined by RA and DEC, but they may curve in projection
   images have boundaries which are lines in pixels coords, but curve in RA and DEC
   
   output[0].found_t[Ncat] but stars[Nstar].found
   
*/

