# include "addstar.h"

// given a file with the pointer at the start of the table block and the 
// corresponding image header, load the xrad data from the table
int ReadXradFITS (FILE *f, Header *theader, Catalog *catalog) {

  FTable table;
  
  // we've already read in the header
  table.header = theader;
  off_t Nskip = theader[0].datasize;
  fseeko (f, Nskip, SEEK_CUR); 

  /* load the table data */
  if (!gfits_fread_ftable_data (f, &table, FALSE)) {
    fprintf (stderr, "WARNING: no xrad data, skipping\n");
    return TRUE;
  }

  if (theader[0].Naxis[1] == 0) {
    fprintf (stderr, "WARNING: no xrad data, skipping\n");
    return TRUE;
  }

  // I want to read the following columns from this table:
  
  off_t Nrow, NrowAlt;
  int Ncol, NcolAlt;
  char type[16], name[80];

  strcpy (name, "APER_FLUX");
  float *AperFlux = gfits_get_bintable_column_data (theader, &table, name, type, &Nrow, &Ncol);
  myAssert (!strcmp(type, "float"), "wrong column type for %s\n", name);

  strcpy (name, "APER_FLUX_ERR");
  float *AperFluxErr = gfits_get_bintable_column_data (theader, &table, name, type, &NrowAlt, &NcolAlt);
  myAssert (!strcmp(type, "float"), "wrong column type for %s\n", name); 
  myAssert (Nrow == NrowAlt, "column mismatch?");
  myAssert (Ncol == NcolAlt, "column mismatch?");

  strcpy (name, "APER_FLUX_STDEV");
  float *AperFluxStd = gfits_get_bintable_column_data (theader, &table, name, type, &NrowAlt, &NcolAlt);
  myAssert (!strcmp(type, "float"), "wrong column type for %s\n", name);
  myAssert (Nrow == NrowAlt, "column mismatch?");
  myAssert (Ncol == NcolAlt, "column mismatch?");

  strcpy (name, "APER_FILL");
  float *AperFill = gfits_get_bintable_column_data (theader, &table, name, type, &NrowAlt, &NcolAlt);
  myAssert (!strcmp(type, "float"), "wrong column type for %s\n", name);
  myAssert (Nrow == NrowAlt, "column mismatch?");
  myAssert (Ncol == NcolAlt, "column mismatch?");

  strcpy (name, "IPP_IDET");
  int *RadID = gfits_get_bintable_column_data (theader, &table, name, type, &NrowAlt, &NcolAlt);
  myAssert (!strcmp(type, "int"), "wrong column type for %s\n", name);
  myAssert (Nrow == NrowAlt, "column mismatch?");

  if (Nrow > catalog->Nmeasure) {
    myAbort("more radial measurements than stars?  seems like a bug\n");
  }

  myAssert (catalog->lensing, "lensing is not allocated");
  // we could allocate here, or just insist this is an error?
  // if (!catalog->lensing) {
  //   ALLOCATE (catalog->lensing, Lensing, catalog->Nmeasure);
  // }

  int i;
  int Nap = 0;
  for (i = 0; i < catalog->Nmeasure; i++) {
    if (catalog->measure[i].detID < RadID[Nap]) {
      continue;
      // this is a psf measurement which does not have a radial aperture
    }
    if (catalog->measure[i].detID > RadID[Nap]) {
      myAbort("radial apertures out of order?  seems like a bug\n");
      // this would be a radial aperture which does not have a radial aperture
    }

    // we assumed we have already set up lensing in ReadStarsFITS.c

    // XXX this is all hard-wired and should make use of the headers. 
    // psphot cmfs have 5 radial apertures:
    // array 0, 1, 2, 3, 4
    // SDSS  3, 4, 5, 6, 7

    catalog->lensing[i]. F_ApR5 = AperFlux   [Nap*Ncol + 2];
    catalog->lensing[i].dF_ApR5 = AperFluxErr[Nap*Ncol + 2];
    catalog->lensing[i].sF_ApR5 = AperFluxStd[Nap*Ncol + 2];
    catalog->lensing[i].fF_ApR5 = AperFill   [Nap*Ncol + 2];

    catalog->lensing[i]. F_ApR6 = AperFlux   [Nap*Ncol + 3];
    catalog->lensing[i].dF_ApR6 = AperFluxErr[Nap*Ncol + 3];
    catalog->lensing[i].sF_ApR6 = AperFluxStd[Nap*Ncol + 3];
    catalog->lensing[i].fF_ApR6 = AperFill   [Nap*Ncol + 3];

    catalog->lensing[i]. F_ApR7 = AperFlux   [Nap*Ncol + 4];
    catalog->lensing[i].dF_ApR7 = AperFluxErr[Nap*Ncol + 4];
    catalog->lensing[i].sF_ApR7 = AperFluxStd[Nap*Ncol + 4];
    catalog->lensing[i].fF_ApR7 = AperFill   [Nap*Ncol + 4];
    Nap ++;
  }
  myAssert (Nap == Nrow, "did we go too far???");

  gfits_free_table (&table);
  free (AperFlux);
  free (AperFluxErr);
  free (AperFluxStd);
  free (AperFill);
  free (RadID);

  return TRUE;
}

