Index: /trunk/Ohana/src/addstar/Makefile
===================================================================
--- /trunk/Ohana/src/addstar/Makefile	(revision 3415)
+++ /trunk/Ohana/src/addstar/Makefile	(revision 3416)
@@ -61,5 +61,8 @@
 $(SRC)/update_coords.$(ARCH).o \
 $(SRC)/wcatalog.$(ARCH).o \
-$(SRC)/wimage.$(ARCH).o
+$(SRC)/wimage.$(ARCH).o \
+$(SRC)/rfits.$(ARCH).o \
+$(SRC)/rtext.$(ARCH).o \
+$(SRC)/ConvertStars.$(ARCH).o
 
 EXTRA = \
Index: /trunk/Ohana/src/addstar/include/addstar.h
===================================================================
--- /trunk/Ohana/src/addstar/include/addstar.h	(revision 3415)
+++ /trunk/Ohana/src/addstar/include/addstar.h	(revision 3416)
@@ -17,4 +17,20 @@
     fprintf (stderr, "ERROR:  photcode %s not found in photcode table\n", NAME); \
     exit (0); }
+
+/* this structure is used for loading the data from the FITS smp file */
+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];
+} StarData;
 
 typedef struct {
@@ -198,2 +214,3 @@
 GSCRegion *gregion_match (GSCRegion *regions, int *nregions);
 GSCRegion *gregion_patch (GSCRegion *patch, int *nregions);
+int ConvertStars (StarData *data, int size, int nitems);
Index: /trunk/Ohana/src/addstar/src/ConvertStars.c
===================================================================
--- /trunk/Ohana/src/addstar/src/ConvertStars.c	(revision 3416)
+++ /trunk/Ohana/src/addstar/src/ConvertStars.c	(revision 3416)
@@ -0,0 +1,58 @@
+# include "addstar.h"
+
+# 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 (StarData *data, int size, int nitems) {
+
+  int i;
+  unsigned char *byte, tmp;
+
+# ifdef BYTE_SWAP
+
+  if (size != STARS_SIZE) {
+    fprintf (stderr, "mismatch in type sizes (StarData) %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/addstar/src/gimages.c
===================================================================
--- /trunk/Ohana/src/addstar/src/gimages.c	(revision 3415)
+++ /trunk/Ohana/src/addstar/src/gimages.c	(revision 3416)
@@ -98,4 +98,5 @@
   ALLOCATE (pimage, Image, NPIMAGE);
 
+  fprintf (stderr, "find overlapping images\n");
   /* run through image table, reading images in blocks */
   for (Ntimage = 0; Ntimage < Nimage; Ntimage += ntimage) {
@@ -141,4 +142,5 @@
     }
   }
+  fprintf (stderr, "done\n");
 
   BuildChipMatch (pimage, npimage);
@@ -157,3 +159,5 @@
    if the WRP image is loaded near the beginning of a block, then the corresponding DIS 
    may be in the previous block, and thus missed 
+
+   we are not adding the matching DIS image to the table.
 */
Index: /trunk/Ohana/src/addstar/src/gstars.c
===================================================================
--- /trunk/Ohana/src/addstar/src/gstars.c	(revision 3415)
+++ /trunk/Ohana/src/addstar/src/gstars.c	(revision 3416)
@@ -1,6 +1,3 @@
 # include "addstar.h"
-# define D_NSTARS 1000
-# define BYTES_STAR 66
-# define BLOCK 1000
 
 Stars *gstars (char *file, int *NSTARS, Image *image) {
@@ -8,7 +5,7 @@
   FILE *f;
   Header header;
-  int j, N, Ninstar;
+  int j, N, Ninstar, extend;
   int nbytes, Nbytes;
-  Stars *stars;
+  Stars *stars, *rdstars;
   char *buffer, *c, *c2, photcode[64];
   double tmp;
@@ -22,12 +19,4 @@
     exit (1);
   }
-
-  /* open file */
-  f = fopen (file, "r");
-  if (f == NULL) {
-    fprintf (stderr, "ERROR: can't read data from %s\n", file);
-    exit (1);
-  }
-  fseek (f, header.size, SEEK_SET); 
 
   /* find image rootname */
@@ -147,5 +136,4 @@
     exit (1);
   }
-  ALLOCATE (stars, Stars, image[0].nstar);
 
   /* this is really lame.  get sky background from Flips data */
@@ -170,92 +158,54 @@
   }
 
-  /* load in stars by blocks of 1000 */
-  N = 0;
-  ALLOCATE (buffer, char, (BLOCK*BYTES_STAR) + 1);
-  buffer[BLOCK*BYTES_STAR] = 0;
-  Nextra = 0;
-  doneread = FALSE;
-  while (!doneread) {
-    Nbytes = BYTES_STAR * BLOCK - Nextra;
-    nbytes = Fread (&buffer[Nextra], 1, Nbytes, f, "char");
-    if (nbytes == 0) {
-      doneread = TRUE;
-      continue;
+  /* read in data section */
+  f = fopen (file, "r");
+  if (f == NULL) {
+    fprintf (stderr, "ERROR: can't read data from %s\n", file);
+    exit (1);
+  }
+  fseek (f, header.size, SEEK_SET); 
+
+  extend = FALSE;
+  fits_scan (&header, "EXTEND",  "%t", 1, &extend);
+  if (extend) {
+    Nbytes = fits_matrix_size (&header); 
+    fseek (f, Nbytes, SEEK_CUR); 
+    rdstars = rfits (f, &image[0].nstar);
+  } else {
+    rdstars = rtext (f, &image[0].nstar);
+  }
+  fclose (f);
+
+  /* modify resulting star list */
+  ALLOCATE (stars, Stars, image[0].nstar);
+  for (N = j = 0; j < image[0].nstar; j++) {
+    /* allow for some dynamic filtering of star list */
+    if (rdstars[j].dM > 1000.0 / SNLIMIT) continue;
+    if (XMAX && (rdstars[j].X > XMAX)) continue;
+    if (XMIN && (rdstars[j].X < XMIN)) continue;
+    if (YMAX && (rdstars[j].Y > YMAX)) continue;
+    if (YMIN && (rdstars[j].Y < YMIN)) continue;
+    stars[N] = rdstars[j];
+
+    XY_to_RD (&stars[N].R, &stars[N].D, stars[N].X, stars[N].Y, &image[0].coords);
+    while (stars[N].R < 0.0) stars[N].R += 360.0;
+    while (stars[N].R >= 360.0) stars[N].R -= 360.0;
+    stars[N].found = -1;
+    stars[N].code = thiscode[0].code;
+
+    if (SKYPROBE) {
+      dMs = get_subpix (stars[N].X, stars[N].Y);
+      stars[N].M    -= dMs;
+      stars[N].Mgal -= dMs;
+      stars[N].Map  -= dMs;
+      dMs = 1000.0*scat_subpix (stars[N].X, stars[N].Y);
+      stars[N].dM = hypot (stars[N].dM, dMs);
     }
-    nbytes += Nextra;
-    /* check line-by-line integrity */
-    c = buffer;
-    done = FALSE;
-    while ((c < buffer + nbytes) && (!done)) { 
-      for (c2 = c; *c2 == '\n'; c2++);
-      if (c2 > c) { /* extra return chars */
-	memmove (c, c2, (int)(buffer + nbytes - c2));
-	Nskip = c2 - c;
-	nbytes -= Nskip;
-	bzero (buffer + nbytes, Nskip);
-	if (VERBOSE) fprintf (stderr, "deleted %d extra return chars\n", Nskip);
-      }
-      c2 = strchr (c, '\n');
-      if (c2 == (char *) NULL) {
-	done = TRUE;	
-	continue;
-      }
-      c2++;
-      if ((c2 - c) != BYTES_STAR) { /* bad line, delete it */
-	memmove (c, c2, (int)(buffer + nbytes - c2));
-	Nskip = c2 - c;
-	nbytes -= Nskip;
-	bzero (buffer + nbytes, Nskip);
-	if (VERBOSE) fprintf (stderr, "deleted line, %d extra chars\n", Nskip);
-      } else {
-	c = c2;
-      }
-    }
-    Ninstar = nbytes / BYTES_STAR;
-    Nextra = nbytes % BYTES_STAR;
-    for (j = 0; j < Ninstar; j++, N++) {
-      dparse (&stars[N].X,  1, &buffer[j*BYTES_STAR]);
-      dparse (&stars[N].Y,  2, &buffer[j*BYTES_STAR]);
-      dparse (&stars[N].M,  3, &buffer[j*BYTES_STAR]);
-
-      /* cmp files carry dM in millimags */
-      dparse (&tmp, 4, &buffer[j*BYTES_STAR]);
-      stars[N].dM = 0.001*tmp;
-
-      /* allow for some dynamic filtering of star list */
-      if (stars[N].dM > 1000.0 / SNLIMIT) { N--; continue; }
-      if (XMAX && (stars[N].X > XMAX))    { N--; continue; }
-      if (XMIN && (stars[N].X < XMIN))    { N--; continue; }
-      if (YMAX && (stars[N].Y > YMAX))    { N--; continue; }
-      if (YMIN && (stars[N].Y < YMIN))    { N--; continue; }
-
-      dparse (&tmp,         5, &buffer[j*BYTES_STAR]);
-      stars[N].dophot = tmp;
-
-      dparse (&stars[N].Mgal, 7, &buffer[j*BYTES_STAR]);
-      dparse (&stars[N].Map,  8, &buffer[j*BYTES_STAR]);
-      dparse (&stars[N].fx,   9, &buffer[j*BYTES_STAR]);
-      dparse (&stars[N].fy,  10, &buffer[j*BYTES_STAR]);
-      dparse (&stars[N].df,  11, &buffer[j*BYTES_STAR]);
-
-      XY_to_RD (&stars[N].R, &stars[N].D, stars[N].X, stars[N].Y, &image[0].coords);
-      while (stars[N].R < 0.0) stars[N].R += 360.0;
-      while (stars[N].R >= 360.0) stars[N].R -= 360.0;
-      stars[N].found = -1;
-      stars[N].code = thiscode[0].code;
-
-      if (SKYPROBE) {
-	dMs = get_subpix (stars[N].X, stars[N].Y);
-	stars[N].M    -= dMs;
-	stars[N].Mgal -= dMs;
-	stars[N].Map  -= dMs;
-	dMs = 1000.0*scat_subpix (stars[N].X, stars[N].Y);
-	stars[N].dM = hypot (stars[N].dM, dMs);
-      }
-    }
-  }
-
+    N ++;
+  }
   image[0].nstar = N;
   REALLOCATE (stars, Stars, image[0].nstar);
+  free (rdstars);
+
   if (VERBOSE) fprintf (stderr, "read %d stars from target file\n", image[0].nstar);
   *NSTARS = image[0].nstar;
Index: /trunk/Ohana/src/addstar/src/rfits.c
===================================================================
--- /trunk/Ohana/src/addstar/src/rfits.c	(revision 3416)
+++ /trunk/Ohana/src/addstar/src/rfits.c	(revision 3416)
@@ -0,0 +1,43 @@
+# include "addstar.h"
+
+Stars *rfits (FILE *f, int *nstars) {
+
+  int i, N, Nx, Ny, Nstars;
+  Header theader;
+  FTable table;
+  Stars *stars;
+  StarData *stardata;
+
+  /* init & load in table data */
+  table.header   = &theader;
+  if (!fits_fread_ftable (f, &table, "SMPFILE")) goto escape;
+
+  /* convert to internal format */
+  stardata = (StarData *) table.buffer;
+  fits_scan (table.header, "NAXIS1", "%d", 1, &Nx);
+  fits_scan (table.header, "NAXIS2", "%d", 1, &Ny);
+  Nstars = Ny;
+
+  if (!ConvertStars (stardata, sizeof (StarData), Nstars)) goto escape;
+
+  ALLOCATE (stars, Stars, Nstars);
+  for (i = 0; i < Nstars; i++) {
+    stars[i].X      = stardata[i].X;
+    stars[i].Y      = stardata[i].Y;
+    stars[i].M      = stardata[i].M;
+    stars[i].dM     = stardata[i].dM;
+    stars[i].dophot = stardata[i].dophot;
+
+    stars[i].Mgal   = stardata[i].M;
+    stars[i].Map    = stardata[i].dM;
+    stars[i].fx     = stardata[i].fx;
+    stars[i].fy     = stardata[i].fy;
+    stars[i].df     = stardata[i].df;
+  }    
+  *nstars = Nstars;
+  return (stars);
+
+escape:
+  fprintf (stderr, "error reading file\n");
+  exit (1);
+}
Index: /trunk/Ohana/src/addstar/src/rtext.c
===================================================================
--- /trunk/Ohana/src/addstar/src/rtext.c	(revision 3416)
+++ /trunk/Ohana/src/addstar/src/rtext.c	(revision 3416)
@@ -0,0 +1,83 @@
+# include "addstar.h"
+# define D_NSTARS 1000
+# define BYTES_STAR 66
+# define BLOCK 1000
+
+Stars *rtext (FILE *f, int *nstars) {
+
+  int j, N, Nextra, Ninstar, Nskip, Nbytes, nbytes;
+  int done;
+  char *buffer, *c, *c2;
+  double tmp;
+  Stars *stars;
+  
+  /* load in stars by blocks of 1000 */
+  N = 0;
+  ALLOCATE (buffer, char, (BLOCK*BYTES_STAR) + 1);
+  buffer[BLOCK*BYTES_STAR] = 0;
+  Nextra = 0;
+
+  ALLOCATE (stars, Stars, *nstars);
+
+  while (1) {
+    /* load next data block */
+    Nbytes = BYTES_STAR * BLOCK - Nextra;
+    nbytes = Fread (&buffer[Nextra], 1, Nbytes, f, "char");
+    if (nbytes == 0) {
+      *nstars = N;
+      return (stars);
+    }
+    nbytes += Nextra;
+
+    /* check line-by-line integrity */
+    c = buffer;
+    done = FALSE;
+    while ((c < buffer + nbytes) && (!done)) { 
+      for (c2 = c; *c2 == '\n'; c2++);
+      if (c2 > c) { /* extra return chars */
+	memmove (c, c2, (int)(buffer + nbytes - c2));
+	Nskip = c2 - c;
+	nbytes -= Nskip;
+	bzero (buffer + nbytes, Nskip);
+	if (VERBOSE) fprintf (stderr, "deleted %d extra return chars\n", Nskip);
+      }
+      c2 = strchr (c, '\n');
+      if (c2 == (char *) NULL) {
+	done = TRUE;	
+	continue;
+      }
+      c2++;
+      if ((c2 - c) != BYTES_STAR) { /* bad line, delete it */
+	memmove (c, c2, (int)(buffer + nbytes - c2));
+	Nskip = c2 - c;
+	nbytes -= Nskip;
+	bzero (buffer + nbytes, Nskip);
+	if (VERBOSE) fprintf (stderr, "deleted line, %d extra chars\n", Nskip);
+      } else {
+	c = c2;
+      }
+    }
+
+    /* extract data for stars */
+    Ninstar = nbytes / BYTES_STAR;
+    Nextra = nbytes % BYTES_STAR;
+    for (j = 0; j < Ninstar; j++, N++) {
+      dparse (&stars[N].X,  1, &buffer[j*BYTES_STAR]);
+      dparse (&stars[N].Y,  2, &buffer[j*BYTES_STAR]);
+      dparse (&stars[N].M,  3, &buffer[j*BYTES_STAR]);
+
+      /* cmp files carry dM in millimags */
+      dparse (&tmp, 4, &buffer[j*BYTES_STAR]);
+      stars[N].dM = 0.001*tmp;
+
+      dparse (&tmp,         5, &buffer[j*BYTES_STAR]);
+      stars[N].dophot = tmp;
+
+      dparse (&stars[N].Mgal, 7, &buffer[j*BYTES_STAR]);
+      dparse (&stars[N].Map,  8, &buffer[j*BYTES_STAR]);
+      dparse (&stars[N].fx,   9, &buffer[j*BYTES_STAR]);
+      dparse (&stars[N].fy,  10, &buffer[j*BYTES_STAR]);
+      dparse (&stars[N].df,  11, &buffer[j*BYTES_STAR]);
+    }
+  }
+}
Index: /trunk/Ohana/src/mosastro/Makefile
===================================================================
--- /trunk/Ohana/src/mosastro/Makefile	(revision 3415)
+++ /trunk/Ohana/src/mosastro/Makefile	(revision 3416)
@@ -57,5 +57,9 @@
 $(SRC)/clip.$(ARCH).o \
 $(SRC)/parse_time.$(ARCH).o \
-$(SRC)/testcoords.$(ARCH).o
+$(SRC)/testcoords.$(ARCH).o \
+$(SRC)/rfits.$(ARCH).o \
+$(SRC)/wfits.$(ARCH).o \
+$(SRC)/rtext.$(ARCH).o \
+$(SRC)/ConvertStars.$(ARCH).o
 
 mosastro: $(BIN)/mosastro.$(ARCH)
Index: /trunk/Ohana/src/mosastro/include/mosastro.h
===================================================================
--- /trunk/Ohana/src/mosastro/include/mosastro.h	(revision 3415)
+++ /trunk/Ohana/src/mosastro/include/mosastro.h	(revision 3416)
@@ -23,6 +23,10 @@
   StarData *stars;
   Header header;
+  Header theader;
+  Matrix matrix;
+
   char *buffer;
   int Nbuffer;
+  int FITS;
 
   int Nmatch;
@@ -55,11 +59,16 @@
 
 typedef struct {
-  double X;
-  double Y;
-  double M, dM;
-  char   dophot;
-  double sky;
-  double fx, fy, df;
-  double Mgal, Map;
+  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;
 
@@ -168,2 +177,3 @@
 void wchip (char *filename, Chip *data);
 void wstars (char *filename, Stars *stars, int Nstars, Header *header);
+int ConvertStars (Stars *data, int size, int nitems);
Index: /trunk/Ohana/src/mosastro/src/ConvertStars.c
===================================================================
--- /trunk/Ohana/src/mosastro/src/ConvertStars.c	(revision 3416)
+++ /trunk/Ohana/src/mosastro/src/ConvertStars.c	(revision 3416)
@@ -0,0 +1,58 @@
+# include "mosastro.h"
+
+# 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/mosastro/src/LoadStars.c
===================================================================
--- /trunk/Ohana/src/mosastro/src/LoadStars.c	(revision 3415)
+++ /trunk/Ohana/src/mosastro/src/LoadStars.c	(revision 3416)
@@ -1,8 +1,7 @@
 # include "mosastro.h"
-# define BYTES_STAR 66
 
 int LoadStars (int Nfile, char **file) {
 
-  int i, j, itmp, Nbytes, nbytes;
+  int i, j, itmp, Nbytes, nbytes, status, extend;
   FILE *f;
   time_t tsval;
@@ -23,11 +22,4 @@
       continue;
     }
-    f = fopen (chip[Nchip].file, "r");
-    if (f == NULL) {
-      fprintf (stderr, "ERROR: can't read data from %s\n", file[i]);
-      free (chip[Nchip].file);
-      continue;
-    }
-    fseek (f, chip[Nchip].header.size, SEEK_SET); 
 
     /* get image dimensions */
@@ -39,4 +31,5 @@
       fprintf (stderr, "ERROR: no astrometric solution in header\n");
       free (chip[Nchip].file);
+      fits_free_header (&chip[Nchip].header);
       continue;
     }
@@ -49,4 +42,5 @@
       fprintf (stderr, "ERROR: bad astrometric solution in header %s\n", file[i]);
       free (chip[Nchip].file);
+      fits_free_header (&chip[Nchip].header);
       continue;
     }
@@ -57,26 +51,16 @@
     Year = tmval[0].tm_year;
 
-    /* find expected number of stars */
-    fits_scan (&chip[Nchip].header, "NSTARS", "%d", 1, &chip[Nchip].Nstars);
-    if (chip[Nchip].Nstars == 0) {
-      fprintf (stderr, "ERROR: can't get NSTARS from header for %s\n", file[i]);
+    extend = FALSE;
+    fits_scan (&chip[Nchip].header, "EXTEND",  "%t", 1, &extend);
+    if (extend) {
+      status = rfits (&chip[Nchip]);
+    } else {
+      status = rtext (&chip[Nchip]);
+    }
+    if (!status) {
+      /* skip on failure */
       free (chip[Nchip].file);
+      fits_free_header (&chip[Nchip].header);
       continue;
-    }
-    ALLOCATE (chip[Nchip].stars, StarData, chip[Nchip].Nstars);
-    
-    Nbytes = BYTES_STAR * chip[Nchip].Nstars;
-    ALLOCATE (chip[Nchip].buffer, char, Nbytes + 1);
-    nbytes = Fread (chip[Nchip].buffer, 1, Nbytes, f, "char");
-    if (nbytes != Nbytes) { exit (1); }
-    chip[Nchip].Nbuffer = Nbytes;
-
-    for (j = 0; j < chip[Nchip].Nstars; j++) {
-      bzero (&chip[Nchip].stars[j], sizeof(StarData));
-      dparse (&chip[Nchip].stars[j].X,    1, &chip[Nchip].buffer[j*BYTES_STAR]);
-      dparse (&chip[Nchip].stars[j].Y,    2, &chip[Nchip].buffer[j*BYTES_STAR]);
-      dparse (&chip[Nchip].stars[j].Mag,  3, &chip[Nchip].buffer[j*BYTES_STAR]);
-      dparse (&chip[Nchip].stars[j].dMag, 4, &chip[Nchip].buffer[j*BYTES_STAR]);
-      chip[Nchip].stars[j].dMag *= 0.001;  /* millimag errors stored in file */
     }
 
Index: /trunk/Ohana/src/mosastro/src/clip.c
===================================================================
--- /trunk/Ohana/src/mosastro/src/clip.c	(revision 3415)
+++ /trunk/Ohana/src/mosastro/src/clip.c	(revision 3416)
@@ -8,8 +8,6 @@
   StarData *raw, *ref;
 
-  sigma = GetScatter (&Nscatter);
-  fprintf (stderr, "scatter (chip) : %f for %d stars\n", sigma, Nscatter);
-
   Nmask = Nkeep = 0;
+  sigma = GetScatter (&Nscatter) / (3600.0 * field.project.cdelt1);
 
   for (i = 0; i < Nchip; i++) {
Index: /trunk/Ohana/src/mosastro/src/dump.c
===================================================================
--- /trunk/Ohana/src/mosastro/src/dump.c	(revision 3415)
+++ /trunk/Ohana/src/mosastro/src/dump.c	(revision 3416)
@@ -6,5 +6,5 @@
 
   for (i = 0; i < Nstars; i++) {
-    fprintf (f, "%4d  %10.6f %10.6f  %8.2f %8.2f  %8.2f %8.2f  %7.2f %7.2f  %7.2f %7.2f\n", 
+    fprintf (f, "%4d  %10.6f %10.6f  %8.2f %8.2f  %8.2f %8.2f  %7.2f %7.2f  %7.2f %7.2f  %d\n", 
 	     i, 
 	     stars[i].R, stars[i].D,
@@ -12,5 +12,5 @@
 	     stars[i].L, stars[i].M,
 	     stars[i].X, stars[i].Y,
-	     stars[i].Mag, stars[i].dMag);
+	     stars[i].Mag, stars[i].dMag, stars[i].mask);
   }
   return (1);
Index: /trunk/Ohana/src/mosastro/src/mosastro.c
===================================================================
--- /trunk/Ohana/src/mosastro/src/mosastro.c	(revision 3415)
+++ /trunk/Ohana/src/mosastro/src/mosastro.c	(revision 3416)
@@ -56,5 +56,5 @@
   if ((DUMP != NULL) && !strcmp (DUMP, "fitchips_unclip")) dump_match();
 
-  ClipOnFP (3.0);
+  ClipOnFP (2.0);
   Cerror   = GetScatter (&Nastro);
   fprintf (stderr, "scatter (clip) : %f for %d stars\n", Cerror, Nastro);
@@ -64,5 +64,13 @@
   fprintf (stderr, "scatter (chip) : %f for %d stars\n", Cerror, Nastro);
 
-  ClipOnFP (3.0);
+  ClipOnFP (2.0);
+  Cerror   = GetScatter (&Nastro);
+  fprintf (stderr, "scatter (clip) : %f for %d stars\n", Cerror, Nastro);
+
+  FitChips (ChipOrder);
+  Cerror   = GetScatter (&Nastro);
+  fprintf (stderr, "scatter (chip) : %f for %d stars\n", Cerror, Nastro);
+
+  ClipOnFP (2.0);
   Cerror   = GetScatter (&Nastro);
   fprintf (stderr, "scatter (clip) : %f for %d stars\n", Cerror, Nastro);
Index: /trunk/Ohana/src/mosastro/src/output.c
===================================================================
--- /trunk/Ohana/src/mosastro/src/output.c	(revision 3415)
+++ /trunk/Ohana/src/mosastro/src/output.c	(revision 3416)
@@ -4,6 +4,7 @@
 
   int i, N;
-  char *p;
-  char outname[256];
+  char *p, outname[256];
+  FILE *f;
+  Header *header;
   
   for (i = 0; i < Nchip; i++) {
@@ -26,42 +27,14 @@
   }
 
-  { 
+  field_combine ();
 
-    Header *header;
-    FILE *f;
-    
-    field_combine ();
-
-    f = fopen (phu, "w");
-    if (f == (FILE *) NULL) {
-      fprintf (stderr, "ERROR: can't create output file %s\n", phu);
-      exit (1);
-    }
-
-    header = mkmosaic (1, 1, 0, &field.project);
-    fwrite (header[0].buffer, 1, header[0].size, f);
-    fclose (f);
+  f = fopen (phu, "w");
+  if (f == (FILE *) NULL) {
+    fprintf (stderr, "ERROR: can't create output file %s\n", phu);
+    exit (1);
   }
 
-  { 
-
-    FILE *f;
-    double R, D;
-
-    fprintf (stderr, "starting test.dat\n");
-    f = fopen ("test.dat", "w");
-    if (f == (FILE *) NULL) {
-      fprintf (stderr, "ERROR: can't create output file \n");
-      exit (1);
-    }
-
-    RegisterMosaic (&field.project);
-    for (i = 0; i < chip[0].Nmatch; i++) {
-      XY_to_RD (&R, &D, chip[0].raw[i].X, chip[0].raw[i].Y, &chip[0].map);
-      fprintf (f, "%4d  %10.6f %10.6f  %8.2f %8.2f\n", 
-	       i, R, D, chip[0].raw[i].X, chip[0].raw[i].Y);
-    }
-    fclose (f);
-  }
-  
+  header = mkmosaic (1, 1, 0, &field.project);
+  fwrite (header[0].buffer, 1, header[0].size, f);
+  fclose (f);
 }
Index: /trunk/Ohana/src/mosastro/src/rfits.c
===================================================================
--- /trunk/Ohana/src/mosastro/src/rfits.c	(revision 3416)
+++ /trunk/Ohana/src/mosastro/src/rfits.c	(revision 3416)
@@ -0,0 +1,70 @@
+# include "mosastro.h"
+
+int rfits (Chip *mychip) {
+
+  int i, Nx, Ny;
+  FILE *f;
+  FTable table;
+  Stars *stars;
+  StarData *stardata;
+
+  /* open file for stars */
+  f = fopen (mychip[0].file, "r");
+  if (f == NULL) {
+    fprintf (stderr, "ERROR: can't open file to load stars\n");
+    exit (1);
+  }
+  fseek (f, mychip[0].header.size, SEEK_SET); 
+
+  /* init & load in table data */
+  table.header   = &mychip[0].theader;
+  if (!fits_fread_matrix (f, &mychip[0].matrix, &mychip[0].header)) {
+    fprintf (stderr, "error reading file\n");
+    fclose (f);
+    return (FALSE);
+  }
+  if (!fits_fread_ftable (f, &table, "SMPFILE")) {
+    fprintf (stderr, "error reading file\n");
+    fits_free_matrix (&mychip[0].matrix);
+    fclose (f);
+    return (FALSE);
+  }
+
+  /* convert to internal format */
+  stars = (Stars *) table.buffer;
+  fits_scan (table.header, "NAXIS1", "%d", 1, &Nx);
+  fits_scan (table.header, "NAXIS2", "%d", 1, &Ny);
+  mychip[0].Nstars = Ny;
+
+  /* save raw data for output */
+  mychip[0].FITS = TRUE;
+  mychip[0].buffer = (char *) stars;
+  mychip[0].Nbuffer = Nx*Ny;
+
+  if (Ny < 5) { 
+    fprintf (stderr, "Too few stars for reliable solution, only %d\n", Ny);
+    fits_free_matrix (&mychip[0].matrix);
+    fits_free_table (&table);
+    fclose (f);
+    return (FALSE);
+  }
+
+  if (!ConvertStars (stars, sizeof (Stars), mychip[0].Nstars)) {
+    fprintf (stderr, "conversion error\n");
+    fits_free_matrix (&mychip[0].matrix);
+    fits_free_table (&table);
+    fclose (f);
+    return (FALSE);
+  }
+
+  /* note the different structures for mychip[0].stars and stars */
+  ALLOCATE (mychip[0].stars, StarData, mychip[0].Nstars);
+  for (i = 0; i < mychip[0].Nstars; i++) {
+    mychip[0].stars[i].X    = stars[i].X;
+    mychip[0].stars[i].Y    = stars[i].Y;
+    mychip[0].stars[i].Mag  = stars[i].M;
+    mychip[0].stars[i].dMag = stars[i].dM;
+  }    
+  return (TRUE);
+}
+
Index: /trunk/Ohana/src/mosastro/src/rtext.c
===================================================================
--- /trunk/Ohana/src/mosastro/src/rtext.c	(revision 3416)
+++ /trunk/Ohana/src/mosastro/src/rtext.c	(revision 3416)
@@ -0,0 +1,40 @@
+# include "mosastro.h"
+# define BYTES_STAR 66
+
+int rtext (Chip *mychip) {
+
+  int i, Nbytes, nbytes;
+  FILE *f;
+
+  f = fopen (mychip[0].file, "r");
+  if (f == NULL) {
+    fprintf (stderr, "ERROR: can't read data from %s\n", mychip[0].file);
+    return (FALSE);
+  }
+  fseek (f, mychip[0].header.size, SEEK_SET); 
+
+  /* find expected number of stars */
+  fits_scan (&mychip[0].header, "NSTARS", "%d", 1, &mychip[0].Nstars);
+  if (mychip[0].Nstars == 0) {
+    fprintf (stderr, "ERROR: can't get NSTARS from header for %s\n", mychip[0].file);
+    return (FALSE);
+  }
+  ALLOCATE (mychip[0].stars, StarData, mychip[0].Nstars);
+    
+  Nbytes = BYTES_STAR * mychip[0].Nstars;
+  ALLOCATE (mychip[0].buffer, char, Nbytes + 1);
+  nbytes = Fread (mychip[0].buffer, 1, Nbytes, f, "char");
+  if (nbytes != Nbytes) { exit (1); }
+  mychip[0].Nbuffer = Nbytes;
+  mychip[0].FITS = FALSE;
+
+  for (i = 0; i < mychip[0].Nstars; i++) {
+    bzero (&mychip[0].stars[i], sizeof(StarData));
+    dparse (&mychip[0].stars[i].X,    1, &mychip[0].buffer[i*BYTES_STAR]);
+    dparse (&mychip[0].stars[i].Y,    2, &mychip[0].buffer[i*BYTES_STAR]);
+    dparse (&mychip[0].stars[i].Mag,  3, &mychip[0].buffer[i*BYTES_STAR]);
+    dparse (&mychip[0].stars[i].dMag, 4, &mychip[0].buffer[i*BYTES_STAR]);
+    mychip[0].stars[i].dMag *= 0.001;  /* millimag errors stored in file */
+  }
+  return (TRUE);
+}
Index: /trunk/Ohana/src/mosastro/src/wfits.c
===================================================================
--- /trunk/Ohana/src/mosastro/src/wfits.c	(revision 3416)
+++ /trunk/Ohana/src/mosastro/src/wfits.c	(revision 3416)
@@ -0,0 +1,49 @@
+# include "mosastro.h"
+
+void wfits (char *filename, Stars *stars, int Nstars, Header *header) {
+
+  int i;
+  Matrix matrix;
+  Header theader;
+  FTable table;
+
+  header[0].extend = TRUE;
+  header[0].Naxes = 0;
+  fits_modify (header, "NAXIS",   "%d", 1, 0);
+  fits_modify (header, "EXTEND",  "%t", 1, TRUE);
+  fits_modify (header, "NEXTEND", "%d", 1, 1);
+
+  /* add in some keywords to specify the datatype & software version? */
+
+  /* create (empty) data matrix */
+  fits_create_matrix (header, &matrix);
+    
+  /* create bintable header */
+  fits_create_table_header (&theader, "BINTABLE", "SMPFILE");
+
+  /* define bintable layout */
+  fits_define_bintable_column (&theader, "E",    "X_PIX",      "x coord",              "pixels",                         1.0, 0.0); 
+  fits_define_bintable_column (&theader, "E",    "Y_PIX",      "y coord",              "pixels",                         1.0, 0.0); 
+  fits_define_bintable_column (&theader, "E",    "MAG_RAW",    "inst mags",            "mags",                           1.0, 0.0); 
+  fits_define_bintable_column (&theader, "E",    "MAG_ERR",    "inst mag error",       "mags",                           1.0, 0.0); 
+  fits_define_bintable_column (&theader, "E",    "MAG_GAL",    "galaxy mag",           "mags",                           1.0, 0.0); 
+  fits_define_bintable_column (&theader, "E",    "MAG_AP",     "aperture mag",         "mags",                           1.0, 0.0); 
+  fits_define_bintable_column (&theader, "E",    "LOG_SKY",    "log-10 of sky",        "mags",                           1.0, 0.0); 
+  fits_define_bintable_column (&theader, "E",    "FWHM_X",     "semi-major",           "mags",                           1.0, 0.0); 
+  fits_define_bintable_column (&theader, "E",    "FWHM_Y",     "semi-minor",           "mags",                           1.0, 0.0); 
+  fits_define_bintable_column (&theader, "E",    "THETA",      "ellipse angle",        "mags",                           1.0, 0.0); 
+  fits_define_bintable_column (&theader, "B",    "DOPHOT",     "dophot type",          "",                              1.0, 0.0);
+  fits_define_bintable_column (&theader, "3A",   "DUMMY",      "padding",              "",                              1.0, 0.0);
+
+  /* create table, add data values */
+  fits_create_table (&theader, &table);
+
+  ConvertStars (stars, sizeof (Stars), Nstars);
+  fits_add_rows (&table, (char *) stars, Nstars, sizeof (Stars));
+
+  fits_write_header  (filename, header);
+  fits_write_matrix  (filename, &matrix);
+  fits_write_Theader (filename, &theader);
+  fits_write_table   (filename, &table);
+  return;
+}
Index: /trunk/Ohana/src/mosastro/src/wstars.c
===================================================================
--- /trunk/Ohana/src/mosastro/src/wstars.c	(revision 3415)
+++ /trunk/Ohana/src/mosastro/src/wstars.c	(revision 3416)
@@ -6,14 +6,18 @@
   FILE *g;
 
-  g = fopen (filename, "w");
-  if (g == (FILE *) NULL) {
-    fprintf (stderr, "ERROR: can't create output file %s\n", filename);
-    exit (1);
-  }
+  if (data[0].FITS) {
+    wfits (filename, (Stars *) data[0].buffer, data[0].Nstars, &data[0].header);
+  } else {
 
-  fwrite (data[0].header.buffer, 1, data[0].header.size, g);
-  fwrite (data[0].buffer, 1, data[0].Nbuffer, g);
-  fclose (g);
+    g = fopen (filename, "w");
+    if (g == (FILE *) NULL) {
+      fprintf (stderr, "ERROR: can't create output file %s\n", filename);
+      exit (1);
+    }
 
+    fwrite (data[0].header.buffer, 1, data[0].header.size, g);
+    fwrite (data[0].buffer, 1, data[0].Nbuffer, g);
+    fclose (g);
+  }  
 }
 
