# include "getstar.h"
# define NCHAR 66 /* 65 char EXCLUDING return */

int wstars (char *filename, Stars *stars, int Nstars, Image *image) {

  /* construct a header from the image structure */
  /* write out header */
  /* write out data lines */

  int i, Nchar, Ny, mode;
  double scale;
  char *photcode, *c, *py, *pm, *pd, datestr[64], timestr[64];
  struct tm *gmt;
  Header header;
  FILE *f;
  char line[NCHAR];

  fits_init_header (&header);
  header.Naxis[0] = image[0].NX + XOVERSCAN;
  header.Naxis[1] = image[0].NY + YOVERSCAN;
  header.bitpix   = 16;
  header.Naxes    = 2;
  fits_create_header (&header);

  /* astrometric data */
  fits_print (&header, "CTYPE1",  "%s", 1, "RA---SIN");
  fits_print (&header, "CTYPE2",  "%s", 1, "DEC--SIN");

  fits_print (&header, "CRVAL1", "%f", 1, image[0].coords.crval1);
  fits_print (&header, "CRVAL2", "%f", 1, image[0].coords.crval2);

  fits_print (&header, "CRPIX1", "%f", 1, image[0].coords.crpix1);
  fits_print (&header, "CRPIX2", "%f", 1, image[0].coords.crpix2);

  fits_print (&header, "PC001001",  "%f", 1, image[0].coords.pc1_1);
  fits_print (&header, "PC001002",  "%f", 1, image[0].coords.pc1_2);
  fits_print (&header, "PC002001",  "%f", 1, image[0].coords.pc2_1);
  fits_print (&header, "PC002002",  "%f", 1, image[0].coords.pc2_2);

  fits_print (&header, "CDELT1",  "%f", 1, image[0].coords.cdelt1);
  fits_print (&header, "CDELT2",  "%f", 1, image[0].coords.cdelt2);
  
  fits_print (&header, "NASTRO", "%d", 1, 1); /* TBD */
  fits_print (&header, "EQUINOX", "%f", 1, 2000.0); /* TBD */
  
  fits_print (&header, "CERROR", "%f", 1, 0.02*image[0].cerror);

  photcode = GetPhotcodeNamebyCode (image[0].source);
  fits_print (&header, "PHOTCODE", "%s", 1, photcode);

  fits_print (&header, ExptimeKeyword, "%f", 1, image[0].exptime);
  
  fits_print (&header, "FLIMIT",   "%lf", 1, 0.1*image[0].detection_limit);
  fits_print (&header, "FSATUR",   "%lf", 1, 0.1*image[0].saturation_limit);

  scale = image[0].coords.cdelt1*25.0*3600.0;
  fits_print (&header, "FWHM_X",   "%lf", 1, image[0].fwhm_x / scale);
  fits_print (&header, "FWHM_Y",   "%lf", 1, image[0].fwhm_y / scale);
	      
  fits_print (&header, "TRATE",   "%lf", 1, image[0].trate/10000.0);
  fits_print (&header, AirmassKeyword, "%lf", 1, 0.001*image[0].secz);

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

  /* parse date-mode line */
  uppercase (DateMode);
  for (Ny = 0, c = strchr (DateMode, 'Y'); c != (char ) NULL; c = strchr (c + 1, 'Y'), Ny++);
  if ((Ny != 2) && (Ny != 4)) {
    fprintf (stderr, "error in DATE-MODE format: %s\n", DateMode);
    exit (1);
  }
  py = strchr (DateMode, 'Y');
  pm = strchr (DateMode, 'M');
  pd = strchr (DateMode, 'D');
  if ((py == (char *) NULL) || (pm == (char *) NULL) || (pd == (char *) NULL)) {
    fprintf (stderr, "error in DATE-MODE format: %s\n", DateMode);
    exit (1);
  }
  if ((py > pm) && (py < pd)) {
    fprintf (stderr, "error in DATE-MODE format: %s\n", DateMode);
    exit (1);
  }
  if ((py > pd) && (py < pm)) {
    fprintf (stderr, "error in DATE-MODE format: %s\n", DateMode);
    exit (1);
  }
  mode = 0;
  if ((py < pm) && (pm < pd)) { mode = 1; }  /* yyyy-mm-dd */
  if ((py < pm) && (pm > pd)) { mode = 2; }  /* yyyy-dd-mm */
  if ((py > pm) && (pm < pd)) { mode = 3; }  /* mm-dd-yyyy */
  if ((py > pm) && (pm > pd)) { mode = 4; }  /* dd-mm-yyyy */
  if (!mode) {
    fprintf (stderr, "error in DATE-MODE format: %s\n", DateMode);
    exit (1);
  }

  gmt = gmtime ((unsigned long *)&image[0].tzero);

  switch (mode) {
  case 1:
    sprintf (datestr, "%04d/%02d/%02d", 1900 + gmt[0].tm_year, gmt[0].tm_mon+1, gmt[0].tm_mday);
    break;
  case 2:
    sprintf (datestr, "%04d/%02d/%02d", 1900 + gmt[0].tm_year, gmt[0].tm_mday, gmt[0].tm_mon+1);
    break;
  case 3:
    sprintf (datestr, "%02d/%02d/%04d", gmt[0].tm_mon+1, gmt[0].tm_mday, 1900 + gmt[0].tm_year);
    break;
  case 4:
    sprintf (datestr, "%02d/%02d/%04d", gmt[0].tm_mday, gmt[0].tm_mon+1, 1900 + gmt[0].tm_year);
    break;
  }
  sprintf (timestr, "%02d:%02d:%02d", gmt[0].tm_hour, gmt[0].tm_min, gmt[0].tm_sec); 
  fits_print (&header, UTKeyword, "%s", 1, timestr);
  fits_print (&header, DateKeyword, "%s", 1, datestr);

  fits_print (&header, "ZERO_PT", "%lf", 1, ZeroPt);
  fits_print (&header, "NSTARS", "%d", 1, Nstars);
  
  f = fopen (filename, "w");
  if (f == (FILE *) NULL) {
    fprintf (stderr, "ERROR: can't create output file %s\n", filename);
    exit (1);
  }

  fwrite (header.buffer, 1, header.size, f);

  for (i = 0; i < Nstars; i++) {
    Nchar = snprintf (line, NCHAR, "%6.1f %6.1f %6.3f %03d %2d %3.1f %6.3f %6.3f %6.2f %6.2f %5.1f", 
		      stars[i].X, stars[i].Y, stars[i].M, 
		      (int)(stars[i].dM), stars[i].dophot, stars[i].sky, 
		      stars[i].Mgal, stars[i].Map, stars[i].fx, stars[i].fy, stars[i].df);
    fprintf (f, "%s\n", line);
  }
  fclose (f);
}

uppercase (char *string) {

  int i;
    
  for (i = 0; i < strlen (string); i++) string[i] = toupper (string[i]);

}
