Index: /trunk/Ohana/src/addstar/Makefile
===================================================================
--- /trunk/Ohana/src/addstar/Makefile	(revision 7682)
+++ /trunk/Ohana/src/addstar/Makefile	(revision 7683)
@@ -27,5 +27,4 @@
 $(SRC)/build_links.$(ARCH).o \
 $(SRC)/calibrate.$(ARCH).o \
-$(SRC)/check_permissions.$(ARCH).o \
 $(SRC)/ConfigInit.$(ARCH).o \
 $(SRC)/dump.$(ARCH).o \
@@ -39,4 +38,5 @@
 $(SRC)/get2mass.$(ARCH).o \
 $(SRC)/get2mass_as.$(ARCH).o \
+$(SRC)/get2mass_as_raw.$(ARCH).o \
 $(SRC)/get2mass_dr2.$(ARCH).o \
 $(SRC)/getgsc.$(ARCH).o \
@@ -78,10 +78,16 @@
 $(SRC)/mkacc-2mass.$(ARCH).o \
 $(SRC)/image-db.$(ARCH).o \
-$(SRC)/check_permissions.$(ARCH).o \
-$(SRC)/ConfigInit.$(ARCH).o \
-$(SRC)/SetSignals.$(ARCH).o \
-$(SRC)/conversions.$(ARCH).o \
+$(SRC)/ConfigInit.$(ARCH).o \
+$(SRC)/SetSignals.$(ARCH).o \
 $(SRC)/wimage.$(ARCH).o \
 $(SRC)/get2mass_as.$(ARCH).o
+
+SCAN-2MASS = \
+$(SRC)/scan2mass.$(ARCH).o \
+$(SRC)/scan2mass_as_data.$(ARCH).o \
+$(SRC)/get2mass_as.$(ARCH).o \
+$(SRC)/ConfigInit.$(ARCH).o \
+$(SRC)/Shutdown.$(ARCH).o \
+$(SRC)/SetSignals.$(ARCH).o
 
 EXTRA = \
@@ -132,5 +138,4 @@
 $(SRC)/Shutdown.$(ARCH).o \
 $(SRC)/edge_check.$(ARCH).o \
-$(SRC)/check_permissions.$(ARCH).o \
 $(SRC)/opening_angle.$(ARCH).o \
 $(SRC)/NewImage.$(ARCH).o \
@@ -180,5 +185,4 @@
 $(SRC)/Shutdown.$(ARCH).o \
 $(SRC)/edge_check.$(ARCH).o \
-$(SRC)/check_permissions.$(ARCH).o \
 $(SRC)/opening_angle.$(ARCH).o \
 $(SRC)/SkyTableFromTychoIndex.$(ARCH).o \
@@ -227,4 +231,8 @@
 mkacc-2mass.install        : $(DESTBIN)/mkacc-2mass
 
+scan2mass                : $(BIN)/scan2mass.$(ARCH)
+$(BIN)/scan2mass.$(ARCH) : $(SCAN-2MASS)
+scan2mass.install        : $(DESTBIN)/scan2mass
+
 all: addstar addstarc addstard addstart 
 
Index: /trunk/Ohana/src/addstar/include/2mass.h
===================================================================
--- /trunk/Ohana/src/addstar/include/2mass.h	(revision 7682)
+++ /trunk/Ohana/src/addstar/include/2mass.h	(revision 7683)
@@ -7,4 +7,10 @@
 } TM_Region;
 
+typedef struct {
+  double Rmin, Rmax, Dmin, Dmax;
+  int index[20];
+  int Nindex;
+} TMBands;
+
 short TM_J, TM_H, TM_K;
 
@@ -12,2 +18,9 @@
 Stars *get2mass_2DR_data (SkyRegion *region, char *filename, SkyRegion *patch, int photcode, int *nstars);
 Stars *get2mass_AS_data (SkyRegion *region, char *filename, SkyRegion *patch, int phocode, int *nstars);
+Stars *get2mass_AS_rawdata (SkyRegion *region, char *filename, SkyRegion *patch, int phocode, int *nstars);
+
+SkyTable *scan2mass_acc (char *path, char *accel);
+int scan2mass_as_data (char *filename);
+
+char *skipNbounds (char *line, char bound, int Nbound, int Nbyte);
+e_time get2mass_time (char *ptr, char *buffer, int Nbound, int Nbyte);
Index: /trunk/Ohana/src/addstar/include/addstar.h
===================================================================
--- /trunk/Ohana/src/addstar/include/addstar.h	(revision 7682)
+++ /trunk/Ohana/src/addstar/include/addstar.h	(revision 7683)
@@ -219,2 +219,5 @@
 int NewRefcat_Thread (int BindSocket);
 int NewReflist_Thread (int BindSocket);
+
+// this is a gnu extension?? caution!
+void *memrchr(const void *s, int c, size_t n);
Index: /trunk/Ohana/src/addstar/src/get2mass.c
===================================================================
--- /trunk/Ohana/src/addstar/src/get2mass.c	(revision 7682)
+++ /trunk/Ohana/src/addstar/src/get2mass.c	(revision 7683)
@@ -5,8 +5,10 @@
   
   char *path;
-  int i, Nstars, Nrefcat; 
+  char gzname[1024];
+  int i, status, Nstars, Nrefcat; 
   Stars    *stars;
   Stars    *refcat;
   SkyTable *sky;
+  struct stat filestat;
 
   NAMED_PHOTCODE (TM_J, "2MASS_J");
@@ -22,4 +24,6 @@
   if (mode == 1) path = TWO_MASS_DIR_DR2;
 
+  // the accel.dat file has the raw filenames
+  // test if the file exists, or else try the .gz version
   sky = get2mass_acc (patch, path, "accel.dat");
   
@@ -32,6 +36,12 @@
       case 0:
 	// XXX put filename from table here
-	refcat = get2mass_AS_data (&sky[0].regions[i], sky[0].filename[i], patch, photcode, &Nrefcat);
-	if (VERBOSE) fprintf (stderr, "loaded %d stars from 2MASS (allsky)\n", Nrefcat);
+	status = stat (sky[0].filename[i], &filestat);
+	if ((status == -1) && (errno == ENOENT)) {
+	  sprintf (gzname, "%s.gz", sky[0].filename[i]);
+	  refcat = get2mass_AS_data (&sky[0].regions[i], gzname, patch, photcode, &Nrefcat);
+	} else {
+	  refcat = get2mass_AS_rawdata (&sky[0].regions[i], sky[0].filename[i], patch, photcode, &Nrefcat);
+	}
+	if (VERBOSE) fprintf (stderr, "loaded %d stars from 2MASS (allsky) : %s\n", Nrefcat, sky[0].filename[i]);
 	break;
       case 1:
@@ -53,8 +63,8 @@
 }  
 
-/* watch for patch which cross 0,360 boundary */
+/* watch for patches which cross 0,360 boundary */
 SkyTable *get2mass_acc (SkyRegion *patch, char *path, char *accel) {
 
-  int Nregions, NREGIONS;
+  int Nregions, NREGIONS, Nrec;
   char accelfile[1024], line[256], filename[128], datafile[256], **filenames;
   FILE *f;
@@ -78,5 +88,5 @@
     if (line[0] == 0) continue;
     if (line[0] == '#') continue;
-    sscanf (line, "%s %lf %lf %lf %lf", filename, &Rs, &Re, &Ds, &De);
+    sscanf (line, "%s %lf %lf %lf %lf %d", filename, &Rs, &Re, &Ds, &De, &Nrec);
     Rs *= 15.0;
     Re *= 15.0;
@@ -89,4 +99,6 @@
     regions[Nregions].Dmin = Ds;
     regions[Nregions].Dmax = De;
+    regions[Nregions].childE = Nrec; // a cheat since 2MASS only has one depth
+    fprintf (stderr, "choosing: %10.6f - %10.6f, %10.6f - %10.6f\n", Rs, Re, Ds, De);
 
     sprintf (datafile, "%s/%s", path, filename);
Index: /trunk/Ohana/src/addstar/src/get2mass_as.c
===================================================================
--- /trunk/Ohana/src/addstar/src/get2mass_as.c	(revision 7682)
+++ /trunk/Ohana/src/addstar/src/get2mass_as.c	(revision 7683)
@@ -8,8 +8,4 @@
 /* read in chunks of ~16MB */
 # define NBYTE 0x1000000
-
-char *skipNbounds (char *line, char bound, int Nbound, int Nbyte);
-void *memrchr(const void *s, int c, size_t n);
-e_time get2mass_time (char *ptr, char *buffer, int Nbound, int Nbyte);
 
 Stars *get2mass_AS_data (SkyRegion *region, char *filename, SkyRegion *patch, int photcode, int *nstars) {
@@ -61,4 +57,7 @@
   NSTARS = 10000;
   ALLOCATE (stars, Stars, NSTARS);
+
+  /* I want to add a seek-ahead test to find a good starting position in the file
+     this is very expensive using gzseek / gzread.  */
 
   Nextra = 0;
Index: /trunk/Ohana/src/addstar/src/get2mass_as_raw.c
===================================================================
--- /trunk/Ohana/src/addstar/src/get2mass_as_raw.c	(revision 7683)
+++ /trunk/Ohana/src/addstar/src/get2mass_as_raw.c	(revision 7683)
@@ -0,0 +1,180 @@
+# include "addstar.h"
+# include "2mass.h"
+
+/* unlike the DR2 data, the AS data is NOT fixed bytes/row 
+ * we need to handle fractional lines at the end of each read block
+ */
+
+/* read in chunks of ~16MB */
+# define NBYTE 0x1000000
+# define NBREC 330
+
+Stars *get2mass_AS_rawdata (SkyRegion *region, char *filename, SkyRegion *patch, int photcode, int *nstars) {
+  
+  int FilterSkip, TimeSkip;
+  int Nstars, NSTARS, Nbyte, Nextra, Qentry;
+  int Noffset, dNoffset;
+  Stars *stars;
+  FILE *f;
+  char qc;
+  char *buffer;
+  char *p, *q, *ptr, *tmp;
+  double RA, DEC, M, dM;
+  double RA0, RA1, DEC0, DEC1;
+  e_time time;
+
+  ALLOCATE (buffer, char, NBYTE);
+
+  RA0  = MAX (patch[0].Rmin, UserPatch.Rmin);
+  RA1  = MIN (patch[0].Rmax, UserPatch.Rmax);
+  DEC0 = MAX (patch[0].Dmin, UserPatch.Dmin);
+  DEC1 = MIN (patch[0].Dmax, UserPatch.Dmax);
+
+  FilterSkip = TimeSkip = Qentry = 0;
+  if (photcode == TM_J) {
+      FilterSkip = 6;
+      // TimeSkip = 23;
+      TimeSkip = 28;
+      Qentry   = 0;
+  }
+  if (photcode == TM_H) {
+      FilterSkip = 10;
+      // TimeSkip = 19;
+      TimeSkip = 24;
+      Qentry   = 1;
+  }
+  if (photcode == TM_K) {
+      FilterSkip = 14;
+      // looks like this should be 20, not 15...
+      // TimeSkip = 15;
+      TimeSkip = 20;
+      Qentry   = 2;
+  }
+  if (!FilterSkip) Shutdown ("invalid photcode %s", GetPhotcodeNamebyCode(photcode));
+
+  f = fopen (filename, "r");
+  if (f == NULL) Shutdown ("can't read 2mass data file: %s", filename);
+  // test if this is a raw datafile or gzipped...
+
+  Nstars = 0;
+  NSTARS = 10000;
+  ALLOCATE (stars, Stars, NSTARS);
+
+  /* I want to add a seek-ahead test to find a good starting position in the file
+     this is very expensive using gzseek / gzread.  */
+
+  # if 0
+  if (patch[0].Rmin > 5) {
+    // each 2MASS record is a random length of ~ 330 bytes
+    // guess at start, then used a binary search to narrow down
+    // first guess at start:
+    dNoffset = region[0].childE / 360.0;
+    Noffset = dNoffset * patch[0].Rmin;
+    fseek (f, Noffset * NBREC, SEEK_SET);
+    Nbyte = fread (buffer, 1, 10*NBREC, f);
+    q = memchr (p, '\n', Nbyte);
+    if (q == NULL) Shutdown ("incomplete line at end of file\n");
+    q++; // first (or second) complete line
+  
+    // get coords for this line
+    RA = strtod (q, NULL);
+    ptr = skipNbounds (q, '|', 1, Nbyte - (q - buffer));
+    DEC = strtod (ptr, NULL);
+    if (DEC > 90) Shutdown ("weird DEC value: something is wrong");
+
+    // if we are beyond, spin back in ~5 deg chunks 'till we are below
+    while (RA > patch[0].Rmin) {
+      fseek (f, -5 * dNoffset * NBREC, SEEK_CUR);
+      Nbyte = fread (buffer, 1, 10*NBREC, f);
+      q = memchr (p, '\n', Nbyte);
+      if (q == NULL) Shutdown ("incomplete line at end of file\n");
+      q++; // first (or second) complete line
+  
+      // get coords for this line
+      RA = strtod (q, NULL);
+      ptr = skipNbounds (q, '|', 1, Nbyte - (q - buffer));
+      DEC = strtod (ptr, NULL);
+      if (DEC > 90) Shutdown ("weird DEC value: something is wrong");
+
+      Nextra = Nbyte - (q - buffer);
+      memmove (buffer, q, Nextra);
+    }
+  } else {
+    Nextra = 0;
+  }
+  # endif
+
+  Nextra = 0;
+  while ((Nbyte = fread (&buffer[Nextra], 1, NBYTE-Nextra, f)) != 0) {
+    if (Nbyte == -1) Shutdown ("error reading from raw file %s", filename);
+    Nbyte += Nextra;
+
+    if (VERBOSE) fprintf (stderr, ".");
+
+    /* find bounds on first complete line */
+    p = buffer;
+    q = memchr (p, '\n', Nbyte);
+    if (q == NULL) Shutdown ("incomplete line at end of file\n");
+
+    while (1) {
+
+      RA = strtod (p, NULL);
+      ptr = skipNbounds (p, '|', 1, Nbyte - (p - buffer));
+      DEC = strtod (ptr, NULL);
+      if (DEC > 90) Shutdown ("weird DEC value: something is wrong");
+
+      /* skip stars which are outside desired region */
+      if (DEC > DEC1) goto skip_star;
+      if (DEC < DEC0) goto skip_star;
+      if (RA <  RA0)  goto skip_star;
+      if (RA >  RA1)  goto skip_star;
+
+      ptr = skipNbounds (p, '|', FilterSkip, Nbyte - (p - buffer));
+      if (ptr == NULL) Shutdown ("format error in 2mass");
+      M  = strtod (ptr, NULL);
+      ptr = skipNbounds (ptr, '|', 1, Nbyte - (ptr - buffer));
+      dM = strtod (ptr, NULL);
+      time = get2mass_time (ptr, buffer, TimeSkip, Nbyte);
+
+      /* filter on the ph_qual flag for this filter (field 19) */
+      if (SELECT_2MASS_QUALITY != NULL) {
+	ptr = skipNbounds (p, '|', 18, Nbyte - (p - buffer));
+	qc  = ptr[Qentry];
+	if (strchr (SELECT_2MASS_QUALITY, qc) == NULL) goto skip_star;
+      }
+
+      stars[Nstars].R     = RA;
+      stars[Nstars].D     = DEC;
+      stars[Nstars].M     = M;
+      stars[Nstars].dM    = dM;
+      stars[Nstars].code  = photcode;
+      stars[Nstars].t     = time;
+      stars[Nstars].found = -1;
+
+      Nstars ++;
+      CHECK_REALLOCATE (stars, Stars, NSTARS, Nstars, 5000);
+
+    skip_star:
+      /* start of the next line */
+      tmp = p;
+      p = q + 1;
+      if (p - buffer == Nbyte) {
+	Nextra = 0;
+	break;
+      }
+      /* end of the next line */
+      q = memchr (p, '\n', Nbyte - (p - buffer));
+      if (q == NULL) {
+	Nextra = Nbyte - (p - buffer);
+	memmove (buffer, p, Nextra);
+	break;
+      } 
+    }
+  }
+  if (VERBOSE) fprintf (stderr, "\n");
+  
+  fclose (f);
+  free (buffer);
+  *nstars = Nstars;
+  return (stars);
+}
Index: /trunk/Ohana/src/addstar/src/mkacc-2mass.c
===================================================================
--- /trunk/Ohana/src/addstar/src/mkacc-2mass.c	(revision 7682)
+++ /trunk/Ohana/src/addstar/src/mkacc-2mass.c	(revision 7683)
@@ -67,2 +67,8 @@
   exit (0);
 }
+
+/* XXX update this function to create an additional accelerator file for each 2mass file
+   each file: one row per DEC band
+   each row: Rmin, Rmax, Dmin, Dmax, Nbyte(i)
+   where Nbyte(i) = byte for each Rmin + i*30 deg transition
+*/
Index: /trunk/Ohana/src/addstar/src/scan2mass.c
===================================================================
--- /trunk/Ohana/src/addstar/src/scan2mass.c	(revision 7683)
+++ /trunk/Ohana/src/addstar/src/scan2mass.c	(revision 7683)
@@ -0,0 +1,86 @@
+# include "addstar.h"
+# include "2mass.h"
+
+int main (int argc, char **argv) {
+
+  char *path;
+  int i, N, Nrefcat;
+  SkyTable *sky;
+
+  if (argc == 2) {
+    scan2mass_as_data (argv[1]);
+    exit (0);
+  }
+
+  ConfigInit (&argc, argv);
+
+  if (argc != 1) {
+    fprintf (stderr, "USAGE: mkacc.2mass\n");
+    exit (2);
+  }
+
+  path = TWO_MASS_DIR_AS;
+
+  // the accel.dat file has the raw filenames
+  // test if the file exists, or else try the .gz version
+  sky = scan2mass_acc (path, "accel.dat");
+  
+  for (i = 0; i < sky[0].Nregions; i++) {
+    fprintf (stderr, "scanning %s\n", sky[0].filename[i]);
+    scan2mass_as_data (sky[0].filename[i]);
+  }
+  exit (0);
+}  
+
+/* watch for patches which cross 0,360 boundary */
+SkyTable *scan2mass_acc (char *path, char *accel) {
+
+  int Nregions, NREGIONS, Nrec;
+  char accelfile[1024], line[256], filename[128], datafile[256], **filenames;
+  FILE *f;
+  double Rs, Re, Ds, De;
+
+  SkyTable *sky;
+  SkyRegion *regions;
+
+  sprintf (accelfile, "%s/%s", path, accel);
+  f = fopen (accelfile, "r");
+  if (f == NULL) Shutdown ("can't read data from accelerator %s", accelfile);
+
+  Nregions = 0;
+  NREGIONS = 200;
+  ALLOCATE (regions, SkyRegion, NREGIONS);
+  ALLOCATE (filenames, char *, NREGIONS);
+
+  /* read in stars line-by-line */
+  while (scan_line (f, line) != EOF) {
+    stripwhite (line);
+    if (line[0] == 0) continue;
+    if (line[0] == '#') continue;
+    sscanf (line, "%s %lf %lf %lf %lf %d", filename, &Rs, &Re, &Ds, &De, &Nrec);
+    Rs *= 15.0;
+    Re *= 15.0;
+    regions[Nregions].Rmin = Rs;
+    regions[Nregions].Rmax = Re;
+    regions[Nregions].Dmin = Ds;
+    regions[Nregions].Dmax = De;
+    regions[Nregions].childE = Nrec; // a cheat since 2MASS only has one depth
+
+    sprintf (datafile, "%s/%s", path, filename);
+    filenames[Nregions] = strcreate (datafile);
+
+    Nregions ++;
+    if (Nregions >= NREGIONS) {
+	NREGIONS += 20;
+	REALLOCATE (regions, SkyRegion, NREGIONS);
+	REALLOCATE (filenames, char *, NREGIONS);
+    }
+  }    
+  fclose (f);
+
+  ALLOCATE (sky, SkyTable, 1);
+  sky[0].regions = regions;
+  sky[0].filename = filenames;
+  sky[0].Nregions = Nregions;
+  return (sky);
+}
Index: /trunk/Ohana/src/addstar/src/scan2mass_as_data.c
===================================================================
--- /trunk/Ohana/src/addstar/src/scan2mass_as_data.c	(revision 7683)
+++ /trunk/Ohana/src/addstar/src/scan2mass_as_data.c	(revision 7683)
@@ -0,0 +1,221 @@
+# include "addstar.h"
+# include "2mass.h"
+
+/* unlike the DR2 data, the AS data is NOT fixed bytes/row 
+ * we need to handle fractional lines at the end of each read block
+ */
+
+/* read in chunks of ~16MB */
+# define NBYTE 0x2000000
+# define DSTEP 3000.0
+# define TESTPRINT 0
+
+int scan2mass_as_data (char *filename) {
+  
+  int i, j, Nbands, NBANDS, Nbyte, Nextra, Ntotal, badStar, Nbad;
+  int firstBand, tryBand, newBand, step;
+  FILE *f, *g;
+  char *buffer, newfile[1024], badfile[1024], line[1024];
+  char *p, *q, *ptr;
+  double Ro, Do, Rstep, DmaxLast, Rlast, Dlast;
+  double Rmin, Rmax, Dmin, Dmax;
+  TMBands *bands;
+
+  Nbad = 0;
+  Nbands = 0;
+  NBANDS = 10;
+  ALLOCATE (bands, TMBands, 10);
+  ALLOCATE (buffer, char, NBYTE);
+
+  sprintf (badfile, "%s.bad", filename);
+  g = fopen (badfile, "w");
+  if (g == NULL) Shutdown ("can't write badfile for %s", filename);
+
+  f = fopen (filename, "r");
+  if (f == NULL) Shutdown ("can't read 2mass data file: %s", filename);
+  // test if this is a raw datafile or gzipped...
+
+  firstBand = TRUE;
+  newBand = FALSE;
+  tryBand = FALSE;
+  DmaxLast = 90.0;
+  badStar = FALSE;
+
+  step = 0;
+
+  Nextra = 0;
+  Ntotal = 0;
+  while ((Nbyte = fread (&buffer[Nextra], 1, NBYTE-Nextra, f)) != 0) {
+    if (Nbyte == -1) Shutdown ("error reading from raw file %s", filename);
+    Nbyte += Nextra;
+    fprintf (stderr, ".");
+
+    /* find bounds on first complete line */
+    p = buffer;
+    q = memchr (p, '\n', Nbyte);
+    if (q == NULL) Shutdown ("incomplete line at end of file\n");
+
+    while (1) {
+
+      Ro = strtod (p, NULL);
+      ptr = skipNbounds (p, '|', 1, Nbyte - (p - buffer));
+      Do = strtod (ptr, NULL);
+      if (Do > 90) Shutdown ("weird DEC value: something is wrong");
+      if (badStar) {
+      fprintf (stderr, "next R,D: %10.6f %10.6f\n", Ro, Do);
+      }
+
+      if (firstBand) {
+	Rmax = Rmin = Ro;
+	Dmin = Dmax = Do;
+	Rlast = Ro;
+	Dlast = Do;
+
+	/* 
+	if (Do < 0.0) {
+	  Dmax = 0.1*(int)(Do * 10.0);
+	  Dmin = 0.1*(int)(Do * 10.0) - 0.1; 
+	} else {
+	  Dmin = 0.1*(int)(Do * 10.0);
+	  Dmax = 0.1*(int)(Do * 10.0) + 0.1; 
+	}	  
+	*/
+
+	bands[Nbands].index[0] = Ntotal + p - buffer;
+	step = 1;
+	Rstep = Rmin + step*DSTEP;
+	firstBand = FALSE;
+	tryBand = FALSE;
+	if (TESTPRINT) fprintf (stderr, "first: %10.6f, %10.6f\n", Ro, Do);
+      }
+
+      // is star in the next band?
+      badStar = FALSE;
+      if (tryBand && (Ro < 90.0)) {
+	if ((Ro + 360.0 - Rlast) > 0.1) {
+	  fprintf (stderr, "warning: star out of order?\n");
+	  fprintf (stderr, "last R,D: %10.6f %10.6f\n", Rlast, Dlast);
+	  fprintf (stderr, "this R,D: %10.6f %10.6f\n", Ro, Do);
+	  snprintf (line, q-p+1, "%s", p);
+	  fprintf (g, "%s\n", line);
+	  Nbad ++;
+	  badStar = TRUE;
+	} else {
+	  newBand = TRUE;
+	}
+      }
+      Rlast = Ro;
+      Dlast = Do;
+
+      if ((Nbands > 0) && (Do < DmaxLast)) {
+	fprintf (stderr, "warning: star out of order?\n");
+	fprintf (stderr, "R,D: %10.6f %10.6f\n", Ro, Do);
+      }
+
+      if (!newBand && !badStar) {
+	Rmin = MIN (Rmin, Ro);
+	Rmax = MAX (Rmax, Ro);
+	Dmin = MIN (Dmin, Do);
+	Dmax = MAX (Dmax, Do);
+      }
+
+      // test of the next step:
+      if (Ro > Rstep) {
+	bands[Nbands].index[step] = Ntotal + p - buffer;
+	if (TESTPRINT) fprintf (stderr, "  step: %10.6f | %10.6f : %d\n", Ro, Rstep, bands[Nbands].index[step]);
+	step ++;
+	Rstep = Rmin + step*DSTEP;
+      }
+
+      if (Ro > 270.0) tryBand = TRUE;
+
+      // test of end of band
+      if (newBand) {
+	bands[Nbands].index[step] = Ntotal + p - buffer;
+	step ++;
+
+	bands[Nbands].Nindex = step;
+	bands[Nbands].Rmin = Rmin;
+	bands[Nbands].Rmax = Rmax;
+	bands[Nbands].Dmin = Dmin;
+	bands[Nbands].Dmax = Dmax;
+
+	DmaxLast = Dmax;
+
+	if (TESTPRINT) fprintf (stderr, " last: %d steps: %10.6f - %10.6f, %10.6f - %10.6f : %d - %d\n", 
+		 bands[Nbands].Nindex, bands[Nbands].Rmin, bands[Nbands].Rmax, bands[Nbands].Dmin, bands[Nbands].Dmax,
+		 bands[Nbands].index[0], bands[Nbands].index[bands[Nbands].Nindex-1]);
+	if (TESTPRINT) fprintf (stderr, "start: %10.6f, %10.6f\n", Ro, Do);
+
+	Nbands ++;
+	CHECK_REALLOCATE (bands, TMBands, NBANDS, Nbands, 10);
+
+	Rmax = Rmin = Ro;
+	Dmax = Dmin = Do;
+
+	bands[Nbands].index[0] = Ntotal + p - buffer;
+	step = 1;
+	Rstep = Rmin + step*DSTEP;
+
+	newBand = FALSE;
+	tryBand = FALSE;
+      }
+
+      /* start of the next line */
+      p = q + 1;
+      if (p - buffer == Nbyte) {
+	Nextra = 0;
+	Ntotal += Nbyte;
+	break;
+      }
+      /* end of the next line */
+      q = memchr (p, '\n', Nbyte - (p - buffer));
+      if (q == NULL) {
+	Nextra = Nbyte - (p - buffer);
+	memmove (buffer, p, Nextra);
+	Ntotal += Nbyte - Nextra;
+	break;
+      } 
+    }
+  }
+  fprintf (stderr, "\n");
+
+  bands[Nbands].index[step] = Ntotal;
+  step ++;
+  bands[Nbands].Nindex = step;
+  bands[Nbands].Rmin = Rmin;
+  bands[Nbands].Rmax = Rmax;
+  bands[Nbands].Dmin = Dmin;
+  bands[Nbands].Dmax = Dmax;
+  if (TESTPRINT) fprintf (stderr, " last: %d steps: %10.6f - %10.6f, %10.6f - %10.6f : %d - %d\n", 
+	   bands[Nbands].Nindex, bands[Nbands].Rmin, bands[Nbands].Rmax, bands[Nbands].Dmin, bands[Nbands].Dmax,
+	   bands[Nbands].index[0], bands[Nbands].index[bands[Nbands].Nindex-1]);
+  Nbands ++;
+
+  fprintf (stderr, "done with %s: %d bands (%d bad)\n", filename, Nbands, Nbad);
+  
+  fclose (f);
+  fclose (g);
+  free (buffer);
+
+  sprintf (newfile, "%s.idx", filename);
+  f = fopen (newfile, "w");
+  if (f == NULL) {
+    fprintf (stderr, "can't write index file\n");
+    exit (1);
+  }
+
+  for (i = 0; i < Nbands; i++) {
+    fprintf (f, "%10.6f %10.6f %10.6f %10.6f  %d ", 
+	     bands[i].Rmin, bands[i].Rmax, bands[i].Dmin, bands[i].Dmax, bands[i].Nindex);
+
+    for (j = 0; j < bands[i].Nindex; j++) {
+      fprintf (f, " %d", bands[i].index[j]);
+    }
+    fprintf (f, "\n");
+  }    
+  fclose (f);
+
+  free (bands);
+  return (TRUE);
+}
Index: /trunk/Ohana/src/addstar/src/sort_lists.c
===================================================================
--- /trunk/Ohana/src/addstar/src/sort_lists.c	(revision 7682)
+++ /trunk/Ohana/src/addstar/src/sort_lists.c	(revision 7683)
@@ -57,6 +57,6 @@
     j = (l << 1) + 1;
     while (j <= ir) {
-      if (j < ir && region[j].DECmin < region[j+1].DECmin) j++;
-      if (tmp.DECmin < region[j].DECmin) {
+      if (j < ir && region[j].Dmin < region[j+1].Dmin) j++;
+      if (tmp.Dmin < region[j].Dmin) {
 	region[i] = region[j];
 	j += (i=j) + 1;
