# include "addstar.h"

// XXX largely psphot specific

HeaderSet *MatchHeaders (off_t **extsize, off_t *nimage, int mode, Header **headers, int Nheaders) {

  int i, j, Nimage, NIMAGE;
  char extname[80], exttype[80], exthead[80], xtension[80];
  HeaderSet *headerSets;

  ALLOCATE (extsize[0], off_t, Nheaders);

  Nimage = 0;
  NIMAGE = 10;
  ALLOCATE (headerSets, HeaderSet, NIMAGE);

  // what is the mode of the first header (ie, do we have a PHU DIS image?)
  mode = GetFileMode (headers[0]);

  if (mode == MOSAIC_MEF) {
    headerSets[Nimage].exthead     = strcreate ("PHU");
    headerSets[Nimage].extdata     = strcreate ("NONE");
    headerSets[Nimage].extnum_data = -1;
    headerSets[Nimage].extnum_head =  0;
    Nimage ++;
  }

  if (mode == SDSS_OBJ) {
    // XXX these should have two headers (phu + table)
    assert (Nheaders == 2);
    headerSets[0].extdata     = strcreate ("SDSS_OBJ");
    headerSets[0].exttype     = strcreate ("SDSS_OBJ");
    headerSets[0].exthead     = strcreate ("PHU");
    headerSets[0].extnum_head = 0;
    headerSets[0].extnum_data = 1;
    extsize[0][0] = headers[0][0].datasize;
    *nimage = 1;
    return headerSets;
  }

  // now examine the headers, count the table entries, find corresponding headers
  for (i = 0; i < Nheaders; i++) {
    if (mode == SIMPLE_CMP) {
      extsize[0][i] = headers[i][0].datasize;
    } else {
      extsize[0][i] = headers[i][0].datasize + gfits_data_size (headers[i]);
    }

    // is this a fits table? (otherwise we skip it)
    if (!gfits_scan (headers[i], "XTENSION", "%s", 1, xtension)) continue;
    if (strcmp (xtension, "BINTABLE")) continue;

    if (!gfits_scan (headers[i], "EXTTYPE", "%s", 1, exttype)) continue;
    if (!strcmp (exttype, "SMPDATA")) goto keep;
    if (!strcmp (exttype, "PS1_DEV_0")) goto keep;
    if (!strcmp (exttype, "PS1_DEV_1")) goto keep;
    if (!strcmp (exttype, "PS1_V1")) goto keep;
    if (!strcmp (exttype, "PS1_V2")) goto keep;
    continue;

  keep:
    headerSets[Nimage].exttype = strcreate (exttype);

    gfits_scan (headers[i], ExtnameKeyword, "%s", 1, extname);
    gfits_scan (headers[i], "EXTHEAD", "%s", 1, exthead);

    headerSets[Nimage].extdata     = strcreate (extname);
    headerSets[Nimage].exthead     = strcreate (exthead);
    headerSets[Nimage].extnum_data = i;
    headerSets[Nimage].extnum_head = -1;

    // find the matching exthead entry
    for (j = 0; j < Nheaders; j++) {
      if (!gfits_scan (headers[j], ExtnameKeyword, "%s", 1, extname)) continue;
      if (strcmp (extname, headerSets[Nimage].exthead)) continue;
      headerSets[Nimage].extnum_head = j;
      break;
    }

    // skip or crash on table with missing matching header?
    if (headerSets[Nimage].extnum_head == -1) {
      return NULL;
    }
    Nimage ++;
    if (Nimage == NIMAGE) {
      NIMAGE += 10;
      REALLOCATE (headerSets, HeaderSet, NIMAGE);
    }
  }

  // some old format files did not write EXTTYPE.  they have a single table in the first
  // extension matched to the header in the PHU
  if (Nimage == 0) {
    extsize[0][0] = headers[0][0].datasize + gfits_data_size (headers[0]);
    extsize[0][1] = headers[1][0].datasize + gfits_data_size (headers[1]);
    gfits_scan (headers[1], ExtnameKeyword, "%s", 1, extname);
    if (!strcmp (extname, "SMPFILE")) {
      headerSets[Nimage].extdata     = strcreate (extname);
      headerSets[Nimage].exttype     = strcreate ("SMPDATA");
      headerSets[Nimage].exthead     = strcreate ("PHU");
      headerSets[Nimage].extnum_head = 0;
      headerSets[Nimage].extnum_data = 1;
      Nimage = 1;
    }
  }
  
  *nimage = Nimage;
  return (headerSets);
}

