# include "relastro.h"

int UpdateChips (Catalog *catalog, int Ncatalog) {

  /* we can measure new image parameters for each non-mosaic chip independently */
  int i, Nimage, Nraw, Nref;
  Image *image;
  StarData *raw, *ref;

  image = getimages (&Nimage);

  for (i = 0; i < Nimage; i++) {

    /* skip all except WRP images */
    if (strcmp(&image[i].coords.ctype[4], "-WRP")) continue;

    /* convert measure coordinates to raw entries */
    raw = getImageRaw (catalog, Ncatalog, i, &Nraw, MODE_MOSAIC);

    /* convert average coordinates to ref entries */
    ref = getImageRef (catalog, Ncatalog, i, &Nref, MODE_MOSAIC);

    // note that Nraw & Nref must be equal: if not, we made a programming error in one of these two functions.
    assert (Nraw == Nref);

    // FitChip does iterative, clipped fitting
    fprintf (stderr, "image %d : Nstars: %d\n", i, Nraw);
    FitChip (raw, ref, Nraw, &image[i].coords);

    free (raw);
    free (ref);
  }

  return (TRUE);
}

