# include "addstar.h"
# define DVO_IMAGE_NAME_LEN 128

// XXX this function is somewhat specific to the elixir format output files 
// it is capable of distinguishing several format versions defined for elixir/psphot

// examine the header sets and set the Image entries for the the valid images
int LoadData (FILE *f, char *file, Image **images, off_t *nvalid, Stars **stars, unsigned int *Nstars, Header **headers, off_t *extsize, HeaderSet *headerSets, int Nimages, AddstarClientOptions *options) {

  char *name;
  off_t Nskip, Nvalid, NVALID;
  int i, j, Nhead, Ndata;
  uint32_t parentID = UINT32_MAX;
  Stars *inStars;

  if (images[0] == NULL) {
    Nvalid = 0;
    NVALID = 10;
    ALLOCATE (images[0], Image, NVALID);
  } else {
    Nvalid = *nvalid;
    NVALID = Nvalid + 10;
    REALLOCATE (images[0], Image, NVALID);
  }    

  // find image rootname
  if (USE_NAME) {
      name = filebasename (USE_NAME);
  } else {
      name = filebasename (file);
  }

  // if zero points are calculated for the full exposure using more than just the matched chip header,
  // we need to perform that analysis here
  GetZeroPointExposure (headers, headerSets, Nimages);

  // now run through the images, interpret the headers and read the stars
  for (i = 0; i < Nimages; i++) {
    Nhead = headerSets[i].extnum_head;

    if (VERBOSE) fprintf (stderr, "reading header for %s (%s)\n", headerSets[i].exthead, headerSets[i].extdata);
    if (!ReadImageHeader (headers[Nhead], &images[0][Nvalid], options[0].photcode)) {
      fprintf (stderr, "skipping %s\n", headerSets[i].exthead);
      continue;
    }
    images[0][Nvalid].imageID = Nvalid;

    // XXX EAM : I seemed to have dropped the ability to support TEXT (old-style cmp format files).
    // I need to detect them here and load them with ReadStarsTEXT instead of calling the code
    // below.
    // inStars = ReadStarsFITS (f, headers[Nhead], headers[Ndata], &images[0][Nvalid].nstar);

    // XXX use something to set the chip name? EXTNAME?
    if (!strcmp(headerSets[i].exthead, "PHU") && (Nimages == 1)) {
      snprintf (images[0][Nvalid].name, DVO_IMAGE_NAME_LEN, "%s", name);
    } else {
      snprintf (images[0][Nvalid].name, DVO_IMAGE_NAME_LEN, "%s[%s]", name, headerSets[i].exthead);
    }

    // skip the table if there is no data segment (eg, mosaic WRP image)
    if (!strcmp(headerSets[i].extdata, "NONE")) {
      if (!strcmp(headerSets[i].exthead, "PHU") && (Nimages > 1)) {
        // This image is the parent of subsequent images
        parentID = Nvalid;
        images[0][Nvalid].parentID = UINT32_MAX;
      }
      Nvalid++;
      CHECK_REALLOCATE (images[0], Image, NVALID, Nvalid, 10);
      continue;
    }
    images[0][Nvalid].parentID = parentID;

    // advance the pointer to the start of the corresponding table block
    Ndata = headerSets[i].extnum_data;
    Nskip = 0;
    for (j = 0; j < Ndata; j++) {
      Nskip += extsize[j];
    }
    fseeko (f, Nskip, SEEK_SET); 
	 
    inStars = ReadStarsFITS (f, headers[Nhead], headers[Ndata], &images[0][Nvalid].nstar);
    inStars = FilterStars (inStars, &images[0][Nvalid], Nvalid);
    *stars = MergeStars (*stars, Nstars, inStars, images[0][Nvalid].nstar);
    Nvalid++;
    CHECK_REALLOCATE (images[0], Image, NVALID, Nvalid, 10);
  }
  free (name);
  *nvalid = Nvalid;
  return (TRUE);
}

