# include "addstar.h"
# define NZONE 180
# define NBYTE 207
# define NITEM 100

static SkyTable *tychoTable = NULL;

Stars *gettycho (SkyRegion *catstats, int photcode, int *nstars) {

  int i, j, nitems;
  char *buffer;
  char filename[128], *line;
  FILE *f;
  int NTYCHO, Ntycho;
  int firstRow, firstByte;
  short int TYCHO_B, TYCHO_V;
  e_time TychoEpoch;
  SkyRegion *region;
  SkyList  *skylist;
  Stars *stars;

  /* require photcode */
  NAMED_PHOTCODE (TYCHO_B, "TYCHO_B");
  NAMED_PHOTCODE (TYCHO_V, "TYCHO_V");
  if (photcode == TYCHO_B) goto good_code;
  if (photcode == TYCHO_V) goto good_code;
  Shutdown ("TYCHO photcode not specified");
good_code:

  fprintf (stderr, "loading TYCHO catalog\n");

  /* load tycho index file into sky table */
  if (tychoTable == NULL) {
    sprintf (filename, "%s/index.dat", TYCHO_DIR);
    tychoTable = SkyTableFromTychoIndex (filename, VERBOSE);
    if (tychoTable == NULL) {
      fprintf (stderr, "ERROR: problem loading tycho data\n");
      exit (2);
    }
  }

  /* identify tycho region files overlapping the requested region */
  skylist = SkyListByPatch (tychoTable, 2, catstats);

  /* open the Tycho catalog file */
  sprintf (filename, "%s/tycho.dat", TYCHO_DIR);
  f = fopen (filename, "r");

  Ntycho = 0;
  NTYCHO = 5000;
  ALLOCATE (stars, Stars, NTYCHO);

  ALLOCATE (buffer, char, NITEM*NBYTE);

  TychoEpoch = ohana_date_to_sec ("1991/04/02,07:30:00");

  for (i = 0; i < skylist[0].Nregions; i++) {
    region = skylist[0].regions[i];
    if (VERBOSE) fprintf (stderr, "section %d (%f - %f, %f - %f)...", i, 
			  region[0].Rmin, region[0].Rmax, region[0].Dmin, region[0].Dmax);

    firstRow  = atoi (region[0].name);
    firstByte = firstRow * NBYTE;
    fseek (f, firstByte, SEEK_SET);

    while (1) {
      nitems = fread (buffer, NBYTE, NITEM, f);
      if (nitems == 0) break;

      for (j = 0; j < nitems; j++) {
	line = &buffer[j*NBYTE];
	stars[Ntycho].R = atof (&line[15]);
	stars[Ntycho].D = atof (&line[28]);

	if (stars[Ntycho].R < region[0].Rmin) goto next_section;
	if (stars[Ntycho].R > region[0].Rmax) goto next_section;
	if (stars[Ntycho].D < region[0].Dmin) goto next_section;
	if (stars[Ntycho].D > region[0].Dmax) goto next_section;

	if (stars[Ntycho].R < UserPatch.Rmin) continue;
	if (stars[Ntycho].R > UserPatch.Rmax) continue;
	if (stars[Ntycho].D < UserPatch.Dmin) continue;
	if (stars[Ntycho].D > UserPatch.Dmax) continue;

	stars[Ntycho].dR  = 1000 * atof (&line[57]);
	stars[Ntycho].dD  = 1000 * atof (&line[64]);

	/* XXX : we need to apply uR,uD to R,D to advance to 2000.0 */
	stars[Ntycho].uR  = atof (&line[41]);
	stars[Ntycho].uD  = atof (&line[49]);

	stars[Ntycho].duR = atof (&line[69]);
	stars[Ntycho].duD = atof (&line[75]);
	
	stars[Ntycho].P   = 0;
	stars[Ntycho].dP  = 0;

	stars[Ntycho].detID   = 0;
	stars[Ntycho].imageID = 0;

	/* Tycho uses J2000 equinox and 1991.25 epoch for coordinates */
	/* the magnitudes have no temporal information */ 
	stars[Ntycho].t   = TychoEpoch;
	stars[Ntycho].found = -1;
      
	/* one pass of addstar does either r or b */
	if (photcode == TYCHO_B) {
	  stars[Ntycho].M     = atof (&line[110]);
	  stars[Ntycho].dM    = atof (&line[117]);
	  stars[Ntycho].code  = TYCHO_B;
	} else {
	  stars[Ntycho].M     = atof (&line[123]);
	  stars[Ntycho].dM    = atof (&line[130]);
	  stars[Ntycho].code  = TYCHO_V;
	}	
      
	Ntycho ++;
	CHECK_REALLOCATE (stars, Stars, NTYCHO, Ntycho, 5000);
      }

      if (nitems != NITEM) break;
    }
  next_section:
    if (VERBOSE) fprintf (stderr, "%d stars\n", Ntycho);
  }
  fclose (f);

  SkyListFree (skylist);

  free (buffer);
  REALLOCATE (stars, Stars, MAX (1, Ntycho));

  *nstars = Ntycho;
  if (VERBOSE) fprintf (stderr, "%d stars from Tycho\n", Ntycho);
  return (stars);
}
