Index: /trunk/Ohana/src/addstar/Makefile
===================================================================
--- /trunk/Ohana/src/addstar/Makefile	(revision 16039)
+++ /trunk/Ohana/src/addstar/Makefile	(revision 16040)
@@ -68,5 +68,4 @@
 $(SRC)/ReadStarsTEXT.$(ARCH).o \
 $(SRC)/FilterStars.$(ARCH).o \
-$(SRC)/sort_lists.$(ARCH).o \
 $(SRC)/ImageOptions.$(ARCH).o \
 $(SRC)/GetFileMode.$(ARCH).o \
@@ -114,5 +113,4 @@
 $(SRC)/opening_angle.$(ARCH).o \
 $(SRC)/replace_match.$(ARCH).o \
-$(SRC)/sort_lists.$(ARCH).o \
 $(SRC)/update_coords.$(ARCH).o
 
@@ -151,5 +149,4 @@
 $(SRC)/SkyRegionUtils.$(ARCH).o \
 $(SRC)/SkyListForStars.$(ARCH).o \
-$(SRC)/sort_lists.$(ARCH).o \
 $(SRC)/update_coords.$(ARCH).o \
 $(SRC)/Shutdown.$(ARCH).o \
@@ -194,5 +191,4 @@
 $(SRC)/find_matches_refstars.$(ARCH).o \
 $(SRC)/args_load2mass.$(ARCH).o \
-$(SRC)/sort_lists.$(ARCH).o \
 $(SRC)/replace_match.$(ARCH).o \
 $(SRC)/update_coords.$(ARCH).o \
Index: /trunk/Ohana/src/addstar/src/SEDops.c
===================================================================
--- /trunk/Ohana/src/addstar/src/SEDops.c	(revision 16039)
+++ /trunk/Ohana/src/addstar/src/SEDops.c	(revision 16040)
@@ -57,6 +57,7 @@
 SEDtableRow **sort_SEDtable (SEDtableRow *raw, int N) {
 
-  int l,j,ir,i;
-  SEDtableRow **value, *temp;
+  int i;
+
+  SEDtableRow **value;
   
   if (N <= 0) return (NULL);
@@ -66,33 +67,12 @@
     value[i] = &raw[i];
   }
-  if (N < 2) return (value);
-
-  l = N >> 1;
-  ir = N - 1;
-  for (;;) {
-    if (l > 0) {
-      l--;
-      temp = value[l];
-    }
-    else {
-      temp = value[ir];
-      value[ir] = value[0];
-      if (--ir == 0) {
-	value[0] = temp;
-	return (value);
-      }
-    }
-    i = l;
-    j = (l << 1) + 1;
-    while (j <= ir) {
-      if (j < ir && value[j][0].color < value[j+1][0].color) ++j;
-      if (temp[0].color < value[j][0].color) {
-	value[i] = value[j];
-	j += (i=j) + 1;
-      }
-      else j = ir + 1;
-    }
-    value[i] = temp;
-  }
+
+# define SWAPFUNC(A,B){ SEDtableRow *temp = value[A]; value[A] = value[B]; value[B] = temp; }
+# define COMPARE(A,B)(value[A][0].color < value[B][0].color)
+
+  OHANA_SORT (N, COMPARE, SWAPFUNC);
+
+# undef SWAPFUNC
+# undef COMPARE
 
   return (value);
Index: /trunk/Ohana/src/addstar/src/SkyListForStars.c
===================================================================
--- /trunk/Ohana/src/addstar/src/SkyListForStars.c	(revision 16039)
+++ /trunk/Ohana/src/addstar/src/SkyListForStars.c	(revision 16040)
@@ -15,5 +15,5 @@
   list[0].ownElements = FALSE; // free these elements when freeing the list
 
-  sort_stars (stars, Nstars); /* sort by RA */
+  sort_stars_ra (stars, Nstars); /* sort by RA */
   for (i = 0; i < Nstars; i++) {
     if (stars[i].found) continue;
Index: /trunk/Ohana/src/addstar/src/calibrate.c
===================================================================
--- /trunk/Ohana/src/addstar/src/calibrate.c	(revision 16039)
+++ /trunk/Ohana/src/addstar/src/calibrate.c	(revision 16040)
@@ -188,5 +188,5 @@
     return;
   }
-  fsort2 (Dmag, dDmag, Nkeep);
+  fsortpair (Dmag, dDmag, Nkeep);
 
   /* take sort list of Dmag, find median */
Index: /trunk/Ohana/src/addstar/src/find_matches.c
===================================================================
--- /trunk/Ohana/src/addstar/src/find_matches.c	(revision 16039)
+++ /trunk/Ohana/src/addstar/src/find_matches.c	(revision 16040)
@@ -5,6 +5,6 @@
   int i, j, n, N, J, status, Nstars;
   double RADIUS, RADIUS2;
-  float *X1, *Y1, *X2, *Y2;
-  float dX, dY, dR;
+  double *X1, *Y1, *X2, *Y2;
+  double dX, dY, dR;
   int *N1, *N2,  *next_meas;
   int Nave, NAVE, Nmeas, NMEAS, Nmatch;
@@ -21,12 +21,12 @@
 
   /** allocate local arrays (stars) **/
-  ALLOCATE (X1, float, NstarsIn);
-  ALLOCATE (Y1, float, NstarsIn);
+  ALLOCATE (X1, double, NstarsIn);
+  ALLOCATE (Y1, double, NstarsIn);
   ALLOCATE (N1, int,   NstarsIn);
 
   /** allocate local arrays (catalog) **/
   NAVE = Nave = catalog[0].Naverage;
-  ALLOCATE (X2, float, NAVE);
-  ALLOCATE (Y2, float, NAVE);
+  ALLOCATE (X2, double, NAVE);
+  ALLOCATE (Y2, double, NAVE);
   ALLOCATE (N2, int,   NAVE);
   ALLOCATE (catalog[0].found, int, NAVE);
@@ -60,5 +60,5 @@
   Nstars = 0;
   for (i = 0; i < NstarsIn; i++) {
-    status = fRD_to_XY (&X1[Nstars], &Y1[Nstars], stars[i].R, stars[i].D, &tcoords);
+    status = RD_to_XY (&X1[Nstars], &Y1[Nstars], stars[i].R, stars[i].D, &tcoords);
     if (!status) continue;
     N1[Nstars] = i;
@@ -76,13 +76,13 @@
     return (0);
   }
-  if (Nstars > 1) sort_lists (X1, Y1, N1, Nstars);
+  if (Nstars > 1) sort_coords_index (X1, Y1, N1, Nstars);
   
   /* build spatial index (RA sort) */
   for (i = 0; i < Nave; i++) {
-    fRD_to_XY (&X2[i], &Y2[i], catalog[0].average[i].R, catalog[0].average[i].D, &tcoords);
+    RD_to_XY (&X2[i], &Y2[i], catalog[0].average[i].R, catalog[0].average[i].D, &tcoords);
     N2[i] = i;
     catalog[0].found[N2[i]] = -1;
   }
-  if (Nave > 1) sort_lists (X2, Y2, N2, Nave);
+  if (Nave > 1) sort_coords_index (X2, Y2, N2, Nave);
 
   /* set up pointers for linked list of measure */
Index: /trunk/Ohana/src/addstar/src/find_matches_closest.c
===================================================================
--- /trunk/Ohana/src/addstar/src/find_matches_closest.c	(revision 16039)
+++ /trunk/Ohana/src/addstar/src/find_matches_closest.c	(revision 16040)
@@ -5,6 +5,6 @@
   int i, j, n, N, J, Jmin, status, Nstars;
   double RADIUS, RADIUS2, Rmin;
-  float *X1, *Y1, *X2, *Y2;
-  float dX, dY, dR;
+  double *X1, *Y1, *X2, *Y2;
+  double dX, dY, dR;
   int *N1, *N2,  *next_meas, *next_miss;
   int Nave, NAVE, Nmeas, NMEAS, Nmiss, NMISS, Nmatch;
@@ -21,12 +21,12 @@
 
   /** allocate local arrays (stars) **/
-  ALLOCATE (X1, float, NstarsIn);
-  ALLOCATE (Y1, float, NstarsIn);
+  ALLOCATE (X1, double, NstarsIn);
+  ALLOCATE (Y1, double, NstarsIn);
   ALLOCATE (N1, int,   NstarsIn);
 
   /** allocate local arrays (catalog) **/
   NAVE = Nave = catalog[0].Naverage;
-  ALLOCATE (X2, float, NAVE);
-  ALLOCATE (Y2, float, NAVE);
+  ALLOCATE (X2, double, NAVE);
+  ALLOCATE (Y2, double, NAVE);
   ALLOCATE (N2, int,   NAVE);
   ALLOCATE (catalog[0].found, int, NAVE);
@@ -61,5 +61,5 @@
   Nstars = 0;
   for (i = 0; i < NstarsIn; i++) {
-    status = fRD_to_XY (&X1[Nstars], &Y1[Nstars], stars[i].R, stars[i].D, &tcoords);
+    status = RD_to_XY (&X1[Nstars], &Y1[Nstars], stars[i].R, stars[i].D, &tcoords);
     if (!status) continue;
     N1[Nstars] = i;
@@ -77,13 +77,13 @@
     return (0);
   }
-  if (Nstars > 1) sort_lists (X1, Y1, N1, Nstars);
+  if (Nstars > 1) sort_coords_index (X1, Y1, N1, Nstars);
 
   /* build spatial index (RA sort) */
   for (i = 0; i < Nave; i++) {
-    fRD_to_XY (&X2[i], &Y2[i], catalog[0].average[i].R, catalog[0].average[i].D, &tcoords);
+    RD_to_XY (&X2[i], &Y2[i], catalog[0].average[i].R, catalog[0].average[i].D, &tcoords);
     N2[i] = i;
     catalog[0].found[N2[i]] = -1;
   }
-  if (Nave > 1) sort_lists (X2, Y2, N2, Nave);
+  if (Nave > 1) sort_coords_index (X2, Y2, N2, Nave);
 
   /* set up pointers for linked list of measure, missing */
Index: /trunk/Ohana/src/addstar/src/find_matches_refstars.c
===================================================================
--- /trunk/Ohana/src/addstar/src/find_matches_refstars.c	(revision 16039)
+++ /trunk/Ohana/src/addstar/src/find_matches_refstars.c	(revision 16040)
@@ -5,6 +5,6 @@
   int i, j, k, n, m, N, J;
   double RADIUS, RADIUS2;
-  float *X1, *Y1, *X2, *Y2;
-  float dX, dY, dR;
+  double *X1, *Y1, *X2, *Y2;
+  double dX, dY, dR;
   int *N1, *N2,  *next, *next_miss, last, last_miss;
   int Nave, NAVE, Nmeas, NMEAS, Nmiss, NMISS, Nmatch;
@@ -18,6 +18,6 @@
 
   /** allocate local arrays (stars) **/
-  ALLOCATE (X1, float, Nstars);
-  ALLOCATE (Y1, float, Nstars);
+  ALLOCATE (X1, double, Nstars);
+  ALLOCATE (Y1, double, Nstars);
   ALLOCATE (N1, int,   Nstars);
 
@@ -25,6 +25,6 @@
   Nave = catalog[0].Naverage;
   NAVE = Nave + 1000;
-  ALLOCATE (X2, float, NAVE);
-  ALLOCATE (Y2, float, NAVE);
+  ALLOCATE (X2, double, NAVE);
+  ALLOCATE (Y2, double, NAVE);
   ALLOCATE (N2, int, NAVE);
   ALLOCATE (catalog[0].found, int, NAVE);
@@ -59,15 +59,15 @@
   
   for (i = 0; i < Nstars; i++) {
-    fRD_to_XY (&X1[i], &Y1[i], stars[i][0].R, stars[i][0].D, &tcoords);
+    RD_to_XY (&X1[i], &Y1[i], stars[i][0].R, stars[i][0].D, &tcoords);
     N1[i] = i;
   }
-  if (Nstars > 1) sort_lists (X1, Y1, N1, Nstars);
+  if (Nstars > 1) sort_coords_index (X1, Y1, N1, Nstars);
   
   for (i = 0; i < Nave; i++) {
-    fRD_to_XY (&X2[i], &Y2[i], catalog[0].average[i].R, catalog[0].average[i].D, &tcoords);
+    RD_to_XY (&X2[i], &Y2[i], catalog[0].average[i].R, catalog[0].average[i].D, &tcoords);
     N2[i] = i;
     catalog[0].found[N2[i]] = -1;
   }
-  if (Nave > 1) sort_lists (X2, Y2, N2, Nave);
+  if (Nave > 1) sort_coords_index (X2, Y2, N2, Nave);
 
   /* set up pointers for linked list of measurements */
Index: unk/Ohana/src/addstar/src/sort_lists.c
===================================================================
--- /trunk/Ohana/src/addstar/src/sort_lists.c	(revision 16039)
+++ 	(revision )
@@ -1,186 +1,0 @@
-# include "addstar.h"
-
-void sort_stars (Stars *stars, int N) {
-
-  int l,j,ir,i;
-  Stars tmp;
-  
-  l = N >> 1;
-  ir = N - 1;
-  for (;;) {
-    if (l > 0) {
-      l--;
-      tmp = stars[l];
-    }
-    else {
-      tmp = stars[ir];
-      stars[ir] = stars[0];
-      if (--ir == 0) {
-	stars[0] = tmp;
-	return;
-      }
-    }
-    i = l;
-    j = (l << 1) + 1;
-    while (j <= ir) {
-      if (j < ir && stars[j].R < stars[j+1].R) j++;
-      if (tmp.R < stars[j].R) {
-	stars[i] = stars[j];
-	j += (i=j) + 1;
-      }
-      else j = ir + 1;
-    }
-    stars[i] = tmp;
-  }
-}
-
-void sort_regions (SkyRegion *region, int N) {
-
-  int l,j,ir,i;
-  SkyRegion tmp;
-  
-  l = N >> 1;
-  ir = N - 1;
-  for (;;) {
-    if (l > 0) {
-      l--;
-      tmp = region[l];
-    } else {
-      tmp = region[ir];
-      region[ir] = region[0];
-      if (--ir == 0) {
-	region[0] = tmp;
-	return;
-      }
-    }
-    i = l;
-    j = (l << 1) + 1;
-    while (j <= ir) {
-      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;
-      }
-      else j = ir + 1;
-    }
-    region[i] = tmp;
-  }
-}
-
-void sort_lists (float *X, float *Y, int *S, int N) {
-
-  int l,j,ir,i;
-  double tX, tY, tS;
-  
-  l = N >> 1;
-  ir = N - 1;
-  for (;;) {
-    if (l > 0) {
-      l--;
-      tX = X[l];
-      tY = Y[l];
-      tS = S[l];
-    } else {
-      tX = X[ir];
-      X[ir] = X[0];
-      tY = Y[ir];
-      Y[ir] = Y[0];
-      tS = S[ir];
-      S[ir] = S[0];
-      if (--ir == 0) {
-	X[0] = tX;
-	Y[0] = tY;
-	S[0] = tS;
-	return;
-      }
-    }
-    i = l;
-    j = (l << 1) + 1;
-    while (j <= ir) {
-      if (j < ir && X[j] < X[j+1]) j++;
-      if (tX < X[j]) {
-	X[i] = X[j];
-	Y[i] = Y[j];
-	S[i] = S[j];
-	j += (i=j) + 1;
-      }
-      else j = ir + 1;
-    }
-    X[i] = tX;
-    Y[i] = tY;
-    S[i] = tS;
-  }
-}
-
-void fsort (float *X, int N) {
-
-  int l,j,ir,i;
-  float tX;
-  
-  l = N >> 1;
-  ir = N - 1;
-  for (;;) {
-    if (l > 0) {
-      l--;
-      tX = X[l];
-    } else {
-      tX = X[ir];
-      X[ir] = X[0];
-      if (--ir == 0) {
-	X[0] = tX;
-	return;
-      }
-    }
-    i = l;
-    j = (l << 1) + 1;
-    while (j <= ir) {
-      if (j < ir && X[j] < X[j+1]) j++;
-      if (tX < X[j]) {
-	X[i] = X[j];
-	j += (i=j) + 1;
-      }
-      else j = ir + 1;
-    }
-    X[i] = tX;
-  }
-}
-
-void fsort2 (float *X, float *Y, int N) {
-
-  int l,j,ir,i;
-  float tX, tY;
-  
-  l = N >> 1;
-  ir = N - 1;
-  for (;;) {
-    if (l > 0) {
-      l--;
-      tX = X[l];
-      tY = Y[l];
-    }
-    else {
-      tX = X[ir];
-      tY = Y[ir];
-      X[ir] = X[0];
-      Y[ir] = Y[0];
-      if (--ir == 0) {
-	X[0] = tX;
-	Y[0] = tY;
-	return;
-      }
-    }
-    i = l;
-    j = (l << 1) + 1;
-    while (j <= ir) {
-      if (j < ir && X[j] < X[j+1]) j++;
-      if (tX < X[j]) {
-	X[i] = X[j];
-	Y[i] = Y[j];
-	j += (i=j) + 1;
-      }
-      else j = ir + 1;
-    }
-    X[i] = tX;
-    Y[i] = tY;
-  }
-}
Index: /trunk/Ohana/src/delstar/Makefile
===================================================================
--- /trunk/Ohana/src/delstar/Makefile	(revision 16039)
+++ /trunk/Ohana/src/delstar/Makefile	(revision 16040)
@@ -32,5 +32,4 @@
 $(SRC)/find_matches.$(ARCH).o 	  \
 $(SRC)/parse_time.$(ARCH).o       \
-$(SRC)/sort_lists.$(ARCH).o \
 $(SRC)/check_permissions.$(ARCH).o \
 $(SRC)/Shutdown.$(ARCH).o          
Index: unk/Ohana/src/delstar/src/sort_lists.c
===================================================================
--- /trunk/Ohana/src/delstar/src/sort_lists.c	(revision 16039)
+++ 	(revision )
@@ -1,47 +1,0 @@
-# include "delstar.h"
-
-void sort_lists (float *X, float *Y, int *S, int N) {
-
-  int l,j,ir,i;
-  double tX, tY, tS;
-  
-  l = N >> 1;
-  ir = N - 1;
-  for (;;) {
-    if (l > 0) {
-      l--;
-      tX = X[l];
-      tY = Y[l];
-      tS = S[l];
-    } else {
-      tX = X[ir];
-      X[ir] = X[0];
-      tY = Y[ir];
-      Y[ir] = Y[0];
-      tS = S[ir];
-      S[ir] = S[0];
-      if (--ir == 0) {
-	X[0] = tX;
-	Y[0] = tY;
-	S[0] = tS;
-	return;
-      }
-    }
-    i = l;
-    j = (l << 1) + 1;
-    while (j <= ir) {
-      if (j < ir && X[j] < X[j+1]) j++;
-      if (tX < X[j]) {
-	X[i] = X[j];
-	Y[i] = Y[j];
-	S[i] = S[j];
-	j += (i=j) + 1;
-      }
-      else j = ir + 1;
-    }
-    X[i] = tX;
-    Y[i] = tY;
-    S[i] = tS;
-  }
-}
-
Index: /trunk/Ohana/src/fixcat/include/markstar.h
===================================================================
--- /trunk/Ohana/src/fixcat/include/markstar.h	(revision 16039)
+++ /trunk/Ohana/src/fixcat/include/markstar.h	(revision 16040)
@@ -35,5 +35,5 @@
 typedef struct {
   Coords coords;
-  float *X, *Y;
+  double *X, *Y;
   int *N;
   double RA[2], DEC[2];
Index: /trunk/Ohana/src/fixcat/src/find_bright_stars.c
===================================================================
--- /trunk/Ohana/src/fixcat/src/find_bright_stars.c	(revision 16039)
+++ /trunk/Ohana/src/fixcat/src/find_bright_stars.c	(revision 16040)
@@ -10,5 +10,5 @@
   double MinRA, MinDEC, MaxRA, MaxDEC, RaCenter, DecCenter;
   Coords *tcoords;
-  float *X1, *Y1, *X2, *Y2;
+  double *X1, *Y1, *X2, *Y2;
   int *N1, *N2;
   char *mark;
@@ -25,6 +25,6 @@
   Y2 = catstats[0].Y;
   N2 = catstats[0].N;
-  ALLOCATE (X1, float, Ngsc);
-  ALLOCATE (Y1, float, Ngsc);
+  ALLOCATE (X1, double, Ngsc);
+  ALLOCATE (Y1, double, Ngsc);
   ALLOCATE (N1, int, Ngsc);
   ALLOCATE (mark, char, Nave);
@@ -33,8 +33,8 @@
   tcoords = &catstats[0].coords;
   for (i = 0; i < Ngsc; i++) {
-    fRD_to_XY (&X1[i], &Y1[i], GSCdata.average[i].R, GSCdata.average[i].D, tcoords);
+    RD_to_XY (&X1[i], &Y1[i], GSCdata.average[i].R, GSCdata.average[i].D, tcoords);
     N1[i] = i;
   }
-  if (Ngsc > 1) sort_lists (X1, Y1, N1, Ngsc);
+  if (Ngsc > 1) sort_coords_index (X1, Y1, N1, Ngsc);
   
   /* first find stellar halos */
Index: /trunk/Ohana/src/fixcat/src/find_matches.c
===================================================================
--- /trunk/Ohana/src/fixcat/src/find_matches.c	(revision 16039)
+++ /trunk/Ohana/src/fixcat/src/find_matches.c	(revision 16040)
@@ -9,6 +9,6 @@
   int i, j, k, n, m, N, M, first_j;
   double X, Y, RADIUS, RADIUS2;
-  float *X1, *Y1, *X2, *Y2;
-  float dX, dY, dR;
+  double *X1, *Y1, *X2, *Y2;
+  double dX, dY, dR;
   int *N1, *N2;
   int Nstar, Nghost, Ng;
@@ -27,6 +27,6 @@
   N2 = catstats[0].N;
 
-  ALLOCATE (X1, float, Nghost);
-  ALLOCATE (Y1, float, Nghost);
+  ALLOCATE (X1, double, Nghost);
+  ALLOCATE (Y1, double, Nghost);
   ALLOCATE (N1, int, Nghost);
 
@@ -34,8 +34,8 @@
   tcoords = &catstats[0].coords;
   for (i = 0; i < Nghost; i++) {
-    fRD_to_XY (&X1[i], &Y1[i], Ghostdata[0].average[i].R, Ghostdata[0].average[i].D, tcoords);
+    RD_to_XY (&X1[i], &Y1[i], Ghostdata[0].average[i].R, Ghostdata[0].average[i].D, tcoords);
     N1[i] = i;
   }
-  if (Nghost > 1) sort_lists (X1, Y1, N1, Nghost);
+  if (Nghost > 1) sort_coords_index (X1, Y1, N1, Nghost);
 
   /* choose a radius for matches */
Index: /trunk/Ohana/src/fixcat/src/gcatstats.c
===================================================================
--- /trunk/Ohana/src/fixcat/src/gcatstats.c	(revision 16039)
+++ /trunk/Ohana/src/fixcat/src/gcatstats.c	(revision 16040)
@@ -9,5 +9,5 @@
   double RaCenter, DecCenter;
   double MinRA, MaxRA, MinDEC, MaxDEC;
-  float *X1, *Y1;
+  double *X1, *Y1;
   int *N1;
   Coords tcoords;
@@ -41,6 +41,6 @@
 
   /** allocate local arrays **/
-  ALLOCATE (catstats[0].X, float, catalog[0].Naverage);
-  ALLOCATE (catstats[0].Y, float, catalog[0].Naverage);
+  ALLOCATE (catstats[0].X, double, catalog[0].Naverage);
+  ALLOCATE (catstats[0].Y, double, catalog[0].Naverage);
   ALLOCATE (catstats[0].N, int,   catalog[0].Naverage);
 
@@ -58,8 +58,8 @@
   Y1 = catstats[0].Y;
   for (i = 0; i < catalog[0].Naverage; i++, X1++, Y1++) {
-    fRD_to_XY (X1, Y1, catalog[0].average[i].R, catalog[0].average[i].D, &catstats[0].coords);
+    RD_to_XY (X1, Y1, catalog[0].average[i].R, catalog[0].average[i].D, &catstats[0].coords);
     catstats[0].N[i] = i;
   }
-  if (catalog[0].Naverage > 1) sort_lists (catstats[0].X, catstats[0].Y, catstats[0].N, catalog[0].Naverage);
+  if (catalog[0].Naverage > 1) sort_coords_index (catstats[0].X, catstats[0].Y, catstats[0].N, catalog[0].Naverage);
   
 }
Index: /trunk/Ohana/src/fixcat/src/mark_trail.c
===================================================================
--- /trunk/Ohana/src/fixcat/src/mark_trail.c	(revision 16039)
+++ /trunk/Ohana/src/fixcat/src/mark_trail.c	(revision 16040)
@@ -11,5 +11,5 @@
 {
 
-  float *R, *D;
+  double *R, *D;
   int j, k, jj, kk, N, NPTS, marked, start, end, Nm;
   int *good, *seq, *N1;
Index: /trunk/Ohana/src/fixcat/src/sorts.c
===================================================================
--- /trunk/Ohana/src/fixcat/src/sorts.c	(revision 16039)
+++ /trunk/Ohana/src/fixcat/src/sorts.c	(revision 16040)
@@ -1,175 +1,17 @@
+# include "markstar.h"
 
+void sort_seq (double *X, int *S, int N) {
 
-sort_lists (X, Y, S, N) 
-float *X, *Y;
-int *S, N;
-{
-  int l,j,ir,i;
-  double tX, tY, tS;
-  
-  l = N >> 1;
-  ir = N - 1;
-  for (;;) {
-    if (l > 0) {
-      l--;
-      tX = X[l];
-      tY = Y[l];
-      tS = S[l];
-    }
-    else {
-      tX = X[ir];
-      X[ir] = X[0];
-      tY = Y[ir];
-      Y[ir] = Y[0];
-      tS = S[ir];
-      S[ir] = S[0];
-      if (--ir == 0) {
-	X[0] = tX;
-	Y[0] = tY;
-	S[0] = tS;
-	return;
-      }
-    }
-    i = l;
-    j = (l << 1) + 1;
-    while (j <= ir) {
-      if (j < ir && X[j] < X[j+1]) j++;
-      if (tX < X[j]) {
-	X[i] = X[j];
-	Y[i] = Y[j];
-	S[i] = S[j];
-	j += (i=j) + 1;
-      }
-      else j = ir + 1;
-    }
-    X[i] = tX;
-    Y[i] = tY;
-    S[i] = tS;
-  }
+# define SWAPFUNC(A,B){ \
+  double dtmp = X[A]; X[A] = X[B]; X[B] = dtmp; \
+  int    itmp = Y[A]; Y[A] = Y[B]; Y[B] = itmp; \
+}
+# define COMPARE(A,B)(X[A] < X[B])
+
+  OHANA_SORT (N, COMPARE, SWAPFUNC);
+
+# undef SWAPFUNC
+# undef COMPARE
+
 }
 
-sort_entries (X, Y, N) 
-     double *X, *Y;
-     int N;
-{
-  int l,j,ir,i;
-  double tX, tY;
-  
-  l = N >> 1;
-  ir = N - 1;
-  for (;;) {
-    if (l > 0) {
-      l--;
-      tX = X[l];
-      tY = Y[l];
-    }
-    else {
-      tX = X[ir];
-      X[ir] = X[0];
-      tY = Y[ir];
-      Y[ir] = Y[0];
-      if (--ir == 0) {
-	X[0] = tX;
-	Y[0] = tY;
-	return;
-      }
-    }
-    i = l;
-    j = (l << 1) + 1;
-    while (j <= ir) {
-      if (j < ir && X[j] < X[j+1]) j++;
-      if (tX < X[j]) {
-	X[i] = X[j];
-	Y[i] = Y[j];
-	j += (i=j) + 1;
-      }
-      else j = ir + 1;
-    }
-    X[i] = tX;
-    Y[i] = tY;
-  }
-}
-
-
-
-sort (X, N) 
-     double *X;
-     int N;
-{
-  int l,j,ir,i;
-  double tX;
-  
-  l = N >> 1;
-  ir = N - 1;
-  for (;;) {
-    if (l > 0) {
-      l--;
-      tX = X[l];
-    }
-    else {
-      tX = X[ir];
-      X[ir] = X[0];
-      if (--ir == 0) {
-	X[0] = tX;
-	return;
-      }
-    }
-    i = l;
-    j = (l << 1) + 1;
-    while (j <= ir) {
-      if (j < ir && X[j] < X[j+1]) j++;
-      if (tX < X[j]) {
-	X[i] = X[j];
-	j += (i=j) + 1;
-      }
-      else j = ir + 1;
-    }
-    X[i] = tX;
-  }
-}
-
-
-sort_seq (X, S, N) 
-     double *X;
-     int *S;
-     int N;
-{
-  int l,j,ir,i;
-  int tS;
-  double tX;
-  
-  l = N >> 1;
-  ir = N - 1;
-  for (;;) {
-    if (l > 0) {
-      l--;
-      tX = X[l];
-      tS = S[l];
-    }
-    else {
-      tX = X[ir];
-      X[ir] = X[0];
-      tS = S[ir];
-      S[ir] = S[0];
-      if (--ir == 0) {
-	X[0] = tX;
-	S[0] = tS;
-	return;
-      }
-    }
-    i = l;
-    j = (l << 1) + 1;
-    while (j <= ir) {
-      if (j < ir && X[j] < X[j+1]) j++;
-      if (tX < X[j]) {
-	X[i] = X[j];
-	S[i] = S[j];
-	j += (i=j) + 1;
-      }
-      else j = ir + 1;
-    }
-    X[i] = tX;
-    S[i] = tS;
-  }
-}
-
Index: /trunk/Ohana/src/gastro/Makefile
===================================================================
--- /trunk/Ohana/src/gastro/Makefile	(revision 16039)
+++ /trunk/Ohana/src/gastro/Makefile	(revision 16040)
@@ -31,5 +31,4 @@
 $(SRC)/gheader.$(ARCH).o \
 $(SRC)/ConfigInit.$(ARCH).o \
-$(SRC)/sort.$(ARCH).o \
 $(SRC)/gfitpoly.$(ARCH).o \
 $(SRC)/get_region_coords.$(ARCH).o \
Index: /trunk/Ohana/src/gastro/src/gfit.c
===================================================================
--- /trunk/Ohana/src/gastro/src/gfit.c	(revision 16039)
+++ /trunk/Ohana/src/gastro/src/gfit.c	(revision 16040)
@@ -58,5 +58,5 @@
     tmpN1[i] = i;
   }
-  if (N1 > 1) sort_lists (tmpX1, tmpY1, tmpN1, N1);
+  if (N1 > 1) sort_coords_index (tmpX1, tmpY1, tmpN1, N1);
 
    
@@ -128,5 +128,5 @@
     }
 
-    if (N2 > 1) sort_lists (tmpX2, tmpY2, tmpN2, N2);
+    if (N2 > 1) sort_coords_index (tmpX2, tmpY2, tmpN2, N2);
     
     /* find matched stars */
Index: /trunk/Ohana/src/gastro/src/gfitpoly.c
===================================================================
--- /trunk/Ohana/src/gastro/src/gfitpoly.c	(revision 16039)
+++ /trunk/Ohana/src/gastro/src/gfitpoly.c	(revision 16040)
@@ -64,5 +64,5 @@
     tmpN1[i] = i;
   }
-  if (N1 > 1) sort_lists (tmpX1, tmpY1, tmpN1, N1);
+  if (N1 > 1) sort_coords_index (tmpX1, tmpY1, tmpN1, N1);
   for (i = 0; i < N2; i++) {
     tmpX2[i] = stars2[i].X;
@@ -70,5 +70,5 @@
     tmpN2[i] = i;
   }
-  if (N2 > 1) sort_lists (tmpX2, tmpY2, tmpN2, N2);
+  if (N2 > 1) sort_coords_index (tmpX2, tmpY2, tmpN2, N2);
   
   /* choose iteration ranges */
@@ -119,5 +119,5 @@
 	tmpN1[i] = i;
       }
-      if (N1 > 1) sort_lists (tmpX1, tmpY1, tmpN1, N1);
+      if (N1 > 1) sort_coords_index (tmpX1, tmpY1, tmpN1, N1);
       dX = dY = d2X = d2Y = N = 0;
     }
Index: /trunk/Ohana/src/gastro/src/gstars.c
===================================================================
--- /trunk/Ohana/src/gastro/src/gstars.c	(revision 16039)
+++ /trunk/Ohana/src/gastro/src/gstars.c	(revision 16040)
@@ -9,4 +9,16 @@
 # include <sys/time.h>
 # include <time.h>
+
+void sort_stars_mag (SStars *stars, int N) {
+
+# define SWAPFUNC(A,B){ SStars tmp; tmp = stars[A]; stars[A] = stars[B]; stars[B] = tmp; }
+# define COMPARE(A,B)(stars[A].mag < stars[B].mag)
+
+  OHANA_SORT (N, COMPARE, SWAPFUNC);
+
+# undef SWAPFUNC
+# undef COMPARE
+
+}
 
 SStars *gstars (char *file, int *NSTARS, Coords *coords, int *NX, int *NY, double *dNdM) {
@@ -182,5 +194,5 @@
   }
 
-  sort_stars (stars, N);  /* sorting by magnitude */
+  sort_stars_mag (stars, N);  /* sorting by magnitude */
   Nstars = N;
 
@@ -200,38 +212,4 @@
 }
 
-void sort_stars (SStars *stars, int N) {
-
-  int l,j,ir,i;
-  SStars tempstar;
-  
-  l = N >> 1;
-  ir = N - 1;
-  for (;;) {
-    if (l > 0) {
-      tempstar = stars[--l];
-    }
-    else {
-      tempstar = stars[ir];
-      stars[ir] = stars[0];
-      if (--ir == 0) {
-	stars[0] = tempstar;
-	return;
-      }
-    }
-    i = l;
-    j = (l << 1) + 1;
-    while (j <= ir) {
-      if (j < ir && stars[j].mag < stars[j+1].mag) ++j;
-      if (tempstar.mag < stars[j].mag) {
-	stars[i] = stars[j];
-	j += (i=j) + 1;
-      }
-      else j = ir + 1;
-    }
-    stars[i] = tempstar;
-  }
-}
-
-
 # if (0)
 
Index: unk/Ohana/src/gastro/src/sort.c
===================================================================
--- /trunk/Ohana/src/gastro/src/sort.c	(revision 16039)
+++ 	(revision )
@@ -1,46 +1,0 @@
-
-void sort_lists (double *X, double *Y, int *S, int N) {
-
-  int l,j,ir,i;
-  double tX, tY, tS;
-  
-  l = N >> 1;
-  ir = N - 1;
-  for (;;) {
-    if (l > 0) {
-      l--;
-      tX = X[l];
-      tY = Y[l];
-      tS = S[l];
-    }
-    else {
-      tX = X[ir];
-      X[ir] = X[0];
-      tY = Y[ir];
-      Y[ir] = Y[0];
-      tS = S[ir];
-      S[ir] = S[0];
-      if (--ir == 0) {
-	X[0] = tX;
-	Y[0] = tY;
-	S[0] = tS;
-	return;
-      }
-    }
-    i = l;
-    j = (l << 1) + 1;
-    while (j <= ir) {
-      if (j < ir && X[j] < X[j+1]) j++;
-      if (tX < X[j]) {
-	X[i] = X[j];
-	Y[i] = Y[j];
-	S[i] = S[j];
-	j += (i=j) + 1;
-      }
-      else j = ir + 1;
-    }
-    X[i] = tX;
-    Y[i] = tY;
-    S[i] = tS;
-  }
-}
Index: /trunk/Ohana/src/gastro2/src/grid.c
===================================================================
--- /trunk/Ohana/src/gastro2/src/grid.c	(revision 16039)
+++ /trunk/Ohana/src/gastro2/src/grid.c	(revision 16040)
@@ -55,5 +55,5 @@
     ALLOCATE (ntmp, double, Nbin);
     bcopy (N, ntmp, Nbin*sizeof(double));
-    sort (ntmp, Nbin);
+    dsort (ntmp, Nbin);
     for (i = 0; (ntmp[i] == 0) && (i < Nbin); i++);
     Nval = MIN (Nbin - 1, (int)(0.75*(Nbin - i)) + i);
@@ -168,5 +168,5 @@
   ALLOCATE (ntmp, double, Nbin);
   bcopy (N, ntmp, Nbin*sizeof(double));
-  sort (ntmp, Nbin);
+  dsort (ntmp, Nbin);
   for (i = 0; (ntmp[i] == 0) && (i < Nbin); i++);
   Nval = MIN (Nbin - 1, (int)(0.75*(Nbin - i)) + i);
Index: /trunk/Ohana/src/gastro2/src/lumfunc.c
===================================================================
--- /trunk/Ohana/src/gastro2/src/lumfunc.c	(revision 16039)
+++ /trunk/Ohana/src/gastro2/src/lumfunc.c	(revision 16040)
@@ -60,5 +60,5 @@
 
   /* keep inner 80% */
-  sort_lum (rbin, bin, lbin, Nb);
+  dsortthree (rbin, bin, lbin, Nb);
   for (j = 0, i = 0.1*Nb; i < 0.9*Nb; i++, j++) {
     bin[j]  = bin[i];
Index: /trunk/Ohana/src/gastro2/src/sort.c
===================================================================
--- /trunk/Ohana/src/gastro2/src/sort.c	(revision 16039)
+++ /trunk/Ohana/src/gastro2/src/sort.c	(revision 16040)
@@ -1,205 +1,25 @@
 # include "gastro2.h"
-
-void sort_lum (double *R, double *X, double *Y, int N) {
-
-  int l,j,ir,i;
-  double tX, tY, tR;
-  
-  if (N < 2) return;
-
-  l = N >> 1;
-  ir = N - 1;
-  for (;;) {
-    if (l > 0) {
-      l--;
-      tX = X[l];
-      tY = Y[l];
-      tR = R[l];
-    }
-    else {
-      tX    = X[ir];
-      X[ir] = X[0];
-      tY    = Y[ir];
-      Y[ir] = Y[0];
-      tR    = R[ir];
-      R[ir] = R[0];
-      if (--ir == 0) {
-	X[0] = tX;
-	Y[0] = tY;
-	R[0] = tR;
-	return;
-      }
-    }
-    i = l;
-    j = (l << 1) + 1;
-    while (j <= ir) {
-      if (j < ir && R[j] < R[j+1]) j++;
-      if (tR < R[j]) {
-	X[i] = X[j];
-	Y[i] = Y[j];
-	R[i] = R[j];
-	j += (i=j) + 1;
-      }
-      else j = ir + 1;
-    }
-    X[i] = tX;
-    Y[i] = tY;
-    R[i] = tR;
-  }
-}
-
-void sort_lists (double *X, double *Y, int *S, int N) {
-
-  int l,j,ir,i;
-  double tX, tY, tS;
-  
-  if (N < 2) return;
-
-  l = N >> 1;
-  ir = N - 1;
-  for (;;) {
-    if (l > 0) {
-      l--;
-      tX = X[l];
-      tY = Y[l];
-      tS = S[l];
-    }
-    else {
-      tX = X[ir];
-      X[ir] = X[0];
-      tY = Y[ir];
-      Y[ir] = Y[0];
-      tS = S[ir];
-      S[ir] = S[0];
-      if (--ir == 0) {
-	X[0] = tX;
-	Y[0] = tY;
-	S[0] = tS;
-	return;
-      }
-    }
-    i = l;
-    j = (l << 1) + 1;
-    while (j <= ir) {
-      if (j < ir && X[j] < X[j+1]) j++;
-      if (tX < X[j]) {
-	X[i] = X[j];
-	Y[i] = Y[j];
-	S[i] = S[j];
-	j += (i=j) + 1;
-      }
-      else j = ir + 1;
-    }
-    X[i] = tX;
-    Y[i] = tY;
-    S[i] = tS;
-  }
-}
-
-void sort (double *X, int N) {
-
-  int l,j,ir,i;
-  double tmp;
-
-  if (N < 2) return;
-  
-  l = N >> 1;
-  ir = N - 1;
-  for (;;) {
-    if (l > 0) {
-      l--;
-      tmp = X[l];
-    }
-    else {
-      tmp = X[ir];
-      X[ir] = X[0];
-      if (--ir == 0) {
-	X[0] = tmp;
-	return;
-      }
-    }
-    i = l;
-    j = (l << 1) + 1;
-    while (j <= ir) {
-      if (j < ir && X[j] < X[j+1]) j++;
-      if (tmp < X[j]) {
-	X[i] = X[j];
-	j += (i=j) + 1;
-      }
-      else j = ir + 1;
-    }
-    X[i] = tmp;
-  }
-}
 
 void sort_stars_mag (StarData *stars, int N) {
 
-  int l,j,ir,i;
-  StarData tempstar;
-  
-  if (N < 2) return;
+# define SWAPFUNC(A,B){ StarData tmp; tmp = stars[A]; stars[A] = stars[B]; stars[B] = tmp; }
+# define COMPARE(A,B)(stars[A].M < stars[B].M)
 
-  l = N >> 1;
-  ir = N - 1;
-  for (;;) {
-    if (l > 0) {
-      tempstar = stars[--l];
-    }
-    else {
-      tempstar = stars[ir];
-      stars[ir] = stars[0];
-      if (--ir == 0) {
-	stars[0] = tempstar;
-	return;
-      }
-    }
-    i = l;
-    j = (l << 1) + 1;
-    while (j <= ir) {
-      if (j < ir && stars[j].M < stars[j+1].M) ++j;
-      if (tempstar.M < stars[j].M) {
-	stars[i] = stars[j];
-	j += (i=j) + 1;
-      }
-      else j = ir + 1;
-    }
-    stars[i] = tempstar;
-  }
+  OHANA_SORT (N, COMPARE, SWAPFUNC);
+
+# undef SWAPFUNC
+# undef COMPARE
+
 }
 
 void sort_stars_X (StarData *stars, int N) {
 
-  int l,j,ir,i;
-  StarData tempstar;
-  
-  if (N < 2) return;
+# define SWAPFUNC(A,B){ StarData tmp; tmp = stars[A]; stars[A] = stars[B]; stars[B] = tmp; }
+# define COMPARE(A,B)(stars[A].X < stars[B].X)
 
-  l = N >> 1;
-  ir = N - 1;
-  for (;;) {
-    if (l > 0) {
-      tempstar = stars[--l];
-    }
-    else {
-      tempstar = stars[ir];
-      stars[ir] = stars[0];
-      if (--ir == 0) {
-	stars[0] = tempstar;
-	return;
-      }
-    }
-    i = l;
-    j = (l << 1) + 1;
-    while (j <= ir) {
-      if (j < ir && stars[j].X < stars[j+1].X) ++j;
-      if (tempstar.X < stars[j].X) {
-	stars[i] = stars[j];
-	j += (i=j) + 1;
-      }
-      else j = ir + 1;
-    }
-    stars[i] = tempstar;
-  }
+  OHANA_SORT (N, COMPARE, SWAPFUNC);
+
+# undef SWAPFUNC
+# undef COMPARE
+
 }
-
-
Index: /trunk/Ohana/src/gcompare/include/gcompare.h
===================================================================
--- /trunk/Ohana/src/gcompare/include/gcompare.h	(revision 16039)
+++ /trunk/Ohana/src/gcompare/include/gcompare.h	(revision 16040)
@@ -28,5 +28,5 @@
 int                get_argument      PROTO((int, char **, char *));
 int                remove_argument   PROTO((int, int *, char **));
-void               sort              PROTO((data_type));
+void               data_sort         PROTO((data_type));
 void               output            PROTO((data_type, data_type, match_type *, int, int, int, int, int));
 char              *nextword          PROTO((char *));
Index: /trunk/Ohana/src/gcompare/src/gcompare.c
===================================================================
--- /trunk/Ohana/src/gcompare/src/gcompare.c	(revision 16039)
+++ /trunk/Ohana/src/gcompare/src/gcompare.c	(revision 16040)
@@ -71,6 +71,6 @@
   fprintf (stderr, "list 2: %d values %d %d\n", data2.Nvalues, X2, Y2);
 
-  sort (data1); fprintf (stderr, "sorted 1\n");
-  sort (data2); fprintf (stderr, "sorted 2\n");
+  data_sort (data1); fprintf (stderr, "sorted 1\n");
+  data_sort (data2); fprintf (stderr, "sorted 2\n");
 
   fprintf (stderr, "noauto: %e\n", noauto);
Index: /trunk/Ohana/src/gcompare/src/neighbors.c
===================================================================
--- /trunk/Ohana/src/gcompare/src/neighbors.c	(revision 16039)
+++ /trunk/Ohana/src/gcompare/src/neighbors.c	(revision 16040)
@@ -46,5 +46,5 @@
   fprintf (stderr, "list 1: %d values %d %d\n", data1.Nvalues, X1, Y1);
 
-  sort (data1); fprintf (stderr, "sorted 1\n");
+  data_sort (data1); fprintf (stderr, "sorted 1\n");
 
   count_neighbors (data1, radius, DX, DY);
Index: /trunk/Ohana/src/gcompare/src/sort.c
===================================================================
--- /trunk/Ohana/src/gcompare/src/sort.c	(revision 16039)
+++ /trunk/Ohana/src/gcompare/src/sort.c	(revision 16040)
@@ -1,40 +1,13 @@
 # include "gcompare.h"
 
-void sort (data)
-data_type data;
-{
-  int l,j,ir,i, N;
-  value_type temp;
+void data_sort (data_type data) {
 
-  N = data.Nvalues;
-  if (N == 1)
-    return;
+# define SWAPFUNC(A,B){ value_type tmp; tmp = data.values[A]; data.values[A] = data.values[B]; data.values[B] = tmp; }
+# define COMPARE(A,B)(data.values[A].X < data.values[B].X)
 
-  l = N >> 1;
-  ir = N - 1;
-  for (;;) {
-    if (l > 0) {
-      temp = data.values[--l];
-    }
-    else {
-      temp = data.values[ir];
-      data.values[ir] = data.values[0];
-      if (--ir == 0) {
-        data.values[0] = temp;
-        return;
-      }
-    }
-    i = l;
-    j = (l << 1) + 1;
-    while (j <= ir) {
-      if (j < ir && data.values[j].X < data.values[j+1].X) ++j;
-      if (temp.X < data.values[j].X) {
-        data.values[i]=data.values[j];
-        j += (i=j) + 1;
-      }
-      else j = ir + 1;
-    }
-    data.values[i] = temp;
-  }
+  OHANA_SORT (data.Nvalues, COMPARE, SWAPFUNC);
+
+# undef SWAPFUNC
+# undef COMPARE
+
 }
-
Index: /trunk/Ohana/src/gophot/Makefile
===================================================================
--- /trunk/Ohana/src/gophot/Makefile	(revision 16039)
+++ /trunk/Ohana/src/gophot/Makefile	(revision 16040)
@@ -33,5 +33,5 @@
 $(SRC)/shape.$(ARCH).o          $(SRC)/galaxy.$(ARCH).o \
 $(SRC)/transmask.$(ARCH).o      $(SRC)/cosmic.$(ARCH).o \
-$(SRC)/impaper2.$(ARCH).o       $(SRC)/sort.$(ARCH).o \
+$(SRC)/impaper2.$(ARCH).o       \
 $(SRC)/oblit.$(ARCH).o          $(SRC)/toobright.$(ARCH).o \
 $(SRC)/Chisq.$(ARCH).o          $(SRC)/oblims.$(ARCH).o \
Index: /trunk/Ohana/src/gophot/src/fillerup.c
===================================================================
--- /trunk/Ohana/src/gophot/src/fillerup.c	(revision 16039)
+++ /trunk/Ohana/src/gophot/src/fillerup.c	(revision 16040)
@@ -50,5 +50,5 @@
   if (nsky == 0) return (FALSE);
 
-  sort (ts, nsky);
+  fsort (ts, nsky);
   sky = ts[(int)(0.5*nsky)];
   sum2 = sky;
Index: /trunk/Ohana/src/gophot/src/impaper2.c
===================================================================
--- /trunk/Ohana/src/gophot/src/impaper2.c	(revision 16039)
+++ /trunk/Ohana/src/gophot/src/impaper2.c	(revision 16040)
@@ -20,5 +20,5 @@
   if (nsort < 1) return (0);
 
-  sort (zsort, nsort);
+  fsort (zsort, nsort);
         
   sky = 0;
Index: /trunk/Ohana/src/gophot/src/mediansky.c
===================================================================
--- /trunk/Ohana/src/gophot/src/mediansky.c	(revision 16039)
+++ /trunk/Ohana/src/gophot/src/mediansky.c	(revision 16040)
@@ -4,5 +4,4 @@
 static int Nx, Ny;
 static float fx, fy;
-sort (float *, int);
 
 int make_mediansky () {
@@ -59,5 +58,5 @@
       }
 
-      sort (temp, n);
+      fsort (temp, n);
       value = temp[(int)(0.5*n)];
 
@@ -86,5 +85,5 @@
   memcpy (temp, mediansky, Npix*sizeof(float));
 
-  sort (temp, Npix);
+  fsort (temp, Npix);
 
   *sky = temp[(int)(0.5*Npix)];
Index: /trunk/Ohana/src/gophot/src/paravg.c
===================================================================
--- /trunk/Ohana/src/gophot/src/paravg.c	(revision 16039)
+++ /trunk/Ohana/src/gophot/src/paravg.c	(revision 16040)
@@ -1,3 +1,18 @@
 # include "gophot.h"
+
+sortkey (int *index, float *key, int N) {
+
+# define SWAPFUNC(A,B){ \
+  int   itmp; itmp = index[A]; index[A] = index[B]; index[B] = itmp; \
+  float ftmp; ftmp = key[A]; key[A] = key[B]; key[B] = ftmp; \
+}
+# define COMPARE(A,B)(key[A] < key[B])
+
+  OHANA_SORT (N, COMPARE, SWAPFUNC);
+
+# undef SWAPFUNC
+# undef COMPARE
+
+}
 
 paravg () {
Index: unk/Ohana/src/gophot/src/sort.c
===================================================================
--- /trunk/Ohana/src/gophot/src/sort.c	(revision 16039)
+++ 	(revision )
@@ -1,77 +1,0 @@
-
-sort (float *value, int N) {
-
-  int l,j,ir,i;
-  float temp;
-  
-  if (N < 2) return;
-  l = N >> 1;
-  ir = N - 1;
-  for (;;) {
-    if (l > 0) {
-      temp = value[--l];
-    }
-    else {
-      temp = value[ir];
-      value[ir] = value[0];
-      if (--ir == 0) {
-	value[0] = temp;
-	return;
-      }
-    }
-    i = l;
-    j = (l << 1) + 1;
-    while (j <= ir) {
-      if (j < ir && value[j] < value[j+1]) ++j;
-      if (temp < value[j]) {
-	value[i]=value[j];
-	j += (i=j) + 1;
-      }
-      else j = ir + 1;
-    }
-    value[i] = temp;
-  }
-}
-
-sortkey (int *index, float *key, int N) {
-
-  int l,j,ir,i;
-  float ktmp;
-  int  itmp;
-  
-  if (N < 2) return;
-  l = N >> 1;
-  ir = N - 1;
-  for (;;) {
-    if (l > 0) {
-      l--;
-      ktmp = key[l];
-      itmp = index[l];
-    }
-    else {
-      ktmp    = key[ir];
-      key[ir] = key[0];
-      itmp      = index[ir];
-      index[ir] = index[0];
-      ir--;
-      if (ir == 0) {
-	key[0] = ktmp;
-	index[0] = itmp;
-	return;
-      }
-    }
-    i = l;
-    j = (l << 1) + 1;
-    while (j <= ir) {
-      if (j < ir && key[j] < key[j+1]) ++j;
-      if (ktmp < key[j]) {
-	key[i] = key[j];
-	index[i] = index[j];
-	j += (i=j) + 1;
-      }
-      else j = ir + 1;
-    }
-    key[i] = ktmp;
-    index[i] = itmp;
-  }
-}
Index: /trunk/Ohana/src/imclean/src/sort_stars.c
===================================================================
--- /trunk/Ohana/src/imclean/src/sort_stars.c	(revision 16039)
+++ /trunk/Ohana/src/imclean/src/sort_stars.c	(revision 16040)
@@ -1,36 +1,13 @@
 # include "imclean.h"
 
-void sort_stars (SMPData *X, int N) {
+void sort_stars (SMPData *stars, int N) {
 
-  int l,j,ir,i;
-  SMPData tX;
-  
-  if (N < 1) return;
-  l = N >> 1;
-  ir = N - 1;
-  for (;;) {
-    if (l > 0) {
-      l--;
-      tX = X[l];
-    }
-    else {
-      tX = X[ir];
-      X[ir] = X[0];
-      if (--ir == 0) {
-	X[0] = tX;
-	return;
-      }
-    }
-    i = l;
-    j = (l << 1) + 1;
-    while (j <= ir) {
-      if (j < ir && X[j].X < X[j+1].X) j++;
-      if (tX.X < X[j].X) {
-	X[i] = X[j];
-	j += (i=j) + 1;
-      }
-      else j = ir + 1;
-    }
-    X[i] = tX;
-  }
+# define SWAPFUNC(A,B){ SMPData tmp; tmp = stars[A]; stars[A] = stars[B]; stars[B] = tmp; }
+# define COMPARE(A,B)(stars[A].X < stars[B].X)
+
+  OHANA_SORT (N, COMPARE, SWAPFUNC);
+
+# undef SWAPFUNC
+# undef COMPARE
+
 }
Index: /trunk/Ohana/src/imregister/Makefile
===================================================================
--- /trunk/Ohana/src/imregister/Makefile	(revision 16039)
+++ /trunk/Ohana/src/imregister/Makefile	(revision 16040)
@@ -142,6 +142,5 @@
 $(BASEDIR)/version.$(ARCH).o      \
 $(BASEDIR)/parse_time.$(ARCH).o   \
-$(BASEDIR)/fits_scan_nchar.$(ARCH).o \
-$(BASEDIR)/sort.$(ARCH).o
+$(BASEDIR)/fits_scan_nchar.$(ARCH).o
 
 $(SRC)/photcode-table.$(ARCH).o \
Index: /trunk/Ohana/src/imregister/base/sort.c
===================================================================
--- /trunk/Ohana/src/imregister/base/sort.c	(revision 16039)
+++ /trunk/Ohana/src/imregister/base/sort.c	(revision 16040)
@@ -1,71 +1,3 @@
 # include <string.h>
-
-void sort (float *value, int N) {
-
-  int l,j,ir,i;
-  float temp;
-  
-  if (N < 2) return;
-  l = N >> 1;
-  ir = N - 1;
-  for (;;) {
-    if (l > 0) {
-      temp = value[--l];
-    }
-    else {
-      temp = value[ir];
-      value[ir] = value[0];
-      if (--ir == 0) {
-	value[0] = temp;
-	return;
-      }
-    }
-    i = l;
-    j = (l << 1) + 1;
-    while (j <= ir) {
-      if (j < ir && value[j] < value[j+1]) ++j;
-      if (temp < value[j]) {
-	value[i]=value[j];
-	j += (i=j) + 1;
-      }
-      else j = ir + 1;
-    }
-    value[i] = temp;
-  }
-}
-
-void dsort (double *value, int N) {
-
-  int l,j,ir,i;
-  double temp;
-  
-  if (N < 2) return;
-  l = N >> 1;
-  ir = N - 1;
-  for (;;) {
-    if (l > 0) {
-      temp = value[--l];
-    }
-    else {
-      temp = value[ir];
-      value[ir] = value[0];
-      if (--ir == 0) {
-	value[0] = temp;
-	return;
-      }
-    }
-    i = l;
-    j = (l << 1) + 1;
-    while (j <= ir) {
-      if (j < ir && value[j] < value[j+1]) ++j;
-      if (temp < value[j]) {
-	value[i]=value[j];
-	j += (i=j) + 1;
-      }
-      else j = ir + 1;
-    }
-    value[i] = temp;
-  }
-}
 
 void sortstr (char **S, int *X, int N) {
Index: /trunk/Ohana/src/imregister/imreg/cadc.c
===================================================================
--- /trunk/Ohana/src/imregister/imreg/cadc.c	(revision 16039)
+++ /trunk/Ohana/src/imregister/imreg/cadc.c	(revision 16040)
@@ -212,5 +212,5 @@
     entry[i] = match[i];
   }
-  sortpair (index, entry, Nmatch);
+  isortpair (index, entry, Nmatch);
   *Index = index;
   *Entry = entry;
Index: /trunk/Ohana/src/imregister/imreg/unique.c
===================================================================
--- /trunk/Ohana/src/imregister/imreg/unique.c	(revision 16039)
+++ /trunk/Ohana/src/imregister/imreg/unique.c	(revision 16040)
@@ -63,2 +63,17 @@
   return (match);
 }
+
+void sortstr (char **S, int *X, int N) {
+
+# define SWAPFUNC(A,B){ \
+  char *tmp = S[A]; S[A] = S[B]; S[B] = tmp; \
+  int  itmp = X[A]; X[A] = X[B]; X[B] = itmp; \
+}
+# define COMPARE(A,B)(strcmp(S[A], S[B]) < 0)
+
+  OHANA_SORT (N, COMPARE, SWAPFUNC);
+
+# undef SWAPFUNC
+# undef COMPARE
+
+}
Index: /trunk/Ohana/src/libdvo/Makefile
===================================================================
--- /trunk/Ohana/src/libdvo/Makefile	(revision 16039)
+++ /trunk/Ohana/src/libdvo/Makefile	(revision 16040)
@@ -36,4 +36,5 @@
 $(SRC)/version.$(ARCH).o	 \
 $(SRC)/coordops.$(ARCH).o	 \
+$(SRC)/dvosorts.$(ARCH).o	 \
 $(SRC)/dvo_photcode_ops.$(ARCH).o \
 $(SRC)/dvo_photcode_convert_elixir.$(ARCH).o \
Index: /trunk/Ohana/src/libdvo/include/dvo.h
===================================================================
--- /trunk/Ohana/src/libdvo/include/dvo.h	(revision 16039)
+++ /trunk/Ohana/src/libdvo/include/dvo.h	(revision 16040)
@@ -412,3 +412,10 @@
 int        SkyTableSetFilenames    PROTO((SkyTable *sky, char *path, char *ext));
 
+/* dvo-specific sorting functions */
+void sortave (Average *ave, int N);
+void sort_image_subset (Image *image, int *subset, int N);
+void sort_coords_index (double *X, double *Y, int *S, int N);
+void sort_stars_ra (Stars *stars, int N);
+void sort_regions (SkyRegion *region, int N);
+
 # endif
Index: /trunk/Ohana/src/libdvo/src/dvosorts.c
===================================================================
--- /trunk/Ohana/src/libdvo/src/dvosorts.c	(revision 16040)
+++ /trunk/Ohana/src/libdvo/src/dvosorts.c	(revision 16040)
@@ -0,0 +1,71 @@
+# include <dvo.h>
+
+/* several dvo-specific sorting functions used in a number of locations */
+
+/* values are ave[i].R, ave[i].D, ave[i].M */
+void sortave (Average *ave, int N) {
+
+# define SWAPFUNC(A,B){ Average tmp; tmp = ave[A]; ave[A] = ave[B]; ave[B] = tmp; }
+# define COMPARE(A,B)(ave[A].R < ave[B].R)
+
+  OHANA_SORT (N, COMPARE, SWAPFUNC);
+
+# undef SWAPFUNC
+# undef COMPARE
+
+}
+
+/* sort subset by image[subset[i]].tzero */
+void sort_image_subset (Image *image, int *subset, int N) {
+
+# define SWAPFUNC(A,B){ int tmp; tmp = subset[A]; subset[A] = subset[B]; subset[B] = tmp; }
+# define COMPARE(A,B)(image[subset[A]].tzero < image[subset[B]].tzero)
+
+  OHANA_SORT (N, COMPARE, SWAPFUNC);
+
+# undef SWAPFUNC
+# undef COMPARE
+
+}
+
+/* sort a coordinate pair (X,Y) and the associated index (S) */
+void sort_coords_index (double *X, double *Y, int *S, int N) {
+  
+# define SWAPFUNC(A,B){ double dtmp; int itmp; \
+  dtmp = X[A]; X[A] = X[B]; X[B] = dtmp; \
+  dtmp = Y[A]; Y[A] = Y[B]; Y[B] = dtmp; \
+  itmp = S[A]; S[A] = S[B]; S[B] = itmp; \
+}
+# define COMPARE(A,B)(X[A] < X[B])
+
+  OHANA_SORT (N, COMPARE, SWAPFUNC);
+
+# undef SWAPFUNC
+# undef COMPARE
+
+}
+
+void sort_stars_ra (Stars *stars, int N) {
+
+# define SWAPFUNC(A,B){ Stars tmp; tmp = stars[A]; stars[A] = stars[B]; stars[B] = tmp; }
+# define COMPARE(A,B)(stars[A].R < stars[B].R)
+
+  OHANA_SORT (N, COMPARE, SWAPFUNC);
+
+# undef SWAPFUNC
+# undef COMPARE
+
+}
+
+void sort_regions (SkyRegion *region, int N) {
+
+# define SWAPFUNC(A,B){ SkyRegion tmp; tmp = region[A]; region[A] = region[B]; region[B] = tmp; }
+# define COMPARE(A,B)(region[A].Dmin < region[B].Dmin)
+
+  OHANA_SORT (N, COMPARE, SWAPFUNC);
+
+# undef SWAPFUNC
+# undef COMPARE
+
+}
+
Index: /trunk/Ohana/src/libdvo/src/skyregion_gsc.c
===================================================================
--- /trunk/Ohana/src/libdvo/src/skyregion_gsc.c	(revision 16039)
+++ /trunk/Ohana/src/libdvo/src/skyregion_gsc.c	(revision 16040)
@@ -206,47 +206,20 @@
 void SkyTableSort (SkyTable *table) {
 
-  int i, j, l, ir, N;
   SkyRegion *regions;
-  SkyRegion tempregion;
-  char *tempfile = NULL;
   char **filename = NULL;
-  
-  N = table[0].Nregions;
+
   regions = table[0].regions;
   filename = table[0].filename;
 
-  if (N < 2) return;
-  l = N >> 1;
-  ir = N - 1;
-  for (;;) {
-    if (l > 0) {
-      l--;
-      tempregion = regions[l];
-      if (filename) tempfile   = filename[l];
-    } else {
-      tempregion   = regions[ir];
-      if (filename) tempfile     = filename[ir];
-      regions[ir]   = regions[0];
-      if (filename) filename[ir] = filename[0];
-      if (--ir == 0) {
-	regions[0]   = tempregion;
-	if (filename) filename[0] = tempfile;
-	return;
-      }
-    }
-    i = l;
-    j = (l << 1) + 1;
-    while (j <= ir) {
-      if (j < ir && regions[j].Rmin < regions[j+1].Rmin) ++j;
-      if (tempregion.Rmin < regions[j].Rmin) {
-	regions[i]   = regions[j];
-	if (filename) filename[i] = filename[j];
-	j += (i=j) + 1;
-      } 
-      else j = ir + 1;
-    }
-    regions[i]   = tempregion;
-    if (filename) filename[i] = tempfile;
-  }
+# define SWAPFUNC(A,B){ int tmp; \
+  char     *tempfile = filename[A]; filename[A] = filename[B]; filename[B] = tempfile; \
+  SkyRegion tempregion = regions[A]; regions[A] = regions[B]; regions[B] = tempregion; \
+}
+# define COMPARE(A,B)(regions[A].Rmin < regions[B].Rmin)
+
+  OHANA_SORT (table[0].Nregions, COMPARE, SWAPFUNC);
+
+# undef SWAPFUNC
+# undef COMPARE
 }
 
Index: /trunk/Ohana/src/libohana/Makefile
===================================================================
--- /trunk/Ohana/src/libohana/Makefile	(revision 16039)
+++ /trunk/Ohana/src/libohana/Makefile	(revision 16040)
@@ -27,10 +27,11 @@
 
 INCS = \
-$(DESTINC)/Xohana.h  \
 $(DESTINC)/ohana_allocate.h \
+$(DESTINC)/ohana_sort.h \
 $(DESTINC)/ohana.h
 
 OBJS = \
 $(SRC)/ohana_allocate.$(ARCH).o  \
+$(SRC)/sorts.$(ARCH).o		 \
 $(SRC)/string.$(ARCH).o		 \
 $(SRC)/findexec.$(ARCH).o	 \
Index: /trunk/Ohana/src/libohana/include/ohana.h
===================================================================
--- /trunk/Ohana/src/libohana/include/ohana.h	(revision 16039)
+++ /trunk/Ohana/src/libohana/include/ohana.h	(revision 16040)
@@ -128,4 +128,7 @@
 # define OHANA_WHITESPACE(c)(((c) == 0x09) || ((c) == 0x0a) || ((c) == 0x0b) || ((c) == 0x0b) || ((c) == 0x0c) || ((c) == 0x0d) || ((c) == 0x20))
 
+// sorting is now defined as a macro call
+# include <ohana_sort.h>
+
 /* socket / pipe communication buffer */
 typedef struct {
Index: /trunk/Ohana/src/libohana/include/ohana_sort.h
===================================================================
--- /trunk/Ohana/src/libohana/include/ohana_sort.h	(revision 16040)
+++ /trunk/Ohana/src/libohana/include/ohana_sort.h	(revision 16040)
@@ -0,0 +1,75 @@
+// Heap sort based on descriptions in Sedgewick "Algorithms in C"
+//
+// Copyright (C) 1999  Thomas Walter
+// Copyright (C) 2007  Paul Price, Institute for Astronomy, University of Hawaii
+// Copyright (C) 2008  Eugene Magnier, Institute for Astronomy, University of Hawaii
+//
+// 18 February 2000: Modified for GSL by Brian Gough
+// 29 November 2007: Modified for psLib by Paul Price
+// 07 January 2008: Modified for ohana by Eugene Magnier
+//
+//
+// This is free software; you can redistribute it and/or modify it
+// under the terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 2, or (at your option) any
+// later version.
+//
+// This source is distributed in the hope that it will be useful, but WITHOUT
+// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+// for more details.
+//
+
+#ifndef OHANA_SORT_H
+#define OHANA_SORT_H
+
+// The following sort code is based on gsl_heapsort from GSL-1.8 (www.gnu.org/software/gsl), file
+// gsl-1.8/sort/sort.c, which is distributed under the GNU General Public License, version 2.
+
+// Use of the PSSORT() macro requires additional macros defined:
+// COMPAREEXPR(A,B): expression to compare element A with element B; true if element A is smaller than B.
+// SWAPFUNC(A,B): swap element A with element B
+
+# define OHANA_DOWNHEAP(COMPARE, SWAPFUNC, K) { \
+  unsigned long k = K; /* Local version of i in main loop */ \
+  while (k <= last / 2) {  \
+    unsigned long j = 2 * k;  \
+    if ((j < last) && COMPARE(j, j + 1)) {  \
+      j++;  \
+    }  \
+    if (COMPARE(k, j)) {  \
+      SWAPFUNC(j, k);  \
+    } else {  \
+      break;  \
+    }  \
+    k = j;  \
+  } \
+}
+
+# define OHANA_SORT(NVALUE, COMPARE, SWAPFUNC) { \
+  unsigned long last = NVALUE - 1; \
+  unsigned long i = last / 2 + 1; \
+  if (NVALUE > 1) { \
+    do { \
+      i--;  \
+      OHANA_DOWNHEAP (COMPARE, SWAPFUNC, i); \
+    } while (i > 0); \
+    while (last > 0) { \
+      SWAPFUNC(0, last); /* Swap elements */ \
+      /* Process the heap */ \
+      last--; \
+      OHANA_DOWNHEAP (COMPARE, SWAPFUNC, 0); \
+    } \
+  } \
+}
+
+// pre-defined function versions
+void dsort (double *value, int N);
+void fsort (float *value, int N);
+void fsortpair (float *X, float *Y, int N);
+void dsortpair (double *X, double *Y, int N);
+void isortpair (int *X, int *Y, int N);
+void fsortthree (float *X, float *Y, float *Z, int N);
+void dsortthree (double *X, double *Y, double *Z, int N);
+
+#endif
Index: /trunk/Ohana/src/libohana/src/sorts.c
===================================================================
--- /trunk/Ohana/src/libohana/src/sorts.c	(revision 16040)
+++ /trunk/Ohana/src/libohana/src/sorts.c	(revision 16040)
@@ -0,0 +1,105 @@
+# include <ohana.h>
+
+/* various widely-used sort functions for specific sets of types */
+
+void dsort (double *value, int N) {
+
+# define SWAPFUNC(A,B){ double tmp = value[A]; value[A] = value[B]; value[B] = tmp; }
+# define COMPARE(A,B)(value[A] < value[B])
+
+  OHANA_SORT (N, COMPARE, SWAPFUNC);
+
+# undef SWAPFUNC
+# undef COMPARE
+
+}
+
+void fsort (float *value, int N) {
+
+# define SWAPFUNC(A,B){ float tmp = value[A]; value[A] = value[B]; value[B] = tmp; }
+# define COMPARE(A,B)(value[A] < value[B])
+
+  OHANA_SORT (N, COMPARE, SWAPFUNC);
+
+# undef SWAPFUNC
+# undef COMPARE
+
+}
+
+void fsortpair (float *X, float *Y, int N) {
+
+# define SWAPFUNC(A,B){ float tmp; \
+  tmp = X[A]; X[A] = X[B]; X[B] = tmp; \
+  tmp = Y[A]; Y[A] = Y[B]; Y[B] = tmp; \
+}
+# define COMPARE(A,B)(X[A] < X[B])
+
+  OHANA_SORT (N, COMPARE, SWAPFUNC);
+
+# undef SWAPFUNC
+# undef COMPARE
+
+}
+
+void dsortpair (double *X, double *Y, int N) {
+
+# define SWAPFUNC(A,B){ double tmp; \
+  tmp = X[A]; X[A] = X[B]; X[B] = tmp; \
+  tmp = Y[A]; Y[A] = Y[B]; Y[B] = tmp; \
+}
+# define COMPARE(A,B)(X[A] < X[B])
+
+  OHANA_SORT (N, COMPARE, SWAPFUNC);
+
+# undef SWAPFUNC
+# undef COMPARE
+
+}
+
+// sort two int vectors by first vector
+void isortpair (int *X, int *Y, int N) {
+
+# define SWAPFUNC(A,B){ int tmp; \
+  tmp = X[A]; X[A] = X[B]; X[B] = tmp; \
+  tmp = Y[A]; Y[A] = Y[B]; Y[B] = tmp; \
+}
+# define COMPARE(A,B)(X[A] < X[B])
+
+  OHANA_SORT (N, COMPARE, SWAPFUNC);
+
+# undef SWAPFUNC
+# undef COMPARE
+
+}
+
+void fsortthree (float *X, float *Y, float *Z, int N) {
+
+# define SWAPFUNC(A,B){ float tmp; \
+  tmp = X[A]; X[A] = X[B]; X[B] = tmp; \
+  tmp = Y[A]; Y[A] = Y[B]; Y[B] = tmp; \
+  tmp = Z[A]; Z[A] = Z[B]; Z[B] = tmp; \
+}
+# define COMPARE(A,B)(X[A] < X[B])
+
+  OHANA_SORT (N, COMPARE, SWAPFUNC);
+
+# undef SWAPFUNC
+# undef COMPARE
+
+}
+
+void dsortthree (double *X, double *Y, double *Z, int N) {
+
+# define SWAPFUNC(A,B){ double tmp; \
+  tmp = X[A]; X[A] = X[B]; X[B] = tmp; \
+  tmp = Y[A]; Y[A] = Y[B]; Y[B] = tmp; \
+  tmp = Z[A]; Z[A] = Z[B]; Z[B] = tmp; \
+}
+# define COMPARE(A,B)(X[A] < X[B])
+
+  OHANA_SORT (N, COMPARE, SWAPFUNC);
+
+# undef SWAPFUNC
+# undef COMPARE
+
+}
Index: /trunk/Ohana/src/lightcurve/Makefile
===================================================================
--- /trunk/Ohana/src/lightcurve/Makefile	(revision 16039)
+++ /trunk/Ohana/src/lightcurve/Makefile	(revision 16040)
@@ -26,5 +26,4 @@
 $(SRC)/get_info.$(ARCH).o 	$(SRC)/make_table.$(ARCH).o \
 $(SRC)/lightcurve.$(ARCH).o	\
-$(SRC)/sort_seq.$(ARCH).o	\
 $(SRC)/help.$(ARCH).o 
 
Index: unk/Ohana/src/lightcurve/src/sort_seq.c
===================================================================
--- /trunk/Ohana/src/lightcurve/src/sort_seq.c	(revision 16039)
+++ 	(revision )
@@ -1,39 +1,0 @@
-# include "lightcurve.h"
-
-void sort_seq (seq, value, N) 
-int *seq;
-double *value;
-int N;
-{
-  int l,j,ir,i;
-  int temp;
-  
-  l = N >> 1;
-  ir = N - 1;
-  for (;;) {
-    if (l > 0) {
-      temp = seq[--l];
-    }
-    else {
-      temp = seq[ir];
-      seq[ir] = seq[0];
-      if (--ir == 0) {
-	seq[0] = temp;
-	return;
-      }
-    }
-    i = l;
-    j = (l << 1) + 1;
-    while (j <= ir) {
-      if (j < ir && value[seq[j]] < value[seq[j+1]]) ++j;
-      if (value[temp] < value[seq[j]]) {
-	seq[i]=seq[j];
-	j += (i=j) + 1;
-      }
-      else j = ir + 1;
-    }
-    seq[i] = temp;
-  }
-}
-
-
Index: /trunk/Ohana/src/lightcurve/src/sort_stars.c
===================================================================
--- /trunk/Ohana/src/lightcurve/src/sort_stars.c	(revision 16039)
+++ /trunk/Ohana/src/lightcurve/src/sort_stars.c	(revision 16040)
@@ -1,24 +1,23 @@
 # include "lightcurve.h"
 
-void sort_stars (radec, stars, Nstars)
-int  **radec;
-Star  *stars;
-int    Nstars;
-{
+// construct an index for the stars sorted by RA
+void sort_stars (int **index, Star *stars, int Nstars) {
   
   int i;
   double *RAs;
 
-  ALLOCATE (radec[0], int, Nstars);
-  ALLOCATE (RAs, double, Nstars);
+  ALLOCATE (myIndex, int, Nstars);
   for (i = 0; i < Nstars; i++) {
-    radec[0][i] = i;
-    RAs[i] = stars[i].RA;
+    myIndex[i] = i;
   }
 
-  sort_seq (radec[0], RAs, Nstars);
-  free (RAs);
+# define SWAPFUNC(A,B){ int tmp = myIndex[A]; myIndex[A] = myIndex[B]; myIndex[B] = tmp; }
+# define COMPARE(A,B)(stars[myIndex[A]].RA < stars[myIndex[B]].RA)
 
+  OHANA_SORT (Nstars, COMPARE, SWAPFUNC);
+
+# undef SWAPFUNC
+# undef COMPARE
+
+  *index = myIndex;
 }
-
-
Index: /trunk/Ohana/src/markrock/include/markrock.h
===================================================================
--- /trunk/Ohana/src/markrock/include/markrock.h	(revision 16039)
+++ /trunk/Ohana/src/markrock/include/markrock.h	(revision 16040)
@@ -4,5 +4,5 @@
 typedef struct {
   Coords coords;
-  float *X, *Y;
+  double *X, *Y;
   int *N;
   double RA[2], DEC[2];
Index: /trunk/Ohana/src/markrock/src/find_bright_stars.c
===================================================================
--- /trunk/Ohana/src/markrock/src/find_bright_stars.c	(revision 16039)
+++ /trunk/Ohana/src/markrock/src/find_bright_stars.c	(revision 16040)
@@ -1,8 +1,5 @@
 # include "markrock.h"
 
-find_bright_stars (catalog, catstats)
-Catalog catalog[];
-CatStats catstats[];
-{
+void find_bright_stars (Catalog *catalog, CatStats *catstats) {
 
   int i, j, n, m, first_j, Nave, Ngsc, Nmark;
@@ -10,5 +7,5 @@
   double MinRA, MinDEC, MaxRA, MaxDEC, RaCenter, DecCenter;
   Coords *tcoords;
-  float *X1, *Y1, *X2, *Y2;
+  double *X1, *Y1, *X2, *Y2;
   int *N1, *N2;
   char *mark;
@@ -25,6 +22,6 @@
   Y2 = catstats[0].Y;
   N2 = catstats[0].N;
-  ALLOCATE (X1, float, Ngsc);
-  ALLOCATE (Y1, float, Ngsc);
+  ALLOCATE (X1, double, Ngsc);
+  ALLOCATE (Y1, double, Ngsc);
   ALLOCATE (N1, int, Ngsc);
   ALLOCATE (mark, char, Nave);
@@ -33,8 +30,8 @@
   tcoords = &catstats[0].coords;
   for (i = 0; i < Ngsc; i++) {
-    fRD_to_XY (&X1[i], &Y1[i], GSCdata.average[i].R, GSCdata.average[i].D, tcoords);
+    RD_to_XY (&X1[i], &Y1[i], GSCdata.average[i].R, GSCdata.average[i].D, tcoords);
     N1[i] = i;
   }
-  if (Ngsc > 1) sort_lists (X1, Y1, N1, Ngsc);
+  if (Ngsc > 1) sort_coords_index (X1, Y1, N1, Ngsc);
   
   /* first find stellar halos */
Index: /trunk/Ohana/src/markrock/src/find_rocks.c
===================================================================
--- /trunk/Ohana/src/markrock/src/find_rocks.c	(revision 16039)
+++ /trunk/Ohana/src/markrock/src/find_rocks.c	(revision 16040)
@@ -1,14 +1,10 @@
 # include "markrock.h"
 
-Rocks *find_rocks (catalog, catstats, nrocks)
-Catalog catalog[];
-CatStats catstats[];
-int *nrocks;
-{
+Rocks *find_rocks (Catalog *catalog, CatStats *catstats, int *nrocks) {
 
   int i, j, k, n, m, N, M, first_j, NOBJECT, Nobject, Nrocks, NROCKS;
   double X, Y, RADIUS2;
-  float *X1, *Y1;
-  float dX, dY, dR;
+  double *X1, *Y1;
+  double dX, dY, dR;
   float dtime;
   int *N1, *N0;
@@ -29,6 +25,6 @@
 
   NOBJECT = 1000;
-  ALLOCATE (X1, float, NOBJECT);
-  ALLOCATE (Y1, float, NOBJECT);
+  ALLOCATE (X1, double, NOBJECT);
+  ALLOCATE (Y1, double, NOBJECT);
   ALLOCATE (T1, int,   NOBJECT);
   ALLOCATE (N1, int,   NOBJECT);
@@ -68,6 +64,6 @@
       if (Nobject == NOBJECT - 1) {
 	NOBJECT += 1000;
-	REALLOCATE (X1, float, NOBJECT);
-	REALLOCATE (Y1, float, NOBJECT);
+	REALLOCATE (X1, double, NOBJECT);
+	REALLOCATE (Y1, double, NOBJECT);
 	REALLOCATE (T1, int, NOBJECT);
 	REALLOCATE (N1, int,   NOBJECT);
@@ -75,6 +71,6 @@
     }
   }
-  REALLOCATE (X1, float, Nobject);
-  REALLOCATE (Y1, float, Nobject);
+  REALLOCATE (X1, double, Nobject);
+  REALLOCATE (Y1, double, Nobject);
   REALLOCATE (T1, int, Nobject);
   REALLOCATE (N1, int,   Nobject);
Index: /trunk/Ohana/src/markrock/src/gcatstats.c
===================================================================
--- /trunk/Ohana/src/markrock/src/gcatstats.c	(revision 16039)
+++ /trunk/Ohana/src/markrock/src/gcatstats.c	(revision 16040)
@@ -9,5 +9,5 @@
   double RaCenter, DecCenter;
   double MinRA, MaxRA, MinDEC, MaxDEC;
-  float *X1, *Y1;
+  double *X1, *Y1;
   int *N1;
   Coords tcoords;
@@ -29,6 +29,6 @@
 
   /** allocate local arrays **/
-  ALLOCATE (catstats[0].X, float, catalog[0].Naverage);
-  ALLOCATE (catstats[0].Y, float, catalog[0].Naverage);
+  ALLOCATE (catstats[0].X, double, catalog[0].Naverage);
+  ALLOCATE (catstats[0].Y, double, catalog[0].Naverage);
   ALLOCATE (catstats[0].N, int,   catalog[0].Naverage);
 
@@ -49,5 +49,5 @@
     catstats[0].N[i] = i;
   }
-  if (catalog[0].Naverage > 1) sort_lists (catstats[0].X, catstats[0].Y, catstats[0].N, catalog[0].Naverage);
+  if (catalog[0].Naverage > 1) sort_coords_index (catstats[0].X, catstats[0].Y, catstats[0].N, catalog[0].Naverage);
   
 }
Index: /trunk/Ohana/src/markrock/src/sorts.c
===================================================================
--- /trunk/Ohana/src/markrock/src/sorts.c	(revision 16039)
+++ /trunk/Ohana/src/markrock/src/sorts.c	(revision 16040)
@@ -1,231 +1,16 @@
+# include "markrock.h"
 
-sort_set (X, Y, T, S, N) 
-float *X, *Y;
-int *S, N;
-int *T;
-{
-  int l,j,ir,i;
-  unsigned int tT;
-  double tX, tY, tS;
-  
-  l = N >> 1;
-  ir = N - 1;
-  for (;;) {
-    if (l > 0) {
-      l--;
-      tX = X[l];
-      tY = Y[l];
-      tT = T[l];
-      tS = S[l];
-    }
-    else {
-      tX = X[ir];
-      X[ir] = X[0];
-      tY = Y[ir];
-      Y[ir] = Y[0];
-      tT = T[ir];
-      T[ir] = T[0];
-      tS = S[ir];
-      S[ir] = S[0];
-      if (--ir == 0) {
-	X[0] = tX;
-	Y[0] = tY;
-	T[0] = tT;
-	S[0] = tS;
-	return;
-      }
-    }
-    i = l;
-    j = (l << 1) + 1;
-    while (j <= ir) {
-      if (j < ir && X[j] < X[j+1]) j++;
-      if (tX < X[j]) {
-	X[i] = X[j];
-	Y[i] = Y[j];
-	T[i] = T[j];
-	S[i] = S[j];
-	j += (i=j) + 1;
-      }
-      else j = ir + 1;
-    }
-    X[i] = tX;
-    Y[i] = tY;
-    T[i] = tT;
-    S[i] = tS;
-  }
+void sort_set (double *X, double *Y, int *T, int *S, int N) {
+
+# define SWAPFUNC(A,B){ double dtmp; int itmp; \
+  dtmp = X[A]; X[A] = X[B]; X[B] = dtmp; \
+  dtmp = Y[A]; Y[A] = Y[B]; Y[B] = dtmp; \
+  itmp = T[A]; T[A] = T[B]; T[B] = itmp; \
+  itmp = S[A]; S[A] = S[B]; S[B] = itmp; \
 }
+# define COMPARE(A,B)(X[A] < X[B])
 
+  OHANA_SORT (N, COMPARE, SWAPFUNC);
 
-sort_lists (X, Y, S, N) 
-float *X, *Y;
-int *S, N;
-{
-  int l,j,ir,i;
-  double tX, tY, tS;
-  
-  l = N >> 1;
-  ir = N - 1;
-  for (;;) {
-    if (l > 0) {
-      l--;
-      tX = X[l];
-      tY = Y[l];
-      tS = S[l];
-    }
-    else {
-      tX = X[ir];
-      X[ir] = X[0];
-      tY = Y[ir];
-      Y[ir] = Y[0];
-      tS = S[ir];
-      S[ir] = S[0];
-      if (--ir == 0) {
-	X[0] = tX;
-	Y[0] = tY;
-	S[0] = tS;
-	return;
-      }
-    }
-    i = l;
-    j = (l << 1) + 1;
-    while (j <= ir) {
-      if (j < ir && X[j] < X[j+1]) j++;
-      if (tX < X[j]) {
-	X[i] = X[j];
-	Y[i] = Y[j];
-	S[i] = S[j];
-	j += (i=j) + 1;
-      }
-      else j = ir + 1;
-    }
-    X[i] = tX;
-    Y[i] = tY;
-    S[i] = tS;
-  }
-}
-
-sort_entries (X, Y, N) 
-     double *X, *Y;
-     int N;
-{
-  int l,j,ir,i;
-  double tX, tY;
-  
-  l = N >> 1;
-  ir = N - 1;
-  for (;;) {
-    if (l > 0) {
-      l--;
-      tX = X[l];
-      tY = Y[l];
-    }
-    else {
-      tX = X[ir];
-      X[ir] = X[0];
-      tY = Y[ir];
-      Y[ir] = Y[0];
-      if (--ir == 0) {
-	X[0] = tX;
-	Y[0] = tY;
-	return;
-      }
-    }
-    i = l;
-    j = (l << 1) + 1;
-    while (j <= ir) {
-      if (j < ir && X[j] < X[j+1]) j++;
-      if (tX < X[j]) {
-	X[i] = X[j];
-	Y[i] = Y[j];
-	j += (i=j) + 1;
-      }
-      else j = ir + 1;
-    }
-    X[i] = tX;
-    Y[i] = tY;
-  }
-}
-
-
-
-sort (X, N) 
-     double *X;
-     int N;
-{
-  int l,j,ir,i;
-  double tX;
-  
-  l = N >> 1;
-  ir = N - 1;
-  for (;;) {
-    if (l > 0) {
-      l--;
-      tX = X[l];
-    }
-    else {
-      tX = X[ir];
-      X[ir] = X[0];
-      if (--ir == 0) {
-	X[0] = tX;
-	return;
-      }
-    }
-    i = l;
-    j = (l << 1) + 1;
-    while (j <= ir) {
-      if (j < ir && X[j] < X[j+1]) j++;
-      if (tX < X[j]) {
-	X[i] = X[j];
-	j += (i=j) + 1;
-      }
-      else j = ir + 1;
-    }
-    X[i] = tX;
-  }
-}
-
-
-sort_seq (X, S, N) 
-     double *X;
-     int *S;
-     int N;
-{
-  int l,j,ir,i;
-  int tS;
-  double tX;
-  
-  l = N >> 1;
-  ir = N - 1;
-  for (;;) {
-    if (l > 0) {
-      l--;
-      tX = X[l];
-      tS = S[l];
-    }
-    else {
-      tX = X[ir];
-      X[ir] = X[0];
-      tS = S[ir];
-      S[ir] = S[0];
-      if (--ir == 0) {
-	X[0] = tX;
-	S[0] = tS;
-	return;
-      }
-    }
-    i = l;
-    j = (l << 1) + 1;
-    while (j <= ir) {
-      if (j < ir && X[j] < X[j+1]) j++;
-      if (tX < X[j]) {
-	X[i] = X[j];
-	S[i] = S[j];
-	j += (i=j) + 1;
-      }
-      else j = ir + 1;
-    }
-    X[i] = tX;
-    S[i] = tS;
-  }
-}
-
+# undef SWAPFUNC
+# undef COMPARE
Index: /trunk/Ohana/src/markstar/include/markstar.h
===================================================================
--- /trunk/Ohana/src/markstar/include/markstar.h	(revision 16039)
+++ /trunk/Ohana/src/markstar/include/markstar.h	(revision 16040)
@@ -38,5 +38,5 @@
 typedef struct {
   Coords coords;
-  float *X, *Y;
+  double *X, *Y;
   int *N;
   double RA[2], DEC[2];
Index: /trunk/Ohana/src/markstar/src/find_bright_stars.c
===================================================================
--- /trunk/Ohana/src/markstar/src/find_bright_stars.c	(revision 16039)
+++ /trunk/Ohana/src/markstar/src/find_bright_stars.c	(revision 16040)
@@ -1,8 +1,5 @@
 # include "markstar.h"
 
-find_bright_stars (catalog, catstats)
-Catalog catalog[];
-CatStats catstats[];
-{
+void find_bright_stars (Catalog *catalog, CatStats *catstats) {
 
   int i, j, n, m, first_j, Nave, Ngsc;
@@ -10,5 +7,5 @@
   double MinRA, MinDEC, MaxRA, MaxDEC, RaCenter, DecCenter;
   Coords *tcoords;
-  float *X1, *Y1, *X2, *Y2;
+  double *X1, *Y1, *X2, *Y2;
   int *N1, *N2;
   char *mark;
@@ -26,6 +23,6 @@
   Y2 = catstats[0].Y;
   N2 = catstats[0].N;
-  ALLOCATE (X1, float, Ngsc);
-  ALLOCATE (Y1, float, Ngsc);
+  ALLOCATE (X1, double, Ngsc);
+  ALLOCATE (Y1, double, Ngsc);
   ALLOCATE (N1, int, Ngsc);
   ALLOCATE (mark, char, Nave);
@@ -34,8 +31,8 @@
   tcoords = &catstats[0].coords;
   for (i = 0; i < Ngsc; i++) {
-    fRD_to_XY (&X1[i], &Y1[i], GSCdata.average[i].R, GSCdata.average[i].D, tcoords);
+    RD_to_XY (&X1[i], &Y1[i], GSCdata.average[i].R, GSCdata.average[i].D, tcoords);
     N1[i] = i;
   }
-  if (Ngsc > 1) sort_lists (X1, Y1, N1, Ngsc);
+  if (Ngsc > 1) sort_coords_index (X1, Y1, N1, Ngsc);
   
   /* first find stellar halos */
Index: /trunk/Ohana/src/markstar/src/find_matches.c
===================================================================
--- /trunk/Ohana/src/markstar/src/find_matches.c	(revision 16039)
+++ /trunk/Ohana/src/markstar/src/find_matches.c	(revision 16040)
@@ -1,14 +1,10 @@
 # include "markstar.h"
 
-find_matches (catalog, catstats, Ghostdata, Nimage)
-CatStats catstats[];
-Catalog catalog[], Ghostdata[];
-int Nimage;
-{
+int find_matches (Catalog *catalog, CatStats *catstats, Catalog *Ghostdata, int Nimage) {
 
   int i, j, k, n, m, N, M, first_j;
   double X, Y, RADIUS, RADIUS2;
-  float *X1, *Y1, *X2, *Y2;
-  float dX, dY, dR;
+  double *X1, *Y1, *X2, *Y2;
+  double dX, dY, dR;
   int *N1, *N2;
   int Nstar, Nghost, Ng;
@@ -27,6 +23,6 @@
   N2 = catstats[0].N;
 
-  ALLOCATE (X1, float, Nghost);
-  ALLOCATE (Y1, float, Nghost);
+  ALLOCATE (X1, double, Nghost);
+  ALLOCATE (Y1, double, Nghost);
   ALLOCATE (N1, int, Nghost);
 
@@ -34,8 +30,8 @@
   tcoords = &catstats[0].coords;
   for (i = 0; i < Nghost; i++) {
-    fRD_to_XY (&X1[i], &Y1[i], Ghostdata[0].average[i].R, Ghostdata[0].average[i].D, tcoords);
+    RD_to_XY (&X1[i], &Y1[i], Ghostdata[0].average[i].R, Ghostdata[0].average[i].D, tcoords);
     N1[i] = i;
   }
-  if (Nghost > 1) sort_lists (X1, Y1, N1, Nghost);
+  if (Nghost > 1) sort_coords_index (X1, Y1, N1, Nghost);
 
   /* choose a radius for matches */
Index: /trunk/Ohana/src/markstar/src/gcatstats.c
===================================================================
--- /trunk/Ohana/src/markstar/src/gcatstats.c	(revision 16039)
+++ /trunk/Ohana/src/markstar/src/gcatstats.c	(revision 16040)
@@ -1,13 +1,10 @@
 # include "markstar.h"
 
-gcatstats (catalog, catstats)
-Catalog catalog[];
-CatStats catstats[];
-{
+void gcatstats (Catalog *catalog, CatStats *catstats) {
 
   int i;
   double RaCenter, DecCenter;
   double MinRA, MaxRA, MinDEC, MaxDEC;
-  float *X1, *Y1;
+  double *X1, *Y1;
   int *N1;
   Coords tcoords;
@@ -42,6 +39,6 @@
 
   /** allocate local arrays **/
-  ALLOCATE (catstats[0].X, float, catalog[0].Naverage);
-  ALLOCATE (catstats[0].Y, float, catalog[0].Naverage);
+  ALLOCATE (catstats[0].X, double, catalog[0].Naverage);
+  ALLOCATE (catstats[0].Y, double, catalog[0].Naverage);
   ALLOCATE (catstats[0].N, int,   catalog[0].Naverage);
 
@@ -59,8 +56,8 @@
   Y1 = catstats[0].Y;
   for (i = 0; i < catalog[0].Naverage; i++, X1++, Y1++) {
-    fRD_to_XY (X1, Y1, catalog[0].average[i].R, catalog[0].average[i].D, &catstats[0].coords);
+    RD_to_XY (X1, Y1, catalog[0].average[i].R, catalog[0].average[i].D, &catstats[0].coords);
     catstats[0].N[i] = i;
   }
-  if (catalog[0].Naverage > 1) sort_lists (catstats[0].X, catstats[0].Y, catstats[0].N, catalog[0].Naverage);
+  if (catalog[0].Naverage > 1) sort_coords_index (catstats[0].X, catstats[0].Y, catstats[0].N, catalog[0].Naverage);
   
 }
Index: /trunk/Ohana/src/markstar/src/sorts.c
===================================================================
--- /trunk/Ohana/src/markstar/src/sorts.c	(revision 16039)
+++ /trunk/Ohana/src/markstar/src/sorts.c	(revision 16040)
@@ -1,175 +1,15 @@
+# include "markstar.h"
 
+void sort_seq (double *X, int *S, int N) {
 
-sort_lists (X, Y, S, N) 
-float *X, *Y;
-int *S, N;
-{
-  int l,j,ir,i;
-  double tX, tY, tS;
-  
-  l = N >> 1;
-  ir = N - 1;
-  for (;;) {
-    if (l > 0) {
-      l--;
-      tX = X[l];
-      tY = Y[l];
-      tS = S[l];
-    }
-    else {
-      tX = X[ir];
-      X[ir] = X[0];
-      tY = Y[ir];
-      Y[ir] = Y[0];
-      tS = S[ir];
-      S[ir] = S[0];
-      if (--ir == 0) {
-	X[0] = tX;
-	Y[0] = tY;
-	S[0] = tS;
-	return;
-      }
-    }
-    i = l;
-    j = (l << 1) + 1;
-    while (j <= ir) {
-      if (j < ir && X[j] < X[j+1]) j++;
-      if (tX < X[j]) {
-	X[i] = X[j];
-	Y[i] = Y[j];
-	S[i] = S[j];
-	j += (i=j) + 1;
-      }
-      else j = ir + 1;
-    }
-    X[i] = tX;
-    Y[i] = tY;
-    S[i] = tS;
-  }
+# define SWAPFUNC(A,B){ \
+  double dtmp; dtmp = X[A]; X[A] = X[B]; X[B] = dtmp; }
+  int    itmp; itmp = S[A]; S[A] = S[B]; S[B] = itmp; }
+# define COMPARE(A,B)(X[A] < X[B])
+
+  OHANA_SORT (N, COMPARE, SWAPFUNC);
+
+# undef SWAPFUNC
+# undef COMPARE
+
 }
-
-sort_entries (X, Y, N) 
-     double *X, *Y;
-     int N;
-{
-  int l,j,ir,i;
-  double tX, tY;
-  
-  l = N >> 1;
-  ir = N - 1;
-  for (;;) {
-    if (l > 0) {
-      l--;
-      tX = X[l];
-      tY = Y[l];
-    }
-    else {
-      tX = X[ir];
-      X[ir] = X[0];
-      tY = Y[ir];
-      Y[ir] = Y[0];
-      if (--ir == 0) {
-	X[0] = tX;
-	Y[0] = tY;
-	return;
-      }
-    }
-    i = l;
-    j = (l << 1) + 1;
-    while (j <= ir) {
-      if (j < ir && X[j] < X[j+1]) j++;
-      if (tX < X[j]) {
-	X[i] = X[j];
-	Y[i] = Y[j];
-	j += (i=j) + 1;
-      }
-      else j = ir + 1;
-    }
-    X[i] = tX;
-    Y[i] = tY;
-  }
-}
-
-
-
-sort (X, N) 
-     double *X;
-     int N;
-{
-  int l,j,ir,i;
-  double tX;
-  
-  l = N >> 1;
-  ir = N - 1;
-  for (;;) {
-    if (l > 0) {
-      l--;
-      tX = X[l];
-    }
-    else {
-      tX = X[ir];
-      X[ir] = X[0];
-      if (--ir == 0) {
-	X[0] = tX;
-	return;
-      }
-    }
-    i = l;
-    j = (l << 1) + 1;
-    while (j <= ir) {
-      if (j < ir && X[j] < X[j+1]) j++;
-      if (tX < X[j]) {
-	X[i] = X[j];
-	j += (i=j) + 1;
-      }
-      else j = ir + 1;
-    }
-    X[i] = tX;
-  }
-}
-
-
-sort_seq (X, S, N) 
-     double *X;
-     int *S;
-     int N;
-{
-  int l,j,ir,i;
-  int tS;
-  double tX;
-  
-  l = N >> 1;
-  ir = N - 1;
-  for (;;) {
-    if (l > 0) {
-      l--;
-      tX = X[l];
-      tS = S[l];
-    }
-    else {
-      tX = X[ir];
-      X[ir] = X[0];
-      tS = S[ir];
-      S[ir] = S[0];
-      if (--ir == 0) {
-	X[0] = tX;
-	S[0] = tS;
-	return;
-      }
-    }
-    i = l;
-    j = (l << 1) + 1;
-    while (j <= ir) {
-      if (j < ir && X[j] < X[j+1]) j++;
-      if (tX < X[j]) {
-	X[i] = X[j];
-	S[i] = S[j];
-	j += (i=j) + 1;
-      }
-      else j = ir + 1;
-    }
-    X[i] = tX;
-    S[i] = tS;
-  }
-}
-
Index: /trunk/Ohana/src/misc/src/cubefilter.c
===================================================================
--- /trunk/Ohana/src/misc/src/cubefilter.c	(revision 16039)
+++ /trunk/Ohana/src/misc/src/cubefilter.c	(revision 16040)
@@ -109,37 +109,2 @@
 
 }
-
-fsort (value, N) 
-float value[];
-int N;
-{
-  int l,j,ir,i;
-  float temp;
-  
-  l = N >> 1;
-  ir = N - 1;
-  for (;;) {
-    if (l > 0) {
-      temp = value[--l];
-    }
-    else {
-      temp = value[ir];
-      value[ir] = value[0];
-      if (--ir == 0) {
-	value[0] = temp;
-	return;
-      }
-    }
-    i = l;
-    j = (l << 1) + 1;
-    while (j <= ir) {
-      if (j < ir && value[j] < value[j+1]) ++j;
-      if (temp < value[j]) {
-	value[i]=value[j];
-	j += (i=j) + 1;
-      }
-      else j = ir + 1;
-    }
-    value[i] = temp;
-  }
-}
Index: /trunk/Ohana/src/mosastro/Makefile
===================================================================
--- /trunk/Ohana/src/mosastro/Makefile	(revision 16039)
+++ /trunk/Ohana/src/mosastro/Makefile	(revision 16040)
@@ -54,5 +54,4 @@
 $(SRC)/rfits.$(ARCH).o \
 $(SRC)/rtext.$(ARCH).o \
-$(SRC)/sort.$(ARCH).o \
 $(SRC)/testcoords.$(ARCH).o \
 $(SRC)/transforms.$(ARCH).o \
Index: /trunk/Ohana/src/mosastro/src/match.c
===================================================================
--- /trunk/Ohana/src/mosastro/src/match.c	(revision 16039)
+++ /trunk/Ohana/src/mosastro/src/match.c	(revision 16040)
@@ -23,6 +23,5 @@
     Q[i] = refcat[i].Q;
   }
-  sortthree (P, Q, U, Nrefcat);
-
+  sort_coords_index (P, Q, U, Nrefcat);
 
   for (i = 0; i < Nchip; i++) {
@@ -42,5 +41,5 @@
       q[j] = chip[i].stars[j].Q;
     }
-    sortthree (p, q, u, chip[i].Nstars);
+    sort_coords_index (p, q, u, chip[i].Nstars);
 
     /* find star matches in the tangent plane coord system */
Index: unk/Ohana/src/mosastro/src/sort.c
===================================================================
--- /trunk/Ohana/src/mosastro/src/sort.c	(revision 16039)
+++ 	(revision )
@@ -1,50 +1,0 @@
-# include "mosastro.h"
-
-int sortthree (double *X, double *Y, int *Z, int N) {
-
-  int l,j,ir,i;
-  double tX, tY;
-  int tZ;
-  
-  if (N < 2) return (1);
-  l = N >> 1;
-  ir = N - 1;
-  for (;;) {
-    if (l > 0) {
-      l--;
-      tX = X[l];
-      tY = Y[l];
-      tZ = Z[l];
-    }
-    else {
-      tX = X[ir];
-      X[ir] = X[0];
-      tY = Y[ir];
-      Y[ir] = Y[0];
-      tZ = Z[ir];
-      Z[ir] = Z[0];
-      if (--ir == 0) {
-        X[0] = tX;
-        Y[0] = tY;
-        Z[0] = tZ;
-        return (1);
-      }
-    }
-    i = l;
-    j = (l << 1) + 1;
-    while (j <= ir) {
-      if (j < ir && X[j] < X[j+1]) j++;
-      if (tX < X[j]) {
-        X[i] = X[j];
-        Y[i] = Y[j];
-        Z[i] = Z[j];
-        j += (i=j) + 1;
-      }
-      else j = ir + 1;
-    }
-    X[i] = tX;
-    Y[i] = tY;
-    Z[i] = tZ;
-  }
-  return (1);
-}
Index: /trunk/Ohana/src/opihi/dvo/dvomisc.c
===================================================================
--- /trunk/Ohana/src/opihi/dvo/dvomisc.c	(revision 16039)
+++ /trunk/Ohana/src/opihi/dvo/dvomisc.c	(revision 16040)
@@ -33,110 +33,2 @@
 
 }
-
-/* values are ave[i].R, ave[i].D, ave[i].M */
-void sortave (Average *ave, int N) {
-
-  int l,j,ir,i;
-  Average tmp;
-
-  if (N < 2) return;
-  l = N >> 1;
-  ir = N - 1;
-  for (;;) {
-    if (l > 0) {
-      l--;
-      tmp = ave[l];
-    }
-    else {
-      tmp = ave[ir];
-      ave[ir] = ave[0];
-      if (--ir == 0) {
-	ave[0] = tmp;
-	return;
-      }
-    }
-    i = l;
-    j = (l << 1) + 1;
-    while (j <= ir) {
-      if (j < ir && ave[j].R < ave[j+1].R) j++;
-      if (tmp.R < ave[j].R) {
-	ave[i] = ave[j];
-	j += (i=j) + 1;
-      }
-      else j = ir + 1;
-    }
-    ave[i] = tmp;
-  }
-}
-
-/** this does not seem to be used at the moment */
-/* values are ave[i].R, ave[i].D, ave[i].M */
-void sort_images (Image *image, int N) {
-
-  int l,j,ir,i;
-  Image tmp;
-
-  if (N < 2) return;
-  l = N >> 1;
-  ir = N - 1;
-  for (;;) {
-    if (l > 0) {
-      l--;
-      tmp = image[l];
-    }
-    else {
-      tmp = image[ir];
-      image[ir] = image[0];
-      if (--ir == 0) {
-	image[0] = tmp;
-	return;
-      }
-    }
-    i = l;
-    j = (l << 1) + 1;
-    while (j <= ir) {
-      if (j < ir && image[j].tzero < image[j+1].tzero) j++;
-      if (tmp.tzero < image[j].tzero) {
-	image[i] = image[j];
-	j += (i=j) + 1;
-      }
-      else j = ir + 1;
-    }
-    image[i] = tmp;
-  }
-}
-
-/* sort subset by image[subset[i]].tzero */
-void sort_image_subset (Image *image, int *subset, int N) {
-
-  int l, j, ir, i, tmp;
-
-  if (N < 2) return;
-  l = N >> 1;
-  ir = N - 1;
-  for (;;) {
-    if (l > 0) {
-      l--;
-      tmp = subset[l];
-    }
-    else {
-      tmp = subset[ir];
-      subset[ir] = subset[0];
-      if (--ir == 0) {
-	subset[0] = tmp;
-	return;
-      }
-    }
-    i = l;
-    j = (l << 1) + 1;
-    while (j <= ir) {
-      if (j < ir && image[subset[j]].tzero < image[subset[j+1]].tzero) j++;
-      if (image[tmp].tzero < image[subset[j]].tzero) {
-	subset[i] = subset[j];
-	j += (i=j) + 1;
-      }
-      else j = ir + 1;
-    }
-    subset[i] = tmp;
-  }
-}
Index: /trunk/Ohana/src/opihi/dvo/fitsed.c
===================================================================
--- /trunk/Ohana/src/opihi/dvo/fitsed.c	(revision 16039)
+++ /trunk/Ohana/src/opihi/dvo/fitsed.c	(revision 16040)
@@ -436,6 +436,6 @@
 SEDtableRow **sort_SEDtable (SEDtableRow *raw, int N) {
 
-  int l,j,ir,i;
-  SEDtableRow **value, *temp;
+  int i;
+  SEDtableRow **value;
   
   if (N <= 0) return (NULL);
@@ -445,33 +445,12 @@
     value[i] = &raw[i];
   }
-  if (N < 2) return (value);
-
-  l = N >> 1;
-  ir = N - 1;
-  for (;;) {
-    if (l > 0) {
-      l--;
-      temp = value[l];
-    }
-    else {
-      temp = value[ir];
-      value[ir] = value[0];
-      if (--ir == 0) {
-	value[0] = temp;
-	return (value);
-      }
-    }
-    i = l;
-    j = (l << 1) + 1;
-    while (j <= ir) {
-      if (j < ir && value[j][0].color < value[j+1][0].color) ++j;
-      if (temp[0].color < value[j][0].color) {
-	value[i] = value[j];
-	j += (i=j) + 1;
-      }
-      else j = ir + 1;
-    }
-    value[i] = temp;
-  }
+
+# define SWAPFUNC(A,B){ SEDtableRow *temp = value[A]; value[A] = value[B]; value[B] = temp; }
+# define COMPARE(A,B)(value[A][0].color < value[B][0].color)
+
+  OHANA_SORT (N, COMPARE, SWAPFUNC);
+
+# undef SWAPFUNC
+# undef COMPARE
 
   return (value);
Index: /trunk/Ohana/src/opihi/dvo/gstar.c
===================================================================
--- /trunk/Ohana/src/opihi/dvo/gstar.c	(revision 16039)
+++ /trunk/Ohana/src/opihi/dvo/gstar.c	(revision 16040)
@@ -10,5 +10,5 @@
   double Ra, Dec, Radius, Radius2, r, dec0, dec1;
   double Mcat, Mrel;
-  float *RA, *DEC;
+  double *RA, *DEC;
   int i, j, k, m, N, *N1, Nsecfilt, NPTS, QUIET, FULL_OUTPUT;
   int Nstars, found, GetMeasures, Nlo, Nhi;
@@ -93,6 +93,6 @@
 
   Nstars = catalog.Naverage;
-  ALLOCATE (RA, float, Nstars);
-  ALLOCATE (DEC, float, Nstars);
+  ALLOCATE (RA, double, Nstars);
+  ALLOCATE (DEC, double, Nstars);
   ALLOCATE (N1, int, Nstars);
 
@@ -105,5 +105,5 @@
   }
   /* sort list by DEC */
-  if (Nstars > 1) sort_lists (DEC, RA, N1, Nstars);
+  if (Nstars > 1) sort_coords_index (DEC, RA, N1, Nstars);
   /* at this point, RA, DEC, and N1 are sorted by DEC.  
      catalog.average[N1[i]].R = RA[i] */
@@ -308,5 +308,5 @@
   }
 
-  isort_pair (codeNumber, sequence, Nsecfilt);
+  isortpair (codeNumber, sequence, Nsecfilt);
   free (codeNumber);
 }
Index: /trunk/Ohana/src/opihi/dvo/lcurve.c
===================================================================
--- /trunk/Ohana/src/opihi/dvo/lcurve.c	(revision 16039)
+++ /trunk/Ohana/src/opihi/dvo/lcurve.c	(revision 16040)
@@ -5,5 +5,5 @@
   char string[128], *p;
   double Ra, Dec, Radius, Radius2, r;
-  float *RA, *DEC;
+  double *RA, *DEC;
   int kapa, TimeFormat;
   int Nstars, found, AutoLimits, ErrorBars, GalMag, AbsPhot, SaveVectors;
@@ -86,6 +86,6 @@
 
   Nstars = catalog.Naverage;
-  ALLOCATE (RA, float, Nstars);
-  ALLOCATE (DEC, float, Nstars);
+  ALLOCATE (RA, double, Nstars);
+  ALLOCATE (DEC, double, Nstars);
   ALLOCATE (N1, int, Nstars);
 
@@ -98,5 +98,5 @@
   }
   /* sort list by DEC */
-  if (Nstars > 1) sort_lists (DEC, RA, N1, Nstars);
+  if (Nstars > 1) sort_coords_index (DEC, RA, N1, Nstars);
   /* at this point, RA, DEC, and N1 are sorted by DEC.  
      catalog.average[N1[i]].R = RA[i] */
@@ -142,5 +142,5 @@
   
   if (ErrorBars)
-    sortthree (Xvec.elements, Yvec.elements, dYvec.elements, N);
+    fsortthree (Xvec.elements, Yvec.elements, dYvec.elements, N);
   else
     fsortpair (Xvec.elements, Yvec.elements, N);
Index: /trunk/Ohana/src/opihi/dvo/lightcurve.c
===================================================================
--- /trunk/Ohana/src/opihi/dvo/lightcurve.c	(revision 16039)
+++ /trunk/Ohana/src/opihi/dvo/lightcurve.c	(revision 16040)
@@ -4,5 +4,5 @@
   
   double Ra, Dec, Radius, Radius2, r;
-  float *RA, *DEC;
+  double *RA, *DEC;
   int Nstars, found, PhotCodeSelect;
   int i, j, k, m, N, NPTS, Nsecfilt, RELPHOT, *N1, TimeFormat;
@@ -69,6 +69,6 @@
 
   Nstars = catalog.Naverage;
-  ALLOCATE (RA, float, Nstars);
-  ALLOCATE (DEC, float, Nstars);
+  ALLOCATE (RA, double, Nstars);
+  ALLOCATE (DEC, double, Nstars);
   ALLOCATE (N1, int, Nstars);
 
@@ -81,5 +81,5 @@
   }
   /* sort list by DEC */
-  if (Nstars > 1) sort_lists (DEC, RA, N1, Nstars);
+  if (Nstars > 1) sort_coords_index (DEC, RA, N1, Nstars);
   /* at this point, RA, DEC, and N1 are sorted by DEC.  
      catalog.average[N1[i]].R = RA[i] */
@@ -134,5 +134,5 @@
     }
   }
-  sortthree (tvec[0].elements, mvec[0].elements, dmvec[0].elements, N);
+  fsortthree (tvec[0].elements, mvec[0].elements, dmvec[0].elements, N);
   tvec[0].Nelements = mvec[0].Nelements = dmvec[0].Nelements = N;
 
Index: /trunk/Ohana/src/opihi/dvo/subpix.c
===================================================================
--- /trunk/Ohana/src/opihi/dvo/subpix.c	(revision 16039)
+++ /trunk/Ohana/src/opihi/dvo/subpix.c	(revision 16040)
@@ -60,5 +60,5 @@
     index[i] = i;
   }
-  if (Nstars > 1) dsort_lists (DEC, RA, index, Nstars);
+  if (Nstars > 1) sort_coords_index (DEC, RA, index, Nstars);
 
   /* bracket the DEC range of interest */
Index: /trunk/Ohana/src/opihi/lib.data/Makefile
===================================================================
--- /trunk/Ohana/src/opihi/lib.data/Makefile	(revision 16039)
+++ /trunk/Ohana/src/opihi/lib.data/Makefile	(revision 16040)
@@ -18,5 +18,4 @@
 $(SDIR)/book.$(ARCH).o                  \
 $(SDIR)/page.$(ARCH).o                  \
-$(SDIR)/sort.$(ARCH).o                  \
 $(SDIR)/fft.$(ARCH).o			\
 $(SDIR)/svdcmp.$(ARCH).o		\
Index: unk/Ohana/src/opihi/lib.data/sort.c
===================================================================
--- /trunk/Ohana/src/opihi/lib.data/sort.c	(revision 16039)
+++ 	(revision )
@@ -1,334 +1,0 @@
-# include "data.h"
-
-void sort (double *value, int N) {
-
-  int l,j,ir,i;
-  double temp;
-  
-  if (N < 2) return;
-  l = N >> 1;
-  ir = N - 1;
-  for (;;) {
-    if (l > 0) {
-      temp = value[--l];
-    }
-    else {
-      temp = value[ir];
-      value[ir] = value[0];
-      if (--ir == 0) {
-	value[0] = temp;
-	return;
-      }
-    }
-    i = l;
-    j = (l << 1) + 1;
-    while (j <= ir) {
-      if (j < ir && value[j] < value[j+1]) ++j;
-      if (temp < value[j]) {
-	value[i]=value[j];
-	j += (i=j) + 1;
-      }
-      else j = ir + 1;
-    }
-    value[i] = temp;
-  }
-}
-
-void fsort (float *value, int N) {
-
-  int l,j,ir,i;
-  float temp;
-  
-  if (N < 2) return;
-  l = N >> 1;
-  ir = N - 1;
-  for (;;) {
-    if (l > 0) {
-      temp = value[--l];
-    }
-    else {
-      temp = value[ir];
-      value[ir] = value[0];
-      if (--ir == 0) {
-	value[0] = temp;
-	return;
-      }
-    }
-    i = l;
-    j = (l << 1) + 1;
-    while (j <= ir) {
-      if (j < ir && value[j] < value[j+1]) ++j;
-      if (temp < value[j]) {
-	value[i]=value[j];
-	j += (i=j) + 1;
-      }
-      else j = ir + 1;
-    }
-    value[i] = temp;
-  }
-}
-
-void sortpair (double *value1, double *value2, int N) {
-
-  int l,j,ir,i;
-  double temp1, temp2;
-  
-  if (N < 2) return;
-  l = N >> 1;
-  ir = N - 1;
-  for (;;) {
-    if (l > 0) {
-      l--;
-      temp1 = value1[l];
-      temp2 = value2[l];
-    }
-    else {
-      temp1 = value1[ir];
-      temp2 = value2[ir];
-      value1[ir] = value1[0];
-      value2[ir] = value2[0];
-      if (--ir == 0) {
-	value1[0] = temp1;
-	value2[0] = temp2;
-	return;
-      }
-    }
-    i = l;
-    j = (l << 1) + 1;
-    while (j <= ir) {
-      if (j < ir && value1[j] < value1[j+1]) ++j;
-      if (temp1 < value1[j]) {
-	value1[i]=value1[j];
-	value2[i]=value2[j];
-	j += (i=j) + 1;
-      }
-      else j = ir + 1;
-    }
-    value1[i] = temp1;
-    value2[i] = temp2;
-  }
-}
-
-void fsortpair (float *X, float *Y, int N) {
-
-  int l,j,ir,i;
-  float tX, tY;
-  
-  if (N < 2) return;
-  l = N >> 1;
-  ir = N - 1;
-  for (;;) {
-    if (l > 0) {
-      l--;
-      tX = X[l];
-      tY = Y[l];
-    }
-    else {
-      tX = X[ir];
-      X[ir] = X[0];
-      tY = Y[ir];
-      Y[ir] = Y[0];
-      if (--ir == 0) {
-	X[0] = tX;
-	Y[0] = tY;
-	return;
-      }
-    }
-    i = l;
-    j = (l << 1) + 1;
-    while (j <= ir) {
-      if (j < ir && X[j] < X[j+1]) j++;
-      if (tX < X[j]) {
-	X[i] = X[j];
-	Y[i] = Y[j];
-	j += (i=j) + 1;
-      }
-      else j = ir + 1;
-    }
-    X[i] = tX;
-    Y[i] = tY;
-  }
-}
-
-void sortthree (float *X, float *Y, float *Z, int N) {
-
-  int l,j,ir,i;
-  float tX, tY, tZ;
-  
-  if (N < 2) return;
-  l = N >> 1;
-  ir = N - 1;
-  for (;;) {
-    if (l > 0) {
-      l--;
-      tX = X[l];
-      tY = Y[l];
-      tZ = Z[l];
-    }
-    else {
-      tX = X[ir];
-      X[ir] = X[0];
-      tY = Y[ir];
-      Y[ir] = Y[0];
-      tZ = Z[ir];
-      Z[ir] = Z[0];
-      if (--ir == 0) {
-	X[0] = tX;
-	Y[0] = tY;
-	Z[0] = tZ;
-	return;
-      }
-    }
-    i = l;
-    j = (l << 1) + 1;
-    while (j <= ir) {
-      if (j < ir && X[j] < X[j+1]) j++;
-      if (tX < X[j]) {
-	X[i] = X[j];
-	Y[i] = Y[j];
-	Z[i] = Z[j];
-	j += (i=j) + 1;
-      }
-      else j = ir + 1;
-    }
-    X[i] = tX;
-    Y[i] = tY;
-    Z[i] = tZ;
-  }
-}
-
-void sort_lists (float *X, float *Y, int *S, int N) {
-
-  int l,j,ir,i;
-  double tX, tY, tS;
-  
-  if (N < 2) return;
-  l = N >> 1;
-  ir = N - 1;
-  for (;;) {
-    if (l > 0) {
-      l--;
-      tX = X[l];
-      tY = Y[l];
-      tS = S[l];
-    }
-    else {
-      tX = X[ir];
-      X[ir] = X[0];
-      tY = Y[ir];
-      Y[ir] = Y[0];
-      tS = S[ir];
-      S[ir] = S[0];
-      if (--ir == 0) {
-	X[0] = tX;
-	Y[0] = tY;
-	S[0] = tS;
-	return;
-      }
-    }
-    i = l;
-    j = (l << 1) + 1;
-    while (j <= ir) {
-      if (j < ir && X[j] < X[j+1]) j++;
-      if (tX < X[j]) {
-	X[i] = X[j];
-	Y[i] = Y[j];
-	S[i] = S[j];
-	j += (i=j) + 1;
-      }
-      else j = ir + 1;
-    }
-    X[i] = tX;
-    Y[i] = tY;
-    S[i] = tS;
-  }
-}
-
-void dsort_lists (double *X, double *Y, int *S, int N) {
-
-  int l,j,ir,i;
-  double tX, tY, tS;
-  
-  if (N < 2) return;
-  l = N >> 1;
-  ir = N - 1;
-  for (;;) {
-    if (l > 0) {
-      l--;
-      tX = X[l];
-      tY = Y[l];
-      tS = S[l];
-    }
-    else {
-      tX = X[ir];
-      X[ir] = X[0];
-      tY = Y[ir];
-      Y[ir] = Y[0];
-      tS = S[ir];
-      S[ir] = S[0];
-      if (--ir == 0) {
-	X[0] = tX;
-	Y[0] = tY;
-	S[0] = tS;
-	return;
-      }
-    }
-    i = l;
-    j = (l << 1) + 1;
-    while (j <= ir) {
-      if (j < ir && X[j] < X[j+1]) j++;
-      if (tX < X[j]) {
-	X[i] = X[j];
-	Y[i] = Y[j];
-	S[i] = S[j];
-	j += (i=j) + 1;
-      }
-      else j = ir + 1;
-    }
-    X[i] = tX;
-    Y[i] = tY;
-    S[i] = tS;
-  }
-}
-
-// sort two int vectors by first vector
-void isort_pair (int *X, int *Y, int N) {
-
-  int l,j,ir,i;
-  int tX, tY;
-  
-  if (N < 2) return;
-  l = N >> 1;
-  ir = N - 1;
-  for (;;) {
-    if (l > 0) {
-      l--;
-      tX = X[l];
-      tY = Y[l];
-    }
-    else {
-      tX = X[ir];
-      X[ir] = X[0];
-      tY = Y[ir];
-      Y[ir] = Y[0];
-      if (--ir == 0) {
-	X[0] = tX;
-	Y[0] = tY;
-	return;
-      }
-    }
-    i = l;
-    j = (l << 1) + 1;
-    while (j <= ir) {
-      if (j < ir && X[j] < X[j+1]) j++;
-      if (tX < X[j]) {
-	X[i] = X[j];
-	Y[i] = Y[j];
-	j += (i=j) + 1;
-      }
-      else j = ir + 1;
-    }
-    X[i] = tX;
-    Y[i] = tY;
-  }
-}
Index: /trunk/Ohana/src/opihi/lib.data/starfuncs.c
===================================================================
--- /trunk/Ohana/src/opihi/lib.data/starfuncs.c	(revision 16039)
+++ /trunk/Ohana/src/opihi/lib.data/starfuncs.c	(revision 16040)
@@ -36,5 +36,5 @@
     }
   }
-  sort (ring, Nring);
+  dsort (ring, Nring);
   for (Npts = sky = dsky2 = 0, i = 0.25*Nring; i < 0.75*Nring; i++, Npts += 1.0) {
     sky += ring[i];
@@ -150,5 +150,5 @@
     }
   }
-  sort (sky, Nsky);
+  dsort (sky, Nsky);
   for (Npts = fsky = 0, i = 0.25*Nsky; i < 0.75*Nsky; i++, Npts += 1.0) {
     fsky += sky[i];
@@ -199,5 +199,5 @@
     }
   }
-  sort (sky, Nsky);
+  dsort (sky, Nsky);
   for (Npts = fsky = 0, i = 0.25*Nsky; i < 0.75*Nsky; i++, Npts += 1.0) {
     fsky += sky[i];
Index: /trunk/Ohana/src/opihi/lib.shell/CommandOps.c
===================================================================
--- /trunk/Ohana/src/opihi/lib.shell/CommandOps.c	(revision 16039)
+++ /trunk/Ohana/src/opihi/lib.shell/CommandOps.c	(revision 16040)
@@ -130,4 +130,20 @@
 }
 
+void sort_commands (int *seq) {
+
+  int i;
+
+  for (i = 0; i < Ncommands; i++) seq[i] = i;
+
+# define SWAPFUNC(A,B){ int tmp = seq[A]; seq[A] = seq[B]; seq[B] = tmp; }
+# define COMPARE(A,B)(strcmp (commands[seq[A]].name, commands[seq[B]].name) < 0)
+
+  OHANA_SORT (Ncommands, COMPARE, SWAPFUNC);
+
+# undef SWAPFUNC
+# undef COMPARE
+  
+}
+
 void print_commands (FILE *f) {
 
@@ -143,39 +159,2 @@
   return;
 }
-
-void sort_commands (int *seq) {
-
-  int l,j,ir,i, N;
-  int temp;
-  
-  N = Ncommands;
-  for (i = 0; i < N; i++) 
-    seq[i] = i;
-  
-  l = N >> 1;
-  ir = N - 1;
-  for (;;) {
-    if (l > 0) {
-      temp = seq[--l];
-    }
-    else {
-      temp = seq[ir];
-      seq[ir] = seq[0];
-      if (--ir == 0) {
-        seq[0] = temp;
-        return;
-      }
-    }
-    i = l;
-    j = (l << 1) + 1;
-    while (j <= ir) {
-      if (j < ir && (strcmp (commands[seq[j]].name, commands[seq[j+1]].name) < 0)) ++j;
-      if (strcmp (commands[temp].name, commands[seq[j]].name) < 0) {
-        seq[i]=seq[j];
-        j += (i=j) + 1;
-      }
-      else j = ir + 1;
-    }
-    seq[i] = temp;
-  }
-}
Index: /trunk/Ohana/src/photdbc/src/join_stars.c
===================================================================
--- /trunk/Ohana/src/photdbc/src/join_stars.c	(revision 16039)
+++ /trunk/Ohana/src/photdbc/src/join_stars.c	(revision 16040)
@@ -6,5 +6,5 @@
   int Naves, Nmeas, Ncurr;
   int Naverage, Nmeasure, *found, *index;
-  float *X, *Y, dX, dY, dR, RADIUS2;
+  double *X, *Y, dX, dY, dR, RADIUS2;
   double Sr, Sd, Rmid, Dmid;
 
@@ -45,12 +45,12 @@
   
   /* project & sort coordinates in local linear frame */
-  ALLOCATE (X, float, Naverage);
-  ALLOCATE (Y, float, Naverage);
+  ALLOCATE (X, double, Naverage);
+  ALLOCATE (Y, double, Naverage);
   ALLOCATE (index, int, Naverage);
   for (i = 0; i < Naverage; i++) {
     index[i] = i;
-    fRD_to_XY (&X[i], &Y[i], average[i].R, average[i].D, &tcoords);
+    RD_to_XY (&X[i], &Y[i], average[i].R, average[i].D, &tcoords);
   }
-  sort_lists (X, Y, index, Naverage);
+  sort_coords_index (X, Y, index, Naverage);
   
   /* flags to mark if a star has been handled */
@@ -142,5 +142,5 @@
 
 	/* update current reference star position */
-	fRD_to_XY (&X[i], &Y[i], Sr, Sd, &tcoords);
+	RD_to_XY (&X[i], &Y[i], Sr, Sd, &tcoords);
       }
     }
Index: /trunk/Ohana/src/photdbc/src/liststats.c
===================================================================
--- /trunk/Ohana/src/photdbc/src/liststats.c	(revision 16039)
+++ /trunk/Ohana/src/photdbc/src/liststats.c	(revision 16040)
@@ -32,5 +32,5 @@
   if (N < 2) return (FALSE);
 
-  sortB (value, dvalue, N);
+  dsortpair (value, dvalue, N);
   stats[0].median = value[(int)(0.5*N)];
   stats[0].min    = value[0];
@@ -68,5 +68,5 @@
       chi[i] = (value[i] - stats[0].median) / dvalue[i];
     }
-    sortD (chi, value, dvalue, N);
+    dsortthree (chi, value, dvalue, N);
     free (chi);
   }
Index: /trunk/Ohana/src/photdbc/src/misc.c
===================================================================
--- /trunk/Ohana/src/photdbc/src/misc.c	(revision 16039)
+++ /trunk/Ohana/src/photdbc/src/misc.c	(revision 16040)
@@ -1,273 +1,16 @@
-
-void sortA (double *X, int N) {
-
-  int l,j,ir,i;
-  double tX;
-  
-  if (N < 2) return;
-
-  l = N >> 1;
-  ir = N - 1;
-  for (;;) {
-    if (l > 0) {
-      l--;
-      tX = X[l];
-    }
-    else {
-      tX = X[ir];
-      X[ir] = X[0];
-      if (--ir == 0) {
-	X[0] = tX;
-	return;
-      }
-    }
-    i = l;
-    j = (l << 1) + 1;
-    while (j <= ir) {
-      if (j < ir && X[j] < X[j+1]) j++;
-      if (tX < X[j]) {
-	X[i] = X[j];
-	j += (i=j) + 1;
-      }
-      else j = ir + 1;
-    }
-    X[i] = tX;
-  }
-}
-
-void sortB (double *X, double *Y, int N) {
-
-  int l,j,ir,i;
-  double tX, tY;
-  
-  if (N < 2) return;
-
-  l = N >> 1;
-  ir = N - 1;
-  for (;;) {
-    if (l > 0) {
-      l--;
-      tX = X[l];
-      tY = Y[l];
-    }
-    else {
-      tX = X[ir];
-      X[ir] = X[0];
-      tY = Y[ir];
-      Y[ir] = Y[0];
-      if (--ir == 0) {
-	X[0] = tX;
-	Y[0] = tY;
-	return;
-      }
-    }
-    i = l;
-    j = (l << 1) + 1;
-    while (j <= ir) {
-      if (j < ir && X[j] < X[j+1]) j++;
-      if (tX < X[j]) {
-	X[i] = X[j];
-	Y[i] = Y[j];
-	j += (i=j) + 1;
-      }
-      else j = ir + 1;
-    }
-    X[i] = tX;
-    Y[i] = tY;
-  }
-}
-
-void sortC (double *X, double *Y, double *F1, double *F2, int N) {
-
-  int l,j,ir,i;
-  double tX, tY;
-  double t1, t2;
-  
-  if (N < 2) return;
-
-  l = N >> 1;
-  ir = N - 1;
-  for (;;) {
-    if (l > 0) {
-      l--;
-      tX = X[l];
-      tY = Y[l];
-      t1 = F1[l];
-      t2 = F2[l];
-    }
-    else {
-      tX = X[ir];
-      X[ir] = X[0];
-      tY = Y[ir];
-      Y[ir] = Y[0];
-      t1 = F1[ir];
-      F1[ir] = F1[0];
-      t2 = F2[ir];
-      F2[ir] = F2[0];
-      if (--ir == 0) {
-	X[0] = tX;
-	Y[0] = tY;
-	F1[0] = t1;
-	F2[0] = t2;
-	return;
-      }
-    }
-    i = l;
-    j = (l << 1) + 1;
-    while (j <= ir) {
-      if (j < ir && X[j] < X[j+1]) j++;
-      if (tX < X[j]) {
-	X[i] = X[j];
-	Y[i] = Y[j];
-	F1[i] = F1[j];
-	F2[i] = F2[j];
-	j += (i=j) + 1;
-      }
-      else j = ir + 1;
-    }
-    X[i] = tX;
-    Y[i] = tY;
-    F1[i] = t1;
-    F2[i] = t2;
-  }
-}
-
-void sortD (double *X, double *Y, double *Z, int N) {
-
-  int l,j,ir,i;
-  double tX, tY, tZ;
-  
-  if (N < 2) return;
-
-  l = N >> 1;
-  ir = N - 1;
-  for (;;) {
-    if (l > 0) {
-      l--;
-      tX = X[l];
-      tY = Y[l];
-      tZ = Z[l];
-    }
-    else {
-      tX = X[ir];
-      X[ir] = X[0];
-      tY = Y[ir];
-      Y[ir] = Y[0];
-      tZ = Z[ir];
-      Z[ir] = Z[0];
-      if (--ir == 0) {
-	X[0] = tX;
-	Y[0] = tY;
-	Z[0] = tZ;
-	return;
-      }
-    }
-    i = l;
-    j = (l << 1) + 1;
-    while (j <= ir) {
-      if (j < ir && X[j] < X[j+1]) j++;
-      if (tX < X[j]) {
-	X[i] = X[j];
-	Y[i] = Y[j];
-	Z[i] = Z[j];
-	j += (i=j) + 1;
-      }
-      else j = ir + 1;
-    }
-    X[i] = tX;
-    Y[i] = tY;
-    Z[i] = tZ;
-  }
-}
-
-void sort_lists (float *X, float *Y, int *S, int N) {
-
-  int l,j,ir,i;
-  double tX, tY;
-  int tS;
-  
-  if (N < 2) return;
-
-  l = N >> 1;
-  ir = N - 1;
-  for (;;) {
-    if (l > 0) {
-      l--;
-      tX = X[l];
-      tY = Y[l];
-      tS = S[l];
-    }
-    else {
-      tX = X[ir];
-      X[ir] = X[0];
-      tY = Y[ir];
-      Y[ir] = Y[0];
-      tS = S[ir];
-      S[ir] = S[0];
-      if (--ir == 0) {
-	X[0] = tX;
-	Y[0] = tY;
-	S[0] = tS;
-	return;
-      }
-    }
-    i = l;
-    j = (l << 1) + 1;
-    while (j <= ir) {
-      if (j < ir && X[j] < X[j+1]) j++;
-      if (tX < X[j]) {
-	X[i] = X[j];
-	Y[i] = Y[j];
-	S[i] = S[j];
-	j += (i=j) + 1;
-      }
-      else j = ir + 1;
-    }
-    X[i] = tX;
-    Y[i] = tY;
-    S[i] = tS;
-  }
-}
+# include "photdbc.h"
 
 void sort_time (unsigned int *X, int *Y, int N) {
 
-  int l,j,ir,i;
-  unsigned int tX;
-  int tY;
-  
-  if (N < 2) return;
+# define SWAPFUNC(A,B){ float tmp; \
+  unsigned int utmp = X[A]; X[A] = X[B]; X[B] = utmp; \
+  int          itmp = Y[A]; Y[A] = Y[B]; Y[B] = itmp; \
+}
+# define COMPARE(A,B)(X[A] < X[B])
 
-  l = N >> 1;
-  ir = N - 1;
-  for (;;) {
-    if (l > 0) {
-      l--;
-      tX = X[l];
-      tY = Y[l];
-    }
-    else {
-      tX = X[ir];
-      X[ir] = X[0];
-      tY = Y[ir];
-      Y[ir] = Y[0];
-      if (--ir == 0) {
-	X[0] = tX;
-	Y[0] = tY;
-	return;
-      }
-    }
-    i = l;
-    j = (l << 1) + 1;
-    while (j <= ir) {
-      if (j < ir && X[j] < X[j+1]) j++;
-      if (tX < X[j]) {
-	X[i] = X[j];
-	Y[i] = Y[j];
-	j += (i=j) + 1;
-      }
-      else j = ir + 1;
-    }
-    X[i] = tX;
-    Y[i] = tY;
-  }
+  OHANA_SORT (N, COMPARE, SWAPFUNC);
+
+# undef SWAPFUNC
+# undef COMPARE
+
 }
Index: /trunk/Ohana/src/relastro/Makefile
===================================================================
--- /trunk/Ohana/src/relastro/Makefile	(revision 16039)
+++ /trunk/Ohana/src/relastro/Makefile	(revision 16040)
@@ -49,5 +49,4 @@
 $(SRC)/plotstuff.$(ARCH).o	 \
 $(SRC)/select_images.$(ARCH).o	 \
-$(SRC)/sort.$(ARCH).o	         \
 $(SRC)/relastro.$(ARCH).o	 \
 $(SRC)/save_catalogs.$(ARCH).o   \
Index: /trunk/Ohana/src/relastro/src/liststats.c
===================================================================
--- /trunk/Ohana/src/relastro/src/liststats.c	(revision 16039)
+++ /trunk/Ohana/src/relastro/src/liststats.c	(revision 16040)
@@ -37,5 +37,5 @@
   if (N < 1) return (FALSE);
 
-  sortB (value, dvalue, N);
+  dsortpair (value, dvalue, N);
   stats[0].median = value[(int)(0.5*N)];
   stats[0].min    = value[0];
@@ -73,5 +73,5 @@
       chi[i] = (value[i] - stats[0].median) / dvalue[i];
     }
-    sortD (chi, value, dvalue, N);
+    dsortthree (chi, value, dvalue, N);
     free (chi);
   }
Index: /trunk/Ohana/src/relphot/Makefile
===================================================================
--- /trunk/Ohana/src/relphot/Makefile	(revision 16039)
+++ /trunk/Ohana/src/relphot/Makefile	(revision 16040)
@@ -42,5 +42,4 @@
 $(SRC)/setExclusions.$(ARCH).o 	 \
 $(SRC)/setMrelFinal.$(ARCH).o 	 \
-$(SRC)/sort.$(ARCH).o		 \
 $(SRC)/write_coords.$(ARCH).o
 
Index: /trunk/Ohana/src/relphot/src/liststats.c
===================================================================
--- /trunk/Ohana/src/relphot/src/liststats.c	(revision 16039)
+++ /trunk/Ohana/src/relphot/src/liststats.c	(revision 16040)
@@ -37,5 +37,5 @@
   if (N < 1) return (FALSE);
 
-  sortB (value, dvalue, N);
+  dsortpair (value, dvalue, N);
   stats[0].median = value[(int)(0.5*N)];
   stats[0].min    = value[0];
@@ -73,5 +73,5 @@
       chi[i] = (value[i] - stats[0].median) / dvalue[i];
     }
-    sortD (chi, value, dvalue, N);
+    dsortthree (chi, value, dvalue, N);
     free (chi);
   }
Index: /trunk/Ohana/src/tools/src/medianfilter.c
===================================================================
--- /trunk/Ohana/src/tools/src/medianfilter.c	(revision 16039)
+++ /trunk/Ohana/src/tools/src/medianfilter.c	(revision 16040)
@@ -3,6 +3,4 @@
 
 # define D_NFILES 100
-
-void fsort (float *value, int N);
 
 int main (int argc, char **argv) {
@@ -134,37 +132,2 @@
 
 }
-
-void fsort (float *value, int N) {
-
-  int l,j,ir,i;
-  float temp;
-  
-  if (N < 2) return;
-
-  l = N >> 1;
-  ir = N - 1;
-  for (;;) {
-    if (l > 0) {
-      temp = value[--l];
-    }
-    else {
-      temp = value[ir];
-      value[ir] = value[0];
-      if (--ir == 0) {
-	value[0] = temp;
-	return;
-      }
-    }
-    i = l;
-    j = (l << 1) + 1;
-    while (j <= ir) {
-      if (j < ir && value[j] < value[j+1]) ++j;
-      if (temp < value[j]) {
-	value[i]=value[j];
-	j += (i=j) + 1;
-      }
-      else j = ir + 1;
-    }
-    value[i] = temp;
-  }
-}
Index: /trunk/Ohana/src/uniphot/src/find_image_tgroups.c
===================================================================
--- /trunk/Ohana/src/uniphot/src/find_image_tgroups.c	(revision 16039)
+++ /trunk/Ohana/src/uniphot/src/find_image_tgroups.c	(revision 16040)
@@ -18,5 +18,5 @@
     time[i] = image[i].tzero;
   }
-  sort (time, Nimage);
+  sort_time (time, Nimage);
 
   /* find groups with dt < TRANGE */
Index: /trunk/Ohana/src/uniphot/src/liststats.c
===================================================================
--- /trunk/Ohana/src/uniphot/src/liststats.c	(revision 16039)
+++ /trunk/Ohana/src/uniphot/src/liststats.c	(revision 16040)
@@ -35,5 +35,5 @@
   if (N < 2) return (FALSE);
 
-  sortB (value, dvalue, N);
+  dsortpair (value, dvalue, N);
   stats[0].median = value[(int)(0.5*N)];
   stats[0].min    = value[0];
@@ -71,5 +71,5 @@
       chi[i] = (value[i] - stats[0].median) / dvalue[i];
     }
-    sortD (chi, value, dvalue, N);
+    dsortthree (chi, value, dvalue, N);
     free (chi);
   }
Index: /trunk/Ohana/src/uniphot/src/sort.c
===================================================================
--- /trunk/Ohana/src/uniphot/src/sort.c	(revision 16039)
+++ /trunk/Ohana/src/uniphot/src/sort.c	(revision 16040)
@@ -1,128 +1,13 @@
 # include "uniphot.h"
 
-void sort (unsigned int *X, int N) {
+void sort_time (unsigned int *value, int N) {
 
-  int l,j,ir,i;
-  unsigned tX;
-  
-  if (N < 2) return;
+# define SWAPFUNC(A,B){ unsigned int tmp = value[A]; value[A] = value[B]; value[B] = tmp; }
+# define COMPARE(A,B)(value[A] < value[B])
 
-  l = N >> 1;
-  ir = N - 1;
-  for (;;) {
-    if (l > 0) {
-      l--;
-      tX = X[l];
-    }
-    else {
-      tX = X[ir];
-      X[ir] = X[0];
-      if (--ir == 0) {
-	X[0] = tX;
-	return;
-      }
-    }
-    i = l;
-    j = (l << 1) + 1;
-    while (j <= ir) {
-      if (j < ir && X[j] < X[j+1]) j++;
-      if (tX < X[j]) {
-	X[i] = X[j];
-	j += (i=j) + 1;
-      }
-      else j = ir + 1;
-    }
-    X[i] = tX;
-  }
+  OHANA_SORT (N, COMPARE, SWAPFUNC);
+
+# undef SWAPFUNC
+# undef COMPARE
+
 }
-
-void sortB (double *X, double *Y, int N) {
-
-  int l,j,ir,i;
-  double tX, tY;
-  
-  if (N < 2) return;
-
-  l = N >> 1;
-  ir = N - 1;
-  for (;;) {
-    if (l > 0) {
-      l--;
-      tX = X[l];
-      tY = Y[l];
-    }
-    else {
-      tX = X[ir];
-      X[ir] = X[0];
-      tY = Y[ir];
-      Y[ir] = Y[0];
-      if (--ir == 0) {
-	X[0] = tX;
-	Y[0] = tY;
-	return;
-      }
-    }
-    i = l;
-    j = (l << 1) + 1;
-    while (j <= ir) {
-      if (j < ir && X[j] < X[j+1]) j++;
-      if (tX < X[j]) {
-	X[i] = X[j];
-	Y[i] = Y[j];
-	j += (i=j) + 1;
-      }
-      else j = ir + 1;
-    }
-    X[i] = tX;
-    Y[i] = tY;
-  }
-}
-
-void sortD (double *X, double *Y, double *Z, int N) {
-
-  int l,j,ir,i;
-  double tX, tY, tZ;
-  
-  if (N < 2) return;
-
-  l = N >> 1;
-  ir = N - 1;
-  for (;;) {
-    if (l > 0) {
-      l--;
-      tX = X[l];
-      tY = Y[l];
-      tZ = Z[l];
-    }
-    else {
-      tX = X[ir];
-      X[ir] = X[0];
-      tY = Y[ir];
-      Y[ir] = Y[0];
-      tZ = Z[ir];
-      Z[ir] = Z[0];
-      if (--ir == 0) {
-	X[0] = tX;
-	Y[0] = tY;
-	Z[0] = tZ;
-	return;
-      }
-    }
-    i = l;
-    j = (l << 1) + 1;
-    while (j <= ir) {
-      if (j < ir && X[j] < X[j+1]) j++;
-      if (tX < X[j]) {
-	X[i] = X[j];
-	Y[i] = Y[j];
-	Z[i] = Z[j];
-	j += (i=j) + 1;
-      }
-      else j = ir + 1;
-    }
-    X[i] = tX;
-    Y[i] = tY;
-    Z[i] = tZ;
-  }
-}
-
