# include "addstar.h"

// examine the header sets and set the Image entries for the the valid images
// there should only be a single data set (phu + table) in this file
// each SDSS data set corresponds to 5 images (ugriz)
int LoadDataSDSS (FILE *f, char *file, Image **images, off_t *nvalid, Stars **stars, unsigned int *Nstars, Header **headers, off_t *extsize, HeaderSet *headerSets, off_t Nimages) {

  off_t Nskip, Nvalid, NVALID;
  char *name;
  int j, Nhead, Ndata;
  unsigned int Ninstars;
  Stars *inStars;

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

  // find image rootname
  name = filebasename (file);

  // there is only one SDSS image per file (TRUE?)
  Nhead = headerSets[0].extnum_head;

  // XXX parse the information needed from the PHU header
  if (VERBOSE) fprintf (stderr, "reading header for %s (%s)\n", headerSets[0].exthead, headerSets[0].extdata);

  // advance the pointer to the start of the corresponding table block
  Ndata = headerSets[0].extnum_data;
  Nskip = 0;
  for (j = 0; j < Ndata; j++) {
    Nskip += extsize[j];
  }
  fseeko (f, Nskip, SEEK_SET); 
	 
  // XXX I think this is an error?  should this be &images[0][Nvalid] ??
  inStars = ReadStarsSDSS (f, name, headers[Nhead], headers[Ndata], images[0], &Nvalid, &Ninstars);
  *stars = MergeStars (*stars, Nstars, inStars, Ninstars);

  free (name);
  *nvalid = Nvalid;
  return (TRUE);
}

