# include <ohana.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};
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, 48, 
                   0, 597, 578, 574, 577, 534, 499, 442, 376, 294, 212, 144, 48};

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

  /* USAGE: regions ra dec -- returns filenumber of appropriate GSC region */

  char buffer[2880], temp[50], file[50], line[50];
  char *p, prefix[1024];
  FILE *f;
  double ra, dec, dBigRA, RA, DEC, tmp;
  double BigRABounds[2], RA0, RA1, DEC0, DEC1;
  int i, NBigDec, NBigRA, NBig, VMS, N;
  int NRA, NDEC, NLINES, done, NBLOCKS;

  VMS = FALSE;
  if (N = get_argument (argc, argv, "-vms")) {
    remove_argument (N, &argc, argv);
    VMS = TRUE;
  }

  strcpy (prefix, "/cdrom/");
  if (N = get_argument (argc, argv, "-pre")) {
    remove_argument (N, &argc, argv);
    strcpy (prefix, argv[N]);
    remove_argument (N, &argc, argv);
  }

  if (argc != 2) {
    fprintf (stderr, "USAGE: %s (regions.tbl) [-pre (prefix)] [-vms]\n", argv[0]);
    exit (0);
  }

  while (fscanf (stdin, "%lf %lf", &ra, &dec) != EOF) {
    f = fopen (argv[1], "r");
    if (f == NULL) {
      fprintf (stderr, "could not open GSC regions file %s\n", argv[1]);
      exit (0);
    }
    fread (buffer, 2880, 1, f);
    fread (buffer, 2880, 1, f);
    fread (buffer, 2880, 1, f);
    fread (buffer, 2880, 1, f);
    fread (buffer, 2880, 1, f);
    
    while (ra < 0) { ra += 360.0; }
    while (ra >= 360.0) { ra -= 360.0; }
    
    NBigDec = -1;
    for (i = 0; i < 12; i++) {
      (DEBUG) && fprintf (stderr, "%d %f %f %f\n", i, dec, BigDecBounds[i], BigDecBounds[i+1]);
      if ((dec >= BigDecBounds[i]) && (dec <= BigDecBounds[i+1])) {
	NBigDec = i;
	break;
      }
    }
    if (NBigDec < 0) {
      for (i = 13; i < 24; i++) {
	(DEBUG) && fprintf (stderr, "%d %f %f %f\n", i, dec, BigDecBounds[i], BigDecBounds[i+1]);
	if ((dec <= BigDecBounds[i]) && (dec >= BigDecBounds[i+1])) {
	  NBigDec = i;
	  break;
	}
      }
    }
    if (NBigDec < 0) {
      fprintf (stderr, "dec out of range: %f\n", dec);
    }
    
    NLINES = 0;
    for (i = 0; i < NBigDec; i++) {
      NLINES += NDecLines[i];
    }
    NBLOCKS = (int) (48*NLINES / 2880);
    for (i = 0; i < NBLOCKS; i++) {
      fread (buffer, 2880, 1, f);
    }
    
    done = FALSE;
    while (!done) {
      fread (buffer, 2880, 1, f);
      for (i = 0; !done && (i < 60); i++) {
	strncpy (temp, &buffer[i*48], 48);
	temp[49] = 0;
	hstgsc_hms_to_deg (&RA0, &RA1, &DEC0, &DEC1, &temp[7]);
	(DEBUG) && fprintf (stderr, "%f %f %f    %f %f %f  %s\n", dec, DEC0, DEC1, ra, RA0, RA1, temp);
	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 (VMS) 
      sprintf (file, "%s %sGSC/%s/%c%c%c%c.GSC\\;1\0", prefix, disk[NBigDec], DecSections[NBigDec],
	       temp[1], temp[2], temp[3], temp[4]);
    else 
      sprintf (file, "%s %sgsc/%s/%c%c%c%c.gsc\0", disk[NBigDec], prefix, Dec2Sections[NBigDec],
	       temp[1], temp[2], temp[3], temp[4]);

    fprintf (stdout, "%s\n", file);

    fclose (f);
    
  }

}
