# include "relastro.h"

// we are sharing image astrometry calibrations for all images which (a) I own and (b) which have unowned detections

# define D_NIMAGEPOS 1000
int share_image_pos (RegionHostTable *regionHosts, int nloop) {

  off_t i, Nimages;
  Image *images = getimages (&Nimages, NULL);

  off_t Nimage_pos = 0;
  off_t NIMAGE_POS = D_NIMAGEPOS;
  
  ImagePos *image_pos = NULL;
  ALLOCATE (image_pos, ImagePos, NIMAGE_POS);

  for (i = 0; i < Nimages; i++) {
    // XXX does this image have missing detections (does someone else need it?)
    // XXX : NOTE NEED TO FIX THIS: if (imageExtra[i].Nmiss == 0) continue;
    
    set_image_pos (&image_pos[Nimage_pos], &images[i]);
    Nimage_pos ++;

    CHECK_REALLOCATE (image_pos, ImagePos, NIMAGE_POS, Nimage_pos, D_NIMAGEPOS);
  }

  // write out the image_mag fits table AND write state in some file
  int myHost = regionHosts->index[REGION_HOST_ID];
  char *hostname = regionHosts->hosts[myHost].hostname;

  char *iposfile = make_filename (CATDIR, hostname, REGION_HOST_ID, "imagepos.fits");
  ImagePosSave (iposfile, image_pos, Nimage_pos);
  free (image_pos);
  free (iposfile);

  char *syncfile = make_filename (CATDIR, hostname, REGION_HOST_ID, "imagepos.sync");
  update_sync_file (syncfile, nloop);
  free (syncfile);

  return TRUE;
}

int slurp_image_pos (RegionHostTable *regionHosts, int nloop) {

  off_t Nimage, i;
  Image *images = getimages (&Nimage, NULL);

  int Nimage_pos = 0;
  ImagePos *image_pos = NULL;
  ALLOCATE (image_pos, ImagePos, 1);

  fprintf (stderr, "grabbing image mags from other hosts...\n");

  for (i = 0; i < regionHosts->Nhosts; i++) {
    if (regionHosts->hosts[i].hostID == REGION_HOST_ID) continue;
    if (REGION_HOST_ID && !regionHosts->hosts[i].isNeighbor) continue;

    char *syncfile = make_filename (CATDIR, regionHosts->hosts[i].hostname, regionHosts->hosts[i].hostID, "imagepos.sync");
    check_sync_file (syncfile, nloop);
    free (syncfile);
    
    off_t Nsubset;
    char *iposfile = make_filename (CATDIR, regionHosts->hosts[i].hostname, regionHosts->hosts[i].hostID, "imagepos.fits");
    ImagePos *image_pos_subset = ImagePosLoad (iposfile, &Nsubset);
    free (iposfile);

    image_pos = merge_image_pos (image_pos, &Nimage_pos, image_pos_subset, Nsubset);
  }

  for (i = 0; i < Nimage_pos; i++) {
    off_t seq = getImageByID (image_pos[i].imageID);
    if (seq < 0) {
      // XXX is this a problem? (no, other hosts don't know which images I own)
      continue;
    }
    images[seq].coords     = image_pos[i].coords    ;
    images[seq].dXpixSys   = image_pos[i].dXpixSys  ;
    images[seq].dYpixSys   = image_pos[i].dYpixSys  ;
    images[seq].imageID    = image_pos[i].imageID   ;
    images[seq].nFitAstrom = image_pos[i].nFitAstrom;
    images[seq].flags      = image_pos[i].flags     ;
  }
  free (image_pos);

  fprintf (stderr, "DONE grabbing image mags from other hosts\n");

  return TRUE;
}

int set_image_pos (ImagePos *image_pos, Image *image) {

  image_pos->coords       = image->coords;
  image_pos->dXpixSys     = image->dXpixSys;
  image_pos->dYpixSys     = image->dYpixSys;
  image_pos->imageID      = image->imageID;
  image_pos->nFitAstrom   = image->nFitAstrom;
  image_pos->flags        = image->flags;

  return TRUE;
}

ImagePos *merge_image_pos (ImagePos *target, int *ntarget, ImagePos *source, int Nsource) {

  off_t i;

  REALLOCATE (target, ImagePos, *ntarget + Nsource);
  for (i = 0; i < Nsource; i++) {
    off_t n = i + *ntarget;
    target[n] = source[i];
  }
  
  free (source);

  *ntarget += Nsource;
  return (target);
}

