# include "addstar.h"

Stars *rfits (FILE *f, unsigned int *nstars) {

  int i, Nstars;
  Header theader;
  FTable table;
  Stars *stars;
  SMPData *smpdata;

  /* if no stars, no table */
  if (*nstars == 0) return (NULL);

  /* init & load in table data */
  table.header   = &theader;
  if (!gfits_fread_ftable (f, &table, "SMPFILE")) goto escape;

  smpdata = gfits_table_get_SMPData (&table, &Nstars, NULL);

  ALLOCATE (stars, Stars, Nstars);
  for (i = 0; i < Nstars; i++) {
    stars[i].X      = smpdata[i].X;
    stars[i].Y      = smpdata[i].Y;
    stars[i].M      = smpdata[i].M;
    stars[i].dM     = smpdata[i].dM;
    stars[i].dophot = smpdata[i].dophot;

    stars[i].Mgal   = smpdata[i].M;
    stars[i].Map    = smpdata[i].dM;
    stars[i].fx     = smpdata[i].fx;
    stars[i].fy     = smpdata[i].fy;
    stars[i].df     = smpdata[i].df;
  }    
  *nstars = Nstars;
  return (stars);

escape:
  fprintf (stderr, "error reading file\n");
  *nstars = 0;
  return (NULL);
}

// **** this needs to be fixed to handle MEF smp files...
// there will be multiple extension names, how will these get passed?
