# include "relastro.h"

void FitChip (StarData *raw, StarData *ref, int Nmatch, Coords *coords) {

  int i;
  CoordFit *fit;

  fit = fit_init (coords[0].Npolyterms);
  for (i = 0; i < Nmatch; i++) {
    if (raw[i].mask) continue;
    fit_add (fit, raw[i].X, raw[i].Y, ref[i].L, ref[i].M, 1.0);
  }
  fit_eval (fit);
  fit_apply_coords (fit, coords);
  fit_free (fit);

  // apply new coords to raw (X,Y -> L,M)
  for (i = 0; i < Nmatch; i++) {
    XY_to_LM (&raw[i].L, &raw[i].M, raw[i].X, raw[i].Y, coords);
  }

  fit_free (fit);
}

/* in the mosaic case, we have four coord systems of interest:
   R,D : the sky
   P,Q : the tangent plane
   L,M : the focal plane
   X,Y : the chip

   R,D -> P,Q (projection)
   P,Q -> L,M (polynomial transformation : DIS)
   L,M -> X,Y (polynomial transformation : WRP)
*/

/* XXX I'm not using the errors at all : this could at least be done with the dMag values */
