
/* read an image header corresponding to a CMF / CMP data block */
Image *ReadImageHeader (Header *header) {

  Image *image;

  ALLOCATE (image, Image, 1);

  /* get astrometry information */
  if (!GetCoords (&image[0].coords, &header)) {
    fprintf (stderr, "ERROR: no astrometric solution in header\n");
    exit (1);
  }
  itmp = 0;
  gfits_scan (&header, "NASTRO",   "%d", 1, &itmp);
  if ((itmp == 0) && !ACCEPT_ASTROM) {
    fprintf (stderr, "ERROR: bad astrometric solution in header\n");
    exit (1);
  }
  if (!strcmp (&image[0].coords.ctype[4], "-WRP")) {
    if (MOSAIC == NULL) {
      fprintf (stderr, "ERROR: no mosaic for WRP image (use -mosaic)\n");
      exit (1);
    }
    RegisterMosaic (MOSAIC);
  } else {
    /* force image to lie in 0-360 range */
    while (image[0].coords.crval1 < 0) image[0].coords.crval1 += 360.0;
    while (image[0].coords.crval1 > 360.0) image[0].coords.crval1 -= 360.0;
  }
    
  /* CERROR in data file is in arcsec */
  if (!gfits_scan (&header, "CERROR",   "%lf", 1, &tmp)) tmp = 1.0;
  image[0].cerror = tmp * 50.0;
  gfits_scan (&header, "NAXIS1",   "%hd", 1, &image[0].NX); 
  gfits_scan (&header, "NAXIS2",   "%hd", 1, &image[0].NY);
 
  /* get photcode from header */
  if (photcode == 0) {
    if (!gfits_scan (&header, "PHOTCODE", "%s", 1, photname)) {
      fprintf (stderr, "ERROR: photcode not supplied in header\n");
      exit (1);
    }
    photcode = GetPhotcodeCodebyName (photname);
    if (photcode == 0) {
      fprintf (stderr, "ERROR: photcode %s not found in photcode table\n", photname);
      exit (1);
    }
  }
  if (photcode == 0) {
    fprintf (stderr, "ERROR: no valid photcode is supplied\n");
    exit (1);
  }
  image[0].photcode = photcode;

  image[0].NX -= XOVERSCAN;
  image[0].NY -= YOVERSCAN;
  gfits_scan (&header, ExptimeKeyword,  "%lf", 1, &tmp);
  image[0].exptime = tmp;
  
  /*** why are we no longer using APMIFIT?? ***/
  tmp = 0;
  /* gfits_scan (&header, "APMIFIT",  "%lf", 1, &tmp); */
  image[0].apmifit = tmp;

  tmp = 0;
  /* gfits_scan (&header, "dAPMIFIT", "%lf", 1, &tmp); */
  image[0].dapmifit = tmp;

  tmp = 0;
  gfits_scan (&header, "FLIMIT",   "%lf", 1, &tmp);
  image[0].detection_limit = tmp * 10.0;

  tmp = 0;
  gfits_scan (&header, "FSATUR",   "%lf", 1, &tmp);
  image[0].saturation_limit = tmp * 10.0;

  tmp = 0;
  gfits_scan (&header, "FWHM_X",   "%lf", 1, &tmp);
  image[0].fwhm_x = tmp * 25.0 * image[0].coords.cdelt1 * 3600.0;

  tmp = 0;
  gfits_scan (&header, "FWHM_Y",   "%lf", 1, &tmp);
  image[0].fwhm_y = tmp * 25.0 * image[0].coords.cdelt1 * 3600.0;

  if (!gfits_scan (&header, "TZERO",   "%d",  1, &image[0].tzero)) {
    image[0].tzero = parse_time (&header);
  }

  if (STKeyword[0]) {
    /* get ST (used for airmass calculation) */
    gfits_scan (&header, STKeyword, "%s", 1, line);
    /* remove ':' characters */
    for (c = strchr (line, ':'); c != (char *) NULL; c = strchr (line, ':')) { *c = ' '; }
    sscanf (line, "%d %d %lf", &hour, &min, &sec);
    image[0].sidtime = hour + min/60.0 + sec/3600.0;
  } else {
    double jd;
    jd = sec_to_jd (image[0].tzero);
    image[0].sidtime  = ohana_lst (jd, Longitude);
  }
  image[0].latitude = Latitude;

  tmp = 0;
  gfits_scan (&header, "TRATE",   "%lf", 1, &tmp);
  image[0].trate = 10000 * tmp;

  tmp = 0;
  gfits_scan (&header, AirmassKeyword, "%lf", 1, &tmp);
  image[0].secz = MIN (NO_MAG, tmp);

  if (!gfits_scan (&header, CCDNumKeyword, "%d", 1, &itmp)) {
    image[0].ccdnum = 0xff;
  } else {
    image[0].ccdnum = itmp;
  }

  gfits_scan (&header, "ZERO_PT", "%lf", 1, &ZeroPt);
  SetZeroPoint (ZeroPt);

  /* secz is in units milli-airmass */
  image[0].Mcal = 0.0;
  image[0].Xm   = NO_MAG;
  image[0].code = 0;
  bzero (image[0].dummy, sizeof(image[0].dummy));

  /* find expected number of stars */
  if (!gfits_scan (&header, "NSTARS", "%d", 1, &image[0].nstar)) {
    fprintf (stderr, "ERROR: can't get NSTARS from header\n");
    exit (1);
  }

