Index: trunk/psastro/src/psastroConvert.c
===================================================================
--- trunk/psastro/src/psastroConvert.c	(revision 12806)
+++ trunk/psastro/src/psastroConvert.c	(revision 15083)
@@ -43,5 +43,12 @@
 
     // sort in ascending magnitude order
-    psArraySort (inStars, psastroSortByMag);
+    // psArraySort (inStars, psastroSortByMag);
+    // psVector *index = psArraySortIndex (sources, pmSourceSortBySN);
+    psVector *index = psArraySortIndex (NULL, inStars, psastroSortByMag);
+    for (int i = 0; i < 10; i++) {
+	int n = index->data.S32[i];
+	pmAstromObj *obj = inStars->data[n];
+	fprintf (stderr, "%d %d %f\n", i, n, obj->Mag);
+    }
 
     // we are going to select the brighter Nmax subset for astrometry 
@@ -51,6 +58,10 @@
     // choose the first nMax sources
     psArray *rawStars = psArrayAlloc (PS_MIN (nMax, inStars->n));
-    for (int i = 0; i < rawStars->n; i++) {
-	rawStars->data[i] = psMemIncrRefCounter (inStars->data[i]);
+    for (int i = 0, j = 0; (i < inStars->n) && (j < rawStars->n); i++) {
+	int n = index->data.S32[i];
+	pmSource *source = sources->data[n];
+	if (source->mode & PM_SOURCE_MODE_SATSTAR) continue;
+	rawStars->data[j] = psMemIncrRefCounter (inStars->data[n]);
+	j++;
     }
 
@@ -58,4 +69,5 @@
     psLogMsg ("psastro", 4, "loaded %ld sources, using %ld of %ld good sources\n", sources->n, rawStars->n, inStars->n);
 
+    psFree (index);
     psFree (inStars);
     psFree (rawStars);
@@ -100,8 +112,8 @@
 
 // sort by Mag (ascending)
-int psastroSortByMag (const void **a, const void **b)
+int psastroSortByMag (const void *a, const void *b)
 {
-    pmAstromObj *A = *(pmAstromObj **) a;
-    pmAstromObj *B = *(pmAstromObj **) b;
+    const pmAstromObj *A = a;
+    const pmAstromObj *B = b;
 
     psF32 mA = (isfinite(A->Mag)) ? A->Mag : FLT_MAX;
