# include "gastro.h"
# define DEBUG 0
double BigDecBounds[] = {0.0, 7.5, 15.0, 22.5, 30.0, 37.5, 45.0, 
			 52.5, 60.0, 67.5, 75.0, 82.5, 90.0,
			 0.0, -7.5, -15.0, -22.5, -30.0, -37.5, -45.0, 
			 -52.5, -60.0, -67.5, -75.0, -82.5, -90.0};

double Areas[] = {2700.0, 2665.4505, 2563.567, 2399.5344, 2181.6343, 1920.7299, 1629.5942, 1322.1251, 1012.5, 714.32555, 439.83856, 199.21043, 0.0,
		  2700.0, 2665.4505, 2563.567, 2399.5344, 2181.6343, 1920.7299, 1629.5942, 1322.1251, 1012.5, 714.32555, 439.83856, 199.21043, 0.0};

char *DecSections[] = {"N0000", "N0730", "N1500", "N2230", "N3000", "N3730", "N4500", 
		       "N5230", "N6000", "N6730", "N7500", "N8230", "weirdness", 
		       "S0000", "S0730", "S1500", "S2230", "S3000", "S3730", "S4500", 
		       "S5230", "S6000", "S6730", "S7500", "S8230", "weirdness"};

char *Dec2Sections[] = {"n0000", "n0730", "n1500", "n2230", "n3000", "n3730", "n4500", 
			"n5230", "n6000", "n6730", "n7500", "n8230", "weirdness", 
			"s0000", "s0730", "s1500", "s2230", "s3000", "s3730", "s4500", 
			"s5230", "s6000", "s6730", "s7500", "s8230", "weirdness"};

char *disk[] = {"disk 1", "disk 1", "disk 1", "disk 1", "disk 1", "disk 1", "disk 1", 
		"disk 1", "disk 1", "disk 1", "disk 1", "disk 1", "weirdness", 
		"disk 1", "disk 2", "disk 2", "disk 2", "disk 2", "disk 2", "disk 2", 
		"disk 2", "disk 2", "disk 2", "disk 2", "disk 2", "weirdness"};

int NBigRASections [] = {48, 47, 45, 43, 40, 36, 32, 28, 21, 15, 9, 3, 3, 48, 47, 45, 43, 40, 36, 32, 28, 21, 15, 9, 3, 3};

int NDecLines[] = {593, 584, 551, 530, 522, 465, 406, 362, 280, 198, 123, 24, 
                   0, 597, 578, 574, 577, 534, 499, 442, 376, 294, 212, 144, 48};

char **gregions (CatStats *catstats, int *Nregions) {
  
  char buffer[28800], temp[50], file[50], line[50];
  char *p, prefix[1024];
  char **regions;
  FILE *f;
  double dBigRA, RA, DEC, tmp, dr, dd, dec, ra, Ra;
  double BigRABounds[2], RA0, RA1, DEC0, DEC1;
  int i, NBigDec, NBigRA, NBig, VMS, N;
  int NRA, NDEC, NLINES, done, NBLOCKS, nregion, NREGION;
  
  f = fopen (GSCFILE, "r");
  if (f == NULL) {
    fprintf (stderr, "ERROR: can't find GSC regions file %s\n", GSCFILE);
    exit (0);
  }
  
  /* create space to save names */
  nregion = 0;
  NREGION = 50;
  ALLOCATE (regions, char *, NREGION);

  /* smallest region files are 2.0 deg tall */
  /* uses arbitrary distance (3.0 deg) for width of image */
  dd = dr = 0.5;
  for (Ra = catstats[0].RA[0]; Ra < catstats[0].RA[1] + dr; Ra += dr) {
    for (dec = catstats[0].DEC[0]; dec < catstats[0].DEC[1] + dd; dec += dd) {
      ra = Ra;
      while (ra < 0) { ra += 360.0; }
      while (ra >= 360.0) { ra -= 360.0; }
      
      /* use the pole regions, if near pole */
      if (dec > 86.25) {
	sprintf (file, "%s/n8230/pole.cpt\0", GSC_DIR);
	for (i = 0; i < nregion; i++) {
	  if (!strcmp (regions[i], file)) {
	    goto skip1;
	  }
	}
	regions[nregion] = strcreate (file);
	nregion ++;
	if (nregion == NREGION) {
	  NREGION += 50;
	  REALLOCATE (regions, char *, NREGION);
	}
      skip1:
	continue;
      }

      /* find large DEC region (directory) */
      NBigDec = -1;
      for (i = 0; i < 12; i++) {
	if ((dec >= BigDecBounds[i]) && (dec < BigDecBounds[i+1])) {
	  NBigDec = i;
	  break;
	}
      }
      if (NBigDec < 0) {
	for (i = 13; i < 24; i++) {
	  if ((dec < BigDecBounds[i]) && (dec >= BigDecBounds[i+1])) {
	    NBigDec = i;
	    break;
	  }
	}
      }
      if (NBigDec < 0) {
	fprintf (stderr, "ERROR: Dec out of range: %f\n", dec);
	exit (0);
      }
      
      /* count lines before section */
      NLINES = 0;
      for (i = 0; i < NBigDec; i++) {
	NLINES += NDecLines[i];
      }
      fseek (f, 5*2880 + 48*NLINES, SEEK_SET);
      
      /* should be in this section.  if not, there is a problem counting... */
      done = FALSE;
      fread (buffer, 48*NDecLines[NBigDec], 1, f);
      for (i = 0; !done && (i < NDecLines[NBigDec]); i++) {
	strncpy (temp, &buffer[i*48], 48);
	temp[49] = 0;
	hms_to_deg (&RA0, &RA1, &DEC0, &DEC1, &temp[7]);
	if (RA1 < RA0) RA1 += 360.0;
	if ((dec >= 0) && (dec >= DEC0) && (dec < DEC1) && (ra >= RA0) && (ra < RA1)) {
	  done = TRUE;
	}
	if ((dec < 0) && (dec < DEC0) && (dec >= DEC1) && (ra >= RA0) && (ra < RA1)) {
	  done = TRUE;
	}
      }
      if (!done) {
	fprintf (stderr, "ERROR: can't find coords in region file: %f %f\n", ra, dec);
	exit (0);
      }
      temp[5] = 0;
      sprintf (file, "%s/%s/%s.cpt\0", GSC_DIR, Dec2Sections[NBigDec],&temp[1]);
      for (i = 0; i < nregion; i++) {
	if (!strcmp (regions[i], file)) {
	  goto skip2;
	}
      }
      regions[nregion] = strcreate (file);
      nregion ++;
      if (nregion == NREGION) {
	NREGION += 50;
	REALLOCATE (regions, char *, NREGION);
      }
    skip2:
    }
  }
  fclose (f);

  *Nregions = nregion;
  return (regions);
  
}



/**********/
int hms_to_deg (h0, h1, d0, d1, string) 
     char *string;
     double *h0, *h1, *d0, *d1;
{
  
  char *c;
  int i, flag_d0, flag_d1, flag_h0, flag_h1;
  double tmp;
  
  *d0 = *h0 = *d1 = *h1 = 0;

  flag_h0 = dparse (h0, 1, string);
  flag_h1 = dparse (h1, 4, string);
  flag_d0 = dparse (d0, 7, string);
  flag_d1 = dparse (d1, 9, string);
  *h0 *= flag_h0;
  *h1 *= flag_h1;
  *d0 *= flag_d0;
  *d1 *= flag_d1;

  dparse (&tmp, 2, string);
  *h0 += tmp/60.0;
  dparse (&tmp, 3, string);
  *h0 += tmp/3600.0;
  
  dparse (&tmp, 5, string);
  *h1 += tmp/60.0;
  dparse (&tmp, 6, string);
  *h1 += tmp/3600.0;
  
  dparse (&tmp, 8, string);
  *d0 += tmp/60.0;

  dparse (&tmp, 10, string);
  *d1 += tmp/60.0;

  *h0 *= 15*flag_h0;
  *h1 *= 15*flag_h1;
  *d0 *= flag_d0;
  *d1 *= flag_d1;

  return (TRUE);
}


/*
      if (buffer[i*48 + 19] == ' ') continue;
      strncpy (temp, &buffer[i*48 + 19], 20);       temp[20] = 0;
      hms_to_deg (&RA,  &DEC, temp, ' ', 3);


  dBigRA = 360.0 / (int)(0.5 + 48*cos(3.1415927*0.5*(BigDecBounds[NBigDec] + BigDecBounds[NBigDec + 1])/180.0));

  NBigRA = ra / dBigRA;

  NBig = NBigRA;
  for (i = 0; (i < 12) && (i < NBigDec); i++) {
    NBig += NBigRASections[i];
  }
  for (i = 13; (i < 24) && (i < NBigDec); i++) {
    NBig += NBigRASections[i];
  }


  fprintf (stderr, "%d %d %d %d %f %f -> %f %f %f\n", NBigDec, NBigRA, NBigRASections[NBigDec], NBig, ra, dec, BigDecBounds[NBigDec], BigDecBounds[NBigDec + 1], dBigRA);
*/
