# 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;
  int tmp, havePHOT_VER, haveTARG_VER;

  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);
    
  // SDSS tsObj files have a version number for the PHOTO and 
  // TS (target selection) pipelines present as header keywords
  havePHOT_VER = gfits_scan (header, "PHOT_VER", "%s", 1, &tmp);
  haveTARG_VER = gfits_scan (header, "TARG_VER", "%s", 1, &tmp);
  if (havePHOT_VER && haveTARG_VER) 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);
}
