# include "addstar.h"

// examine the PHU of this file and determine the file mode
int GetFileMode (Header *header) {

  char ctype[80];
  int Naxis;
  int simple, extend, haveNaxis, haveCTYPE;

  gfits_scan (header, "SIMPLE", "%t", 1, &simple);
  haveNaxis = gfits_scan (header, "NAXIS",  "%d", 1, &Naxis);
  haveCTYPE = gfits_scan (header, "CTYPE1", "%s", 1, ctype);

  gfits_scan (header, "EXTEND", "%t", 1, &extend);
    
  { 
    int tmp, haveCAMCOL, haveSTRIPE;
    
    // SDSS tsObj files have CAMCOL & STRIP keywords present in the header
    haveCAMCOL = gfits_scan (header, "CAMCOL",  "%d", 1, &tmp);
    haveSTRIPE = gfits_scan (header, "STRIPE",  "%d", 1, &tmp);
    if (haveCAMCOL && haveSTRIPE) return SDSS_OBJ;
  }

  if ((Naxis == 2) || TEXTMODE || !simple) {
    if (!strcmp (&ctype[4], "-WRP")) {
      return MOSAIC_CMP;
    }
    return SIMPLE_CMP;
  }

  if (!extend && strcmp (&ctype[4], "-DIS")) {
    if (!strcmp (&ctype[4], "-WRP")) {
      return MOSAIC_CMF;
    }
    return SIMPLE_CMF;
  }

  if (!extend && !strcmp (&ctype[4], "-DIS")) {
    return MOSAIC_PHU;
  }

  if (extend && strcmp (&ctype[4], "-DIS")) {
    return SIMPLE_MEF;
  }

  if (extend && !strcmp (&ctype[4], "-DIS")) {
    return MOSAIC_MEF;
  }

  return (NONE);
}
