# include "astro.h"

main (argc, argv)
int argc;
char **argv;
{

  int i, N1, N2, Ncat, Nlast, Nused;
  double dx, dy, Rx, Ry;
  double RA, DEC, dRA, dDEC;
  double *ra, *dec, *mag;
  double X_O, X_X, X_Y, dX, Y_O, Y_X, Y_Y, dY;
  char *c, *ref, filename[200], head[200], obj[200], line[200];
  Stars *stars1, *stars2;

  args (&argc, argv);

  Nlast = Ncat = 0;
  for (i = 1; i < argc; i++) {
    extract_catalog (argv[i], &ra, &dec, &mag, &Ncat);
    fprintf (stderr, "catalog %s, %d stars (%d total)\n", argv[i], Ncat-Nlast, Ncat);
    Nlast = Ncat;
  }
  
  if (DUMP) {
    for (i = 0; i < Ncat; i++) {
      fprintf (stdout, "%d %f %f %f\n", i, ra[i], dec[i], mag[i]);
    }
    exit (0);
  }

  fprintf (stderr, "enter filenames, followed by EOF\n");
  while (fscanf (stdin, "%s", filename) != EOF) {
    /**** See comment 1 below ****/
    strcpy (head, filename);
    if ((c = strrchr(head, '.')) != NULL)
      strcpy(strrchr(head, '.'), ".head");
    else 
      strcat(head, ".head");
    strcpy (obj, filename);
    if ((c = strrchr(obj, '.')) != NULL)
      strcpy(strrchr(obj, '.'), ".obj_out");
    else 
      strcat(obj, ".obj_out");

    stats (head, &RA, &DEC, &dRA, &dDEC, ASEC_PIX);
    if (FORCE) 
      stars1 = get_catalog_stars (ra, dec, mag, Ncat, F_RA, F_DEC, dRA, dDEC, &N1);
    else {
      stars1 = get_catalog_stars (ra, dec, mag, Ncat, RA, DEC, dRA, dDEC, &N1);
    }
    N2 = N2NUMBER;
    stars2 = get_stars (obj, &N2);
    if (N2 >= 3) {
      if (FORCE)
	Rx = ASEC_PIX / (3600 * cos (F_DEC*RAD_DEG));
      else
	Rx = ASEC_PIX / (3600 * cos (DEC*RAD_DEG));
      Ry = ASEC_PIX / 3600;
      delta (stars1, stars2, N1, N2, &dx, &dy, &Rx, &Ry);
      fprintf (stderr, "USING: %f %f %f %f\n", dx, dy, Rx, Ry);
      Nused = line_fit (stars1, stars2, N1, N2, dx, dy, Rx, Ry,
		&X_O, &X_X, &X_Y, &dX, &Y_O, &Y_X, &Y_Y, &dY);
      alter_header (head, argv, argc, X_O, X_X, X_Y, dX, Y_O, Y_X, Y_Y, dY, Nused);
    }
    else {
      fprintf (stderr, "too few stars in %s, skipped\n", filename);
    }
    free (stars2);
    free (stars1);  
  }
}




    /* comment 1:
       assign the assumed names: 
       filename.head, filename.obj_out 
       if filename is of the form xxxx.xxx, cut off
       extention before attaching .head, .obj_out
       this entire problem can be removed by making the 
       Dophot output file contain the header info from
       the image, but until this is the Dophot output
       format... */
