Index: /trunk/Ohana/src/gastro2/Makefile
===================================================================
--- /trunk/Ohana/src/gastro2/Makefile	(revision 3412)
+++ /trunk/Ohana/src/gastro2/Makefile	(revision 3413)
@@ -46,5 +46,7 @@
 $(SRC)/getusno.$(ARCH).o \
 $(SRC)/gcatalog.$(ARCH).o \
-$(SRC)/remove_clumps.$(ARCH).o
+$(SRC)/remove_clumps.$(ARCH).o \
+$(SRC)/rfits.$(ARCH).o \
+$(SRC)/rtext.$(ARCH).o
 
 OBJ = $(GASTRO)
Index: /trunk/Ohana/src/gastro2/include/gastro2.h
===================================================================
--- /trunk/Ohana/src/gastro2/include/gastro2.h	(revision 3412)
+++ /trunk/Ohana/src/gastro2/include/gastro2.h	(revision 3413)
@@ -11,4 +11,19 @@
   int type;
 } StarData;
+
+typedef struct {
+  float X;
+  float Y;
+  float M;
+  float dM;
+  float Mgal;
+  float Map;
+  float sky;
+  float fx;
+  float fy;
+  float df;
+  char  dophot;
+  char  dummy[3];
+} Stars;
 
 typedef struct {
Index: /trunk/Ohana/src/gastro2/src/gstars2.c
===================================================================
--- /trunk/Ohana/src/gastro2/src/gstars2.c	(revision 3412)
+++ /trunk/Ohana/src/gastro2/src/gstars2.c	(revision 3413)
@@ -1,8 +1,5 @@
 # include "gastro2.h"
-
-/* by necesity hard wired */
-# define D_NSTARS 1000
-# define BYTES_STAR 66
-# define BLOCK 1000
+StarData *rtext (char *filename, Header *header, int *nstars);
+StarData *rfits (char *filename, Header *header, int *nstars);
 
 void gstars (char *filename, CmpCatalog *Target) {
@@ -12,5 +9,5 @@
   char line[80], *buffer;
   double dmag, type, det;
-  int NX, NY, FoundAstrom;
+  int NX, NY, FoundAstrom, extend;
 
   FILE *f;
@@ -121,58 +118,17 @@
   Target[0].Area = fabs (NX*NY*Target[0].coords.cdelt1*Target[0].coords.cdelt2*det);
 
-  /* allocate space for stars */
-  Nstars = -1;
-  fits_scan (&Target[0].header, "NSTARS", "%d", 1, &Nstars);
-  if (Nstars == -1) {
-    fprintf (stderr, "ERROR: failed to find NSTARS\n");
-    exit (1);
-  }
-  ALLOCATE (stars, StarData, MAX (Nstars, 1));
-  Nbytes = Nstars*BYTES_STAR;
-
-  /* open file for stars */
-  f = fopen (filename, "r");
-  if (f == NULL) {
-    fprintf (stderr, "ERROR: can't open file to load stars\n");
-    exit (1);
-  }
-  fseek (f, Target[0].header.size, SEEK_SET); 
-
-  N = nstars = 0;
-  ALLOCATE (buffer, char, (BLOCK*BYTES_STAR));
-  
-  while ((nbytes = fread (buffer, 1, (BLOCK*BYTES_STAR), f)) != 0) {
-    Ninstar = nbytes / BYTES_STAR;
-    for (i = 0; i < Ninstar; i++, nstars++) {
-      dparse (&stars[N].X, 1, &buffer[i*BYTES_STAR]);
-      dparse (&stars[N].Y, 2, &buffer[i*BYTES_STAR]);
-      dparse (&stars[N].M, 3, &buffer[i*BYTES_STAR]);
-      dparse (&dmag,       4, &buffer[i*BYTES_STAR]);
-      dparse (&type,       5, &buffer[i*BYTES_STAR]);
-
-      /* hardwired dophot exclusions should eventually be encapsulated elsewhere */
-      if ((type == 4) || (type == 6) || (type == 5) || (type == 9)) continue;
-      if (dmag > 1000*MAX_ERROR) continue;
-      stars[N].dM   = 0.001 * dmag;
-      stars[N].type = type;
-      N++;
-    }
-  }
-  free (buffer);
-  fclose (f);
- 
-  if (nstars != Nstars) {
-    fprintf (stderr, "WARNING: only read %d of %d stars\n", nstars, Nstars);
-  }
-  if (N < 5) { 
-    fprintf (stderr, "ERROR: too few stars for reliable solution, only %d\n", N);
-    exit (1);
+  extend = FALSE;
+  fits_scan (&Target[0].header, "EXTEND",  "%t", 1, &extend);
+  if (extend) {
+    stars = rfits (filename, &Target[0].header, &Nstars);
+  } else {
+    stars = rtext (filename, &Target[0].header, &Nstars);
   }
 
-  stars = remove_clumps (stars, &N, NX, NY);
+  stars = remove_clumps (stars, &Nstars, NX, NY);
 
-  sort_stars_mag (stars, N);  /* sorting by magnitude */
+  sort_stars_mag (stars, Nstars);  /* sorting by magnitude */
   Target[0].stars = stars;
-  Target[0].N = N;
+  Target[0].N = Nstars;
 
   /* limit number of stars */
Index: /trunk/Ohana/src/gastro2/src/rfits.c
===================================================================
--- /trunk/Ohana/src/gastro2/src/rfits.c	(revision 3413)
+++ /trunk/Ohana/src/gastro2/src/rfits.c	(revision 3413)
@@ -0,0 +1,122 @@
+# include "gastro2.h"
+int ConvertStars (Stars *data, int size, int nitems);
+
+StarData *rfits (char *filename, Header *header, int *nstars) {
+
+  int i, N, Nx, Ny, Nstars;
+  FILE *f;
+  Matrix matrix;
+  Header theader;
+  FTable table;
+  Stars *stars;
+  StarData *stardata;
+
+  /* open file for stars */
+  f = fopen (filename, "r");
+  if (f == NULL) {
+    fprintf (stderr, "ERROR: can't open file to load stars\n");
+    exit (1);
+  }
+  fseek (f, header[0].size, SEEK_SET); 
+
+  /* init & load in table data */
+  table.header   = &theader;
+  if (!fits_fread_matrix (f, &matrix, header))          goto escape;
+  if (!fits_fread_ftable (f, &table, "SMPFILE")) goto escape;
+
+  /* convert to internal format */
+  stars = (Stars *) table.buffer;
+  fits_scan (table.header, "NAXIS1", "%d", 1, &Nx);
+  fits_scan (table.header, "NAXIS2", "%d", 1, &Ny);
+  Nstars = Ny;
+
+  if (!ConvertStars (stars, sizeof (Stars), Nstars)) goto escape;
+
+  ALLOCATE (stardata, StarData, Nstars);
+  for (i = N = 0; i < Nstars; i++) {
+    /* hardwired dophot exclusions should eventually be encapsulated elsewhere */
+    if (stars[i].dophot == 4) continue;
+    if (stars[i].dophot == 5) continue;
+    if (stars[i].dophot == 6) continue;
+    if (stars[i].dophot == 9) continue;
+    if ((MAX_ERROR > 0) && (stars[i].dM > MAX_ERROR)) continue;
+    stardata[N].X    = stars[i].X;
+    stardata[N].Y    = stars[i].Y;
+    stardata[N].M    = stars[i].M;
+    stardata[N].dM   = 1000*stars[i].dM;
+    stardata[N].type = stars[i].dophot;
+    stardata[N].R    = 0;
+    stardata[N].D    = 0;
+    N++;
+  }    
+  if (N < 5) { 
+    fprintf (stderr, "ERROR: too few stars for reliable solution, only %d\n", N);
+    exit (1);
+  }
+
+  REALLOCATE (stardata, StarData, MAX(N,1));
+  *nstars = N;
+  return (stardata);
+
+escape:
+  fprintf (stderr, "error reading file\n");
+  exit (1);
+}
+
+# define SWAP_BYTE(X) \
+  tmp = byte[X+0]; byte[X+0] = byte[X+1]; byte[X+1] = tmp;
+# define SWAP_WORD(X) \
+  tmp = byte[X+0]; byte[X+0] = byte[X+3]; byte[X+3] = tmp; \
+  tmp = byte[X+1]; byte[X+1] = byte[X+2]; byte[X+2] = tmp;
+# define SWAP_DBLE(X) \
+  tmp = byte[X+0]; byte[X+0] = byte[X+7]; byte[X+7] = tmp; \
+  tmp = byte[X+1]; byte[X+1] = byte[X+6]; byte[X+6] = tmp; \
+  tmp = byte[X+2]; byte[X+2] = byte[X+5]; byte[X+5] = tmp; \
+  tmp = byte[X+3]; byte[X+3] = byte[X+4]; byte[X+4] = tmp;
+
+# ifdef linux
+# define BYTE_SWAP
+# endif
+
+# ifdef sid
+# define BYTE_SWAP
+# endif
+
+# ifdef dec
+# define BYTE_SWAP
+# endif
+
+# define STARS_SIZE 44
+
+int ConvertStars (Stars *data, int size, int nitems) {
+
+  int i;
+  unsigned char *byte, tmp;
+
+# ifdef BYTE_SWAP
+
+  if (size != STARS_SIZE) {
+    fprintf (stderr, "mismatch in type sizes (Stars) %d vs %d\n", size, STARS_SIZE);
+    return (FALSE);
+  }
+
+  byte = (char *) data;
+  for (i = 0; i < nitems; i++, byte += size) {
+    SWAP_WORD (0);   /* R */
+    SWAP_WORD (4);   /* Y */
+    SWAP_WORD (8);   /* M */
+    SWAP_WORD (12);  /* dM */
+    SWAP_WORD (16);  /* Mgal */
+    SWAP_WORD (20);  /* Map */
+    SWAP_WORD (24);  /* sky */
+    SWAP_WORD (28);  /* fx */
+    SWAP_WORD (32);  /* fy */
+    SWAP_WORD (36);  /* df */
+  }
+  return (TRUE);
+
+# else
+  return (TRUE);
+# endif  
+} 
+
Index: /trunk/Ohana/src/gastro2/src/rtext.c
===================================================================
--- /trunk/Ohana/src/gastro2/src/rtext.c	(revision 3413)
+++ /trunk/Ohana/src/gastro2/src/rtext.c	(revision 3413)
@@ -0,0 +1,65 @@
+# include "gastro2.h"
+/* by necesity hard wired */
+# define D_NSTARS 1000
+# define BYTES_STAR 66
+# define BLOCK 1000
+
+StarData *rtext (char *filename, Header *header, int *nstars) {
+
+  FILE *f;
+  char *buffer;
+  int i, N, Nbytes, nbytes, Ninstar, Nstars, NSTARS;
+  double dmag, type;
+  StarData *stars;
+
+  /* allocate space for stars */
+  NSTARS = -1;
+  fits_scan (header, "NSTARS", "%d", 1, &NSTARS);
+  if (NSTARS == -1) {
+    fprintf (stderr, "ERROR: failed to find NSTARS\n");
+    exit (1);
+  }
+  ALLOCATE (stars, StarData, MAX (NSTARS, 1));
+  Nbytes = NSTARS*BYTES_STAR;
+
+  /* open file for stars */
+  f = fopen (filename, "r");
+  if (f == NULL) {
+    fprintf (stderr, "ERROR: can't open file to load stars\n");
+    exit (1);
+  }
+  fseek (f, header[0].size, SEEK_SET); 
+
+  N = Nstars = 0;
+  ALLOCATE (buffer, char, (BLOCK*BYTES_STAR));
+  
+  while ((nbytes = fread (buffer, 1, (BLOCK*BYTES_STAR), f)) != 0) {
+    Ninstar = nbytes / BYTES_STAR;
+    for (i = 0; i < Ninstar; i++, Nstars++) {
+      dparse (&stars[N].X, 1, &buffer[i*BYTES_STAR]);
+      dparse (&stars[N].Y, 2, &buffer[i*BYTES_STAR]);
+      dparse (&stars[N].M, 3, &buffer[i*BYTES_STAR]);
+      dparse (&dmag,       4, &buffer[i*BYTES_STAR]);
+      dparse (&type,       5, &buffer[i*BYTES_STAR]);
+
+      /* hardwired dophot exclusions should eventually be encapsulated elsewhere */
+      if ((type == 4) || (type == 6) || (type == 5) || (type == 9)) continue;
+      if ((MAX_ERROR > 0) && (dmag > 1000*MAX_ERROR)) continue;
+      stars[N].dM   = 0.001 * dmag;
+      stars[N].type = type;
+      N++;
+    }
+  }
+  free (buffer);
+  fclose (f);
+ 
+  if (Nstars != NSTARS) {
+    fprintf (stderr, "WARNING: only read %d of %d stars\n", Nstars, NSTARS);
+  }
+  if (N < 5) { 
+    fprintf (stderr, "ERROR: too few stars for reliable solution, only %d\n", N);
+    exit (1);
+  }
+  *nstars = N;
+  return (stars);
+}
